Skip to content

Commit 33057f4

Browse files
committed
fix(control-ui): preserve reasoning in live stream rendering
1 parent 43b9493 commit 33057f4

3 files changed

Lines changed: 65 additions & 6 deletions

File tree

‎ui/src/ui/chat/grouped-render.test.ts‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import { html, render } from "lit";
44
import { afterEach, describe, expect, it, vi } from "vitest";
55
import type { MessageGroup } from "../types/chat-types.ts";
6+
import { buildChatItems } from "./build-chat-items.ts";
67
import {
78
formatChatTimestampForDisplay,
89
renderMessageGroup,
@@ -1668,4 +1669,50 @@ describe("grouped chat rendering", () => {
16681669
text: "",
16691670
});
16701671
});
1672+
1673+
it("preserves reasoning for structured streaming messages and keeps plain text fallback", () => {
1674+
const container = document.createElement("div");
1675+
1676+
render(
1677+
renderStreamingGroup(
1678+
"Structured reply",
1679+
1,
1680+
undefined,
1681+
{ name: "OpenClaw", avatar: null },
1682+
undefined,
1683+
null,
1684+
{
1685+
role: "assistant",
1686+
content: [
1687+
{ type: "thinking", thinking: "Plan A" },
1688+
{ type: "text", text: "Structured reply" },
1689+
],
1690+
timestamp: 1,
1691+
},
1692+
true,
1693+
),
1694+
container,
1695+
);
1696+
1697+
expect(container.querySelector(".chat-thinking")).not.toBeNull();
1698+
expect(container.textContent).toContain("Plan A");
1699+
expect(container.textContent).toContain("Structured reply");
1700+
1701+
render(
1702+
renderStreamingGroup(
1703+
"Plain fallback",
1704+
2,
1705+
undefined,
1706+
{ name: "OpenClaw", avatar: null },
1707+
undefined,
1708+
null,
1709+
undefined,
1710+
true,
1711+
),
1712+
container,
1713+
);
1714+
1715+
expect(container.querySelector(".chat-thinking")).toBeNull();
1716+
expect(container.textContent).toContain("Plain fallback");
1717+
});
16711718
});

‎ui/src/ui/chat/grouped-render.ts‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -339,21 +339,31 @@ export function renderStreamingGroup(
339339
assistant?: AssistantIdentity,
340340
basePath?: string,
341341
authToken?: string | null,
342+
message?: unknown,
343+
showReasoning = false,
342344
) {
343345
const name = assistant?.name ?? "Assistant";
346+
const streamingMessage =
347+
message && typeof message === "object"
348+
? {
349+
role: "assistant",
350+
timestamp: startedAt,
351+
...(message as Record<string, unknown>),
352+
}
353+
: {
354+
role: "assistant",
355+
content: [{ type: "text", text }],
356+
timestamp: startedAt,
357+
};
344358

345359
return html`
346360
<div class="chat-group assistant">
347361
${renderChatAvatar("assistant", assistant, undefined, basePath, authToken)}
348362
<div class="chat-group-messages">
349363
${renderGroupedMessage(
350-
{
351-
role: "assistant",
352-
content: [{ type: "text", text }],
353-
timestamp: startedAt,
354-
},
364+
streamingMessage,
355365
`stream:${startedAt}`,
356-
{ isStreaming: true, showReasoning: false },
366+
{ isStreaming: true, showReasoning },
357367
onOpenSidebar,
358368
)}
359369
<div class="chat-group-footer">

‎ui/src/ui/views/chat.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -964,6 +964,8 @@ export function renderChat(props: ChatProps) {
964964
assistantIdentity,
965965
props.basePath,
966966
props.assistantAttachmentAuthToken ?? null,
967+
"message" in item ? item.message : undefined,
968+
showReasoning,
967969
);
968970
}
969971
if (item.kind === "group") {

0 commit comments

Comments
 (0)