Skip to content

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

feat(chat): suggest a new chat under the latest context compaction

AdityaUmalef2e6c73c2610
diff --git a/src/components/ChatView.tsx b/src/components/ChatView.tsx
index d84cbbfe04..dd1989a035 100644
--- a/src/components/ChatView.tsx
+++ b/src/components/ChatView.tsx
@@ -662,7 +662,7 @@ const MessagesList = memo(function MessagesList({
onRegenerate: () => void;
onReply: (message: Message) => void;
}) {
- const { botId, threadId, botName, botColor, bots, hasMore, focus, showToolCalls, locale, simple = false } = useChatRows(), busy = use(ChatBusyContext);
+ const { botId, threadId, botName, botColor, bots, hasMore, focus, showToolCalls, locale, dispatch, canReact, simple = false } = useChatRows(), busy = use(ChatBusyContext);
const messages = useLiveTranscript(threadId, saved, transcript);
// A finished turn keeps the bot's progress lines and folds only its tool steps under "Worked for", in both modes.
const items = useMemo(() => groupTranscript(messages), [messages, locale]);
@@ -749,7 +749,7 @@ const MessagesList = memo(function MessagesList({
case "digest":
return showToolCalls ? <DigestChip message={m} /> : null;
case "compaction":
- return <CompactionChip message={m} />;
+ return <CompactionChip message={m} onNewChat={m.id === lookups.newChatHintId && canReact ? () => dispatch({ type: "newTask", botId }) : undefined} />;
case "screen":
return <ScreenFrame threadId={threadId} message={m} />;
default: {
diff --git a/src/components/CompactionChip.dom.test.ts b/src/components/CompactionChip.dom.test.ts
new file mode 100644
index 0000000000..c2054af426
--- /dev/null
+++ b/src/components/CompactionChip.dom.test.ts
@@ -0,0 +1,56 @@
+// @vitest-environment happy-dom
+import { createElement } from "react";
+import { flushSync } from "react-dom";
+import { createRoot, type Root } from "react-dom/client";
+import { afterEach, beforeEach, expect, it, vi } from "vitest";
+import { setLocale } from "@/lib/i18n";
+import type { Message } from "@/state/store";
+import { CompactionChip } from "./DigestChip";
+
+let host: HTMLDivElement;
+let root: Root;
+
+beforeEach(() => {
+ setLocale("en");
+ host = document.createElement("div");
+ document.body.append(host);
+ root = createRoot(host);
+});
+
+afterEach(() => {
+ flushSync(() => root.unmount());
+ host.remove();
+});
+
+const summary: Message = {
+ id: "context", role: "bot", kind: "compaction", at: 1,
+ compaction: { summary: "The user wants X.", firstKeptId: "", foldedThroughId: "old", tokensBefore: 1, by: "harness" },
+};
+const engine: Message = { id: "engine", role: "bot", kind: "compaction", at: 1, engineCompaction: true };
+const fresh: Message = { ...summary, compaction: { ...summary.compaction!, summary: "", fresh: true } };
+
+function render(message: Message, onNewChat?: () => void) {
+ flushSync(() => root.render(createElement(CompactionChip, { message, onNewChat })));
+ return host.querySelector<HTMLElement>("[data-testid=new-chat-hint]");
+}
+
+it("offers a new chat under a summarized or engine-compacted chat, and the button starts one", () => {
+ for (const message of [summary, engine]) {
+ const onNewChat = vi.fn();
+ const hint = render(message, onNewChat);
+ expect(hint?.textContent).toContain("Start a new chat to save tokens.");
+ const button = hint?.querySelector("button");
+ expect(button?.textContent).toBe("New chat");
+ flushSync(() => button?.click());
+ expect(onNewChat).toHaveBeenCalledTimes(1);
+ }
+});
+
+it("shows no hint on a Start fresh divider or when the chat can't start a new one", () => {
+ expect(render(fresh, vi.fn())).toBeNull();
+ expect(host.querySelector("[data-testid=fresh-start-divider]")).not.toBeNull();
+ expect(render(summary)).toBeNull();
+ expect(host.querySelector("[data-testid=compaction-chip]")).not.toBeNull();
+ expect(render(engine)).toBeNull();
+ expect(host.querySelector("[data-testid=engine-compaction-chip]")).not.toBeNull();
+});
diff --git a/src/components/DigestChip.tsx b/src/components/DigestChip.tsx
index 086161746d..952580f99e 100644
--- a/src/components/DigestChip.tsx
+++ b/src/components/DigestChip.tsx
@@ -47,14 +47,8 @@ export function DigestChip({ message }: { message: Message }) {
const COMPACTION_CHIP = "max-w-[600px] rounded-xl border border-hairline/40 bg-panel px-3 py-2 text-[12px] text-ink-secondary";
-export function CompactionChip({ message }: { message: Message }) {
- if (!message.compaction) return message.engineCompaction ? (
- <div className={COMPACTION_CHIP} data-testid="engine-compaction-chip">
- <p className="font-medium">{t("chat.engineCompactionTitle")}</p>
- <p className="mt-1">{t("chat.engineCompactionHint")}</p>
- </div>
- ) : null;
- if (message.compaction.fresh) {
+export function CompactionChip({ message, onNewChat }: { message: Message; onNewChat?: () => void }) {
+ if (message.compaction?.fresh) {
return (
<div role="separator" data-testid="fresh-start-divider" className="flex w-full items-center gap-3 py-1 text-[12px] text-ink-secondary">
<span aria-hidden="true" className="h-px flex-1 bg-hairline" />
@@ -63,12 +57,27 @@ export function CompactionChip({ message }: { message: Message }) {
</div>
);
}
- const excerpts = message.compaction.summary.startsWith(EXCERPT_SUMMARY_HEADER);
- return (
+ const excerpts = message.compaction?.summary.startsWith(EXCERPT_SUMMARY_HEADER);
+ const chip = message.compaction ? (
<details className={COMPACTION_CHIP} data-testid="compaction-chip">
<summary className="cursor-pointer font-medium">{t(excerpts ? "chat.compactionExcerptTitle" : "chat.compactionTitle")}</summary>
<p className="mt-2">{t(excerpts ? "chat.compactionExcerptHint" : "chat.compactionHint")}</p>
<p className="mt-2 whitespace-pre-wrap break-words">{message.compaction.summary}</p>
</details>
+ ) : message.engineCompaction ? (
+ <div className={COMPACTION_CHIP} data-testid="engine-compaction-chip">
+ <p className="font-medium">{t("chat.engineCompactionTitle")}</p>
+ <p className="mt-1">{t("chat.engineCompactionHint")}</p>
+ </div>
+ ) : null;
+ if (!chip || !onNewChat) return chip;
+ return (
+ <div>
+ {chip}
+ <p className="mt-1 flex flex-wrap items-center gap-x-2 px-3 text-[12px] text-ink-secondary" data-testid="new-chat-hint">
+ {t("chat.newChatHint")}
+ <button type="button" onClick={onNewChat} className="font-medium text-accent hover:underline">{t("chat.newChatHintButton")}</button>
+ </p>
+ </div>
);
}
diff --git a/src/lib/transcript-derivations.test.ts b/src/lib/transcript-derivations.test.ts
index c2d30fa48a..a1b1280b3e 100644
--- a/src/lib/transcript-derivations.test.ts
+++ b/src/lib/transcript-derivations.test.ts
@@ -148,3 +148,16 @@ describe.each([
}
});
});
+
+describe("newChatHintId", () => {
+ const summary = msg("sum", { kind: "compaction", compaction: { summary: "s", firstKeptId: "", foldedThroughId: "a", tokensBefore: 1, by: "harness" } });
+ const engine = msg("eng", { kind: "compaction", engineCompaction: true });
+ const fresh = msg("fresh", { kind: "compaction", compaction: { summary: "", firstKeptId: "", foldedThroughId: "a", tokensBefore: 0, by: "person", fresh: true } });
+ const reply = msg("r", { text: "hi" });
+ it("marks only the newest compaction, and none once the chat started fresh", () => {
+ expect(transcriptLookups([reply], [reply]).newChatHintId).toBeUndefined();
+ expect(transcriptLookups([summary, reply, engine, reply], [summary, reply, engine, reply]).newChatHintId).toBe("eng");
+ expect(transcriptLookups([engine, summary, reply], [engine, summary, reply]).newChatHintId).toBe("sum");
+ expect(transcriptLookups([summary, fresh, reply], [summary, fresh, reply]).newChatHintId).toBeUndefined();
+ });
+});
diff --git a/src/lib/transcript-derivations.ts b/src/lib/transcript-derivations.ts
index 7f5d96a3d4..6faa3b9674 100644
--- a/src/lib/transcript-derivations.ts
+++ b/src/lib/transcript-derivations.ts
@@ -8,6 +8,7 @@ export interface TranscriptLookups {
editVersions(message: Message): readonly Message[] | undefined;
replyTarget(message: Message): Message | undefined;
retryableId: string | undefined;
+ newChatHintId: string | undefined;
}
export function transcriptLookups(all: readonly Message[], branch: readonly Message[]): TranscriptLookups {
@@ -23,6 +24,7 @@ export function transcriptLookups(all: readonly Message[], branch: readonly Mess
}
for (const siblings of forks.values()) siblings.sort((a, b) => a.at - b.at);
const retryableId = lastNonReceipt(branch)?.id;
+ const lastCompaction = branch.findLast((message) => message.kind === "compaction");
return {
editVersions(message) {
if (message.role !== "user" || message.kind !== "text") return undefined;
@@ -31,6 +33,7 @@ export function transcriptLookups(all: readonly Message[], branch: readonly Mess
},
replyTarget: (message) => (message.replyToId ? byId.get(message.replyToId) : undefined),
retryableId,
+ newChatHintId: lastCompaction && !lastCompaction.compaction?.fresh ? lastCompaction.id : undefined,
};
}
diff --git a/src/locales/de.json b/src/locales/de.json
index 474ad685b1..4428d1d797 100644
--- a/src/locales/de.json
+++ b/src/locales/de.json
@@ -4010,5 +4010,7 @@
"server.rooms.linkedEnded": "{place} hat sich 30 Tage lang nicht verbunden, deshalb wurde die Verbindung dorthin beendet. Nicht mehr in diesem Raum: {bots}.",
"room.members.far.place": "auf {place}",
"room.members.far.off": "Verbindung ist aus",
- "room.members.far.missing": "Nicht mehr verfügbar"
+ "room.members.far.missing": "Nicht mehr verfügbar",
+ "chat.newChatHint": "Starte einen neuen Chat, um Tokens zu sparen.",
+ "chat.newChatHintButton": "Neuer Chat"
}
diff --git a/src/locales/en.json b/src/locales/en.json
index df830f83c3..0ef6b7dce2 100644
--- a/src/locales/en.json
+++ b/src/locales/en.json
@@ -2660,6 +2660,8 @@
"chat.compactionTitle": "Earlier context summarized",
"chat.engineCompactionTitle": "Chat condensed into a summary",
"chat.engineCompactionHint": "The AI summarized earlier messages to make room. Your full conversation is still here, but the agent may not recall older details exactly.",
+ "chat.newChatHint": "Start a new chat to save tokens.",
+ "chat.newChatHintButton": "New chat",
"chat.compactionHint": "Your full conversation is still here. The agent uses this summary and recent messages when continuing.",
"chat.startFresh": "Start fresh",
"chat.startFresh.hint": "Starts a new session for this chat. Every message stays visible, and the bot's instructions, skills and memory don't change.",
diff --git a/src/locales/pt-br.json b/src/locales/pt-br.json
index fce9262a73..21e3de357c 100644
--- a/src/locales/pt-br.json
+++ b/src/locales/pt-br.json
@@ -2478,6 +2478,8 @@
"chat.compactionTitle": "Contexto anterior resumido",
"chat.engineCompactionTitle": "Conversa condensada em um resumo",
"chat.engineCompactionHint": "A IA resumiu as mensagens anteriores para abrir espaço. Sua conversa completa continua aqui, mas o agente pode não lembrar exatamente de detalhes mais antigos.",
+ "chat.newChatHint": "Comece uma nova conversa para economizar tokens.",
+ "chat.newChatHintButton": "Nova conversa",
"chat.compactionHint": "Sua conversa completa continua aqui. O agente usa este resumo e as mensagens recentes ao continuar.",
"chat.startFresh": "Começar do zero",
"chat.startFresh.hint": "Inicia uma nova sessão para esta conversa. Todas as mensagens continuam visíveis, e as instruções, habilidades e memória do bot não mudam.",
diff --git a/src/locales/source-hashes.json b/src/locales/source-hashes.json
index 687d8caa12..282096186f 100644
--- a/src/locales/source-hashes.json
+++ b/src/locales/source-hashes.json
@@ -4013,7 +4013,9 @@
"room.routedBy.gemini": "488da2a6e7c26e4a09b8ed8cdd65ff8237685317f18f9dacede1994658db498c",
"devices.computers.title": "700633b6d9e8d3cebf611ff7642fd356d5febbf3d625fa63691063bc87bdf5f5",
"cloudBuy.promise.teamPlan": "cc700a83e92de4a0be260f4cea4f9fa96a4117c58e6d4e0592f6682b09152f2e",
- "webPanels.bar.label": "ffe911248c62dd97ede1343e7fd7f23291c1ee2b8f696d7d46ef05da5c0491b2"
+ "webPanels.bar.label": "ffe911248c62dd97ede1343e7fd7f23291c1ee2b8f696d7d46ef05da5c0491b2",
+ "chat.newChatHint": "f50b2919941dfdd3337f86015f98c770cb22ed01d7060235415abe8c39a06ee0",
+ "chat.newChatHintButton": "db18382a249e0206bc28514931d423b77d55dce180c1eb8e4bf35f861c9a50ec"
},
"es": {
"whop.description": "1a07cf04ef17029448d3781415cbac16d54e66b821907a40d94e0e6349710e70",
@@ -36800,7 +36802,9 @@
"templateImport.notATemplate": "750c5a704ea91da9cbdd9206f63c187546979bd60ddde1d98beb188b1753cbcd",
"sidebar.addDepartment": "8854f21e30a4d47fd0c717c63e7361f5fc6dda6f483f33d24f4ee4bae7996844",
"sidebar.import": "ea94509e38cf6d6427db3a1b5a3b5816cba1e8f89b08f9739f6077ade188163c",
- "team.shareDepartment": "29887a5ff9846ccc23327565a07e17fa6dcaea6211f1898ae825f84ae38f4223"
+ "team.shareDepartment": "29887a5ff9846ccc23327565a07e17fa6dcaea6211f1898ae825f84ae38f4223",
+ "chat.newChatHint": "f50b2919941dfdd3337f86015f98c770cb22ed01d7060235415abe8c39a06ee0",
+ "chat.newChatHintButton": "db18382a249e0206bc28514931d423b77d55dce180c1eb8e4bf35f861c9a50ec"
},
"zh": {
"pro.fromPrice": "a48a04a457d60b08b54fc83e7a6c4584ffedb1e23621859084900c8521a3af7b",