-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy path03-multi-agent.ts
More file actions
116 lines (96 loc) · 3.33 KB
/
Copy path03-multi-agent.ts
File metadata and controls
116 lines (96 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/**
* 03 - Multi-Agent
*
* Demonstrates three orchestration strategies:
* 1. Sequential (.pipe()) — agents run in order
* 2. Parallel — agents run concurrently
* 3. Handoff — agents delegate to sub-agents
*/
import {
Agent,
AgentRuntime,
OnTextMention,
} from '@io-orkes/conductor-javascript/agents';
const MODEL = process.env.AGENTSPAN_LLM_MODEL ?? 'openai/gpt-4o';
// ── Sequential: writer -> editor ─────────────────────────
export const writer = new Agent({
name: 'writer',
model: MODEL,
instructions: 'Write a short paragraph about the given topic.',
});
export const editor = new Agent({
name: 'editor',
model: MODEL,
instructions: 'Edit the text for clarity and brevity.',
});
// .pipe() creates a sequential pipeline
const writingPipeline = writer.pipe(editor);
// ── Parallel: multiple researchers ───────────────────────
export const webResearcher = new Agent({
name: 'web_researcher',
model: MODEL,
instructions: 'Research the topic from web sources.',
});
export const dataAnalyst = new Agent({
name: 'data_analyst',
model: MODEL,
instructions: 'Analyze data trends related to the topic.',
});
export const researchTeam = new Agent({
name: 'research_team',
agents: [webResearcher, dataAnalyst],
strategy: 'parallel',
});
// ── Handoff: router delegates to specialists ────────────
export const pythonExpert = new Agent({
name: 'python_expert',
model: MODEL,
instructions: 'Answer Python programming questions.',
introduction: 'I specialize in Python.',
});
export const jsExpert = new Agent({
name: 'js_expert',
model: MODEL,
instructions: 'Answer JavaScript programming questions.',
introduction: 'I specialize in JavaScript.',
});
export const codingTeam = new Agent({
name: 'coding_team',
model: MODEL,
instructions: 'Route to the appropriate language expert.',
agents: [pythonExpert, jsExpert],
strategy: 'swarm',
handoffs: [
new OnTextMention({ target: 'python_expert', text: 'Python' }),
new OnTextMention({ target: 'js_expert', text: 'JavaScript' }),
],
});
// ── Run examples ─────────────────────────────────────────
async function main() {
const runtime = new AgentRuntime();
try {
console.log('=== Sequential Pipeline ===');
const seqResult = await runtime.run(writingPipeline, 'Quantum computing');
seqResult.printResult();
console.log('\n=== Parallel Research ===');
const parResult = await runtime.run(researchTeam, 'AI trends in 2026');
parResult.printResult();
console.log('\n=== Handoff Team ===');
const handoffResult = await runtime.run(
codingTeam,
'How do I use async/await in Python?',
);
handoffResult.printResult();
// Production pattern:
// 1. Deploy once during CI/CD (optional -- serve() below also deploys):
// await runtime.deploy(writingPipeline);
// CLI alternative:
// agentspan deploy --package sdk/typescript/examples --agents writer
//
// 2. In a separate long-lived worker process (deploys + registers workers + starts polling):
// await runtime.serve(writingPipeline);
} finally {
await runtime.shutdown();
}
}
main().catch(console.error);