From 98fd4dfae2fbd9d4c1e4269e60967d567f927f84 Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Tue, 1 Jul 2025 13:45:41 -0700 Subject: [PATCH 1/4] stringify data streaming error --- core/llm/stream.ts | 114 ----------------------------------- gui/src/pages/gui/Chat.tsx | 11 +--- packages/fetch/src/stream.ts | 8 ++- 3 files changed, 8 insertions(+), 125 deletions(-) delete mode 100644 core/llm/stream.ts diff --git a/core/llm/stream.ts b/core/llm/stream.ts deleted file mode 100644 index 5902ec114..000000000 --- a/core/llm/stream.ts +++ /dev/null @@ -1,114 +0,0 @@ -export async function* toAsyncIterable( - nodeReadable: NodeJS.ReadableStream, -): AsyncGenerator { - for await (const chunk of nodeReadable) { - // @ts-ignore - yield chunk as Uint8Array; - } -} - -export async function* streamResponse( - response: Response, -): AsyncGenerator { - if (response.status !== 200) { - throw new Error(await response.text()); - } - - if (!response.body) { - throw new Error("No response body returned."); - } - - // Get the major version of Node.js - const nodeMajorVersion = parseInt(process.versions.node.split(".")[0], 10); - - if (nodeMajorVersion >= 20) { - // Use the new API for Node 20 and above - const stream = (ReadableStream as any).from(response.body); - for await (const chunk of stream.pipeThrough( - new TextDecoderStream("utf-8"), - )) { - yield chunk; - } - } else { - // Fallback for Node versions below 20 - // Streaming with this method doesn't work as version 20+ does - const decoder = new TextDecoder("utf-8"); - const nodeStream = response.body as unknown as NodeJS.ReadableStream; - for await (const chunk of toAsyncIterable(nodeStream)) { - yield decoder.decode(chunk, { stream: true }); - } - } -} - -function parseDataLine(line: string): any { - const json = line.startsWith("data: ") - ? line.slice("data: ".length) - : line.slice("data:".length); - - try { - const data = JSON.parse(json); - if (data.error) { - throw new Error(`Error streaming response: ${data.error}`); - } - - return data; - } catch (e) { - throw new Error(`Malformed JSON sent from server: ${json}`); - } -} - -function parseSseLine(line: string): { done: boolean; data: any } { - if (line.startsWith("data:[DONE]") || line.startsWith("data: [DONE]")) { - return { done: true, data: undefined }; - } - if (line.startsWith("data:")) { - return { done: false, data: parseDataLine(line) }; - } - if (line.startsWith(": ping")) { - return { done: true, data: undefined }; - } - return { done: false, data: undefined }; -} - -export async function* streamSse(response: Response): AsyncGenerator { - let buffer = ""; - for await (const value of streamResponse(response)) { - buffer += value; - - let position: number; - while ((position = buffer.indexOf("\n")) >= 0) { - const line = buffer.slice(0, position); - buffer = buffer.slice(position + 1); - - const { done, data } = parseSseLine(line); - if (done) { - break; - } - if (data) { - yield data; - } - } - } - - if (buffer.length > 0) { - const { done, data } = parseSseLine(buffer); - if (!done && data) { - yield data; - } - } -} - -export async function* streamJSON(response: Response): AsyncGenerator { - let buffer = ""; - for await (const value of streamResponse(response)) { - buffer += value; - - let position; - while ((position = buffer.indexOf("\n")) >= 0) { - const line = buffer.slice(0, position); - const data = JSON.parse(line); - yield data; - buffer = buffer.slice(position + 1); - } - } -} diff --git a/gui/src/pages/gui/Chat.tsx b/gui/src/pages/gui/Chat.tsx index fb75d200c..b1d48cba1 100644 --- a/gui/src/pages/gui/Chat.tsx +++ b/gui/src/pages/gui/Chat.tsx @@ -4,7 +4,6 @@ import { } from "@heroicons/react/24/outline"; import { Editor, JSONContent } from "@tiptap/react"; import { InputModifiers } from "core"; -import { streamResponse } from "core/llm/stream"; import { renderChatMessage } from "core/util/messageContent"; import { useCallback, @@ -236,15 +235,7 @@ export function Chat() { setLocalStorage("mainTextEntryCounter", 1); } }, - [ - history, - selectedModels, - streamResponse, - mode, - isInEdit, - codeToEdit, - toolCallState, - ], + [history, selectedModels, mode, isInEdit, codeToEdit, toolCallState], ); useWebviewListener( diff --git a/packages/fetch/src/stream.ts b/packages/fetch/src/stream.ts index 540b9c79e..de2deb960 100644 --- a/packages/fetch/src/stream.ts +++ b/packages/fetch/src/stream.ts @@ -81,7 +81,13 @@ export function parseDataLine(line: string): any { try { const data = JSON.parse(json); if (data.error) { - throw new Error(`Error streaming response: ${data.error}`); + if ("message" in data.error) { + console.error("Error in streamed response:", data.error); + throw new Error(`Error streaming response: ${data.error.message}`); + } + throw new Error( + `Error streaming response: ${JSON.stringify(data.error)}`, + ); } return data; From 7b8888a93fda08b0abe5ffbaf01db5c321262204 Mon Sep 17 00:00:00 2001 From: Patrick Erichsen Date: Wed, 2 Jul 2025 10:15:38 -0700 Subject: [PATCH 2/4] Update packages/fetch/src/stream.ts Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com> --- packages/fetch/src/stream.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/fetch/src/stream.ts b/packages/fetch/src/stream.ts index de2deb960..6cead217f 100644 --- a/packages/fetch/src/stream.ts +++ b/packages/fetch/src/stream.ts @@ -81,7 +81,7 @@ export function parseDataLine(line: string): any { try { const data = JSON.parse(json); if (data.error) { - if ("message" in data.error) { + if (data.error && typeof data.error === "object" && "message" in data.error) { console.error("Error in streamed response:", data.error); throw new Error(`Error streaming response: ${data.error.message}`); } From 4f7c2366c88d0844eda338282b52cec524ee6b45 Mon Sep 17 00:00:00 2001 From: Patrick Erichsen Date: Wed, 2 Jul 2025 10:15:45 -0700 Subject: [PATCH 3/4] Update gui/src/pages/gui/Chat.tsx Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com> --- gui/src/pages/gui/Chat.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gui/src/pages/gui/Chat.tsx b/gui/src/pages/gui/Chat.tsx index b1d48cba1..dc7bcb83e 100644 --- a/gui/src/pages/gui/Chat.tsx +++ b/gui/src/pages/gui/Chat.tsx @@ -235,7 +235,7 @@ export function Chat() { setLocalStorage("mainTextEntryCounter", 1); } }, - [history, selectedModels, mode, isInEdit, codeToEdit, toolCallState], + [history, selectedModels, mode, isInEdit, codeToEdit, toolCallState, currentToolCallApplyState], ); useWebviewListener( From 10502a81b24170754a63826ede06f0c4cd9bda3c Mon Sep 17 00:00:00 2001 From: Dallin Romney Date: Thu, 3 Jul 2025 01:35:52 -0700 Subject: [PATCH 4/4] fix prettier --- core/nextEdit/templating/NextEditPromptEngine.ts | 2 +- gui/src/pages/gui/Chat.tsx | 10 +++++++++- packages/fetch/src/stream.ts | 6 +++++- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/core/nextEdit/templating/NextEditPromptEngine.ts b/core/nextEdit/templating/NextEditPromptEngine.ts index 7b21232d6..6d9319fd3 100644 --- a/core/nextEdit/templating/NextEditPromptEngine.ts +++ b/core/nextEdit/templating/NextEditPromptEngine.ts @@ -39,7 +39,7 @@ type NextEditModelName = "mercury-coder-nextedit" | "this field is not used"; const NEXT_EDIT_MODEL_TEMPLATES: Record = { "mercury-coder-nextedit": { template: - "### User Edits:\n\n{{{userEdits}}}\n\n### User Excerpts:\n\n\`\`\`{{{languageShorthand}}}\n{{{userExcerpts}}}\`\`\`", + "### User Edits:\n\n{{{userEdits}}}\n\n### User Excerpts:\n\n```{{{languageShorthand}}}\n{{{userExcerpts}}}```", }, "this field is not used": { template: "NEXT_EDIT", diff --git a/gui/src/pages/gui/Chat.tsx b/gui/src/pages/gui/Chat.tsx index 716a53aa3..700e5352e 100644 --- a/gui/src/pages/gui/Chat.tsx +++ b/gui/src/pages/gui/Chat.tsx @@ -235,7 +235,15 @@ export function Chat() { setLocalStorage("mainTextEntryCounter", 1); } }, - [history, selectedModels, mode, isInEdit, codeToEdit, toolCallState, currentToolCallApplyState], + [ + history, + selectedModels, + mode, + isInEdit, + codeToEdit, + toolCallState, + currentToolCallApplyState, + ], ); useWebviewListener( diff --git a/packages/fetch/src/stream.ts b/packages/fetch/src/stream.ts index 6cead217f..f73d61dba 100644 --- a/packages/fetch/src/stream.ts +++ b/packages/fetch/src/stream.ts @@ -81,7 +81,11 @@ export function parseDataLine(line: string): any { try { const data = JSON.parse(json); if (data.error) { - if (data.error && typeof data.error === "object" && "message" in data.error) { + if ( + data.error && + typeof data.error === "object" && + "message" in data.error + ) { console.error("Error in streamed response:", data.error); throw new Error(`Error streaming response: ${data.error.message}`); }