UNPKG

@paybyrd/ai-agent-openai

Version:

OpenAI integration for AI agent toolkit

38 lines (37 loc) 1.5 kB
import { PaybyrdAPI, tools, isToolAllowed } from '@paybyrd/ai-agent-toolkit'; import { zodToJsonSchema } from 'zod-to-json-schema'; class OpenAIAgentToolkit { constructor({ authToken, configuration, }) { this._paybyrd = new PaybyrdAPI(authToken, configuration.context); const filteredTools = tools.filter((tool) => isToolAllowed(tool, configuration)); this.tools = filteredTools.map((tool) => ({ type: 'function', function: { name: tool.method, description: tool.description, parameters: zodToJsonSchema(tool.parameters), }, })); } getTools() { return this.tools; } /** * Processes a single OpenAI tool call by executing the requested function. * * @param {ChatCompletionMessageToolCall} toolCall - The tool call object from OpenAI containing * function name, arguments, and ID. * @returns {Promise<ChatCompletionToolMessageParam>} A promise that resolves to a tool message * object containing the result of the tool execution with the proper format for the OpenAI API. */ async handleToolCall(toolCall) { const args = JSON.parse(toolCall.function.arguments); const response = await this._paybyrd.run(toolCall.function.name, args); return { role: 'tool', tool_call_id: toolCall.id, content: response, }; } } export default OpenAIAgentToolkit;