Files
hose-use/CLAUDE.md
T
gitea 9ec009f2cb 重构第4批:入口注册 serve 命令,更新命令树和项目文档
__init__.py:
- 注册 serve 命令(ekb serve)
- main() 统一捕获 EkbError,rich 红字输出并 SystemExit(1)

commands/serve.py(新建):
- ekb serve --host --port --reload
- 启动 FastAPI + uvicorn

commands/help.py:
- 命令树补 serve 分支

CLAUDE.md:
- 更新架构分层说明(三层:表现 / 业务 / 传输)
- 更新完整项目结构
- 新增 CLI ↔ HTTP Server 接口对照表
- 更新开发环境和工作规范
2026-04-14 11:29:58 +08:00

175 lines
8.2 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`
## 认证机制
快报 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 请求/响应)
│
├── services/ # 业务逻辑层:校验、编排、组合查询
│ ├── __init__.py # 统一 re-export
│ ├── auth_service.py # 企业认证配置管理
│ ├── agent_service.py # Agent CRUD + copy + 高阶组合查询
│ └── rule_service.py # Rule 查询(按编码/ID)+ CRUD
│
├── server/ # HTTP Server(FastAPI)
│ ├── __init__.py # create_app() 工厂 + 全局异常处理
│ ├── deps.py # FastAPI 依赖注入(ClientDep)
│ ├── auth_router.py # /auth/* 路由
│ ├── agent_router.py # /agents/* 路由
│ └── rule_router.py # /rules/* 路由
│
└── 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 枚举常量、关联关系、命令速查
```
> **新增命令**:在 `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)
└── 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 |
> API 文档(Swagger UI)在 Server 启动后访问:`http://127.0.0.1:9528/docs`
## 已知快报 API 路径
```
GET /api/engine/hab/agent/list # Agent 列表
GET /api/engine/hab/agent/detail # Agent 详情(含关联规则)
GET /api/engine/hab/agent/checkName # 检查 Agent 名称唯一性
POST /api/engine/hab/agent/save # 保存 Agent(新建/更新)
GET /api/engine/hab/agent/active # 修改 Agent 状态(state=2 为删除)
GET /api/engine/hab/agent/rule/list # 规则列表(含关联 Agent)
POST /api/engine/hab/agent/rule/save # 新建规则
POST /api/engine/hab/agent/rule/update # 更新规则
```
> 各接口详细的请求/响应结构见 `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 状态码