@devries-cameron20 / composio-1 / commits / 24dfd33cb
Fork 0

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

fix(core): let `files.upload()` and `files.download()` be cancelled (#5412)

This PR: - closes #5410 - adds a trailing optional `ComposioRequestOptions` argument (`{ signal }`) to `composio.files.upload()` and `composio.files.download()`, matching the other SDK methods from #3463 - forwards the signal to the URL read, the presigned-URL request, and the S3 transfer, including the download body read; the helpers in `fileUtils.node.ts` already accepted it, but `Files` never passed one - wraps both methods in `withCancellation()`, so an aborted call rejects with `ComposioRequestCancelledError` and a cancelled download writes nothing to disk - makes `withCancellation()` also treat an error that *is* `signal.reason` as a cancellation. A direct `fetch` rejects with the signal's reason, so `AbortSignal.timeout()` surfaced as a raw `TimeoutError` (and `abort('reason')` as that value) instead of `ComposioRequestCancelledError`. Unrelated failures that coincide with a late abort still pass through unchanged - adds `test/models/Files.test.ts` (11 tests) and a `@composio/core` patch changeset ## Summary ```typescript const staged = await composio.files.upload( { file: './large-file.pdf', toolSlug: 'GOOGLEDRIVE_UPLOAD_FILE', toolkitSlug: 'googledrive' }, { signal: AbortSignal.timeout(30_000) } ); ``` Existing single-argument calls keep working. The Workers/Edge `Files` stub throws for every method, so it needs no change. The Python SDK has no request-cancellation surface for files, so this change is TypeScript only. ## How has this been tested? - `pnpm --filter @composio/core test`: 2045 passed, 2 expected fail - `pnpm --filter @composio/core typecheck`, `oxlint`, `prettier --check`, `pnpm run lint:antislop`: clean - New tests failed before the fix: the signal was dropped, and without the `signal.reason` check the `AbortSignal.timeout()` and custom-reason cases still fail

Alberto Schiabel committed Nov 16, 2026 at 20:23 UTC 24dfd33cbb965d042b5ac18269908b0ad7d0f4e0
4 files changed +323 -38
.changeset/files-request-cancellation.md new
+5
@@ -0,0 +1,5 @@
1 +---
2 +'@composio/core': patch
3 +---
4 +
5 +Let `composio.files.upload()` and `composio.files.download()` be cancelled. Both now accept trailing request options (`{ signal }`), like the other SDK methods that do HTTP I/O. The signal reaches the presigned-URL request and the S3 transfer, and an aborted call rejects with `ComposioRequestCancelledError` instead of running to completion. Calls cancelled by `AbortSignal.timeout()` or by `abort(reason)` during a direct S3 transfer now also reject with `ComposioRequestCancelledError` rather than the signal's raw reason.
ts/packages/core/src/models/Files.node.ts
+68 -37
@@ -7,7 +7,9 @@
7 */
8 import ComposioClient from '@composio/client';
9 import { FileDownloadData, FileUploadData } from '../types/files.types';
10 +import { ComposioRequestOptions } from '../types/requestOptions.types';
11 import { downloadFileFromS3, getFileDataAfterUploadingToS3 } from '../utils/fileUtils.node';
12 +import { withCancellation } from '../utils/cancellation';
13 import { telemetry } from '../telemetry/Telemetry';
14
15 export class Files {
@@ -36,58 +38,87 @@ export class Files {
38 * @param {File | string} params.file - The path to the file to upload, a URL of the file, or a File object.
39 * @param {string} params.toolSlug - The slug of the tool that is uploading the file.
40 * @param {string} params.toolkitSlug - The slug of the toolkit that is uploading the file.
41 + * @param {ComposioRequestOptions} [requestOptions] - Optional request options. `signal` cancels
42 + * the file read from a URL, the presigned-URL request, and the S3 transfer; the call then
43 + * rejects with {@link ComposioRequestCancelledError}.
44 * @returns {Promise<FileUploadData>} The uploaded file data.
45 *
46 * @example
47 *
43 - * const fileData = await composio.files.upload({
44 - * file: 'path/to/file.pdf',
45 - * toolSlug: 'google_drive_upload',
46 - * toolkitSlug: 'google_drive'
47 - * });
48 + * const fileData = await composio.files.upload(
49 + * {
50 + * file: 'path/to/file.pdf',
51 + * toolSlug: 'google_drive_upload',
52 + * toolkitSlug: 'google_drive',
53 + * },
54 + * { signal: AbortSignal.timeout(30_000) }
55 + * );
56 * */
49 - async upload({
50 - file,
51 - toolSlug,
52 - toolkitSlug,
53 - }: {
54 - file: File | string;
55 - toolSlug: string;
56 - toolkitSlug: string;
57 - }): Promise<FileUploadData> {
57 + async upload(
58 + {
59 + file,
60 + toolSlug,
61 + toolkitSlug,
62 + }: {
63 + file: File | string;
64 + toolSlug: string;
65 + toolkitSlug: string;
66 + },
67 + requestOptions?: ComposioRequestOptions
68 + ): Promise<FileUploadData> {
69 + const signal = requestOptions?.signal;
70 // Note: manual `composio.files.upload()` intentionally skips the
71 // `fileUploadDirs` allowlist. The allowlist only gates *automatic* upload
72 // during tool execution (see FileToolModifier).
61 - const fileData = await getFileDataAfterUploadingToS3(file, {
62 - toolSlug,
63 - toolkitSlug,
64 - client: this.client,
65 - sensitiveFileUploadProtection: this.fileOptions.sensitiveFileUploadProtection,
66 - fileUploadPathDenySegments: this.fileOptions.fileUploadPathDenySegments,
67 - });
68 - return fileData;
73 + return withCancellation(
74 + () =>
75 + getFileDataAfterUploadingToS3(file, {
76 + toolSlug,
77 + toolkitSlug,
78 + client: this.client,
79 + sensitiveFileUploadProtection: this.fileOptions.sensitiveFileUploadProtection,
80 + fileUploadPathDenySegments: this.fileOptions.fileUploadPathDenySegments,
81 + signal,
82 + }),
83 + signal
84 + );
85 }
86
87 /**
88 * Download a file from S3 and return the file data.
73 - * @param s3key - The S3 key of the file to download.
74 - * @returns The file data.
89 + *
90 + * @param params - The download parameters.
91 + * @param {string} params.s3Url - The S3 URL of the file to download.
92 + * @param {string} params.toolSlug - The slug of the tool the file belongs to, used as the file name prefix.
93 + * @param {string} params.mimeType - The MIME type of the file, used to pick the file extension.
94 + * @param {ComposioRequestOptions} [requestOptions] - Optional request options. `signal` cancels
95 + * the download before the file is written; the call then rejects with
96 + * {@link ComposioRequestCancelledError}.
97 + * @returns {Promise<FileDownloadData>} The downloaded file data.
98 */
76 - async download({
77 - toolSlug,
78 - s3Url,
79 - mimeType,
80 - }: {
81 - s3Url: string;
82 - toolSlug: string;
83 - mimeType: string;
84 - }): Promise<FileDownloadData> {
85 - const fileDownloadData = await downloadFileFromS3({
99 + async download(
100 + {
101 toolSlug,
102 s3Url,
103 mimeType,
89 - fileDownloadDir: this.fileOptions.fileDownloadDir,
90 - });
91 - return fileDownloadData;
104 + }: {
105 + s3Url: string;
106 + toolSlug: string;
107 + mimeType: string;
108 + },
109 + requestOptions?: ComposioRequestOptions
110 + ): Promise<FileDownloadData> {
111 + const signal = requestOptions?.signal;
112 + return withCancellation(
113 + () =>
114 + downloadFileFromS3({
115 + toolSlug,
116 + s3Url,
117 + mimeType,
118 + fileDownloadDir: this.fileOptions.fileDownloadDir,
119 + signal,
120 + }),
121 + signal
122 + );
123 }
124 }
ts/packages/core/src/utils/cancellation.ts
+5 -1
@@ -8,7 +8,11 @@ export async function withCancellation<T>(
8 try {
9 return await call();
10 } catch (error) {
11 - if (signal?.aborted && isRequestAbortError(error)) {
11 + // A raw `fetch` rejects with the signal's reason, so an `AbortSignal.timeout()`
12 + // surfaces as a `TimeoutError` (or a custom reason passed to `abort()`) that is
13 + // not abort-shaped. Matching the reason itself still excludes an unrelated
14 + // failure that merely coincides with a late abort.
15 + if (signal?.aborted && (isRequestAbortError(error) || error === signal.reason)) {
16 const underlyingMessage = error instanceof Error ? error.message : '';
17 const message = underlyingMessage
18 ? `Request was cancelled by the caller: ${underlyingMessage}`
ts/packages/core/test/models/Files.test.ts new
+245
@@ -0,0 +1,245 @@
1 +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
2 +import * as fs from 'node:fs';
3 +import ComposioClient, { APIUserAbortError } from '@composio/client';
4 +import { Files } from '../../src/models/Files.node';
5 +import { ComposioRequestCancelledError } from '../../src/errors/SDKErrors';
6 +
7 +vi.mock('fs', () => ({
8 + default: {
9 + readFileSync: vi.fn(),
10 + writeFileSync: vi.fn(),
11 + existsSync: vi.fn(() => true),
12 + mkdirSync: vi.fn(),
13 + },
14 + readFileSync: vi.fn(),
15 + writeFileSync: vi.fn(),
16 + existsSync: vi.fn(() => true),
17 + mkdirSync: vi.fn(),
18 +}));
19 +
20 +// The SSRF guard resolves every S3 host before fetching it; resolve the test
21 +// hosts to a public address so no lookup leaves the process.
22 +vi.mock('node:dns/promises', () => ({
23 + lookup: vi.fn().mockResolvedValue([{ address: '93.184.216.34', family: 4 }]),
24 +}));
25 +
26 +const mockFetch = vi.fn();
27 +global.fetch = mockFetch;
28 +
29 +/**
30 + * A transfer that only ends when its signal aborts, rejecting with the
31 + * signal's reason the way `fetch` does. A call that drops the caller's signal
32 + * fails at once instead of hanging the test.
33 + */
34 +const transferUntilAborted = (_url: string, init?: RequestInit): Promise<Response> =>
35 + new Promise((_resolve, reject) => {
36 + const signal = init?.signal;
37 + if (!signal) {
38 + reject(new Error('fetch was called without the caller signal'));
39 + return;
40 + }
41 + if (signal.aborted) {
42 + reject(signal.reason);
43 + return;
44 + }
45 + signal.addEventListener('abort', () => reject(signal.reason), { once: true });
46 + });
47 +
48 +/**
49 + * A download whose headers arrive at once but whose body stalls after the
50 + * first chunk. Like `fetch`, the body stream fails with the signal's reason
51 + * when the signal aborts. `bodyRead` reports when the SDK has started reading.
52 + */
53 +const responseWithStalledBody = () => {
54 + const state = { bodyRead: false };
55 + const fetchImpl = (_url: string, init?: RequestInit): Promise<Response> => {
56 + const signal = init?.signal;
57 + if (!signal) {
58 + return Promise.reject(new Error('fetch was called without the caller signal'));
59 + }
60 + const body = new ReadableStream<Uint8Array>({
61 + start(controller) {
62 + signal.addEventListener('abort', () => controller.error(signal.reason), { once: true });
63 + },
64 + pull(controller) {
65 + if (state.bodyRead) {
66 + return new Promise<void>(() => {});
67 + }
68 + state.bodyRead = true;
69 + controller.enqueue(new Uint8Array(4));
70 + },
71 + });
72 + return Promise.resolve(new Response(body, { status: 200 }));
73 + };
74 + return { state, fetchImpl };
75 +};
76 +
77 +const uploadParams = {
78 + file: new File(['report contents'], 'report.txt', { type: 'text/plain' }),
79 + toolSlug: 'GOOGLEDRIVE_UPLOAD_FILE',
80 + toolkitSlug: 'googledrive',
81 +};
82 +
83 +const downloadParams = {
84 + s3Url: 'https://s3.example.com/report.pdf',
85 + toolSlug: 'GOOGLEDRIVE_DOWNLOAD_FILE',
86 + mimeType: 'application/pdf',
87 +};
88 +
89 +describe('Files request cancellation', () => {
90 + let createPresignedURL: ReturnType<typeof vi.fn>;
91 + let files: Files;
92 +
93 + beforeEach(() => {
94 + vi.clearAllMocks();
95 + createPresignedURL = vi.fn().mockResolvedValue({
96 + key: 'uploads/report.txt',
97 + type: 'new',
98 + new_presigned_url: 'https://s3.example.com/upload',
99 + });
100 + const client = { files: { createPresignedURL } } as unknown as ComposioClient;
101 + files = new Files(client);
102 + });
103 +
104 + afterEach(() => {
105 + vi.restoreAllMocks();
106 + });
107 +
108 + describe('upload', () => {
109 + it('sends the caller signal with the presigned-URL request and the S3 transfer', async () => {
110 + const controller = new AbortController();
111 + mockFetch.mockResolvedValueOnce(new Response(null, { status: 200 }));
112 +
113 + await expect(files.upload(uploadParams, { signal: controller.signal })).resolves.toEqual({
114 + name: 'report.txt',
115 + mimetype: 'text/plain',
116 + s3key: 'uploads/report.txt',
117 + });
118 +
119 + expect(createPresignedURL).toHaveBeenCalledWith(expect.any(Object), {
120 + signal: controller.signal,
121 + });
122 + expect(mockFetch).toHaveBeenCalledWith(
123 + 'https://s3.example.com/upload',
124 + expect.objectContaining({ method: 'PUT', signal: controller.signal })
125 + );
126 + });
127 +
128 + it('rejects with ComposioRequestCancelledError when aborted during the S3 transfer', async () => {
129 + const controller = new AbortController();
130 + mockFetch.mockImplementationOnce(transferUntilAborted);
131 +
132 + const upload = files.upload(uploadParams, { signal: controller.signal });
133 + await vi.waitFor(() => expect(mockFetch).toHaveBeenCalled());
134 + controller.abort();
135 +
136 + await expect(upload).rejects.toBeInstanceOf(ComposioRequestCancelledError);
137 + });
138 +
139 + it('rejects with ComposioRequestCancelledError when aborted during the presigned-URL request', async () => {
140 + createPresignedURL.mockRejectedValueOnce(new APIUserAbortError());
141 +
142 + await expect(
143 + files.upload(uploadParams, { signal: AbortSignal.abort() })
144 + ).rejects.toBeInstanceOf(ComposioRequestCancelledError);
145 + expect(mockFetch).not.toHaveBeenCalled();
146 + });
147 +
148 + it('rejects with ComposioRequestCancelledError when an AbortSignal.timeout fires', async () => {
149 + mockFetch.mockImplementationOnce(transferUntilAborted);
150 +
151 + await expect(
152 + files.upload(uploadParams, { signal: AbortSignal.timeout(10) })
153 + ).rejects.toBeInstanceOf(ComposioRequestCancelledError);
154 + });
155 +
156 + it('rejects with ComposioRequestCancelledError when aborted while reading a URL', async () => {
157 + const controller = new AbortController();
158 + mockFetch.mockImplementationOnce(transferUntilAborted);
159 +
160 + const upload = files.upload(
161 + { ...uploadParams, file: 'https://files.example.com/report.pdf' },
162 + { signal: controller.signal }
163 + );
164 + await vi.waitFor(() =>
165 + expect(mockFetch).toHaveBeenCalledWith(
166 + 'https://files.example.com/report.pdf',
167 + expect.objectContaining({ signal: controller.signal })
168 + )
169 + );
170 + controller.abort();
171 +
172 + await expect(upload).rejects.toBeInstanceOf(ComposioRequestCancelledError);
173 + expect(createPresignedURL).not.toHaveBeenCalled();
174 + });
175 +
176 + it('keeps a failure that is not an abort as-is', async () => {
177 + mockFetch.mockResolvedValueOnce(new Response(null, { status: 403, statusText: 'Forbidden' }));
178 +
179 + await expect(
180 + files.upload(uploadParams, { signal: new AbortController().signal })
181 + ).rejects.toThrow('Failed to upload file to S3: Forbidden');
182 + });
183 + });
184 +
185 + describe('download', () => {
186 + it('rejects with ComposioRequestCancelledError and writes nothing when aborted', async () => {
187 + const controller = new AbortController();
188 + mockFetch.mockImplementationOnce(transferUntilAborted);
189 +
190 + const download = files.download(downloadParams, { signal: controller.signal });
191 + await vi.waitFor(() => expect(mockFetch).toHaveBeenCalled());
192 + controller.abort();
193 +
194 + await expect(download).rejects.toBeInstanceOf(ComposioRequestCancelledError);
195 + expect(fs.writeFileSync).not.toHaveBeenCalled();
196 + });
197 +
198 + it('rejects with ComposioRequestCancelledError when an AbortSignal.timeout fires', async () => {
199 + mockFetch.mockImplementationOnce(transferUntilAborted);
200 +
201 + await expect(
202 + files.download(downloadParams, { signal: AbortSignal.timeout(10) })
203 + ).rejects.toBeInstanceOf(ComposioRequestCancelledError);
204 + expect(fs.writeFileSync).not.toHaveBeenCalled();
205 + });
206 +
207 + it('rejects with ComposioRequestCancelledError and writes nothing when aborted while reading the body', async () => {
208 + const controller = new AbortController();
209 + const { state, fetchImpl } = responseWithStalledBody();
210 + mockFetch.mockImplementationOnce(fetchImpl);
211 +
212 + const download = files.download(downloadParams, { signal: controller.signal });
213 + await vi.waitFor(() => expect(state.bodyRead).toBe(true));
214 + controller.abort();
215 +
216 + await expect(download).rejects.toBeInstanceOf(ComposioRequestCancelledError);
217 + expect(fs.writeFileSync).not.toHaveBeenCalled();
218 + });
219 +
220 + it('rejects with ComposioRequestCancelledError when aborted with a custom reason', async () => {
221 + const controller = new AbortController();
222 + mockFetch.mockImplementationOnce(transferUntilAborted);
223 +
224 + const download = files.download(downloadParams, { signal: controller.signal });
225 + await vi.waitFor(() => expect(mockFetch).toHaveBeenCalled());
226 + controller.abort('caller gave up');
227 +
228 + const error = await download.catch((e: unknown) => e);
229 + expect(error).toBeInstanceOf(ComposioRequestCancelledError);
230 + expect((error as Error).message).toBe('Request was cancelled by the caller');
231 + expect((error as Error).cause).toBeUndefined();
232 + });
233 +
234 + it('downloads the file when the signal does not abort', async () => {
235 + mockFetch.mockResolvedValueOnce(new Response(new Uint8Array(8), { status: 200 }));
236 +
237 + const result = await files.download(downloadParams, {
238 + signal: new AbortController().signal,
239 + });
240 +
241 + expect(result.s3Url).toBe(downloadParams.s3Url);
242 + expect(fs.writeFileSync).toHaveBeenCalledOnce();
243 + });
244 + });
245 +});