Skip to content

Build your personal knowledge base with Trilium Notes

refactor(llm): use diff library instead

Elian Doran6c4d482bea22
diff --git a/apps/client/package.json b/apps/client/package.json
index a6f3bfff00..f1e71dc17c 100644
--- a/apps/client/package.json
+++ b/apps/client/package.json
@@ -50,6 +50,7 @@
"clsx": "2.1.1",
"color": "5.0.3",
"debounce": "3.0.0",
+ "diff": "9.0.0",
"dompurify": "3.4.3",
"draggabilly": "3.0.0",
"force-graph": "1.51.4",
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/EditNoteContentDiff.tsx b/apps/client/src/widgets/type_widgets/llm_chat/EditNoteContentDiff.tsx
index a28ccface8..73b2da3085 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/EditNoteContentDiff.tsx
+++ b/apps/client/src/widgets/type_widgets/llm_chat/EditNoteContentDiff.tsx
@@ -1,5 +1,7 @@
import "./EditNoteContentDiff.css";
+import { diffLines as jsDiffLines } from "diff";
+
import { t } from "../../../services/i18n.js";
/** A single find-and-replace edit performed by the `edit_note_content` tool. */
@@ -16,44 +18,26 @@ export interface DiffLine {
}
/**
- * 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.
+ * Compute a line-based diff between two text blocks via the `diff` (jsdiff)
+ * library, flattening its grouped change list into one entry per line — each
+ * tagged as added, removed or unchanged context — for row-by-row rendering.
*/
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++;
+ // `ignoreNewlineAtEof` keeps a trailing-newline-only difference from
+ // showing up as a spurious changed line.
+ for (const part of jsDiffLines(oldText, newText, { ignoreNewlineAtEof: true })) {
+ const type: DiffLineType = part.added ? "add" : part.removed ? "remove" : "context";
+ const lines = part.value.split("\n");
+ // jsdiff keeps each line's trailing newline, so a split yields an empty
+ // tail element — drop it rather than rendering a blank line.
+ if (lines.length > 1 && lines[lines.length - 1] === "") {
+ lines.pop();
+ }
+ for (const line of lines) {
+ result.push({ type, text: line });
}
}
- while (i < m) result.push({ type: "remove", text: a[i++] });
- while (j < n) result.push({ type: "add", text: b[j++] });
return result;
}
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 4b16810f4c..8190dc7077 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -294,6 +294,9 @@ importers:
debounce:
specifier: 3.0.0
version: 3.0.0
+ diff:
+ specifier: 9.0.0
+ version: 9.0.0
dompurify:
specifier: 3.4.3
version: 3.4.3
@@ -2496,7 +2499,7 @@ packages:
engines: {node: '>=14'}
'@electron/node-gyp@https://codeload.github.com/electron/node-gyp/tar.gz/06b29aafb7708acef8b3669835c8a7857ebc92d2':
- resolution: {gitHosted: true, integrity: sha512-MXgzlTDEEndJB3TBbvd5uFQO/8gaINo1Hfen8vef5rq/VHVPeB63uuv/uO5+8GFsAJ/rauu6XB79S6K4+aXc+w==, tarball: https://codeload.github.com/electron/node-gyp/tar.gz/06b29aafb7708acef8b3669835c8a7857ebc92d2}
+ resolution: {gitHosted: true, tarball: https://codeload.github.com/electron/node-gyp/tar.gz/06b29aafb7708acef8b3669835c8a7857ebc92d2}
version: 10.2.0-electron.1
engines: {node: '>=12.13.0'}
hasBin: true