Skip to content

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

Composer context panel: drop the count when another branch of the conversation is picked

luishron123078f67519224
diff --git a/src/components/ComposerContextMenu.test.ts b/src/components/ComposerContextMenu.test.ts
index 8595dce54f..457319c87e 100644
--- a/src/components/ComposerContextMenu.test.ts
+++ b/src/components/ComposerContextMenu.test.ts
@@ -219,6 +219,27 @@ describe("the composer's context button", () => {
expect([...panel()!.querySelectorAll("[data-context-breakdown] dd")].map((cell) => cell.textContent)).toEqual(["18k · 9%", "182k · 91%"]);
});
+ it("drops the count and asks again when another branch of the conversation is picked", async () => {
+ advancedMode();
+ const bot = chat({ tokens: 12, window: 200_000 }, {
+ id: "b5", activeLeafId: "m2",
+ messages: [line("m1", "user"), line("m2", "bot", "text", { parentId: "m1" }), line("m2b", "bot", "text", { parentId: "m1" })],
+ });
+ const asked = () => requests().filter(([path]) => path.endsWith("/context")).length;
+ breakdown = { tokens: 6_000, window: 200_000, categories: [{ id: "messages", kind: "used", tokens: 6_000 }, { id: "free_space", kind: "free", tokens: 194_000 }] };
+ await render({ bot });
+ await press(trigger());
+ expect(words()).toContain("6k of 200k tokens");
+ expect(asked()).toBe(1);
+
+ breakdown = null;
+ await render({ bot: { ...bot, activeLeafId: "m2b" } as Bot });
+ await act(async () => { await new Promise((resolve) => setTimeout(resolve, 0)); });
+ expect(asked()).toBe(2);
+ expect(words()).not.toContain("6k");
+ expect(panel()!.querySelector("[data-context-breakdown]")).toBeNull();
+ });
+
it("keeps the total alone when the engine has no count, and the last count on screen while a new one loads", async () => {
advancedMode();
await render({ bot: chat({ tokens: 136_000, window: 200_000 }, { id: "b2" }) });
diff --git a/src/components/ComposerContextMenu.tsx b/src/components/ComposerContextMenu.tsx
index 4a69c11127..12fe922dfa 100644
--- a/src/components/ComposerContextMenu.tsx
+++ b/src/components/ComposerContextMenu.tsx
@@ -90,7 +90,8 @@ const newestRequest = new Map<string, number>();
let requests = 0;
/** The engine's own count, asked for while the panel is open in Advanced mode 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. */
+ * 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,
+ * and a count taken on one branch of the conversation is dropped when another is picked. */
function useContextBreakdown(bot: Bot | undefined, open: boolean): ContextBreakdown | null {
const id = bot?.id;
const threadId = bot?.threadId;
@@ -98,8 +99,20 @@ function useContextBreakdown(bot: Bot | undefined, open: boolean): ContextBreakd
const messages = bot ? visibleMessages(bot) : [];
const slot = `${id}:${threadId ?? ""}`;
const withheld = summaryNotCounted(messages, busy);
- const moment = `${messages.filter((message) => message.kind === "compaction").length}:${withheld}`;
+ const leaf = messages.at(-1)?.id;
+ const [path, setPath] = useState({ slot, leaf, switches: 0 });
+ if (path.slot !== slot || path.leaf !== leaf) {
+ // A message that is still in the conversation but no longer shown means another branch was picked.
+ const switched = path.slot === slot && !messages.some((message) => message.id === path.leaf) && Boolean(bot?.messages.some((message) => message.id === path.leaf));
+ setPath({ slot, leaf, switches: path.slot === slot ? path.switches + Number(switched) : 0 });
+ }
+ const moment = `${messages.filter((message) => message.kind === "compaction").length}:${withheld}:${path.switches}`;
const [answer, setAnswer] = useState<{ slot: string; moment: string; breakdown: ContextBreakdown | null } | null>(null);
+ useEffect(() => {
+ if (!path.switches) return;
+ lastBreakdown.delete(slot);
+ newestRequest.delete(slot);
+ }, [slot, path.switches]);
useEffect(() => {
if (withheld) {
lastBreakdown.delete(slot);