27
import { ConnectionRequest } from '../types/connectionRequest.types';
28
import { createConnectionRequest } from './ConnectionRequest';
29
import {
30
+ ConnectedAccountAclConfigSchema,
31
ConnectedAccountStatuses,
32
ConnectedAccountType,
33
+ ConnectedAccountTypeSchema,
34
ConnectedAccountAclConfig,
35
} from '../types/connectedAccounts.types';
36
+import { z } from 'zod/v3';
37
import { transform } from '../utils/transform';
38
import { ToolkitConnectionStateSchema } from '../types/toolRouter.types';
39
import { ComposioAclOnlyForSharedError, ValidationError } from '../errors';
72
export const DIRECT_CUSTOM_TOOL_DESCRIPTION_PREFIX =
73
'[Direct tool - call directly, no search needed beforehand.]';
74
75
+/**
76
+ * Options accepted by {@link ToolRouterSession.authorize}.
77
+ *
78
+ * Validated at the SDK boundary so callers get clear `ValidationError`s for
79
+ * oversized ACL lists or invalid user IDs — same Zod caps as the equivalent
80
+ * `composio.connectedAccounts.link()` path (≤1000 entries per list, each
81
+ * user_id 1..256 chars). Caught Bugbot review.
82
+ */
83
+const AuthorizeOptionsSchema = z.object({
84
+ callbackUrl: z.string().optional(),
85
+ alias: z.string().optional(),
86
+ accountType: ConnectedAccountTypeSchema.optional(),
87
+ aclConfigForShared: ConnectedAccountAclConfigSchema.optional(),
88
+});
89
+
90
export class ToolRouterSession<
91
TToolCollection,
92
TTool,
330
* `accountType` and `aclConfigForShared` (Hermes #9860, #9902) let the
331
* caller create a SHARED connection with a per-user ACL in one flow.
332
* Default behaviour (omit both) creates a PRIVATE connection.
333
+ *
334
+ * `aclConfigForShared` is validated through `ConnectedAccountAclConfigSchema`
335
+ * — same Zod caps as `composio.connectedAccounts.link()` (≤1000 entries
336
+ * per list, each user_id 1..256 chars). Invalid input throws
337
+ * `ValidationError` at the SDK boundary instead of hitting an opaque
338
+ * backend 400.
339
*/
340
async authorize(
341
toolkit: string,
346
aclConfigForShared?: ConnectedAccountAclConfig;
347
}
348
): Promise<ConnectionRequest> {
349
+ const requestOptions = AuthorizeOptionsSchema.safeParse(options ?? {});
350
+ if (!requestOptions.success) {
351
+ throw new ValidationError('Failed to parse tool router authorize options', {
352
+ cause: requestOptions.error,
353
+ });
354
+ }
355
+ const opts = requestOptions.data;
356
const aclWire: SessionLinkParams.ACLConfigForShared | undefined =
326
- options?.aclConfigForShared === undefined
357
+ opts.aclConfigForShared === undefined
358
? undefined
359
: {
329
- ...(options.aclConfigForShared.allowAllUsers !== undefined && {
330
- allow_all_users: options.aclConfigForShared.allowAllUsers,
360
+ ...(opts.aclConfigForShared.allowAllUsers !== undefined && {
361
+ allow_all_users: opts.aclConfigForShared.allowAllUsers,
362
}),
332
- ...(options.aclConfigForShared.allowedUserIds !== undefined && {
333
- allowed_user_ids: options.aclConfigForShared.allowedUserIds,
363
+ ...(opts.aclConfigForShared.allowedUserIds !== undefined && {
364
+ allowed_user_ids: opts.aclConfigForShared.allowedUserIds,
365
}),
335
- ...(options.aclConfigForShared.notAllowedUserIds !== undefined && {
336
- not_allowed_user_ids: options.aclConfigForShared.notAllowedUserIds,
366
+ ...(opts.aclConfigForShared.notAllowedUserIds !== undefined && {
367
+ not_allowed_user_ids: opts.aclConfigForShared.notAllowedUserIds,
368
}),
369
};
370
const body: SessionLinkParams = {
371
toolkit,
341
- ...(options?.callbackUrl !== undefined && { callback_url: options.callbackUrl }),
342
- ...(options?.alias !== undefined && { alias: options.alias }),
343
- ...(options?.accountType !== undefined && { account_type: options.accountType }),
372
+ ...(opts.callbackUrl !== undefined && { callback_url: opts.callbackUrl }),
373
+ ...(opts.alias !== undefined && { alias: opts.alias }),
374
+ ...(opts.accountType !== undefined && { account_type: opts.accountType }),
375
...(aclWire !== undefined && { acl_config_for_shared: aclWire }),
376
};
377