@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.
Files changed (144) hide show
  1. package/dist/BaseProvider-Brb3o86-.d.mts +29425 -0
  2. package/dist/{CustomTool-DEB4BQPl.mjs → CustomTool-D9qwyMrv.mjs} +1 -1
  3. package/dist/{CustomTool-DH18rVQu.d.mts → CustomTool-Dmi6ti3I.d.mts} +1 -1
  4. package/dist/CustomTool-DrJpOhY2.d.mts +74 -0
  5. package/dist/{composio-CHnEggnv.d.mts → composio-DHIKRdeG.d.mts} +253 -37
  6. package/dist/composio-vMwhASRc.d.mts +5698 -0
  7. package/dist/{customTool.types-DpIdOJjL.d.mts → customTool.types-CKH2R2ia.d.mts} +344 -25
  8. package/dist/customTool.types-DXin7Cfc.d.mts +2773 -0
  9. package/dist/docs/error-consistency.md +97 -0
  10. package/dist/docs/reference/sdk-reference/typescript/auth-configs.mdx +306 -0
  11. package/dist/docs/reference/sdk-reference/typescript/composio.mdx +205 -0
  12. package/dist/docs/reference/sdk-reference/typescript/connected-accounts.mdx +488 -0
  13. package/dist/docs/reference/sdk-reference/typescript/experimental.mdx +94 -0
  14. package/dist/docs/reference/sdk-reference/typescript/index.mdx +67 -0
  15. package/dist/docs/reference/sdk-reference/typescript/mcp.mdx +304 -0
  16. package/dist/docs/reference/sdk-reference/typescript/meta.json +17 -0
  17. package/dist/docs/reference/sdk-reference/typescript/remote-file.mdx +107 -0
  18. package/dist/docs/reference/sdk-reference/typescript/session-files.mdx +163 -0
  19. package/dist/docs/reference/sdk-reference/typescript/session.mdx +229 -0
  20. package/dist/docs/reference/sdk-reference/typescript/sessions.mdx +131 -0
  21. package/dist/docs/reference/sdk-reference/typescript/toolkits.mdx +169 -0
  22. package/dist/docs/reference/sdk-reference/typescript/tools.mdx +453 -0
  23. package/dist/docs/reference/sdk-reference/typescript/triggers.mdx +434 -0
  24. package/dist/{errors-D5f4pfvI.mjs → errors-DSIlYfl5.mjs} +2 -2
  25. package/dist/experimental/index.d.mts +2 -2
  26. package/dist/experimental/index.mjs +1 -1
  27. package/dist/index.d.mts +27 -4
  28. package/dist/index.mjs +423 -123
  29. package/dist/{jsonSchema-daqF3S3d.mjs → jsonSchema-CFKdONpP.mjs} +1 -1
  30. package/dist/{types-CaI5Z4PA.d.mts → types-D4Id3wXA.d.mts} +1 -1
  31. package/dist/types-qTbYYBJw.d.mts +12 -0
  32. package/dist/utils/config-defaults/ConfigDefaults.node.d.mts +1 -1
  33. package/dist/utils/config-defaults/ConfigDefaults.workerd.d.mts +1 -1
  34. package/dist/utils/modifiers/FileToolModifier.node.mjs +2 -2
  35. package/docs/error-consistency.md +97 -0
  36. package/package.json +4 -3
  37. package/src/composio.ts +509 -0
  38. package/src/errors/AuthConfigErrors.ts +23 -0
  39. package/src/errors/ComposioError.ts +408 -0
  40. package/src/errors/ConnectedAccountsErrors.ts +158 -0
  41. package/src/errors/ConnectionRequestErrors.ts +32 -0
  42. package/src/errors/FileModifierErrors.ts +100 -0
  43. package/src/errors/PusherErrors.ts +28 -0
  44. package/src/errors/RemoteFileErrors.ts +62 -0
  45. package/src/errors/SDKErrors.ts +92 -0
  46. package/src/errors/ToolErrors.ts +244 -0
  47. package/src/errors/ToolkitErrors.ts +39 -0
  48. package/src/errors/TriggerErrors.ts +120 -0
  49. package/src/errors/ValidationErrors.ts +99 -0
  50. package/src/errors/index.ts +41 -0
  51. package/src/experimental/index.ts +28 -0
  52. package/src/index.ts +64 -0
  53. package/src/lib/toolRouterConstants.ts +1 -0
  54. package/src/lib/toolRouterParams.ts +306 -0
  55. package/src/models/AuthConfigs.ts +398 -0
  56. package/src/models/AuthScheme.ts +206 -0
  57. package/src/models/ConnectedAccounts.ts +735 -0
  58. package/src/models/ConnectionRequest.ts +150 -0
  59. package/src/models/CustomTool.ts +676 -0
  60. package/src/models/Experimental.ts +202 -0
  61. package/src/models/Files.node.ts +93 -0
  62. package/src/models/Files.workerd.ts +49 -0
  63. package/src/models/MCP.ts +478 -0
  64. package/src/models/RemoteFile.ts +172 -0
  65. package/src/models/SessionContext.ts +147 -0
  66. package/src/models/Sessions.ts +21 -0
  67. package/src/models/ToolRouter.ts +391 -0
  68. package/src/models/ToolRouterSession.ts +827 -0
  69. package/src/models/ToolRouterSessionFileMount.ts +389 -0
  70. package/src/models/Toolkits.ts +412 -0
  71. package/src/models/Tools.ts +1260 -0
  72. package/src/models/Triggers.ts +1381 -0
  73. package/src/models/customToolExecution.ts +118 -0
  74. package/src/models/inlineCustomToolsPayload.ts +17 -0
  75. package/src/models/proxyParamsTransform.ts +26 -0
  76. package/src/provider/BaseProvider.ts +138 -0
  77. package/src/provider/ComposioProvider.ts +29 -0
  78. package/src/provider/OpenAIProvider.ts +547 -0
  79. package/src/services/internal/InternalService.ts +50 -0
  80. package/src/services/internal/InternalService.types.ts +14 -0
  81. package/src/services/pusher/Pusher.ts +230 -0
  82. package/src/services/telemetry/TelemetryService.ts +48 -0
  83. package/src/services/telemetry/TelemetryService.types.ts +67 -0
  84. package/src/telemetry/BatchProcessor.ts +77 -0
  85. package/src/telemetry/README.md +123 -0
  86. package/src/telemetry/Telemetry.ts +356 -0
  87. package/src/types/ToolRouterSessionFilesMount.types.ts +96 -0
  88. package/src/types/authConfigs.types.ts +174 -0
  89. package/src/types/composio.types.ts +1 -0
  90. package/src/types/connectedAccountAuthStates.types.ts +852 -0
  91. package/src/types/connectedAccounts.types.ts +395 -0
  92. package/src/types/connectionRequest.types.ts +16 -0
  93. package/src/types/customTool.types.ts +261 -0
  94. package/src/types/files.types.ts +15 -0
  95. package/src/types/mcp.experimental.types.ts +163 -0
  96. package/src/types/mcp.types.ts +362 -0
  97. package/src/types/modifiers.types.ts +663 -0
  98. package/src/types/org.types.ts +0 -0
  99. package/src/types/provider.types.ts +41 -0
  100. package/src/types/pusher.types.ts +18 -0
  101. package/src/types/requestOptions.types.ts +62 -0
  102. package/src/types/telemetry.types.ts +89 -0
  103. package/src/types/tool.types.ts +408 -0
  104. package/src/types/toolRouter.types.ts +761 -0
  105. package/src/types/toolkit.types.ts +130 -0
  106. package/src/types/triggers.types.ts +401 -0
  107. package/src/types/webhookEvents.types.ts +219 -0
  108. package/src/utils/buffer.ts +30 -0
  109. package/src/utils/cancellation.ts +22 -0
  110. package/src/utils/config-defaults/ConfigDefaults.node.ts +7 -0
  111. package/src/utils/config-defaults/ConfigDefaults.workerd.ts +10 -0
  112. package/src/utils/config-defaults/index.ts +1 -0
  113. package/src/utils/config-defaults/types.ts +14 -0
  114. package/src/utils/constants.ts +22 -0
  115. package/src/utils/crypto.ts +34 -0
  116. package/src/utils/env.ts +34 -0
  117. package/src/utils/fileDirs.ts +81 -0
  118. package/src/utils/fileUtils.node.ts +458 -0
  119. package/src/utils/json-schema.ts +1 -0
  120. package/src/utils/jsonSchema.ts +355 -0
  121. package/src/utils/logger.ts +99 -0
  122. package/src/utils/mime.ts +75 -0
  123. package/src/utils/modifiers/FileToolModifier.node.ts +477 -0
  124. package/src/utils/modifiers/FileToolModifier.utils.neutral.ts +132 -0
  125. package/src/utils/modifiers/FileToolModifier.workerd.ts +51 -0
  126. package/src/utils/pagination.ts +73 -0
  127. package/src/utils/pusher.ts +286 -0
  128. package/src/utils/schemaPropertyKeys.ts +433 -0
  129. package/src/utils/sdk.ts +110 -0
  130. package/src/utils/sensitiveFileUploadPaths.node.ts +105 -0
  131. package/src/utils/session.ts +114 -0
  132. package/src/utils/toolArguments.ts +73 -0
  133. package/src/utils/toolkitVersion.ts +26 -0
  134. package/src/utils/transform.ts +42 -0
  135. package/src/utils/transformers/authConfigs.ts +107 -0
  136. package/src/utils/transformers/connectedAccounts.ts +119 -0
  137. package/src/utils/transformers/mcp.ts +164 -0
  138. package/src/utils/transformers/toolRouterResponseTransform.ts +160 -0
  139. package/src/utils/transformers/toolkits.ts +100 -0
  140. package/src/utils/transformers/triggers.ts +118 -0
  141. package/src/utils/uploadDirAllowlist.node.ts +178 -0
  142. package/src/utils/uuid.ts +7 -0
  143. package/src/utils/version.ts +58 -0
  144. package/src/utils/zodSchema.ts +90 -0
