diff --git a/CLAUDE.md b/CLAUDE.md index 580b2ac..326574b 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -34,11 +34,12 @@ src/ekb_cli/ ## 已知 API 路径 ``` -GET/POST /api/engine/hab/agent/list -GET/POST /api/engine/hab/agent/detail -POST /api/engine/hab/agent/save -GET /api/engine/hab/agent/rule/list -POST /api/engine/hab/agent/rule/save +GET /api/engine/hab/agent/list +GET /api/engine/hab/agent/detail +POST /api/engine/hab/agent/save +GET /api/engine/hab/agent/rule/list +POST /api/engine/hab/agent/rule/save +POST /api/engine/hab/agent/rule/update ``` > 详细请求/响应结构参考 api/agent.py 和 Chrome DevTools 网络请求记录。 diff --git a/src/ekb_cli/api/agent.py b/src/ekb_cli/api/agent.py index fa6ce67..84e8167 100644 --- a/src/ekb_cli/api/agent.py +++ b/src/ekb_cli/api/agent.py @@ -13,3 +13,17 @@ def get_agent(client: EkbClient, agent_id: str) -> dict: def save_agent(client: EkbClient, payload: dict) -> dict: return client.post("/engine/hab/agent/save", payload) + + +def list_rules(client: EkbClient, page: int = 1, size: int = 1000) -> dict: + return client.get("/engine/hab/agent/rule/list", page=page, size=size) + + +def save_rule(client: EkbClient, payload: dict) -> dict: + """新建规则:payload 包含 purpose / description / level""" + return client.post("/engine/hab/agent/rule/save", payload) + + +def update_rule(client: EkbClient, payload: dict) -> dict: + """更新规则:payload 包含 id / code / purpose / description / level""" + return client.post("/engine/hab/agent/rule/update", payload) diff --git a/src/ekb_cli/commands/agent/__init__.py b/src/ekb_cli/commands/agent/__init__.py index 4f76133..c851bdf 100644 --- a/src/ekb_cli/commands/agent/__init__.py +++ b/src/ekb_cli/commands/agent/__init__.py @@ -5,6 +5,7 @@ from ekb_cli.commands.agent.list import cmd_list from ekb_cli.commands.agent.detail import cmd_detail from ekb_cli.commands.agent.logs import cmd_logs from ekb_cli.commands.agent.update import cmd_update +from ekb_cli.commands.agent.rule_list import cmd_rule_list app = typer.Typer(help="智能审核 Agent 管理") @@ -12,3 +13,4 @@ app.command("list")(cmd_list) app.command("get")(cmd_detail) app.command("update")(cmd_update) app.command("logs")(cmd_logs) +app.command("rules")(cmd_rule_list) diff --git a/src/ekb_cli/commands/agent/rule_list.py b/src/ekb_cli/commands/agent/rule_list.py new file mode 100644 index 0000000..fffde45 --- /dev/null +++ b/src/ekb_cli/commands/agent/rule_list.py @@ -0,0 +1,72 @@ +"""ekb agent rules:列出企业所有审核规则""" + +import typer +from rich.console import Console +from rich.table import Table +from rich import print as rprint + +from ekb_cli.api import get_client +from ekb_cli.api.agent import list_rules + +console = Console() + +DESC_MAX = 20 +PURPOSE_MAX = 12 + + +def cmd_rule_list( + full: bool = typer.Option(False, "--full", "-f", help="显示完整内容,不截断描述字段"), +): + """列出企业所有审核规则""" + with get_client() as client: + resp = list_rules(client) + + data = resp.get("data", {}) + items = data.get("items", []) + total = data.get("totalItems", 0) + + if not items: + rprint("[dim](暂无规则)[/dim]") + return + + table = Table(title=f"审核规则(共 {total} 条)", show_lines=True) + table.add_column("规则编码", style="dim", no_wrap=True) + if full: + table.add_column("规则描述") + table.add_column("适用场景") + else: + table.add_column("规则描述", max_width=DESC_MAX, no_wrap=True) + table.add_column("适用场景", max_width=PURPOSE_MAX, no_wrap=True) + table.add_column("风险等级", no_wrap=True) + table.add_column("关联Agent", no_wrap=True) + table.add_column("更新日期", style="dim", no_wrap=True) + table.add_column("ID", style="dim", no_wrap=True) + + any_truncated = False + total_chars = 0 + for r in items: + level = r.get("level", "") + level_str = f"[red]{level}[/red]" if level == "严重" else f"[yellow]{level}[/yellow]" + update_date = (r.get("updateTime", "") or "")[:10] + desc = r.get("description", "") or "" + purpose = r.get("purpose", "") or "" + total_chars += len(desc) + len(purpose) + if not full and (len(desc) > DESC_MAX or len(purpose) > PURPOSE_MAX): + any_truncated = True + agents = r.get("agents", []) + agent_str = str(len(agents)) if agents else "0" + table.add_row( + r.get("code", ""), + desc, + purpose, + level_str, + agent_str, + update_date, + r.get("id", ""), + ) + + console.print(table) + + if any_truncated: + rprint(f"[yellow]⚠ 部分字段已截断,完整规则文本共 {total_chars} 字符[/yellow]") + rprint("[dim]使用 --full 参数获取完整内容:ekb agent rules --full[/dim]") diff --git a/src/ekb_cli/commands/help.py b/src/ekb_cli/commands/help.py index 4182fdc..2f23596 100644 --- a/src/ekb_cli/commands/help.py +++ b/src/ekb_cli/commands/help.py @@ -22,6 +22,7 @@ COMMAND_TREE = { "list": "列出所有 Agent", "get": "查看 Agent 详情 → ekb agent get --help", "update": "修改 Agent 配置 → ekb agent update --help", + "rules": "列出所有审核规则 → ekb agent rules --help", "logs": "查看审核日志", }, }