agentis
Version:
A TypeScript framework for building sophisticated multi-agent systems
57 lines (56 loc) • 2.36 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ToolOrchestrator = void 0;
const Logger_1 = require("../logs/Logger");
class ToolOrchestrator {
constructor() {
this.tools = new Map();
this.executionHistory = new Map();
}
registerTool(tool) {
this.tools.set(tool.name, tool);
}
async executePlan(plans, agentId) {
const results = new Map();
const pendingPlans = [...plans].sort((a, b) => a.priority - b.priority);
while (pendingPlans.length > 0) {
const executable = pendingPlans.filter(plan => !plan.dependsOn || plan.dependsOn.every(dep => results.has(dep)));
if (executable.length === 0) {
throw new Error('Circular dependency detected in tool execution plan');
}
await Promise.all(executable.map(async (plan) => {
const tool = this.tools.get(plan.toolName);
if (!tool) {
throw new Error(`Tool ${plan.toolName} not found`);
}
try {
const result = await tool.execute(plan.input);
results.set(plan.toolName, result);
this.executionHistory.set(plan.toolName, result);
await Logger_1.Logger.log(agentId, Logger_1.LogType.TOOL_CALL, {
tool: plan.toolName,
input: plan.input,
result
});
}
catch (error) {
const toolError = {
message: error instanceof Error ? error.message : 'Unknown error occurred',
details: error
};
await Logger_1.Logger.log(agentId, Logger_1.LogType.ERROR, {
tool: plan.toolName,
error: toolError
});
throw new Error(`Tool execution failed: ${toolError.message}`);
}
}));
executable.forEach(executed => {
const index = pendingPlans.findIndex(p => p.toolName === executed.toolName);
pendingPlans.splice(index, 1);
});
}
return results;
}
}
exports.ToolOrchestrator = ToolOrchestrator;