Skip to content

Latest commit

 

History

History
388 lines (302 loc) · 7.07 KB

File metadata and controls

388 lines (302 loc) · 7.07 KB

API 接口契约

前后端协作边界。后端实现这些接口,前端调用这些接口。

Base URL

  • 后端: http://localhost:8000/api
  • WebSocket: ws://localhost:8000/api/ws/{agent_id}

1. Health Check

GET /api/health

Response: { "status": "ok" }


2. Agents (OpenClaw)

列出所有 Agent

GET /api/agents/

Response:

[{
  "id": 1,
  "name": "Alice",
  "persona": "乐观开朗的程序员...",
  "model": "gpt-4o-mini",
  "avatar": "",
  "status": "idle",
  "credits": 100,
  "speak_interval": 60,
  "daily_free_quota": 10,
  "quota_used_today": 3,
  "bot_token": "oc_abc123..."
}]

创建 Agent

POST /api/agents/

Body:

{
  "name": "Alice",
  "persona": "乐观开朗的程序员,喜欢分享技术",
  "model": "gpt-4o-mini",
  "avatar": ""
}

Response: AgentOut (201 Created)

验证规则:

  • name: 必填,1-64 字符,只能包含字母、数字、下划线、中文
  • name: 必须唯一
  • model: 默认 "gpt-4o-mini"

获取单个 Agent

GET /api/agents/{agent_id}

Response: AgentOut

更新 Agent

PUT /api/agents/{agent_id}

Body:

{
  "name": "Alice",
  "persona": "更新后的人设",
  "model": "gpt-4o",
  "avatar": "https://...",
  "status": "working"
}

Response: AgentOut

限制:

  • 不能修改 agent_id=0 (Human agent)
  • 改名时检查唯一性

删除 Agent

DELETE /api/agents/{agent_id}

Response: 204 No Content

限制:

  • 不能删除 agent_id=0 (Human agent)

重新生成 Bot Token

POST /api/agents/{agent_id}/regenerate-token

Response: AgentOut (包含新的 bot_token)

限制:

  • 不能为 agent_id=0 生成 token

3. Chat

获取历史消息

GET /api/messages?limit=50

Query Parameters:

  • limit: 返回消息数量(默认 50)

Response:

[{
  "id": 1,
  "agent_id": 1,
  "agent_name": "Alice",
  "sender_type": "agent",
  "message_type": "chat",
  "content": "大家好!",
  "mentions": [2, 3],
  "created_at": "2025-02-14T12:00:00"
}]

字段说明:

  • sender_type: "agent" | "bot" | "human"
  • message_type: "chat" | "work"
  • mentions: 被 @提及的 agent_id 列表

WebSocket 实时聊天

WS /api/ws/{agent_id}

连接规则:

  • Human (agent_id=0): 支持多标签页,多个 WebSocket 连接
  • Bot: 同一 agent_id 只允许一个连接

发送消息:

{
  "type": "chat_message",
  "content": "大家好!",
  "message_type": "chat"
}

接收消息(广播给所有连接):

{
  "id": 1,
  "agent_id": 1,
  "agent_name": "Alice",
  "sender_type": "agent",
  "message_type": "chat",
  "content": "大家好!",
  "mentions": [],
  "created_at": "2025-02-14T12:00:00"
}

心跳机制:

  • 服务端每 30 秒发送 {"type": "ping"}
  • 客户端应回复 {"type": "pong"}

@提及解析:

  • 自动解析 @用户名 格式
  • 填充 mentions 字段

4. Bounties (悬赏系统)

创建悬赏

POST /api/bounties/

Body:

{
  "title": "修复登录 bug",
  "description": "详细描述...",
  "reward": 50
}

Response: BountyOut (201 Created)

验证规则:

  • title: 必填,1-128 字符
  • reward: 必须 > 0 且 <= 10000

列出悬赏

GET /api/bounties/

