- 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 命令树同步更新
57 lines
2.2 KiB
Python
57 lines
2.2 KiB
Python
"""sealai knowledge-create:新建知识文档
|
||
|
||
表现层:交互式输入 + rich 输出。
|
||
业务逻辑委托给 services.sealai.knowledge_service。
|
||
"""
|
||
|
||
import typer
|
||
from rich import print as rprint
|
||
|
||
from ekb_cli.services.sealai import get_sealai_client, add_article
|
||
|
||
|
||
def cmd_knowledge_create(
|
||
corp_id: str = typer.Option(None, "--corp", "-c", help="指定企业 corpId,默认使用当前企业"),
|
||
title: str = typer.Option(None, "--title", "-t", help="文档标题"),
|
||
purpose: str = typer.Option(None, "--purpose", "-p", help="适用说明"),
|
||
content: str = typer.Option(None, "--content", help="正文内容(Markdown),不传则交互输入"),
|
||
content_type: str = typer.Option("policy", "--type", help="内容类型,默认 policy"),
|
||
status: str = typer.Option("published", "--status", help="状态:published / draft"),
|
||
scenarios: list[str] = typer.Option([], "--scenario", "-s", help="适用场景(可多次传),如 -s audit -s fill"),
|
||
tags: list[str] = typer.Option([], "--tag", help="标签(可多次传),如 --tag __all__"),
|
||
):
|
||
"""新建知识文档"""
|
||
if title is None:
|
||
title = typer.prompt("文档标题").strip()
|
||
if purpose is None:
|
||
purpose = typer.prompt("适用说明").strip()
|
||
if not title or not purpose:
|
||
rprint("[red]标题和适用说明不能为空[/red]")
|
||
raise typer.Exit(1)
|
||
if content is None:
|
||
rprint("[dim]请输入正文内容(Markdown 格式),输入完毕后按 Enter 再输入 EOF 结束:[/dim]")
|
||
lines = []
|
||
try:
|
||
while True:
|
||
line = input()
|
||
if line == "EOF":
|
||
break
|
||
lines.append(line)
|
||
except EOFError:
|
||
pass
|
||
content = "\n".join(lines)
|
||
|
||
client = get_sealai_client(corp_id=corp_id)
|
||
article = add_article(
|
||
client,
|
||
title=title,
|
||
purpose=purpose,
|
||
content=content,
|
||
content_type=content_type,
|
||
status=status,
|
||
scenarios=list(scenarios) or None,
|
||
tags=list(tags) or None,
|
||
)
|
||
rprint(f"[green]✓[/green] 文档已创建:[bold]{article.title}[/bold]")
|
||
rprint(f" ID:[dim]{article.id}[/dim]")
|