- knowledge_list.py:列表展示,支持 --scenario 过滤、--detail 展开正文 Markdown - knowledge_get.py:单篇详情,元信息表格 + Panel Markdown 渲染 - knowledge_create.py:新建文档,支持交互式输入正文(EOF 结束) - knowledge_update.py:更新文档,--enable/--disable 互斥开关控制启停状态 - 注册 4 条新命令到 commands/sealai/__init__.py - help.py 命令树同步更新
71 lines
2.7 KiB
Python
71 lines
2.7 KiB
Python
"""sealai knowledge-list:查看知识文档列表
|
|
|
|
表现层:只负责 rich 格式化输出。
|
|
业务逻辑委托给 services.sealai.knowledge_service。
|
|
"""
|
|
|
|
import typer
|
|
from datetime import datetime, timezone
|
|
from rich.console import Console
|
|
from rich.table import Table
|
|
from rich.panel import Panel
|
|
from rich.markdown import Markdown
|
|
from rich import print as rprint
|
|
|
|
from ekb_cli.services.sealai import get_sealai_client, get_articles
|
|
|
|
console = Console()
|
|
|
|
_SCENARIO_LABEL = {
|
|
"audit": "审核",
|
|
"fill": "填单",
|
|
}
|
|
_STATUS_LABEL = {
|
|
"published": "[green]已发布[/green]",
|
|
"draft": "[yellow]草稿[/yellow]",
|
|
}
|
|
|
|
|
|
def cmd_knowledge_list(
|
|
corp_id: str = typer.Option(None, "--corp", "-c", help="指定企业 corpId,默认使用当前企业"),
|
|
scenario: str = typer.Option(None, "--scenario", "-s", help="按场景过滤:audit / fill"),
|
|
limit: int = typer.Option(20, "--limit", "-n", help="返回数量上限"),
|
|
detail: bool = typer.Option(False, "--detail", help="展开每篇文档的正文内容"),
|
|
):
|
|
"""查看知识文档列表"""
|
|
scenarios = [scenario] if scenario else None
|
|
client = get_sealai_client(corp_id=corp_id)
|
|
data = get_articles(client, scenarios=scenarios, limit=limit)
|
|
|
|
if not data.articles:
|
|
rprint("[yellow]暂无知识文档[/yellow]")
|
|
raise typer.Exit()
|
|
|
|
if not detail:
|
|
table = Table(title=f"知识文档(共 {data.total or len(data.articles)} 篇)", show_lines=True)
|
|
table.add_column("ID", style="dim", no_wrap=True)
|
|
table.add_column("标题")
|
|
table.add_column("适用说明", max_width=30)
|
|
table.add_column("场景")
|
|
table.add_column("状态")
|
|
table.add_column("更新时间")
|
|
|
|
for a in data.articles:
|
|
scenes = " / ".join(_SCENARIO_LABEL.get(s, s) for s in a.scenarios)
|
|
status = _STATUS_LABEL.get(a.status, a.status)
|
|
ts = datetime.fromtimestamp(a.updatedAt / 1000, tz=timezone.utc).strftime("%Y-%m-%d %H:%M") if a.updatedAt else "-"
|
|
table.add_row(a.id[:8] + "…", a.title, a.purpose, scenes, status, ts)
|
|
|
|
console.print(table)
|
|
console.print("[dim]查看正文内容:ekb sealai knowledge-list --detail[/dim]")
|
|
console.print("[dim]查看单篇详情:ekb sealai knowledge-get <id>[/dim]")
|
|
else:
|
|
for a in data.articles:
|
|
scenes = " / ".join(_SCENARIO_LABEL.get(s, s) for s in a.scenarios)
|
|
status = _STATUS_LABEL.get(a.status, a.status)
|
|
console.print(Panel(
|
|
Markdown(a.content),
|
|
title=f"[bold]{a.title}[/bold] {status} [dim]{scenes}[/dim]",
|
|
subtitle=f"[dim]ID: {a.id}[/dim]",
|
|
))
|