@composio/core 0.17.0 → 0.18.1
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/{CustomTool-l-j5HtEf.mjs → CustomTool-Bz0f52QR.mjs} +16 -6
- package/dist/{CustomTool-Uip-fhPw.d.mts → CustomTool-VRfWzMix.d.mts} +1 -1
- package/dist/{FileModifierErrors-B8V8_3VE.mjs → FileModifierErrors-CwB-Inm9.mjs} +1 -1
- package/dist/FileToolModifier.utils.neutral-D_YkHwZP.mjs +188 -0
- package/dist/{SsrfErrors-DNJt57GL.mjs → SsrfErrors-xhdVE4yw.mjs} +123 -3
- package/dist/{Telemetry-DuGJSwUt.mjs → Telemetry-Dw9W5RSp.mjs} +28 -44
- package/dist/cancellation-P5YUlcfz.mjs +704 -0
- package/dist/{composio-B27X6i6A.d.mts → composio-Cb3d1jFe.d.mts} +250 -244
- package/dist/{customTool.types-B2Ava_j5.d.mts → customTool.types-B6ej_8XZ.d.mts} +146 -144
- package/dist/docs/reference/sdk-reference/typescript/session-files.mdx +1 -1
- package/dist/docs/reference/sdk-reference/typescript/session.mdx +4 -3
- package/dist/{errors-BcEUHZbq.mjs → errors-Ue9wMrPK.mjs} +5 -2
- package/dist/experimental/index.d.mts +2 -2
- package/dist/experimental/index.mjs +1 -1
- package/dist/{fileUtils.node-B1Wn848p.mjs → fileUtils.node-KE5Fa-af.mjs} +7 -7
- package/dist/index.d.mts +108 -8
- package/dist/index.mjs +109 -55
- package/dist/models/Files.node.mjs +2 -2
- package/dist/{sensitiveFileUploadPaths-BVEt0peg.mjs → sensitiveFileUploadPaths-QN7w4n8v.mjs} +24 -7
- package/dist/ssrfGuard.node-BLX6b-Rf.mjs +384 -0
- package/dist/{types-DdZJuMm2.d.mts → types-CGdgK442.d.mts} +2 -2
- 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.d.mts +1 -1
- package/dist/utils/modifiers/FileToolModifier.node.mjs +59 -103
- package/dist/utils/modifiers/FileToolModifier.workerd.d.mts +1 -1
- package/dist/utils/modifiers/FileToolModifier.workerd.mjs +1 -1
- package/dist/utils/ssrfGuard.node.d.mts +14 -5
- package/dist/utils/ssrfGuard.node.mjs +1 -161
- package/dist/utils/ssrfGuard.workerd.d.mts +1 -1
- package/dist/utils/ssrfGuard.workerd.mjs +2 -2
- package/package.json +25 -3
- package/src/errors/ToolErrors.ts +1 -0
- package/src/errors/ValidationErrors.ts +2 -0
- package/src/index.ts +6 -0
- package/src/models/CustomTool.ts +41 -6
- package/src/models/RemoteFile.ts +54 -36
- package/src/models/ToolRouterSession.ts +45 -12
- package/src/models/Tools.ts +102 -39
- package/src/models/Triggers.ts +15 -0
- package/src/services/telemetry/TelemetryService.ts +27 -12
- package/src/telemetry/redact.ts +133 -6
- package/src/utils/fileUtils.node.ts +10 -2
- package/src/utils/jsonSchema.ts +505 -27
- package/src/utils/logger.ts +6 -6
- package/src/utils/modifiers/FileToolModifier.node.ts +85 -181
- package/src/utils/modifiers/FileToolModifier.utils.neutral.ts +199 -0
- package/src/utils/pinnedDispatcher.node.ts +214 -0
- package/src/utils/sdk.ts +1 -1
- package/src/utils/sensitiveFileUploadPaths.ts +36 -9
- package/src/utils/ssrfGuard.node.ts +101 -11
- package/src/utils/ssrfGuard.workerd.ts +5 -1
- package/dist/FileToolModifier.utils.neutral-B5rT711M.mjs +0 -72
- package/dist/cancellation-NVtGf3St.mjs +0 -364
|
@@ -22,9 +22,13 @@ import {
|
|
|
22
22
|
type GetFileDataAfterUploadingToS3Options,
|
|
23
23
|
} from '../fileUtils.node';
|
|
24
24
|
import {
|
|
25
|
+
DELETE_VALUE,
|
|
26
|
+
findSchemaVariantWithFileProperty,
|
|
27
|
+
getSchemaVariants,
|
|
28
|
+
isEmptyFileValue,
|
|
25
29
|
isPlainObject,
|
|
26
30
|
transformProperties,
|
|
27
|
-
|
|
31
|
+
walkFileUploadableLeaves,
|
|
28
32
|
} from './FileToolModifier.utils.neutral';
|
|
29
33
|
import { dereferenceJsonSchema } from '../jsonSchema';
|
|
30
34
|
import { withCancellation } from '../cancellation';
|
|
@@ -44,68 +48,14 @@ const resolveFileSchema = <Schema extends JSONSchemaProperty>(
|
|
|
44
48
|
): Schema | undefined =>
|
|
45
49
|
schema ? dereferenceJsonSchema(schema, { onUnresolved: 'sentinel' }) : schema;
|
|
46
50
|
|
|
47
|
-
const getSchemaVariants = (schema: JSONSchemaProperty | undefined): JSONSchemaProperty[] => [
|
|
48
|
-
...(schema?.anyOf ?? []),
|
|
49
|
-
...(schema?.oneOf ?? []),
|
|
50
|
-
...(schema?.allOf ?? []),
|
|
51
|
-
];
|
|
52
|
-
|
|
53
|
-
const jsonSchemaTypeMatchesValue = (schema: JSONSchemaProperty, value: unknown): boolean => {
|
|
54
|
-
const schemaType = schema.type;
|
|
55
|
-
|
|
56
|
-
if (Array.isArray(schemaType)) {
|
|
57
|
-
return schemaType.some(type => jsonSchemaTypeMatchesValue({ ...schema, type }, value));
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
if (schemaType === undefined) {
|
|
61
|
-
if (schema.properties) return isPlainObject(value);
|
|
62
|
-
if (schema.items) return Array.isArray(value);
|
|
63
|
-
return false;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
switch (schemaType) {
|
|
67
|
-
case 'array':
|
|
68
|
-
return Array.isArray(value);
|
|
69
|
-
case 'object':
|
|
70
|
-
return isPlainObject(value);
|
|
71
|
-
case 'string':
|
|
72
|
-
return typeof value === 'string';
|
|
73
|
-
case 'integer':
|
|
74
|
-
return Number.isInteger(value);
|
|
75
|
-
case 'number':
|
|
76
|
-
return typeof value === 'number';
|
|
77
|
-
case 'boolean':
|
|
78
|
-
return typeof value === 'boolean';
|
|
79
|
-
case 'null':
|
|
80
|
-
return value === null;
|
|
81
|
-
default:
|
|
82
|
-
return false;
|
|
83
|
-
}
|
|
84
|
-
};
|
|
85
|
-
|
|
86
|
-
const findSchemaVariantWithFileProperty = (
|
|
87
|
-
schema: JSONSchemaProperty | undefined,
|
|
88
|
-
property: 'file_uploadable' | 'file_downloadable',
|
|
89
|
-
value: unknown
|
|
90
|
-
): JSONSchemaProperty | undefined => {
|
|
91
|
-
const candidates = getSchemaVariants(schema).filter(variant =>
|
|
92
|
-
schemaHasFileProperty(variant, property)
|
|
93
|
-
);
|
|
94
|
-
|
|
95
|
-
return (
|
|
96
|
-
candidates.find(candidate => jsonSchemaTypeMatchesValue(candidate, value)) ?? candidates[0]
|
|
97
|
-
);
|
|
98
|
-
};
|
|
99
|
-
|
|
100
51
|
/**
|
|
101
|
-
*
|
|
102
|
-
*
|
|
103
|
-
*
|
|
104
|
-
*
|
|
52
|
+
* Stages one `file_uploadable` leaf: a local path / URL string or a `File`
|
|
53
|
+
* is uploaded to S3 and replaced by its `{ name, mimetype, s3key }`
|
|
54
|
+
* descriptor; `''` means "no file" and is dropped from the request;
|
|
55
|
+
* anything else (e.g. an already-staged descriptor) passes through.
|
|
105
56
|
*/
|
|
106
|
-
const
|
|
57
|
+
const stageFileValue = async (
|
|
107
58
|
value: unknown,
|
|
108
|
-
schema: JSONSchemaProperty | undefined,
|
|
109
59
|
ctx: {
|
|
110
60
|
toolSlug: string;
|
|
111
61
|
toolkitSlug: string;
|
|
@@ -120,133 +70,85 @@ const hydrateFiles = async (
|
|
|
120
70
|
beforeFileUpload?: beforeFileUploadModifier;
|
|
121
71
|
}
|
|
122
72
|
): Promise<unknown> => {
|
|
123
|
-
|
|
124
|
-
//
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
): Promise<string> => {
|
|
134
|
-
if (!ctx.beforeFileUpload) {
|
|
135
|
-
return path;
|
|
136
|
-
}
|
|
137
|
-
const out = await ctx.beforeFileUpload({
|
|
138
|
-
path,
|
|
139
|
-
source,
|
|
140
|
-
toolSlug: ctx.toolSlug,
|
|
141
|
-
toolkitSlug: ctx.toolkitSlug,
|
|
142
|
-
});
|
|
143
|
-
if (out === false) {
|
|
144
|
-
throw new ComposioFileUploadAbortedError(
|
|
145
|
-
'File upload was aborted because beforeFileUpload returned false.'
|
|
146
|
-
);
|
|
147
|
-
}
|
|
148
|
-
return out;
|
|
149
|
-
};
|
|
150
|
-
|
|
151
|
-
if (typeof value === 'string') {
|
|
152
|
-
// Match the URL/local-path split used downstream in
|
|
153
|
-
// getFileDataAfterUploadingToS3 so the hook sees the same categorisation.
|
|
154
|
-
const source = isHttpUrl(value) ? 'url' : 'path';
|
|
155
|
-
const pathOrUrl = await runBeforeFileUpload(value, source);
|
|
156
|
-
logger.debug(`Uploading file "${pathOrUrl}"`);
|
|
157
|
-
return getFileDataAfterUploadingToS3(pathOrUrl, {
|
|
158
|
-
toolSlug: ctx.toolSlug,
|
|
159
|
-
toolkitSlug: ctx.toolkitSlug,
|
|
160
|
-
client: ctx.client,
|
|
161
|
-
sensitiveFileUploadProtection: ctx.sensitiveFileUploadProtection,
|
|
162
|
-
fileUploadPathDenySegments: ctx.fileUploadPathDenySegments,
|
|
163
|
-
fileUploadAllowlist: ctx.fileUploadAllowlist,
|
|
164
|
-
signal: ctx.signal,
|
|
165
|
-
});
|
|
73
|
+
if (isEmptyFileValue(value)) return DELETE_VALUE;
|
|
74
|
+
// Upload only if the runtime value is a string (i.e., a local path) or blob
|
|
75
|
+
if (typeof value !== 'string' && !(value instanceof File)) return value;
|
|
76
|
+
|
|
77
|
+
const runBeforeFileUpload = async (
|
|
78
|
+
path: string,
|
|
79
|
+
source: 'path' | 'url' | 'file'
|
|
80
|
+
): Promise<string> => {
|
|
81
|
+
if (!ctx.beforeFileUpload) {
|
|
82
|
+
return path;
|
|
166
83
|
}
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
if (out === false) {
|
|
178
|
-
throw new ComposioFileUploadAbortedError(
|
|
179
|
-
'File upload was aborted because beforeFileUpload returned false.'
|
|
180
|
-
);
|
|
181
|
-
}
|
|
182
|
-
if (typeof out === 'string' && out !== value.name) {
|
|
183
|
-
logger.debug(`Uploading file from path "${out}" (replaced File: ${value.name})`);
|
|
184
|
-
return getFileDataAfterUploadingToS3(out, {
|
|
185
|
-
toolSlug: ctx.toolSlug,
|
|
186
|
-
toolkitSlug: ctx.toolkitSlug,
|
|
187
|
-
client: ctx.client,
|
|
188
|
-
sensitiveFileUploadProtection: ctx.sensitiveFileUploadProtection,
|
|
189
|
-
fileUploadPathDenySegments: ctx.fileUploadPathDenySegments,
|
|
190
|
-
fileUploadAllowlist: ctx.fileUploadAllowlist,
|
|
191
|
-
signal: ctx.signal,
|
|
192
|
-
});
|
|
193
|
-
}
|
|
84
|
+
const out = await ctx.beforeFileUpload({
|
|
85
|
+
path,
|
|
86
|
+
source,
|
|
87
|
+
toolSlug: ctx.toolSlug,
|
|
88
|
+
toolkitSlug: ctx.toolkitSlug,
|
|
89
|
+
});
|
|
90
|
+
if (out === false) {
|
|
91
|
+
throw new ComposioFileUploadAbortedError(
|
|
92
|
+
'File upload was aborted because beforeFileUpload returned false.'
|
|
93
|
+
);
|
|
194
94
|
}
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
95
|
+
return out;
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
if (typeof value === 'string') {
|
|
99
|
+
// Match the URL/local-path split used downstream in
|
|
100
|
+
// getFileDataAfterUploadingToS3 so the hook sees the same categorisation.
|
|
101
|
+
const source = isHttpUrl(value) ? 'url' : 'path';
|
|
102
|
+
const pathOrUrl = await runBeforeFileUpload(value, source);
|
|
103
|
+
logger.debug(`Uploading file "${pathOrUrl}"`);
|
|
104
|
+
return getFileDataAfterUploadingToS3(pathOrUrl, {
|
|
198
105
|
toolSlug: ctx.toolSlug,
|
|
199
106
|
toolkitSlug: ctx.toolkitSlug,
|
|
200
107
|
client: ctx.client,
|
|
201
108
|
sensitiveFileUploadProtection: ctx.sensitiveFileUploadProtection,
|
|
202
109
|
fileUploadPathDenySegments: ctx.fileUploadPathDenySegments,
|
|
110
|
+
fileUploadAllowlist: ctx.fileUploadAllowlist,
|
|
203
111
|
signal: ctx.signal,
|
|
204
112
|
});
|
|
205
113
|
}
|
|
206
114
|
|
|
207
|
-
//
|
|
208
|
-
//
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
115
|
+
// File — `path` is the filename only; a string return replaces it with a
|
|
116
|
+
// local-path upload.
|
|
117
|
+
if (ctx.beforeFileUpload) {
|
|
118
|
+
const out = await ctx.beforeFileUpload({
|
|
119
|
+
path: value.name,
|
|
120
|
+
source: 'file',
|
|
121
|
+
toolSlug: ctx.toolSlug,
|
|
122
|
+
toolkitSlug: ctx.toolkitSlug,
|
|
123
|
+
});
|
|
124
|
+
if (out === false) {
|
|
125
|
+
throw new ComposioFileUploadAbortedError(
|
|
126
|
+
'File upload was aborted because beforeFileUpload returned false.'
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
if (typeof out === 'string' && out !== value.name) {
|
|
130
|
+
logger.debug(`Uploading file from path "${out}" (replaced File: ${value.name})`);
|
|
131
|
+
return getFileDataAfterUploadingToS3(out, {
|
|
132
|
+
toolSlug: ctx.toolSlug,
|
|
133
|
+
toolkitSlug: ctx.toolkitSlug,
|
|
134
|
+
client: ctx.client,
|
|
135
|
+
sensitiveFileUploadProtection: ctx.sensitiveFileUploadProtection,
|
|
136
|
+
fileUploadPathDenySegments: ctx.fileUploadPathDenySegments,
|
|
137
|
+
fileUploadAllowlist: ctx.fileUploadAllowlist,
|
|
138
|
+
signal: ctx.signal,
|
|
139
|
+
});
|
|
230
140
|
}
|
|
231
|
-
return transformed;
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
// ──────────────────────────────────────────────────────────────────────────
|
|
235
|
-
// 4. Array → traverse each item
|
|
236
|
-
// ──────────────────────────────────────────────────────────────────────────
|
|
237
|
-
if (schema?.type === 'array' && schema.items && Array.isArray(value)) {
|
|
238
|
-
// `items` can be a single schema or an array of schemas; we handle both.
|
|
239
|
-
const itemSchema = Array.isArray(schema.items) ? schema.items[0] : schema.items;
|
|
240
|
-
|
|
241
|
-
return Promise.all(
|
|
242
|
-
value.map(item => hydrateFiles(item, itemSchema as JSONSchemaProperty, ctx))
|
|
243
|
-
);
|
|
244
141
|
}
|
|
245
|
-
|
|
246
|
-
//
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
142
|
+
logger.debug(`Uploading file "${value.name}"`);
|
|
143
|
+
// File/Blob values are not subject to the upload-dir allowlist.
|
|
144
|
+
return getFileDataAfterUploadingToS3(value, {
|
|
145
|
+
toolSlug: ctx.toolSlug,
|
|
146
|
+
toolkitSlug: ctx.toolkitSlug,
|
|
147
|
+
client: ctx.client,
|
|
148
|
+
sensitiveFileUploadProtection: ctx.sensitiveFileUploadProtection,
|
|
149
|
+
fileUploadPathDenySegments: ctx.fileUploadPathDenySegments,
|
|
150
|
+
signal: ctx.signal,
|
|
151
|
+
});
|
|
250
152
|
};
|
|
251
153
|
|
|
252
154
|
/**
|
|
@@ -426,13 +328,15 @@ export class FileToolModifier {
|
|
|
426
328
|
try {
|
|
427
329
|
const newArgs = await withCancellation(
|
|
428
330
|
() =>
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
331
|
+
walkFileUploadableLeaves(args, resolveFileSchema(tool.inputParameters), value =>
|
|
332
|
+
stageFileValue(value, {
|
|
333
|
+
toolSlug,
|
|
334
|
+
toolkitSlug,
|
|
335
|
+
client: this.client,
|
|
336
|
+
...this.fileUploadPathOptions,
|
|
337
|
+
signal,
|
|
338
|
+
})
|
|
339
|
+
),
|
|
436
340
|
signal
|
|
437
341
|
);
|
|
438
342
|
return { ...params, arguments: newArgs as ToolExecuteParams['arguments'] };
|
|
@@ -116,6 +116,20 @@ export const schemaHasFileProperty = (
|
|
|
116
116
|
}
|
|
117
117
|
}
|
|
118
118
|
|
|
119
|
+
// Check `$ref` targets. Composio toolkits routinely express file flags
|
|
120
|
+
// through a `$ref`/`$defs` indirection (e.g. GMAIL_GET_ATTACHMENT), and
|
|
121
|
+
// this predicate is the cheap gate that decides whether a schema is worth
|
|
122
|
+
// dereferencing at all — without this it answers `false` for every
|
|
123
|
+
// ref-based schema. Callers walk the *dereferenced* schema, where
|
|
124
|
+
// `dereferenceJsonSchema` has already stripped these blocks, so an unused
|
|
125
|
+
// `$defs` entry can only cost a redundant walk, never a wrong upload.
|
|
126
|
+
for (const definitions of [schema.$defs, schema.definitions]) {
|
|
127
|
+
if (!definitions) continue;
|
|
128
|
+
for (const definition of Object.values(definitions)) {
|
|
129
|
+
if (schemaHasFileProperty(definition, property)) return true;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
119
133
|
return false;
|
|
120
134
|
};
|
|
121
135
|
|
|
@@ -132,3 +146,188 @@ export const schemaHasFileUploadable = (schema: JSONSchemaProperty | undefined):
|
|
|
132
146
|
export const schemaHasFileDownloadable = (schema: JSONSchemaProperty | undefined): boolean => {
|
|
133
147
|
return schemaHasFileProperty(schema, 'file_downloadable');
|
|
134
148
|
};
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Sentinel a leaf handler returns to have {@link walkFileUploadableLeaves}
|
|
152
|
+
* omit that key/item from its parent container. `''` and `null` are legal
|
|
153
|
+
* payload values, so neither can double as "remove this".
|
|
154
|
+
*/
|
|
155
|
+
export const DELETE_VALUE: unique symbol = Symbol('composio.delete-value');
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* `''` at a `file_uploadable` leaf means "no file": it is never a staged
|
|
159
|
+
* `{ name, mimetype, s3key }` descriptor, so the backend rejects it with a
|
|
160
|
+
* validation error, while callers (and the playground UI) mean "absent". The
|
|
161
|
+
* walker omits the key instead. `null` is left alone — schemas with a `null`
|
|
162
|
+
* variant accept it, and the backend reports the mismatch otherwise.
|
|
163
|
+
*/
|
|
164
|
+
export const isEmptyFileValue = (value: unknown): boolean => value === '';
|
|
165
|
+
|
|
166
|
+
export const getSchemaVariants = (schema: JSONSchemaProperty | undefined): JSONSchemaProperty[] => [
|
|
167
|
+
...(schema?.anyOf ?? []),
|
|
168
|
+
...(schema?.oneOf ?? []),
|
|
169
|
+
...(schema?.allOf ?? []),
|
|
170
|
+
];
|
|
171
|
+
|
|
172
|
+
/** Best-effort runtime shape match for selecting composed schema variants. */
|
|
173
|
+
export const jsonSchemaTypeMatchesValue = (schema: JSONSchemaProperty, value: unknown): boolean => {
|
|
174
|
+
const schemaType = schema.type;
|
|
175
|
+
|
|
176
|
+
if (Array.isArray(schemaType)) {
|
|
177
|
+
return schemaType.some(type => jsonSchemaTypeMatchesValue({ ...schema, type }, value));
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
if (schemaType === undefined) {
|
|
181
|
+
if (schema.properties) return isPlainObject(value);
|
|
182
|
+
if (schema.items) return Array.isArray(value);
|
|
183
|
+
return false;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
switch (schemaType) {
|
|
187
|
+
case 'array':
|
|
188
|
+
return Array.isArray(value);
|
|
189
|
+
case 'object':
|
|
190
|
+
return isPlainObject(value);
|
|
191
|
+
case 'string':
|
|
192
|
+
return typeof value === 'string';
|
|
193
|
+
case 'integer':
|
|
194
|
+
return Number.isInteger(value);
|
|
195
|
+
case 'number':
|
|
196
|
+
return typeof value === 'number';
|
|
197
|
+
case 'boolean':
|
|
198
|
+
return typeof value === 'boolean';
|
|
199
|
+
case 'null':
|
|
200
|
+
return value === null;
|
|
201
|
+
default:
|
|
202
|
+
return false;
|
|
203
|
+
}
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
/** Mirrors the array-shape inference used by {@link jsonSchemaTypeMatchesValue}. */
|
|
207
|
+
const isArrayShapedSchema = (schema: JSONSchemaProperty | undefined): boolean => {
|
|
208
|
+
if (!schema) return false;
|
|
209
|
+
if (Array.isArray(schema.type)) return schema.type.includes('array');
|
|
210
|
+
return schema.type === 'array' || (schema.type === undefined && schema.items !== undefined);
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
/** Mirrors the object-shape inference used by {@link jsonSchemaTypeMatchesValue}. */
|
|
214
|
+
const isObjectShapedSchema = (schema: JSONSchemaProperty | undefined): boolean => {
|
|
215
|
+
if (!schema) return false;
|
|
216
|
+
if (Array.isArray(schema.type)) return schema.type.includes('object');
|
|
217
|
+
return schema.type === 'object' || (schema.type === undefined && schema.properties !== undefined);
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Picks the file-bearing `anyOf`/`oneOf`/`allOf` variant whose JSON Schema
|
|
222
|
+
* shape matches the runtime value, falling back to the first file-bearing
|
|
223
|
+
* variant to preserve historical behavior.
|
|
224
|
+
*/
|
|
225
|
+
export const findSchemaVariantWithFileProperty = (
|
|
226
|
+
schema: JSONSchemaProperty | undefined,
|
|
227
|
+
property: 'file_uploadable' | 'file_downloadable',
|
|
228
|
+
value: unknown
|
|
229
|
+
): JSONSchemaProperty | undefined => {
|
|
230
|
+
const candidates = getSchemaVariants(schema).filter(variant =>
|
|
231
|
+
schemaHasFileProperty(variant, property)
|
|
232
|
+
);
|
|
233
|
+
|
|
234
|
+
return (
|
|
235
|
+
candidates.find(candidate => jsonSchemaTypeMatchesValue(candidate, value)) ?? candidates[0]
|
|
236
|
+
);
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Recursively walks a runtime value alongside its (already dereferenced)
|
|
241
|
+
* JSON-Schema node and passes every value sitting at a `file_uploadable`
|
|
242
|
+
* node through `leaf`. A leaf returning {@link DELETE_VALUE} is omitted from
|
|
243
|
+
* its parent object/array. Returns a **new** value; nothing is mutated.
|
|
244
|
+
*
|
|
245
|
+
* Composed schemas are resolved to the single file-bearing variant whose
|
|
246
|
+
* shape matches the value: with `oneOf`, exactly one variant should apply at
|
|
247
|
+
* runtime, and applying several would stage the same file once per variant.
|
|
248
|
+
*/
|
|
249
|
+
export const walkFileUploadableLeaves = async (
|
|
250
|
+
value: unknown,
|
|
251
|
+
schema: JSONSchemaProperty | undefined,
|
|
252
|
+
leaf: (value: unknown) => Promise<unknown>
|
|
253
|
+
): Promise<unknown> => {
|
|
254
|
+
if (schema?.file_uploadable) {
|
|
255
|
+
return leaf(value);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
const uploadableVariant = findSchemaVariantWithFileProperty(schema, 'file_uploadable', value);
|
|
259
|
+
if (uploadableVariant) {
|
|
260
|
+
// A scalar empty string cannot match an array-only file input, but callers
|
|
261
|
+
// use it to mean "no files". Route it through the leaf handler so it is
|
|
262
|
+
// omitted, unless another non-file variant explicitly accepts strings.
|
|
263
|
+
const matchingNonFileVariant = getSchemaVariants(schema).some(
|
|
264
|
+
variant =>
|
|
265
|
+
!schemaHasFileProperty(variant, 'file_uploadable') &&
|
|
266
|
+
jsonSchemaTypeMatchesValue(variant, value)
|
|
267
|
+
);
|
|
268
|
+
if (
|
|
269
|
+
isEmptyFileValue(value) &&
|
|
270
|
+
isArrayShapedSchema(uploadableVariant) &&
|
|
271
|
+
!matchingNonFileVariant
|
|
272
|
+
) {
|
|
273
|
+
return leaf(value);
|
|
274
|
+
}
|
|
275
|
+
return walkFileUploadableLeaves(value, uploadableVariant, leaf);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
if (
|
|
279
|
+
isEmptyFileValue(value) &&
|
|
280
|
+
isArrayShapedSchema(schema) &&
|
|
281
|
+
schemaHasFileProperty(schema, 'file_uploadable')
|
|
282
|
+
) {
|
|
283
|
+
return leaf(value);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
if (isObjectShapedSchema(schema) && schema?.properties && isPlainObject(value)) {
|
|
287
|
+
const transformed: Record<string, unknown> = {};
|
|
288
|
+
for (const [k, v] of Object.entries(value)) {
|
|
289
|
+
const propertySchema = Object.hasOwn(schema.properties, k) ? schema.properties[k] : undefined;
|
|
290
|
+
const walked = await walkFileUploadableLeaves(v, propertySchema, leaf);
|
|
291
|
+
if (walked !== DELETE_VALUE) {
|
|
292
|
+
// Define every key as an own data property so reserved JSON keys such
|
|
293
|
+
// as `__proto__` remain enumerable without invoking its inherited
|
|
294
|
+
// setter and changing this ordinary object's prototype.
|
|
295
|
+
Object.defineProperty(transformed, k, {
|
|
296
|
+
configurable: true,
|
|
297
|
+
enumerable: true,
|
|
298
|
+
value: walked,
|
|
299
|
+
writable: true,
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
return transformed;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
if (isArrayShapedSchema(schema) && schema?.items && Array.isArray(value)) {
|
|
307
|
+
// `items` can be a single schema or an array of schemas; we handle both.
|
|
308
|
+
const itemSchema = Array.isArray(schema.items) ? schema.items[0] : schema.items;
|
|
309
|
+
const walked = await Promise.all(
|
|
310
|
+
value.map(item => walkFileUploadableLeaves(item, itemSchema as JSONSchemaProperty, leaf))
|
|
311
|
+
);
|
|
312
|
+
return walked.filter(item => item !== DELETE_VALUE);
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
return value;
|
|
316
|
+
};
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* Omits `''` at `file_uploadable` leaves of `args` without uploading
|
|
320
|
+
* anything. This is the schema-aware half of the upload modifier that needs
|
|
321
|
+
* no filesystem access, so it also runs when automatic upload is disabled:
|
|
322
|
+
* an empty file value is never a valid staged descriptor and the backend
|
|
323
|
+
* would reject it (https://github.com/ComposioHQ/composio/issues/4233).
|
|
324
|
+
* Any other value — a local path, a staged descriptor, a list — is
|
|
325
|
+
* forwarded untouched.
|
|
326
|
+
*/
|
|
327
|
+
export const dropEmptyFileUploads = (
|
|
328
|
+
args: Record<string, unknown>,
|
|
329
|
+
schema: JSONSchemaProperty | undefined
|
|
330
|
+
): Promise<unknown> =>
|
|
331
|
+
walkFileUploadableLeaves(args, schema, async value =>
|
|
332
|
+
isEmptyFileValue(value) ? DELETE_VALUE : value
|
|
333
|
+
);
|