Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Mirrored from https://github.com/Camo67/composio.git
Synced Nov 21, 06:00 UTC
| 1 | /** |
| 2 | * E2E fixture: Tool Router session files mount (list, upload, download, delete). |
| 3 | * Requires COMPOSIO_API_KEY in environment. |
| 4 | */ |
| 5 | import { Composio } from '@composio/core'; |
| 6 | |
| 7 | const apiKey = process.env.COMPOSIO_API_KEY; |
| 8 | if (!apiKey) { |
| 9 | console.error('COMPOSIO_API_KEY is required'); |
| 10 | process.exit(1); |
| 11 | } |
| 12 | |
| 13 | const composio = new Composio({ apiKey }); |
| 14 | |
| 15 | function isStorageUploadUnavailable(err) { |
| 16 | const message = err?.message || String(err); |
| 17 | return message.includes('Failed to upload file') && message.includes('Unauthorized'); |
| 18 | } |
| 19 | |
| 20 | async function main() { |
| 21 | // Create a session (hackernews is public, no auth needed; files mount is always available) |
| 22 | const session = await composio.create('e2e-tool-router-files-user', { |
| 23 | toolkits: ['hackernews'], |
| 24 | manageConnections: false, |
| 25 | }); |
| 26 | |
| 27 | const { files } = session.experimental; |
| 28 | const testPath = 'e2e-test-upload.txt'; |
| 29 | const testContent = 'Hello from Tool Router files E2E test'; |
| 30 | |
| 31 | // Upload a buffer |
| 32 | const buffer = new TextEncoder().encode(testContent); |
| 33 | let uploaded; |
| 34 | try { |
| 35 | uploaded = await files.upload(buffer, { |
| 36 | remotePath: testPath, |
| 37 | mimetype: 'text/plain', |
| 38 | }); |
| 39 | } catch (err) { |
| 40 | if (isStorageUploadUnavailable(err)) { |
| 41 | console.log('UPLOAD_UNAVAILABLE'); |
| 42 | console.log(`Upload skipped: ${err.message}`); |
| 43 | console.log('LIST_SKIP'); |
| 44 | console.log('DOWNLOAD_SKIP'); |
| 45 | console.log('DELETE_SKIP'); |
| 46 | console.log('ALL_SKIP'); |
| 47 | return; |
| 48 | } |
| 49 | throw err; |
| 50 | } |
| 51 | if (!uploaded.mountRelativePath || !uploaded.downloadUrl) { |
| 52 | throw new Error('Upload failed: missing mountRelativePath or downloadUrl'); |
| 53 | } |
| 54 | const candidatePaths = Array.from( |
| 55 | new Set([uploaded.mountRelativePath, uploaded.mountRelativePath.replace(/^\/+/, '')]).values() |
| 56 | ).filter(Boolean); |
| 57 | console.log('UPLOAD_OK'); |
| 58 | |
| 59 | // List files (retry for eventual consistency; omit path for root - SDK normalizes) |
| 60 | let listOk = false; |
| 61 | for (let attempt = 0; attempt < 5; attempt++) { |
| 62 | const listResult = await files.list(); |
| 63 | const found = listResult.items?.some( |
| 64 | item => |
| 65 | candidatePaths.includes(item.mountRelativePath) || |
| 66 | item.mountRelativePath === testPath || |
| 67 | item.mountRelativePath?.endsWith(testPath) || |
| 68 | item.mountRelativePath?.includes(testPath) |
| 69 | ); |
| 70 | if (found) { |
| 71 | listOk = true; |
| 72 | break; |
| 73 | } |
| 74 | await new Promise(r => setTimeout(r, 1000)); |
| 75 | } |
| 76 | if (listOk) console.log('LIST_OK'); |
| 77 | else console.log('LIST_SKIP'); // eventual consistency |
| 78 | |
| 79 | // Download the file (retry for eventual consistency, same as list above) |
| 80 | let downloaded; |
| 81 | let lastDownloadError; |
| 82 | let resolvedPath; |
| 83 | for (let attempt = 0; attempt < 5; attempt++) { |
| 84 | for (const candidatePath of candidatePaths) { |
| 85 | try { |
| 86 | const remoteFile = await files.download(candidatePath); |
| 87 | const content = await remoteFile.text(); |
| 88 | if (content !== testContent) { |
| 89 | throw new Error(`Download failed: expected "${testContent}", got "${content}"`); |
| 90 | } |
| 91 | downloaded = remoteFile; |
| 92 | resolvedPath = candidatePath; |
| 93 | break; |
| 94 | } catch (err) { |
| 95 | lastDownloadError = err; |
| 96 | } |
| 97 | } |
| 98 | if (downloaded) { |
| 99 | break; |
| 100 | } |
| 101 | if (attempt < 4) { |
| 102 | await new Promise(r => setTimeout(r, 1000)); |
| 103 | } |
| 104 | } |
| 105 | if (!downloaded) { |
| 106 | throw lastDownloadError ?? new Error('Download failed after retries'); |
| 107 | } |
| 108 | console.log('DOWNLOAD_OK'); |
| 109 | |
| 110 | // Delete the file (use path from API response) |
| 111 | await files.delete(resolvedPath ?? candidatePaths[0]); |
| 112 | console.log('DELETE_OK'); |
| 113 | |
| 114 | console.log('ALL_OK'); |
| 115 | } |
| 116 | |
| 117 | main().catch(err => { |
| 118 | const message = err?.message || String(err); |
| 119 | if (message.includes('Failed to upload file: Unauthorized')) { |
| 120 | console.log(`FILES_MOUNT_UNAVAILABLE storage authorization failed: ${message}`); |
| 121 | process.exit(0); |
| 122 | } |
| 123 | console.log('ERROR:', message); |
| 124 | process.exit(1); |
| 125 | }); |