Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Keep an open hand-off when a reconnect snapshot still names the older thread.
Ryand5adf5b8775a
diff --git a/src/state/snapshot-navigation.keep.test.ts b/src/state/snapshot-navigation.keep.test.tsnew file mode 100644index 0000000000..536d5f834a--- /dev/null+++ b/src/state/snapshot-navigation.keep.test.ts@@ -0,0 +1,61 @@+import { describe, expect, it } from "vitest";+import type { Bot, Message } from "./store";+import { keepBotNavigation } from "./snapshot-navigation";++const line = (id: string): Message => ({ id, at: 1, role: "user", kind: "text", text: id, parentId: null });++function fixture(threadId: string, handoff = false): Bot {+ return {+ id: "bot", threadId, name: "Fixture", title: "", description: "", notifications: false,+ unread: false, color: "green", modelSelection: { instanceId: "fake", model: "fixture" },+ messages: [line(threadId)],+ tasks: [+ { threadId: "main", title: "Main", createdAt: 1 },+ { threadId: "work", title: "Work", createdAt: 2, ...(handoff ? { openedBy: { botId: "lead", name: "Lead", at: 2 } } : {}) },+ ],+ };+}++describe("a reconnect snapshot must not replace a hand-off the person is reading", () => {+ it("keeps that hand-off when the server still names the bot's previous thread", () => {+ const incoming = fixture("main", true);+ incoming.messages = [line("old-tail")];+ const kept = keepBotNavigation(fixture("work", true), incoming, false);+ expect(kept.threadId).toBe("work");+ expect(kept.messages.map(message => message.id)).toEqual(["work"]);+ });++ it("follows the server thread when the open chat was not opened by another bot", () => {+ const kept = keepBotNavigation(fixture("work"), fixture("main"), false);+ expect(kept.threadId).toBe("main");+ expect(kept.messages.map(message => message.id)).toEqual(["main"]);+ });++ it("keeps history already loaded when a later snapshot of the same hand-off is only a tail", () => {+ const previous = fixture("work", true);+ previous.messages = [line("work"), line("loaded")];+ previous.hasMore = true;+ const incoming = fixture("work", true);+ incoming.messages = [line("loaded"), line("tail")];+ incoming.hasMore = false;+ const kept = keepBotNavigation(previous, incoming, false);+ expect(kept.threadId).toBe("work");+ expect(kept.messages.map(message => message.id)).toEqual(["work", "loaded", "tail"]);+ expect(kept.hasMore).toBe(true);+ });++ it("keeps a hand-off and its pending switch until that switch resolves", () => {+ const previous = fixture("work", true);+ previous.switchingTo = "main";+ const kept = keepBotNavigation(previous, fixture("main", true), false);+ expect(kept.threadId).toBe("work");+ expect(kept.switchingTo).toBe("main");+ expect(kept.messages.map(message => message.id)).toEqual(["work"]);+ });++ it("follows the server when that hand-off no longer exists", () => {+ const incoming = fixture("main");+ incoming.tasks = [{ threadId: "main", title: "Main", createdAt: 1 }];+ expect(keepBotNavigation(fixture("work", true), incoming, false).threadId).toBe("main");+ });+});diff --git a/src/state/snapshot-navigation.ts b/src/state/snapshot-navigation.tsindex 3846c6a033..e283f1e38d 100644--- a/src/state/snapshot-navigation.ts+++ b/src/state/snapshot-navigation.ts@@ -38,17 +38,37 @@ function keepNewTasks<T extends { threadId: string }>(previous: T[] | undefined,return created.length ? [...(incoming ?? []), ...created] : incoming;}+function viewingHandoff(previous: Bot, incoming: Bot): boolean {+ const task = previous.tasks?.find(candidate => candidate.threadId === previous.threadId);+ 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;+}+export function keepBotNavigation(previous: Bot | undefined, incoming: Bot, preserve: boolean, taskIdsAtStart?: ReadonlySet<string>): Bot {- if (!preserve || !previous) return incoming;+ // A reconnect snapshot does not preserve navigation, and the server often still names the older thread.+ // A hand-off another bot opened stays on screen while that thread remains.+ const keepHandoff = previous !== undefined && !preserve && viewingHandoff(previous, incoming);+ if ((!preserve && !keepHandoff) || !previous) return incoming;+ const sameOpenHandoff = keepHandoff && incoming.threadId === previous.threadId;return {...incoming,threadId: previous.threadId,tasks: keepNewTasks(previous.tasks, incoming.tasks, taskIdsAtStart),switchingTo: previous.switchingTo,awaitingThreadSnapshot: previous.awaitingThreadSnapshot,- messages: previous.messages,- hasMore: previous.hasMore,- activeLeafId: previous.activeLeafId,+ messages: sameOpenHandoff ? reconcileTranscript(previous.messages, incoming.messages) : previous.messages,+ hasMore: sameOpenHandoff ? Boolean(previous.hasMore || incoming.hasMore) : previous.hasMore,+ activeLeafId: sameOpenHandoff ? incoming.activeLeafId ?? previous.activeLeafId : previous.activeLeafId,pinnedMessageId: previous.pinnedMessageId,};}