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): let a body-less thought open only when its label is cut off
Elian Doranbd5583ea0b00
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.css b/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.cssindex d3aedad992..87f7f915de 100644--- a/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.css+++ b/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.css@@ -244,10 +244,26 @@ body.background-effects.theme-supports-background-effects .llm-chat-message-userdisplay: block;}+ .llm-chat-thinking-label {+ display: block;+ overflow: hidden;+ text-overflow: ellipsis;+ white-space: nowrap;+ }+/* The label is left out of the body, so the opened thought shows it in full. */- &[open] > .expandable-section-summary .expandable-section-label {+ &[open] > .expandable-section-summary :is(.expandable-section-label, .llm-chat-thinking-label) {white-space: normal;}++ /* A body-less thought whose label fits has nothing to reveal. */+ &.llm-chat-thinking-fits > .expandable-section-summary {+ pointer-events: none;++ .expandable-section-chevron {+ display: none;+ }+ }}.llm-chat-thinking-title {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 f444ad0ac1..14ea4dd392 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@@ -91,11 +91,35 @@ describe("ChatMessage thinking", () => {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("opens a thought with nothing under its label only when the label is cut off", () => {+ const scrollWidth = vi.spyOn(Element.prototype, "scrollWidth", "get").mockReturnValue(100);+ const clientWidth = vi.spyOn(HTMLElement.prototype, "clientWidth", "get").mockReturnValue(100);+ try {+ const content = [+ { type: "thinking" as const, content: "I should look up who maintains it." },+ { type: "thinking" as const, content: "\n\n**Acknowledging uncertainty on contentPreview meaning**" }+ ];+ let thoughts = [...renderMessage(content).querySelectorAll(".llm-chat-thinking")];+ expect(thoughts.map(el => el.querySelector(".llm-chat-thinking-label")?.textContent)).toEqual([+ "I should look up who maintains it.",+ "Acknowledging uncertainty on contentPreview meaning"+ ]);+ for (const thought of thoughts) {+ expect(thought instanceof HTMLDetailsElement).toBe(true);+ expect(thought.classList.contains("llm-chat-thinking-fits")).toBe(true);+ expect(thought.querySelector(".llm-chat-thinking-content")).toBeNull();+ }++ host && render(null, host);+ scrollWidth.mockReturnValue(250);+ thoughts = [...renderMessage(content).querySelectorAll(".llm-chat-thinking")];+ for (const thought of thoughts) {+ expect(thought.classList.contains("llm-chat-thinking-fits")).toBe(false);+ }+ } finally {+ scrollWidth.mockRestore();+ clientWidth.mockRestore();+ }});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 a198121459..faee0d206d 100644--- a/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.tsx+++ b/apps/client/src/widgets/type_widgets/llm_chat/ChatMessage.tsx@@ -3,12 +3,13 @@ import "../markdown/MarkdownCommons.css";import { type LlmCitation } from "@triliumnext/commons";import { memo } from "preact/compat";-import { useMemo } from "preact/hooks";+import { useCallback, useLayoutEffect, useMemo, useRef, useState } from "preact/hooks";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 { useResizeObserver } from "../../react/hooks.js";import LoadingSpinner from "../../react/LoadingSpinner.js";import { ReadOnlyTextContent } from "../text/ReadOnlyText.js";import { formatErrorDetails } from "./chat_error.js";@@ -142,17 +143,44 @@ function ThinkingCard({ content, isLive }: { content: string; isLive?: boolean }}const { label, body } = splitThinkingLabel(content);+ if (!body) {+ return <ThinkingLine label={label || t("llm_chat.thought_process")} />;+ }+return (<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>- )}+ <div className="llm-chat-thinking-content">+ <TextBlockContent content={body} />+ </div></ExpandableSection>);}+/**+ * A finished thought with nothing under its label. It opens, letting the label wrap, only when the+ * label is cut off. The label is measured while closed, since an open one wraps and always fits.+ */+function ThinkingLine({ label }: { label: string }) {+ const labelRef = useRef<HTMLSpanElement>(null);+ const [fits, setFits] = useState(true);+ const measure = useCallback(() => {+ const el = labelRef.current;+ if (el && !el.closest("details")?.open) {+ setFits(el.scrollWidth <= el.clientWidth);+ }+ }, []);+ useLayoutEffect(measure, [label, measure]);+ useResizeObserver(labelRef, measure);++ return (+ <ExpandableSection+ className={`llm-chat-thinking ${fits ? "llm-chat-thinking-fits" : ""}`}+ icon="bx bx-brain"+ label={<span ref={labelRef} className="llm-chat-thinking-label">{label}</span>}+ />+ );+}+function ChatMessage({ message, isStreaming, onRetry }: Props) {const isError = message.type === "error";const isThinking = message.type === "thinking";diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ExpandableCard.tsx b/apps/client/src/widgets/type_widgets/llm_chat/ExpandableCard.tsxindex 1c0b9b7f6c..fb6976ad66 100644--- a/apps/client/src/widgets/type_widgets/llm_chat/ExpandableCard.tsx+++ b/apps/client/src/widgets/type_widgets/llm_chat/ExpandableCard.tsx@@ -8,7 +8,7 @@ interface ExpandableSectionProps {className?: string;/** Whether the section is expanded on initial render. */open?: boolean;- children: ComponentChildren;+ children?: ComponentChildren;}/** A collapsible section within an ExpandableCard. */