Skip to content

Build your personal knowledge base with Trilium Notes

feat(llm): save code-running attributes as disabled: instead of refusing them

Elian Doran4bbba2d6e852
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.spec.tsx b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.spec.tsx
index 03a0b00298..afd3f14a6f 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallCard.spec.tsx
@@ -559,9 +559,15 @@ describe("ToolCallCard", () => {
toolName: "delete_attribute",
input: { noteId: "n", attributeId: "a4" },
result: JSON.stringify({ success: true, attributeId: "a4", type: "label", name: "temp", value: "1" })
+ },
+ {
+ id: "5",
+ toolName: "set_attribute",
+ input: { noteId: "n", type: "label", name: "run", value: "frontendStartup" },
+ result: JSON.stringify({ success: true, noteId: "n", type: "label", name: "disabled:run", value: "frontendStartup", disabled: true })
}
]);
- const [ list, single, set, deleted ] = [ ...(target.querySelector(".llm-chat-tool-calls")?.children ?? []) ];
+ const [ list, single, set, deleted, disabled ] =[ ...(target.querySelector(".llm-chat-tool-calls")?.children ?? []) ];
const pills = (line: Element | undefined) => [ ...(line?.querySelectorAll(".llm-chat-attribute") ?? []) ].map(pill => pill.textContent);
expect(list instanceof HTMLDetailsElement).toBe(true);
@@ -576,6 +582,8 @@ describe("ToolCallCard", () => {
expect(pills(deleted)).toEqual([ "#temp=1" ]);
expect(deleted?.querySelector(".llm-chat-attribute")?.classList.contains("llm-chat-attribute-deleted")).toBe(true);
+
+ expect(pills(disabled)).toEqual([ "#disabled:run=frontendStartup" ]);
});
it("shows the icons an icon search found, and how many it left out", () => {
diff --git a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.tsx b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.tsx
index 76de20e837..bf2f128b18 100644
--- a/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.tsx
+++ b/apps/client/src/widgets/type_widgets/llm_chat/ToolCallViews.tsx
@@ -54,7 +54,9 @@ export function getToolCallView(toolCall: ToolCall): ToolCallView | null {
}
if (toolCall.toolName === "set_attribute") {
- const attribute = parseAttribute(toolCall.input);
+ // The result names the attribute as saved, which differs from the input when the tool prefixed it with `disabled:`.
+ const saved = toolCall.result ? parseAttribute(parseJson(toolCall.result)) : null;
+ const attribute = saved ?? parseAttribute(toolCall.input);
return attribute ? { lead: <AttributePill {...attribute} /> } : null;
}
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 33704cb5bc..554d5cddf1 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
@@ -173,7 +173,15 @@ class="admonition note">
<li>At attribute level:
<ul>
<li>Get the full list of attributes, or a specific attribute.</li>
- <li>Set the value of an attribute.</li>
+ <li>Set the value of an attribute.
+ <ul>
+ <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 remove the <code>disabled:</code> prefix
+ to activate it.</li>
+ </ul>
+ </li>
<li>Delete an attribute.</li>
</ul>
</li>
diff --git a/docs/User Guide/User Guide/AI.md b/docs/User Guide/User Guide/AI.md
index 3930c94866..734f9b490f 100644
--- a/docs/User Guide/User Guide/AI.md
+++ b/docs/User Guide/User Guide/AI.md
@@ -123,6 +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 remove the `disabled:` prefix to activate it.
* Delete an attribute.
* At tree level:
* Get the direct children of a note.
diff --git a/packages/trilium-core/src/assets/llm/skills/dashboards.md b/packages/trilium-core/src/assets/llm/skills/dashboards.md
index 428d57899d..0ed6fb8f25 100644
--- a/packages/trilium-core/src/assets/llm/skills/dashboards.md
+++ b/packages/trilium-core/src/assets/llm/skills/dashboards.md
@@ -30,7 +30,7 @@ For widgets that compute something, fetch data, or respond to clicks, use a rend
1. Create a note of type `render` as a **child of the dashboard** — this is the widget.
2. Create a `code` note with mime `text/jsx` as a **child of the render note**, exporting a default component.
-3. Ask the user to activate the widget by adding a `~renderNote` relation on the render note pointing to the JSX note. You CANNOT set this relation yourself — it enables code execution, so `set_attribute` refuses it as dangerous. Tell the user exactly what to do, e.g.: "Open the widget note '<render note title>', click the attribute area at the top, and add `~renderNote` pointing to '<JSX note title>'." Mention which JSX note to target by title.
+3. Call `set_attribute` on the render note with the relation `renderNote` pointing to the JSX note. Because it enables code execution, it is saved as `~disabled:renderNote` and the widget stays inactive until the user activates it. Tell the user exactly what to do, e.g.: "Open the widget note '<render note title>', review '<JSX note title>', then in the attribute area rename `~disabled:renderNote` to `~renderNote`."
JSX rules (load the `frontend_scripting` skill for the full API):
@@ -76,7 +76,7 @@ export default function RecentNotesWidget() {
## Anti-patterns (do NOT do this)
- ❌ Putting the JSX code note directly under the dashboard — it would render as a code widget showing its own source. The JSX note belongs under the render note.
-- ❌ Calling `set_attribute` with `~renderNote` — it will be rejected as dangerous. Ask the user to add the relation instead.
+- ❌ Telling the user the widget is ready after setting `~renderNote` — it is saved as `~disabled:renderNote` and renders nothing until the user removes the `disabled:` prefix.
- ❌ Directing the user to set `~renderNote` on the dashboard note itself — it belongs on the `render`-type child.
- ❌ Trying to write the `dashboard.json` attachment (or any view configuration) to lay out widgets — attachments cannot be modified by tools, and the dashboard picks up new widgets automatically; rely on auto-placement.
- ❌ Using `getActiveContextNote()` inside a widget's JSX to get "this widget's note" — when the dashboard is open, the active note is the dashboard, not the widget. Use `originEntity` (the render note) instead.
diff --git a/packages/trilium-core/src/assets/llm/skills/frontend_scripting.md b/packages/trilium-core/src/assets/llm/skills/frontend_scripting.md
index dc5ca661ea..16d7eae26b 100644
--- a/packages/trilium-core/src/assets/llm/skills/frontend_scripting.md
+++ b/packages/trilium-core/src/assets/llm/skills/frontend_scripting.md
@@ -12,7 +12,7 @@ CRITICAL: In JSX notes, always use top-level `import` statements (e.g. `import {
2. Add `#widget` label for widgets, or `#run=frontendStartup` for auto-run scripts.
3. For mobile, use `#run=mobileStartup` instead.
-NOTE: `#widget`, `#run` and `~renderNote` enable code execution, so `set_attribute` refuses them as dangerous. Create the code note yourself, then ask the user to add the activating attribute, telling them exactly which note to open and what to type into its attribute area.
+NOTE: `#widget`, `#run` and `~renderNote` enable code execution, so `set_attribute` saves them with a `disabled:` prefix (`#disabled:widget`, `#disabled:run`, `~disabled:renderNote`) and they stay inactive. Set them anyway, then tell the user which note to open and that, once they have reviewed the code, they activate it by removing the `disabled:` prefix in its attribute area.
## Script types
@@ -128,7 +128,7 @@ import { ActionButton, Button, LinkButton, Modal,
For rendering custom content inside a note:
1. Create a "render note" (type: Render Note) where you want the content to appear.
2. Create a JSX code note **as a child** of the render note, exporting a default component.
-3. Ask the user to add a `~renderNote` relation on the render note pointing to the child JSX note (you cannot set this dangerous attribute yourself).
+3. Set a `~renderNote` relation on the render note pointing to the child JSX note. It is saved as `~disabled:renderNote`, so ask the user to remove the `disabled:` prefix to activate it.
IMPORTANT: Always create the JSX code note as a child of the render note, not as a sibling or at the root. This keeps them organized together.
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 1df7f6ec6e..c190d3ff81 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,7 +102,7 @@ describe("attribute_tools", () => {
expect(result.value).toBe("");
});
- it("rejects a missing note, protected note, dangerous attribute, and missing relation target", () => {
+ 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" });
@@ -113,17 +113,34 @@ describe("attribute_tools", () => {
}))).toMatchObject({ error: expect.stringContaining("protected") });
protectedNote.isProtected = false;
- const dangerHost = createNote("Danger host");
- expect(cls.init(() => getTool("set_attribute").execute({
- noteId: dangerHost.noteId, type: "label", name: "run"
- }))).toMatchObject({ error: expect.stringContaining("dangerous") });
-
const relHost = createNote("Relation host");
expect(cls.init(() => getTool("set_attribute").execute({
noteId: relHost.noteId, type: "relation", name: "ref", value: "doesNotExist"
}))).toEqual({ error: "Target note not found for relation" });
});
+ it("prefixes a dangerous attribute with disabled: instead of activating it", () => {
+ const note = createNote("Danger host");
+ const result = cls.init(() => getTool("set_attribute").execute({
+ noteId: note.noteId, type: "label", name: " run ", value: "frontendStartup"
+ }));
+ expect(result).toMatchObject({
+ success: true, type: "label", name: "disabled:run", value: "frontendStartup",
+ disabled: true, message: expect.stringContaining("#run")
+ });
+ expect(note.getOwnedLabelValue("disabled:run")).toBe("frontendStartup");
+ expect(note.hasOwnedLabel("run")).toBe(false);
+
+ const relation = cls.init(() => getTool("set_attribute").execute({
+ noteId: note.noteId, type: "relation", name: "renderNote", value: "root"
+ }));
+ expect(relation).toMatchObject({
+ name: "disabled:renderNote", disabled: true, message: expect.stringContaining("~renderNote")
+ });
+ expect(note.getOwnedRelationValue("disabled:renderNote")).toBe("root");
+ expect(note.getOwnedRelation("renderNote")).toBeNull();
+ });
+
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 a64900ae5b..787eb5f8cc 100644
--- a/packages/trilium-core/src/services/llm/tools/attribute_tools.ts
+++ b/packages/trilium-core/src/services/llm/tools/attribute_tools.ts
@@ -56,7 +56,7 @@ export const attributeTools = defineTools({
},
set_attribute: {
- description: "Add or update an attribute on a note. If an attribute with the same type and name exists, it is updated; otherwise a new one is created. Use type 'label' for text values, 'relation' for linking to another note (value must be a noteId).",
+ description: "Add or update an attribute on a note. If an attribute with the same type and name exists, it is updated; otherwise a new one is created. 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 reviews the code and removes the prefix; when that happens, tell the user which note to open and which attribute to rename.",
inputSchema: z.object({
noteId: z.string().describe("The ID of the note"),
type: z.enum(["label", "relation"]).describe("The attribute type"),
@@ -72,13 +72,27 @@ export const attributeTools = defineTools({
if (note.isProtected) {
return { error: "Note is protected and cannot be modified" };
}
- if (attributeService.isAttributeDangerous(type, name)) {
- return { error: `Attribute '${name}' is potentially dangerous and cannot be set by the LLM` };
- }
if (type === "relation" && value && !becca.getNote(value)) {
return { error: "Target note not found for relation" };
}
+ if (attributeService.isAttributeDangerous(type, name)) {
+ const sigil = type === "label" ? "#" : "~";
+ const disabledName = `disabled:${name.trim()}`;
+ note.setAttribute(type, disabledName, value);
+
+ return {
+ success: true,
+ noteId: note.noteId,
+ type,
+ name: disabledName,
+ value,
+ disabled: true,
+ message: `${sigil}${name.trim()} can run code, so it was saved as ${sigil}${disabledName} and is inactive. `
+ + `Tell the user to review the code and, if they trust it, remove the "disabled:" prefix from the attribute to activate it.`
+ };
+ }
+
note.setAttribute(type, name, value);
return {