SealAI 风格偏好 + 知识文档:API、服务层、HTTP 路由全部完成
风格偏好(preference):
- api/sealai/preference_api.py:GET/POST /api/v1/agent/ai-approval/config 封装
- services/sealai/preference_service.py:get_preference、save_preference、
update_decision_preferences(内部 GET-merge-POST,对调用方透明的 patch 语义)
- Server 新增 GET/POST /sealai/preference、PATCH /sealai/preference/decision
知识文档(knowledge):
- api/sealai/knowledge_api.py:list/get/create/patch_article 封装
注意 PATCH 请求体为 {attributes: {...}} 包装,已在传输层处理
- services/sealai/knowledge_service.py:get_articles、get_article_detail、
add_article、edit_article(含 enabled 启停、场景校验)
- Server 新增 GET/POST /sealai/knowledge/articles、
GET/PATCH /sealai/knowledge/articles/{id}
其他:
- client.py:get() 支持 params dict,解决 filter[scenarios][0] 含方括号参数问题
- schemas.py:新增 AiApprovalConfig、DecisionPreferences、
KnowledgeArticle、KnowledgeArticleListData 等模型
This commit is contained in:
1 parent
4d977bb704
commit
630633889b
6 files changed
+433
No files matched your search
@@ -14,6 +14,16 @@ from ekb_cli.api.sealai.rules_api import (
|
||||
list_rule_set_versions,
|
||||
publish_rule_set_version,
|
||||
)
|
||||
from ekb_cli.api.sealai.preference_api import (
|
||||
get_ai_approval_config,
|
||||
save_ai_approval_config,
|
||||
)
|
||||
from ekb_cli.api.sealai.knowledge_api import (
|
||||
list_articles,
|
||||
get_article,
|
||||
create_article,
|
||||
patch_article,
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
"SealAiClient",
|
||||
@@ -25,4 +35,11 @@ __all__ = [
|
||||
"delete_rule",
|
||||
"list_rule_set_versions",
|
||||
"publish_rule_set_version",
|
||||
"get_ai_approval_config",
|
||||
"save_ai_approval_config",
|
||||
"list_articles",
|
||||
"get_article",
|
||||
"create_article",
|
||||
"patch_article",
|
||||
]
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
"""SealAI 风格偏好 API 封装(传输层)。
|
||||
|
||||
已逆向确认端点:
|
||||
GET /api/v1/agent/ai-approval/config 获取 AI 审批配置(决策偏好 + 集成信息)
|
||||
POST /api/v1/agent/ai-approval/config 保存 AI 审批配置(整体替换)
|
||||
|
||||
注意:POST 是整体替换语义,需将完整配置对象回传,
|
||||
调用方(services 层)负责先 GET 再 merge,再 POST。
|
||||
"""
|
||||
|
||||
from ekb_cli.api.sealai.client import SealAiClient
|
||||
from ekb_cli.api.sealai.schemas import (
|
||||
SealAiResponse,
|
||||
AiApprovalConfig,
|
||||
AiApprovalSaveResult,
|
||||
)
|
||||
|
||||
|
||||
def get_ai_approval_config(client: SealAiClient) -> AiApprovalConfig:
|
||||
"""获取 AI 审批配置(风格偏好 + 集成系统信息)。"""
|
||||
raw = client.get("/api/v1/agent/ai-approval/config")
|
||||
return SealAiResponse[AiApprovalConfig].model_validate(raw).data
|
||||
|
||||
|
||||
def save_ai_approval_config(
|
||||
client: SealAiClient,
|
||||
config: AiApprovalConfig,
|
||||
) -> AiApprovalSaveResult:
|
||||
"""保存 AI 审批配置(整体替换)。
|
||||
|
||||
Args:
|
||||
config: 完整的配置对象,需包含 decisionPreferences、
|
||||
integrationSystem、integrations 等所有字段。
|
||||
Returns:
|
||||
AiApprovalSaveResult,含 success 标志。
|
||||
"""
|
||||
raw = client.post(
|
||||
"/api/v1/agent/ai-approval/config",
|
||||
config.model_dump(exclude_none=False),
|
||||
)
|
||||
return SealAiResponse[AiApprovalSaveResult].model_validate(raw).data
|
||||
@@ -185,3 +185,114 @@ class PublishVersionData(BaseModel):
|
||||
|
||||
class Config:
|
||||
extra = "allow"
|
||||
|
||||
|
||||
# ── 风格偏好(AI 审批配置) ───────────────────────────────────────────────────
|
||||
|
||||
class DecisionPreferences(BaseModel):
|
||||
"""AI 审批四类决策场景的提示词配置。
|
||||
|
||||
Attributes:
|
||||
explicitApprove: 明确通过的条件描述
|
||||
explicitReject: 明确拒绝的条件描述
|
||||
exemptApprove: 豁免通过的条件描述
|
||||
review: 转人工判断的条件描述
|
||||
"""
|
||||
explicitApprove: str
|
||||
explicitReject: str
|
||||
exemptApprove: str
|
||||
review: str
|
||||
|
||||
class Config:
|
||||
extra = "allow"
|
||||
|
||||
|
||||
class AiApprovalIntegration(BaseModel):
|
||||
"""单个集成系统配置(快报 / 飞书 / 钉钉等)。"""
|
||||
type: str # "hosecloud" 等
|
||||
mode: Optional[str] = None # "fully_automatic" | "semi_automatic"
|
||||
autoApprovalRate: Optional[int] = None
|
||||
|
||||
class Config:
|
||||
extra = "allow"
|
||||
|
||||
|
||||
class AiApprovalConfig(BaseModel):
|
||||
"""GET/POST /api/v1/agent/ai-approval/config 响应体 data 字段。
|
||||
|
||||
风格偏好页面的完整配置,包含决策偏好和集成系统信息。
|
||||
写入时需原样回传整个对象(整体替换语义)。
|
||||
"""
|
||||
decisionPreferences: DecisionPreferences
|
||||
integrationSystem: Optional[str] = None # "feishu" | "dingtalk" 等
|
||||
integrations: Optional[list[AiApprovalIntegration]] = None
|
||||
|
||||
class Config:
|
||||
extra = "allow"
|
||||
|
||||
|
||||
class AiApprovalSaveResult(BaseModel):
|
||||
"""POST /api/v1/agent/ai-approval/config 响应体 data 字段。"""
|
||||
success: bool
|
||||
|
||||
class Config:
|
||||
extra = "allow"
|
||||
|
||||
|
||||
# ── 知识文档 ──────────────────────────────────────────────────────────────────
|
||||
|
||||
class KnowledgeArticle(BaseModel):
|
||||
"""知识文档条目(新建/详情/列表元素)。
|
||||
|
||||
Attributes:
|
||||
id: 文档唯一 ID
|
||||
tenantId: 租户 ID
|
||||
title: 文档标题
|
||||
purpose: 适用说明(适用场景文字描述)
|
||||
contentType: 内容类型,如 "policy"
|
||||
content: 正文内容(Markdown 格式)
|
||||
status: "published" | "draft"
|
||||
enabled: 是否启用
|
||||
scenarios: 适用场景列表,如 ["audit", "fill"]
|
||||
tags: 标签列表,"__all__" 表示全适用
|
||||
category: 分类(可为 null)
|
||||
categoryTags: 分类标签列表
|
||||
createdBy: 创建人 ID
|
||||
createdByName: 创建人姓名
|
||||
createdAt: 创建时间(毫秒时间戳)
|
||||
updatedAt: 更新时间(毫秒时间戳)
|
||||
updatedBy: 最后更新人 ID
|
||||
updatedByName: 最后更新人姓名
|
||||
isDeleted: 是否已删除
|
||||
"""
|
||||
id: str
|
||||
tenantId: str
|
||||
title: str
|
||||
purpose: str
|
||||
contentType: str # "policy" 等
|
||||
content: str # Markdown 正文
|
||||
status: str # "published" | "draft"
|
||||
enabled: bool = True
|
||||
scenarios: list[str] = [] # ["audit", "fill"] 等
|
||||
tags: list[str] = [] # ["__all__"] 或具体标签
|
||||
category: Optional[str] = None
|
||||
categoryTags: list[str] = []
|
||||
createdBy: Optional[str] = None
|
||||
createdByName: Optional[str] = None
|
||||
createdAt: Optional[int] = None
|
||||
updatedAt: Optional[int] = None
|
||||
updatedBy: Optional[str] = None
|
||||
updatedByName: Optional[str] = None
|
||||
isDeleted: bool = False
|
||||
|
||||
class Config:
|
||||
extra = "allow"
|
||||
|
||||
|
||||
class KnowledgeArticleListData(BaseModel):
|
||||
"""GET /api/v1/knowledge/articles 响应体 data 字段。"""
|
||||
articles: list[KnowledgeArticle]
|
||||
total: Optional[int] = None
|
||||
|
||||
class Config:
|
||||
extra = "allow"
|
||||
@@ -8,11 +8,19 @@
|
||||
ekb sealai rule-update → PUT /sealai/rules/{rule_id}
|
||||
ekb sealai rule-delete → DELETE /sealai/rules/{rule_id}
|
||||
ekb sealai versions → GET /sealai/rule-set-versions
|
||||
—(Server 专属)— → GET /sealai/preference
|
||||
—(Server 专属)— → POST /sealai/preference
|
||||
—(Server 专属)— → PATCH /sealai/preference/decision
|
||||
—(Server 专属)— → GET /sealai/knowledge/articles
|
||||
—(Server 专属)— → POST /sealai/knowledge/articles
|
||||
—(Server 专属)— → GET /sealai/knowledge/articles/{article_id}
|
||||
—(Server 专属)— → PATCH /sealai/knowledge/articles/{article_id}
|
||||
|
||||
注意:SealAI 接口使用独立的 SealAiClient(Bearer JWT),
|
||||
不使用快报主站的 EkbClient(三 Header 认证)。
|
||||
"""
|
||||
|
||||
from typing import Optional
|
||||
from fastapi import APIRouter, Query
|
||||
from pydantic import BaseModel
|
||||
|
||||
@@ -25,6 +33,13 @@ from ekb_cli.services.sealai import (
|
||||
remove_draft_rule,
|
||||
get_published_versions,
|
||||
publish_draft_rules,
|
||||
get_preference,
|
||||
update_decision_preferences,
|
||||
save_preference,
|
||||
get_articles,
|
||||
get_article_detail,
|
||||
add_article,
|
||||
edit_article,
|
||||
)
|
||||
from ekb_cli.api.sealai import get_me
|
||||
from ekb_cli.api.sealai.schemas import (
|
||||
@@ -34,6 +49,10 @@ from ekb_cli.api.sealai.schemas import (
|
||||
DraftRulesData,
|
||||
RuleSetVersion,
|
||||
PublishVersionData,
|
||||
AiApprovalConfig,
|
||||
AiApprovalSaveResult,
|
||||
KnowledgeArticle,
|
||||
KnowledgeArticleListData,
|
||||
)
|
||||
|
||||
router = APIRouter()
|
||||
@@ -148,3 +167,163 @@ def sealai_publish_version(
|
||||
):
|
||||
client = get_sealai_client(corp_id=corp_id)
|
||||
return publish_draft_rules(client, version_name=body.version_name)
|
||||
|
||||
|
||||
# ── 风格偏好(AI 审批配置) ─────────────────────────────────────────────────────
|
||||
|
||||
@router.get(
|
||||
"/preference",
|
||||
response_model=AiApprovalConfig,
|
||||
summary="查看 AI 审批风格偏好配置(决策偏好 + 集成系统信息)",
|
||||
)
|
||||
def sealai_get_preference(
|
||||
corp_id: str = Query(None, description="企业 corpId,不传则使用当前企业"),
|
||||
):
|
||||
client = get_sealai_client(corp_id=corp_id)
|
||||
return get_preference(client)
|
||||
|
||||
|
||||
@router.post(
|
||||
"/preference",
|
||||
response_model=AiApprovalSaveResult,
|
||||
summary="整体替换 AI 审批配置(需传完整配置对象)",
|
||||
)
|
||||
def sealai_save_preference(
|
||||
body: AiApprovalConfig,
|
||||
corp_id: str = Query(None, description="企业 corpId,不传则使用当前企业"),
|
||||
):
|
||||
client = get_sealai_client(corp_id=corp_id)
|
||||
return save_preference(client, body)
|
||||
|
||||
|
||||
class DecisionPreferencesUpdateRequest(BaseModel):
|
||||
explicit_approve: str | None = None
|
||||
explicit_reject: str | None = None
|
||||
exempt_approve: str | None = None
|
||||
review: str | None = None
|
||||
|
||||
|
||||
@router.patch(
|
||||
"/preference/decision",
|
||||
response_model=AiApprovalSaveResult,
|
||||
summary="Patch 式更新决策偏好(只传需要改动的字段,其余保持不变)",
|
||||
)
|
||||
def sealai_patch_decision_preferences(
|
||||
body: DecisionPreferencesUpdateRequest,
|
||||
corp_id: str = Query(None, description="企业 corpId,不传则使用当前企业"),
|
||||
):
|
||||
client = get_sealai_client(corp_id=corp_id)
|
||||
return update_decision_preferences(
|
||||
client,
|
||||
explicit_approve=body.explicit_approve,
|
||||
explicit_reject=body.explicit_reject,
|
||||
exempt_approve=body.exempt_approve,
|
||||
review=body.review,
|
||||
)
|
||||
|
||||
|
||||
# ── 知识文档 ───────────────────────────────────────────────────────────────────
|
||||
|
||||
@router.get(
|
||||
"/knowledge/articles",
|
||||
response_model=KnowledgeArticleListData,
|
||||
summary="查询知识文档列表(支持场景过滤、分页、排序)",
|
||||
)
|
||||
def sealai_list_articles(
|
||||
corp_id: str = Query(None, description="企业 corpId,不传则使用当前企业"),
|
||||
scenarios: Optional[list[str]] = Query(None, description="场景过滤,可选 audit / fill"),
|
||||
offset: int = Query(0, description="分页偏移量"),
|
||||
limit: int = Query(20, description="每页数量(最大建议 100)"),
|
||||
sort_by: str = Query("createdAt", description="排序字段"),
|
||||
sort_order: str = Query("desc", description="排序方向:asc | desc"),
|
||||
):
|
||||
client = get_sealai_client(corp_id=corp_id)
|
||||
return get_articles(
|
||||
client,
|
||||
scenarios=scenarios,
|
||||
offset=offset,
|
||||
limit=limit,
|
||||
sort_by=sort_by,
|
||||
sort_order=sort_order,
|
||||
)
|
||||
|
||||
|
||||
class ArticleCreateRequest(BaseModel):
|
||||
title: str
|
||||
purpose: str
|
||||
content: str
|
||||
content_type: str = "policy"
|
||||
status: str = "published"
|
||||
scenarios: list[str] = []
|
||||
tags: list[str] = []
|
||||
|
||||
|
||||
@router.post(
|
||||
"/knowledge/articles",
|
||||
response_model=KnowledgeArticle,
|
||||
summary="新建知识文档",
|
||||
)
|
||||
def sealai_create_article(
|
||||
body: ArticleCreateRequest,
|
||||
corp_id: str = Query(None, description="企业 corpId,不传则使用当前企业"),
|
||||
):
|
||||
client = get_sealai_client(corp_id=corp_id)
|
||||
return add_article(
|
||||
client,
|
||||
title=body.title,
|
||||
purpose=body.purpose,
|
||||
content=body.content,
|
||||
content_type=body.content_type,
|
||||
status=body.status,
|
||||
scenarios=body.scenarios,
|
||||
tags=body.tags,
|
||||
)
|
||||
|
||||
|
||||
@router.get(
|
||||
"/knowledge/articles/{article_id}",
|
||||
response_model=KnowledgeArticle,
|
||||
summary="查看知识文档详情",
|
||||
)
|
||||
def sealai_get_article(
|
||||
article_id: str,
|
||||
corp_id: str = Query(None, description="企业 corpId,不传则使用当前企业"),
|
||||
):
|
||||
client = get_sealai_client(corp_id=corp_id)
|
||||
return get_article_detail(client, article_id)
|
||||
|
||||
|
||||
class ArticleUpdateRequest(BaseModel):
|
||||
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
|
||||
|
||||
|
||||
@router.patch(
|
||||
"/knowledge/articles/{article_id}",
|
||||
response_model=KnowledgeArticle,
|
||||
summary="更新知识文档(只传需要改动的字段)",
|
||||
)
|
||||
def sealai_update_article(
|
||||
article_id: str,
|
||||
body: ArticleUpdateRequest,
|
||||
corp_id: str = Query(None, description="企业 corpId,不传则使用当前企业"),
|
||||
):
|
||||
client = get_sealai_client(corp_id=corp_id)
|
||||
return edit_article(
|
||||
client,
|
||||
article_id,
|
||||
title=body.title,
|
||||
purpose=body.purpose,
|
||||
content=body.content,
|
||||
content_type=body.content_type,
|
||||
status=body.status,
|
||||
enabled=body.enabled,
|
||||
scenarios=body.scenarios,
|
||||
tags=body.tags,
|
||||
)
|
||||
@@ -17,6 +17,17 @@ from ekb_cli.services.sealai.rules_service import (
|
||||
get_published_versions,
|
||||
publish_draft_rules,
|
||||
)
|
||||
from ekb_cli.services.sealai.preference_service import (
|
||||
get_preference,
|
||||
update_decision_preferences,
|
||||
save_preference,
|
||||
)
|
||||
from ekb_cli.services.sealai.knowledge_service import (
|
||||
get_articles,
|
||||
get_article_detail,
|
||||
add_article,
|
||||
edit_article,
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
"get_sealai_client",
|
||||
@@ -30,4 +41,12 @@ __all__ = [
|
||||
"remove_draft_rule",
|
||||
"get_published_versions",
|
||||
"publish_draft_rules",
|
||||
"get_preference",
|
||||
"update_decision_preferences",
|
||||
"save_preference",
|
||||
"get_articles",
|
||||
"get_article_detail",
|
||||
"add_article",
|
||||
"edit_article",
|
||||
]
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
"""SealAI 风格偏好业务逻辑层。
|
||||
|
||||
管理 AI 审批配置的读取与更新(决策场景提示词 + 集成系统信息)。
|
||||
|
||||
核心设计:POST 是整体替换语义,因此 update_decision_preferences() 会先 GET
|
||||
当前配置,patch 指定字段,再整体 POST 回去——对上层调用方透明。
|
||||
"""
|
||||
|
||||
from ekb_cli.api.sealai.client import SealAiClient
|
||||
from ekb_cli.api.sealai.preference_api import (
|
||||
get_ai_approval_config,
|
||||
save_ai_approval_config,
|
||||
)
|
||||
from ekb_cli.api.sealai.schemas import (
|
||||
AiApprovalConfig,
|
||||
AiApprovalSaveResult,
|
||||
DecisionPreferences,
|
||||
)
|
||||
|
||||
|
||||
def get_preference(client: SealAiClient) -> AiApprovalConfig:
|
||||
"""查询当前 AI 审批配置(风格偏好 + 集成系统信息)。"""
|
||||
return get_ai_approval_config(client)
|
||||
|
||||
|
||||
def update_decision_preferences(
|
||||
client: SealAiClient,
|
||||
*,
|
||||
explicit_approve: str | None = None,
|
||||
explicit_reject: str | None = None,
|
||||
exempt_approve: str | None = None,
|
||||
review: str | None = None,
|
||||
) -> AiApprovalSaveResult:
|
||||
"""Patch 式更新决策偏好(只传需要改动的字段)。
|
||||
|
||||
内部会先 GET 当前配置,合并改动后整体 POST 回去,
|
||||
确保 integrations 等无关字段不丢失。
|
||||
|
||||
Args:
|
||||
explicit_approve: 明确通过的条件描述(不传则保持不变)
|
||||
explicit_reject: 明确拒绝的条件描述(不传则保持不变)
|
||||
exempt_approve: 豁免通过的条件描述(不传则保持不变)
|
||||
review: 转人工判断的条件描述(不传则保持不变)
|
||||
Returns:
|
||||
AiApprovalSaveResult,含 success 标志。
|
||||
"""
|
||||
current = get_ai_approval_config(client)
|
||||
prefs = current.decisionPreferences
|
||||
|
||||
# Patch:只覆盖传入的字段
|
||||
new_prefs = DecisionPreferences(
|
||||
explicitApprove=explicit_approve if explicit_approve is not None else prefs.explicitApprove,
|
||||
explicitReject=explicit_reject if explicit_reject is not None else prefs.explicitReject,
|
||||
exemptApprove=exempt_approve if exempt_approve is not None else prefs.exemptApprove,
|
||||
review=review if review is not None else prefs.review,
|
||||
)
|
||||
updated = current.model_copy(update={"decisionPreferences": new_prefs})
|
||||
return save_ai_approval_config(client, updated)
|
||||
|
||||
|
||||
def save_preference(
|
||||
client: SealAiClient,
|
||||
config: AiApprovalConfig,
|
||||
) -> AiApprovalSaveResult:
|
||||
"""直接保存完整的 AI 审批配置(整体替换,适合高阶调用方)。"""
|
||||
return save_ai_approval_config(client, config)
|
||||
Reference in new issue
Block a user