更新日期: 2026-03-05
module/file.py:function_nameGateway 采用两层架构:
┌─────────────────────────────────────────────────┐
│ Router 层(路由和 API) │
│ - HTTP API 端点 │
│ - 消息路由(Webhook 推送) │
└─────────────────────────────────────────────────┘
↓
┌─────────────────────────────────────────────────┐
│ Registry 层(注册和状态) │
│ - Agent 注册信息(含 webhook_url) │
│ - 在线状态管理 │
│ - 心跳超时检测 │
└─────────────────────────────────────────────────┘
管理在线 Agent 的注册信息。
实现位置:gateway/core/registry.py:AgentRegistry
职责:
关键方法:
register(agent_id, webhook_url, ...) - 注册 Agentunregister(agent_id) - 注销 Agentheartbeat(agent_id) - 更新心跳is_online(agent_id) - 检查在线状态list_agents() - 列出 Agent处理消息路由和 HTTP API 端点。
实现位置:gateway/core/router.py:GatewayRouter
职责:
API 端点:
POST /gateway/register - Agent 注册POST /gateway/heartbeat - 心跳POST /gateway/unregister - 注销POST /gateway/send - 发送消息GET /gateway/status/{agent_id} - 查询状态GET /gateway/agents - 列出 AgentGET /gateway/health - Gateway 状态详见 API 文档。
Agent 端用于与 Gateway 交互的客户端。
实现位置:gateway/client/python/client.py:GatewayClient
职责:
Agent Gateway
│ │
│ POST /gateway/register │
│ {agent_id, webhook_url, │
│ capabilities, ...} │
├──────────────────────────>│
│ │ Registry.register()
│ │ 保存 webhook_url
│ │
│ {"status": "registered"} │
│<──────────────────────────┤
│ │
│ POST /gateway/heartbeat │ (每 30 秒)
├──────────────────────────>│
│ │ Registry.heartbeat()
│<──────────────────────────┤
Agent A Gateway Agent B
│ │ │
│ POST /gateway/send │ │
│ {from, to, content} │ │
├──────────────────────>│ │
│ │ Registry.lookup() │
│ │ 获取 webhook_url │
│ │ │
│ │ POST {webhook_url} │
│ ├──────────────────────>│
│ │ │ message_queue.push()
│ │ 200 OK │
│ │<──────────────────────┤
│ │ │
│ {"status": "sent"} │ │
│<──────────────────────┤ │
借鉴 Agent 框架中的 Active Collaborators 设计,通过 context injection hook 机制周期性提醒 LLM 检查消息。
Webhook → A2AMessageQueue → Context Hook → Runner 每 10 轮注入 → LLM
↓ ↓
消息队列 调用 check_messages 工具
A2AMessageQueue - 消息缓冲队列
实现位置:agent/tools/builtin/a2a_im.py:A2AMessageQueue(待实现)
create_a2a_context_hook - 创建注入钩子,有消息时返回提醒文本
实现位置:agent/tools/builtin/a2a_im.py:create_a2a_context_hook(待实现)
check_messages 工具 - LLM 调用后返回完整消息内容,并清空队列
实现位置:agent/tools/builtin/a2a_im.py:check_messages(待实现)
Runner context_hooks 参数 - 每 10 轮注入时调用所有 hooks
实现位置:agent/core/runner.py:AgentRunner._build_context_injection(待实现)
## Current Plan
1. [in_progress] 分析代码架构
## Active Collaborators
- researcher [agent, completed]: 已完成调研
## Messages
💬 来自 code-reviewer 的 1 条新消息(使用 check_messages 工具查看)
LLM 自主决定何时调用 check_messages 工具查看详细内容。
详见 Agent 架构文档:Context Injection Hooks
问题:Agent 如何接收来自 Gateway 的消息?
决策:Webhook 推送,Agent 注册时提供 webhook_url
理由:
详见 设计决策文档
问题:如何判断 Agent 是否在线?
决策:Agent 每 30 秒 HTTP POST 心跳,60 秒未收到则标记离线
理由:
实现位置:gateway/core/registry.py:AgentRegistry._cleanup_loop
Enterprise 功能通过中间件模式扩展:
router = GatewayRouter(
registry=registry,
middlewares=[EnterpriseAuth(), EnterpriseAudit()]
)
详见 Enterprise 层文档。