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): fold each thought to a plain muted line
Elian Doran193d76f5fc3e
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.css b/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.cssindex 0566945568..be888b06be 100644--- a/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.css+++ b/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.css@@ -200,26 +200,33 @@ body.background-effects.theme-supports-background-effects .llm-chat-message-usermargin-top: 0.75rem;}-.llm-chat-thinking-card.expandable-card {- width: 100%;- max-width: 100%;- border-style: dashed;-}--.llm-chat-thinking-card .expandable-section-summary,-.llm-chat-thinking-header {+.llm-chat-thinking {+ margin: 0.25rem 0;+ font-size: 0.85rem;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;+ .expandable-section-summary,+ .llm-chat-thinking-header {+ display: flex;+ align-items: center;+ gap: 0.4rem;+ width: fit-content;+ max-width: 100%;+ padding: 0.15rem 0;+ font-weight: normal;++ > .bx {+ font-size: 1rem;+ margin-right: 0;+ }+ }- > .bx {- font-size: 1rem;+ .expandable-section-summary:hover {+ color: var(--main-text-color);+ }++ .expandable-section-chevron {+ margin-left: 0;}}@@ -230,10 +237,11 @@ 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. */.llm-chat-thinking-content {- padding: 0.5rem 0.75rem;- font-size: 0.9rem;- color: var(--muted-text-color);+ margin: 0.15rem 0 0.25rem 0.5rem;+ padding-left: 0.9rem;+ border-left: 2px solid var(--main-border-color);}/* Aligning the content to the end clips the overflow at the top, keeping the newest lines in view. */@@ -241,8 +249,7 @@ body.background-effects.theme-supports-background-effects .llm-chat-message-userdisplay: flex;flex-direction: column;justify-content: flex-end;- max-height: calc(4 * 1.6em + 1rem);- padding-top: 0;+ max-height: calc(4 * 1.6em);overflow: hidden;> * {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 c8b90a0bd4..723b7fb8e7 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@@ -68,32 +68,30 @@ describe("ChatMessage thinking", () => {return target;}- it("folds each finished thought to its title, in stream order between the tool calls", () => {+ it("folds each finished thought to its title or first line, 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: "thinking", content: "The file holds **the** `name`.\n\nNothing else to check." },{ 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]);+ expect(blocks.map(el => el.classList.contains("llm-chat-thinking"))).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);+ const [titled, untitled] = target.querySelectorAll(".llm-chat-thinking");+ expect(titled instanceof HTMLDetailsElement && !titled.open).toBe(true);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");+ expect(untitled.querySelector(".expandable-section-label")?.textContent).toBe("The file holds the name.");});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");+ let card = target.querySelector(".llm-chat-thinking");expect(card?.classList.contains("llm-chat-thinking-live")).toBe(true);expect(card?.querySelector("details")).toBeNull();expect(card?.querySelector(".bx-spin")).not.toBeNull();@@ -101,15 +99,15 @@ describe("ChatMessage thinking", () => {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");+ card = target.querySelector(".llm-chat-thinking");expect(card?.classList.contains("llm-chat-thinking-live")).toBe(false);- expect(card?.querySelector("details")?.open).toBe(false);+ expect(card instanceof HTMLDetailsElement && !card.open).toBe(true);expect(card?.querySelector(".expandable-section-label")?.textContent).toBe("Reading the hostname");});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");+ const card = target.querySelector(".llm-chat-thinking");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 75c207f834..484c8bcc18 100644--- a/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.tsx+++ b/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.tsx@@ -121,14 +121,14 @@ 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.+ * 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.*/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 llm-chat-thinking-live"><div className="llm-chat-thinking-header"><LoadingSpinner /><span className="llm-chat-thinking-title">{latestThinkingTitle(content) ?? t("llm_chat.thinking")}</span>@@ -136,19 +136,17 @@ function ThinkingCard({ content, isLive }: { content: string; isLive?: boolean }<div className="llm-chat-thinking-content"><TextBlockContent content={content} /></div>- </ExpandableCard>+ </div>);}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>+ <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>);}@@ -364,6 +362,11 @@ function splitThinkingTitle(content: string): { title?: string; body: string } {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;+}+/** 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/chat_highlights_anchor.spec.ts b/apps/client/src/widgets/type_widgets/llm_chat/chat_highlights_anchor.spec.tsindex eafa0b3438..99d0ddce21 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@@ -51,7 +51,7 @@ describe("buildProseProjection", () => {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="llm-chat-thinking-card"><div class="llm-chat-markdown"><p>thought</p></div></div>`+ `<div class="llm-chat-thinking"><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 6fd47396ed..fbe88737b2 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@@ -14,7 +14,7 @@ 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, thoughts. */-const NON_PROSE_SELECTOR = "pre, code, .math-tex, .katex, .ck-math-tex, .mermaid, svg, .llm-chat-thinking-card";+const NON_PROSE_SELECTOR = "pre, code, .math-tex, .katex, .ck-math-tex, .mermaid, svg, .llm-chat-thinking";/** 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 6cb1cd7b95..760144d3c7 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@@ -281,7 +281,7 @@ describe("extractChatHeadings", () => {assistantMessage("a1", "Answer")], containerWith(messageWrapper("u1"),- messageWrapper("a1", `<div class="llm-chat-thinking-card">${markdownContent("<h1>Thought</h1>")}</div>${markdownContent("<h1>Real</h1>")}`)+ messageWrapper("a1", `<div class="llm-chat-thinking">${markdownContent("<h1>Thought</h1>")}</div>${markdownContent("<h1>Real</h1>")}`)));expect(headings).toMatchObject([{ id: "u1", level: 1 },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 0ce93794dc..c31132bf05 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,7 +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;+ if (headingEl.closest(".llm-chat-thinking")) continue;headings.push({id: `${message.id}:${index}`,level: parseInt(headingEl.tagName.substring(1), 10) + levelOffset,