Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

next
ts 121 lines 4.98 KB
Raw
1 import type { ConnectedAccountListResponse as ApiConnectedAccountListResponse } from '@composio/client/resources/connected-accounts';
2 import { Data, Effect, Match, Schema, type SchemaIssue } from 'effect';
3 import {
4 ConnectedAccountItems,
5 ConnectedAccountListResponse,
6 isKnownConnectedAccountStatus,
7 } from 'src/models/connected-accounts';
8 import { TerminalUI } from 'src/services/terminal-ui';
9
10 /**
11 * Flattens a schema issue tree into `path: kind` pairs only — never the
12 * failing values. The built-in formatters may echo reported input, and a
13 * container-level failure would otherwise print the raw payload, which can
14 * include credential-bearing fields (`state`, `data`, ...) the
15 * connected-account schemas deliberately exclude.
16 */
17 const collectIssueKinds = (
18 issue: SchemaIssue.Issue,
19 path: ReadonlyArray<PropertyKey>
20 ): ReadonlyArray<string> =>
21 Match.value(issue).pipe(
22 Match.tag('Pointer', pointer => collectIssueKinds(pointer.issue, [...path, ...pointer.path])),
23 Match.tag('Composite', 'AnyOf', composite =>
24 composite.issues.flatMap(inner => collectIssueKinds(inner, path))
25 ),
26 Match.tag('Filter', 'Encoding', wrapper => collectIssueKinds(wrapper.issue, path)),
27 Match.orElse(leaf => [`${path.map(String).join('.') || '(root)'}: ${leaf._tag}`])
28 );
29
30 const formatParseErrorRedacted = (error: Schema.SchemaError): string =>
31 collectIssueKinds(error.issue, []).join(', ');
32
33 /**
34 * Warns when the server returned `status` values newer than this CLI build's
35 * known set. Status values are non-credential by design, so printing them
36 * verbatim is safe — and exactly what the user needs to act on.
37 */
38 const warnOnUnknownStatuses = (
39 items: ReadonlyArray<{ readonly status: string }>
40 ): Effect.Effect<void, never, TerminalUI> =>
41 Effect.gen(function* () {
42 const unknown = [
43 ...new Set(items.map(item => item.status).filter(s => !isKnownConnectedAccountStatus(s))),
44 ];
45 if (unknown.length === 0) return;
46 const ui = yield* TerminalUI;
47 yield* ui.log.warn(
48 `Server returned connected account status(es) this CLI does not ` +
49 `recognize: ${unknown.join(', ')}. Run "composio upgrade" to pick up ` +
50 `the latest CLI.`
51 );
52 });
53
54 /** A command's failure when a connected-accounts response does not decode. */
55 export const CONNECTED_ACCOUNTS_SHAPE_MESSAGE =
56 'Connected accounts response was not the shape this CLI understands; run `composio upgrade`.';
57
58 /**
59 * A list response that does not match `ConnectedAccountListResponse`. The
60 * message names only the mismatched paths and issue kinds, never the values,
61 * so it is safe to print as the cause of a command's failure.
62 */
63 export class ConnectedAccountListShapeError extends Data.TaggedError(
64 'effects/ConnectedAccountListShapeError'
65 )<{
66 readonly message: string;
67 }> {}
68
69 /**
70 * Decodes a `client.connectedAccounts.list(...)` response into the CLI's
71 * `ConnectedAccountListResponse`: the field allowlist that keeps
72 * credential-bearing fields (`state`, `data`, ...) out, plus `display_name`
73 * derived from `state`.
74 *
75 * `status` is an open enum (`ConnectedAccountStatus`), so a status newer than
76 * this CLI build decodes and only triggers the "composio upgrade" warning.
77 * Any other mismatch fails with {@link ConnectedAccountListShapeError}.
78 */
79 export const decodeConnectedAccountList = (
80 response: ApiConnectedAccountListResponse
81 ): Effect.Effect<ConnectedAccountListResponse, ConnectedAccountListShapeError, TerminalUI> =>
82 Schema.decodeEffect(ConnectedAccountListResponse)(response).pipe(
83 Effect.mapError(
84 error =>
85 new ConnectedAccountListShapeError({
86 message: `Mismatched fields: ${formatParseErrorRedacted(error)}`,
87 })
88 ),
89 Effect.tap(result => warnOnUnknownStatuses(result.items))
90 );
91
92 /**
93 * Decodes the accounts of every page of a connected-accounts listing (see
94 * `listAllConnectedAccounts`) through the same allowlist as
95 * {@link decodeConnectedAccountList}, failing with the same redacted
96 * {@link ConnectedAccountListShapeError}, for commands that print the accounts.
97 */
98 export const decodeAllConnectedAccountPages = (
99 items: ApiConnectedAccountListResponse['items']
100 ): Effect.Effect<ConnectedAccountItems, ConnectedAccountListShapeError, TerminalUI> =>
101 Schema.decodeEffect(ConnectedAccountItems)(items).pipe(
102 Effect.mapError(
103 error =>
104 new ConnectedAccountListShapeError({
105 message: `Mismatched fields: ${formatParseErrorRedacted(error)}`,
106 })
107 ),
108 Effect.tap(decoded => warnOnUnknownStatuses(decoded))
109 );
110
111 /**
112 * Item-level variant of {@link decodeConnectedAccountList} for call sites
113 * that decode `response.items` rather than the whole list response. It fails
114 * with the raw `SchemaError`, which callers wrap in their own error.
115 */
116 export const decodeConnectedAccountItems = (
117 items: ApiConnectedAccountListResponse['items']
118 ): Effect.Effect<ConnectedAccountItems, Schema.SchemaError, TerminalUI> =>
119 Schema.decodeEffect(ConnectedAccountItems)(items).pipe(
120 Effect.tap(decoded => warnOnUnknownStatuses(decoded))
121 );