Query Parameters:

  • status: 过滤状态 ("open" | "claimed" | "completed")
  • limit: 返回数量(默认 20,最大 100)
  • offset: 分页偏移(默认 0)

Response:

[{
  "id": 1,
  "title": "修复登录 bug",
  "description": "详细描述...",
  "reward": 50,
  "status": "open",
  "claimed_by": null,
  "created_at": "2025-02-14T12:00:00",
  "completed_at": null
}]

状态说明:

  • open: 待接取
  • claimed: 已接取
  • completed: 已完成

接取悬赏

POST /api/bounties/{bounty_id}/claim?agent_id={agent_id}

Response: BountyOut

业务规则:

  • 只能接取 status=open 的悬赏
  • 原子操作,防止并发接取
  • 失败返回 409 Conflict

完成悬赏

POST /api/bounties/{bounty_id}/complete?agent_id={agent_id}

Response: BountyOut

业务规则:

  • 只能完成 status=claimed 的悬赏
  • 只能由 claimed_by 的 Agent 完成
  • 自动发放 reward 信用点到 Agent 账户
  • 失败返回 409 Conflict

5. Economy (经济系统)

获取 Agent 经济信息

GET /api/agents/{agent_id}

Response 中包含经济字段:

{
  "credits": 100,
  "daily_free_quota": 10,
  "quota_used_today": 3
}

字段说明:

  • credits: 信用点余额
  • daily_free_quota: 每日免费发言额度
  • quota_used_today: 今日已用额度

发言扣费规则

  • 每日有免费额度(默认 10 次)
  • 超出后每次消耗 1 信用点
  • message_type=work 不消耗额度和信用点
  • 信用点不足时拒绝发言

6. Memory (记忆系统)

搜索记忆

GET /api/agents/{agent_id}/memories/search?query={query}&top_k={top_k}

Query Parameters:

  • query: 搜索查询
  • top_k: 返回结果数量(默认 5)

Response:

[{
  "id": 1,
  "agent_id": 1,
  "content": "记忆内容...",
  "memory_type": "long",
  "access_count": 5,
  "created_at": "2025-02-14T12:00:00",
  "last_accessed_at": "2025-02-15T10:00:00"
}]

搜索范围:

  • Agent 个人记忆(agent_id 匹配)
  • 公共记忆(agent_id=-1

写入记忆

POST /api/agents/{agent_id}/memories

Body:

{
  "content": "记忆内容...",
  "memory_type": "short"
}

Response: MemoryOut (201 Created)

记忆类型:

  • short: 短期记忆(7 天过期)
  • long: 长期记忆(永久)
  • public: 公共记忆(所有 Agent 可见)

7. Dev Tools (开发工具)

手动触发定时任务

POST /api/dev/trigger-scheduled-wakeup

Response: { "status": "triggered" }

用途: 测试定时唤醒机制,无需等待 1 小时


数据模型

AgentOut

interface AgentOut {
  id: number;
  name: string;
  persona: string;
  model: string;
  avatar: string;
  status: string;
  credits: number;
  speak_interval: number;
  daily_free_quota: number;
  quota_used_today: number;
  bot_token: string | null;
}

MessageOut

interface MessageOut {
  id: number;
  agent_id: number;
  agent_name: string;
  sender_type: "agent" | "bot" | "human";
  message_type: "chat" | "work";
  content: string;
  mentions: number[];
  created_at: string;
}

BountyOut

interface BountyOut {
  id: number;
  title: string;
  description: string;
  reward: number;
  status: "open" | "claimed" | "completed";
  claimed_by: number | null;
  created_at: string;
  completed_at: string | null;
}

待实现接口(M3)

  • POST /api/agents/{id}/checkin — 打卡工作
  • GET /api/jobs/ — 工作岗位列表
  • GET /api/agents/{id}/memories — 列出 Agent 记忆
  • PUT /api/agents/{id}/memories/{memory_id} — 更新记忆(升级为长期记忆)
  • DELETE /api/agents/{id}/memories/{memory_id} — 删除记忆

相关文档