架构整改:收口分层边界、统一异常体系、规范文档约定

【止血】
- 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:
gitea committed 2026-04-15 17:41:02 +08:00
1 parent 1fc6c75f13
commit 19f6c02c2c
16 files changed
+463 -77

No files matched your search

+47
View File
@@ -0,0 +1,47 @@
# server/ — HTTP API Server(表现层)
## 职责边界
- **只做 HTTP 适配**:路由注册、Query/Body 参数绑定、JSON 序列化、HTTP 状态码映射。
- **不做业务编排**:调用 `services.*` 完成,路由函数体保持简短。
- **不定义请求/响应模型**:所有 `BaseModel` 子类统一放在对应的 `schemas.py` 中,
路由文件只做 import,不在文件内 inline 定义。
## 目录结构
```
server/
├── __init__.py # create_app() 工厂;全局 EkbError → HTTP 异常处理器
├── deps.py # FastAPI 依赖注入:ClientDep(EkbClient)
├── auth_router.py # /auth/* 路由
├── agent_router.py # /agents/* 路由
├── rule_router.py # /rules/* 路由
└── sealai_router.py # /sealai/* 路由
```
## 依赖约定
```
server/* → services/* ✅ 允许
server/deps.py → api.get_client ✅ 允许(专属基础设施职责)
server/* → api.*(业务函数) ❌ 禁止(边界穿透)
```
## 请求模型归属规则
| 模型类型 | 存放位置 |
|---|---|
| 快报 API 响应模型 | `api/schemas.py` |
| SealAI API 响应模型 | `api/sealai/schemas.py` |
| Server 请求/响应模型 | 同上,在文件末尾「Server 请求模型」节 |
## 异常处理
`server/__init__.py` 中注册了全局 handler:
- `EkbError` → `code` 字段映射 HTTP 状态码,`message` 字段作为 `detail` 返回
- 路由函数内**不应**局部 catch `EkbError`
## Swagger UI
Server 启动后访问:`http://127.0.0.1:9528/docs`