Skip to content

Build your personal knowledge base with Trilium Notes

feat(llm): basic diff for edited notes

Elian Dorancaee157609a4
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index a6a1a9681f..8b89b3a06e 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -1793,6 +1793,8 @@
"thought_process": "Thought process",
"tool_calls": "{{count}} tool call(s)",
"input": "Input",
+ "changes": "Changes",
+ "edit_index": "Edit {{index}} of {{total}}",
"result": "Result",
"error": "Error",
"tool_error": "failed",
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/EditNoteContentDiff.css b/apps/client/src/widgets/type_widgets/llm_chat/EditNoteContentDiff.css
new file mode 100644
index 0000000000..0bcd97fe99
--- /dev/null
+++ b/apps/client/src/widgets/type_widgets/llm_chat/EditNoteContentDiff.css
@@ -0,0 +1,64 @@
+/* Unified diff rendering for the `edit_note_content` tool call. */
+
+.llm-diff {
+ display: flex;
+ flex-direction: column;
+ 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;
+ 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;
+}
+
+.llm-diff-gutter {
+ flex: 0 0 1.5rem;
+ text-align: center;
+ user-select: none;
+ color: var(--muted-text-color);
+ opacity: 0.7;
+}
+
+.llm-diff-text {
+ flex: 1 1 auto;
+}
+
+.llm-diff-line-add {
+ background: rgba(100, 200, 100, 0.2);
+}
+
+.llm-diff-line-add .llm-diff-gutter {
+ color: rgb(60, 160, 60);
+ opacity: 1;
+}
+
+.llm-diff-line-remove {
+ background: rgba(255, 100, 100, 0.18);
+}
+
+.llm-diff-line-remove .llm-diff-gutter {
+ color: rgb(200, 70, 70);
+ opacity: 1;
+}
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.ts
new file mode 100644
index 0000000000..de780583cf
--- /dev/null
+++ b/apps/client/src/widgets/type_widgets/llm_chat/EditNoteContentDiff.spec.ts
@@ -0,0 +1,52 @@
+import { describe, expect, it } from "vitest";
+
+import { diffLines, parseNoteContentEdits } from "./EditNoteContentDiff.js";
+
+describe("diffLines", () => {
+ it("marks a replaced line as remove then add, keeping surrounding context", () => {
+ const diff = diffLines(
+ "const a = 1;\nconst b = 2;\nconst c = 3;",
+ "const a = 1;\nconst b = 20;\nconst c = 3;"
+ );
+ expect(diff).toEqual([
+ { type: "context", text: "const a = 1;" },
+ { type: "remove", text: "const b = 2;" },
+ { type: "add", text: "const b = 20;" },
+ { type: "context", text: "const c = 3;" }
+ ]);
+ });
+
+ it("treats pure insertions and deletions correctly", () => {
+ expect(diffLines("line", "line\nextra")).toEqual([
+ { type: "context", text: "line" },
+ { type: "add", text: "extra" }
+ ]);
+ expect(diffLines("line\nextra", "line")).toEqual([
+ { type: "context", text: "line" },
+ { type: "remove", text: "extra" }
+ ]);
+ });
+
+ it("returns all context when the texts are identical", () => {
+ expect(diffLines("a\nb", "a\nb")).toEqual([
+ { type: "context", text: "a" },
+ { type: "context", text: "b" }
+ ]);
+ });
+});
+
+describe("parseNoteContentEdits", () => {
+ it("accepts a well-formed edits array", () => {
+ expect(parseNoteContentEdits([{ oldText: "a", newText: "b" }])).toEqual([
+ { oldText: "a", newText: "b" }
+ ]);
+ });
+
+ it("rejects empty, non-array, or malformed input", () => {
+ expect(parseNoteContentEdits([])).toBeNull();
+ expect(parseNoteContentEdits(undefined)).toBeNull();
+ expect(parseNoteContentEdits("nope")).toBeNull();
+ expect(parseNoteContentEdits([{ oldText: "a" }])).toBeNull();
+ expect(parseNoteContentEdits([{ oldText: 1, newText: 2 }])).toBeNull();
+ });
+});
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/EditNoteContentDiff.tsx b/apps/client/src/widgets/type_widgets/llm_chat/EditNoteContentDiff.tsx
new file mode 100644
index 0000000000..a28ccface8
--- /dev/null
+++ b/apps/client/src/widgets/type_widgets/llm_chat/EditNoteContentDiff.tsx
@@ -0,0 +1,110 @@
+import "./EditNoteContentDiff.css";
+
+import { t } from "../../../services/i18n.js";
+
+/** A single find-and-replace edit performed by the `edit_note_content` tool. */
+export interface NoteContentEdit {
+ oldText: string;
+ newText: string;
+}
+
+export type DiffLineType = "add" | "remove" | "context";
+
+export interface DiffLine {
+ type: DiffLineType;
+ text: string;
+}
+
+/**
+ * Compute a line-based diff between two text blocks using the classic
+ * longest-common-subsequence algorithm. Returns the lines in display order,
+ * each tagged as added, removed or unchanged context.
+ */
+export function diffLines(oldText: string, newText: string): DiffLine[] {
+ const a = oldText.split("\n");
+ const b = newText.split("\n");
+ const m = a.length;
+ const n = b.length;
+
+ // dp[i][j] = length of the LCS of a[i:] and b[j:].
+ const dp: number[][] = Array.from({ length: m + 1 }, () => new Array<number>(n + 1).fill(0));
+ for (let i = m - 1; i >= 0; i--) {
+ for (let j = n - 1; j >= 0; j--) {
+ dp[i][j] = a[i] === b[j]
+ ? dp[i + 1][j + 1] + 1
+ : Math.max(dp[i + 1][j], dp[i][j + 1]);
+ }
+ }
+
+ const result: DiffLine[] = [];
+ let i = 0;
+ let j = 0;
+ while (i < m && j < n) {
+ if (a[i] === b[j]) {
+ result.push({ type: "context", text: a[i] });
+ i++;
+ j++;
+ } else if (dp[i + 1][j] >= dp[i][j + 1]) {
+ result.push({ type: "remove", text: a[i] });
+ i++;
+ } else {
+ result.push({ type: "add", text: b[j] });
+ j++;
+ }
+ }
+ while (i < m) result.push({ type: "remove", text: a[i++] });
+ while (j < n) result.push({ type: "add", text: b[j++] });
+ return result;
+}
+
+const GUTTER_MARKER: Record<DiffLineType, string> = {
+ add: "+",
+ remove: "-",
+ context: " "
+};
+
+/** 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);
+ 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>
+ </div>
+ ))}
+ </div>
+ );
+}
+
+/** Validate that an unknown value is a usable list of note-content edits. */
+export function parseNoteContentEdits(value: unknown): NoteContentEdit[] | null {
+ if (!Array.isArray(value) || value.length === 0) return null;
+ const edits: NoteContentEdit[] = [];
+ for (const item of value) {
+ if (typeof item !== "object" || item === null) return null;
+ const { oldText, newText } = item as Record<string, unknown>;
+ if (typeof oldText !== "string" || typeof newText !== "string") return null;
+ edits.push({ oldText, newText });
+ }
+ return edits;
+}
+
+/** A fancy unified diff for the `edit_note_content` tool's list of edits. */
+export function EditNoteContentDiff({ edits }: { edits: NoteContentEdit[] }) {
+ return (
+ <div className="llm-diff">
+ {edits.map((edit, idx) => (
+ <div key={idx} className="llm-diff-edit">
+ {edits.length > 1 && (
+ <div className="llm-diff-edit-header">
+ {t("llm_chat.edit_index", { index: idx + 1, total: edits.length })}
+ </div>
+ )}
+ <DiffHunk edit={edit} />
+ </div>
+ ))}
+ </div>
+ );
+}
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.tsx b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.tsx
index c637ca7007..d642997bde 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.tsx
+++ b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.tsx
@@ -4,6 +4,7 @@ import { Trans } from "react-i18next";
import { t } from "../../../services/i18n.js";
import { NewNoteLink } from "../../react/NoteLink.js";
+import { EditNoteContentDiff, parseNoteContentEdits } from "./EditNoteContentDiff.js";
import { ExpandableCard, ExpandableSection } from "./ExpandableCard.js";
import type { ToolCall } from "./llm_chat_types.js";
@@ -186,6 +187,11 @@ function ToolCallLabel({ toolCall }: { toolCall: ToolCall }) {
function ToolCallSection({ toolCall }: { toolCall: ToolCall }) {
const hasError = toolCall.isError;
+ // The `edit_note_content` tool gets a fancy unified diff instead of a raw input table.
+ const noteContentEdits = toolCall.toolName === "edit_note_content"
+ ? parseNoteContentEdits(toolCall.input?.edits)
+ : null;
+
return (
<ExpandableSection
icon={toolCallIcon(toolCall)}
@@ -193,8 +199,10 @@ function ToolCallSection({ toolCall }: { toolCall: ToolCall }) {
className={hasError ? "llm-chat-tool-call-error" : ""}
>
<div className="llm-chat-tool-call-input">
- <strong>{t("llm_chat.input")}</strong>
- <KeyValueTable data={toolCall.input} />
+ <strong>{noteContentEdits ? t("llm_chat.changes") : t("llm_chat.input")}</strong>
+ {noteContentEdits
+ ? <EditNoteContentDiff edits={noteContentEdits} />
+ : <KeyValueTable data={toolCall.input} />}
</div>
{toolCall.result && (
<div className={`llm-chat-tool-call-result ${hasError ? "llm-chat-tool-call-result-error" : ""}`}>