@composio/core 0.12.0 → 0.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/BaseProvider-Brb3o86-.d.mts +29425 -0
- package/dist/{CustomTool-DEB4BQPl.mjs → CustomTool-D9qwyMrv.mjs} +1 -1
- package/dist/{CustomTool-DH18rVQu.d.mts → CustomTool-Dmi6ti3I.d.mts} +1 -1
- package/dist/CustomTool-DrJpOhY2.d.mts +74 -0
- package/dist/{composio-CHnEggnv.d.mts → composio-DHIKRdeG.d.mts} +253 -37
- package/dist/composio-vMwhASRc.d.mts +5698 -0
- package/dist/{customTool.types-DpIdOJjL.d.mts → customTool.types-CKH2R2ia.d.mts} +344 -25
- package/dist/customTool.types-DXin7Cfc.d.mts +2773 -0
- package/dist/docs/error-consistency.md +97 -0
- package/dist/docs/reference/sdk-reference/typescript/auth-configs.mdx +306 -0
- package/dist/docs/reference/sdk-reference/typescript/composio.mdx +205 -0
- package/dist/docs/reference/sdk-reference/typescript/connected-accounts.mdx +488 -0
- package/dist/docs/reference/sdk-reference/typescript/experimental.mdx +94 -0
- package/dist/docs/reference/sdk-reference/typescript/index.mdx +67 -0
- package/dist/docs/reference/sdk-reference/typescript/mcp.mdx +304 -0
- package/dist/docs/reference/sdk-reference/typescript/meta.json +17 -0
- package/dist/docs/reference/sdk-reference/typescript/remote-file.mdx +107 -0
- package/dist/docs/reference/sdk-reference/typescript/session-files.mdx +163 -0
- package/dist/docs/reference/sdk-reference/typescript/session.mdx +229 -0
- package/dist/docs/reference/sdk-reference/typescript/sessions.mdx +131 -0
- package/dist/docs/reference/sdk-reference/typescript/toolkits.mdx +169 -0
- package/dist/docs/reference/sdk-reference/typescript/tools.mdx +453 -0
- package/dist/docs/reference/sdk-reference/typescript/triggers.mdx +434 -0
- package/dist/{errors-D5f4pfvI.mjs → errors-DSIlYfl5.mjs} +2 -2
- package/dist/experimental/index.d.mts +2 -2
- package/dist/experimental/index.mjs +1 -1
- package/dist/index.d.mts +27 -4
- package/dist/index.mjs +423 -123
- package/dist/{jsonSchema-daqF3S3d.mjs → jsonSchema-CFKdONpP.mjs} +1 -1
- package/dist/{types-CaI5Z4PA.d.mts → types-D4Id3wXA.d.mts} +1 -1
- package/dist/types-qTbYYBJw.d.mts +12 -0
- package/dist/utils/config-defaults/ConfigDefaults.node.d.mts +1 -1
- package/dist/utils/config-defaults/ConfigDefaults.workerd.d.mts +1 -1
- package/dist/utils/modifiers/FileToolModifier.node.mjs +2 -2
- package/docs/error-consistency.md +97 -0
- package/package.json +4 -3
- package/src/composio.ts +509 -0
- package/src/errors/AuthConfigErrors.ts +23 -0
- package/src/errors/ComposioError.ts +408 -0
- package/src/errors/ConnectedAccountsErrors.ts +158 -0
- package/src/errors/ConnectionRequestErrors.ts +32 -0
- package/src/errors/FileModifierErrors.ts +100 -0
- package/src/errors/PusherErrors.ts +28 -0
- package/src/errors/RemoteFileErrors.ts +62 -0
- package/src/errors/SDKErrors.ts +92 -0
- package/src/errors/ToolErrors.ts +244 -0
- package/src/errors/ToolkitErrors.ts +39 -0
- package/src/errors/TriggerErrors.ts +120 -0
- package/src/errors/ValidationErrors.ts +99 -0
- package/src/errors/index.ts +41 -0
- package/src/experimental/index.ts +28 -0
- package/src/index.ts +64 -0
- package/src/lib/toolRouterConstants.ts +1 -0
- package/src/lib/toolRouterParams.ts +306 -0
- package/src/models/AuthConfigs.ts +398 -0
- package/src/models/AuthScheme.ts +206 -0
- package/src/models/ConnectedAccounts.ts +735 -0
- package/src/models/ConnectionRequest.ts +150 -0
- package/src/models/CustomTool.ts +676 -0
- package/src/models/Experimental.ts +202 -0
- package/src/models/Files.node.ts +93 -0
- package/src/models/Files.workerd.ts +49 -0
- package/src/models/MCP.ts +478 -0
- package/src/models/RemoteFile.ts +172 -0
- package/src/models/SessionContext.ts +147 -0
- package/src/models/Sessions.ts +21 -0
- package/src/models/ToolRouter.ts +391 -0
- package/src/models/ToolRouterSession.ts +827 -0
- package/src/models/ToolRouterSessionFileMount.ts +389 -0
- package/src/models/Toolkits.ts +412 -0
- package/src/models/Tools.ts +1260 -0
- package/src/models/Triggers.ts +1381 -0
- package/src/models/customToolExecution.ts +118 -0
- package/src/models/inlineCustomToolsPayload.ts +17 -0
- package/src/models/proxyParamsTransform.ts +26 -0
- package/src/provider/BaseProvider.ts +138 -0
- package/src/provider/ComposioProvider.ts +29 -0
- package/src/provider/OpenAIProvider.ts +547 -0
- package/src/services/internal/InternalService.ts +50 -0
- package/src/services/internal/InternalService.types.ts +14 -0
- package/src/services/pusher/Pusher.ts +230 -0
- package/src/services/telemetry/TelemetryService.ts +48 -0
- package/src/services/telemetry/TelemetryService.types.ts +67 -0
- package/src/telemetry/BatchProcessor.ts +77 -0
- package/src/telemetry/README.md +123 -0
- package/src/telemetry/Telemetry.ts +356 -0
- package/src/types/ToolRouterSessionFilesMount.types.ts +96 -0
- package/src/types/authConfigs.types.ts +174 -0
- package/src/types/composio.types.ts +1 -0
- package/src/types/connectedAccountAuthStates.types.ts +852 -0
- package/src/types/connectedAccounts.types.ts +395 -0
- package/src/types/connectionRequest.types.ts +16 -0
- package/src/types/customTool.types.ts +261 -0
- package/src/types/files.types.ts +15 -0
- package/src/types/mcp.experimental.types.ts +163 -0
- package/src/types/mcp.types.ts +362 -0
- package/src/types/modifiers.types.ts +663 -0
- package/src/types/org.types.ts +0 -0
- package/src/types/provider.types.ts +41 -0
- package/src/types/pusher.types.ts +18 -0
- package/src/types/requestOptions.types.ts +62 -0
- package/src/types/telemetry.types.ts +89 -0
- package/src/types/tool.types.ts +408 -0
- package/src/types/toolRouter.types.ts +761 -0
- package/src/types/toolkit.types.ts +130 -0
- package/src/types/triggers.types.ts +401 -0
- package/src/types/webhookEvents.types.ts +219 -0
- package/src/utils/buffer.ts +30 -0
- package/src/utils/cancellation.ts +22 -0
- package/src/utils/config-defaults/ConfigDefaults.node.ts +7 -0
- package/src/utils/config-defaults/ConfigDefaults.workerd.ts +10 -0
- package/src/utils/config-defaults/index.ts +1 -0
- package/src/utils/config-defaults/types.ts +14 -0
- package/src/utils/constants.ts +22 -0
- package/src/utils/crypto.ts +34 -0
- package/src/utils/env.ts +34 -0
- package/src/utils/fileDirs.ts +81 -0
- package/src/utils/fileUtils.node.ts +458 -0
- package/src/utils/json-schema.ts +1 -0
- package/src/utils/jsonSchema.ts +355 -0
- package/src/utils/logger.ts +99 -0
- package/src/utils/mime.ts +75 -0
- package/src/utils/modifiers/FileToolModifier.node.ts +477 -0
- package/src/utils/modifiers/FileToolModifier.utils.neutral.ts +132 -0
- package/src/utils/modifiers/FileToolModifier.workerd.ts +51 -0
- package/src/utils/pagination.ts +73 -0
- package/src/utils/pusher.ts +286 -0
- package/src/utils/schemaPropertyKeys.ts +433 -0
- package/src/utils/sdk.ts +110 -0
- package/src/utils/sensitiveFileUploadPaths.node.ts +105 -0
- package/src/utils/session.ts +114 -0
- package/src/utils/toolArguments.ts +73 -0
- package/src/utils/toolkitVersion.ts +26 -0
- package/src/utils/transform.ts +42 -0
- package/src/utils/transformers/authConfigs.ts +107 -0
- package/src/utils/transformers/connectedAccounts.ts +119 -0
- package/src/utils/transformers/mcp.ts +164 -0
- package/src/utils/transformers/toolRouterResponseTransform.ts +160 -0
- package/src/utils/transformers/toolkits.ts +100 -0
- package/src/utils/transformers/triggers.ts +118 -0
- package/src/utils/uploadDirAllowlist.node.ts +178 -0
- package/src/utils/uuid.ts +7 -0
- package/src/utils/version.ts +58 -0
- package/src/utils/zodSchema.ts +90 -0
|
@@ -0,0 +1,477 @@
|
|
|
1
|
+
import {
|
|
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 {
|
|
10
|
+
ComposioFileNotFoundError,
|
|
11
|
+
ComposioFileUploadAbortedError,
|
|
12
|
+
ComposioFileUploadError,
|
|
13
|
+
ComposioFileUploadPathNotAllowedError,
|
|
14
|
+
ComposioSensitiveFilePathBlockedError,
|
|
15
|
+
} from '../../errors/FileModifierErrors';
|
|
16
|
+
import { ComposioRequestCancelledError } from '../../errors/SDKErrors';
|
|
17
|
+
import type { beforeFileUploadModifier } from '../../types/modifiers.types';
|
|
18
|
+
import {
|
|
19
|
+
downloadFileFromS3,
|
|
20
|
+
getFileDataAfterUploadingToS3,
|
|
21
|
+
type GetFileDataAfterUploadingToS3Options,
|
|
22
|
+
} from '../fileUtils.node';
|
|
23
|
+
import {
|
|
24
|
+
isPlainObject,
|
|
25
|
+
transformProperties,
|
|
26
|
+
schemaHasFileProperty,
|
|
27
|
+
} from './FileToolModifier.utils.neutral';
|
|
28
|
+
import { dereferenceJsonSchema } from '../jsonSchema';
|
|
29
|
+
import { withCancellation } from '../cancellation';
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Inlines internal `$ref` pointers so the file walkers below can see the
|
|
33
|
+
* `file_uploadable` / `file_downloadable` flags that live behind a
|
|
34
|
+
* `$ref`/`$defs` indirection (e.g. `GMAIL_GET_ATTACHMENT`). The walkers only
|
|
35
|
+
* recurse `properties`/`anyOf`/`oneOf`/`allOf`/`items` and never dereference,
|
|
36
|
+
* so without this a flagged field reachable only through a `$ref` is silently
|
|
37
|
+
* missed — the file is never staged/downloaded. `'sentinel'` mode keeps tools
|
|
38
|
+
* whose schema omits the `$defs` target working (see
|
|
39
|
+
* https://github.com/ComposioHQ/composio/issues/3307) instead of throwing.
|
|
40
|
+
*/
|
|
41
|
+
const resolveFileSchema = (
|
|
42
|
+
schema: JSONSchemaProperty | undefined
|
|
43
|
+
): JSONSchemaProperty | undefined =>
|
|
44
|
+
schema ? dereferenceJsonSchema(schema, { onUnresolved: 'sentinel' }) : schema;
|
|
45
|
+
|
|
46
|
+
const getSchemaVariants = (schema: JSONSchemaProperty | undefined): JSONSchemaProperty[] => [
|
|
47
|
+
...(schema?.anyOf ?? []),
|
|
48
|
+
...(schema?.oneOf ?? []),
|
|
49
|
+
...(schema?.allOf ?? []),
|
|
50
|
+
];
|
|
51
|
+
|
|
52
|
+
const jsonSchemaTypeMatchesValue = (schema: JSONSchemaProperty, value: unknown): boolean => {
|
|
53
|
+
const schemaType = schema.type;
|
|
54
|
+
|
|
55
|
+
if (Array.isArray(schemaType)) {
|
|
56
|
+
return schemaType.some(type => jsonSchemaTypeMatchesValue({ ...schema, type }, value));
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (schemaType === undefined) {
|
|
60
|
+
if (schema.properties) return isPlainObject(value);
|
|
61
|
+
if (schema.items) return Array.isArray(value);
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
switch (schemaType) {
|
|
66
|
+
case 'array':
|
|
67
|
+
return Array.isArray(value);
|
|
68
|
+
case 'object':
|
|
69
|
+
return isPlainObject(value);
|
|
70
|
+
case 'string':
|
|
71
|
+
return typeof value === 'string';
|
|
72
|
+
case 'integer':
|
|
73
|
+
return Number.isInteger(value);
|
|
74
|
+
case 'number':
|
|
75
|
+
return typeof value === 'number';
|
|
76
|
+
case 'boolean':
|
|
77
|
+
return typeof value === 'boolean';
|
|
78
|
+
case 'null':
|
|
79
|
+
return value === null;
|
|
80
|
+
default:
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
const findSchemaVariantWithFileProperty = (
|
|
86
|
+
schema: JSONSchemaProperty | undefined,
|
|
87
|
+
property: 'file_uploadable' | 'file_downloadable',
|
|
88
|
+
value: unknown
|
|
89
|
+
): JSONSchemaProperty | undefined => {
|
|
90
|
+
const candidates = getSchemaVariants(schema).filter(variant =>
|
|
91
|
+
schemaHasFileProperty(variant, property)
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
return (
|
|
95
|
+
candidates.find(candidate => jsonSchemaTypeMatchesValue(candidate, value)) ?? candidates[0]
|
|
96
|
+
);
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Recursively walks a runtime value and its matching JSON-Schema node,
|
|
101
|
+
* uploading any string path whose schema node has `file_uploadable: true`.
|
|
102
|
+
* The function returns a **new** value with all substitutions applied;
|
|
103
|
+
* nothing is mutated in-place.
|
|
104
|
+
*/
|
|
105
|
+
const hydrateFiles = async (
|
|
106
|
+
value: unknown,
|
|
107
|
+
schema: JSONSchemaProperty | undefined,
|
|
108
|
+
ctx: {
|
|
109
|
+
toolSlug: string;
|
|
110
|
+
toolkitSlug: string;
|
|
111
|
+
client: ComposioClient;
|
|
112
|
+
} & Pick<
|
|
113
|
+
GetFileDataAfterUploadingToS3Options,
|
|
114
|
+
| 'sensitiveFileUploadProtection'
|
|
115
|
+
| 'fileUploadPathDenySegments'
|
|
116
|
+
| 'fileUploadAllowlist'
|
|
117
|
+
| 'signal'
|
|
118
|
+
> & {
|
|
119
|
+
beforeFileUpload?: beforeFileUploadModifier;
|
|
120
|
+
}
|
|
121
|
+
): Promise<unknown> => {
|
|
122
|
+
// ──────────────────────────────────────────────────────────────────────────
|
|
123
|
+
// 1. Direct file upload
|
|
124
|
+
// ──────────────────────────────────────────────────────────────────────────
|
|
125
|
+
if (schema?.file_uploadable) {
|
|
126
|
+
// Upload only if the runtime value is a string (i.e., a local path) or blob
|
|
127
|
+
if (typeof value !== 'string' && !(value instanceof File)) return value;
|
|
128
|
+
|
|
129
|
+
const runBeforeFileUpload = async (
|
|
130
|
+
path: string,
|
|
131
|
+
source: 'path' | 'url' | 'file'
|
|
132
|
+
): Promise<string> => {
|
|
133
|
+
if (!ctx.beforeFileUpload) {
|
|
134
|
+
return path;
|
|
135
|
+
}
|
|
136
|
+
const out = await ctx.beforeFileUpload({
|
|
137
|
+
path,
|
|
138
|
+
source,
|
|
139
|
+
toolSlug: ctx.toolSlug,
|
|
140
|
+
toolkitSlug: ctx.toolkitSlug,
|
|
141
|
+
});
|
|
142
|
+
if (out === false) {
|
|
143
|
+
throw new ComposioFileUploadAbortedError(
|
|
144
|
+
'File upload was aborted because beforeFileUpload returned false.'
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
return out;
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
if (typeof value === 'string') {
|
|
151
|
+
// Match the URL/local-path split used downstream in
|
|
152
|
+
// getFileDataAfterUploadingToS3 so the hook sees the same categorisation.
|
|
153
|
+
const source = value.startsWith('http') ? 'url' : 'path';
|
|
154
|
+
const pathOrUrl = await runBeforeFileUpload(value, source);
|
|
155
|
+
logger.debug(`Uploading file "${pathOrUrl}"`);
|
|
156
|
+
return getFileDataAfterUploadingToS3(pathOrUrl, {
|
|
157
|
+
toolSlug: ctx.toolSlug,
|
|
158
|
+
toolkitSlug: ctx.toolkitSlug,
|
|
159
|
+
client: ctx.client,
|
|
160
|
+
sensitiveFileUploadProtection: ctx.sensitiveFileUploadProtection,
|
|
161
|
+
fileUploadPathDenySegments: ctx.fileUploadPathDenySegments,
|
|
162
|
+
fileUploadAllowlist: ctx.fileUploadAllowlist,
|
|
163
|
+
signal: ctx.signal,
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// File — `path` is the filename only; a string return replaces it with a
|
|
168
|
+
// local-path upload.
|
|
169
|
+
if (ctx.beforeFileUpload) {
|
|
170
|
+
const out = await ctx.beforeFileUpload({
|
|
171
|
+
path: value.name,
|
|
172
|
+
source: 'file',
|
|
173
|
+
toolSlug: ctx.toolSlug,
|
|
174
|
+
toolkitSlug: ctx.toolkitSlug,
|
|
175
|
+
});
|
|
176
|
+
if (out === false) {
|
|
177
|
+
throw new ComposioFileUploadAbortedError(
|
|
178
|
+
'File upload was aborted because beforeFileUpload returned false.'
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
if (typeof out === 'string' && out !== value.name) {
|
|
182
|
+
logger.debug(`Uploading file from path "${out}" (replaced File: ${value.name})`);
|
|
183
|
+
return getFileDataAfterUploadingToS3(out, {
|
|
184
|
+
toolSlug: ctx.toolSlug,
|
|
185
|
+
toolkitSlug: ctx.toolkitSlug,
|
|
186
|
+
client: ctx.client,
|
|
187
|
+
sensitiveFileUploadProtection: ctx.sensitiveFileUploadProtection,
|
|
188
|
+
fileUploadPathDenySegments: ctx.fileUploadPathDenySegments,
|
|
189
|
+
fileUploadAllowlist: ctx.fileUploadAllowlist,
|
|
190
|
+
signal: ctx.signal,
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
logger.debug(`Uploading file "${value.name}"`);
|
|
195
|
+
// File/Blob values are not subject to the upload-dir allowlist.
|
|
196
|
+
return getFileDataAfterUploadingToS3(value, {
|
|
197
|
+
toolSlug: ctx.toolSlug,
|
|
198
|
+
toolkitSlug: ctx.toolkitSlug,
|
|
199
|
+
client: ctx.client,
|
|
200
|
+
sensitiveFileUploadProtection: ctx.sensitiveFileUploadProtection,
|
|
201
|
+
fileUploadPathDenySegments: ctx.fileUploadPathDenySegments,
|
|
202
|
+
signal: ctx.signal,
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// ──────────────────────────────────────────────────────────────────────────
|
|
207
|
+
// 2. Handle anyOf/oneOf/allOf — pick the file-bearing variant whose
|
|
208
|
+
// JSON Schema shape matches the runtime value, then hydrate against it.
|
|
209
|
+
//
|
|
210
|
+
// We deliberately do NOT loop over every uploadable variant. With `oneOf`,
|
|
211
|
+
// exactly one variant should apply at runtime, and applying multiple would
|
|
212
|
+
// upload the same file once per variant — two presigned-URL round-trips and
|
|
213
|
+
// two S3 PUTs for a two-variant `oneOf`. If no runtime shape matches, fall
|
|
214
|
+
// back to the first file-bearing variant to preserve historical behavior.
|
|
215
|
+
// ──────────────────────────────────────────────────────────────────────────
|
|
216
|
+
const uploadableVariant = findSchemaVariantWithFileProperty(schema, 'file_uploadable', value);
|
|
217
|
+
if (uploadableVariant) {
|
|
218
|
+
return hydrateFiles(value, uploadableVariant, ctx);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// ──────────────────────────────────────────────────────────────────────────
|
|
222
|
+
// 3. Object → traverse each property
|
|
223
|
+
// ──────────────────────────────────────────────────────────────────────────
|
|
224
|
+
if (schema?.type === 'object' && schema.properties && isPlainObject(value)) {
|
|
225
|
+
const transformed: Record<string, unknown> = {};
|
|
226
|
+
|
|
227
|
+
for (const [k, v] of Object.entries(value)) {
|
|
228
|
+
transformed[k] = await hydrateFiles(v, schema.properties[k], ctx);
|
|
229
|
+
}
|
|
230
|
+
return transformed;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// ──────────────────────────────────────────────────────────────────────────
|
|
234
|
+
// 4. Array → traverse each item
|
|
235
|
+
// ──────────────────────────────────────────────────────────────────────────
|
|
236
|
+
if (schema?.type === 'array' && schema.items && Array.isArray(value)) {
|
|
237
|
+
// `items` can be a single schema or an array of schemas; we handle both.
|
|
238
|
+
const itemSchema = Array.isArray(schema.items) ? schema.items[0] : schema.items;
|
|
239
|
+
|
|
240
|
+
return Promise.all(
|
|
241
|
+
value.map(item => hydrateFiles(item, itemSchema as JSONSchemaProperty, ctx))
|
|
242
|
+
);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// ──────────────────────────────────────────────────────────────────────────
|
|
246
|
+
// 5. Primitive or schema-less branch → return unchanged
|
|
247
|
+
// ──────────────────────────────────────────────────────────────────────────
|
|
248
|
+
return value;
|
|
249
|
+
};
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Downloads a file from S3 and returns a replacement object.
|
|
253
|
+
*/
|
|
254
|
+
const downloadS3File = async (
|
|
255
|
+
value: Record<string, unknown>,
|
|
256
|
+
ctx: { toolSlug: string; fileDownloadDir?: string; signal?: AbortSignal }
|
|
257
|
+
): Promise<unknown> => {
|
|
258
|
+
const { s3url, mimetype } = value as {
|
|
259
|
+
s3url: string;
|
|
260
|
+
mimetype?: string;
|
|
261
|
+
};
|
|
262
|
+
|
|
263
|
+
try {
|
|
264
|
+
logger.debug(`Downloading from S3: ${s3url}`);
|
|
265
|
+
|
|
266
|
+
const dl = await downloadFileFromS3({
|
|
267
|
+
toolSlug: ctx.toolSlug,
|
|
268
|
+
s3Url: s3url,
|
|
269
|
+
mimeType: mimetype ?? 'application/octet-stream',
|
|
270
|
+
fileDownloadDir: ctx.fileDownloadDir,
|
|
271
|
+
signal: ctx.signal,
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
logger.debug(`Downloaded → ${dl.filePath}`);
|
|
275
|
+
|
|
276
|
+
return {
|
|
277
|
+
uri: dl.filePath,
|
|
278
|
+
file_downloaded: dl.filePath ? true : false,
|
|
279
|
+
s3url,
|
|
280
|
+
mimeType: dl.mimeType,
|
|
281
|
+
};
|
|
282
|
+
} catch (err) {
|
|
283
|
+
logger.error(`Download failed: ${s3url}`, { cause: err });
|
|
284
|
+
return {
|
|
285
|
+
uri: '',
|
|
286
|
+
file_downloaded: false,
|
|
287
|
+
s3url,
|
|
288
|
+
mimeType: mimetype ?? 'application/octet-stream',
|
|
289
|
+
};
|
|
290
|
+
}
|
|
291
|
+
};
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* Recursively walks an arbitrary value and its matching JSON-Schema node.
|
|
295
|
+
* Whenever it encounters an object that represents a file reference
|
|
296
|
+
* (i.e. has an `s3url`), it downloads the file and returns a replacement:
|
|
297
|
+
* {
|
|
298
|
+
* uri: "<local-path>",
|
|
299
|
+
* file_downloaded: true | false,
|
|
300
|
+
* s3url: "<original S3 URL>",
|
|
301
|
+
* mimeType: "<detected-or-fallback-mime-type>"
|
|
302
|
+
* }
|
|
303
|
+
*
|
|
304
|
+
* The function is side-effect-free: it never mutates the input value.
|
|
305
|
+
*/
|
|
306
|
+
const hydrateDownloads = async (
|
|
307
|
+
value: unknown,
|
|
308
|
+
schema: JSONSchemaProperty | undefined,
|
|
309
|
+
ctx: { toolSlug: string; fileDownloadDir?: string; signal?: AbortSignal }
|
|
310
|
+
): Promise<unknown> => {
|
|
311
|
+
// ──────────────────────────────────────────────────────────────────────────
|
|
312
|
+
// 1. Direct S3 reference (data-driven detection)
|
|
313
|
+
// ──────────────────────────────────────────────────────────────────────────
|
|
314
|
+
if (isPlainObject(value) && typeof value.s3url === 'string') {
|
|
315
|
+
return downloadS3File(value, ctx);
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
// ──────────────────────────────────────────────────────────────────────────
|
|
319
|
+
// 2. Schema-guided: Handle file_downloadable property
|
|
320
|
+
// ──────────────────────────────────────────────────────────────────────────
|
|
321
|
+
if (schema?.file_downloadable && isPlainObject(value) && typeof value.s3url === 'string') {
|
|
322
|
+
return downloadS3File(value, ctx);
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
// ──────────────────────────────────────────────────────────────────────────
|
|
326
|
+
// 3. Handle anyOf/oneOf/allOf - try each variant that may contain file_downloadable
|
|
327
|
+
// ──────────────────────────────────────────────────────────────────────────
|
|
328
|
+
const schemaVariants = getSchemaVariants(schema);
|
|
329
|
+
|
|
330
|
+
if (schemaVariants.length > 0) {
|
|
331
|
+
const downloadableVariant = findSchemaVariantWithFileProperty(
|
|
332
|
+
schema,
|
|
333
|
+
'file_downloadable',
|
|
334
|
+
value
|
|
335
|
+
);
|
|
336
|
+
if (downloadableVariant) {
|
|
337
|
+
return hydrateDownloads(value, downloadableVariant, ctx);
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
// If no downloadable variants found, still traverse the value for s3url objects
|
|
341
|
+
return hydrateDownloads(value, undefined, ctx);
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
// ──────────────────────────────────────────────────────────────────────────
|
|
345
|
+
// 4. Object → traverse each property
|
|
346
|
+
// ──────────────────────────────────────────────────────────────────────────
|
|
347
|
+
if (isPlainObject(value)) {
|
|
348
|
+
const pairs = await Promise.all(
|
|
349
|
+
Object.entries(value).map(async ([k, v]) => [
|
|
350
|
+
k,
|
|
351
|
+
await hydrateDownloads(v, schema?.properties?.[k], ctx),
|
|
352
|
+
])
|
|
353
|
+
);
|
|
354
|
+
return Object.fromEntries(pairs);
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
// ──────────────────────────────────────────────────────────────────────────
|
|
358
|
+
// 5. Array → traverse each item
|
|
359
|
+
// ──────────────────────────────────────────────────────────────────────────
|
|
360
|
+
if (Array.isArray(value)) {
|
|
361
|
+
const itemSchema = schema?.items
|
|
362
|
+
? Array.isArray(schema.items)
|
|
363
|
+
? schema.items[0]
|
|
364
|
+
: schema.items
|
|
365
|
+
: undefined;
|
|
366
|
+
|
|
367
|
+
return Promise.all(
|
|
368
|
+
value.map(item => hydrateDownloads(item, itemSchema as JSONSchemaProperty | undefined, ctx))
|
|
369
|
+
);
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
// ──────────────────────────────────────────────────────────────────────────
|
|
373
|
+
// 6. Primitive → return unchanged
|
|
374
|
+
// ──────────────────────────────────────────────────────────────────────────
|
|
375
|
+
return value;
|
|
376
|
+
};
|
|
377
|
+
|
|
378
|
+
export class FileToolModifier {
|
|
379
|
+
private client: ComposioClient;
|
|
380
|
+
private fileUploadPathOptions: Pick<
|
|
381
|
+
GetFileDataAfterUploadingToS3Options,
|
|
382
|
+
'sensitiveFileUploadProtection' | 'fileUploadPathDenySegments' | 'fileUploadAllowlist'
|
|
383
|
+
> & { beforeFileUpload?: beforeFileUploadModifier; fileDownloadDir?: string };
|
|
384
|
+
|
|
385
|
+
constructor(
|
|
386
|
+
client: ComposioClient,
|
|
387
|
+
fileUploadPathOptions: FileToolModifier['fileUploadPathOptions'] = {}
|
|
388
|
+
) {
|
|
389
|
+
this.client = client;
|
|
390
|
+
this.fileUploadPathOptions = fileUploadPathOptions;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
async modifyToolSchema(schema: Tool): Promise<Tool> {
|
|
394
|
+
if (!schema.inputParameters?.properties) {
|
|
395
|
+
return schema;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
const inputParameters = resolveFileSchema(schema.inputParameters)!;
|
|
399
|
+
const properties = transformProperties(inputParameters.properties!);
|
|
400
|
+
|
|
401
|
+
return {
|
|
402
|
+
...schema,
|
|
403
|
+
inputParameters: {
|
|
404
|
+
...inputParameters,
|
|
405
|
+
type: 'object',
|
|
406
|
+
properties,
|
|
407
|
+
},
|
|
408
|
+
};
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
async fileUploadModifier(
|
|
412
|
+
tool: Tool,
|
|
413
|
+
options: {
|
|
414
|
+
toolSlug: string;
|
|
415
|
+
toolkitSlug?: string;
|
|
416
|
+
params: ToolExecuteParams;
|
|
417
|
+
signal?: AbortSignal;
|
|
418
|
+
}
|
|
419
|
+
): Promise<ToolExecuteParams> {
|
|
420
|
+
const { params, toolSlug, toolkitSlug = 'unknown', signal } = options;
|
|
421
|
+
const { arguments: args } = params;
|
|
422
|
+
|
|
423
|
+
if (!args || typeof args !== 'object') return params;
|
|
424
|
+
|
|
425
|
+
try {
|
|
426
|
+
const newArgs = await withCancellation(
|
|
427
|
+
() =>
|
|
428
|
+
hydrateFiles(args, resolveFileSchema(tool.inputParameters), {
|
|
429
|
+
toolSlug,
|
|
430
|
+
toolkitSlug,
|
|
431
|
+
client: this.client,
|
|
432
|
+
...this.fileUploadPathOptions,
|
|
433
|
+
signal,
|
|
434
|
+
}),
|
|
435
|
+
signal
|
|
436
|
+
);
|
|
437
|
+
return { ...params, arguments: newArgs as ToolExecuteParams['arguments'] };
|
|
438
|
+
} catch (error) {
|
|
439
|
+
if (
|
|
440
|
+
error instanceof ComposioRequestCancelledError ||
|
|
441
|
+
error instanceof ComposioSensitiveFilePathBlockedError ||
|
|
442
|
+
error instanceof ComposioFileUploadAbortedError ||
|
|
443
|
+
error instanceof ComposioFileUploadPathNotAllowedError ||
|
|
444
|
+
error instanceof ComposioFileNotFoundError
|
|
445
|
+
) {
|
|
446
|
+
throw error;
|
|
447
|
+
}
|
|
448
|
+
throw new ComposioFileUploadError('Failed to upload file', {
|
|
449
|
+
cause: error,
|
|
450
|
+
});
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
async fileDownloadModifier(
|
|
455
|
+
tool: Tool,
|
|
456
|
+
options: {
|
|
457
|
+
toolSlug: string;
|
|
458
|
+
toolkitSlug: string;
|
|
459
|
+
result: ToolExecuteResponse;
|
|
460
|
+
signal?: AbortSignal;
|
|
461
|
+
}
|
|
462
|
+
): Promise<ToolExecuteResponse> {
|
|
463
|
+
const { result, toolSlug, signal } = options;
|
|
464
|
+
|
|
465
|
+
const dataWithDownloads = await hydrateDownloads(
|
|
466
|
+
result.data,
|
|
467
|
+
resolveFileSchema(tool.outputParameters),
|
|
468
|
+
{
|
|
469
|
+
toolSlug,
|
|
470
|
+
fileDownloadDir: this.fileUploadPathOptions.fileDownloadDir,
|
|
471
|
+
signal,
|
|
472
|
+
}
|
|
473
|
+
);
|
|
474
|
+
|
|
475
|
+
return { ...result, data: dataWithDownloads as typeof result.data };
|
|
476
|
+
}
|
|
477
|
+
}
|
|
@@ -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
|
+
};
|
|
@@ -0,0 +1,51 @@
|
|
|
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 `dangerouslyAllowAutoUploadDownloadFiles: false` (or unset it; it defaults to false) or run Composio in another JS runtime (Node.js / Bun).';
|
|
8
|
+
|
|
9
|
+
export class FileToolModifier {
|
|
10
|
+
constructor(_client: ComposioClient) {}
|
|
11
|
+
|
|
12
|
+
async modifyToolSchema(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
|
+
signal?: AbortSignal;
|
|
35
|
+
}
|
|
36
|
+
): Promise<ToolExecuteParams> {
|
|
37
|
+
throw new Error(UNSUPPORTED_MESSAGE);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async fileDownloadModifier(
|
|
41
|
+
_tool: Tool,
|
|
42
|
+
_options: {
|
|
43
|
+
toolSlug: string;
|
|
44
|
+
toolkitSlug: string;
|
|
45
|
+
result: ToolExecuteResponse;
|
|
46
|
+
signal?: AbortSignal;
|
|
47
|
+
}
|
|
48
|
+
): Promise<ToolExecuteResponse> {
|
|
49
|
+
throw new Error(UNSUPPORTED_MESSAGE);
|
|
50
|
+
}
|
|
51
|
+
}
|