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: compact context usage ring when the chat header is squeezed (#12355)
Michael Hanb96af046dc6e
diff --git a/studio/frontend/src/features/chat/chat-page.tsx b/studio/frontend/src/features/chat/chat-page.tsxindex 8eead5a3ff8..95906d59922 100644--- a/studio/frontend/src/features/chat/chat-page.tsx+++ b/studio/frontend/src/features/chat/chat-page.tsx@@ -4222,7 +4222,7 @@ export function ChatPage({"right-[var(--thread-scrollbar-gutter,10px)] left-auto w-auto bg-transparent pl-0 pr-[calc(0.5rem*var(--ui-space-scale,1)+var(--studio-chat-header-right-inset,var(--studio-window-control-inset,0px)))]",)}>- <div className="pointer-events-auto flex items-center gap-1">+ <div className="pointer-events-auto flex min-w-0 items-center gap-1">{isTauri && !isMobile && !pinned && view.mode !== "compare" && (<Buttontype="button"@@ -4231,7 +4231,7 @@ export function ChatPage({title="New chat"aria-label="New chat"onClick={handleDesktopNewChat}- className="!size-[calc(30px*var(--ui-space-scale,1))] rounded-[10px] text-muted-foreground"+ className="!size-[calc(30px*var(--ui-space-scale,1))] shrink-0 rounded-[10px] text-muted-foreground"><HugeiconsIconicon={PencilEdit02Icon}@@ -4346,7 +4346,7 @@ export function ChatPage({</div>) : null}</div>- <div className="pointer-events-auto ml-auto flex items-center gap-1">+ <div className="pointer-events-auto ml-auto flex min-w-min max-w-max grow basis-0 items-center gap-1 *:shrink-0">{showContextWindowUsage &&view.mode === "single" &&(contextUsage || contextWindowKnown) ? (diff --git a/studio/frontend/src/features/chat/components/context-usage-bar.tsx b/studio/frontend/src/features/chat/components/context-usage-bar.tsxindex 31cbb6af18a..0669195b776 100644--- a/studio/frontend/src/features/chat/components/context-usage-bar.tsx+++ b/studio/frontend/src/features/chat/components/context-usage-bar.tsx@@ -6,7 +6,7 @@ import {TooltipTrigger,} from "@/components/ui/tooltip";import { cn } from "@/lib/utils";-import type { FC } from "react";+import type { CSSProperties, FC } from "react";import {type ContextUsageBarInput,deriveContextUsageBar,@@ -15,65 +15,124 @@ import {function getSeverityColor(percent: number): {bar: string;+ stroke: string;text: string;} {- if (percent > 85) return { bar: "bg-red-500", text: "text-red-500" };- if (percent > 65) return { bar: "bg-amber-500", text: "text-amber-500" };- return { bar: "bg-control-accent", text: "text-control-accent" };+ if (percent > 85) {+ return { bar: "bg-red-500", stroke: "stroke-red-500", text: "text-red-500" };+ }+ if (percent > 65) {+ return { bar: "bg-amber-500", stroke: "stroke-amber-500", text: "text-amber-500" };+ }+ return {+ bar: "bg-control-accent",+ stroke: "stroke-control-accent",+ text: "text-control-accent",+ };}+// Outer edge matches the header's icon glyphs.+const RING_RADIUS = 6.25;+const RING_LENGTH = 2 * Math.PI * RING_RADIUS;++const UsageRing: FC<{ percent: number | null; stroke: string }> = ({+ percent,+ stroke,+}) => (+ <svg viewBox="0 0 16 16" aria-hidden={true} className="size-icon shrink-0 -rotate-90">+ <circle cx={8} cy={8} r={RING_RADIUS} fill="none" strokeWidth={2} className="stroke-(--track)" />+ {percent ? (+ <circle+ cx={8}+ cy={8}+ r={RING_RADIUS}+ fill="none"+ strokeWidth={2}+ strokeLinecap="round"+ strokeDasharray={RING_LENGTH}+ strokeDashoffset={RING_LENGTH * (1 - percent / 100)}+ className={cn("transition-[stroke-dashoffset]", stroke)}+ />+ ) : null}+ </svg>+);+export const ContextUsageBar: FC<ContextUsageBarInput & { className?: string }-> = ({- used,- total,- cached,- cacheWrites,- promptTokens,- completionTokens,- isMlx,- contextEnforced,- contextBudget,- className,-}) => {- const state = deriveContextUsageBar({- used,- total,- cached,- cacheWrites,- promptTokens,- completionTokens,- isMlx,- contextEnforced,- contextBudget,- });+> = ({ className, ...input }) => {+ const state = deriveContextUsageBar(input);if (!state) return null;- const { percent, advice } = state;+ const { cached, cacheWrites, promptTokens, completionTokens } = input;+ const { percent, advice, face, compactFace } = state;const severity = getSeverityColor(percent ?? 0);+ // Mono text, so widths are exact in ch. Full: padding, face, and the gap and bar. Compact: an icon button.+ const fullWidth = `calc(${face.length}ch + ${percent !== null ? 23 : 5} * var(--spacing))`;+ const compactWidth =+ compactFace === null ? "calc(30px * var(--ui-space-scale, 1))" : `calc(${compactFace.length}ch + 5 * var(--spacing))`;+ const hover = "rounded-[10px] transition-colors group-hover:bg-chat-icon-bg-hover";return (<Tooltip>- <TooltipTrigger asChild>- <button- type="button"- aria-label={state.label}- className={cn(- "flex items-center gap-2 rounded-[10px] px-2.5 py-1 font-mono text-chat-icon-fg text-ui-13 tabular-nums transition-colors hover:bg-chat-icon-bg-hover hover:text-chat-icon-fg-hover",- className,- )}- >- <span>{state.face}</span>- {percent !== null ? (- <div className="h-1.5 w-16 rounded-full bg-[rgb(0_0_0_/_calc(0.1*var(--contrast-wash-gain,1)))] dark:bg-[rgb(255_255_255_/_calc(0.15*var(--contrast-wash-gain,1)))] overflow-hidden">- <div- className={cn("h-full rounded-full transition-all", severity.bar)}- style={{ width: `${percent}%` }}- />- </div>- ) : null}- </button>- </TooltipTrigger>+ {/* The header squeezes this wrapper; the button inside takes only the face it shows. */}+ <div+ style={+ {+ "--full": fullWidth,+ "--compact": compactWidth,+ gridTemplateColumns: "minmax(var(--compact), var(--full))",+ } as CSSProperties+ }+ // Mono here too, so ch in the widths resolves the same as in the button.+ className={cn("grid shrink! items-center font-mono text-ui-13", className)}+ >+ <TooltipTrigger asChild>+ <button+ type="button"+ aria-label={state.label}+ // The full face where it fits, else the compact one, right-aligned against the icons.+ style={{+ width:+ "calc(clamp(0px, (100% - var(--full) + 1px) * 999, var(--full)) + clamp(0px, (var(--full) - 100% - 1px) * 999, var(--compact)))",+ }}+ className={cn(+ "group grid h-full items-center justify-self-end overflow-hidden rounded-[10px] text-chat-icon-fg tabular-nums whitespace-nowrap hover:text-chat-icon-fg-hover focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring",+ // ring and bar track+ "[--track:rgb(0_0_0_/_calc(0.1*var(--contrast-wash-gain,1)))] dark:[--track:rgb(255_255_255_/_calc(0.15*var(--contrast-wash-gain,1)))]",+ )}+ >+ {/* Exactly one of these has width, matching the button's. */}+ <span+ className="col-start-1 row-start-1 flex h-full items-center justify-center overflow-hidden"+ style={{ width: "clamp(0px, (var(--full) - 100% - 1px) * 999, 100%)" }}+ >+ {compactFace === null ? (+ <span className={cn("flex size-[calc(30px*var(--ui-space-scale,1))] shrink-0 items-center justify-center", hover)}>+ <UsageRing percent={percent} stroke={severity.stroke} />+ </span>+ ) : (+ <span className={cn("flex h-full shrink-0 items-center px-2.5", hover)}>{compactFace}</span>+ )}+ </span>+ <span+ className="col-start-1 row-start-1 h-full overflow-hidden"+ style={{ width: "clamp(0px, (100% - var(--full) + 1px) * 999, var(--full))" }}+ >+ <span className={cn("flex h-full w-(--full) items-center gap-2 px-2.5", hover)}>+ <span>{face}</span>+ {percent !== null ? (+ <span className="h-1.5 w-16 shrink-0 overflow-hidden rounded-full bg-(--track)">+ <span+ className={cn("block h-full rounded-full transition-all", severity.bar)}+ style={{ width: `${percent}%` }}+ />+ </span>+ ) : null}+ </span>+ </span>+ </button>+ </TooltipTrigger>+ </div><TooltipContentside="bottom"sideOffset={8}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 c4368950e64..38d324c841f 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@@ -78,6 +78,8 @@ function contextLimitAdvice(export type ContextUsageBarState = {face: string;+ // shown instead of the ring when the header is too narrow for the face; null shows the ring+ compactFace: string | null;label: string;totalRowName: string;totalRowValue: string;@@ -117,6 +119,7 @@ export function deriveContextUsageBar({if (usedTokens <= 0 && !hasUsageDetails) return null;return {face: `${formatTokenCount(usedTokens)} tokens`,+ compactFace: formatTokenCount(usedTokens),label: `Token usage: ${formatTokenCount(usedTokens)} tokens`,totalRowName: "Total tokens",totalRowValue: formatTokenCountFull(usedTokens),@@ -129,6 +132,7 @@ export function deriveContextUsageBar({if (usedTokens === null) {return {face: `— / ${formatTokenCount(limit)}`,+ compactFace: null,label: `Context window: ${formatTokenCount(limit)} tokens, usage not counted yet`,totalRowName: "Context window",totalRowValue: formatTokenCountFull(limit),@@ -138,12 +142,14 @@ export function deriveContextUsageBar({};}+ const percent = Math.min((usedTokens / limit) * 100, 100);return {face: `${formatTokenCount(usedTokens)} / ${formatTokenCount(limit)}`,+ compactFace: null,label: `Context usage: ${formatTokenCount(usedTokens)} of ${formatTokenCount(limit)} tokens`,totalRowName: "Total",totalRowValue: `${formatTokenCountFull(usedTokens)} / ${formatTokenCountFull(limit)}`,- percent: Math.min((usedTokens / limit) * 100, 100),+ percent,hasUsageDetails,advice: contextLimitAdvice(usedTokens,diff --git a/studio/frontend/tests/context-usage-compact.test.ts b/studio/frontend/tests/context-usage-compact.test.tsnew file mode 100644index 00000000000..2e747caf5ef--- /dev/null+++ b/studio/frontend/tests/context-usage-compact.test.ts@@ -0,0 +1,31 @@+// SPDX-License-Identifier: AGPL-3.0-only+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++// When the chat header is squeezed, the context bar shrinks to a ring.++import assert from "node:assert/strict";+import test from "node:test";++import { deriveContextUsageBar } from "../src/features/chat/lib/context-usage-bar-state.ts";++import { readSrc } from "./helpers/kit.ts";++test("the compact face is the ring, unless there is no window to fill", () => {+ // a known window: the ring alone, with no percent label+ assert.equal(deriveContextUsageBar({ used: 16384, total: 32768 })?.compactFace, null);+ // nothing counted yet: the empty ring+ assert.equal(deriveContextUsageBar({ used: null, total: 32768 })?.compactFace, null);+ // no window: the token count stands in for the ring+ assert.equal(deriveContextUsageBar({ used: 4096, total: null })?.compactFace, "4.1k");+});++test("the bar hands every input to the state, batching flags included", () => {+ const bar = readSrc("features/chat/components/context-usage-bar.tsx");+ assert.match(bar, /\(\{ className, \.\.\.input \}\) => \{\s*const state = deriveContextUsageBar\(input\);/);+});++test("the header shrinks the context bar before the model name", () => {+ const page = readSrc("features/chat/chat-page.tsx");+ assert.match(page, /"pointer-events-auto flex min-w-0 items-center gap-1"/);+ assert.match(page, /ml-auto flex min-w-min max-w-max grow basis-0 items-center gap-1 \*:shrink-0/);+});