Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 2 | import { FileToolModifier } from '#file_tool_modifier'; |
| 3 | import ComposioClient from '@composio/client'; |
| 4 | import { Tool } from '../../src/types/tool.types'; |
| 5 | import { |
| 6 | ComposioFileUploadAbortedError, |
| 7 | ComposioFileUploadError, |
| 8 | } from '../../src/errors/FileModifierErrors'; |
| 9 | import { ComposioRequestCancelledError } from '../../src/errors/SDKErrors'; |
| 10 | import * as fileUtils from '../../src/utils/fileUtils.node'; |
| 11 | import { Tools } from '../../src/models/Tools'; |
| 12 | import { ToolRouterSession } from '../../src/models/ToolRouterSession'; |
| 13 | import { createTestContext, setupTest, mockToolExecution } from '../utils/toolExecuteUtils'; |
| 14 | import { mockClient } from '../utils/mocks/client.mock'; |
| 15 | import { MockProvider } from '../utils/mocks/provider.mock'; |
| 16 | |
| 17 | // Mock the fileUtils module |
| 18 | vi.mock('../../src/utils/fileUtils.node', () => ({ |
| 19 | downloadFileFromS3: vi.fn(), |
| 20 | getFileDataAfterUploadingToS3: vi.fn(), |
| 21 | isHttpUrl: (value: string): boolean => { |
| 22 | try { |
| 23 | const url = new URL(value); |
| 24 | return url.protocol === 'http:' || url.protocol === 'https:'; |
| 25 | } catch { |
| 26 | return false; |
| 27 | } |
| 28 | }, |
| 29 | })); |
| 30 | |
| 31 | describe('FileToolModifier', () => { |
| 32 | let fileToolModifier: FileToolModifier; |
| 33 | let mockClient: ComposioClient; |
| 34 | |
| 35 | beforeEach(() => { |
| 36 | mockClient = { |
| 37 | baseURL: 'https://api.composio.dev', |
| 38 | apiKey: 'test-api-key', |
| 39 | } as ComposioClient; |
| 40 | fileToolModifier = new FileToolModifier(mockClient); |
| 41 | vi.clearAllMocks(); |
| 42 | }); |
| 43 | |
| 44 | describe('modifyToolSchema', () => { |
| 45 | it('should return schema unchanged if no input parameters', async () => { |
| 46 | const schema: Tool = { |
| 47 | slug: 'test-tool', |
| 48 | name: 'Test Tool', |
| 49 | description: 'A test tool', |
| 50 | tags: ['test'], |
| 51 | version: '20251201_01', |
| 52 | availableVersions: ['20251201_01'], |
| 53 | }; |
| 54 | |
| 55 | const result = await fileToolModifier.modifyToolSchema(schema); |
| 56 | expect(result).toEqual(schema); |
| 57 | }); |
| 58 | |
| 59 | it('should add format: "path" to file_uploadable properties', async () => { |
| 60 | const schema: Tool = { |
| 61 | slug: 'test-tool', |
| 62 | name: 'Test Tool', |
| 63 | description: 'A test tool', |
| 64 | tags: ['test'], |
| 65 | version: '20251201_01', |
| 66 | availableVersions: ['20251201_01'], |
| 67 | inputParameters: { |
| 68 | type: 'object', |
| 69 | properties: { |
| 70 | file: { |
| 71 | type: 'string', |
| 72 | file_uploadable: true, |
| 73 | }, |
| 74 | text: { |
| 75 | type: 'string', |
| 76 | }, |
| 77 | }, |
| 78 | }, |
| 79 | }; |
| 80 | |
| 81 | const result = await fileToolModifier.modifyToolSchema(schema); |
| 82 | expect(result.inputParameters?.properties?.file).toHaveProperty('format', 'path'); |
| 83 | expect(result.inputParameters?.properties?.text).not.toHaveProperty('format'); |
| 84 | }); |
| 85 | |
| 86 | it('should transform file_uploadable properties inside anyOf', async () => { |
| 87 | const schema: Tool = { |
| 88 | slug: 'test-tool', |
| 89 | name: 'Test Tool', |
| 90 | description: 'A test tool', |
| 91 | tags: ['test'], |
| 92 | version: '20251201_01', |
| 93 | availableVersions: ['20251201_01'], |
| 94 | inputParameters: { |
| 95 | type: 'object', |
| 96 | properties: { |
| 97 | fileInput: { |
| 98 | anyOf: [ |
| 99 | { |
| 100 | type: 'string', |
| 101 | file_uploadable: true, |
| 102 | description: 'File path', |
| 103 | }, |
| 104 | { |
| 105 | type: 'null', |
| 106 | }, |
| 107 | ], |
| 108 | }, |
| 109 | text: { |
| 110 | type: 'string', |
| 111 | }, |
| 112 | }, |
| 113 | }, |
| 114 | }; |
| 115 | |
| 116 | const result = await fileToolModifier.modifyToolSchema(schema); |
| 117 | expect(result.inputParameters?.properties?.fileInput?.anyOf?.[0]).toHaveProperty( |
| 118 | 'format', |
| 119 | 'path' |
| 120 | ); |
| 121 | expect(result.inputParameters?.properties?.fileInput?.anyOf?.[0]).toHaveProperty( |
| 122 | 'file_uploadable', |
| 123 | true |
| 124 | ); |
| 125 | expect(result.inputParameters?.properties?.fileInput?.anyOf?.[1]).not.toHaveProperty( |
| 126 | 'format' |
| 127 | ); |
| 128 | expect(result.inputParameters?.properties?.text).not.toHaveProperty('format'); |
| 129 | }); |
| 130 | |
| 131 | it('should transform file_uploadable properties inside oneOf', async () => { |
| 132 | const schema: Tool = { |
| 133 | slug: 'test-tool', |
| 134 | name: 'Test Tool', |
| 135 | description: 'A test tool', |
| 136 | tags: ['test'], |
| 137 | version: '20251201_01', |
| 138 | availableVersions: ['20251201_01'], |
| 139 | inputParameters: { |
| 140 | type: 'object', |
| 141 | properties: { |
| 142 | fileInput: { |
| 143 | oneOf: [ |
| 144 | { |
| 145 | type: 'string', |
| 146 | file_uploadable: true, |
| 147 | title: 'Upload File', |
| 148 | }, |
| 149 | { |
| 150 | type: 'string', |
| 151 | description: 'URL reference', |
| 152 | }, |
| 153 | ], |
| 154 | }, |
| 155 | }, |
| 156 | }, |
| 157 | }; |
| 158 | |
| 159 | const result = await fileToolModifier.modifyToolSchema(schema); |
| 160 | expect(result.inputParameters?.properties?.fileInput?.oneOf?.[0]).toHaveProperty( |
| 161 | 'format', |
| 162 | 'path' |
| 163 | ); |
| 164 | expect(result.inputParameters?.properties?.fileInput?.oneOf?.[0]).toHaveProperty( |
| 165 | 'file_uploadable', |
| 166 | true |
| 167 | ); |
| 168 | expect(result.inputParameters?.properties?.fileInput?.oneOf?.[1]).not.toHaveProperty( |
| 169 | 'format' |
| 170 | ); |
| 171 | }); |
| 172 | |
| 173 | it('should transform file_uploadable properties inside allOf', async () => { |
| 174 | const schema: Tool = { |
| 175 | slug: 'test-tool', |
| 176 | name: 'Test Tool', |
| 177 | description: 'A test tool', |
| 178 | tags: ['test'], |
| 179 | version: '20251201_01', |
| 180 | availableVersions: ['20251201_01'], |
| 181 | inputParameters: { |
| 182 | type: 'object', |
| 183 | properties: { |
| 184 | fileInput: { |
| 185 | allOf: [ |
| 186 | { |
| 187 | type: 'string', |
| 188 | file_uploadable: true, |
| 189 | }, |
| 190 | { |
| 191 | minLength: 1, |
| 192 | }, |
| 193 | ], |
| 194 | }, |
| 195 | }, |
| 196 | }, |
| 197 | }; |
| 198 | |
| 199 | const result = await fileToolModifier.modifyToolSchema(schema); |
| 200 | expect(result.inputParameters?.properties?.fileInput?.allOf?.[0]).toHaveProperty( |
| 201 | 'format', |
| 202 | 'path' |
| 203 | ); |
| 204 | expect(result.inputParameters?.properties?.fileInput?.allOf?.[0]).toHaveProperty( |
| 205 | 'file_uploadable', |
| 206 | true |
| 207 | ); |
| 208 | expect(result.inputParameters?.properties?.fileInput?.allOf?.[1]).not.toHaveProperty( |
| 209 | 'format' |
| 210 | ); |
| 211 | }); |
| 212 | |
| 213 | it('should transform nested file_uploadable properties inside anyOf with objects', async () => { |
| 214 | const schema: Tool = { |
| 215 | slug: 'test-tool', |
| 216 | name: 'Test Tool', |
| 217 | description: 'A test tool', |
| 218 | tags: ['test'], |
| 219 | version: '20251201_01', |
| 220 | availableVersions: ['20251201_01'], |
| 221 | inputParameters: { |
| 222 | type: 'object', |
| 223 | properties: { |
| 224 | content: { |
| 225 | anyOf: [ |
| 226 | { |
| 227 | type: 'object', |
| 228 | properties: { |
| 229 | attachment: { |
| 230 | type: 'string', |
| 231 | file_uploadable: true, |
| 232 | }, |
| 233 | }, |
| 234 | }, |
| 235 | { |
| 236 | type: 'string', |
| 237 | }, |
| 238 | ], |
| 239 | }, |
| 240 | }, |
| 241 | }, |
| 242 | }; |
| 243 | |
| 244 | const result = await fileToolModifier.modifyToolSchema(schema); |
| 245 | expect( |
| 246 | result.inputParameters?.properties?.content?.anyOf?.[0]?.properties?.attachment |
| 247 | ).toHaveProperty('format', 'path'); |
| 248 | expect( |
| 249 | result.inputParameters?.properties?.content?.anyOf?.[0]?.properties?.attachment |
| 250 | ).toHaveProperty('file_uploadable', true); |
| 251 | }); |
| 252 | |
| 253 | it('should transform file_uploadable properties inside array items with anyOf', async () => { |
| 254 | const schema: Tool = { |
| 255 | slug: 'test-tool', |
| 256 | name: 'Test Tool', |
| 257 | description: 'A test tool', |
| 258 | tags: ['test'], |
| 259 | version: '20251201_01', |
| 260 | availableVersions: ['20251201_01'], |
| 261 | inputParameters: { |
| 262 | type: 'object', |
| 263 | properties: { |
| 264 | files: { |
| 265 | type: 'array', |
| 266 | items: { |
| 267 | anyOf: [ |
| 268 | { |
| 269 | type: 'string', |
| 270 | file_uploadable: true, |
| 271 | }, |
| 272 | { |
| 273 | type: 'null', |
| 274 | }, |
| 275 | ], |
| 276 | }, |
| 277 | }, |
| 278 | }, |
| 279 | }, |
| 280 | }; |
| 281 | |
| 282 | const result = await fileToolModifier.modifyToolSchema(schema); |
| 283 | expect(result.inputParameters?.properties?.files?.items?.anyOf?.[0]).toHaveProperty( |
| 284 | 'format', |
| 285 | 'path' |
| 286 | ); |
| 287 | expect(result.inputParameters?.properties?.files?.items?.anyOf?.[0]).toHaveProperty( |
| 288 | 'file_uploadable', |
| 289 | true |
| 290 | ); |
| 291 | }); |
| 292 | }); |
| 293 | |
| 294 | describe('fileUploadModifier', () => { |
| 295 | const mockTool: Tool = { |
| 296 | slug: 'test-tool', |
| 297 | name: 'Test Tool', |
| 298 | description: 'A test tool', |
| 299 | tags: ['test'], |
| 300 | inputParameters: { |
| 301 | type: 'object', |
| 302 | properties: { |
| 303 | file: { |
| 304 | type: 'string', |
| 305 | file_uploadable: true, |
| 306 | }, |
| 307 | text: { |
| 308 | type: 'string', |
| 309 | }, |
| 310 | }, |
| 311 | }, |
| 312 | version: '20251201_01', |
| 313 | availableVersions: ['20251201_01'], |
| 314 | }; |
| 315 | |
| 316 | it('drops empty file values instead of uploading them (issue #4233)', async () => { |
| 317 | const fileUploadable = { |
| 318 | type: 'object' as const, |
| 319 | title: 'FileUploadable', |
| 320 | file_uploadable: true, |
| 321 | properties: { |
| 322 | name: { type: 'string' as const }, |
| 323 | mimetype: { type: 'string' as const }, |
| 324 | s3key: { type: 'string' as const }, |
| 325 | }, |
| 326 | required: ['name', 'mimetype', 's3key'], |
| 327 | }; |
| 328 | const gmailLikeTool: Tool = { |
| 329 | ...mockTool, |
| 330 | inputParameters: { |
| 331 | type: 'object', |
| 332 | properties: { |
| 333 | subject: { type: 'string' }, |
| 334 | attachment: { anyOf: [fileUploadable, { type: 'array', items: fileUploadable }] }, |
| 335 | extra: { type: 'array', items: fileUploadable }, |
| 336 | thread_id: { type: 'string' }, |
| 337 | }, |
| 338 | }, |
| 339 | }; |
| 340 | const staged = { name: 'a.txt', mimetype: 'text/plain', s3key: 'k' }; |
| 341 | |
| 342 | const result = await fileToolModifier.fileUploadModifier(gmailLikeTool, { |
| 343 | toolSlug: 'test-tool', |
| 344 | toolkitSlug: 'test-toolkit', |
| 345 | params: { |
| 346 | arguments: { |
| 347 | subject: 'Test', |
| 348 | attachment: '', |
| 349 | extra: ['', staged], |
| 350 | thread_id: '', |
| 351 | }, |
| 352 | userId: 'test-user', |
| 353 | }, |
| 354 | }); |
| 355 | |
| 356 | expect(fileUtils.getFileDataAfterUploadingToS3).not.toHaveBeenCalled(); |
| 357 | expect(result.arguments).toEqual({ |
| 358 | subject: 'Test', |
| 359 | extra: [staged], |
| 360 | // non-file empty strings are not the walker's business |
| 361 | thread_id: '', |
| 362 | }); |
| 363 | }); |
| 364 | |
| 365 | it('drops scalar empty values for array-only file inputs and preserves null and staged files', async () => { |
| 366 | const fileUploadable = { |
| 367 | type: 'object' as const, |
| 368 | file_uploadable: true, |
| 369 | properties: { |
| 370 | name: { type: 'string' as const }, |
| 371 | mimetype: { type: 'string' as const }, |
| 372 | s3key: { type: 'string' as const }, |
| 373 | }, |
| 374 | }; |
| 375 | const staged = { name: 'a.txt', mimetype: 'text/plain', s3key: 'staged/a.txt' }; |
| 376 | const arrayFileTool: Tool = { |
| 377 | ...mockTool, |
| 378 | inputParameters: { |
| 379 | type: 'object', |
| 380 | properties: { |
| 381 | files: { |
| 382 | anyOf: [{ type: 'array', items: fileUploadable }, { type: 'null' }], |
| 383 | }, |
| 384 | directFiles: { type: 'array', items: fileUploadable }, |
| 385 | nullableFile: { anyOf: [fileUploadable, { type: 'null' }] }, |
| 386 | stagedFiles: { type: 'array', items: fileUploadable }, |
| 387 | }, |
| 388 | }, |
| 389 | }; |
| 390 | |
| 391 | const result = await fileToolModifier.fileUploadModifier(arrayFileTool, { |
| 392 | toolSlug: 'test-tool', |
| 393 | toolkitSlug: 'test-toolkit', |
| 394 | params: { |
| 395 | arguments: { |
| 396 | files: '', |
| 397 | directFiles: '', |
| 398 | nullableFile: null, |
| 399 | stagedFiles: [staged], |
| 400 | }, |
| 401 | userId: 'test-user', |
| 402 | }, |
| 403 | }); |
| 404 | |
| 405 | expect(fileUtils.getFileDataAfterUploadingToS3).not.toHaveBeenCalled(); |
| 406 | expect(result.arguments).toEqual({ |
| 407 | nullableFile: null, |
| 408 | stagedFiles: [staged], |
| 409 | }); |
| 410 | }); |
| 411 | |
| 412 | it('drops scalar empty values for composed file arrays inferred from items', async () => { |
| 413 | const typelessArrayFileTool: Tool = { |
| 414 | ...mockTool, |
| 415 | inputParameters: { |
| 416 | type: 'object', |
| 417 | properties: { |
| 418 | files: { |
| 419 | anyOf: [ |
| 420 | { |
| 421 | items: { type: 'object', file_uploadable: true }, |
| 422 | }, |
| 423 | { type: 'null' }, |
| 424 | ], |
| 425 | }, |
| 426 | }, |
| 427 | }, |
| 428 | }; |
| 429 | |
| 430 | const result = await fileToolModifier.fileUploadModifier(typelessArrayFileTool, { |
| 431 | toolSlug: 'test-tool', |
| 432 | toolkitSlug: 'test-toolkit', |
| 433 | params: { |
| 434 | arguments: { files: '' }, |
| 435 | userId: 'test-user', |
| 436 | }, |
| 437 | }); |
| 438 | |
| 439 | expect(fileUtils.getFileDataAfterUploadingToS3).not.toHaveBeenCalled(); |
| 440 | expect(result.arguments).toEqual({}); |
| 441 | }); |
| 442 | |
| 443 | it('should upload file for file_uploadable parameters', async () => { |
| 444 | const mockFileData = { |
| 445 | name: 'file.txt', |
| 446 | mimetype: 'text/plain', |
| 447 | s3key: 'uploads/file.txt', |
| 448 | }; |
| 449 | vi.mocked(fileUtils.getFileDataAfterUploadingToS3).mockResolvedValue(mockFileData); |
| 450 | |
| 451 | const params = { |
| 452 | arguments: { |
| 453 | file: '/path/to/file.txt', |
| 454 | text: 'some text', |
| 455 | }, |
| 456 | userId: 'test-user', |
| 457 | }; |
| 458 | |
| 459 | const result = await fileToolModifier.fileUploadModifier(mockTool, { |
| 460 | toolSlug: 'test-tool', |
| 461 | toolkitSlug: 'test-toolkit', |
| 462 | params, |
| 463 | }); |
| 464 | |
| 465 | expect(fileUtils.getFileDataAfterUploadingToS3).toHaveBeenCalledWith('/path/to/file.txt', { |
| 466 | toolSlug: 'test-tool', |
| 467 | toolkitSlug: 'test-toolkit', |
| 468 | client: mockClient, |
| 469 | }); |
| 470 | expect(result.arguments?.file).toEqual(mockFileData); |
| 471 | expect(result.arguments?.text).toBe('some text'); |
| 472 | }); |
| 473 | |
| 474 | it('should call beforeFileUpload and use returned path for string files', async () => { |
| 475 | const mockFileData = { |
| 476 | name: 'file.txt', |
| 477 | mimetype: 'text/plain', |
| 478 | s3key: 'uploads/file.txt', |
| 479 | }; |
| 480 | vi.mocked(fileUtils.getFileDataAfterUploadingToS3).mockResolvedValue(mockFileData); |
| 481 | |
| 482 | const withHook = new FileToolModifier(mockClient, { |
| 483 | beforeFileUpload: async ({ path, toolSlug, toolkitSlug }) => { |
| 484 | expect(toolSlug).toBe('test-tool'); |
| 485 | expect(toolkitSlug).toBe('test-toolkit'); |
| 486 | return path.replace('original', 'rewritten'); |
| 487 | }, |
| 488 | }); |
| 489 | |
| 490 | const params = { |
| 491 | arguments: { file: '/path/to/original.txt' }, |
| 492 | userId: 'test-user', |
| 493 | }; |
| 494 | |
| 495 | const result = await withHook.fileUploadModifier(mockTool, { |
| 496 | toolSlug: 'test-tool', |
| 497 | toolkitSlug: 'test-toolkit', |
| 498 | params, |
| 499 | }); |
| 500 | |
| 501 | expect(fileUtils.getFileDataAfterUploadingToS3).toHaveBeenCalledWith( |
| 502 | '/path/to/rewritten.txt', |
| 503 | { |
| 504 | toolSlug: 'test-tool', |
| 505 | toolkitSlug: 'test-toolkit', |
| 506 | client: mockClient, |
| 507 | } |
| 508 | ); |
| 509 | expect(result.arguments?.file).toEqual(mockFileData); |
| 510 | }); |
| 511 | |
| 512 | it('should pass source="path" to beforeFileUpload for local filesystem paths', async () => { |
| 513 | const mockFileData = { |
| 514 | name: 'file.txt', |
| 515 | mimetype: 'text/plain', |
| 516 | s3key: 'uploads/file.txt', |
| 517 | }; |
| 518 | vi.mocked(fileUtils.getFileDataAfterUploadingToS3).mockResolvedValue(mockFileData); |
| 519 | |
| 520 | const hook = vi.fn(async ({ path }) => path); |
| 521 | const withHook = new FileToolModifier(mockClient, { beforeFileUpload: hook }); |
| 522 | |
| 523 | await withHook.fileUploadModifier(mockTool, { |
| 524 | toolSlug: 'test-tool', |
| 525 | toolkitSlug: 'test-toolkit', |
| 526 | params: { arguments: { file: '/path/to/a.txt' }, userId: 'u' }, |
| 527 | }); |
| 528 | |
| 529 | expect(hook).toHaveBeenCalledWith( |
| 530 | expect.objectContaining({ path: '/path/to/a.txt', source: 'path' }) |
| 531 | ); |
| 532 | }); |
| 533 | |
| 534 | it('should pass source="url" to beforeFileUpload for http(s) URLs', async () => { |
| 535 | const mockFileData = { |
| 536 | name: 'file.txt', |
| 537 | mimetype: 'text/plain', |
| 538 | s3key: 'uploads/file.txt', |
| 539 | }; |
| 540 | vi.mocked(fileUtils.getFileDataAfterUploadingToS3).mockResolvedValue(mockFileData); |
| 541 | |
| 542 | const hook = vi.fn(async ({ path }) => path); |
| 543 | const withHook = new FileToolModifier(mockClient, { beforeFileUpload: hook }); |
| 544 | |
| 545 | await withHook.fileUploadModifier(mockTool, { |
| 546 | toolSlug: 'test-tool', |
| 547 | toolkitSlug: 'test-toolkit', |
| 548 | params: { |
| 549 | arguments: { file: 'https://example.com/report.pdf' }, |
| 550 | userId: 'u', |
| 551 | }, |
| 552 | }); |
| 553 | |
| 554 | expect(hook).toHaveBeenCalledWith( |
| 555 | expect.objectContaining({ |
| 556 | path: 'https://example.com/report.pdf', |
| 557 | source: 'url', |
| 558 | }) |
| 559 | ); |
| 560 | }); |
| 561 | |
| 562 | it('should pass source="file" to beforeFileUpload for File objects', async () => { |
| 563 | const mockFileData = { |
| 564 | name: 'file.txt', |
| 565 | mimetype: 'text/plain', |
| 566 | s3key: 'uploads/file.txt', |
| 567 | }; |
| 568 | vi.mocked(fileUtils.getFileDataAfterUploadingToS3).mockResolvedValue(mockFileData); |
| 569 | |
| 570 | const hook = vi.fn(async () => undefined as unknown as string); |
| 571 | const withHook = new FileToolModifier(mockClient, { beforeFileUpload: hook }); |
| 572 | |
| 573 | const fileObject = new File(['x'], 'uploaded.txt', { type: 'text/plain' }); |
| 574 | await withHook.fileUploadModifier(mockTool, { |
| 575 | toolSlug: 'test-tool', |
| 576 | toolkitSlug: 'test-toolkit', |
| 577 | params: { arguments: { file: fileObject }, userId: 'u' }, |
| 578 | }); |
| 579 | |
| 580 | // `path` is the File's name, not a filesystem path. |
| 581 | expect(hook).toHaveBeenCalledWith( |
| 582 | expect.objectContaining({ path: 'uploaded.txt', source: 'file' }) |
| 583 | ); |
| 584 | }); |
| 585 | |
| 586 | it('should throw ComposioFileUploadAbortedError when beforeFileUpload returns false', async () => { |
| 587 | const withHook = new FileToolModifier(mockClient, { |
| 588 | beforeFileUpload: async () => false, |
| 589 | }); |
| 590 | |
| 591 | await expect( |
| 592 | withHook.fileUploadModifier(mockTool, { |
| 593 | toolSlug: 'test-tool', |
| 594 | toolkitSlug: 'test-toolkit', |
| 595 | params: { arguments: { file: '/any/path' }, userId: 'u' }, |
| 596 | }) |
| 597 | ).rejects.toThrow(ComposioFileUploadAbortedError); |
| 598 | expect(fileUtils.getFileDataAfterUploadingToS3).not.toHaveBeenCalled(); |
| 599 | }); |
| 600 | |
| 601 | it('should throw ComposioFileUploadError on upload failure', async () => { |
| 602 | vi.mocked(fileUtils.getFileDataAfterUploadingToS3).mockRejectedValue( |
| 603 | new Error('Upload failed') |
| 604 | ); |
| 605 | |
| 606 | const params = { |
| 607 | arguments: { |
| 608 | file: '/path/to/file.txt', |
| 609 | }, |
| 610 | userId: 'test-user', |
| 611 | }; |
| 612 | |
| 613 | await expect( |
| 614 | fileToolModifier.fileUploadModifier(mockTool, { |
| 615 | toolSlug: 'test-tool', |
| 616 | toolkitSlug: 'test-toolkit', |
| 617 | params, |
| 618 | }) |
| 619 | ).rejects.toThrow(ComposioFileUploadError); |
| 620 | }); |
| 621 | |
| 622 | it('should throw ComposioRequestCancelledError on caller-aborted upload', async () => { |
| 623 | const controller = new AbortController(); |
| 624 | vi.mocked(fileUtils.getFileDataAfterUploadingToS3).mockImplementationOnce(async () => { |
| 625 | controller.abort(); |
| 626 | const error = new Error('The operation was aborted'); |
| 627 | error.name = 'AbortError'; |
| 628 | throw error; |
| 629 | }); |
| 630 | |
| 631 | const params = { |
| 632 | arguments: { |
| 633 | file: '/path/to/file.txt', |
| 634 | }, |
| 635 | userId: 'test-user', |
| 636 | }; |
| 637 | |
| 638 | await expect( |
| 639 | fileToolModifier.fileUploadModifier(mockTool, { |
| 640 | toolSlug: 'test-tool', |
| 641 | toolkitSlug: 'test-toolkit', |
| 642 | params, |
| 643 | signal: controller.signal, |
| 644 | }) |
| 645 | ).rejects.toBeInstanceOf(ComposioRequestCancelledError); |
| 646 | }); |
| 647 | |
| 648 | it('should handle File object for file_uploadable parameters', async () => { |
| 649 | const mockFileData = { |
| 650 | name: 'file.txt', |
| 651 | mimetype: 'text/plain', |
| 652 | s3key: 'uploads/file.txt', |
| 653 | }; |
| 654 | vi.mocked(fileUtils.getFileDataAfterUploadingToS3).mockResolvedValue(mockFileData); |
| 655 | |
| 656 | const fileObject = new File(['test content'], 'file.txt', { type: 'text/plain' }); |
| 657 | const params = { |
| 658 | arguments: { |
| 659 | file: fileObject, |
| 660 | text: 'some text', |
| 661 | }, |
| 662 | userId: 'test-user', |
| 663 | }; |
| 664 | |
| 665 | const result = await fileToolModifier.fileUploadModifier(mockTool, { |
| 666 | toolSlug: 'test-tool', |
| 667 | toolkitSlug: 'test-toolkit', |
| 668 | params, |
| 669 | }); |
| 670 | |
| 671 | expect(fileUtils.getFileDataAfterUploadingToS3).toHaveBeenCalledWith(fileObject, { |
| 672 | toolSlug: 'test-tool', |
| 673 | toolkitSlug: 'test-toolkit', |
| 674 | client: mockClient, |
| 675 | }); |
| 676 | expect(result.arguments?.file).toEqual(mockFileData); |
| 677 | expect(result.arguments?.text).toBe('some text'); |
| 678 | }); |
| 679 | |
| 680 | it('should upload file for file_uploadable inside anyOf schema', async () => { |
| 681 | const mockFileData = { |
| 682 | name: 'file.txt', |
| 683 | mimetype: 'text/plain', |
| 684 | s3key: 'uploads/file.txt', |
| 685 | }; |
| 686 | vi.mocked(fileUtils.getFileDataAfterUploadingToS3).mockResolvedValue(mockFileData); |
| 687 | |
| 688 | const toolWithAnyOf: Tool = { |
| 689 | slug: 'test-tool', |
| 690 | name: 'Test Tool', |
| 691 | description: 'A test tool', |
| 692 | tags: ['test'], |
| 693 | inputParameters: { |
| 694 | type: 'object', |
| 695 | properties: { |
| 696 | fileInput: { |
| 697 | anyOf: [ |
| 698 | { |
| 699 | type: 'string', |
| 700 | file_uploadable: true, |
| 701 | }, |
| 702 | { |
| 703 | type: 'null', |
| 704 | }, |
| 705 | ], |
| 706 | }, |
| 707 | }, |
| 708 | }, |
| 709 | version: '20251201_01', |
| 710 | availableVersions: ['20251201_01'], |
| 711 | }; |
| 712 | |
| 713 | const params = { |
| 714 | arguments: { |
| 715 | fileInput: '/path/to/file.txt', |
| 716 | }, |
| 717 | userId: 'test-user', |
| 718 | }; |
| 719 | |
| 720 | const result = await fileToolModifier.fileUploadModifier(toolWithAnyOf, { |
| 721 | toolSlug: 'test-tool', |
| 722 | toolkitSlug: 'test-toolkit', |
| 723 | params, |
| 724 | }); |
| 725 | |
| 726 | expect(fileUtils.getFileDataAfterUploadingToS3).toHaveBeenCalledWith('/path/to/file.txt', { |
| 727 | toolSlug: 'test-tool', |
| 728 | toolkitSlug: 'test-toolkit', |
| 729 | client: mockClient, |
| 730 | }); |
| 731 | expect(result.arguments?.fileInput).toEqual(mockFileData); |
| 732 | }); |
| 733 | |
| 734 | it('should upload file for file_uploadable inside oneOf schema', async () => { |
| 735 | const mockFileData = { |
| 736 | name: 'file.txt', |
| 737 | mimetype: 'text/plain', |
| 738 | s3key: 'uploads/file.txt', |
| 739 | }; |
| 740 | vi.mocked(fileUtils.getFileDataAfterUploadingToS3).mockResolvedValue(mockFileData); |
| 741 | |
| 742 | const toolWithOneOf: Tool = { |
| 743 | slug: 'test-tool', |
| 744 | name: 'Test Tool', |
| 745 | description: 'A test tool', |
| 746 | tags: ['test'], |
| 747 | inputParameters: { |
| 748 | type: 'object', |
| 749 | properties: { |
| 750 | fileInput: { |
| 751 | oneOf: [ |
| 752 | { |
| 753 | type: 'string', |
| 754 | file_uploadable: true, |
| 755 | }, |
| 756 | { |
| 757 | type: 'string', |
| 758 | description: 'URL', |
| 759 | }, |
| 760 | ], |
| 761 | }, |
| 762 | }, |
| 763 | }, |
| 764 | version: '20251201_01', |
| 765 | availableVersions: ['20251201_01'], |
| 766 | }; |
| 767 | |
| 768 | const params = { |
| 769 | arguments: { |
| 770 | fileInput: '/path/to/file.txt', |
| 771 | }, |
| 772 | userId: 'test-user', |
| 773 | }; |
| 774 | |
| 775 | const result = await fileToolModifier.fileUploadModifier(toolWithOneOf, { |
| 776 | toolSlug: 'test-tool', |
| 777 | toolkitSlug: 'test-toolkit', |
| 778 | params, |
| 779 | }); |
| 780 | |
| 781 | expect(fileUtils.getFileDataAfterUploadingToS3).toHaveBeenCalledWith('/path/to/file.txt', { |
| 782 | toolSlug: 'test-tool', |
| 783 | toolkitSlug: 'test-toolkit', |
| 784 | client: mockClient, |
| 785 | }); |
| 786 | expect(result.arguments?.fileInput).toEqual(mockFileData); |
| 787 | }); |
| 788 | |
| 789 | it('should upload file for file_uploadable inside allOf schema', async () => { |
| 790 | const mockFileData = { |
| 791 | name: 'file.txt', |
| 792 | mimetype: 'text/plain', |
| 793 | s3key: 'uploads/file.txt', |
| 794 | }; |
| 795 | vi.mocked(fileUtils.getFileDataAfterUploadingToS3).mockResolvedValue(mockFileData); |
| 796 | |
| 797 | const toolWithAllOf: Tool = { |
| 798 | slug: 'test-tool', |
| 799 | name: 'Test Tool', |
| 800 | description: 'A test tool', |
| 801 | tags: ['test'], |
| 802 | inputParameters: { |
| 803 | type: 'object', |
| 804 | properties: { |
| 805 | fileInput: { |
| 806 | allOf: [ |
| 807 | { |
| 808 | type: 'string', |
| 809 | file_uploadable: true, |
| 810 | }, |
| 811 | { |
| 812 | minLength: 1, |
| 813 | }, |
| 814 | ], |
| 815 | }, |
| 816 | }, |
| 817 | }, |
| 818 | version: '20251201_01', |
| 819 | availableVersions: ['20251201_01'], |
| 820 | }; |
| 821 | |
| 822 | const params = { |
| 823 | arguments: { |
| 824 | fileInput: '/path/to/file.txt', |
| 825 | }, |
| 826 | userId: 'test-user', |
| 827 | }; |
| 828 | |
| 829 | const result = await fileToolModifier.fileUploadModifier(toolWithAllOf, { |
| 830 | toolSlug: 'test-tool', |
| 831 | toolkitSlug: 'test-toolkit', |
| 832 | params, |
| 833 | }); |
| 834 | |
| 835 | expect(fileUtils.getFileDataAfterUploadingToS3).toHaveBeenCalledWith('/path/to/file.txt', { |
| 836 | toolSlug: 'test-tool', |
| 837 | toolkitSlug: 'test-toolkit', |
| 838 | client: mockClient, |
| 839 | }); |
| 840 | expect(result.arguments?.fileInput).toEqual(mockFileData); |
| 841 | }); |
| 842 | |
| 843 | it('should upload file for nested file_uploadable inside anyOf with objects', async () => { |
| 844 | const mockFileData = { |
| 845 | name: 'attachment.pdf', |
| 846 | mimetype: 'application/pdf', |
| 847 | s3key: 'uploads/attachment.pdf', |
| 848 | }; |
| 849 | vi.mocked(fileUtils.getFileDataAfterUploadingToS3).mockResolvedValue(mockFileData); |
| 850 | |
| 851 | const toolWithNestedAnyOf: Tool = { |
| 852 | slug: 'test-tool', |
| 853 | name: 'Test Tool', |
| 854 | description: 'A test tool', |
| 855 | tags: ['test'], |
| 856 | inputParameters: { |
| 857 | type: 'object', |
| 858 | properties: { |
| 859 | content: { |
| 860 | anyOf: [ |
| 861 | { |
| 862 | type: 'object', |
| 863 | properties: { |
| 864 | attachment: { |
| 865 | type: 'string', |
| 866 | file_uploadable: true, |
| 867 | }, |
| 868 | }, |
| 869 | }, |
| 870 | { |
| 871 | type: 'string', |
| 872 | }, |
| 873 | ], |
| 874 | }, |
| 875 | }, |
| 876 | }, |
| 877 | version: '20251201_01', |
| 878 | availableVersions: ['20251201_01'], |
| 879 | }; |
| 880 | |
| 881 | const params = { |
| 882 | arguments: { |
| 883 | content: { |
| 884 | attachment: '/path/to/attachment.pdf', |
| 885 | }, |
| 886 | }, |
| 887 | userId: 'test-user', |
| 888 | }; |
| 889 | |
| 890 | const result = await fileToolModifier.fileUploadModifier(toolWithNestedAnyOf, { |
| 891 | toolSlug: 'test-tool', |
| 892 | toolkitSlug: 'test-toolkit', |
| 893 | params, |
| 894 | }); |
| 895 | |
| 896 | expect(fileUtils.getFileDataAfterUploadingToS3).toHaveBeenCalledWith( |
| 897 | '/path/to/attachment.pdf', |
| 898 | { |
| 899 | toolSlug: 'test-tool', |
| 900 | toolkitSlug: 'test-toolkit', |
| 901 | client: mockClient, |
| 902 | } |
| 903 | ); |
| 904 | expect((result.arguments?.content as Record<string, unknown>)?.attachment).toEqual( |
| 905 | mockFileData |
| 906 | ); |
| 907 | }); |
| 908 | |
| 909 | it('uploads once when multiple oneOf variants are file_uploadable', async () => { |
| 910 | // Regression: previously hydrateFiles iterated over every uploadable |
| 911 | // variant in anyOf/oneOf/allOf and re-uploaded the same file once per |
| 912 | // variant. With `oneOf` semantics this is doubly wrong (only one |
| 913 | // variant matches at runtime). Match the Python SDK and short-circuit |
| 914 | // on the first uploadable variant. |
| 915 | const mockFileData = { |
| 916 | name: 'file.txt', |
| 917 | mimetype: 'text/plain', |
| 918 | s3key: 'uploads/file.txt', |
| 919 | }; |
| 920 | vi.mocked(fileUtils.getFileDataAfterUploadingToS3).mockResolvedValue(mockFileData); |
| 921 | |
| 922 | const toolWithTwoUploadableVariants: Tool = { |
| 923 | slug: 'test-tool', |
| 924 | name: 'Test Tool', |
| 925 | description: 'A test tool', |
| 926 | tags: ['test'], |
| 927 | inputParameters: { |
| 928 | type: 'object', |
| 929 | properties: { |
| 930 | fileInput: { |
| 931 | oneOf: [ |
| 932 | { type: 'string', file_uploadable: true }, |
| 933 | { type: 'string', file_uploadable: true }, |
| 934 | ], |
| 935 | }, |
| 936 | }, |
| 937 | }, |
| 938 | version: '20251201_01', |
| 939 | availableVersions: ['20251201_01'], |
| 940 | }; |
| 941 | |
| 942 | const params = { |
| 943 | arguments: { fileInput: '/path/to/file.txt' }, |
| 944 | userId: 'test-user', |
| 945 | }; |
| 946 | |
| 947 | const result = await fileToolModifier.fileUploadModifier(toolWithTwoUploadableVariants, { |
| 948 | toolSlug: 'test-tool', |
| 949 | toolkitSlug: 'test-toolkit', |
| 950 | params, |
| 951 | }); |
| 952 | |
| 953 | expect(fileUtils.getFileDataAfterUploadingToS3).toHaveBeenCalledTimes(1); |
| 954 | expect(result.arguments?.fileInput).toEqual(mockFileData); |
| 955 | }); |
| 956 | |
| 957 | it('should upload each file when anyOf accepts a single file or an array of files', async () => { |
| 958 | const mockFileData1 = { |
| 959 | name: 'file1.txt', |
| 960 | mimetype: 'text/plain', |
| 961 | s3key: 'uploads/file1.txt', |
| 962 | }; |
| 963 | const mockFileData2 = { |
| 964 | name: 'file2.txt', |
| 965 | mimetype: 'text/plain', |
| 966 | s3key: 'uploads/file2.txt', |
| 967 | }; |
| 968 | vi.mocked(fileUtils.getFileDataAfterUploadingToS3) |
| 969 | .mockResolvedValueOnce(mockFileData1) |
| 970 | .mockResolvedValueOnce(mockFileData2); |
| 971 | |
| 972 | const toolWithSingleOrArrayFile: Tool = { |
| 973 | slug: 'test-tool', |
| 974 | name: 'Test Tool', |
| 975 | description: 'A test tool', |
| 976 | tags: ['test'], |
| 977 | inputParameters: { |
| 978 | type: 'object', |
| 979 | properties: { |
| 980 | attachment: { |
| 981 | anyOf: [ |
| 982 | { type: 'string', file_uploadable: true }, |
| 983 | { |
| 984 | type: 'array', |
| 985 | items: { type: 'string', file_uploadable: true }, |
| 986 | }, |
| 987 | ], |
| 988 | }, |
| 989 | }, |
| 990 | }, |
| 991 | version: '20251201_01', |
| 992 | availableVersions: ['20251201_01'], |
| 993 | }; |
| 994 | |
| 995 | const result = await fileToolModifier.fileUploadModifier(toolWithSingleOrArrayFile, { |
| 996 | toolSlug: 'test-tool', |
| 997 | toolkitSlug: 'test-toolkit', |
| 998 | params: { |
| 999 | arguments: { |
| 1000 | attachment: ['/path/to/file1.txt', '/path/to/file2.txt'], |
| 1001 | }, |
| 1002 | userId: 'test-user', |
| 1003 | }, |
| 1004 | }); |
| 1005 | |
| 1006 | expect(fileUtils.getFileDataAfterUploadingToS3).toHaveBeenCalledTimes(2); |
| 1007 | expect(fileUtils.getFileDataAfterUploadingToS3).toHaveBeenNthCalledWith( |
| 1008 | 1, |
| 1009 | '/path/to/file1.txt', |
| 1010 | { |
| 1011 | toolSlug: 'test-tool', |
| 1012 | toolkitSlug: 'test-toolkit', |
| 1013 | client: mockClient, |
| 1014 | } |
| 1015 | ); |
| 1016 | expect(fileUtils.getFileDataAfterUploadingToS3).toHaveBeenNthCalledWith( |
| 1017 | 2, |
| 1018 | '/path/to/file2.txt', |
| 1019 | { |
| 1020 | toolSlug: 'test-tool', |
| 1021 | toolkitSlug: 'test-toolkit', |
| 1022 | client: mockClient, |
| 1023 | } |
| 1024 | ); |
| 1025 | expect(result.arguments?.attachment).toEqual([mockFileData1, mockFileData2]); |
| 1026 | }); |
| 1027 | |
| 1028 | it('should keep single-file behavior when anyOf also accepts an array of files', async () => { |
| 1029 | const mockFileData = { |
| 1030 | name: 'file.txt', |
| 1031 | mimetype: 'text/plain', |
| 1032 | s3key: 'uploads/file.txt', |
| 1033 | }; |
| 1034 | vi.mocked(fileUtils.getFileDataAfterUploadingToS3).mockResolvedValue(mockFileData); |
| 1035 | |
| 1036 | const toolWithSingleOrArrayFile: Tool = { |
| 1037 | slug: 'test-tool', |
| 1038 | name: 'Test Tool', |
| 1039 | description: 'A test tool', |
| 1040 | tags: ['test'], |
| 1041 | inputParameters: { |
| 1042 | type: 'object', |
| 1043 | properties: { |
| 1044 | attachment: { |
| 1045 | anyOf: [ |
| 1046 | { type: 'string', file_uploadable: true }, |
| 1047 | { |
| 1048 | type: 'array', |
| 1049 | items: { type: 'string', file_uploadable: true }, |
| 1050 | }, |
| 1051 | ], |
| 1052 | }, |
| 1053 | }, |
| 1054 | }, |
| 1055 | version: '20251201_01', |
| 1056 | availableVersions: ['20251201_01'], |
| 1057 | }; |
| 1058 | |
| 1059 | const result = await fileToolModifier.fileUploadModifier(toolWithSingleOrArrayFile, { |
| 1060 | toolSlug: 'test-tool', |
| 1061 | toolkitSlug: 'test-toolkit', |
| 1062 | params: { |
| 1063 | arguments: { |
| 1064 | attachment: '/path/to/file.txt', |
| 1065 | }, |
| 1066 | userId: 'test-user', |
| 1067 | }, |
| 1068 | }); |
| 1069 | |
| 1070 | expect(fileUtils.getFileDataAfterUploadingToS3).toHaveBeenCalledTimes(1); |
| 1071 | expect(fileUtils.getFileDataAfterUploadingToS3).toHaveBeenCalledWith('/path/to/file.txt', { |
| 1072 | toolSlug: 'test-tool', |
| 1073 | toolkitSlug: 'test-toolkit', |
| 1074 | client: mockClient, |
| 1075 | }); |
| 1076 | expect(result.arguments?.attachment).toEqual(mockFileData); |
| 1077 | }); |
| 1078 | |
| 1079 | it('should upload file array items whose item schema is an anyOf file branch', async () => { |
| 1080 | const mockFileData1 = { |
| 1081 | name: 'file1.txt', |
| 1082 | mimetype: 'text/plain', |
| 1083 | s3key: 'uploads/file1.txt', |
| 1084 | }; |
| 1085 | const mockFileData2 = { |
| 1086 | name: 'file2.txt', |
| 1087 | mimetype: 'text/plain', |
| 1088 | s3key: 'uploads/file2.txt', |
| 1089 | }; |
| 1090 | vi.mocked(fileUtils.getFileDataAfterUploadingToS3) |
| 1091 | .mockResolvedValueOnce(mockFileData1) |
| 1092 | .mockResolvedValueOnce(mockFileData2); |
| 1093 | |
| 1094 | const toolWithArrayItemAnyOf: Tool = { |
| 1095 | slug: 'test-tool', |
| 1096 | name: 'Test Tool', |
| 1097 | description: 'A test tool', |
| 1098 | tags: ['test'], |
| 1099 | inputParameters: { |
| 1100 | type: 'object', |
| 1101 | properties: { |
| 1102 | attachments: { |
| 1103 | type: 'array', |
| 1104 | items: { |
| 1105 | anyOf: [{ type: 'string', file_uploadable: true }, { type: 'null' }], |
| 1106 | }, |
| 1107 | }, |
| 1108 | }, |
| 1109 | }, |
| 1110 | version: '20251201_01', |
| 1111 | availableVersions: ['20251201_01'], |
| 1112 | }; |
| 1113 | |
| 1114 | const result = await fileToolModifier.fileUploadModifier(toolWithArrayItemAnyOf, { |
| 1115 | toolSlug: 'test-tool', |
| 1116 | toolkitSlug: 'test-toolkit', |
| 1117 | params: { |
| 1118 | arguments: { |
| 1119 | attachments: ['/path/to/file1.txt', null, '/path/to/file2.txt'], |
| 1120 | }, |
| 1121 | userId: 'test-user', |
| 1122 | }, |
| 1123 | }); |
| 1124 | |
| 1125 | expect(fileUtils.getFileDataAfterUploadingToS3).toHaveBeenCalledTimes(2); |
| 1126 | expect(result.arguments?.attachments).toEqual([mockFileData1, null, mockFileData2]); |
| 1127 | }); |
| 1128 | |
| 1129 | it('should not upload when value is null for anyOf with null variant', async () => { |
| 1130 | const toolWithAnyOf: Tool = { |
| 1131 | slug: 'test-tool', |
| 1132 | name: 'Test Tool', |
| 1133 | description: 'A test tool', |
| 1134 | tags: ['test'], |
| 1135 | inputParameters: { |
| 1136 | type: 'object', |
| 1137 | properties: { |
| 1138 | fileInput: { |
| 1139 | anyOf: [ |
| 1140 | { |
| 1141 | type: 'string', |
| 1142 | file_uploadable: true, |
| 1143 | }, |
| 1144 | { |
| 1145 | type: 'null', |
| 1146 | }, |
| 1147 | ], |
| 1148 | }, |
| 1149 | }, |
| 1150 | }, |
| 1151 | version: '20251201_01', |
| 1152 | availableVersions: ['20251201_01'], |
| 1153 | }; |
| 1154 | |
| 1155 | const params = { |
| 1156 | arguments: { |
| 1157 | fileInput: null, |
| 1158 | }, |
| 1159 | userId: 'test-user', |
| 1160 | }; |
| 1161 | |
| 1162 | const result = await fileToolModifier.fileUploadModifier(toolWithAnyOf, { |
| 1163 | toolSlug: 'test-tool', |
| 1164 | toolkitSlug: 'test-toolkit', |
| 1165 | params, |
| 1166 | }); |
| 1167 | |
| 1168 | expect(fileUtils.getFileDataAfterUploadingToS3).not.toHaveBeenCalled(); |
| 1169 | expect(result.arguments?.fileInput).toBeNull(); |
| 1170 | }); |
| 1171 | |
| 1172 | it('should upload file from base properties when anyOf has no file_uploadable', async () => { |
| 1173 | const mockFileData = { |
| 1174 | name: 'file.txt', |
| 1175 | mimetype: 'text/plain', |
| 1176 | s3key: 'uploads/file.txt', |
| 1177 | }; |
| 1178 | vi.mocked(fileUtils.getFileDataAfterUploadingToS3).mockResolvedValue(mockFileData); |
| 1179 | |
| 1180 | // Schema with anyOf at property level (no file_uploadable) but file_uploadable in sibling property |
| 1181 | const toolWithMixedSchema: Tool = { |
| 1182 | slug: 'test-tool', |
| 1183 | name: 'Test Tool', |
| 1184 | description: 'A test tool', |
| 1185 | tags: ['test'], |
| 1186 | inputParameters: { |
| 1187 | type: 'object', |
| 1188 | properties: { |
| 1189 | metadata: { |
| 1190 | anyOf: [{ type: 'string' }, { type: 'null' }], |
| 1191 | }, |
| 1192 | file: { |
| 1193 | type: 'string', |
| 1194 | file_uploadable: true, |
| 1195 | }, |
| 1196 | }, |
| 1197 | }, |
| 1198 | version: '20251201_01', |
| 1199 | availableVersions: ['20251201_01'], |
| 1200 | }; |
| 1201 | |
| 1202 | const params = { |
| 1203 | arguments: { |
| 1204 | metadata: 'some metadata', |
| 1205 | file: '/path/to/file.txt', |
| 1206 | }, |
| 1207 | userId: 'test-user', |
| 1208 | }; |
| 1209 | |
| 1210 | const result = await fileToolModifier.fileUploadModifier(toolWithMixedSchema, { |
| 1211 | toolSlug: 'test-tool', |
| 1212 | toolkitSlug: 'test-toolkit', |
| 1213 | params, |
| 1214 | }); |
| 1215 | |
| 1216 | expect(fileUtils.getFileDataAfterUploadingToS3).toHaveBeenCalledWith('/path/to/file.txt', { |
| 1217 | toolSlug: 'test-tool', |
| 1218 | toolkitSlug: 'test-toolkit', |
| 1219 | client: mockClient, |
| 1220 | }); |
| 1221 | expect(result.arguments?.file).toEqual(mockFileData); |
| 1222 | expect(result.arguments?.metadata).toBe('some metadata'); |
| 1223 | }); |
| 1224 | |
| 1225 | it('should upload file when schema has anyOf without file_uploadable alongside properties with file_uploadable', async () => { |
| 1226 | const mockFileData = { |
| 1227 | name: 'document.pdf', |
| 1228 | mimetype: 'application/pdf', |
| 1229 | s3key: 'uploads/document.pdf', |
| 1230 | }; |
| 1231 | vi.mocked(fileUtils.getFileDataAfterUploadingToS3).mockResolvedValue(mockFileData); |
| 1232 | |
| 1233 | // Schema where the root has both anyOf (without file_uploadable) and properties (with file_uploadable) |
| 1234 | const toolWithAnyOfAndProperties: Tool = { |
| 1235 | slug: 'test-tool', |
| 1236 | name: 'Test Tool', |
| 1237 | description: 'A test tool', |
| 1238 | tags: ['test'], |
| 1239 | inputParameters: { |
| 1240 | type: 'object', |
| 1241 | anyOf: [{ required: ['text'] }, { required: ['file'] }], |
| 1242 | properties: { |
| 1243 | text: { |
| 1244 | type: 'string', |
| 1245 | }, |
| 1246 | file: { |
| 1247 | type: 'string', |
| 1248 | file_uploadable: true, |
| 1249 | }, |
| 1250 | }, |
| 1251 | }, |
| 1252 | version: '20251201_01', |
| 1253 | availableVersions: ['20251201_01'], |
| 1254 | }; |
| 1255 | |
| 1256 | const params = { |
| 1257 | arguments: { |
| 1258 | file: '/path/to/document.pdf', |
| 1259 | }, |
| 1260 | userId: 'test-user', |
| 1261 | }; |
| 1262 | |
| 1263 | const result = await fileToolModifier.fileUploadModifier(toolWithAnyOfAndProperties, { |
| 1264 | toolSlug: 'test-tool', |
| 1265 | toolkitSlug: 'test-toolkit', |
| 1266 | params, |
| 1267 | }); |
| 1268 | |
| 1269 | expect(fileUtils.getFileDataAfterUploadingToS3).toHaveBeenCalledWith( |
| 1270 | '/path/to/document.pdf', |
| 1271 | { |
| 1272 | toolSlug: 'test-tool', |
| 1273 | toolkitSlug: 'test-toolkit', |
| 1274 | client: mockClient, |
| 1275 | } |
| 1276 | ); |
| 1277 | expect(result.arguments?.file).toEqual(mockFileData); |
| 1278 | }); |
| 1279 | }); |
| 1280 | |
| 1281 | describe('fileDownloadModifier', () => { |
| 1282 | const mockTool: Tool = { |
| 1283 | slug: 'test-tool', |
| 1284 | name: 'Test Tool', |
| 1285 | description: 'A test tool', |
| 1286 | tags: ['test'], |
| 1287 | availableVersions: ['20251201_01'], |
| 1288 | version: '20251201_01', |
| 1289 | }; |
| 1290 | |
| 1291 | it('should download file from S3 URL', async () => { |
| 1292 | const mockDownloadResult = { |
| 1293 | name: 'file.txt', |
| 1294 | mimeType: 'text/plain', |
| 1295 | s3Url: 'downloads/file.txt', |
| 1296 | filePath: '/downloaded/file.txt', |
| 1297 | }; |
| 1298 | vi.mocked(fileUtils.downloadFileFromS3).mockResolvedValue(mockDownloadResult); |
| 1299 | |
| 1300 | const result = { |
| 1301 | data: { |
| 1302 | file: { |
| 1303 | s3url: 'https://s3.example.com/file.txt', |
| 1304 | mimetype: 'text/plain', |
| 1305 | }, |
| 1306 | }, |
| 1307 | error: null, |
| 1308 | successful: true, |
| 1309 | }; |
| 1310 | |
| 1311 | const modifiedResult = await fileToolModifier.fileDownloadModifier(mockTool, { |
| 1312 | toolSlug: 'test-tool', |
| 1313 | toolkitSlug: 'test-toolkit', |
| 1314 | result, |
| 1315 | }); |
| 1316 | |
| 1317 | expect(fileUtils.downloadFileFromS3).toHaveBeenCalledWith({ |
| 1318 | toolSlug: 'test-tool', |
| 1319 | s3Url: 'https://s3.example.com/file.txt', |
| 1320 | mimeType: 'text/plain', |
| 1321 | }); |
| 1322 | |
| 1323 | expect(modifiedResult.data.file).toEqual({ |
| 1324 | uri: '/downloaded/file.txt', |
| 1325 | file_downloaded: true, |
| 1326 | s3url: 'https://s3.example.com/file.txt', |
| 1327 | mimeType: 'text/plain', |
| 1328 | }); |
| 1329 | }); |
| 1330 | |
| 1331 | it('should handle download failure gracefully', async () => { |
| 1332 | vi.mocked(fileUtils.downloadFileFromS3).mockRejectedValue(new Error('Download failed')); |
| 1333 | |
| 1334 | const result = { |
| 1335 | data: { |
| 1336 | file: { |
| 1337 | s3url: 'https://s3.example.com/file.txt', |
| 1338 | mimetype: 'text/plain', |
| 1339 | }, |
| 1340 | }, |
| 1341 | error: null, |
| 1342 | successful: true, |
| 1343 | }; |
| 1344 | |
| 1345 | const modifiedResult = await fileToolModifier.fileDownloadModifier(mockTool, { |
| 1346 | toolSlug: 'test-tool', |
| 1347 | toolkitSlug: 'test-toolkit', |
| 1348 | result, |
| 1349 | }); |
| 1350 | |
| 1351 | expect(modifiedResult.data.file).toEqual({ |
| 1352 | uri: '', |
| 1353 | file_downloaded: false, |
| 1354 | s3url: 'https://s3.example.com/file.txt', |
| 1355 | mimeType: 'text/plain', |
| 1356 | }); |
| 1357 | }); |
| 1358 | |
| 1359 | it('should skip fields without s3url', async () => { |
| 1360 | const result = { |
| 1361 | data: { |
| 1362 | text: 'some text', |
| 1363 | file: { |
| 1364 | other_field: 'value', |
| 1365 | }, |
| 1366 | }, |
| 1367 | error: null, |
| 1368 | successful: true, |
| 1369 | }; |
| 1370 | |
| 1371 | const modifiedResult = await fileToolModifier.fileDownloadModifier(mockTool, { |
| 1372 | toolSlug: 'test-tool', |
| 1373 | toolkitSlug: 'test-toolkit', |
| 1374 | result, |
| 1375 | }); |
| 1376 | |
| 1377 | expect(fileUtils.downloadFileFromS3).not.toHaveBeenCalled(); |
| 1378 | expect(modifiedResult).toEqual(result); |
| 1379 | }); |
| 1380 | |
| 1381 | it('should download file with file_downloadable inside anyOf schema', async () => { |
| 1382 | const mockDownloadResult = { |
| 1383 | name: 'file.txt', |
| 1384 | mimeType: 'text/plain', |
| 1385 | s3Url: 'downloads/file.txt', |
| 1386 | filePath: '/downloaded/file.txt', |
| 1387 | }; |
| 1388 | vi.mocked(fileUtils.downloadFileFromS3).mockResolvedValue(mockDownloadResult); |
| 1389 | |
| 1390 | const toolWithAnyOf: Tool = { |
| 1391 | slug: 'test-tool', |
| 1392 | name: 'Test Tool', |
| 1393 | description: 'A test tool', |
| 1394 | tags: ['test'], |
| 1395 | outputParameters: { |
| 1396 | type: 'object', |
| 1397 | properties: { |
| 1398 | fileOutput: { |
| 1399 | anyOf: [ |
| 1400 | { |
| 1401 | type: 'object', |
| 1402 | file_downloadable: true, |
| 1403 | properties: { |
| 1404 | s3url: { type: 'string' }, |
| 1405 | mimetype: { type: 'string' }, |
| 1406 | }, |
| 1407 | }, |
| 1408 | { |
| 1409 | type: 'null', |
| 1410 | }, |
| 1411 | ], |
| 1412 | }, |
| 1413 | }, |
| 1414 | }, |
| 1415 | version: '20251201_01', |
| 1416 | availableVersions: ['20251201_01'], |
| 1417 | }; |
| 1418 | |
| 1419 | const result = { |
| 1420 | data: { |
| 1421 | fileOutput: { |
| 1422 | s3url: 'https://s3.example.com/file.txt', |
| 1423 | mimetype: 'text/plain', |
| 1424 | }, |
| 1425 | }, |
| 1426 | error: null, |
| 1427 | successful: true, |
| 1428 | }; |
| 1429 | |
| 1430 | const modifiedResult = await fileToolModifier.fileDownloadModifier(toolWithAnyOf, { |
| 1431 | toolSlug: 'test-tool', |
| 1432 | toolkitSlug: 'test-toolkit', |
| 1433 | result, |
| 1434 | }); |
| 1435 | |
| 1436 | expect(fileUtils.downloadFileFromS3).toHaveBeenCalledWith({ |
| 1437 | toolSlug: 'test-tool', |
| 1438 | s3Url: 'https://s3.example.com/file.txt', |
| 1439 | mimeType: 'text/plain', |
| 1440 | }); |
| 1441 | |
| 1442 | expect(modifiedResult.data.fileOutput).toEqual({ |
| 1443 | uri: '/downloaded/file.txt', |
| 1444 | file_downloaded: true, |
| 1445 | s3url: 'https://s3.example.com/file.txt', |
| 1446 | mimeType: 'text/plain', |
| 1447 | }); |
| 1448 | }); |
| 1449 | |
| 1450 | it('should download file with file_downloadable inside oneOf schema', async () => { |
| 1451 | const mockDownloadResult = { |
| 1452 | name: 'document.pdf', |
| 1453 | mimeType: 'application/pdf', |
| 1454 | s3Url: 'downloads/document.pdf', |
| 1455 | filePath: '/downloaded/document.pdf', |
| 1456 | }; |
| 1457 | vi.mocked(fileUtils.downloadFileFromS3).mockResolvedValue(mockDownloadResult); |
| 1458 | |
| 1459 | const toolWithOneOf: Tool = { |
| 1460 | slug: 'test-tool', |
| 1461 | name: 'Test Tool', |
| 1462 | description: 'A test tool', |
| 1463 | tags: ['test'], |
| 1464 | outputParameters: { |
| 1465 | type: 'object', |
| 1466 | properties: { |
| 1467 | result: { |
| 1468 | oneOf: [ |
| 1469 | { |
| 1470 | type: 'object', |
| 1471 | file_downloadable: true, |
| 1472 | properties: { |
| 1473 | s3url: { type: 'string' }, |
| 1474 | mimetype: { type: 'string' }, |
| 1475 | }, |
| 1476 | }, |
| 1477 | { |
| 1478 | type: 'string', |
| 1479 | }, |
| 1480 | ], |
| 1481 | }, |
| 1482 | }, |
| 1483 | }, |
| 1484 | version: '20251201_01', |
| 1485 | availableVersions: ['20251201_01'], |
| 1486 | }; |
| 1487 | |
| 1488 | const result = { |
| 1489 | data: { |
| 1490 | result: { |
| 1491 | s3url: 'https://s3.example.com/document.pdf', |
| 1492 | mimetype: 'application/pdf', |
| 1493 | }, |
| 1494 | }, |
| 1495 | error: null, |
| 1496 | successful: true, |
| 1497 | }; |
| 1498 | |
| 1499 | const modifiedResult = await fileToolModifier.fileDownloadModifier(toolWithOneOf, { |
| 1500 | toolSlug: 'test-tool', |
| 1501 | toolkitSlug: 'test-toolkit', |
| 1502 | result, |
| 1503 | }); |
| 1504 | |
| 1505 | expect(fileUtils.downloadFileFromS3).toHaveBeenCalledWith({ |
| 1506 | toolSlug: 'test-tool', |
| 1507 | s3Url: 'https://s3.example.com/document.pdf', |
| 1508 | mimeType: 'application/pdf', |
| 1509 | }); |
| 1510 | |
| 1511 | expect(modifiedResult.data.result).toEqual({ |
| 1512 | uri: '/downloaded/document.pdf', |
| 1513 | file_downloaded: true, |
| 1514 | s3url: 'https://s3.example.com/document.pdf', |
| 1515 | mimeType: 'application/pdf', |
| 1516 | }); |
| 1517 | }); |
| 1518 | |
| 1519 | it('should download file with file_downloadable inside allOf schema', async () => { |
| 1520 | const mockDownloadResult = { |
| 1521 | name: 'image.png', |
| 1522 | mimeType: 'image/png', |
| 1523 | s3Url: 'downloads/image.png', |
| 1524 | filePath: '/downloaded/image.png', |
| 1525 | }; |
| 1526 | vi.mocked(fileUtils.downloadFileFromS3).mockResolvedValue(mockDownloadResult); |
| 1527 | |
| 1528 | const toolWithAllOf: Tool = { |
| 1529 | slug: 'test-tool', |
| 1530 | name: 'Test Tool', |
| 1531 | description: 'A test tool', |
| 1532 | tags: ['test'], |
| 1533 | outputParameters: { |
| 1534 | type: 'object', |
| 1535 | properties: { |
| 1536 | image: { |
| 1537 | allOf: [ |
| 1538 | { |
| 1539 | type: 'object', |
| 1540 | file_downloadable: true, |
| 1541 | properties: { |
| 1542 | s3url: { type: 'string' }, |
| 1543 | mimetype: { type: 'string' }, |
| 1544 | }, |
| 1545 | }, |
| 1546 | { |
| 1547 | required: ['s3url'], |
| 1548 | }, |
| 1549 | ], |
| 1550 | }, |
| 1551 | }, |
| 1552 | }, |
| 1553 | version: '20251201_01', |
| 1554 | availableVersions: ['20251201_01'], |
| 1555 | }; |
| 1556 | |
| 1557 | const result = { |
| 1558 | data: { |
| 1559 | image: { |
| 1560 | s3url: 'https://s3.example.com/image.png', |
| 1561 | mimetype: 'image/png', |
| 1562 | }, |
| 1563 | }, |
| 1564 | error: null, |
| 1565 | successful: true, |
| 1566 | }; |
| 1567 | |
| 1568 | const modifiedResult = await fileToolModifier.fileDownloadModifier(toolWithAllOf, { |
| 1569 | toolSlug: 'test-tool', |
| 1570 | toolkitSlug: 'test-toolkit', |
| 1571 | result, |
| 1572 | }); |
| 1573 | |
| 1574 | expect(fileUtils.downloadFileFromS3).toHaveBeenCalledWith({ |
| 1575 | toolSlug: 'test-tool', |
| 1576 | s3Url: 'https://s3.example.com/image.png', |
| 1577 | mimeType: 'image/png', |
| 1578 | }); |
| 1579 | |
| 1580 | expect(modifiedResult.data.image).toEqual({ |
| 1581 | uri: '/downloaded/image.png', |
| 1582 | file_downloaded: true, |
| 1583 | s3url: 'https://s3.example.com/image.png', |
| 1584 | mimeType: 'image/png', |
| 1585 | }); |
| 1586 | }); |
| 1587 | |
| 1588 | it('should download nested file with file_downloadable inside anyOf with objects', async () => { |
| 1589 | const mockDownloadResult = { |
| 1590 | name: 'attachment.docx', |
| 1591 | mimeType: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', |
| 1592 | s3Url: 'downloads/attachment.docx', |
| 1593 | filePath: '/downloaded/attachment.docx', |
| 1594 | }; |
| 1595 | vi.mocked(fileUtils.downloadFileFromS3).mockResolvedValue(mockDownloadResult); |
| 1596 | |
| 1597 | const toolWithNestedAnyOf: Tool = { |
| 1598 | slug: 'test-tool', |
| 1599 | name: 'Test Tool', |
| 1600 | description: 'A test tool', |
| 1601 | tags: ['test'], |
| 1602 | outputParameters: { |
| 1603 | type: 'object', |
| 1604 | properties: { |
| 1605 | response: { |
| 1606 | anyOf: [ |
| 1607 | { |
| 1608 | type: 'object', |
| 1609 | properties: { |
| 1610 | attachment: { |
| 1611 | type: 'object', |
| 1612 | file_downloadable: true, |
| 1613 | properties: { |
| 1614 | s3url: { type: 'string' }, |
| 1615 | mimetype: { type: 'string' }, |
| 1616 | }, |
| 1617 | }, |
| 1618 | }, |
| 1619 | }, |
| 1620 | { |
| 1621 | type: 'string', |
| 1622 | }, |
| 1623 | ], |
| 1624 | }, |
| 1625 | }, |
| 1626 | }, |
| 1627 | version: '20251201_01', |
| 1628 | availableVersions: ['20251201_01'], |
| 1629 | }; |
| 1630 | |
| 1631 | const result = { |
| 1632 | data: { |
| 1633 | response: { |
| 1634 | attachment: { |
| 1635 | s3url: 'https://s3.example.com/attachment.docx', |
| 1636 | mimetype: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', |
| 1637 | }, |
| 1638 | }, |
| 1639 | }, |
| 1640 | error: null, |
| 1641 | successful: true, |
| 1642 | }; |
| 1643 | |
| 1644 | const modifiedResult = await fileToolModifier.fileDownloadModifier(toolWithNestedAnyOf, { |
| 1645 | toolSlug: 'test-tool', |
| 1646 | toolkitSlug: 'test-toolkit', |
| 1647 | result, |
| 1648 | }); |
| 1649 | |
| 1650 | expect(fileUtils.downloadFileFromS3).toHaveBeenCalledWith({ |
| 1651 | toolSlug: 'test-tool', |
| 1652 | s3Url: 'https://s3.example.com/attachment.docx', |
| 1653 | mimeType: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', |
| 1654 | }); |
| 1655 | |
| 1656 | expect((modifiedResult.data.response as Record<string, unknown>)?.attachment).toEqual({ |
| 1657 | uri: '/downloaded/attachment.docx', |
| 1658 | file_downloaded: true, |
| 1659 | s3url: 'https://s3.example.com/attachment.docx', |
| 1660 | mimeType: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', |
| 1661 | }); |
| 1662 | }); |
| 1663 | |
| 1664 | it('should download files in array with anyOf schema', async () => { |
| 1665 | const mockDownloadResult = { |
| 1666 | name: 'file.txt', |
| 1667 | mimeType: 'text/plain', |
| 1668 | s3Url: 'downloads/file.txt', |
| 1669 | filePath: '/downloaded/file.txt', |
| 1670 | }; |
| 1671 | vi.mocked(fileUtils.downloadFileFromS3).mockResolvedValue(mockDownloadResult); |
| 1672 | |
| 1673 | const toolWithArrayAnyOf: Tool = { |
| 1674 | slug: 'test-tool', |
| 1675 | name: 'Test Tool', |
| 1676 | description: 'A test tool', |
| 1677 | tags: ['test'], |
| 1678 | outputParameters: { |
| 1679 | type: 'object', |
| 1680 | properties: { |
| 1681 | files: { |
| 1682 | type: 'array', |
| 1683 | items: { |
| 1684 | anyOf: [ |
| 1685 | { |
| 1686 | type: 'object', |
| 1687 | file_downloadable: true, |
| 1688 | properties: { |
| 1689 | s3url: { type: 'string' }, |
| 1690 | mimetype: { type: 'string' }, |
| 1691 | }, |
| 1692 | }, |
| 1693 | { |
| 1694 | type: 'null', |
| 1695 | }, |
| 1696 | ], |
| 1697 | }, |
| 1698 | }, |
| 1699 | }, |
| 1700 | }, |
| 1701 | version: '20251201_01', |
| 1702 | availableVersions: ['20251201_01'], |
| 1703 | }; |
| 1704 | |
| 1705 | const result = { |
| 1706 | data: { |
| 1707 | files: [ |
| 1708 | { |
| 1709 | s3url: 'https://s3.example.com/file1.txt', |
| 1710 | mimetype: 'text/plain', |
| 1711 | }, |
| 1712 | { |
| 1713 | s3url: 'https://s3.example.com/file2.txt', |
| 1714 | mimetype: 'text/plain', |
| 1715 | }, |
| 1716 | ], |
| 1717 | }, |
| 1718 | error: null, |
| 1719 | successful: true, |
| 1720 | }; |
| 1721 | |
| 1722 | const modifiedResult = await fileToolModifier.fileDownloadModifier(toolWithArrayAnyOf, { |
| 1723 | toolSlug: 'test-tool', |
| 1724 | toolkitSlug: 'test-toolkit', |
| 1725 | result, |
| 1726 | }); |
| 1727 | |
| 1728 | expect(fileUtils.downloadFileFromS3).toHaveBeenCalledTimes(2); |
| 1729 | expect((modifiedResult.data.files as unknown[])?.[0]).toEqual({ |
| 1730 | uri: '/downloaded/file.txt', |
| 1731 | file_downloaded: true, |
| 1732 | s3url: 'https://s3.example.com/file1.txt', |
| 1733 | mimeType: 'text/plain', |
| 1734 | }); |
| 1735 | }); |
| 1736 | |
| 1737 | it('should download each file when anyOf accepts a single file or an array of files', async () => { |
| 1738 | const mockDownloadResult1 = { |
| 1739 | name: 'file1.txt', |
| 1740 | mimeType: 'text/plain', |
| 1741 | s3Url: 'downloads/file1.txt', |
| 1742 | filePath: '/downloaded/file1.txt', |
| 1743 | }; |
| 1744 | const mockDownloadResult2 = { |
| 1745 | name: 'file2.txt', |
| 1746 | mimeType: 'text/plain', |
| 1747 | s3Url: 'downloads/file2.txt', |
| 1748 | filePath: '/downloaded/file2.txt', |
| 1749 | }; |
| 1750 | vi.mocked(fileUtils.downloadFileFromS3) |
| 1751 | .mockResolvedValueOnce(mockDownloadResult1) |
| 1752 | .mockResolvedValueOnce(mockDownloadResult2); |
| 1753 | |
| 1754 | const toolWithSingleOrArrayFile: Tool = { |
| 1755 | slug: 'test-tool', |
| 1756 | name: 'Test Tool', |
| 1757 | description: 'A test tool', |
| 1758 | tags: ['test'], |
| 1759 | outputParameters: { |
| 1760 | type: 'object', |
| 1761 | properties: { |
| 1762 | attachment: { |
| 1763 | anyOf: [ |
| 1764 | { |
| 1765 | type: 'object', |
| 1766 | file_downloadable: true, |
| 1767 | properties: { |
| 1768 | s3url: { type: 'string' }, |
| 1769 | mimetype: { type: 'string' }, |
| 1770 | }, |
| 1771 | }, |
| 1772 | { |
| 1773 | type: 'array', |
| 1774 | items: { |
| 1775 | type: 'object', |
| 1776 | file_downloadable: true, |
| 1777 | properties: { |
| 1778 | s3url: { type: 'string' }, |
| 1779 | mimetype: { type: 'string' }, |
| 1780 | }, |
| 1781 | }, |
| 1782 | }, |
| 1783 | ], |
| 1784 | }, |
| 1785 | }, |
| 1786 | }, |
| 1787 | version: '20251201_01', |
| 1788 | availableVersions: ['20251201_01'], |
| 1789 | }; |
| 1790 | |
| 1791 | const modifiedResult = await fileToolModifier.fileDownloadModifier( |
| 1792 | toolWithSingleOrArrayFile, |
| 1793 | { |
| 1794 | toolSlug: 'test-tool', |
| 1795 | toolkitSlug: 'test-toolkit', |
| 1796 | result: { |
| 1797 | data: { |
| 1798 | attachment: [ |
| 1799 | { |
| 1800 | s3url: 'https://s3.example.com/file1.txt', |
| 1801 | mimetype: 'text/plain', |
| 1802 | }, |
| 1803 | { |
| 1804 | s3url: 'https://s3.example.com/file2.txt', |
| 1805 | mimetype: 'text/plain', |
| 1806 | }, |
| 1807 | ], |
| 1808 | }, |
| 1809 | error: null, |
| 1810 | successful: true, |
| 1811 | }, |
| 1812 | } |
| 1813 | ); |
| 1814 | |
| 1815 | expect(fileUtils.downloadFileFromS3).toHaveBeenCalledTimes(2); |
| 1816 | expect(modifiedResult.data.attachment).toEqual([ |
| 1817 | { |
| 1818 | uri: '/downloaded/file1.txt', |
| 1819 | file_downloaded: true, |
| 1820 | s3url: 'https://s3.example.com/file1.txt', |
| 1821 | mimeType: 'text/plain', |
| 1822 | }, |
| 1823 | { |
| 1824 | uri: '/downloaded/file2.txt', |
| 1825 | file_downloaded: true, |
| 1826 | s3url: 'https://s3.example.com/file2.txt', |
| 1827 | mimeType: 'text/plain', |
| 1828 | }, |
| 1829 | ]); |
| 1830 | }); |
| 1831 | |
| 1832 | it('should not download when value is null for anyOf with null variant', async () => { |
| 1833 | const toolWithAnyOf: Tool = { |
| 1834 | slug: 'test-tool', |
| 1835 | name: 'Test Tool', |
| 1836 | description: 'A test tool', |
| 1837 | tags: ['test'], |
| 1838 | outputParameters: { |
| 1839 | type: 'object', |
| 1840 | properties: { |
| 1841 | fileOutput: { |
| 1842 | anyOf: [ |
| 1843 | { |
| 1844 | type: 'object', |
| 1845 | file_downloadable: true, |
| 1846 | properties: { |
| 1847 | s3url: { type: 'string' }, |
| 1848 | mimetype: { type: 'string' }, |
| 1849 | }, |
| 1850 | }, |
| 1851 | { |
| 1852 | type: 'null', |
| 1853 | }, |
| 1854 | ], |
| 1855 | }, |
| 1856 | }, |
| 1857 | }, |
| 1858 | version: '20251201_01', |
| 1859 | availableVersions: ['20251201_01'], |
| 1860 | }; |
| 1861 | |
| 1862 | const result = { |
| 1863 | data: { |
| 1864 | fileOutput: null, |
| 1865 | }, |
| 1866 | error: null, |
| 1867 | successful: true, |
| 1868 | }; |
| 1869 | |
| 1870 | const modifiedResult = await fileToolModifier.fileDownloadModifier(toolWithAnyOf, { |
| 1871 | toolSlug: 'test-tool', |
| 1872 | toolkitSlug: 'test-toolkit', |
| 1873 | result, |
| 1874 | }); |
| 1875 | |
| 1876 | expect(fileUtils.downloadFileFromS3).not.toHaveBeenCalled(); |
| 1877 | expect(modifiedResult.data.fileOutput).toBeNull(); |
| 1878 | }); |
| 1879 | }); |
| 1880 | |
| 1881 | // The schema walkers recurse properties/anyOf/oneOf/allOf/items but never |
| 1882 | // dereference `$ref`. Composio toolkits express file flags through a |
| 1883 | // `$ref`/`$defs` indirection (e.g. GMAIL_GET_ATTACHMENT), so without |
| 1884 | // dereferencing the flag is invisible and the file is silently skipped. |
| 1885 | // See https://github.com/ComposioHQ/composio/issues/3506. |
| 1886 | describe('$ref / $defs indirection', () => { |
| 1887 | it('marks a file_uploadable behind a $ref/$defs as format: "path" in modifyToolSchema', async () => { |
| 1888 | const schema: Tool = { |
| 1889 | slug: 'test-tool', |
| 1890 | name: 'Test Tool', |
| 1891 | description: 'A test tool', |
| 1892 | tags: ['test'], |
| 1893 | version: '20251201_01', |
| 1894 | availableVersions: ['20251201_01'], |
| 1895 | inputParameters: { |
| 1896 | type: 'object', |
| 1897 | properties: { |
| 1898 | attachment: { $ref: '#/$defs/Attachment' }, |
| 1899 | legacyAttachment: { $ref: '#/definitions/LegacyAttachment' }, |
| 1900 | text: { type: 'string' }, |
| 1901 | }, |
| 1902 | $defs: { |
| 1903 | Attachment: { |
| 1904 | type: 'string', |
| 1905 | file_uploadable: true, |
| 1906 | description: 'Local path to attach', |
| 1907 | }, |
| 1908 | }, |
| 1909 | definitions: { |
| 1910 | LegacyAttachment: { |
| 1911 | type: 'string', |
| 1912 | file_uploadable: true, |
| 1913 | description: 'Legacy local path to attach', |
| 1914 | }, |
| 1915 | }, |
| 1916 | }, |
| 1917 | }; |
| 1918 | |
| 1919 | const result = await fileToolModifier.modifyToolSchema(schema); |
| 1920 | |
| 1921 | expect(result.inputParameters?.properties?.attachment).toHaveProperty('format', 'path'); |
| 1922 | expect(result.inputParameters?.properties?.attachment).toHaveProperty( |
| 1923 | 'file_uploadable', |
| 1924 | true |
| 1925 | ); |
| 1926 | expect(result.inputParameters?.properties?.legacyAttachment).toHaveProperty('format', 'path'); |
| 1927 | expect(result.inputParameters?.properties?.legacyAttachment).toHaveProperty( |
| 1928 | 'file_uploadable', |
| 1929 | true |
| 1930 | ); |
| 1931 | expect(result.inputParameters?.properties?.text).not.toHaveProperty('format'); |
| 1932 | }); |
| 1933 | |
| 1934 | it('uploads a file whose file_uploadable flag is reachable only via $ref/$defs', async () => { |
| 1935 | const mockFileData = { |
| 1936 | name: 'doc.pdf', |
| 1937 | mimetype: 'application/pdf', |
| 1938 | s3key: 'uploads/doc.pdf', |
| 1939 | }; |
| 1940 | vi.mocked(fileUtils.getFileDataAfterUploadingToS3).mockResolvedValue(mockFileData); |
| 1941 | |
| 1942 | const toolWithRef: Tool = { |
| 1943 | slug: 'test-tool', |
| 1944 | name: 'Test Tool', |
| 1945 | description: 'A test tool', |
| 1946 | tags: ['test'], |
| 1947 | version: '20251201_01', |
| 1948 | availableVersions: ['20251201_01'], |
| 1949 | inputParameters: { |
| 1950 | type: 'object', |
| 1951 | properties: { |
| 1952 | attachment: { $ref: '#/$defs/Attachment' }, |
| 1953 | }, |
| 1954 | $defs: { |
| 1955 | Attachment: { type: 'string', file_uploadable: true }, |
| 1956 | }, |
| 1957 | }, |
| 1958 | }; |
| 1959 | |
| 1960 | const result = await fileToolModifier.fileUploadModifier(toolWithRef, { |
| 1961 | toolSlug: 'test-tool', |
| 1962 | toolkitSlug: 'test-toolkit', |
| 1963 | params: { |
| 1964 | arguments: { attachment: '/path/to/doc.pdf' }, |
| 1965 | userId: 'test-user', |
| 1966 | }, |
| 1967 | }); |
| 1968 | |
| 1969 | expect(fileUtils.getFileDataAfterUploadingToS3).toHaveBeenCalledWith('/path/to/doc.pdf', { |
| 1970 | toolSlug: 'test-tool', |
| 1971 | toolkitSlug: 'test-toolkit', |
| 1972 | client: mockClient, |
| 1973 | }); |
| 1974 | expect(result.arguments?.attachment).toEqual(mockFileData); |
| 1975 | }); |
| 1976 | |
| 1977 | it('degrades gracefully when a $ref has no matching $defs target', async () => { |
| 1978 | // Some toolkits ship a `$ref` without ever declaring `$defs` |
| 1979 | // (https://github.com/ComposioHQ/composio/issues/3307). The modifier |
| 1980 | // must not throw; the unresolved branch simply carries no file flag. |
| 1981 | const schema: Tool = { |
| 1982 | slug: 'test-tool', |
| 1983 | name: 'Test Tool', |
| 1984 | description: 'A test tool', |
| 1985 | tags: ['test'], |
| 1986 | version: '20251201_01', |
| 1987 | availableVersions: ['20251201_01'], |
| 1988 | inputParameters: { |
| 1989 | type: 'object', |
| 1990 | properties: { |
| 1991 | attachment: { $ref: '#/$defs/Missing' }, |
| 1992 | }, |
| 1993 | }, |
| 1994 | }; |
| 1995 | |
| 1996 | const result = await fileToolModifier.modifyToolSchema(schema); |
| 1997 | expect(result.inputParameters?.properties?.attachment).not.toHaveProperty('format'); |
| 1998 | }); |
| 1999 | }); |
| 2000 | }); |
| 2001 | |
| 2002 | describe('Tools with dangerouslyAllowAutoUploadDownloadFiles', () => { |
| 2003 | const context = createTestContext(); |
| 2004 | setupTest(context); |
| 2005 | |
| 2006 | const mockToolWithFileUpload: Tool = { |
| 2007 | slug: 'COMPOSIO_TOOL', |
| 2008 | name: 'Composio Tool', |
| 2009 | description: 'A test tool', |
| 2010 | tags: ['test'], |
| 2011 | toolkit: { |
| 2012 | slug: 'test-toolkit', |
| 2013 | name: 'Test Toolkit', |
| 2014 | }, |
| 2015 | inputParameters: { |
| 2016 | type: 'object' as const, |
| 2017 | properties: { |
| 2018 | file: { |
| 2019 | type: 'string', |
| 2020 | file_uploadable: true, |
| 2021 | }, |
| 2022 | }, |
| 2023 | additionalProperties: false, |
| 2024 | }, |
| 2025 | outputParameters: { |
| 2026 | type: 'object' as const, |
| 2027 | properties: { |
| 2028 | file: { |
| 2029 | type: 'object', |
| 2030 | properties: { |
| 2031 | s3url: { |
| 2032 | type: 'string', |
| 2033 | }, |
| 2034 | mimetype: { |
| 2035 | type: 'string', |
| 2036 | }, |
| 2037 | }, |
| 2038 | }, |
| 2039 | }, |
| 2040 | additionalProperties: false, |
| 2041 | }, |
| 2042 | version: '20251201_01', |
| 2043 | availableVersions: ['20251201_01'], |
| 2044 | }; |
| 2045 | |
| 2046 | const mockRawToolWithFileUpload = { |
| 2047 | slug: 'COMPOSIO_TOOL', |
| 2048 | name: 'Composio Tool', |
| 2049 | description: 'A test tool', |
| 2050 | tags: ['test'], |
| 2051 | toolkit: { |
| 2052 | slug: 'test-toolkit', |
| 2053 | name: 'Test Toolkit', |
| 2054 | }, |
| 2055 | input_parameters: { |
| 2056 | type: 'object', |
| 2057 | properties: { |
| 2058 | file: { |
| 2059 | type: 'string', |
| 2060 | file_uploadable: true, |
| 2061 | }, |
| 2062 | }, |
| 2063 | additionalProperties: false, |
| 2064 | }, |
| 2065 | output_parameters: { |
| 2066 | type: 'object', |
| 2067 | properties: { |
| 2068 | file: { |
| 2069 | type: 'object', |
| 2070 | properties: { |
| 2071 | s3url: { |
| 2072 | type: 'string', |
| 2073 | }, |
| 2074 | mimetype: { |
| 2075 | type: 'string', |
| 2076 | }, |
| 2077 | }, |
| 2078 | }, |
| 2079 | }, |
| 2080 | additionalProperties: false, |
| 2081 | }, |
| 2082 | version: '20251201_01', |
| 2083 | availableVersions: ['20251201_01'], |
| 2084 | }; |
| 2085 | |
| 2086 | describe('when dangerouslyAllowAutoUploadDownloadFiles is true', () => { |
| 2087 | beforeEach(() => { |
| 2088 | context.tools = new Tools(mockClient as unknown as ComposioClient, { |
| 2089 | provider: context.mockProvider, |
| 2090 | dangerouslyAllowAutoUploadDownloadFiles: true, |
| 2091 | }); |
| 2092 | vi.mocked(fileUtils.getFileDataAfterUploadingToS3).mockResolvedValue({ |
| 2093 | name: 'file.txt', |
| 2094 | mimetype: 'text/plain', |
| 2095 | s3key: 'uploads/file.txt', |
| 2096 | }); |
| 2097 | mockClient.toolRouter.session.execute.mockResolvedValue({ |
| 2098 | result_type: 'completed', |
| 2099 | data: { uploaded: true }, |
| 2100 | error: null, |
| 2101 | log_id: 'session-log', |
| 2102 | }); |
| 2103 | }); |
| 2104 | |
| 2105 | it('stages session file arguments before beforeExecute and the request', async () => { |
| 2106 | const beforeExecute = vi.fn(({ params }) => params); |
| 2107 | |
| 2108 | await context.tools.executeSessionTool( |
| 2109 | 'COMPOSIO_TOOL', |
| 2110 | { |
| 2111 | sessionId: 'session-123', |
| 2112 | arguments: { file: '/path/to/file.txt' }, |
| 2113 | }, |
| 2114 | { beforeExecute }, |
| 2115 | mockToolWithFileUpload |
| 2116 | ); |
| 2117 | |
| 2118 | const staged = { name: 'file.txt', mimetype: 'text/plain', s3key: 'uploads/file.txt' }; |
| 2119 | expect(fileUtils.getFileDataAfterUploadingToS3).toHaveBeenCalledWith( |
| 2120 | '/path/to/file.txt', |
| 2121 | expect.objectContaining({ |
| 2122 | toolSlug: 'COMPOSIO_TOOL', |
| 2123 | toolkitSlug: 'test-toolkit', |
| 2124 | }) |
| 2125 | ); |
| 2126 | expect(beforeExecute).toHaveBeenCalledWith({ |
| 2127 | toolSlug: 'COMPOSIO_TOOL', |
| 2128 | toolkitSlug: 'test-toolkit', |
| 2129 | sessionId: 'session-123', |
| 2130 | params: { file: staged }, |
| 2131 | }); |
| 2132 | expect(mockClient.toolRouter.session.execute).toHaveBeenCalledWith( |
| 2133 | 'session-123', |
| 2134 | { |
| 2135 | tool_slug: 'COMPOSIO_TOOL', |
| 2136 | arguments: { file: staged }, |
| 2137 | enable_auto_workbench_offload: true, |
| 2138 | }, |
| 2139 | { maxRetries: 0 } |
| 2140 | ); |
| 2141 | }); |
| 2142 | }); |
| 2143 | |
| 2144 | describe('when dangerouslyAllowAutoUploadDownloadFiles is false', () => { |
| 2145 | beforeEach(async () => { |
| 2146 | context.tools = new Tools(mockClient as unknown as ComposioClient, { |
| 2147 | provider: context.mockProvider, |
| 2148 | dangerouslyAllowAutoUploadDownloadFiles: false, |
| 2149 | }); |
| 2150 | |
| 2151 | // Mock the tool execution |
| 2152 | const { getRawComposioToolBySlugSpy } = await mockToolExecution(context.tools); |
| 2153 | getRawComposioToolBySlugSpy.mockReset(); |
| 2154 | getRawComposioToolBySlugSpy.mockResolvedValue(mockToolWithFileUpload); |
| 2155 | |
| 2156 | // Mock the client's tools.list method |
| 2157 | mockClient.tools.list.mockResolvedValue({ |
| 2158 | items: [mockRawToolWithFileUpload], |
| 2159 | totalPages: 1, |
| 2160 | }); |
| 2161 | |
| 2162 | // Mock the file upload and download utilities |
| 2163 | vi.mocked(fileUtils.getFileDataAfterUploadingToS3).mockResolvedValue({ |
| 2164 | name: 'file.txt', |
| 2165 | mimetype: 'text/plain', |
| 2166 | s3key: 'uploads/file.txt', |
| 2167 | }); |
| 2168 | |
| 2169 | vi.mocked(fileUtils.downloadFileFromS3).mockResolvedValue({ |
| 2170 | name: 'file.txt', |
| 2171 | mimeType: 'text/plain', |
| 2172 | s3Url: 'downloads/file.txt', |
| 2173 | filePath: '/path/to/downloaded/file.txt', |
| 2174 | }); |
| 2175 | |
| 2176 | // Mock the provider's wrapTools method |
| 2177 | context.mockProvider.wrapTools.mockImplementation(tools => tools); |
| 2178 | }); |
| 2179 | |
| 2180 | it('should not modify tool schema for file upload', async () => { |
| 2181 | // The collapsed `{ type: 'string', format: 'path' }` shape is a promise |
| 2182 | // that the SDK will stage local paths for the caller. That promise is |
| 2183 | // only true when auto-upload is on, so we leave the raw shape |
| 2184 | // untouched when the flag is off — callers are expected to pre-stage |
| 2185 | // via `composio.files.upload()`. Runtime behavior is documented by |
| 2186 | // the warning emitted from `applyBeforeExecuteModifiers`. |
| 2187 | const result = await context.tools.getRawComposioTools({ tools: ['COMPOSIO_TOOL'] }); |
| 2188 | expect(result[0].inputParameters?.properties?.file).not.toHaveProperty('format'); |
| 2189 | expect(fileUtils.getFileDataAfterUploadingToS3).not.toHaveBeenCalled(); |
| 2190 | }); |
| 2191 | |
| 2192 | it('should not upload files during execution', async () => { |
| 2193 | // Mock getRawComposioToolBySlug for this test |
| 2194 | vi.spyOn(context.tools, 'getRawComposioToolBySlug').mockResolvedValueOnce( |
| 2195 | mockToolWithFileUpload |
| 2196 | ); |
| 2197 | |
| 2198 | await context.tools.execute('COMPOSIO_TOOL', { |
| 2199 | arguments: { |
| 2200 | file: '/path/to/file.txt', |
| 2201 | }, |
| 2202 | userId: 'test-user', |
| 2203 | dangerouslySkipVersionCheck: true, |
| 2204 | }); |
| 2205 | |
| 2206 | expect(fileUtils.getFileDataAfterUploadingToS3).not.toHaveBeenCalled(); |
| 2207 | expect(mockClient.tools.execute).toHaveBeenCalledWith( |
| 2208 | 'COMPOSIO_TOOL', |
| 2209 | { |
| 2210 | arguments: { |
| 2211 | file: '/path/to/file.txt', |
| 2212 | }, |
| 2213 | allow_tracing: undefined, |
| 2214 | connected_account_id: undefined, |
| 2215 | custom_auth_params: undefined, |
| 2216 | custom_connection_data: undefined, |
| 2217 | text: undefined, |
| 2218 | user_id: 'test-user', |
| 2219 | version: 'latest', |
| 2220 | }, |
| 2221 | { maxRetries: 0 } |
| 2222 | ); |
| 2223 | }); |
| 2224 | |
| 2225 | it('omits empty file values from the request when auto-upload is off (issue #4233)', async () => { |
| 2226 | vi.spyOn(context.tools, 'getRawComposioToolBySlug').mockResolvedValue(mockToolWithFileUpload); |
| 2227 | |
| 2228 | await context.tools.execute('COMPOSIO_TOOL', { |
| 2229 | arguments: { file: '', text: 'keep' }, |
| 2230 | userId: 'test-user', |
| 2231 | dangerouslySkipVersionCheck: true, |
| 2232 | }); |
| 2233 | |
| 2234 | expect(fileUtils.getFileDataAfterUploadingToS3).not.toHaveBeenCalled(); |
| 2235 | expect(mockClient.tools.execute.mock.calls[0]?.[1]?.arguments).toEqual({ text: 'keep' }); |
| 2236 | }); |
| 2237 | |
| 2238 | it('omits empty file values reachable only through a $ref (issue #4233)', async () => { |
| 2239 | // Composio toolkits express file flags through `$ref`/`$defs`, so the |
| 2240 | // cheap "does this tool take a file?" gate has to see through the |
| 2241 | // indirection or the fix never fires for a real tool. |
| 2242 | vi.spyOn(context.tools, 'getRawComposioToolBySlug').mockResolvedValue({ |
| 2243 | ...mockToolWithFileUpload, |
| 2244 | inputParameters: { |
| 2245 | type: 'object', |
| 2246 | properties: { file: { $ref: '#/$defs/Attachment' }, text: { type: 'string' } }, |
| 2247 | $defs: { |
| 2248 | Attachment: { |
| 2249 | type: 'object', |
| 2250 | file_uploadable: true, |
| 2251 | properties: { name: { type: 'string' }, s3key: { type: 'string' } }, |
| 2252 | }, |
| 2253 | }, |
| 2254 | }, |
| 2255 | }); |
| 2256 | |
| 2257 | await context.tools.execute('COMPOSIO_TOOL', { |
| 2258 | arguments: { file: '', text: 'keep' }, |
| 2259 | userId: 'test-user', |
| 2260 | dangerouslySkipVersionCheck: true, |
| 2261 | }); |
| 2262 | |
| 2263 | expect(fileUtils.getFileDataAfterUploadingToS3).not.toHaveBeenCalled(); |
| 2264 | expect(mockClient.tools.execute.mock.calls[0]?.[1]?.arguments).toEqual({ text: 'keep' }); |
| 2265 | }); |
| 2266 | |
| 2267 | it('cleans typeless session file schemas without allowing prototype-key injection', async () => { |
| 2268 | const staged = { name: 'a.txt', mimetype: 'text/plain', s3key: 'staged/a.txt' }; |
| 2269 | const argumentsWithPrototypeKey: Record<string, unknown> = JSON.parse( |
| 2270 | '{"file":"","__proto__":{"polluted":true},"staged":{"name":"a.txt","mimetype":"text/plain","s3key":"staged/a.txt"},"nullable":null,"nested":{"file":"","__proto__":{"nestedPolluted":true},"text":"nested keep"},"text":"keep"}' |
| 2271 | ); |
| 2272 | const tool: Tool = { |
| 2273 | ...mockToolWithFileUpload, |
| 2274 | inputParameters: { |
| 2275 | properties: { |
| 2276 | file: { type: 'string', file_uploadable: true }, |
| 2277 | staged: { type: 'object', file_uploadable: true }, |
| 2278 | nullable: { |
| 2279 | anyOf: [{ type: 'object', file_uploadable: true }, { type: 'null' }], |
| 2280 | }, |
| 2281 | nested: { |
| 2282 | properties: { |
| 2283 | file: { type: 'string', file_uploadable: true }, |
| 2284 | text: { type: 'string' }, |
| 2285 | }, |
| 2286 | }, |
| 2287 | text: { type: 'string' }, |
| 2288 | }, |
| 2289 | }, |
| 2290 | }; |
| 2291 | const beforeExecute = vi.fn(({ params }) => params); |
| 2292 | mockClient.toolRouter.session.execute.mockResolvedValueOnce({ |
| 2293 | result_type: 'completed', |
| 2294 | data: { ok: true }, |
| 2295 | error: null, |
| 2296 | log_id: 'session-log', |
| 2297 | }); |
| 2298 | |
| 2299 | await context.tools.executeSessionTool( |
| 2300 | 'COMPOSIO_TOOL', |
| 2301 | { sessionId: 'session-123', arguments: argumentsWithPrototypeKey }, |
| 2302 | { beforeExecute }, |
| 2303 | tool |
| 2304 | ); |
| 2305 | |
| 2306 | const hookArguments = beforeExecute.mock.calls[0]?.[0].params as Record<string, unknown>; |
| 2307 | expect(Object.getPrototypeOf(hookArguments)).toBe(Object.prototype); |
| 2308 | expect(Object.hasOwn(hookArguments, '__proto__')).toBe(true); |
| 2309 | expect(Object.keys(hookArguments)).toContain('__proto__'); |
| 2310 | expect(hookArguments).not.toHaveProperty('file'); |
| 2311 | expect(hookArguments.__proto__).toEqual({ polluted: true }); |
| 2312 | expect(hookArguments.polluted).toBeUndefined(); |
| 2313 | expect(hookArguments.staged).toEqual(staged); |
| 2314 | expect(hookArguments.nullable).toBeNull(); |
| 2315 | expect(hookArguments.text).toBe('keep'); |
| 2316 | const hookNested = hookArguments.nested as Record<string, unknown>; |
| 2317 | expect(Object.getPrototypeOf(hookNested)).toBe(Object.prototype); |
| 2318 | expect(Object.hasOwn(hookNested, '__proto__')).toBe(true); |
| 2319 | expect(Object.keys(hookNested)).toContain('__proto__'); |
| 2320 | expect(hookNested).not.toHaveProperty('file'); |
| 2321 | expect(hookNested.__proto__).toEqual({ nestedPolluted: true }); |
| 2322 | expect(hookNested.nestedPolluted).toBeUndefined(); |
| 2323 | expect(hookNested.text).toBe('nested keep'); |
| 2324 | |
| 2325 | const requestArguments = mockClient.toolRouter.session.execute.mock.calls[0]?.[1] |
| 2326 | ?.arguments as Record<string, unknown>; |
| 2327 | expect(Object.getPrototypeOf(requestArguments)).toBe(Object.prototype); |
| 2328 | expect(Object.hasOwn(requestArguments, '__proto__')).toBe(true); |
| 2329 | expect(Object.keys(requestArguments)).toContain('__proto__'); |
| 2330 | expect(requestArguments.__proto__).toEqual({ polluted: true }); |
| 2331 | expect(requestArguments.polluted).toBeUndefined(); |
| 2332 | expect(requestArguments.staged).toEqual(staged); |
| 2333 | expect(requestArguments.nullable).toBeNull(); |
| 2334 | const requestNested = requestArguments.nested as Record<string, unknown>; |
| 2335 | expect(Object.getPrototypeOf(requestNested)).toBe(Object.prototype); |
| 2336 | expect(Object.hasOwn(requestNested, '__proto__')).toBe(true); |
| 2337 | expect(Object.keys(requestNested)).toContain('__proto__'); |
| 2338 | expect(requestNested).not.toHaveProperty('file'); |
| 2339 | expect(requestNested.__proto__).toEqual({ nestedPolluted: true }); |
| 2340 | expect(requestNested.nestedPolluted).toBeUndefined(); |
| 2341 | expect(fileUtils.getFileDataAfterUploadingToS3).not.toHaveBeenCalled(); |
| 2342 | }); |
| 2343 | |
| 2344 | it('uses raw session schemas for cleanup when modifySchema removes file flags', async () => { |
| 2345 | const provider = new MockProvider(); |
| 2346 | const session = new ToolRouterSession( |
| 2347 | mockClient as unknown as ComposioClient, |
| 2348 | { |
| 2349 | apiKey: 'test-api-key', |
| 2350 | provider, |
| 2351 | dangerouslyAllowAutoUploadDownloadFiles: false, |
| 2352 | }, |
| 2353 | 'session-123', |
| 2354 | { type: 'http', url: 'https://api.composio.dev/api/v3/tool_router/session/session-123' } |
| 2355 | ); |
| 2356 | mockClient.toolRouter.session.tools.mockResolvedValueOnce({ |
| 2357 | items: [mockRawToolWithFileUpload], |
| 2358 | next_cursor: null, |
| 2359 | }); |
| 2360 | mockClient.toolRouter.session.execute.mockResolvedValueOnce({ |
| 2361 | result_type: 'completed', |
| 2362 | data: { ok: true }, |
| 2363 | error: null, |
| 2364 | log_id: 'session-log', |
| 2365 | }); |
| 2366 | provider.wrapTools.mockImplementation(tools => tools); |
| 2367 | |
| 2368 | await session.tools({ |
| 2369 | modifySchema: ({ schema }) => ({ |
| 2370 | ...schema, |
| 2371 | inputParameters: { |
| 2372 | type: 'object', |
| 2373 | properties: { |
| 2374 | file: { type: 'string' }, |
| 2375 | text: { type: 'string' }, |
| 2376 | }, |
| 2377 | }, |
| 2378 | }), |
| 2379 | }); |
| 2380 | |
| 2381 | const displayTools = provider.wrapTools.mock.calls[0]?.[0] as Tool[]; |
| 2382 | expect(displayTools[0]?.inputParameters?.properties?.file).not.toHaveProperty( |
| 2383 | 'file_uploadable' |
| 2384 | ); |
| 2385 | |
| 2386 | const executeTool = provider.wrapTools.mock.calls[0]?.[1] as ( |
| 2387 | toolSlug: string, |
| 2388 | input: Record<string, unknown> |
| 2389 | ) => Promise<unknown>; |
| 2390 | await executeTool('COMPOSIO_TOOL', { file: '', text: 'keep' }); |
| 2391 | |
| 2392 | expect(fileUtils.getFileDataAfterUploadingToS3).not.toHaveBeenCalled(); |
| 2393 | expect(mockClient.toolRouter.session.execute).toHaveBeenCalledWith( |
| 2394 | 'session-123', |
| 2395 | { |
| 2396 | tool_slug: 'COMPOSIO_TOOL', |
| 2397 | arguments: { text: 'keep' }, |
| 2398 | enable_auto_workbench_offload: true, |
| 2399 | }, |
| 2400 | { maxRetries: 0 } |
| 2401 | ); |
| 2402 | }); |
| 2403 | |
| 2404 | it('warns once per tool when auto-upload is off and the tool has a file-uploadable input', async () => { |
| 2405 | const warnSpy = vi.spyOn((await import('../../src/utils/logger')).default, 'warn'); |
| 2406 | warnSpy.mockImplementation(() => {}); // silence the log in test output |
| 2407 | |
| 2408 | vi.spyOn(context.tools, 'getRawComposioToolBySlug').mockResolvedValue(mockToolWithFileUpload); |
| 2409 | |
| 2410 | // First call emits the warning. |
| 2411 | await context.tools.execute('COMPOSIO_TOOL', { |
| 2412 | arguments: { file: '/path/to/file.txt' }, |
| 2413 | userId: 'u', |
| 2414 | dangerouslySkipVersionCheck: true, |
| 2415 | }); |
| 2416 | expect(warnSpy).toHaveBeenCalledTimes(1); |
| 2417 | const msg = String(warnSpy.mock.calls[0]?.[0] ?? ''); |
| 2418 | expect(msg).toContain('COMPOSIO_TOOL'); |
| 2419 | expect(msg).toContain('dangerouslyAllowAutoUploadDownloadFiles'); |
| 2420 | expect(msg).toContain('composio.files.upload'); |
| 2421 | |
| 2422 | // Repeated executions of the same tool do NOT re-warn. |
| 2423 | await context.tools.execute('COMPOSIO_TOOL', { |
| 2424 | arguments: { file: '/path/to/file.txt' }, |
| 2425 | userId: 'u', |
| 2426 | dangerouslySkipVersionCheck: true, |
| 2427 | }); |
| 2428 | expect(warnSpy).toHaveBeenCalledTimes(1); |
| 2429 | |
| 2430 | warnSpy.mockRestore(); |
| 2431 | }); |
| 2432 | |
| 2433 | it('should not download files from execution results', async () => { |
| 2434 | // Mock getRawComposioToolBySlug for this test |
| 2435 | vi.spyOn(context.tools, 'getRawComposioToolBySlug').mockResolvedValueOnce( |
| 2436 | mockToolWithFileUpload |
| 2437 | ); |
| 2438 | |
| 2439 | // Mock the response data structure correctly |
| 2440 | const mockResponse = { |
| 2441 | data: { |
| 2442 | file: { |
| 2443 | s3url: 'https://s3.example.com/file.txt', |
| 2444 | mimetype: 'text/plain', |
| 2445 | }, |
| 2446 | }, |
| 2447 | error: null, |
| 2448 | successful: true, |
| 2449 | log_id: '123', |
| 2450 | session_info: {}, |
| 2451 | }; |
| 2452 | |
| 2453 | // Mock the client's execute method to return the raw response |
| 2454 | mockClient.tools.execute.mockResolvedValueOnce(mockResponse); |
| 2455 | |
| 2456 | // Mock transformToolExecuteResponse to return the same data structure |
| 2457 | vi.spyOn(context.tools as unknown, 'transformToolExecuteResponse').mockReturnValue({ |
| 2458 | data: mockResponse.data, |
| 2459 | error: mockResponse.error, |
| 2460 | successful: mockResponse.successful, |
| 2461 | logId: mockResponse.log_id, |
| 2462 | sessionInfo: mockResponse.session_info, |
| 2463 | }); |
| 2464 | |
| 2465 | const result = await context.tools.execute('COMPOSIO_TOOL', { |
| 2466 | arguments: { |
| 2467 | file: '/path/to/file.txt', |
| 2468 | }, |
| 2469 | userId: 'test-user', |
| 2470 | dangerouslySkipVersionCheck: true, |
| 2471 | }); |
| 2472 | |
| 2473 | expect(fileUtils.downloadFileFromS3).not.toHaveBeenCalled(); |
| 2474 | expect(result.data.file).toEqual({ |
| 2475 | s3url: 'https://s3.example.com/file.txt', |
| 2476 | mimetype: 'text/plain', |
| 2477 | }); |
| 2478 | }); |
| 2479 | }); |
| 2480 | }); |