Skip to content

Build your personal knowledge base with Trilium Notes

fix(llm): remove an active attribute that set_attribute saves as disabled:

Elian Doran3a704c53ef87
diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/AI.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/AI.html
index 4ec0670e95..230cab1447 100644
--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/AI.html
+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/AI.html
@@ -178,10 +178,12 @@ class="admonition note">
<li>An attribute that runs code, such as <code>#run</code>, <code>#widget</code> or <code>~renderNote</code>,
is saved with a <code>disabled:</code> prefix (for example <code>#disabled:widget</code>),
the same way a safe import disables it (see <a class="reference-link" href="#root/_help_YzMcWlCVeW09">Active content</a>).
- The AI tells you which note it is on; review the code, then enable it with
- the toggle next to the active content badge beside the note's title or,
- for a <a class="reference-link" href="#root/_help_HcABDtFCkbFN">Render Note</a>,
- the <em>Enable render note</em> button in the note.</li>
+ If the note already had that attribute enabled, the AI removes it, so none
+ of it runs until you enable the new value. The AI tells you which note
+ it is on; review the code, then enable it with the toggle next to the active
+ content badge beside the note's title or, for a <a class="reference-link"
+ href="#root/_help_HcABDtFCkbFN">Render Note</a>, the <em>Enable render note</em> button
+ in the note.</li>
</ul>
</li>
<li>Delete an attribute.</li>
diff --git a/docs/User Guide/User Guide/AI.md b/docs/User Guide/User Guide/AI.md
index ee3a4e8509..54138ba64b 100644
--- a/docs/User Guide/User Guide/AI.md
+++ b/docs/User Guide/User Guide/AI.md
@@ -123,7 +123,7 @@ Here are a few tools that Trilium provides for the LLM:
* At attribute level:
* Get the full list of attributes, or a specific attribute.
* Set the value of an attribute.
- * An attribute that runs code, such as `#run`, `#widget` or `~renderNote`, is saved with a `disabled:` prefix (for example `#disabled:widget`), the same way a safe import disables it (see <a class="reference-link" href="Basic%20Concepts%20and%20Features/Active%20content.md">Active content</a>). The AI tells you which note it is on; review the code, then enable it with the toggle next to the active content badge beside the note's title or, for a <a class="reference-link" href="Note%20Types/Render%20Note.md">Render Note</a>, the _Enable render note_ button in the note.
+ * An attribute that runs code, such as `#run`, `#widget` or `~renderNote`, is saved with a `disabled:` prefix (for example `#disabled:widget`), the same way a safe import disables it (see <a class="reference-link" href="Basic%20Concepts%20and%20Features/Active%20content.md">Active content</a>). If the note already had that attribute enabled, the AI removes it, so none of it runs until you enable the new value. The AI tells you which note it is on; review the code, then enable it with the toggle next to the active content badge beside the note's title or, for a <a class="reference-link" href="Note%20Types/Render%20Note.md">Render Note</a>, the _Enable render note_ button in the note.
* Delete an attribute.
* At tree level:
* Get the direct children of a note.
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 c190d3ff81..71cae138e7 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
@@ -141,6 +141,25 @@ describe("attribute_tools", () => {
expect(note.getOwnedRelation("renderNote")).toBeNull();
});
+ it("removes an active dangerous attribute it replaces with a disabled one", () => {
+ const note = createNote("Active danger host");
+ cls.init(() => {
+ note.addLabel("run", "frontendStartup");
+ note.addLabel("run", "backendStartup");
+ });
+ expect(note.getOwnedLabels("run")).toHaveLength(2);
+
+ const result = cls.init(() => getTool("set_attribute").execute({
+ noteId: note.noteId, type: "label", name: "run", value: "hourly"
+ }));
+ expect(result).toMatchObject({
+ success: true, name: "disabled:run", disabled: true,
+ message: expect.stringContaining("removed the active #run")
+ });
+ expect(note.getOwnedLabels("run")).toHaveLength(0);
+ expect(note.getOwnedLabelValue("disabled:run")).toBe("hourly");
+ });
+
it("creates a relation to an existing target note", () => {
const note = createNote("Relation ok host");
const result = cls.init(() => getTool("set_attribute").execute({
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 79246ac428..2ce8516285 100644
--- a/packages/trilium-core/src/services/llm/tools/attribute_tools.ts
+++ b/packages/trilium-core/src/services/llm/tools/attribute_tools.ts
@@ -79,6 +79,11 @@ export const attributeTools = defineTools({
if (attributeService.isAttributeDangerous(type, name)) {
const sigil = type === "label" ? "#" : "~";
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 = note.getOwnedAttributes(type, name.trim());
+ for (const attribute of activeAttributes) {
+ attribute.markAsDeleted();
+ }
note.setAttribute(type, disabledName, value);
return {
@@ -89,6 +94,9 @@ 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. `
+ : "")
+ `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.`
};