Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Mirrored from https://github.com/Camo67/composio.git
Synced Nov 21, 04:45 UTC
| 1 | import { z } from 'zod/v3'; |
| 2 | import { ValidationError } from '../errors/ValidationErrors'; |
| 3 | |
| 4 | const SESSION_CONFIG_CONFLICT_MARKER = 'sessionConfigConflict'; |
| 5 | |
| 6 | /** |
| 7 | * Adds the issue raised when a saved Session config (`experimental.sessionConfigId`) |
| 8 | * is combined with inline access fields. `fields` lists every conflicting |
| 9 | * field that was provided; nothing is added when it is empty. |
| 10 | */ |
| 11 | export const addSessionConfigConflictIssue = (ctx: z.RefinementCtx, fields: string[]): void => { |
| 12 | if (fields.length === 0) { |
| 13 | return; |
| 14 | } |
| 15 | ctx.addIssue({ |
| 16 | code: z.ZodIssueCode.custom, |
| 17 | message: `experimental.sessionConfigId cannot be combined with ${fields.join(', ')}. A saved Session config sets the session's toolkit, tool and tag access; remove ${fields.length === 1 ? 'that field' : 'those fields'} or sessionConfigId`, |
| 18 | path: ['experimental', 'sessionConfigId'], |
| 19 | params: { [SESSION_CONFIG_CONFLICT_MARKER]: true }, |
| 20 | }); |
| 21 | }; |
| 22 | |
| 23 | /** |
| 24 | * Parses session create or update input. A saved Session config combined with |
| 25 | * inline access fields throws `ValidationError`; any other failure rethrows the |
| 26 | * original `ZodError`, as `schema.parse()` would. |
| 27 | */ |
| 28 | export const parseSessionConfigInput = <TSchema extends z.ZodTypeAny>( |
| 29 | schema: TSchema, |
| 30 | input: unknown |
| 31 | ): z.output<TSchema> => { |
| 32 | const result = schema.safeParse(input); |
| 33 | if (result.success) { |
| 34 | return result.data; |
| 35 | } |
| 36 | const isConflict = result.error.issues.some( |
| 37 | issue => issue.code === z.ZodIssueCode.custom && issue.params?.[SESSION_CONFIG_CONFLICT_MARKER] |
| 38 | ); |
| 39 | if (isConflict) { |
| 40 | throw new ValidationError('Invalid session config', { cause: result.error }); |
| 41 | } |
| 42 | throw result.error; |
| 43 | }; |