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, 04:45 UTC
| 1 | import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 2 | import ComposioClient, { APIError } from '@composio/client'; |
| 3 | import { mockClient } from '../utils/mocks/client.mock'; |
| 4 | import { toolMocks } from '../utils/mocks/data.mock'; |
| 5 | import { Tool, ToolListParams, ToolExecuteParams } from '../../src/types/tool.types'; |
| 6 | import { ExecuteToolFn } from '../../src/types/provider.types'; |
| 7 | import { Tools } from '../../src/models/Tools'; |
| 8 | import { |
| 9 | createTestContext, |
| 10 | setupTest, |
| 11 | mockToolExecution, |
| 12 | createSchemaModifier, |
| 13 | } from '../utils/toolExecuteUtils'; |
| 14 | import { MockProvider } from '../utils/mocks/provider.mock'; |
| 15 | import { ValidationError } from '../../src/errors/ValidationErrors'; |
| 16 | import { |
| 17 | ComposioToolExecutionError, |
| 18 | ComposioToolFetchError, |
| 19 | ComposioToolNotFoundError, |
| 20 | ComposioToolSchemaInvalidError, |
| 21 | } from '../../src/errors/ToolErrors'; |
| 22 | import logger from '../../src/utils/logger'; |
| 23 | |
| 24 | // Minimal structural shape for a ComposioError-like value (possibly wrapping |
| 25 | // another error as its `cause`), used to narrow `catch (error: unknown)` |
| 26 | // blocks that assert on `.code` / `.message` / `.possibleFixes`. |
| 27 | type ErrorWithPossibleFixes = { |
| 28 | code?: string; |
| 29 | message?: string; |
| 30 | possibleFixes?: string[]; |
| 31 | cause?: { |
| 32 | code?: string; |
| 33 | message?: string; |
| 34 | possibleFixes?: string[]; |
| 35 | }; |
| 36 | }; |
| 37 | |
| 38 | describe('Tools', () => { |
| 39 | const context = createTestContext(); |
| 40 | setupTest(context); |
| 41 | |
| 42 | describe('constructor', () => { |
| 43 | it('should throw an error if client is not provided', () => { |
| 44 | expect(() => new Tools(null as unknown, { provider: context.mockProvider })).toThrow( |
| 45 | 'ComposioClient is required' |
| 46 | ); |
| 47 | }); |
| 48 | |
| 49 | it('should throw an error if provider is not provided', () => { |
| 50 | expect(() => new Tools(mockClient, null as unknown)).toThrow( |
| 51 | 'Provider not passed into Tools instance' |
| 52 | ); |
| 53 | }); |
| 54 | |
| 55 | it('should create an instance successfully with valid parameters', () => { |
| 56 | expect(context.tools).toBeInstanceOf(Tools); |
| 57 | }); |
| 58 | }); |
| 59 | |
| 60 | describe('getRawComposioTools', () => { |
| 61 | it('should fetch tools from the API', async () => { |
| 62 | mockClient.tools.list.mockResolvedValueOnce({ |
| 63 | items: [toolMocks.rawTool], |
| 64 | totalPages: 1, |
| 65 | }); |
| 66 | |
| 67 | const result = await context.tools.getRawComposioTools({ tools: ['TEST_TOOL'] }); |
| 68 | |
| 69 | expect(mockClient.tools.list).toHaveBeenCalledTimes(1); |
| 70 | expect(result).toHaveLength(1); |
| 71 | expect(result[0].slug).toEqual(toolMocks.transformedTool.slug); |
| 72 | expect(result[0].inputParameters).toBeDefined(); |
| 73 | }); |
| 74 | |
| 75 | it('should handle query parameters correctly', async () => { |
| 76 | const query = { |
| 77 | tools: ['TOOL1', 'TOOL2'], |
| 78 | }; |
| 79 | |
| 80 | mockClient.tools.list.mockResolvedValueOnce({ |
| 81 | items: [toolMocks.rawTool], |
| 82 | totalPages: 1, |
| 83 | }); |
| 84 | |
| 85 | await context.tools.getRawComposioTools(query); |
| 86 | |
| 87 | expect(mockClient.tools.list).toHaveBeenCalledWith( |
| 88 | { |
| 89 | tool_slugs: 'TOOL1,TOOL2', |
| 90 | limit: 9999, |
| 91 | toolkit_versions: 'latest', |
| 92 | }, |
| 93 | undefined |
| 94 | ); |
| 95 | }); |
| 96 | |
| 97 | it('should handle toolkit query parameters correctly', async () => { |
| 98 | const query = { |
| 99 | toolkits: ['github'], |
| 100 | }; |
| 101 | |
| 102 | mockClient.tools.list.mockResolvedValueOnce({ |
| 103 | items: [toolMocks.rawTool], |
| 104 | totalPages: 1, |
| 105 | }); |
| 106 | |
| 107 | await context.tools.getRawComposioTools(query); |
| 108 | |
| 109 | expect(mockClient.tools.list).toHaveBeenCalledWith( |
| 110 | { |
| 111 | toolkit_slug: 'github', |
| 112 | important: 'true', |
| 113 | toolkit_versions: 'latest', |
| 114 | }, |
| 115 | undefined |
| 116 | ); |
| 117 | }); |
| 118 | |
| 119 | it('should respect explicit important=false to opt-out', async () => { |
| 120 | const query = { |
| 121 | toolkits: ['github'], |
| 122 | important: false, |
| 123 | limit: 10, |
| 124 | }; |
| 125 | |
| 126 | mockClient.tools.list.mockResolvedValueOnce({ |
| 127 | items: [toolMocks.rawTool], |
| 128 | totalPages: 1, |
| 129 | }); |
| 130 | |
| 131 | await context.tools.getRawComposioTools(query); |
| 132 | |
| 133 | expect(mockClient.tools.list).toHaveBeenCalledWith( |
| 134 | { |
| 135 | toolkit_slug: 'github', |
| 136 | limit: 10, |
| 137 | toolkit_versions: 'latest', |
| 138 | }, |
| 139 | undefined |
| 140 | ); |
| 141 | }); |
| 142 | |
| 143 | it('should handle toolkit search parameters correctly', async () => { |
| 144 | const query = { |
| 145 | toolkits: ['github'], |
| 146 | search: 'test', |
| 147 | limit: 10, |
| 148 | }; |
| 149 | |
| 150 | mockClient.tools.list.mockResolvedValueOnce({ |
| 151 | items: [toolMocks.rawTool], |
| 152 | totalPages: 1, |
| 153 | }); |
| 154 | |
| 155 | await context.tools.getRawComposioTools(query); |
| 156 | |
| 157 | expect(mockClient.tools.list).toHaveBeenCalledWith( |
| 158 | { |
| 159 | toolkit_slug: 'github', |
| 160 | limit: 10, |
| 161 | query: 'test', |
| 162 | toolkit_versions: 'latest', |
| 163 | }, |
| 164 | undefined |
| 165 | ); |
| 166 | }); |
| 167 | |
| 168 | it('should handle toolkit scopes parameters correctly', async () => { |
| 169 | const query: ToolListParams = { |
| 170 | toolkits: ['todoist'], |
| 171 | scopes: ['task:add', 'task:read'], |
| 172 | }; |
| 173 | |
| 174 | mockClient.tools.list.mockResolvedValueOnce({ |
| 175 | items: [toolMocks.rawTool], |
| 176 | totalPages: 1, |
| 177 | }); |
| 178 | |
| 179 | await context.tools.getRawComposioTools(query); |
| 180 | |
| 181 | expect(mockClient.tools.list).toHaveBeenCalledWith( |
| 182 | { |
| 183 | toolkit_slug: 'todoist', |
| 184 | important: 'true', |
| 185 | scopes: ['task:add', 'task:read'], |
| 186 | toolkit_versions: 'latest', |
| 187 | }, |
| 188 | undefined |
| 189 | ); |
| 190 | }); |
| 191 | |
| 192 | it('should handle toolkit scopes with search parameters correctly', async () => { |
| 193 | const query: ToolListParams = { |
| 194 | toolkits: ['todoist'], |
| 195 | scopes: ['task:add'], |
| 196 | search: 'add task', |
| 197 | limit: 10, |
| 198 | }; |
| 199 | |
| 200 | mockClient.tools.list.mockResolvedValueOnce({ |
| 201 | items: [toolMocks.rawTool], |
| 202 | totalPages: 1, |
| 203 | }); |
| 204 | |
| 205 | await context.tools.getRawComposioTools(query); |
| 206 | |
| 207 | expect(mockClient.tools.list).toHaveBeenCalledWith( |
| 208 | { |
| 209 | toolkit_slug: 'todoist', |
| 210 | limit: 10, |
| 211 | query: 'add task', |
| 212 | scopes: ['task:add'], |
| 213 | toolkit_versions: 'latest', |
| 214 | }, |
| 215 | undefined |
| 216 | ); |
| 217 | }); |
| 218 | |
| 219 | it('should respect explicit important=true', async () => { |
| 220 | const query: ToolListParams = { |
| 221 | toolkits: ['github'], |
| 222 | important: true, |
| 223 | limit: 10, |
| 224 | }; |
| 225 | |
| 226 | mockClient.tools.list.mockResolvedValueOnce({ |
| 227 | items: [toolMocks.rawTool], |
| 228 | totalPages: 1, |
| 229 | }); |
| 230 | |
| 231 | await context.tools.getRawComposioTools(query); |
| 232 | |
| 233 | expect(mockClient.tools.list).toHaveBeenCalledWith( |
| 234 | { |
| 235 | toolkit_slug: 'github', |
| 236 | important: 'true', |
| 237 | limit: 10, |
| 238 | toolkit_versions: 'latest', |
| 239 | }, |
| 240 | undefined |
| 241 | ); |
| 242 | }); |
| 243 | |
| 244 | it('should NOT auto-apply important when tags are passed with values', async () => { |
| 245 | const query: ToolListParams = { |
| 246 | toolkits: ['github'], |
| 247 | tags: ['important', 'custom'], |
| 248 | limit: 10, |
| 249 | }; |
| 250 | |
| 251 | mockClient.tools.list.mockResolvedValueOnce({ |
| 252 | items: [toolMocks.rawTool], |
| 253 | totalPages: 1, |
| 254 | }); |
| 255 | |
| 256 | await context.tools.getRawComposioTools(query); |
| 257 | |
| 258 | expect(mockClient.tools.list).toHaveBeenCalledWith( |
| 259 | { |
| 260 | toolkit_slug: 'github', |
| 261 | tags: ['important', 'custom'], |
| 262 | limit: 10, |
| 263 | toolkit_versions: 'latest', |
| 264 | }, |
| 265 | undefined |
| 266 | ); |
| 267 | // Should NOT include important: 'true' when tags are provided |
| 268 | }); |
| 269 | |
| 270 | it('should NOT auto-apply important when tags are passed as empty array', async () => { |
| 271 | const query: ToolListParams = { |
| 272 | toolkits: ['github'], |
| 273 | tags: [], |
| 274 | }; |
| 275 | |
| 276 | mockClient.tools.list.mockResolvedValueOnce({ |
| 277 | items: [toolMocks.rawTool], |
| 278 | totalPages: 1, |
| 279 | }); |
| 280 | |
| 281 | await context.tools.getRawComposioTools(query); |
| 282 | |
| 283 | expect(mockClient.tools.list).toHaveBeenCalledWith( |
| 284 | { |
| 285 | toolkit_slug: 'github', |
| 286 | tags: [], |
| 287 | toolkit_versions: 'latest', |
| 288 | }, |
| 289 | undefined |
| 290 | ); |
| 291 | // Should NOT include important: 'true' when tags array is present (even if empty) |
| 292 | }); |
| 293 | |
| 294 | it('should NOT auto-apply important when limit is provided', async () => { |
| 295 | const query: ToolListParams = { |
| 296 | toolkits: ['github'], |
| 297 | limit: 10, |
| 298 | }; |
| 299 | |
| 300 | mockClient.tools.list.mockResolvedValueOnce({ |
| 301 | items: [toolMocks.rawTool], |
| 302 | totalPages: 1, |
| 303 | }); |
| 304 | |
| 305 | await context.tools.getRawComposioTools(query); |
| 306 | |
| 307 | expect(mockClient.tools.list).toHaveBeenCalledWith( |
| 308 | { |
| 309 | toolkit_slug: 'github', |
| 310 | limit: 10, |
| 311 | toolkit_versions: 'latest', |
| 312 | }, |
| 313 | undefined |
| 314 | ); |
| 315 | // Should NOT include important: 'true' when limit is provided |
| 316 | }); |
| 317 | |
| 318 | it('should throw a validation error when scopes are provided without toolkits', async () => { |
| 319 | const invalidQuery = { |
| 320 | scopes: ['task:add'], |
| 321 | } as unknown; |
| 322 | |
| 323 | await expect(context.tools.getRawComposioTools(invalidQuery)).rejects.toThrow( |
| 324 | 'Invalid tool list parameters' |
| 325 | ); |
| 326 | }); |
| 327 | |
| 328 | it('should transform tool case correctly', async () => { |
| 329 | mockClient.tools.list.mockResolvedValueOnce({ |
| 330 | items: [toolMocks.rawTool], |
| 331 | totalPages: 1, |
| 332 | }); |
| 333 | |
| 334 | const result = await context.tools.getRawComposioTools({ tools: ['TEST_TOOL'] }); |
| 335 | |
| 336 | expect(result[0].inputParameters).toEqual(toolMocks.transformedTool.inputParameters); |
| 337 | expect(result[0].outputParameters).toEqual(toolMocks.transformedTool.outputParameters); |
| 338 | }); |
| 339 | |
| 340 | it('should apply schema modifiers when provided', async () => { |
| 341 | const schemaModifier = createSchemaModifier({ |
| 342 | description: 'Modified description', |
| 343 | }); |
| 344 | |
| 345 | mockClient.tools.list.mockResolvedValueOnce({ |
| 346 | items: [toolMocks.rawTool], |
| 347 | totalPages: 1, |
| 348 | }); |
| 349 | |
| 350 | const result = await context.tools.getRawComposioTools( |
| 351 | { tools: ['TEST_TOOL'] }, |
| 352 | { modifySchema: schemaModifier } |
| 353 | ); |
| 354 | |
| 355 | expect(schemaModifier).toHaveBeenCalled(); |
| 356 | expect(result[0].description).toEqual('Modified description'); |
| 357 | }); |
| 358 | |
| 359 | it('should throw an error if schema modifier is not a function', async () => { |
| 360 | const invalidModifier = 'not a function' as unknown; |
| 361 | |
| 362 | mockClient.tools.list.mockResolvedValueOnce({ |
| 363 | items: [toolMocks.rawTool], |
| 364 | totalPages: 1, |
| 365 | }); |
| 366 | |
| 367 | await expect( |
| 368 | context.tools.getRawComposioTools( |
| 369 | { |
| 370 | toolkits: ['invalid'], |
| 371 | }, |
| 372 | { modifySchema: invalidModifier } |
| 373 | ) |
| 374 | ).rejects.toThrow('Invalid schema modifier. Not a function.'); |
| 375 | }); |
| 376 | |
| 377 | it('should throw a validation error when both tools and toolkits are provided', async () => { |
| 378 | const invalidQuery = { |
| 379 | tools: ['TOOL1'], |
| 380 | toolkits: ['github'], |
| 381 | } as unknown; |
| 382 | |
| 383 | await expect(context.tools.getRawComposioTools(invalidQuery)).rejects.toThrow( |
| 384 | 'Invalid tool list parameters' |
| 385 | ); |
| 386 | }); |
| 387 | |
| 388 | it('should throw a validation error when no required parameters are provided', async () => { |
| 389 | const emptyQuery = {} as unknown; |
| 390 | |
| 391 | await expect(context.tools.getRawComposioTools(emptyQuery)).rejects.toThrow(ValidationError); |
| 392 | }); |
| 393 | |
| 394 | // One tool's malformed schema used to fail the whole call (#4757). |
| 395 | it('should tolerate a tool whose inputParameters holds a boolean schema node', async () => { |
| 396 | mockClient.tools.list.mockResolvedValueOnce({ |
| 397 | items: [ |
| 398 | { |
| 399 | ...toolMocks.rawTool, |
| 400 | input_parameters: { |
| 401 | type: 'object', |
| 402 | properties: { |
| 403 | params: { type: 'object', items: true }, |
| 404 | }, |
| 405 | }, |
| 406 | }, |
| 407 | ], |
| 408 | totalPages: 1, |
| 409 | }); |
| 410 | |
| 411 | const result = await context.tools.getRawComposioTools({ tools: ['COMPOSIO_TOOL'] }); |
| 412 | |
| 413 | expect(result).toHaveLength(1); |
| 414 | expect(result[0].inputParameters).toEqual({ |
| 415 | type: 'object', |
| 416 | properties: { |
| 417 | params: { type: 'object', items: {} }, |
| 418 | }, |
| 419 | }); |
| 420 | }); |
| 421 | |
| 422 | it('should express a false boolean node as not-anything, not anything', async () => { |
| 423 | mockClient.tools.list.mockResolvedValueOnce({ |
| 424 | items: [ |
| 425 | { |
| 426 | ...toolMocks.rawTool, |
| 427 | input_parameters: { |
| 428 | type: 'object', |
| 429 | properties: { |
| 430 | // `false` accepts nothing; coercing it to `{}` would invert |
| 431 | // the rule and accept everything. |
| 432 | forbidden: { type: 'array', items: false }, |
| 433 | anything: { type: 'array', items: true }, |
| 434 | }, |
| 435 | }, |
| 436 | }, |
| 437 | ], |
| 438 | totalPages: 1, |
| 439 | }); |
| 440 | |
| 441 | const result = await context.tools.getRawComposioTools({ tools: ['COMPOSIO_TOOL'] }); |
| 442 | |
| 443 | expect(result).toHaveLength(1); |
| 444 | expect(result[0].inputParameters).toEqual({ |
| 445 | type: 'object', |
| 446 | properties: { |
| 447 | forbidden: { type: 'array', items: { not: {} } }, |
| 448 | anything: { type: 'array', items: {} }, |
| 449 | }, |
| 450 | }); |
| 451 | }); |
| 452 | |
| 453 | it('should tolerate a draft-04 boolean exclusiveMinimum', async () => { |
| 454 | mockClient.tools.list.mockResolvedValueOnce({ |
| 455 | items: [ |
| 456 | { |
| 457 | ...toolMocks.rawTool, |
| 458 | input_parameters: { |
| 459 | type: 'object', |
| 460 | properties: { |
| 461 | amount: { type: 'number', minimum: 5, exclusiveMinimum: true }, |
| 462 | }, |
| 463 | }, |
| 464 | }, |
| 465 | ], |
| 466 | totalPages: 1, |
| 467 | }); |
| 468 | |
| 469 | const result = await context.tools.getRawComposioTools({ tools: ['COMPOSIO_TOOL'] }); |
| 470 | |
| 471 | expect(result).toHaveLength(1); |
| 472 | expect(result[0].inputParameters).toEqual({ |
| 473 | type: 'object', |
| 474 | properties: { |
| 475 | amount: { type: 'number', minimum: 5, exclusiveMinimum: 5 }, |
| 476 | }, |
| 477 | }); |
| 478 | }); |
| 479 | |
| 480 | it('should tolerate a bare combinator root without a type', async () => { |
| 481 | mockClient.tools.list.mockResolvedValueOnce({ |
| 482 | items: [ |
| 483 | { |
| 484 | ...toolMocks.rawTool, |
| 485 | input_parameters: { |
| 486 | oneOf: [{ type: 'object', properties: { a: { type: 'string' } } }], |
| 487 | }, |
| 488 | }, |
| 489 | ], |
| 490 | totalPages: 1, |
| 491 | }); |
| 492 | |
| 493 | const result = await context.tools.getRawComposioTools({ tools: ['COMPOSIO_TOOL'] }); |
| 494 | |
| 495 | expect(result).toHaveLength(1); |
| 496 | expect(result[0].inputParameters).toEqual({ |
| 497 | type: 'object', |
| 498 | oneOf: [{ type: 'object', properties: { a: { type: 'string' } } }], |
| 499 | }); |
| 500 | }); |
| 501 | |
| 502 | it('should return the conforming tools of a batch when one schema is malformed', async () => { |
| 503 | mockClient.tools.list.mockResolvedValueOnce({ |
| 504 | items: [ |
| 505 | toolMocks.rawTool, |
| 506 | { |
| 507 | ...toolMocks.customTool, |
| 508 | input_parameters: { |
| 509 | type: 'object', |
| 510 | properties: { filters: { type: 'array', items: true } }, |
| 511 | }, |
| 512 | }, |
| 513 | ], |
| 514 | totalPages: 1, |
| 515 | }); |
| 516 | |
| 517 | const result = await context.tools.getRawComposioTools({ toolkits: ['test-toolkit'] }); |
| 518 | |
| 519 | expect(result.map(tool => tool.slug)).toEqual(['COMPOSIO_TOOL', 'CUSTOM_TOOL']); |
| 520 | }); |
| 521 | |
| 522 | it('should keep a tool whose output schema cannot be repaired, minus that schema', async () => { |
| 523 | mockClient.tools.list.mockResolvedValueOnce({ |
| 524 | items: [ |
| 525 | { |
| 526 | ...toolMocks.rawTool, |
| 527 | output_parameters: { |
| 528 | type: 'object', |
| 529 | properties: { query: { type: 'not-a-json-schema-type' } }, |
| 530 | }, |
| 531 | }, |
| 532 | ], |
| 533 | totalPages: 1, |
| 534 | }); |
| 535 | |
| 536 | const result = await context.tools.getRawComposioTools({ tools: ['COMPOSIO_TOOL'] }); |
| 537 | |
| 538 | expect(result).toHaveLength(1); |
| 539 | expect(result[0].slug).toEqual('COMPOSIO_TOOL'); |
| 540 | expect(result[0].outputParameters).toBeUndefined(); |
| 541 | expect(result[0].inputParameters).toBeDefined(); |
| 542 | }); |
| 543 | |
| 544 | it('should skip a tool whose input schema cannot be repaired', async () => { |
| 545 | // An unrepairable input schema cannot degrade to "no schema": providers |
| 546 | // build their call schema from it (@composio/langchain's wrapTool throws |
| 547 | // "Tool input parameters are not defined"), so the tool is skipped and |
| 548 | // the rest of the batch still returns. |
| 549 | mockClient.tools.list.mockResolvedValueOnce({ |
| 550 | items: [ |
| 551 | toolMocks.rawTool, |
| 552 | { |
| 553 | ...toolMocks.customTool, |
| 554 | input_parameters: { |
| 555 | type: 'object', |
| 556 | properties: { query: { type: 'not-a-json-schema-type' } }, |
| 557 | }, |
| 558 | }, |
| 559 | ], |
| 560 | totalPages: 1, |
| 561 | }); |
| 562 | |
| 563 | const result = await context.tools.getRawComposioTools({ toolkits: ['test-toolkit'] }); |
| 564 | |
| 565 | expect(result.map(tool => tool.slug)).toEqual(['COMPOSIO_TOOL']); |
| 566 | }); |
| 567 | |
| 568 | it('should still throw for a broken identity when a schema is also malformed', async () => { |
| 569 | // Skipping the tool must not swallow a failure that is not about the |
| 570 | // parameters: a tool with no name is broken data either way. |
| 571 | mockClient.tools.list.mockResolvedValueOnce({ |
| 572 | items: [ |
| 573 | { |
| 574 | ...toolMocks.rawTool, |
| 575 | name: undefined as unknown as string, |
| 576 | input_parameters: { |
| 577 | type: 'object', |
| 578 | properties: { query: { type: 'not-a-json-schema-type' } }, |
| 579 | }, |
| 580 | }, |
| 581 | ], |
| 582 | totalPages: 1, |
| 583 | }); |
| 584 | |
| 585 | await expect( |
| 586 | context.tools.getRawComposioTools({ toolkits: ['test-toolkit'] }) |
| 587 | ).rejects.toThrow(); |
| 588 | }); |
| 589 | |
| 590 | it('should not let a schema property name forge lines in the warning', async () => { |
| 591 | const warn = vi.spyOn(logger, 'warn').mockImplementation(() => undefined); |
| 592 | mockClient.tools.list.mockResolvedValueOnce({ |
| 593 | items: [ |
| 594 | { |
| 595 | ...toolMocks.customTool, |
| 596 | slug: 'FORGERY_TOOL', |
| 597 | input_parameters: { |
| 598 | type: 'object', |
| 599 | properties: { |
| 600 | 'evil\n[ERROR] security scan passed': { type: 'not-a-json-schema-type' }, |
| 601 | }, |
| 602 | }, |
| 603 | }, |
| 604 | ], |
| 605 | totalPages: 1, |
| 606 | }); |
| 607 | |
| 608 | await context.tools.getRawComposioTools({ toolkits: ['test-toolkit'] }); |
| 609 | |
| 610 | expect(warn).toHaveBeenCalledTimes(1); |
| 611 | const message = String(warn.mock.calls[0][0]); |
| 612 | // The forged line must not appear as its own line. |
| 613 | expect(message.split('\n')).toHaveLength(1); |
| 614 | expect(message).not.toContain('\n[ERROR]'); |
| 615 | warn.mockRestore(); |
| 616 | }); |
| 617 | }); |
| 618 | |
| 619 | describe('getRawToolRouterSessionTools', () => { |
| 620 | it('should fetch all paginated session tool pages', async () => { |
| 621 | mockClient.toolRouter.session.tools |
| 622 | .mockResolvedValueOnce({ |
| 623 | items: [{ ...toolMocks.rawTool, slug: 'FIRST_TOOL' }], |
| 624 | next_cursor: 'next_page', |
| 625 | }) |
| 626 | .mockResolvedValueOnce({ |
| 627 | items: [{ ...toolMocks.rawTool, slug: 'SECOND_TOOL' }], |
| 628 | next_cursor: null, |
| 629 | }); |
| 630 | |
| 631 | const result = await context.tools.getRawToolRouterSessionTools('session_123'); |
| 632 | |
| 633 | expect(result.map(tool => tool.slug)).toEqual(['FIRST_TOOL', 'SECOND_TOOL']); |
| 634 | expect(mockClient.toolRouter.session.tools).toHaveBeenNthCalledWith( |
| 635 | 1, |
| 636 | 'session_123', |
| 637 | { |
| 638 | limit: 500, |
| 639 | }, |
| 640 | undefined |
| 641 | ); |
| 642 | expect(mockClient.toolRouter.session.tools).toHaveBeenNthCalledWith( |
| 643 | 2, |
| 644 | 'session_123', |
| 645 | { |
| 646 | limit: 500, |
| 647 | cursor: 'next_page', |
| 648 | }, |
| 649 | undefined |
| 650 | ); |
| 651 | }); |
| 652 | |
| 653 | it('should pass unknown toolkit metadata to schema modifiers', async () => { |
| 654 | const toolWithoutToolkit = { |
| 655 | ...toolMocks.rawTool, |
| 656 | toolkit: undefined, |
| 657 | }; |
| 658 | const modifySchema = vi.fn(({ schema }) => schema); |
| 659 | mockClient.toolRouter.session.tools.mockResolvedValueOnce({ |
| 660 | items: [toolWithoutToolkit], |
| 661 | next_cursor: null, |
| 662 | }); |
| 663 | |
| 664 | await context.tools.getRawToolRouterSessionTools('session_123', { modifySchema }); |
| 665 | |
| 666 | expect(modifySchema).toHaveBeenCalledWith({ |
| 667 | toolSlug: toolMocks.rawTool.slug, |
| 668 | toolkitSlug: 'unknown', |
| 669 | schema: expect.objectContaining({ slug: toolMocks.rawTool.slug }), |
| 670 | }); |
| 671 | }); |
| 672 | }); |
| 673 | |
| 674 | describe('getRawComposioToolBySlug', () => { |
| 675 | it('should report an unusable schema as a schema error, not a missing tool', async () => { |
| 676 | // The tool exists; only its schema is broken. Callers that catch |
| 677 | // TOOL_NOT_FOUND would otherwise hide a real tool as missing. |
| 678 | const slug = 'SCHEMA_BROKEN_TOOL'; |
| 679 | mockClient.tools.retrieve.mockResolvedValueOnce({ |
| 680 | ...toolMocks.customTool, |
| 681 | slug, |
| 682 | input_parameters: { |
| 683 | type: 'object', |
| 684 | properties: { query: { type: 'not-a-json-schema-type' } }, |
| 685 | }, |
| 686 | }); |
| 687 | |
| 688 | await expect(context.tools.getRawComposioToolBySlug(slug)).rejects.toThrow( |
| 689 | ComposioToolSchemaInvalidError |
| 690 | ); |
| 691 | }); |
| 692 | |
| 693 | it('should fetch a tool by slug from the API', async () => { |
| 694 | const slug = 'TOOL_SLUG'; |
| 695 | |
| 696 | mockClient.tools.retrieve.mockResolvedValueOnce(toolMocks.rawTool); |
| 697 | |
| 698 | const result = await context.tools.getRawComposioToolBySlug(slug); |
| 699 | |
| 700 | expect(mockClient.tools.retrieve).toHaveBeenCalledWith( |
| 701 | slug, |
| 702 | { toolkit_versions: 'latest' }, |
| 703 | undefined |
| 704 | ); |
| 705 | expect(result.slug).toEqual(toolMocks.transformedTool.slug); |
| 706 | }); |
| 707 | |
| 708 | it('should throw ComposioToolNotFoundError when the API returns 404', async () => { |
| 709 | const slug = 'NONEXISTENT_TOOL'; |
| 710 | const notFound = new ComposioClient.NotFoundError(404, undefined, undefined, new Headers()); |
| 711 | |
| 712 | mockClient.tools.retrieve.mockRejectedValueOnce(notFound); |
| 713 | |
| 714 | const error = await context.tools.getRawComposioToolBySlug(slug).catch(e => e); |
| 715 | |
| 716 | expect(error).toBeInstanceOf(ComposioToolNotFoundError); |
| 717 | expect(error.message).toBe(`Tool with slug ${slug} not found`); |
| 718 | expect(error.cause).toBe(notFound); |
| 719 | }); |
| 720 | |
| 721 | it('should throw ComposioToolNotFoundError when the API returns 400', async () => { |
| 722 | const slug = 'malformed slug'; |
| 723 | const badRequest = new ComposioClient.BadRequestError( |
| 724 | 400, |
| 725 | undefined, |
| 726 | undefined, |
| 727 | new Headers() |
| 728 | ); |
| 729 | |
| 730 | mockClient.tools.retrieve.mockRejectedValueOnce(badRequest); |
| 731 | |
| 732 | const error = await context.tools.getRawComposioToolBySlug(slug).catch(e => e); |
| 733 | |
| 734 | expect(error).toBeInstanceOf(ComposioToolNotFoundError); |
| 735 | expect(error.cause).toBe(badRequest); |
| 736 | }); |
| 737 | |
| 738 | it('should not report an invalid API key (401) as tool not found', async () => { |
| 739 | const slug = 'SLACK_FETCH_CONVERSATION_HISTORY'; |
| 740 | const unauthorized = new ComposioClient.AuthenticationError( |
| 741 | 401, |
| 742 | { error: { message: 'Invalid API key', code: 801, slug: 'UNAUTHORIZED', status: 401 } }, |
| 743 | undefined, |
| 744 | new Headers() |
| 745 | ); |
| 746 | |
| 747 | mockClient.tools.retrieve.mockRejectedValueOnce(unauthorized); |
| 748 | |
| 749 | const error = await context.tools.getRawComposioToolBySlug(slug).catch(e => e); |
| 750 | |
| 751 | expect(error).toBeInstanceOf(ComposioToolFetchError); |
| 752 | expect(error).not.toBeInstanceOf(ComposioToolNotFoundError); |
| 753 | expect(error.name).toBe('ComposioToolFetchError'); |
| 754 | expect(error.message).toBe(`Unable to retrieve tool with slug ${slug}: Invalid API key`); |
| 755 | expect(error.statusCode).toBe(401); |
| 756 | expect(error.apiErrorCode).toBe(801); |
| 757 | expect(error.cause).toBe(unauthorized); |
| 758 | expect(error.cause.status).toBe(401); |
| 759 | }); |
| 760 | |
| 761 | it('should wrap non-API failures in ComposioToolFetchError', async () => { |
| 762 | const slug = 'TOOL_SLUG'; |
| 763 | const networkError = new Error('socket hang up'); |
| 764 | |
| 765 | mockClient.tools.retrieve.mockRejectedValueOnce(networkError); |
| 766 | |
| 767 | const error = await context.tools.getRawComposioToolBySlug(slug).catch(e => e); |
| 768 | |
| 769 | expect(error).toBeInstanceOf(ComposioToolFetchError); |
| 770 | expect(error.cause).toBe(networkError); |
| 771 | }); |
| 772 | |
| 773 | it('should apply schema modifiers when provided', async () => { |
| 774 | const slug = 'TOOL_SLUG'; |
| 775 | const schemaModifier = createSchemaModifier({ |
| 776 | description: 'Modified description', |
| 777 | }); |
| 778 | |
| 779 | mockClient.tools.retrieve.mockResolvedValueOnce(toolMocks.rawTool); |
| 780 | |
| 781 | const result = await context.tools.getRawComposioToolBySlug(slug, { |
| 782 | modifySchema: schemaModifier, |
| 783 | }); |
| 784 | |
| 785 | expect(schemaModifier).toHaveBeenCalled(); |
| 786 | expect(result.description).toEqual('Modified description'); |
| 787 | }); |
| 788 | |
| 789 | it('should use version parameter when provided (explicit version string)', async () => { |
| 790 | const slug = 'GITHUB_CREATE_ISSUE'; |
| 791 | const explicitVersion = '20250909_00'; |
| 792 | |
| 793 | mockClient.tools.retrieve.mockResolvedValueOnce(toolMocks.rawTool); |
| 794 | |
| 795 | await context.tools.getRawComposioToolBySlug(slug, { |
| 796 | version: explicitVersion, |
| 797 | }); |
| 798 | |
| 799 | // Should use 'version' param, not 'toolkit_versions' |
| 800 | expect(mockClient.tools.retrieve).toHaveBeenCalledWith( |
| 801 | slug, |
| 802 | { |
| 803 | version: explicitVersion, |
| 804 | }, |
| 805 | undefined |
| 806 | ); |
| 807 | }); |
| 808 | |
| 809 | it('should use version parameter when provided (latest string)', async () => { |
| 810 | const slug = 'GITHUB_CREATE_ISSUE'; |
| 811 | |
| 812 | mockClient.tools.retrieve.mockResolvedValueOnce(toolMocks.rawTool); |
| 813 | |
| 814 | await context.tools.getRawComposioToolBySlug(slug, { |
| 815 | version: 'latest', |
| 816 | }); |
| 817 | |
| 818 | // Should use 'version' param with 'latest' |
| 819 | expect(mockClient.tools.retrieve).toHaveBeenCalledWith( |
| 820 | slug, |
| 821 | { |
| 822 | version: 'latest', |
| 823 | }, |
| 824 | undefined |
| 825 | ); |
| 826 | }); |
| 827 | |
| 828 | it('should use version parameter when provided (version mapping object)', async () => { |
| 829 | const slug = 'GITHUB_CREATE_ISSUE'; |
| 830 | const versionMapping = { github: '20250909_00', slack: 'latest' }; |
| 831 | |
| 832 | mockClient.tools.retrieve.mockResolvedValueOnce(toolMocks.rawTool); |
| 833 | |
| 834 | // Create a Tools instance with SDK-level toolkitVersions as mapping object |
| 835 | const toolsWithVersions = new Tools(mockClient, { |
| 836 | provider: context.mockProvider, |
| 837 | toolkitVersions: versionMapping, |
| 838 | }); |
| 839 | |
| 840 | await toolsWithVersions.getRawComposioToolBySlug(slug); |
| 841 | |
| 842 | // Should use 'toolkit_versions' param with mapping object (not 'version') |
| 843 | expect(mockClient.tools.retrieve).toHaveBeenCalledWith( |
| 844 | slug, |
| 845 | { |
| 846 | toolkit_versions: versionMapping, |
| 847 | }, |
| 848 | undefined |
| 849 | ); |
| 850 | }); |
| 851 | |
| 852 | it('should fallback to SDK toolkitVersions when version not provided', async () => { |
| 853 | const slug = 'GITHUB_CREATE_ISSUE'; |
| 854 | |
| 855 | mockClient.tools.retrieve.mockResolvedValueOnce(toolMocks.rawTool); |
| 856 | |
| 857 | // Call without version option |
| 858 | await context.tools.getRawComposioToolBySlug(slug); |
| 859 | |
| 860 | // Should use 'toolkit_versions' param with SDK-level config |
| 861 | expect(mockClient.tools.retrieve).toHaveBeenCalledWith( |
| 862 | slug, |
| 863 | { |
| 864 | toolkit_versions: 'latest', |
| 865 | }, |
| 866 | undefined |
| 867 | ); |
| 868 | }); |
| 869 | |
| 870 | it('should prioritize explicit version over SDK toolkitVersions', async () => { |
| 871 | const slug = 'GITHUB_CREATE_ISSUE'; |
| 872 | const explicitVersion = '20250909_00'; |
| 873 | |
| 874 | // Create a Tools instance with SDK-level toolkitVersions |
| 875 | const toolsWithVersions = new Tools(mockClient, { |
| 876 | provider: context.mockProvider, |
| 877 | toolkitVersions: { github: explicitVersion }, |
| 878 | }); |
| 879 | |
| 880 | mockClient.tools.retrieve.mockResolvedValueOnce(toolMocks.rawTool); |
| 881 | |
| 882 | await toolsWithVersions.getRawComposioToolBySlug(slug, { |
| 883 | version: explicitVersion, |
| 884 | }); |
| 885 | |
| 886 | // Explicit version should take precedence, using 'version' param |
| 887 | expect(mockClient.tools.retrieve).toHaveBeenCalledWith( |
| 888 | slug, |
| 889 | { |
| 890 | version: explicitVersion, |
| 891 | }, |
| 892 | undefined |
| 893 | ); |
| 894 | }); |
| 895 | |
| 896 | it('should support combining version and modifySchema options', async () => { |
| 897 | const slug = 'GITHUB_CREATE_ISSUE'; |
| 898 | const explicitVersion = '20250909_00'; |
| 899 | const schemaModifier = createSchemaModifier({ |
| 900 | description: 'Modified description', |
| 901 | }); |
| 902 | |
| 903 | mockClient.tools.retrieve.mockResolvedValueOnce(toolMocks.rawTool); |
| 904 | |
| 905 | const result = await context.tools.getRawComposioToolBySlug(slug, { |
| 906 | version: explicitVersion, |
| 907 | modifySchema: schemaModifier, |
| 908 | }); |
| 909 | |
| 910 | // Should use version param |
| 911 | expect(mockClient.tools.retrieve).toHaveBeenCalledWith( |
| 912 | slug, |
| 913 | { |
| 914 | version: explicitVersion, |
| 915 | }, |
| 916 | undefined |
| 917 | ); |
| 918 | // Should also apply schema modifier |
| 919 | expect(schemaModifier).toHaveBeenCalled(); |
| 920 | expect(result.description).toEqual('Modified description'); |
| 921 | }); |
| 922 | }); |
| 923 | |
| 924 | // MCP-backed toolkits (granola_mcp, apify_mcp, tavily_mcp, …) have no |
| 925 | // declared output schema, so the Composio API returns |
| 926 | // output_parameters: {}. Before the fix, transformToolCases ran that |
| 927 | // through ParametersSchema (which requires { type: 'object', properties: |
| 928 | // {...} }) and threw a ZodError, breaking every tools.execute / |
| 929 | // tools.list call for these toolkits. See |
| 930 | // https://github.com/ComposioHQ/composio/issues/3354. |
| 931 | describe('MCP-shaped tool metadata', () => { |
| 932 | it('should normalize output_parameters: {} to undefined on the transformed Tool', async () => { |
| 933 | const slug = 'GRANOLA_MCP_LIST_MEETINGS'; |
| 934 | mockClient.tools.retrieve.mockResolvedValueOnce(toolMocks.mcpRawTool); |
| 935 | |
| 936 | const tool = await context.tools.getRawComposioToolBySlug(slug); |
| 937 | |
| 938 | expect(tool.outputParameters).toBeUndefined(); |
| 939 | expect(tool.inputParameters).toEqual(toolMocks.mcpRawTool.input_parameters); |
| 940 | }); |
| 941 | |
| 942 | it('should normalize both empty input and output parameters to undefined', async () => { |
| 943 | const slug = 'SOME_MCP_PING'; |
| 944 | mockClient.tools.retrieve.mockResolvedValueOnce(toolMocks.mcpRawToolBothEmpty); |
| 945 | |
| 946 | const tool = await context.tools.getRawComposioToolBySlug(slug); |
| 947 | |
| 948 | expect(tool.inputParameters).toBeUndefined(); |
| 949 | expect(tool.outputParameters).toBeUndefined(); |
| 950 | }); |
| 951 | |
| 952 | it('should also tolerate empty output_parameters in tools.list responses', async () => { |
| 953 | mockClient.tools.list.mockResolvedValueOnce({ |
| 954 | items: [toolMocks.mcpRawTool], |
| 955 | totalPages: 1, |
| 956 | }); |
| 957 | |
| 958 | const result = await context.tools.getRawComposioTools({ toolkits: ['granola_mcp'] }); |
| 959 | |
| 960 | expect(result).toHaveLength(1); |
| 961 | expect(result[0].slug).toEqual('GRANOLA_MCP_LIST_MEETINGS'); |
| 962 | expect(result[0].outputParameters).toBeUndefined(); |
| 963 | expect(result[0].inputParameters).toEqual(toolMocks.mcpRawTool.input_parameters); |
| 964 | }); |
| 965 | |
| 966 | it('should execute an MCP tool whose metadata has empty output_parameters', async () => { |
| 967 | // End-to-end: tools.execute → getRawComposioToolBySlug → |
| 968 | // transformToolCases → executeComposioTool → client.tools.execute. |
| 969 | mockClient.tools.retrieve.mockResolvedValueOnce(toolMocks.mcpRawTool); |
| 970 | mockClient.tools.execute.mockResolvedValueOnce(toolMocks.rawToolExecuteResponse); |
| 971 | |
| 972 | const result = await context.tools.execute('GRANOLA_MCP_LIST_MEETINGS', { |
| 973 | userId: 'pg-test-9f4d0df7-a59d-4341-ad00-887b2c58004b', |
| 974 | arguments: { time_range: 'last_30_days' }, |
| 975 | version: '20260206_00', |
| 976 | }); |
| 977 | |
| 978 | expect(mockClient.tools.execute).toHaveBeenCalledTimes(1); |
| 979 | expect(result).toEqual(toolMocks.toolExecuteResponse); |
| 980 | }); |
| 981 | }); |
| 982 | |
| 983 | // `ParametersSchema` is a strict `z.object`, so any key it doesn't declare is |
| 984 | // dropped on parse. It previously omitted `$defs`/`definitions`, so a tool |
| 985 | // whose parameters root carries those blocks (the `$ref` targets) lost them — |
| 986 | // leaving every nested `$ref` dangling and unresolvable for providers and the |
| 987 | // file modifier. See https://github.com/ComposioHQ/composio/issues/3506. |
| 988 | describe('$ref / $defs preservation on tool parameters', () => { |
| 989 | const rawToolWithDefs = { |
| 990 | slug: 'GMAIL_GET_ATTACHMENT', |
| 991 | name: 'Get Attachment', |
| 992 | description: 'Fetch a Gmail attachment', |
| 993 | input_parameters: { |
| 994 | type: 'object', |
| 995 | properties: { |
| 996 | message_id: { type: 'string' }, |
| 997 | }, |
| 998 | }, |
| 999 | output_parameters: { |
| 1000 | type: 'object', |
| 1001 | properties: { |
| 1002 | data: { $ref: '#/$defs/GetAttachmentResponse' }, |
| 1003 | legacy_data: { $ref: '#/definitions/LegacyAttachmentResponse' }, |
| 1004 | }, |
| 1005 | $defs: { |
| 1006 | GetAttachmentResponse: { |
| 1007 | type: 'object', |
| 1008 | properties: { file: { $ref: '#/$defs/FileDownloadable' } }, |
| 1009 | }, |
| 1010 | FileDownloadable: { |
| 1011 | type: 'object', |
| 1012 | file_downloadable: true, |
| 1013 | properties: { s3url: { type: 'string' } }, |
| 1014 | }, |
| 1015 | }, |
| 1016 | definitions: { |
| 1017 | LegacyAttachmentResponse: { |
| 1018 | type: 'object', |
| 1019 | properties: { file: { $ref: '#/definitions/LegacyFileDownloadable' } }, |
| 1020 | }, |
| 1021 | LegacyFileDownloadable: { |
| 1022 | type: 'object', |
| 1023 | file_downloadable: true, |
| 1024 | properties: { s3url: { type: 'string' } }, |
| 1025 | }, |
| 1026 | }, |
| 1027 | }, |
| 1028 | toolkit: { logo: 'https://example.com/gmail.png', slug: 'gmail', name: 'Gmail' }, |
| 1029 | version: '20260515_00', |
| 1030 | }; |
| 1031 | |
| 1032 | it('keeps the root $defs block so nested $ref pointers stay resolvable', async () => { |
| 1033 | mockClient.tools.retrieve.mockResolvedValueOnce(rawToolWithDefs); |
| 1034 | |
| 1035 | const tool = await context.tools.getRawComposioToolBySlug('GMAIL_GET_ATTACHMENT'); |
| 1036 | |
| 1037 | expect(tool.outputParameters?.$defs).toBeDefined(); |
| 1038 | expect(tool.outputParameters?.$defs?.FileDownloadable).toMatchObject({ |
| 1039 | file_downloadable: true, |
| 1040 | }); |
| 1041 | expect(tool.outputParameters?.definitions?.LegacyFileDownloadable).toMatchObject({ |
| 1042 | file_downloadable: true, |
| 1043 | }); |
| 1044 | expect(tool.outputParameters?.properties?.data).toEqual({ |
| 1045 | $ref: '#/$defs/GetAttachmentResponse', |
| 1046 | }); |
| 1047 | expect(tool.outputParameters?.properties?.legacy_data).toEqual({ |
| 1048 | $ref: '#/definitions/LegacyAttachmentResponse', |
| 1049 | }); |
| 1050 | }); |
| 1051 | }); |
| 1052 | |
| 1053 | describe('get', () => { |
| 1054 | it('should get a single tool by slug and wrap it with provider as a collection', async () => { |
| 1055 | const userId = 'test-user'; |
| 1056 | const slug = 'TOOL_SLUG'; |
| 1057 | |
| 1058 | const getRawComposioToolBySlugSpy = vi.spyOn(context.tools, 'getRawComposioToolBySlug'); |
| 1059 | getRawComposioToolBySlugSpy.mockResolvedValueOnce( |
| 1060 | toolMocks.transformedTool as unknown as Tool |
| 1061 | ); |
| 1062 | |
| 1063 | context.mockProvider.wrapTools.mockReturnValueOnce('wrapped-tools-collection'); |
| 1064 | |
| 1065 | const result = await context.tools.get(userId, slug); |
| 1066 | |
| 1067 | expect(getRawComposioToolBySlugSpy).toHaveBeenCalledWith(slug, undefined, undefined); |
| 1068 | expect(context.mockProvider.wrapTools).toHaveBeenCalledWith( |
| 1069 | [toolMocks.transformedTool], |
| 1070 | expect.any(Function) |
| 1071 | ); |
| 1072 | expect(result).toEqual('wrapped-tools-collection'); |
| 1073 | }); |
| 1074 | |
| 1075 | it('should get multiple tools and wrap them with provider', async () => { |
| 1076 | const userId = 'test-user'; |
| 1077 | const filters = { tools: ['TOOL1', 'TOOL2'] }; |
| 1078 | |
| 1079 | const getRawComposioToolsSpy = vi.spyOn(context.tools, 'getRawComposioTools'); |
| 1080 | getRawComposioToolsSpy.mockResolvedValueOnce([toolMocks.transformedTool as unknown as Tool]); |
| 1081 | |
| 1082 | context.mockProvider.wrapTools.mockReturnValueOnce('wrapped-tools-collection'); |
| 1083 | |
| 1084 | const result = await context.tools.get(userId, filters); |
| 1085 | |
| 1086 | expect(getRawComposioToolsSpy).toHaveBeenCalledWith(filters, undefined, undefined); |
| 1087 | expect(context.mockProvider.wrapTools).toHaveBeenCalled(); |
| 1088 | expect(result).toEqual('wrapped-tools-collection'); |
| 1089 | }); |
| 1090 | |
| 1091 | it('should apply schema modifiers before wrapping tools for the provider', async () => { |
| 1092 | const userId = 'test-user'; |
| 1093 | const slug = 'TOOL_SLUG'; |
| 1094 | const schemaModifier = createSchemaModifier({ |
| 1095 | description: 'Modified description', |
| 1096 | }); |
| 1097 | |
| 1098 | const getRawComposioToolBySlugSpy = vi.spyOn(context.tools, 'getRawComposioToolBySlug'); |
| 1099 | getRawComposioToolBySlugSpy.mockResolvedValueOnce( |
| 1100 | toolMocks.transformedTool as unknown as Tool |
| 1101 | ); |
| 1102 | |
| 1103 | await context.tools.get(userId, slug, { modifySchema: schemaModifier }); |
| 1104 | |
| 1105 | expect(getRawComposioToolBySlugSpy).toHaveBeenCalledWith(slug, undefined, undefined); |
| 1106 | expect(schemaModifier).toHaveBeenCalledOnce(); |
| 1107 | expect(context.mockProvider.wrapTools).toHaveBeenCalledWith( |
| 1108 | [expect.objectContaining({ description: 'Modified description' })], |
| 1109 | expect.any(Function) |
| 1110 | ); |
| 1111 | }); |
| 1112 | |
| 1113 | it('should reject an invalid schema modifier when no tools are returned', async () => { |
| 1114 | const invalidModifier = 'not a function' as unknown; |
| 1115 | vi.spyOn(context.tools, 'getRawComposioTools').mockResolvedValueOnce([]); |
| 1116 | |
| 1117 | await expect( |
| 1118 | context.tools.get('test-user', { toolkits: ['github'] }, { modifySchema: invalidModifier }) |
| 1119 | ).rejects.toThrow('Invalid schema modifier. Not a function.'); |
| 1120 | }); |
| 1121 | }); |
| 1122 | |
| 1123 | describe('execute', () => { |
| 1124 | it('should execute a composio tool', async () => { |
| 1125 | const slug = 'COMPOSIO_TOOL'; |
| 1126 | const body = { |
| 1127 | userId: 'test-user', |
| 1128 | connectedAccountId: undefined, |
| 1129 | arguments: { query: 'test' }, |
| 1130 | dangerouslySkipVersionCheck: true, |
| 1131 | }; |
| 1132 | |
| 1133 | await mockToolExecution(context.tools); |
| 1134 | |
| 1135 | const result = await context.tools.execute(slug, body); |
| 1136 | |
| 1137 | expect(mockClient.tools.execute).toHaveBeenCalledWith( |
| 1138 | slug, |
| 1139 | { |
| 1140 | allow_tracing: undefined, |
| 1141 | connected_account_id: undefined, |
| 1142 | custom_auth_params: undefined, |
| 1143 | custom_connection_data: undefined, |
| 1144 | arguments: body.arguments, |
| 1145 | user_id: body.userId, |
| 1146 | version: 'latest', |
| 1147 | text: undefined, |
| 1148 | }, |
| 1149 | { maxRetries: 0 } |
| 1150 | ); |
| 1151 | expect(result).toEqual(toolMocks.toolExecuteResponse); |
| 1152 | }); |
| 1153 | |
| 1154 | it('should execute a composio tool with a connected account', async () => { |
| 1155 | const slug = 'COMPOSIO_TOOL'; |
| 1156 | const body = { |
| 1157 | userId: 'test-user', |
| 1158 | connectedAccountId: 'test-connected-account-id', |
| 1159 | arguments: { query: 'test' }, |
| 1160 | dangerouslySkipVersionCheck: true, |
| 1161 | }; |
| 1162 | |
| 1163 | await mockToolExecution(context.tools); |
| 1164 | |
| 1165 | const result = await context.tools.execute(slug, body); |
| 1166 | |
| 1167 | expect(mockClient.tools.execute).toHaveBeenCalledWith( |
| 1168 | slug, |
| 1169 | { |
| 1170 | allow_tracing: undefined, |
| 1171 | connected_account_id: 'test-connected-account-id', |
| 1172 | custom_auth_params: undefined, |
| 1173 | custom_connection_data: undefined, |
| 1174 | arguments: body.arguments, |
| 1175 | user_id: body.userId, |
| 1176 | version: 'latest', |
| 1177 | text: undefined, |
| 1178 | }, |
| 1179 | { maxRetries: 0 } |
| 1180 | ); |
| 1181 | expect(result).toEqual(toolMocks.toolExecuteResponse); |
| 1182 | }); |
| 1183 | |
| 1184 | it('should transform customAuthParams.baseURL to base_url when executing a tool', async () => { |
| 1185 | const slug = 'COMPOSIO_TOOL'; |
| 1186 | const body = { |
| 1187 | userId: 'test-user', |
| 1188 | connectedAccountId: 'test-connected-account-id', |
| 1189 | arguments: { query: 'test' }, |
| 1190 | dangerouslySkipVersionCheck: true, |
| 1191 | customAuthParams: { |
| 1192 | baseURL: 'https://custom-api.example.com', |
| 1193 | parameters: [{ name: 'x-api-key', value: 'test-key', in: 'header' as const }], |
| 1194 | body: { extra: 'data' }, |
| 1195 | }, |
| 1196 | }; |
| 1197 | |
| 1198 | await mockToolExecution(context.tools); |
| 1199 | |
| 1200 | const result = await context.tools.execute(slug, body); |
| 1201 | |
| 1202 | expect(mockClient.tools.execute).toHaveBeenCalledWith( |
| 1203 | slug, |
| 1204 | { |
| 1205 | allow_tracing: undefined, |
| 1206 | connected_account_id: 'test-connected-account-id', |
| 1207 | custom_auth_params: { |
| 1208 | base_url: 'https://custom-api.example.com', |
| 1209 | parameters: [{ name: 'x-api-key', value: 'test-key', in: 'header' }], |
| 1210 | body: { extra: 'data' }, |
| 1211 | }, |
| 1212 | custom_connection_data: undefined, |
| 1213 | arguments: body.arguments, |
| 1214 | user_id: body.userId, |
| 1215 | version: 'latest', |
| 1216 | text: undefined, |
| 1217 | }, |
| 1218 | { maxRetries: 0 } |
| 1219 | ); |
| 1220 | expect(result).toEqual(toolMocks.toolExecuteResponse); |
| 1221 | }); |
| 1222 | |
| 1223 | it('should pass version parameter from execute() to getRawComposioToolBySlug()', async () => { |
| 1224 | const slug = 'COMPOSIO_TOOL'; |
| 1225 | const explicitVersion = '20250909_00'; |
| 1226 | const body = { |
| 1227 | userId: 'test-user', |
| 1228 | version: explicitVersion, |
| 1229 | arguments: { title: 'Test Issue' }, |
| 1230 | }; |
| 1231 | |
| 1232 | const { getRawComposioToolBySlugSpy } = await mockToolExecution(context.tools); |
| 1233 | |
| 1234 | await context.tools.execute(slug, body); |
| 1235 | |
| 1236 | // Verify getRawComposioToolBySlug was called with version |
| 1237 | expect(getRawComposioToolBySlugSpy).toHaveBeenCalledWith( |
| 1238 | slug, |
| 1239 | { |
| 1240 | version: explicitVersion, |
| 1241 | }, |
| 1242 | undefined |
| 1243 | ); |
| 1244 | |
| 1245 | // Verify the API execute call received the version |
| 1246 | expect(mockClient.tools.execute).toHaveBeenCalledWith( |
| 1247 | slug, |
| 1248 | expect.objectContaining({ |
| 1249 | version: explicitVersion, |
| 1250 | }), |
| 1251 | { maxRetries: 0 } |
| 1252 | ); |
| 1253 | }); |
| 1254 | |
| 1255 | it('should pass version: latest from execute() to getRawComposioToolBySlug()', async () => { |
| 1256 | const slug = 'COMPOSIO_TOOL'; |
| 1257 | const body = { |
| 1258 | userId: 'test-user', |
| 1259 | version: 'latest' as const, |
| 1260 | dangerouslySkipVersionCheck: true, |
| 1261 | arguments: { title: 'Test Issue' }, |
| 1262 | }; |
| 1263 | |
| 1264 | const { getRawComposioToolBySlugSpy } = await mockToolExecution(context.tools); |
| 1265 | |
| 1266 | await context.tools.execute(slug, body); |
| 1267 | |
| 1268 | // Verify getRawComposioToolBySlug was called with version: 'latest' |
| 1269 | expect(getRawComposioToolBySlugSpy).toHaveBeenCalledWith( |
| 1270 | slug, |
| 1271 | { |
| 1272 | version: 'latest', |
| 1273 | }, |
| 1274 | undefined |
| 1275 | ); |
| 1276 | }); |
| 1277 | |
| 1278 | it('should not pass version to getRawComposioToolBySlug() when not provided in execute()', async () => { |
| 1279 | const slug = 'COMPOSIO_TOOL'; |
| 1280 | const body = { |
| 1281 | userId: 'test-user', |
| 1282 | arguments: { title: 'Test Issue' }, |
| 1283 | dangerouslySkipVersionCheck: true, |
| 1284 | }; |
| 1285 | |
| 1286 | const { getRawComposioToolBySlugSpy } = await mockToolExecution(context.tools); |
| 1287 | |
| 1288 | await context.tools.execute(slug, body); |
| 1289 | |
| 1290 | // Verify getRawComposioToolBySlug was called without version (should fallback to SDK config) |
| 1291 | expect(getRawComposioToolBySlugSpy).toHaveBeenCalledWith( |
| 1292 | slug, |
| 1293 | { |
| 1294 | version: undefined, |
| 1295 | }, |
| 1296 | undefined |
| 1297 | ); |
| 1298 | }); |
| 1299 | }); |
| 1300 | |
| 1301 | describe('proxyExecute', () => { |
| 1302 | it('should handle proxy request with headers and query parameters correctly', async () => { |
| 1303 | const proxyParams = { |
| 1304 | endpoint: '/api/test', |
| 1305 | method: 'POST' as const, |
| 1306 | body: { data: 'test' }, |
| 1307 | parameters: [ |
| 1308 | { |
| 1309 | in: 'header' as const, |
| 1310 | name: 'Authorization', |
| 1311 | value: 'Bearer token123', |
| 1312 | }, |
| 1313 | { |
| 1314 | in: 'header' as const, |
| 1315 | name: 'Content-Type', |
| 1316 | value: 'application/json', |
| 1317 | }, |
| 1318 | { |
| 1319 | in: 'query' as const, |
| 1320 | name: 'page', |
| 1321 | value: 1, |
| 1322 | }, |
| 1323 | { |
| 1324 | in: 'query' as const, |
| 1325 | name: 'limit', |
| 1326 | value: 10, |
| 1327 | }, |
| 1328 | ], |
| 1329 | connectedAccountId: 'test-account-id', |
| 1330 | }; |
| 1331 | |
| 1332 | const expectedProxyResponse = { |
| 1333 | data: { success: true }, |
| 1334 | successful: true, |
| 1335 | }; |
| 1336 | |
| 1337 | mockClient.tools.proxy.mockResolvedValueOnce(expectedProxyResponse); |
| 1338 | |
| 1339 | const result = await context.tools.proxyExecute(proxyParams); |
| 1340 | |
| 1341 | expect(mockClient.tools.proxy).toHaveBeenCalledWith( |
| 1342 | { |
| 1343 | endpoint: '/api/test', |
| 1344 | method: 'POST', |
| 1345 | body: { data: 'test' }, |
| 1346 | connected_account_id: 'test-account-id', |
| 1347 | parameters: [ |
| 1348 | { |
| 1349 | name: 'Authorization', |
| 1350 | type: 'header', |
| 1351 | value: 'Bearer token123', |
| 1352 | }, |
| 1353 | { |
| 1354 | name: 'Content-Type', |
| 1355 | type: 'header', |
| 1356 | value: 'application/json', |
| 1357 | }, |
| 1358 | { |
| 1359 | name: 'page', |
| 1360 | type: 'query', |
| 1361 | value: '1', |
| 1362 | }, |
| 1363 | { |
| 1364 | name: 'limit', |
| 1365 | type: 'query', |
| 1366 | value: '10', |
| 1367 | }, |
| 1368 | ], |
| 1369 | }, |
| 1370 | { maxRetries: 0 } |
| 1371 | ); |
| 1372 | |
| 1373 | expect(result).toEqual(expectedProxyResponse); |
| 1374 | }); |
| 1375 | |
| 1376 | it('should forward userId as user_id', async () => { |
| 1377 | const expectedProxyResponse = { data: { ok: true }, status: 200 }; |
| 1378 | mockClient.tools.proxy.mockResolvedValueOnce(expectedProxyResponse); |
| 1379 | |
| 1380 | await context.tools.proxyExecute({ |
| 1381 | endpoint: '/api/test', |
| 1382 | method: 'GET', |
| 1383 | connectedAccountId: 'test-account-id', |
| 1384 | userId: 'user-123', |
| 1385 | }); |
| 1386 | |
| 1387 | expect(mockClient.tools.proxy).toHaveBeenCalledWith( |
| 1388 | expect.objectContaining({ connected_account_id: 'test-account-id', user_id: 'user-123' }), |
| 1389 | { maxRetries: 0 } |
| 1390 | ); |
| 1391 | }); |
| 1392 | |
| 1393 | it('should handle proxy request with only header parameters', async () => { |
| 1394 | const proxyParams = { |
| 1395 | endpoint: '/api/headers-only', |
| 1396 | method: 'GET' as const, |
| 1397 | parameters: [ |
| 1398 | { |
| 1399 | in: 'header' as const, |
| 1400 | name: 'Authorization', |
| 1401 | value: 'Bearer token123', |
| 1402 | }, |
| 1403 | { |
| 1404 | in: 'header' as const, |
| 1405 | name: 'Accept', |
| 1406 | value: 'application/json', |
| 1407 | }, |
| 1408 | ], |
| 1409 | }; |
| 1410 | |
| 1411 | const expectedProxyResponse = { |
| 1412 | data: { result: 'success' }, |
| 1413 | successful: true, |
| 1414 | }; |
| 1415 | |
| 1416 | mockClient.tools.proxy.mockResolvedValueOnce(expectedProxyResponse); |
| 1417 | |
| 1418 | const result = await context.tools.proxyExecute(proxyParams); |
| 1419 | |
| 1420 | expect(mockClient.tools.proxy).toHaveBeenCalledWith( |
| 1421 | { |
| 1422 | endpoint: '/api/headers-only', |
| 1423 | method: 'GET', |
| 1424 | body: undefined, |
| 1425 | connected_account_id: undefined, |
| 1426 | parameters: [ |
| 1427 | { |
| 1428 | name: 'Authorization', |
| 1429 | type: 'header', |
| 1430 | value: 'Bearer token123', |
| 1431 | }, |
| 1432 | { |
| 1433 | name: 'Accept', |
| 1434 | type: 'header', |
| 1435 | value: 'application/json', |
| 1436 | }, |
| 1437 | ], |
| 1438 | }, |
| 1439 | { maxRetries: 0 } |
| 1440 | ); |
| 1441 | |
| 1442 | expect(result).toEqual(expectedProxyResponse); |
| 1443 | }); |
| 1444 | |
| 1445 | it('should handle proxy request with only query parameters', async () => { |
| 1446 | const proxyParams = { |
| 1447 | endpoint: '/api/search', |
| 1448 | method: 'GET' as const, |
| 1449 | parameters: [ |
| 1450 | { |
| 1451 | in: 'query' as const, |
| 1452 | name: 'q', |
| 1453 | value: 'test search', |
| 1454 | }, |
| 1455 | { |
| 1456 | in: 'query' as const, |
| 1457 | name: 'page', |
| 1458 | value: 2, |
| 1459 | }, |
| 1460 | ], |
| 1461 | }; |
| 1462 | |
| 1463 | const expectedProxyResponse = { |
| 1464 | data: { results: ['item1', 'item2'] }, |
| 1465 | successful: true, |
| 1466 | }; |
| 1467 | |
| 1468 | mockClient.tools.proxy.mockResolvedValueOnce(expectedProxyResponse); |
| 1469 | |
| 1470 | const result = await context.tools.proxyExecute(proxyParams); |
| 1471 | |
| 1472 | expect(mockClient.tools.proxy).toHaveBeenCalledWith( |
| 1473 | { |
| 1474 | endpoint: '/api/search', |
| 1475 | method: 'GET', |
| 1476 | body: undefined, |
| 1477 | connected_account_id: undefined, |
| 1478 | parameters: [ |
| 1479 | { |
| 1480 | name: 'q', |
| 1481 | type: 'query', |
| 1482 | value: 'test search', |
| 1483 | }, |
| 1484 | { |
| 1485 | name: 'page', |
| 1486 | type: 'query', |
| 1487 | value: '2', |
| 1488 | }, |
| 1489 | ], |
| 1490 | }, |
| 1491 | { maxRetries: 0 } |
| 1492 | ); |
| 1493 | |
| 1494 | expect(result).toEqual(expectedProxyResponse); |
| 1495 | }); |
| 1496 | |
| 1497 | it('should handle proxy request without parameters', async () => { |
| 1498 | const proxyParams = { |
| 1499 | endpoint: '/api/no-params', |
| 1500 | method: 'PUT' as const, |
| 1501 | body: { update: 'data' }, |
| 1502 | connectedAccountId: 'test-account-id', |
| 1503 | }; |
| 1504 | |
| 1505 | const expectedProxyResponse = { |
| 1506 | data: { updated: true }, |
| 1507 | successful: true, |
| 1508 | }; |
| 1509 | |
| 1510 | mockClient.tools.proxy.mockResolvedValueOnce(expectedProxyResponse); |
| 1511 | |
| 1512 | const result = await context.tools.proxyExecute(proxyParams); |
| 1513 | |
| 1514 | expect(mockClient.tools.proxy).toHaveBeenCalledWith( |
| 1515 | { |
| 1516 | endpoint: '/api/no-params', |
| 1517 | method: 'PUT', |
| 1518 | body: { update: 'data' }, |
| 1519 | connected_account_id: 'test-account-id', |
| 1520 | parameters: [], |
| 1521 | }, |
| 1522 | { maxRetries: 0 } |
| 1523 | ); |
| 1524 | |
| 1525 | expect(result).toEqual(expectedProxyResponse); |
| 1526 | }); |
| 1527 | |
| 1528 | it('should convert numeric parameter values to strings', async () => { |
| 1529 | const proxyParams = { |
| 1530 | endpoint: '/api/numeric', |
| 1531 | method: 'GET' as const, |
| 1532 | parameters: [ |
| 1533 | { |
| 1534 | in: 'query' as const, |
| 1535 | name: 'count', |
| 1536 | value: 42, |
| 1537 | }, |
| 1538 | { |
| 1539 | in: 'header' as const, |
| 1540 | name: 'Version', |
| 1541 | value: 1.5, |
| 1542 | }, |
| 1543 | ], |
| 1544 | }; |
| 1545 | |
| 1546 | const expectedProxyResponse = { |
| 1547 | data: { result: 'ok' }, |
| 1548 | successful: true, |
| 1549 | }; |
| 1550 | |
| 1551 | mockClient.tools.proxy.mockResolvedValueOnce(expectedProxyResponse); |
| 1552 | |
| 1553 | const result = await context.tools.proxyExecute(proxyParams); |
| 1554 | |
| 1555 | expect(mockClient.tools.proxy).toHaveBeenCalledWith( |
| 1556 | { |
| 1557 | endpoint: '/api/numeric', |
| 1558 | method: 'GET', |
| 1559 | body: undefined, |
| 1560 | connected_account_id: undefined, |
| 1561 | parameters: [ |
| 1562 | { |
| 1563 | name: 'count', |
| 1564 | type: 'query', |
| 1565 | value: '42', |
| 1566 | }, |
| 1567 | { |
| 1568 | name: 'Version', |
| 1569 | type: 'header', |
| 1570 | value: '1.5', |
| 1571 | }, |
| 1572 | ], |
| 1573 | }, |
| 1574 | { maxRetries: 0 } |
| 1575 | ); |
| 1576 | |
| 1577 | expect(result).toEqual(expectedProxyResponse); |
| 1578 | }); |
| 1579 | |
| 1580 | it('should throw validation error for invalid parameters', async () => { |
| 1581 | const invalidProxyParams = { |
| 1582 | endpoint: '/api/test', |
| 1583 | method: 'INVALID_METHOD' as unknown, |
| 1584 | parameters: [ |
| 1585 | { |
| 1586 | in: 'header' as const, |
| 1587 | name: 'Authorization', |
| 1588 | value: 'Bearer token123', |
| 1589 | }, |
| 1590 | ], |
| 1591 | }; |
| 1592 | |
| 1593 | await expect(context.tools.proxyExecute(invalidProxyParams)).rejects.toThrow( |
| 1594 | 'Invalid tool proxy parameters' |
| 1595 | ); |
| 1596 | }); |
| 1597 | }); |
| 1598 | |
| 1599 | describe('Tool Router Execution', () => { |
| 1600 | const sessionId = 'test-session-123'; |
| 1601 | |
| 1602 | describe('executeSessionTool', () => { |
| 1603 | it('should execute a tool via tool router session', async () => { |
| 1604 | const toolSlug = 'COMPOSIO_TOOL'; |
| 1605 | const body = { |
| 1606 | sessionId, |
| 1607 | arguments: { query: 'test' }, |
| 1608 | }; |
| 1609 | |
| 1610 | mockClient.toolRouter.session.execute.mockResolvedValueOnce({ |
| 1611 | result_type: 'completed', |
| 1612 | data: { results: true }, |
| 1613 | error: null, |
| 1614 | log_id: '123', |
| 1615 | }); |
| 1616 | |
| 1617 | const result = await context.tools.executeSessionTool(toolSlug, body); |
| 1618 | |
| 1619 | expect(mockClient.toolRouter.session.execute).toHaveBeenCalledWith( |
| 1620 | sessionId, |
| 1621 | { |
| 1622 | tool_slug: toolSlug, |
| 1623 | arguments: body.arguments, |
| 1624 | enable_auto_workbench_offload: true, |
| 1625 | }, |
| 1626 | { maxRetries: 0 } |
| 1627 | ); |
| 1628 | expect(result).toEqual({ |
| 1629 | data: { results: true }, |
| 1630 | error: null, |
| 1631 | successful: true, |
| 1632 | logId: '123', |
| 1633 | }); |
| 1634 | }); |
| 1635 | |
| 1636 | it('returns the Instant charge for provider-wrapped session tools', async () => { |
| 1637 | mockClient.toolRouter.session.execute.mockResolvedValueOnce({ |
| 1638 | result_type: 'completed', |
| 1639 | data: {}, |
| 1640 | error: null, |
| 1641 | log_id: '123', |
| 1642 | instant_charge: { amount: '0.01', currency: 'USD' }, |
| 1643 | }); |
| 1644 | |
| 1645 | const result = await context.tools.executeSessionTool('EXA_SEARCH', { |
| 1646 | sessionId, |
| 1647 | arguments: {}, |
| 1648 | }); |
| 1649 | |
| 1650 | expect(result.instantCharge).toEqual({ amount: '0.01', currency: 'USD' }); |
| 1651 | }); |
| 1652 | |
| 1653 | it('should pass inline custom tools to tool router session execute', async () => { |
| 1654 | const toolSlug = 'COMPOSIO_TOOL'; |
| 1655 | const body = { |
| 1656 | sessionId, |
| 1657 | arguments: { query: 'test' }, |
| 1658 | }; |
| 1659 | |
| 1660 | mockClient.toolRouter.session.execute.mockResolvedValueOnce({ |
| 1661 | result_type: 'completed', |
| 1662 | data: { results: true }, |
| 1663 | error: null, |
| 1664 | log_id: '123', |
| 1665 | }); |
| 1666 | |
| 1667 | await context.tools.executeSessionTool(toolSlug, body, undefined, undefined, { |
| 1668 | experimental: { |
| 1669 | custom_tools: [ |
| 1670 | { |
| 1671 | slug: 'GREP', |
| 1672 | name: 'Grep', |
| 1673 | description: 'Search local text', |
| 1674 | input_schema: { type: 'object', properties: {} }, |
| 1675 | }, |
| 1676 | ], |
| 1677 | }, |
| 1678 | }); |
| 1679 | |
| 1680 | expect(mockClient.toolRouter.session.execute).toHaveBeenCalledWith( |
| 1681 | sessionId, |
| 1682 | { |
| 1683 | tool_slug: toolSlug, |
| 1684 | arguments: body.arguments, |
| 1685 | enable_auto_workbench_offload: true, |
| 1686 | experimental: { |
| 1687 | custom_tools: [expect.objectContaining({ slug: 'GREP' })], |
| 1688 | }, |
| 1689 | }, |
| 1690 | { maxRetries: 0 } |
| 1691 | ); |
| 1692 | }); |
| 1693 | |
| 1694 | it('should throw validation error for invalid parameters', async () => { |
| 1695 | const invalidBody = { |
| 1696 | // missing sessionId |
| 1697 | arguments: { query: 'test' }, |
| 1698 | } as unknown; |
| 1699 | |
| 1700 | await expect( |
| 1701 | context.tools.executeSessionTool('COMPOSIO_TOOL', invalidBody) |
| 1702 | ).rejects.toThrow('Invalid tool execute session parameters'); |
| 1703 | }); |
| 1704 | |
| 1705 | it('wraps a failed session execution with the tool context', async () => { |
| 1706 | const apiError = new Error('Tool not found'); |
| 1707 | mockClient.toolRouter.session.execute.mockRejectedValueOnce(apiError); |
| 1708 | |
| 1709 | const error = await context.tools |
| 1710 | .executeSessionTool('NONEXISTENT_TOOL', { |
| 1711 | sessionId, |
| 1712 | arguments: { query: 'test' }, |
| 1713 | }) |
| 1714 | .catch(e => e); |
| 1715 | |
| 1716 | expect(error).toBeInstanceOf(ComposioToolExecutionError); |
| 1717 | expect(error.message).toBe('Error executing the tool NONEXISTENT_TOOL: Tool not found'); |
| 1718 | expect(error.cause).toBe(apiError); |
| 1719 | }); |
| 1720 | |
| 1721 | it('surfaces the server details of a rejected session execution', async () => { |
| 1722 | const apiError = new APIError( |
| 1723 | 410, |
| 1724 | { |
| 1725 | error: { |
| 1726 | message: 'Toolkit version 20250101_00 is retired', |
| 1727 | code: 1410, |
| 1728 | slug: 'TOOLKIT_VERSION_RETIRED', |
| 1729 | status: 410, |
| 1730 | suggested_fix: 'Pin 20250909_00', |
| 1731 | }, |
| 1732 | }, |
| 1733 | undefined, |
| 1734 | new Headers({ 'x-request-id': 'req_session_410' }) |
| 1735 | ); |
| 1736 | mockClient.toolRouter.session.execute.mockRejectedValueOnce(apiError); |
| 1737 | |
| 1738 | const error = await context.tools |
| 1739 | .executeSessionTool('GMAIL_SEND_EMAIL', { |
| 1740 | sessionId, |
| 1741 | arguments: { query: 'test' }, |
| 1742 | }) |
| 1743 | .catch(e => e); |
| 1744 | |
| 1745 | expect(error).toBeInstanceOf(ComposioToolExecutionError); |
| 1746 | expect(error.message).toBe( |
| 1747 | 'Error executing the tool GMAIL_SEND_EMAIL: Toolkit version 20250101_00 is retired' |
| 1748 | ); |
| 1749 | expect(error.statusCode).toBe(410); |
| 1750 | expect(error.apiErrorCode).toBe(1410); |
| 1751 | expect(error.requestId).toBe('req_session_410'); |
| 1752 | expect(error.possibleFixes).toEqual(['Pin 20250909_00']); |
| 1753 | }); |
| 1754 | |
| 1755 | it('should apply beforeExecute modifier', async () => { |
| 1756 | const toolSlug = 'COMPOSIO_TOOL'; |
| 1757 | const body = { |
| 1758 | sessionId, |
| 1759 | arguments: { query: 'original' }, |
| 1760 | }; |
| 1761 | |
| 1762 | const toolWithToolkit = { |
| 1763 | ...toolMocks.transformedTool, |
| 1764 | toolkit: { slug: 'test-toolkit', name: 'Test Toolkit' }, |
| 1765 | }; |
| 1766 | |
| 1767 | mockClient.toolRouter.session.execute.mockResolvedValueOnce({ |
| 1768 | result_type: 'completed', |
| 1769 | data: { results: true }, |
| 1770 | error: null, |
| 1771 | log_id: '123', |
| 1772 | }); |
| 1773 | |
| 1774 | const beforeExecute = vi.fn().mockImplementation(({ params }) => ({ |
| 1775 | ...params, |
| 1776 | query: 'modified', |
| 1777 | })); |
| 1778 | |
| 1779 | await context.tools.executeSessionTool( |
| 1780 | toolSlug, |
| 1781 | body, |
| 1782 | { beforeExecute }, |
| 1783 | toolWithToolkit as unknown as Tool |
| 1784 | ); |
| 1785 | |
| 1786 | expect(beforeExecute).toHaveBeenCalledWith({ |
| 1787 | toolSlug, |
| 1788 | toolkitSlug: 'test-toolkit', |
| 1789 | sessionId, |
| 1790 | params: { query: 'original' }, |
| 1791 | }); |
| 1792 | |
| 1793 | expect(mockClient.toolRouter.session.execute).toHaveBeenCalledWith( |
| 1794 | sessionId, |
| 1795 | { |
| 1796 | tool_slug: toolSlug, |
| 1797 | arguments: { query: 'modified' }, |
| 1798 | enable_auto_workbench_offload: true, |
| 1799 | }, |
| 1800 | { maxRetries: 0 } |
| 1801 | ); |
| 1802 | }); |
| 1803 | |
| 1804 | it('should apply afterExecute modifier', async () => { |
| 1805 | const toolSlug = 'COMPOSIO_TOOL'; |
| 1806 | const body = { |
| 1807 | sessionId, |
| 1808 | arguments: { query: 'test' }, |
| 1809 | }; |
| 1810 | |
| 1811 | const toolWithToolkit = { |
| 1812 | ...toolMocks.transformedTool, |
| 1813 | toolkit: { slug: 'test-toolkit', name: 'Test Toolkit' }, |
| 1814 | }; |
| 1815 | |
| 1816 | mockClient.toolRouter.session.execute.mockResolvedValueOnce({ |
| 1817 | result_type: 'completed', |
| 1818 | data: { results: true }, |
| 1819 | error: null, |
| 1820 | log_id: '123', |
| 1821 | }); |
| 1822 | |
| 1823 | const afterExecute = vi.fn().mockImplementation(({ result }) => ({ |
| 1824 | ...result, |
| 1825 | data: { results: 'modified result' }, |
| 1826 | })); |
| 1827 | |
| 1828 | const result = await context.tools.executeSessionTool( |
| 1829 | toolSlug, |
| 1830 | body, |
| 1831 | { afterExecute }, |
| 1832 | toolWithToolkit as unknown as Tool |
| 1833 | ); |
| 1834 | |
| 1835 | expect(afterExecute).toHaveBeenCalledWith({ |
| 1836 | toolSlug, |
| 1837 | toolkitSlug: 'test-toolkit', |
| 1838 | sessionId, |
| 1839 | result: { |
| 1840 | data: { results: true }, |
| 1841 | error: null, |
| 1842 | successful: true, |
| 1843 | logId: '123', |
| 1844 | }, |
| 1845 | }); |
| 1846 | |
| 1847 | expect(result.data).toEqual({ results: 'modified result' }); |
| 1848 | }); |
| 1849 | |
| 1850 | it('should handle error responses from tool router', async () => { |
| 1851 | const toolSlug = 'COMPOSIO_TOOL'; |
| 1852 | const body = { |
| 1853 | sessionId, |
| 1854 | arguments: { query: 'test' }, |
| 1855 | }; |
| 1856 | |
| 1857 | mockClient.toolRouter.session.execute.mockResolvedValueOnce({ |
| 1858 | result_type: 'failed', |
| 1859 | data: null, |
| 1860 | error: 'Something went wrong', |
| 1861 | log_id: '456', |
| 1862 | }); |
| 1863 | |
| 1864 | const result = await context.tools.executeSessionTool(toolSlug, body); |
| 1865 | |
| 1866 | expect(result).toEqual({ |
| 1867 | data: null, |
| 1868 | error: 'Something went wrong', |
| 1869 | successful: false, |
| 1870 | logId: '456', |
| 1871 | }); |
| 1872 | }); |
| 1873 | |
| 1874 | it('should handle tool without toolkit gracefully', async () => { |
| 1875 | const toolSlug = 'TOOL_WITHOUT_TOOLKIT'; |
| 1876 | const body = { |
| 1877 | sessionId, |
| 1878 | arguments: { query: 'test' }, |
| 1879 | }; |
| 1880 | |
| 1881 | const toolWithoutToolkit = { |
| 1882 | ...toolMocks.transformedTool, |
| 1883 | toolkit: undefined, |
| 1884 | }; |
| 1885 | |
| 1886 | mockClient.toolRouter.session.execute.mockResolvedValueOnce({ |
| 1887 | result_type: 'completed', |
| 1888 | data: { results: true }, |
| 1889 | error: null, |
| 1890 | log_id: '123', |
| 1891 | }); |
| 1892 | |
| 1893 | const beforeExecute = vi.fn().mockImplementation(({ params }) => params); |
| 1894 | const afterExecute = vi.fn().mockImplementation(({ result }) => result); |
| 1895 | |
| 1896 | await context.tools.executeSessionTool( |
| 1897 | toolSlug, |
| 1898 | body, |
| 1899 | { beforeExecute, afterExecute }, |
| 1900 | toolWithoutToolkit as unknown as Tool |
| 1901 | ); |
| 1902 | |
| 1903 | expect(beforeExecute).toHaveBeenCalledWith({ |
| 1904 | toolSlug, |
| 1905 | toolkitSlug: 'composio', |
| 1906 | sessionId, |
| 1907 | params: { query: 'test' }, |
| 1908 | }); |
| 1909 | expect(afterExecute).toHaveBeenCalledWith({ |
| 1910 | toolSlug, |
| 1911 | toolkitSlug: 'composio', |
| 1912 | sessionId, |
| 1913 | result: { |
| 1914 | data: { results: true }, |
| 1915 | error: null, |
| 1916 | successful: true, |
| 1917 | logId: '123', |
| 1918 | }, |
| 1919 | }); |
| 1920 | }); |
| 1921 | }); |
| 1922 | |
| 1923 | describe('wrapToolsForToolRouter', () => { |
| 1924 | it('should wrap tools with tool router execute function', async () => { |
| 1925 | const tools = [toolMocks.transformedTool as unknown as Tool]; |
| 1926 | |
| 1927 | context.mockProvider.wrapTools.mockReturnValueOnce('wrapped-tools'); |
| 1928 | |
| 1929 | const result = context.tools.wrapToolsForToolRouter(sessionId, tools); |
| 1930 | |
| 1931 | expect(context.mockProvider.wrapTools).toHaveBeenCalledWith(tools, expect.any(Function)); |
| 1932 | expect(result).toBe('wrapped-tools'); |
| 1933 | }); |
| 1934 | |
| 1935 | it('should create execute function that calls executeSessionTool', async () => { |
| 1936 | const tools = [toolMocks.transformedTool as unknown as Tool]; |
| 1937 | |
| 1938 | let capturedExecuteFn: ( |
| 1939 | toolSlug: string, |
| 1940 | input: Record<string, unknown> |
| 1941 | ) => Promise<unknown>; |
| 1942 | |
| 1943 | context.mockProvider.wrapTools.mockImplementation((tools, executeFn) => { |
| 1944 | capturedExecuteFn = executeFn; |
| 1945 | return 'wrapped-tools'; |
| 1946 | }); |
| 1947 | |
| 1948 | context.tools.wrapToolsForToolRouter(sessionId, tools); |
| 1949 | |
| 1950 | mockClient.toolRouter.session.execute.mockResolvedValueOnce({ |
| 1951 | result_type: 'completed', |
| 1952 | data: { results: true }, |
| 1953 | error: null, |
| 1954 | log_id: '123', |
| 1955 | }); |
| 1956 | |
| 1957 | // Call the captured execute function |
| 1958 | const result = await capturedExecuteFn!('COMPOSIO_TOOL', { query: 'test' }); |
| 1959 | |
| 1960 | expect(mockClient.toolRouter.session.execute).toHaveBeenCalledWith( |
| 1961 | sessionId, |
| 1962 | { |
| 1963 | tool_slug: 'COMPOSIO_TOOL', |
| 1964 | arguments: { query: 'test' }, |
| 1965 | enable_auto_workbench_offload: true, |
| 1966 | }, |
| 1967 | { maxRetries: 0 } |
| 1968 | ); |
| 1969 | expect(result).toEqual({ |
| 1970 | data: { results: true }, |
| 1971 | error: null, |
| 1972 | successful: true, |
| 1973 | logId: '123', |
| 1974 | }); |
| 1975 | }); |
| 1976 | |
| 1977 | it('should pass modifiers to the execute function', async () => { |
| 1978 | const tools = [toolMocks.transformedTool as unknown as Tool]; |
| 1979 | const modifiers = { |
| 1980 | beforeExecute: vi.fn().mockImplementation(({ params }) => ({ |
| 1981 | ...params, |
| 1982 | modified: true, |
| 1983 | })), |
| 1984 | }; |
| 1985 | |
| 1986 | let capturedExecuteFn: ( |
| 1987 | toolSlug: string, |
| 1988 | input: Record<string, unknown> |
| 1989 | ) => Promise<unknown>; |
| 1990 | |
| 1991 | context.mockProvider.wrapTools.mockImplementation((tools, executeFn) => { |
| 1992 | capturedExecuteFn = executeFn; |
| 1993 | return 'wrapped-tools'; |
| 1994 | }); |
| 1995 | |
| 1996 | context.tools.wrapToolsForToolRouter(sessionId, tools, modifiers); |
| 1997 | |
| 1998 | mockClient.toolRouter.session.execute.mockResolvedValueOnce({ |
| 1999 | result_type: 'completed', |
| 2000 | data: { results: true }, |
| 2001 | error: null, |
| 2002 | log_id: '123', |
| 2003 | }); |
| 2004 | |
| 2005 | // Call the captured execute function |
| 2006 | await capturedExecuteFn!('COMPOSIO_TOOL', { query: 'test' }); |
| 2007 | |
| 2008 | expect(modifiers.beforeExecute).toHaveBeenCalled(); |
| 2009 | expect(mockClient.toolRouter.session.execute).toHaveBeenCalledWith( |
| 2010 | sessionId, |
| 2011 | { |
| 2012 | tool_slug: 'COMPOSIO_TOOL', |
| 2013 | arguments: { query: 'test', modified: true }, |
| 2014 | enable_auto_workbench_offload: true, |
| 2015 | }, |
| 2016 | { maxRetries: 0 } |
| 2017 | ); |
| 2018 | }); |
| 2019 | |
| 2020 | it('should handle multiple tools', async () => { |
| 2021 | const tools = [ |
| 2022 | toolMocks.transformedTool as unknown as Tool, |
| 2023 | { ...toolMocks.transformedTool, slug: 'ANOTHER_TOOL' } as unknown as Tool, |
| 2024 | ]; |
| 2025 | |
| 2026 | context.mockProvider.wrapTools.mockReturnValueOnce('wrapped-tools'); |
| 2027 | |
| 2028 | const result = context.tools.wrapToolsForToolRouter(sessionId, tools); |
| 2029 | |
| 2030 | expect(context.mockProvider.wrapTools).toHaveBeenCalledWith(tools, expect.any(Function)); |
| 2031 | expect(result).toBe('wrapped-tools'); |
| 2032 | }); |
| 2033 | |
| 2034 | it('should handle empty tools array', async () => { |
| 2035 | const tools: Tool[] = []; |
| 2036 | |
| 2037 | context.mockProvider.wrapTools.mockReturnValueOnce([]); |
| 2038 | |
| 2039 | const result = context.tools.wrapToolsForToolRouter(sessionId, tools); |
| 2040 | |
| 2041 | expect(context.mockProvider.wrapTools).toHaveBeenCalledWith([], expect.any(Function)); |
| 2042 | expect(result).toEqual([]); |
| 2043 | }); |
| 2044 | }); |
| 2045 | }); |
| 2046 | |
| 2047 | describe('Version Integration Tests', () => { |
| 2048 | describe('toolkit versions in tool fetching', () => { |
| 2049 | it('should pass default "latest" version when no version is configured', async () => { |
| 2050 | const context = createTestContext(); |
| 2051 | |
| 2052 | mockClient.tools.list.mockResolvedValueOnce({ |
| 2053 | items: [toolMocks.rawTool], |
| 2054 | totalPages: 1, |
| 2055 | }); |
| 2056 | |
| 2057 | await context.tools.getRawComposioTools({ tools: ['TEST_TOOL'] }); |
| 2058 | |
| 2059 | expect(mockClient.tools.list).toHaveBeenCalledWith( |
| 2060 | { |
| 2061 | tool_slugs: 'TEST_TOOL', |
| 2062 | limit: 9999, |
| 2063 | toolkit_versions: 'latest', |
| 2064 | }, |
| 2065 | undefined |
| 2066 | ); |
| 2067 | }); |
| 2068 | |
| 2069 | it('should pass global version string when configured', async () => { |
| 2070 | const mockProvider = new MockProvider(); |
| 2071 | const tools = new Tools(mockClient, { |
| 2072 | provider: mockProvider, |
| 2073 | toolkitVersions: '20251201_03' as unknown, |
| 2074 | }); |
| 2075 | |
| 2076 | mockClient.tools.list.mockResolvedValueOnce({ |
| 2077 | items: [toolMocks.rawTool], |
| 2078 | totalPages: 1, |
| 2079 | }); |
| 2080 | |
| 2081 | await tools.getRawComposioTools({ toolkits: ['github'] }); |
| 2082 | |
| 2083 | expect(mockClient.tools.list).toHaveBeenCalledWith( |
| 2084 | { |
| 2085 | toolkit_slug: 'github', |
| 2086 | important: 'true', |
| 2087 | toolkit_versions: '20251201_03', |
| 2088 | }, |
| 2089 | undefined |
| 2090 | ); |
| 2091 | }); |
| 2092 | |
| 2093 | it('should pass toolkit-specific versions when configured as object', async () => { |
| 2094 | const mockProvider = new MockProvider(); |
| 2095 | const tools = new Tools(mockClient, { |
| 2096 | provider: mockProvider, |
| 2097 | toolkitVersions: { |
| 2098 | github: '20251201_01', |
| 2099 | slack: 'latest', |
| 2100 | gmail: '20251201_05', |
| 2101 | }, |
| 2102 | }); |
| 2103 | |
| 2104 | mockClient.tools.list.mockResolvedValueOnce({ |
| 2105 | items: [toolMocks.rawTool], |
| 2106 | totalPages: 1, |
| 2107 | }); |
| 2108 | |
| 2109 | await tools.getRawComposioTools({ toolkits: ['github'] }); |
| 2110 | |
| 2111 | expect(mockClient.tools.list).toHaveBeenCalledWith( |
| 2112 | { |
| 2113 | toolkit_slug: 'github', |
| 2114 | important: 'true', |
| 2115 | toolkit_versions: { |
| 2116 | github: '20251201_01', |
| 2117 | slack: 'latest', |
| 2118 | gmail: '20251201_05', |
| 2119 | }, |
| 2120 | }, |
| 2121 | undefined |
| 2122 | ); |
| 2123 | }); |
| 2124 | |
| 2125 | it('should pass versions when fetching tools by tool slugs', async () => { |
| 2126 | const mockProvider = new MockProvider(); |
| 2127 | const tools = new Tools(mockClient, { |
| 2128 | provider: mockProvider, |
| 2129 | toolkitVersions: { |
| 2130 | github: '20251201_01', |
| 2131 | slack: 'latest', |
| 2132 | }, |
| 2133 | }); |
| 2134 | |
| 2135 | mockClient.tools.list.mockResolvedValueOnce({ |
| 2136 | items: [toolMocks.rawTool], |
| 2137 | totalPages: 1, |
| 2138 | }); |
| 2139 | |
| 2140 | await tools.getRawComposioTools({ tools: ['GITHUB_CREATE_ISSUE', 'SLACK_SEND_MESSAGE'] }); |
| 2141 | |
| 2142 | expect(mockClient.tools.list).toHaveBeenCalledWith( |
| 2143 | { |
| 2144 | tool_slugs: 'GITHUB_CREATE_ISSUE,SLACK_SEND_MESSAGE', |
| 2145 | limit: 9999, |
| 2146 | toolkit_versions: { |
| 2147 | github: '20251201_01', |
| 2148 | slack: 'latest', |
| 2149 | }, |
| 2150 | }, |
| 2151 | undefined |
| 2152 | ); |
| 2153 | }); |
| 2154 | |
| 2155 | it('should pass versions when searching tools', async () => { |
| 2156 | const mockProvider = new MockProvider(); |
| 2157 | const tools = new Tools(mockClient, { |
| 2158 | provider: mockProvider, |
| 2159 | toolkitVersions: 'latest', |
| 2160 | }); |
| 2161 | |
| 2162 | mockClient.tools.list.mockResolvedValueOnce({ |
| 2163 | items: [toolMocks.rawTool], |
| 2164 | totalPages: 1, |
| 2165 | }); |
| 2166 | |
| 2167 | await tools.getRawComposioTools({ search: 'create issue' }); |
| 2168 | |
| 2169 | expect(mockClient.tools.list).toHaveBeenCalledWith( |
| 2170 | { |
| 2171 | query: 'create issue', |
| 2172 | toolkit_versions: 'latest', |
| 2173 | }, |
| 2174 | undefined |
| 2175 | ); |
| 2176 | }); |
| 2177 | }); |
| 2178 | |
| 2179 | describe('toolkit versions in individual tool retrieval', () => { |
| 2180 | it('should pass default "latest" version when retrieving single tool', async () => { |
| 2181 | const context = createTestContext(); |
| 2182 | |
| 2183 | mockClient.tools.retrieve.mockResolvedValueOnce(toolMocks.rawTool); |
| 2184 | |
| 2185 | await context.tools.getRawComposioToolBySlug('GITHUB_CREATE_ISSUE'); |
| 2186 | |
| 2187 | expect(mockClient.tools.retrieve).toHaveBeenCalledWith( |
| 2188 | 'GITHUB_CREATE_ISSUE', |
| 2189 | { |
| 2190 | toolkit_versions: 'latest', |
| 2191 | }, |
| 2192 | undefined |
| 2193 | ); |
| 2194 | }); |
| 2195 | |
| 2196 | it('should pass global version when retrieving single tool', async () => { |
| 2197 | const mockProvider = new MockProvider(); |
| 2198 | const tools = new Tools(mockClient, { |
| 2199 | provider: mockProvider, |
| 2200 | toolkitVersions: '20251201_03' as unknown, |
| 2201 | }); |
| 2202 | |
| 2203 | mockClient.tools.retrieve.mockResolvedValueOnce(toolMocks.rawTool); |
| 2204 | |
| 2205 | await tools.getRawComposioToolBySlug('GITHUB_CREATE_ISSUE'); |
| 2206 | |
| 2207 | expect(mockClient.tools.retrieve).toHaveBeenCalledWith( |
| 2208 | 'GITHUB_CREATE_ISSUE', |
| 2209 | { |
| 2210 | toolkit_versions: '20251201_03', |
| 2211 | }, |
| 2212 | undefined |
| 2213 | ); |
| 2214 | }); |
| 2215 | |
| 2216 | it('should pass toolkit-specific versions when retrieving single tool', async () => { |
| 2217 | const mockProvider = new MockProvider(); |
| 2218 | const tools = new Tools(mockClient, { |
| 2219 | provider: mockProvider, |
| 2220 | toolkitVersions: { |
| 2221 | github: '20251201_01', |
| 2222 | slack: 'latest', |
| 2223 | gmail: '20251201_05', |
| 2224 | }, |
| 2225 | }); |
| 2226 | |
| 2227 | mockClient.tools.retrieve.mockResolvedValueOnce(toolMocks.rawTool); |
| 2228 | |
| 2229 | await tools.getRawComposioToolBySlug('SLACK_SEND_MESSAGE'); |
| 2230 | |
| 2231 | expect(mockClient.tools.retrieve).toHaveBeenCalledWith( |
| 2232 | 'SLACK_SEND_MESSAGE', |
| 2233 | { |
| 2234 | toolkit_versions: { |
| 2235 | github: '20251201_01', |
| 2236 | slack: 'latest', |
| 2237 | gmail: '20251201_05', |
| 2238 | }, |
| 2239 | }, |
| 2240 | undefined |
| 2241 | ); |
| 2242 | }); |
| 2243 | }); |
| 2244 | |
| 2245 | describe('toolkit versions in tool execution', () => { |
| 2246 | it('should use default "latest" version when executing tool without explicit version', async () => { |
| 2247 | const context = createTestContext(); |
| 2248 | const spies = await mockToolExecution(context.tools); |
| 2249 | |
| 2250 | const executeParams: ToolExecuteParams = { |
| 2251 | arguments: { title: 'Test Issue' }, |
| 2252 | connectedAccountId: 'test-account', |
| 2253 | dangerouslySkipVersionCheck: true, // Required when using 'latest' in manual execution |
| 2254 | }; |
| 2255 | |
| 2256 | await context.tools.execute('GITHUB_CREATE_ISSUE', executeParams); |
| 2257 | |
| 2258 | expect(mockClient.tools.execute).toHaveBeenCalledWith( |
| 2259 | 'COMPOSIO_TOOL', |
| 2260 | { |
| 2261 | allow_tracing: undefined, |
| 2262 | connected_account_id: 'test-account', |
| 2263 | custom_auth_params: undefined, |
| 2264 | custom_connection_data: undefined, |
| 2265 | arguments: { title: 'Test Issue' }, |
| 2266 | user_id: undefined, |
| 2267 | version: 'latest', // should use latest as default |
| 2268 | text: undefined, |
| 2269 | }, |
| 2270 | { maxRetries: 0 } |
| 2271 | ); |
| 2272 | }); |
| 2273 | |
| 2274 | it('should use global version when configured', async () => { |
| 2275 | const mockProvider = new MockProvider(); |
| 2276 | const tools = new Tools(mockClient, { |
| 2277 | provider: mockProvider, |
| 2278 | toolkitVersions: '20251201_03' as unknown, |
| 2279 | }); |
| 2280 | const spies = await mockToolExecution(tools); |
| 2281 | |
| 2282 | const executeParams: ToolExecuteParams = { |
| 2283 | arguments: { title: 'Test Issue' }, |
| 2284 | connectedAccountId: 'test-account', |
| 2285 | }; |
| 2286 | |
| 2287 | await tools.execute('GITHUB_CREATE_ISSUE', executeParams); |
| 2288 | |
| 2289 | expect(mockClient.tools.execute).toHaveBeenCalledWith( |
| 2290 | 'COMPOSIO_TOOL', |
| 2291 | { |
| 2292 | allow_tracing: undefined, |
| 2293 | connected_account_id: 'test-account', |
| 2294 | custom_auth_params: undefined, |
| 2295 | custom_connection_data: undefined, |
| 2296 | arguments: { title: 'Test Issue' }, |
| 2297 | user_id: undefined, |
| 2298 | version: '20251201_03', // should use global version |
| 2299 | text: undefined, |
| 2300 | }, |
| 2301 | { maxRetries: 0 } |
| 2302 | ); |
| 2303 | }); |
| 2304 | |
| 2305 | it('should use toolkit-specific version when configured as object', async () => { |
| 2306 | const mockProvider = new MockProvider(); |
| 2307 | const tools = new Tools(mockClient, { |
| 2308 | provider: mockProvider, |
| 2309 | toolkitVersions: { |
| 2310 | 'test-toolkit': '20251201_01', // Use the actual toolkit slug from mock |
| 2311 | slack: 'latest', |
| 2312 | gmail: '20251201_05', |
| 2313 | }, |
| 2314 | }); |
| 2315 | |
| 2316 | // Mock the tool with test-toolkit (matching the mock data) |
| 2317 | const testTool = { |
| 2318 | ...toolMocks.transformedTool, |
| 2319 | toolkit: { slug: 'test-toolkit', name: 'Test Toolkit' }, // Use actual mock toolkit |
| 2320 | }; |
| 2321 | |
| 2322 | const spies = await mockToolExecution(tools); |
| 2323 | // Override the mock to return our custom tool with the correct toolkit |
| 2324 | spies.getRawComposioToolBySlugSpy.mockReset(); |
| 2325 | spies.getRawComposioToolBySlugSpy.mockResolvedValueOnce(testTool as unknown as Tool); |
| 2326 | |
| 2327 | const executeParams: ToolExecuteParams = { |
| 2328 | arguments: { title: 'Test Issue' }, |
| 2329 | connectedAccountId: 'test-account', |
| 2330 | }; |
| 2331 | |
| 2332 | await tools.execute('GITHUB_CREATE_ISSUE', executeParams); |
| 2333 | |
| 2334 | expect(mockClient.tools.execute).toHaveBeenCalledWith( |
| 2335 | 'COMPOSIO_TOOL', |
| 2336 | { |
| 2337 | allow_tracing: undefined, |
| 2338 | connected_account_id: 'test-account', |
| 2339 | custom_auth_params: undefined, |
| 2340 | custom_connection_data: undefined, |
| 2341 | arguments: { title: 'Test Issue' }, |
| 2342 | user_id: undefined, |
| 2343 | version: '20251201_01', // should use test-toolkit-specific version |
| 2344 | text: undefined, |
| 2345 | }, |
| 2346 | { maxRetries: 0 } |
| 2347 | ); |
| 2348 | }); |
| 2349 | |
| 2350 | it('should use fallback to "latest" when toolkit not in version mapping', async () => { |
| 2351 | const mockProvider = new MockProvider(); |
| 2352 | const tools = new Tools(mockClient, { |
| 2353 | provider: mockProvider, |
| 2354 | toolkitVersions: { |
| 2355 | github: '20251201_01', |
| 2356 | slack: 'latest', |
| 2357 | }, |
| 2358 | }); |
| 2359 | |
| 2360 | // Mock the tool with notion toolkit (not in version mapping) |
| 2361 | const notionTool = { |
| 2362 | ...toolMocks.transformedTool, |
| 2363 | toolkit: { slug: 'notion', name: 'Notion' }, |
| 2364 | }; |
| 2365 | |
| 2366 | const spies = await mockToolExecution(tools); |
| 2367 | spies.getRawComposioToolBySlugSpy.mockResolvedValueOnce(notionTool as unknown as Tool); |
| 2368 | |
| 2369 | const executeParams: ToolExecuteParams = { |
| 2370 | arguments: { title: 'Test Page' }, |
| 2371 | connectedAccountId: 'test-account', |
| 2372 | dangerouslySkipVersionCheck: true, // Required when fallback is 'latest' |
| 2373 | }; |
| 2374 | |
| 2375 | await tools.execute('NOTION_CREATE_PAGE', executeParams); |
| 2376 | |
| 2377 | expect(mockClient.tools.execute).toHaveBeenCalledWith( |
| 2378 | 'COMPOSIO_TOOL', |
| 2379 | { |
| 2380 | allow_tracing: undefined, |
| 2381 | connected_account_id: 'test-account', |
| 2382 | custom_auth_params: undefined, |
| 2383 | custom_connection_data: undefined, |
| 2384 | arguments: { title: 'Test Page' }, |
| 2385 | user_id: undefined, |
| 2386 | version: 'latest', // should fallback to latest for unknown toolkit |
| 2387 | text: undefined, |
| 2388 | }, |
| 2389 | { maxRetries: 0 } |
| 2390 | ); |
| 2391 | }); |
| 2392 | |
| 2393 | it('should prioritize explicit version parameter over configured versions', async () => { |
| 2394 | const mockProvider = new MockProvider(); |
| 2395 | const tools = new Tools(mockClient, { |
| 2396 | provider: mockProvider, |
| 2397 | toolkitVersions: { |
| 2398 | github: '20251201_01', |
| 2399 | slack: 'latest', |
| 2400 | }, |
| 2401 | }); |
| 2402 | |
| 2403 | const spies = await mockToolExecution(tools); |
| 2404 | |
| 2405 | const executeParams: ToolExecuteParams = { |
| 2406 | arguments: { title: 'Test Issue' }, |
| 2407 | connectedAccountId: 'test-account', |
| 2408 | version: '20251201_03', // explicit version should override config |
| 2409 | }; |
| 2410 | |
| 2411 | await tools.execute('GITHUB_CREATE_ISSUE', executeParams); |
| 2412 | |
| 2413 | expect(mockClient.tools.execute).toHaveBeenCalledWith( |
| 2414 | 'COMPOSIO_TOOL', |
| 2415 | { |
| 2416 | allow_tracing: undefined, |
| 2417 | connected_account_id: 'test-account', |
| 2418 | custom_auth_params: undefined, |
| 2419 | custom_connection_data: undefined, |
| 2420 | arguments: { title: 'Test Issue' }, |
| 2421 | user_id: undefined, |
| 2422 | version: '20251201_03', // explicit version takes precedence |
| 2423 | text: undefined, |
| 2424 | }, |
| 2425 | { maxRetries: 0 } |
| 2426 | ); |
| 2427 | }); |
| 2428 | |
| 2429 | it('should handle tool without toolkit gracefully', async () => { |
| 2430 | const mockProvider = new MockProvider(); |
| 2431 | const tools = new Tools(mockClient, { |
| 2432 | provider: mockProvider, |
| 2433 | toolkitVersions: { |
| 2434 | github: '20251201_01', |
| 2435 | }, |
| 2436 | }); |
| 2437 | |
| 2438 | // Mock the tool without toolkit |
| 2439 | const toolWithoutToolkit = { |
| 2440 | ...toolMocks.transformedTool, |
| 2441 | toolkit: undefined, |
| 2442 | }; |
| 2443 | |
| 2444 | const spies = await mockToolExecution(tools); |
| 2445 | spies.getRawComposioToolBySlugSpy.mockResolvedValueOnce( |
| 2446 | toolWithoutToolkit as unknown as Tool |
| 2447 | ); |
| 2448 | |
| 2449 | const executeParams: ToolExecuteParams = { |
| 2450 | arguments: { query: 'test' }, |
| 2451 | connectedAccountId: 'test-account', |
| 2452 | dangerouslySkipVersionCheck: true, // Required when toolkit is undefined and version is 'latest' |
| 2453 | }; |
| 2454 | |
| 2455 | const beforeExecute = vi.fn().mockImplementation(({ params }) => params); |
| 2456 | const afterExecute = vi.fn().mockImplementation(({ result }) => result); |
| 2457 | |
| 2458 | await tools.execute('SOME_CUSTOM_TOOL', executeParams, { |
| 2459 | beforeExecute, |
| 2460 | afterExecute, |
| 2461 | }); |
| 2462 | |
| 2463 | expect(beforeExecute).toHaveBeenCalledWith({ |
| 2464 | toolSlug: 'SOME_CUSTOM_TOOL', |
| 2465 | toolkitSlug: 'unknown', |
| 2466 | params: executeParams, |
| 2467 | }); |
| 2468 | expect(afterExecute).toHaveBeenCalledWith( |
| 2469 | expect.objectContaining({ |
| 2470 | toolSlug: 'SOME_CUSTOM_TOOL', |
| 2471 | toolkitSlug: 'unknown', |
| 2472 | }) |
| 2473 | ); |
| 2474 | |
| 2475 | expect(mockClient.tools.execute).toHaveBeenCalledWith( |
| 2476 | 'COMPOSIO_TOOL', |
| 2477 | { |
| 2478 | allow_tracing: undefined, |
| 2479 | connected_account_id: 'test-account', |
| 2480 | custom_auth_params: undefined, |
| 2481 | custom_connection_data: undefined, |
| 2482 | arguments: { query: 'test' }, |
| 2483 | user_id: undefined, |
| 2484 | version: 'latest', // should fallback to latest for unknown toolkit |
| 2485 | text: undefined, |
| 2486 | }, |
| 2487 | { maxRetries: 0 } |
| 2488 | ); |
| 2489 | }); |
| 2490 | }); |
| 2491 | |
| 2492 | describe('environment variable integration', () => { |
| 2493 | const originalEnv = process.env; |
| 2494 | |
| 2495 | beforeEach(() => { |
| 2496 | process.env = { ...originalEnv }; |
| 2497 | }); |
| 2498 | |
| 2499 | afterEach(() => { |
| 2500 | process.env = originalEnv; |
| 2501 | }); |
| 2502 | |
| 2503 | it('should use environment variable versions when no user config provided', async () => { |
| 2504 | // Set environment variables |
| 2505 | process.env.COMPOSIO_TOOLKIT_VERSION_GITHUB = '20251201_08'; |
| 2506 | process.env.COMPOSIO_TOOLKIT_VERSION_SLACK = 'latest'; |
| 2507 | |
| 2508 | const mockProvider = new MockProvider(); |
| 2509 | // Pass the processed environment variables to the Tools constructor |
| 2510 | const tools = new Tools(mockClient, { |
| 2511 | provider: mockProvider, |
| 2512 | toolkitVersions: { |
| 2513 | github: '20251201_08', |
| 2514 | slack: 'latest', |
| 2515 | }, |
| 2516 | }); |
| 2517 | |
| 2518 | mockClient.tools.list.mockResolvedValueOnce({ |
| 2519 | items: [toolMocks.rawTool], |
| 2520 | totalPages: 1, |
| 2521 | }); |
| 2522 | |
| 2523 | await tools.getRawComposioTools({ toolkits: ['github'] }); |
| 2524 | |
| 2525 | expect(mockClient.tools.list).toHaveBeenCalledWith( |
| 2526 | { |
| 2527 | toolkit_slug: 'github', |
| 2528 | important: 'true', |
| 2529 | toolkit_versions: { |
| 2530 | github: '20251201_08', |
| 2531 | slack: 'latest', |
| 2532 | }, |
| 2533 | }, |
| 2534 | undefined |
| 2535 | ); |
| 2536 | }); |
| 2537 | |
| 2538 | it('should prioritize user config over environment variables', async () => { |
| 2539 | // Set environment variables |
| 2540 | process.env.COMPOSIO_TOOLKIT_VERSION_GITHUB = '20251201_08'; |
| 2541 | process.env.COMPOSIO_TOOLKIT_VERSION_SLACK = 'latest'; |
| 2542 | |
| 2543 | const mockProvider = new MockProvider(); |
| 2544 | const tools = new Tools(mockClient, { |
| 2545 | provider: mockProvider, |
| 2546 | toolkitVersions: { |
| 2547 | github: '20251201_04', // should override env |
| 2548 | gmail: '20251201_05', // new toolkit not in env |
| 2549 | }, |
| 2550 | }); |
| 2551 | |
| 2552 | mockClient.tools.list.mockResolvedValueOnce({ |
| 2553 | items: [toolMocks.rawTool], |
| 2554 | totalPages: 1, |
| 2555 | }); |
| 2556 | |
| 2557 | await tools.getRawComposioTools({ toolkits: ['github'] }); |
| 2558 | |
| 2559 | expect(mockClient.tools.list).toHaveBeenCalledWith( |
| 2560 | { |
| 2561 | toolkit_slug: 'github', |
| 2562 | important: 'true', |
| 2563 | toolkit_versions: { |
| 2564 | github: '20251201_04', // user config wins |
| 2565 | gmail: '20251201_05', // from user config |
| 2566 | }, |
| 2567 | }, |
| 2568 | undefined |
| 2569 | ); |
| 2570 | }); |
| 2571 | |
| 2572 | it('should use global version string to override everything', async () => { |
| 2573 | // Set environment variables |
| 2574 | process.env.COMPOSIO_TOOLKIT_VERSION_GITHUB = '20251201_08'; |
| 2575 | process.env.COMPOSIO_TOOLKIT_VERSION_SLACK = 'latest'; |
| 2576 | |
| 2577 | const mockProvider = new MockProvider(); |
| 2578 | const tools = new Tools(mockClient, { |
| 2579 | provider: mockProvider, |
| 2580 | toolkitVersions: '20251201_09' as unknown, // global version overrides everything |
| 2581 | }); |
| 2582 | |
| 2583 | mockClient.tools.list.mockResolvedValueOnce({ |
| 2584 | items: [toolMocks.rawTool], |
| 2585 | totalPages: 1, |
| 2586 | }); |
| 2587 | |
| 2588 | await tools.getRawComposioTools({ toolkits: ['github'] }); |
| 2589 | |
| 2590 | expect(mockClient.tools.list).toHaveBeenCalledWith( |
| 2591 | { |
| 2592 | toolkit_slug: 'github', |
| 2593 | important: 'true', |
| 2594 | toolkit_versions: '20251201_09', // global version ignores env vars |
| 2595 | }, |
| 2596 | undefined |
| 2597 | ); |
| 2598 | }); |
| 2599 | }); |
| 2600 | |
| 2601 | describe('version check safety for manual execution', () => { |
| 2602 | it('should throw ComposioToolVersionRequiredError when version is "latest" without dangerouslySkipVersionCheck', async () => { |
| 2603 | const context = createTestContext(); |
| 2604 | const spies = await mockToolExecution(context.tools); |
| 2605 | |
| 2606 | const executeParams: ToolExecuteParams = { |
| 2607 | arguments: { title: 'Test Issue' }, |
| 2608 | connectedAccountId: 'test-account', |
| 2609 | // dangerouslySkipVersionCheck is not set, should default to false |
| 2610 | }; |
| 2611 | |
| 2612 | await expect(context.tools.execute('GITHUB_CREATE_ISSUE', executeParams)).rejects.toThrow( |
| 2613 | 'Toolkit version not specified. For manual execution of the tool please pass a specific toolkit version' |
| 2614 | ); |
| 2615 | }); |
| 2616 | |
| 2617 | it('should throw ComposioToolVersionRequiredError when version is "latest" with dangerouslySkipVersionCheck set to false', async () => { |
| 2618 | const context = createTestContext(); |
| 2619 | const spies = await mockToolExecution(context.tools); |
| 2620 | |
| 2621 | const executeParams: ToolExecuteParams = { |
| 2622 | arguments: { title: 'Test Issue' }, |
| 2623 | connectedAccountId: 'test-account', |
| 2624 | dangerouslySkipVersionCheck: false, |
| 2625 | }; |
| 2626 | |
| 2627 | await expect(context.tools.execute('GITHUB_CREATE_ISSUE', executeParams)).rejects.toThrow( |
| 2628 | 'Toolkit version not specified. For manual execution of the tool please pass a specific toolkit version' |
| 2629 | ); |
| 2630 | }); |
| 2631 | |
| 2632 | it('should succeed when version is "latest" with dangerouslySkipVersionCheck set to true', async () => { |
| 2633 | const context = createTestContext(); |
| 2634 | const spies = await mockToolExecution(context.tools); |
| 2635 | |
| 2636 | const executeParams: ToolExecuteParams = { |
| 2637 | arguments: { title: 'Test Issue' }, |
| 2638 | connectedAccountId: 'test-account', |
| 2639 | dangerouslySkipVersionCheck: true, |
| 2640 | }; |
| 2641 | |
| 2642 | const result = await context.tools.execute('GITHUB_CREATE_ISSUE', executeParams); |
| 2643 | |
| 2644 | expect(result).toEqual(toolMocks.toolExecuteResponse); |
| 2645 | expect(mockClient.tools.execute).toHaveBeenCalledWith( |
| 2646 | 'COMPOSIO_TOOL', |
| 2647 | { |
| 2648 | allow_tracing: undefined, |
| 2649 | connected_account_id: 'test-account', |
| 2650 | custom_auth_params: undefined, |
| 2651 | custom_connection_data: undefined, |
| 2652 | arguments: { title: 'Test Issue' }, |
| 2653 | user_id: undefined, |
| 2654 | version: 'latest', |
| 2655 | text: undefined, |
| 2656 | }, |
| 2657 | { maxRetries: 0 } |
| 2658 | ); |
| 2659 | }); |
| 2660 | |
| 2661 | it('should succeed when executing with a specific version (not "latest") without dangerouslySkipVersionCheck', async () => { |
| 2662 | const mockProvider = new MockProvider(); |
| 2663 | const tools = new Tools(mockClient, { |
| 2664 | provider: mockProvider, |
| 2665 | toolkitVersions: { |
| 2666 | 'test-toolkit': '20251201_01', |
| 2667 | }, |
| 2668 | }); |
| 2669 | |
| 2670 | const testTool = { |
| 2671 | ...toolMocks.transformedTool, |
| 2672 | toolkit: { slug: 'test-toolkit', name: 'Test Toolkit' }, |
| 2673 | }; |
| 2674 | |
| 2675 | const spies = await mockToolExecution(tools); |
| 2676 | spies.getRawComposioToolBySlugSpy.mockReset(); |
| 2677 | spies.getRawComposioToolBySlugSpy.mockResolvedValueOnce(testTool as unknown as Tool); |
| 2678 | |
| 2679 | const executeParams: ToolExecuteParams = { |
| 2680 | arguments: { title: 'Test Issue' }, |
| 2681 | connectedAccountId: 'test-account', |
| 2682 | // No dangerouslySkipVersionCheck needed when version is specific |
| 2683 | }; |
| 2684 | |
| 2685 | const result = await tools.execute('GITHUB_CREATE_ISSUE', executeParams); |
| 2686 | |
| 2687 | expect(result).toEqual(toolMocks.toolExecuteResponse); |
| 2688 | expect(mockClient.tools.execute).toHaveBeenCalledWith( |
| 2689 | 'COMPOSIO_TOOL', |
| 2690 | { |
| 2691 | allow_tracing: undefined, |
| 2692 | connected_account_id: 'test-account', |
| 2693 | custom_auth_params: undefined, |
| 2694 | custom_connection_data: undefined, |
| 2695 | arguments: { title: 'Test Issue' }, |
| 2696 | user_id: undefined, |
| 2697 | version: '20251201_01', // specific version should work without skip flag |
| 2698 | text: undefined, |
| 2699 | }, |
| 2700 | { maxRetries: 0 } |
| 2701 | ); |
| 2702 | }); |
| 2703 | |
| 2704 | it('should succeed when explicit version parameter overrides "latest" config', async () => { |
| 2705 | const context = createTestContext(); |
| 2706 | const spies = await mockToolExecution(context.tools); |
| 2707 | |
| 2708 | const executeParams: ToolExecuteParams = { |
| 2709 | arguments: { title: 'Test Issue' }, |
| 2710 | connectedAccountId: 'test-account', |
| 2711 | version: '20251201_03', // explicit version overrides default "latest" |
| 2712 | // No dangerouslySkipVersionCheck needed |
| 2713 | }; |
| 2714 | |
| 2715 | const result = await context.tools.execute('GITHUB_CREATE_ISSUE', executeParams); |
| 2716 | |
| 2717 | expect(result).toEqual(toolMocks.toolExecuteResponse); |
| 2718 | expect(mockClient.tools.execute).toHaveBeenCalledWith( |
| 2719 | 'COMPOSIO_TOOL', |
| 2720 | { |
| 2721 | allow_tracing: undefined, |
| 2722 | connected_account_id: 'test-account', |
| 2723 | custom_auth_params: undefined, |
| 2724 | custom_connection_data: undefined, |
| 2725 | arguments: { title: 'Test Issue' }, |
| 2726 | user_id: undefined, |
| 2727 | version: '20251201_03', |
| 2728 | text: undefined, |
| 2729 | }, |
| 2730 | { maxRetries: 0 } |
| 2731 | ); |
| 2732 | }); |
| 2733 | |
| 2734 | it('should throw error when explicit version parameter is "latest" without dangerouslySkipVersionCheck', async () => { |
| 2735 | const mockProvider = new MockProvider(); |
| 2736 | const tools = new Tools(mockClient, { |
| 2737 | provider: mockProvider, |
| 2738 | toolkitVersions: { |
| 2739 | 'test-toolkit': '20251201_01', // specific version in config |
| 2740 | }, |
| 2741 | }); |
| 2742 | |
| 2743 | const testTool = { |
| 2744 | ...toolMocks.transformedTool, |
| 2745 | toolkit: { slug: 'test-toolkit', name: 'Test Toolkit' }, |
| 2746 | }; |
| 2747 | |
| 2748 | const spies = await mockToolExecution(tools); |
| 2749 | spies.getRawComposioToolBySlugSpy.mockReset(); |
| 2750 | spies.getRawComposioToolBySlugSpy.mockResolvedValueOnce(testTool as unknown as Tool); |
| 2751 | |
| 2752 | const executeParams: ToolExecuteParams = { |
| 2753 | arguments: { title: 'Test Issue' }, |
| 2754 | connectedAccountId: 'test-account', |
| 2755 | version: 'latest', // explicit "latest" should still require skip flag |
| 2756 | }; |
| 2757 | |
| 2758 | await expect(tools.execute('GITHUB_CREATE_ISSUE', executeParams)).rejects.toThrow( |
| 2759 | 'Toolkit version not specified. For manual execution of the tool please pass a specific toolkit version' |
| 2760 | ); |
| 2761 | }); |
| 2762 | |
| 2763 | it('should contain appropriate error properties and possible fixes in the cause', async () => { |
| 2764 | const context = createTestContext(); |
| 2765 | const spies = await mockToolExecution(context.tools); |
| 2766 | |
| 2767 | const executeParams: ToolExecuteParams = { |
| 2768 | arguments: { title: 'Test Issue' }, |
| 2769 | connectedAccountId: 'test-account', |
| 2770 | }; |
| 2771 | |
| 2772 | try { |
| 2773 | await context.tools.execute('GITHUB_CREATE_ISSUE', executeParams); |
| 2774 | expect.fail('Should have thrown an error'); |
| 2775 | } catch (rawError: unknown) { |
| 2776 | const error = rawError as unknown as ErrorWithPossibleFixes; |
| 2777 | // The error should be wrapped in ComposioToolExecutionError |
| 2778 | expect(error).toBeDefined(); |
| 2779 | |
| 2780 | // Check if the error has the right properties, whether wrapped or not |
| 2781 | if (error.cause) { |
| 2782 | // If wrapped, check the cause |
| 2783 | expect(error.cause.code).toContain('TOOL_VERSION_REQUIRED'); |
| 2784 | expect(error.cause.message).toContain('Toolkit version not specified'); |
| 2785 | expect(error.cause.possibleFixes).toBeDefined(); |
| 2786 | expect(error.cause.possibleFixes).toContain( |
| 2787 | 'Pass the toolkit version as a parameter to the execute function ("latest" is not supported in manual execution)' |
| 2788 | ); |
| 2789 | } else { |
| 2790 | // If not wrapped (direct error), check the error itself |
| 2791 | expect(error.code).toContain('TOOL_VERSION_REQUIRED'); |
| 2792 | expect(error.message).toContain('Toolkit version not specified'); |
| 2793 | expect(error.possibleFixes).toBeDefined(); |
| 2794 | expect(error.possibleFixes).toContain( |
| 2795 | 'Pass the toolkit version as a parameter to the execute function ("latest" is not supported in manual execution)' |
| 2796 | ); |
| 2797 | expect(error.possibleFixes).toContain( |
| 2798 | 'Set the toolkit versions in the Composio config (toolkitVersions: { <toolkit-slug>: "<toolkit-version>" })' |
| 2799 | ); |
| 2800 | expect(error.possibleFixes).toContain( |
| 2801 | 'Set the toolkit version in the environment variable (COMPOSIO_TOOLKIT_VERSION_<TOOLKIT_SLUG>)' |
| 2802 | ); |
| 2803 | expect(error.possibleFixes).toContain( |
| 2804 | 'Set dangerouslySkipVersionCheck to true (this might cause unexpected behavior when new versions of the tools are released)' |
| 2805 | ); |
| 2806 | } |
| 2807 | } |
| 2808 | }); |
| 2809 | |
| 2810 | it('should reuse the fetched schema during agentic provider execution', async () => { |
| 2811 | const context = createTestContext(); |
| 2812 | const userId = 'test-user'; |
| 2813 | const toolSlug = 'GITHUB_CREATE_ISSUE'; |
| 2814 | const fetchedTool = { |
| 2815 | ...toolMocks.transformedTool, |
| 2816 | slug: toolSlug, |
| 2817 | toolkit: { slug: 'github', name: 'GitHub' }, |
| 2818 | } as unknown as Tool; |
| 2819 | const getRawComposioToolBySlugSpy = vi.spyOn(context.tools, 'getRawComposioToolBySlug'); |
| 2820 | getRawComposioToolBySlugSpy.mockResolvedValue(fetchedTool); |
| 2821 | |
| 2822 | let storedExecuteToolFn: ExecuteToolFn | undefined; |
| 2823 | context.mockProvider.wrapTools.mockImplementation((_tools, executeToolFn) => { |
| 2824 | storedExecuteToolFn = executeToolFn; |
| 2825 | return 'wrapped-tools-collection'; |
| 2826 | }); |
| 2827 | |
| 2828 | await context.tools.get(userId, toolSlug); |
| 2829 | mockClient.tools.execute.mockResolvedValueOnce(toolMocks.rawToolExecuteResponse); |
| 2830 | const result = await storedExecuteToolFn!(toolSlug, { title: 'Test Issue' }); |
| 2831 | |
| 2832 | expect(result).toEqual(toolMocks.toolExecuteResponse); |
| 2833 | expect(getRawComposioToolBySlugSpy).toHaveBeenCalledTimes(1); |
| 2834 | expect(mockClient.tools.retrieve).not.toHaveBeenCalled(); |
| 2835 | expect(mockClient.tools.execute).toHaveBeenCalledWith( |
| 2836 | toolSlug, |
| 2837 | { |
| 2838 | allow_tracing: undefined, |
| 2839 | connected_account_id: undefined, |
| 2840 | custom_auth_params: undefined, |
| 2841 | custom_connection_data: undefined, |
| 2842 | arguments: { title: 'Test Issue' }, |
| 2843 | user_id: userId, |
| 2844 | version: 'latest', |
| 2845 | text: undefined, |
| 2846 | }, |
| 2847 | { maxRetries: 0 } |
| 2848 | ); |
| 2849 | }); |
| 2850 | |
| 2851 | it('should reject invalid agentic provider input before execution', async () => { |
| 2852 | const context = createTestContext(); |
| 2853 | const toolSlug = 'GITHUB_CREATE_ISSUE'; |
| 2854 | const fetchedTool = { |
| 2855 | ...toolMocks.transformedTool, |
| 2856 | slug: toolSlug, |
| 2857 | toolkit: { slug: 'github', name: 'GitHub' }, |
| 2858 | } as unknown as Tool; |
| 2859 | const getRawComposioToolBySlugSpy = vi.spyOn(context.tools, 'getRawComposioToolBySlug'); |
| 2860 | getRawComposioToolBySlugSpy.mockResolvedValue(fetchedTool); |
| 2861 | let storedExecuteToolFn: ExecuteToolFn | undefined; |
| 2862 | |
| 2863 | context.mockProvider.wrapTools.mockImplementation((_tools, executeToolFn) => { |
| 2864 | storedExecuteToolFn = executeToolFn; |
| 2865 | return 'wrapped-tools-collection'; |
| 2866 | }); |
| 2867 | |
| 2868 | await context.tools.get('test-user', toolSlug); |
| 2869 | |
| 2870 | await expect( |
| 2871 | storedExecuteToolFn!(toolSlug, 42 as unknown as Record<string, unknown>) |
| 2872 | ).rejects.toThrow(ValidationError); |
| 2873 | expect(getRawComposioToolBySlugSpy).toHaveBeenCalledTimes(1); |
| 2874 | expect(mockClient.tools.execute).not.toHaveBeenCalled(); |
| 2875 | }); |
| 2876 | |
| 2877 | it('should retrieve an unknown provider tool before execution', async () => { |
| 2878 | const context = createTestContext(); |
| 2879 | const userId = 'test-user'; |
| 2880 | const fetchedToolSlug = 'GITHUB_CREATE_ISSUE'; |
| 2881 | const unknownToolSlug = 'GITHUB_GET_ISSUE'; |
| 2882 | const fetchedTool = { |
| 2883 | ...toolMocks.transformedTool, |
| 2884 | slug: fetchedToolSlug, |
| 2885 | toolkit: { slug: 'github', name: 'GitHub' }, |
| 2886 | } as unknown as Tool; |
| 2887 | const unknownTool = { ...fetchedTool, slug: unknownToolSlug }; |
| 2888 | const getRawComposioToolBySlugSpy = vi.spyOn(context.tools, 'getRawComposioToolBySlug'); |
| 2889 | getRawComposioToolBySlugSpy |
| 2890 | .mockResolvedValueOnce(fetchedTool) |
| 2891 | .mockResolvedValueOnce(unknownTool); |
| 2892 | let storedExecuteToolFn: ExecuteToolFn | undefined; |
| 2893 | |
| 2894 | context.mockProvider.wrapTools.mockImplementation((_tools, executeToolFn) => { |
| 2895 | storedExecuteToolFn = executeToolFn; |
| 2896 | return 'wrapped-tools-collection'; |
| 2897 | }); |
| 2898 | |
| 2899 | await context.tools.get(userId, fetchedToolSlug); |
| 2900 | mockClient.tools.execute.mockResolvedValueOnce(toolMocks.rawToolExecuteResponse); |
| 2901 | await storedExecuteToolFn!(unknownToolSlug, {}); |
| 2902 | |
| 2903 | expect(getRawComposioToolBySlugSpy).toHaveBeenCalledTimes(2); |
| 2904 | expect(getRawComposioToolBySlugSpy).toHaveBeenNthCalledWith( |
| 2905 | 2, |
| 2906 | unknownToolSlug, |
| 2907 | { version: undefined }, |
| 2908 | undefined |
| 2909 | ); |
| 2910 | expect(mockClient.tools.execute).toHaveBeenCalledWith( |
| 2911 | unknownToolSlug, |
| 2912 | expect.objectContaining({ user_id: userId, version: 'latest' }), |
| 2913 | { maxRetries: 0 } |
| 2914 | ); |
| 2915 | }); |
| 2916 | |
| 2917 | it('should preserve execution metadata when modifySchema mutates the fetched tool', async () => { |
| 2918 | const mockProvider = new MockProvider(); |
| 2919 | const tools = new Tools(mockClient, { |
| 2920 | provider: mockProvider, |
| 2921 | toolkitVersions: { 'test-toolkit': '20250101_00' }, |
| 2922 | }); |
| 2923 | const userId = 'test-user'; |
| 2924 | const toolSlug = 'GITHUB_CREATE_ISSUE'; |
| 2925 | const rawTool = { |
| 2926 | ...toolMocks.rawTool, |
| 2927 | slug: toolSlug, |
| 2928 | toolkit: { slug: 'test-toolkit', name: 'Test Toolkit' }, |
| 2929 | }; |
| 2930 | let wrappedTools: Tool[] | undefined; |
| 2931 | let storedExecuteToolFn: ExecuteToolFn | undefined; |
| 2932 | |
| 2933 | mockClient.tools.retrieve.mockReset().mockResolvedValueOnce(rawTool); |
| 2934 | mockProvider.wrapTools.mockImplementation((toolsToWrap, executeToolFn) => { |
| 2935 | wrappedTools = toolsToWrap; |
| 2936 | storedExecuteToolFn = executeToolFn; |
| 2937 | return 'wrapped-tools'; |
| 2938 | }); |
| 2939 | |
| 2940 | await tools.get(userId, toolSlug, { |
| 2941 | modifySchema: ({ schema }) => { |
| 2942 | schema.toolkit = { slug: 'renamed-toolkit', name: 'Renamed Toolkit' }; |
| 2943 | return schema; |
| 2944 | }, |
| 2945 | }); |
| 2946 | mockClient.tools.execute.mockResolvedValueOnce(toolMocks.rawToolExecuteResponse); |
| 2947 | await storedExecuteToolFn!(toolSlug, {}); |
| 2948 | |
| 2949 | expect(wrappedTools?.[0].toolkit?.slug).toBe('renamed-toolkit'); |
| 2950 | expect(mockClient.tools.retrieve).toHaveBeenCalledTimes(1); |
| 2951 | expect(mockClient.tools.execute).toHaveBeenCalledWith( |
| 2952 | toolSlug, |
| 2953 | expect.objectContaining({ version: '20250101_00' }), |
| 2954 | { maxRetries: 0 } |
| 2955 | ); |
| 2956 | }); |
| 2957 | }); |
| 2958 | }); |
| 2959 | }); |