diff --git a/gui/src/pages/gui/StreamError.tsx b/gui/src/pages/gui/StreamError.tsx index 7d5f652bf..580cbd3c0 100644 --- a/gui/src/pages/gui/StreamError.tsx +++ b/gui/src/pages/gui/StreamError.tsx @@ -1,4 +1,5 @@ import { + ArrowPathIcon, ArrowTopRightOnSquareIcon, ClipboardIcon, Cog6ToothIcon, @@ -7,6 +8,7 @@ import { import { DISCORD_LINK, GITHUB_LINK } from "core/util/constants"; import { useContext, useMemo } from "react"; import { GhostButton, SecondaryButton } from "../../components"; +import { useMainEditor } from "../../components/mainInput/TipTapEditor"; import { DiscordIcon } from "../../components/svg/DiscordIcon"; import { GithubIcon } from "../../components/svg/GithubIcon"; import ToggleDiv from "../../components/ToggleDiv"; @@ -16,6 +18,7 @@ import { selectSelectedProfile } from "../../redux/"; import { useAppDispatch, useAppSelector } from "../../redux/hooks"; import { selectSelectedChatModel } from "../../redux/slices/configSlice"; import { setDialogMessage, setShowDialog } from "../../redux/slices/uiSlice"; +import { streamResponseThunk } from "../../redux/thunks"; import { isLocalProfile } from "../../util"; import { providers } from "../AddNewModel/configs/providers"; import { ModelsAddOnLimitDialog } from "./ModelsAddOnLimitDialog"; @@ -44,6 +47,7 @@ const StreamErrorDialog = ({ error }: StreamErrorProps) => { const selectedModel = useAppSelector(selectSelectedChatModel); const selectedProfile = useAppSelector(selectSelectedProfile); const { session, refreshProfiles } = useAuth(); + const { mainEditor } = useMainEditor(); const parsedError = useMemo( () => parseErrorMessage((error as any)?.message || ""), @@ -60,6 +64,8 @@ const StreamErrorDialog = ({ error }: StreamErrorProps) => { void navigator.clipboard.writeText(parsedError); }; + const history = useAppSelector((store) => store.session.history); + // Collect model information to display useful error info let modelTitle = "Chat model"; let providerName = "the model provider"; @@ -131,6 +137,48 @@ const StreamErrorDialog = ({ error }: StreamErrorProps) => { ); + const resubmitButton = ( + { + let index = -1; + for (let i = history.length - 1; i >= 0; i--) { + if ( + history[i].message.role === "user" || + history[i].message.role === "tool" + ) { + index = i; + break; + } + } + + if (!mainEditor) { + console.error("Main editor not found, cannot resubmit message."); + return; + } + + const editorState = + index === -1 ? mainEditor.getJSON() : history[index].editorState; + + void dispatch( + streamResponseThunk({ + editorState, + modifiers: { + noContext: true, + useCodebase: false, + }, + index: index === -1 ? 0 : index, + }), + ); + dispatch(setShowDialog(false)); + dispatch(setDialogMessage(undefined)); + }} + > + + Resubmit last message + + ); + if ( parsedError === "You have exceeded the chat limit for the Models Add-On." ) { @@ -138,7 +186,7 @@ const StreamErrorDialog = ({ error }: StreamErrorProps) => { } let errorContent = ( -
+

There was an error handling the response from{" "} @@ -148,6 +196,7 @@ const StreamErrorDialog = ({ error }: StreamErrorProps) => { Please try to submit your message again, and if the error persists, let us know by reporting the issue using the buttons below.

+
{resubmitButton}
); @@ -233,7 +282,7 @@ const StreamErrorDialog = ({ error }: StreamErrorProps) => { if ( message && (message.toLowerCase().includes("overloaded") || - message.toLowerCase().includes("malformed")) + message.toLowerCase().includes("malformed json")) ) { errorContent = (
diff --git a/packages/fetch/src/premature-close.ts b/packages/fetch/src/premature-close.ts new file mode 100644 index 000000000..dd03b5a83 --- /dev/null +++ b/packages/fetch/src/premature-close.ts @@ -0,0 +1,14 @@ +export function getPrematureCloseErrorMessage(chunks: number): string { + if (chunks === 0) { + return "Connection was closed before any data was received (Premature close). Try again."; + } else { + return ( + "Stream was cancelled mid-stream. This can happen for various reasons, including:\n" + + "- Malformed chunks of data received from the server\n" + + "- The server closed the connection before sending the complete response\n" + + "- Long delays from the server during streaming\n" + + "- 'Keep alive' header being used in combination with an http agent and a set, low number of maxSockets\n" + + "The underlying node fetch error is 'Premature Close'" + ); + } +} diff --git a/packages/fetch/src/stream.ts b/packages/fetch/src/stream.ts index b1f5ab906..3310b639f 100644 --- a/packages/fetch/src/stream.ts +++ b/packages/fetch/src/stream.ts @@ -1,3 +1,5 @@ +import { getPrematureCloseErrorMessage } from "./premature-close.js"; + export async function* toAsyncIterable( nodeReadable: NodeJS.ReadableStream, ): AsyncGenerator { @@ -24,6 +26,7 @@ export async function* streamResponse( // Get the major version of Node.js const nodeMajorVersion = parseInt(process.versions.node.split(".")[0], 10); + let chunks = 0; try { if (nodeMajorVersion >= 20) { @@ -33,6 +36,7 @@ export async function* streamResponse( new TextDecoderStream("utf-8"), )) { yield chunk; + chunks++; } } else { // Fallback for Node versions below 20 @@ -41,12 +45,19 @@ export async function* streamResponse( const nodeStream = response.body as unknown as NodeJS.ReadableStream; for await (const chunk of toAsyncIterable(nodeStream)) { yield decoder.decode(chunk, { stream: true }); + chunks++; } } } catch (e) { - if (e instanceof Error && e.name.startsWith("AbortError")) { - return; // In case of client-side cancellation, just return + if (e instanceof Error) { + if (e.name.startsWith("AbortError")) { + return; // In case of client-side cancellation, just return + } + if (e.message.toLowerCase().includes("premature close")) { + throw new Error(getPrematureCloseErrorMessage(chunks)); + } } + throw e; } }