@devries-cameron20 / composio Mirror · read-only
Fork 0

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

Mirrored from https://github.com/Camo67/composio.git

Synced Nov 21, 06:00 UTC

next
ts 754 lines 22.7 KB
Raw
1 import { describe, it, expect, vi, beforeEach } from 'vitest';
2 import { OpenAIResponsesProvider } from '../src';
3 import { Tool } from '@composio/core';
4 import { OpenAI } from 'openai';
5
6 // Mock the openai modules
7 vi.mock('openai', () => {
8 return {
9 OpenAI: vi.fn().mockImplementation(() => ({
10 beta: {
11 threads: {
12 runs: {
13 retrieve: vi.fn().mockImplementation((threadId, runId) => {
14 return { id: runId, status: 'completed' };
15 }),
16 submitToolOutputs: vi.fn().mockImplementation((threadId, runId, options) => {
17 return { id: runId, status: 'completed' };
18 }),
19 },
20 },
21 },
22 })),
23 };
24 });
25
26 // Define interfaces for our OpenAI mocked types
27 interface MockedOpenAITool {
28 type: 'function';
29 name: string;
30 description?: string;
31 parameters?: unknown;
32 strict?: boolean;
33 }
34
35 describe('OpenAIResponsesProvider', () => {
36 let provider: OpenAIResponsesProvider;
37 let mockTool: Tool;
38 let mockExecuteToolFn: unknown;
39
40 beforeEach(() => {
41 provider = new OpenAIResponsesProvider();
42
43 // Mock the global execute tool function
44 mockExecuteToolFn = vi.fn().mockResolvedValue({
45 data: { result: 'success' },
46 error: null,
47 successful: true,
48 });
49 provider._setExecuteToolFn(mockExecuteToolFn);
50
51 // Create a mock Composio tool
52 mockTool = {
53 slug: 'test-tool',
54 name: 'Test Tool',
55 description: 'A tool for testing',
56 inputParameters: {
57 type: 'object',
58 properties: {
59 input: {
60 type: 'string',
61 description: 'Test input',
62 },
63 },
64 required: ['input'],
65 },
66 tags: [],
67 };
68
69 // Reset mocks before each test
70 vi.clearAllMocks();
71 });
72
73 describe('name property', () => {
74 it('should have the correct name', () => {
75 expect(provider.name).toBe('openai');
76 });
77 });
78
79 describe('wrapTool', () => {
80 it('should wrap a tool in OpenAI function format', () => {
81 const wrapped = provider.wrapTool(mockTool) as MockedOpenAITool;
82
83 expect(wrapped).toEqual({
84 type: 'function',
85 name: mockTool.slug,
86 description: mockTool.description,
87 parameters: mockTool.inputParameters,
88 strict: false,
89 });
90 });
91
92 it('should handle tools without input parameters', () => {
93 const toolWithoutParams: Tool = {
94 ...mockTool,
95 inputParameters: undefined,
96 };
97
98 const wrapped = provider.wrapTool(toolWithoutParams) as MockedOpenAITool;
99
100 expect(wrapped).toEqual({
101 type: 'function',
102 name: toolWithoutParams.slug,
103 description: toolWithoutParams.description,
104 parameters: {},
105 strict: false,
106 });
107 });
108
109 it('should respect strict mode when wrapping tools', () => {
110 const strictProvider = new OpenAIResponsesProvider({ strict: true });
111 const wrapped = strictProvider.wrapTool(mockTool) as MockedOpenAITool;
112
113 expect(wrapped.strict).toBe(true);
114 expect(wrapped.parameters).toEqual({
115 ...mockTool.inputParameters,
116 additionalProperties: false,
117 });
118 expect(mockTool.inputParameters).not.toHaveProperty('additionalProperties');
119 });
120
121 it('deduplicates required entries for directly wrapped tools', () => {
122 const wrapped = provider.wrapTool({
123 ...mockTool,
124 inputParameters: {
125 ...mockTool.inputParameters!,
126 required: ['input', 'input'],
127 },
128 }) as MockedOpenAITool;
129
130 expect(wrapped.parameters.required).toEqual(['input']);
131 });
132
133 it('keeps optional parameters as required-nullable under strict mode', () => {
134 const strictProvider = new OpenAIResponsesProvider({ strict: true });
135 const wrapped = strictProvider.wrapTool({
136 ...mockTool,
137 inputParameters: {
138 type: 'object',
139 properties: {
140 cfg: {
141 type: 'object',
142 properties: {
143 url: { type: 'string' },
144 note: { type: 'string' },
145 },
146 required: ['url'],
147 },
148 id: { type: ['string', 'null'] },
149 payload: {
150 anyOf: [
151 {
152 type: 'object',
153 properties: { inner: { type: 'string' }, extra: { type: 'string' } },
154 required: ['inner'],
155 },
156 { type: 'null' },
157 ],
158 },
159 label: { type: 'string', default: 'x' },
160 },
161 required: ['cfg', 'id', 'payload'],
162 },
163 }) as MockedOpenAITool;
164
165 expect(wrapped.strict).toBe(true);
166 expect(wrapped.parameters).toEqual({
167 type: 'object',
168 properties: {
169 cfg: {
170 type: 'object',
171 properties: { url: { type: 'string' }, note: { type: ['string', 'null'] } },
172 required: ['url', 'note'],
173 additionalProperties: false,
174 },
175 id: { type: ['string', 'null'] },
176 payload: {
177 anyOf: [
178 {
179 type: 'object',
180 properties: { inner: { type: 'string' }, extra: { type: ['string', 'null'] } },
181 required: ['inner', 'extra'],
182 additionalProperties: false,
183 },
184 { type: 'null' },
185 ],
186 },
187 label: { type: ['string', 'null'] },
188 },
189 required: ['cfg', 'id', 'payload', 'label'],
190 additionalProperties: false,
191 });
192 });
193
194 it('keeps nullable nested objects nullable and $defs referenced under strict mode', () => {
195 const strictProvider = new OpenAIResponsesProvider({ strict: true });
196 const wrapped = strictProvider.wrapTool({
197 ...mockTool,
198 inputParameters: {
199 type: 'object',
200 properties: {
201 cfg: { $ref: '#/$defs/Config' },
202 },
203 required: ['cfg'],
204 $defs: {
205 Config: {
206 type: ['object', 'null'],
207 properties: { url: { type: 'string' }, opt: { type: 'string' } },
208 required: ['url'],
209 },
210 },
211 },
212 }) as MockedOpenAITool;
213
214 expect(wrapped.strict).toBe(true);
215 expect(wrapped.parameters).toEqual({
216 type: 'object',
217 properties: {
218 cfg: { $ref: '#/$defs/Config' },
219 },
220 required: ['cfg'],
221 additionalProperties: false,
222 $defs: {
223 Config: {
224 type: ['object', 'null'],
225 properties: { url: { type: 'string' }, opt: { type: ['string', 'null'] } },
226 required: ['url', 'opt'],
227 additionalProperties: false,
228 },
229 },
230 });
231 });
232
233 it('sends tools strict mode cannot express without strict, keeping their schema', () => {
234 const strictProvider = new OpenAIResponsesProvider({ strict: true });
235 const inputParameters = {
236 type: 'object',
237 properties: {
238 headers: { type: 'object', additionalProperties: { type: 'string' } },
239 name: { type: 'string' },
240 },
241 required: ['headers', 'name', 'name'],
242 };
243 const wrapped = strictProvider.wrapTool({ ...mockTool, inputParameters }) as MockedOpenAITool;
244
245 expect(wrapped.strict).toBe(false);
246 expect(wrapped.parameters).toEqual({ ...inputParameters, required: ['headers', 'name'] });
247 });
248
249 it('emits an empty closed object for tools without parameters in strict mode', () => {
250 const strictProvider = new OpenAIResponsesProvider({ strict: true });
251 const wrapped = strictProvider.wrapTool({
252 ...mockTool,
253 inputParameters: undefined,
254 }) as MockedOpenAITool;
255
256 expect(wrapped.strict).toBe(true);
257 expect(wrapped.parameters).toEqual({
258 type: 'object',
259 properties: {},
260 required: [],
261 additionalProperties: false,
262 });
263 });
264 });
265
266 describe('wrapMcpServerResponse', () => {
267 it('never logs MCP server URLs to stdout', () => {
268 const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {});
269 const mcpUrl = 'https://mcp.example.com/t/super-secret-token';
270 const result = provider.wrapMcpServerResponse([{ name: 'srv', url: mcpUrl }]);
271 expect(logSpy).not.toHaveBeenCalled();
272 logSpy.mockRestore();
273 expect(result).toEqual([
274 { type: 'mcp', server_label: 'srv', server_url: mcpUrl, require_approval: 'never' },
275 ]);
276 });
277 });
278
279 describe('wrapTools', () => {
280 it('should wrap multiple tools', () => {
281 const anotherTool: Tool = {
282 ...mockTool,
283 slug: 'another-tool',
284 name: 'Another Tool',
285 };
286 const tools = [mockTool, anotherTool];
287
288 const wrapped = provider.wrapTools(tools);
289
290 expect(wrapped).toHaveLength(2);
291 expect(wrapped[0]).toEqual({
292 type: 'function',
293 name: mockTool.slug,
294 description: mockTool.description,
295 parameters: mockTool.inputParameters,
296 strict: false,
297 });
298 expect(wrapped[1]).toEqual({
299 type: 'function',
300 name: anotherTool.slug,
301 description: anotherTool.description,
302 parameters: anotherTool.inputParameters,
303 strict: false,
304 });
305 });
306
307 it('should return an empty array for empty tools array', () => {
308 const wrapped = provider.wrapTools([]);
309 expect(wrapped).toEqual([]);
310 });
311 });
312
313 describe('executeToolCall', () => {
314 it('should execute a tool call and return the result as string', async () => {
315 const userId = 'test-user';
316 const toolCall = {
317 id: 'call-123',
318 type: 'function',
319 name: 'test-tool',
320 arguments: JSON.stringify({ input: 'test-value' }),
321 call_id: 'call-123',
322 } as unknown as OpenAI.Responses.ResponseFunctionToolCall;
323
324 const result = await provider.executeToolCall(userId, toolCall);
325
326 expect(mockExecuteToolFn).toHaveBeenCalledWith(
327 'test-tool',
328 {
329 arguments: { input: 'test-value' },
330 userId: 'test-user',
331 connectedAccountId: undefined,
332 customAuthParams: undefined,
333 },
334 undefined
335 );
336 expect(result).toBe(
337 JSON.stringify({
338 data: { result: 'success' },
339 error: null,
340 successful: true,
341 })
342 );
343 });
344
345 it('omits nulls the tool schema does not accept before executing under strict mode', async () => {
346 const strictProvider = new OpenAIResponsesProvider({ strict: true });
347 strictProvider._setExecuteToolFn(mockExecuteToolFn);
348 strictProvider.wrapTool({
349 ...mockTool,
350 slug: 'test-tool',
351 inputParameters: {
352 type: 'object',
353 properties: {
354 cfg: {
355 type: 'object',
356 properties: { url: { type: 'string' }, note: { type: 'string' } },
357 required: ['url'],
358 },
359 label: { type: 'string' },
360 clearable: { type: ['string', 'null'] },
361 },
362 required: ['cfg'],
363 },
364 });
365 const toolCall = {
366 id: 'call-123',
367 type: 'function',
368 name: 'test-tool',
369 arguments: JSON.stringify({ cfg: { url: 'u', note: null }, label: null, clearable: null }),
370 call_id: 'call-123',
371 } as unknown as OpenAI.Responses.ResponseFunctionToolCall;
372
373 await strictProvider.executeToolCall('test-user', toolCall);
374
375 expect(mockExecuteToolFn).toHaveBeenCalledWith(
376 'test-tool',
377 {
378 arguments: { cfg: { url: 'u' }, clearable: null },
379 userId: 'test-user',
380 connectedAccountId: undefined,
381 customAuthParams: undefined,
382 },
383 undefined
384 );
385 });
386
387 it('should coerce empty-string arguments to an empty object (issue #2406)', async () => {
388 const toolCall = {
389 id: 'call-123',
390 type: 'function',
391 name: 'test-tool',
392 arguments: '',
393 call_id: 'call-123',
394 } as unknown as OpenAI.Responses.ResponseFunctionToolCall;
395
396 await provider.executeToolCall('test-user', toolCall);
397
398 expect(mockExecuteToolFn).toHaveBeenCalledWith(
399 'test-tool',
400 expect.objectContaining({ arguments: {} }),
401 undefined
402 );
403 });
404
405 it('should pass options to executeTool', async () => {
406 const userId = 'test-user';
407 const toolCall = {
408 id: 'call-123',
409 type: 'function',
410 name: 'test-tool',
411 arguments: JSON.stringify({ input: 'test-value' }),
412 call_id: 'call-123',
413 } as unknown as OpenAI.Responses.ResponseFunctionToolCall;
414
415 const options = {
416 connectedAccountId: 'conn-123',
417 customAuthParams: {
418 parameters: [{ name: 'token', value: 'abc123', in: 'header' as const }],
419 },
420 };
421
422 const modifiers = {
423 beforeExecute: vi.fn(({ params }) => params),
424 afterExecute: vi.fn(({ result }) => result),
425 };
426
427 await provider.executeToolCall(userId, toolCall, options, modifiers);
428
429 expect(mockExecuteToolFn).toHaveBeenCalledWith(
430 'test-tool',
431 {
432 arguments: { input: 'test-value' },
433 userId: 'test-user',
434 connectedAccountId: 'conn-123',
435 customAuthParams: options.customAuthParams,
436 },
437 modifiers
438 );
439 });
440 });
441
442 describe('handleToolCalls', () => {
443 it('preserves Responses output order and errors when executing through a session', async () => {
444 const session = {
445 execute: vi
446 .fn()
447 .mockResolvedValueOnce({
448 data: { index: 1 },
449 error: null,
450 logId: 'log-1',
451 resultType: 'completed',
452 })
453 .mockRejectedValueOnce(new Error('session execution failed')),
454 };
455 const toolCalls = [
456 {
457 id: 'item-1',
458 type: 'function_call',
459 name: 'COMPOSIO_SEARCH_TOOLS',
460 arguments: JSON.stringify({ queries: [{ use_case: 'first' }] }),
461 call_id: 'call-1',
462 },
463 {
464 id: 'item-2',
465 type: 'function_call',
466 name: 'COMPOSIO_SEARCH_TOOLS',
467 arguments: JSON.stringify({ queries: [{ use_case: 'second' }] }),
468 call_id: 'call-2',
469 },
470 ] as OpenAI.Responses.ResponseOutputItem[];
471
472 const results = await provider.handleToolCalls(session, toolCalls);
473
474 expect(session.execute).toHaveBeenNthCalledWith(1, 'COMPOSIO_SEARCH_TOOLS', {
475 queries: [{ use_case: 'first' }],
476 });
477 expect(session.execute).toHaveBeenNthCalledWith(2, 'COMPOSIO_SEARCH_TOOLS', {
478 queries: [{ use_case: 'second' }],
479 });
480 expect(mockExecuteToolFn).not.toHaveBeenCalled();
481 expect(results).toEqual([
482 {
483 call_id: 'call-1',
484 type: 'function_call_output',
485 output: JSON.stringify({
486 data: { index: 1 },
487 error: null,
488 logId: 'log-1',
489 resultType: 'completed',
490 successful: true,
491 }),
492 status: 'completed',
493 },
494 {
495 call_id: 'call-2',
496 type: 'function_call_output',
497 output: 'session execution failed',
498 status: 'incomplete',
499 },
500 ]);
501 });
502
503 it('should handle tool calls from OpenAI response', async () => {
504 const userId = 'test-user';
505 const toolCalls = [
506 {
507 id: 'call-123',
508 type: 'function_call',
509 name: 'test-tool',
510 arguments: JSON.stringify({ input: 'test-value' }),
511 call_id: 'call-123',
512 },
513 ] as OpenAI.Responses.ResponseOutputItem[];
514
515 const executeToolCallSpy = vi.spyOn(provider, 'executeToolCall');
516 executeToolCallSpy.mockResolvedValue(JSON.stringify({ result: 'success' }));
517
518 const results = await provider.handleToolCalls(userId, toolCalls);
519
520 expect(executeToolCallSpy).toHaveBeenCalledWith(
521 userId,
522 {
523 id: 'call-123',
524 name: 'test-tool',
525 arguments: JSON.stringify({ input: 'test-value' }),
526 },
527 undefined,
528 undefined
529 );
530 expect(results).toEqual([
531 {
532 call_id: 'call-123',
533 type: 'function_call_output',
534 output: JSON.stringify({ result: 'success' }),
535 status: 'completed',
536 },
537 ]);
538 });
539
540 it('should handle errors during tool execution', async () => {
541 const userId = 'test-user';
542 const toolCalls = [
543 {
544 id: 'call-123',
545 type: 'function_call',
546 name: 'test-tool',
547 arguments: JSON.stringify({ input: 'test-value' }),
548 call_id: 'call-123',
549 },
550 ] as OpenAI.Responses.ResponseOutputItem[];
551
552 const executeToolCallSpy = vi.spyOn(provider, 'executeToolCall');
553 executeToolCallSpy.mockRejectedValue(new Error('Tool execution failed'));
554
555 const results = await provider.handleToolCalls(userId, toolCalls);
556
557 expect(results).toEqual([
558 {
559 call_id: 'call-123',
560 type: 'function_call_output',
561 output: 'Tool execution failed',
562 status: 'incomplete',
563 },
564 ]);
565 });
566
567 it('should handle multiple tool calls', async () => {
568 const userId = 'test-user';
569 const toolCalls = [
570 {
571 id: 'call-123',
572 type: 'function_call',
573 name: 'test-tool-1',
574 arguments: JSON.stringify({ input: 'test-value-1' }),
575 call_id: 'call-123',
576 },
577 {
578 id: 'call-456',
579 type: 'function_call',
580 name: 'test-tool-2',
581 arguments: JSON.stringify({ input: 'test-value-2' }),
582 call_id: 'call-456',
583 },
584 ] as OpenAI.Responses.ResponseOutputItem[];
585
586 const executeToolCallSpy = vi.spyOn(provider, 'executeToolCall');
587 executeToolCallSpy
588 .mockResolvedValueOnce(JSON.stringify({ result: 'success-1' }))
589 .mockResolvedValueOnce(JSON.stringify({ result: 'success-2' }));
590
591 const results = await provider.handleToolCalls(userId, toolCalls);
592
593 expect(executeToolCallSpy).toHaveBeenCalledTimes(2);
594 expect(results).toEqual([
595 {
596 call_id: 'call-123',
597 type: 'function_call_output',
598 output: JSON.stringify({ result: 'success-1' }),
599 status: 'completed',
600 },
601 {
602 call_id: 'call-456',
603 type: 'function_call_output',
604 output: JSON.stringify({ result: 'success-2' }),
605 status: 'completed',
606 },
607 ]);
608 });
609 });
610
611 describe('handleResponse', () => {
612 it('should handle OpenAI response with tool calls', async () => {
613 const userId = 'test-user';
614 const response = {
615 id: 'response-123',
616 created_at: new Date().toISOString(),
617 output: [
618 {
619 id: 'call-123',
620 type: 'function_call',
621 name: 'test-tool',
622 arguments: JSON.stringify({ input: 'test-value' }),
623 call_id: 'call-123',
624 },
625 ],
626 output_text: 'Test output',
627 error: null,
628 status: 'completed',
629 } as unknown as OpenAI.Responses.Response;
630
631 const handleToolCallsSpy = vi.spyOn(provider, 'handleToolCalls');
632 handleToolCallsSpy.mockResolvedValue([
633 {
634 call_id: 'call-123',
635 type: 'function_call_output',
636 output: JSON.stringify({ result: 'success' }),
637 status: 'completed',
638 },
639 ]);
640
641 const results = await provider.handleResponse(userId, response);
642
643 expect(handleToolCallsSpy).toHaveBeenCalledWith(
644 userId,
645 response.output,
646 undefined,
647 undefined
648 );
649 expect(results).toEqual([
650 {
651 call_id: 'call-123',
652 type: 'function_call_output',
653 output: JSON.stringify({ result: 'success' }),
654 status: 'completed',
655 },
656 ]);
657 });
658
659 it('should handle OpenAI response without tool calls', async () => {
660 const userId = 'test-user';
661 const response = {
662 id: 'response-123',
663 created_at: new Date().toISOString(),
664 output: [],
665 output_text: 'Test output',
666 error: null,
667 status: 'completed',
668 } as unknown as OpenAI.Responses.Response;
669
670 const handleToolCallsSpy = vi.spyOn(provider, 'handleToolCalls');
671 handleToolCallsSpy.mockResolvedValue([]);
672
673 const results = await provider.handleResponse(userId, response);
674
675 expect(handleToolCallsSpy).toHaveBeenCalledWith(userId, [], undefined, undefined);
676 expect(results).toEqual([]);
677 });
678
679 it('should handle OpenAI response with undefined output', async () => {
680 const userId = 'test-user';
681 const response = {
682 id: 'response-123',
683 created_at: new Date().toISOString(),
684 output_text: 'Test output',
685 error: null,
686 status: 'completed',
687 } as unknown as OpenAI.Responses.Response;
688
689 const handleToolCallsSpy = vi.spyOn(provider, 'handleToolCalls');
690 handleToolCallsSpy.mockResolvedValue([]);
691
692 const results = await provider.handleResponse(userId, response);
693
694 expect(handleToolCallsSpy).toHaveBeenCalledWith(userId, [], undefined, undefined);
695 expect(results).toEqual([]);
696 });
697
698 it('executes the response tool calls through a Tool Router session', async () => {
699 const session = {
700 execute: vi.fn().mockResolvedValue({
701 data: { result: 'session-success' },
702 error: null,
703 logId: 'log-session',
704 resultType: 'completed',
705 }),
706 };
707 const response = {
708 output: [
709 { type: 'message', id: 'msg-1', content: [] },
710 {
711 id: 'fc-1',
712 type: 'function_call',
713 name: 'COMPOSIO_SEARCH_TOOLS',
714 arguments: JSON.stringify({ query: 'send an email' }),
715 call_id: 'call-1',
716 },
717 ],
718 } as unknown as OpenAI.Responses.Response;
719
720 const results = await provider.handleResponse(session, response);
721
722 expect(session.execute).toHaveBeenCalledWith('COMPOSIO_SEARCH_TOOLS', {
723 query: 'send an email',
724 });
725 expect(mockExecuteToolFn).not.toHaveBeenCalled();
726 expect(results).toEqual([
727 {
728 call_id: 'call-1',
729 type: 'function_call_output',
730 output: JSON.stringify({
731 data: { result: 'session-success' },
732 error: null,
733 logId: 'log-session',
734 resultType: 'completed',
735 successful: true,
736 }),
737 status: 'completed',
738 },
739 ]);
740 });
741
742 it('rejects direct execution options with a session', async () => {
743 const session = { execute: vi.fn() };
744 const response = { output: [] } as unknown as OpenAI.Responses.Response;
745
746 await expect(
747 // @ts-expect-error direct execution options are not accepted with a session
748 provider.handleResponse(session, response, { connectedAccountId: 'conn-123' })
749 ).rejects.toThrow(
750 'Direct execution options and modifiers cannot be used with a Tool Router session'
751 );
752 });
753 });
754 });