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: mark where a chat was compacted with an inline divider (#13502)
oobabooga7ea699ff2cdd
diff --git a/studio/frontend/src/components/assistant-ui/compaction-divider.tsx b/studio/frontend/src/components/assistant-ui/compaction-divider.tsxnew file mode 100644index 00000000000..02e2c7f3607--- /dev/null+++ b/studio/frontend/src/components/assistant-ui/compaction-divider.tsx@@ -0,0 +1,85 @@+// SPDX-License-Identifier: AGPL-3.0-only+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++import { ArchiveIcon } from "lucide-react";+import type { FC, ReactNode } from "react";++import {+ Tooltip,+ TooltipContent,+ TooltipTrigger,+} from "@/components/ui/tooltip";+import { type ContextTruncation, promptWasShortened } from "@/features/chat";++const DetailRow: FC<{ label: string; value: ReactNode }> = ({+ label,+ value,+}) => (+ <div className="flex items-center justify-between gap-4">+ <span className="text-muted-foreground">{label}</span>+ <span className="font-mono tabular-nums">{value}</span>+ </div>+);++/** renders metadata once per compaction above its triggering prompt and survives reloads without entering model context, editable content, or exports. */+export const CompactionDivider: FC<{ truncation: ContextTruncation }> = ({+ truncation,+}) => {+ if (!promptWasShortened(truncation)) return null;++ const summarized = truncation.summarized === true;+ const dropped = truncation.dropped_messages;+ const archived = truncation.archived_messages ?? 0;+ const recalled = truncation.recalled_chunks ?? 0;+ const before = truncation.prompt_tokens_before;+ const after = truncation.prompt_tokens_after;++ return (+ <div+ data-testid="compaction-divider"+ className="mx-auto mb-6 flex w-full max-w-(--thread-content-max-width) items-center gap-3 text-muted-foreground text-sm"+ >+ <span aria-hidden={true} className="h-px flex-1 bg-border" />+ <Tooltip>+ <TooltipTrigger asChild>+ <button+ type="button"+ data-slot="compaction-divider-label"+ // negative margins keep a large target from moving the rule.+ // positioning stops the reply above from covering the target.+ className="relative -mx-2 -my-2.5 inline-flex shrink-0 cursor-default items-center gap-1.5 whitespace-nowrap rounded-md px-2 py-2.5 leading-none transition-colors hover:text-foreground focus-visible:outline-2 focus-visible:outline-ring"+ >+ <ArchiveIcon className="size-3.5" aria-hidden={true} />+ Context compacted+ </button>+ </TooltipTrigger>+ <TooltipContent side="top" sideOffset={8} variant="rich">+ <div className="grid max-w-72 gap-1.5 text-xs">+ <p className="leading-snug">+ {summarized+ ? "The provider summarized older messages to make room."+ : "Older messages were dropped from the model's context to make room."}{" "}+ {archived+ ? "They are saved and searchable, and the parts relevant to each question are brought back automatically."+ : "The full conversation is still visible and saved here."}+ </p>+ <DetailRow+ label={summarized ? "Messages summarized" : "Messages dropped"}+ value={dropped}+ />+ {recalled > 0 && (+ <DetailRow label="Passages recalled" value={recalled} />+ )}+ {before !== undefined && after !== undefined && (+ <DetailRow+ label="Prompt tokens"+ value={`${before.toLocaleString()} → ${after.toLocaleString()}`}+ />+ )}+ </div>+ </TooltipContent>+ </Tooltip>+ <span aria-hidden={true} className="h-px flex-1 bg-border" />+ </div>+ );+};diff --git a/studio/frontend/src/components/assistant-ui/compaction-notice.tsx b/studio/frontend/src/components/assistant-ui/compaction-notice.tsxdeleted file mode 100644index b9782569e47..00000000000--- a/studio/frontend/src/components/assistant-ui/compaction-notice.tsx+++ /dev/null@@ -1,68 +0,0 @@-// SPDX-License-Identifier: AGPL-3.0-only-// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0--import { ArchiveIcon } from "lucide-react";-import type { FC } from "react";--import {- type ContextTruncation,- promptWasShortened,-} from "@/features/chat/utils/context-truncation";--/**- * Tells the user their conversation was compacted, on the turn where it STARTED.- *- * Deliberately NOT a message: it renders inside the assistant message's container from- * `metadata.custom.contextTruncation`, so it is never sent to the model, editable, or- * exported as content, yet unlike a toast it survives a reload and stays attached to the- * turn it describes.- *- * Rendered once per COMPACTION, gated by the caller, not once per compacted turn: a- * thread past its window refits on every request, so per-turn would mean a notice on- * every reply forever. The caller shows this when the boundary moved or a checkpoint started.- */-export const CompactionNotice: FC<{ truncation: ContextTruncation }> = ({- truncation,-}) => {- if (!promptWasShortened(truncation)) return null;-- const dropped = truncation.dropped_messages;- const archived = truncation.archived_messages ?? 0;- const recalled = truncation.recalled_chunks ?? 0;-- const detail = archived- ? "They are saved and searchable, and the parts relevant to each question are brought back automatically."- : "The full conversation is still visible and saved here.";-- return (- <div- className="aui-compaction-notice mb-3 flex items-start gap-2 rounded-lg border border-border/60 bg-muted/40 px-3 py-2 text-ui-13 text-muted-foreground"- data-testid="compaction-notice"- data-dropped={dropped}- data-archived={archived}- data-recalled={recalled}- >- <ArchiveIcon className="mt-0.5 size-3.5 shrink-0" aria-hidden />- <div className="min-w-0">- <span className="font-medium text-foreground/80">- This conversation got long, so it was compacted.- </span>{" "}- <span>- {truncation.summarized- ? "The provider summarized older messages to make room."- : "Older messages were dropped from the model's context to make room."}{" "}- {detail}- </span>- <span>- {" "}- ({dropped} {dropped === 1 ? "message" : "messages"}{" "}- {truncation.summarized ? "summarized" : "dropped"} here- {recalled > 0- ? `, ${recalled} earlier ${recalled === 1 ? "passage" : "passages"} recalled`- : ""}- .)- </span>- </div>- </div>- );-};diff --git a/studio/frontend/src/components/assistant-ui/message-derived.ts b/studio/frontend/src/components/assistant-ui/message-derived.tsindex 6955b3ca49d..57564e292ae 100644--- a/studio/frontend/src/components/assistant-ui/message-derived.ts+++ b/studio/frontend/src/components/assistant-ui/message-derived.ts@@ -127,3 +127,34 @@ export const compactionNoticeMessageIds = memoOnArray(return ids;},);++const NO_COMPACTION_DIVIDERS: ReadonlyMap<string, ContextTruncation> =+ new Map();++/** maps each compaction to its triggering prompt row or, if absent, its reply row at the kept-context boundary so visible dividers match the context meter. */+export const compactionDividerRows = memoOnArray(+ (+ messages: readonly MessageLike[],+ ): ReadonlyMap<string, ContextTruncation> => {+ const notices = compactionNoticeMessageIds(messages);+ // streamed tokens create new arrays, so reuse this map until a thread compacts.+ if (notices.size === 0) return NO_COMPACTION_DIVIDERS;+ const rows = new Map<string, ContextTruncation>();+ let previous: MessageLike | undefined;+ for (const message of messages) {+ if (notices.has(message.id)) {+ const row = previous?.role === "user" ? previous.id : message.id;+ rows.set(+ row,+ (+ message.metadata as {+ custom: { contextTruncation: ContextTruncation };+ }+ ).custom.contextTruncation,+ );+ }+ previous = message;+ }+ return rows;+ },+);diff --git a/studio/frontend/src/components/assistant-ui/thread.tsx b/studio/frontend/src/components/assistant-ui/thread.tsxindex ac80655b1cf..15584192f64 100644--- a/studio/frontend/src/components/assistant-ui/thread.tsx+++ b/studio/frontend/src/components/assistant-ui/thread.tsx@@ -22,12 +22,11 @@ import {useGeneratedImageOverlay,} from "@/components/assistant-ui/generated-image-overlay-context";import { AutoReadAloud } from "@/components/assistant-ui/auto-read-aloud";-import { CompactionNotice } from "@/components/assistant-ui/compaction-notice";+import { CompactionDivider } from "@/components/assistant-ui/compaction-divider";import {- compactionNoticeMessageIds,+ compactionDividerRows,memoOnArrayByKey,} from "@/components/assistant-ui/message-derived";-import type { ContextTruncation } from "@/features/chat/utils/context-truncation";import { downloadImagePart } from "@/components/assistant-ui/image";import {MarkdownText,@@ -97,6 +96,7 @@ import {type ComposerSendShortcut,type ComposerSubmitIntent,type ComposerFollowUpBehavior,+ type ContextTruncation,attachmentsPastedText,hasPendingPromptQueueStart,isPastedTextFile,@@ -1849,12 +1849,24 @@ const ThreadMessage: FC = () => {}return (<>+ <CompactionRowDivider />{body}<ForkContinuationRule /></>);};+// The selector returns the persisted record, which keeps its identity between renders, so a+// row re-renders only when a divider arrives or leaves above it.+const CompactionRowDivider: FC = () => {+ const truncation = useAuiState(+ ({ thread, message }) =>+ compactionDividerRows(thread.messages).get(message.id) ?? null,+ );+ if (!truncation) return null;+ return <CompactionDivider truncation={truncation} />;+};+/*** Resolves the divider against the branch on screen, once for the thread.*@@ -8051,26 +8063,6 @@ const AssistantMessage: FC = () => {researchReplyOwnsRun(boundResearchAssistantMessageId, messageId)? metadataResearchRunId: null;- // Persisted on the assistant turn that compacted, so the notice survives a reload.- const contextTruncation = useAuiState(({ message }) => {- const custom = (- message.metadata as- | { custom?: { contextTruncation?: unknown } }- | undefined- )?.custom;- const value = custom?.contextTruncation;- return value && typeof value === "object"- ? (value as ContextTruncation)- : null;- });- // Once a thread outgrows the window every request runs the fit, so "this turn- // compacted" is true of every later reply and would put a notice on all of them. What- // matters is when MORE of the conversation fell out of view: the eviction boundary- // rising above the last turn that reported one, or a checkpoint starting inside a tool- // loop (which evicts without moving the boundary). Sticky replays stay quiet.- const showsNotice = useAuiState(({ thread }) =>- compactionNoticeMessageIds(thread.messages).has(messageId),- );const incognito = useChatRuntimeStore((s) => s.incognito);// Use global store for editing state to ensure a single source of truth@@ -8146,9 +8138,6 @@ const AssistantMessage: FC = () => {onBlur={focusReveal.onBlur}><div className="aui-assistant-message-content wrap-break-word min-w-0 text-[#0d0d0d] dark:text-foreground leading-relaxed">- {contextTruncation && showsNotice && !isEditing && (- <CompactionNotice truncation={contextTruncation} />- )}{isEditing ? (<div className="flex flex-col gap-2 w-full">{/* Borderless textarea, so auto-grow fits with no scrollbar; the wrapper keeps corners round. */}diff --git a/studio/frontend/src/features/chat/index.ts b/studio/frontend/src/features/chat/index.tsindex be40afa8e24..36367cf0b5e 100644--- a/studio/frontend/src/features/chat/index.ts+++ b/studio/frontend/src/features/chat/index.ts@@ -212,6 +212,10 @@ export type {SidebarOrganizeBy,} from "./stores/sidebar-organization-store";export { useChatPreferencesStore } from "./stores/chat-preferences-store";+export {+ type ContextTruncation,+ promptWasShortened,+} from "./utils/context-truncation";export { SectionNameDialog } from "./components/section-name-dialog";export { removeCustomSectionWithUndo } from "./stores/remove-custom-section";export {diff --git a/studio/frontend/tests/message-derived.test.ts b/studio/frontend/tests/message-derived.test.tsindex 5670027b636..d14dc390c2e 100644--- a/studio/frontend/tests/message-derived.test.ts+++ b/studio/frontend/tests/message-derived.test.ts@@ -4,6 +4,7 @@import assert from "node:assert/strict";import test from "node:test";import {+ compactionDividerRows,compactionNoticeMessageIds,memoOnArray,memoOnArrayByKey,@@ -196,6 +197,47 @@ test("compaction notice ids match the per-message walk", () => {assert.notEqual(compactionNoticeMessageIds([...messages]), ids);});+test("each compaction draws one divider, above the prompt whose request compacted", () => {+ const compacted = (boundary: number) => ({+ custom: {+ contextTruncation: {+ dropped_messages: boundary,+ boundary_messages: boundary,+ fits: true,+ },+ },+ });+ const messages = [+ // A reply with no prompt above it draws the divider above itself.+ { id: "a0", role: "assistant", metadata: compacted(1) },+ { id: "u1", role: "user" },+ { id: "a1", role: "assistant", metadata: compacted(2) },+ { id: "u2", role: "user" },+ // A replayed boundary is not a new compaction: no divider.+ { id: "a2", role: "assistant", metadata: compacted(2) },+ { id: "u3", role: "user" },+ { id: "a3", role: "assistant" },+ // A reply that follows another reply (a continuation round) marks itself.+ { id: "a4", role: "assistant", metadata: compacted(5) },+ ];+ const rows = compactionDividerRows(messages);+ assert.deepEqual([...rows.keys()], ["a0", "u1", "a4"]);+ assert.equal(+ rows.get("u1"),+ (messages[2].metadata as { custom: { contextTruncation: unknown } }).custom+ .contextTruncation,+ );+ // One divider per compaction, so the meter's count is the number of dividers on screen.+ assert.equal(compactionNoticeMessageIds(messages).size, rows.size);+ assert.equal(compactionDividerRows(messages), rows);+ // A thread that never compacted shares one empty result rather than building one per token.+ const plain = [+ { id: "u", role: "user" },+ { id: "a", role: "assistant" },+ ];+ assert.equal(compactionDividerRows(plain), compactionDividerRows([...plain]));+});+test("memoOnArrayByKey caches per array and per key", () => {const messages = [{ id: "a" }, { id: "b" }];let computed = 0;diff --git a/studio/frontend/tests/rolling-context-window.test.ts b/studio/frontend/tests/rolling-context-window.test.tsindex c6631ee77f3..0e6440e4789 100644--- a/studio/frontend/tests/rolling-context-window.test.ts+++ b/studio/frontend/tests/rolling-context-window.test.ts@@ -13,7 +13,7 @@ import {import { readSrc } from "./helpers/kit.ts";-const COMPACTION_NOTICE = readSrc("components/assistant-ui/compaction-notice.tsx");+const COMPACTION_DIVIDER = readSrc("components/assistant-ui/compaction-divider.tsx");const 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");@@ -150,8 +150,8 @@ test("a rescued turn cannot silence the compactions that follow it", () => {});test("the notice and the toast read the same predicate as the boundary", () => {- assert.match(COMPACTION_NOTICE, /promptWasShortened\(truncation\)/);- assert.doesNotMatch(COMPACTION_NOTICE, /truncation\?\.fits/);+ assert.match(COMPACTION_DIVIDER, /promptWasShortened\(truncation\)/);+ assert.doesNotMatch(COMPACTION_DIVIDER, /truncation\?\.fits/);assert.match(adapter, /promptWasShortened\(chunk\.context_truncated\)/);});@@ -217,16 +217,15 @@ test("compaction counts accumulate and stay absent on a plain rolling window", (test("the compaction notice renders from persisted metadata, not from a message", () => {// Read off metadata.custom so it can never become part of the conversation.- assert.match(THREAD, /custom\?\.contextTruncation/);- assert.match(THREAD, /<CompactionNotice truncation=\{contextTruncation\}/);- assert.match(COMPACTION_NOTICE, /This conversation got long, so it was compacted/);+ assert.match(DERIVED, /\.custom\.contextTruncation/);+ assert.match(THREAD, /<CompactionDivider truncation=\{truncation\} \/>/);+ assert.match(COMPACTION_DIVIDER, /Context compacted/);});test("the compaction notice uses the shared boundary/checkpoint predicate", () => {// Replayed fits stay quiet; a new boundary or checkpoint is a new compaction.- assert.match(THREAD, /const showsNotice = useAuiState/);- assert.match(THREAD, /contextTruncation && showsNotice && !isEditing/);- assert.match(THREAD, /compactionNoticeMessageIds\(thread\.messages\)\.has\(messageId\)/);+ assert.match(THREAD, /compactionDividerRows\(thread\.messages\)\.get\(message\.id\)/);+ assert.match(DERIVED, /const notices = compactionNoticeMessageIds\(messages\)/);assert.match(DERIVED, /shouldShowCompactionNotice\(value, previousDropped\)/);// Walked in order, not against the preceding message: turns between two moves report// the same count and must not reset the baseline.@@ -299,17 +298,7 @@ const functionBody = (source: string, name: string): string => {test("the notice is a NOTICE, never part of the conversation", () => {const exporter = readSrc("features/chat/utils/conversation-markdown-export.ts");- // 1. A sibling of the rendered content parts, not one of them: inside- // MessagePrimitive.Parts everything that walks parts would pick it up.- const noticeAt = THREAD.indexOf("<CompactionNotice");- const partsAt = THREAD.indexOf("<MessagePrimitive.Parts", noticeAt);- assert.ok(noticeAt > 0 && partsAt > noticeAt);- assert.ok(- !/<MessagePrimitive\.Parts[^>]*>[\s\S]*<CompactionNotice/.test(THREAD),- "the notice must not be rendered inside the message's content parts",- );-- // 2. Nothing that builds a request may read the key it renders from. Bounded to the+ // 1. Nothing that builds a request may read the key it renders from. Bounded to the// function bodies: slicing to end-of-file also catches the streaming handler,// which reads contextTruncation legitimately on the way IN.for (const name of ["toOpenAIMessages", "serializeAssistantReplayMessages"]) {@@ -321,12 +310,13 @@ test("the notice is a NOTICE, never part of the conversation", () => {);}- // 3. Nor may the user-facing export, which is the other way text leaves a thread.+ // 2. Nor may the user-facing export, which is the other way text leaves a thread.assert.ok(!exporter.includes("contextTruncation"));assert.ok(!exporter.includes("compacted"));- // 4. Suppressed while editing, so it cannot be saved back as message text.- assert.match(THREAD, /contextTruncation && showsNotice && !isEditing/);+ // 3. Drawn in the row slot, beside the message rather than inside it, so an edit cannot+ // save it back as message text.+ assert.match(THREAD, /<CompactionRowDivider \/>\s*\{body\}/);});test("an irreducible fit reports a diagnosis, and it is dropped once something fits", () => {