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: Preserve Loaded Message Content When Resume Snapshot Is Empty (#14399)

Danny Avila60eba7637521
diff --git a/client/src/hooks/SSE/__tests__/useResumableSSE.spec.ts b/client/src/hooks/SSE/__tests__/useResumableSSE.spec.ts
index 39c422172e..2c94d25294 100644
--- a/client/src/hooks/SSE/__tests__/useResumableSSE.spec.ts
+++ b/client/src/hooks/SSE/__tests__/useResumableSSE.spec.ts
@@ -1239,6 +1239,177 @@ describe('useResumableSSE', () => {
unmount();
});
+ describe('sync content reconciliation', () => {
+ const DB_CONTENT = [{ type: 'text', text: 'streamed before the reload' }];
+
+ const renderWithLoadedResponse = () => {
+ const submission = buildSubmission();
+ const chatHelpers = buildChatHelpers();
+ chatHelpers.getMessages.mockReturnValue([
+ {
+ messageId: 'msg-1',
+ conversationId: CONV_ID,
+ isCreatedByUser: true,
+ text: 'Hello',
+ },
+ {
+ messageId: 'resp-1',
+ parentMessageId: 'msg-1',
+ conversationId: CONV_ID,
+ isCreatedByUser: false,
+ text: '',
+ content: DB_CONTENT,
+ },
+ ] as unknown as TMessage[]);
+ return { submission, chatHelpers };
+ };
+
+ const emitSync = async (aggregatedContent: unknown[]) => {
+ const sse = getLastSSE();
+ await act(async () => {
+ sse._emit('message', {
+ data: JSON.stringify({
+ sync: true,
+ resumeState: {
+ runSteps: [],
+ aggregatedContent,
+ responseMessageId: 'resp-1',
+ },
+ }),
+ });
+ });
+ };
+
+ const syncedResponse = (chatHelpers: ReturnType<typeof buildChatHelpers>) => {
+ const call = chatHelpers.setMessages.mock.calls
+ .map(([messages]) => messages as TMessage[])
+ .reverse()
+ .find((messages) => messages?.some((m) => m.messageId === 'resp-1'));
+ return call?.find((m) => m.messageId === 'resp-1');
+ };
+
+ /**
+ * An empty snapshot is what a resuming client gets when the conversation's job was
+ * replaced mid-flight. Assigning it erased the content the messages query had already
+ * loaded, leaving a message that renders as a bare cursor forever.
+ */
+ it('keeps already-loaded content when the resume snapshot is empty', async () => {
+ const { submission, chatHelpers } = renderWithLoadedResponse();
+ const { unmount } = renderHook(() => useResumableSSE(submission, chatHelpers));
+ await act(async () => {
+ await Promise.resolve();
+ });
+
+ await emitSync([]);
+
+ expect(syncedResponse(chatHelpers)?.content).toEqual(DB_CONTENT);
+ unmount();
+ });
+
+ /**
+ * Regenerate: the new run's response id is not in the loaded history yet, so the
+ * parent-based fallback lands on the answer being REPLACED. Preserving that row's
+ * content would make the regenerated run's deltas append to the stale answer, so an
+ * empty snapshot must still clear a row we only matched heuristically.
+ */
+ it('does not preserve content on a row matched only by the parent fallback', async () => {
+ const submission = buildSubmission();
+ const chatHelpers = buildChatHelpers();
+ chatHelpers.getMessages.mockReturnValue([
+ {
+ messageId: 'msg-1',
+ conversationId: CONV_ID,
+ isCreatedByUser: true,
+ text: 'Hello',
+ },
+ {
+ messageId: 'resp-previous',
+ parentMessageId: 'msg-1',
+ conversationId: CONV_ID,
+ isCreatedByUser: false,
+ text: '',
+ content: [{ type: 'text', text: 'the answer being regenerated' }],
+ },
+ ] as unknown as TMessage[]);
+
+ const { unmount } = renderHook(() => useResumableSSE(submission, chatHelpers));
+ await act(async () => {
+ await Promise.resolve();
+ });
+
+ const sse = getLastSSE();
+ await act(async () => {
+ sse._emit('message', {
+ data: JSON.stringify({
+ sync: true,
+ resumeState: {
+ runSteps: [],
+ aggregatedContent: [],
+ responseMessageId: 'resp-regenerated',
+ },
+ }),
+ });
+ });
+
+ const synced = chatHelpers.setMessages.mock.calls
+ .map(([messages]) => messages as TMessage[])
+ .reverse()
+ .find((messages) => messages?.some((m) => m.messageId === 'resp-previous'))
+ ?.find((m) => m.messageId === 'resp-previous');
+ expect(synced?.content).toEqual([]);
+ unmount();
+ });
+
+ /**
+ * A row with no `content` array must still be assigned the snapshot's array. Handing it
+ * `undefined` would flip which renderer `MultiMessage` selects (it branches on
+ * `message.content` truthiness, and `[]` is truthy while `undefined` is not).
+ */
+ it('assigns the snapshot when the matched row has no content array', async () => {
+ const submission = buildSubmission();
+ const chatHelpers = buildChatHelpers();
+ chatHelpers.getMessages.mockReturnValue([
+ {
+ messageId: 'msg-1',
+ conversationId: CONV_ID,
+ isCreatedByUser: true,
+ text: 'Hello',
+ },
+ {
+ messageId: 'resp-1',
+ parentMessageId: 'msg-1',
+ conversationId: CONV_ID,
+ isCreatedByUser: false,
+ text: 'legacy text-only row',
+ },
+ ] as unknown as TMessage[]);
+
+ const { unmount } = renderHook(() => useResumableSSE(submission, chatHelpers));
+ await act(async () => {
+ await Promise.resolve();
+ });
+
+ await emitSync([]);
+
+ expect(syncedResponse(chatHelpers)?.content).toEqual([]);
+ unmount();
+ });
+
+ it('still applies the resume snapshot when it carries content', async () => {
+ const { submission, chatHelpers } = renderWithLoadedResponse();
+ const { unmount } = renderHook(() => useResumableSSE(submission, chatHelpers));
+ await act(async () => {
+ await Promise.resolve();
+ });
+
+ const resumed = [{ type: 'text', text: 'authoritative resumed content' }];
+ await emitSync(resumed);
+
+ expect(syncedResponse(chatHelpers)?.content).toEqual(resumed);
+ unmount();
+ });
+ });
+
it('replays OAuth run step delta events from resume state sync', async () => {
const submission = buildSubmission();
const chatHelpers = buildChatHelpers();
diff --git a/client/src/hooks/SSE/useResumableSSE.ts b/client/src/hooks/SSE/useResumableSSE.ts
index 11f15b7206..83ef206943 100644
--- a/client/src/hooks/SSE/useResumableSSE.ts
+++ b/client/src/hooks/SSE/useResumableSSE.ts
@@ -1006,10 +1006,15 @@ export default function useResumableSSE(
const messages = getMessages() ?? [];
const userMsgId = userMessage.messageId;
const serverResponseId = data.resumeState.responseMessageId;
+ const hasResumedContent = data.resumeState.aggregatedContent.length > 0;
let responseIdx = -1;
+ /** Only an id match proves the row belongs to THIS generation; the parent-based
+ * fallback below can land on a prior sibling (e.g. the answer being regenerated). */
+ let matchedByResponseId = false;
if (serverResponseId) {
responseIdx = messages.findIndex((m) => m.messageId === serverResponseId);
+ matchedByResponseId = responseIdx >= 0;
}
if (responseIdx < 0) {
responseIdx = messages.findIndex(
@@ -1030,9 +1035,19 @@ export default function useResumableSSE(
if (responseIdx >= 0) {
const oldContent = messages[responseIdx]?.content;
+ /** An EMPTY resume snapshot is not authoritative over content we already loaded
+ * for the SAME response: assigning it would erase that content and leave a bare
+ * cursor. Restricted to an id match — preserving a fallback-matched row would
+ * make a regenerated run append to the answer it is replacing — and to a row
+ * that actually HAS parts, so the array is never swapped for `undefined`. */
+ const preserveLoadedContent =
+ !hasResumedContent &&
+ matchedByResponseId &&
+ Array.isArray(oldContent) &&
+ oldContent.length > 0;
const responseMessage = {
...messages[responseIdx],
- content: data.resumeState.aggregatedContent,
+ content: preserveLoadedContent ? oldContent : data.resumeState.aggregatedContent,
iconURL: preferDefinedString(
messages[responseIdx]?.iconURL,
data.resumeState.iconURL,
@@ -1044,6 +1059,8 @@ export default function useResumableSSE(
messageId: responseMessage.messageId,
oldContentLength: Array.isArray(oldContent) ? oldContent.length : 0,
newContentLength: data.resumeState.aggregatedContent?.length,
+ preservedExistingContent: preserveLoadedContent,
+ matchedByResponseId,
});
setMessages(updated);
resetContentHandler();