Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

🧱 fix: Keep Streamed Content When a Turn Fails (#15961)

Danny Avilae4f6b01b24f5
diff --git a/client/src/hooks/SSE/__tests__/useEventHandlers.spec.ts b/client/src/hooks/SSE/__tests__/useEventHandlers.spec.ts
index 524b696638..d38e5b0dbb 100644
--- a/client/src/hooks/SSE/__tests__/useEventHandlers.spec.ts
+++ b/client/src/hooks/SSE/__tests__/useEventHandlers.spec.ts
@@ -1,5 +1,11 @@
-import { Constants } from 'librechat-data-provider';
-import type { EventSubmission, TMessage, TConversation } from 'librechat-data-provider';
+import { Constants, ContentTypes } from 'librechat-data-provider';
+import type {
+ TMessageContentParts,
+ EventSubmission,
+ TConversation,
+ TMessage,
+} from 'librechat-data-provider';
+import type { TResData } from '~/common';
import {
buildCreatedInitialResponse,
getExistingConversationAbortMessages,
@@ -8,8 +14,10 @@ import {
buildRecoveryPreset,
mergeErrorMessages,
mergeRegenerateFinalMessages,
+ resolveErrorTurn,
startedAsNewConversation,
} from '~/hooks/SSE/useEventHandlers';
+import { stripStreamedIndexStamps, getPartKeyIndex } from '~/utils';
describe('buildCreatedInitialResponse', () => {
const userMessage = {
@@ -352,3 +360,258 @@ describe('buildRecoveryPreset', () => {
expect(buildRecoveryPreset(sent, undefined, '_fresh').codeApprovalMode).toBe('ask');
});
});
+
+describe('resolveErrorTurn', () => {
+ const userMessage = {
+ messageId: 'user-1',
+ conversationId: 'conversation-1',
+ parentMessageId: Constants.NO_PARENT,
+ isCreatedByUser: true,
+ text: 'Look up the issue',
+ sender: 'User',
+ } as TMessage;
+ const initialResponse = {
+ messageId: 'user-1_',
+ parentMessageId: 'user-1',
+ conversationId: 'conversation-1',
+ isCreatedByUser: false,
+ text: '',
+ sender: 'Lia',
+ endpoint: 'agents',
+ model: 'agent_1',
+ } as TMessage;
+ const submission = {
+ messages: [],
+ userMessage,
+ initialResponse,
+ conversation: { conversationId: 'conversation-1' },
+ } as unknown as EventSubmission;
+ const streamedParts = [
+ { type: ContentTypes.THINK, think: 'Checking the issue' },
+ { type: ContentTypes.TEXT, text: 'Let me try the GitHub CLI from the workspace.' },
+ {
+ type: ContentTypes.TOOL_CALL,
+ tool_call: { id: 'call-1', name: 'execute_code', args: '{}', progress: 1 },
+ },
+ ] as TMessageContentParts[];
+ const streamedResponse = { ...initialResponse, content: streamedParts };
+ const startFailureText = JSON.stringify({ code: 'code_workspace_unavailable', reason: 'locked' });
+ const startFailure = {
+ text: startFailureText,
+ metadata: { streamStartFailed: true },
+ } as unknown as TResData;
+
+ it('keeps what the run streamed and takes the failure as one more part', () => {
+ const { conversationId, errorResponse, recover } = resolveErrorTurn({
+ data: startFailure,
+ submission,
+ getMessages: () => [userMessage, streamedResponse],
+ isNewConversationRoute: false,
+ });
+
+ expect(conversationId).toBe('conversation-1');
+ expect(recover).toBe(false);
+ expect(errorResponse.content).toEqual([
+ ...streamedParts,
+ { type: ContentTypes.ERROR, error: startFailureText },
+ ]);
+ expect(errorResponse.error).toBeUndefined();
+ expect(errorResponse.text).toBe('');
+ expect(errorResponse.messageId).toBe('user-1_');
+ expect(errorResponse.parentMessageId).toBe('user-1');
+ expect(errorResponse.metadata).toEqual({ streamStartFailed: true });
+ });
+
+ it('drops holes, keeps empty slots, and keeps the identity every part streamed under', () => {
+ const openedThink = { type: ContentTypes.THINK, think: '' };
+ const openedText = { type: ContentTypes.TEXT, text: '' };
+ const { errorResponse } = resolveErrorTurn({
+ data: startFailure,
+ submission,
+ getMessages: () => [
+ userMessage,
+ {
+ ...initialResponse,
+ content: [
+ openedThink,
+ streamedParts[0],
+ undefined,
+ streamedParts[1],
+ streamedParts[2],
+ openedText,
+ ],
+ } as TMessage,
+ ],
+ isNewConversationRoute: false,
+ });
+
+ const content = errorResponse.content ?? [];
+ expect(stripStreamedIndexStamps(content)).toEqual([
+ openedThink,
+ ...streamedParts,
+ openedText,
+ { type: ContentTypes.ERROR, error: startFailureText },
+ ]);
+ expect(content.map((part, idx) => getPartKeyIndex(part, idx))).toEqual([0, 1, 3, 4, 5, 6]);
+ });
+
+ it('keeps the comparison lanes when one side streamed before the failure', () => {
+ const lanes = [
+ { type: '', agentId: 'agent_a', groupId: 1 },
+ { type: ContentTypes.TEXT, text: 'From the added agent', agentId: 'agent_b', groupId: 1 },
+ ];
+ const { errorResponse } = resolveErrorTurn({
+ data: startFailure,
+ submission,
+ getMessages: () => [
+ userMessage,
+ { ...initialResponse, content: lanes } as unknown as TMessage,
+ ],
+ isNewConversationRoute: false,
+ });
+
+ expect(errorResponse.content).toEqual([
+ ...lanes,
+ { type: ContentTypes.ERROR, error: startFailureText },
+ ]);
+ });
+
+ it('is the whole row when a comparison run failed before either lane streamed', () => {
+ const { errorResponse } = resolveErrorTurn({
+ data: startFailure,
+ submission,
+ getMessages: () => [
+ userMessage,
+ {
+ ...initialResponse,
+ content: [
+ { type: '', agentId: 'agent_a', groupId: 1 },
+ { type: '', agentId: 'agent_b', groupId: 1 },
+ ],
+ } as unknown as TMessage,
+ ],
+ isNewConversationRoute: false,
+ });
+
+ expect(errorResponse.content).toBeUndefined();
+ expect(errorResponse.error).toBe(true);
+ expect(errorResponse.text).toBe(startFailureText);
+ });
+
+ it('is the whole row when nothing streamed', () => {
+ const { errorResponse } = resolveErrorTurn({
+ data: startFailure,
+ submission,
+ getMessages: () => [userMessage, initialResponse],
+ isNewConversationRoute: false,
+ });
+
+ expect(errorResponse.content).toBeUndefined();
+ expect(errorResponse.error).toBe(true);
+ expect(errorResponse.text).toBe(startFailureText);
+ expect(errorResponse.messageId).toBe('user-1_');
+ expect(errorResponse.parentMessageId).toBe('user-1');
+ });
+
+ it('never takes a user row at the tail for the failed response', () => {
+ const { errorResponse } = resolveErrorTurn({
+ data: startFailure,
+ submission,
+ getMessages: () => [
+ { ...userMessage, content: [{ type: ContentTypes.TEXT, text: 'Look up the issue' }] },
+ ],
+ isNewConversationRoute: false,
+ });
+
+ expect(errorResponse.content).toBeUndefined();
+ expect(errorResponse.error).toBe(true);
+ });
+
+ it('records a lost connection as a part of the streamed response and rebuilds the chat', () => {
+ const { conversationId, errorResponse, recover } = resolveErrorTurn({
+ data: undefined,
+ submission,
+ getMessages: () => [userMessage, streamedResponse],
+ isNewConversationRoute: false,
+ });
+
+ expect(conversationId).toBe('conversation-1');
+ expect(recover).toBe(true);
+ expect(errorResponse.content).toEqual([
+ ...streamedParts,
+ {
+ type: ContentTypes.ERROR,
+ error: 'Error connecting to server, try refreshing the page.',
+ },
+ ]);
+ });
+
+ it("keeps the streamed row's envelope when a server failure carries its own", () => {
+ const streamedAt = '2026-09-15T13:00:00.000Z';
+ const { errorResponse } = resolveErrorTurn({
+ data: {
+ conversationId: 'conversation-1',
+ messageId: 'user-1_',
+ isCreatedByUser: false,
+ sender: 'System',
+ model: null,
+ iconURL: null,
+ createdAt: '2026-09-15T13:05:00.000Z',
+ text: startFailureText,
+ metadata: { streamStartFailed: true },
+ } as unknown as TResData,
+ submission,
+ getMessages: () => [
+ userMessage,
+ { ...streamedResponse, iconURL: 'lia.png', createdAt: streamedAt, metadata: { seed: 1 } },
+ ],
+ isNewConversationRoute: false,
+ });
+
+ expect(errorResponse).toEqual(
+ expect.objectContaining({
+ sender: 'Lia',
+ model: 'agent_1',
+ iconURL: 'lia.png',
+ createdAt: streamedAt,
+ metadata: { seed: 1, streamStartFailed: true },
+ }),
+ );
+ expect(errorResponse.content?.at(-1)).toEqual({
+ type: ContentTypes.ERROR,
+ error: startFailureText,
+ });
+ });
+
+ it('keeps the streamed parts under a failure the server addressed to the conversation', () => {
+ const serverText = JSON.stringify({ type: 'invalid_request' });
+ const data = {
+ conversationId: 'conversation-1',
+ messageId: 'user-1_',
+ isCreatedByUser: false,
+ sender: 'Lia',
+ text: serverText,
+ } as unknown as TResData;
+
+ const fromChat = resolveErrorTurn({
+ data,
+ submission,
+ getMessages: () => [userMessage, streamedResponse],
+ isNewConversationRoute: false,
+ });
+ const fromNewChat = resolveErrorTurn({
+ data,
+ submission,
+ getMessages: () => [userMessage, streamedResponse],
+ isNewConversationRoute: true,
+ });
+
+ expect(fromChat.recover).toBe(false);
+ expect(fromNewChat.recover).toBe(true);
+ expect(fromChat.errorResponse.content).toEqual([
+ ...streamedParts,
+ { type: ContentTypes.ERROR, error: serverText },
+ ]);
+ expect(fromChat.errorResponse.parentMessageId).toBe('user-1');
+ });
+});
diff --git a/client/src/hooks/SSE/useEventHandlers.ts b/client/src/hooks/SSE/useEventHandlers.ts
index dbc285664c..91aa610366 100644
--- a/client/src/hooks/SSE/useEventHandlers.ts
+++ b/client/src/hooks/SSE/useEventHandlers.ts
@@ -17,8 +17,9 @@ import type {
TPreset,
TMessage,
TConversation,
- EventSubmission,
TStartupConfig,
+ EventSubmission,
+ TMessageContentParts,
} from 'librechat-data-provider';
import type { InfiniteData } from '@tanstack/react-query';
import type { SetterOrUpdater } from 'recoil';
@@ -39,6 +40,8 @@ import {
removeConvoFromAllQueries,
findConversationInInfinite,
preserveStreamedContentIdentity,
+ isEmptyContentPart,
+ getPartKeyIndex,
} from '~/utils';
import {
startupConfigKey,
@@ -229,6 +232,50 @@ export type EventHandlerParams = {
setShowStopButton: SetterOrUpdater<boolean>;
};
+const CONNECTION_ERROR_TEXT = 'Error connecting to server, try refreshing the page.';
+
+/**
+ * The parts the in-flight response has streamed so far: the transcript's tail (a user row there
+ * means no response was placed), without the holes an interrupted stream leaves. Whether anything
+ * streamed is judged without the slots that never received content — a comparison run's
+ * `type: ''` placeholders, a text or think part opened before its first delta — but once something
+ * did, those slots stay: a placeholder is what keeps a comparison lane's layout and attribution
+ * when only the other lane produced output. The parts carry the render identity they streamed
+ * under, as the final path stamps it, so the settled row does not remount.
+ */
+const getStreamedContent = (message?: TMessage): TMessageContentParts[] => {
+ if (message == null || message.isCreatedByUser === true) {
+ return [];
+ }
+ const streamed = message.content ?? [];
+ const parts = streamed.filter((part): part is TMessageContentParts => part != null);
+ if (!parts.some((part) => !isEmptyContentPart(part))) {
+ return [];
+ }
+ return preserveStreamedContentIdentity(streamed, parts) ?? parts;
+};
+
+/** Keys the appended failure past every key the kept parts render under, physical or stamped. */
+const appendErrorPart = (
+ content: TMessageContentParts[],
+ errorText: string,
+): TMessageContentParts[] => {
+ const keyIndex = content.reduce(
+ (next, part, idx) => Math.max(next, getPartKeyIndex(part, idx) + 1),
+ 0,
+ );
+ const errorPart: TMessageContentParts = { type: ContentTypes.ERROR, error: errorText };
+ return [
+ ...content,
+ keyIndex === content.length ? errorPart : { ...errorPart, streamedIndex: keyIndex },
+ ];
+};
+
+/**
+ * A failure that lands after the run has streamed keeps what streamed and takes the failure as
+ * one more part, the way the server records a failure inside a run; one that lands before
+ * anything streamed is the whole row.
+ */
const createErrorMessage = ({
errorMetadata,
getMessages,
@@ -242,41 +289,24 @@ const createErrorMessage = ({
}): TMessage => {
const currentMessages = getMessages();
const latestMessage = currentMessages?.[currentMessages.length - 1];
- let errorMessage: TMessage;
const text = submission.initialResponse.text.length > 45 ? submission.initialResponse.text : '';
const errorText =
(errorMetadata?.text || text || (error as Error | undefined)?.message) ??
'Error cancelling request';
- const latestContent = latestMessage?.content ?? [];
- let isValidContentPart = false;
- if (latestContent.length > 0) {
- const latestContentPart = latestContent[latestContent.length - 1];
- if (latestContentPart != null) {
- const latestPartValue = latestContentPart[latestContentPart.type ?? ''];
- isValidContentPart =
- latestContentPart.type !== ContentTypes.TEXT ||
- (latestContentPart.type === ContentTypes.TEXT && typeof latestPartValue === 'string')
- ? true
- : latestPartValue?.value !== '';
- }
- }
- if (
- latestMessage?.conversationId &&
- latestMessage?.messageId &&
- latestContent &&
- isValidContentPart
- ) {
- const content = [...latestContent];
- content.push({
- type: ContentTypes.ERROR,
- error: errorText,
- });
- errorMessage = {
+ const streamedContent = getStreamedContent(latestMessage);
+ if (latestMessage?.conversationId && latestMessage.messageId && streamedContent.length > 0) {
+ /** The row keeps the envelope it streamed under — author, model, icon, creation time — and
+ * takes from the failure only what describes the failure: a server payload names `System`
+ * as its sender and the schema dates a fresh envelope now, neither of which applies to a
+ * response that is merely gaining a part. */
+ const errorMessage: TMessage = {
...latestMessage,
- ...errorMetadata,
+ ...(errorMetadata?.metadata != null
+ ? { metadata: { ...latestMessage.metadata, ...errorMetadata.metadata } }
+ : {}),
error: undefined,
text: '',
- content,
+ content: appendErrorPart(streamedContent, errorText),
};
if (
submission.userMessage.messageId &&
@@ -285,18 +315,94 @@ const createErrorMessage = ({
errorMessage.parentMessageId = submission.userMessage.messageId;
}
return errorMessage;
- } else if (errorMetadata) {
+ }
+ if (errorMetadata) {
return errorMetadata as TMessage;
- } else {
- errorMessage = {
- ...submission,
- ...submission.initialResponse,
- text: errorText,
- unfinished: !!text.length,
+ }
+ return tMessageSchema.parse({
+ ...submission,
+ ...submission.initialResponse,
+ text: errorText,
+ unfinished: !!text.length,
+ error: true,
+ }) as TMessage;
+};
+
+export interface ErrorTurn {
+ conversationId: string;
+ errorResponse: TMessage;
+ /** The conversation must be rebuilt: it never learned its id, the transport failed, or the
+ * server named a conversation the new-chat route has not navigated to. */
+ recover: boolean;
+}
+
+/** Builds the row a failed turn leaves in the transcript and names the conversation it belongs to. */
+export const resolveErrorTurn = ({
+ data,
+ submission,
+ getMessages,
+ isNewConversationRoute,
+}: {
+ data?: TResData;
+ submission: EventSubmission;
+ getMessages: () => TMessage[] | undefined;
+ isNewConversationRoute: boolean;
+}): ErrorTurn => {
+ const { userMessage, initialResponse } = submission;
+ const conversationId =
+ userMessage.conversationId ?? submission.conversation?.conversationId ?? '';
+
+ const parseErrorResponse = (payload: TResData | Partial<TMessage>): TMessage => {
+ const metadata = payload['responseMessage'] ?? payload;
+ const errorMessage: Partial<TMessage> = {
+ ...initialResponse,
+ ...metadata,
error: true,
+ parentMessageId: userMessage.messageId,
};
+
+ if (errorMessage.messageId === undefined || errorMessage.messageId === '') {
+ errorMessage.messageId = v4();
+ }
+
+ return tMessageSchema.parse(errorMessage) as TMessage;
+ };
+ const build = (errorMetadata: TMessage): TMessage =>
+ createErrorMessage({ errorMetadata, getMessages, submission });
+
+ if (!data) {
+ const convoId = conversationId || `_${v4()}`;
+ return {
+ conversationId: convoId,
+ recover: true,
+ errorResponse: build(
+ parseErrorResponse({ text: CONNECTION_ERROR_TEXT, ...submission, conversationId: convoId }),
+ ),
+ };
+ }
+
+ const receivedConvoId = data.conversationId ?? '';
+ if (!conversationId && !receivedConvoId) {
+ return {
+ conversationId: `_${v4()}`,
+ recover: true,
+ errorResponse: build(parseErrorResponse(data)),
+ };
+ }
+ if (!receivedConvoId) {
+ return { conversationId, recover: false, errorResponse: build(parseErrorResponse(data)) };
}
- return tMessageSchema.parse(errorMessage) as TMessage;
+ return {
+ conversationId: receivedConvoId,
+ recover: isNewConversationRoute,
+ errorResponse: build(
+ tMessageSchema.parse({
+ ...data,
+ error: true,
+ parentMessageId: userMessage.messageId,
+ }) as TMessage,
+ ),
+ };
};
/**
@@ -1056,81 +1162,22 @@ export default function useEventHandlers({
const errorHandler = useCallback(
({ data, submission }: { data?: TResData; submission: EventSubmission }) => {
- const { userMessage, initialResponse } = submission;
- setCompleted((prev) => new Set(prev.add(initialResponse.messageId)));
+ setCompleted((prev) => new Set(prev.add(submission.initialResponse.messageId)));
setSubmissionStart(null);
- const conversationId =
- userMessage.conversationId ?? submission.conversation?.conversationId ?? '';
-
- const setErrorMessages = (convoId: string, errorMessage: TMessage) => {
- const finalMessages = mergeErrorMessages({ ...submission, errorMessage });
- setMessages(finalMessages);
- queryClient.setQueryData<TMessage[]>([QueryKeys.messages, convoId], finalMessages);
- };
-
- const parseErrorResponse = (data: TResData | Partial<TMessage>): TMessage => {
- const metadata = data['responseMessage'] ?? data;
- const errorMessage: Partial<TMessage> = {
- ...initialResponse,
- ...metadata,
- error: true,
- parentMessageId: userMessage.messageId,
- };
-
- if (errorMessage.messageId === undefined || errorMessage.messageId === '') {
- errorMessage.messageId = v4();
- }
-
- return tMessageSchema.parse(errorMessage) as TMessage;
- };
-
- if (!data) {
- const convoId = conversationId || `_${v4()}`;
- const errorMetadata = parseErrorResponse({
- text: 'Error connecting to server, try refreshing the page.',
- ...submission,
- conversationId: convoId,
- });
- const errorResponse = createErrorMessage({
- errorMetadata,
- getMessages,
- submission,
- });
- setErrorMessages(convoId, errorResponse);
- recoverConversation(convoId, submission);
- setIsSubmitting(false);
- return;
- }
-
- const receivedConvoId = data.conversationId ?? '';
- if (!conversationId && !receivedConvoId) {
- const convoId = `_${v4()}`;
- const errorResponse = parseErrorResponse(data);
- setErrorMessages(convoId, errorResponse);
- recoverConversation(convoId, submission);
- setIsSubmitting(false);
- return;
- } else if (!receivedConvoId) {
- const errorResponse = parseErrorResponse(data);
- setErrorMessages(conversationId, errorResponse);
- setIsSubmitting(false);
- return;
- }
-
- const errorResponse = tMessageSchema.parse({
- ...data,
- error: true,
- parentMessageId: userMessage.messageId,
- }) as TMessage;
-
- setErrorMessages(receivedConvoId, errorResponse);
- if (receivedConvoId && paramId === Constants.NEW_CONVO) {
- recoverConversation(receivedConvoId, submission);
+ const { conversationId, errorResponse, recover } = resolveErrorTurn({
+ data,
+ submission,
+ getMessages,
+ isNewConversationRoute: paramId === Constants.NEW_CONVO,
+ });
+ const finalMessages = mergeErrorMessages({ ...submission, errorMessage: errorResponse });
+ setMessages(finalMessages);
+ queryClient.setQueryData<TMessage[]>([QueryKeys.messages, conversationId], finalMessages);
+ if (recover) {
+ recoverConversation(conversationId, submission);
}
-
setIsSubmitting(false);
- return;
},
[
setCompleted,
diff --git a/client/src/utils/messages.ts b/client/src/utils/messages.ts
index 532add8de9..f279360eb7 100644
--- a/client/src/utils/messages.ts
+++ b/client/src/utils/messages.ts
@@ -204,7 +204,7 @@ const getPartToolCall = (part: TMessageContentParts): Agents.ToolCall | undefine
/** Slots the persistence compaction leaves nothing behind for: the
* dual-message `type: ''` placeholders, text/think parts that never received a
* delta, and tool calls missing their `tool_call` payload. */
-const isEmptyContentPart = (part: TMessageContentParts): boolean => {
+export const isEmptyContentPart = (part: TMessageContentParts): boolean => {
if (!part.type) {
return true;
}