@devries-cameron20 / composio Mirror · read-only / commits / 09e2fb7fa
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, 08:30 UTC

feat(cli): route consumer `composio execute` through the Dashboard (#4754)

> [!NOTE] > The Dashboard endpoints (`POST /api/cli/trpc/execute`, `POST /api/cli/trpc/executeMeta`) are live in production since ComposioHQ/dashboard#1769 merged on 2026-10-16. A merge to `next` publishes a rolling CLI beta that uses them for every consumer `composio execute`. This PR: - is the CLI side of the change. The backend side merged on 2026-10-15 (ComposioHQ/platform#13603, #13849, #13855, private repos) and the Dashboard side on 2026-10-16 (ComposioHQ/dashboard#1769) - includes the refactor from #4849 unchanged (the session-context split and the named executor stages); #4849 was closed in favor of this PR - picks the transport from the command surface: root `execute` (the consumer surface, which `composio run` also spawns) goes to the Dashboard, `composio dev playground-execute` stays on the backend - sends the execution step of consumer `composio execute` (single, `--parallel`, meta slugs) to the Composio Dashboard through a new `DashboardToolExecution` service; the CLI creates no Tool Router session on this path, the Dashboard creates and owns it - sends the connected account the CLI resolved with each `execute` call, since the session can no longer be pinned from the CLI - keeps connection lookups, the permission gate and file uploads on the backend client, and keeps `composio dev playground-execute`, developer projects and every other command on the backend - keeps the backend transport when `COMPOSIO_BASE_URL` is overridden and `COMPOSIO_WEB_URL` is not, so a key for one deployment is never sent to another deployment's Dashboard; `composio run` stops forwarding a web URL to its child CLI in that case - fails a relayed backend failure with the same `@composio/client/effect` status error the backend transport fails with (`@composio/client/PermissionDeniedError`, …), so error mapping, connection tips, `--json` output and telemetry take the existing path - sends each execution once: no retry, `redirect: 'error'`, one 15-minute deadline, no fallback to the backend, and fiber interruption aborts the request - names the Dashboard in `ts/packages/cli/AGENTS.md` as the one first-party host called without `@composio/client`, and adds two notes to the CLI changelog (no changeset: `@composio/cli` is ignored by Changesets) Ticket: PLEN-4353. ## Context ComposioHQ/platform#13603 first refused Instant (Composio-managed credential) executions on consumer projects unless they came through the Dashboard (error `6606`). ComposioHQ/platform#13988, deployed on 2026-10-17, removed that check: any caller with a user API key, including released CLIs, runs Instant tools again, metered and wallet-checked as before. So this PR is not needed to unblock Instant tools; it moves consumer execution onto the Dashboard, which owns the consumer session, its tool policy and the Instant charge. Once this CLI is released and the Dashboard path has proven itself, Apollo can redirect the older CLIs' direct calls. #4774, written to record a 0.4.3 entry, was closed: 0.4.3 shipped without this change, and its changelog entries sit under `## Unreleased` here. ## User-visible behavior None for standard tools: the same output, exit codes, error details, connection tips and `--json` shapes. No new login or configuration. These differences exist by design: - a Dashboard-level failure (unreachable, non-JSON reply, rejected credentials, backend outage) prints the Dashboard's message and has no "Error details" note; rejected credentials suggest `composio login` - there is no fallback to the backend when the Dashboard is unreachable - a `COMPOSIO_WEB_URL` that is not `https://` (or `http://` on localhost), or that carries credentials, a query string or a fragment, makes `composio execute` fail before anything is sent - a request with no complete answer after 15 minutes fails with a message that the tool may still have run - the session is the Dashboard's, not the CLI's: the CLI's auth-config choices and the enhanced link URL are no longer applied on this path, and the account selection travels with each call instead of being pinned on the session ## Contract the CLI speaks Request, for `execute` and `executeMeta` under `<COMPOSIO_WEB_URL>/api/cli/trpc/`: ``` POST /api/cli/trpc/execute Authorization: Bearer <user API key> x-org-id: <org id> content-type: application/json { "json": { "tool_slug": string, "arguments": object, "account"?: string } } ``` `executeMeta` takes `"slug"` in place of `"tool_slug"` and has no `account`. The CLI sends no session settings and no user id. `account` is the connected account the CLI resolved for the tool's toolkit: the `--account` selection (alias, word id or id), else the account aliased `default`, else the newest. It is sent only when there is one. Response 200: ``` { "result": { "data": { "json": { "ok": true, "response": { "result_type": "completed" | "failed" | "input_required", ... } } | { "ok": false, "status": number, "error": { "message": string, "code"?: number, "slug"?: string, "status"?: number, "request_id"?: string, "suggested_fix"?: string } } } } } ``` `response` is the backend's execute answer, relayed as is (`data`, `error`, `log_id`, and `instant_charge` when the Dashboard's session asked for it). The CLI checks only `result_type`, the same as the backend transport: the tool has already run, so a missing field does not turn its result into an error. Dashboard-level failures use the tRPC error envelope `{ "error": { "json": { "message": string, "data": { "code": string, "httpStatus": number } } } }`. The envelopes are decoded with `effect/Schema`. The Instant charge line (#4857) appears on this path because the Dashboard creates its sessions with `instant: { return_instant_charge: true }` (dashboard#1769); the production run below shows it. ## Security hardening - **Clear-text key.** The Dashboard base URL is validated before the user API key is attached: `https:` is required, and `http:` is accepted only for `localhost`, `127.0.0.1` and `[::1]`. A violation fails with a fixed message and sends nothing. - **Secrets in the URL.** A URL with userinfo, a query string or a fragment is rejected by the same check. The message never echoes the configured value, and the remaining transport, response and timeout messages name only the origin. - **Deadline.** One 15-minute timeout (`DASHBOARD_REQUEST_TIMEOUT`) covers the POST and the body read. Expiry aborts the request, does not resend it, and tells the user the tool may still have run and to check before running it again. - **Stored URLs.** `base_url` and `web_url` in `~/.composio/user_data.json` never take effect: the backend client, the Dashboard request and the transport choice all read the environment (or the defaults), so they cannot disagree. Four command-level tests pin this; no production change was needed. ## Analytics While telemetry is enabled, the two Dashboard execution requests also carry `x-cli-install-id: <install_id>`: the random UUID already stored in `~/.composio/analytics.json`, sent verbatim, so Dashboard-side product analytics can be joined with the CLI's own events. The header is gated by the same predicate as the CLI's own events, so it is omitted under `COMPOSIO_CLI_TELEMETRY_DISABLED`, `TELEMETRY_DISABLED`, `COMPOSIO_DISABLE_TELEMETRY`, `CI`, or `NODE_ENV=test`. It is also omitted when no install ID is stored or the state file is unreadable; the read never creates an ID and never fails or delays an execution. It is not sent to the backend API and is not part of `cliRequestHeaders()`. ## How Has This Been Tested? ### Automated On the current head (`def340f14`, rebased onto `next` on 2026-10-19, `@composio/client` rc.19), Node 24.17.0, re-run on 2026-10-20: - `pnpm test` in `ts/packages/cli`: 162 files, 1966 passed, 1 skipped; `validate:command-tests` reports 15 exemptions (unchanged) - `pnpm typecheck` from the repo root: 14/14 tasks - `pnpm --filter @composio/cli run lint:effect`: 0 diagnostics; oxlint and prettier clean on the touched files Rebasing onto `next` rebuilt the transport on the Effect client: the old promise client's `APIError.generate` and `maxRetries: 0` are gone (the Effect client sends an execution once unless the call passes `idempotent`), and the tests' `for` loops became `describe.each`, per the oxlint ban from #4897. #4849's ordering test now runs on both transports: the backend creates the session before the permission gate, and a denied tool reaches neither the backend nor the Dashboard. New tests: - `test/src/services/dashboard-tool-execution.test.ts` (38): URL, headers and body for both procedures; trailing-slash normalization; the client's status error from an `ok: false` reply; tRPC `UNAUTHORIZED` and other Dashboard errors; network failure and non-JSON reply name the Dashboard and send one request; `redirect: 'error'` reaches `fetch`; interruption aborts the in-flight request; the transport choice for each URL-override combination; rejected URLs (remote `http`, userinfo, query, fragment, other scheme, unparseable) send nothing and keep the value out of the message; loopback `http` is allowed; the deadline fires under the test clock with one request, aborted; the install ID header is present on both procedures with telemetry on, absent for each opt-out, absent with no stored ID (and none is created), and an unreadable analytics state does not break execution - `test/src/commands/tools/tools.execute.transport.test.ts` (26), through the real command and executor: one POST and no backend session for a standard tool, with printed lines equal to the backend transport's; meta slug; three parallel tools with one failing; `dev playground-execute` on the backend; project resolution and file upload before the Dashboard call; a denied permission gate sends nothing; a relayed 402 prints the same lines as the backend transport; the `composio link` tip for code 4302; `composio login` hint; no resend and no backend fallback on transport failure; both URL-override cases; stored URLs cannot split the transport; an unsafe web URL sends nothing and the secret appears in neither the printed lines nor the JSON summary; a timeout is reported once with no backend call - `test/src/commands/run.cmd.test.ts`: the web URL is forwarded to run helpers only when this process uses the Dashboard transport `test/__utils__/layers/dashboard.ts` adds a test Dashboard (`DashboardToolExecutionTest`, wired into `TestLive`) that runs the real service over a stub `HttpClient`, rejects any `session` key, forwards `account` to the mock Tool Router's execute call, and relays its answers, so the existing execute tests exercise the new request and reply decoding. The account-selection tests in `tools.execute.cmd.test.ts` now assert the `account` sent with the call. Earlier local runs, before the rebase onto `next`: - On an earlier commit of this branch, Node 24.17.0, all passing: `pnpm typecheck` from the repo root (14/14 tasks); `pnpm test` in `ts/packages/cli` (`validate:skills`, `validate:boundaries`, `vitest run`) with 134 files, 1513 passed, 1 skipped and `lint boundaries OK: 4 registered disables` (no new `eslint-disable`); `oxlint ts/packages/cli` with no errors. - On `e15273a3a`, Node 22.23.2 (so with an engine warning): `tsc --noEmit` clean, and `pnpm test` in `ts/packages/cli` with 134 files, 1513 passed, 1 skipped, 1 failed. The failure is `analytics.dispatch.test.ts > uses one install_id across concurrent first-run events`, which passes when run alone (4 of 4 runs, with and without this change) and failed only inside the full run. ### Production run, 2026-10-20 CLI from this branch at `def340f14` (run from source with Bun, no URL overrides), against the production Dashboard (dashboard#1769) and production Apollo, which runs platform#13988. Existing login, consumer project of the `composio_team` org. A fetch trace confirmed the execution is a single `POST https://dashboard.composio.dev/api/cli/trpc/execute`, after the backend calls for project resolve, connected accounts, the tool version and consumer config, and with no Tool Router session created by the CLI. | Case | Result | |---|---| | `COMPOSIO_SEARCH_TOOLS` (meta, `executeMeta`) | `successful: true` (5 s) | | `TELEGRAM_GET_ME --account first`, then `second` | `successful: true`, a different bot each time | | `TELEGRAM_GET_ME --account nope` | "No connected account matched", both accounts listed, exit 1, no request | | `GITHUB_GET_THE_AUTHENTICATED_USER` | `successful: true` | | `EXA_SEARCH` (Instant) | `successful: true` with `instantCharge: { amount: "0.007000", currency: "USD", chargedBy: "composio" }` | | `--parallel TELEGRAM_GET_ME EXA_SEARCH` | both `successful: true`, the charge on the Exa result | `--account` ownership, sent straight to the production Dashboard route (the CLI itself refuses an id it did not list), `SLACK_FETCH_TEAM_INFO` in `composio_team`: | `account` | Result | |---|---| | the caller's own Slack account in this org | `ok: true`, the team info | | the same user's Slack account in another org (`alberto_workspace`) | `ok: false`, 400 `ToolRouterV2_BadRequest` (4300) "No account found matching …" | | a made-up `ca_*` id | the same 400, so the reply does not reveal whether the id exists | Not run: another member's account in the same org, and `composio run` (needs the compiled binary). ### Live run, 2026-10-16 This run predates ComposioHQ/platform#13988: the two `ManagedCredential_DashboardOriginRequired` rows no longer apply, since that check is gone. CLI built from this branch at `7cfb86de8` (run from source with Bun), against the dashboard#1769 preview at `dda5f1bdf` (`dashboard-git-feat-cli-execute-via-dashboard.preview.composio.dev`) and staging Apollo, which runs platform#13603, #13849 and #13855. Fresh browser-approved login, consumer project of org `ok_ZZCb3s6ETg_7`. "0.4.5" is the released binary, calling Apollo directly with the same key. | Case | Result | |---|---| | `COMPOSIO_SEARCH_TOOLS` (meta, `executeMeta`) | `successful: true` (20 s on the idle preview) | | `GMAIL_FETCH_EMAILS`, no connection | `ToolRouterV2_NoActiveConnection` with the `composio link gmail` tip | | `TELEGRAM_GET_ME --account first`, then `second` | `successful: true`, a different bot each time | | `TELEGRAM_GET_ME --account nope` | "No connected account matched", both accounts listed, exit 1, no request | | `EXA_SEARCH`, Instant off | `ToolRouterV2_NoActiveConnection` (same on 0.4.5) | | `EXA_SEARCH`, Instant on | `successful: true` with `instantCharge: { amount: "0.007000", currency: "USD", chargedBy: "composio" }` | | `--parallel EXA_SEARCH TELEGRAM_GET_ME`, Instant on | both `successful: true`, the charge on the Exa result | | Same build, only `COMPOSIO_BASE_URL` overridden (backend transport), `EXA_SEARCH`, Instant on | `ManagedCredential_DashboardOriginRequired`, the upgrade message | | 0.4.5 `TELEGRAM_GET_ME` | `successful: true` | | 0.4.5 `EXA_SEARCH`, Instant on | `ManagedCredential_DashboardOriginRequired`, the upgrade message | Instant was switched on for the Instant rows and off again afterwards. Not run: `composio run` (needs the compiled binary), Apollo 5xx and timeouts, production. ### Earlier live runs These predate the rebase onto `next` and platform#13988, so every refusal below is the since-removed origin check. Run by the author on 2026-10-07 with a build of this branch, against deployed previews of the Dashboard and backend PRs on staging data, after a fresh browser-approved login: - **Success.** A no-auth tool and a tool on Composio-managed credentials (`EXA_SEARCH`) succeeded. - **Backend errors.** A missing tool, a missing connection and a tool disabled for the session each printed the backend's message. - **`--account`.** With two connected Telegram accounts aliased `first` and `second`: `--account first` and `--account second`, the word id and the `ca_` id each reached the matching account. No `--account` picked the newest. An unknown selector failed in the CLI with "No connected account matched", exit code 1, before any request. - **Backend transport.** With `COMPOSIO_BASE_URL` set to the preview backend and `COMPOSIO_WEB_URL` unset, the CLI called the backend directly and was refused on the managed-credential tool with the upgrade message. - **Released CLIs.** 0.4.2, 0.2.32 and 0.2.31 were refused on the managed-credential tool and still worked on a standard tool. Repeated on 2026-10-08 with a build of the current head (`4ddc26698`), against fresh previews of the Dashboard and backend PRs: every case above gave the same result. Two additions: - **Blocked tool.** A tool on the Dashboard's blocked list printed the Dashboard's marketplace-policy message naming the CLI. - **Managed credentials switched off.** With Composio-managed credentials turned off for the project, the backend now answers a managed-credential tool with an error of its own, saying they are turned off and how to proceed. This branch and released 0.4.2 both printed it in full, because it does not arrive as a missing-connection error, which the CLI would replace with its `composio link` tip. A tool with no managed credential still got that tip. Earlier, on 2026-10-06 and 2026-10-07, the success and backend-error cases were also run against a locally running Dashboard branch and a preview backend on staging data. These runs did not cover production (see the production run above) or `composio run` from this branch (the source build cannot run it; only the compiled binary can). ## Known limits - A reply whose envelope deviates from the contract above, or whose `response` has no valid `result_type`, is reported as an unexpected response. - A failed execution (`"successful": false`, including the backend's refusal) exits 0 on this branch, as on released CLIs. Tracked in #4768, out of scope here. No VHS recording: the command surface and its output are unchanged.

Alberto Schiabel committed Oct 20, 2026 at 19:33 UTC 09e2fb7faa41c51fcd364ee01e0aaf6719a62896
18 files changed +2489 -184
ts/packages/cli/AGENTS.md
+2 -1
@@ -116,8 +116,9 @@ test/src/commands/<group>/<shared-module>.test.ts group helper
116 | `NodeOs` | OS abstraction (`homedir`, `platform`, `arch`) |
117 | `JsPackageManagerDetector` | Detects npm/pnpm/yarn/bun for install instructions |
118 | `UpgradeBinary` | Fetches latest release from GitHub, downloads and replaces binary |
119 +| `DashboardToolExecution` | Sends consumer `composio execute` calls to the Composio Dashboard and decodes its reply |
120
120 -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 the CLI's `identity` (`cliClientIdentity()` in `client-provenance.ts`, from which the client sends the provenance headers and which makes it refuse them in `defaultHeaders` and per-call `headers`), `auth` (`Auth.user` with the user key, `Auth.project` when a caller passes `projectApiKey`, `Auth.none()` before login), `scope` (the org and project, when both are known), `x-cli-session-id` as the one default header (read once per process), 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. The client's deprecation notices, which CLI users cannot act on, are dropped by the root logger (`withoutClientNotices`, `src/effects/without-client-notices.ts`, through the client's `isDeprecationNotice`). `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. Every `getFor` builds a new client, so never key a memo on a client instance: `clientScopeKey(client)` gives its auth mode, a SHA-256 fingerprint of its credential (never the key), org, and project (`listActiveConnectedAccounts` keys on it and the user id). While a client has `auth` and `scope`, a per-call `x-api-key`, `x-user-api-key`, `x-org-api-key`, `x-org-id` or `x-project-id` header fails the call before it is sent; an org or project known alone goes on an unscoped client's request, as `orgScopedOptions` and `getLatestToolVersion` do. Client errors are classified for display by `src/utils/api-error-extraction.ts`.
121 +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 the CLI's `identity` (`cliClientIdentity()` in `client-provenance.ts`, from which the client sends the provenance headers and which makes it refuse them in `defaultHeaders` and per-call `headers`), `auth` (`Auth.user` with the user key, `Auth.project` when a caller passes `projectApiKey`, `Auth.none()` before login), `scope` (the org and project, when both are known), `x-cli-session-id` as the one default header (read once per process), 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. The client's deprecation notices, which CLI users cannot act on, are dropped by the root logger (`withoutClientNotices`, `src/effects/without-client-notices.ts`, through the client's `isDeprecationNotice`). `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.) The Composio Dashboard (`ComposioUserContext.data.webURL`) is the one first-party host the CLI calls without `@composio/client`: `src/services/dashboard-tool-execution.ts` sends consumer `composio execute` calls (tool and meta-tool executions) to it over `HttpClient` from `effect/http`, once, with no retry, no redirect following and one 15-minute deadline, only to an `https:` base URL (or loopback `http:`) that carries no credentials, query or fragment, and fails a relayed backend failure with the client's own status error (`@composio/client/PermissionDeniedError`, …) so the classification below still applies. While telemetry is enabled, those requests (and only those) also carry the stored analytics install ID as `x-cli-install-id`, read through `readInstallIdWhenTelemetryEnabled` in `src/analytics/dispatch.ts`, which never creates an ID and never fails; do not add the header to backend requests or to `cliRequestHeaders()`. Everything else on the execute path (connection lookups, the permission gate, file uploads) and `dev playground-execute` stay on the backend client. Do not route other commands through the Dashboard, and do not add a second raw-HTTP host, without updating this paragraph. 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. Every `getFor` builds a new client, so never key a memo on a client instance: `clientScopeKey(client)` gives its auth mode, a SHA-256 fingerprint of its credential (never the key), org, and project (`listActiveConnectedAccounts` keys on it and the user id). While a client has `auth` and `scope`, a per-call `x-api-key`, `x-user-api-key`, `x-org-api-key`, `x-org-id` or `x-project-id` header fails the call before it is sent; an org or project known alone goes on an unscoped client's request, as `orgScopedOptions` and `getLatestToolVersion` do. Client errors are classified for display by `src/utils/api-error-extraction.ts`.
122
123 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.
124
ts/packages/cli/CHANGELOG.md
+23
@@ -4,6 +4,29 @@
4
5 ### Patch Changes
6
7 +- `composio execute` now sends the execution itself (single, `--parallel`, and
8 + meta tools) through the Composio Dashboard instead of calling the backend
9 + directly. Output, exit codes, error details, connection tips, and `--json`
10 + shapes are unchanged for tool results and for the request errors the backend
11 + reports; a backend outage is reported with the Dashboard's own message. No
12 + new login or configuration is needed.
13 + `composio dev playground-execute` and every other command still call the
14 + backend. A `COMPOSIO_BASE_URL` override without `COMPOSIO_WEB_URL` keeps
15 + `composio execute` on that backend; set both to use that deployment's
16 + Dashboard. The request is sent once, with no retry and no redirect following,
17 + and gives up after 15 minutes with a message that the tool may still have run.
18 + `COMPOSIO_WEB_URL` must be an `https://` URL with no credentials, query
19 + string, or fragment (`http://` is accepted only for localhost); otherwise
20 + `composio execute` fails before sending anything.
21 +
22 +- When telemetry is enabled, `composio execute` sends the CLI's anonymous
23 + analytics install ID (the random UUID in `~/.composio/analytics.json`) to the
24 + Composio Dashboard in an `x-cli-install-id` header, so Dashboard-side product
25 + analytics can be joined with the CLI's own events. It follows the existing
26 + telemetry opt-outs (`COMPOSIO_CLI_TELEMETRY_DISABLED`, `TELEMETRY_DISABLED`,
27 + `COMPOSIO_DISABLE_TELEMETRY`, or `CI`): when telemetry is off, or no install
28 + ID is stored, the header is not sent. It is never sent to the backend API.
29 +
30 - `composio link` and `composio dev connected-accounts link` reword their usage
31 and status messages on stderr: the rejected `--auth-config` on the root
32 command, a toolkit passed together with `--auth-config`, a missing toolkit,
ts/packages/cli/src/analytics/dispatch.ts
+15
@@ -155,6 +155,21 @@ const readAnalyticsState = Effect.gen(function* () {
155 return yield* readOptionalJson<AnalyticsState>(paths.analyticsStatePath);
156 });
157
158 +/**
159 + * The stored analytics install ID, for first-party requests that should join
160 + * the CLI's own events. `None` when telemetry is disabled or no ID is stored or
161 + * readable. Read-only: it never creates an ID, takes no lock, and cannot fail.
162 + */
163 +export const readInstallIdWhenTelemetryEnabled = Effect.gen(function* () {
164 + if (!(yield* postHogEnabled)) {
165 + return Option.none<string>();
166 + }
167 + const state = yield* readAnalyticsState;
168 + return typeof state?.install_id === 'string' && state.install_id.length > 0
169 + ? Option.some(state.install_id)
170 + : Option.none<string>();
171 +}).pipe(Effect.catch(() => Effect.succeed(Option.none<string>())));
172 +
173 const recoverStaleAnalyticsStateLock = (
174 fs: FileSystem.FileSystem,
175 lockPath: string
ts/packages/cli/src/cli-main.ts
+7 -1
@@ -31,6 +31,7 @@ import { UpgradeBinary } from 'src/services/upgrade-binary';
31 import { TerminalUI, TerminalUILive } from 'src/services/terminal-ui';
32 import { TriggersRealtime } from 'src/services/triggers-realtime';
33 import { ToolsExecutorLive as _ToolsExecutorLive } from 'src/services/tools-executor';
34 +import { DashboardToolExecution } from 'src/services/dashboard-tool-execution';
35 import { ToolkitSlugCatalog } from 'src/services/toolkit-slug-catalog';
36 import { ProjectContext } from 'src/services/project-context';
37 import { ProjectEnvironmentDetector } from 'src/services/project-environment-detector';
@@ -113,9 +114,14 @@ export const ToolkitSlugCatalogLive = Layer.provide(
114 ComposioToolkitsRepositoryCachedLive
115 ) satisfies RequiredLayer;
116
117 +export const DashboardToolExecutionLive = Layer.provide(
118 + DashboardToolExecution.Default,
119 + Layer.mergeAll(ComposioUserContextLive, FetchHttpClient.layer)
120 +);
121 +
122 export const ToolsExecutorLive = Layer.provide(
123 _ToolsExecutorLive,
118 - Layer.mergeAll(ComposioClientSingletonLive, ToolkitSlugCatalogLive)
124 + Layer.mergeAll(ComposioClientSingletonLive, ToolkitSlugCatalogLive, DashboardToolExecutionLive)
125 ) satisfies RequiredLayer;
126
127 export const ProjectContextLive = Layer.provide(
ts/packages/cli/src/commands/run.cmd.ts
+6 -1
@@ -11,6 +11,7 @@ import { resolveCommandProject } from 'src/services/command-project';
11 import { type RunHelperContext } from 'src/services/run-helpers-runtime';
12 import { warmToolInputDefinitions } from 'src/services/tool-input-validation';
13 import { ComposioUserContext } from 'src/services/user-context';
14 +import { resolveConsumerExecutionTransport } from 'src/services/dashboard-tool-execution';
15 import {
16 CLI_DEBUG_FLAG_NAMES,
17 debugFlagsToChildEnv,
@@ -268,10 +269,14 @@ const resolveRunHelperContext = () =>
269 const userContext = yield* ComposioUserContext;
270 const apiKey = Option.getOrUndefined(userContext.data.apiKey);
271 const orgId = Option.getOrUndefined(userContext.data.orgId);
272 + // A spawned `composio execute` treats a forwarded web URL as an explicit
273 + // choice of Dashboard. When this process keeps the backend transport (a
274 + // custom backend with no web URL), forward none so the child decides alike.
275 + const forwardWebURL = (yield* resolveConsumerExecutionTransport) === 'dashboard';
276 const baseContext = {
277 apiKey,
278 baseURL: userContext.data.baseURL,
274 - webURL: userContext.data.webURL,
279 + ...(forwardWebURL ? { webURL: userContext.data.webURL } : {}),
280 orgId,
281 } satisfies RunHelperContext;
282
ts/packages/cli/src/commands/tools/commands/tools.execute.cmd.ts
+12
@@ -18,6 +18,7 @@ import {
18 import { TerminalUI } from 'src/services/terminal-ui';
19 import { logToolDebug, makePerfDebugLogger } from 'src/services/runtime-debug-logger';
20 import { ToolsExecutor, detectInBandWarning } from 'src/services/tools-executor';
21 +import { resolveConsumerExecutionTransport } from 'src/services/dashboard-tool-execution';
22 import type {
23 InstantCharge,
24 ToolExecuteParams,
@@ -920,6 +921,14 @@ const resolveExecuteContext = (params: RunToolsExecuteParams) =>
921 }).pipe(Effect.map(Option.map(artifacts => artifacts.directoryPath)))
922 );
923
924 + // Root `execute` (the consumer surface, which is also what `composio run`
925 + // spawns) goes through the Dashboard; `dev playground-execute` stays on the
926 + // backend. The one exception is a backend URL overridden without a
927 + // matching `COMPOSIO_WEB_URL`: the key is never sent to another
928 + // deployment's Dashboard.
929 + const executesViaDashboard =
930 + params.surface === 'root' && (yield* resolveConsumerExecutionTransport) === 'dashboard';
931 +
932 return {
933 ui,
934 executor,
@@ -930,6 +939,9 @@ const resolveExecuteContext = (params: RunToolsExecuteParams) =>
939 executeOutputDir,
940 executeParams: {
941 userId: resolvedUserId.value,
942 + target: executesViaDashboard
943 + ? { kind: 'dashboard', orgId: resolvedProject.orgId }
944 + : { kind: 'backend' },
945 arguments: args,
946 client,
947 projectScope: {
ts/packages/cli/src/effects/create-tool-router-session.ts
+60 -29
@@ -1,5 +1,6 @@
1 import { Data, Effect, Option } from 'effect';
2 import type { ComposioClientShape } from '@composio/client/effect';
3 +import type { SessionCreateParams } from '@composio/client/resources/tool-router';
4 import {
5 getFreshConsumerToolRouterAuthConfigsFromCache,
6 getFreshConsumerToolRouterConnectedAccountsFromCache,
@@ -53,15 +54,21 @@ export class ToolRouterSessionCreateError extends Data.TaggedError(
54 readonly cause: unknown;
55 }> {}
56
57 +/** Everything a Tool Router session is built from, resolved before the session exists. */
58 +export interface ToolRouterSessionContext {
59 + /** The session-create body, minus the user id. */
60 + readonly sessionConfig: Omit<SessionCreateParams, 'user_id'>;
61 + readonly permissionSnapshot?: ConsumerPermissionSnapshot;
62 + readonly connectedAccounts?: Record<string, string>;
63 + readonly connectedAccountWordIds?: Record<string, string>;
64 +}
65 +
66 /**
57 - * Create an ephemeral Tool Router session for the given user ID.
58 - * Returns the session id plus the permission and connected-account context bound to it.
59 - *
60 - * Accepts a pre-resolved client instance (from ComposioClientSingleton)
61 - * so callers can resolve the dependency at layer construction time.
62 - * Used by `ToolsExecutorLive` which already holds the client reference.
67 + * Resolve the connections, permission snapshot and session settings for the
68 + * given user ID without creating a session. Callers that execute through the
69 + * Dashboard use only the connection context; the Dashboard owns the session.
70 */
64 -export const createToolRouterSessionContext = (
71 +export const resolveToolRouterSessionContext = (
72 client: ComposioClientShape,
73 userId: string,
74 options?: CreateToolRouterSessionOptions
@@ -167,9 +174,8 @@ export const createToolRouterSessionContext = (
174 connectedAccountIds,
175 })
176 : undefined;
170 - return yield* client.toolRouter.session
171 - .create({
172 - user_id: userId,
177 + return {
178 + sessionConfig: {
179 auth_configs: connectionContext.authConfigs,
180 connected_accounts: connectionContext.connectedAccounts,
181 manage_connections: { enable: options?.manageConnections ?? false },
@@ -185,26 +191,51 @@ export const createToolRouterSessionContext = (
191 experimental: permissionSnapshot?.enhancedControlsEnabled
192 ? { link_url_overwrite: ENHANCED_LINK_URL_OVERWRITE }
193 : undefined,
188 - })
189 - .pipe(
190 - Effect.mapError(
191 - cause =>
192 - new ToolRouterSessionCreateError({
193 - message: 'Failed to create a Tool Router session.',
194 - cause,
195 - })
196 - ),
197 - Effect.map((session): CreatedToolRouterSession => ({
198 - sessionId: session.session_id,
199 - permissionSnapshot,
200 - connectedAccounts: connectionContext.connectedAccounts,
201 - connectedAccountWordIds: resolveConnectedAccountWordIds(
202 - connectionContext.connectedAccounts,
203 - connectionContext.availableConnectedAccounts
204 - ),
205 - }))
206 - );
194 + },
195 + permissionSnapshot,
196 + connectedAccounts: connectionContext.connectedAccounts,
197 + connectedAccountWordIds: resolveConnectedAccountWordIds(
198 + connectionContext.connectedAccounts,
199 + connectionContext.availableConnectedAccounts
200 + ),
201 + } satisfies ToolRouterSessionContext;
202 });
203
204 +/** Create an ephemeral Tool Router session from an already resolved context. */
205 +export const createToolRouterSessionFromContext = (
206 + client: ComposioClientShape,
207 + userId: string,
208 + { sessionConfig, ...context }: ToolRouterSessionContext
209 +) =>
210 + client.toolRouter.session.create({ user_id: userId, ...sessionConfig }).pipe(
211 + Effect.mapError(
212 + cause =>
213 + new ToolRouterSessionCreateError({
214 + message: 'Failed to create a Tool Router session.',
215 + cause,
216 + })
217 + ),
218 + Effect.map((session): CreatedToolRouterSession => ({
219 + sessionId: session.session_id,
220 + ...context,
221 + }))
222 + );
223 +
224 +/**
225 + * Create an ephemeral Tool Router session for the given user ID.
226 + * Returns the session id plus the permission and connected-account context bound to it.
227 + *
228 + * Accepts a pre-resolved client instance (from ComposioClientSingleton)
229 + * so callers can resolve the dependency at layer construction time.
230 + */
231 +export const createToolRouterSessionContext = (
232 + client: ComposioClientShape,
233 + userId: string,
234 + options?: CreateToolRouterSessionOptions
235 +) =>
236 + resolveToolRouterSessionContext(client, userId, options).pipe(
237 + Effect.flatMap(context => createToolRouterSessionFromContext(client, userId, context))
238 + );
239 +
240 /** The name commands create their Tool Router session by. */
241 export const resolveToolRouterSession = createToolRouterSessionContext;
ts/packages/cli/src/services/composio-error-overrides.ts
+7 -1
@@ -5,6 +5,7 @@ import {
5 type ApiErrorDetails,
6 } from 'src/utils/api-error-extraction';
7 import { guessToolkitFromToolSlug } from 'src/utils/toolkit-from-tool-slug';
8 +import { DashboardToolExecutionError } from 'src/services/dashboard-tool-execution';
9
10 const NO_CONNECTION_SLUGS: ReadonlySet<string> = new Set([
11 'ActionExecute_ConnectedAccountNotFound',
@@ -130,7 +131,12 @@ export const mapComposioError = (params: {
131 normalized,
132 apiDetails,
133 slugValue,
133 - message: extractMessage(apiDetails) ?? extractMessage(normalized) ?? 'Unknown error',
134 + message:
135 + // Its own message names the Dashboard and what to do next; the cause it
136 + // wraps (a transport or decode failure) does not.
137 + normalized instanceof DashboardToolExecutionError
138 + ? normalized.message
139 + : (extractMessage(apiDetails) ?? extractMessage(normalized) ?? 'Unknown error'),
140 override: null,
141 };
142 };
ts/packages/cli/src/services/dashboard-tool-execution.ts new
+372
@@ -0,0 +1,372 @@
1 +import {
2 + APIError,
3 + AuthenticationError,
4 + BadRequestError,
5 + ConflictError,
6 + InternalServerError,
7 + NotFoundError,
8 + PermissionDeniedError,
9 + RateLimitError,
10 + UnprocessableEntityError,
11 + type APIErrorFields,
12 + type APIStatusError,
13 +} from '@composio/client/effect';
14 +import type {
15 + SessionExecuteMetaResponse,
16 + SessionExecuteResponse,
17 +} from '@composio/client/resources/tool-router';
18 +import { Config, Context, Data, Duration, Effect, Layer, Option, Result, Schema } from 'effect';
19 +import { FetchHttpClient, HttpClient, HttpClientRequest } from 'effect/http';
20 +import * as constants from 'src/constants';
21 +import { readInstallIdWhenTelemetryEnabled } from 'src/analytics/dispatch';
22 +import { APP_CONFIG } from 'src/config';
23 +import { cliRequestHeaders } from 'src/services/client-provenance';
24 +import { ComposioUserContext } from 'src/services/user-context';
25 +import { toolInputRequiredError } from 'src/utils/tool-input-required';
26 +
27 +export interface DashboardToolExecutionRequest {
28 + readonly slug: string;
29 + readonly arguments: Record<string, unknown>;
30 + /** Sent as `x-org-id`; the Dashboard resolves the consumer project from it. */
31 + readonly orgId: string;
32 + /** Connected account to run an app tool with; the Dashboard's sessions are multi-account. */
33 + readonly account?: string;
34 +}
35 +
36 +export class DashboardToolExecutionError extends Data.TaggedError(
37 + 'services/DashboardToolExecutionError'
38 +)<{
39 + readonly message: string;
40 + /**
41 + * - `unauthorized`: the Dashboard rejected the stored login, or there is none.
42 + * - `dashboard`: the Dashboard reported a failure of its own.
43 + * - `request`: no response arrived.
44 + * - `response`: the response was not one the CLI understands.
45 + * - `timeout`: no complete response arrived within the deadline.
46 + * - `configuration`: the Dashboard URL is not one the CLI will send a key to.
47 + */
48 + readonly reason:
49 + 'unauthorized' | 'dashboard' | 'request' | 'response' | 'timeout' | 'configuration';
50 + readonly status?: number;
51 + /** The Dashboard's error code, e.g. `UNAUTHORIZED`. */
52 + readonly code?: string;
53 + readonly cause?: unknown;
54 +}> {}
55 +
56 +const JsonObject = Schema.Record(Schema.String, Schema.Unknown);
57 +
58 +/** A backend execute answer that carries a result rather than a request for more input. */
59 +export type DashboardExecutionResult = Exclude<
60 + SessionExecuteResponse | SessionExecuteMetaResponse,
61 + { result_type: 'input_required' }
62 +>;
63 +
64 +// The backend's answer is relayed as the backend sent it. Like the backend
65 +// transport, the CLI checks only what tells a result from a request for input:
66 +// the tool has already run, so a missing field must not turn its result into
67 +// an error.
68 +const ExecutionResult = Schema.StructWithRest(
69 + Schema.Struct({ result_type: Schema.Literals(['completed', 'failed']) }),
70 + [JsonObject]
71 +);
72 +
73 +const ExecutionResponse = Schema.Union([
74 + ExecutionResult,
75 + Schema.Struct({
76 + result_type: Schema.Literal('input_required'),
77 + input_requests: Schema.Record(
78 + Schema.String,
79 + Schema.Struct({
80 + type: Schema.Literal('elicitation'),
81 + mode: Schema.Literal('form'),
82 + message: Schema.String,
83 + requested_schema: JsonObject,
84 + })
85 + ),
86 + request_state: Schema.optional(Schema.String),
87 + }),
88 +]);
89 +
90 +// Fields beyond the named ones (a validation failure's `errors`, say) are kept,
91 +// so the rebuilt `APIError` carries the same body the backend sent.
92 +const ApiErrorEnvelope = Schema.StructWithRest(
93 + Schema.Struct({
94 + message: Schema.String,
95 + code: Schema.optional(Schema.Number),
96 + slug: Schema.optional(Schema.String),
97 + status: Schema.optional(Schema.Number),
98 + request_id: Schema.optional(Schema.String),
99 + suggested_fix: Schema.optional(Schema.String),
100 + }),
101 + [JsonObject]
102 +);
103 +
104 +const ExecutionOutcome = Schema.Union([
105 + Schema.Struct({ ok: Schema.Literal(true), response: ExecutionResponse }),
106 + Schema.Struct({ ok: Schema.Literal(false), status: Schema.Number, error: ApiErrorEnvelope }),
107 +]);
108 +
109 +const ProcedureReply = Schema.Union([
110 + Schema.Struct({
111 + result: Schema.Struct({ data: Schema.Struct({ json: ExecutionOutcome }) }),
112 + }),
113 + Schema.Struct({
114 + error: Schema.Struct({
115 + json: Schema.Struct({
116 + message: Schema.String,
117 + data: Schema.optional(
118 + Schema.Struct({
119 + code: Schema.optional(Schema.String),
120 + httpStatus: Schema.optional(Schema.Number),
121 + })
122 + ),
123 + }),
124 + }),
125 + }),
126 +]);
127 +
128 +const decodeProcedureReply = Schema.decodeUnknownEffect(Schema.fromJsonString(ProcedureReply));
129 +
130 +/**
131 + * The error `@composio/client` fails with for the same backend response, so
132 + * error mapping, connection tips and telemetry need no second code path. The
133 + * status classes and the message follow the client's own rules.
134 + */
135 +const relayedApiError = (
136 + url: string,
137 + status: number,
138 + envelope: Schema.Schema.Type<typeof ApiErrorEnvelope>
139 +): APIStatusError => {
140 + const body = { error: envelope };
141 + const fields: APIErrorFields = {
142 + message: `${status} ${JSON.stringify(body)}`,
143 + method: 'POST',
144 + url,
145 + status,
146 + requestId: envelope.request_id,
147 + details:
148 + envelope.code !== undefined && envelope.slug !== undefined && envelope.status !== undefined
149 + ? {
150 + ...envelope,
151 + code: envelope.code,
152 + slug: envelope.slug,
153 + status: envelope.status,
154 + }
155 + : undefined,
156 + body,
157 + };
158 + if (status === 400) return new BadRequestError(fields);
159 + if (status === 401) return new AuthenticationError(fields);
160 + if (status === 403) return new PermissionDeniedError(fields);
161 + if (status === 404) return new NotFoundError(fields);
162 + if (status === 409) return new ConflictError(fields);
163 + if (status === 422) return new UnprocessableEntityError(fields);
164 + if (status === 429) return new RateLimitError(fields);
165 + if (status >= 500) return new InternalServerError(fields);
166 + return new APIError(fields);
167 +};
168 +
169 +const withoutTrailingSlashes = (url: string) => url.replace(/\/+$/, '');
170 +
171 +const LOGIN_HINT = 'Run `composio login`, then retry.';
172 +
173 +/**
174 + * Covers the POST and reading its body. The Dashboard allows a tool up to 800
175 + * seconds, so anything still pending after this is not coming.
176 + */
177 +const DASHBOARD_REQUEST_TIMEOUT = Duration.minutes(15);
178 +
179 +const LOOPBACK_HOSTNAMES: ReadonlySet<string> = new Set(['localhost', '127.0.0.1', '[::1]']);
180 +
181 +// Fixed text: the configured value may itself hold a secret, so it is never echoed.
182 +const INVALID_DASHBOARD_URL_MESSAGE =
183 + 'The Composio Dashboard URL (COMPOSIO_WEB_URL) must be an https:// URL with no credentials, query string, or fragment; http:// is accepted only for localhost. Nothing was sent.';
184 +
185 +/**
186 + * The Dashboard base the user API key may be sent to: `origin` for messages,
187 + * `base` for building request URLs. Rejects a URL that would put the key on
188 + * the wire in clear text, or that carries anything beyond scheme, host and path.
189 + */
190 +const resolveDashboardBase = (webURL: string) =>
191 + Result.try({
192 + try: () => new URL(webURL),
193 + catch: () => undefined,
194 + }).pipe(
195 + Result.filterOrFail(
196 + url =>
197 + url.username === '' &&
198 + url.password === '' &&
199 + !webURL.includes('?') &&
200 + !webURL.includes('#') &&
201 + (url.protocol === 'https:' ||
202 + (url.protocol === 'http:' && LOOPBACK_HOSTNAMES.has(url.hostname))),
203 + () => undefined
204 + ),
205 + Result.map(url => ({
206 + origin: url.origin,
207 + base: `${url.origin}${withoutTrailingSlashes(url.pathname)}`,
208 + })),
209 + Result.mapError(
210 + () =>
211 + new DashboardToolExecutionError({
212 + reason: 'configuration',
213 + message: INVALID_DASHBOARD_URL_MESSAGE,
214 + })
215 + )
216 + );
217 +
218 +/**
219 + * Which transport a consumer execution uses. The Dashboard is the default; a
220 + * backend URL that was overridden without a matching `COMPOSIO_WEB_URL` keeps
221 + * the backend transport, so a key for one deployment is never sent to another
222 + * deployment's Dashboard.
223 + */
224 +export const resolveConsumerExecutionTransport = Effect.gen(function* () {
225 + const webURL = yield* Config.option(Config.String('COMPOSIO_WEB_URL'));
226 + if (Option.isSome(webURL)) return 'dashboard' as const;
227 +
228 + const baseURL = yield* Config.option(Config.String('COMPOSIO_BASE_URL'));
229 + if (Option.isNone(baseURL)) return 'dashboard' as const;
230 +
231 + const environment = yield* APP_CONFIG.ENVIRONMENT;
232 + const environmentBaseURL =
233 + environment === 'staging' ? constants.STAGING_BASE_URL : constants.DEFAULT_BASE_URL;
234 +
235 + return withoutTrailingSlashes(baseURL.value) === withoutTrailingSlashes(environmentBaseURL)
236 + ? ('dashboard' as const)
237 + : ('backend' as const);
238 +});
239 +
240 +const makeDashboardToolExecution = Effect.gen(function* () {
241 + const httpClient = yield* HttpClient.HttpClient;
242 + const userContext = yield* ComposioUserContext;
243 +
244 + const call = (
245 + procedure: 'execute' | 'executeMeta',
246 + orgId: string,
247 + input: Record<string, unknown>
248 + ) =>
249 + Effect.gen(function* () {
250 + // Read per call: a login earlier in the same process changes the key.
251 + const { apiKey, webURL } = userContext.data;
252 + if (Option.isNone(apiKey)) {
253 + return yield* new DashboardToolExecutionError({
254 + reason: 'unauthorized',
255 + message: `You are not logged in. ${LOGIN_HINT}`,
256 + });
257 + }
258 +
259 + // Checked before the key is attached to anything.
260 + const dashboard = yield* Effect.fromResult(resolveDashboardBase(webURL));
261 + // Lets the Dashboard's product analytics join the CLI's own events. Sent
262 + // only while telemetry is enabled, and only on these requests.
263 + const installId = yield* readInstallIdWhenTelemetryEnabled;
264 + const url = `${dashboard.base}/api/cli/trpc/${procedure}`;
265 + const request = HttpClientRequest.post(url).pipe(
266 + HttpClientRequest.setHeaders({
267 + ...cliRequestHeaders(),
268 + accept: 'application/json',
269 + authorization: `Bearer ${apiKey.value}`,
270 + 'x-org-id': orgId,
271 + ...(Option.isSome(installId) ? { 'x-cli-install-id': installId.value } : {}),
272 + }),
273 + HttpClientRequest.bodyJsonUnsafe({ json: input })
274 + );
275 +
276 + // Sent exactly once: a retry, or a redirect followed by re-posting, could
277 + // run a tool that already acted.
278 + const { response, reply } = yield* Effect.gen(function* () {
279 + const response = yield* httpClient.execute(request).pipe(
280 + Effect.provideService(FetchHttpClient.RequestInit, { redirect: 'error' }),
281 + Effect.mapError(
282 + cause =>
283 + new DashboardToolExecutionError({
284 + reason: 'request',
285 + message: `Could not reach the Composio Dashboard at ${dashboard.origin}. The request was not retried; if the tool may have already run, check before running it again.`,
286 + cause,
287 + })
288 + )
289 + );
290 + const reply = yield* response.text.pipe(
291 + Effect.flatMap(decodeProcedureReply),
292 + Effect.mapError(
293 + cause =>
294 + new DashboardToolExecutionError({
295 + reason: 'response',
296 + status: response.status,
297 + message: `The Composio Dashboard at ${dashboard.origin} returned an unexpected response (HTTP ${response.status}). The request was not retried.`,
298 + cause,
299 + })
300 + )
301 + );
302 + return { response, reply };
303 + }).pipe(
304 + // Expiry interrupts the request, which aborts it; nothing is resent.
305 + Effect.timeoutOrElse({
306 + duration: DASHBOARD_REQUEST_TIMEOUT,
307 + orElse: () =>
308 + Effect.fail(
309 + new DashboardToolExecutionError({
310 + reason: 'timeout',
311 + message: `The Composio Dashboard at ${dashboard.origin} did not answer within ${Duration.toMinutes(DASHBOARD_REQUEST_TIMEOUT)} minutes. The tool may still have run; check before running it again. The request was not retried.`,
312 + })
313 + ),
314 + })
315 + );
316 +
317 + if ('error' in reply) {
318 + const failure = reply.error.json;
319 + const unauthorized = failure.data?.code === 'UNAUTHORIZED';
320 + return yield* new DashboardToolExecutionError({
321 + reason: unauthorized ? 'unauthorized' : 'dashboard',
322 + status: failure.data?.httpStatus ?? response.status,
323 + code: failure.data?.code,
324 + message: unauthorized
325 + ? `Authentication failed: ${failure.message}. ${LOGIN_HINT}`
326 + : failure.message,
327 + });
328 + }
329 +
330 + const outcome = reply.result.data.json;
331 + if (!outcome.ok) {
332 + return yield* Effect.fail(relayedApiError(url, outcome.status, outcome.error));
333 + }
334 + if (outcome.response.result_type === 'input_required') {
335 + return yield* toolInputRequiredError(
336 + `Tool ${input.tool_slug ?? input.slug}`,
337 + outcome.response
338 + );
339 + }
340 + // Not validated beyond `result_type`; see `ExecutionResult`.
341 + return outcome.response as DashboardExecutionResult;
342 + });
343 +
344 + return {
345 + /** Runs a tool through the Dashboard. Never retried. */
346 + execute: (request: DashboardToolExecutionRequest) =>
347 + call('execute', request.orgId, {
348 + tool_slug: request.slug,
349 + arguments: request.arguments,
350 + ...(request.account ? { account: request.account } : {}),
351 + }),
352 + /** Runs a meta tool through the Dashboard. Never retried. */
353 + executeMeta: (request: DashboardToolExecutionRequest) =>
354 + call('executeMeta', request.orgId, {
355 + slug: request.slug,
356 + arguments: request.arguments,
357 + }),
358 + } as const;
359 +});
360 +
361 +export type DashboardToolExecutionShape = Effect.Success<typeof makeDashboardToolExecution>;
362 +
363 +/**
364 + * Sends consumer tool executions to the Composio Dashboard, which runs them on
365 + * the backend and relays the backend's answer.
366 + */
367 +export class DashboardToolExecution extends Context.Service<
368 + DashboardToolExecution,
369 + DashboardToolExecutionShape
370 +>()('services/DashboardToolExecution') {
371 + static readonly Default = Layer.effect(DashboardToolExecution, makeDashboardToolExecution);
372 +}
ts/packages/cli/src/services/tools-executor.ts
+167 -68
@@ -1,13 +1,26 @@
1 import * as FileSystem from 'effect/FileSystem';
2 import * as Path from 'effect/Path';
3 import { Context, Effect, Layer } from 'effect';
4 -import { isComposioError, type ComposioClientShape } from '@composio/client/effect';
4 +import {
5 + isComposioError,
6 + type ComposioClientShape,
7 + type ComposioError,
8 +} from '@composio/client/effect';
9 import type {
10 SessionExecuteResponse,
11 SessionExecuteMetaResponse,
12 } from '@composio/client/resources/tool-router';
13 import { ComposioClientSingleton, type ToolkitProjectScope } from 'src/services/composio-clients';
10 -import { createToolRouterSessionContext } from 'src/effects/create-tool-router-session';
14 +import {
15 + createToolRouterSessionFromContext,
16 + resolveToolRouterSessionContext,
17 + type ToolRouterSessionContext,
18 +} from 'src/effects/create-tool-router-session';
19 +import {
20 + DashboardToolExecution,
21 + type DashboardToolExecutionError,
22 + type DashboardToolExecutionShape,
23 +} from 'src/services/dashboard-tool-execution';
24 import { gateToolExecution, type PermissionGateResult } from 'src/services/tool-permissions';
25 import {
26 ComposioNoActiveConnectionError,
@@ -19,7 +32,7 @@ import { ToolFileUploadError, uploadToolInputFiles } from 'src/services/tool-fil
32 import { toolkitFromToolSlug } from 'src/effects/toolkit-from-tool-slug';
33 import { ToolkitSlugCatalog } from 'src/services/toolkit-slug-catalog';
34 import { isMetaToolSlug } from 'src/utils/meta-tool-slugs';
22 -import { toolInputRequiredError } from 'src/utils/tool-input-required';
35 +import { toolInputRequiredError, type ToolInputRequiredError } from 'src/utils/tool-input-required';
36 import type { NodeOs } from 'src/services/node-os';
37 import type { NodeProcess } from 'src/services/node-process';
38 import type { ComposioUserContext } from 'src/services/user-context';
@@ -27,11 +40,22 @@ import type { ComposioToolkitsRepository } from 'src/services/composio-clients';
40 import type { TerminalUI } from 'src/services/terminal-ui';
41 import type { ComposioCliUserConfig } from 'src/services/cli-user-config';
42
43 +/**
44 + * Where the execution itself is sent. Connection lookups, the permission gate
45 + * and file uploads go to the backend either way.
46 + */
47 +export type ToolExecutionTarget =
48 + | { readonly kind: 'backend' }
49 + /** `orgId` is the organization the Dashboard resolves the consumer project from. */
50 + | { readonly kind: 'dashboard'; readonly orgId: string };
51 +
52 /**
53 * Parameters accepted by the Tool Router-based executor.
54 */
55 export interface ToolExecuteParams {
56 readonly userId: string;
57 + /** Chosen by the command, never inferred by the executor. */
58 + readonly target: ToolExecutionTarget;
59 readonly arguments: Record<string, unknown>;
60 readonly client?: ComposioClientShape;
61 readonly connectedAccounts?: Record<string, string>;
@@ -115,11 +139,21 @@ export const isExecutionSuccessful = (raw: {
139 readonly result_type: 'completed' | 'failed';
140 }): boolean => raw.result_type === 'completed';
141
142 +/** A Tool Router answer that carries a result rather than a request for more input. */
143 type ExecutedSessionResponse = Exclude<
144 SessionExecuteResponse | SessionExecuteMetaResponse,
145 { result_type: 'input_required' }
146 >;
147
148 +/** Sends the prepared arguments to where the tool runs. Called once per invocation. */
149 +type ToolExecutionSend = (
150 + arguments_: Record<string, unknown>
151 +) => Effect.Effect<
152 + ExecutedSessionResponse,
153 + ComposioError | ToolInputRequiredError | DashboardToolExecutionError,
154 + FileSystem.FileSystem | Path.Path | NodeOs
155 +>;
156 +
157 const toInstantCharge = (raw: ExecutedSessionResponse): InstantCharge | undefined => {
158 if (!raw.instant_charge) return undefined;
159 return {
@@ -180,6 +214,117 @@ export const detectInBandWarning = (
214 return null;
215 };
216
217 +/**
218 + * The backend transport: creates the Tool Router session and returns the
219 + * handle that executes on it. One session per invocation — CLI runs one tool
220 + * per process.
221 + */
222 +const createBackendSend = (
223 + client: ComposioClientShape,
224 + userId: string,
225 + slug: string,
226 + sessionContext: ToolRouterSessionContext
227 +) =>
228 + createToolRouterSessionFromContext(client, userId, sessionContext).pipe(
229 + Effect.map(
230 + ({ sessionId }): ToolExecutionSend =>
231 + arguments_ =>
232 + Effect.gen(function* () {
233 + // An execution is not `idempotent`, so it is sent once: a retry after the backend
234 + // already acted duplicates the side effect (e.g. sends the same email twice). The
235 + // response is not validated either, for the same reason.
236 + const raw: SessionExecuteResponse | SessionExecuteMetaResponse = yield* isMetaToolSlug(
237 + slug
238 + )
239 + ? client.toolRouter.session.executeMeta(sessionId, { slug, arguments: arguments_ })
240 + : client.toolRouter.session.execute(sessionId, {
241 + tool_slug: slug,
242 + arguments: arguments_,
243 + });
244 + if (raw.result_type === 'input_required') {
245 + return yield* toolInputRequiredError(`Tool ${slug}`, raw);
246 + }
247 + return raw;
248 + })
249 + )
250 + );
251 +
252 +/**
253 + * The Dashboard transport: the Dashboard creates the session and resolves the
254 + * user itself, so the CLI sends no session settings, only the account it
255 + * resolved for this tool's toolkit.
256 + */
257 +const createDashboardSend =
258 + (
259 + dashboard: DashboardToolExecutionShape,
260 + slug: string,
261 + orgId: string,
262 + connectedAccounts: ToolExecuteParams['connectedAccounts']
263 + ): ToolExecutionSend =>
264 + arguments_ => {
265 + const request = { slug, arguments: arguments_, orgId };
266 + if (isMetaToolSlug(slug)) return dashboard.executeMeta(request);
267 + const [account] = Object.values(connectedAccounts ?? {});
268 + return dashboard.execute({ ...request, account });
269 + };
270 +
271 +/**
272 + * What has to happen before the call is sent, whichever transport sends it:
273 + * the permission gate, then the arguments with their file inputs uploaded.
274 + */
275 +const prepareToolExecution = (
276 + client: ComposioClientShape,
277 + slug: string,
278 + params: ToolExecuteParams,
279 + { permissionSnapshot, connectedAccounts, connectedAccountWordIds }: ToolRouterSessionContext
280 +) =>
281 + Effect.gen(function* () {
282 + const toolkitSlug = yield* toolkitFromToolSlug(slug, toolkitProjectScope(params));
283 + const permissionGateResult = yield* gateToolExecution({
284 + toolSlug: slug,
285 + connectedAccountId: toolkitSlug ? connectedAccounts?.[toolkitSlug] : undefined,
286 + connectedAccountWordId: toolkitSlug ? connectedAccountWordIds?.[toolkitSlug] : undefined,
287 + snapshot: permissionSnapshot,
288 + });
289 + const fs = yield* FileSystem.FileSystem;
290 + const path = yield* Path.Path;
291 + const preparedArguments = isMetaToolSlug(slug)
292 + ? params.arguments
293 + : yield* getOrFetchToolInputDefinition(slug, params.projectScope).pipe(
294 + Effect.catch(() => Effect.succeed(null)),
295 + Effect.flatMap(definition => {
296 + if (!definition) {
297 + return Effect.succeed(params.arguments);
298 + }
299 +
300 + // A presigned-URL request the API refused is reported as
301 + // an upload failure of the source, with the client's error
302 + // as its cause.
303 + return uploadToolInputFiles({
304 + fs,
305 + path,
306 + toolSlug: slug,
307 + toolkitSlug,
308 + arguments_: params.arguments,
309 + inputSchema: definition.schema,
310 + client,
311 + }).pipe(
312 + Effect.catchIf(
313 + isComposioError,
314 + cause =>
315 + new ToolFileUploadError({
316 + cause,
317 + message: cause.message,
318 + reason: 'source-read',
319 + })
320 + )
321 + );
322 + })
323 + );
324 +
325 + return { permissionGateResult, preparedArguments };
326 + });
327 +
328 export const ToolsExecutorLive = Layer.effect(
329 ToolsExecutor,
330 Effect.gen(function* () {
@@ -192,6 +337,7 @@ export const ToolsExecutorLive = Layer.effect(
337 // implementation detail of toolkit resolution, not part of what a caller
338 // has to hand the executor.
339 const slugCatalog = yield* ToolkitSlugCatalog;
340 + const dashboard = yield* DashboardToolExecution;
341
342 return ToolsExecutor.of({
343 execute: (slug, params) =>
@@ -200,75 +346,28 @@ export const ToolsExecutorLive = Layer.effect(
346 // caller on the execute path already hands in a client built for the
347 // resolved org/project.
348 const resolvedClient = params.client ?? (yield* clientSingleton.get());
203 - // One session per invocation — CLI runs one tool per process.
204 - const { sessionId, permissionSnapshot, connectedAccounts, connectedAccountWordIds } =
205 - yield* createToolRouterSessionContext(resolvedClient, params.userId, {
349 + const sessionContext = yield* resolveToolRouterSessionContext(
350 + resolvedClient,
351 + params.userId,
352 + {
353 manageConnections: true,
354 connectedAccounts: params.connectedAccounts,
355 cacheScope: params.cacheScope,
209 - });
210 - const toolkitSlug = yield* toolkitFromToolSlug(slug, toolkitProjectScope(params));
211 - const permissionGateResult = yield* gateToolExecution({
212 - toolSlug: slug,
213 - connectedAccountId: toolkitSlug ? connectedAccounts?.[toolkitSlug] : undefined,
214 - connectedAccountWordId: toolkitSlug
215 - ? connectedAccountWordIds?.[toolkitSlug]
216 - : undefined,
217 - snapshot: permissionSnapshot,
218 - });
219 - const fs = yield* FileSystem.FileSystem;
220 - const path = yield* Path.Path;
221 - const normalizedArguments = isMetaToolSlug(slug)
222 - ? params.arguments
223 - : yield* getOrFetchToolInputDefinition(slug, params.projectScope).pipe(
224 - Effect.catch(() => Effect.succeed(null)),
225 - Effect.flatMap(definition => {
226 - if (!definition) {
227 - return Effect.succeed(params.arguments);
228 - }
229 -
230 - // A presigned-URL request the API refused is reported as
231 - // an upload failure of the source, with the client's error
232 - // as its cause.
233 - return uploadToolInputFiles({
234 - fs,
235 - path,
236 - toolSlug: slug,
237 - toolkitSlug,
238 - arguments_: params.arguments,
239 - inputSchema: definition.schema,
240 - client: resolvedClient,
241 - }).pipe(
242 - Effect.catchIf(
243 - isComposioError,
244 - cause =>
245 - new ToolFileUploadError({
246 - cause,
247 - message: cause.message,
248 - reason: 'source-read',
249 - })
250 - )
251 - );
252 - })
253 - );
356 + }
357 + );
358 + const { target } = params;
359 + const send =
360 + target.kind === 'dashboard'
361 + ? createDashboardSend(dashboard, slug, target.orgId, params.connectedAccounts)
362 + : yield* createBackendSend(resolvedClient, params.userId, slug, sessionContext);
363 + const { permissionGateResult, preparedArguments } = yield* prepareToolExecution(
364 + resolvedClient,
365 + slug,
366 + params,
367 + sessionContext
368 + );
369
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(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 - }
370 + const raw = yield* send(preparedArguments);
371
372 return normalizeResponse(raw, permissionGateResult);
373 }).pipe(
ts/packages/cli/test/__utils__/index.ts
+6
@@ -7,6 +7,12 @@ export { ToolkitsTest } from './layers/toolkits';
7 export { ConnectedAccountsTest } from './layers/connected-accounts';
8 export { CommandRunnerTest } from './layers/command-runner';
9 export { SetupTest } from './layers/setup';
10 +export {
11 + DashboardToolExecutionTest,
12 + dashboardProcedureError,
13 + dashboardProcedureResult,
14 + type DashboardTestRequest,
15 +} from './layers/dashboard';
16 export { cli } from './cli';
17 export { pkg };
18 export { withHttpServer } from './http-server';
ts/packages/cli/test/__utils__/layers/dashboard.ts new
+141
@@ -0,0 +1,141 @@
1 +import { isAPIError, type ComposioClientShape } from '@composio/client/effect';
2 +import type { SessionExecuteMetaParams } from '@composio/client/resources/tool-router';
3 +import { Effect, Layer, Schema } from 'effect';
4 +import { HttpClient, HttpClientError, HttpClientResponse } from 'effect/http';
5 +import { DashboardToolExecution } from 'src/services/dashboard-tool-execution';
6 +
7 +/** One request the CLI sent to the test Dashboard. */
8 +export interface DashboardTestRequest {
9 + readonly method: string;
10 + readonly url: string;
11 + readonly headers: Readonly<Record<string, string>>;
12 + /** The decoded JSON body, or `undefined` when there is none. */
13 + readonly body: unknown;
14 +}
15 +
16 +/** Answers a Dashboard request; a rejection reaches the CLI as a network failure. */
17 +export type DashboardTestResponder = (
18 + request: DashboardTestRequest
19 +) => Response | Promise<Response>;
20 +
21 +const DashboardProcedureInput = Schema.Struct({
22 + json: Schema.Struct({
23 + tool_slug: Schema.optional(Schema.String),
24 + slug: Schema.optional(Schema.String),
25 + arguments: Schema.Record(Schema.String, Schema.Unknown),
26 + account: Schema.optional(Schema.String),
27 + session: Schema.optional(Schema.Unknown),
28 + }),
29 +});
30 +
31 +/** A Dashboard procedure answer in the wire envelope the CLI decodes. */
32 +export const dashboardProcedureResult = (outcome: unknown, status = 200): Response =>
33 + Response.json({ result: { data: { json: outcome } } }, { status });
34 +
35 +/** A Dashboard-level failure in the wire envelope the CLI decodes. */
36 +export const dashboardProcedureError = (params: {
37 + readonly code: string;
38 + readonly httpStatus: number;
39 + readonly message: string;
40 +}): Response =>
41 + Response.json(
42 + {
43 + error: {
44 + json: {
45 + message: params.message,
46 + data: { code: params.code, httpStatus: params.httpStatus },
47 + },
48 + },
49 + },
50 + { status: params.httpStatus }
51 + );
52 +
53 +/**
54 + * What the real Dashboard does with a request: it creates a Tool Router
55 + * session itself and executes on `toolRouter`, relaying the answer, or a
56 + * backend 4xx as data. Session settings from the CLI are refused.
57 + */
58 +export const relayThroughToolRouter =
59 + (toolRouter: ComposioClientShape['toolRouter']) => (request: DashboardTestRequest) =>
60 + Effect.gen(function* () {
61 + const { session, account, ...call } = Schema.decodeUnknownSync(DashboardProcedureInput)(
62 + request.body
63 + ).json;
64 + if (session !== undefined) {
65 + return dashboardProcedureError({
66 + code: 'BAD_REQUEST',
67 + httpStatus: 400,
68 + message: "Unrecognized key(s) in object: 'session'",
69 + });
70 + }
71 + const created = yield* toolRouter.session.create({ user_id: 'user_resolved_by_dashboard' });
72 + const response = yield* request.url.endsWith('/executeMeta')
73 + ? toolRouter.session.executeMeta(created.session_id, {
74 + slug: call.slug as SessionExecuteMetaParams['slug'],
75 + arguments: call.arguments,
76 + })
77 + : toolRouter.session.execute(created.session_id, {
78 + tool_slug: call.tool_slug ?? '',
79 + arguments: call.arguments,
80 + ...(account ? { account } : {}),
81 + });
82 + return dashboardProcedureResult({ ok: true, response });
83 + }).pipe(
84 + Effect.catch(error =>
85 + Effect.succeed(
86 + isAPIError(error) && error.status < 500
87 + ? dashboardProcedureResult({
88 + ok: false,
89 + status: error.status,
90 + error: error.details ?? { message: error.message },
91 + })
92 + : dashboardProcedureError({
93 + code: 'INTERNAL_SERVER_ERROR',
94 + httpStatus: 500,
95 + message: error instanceof Error ? error.message : `${error}`,
96 + })
97 + )
98 + )
99 + );
100 +
101 +/**
102 + * The real `DashboardToolExecution` over an `HttpClient` that answers with
103 + * `respond`, or relays to `toolRouter` like the real Dashboard when the test
104 + * leaves it out, so the request the CLI builds and the reply it decodes are
105 + * both exercised. Needs `ComposioUserContext` for the login it sends.
106 + */
107 +export const DashboardToolExecutionTest = (params: {
108 + readonly toolRouter: ComposioClientShape['toolRouter'];
109 + readonly respond?: DashboardTestResponder;
110 +}) => {
111 + const relay = relayThroughToolRouter(params.toolRouter);
112 + const answer = (request: DashboardTestRequest) =>
113 + params.respond
114 + ? Effect.tryPromise({ try: async () => params.respond!(request), catch: cause => cause })
115 + : relay(request);
116 + return Layer.provide(
117 + DashboardToolExecution.Default,
118 + Layer.succeed(
119 + HttpClient.HttpClient,
120 + HttpClient.make((request, url) =>
121 + answer({
122 + method: request.method,
123 + url: url.toString(),
124 + headers: { ...request.headers },
125 + body:
126 + request.body._tag === 'Uint8Array'
127 + ? JSON.parse(new TextDecoder().decode(request.body.body))
128 + : undefined,
129 + }).pipe(
130 + Effect.map(response => HttpClientResponse.fromWeb(request, response)),
131 + Effect.mapError(
132 + cause =>
133 + new HttpClientError.HttpClientError({
134 + reason: new HttpClientError.TransportError({ request, cause }),
135 + })
136 + )
137 + )
138 + )
139 + )
140 + );
141 +};
ts/packages/cli/test/__utils__/services/test-layer.ts
+26 -1
@@ -51,6 +51,7 @@ import { UpgradeBinary } from 'src/services/upgrade-binary';
51 import { NodeOs } from 'src/services/node-os';
52 import { TriggersRealtime } from 'src/services/triggers-realtime';
53 import { ToolsExecutor, ToolsExecutorLive } from 'src/services/tools-executor';
54 +import { DashboardToolExecutionTest, type DashboardTestResponder } from '../layers/dashboard';
55 import { ToolkitSlugCatalog } from 'src/services/toolkit-slug-catalog';
56 import type { ToolExecuteResponse } from 'src/services/tools-executor';
57 import type {
@@ -201,6 +202,20 @@ export interface TestLiveInput {
202 onExecute?: (slug: string) => void;
203 };
204
205 + /**
206 + * Stand in for the Dashboard endpoints `composio execute` sends consumer
207 + * executions to.
208 + *
209 + * By default the test Dashboard does what the real one does with a request:
210 + * it creates a Tool Router session itself and executes on the mock client's
211 + * `toolRouter.session.*` handlers, relaying their answer or their status
212 + * error. Set `respond` to answer yourself; a rejection is delivered to the
213 + * CLI as a network failure.
214 + */
215 + dashboard?: {
216 + respond?: DashboardTestResponder;
217 + };
218 +
219 /**
220 * Override Tool Router session behavior for tests.
221 *
@@ -847,7 +862,17 @@ export const TestLayer = (input?: TestLiveInput) =>
862 )
863 : Layer.provide(
864 ToolsExecutorLive,
850 - Layer.mergeAll(ComposioClientSingletonTest, ToolkitSlugCatalogTest)
865 + Layer.mergeAll(
866 + ComposioClientSingletonTest,
867 + ToolkitSlugCatalogTest,
868 + Layer.provide(
869 + DashboardToolExecutionTest({
870 + toolRouter: mockComposioClient.toolRouter,
871 + respond: input?.dashboard?.respond,
872 + }),
873 + ComposioUserContextTest
874 + )
875 + )
876 );
877
878 const CliConfigLive = CliConfig.layer(ComposioCliConfig);
ts/packages/cli/test/src/commands/run.cmd.test.ts
+48
@@ -292,6 +292,54 @@ describe('CLI: composio run', () => {
292 );
293 });
294
295 + const webURLForwarding = [
296 + {
297 + name: 'no URL overrides',
298 + env: {},
299 + forwarded: true,
300 + },
301 + {
302 + name: 'a custom backend URL with no web URL',
303 + env: { COMPOSIO_BASE_URL: 'https://composio.internal.example' },
304 + forwarded: false,
305 + },
306 + {
307 + name: 'a custom backend URL and a web URL',
308 + env: {
309 + COMPOSIO_BASE_URL: 'https://composio.internal.example',
310 + COMPOSIO_WEB_URL: 'https://dashboard.internal.example',
311 + },
312 + forwarded: true,
313 + },
314 + ] as const;
315 +
316 + describe.each(webURLForwarding)('$name', testCase => {
317 + layer(
318 + RunTestLive({
319 + baseConfigProvider: ConfigProvider.fromEnv({ env: testCase.env }),
320 + })
321 + )(it => {
322 + // A forwarded web URL is what moves a script's `execute()` onto the
323 + // Dashboard, so it must not appear when this process stays on the backend.
324 + it.effect(
325 + `[Given] ${testCase.name} [Then] the web URL is ${testCase.forwarded ? '' : 'not '}forwarded to helpers`,
326 + () =>
327 + Effect.gen(function* () {
328 + let preloadSource = '';
329 + commandRuns.mockImplementation(command => {
330 + preloadSource = readRunPreloadSource(inspectRunCommand(command).cmd);
331 + return Effect.succeed(ChildProcessSpawner.ExitCode(0));
332 + });
333 +
334 + yield* cli(['run', 'console.log("hi")']);
335 +
336 + expect(preloadSource).toContain('"baseURL":');
337 + expect(preloadSource.includes('"webURL":')).toBe(testCase.forwarded);
338 + })
339 + );
340 + });
341 + });
342 +
343 layer(Layer.merge(RunTestLive(), telemetryDebugModeLayer(true)))(it => {
344 it.effect(
345 '[Given] --telemetry-debug [Then] the spawned script and its children observe it',
ts/packages/cli/test/src/commands/tools/tools.execute.cmd.test.ts
+141 -81
@@ -3,7 +3,7 @@ import * as os from 'node:os';
3 import * as path from 'node:path';
4 import { describe, expect, it, layer } from '@effect/vitest';
5 import { vi, beforeEach, afterEach } from 'vitest';
6 -import { Config, ConfigProvider, DateTime, Effect, Option } from 'effect';
6 +import { Config, ConfigProvider, DateTime, Effect, Exit, Option } from 'effect';
7 import { APIConnectionError, BadRequestError } from '@composio/client/effect';
8 import { ComposioNoActiveConnectionError } from 'src/services/composio-error-overrides';
9 import { setupCacheDir } from 'src/effects/setup-cache-dir';
@@ -12,6 +12,7 @@ import { clearInProcessMemos } from 'src/utils/memoize-in-process';
12 import * as consumerShortTermCache from 'src/services/consumer-short-term-cache';
13 import * as composioAccount from 'src/services/composio-account';
14 import type { ToolkitProjectScope } from 'src/services/composio-clients';
15 +import * as toolPermissions from 'src/services/tool-permissions';
16 import * as redactModule from 'src/ui/redact';
17 import { cli, TestLive, MockConsole } from 'test/__utils__';
18 import { liveEnvConfigProvider } from 'test/__utils__/live-env-config-provider';
@@ -156,12 +157,18 @@ describe('CLI: composio execute', () => {
157 });
158
159 let recordedSessionCreateParams: Array<Record<string, unknown>> = [];
160 + let recordedExecuteParams: Array<Record<string, unknown>> = [];
161 + // Records what one execution call carried; the account travels with the call, not the session.
162 + const recordExecute = <T>(params: unknown, result: T) =>
163 + Effect.sync(() => {
164 + recordedExecuteParams.push(params as Record<string, unknown>);
165 + return result;
166 + });
167 let recordedProjectToolkitScopes: Array<ToolkitProjectScope | undefined> = [];
160 - let recordedExecuteOptions: Array<{ maxRetries?: number; decode?: boolean } | undefined> = [];
168 beforeEach(() => {
169 recordedSessionCreateParams = [];
170 + recordedExecuteParams = [];
171 recordedProjectToolkitScopes = [];
164 - recordedExecuteOptions = [];
172 });
173
174 layer(
@@ -262,7 +269,7 @@ describe('CLI: composio execute', () => {
269 const output = parseLastJson(lines);
270
271 expect(outputText).not.toContain('Response\n{');
265 - // Response flows through real ToolsExecutorLive → mock session.execute
272 + // Response flows through real ToolsExecutorLive → test Dashboard → mock session.execute
273 expect(output.successful).toBe(true);
274 expect(output.data.tool_slug).toBe('GMAIL_SEND_EMAIL');
275 expect(output.data.arguments).toEqual({ recipient: 'a' });
@@ -396,30 +403,17 @@ describe('CLI: composio execute', () => {
403 tool_router_tools: ['COMPOSIO_SEARCH_TOOLS', 'COMPOSIO_MANAGE_CONNECTIONS'],
404 };
405 }),
399 - execute: (_sessionId, params, options) =>
400 - Effect.sync(() => {
401 - recordedExecuteOptions.push(options);
402 - return {
403 - result_type: 'completed' as const,
404 - data: { tool_slug: params.tool_slug, arguments: params.arguments },
405 - error: null,
406 - log_id: 'log_gmail_default',
407 - };
408 - }),
409 - executeMeta: (_sessionId, params, options) =>
410 - Effect.sync(() => {
411 - recordedExecuteOptions.push(options);
412 - return {
413 - result_type: 'completed' as const,
414 - data: { slug: params.slug, arguments: params.arguments },
415 - error: null,
416 - log_id: 'log_meta_default',
417 - };
406 + execute: (_sessionId, params) =>
407 + recordExecute(params, {
408 + result_type: 'completed' as const,
409 + data: { tool_slug: params.tool_slug, arguments: params.arguments },
410 + error: null,
411 + log_id: 'log_gmail_default',
412 }),
413 },
414 })
421 - )('[Given] default alias exists [Then] execute pins the default connected account', it => {
422 - it.effect('passes connected_accounts with the default alias account', () =>
415 + )('[Given] default alias exists [Then] execute sends the default connected account', it => {
416 + it.effect('sends the default alias account with the call', () =>
417 Effect.gen(function* () {
418 yield* cli([
419 'execute',
@@ -429,21 +423,7 @@ describe('CLI: composio execute', () => {
423 '{"recipient":"a"}',
424 ]);
425
432 - expect(recordedSessionCreateParams[0]?.connected_accounts).toEqual({
433 - gmail: 'con_gmail_default',
434 - });
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 - it.effect('never retries a meta tool execution', () =>
443 - Effect.gen(function* () {
444 - yield* cli(['execute', 'COMPOSIO_SEARCH_TOOLS', '-d', '{"query":"email"}']);
445 -
446 - expect(recordedExecuteOptions).toEqual([undefined]);
426 + expect(recordedExecuteParams[0]?.account).toBe('con_gmail_default');
427 })
428 );
429
@@ -541,7 +521,7 @@ describe('CLI: composio execute', () => {
521 };
522 }),
523 execute: (_sessionId, params) =>
544 - Effect.succeed({
524 + recordExecute(params, {
525 result_type: 'completed' as const,
526 data: { tool_slug: params.tool_slug, arguments: params.arguments },
527 error: null,
@@ -549,7 +529,7 @@ describe('CLI: composio execute', () => {
529 }),
530 },
531 })
552 - )('[Given] --account selector [Then] execute pins the matched connected account', it => {
532 + )('[Given] --account selector [Then] execute sends the matched connected account', it => {
533 it.effect('matches even when a stale config disables the former experiment', () =>
534 Effect.gen(function* () {
535 const cliConfig = yield* ComposioCliUserConfig;
@@ -567,9 +547,7 @@ describe('CLI: composio execute', () => {
547 '{"recipient":"a"}',
548 ]);
549
570 - expect(recordedSessionCreateParams[0]?.connected_accounts).toEqual({
571 - gmail: 'con_gmail_secondary',
572 - });
550 + expect(recordedExecuteParams[0]?.account).toBe('con_gmail_secondary');
551 })
552 );
553
@@ -590,8 +568,8 @@ describe('CLI: composio execute', () => {
568 ]);
569
570 expect(recordedSessionCreateParams).toHaveLength(2);
593 - expect(recordedSessionCreateParams.map(params => params.connected_accounts)).toEqual(
594 - expect.arrayContaining([{ gmail: 'con_gmail_secondary' }, { gmail: 'con_gmail_default' }])
571 + expect(recordedExecuteParams.map(params => params.account)).toEqual(
572 + expect.arrayContaining(['con_gmail_secondary', 'con_gmail_default'])
573 );
574 })
575 );
@@ -624,8 +602,8 @@ describe('CLI: composio execute', () => {
602 { recipient: 'repeat-first@example.com' },
603 { recipient: 'repeat-second@example.com' },
604 ]);
627 - expect(recordedSessionCreateParams.map(params => params.connected_accounts)).toEqual(
628 - expect.arrayContaining([{ gmail: 'con_gmail_secondary' }, { gmail: 'con_gmail_default' }])
605 + expect(recordedExecuteParams.map(params => params.account)).toEqual(
606 + expect.arrayContaining(['con_gmail_secondary', 'con_gmail_default'])
607 );
608 expect(recordedSessionCreateParams).toHaveLength(2);
609 })
@@ -706,7 +684,7 @@ describe('CLI: composio execute', () => {
684 };
685 }),
686 execute: (_sessionId, params) =>
709 - Effect.succeed({
687 + recordExecute(params, {
688 result_type: 'completed' as const,
689 data: { tool_slug: params.tool_slug, arguments: params.arguments },
690 error: null,
@@ -727,9 +705,7 @@ describe('CLI: composio execute', () => {
705 '{"property_id":"1"}',
706 ]);
707
730 - expect(recordedSessionCreateParams[0]?.connected_accounts).toMatchObject({
731 - google_analytics: 'con_google_analytics_default',
732 - });
708 + expect(recordedExecuteParams[0]?.account).toBe('con_google_analytics_default');
709 })
710 );
711
@@ -745,9 +721,7 @@ describe('CLI: composio execute', () => {
721 '{"message":"hi"}',
722 ]);
723
748 - expect(recordedSessionCreateParams[0]?.connected_accounts).toMatchObject({
749 - microsoft_teams: 'con_microsoft_teams_default',
750 - });
724 + expect(recordedExecuteParams[0]?.account).toBe('con_microsoft_teams_default');
725 })
726 );
727 });
@@ -809,7 +783,7 @@ describe('CLI: composio execute', () => {
783 };
784 }),
785 execute: (_sessionId, params) =>
812 - Effect.succeed({
786 + recordExecute(params, {
787 result_type: 'completed' as const,
788 data: { tool_slug: params.tool_slug, arguments: params.arguments },
789 error: null,
@@ -840,9 +814,7 @@ describe('CLI: composio execute', () => {
814 '{"query":"ada"}',
815 ]);
816
843 - expect(recordedSessionCreateParams[0]?.connected_accounts).toEqual({
844 - custom_grain: 'ca_1',
845 - });
817 + expect(recordedExecuteParams[0]?.account).toBe('ca_1');
818 // Every lookup — account selection, the permission gate, error
819 // mapping — must ask for the project execute resolved.
820 expect(recordedProjectToolkitScopes.length).toBeGreaterThan(0);
@@ -896,9 +868,7 @@ describe('CLI: composio execute', () => {
868
869 yield* cli(['execute', 'CUSTOM_GRAIN_SEARCH_PERSONS', '-d', '{"query":"ada"}']);
870
899 - expect(recordedSessionCreateParams[0]?.connected_accounts).toEqual({
900 - custom_grain: 'ca_1',
901 - });
871 + expect(recordedExecuteParams[0]?.account).toBe('ca_1');
872 })
873 );
874 });
@@ -948,7 +918,7 @@ describe('CLI: composio execute', () => {
918 };
919 }),
920 execute: (_sessionId, params) =>
951 - Effect.succeed({
921 + recordExecute(params, {
922 result_type: 'completed' as const,
923 data: { tool_slug: params.tool_slug, arguments: params.arguments },
924 error: null,
@@ -957,9 +927,9 @@ describe('CLI: composio execute', () => {
927 },
928 })
929 )(
960 - '[Given] a non-managed connected account [Then] execute preloads auth configs into the Tool Router session',
930 + '[Given] a non-managed connected account [Then] execute leaves the session to the Dashboard',
931 it => {
962 - it.effect('passes explicit auth_configs for custom auth toolkits', () =>
932 + it.effect('sends the connected account and no auth configs for custom auth toolkits', () =>
933 Effect.gen(function* () {
934 yield* cli([
935 'execute',
@@ -974,15 +944,9 @@ describe('CLI: composio execute', () => {
944 expect(output.successful).toBe(true);
945 expect(recordedSessionCreateParams).toHaveLength(1);
946 expect(recordedSessionCreateParams[0]?.user_id).toEqual(expect.any(String));
977 - expect(recordedSessionCreateParams[0]?.auth_configs).toEqual({
978 - posthog: 'ac_posthog_custom',
979 - });
980 - expect(recordedSessionCreateParams[0]?.connected_accounts).toEqual({
981 - posthog: 'con_posthog_active',
982 - });
983 - expect(recordedSessionCreateParams[0]?.instant).toEqual({
984 - return_instant_charge: true,
985 - });
947 + // The Dashboard owns the session, so the CLI sends it no auth configs.
948 + expect(recordedSessionCreateParams[0]?.auth_configs).toBeUndefined();
949 + expect(recordedExecuteParams[0]?.account).toBe('con_posthog_active');
950 })
951 );
952 }
@@ -1012,7 +976,7 @@ describe('CLI: composio execute', () => {
976 };
977 }),
978 execute: (_sessionId, params) =>
1015 - Effect.succeed({
979 + recordExecute(params, {
980 result_type: 'completed' as const,
981 data: { tool_slug: params.tool_slug, arguments: params.arguments },
982 error: null,
@@ -1020,8 +984,8 @@ describe('CLI: composio execute', () => {
984 }),
985 },
986 })
1023 - )('[Given] cached auth configs [Then] execute seeds the session from cache', it => {
1024 - it.effect('uses cached auth_configs for consumer execute sessions', () =>
987 + )('[Given] cached auth configs [Then] execute sends no cached session settings', it => {
988 + it.effect('sends no cached auth configs on consumer executions', () =>
989 Effect.gen(function* () {
990 vi.spyOn(
991 consumerShortTermCache,
@@ -1045,9 +1009,8 @@ describe('CLI: composio execute', () => {
1009 ]);
1010
1011 expect(recordedSessionCreateParams).toHaveLength(1);
1048 - expect(recordedSessionCreateParams[0]?.auth_configs).toEqual({
1049 - posthog: 'ac_posthog_cached',
1050 - });
1012 + // The Dashboard owns the session, so the CLI sends it no auth configs.
1013 + expect(recordedSessionCreateParams[0]?.auth_configs).toBeUndefined();
1014 })
1015 );
1016 });
@@ -2678,6 +2641,103 @@ describe('CLI: composio execute', () => {
2641 );
2642 });
2643
2644 + // --- Execution order (flows through real ToolsExecutorLive) ---
2645 +
2646 + // The backend transport creates the session before the gate; the Dashboard
2647 + // transport has no session to create. A denied tool is sent nowhere on either.
2648 + it.effect.each<{
2649 + readonly transport: string;
2650 + readonly env: Record<string, string>;
2651 + readonly calls: ReadonlyArray<string>;
2652 + }>([
2653 + {
2654 + transport: 'backend',
2655 + // A custom backend with no web URL keeps the backend transport.
2656 + env: { COMPOSIO_BASE_URL: 'https://composio.internal.example' },
2657 + calls: ['create'],
2658 + },
2659 + { transport: 'dashboard', env: {}, calls: [] },
2660 + ])(
2661 + 'on the $transport transport, prepares in order and sends nothing once the permission gate denies',
2662 + ({ env, calls }) => {
2663 + const toolRouterCalls: Array<string> = [];
2664 + // Enhanced controls ask before every call; with no one to answer the
2665 + // prompt (as under test), the gate denies.
2666 + vi.spyOn(toolPermissions, 'getConsumerPermissionSnapshot').mockImplementation(params =>
2667 + Effect.succeed({
2668 + orgId: params.orgId,
2669 + projectId: params.projectId,
2670 + consumerUserId: params.consumerUserId,
2671 + enhancedControlsEnabled: true,
2672 + permissions: { default: 'ask_every_call' as const, overrides: {} },
2673 + connectedAccountIds: [],
2674 + fetchedAt: 1_767_225_600_000,
2675 + })
2676 + );
2677 +
2678 + return Effect.gen(function* () {
2679 + const exit = yield* cli([
2680 + 'execute',
2681 + 'GMAIL_SEND_EMAIL',
2682 + '--skip-connection-check',
2683 + '-d',
2684 + '{"recipient":"a"}',
2685 + ]).pipe(Effect.exit);
2686 + const lines = yield* MockConsole.getLines({ stripAnsi: true });
2687 +
2688 + expect(Exit.isFailure(exit)).toBe(true);
2689 + expect(lines.join('\n')).toContain('requires interactive approval');
2690 + expect(toolRouterCalls).toEqual(calls);
2691 + }).pipe(
2692 + Effect.provide(
2693 + TestLive({
2694 + baseConfigProvider: ConfigProvider.fromEnv({
2695 + env: { COMPOSIO_USER_API_KEY: 'test_api_key', ...env },
2696 + }),
2697 + fixture: 'global-test-user-id',
2698 + stdin: { isTTY: true, data: '' },
2699 + dashboard: {
2700 + respond: () => {
2701 + toolRouterCalls.push('dashboard');
2702 + return Response.error();
2703 + },
2704 + },
2705 + toolRouter: {
2706 + create: params =>
2707 + Effect.sync(() => {
2708 + toolRouterCalls.push('create');
2709 + return {
2710 + session_id: 'trs_backend_session',
2711 + config: {
2712 + user_id: params.user_id,
2713 + execute: {},
2714 + search: {},
2715 + preload: { tools: [] },
2716 + instant: false,
2717 + },
2718 + config_version: 1,
2719 + mcp: { type: 'http' as const, url: 'https://mcp.test.composio.dev' },
2720 + tool_router_tools: [],
2721 + };
2722 + }),
2723 + execute: () =>
2724 + Effect.sync(() => {
2725 + toolRouterCalls.push('execute');
2726 + return {
2727 + result_type: 'completed' as const,
2728 + data: {},
2729 + error: null,
2730 + log_id: 'log_test',
2731 + };
2732 + }),
2733 + },
2734 + })
2735 + ),
2736 + Effect.scoped
2737 + );
2738 + }
2739 + );
2740 +
2741 // --- Tool Router error path (flows through real ToolsExecutorLive) ---
2742
2743 layer(
ts/packages/cli/test/src/commands/tools/tools.execute.transport.test.ts new
+775
@@ -0,0 +1,775 @@
1 +import * as fs from 'node:fs';
2 +import * as os from 'node:os';
3 +import * as path from 'node:path';
4 +import { describe, expect, it } from '@effect/vitest';
5 +import { afterEach, beforeEach, vi } from 'vitest';
6 +import { APIConnectionError, APIError, type ComposioError } from '@composio/client/effect';
7 +import type { SessionExecuteResponse } from '@composio/client/resources/tool-router';
8 +import { ConfigProvider, Effect, Exit, Fiber, Option, Schema } from 'effect';
9 +import { TestClock } from 'effect/testing';
10 +import * as composioAccount from 'src/services/composio-account';
11 +import * as consumerShortTermCache from 'src/services/consumer-short-term-cache';
12 +import * as toolPermissions from 'src/services/tool-permissions';
13 +import { ComposioUserContext } from 'src/services/user-context';
14 +import {
15 + cli,
16 + dashboardProcedureError,
17 + dashboardProcedureResult,
18 + MockConsole,
19 + TestLive,
20 + type DashboardTestRequest,
21 +} from 'test/__utils__';
22 +import type { TestLiveInput } from 'test/__utils__/services/test-layer';
23 +
24 +vi.mock('node:dns/promises', () => ({
25 + lookup: vi.fn().mockResolvedValue([{ address: '93.184.216.34', family: 4 }]),
26 +}));
27 +
28 +// `src/ui/redact` reads `CI` once at import time; clear it before the imports
29 +// above run so both transports print the same unredacted values everywhere.
30 +vi.hoisted(() => {
31 + delete process.env.CI;
32 +});
33 +
34 +const DEFAULT_DASHBOARD = 'https://dashboard.composio.dev';
35 +
36 +const configProvider = (env: Record<string, string> = {}) =>
37 + ConfigProvider.fromEnv({ env: { COMPOSIO_USER_API_KEY: 'test_api_key', ...env } });
38 +
39 +const temporaryCacheDirs: Array<string> = [];
40 +
41 +/**
42 + * A cache directory whose `user_data.json` stores the given URLs, as a login
43 + * made under other `COMPOSIO_*_URL` values would have left it.
44 + */
45 +const cacheDirWithStoredURLs = (stored: { base_url?: string; web_url?: string }) => {
46 + const cacheDir = fs.mkdtempSync(path.join(os.tmpdir(), 'composio-cli-test-cache-'));
47 + temporaryCacheDirs.push(cacheDir);
48 + fs.writeFileSync(
49 + path.join(cacheDir, 'user_data.json'),
50 + JSON.stringify({
51 + api_key: 'test_api_key',
52 + // Distinct from every fixture, so a request carrying it proves this file was read.
53 + org_id: 'org_stored',
54 + project_id: null,
55 + test_user_id: 'global-default',
56 + base_url: null,
57 + web_url: null,
58 + ...stored,
59 + })
60 + );
61 + return cacheDir;
62 +};
63 +
64 +/** A custom backend with no web URL: the one consumer setup that stays on the backend. */
65 +const backendOnlyEnv = { COMPOSIO_BASE_URL: 'https://composio.internal.example' };
66 +
67 +type ToolRouterCall = { readonly method: 'create' | 'execute' | 'executeMeta' } & Record<
68 + string,
69 + unknown
70 +>;
71 +
72 +/**
73 + * One isolated CLI world: a Dashboard that records what it is sent, and a
74 + * backend Tool Router that records what it is asked to do.
75 + */
76 +const makeWorld = (options: {
77 + readonly env?: Record<string, string>;
78 + readonly dashboard?: (request: DashboardTestRequest) => Response | Promise<Response>;
79 + readonly backendExecute?: () => Effect.Effect<SessionExecuteResponse, ComposioError>;
80 + readonly input?: Partial<TestLiveInput>;
81 +}) => {
82 + const dashboardRequests: Array<DashboardTestRequest> = [];
83 + const toolRouterCalls: Array<ToolRouterCall> = [];
84 + const backendResponse = (
85 + data: Record<string, unknown>
86 + ): Effect.Effect<SessionExecuteResponse, ComposioError> =>
87 + options.backendExecute
88 + ? options.backendExecute()
89 + : Effect.succeed({ result_type: 'completed', data, error: null, log_id: 'log_test' });
90 +
91 + const layer = TestLive({
92 + baseConfigProvider: configProvider(options.env),
93 + fixture: 'global-test-user-id',
94 + stdin: { isTTY: true, data: '' },
95 + dashboard: {
96 + respond: request => {
97 + dashboardRequests.push(request);
98 + return options.dashboard
99 + ? options.dashboard(request)
100 + : dashboardProcedureResult({
101 + ok: true,
102 + response: {
103 + result_type: 'completed',
104 + data: { id: 'msg_1' },
105 + error: null,
106 + log_id: 'log_test',
107 + },
108 + });
109 + },
110 + },
111 + toolRouter: {
112 + create: params =>
113 + Effect.sync(() => {
114 + toolRouterCalls.push({ method: 'create', params });
115 + return {
116 + session_id: 'trs_backend_session',
117 + config: { user_id: params.user_id },
118 + mcp: { type: 'http' as const, url: 'https://mcp.test.composio.dev' },
119 + tool_router_tools: [],
120 + } as never;
121 + }),
122 + execute: (sessionId, params, requestOptions) =>
123 + Effect.suspend(() => {
124 + toolRouterCalls.push({ method: 'execute', sessionId, params, requestOptions });
125 + return backendResponse({ id: 'msg_1' });
126 + }),
127 + executeMeta: (sessionId, params, requestOptions) =>
128 + Effect.suspend(() => {
129 + toolRouterCalls.push({ method: 'executeMeta', sessionId, params, requestOptions });
130 + return backendResponse({ id: 'msg_1' });
131 + }),
132 + },
133 + ...options.input,
134 + });
135 +
136 + /**
137 + * Runs the CLI and returns everything it printed, how it exited, and the
138 + * URLs in effect: `baseURL` is what the backend client is built with,
139 + * `webURL` what the Dashboard request is built from.
140 + */
141 + const run = (args: ReadonlyArray<string>) =>
142 + Effect.gen(function* () {
143 + const exit = yield* cli(args).pipe(Effect.exit);
144 + const lines = yield* MockConsole.getLines({ stripAnsi: true });
145 + const { baseURL, webURL } = (yield* ComposioUserContext).data;
146 + return { exit, lines, output: lines.join('\n'), baseURL, webURL };
147 + }).pipe(Effect.provide(layer), Effect.scoped);
148 +
149 + return { run, dashboardRequests, toolRouterCalls };
150 +};
151 +
152 +const JsonRecord = Schema.Record(Schema.String, Schema.Unknown);
153 +const decodeJsonRecord = Schema.decodeUnknownSync(Schema.fromJsonString(JsonRecord));
154 +const decodeProcedureBody = Schema.decodeUnknownSync(Schema.Struct({ json: JsonRecord }));
155 +
156 +const lastJson = (lines: ReadonlyArray<string>): Record<string, unknown> => {
157 + const line = lines.findLast(candidate => candidate.trimStart().startsWith('{'));
158 + if (line === undefined) throw new Error('Expected JSON output but none found');
159 + return decodeJsonRecord(line);
160 +};
161 +
162 +const requestJson = (request: DashboardTestRequest | undefined) =>
163 + request === undefined ? undefined : decodeProcedureBody(request.body).json;
164 +
165 +const EXECUTE_GMAIL = [
166 + 'execute',
167 + 'GMAIL_SEND_EMAIL',
168 + '--skip-connection-check',
169 + '-d',
170 + '{"recipient":"a"}',
171 +] as const;
172 +
173 +const walletExhausted = {
174 + message: 'Your wallet balance is too low to run this tool.',
175 + code: 4020,
176 + slug: 'Wallet_InsufficientBalance',
177 + status: 402,
178 + request_id: 'req_wallet',
179 + suggested_fix: 'Add credits in the dashboard, then retry.',
180 +};
181 +
182 +describe('CLI: composio execute transport', () => {
183 + beforeEach(() => {
184 + vi.spyOn(composioAccount, 'getLatestToolVersion').mockImplementation(() =>
185 + Effect.fail(
186 + new APIConnectionError({
187 + message: 'Connection error.',
188 + method: 'GET',
189 + url: 'https://backend.composio.dev/api/v3/tools/{tool_slug}/get_latest_version',
190 + reason: 'TransportError',
191 + description: undefined,
192 + })
193 + )
194 + );
195 + vi.spyOn(consumerShortTermCache, 'getFreshConsumerConnectedToolkitsFromCache').mockReturnValue(
196 + Effect.succeed(Option.some(['gmail', 'github']))
197 + );
198 + vi.spyOn(consumerShortTermCache, 'refreshConsumerConnectedToolkitsCache').mockImplementation(
199 + () => Effect.succeed(['gmail', 'github'])
200 + );
201 + });
202 + afterEach(() => {
203 + vi.restoreAllMocks();
204 + temporaryCacheDirs.forEach(directory => fs.rmSync(directory, { recursive: true, force: true }));
205 + temporaryCacheDirs.length = 0;
206 + });
207 +
208 + it.effect('sends a consumer execution to the Dashboard and prints what the backend prints', () =>
209 + Effect.gen(function* () {
210 + const viaDashboard = makeWorld({});
211 + const viaBackend = makeWorld({ env: backendOnlyEnv });
212 +
213 + const dashboardRun = yield* viaDashboard.run(EXECUTE_GMAIL);
214 + const backendRun = yield* viaBackend.run(EXECUTE_GMAIL);
215 +
216 + expect(viaDashboard.dashboardRequests).toHaveLength(1);
217 + const [request] = viaDashboard.dashboardRequests;
218 + expect(request?.method).toBe('POST');
219 + expect(request?.url).toBe(`${DEFAULT_DASHBOARD}/api/cli/trpc/execute`);
220 + expect(request?.headers['authorization']).toBe('Bearer test_api_key');
221 + expect(request?.headers['x-org-id']).toBe('org_test');
222 + expect(requestJson(request)).toEqual({
223 + tool_slug: 'GMAIL_SEND_EMAIL',
224 + arguments: { recipient: 'a' },
225 + });
226 + // The Dashboard owns the session: none is created or executed on the backend.
227 + expect(viaDashboard.toolRouterCalls).toEqual([]);
228 +
229 + expect(Exit.isSuccess(dashboardRun.exit)).toBe(true);
230 + expect(lastJson(dashboardRun.lines)).toEqual({
231 + successful: true,
232 + data: { id: 'msg_1' },
233 + error: null,
234 + logId: 'log_test',
235 + });
236 + expect(dashboardRun.lines).toEqual(backendRun.lines);
237 + })
238 + );
239 +
240 + describe.each(['GMAIL_SEND_EMAIL', 'COMPOSIO_SEARCH_TOOLS'])('%s', slug => {
241 + describe.each(['failed', 'input_required'] as const)('%s', resultType => {
242 + it.effect(`preserves ${resultType} replies for ${slug} on both transports`, () =>
243 + Effect.gen(function* () {
244 + const response: SessionExecuteResponse =
245 + resultType === 'failed'
246 + ? { result_type: 'failed', data: {}, error: null, log_id: 'log_failed' }
247 + : {
248 + result_type: 'input_required',
249 + input_requests: {
250 + approval: {
251 + type: 'elicitation',
252 + mode: 'form',
253 + message: 'Approve this tool?',
254 + requested_schema: { type: 'object' },
255 + },
256 + },
257 + request_state: 'opaque-state-token',
258 + };
259 + const viaDashboard = makeWorld({
260 + dashboard: () => dashboardProcedureResult({ ok: true, response }),
261 + });
262 + const viaBackend = makeWorld({
263 + env: backendOnlyEnv,
264 + backendExecute: () => Effect.succeed(response),
265 + });
266 + const args = ['execute', slug, '--skip-connection-check', '-d', '{}'];
267 + const dashboardRun = yield* viaDashboard.run(args);
268 + const backendRun = yield* viaBackend.run(args);
269 +
270 + expect(Exit.isFailure(dashboardRun.exit)).toBe(true);
271 + expect(Exit.isFailure(backendRun.exit)).toBe(true);
272 + expect(dashboardRun.lines).toEqual(backendRun.lines);
273 + expect(lastJson(dashboardRun.lines)).toMatchObject({ successful: false });
274 + if (resultType === 'input_required') {
275 + expect(dashboardRun.output).toContain('requires user input before it can run');
276 + expect(dashboardRun.output).not.toContain('opaque-state-token');
277 + } else {
278 + expect(lastJson(dashboardRun.lines)).toMatchObject({
279 + error: null,
280 + logId: 'log_failed',
281 + });
282 + }
283 + expect(viaDashboard.dashboardRequests).toHaveLength(1);
284 + expect(viaDashboard.toolRouterCalls).toEqual([]);
285 + expect(viaBackend.toolRouterCalls.map(call => call.method)).toEqual([
286 + 'create',
287 + slug === 'COMPOSIO_SEARCH_TOOLS' ? 'executeMeta' : 'execute',
288 + ]);
289 + expect(viaBackend.toolRouterCalls.at(-1)?.requestOptions).toBeUndefined();
290 + })
291 + );
292 + });
293 + });
294 +
295 + it.effect('keeps the backend transport for a custom backend with no web URL', () =>
296 + Effect.gen(function* () {
297 + const world = makeWorld({ env: backendOnlyEnv });
298 +
299 + const { exit } = yield* world.run(EXECUTE_GMAIL);
300 +
301 + expect(Exit.isSuccess(exit)).toBe(true);
302 + expect(world.dashboardRequests).toEqual([]);
303 + expect(world.toolRouterCalls.map(call => call.method)).toEqual(['create', 'execute']);
304 + // An execution does not ask for retries, so the client sends it once: a
305 + // retry after the backend already acted would duplicate the side effect.
306 + expect(world.toolRouterCalls.at(-1)?.requestOptions).toBeUndefined();
307 + })
308 + );
309 +
310 + it.effect('never retries a meta tool execution on the backend transport', () =>
311 + Effect.gen(function* () {
312 + const world = makeWorld({ env: backendOnlyEnv });
313 +
314 + yield* world.run(['execute', 'COMPOSIO_SEARCH_TOOLS', '-d', '{"query":"email"}']);
315 +
316 + expect(world.dashboardRequests).toEqual([]);
317 + expect(world.toolRouterCalls.at(-1)).toMatchObject({ method: 'executeMeta' });
318 + expect(world.toolRouterCalls.at(-1)?.requestOptions).toBeUndefined();
319 + })
320 + );
321 +
322 + describe.each(['https://dashboard.internal.example', 'https://dashboard.internal.example/'])(
323 + '%s',
324 + webURL => {
325 + it.effect(`uses the Dashboard at ${webURL} when both URLs are overridden`, () =>
326 + Effect.gen(function* () {
327 + // This is also the environment a `composio run` script's `execute()`
328 + // gives its child CLI: the parent forwards both URLs.
329 + const world = makeWorld({ env: { ...backendOnlyEnv, COMPOSIO_WEB_URL: webURL } });
330 +
331 + yield* world.run(EXECUTE_GMAIL);
332 +
333 + expect(world.dashboardRequests.map(request => request.url)).toEqual([
334 + 'https://dashboard.internal.example/api/cli/trpc/execute',
335 + ]);
336 + expect(world.toolRouterCalls).toEqual([]);
337 + })
338 + );
339 + }
340 + );
341 +
342 + it.effect('sends a meta tool to the executeMeta procedure', () =>
343 + Effect.gen(function* () {
344 + const world = makeWorld({});
345 +
346 + const { exit } = yield* world.run([
347 + 'execute',
348 + 'COMPOSIO_SEARCH_TOOLS',
349 + '-d',
350 + '{"query":"email"}',
351 + ]);
352 +
353 + expect(Exit.isSuccess(exit)).toBe(true);
354 + expect(world.dashboardRequests.map(request => request.url)).toEqual([
355 + `${DEFAULT_DASHBOARD}/api/cli/trpc/executeMeta`,
356 + ]);
357 + expect(requestJson(world.dashboardRequests[0])).toMatchObject({
358 + slug: 'COMPOSIO_SEARCH_TOOLS',
359 + arguments: { query: 'email' },
360 + });
361 + expect(world.toolRouterCalls).toEqual([]);
362 + })
363 + );
364 +
365 + it.effect('sends each parallel tool on its own and keeps the siblings of a failed one', () =>
366 + Effect.gen(function* () {
367 + const world = makeWorld({
368 + dashboard: request =>
369 + requestJson(request)?.tool_slug === 'GITHUB_CREATE_ISSUE'
370 + ? dashboardProcedureResult({
371 + ok: false,
372 + status: 400,
373 + error: {
374 + message: 'Repository not found',
375 + code: 1803,
376 + slug: 'Tool_Failed',
377 + status: 400,
378 + request_id: 'req_parallel',
379 + },
380 + })
381 + : dashboardProcedureResult({
382 + ok: true,
383 + response: {
384 + result_type: 'completed',
385 + data: { ran: requestJson(request)?.tool_slug },
386 + error: null,
387 + log_id: 'log_parallel',
388 + },
389 + }),
390 + });
391 +
392 + const { exit, lines } = yield* world.run([
393 + 'execute',
394 + '--parallel',
395 + '--skip-checks',
396 + 'GMAIL_SEND_EMAIL',
397 + '-d',
398 + '{"recipient":"a"}',
399 + 'GITHUB_CREATE_ISSUE',
400 + '-d',
401 + '{"title":"Bug"}',
402 + 'GMAIL_CREATE_EMAIL_DRAFT',
403 + '-d',
404 + '{"recipient":"b"}',
405 + ]);
406 +
407 + expect(Exit.isFailure(exit)).toBe(true);
408 + expect(
409 + world.dashboardRequests.map(request => requestJson(request)?.tool_slug).sort()
410 + ).toEqual(['GITHUB_CREATE_ISSUE', 'GMAIL_CREATE_EMAIL_DRAFT', 'GMAIL_SEND_EMAIL']);
411 + expect(world.toolRouterCalls).toEqual([]);
412 + expect(lastJson(lines).results).toEqual([
413 + expect.objectContaining({ slug: 'GMAIL_SEND_EMAIL', successful: true }),
414 + { slug: 'GITHUB_CREATE_ISSUE', successful: false, error: 'Repository not found' },
415 + expect.objectContaining({ slug: 'GMAIL_CREATE_EMAIL_DRAFT', successful: true }),
416 + ]);
417 + })
418 + );
419 +
420 + it.effect('keeps `dev playground-execute` on the backend', () =>
421 + Effect.gen(function* () {
422 + const world = makeWorld({});
423 +
424 + const { exit, output } = yield* world.run([
425 + 'dev',
426 + 'playground-execute',
427 + 'GMAIL_SEND_EMAIL',
428 + '--skip-connection-check',
429 + '-d',
430 + '{"recipient":"a"}',
431 + ]);
432 +
433 + expect(output).toContain('msg_1');
434 + expect(Exit.isSuccess(exit)).toBe(true);
435 + expect(world.dashboardRequests).toEqual([]);
436 + expect(world.toolRouterCalls.map(call => call.method)).toEqual(['create', 'execute']);
437 + })
438 + );
439 +
440 + it.effect('resolves the project and uploads files on the backend before the Dashboard call', () =>
441 + Effect.gen(function* () {
442 + const events: Array<string> = [];
443 + const tempFile = path.join(os.tmpdir(), `composio-upload-${crypto.randomUUID()}.txt`);
444 + fs.writeFileSync(tempFile, 'hello from cli upload', 'utf8');
445 + const originalFetch = globalThis.fetch;
446 + vi.spyOn(globalThis, 'fetch').mockImplementation((input, init) => {
447 + const url =
448 + typeof input === 'string' ? input : input instanceof URL ? input.toString() : input.url;
449 + if (url === 'https://s3.test.composio.dev/upload') {
450 + events.push('backend:file-upload');
451 + return Promise.resolve(new Response(null, { status: 200 }));
452 + }
453 + return originalFetch(input, init);
454 + });
455 +
456 + const world = makeWorld({
457 + dashboard: () => {
458 + events.push('dashboard:execute');
459 + return dashboardProcedureResult({
460 + ok: true,
461 + response: { result_type: 'completed', data: {}, error: null, log_id: 'log_upload' },
462 + });
463 + },
464 + input: {
465 + accountData: { onRequest: request => events.push(`backend:${request.operation}`) },
466 + toolkitsData: {
467 + tools: [
468 + {
469 + name: 'Send Email',
470 + slug: 'GMAIL_SEND_EMAIL',
471 + description: 'Send an email with attachment',
472 + tags: ['email'],
473 + available_versions: ['20260316_00'],
474 + input_parameters: {
475 + type: 'object',
476 + properties: {
477 + recipient_email: { type: 'string' },
478 + attachment: { file_uploadable: true, title: 'Attachment' },
479 + },
480 + },
481 + output_parameters: { type: 'object', properties: {} },
482 + },
483 + ],
484 + } satisfies TestLiveInput['toolkitsData'],
485 + },
486 + });
487 +
488 + const { exit } = yield* world.run([
489 + 'execute',
490 + 'GMAIL_SEND_EMAIL',
491 + '-d',
492 + JSON.stringify({ recipient_email: 'a@b.com', attachment: tempFile }),
493 + ]);
494 + fs.rmSync(tempFile, { force: true });
495 +
496 + expect(Exit.isSuccess(exit)).toBe(true);
497 + expect(events).toContain('backend:org.consumer.project.resolve');
498 + expect(events).toContain('backend:file-upload');
499 + expect(events.at(-1)).toBe('dashboard:execute');
500 + expect(events.filter(event => event === 'dashboard:execute')).toHaveLength(1);
501 + // The Dashboard receives the uploaded file reference, not the local path.
502 + expect(requestJson(world.dashboardRequests[0])?.arguments).toEqual({
503 + recipient_email: 'a@b.com',
504 + attachment: {
505 + name: path.basename(tempFile),
506 + mimetype: 'application/octet-stream',
507 + s3key: `uploads/${path.basename(tempFile)}`,
508 + },
509 + });
510 + })
511 + );
512 +
513 + it.effect('sends nothing to the Dashboard when the permission gate denies the tool', () =>
514 + Effect.gen(function* () {
515 + // Enhanced controls ask before every call; with no one to answer the
516 + // prompt (as under test), the gate denies.
517 + vi.spyOn(toolPermissions, 'getConsumerPermissionSnapshot').mockImplementation(params =>
518 + Effect.succeed({
519 + orgId: params.orgId,
520 + projectId: params.projectId,
521 + consumerUserId: params.consumerUserId,
522 + enhancedControlsEnabled: true,
523 + permissions: { default: 'ask_every_call' as const, overrides: {} },
524 + connectedAccountIds: [],
525 + fetchedAt: 1_767_225_600_000,
526 + })
527 + );
528 + const world = makeWorld({});
529 +
530 + const { exit, output } = yield* world.run(EXECUTE_GMAIL);
531 +
532 + expect(Exit.isFailure(exit)).toBe(true);
533 + expect(output).toContain('requires interactive approval');
534 + expect(world.dashboardRequests).toEqual([]);
535 + expect(world.toolRouterCalls).toEqual([]);
536 + })
537 + );
538 +
539 + it.effect('reports a relayed wallet failure exactly as the backend transport does', () =>
540 + Effect.gen(function* () {
541 + const viaDashboard = makeWorld({
542 + dashboard: () =>
543 + dashboardProcedureResult({ ok: false, status: 402, error: walletExhausted }),
544 + });
545 + const viaBackend = makeWorld({
546 + env: backendOnlyEnv,
547 + backendExecute: () =>
548 + Effect.fail(
549 + new APIError({
550 + message: `402 ${JSON.stringify({ error: walletExhausted })}`,
551 + method: 'POST',
552 + url: 'https://mock.invalid/api/v3.1/tool_router/session/{session_id}/execute',
553 + status: 402,
554 + requestId: walletExhausted.request_id,
555 + details: walletExhausted,
556 + body: { error: walletExhausted },
557 + })
558 + ),
559 + });
560 +
561 + const dashboardRun = yield* viaDashboard.run(EXECUTE_GMAIL);
562 + const backendRun = yield* viaBackend.run(EXECUTE_GMAIL);
563 +
564 + expect(Exit.isFailure(dashboardRun.exit)).toBe(true);
565 + expect(dashboardRun.output).toContain(walletExhausted.message);
566 + expect(dashboardRun.output).toContain('Error details');
567 + expect(dashboardRun.output).toContain(walletExhausted.suggested_fix);
568 + expect(lastJson(dashboardRun.lines)).toEqual({
569 + successful: false,
570 + error: walletExhausted.message,
571 + slug: walletExhausted.slug,
572 + });
573 + expect(dashboardRun.lines).toEqual(backendRun.lines);
574 + expect(viaDashboard.dashboardRequests).toHaveLength(1);
575 + })
576 + );
577 +
578 + it.effect('shows the `composio link` tip for a relayed missing-connection error', () =>
579 + Effect.gen(function* () {
580 + const world = makeWorld({
581 + dashboard: () =>
582 + dashboardProcedureResult({
583 + ok: false,
584 + status: 400,
585 + error: {
586 + message: "No active connection found for toolkit(s) 'gmail' in this session",
587 + code: 4302,
588 + slug: 'ToolRouterV2_NoActiveConnection',
589 + status: 400,
590 + request_id: 'req_no_connection',
591 + },
592 + }),
593 + });
594 +
595 + const { exit, output } = yield* world.run(EXECUTE_GMAIL);
596 +
597 + expect(Exit.isFailure(exit)).toBe(true);
598 + expect(output).toContain('Run `composio link gmail`, then retry.');
599 + expect(output).toContain('Tips');
600 + })
601 + );
602 +
603 + it.effect('suggests `composio login` when the Dashboard rejects the credentials', () =>
604 + Effect.gen(function* () {
605 + const world = makeWorld({
606 + dashboard: () =>
607 + dashboardProcedureError({
608 + code: 'UNAUTHORIZED',
609 + httpStatus: 401,
610 + message: 'Invalid API key',
611 + }),
612 + });
613 +
614 + const { exit, lines, output } = yield* world.run(EXECUTE_GMAIL);
615 +
616 + expect(Exit.isFailure(exit)).toBe(true);
617 + expect(output).toContain('Authentication failed: Invalid API key');
618 + expect(output).toContain('composio login');
619 + expect(lastJson(lines)).toMatchObject({ successful: false });
620 + expect(world.dashboardRequests).toHaveLength(1);
621 + expect(world.toolRouterCalls).toEqual([]);
622 + })
623 + );
624 +
625 + // Stored URLs never take effect: the backend client and the Dashboard request
626 + // both use the environment (or the defaults), which is also what the
627 + // transport choice reads. These pin that the three cannot disagree.
628 + const STORED_BACKEND = 'https://stored-backend.internal.example';
629 + const STORED_DASHBOARD = 'https://stored-dashboard.internal.example';
630 + const storedURLCases: ReadonlyArray<{
631 + readonly name: string;
632 + readonly stored: { base_url?: string; web_url?: string };
633 + readonly env: Record<string, string>;
634 + readonly baseURL: string;
635 + /** The one Dashboard origin that receives the key, or none on the backend transport. */
636 + readonly dashboard: string | undefined;
637 + }> = [
638 + {
639 + name: 'a stored custom backend URL and no environment override',
640 + stored: { base_url: STORED_BACKEND },
641 + env: {},
642 + baseURL: 'https://backend.composio.dev',
643 + dashboard: DEFAULT_DASHBOARD,
644 + },
645 + {
646 + name: 'stored custom backend and web URLs and no environment override',
647 + stored: { base_url: STORED_BACKEND, web_url: STORED_DASHBOARD },
648 + env: {},
649 + baseURL: 'https://backend.composio.dev',
650 + dashboard: DEFAULT_DASHBOARD,
651 + },
652 + {
653 + name: 'a custom backend URL in the environment and a stored default web URL',
654 + stored: { web_url: 'https://dashboard.composio.dev/' },
655 + env: backendOnlyEnv,
656 + baseURL: backendOnlyEnv.COMPOSIO_BASE_URL,
657 + dashboard: undefined,
658 + },
659 + {
660 + name: 'a stored web URL that differs from the one in the environment',
661 + stored: { web_url: STORED_DASHBOARD },
662 + env: { COMPOSIO_WEB_URL: 'https://env-dashboard.internal.example' },
663 + baseURL: 'https://backend.composio.dev',
664 + dashboard: 'https://env-dashboard.internal.example',
665 + },
666 + ];
667 +
668 + describe.each(storedURLCases)('$name', testCase => {
669 + it.effect(`sends the key to one consistent environment given ${testCase.name}`, () =>
670 + Effect.gen(function* () {
671 + const world = makeWorld({
672 + env: { ...testCase.env, COMPOSIO_CACHE_DIR: cacheDirWithStoredURLs(testCase.stored) },
673 + });
674 +
675 + const { exit, baseURL, webURL } = yield* world.run(EXECUTE_GMAIL);
676 +
677 + expect(Exit.isSuccess(exit)).toBe(true);
678 + expect(baseURL).toBe(testCase.baseURL);
679 + expect(webURL).not.toContain('stored-dashboard');
680 + if (testCase.dashboard === undefined) {
681 + expect(world.dashboardRequests).toEqual([]);
682 + expect(world.toolRouterCalls.map(call => call.method)).toEqual(['create', 'execute']);
683 + } else {
684 + expect(
685 + world.dashboardRequests.map(request => [
686 + request.url,
687 + request.headers['authorization'],
688 + request.headers['x-org-id'],
689 + ])
690 + ).toEqual([
691 + [`${testCase.dashboard}/api/cli/trpc/execute`, 'Bearer test_api_key', 'org_stored'],
692 + ]);
693 + expect(world.toolRouterCalls).toEqual([]);
694 + }
695 + })
696 + );
697 + });
698 +
699 + const unsafeWebURLs: ReadonlyArray<{ readonly name: string; readonly webURL: string }> = [
700 + { name: 'plain http to a remote host', webURL: 'http://dashboard.internal.example' },
701 + {
702 + name: 'credentials, a query string and a fragment',
703 + webURL: 'https://user:s3cret-pass@dashboard.internal.example/?token=s3cret-token#s3cret-frag',
704 + },
705 + ];
706 +
707 + describe.each(unsafeWebURLs)('$name', testCase => {
708 + it.effect(`refuses a web URL with ${testCase.name} without sending or echoing it`, () =>
709 + Effect.gen(function* () {
710 + const world = makeWorld({ env: { COMPOSIO_WEB_URL: testCase.webURL } });
711 +
712 + const { exit, lines, output } = yield* world.run(EXECUTE_GMAIL);
713 +
714 + expect(Exit.isFailure(exit)).toBe(true);
715 + expect(world.dashboardRequests).toEqual([]);
716 + expect(world.toolRouterCalls).toEqual([]);
717 + // Covers both the human lines and the JSON summary.
718 + expect(output).toContain('COMPOSIO_WEB_URL');
719 + expect(output).not.toContain('s3cret');
720 + expect(output).not.toContain('dashboard.internal.example');
721 + expect(lastJson(lines)).toMatchObject({ successful: false });
722 + })
723 + );
724 + });
725 +
726 + it.effect('reports a Dashboard timeout once, saying the tool may still have run', () =>
727 + Effect.gen(function* () {
728 + const world = makeWorld({ dashboard: () => new Promise<Response>(() => undefined) });
729 +
730 + const fiber = yield* Effect.forkChild(world.run(EXECUTE_GMAIL));
731 + // Let the command reach the Dashboard call, then pass the deadline.
732 + yield* Effect.promise(() =>
733 + vi.waitFor(() => expect(world.dashboardRequests).toHaveLength(1))
734 + );
735 + yield* TestClock.adjust('15 minutes');
736 + const { exit, output } = yield* Fiber.join(fiber);
737 +
738 + expect(Exit.isFailure(exit)).toBe(true);
739 + expect(output).toContain('did not answer within 15 minutes');
740 + expect(output).toContain('The tool may still have run; check before running it again.');
741 + expect(world.dashboardRequests).toHaveLength(1);
742 + expect(world.toolRouterCalls).toEqual([]);
743 + })
744 + );
745 +
746 + const unreachable: ReadonlyArray<{
747 + readonly name: string;
748 + readonly dashboard: () => Response | Promise<Response>;
749 + }> = [
750 + {
751 + name: 'the Dashboard cannot be reached',
752 + dashboard: () => Promise.reject(new Error('connect ECONNREFUSED')),
753 + },
754 + {
755 + name: 'the Dashboard answers with something other than JSON',
756 + dashboard: () => new Response('<html>Bad gateway</html>', { status: 502 }),
757 + },
758 + ];
759 +
760 + describe.each(unreachable)('$name', testCase => {
761 + it.effect(`names the Dashboard and does not resend or fall back when ${testCase.name}`, () =>
762 + Effect.gen(function* () {
763 + const world = makeWorld({ dashboard: testCase.dashboard });
764 +
765 + const { exit, output } = yield* world.run(EXECUTE_GMAIL);
766 +
767 + expect(Exit.isFailure(exit)).toBe(true);
768 + expect(output).toContain('dashboard.composio.dev');
769 + expect(output).toContain('was not retried');
770 + expect(world.dashboardRequests).toHaveLength(1);
771 + expect(world.toolRouterCalls).toEqual([]);
772 + })
773 + );
774 + });
775 +});
ts/packages/cli/test/src/services/composio-client-interrupt.test.ts
+9 -1
@@ -3,10 +3,12 @@ import { vi } from 'vitest';
3 import * as BunFileSystem from '@effect/platform-bun/BunFileSystem';
4 import * as BunPath from '@effect/platform-bun/BunPath';
5 import { ConfigProvider, Effect, Fiber, Layer } from 'effect';
6 +import { FetchHttpClient } from 'effect/http';
7 import * as tempy from 'tempy';
8 import { createToolRouterSessionContext } from 'src/effects/create-tool-router-session';
9 import { ComposioCliUserConfigLive } from 'src/services/cli-user-config';
10 import { ComposioClientSingleton, ComposioToolkitsRepository } from 'src/services/composio-clients';
11 +import { DashboardToolExecution } from 'src/services/dashboard-tool-execution';
12 import { defaultNodeOs, NodeOs } from 'src/services/node-os';
13 import { NodeProcess } from 'src/services/node-process';
14 import { cliDebugFlagsLayer } from 'src/services/runtime-flags';
@@ -59,8 +61,13 @@ const withExecutor = () => {
61 const singleton = Layer.provide(ComposioClientSingleton.Default, platform);
62 const repository = Layer.provide(ComposioToolkitsRepository.Default, platform);
63 const catalog = Layer.provide(ToolkitSlugCatalog.Default, Layer.mergeAll(repository, platform));
64 + // Never called here: every execution in this file targets the backend.
65 + const dashboard = Layer.provide(
66 + DashboardToolExecution.Default,
67 + Layer.mergeAll(Layer.provide(ComposioUserContextLive, platform), FetchHttpClient.layer)
68 + );
69 return Layer.mergeAll(
63 - Layer.provide(ToolsExecutorLive, Layer.mergeAll(singleton, catalog)),
70 + Layer.provide(ToolsExecutorLive, Layer.mergeAll(singleton, catalog, dashboard)),
71 singleton,
72 repository,
73 platform,
@@ -161,6 +168,7 @@ describe('interrupting a single-shot client request', () => {
168 const fiber = yield* Effect.forkChild(
169 executor.execute('COMPOSIO_SEARCH_TOOLS', {
170 userId: 'default',
171 + target: { kind: 'backend' },
172 arguments: { queries: [{ use_case: 'send an email' }] },
173 })
174 );
ts/packages/cli/test/src/services/dashboard-tool-execution.test.ts new
+672
@@ -0,0 +1,672 @@
1 +import * as fs from 'node:fs';
2 +import * as os from 'node:os';
3 +import * as path from 'node:path';
4 +import * as BunFileSystem from '@effect/platform-bun/BunFileSystem';
5 +import * as BunPath from '@effect/platform-bun/BunPath';
6 +import { describe, expect, it } from '@effect/vitest';
7 +import { afterEach, vi } from 'vitest';
8 +import { isAPIError } from '@composio/client/effect';
9 +import { ConfigProvider, Deferred, Effect, Fiber, Layer, Option } from 'effect';
10 +import { TestClock } from 'effect/testing';
11 +import {
12 + FetchHttpClient,
13 + HttpClient,
14 + HttpClientError,
15 + HttpClientResponse,
16 + type HttpClientRequest,
17 +} from 'effect/http';
18 +import {
19 + DashboardToolExecution,
20 + DashboardToolExecutionError,
21 + resolveConsumerExecutionTransport,
22 +} from 'src/services/dashboard-tool-execution';
23 +import { cliRequestHeaders } from 'src/services/client-provenance';
24 +import { defaultNodeOs, NodeOs } from 'src/services/node-os';
25 +import { ComposioUserContext } from 'src/services/user-context';
26 +import { extractApiErrorDetails } from 'src/utils/api-error-extraction';
27 +
28 +interface RecordedRequest {
29 + readonly method: string;
30 + readonly url: string;
31 + readonly headers: Record<string, string>;
32 + readonly body: unknown;
33 + readonly signal: AbortSignal;
34 +}
35 +
36 +type Respond = (
37 + request: HttpClientRequest.HttpClientRequest
38 +) => Effect.Effect<Response, HttpClientError.HttpClientError>;
39 +
40 +const jsonResponse = (status: number, body: unknown) =>
41 + new Response(JSON.stringify(body), {
42 + status,
43 + headers: { 'content-type': 'application/json' },
44 + });
45 +
46 +const succeedWith =
47 + (outcome: unknown): Respond =>
48 + () =>
49 + Effect.succeed(jsonResponse(200, { result: { data: { json: outcome } } }));
50 +
51 +const requestBody = (request: HttpClientRequest.HttpClientRequest): unknown =>
52 + request.body._tag === 'Uint8Array'
53 + ? JSON.parse(new TextDecoder().decode(request.body.body))
54 + : null;
55 +
56 +/** The filesystem the service reads the analytics state from, rooted at `home`. */
57 +const platform = (home: string) =>
58 + Layer.mergeAll(
59 + BunFileSystem.layer,
60 + BunPath.layer,
61 + Layer.succeed(NodeOs, defaultNodeOs({ homedir: home }))
62 + );
63 +type Platform = Layer.Success<ReturnType<typeof platform>>;
64 +
65 +const temporaryHomes: Array<string> = [];
66 +const emptyHome = () => {
67 + const home = fs.mkdtempSync(path.join(os.tmpdir(), 'composio-cli-test-home-'));
68 + temporaryHomes.push(home);
69 + return home;
70 +};
71 +
72 +afterEach(() => {
73 + temporaryHomes.forEach(home => fs.rmSync(home, { recursive: true, force: true }));
74 + temporaryHomes.length = 0;
75 +});
76 +
77 +const userContext = (
78 + overrides: { webURL?: string; apiKey?: Option.Option<string>; home?: string } = {}
79 +) =>
80 + Layer.succeed(
81 + ComposioUserContext,
82 + ComposioUserContext.of({
83 + data: {
84 + apiKey: overrides.apiKey ?? Option.some('uak_test_key'),
85 + baseURL: 'https://backend.example.test',
86 + webURL: overrides.webURL ?? 'https://dashboard.example.test/',
87 + orgId: Option.some('org_test'),
88 + projectId: Option.none(),
89 + testUserId: Option.none(),
90 + },
91 + isLoggedIn: () => true,
92 + logout: Effect.void,
93 + login: () => Effect.void,
94 + update: () => Effect.void,
95 + })
96 + );
97 +
98 +/** Runs `use` against the real service over a recording HTTP client. */
99 +const withDashboard = <A, E>(
100 + respond: Respond,
101 + use: (
102 + dashboard: DashboardToolExecution['Service'],
103 + requests: ReadonlyArray<RecordedRequest>
104 + ) => Effect.Effect<A, E, Platform>,
105 + overrides?: Parameters<typeof userContext>[0]
106 +) => {
107 + const requests: Array<RecordedRequest> = [];
108 + const httpClient = HttpClient.make((request, url, signal) => {
109 + requests.push({
110 + method: request.method,
111 + url: url.toString(),
112 + headers: { ...request.headers },
113 + body: requestBody(request),
114 + signal,
115 + });
116 + return respond(request).pipe(
117 + Effect.map(response => HttpClientResponse.fromWeb(request, response))
118 + );
119 + });
120 +
121 + return Effect.gen(function* () {
122 + const dashboard = yield* DashboardToolExecution;
123 + return yield* use(dashboard, requests);
124 + }).pipe(
125 + Effect.provide(
126 + DashboardToolExecution.Default.pipe(
127 + Layer.provide(
128 + Layer.mergeAll(Layer.succeed(HttpClient.HttpClient, httpClient), userContext(overrides))
129 + )
130 + )
131 + ),
132 + Effect.provide(platform(overrides?.home ?? emptyHome())),
133 + Effect.provide(
134 + ConfigProvider.layer(
135 + ConfigProvider.fromEnv({
136 + env: Object.fromEntries(
137 + Object.entries(process.env).filter(
138 + (entry): entry is [string, string] => entry[1] !== undefined
139 + )
140 + ),
141 + })
142 + )
143 + )
144 + );
145 +};
146 +
147 +const successOutcome = {
148 + ok: true,
149 + response: {
150 + result_type: 'completed',
151 + data: { id: 'msg_1' },
152 + error: null,
153 + log_id: 'log_dashboard',
154 + },
155 +};
156 +
157 +const executeRequest = {
158 + slug: 'GMAIL_SEND_EMAIL',
159 + arguments: { recipient: 'a@example.com' },
160 + orgId: 'org_test',
161 +};
162 +
163 +describe('DashboardToolExecution', () => {
164 + it.effect('posts a tool execution to the execute procedure and returns the relayed body', () =>
165 + withDashboard(succeedWith(successOutcome), (dashboard, requests) =>
166 + Effect.gen(function* () {
167 + const response = yield* dashboard.execute(executeRequest);
168 +
169 + expect(response).toEqual({
170 + result_type: 'completed',
171 + data: { id: 'msg_1' },
172 + error: null,
173 + log_id: 'log_dashboard',
174 + });
175 + expect(requests).toHaveLength(1);
176 + const [request] = requests;
177 + expect(request?.method).toBe('POST');
178 + expect(request?.url).toBe('https://dashboard.example.test/api/cli/trpc/execute');
179 + expect(request?.headers['authorization']).toBe('Bearer uak_test_key');
180 + expect(request?.headers['x-org-id']).toBe('org_test');
181 + expect(request?.headers['content-type']).toBe('application/json');
182 + expect(request?.headers['x-source']).toBe('CLI');
183 + expect(request?.headers['x-user-api-key']).toBeUndefined();
184 + // Telemetry is off under test unless a case turns it on.
185 + expect(request?.headers['x-cli-install-id']).toBeUndefined();
186 + expect(request?.body).toEqual({
187 + json: {
188 + tool_slug: 'GMAIL_SEND_EMAIL',
189 + arguments: { recipient: 'a@example.com' },
190 + },
191 + });
192 + })
193 + )
194 + );
195 +
196 + it.effect('sends the selected connected account with the execution, and omits it otherwise', () =>
197 + withDashboard(succeedWith(successOutcome), (dashboard, requests) =>
198 + Effect.gen(function* () {
199 + yield* dashboard.execute({ ...executeRequest, account: 'ca_1' });
200 + yield* dashboard.execute(executeRequest);
201 +
202 + expect(requests[0]?.body).toEqual({
203 + json: {
204 + tool_slug: 'GMAIL_SEND_EMAIL',
205 + arguments: { recipient: 'a@example.com' },
206 + account: 'ca_1',
207 + },
208 + });
209 + expect(requests[1]?.body).toEqual({
210 + json: { tool_slug: 'GMAIL_SEND_EMAIL', arguments: { recipient: 'a@example.com' } },
211 + });
212 + })
213 + )
214 + );
215 +
216 + it.effect('posts a meta tool to the executeMeta procedure under `slug`', () =>
217 + withDashboard(succeedWith(successOutcome), (dashboard, requests) =>
218 + Effect.gen(function* () {
219 + yield* dashboard.executeMeta({
220 + slug: 'COMPOSIO_SEARCH_TOOLS',
221 + arguments: { query: 'email' },
222 + orgId: 'org_test',
223 + });
224 +
225 + expect(requests[0]?.url).toBe('https://dashboard.example.test/api/cli/trpc/executeMeta');
226 + expect(requests[0]?.body).toEqual({
227 + json: { slug: 'COMPOSIO_SEARCH_TOOLS', arguments: { query: 'email' } },
228 + });
229 + })
230 + )
231 + );
232 +
233 + describe.each(['https://web.example.test', 'https://web.example.test/'])('%s', webURL => {
234 + it.effect(`builds the same request URL from ${webURL}`, () =>
235 + withDashboard(
236 + succeedWith(successOutcome),
237 + (dashboard, requests) =>
238 + Effect.gen(function* () {
239 + yield* dashboard.execute(executeRequest);
240 + expect(requests[0]?.url).toBe('https://web.example.test/api/cli/trpc/execute');
241 + }),
242 + { webURL }
243 + )
244 + );
245 + });
246 +
247 + it.effect('fails a relayed backend failure with the client status error', () =>
248 + withDashboard(
249 + succeedWith({
250 + ok: false,
251 + status: 402,
252 + error: {
253 + message: 'Insufficient wallet balance',
254 + code: 4020,
255 + slug: 'Wallet_InsufficientBalance',
256 + status: 402,
257 + request_id: 'req_wallet',
258 + suggested_fix: 'Add credits and retry.',
259 + },
260 + }),
261 + dashboard =>
262 + Effect.gen(function* () {
263 + const failure = yield* dashboard.execute(executeRequest).pipe(Effect.flip);
264 +
265 + // The client's own status class for a 402, as the backend transport fails with.
266 + expect(isAPIError(failure) && failure._tag).toBe('@composio/client/APIError');
267 + expect(extractApiErrorDetails(failure)).toEqual({
268 + message: 'Insufficient wallet balance',
269 + code: 4020,
270 + slug: 'Wallet_InsufficientBalance',
271 + status: 402,
272 + request_id: 'req_wallet',
273 + suggested_fix: 'Add credits and retry.',
274 + });
275 + })
276 + )
277 + );
278 +
279 + it.effect('suggests logging in when the Dashboard rejects the credentials', () =>
280 + withDashboard(
281 + () =>
282 + Effect.succeed(
283 + jsonResponse(401, {
284 + error: {
285 + json: {
286 + message: 'Invalid API key',
287 + data: { code: 'UNAUTHORIZED', httpStatus: 401 },
288 + },
289 + },
290 + })
291 + ),
292 + (dashboard, requests) =>
293 + Effect.gen(function* () {
294 + const failure = yield* dashboard.execute(executeRequest).pipe(Effect.flip);
295 +
296 + expect(failure).toBeInstanceOf(DashboardToolExecutionError);
297 + expect(failure).toMatchObject({ reason: 'unauthorized', status: 401 });
298 + expect(failure.message).toContain('Invalid API key');
299 + expect(failure.message).toContain('composio login');
300 + expect(requests).toHaveLength(1);
301 + })
302 + )
303 + );
304 +
305 + it.effect("reports the Dashboard's own message for its other failures", () =>
306 + withDashboard(
307 + () =>
308 + Effect.succeed(
309 + jsonResponse(502, {
310 + error: {
311 + json: {
312 + message: 'Tool execution is temporarily unavailable.',
313 + data: { code: 'BAD_GATEWAY', httpStatus: 502 },
314 + },
315 + },
316 + })
317 + ),
318 + (dashboard, requests) =>
319 + Effect.gen(function* () {
320 + const failure = yield* dashboard.execute(executeRequest).pipe(Effect.flip);
321 +
322 + expect(failure).toMatchObject({
323 + reason: 'dashboard',
324 + status: 502,
325 + code: 'BAD_GATEWAY',
326 + message: 'Tool execution is temporarily unavailable.',
327 + });
328 + expect(requests).toHaveLength(1);
329 + })
330 + )
331 + );
332 +
333 + it.effect('names the Dashboard host on a network failure and sends the request once', () =>
334 + withDashboard(
335 + request =>
336 + Effect.fail(
337 + new HttpClientError.HttpClientError({
338 + reason: new HttpClientError.TransportError({
339 + request,
340 + cause: new Error('connect ECONNREFUSED'),
341 + }),
342 + })
343 + ),
344 + (dashboard, requests) =>
345 + Effect.gen(function* () {
346 + const failure = yield* dashboard.execute(executeRequest).pipe(Effect.flip);
347 +
348 + expect(failure).toMatchObject({ reason: 'request' });
349 + expect(failure.message).toContain('dashboard.example.test');
350 + expect(requests).toHaveLength(1);
351 + })
352 + )
353 + );
354 +
355 + it.effect('names the Dashboard host when the response is not JSON', () =>
356 + withDashboard(
357 + () => Effect.succeed(new Response('<html>Bad gateway</html>', { status: 502 })),
358 + (dashboard, requests) =>
359 + Effect.gen(function* () {
360 + const failure = yield* dashboard.execute(executeRequest).pipe(Effect.flip);
361 +
362 + expect(failure).toMatchObject({ reason: 'response', status: 502 });
363 + expect(failure.message).toContain('dashboard.example.test');
364 + expect(requests).toHaveLength(1);
365 + })
366 + )
367 + );
368 +
369 + it.effect('sends nothing when no user API key is stored', () =>
370 + withDashboard(
371 + succeedWith(successOutcome),
372 + (dashboard, requests) =>
373 + Effect.gen(function* () {
374 + const failure = yield* dashboard.execute(executeRequest).pipe(Effect.flip);
375 +
376 + expect(failure).toMatchObject({ reason: 'unauthorized' });
377 + expect(failure.message).toContain('composio login');
378 + expect(requests).toHaveLength(0);
379 + }),
380 + { apiKey: Option.none() }
381 + )
382 + );
383 +
384 + it.effect('tells fetch to fail on a redirect instead of re-posting the execution', () =>
385 + Effect.gen(function* () {
386 + const inits: Array<RequestInit | undefined> = [];
387 + const fetchStub: typeof globalThis.fetch = Object.assign(
388 + (_input: RequestInfo | URL, init?: RequestInit) => {
389 + inits.push(init);
390 + // What fetch does with `redirect: 'error'` when the server answers 307.
391 + return Promise.reject(new TypeError('unexpected redirect'));
392 + },
393 + { preconnect: () => undefined }
394 + );
395 +
396 + const failure = yield* Effect.gen(function* () {
397 + const dashboard = yield* DashboardToolExecution;
398 + return yield* dashboard.execute(executeRequest).pipe(Effect.flip);
399 + }).pipe(
400 + Effect.provide(
401 + DashboardToolExecution.Default.pipe(
402 + Layer.provide(Layer.mergeAll(FetchHttpClient.layer, userContext()))
403 + )
404 + ),
405 + Effect.provideService(FetchHttpClient.Fetch, fetchStub),
406 + Effect.provide(platform(emptyHome()))
407 + );
408 +
409 + expect(inits).toHaveLength(1);
410 + expect(inits[0]?.redirect).toBe('error');
411 + expect(inits[0]?.method).toBe('POST');
412 + expect(failure).toMatchObject({ reason: 'request' });
413 + })
414 + );
415 +
416 + const rejectedURLs: ReadonlyArray<{ readonly name: string; readonly webURL: string }> = [
417 + { name: 'plain http to a remote host', webURL: 'http://dashboard.example.test' },
418 + { name: 'embedded credentials', webURL: 'https://user:s3cret-pass@dashboard.example.test' },
419 + { name: 'a query string', webURL: 'https://dashboard.example.test/?token=s3cret-token' },
420 + { name: 'a fragment', webURL: 'https://dashboard.example.test/#s3cret-fragment' },
421 + { name: 'a non-http scheme', webURL: 'ftp://dashboard.example.test' },
422 + { name: 'an unparseable value', webURL: 'not a url s3cret-value' },
423 + ];
424 +
425 + describe.each(rejectedURLs)('$name', testCase => {
426 + it.effect(`sends nothing to a Dashboard URL with ${testCase.name}`, () =>
427 + withDashboard(
428 + succeedWith(successOutcome),
429 + (dashboard, requests) =>
430 + Effect.gen(function* () {
431 + const failure = yield* dashboard.execute(executeRequest).pipe(Effect.flip);
432 +
433 + expect(failure).toMatchObject({ reason: 'configuration' });
434 + expect(failure.message).toContain('COMPOSIO_WEB_URL');
435 + expect(failure.message).not.toContain('s3cret');
436 + expect(failure.message).not.toContain('example.test');
437 + expect(requests).toHaveLength(0);
438 + }),
439 + { webURL: testCase.webURL }
440 + )
441 + );
442 + });
443 +
444 + describe.each(['http://localhost:3000', 'http://127.0.0.1:3000/', 'http://[::1]:3000'])(
445 + '%s',
446 + webURL => {
447 + it.effect(`allows plain http for the loopback host in ${webURL}`, () =>
448 + withDashboard(
449 + succeedWith(successOutcome),
450 + (dashboard, requests) =>
451 + Effect.gen(function* () {
452 + yield* dashboard.execute(executeRequest);
453 + expect(requests.map(request => request.url)).toEqual([
454 + `${webURL.replace(/\/$/, '')}/api/cli/trpc/execute`,
455 + ]);
456 + }),
457 + { webURL }
458 + )
459 + );
460 + }
461 + );
462 +
463 + it.effect('names only the origin of the Dashboard in a transport error', () =>
464 + withDashboard(
465 + () => Effect.succeed(new Response('<html>Bad gateway</html>', { status: 502 })),
466 + dashboard =>
467 + Effect.gen(function* () {
468 + const failure = yield* dashboard.execute(executeRequest).pipe(Effect.flip);
469 +
470 + expect(failure.message).toContain('https://dashboard.example.test ');
471 + expect(failure.message).not.toContain('tenant-path');
472 + }),
473 + { webURL: 'https://dashboard.example.test/tenant-path/' }
474 + )
475 + );
476 +
477 + it.effect('gives up after the deadline, aborts the request and does not resend it', () =>
478 + Effect.gen(function* () {
479 + const started = yield* Deferred.make<void>();
480 +
481 + yield* withDashboard(
482 + () => Deferred.succeed(started, undefined).pipe(Effect.andThen(Effect.never)),
483 + (dashboard, requests) =>
484 + Effect.gen(function* () {
485 + const fiber = yield* Effect.forkChild(
486 + dashboard.execute(executeRequest).pipe(Effect.flip)
487 + );
488 + yield* Deferred.await(started);
489 +
490 + yield* TestClock.adjust('14 minutes');
491 + expect(requests[0]?.signal.aborted).toBe(false);
492 + yield* TestClock.adjust('1 minute');
493 + const failure = yield* Fiber.join(fiber);
494 +
495 + expect(failure).toMatchObject({ reason: 'timeout' });
496 + expect(failure.message).toContain('may still have run');
497 + expect(failure.message).toContain('check before running it again');
498 + expect(requests).toHaveLength(1);
499 + expect(requests[0]?.signal.aborted).toBe(true);
500 + })
501 + );
502 + })
503 + );
504 +
505 + it.effect('aborts the in-flight request when the calling fiber is interrupted', () =>
506 + Effect.gen(function* () {
507 + const started = yield* Deferred.make<void>();
508 +
509 + yield* withDashboard(
510 + () => Deferred.succeed(started, undefined).pipe(Effect.andThen(Effect.never)),
511 + (dashboard, requests) =>
512 + Effect.gen(function* () {
513 + const fiber = yield* Effect.forkChild(dashboard.execute(executeRequest));
514 + yield* Deferred.await(started);
515 + expect(requests[0]?.signal.aborted).toBe(false);
516 +
517 + yield* Fiber.interrupt(fiber);
518 +
519 + expect(requests[0]?.signal.aborted).toBe(true);
520 + expect(requests).toHaveLength(1);
521 + })
522 + );
523 + })
524 + );
525 +});
526 +
527 +describe('DashboardToolExecution install ID header', () => {
528 + const INSTALL_ID = '6f1d2c1e-8a4b-4c0e-9f55-2f0d6d1b7a31';
529 +
530 + afterEach(() => {
531 + vi.unstubAllEnvs();
532 + });
533 +
534 + /** The environment in which the CLI sends its own analytics events. */
535 + const enableTelemetry = () => {
536 + vi.stubEnv('NODE_ENV', 'development');
537 + vi.stubEnv('CI', 'false');
538 + vi.stubEnv('COMPOSIO_CLI_TELEMETRY_DISABLED', 'false');
539 + vi.stubEnv('TELEMETRY_DISABLED', 'false');
540 + vi.stubEnv('COMPOSIO_DISABLE_TELEMETRY', 'false');
541 + vi.stubEnv('COMPOSIO_POSTHOG_PROJECT_API_KEY', 'phc_test_key');
542 + };
543 +
544 + const analyticsStatePath = (home: string) => path.join(home, '.composio', 'analytics.json');
545 +
546 + const homeWithAnalyticsState = (contents: string) => {
547 + const home = emptyHome();
548 + fs.mkdirSync(path.join(home, '.composio'), { recursive: true });
549 + fs.writeFileSync(analyticsStatePath(home), contents);
550 + return home;
551 + };
552 +
553 + const sentInstallId = (home: string) =>
554 + withDashboard(
555 + succeedWith(successOutcome),
556 + (dashboard, requests) =>
557 + Effect.gen(function* () {
558 + const response = yield* dashboard.execute(executeRequest);
559 + yield* dashboard.executeMeta({ ...executeRequest, slug: 'COMPOSIO_SEARCH_TOOLS' });
560 + expect(response.log_id).toBe('log_dashboard');
561 + expect(requests).toHaveLength(2);
562 + return requests.map(request => request.headers['x-cli-install-id']);
563 + }),
564 + { home }
565 + );
566 +
567 + it.effect('sends the stored install ID verbatim on both procedures when telemetry is on', () =>
568 + Effect.gen(function* () {
569 + enableTelemetry();
570 + const home = homeWithAnalyticsState(
571 + JSON.stringify({ install_id: INSTALL_ID, apollo_user_id: 'user_linked' })
572 + );
573 +
574 + expect(yield* sentInstallId(home)).toEqual([INSTALL_ID, INSTALL_ID]);
575 + })
576 + );
577 +
578 + describe.each([
579 + 'COMPOSIO_CLI_TELEMETRY_DISABLED',
580 + 'TELEMETRY_DISABLED',
581 + 'COMPOSIO_DISABLE_TELEMETRY',
582 + 'CI',
583 + ])('%s', optOut => {
584 + it.effect(`omits the header when ${optOut} opts out of telemetry`, () =>
585 + Effect.gen(function* () {
586 + enableTelemetry();
587 + vi.stubEnv(optOut, 'true');
588 + const home = homeWithAnalyticsState(JSON.stringify({ install_id: INSTALL_ID }));
589 +
590 + expect(yield* sentInstallId(home)).toEqual([undefined, undefined]);
591 + })
592 + );
593 + });
594 +
595 + it.effect('omits the header and creates no install ID when none is stored', () =>
596 + Effect.gen(function* () {
597 + enableTelemetry();
598 + const home = emptyHome();
599 +
600 + expect(yield* sentInstallId(home)).toEqual([undefined, undefined]);
601 + expect(fs.existsSync(analyticsStatePath(home))).toBe(false);
602 + })
603 + );
604 +
605 + it.effect('still executes, without the header, when the analytics state is unreadable', () =>
606 + Effect.gen(function* () {
607 + enableTelemetry();
608 + const home = homeWithAnalyticsState('{ not json');
609 +
610 + expect(yield* sentInstallId(home)).toEqual([undefined, undefined]);
611 + })
612 + );
613 +
614 + it('is not part of the headers shared with backend requests', () => {
615 + enableTelemetry();
616 + expect(Object.keys(cliRequestHeaders())).not.toContain('x-cli-install-id');
617 + });
618 +});
619 +
620 +describe('resolveConsumerExecutionTransport', () => {
621 + const resolveWith = (env: Record<string, string>) =>
622 + resolveConsumerExecutionTransport.pipe(
623 + Effect.provide(ConfigProvider.layer(ConfigProvider.fromEnv({ env })))
624 + );
625 +
626 + const cases: ReadonlyArray<{
627 + readonly name: string;
628 + readonly env: Record<string, string>;
629 + readonly expected: 'dashboard' | 'backend';
630 + }> = [
631 + { name: 'no overrides', env: {}, expected: 'dashboard' },
632 + {
633 + name: 'the staging environment',
634 + env: { COMPOSIO_ENVIRONMENT: 'staging' },
635 + expected: 'dashboard',
636 + },
637 + {
638 + name: 'the default backend URL spelled out',
639 + env: { COMPOSIO_BASE_URL: 'https://backend.composio.dev/' },
640 + expected: 'dashboard',
641 + },
642 + {
643 + name: 'a custom backend URL without a web URL',
644 + env: { COMPOSIO_BASE_URL: 'https://composio.internal.example' },
645 + expected: 'backend',
646 + },
647 + {
648 + name: 'a backend URL that does not match the selected environment',
649 + env: {
650 + COMPOSIO_ENVIRONMENT: 'staging',
651 + COMPOSIO_BASE_URL: 'https://backend.composio.dev',
652 + },
653 + expected: 'backend',
654 + },
655 + {
656 + name: 'a custom backend URL with a web URL',
657 + env: {
658 + COMPOSIO_BASE_URL: 'https://composio.internal.example',
659 + COMPOSIO_WEB_URL: 'https://dashboard.internal.example',
660 + },
661 + expected: 'dashboard',
662 + },
663 + ];
664 +
665 + describe.each(cases)('$name', testCase => {
666 + it.effect(`uses the ${testCase.expected} transport for ${testCase.name}`, () =>
667 + Effect.gen(function* () {
668 + expect(yield* resolveWith(testCase.env)).toBe(testCase.expected);
669 + })
670 + );
671 + });
672 +});