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
| 1 | import { Predicate } from 'effect'; |
| 2 | import { |
| 3 | extractApiErrorDetails, |
| 4 | extractMessage, |
| 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', |
| 12 | 'ToolRouterV2_NoActiveConnection', |
| 13 | ]); |
| 14 | |
| 15 | export const normalizeCliError = (error: unknown): unknown => { |
| 16 | let current: unknown = error; |
| 17 | const seen = new Set<unknown>(); |
| 18 | |
| 19 | while (Predicate.isObject(current) && !seen.has(current)) { |
| 20 | seen.add(current); |
| 21 | |
| 22 | if (current instanceof Error) { |
| 23 | return current; |
| 24 | } |
| 25 | |
| 26 | if (Predicate.hasProperty(current, 'error')) { |
| 27 | current = current.error; |
| 28 | continue; |
| 29 | } |
| 30 | if (Predicate.hasProperty(current, 'cause')) { |
| 31 | current = current.cause; |
| 32 | continue; |
| 33 | } |
| 34 | break; |
| 35 | } |
| 36 | |
| 37 | return current; |
| 38 | }; |
| 39 | |
| 40 | export const isNoConnectionSlug = (slug: string | undefined | null): boolean => |
| 41 | slug != null && NO_CONNECTION_SLUGS.has(slug); |
| 42 | |
| 43 | export const isNoActiveConnectionApiError = ( |
| 44 | details: { code?: number; slug?: string } | undefined |
| 45 | ): boolean => details?.code === 4302 || isNoConnectionSlug(details?.slug); |
| 46 | |
| 47 | /** The command tree the failing command ran in; connections do not carry over between the two. */ |
| 48 | type ConnectionTree = 'root' | 'dev'; |
| 49 | |
| 50 | /** |
| 51 | * The link command of the tree the failure happened in. A root-tree link |
| 52 | * connects the consumer user, which leaves a `dev` command failing as before. |
| 53 | */ |
| 54 | const linkStep = (toolkit: string, tree: ConnectionTree): string => |
| 55 | tree === 'dev' |
| 56 | ? `Run \`composio dev connected-accounts link ${toolkit}\` (with the same \`--user-id\`, if you passed one), then retry.` |
| 57 | : `Run \`composio link ${toolkit}\`, then retry.`; |
| 58 | |
| 59 | export const buildNoActiveConnectionMessage = (params: { |
| 60 | readonly toolkit?: string; |
| 61 | readonly toolSlug?: string; |
| 62 | readonly tree?: ConnectionTree; |
| 63 | }) => { |
| 64 | const tree = params.tree ?? 'root'; |
| 65 | if (params.toolkit) { |
| 66 | return `No active connection found for toolkit "${params.toolkit}". ${linkStep(params.toolkit, tree)}`; |
| 67 | } |
| 68 | if (params.toolSlug) { |
| 69 | // Best-effort fallback for callers that could not resolve the toolkit. |
| 70 | // `guessToolkitFromToolSlug` returns the whole slug lowercased when there |
| 71 | // is no underscore, so keep the explicit 'composio' guard for the |
| 72 | // bare-slug case. |
| 73 | const toolkit = guessToolkitFromToolSlug(params.toolSlug); |
| 74 | if (toolkit && toolkit !== 'composio') { |
| 75 | return `No active connection found for toolkit "${toolkit}". ${linkStep(toolkit, tree)}`; |
| 76 | } |
| 77 | } |
| 78 | return 'No active connection found for this tool call. Link the required toolkit/app, then retry.'; |
| 79 | }; |
| 80 | |
| 81 | export class ComposioNoActiveConnectionError extends Error { |
| 82 | readonly details: unknown; |
| 83 | readonly apiDetails?: ApiErrorDetails; |
| 84 | readonly toolkit?: string; |
| 85 | readonly toolSlug?: string; |
| 86 | readonly tree: ConnectionTree; |
| 87 | |
| 88 | constructor(params: { |
| 89 | readonly details: unknown; |
| 90 | readonly apiDetails?: ApiErrorDetails; |
| 91 | readonly toolkit?: string; |
| 92 | readonly toolSlug?: string; |
| 93 | readonly tree?: ConnectionTree; |
| 94 | }) { |
| 95 | super( |
| 96 | buildNoActiveConnectionMessage({ |
| 97 | toolkit: params.toolkit, |
| 98 | toolSlug: params.toolSlug, |
| 99 | tree: params.tree, |
| 100 | }) |
| 101 | ); |
| 102 | this.name = 'ComposioNoActiveConnectionError'; |
| 103 | this.details = params.details; |
| 104 | this.apiDetails = params.apiDetails; |
| 105 | this.toolkit = params.toolkit; |
| 106 | this.toolSlug = params.toolSlug; |
| 107 | this.tree = params.tree ?? 'root'; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | export const mapComposioError = (params: { |
| 112 | readonly error: unknown; |
| 113 | readonly toolkit?: string; |
| 114 | readonly toolSlug?: string; |
| 115 | /** The tree of the command reporting the failure; the root tree when omitted. */ |
| 116 | readonly tree?: ConnectionTree; |
| 117 | }) => { |
| 118 | const normalized = normalizeCliError(params.error); |
| 119 | const apiDetails = |
| 120 | extractApiErrorDetails(params.error) ?? |
| 121 | (normalized instanceof ComposioNoActiveConnectionError ? normalized.apiDetails : undefined); |
| 122 | const slugValue = apiDetails?.slug; |
| 123 | |
| 124 | if ( |
| 125 | normalized instanceof ComposioNoActiveConnectionError || |
| 126 | isNoActiveConnectionApiError(apiDetails) |
| 127 | ) { |
| 128 | const tree = params.tree ?? 'root'; |
| 129 | // The executor raises this error without knowing the tree; the command |
| 130 | // that reports it does, and its message has to name that tree's link. |
| 131 | const mapped = |
| 132 | normalized instanceof ComposioNoActiveConnectionError |
| 133 | ? normalized.tree === tree |
| 134 | ? normalized |
| 135 | : new ComposioNoActiveConnectionError({ |
| 136 | details: normalized.details, |
| 137 | apiDetails: normalized.apiDetails, |
| 138 | toolkit: normalized.toolkit ?? params.toolkit, |
| 139 | toolSlug: normalized.toolSlug ?? params.toolSlug, |
| 140 | tree, |
| 141 | }) |
| 142 | : new ComposioNoActiveConnectionError({ |
| 143 | details: apiDetails ?? params.error, |
| 144 | apiDetails, |
| 145 | toolkit: params.toolkit, |
| 146 | toolSlug: params.toolSlug, |
| 147 | tree, |
| 148 | }); |
| 149 | |
| 150 | return { |
| 151 | normalized: mapped, |
| 152 | apiDetails, |
| 153 | slugValue, |
| 154 | message: mapped.message, |
| 155 | override: { |
| 156 | kind: 'no_active_connection' as const, |
| 157 | error: mapped, |
| 158 | }, |
| 159 | }; |
| 160 | } |
| 161 | |
| 162 | return { |
| 163 | normalized, |
| 164 | apiDetails, |
| 165 | slugValue, |
| 166 | message: |
| 167 | // Its own message names the Dashboard and what to do next; the cause it |
| 168 | // wraps (a transport or decode failure) does not. |
| 169 | normalized instanceof DashboardToolExecutionError |
| 170 | ? normalized.message |
| 171 | : (extractMessage(apiDetails) ?? extractMessage(normalized) ?? 'Unknown error'), |
| 172 | override: null, |
| 173 | }; |
| 174 | }; |
| 175 | |
| 176 | export const mapOnlyComposioOverrideError = (params: { |
| 177 | readonly error: unknown; |
| 178 | readonly toolkit?: string; |
| 179 | readonly toolSlug?: string; |
| 180 | }): unknown => { |
| 181 | const mapped = mapComposioError(params); |
| 182 | return mapped.override ? mapped.normalized : params.error; |
| 183 | }; |