Skip to content

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

Composer context panel: show no engine count between a summary and the end of the next turn

luishron123981920b53eec
diff --git a/src/components/ComposerContextMenu.test.ts b/src/components/ComposerContextMenu.test.ts
index ec549e3622..12fa08d903 100644
--- a/src/components/ComposerContextMenu.test.ts
+++ b/src/components/ComposerContextMenu.test.ts
@@ -150,6 +150,45 @@ describe("the composer's context button", () => {
expect(panel()!.querySelector("[data-context-breakdown]")).toBeNull();
});
+ it("shows no count after the chat was summarized, even when the engine still answers, until a new turn ends", async () => {
+ const before: ContextBreakdown = {
+ tokens: 6_000, window: 200_000,
+ categories: [{ id: "system_prompt", kind: "used", tokens: 1_900 }, { id: "messages", kind: "used", tokens: 4_100 }, { id: "free_space", kind: "free", tokens: 194_000 }],
+ };
+ const bot = chat({ tokens: 12, window: 200_000 }, { id: "b4" });
+ const unread = () => {
+ expect(words()).not.toContain("6k");
+ expect(panel()!.querySelector("[data-context-meter]")?.getAttribute("data-context-meter") ?? "").not.toBe("3");
+ expect(panel()!.querySelector("[data-context-breakdown]")).toBeNull();
+ };
+ breakdown = before;
+ await render({ bot });
+ await press(trigger());
+ expect(words()).toContain("6k of 200k tokens");
+
+ const summarized = [...bot.messages!, line("m3", "bot", "compaction")];
+ await render({ bot: { ...bot, messages: summarized } as Bot });
+ await act(async () => { await new Promise((resolve) => setTimeout(resolve, 0)); });
+ expect(words()).toContain("No reading yet");
+ unread();
+ await press(trigger());
+ await press(trigger());
+ expect(words()).toContain("No reading yet");
+ unread();
+
+ const asked = [...summarized, line("m4", "user"), line("m5", "bot")];
+ await render({ bot: { ...bot, busy: true, messages: asked } as Bot });
+ await act(async () => { await new Promise((resolve) => setTimeout(resolve, 0)); });
+ unread();
+
+ breakdown = { tokens: 18_000, window: 200_000, categories: [{ id: "messages", kind: "used", tokens: 18_000 }, { id: "free_space", kind: "free", tokens: 182_000 }] };
+ await render({ bot: { ...bot, messages: asked } as Bot });
+ await act(async () => { await new Promise((resolve) => setTimeout(resolve, 0)); });
+ expect(words()).toContain("9% used");
+ expect(words()).toContain("18k of 200k tokens");
+ expect([...panel()!.querySelectorAll("[data-context-breakdown] dd")].map((cell) => cell.textContent)).toEqual(["18k · 9%", "182k · 91%"]);
+ });
+
it("keeps the total alone when the engine has no count, and the last count on screen while a new one loads", async () => {
await render({ bot: chat({ tokens: 136_000, window: 200_000 }, { id: "b2" }) });
await press(trigger());
diff --git a/src/components/ComposerContextMenu.tsx b/src/components/ComposerContextMenu.tsx
index c3d8734a55..35205f8a6d 100644
--- a/src/components/ComposerContextMenu.tsx
+++ b/src/components/ComposerContextMenu.tsx
@@ -74,20 +74,31 @@ function currentContext(bot: Bot, messages: readonly Message[]) {
return compactedSinceReading(messages) ? undefined : bot.tasks?.find((task) => task.threadId === bot.threadId)?.usage?.context;
}
+/** A summary only reaches the engine's session with the next turn, so until that turn has ended the engine still counts
+ * the conversation as it was before. */
+function summaryNotCounted(messages: readonly Message[], busy: boolean): boolean {
+ if (!busy) return compactedSinceReading(messages);
+ const summary = messages.findLastIndex((message) => message.kind === "compaction");
+ const asked = messages.findLastIndex((message) => message.role === "user");
+ return compactedSinceReading(messages.slice(0, Math.max(asked, summary + 1)));
+}
+
const lastBreakdown = new Map<string, { moment: string; breakdown: ContextBreakdown }>();
/** 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, unless the conversation was summarized or started fresh since. */
+ * 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. */
function useContextBreakdown(bot: Bot | undefined, open: boolean): ContextBreakdown | null {
const id = bot?.id;
const threadId = bot?.threadId;
const busy = Boolean(bot?.busy);
const messages = bot ? visibleMessages(bot) : [];
const slot = `${id}:${threadId ?? ""}`;
- const moment = `${messages.filter((message) => message.kind === "compaction").length}:${compactedSinceReading(messages)}`;
+ const withheld = summaryNotCounted(messages, busy);
+ 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 (!open || !id || !threadId) return;
+ if (withheld) lastBreakdown.delete(slot);
+ if (!open || !id || !threadId || withheld) return;
let current = true;
void api<{ breakdown: ContextBreakdown | null }>(`/api/bots/${id}/context`, { method: "POST", body: JSON.stringify({ threadId }) })
.then((reply) => reply?.breakdown ?? null, () => null)
@@ -96,8 +107,8 @@ function useContextBreakdown(bot: Bot | undefined, open: boolean): ContextBreakd
if (current) setAnswer({ slot, moment, breakdown });
});
return () => { current = false; };
- }, [open, id, threadId, slot, moment, busy]);
- if (!open) return null;
+ }, [open, id, threadId, slot, moment, busy, withheld]);
+ if (!open || withheld) return null;
if (answer?.slot === slot && answer.moment === moment) return answer.breakdown;
const last = lastBreakdown.get(slot);
return last?.moment === moment ? last.breakdown : null;