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 22, 14:30 UTC
feat(ts): add platform-specific #file_tool_modifier to avoid using "node:crypto" in Cloudflare Workers runtime
jkomyno committed
Jan 22, 2026 at 18:59 UTC
b143395a6f2d407e8920ca55d220cc1d95cbcc03
9 files changed
+220
-166
ts/packages/core/package.json
+12
@@ -33,6 +33,18 @@
33
"import": "./dist/models/Files.node.mjs",
34
"require": "./dist/models/Files.node.cjs"
35
}
36
+ },
37
+ "#file_tool_modifier": {
38
+ "workerd": "./dist/utils/modifiers/FileToolModifier.workerd.mjs",
39
+ "edge-light": "./dist/utils/modifiers/FileToolModifier.workerd.mjs",
40
+ "node": {
41
+ "import": "./dist/utils/modifiers/FileToolModifier.node.mjs",
42
+ "require": "./dist/utils/modifiers/FileToolModifier.node.cjs"
43
+ },
44
+ "default": {
45
+ "import": "./dist/utils/modifiers/FileToolModifier.node.mjs",
46
+ "require": "./dist/utils/modifiers/FileToolModifier.node.cjs"
47
+ }
48
}
49
},
50
"exports": {
ts/packages/core/src/models/Tools.ts
+1
-1
@@ -1,4 +1,5 @@
1
import ComposioClient from '@composio/client';
2
+import { FileToolModifier } from '#file_tool_modifier';
3
import {
4
Tool,
5
ToolExecuteParams,
@@ -48,7 +49,6 @@ import {
49
} from '../errors/ToolErrors';
50
import { ValidationError } from '../errors/ValidationErrors';
51
import { telemetry } from '../telemetry/Telemetry';
51
-import { FileToolModifier } from '../utils/modifiers/FileToolModifier';
52
import { ComposioConfig } from '../composio';
53
import { getToolkitVersion } from '../utils/toolkitVersion';
54
import { handleToolExecutionError } from '../errors/ToolErrors';
ts/packages/core/src/utils/fileUtils.ts
+1
-1
@@ -1,4 +1,4 @@
1
-import crypto from 'crypto';
1
+import crypto from 'node:crypto';
2
import ComposioClient from '@composio/client';
3
import { COMPOSIO_DIR, TEMP_FILES_DIRECTORY_NAME } from './constants';
4
import logger from './logger';
ts/packages/core/src/utils/modifiers/FileToolModifier.node.ts
renamed
+7
-162
@@ -1,139 +1,19 @@
1
import {
2
- JSONSchemaProperty,
2
Tool,
3
ToolExecuteParams,
4
ToolExecuteResponse,
5
+ JSONSchemaProperty,
6
} from '../../types/tool.types';
7
import ComposioClient from '@composio/client';
8
import logger from '../logger';
9
import { ComposioFileUploadError } from '../../errors/FileModifierErrors';
10
import { downloadFileFromS3, getFileDataAfterUploadingToS3 } from '../fileUtils';
11
-
12
-/**
13
- * Transforms a single JSON schema property, recursively handling nested properties,
14
- * anyOf, oneOf, and allOf.
15
- */
16
-const transformSchema = (property: JSONSchemaProperty): JSONSchemaProperty => {
17
- if (property.file_uploadable) {
18
- // Transform file-uploadable property
19
- return {
20
- title: property.title,
21
- description: property.description,
22
- format: 'path',
23
- type: 'string',
24
- file_uploadable: true,
25
- };
26
- }
27
-
28
- const newProperty = { ...property };
29
-
30
- if (property.type === 'object' && property.properties) {
31
- // Recursively transform nested properties
32
- newProperty.properties = transformProperties(property.properties);
33
- }
34
-
35
- if (property.anyOf) {
36
- newProperty.anyOf = property.anyOf.map(transformSchema);
37
- }
38
-
39
- if (property.oneOf) {
40
- newProperty.oneOf = property.oneOf.map(transformSchema);
41
- }
42
-
43
- if (property.allOf) {
44
- newProperty.allOf = property.allOf.map(transformSchema);
45
- }
46
-
47
- if (property.items) {
48
- if (Array.isArray(property.items)) {
49
- newProperty.items = property.items.map(transformSchema);
50
- } else {
51
- newProperty.items = transformSchema(property.items);
52
- }
53
- }
54
-
55
- return newProperty;
56
-};
57
-
58
-/**
59
- * Transforms the properties of the tool schema to include the file upload URL.
60
- *
61
- * Attaches the format: 'path' to the properties that are file uploadable for agents.
62
- *
63
- * @param properties - The properties of the tool schema.
64
- * @returns The transformed properties.
65
- */
66
-const transformProperties = (properties: JSONSchemaProperty): JSONSchemaProperty => {
67
- const newProperties: JSONSchemaProperty = {};
68
-
69
- for (const [key, property] of Object.entries(properties) as [string, JSONSchemaProperty][]) {
70
- newProperties[key] = transformSchema(property);
71
- }
72
-
73
- return newProperties;
74
-};
75
-
76
-/**
77
- * Recursively checks if a schema (or any of its variants) contains a specific file property.
78
- */
79
-const schemaHasFileProperty = (
80
- schema: JSONSchemaProperty | undefined,
81
- property: 'file_uploadable' | 'file_downloadable'
82
-): boolean => {
83
- if (!schema) return false;
84
- if (schema[property]) return true;
85
-
86
- // Check nested properties
87
- if (schema.properties) {
88
- for (const prop of Object.values(schema.properties) as JSONSchemaProperty[]) {
89
- if (schemaHasFileProperty(prop, property)) return true;
90
- }
91
- }
92
-
93
- // Check anyOf/oneOf/allOf variants
94
- if (schema.anyOf) {
95
- for (const variant of schema.anyOf) {
96
- if (schemaHasFileProperty(variant, property)) return true;
97
- }
98
- }
99
- if (schema.oneOf) {
100
- for (const variant of schema.oneOf) {
101
- if (schemaHasFileProperty(variant, property)) return true;
102
- }
103
- }
104
- if (schema.allOf) {
105
- for (const variant of schema.allOf) {
106
- if (schemaHasFileProperty(variant, property)) return true;
107
- }
108
- }
109
-
110
- // Check array items
111
- if (schema.items) {
112
- if (Array.isArray(schema.items)) {
113
- for (const item of schema.items) {
114
- if (schemaHasFileProperty(item, property)) return true;
115
- }
116
- } else {
117
- if (schemaHasFileProperty(schema.items, property)) return true;
118
- }
119
- }
120
-
121
- return false;
122
-};
123
-
124
-/**
125
- * Recursively checks if a schema (or any of its variants) contains file_uploadable properties.
126
- */
127
-const schemaHasFileUploadable = (schema: JSONSchemaProperty | undefined): boolean => {
128
- return schemaHasFileProperty(schema, 'file_uploadable');
129
-};
130
-
131
-/**
132
- * Recursively checks if a schema (or any of its variants) contains file_downloadable properties.
133
- */
134
-const schemaHasFileDownloadable = (schema: JSONSchemaProperty | undefined): boolean => {
135
- return schemaHasFileProperty(schema, 'file_downloadable');
136
-};
11
+import {
12
+ isPlainObject,
13
+ transformProperties,
14
+ schemaHasFileUploadable,
15
+ schemaHasFileDownloadable,
16
+} from './FileToolModifier.utils.neutral';
17
18
/**
19
* Recursively walks a runtime value and its matching JSON-Schema node,
@@ -352,11 +232,6 @@ const hydrateDownloads = async (
232
return value;
233
};
234
355
-// Small helper to recognise plain objects
356
-function isPlainObject(val: unknown): val is Record<string, unknown> {
357
- return typeof val === 'object' && val !== null && !Array.isArray(val);
358
-}
359
-
235
export class FileToolModifier {
236
private client: ComposioClient;
237
@@ -364,16 +239,6 @@ export class FileToolModifier {
239
this.client = client;
240
}
241
367
- /**
368
- * Modifies the tool schema to include the file upload URL.
369
- *
370
- * @description This modifier is used to upload a file to the Composio platform and replace the file path with the file upload URL.
371
- *
372
- * @param _toolSlug - The slug of the tool that is being executed.
373
- * @param _toolkitSlug - The slug of the toolkit that is being executed.
374
- * @param schema - The schema of the tool.
375
- * @returns The schema with the file upload URL included.
376
- */
242
async modifyToolSchema(toolSlug: string, toolkitSlug: string, schema: Tool): Promise<Tool> {
243
if (!schema.inputParameters?.properties) {
244
return schema;
@@ -390,15 +255,6 @@ export class FileToolModifier {
255
};
256
}
257
393
- /**
394
- * Modifies the input parameters to include the file upload URL.
395
- *
396
- * @description This modifier is used to upload a file to the Composio platform and replace the file path with the file upload URL.
397
- *
398
- * @param toolSlug - The slug of the tool that is being executed.
399
- * @param toolkitSlug - The slug of the toolkit that is being executed.
400
- *
401
- */
258
async fileUploadModifier(
259
tool: Tool,
260
options: {
@@ -427,17 +283,6 @@ export class FileToolModifier {
283
}
284
}
285
430
- /**
431
- * Modifies the result to include the file download URL.
432
- *
433
- * @description This modifier is used to download a file and
434
- *
435
- * @param tool - The tool schema containing output parameters.
436
- * @param toolSlug - The slug of the tool that is being executed.
437
- * @param toolkitSlug - The slug of the toolkit that is being executed.
438
- * @param result - The result of the tool execution.
439
- * @returns The result with the file download URL included.
440
- */
286
async fileDownloadModifier(
287
tool: Tool,
288
options: {
ts/packages/core/src/utils/modifiers/FileToolModifier.utils.neutral.ts
new
+132
@@ -0,0 +1,132 @@
1
+import type { JSONSchemaProperty } from '../../types/tool.types';
2
+
3
+// Helper function to recognise plain objects
4
+export function isPlainObject(val: unknown): val is Record<string, unknown> {
5
+ return typeof val === 'object' && val !== null && !Array.isArray(val);
6
+}
7
+
8
+/**
9
+ * Transforms a single JSON schema property, recursively handling nested properties,
10
+ * anyOf, oneOf, and allOf.
11
+ */
12
+const transformSchema = (property: JSONSchemaProperty): JSONSchemaProperty => {
13
+ if (property.file_uploadable) {
14
+ // Transform file-uploadable property
15
+ return {
16
+ title: property.title,
17
+ description: property.description,
18
+ format: 'path',
19
+ type: 'string',
20
+ file_uploadable: true,
21
+ };
22
+ }
23
+
24
+ const newProperty = { ...property };
25
+
26
+ if (property.type === 'object' && property.properties) {
27
+ // Recursively transform nested properties
28
+ newProperty.properties = transformProperties(property.properties);
29
+ }
30
+
31
+ if (property.anyOf) {
32
+ newProperty.anyOf = property.anyOf.map(transformSchema);
33
+ }
34
+
35
+ if (property.oneOf) {
36
+ newProperty.oneOf = property.oneOf.map(transformSchema);
37
+ }
38
+
39
+ if (property.allOf) {
40
+ newProperty.allOf = property.allOf.map(transformSchema);
41
+ }
42
+
43
+ if (property.items) {
44
+ if (Array.isArray(property.items)) {
45
+ newProperty.items = property.items.map(transformSchema);
46
+ } else {
47
+ newProperty.items = transformSchema(property.items);
48
+ }
49
+ }
50
+
51
+ return newProperty;
52
+};
53
+
54
+/**
55
+ * Transforms the properties of the tool schema to include the file upload URL.
56
+ *
57
+ * Attaches the format: 'path' to the properties that are file uploadable for agents.
58
+ *
59
+ * @param properties - The properties of the tool schema.
60
+ * @returns The transformed properties.
61
+ */
62
+export const transformProperties = (properties: JSONSchemaProperty): JSONSchemaProperty => {
63
+ const newProperties: JSONSchemaProperty = {};
64
+
65
+ for (const [key, property] of Object.entries(properties) as [string, JSONSchemaProperty][]) {
66
+ newProperties[key] = transformSchema(property);
67
+ }
68
+
69
+ return newProperties;
70
+};
71
+
72
+/**
73
+ * Recursively checks if a schema (or any of its variants) contains a specific file property.
74
+ */
75
+export const schemaHasFileProperty = (
76
+ schema: JSONSchemaProperty | undefined,
77
+ property: 'file_uploadable' | 'file_downloadable'
78
+): boolean => {
79
+ if (!schema) return false;
80
+ if (schema[property]) return true;
81
+
82
+ // Check nested properties
83
+ if (schema.properties) {
84
+ for (const prop of Object.values(schema.properties) as JSONSchemaProperty[]) {
85
+ if (schemaHasFileProperty(prop, property)) return true;
86
+ }
87
+ }
88
+
89
+ // Check anyOf/oneOf/allOf variants
90
+ if (schema.anyOf) {
91
+ for (const variant of schema.anyOf) {
92
+ if (schemaHasFileProperty(variant, property)) return true;
93
+ }
94
+ }
95
+ if (schema.oneOf) {
96
+ for (const variant of schema.oneOf) {
97
+ if (schemaHasFileProperty(variant, property)) return true;
98
+ }
99
+ }
100
+ if (schema.allOf) {
101
+ for (const variant of schema.allOf) {
102
+ if (schemaHasFileProperty(variant, property)) return true;
103
+ }
104
+ }
105
+
106
+ // Check array items
107
+ if (schema.items) {
108
+ if (Array.isArray(schema.items)) {
109
+ for (const item of schema.items) {
110
+ if (schemaHasFileProperty(item, property)) return true;
111
+ }
112
+ } else {
113
+ if (schemaHasFileProperty(schema.items, property)) return true;
114
+ }
115
+ }
116
+
117
+ return false;
118
+};
119
+
120
+/**
121
+ * Recursively checks if a schema (or any of its variants) contains file_uploadable properties.
122
+ */
123
+export const schemaHasFileUploadable = (schema: JSONSchemaProperty | undefined): boolean => {
124
+ return schemaHasFileProperty(schema, 'file_uploadable');
125
+};
126
+
127
+/**
128
+ * Recursively checks if a schema (or any of its variants) contains file_downloadable properties.
129
+ */
130
+export const schemaHasFileDownloadable = (schema: JSONSchemaProperty | undefined): boolean => {
131
+ return schemaHasFileProperty(schema, 'file_downloadable');
132
+};
ts/packages/core/src/utils/modifiers/FileToolModifier.workerd.ts
new
+49
@@ -0,0 +1,49 @@
1
+import { Tool, ToolExecuteParams, ToolExecuteResponse } from '../../types/tool.types';
2
+import ComposioClient from '@composio/client';
3
+import { transformProperties } from './FileToolModifier.utils.neutral';
4
+
5
+const UNSUPPORTED_MESSAGE =
6
+ 'File upload/download modifiers are not available on edge runtimes yet. ' +
7
+ 'Please set `autoUploadDownloadFiles: false` or run Composio in another JS runtime (Node.js / Bun).';
8
+
9
+export class FileToolModifier {
10
+ constructor(_client: ComposioClient) {}
11
+
12
+ async modifyToolSchema(toolSlug: string, toolkitSlug: string, schema: Tool): Promise<Tool> {
13
+ if (!schema.inputParameters?.properties) {
14
+ return schema;
15
+ }
16
+
17
+ const properties = transformProperties(schema.inputParameters.properties);
18
+
19
+ return {
20
+ ...schema,
21
+ inputParameters: {
22
+ ...schema.inputParameters,
23
+ properties,
24
+ },
25
+ };
26
+ }
27
+
28
+ async fileUploadModifier(
29
+ _tool: Tool,
30
+ _options: {
31
+ toolSlug: string;
32
+ toolkitSlug?: string;
33
+ params: ToolExecuteParams;
34
+ }
35
+ ): Promise<ToolExecuteParams> {
36
+ throw new Error(UNSUPPORTED_MESSAGE);
37
+ }
38
+
39
+ async fileDownloadModifier(
40
+ _tool: Tool,
41
+ _options: {
42
+ toolSlug: string;
43
+ toolkitSlug: string;
44
+ result: ToolExecuteResponse;
45
+ }
46
+ ): Promise<ToolExecuteResponse> {
47
+ throw new Error(UNSUPPORTED_MESSAGE);
48
+ }
49
+}
ts/packages/core/tsconfig.json
+5
-1
@@ -13,7 +13,11 @@
13
"resolveJsonModule": true,
14
"paths": {
15
"#platform": ["./src/platform/node.ts", "./src/platform/workerd.ts"],
16
- "#files": ["./src/models/Files.node.ts", "./src/models/Files.workerd.ts"]
16
+ "#files": ["./src/models/Files.node.ts", "./src/models/Files.workerd.ts"],
17
+ "#file_tool_modifier": [
18
+ "./src/utils/modifiers/FileToolModifier.node.ts",
19
+ "./src/utils/modifiers/FileToolModifier.workerd.ts"
20
+ ]
21
}
22
},
23
"include": ["src/**/*.ts"]
ts/packages/core/tsdown.config.ts
+9
-1
@@ -7,10 +7,18 @@ export default defineConfig({
7
copy: [{ from: 'pack/generated/*', to: '.', flatten: false }],
8
entry: [
9
'src/index.ts',
10
+
11
+ // #platform
12
'src/platform/node.ts',
13
'src/platform/workerd.ts',
14
+
15
+ // #files
16
'src/models/Files.node.ts',
17
'src/models/Files.workerd.ts',
18
+
19
+ // #file_tool_modifier
20
+ 'src/utils/modifiers/FileToolModifier.node.ts',
21
+ 'src/utils/modifiers/FileToolModifier.workerd.ts',
22
],
23
attw: {
24
...baseConfig.attw,
@@ -33,5 +41,5 @@ export default defineConfig({
41
* We don't want to accidentally bundle `node:*` packages (e.g., `node:module`)
42
* as not all of them are available in Cloudflare Workers / Vercel Edge runtimes.
43
*/
36
- external: [...(baseConfig.external ?? []), '#platform', '#files'],
44
+ external: [...(baseConfig.external ?? []), '#platform', '#files', '#file_tool_modifier'],
45
});
ts/packages/core/vitest.config.ts
+4
@@ -9,6 +9,10 @@ export default defineConfig({
9
alias: {
10
'#platform': path.resolve(__dirname, 'src/platform/node.ts'),
11
'#files': path.resolve(__dirname, 'src/models/Files.node.ts'),
12
+ '#file_tool_modifier': path.resolve(
13
+ __dirname,
14
+ 'src/utils/modifiers/FileToolModifier.node.ts'
15
+ ),
16
},
17
},
18
test: {