diff --git a/CLAUDE.md b/CLAUDE.md index 9b5ffee..ff682cc 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -9,61 +9,48 @@ - 当前页面:`#/ai-agents`(智能伙伴 / 智能审核) ## 认证机制 -快报 API 使用自定义 Header 认证(**不是** Cookie),三个字段必须同时携带: +快报 API 使用自定义 Header 认证(**不是** Cookie),三个字段:`accesstoken`、`corpid`、`staffid`。 -| Header | 示例值 | 说明 | -|---|---|---| -| `accesstoken` | `rbvOhATmi9zoXbWDuDwcQw` | 登录 token | -| `corpid` | `ID01ofWoOC1wiX` | 企业 ID | -| `staffid` | `ID01ofWoOC1wiX:ID01KsDgHaH5nN` | 员工 ID(格式:corpId:staffId) | - -**获取方式**:登录快报 → F12 → 任意 API 请求 → 复制请求头中这三个字段。 +**获取方式**:登录快报 → F12 → 任意 API 请求 → 复制请求头。 认证信息存储在 `~/.config/ekb-cli/config.json`,支持多企业切换。 ## 项目结构 ``` src/ekb_cli/ -├── __init__.py # 主入口,注册子命令 -├── config.py # 多企业配置存储(~/.config/ekb-cli/config.json) -├── api.py # EkbClient HTTP 基类,自动附带认证 Header -└── commands/ - ├── auth.py # ekb auth add/list/use/remove/whoami - └── agent.py # ekb agent list(智能审核 Agent) +├── api/ # API 调用封装,每个业务域一个文件 +└── commands/ # CLI 命令,按业务域分子目录 + ├── auth.py # ekb auth + └── agent/ # ekb agent(list / get / update / logs) ``` -## 已知 API +> 新增命令:在 commands/ 对应目录添加文件,注册到 __init__.py;同步在 api/ 添加封装函数。 -### 智能审核 Agent +## 枚举常量(Agent) +- **mode**:`0`=全自动,`1`=半自动(有 samplingRate 0-100),`2`=辅助 +- **state**:`0`=启用,`1`=禁用 +- **rule.level**:`"严重"` / `"一般"` + +## 已知 API 路径 ``` -GET /api/engine/hab/agent/list?page=1&size=1000&corpId=... -``` -响应结构: -```json -{ - "code": 200, - "data": { - "items": [ - { - "id": "1978711116974841858", - "name": "华北-西瓜智能体", - "state": 0, // 0=启用, 1=禁用 - "mode": 2, // 2=自动 - "description": "", - "agreeTerms": "...", - "rejectTerms": "...", - "exemptTerms": "...", - "reviewTerms": "...", - "agentFlowRelations": [], - "createTime": "2025-10-16 14:34:28", - "updateTime": "2025-10-16 14:34:33" - } - ], - "totalItems": 1 - } -} +GET/POST /api/engine/hab/agent/list +GET/POST /api/engine/hab/agent/detail +POST /api/engine/hab/agent/save +GET /api/engine/hab/agent/rule/list +POST /api/engine/hab/agent/rule/save ``` +> 详细请求/响应结构参考 api/agent.py 和 Chrome DevTools 网络请求记录。 + +## 开发环境 +- Python 3.12,uv 管理;依赖:typer、httpx、rich +- 运行:`uv run ekb ` + +## 工作规范 +- 每完成一个功能模块就 commit 一次 +- 新增命令/API 同步更新本文件的「已知 API 路径」部分 + + ## 开发环境 - Python 3.12,uv 管理 - 依赖:typer(CLI)、httpx(HTTP)、rich(终端输出) diff --git a/README.md b/README.md index bc01a78..a343ee0 100644 --- a/README.md +++ b/README.md @@ -41,11 +41,13 @@ ekb auth remove # 删除企业 ## 命令总览 -| 命令 | 说明 | +```bash +ekb --help # 查看所有命令组 +ekb auth --help # 企业认证相关命令 +ekb agent --help # 智能审核 Agent 相关命令 +``` + +| 命令组 | 说明 | |---|---| -| `ekb auth add` | 添加企业认证 | -| `ekb auth list` | 列出所有企业 | -| `ekb auth use ` | 切换当前企业 | -| `ekb auth remove ` | 删除企业 | -| `ekb auth whoami` | 查看当前企业 | -| `ekb agent list` | 列出所有智能审核 Agent | +| `ekb auth` | 管理企业认证 | +| `ekb agent` | 智能审核 Agent 管理 | diff --git a/src/ekb_cli/__init__.py b/src/ekb_cli/__init__.py index 95988a7..ce2aba1 100644 --- a/src/ekb_cli/__init__.py +++ b/src/ekb_cli/__init__.py @@ -3,6 +3,7 @@ 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 app = typer.Typer( name="ekb", @@ -12,6 +13,7 @@ app = typer.Typer( app.add_typer(auth.app, name="auth") app.add_typer(agent_app, name="agent") +app.command("commands")(cmd_commands) def main(): diff --git a/src/ekb_cli/api/agent.py b/src/ekb_cli/api/agent.py index b3f664c..fa6ce67 100644 --- a/src/ekb_cli/api/agent.py +++ b/src/ekb_cli/api/agent.py @@ -9,3 +9,7 @@ def list_agents(client: EkbClient, page: int = 1, size: int = 1000) -> dict: def get_agent(client: EkbClient, agent_id: str) -> dict: return client.get("/engine/hab/agent/detail", id=agent_id) + + +def save_agent(client: EkbClient, payload: dict) -> dict: + return client.post("/engine/hab/agent/save", payload) diff --git a/src/ekb_cli/commands/agent/__init__.py b/src/ekb_cli/commands/agent/__init__.py index f8eb760..4f76133 100644 --- a/src/ekb_cli/commands/agent/__init__.py +++ b/src/ekb_cli/commands/agent/__init__.py @@ -4,9 +4,11 @@ import typer from ekb_cli.commands.agent.list import cmd_list from ekb_cli.commands.agent.detail import cmd_detail from ekb_cli.commands.agent.logs import cmd_logs +from ekb_cli.commands.agent.update import cmd_update app = typer.Typer(help="智能审核 Agent 管理") app.command("list")(cmd_list) app.command("get")(cmd_detail) +app.command("update")(cmd_update) app.command("logs")(cmd_logs) diff --git a/src/ekb_cli/commands/agent/update.py b/src/ekb_cli/commands/agent/update.py new file mode 100644 index 0000000..6f9a6e1 --- /dev/null +++ b/src/ekb_cli/commands/agent/update.py @@ -0,0 +1,81 @@ +"""ekb agent update :修改 Agent 配置""" + +from typing import Optional +import typer +from rich import print as rprint + +from ekb_cli.api import get_client +from ekb_cli.api.agent import get_agent, save_agent + +# mode 映射:CLI 友好名称 → 接口值 +MODE_NAMES = {"auto": 0, "semi": 1, "assist": 2} +MODE_DISPLAY = {0: "全自动", 1: "半自动", 2: "辅助"} +STATE_DISPLAY = {0: "启用", 1: "禁用"} + + +def cmd_update( + agent_id: str = typer.Argument(..., help="Agent ID"), + name: Optional[str] = typer.Option(None, "--name", "-n", help="名称"), + description: Optional[str] = typer.Option(None, "--description", "-d", help="描述"), + mode: Optional[str] = typer.Option(None, "--mode", "-m", help="模式:auto/semi/assist"), + sampling_rate: Optional[int] = typer.Option(None, "--sampling-rate", "-r", help="半自动抽检比例 0-100"), + state: Optional[int] = typer.Option(None, "--state", "-s", help="状态:0=启用 1=禁用"), + agree_terms: Optional[str] = typer.Option(None, "--agree-terms", help="同意条款"), + reject_terms: Optional[str] = typer.Option(None, "--reject-terms", help="拒绝条款"), + exempt_terms: Optional[str] = typer.Option(None, "--exempt-terms", help="豁免条款"), + review_terms: Optional[str] = typer.Option(None, "--review-terms", help="转人工条款"), +): + """修改 Agent 配置(只传需要修改的字段)""" + # 验证 mode 参数 + mode_val = None + if mode is not None: + if mode not in MODE_NAMES: + rprint(f"[red]mode 必须是 auto / semi / assist,当前:{mode}[/red]") + raise typer.Exit(1) + mode_val = MODE_NAMES[mode] + + if sampling_rate is not None and not (0 <= sampling_rate <= 100): + rprint("[red]sampling-rate 范围是 0-100[/red]") + raise typer.Exit(1) + + with get_client() as client: + resp = get_agent(client, agent_id) + d = resp.get("data", {}) + if not d: + rprint("[red]未找到该 Agent[/red]") + raise typer.Exit(1) + + # 基于现有数据构建 payload,仅覆盖传入的字段 + payload: dict = { + "id": d["id"], + "icon": d.get("icon", ""), + "name": name if name is not None else d.get("name", ""), + "description": description if description is not None else d.get("description", ""), + "mode": mode_val if mode_val is not None else d.get("mode", 0), + "agreeTerms": agree_terms if agree_terms is not None else d.get("agreeTerms", ""), + "rejectTerms": reject_terms if reject_terms is not None else d.get("rejectTerms", ""), + "exemptTerms": exempt_terms if exempt_terms is not None else d.get("exemptTerms", ""), + "reviewTerms": review_terms if review_terms is not None else d.get("reviewTerms", ""), + "state": state if state is not None else d.get("state", 0), + "ruleIds": [r["id"] for r in d.get("agentRule", [])], + } + + # 半自动才需要 samplingRate + effective_mode = payload["mode"] + if effective_mode == 1: + payload["samplingRate"] = ( + sampling_rate if sampling_rate is not None else d.get("samplingRate", 0) + ) + + save_resp = save_agent(client, payload) + + if save_resp.get("code") != 200: + rprint(f"[red]保存失败:{save_resp.get('msg', '未知错误')}[/red]") + raise typer.Exit(1) + + saved = save_resp.get("data", {}) + rprint( + f"[green]✓[/green] 已更新 Agent [bold]{saved.get('name')}[/bold]" + f" 模式={MODE_DISPLAY.get(saved.get('mode'), str(saved.get('mode')))}" + f" 状态={STATE_DISPLAY.get(saved.get('state'), str(saved.get('state')))}" + ) diff --git a/src/ekb_cli/commands/help.py b/src/ekb_cli/commands/help.py new file mode 100644 index 0000000..4182fdc --- /dev/null +++ b/src/ekb_cli/commands/help.py @@ -0,0 +1,61 @@ +"""ekb commands [GROUP]:查看命令树""" + +from typing import Optional +import typer +from rich.console import Console +from rich.tree import Tree + +console = Console() + +# 命令树定义:新增命令组/命令时在此同步维护 +COMMAND_TREE = { + "auth": { + "_help": "管理企业认证", + "add": "添加企业认证", + "list": "列出所有企业", + "use": "切换当前企业", + "remove": "删除企业", + "whoami": "查看当前企业", + }, + "agent": { + "_help": "智能审核 Agent 管理", + "list": "列出所有 Agent", + "get": "查看 Agent 详情 → ekb agent get --help", + "update": "修改 Agent 配置 → ekb agent update --help", + "logs": "查看审核日志", + }, +} + + +def cmd_commands(group: Optional[str] = typer.Argument(None, help="命令组名称,如 auth / agent")): + """查看命令树(不带参数显示全树,带组名只看该分支)""" + if group is not None: + if group not in COMMAND_TREE: + console.print(f"[red]未知命令组:{group}[/red]") + raise typer.Exit(1) + _print_group(group, COMMAND_TREE[group]) + else: + root = Tree("[bold]ekb[/bold]") + for grp, cmds in COMMAND_TREE.items(): + _build_branch(root, grp, cmds) + console.print(root) + console.print("\n[dim]查看某组详情:ekb commands [/dim]") + console.print("[dim]查看命令参数:ekb --help[/dim]") + + +def _build_branch(parent, grp: str, cmds: dict): + branch = parent.add(f"[bold cyan]{grp}[/bold cyan] {cmds.get('_help', '')}") + for cmd, desc in cmds.items(): + if cmd == "_help": + continue + branch.add(f"[green]{cmd}[/green] {desc}") + + +def _print_group(grp: str, cmds: dict): + tree = Tree(f"[bold]ekb[/bold] [bold cyan]{grp}[/bold cyan] {cmds.get('_help', '')}") + for cmd, desc in cmds.items(): + if cmd == "_help": + continue + tree.add(f"[green]{cmd}[/green] {desc}") + console.print(tree) + console.print(f"\n[dim]查看命令参数:ekb {grp} --help[/dim]")