Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
feat(llm): show each thought in stream order inside the reply
Elian Doran669d9190bfa8
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.css b/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.cssindex 787362f3ff..0566945568 100644--- a/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.css+++ b/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.css@@ -204,20 +204,52 @@ body.background-effects.theme-supports-background-effects .llm-chat-message-userwidth: 100%;max-width: 100%;border-style: dashed;- margin-right: 0;- margin: 0;}-.llm-chat-thinking-card .expandable-section-summary {+.llm-chat-thinking-card .expandable-section-summary,+.llm-chat-thinking-header {color: var(--muted-text-color);}+.llm-chat-thinking-header {+ display: flex;+ align-items: center;+ gap: 0.4rem;+ padding: 0.5rem 0.75rem;+ font-weight: 500;++ > .bx {+ font-size: 1rem;+ }+}++.llm-chat-thinking-title {+ min-width: 0;+ overflow: hidden;+ text-overflow: ellipsis;+ white-space: nowrap;+}+.llm-chat-thinking-content {padding: 0.5rem 0.75rem;font-size: 0.9rem;color: var(--muted-text-color);}+/* Aligning the content to the end clips the overflow at the top, keeping the newest lines in view. */+.llm-chat-thinking-live .llm-chat-thinking-content {+ display: flex;+ flex-direction: column;+ justify-content: flex-end;+ max-height: calc(4 * 1.6em + 1rem);+ padding-top: 0;+ overflow: hidden;++ > * {+ flex-shrink: 0;+ }+}+/* Markdown styles */.llm-chat-markdown {line-height: 1.6;diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.spec.tsx b/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.spec.tsxindex 84cb40cdc7..c8b90a0bd4 100644--- a/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.spec.tsx+++ b/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.spec.tsx@@ -60,20 +60,57 @@ describe("ChatMessage footer", () => {});describe("ChatMessage thinking", () => {- it("renders the thought process as Markdown, as Codex writes its reasoning summaries", () => {- host = document.body.appendChild(document.createElement("div"));+ function renderMessage(content: StoredMessage["content"], options: { type?: StoredMessage["type"]; isStreaming?: boolean } = {}) {+ host ??= document.body.appendChild(document.createElement("div"));const target = host;- const message: StoredMessage = {- id: "t1", role: "assistant", type: "thinking", createdAt: "2026-01-01T00:00:00.000Z",- content: "**Retrieving PC hostname with command**\n\nI'll read `/etc/hostname`."- };- act(() => render(<ChatMessage message={message} />, target));+ const message: StoredMessage = { id: "m1", role: "assistant", type: options.type, createdAt: "2026-01-01T00:00:00.000Z", content };+ act(() => render(<ChatMessage message={message} isStreaming={options.isStreaming} />, target));+ return target;+ }++ it("folds each finished thought to its title, in stream order between the tool calls", () => {+ const target = renderMessage([+ { type: "thinking", content: "**Retrieving PC hostname with command**\n\nI'll read `/etc/hostname`." },+ { type: "tool_call", toolCall: { id: "c1", toolName: "shell", input: {}, result: "pc" } },+ { type: "thinking", content: "The file holds the name." },+ { type: "text", content: "Your PC is called pc." }+ ]);++ const blocks = [...(target.querySelector(".llm-chat-message-content")?.children ?? [])];+ expect(blocks.map(el => el.classList.contains("llm-chat-thinking-card"))).toEqual([true, false, true, false]);++ const [titled, untitled] = target.querySelectorAll(".llm-chat-thinking-card");+ const details = titled.querySelector("details");+ expect(details).not.toBeNull();+ expect(details?.open).toBe(false);+ expect(titled.querySelector(".expandable-section-label")?.textContent).toBe("Retrieving PC hostname with command");+ const body = titled.querySelector(".llm-chat-thinking-content .markdown-stub")?.textContent;+ expect(body).toContain("<code>/etc/hostname</code>");+ expect(body).not.toContain("Retrieving PC hostname");+ expect(untitled.querySelector(".expandable-section-label")?.textContent).toBe("llm_chat.thought_process");+ });++ it("shows the thought being generated under a spinner and its latest title, and folds it once the turn moves on", () => {+ const thought = { type: "thinking" as const, content: "**Reading the hostname**\n\nFirst.\n\n**Checking the network**\n\nSecond." };+ let target = renderMessage([thought], { isStreaming: true });+ let card = target.querySelector(".llm-chat-thinking-card");+ expect(card?.classList.contains("llm-chat-thinking-live")).toBe(true);+ expect(card?.querySelector("details")).toBeNull();+ expect(card?.querySelector(".bx-spin")).not.toBeNull();+ expect(card?.querySelector(".llm-chat-thinking-title")?.textContent).toBe("Checking the network");+ expect(card?.querySelector(".llm-chat-thinking-content .markdown-stub")?.textContent).toContain("Second.");++ target = renderMessage([thought, { type: "tool_call", toolCall: { id: "c1", toolName: "shell", input: {} } }], { isStreaming: true });+ card = target.querySelector(".llm-chat-thinking-card");+ expect(card?.classList.contains("llm-chat-thinking-live")).toBe(false);+ expect(card?.querySelector("details")?.open).toBe(false);+ expect(card?.querySelector(".expandable-section-label")?.textContent).toBe("Reading the hostname");+ });- const thinking = target.querySelector(".llm-chat-thinking-content");- expect(thinking?.textContent).toContain("Retrieving PC hostname with command");- const content = thinking?.querySelector(".markdown-stub");- expect(content).not.toBeNull();- expect(content?.textContent).toContain("<strong>Retrieving PC hostname with command</strong>");- expect(content?.textContent).toContain("<code>/etc/hostname</code>");+ it("renders a stored thinking message from before thoughts moved into the reply", () => {+ const target = renderMessage("**Retrieving PC hostname with command**\n\nI'll read `/etc/hostname`.", { type: "thinking" });+ const card = target.querySelector(".llm-chat-thinking-card");+ expect(card?.querySelector(".expandable-section-label")?.textContent).toBe("Retrieving PC hostname with command");+ expect(card?.querySelector(".llm-chat-thinking-content .markdown-stub")?.textContent).toContain("<code>/etc/hostname</code>");});});diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.tsx b/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.tsxindex 79a9e418b2..75c207f834 100644--- a/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.tsx+++ b/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.tsx@@ -9,12 +9,13 @@ import { t } from "../../../services/i18n.js";import utils from "../../../services/utils.js";import { ExtendedAdmonition } from "../../react/Admonition.js";import Button from "../../react/Button.js";+import LoadingSpinner from "../../react/LoadingSpinner.js";import { ReadOnlyTextContent } from "../text/ReadOnlyText.js";import { formatErrorDetails } from "./chat_error.js";import { renderMarkdown } from "./chat_markdown.js";import { renderQuoteSourceLinks } from "./chat_quote.js";import { ExpandableCard, ExpandableSection } from "./ExpandableCard.js";-import { type ContentBlock, type FileBlock, getMessageText, type ImageBlock, type StoredMessage, type TextBlock, type TextFileBlock, type ToolCallBlock } from "./llm_chat_types.js";+import { type ContentBlock, type FileBlock, getMessageText, type ImageBlock, type StoredMessage, type TextBlock, type TextFileBlock, type ThinkingBlock, type ToolCallBlock } from "./llm_chat_types.js";import { shortModelName } from "./model_name.js";import { SafeImage } from "./retry_image.js";import ToolCallCard from "./ToolCallCard.js";@@ -54,6 +55,7 @@ interface Props {type ContentGroup =| { type: "text"; block: TextBlock; index: number }+ | { type: "thinking"; block: ThinkingBlock; index: number }| { type: "tool_calls"; blocks: ToolCallBlock[]; index: number }| { type: "image"; block: ImageBlock; index: number }| { type: "file"; block: FileBlock; index: number }@@ -118,6 +120,38 @@ function CitationsSection({ citations }: { citations: LlmCitation[] }) {);}+/**+ * One stretch of the model's reasoning. A finished one folds to a line named by its leading+ * `**Title**`, the shape of Codex's reasoning summaries. The one being generated stays open,+ * clamped to its last lines under a spinner and its latest title.+ */+function ThinkingCard({ content, isLive }: { content: string; isLive?: boolean }) {+ if (isLive) {+ return (+ <ExpandableCard className="llm-chat-thinking-card llm-chat-thinking-live">+ <div className="llm-chat-thinking-header">+ <LoadingSpinner />+ <span className="llm-chat-thinking-title">{latestThinkingTitle(content) ?? t("llm_chat.thinking")}</span>+ </div>+ <div className="llm-chat-thinking-content">+ <TextBlockContent content={content} />+ </div>+ </ExpandableCard>+ );+ }++ const { title, body } = splitThinkingTitle(content);+ return (+ <ExpandableCard className="llm-chat-thinking-card">+ <ExpandableSection icon="bx bx-brain" label={title ?? t("llm_chat.thought_process")}>+ <div className="llm-chat-thinking-content">+ <TextBlockContent content={body} />+ </div>+ </ExpandableSection>+ </ExpandableCard>+ );+}+function ChatMessage({ message, isStreaming, onRetry }: Props) {const isError = message.type === "error";const isThinking = message.type === "thinking";@@ -144,17 +178,11 @@ function ChatMessage({ message, isStreaming, onRetry }: Props) {isThinking && "llm-chat-message-thinking"].filter(Boolean).join(" ");- // Render thinking messages in a collapsible card+ // Chats saved before reasoning moved into the reply hold it as a message of its own.if (isThinking) {return (<div className="llm-chat-message-wrapper llm-chat-message-wrapper-assistant">- <ExpandableCard className="llm-chat-thinking-card">- <ExpandableSection icon="bx bx-brain" label={t("llm_chat.thought_process")}>- <div className="llm-chat-thinking-content">- <TextBlockContent content={textContent} isStreaming={isStreaming} />- </div>- </ExpandableSection>- </ExpandableCard>+ <ThinkingCard content={textContent} /></div>);}@@ -260,6 +288,8 @@ function groupContentBlocks(blocks: ContentBlock[]): ContentGroup[] {} else {groups.push({ type: "tool_calls", blocks: [block], index: i });}+ } else if (block.type === "thinking") {+ groups.push({ type: "thinking", block, index: i });} else if (block.type === "image") {groups.push({ type: "image", block, index: i });} else if (block.type === "file") {@@ -276,8 +306,8 @@ function groupContentBlocks(blocks: ContentBlock[]): ContentGroup[] {function renderContentBlocks(blocks: ContentBlock[], isStreaming?: boolean) {return groupContentBlocks(blocks).map((group) => {+ const isLastBlock = group.index === blocks.length - 1;if (group.type === "text") {- const isLastBlock = group.index === blocks.length - 1;return (<div key={group.index}><TextBlockContent content={group.block.content} isStreaming={isStreaming && isLastBlock} />@@ -285,6 +315,10 @@ function renderContentBlocks(blocks: ContentBlock[], isStreaming?: boolean) {);}+ if (group.type === "thinking") {+ return <ThinkingCard key={group.index} content={group.block.content} isLive={isStreaming && isLastBlock} />;+ }+if (group.type === "image") {return (<a@@ -320,3 +354,17 @@ function renderContentBlocks(blocks: ContentBlock[], isStreaming?: boolean) {return <ToolCallCard key={group.index} toolCalls={group.blocks.map((b) => b.toolCall)} />;});}++/** Matches a line that is only bold text, the title Codex puts on each reasoning summary. */+const THINKING_TITLE_LINE = /^\*\*([^*\n]+)\*\*[ \t]*$/gm;++/** Split a thought's leading `**Title**` line from the rest, so the title can name the folded card. */+function splitThinkingTitle(content: string): { title?: string; body: string } {+ const match = /^\s*\*\*([^*\n]+)\*\*[ \t]*(?:\n|$)/.exec(content);+ return match ? { title: match[1].trim(), body: content.slice(match[0].length).trimStart() } : { body: content };+}++/** The last `**Title**` line of a thought, naming what the model is working on now. */+function latestThinkingTitle(content: string): string | undefined {+ return [...content.matchAll(THINKING_TITLE_LINE)].at(-1)?.[1].trim();+}diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ChatMessageList.spec.tsx b/apps/client/src/widgets/type_widgets/llm_chat/ChatMessageList.spec.tsxindex 1f33dd0e86..56b0ef15de 100644--- a/apps/client/src/widgets/type_widgets/llm_chat/ChatMessageList.spec.tsx+++ b/apps/client/src/widgets/type_widgets/llm_chat/ChatMessageList.spec.tsx@@ -31,7 +31,6 @@ function renderStreamingTurn(streamingBlocks: ContentBlock[], streamingStatus: smessages: [],isStreaming: true,streamingStatus,- streamingThinking: "",streamingBlocks,pendingCitations: [],retryLast: () => undefined,diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ChatMessageList.tsx b/apps/client/src/widgets/type_widgets/llm_chat/ChatMessageList.tsxindex 4dc5846449..e79e64bd81 100644--- a/apps/client/src/widgets/type_widgets/llm_chat/ChatMessageList.tsx+++ b/apps/client/src/widgets/type_widgets/llm_chat/ChatMessageList.tsx@@ -42,16 +42,8 @@ export default function ChatMessageList({ chat, emptyStateText, className }: Cha/>)), [messages, isStreaming, retryLast]);- // Stable placeholder objects: rebuilt only when their streamed content advances, so- // renders caused by anything else let memo(ChatMessage) skip the placeholders too.- const thinkingMessage = useMemo<StoredMessage | null>(() => chat.streamingThinking ? {- id: "streaming-thinking",- role: "assistant",- content: chat.streamingThinking,- createdAt: new Date().toISOString(),- type: "thinking"- } : null, [chat.streamingThinking]);-+ // A stable placeholder object: rebuilt only when its streamed content advances, so+ // renders caused by anything else let memo(ChatMessage) skip the placeholder too.const streamingMessage = useMemo<StoredMessage | null>(() => chat.streamingBlocks.length > 0 ? {id: "streaming",role: "assistant",@@ -67,7 +59,7 @@ export default function ChatMessageList({ chat, emptyStateText, className }: Cha<NoItems icon="bx bx-conversation" text={emptyStateText} />)}{storedMessages}- {isStreaming && !thinkingMessage && !streamingMessage && (+ {isStreaming && !streamingMessage && (<div className={`chat-stream-status ${chat.streamingStatus ? "" : "chat-stream-status-waiting"}`} role="status"><LoadingSpinner />{chat.streamingStatus@@ -75,12 +67,6 @@ export default function ChatMessageList({ chat, emptyStateText, className }: Cha: t("llm_chat.stream_status.waiting_for_reply")}</div>)}- {isStreaming && thinkingMessage && (- <ChatMessage- message={thinkingMessage}- isStreaming- />- )}{isStreaming && streamingMessage && (<ChatMessagemessage={streamingMessage}diff --git a/apps/client/src/widgets/type_widgets/llm_chat/chat_highlights_anchor.spec.ts b/apps/client/src/widgets/type_widgets/llm_chat/chat_highlights_anchor.spec.tsindex e993002c51..eafa0b3438 100644--- a/apps/client/src/widgets/type_widgets/llm_chat/chat_highlights_anchor.spec.ts+++ b/apps/client/src/widgets/type_widgets/llm_chat/chat_highlights_anchor.spec.ts@@ -47,10 +47,11 @@ describe("resolveAnchorIndices", () => {});describe("buildProseProjection", () => {- it("concatenates prose while skipping code, math, and non-markdown text", () => {+ it("concatenates prose while skipping code, math, non-markdown text and thoughts", () => {const root = contentRoot(`<p>Hello <code>skip()</code> world</p><pre>ignored block</pre><p>bye</p>`,- `<div class="tool-card">tool noise</div>`+ `<div class="tool-card">tool noise</div>` ++ `<div class="llm-chat-thinking-card"><div class="llm-chat-markdown"><p>thought</p></div></div>`);expect(buildProseProjection(root).text).toBe("Hello worldbye");});diff --git a/apps/client/src/widgets/type_widgets/llm_chat/chat_highlights_anchor.ts b/apps/client/src/widgets/type_widgets/llm_chat/chat_highlights_anchor.tsindex a2af6baa30..6fd47396ed 100644--- a/apps/client/src/widgets/type_widgets/llm_chat/chat_highlights_anchor.ts+++ b/apps/client/src/widgets/type_widgets/llm_chat/chat_highlights_anchor.ts@@ -13,8 +13,8 @@ import type { HighlightAnchor } from "./llm_chat_types.js";/** Only text inside rendered markdown counts as highlightable prose. */const PROSE_CONTAINER_SELECTOR = ".llm-chat-markdown";-/** Non-prose subtrees skipped within the markdown: code, math (async KaTeX), diagrams. */-const NON_PROSE_SELECTOR = "pre, code, .math-tex, .katex, .ck-math-tex, .mermaid, svg";+/** Non-prose subtrees skipped within the markdown: code, math (async KaTeX), diagrams, thoughts. */+const NON_PROSE_SELECTOR = "pre, code, .math-tex, .katex, .ck-math-tex, .mermaid, svg, .llm-chat-thinking-card";/** How much surrounding prose to store as disambiguating context on each side of a highlight. */const CONTEXT_LENGTH = 24;diff --git a/apps/client/src/widgets/type_widgets/llm_chat/chat_toc.spec.ts b/apps/client/src/widgets/type_widgets/llm_chat/chat_toc.spec.tsindex 1d5ce97d22..6cb1cd7b95 100644--- a/apps/client/src/widgets/type_widgets/llm_chat/chat_toc.spec.ts+++ b/apps/client/src/widgets/type_widgets/llm_chat/chat_toc.spec.ts@@ -275,6 +275,20 @@ describe("extractChatHeadings", () => {]);});+ it("skips headings inside a reply's thoughts", () => {+ const headings = extractChatHeadings([+ userMessage("u1", "Q"),+ assistantMessage("a1", "Answer")+ ], containerWith(+ messageWrapper("u1"),+ messageWrapper("a1", `<div class="llm-chat-thinking-card">${markdownContent("<h1>Thought</h1>")}</div>${markdownContent("<h1>Real</h1>")}`)+ ));+ expect(headings).toMatchObject([+ { id: "u1", level: 1 },+ { id: "a1:0", level: 2, text: "Real" }+ ]);+ });+it("skips error and thinking messages entirely", () => {const headings = extractChatHeadings([userMessage("u1", "Q"),diff --git a/apps/client/src/widgets/type_widgets/llm_chat/chat_toc.ts b/apps/client/src/widgets/type_widgets/llm_chat/chat_toc.tsindex 4499ae21f9..0ce93794dc 100644--- a/apps/client/src/widgets/type_widgets/llm_chat/chat_toc.ts+++ b/apps/client/src/widgets/type_widgets/llm_chat/chat_toc.ts@@ -187,6 +187,7 @@ export function extractChatHeadings(messages: StoredMessage[], container: HTMLElconst levelOffset = hasUserHeading ? 1 : 0;let index = 0;for (const headingEl of messageEl.querySelectorAll<HTMLHeadingElement>(REPLY_HEADING_SELECTOR)) {+ if (headingEl.closest(".llm-chat-thinking-card")) continue;headings.push({id: `${message.id}:${index}`,level: parseInt(headingEl.tagName.substring(1), 10) + levelOffset,diff --git a/apps/client/src/widgets/type_widgets/llm_chat/llm_chat_types.ts b/apps/client/src/widgets/type_widgets/llm_chat/llm_chat_types.tsindex 703e6b5a4e..c540e95141 100644--- a/apps/client/src/widgets/type_widgets/llm_chat/llm_chat_types.ts+++ b/apps/client/src/widgets/type_widgets/llm_chat/llm_chat_types.ts@@ -21,6 +21,15 @@ export interface TextBlock {content: string;}+/**+ * A stretch of the model's reasoning, kept in stream order among the reply's other blocks.+ * It is shown in the timeline but never sent back to the model.+ */+export interface ThinkingBlock {+ type: "thinking";+ content: string;+}+/** A tool invocation block shown inline in the message timeline. */export interface ToolCallBlock {type: "tool_call";@@ -71,7 +80,7 @@ export interface TextFileBlock {}/** An ordered content block in a chat message. */-export type ContentBlock = TextBlock | ToolCallBlock | ImageBlock | FileBlock | TextFileBlock;+export type ContentBlock = TextBlock | ThinkingBlock | ToolCallBlock | ImageBlock | FileBlock | TextFileBlock;/*** Extract the plain text from message content (works for both legacy string and block formats).@@ -138,7 +147,10 @@ export interface StoredMessage {content: string | ContentBlock[];createdAt: string;citations?: LlmCitation[];- /** Message type for special rendering. Defaults to "message" if omitted. */+ /**+ * Message type for special rendering. Defaults to "message" if omitted. `"thinking"` appears+ * only in chats saved before reasoning moved into {@link ThinkingBlock}s.+ */type?: MessageType;/*** For `type: "error"` messages, the failed provider call's context (HTTP status, URL,diff --git a/apps/client/src/widgets/type_widgets/llm_chat/useLlmChat.spec.tsx b/apps/client/src/widgets/type_widgets/llm_chat/useLlmChat.spec.tsxindex 24f0767807..54751cba7a 100644--- a/apps/client/src/widgets/type_widgets/llm_chat/useLlmChat.spec.tsx+++ b/apps/client/src/widgets/type_widgets/llm_chat/useLlmChat.spec.tsx@@ -302,6 +302,53 @@ describe("useLlmChat", () => {expect(api().getContent()).toMatchObject({ selectedModel: "mini", selectedProvider: "openai", selectedProviderId: "o_1" });});+ it("keeps thoughts in stream order inside the reply, and never sends them back", async () => {+ streamChatCompletionMock.mockImplementationOnce(async (_messages, _options, cb) => {+ cb.onThinking("**Reading** the ");+ cb.onThinking("hostname");+ cb.onToolUse("c1", "shell", {});+ cb.onToolResult("c1", "shell", "pc", false);+ cb.onThinking("Got it.");+ cb.onChunk("Your PC is pc.");+ cb.onDone();+ });+ await mountChat();+ await act(async () => {+ api().loadFromContent({+ version: 1,+ messages: [+ { id: "m1", role: "user", content: "hi", createdAt: "2026-01-01T00:00:00.000Z" },+ { id: "m2", role: "assistant", type: "thinking", content: "old thoughts", createdAt: "2026-01-01T00:00:01.000Z" },+ { id: "m3", role: "assistant", content: "hello", createdAt: "2026-01-01T00:00:02.000Z" }+ ]+ });+ });++ for (const text of ["name?", "thanks"]) {+ await act(async () => {+ api().setInput(text);+ });+ await act(async () => {+ await api().handleSubmit(new Event("submit"));+ });+ }++ expect(api().messages[4]).toMatchObject({+ role: "assistant",+ content: [+ { type: "thinking", content: "**Reading** the hostname" },+ { type: "tool_call", toolCall: { id: "c1", result: "pc" } },+ { type: "thinking", content: "Got it." },+ { type: "text", content: "Your PC is pc." }+ ]+ });+ expect(api().messages.filter(m => m.type === "thinking").map(m => m.id)).toEqual(["m2"]);++ const sent = (call: number) => (streamChatCompletionMock.mock.calls[call][0] as { content: unknown }[]).map(m => m.content);+ expect(sent(0)).toEqual(["hi", "hello", "name?"]);+ expect(sent(1)).toEqual(["hi", "hello", "name?", "Your PC is pc.", "thanks"]);+ });+it("keeps what a turn waits on until the turn ends", async () => {let callbacks: { onStatus: (status: string) => void } | undefined;let finish: () => void = () => undefined;diff --git a/apps/client/src/widgets/type_widgets/llm_chat/useLlmChat.ts b/apps/client/src/widgets/type_widgets/llm_chat/useLlmChat.tsindex d05870fc3c..af003a5c67 100644--- a/apps/client/src/widgets/type_widgets/llm_chat/useLlmChat.ts+++ b/apps/client/src/widgets/type_widgets/llm_chat/useLlmChat.ts@@ -40,7 +40,7 @@ function getLastUserMessageEl(container: HTMLElement): HTMLElement | null {/*** Flatten a stored message's content into the wire format the server expects.* Plain string content stays as-is; block-shaped content (with images) becomes- * an ordered array of text/image parts, with tool-call blocks stripped.+ * an ordered array of text/image parts, with tool-call and thinking blocks stripped.*/function flattenToApiContent(content: string | ContentBlock[]): string | LlmMessagePart[] {if (typeof content === "string") {@@ -59,7 +59,8 @@ function flattenToApiContent(content: string | ContentBlock[]): string | LlmMess}// tool_call blocks belong to assistant history rendering only — they// are reconstructed from the model's own tool-use turns and must not- // be re-sent as user/assistant content.+ // be re-sent as user/assistant content. thinking blocks are the model's+ // reasoning, which it must not read back as something it said.}// Collapse to a string if there is no multimodal content — keeps backwards// compatibility with providers/paths that haven't been touched.@@ -94,7 +95,6 @@ export interface UseLlmChatReturn {hasInputText: boolean;isStreaming: boolean;streamingBlocks: ContentBlock[];- streamingThinking: string;/** What the streaming turn waits on before its reply starts, if the server said. */streamingStatus: LlmStreamStatus | null;pendingCitations: LlmCitation[];@@ -201,7 +201,6 @@ export function useLlmChat(// displayed `streamingBlocks` is derived from this with the trailing text// block smoothed via useSmoothStreaming for a steady reveal cadence.const [targetBlocks, setTargetBlocks] = useState<ContentBlock[]>([]);- const [streamingThinking, setStreamingThinking] = useState("");const [streamingStatus, setStreamingStatus] = useState<LlmStreamStatus | null>(null);const { displayedText: smoothedTailText, append: smoothAppend, drain: smoothDrain, reset: smoothReset } = useSmoothStreaming();const [pendingCitations, setPendingCitations] = useState<LlmCitation[]>([]);@@ -557,11 +556,9 @@ export function useLlmChat(setMessagesInternal(conversation);setIsStreaming(true);setTargetBlocks([]);- setStreamingThinking("");setStreamingStatus(null);smoothReset();- let thinkingContent = "";const contentBlocks: ContentBlock[] = [];const citations: LlmCitation[] = [];let usage: LlmUsage | undefined;@@ -577,7 +574,7 @@ export function useLlmChat(return block as ContentBlock & { type: "text" };}- const apiMessages: LlmMessage[] = trimToFirstUserMessage(conversation).map(m => ({+ const apiMessages: LlmMessage[] = trimToFirstUserMessage(conversation.filter(m => m.type !== "thinking")).map(m => ({role: m.role,content: stripQuoteSourcesFromApiContent(flattenToApiContent(m.content))}));@@ -634,35 +631,18 @@ export function useLlmChat(}}- const finalNewMessages: StoredMessage[] = [];-- if (thinkingContent) {- finalNewMessages.push({- id: randomString(),- role: "assistant",- content: thinkingContent,- createdAt: new Date().toISOString(),- type: "thinking"- });- }-if (contentBlocks.length > 0) {- finalNewMessages.push({+ setMessages([...conversation, {id: randomString(),role: "assistant",content: contentBlocks,createdAt: new Date().toISOString(),citations: citations.length > 0 ? citations : undefined,usage- });- }-- if (finalNewMessages.length > 0) {- setMessages([...conversation, ...finalNewMessages]);+ }]);}setTargetBlocks([]);- setStreamingThinking("");setPendingCitations([]);setStreamingStatus(null);setIsStreaming(false);@@ -689,8 +669,16 @@ export function useLlmChat(setTargetBlocks([...contentBlocks]);},onThinking: (text) => {- thinkingContent += text;- setStreamingThinking(thinkingContent);+ const last = contentBlocks[contentBlocks.length - 1];+ if (last?.type === "thinking") {+ contentBlocks[contentBlocks.length - 1] = { type: "thinking", content: last.content + text };+ } else {+ // The smoother animates only a trailing text block, so reveal the+ // text before this thought in full.+ smoothDrain();+ contentBlocks.push({ type: "thinking", content: text });+ }+ setTargetBlocks([...contentBlocks]);},onToolInputStart: (toolCallId, toolName) => {// Snap any pending smoothed text to its full value before@@ -793,7 +781,6 @@ export function useLlmChat(setMessages(finalMessages);smoothReset();setTargetBlocks([]);- setStreamingThinking("");setStreamingStatus(null);setIsStreaming(false);},@@ -822,7 +809,6 @@ export function useLlmChat(}]);smoothReset();setTargetBlocks([]);- setStreamingThinking("");setStreamingStatus(null);setIsStreaming(false);abortControllerRef.current = null;@@ -928,7 +914,6 @@ export function useLlmChat(hasInputText,isStreaming,streamingBlocks,- streamingThinking,streamingStatus,pendingCitations,pendingAttachments,