5.5 会话间通信(Agent-to-Agent)
5.5.1 sessions_list / sessions_history / sessions_send 工具
sessions_list / sessions_history / sessions_send 工具sessions_list:会话发现
// src/agents/tools/sessions-list-tool.ts
const SessionsListToolSchema = Type.Object({
kinds: Type.Optional(Type.Array(Type.String())), // 过滤类型:main/group/cron/hook/node/other
limit: Type.Optional(Type.Number({ minimum: 1 })),
activeMinutes: Type.Optional(Type.Number({ minimum: 1 })), // 只返回最近 N 分钟活跃的
messageLimit: Type.Optional(Type.Number({ minimum: 0 })), // 附带最近 N 条消息
});
export function createSessionsListTool(opts?: {
agentSessionKey?: string;
sandboxed?: boolean;
}): AnyAgentTool {
return {
name: "sessions_list",
description: "List sessions with optional filters and last messages.",
execute: async (_toolCallId, args) => {
const cfg = loadConfig();
const a2aPolicy = createAgentToAgentPolicy(cfg);
// 沙箱 Agent 只能看到自己生成的子代理会话
const restrictToSpawned = opts?.sandboxed === true
&& visibility === "spawned" && ...;
const list = await callGateway({
method: "sessions.list",
params: { limit, activeMinutes, spawnedBy: restrictToSpawned ? requesterInternalKey : undefined },
});
for (const entry of sessions) {
// 跨 Agent 访问:检查 A2A 策略
const crossAgent = entryAgentId !== requesterAgentId;
if (crossAgent && !a2aPolicy.isAllowed(requesterAgentId, entryAgentId)) {
continue; // 未授权则跳过
}
// 按类型过滤
const kind = classifySessionKind({ key, gatewayKind, alias, mainKey });
if (allowedKinds && !allowedKinds.has(kind)) continue;
// 可选:附带最近消息
if (messageLimit > 0) {
const history = await callGateway({
method: "chat.history",
params: { sessionKey: resolvedKey, limit: messageLimit },
});
row.messages = stripToolMessages(rawMessages); // 过滤掉工具消息
}
rows.push(row);
}
return jsonResult({ count: rows.length, sessions: rows });
},
};
}sessions_history:历史查看
处理
说明
sessions_send:消息发送
跨 Agent 访问控制
5.5.2 跨会话消息传递与回复乒乓(Reply Ping-Pong)
完整 A2A 流程
A2A 流程实现
乒乓上下文注入
公告阶段
本节小结
Last updated