Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 2 | import { OpenAIProvider } from '../../src/provider/OpenAIProvider'; |
| 3 | import { Tool } from '../../src/types/tool.types'; |
| 4 | import { OpenAI } from 'openai'; |
| 5 | import { toolMocks } from '../utils/mocks/data.mock'; |
| 6 | import { ExecuteToolModifiers } from '../../src/types/modifiers.types'; |
| 7 | |
| 8 | // Create mocks |
| 9 | vi.mock('openai', () => { |
| 10 | return { |
| 11 | OpenAI: vi.fn(), |
| 12 | }; |
| 13 | }); |
| 14 | |
| 15 | describe('OpenAIProvider', () => { |
| 16 | let provider: OpenAIProvider; |
| 17 | let mockTool: Tool; |
| 18 | let mockExecuteToolFn: unknown; |
| 19 | |
| 20 | // Sample tool data |
| 21 | beforeEach(() => { |
| 22 | provider = new OpenAIProvider(); |
| 23 | |
| 24 | // Mock the global execute tool function |
| 25 | mockExecuteToolFn = vi.fn().mockResolvedValue({ result: 'success' }); |
| 26 | provider._setExecuteToolFn(mockExecuteToolFn); |
| 27 | |
| 28 | // Use the transformed tool mock from existing test utilities |
| 29 | mockTool = { |
| 30 | ...toolMocks.transformedTool, |
| 31 | tags: [], |
| 32 | } as Tool; |
| 33 | }); |
| 34 | |
| 35 | describe('name property', () => { |
| 36 | it('should have the correct name', () => { |
| 37 | expect(provider.name).toBe('openai'); |
| 38 | }); |
| 39 | }); |
| 40 | |
| 41 | describe('_isAgentic property', () => { |
| 42 | it('should be non-agentic', () => { |
| 43 | expect(provider._isAgentic).toBe(false); |
| 44 | }); |
| 45 | }); |
| 46 | |
| 47 | describe('wrapTool', () => { |
| 48 | it('should wrap a tool in OpenAI function format', () => { |
| 49 | const wrapped = provider.wrapTool(mockTool); |
| 50 | |
| 51 | expect(wrapped).toEqual({ |
| 52 | type: 'function', |
| 53 | function: { |
| 54 | name: mockTool.slug, |
| 55 | description: mockTool.description, |
| 56 | parameters: mockTool.inputParameters, |
| 57 | }, |
| 58 | }); |
| 59 | }); |
| 60 | |
| 61 | it('deduplicates required entries without mutating the input schema', () => { |
| 62 | const tool = { |
| 63 | ...mockTool, |
| 64 | inputParameters: { |
| 65 | type: 'object', |
| 66 | properties: { |
| 67 | query: { type: 'string' }, |
| 68 | filters: { |
| 69 | type: 'object', |
| 70 | properties: { status: { type: 'string' } }, |
| 71 | required: ['status', 'status'], |
| 72 | }, |
| 73 | }, |
| 74 | required: ['query', 'query'], |
| 75 | }, |
| 76 | } as Tool; |
| 77 | |
| 78 | const wrapped = provider.wrapTool(tool); |
| 79 | |
| 80 | expect(wrapped.function.parameters).toMatchObject({ |
| 81 | required: ['query'], |
| 82 | properties: { filters: { required: ['status'] } }, |
| 83 | }); |
| 84 | expect(tool.inputParameters).toMatchObject({ |
| 85 | required: ['query', 'query'], |
| 86 | properties: { filters: { required: ['status', 'status'] } }, |
| 87 | }); |
| 88 | }); |
| 89 | }); |
| 90 | |
| 91 | describe('wrapTools', () => { |
| 92 | it('should wrap multiple tools', () => { |
| 93 | const anotherTool = { ...mockTool, slug: 'another-tool' }; |
| 94 | const tools = [mockTool, anotherTool]; |
| 95 | |
| 96 | const wrapped = provider.wrapTools(tools); |
| 97 | |
| 98 | expect(wrapped).toHaveLength(2); |
| 99 | expect((wrapped[0] as OpenAI.ChatCompletionFunctionTool).function.name).toBe(mockTool.slug); |
| 100 | expect((wrapped[1] as OpenAI.ChatCompletionFunctionTool).function.name).toBe( |
| 101 | anotherTool.slug |
| 102 | ); |
| 103 | }); |
| 104 | }); |
| 105 | |
| 106 | describe('executeToolCall', () => { |
| 107 | it('should execute a tool call and return the result as string', async () => { |
| 108 | const userId = 'test-user'; |
| 109 | const toolCall = { |
| 110 | id: 'call-123', |
| 111 | type: 'function', |
| 112 | function: { |
| 113 | name: 'test-tool', |
| 114 | arguments: JSON.stringify({ input: 'test-value' }), |
| 115 | }, |
| 116 | } as OpenAI.ChatCompletionMessageFunctionToolCall; |
| 117 | |
| 118 | const result = await provider.executeToolCall(userId, toolCall); |
| 119 | |
| 120 | expect(mockExecuteToolFn).toHaveBeenCalledWith( |
| 121 | 'test-tool', |
| 122 | { |
| 123 | arguments: { input: 'test-value' }, |
| 124 | userId: 'test-user', |
| 125 | connectedAccountId: undefined, |
| 126 | customAuthParams: undefined, |
| 127 | }, |
| 128 | undefined |
| 129 | ); |
| 130 | expect(result).toBe(JSON.stringify({ result: 'success' })); |
| 131 | }); |
| 132 | |
| 133 | it('should coerce empty-string arguments to an empty object (issue #2406)', async () => { |
| 134 | const toolCall = { |
| 135 | id: 'call-123', |
| 136 | type: 'function', |
| 137 | function: { name: 'test-tool', arguments: '' }, |
| 138 | } as OpenAI.ChatCompletionMessageFunctionToolCall; |
| 139 | |
| 140 | await provider.executeToolCall('test-user', toolCall); |
| 141 | |
| 142 | expect(mockExecuteToolFn).toHaveBeenCalledWith( |
| 143 | 'test-tool', |
| 144 | expect.objectContaining({ arguments: {} }), |
| 145 | undefined |
| 146 | ); |
| 147 | }); |
| 148 | |
| 149 | it('should pass options to executeTool', async () => { |
| 150 | const userId = 'test-user'; |
| 151 | const toolCall = { |
| 152 | id: 'call-123', |
| 153 | type: 'function', |
| 154 | function: { |
| 155 | name: 'test-tool', |
| 156 | arguments: JSON.stringify({ input: 'test-value' }), |
| 157 | }, |
| 158 | } as OpenAI.ChatCompletionMessageFunctionToolCall; |
| 159 | |
| 160 | const options = { |
| 161 | connectedAccountId: 'conn-123', |
| 162 | customAuthParams: { |
| 163 | parameters: [{ name: 'token', value: 'abc123', in: 'header' as const }], |
| 164 | }, |
| 165 | }; |
| 166 | |
| 167 | // Create a valid ExecuteToolModifiers object with beforeExecute and afterExecute functions |
| 168 | const modifiers: ExecuteToolModifiers = { |
| 169 | beforeExecute: vi.fn(({ toolSlug, toolkitSlug, params }) => { |
| 170 | return { |
| 171 | ...params, |
| 172 | allowTracing: true, |
| 173 | }; |
| 174 | }), |
| 175 | afterExecute: vi.fn(({ toolSlug, toolkitSlug, result }) => { |
| 176 | return result; |
| 177 | }), |
| 178 | }; |
| 179 | |
| 180 | await provider.executeToolCall(userId, toolCall, options, modifiers); |
| 181 | |
| 182 | expect(mockExecuteToolFn).toHaveBeenCalledWith( |
| 183 | 'test-tool', |
| 184 | { |
| 185 | arguments: { input: 'test-value' }, |
| 186 | userId: 'test-user', |
| 187 | connectedAccountId: 'conn-123', |
| 188 | customAuthParams: options.customAuthParams, |
| 189 | }, |
| 190 | modifiers |
| 191 | ); |
| 192 | }); |
| 193 | }); |
| 194 | |
| 195 | describe('handleToolCalls', () => { |
| 196 | it('routes session tool calls through the session executor', async () => { |
| 197 | const session = { |
| 198 | execute: vi.fn().mockResolvedValue({ |
| 199 | data: { tools: ['GMAIL_SEND_EMAIL'] }, |
| 200 | error: null, |
| 201 | logId: 'log-session', |
| 202 | resultType: 'completed', |
| 203 | }), |
| 204 | }; |
| 205 | const chatCompletion = { |
| 206 | choices: [ |
| 207 | { |
| 208 | message: { |
| 209 | tool_calls: [ |
| 210 | { |
| 211 | id: 'call-search', |
| 212 | type: 'function', |
| 213 | function: { |
| 214 | name: 'COMPOSIO_SEARCH_TOOLS', |
| 215 | arguments: JSON.stringify({ queries: [{ use_case: 'send email' }] }), |
| 216 | }, |
| 217 | }, |
| 218 | ], |
| 219 | }, |
| 220 | }, |
| 221 | ], |
| 222 | } as OpenAI.ChatCompletion; |
| 223 | |
| 224 | const results = await provider.handleToolCalls(session, chatCompletion); |
| 225 | |
| 226 | expect(session.execute).toHaveBeenCalledWith('COMPOSIO_SEARCH_TOOLS', { |
| 227 | queries: [{ use_case: 'send email' }], |
| 228 | }); |
| 229 | expect(mockExecuteToolFn).not.toHaveBeenCalled(); |
| 230 | expect(results).toEqual([ |
| 231 | { |
| 232 | role: 'tool', |
| 233 | tool_call_id: 'call-search', |
| 234 | content: JSON.stringify({ |
| 235 | data: { tools: ['GMAIL_SEND_EMAIL'] }, |
| 236 | error: null, |
| 237 | logId: 'log-session', |
| 238 | resultType: 'completed', |
| 239 | successful: true, |
| 240 | }), |
| 241 | }, |
| 242 | ]); |
| 243 | }); |
| 244 | |
| 245 | it('rejects direct execution options for session tool calls', async () => { |
| 246 | const session = { |
| 247 | execute: vi.fn(), |
| 248 | }; |
| 249 | const chatCompletion = { choices: [] } as unknown as OpenAI.ChatCompletion; |
| 250 | const handleToolCallsWithOptions = provider.handleToolCalls as unknown as ( |
| 251 | executionTarget: typeof session, |
| 252 | response: OpenAI.ChatCompletion, |
| 253 | options: { connectedAccountId: string } |
| 254 | ) => ReturnType<OpenAIProvider['handleToolCalls']>; |
| 255 | |
| 256 | await expect( |
| 257 | handleToolCallsWithOptions.call(provider, session, chatCompletion, { |
| 258 | connectedAccountId: 'conn-123', |
| 259 | }) |
| 260 | ).rejects.toThrow( |
| 261 | 'Direct execution options and modifiers cannot be used with a Tool Router session' |
| 262 | ); |
| 263 | expect(session.execute).not.toHaveBeenCalled(); |
| 264 | }); |
| 265 | |
| 266 | it('should handle tool calls from chat completion', async () => { |
| 267 | const userId = 'test-user'; |
| 268 | const chatCompletion = { |
| 269 | id: 'chat-123', |
| 270 | model: 'gpt-4', |
| 271 | created: 123456789, |
| 272 | object: 'chat.completion', |
| 273 | choices: [ |
| 274 | { |
| 275 | message: { |
| 276 | role: 'assistant', |
| 277 | content: null, |
| 278 | tool_calls: [ |
| 279 | { |
| 280 | id: 'call-123', |
| 281 | type: 'function', |
| 282 | function: { |
| 283 | name: 'test-tool', |
| 284 | arguments: JSON.stringify({ input: 'test-value' }), |
| 285 | }, |
| 286 | } as const, |
| 287 | ], |
| 288 | refusal: null, |
| 289 | }, |
| 290 | index: 0, |
| 291 | finish_reason: 'tool_calls', |
| 292 | }, |
| 293 | ], |
| 294 | usage: { |
| 295 | prompt_tokens: 10, |
| 296 | completion_tokens: 20, |
| 297 | total_tokens: 30, |
| 298 | }, |
| 299 | } as OpenAI.ChatCompletion; |
| 300 | |
| 301 | const executeToolCallSpy = vi.spyOn(provider, 'executeToolCall'); |
| 302 | executeToolCallSpy.mockResolvedValue(JSON.stringify({ result: 'success' })); |
| 303 | |
| 304 | const results = await provider.handleToolCalls(userId, chatCompletion); |
| 305 | |
| 306 | expect(executeToolCallSpy).toHaveBeenCalledWith( |
| 307 | userId, |
| 308 | chatCompletion.choices[0].message.tool_calls![0], |
| 309 | undefined, |
| 310 | undefined |
| 311 | ); |
| 312 | expect(results).toEqual([ |
| 313 | { role: 'tool', tool_call_id: 'call-123', content: JSON.stringify({ result: 'success' }) }, |
| 314 | ]); |
| 315 | }); |
| 316 | |
| 317 | it('should handle multiple parallel tool calls in a single message', async () => { |
| 318 | const userId = 'test-user'; |
| 319 | const chatCompletion = { |
| 320 | id: 'chat-123', |
| 321 | model: 'gpt-4', |
| 322 | created: 123456789, |
| 323 | object: 'chat.completion', |
| 324 | choices: [ |
| 325 | { |
| 326 | message: { |
| 327 | role: 'assistant', |
| 328 | content: null, |
| 329 | tool_calls: [ |
| 330 | { |
| 331 | id: 'call-123', |
| 332 | type: 'function', |
| 333 | function: { |
| 334 | name: 'test-tool', |
| 335 | arguments: JSON.stringify({ input: 'first' }), |
| 336 | }, |
| 337 | } as const, |
| 338 | { |
| 339 | id: 'call-456', |
| 340 | type: 'function', |
| 341 | function: { |
| 342 | name: 'other-tool', |
| 343 | arguments: JSON.stringify({ input: 'second' }), |
| 344 | }, |
| 345 | } as const, |
| 346 | ], |
| 347 | refusal: null, |
| 348 | }, |
| 349 | index: 0, |
| 350 | finish_reason: 'tool_calls', |
| 351 | }, |
| 352 | ], |
| 353 | usage: { |
| 354 | prompt_tokens: 10, |
| 355 | completion_tokens: 20, |
| 356 | total_tokens: 30, |
| 357 | }, |
| 358 | } as OpenAI.ChatCompletion; |
| 359 | |
| 360 | const executeToolCallSpy = vi.spyOn(provider, 'executeToolCall'); |
| 361 | executeToolCallSpy.mockResolvedValue(JSON.stringify({ result: 'success' })); |
| 362 | |
| 363 | const results = await provider.handleToolCalls(userId, chatCompletion); |
| 364 | |
| 365 | // Every parallel tool call must be executed and answered, one tool message |
| 366 | // per tool_call_id — otherwise the follow-up request errors on the unanswered ids. |
| 367 | expect(executeToolCallSpy).toHaveBeenCalledTimes(2); |
| 368 | expect(results).toEqual([ |
| 369 | { role: 'tool', tool_call_id: 'call-123', content: JSON.stringify({ result: 'success' }) }, |
| 370 | { role: 'tool', tool_call_id: 'call-456', content: JSON.stringify({ result: 'success' }) }, |
| 371 | ]); |
| 372 | }); |
| 373 | |
| 374 | it('should only handle tool calls from the first choice when n > 1', async () => { |
| 375 | const userId = 'test-user'; |
| 376 | const makeChoice = (index: number, callId: string) => ({ |
| 377 | message: { |
| 378 | role: 'assistant', |
| 379 | content: null, |
| 380 | tool_calls: [ |
| 381 | { |
| 382 | id: callId, |
| 383 | type: 'function', |
| 384 | function: { |
| 385 | name: 'test-tool', |
| 386 | arguments: JSON.stringify({ input: 'value' }), |
| 387 | }, |
| 388 | } as const, |
| 389 | ], |
| 390 | refusal: null, |
| 391 | }, |
| 392 | index, |
| 393 | finish_reason: 'tool_calls', |
| 394 | }); |
| 395 | const chatCompletion = { |
| 396 | id: 'chat-123', |
| 397 | model: 'gpt-4', |
| 398 | created: 123456789, |
| 399 | object: 'chat.completion', |
| 400 | // n > 1: alternative completions the caller never continues. Only the |
| 401 | // first choice's tool calls should run; the rest would orphan their ids. |
| 402 | choices: [makeChoice(0, 'call-first'), makeChoice(1, 'call-second')], |
| 403 | usage: { |
| 404 | prompt_tokens: 10, |
| 405 | completion_tokens: 20, |
| 406 | total_tokens: 30, |
| 407 | }, |
| 408 | } as OpenAI.ChatCompletion; |
| 409 | |
| 410 | const executeToolCallSpy = vi.spyOn(provider, 'executeToolCall'); |
| 411 | executeToolCallSpy.mockResolvedValue(JSON.stringify({ result: 'success' })); |
| 412 | |
| 413 | const results = await provider.handleToolCalls(userId, chatCompletion); |
| 414 | |
| 415 | expect(executeToolCallSpy).toHaveBeenCalledTimes(1); |
| 416 | expect(results).toEqual([ |
| 417 | { |
| 418 | role: 'tool', |
| 419 | tool_call_id: 'call-first', |
| 420 | content: JSON.stringify({ result: 'success' }), |
| 421 | }, |
| 422 | ]); |
| 423 | }); |
| 424 | }); |
| 425 | }); |