@devries-cameron20 / composio Mirror · read-only / commits / 013147f76
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

fix(cli): resolve the `listen` account like `execute` and `proxy` (#5122)

Closes #4797 This PR: - routes `composio listen` through `resolveConnectedAccountForToolkit`, the resolver `composio execute` and `composio proxy` already use, replacing its own `connectedAccounts.list` + `resolveConnectedAccountSelection` copy - an `--account` that matches nothing, or a failed account lookup, now fails with the shared `ConnectedAccountResolutionError` (an expected failure: plain message on stderr, exit 1) instead of `ListenCommandError`; the unused `connected_accounts` reason is dropped - with several active accounts, none aliased `default`, and no `--account`, `listen` now fails with the same "Multiple connected accounts exist … Pass --account" error as `execute`/`proxy`, instead of using the newest account - keeps listen's own "no active connected account … run `composio link`" failure and its unknown-slug check when the toolkit has no account; `--fail-fast` and the rest of the listener are untouched - `dev listen` (`dev triggers listen`) takes no `--account` and does no account selection, so it is unchanged - adds `listen` to the CLI changelog's ambiguity entry, plus a patch entry for the shared resolver, under Unreleased ## Context The ambiguity guard in `resolveConnectedAccountForToolkit` (originally #4745) landed on `next` via #5126, and this branch now includes it. Because `listen` goes through that resolver, it gets the guard with no listen-specific code. One message change: a mistyped trigger slug combined with an unmatched `--account` now reports the account mismatch rather than the unknown slug, the same as `execute`/`proxy` report a bad `--account`. A mistyped slug with a matching account is still reported as unknown when the trigger is created. Tests: - `listen.cmd.test.ts`: `--account work` among two active accounts creates the temporary trigger for `con_work`; `--account nope` fails with `ConnectedAccountResolutionError` and the shared "No connected account matched … Available accounts: …" message; no `--account` and no `default` alias fails with `ConnectedAccountResolutionError` and the shared "Multiple connected accounts exist … Pass --account <alias or id> to choose one." message. The last two cases fail without the fix. - `listen.client.cmd.test.ts`: the failed-lookup case now expects the shared error and message. Verified locally: `pnpm --filter @composio/cli test` (209 files, 2430 passed, 1 skipped; `validate:command-tests` OK), root `pnpm typecheck`, `lint:effect` (0 diagnostics), `oxlint ts/packages/cli` (0 errors, no findings in touched files), prettier. No VHS recording, because the command surface and documented workflow don't change.

Alberto Schiabel committed Oct 28, 2026 at 10:46 UTC 013147f768b553c4a5ae6d3eae4fffbca49570df
4 files changed +163 -66
ts/packages/cli/CHANGELOG.md
+13 -6
@@ -10,12 +10,12 @@
10 exits 1. Without it, a failed event is still reported and skipped, and the
11 exit code stays 0.
12
13 -- `composio execute` and `composio proxy` now fail with an error listing
14 - the candidate accounts, instead of silently using the newest one, when a
15 - toolkit has several connected accounts, none is aliased `default`, and no
16 - `--account` is passed. Scripts that relied on the implicit newest-account
17 - pick now fail. To migrate, pass `--account <id|alias>`, or alias one account
18 - `default`.
13 +- `composio execute`, `composio proxy` and `composio listen` (for a trigger
14 + slug) now fail with an error listing the candidate accounts, instead of
15 + silently using the newest one, when a toolkit has several connected accounts,
16 + none is aliased `default`, and no `--account` is passed. Scripts that relied
17 + on the implicit newest-account pick now fail. To migrate, pass
18 + `--account <id|alias>`, or alias one account `default`.
19
20 ### Patch Changes
21
@@ -32,6 +32,13 @@
32 the single matching upload field in an existing array item; ambiguous targets
33 require explicit JSON. This fixes Word and PowerPoint image editing through the CLI.
34
35 +- `composio listen` now picks the connected account for a trigger slug the way
36 + `composio execute` and `composio proxy` do, through the same resolver, so the
37 + three commands choose the same account for the same `--account` (or its
38 + absence). An `--account` that matches no active account, or a failed account
39 + lookup, now fails with the message those commands print, as a plain message
40 + on stderr with exit 1.
41 +
42 - `composio upgrade` no longer starts with a stale "Update available" line.
43 The line came from the cached daily update check, so right after a release
44 it could name an older version than the one `composio upgrade` then
ts/packages/cli/src/commands/listen.cmd.ts
+15 -57
@@ -16,15 +16,11 @@ import {
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 {
20 - formatConnectedAccountChoices,
21 - resolveConnectedAccountSelection,
22 -} from 'src/services/connected-account-selection';
19 +import { resolveConnectedAccountForToolkit } from 'src/services/connected-account-selection';
20 import { parseJsonRecord } from 'src/utils/parse-json';
21 import { toolkitFromToolSlug } from 'src/effects/toolkit-from-tool-slug';
22 import { matchesTriggerListenFilters } from './triggers/filter';
23 import { parseTriggerListenEvent } from './triggers/parse';
27 -import { decodeConnectedAccountItems } from 'src/effects/decode-connected-account-list';
24 import { idempotent } from 'src/services/composio-request';
25
26 type TriggerCreateParams = NonNullable<
@@ -34,7 +30,6 @@ type TriggerCreateParams = NonNullable<
30 export class ListenCommandError extends Data.TaggedError('commands/ListenCommandError')<{
31 readonly reason:
32 | 'project_context'
37 - | 'connected_accounts'
33 | 'connected_account_not_found'
34 | 'unknown_trigger'
35 | 'create_trigger'
@@ -157,9 +152,9 @@ const assertSupportedListenParams = (params: {
152 /**
153 * Fails with an `unknown_trigger` error when `slug` is not a known trigger type.
154 *
160 - * Called when no active connected account matched, and when creating the temporary trigger fails,
161 - * so a mistyped slug is reported as such instead of as a missing connection or a generic creation
162 - * failure. Neither call site is on the happy path, which makes no additional request.
155 + * Called when the toolkit has no active connected account, and when creating the temporary trigger
156 + * fails, so a mistyped slug is reported as such instead of as a missing connection or a generic
157 + * creation failure. Neither call site is on the happy path, which makes no additional request.
158 */
159 const assertTriggerTypeExists = (params: {
160 client: ComposioClientShape;
@@ -202,60 +197,23 @@ const resolveConnectedAccountIdForTrigger = (params: {
197 );
198 }
199
205 - const connectedAccounts = yield* params.client.connectedAccounts
206 - .list(
207 - {
208 - toolkit_slugs: [toolkitSlug],
209 - user_ids: [params.consumerUserId],
210 - statuses: ['ACTIVE'],
211 - limit: 100,
212 - },
213 - idempotent
214 - )
215 - .pipe(
216 - Effect.mapError(
217 - cause =>
218 - new ListenCommandError({
219 - reason: 'connected_accounts',
220 - message: `Failed to list connected accounts for "${toolkitSlug}": Error: ${cause.message}`,
221 - slug: params.slug,
222 - toolkitSlug,
223 - cause,
224 - })
225 - )
226 - );
227 - const selectableAccounts = yield* decodeConnectedAccountItems(connectedAccounts.items).pipe(
228 - Effect.mapError(
229 - cause =>
230 - new ListenCommandError({
231 - reason: 'connected_accounts',
232 - message: `Connected accounts for toolkit "${toolkitSlug}" did not match the expected response shape.`,
233 - slug: params.slug,
234 - toolkitSlug,
235 - cause,
236 - })
237 - )
238 - );
239 - const selectedAccount = resolveConnectedAccountSelection(
240 - selectableAccounts,
241 - Option.getOrUndefined(params.account)
242 - );
243 - if (selectedAccount?.id) {
244 - return selectedAccount.id;
200 + // The resolver `composio execute` and `composio proxy` use, so all three pick the same
201 + // account for the same `--account` (or its absence) and fail the same way when they cannot.
202 + const connectedAccountId = yield* resolveConnectedAccountForToolkit({
203 + client: params.client,
204 + toolkitSlug,
205 + userId: params.consumerUserId,
206 + selector: params.account,
207 + });
208 + if (connectedAccountId) {
209 + return connectedAccountId;
210 }
211
212 yield* assertTriggerTypeExists({ client: params.client, slug: params.slug, toolkitSlug });
213
249 - const choices = formatConnectedAccountChoices(selectableAccounts);
250 - const suffix =
251 - Option.isSome(params.account) && choices.length > 0
252 - ? ` Available accounts: ${choices.join(', ')}.`
253 - : '';
214 return yield* new ListenCommandError({
215 reason: 'connected_account_not_found',
256 - message: Option.isSome(params.account)
257 - ? `No connected account matched "${params.account.value}" for toolkit "${toolkitSlug}" and consumer user "${params.consumerUserId}".${suffix}`
258 - : `No active connected account found for toolkit "${toolkitSlug}" and consumer user "${params.consumerUserId}". Run \`composio link ${toolkitSlug}\` first.`,
216 + message: `No active connected account found for toolkit "${toolkitSlug}" and consumer user "${params.consumerUserId}". Run \`composio link ${toolkitSlug}\` first.`,
217 slug: params.slug,
218 toolkitSlug,
219 });
ts/packages/cli/test/src/commands/listen.client.cmd.test.ts
+4 -3
@@ -190,10 +190,11 @@ describe('CLI: composio listen over the API client', () => {
190 Effect.flip
191 );
192
193 + // The same failure `composio execute` and `composio proxy` report.
194 expect(failure).toMatchObject({
194 - _tag: 'commands/ListenCommandError',
195 - reason: 'connected_accounts',
196 - message: `Failed to list connected accounts for "gmail": Error: ${invalidConfigText}`,
195 + _tag: 'services/ConnectedAccountResolutionError',
196 + toolkitSlug: 'gmail',
197 + message: `Failed to load connected accounts for toolkit "gmail": Error: ${invalidConfigText}`,
198 });
199 })
200 );
ts/packages/cli/test/src/commands/listen.cmd.test.ts
+131
@@ -3,6 +3,8 @@ import { describe, expect, layer } from '@effect/vitest';
3 import { ConfigProvider, Effect, Fiber, Latch, Layer } from 'effect';
4 import { TestClock } from 'effect/testing';
5 import { ListenCommandError } from 'src/commands/listen.cmd';
6 +import type { ConnectedAccountItem } from 'src/models/connected-accounts';
7 +import { ConnectedAccountResolutionError } from 'src/services/connected-account-selection';
8 import { ComposioCliUserConfig } from 'src/services/cli-user-config';
9 import { TriggerRealtimeSubscriptionError, TriggersRealtime } from 'src/services/triggers-realtime';
10 import { cli, MockConsole, TestLive } from 'test/__utils__';
@@ -300,6 +302,135 @@ describe('CLI: composio listen', () => {
302 );
303 });
304
305 + const gmailAccount = (id: string, alias: string, updatedAt: string): ConnectedAccountItem => ({
306 + id,
307 + alias,
308 + word_id: null,
309 + status: 'ACTIVE',
310 + status_reason: null,
311 + is_disabled: false,
312 + user_id: 'consumer-user-org_test',
313 + toolkit: { slug: 'gmail' },
314 + auth_config: {
315 + id: 'auth_gmail_test',
316 + auth_scheme: 'OAUTH2',
317 + is_composio_managed: true,
318 + is_disabled: false,
319 + },
320 + created_at: '2026-04-06T17:00:00.000Z',
321 + updated_at: updatedAt,
322 + test_request_endpoint: '',
323 + });
324 +
325 + const gmailMessageFor = (connectedAccountId: string) => ({
326 + id: `evt_${connectedAccountId}`,
327 + type: 'composio.trigger.message',
328 + timestamp: '2026-04-06T18:00:00.000Z',
329 + metadata: {
330 + log_id: `log_${connectedAccountId}`,
331 + trigger_slug: 'GMAIL_NEW_GMAIL_MESSAGE',
332 + trigger_id: `trg_gmail_new_gmail_message_${connectedAccountId}`,
333 + connected_account_id: connectedAccountId,
334 + auth_config_id: 'auth_gmail_test',
335 + user_id: 'consumer-user-org_test',
336 + },
337 + data: { threadId: `thread_${connectedAccountId}` },
338 + });
339 +
340 + layer(
341 + TestLive({
342 + baseConfigProvider: testConfigProvider,
343 + fixture: 'global-test-user-id',
344 + connectedAccountsData: {
345 + items: [
346 + gmailAccount('con_work', 'work', '2026-04-06T18:00:00.000Z'),
347 + gmailAccount('con_personal', 'personal', '2026-04-06T18:01:00.000Z'),
348 + ],
349 + },
350 + toolkitsData: {
351 + triggerTypes: [
352 + {
353 + slug: 'GMAIL_NEW_GMAIL_MESSAGE',
354 + name: 'New Gmail Message',
355 + description: 'Fires when a new email arrives.',
356 + instructions: '',
357 + type: 'poll',
358 + config: {},
359 + payload: {},
360 + toolkit: { name: 'Gmail', slug: 'gmail' },
361 + },
362 + ],
363 + },
364 + // One event per account, so the run stops whichever account the temporary trigger uses.
365 + realtimeData: {
366 + events: [gmailMessageFor('con_work'), gmailMessageFor('con_personal')],
367 + },
368 + })
369 + )('[Given] several active accounts for the inferred toolkit', it => {
370 + it.effect('[Then] --account picks the matching account for the temporary trigger', () =>
371 + Effect.gen(function* () {
372 + yield* enableListen;
373 + yield* cli([
374 + 'listen',
375 + 'GMAIL_NEW_GMAIL_MESSAGE',
376 + '--account',
377 + 'work',
378 + '--max-events',
379 + '1',
380 + '--debug',
381 + ]);
382 +
383 + const output = (yield* MockConsole.getLines({ stripAnsi: true })).join('\n');
384 + expect(output).toContain('trigger_id=trg_gmail_new_gmail_message_con_work ');
385 + expect(output).toContain('Stopped after receiving 1 event. Temporary trigger disabled.');
386 + })
387 + );
388 +
389 + it.effect(
390 + '[Then] an --account that matches none fails like `composio execute` and `composio proxy`',
391 + () =>
392 + Effect.gen(function* () {
393 + yield* enableListen;
394 + const failure = yield* cli([
395 + 'listen',
396 + 'GMAIL_NEW_GMAIL_MESSAGE',
397 + '--account',
398 + 'nope',
399 + ]).pipe(Effect.flip);
400 +
401 + // An expected failure: `cli-main.ts` prints the message and exits 1.
402 + expect(failure).toBeInstanceOf(ConnectedAccountResolutionError);
403 + expect(failure).toMatchObject({
404 + toolkitSlug: 'gmail',
405 + message:
406 + 'No connected account matched "nope" for toolkit "gmail". Available accounts: personal (con_personal), work (con_work).',
407 + });
408 + })
409 + );
410 +
411 + it.effect(
412 + '[Then] no --account and no default alias fails like `composio execute` and `composio proxy`',
413 + () =>
414 + Effect.gen(function* () {
415 + yield* enableListen;
416 + // --max-events bounds the run, so a listener that picked an account stops instead of hanging.
417 + const failure = yield* cli([
418 + 'listen',
419 + 'GMAIL_NEW_GMAIL_MESSAGE',
420 + '--max-events',
421 + '1',
422 + ]).pipe(Effect.flip);
423 +
424 + expect(failure).toBeInstanceOf(ConnectedAccountResolutionError);
425 + expect(failure).toMatchObject({
426 + toolkitSlug: 'gmail',
427 + message:
428 + 'Multiple connected accounts exist for toolkit "gmail" and no --account was given. Available accounts: personal (con_personal), work (con_work). Pass --account <alias or id> to choose one.',
429 + });
430 + })
431 + );
432 + });
433 +
434 layer(
435 TestLive({
436 baseConfigProvider: testConfigProvider,