@devries-cameron20 / composio-1 / commits / 23146a173
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.

Fix incoming payload of triggers

haxzie committed Jun 16, 2025 at 18:39 UTC 23146a173956c6515c9e131ca64e2345f273610e
3 files changed +87 -88
examples/triggers/src/index.ts
+1 -1
@@ -42,7 +42,7 @@ async function main() {
42 );
43
44 composio.triggers.subscribe(data => {
45 - console.log('🔔 Trigger received:', data);
45 + console.log('🔔 Trigger received:', JSON.stringify(data, null, 2));
46 });
47 } catch (error) {
48 console.error('❌ Error running example:', error);
packages/core/src/models/Triggers.ts
+83 -87
@@ -263,6 +263,84 @@ export class Triggers {
263 return this.client.triggersTypes.retrieveEnum();
264 }
265
266 + /**
267 + * Applies compound filters to the trigger data
268 + * @param data data to apply filters to
269 + * @returns True if the trigger data matches the filters, false otherwise
270 + */
271 + private shouldSendTriggerAfterFilters(
272 + filters: TriggerSubscribeParams,
273 + data: IncomingTriggerPayload
274 + ): boolean {
275 + // Check if toolkits filter is provided and matches
276 + if (
277 + filters.toolkits?.length &&
278 + !filters.toolkits
279 + .map(toolkit => toolkit.toLowerCase())
280 + .includes(data.toolkitSlug.toLowerCase())
281 + ) {
282 + logger.debug(
283 + 'Trigger does not match toolkits filter',
284 + JSON.stringify(filters.toolkits, null, 2)
285 + );
286 + return false;
287 + }
288 +
289 + // Check if triggerId filter matches
290 + if (filters.triggerId && filters.triggerId !== data.id) {
291 + logger.debug(
292 + 'Trigger does not match triggerId filter',
293 + JSON.stringify(filters.triggerId, null, 2)
294 + );
295 + return false;
296 + }
297 +
298 + // Check if connectedAccountId filter matches
299 + if (
300 + filters.connectedAccountId &&
301 + filters.connectedAccountId !== data.metadata.connectedAccount.id
302 + ) {
303 + logger.debug(
304 + 'Trigger does not match connectedAccountId filter',
305 + JSON.stringify(filters.connectedAccountId, null, 2)
306 + );
307 + return false;
308 + }
309 +
310 + // Check if triggerName filter matches
311 + if (
312 + filters.triggerSlug?.length &&
313 + !filters.triggerSlug
314 + .map(triggerSlug => triggerSlug.toLowerCase())
315 + .includes(data.triggerSlug.toLowerCase())
316 + ) {
317 + logger.debug(
318 + 'Trigger does not match triggerSlug filter',
319 + JSON.stringify(filters.triggerSlug, null, 2)
320 + );
321 + return false;
322 + }
323 +
324 + // Check if triggerData filter matches
325 + if (filters.triggerData && filters.triggerData !== data.metadata.triggerData) {
326 + logger.debug(
327 + 'Trigger does not match triggerData filter',
328 + JSON.stringify(filters.triggerData, null, 2)
329 + );
330 + return false;
331 + }
332 +
333 + // Check if userId (clientUniqueUserId) filter matches
334 + if (filters.userId && filters.userId !== data.metadata.connectedAccount.userId) {
335 + logger.debug('Trigger does not match userId filter', JSON.stringify(filters.userId, null, 2));
336 + return false;
337 + }
338 +
339 + logger.debug('Trigger matches all filters', JSON.stringify(filters, null, 2));
340 + // If all filters pass or no filters were provided, return true
341 + return true;
342 + }
343 +
344 /**
345 * Subscribe to all the triggers
346 *
@@ -291,90 +369,6 @@ export class Triggers {
369 });
370 }
371
294 - /**
295 - * Applies compound filters to the trigger data
296 - * @param data data to apply filters to
297 - * @returns True if the trigger data matches the filters, false otherwise
298 - */
299 - const shouldSendTriggerAfterFilters = (data: IncomingTriggerPayload): boolean => {
300 - // Check if toolkits filter is provided and matches
301 - if (
302 - parsedFilters.data.toolkits?.length &&
303 - !parsedFilters.data.toolkits
304 - .map(toolkit => toolkit.toLowerCase())
305 - .includes(data.toolkitSlug.toLowerCase())
306 - ) {
307 - logger.debug(
308 - 'Trigger does not match toolkits filter',
309 - JSON.stringify(parsedFilters.data.toolkits, null, 2)
310 - );
311 - return false;
312 - }
313 -
314 - // Check if triggerId filter matches
315 - if (parsedFilters.data.triggerId && parsedFilters.data.triggerId !== data.id) {
316 - logger.debug(
317 - 'Trigger does not match triggerId filter',
318 - JSON.stringify(parsedFilters.data.triggerId, null, 2)
319 - );
320 - return false;
321 - }
322 -
323 - // Check if connectedAccountId filter matches
324 - if (
325 - parsedFilters.data.connectedAccountId &&
326 - parsedFilters.data.connectedAccountId !== data.metadata.connectedAccount.id
327 - ) {
328 - logger.debug(
329 - 'Trigger does not match connectedAccountId filter',
330 - JSON.stringify(parsedFilters.data.connectedAccountId, null, 2)
331 - );
332 - return false;
333 - }
334 -
335 - // Check if triggerName filter matches
336 - if (
337 - parsedFilters.data.triggerSlug?.length &&
338 - !parsedFilters.data.triggerSlug
339 - .map(triggerSlug => triggerSlug.toLowerCase())
340 - .includes(data.triggerSlug.toLowerCase())
341 - ) {
342 - logger.debug(
343 - 'Trigger does not match triggerSlug filter',
344 - JSON.stringify(parsedFilters.data.triggerSlug, null, 2)
345 - );
346 - return false;
347 - }
348 -
349 - // Check if triggerData filter matches
350 - if (
351 - parsedFilters.data.triggerData &&
352 - parsedFilters.data.triggerData !== data.metadata.triggerData
353 - ) {
354 - logger.debug(
355 - 'Trigger does not match triggerData filter',
356 - JSON.stringify(parsedFilters.data.triggerData, null, 2)
357 - );
358 - return false;
359 - }
360 -
361 - // Check if userId (clientUniqueUserId) filter matches
362 - if (
363 - parsedFilters.data.userId &&
364 - parsedFilters.data.userId !== data.metadata.connectedAccount.userId
365 - ) {
366 - logger.debug(
367 - 'Trigger does not match userId filter',
368 - JSON.stringify(parsedFilters.data.userId, null, 2)
369 - );
370 - return false;
371 - }
372 -
373 - logger.debug('Trigger matches all filters', JSON.stringify(parsedFilters.data, null, 2));
374 - // If all filters pass or no filters were provided, return true
375 - return true;
376 - };
377 -
372 logger.debug('🔄 Subscribing to triggers with filters: ', JSON.stringify(filters, null, 2));
373 await this.pusherService.subscribe((_data: Record<string, unknown>) => {
374 logger.debug('Received raw trigger data', JSON.stringify(_data, null, 2));
@@ -382,14 +376,16 @@ export class Triggers {
376 // ideally we should have a type for the trigger data
377 const data = _data as TriggerData;
378 const parsedData = IncomingTriggerPayloadSchema.safeParse({
385 - id: data.metadata.id,
379 + id: data.metadata.nanoId,
380 + uuid: data.metadata.id,
381 triggerSlug: data.metadata.triggerName,
382 toolkitSlug: data.appName,
383 userId: data.metadata.connection?.clientUniqueUserId,
384 payload: data.payload,
385 originalPayload: data.originalPayload,
386 metadata: {
392 - id: data.metadata.id,
387 + id: data.metadata.nanoId,
388 + uuid: data.metadata.id,
389 triggerConfig: data.metadata.triggerConfig,
390 triggerSlug: data.metadata.triggerName,
391 toolkitSlug: data.appName,
@@ -412,7 +408,7 @@ export class Triggers {
408 cause: parsedData.error,
409 });
410 }
415 - if (shouldSendTriggerAfterFilters(parsedData.data)) {
411 + if (this.shouldSendTriggerAfterFilters(parsedFilters.data, parsedData.data)) {
412 try {
413 fn(parsedData.data);
414 } catch (error) {
packages/core/src/types/triggers.types.ts
+3
@@ -94,6 +94,7 @@ export type TriggerInstanceManageDeleteResponse = z.infer<
94
95 export const IncomingTriggerPayloadSchema = z.object({
96 id: z.string().describe('The ID of the trigger'),
97 + uuid: z.string().describe('The UUID of the trigger'),
98 triggerSlug: z.string().describe('The slug of the trigger that triggered the event'),
99 toolkitSlug: z.string().describe('The slug of the toolkit that triggered the event'),
100 userId: z.string().describe('The ID of the user that triggered the event'),
@@ -101,6 +102,7 @@ export const IncomingTriggerPayloadSchema = z.object({
102 originalPayload: z.record(z.unknown()).describe('The original payload of the trigger').optional(),
103 metadata: z.object({
104 id: z.string(),
105 + uuid: z.string(),
106 toolkitSlug: z.string(),
107 triggerSlug: z.string(),
108 triggerData: z.string().optional(),
@@ -125,6 +127,7 @@ export type TriggerData = {
127 originalPayload: Record<string, unknown>;
128 metadata: {
129 id: string;
130 + nanoId: string;
131 triggerName: string;
132 triggerData: string;
133 triggerConfig: Record<string, unknown>;