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 incoming payload of triggers

haxzie23146a173956
diff --git a/examples/triggers/src/index.ts b/examples/triggers/src/index.ts
index ccf83cb4f..9af768f85 100644
--- a/examples/triggers/src/index.ts
+++ b/examples/triggers/src/index.ts
@@ -42,7 +42,7 @@ async function main() {
);
composio.triggers.subscribe(data => {
- console.log('πŸ”” Trigger received:', data);
+ console.log('πŸ”” Trigger received:', JSON.stringify(data, null, 2));
});
} catch (error) {
console.error('❌ Error running example:', error);
diff --git a/packages/core/src/models/Triggers.ts b/packages/core/src/models/Triggers.ts
index e2b4c313e..9327a47df 100644
--- a/packages/core/src/models/Triggers.ts
+++ b/packages/core/src/models/Triggers.ts
@@ -263,6 +263,84 @@ export class Triggers {
return this.client.triggersTypes.retrieveEnum();
}
+ /**
+ * Applies compound filters to the trigger data
+ * @param data data to apply filters to
+ * @returns True if the trigger data matches the filters, false otherwise
+ */
+ private shouldSendTriggerAfterFilters(
+ filters: TriggerSubscribeParams,
+ data: IncomingTriggerPayload
+ ): boolean {
+ // Check if toolkits filter is provided and matches
+ if (
+ filters.toolkits?.length &&
+ !filters.toolkits
+ .map(toolkit => toolkit.toLowerCase())
+ .includes(data.toolkitSlug.toLowerCase())
+ ) {
+ logger.debug(
+ 'Trigger does not match toolkits filter',
+ JSON.stringify(filters.toolkits, null, 2)
+ );
+ return false;
+ }
+
+ // Check if triggerId filter matches
+ if (filters.triggerId && filters.triggerId !== data.id) {
+ logger.debug(
+ 'Trigger does not match triggerId filter',
+ JSON.stringify(filters.triggerId, null, 2)
+ );
+ return false;
+ }
+
+ // Check if connectedAccountId filter matches
+ if (
+ filters.connectedAccountId &&
+ filters.connectedAccountId !== data.metadata.connectedAccount.id
+ ) {
+ logger.debug(
+ 'Trigger does not match connectedAccountId filter',
+ JSON.stringify(filters.connectedAccountId, null, 2)
+ );
+ return false;
+ }
+
+ // Check if triggerName filter matches
+ if (
+ filters.triggerSlug?.length &&
+ !filters.triggerSlug
+ .map(triggerSlug => triggerSlug.toLowerCase())
+ .includes(data.triggerSlug.toLowerCase())
+ ) {
+ logger.debug(
+ 'Trigger does not match triggerSlug filter',
+ JSON.stringify(filters.triggerSlug, null, 2)
+ );
+ return false;
+ }
+
+ // Check if triggerData filter matches
+ if (filters.triggerData && filters.triggerData !== data.metadata.triggerData) {
+ logger.debug(
+ 'Trigger does not match triggerData filter',
+ JSON.stringify(filters.triggerData, null, 2)
+ );
+ return false;
+ }
+
+ // Check if userId (clientUniqueUserId) filter matches
+ if (filters.userId && filters.userId !== data.metadata.connectedAccount.userId) {
+ logger.debug('Trigger does not match userId filter', JSON.stringify(filters.userId, null, 2));
+ return false;
+ }
+
+ logger.debug('Trigger matches all filters', JSON.stringify(filters, null, 2));
+ // If all filters pass or no filters were provided, return true
+ return true;
+ }
+
/**
* Subscribe to all the triggers
*
@@ -291,90 +369,6 @@ export class Triggers {
});
}
- /**
- * Applies compound filters to the trigger data
- * @param data data to apply filters to
- * @returns True if the trigger data matches the filters, false otherwise
- */
- const shouldSendTriggerAfterFilters = (data: IncomingTriggerPayload): boolean => {
- // Check if toolkits filter is provided and matches
- if (
- parsedFilters.data.toolkits?.length &&
- !parsedFilters.data.toolkits
- .map(toolkit => toolkit.toLowerCase())
- .includes(data.toolkitSlug.toLowerCase())
- ) {
- logger.debug(
- 'Trigger does not match toolkits filter',
- JSON.stringify(parsedFilters.data.toolkits, null, 2)
- );
- return false;
- }
-
- // Check if triggerId filter matches
- if (parsedFilters.data.triggerId && parsedFilters.data.triggerId !== data.id) {
- logger.debug(
- 'Trigger does not match triggerId filter',
- JSON.stringify(parsedFilters.data.triggerId, null, 2)
- );
- return false;
- }
-
- // Check if connectedAccountId filter matches
- if (
- parsedFilters.data.connectedAccountId &&
- parsedFilters.data.connectedAccountId !== data.metadata.connectedAccount.id
- ) {
- logger.debug(
- 'Trigger does not match connectedAccountId filter',
- JSON.stringify(parsedFilters.data.connectedAccountId, null, 2)
- );
- return false;
- }
-
- // Check if triggerName filter matches
- if (
- parsedFilters.data.triggerSlug?.length &&
- !parsedFilters.data.triggerSlug
- .map(triggerSlug => triggerSlug.toLowerCase())
- .includes(data.triggerSlug.toLowerCase())
- ) {
- logger.debug(
- 'Trigger does not match triggerSlug filter',
- JSON.stringify(parsedFilters.data.triggerSlug, null, 2)
- );
- return false;
- }
-
- // Check if triggerData filter matches
- if (
- parsedFilters.data.triggerData &&
- parsedFilters.data.triggerData !== data.metadata.triggerData
- ) {
- logger.debug(
- 'Trigger does not match triggerData filter',
- JSON.stringify(parsedFilters.data.triggerData, null, 2)
- );
- return false;
- }
-
- // Check if userId (clientUniqueUserId) filter matches
- if (
- parsedFilters.data.userId &&
- parsedFilters.data.userId !== data.metadata.connectedAccount.userId
- ) {
- logger.debug(
- 'Trigger does not match userId filter',
- JSON.stringify(parsedFilters.data.userId, null, 2)
- );
- return false;
- }
-
- logger.debug('Trigger matches all filters', JSON.stringify(parsedFilters.data, null, 2));
- // If all filters pass or no filters were provided, return true
- return true;
- };
-
logger.debug('πŸ”„ Subscribing to triggers with filters: ', JSON.stringify(filters, null, 2));
await this.pusherService.subscribe((_data: Record<string, unknown>) => {
logger.debug('Received raw trigger data', JSON.stringify(_data, null, 2));
@@ -382,14 +376,16 @@ export class Triggers {
// ideally we should have a type for the trigger data
const data = _data as TriggerData;
const parsedData = IncomingTriggerPayloadSchema.safeParse({
- id: data.metadata.id,
+ id: data.metadata.nanoId,
+ uuid: data.metadata.id,
triggerSlug: data.metadata.triggerName,
toolkitSlug: data.appName,
userId: data.metadata.connection?.clientUniqueUserId,
payload: data.payload,
originalPayload: data.originalPayload,
metadata: {
- id: data.metadata.id,
+ id: data.metadata.nanoId,
+ uuid: data.metadata.id,
triggerConfig: data.metadata.triggerConfig,
triggerSlug: data.metadata.triggerName,
toolkitSlug: data.appName,
@@ -412,7 +408,7 @@ export class Triggers {
cause: parsedData.error,
});
}
- if (shouldSendTriggerAfterFilters(parsedData.data)) {
+ if (this.shouldSendTriggerAfterFilters(parsedFilters.data, parsedData.data)) {
try {
fn(parsedData.data);
} catch (error) {
diff --git a/packages/core/src/types/triggers.types.ts b/packages/core/src/types/triggers.types.ts
index 0546834e2..94a319f12 100644
--- a/packages/core/src/types/triggers.types.ts
+++ b/packages/core/src/types/triggers.types.ts
@@ -94,6 +94,7 @@ export type TriggerInstanceManageDeleteResponse = z.infer<
export const IncomingTriggerPayloadSchema = z.object({
id: z.string().describe('The ID of the trigger'),
+ uuid: z.string().describe('The UUID of the trigger'),
triggerSlug: z.string().describe('The slug of the trigger that triggered the event'),
toolkitSlug: z.string().describe('The slug of the toolkit that triggered the event'),
userId: z.string().describe('The ID of the user that triggered the event'),
@@ -101,6 +102,7 @@ export const IncomingTriggerPayloadSchema = z.object({
originalPayload: z.record(z.unknown()).describe('The original payload of the trigger').optional(),
metadata: z.object({
id: z.string(),
+ uuid: z.string(),
toolkitSlug: z.string(),
triggerSlug: z.string(),
triggerData: z.string().optional(),
@@ -125,6 +127,7 @@ export type TriggerData = {
originalPayload: Record<string, unknown>;
metadata: {
id: string;
+ nanoId: string;
triggerName: string;
triggerData: string;
triggerConfig: Record<string, unknown>;