@@ -0,0 +1,99 @@
1
+ // packages/core/src/errors/ValidationError.ts
2
+
3
+ import { ZodError } from 'zod/v3';
4
+ import { ComposioError, ComposioErrorOptions } from './ComposioError';
5
+
6
+ /**
7
+ * Represents an input validation error using Zod.
8
+ */
9
+ export const ValidationErrorCodes = {
10
+ VALIDATION_ERROR: 'VALIDATION_ERROR',
11
+ JSON_SCHEMA_TO_ZOD_ERROR: 'JSON_SCHEMA_TO_ZOD_ERROR',
12
+ JSON_SCHEMA_REF_RESOLUTION_ERROR: 'JSON_SCHEMA_REF_RESOLUTION_ERROR',
13
+ } as const;
14
+
15
+ export class ValidationError extends ComposioError {
16
+ constructor(message: string = 'Input validation failed', options: ComposioErrorOptions = {}) {
17
+ // Extract and process the ZodError
18
+ const { cause: providedZodError, ...restOptions } = options;
19
+
20
+ // Determine the ZodError instance to use
21
+ let zodErrorInstance: ZodError;
22
+ if (providedZodError instanceof ZodError) {
23
+ zodErrorInstance = providedZodError;
24
+ } else {
25
+ zodErrorInstance = new ZodError([
26
+ {
27
+ path: [],
28
+ message: 'Invalid input',
29
+ code: 'custom',
30
+ },
31
+ ]);
32
+ }
33
+
34
+ // Format validation issues for the cause
35
+ const issues = zodErrorInstance.issues.map(
36
+ issue => `[${issue.code}] ${issue.path.join('.')} - ${issue.message}`
37
+ );
38
+
39
+ super(message, {
40
+ ...restOptions,
41
+ code: options.code || ValidationErrorCodes.VALIDATION_ERROR,
42
+ possibleFixes: issues,
43
+ cause: zodErrorInstance,
44
+ });
45
+
46
+ this.name = 'ValidationError';
47
+
48
+ // Create a friendly error message for users
49
+ const userFriendlyMessage = this.generateUserFriendlyMessage();
50
+ this.message = `${message}: ${userFriendlyMessage}`;
51
+ }
52
+
53
+ private generateUserFriendlyMessage(): string {
54
+ // Extract the most relevant information from the ZodError
55
+ if (this.cause instanceof ZodError && this.cause.issues.length > 0) {
56
+ const issue = this.cause.issues[0];
57
+ const path = issue.path.join('.');
58
+ const param = path || 'parameter';
59
+
60
+ // Create specific messages based on error code
61
+ if (issue.code === 'invalid_type') {
62
+ return `The ${param} should be a ${issue.expected}, but you provided a ${issue.received}`;
63
+ }
64
+
65
+ return issue.message;
66
+ }
67
+
68
+ return 'Please check your input parameters';
69
+ }
70
+ }
71
+
72
+ export class JsonSchemaToZodError extends ComposioError {
73
+ constructor(
74
+ message: string = 'Failed to convert JSON schema to Zod schema',
75
+ options: ComposioErrorOptions = {}
76
+ ) {
77
+ super(message, {
78
+ ...options,
79
+ code: options.code || ValidationErrorCodes.JSON_SCHEMA_TO_ZOD_ERROR,
80
+ });
81
+ }
82
+ }
83
+
84
+ /**
85
+ * Thrown when a JSON Schema `$ref` pointer cannot be resolved against the
86
+ * document. Used by `dereferenceJsonSchema` to surface malformed pointers,
87
+ * missing `$defs`/`definitions` targets, or chains exceeding the depth cap.
88
+ */
89
+ export class JsonSchemaRefResolutionError extends ComposioError {
90
+ constructor(
91
+ message: string = 'Failed to resolve JSON Schema $ref',
92
+ options: ComposioErrorOptions = {}
93
+ ) {
94
+ super(message, {
95
+ ...options,
96
+ code: options.code || ValidationErrorCodes.JSON_SCHEMA_REF_RESOLUTION_ERROR,
97
+ });
98
+ }
99
+ }
@@ -0,0 +1,41 @@
1
+ /**
2
+ * @fileoverview Entry point for error classes in Composio SDK
3
+ *
4
+ * This file exports all error classes from the Composio SDK.
5
+ *
6
+ * @author Musthaq Ahamad <musthaq@composio.dev>
7
+ * @module errors
8
+ */
9
+
10
+ // Base Error
11
+ export * from './ComposioError';
12
+
13
+ // Auth Config Errors
14
+ export * from './AuthConfigErrors';
15
+
16
+ // Connected Accounts Errors
17
+ export * from './ConnectedAccountsErrors';
18
+
19
+ // Connection Request Errors
20
+ export * from './ConnectionRequestErrors';
21
+
22
+ // Tool Errors
23
+ export * from './ToolErrors';
24
+
25
+ // Toolkit Errors
26
+ export * from './ToolkitErrors';
27
+
28
+ // Validation Errors
29
+ export * from './ValidationErrors';
30
+
31
+ // SDK Errors
32
+ export * from './SDKErrors';
33
+
34
+ // Trigger Errors
35
+ export * from './TriggerErrors';
36
+
37
+ // Remote File Errors
38
+ export * from './RemoteFileErrors';
39
+
40
+ // File modifier / auto upload
41
+ export * from './FileModifierErrors';
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Experimental APIs for `@composio/core`, exposed on the
3
+ * `@composio/core/experimental` subpath. **Experimental — shape may change in
4
+ * future releases.**
5
+ *
6
+ * The stateless tool/toolkit factories (`experimental_createTool`,
7
+ * `experimental_createToolkit`) are ALSO re-exported from the package root, so
8
+ * for those prefer the shorter import:
9
+ * `import { experimental_createTool, experimental_createToolkit } from '@composio/core';`
10
+ *
11
+ * The experimental local workbench helpers now live in the standalone
12
+ * `@composio/experimental` package, on the `@composio/experimental/workbench`
13
+ * subpath — they are no longer part of `@composio/core`.
14
+ */
15
+
16
+ // Re-export for backward compatibility with existing code
17
+ export {
18
+ createCustomTool as experimental_createTool,
19
+ createCustomToolkit as experimental_createToolkit,
20
+ } from '../models/CustomTool';
21
+ export type {
22
+ CustomTool,
23
+ CreateCustomToolParams,
24
+ CustomToolExecuteFn,
25
+ SessionContext,
26
+ CustomToolkit,
27
+ CreateCustomToolkitParams,
28
+ } from '../types/customTool.types';
package/src/index.ts ADDED
@@ -0,0 +1,64 @@
1
+ // Core exports
2
+ export { Composio } from './composio';
3
+ export { OpenAIProvider } from './provider/OpenAIProvider';
4
+ export { ComposioProvider } from './provider/ComposioProvider';
5
+ export { BaseNonAgenticProvider, BaseAgenticProvider } from './provider/BaseProvider';
6
+ export type { BaseComposioProvider } from './provider/BaseProvider';
7
+ export {
8
+ dereferenceJsonSchema,
9
+ jsonSchemaToZodSchema,
10
+ removeNonRequiredProperties,
11
+ } from './utils/jsonSchema';
12
+ export type {
13
+ DereferenceJsonSchemaOptions,
14
+ UnresolvedRefReason,
15
+ UnresolvedRefStrategy,
16
+ } from './utils/jsonSchema';
17
+ export { getExtensionFromMimeType } from './utils/mime';
18
+ export { normalizeToolArguments } from './utils/toolArguments';
19
+ export {
20
+ sanitizeSchemaPropertyKeys,
21
+ restoreOriginalKeys,
22
+ mappingHasRenames,
23
+ } from './utils/schemaPropertyKeys';
24
+ export type { KeyMapping, KeySanitizationPolicy } from './utils/schemaPropertyKeys';
25
+ export { AuthScheme } from './models/AuthScheme';
26
+ export { MCP } from './models/MCP';
27
+ export { RemoteFile } from './models/RemoteFile';
28
+ export { createConnectionRequest } from './models/ConnectionRequest';
29
+ export { ToolRouterSession } from './models/ToolRouterSession';
30
+ export { Sessions } from './models/Sessions';
31
+ export * from './types/provider.types';
32
+ export * from './types/customTool.types';
33
+ export * from './types/tool.types';
34
+ export * from './types/authConfigs.types';
35
+ export * from './types/modifiers.types';
36
+ export * from './types/connectedAccountAuthStates.types';
37
+ export * from './types/connectedAccounts.types';
38
+ export * from './types/toolkit.types';
39
+ export * from './types/triggers.types';
40
+ export * from './types/webhookEvents.types';
41
+ export * from './types/mcp.types';
42
+ export * from './types/files.types';
43
+ export * from './types/connectionRequest.types';
44
+ export * from './types/toolRouter.types';
45
+ export * from './types/ToolRouterSessionFilesMount.types';
46
+ export * from './types/requestOptions.types';
47
+ export * as constants from './utils/constants';
48
+
49
+ export { default as logger } from './utils/logger';
50
+ export { telemetry } from './telemetry/Telemetry';
51
+
52
+ // Experimental custom tools — exported with experimental_ prefix for top-level import
53
+ export { createCustomTool as experimental_createTool } from './models/CustomTool';
54
+ export { createCustomToolkit as experimental_createToolkit } from './models/CustomTool';
55
+
56
+ // Experimental shared connected accounts — shape may change in future releases.
57
+ // Prefer `composio.connectedAccounts.updateAcl(...)`; the experimental
58
+ // namespace keeps `composio.experimental.updateAcl(...)` as an alias while
59
+ // the API is experimental. The `Experimental` class itself is re-exported
60
+ // so callers can type their own composio handles (e.g. in test helpers).
61
+ export { Experimental } from './models/Experimental';
62
+
63
+ // Error handling exports
64
+ export * from './errors';
@@ -0,0 +1 @@
1
+ export const PRELOAD_TOOLS_ALL = 'all' as const;
@@ -0,0 +1,306 @@
1
+ import {
2
+ SessionCreateParams,
3
+ SessionPatchParams,
4
+ } from '@composio/client/resources/tool-router/session/session.mjs';
5
+ import {
6
+ ToolRouterConfigTags,
7
+ ToolRouterConfigTools,
8
+ ToolRouterConfigToolsSchema,
9
+ ToolRouterToolsParam,
10
+ ToolRouterConfigManageConnectionsSchema,
11
+ ToolRouterCreateSessionConfig,
12
+ ToolRouterToolkitsParamSchema,
13
+ ToolRouterToolkitsDisabledConfigSchema,
14
+ ToolRouterToolkitsEnabledConfigSchema,
15
+ ToolRouterUpdateSessionConfig,
16
+ ToolRouterSandboxConfig,
17
+ } from '../types/toolRouter.types';
18
+ import { ValidationError } from '../errors';
19
+ import { z } from 'zod';
20
+
21
+ export const transformToolRouterToolsParams = (
22
+ params?: Record<string, ToolRouterToolsParam | ToolRouterConfigTools> | undefined
23
+ ):
24
+ | Record<
25
+ string,
26
+ SessionCreateParams.Enable | SessionCreateParams.Disable | SessionCreateParams.Tags
27
+ >
28
+ | undefined => {
29
+ if (!params) {
30
+ return undefined;
31
+ }
32
+
33
+ if (typeof params === 'object') {
34
+ const result = Object.keys(params).reduce(
35
+ (acc, key) => {
36
+ if (Array.isArray(params[key])) {
37
+ acc[key] = { enable: params[key] };
38
+ } else if (typeof params[key] === 'object') {
39
+ const parsedResult = ToolRouterConfigToolsSchema.safeParse(params[key]);
40
+ if (parsedResult.success) {
41
+ const data = parsedResult.data;
42
+ if (Array.isArray(data)) {
43
+ acc[key] = { enable: data };
44
+ } else if ('enable' in data) {
45
+ acc[key] = { enable: data.enable };
46
+ } else if ('disable' in data) {
47
+ acc[key] = { disable: data.disable };
48
+ } else if ('tags' in data) {
49
+ const tags = transformToolRouterTagsParams(data.tags);
50
+ if (tags) {
51
+ acc[key] = { tags };
52
+ }
53
+ }
54
+ } else {
55
+ throw new ValidationError(parsedResult.error.message);
56
+ }
57
+ } else {
58
+ acc[key] = { enable: params[key] };
59
+ }
60
+ return acc;
61
+ },
62
+ {} as Record<
63
+ string,
64
+ SessionCreateParams.Enable | SessionCreateParams.Disable | SessionCreateParams.Tags
65
+ >
66
+ );
67
+ return result;
68
+ }
69
+ };
70
+
71
+ export const transformToolRouterTagsParams = (
72
+ params?: ToolRouterConfigTags
73
+ ): SessionCreateParams.Tags['tags'] | undefined => {
74
+ if (!params) {
75
+ return undefined;
76
+ }
77
+ if (Array.isArray(params)) {
78
+ return { enable: params };
79
+ } else if (typeof params === 'object') {
80
+ return {
81
+ enable: params.enable,
82
+ disable: params.disable,
83
+ };
84
+ }
85
+ };
86
+
87
+ export const transformToolRouterManageConnectionsParams = (
88
+ params?: boolean | z.infer<typeof ToolRouterConfigManageConnectionsSchema>
89
+ ): SessionCreateParams.ManageConnections => {
90
+ if (params === undefined) {
91
+ // Default case when params is undefined
92
+ return {
93
+ enable: true,
94
+ };
95
+ }
96
+
97
+ if (typeof params === 'boolean') {
98
+ return {
99
+ enable: params,
100
+ };
101
+ }
102
+
103
+ // Parse the params using the zod schema for type safety
104
+ const parsedResult = ToolRouterConfigManageConnectionsSchema.safeParse(params);
105
+ if (!parsedResult.success) {
106
+ throw new ValidationError('Failed to parse manage connections config', {
107
+ cause: parsedResult.error,
108
+ });
109
+ }
110
+
111
+ const config = parsedResult.data;
112
+ return {
113
+ enable: config.enable ?? true,
114
+ callback_url: config.callbackUrl,
115
+ enable_wait_for_connections: config.waitForConnections,
116
+ };
117
+ };
118
+
119
+ export const resolveToolRouterSandboxConfig = (
120
+ config: Pick<ToolRouterCreateSessionConfig, 'sandbox' | 'workbench'>
121
+ ): ToolRouterCreateSessionConfig['sandbox'] | ToolRouterCreateSessionConfig['workbench'] => {
122
+ if (config.sandbox !== undefined && config.workbench !== undefined) {
123
+ throw new ValidationError(
124
+ 'Pass either sandbox or workbench, not both. workbench is a backwards-compatible alias for sandbox.'
125
+ );
126
+ }
127
+ return config.sandbox ?? config.workbench;
128
+ };
129
+
130
+ export const transformToolRouterSandboxParams = (
131
+ params?: ToolRouterCreateSessionConfig['sandbox'] | ToolRouterCreateSessionConfig['workbench']
132
+ ): SessionCreateParams.Workbench | undefined => {
133
+ if (!params) {
134
+ return undefined;
135
+ }
136
+
137
+ return {
138
+ enable: params.enable ?? true,
139
+ enable_proxy_execution: params.enableProxyExecution,
140
+ auto_offload_threshold: params.autoOffloadThreshold,
141
+ sandbox_size: params.sandboxSize,
142
+ };
143
+ };
144
+
145
+ /** @deprecated Renamed — use `transformToolRouterSandboxParams` instead. `workbench` is a backwards-compatible alias for `sandbox`. */
146
+ export const transformToolRouterWorkbenchParams = transformToolRouterSandboxParams;
147
+
148
+ /**
149
+ * PATCH-safe variant of transformToolRouterManageConnectionsParams.
150
+ * Does NOT apply defaults — only includes fields explicitly present in the input.
151
+ */
152
+ export const transformToolRouterUpdateManageConnectionsParams = (
153
+ params: boolean | z.infer<typeof ToolRouterConfigManageConnectionsSchema>
154
+ ): SessionPatchParams.ManageConnections => {
155
+ if (typeof params === 'boolean') {
156
+ return { enable: params };
157
+ }
158
+
159
+ const parsedResult = ToolRouterConfigManageConnectionsSchema.safeParse(params);
160
+ if (!parsedResult.success) {
161
+ throw new ValidationError('Failed to parse manage connections config', {
162
+ cause: parsedResult.error,
163
+ });
164
+ }
165
+
166
+ const config = parsedResult.data;
167
+ const result: Record<string, unknown> = {};
168
+ if (config.enable !== undefined) {
169
+ result.enable = config.enable;
170
+ }
171
+ if (config.callbackUrl !== undefined) {
172
+ result.callback_url = config.callbackUrl;
173
+ }
174
+ if (config.waitForConnections !== undefined) {
175
+ result.enable_wait_for_connections = config.waitForConnections;
176
+ }
177
+ return result as SessionPatchParams.ManageConnections;
178
+ };
179
+
180
+ /**
181
+ * PATCH-safe variant of transformToolRouterSandboxParams.
182
+ * Does NOT apply `enable ?? true` default — only includes fields explicitly present.
183
+ */
184
+ export const transformToolRouterUpdateSandboxParams = (
185
+ params: Partial<ToolRouterSandboxConfig>
186
+ ): SessionPatchParams.Workbench => {
187
+ const result: Record<string, unknown> = {};
188
+ if (params.enable !== undefined) {
189
+ result.enable = params.enable;
190
+ }
191
+ if (params.enableProxyExecution !== undefined) {
192
+ result.enable_proxy_execution = params.enableProxyExecution;
193
+ }
194
+ if (params.autoOffloadThreshold !== undefined) {
195
+ result.auto_offload_threshold = params.autoOffloadThreshold;
196
+ }
197
+ if (params.sandboxSize !== undefined) {
198
+ result.sandbox_size = params.sandboxSize;
199
+ }
200
+ return result as SessionPatchParams.Workbench;
201
+ };
202
+
203
+ /** @deprecated Renamed — use `transformToolRouterUpdateSandboxParams` instead. `workbench` is a backwards-compatible alias for `sandbox`. */
204
+ export const transformToolRouterUpdateWorkbenchParams = transformToolRouterUpdateSandboxParams;
205
+
206
+ export const transformToolRouterMultiAccountParams = (
207
+ params?: ToolRouterCreateSessionConfig['multiAccount']
208
+ ): SessionCreateParams.MultiAccount | undefined => {
209
+ if (!params) {
210
+ return undefined;
211
+ }
212
+
213
+ const transformedParams = {
214
+ enable: params.enable,
215
+ max_accounts_per_toolkit: params.maxAccountsPerToolkit,
216
+ require_explicit_selection:
217
+ params.requireExplicitSelection ?? (params.enable ? true : undefined),
218
+ };
219
+
220
+ if (
221
+ transformedParams.enable === undefined &&
222
+ transformedParams.max_accounts_per_toolkit === undefined &&
223
+ transformedParams.require_explicit_selection === undefined
224
+ ) {
225
+ return undefined;
226
+ }
227
+
228
+ return transformedParams;
229
+ };
230
+
231
+ export const transformToolRouterToolkitsParams = (
232
+ params?: ToolRouterCreateSessionConfig['toolkits']
233
+ ): SessionCreateParams.Enable | SessionCreateParams.Disable | undefined => {
234
+ if (!params) {
235
+ return undefined;
236
+ }
237
+
238
+ // If it's an array, convert to enable format
239
+ if (Array.isArray(params)) {
240
+ return { enable: params };
241
+ }
242
+
243
+ // Otherwise return as-is (already in enable/disable format)
244
+ return params as SessionCreateParams.Enable | SessionCreateParams.Disable;
245
+ };
246
+
247
+ export const transformToolRouterUpdateParams = (
248
+ config: ToolRouterUpdateSessionConfig
249
+ ): SessionPatchParams => {
250
+ const params: SessionPatchParams = {};
251
+
252
+ if (config.toolkits !== undefined) {
253
+ params.toolkits = transformToolRouterToolkitsParams(config.toolkits);
254
+ }
255
+ if (config.tools !== undefined) {
256
+ params.tools = transformToolRouterToolsParams(config.tools);
257
+ }
258
+ if (config.tags !== undefined) {
259
+ params.tags = transformToolRouterTagsParams(config.tags);
260
+ }
261
+ if (config.authConfigs !== undefined) {
262
+ params.auth_configs = config.authConfigs;
263
+ }
264
+ if (config.connectedAccounts !== undefined) {
265
+ const coerced: Record<string, string[]> = {};
266
+ for (const [k, v] of Object.entries(config.connectedAccounts)) {
267
+ coerced[k] = typeof v === 'string' ? [v] : v;
268
+ }
269
+ params.connected_accounts = coerced;
270
+ }
271
+ if (config.manageConnections !== undefined) {
272
+ if (config.manageConnections === null) {
273
+ params.manage_connections = null;
274
+ } else {
275
+ params.manage_connections = transformToolRouterUpdateManageConnectionsParams(
276
+ config.manageConnections
277
+ );
278
+ }
279
+ }
280
+ const sandboxConfig = config.sandbox !== undefined ? config.sandbox : config.workbench;
281
+ if (sandboxConfig !== undefined) {
282
+ if (sandboxConfig === null) {
283
+ params.workbench = null;
284
+ } else {
285
+ params.workbench = transformToolRouterUpdateSandboxParams(sandboxConfig);
286
+ }
287
+ }
288
+ if (config.multiAccount !== undefined) {
289
+ if (config.multiAccount === null) {
290
+ params.multi_account = null;
291
+ } else {
292
+ const ma: Record<string, unknown> = {};
293
+ if (config.multiAccount.enable !== undefined) ma.enable = config.multiAccount.enable;
294
+ if (config.multiAccount.maxAccountsPerToolkit !== undefined)
295
+ ma.max_accounts_per_toolkit = config.multiAccount.maxAccountsPerToolkit;
296
+ if (config.multiAccount.requireExplicitSelection !== undefined)
297
+ ma.require_explicit_selection = config.multiAccount.requireExplicitSelection;
298
+ params.multi_account = ma as SessionPatchParams.MultiAccount;
299
+ }
300
+ }
301
+ if (config.preload !== undefined) {
302
+ params.preload = config.preload;
303
+ }
304
+
305
+ return params;
306
+ };