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.

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

jkomyno34a3dd6dd861
diff --git a/ts/packages/core/src/models/Triggers.ts b/ts/packages/core/src/models/Triggers.ts
index c57afb080..1454aa7ff 100644
--- a/ts/packages/core/src/models/Triggers.ts
+++ b/ts/packages/core/src/models/Triggers.ts
@@ -25,6 +25,7 @@ import {
WebhookPayloadV1Schema,
WebhookPayloadV2Schema,
WebhookPayloadV3Schema,
+ WebhookTriggerPayloadV3Schema,
WebhookVersion,
WebhookVersions,
} from '../types/triggers.types';
@@ -835,29 +836,61 @@ export class Triggers<TProvider extends BaseComposioProvider<unknown, unknown, u
private normalizeV3Payload(
payload: import('../types/triggers.types').WebhookPayloadV3
): IncomingTriggerPayload {
- const triggerSlug = payload.metadata.trigger_slug;
- const toolkitSlug = triggerSlug.split('_')[0]?.toUpperCase() || 'UNKNOWN';
+ // Check if this is a trigger event with trigger-specific metadata
+ const triggerResult = WebhookTriggerPayloadV3Schema.safeParse(payload);
+ if (triggerResult.success) {
+ const triggerSlug = triggerResult.data.metadata.trigger_slug;
+ const toolkitSlug = triggerSlug.split('_')[0]?.toUpperCase() || 'UNKNOWN';
+ return {
+ id: triggerResult.data.metadata.trigger_id,
+ uuid: triggerResult.data.metadata.trigger_id,
+ triggerSlug,
+ toolkitSlug,
+ userId: triggerResult.data.metadata.user_id,
+ payload: payload.data,
+ originalPayload: payload.data,
+ metadata: {
+ id: triggerResult.data.metadata.trigger_id,
+ uuid: triggerResult.data.metadata.trigger_id,
+ toolkitSlug,
+ triggerSlug,
+ triggerConfig: {},
+ connectedAccount: {
+ id: triggerResult.data.metadata.connected_account_id,
+ uuid: triggerResult.data.metadata.connected_account_id,
+ authConfigId: triggerResult.data.metadata.auth_config_id,
+ authConfigUUID: triggerResult.data.metadata.auth_config_id,
+ userId: triggerResult.data.metadata.user_id,
+ status: 'ACTIVE',
+ },
+ },
+ };
+ }
+
+ // Non-trigger V3 event (e.g., connection expired)
+ // Return a minimal payload; callers should use specific schemas
+ // (e.g., ConnectionExpiredEventSchema) on rawPayload for type-safe access
return {
- id: payload.metadata.trigger_id,
- uuid: payload.metadata.trigger_id,
- triggerSlug,
- toolkitSlug,
- userId: payload.metadata.user_id,
+ id: payload.id,
+ uuid: payload.id,
+ triggerSlug: payload.type,
+ toolkitSlug: 'COMPOSIO',
+ userId: '',
payload: payload.data,
- originalPayload: payload.data,
+ originalPayload: payload as unknown as Record<string, unknown>,
metadata: {
- id: payload.metadata.trigger_id,
- uuid: payload.metadata.trigger_id,
- toolkitSlug,
- triggerSlug,
+ id: payload.id,
+ uuid: payload.id,
+ toolkitSlug: 'COMPOSIO',
+ triggerSlug: payload.type,
triggerConfig: {},
connectedAccount: {
- id: payload.metadata.connected_account_id,
- uuid: payload.metadata.connected_account_id,
- authConfigId: payload.metadata.auth_config_id,
- authConfigUUID: payload.metadata.auth_config_id,
- userId: payload.metadata.user_id,
+ id: '',
+ uuid: '',
+ authConfigId: '',
+ authConfigUUID: '',
+ userId: '',
status: 'ACTIVE',
},
},
diff --git a/ts/packages/core/src/types/triggers.types.ts b/ts/packages/core/src/types/triggers.types.ts
index 15b337587..cd423e33b 100644
--- a/ts/packages/core/src/types/triggers.types.ts
+++ b/ts/packages/core/src/types/triggers.types.ts
@@ -227,13 +227,23 @@ export const WebhookPayloadV2Schema = z.object({
});
export type WebhookPayloadV2 = z.infer<typeof WebhookPayloadV2Schema>;
-/** V3 webhook payload - current format with metadata */
+/** V3 webhook payload - generic envelope for all composio.* events */
export const WebhookPayloadV3Schema = z.object({
id: z.string(),
timestamp: z.string(),
type: z.string().refine(val => val.startsWith('composio.'), {
message: "V3 event type must start with 'composio.'",
}),
+ metadata: z.record(z.unknown()),
+ data: z.record(z.unknown()),
+});
+export type WebhookPayloadV3 = z.infer<typeof WebhookPayloadV3Schema>;
+
+/** V3 trigger-specific payload - has trigger metadata fields */
+export const WebhookTriggerPayloadV3Schema = z.object({
+ id: z.string(),
+ timestamp: z.string(),
+ type: z.string(),
metadata: z
.object({
log_id: z.string(),
@@ -246,7 +256,7 @@ export const WebhookPayloadV3Schema = z.object({
.passthrough(),
data: z.record(z.unknown()),
});
-export type WebhookPayloadV3 = z.infer<typeof WebhookPayloadV3Schema>;
+export type WebhookTriggerPayloadV3 = z.infer<typeof WebhookTriggerPayloadV3Schema>;
/** Union of all webhook payload versions */
export const WebhookPayloadSchema = z.union([
diff --git a/ts/packages/core/src/types/webhookEvents.types.ts b/ts/packages/core/src/types/webhookEvents.types.ts
index 4993e6b08..e22f1a551 100644
--- a/ts/packages/core/src/types/webhookEvents.types.ts
+++ b/ts/packages/core/src/types/webhookEvents.types.ts
@@ -17,9 +17,18 @@ export type WebhookEventType = (typeof WebhookEventTypes)[keyof typeof WebhookEv
// =============================================================================
/**
- * Auth config details included in the connected account response.
+ * Auth config schema for webhook payloads (raw API format, snake_case).
+ *
+ * This intentionally does NOT reuse ConnectedAccountAuthConfigSchema from
+ * connectedAccounts.types.ts because:
+ * - Webhook payloads arrive in raw snake_case (is_composio_managed, is_disabled)
+ * while the SDK schema uses camelCase (isComposioManaged, isDisabled)
+ * - Webhook payloads include additional fields (auth_scheme, deprecated)
+ * not present in the SDK schema
+ *
+ * @see connectedAccounts.types.ts for the camelCase SDK version
*/
-export const ConnectedAccountAuthConfigSchema = z.object({
+export const WebhookConnectedAccountAuthConfigSchema = z.object({
/** The nano ID of the auth config */
id: z.string(),
/** @deprecated - use state.authScheme instead */
@@ -36,12 +45,21 @@ export const ConnectedAccountAuthConfigSchema = z.object({
.optional(),
});
-export type ConnectedAccountAuthConfig = z.infer<typeof ConnectedAccountAuthConfigSchema>;
+export type WebhookConnectedAccountAuthConfig = z.infer<
+ typeof WebhookConnectedAccountAuthConfigSchema
+>;
/**
- * Simplified connection state schema for webhook payloads.
- * The full ConnectionDataSchema is too complex for webhook validation;
- * this schema accepts any auth scheme and state values.
+ * Simplified connection state schema for webhook payloads (raw API format).
+ *
+ * This intentionally does NOT reuse ConnectionDataSchema from
+ * connectedAccountAuthStates.types.ts because:
+ * - ConnectionDataSchema is a discriminated union requiring typed `val`
+ * per auth scheme (e.g., Oauth2ConnectionDataSchema)
+ * - Webhook payloads need loose validation (z.record(z.unknown()))
+ * since the connection state varies and isn't the focus of event handling
+ *
+ * @see connectedAccountAuthStates.types.ts for the full typed version
*/
export const WebhookConnectionStateSchema = z.object({
/** The auth scheme type (e.g., 'OAUTH2', 'API_KEY') */
@@ -53,8 +71,18 @@ export const WebhookConnectionStateSchema = z.object({
export type WebhookConnectionState = z.infer<typeof WebhookConnectionStateSchema>;
/**
- * Connected account data schema matching GET /api/v3/connected_accounts/{id} response.
- * Used in webhook payloads for connection lifecycle events.
+ * Connected account data schema for webhook payloads (raw API format, snake_case).
+ *
+ * This intentionally does NOT reuse ConnectedAccountRetrieveResponseSchema from
+ * connectedAccounts.types.ts because:
+ * - Webhook payloads arrive in raw snake_case (auth_config, user_id, created_at, etc.)
+ * while the SDK schema uses camelCase (authConfig, createdAt, etc.)
+ * - The SDK applies a transformation layer (utils/transformers/connectedAccounts.ts)
+ * before validation; webhooks are validated directly by the user
+ * - Webhook payloads include user_id, which the SDK schema omits
+ *
+ * @see connectedAccounts.types.ts for the camelCase SDK version
+ * @see utils/transformers/connectedAccounts.ts for the snake-to-camel transformation
*/
export const SingleConnectedAccountDetailedResponseSchema = z.object({
/** Toolkit information */
@@ -62,7 +90,7 @@ export const SingleConnectedAccountDetailedResponseSchema = z.object({
slug: z.string().describe('The slug of the toolkit'),
}),
/** Auth config details */
- auth_config: ConnectedAccountAuthConfigSchema,
+ auth_config: WebhookConnectedAccountAuthConfigSchema,
/** The nano ID of the connected account */
id: z.string(),
/** @deprecated - user ID of the connection owner */
diff --git a/ts/packages/core/test/models/verifyWebhook.test.ts b/ts/packages/core/test/models/verifyWebhook.test.ts
index 14d7e2c9c..b401c39c2 100644
--- a/ts/packages/core/test/models/verifyWebhook.test.ts
+++ b/ts/packages/core/test/models/verifyWebhook.test.ts
@@ -162,21 +162,23 @@ describe('Triggers.verifyWebhook', () => {
});
it('should detect V3 payload with non-trigger event type (e.g., connected_account.expired)', async () => {
- // This test verifies the fix for PLEN-1397: V3 payloads with event types other than
- // 'composio.trigger.message' should still be detected as V3, not fall back to V2
+ // This test uses realistic connection metadata (project_id, org_id)
+ // instead of fabricated trigger metadata, verifying that V3 detection
+ // works for events with different metadata shapes.
const payload = {
id: 'msg_abc123',
timestamp: new Date().toISOString(),
- type: 'composio.connected_account.expired', // Not 'composio.trigger.message'
+ type: 'composio.connected_account.expired',
metadata: {
- log_id: 'log-123',
- trigger_slug: '',
- trigger_id: '',
- connected_account_id: 'conn-123',
- auth_config_id: 'auth-123',
- user_id: 'user-456',
+ project_id: 'pr_koucdrMIwRsf',
+ org_id: '4a4ded8f-d3ae-4dea-a229-c30234298b05',
+ },
+ data: {
+ toolkit: { slug: 'gmail' },
+ id: 'ca__IvSeEzEBjVt',
+ user_id: 'test-user',
+ status: 'EXPIRED',
},
- data: { reason: 'token_expired' },
};
const payloadStr = JSON.stringify(payload);
const signature = createSignature(testWebhookId, testTimestamp, payloadStr, testSecret);
@@ -189,7 +191,7 @@ describe('Triggers.verifyWebhook', () => {
timestamp: testTimestamp,
});
- expect(result.version).toBe(WebhookVersions.V3); // Should be V3, not V2
+ expect(result.version).toBe(WebhookVersions.V3);
expect(result.rawPayload).toEqual(payload);
});
});