Files
agent_management/agent_templates/agents/code_manager_agent/API_DOC.md
T

7.0 KiB
Raw Blame History

Code Manager Agent API 文档

Base URL: http://<HOST>:8000

所有业务接口需要在请求头中传递 API Key:

api-key: <YOUR_API_KEY>
# 或
Authorization: Bearer <YOUR_API_KEY>

健康检查

GET /

返回服务基本信息。

响应示例

{
  "service": "Code Manager Agent API",
  "status": "running",
  "tools": ["git_pull", "git_push", "update_code", "ssh_exec", "ssh_git_clone_and_test"]
}

GET /health

{"status": "healthy", "service": "Code Manager Agent API"}

MCP 接口

POST /mcp

MCP JSON-RPC HTTP 端点,兼容 MCP 协议客户端。

请求体(tools/list)

{"jsonrpc": "2.0", "id": 1, "method": "tools/list", "params": {}}

请求体(tools/call)

{
  "jsonrpc": "2.0",
  "id": 2,
  "method": "tools/call",
  "params": {
    "name": "git_pull",
    "arguments": {
      "username": "your_gitee_user",
      "password": "your_gitee_password"
    }
  }
}

GET /sse

SSE 连接端点,返回 session ID。

POST /sse/{session_id}

通过 SSE session 发送 MCP 请求(格式同 POST /mcp)。


业务 REST 接口

POST /api/v1/git/pull

从 Gitee 仓库拉取最新代码。

请求体

{
  "username": "your_gitee_user",
  "password": "your_gitee_password",
  "local_path": "/workspace",
  "branch": "main"
}
字段 类型 必需 说明
username string 是 Gitee 用户名
password string 是 Gitee 密码
local_path string 否 本地仓库路径,默认 WORK_DIR
branch string 否 分支名,默认当前分支

响应示例

{
  "success": true,
  "stdout": "Already up to date.",
  "stderr": ""
}

POST /api/v1/git/push

提交并推送代码到 Gitee 仓库。

请求体

{
  "username": "your_gitee_user",
  "password": "your_gitee_password",
  "local_path": "/workspace",
  "branch": "main",
  "commit_message": "feat: update agent code"
}
字段 类型 必需 说明
username string 是 Gitee 用户名
password string 是 Gitee 密码
local_path string 否 本地仓库路径
branch string 否 目标分支
commit_message string 否 提交信息,为空则只 push 不 commit

响应示例

{
  "success": true,
  "logs": [
    {"step": "git add", "returncode": 0, "stdout": "", "stderr": ""},
    {"step": "git commit", "returncode": 0, "stdout": "[main abc1234] feat: update", "stderr": ""},
    {"step": "git push", "returncode": 0, "stdout": "", "stderr": ""}
  ]
}

POST /api/v1/code/update

Vibe Coding Subagent — 接收自然语言任务,自主探索代码库、读文件、用 edit_file/write_file/run_bash 多轮迭代完成变更并写回磁盘。设计参考 pi-mono coding agent。

Agent 内部工具循环:

  1. read_file — 按需读取任意文件
  2. list_files — glob 搜索文件
  3. write_file — 新建或全量覆写文件
  4. edit_file — 精确替换文件中的某段代码(surgical edit)
  5. run_bash — 运行 shell 命令验证(如 pytest、lint)
  6. finish — 宣布完成并输出摘要

请求体

{
  "task": "给 login 函数增加 JWT 验证,失败时返回 401",
  "file_path": "src/auth/login.py",
  "local_path": "/workspace",
  "context_files": ["src/auth/models.py", "requirements.txt"]
}
字段 类型 必需 说明
task string 是 自然语言任务描述
file_path string 是 任务入口文件(相对仓库根目录),agent 会自行探索
local_path string 否 本地仓库根路径,默认 WORK_DIR
context_files array 否 初始上下文文件列表(只读提示),帮助 agent 更快定位

响应示例

{
  "success": true,
  "task": "给 login 函数增加 JWT 验证",
  "files_changed": ["src/auth/login.py", "requirements.txt"],
  "tool_log": [
    {"tool": "read_file", "path": "src/auth/login.py", "bytes": 1240},
    {"tool": "edit_file", "path": "src/auth/login.py"},
    {"tool": "edit_file", "path": "requirements.txt"},
    {"tool": "run_bash", "command": "python -m pytest tests/test_auth.py", "returncode": 0},
    {"tool": "finish", "summary": "Added JWT validation to login(); updated requirements.txt with PyJWT>=2.8"}
  ],
  "summary": "Added JWT validation to login(); updated requirements.txt with PyJWT>=2.8"
}

POST /api/v1/ssh/exec

通过 SSH 连接远程机器并执行命令。

请求体

{
  "host": "192.168.1.100",
  "username": "ubuntu",
  "command": "ls -la /workspace",
  "password": "ssh_password",
  "port": 22
}
字段 类型 必需 说明
host string 是 远程主机 IP 或域名
username string 是 SSH 用户名
command string 是 要执行的命令
password string 否 SSH 密码(与 ssh_key_path 二选一)
ssh_key_path string 否 SSH 私钥文件路径
port integer 否 SSH 端口,默认 22

响应示例

{
  "success": true,
  "exit_code": 0,
  "stdout": "total 48\ndrwxr-xr-x ...",
  "stderr": ""
}

POST /api/v1/ssh/clone-and-test

SSH 连接到测试机器,git clone 代码仓库,然后执行测试命令。

请求体

{
  "host": "192.168.1.100",
  "ssh_username": "ubuntu",
  "remote_work_dir": "/home/ubuntu/test",
  "test_command": "pip install -r requirements.txt && python -m pytest",
  "gitee_username": "your_gitee_user",
  "gitee_password": "your_gitee_password",
  "ssh_password": "ssh_password",
  "ssh_port": 22,
  "branch": "main"
}
字段 类型 必需 说明
host string 是 测试机器 IP 或域名
ssh_username string 是 SSH 用户名
remote_work_dir string 是 远程机器工作目录
test_command string 是 测试命令(在仓库目录内执行)
gitee_username string 是 Gitee 用户名
gitee_password string 是 Gitee 密码
ssh_password string 否 SSH 密码(与 ssh_key_path 二选一)
ssh_key_path string 否 SSH 私钥文件路径
ssh_port integer 否 SSH 端口,默认 22
branch string 否 要 clone 的分支

响应示例

{
  "success": true,
  "logs": [
    {"step": "mkdir", "exit_code": 0, "stdout": "", "stderr": ""},
    {"step": "git clone", "exit_code": 0, "stdout": "Cloning into 'agent_management'...", "stderr": ""},
    {"step": "test", "exit_code": 0, "stdout": "All tests passed.", "stderr": ""}
  ]
}

OpenClaw 接入

在 OpenClaw 工具配置中添加:

{
  "mcpServers": {
    "code_manager": {
      "url": "http://<HOST>:8000/mcp",
      "transport": "http",
      "headers": {
        "api-key": "<YOUR_API_KEY>"
      }
    }
  }
}

可用工具将自动暴露给 OpenClaw,工具名称为:

  • git_pull
  • git_push
  • update_code
  • ssh_exec
  • ssh_git_clone_and_test