-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.js
More file actions
230 lines (186 loc) · 6.52 KB
/
example.js
File metadata and controls
230 lines (186 loc) · 6.52 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
/**
* Example: Using the Multi-Agent Framework
*
* This example demonstrates how to use the framework programmatically
* for custom workflows and agent interactions.
*/
import agentCore, { EventTypes } from './agent-core.js';
import { PlannerAgent } from './agent-planner.js';
import { CoderAgent } from './agent-coder.js';
import { TesterAgent } from './agent-tester.js';
import { SupervisorAgent, VERIFICATION_TYPES } from './agent-supervisor.js';
/**
* Example 1: Basic Agent Registration and State Management
*/
async function basicExample() {
console.log('=== Basic Agent Registration ===\n');
// Reset for clean state
agentCore.reset();
// Register a custom agent
const customAgent = agentCore.registerAgent('my-agent', {
model: 'sonnet',
state: { counter: 0 },
subscribesTo: ['planner'],
tools: [{ name: 'myCustomTool', params: [] }]
});
console.log('Registered agent:', customAgent.name);
console.log('Model:', customAgent.model);
// Update state
agentCore.updateAgentState('my-agent', { counter: 1 });
console.log('Updated state:', agentCore.getAgentState('my-agent'));
// Add a goal
const goal = agentCore.setGoal('my-agent', {
description: 'Complete the example',
metadata: { priority: 'high' }
});
console.log('Set goal:', goal.description);
// Add a task
const task = agentCore.addTask('my-agent', {
description: 'First task',
parentGoalId: goal.id,
metadata: { complexity: 'simple' }
});
console.log('Added task:', task.description);
// Add memory
agentCore.addMemory('my-agent', {
content: 'Important observation',
type: 'observation'
});
console.log('\nSummary:', agentCore.getSummary());
}
/**
* Example 2: Event Subscriptions
*/
async function eventExample() {
console.log('\n=== Event Subscriptions ===\n');
agentCore.reset();
// Register agents
agentCore.registerAgent('publisher');
agentCore.registerAgent('subscriber');
// Set up event listener
const events = [];
agentCore.subscribeToAgents('subscriber', ['publisher'], (event) => {
events.push(event);
console.log(`Event received: ${event.type} from ${event.source}`);
});
// Trigger events
agentCore.updateAgentState('publisher', { status: 'working' });
agentCore.addTask('publisher', 'Do something');
agentCore.addMemory('publisher', 'Remember this');
console.log(`\nTotal events captured: ${events.length}`);
}
/**
* Example 3: Snapshotting and Resume
*/
async function snapshotExample() {
console.log('\n=== Snapshotting ===\n');
agentCore.reset();
// Set up some state
agentCore.registerAgent('snapshot-demo', { state: { data: 'important' } });
agentCore.startWorkflow('demo-workflow', 'Demo goal');
agentCore.addTask('snapshot-demo', 'Task to preserve');
// Save snapshot
const saved = agentCore.snapshot();
console.log('Snapshot saved at:', new Date(saved.timestamp).toISOString());
// Check if resume is possible
console.log('Can resume:', agentCore.canResume());
// Reset and resume
agentCore.reset();
console.log('After reset, agents:', Object.keys(agentCore.agents));
const loaded = agentCore.resume();
console.log('After resume, agents:', Object.keys(agentCore.agents));
console.log('Preserved state:', agentCore.getAgentState('snapshot-demo'));
}
/**
* Example 4: Using the Pre-built Agents
*/
async function agentWrapperExample() {
console.log('\n=== Pre-built Agents ===\n');
agentCore.reset();
// Initialize agents
const planner = new PlannerAgent({ model: 'sonnet' });
const coder = new CoderAgent({ model: 'opus' });
const tester = new TesterAgent({ model: 'opus' });
const supervisor = new SupervisorAgent({ model: 'opus' });
console.log('Initialized agents:');
console.log('- Planner:', planner.name, '(model:', planner.model + ')');
console.log('- Coder:', coder.name, '(model:', coder.model + ')');
console.log('- Tester:', tester.name, '(model:', tester.model + ')');
console.log('- Supervisor:', supervisor.name, '(model:', supervisor.model + ')');
// Get stats
console.log('\nPlanner stats:', planner.getStats());
console.log('Coder stats:', coder.getStats());
console.log('Tester stats:', tester.getStats());
console.log('Supervisor stats:', supervisor.getStats());
}
/**
* Example 5: Workflow Configuration
*/
async function configurationExample() {
console.log('\n=== Workflow Configuration ===\n');
agentCore.reset();
// Load configuration
const config = agentCore.loadConfiguration();
if (config) {
const workflow = config['default-workflow'];
console.log('Workflow name:', workflow.name);
console.log('Agents configured:', Object.keys(workflow.agents));
console.log('Execution phases:', workflow.execution.phases);
console.log('Time limit:', workflow.execution.timeLimit / 1000 / 60, 'minutes');
} else {
console.log('No configuration file found. Creating default...');
// Save default config
agentCore.saveConfiguration({
'default-workflow': {
name: 'Custom Workflow',
agents: {
planner: { model: 'sonnet' },
coder: { model: 'opus' }
}
}
});
console.log('Configuration saved.');
}
}
/**
* Example 6: Inter-Agent Communication
*/
async function communicationExample() {
console.log('\n=== Inter-Agent Communication ===\n');
agentCore.reset();
// Register agents
agentCore.registerAgent('sender');
agentCore.registerAgent('receiver');
// Log an interaction
const interaction = agentCore.logInteraction('sender', 'receiver', {
type: 'request',
content: { action: 'verify', data: { task: 'implementation' } },
toolCalls: [{ name: 'verificationComplete', arguments: { approved: true } }]
});
console.log('Interaction logged:', interaction.id);
console.log('From:', interaction.from, 'To:', interaction.to);
console.log('Content:', JSON.stringify(interaction.content, null, 2));
// Check both agents have the interaction
const sender = agentCore.getAgent('sender');
const receiver = agentCore.getAgent('receiver');
console.log('\nSender interactions:', sender.interactions.length);
console.log('Receiver interactions:', receiver.interactions.length);
}
/**
* Run all examples
*/
async function runExamples() {
try {
await basicExample();
await eventExample();
await snapshotExample();
await agentWrapperExample();
await configurationExample();
await communicationExample();
console.log('\n=== All Examples Complete ===\n');
} catch (error) {
console.error('Example error:', error);
}
}
// Run if executed directly
runExamples();