diff --git a/src/ekb_cli/api/sealai/__init__.py b/src/ekb_cli/api/sealai/__init__.py index 2815efb..50d24f6 100644 --- a/src/ekb_cli/api/sealai/__init__.py +++ b/src/ekb_cli/api/sealai/__init__.py @@ -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", ] diff --git a/src/ekb_cli/api/sealai/client.py b/src/ekb_cli/api/sealai/client.py index f2fa834..15030a0 100644 --- a/src/ekb_cli/api/sealai/client.py +++ b/src/ekb_cli/api/sealai/client.py @@ -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 diff --git a/src/ekb_cli/api/sealai/rules_api.py b/src/ekb_cli/api/sealai/rules_api.py new file mode 100644 index 0000000..96f45bf --- /dev/null +++ b/src/ekb_cli/api/sealai/rules_api.py @@ -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 diff --git a/src/ekb_cli/api/sealai/schemas.py b/src/ekb_cli/api/sealai/schemas.py index e9f1442..5fde604 100644 --- a/src/ekb_cli/api/sealai/schemas.py +++ b/src/ekb_cli/api/sealai/schemas.py @@ -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" diff --git a/src/ekb_cli/commands/sealai/README.sealai.md b/src/ekb_cli/commands/sealai/README.sealai.md index 21431ec..1835e9d 100644 --- a/src/ekb_cli/commands/sealai/README.sealai.md +++ b/src/ekb_cli/commands/sealai/README.sealai.md @@ -10,11 +10,13 @@ api/sealai/ ├── client.py SealAiClient(httpx,Bearer JWT 认证) ├── schemas.py Pydantic 数据模型 ├── auth_api.py GET /api/v1/auth/me -└── billing_api.py GET /api/v1/billing/status +├── billing_api.py GET /api/v1/billing/status +└── rules_api.py 草稿规则 CRUD + 已发布版本历史 services/sealai/ -├── auth_service.py SSO token 换取 + 本地缓存管理 -└── billing_service.py 订阅状态查询 + 单功能配额查询 +├── auth_service.py SSO token 换取 + 本地缓存管理 +├── billing_service.py 订阅状态查询 + 单功能配额查询 +└── rules_service.py 草稿规则增删改查 + 版本历史查询 commands/sealai/ ├── whoami.py ekb sealai whoami @@ -40,13 +42,33 @@ server/sealai_router.py /sealai/me /sealai/billing ## 已知 API 路径 -> `client.get(path)` 直接传完整路径(含 `/api` 前缀)。 +> `client.get/post/put/delete(path)` 直接传完整路径(含 `/api` 前缀)。 ``` -GET /api/v1/auth/me # 当前用户身份(user + tenant) -GET /api/v1/billing/status # 订阅状态 + 各功能配额 +# 认证 +GET /api/v1/auth/me 当前用户身份(user + tenant) + +# 计费 +GET /api/v1/billing/status 订阅状态 + 各功能配额 + +# 审核规则(草稿区) +GET /api/v1/rules 草稿规则列表(含 hasPendingDeletes) +POST /api/v1/rules 新建草稿规则(body: description/scope/strictness) +PUT /api/v1/rules/{id} 更新草稿规则(partial,只传改动字段) +DELETE /api/v1/rules/{id} 删除草稿规则 + +# 审核规则(已发布版本历史) +GET /api/v1/rule-set-versions 已发布版本列表(倒序,含每版规则快照) ``` +### strictness 枚举值 + +| 值 | 含义 | +|----|------| +| `MUST_FOLLOW` | 必须遵守 | +| `SHOULD_FOLLOW` | 应当遵守 | +| `SUGGESTION` | 建议 | + ## 功能项(Feature)说明 | code | 名称 | 类型 | 说明 | diff --git a/src/ekb_cli/services/sealai/__init__.py b/src/ekb_cli/services/sealai/__init__.py index aa56c60..c2156a7 100644 --- a/src/ekb_cli/services/sealai/__init__.py +++ b/src/ekb_cli/services/sealai/__init__.py @@ -9,6 +9,13 @@ from ekb_cli.services.sealai.billing_service import ( query_billing_status, get_feature_usage, ) +from ekb_cli.services.sealai.rules_service import ( + get_draft_rules, + add_draft_rule, + edit_draft_rule, + remove_draft_rule, + get_published_versions, +) __all__ = [ "get_sealai_client", @@ -16,4 +23,9 @@ __all__ = [ "invalidate_token", "query_billing_status", "get_feature_usage", + "get_draft_rules", + "add_draft_rule", + "edit_draft_rule", + "remove_draft_rule", + "get_published_versions", ] diff --git a/src/ekb_cli/services/sealai/rules_service.py b/src/ekb_cli/services/sealai/rules_service.py new file mode 100644 index 0000000..1a964e2 --- /dev/null +++ b/src/ekb_cli/services/sealai/rules_service.py @@ -0,0 +1,74 @@ +"""SealAI 审核规则业务逻辑层。 + +管理草稿区规则的增删改查,以及已发布版本历史的查询。 +""" + +from ekb_cli.api.sealai.client import SealAiClient +from ekb_cli.api.sealai.rules_api import ( + list_rules, + create_rule, + update_rule, + delete_rule, + list_rule_set_versions, +) +from ekb_cli.api.sealai.schemas import DraftRule, DraftRulesData, RuleSetVersion + + +def get_draft_rules(client: SealAiClient) -> DraftRulesData: + """查询草稿区所有审核规则。""" + return list_rules(client) + + +def add_draft_rule( + client: SealAiClient, + description: str, + scope: str, + strictness: str = "MUST_FOLLOW", +) -> DraftRule: + """新建草稿审核规则。 + + Args: + description: 规则描述 + scope: 适用场景(费用类型等自由文本) + strictness: MUST_FOLLOW | SHOULD_FOLLOW | SUGGESTION,默认 MUST_FOLLOW + """ + _validate_strictness(strictness) + return create_rule(client, description=description, scope=scope, strictness=strictness) + + +def edit_draft_rule( + client: SealAiClient, + rule_id: str, + *, + description: str | None = None, + scope: str | None = None, + strictness: str | None = None, +) -> DraftRule: + """更新草稿规则(只传要改动的字段)。""" + if strictness is not None: + _validate_strictness(strictness) + return update_rule(client, rule_id, description=description, scope=scope, strictness=strictness) + + +def remove_draft_rule(client: SealAiClient, rule_id: str) -> None: + """删除草稿规则。""" + delete_rule(client, rule_id) + + +def get_published_versions(client: SealAiClient) -> list[RuleSetVersion]: + """查询已发布的规则集版本历史(最新版本在前)。""" + return list_rule_set_versions(client) + + +# ── 内部校验 ────────────────────────────────────────────────────────────────── + +_VALID_STRICTNESS = {"MUST_FOLLOW", "SHOULD_FOLLOW", "SUGGESTION"} + + +def _validate_strictness(value: str) -> None: + if value not in _VALID_STRICTNESS: + from ekb_cli.errors import EkbError + raise EkbError( + f"strictness 无效:{value!r},合法值为 {sorted(_VALID_STRICTNESS)}", + code=422, + )