@devries-cameron20 / composio-1 / commits / 7012f8d36
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(cli): retry only API calls that are safe to repeat (#4860)

This PR: - is rebased onto `next` after https://github.com/ComposioHQ/composio/pull/4856 merged; the reads https://github.com/ComposioHQ/composio/pull/4878 made lenient pass `idempotent` alone, the three validated reads pass `{ ...idempotent, ...validated }` - builds every CLI client with `maxRetries: 0`; a call that is safe to repeat passes the new `idempotent` request option (`src/services/composio-request.ts`) and keeps the client's two retries - marks about 35 call sites `idempotent`: every read, plus four calls whose repeat changes nothing (realtime channel auth, presigned upload URL, consumer project resolve, the tool-schema meta call in `tools search`) - stops retrying creates, updates, deletes, links, tool-router sessions and CLI login sessions; tool execution and `proxy` were already sent once and drop their now-redundant `maxRetries: 0` - adds `lint:effect` (`scripts/check-effect-diagnostics.ts`), which runs the Effect language service over CLI `src/` and `test/` and fails on error-level diagnostics such as `floatingEffect`; the typecheck workflow runs it - returns the never-succeeding Effect in the two listen commands, the only existing findings of that check - adds `nonEmptyOrUndefined` (`src/utils/optional-params.ts`) for "leave an empty filter list out of the request", replacing five `list.length > 0 ? list : undefined` sites in files this PR touches (review feedback) ## Context The client retried a failed attempt (connection failure, timeout, 408, 409, 429, 5xx) whatever the call. A write that failed in flight may already have acted, so a retry could create a second auth config, trigger, link or session, or turn a successful delete into a "not found". Defaulting to no retries makes a forgotten option cost resilience on a read instead of a repeated side effect. A client call that is never yielded compiles and silently sends nothing. Neither tsc nor oxlint sees it; the Effect language service does, but only in editors until now. The check takes about a minute and uses the CLI's TypeScript 6 alias, because the workspace's TypeScript 7 has no JS API. Behavior change: a write that hits a 429 or a 5xx now fails on the first attempt instead of being retried. Reads behave as before; a live run of 23 read-only commands matches the pre-refactor baseline (22 byte-identical, 1 differing only in a request id). After the rebase: the CLI suite passes (152 files, 1673 passed, 1 skipped; 18 exemptions), `pnpm typecheck`, `lint:effect` (0 diagnostics), `oxlint` on every touched file and `validate:boundaries` are clean.

Alberto Schiabel committed Oct 15, 2026 at 20:47 UTC 7012f8d367c8ab1dd56962f220dad9daa51c5400
36 files changed +396 -192
.github/workflows/ts.typecheck.yml
+3
@@ -50,3 +50,6 @@ jobs:
50
51 - name: Run Typecheck
52 run: pnpm run typecheck
53 +
54 + - name: Check Effect diagnostics in the CLI
55 + run: pnpm --filter @composio/cli run lint:effect
ts/packages/cli/AGENTS.md
+2 -2
@@ -4,7 +4,7 @@ Instructions for AI agents working on `@composio/cli`. The sibling `CLAUDE.md` i
4
5 ## Required Checks
6
7 -When you touch CLI code (anything under `ts/packages/cli/src/`), run `pnpm typecheck` from the repo root before pushing. Fix all type errors. Build/lint failures block CI.
7 +When you touch CLI code (anything under `ts/packages/cli/src/`), run `pnpm typecheck` from the repo root before pushing. Fix all type errors. Build/lint failures block CI. Also run `pnpm --filter @composio/cli run lint:effect`, which runs the Effect language service over `src/` and `test/` and fails on its error-level diagnostics; the main one is `floatingEffect`, an Effect that is created and never yielded (a dropped `client.…(...)` call compiles and silently sends nothing). CI runs it in the typecheck workflow.
8
9 This package pins its `typescript` dependency to TypeScript 6 (`catalog:ts6` in `package.json`) because `src/generation/typescript/*` drives the JS compiler API, which TS7 (tsgo) does not ship. The pin only affects `import ts from 'typescript'` resolution — the `tsc` binary the typecheck scripts invoke still comes from the workspace root (TS7), since the TS6 alias package only ships a `tsc6` bin. Keep the pin until the generation pipeline moves off the compiler API.
10
@@ -116,7 +116,7 @@ test/src/commands/<group>/<shared-module>.test.ts group helper
116 | `JsPackageManagerDetector` | Detects npm/pnpm/yarn/bun for install instructions |
117 | `UpgradeBinary` | Fetches latest release from GitHub, downloads and replaces binary |
118
119 -Every Composio API request goes through an Effect client from `@composio/client/effect`, obtained from `ComposioClientSingleton` (`src/services/composio-clients.ts`). Clients are built with `make`, which never reads the ambient environment: the user key and base URL come from the CLI's own configuration. Each client is built with `deprecationNotices: 'off'`, the CLI's own `defaultHeaders` (provenance headers, `x-user-api-key`, plus `x-org-id`/`x-project-id` when both are known), and a counting `fetch` (`composio-client-metrics.ts`) provided as the transport's `FetchHttpClient.Fetch`. Explicitly configured HTTP base URLs remain supported without an additional environment flag. `composio-clients.ts` holds client construction, the two repositories, and the `collectAll` and `sortBySlug` helpers the catalog fetches share. The repository's auth-config, connected-account, trigger-instance, search, and single-item methods live in `src/services/composio-resource-operations.ts`; the account helpers (`listOrganizations`, `getSessionInfo*`, `resolveConsumerProject`, `createProjectApiKey`, `getLatestToolVersion`, …) in `src/services/composio-account.ts`; toolkit version validation and its errors (`InvalidToolkitsError`, `InvalidToolkitVersionsError`) in `src/services/toolkit-version-validation.ts`; the shared error types (`ClientError`, `HttpDecodingError`, `ComposioClientConfigurationError`) in `src/services/composio-errors.ts`; and the shared `decode` helper and `validated` request option in `src/services/composio-request.ts`. There is no CLI error wrapper around a request: the client's tagged errors (`@composio/client/NotFoundError`, `APIConnectionError`, `DecodeError`, …) flow to callers, which recover with `Effect.catchTag` or the client's `isAPIError` guard. Commands that report an API failure and continue with a fallback share one handler, `handleClientApiError` (`src/effects/handle-http-error.ts`), applied with `Effect.catchTag(CLIENT_API_ERROR_TAGS, …)`; the tag list lives in `src/services/composio-errors.ts`. Endpoints the client does not type use the raw `client.get`/`client.post`; `src/services/tool-permissions.ts` reaches them through its own `requestJson`, which raises `services/ToolPermissionsRequestError` because its callers fail closed on that tag. Do not add a raw `fetch` to the Composio backend from `src/commands`, `src/effects`, or `src/services` — the one sanctioned exception is the bundled companion runtime (`run-helpers-runtime.ts`), which runs in the user's spawned process outside the Effect runtime. (`src/services/agents.ts` also calls `fetch`, against the agents service at agents.composio.dev rather than the Composio backend.) oxlint rejects value imports of the `@composio/client` root and `@composio/client/pagination` from CLI `src/`; type-only imports are allowed. It also rejects importing `make` or the client layers from `@composio/client/effect` anywhere but `composio-clients.ts`, and `getFor` takes an org and a project together or neither, because the two headers are only sent as a pair. Client errors are classified for display by `src/utils/api-error-extraction.ts`.
119 +Every Composio API request goes through an Effect client from `@composio/client/effect`, obtained from `ComposioClientSingleton` (`src/services/composio-clients.ts`). Clients are built with `make`, which never reads the ambient environment: the user key and base URL come from the CLI's own configuration. Each client is built with `deprecationNotices: 'off'`, the CLI's own `defaultHeaders` (provenance headers, `x-user-api-key`, plus `x-org-id`/`x-project-id` when both are known), and a counting `fetch` (`composio-client-metrics.ts`) provided as the transport's `FetchHttpClient.Fetch`. Explicitly configured HTTP base URLs remain supported without an additional environment flag. `composio-clients.ts` holds client construction, the two repositories, and the `collectAll` and `sortBySlug` helpers the catalog fetches share. The repository's auth-config, connected-account, trigger-instance, search, and single-item methods live in `src/services/composio-resource-operations.ts`; the account helpers (`listOrganizations`, `getSessionInfo*`, `resolveConsumerProject`, `createProjectApiKey`, `getLatestToolVersion`, …) in `src/services/composio-account.ts`; toolkit version validation and its errors (`InvalidToolkitsError`, `InvalidToolkitVersionsError`) in `src/services/toolkit-version-validation.ts`; the shared error types (`ClientError`, `HttpDecodingError`, `ComposioClientConfigurationError`) in `src/services/composio-errors.ts`; and the shared `decode` helper and `validated` request option in `src/services/composio-request.ts`. There is no CLI error wrapper around a request: the client's tagged errors (`@composio/client/NotFoundError`, `APIConnectionError`, `DecodeError`, …) flow to callers, which recover with `Effect.catchTag` or the client's `isAPIError` guard. Commands that report an API failure and continue with a fallback share one handler, `handleClientApiError` (`src/effects/handle-http-error.ts`), applied with `Effect.catchTag(CLIENT_API_ERROR_TAGS, …)`; the tag list lives in `src/services/composio-errors.ts`. Endpoints the client does not type use the raw `client.get`/`client.post`; `src/services/tool-permissions.ts` reaches them through its own `requestJson`, which raises `services/ToolPermissionsRequestError` because its callers fail closed on that tag. Do not add a raw `fetch` to the Composio backend from `src/commands`, `src/effects`, or `src/services` — the one sanctioned exception is the bundled companion runtime (`run-helpers-runtime.ts`), which runs in the user's spawned process outside the Effect runtime. (`src/services/agents.ts` also calls `fetch`, against the agents service at agents.composio.dev rather than the Composio backend.) oxlint rejects value imports of the `@composio/client` root and `@composio/client/pagination` from CLI `src/`; type-only imports are allowed. The CLI's clients send every request once. A call that is safe to repeat — a read, or a call whose repeat changes nothing on the server — passes the `idempotent` request option (`src/services/composio-request.ts`) and is retried by the client after a connection failure, a timeout, or a 408, 409, 429 or 5xx response; combine it with `validated` as `{ ...idempotent, ...validated }`. Creates, updates, deletes, links, sessions, tool executions and proxied calls never pass it, because a request that failed in flight may already have acted. It also rejects importing `make` or the client layers from `@composio/client/effect` anywhere but `composio-clients.ts`, and `getFor` takes an org and a project together or neither, because the two headers are only sent as a pair. Client errors are classified for display by `src/utils/api-error-extraction.ts`.
120
121 Services are `Context.Service` classes that export a `<Name>Shape` type and an explicit `static readonly Default` layer (`Layer.effect` / `Layer.sync`, dependencies provided with `Layer.provide`). Build a test double with `Service.of({ ... })`; there are no generated accessors or constructors.
122
ts/packages/cli/package.json
+1
@@ -31,6 +31,7 @@
31 "validate:skills": "tsx ./scripts/validate-skills.ts",
32 "validate:boundaries": "tsx ./scripts/validate-lint-boundaries.ts",
33 "validate:command-tests": "tsx ./scripts/validate-command-tests.ts",
34 + "lint:effect": "tsx ./scripts/check-effect-diagnostics.ts",
35 "install:binary": "bun run ./scripts/install-binary.ts ./dist/composio",
36 "table:metrics": "bun run ./scripts/optimize-metrics-table.ts",
37 "generate:toolkit-slugs": "bun run ./scripts/generate-toolkit-slugs.ts",
ts/packages/cli/scripts/check-effect-diagnostics.ts new
+49
@@ -0,0 +1,49 @@
1 +// Runs the Effect language service over the CLI's sources and tests, and fails
2 +// on any error-level diagnostic. The one this exists for is `floatingEffect`:
3 +// an Effect that is created and never yielded, such as a client call whose
4 +// result is dropped, compiles and silently never runs.
5 +//
6 +// The language service needs TypeScript's JS API, which the workspace's
7 +// TypeScript 7 does not ship, so it runs against this package's TypeScript 6
8 +// alias. It loads `typescript/lib/tsserverlibrary` from its own location, so
9 +// symlinks are preserved to resolve that from this package's node_modules, and
10 +// NODE_PATH lets the alias load the TypeScript 6 package it re-exports.
11 +//
12 +// Usage:
13 +// tsx ./scripts/check-effect-diagnostics.ts
14 +import { spawnSync } from 'node:child_process';
15 +import * as fs from 'node:fs';
16 +import * as path from 'node:path';
17 +
18 +const scriptDir = import.meta.dirname ?? path.dirname(new URL(import.meta.url).pathname);
19 +const packageRoot = path.resolve(scriptDir, '..');
20 +const nodeModules = path.join(packageRoot, 'node_modules');
21 +// <store>/node_modules/@typescript/typescript6 -> <store>/node_modules
22 +const typescriptAliasModules = path.resolve(
23 + fs.realpathSync(path.join(nodeModules, 'typescript')),
24 + '..',
25 + '..'
26 +);
27 +
28 +const result = spawnSync(
29 + process.execPath,
30 + [
31 + '--preserve-symlinks',
32 + '--preserve-symlinks-main',
33 + path.join(nodeModules, '@effect', 'language-service', 'cli.js'),
34 + 'diagnostics',
35 + '--project',
36 + path.join(packageRoot, 'tsconfig.test.json'),
37 + '--severity',
38 + 'error',
39 + '--format',
40 + process.env.GITHUB_ACTIONS === 'true' ? 'github-actions' : 'text',
41 + ],
42 + {
43 + cwd: packageRoot,
44 + stdio: 'inherit',
45 + env: { ...process.env, NODE_PATH: typescriptAliasModules },
46 + }
47 +);
48 +
49 +process.exit(result.status ?? 1);
ts/packages/cli/src/commands/connected-accounts/commands/connected-accounts.link.cmd.ts
+21 -14
@@ -25,6 +25,7 @@ import {
25 resolveDefaultConnectedAccountsByToolkit,
26 } from 'src/services/connected-account-selection';
27 import { decodeConnectedAccountItems } from 'src/effects/decode-connected-account-list';
28 +import { idempotent } from 'src/services/composio-request';
29
30 class ConnectionPollingError extends Data.TaggedError('commands/ConnectionPollingError')<{
31 readonly message: string;
@@ -154,7 +155,7 @@ const waitForActiveConnection = (
155 Effect.retry(
156 Effect.gen(function* () {
157 const account = yield* client.connectedAccounts
157 - .retrieve(connectedAccountId)
158 + .retrieve(connectedAccountId, idempotent)
159 .pipe(
160 failedWith(
161 ConnectedAccountsRequestError,
@@ -308,13 +309,16 @@ const listActiveConnectedAccounts = (params: {
309 readonly toolkitSlug?: string;
310 readonly authConfigId?: string;
311 }) =>
311 - params.client.connectedAccounts.list({
312 - user_ids: [params.userId],
313 - toolkit_slugs: params.toolkitSlug ? [params.toolkitSlug] : undefined,
314 - auth_config_ids: params.authConfigId ? [params.authConfigId] : undefined,
315 - statuses: ['ACTIVE'],
316 - limit: 100,
317 - });
312 + params.client.connectedAccounts.list(
313 + {
314 + user_ids: [params.userId],
315 + toolkit_slugs: params.toolkitSlug ? [params.toolkitSlug] : undefined,
316 + auth_config_ids: params.authConfigId ? [params.authConfigId] : undefined,
317 + statuses: ['ACTIVE'],
318 + limit: 100,
319 + },
320 + idempotent
321 + );
322
323 const formatExistingAccountLabels = (
324 items: ReadonlyArray<{
@@ -513,12 +517,15 @@ const handleListConnectedAccounts = (params: {
517 const accounts = yield* params.ui.withSpinner(
518 `Listing connected accounts for "${toolkitSlug}"...`,
519 client.connectedAccounts
516 - .list({
517 - toolkit_slugs: [toolkitSlug],
518 - user_ids: [resolvedUserId.value],
519 - statuses: ['ACTIVE'],
520 - limit: 100,
521 - })
520 + .list(
521 + {
522 + toolkit_slugs: [toolkitSlug],
523 + user_ids: [resolvedUserId.value],
524 + statuses: ['ACTIVE'],
525 + limit: 100,
526 + },
527 + idempotent
528 + )
529 .pipe(
530 failedWith(
531 ConnectedAccountsRequestError,
ts/packages/cli/src/commands/connected-accounts/commands/connected-accounts.list.cmd.ts
+10 -6
@@ -8,6 +8,7 @@ import { clampLimit } from 'src/ui/clamp-limit';
8 import { redact } from 'src/ui/redact';
9 import { resolveCommandProject } from 'src/services/command-project';
10 import { formatConnectedAccountsTable, formatConnectedAccountsJson } from '../format';
11 +import { idempotent } from 'src/services/composio-request';
12
13 class ConnectedAccountsListRequestError extends requestError(
14 'commands/ConnectedAccountsListRequestError'
@@ -70,12 +71,15 @@ export const connectedAccountsCmd$List = Command.make(
71 const rawResult = yield* ui.withSpinner(
72 'Fetching connected accounts...',
73 client.connectedAccounts
73 - .list({
74 - toolkit_slugs: toolkitSlugs,
75 - user_ids: Option.isSome(userId) ? [userId.value] : undefined,
76 - statuses: Option.isSome(status) ? [status.value] : undefined,
77 - limit: clampLimit(limit),
78 - })
74 + .list(
75 + {
76 + toolkit_slugs: toolkitSlugs,
77 + user_ids: Option.isSome(userId) ? [userId.value] : undefined,
78 + statuses: Option.isSome(status) ? [status.value] : undefined,
79 + limit: clampLimit(limit),
80 + },
81 + idempotent
82 + )
83 .pipe(failedWith(ConnectedAccountsListRequestError, 'Failed to list connected accounts.'))
84 );
85 const result = yield* decodeConnectedAccountListWithFallback(rawResult);
ts/packages/cli/src/commands/connections/commands/connections.list.cmd.ts
+9 -5
@@ -10,6 +10,7 @@ import {
10 getConnectedAccountPermissionGroup,
11 getConsumerPermissionSnapshot,
12 } from 'src/services/tool-permissions';
13 +import { idempotent } from 'src/services/composio-request';
14
15 class MissingConnectionsConsumerUserIdError extends Data.TaggedError(
16 'commands/MissingConnectionsConsumerUserIdError'
@@ -76,11 +77,14 @@ export const connectionsCmd$List = Command.make('list', { toolkit }, ({ toolkit
77 projectId: resolvedProject.projectId,
78 });
79 const rawResult = yield* client.connectedAccounts
79 - .list({
80 - toolkit_slugs: toolkitSlug ? [toolkitSlug] : undefined,
81 - user_ids: [consumerUserId],
82 - limit: 1000,
83 - })
80 + .list(
81 + {
82 + toolkit_slugs: toolkitSlug ? [toolkitSlug] : undefined,
83 + user_ids: [consumerUserId],
84 + limit: 1000,
85 + },
86 + idempotent
87 + )
88 .pipe(failedWith(ConnectionsListRequestError, 'Failed to list connections.'));
89 const result = yield* decodeConnectedAccountListWithFallback(rawResult);
90 const permissionSnapshot = yield* getConsumerPermissionSnapshot({
ts/packages/cli/src/commands/connections/commands/connections.remove.cmd.ts
+2 -1
@@ -8,6 +8,7 @@ import { resolveCommandProject } from 'src/services/command-project';
8 import { TerminalUI } from 'src/services/terminal-ui';
9 import { bold } from 'src/ui/colors';
10 import { redact } from 'src/ui/redact';
11 +import { idempotent } from 'src/services/composio-request';
12
13 class MissingConnectionRemovalConsumerUserIdError extends Data.TaggedError(
14 'commands/MissingConnectionRemovalConsumerUserIdError'
@@ -110,7 +111,7 @@ export const connectionsCmd$Remove = Command.make('remove', { account }, ({ acco
111 const rawResult = yield* ui.withSpinner(
112 'Fetching connections...',
113 client.connectedAccounts
113 - .list({ user_ids: [consumerUserId], limit: 1000 })
114 + .list({ user_ids: [consumerUserId], limit: 1000 }, idempotent)
115 .pipe(failedWith(ConnectionsRemovalRequestError, 'Failed to list connections.'))
116 );
117 const result = yield* decodeConnectedAccountListWithFallback(rawResult);
ts/packages/cli/src/commands/listen.cmd.ts
+12 -9
@@ -23,6 +23,7 @@ import { toolkitFromToolSlug } from 'src/effects/toolkit-from-tool-slug';
23 import { matchesTriggerListenFilters } from './triggers/filter';
24 import { parseTriggerListenEvent } from './triggers/parse';
25 import { decodeConnectedAccountItems } from 'src/effects/decode-connected-account-list';
26 +import { idempotent } from 'src/services/composio-request';
27
28 type TriggerCreateParams = NonNullable<
29 Parameters<ComposioClientShape['triggerInstances']['upsert']>[1]
@@ -158,7 +159,7 @@ const assertTriggerTypeExists = (params: {
159 slug: string;
160 toolkitSlug?: string;
161 }) =>
161 - params.client.triggersTypes.retrieve(params.slug).pipe(
162 + params.client.triggersTypes.retrieve(params.slug, undefined, idempotent).pipe(
163 Effect.catchTag(
164 '@composio/client/NotFoundError',
165 cause =>
@@ -195,12 +196,15 @@ const resolveConnectedAccountIdForTrigger = (params: {
196 }
197
198 const connectedAccounts = yield* params.client.connectedAccounts
198 - .list({
199 - toolkit_slugs: [toolkitSlug],
200 - user_ids: [params.consumerUserId],
201 - statuses: ['ACTIVE'],
202 - limit: 100,
203 - })
199 + .list(
200 + {
201 + toolkit_slugs: [toolkitSlug],
202 + user_ids: [params.consumerUserId],
203 + statuses: ['ACTIVE'],
204 + limit: 100,
205 + },
206 + idempotent
207 + )
208 .pipe(
209 Effect.mapError(
210 cause =>
@@ -667,8 +671,7 @@ export const listenCmd = Command.make(
671 }
672
673 if (maxEventsLimit === undefined && timeoutMs === undefined) {
670 - yield* listenEffect;
671 - return;
674 + return yield* listenEffect;
675 }
676
677 const stopReason = yield* Effect.raceFirst(listenEffect, Deferred.await(stopWhenDone));
ts/packages/cli/src/commands/logs/commands/logs.tools.cmd.ts
+13 -9
@@ -3,8 +3,9 @@ import { Effect, Option } from 'effect';
3 import { failedWith, requestError } from 'src/effects/request-error';
4 import { TerminalUI } from 'src/services/terminal-ui';
5 import { ComposioClientSingleton } from 'src/services/composio-clients';
6 +import { idempotent } from 'src/services/composio-request';
7 import { clampLimit } from 'src/ui/clamp-limit';
7 -import { optionalParams } from 'src/utils/optional-params';
8 +import { nonEmptyOrUndefined, optionalParams } from 'src/utils/optional-params';
9 import { parseCsv } from 'src/commands/triggers/parse-csv';
10 import { formatToolLogInfo, formatToolLogsTable } from '../format';
11 import { commandHintStep } from 'src/services/command-hints';
@@ -187,7 +188,7 @@ export const logsCmd$Tools = Command.make(
188 const toolLog = yield* ui.withSpinner(
189 `Fetching tool log "${toolLogId}"...`,
190 client.logs.tools
190 - .retrieve(toolLogId)
191 + .retrieve(toolLogId, idempotent)
192 .pipe(failedWith(ToolLogsRequestError, `Failed to fetch tool log "${toolLogId}".`))
193 );
194
@@ -201,13 +202,16 @@ export const logsCmd$Tools = Command.make(
202 const response = yield* ui.withSpinner(
203 'Fetching tool logs...',
204 client.logs.tools
204 - .list({
205 - ...optionalParams({ from, to }),
206 - cursor: Option.getOrUndefined(cursor) ?? null,
207 - limit: clampedLimit,
208 - case_sensitive: caseSensitive,
209 - search_params: shorthandSearchParams.length > 0 ? shorthandSearchParams : undefined,
210 - })
205 + .list(
206 + {
207 + ...optionalParams({ from, to }),
208 + cursor: Option.getOrUndefined(cursor) ?? null,
209 + limit: clampedLimit,
210 + case_sensitive: caseSensitive,
211 + search_params: nonEmptyOrUndefined(shorthandSearchParams),
212 + },
213 + idempotent
214 + )
215 .pipe(failedWith(ToolLogsRequestError, 'Failed to fetch tool logs.'))
216 );
217
ts/packages/cli/src/commands/logs/commands/logs.triggers.cmd.ts
+12 -8
@@ -4,12 +4,13 @@ import { failedWith, requestError } from 'src/effects/request-error';
4 import { TerminalUI } from 'src/services/terminal-ui';
5 import { ComposioClientSingleton } from 'src/services/composio-clients';
6 import { clampLimit } from 'src/ui/clamp-limit';
7 -import { optionalParams } from 'src/utils/optional-params';
7 +import { nonEmptyOrUndefined, optionalParams } from 'src/utils/optional-params';
8 import { parseCsv } from 'src/commands/triggers/parse-csv';
9 import { formatTriggerLogInfo, formatTriggerLogsTable } from '../format';
10 import { decodeTriggerLogRecord } from '../trigger-log-record';
11 import { commandHintStep } from 'src/services/command-hints';
12 import { toSearchParam } from '../utils';
13 +import { idempotent } from 'src/services/composio-request';
14
15 class TriggerLogsRequestError extends requestError('commands/TriggerLogsRequestError') {}
16
@@ -152,7 +153,7 @@ export const logsCmd$Triggers = Command.make(
153 const triggerLog = yield* ui.withSpinner(
154 `Fetching trigger log "${triggerLogId}"...`,
155 client.logs.triggers
155 - .retrieve(triggerLogId)
156 + .retrieve(triggerLogId, idempotent)
157 .pipe(
158 failedWith(TriggerLogsRequestError, `Failed to fetch trigger log "${triggerLogId}".`)
159 )
@@ -170,12 +171,15 @@ export const logsCmd$Triggers = Command.make(
171 const response = yield* ui.withSpinner(
172 'Fetching trigger logs...',
173 client.logs.triggers
173 - .list({
174 - ...optionalParams({ cursor, from, to, time, search }),
175 - limit: clampedLimit,
176 - include_payload: includePayload,
177 - search_params: shorthandSearchParams.length > 0 ? shorthandSearchParams : undefined,
178 - })
174 + .list(
175 + {
176 + ...optionalParams({ cursor, from, to, time, search }),
177 + limit: clampedLimit,
178 + include_payload: includePayload,
179 + search_params: nonEmptyOrUndefined(shorthandSearchParams),
180 + },
181 + idempotent
182 + )
183 .pipe(failedWith(TriggerLogsRequestError, 'Failed to fetch trigger logs.'))
184 );
185
ts/packages/cli/src/commands/proxy.cmd.ts
+5 -5
@@ -361,11 +361,11 @@ export const proxyCmd = Command.make('proxy', {
361 method: normalizedMethod,
362 ...(parsedBody !== undefined ? { body: parsedBody } : {}),
363 ...(headerParameters.length > 0 ? { parameters: headerParameters } : {}),
364 - },
365 - // Never retry a proxied call: a retry after the upstream API
366 - // already acted duplicates the side effect. The response is not
367 - // validated either, for the same reason.
368 - { maxRetries: 0 }
364 + }
365 + // A proxied call is not `idempotent`, so it is sent once: a retry
366 + // after the upstream API already acted duplicates the side
367 + // effect. The response is not validated either, for the same
368 + // reason.
369 )
370 .pipe(
371 Effect.mapError(failed),
ts/packages/cli/src/commands/toolkits/commands/toolkits.info.cmd.ts
+2 -1
@@ -2,6 +2,7 @@ import { Argument, Command, Flag } from 'effect/cli';
2 import { Effect, Option } from 'effect';
3 import { ComposioClientSingleton, ComposioToolkitsRepository } from 'src/services/composio-clients';
4 import { TerminalUI } from 'src/services/terminal-ui';
5 +import { idempotent } from 'src/services/composio-request';
6 import { failedWith, requestError } from 'src/effects/request-error';
7 import { resolveToolRouterSession } from 'src/effects/create-tool-router-session';
8 import { extractMessage } from 'src/utils/api-error-extraction';
@@ -90,7 +91,7 @@ export const toolkitsCmd$Info = Command.make(
91 const { sessionId } = yield* resolveToolRouterSession(client, resolvedUserId.value);
92 const sessionToolkits = yield* client.toolRouter.session
93 // Only the first item is read, and only a few of its fields.
93 - .toolkits(sessionId, { toolkits: [slugValue] })
94 + .toolkits(sessionId, { toolkits: [slugValue] }, idempotent)
95 .pipe(
96 failedWith(ToolkitsInfoRequestError, `Failed to fetch toolkit "${slugValue}".`)
97 );
ts/packages/cli/src/commands/toolkits/commands/toolkits.list.cmd.ts
+10 -5
@@ -4,6 +4,7 @@ import { Effect, Option } from 'effect';
4 import { TerminalUI } from 'src/services/terminal-ui';
5 import { resolveToolRouterSession } from 'src/effects/create-tool-router-session';
6 import { ComposioClientSingleton, ComposioToolkitsRepository } from 'src/services/composio-clients';
7 +import { idempotent } from 'src/services/composio-request';
8 import { ProjectContext } from 'src/services/project-context';
9 import { ComposioUserContext } from 'src/services/user-context';
10 import { clampLimit } from 'src/ui/clamp-limit';
@@ -125,11 +126,15 @@ export const toolkitsCmd$List = Command.make(
126 // The session items only add connection status to the catalog rows,
127 // and a failed read is already a silent fallback.
128 sessionItems = yield* client.toolRouter.session
128 - .toolkits(sessionId, {
129 - search: Option.getOrUndefined(query),
130 - limit: clampedLimit,
131 - is_connected: Option.getOrUndefined(connected),
132 - })
129 + .toolkits(
130 + sessionId,
131 + {
132 + search: Option.getOrUndefined(query),
133 + limit: clampedLimit,
134 + is_connected: Option.getOrUndefined(connected),
135 + },
136 + idempotent
137 + )
138 .pipe(
139 Effect.map(r => r.items),
140 Effect.catch(error =>
ts/packages/cli/src/commands/toolkits/commands/toolkits.version.cmd.ts
+2 -1
@@ -3,6 +3,7 @@ import { Effect, Option } from 'effect';
3 import { ComposioClientSingleton } from 'src/services/composio-clients';
4 import { TerminalUI } from 'src/services/terminal-ui';
5 import { extractMessage } from 'src/utils/api-error-extraction';
6 +import { idempotent } from 'src/services/composio-request';
7
8 const slug = Argument.String('slug').pipe(Argument.withDescription('Toolkit slug (e.g. "gmail")'));
9
@@ -24,7 +25,7 @@ export const toolkitsCmd$Version = Command.make('version', { slug }, ({ slug })
25 `Fetching versions for toolkit "${slug}"...`,
26 // Only the name, slug and version list are read, so a toolkit that
27 // lacks other catalog fields still prints its versions.
27 - client.toolkits.retrieve(slug)
28 + client.toolkits.retrieve(slug, undefined, idempotent)
29 )
30 .pipe(
31 Effect.asSome,
ts/packages/cli/src/commands/tools/commands/tools.search.cmd.ts
+11 -5
@@ -13,6 +13,7 @@ import { formatToolsTable } from '../format';
13 import type { Tool } from 'src/models/tools';
14 import { ComposioUserContext } from 'src/services/user-context';
15 import { ComposioClientSingleton } from 'src/services/composio-clients';
16 +import { idempotent } from 'src/services/composio-request';
17 import { resolveCommandProject } from 'src/services/command-project';
18 import { commandHintExample, commandHintStep } from 'src/services/command-hints';
19 import { isRemoteCustomToolSlug } from 'src/utils/remote-custom-toolkit';
@@ -364,7 +365,7 @@ const runToolsSearch = (params: {
365 // The command reads a few of the search response's fields; the rest, enums
366 // among them, may change without this version of the CLI noticing.
367 const searchResponse = yield* client.toolRouter.session
367 - .search(sessionId, searchPayload)
368 + .search(sessionId, searchPayload, idempotent)
369 .pipe(failedWith(ToolsSearchRequestError, 'Failed to search tools.'));
370 const customToolSlugsMissingSchemas = Object.entries(searchResponse.tool_schemas)
371 .filter(([slug, schema]) => isRemoteCustomToolSlug(slug) && !schema.input_schema)
@@ -378,11 +379,16 @@ const runToolsSearch = (params: {
379 message: 'Failed to fetch custom tool schemas.',
380 cause,
381 });
382 + // Fetching tool schemas is a read, so it is retried like one.
383 const schemaResponse = yield* client.toolRouter.session
382 - .executeMeta(sessionId, {
383 - slug: 'COMPOSIO_GET_TOOL_SCHEMAS',
384 - arguments: { tool_slugs: customToolSlugsMissingSchemas },
385 - })
384 + .executeMeta(
385 + sessionId,
386 + {
387 + slug: 'COMPOSIO_GET_TOOL_SCHEMAS',
388 + arguments: { tool_slugs: customToolSlugsMissingSchemas },
389 + },
390 + idempotent
391 + )
392 .pipe(Effect.mapError(schemasFailed));
393 if (schemaResponse.result_type === 'input_required') {
394 return yield* schemasFailed(
ts/packages/cli/src/commands/triggers/commands/triggers.listen.cmd.ts
+1 -2
@@ -326,8 +326,7 @@ export const triggersCmd$Listen = Command.make(
326
327 yield* Effect.gen(function* () {
328 if (maxEventsLimit === undefined) {
329 - yield* listenEffect;
330 - return;
329 + return yield* listenEffect;
330 }
331
332 yield* Effect.raceFirst(listenEffect, Deferred.await(stopWhenDone));
ts/packages/cli/src/effects/create-tool-router-session.ts
+4 -3
@@ -14,6 +14,7 @@ import {
14 getConsumerPermissionSnapshot,
15 type ConsumerPermissionSnapshot,
16 } from 'src/services/tool-permissions';
17 +import { nonEmptyOrUndefined } from 'src/utils/optional-params';
18
19 export interface CreateToolRouterSessionOptions {
20 /** Enable auto connection management. Default: false. */
@@ -101,14 +102,14 @@ export const createToolRouterSessionContext = (
102 ? yield* getFreshConsumerToolRouterAuthConfigsFromCache({
103 orgId: options.cacheScope.orgId,
104 consumerUserId: options.cacheScope.consumerUserId,
104 - toolkits: requestedToolkits.length > 0 ? requestedToolkits : undefined,
105 + toolkits: nonEmptyOrUndefined(requestedToolkits),
106 })
107 : Option.none();
108 const cachedConnectedAccounts = options?.cacheScope
109 ? yield* getFreshConsumerToolRouterConnectedAccountsFromCache({
110 orgId: options.cacheScope.orgId,
111 consumerUserId: options.cacheScope.consumerUserId,
111 - toolkits: requestedToolkits.length > 0 ? requestedToolkits : undefined,
112 + toolkits: nonEmptyOrUndefined(requestedToolkits),
113 })
114 : Option.none();
115
@@ -129,7 +130,7 @@ export const createToolRouterSessionContext = (
130 : undefined,
131 }
132 : yield* resolveToolRouterSessionConnections(client, userId, {
132 - toolkits: requestedToolkits.length > 0 ? requestedToolkits : undefined,
133 + toolkits: nonEmptyOrUndefined(requestedToolkits),
134 }).pipe(
135 Effect.map(connectionContext => ({
136 ...connectionContext,
ts/packages/cli/src/effects/request-error.ts
+1 -1
@@ -28,7 +28,7 @@ export const requestError = <Tag extends string>(
28 *
29 * @example
30 * ```ts
31 - * client.logs.tools.list(query).pipe(failedWith(ToolLogsRequestError, 'Failed to fetch tool logs.'))
31 + * client.logs.tools.list(query, idempotent).pipe(failedWith(ToolLogsRequestError, 'Failed to fetch tool logs.'))
32 * ```
33 */
34 export const failedWith =
ts/packages/cli/src/services/composio-account.ts
+21 -13
@@ -9,7 +9,7 @@ import type { SessionRetrieveInfoResponse } from '@composio/client/resources/aut
9 import { LatestToolVersionResponse } from 'src/models/tools';
10 import { ComposioClientSingleton } from './composio-clients';
11 import { HttpDecodingError, type ClientError } from './composio-errors';
12 -import { decode, validated } from './composio-request';
12 +import { decode, idempotent, validated } from './composio-request';
13
14 /**
15 * Account, session-info, and consumer helpers
@@ -94,7 +94,10 @@ export const listOrganizations = (params: {
94 Effect.gen(function* () {
95 const clientSingleton = yield* ComposioClientSingleton;
96 const client = yield* clientSingleton.getFor({ userApiKey: params.apiKey });
97 - const response = yield* client.org.list({ limit: params.limit ?? 50 }, validated);
97 + const response = yield* client.org.list(
98 + { limit: params.limit ?? 50 },
99 + { ...idempotent, ...validated }
100 + );
101 const organizations = response.organizations.map(
102 (organization: OrgListResponse.Organization) =>
103 ({ id: organization.id, name: organization.name }) satisfies OrganizationSummary
@@ -123,7 +126,7 @@ export const listOrganizationProjects = (params: {
126 const client = yield* clientSingleton.getFor({ userApiKey: params.apiKey });
127 const response = yield* client.org.project.list(
128 { limit: params.limit ?? 50 },
126 - orgScopedOptions(params.orgId)
129 + { ...orgScopedOptions(params.orgId), ...idempotent }
130 );
131 const projects = response.data.map(
132 (project: OrgProject) =>
@@ -153,7 +156,7 @@ export const listOrgProjects = (params: {
156 const client = yield* clientSingleton.getFor({ userApiKey: params.apiKey });
157 return yield* client.org.project.list(
158 { list_all_org_projects: true, limit: params.limit ?? 50 },
156 - orgScopedOptions(params.orgId)
159 + { ...orgScopedOptions(params.orgId), ...idempotent }
160 );
161 });
162
@@ -174,7 +177,7 @@ export const getSessionInfo = (params: {
177 });
178 // Read through a few fields only, and called after login has already
179 // linked the session: drift in a field the CLI never reads must not fail it.
177 - const info = yield* client.auth.session.retrieveInfo();
180 + const info = yield* client.auth.session.retrieveInfo(idempotent);
181 return yield* requireSessionProject(info);
182 });
183
@@ -196,7 +199,10 @@ export const getSessionInfoByUserApiKey = (params: {
199 Effect.gen(function* () {
200 const clientSingleton = yield* ComposioClientSingleton;
201 const client = yield* clientSingleton.getFor({ userApiKey: params.userApiKey });
199 - const info = yield* client.auth.session.retrieveInfo(orgScopedOptions(params.orgId));
202 + const info = yield* client.auth.session.retrieveInfo({
203 + ...orgScopedOptions(params.orgId),
204 + ...idempotent,
205 + });
206 return yield* requireSessionProject(info);
207 });
208
@@ -253,7 +259,6 @@ export const createProjectApiKey = (params: {
259 });
260 const payload = yield* client.post(`/api/v3/org/project/${params.projectId}/api_keys/create`, {
261 body: { name: params.name },
256 - maxRetries: 0,
262 });
263 const createdApiKey = getApiKeyFromPayload(payload);
264
@@ -275,10 +280,13 @@ export const resolveConsumerProject = (params: {
280 Effect.gen(function* () {
281 const clientSingleton = yield* ComposioClientSingleton;
282 const client = yield* clientSingleton.getFor({ userApiKey: params.apiKey });
278 - return yield* client.org.consumer.project.resolve({
279 - 'x-user-api-key': params.apiKey,
280 - 'x-org-id': params.orgId,
281 - });
283 + return yield* client.org.consumer.project.resolve(
284 + {
285 + 'x-user-api-key': params.apiKey,
286 + 'x-org-id': params.orgId,
287 + },
288 + idempotent
289 + );
290 });
291
292 /**
@@ -310,7 +318,7 @@ export const getLatestToolVersion = (params: {
318 );
319 const payload = yield* client.get(
320 `/api/v3/tools/${encodeURIComponent(params.toolSlug)}/get_latest_version`,
313 - { headers }
321 + { headers, ...idempotent }
322 );
323 return yield* decode(LatestToolVersionResponse)(payload);
324 });
@@ -329,7 +337,7 @@ export const getConsumerConnectedToolkits = (params: {
337 'x-user-api-key': params.apiKey,
338 'x-org-id': params.orgId,
339 },
332 - validated
340 + { ...idempotent, ...validated }
341 );
342 });
343
ts/packages/cli/src/services/composio-clients.ts
+25 -15
@@ -36,7 +36,12 @@ import { ProjectContext } from './project-context';
36 import { makeClientMetricsCollector, type ClientMetricsCollector } from './composio-client-metrics';
37 import { ComposioClientConfigurationError, type ClientError } from './composio-errors';
38 import { isClientConfigurationError } from 'src/utils/api-error-extraction';
39 -import { MAX_CONCURRENT_REQUESTS_PER_ENDPOINT, decode, validated } from './composio-request';
39 +import {
40 + MAX_CONCURRENT_REQUESTS_PER_ENDPOINT,
41 + decode,
42 + idempotent,
43 + validated,
44 +} from './composio-request';
45 import { lookupOperations, resourceOperations } from './composio-resource-operations';
46 import { schemaLoaders } from '@composio/client/effect/schemas';
47 import {
@@ -201,6 +206,8 @@ const makeComposioClientSingleton = (metrics: ClientMetricsCollector) =>
206 projectId: scope.projectId,
207 cliSessionId,
208 }),
209 + // Only a call that passes `idempotent` is retried.
210 + maxRetries: 0,
211 schemas: schemaLoaders,
212 },
213 httpClient
@@ -302,12 +309,12 @@ const makeComposioToolkitsRepository = Effect.gen(function* () {
309
310 const listToolkits = (managedBy?: 'project', scope?: ToolkitProjectScope) =>
311 collectAll(scope ? clientSingleton.getFor(scope) : client, c =>
305 - c.toolkits.listStream({ limit: MAX_PAGE_SIZE, managed_by: managedBy })
312 + c.toolkits.listStream({ limit: MAX_PAGE_SIZE, managed_by: managedBy }, idempotent)
313 ).pipe(Effect.flatMap(decode(Toolkits)), Effect.map(sortBySlug));
314
315 const getToolkitDetailed = (slug: string) =>
316 client.pipe(
310 - Effect.flatMap(c => c.toolkits.retrieve(slug)),
317 + Effect.flatMap(c => c.toolkits.retrieve(slug, undefined, idempotent)),
318 Effect.flatMap(decode(ToolkitDetailed))
319 );
320
@@ -344,9 +351,9 @@ const makeComposioToolkitsRepository = Effect.gen(function* () {
351 ).pipe(Effect.map(sortBySlug));
352
353 const listTools = (query: { toolkit_slug?: string; toolkit_versions: string }) =>
347 - collectAll(client, c => c.tools.listStream({ ...query, limit: MAX_PAGE_SIZE })).pipe(
348 - Effect.flatMap(decode(Tools))
349 - );
354 + collectAll(client, c =>
355 + c.tools.listStream({ ...query, limit: MAX_PAGE_SIZE }, idempotent)
356 + ).pipe(Effect.flatMap(decode(Tools)));
357
358 return {
359 getToolkits,
@@ -358,7 +365,7 @@ const makeComposioToolkitsRepository = Effect.gen(function* () {
365 */
366 getToolsAsEnums: () =>
367 client.pipe(
361 - Effect.flatMap(c => c.tools.retrieveEnum()),
368 + Effect.flatMap(c => c.tools.retrieveEnum(idempotent)),
369 Effect.flatMap(decode(ToolsAsEnums))
370 ),
371 /**
@@ -388,7 +395,7 @@ const makeComposioToolkitsRepository = Effect.gen(function* () {
395 */
396 getTriggerTypesAsEnums: () =>
397 client.pipe(
391 - Effect.flatMap(c => c.triggersTypes.retrieveEnum()),
398 + Effect.flatMap(c => c.triggersTypes.retrieveEnum(idempotent)),
399 Effect.flatMap(decode(TriggerTypesAsEnums))
400 ),
401 /**
@@ -398,10 +405,13 @@ const makeComposioToolkitsRepository = Effect.gen(function* () {
405 */
406 getTriggerTypes: (toolkitSlugs?: ReadonlyArray<string>) =>
407 collectAll(client, c =>
401 - c.triggersTypes.listStream({
402 - limit: MAX_PAGE_SIZE,
403 - toolkit_slugs: toolkitSlugs ? [...toolkitSlugs] : undefined,
404 - })
408 + c.triggersTypes.listStream(
409 + {
410 + limit: MAX_PAGE_SIZE,
411 + toolkit_slugs: toolkitSlugs ? [...toolkitSlugs] : undefined,
412 + },
413 + idempotent
414 + )
415 ).pipe(Effect.flatMap(decode(TriggerTypes)), Effect.map(sortBySlug)),
416 /**
417 * Validates that the given toolkit slugs are valid by comparing them against the list
@@ -504,13 +514,13 @@ const makeComposioSessionRepository = Effect.gen(function* () {
514 */
515 getSession: (session: { id: string }) =>
516 client.pipe(
507 - Effect.flatMap(c => c.cli.getSession({ id: session.id })),
517 + Effect.flatMap(c => c.cli.getSession({ id: session.id }, idempotent)),
518 Effect.flatMap(decode(RetrievedSession))
519 ),
520 getRealtimeCredentials: () =>
511 - Effect.flatMap(client, c => c.cli.realtime.credentials(validated)),
521 + Effect.flatMap(client, c => c.cli.realtime.credentials({ ...idempotent, ...validated })),
522 authRealtimeChannel: (params: { channel_name: string; socket_id: string }) =>
513 - Effect.flatMap(client, c => c.cli.realtime.auth(params)),
523 + Effect.flatMap(client, c => c.cli.realtime.auth(params, idempotent)),
524 };
525 });
526
ts/packages/cli/src/services/composio-request.ts
+17 -2
@@ -5,6 +5,22 @@ import { HttpDecodingError } from './composio-errors';
5 // Maximum concurrent requests per each endpoint
6 export const MAX_CONCURRENT_REQUESTS_PER_ENDPOINT = 4;
7
8 +/**
9 + * The client's default number of retries, which the CLI's clients turn off.
10 + */
11 +const DEFAULT_MAX_RETRIES = 2;
12 +
13 +/**
14 + * Request options for a call that is safe to repeat: a read, or a call whose
15 + * repeat changes nothing on the server. The client retries such a call after a
16 + * connection failure, a timeout, or a 408, 409, 429 or 5xx response.
17 + *
18 + * Every other call is sent once. A create, an update, a delete, or an execution
19 + * that failed in flight may still have taken effect, and repeating it could
20 + * repeat its side effect.
21 + */
22 +export const idempotent = { maxRetries: DEFAULT_MAX_RETRIES } satisfies RequestOptions;
23 +
24 /**
25 * Request options that make the client validate the response against its own
26 * schema for the operation, so a response in an unexpected shape fails the
@@ -13,8 +29,7 @@ export const MAX_CONCURRENT_REQUESTS_PER_ENDPOINT = 4;
29 * Pass it on a read whose fields the command uses through the client's types.
30 * Leave it off when the CLI decodes the response with a schema it owns, when
31 * the endpoint is read leniently on purpose, and on every call that is not a
16 - * read: failing after the server acted would invite a retry that repeats the
17 - * side effect.
32 + * read. Combine it with {@link idempotent}: `{ ...idempotent, ...validated }`.
33 */
34 export const validated = { decode: true } satisfies RequestOptions;
35
ts/packages/cli/src/services/composio-resource-operations.ts
+46 -34
@@ -15,7 +15,7 @@ import { Toolkits, type ToolkitSearchResult } from 'src/models/toolkits';
15 import { TriggerInstanceItems } from 'src/models/triggers';
16 import { TriggerType } from 'src/models/trigger-types';
17 import type { ClientError } from './composio-errors';
18 -import { decode } from './composio-request';
18 +import { decode, idempotent } from './composio-request';
19
20 /**
21 * Auth config, connected account, and trigger instance operations: single-page
@@ -34,12 +34,15 @@ export const resourceOperations = (client: Effect.Effect<ComposioClientShape, Cl
34 }) =>
35 client.pipe(
36 Effect.flatMap(c =>
37 - c.authConfigs.list({
38 - search: params.search,
39 - toolkit_slug: params.toolkit_slug,
40 - limit: params.limit,
41 - show_disabled: params.show_disabled ?? true,
42 - })
37 + c.authConfigs.list(
38 + {
39 + search: params.search,
40 + toolkit_slug: params.toolkit_slug,
41 + limit: params.limit,
42 + show_disabled: params.show_disabled ?? true,
43 + },
44 + idempotent
45 + )
46 ),
47 Effect.flatMap(decode(AuthConfigListResponse))
48 ),
@@ -49,7 +52,7 @@ export const resourceOperations = (client: Effect.Effect<ComposioClientShape, Cl
52 */
53 getAuthConfig: (nanoid: string) =>
54 client.pipe(
52 - Effect.flatMap(c => c.authConfigs.retrieve(nanoid)),
55 + Effect.flatMap(c => c.authConfigs.retrieve(nanoid, idempotent)),
56 Effect.flatMap(decode(AuthConfigItem))
57 ),
58 /**
@@ -67,7 +70,7 @@ export const resourceOperations = (client: Effect.Effect<ComposioClientShape, Cl
70 */
71 getConnectedAccount: (nanoid: string) =>
72 client.pipe(
70 - Effect.flatMap(c => c.connectedAccounts.retrieve(nanoid)),
73 + Effect.flatMap(c => c.connectedAccounts.retrieve(nanoid, idempotent)),
74 Effect.flatMap(decode(ConnectedAccountItem))
75 ),
76 /**
@@ -77,15 +80,18 @@ export const resourceOperations = (client: Effect.Effect<ComposioClientShape, Cl
80 listActiveTriggers: (params: TriggerInstanceListActiveParams) =>
81 client.pipe(
82 Effect.flatMap(c =>
80 - c.triggerInstances.listActive({
81 - user_ids: params.user_ids,
82 - connected_account_ids: params.connected_account_ids,
83 - auth_config_ids: params.auth_config_ids,
84 - trigger_ids: params.trigger_ids,
85 - trigger_names: params.trigger_names,
86 - show_disabled: params.show_disabled,
87 - limit: params.limit,
88 - })
83 + c.triggerInstances.listActive(
84 + {
85 + user_ids: params.user_ids,
86 + connected_account_ids: params.connected_account_ids,
87 + auth_config_ids: params.auth_config_ids,
88 + trigger_ids: params.trigger_ids,
89 + trigger_names: params.trigger_names,
90 + show_disabled: params.show_disabled,
91 + limit: params.limit,
92 + },
93 + idempotent
94 + )
95 ),
96 Effect.flatMap(response =>
97 decode(TriggerInstanceItems)(response.items).pipe(
@@ -126,12 +132,15 @@ export const lookupOperations = (client: Effect.Effect<ComposioClientShape, Clie
132 cursor?: string;
133 }): Effect.Effect<ToolkitSearchResult, ClientError> =>
134 Effect.flatMap(client, c =>
129 - c.toolkits.list({
130 - search: params.search,
131 - category: params.category,
132 - limit: params.limit,
133 - cursor: params.cursor,
134 - })
135 + c.toolkits.list(
136 + {
137 + search: params.search,
138 + category: params.category,
139 + limit: params.limit,
140 + cursor: params.cursor,
141 + },
142 + idempotent
143 + )
144 ).pipe(
145 Effect.flatMap(response =>
146 decode(Toolkits)(response.items).pipe(
@@ -163,14 +172,17 @@ export const lookupOperations = (client: Effect.Effect<ComposioClientShape, Clie
172 cursor?: string;
173 }) =>
174 Effect.flatMap(client, c =>
166 - c.tools.list({
167 - search: params.search,
168 - toolkit_slug: params.toolkit_slug,
169 - tags: params.tags ? params.tags.split(',').map(t => t.trim()) : undefined,
170 - limit: params.limit,
171 - cursor: params.cursor,
172 - toolkit_versions: 'latest',
173 - })
175 + c.tools.list(
176 + {
177 + search: params.search,
178 + toolkit_slug: params.toolkit_slug,
179 + tags: params.tags ? params.tags.split(',').map(t => t.trim()) : undefined,
180 + limit: params.limit,
181 + cursor: params.cursor,
182 + toolkit_versions: 'latest',
183 + },
184 + idempotent
185 + )
186 ),
187 /**
188 * Retrieves detailed info about a single tool by slug.
@@ -181,14 +193,14 @@ export const lookupOperations = (client: Effect.Effect<ComposioClientShape, Clie
193 * in a field it never reads.
194 */
195 getToolDetailed: (slug: string) =>
184 - Effect.flatMap(client, c => c.tools.retrieve(slug, { toolkit_versions: 'latest' })),
196 + Effect.flatMap(client, c => c.tools.retrieve(slug, { toolkit_versions: 'latest' }, idempotent)),
197 /**
198 * Retrieves detailed info about a single trigger type by slug.
199 * @param slug - Trigger type slug (e.g. "GMAIL_NEW_GMAIL_MESSAGE")
200 */
201 getTriggerTypeDetailed: (slug: string) =>
202 client.pipe(
191 - Effect.flatMap(c => c.triggersTypes.retrieve(slug)),
203 + Effect.flatMap(c => c.triggersTypes.retrieve(slug, undefined, idempotent)),
204 Effect.flatMap(decode(TriggerType))
205 ),
206 });
ts/packages/cli/src/services/connected-account-selection.ts
+18 -11
@@ -4,6 +4,7 @@ import type { ConnectedAccountItem } from 'src/models/connected-accounts';
4 import { decodeConnectedAccountItems } from 'src/effects/decode-connected-account-list';
5 import type { TerminalUI } from 'src/services/terminal-ui';
6 import { memoizeInProcess } from 'src/utils/memoize-in-process';
7 +import { idempotent } from 'src/services/composio-request';
8
9 // `status` is an open enum, so this is shape-identical to
10 // `ConnectedAccountItem`; tool-router rows normalize statuses outside the
@@ -173,11 +174,14 @@ export const listActiveConnectedAccounts = memoizeInProcess({
174 keyOf: (input: { readonly client: ComposioClientShape; readonly userId: string }) =>
175 `${clientId(input.client)}\u0000${input.userId}`,
176 make: ({ client, userId }) =>
176 - client.connectedAccounts.list({
177 - user_ids: [userId],
178 - statuses: ['ACTIVE'],
179 - limit: ACTIVE_ACCOUNTS_PAGE_SIZE,
180 - }),
177 + client.connectedAccounts.list(
178 + {
179 + user_ids: [userId],
180 + statuses: ['ACTIVE'],
181 + limit: ACTIVE_ACCOUNTS_PAGE_SIZE,
182 + },
183 + idempotent
184 + ),
185 });
186
187 const TOOLKIT_ACCOUNTS_PAGE_SIZE = 100;
@@ -191,12 +195,15 @@ const fetchConnectedAccountsForToolkit = (params: {
195 readonly toolkitSlug: string;
196 }) =>
197 params.client.connectedAccounts
194 - .list({
195 - toolkit_slugs: [params.toolkitSlug],
196 - user_ids: [params.userId],
197 - statuses: ['ACTIVE'],
198 - limit: TOOLKIT_ACCOUNTS_PAGE_SIZE,
199 - })
198 + .list(
199 + {
200 + toolkit_slugs: [params.toolkitSlug],
201 + user_ids: [params.userId],
202 + statuses: ['ACTIVE'],
203 + limit: TOOLKIT_ACCOUNTS_PAGE_SIZE,
204 + },
205 + idempotent
206 + )
207 .pipe(Effect.map(response => response.items));
208
209 /**
ts/packages/cli/src/services/tool-file-uploads.ts
+12 -7
@@ -8,6 +8,7 @@ import type * as Path from 'effect/Path';
8 import type { ComposioClientShape, ComposioError } from '@composio/client/effect';
9 import { Data, Effect, Predicate } from 'effect';
10 import { guessToolkitFromToolSlug } from 'src/utils/toolkit-from-tool-slug';
11 +import { idempotent } from 'src/services/composio-request';
12
13 type JsonSchema = Record<string, unknown>;
14
@@ -283,13 +284,17 @@ const uploadFile = (params: {
284 // eslint-disable-next-line no-restricted-imports -- MD5 for the presigned-upload checksum is not available in Web Crypto
285 const { createHash } = yield* Effect.promise(() => import('node:crypto'));
286 const md5 = createHash('md5').update(fileData.bytes).digest('hex');
286 - const presigned = yield* params.client.files.createPresignedURL({
287 - filename: fileData.fileName,
288 - mimetype: fileData.mimeType,
289 - md5,
290 - tool_slug: params.toolSlug,
291 - toolkit_slug: params.toolkitSlug,
292 - });
287 + // Issuing an upload URL changes nothing until the upload, so a retry is safe.
288 + const presigned = yield* params.client.files.createPresignedURL(
289 + {
290 + filename: fileData.fileName,
291 + mimetype: fileData.mimeType,
292 + md5,
293 + tool_slug: params.toolSlug,
294 + toolkit_slug: params.toolkitSlug,
295 + },
296 + idempotent
297 + );
298
299 const { ssrfSafeFetch } = yield* loadSsrfSafeFetch;
300 const uploadResponse = yield* Effect.tryPromise({
ts/packages/cli/src/services/tool-permissions.ts
+6 -1
@@ -25,6 +25,7 @@ import {
25 import { ComposioClientSingleton } from 'src/services/composio-clients';
26 import { type ClientError } from 'src/services/composio-errors';
27 import { ComposioUserContext } from 'src/services/user-context';
28 +import { idempotent } from 'src/services/composio-request';
29
30 export const ENHANCED_LINK_URL_OVERWRITE = 'https://connect.composio.dev/enhanced';
31
@@ -323,7 +324,11 @@ const requestJson = <S extends Schema.ConstraintDecoder<unknown>>(
324 readonly message: string;
325 }
326 ): Effect.Effect<S['Type'], ToolPermissionsRequestError> =>
326 - (method === 'GET' ? client.get(path) : client.post(path, { body })).pipe(
327 + // Both endpoints are reads, including the POST that resolves permissions.
328 + (method === 'GET'
329 + ? client.get(path, idempotent)
330 + : client.post(path, { body, ...idempotent })
331 + ).pipe(
332 Effect.flatMap(Schema.decodeUnknownEffect(responseSchema)),
333 Effect.mapError(cause => new ToolPermissionsRequestError({ path, message, cause }))
334 );
ts/packages/cli/src/services/tool-router-session-connections.ts
+10 -6
@@ -8,6 +8,7 @@ import {
8 resolveDefaultConnectedAccountsByToolkit,
9 type SelectableConnectedAccount,
10 } from 'src/services/connected-account-selection';
11 +import { idempotent } from 'src/services/composio-request';
12
13 type RawConnectedAccount = {
14 readonly id: string;
@@ -57,12 +58,15 @@ const listConnectedAccountsForToolkits = (
58 userId: string,
59 toolkits: ReadonlyArray<string>
60 ) =>
60 - client.connectedAccounts.list({
61 - user_ids: [userId],
62 - statuses: ['ACTIVE'],
63 - toolkit_slugs: [...toolkits],
64 - limit: 1000,
65 - });
61 + client.connectedAccounts.list(
62 + {
63 + user_ids: [userId],
64 + statuses: ['ACTIVE'],
65 + toolkit_slugs: [...toolkits],
66 + limit: 1000,
67 + },
68 + idempotent
69 + );
70
71 export const resolveToolRouterSessionConnections = (
72 client: ComposioClientShape,
ts/packages/cli/src/services/tools-executor.ts
+9 -11
@@ -252,22 +252,20 @@ export const ToolsExecutorLive = Layer.effect(
252 })
253 );
254
255 - // Never retry an execution: a retry after the backend already acted
255 + // An execution is not `idempotent`, so it is sent once: a retry after the backend already acted
256 // duplicates the side effect (e.g. sends the same email twice). The
257 // response is not validated either, for the same reason.
258 const raw: SessionExecuteResponse | SessionExecuteMetaResponse = yield* isMetaToolSlug(
259 slug
260 )
261 - ? resolvedClient.toolRouter.session.executeMeta(
262 - sessionId,
263 - { slug, arguments: normalizedArguments },
264 - { maxRetries: 0 }
265 - )
266 - : resolvedClient.toolRouter.session.execute(
267 - sessionId,
268 - { tool_slug: slug, arguments: normalizedArguments },
269 - { maxRetries: 0 }
270 - );
261 + ? resolvedClient.toolRouter.session.executeMeta(sessionId, {
262 + slug,
263 + arguments: normalizedArguments,
264 + })
265 + : resolvedClient.toolRouter.session.execute(sessionId, {
266 + tool_slug: slug,
267 + arguments: normalizedArguments,
268 + });
269 if (raw.result_type === 'input_required') {
270 return yield* toolInputRequiredError(`Tool ${slug}`, raw);
271 }
ts/packages/cli/src/services/triggers-realtime.ts
+3 -3
@@ -15,7 +15,7 @@ import type {
15 RealtimeCredentialsResponse as CliRealtimeCredentialsResponse,
16 } from '@composio/client/resources/cli';
17 import { ComposioClientSingleton, ComposioSessionRepository } from 'src/services/composio-clients';
18 -import { validated } from 'src/services/composio-request';
18 +import { idempotent, validated } from 'src/services/composio-request';
19
20 const RawRealtimeEvent = JsonRecordSchema;
21 type RawRealtimeEvent = typeof RawRealtimeEvent.Type;
@@ -298,11 +298,11 @@ const makeTriggersRealtime = Effect.gen(function* () {
298 return yield* listenWith({
299 getRealtimeCredentials: () =>
300 client.cli.realtime
301 - .credentials(validated)
301 + .credentials({ ...idempotent, ...validated })
302 .pipe(Effect.mapError(subscriptionError('Failed to fetch realtime credentials'))),
303 authRealtimeChannel: params =>
304 client.cli.realtime
305 - .auth(params)
305 + .auth(params, idempotent)
306 .pipe(Effect.mapError(subscriptionError('Failed to authorize the realtime channel'))),
307 onEvent,
308 });
ts/packages/cli/src/utils/optional-params.ts
+10 -1
@@ -1,4 +1,4 @@
1 -import { Option, Record as EffectRecord } from 'effect';
1 +import { Array as Arr, Option, Record as EffectRecord } from 'effect';
2
3 /**
4 * The request parameters a record of optional flags turns into: each present
@@ -19,3 +19,12 @@ export type OptionalParams<R extends Readonly<Record<string, Option.Option<unkno
19 export const optionalParams = <const R extends Readonly<Record<string, Option.Option<unknown>>>>(
20 flags: R
21 ): OptionalParams<R> => EffectRecord.map(flags, Option.getOrUndefined) as OptionalParams<R>;
22 +
23 +/**
24 + * The request parameter a list of filters turns into: the list when it has
25 + * elements and `undefined` when it is empty, so an unused filter is left out of
26 + * the request instead of being sent as `[]`.
27 + */
28 +export const nonEmptyOrUndefined = <const T extends ReadonlyArray<unknown>>(
29 + values: T
30 +): T | undefined => (Arr.isReadonlyArrayNonEmpty(values) ? values : undefined);
ts/packages/cli/test/__utils__/services/stub-composio-client.ts
+1
@@ -46,6 +46,7 @@ export const stubComposioClient = (
46 orgApiKey: null,
47 baseURL: 'https://stub.invalid',
48 deprecationNotices: 'off',
49 + maxRetries: 0,
50 schemas: schemaLoaders,
51 },
52 httpClient
ts/packages/cli/test/src/commands/proxy.cmd.test.ts
+4 -3
@@ -189,9 +189,10 @@ describe('CLI: composio proxy', () => {
189 ],
190 },
191 });
192 - // A proxied call is never retried: a retry after the upstream API
193 - // already acted would duplicate the side effect.
194 - expect(proxyOptions).toEqual({ maxRetries: 0 });
192 + // A proxied call does not ask for retries, so the client sends it
193 + // once: a retry after the upstream API already acted would duplicate
194 + // the side effect.
195 + expect(proxyOptions).toBeUndefined();
196
197 expect(output).toContain('Status: 200');
198 expect(output).toContain('"ok": true');
ts/packages/cli/test/src/commands/tools/tools.execute.cmd.test.ts
+5 -4
@@ -432,9 +432,10 @@ describe('CLI: composio execute', () => {
432 expect(recordedSessionCreateParams[0]?.connected_accounts).toEqual({
433 gmail: 'con_gmail_default',
434 });
435 - // An execution is never retried, and its response never validated: a
436 - // retry after the backend already acted would duplicate the side effect.
437 - expect(recordedExecuteOptions.at(-1)).toEqual({ maxRetries: 0 });
435 + // An execution does not ask for retries or validation, so the client
436 + // sends it once: a retry after the backend already acted would
437 + // duplicate the side effect.
438 + expect(recordedExecuteOptions.at(-1)).toBeUndefined();
439 })
440 );
441
@@ -442,7 +443,7 @@ describe('CLI: composio execute', () => {
443 Effect.gen(function* () {
444 yield* cli(['execute', 'COMPOSIO_SEARCH_TOOLS', '-d', '{"query":"email"}']);
445
445 - expect(recordedExecuteOptions).toEqual([{ maxRetries: 0 }]);
446 + expect(recordedExecuteOptions).toEqual([undefined]);
447 })
448 );
449
ts/packages/cli/test/src/services/composio-client-singleton.test.ts
+24 -2
@@ -9,7 +9,7 @@ import { FetchHttpClient } from 'effect/http';
9 import * as tempy from 'tempy';
10 import type { ComposioClientShape } from '@composio/client/effect';
11 import { ComposioClientSingleton } from 'src/services/composio-clients';
12 -import { validated } from 'src/services/composio-request';
12 +import { idempotent, validated } from 'src/services/composio-request';
13 import { APP_VERSION } from 'src/constants';
14 import { defaultNodeOs, NodeOs } from 'src/services/node-os';
15
@@ -525,11 +525,33 @@ describe('ComposioClientSingleton client', () => {
525 const clientSingleton = yield* ComposioClientSingleton;
526 const client = yield* clientSingleton.get();
527
528 - expect(yield* client.tools.retrieveEnum()).toEqual(['GITHUB_CREATE_ISSUE']);
528 + expect(yield* client.tools.retrieveEnum(idempotent)).toEqual(['GITHUB_CREATE_ISSUE']);
529 expect((yield* clientSingleton.getMetrics()).requests).toBe(2);
530 }).pipe(Effect.provide(singletonLayer(baseConfig([['COMPOSIO_USER_API_KEY', 'uak_retry']]))));
531 });
532
533 + it.effect('sends a call that is not marked idempotent once', () => {
534 + const fetchSpy = vi.spyOn(globalThis, 'fetch').mockImplementation(() =>
535 + Promise.resolve(
536 + new Response('{}', {
537 + status: 500,
538 + headers: { 'Content-Type': 'application/json', 'retry-after-ms': '0' },
539 + })
540 + )
541 + );
542 +
543 + return Effect.gen(function* () {
544 + const clientSingleton = yield* ComposioClientSingleton;
545 + const client = yield* clientSingleton.get();
546 + const error = yield* Effect.flip(
547 + client.triggerInstances.manage.update('ti_1', { status: 'enable' })
548 + );
549 +
550 + expect(error._tag).toBe('@composio/client/InternalServerError');
551 + expect(fetchSpy).toHaveBeenCalledTimes(1);
552 + }).pipe(Effect.provide(singletonLayer(baseConfig([['COMPOSIO_USER_API_KEY', 'uak_once']]))));
553 + });
554 +
555 it.effect('counts requests when the application already provides a fetch transport', () => {
556 vi.spyOn(globalThis, 'fetch').mockResolvedValue(okResponse());
557
ts/packages/cli/test/src/utils/optional-params.test.ts
+11 -1
@@ -1,6 +1,6 @@
1 import { describe, expect, it } from '@effect/vitest';
2 import { Option } from 'effect';
3 -import { optionalParams } from 'src/utils/optional-params';
3 +import { nonEmptyOrUndefined, optionalParams } from 'src/utils/optional-params';
4
5 describe('optionalParams', () => {
6 it('keeps the value of a present flag and turns an absent one into undefined', () => {
@@ -18,3 +18,13 @@ describe('optionalParams', () => {
18 expect(optionalParams({})).toEqual({});
19 });
20 });
21 +
22 +describe('nonEmptyOrUndefined', () => {
23 + it('keeps a list that has elements', () => {
24 + expect(nonEmptyOrUndefined(['a', 'b'])).toEqual(['a', 'b']);
25 + });
26 +
27 + it('turns an empty list into undefined', () => {
28 + expect(nonEmptyOrUndefined([])).toBeUndefined();
29 + });
30 +});
ts/packages/cli/tsconfig.options.json
+4 -1
@@ -16,7 +16,10 @@
16 "noUnusedParameters": false,
17 "plugins": [
18 {
19 - "name": "@effect/language-service"
19 + "name": "@effect/language-service",
20 + "diagnosticSeverity": {
21 + "floatingEffect": "error"
22 + }
23 }
24 ]
25 },