Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
Check snapshots in one helper, and cover instances and webhooks
milind-soni413dfca71cfc
diff --git a/src/state/api.ts b/src/state/api.tsindex a667320a26..23fa4a905b 100644--- a/src/state/api.ts+++ b/src/state/api.ts@@ -65,3 +65,10 @@ function heldUntilShown<T>(path: string, init: ApiInit): Promise<T> {void sent.finally(() => { if (heldReads.get(path) === sent) heldReads.delete(path); }).catch(() => {});return sent;}++/** A 200 that isn't the snapshot: `api` turns a non-JSON answer into `{}`, and the reducer can't take a missing list. The part+ * fails instead, which keeps what's on screen. */+export function snapshotArrays<T>(answer: T, ...keys: (keyof T)[]): T {+ for (const key of keys) if (!Array.isArray(answer[key])) throw new Error(`Invalid snapshot: ${String(key)} is not a list`);+ return answer;+}diff --git a/src/state/store.test.ts b/src/state/store.test.tsindex 4fcb4e4c35..7cd38174c9 100644--- a/src/state/store.test.ts+++ b/src/state/store.test.ts@@ -2945,6 +2945,20 @@ describe("restoring the open conversation", () => {expect(seen.routinesLoadState).toBe("error");});+ it("keeps the engines and webhooks it had when a reconnect receives empty answers for them", async () => {+ const { stream } = await boot();+ stream.send(hello());+ await settle();+ vi.stubGlobal("fetch", (path: string) => Promise.resolve(new Response(JSON.stringify(+ path.startsWith("/api/bots?") ? { bots: [first, second], groups: [room] } : {},+ ))));+ stream.send(hello());+ await settle();+ expect(container?.querySelector("#snapshot-crash")).toBeNull();+ expect(Array.isArray(seen.instances)).toBe(true);+ expect(Array.isArray(seen.webhooks)).toBe(true);+ });+it("the provider remembers each non-empty selection and never an empty one", async () => {const { stored, stream } = await boot();diff --git a/src/state/store.tsx b/src/state/store.tsxindex 53fdce418e..33ed80cb31 100644--- a/src/state/store.tsx+++ b/src/state/store.tsx@@ -1,5 +1,5 @@import { cloudAsleep, onCloudPower } from "../lib/cloud-power";-import { api, ApiError } from "./api";+import { api, ApiError, snapshotArrays } from "./api";import {createContext,useCallback,@@ -2962,7 +2962,7 @@ export function StoreProvider({ children }: { children: ReactNode }) {{key: "instances",request: async () => {- const answer = await unlessRefused<{ instances: InstanceInfo[] } | null>("/api/instances", () => api("/api/instances"), null);+ const answer = await unlessRefused<{ instances: InstanceInfo[] } | null>("/api/instances", async () => snapshotArrays(await api("/api/instances"), "instances"), null);return () => { if (answer) rawDispatch({ type: "instances", instances: answer.instances }); };},},@@ -2976,8 +2976,7 @@ export function StoreProvider({ children }: { children: ReactNode }) {{key: "routines",request: async () => {- const since = Date.now() - ROUTINE_RUNS_WINDOW_MS, { routines, runs } = await api(`/api/routines?since=${since}`);- if (!Array.isArray(routines) || !Array.isArray(runs)) throw new Error("Invalid routines snapshot");+ const since = Date.now() - ROUTINE_RUNS_WINDOW_MS, { routines, runs } = snapshotArrays(await api(`/api/routines?since=${since}`), "routines", "runs");return () => rawDispatch({ type: "routinesHydrated", routines, runs, since });},},@@ -2991,7 +2990,7 @@ export function StoreProvider({ children }: { children: ReactNode }) {...(window.ogb?.remoteClient?.active ? [] : [{key: "webhooks",request: async () => {- const { webhooks, attempts, ingress } = await api("/api/webhooks");+ const { webhooks, attempts, ingress } = snapshotArrays(await api("/api/webhooks"), "webhooks");return () =>rawDispatch({ type: "webhooksHydrated", webhooks, attempts: attempts ?? [], ingress });},@@ -3056,8 +3055,7 @@ export function StoreProvider({ children }: { children: ReactNode }) {}).catch(() => {});const chat = () =>- api(`/api/bots?messages=${MESSAGE_PAGE_SIZE}`).then(({ bots, groups, sections, computerControl, botQueuedMessages }) => {- if (!Array.isArray(bots) || (groups != null && !Array.isArray(groups))) throw new Error("Invalid bots snapshot");+ api(`/api/bots?messages=${MESSAGE_PAGE_SIZE}`).then((answer) => snapshotArrays(answer, "bots")).then(({ bots, groups, sections, computerControl, botQueuedMessages }) => {if (!alive) return;rawDispatch({type: "hydrate",