UNPKG

agentis

Version:

A TypeScript framework for building sophisticated multi-agent systems

95 lines (94 loc) 3.75 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AgentRuntime = void 0; const ToolOrchestrator_1 = require("../tools/ToolOrchestrator"); const VectorMemoryClient_1 = require("../memory/VectorMemoryClient"); const events_1 = require("events"); const GoalPlanner_1 = require("../agents/GoalPlanner"); const Logger_1 = require("../logs/Logger"); class AgentRuntime extends events_1.EventEmitter { constructor() { super(); this.agents = new Map(); this.isRunning = false; this.conversationHistory = new Map(); this.goalPlanner = new GoalPlanner_1.GoalPlanner(); this.toolOrchestrator = new ToolOrchestrator_1.ToolOrchestrator(); this.memoryClient = new VectorMemoryClient_1.VectorMemoryClient(); } registerAgent(agent) { this.agents.set(agent.id, agent); } getAgent(id) { return this.agents.get(id); } async start() { this.isRunning = true; this.emit('runtime:started'); // Initialize all agents for (const agent of this.agents.values()) { await agent.initializeMemory(); } } async handleUserMessage(message) { // Get or initialize conversation history const conversationId = `conv-${message.sender_id}`; if (!this.conversationHistory.has(conversationId)) { this.conversationHistory.set(conversationId, []); } const history = this.conversationHistory.get(conversationId); try { // Analyze the request with GoalPlanner const analysis = await this.goalPlanner.analyzeRequest(message.content, history); await Logger_1.Logger.log('runtime', Logger_1.LogType.STATUS_UPDATE, { event: 'goal_analysis', analysis }); let response; if (analysis.requiresNewGoal) { // Route to the appropriate agent for new goal const targetAgent = this.selectAgentForMessage(message); if (!targetAgent) { throw new Error('No suitable agent found for the message'); } response = await targetAgent.receiveMessage(message); } else { // Use existing context for follow-up response = { id: `msg-${Date.now()}`, sender_id: message.recipient_id, recipient_id: message.sender_id, content: analysis.contextualResponse || 'I cannot provide a response at this time.', timestamp: Date.now() }; } // Update conversation history history.push(`User: ${message.content}`); history.push(`Agent: ${response.content}`); this.conversationHistory.set(conversationId, history); return response; } catch (error) { console.error('Error handling message:', error); return { id: `error-${Date.now()}`, sender_id: 'runtime', recipient_id: message.sender_id, content: 'I apologize, but I encountered an error processing your request.', timestamp: Date.now() }; } } selectAgentForMessage(message) { // For now, return the first available agent // TODO: Implement more sophisticated agent selection logic return Array.from(this.agents.values())[0]; } async stop() { this.isRunning = false; this.messageChannel?.unsubscribe(); this.emit('runtime:stopped'); } } exports.AgentRuntime = AgentRuntime;