Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
fix(core): address PR review comments for session.update()
- Fix stale preloadedCustomToolSlugs after update by recomputing them - Add ToolRouterUpdateSessionConfigSchema for runtime validation - Add PATCH-safe transform variants that don't inject defaults - Add 11 unit tests for transformToolRouterUpdateParams Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Abir Taheer committed
May 7, 2026 at 02:24 UTC
5a133dce13a57238245d0e27bf1fb8de1d107db9
4 files changed
+268
-16
ts/packages/core/src/lib/toolRouterParams.ts
+63
-3
@@ -130,6 +130,66 @@ export const transformToolRouterWorkbenchParams = (
130
};
131
};
132
133
+/**
134
+ * PATCH-safe variant of transformToolRouterManageConnectionsParams.
135
+ * Does NOT apply defaults — only includes fields explicitly present in the input.
136
+ */
137
+export const transformToolRouterUpdateManageConnectionsParams = (
138
+ params: boolean | z.infer<typeof ToolRouterConfigManageConnectionsSchema>
139
+): SessionPatchParams.ManageConnections => {
140
+ if (typeof params === 'boolean') {
141
+ return { enable: params };
142
+ }
143
+
144
+ const parsedResult = ToolRouterConfigManageConnectionsSchema.safeParse(params);
145
+ if (!parsedResult.success) {
146
+ throw new ValidationError('Failed to parse manage connections config', {
147
+ cause: parsedResult.error,
148
+ });
149
+ }
150
+
151
+ const config = parsedResult.data;
152
+ const result: Record<string, unknown> = {};
153
+ if (config.enable !== undefined) {
154
+ result.enable = config.enable;
155
+ }
156
+ if (config.callbackUrl !== undefined) {
157
+ result.callback_url = config.callbackUrl;
158
+ }
159
+ if (config.waitForConnections !== undefined) {
160
+ result.enable_wait_for_connections = config.waitForConnections;
161
+ }
162
+ return result as SessionPatchParams.ManageConnections;
163
+};
164
+
165
+/**
166
+ * PATCH-safe variant of transformToolRouterWorkbenchParams.
167
+ * Does NOT apply `enable ?? true` default — only includes fields explicitly present.
168
+ */
169
+export const transformToolRouterUpdateWorkbenchParams = (
170
+ params: {
171
+ enable?: boolean;
172
+ enableProxyExecution?: boolean;
173
+ autoOffloadThreshold?: number;
174
+ sandboxSize?: 'standard' | 'medium' | 'large' | 'xlarge';
175
+ }
176
+): SessionPatchParams.Workbench => {
177
+ const result: Record<string, unknown> = {};
178
+ if (params.enable !== undefined) {
179
+ result.enable = params.enable;
180
+ }
181
+ if (params.enableProxyExecution !== undefined) {
182
+ result.enable_proxy_execution = params.enableProxyExecution;
183
+ }
184
+ if (params.autoOffloadThreshold !== undefined) {
185
+ result.auto_offload_threshold = params.autoOffloadThreshold;
186
+ }
187
+ if (params.sandboxSize !== undefined) {
188
+ result.sandbox_size = params.sandboxSize;
189
+ }
190
+ return result as SessionPatchParams.Workbench;
191
+};
192
+
193
export const transformToolRouterMultiAccountParams = (
194
params?: ToolRouterCreateSessionConfig['multiAccount']
195
): SessionCreateParams.MultiAccount | undefined => {
@@ -194,21 +254,21 @@ export const transformToolRouterUpdateParams = (
254
if (config.manageConnections === null) {
255
params.manage_connections = null;
256
} else {
197
- params.manage_connections = transformToolRouterManageConnectionsParams(config.manageConnections);
257
+ params.manage_connections = transformToolRouterUpdateManageConnectionsParams(config.manageConnections);
258
}
259
}
260
if (config.workbench !== undefined) {
261
if (config.workbench === null) {
262
params.workbench = null;
263
} else {
204
- params.workbench = transformToolRouterWorkbenchParams(config.workbench);
264
+ params.workbench = transformToolRouterUpdateWorkbenchParams(config.workbench);
265
}
266
}
267
if (config.multiAccount !== undefined) {
268
if (config.multiAccount === null) {
269
params.multi_account = null;
270
} else {
211
- params.multi_account = transformToolRouterMultiAccountParams(config.multiAccount);
271
+ params.multi_account = transformToolRouterMultiAccountParams(config.multiAccount as ToolRouterCreateSessionConfig['multiAccount']);
272
}
273
}
274
if (config.preload !== undefined) {
ts/packages/core/src/models/ToolRouterSession.ts
+14
-2
@@ -17,6 +17,7 @@ import {
17
ToolRouterSessionPreloadConfig,
18
ToolRouterSessionWarning,
19
ToolRouterUpdateSessionConfig,
20
+ ToolRouterUpdateSessionConfigSchema,
21
} from '../types/toolRouter.types';
22
import {
23
transformSearchResponse,
@@ -51,6 +52,7 @@ import { findCustomToolMapEntryByFinalSlug } from './CustomTool';
52
import { transformProxyParams } from './proxyParamsTransform';
53
import { inlineCustomToolsExperimental } from './inlineCustomToolsPayload';
54
import { transformToolRouterUpdateParams } from '../lib/toolRouterParams';
55
+import { getPreloadedCustomToolSlugs } from './CustomTool';
56
57
const COMPOSIO_MULTI_EXECUTE_TOOL = 'COMPOSIO_MULTI_EXECUTE_TOOL';
58
export const DIRECT_CUSTOM_TOOL_DESCRIPTION_PREFIX =
@@ -67,7 +69,7 @@ export class ToolRouterSession<
69
public preload: ToolRouterSessionPreloadConfig;
70
public configVersion?: number;
71
public warnings: ToolRouterSessionWarning[];
70
- private readonly preloadedCustomToolSlugs: string[];
72
+ private preloadedCustomToolSlugs: string[];
73
private readonly inlineCustomToolsPayload: ToolRouterSessionMetadata['inlineCustomToolsPayload'];
74
75
/** Singleton session context — shared across all custom tool executions */
@@ -479,11 +481,21 @@ export class ToolRouterSession<
481
* Mutates this session's `configVersion`, `preload`, and `warnings` in-place.
482
*/
483
async update(config: ToolRouterUpdateSessionConfig): Promise<void> {
482
- const params = transformToolRouterUpdateParams(config);
484
+ const parsed = ToolRouterUpdateSessionConfigSchema.safeParse(config);
485
+ if (!parsed.success) {
486
+ throw new ValidationError('Failed to parse update session config', {
487
+ cause: parsed.error,
488
+ });
489
+ }
490
+ const params = transformToolRouterUpdateParams(parsed.data);
491
const response = await this.client.toolRouter.session.patch(this.sessionId, params);
492
this.configVersion = response.config_version;
493
this.preload = response.config.preload;
494
this.warnings = response.warnings ?? [];
495
+ this.preloadedCustomToolSlugs = getPreloadedCustomToolSlugs(
496
+ this.customToolsMap,
497
+ this.preload.tools === 'all'
498
+ );
499
}
500
501
// ── Private helpers ──────────────────────────────────────────
ts/packages/core/src/types/toolRouter.types.ts
+77
-11
@@ -612,6 +612,82 @@ export type ToolRouterSessionProxyExecuteFn = (
612
params: SessionProxyExecuteParams
613
) => Promise<ToolRouterSessionProxyExecuteResponse>;
614
615
+/**
616
+ * Zod schema for `session.update()` config. Same shape as the create config
617
+ * but every field is optional, `sessionPreset` and custom tools/toolkits are
618
+ * excluded, and `manageConnections`/`workbench`/`multiAccount` accept `null`
619
+ * to clear the stored value. No defaults are applied (PATCH semantics).
620
+ */
621
+export const ToolRouterUpdateSessionConfigSchema = z
622
+ .object({
623
+ toolkits: z
624
+ .union([
625
+ ToolRouterToolkitsParamSchema,
626
+ ToolRouterToolkitsDisabledConfigSchema,
627
+ ToolRouterToolkitsEnabledConfigSchema,
628
+ ])
629
+ .optional(),
630
+
631
+ tools: z
632
+ .record(z.string(), z.union([ToolRouterToolsParamSchema, ToolRouterConfigToolsSchema]))
633
+ .optional(),
634
+
635
+ tags: ToolRouterConfigTagsSchema.optional(),
636
+
637
+ authConfigs: z.record(z.string(), z.string()).optional(),
638
+
639
+ connectedAccounts: z
640
+ .record(z.string(), z.union([z.string(), z.array(z.string())]))
641
+ .transform((rec) => {
642
+ const out: Record<string, string[]> = {};
643
+ for (const [k, v] of Object.entries(rec)) {
644
+ out[k] = typeof v === 'string' ? [v] : v;
645
+ }
646
+ return out;
647
+ })
648
+ .optional(),
649
+
650
+ manageConnections: z
651
+ .union([
652
+ z.boolean(),
653
+ z.object({
654
+ enable: z.boolean().optional(),
655
+ callbackUrl: z.string().optional(),
656
+ waitForConnections: z.boolean().optional(),
657
+ }).strict(),
658
+ ])
659
+ .nullable()
660
+ .optional(),
661
+
662
+ workbench: z
663
+ .object({
664
+ enable: z.boolean().optional(),
665
+ enableProxyExecution: z.boolean().optional(),
666
+ autoOffloadThreshold: z.number().optional(),
667
+ sandboxSize: SandboxSizeSchema.optional(),
668
+ })
669
+ .nullable()
670
+ .optional(),
671
+
672
+ multiAccount: z
673
+ .object({
674
+ enable: z.boolean().optional(),
675
+ maxAccountsPerToolkit: z.number().int().min(2).max(10).optional(),
676
+ requireExplicitSelection: z.boolean().optional(),
677
+ })
678
+ .nullable()
679
+ .optional(),
680
+
681
+ preload: z
682
+ .object({
683
+ tools: z.union([z.array(z.string()), z.literal('all')]).optional(),
684
+ })
685
+ .strict()
686
+ .optional(),
687
+ })
688
+ .strict()
689
+ .describe('The config for updating a tool router session');
690
+
691
/**
692
* Config accepted by `session.update()`. Same camelCase shape as
693
* `ToolRouterCreateSessionConfig` but every field is optional and
@@ -619,17 +695,7 @@ export type ToolRouterSessionProxyExecuteFn = (
695
* to clear the stored value. `sessionPreset` and custom tools/toolkits
696
* are not supported on update.
697
*/
622
-export interface ToolRouterUpdateSessionConfig {
623
- toolkits?: ToolRouterCreateSessionConfig['toolkits'];
624
- tools?: ToolRouterCreateSessionConfig['tools'];
625
- tags?: ToolRouterCreateSessionConfig['tags'];
626
- authConfigs?: ToolRouterCreateSessionConfig['authConfigs'];
627
- connectedAccounts?: ToolRouterCreateSessionConfig['connectedAccounts'];
628
- manageConnections?: ToolRouterCreateSessionConfig['manageConnections'] | null;
629
- workbench?: ToolRouterCreateSessionConfig['workbench'] | null;
630
- multiAccount?: ToolRouterCreateSessionConfig['multiAccount'] | null;
631
- preload?: ToolRouterCreateSessionConfig['preload'];
632
-}
698
+export type ToolRouterUpdateSessionConfig = z.infer<typeof ToolRouterUpdateSessionConfigSchema>;
699
700
export type ToolRouterSessionUpdateFn = (config: ToolRouterUpdateSessionConfig) => Promise<void>;
701
ts/packages/core/test/lib/toolRouterParams.test.ts
+114
@@ -2,6 +2,7 @@ import { describe, it, expect } from 'vitest';
2
import {
3
transformToolRouterTagsParams,
4
transformToolRouterMultiAccountParams,
5
+ transformToolRouterUpdateParams,
6
} from '../../src/lib/toolRouterParams';
7
import { ToolRouterConfigTags } from '../../src/types/toolRouter.types';
8
@@ -180,3 +181,116 @@ describe('transformToolRouterTagsParams', () => {
181
});
182
});
183
});
184
+
185
+describe('transformToolRouterUpdateParams', () => {
186
+ it('should pass through toolkits correctly', () => {
187
+ const result = transformToolRouterUpdateParams({
188
+ toolkits: ['github', 'gmail'],
189
+ });
190
+ expect(result).toEqual({
191
+ toolkits: { enable: ['github', 'gmail'] },
192
+ });
193
+ });
194
+
195
+ it('should pass through tools correctly', () => {
196
+ const result = transformToolRouterUpdateParams({
197
+ tools: {
198
+ github: ['GITHUB_CREATE_REPO'],
199
+ },
200
+ });
201
+ expect(result).toEqual({
202
+ tools: {
203
+ github: { enable: ['GITHUB_CREATE_REPO'] },
204
+ },
205
+ });
206
+ });
207
+
208
+ it('should pass through tags correctly', () => {
209
+ const result = transformToolRouterUpdateParams({
210
+ tags: ['readOnlyHint'],
211
+ });
212
+ expect(result).toEqual({
213
+ tags: { enable: ['readOnlyHint'] },
214
+ });
215
+ });
216
+
217
+ it('should handle null for manageConnections (clearing)', () => {
218
+ const result = transformToolRouterUpdateParams({
219
+ manageConnections: null,
220
+ });
221
+ expect(result).toEqual({
222
+ manage_connections: null,
223
+ });
224
+ });
225
+
226
+ it('should handle null for workbench (clearing)', () => {
227
+ const result = transformToolRouterUpdateParams({
228
+ workbench: null,
229
+ });
230
+ expect(result).toEqual({
231
+ workbench: null,
232
+ });
233
+ });
234
+
235
+ it('should handle null for multiAccount (clearing)', () => {
236
+ const result = transformToolRouterUpdateParams({
237
+ multiAccount: null,
238
+ });
239
+ expect(result).toEqual({
240
+ multi_account: null,
241
+ });
242
+ });
243
+
244
+ it('should only include fields that were explicitly set', () => {
245
+ const result = transformToolRouterUpdateParams({
246
+ toolkits: ['github'],
247
+ });
248
+ // Should only have toolkits, not tools, tags, manage_connections, etc.
249
+ expect(result).toEqual({
250
+ toolkits: { enable: ['github'] },
251
+ });
252
+ expect(result).not.toHaveProperty('tools');
253
+ expect(result).not.toHaveProperty('tags');
254
+ expect(result).not.toHaveProperty('manage_connections');
255
+ expect(result).not.toHaveProperty('workbench');
256
+ expect(result).not.toHaveProperty('multi_account');
257
+ expect(result).not.toHaveProperty('preload');
258
+ expect(result).not.toHaveProperty('auth_configs');
259
+ expect(result).not.toHaveProperty('connected_accounts');
260
+ });
261
+
262
+ it('should return empty object when no fields are set', () => {
263
+ const result = transformToolRouterUpdateParams({});
264
+ expect(result).toEqual({});
265
+ });
266
+
267
+ it('should NOT apply default enable:true for manageConnections when only callbackUrl is provided', () => {
268
+ const result = transformToolRouterUpdateParams({
269
+ manageConnections: { callbackUrl: 'https://example.com/callback' },
270
+ });
271
+ // Should not have enable: true injected
272
+ expect(result.manage_connections).toBeDefined();
273
+ expect(result.manage_connections).not.toBeNull();
274
+ const mc = result.manage_connections as Record<string, unknown>;
275
+ expect(mc).not.toHaveProperty('enable');
276
+ expect(mc).toHaveProperty('callback_url', 'https://example.com/callback');
277
+ });
278
+
279
+ it('should NOT apply default enable:true for workbench when only enableProxyExecution is provided', () => {
280
+ const result = transformToolRouterUpdateParams({
281
+ workbench: { enableProxyExecution: true },
282
+ });
283
+ const wb = result.workbench as Record<string, unknown>;
284
+ expect(wb).not.toHaveProperty('enable');
285
+ expect(wb).toHaveProperty('enable_proxy_execution', true);
286
+ });
287
+
288
+ it('should include enable for manageConnections when explicitly set', () => {
289
+ const result = transformToolRouterUpdateParams({
290
+ manageConnections: { enable: false, callbackUrl: 'https://example.com' },
291
+ });
292
+ const mc = result.manage_connections as Record<string, unknown>;
293
+ expect(mc).toHaveProperty('enable', false);
294
+ expect(mc).toHaveProperty('callback_url', 'https://example.com');
295
+ });
296
+});