Skip to content

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

Composer context panel: an older request that answers last no longer replaces the newest count

luishron123ea22b9c44b83
diff --git a/src/components/ComposerContextMenu.test.ts b/src/components/ComposerContextMenu.test.ts
index e59ff7e458..d841a9d045 100644
--- a/src/components/ComposerContextMenu.test.ts
+++ b/src/components/ComposerContextMenu.test.ts
@@ -212,6 +212,29 @@ describe("the composer's context button", () => {
expect(store.dispatch).not.toHaveBeenCalledWith(expect.objectContaining({ type: "error" }));
});
+ it("keeps the newest count when an older request answers last, also for a panel mounted afterwards", async () => {
+ const bot = chat({ tokens: 12, window: 200_000 }, { id: "b5" });
+ const waiting: Array<(response: Response) => void> = [];
+ fetched.mockImplementation(() => new Promise<Response>((resolve) => waiting.push(resolve)));
+ const answer = (tokens: number) => new Response(JSON.stringify({ threadId: "t1", breakdown: { tokens, window: 200_000, categories: [{ id: "messages", kind: "used", tokens }] } }), { status: 200, headers: { "content-type": "application/json" } });
+ await render({ bot });
+ await press(trigger());
+ await render({ bot: { ...bot, busy: true } as Bot });
+ expect(waiting).toHaveLength(2);
+ await act(async () => { waiting[1](answer(18_000)); });
+ expect(words()).toContain("18k of 200k tokens");
+ await act(async () => { waiting[0](answer(6_000)); });
+ expect(words()).toContain("18k of 200k tokens");
+
+ act(() => root.unmount());
+ root = createRoot(host);
+ await render({ bot: { ...bot, busy: true } as Bot });
+ await press(trigger());
+ expect(waiting).toHaveLength(3);
+ expect(words()).toContain("9% used");
+ expect(words()).toContain("18k of 200k tokens");
+ });
+
it("shows each measured bot of a room with its own compact, and no start fresh", async () => {
const members = [{ id: "s", name: "Scout" }, { id: "p", name: "Pixel" }, { id: "q", name: "Quill" }] as Bot[];
const group = { id: "g1", memberContext: { s: { tokens: 170_000, window: 200_000 }, q: { tokens: 60_000, window: 200_000, compactRequested: true } } } as unknown as Group;
diff --git a/src/components/ComposerContextMenu.tsx b/src/components/ComposerContextMenu.tsx
index 35205f8a6d..a6af3be9ec 100644
--- a/src/components/ComposerContextMenu.tsx
+++ b/src/components/ComposerContextMenu.tsx
@@ -84,6 +84,8 @@ function summaryNotCounted(messages: readonly Message[], busy: boolean): boolean
}
const lastBreakdown = new Map<string, { moment: string; breakdown: ContextBreakdown }>();
+const newestRequest = new Map<string, number>();
+let requests = 0;
/** The engine's own count, asked for while the panel is open and again whenever a turn starts or ends. The last answer
* shows until the new one lands; none is asked for or shown between a summary or fresh start and the end of the next turn. */
@@ -97,12 +99,18 @@ function useContextBreakdown(bot: Bot | undefined, open: boolean): ContextBreakd
const moment = `${messages.filter((message) => message.kind === "compaction").length}:${withheld}`;
const [answer, setAnswer] = useState<{ slot: string; moment: string; breakdown: ContextBreakdown | null } | null>(null);
useEffect(() => {
- if (withheld) lastBreakdown.delete(slot);
+ if (withheld) {
+ lastBreakdown.delete(slot);
+ newestRequest.delete(slot);
+ }
if (!open || !id || !threadId || withheld) return;
let current = true;
+ const request = ++requests;
+ newestRequest.set(slot, request);
void api<{ breakdown: ContextBreakdown | null }>(`/api/bots/${id}/context`, { method: "POST", body: JSON.stringify({ threadId }) })
.then((reply) => reply?.breakdown ?? null, () => null)
.then((breakdown) => {
+ if (newestRequest.get(slot) !== request) return;
if (breakdown) lastBreakdown.set(slot, { moment, breakdown }); else lastBreakdown.delete(slot);
if (current) setAnswer({ slot, moment, breakdown });
});