SealAI 知识文档 CLI 层补全(knowledge-list/get/create/update)
- 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 命令树同步更新
This commit is contained in:
1 parent
630633889b
commit
9cbbbb56a9
8 files changed
+541
-7
No files matched your search
@@ -0,0 +1,143 @@
|
||||
"""SealAI 知识文档业务逻辑层。
|
||||
|
||||
管理知识文档的增删改查。
|
||||
知识文档是 AI 审批的训练素材,支持按场景(audit/fill)分类,
|
||||
内容以 Markdown 格式存储,前端可渲染。
|
||||
"""
|
||||
|
||||
from typing import Optional
|
||||
from ekb_cli.api.sealai.client import SealAiClient
|
||||
from ekb_cli.api.sealai.knowledge_api import (
|
||||
list_articles,
|
||||
get_article,
|
||||
create_article,
|
||||
patch_article,
|
||||
)
|
||||
from ekb_cli.api.sealai.schemas import (
|
||||
KnowledgeArticle,
|
||||
KnowledgeArticleListData,
|
||||
)
|
||||
|
||||
# 合法场景值
|
||||
_VALID_SCENARIOS = {"audit", "fill"}
|
||||
# 合法状态值
|
||||
_VALID_STATUSES = {"published", "draft"}
|
||||
|
||||
|
||||
def get_articles(
|
||||
client: SealAiClient,
|
||||
*,
|
||||
scenarios: Optional[list[str]] = None,
|
||||
offset: int = 0,
|
||||
limit: int = 20,
|
||||
sort_by: str = "createdAt",
|
||||
sort_order: str = "desc",
|
||||
) -> KnowledgeArticleListData:
|
||||
"""查询知识文档列表。
|
||||
|
||||
Args:
|
||||
scenarios: 按场景过滤,可选值 "audit" | "fill",None 表示不过滤
|
||||
offset: 分页偏移量(默认 0)
|
||||
limit: 每页数量(默认 20,建议不超过 100)
|
||||
sort_by: 排序字段(默认 createdAt)
|
||||
sort_order: "asc" | "desc"(默认 desc,最新在前)
|
||||
"""
|
||||
if scenarios:
|
||||
_validate_scenarios(scenarios)
|
||||
return list_articles(
|
||||
client,
|
||||
scenarios=scenarios,
|
||||
offset=offset,
|
||||
limit=limit,
|
||||
sort_by=sort_by,
|
||||
sort_order=sort_order,
|
||||
)
|
||||
|
||||
|
||||
def get_article_detail(client: SealAiClient, article_id: str) -> KnowledgeArticle:
|
||||
"""查询单篇知识文档详情。"""
|
||||
return get_article(client, article_id)
|
||||
|
||||
|
||||
def add_article(
|
||||
client: SealAiClient,
|
||||
*,
|
||||
title: str,
|
||||
purpose: str,
|
||||
content: str,
|
||||
content_type: str = "policy",
|
||||
status: str = "published",
|
||||
scenarios: Optional[list[str]] = None,
|
||||
tags: Optional[list[str]] = None,
|
||||
) -> KnowledgeArticle:
|
||||
"""新建知识文档。
|
||||
|
||||
Args:
|
||||
title: 文档标题
|
||||
purpose: 适用说明
|
||||
content: 正文(Markdown 格式)
|
||||
content_type: 内容类型,默认 "policy"
|
||||
status: "published" | "draft",默认 "published"
|
||||
scenarios: 适用场景,如 ["audit", "fill"]
|
||||
tags: 标签列表,"__all__" 表示全部适用
|
||||
"""
|
||||
if status not in _VALID_STATUSES:
|
||||
from ekb_cli.errors import EkbError
|
||||
raise EkbError(f"status 无效:{status!r},合法值为 {sorted(_VALID_STATUSES)}", code=422)
|
||||
if scenarios:
|
||||
_validate_scenarios(scenarios)
|
||||
return create_article(
|
||||
client,
|
||||
title=title,
|
||||
purpose=purpose,
|
||||
content=content,
|
||||
content_type=content_type,
|
||||
status=status,
|
||||
scenarios=scenarios,
|
||||
tags=tags,
|
||||
)
|
||||
|
||||
|
||||
def edit_article(
|
||||
client: SealAiClient,
|
||||
article_id: str,
|
||||
*,
|
||||
title: Optional[str] = None,
|
||||
purpose: Optional[str] = None,
|
||||
content: Optional[str] = None,
|
||||
content_type: Optional[str] = None,
|
||||
status: Optional[str] = None,
|
||||
enabled: Optional[bool] = None,
|
||||
scenarios: Optional[list[str]] = None,
|
||||
tags: Optional[list[str]] = None,
|
||||
) -> KnowledgeArticle:
|
||||
"""更新知识文档(只传需要改动的字段)。"""
|
||||
if status is not None and status not in _VALID_STATUSES:
|
||||
from ekb_cli.errors import EkbError
|
||||
raise EkbError(f"status 无效:{status!r},合法值为 {sorted(_VALID_STATUSES)}", code=422)
|
||||
if scenarios is not None:
|
||||
_validate_scenarios(scenarios)
|
||||
return patch_article(
|
||||
client,
|
||||
article_id,
|
||||
title=title,
|
||||
purpose=purpose,
|
||||
content=content,
|
||||
content_type=content_type,
|
||||
status=status,
|
||||
enabled=enabled,
|
||||
scenarios=scenarios,
|
||||
tags=tags,
|
||||
)
|
||||
|
||||
|
||||
# ── 内部校验 ──────────────────────────────────────────────────────────────────
|
||||
|
||||
def _validate_scenarios(scenarios: list[str]) -> None:
|
||||
invalid = set(scenarios) - _VALID_SCENARIOS
|
||||
if invalid:
|
||||
from ekb_cli.errors import EkbError
|
||||
raise EkbError(
|
||||
f"scenarios 包含无效值:{sorted(invalid)},合法值为 {sorted(_VALID_SCENARIOS)}",
|
||||
code=422,
|
||||
)
|
||||
Reference in new issue
Block a user