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:
gitea committed 2026-04-15 00:06:44 +08:00
1 parent 630633889b
commit 9cbbbb56a9
8 files changed
+541 -7

No files matched your search

+132
View File
@@ -0,0 +1,132 @@
"""SealAI 知识文档 API 封装(传输层)。
已逆向确认端点:
GET /api/v1/knowledge/articles 文档列表(场景过滤 + 分页 + 排序)
POST /api/v1/knowledge/articles 新建文档
GET /api/v1/knowledge/articles/{id} 文档详情
PATCH /api/v1/knowledge/articles/{id} 更新文档(请求体为 {attributes: {...}})
注意:PATCH 请求体有特殊包装:{"attributes": {改动字段}},与 POST 直接传字段不同。
"""
from typing import Optional
from ekb_cli.api.sealai.client import SealAiClient
from ekb_cli.api.sealai.schemas import (
SealAiResponse,
KnowledgeArticle,
KnowledgeArticleListData,
)
def list_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)
sort_by: 排序字段(默认 createdAt)
sort_order: 排序方向,"asc" | "desc"(默认 desc)
"""
# 构造 filter[scenarios][N]=xxx 风格的 query 参数
params: dict = {
"offset": offset,
"limit": limit,
"sortBy": sort_by,
"sortOrder": sort_order,
}
if scenarios:
for i, s in enumerate(scenarios):
params[f"filter[scenarios][{i}]"] = s
raw = client.get("/api/v1/knowledge/articles", params=params)
return SealAiResponse[KnowledgeArticleListData].model_validate(raw).data
def get_article(client: SealAiClient, article_id: str) -> KnowledgeArticle:
"""获取单篇知识文档详情。"""
raw = client.get(f"/api/v1/knowledge/articles/{article_id}")
return SealAiResponse[KnowledgeArticle].model_validate(raw).data
def create_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__" 表示全适用
"""
body = {
"title": title,
"purpose": purpose,
"contentType": content_type,
"content": content,
"status": status,
"scenarios": scenarios or [],
"tags": tags or [],
}
raw = client.post("/api/v1/knowledge/articles", body)
return SealAiResponse[KnowledgeArticle].model_validate(raw).data
def patch_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:
"""更新知识文档(Patch 式,只传改动字段)。
注意:PATCH 请求体格式为 {"attributes": {改动字段}},
与 POST 的直传字段格式不同,这里已在传输层做好封装。
"""
attributes: dict = {}
if title is not None:
attributes["title"] = title
if purpose is not None:
attributes["purpose"] = purpose
if content is not None:
attributes["content"] = content
if content_type is not None:
attributes["contentType"] = content_type
if status is not None:
attributes["status"] = status
if enabled is not None:
attributes["enabled"] = enabled
if scenarios is not None:
attributes["scenarios"] = scenarios
if tags is not None:
attributes["tags"] = tags
raw = client.patch(f"/api/v1/knowledge/articles/{article_id}", {"attributes": attributes})
return SealAiResponse[KnowledgeArticle].model_validate(raw).data