From c0419666ae630eee9495fba0422db9940c6131d2 Mon Sep 17 00:00:00 2001 From: yunyingk Date: Wed, 8 Apr 2026 00:41:24 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=EF=BC=9Aekb=20agent=20delete?= =?UTF-8?q?=20=E5=91=BD=E4=BB=A4=EF=BC=8C=E6=94=AF=E6=8C=81=E6=96=B9?= =?UTF-8?q?=E5=90=91=E9=94=AE=E9=80=89=E6=8B=A9=E7=A1=AE=E8=AE=A4=E5=92=8C?= =?UTF-8?q?=20AI=20=E5=8F=8B=E5=A5=BD=E6=96=87=E5=AD=97=E6=A8=A1=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pyproject.toml | 1 + src/ekb_cli/api/__init__.py | 1 + src/ekb_cli/api/agent.py | 6 ++ src/ekb_cli/commands/agent/README.agent.md | 13 ++-- src/ekb_cli/commands/agent/__init__.py | 3 + src/ekb_cli/commands/agent/delete.py | 72 ++++++++++++++++++++++ src/ekb_cli/commands/help.py | 1 + uv.lock | 35 +++++++++++ 8 files changed, 127 insertions(+), 5 deletions(-) create mode 100644 src/ekb_cli/commands/agent/delete.py diff --git a/pyproject.toml b/pyproject.toml index 31e36b0..d4b991f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -10,6 +10,7 @@ requires-python = ">=3.12" dependencies = [ "httpx[socks]>=0.28.1", "pydantic>=2.12.5", + "questionary>=2.1.1", "rich>=14.3.3", "typer>=0.24.1", ] diff --git a/src/ekb_cli/api/__init__.py b/src/ekb_cli/api/__init__.py index 647d427..2fe664f 100644 --- a/src/ekb_cli/api/__init__.py +++ b/src/ekb_cli/api/__init__.py @@ -62,6 +62,7 @@ from ekb_cli.api.agent import ( # noqa: E402 list_agents, get_agent, save_agent, + delete_agent, ) from ekb_cli.api.rule import ( # noqa: E402 list_rules, diff --git a/src/ekb_cli/api/agent.py b/src/ekb_cli/api/agent.py index c183b12..084d5ed 100644 --- a/src/ekb_cli/api/agent.py +++ b/src/ekb_cli/api/agent.py @@ -29,3 +29,9 @@ def save_agent(client: EkbClient, payload: dict) -> AgentDetail: raw = client.post("/engine/hab/agent/save", payload) resp = EkbResponse[AgentDetail].model_validate(raw) return resp.data + + +def delete_agent(client: EkbClient, agent_id: str) -> None: + """删除 Agent(state=2)""" + raw = client.get("/engine/hab/agent/active", id=agent_id, state=2) + EkbResponse[AgentDetail].model_validate(raw) diff --git a/src/ekb_cli/commands/agent/README.agent.md b/src/ekb_cli/commands/agent/README.agent.md index ecacf6c..2d917e3 100644 --- a/src/ekb_cli/commands/agent/README.agent.md +++ b/src/ekb_cli/commands/agent/README.agent.md @@ -10,12 +10,15 @@ | `1` | 半自动 | samplingRate 0–100,表示抽检比例 | | `2` | 辅助 | | -**state(启用状态)** +**state(Agent 状态)** -| 值 | 含义 | -|---|---| -| `0` | 启用 | -| `1` | 未启用(未关联审批流) | +| 值 | 含义 | 接口 | +|---|---|---| +| `0` | 启用(已关联审批流) | `GET /agent/active?state=0` | +| `1` | 未启用(未关联审批流) | `GET /agent/active?state=1` 或新建默认值 | +| `2` | 已删除 | `GET /agent/active?state=2`(不可恢复) | + +> `state` 通过 `/agent/active` 接口单独变更,与 `/agent/save` 无关。 ## 四类条款语义(对应前端「AI审核决策偏好」) diff --git a/src/ekb_cli/commands/agent/__init__.py b/src/ekb_cli/commands/agent/__init__.py index 44e7099..5db3c6d 100644 --- a/src/ekb_cli/commands/agent/__init__.py +++ b/src/ekb_cli/commands/agent/__init__.py @@ -11,6 +11,8 @@ from ekb_cli.commands.agent.rule_list import cmd_rule_list from ekb_cli.commands.agent.rule_detail import cmd_rule_detail from ekb_cli.commands.agent.rule_create import cmd_rule_create +from ekb_cli.commands.agent.delete import cmd_delete + app = typer.Typer(help="智能审核 Agent 管理") app.command("list")(cmd_list) @@ -18,6 +20,7 @@ app.command("get")(cmd_detail) app.command("create")(cmd_create) app.command("copy")(cmd_copy) app.command("update")(cmd_update) +app.command("delete")(cmd_delete) app.command("logs")(cmd_logs) app.command("rules")(cmd_rule_list) app.command("rule-get")(cmd_rule_detail) diff --git a/src/ekb_cli/commands/agent/delete.py b/src/ekb_cli/commands/agent/delete.py new file mode 100644 index 0000000..4c98a5b --- /dev/null +++ b/src/ekb_cli/commands/agent/delete.py @@ -0,0 +1,72 @@ +"""ekb agent delete :删除智能审核 Agent(需二次确认)""" + +import sys +import questionary +import typer +from rich import print as rprint +from rich.panel import Panel +from rich.console import Console + +from ekb_cli.api import get_client, get_agent, delete_agent + +console = Console() + +_CANCEL = "取消(默认安全)" +_CONFIRM = "确认删除" + + +def cmd_delete( + agent_id: str = typer.Argument(..., help="要删除的 Agent ID"), + yes: bool = typer.Option(False, "--yes", "-y", help="跳过确认直接删除(危险)"), + ai: bool = typer.Option(False, "--ai", help="AI/脚本模式:改用文字输入 y/N 确认,适合非交互式环境"), +): + """删除 Agent(不可恢复,操作前会展示 Agent 信息并要求确认)""" + with get_client() as client: + d = get_agent(client, agent_id) + if not d: + rprint("[red]未找到该 Agent[/red]") + raise typer.Exit(1) + + rule_count = len(d.agentRule) + console.print(Panel( + "\n".join([ + f"[bold]名称[/bold] {d.name}", + f"[bold]ID[/bold] [dim]{d.id}[/dim]", + f"[bold]描述[/bold] {d.description or '(无)'}", + f"[bold]关联规则[/bold] {rule_count} 条{'(规则本身不会被删除)' if rule_count else ''}", + f"[bold]更新时间[/bold] {d.updateTime}", + ]), + title="[bold red]⚠ 即将删除以下 Agent[/bold red]", + border_style="red", + expand=False, + )) + + if not yes: + if ai or not sys.stdin.isatty(): + # AI / 非交互式模式:文字输入 + rprint(f"确认删除 Agent「{d.name}」?输入 y 确认,其他任意键取消:", end=" ") + answer = input().strip().lower() + if answer != "y": + rprint("[dim]已取消[/dim]") + raise typer.Exit(0) + else: + # 人工交互模式:方向键选择 + choice = questionary.select( + f"确认删除 Agent「{d.name}」?", + choices=[_CANCEL, _CONFIRM], + default=_CANCEL, + style=questionary.Style([ + ("selected", "fg:#ff5555 bold"), + ("pointer", "fg:#ff5555 bold"), + ("highlighted", "fg:#ff5555"), + ("question", "bold"), + ]), + ).ask() + + if choice != _CONFIRM: + rprint("[dim]已取消[/dim]") + raise typer.Exit(0) + + delete_agent(client, agent_id) + + rprint(f"[green]✓ Agent「{d.name}」已删除[/green]") diff --git a/src/ekb_cli/commands/help.py b/src/ekb_cli/commands/help.py index 6a489a3..9f1223f 100644 --- a/src/ekb_cli/commands/help.py +++ b/src/ekb_cli/commands/help.py @@ -24,6 +24,7 @@ COMMAND_TREE = { "create": "新建 Agent → ekb agent create --help", "copy": "复制 Agent(名称自动加「副本」) → ekb agent copy ", "update": "修改 Agent 配置 → ekb agent update --help", + "delete": "删除 Agent(有二次确认) → ekb agent delete ", "rules": "列出所有审核规则 → ekb agent rules --help", "rule-get": "查看规则详情 → ekb agent rule-get ", "rule-create": "新建审核规则 → ekb agent rule-create --help", diff --git a/uv.lock b/uv.lock index 602c41b..2e78572 100644 --- a/uv.lock +++ b/uv.lock @@ -70,6 +70,7 @@ source = { editable = "." } dependencies = [ { name = "httpx", extra = ["socks"] }, { name = "pydantic" }, + { name = "questionary" }, { name = "rich" }, { name = "typer" }, ] @@ -78,6 +79,7 @@ dependencies = [ requires-dist = [ { name = "httpx", extras = ["socks"], specifier = ">=0.28.1" }, { name = "pydantic", specifier = ">=2.12.5" }, + { name = "questionary", specifier = ">=2.1.1" }, { name = "rich", specifier = ">=14.3.3" }, { name = "typer", specifier = ">=0.24.1" }, ] @@ -154,6 +156,18 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/b3/38/89ba8ad64ae25be8de66a6d463314cf1eb366222074cfda9ee839c56a4b4/mdurl-0.1.2-py3-none-any.whl", hash = "sha256:84008a41e51615a49fc9966191ff91509e3c40b939176e643fd50a5c2196b8f8", size = 9979, upload-time = "2022-08-14T12:40:09.779Z" }, ] +[[package]] +name = "prompt-toolkit" +version = "3.0.52" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "wcwidth" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/a1/96/06e01a7b38dce6fe1db213e061a4602dd6032a8a97ef6c1a862537732421/prompt_toolkit-3.0.52.tar.gz", hash = "sha256:28cde192929c8e7321de85de1ddbe736f1375148b02f2e17edd840042b1be855", size = 434198, upload-time = "2025-08-27T15:24:02.057Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/84/03/0d3ce49e2505ae70cf43bc5bb3033955d2fc9f932163e84dc0779cc47f48/prompt_toolkit-3.0.52-py3-none-any.whl", hash = "sha256:9aac639a3bbd33284347de5ad8d68ecc044b91a762dc39b7c21095fcd6a19955", size = 391431, upload-time = "2025-08-27T15:23:59.498Z" }, +] + [[package]] name = "pydantic" version = "2.12.5" @@ -249,6 +263,18 @@ wheels = [ { url = "https://files.pythonhosted.org/packages/f4/7e/a72dd26f3b0f4f2bf1dd8923c85f7ceb43172af56d63c7383eb62b332364/pygments-2.20.0-py3-none-any.whl", hash = "sha256:81a9e26dd42fd28a23a2d169d86d7ac03b46e2f8b59ed4698fb4785f946d0176", size = 1231151, upload-time = "2026-03-29T13:29:30.038Z" }, ] +[[package]] +name = "questionary" +version = "2.1.1" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "prompt-toolkit" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/f6/45/eafb0bba0f9988f6a2520f9ca2df2c82ddfa8d67c95d6625452e97b204a5/questionary-2.1.1.tar.gz", hash = "sha256:3d7e980292bb0107abaa79c68dd3eee3c561b83a0f89ae482860b181c8bd412d", size = 25845, upload-time = "2025-08-28T19:00:20.851Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/3c/26/1062c7ec1b053db9e499b4d2d5bc231743201b74051c973dadeac80a8f43/questionary-2.1.1-py3-none-any.whl", hash = "sha256:a51af13f345f1cdea62347589fbb6df3b290306ab8930713bfae4d475a7d4a59", size = 36753, upload-time = "2025-08-28T19:00:19.56Z" }, +] + [[package]] name = "rich" version = "14.3.3" @@ -315,3 +341,12 @@ sdist = { url = "https://files.pythonhosted.org/packages/55/e3/70399cb7dd41c10ac wheels = [ { url = "https://files.pythonhosted.org/packages/dc/9b/47798a6c91d8bdb567fe2698fe81e0c6b7cb7ef4d13da4114b41d239f65d/typing_inspection-0.4.2-py3-none-any.whl", hash = "sha256:4ed1cacbdc298c220f1bd249ed5287caa16f34d44ef4e9c3d0cbad5b521545e7", size = 14611, upload-time = "2025-10-01T02:14:40.154Z" }, ] + +[[package]] +name = "wcwidth" +version = "0.6.0" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/35/a2/8e3becb46433538a38726c948d3399905a4c7cabd0df578ede5dc51f0ec2/wcwidth-0.6.0.tar.gz", hash = "sha256:cdc4e4262d6ef9a1a57e018384cbeb1208d8abbc64176027e2c2455c81313159", size = 159684, upload-time = "2026-02-06T19:19:40.919Z" } +wheels = [ + { url = "https://files.pythonhosted.org/packages/68/5a/199c59e0a824a3db2b89c5d2dade7ab5f9624dbf6448dc291b46d5ec94d3/wcwidth-0.6.0-py3-none-any.whl", hash = "sha256:1a3a1e510b553315f8e146c54764f4fb6264ffad731b3d78088cdb1478ffbdad", size = 94189, upload-time = "2026-02-06T19:19:39.646Z" }, +]