Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

feat: reuse /compact for the chat menu button (#13579)

Long Yixing85b6414c68b6
diff --git a/studio/frontend/scripts/check-bundle-budget.ts b/studio/frontend/scripts/check-bundle-budget.ts
index 697119d51ff..842783f3a7e 100644
--- a/studio/frontend/scripts/check-bundle-budget.ts
+++ b/studio/frontend/scripts/check-bundle-budget.ts
@@ -92,8 +92,10 @@ export const BUDGET = {
// +806 B raw / +311 B transfer: its training field and strings); the maximized window layout fix in the
// app provider (#13647) 539 B raw, the + menu reorder (#13679, measured together with #13659) 711 B raw,
// and #13703 / #13700 the last 61 B. Raised by that growth so main's margin stays where it was.
- transferBytes: 2_027_313,
- rawBytes: 6_745_353,
+ // The chat menu's Compact conversation item adds 1.6 KB raw / 0.3 KB transfer to the eager chat chunk
+ // (6,741,807 -> 6,743,381 B raw, 2,021,316 -> 2,021,655 B transfer against 2a03c22812), raised by that.
+ transferBytes: 2_027_652,
+ rawBytes: 6_746_927,
};
// The chunk count is reported but not budgeted. Splitting a page out of the entry raises it while lowering the
diff --git a/studio/frontend/src/features/chat/components/chat-header-menu.tsx b/studio/frontend/src/features/chat/components/chat-header-menu.tsx
index b8b3a09196e..3f3f8020ba6 100644
--- a/studio/frontend/src/features/chat/components/chat-header-menu.tsx
+++ b/studio/frontend/src/features/chat/components/chat-header-menu.tsx
@@ -19,6 +19,7 @@ import {
TooltipTrigger,
} from "@/components/ui/tooltip";
import { useChatFavoritesStore } from "@/features/library/chats/favorites-store";
+import { resumesThought } from "@/features/model-picker";
import { useShortcutLabel } from "@/features/settings";
import type { ShortcutId } from "@/features/settings";
import { useT } from "@/i18n";
@@ -47,12 +48,24 @@ import {
} from "@hugeicons/core-free-icons";
import { HugeiconsIcon, type IconSvgElement } from "@hugeicons/react";
import { ForkIcon } from "@/lib/fork-icon";
-import type { ReactNode } from "react";
+import { type ReactNode, useSyncExternalStore } from "react";
import { useChatRuntimeStore } from "../stores/chat-runtime-store";
import {
type ActiveChatMenu,
useActiveChatMenuStore,
} from "../stores/active-chat-menu-store";
+import { requestThreadCompaction } from "../stores/compact-request-store";
+import {
+ liveThreadBranch,
+ liveThreadRunning,
+ subscribeLiveThreadViews,
+} from "../utils/live-thread-head";
+import {
+ hasPreStreamRunReservation,
+ subscribePreStreamRunReservations,
+} from "../utils/pre-stream-run-reservation";
+import { deriveContextUsageBar } from "../lib/context-usage-bar-state";
+import { UsageRing } from "./context-usage-bar";
import {
type ConversationExportFormat,
chatExportOptions,
@@ -152,6 +165,55 @@ function TemporaryChatButton({
function ChatMenuItems({ menu }: { menu: ActiveChatMenu }) {
const t = useT();
+ const preStreamRunActive = useSyncExternalStore(
+ subscribePreStreamRunReservations,
+ () => hasPreStreamRunReservation([menu.item.id]),
+ () => false,
+ );
+ const liveRunActive = useSyncExternalStore(
+ subscribeLiveThreadViews,
+ () => liveThreadRunning(menu.item.id),
+ () => false,
+ );
+ const busy = useChatRuntimeStore((state) =>
+ state.modelLoading || Boolean(state.runningByThreadId[menu.item.id]),
+ ) || preStreamRunActive || liveRunActive;
+ const usedPercent = useChatRuntimeStore((state) => {
+ const usage =
+ state.activeThreadId === menu.item.id
+ ? state.contextUsage
+ : state.contextUsageByThreadId[menu.item.id];
+ return deriveContextUsageBar({
+ used: usage?.contextTokens ?? usage?.totalTokens ?? null,
+ total: state.loadedContextLength,
+ })?.percent ?? null;
+ });
+ const compact = () => {
+ const runtime = useChatRuntimeStore.getState();
+ if (
+ menu.item.type !== "single" ||
+ runtime.modelLoading ||
+ runtime.runningByThreadId[menu.item.id] ||
+ hasPreStreamRunReservation([menu.item.id]) ||
+ liveThreadRunning(menu.item.id)
+ ) return;
+ if (!liveThreadBranch(menu.item.id)?.length) {
+ toast.info(t("chatMenu.compactEmpty"));
+ return;
+ }
+ if (!runtime.supportsTools || !resumesThought({
+ loadedIsGguf: runtime.loadedIsGguf,
+ loadedIsMlx: runtime.loadedIsMlx,
+ activeGgufVariant: runtime.activeGgufVariant,
+ activeNativePathToken: runtime.activeNativePathToken,
+ checkpoint: runtime.params.checkpoint,
+ })) {
+ toast.info(t("chatMenu.compactUnsupported"));
+ return;
+ }
+ requestThreadCompaction(menu.item.id);
+ toast.info(t("chatMenu.compactQueued"));
+ };
const favorite = useChatFavoritesStore((state) =>
state.chatIds.includes(menu.item.id),
);
@@ -223,6 +285,17 @@ function ChatMenuItems({ menu }: { menu: ActiveChatMenu }) {
)}
</Item>
<DropdownMenuSeparator className="mx-3" />
+ <Item
+ glyph={
+ <span className="flex [--track:color-mix(in_srgb,currentColor_30%,transparent)]">
+ <UsageRing percent={usedPercent} stroke="stroke-current" />
+ </span>
+ }
+ onSelect={compact}
+ disabled={menu.item.type !== "single" || busy}
+ >
+ {t("chatMenu.compact")}
+ </Item>
<Item
glyph={<HugeiconsIcon icon={ForkIcon} strokeWidth={1.75} className={ICON} />}
onSelect={menu.fork}
diff --git a/studio/frontend/src/features/chat/components/context-usage-bar.tsx b/studio/frontend/src/features/chat/components/context-usage-bar.tsx
index 2a8f292f285..7ff28a72224 100644
--- a/studio/frontend/src/features/chat/components/context-usage-bar.tsx
+++ b/studio/frontend/src/features/chat/components/context-usage-bar.tsx
@@ -35,7 +35,7 @@ function getSeverityColor(percent: number): {
const RING_RADIUS = 6.25;
const RING_LENGTH = 2 * Math.PI * RING_RADIUS;
-const UsageRing: FC<{ percent: number | null; stroke: string }> = ({
+export const UsageRing: FC<{ percent: number | null; stroke: string }> = ({
percent,
stroke,
}) => (
diff --git a/studio/frontend/src/features/chat/utils/live-thread-head.ts b/studio/frontend/src/features/chat/utils/live-thread-head.ts
index d81a79a5a98..660bf347e6e 100644
--- a/studio/frontend/src/features/chat/utils/live-thread-head.ts
+++ b/studio/frontend/src/features/chat/utils/live-thread-head.ts
@@ -2,36 +2,59 @@
// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
type LiveThreadView = {
+ subscribe?: (listener: () => void) => () => void;
threads: () => { getState: () => { mainThreadId?: string } };
threadListItem: () => {
getState: () => { id?: string; remoteId?: string | null };
};
thread: () => {
- getState: () => { messages: ReadonlyArray<{ id: string }> };
+ getState: () => { messages: ReadonlyArray<{ id: string }>; isRunning?: boolean };
};
};
const views = new Set<LiveThreadView>();
+const listeners = new Set<() => void>();
+
+function notifyViewsChanged(): void {
+ for (const listener of [...listeners]) listener();
+}
+
+export function subscribeLiveThreadViews(listener: () => void): () => void {
+ listeners.add(listener);
+ return () => { listeners.delete(listener); };
+}
export function registerLiveThreadView(view: LiveThreadView): () => void {
views.add(view);
+ const unsubscribe = view.subscribe?.(notifyViewsChanged);
+ notifyViewsChanged();
return () => {
+ unsubscribe?.();
views.delete(view);
+ notifyViewsChanged();
};
}
// The branch picker moves the head only in memory, so storage alone cannot say which branch is on screen.
-export function liveThreadBranch(threadId: string): string[] | null {
+function liveThreadState(threadId: string) {
for (const view of views) {
try {
const item = view.threadListItem().getState();
if (item.remoteId !== threadId) continue;
// A chat opened earlier keeps its runtime alive, but its thread() reads the chat on screen.
if (item.id !== view.threads().getState().mainThreadId) continue;
- return view.thread().getState().messages.map((message) => message.id);
+ return view.thread().getState();
} catch {
// A view torn down mid-switch has no thread to read.
}
}
return null;
}
+
+export function liveThreadBranch(threadId: string): string[] | null {
+ return liveThreadState(threadId)?.messages.map((message) => message.id) ?? null;
+}
+
+export function liveThreadRunning(threadId: string): boolean {
+ return Boolean(liveThreadState(threadId)?.isRunning);
+}
diff --git a/studio/frontend/src/i18n/locales/ar.ts b/studio/frontend/src/i18n/locales/ar.ts
index 1cd52b243c5..064a27353e8 100644
--- a/studio/frontend/src/i18n/locales/ar.ts
+++ b/studio/frontend/src/i18n/locales/ar.ts
@@ -586,6 +586,10 @@ export const ar = {
chatMenu: {
more: "خيارات المحادثة",
copy: "نسخ",
+ compact: "ضغط المحادثة",
+ compactQueued: "تم طلب ضغط المحادثة للرسالة التالية",
+ compactEmpty: "لا يوجد ما يمكن ضغطه بعد",
+ compactUnsupported: "ضغط المحادثة يتطلب نموذج GGUF أو MLX يدعم الأدوات",
},
welcome: {
goodToSeeYou: "سعيد برؤيتك",
diff --git a/studio/frontend/src/i18n/locales/de.ts b/studio/frontend/src/i18n/locales/de.ts
index eede95463b6..c658cdfd1cb 100644
--- a/studio/frontend/src/i18n/locales/de.ts
+++ b/studio/frontend/src/i18n/locales/de.ts
@@ -586,6 +586,10 @@ export const de = {
chatMenu: {
more: "Chat-Optionen",
copy: "Kopieren",
+ compact: "Unterhaltung komprimieren",
+ compactQueued: "Komprimierung für die nächste Nachricht angefordert",
+ compactEmpty: "Noch nichts zu komprimieren",
+ compactUnsupported: "Die Komprimierung erfordert ein GGUF- oder MLX-Modell mit Tool-Unterstützung",
},
welcome: {
goodToSeeYou: "Schön, dich zu sehen",
diff --git a/studio/frontend/src/i18n/locales/en.ts b/studio/frontend/src/i18n/locales/en.ts
index 8a7246b18ca..d99e643d574 100644
--- a/studio/frontend/src/i18n/locales/en.ts
+++ b/studio/frontend/src/i18n/locales/en.ts
@@ -638,6 +638,10 @@ export const en = {
chatMenu: {
more: "Chat options",
copy: "Copy",
+ compact: "Compact conversation",
+ compactQueued: "Compaction requested for the next message",
+ compactEmpty: "Nothing to compact yet",
+ compactUnsupported: "Compaction requires a GGUF or MLX model with tool support",
},
// The empty chat's greeting; {name} is the user's nickname or first name.
welcome: {
diff --git a/studio/frontend/src/i18n/locales/es.ts b/studio/frontend/src/i18n/locales/es.ts
index 366072a787a..c0937b4ad6c 100644
--- a/studio/frontend/src/i18n/locales/es.ts
+++ b/studio/frontend/src/i18n/locales/es.ts
@@ -586,6 +586,10 @@ export const es = {
chatMenu: {
more: "Opciones del chat",
copy: "Copiar",
+ compact: "Compactar conversación",
+ compactQueued: "Se solicitó la compactación para el siguiente mensaje",
+ compactEmpty: "Aún no hay nada que compactar",
+ compactUnsupported: "La compactación requiere un modelo GGUF o MLX compatible con herramientas",
},
welcome: {
goodToSeeYou: "Me alegra verte",
diff --git a/studio/frontend/src/i18n/locales/fr.ts b/studio/frontend/src/i18n/locales/fr.ts
index 9f38e9f25cd..e3a9faa2886 100644
--- a/studio/frontend/src/i18n/locales/fr.ts
+++ b/studio/frontend/src/i18n/locales/fr.ts
@@ -586,6 +586,10 @@ export const fr = {
chatMenu: {
more: "Options de la discussion",
copy: "Copier",
+ compact: "Compacter la conversation",
+ compactQueued: "Compactage demandé pour le prochain message",
+ compactEmpty: "Rien à compacter pour le moment",
+ compactUnsupported: "Le compactage nécessite un modèle GGUF ou MLX prenant en charge les outils",
},
welcome: {
goodToSeeYou: "Ravi de vous voir",
diff --git a/studio/frontend/src/i18n/locales/he.ts b/studio/frontend/src/i18n/locales/he.ts
index b8971fb0227..a53a5585e00 100644
--- a/studio/frontend/src/i18n/locales/he.ts
+++ b/studio/frontend/src/i18n/locales/he.ts
@@ -671,6 +671,10 @@ export const he = {
chatMenu: {
more: "אפשרויות צ'אט",
copy: "העתקה",
+ compact: "כווץ שיחה",
+ compactQueued: "התבקש כיווץ שיחה עבור ההודעה הבאה",
+ compactEmpty: "אין עדיין מה לכווץ",
+ compactUnsupported: "כיווץ שיחה דורש מודל GGUF או MLX עם תמיכה בכלים",
},
welcome: {
goodToSeeYou: "טוב לראות אותך",
diff --git a/studio/frontend/src/i18n/locales/hi.ts b/studio/frontend/src/i18n/locales/hi.ts
index 5a15b638901..d31037bc003 100644
--- a/studio/frontend/src/i18n/locales/hi.ts
+++ b/studio/frontend/src/i18n/locales/hi.ts
@@ -586,6 +586,10 @@ export const hi = {
chatMenu: {
more: "चैट विकल्प",
copy: "कॉपी करें",
+ compact: "बातचीत कॉम्पैक्ट करें",
+ compactQueued: "अगले संदेश के लिए कॉम्पैक्शन का अनुरोध किया गया",
+ compactEmpty: "अभी कॉम्पैक्ट करने के लिए कुछ नहीं है",
+ compactUnsupported: "कॉम्पैक्शन के लिए टूल सपोर्ट वाला GGUF या MLX मॉडल आवश्यक है",
},
welcome: {
goodToSeeYou: "आपको देखकर अच्छा लगा",
diff --git a/studio/frontend/src/i18n/locales/it.ts b/studio/frontend/src/i18n/locales/it.ts
index 6430e74bf4b..e844544714c 100644
--- a/studio/frontend/src/i18n/locales/it.ts
+++ b/studio/frontend/src/i18n/locales/it.ts
@@ -3164,6 +3164,10 @@ export const it = {
chatMenu: {
more: "Opzioni chat",
copy: "Copia",
+ compact: "Compatta conversazione",
+ compactQueued: "Compattazione richiesta per il prossimo messaggio",
+ compactEmpty: "Ancora niente da compattare",
+ compactUnsupported: "La compattazione richiede un modello GGUF o MLX con supporto per gli strumenti",
},
welcome: {
goodToSeeYou: "Che piacere vederti",
diff --git a/studio/frontend/src/i18n/locales/ja.ts b/studio/frontend/src/i18n/locales/ja.ts
index fcd46623306..9f28f106b4c 100644
--- a/studio/frontend/src/i18n/locales/ja.ts
+++ b/studio/frontend/src/i18n/locales/ja.ts
@@ -587,6 +587,10 @@ export const ja = {
chatMenu: {
more: "チャットのオプション",
copy: "コピー",
+ compact: "会話を圧縮",
+ compactQueued: "次のメッセージで会話を圧縮するようリクエストしました",
+ compactEmpty: "まだ圧縮する内容がありません",
+ compactUnsupported: "会話の圧縮にはツール対応のGGUFまたはMLXモデルが必要です",
},
welcome: {
goodToSeeYou: "お会いできてうれしいです",
diff --git a/studio/frontend/src/i18n/locales/ko.ts b/studio/frontend/src/i18n/locales/ko.ts
index c58c7ccb097..feaba8629bf 100644
--- a/studio/frontend/src/i18n/locales/ko.ts
+++ b/studio/frontend/src/i18n/locales/ko.ts
@@ -586,6 +586,10 @@ export const ko = {
chatMenu: {
more: "채팅 옵션",
copy: "복사",
+ compact: "대화 압축",
+ compactQueued: "다음 메시지에 대한 대화 압축을 요청했습니다",
+ compactEmpty: "아직 압축할 내용이 없습니다",
+ compactUnsupported: "대화 압축에는 도구를 지원하는 GGUF 또는 MLX 모델이 필요합니다",
},
welcome: {
goodToSeeYou: "반가워요",
diff --git a/studio/frontend/src/i18n/locales/pt-br.ts b/studio/frontend/src/i18n/locales/pt-br.ts
index 4e6b86b10db..5857cf9f021 100644
--- a/studio/frontend/src/i18n/locales/pt-br.ts
+++ b/studio/frontend/src/i18n/locales/pt-br.ts
@@ -586,6 +586,10 @@ export const ptBR = {
chatMenu: {
more: "Opções do chat",
copy: "Copiar",
+ compact: "Compactar conversa",
+ compactQueued: "A compactação foi solicitada para a próxima mensagem",
+ compactEmpty: "Ainda não há nada para compactar",
+ compactUnsupported: "A compactação requer um modelo GGUF ou MLX com suporte a ferramentas",
},
welcome: {
goodToSeeYou: "Que bom ver você",
diff --git a/studio/frontend/src/i18n/locales/ru.ts b/studio/frontend/src/i18n/locales/ru.ts
index 5bc0081f74a..d5b061da224 100644
--- a/studio/frontend/src/i18n/locales/ru.ts
+++ b/studio/frontend/src/i18n/locales/ru.ts
@@ -586,6 +586,10 @@ export const ru = {
chatMenu: {
more: "Параметры чата",
copy: "Копировать",
+ compact: "Сжать беседу",
+ compactQueued: "Запрошено сжатие для следующего сообщения",
+ compactEmpty: "Пока нечего сжимать",
+ compactUnsupported: "Для сжатия нужна модель GGUF или MLX с поддержкой инструментов",
},
welcome: {
goodToSeeYou: "Рады вас видеть",
diff --git a/studio/frontend/src/i18n/locales/sv.ts b/studio/frontend/src/i18n/locales/sv.ts
index 655dcb5104c..cba80063396 100644
--- a/studio/frontend/src/i18n/locales/sv.ts
+++ b/studio/frontend/src/i18n/locales/sv.ts
@@ -687,6 +687,10 @@ export const sv = {
chatMenu: {
more: "Chattalternativ",
copy: "Kopiera",
+ compact: "Komprimera konversationen",
+ compactQueued: "Komprimering begärd för nästa meddelande",
+ compactEmpty: "Inget att komprimera än",
+ compactUnsupported: "Komprimering kräver en GGUF- eller MLX-modell med verktygsstöd",
},
welcome: {
goodToSeeYou: "Kul att se dig",
diff --git a/studio/frontend/src/i18n/locales/zh-CN.ts b/studio/frontend/src/i18n/locales/zh-CN.ts
index 0fbcdf4a061..ea0448abc72 100644
--- a/studio/frontend/src/i18n/locales/zh-CN.ts
+++ b/studio/frontend/src/i18n/locales/zh-CN.ts
@@ -586,6 +586,10 @@ export const zhCN = {
chatMenu: {
more: "聊天选项",
copy: "复制",
+ compact: "压缩对话",
+ compactQueued: "已请求在下一条消息时压缩对话",
+ compactEmpty: "暂无可压缩的内容",
+ compactUnsupported: "对话压缩需要支持工具调用的 GGUF 或 MLX 模型",
},
welcome: {
goodToSeeYou: "很高兴见到你",
diff --git a/studio/frontend/tests/manual-compaction.test.ts b/studio/frontend/tests/manual-compaction.test.ts
index 524cb4e8198..ab058abfc84 100644
--- a/studio/frontend/tests/manual-compaction.test.ts
+++ b/studio/frontend/tests/manual-compaction.test.ts
@@ -3,6 +3,20 @@
import assert from "node:assert/strict";
import test from "node:test";
+import ts from "typescript";
+import { getThreadState, LocalThreadRuntimeCore } from "@assistant-ui/core/internal";
+import {
+ liveThreadRunning,
+ registerLiveThreadView,
+ subscribeLiveThreadViews,
+} from "../src/features/chat/utils/live-thread-head.ts";
+import { deriveContextUsageBar } from "../src/features/chat/lib/context-usage-bar-state.ts";
+import {
+ hasPreStreamRunReservation,
+ releasePreStreamRunReservation,
+ reservePreStreamRun,
+ subscribePreStreamRunReservations,
+} from "../src/features/chat/utils/pre-stream-run-reservation.ts";
import {
compactionDividerCount,
compactionDividerRows,
@@ -255,3 +269,307 @@ test("two replies reusing a tool call id keep their own dividers", () => {
assert.equal(dividers.get("a2")?.truncation, second);
assert.equal(compactionDividerCount(messages), 2);
});
+
+function menuCallback(name: string, deps: Record<string, unknown>, argument: number | null = 0) {
+ const source = ts.createSourceFile(
+ "menu.tsx",
+ readSrc("features/chat/components/chat-header-menu.tsx"),
+ ts.ScriptTarget.Latest,
+ true,
+ ts.ScriptKind.TSX,
+ );
+ let expression: ts.Expression | undefined;
+ function visit(node: ts.Node) {
+ if (ts.isVariableDeclaration(node) && node.name.getText(source) === name) {
+ const value = node.initializer;
+ expression = value && ts.isCallExpression(value) && argument !== null ? value.arguments[argument] : value;
+ }
+ ts.forEachChild(node, visit);
+ }
+ visit(source);
+ assert.ok(expression, `menu callback ${name} exists`);
+ const code = ts.transpileModule(`return (${expression.getText(source)});`, {
+ compilerOptions: { target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.None },
+ }).outputText;
+ return new Function(...Object.keys(deps), code)(...Object.values(deps));
+}
+
+function compactMenu(threadId: string) {
+ const runtime = {
+ modelLoading: false,
+ runningByThreadId: {} as Record<string, boolean>,
+ loadedIsGguf: true,
+ loadedIsMlx: false,
+ supportsTools: true,
+ activeGgufVariant: "Q4_K_M",
+ activeNativePathToken: null,
+ params: { checkpoint: "local-model" },
+ };
+ const menu = { item: { id: threadId, type: "single" } };
+ const notices: string[] = [];
+ const branch = { ids: ["user", "reply"] as string[] | null };
+ let supported = true;
+ const click = menuCallback("compact", {
+ menu,
+ useChatRuntimeStore: { getState: () => runtime },
+ liveThreadBranch: (id: string) => id === threadId ? branch.ids : null,
+ resumesThought: (target: unknown) => {
+ assert.deepEqual(target, {
+ loadedIsGguf: runtime.loadedIsGguf,
+ loadedIsMlx: runtime.loadedIsMlx,
+ activeGgufVariant: runtime.activeGgufVariant,
+ activeNativePathToken: runtime.activeNativePathToken,
+ checkpoint: runtime.params.checkpoint,
+ });
+ return supported;
+ },
+ requestThreadCompaction,
+ hasPreStreamRunReservation,
+ liveThreadRunning,
+ toast: { info: (text: string) => notices.push(text) },
+ t: (key: string) => key,
+ }) as () => void;
+ return { click, runtime, menu, branch, notices, unsupported: () => { supported = false; } };
+}
+
+test("the compact button queues the same one-shot request for the selected chat", () => {
+ for (const id of ["menu-chat-1", "menu-chat-2"]) {
+ const menu = compactMenu(id);
+ menu.click();
+ menu.click();
+ assert.deepEqual(menu.notices, ["chatMenu.compactQueued", "chatMenu.compactQueued"]);
+ assert.equal(takeThreadCompactionRequest("another-chat"), false);
+ assert.equal(takeThreadCompactionRequest(id), true);
+ assert.equal(takeThreadCompactionRequest(id), false);
+ }
+});
+
+test("the compact button does not queue empty, hidden, unsupported or busy chats", () => {
+ for (const blocked of ["empty", "hidden", "unsupported", "running", "loading", "compare"]) {
+ const menu = compactMenu(`menu-${blocked}`);
+ if (blocked === "empty") menu.branch.ids = [];
+ if (blocked === "hidden") menu.branch.ids = null;
+ if (blocked === "unsupported") menu.unsupported();
+ if (blocked === "running") menu.runtime.runningByThreadId[menu.menu.item.id] = true;
+ if (blocked === "loading") menu.runtime.modelLoading = true;
+ if (blocked === "compare") menu.menu.item.type = "compare";
+ menu.click();
+ assert.equal(takeThreadCompactionRequest(menu.menu.item.id), false, blocked);
+ assert.deepEqual(menu.notices, blocked === "empty" || blocked === "hidden"
+ ? ["chatMenu.compactEmpty"]
+ : blocked === "unsupported" ? ["chatMenu.compactUnsupported"] : [], blocked);
+ }
+});
+
+test("the compact button requires tool capability for both local backends", () => {
+ for (const mlx of [false, true]) {
+ const menu = compactMenu(`tools-${mlx}`);
+ menu.runtime.loadedIsGguf = !mlx;
+ menu.runtime.loadedIsMlx = mlx;
+ menu.runtime.supportsTools = false;
+ menu.click();
+ assert.equal(takeThreadCompactionRequest(menu.menu.item.id), false);
+ assert.deepEqual(menu.notices, ["chatMenu.compactUnsupported"]);
+ menu.runtime.supportsTools = true;
+ menu.click();
+ assert.equal(takeThreadCompactionRequest(menu.menu.item.id), true);
+ assert.deepEqual(menu.notices, ["chatMenu.compactUnsupported", "chatMenu.compactQueued"]);
+ }
+});
+
+test("the compact button waits for its accepted send's preflight to release", () => {
+ for (const [id, siblingId] of [["preflight-a", "preflight-b"], ["preflight-b", "preflight-a"]]) {
+ const menu = compactMenu(id);
+ const isBusy = () => {
+ const preStreamRunActive = menuCallback("preStreamRunActive", {
+ menu: menu.menu,
+ hasPreStreamRunReservation,
+ subscribePreStreamRunReservations,
+ useSyncExternalStore: (
+ subscribe: typeof subscribePreStreamRunReservations,
+ snapshot: () => boolean,
+ serverSnapshot: () => boolean,
+ ) => {
+ assert.equal(subscribe, subscribePreStreamRunReservations);
+ assert.equal(serverSnapshot(), false);
+ return snapshot();
+ },
+ }, null);
+ return menuCallback("busy", {
+ menu: menu.menu,
+ preStreamRunActive,
+ liveRunActive: false,
+ useChatRuntimeStore: (selector: (state: typeof menu.runtime) => boolean) => selector(menu.runtime),
+ }, null) as boolean;
+ };
+ assert.equal(isBusy(), false);
+ const token = reservePreStreamRun([id]);
+ const sibling = reservePreStreamRun([siblingId]);
+ assert.ok(token);
+ assert.ok(sibling);
+ try {
+ assert.equal(isBusy(), true);
+ menu.click();
+ assert.equal(takeThreadCompactionRequest(id), false);
+ assert.deepEqual(menu.notices, []);
+ releasePreStreamRunReservation(token);
+ assert.equal(isBusy(), false);
+ menu.click();
+ assert.equal(takeThreadCompactionRequest(id), true);
+ assert.equal(takeThreadCompactionRequest(siblingId), false);
+ assert.deepEqual(menu.notices, ["chatMenu.compactQueued"]);
+ } finally {
+ releasePreStreamRunReservation(token);
+ releasePreStreamRunReservation(sibling);
+ takeThreadCompactionRequest(id);
+ takeThreadCompactionRequest(siblingId);
+ }
+ }
+});
+
+test("the compact button waits for a directly issued assistant run", async () => {
+ for (const [id, siblingId] of [["direct-a", "direct-b"], ["direct-b", "direct-a"]]) {
+ let finish!: () => void;
+ const gate = new Promise<void>((resolve) => { finish = resolve; });
+ const core = new LocalThreadRuntimeCore({ getModelContext: () => ({}) }, {
+ adapters: { chatModel: { run: async () => {
+ await gate;
+ return { content: [{ type: "text", text: "continued" }], status: { type: "complete", reason: "stop" } };
+ } } },
+ });
+ const item = { id: `local-${id}`, remoteId: id, externalId: undefined, title: undefined, status: "regular" as const, isMain: true };
+ const main = { id: item.id };
+ const view = {
+ subscribe: (listener: () => void) => core.subscribe(listener),
+ threads: () => ({ getState: () => ({ mainThreadId: main.id }) }),
+ threadListItem: () => ({ getState: () => item }),
+ thread: () => ({ getState: () => getThreadState(core, item) }),
+ };
+ let notifications = 0;
+ const unsubscribe = subscribeLiveThreadViews(() => { notifications++; });
+ const unregister = registerLiveThreadView(view);
+ assert.equal(notifications, 1);
+ const menu = compactMenu(id);
+ const started = core.startRun({ parentId: null, sourceId: null, runConfig: {} });
+ try {
+ assert.equal(liveThreadRunning(id), true);
+ assert.equal(liveThreadRunning(siblingId), false);
+ main.id = "other-selected-chat";
+ assert.equal(liveThreadRunning(id), false);
+ main.id = item.id;
+ assert.equal(hasPreStreamRunReservation([id]), false);
+ assert.deepEqual(menu.runtime.runningByThreadId, {});
+ menu.click();
+ assert.equal(takeThreadCompactionRequest(id), false);
+ assert.deepEqual(menu.notices, []);
+ const sibling = compactMenu(siblingId);
+ sibling.click();
+ assert.equal(takeThreadCompactionRequest(siblingId), true);
+ const isBusy = () => {
+ const liveRunActive = menuCallback("liveRunActive", {
+ menu: menu.menu, liveThreadRunning, subscribeLiveThreadViews,
+ useSyncExternalStore: (subscribe: typeof subscribeLiveThreadViews, snapshot: () => boolean, serverSnapshot: () => boolean) => {
+ assert.equal(subscribe, subscribeLiveThreadViews);
+ assert.equal(serverSnapshot(), false);
+ return snapshot();
+ },
+ }, null);
+ return menuCallback("busy", {
+ menu: menu.menu, preStreamRunActive: false, liveRunActive,
+ useChatRuntimeStore: (selector: (state: typeof menu.runtime) => boolean) => selector(menu.runtime),
+ }, null);
+ };
+ assert.equal(isBusy(), true);
+ const beforeFinish = notifications;
+ finish();
+ await started;
+ assert.ok(notifications > beforeFinish);
+ assert.equal(liveThreadRunning(id), false);
+ assert.equal(isBusy(), false);
+ menu.click();
+ assert.equal(takeThreadCompactionRequest(id), true);
+ item.remoteId = siblingId;
+ const otherStarted = core.startRun({ parentId: null, sourceId: null, runConfig: {} });
+ assert.equal(liveThreadRunning(id), false);
+ assert.equal(liveThreadRunning(siblingId), true);
+ const beforeUnmount = notifications;
+ unregister();
+ assert.ok(notifications > beforeUnmount);
+ assert.equal(liveThreadRunning(siblingId), false);
+ const afterUnmount = notifications;
+ await otherStarted;
+ assert.equal(notifications, afterUnmount);
+ } finally {
+ finish();
+ await started;
+ unregister();
+ unsubscribe();
+ takeThreadCompactionRequest(id);
+ }
+ }
+});
+
+test("the compact menu ring follows the selected chat's current context usage", () => {
+ const select = menuCallback("usedPercent", {
+ menu: { item: { id: "ring-chat" } },
+ deriveContextUsageBar,
+ }) as (state: object) => number | null;
+ const state = {
+ activeThreadId: "ring-chat",
+ contextUsage: { contextTokens: 780, totalTokens: 900 },
+ contextUsageByThreadId: { "ring-chat": { totalTokens: 200 } },
+ loadedContextLength: 1000 as number | null,
+ };
+ assert.equal(select(state), 78);
+ state.contextUsage.contextTokens = 180;
+ assert.equal(select(state), 18);
+ state.contextUsage.contextTokens = 980;
+ assert.equal(select(state), 98);
+ state.activeThreadId = "another-chat";
+ assert.equal(select(state), 20);
+ state.contextUsageByThreadId = {} as typeof state.contextUsageByThreadId;
+ assert.equal(select(state), null);
+ state.contextUsageByThreadId = { "ring-chat": { totalTokens: 200 } };
+ state.loadedContextLength = null;
+ assert.equal(select(state), null);
+});
+
+test("the compact menu item binds its action, live ring and busy state", () => {
+ const source = ts.createSourceFile(
+ "menu.tsx",
+ readSrc("features/chat/components/chat-header-menu.tsx"),
+ ts.ScriptTarget.Latest,
+ true,
+ ts.ScriptKind.TSX,
+ );
+ const items: ts.JsxElement[] = [];
+ const rings: ts.JsxSelfClosingElement[] = [];
+ function visit(node: ts.Node) {
+ if (ts.isJsxElement(node) && node.openingElement.tagName.getText(source) === "Item" &&
+ node.getText(source).includes('t("chatMenu.compact")')) items.push(node);
+ ts.forEachChild(node, visit);
+ }
+ visit(source);
+ assert.equal(items.length, 1);
+ const item = items[0];
+ function expression(tag: ts.JsxOpeningLikeElement, name: string) {
+ const attribute = tag.attributes.properties.find((prop) =>
+ ts.isJsxAttribute(prop) && prop.name.getText(source) === name,
+ ) as ts.JsxAttribute | undefined;
+ assert.ok(attribute?.initializer && ts.isJsxExpression(attribute.initializer));
+ assert.ok(attribute.initializer.expression);
+ return attribute.initializer.expression.getText(source);
+ }
+ assert.equal(expression(item.openingElement, "onSelect"), "compact");
+ const disabled = new Function("menu", "busy", `return (${expression(item.openingElement, "disabled")});`);
+ assert.equal(disabled({ item: { type: "single" } }, false), false);
+ assert.equal(disabled({ item: { type: "single" } }, true), true);
+ assert.equal(disabled({ item: { type: "compare" } }, false), true);
+ function findRing(node: ts.Node) {
+ if (ts.isJsxSelfClosingElement(node) && node.tagName.getText(source) === "UsageRing") rings.push(node);
+ ts.forEachChild(node, findRing);
+ }
+ findRing(item);
+ assert.equal(rings.length, 1);
+ assert.equal(expression(rings[0], "percent"), "usedPercent");
+});