架构整改:收口分层边界、统一异常体系、规范文档约定
【止血】
- errors.py: NotLoggedInError.__init__ 增加可选 message 参数,修复
services/sealai/auth_service.py 中 raise NotLoggedInError(f"...") 导致
TypeError 的硬错误
【边界收口】
- services/sealai/auth_service.py: 新增 get_whoami(),封装 api.sealai.get_me,
消灭 commands/server 对 api 层的直接穿透
- commands/sealai/whoami.py: 改用 services.sealai.get_whoami(),去掉 api 层导入
- server/sealai_router.py /me: 改用 services.sealai.get_whoami(),去掉 api 层导入
- services/agent_service.py: 新增 list_agents_cmd 等 _cmd 系列便捷函数,
commands 层无需再 import get_client
- commands/agent/list.py: 改用 list_agents_cmd(),去掉对 api.get_client 的直接依赖
【Schema 归属】
- api/sealai/schemas.py: 新增「Server 请求模型」节,迁入 sealai_router.py 中
原有 6 个内联 BaseModel(RuleCreateRequest 等)
- server/sealai_router.py: 去掉所有内联 BaseModel 和 pydantic 导入,
改从 api/sealai/schemas 统一导入
【导出统一】
- services/__init__.py: 纳入 SealAI 子域的全部 service 函数,
消灭两套并行导出体系;补充模块 docstring
【工程纪律】
- commands/auth.py: 去掉 auth_use / auth_whoami 中的局部异常捕获,
统一由 CLI 入口全局 handler 处理
【文档规范】
- CLAUDE.md: 新增「文档规范」节,明确 {module}.readme.md / {module}.test.py /
README.{dirname}.md 命名约定及分层导入禁止规则
- 新建 README.commands.md / README.server.md / README.services.md /
services/sealai/README.sealai.md / api/sealai/README.sealai.md
This commit is contained in:
1 parent
1fc6c75f13
commit
19f6c02c2c
16 files changed
+463
-77
No files matched your search
@@ -0,0 +1,35 @@
|
||||
# commands/ — CLI 表现层
|
||||
|
||||
## 职责边界
|
||||
|
||||
- **只做 I/O 适配**:参数解析(typer)、交互式输入、rich 格式化输出。
|
||||
- **不做业务编排**:调用 `services.*` 完成,不直接调用 `api.*`。
|
||||
- **不做异常处理**:让 `EkbError` 自然冒泡至 CLI 入口(`__init__.py`)的全局 handler;
|
||||
命令内部**不应**局部 catch 再重新打印,以免造成双重输出或消息漂移。
|
||||
|
||||
## 目录结构
|
||||
|
||||
```
|
||||
commands/
|
||||
├── auth.py # ekb auth 命令组(企业认证管理)
|
||||
├── help.py # ekb commands(命令树速查)
|
||||
├── serve.py # ekb serve(启动 HTTP API Server)
|
||||
├── agent/ # ekb agent 命令组,见 README.agent.md / README.rule.md
|
||||
└── sealai/ # ekb sealai 命令组,见 README.sealai.md
|
||||
```
|
||||
|
||||
## 依赖约定
|
||||
|
||||
```
|
||||
commands/* → services/* ✅ 允许
|
||||
commands/* → api/* ❌ 禁止(边界穿透)
|
||||
```
|
||||
|
||||
## 新增命令流程
|
||||
|
||||
1. 在对应子目录新建 `{verb}.py`,只写展示逻辑
|
||||
2. 在 `{dir}/__init__.py` 注册到 typer app
|
||||
3. 在 `services/` 添加或复用业务函数
|
||||
4. 在 `server/` 对应 router 添加 HTTP 端点
|
||||
5. 更新 `help.py` 命令树
|
||||
6. 更新 `CLAUDE.md` 命令全图和接口对照表
|
||||
@@ -5,8 +5,7 @@ from rich.console import Console
|
||||
from rich.table import Table
|
||||
from rich import print as rprint
|
||||
|
||||
from ekb_cli.api import get_client
|
||||
from ekb_cli.services import list_agents
|
||||
from ekb_cli.services import list_agents_cmd
|
||||
|
||||
console = Console()
|
||||
STATE_MAP = {0: "[dim]草稿[/dim]", 1: "[green]已发布[/green]"}
|
||||
@@ -15,8 +14,7 @@ MODE_MAP = {0: "全自动", 1: "半自动", 2: "辅助"}
|
||||
|
||||
def cmd_list():
|
||||
"""列出所有智能审核 Agent"""
|
||||
with get_client() as client:
|
||||
page = list_agents(client)
|
||||
page = list_agents_cmd()
|
||||
|
||||
if not page.items:
|
||||
rprint("[yellow]暂无 Agent[/yellow]")
|
||||
|
||||
@@ -2,6 +2,10 @@
|
||||
|
||||
表现层:只负责参数解析、交互式输入、rich 格式化输出。
|
||||
业务逻辑委托给 services.auth_service。
|
||||
|
||||
异常处理约定:
|
||||
所有命令不做局部捕获,让 EkbError 冒泡到 CLI 入口的全局 handler,
|
||||
由 __init__.py 统一以红字输出并 Exit(1)。
|
||||
"""
|
||||
|
||||
import typer
|
||||
@@ -62,13 +66,8 @@ def auth_list():
|
||||
@app.command("use")
|
||||
def auth_use(corp_id: str = typer.Argument(..., help="要切换的企业 corpId")):
|
||||
"""切换当前使用的企业"""
|
||||
from ekb_cli.errors import EkbError
|
||||
try:
|
||||
corp = auth_service.use_corp(corp_id)
|
||||
rprint(f"[green]✓[/green] 已切换到企业 [bold]{corp.name}[/bold] ({corp.corp_id})")
|
||||
except EkbError as e:
|
||||
rprint(f"[red]{e.message}[/red]")
|
||||
raise typer.Exit(1)
|
||||
corp = auth_service.use_corp(corp_id)
|
||||
rprint(f"[green]✓[/green] 已切换到企业 [bold]{corp.name}[/bold] ({corp.corp_id})")
|
||||
|
||||
|
||||
@app.command("remove")
|
||||
@@ -81,10 +80,5 @@ def auth_remove(corp_id: str = typer.Argument(..., help="要删除的企业 corp
|
||||
@app.command("whoami")
|
||||
def auth_whoami():
|
||||
"""查看当前使用的企业"""
|
||||
from ekb_cli.errors import NotLoggedInError
|
||||
try:
|
||||
corp = auth_service.whoami()
|
||||
rprint(f"当前企业:[bold]{corp.name}[/bold] ({corp.corp_id})")
|
||||
except NotLoggedInError:
|
||||
rprint("[yellow]未登录,请先执行 `ekb auth add`[/yellow]")
|
||||
raise typer.Exit(1)
|
||||
corp = auth_service.whoami()
|
||||
rprint(f"当前企业:[bold]{corp.name}[/bold] ({corp.corp_id})")
|
||||
@@ -1,16 +1,15 @@
|
||||
"""sealai whoami:查看当前 SealAI 用户身份
|
||||
|
||||
表现层:只负责 rich 格式化输出。
|
||||
业务逻辑委托给 services.sealai.auth_service。
|
||||
业务逻辑(含 get_me 调用)完全委托给 services.sealai.get_whoami,
|
||||
不直接导入 api 层任何函数。
|
||||
"""
|
||||
|
||||
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
|
||||
from ekb_cli.services.sealai import get_whoami
|
||||
|
||||
console = Console()
|
||||
|
||||
@@ -20,8 +19,7 @@ def cmd_whoami(
|
||||
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)
|
||||
me = get_whoami(corp_id=corp_id, force_refresh=refresh)
|
||||
|
||||
user = me.user
|
||||
tenant = me.tenant
|
||||
|
||||
Reference in new issue
Block a user