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 an edit's changes as blocks like the append view
Elian Doran3b4aa2400ee6
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/EditNoteContentDiff.css b/apps/client/src/widgets/type_widgets/llm_chat/EditNoteContentDiff.cssindex 0bcd97fe99..e4ffcbbcd4 100644--- a/apps/client/src/widgets/type_widgets/llm_chat/EditNoteContentDiff.css+++ b/apps/client/src/widgets/type_widgets/llm_chat/EditNoteContentDiff.css@@ -1,4 +1,4 @@-/* Unified diff rendering for the `edit_note_content` tool call. */+/* The `edit_note_content` tool's edits, one block per run of added, removed or unchanged lines. */.llm-diff {display: flex;@@ -6,59 +6,50 @@gap: 0.5rem;}-.llm-diff-edit {- border: 1px solid var(--main-border-color);- border-radius: 4px;- overflow: hidden;-}-.llm-diff-edit-header {- padding: 0.2rem 0.5rem;- font-size: 0.7rem;- text-transform: uppercase;+ margin-bottom: 0.15rem;+ font-size: 0.75rem;color: var(--muted-text-color);- background: var(--accented-background-color);- border-bottom: 1px solid var(--main-border-color);}.llm-diff-hunk {- font-family: var(--monospace-font-family, monospace);- font-size: 0.8rem;- line-height: 1.5;-}--.llm-diff-line {display: flex;- white-space: pre-wrap;- word-break: break-word;+ flex-direction: column;+ gap: 0.15rem;+ font-size: 0.85rem;}-.llm-diff-gutter {- flex: 0 0 1.5rem;- text-align: center;- user-select: none;- color: var(--muted-text-color);- opacity: 0.7;-}+.llm-diff-block {+ padding-left: 0.5rem;+ border-left: 2px solid transparent;-.llm-diff-text {- flex: 1 1 auto;-}+ > pre {+ margin: 0;+ font-size: 0.8rem;+ white-space: pre-wrap;+ overflow-wrap: anywhere;+ }++ .llm-chat-markdown > :first-child {+ margin-top: 0;+ }-.llm-diff-line-add {- background: rgba(100, 200, 100, 0.2);+ .llm-chat-markdown > :last-child {+ margin-bottom: 0;+ }}-.llm-diff-line-add .llm-diff-gutter {- color: rgb(60, 160, 60);- opacity: 1;+.llm-diff-block-context {+ color: var(--muted-text-color);}-.llm-diff-line-remove {- background: rgba(255, 100, 100, 0.18);+/* The same green as `.llm-chat-written-appended` in ToolCallViews.css. */+.llm-diff-block-add {+ border-left-color: rgb(60, 160, 60);}-.llm-diff-line-remove .llm-diff-gutter {- color: rgb(200, 70, 70);- opacity: 1;+.llm-diff-block-remove {+ border-left-color: rgb(200, 70, 70);+ color: var(--muted-text-color);+ text-decoration: line-through;}diff --git a/apps/client/src/widgets/type_widgets/llm_chat/EditNoteContentDiff.spec.ts b/apps/client/src/widgets/type_widgets/llm_chat/EditNoteContentDiff.spec.tsindex e11112fbd4..74ec9e5fbb 100644--- a/apps/client/src/widgets/type_widgets/llm_chat/EditNoteContentDiff.spec.ts+++ b/apps/client/src/widgets/type_widgets/llm_chat/EditNoteContentDiff.spec.ts@@ -1,6 +1,6 @@import { describe, expect, it } from "vitest";-import { diffLines, isSmallEdit, parseNoteContentEdits } from "./EditNoteContentDiff.js";+import { diffBlocks, diffLines, isSmallEdit, parseNoteContentEdits } from "./EditNoteContentDiff.js";describe("diffLines", () => {it("marks a replaced line as remove then add, keeping surrounding context", () => {@@ -68,3 +68,14 @@ describe("parseNoteContentEdits", () => {expect(parseNoteContentEdits([{ oldText: 1, newText: 2 }])).toBeNull();});});++describe("diffBlocks", () => {+ it("joins consecutive lines of one kind into a block", () => {+ expect(diffBlocks("a\nb\nold 1\nold 2\nc", "a\nb\nnew\nc")).toEqual([+ { type: "context", text: "a\nb" },+ { type: "remove", text: "old 1\nold 2" },+ { type: "add", text: "new" },+ { type: "context", text: "c" }+ ]);+ });+});diff --git a/apps/client/src/widgets/type_widgets/llm_chat/EditNoteContentDiff.tsx b/apps/client/src/widgets/type_widgets/llm_chat/EditNoteContentDiff.tsxindex f53aa617f6..e4153ab8d8 100644--- a/apps/client/src/widgets/type_widgets/llm_chat/EditNoteContentDiff.tsx+++ b/apps/client/src/widgets/type_widgets/llm_chat/EditNoteContentDiff.tsx@@ -3,6 +3,10 @@ import "./EditNoteContentDiff.css";import { diffLines as jsDiffLines } from "diff";import { t } from "../../../services/i18n.js";+import { MARKDOWN_NOTE_TYPE_MIME } from "../../../services/note_types.js";+import { useNote } from "../../react/hooks.js";+import { ReadOnlyTextContent } from "../text/ReadOnlyText.js";+import { renderMarkdown } from "./chat_markdown.js";/** A single find-and-replace edit performed by the `edit_note_content` tool. */export interface NoteContentEdit {@@ -41,21 +45,38 @@ export function diffLines(oldText: string, newText: string): DiffLine[] {return result;}-const GUTTER_MARKER: Record<DiffLineType, string> = {- add: "+",- remove: "-",- context: " "-};+/** A run of consecutive diff lines of one kind, joined back into text. */+export interface DiffBlock {+ type: DiffLineType;+ text: string;+}-/** Renders a single edit (one find/replace pair) as a unified diff hunk. */-function DiffHunk({ edit }: { edit: NoteContentEdit }) {- const lines = diffLines(edit.oldText, edit.newText);+/** Diffs two texts into blocks, each a run of added, removed or unchanged lines. */+export function diffBlocks(oldText: string, newText: string): DiffBlock[] {+ const blocks: DiffBlock[] = [];+ for (const line of diffLines(oldText, newText)) {+ const last = blocks[blocks.length - 1];+ if (last?.type === line.type) {+ last.text += `\n${line.text}`;+ } else {+ blocks.push({ ...line });+ }+ }+ return blocks;+}++/**+ * Renders a single edit (one find/replace pair) as blocks: added text on a green edge, removed text on+ * a red edge and struck through. A Markdown note's blocks render as Markdown; other notes stay code.+ */+function DiffHunk({ edit, markdown }: { edit: NoteContentEdit; markdown: boolean }) {return (<div className="llm-diff-hunk">- {lines.map((line, idx) => (- <div key={idx} className={`llm-diff-line llm-diff-line-${line.type}`}>- <span className="llm-diff-gutter">{GUTTER_MARKER[line.type]}</span>- <span className="llm-diff-text">{line.text || " "}</span>+ {diffBlocks(edit.oldText, edit.newText).map((block, idx) => (+ <div key={idx} className={`llm-diff-block llm-diff-block-${block.type}`}>+ {markdown+ ? <ReadOnlyTextContent html={renderMarkdown(block.text)} className="llm-chat-markdown" />+ : <pre>{block.text || " "}</pre>}</div>))}</div>@@ -95,7 +116,9 @@ export function parseNoteContentEdits(value: unknown): NoteContentEdit[] | null}/** A fancy unified diff for the `edit_note_content` tool's list of edits. */-export function EditNoteContentDiff({ edits }: { edits: NoteContentEdit[] }) {+export function EditNoteContentDiff({ noteId, edits }: { noteId?: string; edits: NoteContentEdit[] }) {+ const note = useNote(noteId);+ const markdown = note?.type === "code" && note.mime === MARKDOWN_NOTE_TYPE_MIME;return (<div className="llm-diff">{edits.map((edit, idx) => (@@ -105,7 +128,7 @@ export function EditNoteContentDiff({ edits }: { edits: NoteContentEdit[] }) {{t("llm_chat.edit_index", { index: idx + 1, total: edits.length })}</div>)}- <DiffHunk edit={edit} />+ <DiffHunk edit={edit} markdown={markdown} /></div>))}</div>diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.spec.tsx b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.spec.tsxindex 1341cc9964..eb676cf62d 100644--- a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.spec.tsx+++ b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.spec.tsx@@ -400,4 +400,31 @@ describe("ToolCallCard", () => {expect(json).not.toBeUndefined();expect(json?.querySelector(".llm-chat-written")).toBeNull();});++ it("shows an edit as blocks with colored edges, rendering Markdown on a Markdown note", () => {+ mocks.notes = {+ m: { type: "code", mime: "text/x-markdown" },+ c: { type: "code", mime: "text/x-python" }+ };+ const edit = (id: string, noteId: string, oldText: string, newText: string): ToolCall => ({+ id, toolName: "edit_note_content", input: { noteId, edits: [ { oldText, newText } ] }+ });+ const target = renderCard([+ edit("1", "m", "a\n**old**\nc", "a\n**new**\nc"),+ { id: "2", toolName: "get_note", input: { noteId: "x" }, result: "{}" },+ edit("3", "c", "x = 1\ny = 2", "x = 1\ny = 3")+ ]);+ const [ markdown, , code ] = [ ...(target.querySelector(".llm-chat-tool-calls")?.children ?? []) ];++ const blocks = [ ...(markdown?.querySelectorAll(".llm-diff-block") ?? []) ];+ expect(blocks.map(block => block.className.replace("llm-diff-block ", ""))).toEqual([+ "llm-diff-block-context", "llm-diff-block-remove", "llm-diff-block-add", "llm-diff-block-context"+ ]);+ expect(blocks[1]?.querySelector(".markdown-stub")?.textContent).toContain("<strong>old</strong>");+ expect(blocks[2]?.querySelector(".markdown-stub")?.textContent).toContain("<strong>new</strong>");++ const codeRemoved = code?.querySelector(".llm-diff-block-remove");+ expect(codeRemoved?.querySelector("pre")?.textContent).toBe("y = 2");+ expect(codeRemoved?.querySelector(".markdown-stub")).toBeNull();+ });});diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.tsx b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.tsxindex ec169e56e7..a02f965967 100644--- a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.tsx+++ b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.tsx@@ -133,6 +133,7 @@ function ToolCallSection({ toolCall }: { toolCall: ToolCall }) {const noteContentEdits = !isStreamingInput && toolCall.toolName === "edit_note_content"? parseNoteContentEdits(toolCall.input?.edits): null;+ const editedNoteId = typeof toolCall.input.noteId === "string" ? toolCall.input.noteId : undefined;const errorMessage = hasError && toolCall.result ? getErrorMessage(toolCall.result) : null;const view = isStreamingInput ? null : getToolCallView(toolCall);@@ -165,7 +166,7 @@ function ToolCallSection({ toolCall }: { toolCall: ToolCall }) {{isStreamingInput && <pre className="llm-chat-tool-call-streaming">{toolCall.inputStreaming}</pre>}{noteContentEdits && (<div className="llm-chat-tool-call-diff">- <EditNoteContentDiff edits={noteContentEdits} />+ <EditNoteContentDiff noteId={editedNoteId} edits={noteContentEdits} /></div>)}{errorMessage && <p className="llm-chat-tool-call-error-message">{errorMessage}</p>}diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.css b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.cssindex 53d8777957..8b8f05a5aa 100644--- a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.css+++ b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.css@@ -126,7 +126,8 @@}}-/* Appended content is new at the end of the note, so it wears the diff's color for added lines. */+/* Appended content is new at the end of the note. The green matches `.llm-diff-block-add` in+ EditNoteContentDiff.css. */.llm-chat-written-appended {padding-left: 0.5rem;border-left: 2px solid rgb(60, 160, 60);