Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

perf batch 9-11 follow-ups: keep a long tools list's first pages, refresh room budgets on use, one widget text source

milind-soni71e14fadd4e6
diff --git a/packages/phone-protocol/src/widget-text.ts b/packages/phone-protocol/src/widget-text.ts
index 08254acc6d..e5e59f0511 100644
--- a/packages/phone-protocol/src/widget-text.ts
+++ b/packages/phone-protocol/src/widget-text.ts
@@ -26,13 +26,14 @@ const STYLES = new Set<WidgetMoment["style"]>(["clock", "bareClock", "longDate",
const MAX_PARAMS = 8;
const MAX_DEPTH = 3;
const MAX_STRING = 400;
-const WIDGET_FALLBACK_LIMIT = 1_500;
+/** The longest readable version of a widget (WIDGET_LIMITS.fallback). */
+export const WIDGET_FALLBACK_LIMIT = 1_500;
const isRecord = (value: unknown): value is Record<string, unknown> => value !== null && typeof value === "object" && !Array.isArray(value);
-const widgetString = (node: WidgetNode, key: string): string | undefined => (typeof node[key] === "string" ? node[key] as string : undefined);
-const widgetNumber = (node: WidgetNode, key: string): number | undefined => (typeof node[key] === "number" ? node[key] as number : undefined);
+export const widgetString = (node: WidgetNode, key: string): string | undefined => (typeof node[key] === "string" ? node[key] as string : undefined);
+export const widgetNumber = (node: WidgetNode, key: string): number | undefined => (typeof node[key] === "number" ? node[key] as number : undefined);
-function buttonSpecOf(root: WidgetRoot, key: "confirm" | "cancel"): WidgetButtonSpec | undefined {
+export function buttonSpecOf(root: WidgetRoot, key: "confirm" | "cancel"): WidgetButtonSpec | undefined {
const spec = root[key];
return isRecord(spec) && typeof spec.label === "string" && isRecord(spec.action) ? spec as unknown as WidgetButtonSpec : undefined;
}
diff --git a/packages/phone-protocol/src/widget.ts b/packages/phone-protocol/src/widget.ts
index b23d2c4202..55953e0035 100644
--- a/packages/phone-protocol/src/widget.ts
+++ b/packages/phone-protocol/src/widget.ts
@@ -3,8 +3,8 @@
// Model input is normalised here once; every renderer reads canonical trees only.
import type { JsonValue } from "./json.ts";
import { childNodes, nodeTypeProblem } from "./widget-input.ts";
-import { dayList, readWidgetText, widgetToText } from "./widget-text.ts";
-export { dayList, widgetToText };
+import { buttonSpecOf, dayList, readWidgetText, WIDGET_FALLBACK_LIMIT, widgetNumber, widgetString, widgetToText } from "./widget-text.ts";
+export { dayList, widgetNumber, widgetString, widgetToText };
import { buttonStyle } from "./widget-button-style.ts";
export const WIDGET_VERSION = 1;
@@ -29,7 +29,7 @@ export const WIDGET_LIMITS = {
label: 80,
actionPayloadBytes: 2_000,
fieldValue: 2_000,
- fallback: 1_500,
+ fallback: WIDGET_FALLBACK_LIMIT,
} as const;
/** ChatKit's icon names, plus a few this app needs. Each platform maps them
@@ -653,13 +653,6 @@ export function parseWidgetInput(value: unknown): ParsedWidgetInput {
}
-export const widgetString = (node: WidgetNode, key: string): string | undefined => (typeof node[key] === "string" ? node[key] as string : undefined);
-export const widgetNumber = (node: WidgetNode, key: string): number | undefined => (typeof node[key] === "number" ? node[key] as number : undefined);
-
-function buttonSpecOf(root: WidgetRoot, key: "confirm" | "cancel"): WidgetButtonSpec | undefined {
- const spec = root[key];
- return isRecord(spec) && typeof spec.label === "string" && isRecord(spec.action) ? spec as unknown as WidgetButtonSpec : undefined;
-}
function walk(node: WidgetNode, visit: (node: WidgetNode, ancestors: WidgetNode[]) => void, ancestors: WidgetNode[] = []): void {
visit(node, ancestors);
diff --git a/server/connector-tools-list.test.ts b/server/connector-tools-list.test.ts
index 309dee95a8..3407a1a0aa 100644
--- a/server/connector-tools-list.test.ts
+++ b/server/connector-tools-list.test.ts
@@ -245,4 +245,16 @@ describe("bounded tools/list cache", () => {
expect(newest.status).toBe(200);
expect(await newest.json()).toEqual({ jsonrpc: "2.0", id: "retry-130", result: { tools: [{ name: "tool-130" }] } });
});
+
+ it("keeps a long list's first pages past the page cap, so a fallback walk can still start", async () => {
+ const backend = "many-pages";
+ const page = (id: string, cursor?: string) => ({ jsonrpc: "2.0", id, method: "tools/list", params: cursor === undefined ? {} : { cursor } });
+ const reply = (id: string, name: string, nextCursor?: string) => Response.json({ jsonrpc: "2.0", id, result: { tools: [{ name }], nextCursor } });
+ const down = async () => new Response(null, { status: 503 });
+ await withToolsListFallback(backend, page("first"), async () => reply("first", "tool-0", "c1"));
+ for (let i = 1; i <= 20; i++) await withToolsListFallback(backend, page(`p${i}`, `c${i}`), async () => reply(`p${i}`, `tool-${i}`, `c${i + 1}`));
+ expect(await (await withToolsListFallback(backend, page("retry-first"), down)).json()).toMatchObject({ result: { tools: [{ name: "tool-0" }] } });
+ expect(await (await withToolsListFallback(backend, page("retry-15", "c15"), down)).json()).toMatchObject({ result: { tools: [{ name: "tool-15" }] } });
+ expect((await withToolsListFallback(backend, page("retry-16", "c16"), down)).status).toBe(503);
+ });
});
diff --git a/server/connector-tools-list.ts b/server/connector-tools-list.ts
index e3de6d2e06..bf63db1f22 100644
--- a/server/connector-tools-list.ts
+++ b/server/connector-tools-list.ts
@@ -91,8 +91,8 @@ export async function withToolsListFallback(
const pages = lastToolsLists.get(backend);
if (pages === pagesAtStart || (pagesAtStart === undefined && !pages?.has(undefined))) {
const current = pages ?? new Map<string | undefined, unknown>();
- if (!current.has(request.cursor) && current.size >= MAX_TOOL_LIST_PAGES) current.delete(current.keys().next().value!);
- current.set(request.cursor, result);
+ // Past the cap a later page goes unkept: a walk starts at the first page, so the head is what a fallback serves.
+ if (current.has(request.cursor) || current.size < MAX_TOOL_LIST_PAGES) current.set(request.cursor, result);
rememberToolsList(backend, current);
}
}
diff --git a/server/index.ts b/server/index.ts
index b2dd14e998..0ceee4a05a 100644
--- a/server/index.ts
+++ b/server/index.ts
@@ -13757,12 +13757,12 @@ const handleRequest = async (req: IncomingMessage, res: ServerResponse) => {
// A refusal is stored, an allowance is not: losing a refusal would let a ring re-form one call later.
const preflight = askBudget(from, room);
if (!preflight.allowed) {
- if (!roomPostBudgets.has(room.id) && roomPostBudgets.size >= 1_000) roomPostBudgets.delete(roomPostBudgets.keys().next().value!); roomPostBudgets.set(room.id, preflight.budget);
+ roomPostBudgets.delete(room.id); if (roomPostBudgets.size >= 1_000) roomPostBudgets.delete(roomPostBudgets.keys().next().value!); roomPostBudgets.set(room.id, preflight.budget);
return json(res, 429, { error: preflight.message });
}
const poster = from;
const decision = askBudget(poster, room);
- if (!roomPostBudgets.has(room.id) && roomPostBudgets.size >= 1_000) roomPostBudgets.delete(roomPostBudgets.keys().next().value!); roomPostBudgets.set(room.id, decision.budget);
+ roomPostBudgets.delete(room.id); if (roomPostBudgets.size >= 1_000) roomPostBudgets.delete(roomPostBudgets.keys().next().value!); roomPostBudgets.set(room.id, decision.budget);
if (!decision.allowed) return json(res, 429, { error: decision.message });
// Reading the mark keeps its window alive through a turn that only posts; an aged-out mark would hand the next hop to auto-approve.
const unattended = isUnattended(poster.id, internalCapability.threadId);
diff --git a/src/components/DataResultChip.store.test.ts b/src/components/DataResultChip.store.test.ts
new file mode 100644
index 0000000000..6314f80072
--- /dev/null
+++ b/src/components/DataResultChip.store.test.ts
@@ -0,0 +1,37 @@
+// @vitest-environment happy-dom
+import { createElement } from "react";
+import { flushSync } from "react-dom";
+import { createRoot } from "react-dom/client";
+import { describe, expect, it, vi } from "vitest";
+import { createAppStore } from "@/state/app-store";
+import { initialState, reducer, type Action, type AppState, type Bot, type Message } from "@/state/store";
+import { StoreSourceContext, type StoreSource } from "@/state/store-context";
+import { DataResultChip } from "./DataResultChip";
+
+const bot = (id: string): Bot => ({
+ id, threadId: `${id}-thread`, name: id, title: "", description: "", notifications: true, unread: false, color: "green",
+ modelSelection: { instanceId: "fake", model: "m" }, messages: [],
+} as unknown as Bot);
+
+describe("a data result's Open button follows the store", () => {
+ it("turns on once the result's bot arrives, without the row being drawn again from above", () => {
+ const store = createAppStore<AppState, Action, never>(reducer, { ...initialState, bots: [] }, (flush) => { flush(); return () => {}; });
+ const source: StoreSource = {
+ getState: store.getState, subscribe: store.subscribe, dispatch: store.dispatch,
+ flushBotPatches: vi.fn(), refreshInstances: vi.fn(), refreshModels: vi.fn(),
+ };
+ const message: Message = { id: "receipt", at: 1, role: "bot", kind: "activity", tool: { name: "data_show", ok: true },
+ dataResult: { botId: "analyst", cardId: "c_1", title: "Signups", kind: "table" } };
+ const container = document.createElement("div");
+ const root = createRoot(container);
+ flushSync(() => root.render(createElement(StoreSourceContext, { value: source }, createElement(DataResultChip, { message }))));
+ try {
+ const open = () => container.querySelector<HTMLButtonElement>("[data-data-result=c_1] button")!;
+ expect(open().disabled, "no such bot yet").toBe(true);
+ flushSync(() => store.dispatch({ type: "botAdded", bot: bot("analyst"), preserveSelection: true }));
+ expect(open().disabled, "the bot's arrival reaches the row").toBe(false);
+ } finally {
+ flushSync(() => root.unmount());
+ }
+ });
+});