SealAI 审核规则 CLI + HTTP 路由全部完成,补充模块文档
This commit is contained in:
1 parent
9525f327cd
commit
ef076898ea
8 files changed
+504
-15
No files matched your search
@@ -1,15 +1,30 @@
|
||||
"""sealai 子命令组
|
||||
|
||||
对应 CLI 命令:
|
||||
ekb sealai whoami → 查看当前 SealAI 用户身份及租户信息
|
||||
ekb sealai billing → 查看当前企业 SealAI 订阅状态及功能配额
|
||||
ekb sealai whoami → 查看当前 SealAI 用户身份及租户信息
|
||||
ekb sealai billing → 查看当前企业 SealAI 订阅状态及功能配额
|
||||
ekb sealai rules → 查看草稿区审核规则列表
|
||||
ekb sealai rule-create → 新建草稿审核规则
|
||||
ekb sealai rule-update → 更新草稿审核规则
|
||||
ekb sealai rule-delete → 删除草稿审核规则
|
||||
ekb sealai versions → 查看已发布规则集版本历史
|
||||
"""
|
||||
|
||||
import typer
|
||||
from ekb_cli.commands.sealai.whoami import cmd_whoami
|
||||
from ekb_cli.commands.sealai.billing import cmd_billing
|
||||
from ekb_cli.commands.sealai.rules import cmd_rules
|
||||
from ekb_cli.commands.sealai.rule_create import cmd_rule_create
|
||||
from ekb_cli.commands.sealai.rule_update import cmd_rule_update
|
||||
from ekb_cli.commands.sealai.rule_delete import cmd_rule_delete
|
||||
from ekb_cli.commands.sealai.versions import cmd_versions
|
||||
|
||||
app = typer.Typer(help="SealAI 智能填单/审核平台管理")
|
||||
|
||||
app.command("whoami")(cmd_whoami)
|
||||
app.command("billing")(cmd_billing)
|
||||
app.command("rules")(cmd_rules)
|
||||
app.command("rule-create")(cmd_rule_create)
|
||||
app.command("rule-update")(cmd_rule_update)
|
||||
app.command("rule-delete")(cmd_rule_delete)
|
||||
app.command("versions")(cmd_versions)
|
||||
@@ -0,0 +1,35 @@
|
||||
"""sealai rule-create:新建草稿审核规则
|
||||
|
||||
表现层:交互式输入 + rich 输出。
|
||||
业务逻辑委托给 services.sealai.rules_service。
|
||||
"""
|
||||
|
||||
import typer
|
||||
from rich import print as rprint
|
||||
|
||||
from ekb_cli.services.sealai import get_sealai_client, add_draft_rule
|
||||
|
||||
_STRICTNESS_CHOICES = ["MUST_FOLLOW", "SHOULD_FOLLOW", "SUGGESTION"]
|
||||
|
||||
|
||||
def cmd_rule_create(
|
||||
corp_id: str = typer.Option(None, "--corp", "-c", help="指定企业 corpId,默认使用当前企业"),
|
||||
description: str = typer.Option(None, "--description", "-d", help="规则描述"),
|
||||
scope: str = typer.Option(None, "--scope", "-s", help="适用场景(费用类型等)"),
|
||||
strictness: str = typer.Option(
|
||||
"MUST_FOLLOW", "--strictness",
|
||||
help="严格程度:MUST_FOLLOW / SHOULD_FOLLOW / SUGGESTION",
|
||||
),
|
||||
):
|
||||
"""新建草稿审核规则"""
|
||||
if description is None:
|
||||
description = typer.prompt("规则描述").strip()
|
||||
if scope is None:
|
||||
scope = typer.prompt("适用场景").strip()
|
||||
if not description or not scope:
|
||||
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)
|
||||
rprint(f"[green]✓[/green] 规则已创建:[bold]{rule.description}[/bold](ID: {rule.id})")
|
||||
@@ -0,0 +1,27 @@
|
||||
"""sealai rule-delete:删除草稿审核规则
|
||||
|
||||
表现层:二次确认 + rich 输出。
|
||||
业务逻辑委托给 services.sealai.rules_service。
|
||||
"""
|
||||
|
||||
import typer
|
||||
from rich import print as rprint
|
||||
|
||||
from ekb_cli.services.sealai import get_sealai_client, remove_draft_rule
|
||||
|
||||
|
||||
def cmd_rule_delete(
|
||||
rule_id: str = typer.Argument(..., help="草稿规则 ID"),
|
||||
yes: bool = typer.Option(False, "--yes", "-y", help="跳过确认直接删除"),
|
||||
corp_id: str = typer.Option(None, "--corp", "-c", help="指定企业 corpId,默认使用当前企业"),
|
||||
):
|
||||
"""删除草稿审核规则"""
|
||||
if not yes:
|
||||
confirmed = typer.confirm(f"确定要删除规则 {rule_id}?")
|
||||
if not confirmed:
|
||||
rprint("[dim]已取消[/dim]")
|
||||
raise typer.Exit()
|
||||
|
||||
client = get_sealai_client(corp_id=corp_id)
|
||||
remove_draft_rule(client, rule_id)
|
||||
rprint(f"[green]✓[/green] 规则 {rule_id} 已删除")
|
||||
@@ -0,0 +1,35 @@
|
||||
"""sealai rule-update:更新草稿审核规则
|
||||
|
||||
表现层:只负责参数解析 + rich 输出。
|
||||
业务逻辑委托给 services.sealai.rules_service。
|
||||
"""
|
||||
|
||||
import typer
|
||||
from rich import print as rprint
|
||||
|
||||
from ekb_cli.services.sealai import get_sealai_client, edit_draft_rule
|
||||
|
||||
|
||||
def cmd_rule_update(
|
||||
rule_id: str = typer.Argument(..., help="草稿规则 ID"),
|
||||
description: str = typer.Option(None, "--description", "-d", help="新描述"),
|
||||
scope: str = typer.Option(None, "--scope", "-s", help="新适用场景"),
|
||||
strictness: str = typer.Option(
|
||||
None, "--strictness",
|
||||
help="新严格程度:MUST_FOLLOW / SHOULD_FOLLOW / SUGGESTION",
|
||||
),
|
||||
corp_id: str = typer.Option(None, "--corp", "-c", help="指定企业 corpId,默认使用当前企业"),
|
||||
):
|
||||
"""更新草稿审核规则(只传要修改的字段)"""
|
||||
if not any([description, scope, strictness]):
|
||||
rprint("[yellow]未指定任何修改字段,无需更新[/yellow]")
|
||||
raise typer.Exit()
|
||||
|
||||
client = get_sealai_client(corp_id=corp_id)
|
||||
rule = edit_draft_rule(
|
||||
client, rule_id,
|
||||
description=description,
|
||||
scope=scope,
|
||||
strictness=strictness,
|
||||
)
|
||||
rprint(f"[green]✓[/green] 规则已更新:[bold]{rule.description}[/bold](ID: {rule.id})")
|
||||
@@ -0,0 +1,46 @@
|
||||
"""sealai rules:查看草稿规则列表
|
||||
|
||||
表现层:只负责 rich 格式化输出。
|
||||
业务逻辑委托给 services.sealai.rules_service。
|
||||
"""
|
||||
|
||||
import typer
|
||||
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
|
||||
|
||||
console = Console()
|
||||
|
||||
_STRICTNESS_LABEL = {
|
||||
"MUST_FOLLOW": "[red]必须遵守[/red]",
|
||||
"SHOULD_FOLLOW": "[yellow]应当遵守[/yellow]",
|
||||
"SUGGESTION": "[dim]建议[/dim]",
|
||||
}
|
||||
|
||||
|
||||
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)
|
||||
|
||||
if not data.rules:
|
||||
rprint("[yellow]草稿区暂无审核规则[/yellow]")
|
||||
raise typer.Exit()
|
||||
|
||||
table = Table(title=f"草稿审核规则(共 {len(data.rules)} 条)", show_lines=True)
|
||||
table.add_column("ID", style="dim", no_wrap=True)
|
||||
table.add_column("描述")
|
||||
table.add_column("适用场景")
|
||||
table.add_column("严格程度")
|
||||
|
||||
for r in data.rules:
|
||||
label = _STRICTNESS_LABEL.get(r.strictness, r.strictness)
|
||||
table.add_row(r.id[:8] + "…", r.description, r.scope, label)
|
||||
|
||||
console.print(table)
|
||||
if data.hasPendingDeletes:
|
||||
rprint("[yellow]⚠ 草稿区有待删除规则,发布后生效[/yellow]")
|
||||
@@ -0,0 +1,66 @@
|
||||
"""sealai versions:查看已发布规则集版本历史
|
||||
|
||||
表现层:只负责 rich 格式化输出。
|
||||
业务逻辑委托给 services.sealai.rules_service。
|
||||
"""
|
||||
|
||||
import typer
|
||||
from datetime import datetime, timezone
|
||||
from rich.console import Console
|
||||
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
|
||||
|
||||
console = Console()
|
||||
|
||||
_STRICTNESS_LABEL = {
|
||||
"MUST_FOLLOW": "必须",
|
||||
"SHOULD_FOLLOW": "应当",
|
||||
"SUGGESTION": "建议",
|
||||
}
|
||||
|
||||
|
||||
def cmd_versions(
|
||||
corp_id: str = typer.Option(None, "--corp", "-c", help="指定企业 corpId,默认使用当前企业"),
|
||||
detail: bool = typer.Option(False, "--detail", help="展开每个版本的规则列表"),
|
||||
):
|
||||
"""查看已发布规则集版本历史"""
|
||||
client = get_sealai_client(corp_id=corp_id)
|
||||
versions = get_published_versions(client)
|
||||
|
||||
if not versions:
|
||||
rprint("[yellow]暂无已发布版本[/yellow]")
|
||||
raise typer.Exit()
|
||||
|
||||
if not detail:
|
||||
table = Table(title=f"已发布版本(共 {len(versions)} 个)")
|
||||
table.add_column("版本号", justify="right")
|
||||
table.add_column("版本名称")
|
||||
table.add_column("发布者")
|
||||
table.add_column("规则数", justify="right")
|
||||
table.add_column("发布时间")
|
||||
|
||||
for v in versions:
|
||||
ts = datetime.fromtimestamp(v.publishedAt / 1000, tz=timezone.utc).strftime("%Y-%m-%d %H:%M")
|
||||
table.add_row(
|
||||
str(v.versionNumber),
|
||||
v.versionName,
|
||||
v.publishedByName,
|
||||
str(len(v.rules)),
|
||||
ts,
|
||||
)
|
||||
console.print(table)
|
||||
console.print("[dim]查看规则详情:ekb sealai versions --detail[/dim]")
|
||||
else:
|
||||
for v in versions:
|
||||
ts = datetime.fromtimestamp(v.publishedAt / 1000, tz=timezone.utc).strftime("%Y-%m-%d %H:%M")
|
||||
tree = Tree(
|
||||
f"[bold cyan]版本 {v.versionNumber}[/bold cyan] {v.versionName} "
|
||||
f"[dim]{v.publishedByName} · {ts}[/dim]"
|
||||
)
|
||||
for r in v.rules:
|
||||
label = _STRICTNESS_LABEL.get(r.strictness, r.strictness)
|
||||
tree.add(f"[green]{label}[/green] {r.description} [dim]({r.scope})[/dim]")
|
||||
console.print(tree)
|
||||
@@ -1,30 +1,45 @@
|
||||
"""Server:SealAI 路由
|
||||
|
||||
对应 CLI 命令:
|
||||
ekb sealai whoami → GET /sealai/me
|
||||
ekb sealai billing → GET /sealai/billing
|
||||
ekb sealai whoami → GET /sealai/me
|
||||
ekb sealai billing → GET /sealai/billing
|
||||
ekb sealai rules → GET /sealai/rules
|
||||
ekb sealai rule-create → POST /sealai/rules
|
||||
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
|
||||
|
||||
注意:SealAI 接口使用独立的 SealAiClient(Bearer JWT),
|
||||
不使用快报主站的 EkbClient(三 Header 认证)。
|
||||
"""
|
||||
|
||||
from fastapi import APIRouter, Query
|
||||
from pydantic import BaseModel
|
||||
|
||||
from ekb_cli.services.sealai import (
|
||||
get_sealai_client,
|
||||
query_billing_status,
|
||||
get_draft_rules,
|
||||
add_draft_rule,
|
||||
edit_draft_rule,
|
||||
remove_draft_rule,
|
||||
get_published_versions,
|
||||
)
|
||||
from ekb_cli.api.sealai import get_me
|
||||
from ekb_cli.api.sealai.schemas import MeData, BillingStatusData
|
||||
from ekb_cli.api.sealai.schemas import (
|
||||
MeData,
|
||||
BillingStatusData,
|
||||
DraftRule,
|
||||
DraftRulesData,
|
||||
RuleSetVersion,
|
||||
)
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.get(
|
||||
"/me",
|
||||
response_model=MeData,
|
||||
summary="查看当前 SealAI 用户身份及租户信息",
|
||||
)
|
||||
# ── 认证 / 计费 ────────────────────────────────────────────────────────────────
|
||||
|
||||
@router.get("/me", response_model=MeData, summary="查看当前 SealAI 用户身份及租户信息")
|
||||
def sealai_whoami(
|
||||
corp_id: str = Query(None, description="企业 corpId,不传则使用当前企业"),
|
||||
refresh: bool = Query(False, description="强制刷新 Bearer token"),
|
||||
@@ -33,13 +48,84 @@ def sealai_whoami(
|
||||
return get_me(client)
|
||||
|
||||
|
||||
@router.get(
|
||||
"/billing",
|
||||
response_model=BillingStatusData,
|
||||
summary="查看 SealAI 订阅状态及功能配额",
|
||||
)
|
||||
@router.get("/billing", response_model=BillingStatusData, summary="查看 SealAI 订阅状态及功能配额")
|
||||
def sealai_billing(
|
||||
corp_id: str = Query(None, description="企业 corpId,不传则使用当前企业"),
|
||||
):
|
||||
client = get_sealai_client(corp_id=corp_id)
|
||||
return query_billing_status(client)
|
||||
|
||||
|
||||
# ── 审核规则(草稿区) ─────────────────────────────────────────────────────────
|
||||
|
||||
@router.get("/rules", response_model=DraftRulesData, summary="查看草稿区审核规则列表")
|
||||
def sealai_list_rules(
|
||||
corp_id: str = Query(None, description="企业 corpId,不传则使用当前企业"),
|
||||
):
|
||||
client = get_sealai_client(corp_id=corp_id)
|
||||
return get_draft_rules(client)
|
||||
|
||||
|
||||
class RuleCreateRequest(BaseModel):
|
||||
description: str
|
||||
scope: str
|
||||
strictness: str = "MUST_FOLLOW"
|
||||
|
||||
|
||||
@router.post("/rules", response_model=DraftRule, summary="新建草稿审核规则")
|
||||
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,
|
||||
description=body.description,
|
||||
scope=body.scope,
|
||||
strictness=body.strictness,
|
||||
)
|
||||
|
||||
|
||||
class RuleUpdateRequest(BaseModel):
|
||||
description: str | None = None
|
||||
scope: str | None = None
|
||||
strictness: str | None = None
|
||||
|
||||
|
||||
@router.put("/rules/{rule_id}", response_model=DraftRule, summary="更新草稿审核规则(partial)")
|
||||
def sealai_update_rule(
|
||||
rule_id: str,
|
||||
body: RuleUpdateRequest,
|
||||
corp_id: str = Query(None, description="企业 corpId,不传则使用当前企业"),
|
||||
):
|
||||
client = get_sealai_client(corp_id=corp_id)
|
||||
return edit_draft_rule(
|
||||
client, rule_id,
|
||||
description=body.description,
|
||||
scope=body.scope,
|
||||
strictness=body.strictness,
|
||||
)
|
||||
|
||||
|
||||
@router.delete("/rules/{rule_id}", summary="删除草稿审核规则")
|
||||
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)
|
||||
return {"success": True, "rule_id": rule_id}
|
||||
|
||||
|
||||
# ── 已发布版本历史 ─────────────────────────────────────────────────────────────
|
||||
|
||||
@router.get(
|
||||
"/rule-set-versions",
|
||||
response_model=list[RuleSetVersion],
|
||||
summary="查看已发布规则集版本历史",
|
||||
)
|
||||
def sealai_list_versions(
|
||||
corp_id: str = Query(None, description="企业 corpId,不传则使用当前企业"),
|
||||
):
|
||||
client = get_sealai_client(corp_id=corp_id)
|
||||
return get_published_versions(client)
|
||||
Reference in new issue
Block a user