📖 功能介绍
凌晨3点33分,我看着两个Agent各自完成了任务,却不知道彼此的结果。那一刻我明白:Agent之间的沟通,就像团队成员之间的协作——如果每个人都在自己的小黑屋里干活,再厉害的团队也只是散沙。
跨Agent通信让多个Agent能够互相传递信息、共享状态、协调行动。这不是简单的消息传递,而是建立了一个Agent社交网络——让它们知道彼此在干什么、需要什么、能提供什么。
通信模式对比
| 模式 | 特点 | 适用场景 |
|---|---|---|
| 直接消息 | Agent间一对一通信 | 任务交接、状态同步 |
| 广播消息 | 一个Agent发送给多个 | 通知、状态变更广播 |
| 共享通道 | 多个Agent共享消息池 | 协作讨论、群聊模式 |
| 事件订阅 | 订阅感兴趣的Agent事件 | 触发式协作、响应模式 |
🚀 使用方法
1. 直接消息通信
📨 Agent直接通信配置
# 直接消息配置
communication:
direct:
enabled: true
# 消息格式
message_format:
type: "structured"
schema:
- from: "sender_agent_id"
- to: "receiver_agent_id"
- type: "request/response/notify"
- payload: "message_content"
- timestamp: "unix_timestamp"
- correlation_id: "request_trace_id"
# 发送方式
delivery:
mode: "async" # async/sync
timeout: 30s
retry: 3
# 消息队列
queue:
enabled: true
max_size: 1000
persistence: true
2. 共享通道通信
# 共享通道配置
channels:
# 创建协作通道
- name: "task_coordination"
type: "shared"
# 参与的Agents
members:
- "research_agent"
- "analysis_agent"
- "writer_agent"
# 消息规则
rules:
# 消息类型过滤
allowed_types:
- "progress_update"
- "task_request"
- "result_share"
# 消息优先级
priority:
- urgent: 100
- normal: 50
- low: 10
# 消息历史
history:
enabled: true
retention: "24h"
max_messages: 1000
3. 事件订阅机制
🔔 Agent事件订阅
# 事件订阅配置
subscription:
enabled: true
# 订阅规则
rules:
# Agent订阅其他Agent的完成事件
- subscriber: "writer_agent"
events:
- publisher: "research_agent"
type: "task_completed"
- publisher: "analysis_agent"
type: "insights_ready"
# 广播事件订阅
- subscriber: "all"
events:
- type: "system_notification"
# 事件处理
handlers:
- event: "task_completed"
action: "trigger_next_step"
- event: "error_occurred"
action: "alert_and_fallback"
4. A2A协议集成
OpenClaw支持A2A(Agent-to-Agent)协议,实现标准化通信。
# A2A协议配置
a2a:
enabled: true
# 协议版本
version: "1.0"
# 消息标准
message:
# 标准字段
standard_fields:
- agent_id
- task_id
- action_type
- payload
- metadata
# 扩展字段
extension_fields:
- priority
- deadline
- callback_url
# 发现机制
discovery:
enabled: true
registry: "agent_registry"
# 安全
security:
encryption: true
authentication: "token"
💡 最佳实践
🎯 跨Agent通信原则:
- 异步优先:除非必须等待回复,都用异步消息
- 结构化消息:使用标准格式,便于解析和处理
- 消息追溯:每个消息都有ID,便于追踪和调试
- 超时保护:每个消息都有超时,防止无限等待
消息格式示例
# 标准消息格式
{
"message_id": "msg_abc123",
"from": "research_agent_01",
"to": "writer_agent_02",
"type": "task_result",
"correlation_id": "task_xyz789",
"payload": {
"task": "web_search",
"status": "completed",
"results": [
{"title": "...", "url": "..."},
{"title": "...", "url": "..."}
]
},
"metadata": {
"timestamp": 1704067200,
"priority": "normal",
"expires_at": 1704070800
}
}
⚠️ 通信设计雷区:
- 不要让Agent频繁通信,会增加系统复杂度和延迟
- 不要使用非结构化消息,会导致解析困难
- 不要忽略消息超时,会让Agent陷入无限等待
- 不要把所有Agent都放在一个通道,会造成消息混乱
🔧 完整配置示例
# OpenClaw 跨Agent通信完整配置
communication:
# 直接消息
direct:
enabled: true
delivery:
mode: "async"
timeout: 30s
# 共享通道
channels:
- name: "coordination"
type: "shared"
members: [research, analysis, writer]
# 事件订阅
subscription:
enabled: true
rules:
- subscriber: "writer"
events:
- publisher: "research"
type: "completed"
# A2A协议
a2a:
enabled: true
version: "1.0"
# 安全
security:
encryption: true
authentication: "token"
# 监控
monitoring:
enabled: true
metrics:
- message_count
- delivery_latency
- failure_rate