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.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Studio: show the compaction count in the context meter (#13355)
Daniel Hanc1c306ab3bea
diff --git a/studio/frontend/src/features/chat/chat-page.tsx b/studio/frontend/src/features/chat/chat-page.tsxindex aeabf9d8b96..80dea6daa8d 100644--- a/studio/frontend/src/features/chat/chat-page.tsx+++ b/studio/frontend/src/features/chat/chat-page.tsx@@ -2808,6 +2808,24 @@ export function ChatPage({const modelLoading = useChatRuntimeStore((state) => state.modelLoading);const clearCheckpoint = useChatRuntimeStore((state) => state.clearCheckpoint);const activeThreadId = useChatRuntimeStore((state) => state.activeThreadId);+ const [compactionSnapshot, setCompactionSnapshot] = useState<{+ threadId: string;+ count: number;+ } | null>(null);+ const handleCompactionCountChange = useCallback(+ (threadId: string, count: number) => {+ setCompactionSnapshot((current) =>+ current?.threadId === threadId && current.count === count+ ? current+ : { threadId, count },+ );+ },+ [],+ );+ const compactionCount =+ compactionSnapshot?.threadId === activeThreadId+ ? compactionSnapshot.count+ : 0;const latestResearchRunId = useResearchRunStore((state) =>activeThreadId ? state.latestRunByThreadId[activeThreadId] : undefined,);@@ -4793,6 +4811,7 @@ export function ChatPage({? contextUsage.contextTokens - contextUsage.promptTokens: contextUsage?.completionTokens}+ compactionCount={compactionCount}isMlx={isServedByMlx(Boolean(loadedIsGguf),platformDeviceType,@@ -4925,6 +4944,7 @@ export function ChatPage({? markProjectRuntimeReady: undefined}+ onCompactionCountChange={handleCompactionCountChange}>{baseView.mode === "project" ? (<ProjectLandingdiff --git a/studio/frontend/src/features/chat/components/context-usage-bar.tsx b/studio/frontend/src/features/chat/components/context-usage-bar.tsxindex 2d42ae3ba26..4258b5884db 100644--- a/studio/frontend/src/features/chat/components/context-usage-bar.tsx+++ b/studio/frontend/src/features/chat/components/context-usage-bar.tsx@@ -209,6 +209,14 @@ export const ContextUsageBar: FC<</span></div>)}+ {(input.compactionCount ?? 0) > 0 && (+ <div className="flex items-center justify-between gap-4">+ <span className="text-muted-foreground">Compactions</span>+ <span className="font-mono tabular-nums">+ {input.compactionCount}+ </span>+ </div>+ )}{cached !== undefined && cached > 0 && (<div className="flex items-center justify-between gap-4"><span className="text-muted-foreground">Cache hits</span>diff --git a/studio/frontend/src/features/chat/lib/context-usage-bar-state.ts b/studio/frontend/src/features/chat/lib/context-usage-bar-state.tsindex ad814cb041d..47c8e4009b4 100644--- a/studio/frontend/src/features/chat/lib/context-usage-bar-state.ts+++ b/studio/frontend/src/features/chat/lib/context-usage-bar-state.ts@@ -20,6 +20,7 @@ export type ContextUsageBarInput = {cached?: number;// anthropic-only (billed at the write premium)cacheWrites?: number;+ compactionCount?: number;promptTokens?: number;completionTokens?: number;// MLX keeps generating past the window instead of stopping there, so it needs thediff --git a/studio/frontend/src/features/chat/runtime-provider.tsx b/studio/frontend/src/features/chat/runtime-provider.tsxindex cf581b58707..34ebc094c1c 100644--- a/studio/frontend/src/features/chat/runtime-provider.tsx+++ b/studio/frontend/src/features/chat/runtime-provider.tsx@@ -56,6 +56,7 @@ import {streamChatCompletions,uploadChatAttachmentOriginal,} from "./api/chat-api";+import { compactionNoticeMessageIds } from "@/components/assistant-ui/message-derived";import { selectCodeToolNames } from "./api/code-tool-placement";import { getResearchThreadState } from "./api/research-api";import {@@ -3622,6 +3623,25 @@ function ActiveBranchRegistrar({return null;}+// The header meter sits outside AssistantRuntimeProvider, so the count is published upward.+// Same set the thread renders notices for, so the meter never disagrees with the transcript.+function CompactionCountRegistrar({+ onCountChange,+}: {+ onCountChange: (threadId: string, count: number) => void;+}): ReactElement | null {+ const threadId = useAuiState(({ threadListItem }) => threadListItem.remoteId);+ const count = useAuiState(+ ({ thread }) => compactionNoticeMessageIds(thread.messages).size,+ );++ useEffect(() => {+ if (threadId) onCountChange(threadId, count);+ }, [count, onCountChange, threadId]);++ return null;+}+// include hidden panes because background replies also move the branch headfunction BranchHeadRecorder(): ReactElement | null {useBranchHeadRecorder();@@ -4002,6 +4022,7 @@ export function ChatRuntimeProvider({listThreads = true,backgrounded = false,onInitialHistoryReady,+ onCompactionCountChange,}: {children: ReactNode;modelType?: ModelType;@@ -4015,6 +4036,7 @@ export function ChatRuntimeProvider({// stays alive, and everything driving the shared single-chat state stands down.backgrounded?: boolean;onInitialHistoryReady?: () => void;+ onCompactionCountChange?: (threadId: string, count: number) => void;}): ReactElement {const signalReady = useAppShellReadySignal();// Read by the history adapter's own active-thread publication, the sibling of@@ -4102,6 +4124,9 @@ export function ChatRuntimeProvider({<ThreadContextUsageRecountenabled={modelType === "base" && !pairId && !backgrounded}/>+ {onCompactionCountChange ? (+ <CompactionCountRegistrar onCountChange={onCompactionCountChange} />+ ) : null}<ThreadBackendAutosavemodelType={modelType}pairId={pairId}diff --git a/studio/frontend/tests/message-derived.test.ts b/studio/frontend/tests/message-derived.test.tsindex e816a35811c..da376a47e8b 100644--- a/studio/frontend/tests/message-derived.test.ts+++ b/studio/frontend/tests/message-derived.test.ts@@ -187,6 +187,8 @@ test("compaction notice ids match the per-message walk", () => {assert.equal(ids.has(message.id), walk(messages, message.id), message.id);}assert.deepEqual([...ids], ["a1", "a3", "a4"]);+ // The context meter's compaction count is this set's size.+ assert.equal(ids.size, 3);assert.equal(ids.has("missing"), false);assert.equal(compactionNoticeMessageIds(messages), ids);assert.notEqual(compactionNoticeMessageIds([...messages]), ids);diff --git a/studio/frontend/tests/rolling-context-window.test.ts b/studio/frontend/tests/rolling-context-window.test.tsindex 2eb8b2d3c33..c6631ee77f3 100644--- a/studio/frontend/tests/rolling-context-window.test.ts+++ b/studio/frontend/tests/rolling-context-window.test.ts@@ -17,6 +17,9 @@ const COMPACTION_NOTICE = readSrc("components/assistant-ui/compaction-notice.tsxconst THREAD = readSrc("components/assistant-ui/thread.tsx");const DERIVED = readSrc("components/assistant-ui/message-derived.ts");const CHAT_ADAPTER = readSrc("features/chat/api/chat-adapter.ts");+const RUNTIME_PROVIDER = readSrc("features/chat/runtime-provider.tsx");+const CHAT_PAGE = readSrc("features/chat/chat-page.tsx");+const CONTEXT_USAGE_BAR = readSrc("features/chat/components/context-usage-bar.tsx");const adapter = readSrc("features/chat/api/chat-adapter.ts");const transport = readSrc("features/chat/api/chat-api.ts");@@ -263,6 +266,20 @@ test("a boundary that goes BACKWARDS does not re-announce", () => {assert.deepStrictEqual(noticeTurns([52, 20, 20, 20]), [0]);});+test("the context meter counts exactly the compaction notices the thread renders", () => {+ assert.match(+ RUNTIME_PROVIDER,+ /compactionNoticeMessageIds\(thread\.messages\)\.size/,+ );+ assert.match(RUNTIME_PROVIDER, /<CompactionCountRegistrar onCountChange=/);+ // Project views show the meter for a chat started on the landing page too.+ assert.match(CHAT_PAGE, /onCompactionCountChange=\{handleCompactionCountChange\}/);+ assert.match(CHAT_PAGE, /compactionCount=\{compactionCount\}/);+ // A chat that never compacted keeps the tooltip it had.+ assert.match(CONTEXT_USAGE_BAR, /\(input\.compactionCount \?\? 0\) > 0 && \(/);+ assert.match(CONTEXT_USAGE_BAR, />Compactions</);+});+/** The source of one function, by brace matching from its declaration. */const functionBody = (source: string, name: string): string => {const start = source.indexOf(`function ${name}(`);