UNPKG

contextual-agent-sdk

Version:

SDK for building AI agents with seamless voice-text context switching

141 lines 5.17 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ToolManager = void 0; class ToolManager { tools = new Map(); agentId; constructor(agentId) { this.agentId = agentId; } initializeTools(tools) { this.tools.clear(); for (const tool of tools) { this.tools.set(tool.id, tool); console.log(`[ToolManager] Registered tool: ${tool.id} (${tool.name})`); } console.log(`[ToolManager] Initialized with ${tools.length} tools`); } hasTool(toolId) { return this.tools.has(toolId); } getAvailableToolIds() { return Array.from(this.tools.keys()); } getToolInfo(toolId) { const tool = this.tools.get(toolId); if (!tool) return undefined; return { id: tool.id, name: tool.name, description: tool.description }; } async executeTool(toolId, parameters, context) { try { const tool = this.tools.get(toolId); if (!tool) { return { success: false, error: `Tool '${toolId}' not found. Available tools: ${this.getAvailableToolIds().join(', ')}`, metadata: { availableTools: this.getAvailableToolIds() } }; } const executionContext = { agentId: this.agentId, sessionId: context?.sessionId, userId: context?.userId, metadata: context }; console.log(`[ToolManager] Executing tool '${toolId}' with params:`, parameters); const result = await tool.execute(parameters, executionContext); console.log(`[ToolManager] Tool '${toolId}' result:`, { success: result.success, hasData: !!result.data, error: result.error }); return result; } catch (error) { console.error(`[ToolManager] Tool execution failed for '${toolId}':`, error); return { success: false, error: `Tool execution failed: ${error instanceof Error ? error.message : 'Unknown error'}`, metadata: { toolId, parameters } }; } } async detectAndExecuteTools(llmResponse, context) { const toolCalls = []; const toolResults = []; let enhancedResponse = llmResponse; try { const toolPattern = /\[TOOL:([^:]+):(\{[^}]+\})\]/g; let match; while ((match = toolPattern.exec(llmResponse)) !== null) { const [fullMatch, toolId, paramsJson] = match; try { const parameters = JSON.parse(paramsJson); const toolCall = { toolId, parameters }; toolCalls.push(toolCall); const result = await this.executeTool(toolId, parameters, context); toolResults.push(result); if (result.success && result.data) { const resultText = this.formatToolResult(toolCall, result); enhancedResponse = enhancedResponse.replace(fullMatch, resultText); } else { enhancedResponse = enhancedResponse.replace(fullMatch, `[Tool '${toolId}' failed: ${result.error}]`); } } catch (parseError) { console.error(`[ToolManager] Failed to parse tool parameters:`, parseError); enhancedResponse = enhancedResponse.replace(fullMatch, `[Invalid tool call format for '${toolId}']`); } } return { originalResponse: llmResponse, toolCalls, toolResults, enhancedResponse }; } catch (error) { console.error('[ToolManager] Tool detection failed:', error); return { originalResponse: llmResponse, toolCalls: [], toolResults: [], enhancedResponse: llmResponse }; } } formatToolResult(toolCall, result) { if (!result.success || !result.data) { return `[${toolCall.toolId} failed]`; } const data = result.data; if (data.message) { return `[${toolCall.toolId}: ${data.message}]`; } if (data.status) { return `[${toolCall.toolId}: ${data.status}]`; } if (data.result) { return `[${toolCall.toolId}: ${data.result}]`; } return `[${toolCall.toolId}: executed successfully]`; } getStats() { return { totalTools: this.tools.size, availableTools: this.getAvailableToolIds() }; } cleanup() { this.tools.clear(); console.log('[ToolManager] Cleanup completed'); } } exports.ToolManager = ToolManager; //# sourceMappingURL=ToolManager.js.map