Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
perf: inbox rows find their conversation's newest message without loading the thread
mattmillartech07490c70c247
diff --git a/server/host/place-inbox.test.ts b/server/host/place-inbox.test.tsindex 965205708e..d0b1d0c8dd 100644--- a/server/host/place-inbox.test.ts+++ b/server/host/place-inbox.test.ts@@ -1,7 +1,7 @@import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";import { tmpdir } from "node:os";import { join } from "node:path";-import { afterEach, describe, expect, it } from "vitest";+import { afterEach, describe, expect, it, vi } from "vitest";import type { Notification } from "../../shared/notification.ts";import type { PersonWait } from "../../shared/person-wait.ts";@@ -47,6 +47,7 @@ function setup(over: Partial<PlaceInboxDeps> = {}, dataDir = mkdtempSync(join(tm},patchTask: () => undefined, patchGroup: () => undefined,messagesFor: (threadId: string) => said.get(threadId) ?? [],+ newestMessageId: (threadId: string) => said.get(threadId)?.at(-1)?.id,activePath: (threadId: string) => said.get(threadId) ?? [],onChange: (listener: (change: { type: string; threadId: string }) => void) => { listeners.push(listener); return () => undefined; },};@@ -141,6 +142,17 @@ describe("finished work reaches the inbox once", () => {f.inbox.route(summary, { summary: true });expect(f.inbox.view(OWNER).rows[0]!.origin?.messageId).toBe(posted.id);});++ it("finds that message without loading the whole conversation", () => {+ const f = setup();+ f.store.appendMessage("eli-main", { role: "bot", text: "Earlier." });+ const posted = f.store.appendMessage("eli-main", { role: "bot", text: "All three pages are live." });+ f.inbox.route(summary, { summary: true });+ const whole = vi.spyOn(f.store, "messagesFor");+ expect(f.inbox.view(OWNER).rows[0]!.origin?.messageId).toBe(posted.id);+ expect(f.inbox.count(OWNER)).toBe(1);+ expect(whole).not.toHaveBeenCalled();+ });});describe("who is asking for the inbox", () => {diff --git a/server/host/place-inbox.ts b/server/host/place-inbox.tsindex 1de349d759..c043b9cf2b 100644--- a/server/host/place-inbox.ts+++ b/server/host/place-inbox.ts@@ -66,7 +66,7 @@ export function placeInboxViewer(auth: RequestAuth, cloud: { personal: boolean;export interface PlaceInboxDeps {dataDir: string;- store: Pick<Store, "bot" | "botByThread" | "taskByThread" | "groupByThread" | "appendMessage" | "patchTask" | "patchGroup" | "messagesFor" | "activePath" | "onChange">;+ store: Pick<Store, "bot" | "botByThread" | "taskByThread" | "groupByThread" | "appendMessage" | "patchTask" | "patchGroup" | "messagesFor" | "newestMessageId" | "activePath" | "onChange">;openCards(): OpenCardRow[];/** A running turn waits on this card's answer. */live(threadId: string, requestId: string): boolean;@@ -194,7 +194,7 @@ export function createPlaceInbox(deps: PlaceInboxDeps) {/** The message a line points at: its own message, or the newest one in the conversation it came from, so a client* can put the person at it. */- const focusOf = (line: Line): string | undefined => line.kind === "message" ? line.id : store.messagesFor(line.threadId).at(-1)?.id;+ const focusOf = (line: Line): string | undefined => line.kind === "message" ? line.id : store.newestMessageId(line.threadId);const lineRow = (line: Line): InboxRow => {const focus = focusOf(line);diff --git a/server/store.test.ts b/server/store.test.tsindex f6dc4ca489..7d0e1c881d 100644--- a/server/store.test.ts+++ b/server/store.test.ts@@ -726,6 +726,25 @@ describe("Store", () => {expect(whole.hasMore).toBe(false);});+ it("newestMessageId answers from the cached thread, or with one indexed read when it is not cached", () => {+ const store = new Store(selection);+ const bot = store.createBot({}, { seedMessages: false });+ expect(store.newestMessageId(bot.threadId)).toBeUndefined();+ store.appendMessage(bot.threadId, { role: "user", kind: "text", text: "first" });+ const last = store.appendMessage(bot.threadId, { role: "bot", kind: "text", text: "second" });+ expect(store.newestMessageId(bot.threadId)).toBe(last.id);+ flushLaterSaves();+ const read = vi.spyOn(mdb, "readThread");+ try {+ const fresh = new Store(selection);+ expect(fresh.newestMessageId(bot.threadId)).toBe(last.id);+ expect(fresh.newestMessageId("no-such-thread")).toBeUndefined();+ expect(read).not.toHaveBeenCalled();+ } finally {+ read.mockRestore();+ }+ });+it("caches a complete tail but never caches a zero-message page as an empty thread", () => {const store = new Store(selection);const bot = store.createBot({}, { seedMessages: false });diff --git a/server/store.ts b/server/store.tsindex 8bd2ccc0b8..75ef4bcd5b 100644--- a/server/store.ts+++ b/server/store.ts@@ -1073,6 +1073,12 @@ export class Store {return this.thread(threadId).messages;}+ newestMessageId(threadId: string): string | undefined {+ const cached = this.threads.get(threadId);+ if (cached) return cached.messages.at(-1)?.id;+ return lastMessageId(threadId) ?? undefined;+ }+messagesBefore(threadId: string, before: string, limit: number) {return messagesBefore(threadId, before, limit, this.threads.get(threadId), () => this.thread(threadId));}diff --git a/server/testing/team-inbox-fixture.ts b/server/testing/team-inbox-fixture.tsindex 15f6afab5a..f3c1c8401d 100644--- a/server/testing/team-inbox-fixture.ts+++ b/server/testing/team-inbox-fixture.ts@@ -81,6 +81,7 @@ export function teamInbox(options: { team?: boolean; place?: boolean; named?: Te},patchGroup: (id: string) => { unread.push(id); return null; },messagesFor: (threadId: string) => threads.get(threadId) ?? [],+ newestMessageId: (threadId: string) => threads.get(threadId)?.at(-1)?.id,activePath: (threadId: string) => threads.get(threadId) ?? [],onChange: (listener: (change: StoreChange) => void) => { listeners.add(listener); return () => listeners.delete(listener); },};