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.

Studio: let an agent reply's round tail leave from the end of the list (#17501)

Daniel Hanf2572a6770d3
diff --git a/studio/frontend/smoke-thread-weight-main.tsx b/studio/frontend/smoke-thread-weight-main.tsx
index b5fe7f9688d..9b70c839ab3 100644
--- a/studio/frontend/smoke-thread-weight-main.tsx
+++ b/studio/frontend/smoke-thread-weight-main.tsx
@@ -16,6 +16,7 @@ import {
type ChatModelAdapter,
ExportedMessageRepository,
SimpleImageAttachmentAdapter,
+ type ThreadAssistantMessagePart,
type ThreadMessageLike,
useAui,
unstable_useRemoteThreadListRuntime,
@@ -239,9 +240,36 @@ const STREAM_CHUNKS = Number(
// `&sources=1`: the streamed reply follows a web_search call with six URL sources.
const STREAM_SOURCES =
new URLSearchParams(window.location.search).get("sources") === "1";
+// `&rounds=N`: N agent rounds (reasoning, a call, its result, text with math) stream before the reply.
+const STREAM_ROUNDS = Number(
+ new URLSearchParams(window.location.search).get("rounds") ?? "0",
+);
+const tick = () => new Promise((resolve) => setTimeout(resolve, 20));
const NEVER_RUNS: ChatModelAdapter = STREAM_CHUNKS > 0
? {
async *run({ abortSignal }) {
+ const rounds: ThreadAssistantMessagePart[] = [];
+ for (let round = 0; round < STREAM_ROUNDS; round++) {
+ const callArgs = { url: `https://example.com/${round}` };
+ const call = {
+ type: "tool-call" as const,
+ toolCallId: `call_round_${round}`,
+ toolName: "browser_snapshot",
+ args: callArgs,
+ argsText: JSON.stringify(callArgs),
+ };
+ for (const step of [
+ () => rounds.push({ type: "reasoning", text: `Planning round ${round}.` }),
+ () => rounds.push(call),
+ () => (rounds[rounds.length - 1] = { ...call, result: `Snapshot ${round}. ${PROSE}` }),
+ () => rounds.push({ type: "text", text: `Round ${round} gave $x_{${round}}^2$.` }),
+ ]) {
+ step();
+ await tick();
+ if (abortSignal.aborted) return;
+ yield { content: [...rounds] };
+ }
+ }
const args = { query: "live reference" };
const prefix = STREAM_SOURCES
? [
@@ -258,10 +286,10 @@ const NEVER_RUNS: ChatModelAdapter = STREAM_CHUNKS > 0
: [];
let text = "";
for (let chunk = 0; chunk < STREAM_CHUNKS; chunk++) {
- await new Promise((resolve) => setTimeout(resolve, 20));
+ await tick();
if (abortSignal.aborted) return;
text += `word${chunk} `;
- yield { content: [...prefix, { type: "text" as const, text }] };
+ yield { content: [...rounds, ...prefix, { type: "text" as const, text }] };
}
},
}
diff --git a/studio/frontend/src/components/assistant-ui/thread.tsx b/studio/frontend/src/components/assistant-ui/thread.tsx
index 374c5583552..2b7c0de5ce1 100644
--- a/studio/frontend/src/components/assistant-ui/thread.tsx
+++ b/studio/frontend/src/components/assistant-ui/thread.tsx
@@ -249,10 +249,10 @@ import {
} from "@/features/chat/external-providers";
import { toolStatusKind } from "@/features/chat/utils/tool-status";
import {
- betweenToolRounds,
formatPrefillDetails,
formatPrefillTimeLeft,
prefillPercent,
+ runningReplyTail,
} from "@/features/chat";
import { replySourceMarkdown } from "@/features/chat/utils/reply-source-markdown";
import { toolResultModelText } from "@/features/chat/api/chat-adapter";
@@ -461,6 +461,7 @@ import {
ComposerPrimitive,
ErrorPrimitive,
MessagePrimitive,
+ TextMessagePartProvider,
ThreadPrimitive,
useAui,
useAuiEvent,
@@ -8054,18 +8055,28 @@ const GeneratingIndicator: FC = () => {
return <GeneratingStatus />;
};
-// GeneratingIndicator's status for the gap after a tool result, until the next pass streams its first part.
-const NextRoundIndicator: FC = () => {
- const show = useAuiState(({ message }) =>
- betweenToolRounds(message.status?.type === "running", message.content),
+// assistant-ui's empty text part (fallback off) and the next-round status, in a box owned here so React removes
+// it before appending the next part: a mid-list removal restyles the whole reply (MathML UA :nth-child rules).
+const PartsWithRoundTail: FC<{ children: ReactNode }> = ({ children }) => {
+ const tail = useAuiState(({ message }) =>
+ runningReplyTail(message.status?.type === "running", message.content),
);
- if (!show) {
- return null;
- }
return (
- <div data-slot="next-round-indicator" className="mt-2">
- <GeneratingStatus />
- </div>
+ <>
+ {children}
+ {tail !== 0 && (
+ <div data-slot="round-tail" className="contents">
+ <TextMessagePartProvider text="" isRunning={true}>
+ <MarkdownText />
+ </TextMessagePartProvider>
+ {tail === 2 && (
+ <div data-slot="next-round-indicator" className="mt-2">
+ <GeneratingStatus />
+ </div>
+ )}
+ </div>
+ )}
+ </>
);
};
@@ -8681,8 +8692,12 @@ const AssistantMessage: FC = () => {
<CancelledIndicator />
<DiffusionCanvas />
- <MessagePrimitive.Parts components={ASSISTANT_PART_COMPONENTS} />
- <NextRoundIndicator />
+ <PartsWithRoundTail>
+ <MessagePrimitive.Parts
+ components={ASSISTANT_PART_COMPONENTS}
+ unstable_showEmptyOnNonTextEnd={false}
+ />
+ </PartsWithRoundTail>
<SourcesGroup />
<MessageHtmlArtifacts />
<ContinueMessageBar />
diff --git a/studio/frontend/src/features/chat/index.ts b/studio/frontend/src/features/chat/index.ts
index 738dd4d8674..3ae907555e4 100644
--- a/studio/frontend/src/features/chat/index.ts
+++ b/studio/frontend/src/features/chat/index.ts
@@ -94,6 +94,7 @@ export { describePlacement, offloadCountsFrom, offloadWarning } from "./lib/part
export type { PlacementPool } from "./lib/partial-offload";
export {
betweenToolRounds,
+ runningReplyTail,
formatLiveDecode,
formatPrefillDetails,
formatPrefillTimeLeft,
diff --git a/studio/frontend/src/features/chat/lib/generation-progress.ts b/studio/frontend/src/features/chat/lib/generation-progress.ts
index 73fb70b378d..52ca106b9b8 100644
--- a/studio/frontend/src/features/chat/lib/generation-progress.ts
+++ b/studio/frontend/src/features/chat/lib/generation-progress.ts
@@ -32,6 +32,17 @@ export function betweenToolRounds(
return false;
}
+/** After a running reply's parts: 0 nothing, 1 assistant-ui's empty text part, 2 that and the next-round status. */
+export function runningReplyTail(
+ running: boolean,
+ content: readonly { type: string; result?: unknown }[],
+): 0 | 1 | 2 {
+ if (!running || content.length === 0) return 0;
+ const last = content[content.length - 1].type;
+ if (last === "text" || last === "reasoning") return 0;
+ return betweenToolRounds(running, content) ? 2 : 1;
+}
+
export type GenerationProgressUpdate =
| { phase: "prefill"; prefill: LivePrefillProgress }
| { phase: "decode"; decode: LiveDecodeProgress }
diff --git a/studio/frontend/src/index.css b/studio/frontend/src/index.css
index 41d64d7af8d..829fd0e4698 100644
--- a/studio/frontend/src/index.css
+++ b/studio/frontend/src/index.css
@@ -4529,6 +4529,17 @@ html[data-chat-font] .aui-root {
margin-top: 0;
}
+ /* thread.tsx PartsWithRoundTail is display:contents: the two rules above and the trailing reset, one level down. */
+ :where(.aui-assistant-message-content > [data-slot="round-tail"] > *) {
+ margin-top: calc(1rem * var(--ui-space-scale, 1));
+ }
+ :where(.aui-assistant-message-content > .h-0 + [data-slot="round-tail"] > *) {
+ margin-top: 0;
+ }
+ .aui-assistant-message-content > [data-slot="round-tail"]:last-child > *:last-child > *:last-child > *:last-child > *:last-child > *:last-child > *:last-child {
+ margin-bottom: 0 !important;
+ }
+
/* Margin collapsing keeps the gap before a following block, so this only affects a trailing code
block, matching a text-trailing message. */
.aui-assistant-message-content [data-streamdown="code-block"] {
diff --git a/studio/frontend/tests/generation-details.test.ts b/studio/frontend/tests/generation-details.test.ts
index e81482e35a8..3357ffe5458 100644
--- a/studio/frontend/tests/generation-details.test.ts
+++ b/studio/frontend/tests/generation-details.test.ts
@@ -243,11 +243,12 @@ test("a tool result drops the last pass's figures, live or followed, so the next
test("the status line comes back between tool rounds, after the finished call", () => {
const thread = readSrc("components/assistant-ui/thread.tsx");
- const indicator = thread.slice(thread.indexOf("const NextRoundIndicator"), thread.indexOf("// Mounted only under a running reply"));
- assert.match(indicator, /betweenToolRounds\(message\.status\?\.type === "running", message\.content\)/);
- assert.match(indicator, /<GeneratingStatus \/>/);
+ const indicator = thread.slice(thread.indexOf("const PartsWithRoundTail"), thread.indexOf("// Mounted only under a running reply"));
+ assert.match(indicator, /runningReplyTail\(message\.status\?\.type === "running", message\.content\)/);
+ assert.match(indicator, /tail === 2 && \(\s*<div data-slot="next-round-indicator" className="mt-2">\s*<GeneratingStatus \/>/);
// After the parts, so it sits under the call it follows rather than above the reply.
- assert.match(thread, /<MessagePrimitive\.Parts components=\{ASSISTANT_PART_COMPONENTS\} \/>\s*<NextRoundIndicator \/>/);
+ assert.match(indicator, /\{children\}\s*\{tail !== 0 && \(/);
+ assert.match(thread, /<PartsWithRoundTail>\s*<MessagePrimitive\.Parts\s+components=\{ASSISTANT_PART_COMPONENTS\}/);
});
test("a finished call shows the next round's status, past the sources a recovered reply ends with", () => {
diff --git a/studio/frontend/tests/round-tail-end-of-list.test.ts b/studio/frontend/tests/round-tail-end-of-list.test.ts
new file mode 100644
index 00000000000..34710cca20d
--- /dev/null
+++ b/studio/frontend/tests/round-tail-end-of-list.test.ts
@@ -0,0 +1,86 @@
+// 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 assert from "node:assert/strict";
+import { readFileSync } from "node:fs";
+import { createRequire } from "node:module";
+import { join, sep } from "node:path";
+import test from "node:test";
+
+import { readSrc, registerBundlerResolver } from "./helpers/kit.ts";
+
+registerBundlerResolver();
+const { betweenToolRounds, runningReplyTail } = await import(
+ "../src/features/chat/lib/generation-progress.ts"
+);
+
+const done = { type: "tool-call", result: "ok" };
+const pending = { type: "tool-call" };
+const source = { type: "source" };
+const text = { type: "text" };
+const reasoning = { type: "reasoning" };
+
+test("the tail is the empty text part after any non-text end, plus the status once a call finished", () => {
+ assert.equal(runningReplyTail(true, [reasoning, pending]), 1, "a call still running: empty part only");
+ assert.equal(runningReplyTail(true, [reasoning, done]), 2, "a finished call: empty part and status");
+ assert.equal(runningReplyTail(true, [done, source, source]), 2, "sources after the call");
+ assert.equal(runningReplyTail(true, [pending, source]), 1);
+ assert.equal(runningReplyTail(true, [source]), 1);
+ assert.equal(runningReplyTail(true, [{ type: "image" }]), 1);
+ assert.equal(runningReplyTail(true, [done, text]), 0, "the next pass streamed text");
+ assert.equal(runningReplyTail(true, [done, reasoning]), 0, "the next pass streamed reasoning");
+ assert.equal(runningReplyTail(true, []), 0, "GeneratingIndicator covers an empty reply");
+ assert.equal(runningReplyTail(false, [done]), 0, "a finished reply has no tail");
+ for (const parts of [[done], [pending], [done, source], [source], [done, text], [reasoning], []]) {
+ for (const running of [true, false]) {
+ assert.equal(
+ runningReplyTail(running, parts) === 2,
+ betweenToolRounds(running, parts),
+ "the status shows exactly when betweenToolRounds says so",
+ );
+ }
+ }
+});
+
+test("assistant-ui's empty part fallback still matches the replica the tail renders", () => {
+ const require = createRequire(import.meta.url);
+ const entry = require.resolve("@assistant-ui/core");
+ const dist = entry.slice(0, entry.lastIndexOf(`${sep}dist${sep}`) + 6);
+ const parts = readFileSync(join(dist, "react/primitives/message/MessageParts.js"), "utf8");
+ assert.match(parts, /return lastPart\?\.type !== "text" && lastPart\?\.type !== "reasoning";/);
+ assert.match(parts, /if \(status\.type !== "running"\)\s*return null;/);
+ assert.match(parts, /_jsx\(TextMessagePartProvider, \{ text: "", isRunning: status\.type === "running", children: _jsx\(Component,/);
+ assert.match(parts, /_jsx\(ConditionalEmpty, \{ components: components, enabled: unstable_showEmptyOnNonTextEnd \}\)/);
+});
+
+test("both tail nodes sit in one display:contents box that is a direct sibling of the parts", () => {
+ const thread = readSrc("components/assistant-ui/thread.tsx");
+ const tail = thread.slice(
+ thread.indexOf("const PartsWithRoundTail"),
+ thread.indexOf("// Mounted only under a running reply"),
+ );
+ // Only as a direct child of the component owning {children} is it removed before the next part lands.
+ assert.match(
+ tail,
+ /return \(\s*<>\s*\{children\}\s*\{tail !== 0 && \(\s*<div data-slot="round-tail" className="contents">\s*<TextMessagePartProvider text="" isRunning=\{true\}>\s*<MarkdownText \/>\s*<\/TextMessagePartProvider>\s*\{tail === 2 && \(\s*<div data-slot="next-round-indicator" className="mt-2">/,
+ );
+ assert.match(
+ thread,
+ /<PartsWithRoundTail>\s*<MessagePrimitive\.Parts\s+components=\{ASSISTANT_PART_COMPONENTS\}\s+unstable_showEmptyOnNonTextEnd=\{false\}\s*\/>\s*<\/PartsWithRoundTail>/,
+ );
+ assert.equal(thread.match(/<MessagePrimitive\.Parts components=\{ASSISTANT_PART_COMPONENTS\}/g), null);
+ assert.equal(thread.includes("NextRoundIndicator"), false);
+});
+
+test("the tail's children keep the reply's spacing rules through the contents box", () => {
+ const css = readSrc("index.css");
+ for (const rule of [
+ ':where(.aui-assistant-message-content > [data-slot="round-tail"] > *) {\n\t\tmargin-top: calc(1rem * var(--ui-space-scale, 1));',
+ ':where(.aui-assistant-message-content > .h-0 + [data-slot="round-tail"] > *) {\n\t\tmargin-top: 0;',
+ '.aui-assistant-message-content > [data-slot="round-tail"]:last-child > *:last-child > *:last-child > *:last-child > *:last-child > *:last-child > *:last-child {\n\t\tmargin-bottom: 0 !important;',
+ ]) {
+ assert.ok(css.includes(rule), rule);
+ }
+ assert.ok(css.includes(":where(.aui-assistant-message-content > *):where(:not(:first-child)) {\n\t\tmargin-top: calc(1rem * var(--ui-space-scale, 1));"));
+ assert.ok(css.includes(".aui-assistant-message-content > *:last-child > *:last-child > *:last-child > *:last-child > *:last-child > *:last-child {"));
+});
diff --git a/studio/frontend/tests/thread-part-components-stable.test.ts b/studio/frontend/tests/thread-part-components-stable.test.ts
index 5305271c7a6..ead98833608 100644
--- a/studio/frontend/tests/thread-part-components-stable.test.ts
+++ b/studio/frontend/tests/thread-part-components-stable.test.ts
@@ -36,7 +36,7 @@ test("the assistant part components are a single module-scope object", () => {
);
assert.match(
THREAD_SOURCE,
- /<MessagePrimitive\.Parts components=\{ASSISTANT_PART_COMPONENTS\} \/>/,
+ /<MessagePrimitive\.Parts\s+components=\{ASSISTANT_PART_COMPONENTS\}\s+unstable_showEmptyOnNonTextEnd=\{false\}\s*\/>/,
"MessagePrimitive.Parts must be given the hoisted constant by name",
);
});
diff --git a/tests/studio/playwright_long_chat_renders.py b/tests/studio/playwright_long_chat_renders.py
index 0ebef7477bc..f2a027ca025 100644
--- a/tests/studio/playwright_long_chat_renders.py
+++ b/tests/studio/playwright_long_chat_renders.py
@@ -131,6 +131,47 @@ def check_reply_paint(page) -> None:
), "wide reply lost its horizontal scroll range"
+ROUNDS = 12
+# With KaTeX's MathML on the page, each removal from the middle of the reply restyles all of it.
+CHILD_LIST_WATCH = r"""
+() => {
+ window.__tail = {midRemovals: 0, statuses: 0};
+ const seen = new WeakSet();
+ const watch = () => document.querySelectorAll('.aui-assistant-message-content').forEach((el) => {
+ if (seen.has(el)) return;
+ seen.add(el);
+ new MutationObserver((records) => records.forEach((r) => {
+ if (r.target === el && r.nextSibling) window.__tail.midRemovals += r.removedNodes.length;
+ r.addedNodes.forEach((n) => {
+ if (n.nodeType === 1 && (n.matches('[data-slot="next-round-indicator"]') || n.querySelector('[data-slot="next-round-indicator"]'))) window.__tail.statuses++;
+ });
+ })).observe(el, {childList: true, subtree: true});
+ });
+ new MutationObserver(watch).observe(document.body, {childList: true, subtree: true});
+}
+"""
+
+
+def check_round_tail(page) -> dict[str, int]:
+ page.goto(f"{BASE}/smoke-thread-weight.html?stream=5&rounds={ROUNDS}")
+ page.wait_for_function("() => window.__threadWeight !== undefined", timeout = 120_000)
+ page.evaluate(CHILD_LIST_WATCH)
+ page.evaluate("() => window.__threadWeight.send('go')")
+ page.wait_for_function("() => window.__threadWeight.isRunning()", timeout = 60_000)
+ page.wait_for_function("() => !window.__threadWeight.isRunning()", timeout = 120_000, polling = 100)
+ tail = page.evaluate("() => window.__tail")
+ text = page.evaluate("() => window.__threadWeight.lastAssistantMessage()?.innerText ?? ''")
+ assert "word4" in text and f"Round {ROUNDS - 1}" in text, "the agent stream did not finish"
+ assert page.locator(".katex-mathml").count() >= ROUNDS, "no KaTeX MathML on the page"
+ assert tail["statuses"] >= ROUNDS - 1, f"the next-round status showed {tail['statuses']} times"
+ # One for the first "Working..." status, which the first part replaces.
+ assert tail["midRemovals"] <= 1, (
+ f"{tail['midRemovals']} nodes left the middle of the reply over {ROUNDS} tool rounds: "
+ f"a round's tail is being removed after the next part is appended"
+ )
+ return tail
+
+
def main() -> int:
proc = None
try:
@@ -152,11 +193,21 @@ def main() -> int:
page.on("pageerror", lambda error: errors.append(str(error)))
counts = run(page)
check_reply_paint(page)
+ tail = check_round_tail(context.new_page())
finally:
browser.close()
badges = counts.get("SourceBadge", 0)
cards = counts.get("PythonToolUIImpl", 0) + counts.get("TerminalToolUIImpl", 0)
- info(json.dumps({"SourceBadge": badges, "python/terminal cards": cards, "chunks": CHUNKS}))
+ info(
+ json.dumps(
+ {
+ "SourceBadge": badges,
+ "python/terminal cards": cards,
+ "chunks": CHUNKS,
+ "round_tail": tail,
+ }
+ )
+ )
assert not errors, errors
# The live reply's six badges may render a couple of times (mount, status change), not per token.
assert badges <= 2 * 6, (