Skip to content

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()

Abir Taheer5a133dce13a5
diff --git a/ts/packages/core/src/lib/toolRouterParams.ts b/ts/packages/core/src/lib/toolRouterParams.ts
index 3b457ba40..3f41ac5b2 100644
--- a/ts/packages/core/src/lib/toolRouterParams.ts
+++ b/ts/packages/core/src/lib/toolRouterParams.ts
@@ -130,6 +130,66 @@ export const transformToolRouterWorkbenchParams = (
};
};
+/**
+ * PATCH-safe variant of transformToolRouterManageConnectionsParams.
+ * Does NOT apply defaults — only includes fields explicitly present in the input.
+ */
+export const transformToolRouterUpdateManageConnectionsParams = (
+ params: boolean | z.infer<typeof ToolRouterConfigManageConnectionsSchema>
+): SessionPatchParams.ManageConnections => {
+ if (typeof params === 'boolean') {
+ return { enable: params };
+ }
+
+ const parsedResult = ToolRouterConfigManageConnectionsSchema.safeParse(params);
+ if (!parsedResult.success) {
+ throw new ValidationError('Failed to parse manage connections config', {
+ cause: parsedResult.error,
+ });
+ }
+
+ const config = parsedResult.data;
+ const result: Record<string, unknown> = {};
+ if (config.enable !== undefined) {
+ result.enable = config.enable;
+ }
+ if (config.callbackUrl !== undefined) {
+ result.callback_url = config.callbackUrl;
+ }
+ if (config.waitForConnections !== undefined) {
+ result.enable_wait_for_connections = config.waitForConnections;
+ }
+ return result as SessionPatchParams.ManageConnections;
+};
+
+/**
+ * PATCH-safe variant of transformToolRouterWorkbenchParams.
+ * Does NOT apply `enable ?? true` default — only includes fields explicitly present.
+ */
+export const transformToolRouterUpdateWorkbenchParams = (
+ params: {
+ enable?: boolean;
+ enableProxyExecution?: boolean;
+ autoOffloadThreshold?: number;
+ sandboxSize?: 'standard' | 'medium' | 'large' | 'xlarge';
+ }
+): SessionPatchParams.Workbench => {
+ const result: Record<string, unknown> = {};
+ if (params.enable !== undefined) {
+ result.enable = params.enable;
+ }
+ if (params.enableProxyExecution !== undefined) {
+ result.enable_proxy_execution = params.enableProxyExecution;
+ }
+ if (params.autoOffloadThreshold !== undefined) {
+ result.auto_offload_threshold = params.autoOffloadThreshold;
+ }
+ if (params.sandboxSize !== undefined) {
+ result.sandbox_size = params.sandboxSize;
+ }
+ return result as SessionPatchParams.Workbench;
+};
+
export const transformToolRouterMultiAccountParams = (
params?: ToolRouterCreateSessionConfig['multiAccount']
): SessionCreateParams.MultiAccount | undefined => {
@@ -194,21 +254,21 @@ export const transformToolRouterUpdateParams = (
if (config.manageConnections === null) {
params.manage_connections = null;
} else {
- params.manage_connections = transformToolRouterManageConnectionsParams(config.manageConnections);
+ params.manage_connections = transformToolRouterUpdateManageConnectionsParams(config.manageConnections);
}
}
if (config.workbench !== undefined) {
if (config.workbench === null) {
params.workbench = null;
} else {
- params.workbench = transformToolRouterWorkbenchParams(config.workbench);
+ params.workbench = transformToolRouterUpdateWorkbenchParams(config.workbench);
}
}
if (config.multiAccount !== undefined) {
if (config.multiAccount === null) {
params.multi_account = null;
} else {
- params.multi_account = transformToolRouterMultiAccountParams(config.multiAccount);
+ params.multi_account = transformToolRouterMultiAccountParams(config.multiAccount as ToolRouterCreateSessionConfig['multiAccount']);
}
}
if (config.preload !== undefined) {
diff --git a/ts/packages/core/src/models/ToolRouterSession.ts b/ts/packages/core/src/models/ToolRouterSession.ts
index 39b37fcbf..0fa76a9d3 100644
--- a/ts/packages/core/src/models/ToolRouterSession.ts
+++ b/ts/packages/core/src/models/ToolRouterSession.ts
@@ -17,6 +17,7 @@ import {
ToolRouterSessionPreloadConfig,
ToolRouterSessionWarning,
ToolRouterUpdateSessionConfig,
+ ToolRouterUpdateSessionConfigSchema,
} from '../types/toolRouter.types';
import {
transformSearchResponse,
@@ -51,6 +52,7 @@ import { findCustomToolMapEntryByFinalSlug } from './CustomTool';
import { transformProxyParams } from './proxyParamsTransform';
import { inlineCustomToolsExperimental } from './inlineCustomToolsPayload';
import { transformToolRouterUpdateParams } from '../lib/toolRouterParams';
+import { getPreloadedCustomToolSlugs } from './CustomTool';
const COMPOSIO_MULTI_EXECUTE_TOOL = 'COMPOSIO_MULTI_EXECUTE_TOOL';
export const DIRECT_CUSTOM_TOOL_DESCRIPTION_PREFIX =
@@ -67,7 +69,7 @@ export class ToolRouterSession<
public preload: ToolRouterSessionPreloadConfig;
public configVersion?: number;
public warnings: ToolRouterSessionWarning[];
- private readonly preloadedCustomToolSlugs: string[];
+ private preloadedCustomToolSlugs: string[];
private readonly inlineCustomToolsPayload: ToolRouterSessionMetadata['inlineCustomToolsPayload'];
/** Singleton session context — shared across all custom tool executions */
@@ -479,11 +481,21 @@ export class ToolRouterSession<
* Mutates this session's `configVersion`, `preload`, and `warnings` in-place.
*/
async update(config: ToolRouterUpdateSessionConfig): Promise<void> {
- const params = transformToolRouterUpdateParams(config);
+ const parsed = ToolRouterUpdateSessionConfigSchema.safeParse(config);
+ if (!parsed.success) {
+ throw new ValidationError('Failed to parse update session config', {
+ cause: parsed.error,
+ });
+ }
+ const params = transformToolRouterUpdateParams(parsed.data);
const response = await this.client.toolRouter.session.patch(this.sessionId, params);
this.configVersion = response.config_version;
this.preload = response.config.preload;
this.warnings = response.warnings ?? [];
+ this.preloadedCustomToolSlugs = getPreloadedCustomToolSlugs(
+ this.customToolsMap,
+ this.preload.tools === 'all'
+ );
}
// ── Private helpers ──────────────────────────────────────────
diff --git a/ts/packages/core/src/types/toolRouter.types.ts b/ts/packages/core/src/types/toolRouter.types.ts
index bb77c1ae5..4a1ff6696 100644
--- a/ts/packages/core/src/types/toolRouter.types.ts
+++ b/ts/packages/core/src/types/toolRouter.types.ts
@@ -612,6 +612,82 @@ export type ToolRouterSessionProxyExecuteFn = (
params: SessionProxyExecuteParams
) => Promise<ToolRouterSessionProxyExecuteResponse>;
+/**
+ * Zod schema for `session.update()` config. Same shape as the create config
+ * but every field is optional, `sessionPreset` and custom tools/toolkits are
+ * excluded, and `manageConnections`/`workbench`/`multiAccount` accept `null`
+ * to clear the stored value. No defaults are applied (PATCH semantics).
+ */
+export const ToolRouterUpdateSessionConfigSchema = z
+ .object({
+ toolkits: z
+ .union([
+ ToolRouterToolkitsParamSchema,
+ ToolRouterToolkitsDisabledConfigSchema,
+ ToolRouterToolkitsEnabledConfigSchema,
+ ])
+ .optional(),
+
+ tools: z
+ .record(z.string(), z.union([ToolRouterToolsParamSchema, ToolRouterConfigToolsSchema]))
+ .optional(),
+
+ tags: ToolRouterConfigTagsSchema.optional(),
+
+ authConfigs: z.record(z.string(), z.string()).optional(),
+
+ connectedAccounts: z
+ .record(z.string(), z.union([z.string(), z.array(z.string())]))
+ .transform((rec) => {
+ const out: Record<string, string[]> = {};
+ for (const [k, v] of Object.entries(rec)) {
+ out[k] = typeof v === 'string' ? [v] : v;
+ }
+ return out;
+ })
+ .optional(),
+
+ manageConnections: z
+ .union([
+ z.boolean(),
+ z.object({
+ enable: z.boolean().optional(),
+ callbackUrl: z.string().optional(),
+ waitForConnections: z.boolean().optional(),
+ }).strict(),
+ ])
+ .nullable()
+ .optional(),
+
+ workbench: z
+ .object({
+ enable: z.boolean().optional(),
+ enableProxyExecution: z.boolean().optional(),
+ autoOffloadThreshold: z.number().optional(),
+ sandboxSize: SandboxSizeSchema.optional(),
+ })
+ .nullable()
+ .optional(),
+
+ multiAccount: z
+ .object({
+ enable: z.boolean().optional(),
+ maxAccountsPerToolkit: z.number().int().min(2).max(10).optional(),
+ requireExplicitSelection: z.boolean().optional(),
+ })
+ .nullable()
+ .optional(),
+
+ preload: z
+ .object({
+ tools: z.union([z.array(z.string()), z.literal('all')]).optional(),
+ })
+ .strict()
+ .optional(),
+ })
+ .strict()
+ .describe('The config for updating a tool router session');
+
/**
* Config accepted by `session.update()`. Same camelCase shape as
* `ToolRouterCreateSessionConfig` but every field is optional and
@@ -619,17 +695,7 @@ export type ToolRouterSessionProxyExecuteFn = (
* to clear the stored value. `sessionPreset` and custom tools/toolkits
* are not supported on update.
*/
-export interface ToolRouterUpdateSessionConfig {
- toolkits?: ToolRouterCreateSessionConfig['toolkits'];
- tools?: ToolRouterCreateSessionConfig['tools'];
- tags?: ToolRouterCreateSessionConfig['tags'];
- authConfigs?: ToolRouterCreateSessionConfig['authConfigs'];
- connectedAccounts?: ToolRouterCreateSessionConfig['connectedAccounts'];
- manageConnections?: ToolRouterCreateSessionConfig['manageConnections'] | null;
- workbench?: ToolRouterCreateSessionConfig['workbench'] | null;
- multiAccount?: ToolRouterCreateSessionConfig['multiAccount'] | null;
- preload?: ToolRouterCreateSessionConfig['preload'];
-}
+export type ToolRouterUpdateSessionConfig = z.infer<typeof ToolRouterUpdateSessionConfigSchema>;
export type ToolRouterSessionUpdateFn = (config: ToolRouterUpdateSessionConfig) => Promise<void>;
diff --git a/ts/packages/core/test/lib/toolRouterParams.test.ts b/ts/packages/core/test/lib/toolRouterParams.test.ts
index c304a448f..b60595101 100644
--- a/ts/packages/core/test/lib/toolRouterParams.test.ts
+++ b/ts/packages/core/test/lib/toolRouterParams.test.ts
@@ -2,6 +2,7 @@ import { describe, it, expect } from 'vitest';
import {
transformToolRouterTagsParams,
transformToolRouterMultiAccountParams,
+ transformToolRouterUpdateParams,
} from '../../src/lib/toolRouterParams';
import { ToolRouterConfigTags } from '../../src/types/toolRouter.types';
@@ -180,3 +181,116 @@ describe('transformToolRouterTagsParams', () => {
});
});
});
+
+describe('transformToolRouterUpdateParams', () => {
+ it('should pass through toolkits correctly', () => {
+ const result = transformToolRouterUpdateParams({
+ toolkits: ['github', 'gmail'],
+ });
+ expect(result).toEqual({
+ toolkits: { enable: ['github', 'gmail'] },
+ });
+ });
+
+ it('should pass through tools correctly', () => {
+ const result = transformToolRouterUpdateParams({
+ tools: {
+ github: ['GITHUB_CREATE_REPO'],
+ },
+ });
+ expect(result).toEqual({
+ tools: {
+ github: { enable: ['GITHUB_CREATE_REPO'] },
+ },
+ });
+ });
+
+ it('should pass through tags correctly', () => {
+ const result = transformToolRouterUpdateParams({
+ tags: ['readOnlyHint'],
+ });
+ expect(result).toEqual({
+ tags: { enable: ['readOnlyHint'] },
+ });
+ });
+
+ it('should handle null for manageConnections (clearing)', () => {
+ const result = transformToolRouterUpdateParams({
+ manageConnections: null,
+ });
+ expect(result).toEqual({
+ manage_connections: null,
+ });
+ });
+
+ it('should handle null for workbench (clearing)', () => {
+ const result = transformToolRouterUpdateParams({
+ workbench: null,
+ });
+ expect(result).toEqual({
+ workbench: null,
+ });
+ });
+
+ it('should handle null for multiAccount (clearing)', () => {
+ const result = transformToolRouterUpdateParams({
+ multiAccount: null,
+ });
+ expect(result).toEqual({
+ multi_account: null,
+ });
+ });
+
+ it('should only include fields that were explicitly set', () => {
+ const result = transformToolRouterUpdateParams({
+ toolkits: ['github'],
+ });
+ // Should only have toolkits, not tools, tags, manage_connections, etc.
+ expect(result).toEqual({
+ toolkits: { enable: ['github'] },
+ });
+ expect(result).not.toHaveProperty('tools');
+ expect(result).not.toHaveProperty('tags');
+ expect(result).not.toHaveProperty('manage_connections');
+ expect(result).not.toHaveProperty('workbench');
+ expect(result).not.toHaveProperty('multi_account');
+ expect(result).not.toHaveProperty('preload');
+ expect(result).not.toHaveProperty('auth_configs');
+ expect(result).not.toHaveProperty('connected_accounts');
+ });
+
+ it('should return empty object when no fields are set', () => {
+ const result = transformToolRouterUpdateParams({});
+ expect(result).toEqual({});
+ });
+
+ it('should NOT apply default enable:true for manageConnections when only callbackUrl is provided', () => {
+ const result = transformToolRouterUpdateParams({
+ manageConnections: { callbackUrl: 'https://example.com/callback' },
+ });
+ // Should not have enable: true injected
+ expect(result.manage_connections).toBeDefined();
+ expect(result.manage_connections).not.toBeNull();
+ const mc = result.manage_connections as Record<string, unknown>;
+ expect(mc).not.toHaveProperty('enable');
+ expect(mc).toHaveProperty('callback_url', 'https://example.com/callback');
+ });
+
+ it('should NOT apply default enable:true for workbench when only enableProxyExecution is provided', () => {
+ const result = transformToolRouterUpdateParams({
+ workbench: { enableProxyExecution: true },
+ });
+ const wb = result.workbench as Record<string, unknown>;
+ expect(wb).not.toHaveProperty('enable');
+ expect(wb).toHaveProperty('enable_proxy_execution', true);
+ });
+
+ it('should include enable for manageConnections when explicitly set', () => {
+ const result = transformToolRouterUpdateParams({
+ manageConnections: { enable: false, callbackUrl: 'https://example.com' },
+ });
+ const mc = result.manage_connections as Record<string, unknown>;
+ expect(mc).toHaveProperty('enable', false);
+ expect(mc).toHaveProperty('callback_url', 'https://example.com');
+ });
+});