1
import { Command, Options } from '@effect/cli';
2
+import { FileSystem } from '@effect/platform';
3
import { Deferred, Effect, Option, Runtime } from 'effect';
4
import path from 'node:path';
4
-import process from 'node:process';
5
-import { promises as fs } from 'node:fs';
5
import { requireAuth } from 'src/effects/require-auth';
6
import { TerminalUI } from 'src/services/terminal-ui';
7
import { TriggersRealtime } from 'src/services/triggers-realtime';
133
134
const emitTableLine = (line: string, ui: TerminalUI): Effect.Effect<void> =>
135
Effect.gen(function* () {
137
- if (process.stdout.isTTY) {
138
- yield* Effect.sync(() => {
139
- process.stderr.write(`${line}\n`);
140
- });
141
- return;
142
- }
136
+ yield* ui.log.message(line);
137
yield* ui.output(line);
138
});
139
140
+/**
141
+ * Listen to realtime trigger events for your project.
142
+ *
143
+ * @example
144
+ * ```bash
145
+ * composio triggers listen --toolkits gmail --table
146
+ * composio triggers listen --trigger-slug GMAIL_NEW_GMAIL_MESSAGE --json --max-events 5
147
+ * composio triggers listen --forward "http://localhost:8080/webhook" --out events.jsonl
148
+ * ```
149
+ */
150
export const triggersCmd$Listen = Command.make(
151
'listen',
152
{
177
if (!(yield* requireAuth)) return;
178
179
const ui = yield* TerminalUI;
180
+ const fs = yield* FileSystem.FileSystem;
181
const realtime = yield* TriggersRealtime;
182
const runtime = yield* Effect.runtime<never>();
183
const forwardUrl = Option.getOrUndefined(forward);
188
189
if (outputFilePath) {
190
// Ensure output directory exists before listening.
186
- yield* Effect.tryPromise({
187
- try: async () => {
188
- await fs.mkdir(path.dirname(outputFilePath), { recursive: true });
189
- },
190
- catch: error => new Error(`Failed to prepare output file directory: ${String(error)}`),
191
- }).pipe(
192
- Effect.catchAll(error =>
193
- Effect.gen(function* () {
194
- yield* ui.log.error(String(error));
195
- return yield* Effect.fail(error);
196
- })
197
- )
198
- );
191
+ yield* fs
192
+ .makeDirectory(path.dirname(outputFilePath), { recursive: true })
193
+ .pipe(Effect.tapError(error => ui.log.error(String(error))));
194
}
195
196
const filters: TriggerListenFilters = {
227
if (outputFilePath) {
228
yield* ui.log.info(`Writing matching events to: ${outputFilePath}`);
229
}
235
- if (forwardUrl) {
236
- yield* ui.log.info(`Forwarding matching events to: ${forwardUrl}`);
237
- }
230
231
if (maxEventsLimit !== undefined) {
232
yield* ui.log.info(`Auto-stop after ${maxEventsLimit} matching events.`);
270
const logLine = `${payloadForForwarding}\n`;
271
272
if (outputFilePath) {
281
- yield* Effect.tryPromise({
282
- try: () => fs.appendFile(outputFilePath, logLine, 'utf8'),
283
- catch: error => new Error(`Failed writing to ${outputFilePath}: ${String(error)}`),
284
- }).pipe(Effect.catchAll(error => ui.log.warn(String(error))));
273
+ yield* fs
274
+ .writeFileString(outputFilePath, logLine, { flag: 'a' })
275
+ .pipe(Effect.catchAll(error => ui.log.warn(String(error))));
276
}
277
278
if (forwardUrl && webhookSecret) {
288
- const webhookId =
289
- typeof eventData.id === 'string' && eventData.id.length > 0
290
- ? eventData.id
291
- : randomUUID();
292
- const webhookTimestamp = `${Math.floor(Date.now() / 1000)}`;
293
- const webhookSignature = yield* Effect.tryPromise(() =>
294
- signWebhookPayload({
295
- secret: webhookSecret,
296
- webhookId,
297
- webhookTimestamp,
298
- payload: payloadForForwarding,
299
- })
300
- );
301
- const webhookVersion = detectWebhookPayloadVersion(eventData);
302
-
303
- yield* Effect.tryPromise({
304
- try: async () => {
305
- const response = await fetch(forwardUrl, {
306
- method: 'POST',
307
- headers: {
308
- 'content-type': 'application/json',
309
- 'webhook-id': webhookId,
310
- 'webhook-timestamp': webhookTimestamp,
311
- 'webhook-signature': webhookSignature,
312
- 'x-composio-webhook-version': webhookVersion,
313
- },
314
- body: payloadForForwarding,
315
- });
316
- if (!response.ok) {
317
- throw new Error(`Forwarding failed with HTTP ${response.status}`);
318
- }
319
- },
320
- catch: error =>
321
- new Error(`Failed forwarding event to ${forwardUrl}: ${String(error)}`),
279
+ yield* Effect.gen(function* () {
280
+ const webhookId =
281
+ typeof eventData.id === 'string' && eventData.id.length > 0
282
+ ? eventData.id
283
+ : randomUUID();
284
+ const webhookTimestamp = `${Math.floor(Date.now() / 1000)}`;
285
+ const webhookSignature = yield* Effect.tryPromise({
286
+ try: () =>
287
+ signWebhookPayload({
288
+ secret: webhookSecret,
289
+ webhookId,
290
+ webhookTimestamp,
291
+ payload: payloadForForwarding,
292
+ }),
293
+ catch: error => new Error(`Failed to sign webhook payload: ${String(error)}`),
294
+ });
295
+ const webhookVersion = detectWebhookPayloadVersion(eventData);
296
+
297
+ yield* Effect.tryPromise({
298
+ try: async () => {
299
+ const response = await fetch(forwardUrl, {
300
+ method: 'POST',
301
+ headers: {
302
+ 'content-type': 'application/json',
303
+ 'webhook-id': webhookId,
304
+ 'webhook-timestamp': webhookTimestamp,
305
+ 'webhook-signature': webhookSignature,
306
+ 'x-composio-webhook-version': webhookVersion,
307
+ },
308
+ body: payloadForForwarding,
309
+ });
310
+ if (!response.ok) {
311
+ throw new Error(`Forwarding failed with HTTP ${response.status}`);
312
+ }
313
+ },
314
+ catch: error =>
315
+ new Error(`Failed forwarding event to ${forwardUrl}: ${String(error)}`),
316
+ });
317
}).pipe(Effect.catchAll(error => ui.log.warn(String(error))));
318
}
319