新增 SealAI 审核规则服务层:草稿 CRUD + 版本历史查询

This commit is contained in:
gitea committed 2026-04-14 18:10:40 +08:00
1 parent 29ec257788
commit 9525f327cd
7 files changed
+264 -8

No files matched your search

+12
View File
@@ -6,9 +6,21 @@
from ekb_cli.api.sealai.client import SealAiClient
from ekb_cli.api.sealai.auth_api import get_me
from ekb_cli.api.sealai.billing_api import get_billing_status
from ekb_cli.api.sealai.rules_api import (
list_rules,
create_rule,
update_rule,
delete_rule,
list_rule_set_versions,
)
__all__ = [
"SealAiClient",
"get_me",
"get_billing_status",
"list_rules",
"create_rule",
"update_rule",
"delete_rule",
"list_rule_set_versions",
]
+9 -2
View File
@@ -56,6 +56,14 @@ class SealAiClient:
self._handle_error(e)
return resp.json()
def put(self, path: str, body: dict | None = None) -> Any:
try:
resp = self._client.put(path, json=body or {})
resp.raise_for_status()
except httpx.HTTPStatusError as e:
self._handle_error(e)
return resp.json()
def patch(self, path: str, body: dict | None = None) -> Any:
try:
resp = self._client.patch(path, json=body or {})
@@ -64,13 +72,12 @@ class SealAiClient:
self._handle_error(e)
return resp.json()
def delete(self, path: str) -> Any:
def delete(self, path: str) -> None:
try:
resp = self._client.delete(path)
resp.raise_for_status()
except httpx.HTTPStatusError as e:
self._handle_error(e)
return resp.json()
def __enter__(self):
return self
+75
View File
@@ -0,0 +1,75 @@
"""SealAI 审核规则 API 封装(传输层)。
已逆向确认端点:
GET /api/v1/rules 草稿规则列表
POST /api/v1/rules 新建草稿规则
PUT /api/v1/rules/{id} 更新草稿规则(partial,只传改动字段)
DELETE /api/v1/rules/{id} 删除草稿规则
GET /api/v1/rule-set-versions 已发布版本历史列表
"""
from ekb_cli.api.sealai.client import SealAiClient
from ekb_cli.api.sealai.schemas import (
SealAiResponse,
DraftRule,
DraftRulesData,
RuleSetVersion,
)
def list_rules(client: SealAiClient) -> DraftRulesData:
"""获取草稿区所有审核规则。"""
raw = client.get("/api/v1/rules")
return SealAiResponse[DraftRulesData].model_validate(raw).data
def create_rule(
client: SealAiClient,
description: str,
scope: str,
strictness: str,
) -> DraftRule:
"""新建草稿审核规则。
Args:
description: 规则描述
scope: 适用场景(费用类型等自由文本)
strictness: MUST_FOLLOW | SHOULD_FOLLOW | SUGGESTION
"""
raw = client.post("/api/v1/rules", {
"description": description,
"scope": scope,
"strictness": strictness,
})
return SealAiResponse[DraftRule].model_validate(raw).data
def update_rule(
client: SealAiClient,
rule_id: str,
*,
description: str | None = None,
scope: str | None = None,
strictness: str | None = None,
) -> DraftRule:
"""更新草稿规则(partial,只传改动字段)。"""
body = {}
if description is not None:
body["description"] = description
if scope is not None:
body["scope"] = scope
if strictness is not None:
body["strictness"] = strictness
raw = client.put(f"/api/v1/rules/{rule_id}", body)
return SealAiResponse[DraftRule].model_validate(raw).data
def delete_rule(client: SealAiClient, rule_id: str) -> None:
"""删除草稿规则。"""
client.delete(f"/api/v1/rules/{rule_id}")
def list_rule_set_versions(client: SealAiClient) -> list[RuleSetVersion]:
"""获取已发布的规则集版本历史(倒序,最新在前)。"""
raw = client.get("/api/v1/rule-set-versions")
return SealAiResponse[list[RuleSetVersion]].model_validate(raw).data
+54
View File
@@ -122,3 +122,57 @@ class BillingStatusData(BaseModel):
if f.code == code:
return f
return None
# ── 审核规则草稿 ──────────────────────────────────────────────────────────────
class DraftRule(BaseModel):
"""草稿区单条审核规则。"""
id: str
tenantId: str
description: str # 规则描述
scope: str # 适用场景(费用类型等)
strictness: str # MUST_FOLLOW / SHOULD_FOLLOW / SUGGESTION
status: str # DRAFT
createdAt: int
updatedAt: int
class Config:
extra = "allow"
class DraftRulesData(BaseModel):
"""GET /api/v1/rules 响应体 data 字段。"""
rules: list[DraftRule]
hasPendingDeletes: bool = False
class Config:
extra = "allow"
# ── 已发布版本 ────────────────────────────────────────────────────────────────
class PublishedRuleSnapshot(BaseModel):
"""版本快照内单条规则(无 id,不可变)。"""
description: str
scope: str
strictness: str
class Config:
extra = "allow"
class RuleSetVersion(BaseModel):
"""已发布的规则集版本快照。"""
id: str
tenantId: str
versionNumber: int
versionName: str
publishedBy: str
publishedByName: str
rules: list[PublishedRuleSnapshot]
publishedAt: int
createdAt: int
class Config:
extra = "allow"