Skip to content

Build your personal knowledge base with Trilium Notes

feat(llm): let set_attribute change one of several same-name attributes

Elian Doranf0b1550d401f
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.ts
index 13b9465119..352d93c7c2 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
@@ -151,6 +151,51 @@ describe("attribute_tools", () => {
expect(note.getOwnedLabels("tag").map((l) => l.value)).toEqual([ "Blender", "AI" ]);
});
+ it("updates the attribute an attributeId names, keeping its place among same-name ones", () => {
+ const note = createNote("By id host");
+ const [ , ai ] = cls.init(() => [ note.addLabel("tag", "MCP"), note.addLabel("tag", "AI") ]);
+
+ const result = cls.init(() => getTool("set_attribute").execute({
+ noteId: note.noteId, type: "label", name: "tag", value: "Blender", attributeId: ai.attributeId
+ }));
+ expect(result).toEqual({
+ success: true, noteId: note.noteId, type: "label", name: "tag", value: "Blender", previousValue: "AI"
+ });
+ expect(note.getOwnedLabels("tag").map((l) => [ l.attributeId, l.value ]))
+ .toEqual([ [ expect.any(String), "MCP" ], [ ai.attributeId, "Blender" ] ]);
+ });
+
+ it("rejects an attributeId that is missing, on another note, of another name, or paired with add mode", () => {
+ const note = createNote("By id errors host");
+ const other = createNote("By id other host");
+ const [ tag, foreign ] = cls.init(() => [ note.addLabel("tag", "MCP"), other.addLabel("tag", "AI") ]);
+ const call = (args: object) => cls.init(() => getTool("set_attribute").execute({
+ noteId: note.noteId, type: "label", name: "tag", value: "x", ...args
+ }));
+
+ expect(call({ attributeId: "missing" })).toEqual({ error: "Attribute not found" });
+ expect(call({ attributeId: foreign.attributeId }))
+ .toEqual({ error: "Attribute does not belong to the specified note" });
+ expect(call({ attributeId: tag.attributeId, name: "status" }))
+ .toEqual({ error: "Attribute is #tag, not #status" });
+ expect(call({ attributeId: tag.attributeId, mode: "add" }))
+ .toEqual({ error: "attributeId updates an existing attribute, so it cannot be combined with mode \"add\"" });
+ expect(note.getOwnedLabels("tag").map((l) => l.value)).toEqual([ "MCP" ]);
+ expect(note.hasOwnedLabel("status")).toBe(false);
+ });
+
+ it("points to attributeId and add mode when set mode replaces one of several values", () => {
+ const note = createNote("Hint host");
+ cls.init(() => {
+ note.addLabel("tag", "MCP");
+ note.addLabel("tag", "AI");
+ });
+ const result = cls.init(() => getTool("set_attribute").execute({
+ noteId: note.noteId, type: "label", name: "tag", value: "Blender"
+ })) as { message: string };
+ expect(result.message).toContain("attributeId");
+ });
+
it("rejects a missing note, protected note, and missing relation target", () => {
expect(getTool("set_attribute").execute({ noteId: "missing", type: "label", name: "x" }))
.toEqual({ error: "Note not found" });
@@ -209,6 +254,22 @@ describe("attribute_tools", () => {
expect(note.getOwnedLabelValue("disabled:run")).toBe("hourly");
});
+ it("disables only the dangerous attribute an attributeId names", () => {
+ const note = createNote("Danger by id host");
+ const [ , hourly ] = cls.init(() => [ note.addLabel("run", "frontendStartup"), note.addLabel("run", "hourly") ]);
+
+ const result = cls.init(() => getTool("set_attribute").execute({
+ noteId: note.noteId, type: "label", name: "run", value: "daily", attributeId: hourly.attributeId
+ }));
+ expect(result).toMatchObject({
+ success: true, name: "disabled:run", value: "daily", disabled: true,
+ message: expect.stringContaining("removed the active #run=hourly it replaced")
+ });
+ expect(becca.getAttribute(hourly.attributeId)).toBeNull();
+ expect(note.getOwnedLabels("run").map((l) => l.value)).toEqual([ "frontendStartup" ]);
+ expect(note.getOwnedLabels("disabled:run").map((l) => l.value)).toEqual([ "daily" ]);
+ });
+
it("keeps active and disabled dangerous attributes when adding a disabled one", () => {
const note = createNote("Add danger host");
cls.init(() => {
diff --git a/packages/trilium-core/src/services/llm/tools/attribute_tools.ts b/packages/trilium-core/src/services/llm/tools/attribute_tools.ts
index 5d7b9193ae..2625d248e5 100644
--- a/packages/trilium-core/src/services/llm/tools/attribute_tools.ts
+++ b/packages/trilium-core/src/services/llm/tools/attribute_tools.ts
@@ -56,16 +56,17 @@ export const attributeTools = defineTools({
},
set_attribute: {
- description: "Add or update an attribute on a note. In the default mode 'set', an existing attribute with the same type and name is updated (the reply gives the value it replaced); otherwise a new one is created. Mode 'add' always creates a new attribute, so a note can carry several values of one name (e.g. #tag=MCP and #tag=Blender). Use type 'label' for text values, 'relation' for linking to another note (value must be a noteId). Attributes that run code (such as #run, #widget, ~renderNote) are saved with a 'disabled:' prefix (e.g. #disabled:run) and stay inactive until the user enables them; when that happens, tell the user which note to open and how to enable it (the reply says how).",
+ description: "Add or update an attribute on a note. In the default mode 'set', an existing attribute with the same type and name is updated (the reply gives the value it replaced); otherwise a new one is created. Mode 'add' always creates a new attribute, so a note can carry several values of one name (e.g. #tag=MCP and #tag=Blender). To change one of several same-name attributes, pass its attributeId from get_attributes. Use type 'label' for text values, 'relation' for linking to another note (value must be a noteId). Attributes that run code (such as #run, #widget, ~renderNote) are saved with a 'disabled:' prefix (e.g. #disabled:run) and stay inactive until the user enables them; when that happens, tell the user which note to open and how to enable it (the reply says how).",
inputSchema: z.object({
noteId: z.string().describe("The ID of the note"),
type: z.enum(["label", "relation"]).describe("The attribute type"),
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")
+ 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'")
}),
mutates: true,
- execute: ({ noteId, type, name, value = "", mode = "set" }) => {
+ execute: ({ noteId, type, name, value = "", mode = "set", attributeId }) => {
const note = getLlmNote(noteId);
if (!note) {
return { error: "Note not found" };
@@ -79,14 +80,32 @@ export const attributeTools = defineTools({
const sigil = type === "label" ? "#" : "~";
+ const target = attributeId ? becca.getAttribute(attributeId) : null;
+ if (attributeId) {
+ if (mode === "add") {
+ return { error: "attributeId updates an existing attribute, so it cannot be combined with mode \"add\"" };
+ }
+ if (!target || !isAttributeVisibleToLlm(target)) {
+ return { error: "Attribute not found" };
+ }
+ if (target.noteId !== note.noteId) {
+ return { error: "Attribute does not belong to the specified note" };
+ }
+ if (target.type !== type || target.name !== name.trim()) {
+ return { error: `Attribute is ${target.type === "label" ? "#" : "~"}${target.name}, not ${sigil}${name.trim()}` };
+ }
+ }
+
if (attributeService.isAttributeDangerous(type, name)) {
const disabledName = `disabled:${name.trim()}`;
// An active attribute left in place would keep running its old value while the reply says it is inactive.
- const activeAttributes = mode === "set" ? note.getOwnedAttributes(type, name.trim()) : [];
+ const activeAttributes = target ? [ target ]
+ : mode === "set" ? note.getOwnedAttributes(type, name.trim())
+ : [];
for (const attribute of activeAttributes) {
attribute.markAsDeleted();
}
- if (mode === "add") {
+ if (mode === "add" || target) {
note.addAttribute(type, disabledName, value);
} else {
note.setAttribute(type, disabledName, value);
@@ -100,9 +119,11 @@ export const attributeTools = defineTools({
value,
disabled: true,
message: `${sigil}${name.trim()} can run code, so it was saved as ${sigil}${disabledName} and is inactive. `
- + (activeAttributes.length
- ? `This removed the active ${sigil}${name.trim()}, so the note runs none of it until the user enables the new one. `
- : "")
+ + (target
+ ? `This removed the active ${sigil}${name.trim()}=${target.value} it replaced; other ${sigil}${name.trim()} attributes stay as they were. `
+ : activeAttributes.length
+ ? `This removed the active ${sigil}${name.trim()}, so the note runs none of it until the user enables the new one. `
+ : "")
+ `Tell the user which note to open and to enable it there once they have reviewed the code: `
+ `a render note shows an "Enable render note" button, other notes a toggle next to the badge beside the note title.`
};
@@ -115,6 +136,16 @@ export const attributeTools = defineTools({
return result;
}
+ if (target) {
+ const previousValue = target.value;
+ if (previousValue === value) {
+ return result;
+ }
+ target.value = value;
+ target.save();
+ return { ...result, previousValue };
+ }
+
const [ existing, ...others ] = note.getOwnedAttributes(type, name);
const previousValue = existing?.value;
note.setAttribute(type, name, value);
@@ -127,7 +158,7 @@ export const attributeTools = defineTools({
previousValue,
message: `Replaced ${sigil}${name}=${previousValue}. `
+ (others.length
- ? `${others.length} other ${sigil}${name} ${others.length === 1 ? "attribute was" : "attributes were"} left unchanged. `
+ ? `${others.length} other ${sigil}${name} ${others.length === 1 ? "attribute was" : "attributes were"} left unchanged; to change one of them, pass its attributeId. `
: "")
+ `To keep several values of ${sigil}${name}, use mode "add" instead.`
};