Files
hose-use/CLAUDE.md
T
gitea 19f6c02c2c 架构整改:收口分层边界、统一异常体系、规范文档约定
【止血】
- 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
2026-04-15 17:41:02 +08:00

275 lines
13 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# ekb-cli 项目上下文(CLAUDE.md)
## 项目目标
为快报(ekuaibao)企业费控 SaaS 开发命令行工具,替代图形界面完成智能审核等配置操作。
同时提供 HTTP API Server,供其他服务/AI Agent 以结构化方式调用相同功能。
- 目标站点:`https://app.ekuaibao.com/web/app.html`
### 调试企业列表
| 企业名 | corpId | 备注 |
|--------|--------|------|
| 测试企业 | `ID01ofWoOC1wiX` | 小型测试数据 |
| AI应用演示环境 | `ID01Jdq2yIDnMr` | 大型真实数据,281条规则,多个生产 Agent |
## 认证机制
快报 API 使用自定义 Header 认证(**不是** Cookie),三个字段:`accesstoken`、`corpid`、`staffid`。
获取方式:登录快报 → F12 → 任意 API 请求 → 复制请求头。
认证信息存储在 `~/.config/ekb-cli/config.json`,支持多企业切换。
## 架构分层
```
调用方
├── CLI (typer + rich) # 表现层:参数解析 + 终端格式化输出
│ commands/
└── HTTP Server (FastAPI) # 表现层:路由定义 + JSON 序列化
server/
↓ 共同调用
services/ # 业务逻辑层(厚):校验、编排、组合查询
↓
api/ # 传输层(薄):httpx 封装 + pydantic 校验
↓
快报 SaaS API(ekuaibao.com)
横切关注:
errors.py 统一异常体系(EkbError 及子类)
config.py 认证配置读写
api/schemas.py 数据模型(快报响应模型 + Server 请求/响应模型)
```
**核心原则**:CLI 和 Server 都是薄壳,只做 IO 适配。所有业务逻辑在 `services/` 层完成。
## 项目结构
```
src/ekb_cli/
├── __init__.py # CLI 入口,注册命令组,统一捕获 EkbError
├── config.py # 认证配置读写(~/.config/ekb-cli/config.json)
├── errors.py # 统一异常体系(EkbError / NotFound / Conflict 等)
│
├── api/ # 传输层:对快报 API 的 HTTP 封装
│ ├── __init__.py # EkbClient + get_client() + 统一 re-export
│ ├── agent.py # Agent 相关 API 函数
│ ├── rule.py # Rule 相关 API 函数
│ ├── 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
│ └── 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/* 路由
│ └── 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 <id>
│ ├── create.py # ekb agent create
│ ├── copy.py # ekb agent copy <id>
│ ├── update.py # ekb agent update <id>
│ ├── delete.py # ekb agent delete <id>
│ ├── 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/` 添加业务逻辑 →
> 注册到 `__init__.py` → 在 `server/` 对应路由文件添加端点 →
> 更新 `help.py` 命令树 → 更新本文件命令全图和 CLI↔HTTP 对照表。
## 当前命令全图
```
ekb
├── auth
│ ├── add 添加企业认证
│ ├── list 列出所有企业
│ ├── use 切换当前企业
│ ├── remove 删除企业
│ └── whoami 查看当前企业
├── agent
│ ├── list 列出所有 Agent
│ ├── get 查看 Agent 详情(关联规则列表,--full 不截断)
│ ├── create 新建 Agent
│ ├── copy 复制 Agent(名称自动加「副本」)
│ ├── update 修改 Agent 配置(patch 式,只改传入字段)
│ ├── delete 删除 Agent(有二次确认)
│ ├── rules 列出所有审核规则(--level 筛选,--full 不截断)
│ ├── rule-get 查看规则详情(含关联 Agent,支持编码/ID)
│ ├── rule-create 新建审核规则
│ └── logs 查看审核日志(待实现)
├── serve 启动 HTTP API Server(默认 127.0.0.1:9528)
├── sealai
│ ├── whoami 查看当前 SealAI 用户身份及租户信息
│ └── billing 查看当前企业 SealAI 订阅状态及功能配额
└── commands 显示命令树(可带组名)
```
## CLI ↔ HTTP Server 接口对照
| CLI 命令 | HTTP 端点 | 方法 |
|---|---|---|
| `ekb auth list` | `GET /auth/corps` | GET |
| `ekb auth add` | `POST /auth/corps` | POST |
| `ekb auth use <id>` | `PUT /auth/corps/{corp_id}/use` | PUT |
| `ekb auth remove <id>` | `DELETE /auth/corps/{corp_id}` | DELETE |
| `ekb auth whoami` | `GET /auth/whoami` | GET |
| `ekb agent list` | `GET /agents` | GET |
| `ekb agent get <id>` | `GET /agents/{agent_id}` | GET |
| `ekb agent create` | `POST /agents` | POST |
| `ekb agent copy <id>` | `POST /agents/{agent_id}/copy` | POST |
| `ekb agent update <id>` | `PATCH /agents/{agent_id}` | PATCH |
| `ekb agent delete <id>` | `DELETE /agents/{agent_id}` | DELETE |
| `ekb agent rules` | `GET /rules` | GET |
| `ekb agent rule-get <ref>` | `GET /rules/{rule_ref}` | GET |
| `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`
## SealAI 子平台
SealAI 是独立于快报主站的 Next.js 平台(`{corpId.lower()}.sealai.cc`),认证体系完全独立。
> 认证机制、API 路径、功能说明详见 `commands/sealai/README.sealai.md`
## 已知快报 API 路径
> 所有路径前缀为 `/api`,后端框架:Spring Boot(com.hosecloud.habserver)。
> `client.get(path)` 会自动补 `/api` 前缀,路径写 `/engine/...` 即可。
```
# Agent
GET /api/engine/hab/agent/list # Agent 列表(page, size)
GET /api/engine/hab/agent/detail?id= # Agent 详情(含关联规则)⚠️ 参数是 id 非 agentId
GET /api/engine/hab/agent/checkName?name= # 检查 Agent 名称唯一性,data=false 表示名称已存在
POST /api/engine/hab/agent/save # 保存 Agent(新建=无id,更新=有id)
GET /api/engine/hab/agent/active?id=&state= # 修改 Agent 状态(state: 0=草稿 1=发布 2=删除)
# Rule(审核规则)
GET /api/engine/hab/agent/rule/list # 规则列表(含关联 Agent 摘要)(page, size)
GET /api/engine/hab/agent/rule/detail?id= # 规则详情(单条,比 list 多 staffId/staffName)
POST /api/engine/hab/agent/rule/save # 新建规则
POST /api/engine/hab/agent/rule/update # 更新规则
POST /api/engine/hab/agent/rule/delete # 删除规则(POST,非 GET)
```
### rule/detail 返回字段
```json
{
"id": "...", "corporationId": "...", "code": "#0001",
"version": 1, "purpose": "...", "description": "...", "level": "严重",
"createTime": "...", "updateTime": "...", "active": true,
"staffId": "corpId:staffId", "staffName": "..."
}
```
### 已排除(404)路径
以下路径确认不存在,不要重复尝试:
```
/engine/hab/agent/log/list /engine/hab/agent/audit/list
/engine/hab/review/list /engine/hab/audit/list
/engine/hab/freeflow/list /engine/hab/flow/list
/engine/hab/agent/terms /engine/hab/agent/copy
/engine/hab/agent/clone /engine/hab/agent/publish
/engine/hab/stats /engine/hab/config
/v1/staffs /v1/departments
/v1/corporations
```
> 各接口详细的请求/响应结构见 `api/agent.py` / `api/rule.py`;
> 枚举常量和字段语义见 `commands/agent/README.agent.md` 和 `README.rule.md`。
## 开发环境
- Python 3.12,uv 管理
- 依赖:typer(CLI)、httpx[socks](HTTP)、rich(终端输出)、
pydantic(模型校验)、questionary(交互式菜单)、
fastapi + uvicorn[standard](HTTP Server)
- 运行 CLI:`uv run ekb <command>`
- 运行 Server:`uv run ekb serve`(或 `uv run ekb serve --reload` 开发模式)
## 工作规范
- 每完成一个功能模块就 commit 一次,commit message 用中文
- 新增命令时同步更新:`services/` 业务逻辑 → `commands/` CLI 薄壳 →
`server/` 对应路由 → `help.py` 命令树 → 本文件命令全图和接口对照表
- 每个文件顶部保持 docstring 说明用途和分层职责
- 枚举常量、字段语义写在命令组 README,不写在本文件
- `services/` 层只抛出 `EkbError` 子类,不关心如何渲染;
CLI 层用 rich 红字,Server 层转 HTTP 状态码
## 文档规范
每个源文件和目录都应该有配套的说明文档,命名规则如下:
### 文件级文档(`{module}.readme.md`)
- 每个 `demo.py` 对应一份 `demo.readme.md`,记录:
- 枚举常量的取值含义(不要散落在代码注释里)
- 模式选择的决策依据(为什么这么设计)
- 对外暴露的函数/类的使用约定
- 不要把这些内容写进 CLAUDE.md,保持各自归属
### 测试文件(`{module}.test.py`,可选)
- 每个 `demo.py` 可以有 `demo.test.py` 作为配套测试
- 测试文件与被测模块放在同一目录
- 最外层独立模块(`config.py`、`errors.py`)的测试**暂时可以忽略**
### 目录级文档(`README.{dirname}.md`)
- 每个含 `__init__.py` 的目录应有目录级说明文档
- 命名约定:`README.{dirname}.md`(如 `README.services.md`、`README.server.md`)
- 内容包括:该层的职责边界、组织方式、模块间的调用约定
### 分层导入约束(硬规则)
以下导入关系**严格禁止**,违反则视为边界穿透:
| 禁止的方向 | 说明 |
|---|---|
| `commands/*` → `api/*` | CLI 表现层不得直接调用传输层 |
| `server/*` → `api/*`(业务函数) | Server 路由不得直接调用 API 域函数(`deps.py` 中的 `get_client` 除外) |
正确路径:`commands/*` → `services/*` → `api/*`