@devries-cameron20 / composio Mirror · read-only / commits / 9c6cf225c
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.

Mirrored from https://github.com/Camo67/composio.git

Synced Nov 21, 07:15 UTC

refactor(cli): move `listen` into the directory shape (#5141)

This PR: - targets `next`, now that https://github.com/ComposioHQ/composio/pull/5136 has landed there - splits `listen.cmd.ts` into `listen/listen.{cmd,handler,format,errors,parse}.ts` and moves its two test files to the mirrored path, unchanged apart from one import - keeps behaviour identical: same flags, output, error class/tag/reasons/messages, telemetry, and the signal handling from the base (a stop after the subscription exits 0 once the temporary trigger is disabled) - regenerates `COMMANDS.md` with `pnpm run commands:map` | Old | New | | --- | --- | | `src/commands/listen.cmd.ts` (flags, description, examples, `Command.make`) | `src/commands/listen/listen.cmd.ts` | | `listen.cmd.ts` (handler, account and trigger resolution, event saving) | `src/commands/listen/listen.handler.ts` (`runListen(params: ListenParams)`) | | `listen.cmd.ts` (`emitStreamLine`, `formatStreamValue`, `formatStopMessage`) | `src/commands/listen/listen.format.ts` | | `listen.cmd.ts` (`ListenCommandError`) | `src/commands/listen/listen.errors.ts` | | `listen.cmd.ts` (`--params`, `--stream` and `--timeout` parsers) | `src/commands/listen/listen.parse.ts` | | `test/src/commands/listen.cmd.test.ts`, `listen.client.cmd.test.ts` | `test/src/commands/listen/` | ## Context Two notes for review: - The handler's body is moved verbatim except for one block: the artifacts directory and `events.jsonl` setup is now `prepareListenEventFiles` in the handler file, with the same calls in the same order. As a standalone function the handler went 9 lines over the `max-lines-per-function` limit of 250, a limit the inline `Command.make` callback sat exactly at. - `listen` still imports `triggers/filter`, `triggers/parse` and `triggers/sink-failures`. These are shared modules of the `triggers` group, not another command's directory, so they stay put; the imports are now absolute (`src/commands/triggers/...`). `dev triggers listen` does not import from `listen.cmd.ts` and is untouched. Verified on the branch tip: root `pnpm typecheck`, `pnpm --filter @composio/cli run lint:effect`, `pnpm --filter @composio/cli test` (validate:boundaries and validate:command-tests included) and `oxlint ts/packages/cli` (no errors) all pass. Tests: 2893 passed and 1 skipped in 222 files, the same as the base; the two `listen` suites run their 26 tests, including the base's ambiguous-account case, at the moved path. The changes the base has made to the old `listen.cmd.ts` since this PR was opened are included in the split files: #5122 (the account comes from `resolveConnectedAccountForToolkit`, so `ListenCommandError` no longer has the `project_context` and `connected_accounts` reasons) and #5127/#5130 (the acting user comes from `actingUser()`). Draft #4804 still edits the old `listen.cmd.ts` and needs to follow the move. --------- Co-authored-by: Vinayak Agarwal <vinayak@composio.dev> Co-authored-by: manobhisriram <manobhisriram@gmail.com>

Alberto Schiabel committed Oct 28, 2026 at 20:09 UTC 9c6cf225ccc799232c6ae340f6f3615c45cbc5dc
10 files changed +810 -747
ts/packages/cli/src/commands/COMMANDS.md
+1 -1
@@ -69,7 +69,7 @@ Every command registration of the CLI and the file that defines it. To find a co
69 | `help` | root | `ts/packages/cli/src/commands/root-help.ts` | | | no | listed |
70 | `install` | root | `ts/packages/cli/src/commands/install/install.cmd.ts` | | | no | listed |
71 | `link` | root | `ts/packages/cli/src/commands/connected-accounts/link/connected-accounts.link.cmd.ts` | | `dev connected-accounts link` | yes | listed |
72 -| `listen` | root | `ts/packages/cli/src/commands/listen.cmd.ts` | | | yes | experimental |
72 +| `listen` | root | `ts/packages/cli/src/commands/listen/listen.cmd.ts` | | | yes | experimental |
73 | `login` | root | `ts/packages/cli/src/commands/login/login.cmd.ts` | | | no | listed |
74 | `logout` | root | `ts/packages/cli/src/commands/logout.cmd.ts` | | | no | listed |
75 | `orgs` | root | `ts/packages/cli/src/commands/orgs/orgs.cmd.ts` | | | yes | listed |
ts/packages/cli/src/commands/index.ts
+1 -1
@@ -14,7 +14,7 @@ import { whoamiCmd } from './whoami.cmd';
14 import { loginCmd } from './login/login.cmd';
15 import { signupCmd } from './signup/signup.cmd';
16 import { setupCmd } from './setup.cmd';
17 -import { listenCmd } from './listen.cmd';
17 +import { listenCmd } from './listen/listen.cmd';
18 import { logoutCmd } from './logout.cmd';
19 import { runCmd } from './run/run.cmd';
20 import { proxyCmd } from './proxy/proxy.cmd';
ts/packages/cli/src/commands/listen.cmd.ts deleted
-744
@@ -1,744 +0,0 @@
1 -import { withCommandMeta } from 'src/invocation/command-identity';
2 -import { reportFailure } from 'src/effects/report-failure';
3 -import { eventCount, finishAcceptedEvents, makeSinkFailures } from './triggers/sink-failures';
4 -import { commandInputError } from 'src/commands/errors';
5 -import { Argument, Command, Flag } from 'effect/cli';
6 -import * as FileSystem from 'effect/FileSystem';
7 -import * as Path from 'effect/Path';
8 -import type { ComposioClientShape } from '@composio/client/effect';
9 -import { Data, Deferred, Effect, FiberSet, Result, Option, Predicate } from 'effect';
10 -import { resolveOptionalTextInput } from 'src/effects/resolve-optional-text-input';
11 -import { ComposioClientSingleton, type ToolkitProjectScope } from 'src/services/composio-clients';
12 -import { actingUser } from 'src/commands/surface';
13 -import {
14 - resolveArtifactsRoot,
15 - resolveCliSessionArtifacts,
16 -} from 'src/services/cli-session-artifacts';
17 -import { TerminalUI } from 'src/services/terminal-ui';
18 -import { TriggersRealtime } from 'src/services/triggers-realtime';
19 -import { LISTENER_STOP, ShutdownSignal } from 'src/services/shutdown-signal';
20 -import { resolveConnectedAccountForToolkit } from 'src/services/connected-account-selection';
21 -import { parseJsonRecord } from 'src/utils/parse-json';
22 -import { toolkitFromToolSlug } from 'src/effects/toolkit-from-tool-slug';
23 -import { matchesTriggerListenFilters } from './triggers/filter';
24 -import { parseTriggerListenEvent } from './triggers/parse';
25 -import { idempotent } from 'src/services/composio-request';
26 -
27 -type TriggerCreateParams = NonNullable<
28 - Parameters<ComposioClientShape['triggerInstances']['upsert']>[1]
29 ->;
30 -
31 -export class ListenCommandError extends Data.TaggedError('commands/ListenCommandError')<{
32 - readonly reason:
33 - 'connected_account_not_found' | 'unknown_trigger' | 'create_trigger' | 'disable_trigger';
34 - readonly message: string;
35 - readonly slug?: string;
36 - readonly toolkitSlug?: string;
37 - readonly cause?: unknown;
38 -}> {}
39 -
40 -const invalidOptionValue = commandInputError('listen');
41 -
42 -const errorMessage = (error: unknown): string =>
43 - Predicate.isError(error) ? error.message : String(error);
44 -
45 -const slug = Argument.String('slug').pipe(
46 - Argument.withDescription(
47 - 'Trigger slug (e.g. "GMAIL_NEW_GMAIL_MESSAGE") or project event type (e.g. "composio.connected_account.expired")'
48 - )
49 -);
50 -
51 -const params = Flag.String('params').pipe(
52 - Flag.withAlias('p'),
53 - Flag.withDescription(
54 - 'Trigger create params as JSON/JS object, @file, or - for stdin. Only valid for trigger slugs.'
55 - ),
56 - Flag.optional
57 -);
58 -
59 -const maxEvents = Flag.Int('max-events').pipe(
60 - Flag.withDescription('Stop after receiving N matching events'),
61 - Flag.optional
62 -);
63 -
64 -const timeout = Flag.String('timeout').pipe(
65 - Flag.withDescription('Stop after a duration such as "5m", "1hr", or "30s"'),
66 - Flag.optional
67 -);
68 -
69 -const stream = Flag.String('stream').pipe(
70 - Flag.withDescription(
71 - 'Also stream each event payload inline. Pass an optional jq-like path such as ".thread.id" or ".data[0].id".'
72 - ),
73 - Flag.optional
74 -);
75 -const account = Flag.String('account').pipe(
76 - Flag.withDescription(
77 - 'Connected account selector. Matches alias, word_id, or connected account id for the inferred toolkit.'
78 - ),
79 - Flag.optional
80 -);
81 -
82 -const failFast = Flag.Boolean('fail-fast').pipe(
83 - Flag.withDescription('Stop at the first event that fails to save, and exit 1'),
84 - Flag.withDefault(false)
85 -);
86 -
87 -const debug = Flag.Boolean('debug').pipe(
88 - Flag.withDescription(
89 - 'Print verbose debug information (raw events, filter results, Pusher state)'
90 - ),
91 - Flag.withDefault(false)
92 -);
93 -
94 -const sanitizePathPart = (value: string): string =>
95 - value.replace(/[^A-Z0-9._-]+/gi, '-').replace(/^-+|-+$/g, '') || 'unknown';
96 -
97 -const disableTemporaryTrigger = (params: {
98 - client: ComposioClientShape;
99 - ui: TerminalUI;
100 - slug: string;
101 - triggerId: string;
102 -}) =>
103 - params.client.triggerInstances.manage.update(params.triggerId, { status: 'disable' }).pipe(
104 - Effect.mapError(
105 - cause =>
106 - new ListenCommandError({
107 - reason: 'disable_trigger',
108 - // "Error: " is how these messages have always introduced the client's failure.
109 - message: `Failed to disable temporary trigger "${params.triggerId}": Error: ${cause.message}`,
110 - slug: params.slug,
111 - cause,
112 - })
113 - ),
114 - Effect.tapError(error => params.ui.log.warn(error.message)),
115 - Effect.ignore
116 - );
117 -
118 -const isProjectEventType = (value: string): boolean => value.startsWith('composio.');
119 -
120 -const resolveParamsInput = (input: Option.Option<string>) =>
121 - resolveOptionalTextInput(input, { missingValue: '{}' });
122 -
123 -const parseCreateParams = (raw: string) =>
124 - parseJsonRecord(raw).pipe(
125 - Result.mapError(error =>
126 - invalidOptionValue(
127 - error.reason === 'not-a-record'
128 - ? "Expected --params to be an object, e.g. -p '{ trigger_config: { ... } }'."
129 - : "Invalid --params input. Provide JSON or a JS-style object literal, e.g. -p '{ trigger_config: { ... } }'.",
130 - '--params'
131 - )
132 - )
133 - );
134 -
135 -const assertSupportedListenParams = (params: {
136 - listeningToProjectEvent: boolean;
137 - slug: string;
138 - createParamsInput: Record<string, unknown>;
139 -}) =>
140 - params.listeningToProjectEvent && Object.keys(params.createParamsInput).length > 0
141 - ? Effect.fail(
142 - invalidOptionValue(
143 - `--params is only supported for trigger slugs. "${params.slug}" is a project-level composio.* event type and does not create a temporary trigger.`,
144 - '--params'
145 - )
146 - )
147 - : Effect.void;
148 -
149 -/**
150 - * Fails with an `unknown_trigger` error when `slug` is not a known trigger type.
151 - *
152 - * Called when the toolkit has no active connected account, and when creating the temporary trigger
153 - * fails, so a mistyped slug is reported as such instead of as a missing connection or a generic
154 - * creation failure. Neither call site is on the happy path, which makes no additional request.
155 - */
156 -const assertTriggerTypeExists = (params: {
157 - client: ComposioClientShape;
158 - slug: string;
159 - toolkitSlug?: string;
160 -}) =>
161 - params.client.triggersTypes.retrieve(params.slug, undefined, idempotent).pipe(
162 - Effect.catchTag(
163 - '@composio/client/NotFoundError',
164 - cause =>
165 - new ListenCommandError({
166 - reason: 'unknown_trigger',
167 - message: `Unknown trigger slug "${params.slug}". List available slugs with \`composio triggers list <toolkit>\`.`,
168 - slug: params.slug,
169 - toolkitSlug: params.toolkitSlug,
170 - cause,
171 - }),
172 - // A found trigger type, or any failure other than 404, is inconclusive here: fall through to
173 - // the caller's own error.
174 - () => Effect.void
175 - ),
176 - Effect.asVoid
177 - );
178 -
179 -const resolveConnectedAccountIdForTrigger = (params: {
180 - client: ComposioClientShape;
181 - slug: string;
182 - consumerUserId: string;
183 - account: Option.Option<string>;
184 - projectScope: ToolkitProjectScope;
185 -}) =>
186 - Effect.gen(function* () {
187 - const toolkitSlug = yield* toolkitFromToolSlug(params.slug, params.projectScope);
188 - if (!toolkitSlug) {
189 - return yield* Effect.fail(
190 - invalidOptionValue(
191 - `Could not infer a toolkit from trigger slug "${params.slug}". Use a standard trigger slug such as "GMAIL_NEW_GMAIL_MESSAGE", or a project event type such as "composio.connected_account.expired".`,
192 - '<slug>'
193 - )
194 - );
195 - }
196 -
197 - // The resolver `composio execute` and `composio proxy` use, so all three pick the same
198 - // account for the same `--account` (or its absence) and fail the same way when they cannot.
199 - const connectedAccountId = yield* resolveConnectedAccountForToolkit({
200 - client: params.client,
201 - toolkitSlug,
202 - userId: params.consumerUserId,
203 - selector: params.account,
204 - });
205 - if (connectedAccountId) {
206 - return connectedAccountId;
207 - }
208 -
209 - yield* assertTriggerTypeExists({ client: params.client, slug: params.slug, toolkitSlug });
210 -
211 - return yield* new ListenCommandError({
212 - reason: 'connected_account_not_found',
213 - message: `No active connected account found for toolkit "${toolkitSlug}" and consumer user "${params.consumerUserId}". Run \`composio link ${toolkitSlug}\` first.`,
214 - slug: params.slug,
215 - toolkitSlug,
216 - });
217 - });
218 -const emitStreamLine = (line: string, ui: TerminalUI) =>
219 - Effect.gen(function* () {
220 - yield* ui.log.message(line);
221 - yield* ui.output(line, { force: true });
222 - });
223 -
224 -const eventTypeOf = (eventData: Record<string, unknown>): string | undefined =>
225 - typeof eventData.type === 'string' && eventData.type.length > 0 ? eventData.type : undefined;
226 -
227 -const extractEventFileId = (eventData: Record<string, unknown>): string => {
228 - const metadata = Predicate.isObject(eventData.metadata) ? eventData.metadata : undefined;
229 - const candidates = [eventData.id, eventData.log_id, metadata?.id];
230 -
231 - for (const candidate of candidates) {
232 - if (typeof candidate === 'string' && candidate.length > 0) {
233 - return sanitizePathPart(candidate);
234 - }
235 - }
236 -
237 - return `${Date.now()}-${crypto.randomUUID().slice(0, 8)}`;
238 -};
239 -
240 -const parseStreamPath = (expression: string) =>
241 - Effect.gen(function* () {
242 - const trimmed = expression.trim();
243 - const pathTokens: Array<string | number> = [];
244 - // An empty --stream streams the whole event.
245 - if (trimmed.length === 0) return pathTokens;
246 - if (!trimmed.startsWith('.')) {
247 - return yield* Effect.fail(
248 - invalidOptionValue(
249 - 'Expected --stream to contain a jq-like path starting with "."',
250 - '--stream'
251 - )
252 - );
253 - }
254 -
255 - const tokenPattern = /(?:\.([A-Za-z0-9_-]+))|(?:\[(\d+)\])/g;
256 - let lastIndex = 0;
257 -
258 - for (const match of trimmed.matchAll(tokenPattern)) {
259 - if ((match.index ?? -1) !== lastIndex) {
260 - return yield* Effect.fail(
261 - invalidOptionValue(
262 - 'Unsupported --stream expression. Use a jq-like path such as ".foo.bar" or ".items[0].id".',
263 - '--stream'
264 - )
265 - );
266 - }
267 -
268 - if (match[1]) pathTokens.push(match[1]);
269 - if (match[2]) pathTokens.push(Number(match[2]));
270 - lastIndex += match[0].length;
271 - }
272 -
273 - if (lastIndex !== trimmed.length) {
274 - return yield* Effect.fail(
275 - invalidOptionValue(
276 - 'Unsupported --stream expression. Use a jq-like path such as ".foo.bar" or ".items[0].id".',
277 - '--stream'
278 - )
279 - );
280 - }
281 -
282 - return pathTokens;
283 - });
284 -
285 -const applyStreamPath = (value: unknown, pathTokens: ReadonlyArray<string | number>): unknown => {
286 - let current = value;
287 - for (const token of pathTokens) {
288 - if (typeof token === 'number') {
289 - if (!Array.isArray(current) || token < 0 || token >= current.length) {
290 - return undefined;
291 - }
292 - current = current[token];
293 - continue;
294 - }
295 -
296 - if (!Predicate.isObject(current)) {
297 - return undefined;
298 - }
299 -
300 - current = current[token];
301 - }
302 -
303 - return current;
304 -};
305 -
306 -const formatStreamValue = (value: unknown): string => {
307 - if (value === undefined) return 'null';
308 - if (typeof value === 'string') return value;
309 - return JSON.stringify(value);
310 -};
311 -
312 -/**
313 - * Saves one matched event: its payload file, its line in the stream file, then the `event:` line
314 - * and, when streaming (`streamPath` set), the `stream:` line.
315 - */
316 -const saveListenEvent = (params: {
317 - readonly fs: FileSystem.FileSystem;
318 - readonly ui: TerminalUI;
319 - readonly eventData: Record<string, unknown>;
320 - readonly eventFilePath: string;
321 - readonly streamFilePath: string;
322 - readonly streamEntry: string;
323 - readonly streamPath: ReadonlyArray<string | number> | undefined;
324 -}) =>
325 - Effect.gen(function* () {
326 - const { fs, ui, eventFilePath, streamPath } = params;
327 - yield* fs.writeFileString(eventFilePath, `${JSON.stringify(params.eventData, null, 2)}\n`);
328 - yield* fs.writeFileString(params.streamFilePath, `${params.streamEntry}\n`, { flag: 'a' });
329 - yield* emitStreamLine(`event: ${eventFilePath}`, ui);
330 - if (streamPath !== undefined) {
331 - const streamValue = applyStreamPath(params.eventData, streamPath);
332 - yield* emitStreamLine(`stream: ${formatStreamValue(streamValue)}`, ui);
333 - }
334 - });
335 -
336 -const formatStopMessage = (params: {
337 - matchingEvents: number;
338 - timedOut: boolean;
339 - temporaryTriggerDisabled: boolean;
340 -}): string => {
341 - const eventLabel = `event${params.matchingEvents === 1 ? '' : 's'}`;
342 - if (params.timedOut) {
343 - return params.temporaryTriggerDisabled
344 - ? `Stopped after timeout with ${params.matchingEvents} matching ${eventLabel}. Temporary trigger disabled.`
345 - : `Stopped after timeout with ${params.matchingEvents} matching ${eventLabel}.`;
346 - }
347 -
348 - return params.temporaryTriggerDisabled
349 - ? `Stopped after receiving ${params.matchingEvents} ${eventLabel}. Temporary trigger disabled.`
350 - : `Stopped after receiving ${params.matchingEvents} ${eventLabel}.`;
351 -};
352 -
353 -const TIMEOUT_UNITS_MS: Record<string, number> = {
354 - ms: 1,
355 - millisecond: 1,
356 - milliseconds: 1,
357 - s: 1_000,
358 - sec: 1_000,
359 - secs: 1_000,
360 - second: 1_000,
361 - seconds: 1_000,
362 - m: 60_000,
363 - min: 60_000,
364 - mins: 60_000,
365 - minute: 60_000,
366 - minutes: 60_000,
367 - h: 3_600_000,
368 - hr: 3_600_000,
369 - hrs: 3_600_000,
370 - hour: 3_600_000,
371 - hours: 3_600_000,
372 - d: 86_400_000,
373 - day: 86_400_000,
374 - days: 86_400_000,
375 -};
376 -
377 -const parseTimeoutMs = (value: string) =>
378 - Effect.gen(function* () {
379 - const trimmed = value.trim().toLowerCase();
380 - const match = /^(\d+(?:\.\d+)?)\s*([a-z]+)$/.exec(trimmed);
381 - const amount = Number(match?.[1]);
382 - const unit = match?.[2];
383 - const unitMs = unit === undefined ? undefined : TIMEOUT_UNITS_MS[unit];
384 - if (!match || !Number.isFinite(amount) || amount <= 0 || unitMs === undefined) {
385 - return yield* Effect.fail(
386 - invalidOptionValue(
387 - 'Invalid --timeout value. Use a duration such as "30s", "5m", "1hr", or "1day".',
388 - '--timeout'
389 - )
390 - );
391 - }
392 -
393 - return Math.round(amount * unitMs);
394 - });
395 -
396 -const resolveListenSetup = (params: {
397 - readonly slug: string;
398 - readonly inputParams: Option.Option<string>;
399 - readonly timeout: Option.Option<string>;
400 - readonly stream: Option.Option<string>;
401 - readonly maxEvents: Option.Option<number>;
402 - readonly account: Option.Option<string>;
403 - readonly client: ComposioClientShape;
404 - readonly resolvedProject: {
405 - readonly orgId: string;
406 - readonly projectId: string;
407 - };
408 - readonly consumerUserId: string;
409 -}) =>
410 - Effect.gen(function* () {
411 - const consumerUserId = params.consumerUserId;
412 - const listeningToProjectEvent = isProjectEventType(params.slug);
413 - const rawParams = Option.isSome(params.inputParams)
414 - ? (yield* resolveParamsInput(params.inputParams))?.trim() || '{}'
415 - : '{}';
416 - const createParamsInput = yield* Effect.fromResult(parseCreateParams(rawParams));
417 - yield* assertSupportedListenParams({
418 - listeningToProjectEvent,
419 - slug: params.slug,
420 - createParamsInput,
421 - });
422 -
423 - const resolvedConnectedAccountId = listeningToProjectEvent
424 - ? undefined
425 - : yield* resolveConnectedAccountIdForTrigger({
426 - client: params.client,
427 - slug: params.slug,
428 - consumerUserId,
429 - account: params.account,
430 - projectScope: {
431 - orgId: params.resolvedProject.orgId,
432 - projectId: params.resolvedProject.projectId,
433 - },
434 - });
435 -
436 - const createParams: TriggerCreateParams | undefined = listeningToProjectEvent
437 - ? undefined
438 - : {
439 - ...createParamsInput,
440 - connected_account_id: resolvedConnectedAccountId,
441 - };
442 -
443 - const timeoutMs = Option.isSome(params.timeout)
444 - ? yield* parseTimeoutMs(params.timeout.value)
445 - : undefined;
446 - const streamPath = Option.isSome(params.stream)
447 - ? yield* parseStreamPath(params.stream.value)
448 - : undefined;
449 -
450 - return {
451 - listeningToProjectEvent,
452 - createParams,
453 - timeoutMs,
454 - streamPath,
455 - shouldStream: Option.isSome(params.stream),
456 - maxEventsLimit: Option.getOrUndefined(params.maxEvents),
457 - consumerUserId,
458 - };
459 - });
460 -
461 -export const listenCmd = Command.make(
462 - 'listen',
463 - { slug, params, maxEvents, timeout, stream, account, failFast, debug },
464 - ({ slug, params, maxEvents, timeout, stream, account, failFast, debug }) =>
465 - Effect.gen(function* () {
466 - const ui = yield* TerminalUI;
467 - const fs = yield* FileSystem.FileSystem;
468 - const path = yield* Path.Path;
469 - const clientSingleton = yield* ComposioClientSingleton;
470 - const realtime = yield* TriggersRealtime;
471 - const shutdown = yield* ShutdownSignal;
472 -
473 - const { userId: actingUserId, project: resolvedProject } = yield* actingUser();
474 -
475 - const client = yield* clientSingleton.getFor({
476 - orgId: resolvedProject.orgId,
477 - projectId: resolvedProject.projectId,
478 - });
479 - const {
480 - listeningToProjectEvent,
481 - createParams,
482 - timeoutMs,
483 - streamPath,
484 - shouldStream,
485 - maxEventsLimit,
486 - consumerUserId,
487 - } = yield* resolveListenSetup({
488 - slug,
489 - inputParams: params,
490 - timeout,
491 - stream,
492 - maxEvents,
493 - account,
494 - client,
495 - resolvedProject,
496 - consumerUserId: actingUserId,
497 - });
498 -
499 - const artifactsOption = yield* resolveCliSessionArtifacts({
500 - orgId: resolvedProject.orgId,
501 - consumerUserId,
502 - });
503 - const artifactsRoot = Option.isSome(artifactsOption)
504 - ? artifactsOption.value.directoryPath
505 - : yield* resolveArtifactsRoot;
506 - const triggerDir = path.join(
507 - artifactsRoot,
508 - listeningToProjectEvent ? 'events' : 'triggers',
509 - sanitizePathPart(slug)
510 - );
511 - const streamFilePath = path.join(triggerDir, 'events.jsonl');
512 -
513 - yield* fs.makeDirectory(triggerDir, { recursive: true });
514 - yield* fs.writeFileString(streamFilePath, '', { flag: 'a' });
515 -
516 - const stopWhenDone = yield* Deferred.make<'max-events' | 'timeout' | 'failed'>();
517 - // Its own signal, so a --fail-fast failure also ends finishAcceptedEvents' wait.
518 - const failed = yield* Deferred.make<void>();
519 - let matchingEvents = 0;
520 - const writeFailures = makeSinkFailures({
521 - ui,
522 - failFast,
523 - stop: Effect.andThen(
524 - Deferred.succeed(failed, undefined),
525 - Deferred.succeed(stopWhenDone, 'failed')
526 - ).pipe(Effect.asVoid),
527 - summary: count => `${eventCount(count)} failed to write while listening.`,
528 - });
529 - const recordWriteFailure = (error: unknown) => writeFailures.record(errorMessage(error));
530 - const seenEventIds = new Set<string>();
531 -
532 - yield* Effect.acquireUseRelease(
533 - listeningToProjectEvent
534 - ? Effect.succeed<null | { trigger_id: string }>(null)
535 - : client.triggerInstances.upsert(slug, createParams).pipe(
536 - Effect.mapError(
537 - cause =>
538 - new ListenCommandError({
539 - reason: 'create_trigger',
540 - message: `Failed to create temporary trigger "${slug}": Error: ${cause.message}`,
541 - slug,
542 - cause,
543 - })
544 - ),
545 - // An active account for the inferred toolkit skips the lookup in
546 - // resolveConnectedAccountIdForTrigger, so a mistyped slug with a valid toolkit prefix
547 - // only surfaces here. The lookup's unknown_trigger failure replaces the create_trigger
548 - // error; any other outcome re-fails with the original error.
549 - Effect.catch(error =>
550 - assertTriggerTypeExists({ client, slug }).pipe(Effect.andThen(Effect.fail(error)))
551 - )
552 - ),
553 - createdTrigger =>
554 - Effect.gen(function* () {
555 - // Scoped below: a stop other than --max-events or --timeout interrupts the event
556 - // callbacks still running before the release hook sums up.
557 - const events = yield* FiberSet.make();
558 - const runEvent = yield* FiberSet.runtime(events)();
559 - yield* emitStreamLine(`listening for events ${slug} (tail at ${streamFilePath})`, ui);
560 - if (debug) {
561 - const debugMsg =
562 - createdTrigger === null
563 - ? `[debug] event_type=${slug} project=${resolvedProject.projectId} org=${resolvedProject.orgId}`
564 - : `[debug] trigger_id=${createdTrigger.trigger_id} project=${resolvedProject.projectId} org=${resolvedProject.orgId} createParams=${JSON.stringify(createParams)}`;
565 - yield* emitStreamLine(debugMsg, ui);
566 - if (createdTrigger !== null) {
567 - yield* emitStreamLine(
568 - `[debug] upsert response: ${JSON.stringify(createdTrigger)}`,
569 - ui
570 - );
571 - }
572 - }
573 -
574 - const onEvent = (eventData: Record<string, unknown>) => {
575 - runEvent(
576 - Effect.gen(function* () {
577 - if (debug) {
578 - yield* emitStreamLine(
579 - `[debug] raw event keys: ${Object.keys(eventData).join(', ')}`,
580 - ui
581 - );
582 - yield* emitStreamLine(
583 - `[debug] raw event (truncated): ${JSON.stringify(eventData).slice(0, 500)}`,
584 - ui
585 - );
586 - }
587 - const parsedTriggerEvent =
588 - createdTrigger === null ? undefined : parseTriggerListenEvent(eventData);
589 - const filterResult =
590 - createdTrigger === null
591 - ? eventTypeOf(eventData) === slug
592 - : parsedTriggerEvent !== undefined &&
593 - matchesTriggerListenFilters(
594 - { triggerId: createdTrigger.trigger_id },
595 - parsedTriggerEvent
596 - );
597 - if (debug) {
598 - yield* emitStreamLine(
599 - createdTrigger === null
600 - ? `[debug] event.type=${eventTypeOf(eventData) ?? '<missing>'} match=${filterResult}`
601 - : `[debug] parsed.id=${parsedTriggerEvent?.id ?? '<missing>'} triggerSlug=${parsedTriggerEvent?.triggerSlug ?? '<missing>'} trigger_id=${createdTrigger.trigger_id} match=${filterResult}`,
602 - ui
603 - );
604 - }
605 - if (!filterResult) {
606 - return;
607 - }
608 -
609 - const eventFileId = extractEventFileId(eventData);
610 - if (seenEventIds.has(eventFileId)) {
611 - if (debug) {
612 - yield* emitStreamLine(`[debug] skipping duplicate event ${eventFileId}`, ui);
613 - }
614 - return;
615 - }
616 - seenEventIds.add(eventFileId);
617 -
618 - matchingEvents += 1;
619 - const eventFilePath = path.join(triggerDir, `${eventFileId}-payload.json`);
620 - const streamEntry = JSON.stringify({
621 - event_id: eventFileId,
622 - event_type: eventTypeOf(eventData),
623 - trigger_id: createdTrigger?.trigger_id,
624 - trigger_slug: createdTrigger ? slug : undefined,
625 - file_path: eventFilePath,
626 - received_at: new Date().toISOString(),
627 - });
628 -
629 - // A matched event counts toward --max-events even when saving it fails.
630 - yield* saveListenEvent({
631 - fs,
632 - ui,
633 - eventData,
634 - eventFilePath,
635 - streamFilePath,
636 - streamEntry,
637 - streamPath: shouldStream ? (streamPath ?? []) : undefined,
638 - }).pipe(Effect.catch(recordWriteFailure));
639 -
640 - if (maxEventsLimit !== undefined && matchingEvents >= maxEventsLimit) {
641 - yield* Deferred.succeed(stopWhenDone, 'max-events').pipe(Effect.ignore);
642 - }
643 - }).pipe(Effect.catch(recordWriteFailure))
644 - );
645 - };
646 -
647 - const listenEffect = realtime
648 - .listenInProject(
649 - { orgId: resolvedProject.orgId, projectId: resolvedProject.projectId },
650 - onEvent,
651 - { onSubscribed: shutdown.cooperate(LISTENER_STOP) }
652 - )
653 - .pipe(Effect.onInterrupt(() => ui.log.info(`Stopped listening for events ${slug}.`)));
654 -
655 - if (timeoutMs !== undefined) {
656 - yield* Effect.forkScoped(
657 - Effect.sleep(timeoutMs).pipe(
658 - Effect.andThen(Deferred.succeed(stopWhenDone, 'timeout')),
659 - Effect.ignore
660 - )
661 - );
662 - }
663 -
664 - // Ctrl-C or SIGTERM after the subscription is a clean stop: exit 0 after the release.
665 - const stopReason = yield* Effect.raceAllFirst([
666 - listenEffect,
667 - Deferred.await(stopWhenDone),
668 - Effect.as(shutdown.requested, 'signal' as const),
669 - ]);
670 - if (stopReason === 'max-events' || stopReason === 'timeout') {
671 - yield* finishAcceptedEvents(events, failed);
672 - }
673 - if (stopReason === 'max-events') {
674 - yield* ui.outro(
675 - formatStopMessage({
676 - matchingEvents,
677 - timedOut: false,
678 - temporaryTriggerDisabled: createdTrigger !== null,
679 - })
680 - );
681 - return;
682 - }
683 -
684 - if (stopReason === 'timeout') {
685 - yield* ui.outro(
686 - formatStopMessage({
687 - matchingEvents,
688 - timedOut: true,
689 - temporaryTriggerDisabled: createdTrigger !== null,
690 - })
691 - );
692 - }
693 - }).pipe(Effect.scoped),
694 - created =>
695 - Effect.gen(function* () {
696 - if (created !== null) {
697 - yield* disableTemporaryTrigger({ client, ui, slug, triggerId: created.trigger_id });
698 - }
699 - // Runs on every stop of the use phase — max-events, timeout, --fail-fast, Ctrl+C or
700 - // SIGTERM, and an interruption — so a piped caller always gets the count.
701 - yield* writeFailures.report;
702 - })
703 - ).pipe(
704 - Effect.catchTag('services/TriggerRealtimeSubscriptionError', error =>
705 - reportFailure(
706 - ui,
707 - `Could not subscribe to realtime trigger events: ${error.message}. Check your network connection or run \`composio login\`.`
708 - )
709 - )
710 - );
711 - })
712 -).pipe(
713 - Command.withDescription(
714 - 'Listen to consumer-project realtime events. Trigger slugs create a temporary trigger; top-level composio.* event types subscribe directly.'
715 - ),
716 - Command.withExamples([
717 - {
718 - command: 'composio listen GMAIL_NEW_GMAIL_MESSAGE',
719 - },
720 - {
721 - command: 'composio listen GMAIL_NEW_GMAIL_MESSAGE -p @trigger.json --max-events 5',
722 - },
723 - {
724 - command: 'composio listen GMAIL_NEW_GMAIL_MESSAGE --account work --timeout 5m',
725 - },
726 - {
727 - command: 'composio listen GMAIL_NEW_GMAIL_MESSAGE --max-events 5 --fail-fast',
728 - },
729 - {
730 - command: "composio listen GMAIL_NEW_GMAIL_MESSAGE --timeout 1hr --stream '.data.threadId'",
731 - },
732 - {
733 - command:
734 - 'composio listen SLACK_CHANNEL_MESSAGE_RECEIVED -p \'{ trigger_config: { channel_id: "C123" } }\'',
735 - },
736 - {
737 - command: 'composio listen GMAIL_NEW_GMAIL_MESSAGE -p @trigger.json --stream',
738 - },
739 - {
740 - command: "composio listen GMAIL_NEW_GMAIL_MESSAGE -p @trigger.json --stream '.data.threadId'",
741 - },
742 - ]),
743 - withCommandMeta({ requiresLogin: true })
744 -);
ts/packages/cli/src/commands/listen/listen.cmd.ts new
+90
@@ -0,0 +1,90 @@
1 +import { Argument, Command, Flag } from 'effect/cli';
2 +import { withCommandMeta } from 'src/invocation/command-identity';
3 +import { runListen } from './listen.handler';
4 +
5 +const slug = Argument.String('slug').pipe(
6 + Argument.withDescription(
7 + 'Trigger slug (e.g. "GMAIL_NEW_GMAIL_MESSAGE") or project event type (e.g. "composio.connected_account.expired")'
8 + )
9 +);
10 +
11 +const params = Flag.String('params').pipe(
12 + Flag.withAlias('p'),
13 + Flag.withDescription(
14 + 'Trigger create params as JSON/JS object, @file, or - for stdin. Only valid for trigger slugs.'
15 + ),
16 + Flag.optional
17 +);
18 +
19 +const maxEvents = Flag.Int('max-events').pipe(
20 + Flag.withDescription('Stop after receiving N matching events'),
21 + Flag.optional
22 +);
23 +
24 +const timeout = Flag.String('timeout').pipe(
25 + Flag.withDescription('Stop after a duration such as "5m", "1hr", or "30s"'),
26 + Flag.optional
27 +);
28 +
29 +const stream = Flag.String('stream').pipe(
30 + Flag.withDescription(
31 + 'Also stream each event payload inline. Pass an optional jq-like path such as ".thread.id" or ".data[0].id".'
32 + ),
33 + Flag.optional
34 +);
35 +const account = Flag.String('account').pipe(
36 + Flag.withDescription(
37 + 'Connected account selector. Matches alias, word_id, or connected account id for the inferred toolkit.'
38 + ),
39 + Flag.optional
40 +);
41 +
42 +const failFast = Flag.Boolean('fail-fast').pipe(
43 + Flag.withDescription('Stop at the first event that fails to save, and exit 1'),
44 + Flag.withDefault(false)
45 +);
46 +
47 +const debug = Flag.Boolean('debug').pipe(
48 + Flag.withDescription(
49 + 'Print verbose debug information (raw events, filter results, Pusher state)'
50 + ),
51 + Flag.withDefault(false)
52 +);
53 +
54 +export const listenCmd = Command.make(
55 + 'listen',
56 + { slug, params, maxEvents, timeout, stream, account, failFast, debug },
57 + runListen
58 +).pipe(
59 + Command.withDescription(
60 + 'Listen to consumer-project realtime events. Trigger slugs create a temporary trigger; top-level composio.* event types subscribe directly.'
61 + ),
62 + Command.withExamples([
63 + {
64 + command: 'composio listen GMAIL_NEW_GMAIL_MESSAGE',
65 + },
66 + {
67 + command: 'composio listen GMAIL_NEW_GMAIL_MESSAGE -p @trigger.json --max-events 5',
68 + },
69 + {
70 + command: 'composio listen GMAIL_NEW_GMAIL_MESSAGE --account work --timeout 5m',
71 + },
72 + {
73 + command: 'composio listen GMAIL_NEW_GMAIL_MESSAGE --max-events 5 --fail-fast',
74 + },
75 + {
76 + command: "composio listen GMAIL_NEW_GMAIL_MESSAGE --timeout 1hr --stream '.data.threadId'",
77 + },
78 + {
79 + command:
80 + 'composio listen SLACK_CHANNEL_MESSAGE_RECEIVED -p \'{ trigger_config: { channel_id: "C123" } }\'',
81 + },
82 + {
83 + command: 'composio listen GMAIL_NEW_GMAIL_MESSAGE -p @trigger.json --stream',
84 + },
85 + {
86 + command: "composio listen GMAIL_NEW_GMAIL_MESSAGE -p @trigger.json --stream '.data.threadId'",
87 + },
88 + ]),
89 + withCommandMeta({ requiresLogin: true })
90 +);
ts/packages/cli/src/commands/listen/listen.errors.ts new
+10
@@ -0,0 +1,10 @@
1 +import { Data } from 'effect';
2 +
3 +export class ListenCommandError extends Data.TaggedError('commands/ListenCommandError')<{
4 + readonly reason:
5 + 'connected_account_not_found' | 'unknown_trigger' | 'create_trigger' | 'disable_trigger';
6 + readonly message: string;
7 + readonly slug?: string;
8 + readonly toolkitSlug?: string;
9 + readonly cause?: unknown;
10 +}> {}
ts/packages/cli/src/commands/listen/listen.format.ts new
+31
@@ -0,0 +1,31 @@
1 +import { Effect } from 'effect';
2 +import type { TerminalUI } from 'src/services/terminal-ui';
3 +
4 +export const emitStreamLine = (line: string, ui: TerminalUI) =>
5 + Effect.gen(function* () {
6 + yield* ui.log.message(line);
7 + yield* ui.output(line, { force: true });
8 + });
9 +
10 +export const formatStreamValue = (value: unknown): string => {
11 + if (value === undefined) return 'null';
12 + if (typeof value === 'string') return value;
13 + return JSON.stringify(value);
14 +};
15 +
16 +export const formatStopMessage = (params: {
17 + matchingEvents: number;
18 + timedOut: boolean;
19 + temporaryTriggerDisabled: boolean;
20 +}): string => {
21 + const eventLabel = `event${params.matchingEvents === 1 ? '' : 's'}`;
22 + if (params.timedOut) {
23 + return params.temporaryTriggerDisabled
24 + ? `Stopped after timeout with ${params.matchingEvents} matching ${eventLabel}. Temporary trigger disabled.`
25 + : `Stopped after timeout with ${params.matchingEvents} matching ${eventLabel}.`;
26 + }
27 +
28 + return params.temporaryTriggerDisabled
29 + ? `Stopped after receiving ${params.matchingEvents} ${eventLabel}. Temporary trigger disabled.`
30 + : `Stopped after receiving ${params.matchingEvents} ${eventLabel}.`;
31 +};
ts/packages/cli/src/commands/listen/listen.handler.ts new
+547
@@ -0,0 +1,547 @@
1 +import * as FileSystem from 'effect/FileSystem';
2 +import * as Path from 'effect/Path';
3 +import type { ComposioClientShape } from '@composio/client/effect';
4 +import { Deferred, Effect, FiberSet, Option, Predicate } from 'effect';
5 +import { commandInputError } from 'src/commands/errors';
6 +import { actingUser } from 'src/commands/surface';
7 +import { matchesTriggerListenFilters } from 'src/commands/triggers/filter';
8 +import { parseTriggerListenEvent } from 'src/commands/triggers/parse';
9 +import {
10 + eventCount,
11 + finishAcceptedEvents,
12 + makeSinkFailures,
13 +} from 'src/commands/triggers/sink-failures';
14 +import { reportFailure } from 'src/effects/report-failure';
15 +import { resolveOptionalTextInput } from 'src/effects/resolve-optional-text-input';
16 +import { toolkitFromToolSlug } from 'src/effects/toolkit-from-tool-slug';
17 +import {
18 + resolveArtifactsRoot,
19 + resolveCliSessionArtifacts,
20 +} from 'src/services/cli-session-artifacts';
21 +import { ComposioClientSingleton, type ToolkitProjectScope } from 'src/services/composio-clients';
22 +import { idempotent } from 'src/services/composio-request';
23 +import { resolveConnectedAccountForToolkit } from 'src/services/connected-account-selection';
24 +import { LISTENER_STOP, ShutdownSignal } from 'src/services/shutdown-signal';
25 +import { TerminalUI } from 'src/services/terminal-ui';
26 +import { TriggersRealtime } from 'src/services/triggers-realtime';
27 +import { ListenCommandError } from './listen.errors';
28 +import { emitStreamLine, formatStopMessage, formatStreamValue } from './listen.format';
29 +import {
30 + applyStreamPath,
31 + parseCreateParams,
32 + parseStreamPath,
33 + parseTimeoutMs,
34 +} from './listen.parse';
35 +
36 +type TriggerCreateParams = NonNullable<
37 + Parameters<ComposioClientShape['triggerInstances']['upsert']>[1]
38 +>;
39 +
40 +export type ListenParams = {
41 + readonly slug: string;
42 + readonly params: Option.Option<string>;
43 + readonly maxEvents: Option.Option<number>;
44 + readonly timeout: Option.Option<string>;
45 + readonly stream: Option.Option<string>;
46 + readonly account: Option.Option<string>;
47 + readonly failFast: boolean;
48 + readonly debug: boolean;
49 +};
50 +
51 +const invalidOptionValue = commandInputError('listen');
52 +
53 +const errorMessage = (error: unknown): string =>
54 + Predicate.isError(error) ? error.message : String(error);
55 +
56 +const sanitizePathPart = (value: string): string =>
57 + value.replace(/[^A-Z0-9._-]+/gi, '-').replace(/^-+|-+$/g, '') || 'unknown';
58 +
59 +const disableTemporaryTrigger = (params: {
60 + client: ComposioClientShape;
61 + ui: TerminalUI;
62 + slug: string;
63 + triggerId: string;
64 +}) =>
65 + params.client.triggerInstances.manage.update(params.triggerId, { status: 'disable' }).pipe(
66 + Effect.mapError(
67 + cause =>
68 + new ListenCommandError({
69 + reason: 'disable_trigger',
70 + // "Error: " is how these messages have always introduced the client's failure.
71 + message: `Failed to disable temporary trigger "${params.triggerId}": Error: ${cause.message}`,
72 + slug: params.slug,
73 + cause,
74 + })
75 + ),
76 + Effect.tapError(error => params.ui.log.warn(error.message)),
77 + Effect.ignore
78 + );
79 +
80 +const isProjectEventType = (value: string): boolean => value.startsWith('composio.');
81 +
82 +const resolveParamsInput = (input: Option.Option<string>) =>
83 + resolveOptionalTextInput(input, { missingValue: '{}' });
84 +
85 +const assertSupportedListenParams = (params: {
86 + listeningToProjectEvent: boolean;
87 + slug: string;
88 + createParamsInput: Record<string, unknown>;
89 +}) =>
90 + params.listeningToProjectEvent && Object.keys(params.createParamsInput).length > 0
91 + ? Effect.fail(
92 + invalidOptionValue(
93 + `--params is only supported for trigger slugs. "${params.slug}" is a project-level composio.* event type and does not create a temporary trigger.`,
94 + '--params'
95 + )
96 + )
97 + : Effect.void;
98 +
99 +/**
100 + * Fails with an `unknown_trigger` error when `slug` is not a known trigger type.
101 + *
102 + * Called when the toolkit has no active connected account, and when creating the temporary trigger
103 + * fails, so a mistyped slug is reported as such instead of as a missing connection or a generic
104 + * creation failure. Neither call site is on the happy path, which makes no additional request.
105 + */
106 +const assertTriggerTypeExists = (params: {
107 + client: ComposioClientShape;
108 + slug: string;
109 + toolkitSlug?: string;
110 +}) =>
111 + params.client.triggersTypes.retrieve(params.slug, undefined, idempotent).pipe(
112 + Effect.catchTag(
113 + '@composio/client/NotFoundError',
114 + cause =>
115 + new ListenCommandError({
116 + reason: 'unknown_trigger',
117 + message: `Unknown trigger slug "${params.slug}". List available slugs with \`composio triggers list <toolkit>\`.`,
118 + slug: params.slug,
119 + toolkitSlug: params.toolkitSlug,
120 + cause,
121 + }),
122 + // A found trigger type, or any failure other than 404, is inconclusive here: fall through to
123 + // the caller's own error.
124 + () => Effect.void
125 + ),
126 + Effect.asVoid
127 + );
128 +
129 +const resolveConnectedAccountIdForTrigger = (params: {
130 + client: ComposioClientShape;
131 + slug: string;
132 + consumerUserId: string;
133 + account: Option.Option<string>;
134 + projectScope: ToolkitProjectScope;
135 +}) =>
136 + Effect.gen(function* () {
137 + const toolkitSlug = yield* toolkitFromToolSlug(params.slug, params.projectScope);
138 + if (!toolkitSlug) {
139 + return yield* Effect.fail(
140 + invalidOptionValue(
141 + `Could not infer a toolkit from trigger slug "${params.slug}". Use a standard trigger slug such as "GMAIL_NEW_GMAIL_MESSAGE", or a project event type such as "composio.connected_account.expired".`,
142 + '<slug>'
143 + )
144 + );
145 + }
146 +
147 + // The resolver `composio execute` and `composio proxy` use, so all three pick the same
148 + // account for the same `--account` (or its absence) and fail the same way when they cannot.
149 + const connectedAccountId = yield* resolveConnectedAccountForToolkit({
150 + client: params.client,
151 + toolkitSlug,
152 + userId: params.consumerUserId,
153 + selector: params.account,
154 + });
155 + if (connectedAccountId) {
156 + return connectedAccountId;
157 + }
158 +
159 + yield* assertTriggerTypeExists({ client: params.client, slug: params.slug, toolkitSlug });
160 +
161 + return yield* new ListenCommandError({
162 + reason: 'connected_account_not_found',
163 + message: `No active connected account found for toolkit "${toolkitSlug}" and consumer user "${params.consumerUserId}". Run \`composio link ${toolkitSlug}\` first.`,
164 + slug: params.slug,
165 + toolkitSlug,
166 + });
167 + });
168 +
169 +const eventTypeOf = (eventData: Record<string, unknown>): string | undefined =>
170 + typeof eventData.type === 'string' && eventData.type.length > 0 ? eventData.type : undefined;
171 +
172 +const extractEventFileId = (eventData: Record<string, unknown>): string => {
173 + const metadata = Predicate.isObject(eventData.metadata) ? eventData.metadata : undefined;
174 + const candidates = [eventData.id, eventData.log_id, metadata?.id];
175 +
176 + for (const candidate of candidates) {
177 + if (typeof candidate === 'string' && candidate.length > 0) {
178 + return sanitizePathPart(candidate);
179 + }
180 + }
181 +
182 + return `${Date.now()}-${crypto.randomUUID().slice(0, 8)}`;
183 +};
184 +
185 +/**
186 + * Saves one matched event: its payload file, its line in the stream file, then the `event:` line
187 + * and, when streaming (`streamPath` set), the `stream:` line.
188 + */
189 +const saveListenEvent = (params: {
190 + readonly fs: FileSystem.FileSystem;
191 + readonly ui: TerminalUI;
192 + readonly eventData: Record<string, unknown>;
193 + readonly eventFilePath: string;
194 + readonly streamFilePath: string;
195 + readonly streamEntry: string;
196 + readonly streamPath: ReadonlyArray<string | number> | undefined;
197 +}) =>
198 + Effect.gen(function* () {
199 + const { fs, ui, eventFilePath, streamPath } = params;
200 + yield* fs.writeFileString(eventFilePath, `${JSON.stringify(params.eventData, null, 2)}\n`);
201 + yield* fs.writeFileString(params.streamFilePath, `${params.streamEntry}\n`, { flag: 'a' });
202 + yield* emitStreamLine(`event: ${eventFilePath}`, ui);
203 + if (streamPath !== undefined) {
204 + const streamValue = applyStreamPath(params.eventData, streamPath);
205 + yield* emitStreamLine(`stream: ${formatStreamValue(streamValue)}`, ui);
206 + }
207 + });
208 +
209 +const resolveListenSetup = (params: {
210 + readonly slug: string;
211 + readonly inputParams: Option.Option<string>;
212 + readonly timeout: Option.Option<string>;
213 + readonly stream: Option.Option<string>;
214 + readonly maxEvents: Option.Option<number>;
215 + readonly account: Option.Option<string>;
216 + readonly client: ComposioClientShape;
217 + readonly resolvedProject: {
218 + readonly orgId: string;
219 + readonly projectId: string;
220 + };
221 + readonly consumerUserId: string;
222 +}) =>
223 + Effect.gen(function* () {
224 + const consumerUserId = params.consumerUserId;
225 + const listeningToProjectEvent = isProjectEventType(params.slug);
226 + const rawParams = Option.isSome(params.inputParams)
227 + ? (yield* resolveParamsInput(params.inputParams))?.trim() || '{}'
228 + : '{}';
229 + const createParamsInput = yield* Effect.fromResult(parseCreateParams(rawParams));
230 + yield* assertSupportedListenParams({
231 + listeningToProjectEvent,
232 + slug: params.slug,
233 + createParamsInput,
234 + });
235 +
236 + const resolvedConnectedAccountId = listeningToProjectEvent
237 + ? undefined
238 + : yield* resolveConnectedAccountIdForTrigger({
239 + client: params.client,
240 + slug: params.slug,
241 + consumerUserId,
242 + account: params.account,
243 + projectScope: {
244 + orgId: params.resolvedProject.orgId,
245 + projectId: params.resolvedProject.projectId,
246 + },
247 + });
248 +
249 + const createParams: TriggerCreateParams | undefined = listeningToProjectEvent
250 + ? undefined
251 + : {
252 + ...createParamsInput,
253 + connected_account_id: resolvedConnectedAccountId,
254 + };
255 +
256 + const timeoutMs = Option.isSome(params.timeout)
257 + ? yield* parseTimeoutMs(params.timeout.value)
258 + : undefined;
259 + const streamPath = Option.isSome(params.stream)
260 + ? yield* parseStreamPath(params.stream.value)
261 + : undefined;
262 +
263 + return {
264 + listeningToProjectEvent,
265 + createParams,
266 + timeoutMs,
267 + streamPath,
268 + shouldStream: Option.isSome(params.stream),
269 + maxEventsLimit: Option.getOrUndefined(params.maxEvents),
270 + consumerUserId,
271 + };
272 + });
273 +
274 +/**
275 + * Creates the directory that holds one payload file per matched event, and its `events.jsonl`
276 + * stream file.
277 + */
278 +const prepareListenEventFiles = (params: {
279 + readonly fs: FileSystem.FileSystem;
280 + readonly path: Path.Path;
281 + readonly orgId: string;
282 + readonly consumerUserId: string;
283 + readonly slug: string;
284 + readonly listeningToProjectEvent: boolean;
285 +}) =>
286 + Effect.gen(function* () {
287 + const { fs, path, slug } = params;
288 + const artifactsOption = yield* resolveCliSessionArtifacts({
289 + orgId: params.orgId,
290 + consumerUserId: params.consumerUserId,
291 + });
292 + const artifactsRoot = Option.isSome(artifactsOption)
293 + ? artifactsOption.value.directoryPath
294 + : yield* resolveArtifactsRoot;
295 + const triggerDir = path.join(
296 + artifactsRoot,
297 + params.listeningToProjectEvent ? 'events' : 'triggers',
298 + sanitizePathPart(slug)
299 + );
300 + const streamFilePath = path.join(triggerDir, 'events.jsonl');
301 +
302 + yield* fs.makeDirectory(triggerDir, { recursive: true });
303 + yield* fs.writeFileString(streamFilePath, '', { flag: 'a' });
304 + return { triggerDir, streamFilePath };
305 + });
306 +
307 +export const runListen = (input: ListenParams) =>
308 + Effect.gen(function* () {
309 + const { slug, params, maxEvents, timeout, stream, account, failFast, debug } = input;
310 + const ui = yield* TerminalUI;
311 + const fs = yield* FileSystem.FileSystem;
312 + const path = yield* Path.Path;
313 + const clientSingleton = yield* ComposioClientSingleton;
314 + const realtime = yield* TriggersRealtime;
315 + const shutdown = yield* ShutdownSignal;
316 +
317 + const { userId: actingUserId, project: resolvedProject } = yield* actingUser();
318 +
319 + const client = yield* clientSingleton.getFor({
320 + orgId: resolvedProject.orgId,
321 + projectId: resolvedProject.projectId,
322 + });
323 + const {
324 + listeningToProjectEvent,
325 + createParams,
326 + timeoutMs,
327 + streamPath,
328 + shouldStream,
329 + maxEventsLimit,
330 + consumerUserId,
331 + } = yield* resolveListenSetup({
332 + slug,
333 + inputParams: params,
334 + timeout,
335 + stream,
336 + maxEvents,
337 + account,
338 + client,
339 + resolvedProject,
340 + consumerUserId: actingUserId,
341 + });
342 +
343 + const { triggerDir, streamFilePath } = yield* prepareListenEventFiles({
344 + fs,
345 + path,
346 + orgId: resolvedProject.orgId,
347 + consumerUserId,
348 + slug,
349 + listeningToProjectEvent,
350 + });
351 +
352 + const stopWhenDone = yield* Deferred.make<'max-events' | 'timeout' | 'failed'>();
353 + // Its own signal, so a --fail-fast failure also ends finishAcceptedEvents' wait.
354 + const failed = yield* Deferred.make<void>();
355 + let matchingEvents = 0;
356 + const writeFailures = makeSinkFailures({
357 + ui,
358 + failFast,
359 + stop: Effect.andThen(
360 + Deferred.succeed(failed, undefined),
361 + Deferred.succeed(stopWhenDone, 'failed')
362 + ).pipe(Effect.asVoid),
363 + summary: count => `${eventCount(count)} failed to write while listening.`,
364 + });
365 + const recordWriteFailure = (error: unknown) => writeFailures.record(errorMessage(error));
366 + const seenEventIds = new Set<string>();
367 +
368 + yield* Effect.acquireUseRelease(
369 + listeningToProjectEvent
370 + ? Effect.succeed<null | { trigger_id: string }>(null)
371 + : client.triggerInstances.upsert(slug, createParams).pipe(
372 + Effect.mapError(
373 + cause =>
374 + new ListenCommandError({
375 + reason: 'create_trigger',
376 + message: `Failed to create temporary trigger "${slug}": Error: ${cause.message}`,
377 + slug,
378 + cause,
379 + })
380 + ),
381 + // An active account for the inferred toolkit skips the lookup in
382 + // resolveConnectedAccountIdForTrigger, so a mistyped slug with a valid toolkit prefix
383 + // only surfaces here. The lookup's unknown_trigger failure replaces the create_trigger
384 + // error; any other outcome re-fails with the original error.
385 + Effect.catch(error =>
386 + assertTriggerTypeExists({ client, slug }).pipe(Effect.andThen(Effect.fail(error)))
387 + )
388 + ),
389 + createdTrigger =>
390 + Effect.gen(function* () {
391 + // Scoped below: a stop other than --max-events or --timeout interrupts the event
392 + // callbacks still running before the release hook sums up.
393 + const events = yield* FiberSet.make();
394 + const runEvent = yield* FiberSet.runtime(events)();
395 + yield* emitStreamLine(`listening for events ${slug} (tail at ${streamFilePath})`, ui);
396 + if (debug) {
397 + const debugMsg =
398 + createdTrigger === null
399 + ? `[debug] event_type=${slug} project=${resolvedProject.projectId} org=${resolvedProject.orgId}`
400 + : `[debug] trigger_id=${createdTrigger.trigger_id} project=${resolvedProject.projectId} org=${resolvedProject.orgId} createParams=${JSON.stringify(createParams)}`;
401 + yield* emitStreamLine(debugMsg, ui);
402 + if (createdTrigger !== null) {
403 + yield* emitStreamLine(
404 + `[debug] upsert response: ${JSON.stringify(createdTrigger)}`,
405 + ui
406 + );
407 + }
408 + }
409 +
410 + const onEvent = (eventData: Record<string, unknown>) => {
411 + runEvent(
412 + Effect.gen(function* () {
413 + if (debug) {
414 + yield* emitStreamLine(
415 + `[debug] raw event keys: ${Object.keys(eventData).join(', ')}`,
416 + ui
417 + );
418 + yield* emitStreamLine(
419 + `[debug] raw event (truncated): ${JSON.stringify(eventData).slice(0, 500)}`,
420 + ui
421 + );
422 + }
423 + const parsedTriggerEvent =
424 + createdTrigger === null ? undefined : parseTriggerListenEvent(eventData);
425 + const filterResult =
426 + createdTrigger === null
427 + ? eventTypeOf(eventData) === slug
428 + : parsedTriggerEvent !== undefined &&
429 + matchesTriggerListenFilters(
430 + { triggerId: createdTrigger.trigger_id },
431 + parsedTriggerEvent
432 + );
433 + if (debug) {
434 + yield* emitStreamLine(
435 + createdTrigger === null
436 + ? `[debug] event.type=${eventTypeOf(eventData) ?? '<missing>'} match=${filterResult}`
437 + : `[debug] parsed.id=${parsedTriggerEvent?.id ?? '<missing>'} triggerSlug=${parsedTriggerEvent?.triggerSlug ?? '<missing>'} trigger_id=${createdTrigger.trigger_id} match=${filterResult}`,
438 + ui
439 + );
440 + }
441 + if (!filterResult) {
442 + return;
443 + }
444 +
445 + const eventFileId = extractEventFileId(eventData);
446 + if (seenEventIds.has(eventFileId)) {
447 + if (debug) {
448 + yield* emitStreamLine(`[debug] skipping duplicate event ${eventFileId}`, ui);
449 + }
450 + return;
451 + }
452 + seenEventIds.add(eventFileId);
453 +
454 + matchingEvents += 1;
455 + const eventFilePath = path.join(triggerDir, `${eventFileId}-payload.json`);
456 + const streamEntry = JSON.stringify({
457 + event_id: eventFileId,
458 + event_type: eventTypeOf(eventData),
459 + trigger_id: createdTrigger?.trigger_id,
460 + trigger_slug: createdTrigger ? slug : undefined,
461 + file_path: eventFilePath,
462 + received_at: new Date().toISOString(),
463 + });
464 +
465 + // A matched event counts toward --max-events even when saving it fails.
466 + yield* saveListenEvent({
467 + fs,
468 + ui,
469 + eventData,
470 + eventFilePath,
471 + streamFilePath,
472 + streamEntry,
473 + streamPath: shouldStream ? (streamPath ?? []) : undefined,
474 + }).pipe(Effect.catch(recordWriteFailure));
475 +
476 + if (maxEventsLimit !== undefined && matchingEvents >= maxEventsLimit) {
477 + yield* Deferred.succeed(stopWhenDone, 'max-events').pipe(Effect.ignore);
478 + }
479 + }).pipe(Effect.catch(recordWriteFailure))
480 + );
481 + };
482 +
483 + const listenEffect = realtime
484 + .listenInProject(
485 + { orgId: resolvedProject.orgId, projectId: resolvedProject.projectId },
486 + onEvent,
487 + { onSubscribed: shutdown.cooperate(LISTENER_STOP) }
488 + )
489 + .pipe(Effect.onInterrupt(() => ui.log.info(`Stopped listening for events ${slug}.`)));
490 +
491 + if (timeoutMs !== undefined) {
492 + yield* Effect.forkScoped(
493 + Effect.sleep(timeoutMs).pipe(
494 + Effect.andThen(Deferred.succeed(stopWhenDone, 'timeout')),
495 + Effect.ignore
496 + )
497 + );
498 + }
499 +
500 + // Ctrl-C or SIGTERM after the subscription is a clean stop: exit 0 after the release.
501 + const stopReason = yield* Effect.raceAllFirst([
502 + listenEffect,
503 + Deferred.await(stopWhenDone),
504 + Effect.as(shutdown.requested, 'signal' as const),
505 + ]);
506 + if (stopReason === 'max-events' || stopReason === 'timeout') {
507 + yield* finishAcceptedEvents(events, failed);
508 + }
509 + if (stopReason === 'max-events') {
510 + yield* ui.outro(
511 + formatStopMessage({
512 + matchingEvents,
513 + timedOut: false,
514 + temporaryTriggerDisabled: createdTrigger !== null,
515 + })
516 + );
517 + return;
518 + }
519 +
520 + if (stopReason === 'timeout') {
521 + yield* ui.outro(
522 + formatStopMessage({
523 + matchingEvents,
524 + timedOut: true,
525 + temporaryTriggerDisabled: createdTrigger !== null,
526 + })
527 + );
528 + }
529 + }).pipe(Effect.scoped),
530 + created =>
531 + Effect.gen(function* () {
532 + if (created !== null) {
533 + yield* disableTemporaryTrigger({ client, ui, slug, triggerId: created.trigger_id });
534 + }
535 + // Runs on every stop of the use phase — max-events, timeout, --fail-fast, Ctrl+C or
536 + // SIGTERM, and an interruption — so a piped caller always gets the count.
537 + yield* writeFailures.report;
538 + })
539 + ).pipe(
540 + Effect.catchTag('services/TriggerRealtimeSubscriptionError', error =>
541 + reportFailure(
542 + ui,
543 + `Could not subscribe to realtime trigger events: ${error.message}. Check your network connection or run \`composio login\`.`
544 + )
545 + )
546 + );
547 + });
ts/packages/cli/src/commands/listen/listen.parse.ts new
+129
@@ -0,0 +1,129 @@
1 +import { Effect, Predicate, Result } from 'effect';
2 +import { commandInputError } from 'src/commands/errors';
3 +import { parseJsonRecord } from 'src/utils/parse-json';
4 +
5 +const invalidOptionValue = commandInputError('listen');
6 +
7 +export const parseCreateParams = (raw: string) =>
8 + parseJsonRecord(raw).pipe(
9 + Result.mapError(error =>
10 + invalidOptionValue(
11 + error.reason === 'not-a-record'
12 + ? "Expected --params to be an object, e.g. -p '{ trigger_config: { ... } }'."
13 + : "Invalid --params input. Provide JSON or a JS-style object literal, e.g. -p '{ trigger_config: { ... } }'.",
14 + '--params'
15 + )
16 + )
17 + );
18 +
19 +export const parseStreamPath = (expression: string) =>
20 + Effect.gen(function* () {
21 + const trimmed = expression.trim();
22 + const pathTokens: Array<string | number> = [];
23 + // An empty --stream streams the whole event.
24 + if (trimmed.length === 0) return pathTokens;
25 + if (!trimmed.startsWith('.')) {
26 + return yield* Effect.fail(
27 + invalidOptionValue(
28 + 'Expected --stream to contain a jq-like path starting with "."',
29 + '--stream'
30 + )
31 + );
32 + }
33 +
34 + const tokenPattern = /(?:\.([A-Za-z0-9_-]+))|(?:\[(\d+)\])/g;
35 + let lastIndex = 0;
36 +
37 + for (const match of trimmed.matchAll(tokenPattern)) {
38 + if ((match.index ?? -1) !== lastIndex) {
39 + return yield* Effect.fail(
40 + invalidOptionValue(
41 + 'Unsupported --stream expression. Use a jq-like path such as ".foo.bar" or ".items[0].id".',
42 + '--stream'
43 + )
44 + );
45 + }
46 +
47 + if (match[1]) pathTokens.push(match[1]);
48 + if (match[2]) pathTokens.push(Number(match[2]));
49 + lastIndex += match[0].length;
50 + }
51 +
52 + if (lastIndex !== trimmed.length) {
53 + return yield* Effect.fail(
54 + invalidOptionValue(
55 + 'Unsupported --stream expression. Use a jq-like path such as ".foo.bar" or ".items[0].id".',
56 + '--stream'
57 + )
58 + );
59 + }
60 +
61 + return pathTokens;
62 + });
63 +
64 +export const applyStreamPath = (
65 + value: unknown,
66 + pathTokens: ReadonlyArray<string | number>
67 +): unknown => {
68 + let current = value;
69 + for (const token of pathTokens) {
70 + if (typeof token === 'number') {
71 + if (!Array.isArray(current) || token < 0 || token >= current.length) {
72 + return undefined;
73 + }
74 + current = current[token];
75 + continue;
76 + }
77 +
78 + if (!Predicate.isObject(current)) {
79 + return undefined;
80 + }
81 +
82 + current = current[token];
83 + }
84 +
85 + return current;
86 +};
87 +
88 +const TIMEOUT_UNITS_MS: Record<string, number> = {
89 + ms: 1,
90 + millisecond: 1,
91 + milliseconds: 1,
92 + s: 1_000,
93 + sec: 1_000,
94 + secs: 1_000,
95 + second: 1_000,
96 + seconds: 1_000,
97 + m: 60_000,
98 + min: 60_000,
99 + mins: 60_000,
100 + minute: 60_000,
101 + minutes: 60_000,
102 + h: 3_600_000,
103 + hr: 3_600_000,
104 + hrs: 3_600_000,
105 + hour: 3_600_000,
106 + hours: 3_600_000,
107 + d: 86_400_000,
108 + day: 86_400_000,
109 + days: 86_400_000,
110 +};
111 +
112 +export const parseTimeoutMs = (value: string) =>
113 + Effect.gen(function* () {
114 + const trimmed = value.trim().toLowerCase();
115 + const match = /^(\d+(?:\.\d+)?)\s*([a-z]+)$/.exec(trimmed);
116 + const amount = Number(match?.[1]);
117 + const unit = match?.[2];
118 + const unitMs = unit === undefined ? undefined : TIMEOUT_UNITS_MS[unit];
119 + if (!match || !Number.isFinite(amount) || amount <= 0 || unitMs === undefined) {
120 + return yield* Effect.fail(
121 + invalidOptionValue(
122 + 'Invalid --timeout value. Use a duration such as "30s", "5m", "1hr", or "1day".',
123 + '--timeout'
124 + )
125 + );
126 + }
127 +
128 + return Math.round(amount * unitMs);
129 + });
ts/packages/cli/test/src/commands/listen/listen.client.cmd.test.ts renamed
ts/packages/cli/test/src/commands/listen/listen.cmd.test.ts renamed
+1 -1
@@ -1,7 +1,7 @@
1 import { describe, expect, layer } from '@effect/vitest';
2 import { ConfigProvider, Effect, Fiber, Latch, Layer } from 'effect';
3 import { TestClock } from 'effect/testing';
4 -import { ListenCommandError } from 'src/commands/listen.cmd';
4 +import { ListenCommandError } from 'src/commands/listen/listen.errors';
5 import type { ConnectedAccountItem } from 'src/models/connected-accounts';
6 import { ConnectedAccountResolutionError } from 'src/services/connected-account-selection';
7 import { MissingActingUserError } from 'src/commands/surface';