Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
perf(chat): load 80 messages when switching chats, older ones on scroll
AdityaUmalefad2d0733dd0
diff --git a/src/state/chat-switch-page.test.ts b/src/state/chat-switch-page.test.tsnew file mode 100644index 0000000000..5cacd36aad--- /dev/null+++ b/src/state/chat-switch-page.test.ts@@ -0,0 +1,56 @@+// @vitest-environment happy-dom+// Opening a chat fetches a short tail so big chats switch quickly; older messages load on scroll.+import { createElement, type Dispatch } from "react";+import { flushSync } from "react-dom";+import { createRoot, type Root } from "react-dom/client";+import { afterEach, beforeEach, expect, it, vi } from "vitest";+import { initialState, StoreProvider, useStore, type Action, type Bot } from "./store";++const profile: Bot = {+ id: "B", name: "B", threadId: "B-old", title: "", description: "", color: "green", unread: false, notifications: true,+ messages: [], modelSelection: { instanceId: "fake", model: "test" },+ tasks: [{ threadId: "B-old", title: "Existing", createdAt: 1 }, { threadId: "B-other", title: "Other", createdAt: 2 }],+};+class OfflineEvents {+ onopen = null;+ onerror = null;+ onmessage = null;+ close() {}+}++const original = { bots: initialState.bots, selectedId: initialState.selectedId };+let root: Root;+let host: HTMLDivElement;+let dispatch: Dispatch<Action>;+let requests: ReturnType<typeof vi.fn<typeof fetch>>;+function Probe() {+ dispatch = useStore().dispatch;+ return null;+}++beforeEach(() => {+ Object.assign(initialState, { bots: [profile], selectedId: "B" });+ requests = vi.fn<typeof fetch>(() => new Promise(() => {}));+ vi.stubGlobal("fetch", requests);+ vi.stubGlobal("EventSource", OfflineEvents);+ host = document.createElement("div");+ document.body.append(host);+ root = createRoot(host);+ flushSync(() => root.render(createElement(StoreProvider, null, createElement(Probe))));+});+afterEach(() => {+ flushSync(() => root.unmount());+ host.remove();+ Object.assign(initialState, original);+ vi.unstubAllGlobals();+});++it("asks for the newest 80 messages when switching to a bot's chat", async () => {+ flushSync(() => dispatch({ type: "switchTask", botId: "B", threadId: "B-other" }));+ await vi.waitFor(() => expect(requests).toHaveBeenCalledWith("/api/bots/B/tasks/B-other?messages=80", expect.objectContaining({ method: "POST" })));+});++it("still asks for the whole chat when a jump needs it", async () => {+ flushSync(() => dispatch({ type: "switchTask", botId: "B", threadId: "B-other", whole: true }));+ await vi.waitFor(() => expect(requests).toHaveBeenCalledWith("/api/bots/B/tasks/B-other", expect.objectContaining({ method: "POST" })));+});diff --git a/src/state/room-navigation.test.ts b/src/state/room-navigation.test.tsindex ddb2970715..0145261003 100644--- a/src/state/room-navigation.test.ts+++ b/src/state/room-navigation.test.ts@@ -55,7 +55,7 @@ const switchTo = async (threadId: string) => {flushSync(() => dispatch({ type: "switchGroupTask", groupId: "room", threadId }));await settle();};-const switchPath = (threadId: string) => `/api/groups/room/tasks/${threadId}?messages=200`;+const switchPath = (threadId: string) => `/api/groups/room/tasks/${threadId}?messages=80`;beforeAll(async () => {vi.stubGlobal("EventSource", FixtureEventSource);diff --git a/src/state/store.tsx b/src/state/store.tsxindex 8d2e287cfc..f5052c87cc 100644--- a/src/state/store.tsx+++ b/src/state/store.tsx@@ -1921,7 +1921,7 @@ export { api, ApiError, liveCallFromFrame, nextLiveCallLookup };/** Messages per snapshot thread and per scrollback page: the server's own page maximum. */export const MESSAGE_PAGE_SIZE = 200;-+export const SWITCH_MESSAGE_COUNT = 80; // a chat switch loads a short tail so big chats open fast; older messages load on scrolltype TrustedApprovalBridge = {setMode(@@ -2829,7 +2829,7 @@ export function StoreProvider({ children }: { children: ReactNode }) {: Promise.resolve();void ready.then(() => api<{ bot: Bot }>(action.type === "newTask"? `/api/bots/${action.botId}/tasks`- : `/api/bots/${action.botId}/tasks/${action.threadId}${action.whole ? "" : `?messages=${MESSAGE_PAGE_SIZE}`}`, { method: "POST", body: JSON.stringify(action.type === "newTask" ? { projectId: action.projectId, cwd: action.cwd } : {}) }))+ : `/api/bots/${action.botId}/tasks/${action.threadId}${action.whole ? "" : `?messages=${SWITCH_MESSAGE_COUNT}`}`, { method: "POST", body: JSON.stringify(action.type === "newTask" ? { projectId: action.projectId, cwd: action.cwd } : {}) })).then((r) => {if (action.type === "newTask") { settle(null); action.onCreated?.(); }if (navigation.get(action.botId) !== revision) return;@@ -2872,7 +2872,7 @@ export function StoreProvider({ children }: { children: ReactNode }) {.catch(showError);break;case "switchGroupTask":- api<{ group?: Partial<Group> & { id: string } }>(`/api/groups/${action.groupId}/tasks/${action.threadId}?messages=${MESSAGE_PAGE_SIZE}`, { method: "POST" })+ api<{ group?: Partial<Group> & { id: string } }>(`/api/groups/${action.groupId}/tasks/${action.threadId}?messages=${SWITCH_MESSAGE_COUNT}`, { method: "POST" }).then(roomAnswer(action.groupId, true)).catch(showError);break;