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): automatically expand "Edit note" when small enough
Elian Dorane5238cf91ece
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 de780583cf..e11112fbd4 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, parseNoteContentEdits } from "./EditNoteContentDiff.js";+import { diffLines, isSmallEdit, parseNoteContentEdits } from "./EditNoteContentDiff.js";describe("diffLines", () => {it("marks a replaced line as remove then add, keeping surrounding context", () => {@@ -35,6 +35,24 @@ describe("diffLines", () => {});});+describe("isSmallEdit", () => {+ it("treats a few changed lines as small", () => {+ expect(isSmallEdit([{ oldText: "a\nb\nc", newText: "a\nB\nc" }])).toBe(true);+ });++ it("treats a large rewrite as not small", () => {+ const oldText = Array.from({ length: 20 }, (_, i) => `old ${i}`).join("\n");+ const newText = Array.from({ length: 20 }, (_, i) => `new ${i}`).join("\n");+ expect(isSmallEdit([{ oldText, newText }])).toBe(false);+ });++ it("sums changed lines across multiple edits", () => {+ const edit = { oldText: "x", newText: "y" };+ expect(isSmallEdit(Array.from({ length: 4 }, () => edit))).toBe(true);+ expect(isSmallEdit(Array.from({ length: 6 }, () => edit))).toBe(false);+ });+});+describe("parseNoteContentEdits", () => {it("accepts a well-formed edits array", () => {expect(parseNoteContentEdits([{ oldText: "a", newText: "b" }])).toEqual([diff --git a/apps/client/src/widgets/type_widgets/llm_chat/EditNoteContentDiff.tsx b/apps/client/src/widgets/type_widgets/llm_chat/EditNoteContentDiff.tsxindex 73b2da3085..f53aa617f6 100644--- a/apps/client/src/widgets/type_widgets/llm_chat/EditNoteContentDiff.tsx+++ b/apps/client/src/widgets/type_widgets/llm_chat/EditNoteContentDiff.tsx@@ -62,6 +62,25 @@ function DiffHunk({ edit }: { edit: NoteContentEdit }) {);}+/** Maximum number of changed (added + removed) lines for an edit to count as "small". */+export const SMALL_EDIT_LINE_LIMIT = 10;++/**+ * Whether the combined diff of all edits is small enough that the section+ * should be expanded by default — i.e. the user can take it in at a glance.+ */+export function isSmallEdit(edits: NoteContentEdit[]): boolean {+ let changedLines = 0;+ for (const edit of edits) {+ for (const line of diffLines(edit.oldText, edit.newText)) {+ if (line.type !== "context") {+ changedLines++;+ }+ }+ }+ return changedLines <= SMALL_EDIT_LINE_LIMIT;+}+/** 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;diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ExpandableCard.tsx b/apps/client/src/widgets/type_widgets/llm_chat/ExpandableCard.tsxindex 2e12c08e64..1c0b9b7f6c 100644--- a/apps/client/src/widgets/type_widgets/llm_chat/ExpandableCard.tsx+++ b/apps/client/src/widgets/type_widgets/llm_chat/ExpandableCard.tsx@@ -6,13 +6,15 @@ interface ExpandableSectionProps {icon: string;label: ComponentChildren;className?: string;+ /** Whether the section is expanded on initial render. */+ open?: boolean;children: ComponentChildren;}/** A collapsible section within an ExpandableCard. */-export function ExpandableSection({ icon, label, className, children }: ExpandableSectionProps) {+export function ExpandableSection({ icon, label, className, open, children }: ExpandableSectionProps) {return (- <details className={`expandable-section ${className ?? ""}`}>+ <details className={`expandable-section ${className ?? ""}`} open={open}><summary className="expandable-section-summary"><span className={icon} /><span className="expandable-section-label">{label}</span>diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.tsx b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.tsxindex 4e1241806d..80f9d0d646 100644--- a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.tsx+++ b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.tsx@@ -4,7 +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 { EditNoteContentDiff, isSmallEdit, parseNoteContentEdits } from "./EditNoteContentDiff.js";import { ExpandableCard, ExpandableSection } from "./ExpandableCard.js";import type { ToolCall } from "./llm_chat_types.js";@@ -197,6 +197,7 @@ function ToolCallSection({ toolCall }: { toolCall: ToolCall }) {icon={toolCallIcon(toolCall)}label={<ToolCallLabel toolCall={toolCall} />}className={hasError ? "llm-chat-tool-call-error" : ""}+ open={noteContentEdits ? isSmallEdit(noteContentEdits) : undefined}><div className="llm-chat-tool-call-input">{noteContentEdits ? (