@devries-cameron20 / composio-1 / commits / 34a3dd6dd
Fork 0

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

feat(ts/core): address Musthaq's comment

jkomyno committed Feb 6, 2026 at 01:08 UTC 34a3dd6dd861d699b1e97cbbb0a76816342b4d50
4 files changed +112 -39
ts/packages/core/src/models/Triggers.ts
+50 -17
@@ -25,6 +25,7 @@ import {
25 WebhookPayloadV1Schema,
26 WebhookPayloadV2Schema,
27 WebhookPayloadV3Schema,
28 + WebhookTriggerPayloadV3Schema,
29 WebhookVersion,
30 WebhookVersions,
31 } from '../types/triggers.types';
@@ -835,29 +836,61 @@ export class Triggers<TProvider extends BaseComposioProvider<unknown, unknown, u
836 private normalizeV3Payload(
837 payload: import('../types/triggers.types').WebhookPayloadV3
838 ): IncomingTriggerPayload {
838 - const triggerSlug = payload.metadata.trigger_slug;
839 - const toolkitSlug = triggerSlug.split('_')[0]?.toUpperCase() || 'UNKNOWN';
839 + // Check if this is a trigger event with trigger-specific metadata
840 + const triggerResult = WebhookTriggerPayloadV3Schema.safeParse(payload);
841 + if (triggerResult.success) {
842 + const triggerSlug = triggerResult.data.metadata.trigger_slug;
843 + const toolkitSlug = triggerSlug.split('_')[0]?.toUpperCase() || 'UNKNOWN';
844
845 + return {
846 + id: triggerResult.data.metadata.trigger_id,
847 + uuid: triggerResult.data.metadata.trigger_id,
848 + triggerSlug,
849 + toolkitSlug,
850 + userId: triggerResult.data.metadata.user_id,
851 + payload: payload.data,
852 + originalPayload: payload.data,
853 + metadata: {
854 + id: triggerResult.data.metadata.trigger_id,
855 + uuid: triggerResult.data.metadata.trigger_id,
856 + toolkitSlug,
857 + triggerSlug,
858 + triggerConfig: {},
859 + connectedAccount: {
860 + id: triggerResult.data.metadata.connected_account_id,
861 + uuid: triggerResult.data.metadata.connected_account_id,
862 + authConfigId: triggerResult.data.metadata.auth_config_id,
863 + authConfigUUID: triggerResult.data.metadata.auth_config_id,
864 + userId: triggerResult.data.metadata.user_id,
865 + status: 'ACTIVE',
866 + },
867 + },
868 + };
869 + }
870 +
871 + // Non-trigger V3 event (e.g., connection expired)
872 + // Return a minimal payload; callers should use specific schemas
873 + // (e.g., ConnectionExpiredEventSchema) on rawPayload for type-safe access
874 return {
842 - id: payload.metadata.trigger_id,
843 - uuid: payload.metadata.trigger_id,
844 - triggerSlug,
845 - toolkitSlug,
846 - userId: payload.metadata.user_id,
875 + id: payload.id,
876 + uuid: payload.id,
877 + triggerSlug: payload.type,
878 + toolkitSlug: 'COMPOSIO',
879 + userId: '',
880 payload: payload.data,
848 - originalPayload: payload.data,
881 + originalPayload: payload as unknown as Record<string, unknown>,
882 metadata: {
850 - id: payload.metadata.trigger_id,
851 - uuid: payload.metadata.trigger_id,
852 - toolkitSlug,
853 - triggerSlug,
883 + id: payload.id,
884 + uuid: payload.id,
885 + toolkitSlug: 'COMPOSIO',
886 + triggerSlug: payload.type,
887 triggerConfig: {},
888 connectedAccount: {
856 - id: payload.metadata.connected_account_id,
857 - uuid: payload.metadata.connected_account_id,
858 - authConfigId: payload.metadata.auth_config_id,
859 - authConfigUUID: payload.metadata.auth_config_id,
860 - userId: payload.metadata.user_id,
889 + id: '',
890 + uuid: '',
891 + authConfigId: '',
892 + authConfigUUID: '',
893 + userId: '',
894 status: 'ACTIVE',
895 },
896 },
ts/packages/core/src/types/triggers.types.ts
+12 -2
@@ -227,13 +227,23 @@ export const WebhookPayloadV2Schema = z.object({
227 });
228 export type WebhookPayloadV2 = z.infer<typeof WebhookPayloadV2Schema>;
229
230 -/** V3 webhook payload - current format with metadata */
230 +/** V3 webhook payload - generic envelope for all composio.* events */
231 export const WebhookPayloadV3Schema = z.object({
232 id: z.string(),
233 timestamp: z.string(),
234 type: z.string().refine(val => val.startsWith('composio.'), {
235 message: "V3 event type must start with 'composio.'",
236 }),
237 + metadata: z.record(z.unknown()),
238 + data: z.record(z.unknown()),
239 +});
240 +export type WebhookPayloadV3 = z.infer<typeof WebhookPayloadV3Schema>;
241 +
242 +/** V3 trigger-specific payload - has trigger metadata fields */
243 +export const WebhookTriggerPayloadV3Schema = z.object({
244 + id: z.string(),
245 + timestamp: z.string(),
246 + type: z.string(),
247 metadata: z
248 .object({
249 log_id: z.string(),
@@ -246,7 +256,7 @@ export const WebhookPayloadV3Schema = z.object({
256 .passthrough(),
257 data: z.record(z.unknown()),
258 });
249 -export type WebhookPayloadV3 = z.infer<typeof WebhookPayloadV3Schema>;
259 +export type WebhookTriggerPayloadV3 = z.infer<typeof WebhookTriggerPayloadV3Schema>;
260
261 /** Union of all webhook payload versions */
262 export const WebhookPayloadSchema = z.union([
ts/packages/core/src/types/webhookEvents.types.ts
+37 -9
@@ -17,9 +17,18 @@ export type WebhookEventType = (typeof WebhookEventTypes)[keyof typeof WebhookEv
17 // =============================================================================
18
19 /**
20 - * Auth config details included in the connected account response.
20 + * Auth config schema for webhook payloads (raw API format, snake_case).
21 + *
22 + * This intentionally does NOT reuse ConnectedAccountAuthConfigSchema from
23 + * connectedAccounts.types.ts because:
24 + * - Webhook payloads arrive in raw snake_case (is_composio_managed, is_disabled)
25 + * while the SDK schema uses camelCase (isComposioManaged, isDisabled)
26 + * - Webhook payloads include additional fields (auth_scheme, deprecated)
27 + * not present in the SDK schema
28 + *
29 + * @see connectedAccounts.types.ts for the camelCase SDK version
30 */
22 -export const ConnectedAccountAuthConfigSchema = z.object({
31 +export const WebhookConnectedAccountAuthConfigSchema = z.object({
32 /** The nano ID of the auth config */
33 id: z.string(),
34 /** @deprecated - use state.authScheme instead */
@@ -36,12 +45,21 @@ export const ConnectedAccountAuthConfigSchema = z.object({
45 .optional(),
46 });
47
39 -export type ConnectedAccountAuthConfig = z.infer<typeof ConnectedAccountAuthConfigSchema>;
48 +export type WebhookConnectedAccountAuthConfig = z.infer<
49 + typeof WebhookConnectedAccountAuthConfigSchema
50 +>;
51
52 /**
42 - * Simplified connection state schema for webhook payloads.
43 - * The full ConnectionDataSchema is too complex for webhook validation;
44 - * this schema accepts any auth scheme and state values.
53 + * Simplified connection state schema for webhook payloads (raw API format).
54 + *
55 + * This intentionally does NOT reuse ConnectionDataSchema from
56 + * connectedAccountAuthStates.types.ts because:
57 + * - ConnectionDataSchema is a discriminated union requiring typed `val`
58 + * per auth scheme (e.g., Oauth2ConnectionDataSchema)
59 + * - Webhook payloads need loose validation (z.record(z.unknown()))
60 + * since the connection state varies and isn't the focus of event handling
61 + *
62 + * @see connectedAccountAuthStates.types.ts for the full typed version
63 */
64 export const WebhookConnectionStateSchema = z.object({
65 /** The auth scheme type (e.g., 'OAUTH2', 'API_KEY') */
@@ -53,8 +71,18 @@ export const WebhookConnectionStateSchema = z.object({
71 export type WebhookConnectionState = z.infer<typeof WebhookConnectionStateSchema>;
72
73 /**
56 - * Connected account data schema matching GET /api/v3/connected_accounts/{id} response.
57 - * Used in webhook payloads for connection lifecycle events.
74 + * Connected account data schema for webhook payloads (raw API format, snake_case).
75 + *
76 + * This intentionally does NOT reuse ConnectedAccountRetrieveResponseSchema from
77 + * connectedAccounts.types.ts because:
78 + * - Webhook payloads arrive in raw snake_case (auth_config, user_id, created_at, etc.)
79 + * while the SDK schema uses camelCase (authConfig, createdAt, etc.)
80 + * - The SDK applies a transformation layer (utils/transformers/connectedAccounts.ts)
81 + * before validation; webhooks are validated directly by the user
82 + * - Webhook payloads include user_id, which the SDK schema omits
83 + *
84 + * @see connectedAccounts.types.ts for the camelCase SDK version
85 + * @see utils/transformers/connectedAccounts.ts for the snake-to-camel transformation
86 */
87 export const SingleConnectedAccountDetailedResponseSchema = z.object({
88 /** Toolkit information */
@@ -62,7 +90,7 @@ export const SingleConnectedAccountDetailedResponseSchema = z.object({
90 slug: z.string().describe('The slug of the toolkit'),
91 }),
92 /** Auth config details */
65 - auth_config: ConnectedAccountAuthConfigSchema,
93 + auth_config: WebhookConnectedAccountAuthConfigSchema,
94 /** The nano ID of the connected account */
95 id: z.string(),
96 /** @deprecated - user ID of the connection owner */
ts/packages/core/test/models/verifyWebhook.test.ts
+13 -11
@@ -162,21 +162,23 @@ describe('Triggers.verifyWebhook', () => {
162 });
163
164 it('should detect V3 payload with non-trigger event type (e.g., connected_account.expired)', async () => {
165 - // This test verifies the fix for PLEN-1397: V3 payloads with event types other than
166 - // 'composio.trigger.message' should still be detected as V3, not fall back to V2
165 + // This test uses realistic connection metadata (project_id, org_id)
166 + // instead of fabricated trigger metadata, verifying that V3 detection
167 + // works for events with different metadata shapes.
168 const payload = {
169 id: 'msg_abc123',
170 timestamp: new Date().toISOString(),
170 - type: 'composio.connected_account.expired', // Not 'composio.trigger.message'
171 + type: 'composio.connected_account.expired',
172 metadata: {
172 - log_id: 'log-123',
173 - trigger_slug: '',
174 - trigger_id: '',
175 - connected_account_id: 'conn-123',
176 - auth_config_id: 'auth-123',
177 - user_id: 'user-456',
173 + project_id: 'pr_koucdrMIwRsf',
174 + org_id: '4a4ded8f-d3ae-4dea-a229-c30234298b05',
175 + },
176 + data: {
177 + toolkit: { slug: 'gmail' },
178 + id: 'ca__IvSeEzEBjVt',
179 + user_id: 'test-user',
180 + status: 'EXPIRED',
181 },
179 - data: { reason: 'token_expired' },
182 };
183 const payloadStr = JSON.stringify(payload);
184 const signature = createSignature(testWebhookId, testTimestamp, payloadStr, testSecret);
@@ -189,7 +191,7 @@ describe('Triggers.verifyWebhook', () => {
191 timestamp: testTimestamp,
192 });
193
192 - expect(result.version).toBe(WebhookVersions.V3); // Should be V3, not V2
194 + expect(result.version).toBe(WebhookVersions.V3);
195 expect(result.rawPayload).toEqual(payload);
196 });
197 });