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

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