diff --git a/CLAUDE.md b/CLAUDE.md new file mode 100644 index 0000000..9b5ffee --- /dev/null +++ b/CLAUDE.md @@ -0,0 +1,75 @@ +# ekb-cli 项目上下文(CLAUDE.md) + +## 项目目标 +为快报(ekuaibao)企业费控 SaaS 开发命令行工具,替代图形界面完成智能审核等配置操作。 + +## 目标网站 +- 地址:`https://app.ekuaibao.com/web/app.html` +- 当前调试企业 corpId:`ID01ofWoOC1wiX` +- 当前页面:`#/ai-agents`(智能伙伴 / 智能审核) + +## 认证机制 +快报 API 使用自定义 Header 认证(**不是** Cookie),三个字段必须同时携带: + +| Header | 示例值 | 说明 | +|---|---|---| +| `accesstoken` | `rbvOhATmi9zoXbWDuDwcQw` | 登录 token | +| `corpid` | `ID01ofWoOC1wiX` | 企业 ID | +| `staffid` | `ID01ofWoOC1wiX:ID01KsDgHaH5nN` | 员工 ID(格式:corpId:staffId) | + +**获取方式**:登录快报 → 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 + +### 智能审核 Agent +``` +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 + } +} +``` + +## 开发环境 +- Python 3.12,uv 管理 +- 依赖:typer(CLI)、httpx(HTTP)、rich(终端输出) +- 运行:`uv run ekb ` + +## 工作规范 +- 每完成一个功能模块就 commit 一次,不攒着提交 +- 每新增命令或 API,同步更新本文件的「已知 API」部分 +- 每个命令文件顶部保持 docstring 说明用途 diff --git a/README.md b/README.md new file mode 100644 index 0000000..bc01a78 --- /dev/null +++ b/README.md @@ -0,0 +1,51 @@ +# ekb-cli + +快报(ekuaibao)命令行工具,用于替代图形界面完成智能审核等配置操作。 + +## 安装 + +```bash +uv pip install -e . +``` + +## 快速开始 + +### 1. 添加企业认证 + +登录快报 → F12 → 任意 API 请求 → 复制请求头中的三个字段: + +```bash +uv run ekb auth add +# 按提示输入 accesstoken、corpid、staffid、企业名称 +``` + +### 2. 查看当前企业 + +```bash +uv run ekb auth whoami +``` + +### 3. 列出智能审核 Agent + +```bash +uv run ekb agent list +``` + +## 多企业切换 + +```bash +ekb auth list # 查看所有企业 +ekb auth use # 切换企业 +ekb auth remove # 删除企业 +``` + +## 命令总览 + +| 命令 | 说明 | +|---|---| +| `ekb auth add` | 添加企业认证 | +| `ekb auth list` | 列出所有企业 | +| `ekb auth use ` | 切换当前企业 | +| `ekb auth remove ` | 删除企业 | +| `ekb auth whoami` | 查看当前企业 | +| `ekb agent list` | 列出所有智能审核 Agent | diff --git a/src/ekb_cli/api.py b/src/ekb_cli/api.py index 7799df3..b7d3edb 100644 --- a/src/ekb_cli/api.py +++ b/src/ekb_cli/api.py @@ -1,4 +1,4 @@ -"""HTTP 客户端:封装对快报 API 的请求,自动带上认证 cookie 和 corpId""" +"""HTTP 客户端:封装对快报 API 的请求,使用 accesstoken + corpid + staffid 认证""" import httpx from typing import Any @@ -7,14 +7,17 @@ BASE_URL = "https://app.ekuaibao.com" class EkbClient: - def __init__(self, corp_id: str, cookie: str): + def __init__(self, corp_id: str, access_token: str, staff_id: str): self.corp_id = corp_id self._client = httpx.Client( base_url=BASE_URL, headers={ - "Cookie": cookie, + "accesstoken": access_token, + "corpid": corp_id, + "staffid": staff_id, "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36", - "Accept": "application/json, text/plain, */*", + "Accept": "application/json", + "Content-Type": "application/json", "Accept-Language": "zh-CN,zh;q=0.9", }, timeout=30, @@ -46,4 +49,8 @@ def get_client() -> EkbClient: if not corp: raise RuntimeError("未登录,请先执行 `ekb auth add` 添加企业认证") corp_id = config.get_current_corp_id() - return EkbClient(corp_id=corp_id, cookie=corp["cookie"]) + return EkbClient( + corp_id=corp_id, + access_token=corp["access_token"], + staff_id=corp["staff_id"], + ) diff --git a/src/ekb_cli/commands/agent.py b/src/ekb_cli/commands/agent.py index 08cd8fa..54dae0d 100644 --- a/src/ekb_cli/commands/agent.py +++ b/src/ekb_cli/commands/agent.py @@ -1,4 +1,4 @@ -"""agent 命令:智能审核 Agent 管理(骨架,待根据 API 响应完善)""" +"""agent 命令:智能审核 Agent 管理""" import typer from rich.console import Console @@ -10,27 +10,35 @@ from ekb_cli.api import get_client app = typer.Typer(help="智能审核 Agent 管理") console = Console() +STATE_MAP = {0: "[green]启用[/green]", 1: "[red]禁用[/red]"} + @app.command("list") def agent_list(): """列出所有智能审核 Agent""" with get_client() as client: - data = client.get("/engine/hab/agent/list", page=1, size=1000) + resp = client.get("/engine/hab/agent/list", page=1, size=1000) - items = data.get("items") or data.get("data") or [] + items = resp.get("data", {}).get("items") or [] if not items: rprint("[yellow]暂无 Agent[/yellow]") raise typer.Exit() table = Table(title="智能审核 Agent 列表") - table.add_column("ID") + table.add_column("ID", style="dim") table.add_column("名称") + table.add_column("模式") table.add_column("状态") + table.add_column("更新时间") for item in items: + state_text = STATE_MAP.get(item.get("state"), str(item.get("state", ""))) + mode = "自动" if item.get("mode") == 2 else str(item.get("mode", "")) table.add_row( str(item.get("id", "")), item.get("name", ""), - item.get("status", ""), + mode, + state_text, + item.get("updateTime", ""), ) console.print(table) diff --git a/src/ekb_cli/commands/auth.py b/src/ekb_cli/commands/auth.py index 234684b..b7253b1 100644 --- a/src/ekb_cli/commands/auth.py +++ b/src/ekb_cli/commands/auth.py @@ -1,6 +1,5 @@ """auth 命令:管理企业认证信息""" -import re import typer from rich.console import Console from rich.table import Table @@ -12,32 +11,24 @@ app = typer.Typer(help="管理企业认证") console = Console() -def _parse_corp_id(cookie: str) -> str: - """从 cookie 中尝试提取 corpId""" - match = re.search(r"corpId=([^;& ]+)", cookie) - return match.group(1) if match else "" - - @app.command("add") def auth_add(): - """添加企业认证(从浏览器 F12 复制请求的 Cookie 头)""" + """添加企业认证(从浏览器 F12 复制请求头中的 accesstoken / corpid / staffid)""" console.print("\n[bold]添加企业认证[/bold]") - console.print("步骤:打开快报 → F12 → 任意请求 → 复制 [cyan]Cookie[/cyan] 请求头的值\n") + console.print("步骤:打开快报 → F12 → 任意 API 请求 → 请求头中找到以下三个字段\n") - cookie = typer.prompt("粘贴 Cookie 值").strip() - if not cookie: - rprint("[red]Cookie 不能为空[/red]") - raise typer.Exit(1) + access_token = typer.prompt("accesstoken").strip() + corp_id = typer.prompt("corpid").strip() + staff_id = typer.prompt("staffid").strip() - detected = _parse_corp_id(cookie) - corp_id = typer.prompt("企业 corpId", default=detected).strip() - if not corp_id: - rprint("[red]corpId 不能为空[/red]") - raise typer.Exit(1) + for label, val in [("accesstoken", access_token), ("corpid", corp_id), ("staffid", staff_id)]: + if not val: + rprint(f"[red]{label} 不能为空[/red]") + raise typer.Exit(1) name = typer.prompt("企业名称(方便识别)", default=corp_id).strip() - config.add_corp(corp_id=corp_id, name=name, cookie=cookie) + config.add_corp(corp_id=corp_id, name=name, access_token=access_token, staff_id=staff_id) rprint(f"[green]✓[/green] 已添加企业 [bold]{name}[/bold] ({corp_id})") diff --git a/src/ekb_cli/config.py b/src/ekb_cli/config.py index 6c2a761..1e4e4e2 100644 --- a/src/ekb_cli/config.py +++ b/src/ekb_cli/config.py @@ -35,9 +35,9 @@ def get_current_corp() -> Optional[dict]: return data["corps"].get(corp_id) -def add_corp(corp_id: str, name: str, cookie: str) -> None: +def add_corp(corp_id: str, name: str, access_token: str, staff_id: str) -> None: data = _load() - data["corps"][corp_id] = {"name": name, "cookie": cookie} + data["corps"][corp_id] = {"name": name, "access_token": access_token, "staff_id": staff_id} if not data.get("current"): data["current"] = corp_id _save(data)