Files
hose-use/CLAUDE.md
T

240 lines
11 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 状态码