重构第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 接口对照表 - 更新开发环境和工作规范
This commit is contained in:
1 parent
db378b9140
commit
9ec009f2cb
4 files changed
+171
-26
No files matched your search
@@ -3,6 +3,7 @@
|
||||
## 项目目标
|
||||
|
||||
为快报(ekuaibao)企业费控 SaaS 开发命令行工具,替代图形界面完成智能审核等配置操作。
|
||||
同时提供 HTTP API Server,供其他服务/AI Agent 以结构化方式调用相同功能。
|
||||
|
||||
- 目标站点:`https://app.ekuaibao.com/web/app.html`
|
||||
- 当前调试企业 corpId:`ID01ofWoOC1wiX`
|
||||
@@ -14,29 +15,79 @@
|
||||
获取方式:登录快报 → 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/
|
||||
├── api/
|
||||
│ └── agent.py # agent / rule 相关 API 封装
|
||||
├── commands/
|
||||
│ ├── auth.py # ekb auth(add / list / use / remove / whoami)
|
||||
│ ├── help.py # ekb commands(命令树)
|
||||
│ └── agent/ # ekb agent 命令组
|
||||
│ ├── __init__.py
|
||||
│ ├── list.py # ekb agent list
|
||||
│ ├── detail.py # ekb agent get <id>
|
||||
│ ├── update.py # ekb agent update <id>
|
||||
│ ├── logs.py # ekb agent logs(stub)
|
||||
│ ├── rule_list.py # ekb agent rules
|
||||
│ ├── rule_detail.py # ekb agent rule-get <编码或ID>
|
||||
│ ├── README.agent.md # agent 枚举常量、条款语义、命令速查
|
||||
│ └── README.rule.md # rule 枚举常量、关联关系、命令速查
|
||||
└── __init__.py # 顶层入口,注册所有命令组
|
||||
├── __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/` 对应目录添加文件 → 注册到 `__init__.py` → 在 `api/` 添加封装函数 → 更新 `help.py` 命令树 → 更新本文件 API 路径。
|
||||
> **新增命令**:在 `commands/` 对应目录添加文件 → 在 `services/` 添加业务逻辑 →
|
||||
> 注册到 `__init__.py` → 在 `server/` 对应路由文件添加端点 →
|
||||
> 更新 `help.py` 命令树 → 更新本文件命令全图和 CLI↔HTTP 对照表。
|
||||
|
||||
## 当前命令全图
|
||||
|
||||
@@ -51,36 +102,73 @@ ekb
|
||||
├── 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 显示命令树(可带组名)
|
||||
```
|
||||
|
||||
## 已知 API 路径
|
||||
## 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`;枚举常量和字段语义见各命令组的 README。
|
||||
> 各接口详细的请求/响应结构见 `api/agent.py` / `api/rule.py`;
|
||||
> 枚举常量和字段语义见 `commands/agent/README.agent.md` 和 `README.rule.md`。
|
||||
|
||||
## 开发环境
|
||||
|
||||
- Python 3.12,uv 管理
|
||||
- 依赖:typer(CLI)、httpx + httpx[socks](HTTP)、rich(终端输出)
|
||||
- 运行:`uv run ekb <command>`
|
||||
- 依赖: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 用中文
|
||||
- 新增命令或 API 时,同步更新:本文件命令全图 + API 路径、`help.py` 命令树、对应 README
|
||||
- 每个命令文件顶部保持 docstring 说明用途
|
||||
- 新增命令时同步更新:`services/` 业务逻辑 → `commands/` CLI 薄壳 →
|
||||
`server/` 对应路由 → `help.py` 命令树 → 本文件命令全图和接口对照表
|
||||
- 每个文件顶部保持 docstring 说明用途和分层职责
|
||||
- 枚举常量、字段语义写在命令组 README,不写在本文件
|
||||
- `services/` 层只抛出 `EkbError` 子类,不关心如何渲染;
|
||||
CLI 层用 rich 红字,Server 层转 HTTP 状态码
|
||||
+15
-2
@@ -1,9 +1,13 @@
|
||||
"""ekb-cli:快报命令行工具"""
|
||||
"""ekb-cli:快报命令行工具
|
||||
|
||||
入口:注册所有命令组,统一捕获 EkbError 异常。
|
||||
"""
|
||||
|
||||
import typer
|
||||
from ekb_cli.commands import auth
|
||||
from ekb_cli.commands.agent import app as agent_app
|
||||
from ekb_cli.commands.help import cmd_commands
|
||||
from ekb_cli.commands.serve import cmd_serve
|
||||
|
||||
app = typer.Typer(
|
||||
name="ekb",
|
||||
@@ -14,7 +18,16 @@ app = typer.Typer(
|
||||
app.add_typer(auth.app, name="auth")
|
||||
app.add_typer(agent_app, name="agent")
|
||||
app.command("commands")(cmd_commands)
|
||||
app.command("serve")(cmd_serve)
|
||||
|
||||
|
||||
def main():
|
||||
app()
|
||||
"""CLI 入口:统一捕获 EkbError,输出友好错误信息"""
|
||||
from ekb_cli.errors import EkbError
|
||||
from rich import print as rprint
|
||||
|
||||
try:
|
||||
app()
|
||||
except EkbError as e:
|
||||
rprint(f"[red]{e.message}[/red]")
|
||||
raise SystemExit(1)
|
||||
@@ -30,10 +30,16 @@ COMMAND_TREE = {
|
||||
"rule-create": "新建审核规则 → ekb agent rule-create --help",
|
||||
"logs": "查看审核日志",
|
||||
},
|
||||
"serve": {
|
||||
"_help": "启动 HTTP API Server",
|
||||
"--host": "监听地址(默认 127.0.0.1)",
|
||||
"--port": "监听端口(默认 9528)",
|
||||
"--reload": "开发模式自动重载",
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def cmd_commands(group: Optional[str] = typer.Argument(None, help="命令组名称,如 auth / agent")):
|
||||
def cmd_commands(group: Optional[str] = typer.Argument(None, help="命令组名称,如 auth / agent / serve")):
|
||||
"""查看命令树(不带参数显示全树,带组名只看该分支)"""
|
||||
if group is not None:
|
||||
if group not in COMMAND_TREE:
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
"""ekb serve:启动 HTTP API Server
|
||||
|
||||
将 ekb-cli 的功能以 HTTP API 形式暴露,
|
||||
供其他服务、AI Agent 或脚本调用,返回结构化 JSON 数据。
|
||||
"""
|
||||
|
||||
import typer
|
||||
from rich import print as rprint
|
||||
|
||||
|
||||
def cmd_serve(
|
||||
host: str = typer.Option("127.0.0.1", "--host", "-h", help="监听地址"),
|
||||
port: int = typer.Option(9528, "--port", "-p", help="监听端口"),
|
||||
reload: bool = typer.Option(False, "--reload", help="开发模式:文件变更时自动重载"),
|
||||
):
|
||||
"""启动 HTTP API Server(FastAPI + uvicorn)
|
||||
|
||||
启动后可访问:
|
||||
\b
|
||||
- API 文档:http://<host>:<port>/docs
|
||||
- 所有接口与 CLI 命令一一对应,返回结构化 JSON
|
||||
"""
|
||||
try:
|
||||
import uvicorn
|
||||
except ImportError:
|
||||
rprint("[red]缺少 uvicorn,请执行:uv add uvicorn[standard][/red]")
|
||||
raise typer.Exit(1)
|
||||
|
||||
from ekb_cli.server import create_app
|
||||
|
||||
rprint(f"[bold green]ekb-cli API Server[/bold green]")
|
||||
rprint(f" 地址:http://{host}:{port}")
|
||||
rprint(f" 文档:[link=http://{host}:{port}/docs]http://{host}:{port}/docs[/link]")
|
||||
rprint(f" 模式:{'[yellow]开发(--reload)[/yellow]' if reload else '生产'}")
|
||||
rprint("")
|
||||
|
||||
app = create_app()
|
||||
uvicorn.run(app, host=host, port=port, reload=reload)
|
||||
Reference in new issue
Block a user