UNPKG

contextual-agent-sdk

Version:

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

130 lines 4.06 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ToolConverter = void 0; class ToolConverter { static toOpenAIFunction(tool) { return { type: 'function', function: { name: tool.id, description: tool.description, parameters: { type: 'object', properties: this.inferParametersFromTool(tool), required: [] } } }; } static toAnthropicTool(tool) { return { name: tool.id, description: tool.description, input_schema: { type: 'object', properties: this.inferParametersFromTool(tool), required: [] } }; } static toOpenAIFunctions(tools) { return tools.map(tool => this.toOpenAIFunction(tool)); } static toAnthropicTools(tools) { return tools.map(tool => this.toAnthropicTool(tool)); } static async executeToolCall(toolCall, tools, context) { const tool = tools.find(t => t.id === toolCall.function.name); if (!tool) { return { success: false, error: `Tool ${toolCall.function.name} not found`, metadata: { toolCallId: toolCall.id } }; } try { const params = JSON.parse(toolCall.function.arguments); const result = await tool.execute(params, context); return { ...result, metadata: { ...result.metadata, toolCallId: toolCall.id, toolName: tool.id } }; } catch (error) { return { success: false, error: `Tool execution failed: ${error.message}`, metadata: { toolCallId: toolCall.id, toolName: tool.id, originalError: error.message } }; } } static async executeToolCalls(toolCalls, tools, context) { const promises = toolCalls.map(toolCall => this.executeToolCall(toolCall, tools, context)); return Promise.all(promises); } static inferParametersFromTool(tool) { if (tool.id.includes('sms') || tool.id.includes('twilio')) { return { to: { type: 'string', description: 'Phone number to send SMS to (E.164 format, e.g., +1234567890)' }, message: { type: 'string', description: 'The message content to send' } }; } if (tool.id.includes('email')) { return { to: { type: 'string', description: 'Email address to send to' }, subject: { type: 'string', description: 'Email subject line' }, body: { type: 'string', description: 'Email body content' } }; } return { input: { type: 'string', description: 'Input parameter for the tool' } }; } static formatToolResultForLLM(result) { if (result.success) { if (typeof result.data === 'string') { return result.data; } return JSON.stringify(result.data); } else { return `Error: ${result.error}`; } } static createToolContext(agentId, sessionId, userId, metadata) { return { agentId, sessionId, userId, metadata }; } } exports.ToolConverter = ToolConverter; //# sourceMappingURL=ToolConverter.js.map