详细分析 ▾
运行时依赖
版本
- Multi-Agent Orchestrator 技能首次发布 - 设计并管理多智能体协作、任务路由与状态管理 - 包含智能体基类、任务分解与调度编排器、消息总线及智能体状态管理器 - 支持多种通信模式(广播、点对点、发布/订阅、黑板)与编排模式(角色扮演、辩论) - 提供智能体注册、并发执行、依赖管理与通信机制
安装命令
点击复制技能文档
功能说明
设计和管理多Agent协作系统。架构模式
``
┌─────────────┐
│ Orchestrator │ ← 任务分解、协调
└──────┬──────┘
│
┌───┼───┐
▼ ▼ ▼
┌───┐┌───┐┌───┐
│ A ││ B ││ C │ ← 专业Agent
└───┘└───┘└───┘
` 核心实现
1. Agent基类
`typescript
interface AgentConfig {
name: string;
role: string;
capabilities: string[];
llm: LLMConfig;
tools: Tool[];
instructions: string;
} class BaseAgent {
protected config: AgentConfig;
protected memory: AgentMemory;
constructor(config: AgentConfig) {
this.config = config;
this.memory = new AgentMemory(config.name);
}
async think(task: Task): Promise {
const context = await this.memory.buildContext(task.description);
const prompt = this.buildPrompt(task, context);
const response = await this.callLLM(prompt);
await this.memory.add({
type: 'semantic',
content: task.description + ' -> ' + response.content,
importance: 8
});
return response;
}
protected buildPrompt(task: Task, context: string): Message[] {
return [
{ role: 'system', content: this.config.instructions },
{ role: 'system', content: context },
{ role: 'user', content: task.description }
];
}
protected async callLLM(messages: Message[]): Promise {
const res = await fetch('https://api.openai.com/v1/chat/completions', {
method: 'POST',
headers: { 'Authorization': Bearer ${process.env.OPENAI_API_KEY} },
body: JSON.stringify({
model: this.config.llm.model,
messages,
tools: this.config.tools.map(t => t.definition)
})
});
return res.json();
}
}
`
2. 编排器
`typescript
interface TaskResult {
agentId: string;
status: 'pending' | 'running' | 'done' | 'failed';
output?: string;
dependencies: string[];
startTime?: number;
endTime?: number;
} class Orchestrator {
private agents: Map = new Map();
private taskGraph: DAG;
constructor(private llmRouter: LLMRouter) {}
registerAgent(agent: BaseAgent) {
this.agents.set(agent.config.name, agent);
}
async execute(goal: string): Promise {
// 1. 任务分解
const plan = await this.decompose(goal);
// 2. 构建DAG
this.taskGraph = this.buildDAG(plan);
// 3. 执行调度
const results = await this.schedule();
// 4. 汇总结果
return this.summarize(goal, results);
}
private async decompose(goal: string): Promise {
const response = await this.llmRouter.route({
prompt: 将以下任务分解为可执行的子任务,返回JSON数组:
目标: ${goal}
要求:
- 每个子任务只由一个Agent负责
- 明确任务依赖关系
- 返回格式: [{"id":"t1","description":"...","agent":"researcher","depends":[]},...]`,
private async schedule(): Promise> { const results = new Map(); const pending = new Set(this.taskGraph.nodes); const running: Promise[] = []; const maxConcurrent = 3;
while (pending.size > 0 || running.length > 0) { // 启动可并行的任务 while (running.length < maxConcurrent) { const next = this.findNextRunnable(pending, results); if (!next) break; pending.delete(next.id); const p = this.runTask(next, results).catch(console.error); running.push(p); } // 等待一个完成 await Promise.race(running); running.splice(running.findIndex(p => false), 1); } return results; }
private async runTask(task: Task, results: Map) { results.set(task.id, { agentId: task.agent, status: 'running', dependencies: task.depends || [], startTime: Date.now() }); try { // 等待依赖完成 for (const depId of task.depends || []) { const dep = results.get(depId); if (dep?.status !== 'done') { await this.waitFor(depId, results); } } const agent = this.agents.get(task.agent); const context = this.buildContext(task, results); const response = await agent.think({ id: task.id, description: task.description, context }); results.set(task.id, { ...results.get(task.id)!, status: 'done', output: response.content, endTime: Date.now() }); } catch (error) { results.set(task.id, { ...results.get(task.id)!, status: 'failed', output: String(error), endTime: Date.now() }); } }
private buildContext(task: Task, results: Map): string { return (task.depends || []).map(depId => { const dep = results.get(d