From 4ecb4c8fe322af96fa07a742341db8696af414c6 Mon Sep 17 00:00:00 2001 From: Nate Date: Tue, 15 Jul 2025 23:14:17 -0700 Subject: [PATCH] Handle stream abortion gracefully and add global error handlers --- package-lock.json | 4 +- src/index.ts | 11 +++ src/streamChatResponse.ts | 148 ++++++++++++++++++++------------------ 3 files changed, 92 insertions(+), 71 deletions(-) diff --git a/package-lock.json b/package-lock.json index 94c93ead0..e2cc9c085 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@continuedev/cli", - "version": "1.3.1", + "version": "1.3.3", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@continuedev/cli", - "version": "1.3.1", + "version": "1.3.3", "license": "Apache-2.0", "dependencies": { "@continuedev/config-yaml": "^1.0.96", diff --git a/src/index.ts b/src/index.ts index 85a7fc66a..bd16a295f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,6 +6,17 @@ import { login } from "./commands/login.js"; import { logout } from "./commands/logout.js"; import { getVersion } from "./version.js"; +// Add global error handlers to prevent uncaught errors from crashing the process +process.on('unhandledRejection', (reason, promise) => { + console.error('Unhandled Rejection at:', promise, 'reason:', reason); + // Don't exit the process, just log the error +}); + +process.on('uncaughtException', (error) => { + console.error('Uncaught Exception:', error); + // Don't exit the process, just log the error +}); + const program = new Command(); program diff --git a/src/streamChatResponse.ts b/src/streamChatResponse.ts index 1028c721a..e0c1d0cd7 100644 --- a/src/streamChatResponse.ts +++ b/src/streamChatResponse.ts @@ -98,7 +98,7 @@ export async function streamChatResponse( chalk.red("Error in streamChatResponse:"), chalk.red(error.message) ); - process.exit(1); + throw error; } let aiResponse = ""; @@ -106,87 +106,97 @@ export async function streamChatResponse( let currentToolCallId = ""; let toolArguments = ""; - for await (const chunk of stream) { - // Check if we should abort - if (abortController?.signal.aborted) { - break; - } - - // Handle regular content - if (chunk.choices[0].delta.content) { - const content = chunk.choices[0].delta.content; - if (callbacks?.onContent) { - callbacks.onContent(content); - } else if (!isHeadless) { - process.stdout.write(chalk.white(content)); + try { + for await (const chunk of stream) { + // Check if we should abort + if (abortController?.signal.aborted) { + break; } - aiResponse += content; - fullResponse += content; - } - // Handle tool calls - if (chunk.choices[0].delta.tool_calls) { - for (const toolCallDelta of chunk.choices[0].delta.tool_calls) { - // Initialize a new tool call if we get an index and id - if (toolCallDelta.index !== undefined && toolCallDelta.id) { - if (!currentToolCalls[toolCallDelta.index]) { - currentToolCalls[toolCallDelta.index] = { - id: toolCallDelta.id, - name: "", - arguments: {}, - startNotified: false, - }; - } - currentToolCallId = toolCallDelta.id; + // Handle regular content + if (chunk.choices[0].delta.content) { + const content = chunk.choices[0].delta.content; + if (callbacks?.onContent) { + callbacks.onContent(content); + } else if (!isHeadless) { + process.stdout.write(chalk.white(content)); } + aiResponse += content; + fullResponse += content; + } - // Add function name if present - if (toolCallDelta.function?.name) { - const toolCall = currentToolCalls.find( - (tc) => tc.id === currentToolCallId - ); - if (toolCall) { - if (!toolCall.name) { - toolCall.name = toolCallDelta.function.name; - toolCall.startNotified = false; + // Handle tool calls + if (chunk.choices[0].delta.tool_calls) { + for (const toolCallDelta of chunk.choices[0].delta.tool_calls) { + // Initialize a new tool call if we get an index and id + if (toolCallDelta.index !== undefined && toolCallDelta.id) { + if (!currentToolCalls[toolCallDelta.index]) { + currentToolCalls[toolCallDelta.index] = { + id: toolCallDelta.id, + name: "", + arguments: {}, + startNotified: false, + }; } + currentToolCallId = toolCallDelta.id; } - } - // Collect function arguments - if (toolCallDelta.function?.arguments) { - const toolCall = currentToolCalls.find( - (tc) => tc.id === currentToolCallId - ); - if (toolCall) { - // Accumulate arguments as string to later parse as JSON - toolArguments += toolCallDelta.function.arguments; - - try { - // Try to parse complete JSON - const parsed = JSON.parse(toolArguments); - toolCall.arguments = parsed; - - // Notify start if we haven't already and have both name and args - if (toolCall.name && !toolCall.startNotified) { - toolCall.startNotified = true; - if (callbacks?.onToolStart) { - callbacks.onToolStart(toolCall.name, toolCall.arguments); - } else if (!isHeadless) { - process.stdout.write( - `\n${chalk.yellow("[Using tool:")} ${chalk.yellow.bold( - toolCall.name - )}${chalk.yellow("]")}` - ); - } + // Add function name if present + if (toolCallDelta.function?.name) { + const toolCall = currentToolCalls.find( + (tc) => tc.id === currentToolCallId + ); + if (toolCall) { + if (!toolCall.name) { + toolCall.name = toolCallDelta.function.name; + toolCall.startNotified = false; + } + } + } + + // Collect function arguments + if (toolCallDelta.function?.arguments) { + const toolCall = currentToolCalls.find( + (tc) => tc.id === currentToolCallId + ); + if (toolCall) { + // Accumulate arguments as string to later parse as JSON + toolArguments += toolCallDelta.function.arguments; + + try { + // Try to parse complete JSON + const parsed = JSON.parse(toolArguments); + toolCall.arguments = parsed; + + // Notify start if we haven't already and have both name and args + if (toolCall.name && !toolCall.startNotified) { + toolCall.startNotified = true; + if (callbacks?.onToolStart) { + callbacks.onToolStart(toolCall.name, toolCall.arguments); + } else if (!isHeadless) { + process.stdout.write( + `\n${chalk.yellow("[Using tool:")} ${chalk.yellow.bold( + toolCall.name + )}${chalk.yellow("]")}` + ); + } + } + } catch (e) { + // Not complete JSON yet, continue collecting } - } catch (e) { - // Not complete JSON yet, continue collecting } } } } } + } catch (error: any) { + // Handle AbortError gracefully - this is expected when user cancels + if (error.name === 'AbortError' || abortController?.signal.aborted) { + // Stream was aborted, this is expected behavior + return fullResponse; + } + // For other errors, re-throw them + throw error; } if (!callbacks?.onContent && !isHeadless) {