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
| 1 | import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 2 | import { OpenAIAgentsProvider } from '../src'; |
| 3 | import { Tool, ExecuteToolFn } from '@composio/core'; |
| 4 | import { tool as createOpenAIAgentTool } from '@openai/agents'; |
| 5 | |
| 6 | // Define an interface for our mocked OpenAI Agent tool |
| 7 | interface MockedOpenAIAgentTool { |
| 8 | name: string; |
| 9 | description: string; |
| 10 | parameters: unknown; |
| 11 | execute: Function; |
| 12 | _isMockedOpenAIAgentTool: boolean; |
| 13 | } |
| 14 | |
| 15 | // Mock the @openai/agents module |
| 16 | vi.mock('@openai/agents', () => { |
| 17 | return { |
| 18 | tool: vi.fn().mockImplementation(toolConfig => { |
| 19 | return { |
| 20 | ...toolConfig, |
| 21 | _isMockedOpenAIAgentTool: true, |
| 22 | } as MockedOpenAIAgentTool; |
| 23 | }), |
| 24 | }; |
| 25 | }); |
| 26 | |
| 27 | describe('OpenAIAgentsProvider', () => { |
| 28 | let provider: OpenAIAgentsProvider; |
| 29 | let mockTool: Tool; |
| 30 | let mockExecuteToolFn: unknown; |
| 31 | |
| 32 | beforeEach(() => { |
| 33 | provider = new OpenAIAgentsProvider(); |
| 34 | |
| 35 | // Mock the global execute tool function |
| 36 | mockExecuteToolFn = vi.fn().mockResolvedValue({ |
| 37 | data: { result: 'success' }, |
| 38 | error: null, |
| 39 | successful: true, |
| 40 | }); |
| 41 | provider._setExecuteToolFn(mockExecuteToolFn); |
| 42 | |
| 43 | // Create a mock Composio tool |
| 44 | mockTool = { |
| 45 | slug: 'test-tool', |
| 46 | name: 'Test Tool', |
| 47 | description: 'A tool for testing', |
| 48 | version: '20250909_00', |
| 49 | availableVersions: ['20250909_00', '20250901_00'], |
| 50 | inputParameters: { |
| 51 | type: 'object', |
| 52 | properties: { |
| 53 | input: { |
| 54 | type: 'string', |
| 55 | description: 'Test input', |
| 56 | }, |
| 57 | }, |
| 58 | required: ['input'], |
| 59 | }, |
| 60 | tags: [], |
| 61 | }; |
| 62 | |
| 63 | // Reset mocks before each test |
| 64 | vi.clearAllMocks(); |
| 65 | }); |
| 66 | |
| 67 | describe('name property', () => { |
| 68 | it('should have the correct name', () => { |
| 69 | expect(provider.name).toBe('openai-agents'); |
| 70 | }); |
| 71 | }); |
| 72 | |
| 73 | describe('_isAgentic property', () => { |
| 74 | it('should be agentic', () => { |
| 75 | expect(provider._isAgentic).toBe(true); |
| 76 | }); |
| 77 | }); |
| 78 | |
| 79 | describe('wrapTool', () => { |
| 80 | it('should wrap a tool in OpenAI Agent tool format', () => { |
| 81 | const wrapped = provider.wrapTool( |
| 82 | mockTool, |
| 83 | mockExecuteToolFn |
| 84 | ) as unknown as MockedOpenAIAgentTool; |
| 85 | |
| 86 | expect(createOpenAIAgentTool).toHaveBeenCalledWith({ |
| 87 | name: mockTool.slug, |
| 88 | description: mockTool.description, |
| 89 | parameters: { |
| 90 | type: 'object', |
| 91 | properties: mockTool.inputParameters?.properties || {}, |
| 92 | required: mockTool.inputParameters?.required || [], |
| 93 | additionalProperties: true, |
| 94 | }, |
| 95 | strict: false, |
| 96 | execute: expect.any(Function), |
| 97 | }); |
| 98 | |
| 99 | expect(wrapped._isMockedOpenAIAgentTool).toBe(true); |
| 100 | }); |
| 101 | |
| 102 | it('should handle tools without input parameters', () => { |
| 103 | const toolWithoutParams: Tool = { |
| 104 | ...mockTool, |
| 105 | inputParameters: undefined, |
| 106 | }; |
| 107 | |
| 108 | const wrapped = provider.wrapTool( |
| 109 | toolWithoutParams, |
| 110 | mockExecuteToolFn |
| 111 | ) as unknown as MockedOpenAIAgentTool; |
| 112 | |
| 113 | expect(wrapped._isMockedOpenAIAgentTool).toBe(true); |
| 114 | expect(wrapped.parameters).toEqual({ |
| 115 | type: 'object', |
| 116 | properties: {}, |
| 117 | required: [], |
| 118 | additionalProperties: true, |
| 119 | }); |
| 120 | }); |
| 121 | |
| 122 | it('deduplicates required entries for directly wrapped tools', () => { |
| 123 | provider.wrapTool( |
| 124 | { |
| 125 | ...mockTool, |
| 126 | inputParameters: { |
| 127 | ...mockTool.inputParameters!, |
| 128 | required: ['input', 'input'], |
| 129 | }, |
| 130 | }, |
| 131 | mockExecuteToolFn |
| 132 | ); |
| 133 | |
| 134 | expect(createOpenAIAgentTool).toHaveBeenCalledWith( |
| 135 | expect.objectContaining({ |
| 136 | parameters: expect.objectContaining({ required: ['input'] }), |
| 137 | }) |
| 138 | ); |
| 139 | }); |
| 140 | |
| 141 | it('should normalize a stringified-JSON input to an object before executing (issue #2406)', async () => { |
| 142 | const wrapped = provider.wrapTool( |
| 143 | mockTool, |
| 144 | mockExecuteToolFn |
| 145 | ) as unknown as MockedOpenAIAgentTool; |
| 146 | const params = { input: 'test-value' }; |
| 147 | |
| 148 | await wrapped.execute(params); |
| 149 | expect(mockExecuteToolFn).toHaveBeenCalledWith(mockTool.slug, params); |
| 150 | |
| 151 | vi.clearAllMocks(); |
| 152 | await wrapped.execute(JSON.stringify(params)); |
| 153 | expect(mockExecuteToolFn).toHaveBeenCalledWith(mockTool.slug, params); |
| 154 | }); |
| 155 | |
| 156 | it('should throw a typed error for a malformed-JSON string input (issue #2406)', async () => { |
| 157 | const wrapped = provider.wrapTool( |
| 158 | mockTool, |
| 159 | mockExecuteToolFn |
| 160 | ) as unknown as MockedOpenAIAgentTool; |
| 161 | |
| 162 | await expect(wrapped.execute('{"input":')).rejects.toThrow(/not valid JSON/); |
| 163 | }); |
| 164 | }); |
| 165 | |
| 166 | describe('strict mode', () => { |
| 167 | it('registers a strict schema with optional parameters kept as required-nullable', () => { |
| 168 | const strictProvider = new OpenAIAgentsProvider({ strict: true }); |
| 169 | strictProvider._setExecuteToolFn(mockExecuteToolFn); |
| 170 | const wrapped = strictProvider.wrapTool( |
| 171 | { |
| 172 | ...mockTool, |
| 173 | inputParameters: { |
| 174 | type: 'object', |
| 175 | properties: { |
| 176 | input: { type: 'string' }, |
| 177 | cfg: { |
| 178 | type: 'object', |
| 179 | properties: { url: { type: 'string' }, note: { type: 'string' } }, |
| 180 | required: ['url'], |
| 181 | }, |
| 182 | }, |
| 183 | required: ['input'], |
| 184 | }, |
| 185 | }, |
| 186 | mockExecuteToolFn as ExecuteToolFn |
| 187 | ) as unknown as MockedOpenAIAgentTool; |
| 188 | |
| 189 | expect(wrapped.strict).toBe(true); |
| 190 | expect(wrapped.parameters).toEqual({ |
| 191 | type: 'object', |
| 192 | properties: { |
| 193 | input: { type: 'string' }, |
| 194 | cfg: { |
| 195 | type: ['object', 'null'], |
| 196 | properties: { url: { type: 'string' }, note: { type: ['string', 'null'] } }, |
| 197 | required: ['url', 'note'], |
| 198 | additionalProperties: false, |
| 199 | }, |
| 200 | }, |
| 201 | required: ['input', 'cfg'], |
| 202 | additionalProperties: false, |
| 203 | }); |
| 204 | }); |
| 205 | |
| 206 | it('registers tools strict mode cannot express without strict', () => { |
| 207 | const strictProvider = new OpenAIAgentsProvider({ strict: true }); |
| 208 | const wrapped = strictProvider.wrapTool( |
| 209 | { |
| 210 | ...mockTool, |
| 211 | inputParameters: { |
| 212 | type: 'object', |
| 213 | properties: { headers: { type: 'object', additionalProperties: { type: 'string' } } }, |
| 214 | required: ['headers'], |
| 215 | }, |
| 216 | }, |
| 217 | mockExecuteToolFn as ExecuteToolFn |
| 218 | ) as unknown as MockedOpenAIAgentTool; |
| 219 | |
| 220 | expect(wrapped.strict).toBe(false); |
| 221 | expect(wrapped.parameters).toEqual({ |
| 222 | type: 'object', |
| 223 | properties: { headers: { type: 'object', additionalProperties: { type: 'string' } } }, |
| 224 | required: ['headers'], |
| 225 | additionalProperties: true, |
| 226 | }); |
| 227 | }); |
| 228 | |
| 229 | it('omits null arguments the tool schema rejects before executing under strict mode', async () => { |
| 230 | const strictProvider = new OpenAIAgentsProvider({ strict: true }); |
| 231 | const wrapped = strictProvider.wrapTool( |
| 232 | { |
| 233 | ...mockTool, |
| 234 | inputParameters: { |
| 235 | type: 'object', |
| 236 | properties: { |
| 237 | input: { type: 'string' }, |
| 238 | label: { type: 'string' }, |
| 239 | clearable: { type: ['string', 'null'] }, |
| 240 | }, |
| 241 | required: ['input'], |
| 242 | }, |
| 243 | }, |
| 244 | mockExecuteToolFn as ExecuteToolFn |
| 245 | ) as unknown as MockedOpenAIAgentTool; |
| 246 | |
| 247 | await wrapped.execute({ input: 'x', label: null, clearable: null }); |
| 248 | |
| 249 | expect(mockExecuteToolFn).toHaveBeenCalledWith(mockTool.slug, { |
| 250 | input: 'x', |
| 251 | clearable: null, |
| 252 | }); |
| 253 | }); |
| 254 | }); |
| 255 | |
| 256 | describe('wrapTools', () => { |
| 257 | it('should wrap multiple tools', () => { |
| 258 | const anotherTool: Tool = { |
| 259 | ...mockTool, |
| 260 | slug: 'another-tool', |
| 261 | name: 'Another Tool', |
| 262 | }; |
| 263 | const tools = [mockTool, anotherTool]; |
| 264 | |
| 265 | const wrapped = provider.wrapTools(tools, mockExecuteToolFn); |
| 266 | |
| 267 | expect(Array.isArray(wrapped)).toBe(true); |
| 268 | expect(wrapped).toHaveLength(2); |
| 269 | expect(wrapped[0]).toHaveProperty('name', 'test-tool'); |
| 270 | expect(wrapped[1]).toHaveProperty('name', 'another-tool'); |
| 271 | |
| 272 | // Verify tool was called with the right parameters for each tool |
| 273 | expect(createOpenAIAgentTool).toHaveBeenCalledTimes(2); |
| 274 | expect(createOpenAIAgentTool).toHaveBeenCalledWith({ |
| 275 | name: mockTool.slug, |
| 276 | description: mockTool.description, |
| 277 | parameters: { |
| 278 | type: 'object', |
| 279 | properties: mockTool.inputParameters?.properties || {}, |
| 280 | required: mockTool.inputParameters?.required || [], |
| 281 | additionalProperties: true, |
| 282 | }, |
| 283 | strict: false, |
| 284 | execute: expect.any(Function), |
| 285 | }); |
| 286 | expect(createOpenAIAgentTool).toHaveBeenCalledWith({ |
| 287 | name: anotherTool.slug, |
| 288 | description: anotherTool.description, |
| 289 | parameters: { |
| 290 | type: 'object', |
| 291 | properties: anotherTool.inputParameters?.properties || {}, |
| 292 | required: anotherTool.inputParameters?.required || [], |
| 293 | additionalProperties: true, |
| 294 | }, |
| 295 | strict: false, |
| 296 | execute: expect.any(Function), |
| 297 | }); |
| 298 | }); |
| 299 | |
| 300 | it('should return an empty array for empty tools array', () => { |
| 301 | const wrapped = provider.wrapTools([], mockExecuteToolFn); |
| 302 | expect(wrapped).toEqual([]); |
| 303 | expect(createOpenAIAgentTool).not.toHaveBeenCalled(); |
| 304 | }); |
| 305 | }); |
| 306 | |
| 307 | describe('MCP functionality', () => { |
| 308 | describe('wrapMcpServerResponse', () => { |
| 309 | it('should transform McpUrlResponse to standard McpServerGetResponse format', () => { |
| 310 | const mcpResponse = [ |
| 311 | { name: 'server-1', url: 'https://mcp1.example.com' }, |
| 312 | { name: 'server-2', url: 'https://mcp2.example.com' }, |
| 313 | { name: 'server-3', url: 'https://mcp3.example.com' }, |
| 314 | ]; |
| 315 | |
| 316 | const result = provider.wrapMcpServerResponse(mcpResponse); |
| 317 | |
| 318 | expect(Array.isArray(result)).toBe(true); |
| 319 | expect(result).toHaveLength(3); |
| 320 | expect(result[0]).toEqual({ |
| 321 | url: new URL('https://mcp1.example.com'), |
| 322 | name: 'server-1', |
| 323 | }); |
| 324 | expect(result[1]).toEqual({ |
| 325 | url: new URL('https://mcp2.example.com'), |
| 326 | name: 'server-2', |
| 327 | }); |
| 328 | expect(result[2]).toEqual({ |
| 329 | url: new URL('https://mcp3.example.com'), |
| 330 | name: 'server-3', |
| 331 | }); |
| 332 | }); |
| 333 | |
| 334 | it('should handle empty array', () => { |
| 335 | const mcpResponse: Array<{ name: string; url: string }> = []; |
| 336 | |
| 337 | const result = provider.wrapMcpServerResponse(mcpResponse); |
| 338 | |
| 339 | expect(Array.isArray(result)).toBe(true); |
| 340 | expect(result).toHaveLength(0); |
| 341 | }); |
| 342 | }); |
| 343 | }); |
| 344 | }); |