111 lines
4.4 KiB
JavaScript
111 lines
4.4 KiB
JavaScript
/**
|
|
* Multi-Agent Swarm Simulation
|
|
*
|
|
* Tests concurrent access and coordination using agentic-flow
|
|
*/
|
|
import { createUnifiedDatabase } from '../../src/db-unified.js';
|
|
import { ReflexionMemory } from '../../src/controllers/ReflexionMemory.js';
|
|
import { SkillLibrary } from '../../src/controllers/SkillLibrary.js';
|
|
import { EmbeddingService } from '../../src/controllers/EmbeddingService.js';
|
|
import * as path from 'path';
|
|
export default {
|
|
description: 'Multi-agent swarm with concurrent database access',
|
|
async run(config) {
|
|
const { verbosity = 2, size = 5, parallel = true } = config;
|
|
if (verbosity >= 2) {
|
|
console.log(` 🤖 Initializing ${size}-Agent Swarm Simulation`);
|
|
}
|
|
// Initialize shared AgentDB
|
|
const embedder = new EmbeddingService({
|
|
model: 'Xenova/all-MiniLM-L6-v2',
|
|
dimension: 384,
|
|
provider: 'transformers'
|
|
});
|
|
await embedder.initialize();
|
|
const db = await createUnifiedDatabase(path.join(process.cwd(), 'simulation', 'data', 'swarm.graph'), embedder, { forceMode: 'graph' });
|
|
const results = {
|
|
agents: size,
|
|
operations: 0,
|
|
conflicts: 0,
|
|
avgLatency: 0,
|
|
totalTime: 0
|
|
};
|
|
const startTime = performance.now();
|
|
// Simulate agent tasks
|
|
const agentTask = async (agentId) => {
|
|
const reflexion = new ReflexionMemory(db.getGraphDatabase(), embedder, undefined, undefined, db.getGraphDatabase());
|
|
const skills = new SkillLibrary(db.getGraphDatabase(), embedder, undefined, // vectorBackend
|
|
db.getGraphDatabase() // graphBackend
|
|
);
|
|
const taskStart = performance.now();
|
|
try {
|
|
// Each agent stores episodes
|
|
await reflexion.storeEpisode({
|
|
sessionId: `agent-${agentId}`,
|
|
task: `agent ${agentId} completing task`,
|
|
reward: 0.8 + (Math.random() * 0.2),
|
|
success: true
|
|
});
|
|
// Each agent creates skills
|
|
await skills.createSkill({
|
|
name: `agent-${agentId}-skill`,
|
|
description: `Skill created by agent ${agentId}`,
|
|
code: `function agent${agentId}() { return true; }`,
|
|
successRate: 0.9
|
|
});
|
|
// Each agent searches
|
|
await reflexion.retrieveRelevant({
|
|
task: 'completing task',
|
|
k: 5
|
|
});
|
|
const taskEnd = performance.now();
|
|
results.operations += 3; // store + create + retrieve
|
|
return {
|
|
agentId,
|
|
duration: taskEnd - taskStart,
|
|
success: true
|
|
};
|
|
}
|
|
catch (error) {
|
|
results.conflicts++;
|
|
return {
|
|
agentId,
|
|
duration: performance.now() - taskStart,
|
|
success: false,
|
|
error
|
|
};
|
|
}
|
|
};
|
|
// Execute agent tasks
|
|
let taskResults;
|
|
if (parallel) {
|
|
// Parallel execution
|
|
taskResults = await Promise.all(Array.from({ length: size }, (_, i) => agentTask(i)));
|
|
}
|
|
else {
|
|
// Sequential execution
|
|
taskResults = [];
|
|
for (let i = 0; i < size; i++) {
|
|
taskResults.push(await agentTask(i));
|
|
}
|
|
}
|
|
const endTime = performance.now();
|
|
results.totalTime = endTime - startTime;
|
|
// Calculate metrics
|
|
const successfulTasks = taskResults.filter(r => r.success);
|
|
results.avgLatency = successfulTasks.reduce((sum, r) => sum + r.duration, 0) / successfulTasks.length;
|
|
db.close();
|
|
if (verbosity >= 2) {
|
|
console.log(` 📊 Agents: ${results.agents}`);
|
|
console.log(` 📊 Operations: ${results.operations}`);
|
|
console.log(` 📊 Conflicts: ${results.conflicts}`);
|
|
console.log(` 📊 Avg Agent Latency: ${results.avgLatency.toFixed(2)}ms`);
|
|
console.log(` ⏱️ Total Duration: ${results.totalTime.toFixed(2)}ms`);
|
|
}
|
|
if (verbosity >= 3) {
|
|
console.log(` 📋 Agent Results:`, taskResults);
|
|
}
|
|
return results;
|
|
}
|
|
};
|
|
//# sourceMappingURL=multi-agent-swarm.js.map
|