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
refactor(llm): tighten the #excludeFromLlm comments and drop the zero-branch guard in move_note
BeatLinke529f0fff288
diff --git a/packages/trilium-core/src/services/llm/tools/attachment_tools.spec.ts b/packages/trilium-core/src/services/llm/tools/attachment_tools.spec.tsindex b73f7dd72d..aab0697497 100644--- a/packages/trilium-core/src/services/llm/tools/attachment_tools.spec.ts+++ b/packages/trilium-core/src/services/llm/tools/attachment_tools.spec.ts@@ -6,8 +6,7 @@ const { getAttachmentMock, getBlobMock, getNoteMock } = vi.hoisted(() => ({getNoteMock: vi.fn()}));-// Attachment + blob lookups go through SQL in the real becca; partial-mock core-// so these lookups and the owner-note lookup return deterministic stubs while the rest of core is untouched.+// Partial-mock core so `getAttachment`, `getBlob` and the owner lookup `getNote` return stubs while the rest of core is untouched.vi.mock("../../../becca/becca.js", async (importOriginal) => {const actual = await importOriginal<typeof import("../../../becca/becca.js")>();return { default: { ...actual.default, getAttachment: getAttachmentMock, getBlob: getBlobMock, getNote: getNoteMock } };diff --git a/packages/trilium-core/src/services/llm/tools/helpers.spec.ts b/packages/trilium-core/src/services/llm/tools/helpers.spec.tsindex c1523f78ee..7f9801815d 100644--- a/packages/trilium-core/src/services/llm/tools/helpers.spec.ts+++ b/packages/trilium-core/src/services/llm/tools/helpers.spec.ts@@ -5,8 +5,8 @@ import { beforeEach, describe, expect, it, vi } from "vitest";const getBlobMock = vi.hoisted(() => vi.fn());const getNoteMock = vi.hoisted(() => vi.fn());-// getContentPreview / getNoteContentForLlm consult becca.getBlob (SQL-backed);-// partial-mock core so it returns deterministic blobs while markdown+// getContentPreview / getNoteContentForLlm consult becca.getBlob (SQL-backed) and getNoteMeta checks owners through becca.getNote;+// partial-mock core so both return deterministic stubs while markdown// import/export services keep their real implementations.vi.mock("../../../becca/becca.js", async (importOriginal) => {const actual = await importOriginal<typeof import("../../../becca/becca.js")>();diff --git a/packages/trilium-core/src/services/llm/tools/helpers.ts b/packages/trilium-core/src/services/llm/tools/helpers.tsindex fe0b8bd62a..484a8a0c70 100644--- a/packages/trilium-core/src/services/llm/tools/helpers.ts+++ b/packages/trilium-core/src/services/llm/tools/helpers.ts@@ -32,7 +32,7 @@ const SVG_SOURCE_RE = /^\s*(?:<\?xml[\s\S]*?\?>\s*)?(?:(?:<!--(?:(?!-->)[\s\S])*/** Note IDs that must not be deleted, moved, or cloned by the LLM. */export const PROTECTED_SYSTEM_NOTES = new Set(["root", "_hidden", "_share", "_lbRoot", "_globalNoteMap"]);-/** Label that keeps a note out of reach of the LLM tools; made inheritable, it covers the subtree too. */+/** Label that hides a note from the LLM tools, and its subtree too when the label is inheritable. */export const EXCLUDE_FROM_LLM_LABEL = "excludeFromLlm";/** Whether the LLM must not see a note: it owns or inherits a truthy {@link EXCLUDE_FROM_LLM_LABEL}. */@@ -40,23 +40,23 @@ export function isExcludedFromLlm(note: BNote) {return note.isLabelTruthy(EXCLUDE_FROM_LLM_LABEL);}-/** Look a note up for the LLM; an excluded note is reported as missing so its existence does not leak. */+/** Returns the note, or `null` when it is missing or excluded, so the LLM cannot tell the two apart. */export function getLlmNote(noteId: string): BNote | null {const note = becca.getNote(noteId);return note && !isExcludedFromLlm(note) ? note : null;}-/** The note's children the LLM may see. */+/** Returns the note's children that the LLM can see. */export function getLlmChildNotes(note: BNote) {return note.getChildNotes().filter((child) => !isExcludedFromLlm(child));}-/** The note's parents the LLM may see. */+/** Returns the note's parents that the LLM can see. */export function getLlmParentNotes(note: BNote) {return note.getParentNotes().filter((parent) => !isExcludedFromLlm(parent));}-/** Whether an attribute may be shown to the LLM: neither its owner nor, for a relation, its target is excluded. */+/** Whether the LLM can see an attribute: its owner and, for a relation, its target are not excluded. */export function isAttributeVisibleToLlm(attribute: BAttribute) {const owner = becca.getNote(attribute.noteId);if (!owner || isExcludedFromLlm(owner)) {@@ -66,7 +66,7 @@ export function isAttributeVisibleToLlm(attribute: BAttribute) {return !target || !isExcludedFromLlm(target);}-/** Look an attachment up for the LLM; one whose owning note (directly or through a revision) is excluded is reported as missing. */+/** Returns the attachment, or `null` when it is missing or its owning note (or the revision's note) is excluded. */export function getLlmAttachment(attachmentId: string): BAttachment | null {const attachment = becca.getAttachment(attachmentId);if (!attachment) {diff --git a/packages/trilium-core/src/services/llm/tools/hierarchy_tools.ts b/packages/trilium-core/src/services/llm/tools/hierarchy_tools.tsindex 0db5de9775..1fd3355d0a 100644--- a/packages/trilium-core/src/services/llm/tools/hierarchy_tools.ts+++ b/packages/trilium-core/src/services/llm/tools/hierarchy_tools.ts@@ -120,16 +120,8 @@ export const hierarchyTools = defineTools({return { error: "Cannot move note to a protected parent" };}- const branches = note.getParentBranches();- /* v8 ignore next 3 -- rare edge case, not worth simulating in a unit test:- an orphaned note (its last parent branch removed but the note itself not- yet deleted from Becca, e.g. during a sync race) can momentarily have zero- parent branches. The guard returns a clean error instead of crashing on branches[0]. */- if (branches.length === 0) {- return { error: "Note has no parent branches" };- }- // Move the first branch whose parent the LLM can see, so an excluded parent is neither changed nor revealed.- const branch = branches.find((b) => b.parentNote && !isExcludedFromLlm(b.parentNote));+ // Moving only a branch under a visible parent keeps an excluded parent unchanged and unnamed.+ const branch = note.getParentBranches().find((b) => b.parentNote && !isExcludedFromLlm(b.parentNote));if (!branch) {return { error: "Note has no parent it can be moved from" };}