From 54ca2ffc4bf39ddd8ae0107314112c066297d506 Mon Sep 17 00:00:00 2001 From: yunyingk Date: Tue, 14 Apr 2026 16:47:20 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20SealAI=20CLI=20=E5=91=BD?= =?UTF-8?q?=E4=BB=A4=E5=B1=82=EF=BC=88whoami/billing=EF=BC=89+=20HTTP=20?= =?UTF-8?q?=E8=B7=AF=E7=94=B1=20+=20=E6=96=87=E6=A1=A3=E5=90=8C=E6=AD=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CLAUDE.md | 84 +++++++++++++++++++------ src/ekb_cli/__init__.py | 2 + src/ekb_cli/commands/help.py | 5 ++ src/ekb_cli/commands/sealai/__init__.py | 15 +++++ src/ekb_cli/commands/sealai/billing.py | 63 +++++++++++++++++++ src/ekb_cli/commands/sealai/whoami.py | 49 +++++++++++++++ src/ekb_cli/server/__init__.py | 4 +- src/ekb_cli/server/sealai_router.py | 45 +++++++++++++ 8 files changed, 248 insertions(+), 19 deletions(-) create mode 100644 src/ekb_cli/commands/sealai/billing.py create mode 100644 src/ekb_cli/commands/sealai/whoami.py create mode 100644 src/ekb_cli/server/sealai_router.py diff --git a/CLAUDE.md b/CLAUDE.md index c2ec553..328487a 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -56,39 +56,54 @@ src/ekb_cli/ │ ├── __init__.py # EkbClient + get_client() + 统一 re-export │ ├── agent.py # Agent 相关 API 函数 │ ├── rule.py # Rule 相关 API 函数 -│ └── schemas.py # Pydantic 模型(快报响应 + Server 请求/响应) +│ ├── schemas.py # Pydantic 模型(快报响应 + Server 请求/响应) +│ └── sealai/ # SealAI 子平台传输层 +│ ├── __init__.py # SealAiClient + 统一 re-export +│ ├── client.py # SealAiClient(Bearer JWT 认证) +│ ├── schemas.py # SealAI Pydantic 模型 +│ ├── auth_api.py # GET /api/v1/auth/me +│ └── billing_api.py # GET /api/v1/billing/status │ ├── services/ # 业务逻辑层:校验、编排、组合查询 │ ├── __init__.py # 统一 re-export │ ├── auth_service.py # 企业认证配置管理 │ ├── agent_service.py # Agent CRUD + copy + 高阶组合查询 -│ └── rule_service.py # Rule 查询(按编码/ID)+ CRUD +│ ├── rule_service.py # Rule 查询(按编码/ID)+ CRUD +│ └── sealai/ # SealAI 子平台业务逻辑层 +│ ├── __init__.py # 统一 re-export +│ ├── auth_service.py # SSO token 换取 + 本地缓存(~/.config/ekb-cli/sealai_tokens.json) +│ └── billing_service.py # 订阅状态查询 + 单功能配额查询 │ ├── server/ # HTTP Server(FastAPI) │ ├── __init__.py # create_app() 工厂 + 全局异常处理 │ ├── deps.py # FastAPI 依赖注入(ClientDep) │ ├── auth_router.py # /auth/* 路由 │ ├── agent_router.py # /agents/* 路由 -│ └── rule_router.py # /rules/* 路由 +│ ├── rule_router.py # /rules/* 路由 +│ └── sealai_router.py # /sealai/* 路由 │ └── commands/ # CLI 表现层(薄壳) ├── auth.py # ekb auth 命令组 ├── help.py # ekb commands(命令树) ├── serve.py # ekb serve(启动 HTTP Server) - └── agent/ - ├── __init__.py # agent 子命令组注册 - ├── list.py # ekb agent list - ├── detail.py # ekb agent get - ├── create.py # ekb agent create - ├── copy.py # ekb agent copy - ├── update.py # ekb agent update - ├── delete.py # ekb agent delete - ├── logs.py # ekb agent logs(stub) - ├── rule_list.py # ekb agent rules - ├── rule_detail.py # ekb agent rule-get <编码或ID> - ├── rule_create.py # ekb agent rule-create - ├── README.agent.md # agent 枚举常量、条款语义、命令速查 - └── README.rule.md # rule 枚举常量、关联关系、命令速查 + ├── agent/ + │ ├── __init__.py # agent 子命令组注册 + │ ├── list.py # ekb agent list + │ ├── detail.py # ekb agent get + │ ├── create.py # ekb agent create + │ ├── copy.py # ekb agent copy + │ ├── update.py # ekb agent update + │ ├── delete.py # ekb agent delete + │ ├── logs.py # ekb agent logs(stub) + │ ├── rule_list.py # ekb agent rules + │ ├── rule_detail.py # ekb agent rule-get <编码或ID> + │ ├── rule_create.py # ekb agent rule-create + │ ├── README.agent.md # agent 枚举常量、条款语义、命令速查 + │ └── README.rule.md # rule 枚举常量、关联关系、命令速查 + └── sealai/ + ├── __init__.py # sealai 子命令组注册 + ├── whoami.py # ekb sealai whoami + └── billing.py # ekb sealai billing ``` > **新增命令**:在 `commands/` 对应目录添加文件 → 在 `services/` 添加业务逻辑 → @@ -117,6 +132,9 @@ ekb │ ├── rule-create 新建审核规则 │ └── logs 查看审核日志(待实现) ├── serve 启动 HTTP API Server(默认 127.0.0.1:9528) +├── sealai +│ ├── whoami 查看当前 SealAI 用户身份及租户信息 +│ └── billing 查看当前企业 SealAI 订阅状态及功能配额 └── commands 显示命令树(可带组名) ``` @@ -140,10 +158,40 @@ ekb | `ekb agent rule-create` | `POST /rules` | POST | | —(高阶接口)— | `GET /agents/dedup-rules` | GET | | —(高阶接口)— | `PATCH /rules/{rule_ref}` | PATCH | +| `ekb sealai whoami` | `GET /sealai/me` | GET | +| `ekb sealai billing` | `GET /sealai/billing` | GET | > API 文档(Swagger UI)在 Server 启动后访问:`http://127.0.0.1:9528/docs` -## 已知快报 API 路径 +## SealAI 子平台 + +SealAI 是独立于快报主站的 Next.js 平台(`{corpId.lower()}.sealai.cc`),提供 AI 填单/审批功能。 +Kubernetes/Istio 后端,认证体系完全独立。 + +### SealAI 认证流程 + +``` +快报 accesstoken + → GET https://{corpId.lower()}.sealai.cc/api/auth/oauth2/session/oem-hosecloud + ?token={accesstoken}&returnToken=1 + → Bearer JWT(缓存到 ~/.config/ekb-cli/sealai_tokens.json,TTL 1小时) + → SealAiClient(corp_id, bearer_token) +``` + +- corpId 转小写作为子域名:`ID01Jdq2yIDnMr` → `id01jdq2yidnmr.sealai.cc` +- token 缓存格式:`{ "corpId": { "token": "...", "fetched_at": 1712345678 } }` +- 401 时自动清除缓存,下次调用触发重新换取 + +### 已知 SealAI API 路径 + +> 所有路径含完整前缀,`client.get(path)` 直接传完整路径。 + +``` +GET /api/v1/auth/me # 当前用户身份(user + tenant) +GET /api/v1/billing/status # 订阅状态 + 各功能配额 +``` + + > 所有路径前缀为 `/api`,后端框架:Spring Boot(com.hosecloud.habserver)。 > `client.get(path)` 会自动补 `/api` 前缀,路径写 `/engine/...` 即可。 diff --git a/src/ekb_cli/__init__.py b/src/ekb_cli/__init__.py index 52f6d27..693536b 100644 --- a/src/ekb_cli/__init__.py +++ b/src/ekb_cli/__init__.py @@ -6,6 +6,7 @@ import typer from ekb_cli.commands import auth from ekb_cli.commands.agent import app as agent_app +from ekb_cli.commands.sealai import app as sealai_app from ekb_cli.commands.help import cmd_commands from ekb_cli.commands.serve import cmd_serve @@ -17,6 +18,7 @@ app = typer.Typer( app.add_typer(auth.app, name="auth") app.add_typer(agent_app, name="agent") +app.add_typer(sealai_app, name="sealai") app.command("commands")(cmd_commands) app.command("serve")(cmd_serve) diff --git a/src/ekb_cli/commands/help.py b/src/ekb_cli/commands/help.py index 4e4cc76..6f373dd 100644 --- a/src/ekb_cli/commands/help.py +++ b/src/ekb_cli/commands/help.py @@ -36,6 +36,11 @@ COMMAND_TREE = { "--port": "监听端口(默认 9528)", "--reload": "开发模式自动重载", }, + "sealai": { + "_help": "SealAI 智能填单/审核平台管理", + "whoami": "查看当前 SealAI 用户身份及租户信息", + "billing": "查看当前企业 SealAI 订阅状态及功能配额", + }, } diff --git a/src/ekb_cli/commands/sealai/__init__.py b/src/ekb_cli/commands/sealai/__init__.py index e69de29..95964f3 100644 --- a/src/ekb_cli/commands/sealai/__init__.py +++ b/src/ekb_cli/commands/sealai/__init__.py @@ -0,0 +1,15 @@ +"""sealai 子命令组 + +对应 CLI 命令: + ekb sealai whoami → 查看当前 SealAI 用户身份及租户信息 + ekb sealai billing → 查看当前企业 SealAI 订阅状态及功能配额 +""" + +import typer +from ekb_cli.commands.sealai.whoami import cmd_whoami +from ekb_cli.commands.sealai.billing import cmd_billing + +app = typer.Typer(help="SealAI 智能填单/审核平台管理") + +app.command("whoami")(cmd_whoami) +app.command("billing")(cmd_billing) diff --git a/src/ekb_cli/commands/sealai/billing.py b/src/ekb_cli/commands/sealai/billing.py new file mode 100644 index 0000000..b3be95b --- /dev/null +++ b/src/ekb_cli/commands/sealai/billing.py @@ -0,0 +1,63 @@ +"""sealai billing:查看 SealAI 订阅状态及功能配额 + +表现层:只负责 rich 格式化输出。 +业务逻辑委托给 services.sealai.billing_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, query_billing_status + +console = Console() + + +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) + + # 订阅信息 + if status.subscriptions: + sub = status.subscriptions[0] + console.print(f"\n[bold]订阅信息[/bold]") + table = Table(show_header=False, box=None, padding=(0, 2)) + table.add_column("字段", style="dim") + table.add_column("值") + table.add_row("订阅 ID", sub.id) + table.add_row("状态", "[green]生效中[/green]" if sub.status == "active" else sub.status) + table.add_row("开始日期", sub.startDate) + console.print(table) + else: + rprint("[yellow]当前企业无有效订阅[/yellow]") + + # 功能配额表 + if not status.features: + return + + console.print(f"\n[bold]功能配额[/bold]") + feat_table = Table(show_header=True, header_style="bold") + feat_table.add_column("功能名称") + feat_table.add_column("code", style="dim") + feat_table.add_column("状态/用量", justify="right") + + for f in status.features: + if f.enabled is not None: + # 开关型 + usage = "[green]已开启[/green]" if f.enabled else "[red]未开启[/red]" + elif f.balance is not None: + # 余额型 + usage = f"余额 {f.balance}/{f.totalQuota}" + elif f.quota is not None: + # 配额型 + usage = f"已用 {f.used}/{f.quota}" + else: + usage = "-" + feat_table.add_row(f.name, f.code, usage) + + console.print(feat_table) + console.print() diff --git a/src/ekb_cli/commands/sealai/whoami.py b/src/ekb_cli/commands/sealai/whoami.py new file mode 100644 index 0000000..4023432 --- /dev/null +++ b/src/ekb_cli/commands/sealai/whoami.py @@ -0,0 +1,49 @@ +"""sealai whoami:查看当前 SealAI 用户身份 + +表现层:只负责 rich 格式化输出。 +业务逻辑委托给 services.sealai.auth_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_bearer_token +from ekb_cli.api.sealai import get_me + +console = Console() + + +def cmd_whoami( + corp_id: str = typer.Option(None, "--corp", "-c", help="指定企业 corpId,默认使用当前企业"), + refresh: bool = typer.Option(False, "--refresh", help="强制刷新 SealAI Bearer token"), +): + """查看当前 SealAI 用户身份及租户信息""" + client = get_sealai_client(corp_id=corp_id, force_refresh=refresh) + me = get_me(client) + + user = me.user + tenant = me.tenant + + # 用户信息 + console.print(f"\n[bold]SealAI 用户[/bold]") + table = Table(show_header=False, box=None, padding=(0, 2)) + table.add_column("字段", style="dim") + table.add_column("值") + table.add_row("姓名", user.name) + table.add_row("用户 ID", user.id) + table.add_row("是否管理员", "[green]是[/green]" if user.isAdmin else "否") + table.add_row("快报 staffId", user.providerUserId) + console.print(table) + + # 租户信息 + console.print(f"\n[bold]租户(企业)[/bold]") + table2 = Table(show_header=False, box=None, padding=(0, 2)) + table2.add_column("字段", style="dim") + table2.add_column("值") + table2.add_row("企业名称", tenant.name) + table2.add_row("租户 Slug", tenant.tenantSlug) + table2.add_row("企业 ID(快报)", tenant.hoseCorpConfig.corporationId) + console.print(table2) + console.print() diff --git a/src/ekb_cli/server/__init__.py b/src/ekb_cli/server/__init__.py index 5252c3a..9f20e61 100644 --- a/src/ekb_cli/server/__init__.py +++ b/src/ekb_cli/server/__init__.py @@ -1,7 +1,7 @@ """FastAPI 应用工厂 create_app() 创建并返回完整配置的 FastAPI 实例: -- 注册 auth / agent / rule 三个路由组 +- 注册 auth / agent / rule / sealai 四个路由组 - 统一 EkbError 异常处理(转 HTTP 状态码 + JSON 响应) """ @@ -28,10 +28,12 @@ def create_app() -> FastAPI: from ekb_cli.server.auth_router import router as auth_router from ekb_cli.server.agent_router import router as agent_router from ekb_cli.server.rule_router import router as rule_router + from ekb_cli.server.sealai_router import router as sealai_router app.include_router(auth_router, prefix="/auth", tags=["auth"]) app.include_router(agent_router, prefix="/agents", tags=["agent"]) app.include_router(rule_router, prefix="/rules", tags=["rule"]) + app.include_router(sealai_router, prefix="/sealai", tags=["sealai"]) # ── 统一异常处理:EkbError → HTTP JSON ────────────────── # code → HTTP status 的映射表 diff --git a/src/ekb_cli/server/sealai_router.py b/src/ekb_cli/server/sealai_router.py new file mode 100644 index 0000000..21e39fe --- /dev/null +++ b/src/ekb_cli/server/sealai_router.py @@ -0,0 +1,45 @@ +"""Server:SealAI 路由 + +对应 CLI 命令: + ekb sealai whoami → GET /sealai/me + ekb sealai billing → GET /sealai/billing + +注意:SealAI 接口使用独立的 SealAiClient(Bearer JWT), + 不使用快报主站的 EkbClient(三 Header 认证)。 +""" + +from fastapi import APIRouter, Query + +from ekb_cli.services.sealai import ( + get_sealai_client, + query_billing_status, +) +from ekb_cli.api.sealai import get_me +from ekb_cli.api.sealai.schemas import MeData, BillingStatusData + +router = APIRouter() + + +@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"), +): + client = get_sealai_client(corp_id=corp_id, force_refresh=refresh) + return get_me(client) + + +@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)