@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,355 @@
|
|
|
1
|
+
import { z } from 'zod/v3';
|
|
2
|
+
import { JsonSchemaRefResolutionError, JsonSchemaToZodError } from '../errors';
|
|
3
|
+
import { jsonSchemaToZod } from '@composio/json-schema-to-zod';
|
|
4
|
+
import logger from './logger';
|
|
5
|
+
import { isPlainObject } from './modifiers/FileToolModifier.utils.neutral';
|
|
6
|
+
|
|
7
|
+
const MAX_REF_CHAIN_DEPTH = 100;
|
|
8
|
+
const MAX_NODE_DEPTH = 512;
|
|
9
|
+
const CYCLE_BREAK_SENTINEL = { type: 'object', additionalProperties: true } as const;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* In-band hint attached to the cycle-break sentinel when lenient mode
|
|
13
|
+
* substitutes it for a dangling `$ref`. Makes the degradation visible to
|
|
14
|
+
* LLMs that read the wrapped tool's schema — without it the LLM sees a
|
|
15
|
+
* useless permissive object and no prose context. Wording is intentionally
|
|
16
|
+
* context-neutral ("Schema shape", not "Output shape") because the helper
|
|
17
|
+
* is used for both `inputParameters` and `outputParameters`. Overridden
|
|
18
|
+
* when the caller's `$ref` node carries its own `description` sibling
|
|
19
|
+
* (Draft 2020-12 sibling-keyword semantics).
|
|
20
|
+
*/
|
|
21
|
+
const UNRESOLVED_REF_DESCRIPTION =
|
|
22
|
+
'Schema shape unresolved at the source — validate loosely. ' +
|
|
23
|
+
'See https://github.com/ComposioHQ/composio/issues/3307.';
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Strategy for `dereferenceJsonSchema` when an internal `$ref` cannot be
|
|
27
|
+
* resolved (target missing under `$defs`/`definitions`, or a malformed
|
|
28
|
+
* pointer beneath the internal `#/` prefix).
|
|
29
|
+
*
|
|
30
|
+
* - `'throw'` (default): throw `JsonSchemaRefResolutionError`. Right for
|
|
31
|
+
* first-party / custom-tool schemas where a dangling `$ref` is a developer
|
|
32
|
+
* bug worth surfacing.
|
|
33
|
+
* - `'sentinel'`: replace the offending node with the cycle-break sentinel
|
|
34
|
+
* (`{ type: 'object', additionalProperties: true }`). Right for schemas
|
|
35
|
+
* sourced from an upstream service the caller cannot edit (e.g. an
|
|
36
|
+
* API-provided tool definition that emits `$ref` without a `$defs`
|
|
37
|
+
* block — see https://github.com/ComposioHQ/composio/issues/3307).
|
|
38
|
+
*
|
|
39
|
+
* Safety caps (`MAX_REF_CHAIN_DEPTH`, `MAX_NODE_DEPTH`) still throw in both
|
|
40
|
+
* modes; cycle handling (already permissive via `CYCLE_BREAK_SENTINEL`) is
|
|
41
|
+
* unaffected.
|
|
42
|
+
*/
|
|
43
|
+
export type UnresolvedRefStrategy = 'throw' | 'sentinel';
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Reason a `$ref` was replaced with the sentinel in `'sentinel'` mode.
|
|
47
|
+
* `onReplace` callbacks receive this so callers can attribute the
|
|
48
|
+
* fallback (e.g., warn at the offending tool slug).
|
|
49
|
+
*/
|
|
50
|
+
export type UnresolvedRefReason = 'missing-target' | 'malformed-pointer';
|
|
51
|
+
|
|
52
|
+
export interface DereferenceJsonSchemaOptions {
|
|
53
|
+
onUnresolved?: UnresolvedRefStrategy;
|
|
54
|
+
/**
|
|
55
|
+
* Invoked once per replaced node in `'sentinel'` mode. The ref is the
|
|
56
|
+
* original pointer string from the schema; reason distinguishes a
|
|
57
|
+
* missing `$defs` target from a malformed pointer.
|
|
58
|
+
*/
|
|
59
|
+
onReplace?: (ref: string, reason: UnresolvedRefReason) => void;
|
|
60
|
+
}
|
|
61
|
+
const POLLUTING_KEYS = new Set(['__proto__', 'constructor', 'prototype']);
|
|
62
|
+
const REF_RESOLUTION_FIXES = [
|
|
63
|
+
'Ensure the $ref pointer matches a path in $defs or definitions',
|
|
64
|
+
'External $ref pointers (http://, https://, file://, …) are not resolved by the SDK',
|
|
65
|
+
];
|
|
66
|
+
|
|
67
|
+
const decodePointerSegment = (segment: string): string =>
|
|
68
|
+
segment.replace(/~1/g, '/').replace(/~0/g, '~');
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Tagged result of attempting to resolve a `$ref`. `failedAt` is populated
|
|
72
|
+
* only for the segment-walk path so strict-mode error meta stays identical
|
|
73
|
+
* to the pre-lenient-mode shape.
|
|
74
|
+
*/
|
|
75
|
+
type ResolutionResult =
|
|
76
|
+
| { kind: 'ok'; value: unknown }
|
|
77
|
+
| { kind: 'unresolved'; reason: UnresolvedRefReason; failedAt?: string };
|
|
78
|
+
|
|
79
|
+
const tryStep = (cursor: unknown, segment: string): ResolutionResult => {
|
|
80
|
+
if (cursor === null || typeof cursor !== 'object') {
|
|
81
|
+
return { kind: 'unresolved', reason: 'missing-target', failedAt: segment };
|
|
82
|
+
}
|
|
83
|
+
if (Array.isArray(cursor)) {
|
|
84
|
+
const i = Number(segment);
|
|
85
|
+
if (!Number.isInteger(i) || i < 0 || i >= cursor.length) {
|
|
86
|
+
return { kind: 'unresolved', reason: 'missing-target', failedAt: segment };
|
|
87
|
+
}
|
|
88
|
+
return { kind: 'ok', value: cursor[i] };
|
|
89
|
+
}
|
|
90
|
+
const obj = cursor as Record<string, unknown>;
|
|
91
|
+
if (!Object.prototype.hasOwnProperty.call(obj, segment)) {
|
|
92
|
+
return { kind: 'unresolved', reason: 'missing-target', failedAt: segment };
|
|
93
|
+
}
|
|
94
|
+
return { kind: 'ok', value: obj[segment] };
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
const tryResolvePointer = (root: Record<string, unknown>, pointer: string): ResolutionResult => {
|
|
98
|
+
if (pointer === '#' || pointer === '') return { kind: 'ok', value: root };
|
|
99
|
+
if (!pointer.startsWith('#/')) {
|
|
100
|
+
return { kind: 'unresolved', reason: 'malformed-pointer' };
|
|
101
|
+
}
|
|
102
|
+
const segments = pointer.slice(2).split('/').map(decodePointerSegment);
|
|
103
|
+
let cursor: unknown = root;
|
|
104
|
+
for (const seg of segments) {
|
|
105
|
+
const step = tryStep(cursor, seg);
|
|
106
|
+
if (step.kind === 'unresolved') return step;
|
|
107
|
+
cursor = step.value;
|
|
108
|
+
}
|
|
109
|
+
return { kind: 'ok', value: cursor };
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
const throwResolutionError = (
|
|
113
|
+
pointer: string,
|
|
114
|
+
result: Extract<ResolutionResult, { kind: 'unresolved' }>
|
|
115
|
+
): never => {
|
|
116
|
+
if (result.reason === 'malformed-pointer') {
|
|
117
|
+
throw new JsonSchemaRefResolutionError(`Unsupported $ref pointer: ${pointer}`, {
|
|
118
|
+
meta: { ref: pointer },
|
|
119
|
+
possibleFixes: REF_RESOLUTION_FIXES,
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
throw new JsonSchemaRefResolutionError(`Cannot resolve $ref ${pointer}`, {
|
|
123
|
+
meta: {
|
|
124
|
+
ref: pointer,
|
|
125
|
+
...(result.failedAt !== undefined ? { failedAt: result.failedAt } : {}),
|
|
126
|
+
},
|
|
127
|
+
possibleFixes: REF_RESOLUTION_FIXES,
|
|
128
|
+
});
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Inlines internal JSON Schema `$ref` pointers (`#/$defs/...` and legacy
|
|
133
|
+
* `#/definitions/...`) so the returned schema can be safely handed to
|
|
134
|
+
* consumers that don't tolerate unresolved references (e.g. AJV in
|
|
135
|
+
* `@mastra/schema-compat`). External (`http://`, `https://`, …) refs are
|
|
136
|
+
* left untouched. Cycles are broken with `{ type: 'object',
|
|
137
|
+
* additionalProperties: true }`. The input is never mutated.
|
|
138
|
+
*
|
|
139
|
+
* By default, unresolved internal refs throw `JsonSchemaRefResolutionError`.
|
|
140
|
+
* Pass `{ onUnresolved: 'sentinel' }` to replace the offending node with the
|
|
141
|
+
* cycle-break sentinel instead — appropriate for schemas sourced from an
|
|
142
|
+
* upstream service the caller cannot edit (the Composio API ships some
|
|
143
|
+
* `outputParameters` with a `$ref` into `#/$defs/...` but never declares
|
|
144
|
+
* `$defs`; see https://github.com/ComposioHQ/composio/issues/3307).
|
|
145
|
+
*
|
|
146
|
+
* @throws {JsonSchemaRefResolutionError} on malformed pointers, missing
|
|
147
|
+
* targets (strict mode only), or chains past the depth cap (both modes).
|
|
148
|
+
*/
|
|
149
|
+
export function dereferenceJsonSchema<T = unknown>(
|
|
150
|
+
schema: T,
|
|
151
|
+
options?: DereferenceJsonSchemaOptions
|
|
152
|
+
): T {
|
|
153
|
+
if (!isPlainObject(schema)) return schema;
|
|
154
|
+
|
|
155
|
+
const strategy: UnresolvedRefStrategy = options?.onUnresolved ?? 'throw';
|
|
156
|
+
const onReplace = options?.onReplace;
|
|
157
|
+
|
|
158
|
+
const root = schema as Record<string, unknown>;
|
|
159
|
+
const visiting = new WeakSet<object>();
|
|
160
|
+
|
|
161
|
+
// POLLUTING_KEYS filter prevents an attacker-shaped $defs entry from altering
|
|
162
|
+
// the cloned node's prototype.
|
|
163
|
+
const cloneChildren = (
|
|
164
|
+
obj: Record<string, unknown>,
|
|
165
|
+
visitedRefs: ReadonlySet<string>,
|
|
166
|
+
chainDepth: number,
|
|
167
|
+
nodeDepth: number
|
|
168
|
+
): Record<string, unknown> =>
|
|
169
|
+
Object.fromEntries(
|
|
170
|
+
Object.entries(obj)
|
|
171
|
+
.filter(([k]) => !POLLUTING_KEYS.has(k))
|
|
172
|
+
.map(([k, v]) => [k, walk(v, visitedRefs, chainDepth, nodeDepth + 1)])
|
|
173
|
+
);
|
|
174
|
+
|
|
175
|
+
// `function` (not `const`) so `cloneChildren` above can call it via hoisting.
|
|
176
|
+
function walk(
|
|
177
|
+
node: unknown,
|
|
178
|
+
visitedRefs: ReadonlySet<string>,
|
|
179
|
+
chainDepth: number,
|
|
180
|
+
nodeDepth: number
|
|
181
|
+
): unknown {
|
|
182
|
+
if (nodeDepth >= MAX_NODE_DEPTH) {
|
|
183
|
+
throw new JsonSchemaRefResolutionError(
|
|
184
|
+
`JSON Schema node depth exceeded cap (${MAX_NODE_DEPTH})`,
|
|
185
|
+
{ possibleFixes: REF_RESOLUTION_FIXES }
|
|
186
|
+
);
|
|
187
|
+
}
|
|
188
|
+
if (Array.isArray(node)) {
|
|
189
|
+
if (visiting.has(node)) return { ...CYCLE_BREAK_SENTINEL };
|
|
190
|
+
visiting.add(node);
|
|
191
|
+
try {
|
|
192
|
+
return node.map(item => walk(item, visitedRefs, chainDepth, nodeDepth + 1));
|
|
193
|
+
} finally {
|
|
194
|
+
visiting.delete(node);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
if (!isPlainObject(node)) return node;
|
|
198
|
+
if (visiting.has(node)) return { ...CYCLE_BREAK_SENTINEL };
|
|
199
|
+
visiting.add(node);
|
|
200
|
+
try {
|
|
201
|
+
const ref = typeof node.$ref === 'string' ? node.$ref : null;
|
|
202
|
+
// External refs and non-$ref nodes both pass through the same clone path.
|
|
203
|
+
if (ref === null || !ref.startsWith('#')) {
|
|
204
|
+
if (ref !== null) {
|
|
205
|
+
// Audit signal for security-sensitive deployments: a downstream
|
|
206
|
+
// resolver may fetch this and trigger SSRF or local-file disclosure.
|
|
207
|
+
logger.warn(`Leaving external $ref untouched: ${ref}`);
|
|
208
|
+
}
|
|
209
|
+
return cloneChildren(node, visitedRefs, chainDepth, nodeDepth);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
if (chainDepth >= MAX_REF_CHAIN_DEPTH) {
|
|
213
|
+
throw new JsonSchemaRefResolutionError(
|
|
214
|
+
`JSON Schema $ref chain exceeded depth cap (${MAX_REF_CHAIN_DEPTH}): ${ref}`,
|
|
215
|
+
{ meta: { ref }, possibleFixes: REF_RESOLUTION_FIXES }
|
|
216
|
+
);
|
|
217
|
+
}
|
|
218
|
+
if (visitedRefs.has(ref)) return { ...CYCLE_BREAK_SENTINEL };
|
|
219
|
+
|
|
220
|
+
const result = tryResolvePointer(root, ref);
|
|
221
|
+
let target: unknown;
|
|
222
|
+
if (result.kind === 'ok') {
|
|
223
|
+
target = result.value;
|
|
224
|
+
} else if (strategy === 'sentinel') {
|
|
225
|
+
// Lenient mode: replace the unresolved branch with the same
|
|
226
|
+
// permissive sentinel used for cycles, and notify the caller so
|
|
227
|
+
// they can emit a one-shot warn at the offending tool surface.
|
|
228
|
+
// The injected `description` gives the LLM an in-band signal that
|
|
229
|
+
// the branch is opaque; sibling-merge below will overwrite it with
|
|
230
|
+
// a caller-provided description if the original node has one.
|
|
231
|
+
onReplace?.(ref, result.reason);
|
|
232
|
+
target = { ...CYCLE_BREAK_SENTINEL, description: UNRESOLVED_REF_DESCRIPTION };
|
|
233
|
+
} else {
|
|
234
|
+
throwResolutionError(ref, result);
|
|
235
|
+
}
|
|
236
|
+
const nextRefs = new Set(visitedRefs).add(ref);
|
|
237
|
+
const resolved = walk(target, nextRefs, chainDepth + 1, nodeDepth + 1);
|
|
238
|
+
|
|
239
|
+
// Shallow-merge sibling keywords (Draft 2020-12 semantics: siblings win
|
|
240
|
+
// on collision). Draft 7 ignores siblings entirely, but the Composio
|
|
241
|
+
// tool surface admits both drafts so we honor siblings for safety.
|
|
242
|
+
const siblings: Record<string, unknown> = { ...node };
|
|
243
|
+
delete siblings.$ref;
|
|
244
|
+
if (Object.keys(siblings).length === 0 || !isPlainObject(resolved)) {
|
|
245
|
+
return resolved;
|
|
246
|
+
}
|
|
247
|
+
return { ...resolved, ...cloneChildren(siblings, visitedRefs, chainDepth, nodeDepth) };
|
|
248
|
+
} finally {
|
|
249
|
+
visiting.delete(node);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
const out = walk(root, new Set(), 0, 0);
|
|
254
|
+
if (isPlainObject(out)) {
|
|
255
|
+
delete out.$defs;
|
|
256
|
+
delete out.definitions;
|
|
257
|
+
}
|
|
258
|
+
return out as T;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Removes all non-required properties from the schema
|
|
263
|
+
*
|
|
264
|
+
* if no items are required, the schema is returned as is
|
|
265
|
+
* @param schema - The JSON schema to remove non-required properties from
|
|
266
|
+
* @returns The JSON schema with all non-required properties removed
|
|
267
|
+
*/
|
|
268
|
+
export const removeNonRequiredProperties = <
|
|
269
|
+
T extends {
|
|
270
|
+
type: 'object';
|
|
271
|
+
properties: Record<string, unknown>;
|
|
272
|
+
required?: string[];
|
|
273
|
+
additionalProperties?: boolean;
|
|
274
|
+
},
|
|
275
|
+
>(
|
|
276
|
+
schema: T
|
|
277
|
+
): T => {
|
|
278
|
+
if (schema && schema.type === 'object' && (schema.required as string[])?.length) {
|
|
279
|
+
schema.properties = Object.fromEntries(
|
|
280
|
+
Object.entries(schema.properties || {}).filter(([key]) =>
|
|
281
|
+
(schema.required as string[]).includes(key)
|
|
282
|
+
)
|
|
283
|
+
);
|
|
284
|
+
}
|
|
285
|
+
// In strict mode, we don't allow additional properties
|
|
286
|
+
schema.additionalProperties = false;
|
|
287
|
+
return schema as T;
|
|
288
|
+
};
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* Convert a JSON schema to a Zod schema
|
|
292
|
+
* @param jsonSchema - The JSON schema to convert
|
|
293
|
+
* @param strict - Eliminates all non-required properties from the schema
|
|
294
|
+
* @returns The Zod schema
|
|
295
|
+
*
|
|
296
|
+
* @throws {JsonSchemaToZodError} If the JSON schema is invalid
|
|
297
|
+
*
|
|
298
|
+
* @example
|
|
299
|
+
* ```ts
|
|
300
|
+
* const zodSchema = jsonSchemaToZodSchema({
|
|
301
|
+
* type: 'object',
|
|
302
|
+
* properties: {
|
|
303
|
+
* name: { type: 'string' },
|
|
304
|
+
* },
|
|
305
|
+
* });
|
|
306
|
+
*
|
|
307
|
+
* console.log(zodSchema);
|
|
308
|
+
* ```
|
|
309
|
+
*
|
|
310
|
+
* @example
|
|
311
|
+
* ```ts
|
|
312
|
+
* const zodSchema = jsonSchemaToZodSchema({
|
|
313
|
+
* type: 'object',
|
|
314
|
+
* properties: {
|
|
315
|
+
* name: { type: 'string' },
|
|
316
|
+
* age: { type: 'number' },
|
|
317
|
+
* },
|
|
318
|
+
* required: ['name'],
|
|
319
|
+
* }, { strict: true });
|
|
320
|
+
*
|
|
321
|
+
* console.log(zodSchema);
|
|
322
|
+
*
|
|
323
|
+
* // Output:
|
|
324
|
+
* // z.object({
|
|
325
|
+
* // name: z.string(),
|
|
326
|
+
* // })
|
|
327
|
+
* ```
|
|
328
|
+
*/
|
|
329
|
+
export function jsonSchemaToZodSchema<T extends z.ZodTypeAny>(
|
|
330
|
+
jsonSchema: Record<string, unknown>,
|
|
331
|
+
{ strict }: { strict?: boolean } = {
|
|
332
|
+
strict: false,
|
|
333
|
+
}
|
|
334
|
+
): T {
|
|
335
|
+
try {
|
|
336
|
+
let schema = jsonSchema;
|
|
337
|
+
// Remove all non-required properties from the schema if strict is true
|
|
338
|
+
if (strict && schema) {
|
|
339
|
+
schema = removeNonRequiredProperties(
|
|
340
|
+
schema as {
|
|
341
|
+
type: 'object';
|
|
342
|
+
properties: Record<string, unknown>;
|
|
343
|
+
required?: string[] | undefined;
|
|
344
|
+
}
|
|
345
|
+
);
|
|
346
|
+
}
|
|
347
|
+
// Convert the JSON schema properties to Zod schema
|
|
348
|
+
const zodSchema = jsonSchemaToZod(schema) as T;
|
|
349
|
+
return zodSchema;
|
|
350
|
+
} catch (error) {
|
|
351
|
+
throw new JsonSchemaToZodError('Failed to convert JSON Schema to Zod Schema', {
|
|
352
|
+
cause: error,
|
|
353
|
+
});
|
|
354
|
+
}
|
|
355
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import pc from 'picocolors';
|
|
2
|
+
import { getEnvVariable } from './env';
|
|
3
|
+
import { COMPOSIO_LOG_LEVEL } from './constants';
|
|
4
|
+
|
|
5
|
+
// Define log levels with corresponding priorities
|
|
6
|
+
export const LOG_LEVELS = {
|
|
7
|
+
silent: -1, // No logs
|
|
8
|
+
error: 0, // Highest priority - critical errors
|
|
9
|
+
warn: 1, // Warning messages
|
|
10
|
+
info: 2, // General information
|
|
11
|
+
debug: 3, // Debug information
|
|
12
|
+
} as const;
|
|
13
|
+
|
|
14
|
+
export type LogLevel = keyof typeof LOG_LEVELS;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Get the current log level from environment variables.
|
|
18
|
+
* Defaults to 'info' if not set or invalid.
|
|
19
|
+
* @returns {LogLevel} The current log level
|
|
20
|
+
*/
|
|
21
|
+
export const getLogLevel = (): LogLevel => {
|
|
22
|
+
const envLevel = (COMPOSIO_LOG_LEVEL ?? 'info')?.toLowerCase();
|
|
23
|
+
return envLevel && envLevel in LOG_LEVELS ? (envLevel as LogLevel) : 'info';
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
interface LoggerOptions {
|
|
27
|
+
level?: LogLevel;
|
|
28
|
+
includeTimestamp?: boolean;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
class Logger {
|
|
32
|
+
private readonly level: LogLevel;
|
|
33
|
+
private readonly includeTimestamp: boolean;
|
|
34
|
+
private readonly console: Console;
|
|
35
|
+
|
|
36
|
+
constructor(options: LoggerOptions = {}) {
|
|
37
|
+
this.level = options.level ?? getLogLevel();
|
|
38
|
+
this.includeTimestamp = options.includeTimestamp ?? true;
|
|
39
|
+
this.console = console;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
private formatMessage(args: unknown[]): string {
|
|
43
|
+
const formattedArgs = args
|
|
44
|
+
.map((arg, index) => {
|
|
45
|
+
if (typeof arg === 'object') {
|
|
46
|
+
return JSON.stringify(arg);
|
|
47
|
+
} else {
|
|
48
|
+
if (index === 0) {
|
|
49
|
+
if (args.length > 1) {
|
|
50
|
+
return pc.yellow(`${arg}`);
|
|
51
|
+
} else {
|
|
52
|
+
return String(arg);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return String(arg);
|
|
56
|
+
}
|
|
57
|
+
})
|
|
58
|
+
.join('\n');
|
|
59
|
+
|
|
60
|
+
if (!this.includeTimestamp) {
|
|
61
|
+
return formattedArgs;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const timestamp = new Date().toISOString();
|
|
65
|
+
return `${pc.gray(timestamp)} - ${formattedArgs}`;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
private shouldLog(level: LogLevel): boolean {
|
|
69
|
+
return LOG_LEVELS[level] <= LOG_LEVELS[this.level];
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
error(...args: unknown[]): void {
|
|
73
|
+
if (this.shouldLog('error')) {
|
|
74
|
+
this.console.error(this.formatMessage(args));
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
warn(...args: unknown[]): void {
|
|
79
|
+
if (this.shouldLog('warn')) {
|
|
80
|
+
this.console.warn(this.formatMessage(args));
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
info(...args: unknown[]): void {
|
|
85
|
+
if (this.shouldLog('info')) {
|
|
86
|
+
this.console.info(this.formatMessage(args));
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
debug(...args: unknown[]): void {
|
|
91
|
+
if (this.shouldLog('debug')) {
|
|
92
|
+
this.console.debug(this.formatMessage(args));
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// Create and export a default logger instance
|
|
98
|
+
const logger = new Logger();
|
|
99
|
+
export default logger;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Maps MIME types to file extensions.
|
|
3
|
+
* Used when deriving filenames from content-type headers (e.g. for URLs without path segments).
|
|
4
|
+
*/
|
|
5
|
+
export function getExtensionFromMimeType(mimeType: string): string {
|
|
6
|
+
const mimeToExt: Record<string, string> = {
|
|
7
|
+
'text/plain': 'txt',
|
|
8
|
+
'text/html': 'html',
|
|
9
|
+
'text/css': 'css',
|
|
10
|
+
'text/javascript': 'js',
|
|
11
|
+
'application/json': 'json',
|
|
12
|
+
'application/xml': 'xml',
|
|
13
|
+
'application/pdf': 'pdf',
|
|
14
|
+
'application/zip': 'zip',
|
|
15
|
+
'application/x-zip-compressed': 'zip',
|
|
16
|
+
'application/gzip': 'gz',
|
|
17
|
+
'application/octet-stream': 'bin',
|
|
18
|
+
'application/x-tar': 'tar',
|
|
19
|
+
'application/msword': 'doc',
|
|
20
|
+
'application/vnd.openxmlformats-officedocument.wordprocessingml.document': 'docx',
|
|
21
|
+
'application/vnd.ms-excel': 'xls',
|
|
22
|
+
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': 'xlsx',
|
|
23
|
+
'application/vnd.ms-powerpoint': 'ppt',
|
|
24
|
+
'application/vnd.openxmlformats-officedocument.presentationml.presentation': 'pptx',
|
|
25
|
+
'image/jpeg': 'jpg',
|
|
26
|
+
'image/jpg': 'jpg',
|
|
27
|
+
'image/png': 'png',
|
|
28
|
+
'image/gif': 'gif',
|
|
29
|
+
'image/svg+xml': 'svg',
|
|
30
|
+
'image/webp': 'webp',
|
|
31
|
+
'image/bmp': 'bmp',
|
|
32
|
+
'image/tiff': 'tiff',
|
|
33
|
+
'audio/mpeg': 'mp3',
|
|
34
|
+
'audio/wav': 'wav',
|
|
35
|
+
'audio/ogg': 'ogg',
|
|
36
|
+
'video/mp4': 'mp4',
|
|
37
|
+
'video/mpeg': 'mpeg',
|
|
38
|
+
'video/quicktime': 'mov',
|
|
39
|
+
'video/x-msvideo': 'avi',
|
|
40
|
+
'video/webm': 'webm',
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
const cleanMimeType = mimeType.split(';')[0].toLowerCase().trim();
|
|
44
|
+
|
|
45
|
+
if (mimeToExt[cleanMimeType]) {
|
|
46
|
+
return mimeToExt[cleanMimeType];
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const parts = cleanMimeType.split('/');
|
|
50
|
+
if (parts.length === 2) {
|
|
51
|
+
const subtype = parts[1].toLowerCase();
|
|
52
|
+
|
|
53
|
+
if (subtype.includes('+')) {
|
|
54
|
+
const plusParts = subtype.split('+');
|
|
55
|
+
const prefix = plusParts[0];
|
|
56
|
+
const suffix = plusParts[plusParts.length - 1];
|
|
57
|
+
|
|
58
|
+
const knownPrefixes = ['svg', 'atom', 'rss'];
|
|
59
|
+
if (knownPrefixes.includes(prefix)) {
|
|
60
|
+
return prefix;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const structuredSuffixes = ['json', 'xml', 'yaml', 'zip', 'gzip'];
|
|
64
|
+
if (structuredSuffixes.includes(suffix)) {
|
|
65
|
+
return suffix;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return suffix;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return subtype || 'txt';
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return 'bin';
|
|
75
|
+
}
|