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 { Effect } from 'effect'; |
| 2 | import { trackCliCodactFailureEffect, trackCliEventEffect } from 'src/analytics/dispatch'; |
| 3 | import { |
| 4 | getToolExecuteFailedEvent, |
| 5 | getToolExecuteToolNotFoundEvent, |
| 6 | getToolExecuteValidationFailedEvent, |
| 7 | isMaybeToolValidationError, |
| 8 | isMaybeToolNotFoundError, |
| 9 | } from 'src/analytics/events'; |
| 10 | import * as constants from 'src/constants'; |
| 11 | import { retryHint, type RetryAdvice } from 'src/effects/retry-advice'; |
| 12 | import { toolkitFromToolSlug } from 'src/effects/toolkit-from-tool-slug'; |
| 13 | import type { ToolkitProjectScope } from 'src/services/composio-clients'; |
| 14 | import { |
| 15 | ComposioNoActiveConnectionError, |
| 16 | mapComposioError, |
| 17 | normalizeCliError, |
| 18 | } from 'src/services/composio-error-overrides'; |
| 19 | import { cliInvocationContext } from 'src/services/runtime-cli-context'; |
| 20 | import type { TerminalUI } from 'src/services/terminal-ui'; |
| 21 | import { ToolInputValidationError } from 'src/services/tool-input-validation'; |
| 22 | import { escapeTerminalLines, escapeTerminalText } from 'src/ui/escape-terminal-text'; |
| 23 | import { connectionTips, formatUnknownObject, redactRequestId } from './tools.execute.format'; |
| 24 | |
| 25 | export const emitExecuteFailureTelemetry = (params: { |
| 26 | readonly toolSlug: string; |
| 27 | readonly args: Record<string, unknown>; |
| 28 | readonly error: unknown; |
| 29 | readonly surface: 'root' | 'dev'; |
| 30 | readonly projectMode: 'consumer' | 'developer'; |
| 31 | readonly stage: 'schema_fetch' | 'dry_run' | 'validation' | 'execution'; |
| 32 | readonly logId?: string; |
| 33 | readonly mappedError?: ReturnType<typeof mapComposioError>; |
| 34 | readonly projectScope?: ToolkitProjectScope; |
| 35 | }) => |
| 36 | Effect.gen(function* () { |
| 37 | const toolkitSlug = yield* toolkitFromToolSlug(params.toolSlug, params.projectScope); |
| 38 | const { invocationOrigin } = yield* cliInvocationContext; |
| 39 | const normalized = params.mappedError?.normalized ?? normalizeCliError(params.error); |
| 40 | const failureOrigin: 'fast_fail' | 'main_endpoint' = |
| 41 | normalized instanceof ToolInputValidationError || params.stage !== 'execution' |
| 42 | ? 'fast_fail' |
| 43 | : 'main_endpoint'; |
| 44 | |
| 45 | if (normalized instanceof ToolInputValidationError) { |
| 46 | yield* trackCliEventEffect( |
| 47 | getToolExecuteValidationFailedEvent({ |
| 48 | toolSlug: params.toolSlug, |
| 49 | toolkitSlug, |
| 50 | args: params.args, |
| 51 | error: normalized, |
| 52 | invocationOrigin, |
| 53 | surface: params.surface, |
| 54 | projectMode: params.projectMode, |
| 55 | stage: params.stage === 'dry_run' ? 'dry_run' : 'validation', |
| 56 | failureOrigin, |
| 57 | logId: params.logId, |
| 58 | }) |
| 59 | ); |
| 60 | yield* trackCliCodactFailureEffect({ |
| 61 | failureType: 'wrong_tool_input_param', |
| 62 | toolInfo: { |
| 63 | ...(toolkitSlug ? { toolkit: toolkitSlug } : {}), |
| 64 | }, |
| 65 | ctx: { |
| 66 | tool_slug: params.toolSlug, |
| 67 | issues: [...normalized.issues].slice(0, 50), |
| 68 | schema_path: normalized.schemaPath, |
| 69 | stage: params.stage === 'dry_run' ? 'dry_run' : 'validation', |
| 70 | }, |
| 71 | session: { |
| 72 | source: 'cli', |
| 73 | command_path: params.surface === 'root' ? 'execute' : 'dev playground-execute', |
| 74 | project_mode: params.projectMode, |
| 75 | surface: params.surface, |
| 76 | cli_version: constants.APP_VERSION, |
| 77 | }, |
| 78 | }); |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | const mapped = |
| 83 | params.mappedError ?? |
| 84 | mapComposioError({ |
| 85 | error: params.error, |
| 86 | toolkit: toolkitSlug, |
| 87 | toolSlug: params.toolSlug, |
| 88 | tree: params.surface, |
| 89 | }); |
| 90 | const apiDetails = mapped.apiDetails; |
| 91 | const message = mapped.message; |
| 92 | const errorSlug = mapped.slugValue; |
| 93 | const status = apiDetails?.status; |
| 94 | const apiCode = apiDetails?.code; |
| 95 | const isNoConnectionError = |
| 96 | normalized instanceof ComposioNoActiveConnectionError || Boolean(mapped.override); |
| 97 | |
| 98 | const isValidationError = isMaybeToolValidationError({ message, errorSlug, apiCode }); |
| 99 | const executionStage = params.stage === 'validation' ? 'execution' : params.stage; |
| 100 | const failureEvent = () => { |
| 101 | if (isValidationError) { |
| 102 | return getToolExecuteValidationFailedEvent({ |
| 103 | toolSlug: params.toolSlug, |
| 104 | toolkitSlug, |
| 105 | args: params.args, |
| 106 | error: new ToolInputValidationError({ |
| 107 | toolSlug: params.toolSlug, |
| 108 | schemaPath: 'server', |
| 109 | issues: [message], |
| 110 | }), |
| 111 | invocationOrigin, |
| 112 | surface: params.surface, |
| 113 | projectMode: params.projectMode, |
| 114 | stage: params.stage === 'dry_run' ? 'dry_run' : 'execution', |
| 115 | failureOrigin, |
| 116 | logId: params.logId, |
| 117 | }); |
| 118 | } |
| 119 | if (isMaybeToolNotFoundError({ message, errorSlug, status, apiCode })) { |
| 120 | return getToolExecuteToolNotFoundEvent({ |
| 121 | toolSlug: params.toolSlug, |
| 122 | toolkitSlug, |
| 123 | args: params.args, |
| 124 | invocationOrigin, |
| 125 | surface: params.surface, |
| 126 | projectMode: params.projectMode, |
| 127 | stage: executionStage, |
| 128 | failureOrigin, |
| 129 | logId: params.logId, |
| 130 | errorSlug, |
| 131 | status, |
| 132 | apiCode, |
| 133 | message, |
| 134 | }); |
| 135 | } |
| 136 | return getToolExecuteFailedEvent({ |
| 137 | toolSlug: params.toolSlug, |
| 138 | toolkitSlug, |
| 139 | args: params.args, |
| 140 | invocationOrigin, |
| 141 | surface: params.surface, |
| 142 | projectMode: params.projectMode, |
| 143 | stage: executionStage, |
| 144 | failureOrigin, |
| 145 | logId: params.logId, |
| 146 | errorSlug, |
| 147 | status, |
| 148 | apiCode, |
| 149 | message, |
| 150 | errorName: normalized instanceof Error ? normalized.name : undefined, |
| 151 | isNoConnectionError, |
| 152 | }); |
| 153 | }; |
| 154 | |
| 155 | yield* trackCliEventEffect(failureEvent()); |
| 156 | |
| 157 | if (isNoConnectionError) { |
| 158 | return; |
| 159 | } |
| 160 | |
| 161 | if (isValidationError) { |
| 162 | yield* trackCliCodactFailureEffect({ |
| 163 | failureType: 'wrong_tool_input_param', |
| 164 | toolInfo: { |
| 165 | ...(toolkitSlug ? { toolkit: toolkitSlug } : {}), |
| 166 | }, |
| 167 | ctx: { |
| 168 | tool_slug: params.toolSlug, |
| 169 | error_slug: errorSlug, |
| 170 | api_error_code: apiCode, |
| 171 | http_status: status, |
| 172 | error_message: message, |
| 173 | stage: params.stage === 'dry_run' ? 'dry_run' : 'execution', |
| 174 | }, |
| 175 | session: { |
| 176 | source: 'cli', |
| 177 | command_path: params.surface === 'root' ? 'execute' : 'dev playground-execute', |
| 178 | project_mode: params.projectMode, |
| 179 | surface: params.surface, |
| 180 | cli_version: constants.APP_VERSION, |
| 181 | }, |
| 182 | requestId: apiDetails?.request_id, |
| 183 | }); |
| 184 | return; |
| 185 | } |
| 186 | |
| 187 | if ( |
| 188 | isMaybeToolNotFoundError({ |
| 189 | message, |
| 190 | errorSlug, |
| 191 | status, |
| 192 | apiCode, |
| 193 | }) |
| 194 | ) { |
| 195 | yield* trackCliCodactFailureEffect({ |
| 196 | failureType: 'wrong_tool_slug', |
| 197 | toolInfo: { |
| 198 | ...(toolkitSlug ? { toolkit: toolkitSlug } : {}), |
| 199 | }, |
| 200 | ctx: { |
| 201 | invalid_tool_slug: params.toolSlug, |
| 202 | error_slug: errorSlug, |
| 203 | api_error_code: apiCode, |
| 204 | http_status: status, |
| 205 | error_message: message, |
| 206 | stage: params.stage === 'validation' ? 'execution' : params.stage, |
| 207 | }, |
| 208 | session: { |
| 209 | source: 'cli', |
| 210 | command_path: params.surface === 'root' ? 'execute' : 'dev playground-execute', |
| 211 | project_mode: params.projectMode, |
| 212 | surface: params.surface, |
| 213 | cli_version: constants.APP_VERSION, |
| 214 | }, |
| 215 | requestId: apiDetails?.request_id, |
| 216 | }); |
| 217 | } |
| 218 | }); |
| 219 | |
| 220 | export const handleExecutionError = ( |
| 221 | ui: TerminalUI, |
| 222 | error: unknown, |
| 223 | context: { |
| 224 | toolSlug: string; |
| 225 | args: Record<string, unknown>; |
| 226 | surface: 'root' | 'dev'; |
| 227 | projectMode: 'consumer' | 'developer'; |
| 228 | stage: 'schema_fetch' | 'dry_run' | 'validation' | 'execution'; |
| 229 | logId?: string; |
| 230 | projectScope?: ToolkitProjectScope; |
| 231 | } |
| 232 | ) => |
| 233 | Effect.gen(function* () { |
| 234 | const toolkit = yield* toolkitFromToolSlug(context.toolSlug, context.projectScope); |
| 235 | const mapped = mapComposioError({ |
| 236 | error, |
| 237 | toolkit, |
| 238 | toolSlug: context.toolSlug, |
| 239 | tree: context.surface, |
| 240 | }); |
| 241 | const normalized = mapped.normalized; |
| 242 | if (normalized instanceof ToolInputValidationError) { |
| 243 | yield* emitExecuteFailureTelemetry({ |
| 244 | toolSlug: context.toolSlug, |
| 245 | args: context.args, |
| 246 | error: normalized, |
| 247 | surface: context.surface, |
| 248 | projectMode: context.projectMode, |
| 249 | stage: context.stage, |
| 250 | logId: context.logId, |
| 251 | projectScope: context.projectScope, |
| 252 | }); |
| 253 | yield* ui.log.error(`Input validation failed for ${context.toolSlug}`); |
| 254 | yield* ui.note( |
| 255 | [ |
| 256 | `Schema: ${normalized.schemaPath}`, |
| 257 | ...normalized.issues.map(issue => `- ${escapeTerminalText(issue)}`), |
| 258 | ].join('\n'), |
| 259 | 'Tool schema validation' |
| 260 | ); |
| 261 | return { error: normalized.message, slug: context.toolSlug }; |
| 262 | } |
| 263 | |
| 264 | const apiDetails = mapped.apiDetails; |
| 265 | const slugValue = mapped.slugValue; |
| 266 | |
| 267 | yield* emitExecuteFailureTelemetry({ |
| 268 | toolSlug: context.toolSlug, |
| 269 | args: context.args, |
| 270 | error, |
| 271 | surface: context.surface, |
| 272 | projectMode: context.projectMode, |
| 273 | stage: context.stage, |
| 274 | logId: context.logId, |
| 275 | projectScope: context.projectScope, |
| 276 | mappedError: mapped, |
| 277 | }); |
| 278 | |
| 279 | // The message and details can quote the API or the tool; the returned summary keeps them as |
| 280 | // they are for the JSON result. |
| 281 | const displayMessage = escapeTerminalText(mapped.message); |
| 282 | if (normalized instanceof ComposioNoActiveConnectionError) { |
| 283 | yield* ui.log.error(displayMessage); |
| 284 | if (toolkit) { |
| 285 | yield* ui.note( |
| 286 | connectionTips({ toolkit, toolSlug: context.toolSlug, surface: context.surface }), |
| 287 | 'Tips' |
| 288 | ); |
| 289 | } |
| 290 | return { error: mapped.message, slug: slugValue ?? context.toolSlug }; |
| 291 | } |
| 292 | |
| 293 | yield* ui.log.error(displayMessage); |
| 294 | |
| 295 | const detailsObject = apiDetails; |
| 296 | if (detailsObject) { |
| 297 | yield* ui.note( |
| 298 | escapeTerminalLines(formatUnknownObject(redactRequestId(detailsObject))), |
| 299 | 'Error details' |
| 300 | ); |
| 301 | } |
| 302 | |
| 303 | return { error: mapped.message, slug: slugValue }; |
| 304 | }); |
| 305 | |
| 306 | /** |
| 307 | * Tells the reader on a terminal whether the failed execution can be run again. Off a terminal it |
| 308 | * prints nothing, like the rest of the failure: the JSON result on stdout carries it as `retry`. |
| 309 | * In the developer tree, a request that may have taken effect also names where recent executions |
| 310 | * are listed. |
| 311 | */ |
| 312 | export const printExecuteRetryAdvice = ( |
| 313 | ui: TerminalUI, |
| 314 | advice: RetryAdvice, |
| 315 | surface: 'root' | 'dev' |
| 316 | ) => |
| 317 | ui.log.step( |
| 318 | advice === 'check-first' && surface === 'dev' |
| 319 | ? `${retryHint(advice)}\nRecent tool executions:\n> composio dev logs tools` |
| 320 | : retryHint(advice) |
| 321 | ); |