新增:ekb agent delete 命令,支持方向键选择确认和 AI 友好文字模式

This commit is contained in:
gitea committed 2026-04-08 00:41:24 +08:00
1 parent 4466af7726
commit c0419666ae
8 files changed
+127 -5

No files matched your search

+1
View File
@@ -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,
+6
View File
@@ -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)
+8 -5
View File
@@ -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审核决策偏好」)
+3
View File
@@ -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)
+72
View File
@@ -0,0 +1,72 @@
"""ekb agent delete <id>:删除智能审核 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]")
+1
View File
@@ -24,6 +24,7 @@ COMMAND_TREE = {
"create": "新建 Agent → ekb agent create --help",
"copy": "复制 Agent(名称自动加「副本」) → ekb agent copy <id>",
"update": "修改 Agent 配置 → ekb agent update <id> --help",
"delete": "删除 Agent(有二次确认) → ekb agent delete <id>",
"rules": "列出所有审核规则 → ekb agent rules --help",
"rule-get": "查看规则详情 → ekb agent rule-get <id或编码>",
"rule-create": "新建审核规则 → ekb agent rule-create --help",