Files
hose-use/src/ekb_cli/commands/agent/detail.py
T
gitea c87dfa52a5 批量收口 commands/agent/* 边界:全部改用 _cmd 便捷函数
- rule_service.py 新增 list_rules_cmd / get_rule_cmd / create_rule_cmd
- services/__init__.py 导出以上三个 _cmd 函数
- commands/agent/ 下 8 个文件一律移除 `from ekb_cli.api import get_client`,
  改为调用 services 层对应的 _cmd 函数(自管 client 生命周期)
- 同步清理各文件中残留的局部 try/except EkbError,统一冒泡至全局 handler
- 验证:commands/ 目录内已无任何 api 层直接引用
2026-04-15 18:17:13 +08:00

88 lines
3.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""ekb agent get <id>:查看 Agent 详情(表现层)
不导入 api 层;通过 services.get_agent_cmd 获取数据。
"""
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.services import get_agent_cmd
console = Console()
STATE_MAP = {0: "[dim]草稿[/dim]", 1: "[green]已发布[/green]"}
MODE_MAP = {0: "全自动", 1: "半自动", 2: "辅助"}
DESC_MAX = 24
PURPOSE_MAX = 16
def cmd_detail(
agent_id: str = typer.Argument(..., help="Agent ID"),
full: bool = typer.Option(False, "--full", "-f", help="显示完整内容,不截断规则字段"),
):
"""查看 Agent 详情"""
d = get_agent_cmd(agent_id)
state = STATE_MAP.get(d.state, str(d.state))
mode = MODE_MAP.get(d.mode, str(d.mode))
if d.mode == 1:
mode += f"(抽检 {d.samplingRate}%)"
content = "\n".join([
f"[bold]ID[/bold] {d.id}",
f"[bold]名称[/bold] {d.name}",
f"[bold]状态[/bold] {state}",
f"[bold]模式[/bold] {mode}",
f"[bold]描述[/bold] {d.description or '(无)'}",
f"[bold]创建时间[/bold] {d.createTime}",
f"[bold]更新时间[/bold] {d.updateTime}",
"",
f"[bold cyan]明确同意[/bold cyan]\n{d.agreeTerms}",
"",
f"[bold red]明确驳回[/bold red]\n{d.rejectTerms}",
"",
f"[bold yellow]理应驳回,但可豁免(即备注同意)[/bold yellow]\n{d.exemptTerms}",
"",
f"[bold green]建议复核,转人工[/bold green]\n{d.reviewTerms}",
])
console.print(Panel(content, title=f"[bold]{d.name}[/bold]", expand=False))
if d.agentRule:
table = Table(title="关联规则", show_lines=True)
if full:
table.add_column("规则描述")
table.add_column("规则编码", style="dim", no_wrap=True)
table.add_column("适用场景")
table.add_column("风险等级", no_wrap=True)
table.add_column("更新日期", style="dim", no_wrap=True)
table.add_column("ID", style="dim", no_wrap=True)
else:
table.add_column("规则描述", max_width=DESC_MAX, no_wrap=True)
table.add_column("规则编码", style="dim", no_wrap=True)
table.add_column("适用场景", max_width=PURPOSE_MAX, no_wrap=True)
table.add_column("风险等级", 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 d.agentRule:
level_str = f"[red]{r.level}[/red]" if r.level == "严重" else f"[yellow]{r.level}[/yellow]"
update_date = r.updateTime[:10]
total_chars += len(r.description) + len(r.purpose)
if not full and (len(r.description) > DESC_MAX or len(r.purpose) > PURPOSE_MAX):
any_truncated = True
table.add_row(r.description, r.code, r.purpose, level_str, update_date, r.id)
console.print(table)
if any_truncated:
rprint(f"[yellow]⚠ 部分规则字段已截断,完整规则文本共 {total_chars} 字符[/yellow]")
rprint("[dim]使用 --full 参数获取完整内容:ekb agent get <id> --full[/dim]")
else:
rprint("[dim](未关联任何规则)[/dim]")