Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Keep visible panel previews and defer sidebar reads while hidden
vincent468e929419e2
diff --git a/src/components/Sidebar.conversation-preview.dom.test.ts b/src/components/Sidebar.conversation-preview.dom.test.tsindex 6fa3943cca..6ea0cbe07c 100644--- a/src/components/Sidebar.conversation-preview.dom.test.ts+++ b/src/components/Sidebar.conversation-preview.dom.test.ts@@ -169,3 +169,25 @@ it("does not retry a failed sidebar request on an unrelated rerender", async ()await visible("first");expect(network).toHaveBeenCalledTimes(2);});++it("waits until the document is visible before starting a sidebar read", async () => {+ const state = vi.spyOn(document, "visibilityState", "get").mockReturnValue("hidden");+ try {+ await render();+ await visible("first");+ expect(network).not.toHaveBeenCalled();+ state.mockReturnValue("visible");+ await act(async () => { document.dispatchEvent(new Event("visibilitychange")); });+ expect(row("first").textContent).toContain("First answer");+ } finally {+ state.mockRestore();+ }+});++it("keeps every expanded panel preview when the list exceeds the shared cache", async () => {+ const tasks = Array.from({ length: 205 }, (_, i) => ({ threadId: `many-${i}`, title: `Thread ${i}`, createdAt: i + 1 }));+ showAllConversations(bot.id);+ await act(async () => root.render(createElement(StoreProvider, null, createElement(BotConversations, { bot: { ...bot, tasks } }))));+ expect(network).toHaveBeenCalledTimes(205);+ expect(host.querySelectorAll("[data-conversation]")).toHaveLength(205);+});diff --git a/src/components/bot-panel/Conversations.tsx b/src/components/bot-panel/Conversations.tsxindex 0c043f8908..870d6366e9 100644--- a/src/components/bot-panel/Conversations.tsx+++ b/src/components/bot-panel/Conversations.tsx@@ -20,13 +20,12 @@ const MAX_CACHED = 200;type Page = { messages?: Message[]; activeLeafId?: string | null };const fetched = new Map<string, Message[]>();-const asked = new Set<string>();-const requests = new Map<string, { promise: Promise<void>; signal?: AbortSignal; promote?: () => void }>();+const requests = new Map<string, { promise: Promise<Message[] | undefined>; signal?: AbortSignal; promote?: () => void }>();let generation = 0;const lineKey = (task: { threadId: string; updatedAt?: number; createdAt?: number }) => `${task.threadId}@${threadRecency(task)}`;-function fetchLine(key: string, signal?: AbortSignal, promote?: () => void): Promise<void> {+function fetchLine(key: string, signal?: AbortSignal, promote?: () => void): Promise<Message[] | undefined> {const existing = requests.get(key);if (existing) {if (existing.signal?.aborted) {@@ -42,12 +41,14 @@ function fetchLine(key: string, signal?: AbortSignal, promote?: () => void): Pro}const threadId = key.slice(0, key.lastIndexOf("@"));const currentGeneration = generation;- const entry = { promise: Promise.resolve(), signal, promote };+ const entry = { promise: Promise.resolve<Message[] | undefined>(undefined), signal, promote };entry.promise = api<Page>(`/api/threads/${encodeURIComponent(threadId)}/messages?limit=${LAST_LINE_PAGE}`, signal ? { background: true, signal } : { background: true }).then((page) => {if (currentGeneration !== generation || signal?.aborted) return;- fetched.set(key, visibleMessages({ messages: page.messages ?? [], activeLeafId: page.activeLeafId ?? undefined }));+ const lines = visibleMessages({ messages: page.messages ?? [], activeLeafId: page.activeLeafId ?? undefined });+ fetched.set(key, lines);if (fetched.size > MAX_CACHED) fetched.delete(fetched.keys().next().value!);+ return lines;}).finally(() => { if (requests.get(key) === entry) requests.delete(key); });requests.set(key, entry);@@ -57,18 +58,27 @@ function fetchLine(key: string, signal?: AbortSignal, promote?: () => void): Pro/** Each listed conversation's newest messages: the open one from the chat, the rest fetched once per change. */function useRecentMessages(bot: Bot, threads: readonly { threadId: string; updatedAt?: number; createdAt?: number }[]): (threadId: string) => Message[] | undefined {const [, settle] = useState(0);- const wanted = threads.filter((task) => task.threadId !== bot.threadId).map(lineKey);- const missing = wanted.filter((key) => !fetched.has(key) && !asked.has(key)).join(",");+ const panel = useRef(new Map<string, Message[]>());+ const wanted = JSON.stringify(threads.filter((task) => task.threadId !== bot.threadId).map(lineKey));useEffect(() => {- if (!missing) return;- for (const key of missing.split(",")) {- asked.add(key);- void fetchLine(key).then(() => settle((count) => count + 1)).catch(() => {}).finally(() => asked.delete(key));+ let active = true;+ const keys = new Set<string>(JSON.parse(wanted));+ for (const key of panel.current.keys()) if (!keys.has(key)) panel.current.delete(key);+ for (const key of keys) {+ const cached = fetched.get(key);+ if (cached) panel.current.set(key, cached);+ if (!panel.current.has(key)) void fetchLine(key).then((lines) => {+ if (active && lines) {+ panel.current.set(key, lines);+ settle((count) => count + 1);+ }+ }).catch(() => {});}- }, [missing]);+ return () => { active = false; };+ }, [wanted]);const open = visibleMessages(bot);const byThread = new Map(threads.map((task) => [task.threadId, lineKey(task)]));- return (threadId) => threadId === bot.threadId ? open : fetched.get(byThread.get(threadId) ?? "");+ return (threadId) => threadId === bot.threadId ? open : panel.current.get(byThread.get(threadId) ?? "") ?? fetched.get(byThread.get(threadId) ?? "");}const sidebarQueue = new Map<string, Set<() => void>>();@@ -76,6 +86,10 @@ const sidebarActive = new Map<string, { listeners: Set<() => void>; cancel: () =let sidebarRunning = 0;function startSidebarLines() {+ if (document.visibilityState === "hidden" && sidebarQueue.size) {+ document.addEventListener("visibilitychange", startSidebarLines, { once: true });+ return;+ }for (const [key, listeners] of sidebarQueue) {if (sidebarRunning >= 4) break;sidebarQueue.delete(key);@@ -142,7 +156,6 @@ export function showAllConversations(botId: string): void {export function forgetConversationLines(): void {generation++;fetched.clear();- asked.clear();sidebarQueue.clear();for (const active of sidebarActive.values()) { active.listeners.clear(); active.cancel(); }sidebarActive.clear();