-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy path32-skills-multi-agent.ts
More file actions
311 lines (284 loc) · 12.2 KB
/
Copy path32-skills-multi-agent.ts
File metadata and controls
311 lines (284 loc) · 12.2 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/**
* Skills — Multi-agent workflows with skills as sub-agents.
*
* Demonstrates:
* - Skills as sub-agents in router, sequential, and parallel teams
* - Mixing skill-based agents with regular tool agents
* - Skills composed via agentTool() on an orchestrator
* - Skills in a pipeline with .pipe()
* - Full visibility: each skill sub-agent is a real Conductor SUB_WORKFLOW
*
* Requirements:
* - Conductor server with LLM support
* - CONDUCTOR_SERVER_URL=http://localhost:8080/api
* - /dg skill installed (https://github.com/v1r3n/dinesh-gilfoyle)
* - conductor skill installed (https://github.com/conductor-oss/conductor-skills)
*/
import { Agent, AgentRuntime, OnTextMention, agentTool, skill, tool } from '@io-orkes/conductor-javascript/agents';
import { llmModel, secondaryLlmModel } from './settings';
// ── Load skills ────────────────────────────────────────────────────
const dg = skill('~/.claude/skills/dg', { model: llmModel });
const conductorSkill = skill('~/.claude/skills/conductor', {
model: secondaryLlmModel,
});
// ── Shared tools ───────────────────────────────────────────────────
const runTests = tool(
async (args: { code: string }) => {
if (!args.code) {
return { result: 'ERROR: no code provided to test' };
}
if (args.code.includes('SELECT *') && args.code.includes("f'")) {
return { result: 'FAIL: test_sql_injection detected SQL injection vulnerability' };
}
if (args.code.includes('subprocess') && args.code.includes('shell=True')) {
return { result: 'FAIL: test_command_injection detected command injection' };
}
return { result: 'PASS: all tests passed' };
},
{
name: 'run_tests',
description: 'Run unit tests on the provided code (simulated).',
inputSchema: {
type: 'object',
properties: {
code: { type: 'string', description: 'Code to test' },
},
required: ['code'],
},
},
);
// ═══════════════════════════════════════════════════════════════════
// Example 1: Router — DevOps team routes to the right specialist
// ═══════════════════════════════════════════════════════════════════
async function exampleRouter() {
const coder = new Agent({
name: 'coder',
model: llmModel,
instructions:
'You are a senior developer. Write clean, production-ready code. ' +
'Always include error handling and type annotations.',
});
const devopsTeam = new Agent({
name: 'devops_team',
model: llmModel,
agents: [dg, coder, conductorSkill],
strategy: 'router',
router: new Agent({
name: 'router',
model: llmModel,
instructions:
'Route tasks to the right specialist:\n' +
'- Code review, PR review → dg\n' +
'- Writing code, fixing bugs → coder\n' +
'- Workflow orchestration → conductor',
}),
});
const runtime = new AgentRuntime();
try {
console.log('=== Example 1: Router Team ===\n');
const result = await runtime.run(
devopsTeam,
"Review this function for security issues:\n\n" +
"def login(username, password):\n" +
" query = f\"SELECT * FROM users WHERE user='{username}' AND pass='{password}'\"\n" +
' return db.execute(query)\n',
);
console.log(`Execution ID: ${result.executionId}`);
console.log(`Status: ${result.status}`);
console.log(`Tokens: ${JSON.stringify(result.tokenUsage)}`);
result.printResult();
} finally {
await runtime.shutdown();
}
}
// ═══════════════════════════════════════════════════════════════════
// Example 2: Sequential Pipeline — Review → Fix → Deploy
// ═══════════════════════════════════════════════════════════════════
async function examplePipeline() {
const fixer = new Agent({
name: 'fixer',
model: secondaryLlmModel,
instructions:
'You receive a code review with findings. For each critical finding, ' +
'rewrite the code with the fix applied. Include comments explaining each fix.',
});
const deployer = new Agent({
name: 'deployer',
model: llmModel,
instructions:
'You receive fixed code. Create a Conductor workflow definition that ' +
'uses a SIMPLE task to run this code as a worker. Output the JSON.',
});
const reviewFixDeploy = dg.pipe(fixer).pipe(deployer);
const runtime = new AgentRuntime();
try {
console.log('=== Example 2: Review → Fix → Deploy Pipeline ===\n');
const result = await runtime.run(
reviewFixDeploy,
"Review, fix, and deploy:\n\n" +
"def process_payment(amount, card_number):\n" +
" log.info(f'Processing {card_number} for ${amount}')\n" +
' if amount > 0:\n' +
' return charge_card(card_number, amount)\n' +
" return {'error': 'invalid amount'}\n",
);
console.log(`Execution ID: ${result.executionId}`);
console.log(`Status: ${result.status}`);
console.log(`Tokens: ${JSON.stringify(result.tokenUsage)}`);
result.printResult();
} finally {
await runtime.shutdown();
}
}
// ═══════════════════════════════════════════════════════════════════
// Example 3: Parallel — Multiple reviewers simultaneously
// ═══════════════════════════════════════════════════════════════════
async function exampleParallel() {
const securityReviewer = new Agent({
name: 'security_reviewer',
model: llmModel,
instructions:
'Review code ONLY for security: injection, credentials, auth gaps, OWASP Top 10.',
});
const performanceReviewer = new Agent({
name: 'performance_reviewer',
model: llmModel,
instructions:
'Review code ONLY for performance: O(n²), missing caching, N+1 queries, blocking calls.',
});
const parallelReview = new Agent({
name: 'parallel_review',
model: llmModel,
agents: [dg, securityReviewer, performanceReviewer],
strategy: 'parallel',
instructions:
'Run all three reviewers in parallel. Aggregate findings into a unified report.',
});
const runtime = new AgentRuntime();
try {
console.log('=== Example 3: Parallel Review ===\n');
const result = await runtime.run(
parallelReview,
"Review this endpoint:\n\n" +
'from flask import request\n' +
'import subprocess\n\n' +
"@app.route('/run')\n" +
'def execute():\n' +
" cmd = request.args.get('cmd')\n" +
' output = subprocess.check_output(cmd, shell=True)\n' +
' return output.decode()\n',
);
console.log(`Execution ID: ${result.executionId}`);
console.log(`Status: ${result.status}`);
console.log(`Tokens: ${JSON.stringify(result.tokenUsage)}`);
result.printResult();
} finally {
await runtime.shutdown();
}
}
// ═══════════════════════════════════════════════════════════════════
// Example 4: Skills as tools on an orchestrator
// ═══════════════════════════════════════════════════════════════════
async function exampleOrchestrator() {
const techLead = new Agent({
name: 'tech_lead',
model: llmModel,
instructions:
'You are a tech lead managing a review and deployment pipeline.\n\n' +
'1. Run code review using dg tool\n' +
'2. If critical issues → stop and report\n' +
'3. If passes → run tests\n' +
'4. If tests pass → use conductor to create deployment workflow\n' +
'5. Summarize the full pipeline result',
tools: [
agentTool(dg, { description: 'Run adversarial Dinesh vs Gilfoyle code review' }),
agentTool(conductorSkill, { description: 'Create and manage Conductor workflows' }),
runTests,
],
});
const runtime = new AgentRuntime();
try {
console.log('=== Example 4: Tech Lead Orchestrator ===\n');
const result = await runtime.run(
techLead,
'Review and deploy this worker:\n\n' +
'def enrich_customer(task):\n' +
" customer_id = task.input_data['customer_id']\n" +
' profile = fetch_profile(customer_id)\n' +
' enriched = {\n' +
" 'name': profile['name'],\n" +
" 'segment': classify_segment(profile),\n" +
" 'ltv': calculate_ltv(profile['orders']),\n" +
' }\n' +
" return {'status': 'COMPLETED', 'output': enriched}\n",
);
console.log(`Execution ID: ${result.executionId}`);
console.log(`Status: ${result.status}`);
console.log(`Tokens: ${JSON.stringify(result.tokenUsage)}`);
result.printResult();
} finally {
await runtime.shutdown();
}
}
// ═══════════════════════════════════════════════════════════════════
// Example 5: Swarm — Agents hand off to each other
// ═══════════════════════════════════════════════════════════════════
async function exampleSwarm() {
const architect = new Agent({
name: 'architect',
model: secondaryLlmModel,
instructions:
'You are a software architect. Design the system for the given requirements. ' +
'When ready, say HANDOFF_TO_DG for review. If review finds issues, redesign.',
});
const swarmTeam = new Agent({
name: 'design_review_loop',
model: llmModel,
agents: [architect, dg],
strategy: 'swarm',
handoffs: [
new OnTextMention({ text: 'HANDOFF_TO_DG', target: 'dg' }),
new OnTextMention({ text: 'HANDOFF_TO_ARCHITECT', target: 'architect' }),
],
});
const runtime = new AgentRuntime();
try {
console.log('=== Example 5: Architect ↔ /dg Swarm ===\n');
const result = await runtime.run(
swarmTeam,
'Design a rate limiter service with:\n' +
'- Fixed window and sliding window algorithms\n' +
'- Redis backend for distributed state\n' +
'- REST API for configuration\n' +
'- Middleware for Express.js',
);
console.log(`Execution ID: ${result.executionId}`);
console.log(`Status: ${result.status}`);
console.log(`Tokens: ${JSON.stringify(result.tokenUsage)}`);
result.printResult();
} finally {
await runtime.shutdown();
}
}
// ── Main ───────────────────────────────────────────────────────────
async function main() {
const choice = process.argv[2] ?? 'router';
const examples: Record<string, () => Promise<void>> = {
router: exampleRouter,
pipeline: examplePipeline,
parallel: exampleParallel,
orchestrator: exampleOrchestrator,
swarm: exampleSwarm,
};
if (choice === 'all') {
for (const [name, fn] of Object.entries(examples)) {
console.log(`\n${'='.repeat(60)}`);
await fn();
}
} else if (choice in examples) {
await examples[choice]();
} else {
console.log(`Usage: npx tsx ${process.argv[1]} [${Object.keys(examples).join('/')}/all]`);
}
}
main().catch(console.error);