@composio/core 1.0.0-beta.0 → 1.0.0-beta.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-CM-roKk7.mjs} +16 -6
- package/dist/{CustomTool-CwpeQhz2.d.mts → CustomTool-yMQjEyMz.d.mts} +1 -1
- package/dist/{FileModifierErrors-B8V8_3VE.mjs → FileModifierErrors-CdGSABoq.mjs} +1 -1
- package/dist/{FileToolModifier.utils.neutral-D_YkHwZP.mjs → FileToolModifier.utils.neutral-DWFpnsj2.mjs} +4 -2
- package/dist/{SsrfErrors-DNJt57GL.mjs → SsrfErrors-DiQldIdC.mjs} +154 -19
- package/dist/{Telemetry-DK1uAWnD.mjs → Telemetry-JRJJYS4I.mjs} +28 -44
- package/dist/cancellation-BEpFk2S8.mjs +706 -0
- package/dist/{composio-XjF7EMEe.d.mts → composio-BVNJTVYa.d.mts} +2450 -119
- package/dist/{customTool.types-DpJbYY5m.d.mts → customTool.types-CY5SXFfF.d.mts} +143 -28
- 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-CvZjP9CH.mjs} +42 -4
- package/dist/experimental/index.d.mts +2 -2
- package/dist/experimental/index.mjs +1 -1
- package/dist/{fileUtils.node-BwpUOF0s.mjs → fileUtils.node-HQsywtTj.mjs} +11 -9
- package/dist/index.d.mts +393 -142
- package/dist/index.mjs +1945 -171
- package/dist/models/Files.node.d.mts +2 -3
- package/dist/models/Files.node.mjs +2 -2
- package/dist/models/Files.workerd.d.mts +2 -3
- package/dist/platform/node.d.mts +2 -3
- package/dist/platform/workerd.d.mts +2 -3
- package/dist/{sensitiveFileUploadPaths-B0w8dqpv.mjs → sensitiveFileUploadPaths-BlLp-Yo2.mjs} +10 -7
- package/dist/{ssrfGuard.node-DlxO8uzr.mjs → ssrfGuard.node-BK50mYTq.mjs} +187 -24
- package/dist/{types-C9kJAurw.d.mts → types-CqGfjlZg.d.mts} +2 -2
- package/dist/utils/config-defaults/ConfigDefaults.node.d.mts +3 -4
- package/dist/utils/config-defaults/ConfigDefaults.workerd.d.mts +3 -4
- package/dist/utils/modifiers/FileToolModifier.node.d.mts +3 -4
- package/dist/utils/modifiers/FileToolModifier.node.mjs +6 -6
- package/dist/utils/modifiers/FileToolModifier.workerd.d.mts +3 -4
- package/dist/utils/modifiers/FileToolModifier.workerd.mjs +1 -1
- package/dist/utils/ssrfGuard.node.d.mts +7 -7
- package/dist/utils/ssrfGuard.node.mjs +1 -1
- package/dist/utils/ssrfGuard.workerd.d.mts +3 -4
- package/dist/utils/ssrfGuard.workerd.mjs +2 -2
- package/package.json +25 -7
- package/src/composio.ts +86 -2
- package/src/errors/ConnectedAccountsErrors.ts +47 -0
- package/src/errors/ToolErrors.ts +1 -0
- package/src/errors/ValidationErrors.ts +2 -0
- package/src/index.ts +21 -0
- package/src/models/ConnectedAccounts.ts +118 -2
- package/src/models/CustomTool.ts +41 -6
- package/src/models/CustomToolkits.ts +169 -0
- package/src/models/Experimental.ts +27 -0
- package/src/models/Keyring.ts +65 -0
- package/src/models/Logs.ts +101 -0
- package/src/models/RemoteFile.ts +54 -36
- package/src/models/ToolRouterSession.ts +60 -4
- package/src/models/Toolkits.ts +180 -0
- package/src/models/Tools.ts +3 -1
- package/src/models/Triggers.ts +17 -87
- package/src/models/Usage.ts +119 -0
- package/src/models/Webhooks.ts +520 -0
- package/src/services/telemetry/TelemetryService.ts +27 -12
- package/src/telemetry/redact.ts +133 -6
- package/src/types/authConfigs.types.ts +2 -0
- package/src/types/connectedAccounts.types.ts +54 -0
- package/src/types/customToolkits.types.ts +102 -0
- package/src/types/keyring.types.ts +32 -0
- package/src/types/logs.types.ts +108 -0
- package/src/types/toolRouter.types.ts +49 -1
- package/src/types/toolkit.types.ts +131 -0
- package/src/types/usage.types.ts +81 -0
- package/src/types/webhooks.types.ts +143 -0
- package/src/utils/constants.ts +1 -5
- package/src/utils/fileUtils.node.ts +10 -2
- package/src/utils/jsonSchema.ts +509 -28
- package/src/utils/logger.ts +45 -13
- package/src/utils/pinnedDispatcher.node.ts +109 -13
- package/src/utils/sdk.ts +1 -1
- package/src/utils/ssrfGuard.node.ts +124 -21
- package/src/utils/ssrfGuard.workerd.ts +5 -1
- package/src/utils/transformers/connectedAccounts.ts +37 -2
- package/src/utils/transformers/customToolkits.ts +98 -0
- package/src/utils/transformers/keyring.ts +24 -0
- package/src/utils/transformers/logs.ts +85 -0
- package/src/utils/transformers/toolkits.ts +100 -1
- package/src/utils/transformers/triggers.ts +2 -2
- package/src/utils/transformers/usage.ts +77 -0
- package/src/utils/transformers/webhooks.ts +139 -0
- package/dist/cancellation-C-qy8rcj.mjs +0 -364
|
@@ -1,364 +0,0 @@
|
|
|
1
|
-
import { i as logger } from "./SsrfErrors-DNJt57GL.mjs";
|
|
2
|
-
import { f as ComposioRequestCancelledError, g as JsonSchemaToZodError, h as JsonSchemaRefResolutionError, m as isRequestAbortError } from "./errors-BcEUHZbq.mjs";
|
|
3
|
-
import { o as isPlainObject } from "./FileToolModifier.utils.neutral-D_YkHwZP.mjs";
|
|
4
|
-
import { jsonSchemaToZod } from "@composio/json-schema-to-zod";
|
|
5
|
-
|
|
6
|
-
//#region src/utils/jsonSchema.ts
|
|
7
|
-
const MAX_REF_CHAIN_DEPTH = 100;
|
|
8
|
-
const MAX_NODE_DEPTH = 512;
|
|
9
|
-
const CYCLE_BREAK_SENTINEL = {
|
|
10
|
-
type: "object",
|
|
11
|
-
additionalProperties: true
|
|
12
|
-
};
|
|
13
|
-
/**
|
|
14
|
-
* In-band hint attached to the cycle-break sentinel when lenient mode
|
|
15
|
-
* substitutes it for a dangling `$ref`. Makes the degradation visible to
|
|
16
|
-
* LLMs that read the wrapped tool's schema — without it the LLM sees a
|
|
17
|
-
* useless permissive object and no prose context. Wording is intentionally
|
|
18
|
-
* context-neutral ("Schema shape", not "Output shape") because the helper
|
|
19
|
-
* is used for both `inputParameters` and `outputParameters`. Overridden
|
|
20
|
-
* when the caller's `$ref` node carries its own `description` sibling
|
|
21
|
-
* (Draft 2020-12 sibling-keyword semantics).
|
|
22
|
-
*/
|
|
23
|
-
const UNRESOLVED_REF_DESCRIPTION = "Schema shape unresolved at the source — validate loosely. See https://github.com/ComposioHQ/composio/issues/3307.";
|
|
24
|
-
const POLLUTING_KEYS = /* @__PURE__ */ new Set([
|
|
25
|
-
"__proto__",
|
|
26
|
-
"constructor",
|
|
27
|
-
"prototype"
|
|
28
|
-
]);
|
|
29
|
-
const REF_RESOLUTION_FIXES = ["Ensure the $ref pointer matches a path in $defs or definitions", "External $ref pointers (http://, https://, file://, …) are not resolved by the SDK"];
|
|
30
|
-
const decodePointerSegment = (segment) => segment.replace(/~1/g, "/").replace(/~0/g, "~");
|
|
31
|
-
const tryStep = (cursor, segment) => {
|
|
32
|
-
if (cursor === null || typeof cursor !== "object") return {
|
|
33
|
-
kind: "unresolved",
|
|
34
|
-
reason: "missing-target",
|
|
35
|
-
failedAt: segment
|
|
36
|
-
};
|
|
37
|
-
if (Array.isArray(cursor)) {
|
|
38
|
-
const i = Number(segment);
|
|
39
|
-
if (!Number.isInteger(i) || i < 0 || i >= cursor.length) return {
|
|
40
|
-
kind: "unresolved",
|
|
41
|
-
reason: "missing-target",
|
|
42
|
-
failedAt: segment
|
|
43
|
-
};
|
|
44
|
-
return {
|
|
45
|
-
kind: "ok",
|
|
46
|
-
value: cursor[i]
|
|
47
|
-
};
|
|
48
|
-
}
|
|
49
|
-
const obj = cursor;
|
|
50
|
-
if (!Object.prototype.hasOwnProperty.call(obj, segment)) return {
|
|
51
|
-
kind: "unresolved",
|
|
52
|
-
reason: "missing-target",
|
|
53
|
-
failedAt: segment
|
|
54
|
-
};
|
|
55
|
-
return {
|
|
56
|
-
kind: "ok",
|
|
57
|
-
value: obj[segment]
|
|
58
|
-
};
|
|
59
|
-
};
|
|
60
|
-
const tryResolvePointer = (root, pointer) => {
|
|
61
|
-
if (pointer === "#" || pointer === "") return {
|
|
62
|
-
kind: "ok",
|
|
63
|
-
value: root
|
|
64
|
-
};
|
|
65
|
-
if (!pointer.startsWith("#/")) return {
|
|
66
|
-
kind: "unresolved",
|
|
67
|
-
reason: "malformed-pointer"
|
|
68
|
-
};
|
|
69
|
-
const segments = pointer.slice(2).split("/").map(decodePointerSegment);
|
|
70
|
-
let cursor = root;
|
|
71
|
-
for (const seg of segments) {
|
|
72
|
-
const step = tryStep(cursor, seg);
|
|
73
|
-
if (step.kind === "unresolved") return step;
|
|
74
|
-
cursor = step.value;
|
|
75
|
-
}
|
|
76
|
-
return {
|
|
77
|
-
kind: "ok",
|
|
78
|
-
value: cursor
|
|
79
|
-
};
|
|
80
|
-
};
|
|
81
|
-
const throwResolutionError = (pointer, result) => {
|
|
82
|
-
if (result.reason === "malformed-pointer") throw new JsonSchemaRefResolutionError(`Unsupported $ref pointer: ${pointer}`, {
|
|
83
|
-
meta: { ref: pointer },
|
|
84
|
-
possibleFixes: REF_RESOLUTION_FIXES
|
|
85
|
-
});
|
|
86
|
-
throw new JsonSchemaRefResolutionError(`Cannot resolve $ref ${pointer}`, {
|
|
87
|
-
meta: {
|
|
88
|
-
ref: pointer,
|
|
89
|
-
...result.failedAt !== void 0 ? { failedAt: result.failedAt } : {}
|
|
90
|
-
},
|
|
91
|
-
possibleFixes: REF_RESOLUTION_FIXES
|
|
92
|
-
});
|
|
93
|
-
};
|
|
94
|
-
/**
|
|
95
|
-
* Inlines internal JSON Schema `$ref` pointers (`#/$defs/...` and legacy
|
|
96
|
-
* `#/definitions/...`) so the returned schema can be safely handed to
|
|
97
|
-
* consumers that don't tolerate unresolved references (e.g. AJV in
|
|
98
|
-
* `@mastra/schema-compat`). External (`http://`, `https://`, …) refs are
|
|
99
|
-
* left untouched. Cycles are broken with `{ type: 'object',
|
|
100
|
-
* additionalProperties: true }`. The input is never mutated.
|
|
101
|
-
*
|
|
102
|
-
* By default, unresolved internal refs throw `JsonSchemaRefResolutionError`.
|
|
103
|
-
* Pass `{ onUnresolved: 'sentinel' }` to replace the offending node with the
|
|
104
|
-
* cycle-break sentinel instead — appropriate for schemas sourced from an
|
|
105
|
-
* upstream service the caller cannot edit (the Composio API ships some
|
|
106
|
-
* `outputParameters` with a `$ref` into `#/$defs/...` but never declares
|
|
107
|
-
* `$defs`; see https://github.com/ComposioHQ/composio/issues/3307).
|
|
108
|
-
*
|
|
109
|
-
* @throws {JsonSchemaRefResolutionError} on malformed pointers, missing
|
|
110
|
-
* targets (strict mode only), or chains past the depth cap (both modes).
|
|
111
|
-
*/
|
|
112
|
-
function dereferenceJsonSchema(schema, options) {
|
|
113
|
-
if (!isPlainObject(schema)) return schema;
|
|
114
|
-
const strategy = options?.onUnresolved ?? "throw";
|
|
115
|
-
const onReplace = options?.onReplace;
|
|
116
|
-
const root = schema;
|
|
117
|
-
const visiting = /* @__PURE__ */ new WeakSet();
|
|
118
|
-
const cloneChildren = (obj, visitedRefs, chainDepth, nodeDepth) => Object.fromEntries(Object.entries(obj).filter(([k]) => !POLLUTING_KEYS.has(k)).map(([k, v]) => [k, walk(v, visitedRefs, chainDepth, nodeDepth + 1)]));
|
|
119
|
-
function walk(node, visitedRefs, chainDepth, nodeDepth) {
|
|
120
|
-
if (nodeDepth >= MAX_NODE_DEPTH) throw new JsonSchemaRefResolutionError(`JSON Schema node depth exceeded cap (${MAX_NODE_DEPTH})`, { possibleFixes: REF_RESOLUTION_FIXES });
|
|
121
|
-
if (Array.isArray(node)) {
|
|
122
|
-
if (visiting.has(node)) return { ...CYCLE_BREAK_SENTINEL };
|
|
123
|
-
visiting.add(node);
|
|
124
|
-
try {
|
|
125
|
-
return node.map((item) => walk(item, visitedRefs, chainDepth, nodeDepth + 1));
|
|
126
|
-
} finally {
|
|
127
|
-
visiting.delete(node);
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
if (!isPlainObject(node)) return node;
|
|
131
|
-
if (visiting.has(node)) return { ...CYCLE_BREAK_SENTINEL };
|
|
132
|
-
visiting.add(node);
|
|
133
|
-
try {
|
|
134
|
-
const ref = typeof node.$ref === "string" ? node.$ref : null;
|
|
135
|
-
if (ref === null || !ref.startsWith("#")) {
|
|
136
|
-
if (ref !== null) logger.warn(`Leaving external $ref untouched: ${ref}`);
|
|
137
|
-
return cloneChildren(node, visitedRefs, chainDepth, nodeDepth);
|
|
138
|
-
}
|
|
139
|
-
if (chainDepth >= MAX_REF_CHAIN_DEPTH) throw new JsonSchemaRefResolutionError(`JSON Schema $ref chain exceeded depth cap (${MAX_REF_CHAIN_DEPTH}): ${ref}`, {
|
|
140
|
-
meta: { ref },
|
|
141
|
-
possibleFixes: REF_RESOLUTION_FIXES
|
|
142
|
-
});
|
|
143
|
-
if (visitedRefs.has(ref)) return { ...CYCLE_BREAK_SENTINEL };
|
|
144
|
-
const result = tryResolvePointer(root, ref);
|
|
145
|
-
let target;
|
|
146
|
-
if (result.kind === "ok") target = result.value;
|
|
147
|
-
else if (strategy === "sentinel") {
|
|
148
|
-
onReplace?.(ref, result.reason);
|
|
149
|
-
target = {
|
|
150
|
-
...CYCLE_BREAK_SENTINEL,
|
|
151
|
-
description: UNRESOLVED_REF_DESCRIPTION
|
|
152
|
-
};
|
|
153
|
-
} else throwResolutionError(ref, result);
|
|
154
|
-
const nextRefs = new Set(visitedRefs).add(ref);
|
|
155
|
-
const resolved = walk(target, nextRefs, chainDepth + 1, nodeDepth + 1);
|
|
156
|
-
const siblings = { ...node };
|
|
157
|
-
delete siblings.$ref;
|
|
158
|
-
if (Object.keys(siblings).length === 0 || !isPlainObject(resolved)) return resolved;
|
|
159
|
-
return {
|
|
160
|
-
...resolved,
|
|
161
|
-
...cloneChildren(siblings, visitedRefs, chainDepth, nodeDepth)
|
|
162
|
-
};
|
|
163
|
-
} finally {
|
|
164
|
-
visiting.delete(node);
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
const out = walk(root, /* @__PURE__ */ new Set(), 0, 0);
|
|
168
|
-
if (isPlainObject(out)) {
|
|
169
|
-
delete out.$defs;
|
|
170
|
-
delete out.definitions;
|
|
171
|
-
}
|
|
172
|
-
return out;
|
|
173
|
-
}
|
|
174
|
-
/**
|
|
175
|
-
* Returns a deep-cloned JSON Schema in which every node that carries a
|
|
176
|
-
* `properties` keyword also carries `type: "object"` when it has no explicit
|
|
177
|
-
* `type`. OpenAI tolerates the omission, but Google Gemini enforces OpenAPI
|
|
178
|
-
* 3.0 strictly and rejects function declarations whose nested objects are
|
|
179
|
-
* missing the `type` (see https://github.com/ComposioHQ/composio/issues/4022).
|
|
180
|
-
*
|
|
181
|
-
* The function follows JSON Schema's schema-bearing keywords while keeping
|
|
182
|
-
* property maps and instance values as containers. Nodes that already declare
|
|
183
|
-
* a `type` are left untouched.
|
|
184
|
-
*/
|
|
185
|
-
function ensureObjectTypeOnProperties(schema) {
|
|
186
|
-
const schemaKeywords = /* @__PURE__ */ new Set([
|
|
187
|
-
"additionalItems",
|
|
188
|
-
"additionalProperties",
|
|
189
|
-
"contains",
|
|
190
|
-
"contentSchema",
|
|
191
|
-
"else",
|
|
192
|
-
"if",
|
|
193
|
-
"not",
|
|
194
|
-
"propertyNames",
|
|
195
|
-
"then",
|
|
196
|
-
"unevaluatedItems",
|
|
197
|
-
"unevaluatedProperties"
|
|
198
|
-
]);
|
|
199
|
-
const schemaArrayKeywords = /* @__PURE__ */ new Set([
|
|
200
|
-
"allOf",
|
|
201
|
-
"anyOf",
|
|
202
|
-
"oneOf",
|
|
203
|
-
"prefixItems"
|
|
204
|
-
]);
|
|
205
|
-
const schemaMapKeywords = /* @__PURE__ */ new Set([
|
|
206
|
-
"$defs",
|
|
207
|
-
"definitions",
|
|
208
|
-
"dependentSchemas",
|
|
209
|
-
"patternProperties",
|
|
210
|
-
"properties"
|
|
211
|
-
]);
|
|
212
|
-
function walk(value, mode, depth = 0) {
|
|
213
|
-
if (depth > MAX_NODE_DEPTH) throw new RangeError(`JSON Schema exceeds maximum nesting depth of ${MAX_NODE_DEPTH}`);
|
|
214
|
-
if (Array.isArray(value)) {
|
|
215
|
-
const itemMode = mode === "schema-array" ? "schema" : "value";
|
|
216
|
-
return value.map((item) => walk(item, itemMode, depth + 1));
|
|
217
|
-
}
|
|
218
|
-
if (!isPlainObject(value)) return value;
|
|
219
|
-
const clone = {};
|
|
220
|
-
for (const [key, child] of Object.entries(value)) {
|
|
221
|
-
let childMode = "value";
|
|
222
|
-
if (mode === "schema-map") childMode = "schema";
|
|
223
|
-
else if (mode === "dependencies-map") childMode = Array.isArray(child) ? "value" : "schema";
|
|
224
|
-
else if (mode === "schema") {
|
|
225
|
-
if (schemaMapKeywords.has(key)) childMode = "schema-map";
|
|
226
|
-
else if (schemaArrayKeywords.has(key) || key === "items" && Array.isArray(child)) childMode = "schema-array";
|
|
227
|
-
else if (schemaKeywords.has(key) || key === "items") childMode = "schema";
|
|
228
|
-
else if (key === "dependencies") childMode = "dependencies-map";
|
|
229
|
-
}
|
|
230
|
-
Object.defineProperty(clone, key, {
|
|
231
|
-
configurable: true,
|
|
232
|
-
enumerable: true,
|
|
233
|
-
value: walk(child, childMode, depth + 1),
|
|
234
|
-
writable: true
|
|
235
|
-
});
|
|
236
|
-
}
|
|
237
|
-
if (mode === "schema" && clone.properties !== void 0 && clone.type === void 0) clone.type = "object";
|
|
238
|
-
return clone;
|
|
239
|
-
}
|
|
240
|
-
return walk(schema, "schema");
|
|
241
|
-
}
|
|
242
|
-
/**
|
|
243
|
-
* Returns a deep-cloned JSON Schema whose `required` arrays contain each entry
|
|
244
|
-
* at most once. Duplicate entries are invalid in JSON Schema 2020-12 but do
|
|
245
|
-
* not change the schema's meaning, so preserving the first occurrence is safe.
|
|
246
|
-
*
|
|
247
|
-
* Tool schemas originate both from the Composio API and direct provider calls.
|
|
248
|
-
* Keeping this normalization provider-agnostic lets every SDK provider receive
|
|
249
|
-
* canonical API schemas, while providers that further transform a schema can
|
|
250
|
-
* reapply it at their own emission boundary.
|
|
251
|
-
*/
|
|
252
|
-
function deduplicateJsonSchemaRequiredArrays(schema) {
|
|
253
|
-
const seenSchemaValues = /* @__PURE__ */ new WeakMap();
|
|
254
|
-
const seenInstanceValues = /* @__PURE__ */ new WeakMap();
|
|
255
|
-
const instanceValueKeywords = /* @__PURE__ */ new Set([
|
|
256
|
-
"const",
|
|
257
|
-
"default",
|
|
258
|
-
"enum",
|
|
259
|
-
"examples"
|
|
260
|
-
]);
|
|
261
|
-
function walk(value, isSchema, depth = 0) {
|
|
262
|
-
if (depth > MAX_NODE_DEPTH) throw new RangeError(`JSON Schema exceeds maximum nesting depth of ${MAX_NODE_DEPTH}`);
|
|
263
|
-
const seen = isSchema ? seenSchemaValues : seenInstanceValues;
|
|
264
|
-
if (Array.isArray(value)) {
|
|
265
|
-
const existing = seen.get(value);
|
|
266
|
-
if (existing) return existing;
|
|
267
|
-
const clone = [];
|
|
268
|
-
seen.set(value, clone);
|
|
269
|
-
for (const item of value) clone.push(walk(item, isSchema, depth + 1));
|
|
270
|
-
return clone;
|
|
271
|
-
}
|
|
272
|
-
if (!isPlainObject(value)) return value;
|
|
273
|
-
const existing = seen.get(value);
|
|
274
|
-
if (existing) return existing;
|
|
275
|
-
const clone = {};
|
|
276
|
-
seen.set(value, clone);
|
|
277
|
-
for (const [key, child] of Object.entries(value)) Object.defineProperty(clone, key, {
|
|
278
|
-
configurable: true,
|
|
279
|
-
enumerable: true,
|
|
280
|
-
value: isSchema && key === "required" && Array.isArray(child) ? [...new Set(walk(child, false, depth + 1))] : walk(child, isSchema && !instanceValueKeywords.has(key), depth + 1),
|
|
281
|
-
writable: true
|
|
282
|
-
});
|
|
283
|
-
return clone;
|
|
284
|
-
}
|
|
285
|
-
return walk(schema, true);
|
|
286
|
-
}
|
|
287
|
-
/**
|
|
288
|
-
* Removes all non-required properties from the schema
|
|
289
|
-
*
|
|
290
|
-
* if no items are required, the schema is returned as is
|
|
291
|
-
* @param schema - The JSON schema to remove non-required properties from
|
|
292
|
-
* @returns The JSON schema with all non-required properties removed
|
|
293
|
-
*/
|
|
294
|
-
const removeNonRequiredProperties = (schema) => {
|
|
295
|
-
if (schema && schema.type === "object" && schema.required?.length) schema.properties = Object.fromEntries(Object.entries(schema.properties || {}).filter(([key]) => schema.required.includes(key)));
|
|
296
|
-
schema.additionalProperties = false;
|
|
297
|
-
return schema;
|
|
298
|
-
};
|
|
299
|
-
/**
|
|
300
|
-
* Convert a JSON schema to a Zod schema
|
|
301
|
-
* @param jsonSchema - The JSON schema to convert
|
|
302
|
-
* @param strict - Eliminates all non-required properties from the schema
|
|
303
|
-
* @returns The Zod schema
|
|
304
|
-
*
|
|
305
|
-
* @throws {JsonSchemaToZodError} If the JSON schema is invalid
|
|
306
|
-
*
|
|
307
|
-
* @example
|
|
308
|
-
* ```ts
|
|
309
|
-
* const zodSchema = jsonSchemaToZodSchema({
|
|
310
|
-
* type: 'object',
|
|
311
|
-
* properties: {
|
|
312
|
-
* name: { type: 'string' },
|
|
313
|
-
* },
|
|
314
|
-
* });
|
|
315
|
-
*
|
|
316
|
-
* console.log(zodSchema);
|
|
317
|
-
* ```
|
|
318
|
-
*
|
|
319
|
-
* @example
|
|
320
|
-
* ```ts
|
|
321
|
-
* const zodSchema = jsonSchemaToZodSchema({
|
|
322
|
-
* type: 'object',
|
|
323
|
-
* properties: {
|
|
324
|
-
* name: { type: 'string' },
|
|
325
|
-
* age: { type: 'number' },
|
|
326
|
-
* },
|
|
327
|
-
* required: ['name'],
|
|
328
|
-
* }, { strict: true });
|
|
329
|
-
*
|
|
330
|
-
* console.log(zodSchema);
|
|
331
|
-
*
|
|
332
|
-
* // Output:
|
|
333
|
-
* // z.object({
|
|
334
|
-
* // name: z.string(),
|
|
335
|
-
* // })
|
|
336
|
-
* ```
|
|
337
|
-
*/
|
|
338
|
-
function jsonSchemaToZodSchema(jsonSchema, { strict } = { strict: false }) {
|
|
339
|
-
try {
|
|
340
|
-
let schema = jsonSchema;
|
|
341
|
-
if (strict && schema) schema = removeNonRequiredProperties(schema);
|
|
342
|
-
return jsonSchemaToZod(schema);
|
|
343
|
-
} catch (error) {
|
|
344
|
-
throw new JsonSchemaToZodError("Failed to convert JSON Schema to Zod Schema", { cause: error });
|
|
345
|
-
}
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
//#endregion
|
|
349
|
-
//#region src/utils/cancellation.ts
|
|
350
|
-
/** @internal */
|
|
351
|
-
async function withCancellation(call, signal) {
|
|
352
|
-
try {
|
|
353
|
-
return await call();
|
|
354
|
-
} catch (error) {
|
|
355
|
-
if (signal?.aborted && isRequestAbortError(error)) {
|
|
356
|
-
const underlyingMessage = error instanceof Error ? error.message : "";
|
|
357
|
-
throw new ComposioRequestCancelledError(underlyingMessage ? `Request was cancelled by the caller: ${underlyingMessage}` : "Request was cancelled by the caller", { cause: error instanceof Error ? error : void 0 });
|
|
358
|
-
}
|
|
359
|
-
throw error;
|
|
360
|
-
}
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
//#endregion
|
|
364
|
-
export { jsonSchemaToZodSchema as a, ensureObjectTypeOnProperties as i, deduplicateJsonSchemaRequiredArrays as n, removeNonRequiredProperties as o, dereferenceJsonSchema as r, withCancellation as t };
|