"""ekb agent rules:列出企业所有审核规则(表现层) 不导入 api 层;通过 services.list_rules_cmd 获取数据。 EkbError 由 CLI 入口全局 handler 统一处理,本层不 catch。 """ import typer from rich.console import Console from rich.table import Table from rich import print as rprint from ekb_cli.services import list_rules_cmd console = Console() DESC_MAX = 20 PURPOSE_MAX = 12 def cmd_rule_list( full: bool = typer.Option(False, "--full", "-f", help="显示完整内容,不截断描述字段"), level: str = typer.Option(None, "--level", "-l", help="按风险等级筛选:严重 / 一般"), ): """列出企业所有审核规则""" page, items = list_rules_cmd(level=level) if not items: rprint("[dim](暂无规则)[/dim]") return title = ( f"审核规则(共 {page.totalItems} 条,筛选:{level})" if level else f"审核规则(共 {page.totalItems} 条)" ) table = Table(title=title, 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_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.code, r.description, r.purpose, level_str, str(len(r.agents)), update_date, r.id, ) console.print(table) if any_truncated: rprint(f"[yellow]⚠ 部分字段已截断,完整规则文本共 {total_chars} 字符[/yellow]") rprint("[dim]使用 --full 参数获取完整内容:ekb agent rules --full[/dim]")