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 Chat Rollback, Publish and Snapshot Version Coherent Across Store Boundaries (#17518)

Marco Beretta220ad4c072f1
diff --git a/packages/chat/src/core/__tests__/chat.spec.ts b/packages/chat/src/core/__tests__/chat.spec.ts
index ffc11c922c..806c0a892c 100644
--- a/packages/chat/src/core/__tests__/chat.spec.ts
+++ b/packages/chat/src/core/__tests__/chat.spec.ts
@@ -1,4 +1,4 @@
-import { StepEvents } from 'librechat-data-provider';
+import { StepEvents, buildTree } from 'librechat-data-provider';
import type { Agents, TMessage, TPayload, ChatEvent } from 'librechat-data-provider';
import type { ChatStream, ChatStatus, ChatTransport, ChatConnection } from '../types';
import type { ChatState, ChatInit } from '../chat';
@@ -271,7 +271,12 @@ describe('Chat', () => {
});
it('undoes the optimistic rows of a turn disposed before it was sent', async () => {
- const earlier: TMessage = { ...placeholder, messageId: 'earlier-1', text: 'Earlier' };
+ const earlier: TMessage = {
+ ...placeholder,
+ messageId: 'earlier-1',
+ parentMessageId: null,
+ text: 'Earlier',
+ };
const state = createState([earlier]);
const { chat, transport } = setup({
state,
@@ -596,6 +601,85 @@ describe('Chat', () => {
unsubscribe();
});
+ it('keeps the next turn submitted when a store that reports its status writes starts it as the connection goes idle', async () => {
+ const state = createState([]);
+ const listeners = new Set<() => void>();
+ let connection: ChatConnection = 'idle';
+ Object.defineProperty(state, 'connection', {
+ get: () => connection,
+ set: (next: ChatConnection) => {
+ connection = next;
+ [...listeners].forEach((listener) => listener());
+ },
+ });
+ state.subscribe = (listener: () => void) => {
+ listeners.add(listener);
+ return () => {
+ listeners.delete(listener);
+ };
+ };
+ let next: Promise<void> | undefined;
+ const { chat, stream } = setup({ state });
+ const sent = chat.sendMessage({ parts: [{ type: 'text', text: 'Hi' }] });
+ await flush();
+ let started = false;
+ chat.subscribe(() => {
+ if (chat.connection === 'idle' && !started) {
+ started = true;
+ next = chat.sendMessage({ parts: [{ type: 'text', text: 'Again' }] });
+ }
+ });
+ stream.push(finalEvent());
+ await sent;
+
+ expect(next).toBeDefined();
+ expect(chat.status).toBe('submitted');
+ expect(chat.error).toBeUndefined();
+ });
+
+ it.each(['final', 'error'] as const)(
+ 'keeps the next turn a store subscriber starts as the connection goes idle, after a %s',
+ async (end) => {
+ const state = createState([]);
+ const listeners = new Set<() => void>();
+ let connection: ChatConnection = 'idle';
+ Object.defineProperty(state, 'connection', {
+ get: () => connection,
+ set: (next: ChatConnection) => {
+ connection = next;
+ [...listeners].forEach((listener) => listener());
+ },
+ });
+ state.subscribe = (listener: () => void) => {
+ listeners.add(listener);
+ return () => {
+ listeners.delete(listener);
+ };
+ };
+ const { chat, stream } = setup({ state });
+ const sent = chat.sendMessage({ parts: [{ type: 'text', text: 'Hi' }] });
+ await flush();
+ let next: Promise<void> | undefined;
+ let started = false;
+ listeners.add(() => {
+ if (connection === 'idle' && !started) {
+ started = true;
+ next = chat.sendMessage({ parts: [{ type: 'text', text: 'Again' }] });
+ }
+ });
+ stream.push(
+ end === 'final'
+ ? finalEvent()
+ : ({ type: 'error', data: { message: 'Failed' } } as ChatEvent),
+ );
+ await sent;
+
+ expect(next).toBeDefined();
+ expect(chat.status).toBe('submitted');
+ expect(chat.error).toBeUndefined();
+ },
+ );
+
it.each(['final', 'abort'] as const)(
'finishes on %s with the response the turn produced, not the tail of another branch',
async (end) => {
@@ -908,8 +992,51 @@ describe('Chat', () => {
expect(onEvent).not.toHaveBeenCalled();
});
+ it.each([
+ ['a content frame', stepEvent],
+ [
+ 'a pause',
+ {
+ type: 'pending_action',
+ data: {
+ actionId: 'action-1',
+ streamId: 'convo-1',
+ conversationId: 'convo-1',
+ createdAt: 1,
+ expiresAt: Date.now() + 60_000,
+ payload: {
+ type: 'ask_user_question',
+ question: { question: 'Which?' },
+ tool_call_id: 'a',
+ },
+ } as Agents.PendingAction,
+ } as ChatEvent,
+ ],
+ ])(
+ 'stops %s when a listener disposes the chat on the streaming status',
+ async (_label, event) => {
+ const { chat, stream, onEvent } = setup();
+ chat.subscribe(() => {
+ if (chat.status === 'streaming') {
+ chat.dispose();
+ }
+ });
+ const sent = chat.sendMessage({ parts: [{ type: 'text', text: 'Hi' }] });
+ await flush();
+ stream.push(event);
+ await sent;
+
+ expect(onEvent).not.toHaveBeenCalled();
+ },
+ );
+
it('reports no response for a turn whose optimistic write failed', async () => {
- const earlier: TMessage = { ...placeholder, messageId: 'earlier-1', text: 'Earlier' };
+ const earlier: TMessage = {
+ ...placeholder,
+ messageId: 'earlier-1',
+ parentMessageId: null,
+ text: 'Earlier',
+ };
const { chat, onFinish, transport } = setup({
state: createState([userMessage, earlier]),
prepareRequest: jest.fn(() => ({
@@ -929,7 +1056,12 @@ describe('Chat', () => {
});
it('reports no response for a turn whose preparation failed', async () => {
- const earlier: TMessage = { ...placeholder, messageId: 'earlier-1', text: 'Earlier' };
+ const earlier: TMessage = {
+ ...placeholder,
+ messageId: 'earlier-1',
+ parentMessageId: null,
+ text: 'Earlier',
+ };
const { chat, onFinish } = setup({
state: createState([userMessage, earlier]),
prepareRequest: jest.fn(() => Promise.reject(new Error('Upload failed'))),
@@ -1028,7 +1160,12 @@ describe('Chat attachments and reported conversations', () => {
});
it('drops the optimistic rows of a start the server replaced before attaching', async () => {
- const earlier: TMessage = { ...placeholder, messageId: 'earlier-1', text: 'Earlier' };
+ const earlier: TMessage = {
+ ...placeholder,
+ messageId: 'earlier-1',
+ parentMessageId: null,
+ text: 'Earlier',
+ };
const stream = createStream({ streamId: 'stream-2', generationCreatedAt: 200 });
let attachedWith: string[] = [];
const { chat } = setup(
@@ -1093,8 +1230,8 @@ describe('Chat attachments and reported conversations', () => {
'undoes the optimistic write of a replaced start: %s',
async (_label, optimistic, external, expected) => {
const earlier: TMessage[] = [
- { ...placeholder, messageId: 'earlier-1', text: 'Earlier' },
- { ...placeholder, messageId: 'earlier-2', text: 'Answer' },
+ { ...placeholder, messageId: 'earlier-1', parentMessageId: null, text: 'Earlier' },
+ { ...placeholder, messageId: 'earlier-2', parentMessageId: 'earlier-1', text: 'Answer' },
];
const stream = createStream({ streamId: 'stream-2', generationCreatedAt: 200 });
const state = createState(earlier);
@@ -1145,8 +1282,18 @@ describe('Chat attachments and reported conversations', () => {
])(
'keeps a row a listener appended on the optimistic write when a replaced start rolls back, %s',
async (_label, viaStore) => {
- const earlier: TMessage = { ...placeholder, messageId: 'earlier-1', text: 'Earlier' };
- const external: TMessage = { ...placeholder, messageId: 'external-1', text: 'External' };
+ const earlier: TMessage = {
+ ...placeholder,
+ messageId: 'earlier-1',
+ parentMessageId: null,
+ text: 'Earlier',
+ };
+ const external: TMessage = {
+ ...placeholder,
+ messageId: 'external-1',
+ parentMessageId: 'earlier-1',
+ text: 'External',
+ };
const stream = createStream({ streamId: 'stream-2', generationCreatedAt: 200 });
const state = createState([earlier]);
const storeListeners = new Set<() => void>();
@@ -1205,16 +1352,31 @@ describe('Chat attachments and reported conversations', () => {
describe('optimistic rollback at the store boundary', () => {
const earlier: TMessage[] = [
- { ...placeholder, messageId: 'earlier-1', text: 'Earlier' },
- { ...placeholder, messageId: 'earlier-2', text: 'Answer' },
+ { ...userMessage, messageId: 'earlier-1', parentMessageId: null, text: 'Earlier' },
+ { ...placeholder, messageId: 'earlier-2', parentMessageId: 'earlier-1', text: 'Answer' },
];
- const external: TMessage = { ...placeholder, messageId: 'external-1', text: 'External' };
+ const turnUser: TMessage = { ...userMessage, parentMessageId: 'earlier-2' };
+ const external: TMessage = {
+ ...placeholder,
+ messageId: 'external-1',
+ parentMessageId: 'earlier-2',
+ text: 'External',
+ };
/** Overwrites `earlier-2` and appends the turn, so the write both replaces and adds rows. */
const optimistic = (current: TMessage[]) => [
...current.map((m) => (m.messageId === 'earlier-2' ? { ...m, text: '' } : m)),
- userMessage,
+ turnUser,
placeholder,
];
+ type Node = TMessage & { children?: Node[] };
+ /** The tree `buildTree` files the list as: one id path per root, depth first. */
+ const chains = (messages: TMessage[]) => {
+ const walk = (node: Node): string[] => [
+ node.messageId,
+ ...(node.children ?? []).flatMap((child) => walk(child)),
+ ];
+ return ((buildTree({ messages }) ?? []) as Node[]).map((root) => walk(root));
+ };
const replaced = {
status: 'replaced' as const,
generationProtocolVersion: 2,
@@ -1322,46 +1484,121 @@ describe('Chat attachments and reported conversations', () => {
},
);
+ /** Edits the turn's response and the row it overwrote, as another writer of the store would. */
+ const editTurn = (current: TMessage[]) =>
+ current.map((m) =>
+ m.messageId === placeholder.messageId || m.messageId === 'earlier-2'
+ ? { ...m, text: 'Edited' }
+ : m,
+ );
+
+ it.each([
+ [
+ 'by a store subscriber inside the write, over a store that reports writes synchronously',
+ false,
+ true,
+ ],
+ ['by a store subscriber inside the write, over a structurally shared store', true, true],
+ ['by the host after the write, over a store that reports writes synchronously', false, false],
+ ['by the host after the write, over a structurally shared store', true, false],
+ ])(
+ 'keeps an edited turn row and the turn rows above it when a replaced start rolls back, edited %s',
+ async (_label, shared, inside) => {
+ const state = createStore(shared);
+ if (inside) {
+ state.subscribe?.(() => {
+ const current = state.messages;
+ if (current.some((m) => m.messageId === placeholder.messageId && m.text === '')) {
+ state.replaceMessages(editTurn(current));
+ }
+ });
+ }
+ const stream = createStream({ streamId: 'stream-2', generationCreatedAt: 200 });
+ let attachedWith: TMessage[] = [];
+ let answer: (() => void) | undefined;
+ const { chat } = setup(
+ { state, prepareRequest: jest.fn(() => ({ payload, messages: optimistic })) },
+ {
+ sendMessages: jest.fn(
+ () =>
+ new Promise((resolve) => {
+ answer = () => resolve(replaced);
+ }),
+ ),
+ reconnectToStream: jest.fn(async () => {
+ attachedWith = chat.messages;
+ return stream;
+ }),
+ },
+ );
+ const sent = chat.sendMessage({ parts: [{ type: 'text', text: 'Hi' }] });
+ await flush();
+ if (!inside) {
+ state.replaceMessages(editTurn(state.messages));
+ }
+ answer?.();
+ await flush();
+ stream.push(finalEvent({ requestMessage: null, responseMessage: null }));
+ await sent;
+
+ expect(rows(attachedWith)).toEqual([
+ 'earlier-1:Earlier',
+ 'earlier-2:Edited',
+ 'user-1:Hi',
+ 'response-1:Edited',
+ ]);
+ expect(chains(attachedWith)).toEqual([['earlier-1', 'earlier-2', 'user-1', 'response-1']]);
+ },
+ );
+
it.each([
['over a store that reports writes synchronously', false],
['over a structurally shared store', true],
])(
- 'keeps the edits a store subscriber made to the turn rows inside the write when a replaced start rolls back, %s',
+ 'keeps the turn rows above a row another writer appended under them when a replaced start rolls back, %s',
async (_label, shared) => {
const state = createStore(shared);
- state.subscribe?.(() => {
- const current = state.messages;
- if (current.some((m) => m.messageId === placeholder.messageId && m.text === '')) {
- state.replaceMessages(
- current.map((m) =>
- m.messageId === placeholder.messageId || m.messageId === 'earlier-2'
- ? { ...m, text: 'Edited' }
- : m,
- ),
- );
- }
- });
+ const child: TMessage = {
+ ...turnUser,
+ messageId: 'child-1',
+ parentMessageId: placeholder.messageId,
+ text: 'Child',
+ };
const stream = createStream({ streamId: 'stream-2', generationCreatedAt: 200 });
- let attachedWith: string[] = [];
+ let attachedWith: TMessage[] = [];
+ let answer: (() => void) | undefined;
const { chat } = setup(
{ state, prepareRequest: jest.fn(() => ({ payload, messages: optimistic })) },
{
- sendMessages: jest.fn(async () => replaced),
+ sendMessages: jest.fn(
+ () =>
+ new Promise((resolve) => {
+ answer = () => resolve(replaced);
+ }),
+ ),
reconnectToStream: jest.fn(async () => {
- attachedWith = rows(chat.messages);
+ attachedWith = chat.messages;
return stream;
}),
},
);
const sent = chat.sendMessage({ parts: [{ type: 'text', text: 'Hi' }] });
await flush();
+ state.replaceMessages([...state.messages, child]);
+ answer?.();
+ await flush();
stream.push(finalEvent({ requestMessage: null, responseMessage: null }));
await sent;
- expect(attachedWith).toEqual([
+ expect(rows(attachedWith)).toEqual([
'earlier-1:Earlier',
- 'earlier-2:Edited',
- 'response-1:Edited',
+ 'earlier-2:Answer',
+ 'user-1:Hi',
+ 'response-1:',
+ 'child-1:Child',
+ ]);
+ expect(chains(attachedWith)).toEqual([
+ ['earlier-1', 'earlier-2', 'user-1', 'response-1', 'child-1'],
]);
},
);
diff --git a/packages/chat/src/core/chat.ts b/packages/chat/src/core/chat.ts
index fa1ce18ec6..b434dad8d4 100644
--- a/packages/chat/src/core/chat.ts
+++ b/packages/chat/src/core/chat.ts
@@ -491,23 +491,36 @@ const recordOptimisticWrite = (
/**
* Undoes an optimistic write on the list as it stands now: rows it added go, rows it replaced or
* removed come back as they were, and any other row, or one of its rows someone else wrote since,
- * stays as it is.
+ * stays as it is. A row that stays keeps the rows the write added above it, so the tree never loses
+ * the parent of a row it still lists.
*/
const undoOptimisticWrite = (record: OptimisticWrite, current: TMessage[]): TMessage[] => {
- const next: TMessage[] = [];
- const kept = new Set<string>();
- for (const message of current) {
+ const dropped = new Map<string, TMessage>();
+ const undone = current.map((message) => {
if (record.committed.get(message.messageId) !== message) {
- next.push(message);
- kept.add(message.messageId);
- continue;
+ return message;
}
const was = record.replaced.get(message.messageId);
- if (was != null) {
- next.push(was);
- kept.add(message.messageId);
+ if (was == null) {
+ dropped.set(message.messageId, message);
+ }
+ return was;
+ });
+ for (const row of undone) {
+ let parent = row != null ? dropped.get(row.parentMessageId ?? '') : undefined;
+ while (parent != null && dropped.delete(parent.messageId)) {
+ parent = dropped.get(parent.parentMessageId ?? '');
}
}
+ const next: TMessage[] = [];
+ const kept = new Set<string>();
+ current.forEach((message, index) => {
+ const row = dropped.has(message.messageId) ? undefined : (undone[index] ?? message);
+ if (row != null) {
+ next.push(row);
+ kept.add(row.messageId);
+ }
+ });
record.before.forEach((message, index) => {
if (record.removed.has(message.messageId) && !kept.has(message.messageId)) {
next.splice(getRestoreIndex(record.before, index, next), 0, message);
@@ -861,6 +874,10 @@ export class Chat {
if (this.markStreaming()) {
this.notify();
}
+ /** A listener told of that status may have disposed the chat too. */
+ if (this.disposed) {
+ return undefined;
+ }
} else if (event.type === 'final') {
outcome = getFinalOutcome(event.data);
} else if (event.type === 'abort') {
@@ -953,13 +970,24 @@ export class Chat {
): void {
const { state } = this.init;
const status: ChatStatus = outcome.kind === 'error' ? 'error' : 'ready';
- if (state.connection !== 'idle') {
- state.connection = 'idle';
- }
- if (state.status !== status) {
- state.status = status;
+ /**
+ * One terminal state, with the chat's listeners told once it is all set. A store subscriber
+ * told of a field may start the next turn, which then owns the fields this one has not set.
+ */
+ this.writing = true;
+ try {
+ if (state.connection !== 'idle') {
+ state.connection = 'idle';
+ }
+ if (!this.active && state.status !== status) {
+ state.status = status;
+ }
+ if (!this.active) {
+ state.error = outcome.kind === 'error' ? outcome.error : undefined;
+ }
+ } finally {
+ this.writing = false;
}
- state.error = outcome.kind === 'error' ? outcome.error : undefined;
const messages = this.messages;
let message: TMessage | undefined;
if (responseId !== undefined) {
diff --git a/packages/chat/src/react/__tests__/state.spec.ts b/packages/chat/src/react/__tests__/state.spec.ts
index df7eabb1fc..c5a5b3967b 100644
--- a/packages/chat/src/react/__tests__/state.spec.ts
+++ b/packages/chat/src/react/__tests__/state.spec.ts
@@ -136,6 +136,30 @@ describe('ReactQueryChatState', () => {
unsubscribe();
});
+ it('reports a host store write another reader saw between rendering and subscribing', async () => {
+ const queryClient = new QueryClient();
+ let stored = [message('a')];
+ const state = new ReactQueryChatState({
+ queryClient,
+ key: 'convo-1',
+ read: () => stored,
+ write: (next) => {
+ stored = next;
+ },
+ subscribe: () => () => undefined,
+ });
+ const rendered = state.version;
+ stored = [message('b')];
+ const seen = state.version;
+ const listener = jest.fn();
+ const unsubscribe = state.subscribe(listener);
+ await flushMicrotasks();
+
+ expect(seen).not.toBe(rendered);
+ expect(state.version).not.toBe(rendered);
+ unsubscribe();
+ });
+
it('counts writes to a host store that bypass the cache, same array included', () => {
const queryClient = new QueryClient();
const stored = [message('a')];
@@ -219,6 +243,26 @@ describe('ReactQueryChatState', () => {
unsubscribe();
});
+ it('moves past the last version when a reset keeps the same array and count while unsubscribed', () => {
+ const queryClient = new QueryClient();
+ const state = new ReactQueryChatState({ queryClient, key: 'convo-1' });
+ const key = [QueryKeys.messages, 'convo-1'];
+ const list = [message('a')];
+ queryClient.getQueryCache().build(queryClient, {
+ queryKey: key,
+ initialData: list,
+ initialDataUpdatedAt: 1,
+ });
+ queryClient.setQueryData(key, list, { updatedAt: 2 });
+ const before = state.version;
+
+ queryClient.resetQueries({ queryKey: key, exact: true });
+ list[0] = { ...list[0], text: 'changed in place' };
+ queryClient.setQueryData(key, list, { updatedAt: 3 });
+
+ expect(state.version).not.toBe(before);
+ });
+
it('reports a write that creates its cache entry once', async () => {
const queryClient = new QueryClient();
const state = new ReactQueryChatState({ queryClient, key: 'convo-1' });
diff --git a/packages/chat/src/react/state.ts b/packages/chat/src/react/state.ts
index 1a91e3b5d0..ef37cd4e30 100644
--- a/packages/chat/src/react/state.ts
+++ b/packages/chat/src/react/state.ts
@@ -43,6 +43,8 @@ export class ReactQueryChatState implements ChatState {
/** The cache entry and the data `version` last counted, and what it handed out for them. */
private counted?: Query;
private countedData?: unknown;
+ /** When the cache last wrote that data, which moves on a write no subscription heard. */
+ private countedUpdatedAt?: number;
private carried = 0;
private reported?: number;
/** Data events the cache reported for the key, and how many `version` last accounted for. */
@@ -74,9 +76,15 @@ export class ReactQueryChatState implements ChatState {
get version(): number {
const query = this.options.queryClient.getQueryCache().get(this.queryHash);
const data = query?.state.data;
+ const read = this.options.read?.();
let version = this.carried + (query?.state.dataUpdateCount ?? 0) + this.revision;
+ /** A host store's list that moved unheard is a change too, or this read would hide it. */
const changed =
- query !== this.counted || data !== this.countedData || this.changes !== this.countedChanges;
+ query !== this.counted ||
+ data !== this.countedData ||
+ query?.state.dataUpdatedAt !== this.countedUpdatedAt ||
+ read !== this.countedRead ||
+ this.changes !== this.countedChanges;
if (changed && this.reported != null && version <= this.reported) {
/**
* The write count restarts when an entry is created with initial data, removed and created
@@ -87,7 +95,8 @@ export class ReactQueryChatState implements ChatState {
}
this.counted = query;
this.countedData = data;
- this.countedRead = this.options.read?.();
+ this.countedUpdatedAt = query?.state.dataUpdatedAt;
+ this.countedRead = read;
this.countedChanges = this.changes;
this.reported = version;
return version;