diff --git a/src/ekb_cli/commands/agent/__init__.py b/src/ekb_cli/commands/agent/__init__.py index c851bdf..670e430 100644 --- a/src/ekb_cli/commands/agent/__init__.py +++ b/src/ekb_cli/commands/agent/__init__.py @@ -6,6 +6,7 @@ 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 +from ekb_cli.commands.agent.rule_detail import cmd_rule_detail app = typer.Typer(help="智能审核 Agent 管理") @@ -14,3 +15,4 @@ app.command("get")(cmd_detail) app.command("update")(cmd_update) 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/rule_detail.py b/src/ekb_cli/commands/agent/rule_detail.py new file mode 100644 index 0000000..490e6d1 --- /dev/null +++ b/src/ekb_cli/commands/agent/rule_detail.py @@ -0,0 +1,63 @@ +"""ekb agent rule-get:查看单条规则详情(含关联 Agent 列表)""" + +import typer +from rich.console import Console +from rich.panel import Panel +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() + + +def cmd_rule_detail( + rule_ref: str = typer.Argument(..., help="规则 ID 或编码(如 2040826... 或 #0001 或 0001)"), +): + """查看单条规则详情(含关联 Agent 列表)""" + with get_client() as client: + resp = list_rules(client) + + items = resp.get("data", {}).get("items", []) + + # 自适应:长字符串按 ID 匹配,短字符串按编码匹配(补井号) + if len(rule_ref) > 10: + rule = next((r for r in items if r.get("id") == rule_ref), None) + else: + code = rule_ref if rule_ref.startswith("#") else f"#{rule_ref}" + rule = next((r for r in items if r.get("code") == code), None) + + if not rule: + rprint(f"[red]未找到规则:{rule_ref}[/red]") + raise typer.Exit(1) + + level = rule.get("level", "") + level_str = f"[red]{level}[/red]" if level == "严重" else f"[yellow]{level}[/yellow]" + + content = "\n".join([ + f"[bold]ID[/bold] {rule.get('id')}", + f"[bold]编码[/bold] {rule.get('code')}", + f"[bold]风险等级[/bold] {level_str}", + f"[bold]创建时间[/bold] {rule.get('createTime')}", + f"[bold]更新时间[/bold] {rule.get('updateTime')}", + "", + f"[bold]适用场景[/bold]\n{rule.get('purpose') or '(无)'}", + "", + f"[bold]规则描述[/bold]\n{rule.get('description') or '(无)'}", + ]) + + code_display = rule.get("code", "") + console.print(Panel(content, title=f"[bold]{code_display}[/bold]", expand=False)) + + # 关联 Agent 列表 + agents = rule.get("agents", []) + if agents: + table = Table(title=f"关联 Agent(共 {len(agents)} 个)", show_lines=True) + table.add_column("名称") + table.add_column("ID", style="dim", no_wrap=True) + for a in agents: + table.add_row(a.get("name", ""), a.get("id", "")) + console.print(table) + else: + rprint("[dim](未关联任何 Agent)[/dim]") diff --git a/src/ekb_cli/commands/agent/rule_list.py b/src/ekb_cli/commands/agent/rule_list.py index fffde45..6d0a78a 100644 --- a/src/ekb_cli/commands/agent/rule_list.py +++ b/src/ekb_cli/commands/agent/rule_list.py @@ -16,6 +16,7 @@ PURPOSE_MAX = 12 def cmd_rule_list( full: bool = typer.Option(False, "--full", "-f", help="显示完整内容,不截断描述字段"), + level: str = typer.Option(None, "--level", "-l", help="按风险等级筛选:严重 / 一般"), ): """列出企业所有审核规则""" with get_client() as client: @@ -25,11 +26,15 @@ def cmd_rule_list( items = data.get("items", []) total = data.get("totalItems", 0) + if level: + items = [r for r in items if r.get("level") == level] + if not items: rprint("[dim](暂无规则)[/dim]") return - table = Table(title=f"审核规则(共 {total} 条)", show_lines=True) + title = f"审核规则(共 {total} 条,筛选:{level})" if level else f"审核规则(共 {total} 条)" + table = Table(title=title, show_lines=True) table.add_column("规则编码", style="dim", no_wrap=True) if full: table.add_column("规则描述") diff --git a/src/ekb_cli/commands/help.py b/src/ekb_cli/commands/help.py index 2f23596..eb043ce 100644 --- a/src/ekb_cli/commands/help.py +++ b/src/ekb_cli/commands/help.py @@ -23,6 +23,7 @@ COMMAND_TREE = { "get": "查看 Agent 详情 → ekb agent get --help", "update": "修改 Agent 配置 → ekb agent update --help", "rules": "列出所有审核规则 → ekb agent rules --help", + "rule-get": "查看规则详情 → ekb agent rule-get ", "logs": "查看审核日志", }, }