@composio/core 0.17.0 → 0.18.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/{CustomTool-Uip-fhPw.d.mts → CustomTool-BKhHv2hJ.d.mts} +1 -1
- package/dist/{CustomTool-l-j5HtEf.mjs → CustomTool-C4tv-GL1.mjs} +1 -1
- package/dist/FileToolModifier.utils.neutral-D_YkHwZP.mjs +188 -0
- package/dist/{Telemetry-DuGJSwUt.mjs → Telemetry-CBtSnWrm.mjs} +27 -11
- package/dist/cancellation-jUVItfHw.mjs +704 -0
- package/dist/{composio-B27X6i6A.d.mts → composio-CldtZUyC.d.mts} +256 -250
- package/dist/{customTool.types-B2Ava_j5.d.mts → customTool.types-DC5fPOOi.d.mts} +178 -178
- package/dist/docs/reference/sdk-reference/typescript/session-files.mdx +1 -1
- package/dist/{errors-BcEUHZbq.mjs → errors-DyZt79fi.mjs} +3 -0
- package/dist/experimental/index.d.mts +2 -2
- package/dist/experimental/index.mjs +1 -1
- package/dist/{fileUtils.node-B1Wn848p.mjs → fileUtils.node-BwpUOF0s.mjs} +2 -2
- package/dist/index.d.mts +104 -5
- package/dist/index.mjs +61 -27
- package/dist/models/Files.node.mjs +2 -2
- package/dist/{sensitiveFileUploadPaths-BVEt0peg.mjs → sensitiveFileUploadPaths-B0w8dqpv.mjs} +23 -6
- package/dist/ssrfGuard.node-DlxO8uzr.mjs +307 -0
- package/dist/{types-DdZJuMm2.d.mts → types-BgsRW-XH.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 +57 -101
- 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/package.json +7 -3
- package/src/errors/ToolErrors.ts +1 -0
- package/src/errors/ValidationErrors.ts +2 -0
- package/src/index.ts +6 -0
- package/src/models/ToolRouterSession.ts +36 -8
- package/src/models/Tools.ts +102 -39
- package/src/services/telemetry/TelemetryService.ts +27 -12
- package/src/utils/jsonSchema.ts +505 -27
- 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 +118 -0
- package/src/utils/sensitiveFileUploadPaths.ts +36 -9
- package/src/utils/ssrfGuard.node.ts +89 -11
- 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
|
+
);
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { isIP } from 'node:net';
|
|
2
|
+
import type { Agent } from 'undici';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The `globalThis` symbols that hold the process-wide dispatcher `fetch` falls
|
|
6
|
+
* back to. Every undici in the process shares them — including the one inside
|
|
7
|
+
* the Node runtime — and `setGlobalDispatcher` writes both, so reading them
|
|
8
|
+
* directly sees the same dispatcher the runtime's own `fetch` would use.
|
|
9
|
+
*
|
|
10
|
+
* Read rather than imported: importing `undici` installs a dispatcher of its
|
|
11
|
+
* own as an import side effect (see {@link loadUndici}), which is exactly what
|
|
12
|
+
* this module must not do at load time.
|
|
13
|
+
*/
|
|
14
|
+
const GLOBAL_DISPATCHER_SYMBOLS = [
|
|
15
|
+
Symbol.for('undici.globalDispatcher.1'),
|
|
16
|
+
Symbol.for('undici.globalDispatcher.2'),
|
|
17
|
+
] as const;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* `undici`, imported on first use rather than at module load.
|
|
21
|
+
*
|
|
22
|
+
* Importing it runs `if (getGlobalDispatcher() === undefined) setGlobalDispatcher(new Agent())`
|
|
23
|
+
* as a side effect, which would hand every unrelated `fetch` in the host
|
|
24
|
+
* application this package's undici instead of the runtime's own — a global
|
|
25
|
+
* change no caller asked for by importing `@composio/core`. Deferring the
|
|
26
|
+
* import to the first pinned request keeps that side effect on the code path
|
|
27
|
+
* that actually needs undici, and behind the `hasCustomGlobalDispatcher` check
|
|
28
|
+
* that reads the slot's true, pre-import state.
|
|
29
|
+
*
|
|
30
|
+
* A process that does make a pinned request still ends up on this package's
|
|
31
|
+
* `Agent` if nothing had claimed the slot yet. That much is not reversible:
|
|
32
|
+
* undici defines the slot non-configurable and writable, so it cannot be
|
|
33
|
+
* cleared back to empty — assigning `undefined` leaves the runtime's own
|
|
34
|
+
* `fetch` asserting on a missing dispatcher.
|
|
35
|
+
*/
|
|
36
|
+
let undici: Promise<typeof import('undici')> | undefined;
|
|
37
|
+
const loadUndici = (): Promise<typeof import('undici')> => (undici ??= import('undici'));
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* A `fetch` dispatcher that connects to `addresses` instead of resolving the
|
|
41
|
+
* hostname again.
|
|
42
|
+
*
|
|
43
|
+
* The SSRF guard resolves a hostname to decide whether a URL is safe to fetch.
|
|
44
|
+
* `fetch` would then resolve that hostname a second time when it opens the
|
|
45
|
+
* socket, and the two answers need not match: a short-TTL record can answer
|
|
46
|
+
* with a public address for the check and an internal one for the connect
|
|
47
|
+
* (DNS rebinding). Pinning the connect to the address that was validated
|
|
48
|
+
* removes the second lookup, so there is nothing left to rebind.
|
|
49
|
+
*
|
|
50
|
+
* Only the connect target is replaced. The request still carries the original
|
|
51
|
+
* hostname in `Host` and offers it as TLS SNI, so certificate verification is
|
|
52
|
+
* unchanged — connecting by IP with the IP as SNI would fail against every
|
|
53
|
+
* origin whose certificate names a hostname.
|
|
54
|
+
*
|
|
55
|
+
* The dispatcher is handed to the runtime's own `fetch`, so callers that stub
|
|
56
|
+
* `globalThis.fetch` keep working. That does tie the `undici` major to the one
|
|
57
|
+
* Node's `fetch` speaks — undici 8 dispatchers are rejected by the Node
|
|
58
|
+
* versions this package supports — which is why the dependency is pinned to
|
|
59
|
+
* `^7`, and why `pinnedDispatcher.node.test.ts` exercises a real socket on
|
|
60
|
+
* every Node version in CI rather than mocking the transport.
|
|
61
|
+
*/
|
|
62
|
+
export const createPinnedDispatcher = async (addresses: ReadonlyArray<string>): Promise<Agent> => {
|
|
63
|
+
const { Agent } = await loadUndici();
|
|
64
|
+
|
|
65
|
+
return new Agent({
|
|
66
|
+
connect: {
|
|
67
|
+
lookup: (_hostname, options, callback) => {
|
|
68
|
+
// Node calls a lookup in one of two shapes and picks between them at
|
|
69
|
+
// connect time: `(err, addresses)` when it is doing Happy Eyeballs,
|
|
70
|
+
// and `(err, address, family)` otherwise — when `autoSelectFamily` is
|
|
71
|
+
// off, or a `family`/`localAddress` was requested. Answering in the
|
|
72
|
+
// wrong shape is rejected as an invalid address, which would fail
|
|
73
|
+
// every pinned fetch in such a process.
|
|
74
|
+
if (options.all) {
|
|
75
|
+
// All of them, in resolver order: handing over a single address of a
|
|
76
|
+
// dual-stack host would strand callers whose network cannot reach
|
|
77
|
+
// that family, where the runtime would otherwise have fallen back.
|
|
78
|
+
callback(
|
|
79
|
+
null,
|
|
80
|
+
addresses.map(address => ({ address, family: isIP(address) }))
|
|
81
|
+
);
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const [address] = addresses;
|
|
86
|
+
callback(null, address, isIP(address));
|
|
87
|
+
},
|
|
88
|
+
},
|
|
89
|
+
});
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Whether the process-wide dispatcher `fetch` falls back to (when a request
|
|
94
|
+
* carries no `dispatcher` of its own) is something other than a stock `Agent`.
|
|
95
|
+
*
|
|
96
|
+
* A `ProxyAgent`, `EnvHttpProxyAgent`, or custom dispatcher installed through
|
|
97
|
+
* `setGlobalDispatcher` is visible here, and pinning stands down for it: the
|
|
98
|
+
* connect would dial the validated origin instead of that route's next hop.
|
|
99
|
+
*
|
|
100
|
+
* Nothing installed yet is *not* custom. The dispatcher slot is empty until
|
|
101
|
+
* either the runtime's `fetch` or an undici import fills it, so an absent
|
|
102
|
+
* dispatcher means no caller has configured a route — treating it as custom
|
|
103
|
+
* would disable pinning for every process that had not yet made a request.
|
|
104
|
+
*
|
|
105
|
+
* Stock-ness is compared by constructor *name*, deliberately not with
|
|
106
|
+
* `instanceof Agent`: an `Agent` created by the runtime's internal undici is a
|
|
107
|
+
* different class from this package's `Agent`, so once the runtime's own
|
|
108
|
+
* `fetch` has initialized its default dispatcher, an `instanceof` check would
|
|
109
|
+
* report a stock setup as custom and silently disable pinning.
|
|
110
|
+
*/
|
|
111
|
+
export const hasCustomGlobalDispatcher = (): boolean => {
|
|
112
|
+
const slots = globalThis as unknown as Record<symbol, { constructor?: { name?: string } }>;
|
|
113
|
+
const dispatcher = GLOBAL_DISPATCHER_SYMBOLS.map(symbol => slots[symbol]).find(
|
|
114
|
+
installed => installed !== undefined
|
|
115
|
+
);
|
|
116
|
+
|
|
117
|
+
return dispatcher !== undefined && dispatcher.constructor?.name !== 'Agent';
|
|
118
|
+
};
|