@composio/core 0.12.0 → 0.13.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/{BaseProvider-BBnsv9kS.d.mts → BaseProvider-Brb3o86-.d.mts} +80 -80
- package/dist/{CustomTool-DEB4BQPl.mjs → CustomTool-D9qwyMrv.mjs} +1 -1
- package/dist/{CustomTool-DH18rVQu.d.mts → CustomTool-DLf7JeKt.d.mts} +1 -1
- package/dist/CustomTool-NpHpqdnU.d.mts +74 -0
- package/dist/{composio-CHnEggnv.d.mts → composio-BHSQwOUz.d.mts} +503 -319
- package/dist/composio-DfPD7BAi.d.mts +5666 -0
- package/dist/{customTool.types-DpIdOJjL.d.mts → customTool.types-9yFqPjdw.d.mts} +448 -76
- package/dist/customTool.types-D-6YqFdW.d.mts +2826 -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 +252 -0
- package/dist/docs/reference/sdk-reference/typescript/sessions.mdx +155 -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 +36 -6
- package/dist/index.mjs +455 -123
- package/dist/{jsonSchema-daqF3S3d.mjs → jsonSchema-CFKdONpP.mjs} +1 -1
- package/dist/{types-CaI5Z4PA.d.mts → types-BJLCRj9q.d.mts} +2 -2
- package/dist/types-CCh8qvui.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.d.mts +1 -1
- package/dist/utils/modifiers/FileToolModifier.node.mjs +2 -2
- package/dist/utils/modifiers/FileToolModifier.workerd.d.mts +1 -1
- 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/lib/toolRouterSessionDelete.ts +30 -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 +406 -0
- package/src/models/ToolRouterSession.ts +839 -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 +774 -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,676 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Factory functions for creating custom tools and toolkits.
|
|
3
|
+
*
|
|
4
|
+
* Usage:
|
|
5
|
+
* ```typescript
|
|
6
|
+
* import { experimental_createTool, experimental_createToolkit } from '@composio/core';
|
|
7
|
+
*
|
|
8
|
+
* const grep = createCustomTool('GREP', {
|
|
9
|
+
* name: 'Grep Search',
|
|
10
|
+
* description: 'Search for patterns in files',
|
|
11
|
+
* inputParams: z.object({ pattern: z.string() }),
|
|
12
|
+
* execute: async (input) => ({ matches: [] }),
|
|
13
|
+
* });
|
|
14
|
+
*
|
|
15
|
+
* const devTools = createCustomToolkit('DEV_TOOLS', {
|
|
16
|
+
* name: 'Dev Tools',
|
|
17
|
+
* description: 'Local dev utilities',
|
|
18
|
+
* tools: [grep],
|
|
19
|
+
* });
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
import {
|
|
23
|
+
CreateCustomToolBaseSchema,
|
|
24
|
+
CreateCustomToolkitBaseSchema,
|
|
25
|
+
CustomToolSlugSchema,
|
|
26
|
+
type AnyZodSchema,
|
|
27
|
+
type CreateCustomToolParams,
|
|
28
|
+
type CreateCustomToolkitParams,
|
|
29
|
+
type CustomTool,
|
|
30
|
+
type CustomToolkit,
|
|
31
|
+
type CustomToolExecuteFn,
|
|
32
|
+
type CustomToolsMap,
|
|
33
|
+
type CustomToolsMapEntry,
|
|
34
|
+
type CustomToolWireDefinition,
|
|
35
|
+
type CustomToolkitWireDefinition,
|
|
36
|
+
} from '../types/customTool.types';
|
|
37
|
+
import type {
|
|
38
|
+
SessionCreateResponse,
|
|
39
|
+
SessionRetrieveResponse,
|
|
40
|
+
} from '@composio/client/resources/tool-router/session/session.mjs';
|
|
41
|
+
import { ValidationError } from '../errors';
|
|
42
|
+
import { PRELOAD_TOOLS_ALL } from '../lib/toolRouterConstants';
|
|
43
|
+
import {
|
|
44
|
+
isZodObjectSchema,
|
|
45
|
+
zodObjectSchemaToJsonSchema,
|
|
46
|
+
zodSchemaToJsonSchema,
|
|
47
|
+
} from '../utils/zodSchema';
|
|
48
|
+
|
|
49
|
+
/** Prefix applied by the backend to local tool slugs for disambiguation. */
|
|
50
|
+
export const LOCAL_TOOL_PREFIX = 'LOCAL_';
|
|
51
|
+
|
|
52
|
+
/** Maximum allowed length for the final prefixed slug. */
|
|
53
|
+
const MAX_SLUG_LENGTH = 60;
|
|
54
|
+
|
|
55
|
+
type InputParamsSchema = {
|
|
56
|
+
definitions: {
|
|
57
|
+
input: {
|
|
58
|
+
type: string;
|
|
59
|
+
properties: Record<string, unknown>;
|
|
60
|
+
required?: string[];
|
|
61
|
+
};
|
|
62
|
+
};
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Compute the final slug length for a tool given its context.
|
|
67
|
+
* Returns the expected length of LOCAL_[TOOLKIT_]SLUG.
|
|
68
|
+
*/
|
|
69
|
+
function computeFinalSlugLength(toolSlug: string, toolkitSlug?: string): number {
|
|
70
|
+
// LOCAL_ + optional TOOLKIT_ + SLUG
|
|
71
|
+
return (
|
|
72
|
+
LOCAL_TOOL_PREFIX.length +
|
|
73
|
+
(toolkitSlug ? toolkitSlug.length + 1 : 0) + // +1 for underscore separator
|
|
74
|
+
toolSlug.length
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Validate that the final slug won't exceed the max length.
|
|
80
|
+
* Called early in createCustomTool/createCustomToolkit for fast feedback.
|
|
81
|
+
*/
|
|
82
|
+
function validateSlugLength(
|
|
83
|
+
toolSlug: string,
|
|
84
|
+
toolkitSlug: string | undefined,
|
|
85
|
+
context: string
|
|
86
|
+
): void {
|
|
87
|
+
const finalLength = computeFinalSlugLength(toolSlug, toolkitSlug);
|
|
88
|
+
if (finalLength > MAX_SLUG_LENGTH) {
|
|
89
|
+
const prefix = LOCAL_TOOL_PREFIX + (toolkitSlug ? `${toolkitSlug.toUpperCase()}_` : '');
|
|
90
|
+
const available = MAX_SLUG_LENGTH - prefix.length;
|
|
91
|
+
throw new ValidationError(
|
|
92
|
+
`${context}: slug "${toolSlug}" is too long. ` +
|
|
93
|
+
`With prefix "${prefix}", the final slug would be ${finalLength} characters (max ${MAX_SLUG_LENGTH}). ` +
|
|
94
|
+
`Shorten the slug to at most ${available} characters.`
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Create a custom tool for use in tool router sessions.
|
|
101
|
+
*
|
|
102
|
+
* The returned object is a lightweight reference containing the tool's metadata
|
|
103
|
+
* and execute function. Pass it to `composio.create(userId, { experimental: { customTools: [...] } })`
|
|
104
|
+
* to bind it to a session.
|
|
105
|
+
*
|
|
106
|
+
* Just return the result data from `execute`, or throw an error.
|
|
107
|
+
* The SDK wraps it into the standard response format internally.
|
|
108
|
+
*
|
|
109
|
+
* **Slug naming:** The slug you provide is automatically prefixed with `LOCAL_` when
|
|
110
|
+
* exposed to the agent (e.g. `'GREP'` becomes `LOCAL_GREP`). If the tool is inside a
|
|
111
|
+
* custom toolkit, the toolkit slug is also included (e.g. `LOCAL_DEV_TOOLS_GREP`).
|
|
112
|
+
* The `LOCAL_` prefix is reserved and cannot be used in your slug.
|
|
113
|
+
*
|
|
114
|
+
* @param slug - Unique tool identifier (alphanumeric, underscores, hyphens; no LOCAL_ or COMPOSIO_ prefix)
|
|
115
|
+
* @param options - Tool definition including name, schema, and execute function
|
|
116
|
+
* @returns A CustomTool to pass to session creation
|
|
117
|
+
*
|
|
118
|
+
* @example Standalone tool (no auth)
|
|
119
|
+
* ```typescript
|
|
120
|
+
* const grep = createCustomTool('GREP', {
|
|
121
|
+
* name: 'Grep Search',
|
|
122
|
+
* description: 'Search for patterns in files',
|
|
123
|
+
* inputParams: z.object({ pattern: z.string(), path: z.string() }),
|
|
124
|
+
* execute: async (input) => ({ matches: [] }),
|
|
125
|
+
* });
|
|
126
|
+
* ```
|
|
127
|
+
*
|
|
128
|
+
* @example Tool extending a Composio toolkit (inherits auth)
|
|
129
|
+
* ```typescript
|
|
130
|
+
* const getImportant = createCustomTool('GET_IMPORTANT_EMAILS', {
|
|
131
|
+
* name: 'Get Important Emails',
|
|
132
|
+
* description: 'Fetch high-priority emails',
|
|
133
|
+
* extendsToolkit: 'gmail',
|
|
134
|
+
* inputParams: z.object({ limit: z.number().default(10) }),
|
|
135
|
+
* execute: async (input, ctx) => {
|
|
136
|
+
* // Same response shape as session.execute(): { data, error, logId }
|
|
137
|
+
* const result = await ctx.execute('GMAIL_SEARCH', { query: 'is:important' });
|
|
138
|
+
* return { emails: result.data };
|
|
139
|
+
* },
|
|
140
|
+
* });
|
|
141
|
+
* ```
|
|
142
|
+
*/
|
|
143
|
+
export function createCustomTool<T extends AnyZodSchema>(
|
|
144
|
+
slug: string,
|
|
145
|
+
options: CreateCustomToolParams<T>
|
|
146
|
+
): CustomTool {
|
|
147
|
+
// Validate slug separately
|
|
148
|
+
const slugResult = CustomToolSlugSchema.safeParse(slug);
|
|
149
|
+
if (!slugResult.success) {
|
|
150
|
+
throw new ValidationError(`createCustomTool: ${slugResult.error.issues[0].message}`, {
|
|
151
|
+
cause: slugResult.error,
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// Validate string/scalar fields via Zod schema
|
|
156
|
+
const validated = CreateCustomToolBaseSchema.parse(options);
|
|
157
|
+
|
|
158
|
+
// Manual checks for fields Zod can't validate
|
|
159
|
+
if (!options.inputParams) {
|
|
160
|
+
throw new ValidationError('createCustomTool: inputParams is required');
|
|
161
|
+
}
|
|
162
|
+
if (!isZodObjectSchema(options.inputParams)) {
|
|
163
|
+
throw new ValidationError(
|
|
164
|
+
'createCustomTool: inputParams must be a z.object() schema. ' +
|
|
165
|
+
'Tool input parameters are always an object with named properties.'
|
|
166
|
+
);
|
|
167
|
+
}
|
|
168
|
+
if (typeof options.execute !== 'function') {
|
|
169
|
+
throw new ValidationError('createCustomTool: execute must be a function');
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// Early length validation (standalone or extension — we know both parts)
|
|
173
|
+
validateSlugLength(slug, validated.extendsToolkit, 'createCustomTool');
|
|
174
|
+
|
|
175
|
+
const { inputParams, execute } = options;
|
|
176
|
+
|
|
177
|
+
// Convert Zod input schema → JSON Schema (inputParams is guaranteed to be z.object)
|
|
178
|
+
const inputSchema = zodObjectSchemaToJsonSchema(inputParams, 'input');
|
|
179
|
+
|
|
180
|
+
// Convert Zod output schema → JSON Schema (if provided, any Zod type allowed)
|
|
181
|
+
let outputSchema: Record<string, unknown> | undefined;
|
|
182
|
+
if (options.outputParams) {
|
|
183
|
+
outputSchema = zodSchemaToJsonSchema(options.outputParams, 'output');
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
return {
|
|
187
|
+
slug: slugResult.data,
|
|
188
|
+
name: validated.name,
|
|
189
|
+
description: validated.description,
|
|
190
|
+
extendsToolkit: validated.extendsToolkit,
|
|
191
|
+
preload: validated.preload,
|
|
192
|
+
inputSchema,
|
|
193
|
+
outputSchema,
|
|
194
|
+
inputParams,
|
|
195
|
+
execute: execute as CustomToolExecuteFn<AnyZodSchema>,
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Create a custom toolkit that groups related tools.
|
|
201
|
+
*
|
|
202
|
+
* Tools passed here must NOT have `extendsToolkit` set — they inherit the toolkit identity instead.
|
|
203
|
+
*
|
|
204
|
+
* **Slug naming:** The toolkit slug becomes part of the final tool slug exposed to the agent.
|
|
205
|
+
* For example, a toolkit `'DEV_TOOLS'` with a tool `'GREP'` produces `LOCAL_DEV_TOOLS_GREP`.
|
|
206
|
+
* The `LOCAL_` prefix is reserved and cannot be used in your slug.
|
|
207
|
+
*
|
|
208
|
+
* @param slug - Unique toolkit identifier (alphanumeric, underscores, hyphens; no LOCAL_ or COMPOSIO_ prefix)
|
|
209
|
+
* @param options - Toolkit definition including name, description, and tools
|
|
210
|
+
* @returns A CustomToolkit to pass to session creation
|
|
211
|
+
*
|
|
212
|
+
* @example
|
|
213
|
+
* ```typescript
|
|
214
|
+
* const devTools = createCustomToolkit('DEV_TOOLS', {
|
|
215
|
+
* name: 'Dev Tools',
|
|
216
|
+
* description: 'Local dev utilities',
|
|
217
|
+
* tools: [grepTool, sedTool],
|
|
218
|
+
* });
|
|
219
|
+
* ```
|
|
220
|
+
*/
|
|
221
|
+
export function createCustomToolkit(
|
|
222
|
+
slug: string,
|
|
223
|
+
options: CreateCustomToolkitParams
|
|
224
|
+
): CustomToolkit {
|
|
225
|
+
// Validate slug
|
|
226
|
+
const slugResult = CustomToolSlugSchema.safeParse(slug);
|
|
227
|
+
if (!slugResult.success) {
|
|
228
|
+
throw new ValidationError(`createCustomToolkit: ${slugResult.error.issues[0].message}`, {
|
|
229
|
+
cause: slugResult.error,
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// Validate name/description
|
|
234
|
+
const validated = CreateCustomToolkitBaseSchema.parse(options);
|
|
235
|
+
|
|
236
|
+
// Non-empty tools required
|
|
237
|
+
if (!options.tools?.length) {
|
|
238
|
+
throw new ValidationError('createCustomToolkit: at least one tool is required');
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// Validate each tool
|
|
242
|
+
for (const tool of options.tools) {
|
|
243
|
+
// Reject tools with extendsToolkit
|
|
244
|
+
if (tool.extendsToolkit) {
|
|
245
|
+
throw new ValidationError(
|
|
246
|
+
`createCustomToolkit: tool "${tool.slug}" has extendsToolkit set. ` +
|
|
247
|
+
`Tools in a custom toolkit must not use extendsToolkit — they inherit the toolkit identity instead.`
|
|
248
|
+
);
|
|
249
|
+
}
|
|
250
|
+
// Early length validation — we know both toolkit slug and tool slug
|
|
251
|
+
validateSlugLength(tool.slug, slug, `createCustomToolkit("${slug}")`);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
return {
|
|
255
|
+
slug: slugResult.data,
|
|
256
|
+
name: validated.name,
|
|
257
|
+
description: validated.description,
|
|
258
|
+
preload: validated.preload,
|
|
259
|
+
tools: options.tools,
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* Build the final slug for a custom tool given its context.
|
|
265
|
+
* @internal
|
|
266
|
+
*/
|
|
267
|
+
function buildFinalSlug(toolSlug: string, toolkitSlug?: string): string {
|
|
268
|
+
const upper = toolSlug.toUpperCase();
|
|
269
|
+
return toolkitSlug
|
|
270
|
+
? `${LOCAL_TOOL_PREFIX}${toolkitSlug.toUpperCase()}_${upper}`
|
|
271
|
+
: `${LOCAL_TOOL_PREFIX}${upper}`;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
const qualifiedOriginalSlugKey = (toolkit: string | undefined, originalSlug: string): string =>
|
|
275
|
+
`${toolkit?.toUpperCase() ?? ''}::${originalSlug.toUpperCase()}`;
|
|
276
|
+
|
|
277
|
+
const canShareOriginalSlug = (existing: CustomToolsMapEntry, next: CustomToolsMapEntry): boolean =>
|
|
278
|
+
!!existing.toolkit &&
|
|
279
|
+
!!next.toolkit &&
|
|
280
|
+
existing.toolkit.toLowerCase() !== next.toolkit.toLowerCase();
|
|
281
|
+
|
|
282
|
+
const addOriginalSlugAlias = (params: {
|
|
283
|
+
byOriginalSlug: Map<string, CustomToolsMapEntry>;
|
|
284
|
+
ambiguousOriginalSlugs: Set<string>;
|
|
285
|
+
originalSlug: string;
|
|
286
|
+
entry: CustomToolsMapEntry;
|
|
287
|
+
}) => {
|
|
288
|
+
const originalSlugKey = params.originalSlug.toUpperCase();
|
|
289
|
+
if (params.ambiguousOriginalSlugs.has(originalSlugKey)) {
|
|
290
|
+
return;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
const existing = params.byOriginalSlug.get(originalSlugKey);
|
|
294
|
+
if (!existing) {
|
|
295
|
+
params.byOriginalSlug.set(originalSlugKey, params.entry);
|
|
296
|
+
return;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
if (existing.finalSlug.toUpperCase() === params.entry.finalSlug.toUpperCase()) {
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
if (canShareOriginalSlug(existing, params.entry)) {
|
|
304
|
+
params.byOriginalSlug.delete(originalSlugKey);
|
|
305
|
+
params.ambiguousOriginalSlugs.add(originalSlugKey);
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
throw new ValidationError(
|
|
310
|
+
`Custom tool slug collision: original slug "${params.originalSlug}" maps to multiple final slugs. ` +
|
|
311
|
+
`"${existing.finalSlug}" and "${params.entry.finalSlug}" both resolve from "${originalSlugKey}".`
|
|
312
|
+
);
|
|
313
|
+
};
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* Build a CustomToolsMap from custom tools and toolkits.
|
|
317
|
+
* Used internally by ToolRouter.create() to construct the per-session routing map.
|
|
318
|
+
*
|
|
319
|
+
* @internal
|
|
320
|
+
* @param tools - Standalone custom tools
|
|
321
|
+
* @param toolkits - Custom toolkits containing grouped tools
|
|
322
|
+
* @returns Maps for O(1) lookup by both final and original slug
|
|
323
|
+
* @throws If duplicate slugs or cross-group collisions
|
|
324
|
+
*/
|
|
325
|
+
export function buildCustomToolsMap(
|
|
326
|
+
tools: CustomTool[],
|
|
327
|
+
toolkits?: CustomToolkit[]
|
|
328
|
+
): CustomToolsMap {
|
|
329
|
+
const byFinalSlug = new Map<string, CustomToolsMapEntry>();
|
|
330
|
+
const byOriginalSlug = new Map<string, CustomToolsMapEntry>();
|
|
331
|
+
const byToolkitAndOriginalSlug = new Map<string, CustomToolsMapEntry>();
|
|
332
|
+
const ambiguousOriginalSlugs = new Set<string>();
|
|
333
|
+
|
|
334
|
+
const addEntry = (handle: CustomTool, finalSlug: string, toolkit?: string) => {
|
|
335
|
+
const originalSlug = handle.slug.toUpperCase();
|
|
336
|
+
// Custom tool slugs are matched case-insensitively across local and response maps.
|
|
337
|
+
const finalSlugKey = finalSlug.toUpperCase();
|
|
338
|
+
|
|
339
|
+
// Length validated early in createCustomTool/createCustomToolkit, but check as safety net
|
|
340
|
+
if (finalSlug.length > MAX_SLUG_LENGTH) {
|
|
341
|
+
throw new ValidationError(
|
|
342
|
+
`Custom tool slug "${handle.slug}" produces final slug "${finalSlug}" ` +
|
|
343
|
+
`which exceeds ${MAX_SLUG_LENGTH} characters.`
|
|
344
|
+
);
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
// Check cross-group collisions on final slug
|
|
348
|
+
if (byFinalSlug.has(finalSlugKey)) {
|
|
349
|
+
throw new ValidationError(
|
|
350
|
+
`Custom tool slug collision: "${finalSlug}" is already registered.`
|
|
351
|
+
);
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
const qualifiedKey = qualifiedOriginalSlugKey(toolkit, originalSlug);
|
|
355
|
+
if (byToolkitAndOriginalSlug.has(qualifiedKey)) {
|
|
356
|
+
throw new ValidationError(
|
|
357
|
+
`Custom tool slug collision: original slug "${handle.slug}" is already registered for toolkit "${toolkit ?? 'custom'}".`
|
|
358
|
+
);
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
const entry: CustomToolsMapEntry = { handle, finalSlug, toolkit };
|
|
362
|
+
byFinalSlug.set(finalSlugKey, entry);
|
|
363
|
+
byToolkitAndOriginalSlug.set(qualifiedKey, entry);
|
|
364
|
+
addOriginalSlugAlias({ byOriginalSlug, ambiguousOriginalSlugs, originalSlug, entry });
|
|
365
|
+
};
|
|
366
|
+
|
|
367
|
+
// Process standalone tools
|
|
368
|
+
for (const handle of tools) {
|
|
369
|
+
addEntry(handle, buildFinalSlug(handle.slug, handle.extendsToolkit), handle.extendsToolkit);
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
// Process toolkit tools
|
|
373
|
+
if (toolkits) {
|
|
374
|
+
for (const toolkit of toolkits) {
|
|
375
|
+
for (const handle of toolkit.tools) {
|
|
376
|
+
addEntry(handle, buildFinalSlug(handle.slug, toolkit.slug), toolkit.slug);
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
return {
|
|
382
|
+
byFinalSlug,
|
|
383
|
+
byOriginalSlug,
|
|
384
|
+
byToolkitAndOriginalSlug,
|
|
385
|
+
...(ambiguousOriginalSlugs.size ? { ambiguousOriginalSlugs } : {}),
|
|
386
|
+
toolkits,
|
|
387
|
+
tools: tools.length ? tools : undefined,
|
|
388
|
+
};
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
/**
|
|
392
|
+
* Serialize custom tools into the format expected by the backend session creation API.
|
|
393
|
+
*
|
|
394
|
+
* Maps `extendsToolkit` → `extends_toolkit` for backend. Omitted for standalone tools.
|
|
395
|
+
*
|
|
396
|
+
* @internal
|
|
397
|
+
* @param tools - The custom tools to serialize
|
|
398
|
+
* @returns Array of CustomToolDefinition for the API payload
|
|
399
|
+
*/
|
|
400
|
+
type CustomToolSerializationOptions = {
|
|
401
|
+
defaultPreload?: boolean;
|
|
402
|
+
};
|
|
403
|
+
|
|
404
|
+
function shouldSerializePreload(
|
|
405
|
+
preload: boolean | undefined,
|
|
406
|
+
inheritedPreload: boolean | undefined,
|
|
407
|
+
defaultPreload: boolean
|
|
408
|
+
): boolean | undefined {
|
|
409
|
+
const inheritedOrDefault = inheritedPreload ?? defaultPreload;
|
|
410
|
+
if (preload !== undefined) {
|
|
411
|
+
return preload || inheritedOrDefault ? preload : undefined;
|
|
412
|
+
}
|
|
413
|
+
if (inheritedPreload !== undefined) {
|
|
414
|
+
return inheritedPreload || defaultPreload ? inheritedPreload : undefined;
|
|
415
|
+
}
|
|
416
|
+
return defaultPreload ? true : undefined;
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
function preloadWireProperty(
|
|
420
|
+
preload: boolean | undefined,
|
|
421
|
+
inheritedPreload: boolean | undefined,
|
|
422
|
+
defaultPreload: boolean
|
|
423
|
+
): { preload?: boolean } {
|
|
424
|
+
const serializedPreload = shouldSerializePreload(preload, inheritedPreload, defaultPreload);
|
|
425
|
+
return serializedPreload === undefined ? {} : { preload: serializedPreload };
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
export function serializeCustomTools(
|
|
429
|
+
tools: CustomTool[],
|
|
430
|
+
options: CustomToolSerializationOptions = {}
|
|
431
|
+
): CustomToolWireDefinition[] {
|
|
432
|
+
const defaultPreload = options.defaultPreload ?? false;
|
|
433
|
+
return tools.map(handle => ({
|
|
434
|
+
slug: handle.slug,
|
|
435
|
+
name: handle.name,
|
|
436
|
+
description: handle.description,
|
|
437
|
+
input_schema: handle.inputSchema,
|
|
438
|
+
...(handle.outputSchema ? { output_schema: handle.outputSchema } : {}),
|
|
439
|
+
...(handle.extendsToolkit ? { extends_toolkit: handle.extendsToolkit } : {}),
|
|
440
|
+
...preloadWireProperty(handle.preload, undefined, defaultPreload),
|
|
441
|
+
}));
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
/**
|
|
445
|
+
* Serialize custom toolkits into the format expected by the backend session creation API.
|
|
446
|
+
*
|
|
447
|
+
* @internal
|
|
448
|
+
* @param toolkits - The custom toolkits to serialize
|
|
449
|
+
* @returns Array of CustomToolkitDefinition for the API payload
|
|
450
|
+
*/
|
|
451
|
+
export function serializeCustomToolkits(
|
|
452
|
+
toolkits: CustomToolkit[],
|
|
453
|
+
options: CustomToolSerializationOptions = {}
|
|
454
|
+
): CustomToolkitWireDefinition[] {
|
|
455
|
+
const defaultPreload = options.defaultPreload ?? false;
|
|
456
|
+
return toolkits.map(tk => ({
|
|
457
|
+
slug: tk.slug,
|
|
458
|
+
name: tk.name,
|
|
459
|
+
description: tk.description,
|
|
460
|
+
...preloadWireProperty(tk.preload, undefined, defaultPreload),
|
|
461
|
+
tools: tk.tools.map(t => ({
|
|
462
|
+
slug: t.slug,
|
|
463
|
+
name: t.name,
|
|
464
|
+
description: t.description,
|
|
465
|
+
input_schema: t.inputSchema,
|
|
466
|
+
...(t.outputSchema ? { output_schema: t.outputSchema } : {}),
|
|
467
|
+
...preloadWireProperty(t.preload, tk.preload, defaultPreload),
|
|
468
|
+
})),
|
|
469
|
+
}));
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
/**
|
|
473
|
+
* Build a CustomToolsMap using the slug/original_slug mapping from the session create response.
|
|
474
|
+
* This uses the backend's authoritative prefixed slugs instead of computing them client-side.
|
|
475
|
+
*
|
|
476
|
+
* @internal
|
|
477
|
+
* @param tools - The original custom tools passed to session creation
|
|
478
|
+
* @param toolkits - The original custom toolkits passed to session creation
|
|
479
|
+
* @param experimental - The experimental section from the session create response
|
|
480
|
+
* @returns Maps for O(1) lookup by both final and original slug
|
|
481
|
+
*/
|
|
482
|
+
export function buildCustomToolsMapFromResponse(
|
|
483
|
+
tools: CustomTool[],
|
|
484
|
+
toolkits: CustomToolkit[] | undefined,
|
|
485
|
+
experimental:
|
|
486
|
+
| SessionCreateResponse['experimental']
|
|
487
|
+
| SessionRetrieveResponse['experimental']
|
|
488
|
+
| undefined
|
|
489
|
+
): CustomToolsMap {
|
|
490
|
+
const byFinalSlug = new Map<string, CustomToolsMapEntry>();
|
|
491
|
+
const byOriginalSlug = new Map<string, CustomToolsMapEntry>();
|
|
492
|
+
const byToolkitAndOriginalSlug = new Map<string, CustomToolsMapEntry>();
|
|
493
|
+
const ambiguousOriginalSlugs = new Set<string>();
|
|
494
|
+
|
|
495
|
+
type HandleMatch = { handle: CustomTool; toolkit?: string };
|
|
496
|
+
const handlesByQualifiedOriginalSlug = new Map<string, HandleMatch>();
|
|
497
|
+
const handlesByOriginalSlug = new Map<string, HandleMatch[]>();
|
|
498
|
+
|
|
499
|
+
const registerHandle = (handle: CustomTool, toolkit?: string) => {
|
|
500
|
+
const originalSlug = handle.slug.toUpperCase();
|
|
501
|
+
const match = { handle, toolkit };
|
|
502
|
+
handlesByQualifiedOriginalSlug.set(qualifiedOriginalSlugKey(toolkit, originalSlug), match);
|
|
503
|
+
const existing = handlesByOriginalSlug.get(originalSlug) ?? [];
|
|
504
|
+
existing.push(match);
|
|
505
|
+
handlesByOriginalSlug.set(originalSlug, existing);
|
|
506
|
+
};
|
|
507
|
+
|
|
508
|
+
for (const handle of tools) {
|
|
509
|
+
registerHandle(handle, handle.extendsToolkit);
|
|
510
|
+
}
|
|
511
|
+
if (toolkits) {
|
|
512
|
+
for (const tk of toolkits) {
|
|
513
|
+
for (const handle of tk.tools) {
|
|
514
|
+
registerHandle(handle, tk.slug);
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
const findHandle = (originalSlug: string, toolkit?: string): HandleMatch | undefined => {
|
|
520
|
+
const originalSlugKey = originalSlug.toUpperCase();
|
|
521
|
+
const qualifiedMatch = handlesByQualifiedOriginalSlug.get(
|
|
522
|
+
qualifiedOriginalSlugKey(toolkit, originalSlugKey)
|
|
523
|
+
);
|
|
524
|
+
if (qualifiedMatch) return qualifiedMatch;
|
|
525
|
+
|
|
526
|
+
const bareMatches = handlesByOriginalSlug.get(originalSlugKey) ?? [];
|
|
527
|
+
return bareMatches.length === 1 ? bareMatches[0] : undefined;
|
|
528
|
+
};
|
|
529
|
+
|
|
530
|
+
const addEntry = (finalSlug: string, originalSlug: string, toolkit?: string | null) => {
|
|
531
|
+
const resolvedToolkit = toolkit ?? undefined;
|
|
532
|
+
const match = findHandle(originalSlug, resolvedToolkit);
|
|
533
|
+
if (!match) return; // Response tool not found in our handles (shouldn't happen)
|
|
534
|
+
|
|
535
|
+
const finalSlugKey = finalSlug.toUpperCase();
|
|
536
|
+
if (byFinalSlug.has(finalSlugKey)) {
|
|
537
|
+
throw new ValidationError(
|
|
538
|
+
`Custom tool slug collision: "${finalSlug}" is already registered.`
|
|
539
|
+
);
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
const entry: CustomToolsMapEntry = {
|
|
543
|
+
handle: match.handle,
|
|
544
|
+
finalSlug,
|
|
545
|
+
toolkit: resolvedToolkit ?? match.toolkit,
|
|
546
|
+
};
|
|
547
|
+
byFinalSlug.set(finalSlugKey, entry);
|
|
548
|
+
byToolkitAndOriginalSlug.set(qualifiedOriginalSlugKey(entry.toolkit, originalSlug), entry);
|
|
549
|
+
addOriginalSlugAlias({ byOriginalSlug, ambiguousOriginalSlugs, originalSlug, entry });
|
|
550
|
+
};
|
|
551
|
+
|
|
552
|
+
// Map standalone custom tools from response
|
|
553
|
+
if (experimental?.custom_tools) {
|
|
554
|
+
for (const ct of experimental.custom_tools) {
|
|
555
|
+
addEntry(ct.slug, ct.original_slug, ct.extends_toolkit);
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
// Map toolkit custom tools from response
|
|
560
|
+
if (experimental?.custom_toolkits) {
|
|
561
|
+
for (const ctk of experimental.custom_toolkits) {
|
|
562
|
+
for (const ct of ctk.tools) {
|
|
563
|
+
addEntry(ct.slug, ct.original_slug, ctk.slug);
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
return {
|
|
569
|
+
byFinalSlug,
|
|
570
|
+
byOriginalSlug,
|
|
571
|
+
byToolkitAndOriginalSlug,
|
|
572
|
+
...(ambiguousOriginalSlugs.size ? { ambiguousOriginalSlugs } : {}),
|
|
573
|
+
toolkits,
|
|
574
|
+
tools: tools.length ? tools : undefined,
|
|
575
|
+
};
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
/**
|
|
579
|
+
* Find a custom tool entry by final slug only.
|
|
580
|
+
*
|
|
581
|
+
* @internal
|
|
582
|
+
*/
|
|
583
|
+
export function findCustomToolMapEntryByFinalSlug(
|
|
584
|
+
customToolsMap: CustomToolsMap | undefined,
|
|
585
|
+
slug: string
|
|
586
|
+
): CustomToolsMapEntry | undefined {
|
|
587
|
+
return customToolsMap?.byFinalSlug.get(slug.toUpperCase());
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
/**
|
|
591
|
+
* Find a custom toolkit tool entry by toolkit slug and original tool slug.
|
|
592
|
+
*
|
|
593
|
+
* @internal
|
|
594
|
+
*/
|
|
595
|
+
export function findCustomToolMapEntryByToolkitAndOriginalSlug(
|
|
596
|
+
customToolsMap: CustomToolsMap | undefined,
|
|
597
|
+
toolkit: string | undefined,
|
|
598
|
+
slug: string
|
|
599
|
+
): CustomToolsMapEntry | undefined {
|
|
600
|
+
return customToolsMap?.byToolkitAndOriginalSlug?.get(qualifiedOriginalSlugKey(toolkit, slug));
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
/**
|
|
604
|
+
* Reject legacy attempts to preload custom tools through top-level preload.tools.
|
|
605
|
+
*
|
|
606
|
+
* Custom tool direct exposure is controlled by CustomTool.preload /
|
|
607
|
+
* CustomToolkit.preload so it follows the current SDK-provided custom
|
|
608
|
+
* definitions instead of stale session config.
|
|
609
|
+
*
|
|
610
|
+
* @internal
|
|
611
|
+
*/
|
|
612
|
+
export function assertNoCustomToolSlugsInPreload(
|
|
613
|
+
preloadTools: readonly string[] | typeof PRELOAD_TOOLS_ALL | undefined,
|
|
614
|
+
customToolsMap: CustomToolsMap | undefined
|
|
615
|
+
): void {
|
|
616
|
+
if (!preloadTools || preloadTools === PRELOAD_TOOLS_ALL) {
|
|
617
|
+
return;
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
const customPreloadSlugs = preloadTools.filter(slug => {
|
|
621
|
+
const normalized = slug.toUpperCase();
|
|
622
|
+
return (
|
|
623
|
+
// Top-level preload.tools is only for Composio-managed tool slugs.
|
|
624
|
+
// Custom tools use `preload: true` on their SDK definitions instead.
|
|
625
|
+
normalized.startsWith(LOCAL_TOOL_PREFIX) ||
|
|
626
|
+
customToolsMap?.byOriginalSlug.has(normalized) ||
|
|
627
|
+
customToolsMap?.ambiguousOriginalSlugs?.has(normalized) ||
|
|
628
|
+
customToolsMap?.byFinalSlug.has(normalized)
|
|
629
|
+
);
|
|
630
|
+
});
|
|
631
|
+
|
|
632
|
+
if (customPreloadSlugs.length) {
|
|
633
|
+
throw new ValidationError(
|
|
634
|
+
`Custom tool slugs are not supported in preload.tools: ${customPreloadSlugs.join(
|
|
635
|
+
', '
|
|
636
|
+
)}. Set preload: true on the SDK custom tool or custom toolkit definition instead.`
|
|
637
|
+
);
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
/**
|
|
642
|
+
* Resolve custom tools that the SDK should expose directly from session.tools().
|
|
643
|
+
*
|
|
644
|
+
* @internal
|
|
645
|
+
*/
|
|
646
|
+
export function getPreloadedCustomToolSlugs(
|
|
647
|
+
customToolsMap: CustomToolsMap | undefined,
|
|
648
|
+
defaultPreload = false
|
|
649
|
+
): string[] {
|
|
650
|
+
if (!customToolsMap) {
|
|
651
|
+
return [];
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
const seen = new Set<string>();
|
|
655
|
+
const customToolSlugs: string[] = [];
|
|
656
|
+
|
|
657
|
+
for (const entry of customToolsMap.byFinalSlug.values()) {
|
|
658
|
+
const toolkit = customToolsMap.toolkits?.find(
|
|
659
|
+
tk => entry.toolkit && tk.slug.toLowerCase() === entry.toolkit.toLowerCase()
|
|
660
|
+
);
|
|
661
|
+
const shouldPreload = entry.handle.preload ?? toolkit?.preload ?? defaultPreload;
|
|
662
|
+
if (!shouldPreload) {
|
|
663
|
+
continue;
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
const finalSlugKey = entry.finalSlug.toUpperCase();
|
|
667
|
+
if (seen.has(finalSlugKey)) {
|
|
668
|
+
continue;
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
seen.add(finalSlugKey);
|
|
672
|
+
customToolSlugs.push(entry.finalSlug);
|
|
673
|
+
}
|
|
674
|
+
|
|
675
|
+
return customToolSlugs;
|
|
676
|
+
}
|