SealAI 全面收口:service 层统一管理 client 生命周期 + 401 自动恢复
核心改动(auth_service.py): - 新增模块级内存 client 缓存(_client_cache),按 corp_id 键入 + TTL 复用 - 磁盘 token 缓存 + 内存 client 缓存双层同步驱逐(invalidate_token 一并清除) - 新增 _sealai_call(corp_id, fn, ...):统一调用入口,401 时自动清缓存并单次重试 - get_sealai_client 降级为内部函数,不再对外导出 业务 service 层(4 个文件): - 函数签名从 (client: SealAiClient, ...) 改为 (corp_id: str | None = None, ...) - 内部统一走 _sealai_call,不自行管理 client 表现层全面收口(11 个 commands 文件 + sealai_router.py): - 移除所有 get_sealai_client 调用,直接传 corp_id 给 service 函数 - CLI 和 Server 不再感知 SealAiClient 的存在 效果: - CLI 进程短命,内存 client 随进程回收;Server 长命,连接池复用 - 两条路径代码完全统一,差别仅由进程生命周期自然体现 - 全仓 grep 验证:commands/* 和 server/* 已无任何 get_sealai_client 引用
This commit is contained in:
1 parent
942449653a
commit
2f51f0f611
20 files changed
+426
-186
No files matched your search
@@ -3,8 +3,8 @@
|
||||
与快报主站的 EkbClient 并列,同属 api/ 传输层。
|
||||
区别:SealAI 用 Bearer JWT 认证,域名按企业 corpId 动态生成。
|
||||
|
||||
使用方:services/sealai/ 通过 get_sealai_client() 获取实例,
|
||||
不直接 import 此模块。
|
||||
使用方:services/sealai/auth_service 通过 _get_or_create_client() 获取实例,
|
||||
并由 _sealai_call 统一管理生命周期,外部不直接持有或关闭此对象。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
表现层:只负责 rich 格式化输出。
|
||||
业务逻辑委托给 services.sealai.billing_service。
|
||||
不感知 SealAiClient,corp_id 由 service 层统一解析。
|
||||
"""
|
||||
|
||||
import typer
|
||||
@@ -9,7 +10,7 @@ from rich.console import Console
|
||||
from rich.table import Table
|
||||
from rich import print as rprint
|
||||
|
||||
from ekb_cli.services.sealai import get_sealai_client, query_billing_status
|
||||
from ekb_cli.services.sealai import query_billing_status
|
||||
|
||||
console = Console()
|
||||
|
||||
@@ -18,8 +19,7 @@ def cmd_billing(
|
||||
corp_id: str = typer.Option(None, "--corp", "-c", help="指定企业 corpId,默认使用当前企业"),
|
||||
):
|
||||
"""查看当前企业 SealAI 订阅状态及各功能配额"""
|
||||
client = get_sealai_client(corp_id=corp_id)
|
||||
status = query_billing_status(client)
|
||||
status = query_billing_status(corp_id=corp_id)
|
||||
|
||||
# 订阅信息
|
||||
if status.subscriptions:
|
||||
|
||||
@@ -2,12 +2,13 @@
|
||||
|
||||
表现层:交互式输入 + rich 输出。
|
||||
业务逻辑委托给 services.sealai.knowledge_service。
|
||||
不感知 SealAiClient,corp_id 由 service 层统一解析。
|
||||
"""
|
||||
|
||||
import typer
|
||||
from rich import print as rprint
|
||||
|
||||
from ekb_cli.services.sealai import get_sealai_client, add_article
|
||||
from ekb_cli.services.sealai import add_article
|
||||
|
||||
|
||||
def cmd_knowledge_create(
|
||||
@@ -41,9 +42,8 @@ def cmd_knowledge_create(
|
||||
pass
|
||||
content = "\n".join(lines)
|
||||
|
||||
client = get_sealai_client(corp_id=corp_id)
|
||||
article = add_article(
|
||||
client,
|
||||
corp_id=corp_id,
|
||||
title=title,
|
||||
purpose=purpose,
|
||||
content=content,
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
表现层:只负责 rich 格式化输出。
|
||||
业务逻辑委托给 services.sealai.knowledge_service。
|
||||
不感知 SealAiClient,corp_id 由 service 层统一解析。
|
||||
"""
|
||||
|
||||
import typer
|
||||
@@ -12,7 +13,7 @@ from rich.markdown import Markdown
|
||||
from rich.table import Table
|
||||
from rich import print as rprint
|
||||
|
||||
from ekb_cli.services.sealai import get_sealai_client, get_article_detail
|
||||
from ekb_cli.services.sealai import get_article_detail
|
||||
|
||||
console = Console()
|
||||
|
||||
@@ -31,8 +32,7 @@ def cmd_knowledge_get(
|
||||
corp_id: str = typer.Option(None, "--corp", "-c", help="指定企业 corpId,默认使用当前企业"),
|
||||
):
|
||||
"""查看单篇知识文档详情(含正文 Markdown 渲染)"""
|
||||
client = get_sealai_client(corp_id=corp_id)
|
||||
a = get_article_detail(client, article_id)
|
||||
a = get_article_detail(corp_id=corp_id, article_id=article_id)
|
||||
|
||||
scenes = " / ".join(_SCENARIO_LABEL.get(s, s) for s in a.scenarios)
|
||||
status = _STATUS_LABEL.get(a.status, a.status)
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
表现层:只负责 rich 格式化输出。
|
||||
业务逻辑委托给 services.sealai.knowledge_service。
|
||||
不感知 SealAiClient,corp_id 由 service 层统一解析。
|
||||
"""
|
||||
|
||||
import typer
|
||||
@@ -12,7 +13,7 @@ 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
|
||||
from ekb_cli.services.sealai import get_articles
|
||||
|
||||
console = Console()
|
||||
|
||||
@@ -34,8 +35,7 @@ def cmd_knowledge_list(
|
||||
):
|
||||
"""查看知识文档列表"""
|
||||
scenarios = [scenario] if scenario else None
|
||||
client = get_sealai_client(corp_id=corp_id)
|
||||
data = get_articles(client, scenarios=scenarios, limit=limit)
|
||||
data = get_articles(corp_id=corp_id, scenarios=scenarios, limit=limit)
|
||||
|
||||
if not data.articles:
|
||||
rprint("[yellow]暂无知识文档[/yellow]")
|
||||
|
||||
@@ -2,13 +2,14 @@
|
||||
|
||||
表现层:只负责参数解析 + rich 输出。
|
||||
业务逻辑委托给 services.sealai.knowledge_service。
|
||||
不感知 SealAiClient,corp_id 由 service 层统一解析。
|
||||
"""
|
||||
|
||||
import typer
|
||||
from typing import Optional
|
||||
from rich import print as rprint
|
||||
|
||||
from ekb_cli.services.sealai import get_sealai_client, edit_article
|
||||
from ekb_cli.services.sealai import edit_article
|
||||
|
||||
|
||||
def cmd_knowledge_update(
|
||||
@@ -30,10 +31,9 @@ def cmd_knowledge_update(
|
||||
rprint("[yellow]未指定任何修改字段,无需更新[/yellow]")
|
||||
raise typer.Exit()
|
||||
|
||||
client = get_sealai_client(corp_id=corp_id)
|
||||
article = edit_article(
|
||||
client,
|
||||
article_id,
|
||||
corp_id=corp_id,
|
||||
article_id=article_id,
|
||||
title=title,
|
||||
purpose=purpose,
|
||||
content=content,
|
||||
|
||||
@@ -2,12 +2,13 @@
|
||||
|
||||
表现层:交互式输入 + rich 输出。
|
||||
业务逻辑委托给 services.sealai.rules_service。
|
||||
不感知 SealAiClient,corp_id 由 service 层统一解析。
|
||||
"""
|
||||
|
||||
import typer
|
||||
from rich import print as rprint
|
||||
|
||||
from ekb_cli.services.sealai import get_sealai_client, add_draft_rule
|
||||
from ekb_cli.services.sealai import add_draft_rule
|
||||
|
||||
_STRICTNESS_CHOICES = ["MUST_FOLLOW", "SHOULD_FOLLOW", "SUGGESTION"]
|
||||
|
||||
@@ -30,6 +31,5 @@ def cmd_rule_create(
|
||||
rprint("[red]描述和适用场景不能为空[/red]")
|
||||
raise typer.Exit(1)
|
||||
|
||||
client = get_sealai_client(corp_id=corp_id)
|
||||
rule = add_draft_rule(client, description=description, scope=scope, strictness=strictness)
|
||||
rule = add_draft_rule(corp_id=corp_id, description=description, scope=scope, strictness=strictness)
|
||||
rprint(f"[green]✓[/green] 规则已创建:[bold]{rule.description}[/bold](ID: {rule.id})")
|
||||
@@ -2,12 +2,13 @@
|
||||
|
||||
表现层:二次确认 + rich 输出。
|
||||
业务逻辑委托给 services.sealai.rules_service。
|
||||
不感知 SealAiClient,corp_id 由 service 层统一解析。
|
||||
"""
|
||||
|
||||
import typer
|
||||
from rich import print as rprint
|
||||
|
||||
from ekb_cli.services.sealai import get_sealai_client, remove_draft_rule
|
||||
from ekb_cli.services.sealai import remove_draft_rule
|
||||
|
||||
|
||||
def cmd_rule_delete(
|
||||
@@ -22,6 +23,5 @@ def cmd_rule_delete(
|
||||
rprint("[dim]已取消[/dim]")
|
||||
raise typer.Exit()
|
||||
|
||||
client = get_sealai_client(corp_id=corp_id)
|
||||
remove_draft_rule(client, rule_id)
|
||||
remove_draft_rule(corp_id=corp_id, rule_id=rule_id)
|
||||
rprint(f"[green]✓[/green] 规则 {rule_id} 已删除")
|
||||
@@ -2,12 +2,13 @@
|
||||
|
||||
表现层:只负责参数解析 + rich 输出。
|
||||
业务逻辑委托给 services.sealai.rules_service。
|
||||
不感知 SealAiClient,corp_id 由 service 层统一解析。
|
||||
"""
|
||||
|
||||
import typer
|
||||
from rich import print as rprint
|
||||
|
||||
from ekb_cli.services.sealai import get_sealai_client, edit_draft_rule
|
||||
from ekb_cli.services.sealai import edit_draft_rule
|
||||
|
||||
|
||||
def cmd_rule_update(
|
||||
@@ -25,9 +26,8 @@ def cmd_rule_update(
|
||||
rprint("[yellow]未指定任何修改字段,无需更新[/yellow]")
|
||||
raise typer.Exit()
|
||||
|
||||
client = get_sealai_client(corp_id=corp_id)
|
||||
rule = edit_draft_rule(
|
||||
client, rule_id,
|
||||
corp_id=corp_id, rule_id=rule_id,
|
||||
description=description,
|
||||
scope=scope,
|
||||
strictness=strictness,
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
表现层:只负责 rich 格式化输出。
|
||||
业务逻辑委托给 services.sealai.rules_service。
|
||||
不感知 SealAiClient,corp_id 由 service 层统一解析。
|
||||
"""
|
||||
|
||||
import typer
|
||||
@@ -9,7 +10,7 @@ from rich.console import Console
|
||||
from rich.table import Table
|
||||
from rich import print as rprint
|
||||
|
||||
from ekb_cli.services.sealai import get_sealai_client, get_draft_rules
|
||||
from ekb_cli.services.sealai import get_draft_rules
|
||||
|
||||
console = Console()
|
||||
|
||||
@@ -24,8 +25,7 @@ def cmd_rules(
|
||||
corp_id: str = typer.Option(None, "--corp", "-c", help="指定企业 corpId,默认使用当前企业"),
|
||||
):
|
||||
"""查看草稿区所有审核规则"""
|
||||
client = get_sealai_client(corp_id=corp_id)
|
||||
data = get_draft_rules(client)
|
||||
data = get_draft_rules(corp_id=corp_id)
|
||||
|
||||
if not data.rules:
|
||||
rprint("[yellow]草稿区暂无审核规则[/yellow]")
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
表现层:只负责 rich 格式化输出。
|
||||
业务逻辑委托给 services.sealai.rules_service。
|
||||
不感知 SealAiClient,corp_id 由 service 层统一解析。
|
||||
"""
|
||||
|
||||
import typer
|
||||
@@ -11,7 +12,7 @@ from rich.table import Table
|
||||
from rich.tree import Tree
|
||||
from rich import print as rprint
|
||||
|
||||
from ekb_cli.services.sealai import get_sealai_client, get_published_versions
|
||||
from ekb_cli.services.sealai import get_published_versions
|
||||
|
||||
console = Console()
|
||||
|
||||
@@ -27,8 +28,7 @@ def cmd_versions(
|
||||
detail: bool = typer.Option(False, "--detail", help="展开每个版本的规则列表"),
|
||||
):
|
||||
"""查看已发布规则集版本历史"""
|
||||
client = get_sealai_client(corp_id=corp_id)
|
||||
versions = get_published_versions(client)
|
||||
versions = get_published_versions(corp_id=corp_id)
|
||||
|
||||
if not versions:
|
||||
rprint("[yellow]暂无已发布版本[/yellow]")
|
||||
|
||||
@@ -16,15 +16,15 @@
|
||||
—(Server 专属)— → GET /sealai/knowledge/articles/{article_id}
|
||||
—(Server 专属)— → PATCH /sealai/knowledge/articles/{article_id}
|
||||
|
||||
注意:SealAI 接口使用独立的 SealAiClient(Bearer JWT),
|
||||
不使用快报主站的 EkbClient(三 Header 认证)。
|
||||
设计约定:
|
||||
- 路由函数不持有 SealAiClient,统一由 services.sealai 层管理(缓存、关闭、401 恢复)
|
||||
- 路由函数只做参数绑定 + 调用 service 函数,返回值直接交给 FastAPI 序列化
|
||||
"""
|
||||
|
||||
from typing import Optional
|
||||
from fastapi import APIRouter, Query
|
||||
|
||||
from ekb_cli.services.sealai import (
|
||||
get_sealai_client,
|
||||
get_whoami,
|
||||
query_billing_status,
|
||||
get_draft_rules,
|
||||
@@ -78,8 +78,7 @@ def sealai_whoami(
|
||||
def sealai_billing(
|
||||
corp_id: str = Query(None, description="企业 corpId,不传则使用当前企业"),
|
||||
):
|
||||
client = get_sealai_client(corp_id=corp_id)
|
||||
return query_billing_status(client)
|
||||
return query_billing_status(corp_id=corp_id)
|
||||
|
||||
|
||||
# ── 审核规则(草稿区) ─────────────────────────────────────────────────────────
|
||||
@@ -88,8 +87,7 @@ def sealai_billing(
|
||||
def sealai_list_rules(
|
||||
corp_id: str = Query(None, description="企业 corpId,不传则使用当前企业"),
|
||||
):
|
||||
client = get_sealai_client(corp_id=corp_id)
|
||||
return get_draft_rules(client)
|
||||
return get_draft_rules(corp_id=corp_id)
|
||||
|
||||
|
||||
@router.post("/rules", response_model=DraftRule, summary="新建草稿审核规则")
|
||||
@@ -97,9 +95,8 @@ def sealai_create_rule(
|
||||
body: RuleCreateRequest,
|
||||
corp_id: str = Query(None, description="企业 corpId,不传则使用当前企业"),
|
||||
):
|
||||
client = get_sealai_client(corp_id=corp_id)
|
||||
return add_draft_rule(
|
||||
client,
|
||||
corp_id=corp_id,
|
||||
description=body.description,
|
||||
scope=body.scope,
|
||||
strictness=body.strictness,
|
||||
@@ -112,9 +109,8 @@ def sealai_update_rule(
|
||||
body: RuleUpdateRequest,
|
||||
corp_id: str = Query(None, description="企业 corpId,不传则使用当前企业"),
|
||||
):
|
||||
client = get_sealai_client(corp_id=corp_id)
|
||||
return edit_draft_rule(
|
||||
client, rule_id,
|
||||
corp_id=corp_id, rule_id=rule_id,
|
||||
description=body.description,
|
||||
scope=body.scope,
|
||||
strictness=body.strictness,
|
||||
@@ -126,8 +122,7 @@ def sealai_delete_rule(
|
||||
rule_id: str,
|
||||
corp_id: str = Query(None, description="企业 corpId,不传则使用当前企业"),
|
||||
):
|
||||
client = get_sealai_client(corp_id=corp_id)
|
||||
remove_draft_rule(client, rule_id)
|
||||
remove_draft_rule(corp_id=corp_id, rule_id=rule_id)
|
||||
return {"success": True, "rule_id": rule_id}
|
||||
|
||||
|
||||
@@ -141,8 +136,7 @@ def sealai_delete_rule(
|
||||
def sealai_list_versions(
|
||||
corp_id: str = Query(None, description="企业 corpId,不传则使用当前企业"),
|
||||
):
|
||||
client = get_sealai_client(corp_id=corp_id)
|
||||
return get_published_versions(client)
|
||||
return get_published_versions(corp_id=corp_id)
|
||||
|
||||
|
||||
@router.post(
|
||||
@@ -154,8 +148,7 @@ def sealai_publish_version(
|
||||
body: PublishVersionRequest,
|
||||
corp_id: str = Query(None, description="企业 corpId,不传则使用当前企业"),
|
||||
):
|
||||
client = get_sealai_client(corp_id=corp_id)
|
||||
return publish_draft_rules(client, version_name=body.version_name)
|
||||
return publish_draft_rules(corp_id=corp_id, version_name=body.version_name)
|
||||
|
||||
|
||||
# ── 风格偏好(AI 审批配置) ─────────────────────────────────────────────────────
|
||||
@@ -168,8 +161,7 @@ def sealai_publish_version(
|
||||
def sealai_get_preference(
|
||||
corp_id: str = Query(None, description="企业 corpId,不传则使用当前企业"),
|
||||
):
|
||||
client = get_sealai_client(corp_id=corp_id)
|
||||
return get_preference(client)
|
||||
return get_preference(corp_id=corp_id)
|
||||
|
||||
|
||||
@router.post(
|
||||
@@ -181,8 +173,7 @@ 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)
|
||||
return save_preference(corp_id=corp_id, config=body)
|
||||
|
||||
|
||||
@router.patch(
|
||||
@@ -194,9 +185,8 @@ 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,
|
||||
corp_id=corp_id,
|
||||
explicit_approve=body.explicit_approve,
|
||||
explicit_reject=body.explicit_reject,
|
||||
exempt_approve=body.exempt_approve,
|
||||
@@ -219,9 +209,8 @@ def sealai_list_articles(
|
||||
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,
|
||||
corp_id=corp_id,
|
||||
scenarios=scenarios,
|
||||
offset=offset,
|
||||
limit=limit,
|
||||
@@ -239,9 +228,8 @@ 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,
|
||||
corp_id=corp_id,
|
||||
title=body.title,
|
||||
purpose=body.purpose,
|
||||
content=body.content,
|
||||
@@ -261,8 +249,7 @@ 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)
|
||||
return get_article_detail(corp_id=corp_id, article_id=article_id)
|
||||
|
||||
|
||||
@router.patch(
|
||||
@@ -275,10 +262,9 @@ def sealai_update_article(
|
||||
body: ArticleUpdateRequest,
|
||||
corp_id: str = Query(None, description="企业 corpId,不传则使用当前企业"),
|
||||
):
|
||||
client = get_sealai_client(corp_id=corp_id)
|
||||
return edit_article(
|
||||
client,
|
||||
article_id,
|
||||
corp_id=corp_id,
|
||||
article_id=article_id,
|
||||
title=body.title,
|
||||
purpose=body.purpose,
|
||||
content=body.content,
|
||||
|
||||
@@ -56,7 +56,6 @@ from ekb_cli.services.rule_service import ( # noqa: F401
|
||||
# commands/server 使用 `from ekb_cli.services.sealai import xxx`
|
||||
# 此处仅做显式 re-export 以便 IDE 跳转和 import lint 检查。
|
||||
from ekb_cli.services.sealai import ( # noqa: F401
|
||||
get_sealai_client,
|
||||
get_bearer_token,
|
||||
invalidate_token,
|
||||
get_whoami,
|
||||
|
||||
@@ -3,48 +3,66 @@
|
||||
## 职责边界
|
||||
|
||||
SealAI 是独立于快报主站的 Next.js 平台(`{corpId}.sealai.cc`),认证体系完全独立。
|
||||
本子包封装所有 SealAI 相关业务逻辑,对外隐藏 `api/sealai/` 传输层细节。
|
||||
本子包封装所有 SealAI 相关业务逻辑,对外隐藏 `api/sealai/` 传输层细节,
|
||||
**同时隐藏 `SealAiClient` 的创建、缓存与关闭**。
|
||||
|
||||
## 模块说明
|
||||
|
||||
| 模块 | 职责 |
|
||||
|---|---|
|
||||
| `auth_service.py` | SSO token 换取、缓存、刷新;`get_sealai_client()`;`get_whoami()` |
|
||||
| `auth_service.py` | SSO token 换取 + 磁盘/内存双层缓存 + `_sealai_call` 统一调用入口(含 401 自动恢复) |
|
||||
| `billing_service.py` | 订阅状态查询;单功能配额查询 |
|
||||
| `rules_service.py` | 草稿区审核规则 CRUD;版本发布 |
|
||||
| `preference_service.py` | AI 审批风格偏好读写 |
|
||||
| `knowledge_service.py` | 知识文档(Articles)CRUD |
|
||||
|
||||
## 认证流程
|
||||
## 认证与缓存设计
|
||||
|
||||
```
|
||||
快报 accesstoken(三 Header 认证)
|
||||
→ GET {corpId}.sealai.cc/api/auth/oauth2/session/oem-hosecloud?token=...&returnToken=1
|
||||
→ Bearer JWT(缓存至 ~/.config/ekb-cli/sealai_tokens.json,TTL 3600s)
|
||||
→ SealAiClient(corp_id, bearer_token)
|
||||
→ Bearer JWT
|
||||
|
||||
磁盘缓存(~/.config/ekb-cli/sealai_tokens.json,TTL 3600s):
|
||||
{ "ID01Jdq2yIDnMr": { "token": "...", "fetched_at": 1712345678 } }
|
||||
→ CLI / Server 进程间共享
|
||||
|
||||
内存缓存(模块级 _client_cache,TTL 同 token):
|
||||
{ "corp_id": (SealAiClient, fetched_at_float) }
|
||||
→ CLI 进程结束自动回收;Server 进程中长期保持,复用 httpx 连接池
|
||||
```
|
||||
|
||||
缓存格式:
|
||||
```json
|
||||
{ "ID01Jdq2yIDnMr": { "token": "...", "fetched_at": 1712345678 } }
|
||||
```
|
||||
**401 自动恢复**:`_sealai_call` 捕获 `AuthenticationError` 后,同时清除内存 client
|
||||
和磁盘 token,强制重新换取,然后单次重试。调用方无需感知这一过程。
|
||||
|
||||
## 调用方约定
|
||||
|
||||
- `commands/*` 和 `server/*` 只传 `corp_id`,不持有也不传递 `SealAiClient`
|
||||
- `SealAiClient` 的创建和生命周期完全由 `auth_service` 管理
|
||||
|
||||
## 对外接口(统一从 `services.sealai` 导入)
|
||||
|
||||
```python
|
||||
from ekb_cli.services.sealai import (
|
||||
get_sealai_client, # 获取 SealAiClient(自动换取/刷新 token)
|
||||
# 认证(调试 / 外部集成)
|
||||
get_bearer_token, # 获取 Bearer JWT 字符串
|
||||
invalidate_token, # 清除本地缓存 token(401 后调用)
|
||||
get_whoami, # 查询当前用户身份(封装 api.sealai.get_me)
|
||||
invalidate_token, # 强制清除缓存(磁盘 + 内存)
|
||||
get_whoami, # 查询当前用户身份
|
||||
# 计费
|
||||
query_billing_status,
|
||||
get_feature_usage,
|
||||
# 审核规则
|
||||
get_draft_rules, add_draft_rule, edit_draft_rule, 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,
|
||||
)
|
||||
```
|
||||
|
||||
**不对外暴露**:`get_sealai_client`、`_sealai_call`、`_get_or_create_client`
|
||||
|
||||
## 导入约定
|
||||
|
||||
```
|
||||
|
||||
@@ -1,16 +1,24 @@
|
||||
"""SealAI 服务层统一 re-export。"""
|
||||
"""SealAI 服务层统一 re-export。
|
||||
|
||||
from ekb_cli.services.sealai.auth_service import (
|
||||
get_sealai_client,
|
||||
get_bearer_token,
|
||||
对外约定
|
||||
--------
|
||||
commands/* 和 server/* 只从此处(或 services 顶层)导入,不直接访问子模块。
|
||||
|
||||
不再对外暴露 get_sealai_client / _sealai_call 等内部函数:
|
||||
- SealAiClient 的创建、缓存、关闭、401 恢复全部由 auth_service 内部承担
|
||||
- 调用方只需传 corp_id,无需感知 client 的存在
|
||||
"""
|
||||
|
||||
from ekb_cli.services.sealai.auth_service import ( # noqa: F401
|
||||
invalidate_token,
|
||||
get_bearer_token,
|
||||
get_whoami,
|
||||
)
|
||||
from ekb_cli.services.sealai.billing_service import (
|
||||
from ekb_cli.services.sealai.billing_service import ( # noqa: F401
|
||||
query_billing_status,
|
||||
get_feature_usage,
|
||||
)
|
||||
from ekb_cli.services.sealai.rules_service import (
|
||||
from ekb_cli.services.sealai.rules_service import ( # noqa: F401
|
||||
get_draft_rules,
|
||||
add_draft_rule,
|
||||
edit_draft_rule,
|
||||
@@ -18,12 +26,12 @@ from ekb_cli.services.sealai.rules_service import (
|
||||
get_published_versions,
|
||||
publish_draft_rules,
|
||||
)
|
||||
from ekb_cli.services.sealai.preference_service import (
|
||||
from ekb_cli.services.sealai.preference_service import ( # noqa: F401
|
||||
get_preference,
|
||||
update_decision_preferences,
|
||||
save_preference,
|
||||
)
|
||||
from ekb_cli.services.sealai.knowledge_service import (
|
||||
from ekb_cli.services.sealai.knowledge_service import ( # noqa: F401
|
||||
get_articles,
|
||||
get_article_detail,
|
||||
add_article,
|
||||
@@ -31,24 +39,27 @@ from ekb_cli.services.sealai.knowledge_service import (
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
"get_sealai_client",
|
||||
"get_bearer_token",
|
||||
# 认证(仅保留对外有意义的操作)
|
||||
"invalidate_token",
|
||||
"get_bearer_token",
|
||||
"get_whoami",
|
||||
# 计费
|
||||
"query_billing_status",
|
||||
"get_feature_usage",
|
||||
# 审核规则
|
||||
"get_draft_rules",
|
||||
"add_draft_rule",
|
||||
"edit_draft_rule",
|
||||
"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",
|
||||
]
|
||||
|
||||
@@ -1,21 +1,43 @@
|
||||
"""SealAI 认证业务逻辑层。
|
||||
|
||||
职责:SSO 换 Bearer token、本地缓存、自动刷新、对外暴露 get_sealai_client() 和 get_whoami()。
|
||||
职责
|
||||
----
|
||||
1. SSO 换 Bearer token(快报 accesstoken → SealAI Bearer JWT)
|
||||
2. Token 持久缓存(磁盘文件,CLI/Server 共用)
|
||||
3. Client 对象内存缓存(模块级 dict,Server 连接池复用;CLI 进程结束自动回收)
|
||||
4. 401 自动恢复:清除内存 client + 磁盘 token → 重取 → 单次重试
|
||||
5. 对上层(commands/server)隐藏 SealAiClient 的创建与关闭
|
||||
|
||||
认证流程:
|
||||
快报 accesstoken
|
||||
→ GET {corpId}.sealai.cc/api/auth/oauth2/session/oem-hosecloud?token=...&returnToken=1
|
||||
→ Bearer JWT(缓存到 ~/.config/ekb-cli/sealai_tokens.json)
|
||||
→ SealAiClient(corp_id, bearer_token)
|
||||
对外接口
|
||||
--------
|
||||
- get_whoami(corp_id, force_refresh) — 查询用户身份
|
||||
- invalidate_token(corp_id) — 强制清除缓存(外部显式调用场景)
|
||||
|
||||
缓存格式:
|
||||
{ "ID01Jdq2yIDnMr": { "token": "...", "fetched_at": 1712345678 } }
|
||||
内部函数(服务层内部使用,不对外导出)
|
||||
--------------------------------------
|
||||
- _resolve_corp_id(corp_id) — 解析 corp_id,None 时用当前企业
|
||||
- _get_or_create_client(corp_id, force) — 带缓存的 client 工厂
|
||||
- _sealai_call(corp_id, fn, *args, **kwargs) — 调用 SealAI API,401 时自动恢复并重试
|
||||
|
||||
缓存设计
|
||||
--------
|
||||
磁盘(~/.config/ekb-cli/sealai_tokens.json):
|
||||
{ "corpId": { "token": "...", "fetched_at": 1712345678 } }
|
||||
→ CLI 进程间共享,TTL = _TOKEN_TTL 秒
|
||||
|
||||
内存(模块级 _client_cache):
|
||||
{ "corpId": (SealAiClient, fetched_at_float) }
|
||||
→ 进程内复用 httpx 连接池;CLI 进程结束自然回收,Server 长期保持
|
||||
|
||||
两层缓存的 TTL 相同(_TOKEN_TTL)。token 刷新时同步驱逐内存 client,确保
|
||||
client 携带的 Authorization header 始终与最新 token 一致。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
import json
|
||||
import time
|
||||
from pathlib import Path
|
||||
from typing import Any, Callable
|
||||
|
||||
import httpx
|
||||
|
||||
@@ -26,11 +48,22 @@ from ekb_cli.api.sealai.auth_api import get_me as api_get_me
|
||||
from ekb_cli.api.sealai.schemas import MeData
|
||||
|
||||
|
||||
# ── 常量 ──────────────────────────────────────────────────────────────────────
|
||||
|
||||
_TOKEN_CACHE_PATH = Path.home() / ".config" / "ekb-cli" / "sealai_tokens.json"
|
||||
_TOKEN_TTL = 3600 # 保守 1 小时,到期前强制刷新
|
||||
|
||||
|
||||
def _load_cache() -> dict:
|
||||
# ── 内存 client 缓存 ──────────────────────────────────────────────────────────
|
||||
# key: corp_id, value: (SealAiClient, fetched_at)
|
||||
# CLI 进程结束自动回收;Server 进程中长期保持,复用 httpx 连接池。
|
||||
|
||||
_client_cache: dict[str, tuple[SealAiClient, float]] = {}
|
||||
|
||||
|
||||
# ── 磁盘 token 缓存(私有) ───────────────────────────────────────────────────
|
||||
|
||||
def _load_token_cache() -> dict:
|
||||
if _TOKEN_CACHE_PATH.exists():
|
||||
try:
|
||||
return json.loads(_TOKEN_CACHE_PATH.read_text())
|
||||
@@ -39,7 +72,7 @@ def _load_cache() -> dict:
|
||||
return {}
|
||||
|
||||
|
||||
def _save_cache(cache: dict) -> None:
|
||||
def _save_token_cache(cache: dict) -> None:
|
||||
_TOKEN_CACHE_PATH.parent.mkdir(parents=True, exist_ok=True)
|
||||
_TOKEN_CACHE_PATH.write_text(json.dumps(cache, indent=2, ensure_ascii=False))
|
||||
|
||||
@@ -69,9 +102,12 @@ def _fetch_token(corp_id: str, access_token: str) -> str:
|
||||
return token
|
||||
|
||||
|
||||
def get_bearer_token(corp_id: str, force_refresh: bool = False) -> str:
|
||||
"""获取 Bearer token(优先读缓存,过期自动刷新)。"""
|
||||
cache = _load_cache()
|
||||
def _get_bearer_token(corp_id: str, force_refresh: bool = False) -> str:
|
||||
"""获取 Bearer token(优先读磁盘缓存,过期或 force_refresh 时重新换取)。
|
||||
|
||||
此函数为内部实现细节,外部不应直接调用。
|
||||
"""
|
||||
cache = _load_token_cache()
|
||||
entry = cache.get(corp_id, {})
|
||||
now = int(time.time())
|
||||
|
||||
@@ -84,41 +120,114 @@ def get_bearer_token(corp_id: str, force_refresh: bool = False) -> str:
|
||||
|
||||
token = _fetch_token(corp_id, corp["access_token"])
|
||||
cache[corp_id] = {"token": token, "fetched_at": now}
|
||||
_save_cache(cache)
|
||||
_save_token_cache(cache)
|
||||
return token
|
||||
|
||||
|
||||
# ── 对外公共接口 ──────────────────────────────────────────────────────────────
|
||||
|
||||
def invalidate_token(corp_id: str) -> None:
|
||||
"""清除指定企业的缓存 token(用于 401 后强制重新获取)。"""
|
||||
cache = _load_cache()
|
||||
"""清除指定企业的全部缓存(磁盘 token + 内存 client)。
|
||||
|
||||
调用场景:
|
||||
- 用户显式执行 --refresh 参数
|
||||
- 服务层内部在 401 后自动调用(见 _sealai_call)
|
||||
|
||||
清除内存 client 是必要的:client 构造时已将 token 写入 Authorization header,
|
||||
token 失效后若继续复用 client,所有请求都会继续 401。
|
||||
"""
|
||||
# 清磁盘
|
||||
cache = _load_token_cache()
|
||||
cache.pop(corp_id, None)
|
||||
_save_cache(cache)
|
||||
_save_token_cache(cache)
|
||||
# 清内存
|
||||
_client_cache.pop(corp_id, None)
|
||||
|
||||
|
||||
def get_sealai_client(corp_id: str | None = None, force_refresh: bool = False) -> SealAiClient:
|
||||
"""获取当前企业的 SealAiClient,自动处理 token 获取和缓存。
|
||||
def get_bearer_token(corp_id: str, force_refresh: bool = False) -> str:
|
||||
"""获取当前企业的 Bearer JWT 字符串(主要供调试 / 外部集成使用)。
|
||||
|
||||
Args:
|
||||
corp_id: 企业 ID,None 时使用当前配置企业
|
||||
force_refresh: 强制重新换取 token(快报 token 更换后使用)
|
||||
正常业务调用应走 _sealai_call,不需要直接拿 token。
|
||||
"""
|
||||
if corp_id is None:
|
||||
corp_id = ekb_config.get_current_corp_id()
|
||||
if not corp_id:
|
||||
raise NotLoggedInError()
|
||||
return _get_bearer_token(corp_id, force_refresh=force_refresh)
|
||||
|
||||
token = get_bearer_token(corp_id, force_refresh=force_refresh)
|
||||
return SealAiClient(corp_id=corp_id, bearer_token=token)
|
||||
|
||||
# ── 内部工具函数 ──────────────────────────────────────────────────────────────
|
||||
|
||||
def _resolve_corp_id(corp_id: str | None) -> str:
|
||||
"""将 None 解析为当前配置企业的 corp_id,未登录时抛 NotLoggedInError。"""
|
||||
if corp_id is not None:
|
||||
return corp_id
|
||||
resolved = ekb_config.get_current_corp_id()
|
||||
if not resolved:
|
||||
raise NotLoggedInError()
|
||||
return resolved
|
||||
|
||||
|
||||
def _get_or_create_client(corp_id: str, force_refresh: bool = False) -> SealAiClient:
|
||||
"""带 TTL 缓存的 SealAiClient 工厂。
|
||||
|
||||
缓存命中条件:
|
||||
- 内存中有该 corp_id 的 client
|
||||
- 距创建时间未超过 _TOKEN_TTL
|
||||
- 未强制刷新
|
||||
|
||||
缓存未命中时:重新获取 token(走磁盘缓存或 SSO),创建新 client,写入内存缓存。
|
||||
"""
|
||||
now = time.time()
|
||||
|
||||
if not force_refresh:
|
||||
cached = _client_cache.get(corp_id)
|
||||
if cached:
|
||||
client, fetched_at = cached
|
||||
if now - fetched_at < _TOKEN_TTL:
|
||||
return client
|
||||
|
||||
# 缓存未命中或强制刷新:获取 token,构造新 client
|
||||
token = _get_bearer_token(corp_id, force_refresh=force_refresh)
|
||||
client = SealAiClient(corp_id=corp_id, bearer_token=token)
|
||||
_client_cache[corp_id] = (client, now)
|
||||
return client
|
||||
|
||||
|
||||
def _sealai_call(corp_id: str, fn: Callable[..., Any], *args: Any, **kwargs: Any) -> Any:
|
||||
"""统一的 SealAI API 调用入口,内置 401 自动恢复与单次重试。
|
||||
|
||||
调用流程:
|
||||
1. 从缓存获取(或新建)SealAiClient
|
||||
2. 调用 fn(client, *args, **kwargs)
|
||||
3. 若抛 AuthenticationError(401):
|
||||
a. 清除内存 client + 磁盘 token(invalidate_token)
|
||||
b. 强制重新获取 token,构造新 client
|
||||
c. 重试一次 fn(new_client, *args, **kwargs)
|
||||
d. 若仍然 401 则向上抛出
|
||||
|
||||
所有 SealAI 业务函数都应通过此函数发起调用,不应自行管理 client。
|
||||
"""
|
||||
client = _get_or_create_client(corp_id)
|
||||
try:
|
||||
return fn(client, *args, **kwargs)
|
||||
except AuthenticationError:
|
||||
# token 已被服务端吊销或提前失效,清缓存重试一次
|
||||
invalidate_token(corp_id)
|
||||
client = _get_or_create_client(corp_id, force_refresh=True)
|
||||
return fn(client, *args, **kwargs)
|
||||
|
||||
|
||||
def get_whoami(corp_id: str | None = None, force_refresh: bool = False) -> MeData:
|
||||
"""查询当前 SealAI 用户身份及租户信息。
|
||||
|
||||
封装 api.sealai.get_me,commands/server 统一调用此函数,不直接调用 api 层。
|
||||
commands/server 调用此函数,无需感知 SealAiClient 的存在。
|
||||
|
||||
Args:
|
||||
corp_id: 企业 ID,None 时使用当前配置企业
|
||||
force_refresh: 强制刷新 Bearer token
|
||||
corp_id: 企业 ID,None 时使用当前配置企业
|
||||
force_refresh: 强制刷新 Bearer token(token 被手动吊销后使用)
|
||||
"""
|
||||
client = get_sealai_client(corp_id=corp_id, force_refresh=force_refresh)
|
||||
return api_get_me(client)
|
||||
resolved = _resolve_corp_id(corp_id)
|
||||
if force_refresh:
|
||||
invalidate_token(resolved)
|
||||
return _sealai_call(resolved, api_get_me)
|
||||
@@ -1,24 +1,39 @@
|
||||
"""SealAI 计费 / 订阅业务逻辑层。"""
|
||||
"""SealAI 计费 / 订阅业务逻辑层。
|
||||
|
||||
不接受 SealAiClient 参数,由 auth_service._sealai_call 统一管理 client 生命周期。
|
||||
调用方只需传 corp_id(None 表示使用当前企业)。
|
||||
|
||||
功能配额 code 参考
|
||||
------------------
|
||||
ai-approval AI 审批余额(balance 型)
|
||||
approval-rules 审核规则配额(quota 型)
|
||||
ai-fill AI 填单开关(enabled 型)
|
||||
financial-policies 财务制度配额(quota 型)
|
||||
knowledge-documents 知识文档配额(quota 型)
|
||||
"""
|
||||
|
||||
from ekb_cli.api.sealai import get_billing_status
|
||||
from ekb_cli.api.sealai.client import SealAiClient
|
||||
from ekb_cli.api.sealai.schemas import BillingStatusData, Feature
|
||||
from ekb_cli.services.sealai.auth_service import _resolve_corp_id, _sealai_call
|
||||
|
||||
|
||||
def query_billing_status(client: SealAiClient) -> BillingStatusData:
|
||||
"""查询当前企业订阅状态及各功能配额。"""
|
||||
return get_billing_status(client)
|
||||
def query_billing_status(corp_id: str | None = None) -> BillingStatusData:
|
||||
"""查询当前企业订阅状态及各功能配额。
|
||||
|
||||
|
||||
def get_feature_usage(client: SealAiClient, code: str) -> Feature | None:
|
||||
"""查询单个功能项使用情况。
|
||||
|
||||
常用 code:
|
||||
ai-approval AI 审批余额
|
||||
approval-rules 审核规则配额
|
||||
ai-fill AI 填单开关
|
||||
financial-policies 财务制度配额
|
||||
knowledge-documents 知识文档配额
|
||||
Args:
|
||||
corp_id: 企业 ID,None 时使用当前配置企业
|
||||
"""
|
||||
status = get_billing_status(client)
|
||||
resolved = _resolve_corp_id(corp_id)
|
||||
return _sealai_call(resolved, get_billing_status)
|
||||
|
||||
|
||||
def get_feature_usage(corp_id: str | None = None, code: str = "") -> Feature | None:
|
||||
"""查询单个功能项使用情况,不存在时返回 None。
|
||||
|
||||
Args:
|
||||
corp_id: 企业 ID,None 时使用当前配置企业
|
||||
code: 功能 code,见模块 docstring
|
||||
"""
|
||||
resolved = _resolve_corp_id(corp_id)
|
||||
status = _sealai_call(resolved, get_billing_status)
|
||||
return status.get_feature(code)
|
||||
@@ -3,10 +3,29 @@
|
||||
管理知识文档的增删改查。
|
||||
知识文档是 AI 审批的训练素材,支持按场景(audit/fill)分类,
|
||||
内容以 Markdown 格式存储,前端可渲染。
|
||||
|
||||
不接受 SealAiClient 参数,由 auth_service._sealai_call 统一管理 client 生命周期。
|
||||
调用方只需传 corp_id(None 表示使用当前企业)。
|
||||
|
||||
枚举常量
|
||||
--------
|
||||
scenarios(适用场景):
|
||||
"audit" —— AI 审批场景(审单、规则匹配)
|
||||
"fill" —— AI 填单场景(智能填报)
|
||||
|
||||
status(文档状态):
|
||||
"published" —— 已发布,AI 可读取
|
||||
"draft" —— 草稿,AI 不读取
|
||||
|
||||
content_type(内容类型):
|
||||
"policy" —— 公司政策(默认)
|
||||
其他值视 SealAI 平台支持情况而定
|
||||
|
||||
tags:
|
||||
"__all__" —— 适用于全部分类(特殊保留值)
|
||||
"""
|
||||
|
||||
from typing import Optional
|
||||
from ekb_cli.api.sealai.client import SealAiClient
|
||||
from ekb_cli.api.sealai.knowledge_api import (
|
||||
list_articles,
|
||||
get_article,
|
||||
@@ -17,6 +36,7 @@ from ekb_cli.api.sealai.schemas import (
|
||||
KnowledgeArticle,
|
||||
KnowledgeArticleListData,
|
||||
)
|
||||
from ekb_cli.services.sealai.auth_service import _resolve_corp_id, _sealai_call
|
||||
|
||||
# 合法场景值
|
||||
_VALID_SCENARIOS = {"audit", "fill"}
|
||||
@@ -25,7 +45,7 @@ _VALID_STATUSES = {"published", "draft"}
|
||||
|
||||
|
||||
def get_articles(
|
||||
client: SealAiClient,
|
||||
corp_id: str | None = None,
|
||||
*,
|
||||
scenarios: Optional[list[str]] = None,
|
||||
offset: int = 0,
|
||||
@@ -36,16 +56,18 @@ def get_articles(
|
||||
"""查询知识文档列表。
|
||||
|
||||
Args:
|
||||
scenarios: 按场景过滤,可选值 "audit" | "fill",None 表示不过滤
|
||||
corp_id: 企业 ID,None 时使用当前配置企业
|
||||
scenarios: 按场景过滤,可选值 \"audit\" | \"fill\",None 表示不过滤
|
||||
offset: 分页偏移量(默认 0)
|
||||
limit: 每页数量(默认 20,建议不超过 100)
|
||||
sort_by: 排序字段(默认 createdAt)
|
||||
sort_order: "asc" | "desc"(默认 desc,最新在前)
|
||||
sort_order: \"asc\" | \"desc\"(默认 desc,最新在前)
|
||||
"""
|
||||
if scenarios:
|
||||
_validate_scenarios(scenarios)
|
||||
return list_articles(
|
||||
client,
|
||||
resolved = _resolve_corp_id(corp_id)
|
||||
return _sealai_call(
|
||||
resolved, list_articles,
|
||||
scenarios=scenarios,
|
||||
offset=offset,
|
||||
limit=limit,
|
||||
@@ -54,13 +76,19 @@ def get_articles(
|
||||
)
|
||||
|
||||
|
||||
def get_article_detail(client: SealAiClient, article_id: str) -> KnowledgeArticle:
|
||||
"""查询单篇知识文档详情。"""
|
||||
return get_article(client, article_id)
|
||||
def get_article_detail(corp_id: str | None = None, article_id: str = "") -> KnowledgeArticle:
|
||||
"""查询单篇知识文档详情。
|
||||
|
||||
Args:
|
||||
corp_id: 企业 ID,None 时使用当前配置企业
|
||||
article_id: 文档 ID
|
||||
"""
|
||||
resolved = _resolve_corp_id(corp_id)
|
||||
return _sealai_call(resolved, get_article, article_id)
|
||||
|
||||
|
||||
def add_article(
|
||||
client: SealAiClient,
|
||||
corp_id: str | None = None,
|
||||
*,
|
||||
title: str,
|
||||
purpose: str,
|
||||
@@ -73,21 +101,23 @@ def add_article(
|
||||
"""新建知识文档。
|
||||
|
||||
Args:
|
||||
corp_id: 企业 ID,None 时使用当前配置企业
|
||||
title: 文档标题
|
||||
purpose: 适用说明
|
||||
content: 正文(Markdown 格式)
|
||||
content_type: 内容类型,默认 "policy"
|
||||
status: "published" | "draft",默认 "published"
|
||||
scenarios: 适用场景,如 ["audit", "fill"]
|
||||
tags: 标签列表,"__all__" 表示全部适用
|
||||
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,
|
||||
resolved = _resolve_corp_id(corp_id)
|
||||
return _sealai_call(
|
||||
resolved, create_article,
|
||||
title=title,
|
||||
purpose=purpose,
|
||||
content=content,
|
||||
@@ -99,8 +129,8 @@ def add_article(
|
||||
|
||||
|
||||
def edit_article(
|
||||
client: SealAiClient,
|
||||
article_id: str,
|
||||
corp_id: str | None = None,
|
||||
article_id: str = "",
|
||||
*,
|
||||
title: Optional[str] = None,
|
||||
purpose: Optional[str] = None,
|
||||
@@ -111,14 +141,21 @@ def edit_article(
|
||||
scenarios: Optional[list[str]] = None,
|
||||
tags: Optional[list[str]] = None,
|
||||
) -> KnowledgeArticle:
|
||||
"""更新知识文档(只传需要改动的字段)。"""
|
||||
"""更新知识文档(只传需要改动的字段)。
|
||||
|
||||
Args:
|
||||
corp_id: 企业 ID,None 时使用当前配置企业
|
||||
article_id: 文档 ID
|
||||
其余参数: 对应字段的新值,不传则保持原值
|
||||
"""
|
||||
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,
|
||||
resolved = _resolve_corp_id(corp_id)
|
||||
return _sealai_call(
|
||||
resolved, patch_article,
|
||||
article_id,
|
||||
title=title,
|
||||
purpose=purpose,
|
||||
|
||||
@@ -2,11 +2,13 @@
|
||||
|
||||
管理 AI 审批配置的读取与更新(决策场景提示词 + 集成系统信息)。
|
||||
|
||||
不接受 SealAiClient 参数,由 auth_service._sealai_call 统一管理 client 生命周期。
|
||||
调用方只需传 corp_id(None 表示使用当前企业)。
|
||||
|
||||
核心设计: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,
|
||||
@@ -16,15 +18,21 @@ from ekb_cli.api.sealai.schemas import (
|
||||
AiApprovalSaveResult,
|
||||
DecisionPreferences,
|
||||
)
|
||||
from ekb_cli.services.sealai.auth_service import _resolve_corp_id, _sealai_call
|
||||
|
||||
|
||||
def get_preference(client: SealAiClient) -> AiApprovalConfig:
|
||||
"""查询当前 AI 审批配置(风格偏好 + 集成系统信息)。"""
|
||||
return get_ai_approval_config(client)
|
||||
def get_preference(corp_id: str | None = None) -> AiApprovalConfig:
|
||||
"""查询当前 AI 审批配置(风格偏好 + 集成系统信息)。
|
||||
|
||||
Args:
|
||||
corp_id: 企业 ID,None 时使用当前配置企业
|
||||
"""
|
||||
resolved = _resolve_corp_id(corp_id)
|
||||
return _sealai_call(resolved, get_ai_approval_config)
|
||||
|
||||
|
||||
def update_decision_preferences(
|
||||
client: SealAiClient,
|
||||
corp_id: str | None = None,
|
||||
*,
|
||||
explicit_approve: str | None = None,
|
||||
explicit_reject: str | None = None,
|
||||
@@ -37,6 +45,7 @@ def update_decision_preferences(
|
||||
确保 integrations 等无关字段不丢失。
|
||||
|
||||
Args:
|
||||
corp_id: 企业 ID,None 时使用当前配置企业
|
||||
explicit_approve: 明确通过的条件描述(不传则保持不变)
|
||||
explicit_reject: 明确拒绝的条件描述(不传则保持不变)
|
||||
exempt_approve: 豁免通过的条件描述(不传则保持不变)
|
||||
@@ -44,10 +53,10 @@ def update_decision_preferences(
|
||||
Returns:
|
||||
AiApprovalSaveResult,含 success 标志。
|
||||
"""
|
||||
current = get_ai_approval_config(client)
|
||||
resolved = _resolve_corp_id(corp_id)
|
||||
current = _sealai_call(resolved, get_ai_approval_config)
|
||||
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,
|
||||
@@ -55,12 +64,18 @@ def update_decision_preferences(
|
||||
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)
|
||||
return _sealai_call(resolved, save_ai_approval_config, updated)
|
||||
|
||||
|
||||
def save_preference(
|
||||
client: SealAiClient,
|
||||
config: AiApprovalConfig,
|
||||
corp_id: str | None = None,
|
||||
config: AiApprovalConfig | None = None,
|
||||
) -> AiApprovalSaveResult:
|
||||
"""直接保存完整的 AI 审批配置(整体替换,适合高阶调用方)。"""
|
||||
return save_ai_approval_config(client, config)
|
||||
"""直接保存完整的 AI 审批配置(整体替换,适合高阶调用方)。
|
||||
|
||||
Args:
|
||||
corp_id: 企业 ID,None 时使用当前配置企业
|
||||
config: 完整配置对象
|
||||
"""
|
||||
resolved = _resolve_corp_id(corp_id)
|
||||
return _sealai_call(resolved, save_ai_approval_config, config)
|
||||
@@ -1,9 +1,17 @@
|
||||
"""SealAI 审核规则业务逻辑层。
|
||||
|
||||
管理草稿区规则的增删改查,以及已发布版本历史的查询和发布操作。
|
||||
|
||||
不接受 SealAiClient 参数,由 auth_service._sealai_call 统一管理 client 生命周期。
|
||||
调用方只需传 corp_id(None 表示使用当前企业)。
|
||||
|
||||
strictness 取值
|
||||
---------------
|
||||
MUST_FOLLOW 必须遵守(最严格,违规直接驳回)
|
||||
SHOULD_FOLLOW 应该遵守(违规可提示但不强制驳回)
|
||||
SUGGESTION 建议遵守(仅作为参考提示)
|
||||
"""
|
||||
|
||||
from ekb_cli.api.sealai.client import SealAiClient
|
||||
from ekb_cli.api.sealai.rules_api import (
|
||||
list_rules,
|
||||
create_rule,
|
||||
@@ -18,15 +26,24 @@ from ekb_cli.api.sealai.schemas import (
|
||||
RuleSetVersion,
|
||||
PublishVersionData,
|
||||
)
|
||||
from ekb_cli.services.sealai.auth_service import _resolve_corp_id, _sealai_call
|
||||
|
||||
|
||||
def get_draft_rules(client: SealAiClient) -> DraftRulesData:
|
||||
"""查询草稿区所有审核规则。"""
|
||||
return list_rules(client)
|
||||
# ── 草稿区规则 CRUD ────────────────────────────────────────────────────────────
|
||||
|
||||
def get_draft_rules(corp_id: str | None = None) -> DraftRulesData:
|
||||
"""查询草稿区所有审核规则。
|
||||
|
||||
Args:
|
||||
corp_id: 企业 ID,None 时使用当前配置企业
|
||||
"""
|
||||
resolved = _resolve_corp_id(corp_id)
|
||||
return _sealai_call(resolved, list_rules)
|
||||
|
||||
|
||||
def add_draft_rule(
|
||||
client: SealAiClient,
|
||||
corp_id: str | None = None,
|
||||
*,
|
||||
description: str,
|
||||
scope: str,
|
||||
strictness: str = "MUST_FOLLOW",
|
||||
@@ -34,50 +51,83 @@ def add_draft_rule(
|
||||
"""新建草稿审核规则。
|
||||
|
||||
Args:
|
||||
corp_id: 企业 ID,None 时使用当前配置企业
|
||||
description: 规则描述
|
||||
scope: 适用场景(费用类型等自由文本)
|
||||
strictness: MUST_FOLLOW | SHOULD_FOLLOW | SUGGESTION,默认 MUST_FOLLOW
|
||||
"""
|
||||
_validate_strictness(strictness)
|
||||
return create_rule(client, description=description, scope=scope, strictness=strictness)
|
||||
resolved = _resolve_corp_id(corp_id)
|
||||
return _sealai_call(
|
||||
resolved, create_rule,
|
||||
description=description, scope=scope, strictness=strictness,
|
||||
)
|
||||
|
||||
|
||||
def edit_draft_rule(
|
||||
client: SealAiClient,
|
||||
rule_id: str,
|
||||
corp_id: str | None = None,
|
||||
rule_id: str = "",
|
||||
*,
|
||||
description: str | None = None,
|
||||
scope: str | None = None,
|
||||
strictness: str | None = None,
|
||||
) -> DraftRule:
|
||||
"""更新草稿规则(只传要改动的字段)。"""
|
||||
"""更新草稿规则(只传要改动的字段)。
|
||||
|
||||
Args:
|
||||
corp_id: 企业 ID,None 时使用当前配置企业
|
||||
rule_id: 规则 ID
|
||||
description: 新描述(不传则保持原值)
|
||||
scope: 新适用场景(不传则保持原值)
|
||||
strictness: 新严格度(不传则保持原值)
|
||||
"""
|
||||
if strictness is not None:
|
||||
_validate_strictness(strictness)
|
||||
return update_rule(client, rule_id, description=description, scope=scope, strictness=strictness)
|
||||
resolved = _resolve_corp_id(corp_id)
|
||||
return _sealai_call(
|
||||
resolved, update_rule,
|
||||
rule_id, description=description, scope=scope, strictness=strictness,
|
||||
)
|
||||
|
||||
|
||||
def remove_draft_rule(client: SealAiClient, rule_id: str) -> None:
|
||||
"""删除草稿规则。"""
|
||||
delete_rule(client, rule_id)
|
||||
def remove_draft_rule(corp_id: str | None = None, rule_id: str = "") -> None:
|
||||
"""删除草稿规则。
|
||||
|
||||
Args:
|
||||
corp_id: 企业 ID,None 时使用当前配置企业
|
||||
rule_id: 规则 ID
|
||||
"""
|
||||
resolved = _resolve_corp_id(corp_id)
|
||||
_sealai_call(resolved, delete_rule, rule_id)
|
||||
|
||||
|
||||
def get_published_versions(client: SealAiClient) -> list[RuleSetVersion]:
|
||||
"""查询已发布的规则集版本历史(最新版本在前)。"""
|
||||
return list_rule_set_versions(client)
|
||||
# ── 已发布版本 ────────────────────────────────────────────────────────────────
|
||||
|
||||
def get_published_versions(corp_id: str | None = None) -> list[RuleSetVersion]:
|
||||
"""查询已发布的规则集版本历史(最新版本在前)。
|
||||
|
||||
Args:
|
||||
corp_id: 企业 ID,None 时使用当前配置企业
|
||||
"""
|
||||
resolved = _resolve_corp_id(corp_id)
|
||||
return _sealai_call(resolved, list_rule_set_versions)
|
||||
|
||||
|
||||
def publish_draft_rules(
|
||||
client: SealAiClient,
|
||||
corp_id: str | None = None,
|
||||
*,
|
||||
version_name: str,
|
||||
) -> PublishVersionData:
|
||||
"""将当前草稿区规则发布为新版本。
|
||||
|
||||
Args:
|
||||
version_name: 版本名称,建议格式:"审核规则 版本N"
|
||||
corp_id: 企业 ID,None 时使用当前配置企业
|
||||
version_name: 版本名称,建议格式:\"审核规则 版本N\"
|
||||
Returns:
|
||||
PublishVersionData,含新版本快照和规则数量
|
||||
"""
|
||||
return publish_rule_set_version(client, version_name=version_name)
|
||||
resolved = _resolve_corp_id(corp_id)
|
||||
return _sealai_call(resolved, publish_rule_set_version, version_name=version_name)
|
||||
|
||||
|
||||
# ── 内部校验 ──────────────────────────────────────────────────────────────────
|
||||
|
||||
Reference in new issue
Block a user