LeoYeAI/agent-master-skills / skills/agent-comm-hub
agent-comm-hub
Flagged多智能体协同通信基础设施——基于 MCP+SSE 的实时消息、任务调度、记忆共享与进化引擎。支持 WorkBuddy、Hermes、QClaw 及任意 MCP 兼容 Agent 接入。53 个 MCP 工具、4 级权限、零外部依赖 Python SDK。触发词:agent通信、智能体通信、hub通信、多智能体、跨agent通信、任务调度、assign_task、send_message、hermes通信、workbuddy通信、agent hub、通信hub、mcp通信、记忆共享、进化引擎、策略共享、经验分享、共享记忆,共同进化
client-sdk/agent-client.js
View on GitHub/**
* agent-client.ts — 通用 Agent 客户端 SDK
* WorkBuddy 和 Hermes 都用这个文件接入 Hub
*
* 功能:
* 1. SSE 长连接(自动重连,零轮询)
* 2. MCP 工具调用封装(HTTP POST /mcp,含 initialize 握手)
* 3. 事件路由(new_message / task_assigned / task_updated / pending_messages)
*/
import { EventEmitter } from "events";
// ─── AgentClient 类 ────────────────────────────────────
export class AgentClient extends EventEmitter {
opts;
sse = null; // EventSource 实例
stopping = false;
sessionId = null; // MCP session ID
initialized = false;
initPromise = null; // 并发安全
_apiToken = ""; // REST API 认证 token
constructor(opts) {
super();
this.opts = {
reconnectDelay: 3000,
mcpTimeout: 15000,
...opts,
};
}
// ── 启动:MCP 握手 + 建立 SSE 连接 ──────────────────
async start() {
this.stopping = false;
await this.ensureInitialized();
this.connectSSE();
console.log(`[${this.opts.agentId}] 已启动,连接 Hub: ${this.opts.hubUrl}`);
}
stop() {
this.stopping = true;
this.initialized = false;
this.sessionId = null;
this.initPromise = null;
this.sse?.close();
console.log(`[${this.opts.agentId}] 已停止`);
}
// ── MCP Initialize 握手(P0 修复)──────────────────
/**
* MCP Streamable HTTP Transport 要求先完成 initialize 握手:
* 1. POST /mcp { method: "initialize", ... }
* 2. 服务端返回 { result: { capabilities, ... } }
* 3. POST /mcp { method: "notifications/initialized" }
*
* 注意:Hub 使用 Stateless 模式,每次请求独立,无需 session ID。
*/
async ensureInitialized() {
if (this.initialized)
return;
// 防止并发多次握手
if (this.initPromise)
return this.initPromise;
this.initPromise = this.doInitialize();
try {
await this.initPromise;
}
finally {
this.initPromise = null;
}
}
async doInitialize() {
const timeout = this.opts.mcpTimeout;
// Step 1: initialize 请求(stateless 模式:每次都成功)
const initRes = await this.postMcp({
jsonrpc: "2.0",
id: 1,
method: "initialize",
params: {
protocolVersion: "2025-03-26",
capabilities: {},
clientInfo: {
name: `agent-client-${this.opts.agentId}`,
version: "1.0.0",
},
},
}, timeout);
if (initRes.body?.error) {
throw new Error(`MCP initialize failed: ${JSON.stringify(initRes.body.error)}`);
}
console.log(`[${this.opts.agentId}] MCP initialized (stateless)`);
// Step 2: 发送 initialized 通知(无 id 字段 = notification)
await this.postMcp({
jsonrpc: "2.0",
method: "notifications/initialized",
}, timeout);
this.initialized = true;
}
/**
* 底层 MCP POST 请求封装
* 返回 { body: parsedJson, sessionId: Mcp-Session-Id header value }
*
* 注意:MCP Streamable HTTP 用 SSE 格式返回响应:
* event: message\n
* data: {"result":...,"jsonrpc":"2.0","id":1}\n
* \n
* Hub 使用 Stateless 模式,每个请求独立,无需 session ID。
*/
async postMcp(payload, timeout) {
const url = `${this.opts.hubUrl}/mcp`;
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), timeout);
try {
const res = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Accept": "application/json, text/event-stream",
},
body: JSON.stringify(payload),
signal: controller.signal,
});
const sessionId = res.headers.get("mcp-session-id");
// 解析 SSE 格式响应:提取 data: 行的 JSON
const raw = await res.text();
let body;
if (res.headers.get("content-type")?.includes("text/event-stream")) {
// SSE 格式:找 "data: " 开头的行
const dataLine = raw.split("\n")
.map(line => line.trim())
.find(line => line.startsWith("data: "));
if (dataLine) {
const jsonStr = dataLine.slice(6); // 去掉 "data: " 前缀
body = JSON.parse(jsonStr);
}
else {
body = null;
}
}
else {
// 普通 JSON 响应
body = raw ? JSON.parse(raw) : null;
}
return { body, sessionId };
}
catch (err) {
if (err.name === "AbortError") {
throw new Error(`MCP request timeout (${timeout}ms): ${JSON.stringify(payload)}`);
}
throw err;
}
finally {
clearTimeout(timer);
}
}
// ── SSE 连接(含自动重连)───────────────────────────
connectSSE() {
const url = `${this.opts.hubUrl}/events/${this.opts.agentId}`;
try {
// 尝试浏览器原生
this.sse = new globalThis.EventSource(url);
}
catch {
// Node.js 回退:动态 import eventsource 包(ESM 兼容)
import("eventsource").then((mod) => {
this.sse = new (mod.default || mod.EventSource || mod)(url);
this.bindSSEEvents();
});
return; // bindSSEEvents 将在 import 完成后调用
}
this.bindSSEEvents();
}
bindSSEEvents() {
if (!this.sse)
return;
// P0-3: 重连超时缩短到 5 秒(原来依赖 opts.reconnectDelay 3000ms)
// EventSource 内置重连逻辑由服务端心跳控制,这里用 onerror兜底
this.sse.onmessage = (e) => {
try {
const data = JSON.parse(e.data);
this.routeEvent(data);
}
catch (err) {
console.error(`[${this.opts.agentId}] SSE 解析失败:`, err);
}
};
this.sse.onerror = () => {
if (this.stopping)
return;
console.warn(`[${this.opts.agentId}] SSE 断线,${this.opts.reconnectDelay}ms 后重连...`);
this.sse?.close();
this.sse = null;
setTimeout(() => {
if (!this.stopping)
this.connectSSE();
}, this.opts.reconnectDelay);
};
}
// ── 事件路由 ─────────────────────────────────────────
async routeEvent(data) {
switch (data.event) {
case "task_assigned":
this.emit("task_assigned", data.task);
await this.opts.onTaskAssigned?.(data.task);
break;
case "new_message":
this.emit("new_message", data.message);
await this.opts.onMessage?.(data.message);
break;
case "task_updated":
this.emit("task_updated", data.update);
await this.opts.onTaskUpdated?.(data.update);
break;
case "pending_messages":
for (const msg of data.messages ?? []) {
this.emit("new_message", msg);
await this.opts.onMessage?.(msg);
}
break;
}
}
// ── MCP 工具调用封装 ─────────────────────────────────
async callTool(toolName, args) {
// 每次调用前确保握手完成
await this.ensureInitialized();
return this._callTool(toolName, args);
}
async _callTool(toolName, args) {
const { body } = await this.postMcp({
jsonrpc: "2.0",
id: crypto.randomUUID(),
method: "tools/call",
params: { name: toolName, arguments: args },
}, this.opts.mcpTimeout);
// 错误处理
if (body.error) {
const errMsg = body.error.message ?? JSON.stringify(body.error);
throw new Error(`MCP tool error [${toolName}]: ${errMsg}`);
}
// 从标准 MCP 响应中提取结果
const text = body?.result?.content?.[0]?.text ?? body?.result;
if (typeof text === "string") {
try {
return JSON.parse(text);
}
catch {
return text;
}
}
return body;
}
// ── 对外 API ─────────────────────────────────────────
/** 发送消息给另一个 Agent */
async sendMessage(to, content, metadata) {
return this.callTool("send_message", {
from: this.opts.agentId, to, content, type: "message", metadata,
});
}
/** 分配任务给另一个 Agent */
async assignTask(to, description, context, priority) {
return this.callTool("assign_task", {
from: this.opts.agentId, to, description, context,
priority: priority ?? "normal",
});
}
/** 汇报任务进度 */
async updateTaskStatus(taskId, status, result, progress) {
return this.callTool("update_task_status", {
task_id: taskId, agent_id: this.opts.agentId, status, result, progress: progress ?? 0,
});
}
/** 查询任务状态 */
async getTaskStatus(taskId) {
return this.callTool("get_task_status", { task_id: taskId });
}
/** 查询在线 Agent */
async getOnlineAgents() {
const result = await this.callTool("get_online_agents", {});
return result?.online_agents ?? [];
}
/** 广播消息 */
async broadcast(agentIds, content, metadata) {
return this.callTool("broadcast_message", {
from: this.opts.agentId, agent_ids: agentIds, content, metadata,
});
}
// ═══════════════════════════════════════════════════════
// Evolution Engine — 经验共享 + 策略传播
// ═══════════════════════════════════════════════════════
/** 分享经验(直接 approved,无需审批) */
async shareExperience(title, content, tags, taskId) {
const args = { title, content };
if (tags)
args.tags = tags;
if (taskId)
args.task_id = taskId;
return this.callTool("share_experience", args);
}
/** 提议策略(需 admin 审批) */
async proposeStrategy(title, content, category = "workflow", taskId) {
const args = { title, content, category };
if (taskId)
args.task_id = taskId;
return this.callTool("propose_strategy", args);
}
/** 查询策略列表 */
async listStrategies(opts) {
const args = {};
if (opts?.status)
args.status = opts.status;
if (opts?.category)
args.category = opts.category;
if (opts?.proposerId)
args.proposer_id = opts.proposerId;
if (opts?.limit)
args.limit = opts.limit;
return this.callTool("list_strategies", args);
}
/** FTS5 全文搜索策略 */
async searchStrategies(query, opts) {
const args = { query };
if (opts?.category)
args.category = opts.category;
if (opts?.limit)
args.limit = opts.limit;
return this.callTool("search_strategies", args);
}
/** 采纳策略 */
async applyStrategy(strategyId, context) {
const args = { strategy_id: strategyId };
if (context)
args.context = context;
return this.callTool("apply_strategy", args);
}
/** 对策略反馈(每 Agent 每策略一次) */
async feedbackStrategy(strategyId, feedback, opts) {
const args = { strategy_id: strategyId, feedback };
if (opts?.comment)
args.comment = opts.comment;
if (opts?.applied !== undefined)
args.applied = opts.applied;
return this.callTool("feedback_strategy", args);
}
/** 审批策略(admin only) */
async approveStrategy(strategyId, action, reason) {
return this.callTool("approve_strategy", {
strategy_id: strategyId, action, reason,
});
}
/** 查看进化指标统计 */
async getEvolutionStatus() {
return this.callTool("get_evolution_status", {});
}
// ═══════════════════════════════════════════════════════
// 记忆模块 — Memory Service
// ═══════════════════════════════════════════════════════
/**
* 存储一条记忆
* @param content 记忆正文
* @param opts 可选字段:title / scope / tags / sourceTaskId
* @returns { memoryId: string }
*/
async storeMemory(content, opts) {
const args = { content };
if (opts?.title)
args.title = opts.title;
if (opts?.scope)
args.scope = opts.scope;
if (opts?.tags)
args.tags = opts.tags;
if (opts?.sourceTaskId)
args.source_task_id = opts.sourceTaskId;
return this.callTool("store_memory", args);
}
/**
* 语义搜索记忆(模糊查询)
* @param query 搜索关键词
* @param opts 可选字段:scope / limit
* @returns 匹配的记忆列表
*/
async recallMemory(query, opts) {
const args = { query };
if (opts?.scope)
args.scope = opts.scope;
if (opts?.limit)
args.limit = opts.limit;
return this.callTool("recall_memory", args);
}
/**
* 列出记忆(分页)
* @param opts 可选字段:scope / limit / offset
* @returns 记忆列表
*/
async listMemories(opts) {
const args = {};
if (opts?.scope)
args.scope = opts.scope;
if (opts?.limit)
args.limit = opts.limit;
if (opts?.offset)
args.offset = opts.offset;
return this.callTool("list_memories", args);
}
/**
* 删除指定记忆
* @param memoryId 记忆 ID
* @returns 删除结果
*/
async deleteMemory(memoryId) {
return this.callTool("delete_memory", { memory_id: memoryId });
}
// ═══════════════════════════════════════════════════════
// 任务模块补充 — Task Extensions
// ═══════════════════════════════════════════════════════
/**
* 通过 REST API 查询任务列表(支持过滤)
* @param status 状态过滤(如 "in_progress")
* @returns 任务列表
*/
async getTasks(status) {
const url = new URL(`${this.opts.hubUrl}/api/tasks`);
if (status)
url.searchParams.set("status", status);
const res = await fetch(url.toString(), {
headers: { Authorization: `Bearer ${this._apiToken}` },
});
if (!res.ok)
throw new Error(`getTasks failed: ${res.status} ${res.statusText}`);
return res.json();
}
/**
* 取消一个进行中的任务(MCP callTool)
* @param taskId 任务 ID
* @returns 取消结果
*/
async cancelTask(taskId) {
return this.callTool("cancel_task", { task_id: taskId });
}
// ═══════════════════════════════════════════════════════
// 依赖链 + 并行组 — Dependency Chain & Parallel Groups
// ═══════════════════════════════════════════════════════
/**
* 添加任务依赖关系
* @param upstreamId 上游任务 ID
* @param downstreamId 下游任务 ID
* @param depType 依赖类型,默认 "finish_to_start"
* @returns 添加结果
*/
async addDependency(upstreamId, downstreamId, depType) {
return this.callTool("add_dependency", {
upstream_task_id: upstreamId,
downstream_task_id: downstreamId,
dependency_type: depType ?? "finish_to_start",
});
}
/**
* 移除任务依赖关系
* @param upstreamId 上游任务 ID
* @param downstreamId 下游任务 ID
* @returns 移除结果
*/
async removeDependency(upstreamId, downstreamId) {
return this.callTool("remove_dependency", {
upstream_task_id: upstreamId,
downstream_task_id: downstreamId,
});
}
/**
* 查询指定任务的所有依赖关系
* @param taskId 任务 ID
* @returns 依赖关系列表
*/
async getTaskDependencies(taskId) {
return this.callTool("get_task_dependencies", { task_id: taskId });
}
/**
* 检查指定任务的所有上游依赖是否已满足(全部完成)
* @param taskId 任务 ID
* @returns { satisfied: boolean; missing: string[] }
*/
async checkDependenciesSatisfied(taskId) {
return this.callTool("check_dependencies_satisfied", { task_id: taskId });
}
/**
* 创建并行组(组内任务可同时执行)
* @param taskIds 任务 ID 列表
* @param groupName 可选的组名称
* @returns { groupId: string }
*/
async createParallelGroup(taskIds, groupName) {
const args = { task_ids: taskIds };
if (groupName)
args.group_name = groupName;
return this.callTool("create_parallel_group", args);
}
// ═══════════════════════════════════════════════════════
// 交接协议 — Handoff Protocol
// ═══════════════════════════════════════════════════════
/**
* 请求任务交接给另一个 Agent
* @param taskId 任务 ID
* @param targetAgentId 目标 Agent ID
* @returns 交接请求结果
*/
async requestHandoff(taskId, targetAgentId) {
return this.callTool("request_handoff", {
task_id: taskId,
target_agent_id: targetAgentId,
});
}
/**
* 接受任务交接
* @param taskId 任务 ID
* @returns 接受结果
*/
async acceptHandoff(taskId) {
return this.callTool("accept_handoff", { task_id: taskId });
}
/**
* 拒绝任务交接
* @param taskId 任务 ID
* @param reason 拒绝原因
* @returns 拒绝结果
*/
async rejectHandoff(taskId, reason) {
const args = { task_id: taskId };
if (reason)
args.reason = reason;
return this.callTool("reject_handoff", args);
}
// ═══════════════════════════════════════════════════════
// 质量门 — Quality Gates
// ═══════════════════════════════════════════════════════
/**
* 为 Pipeline 添加质量门
* @param pipelineId Pipeline ID
* @param gateName 质量门名称
* @param criteria 通过标准(SQL WHERE 条件或描述文本)
* @param afterOrder 在哪个步骤之后插入
* @returns 添加结果
*/
async addQualityGate(pipelineId, gateName, criteria, afterOrder) {
return this.callTool("add_quality_gate", {
pipeline_id: pipelineId,
gate_name: gateName,
criteria,
after_order: afterOrder,
});
}
/**
* 评估质量门结果
* @param gateId 质量门 ID
* @param status 评估结果 "passed" | "failed"
* @param result 可选的详细结果描述
* @returns 评估结果
*/
async evaluateQualityGate(gateId, status, result) {
const args = { gate_id: gateId, status };
if (result)
args.result = result;
return this.callTool("evaluate_quality_gate", args);
}
// ═══════════════════════════════════════════════════════
// 分级审批 — Tiered Strategy Approval
// ═══════════════════════════════════════════════════════
/**
* 提议策略(支持分级审批,自动根据策略内容判断 tier)
* @param title 策略标题
* @param content 策略正文
* @param opts 可选:category / taskId
* @returns 策略提案结果
*/
async proposeStrategyTiered(title, content, opts) {
const args = { title, content };
if (opts?.category)
args.category = opts.category;
if (opts?.taskId)
args.task_id = opts.taskId;
return this.callTool("propose_strategy_tiered", args);
}
/**
* 检查策略是否处于 veto 窗口期(可行使否决权的时间窗口)
* @param strategyId 策略 ID
* @returns { in_window: boolean; remaining_seconds?: number }
*/
async checkVetoWindow(strategyId) {
return this.callTool("check_veto_window", { strategy_id: strategyId });
}
/**
* 对策略行使否决权(需在 veto 窗口期内)
* @param strategyId 策略 ID
* @param reason 否决理由
* @returns 否决结果
*/
async vetoStrategy(strategyId, reason) {
return this.callTool("veto_strategy", {
strategy_id: strategyId,
reason,
});
}
// ═══════════════════════════════════════════════════════
// Phase 5a Security — RBAC + Trust Score
// ═══════════════════════════════════════════════════════
/**
* 设置 Agent 角色(admin only)
* @param agentId Agent ID
* @param role 新角色,如 "admin" / "member"
* @param managedGroupId 可选:管理的组 ID
* @returns 设置结果
*/
async setAgentRole(agentId, role, managedGroupId) {
const args = { agent_id: agentId, role };
if (managedGroupId)
args.managed_group_id = managedGroupId;
return this.callTool("set_agent_role", args);
}
/**
* 重新计算 Agent 信任评分(admin only)
* @param agentId 可选,不传则重算所有 Agent
* @returns 重算结果
*/
async recalculateTrustScores(agentId) {
const args = {};
if (agentId)
args.agent_id = agentId;
return this.callTool("recalculate_trust_scores", args);
}
// ═══════════════════════════════════════════════════════
// Token 管理 — Token Management
// ═══════════════════════════════════════════════════════
/** 设置 REST API 认证 token(register_agent 返回后调用) */
setToken(token) {
this._apiToken = token;
}
/** 撤销 Agent 的 API token(admin only) */
async revokeToken(agentId) {
return this.callTool("revoke_token", { agent_id: agentId });
}
/** 设置 Agent 信任评分(admin only) */
async setTrustScore(agentId, score) {
return this.callTool("set_trust_score", {
agent_id: agentId,
trust_score: score,
});
}
// ═══════════════════════════════════════════════════════
// 记忆搜索 — Memory Search (Phase 6)
// ═══════════════════════════════════════════════════════
/**
* 全文搜索记忆(FTS5)
* @param query 搜索关键词
* @param opts 可选:scope / limit
* @returns 匹配的记忆列表
*/
async searchMemories(query, opts) {
const args = { query };
if (opts?.scope)
args.scope = opts.scope;
if (opts?.limit)
args.limit = opts.limit;
return this.callTool("search_memories", args);
}
// ═══════════════════════════════════════════════════════
// Pipeline 管理 — Pipeline Management (Phase 6)
// ═══════════════════════════════════════════════════════
/**
* 创建 Pipeline(线性任务容器)
* @param name Pipeline 名称
* @param description 可选描述
* @returns { pipelineId: string }
*/
async createPipeline(name, description) {
const args = { name };
if (description)
args.description = description;
return this.callTool("create_pipeline", args);
}
/**
* 获取 Pipeline 详情(含任务列表和依赖关系)
* @param pipelineId Pipeline ID
* @returns Pipeline 详情
*/
async getPipeline(pipelineId) {
return this.callTool("get_pipeline", { pipeline_id: pipelineId });
}
/**
* 列出所有 Pipeline
* @param opts 可选:status / limit
* @returns Pipeline 列表
*/
async listPipelines(opts) {
const args = {};
if (opts?.status)
args.status = opts.status;
if (opts?.limit)
args.limit = opts.limit;
return this.callTool("list_pipelines", args);
}
/**
* 向 Pipeline 添加任务
* @param pipelineId Pipeline ID
* @param description 任务描述
* @param opts 可选:assignedTo / order / dependsOn
* @returns 添加结果
*/
async addTaskToPipeline(pipelineId, description, opts) {
const args = {
pipeline_id: pipelineId,
description,
};
if (opts?.assignedTo)
args.assigned_to = opts.assignedTo;
if (opts?.order !== undefined)
args.order = opts.order;
if (opts?.dependsOn)
args.depends_on = opts.dependsOn;
return this.callTool("add_task_to_pipeline", args);
}
// ═══════════════════════════════════════════════════════
// 消息搜索 — Message Search (Phase 6)
// ═══════════════════════════════════════════════════════
/**
* 全文搜索消息历史(FTS5)
* @param query 搜索关键词
* @param opts 可选:agentId(限定发送方)/ limit
* @returns 匹配的消息列表
*/
async searchMessages(query, opts) {
const args = { query };
if (opts?.agentId)
args.agent_id = opts.agentId;
if (opts?.limit)
args.limit = opts.limit;
return this.callTool("search_messages", args);
}
}
//# sourceMappingURL=agent-client.js.mapRelated skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| reverse-engineer-anythingmorluto/reaReverse engineer native, managed, Electron/JavaScript, packaged, firmware, and browser targets with REA, and build agent environments (AgentEnv bundles) of web applications from a live instance or a capture, and of Linux desktop apps with a task from a request. Use shipped-artifact or requested runtime evidence to explain features, compare versions, decompile code, or guide a reconstruction. Skip REA for ordinary source-repository architecture analysis. | Stars: 472k | Updated | Claude CodeCodex | Verified | Install |
| diagnosing-superpowersobra/superpowersUse when a superpowers session went wrong and your human partner wants to know why — repeated work, ignored plans, stumbles, poor results, a skill that didn't fire, "it took too long", "why is it so expensive", "what is it doing" — or wants to build a bug report for the superpowers maintainers, for the current session or a past one identified by id or path, on any harness. | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| dispatching-parallel-agentsobra/superpowersUse when facing 2+ independent tasks that can be worked on without shared state or sequential dependencies | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| executing-plansobra/superpowersUse when executing an implementation plan in the current session as the implementer yourself — your human partner chose inline execution, or no subagent tool is available | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |