Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(chat): a reconnect snapshot of the open hand-off is the server's copy from its first row on

milind-soni94ec05663f11
diff --git a/src/state/snapshot-navigation.keep.test.ts b/src/state/snapshot-navigation.keep.test.ts
index e18becdf57..8f39c435e3 100644
--- a/src/state/snapshot-navigation.keep.test.ts
+++ b/src/state/snapshot-navigation.keep.test.ts
@@ -1,6 +1,7 @@
import { describe, expect, it } from "vitest";
import type { Bot, Message } from "./store";
import { keepBotNavigation } from "./snapshot-navigation";
+import { optimisticMessageId } from "./optimistic-send";
const line = (id: string): Message => ({ id, at: 1, role: "user", kind: "text", text: id, parentId: null });
@@ -76,4 +77,27 @@ describe("a reconnect snapshot must not replace a hand-off the person is reading
incoming.tasks = [{ threadId: "main", title: "Main", createdAt: 1 }];
expect(keepBotNavigation(fixture("work", true), incoming, false).threadId).toBe("main");
});
+
+ it("lets the same hand-off's snapshot replace a confirmed send and drop a row the server no longer has", () => {
+ const previous = fixture("work", true);
+ previous.messages = [line("older"), line("work"), line("gone"), { ...line(optimisticMessageId("s1")), sendId: "s1" }];
+ previous.hasMore = true;
+ const incoming = fixture("work", true);
+ incoming.messages = [line("work"), { ...line("sent"), sendId: "s1" }];
+ incoming.hasMore = true;
+ const kept = keepBotNavigation(previous, incoming, false);
+ expect(kept.messages.map(message => message.id)).toEqual(["older", "work", "sent"]);
+ expect(kept.hasMore).toBe(true);
+ });
+
+ it("takes the same hand-off's snapshot whole when it shares no row with what is on screen", () => {
+ const previous = fixture("work", true);
+ previous.messages = [line("a"), line("b")];
+ const incoming = fixture("work", true);
+ incoming.messages = [line("c"), line("d")];
+ incoming.hasMore = true;
+ const kept = keepBotNavigation(previous, incoming, false);
+ expect(kept.messages.map(message => message.id)).toEqual(["c", "d"]);
+ expect(kept.hasMore).toBe(true);
+ });
});
diff --git a/src/state/snapshot-navigation.ts b/src/state/snapshot-navigation.ts
index a4ac0394ba..5867f06f7c 100644
--- a/src/state/snapshot-navigation.ts
+++ b/src/state/snapshot-navigation.ts
@@ -61,15 +61,11 @@ function viewingHandoff(previous: Bot, incoming: Bot): boolean {
return Boolean(task?.openedBy?.botId) && (incoming.tasks ?? []).some(candidate => candidate.threadId === previous.threadId);
}
-function reconcileTranscript(previous: Message[], incoming: Message[]): Message[] {
- const incomingById = new Map(incoming.map(message => [message.id, message]));
- const seen = new Set<string>();
- const merged = previous.map(message => {
- seen.add(message.id);
- return incomingById.get(message.id) ?? message;
- });
- for (const message of incoming) if (!seen.has(message.id)) merged.push(message);
- return merged;
+/** The snapshot is the server's copy of the hand-off from its first row on, so a send it confirmed or a row it no longer
+ * has does not linger; only the rows loaded before that first row stay. Without an overlap the snapshot wins. */
+function reconcileTranscript(previous: Message[], incoming: Message[]): { messages: Message[]; earlier: boolean } {
+ const start = incoming[0] ? previous.findIndex(message => message.id === incoming[0]!.id) : -1;
+ return start > 0 ? { messages: [...previous.slice(0, start), ...incoming], earlier: true } : { messages: incoming, earlier: false };
}
export function keepBotNavigation(previous: Bot | undefined, incoming: Bot, preserve: boolean, taskIdsAtStart?: ReadonlySet<string>): Bot {
@@ -78,14 +74,15 @@ export function keepBotNavigation(previous: Bot | undefined, incoming: Bot, pres
const keepHandoff = previous !== undefined && !preserve && viewingHandoff(previous, incoming);
if ((!preserve && !keepHandoff) || !previous) return incoming;
const sameOpenHandoff = keepHandoff && incoming.threadId === previous.threadId;
+ const reconciled = sameOpenHandoff ? reconcileTranscript(previous.messages, incoming.messages) : null;
return {
...incoming,
threadId: previous.threadId,
tasks: keepNewTasks(previous.tasks, incoming.tasks, taskIdsAtStart),
switchingTo: previous.switchingTo,
awaitingThreadSnapshot: previous.awaitingThreadSnapshot,
- messages: sameOpenHandoff ? reconcileTranscript(previous.messages, incoming.messages) : previous.messages,
- hasMore: sameOpenHandoff ? Boolean(previous.hasMore || incoming.hasMore) : previous.hasMore,
+ messages: reconciled ? reconciled.messages : previous.messages,
+ hasMore: reconciled ? (reconciled.earlier ? previous.hasMore : incoming.hasMore) : previous.hasMore,
activeLeafId: sameOpenHandoff ? (incoming.activeLeafId === undefined ? previous.activeLeafId : incoming.activeLeafId) : previous.activeLeafId,
pinnedMessageId: previous.pinnedMessageId,
};