1
-import { Effect, Schema } from 'effect';
2
-import type { ParseResult } from 'effect';
1
+import { Effect, ParseResult, Schema } from 'effect';
2
import {
3
ConnectedAccountItems,
5
- ConnectedAccountItemsPermissive,
4
+ isKnownConnectedAccountStatus,
5
} from 'src/models/connected-accounts';
6
import {
7
ConnectedAccountListResponse,
9
} from 'src/services/composio-clients';
10
import { TerminalUI } from 'src/services/terminal-ui';
11
12
+/**
13
+ * Renders a `ParseError` as `path: kind` pairs only — never the failing
14
+ * values. A container-level failure would otherwise echo the raw payload,
15
+ * which can include credential-bearing fields (`state`, `data`, ...) the
16
+ * connected-account schemas deliberately exclude.
17
+ */
18
+const formatParseErrorRedacted = (error: ParseResult.ParseError): string =>
19
+ ParseResult.ArrayFormatter.formatErrorSync(error)
20
+ .map(issue => `${issue.path.join('.') || '(root)'}: ${issue._tag}`)
21
+ .join(', ');
22
+
23
+/**
24
+ * Warns when the server returned `status` values newer than this CLI build's
25
+ * known set. Status values are non-credential by design, so printing them
26
+ * verbatim is safe — and exactly what the user needs to act on.
27
+ */
28
+const warnOnUnknownStatuses = (
29
+ items: ReadonlyArray<{ readonly status: string }>
30
+): Effect.Effect<void, never, TerminalUI> =>
31
+ Effect.gen(function* () {
32
+ const unknown = [
33
+ ...new Set(items.map(item => item.status).filter(s => !isKnownConnectedAccountStatus(s))),
34
+ ];
35
+ if (unknown.length === 0) return;
36
+ const ui = yield* TerminalUI;
37
+ yield* ui.log.warn(
38
+ `Server returned connected account status(es) this CLI does not ` +
39
+ `recognize: ${unknown.join(', ')}. Run "composio upgrade" to pick up ` +
40
+ `the latest CLI.`
41
+ );
42
+ });
43
+
44
/**
45
* Decodes a raw `client.connectedAccounts.list(...)` response against
46
* `ConnectedAccountListResponse`, falling back to the raw payload on
47
* `ParseError`.
48
*
18
- * Forward-compat: a future Apollo status would otherwise brick the caller
19
- * via the closed `Schema.Literal(...)`. On decode failure we warn via
20
- * `ui.log.warn` (pointing the user at `composio upgrade`) and return the
21
- * unvalidated raw response — safe because the three current call sites
22
- * only render non-credential fields (`id`, `alias`, `word_id`,
23
- * `toolkit.slug`, `status`, ...).
49
+ * `status` is an open enum (`ConnectedAccountStatus`), so a status newer than
50
+ * this CLI build decodes fine and only triggers the "composio upgrade"
51
+ * warning. The raw fallback remains for other shape skew (pagination fields,
52
+ * renames, ...) — safe because the three current call sites only render
53
+ * non-credential fields (`id`, `alias`, `word_id`, `toolkit.slug`,
54
+ * `status`, ...).
55
*/
56
export const decodeConnectedAccountListWithFallback = (
57
rawResult: unknown
59
Effect.gen(function* () {
60
const ui = yield* TerminalUI;
61
return yield* Schema.decodeUnknown(ConnectedAccountListResponse)(rawResult).pipe(
62
+ Effect.tap(result => warnOnUnknownStatuses(result.items)),
63
Effect.catchTag('ParseError', error =>
64
Effect.gen(function* () {
65
yield* ui.log.warn(
34
- `Server returned a connection field this CLI does not recognize ` +
35
- `(likely a newly-added status). Run "composio upgrade" to pick up ` +
36
- `the latest schema. Continuing with raw response.\n\n` +
37
- `Decode error: ${error.message}`
66
+ `Server response did not match the shape this CLI expects. Run ` +
67
+ `"composio upgrade" to pick up the latest CLI. Continuing with ` +
68
+ `raw response.\n\n` +
69
+ `Mismatched fields: ${formatParseErrorRedacted(error)}`
70
);
71
// Safe: callers only read non-credential fields.
72
return rawResult as ConnectedAccountListResponseType;
78
/**
79
* Item-level variant of {@link decodeConnectedAccountListWithFallback} for
80
* call sites that decode `response.items` rather than the whole list
49
- * response. Same forward-compat contract: on `ParseError` (e.g. a status
50
- * value newer than this CLI's closed `Schema.Literal(...)`) warn via
51
- * `ui.log.warn` and retry with `status` widened to `Schema.String`.
81
+ * response.
82
*
53
- * Unlike the list-response helper above, this one must NOT fall back to the
54
- * raw payload: `link --list` JSON-dumps the decoded items to stdout, so the
83
+ * `status` is an open enum, so a status newer than this CLI build decodes
84
+ * instead of failing — we just warn, pointing at `composio upgrade`. Unlike
85
+ * the list-response helper above, this one must NOT fall back to the raw
86
+ * payload: `link --list` JSON-dumps the decoded items to stdout, so the
87
* schema's field allowlist is what keeps credential-bearing fields (`state`,
56
- * `data`, ...) out of pipeable output. The permissive re-decode preserves
57
- * that allowlist; truly malformed data still fails with `ParseError`.
88
+ * `data`, ...) out of pipeable output. Truly malformed data fails with
89
+ * `ParseError`.
90
*/
59
-export const decodeConnectedAccountItemsWithFallback = (
91
+export const decodeConnectedAccountItems = (
92
rawItems: unknown
93
): Effect.Effect<ConnectedAccountItems, ParseResult.ParseError, TerminalUI> =>
62
- Effect.gen(function* () {
63
- const ui = yield* TerminalUI;
64
- return yield* Schema.decodeUnknown(ConnectedAccountItems)(rawItems).pipe(
65
- Effect.catchTag('ParseError', error =>
66
- Effect.gen(function* () {
67
- yield* ui.log.warn(
68
- `Server returned a connection field this CLI does not recognize ` +
69
- `(likely a newly-added status). Run "composio upgrade" to pick up ` +
70
- `the latest schema. Continuing with a permissive schema.\n\n` +
71
- `Decode error: ${error.message}`
72
- );
73
- const permissive = yield* Schema.decodeUnknown(ConnectedAccountItemsPermissive)(rawItems);
74
- // Identical shape modulo the widened `status`; callers render it as text.
75
- return permissive as ConnectedAccountItems;
76
- })
77
- )
78
- );
79
- });
94
+ Schema.decodeUnknown(ConnectedAccountItems)(rawItems).pipe(
95
+ Effect.tap(items => warnOnUnknownStatuses(items))
96
+ );