@devries-cameron20 / composio Mirror · read-only
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, 04:45 UTC

next
ts 177 lines 6.31 KB
Raw
1 /**
2 * Adapters from failures to the Composio API error envelope.
3 *
4 * `@composio/client/effect` fails a non-success response with a tagged status
5 * error whose `details` is the parsed `{ error: { message, code, slug, ... } }`
6 * envelope. The CLI's own errors keep that failure in `.cause`, so the adapter
7 * walks that chain to the first status error, recognized by its tag.
8 *
9 * Used by both the `ToolsExecutor` service (for error classification) and the
10 * `tools execute` command (for error display).
11 */
12
13 import {
14 isAPIError,
15 isComposioError,
16 type APIConnectionError,
17 type APIStatusError,
18 type ConfigurationError,
19 type DecodeError,
20 } from '@composio/client/effect';
21 import { Predicate } from 'effect';
22 import {
23 CLIENT_API_ERROR_TAGS,
24 HttpDecodingError,
25 type ClientApiError,
26 } from 'src/services/composio-errors';
27
28 export type { ClientApiError };
29
30 const clientApiErrorTags: ReadonlySet<string> = new Set(CLIENT_API_ERROR_TAGS);
31
32 export const isClientApiError = (value: unknown): value is ClientApiError =>
33 isComposioError(value) && clientApiErrorTags.has(value._tag);
34
35 export const isClientConfigurationError = (value: unknown): value is ConfigurationError =>
36 isComposioError(value) && Predicate.isTagged(value, '@composio/client/ConfigurationError');
37
38 export const isClientConnectionError = (value: unknown): value is APIConnectionError =>
39 isComposioError(value) && Predicate.isTagged(value, '@composio/client/APIConnectionError');
40
41 export const isClientDecodeError = (value: unknown): value is DecodeError =>
42 isComposioError(value) && Predicate.isTagged(value, '@composio/client/DecodeError');
43
44 /** A response that failed the CLI's own schema, for a request named by its client operation. */
45 export type OperationDecodingError = HttpDecodingError & { readonly operation: string };
46
47 export const isOperationDecodingError = (value: unknown): value is OperationDecodingError =>
48 value instanceof HttpDecodingError && value.operation !== undefined;
49
50 /**
51 * What the CLI prints when a response failed client decoding, or the CLI's
52 * own schema for a named operation. It names the request and never quotes the
53 * response.
54 */
55 export const describeDecodeError = (
56 error: DecodeError | OperationDecodingError
57 ): { readonly message: string; readonly hint: string } => {
58 const subject =
59 error.operation !== undefined
60 ? `'${error.operation}'`
61 : isClientDecodeError(error)
62 ? `${error.method} ${error.url}`
63 : 'a request';
64 const requestId =
65 isClientDecodeError(error) && error.requestId !== undefined
66 ? ` (request id: ${error.requestId})`
67 : '';
68 return {
69 message: `The API response for ${subject} is not in the shape this version of the CLI expects${requestId}.`,
70 hint: 'Upgrade the CLI, then retry:\n> composio upgrade',
71 };
72 };
73
74 export interface ApiErrorDetails {
75 readonly message?: string;
76 readonly code?: number;
77 readonly slug?: string;
78 readonly status?: number;
79 readonly request_id?: string;
80 readonly suggested_fix?: string;
81 }
82
83 /**
84 * Returns the first client status error along the `.cause` chain, starting at
85 * `value`.
86 */
87 export const findApiError = (value: unknown): APIStatusError | undefined => {
88 const seen = new Set<unknown>();
89 let current: unknown = value;
90
91 while (Predicate.isObject(current) && !seen.has(current)) {
92 if (isAPIError(current)) return current;
93 seen.add(current);
94 current = Predicate.hasProperty(current, 'cause') ? current.cause : undefined;
95 }
96
97 return undefined;
98 };
99
100 /**
101 * The API error envelope carried by the first status error in the cause chain,
102 * with the HTTP status and request id. A body that is not the envelope keeps
103 * the status and falls back to the error's own message.
104 */
105 export const extractApiErrorDetails = (value: unknown): ApiErrorDetails | undefined => {
106 const apiError = findApiError(value);
107 if (!apiError) return undefined;
108
109 const details = apiError.details;
110 return {
111 message: details?.message ?? apiError.message,
112 code: details?.code,
113 slug: details?.slug,
114 status: apiError.status,
115 request_id: apiError.requestId ?? details?.request_id,
116 suggested_fix: details?.suggested_fix,
117 };
118 };
119
120 /**
121 * Extract a human-readable message from an unknown error value.
122 * Checks `.message`, then `.error` (string or `.error.message`).
123 */
124 export const extractMessage = (value: unknown, seen?: Set<unknown>): string | undefined => {
125 if (typeof value === 'string') return value;
126
127 // Walk the .cause / .error chain to find the deepest meaningful message.
128 // A wrapper stores the failure it wraps in .cause; an API response body
129 // stores its envelope in .error (which itself has .error.message).
130 // We prefer the deepest message because outer wrappers often have generic
131 // messages like "An unknown error occurred in Effect.tryPromise".
132 if (Predicate.isObject(value)) {
133 const visited = seen ?? new Set<unknown>();
134 if (visited.has(value)) return undefined;
135 visited.add(value);
136
137 // A client status error keeps the response body in .body.
138 if (isAPIError(value)) {
139 return value.details?.message ?? extractMessage(value.body, visited) ?? value.message;
140 }
141
142 // A response that failed client decoding (or the CLI's schema for a named
143 // operation) is reported by operation and request id, with the remedy,
144 // never by the raw message.
145 if (isClientDecodeError(value) || isOperationDecodingError(value)) {
146 const { message, hint } = describeDecodeError(value);
147 return `${message}\n${hint}`;
148 }
149
150 // A client connection error has no cause chain; the transport text is in .description.
151 if (isClientConnectionError(value)) {
152 return value.description ?? value.message;
153 }
154
155 // Try .cause chain first (wrapper → the failure it wraps)
156 if (Predicate.hasProperty(value, 'cause')) {
157 const causeMsg = extractMessage(value.cause, visited);
158 if (causeMsg) return causeMsg;
159 }
160
161 // Try .error chain (API response body → nested .error.message)
162 if (Predicate.hasProperty(value, 'error')) {
163 const inner = value.error;
164 const innerMsg = extractMessage(inner, visited);
165 if (innerMsg) return innerMsg;
166 }
167
168 // Fall back to the wrapper's own message
169 if (Predicate.hasProperty(value, 'message') && Predicate.isString(value.message)) {
170 return value.message;
171 }
172 }
173
174 if (value instanceof Error) return value.message;
175
176 return undefined;
177 };