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
fix(llm): stop a folded thought from repeating its first line
Elian Doranb3e3d58bfd6f
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.css b/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.cssindex be888b06be..d3aedad992 100644--- a/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.css+++ b/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.css@@ -205,20 +205,30 @@ body.background-effects.theme-supports-background-effects .llm-chat-message-userfont-size: 0.85rem;color: var(--muted-text-color);+ /* Top alignment with a shared line height keeps the icons on the first line of the label,+ whether it wraps or not. */.expandable-section-summary,.llm-chat-thinking-header {display: flex;- align-items: center;+ align-items: flex-start;gap: 0.4rem;width: fit-content;max-width: 100%;padding: 0.15rem 0;font-weight: normal;+ line-height: 1.4rem;> .bx {font-size: 1rem;+ line-height: inherit;margin-right: 0;}++ /* The brain and the live spinner share a size, so a thought keeps its place when it stops streaming. */+ > .bx-brain,+ > .bx-loader {+ font-size: 0.9rem;+ }}.expandable-section-summary:hover {@@ -228,6 +238,16 @@ body.background-effects.theme-supports-background-effects .llm-chat-message-user.expandable-section-chevron {margin-left: 0;}++ /* `text-overflow` needs a block container: as a flex container the label never shows the ellipsis. */+ .expandable-section-label {+ display: block;+ }++ /* The label is left out of the body, so the opened thought shows it in full. */+ &[open] > .expandable-section-summary .expandable-section-label {+ white-space: normal;+ }}.llm-chat-thinking-title {@@ -237,10 +257,10 @@ body.background-effects.theme-supports-background-effects .llm-chat-message-userwhite-space: nowrap;}-/* The left margin centers the rule under the 1rem icon of the line above. */+/* The left margin centers the rule under the 0.9rem icon of the line above. */.llm-chat-thinking-content {- margin: 0.15rem 0 0.25rem 0.5rem;- padding-left: 0.9rem;+ margin: 0.15rem 0 0.25rem 0.45rem;+ padding-left: 0.85rem;border-left: 2px solid var(--main-border-color);}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 723b7fb8e7..f444ad0ac1 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@@ -86,6 +86,16 @@ describe("ChatMessage thinking", () => {expect(body).toContain("<code>/etc/hostname</code>");expect(body).not.toContain("Retrieving PC hostname");expect(untitled.querySelector(".expandable-section-label")?.textContent).toBe("The file holds the name.");+ const untitledBody = untitled.querySelector(".llm-chat-thinking-content .markdown-stub")?.textContent;+ expect(untitledBody).toContain("Nothing else to check.");+ expect(untitledBody).not.toContain("holds");+ });++ it("leaves a one-line thought without a body, since its label already says it all", () => {+ const target = renderMessage([{ type: "thinking", content: "I should look up who maintains it." }]);+ const thought = target.querySelector(".llm-chat-thinking");+ expect(thought?.querySelector(".expandable-section-label")?.textContent).toBe("I should look up who maintains it.");+ expect(thought?.querySelector(".llm-chat-thinking-content")).toBeNull();});it("shows the thought being generated under a spinner and its latest title, and folds it once the turn moves on", () => {diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.tsx b/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.tsxindex 484c8bcc18..a198121459 100644--- a/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.tsx+++ b/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.tsx@@ -122,8 +122,9 @@ function CitationsSection({ citations }: { citations: LlmCitation[] }) {/*** One stretch of the model's reasoning. A finished one folds to a single muted line: its leading- * `**Title**` (the shape of Codex's reasoning summaries), or else its first line. The one being- * generated stays open, clamped to its last lines under a spinner and its latest title.+ * `**Title**` (the shape of Codex's reasoning summaries), or else its first line, which the body+ * then leaves out. 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) {@@ -140,12 +141,14 @@ function ThinkingCard({ content, isLive }: { content: string; isLive?: boolean });}- const { title, body } = splitThinkingTitle(content);+ const { label, body } = splitThinkingLabel(content);return (- <ExpandableSection className="llm-chat-thinking" icon="bx bx-brain" label={title ?? firstLinePlainText(content) ?? t("llm_chat.thought_process")}>- <div className="llm-chat-thinking-content">- <TextBlockContent content={body} />- </div>+ <ExpandableSection className="llm-chat-thinking" icon="bx bx-brain" label={label || t("llm_chat.thought_process")}>+ {body && (+ <div className="llm-chat-thinking-content">+ <TextBlockContent content={body} />+ </div>+ )}</ExpandableSection>);}@@ -356,15 +359,16 @@ function renderContentBlocks(blocks: ContentBlock[], isStreaming?: boolean) {/** 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 first line of a thought without its inline Markdown marks, to name a thought that has no title. */-function firstLinePlainText(content: string): string | undefined {- return content.trim().split("\n")[0].replace(/\*\*|`/g, "").trim() || undefined;+/**+ * Split off the line that names a folded thought: its leading `**Title**`, or else its first line+ * without inline Markdown marks. The body is the rest, so the opened thought does not repeat it.+ */+function splitThinkingLabel(content: string): { label: string; body: string } {+ const trimmed = content.trim();+ const title = /^\*\*([^*\n]+)\*\*[ \t]*(?:\n|$)/.exec(trimmed);+ const firstLine = title?.[0] ?? trimmed.split("\n", 1)[0];+ const label = title?.[1] ?? firstLine.replace(/\*\*|`/g, "");+ return { label: label.trim(), body: trimmed.slice(firstLine.length).trim() };}/** The last `**Title**` line of a thought, naming what the model is working on now. */