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
fix(llm): let set_attribute tool set isInheritable (closes #12326)
Alex Castillo3783fe79ccfd
diff --git a/packages/trilium-core/src/services/llm/tools/attribute_tools.spec.ts b/packages/trilium-core/src/services/llm/tools/attribute_tools.spec.tsindex 352d93c7c2..9b745f8ba5 100644--- a/packages/trilium-core/src/services/llm/tools/attribute_tools.spec.ts+++ b/packages/trilium-core/src/services/llm/tools/attribute_tools.spec.ts@@ -102,6 +102,44 @@ describe("attribute_tools", () => {expect(result.value).toBe("");});+ it("creates an inheritable label when isInheritable is true", () => {+ const note = createNote("Inheritable host");+ const result = cls.init(() => getTool("set_attribute").execute({+ noteId: note.noteId, type: "label", name: "iconClass", value: "bx bx-star", isInheritable: true+ }));+ expect(result).toMatchObject({ success: true, isInheritable: true });+ expect(note.getOwnedAttributes("label", "iconClass")[0].isInheritable).toBe(true);+ });++ it("toggles isInheritable on an existing attribute and keeps it when omitted", () => {+ const note = createNote("Toggle host");+ cls.init(() => note.addLabel("tag", "MCP"));++ cls.init(() => getTool("set_attribute").execute({+ noteId: note.noteId, type: "label", name: "tag", value: "MCP", isInheritable: true+ }));+ expect(note.getOwnedLabels("tag")[0].isInheritable).toBe(true);++ cls.init(() => getTool("set_attribute").execute({+ noteId: note.noteId, type: "label", name: "tag", value: "AI"+ }));+ expect(note.getOwnedLabels("tag")[0]).toMatchObject({ value: "AI", isInheritable: true });++ const attributeId = note.getOwnedLabels("tag")[0].attributeId;+ cls.init(() => getTool("set_attribute").execute({+ noteId: note.noteId, type: "label", name: "tag", value: "AI", attributeId, isInheritable: false+ }));+ expect(note.getOwnedLabels("tag")[0].isInheritable).toBe(false);+ });++ it("creates an inheritable attribute in add mode", () => {+ const note = createNote("Add inheritable host");+ cls.init(() => getTool("set_attribute").execute({+ noteId: note.noteId, type: "label", name: "tag", value: "MCP", mode: "add", isInheritable: true+ }));+ expect(note.getOwnedLabels("tag")[0].isInheritable).toBe(true);+ });+it("appends same-name labels in add mode instead of replacing them", () => {const note = createNote("Add mode host");for (const value of [ "MCP", "AI", "Blender" ]) {diff --git a/packages/trilium-core/src/services/llm/tools/attribute_tools.ts b/packages/trilium-core/src/services/llm/tools/attribute_tools.tsindex 2625d248e5..2c2ea0e285 100644--- a/packages/trilium-core/src/services/llm/tools/attribute_tools.ts+++ b/packages/trilium-core/src/services/llm/tools/attribute_tools.ts@@ -6,9 +6,26 @@ import attributeService from "../../../services/attributes.js";import becca from "../../../becca/becca.js";import { z } from "zod";+import type BNote from "../../../becca/entities/bnote.js";+import type { AttributeType } from "@triliumnext/commons";+import { flag, getLlmNote, isAttributeVisibleToLlm } from "./helpers.js";import { defineTools } from "./tool_registry.js";+/** Like `note.setAttribute`, but also applies `isInheritable` when it is given. */+function upsertAttribute(note: BNote, type: AttributeType, name: string, value: string, isInheritable?: boolean) {+ const existing = note.getOwnedAttributes(type, name)[0];+ if (!existing) {+ note.addAttribute(type, name, value, isInheritable ?? false);+ } else if (existing.value !== value || (isInheritable !== undefined && existing.isInheritable !== isInheritable)) {+ existing.value = value;+ if (isInheritable !== undefined) {+ existing.isInheritable = isInheritable;+ }+ existing.save();+ }+}+export const attributeTools = defineTools({get_attributes: {description: "Get all attributes (labels and relations) of a note. Labels store text values; relations link to other notes by ID.",@@ -63,10 +80,11 @@ export const attributeTools = defineTools({name: z.string().describe("The attribute name"),value: z.string().optional().describe("The attribute value (for relations, this must be a target noteId)"),mode: z.enum(["set", "add"]).optional().describe("'set' (default) updates the existing attribute of this name; 'add' creates another one next to it, for multi-valued labels such as #tag"),- attributeId: z.string().optional().describe("The ID of the attribute to update (from get_attributes), when several share the name; type and name must match it. Not used with mode 'add'")+ attributeId: z.string().optional().describe("The ID of the attribute to update (from get_attributes), when several share the name; type and name must match it. Not used with mode 'add'"),+ isInheritable: z.boolean().optional().describe("Whether child notes inherit the attribute. New attributes are not inheritable by default; when updating an existing attribute, leaving this out keeps its current setting")}),mutates: true,- execute: ({ noteId, type, name, value = "", mode = "set", attributeId }) => {+ execute: ({ noteId, type, name, value = "", mode = "set", attributeId, isInheritable }) => {const note = getLlmNote(noteId);if (!note) {return { error: "Note not found" };@@ -106,9 +124,9 @@ export const attributeTools = defineTools({attribute.markAsDeleted();}if (mode === "add" || target) {- note.addAttribute(type, disabledName, value);+ note.addAttribute(type, disabledName, value, isInheritable ?? false);} else {- note.setAttribute(type, disabledName, value);+ upsertAttribute(note, type, disabledName, value, isInheritable);}return {@@ -117,6 +135,7 @@ export const attributeTools = defineTools({type,name: disabledName,value,+ ...(isInheritable === undefined ? {} : { isInheritable }),disabled: true,message: `${sigil}${name.trim()} can run code, so it was saved as ${sigil}${disabledName} and is inactive. `+ (target@@ -129,26 +148,29 @@ export const attributeTools = defineTools({};}- const result = { success: true, noteId: note.noteId, type, name, value };+ const result = { success: true, noteId: note.noteId, type, name, value, ...(isInheritable === undefined ? {} : { isInheritable }) };if (mode === "add") {- note.addAttribute(type, name, value);+ note.addAttribute(type, name, value, isInheritable ?? false);return result;}if (target) {const previousValue = target.value;- if (previousValue === value) {+ if (previousValue === value && (isInheritable === undefined || target.isInheritable === isInheritable)) {return result;}target.value = value;+ if (isInheritable !== undefined) {+ target.isInheritable = isInheritable;+ }target.save();- return { ...result, previousValue };+ return previousValue === value ? result : { ...result, previousValue };}const [ existing, ...others ] = note.getOwnedAttributes(type, name);const previousValue = existing?.value;- note.setAttribute(type, name, value);+ upsertAttribute(note, type, name, value, isInheritable);if (previousValue === undefined || previousValue === value) {return result;}