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 122 lines 4.8 KB
Raw
1 import { Effect, Option, Schema, SchemaGetter } from 'effect';
2
3 /**
4 * Statuses this CLI build knows about. The server owns this enum and may add
5 * values at any time, so schemas must stay open (see
6 * {@link ConnectedAccountStatus}) — use {@link isKnownConnectedAccountStatus}
7 * to detect values newer than this build.
8 */
9 export const KnownConnectedAccountStatus = Schema.Literals([
10 'INITIALIZING',
11 'INITIATED',
12 'ACTIVE',
13 'FAILED',
14 'EXPIRED',
15 'INACTIVE',
16 'REVOKED',
17 ]);
18 export type KnownConnectedAccountStatus = typeof KnownConnectedAccountStatus.Type;
19
20 export const isKnownConnectedAccountStatus = Schema.is(KnownConnectedAccountStatus);
21
22 /**
23 * Open enum for the server-owned account status: the known literals document
24 * the values this build understands, the `Schema.String` branch lets a newer
25 * status decode instead of failing the whole payload. Callers only compare
26 * against literals or render the value as text, so widening is safe.
27 */
28 export const ConnectedAccountStatus = Schema.Union([KnownConnectedAccountStatus, Schema.String]);
29
30 /** Longest provider display name the CLI will print, once trimmed. */
31 export const CONNECTED_ACCOUNT_DISPLAY_NAME_MAX_LENGTH = 200;
32
33 /**
34 * The single value this CLI reads out of a connected account's `state`: the
35 * provider identity Composio records at `state.val.displayName` for an active
36 * connection (a Gmail address, a GitHub login, ...). Decoding against this
37 * narrow shape keeps that value and discards every other key under `state`.
38 */
39 const ProviderIdentityFromState = Schema.Struct({
40 val: Schema.Struct({
41 displayName: Schema.Trim.check(
42 Schema.isNonEmpty(),
43 Schema.isMaxLength(CONNECTED_ACCOUNT_DISPLAY_NAME_MAX_LENGTH)
44 ),
45 }),
46 });
47
48 /**
49 * The trimmed display name held in a raw `state` value, or `None` when it is
50 * absent, blank, not a string, or longer than the limit.
51 */
52 const displayNameFromState = (state: unknown): Option.Option<string> =>
53 Schema.decodeUnknownOption(ProviderIdentityFromState)(state).pipe(
54 Option.map(identity => identity.val.displayName)
55 );
56
57 /**
58 * `display_name` as decoded from the raw `state` key (see `encodeKeys` on
59 * {@link ConnectedAccountItem}). Decoding never fails the item: any unusable
60 * display name simply leaves the field out. Encoding omits it, so a decoded
61 * item can never be turned back into a `state` payload.
62 */
63 const DisplayNameFromState = Schema.optionalKey(Schema.Unknown).pipe(
64 Schema.decodeTo(Schema.optionalKey(Schema.String), {
65 decode: SchemaGetter.transformOptional(Option.flatMap(displayNameFromState)),
66 encode: SchemaGetter.omit(),
67 })
68 );
69
70 /**
71 * A connected account item from the list or retrieve endpoints.
72 * Field names match the raw API response (snake_case).
73 *
74 * SECURITY: This schema intentionally excludes `state`, `data`, `params`,
75 * and `deprecated` fields to prevent credential leakage in CLI output.
76 * The `state` discriminated union contains raw OAuth tokens, refresh tokens,
77 * and API keys per auth scheme — never add it without a redaction layer.
78 * `display_name` is the one exception: it is extracted from `state` through
79 * {@link ProviderIdentityFromState}, which retains nothing else.
80 */
81 export const ConnectedAccountItem = Schema.Struct({
82 id: Schema.String,
83 word_id: Schema.optional(Schema.NullOr(Schema.String)),
84 alias: Schema.optional(Schema.NullOr(Schema.String)),
85 status: ConnectedAccountStatus,
86 status_reason: Schema.NullOr(Schema.String).pipe(
87 Schema.withDecodingDefaultType(Effect.succeed(null))
88 ),
89 is_disabled: Schema.Boolean,
90 user_id: Schema.String,
91 toolkit: Schema.Struct({
92 slug: Schema.String,
93 }),
94 auth_config: Schema.Struct({
95 id: Schema.String,
96 auth_scheme: Schema.String,
97 is_composio_managed: Schema.Boolean,
98 is_disabled: Schema.Boolean,
99 }),
100 created_at: Schema.String,
101 updated_at: Schema.String,
102 test_request_endpoint: Schema.String.pipe(Schema.withDecodingDefaultType(Effect.succeed(''))),
103 display_name: DisplayNameFromState,
104 })
105 .pipe(Schema.encodeKeys({ display_name: 'state' }))
106 .annotate({ identifier: 'ConnectedAccountItem' });
107 export type ConnectedAccountItem = Schema.Schema.Type<typeof ConnectedAccountItem>;
108
109 export const ConnectedAccountItems = Schema.Array(ConnectedAccountItem);
110 export type ConnectedAccountItems = Schema.Schema.Type<typeof ConnectedAccountItems>;
111
112 // Connected account list response (single page with total_items for "Listing X of Y" display)
113 export const ConnectedAccountListResponse = Schema.Struct({
114 items: ConnectedAccountItems,
115 total_items: Schema.Int,
116 total_pages: Schema.Int,
117 current_page: Schema.Int,
118 next_cursor: Schema.NullOr(Schema.String).pipe(
119 Schema.withDecodingDefaultType(Effect.succeed(null))
120 ),
121 }).annotate({ identifier: 'ConnectedAccountListResponse' });
122 export type ConnectedAccountListResponse = Schema.Schema.Type<typeof ConnectedAccountListResponse>;