Skip to content

Build your personal knowledge base with Trilium Notes

docs(llm): teach skills about script context properties

Elian Doran747898c93bf6
diff --git a/apps/server/src/assets/llm/skills/backend_scripting.md b/apps/server/src/assets/llm/skills/backend_scripting.md
index 65f9cb72e3..07aa8ba67a 100644
--- a/apps/server/src/assets/llm/skills/backend_scripting.md
+++ b/apps/server/src/assets/llm/skills/backend_scripting.md
@@ -25,6 +25,11 @@ Note that most `api.*` methods (e.g. `api.getNote`, `api.searchForNotes`, `api.c
## Script API (`api` global)
+### Script context
+- `api.startNote` - note where the script execution started (the entry point of the script bundle; in C terms, the file with `main()`). All module notes loaded via `require()` share the same `startNote`. May be null when the execution came from the frontend via `runOnBackend()` (the frontend's `startNote` is preserved). `api.log()` messages are grouped under this note.
+- `api.currentNote` - note containing the source code currently executing (in C terms, `__FILE__`). Equal to `startNote` except inside child module notes loaded via `require()`. NOT the note open in the UI.
+- `api.originEntity` - entity whose event triggered this execution; `undefined` when the run was not event-driven (manual Execute button, `note.executeScript()`). For `~runOn*` relations see the table under "Events and triggers"; for scheduled scripts (`#run=hourly`/`#run=daily`) it is the script note itself; for `~searchScript` scripts it is the search note.
+
### Note retrieval
- `api.getNote(noteId)` - get note by ID
- `api.searchForNotes(query, searchParams)` - search notes (returns array)
diff --git a/apps/server/src/assets/llm/skills/dashboards.md b/apps/server/src/assets/llm/skills/dashboards.md
index dc8e102fe8..428d57899d 100644
--- a/apps/server/src/assets/llm/skills/dashboards.md
+++ b/apps/server/src/assets/llm/skills/dashboards.md
@@ -37,6 +37,7 @@ JSX rules (load the `frontend_scripting` skill for the full API):
- Use top-level ES `import` only; hooks come from `"trilium:preact"`, API methods from `"trilium:api"`.
- NEVER use `React`, `require()`, or `await import()` — Trilium uses Preact and JSX notes are ES modules.
- Export the component as `export default`.
+- To reference the widget's own note (e.g. to read its attributes or store data under it), use `originEntity` from `"trilium:api"` — it is the render note hosting the component. Do NOT use `getActiveContextNote()`: on a dashboard the active note is the dashboard itself, not the widget.
Example — a widget showing the number of notes created in the last 7 days:
@@ -78,5 +79,6 @@ export default function RecentNotesWidget() {
- ❌ Calling `set_attribute` with `~renderNote` — it will be rejected as dangerous. Ask the user to add the relation instead.
- ❌ 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.
- ❌ Setting `#viewType=dashboard` on a `text` note — the view type only applies to `book` (and search) notes.
- ❌ Prepending an emoji to a widget title to decorate it — find an icon with `search_icons` and set the `iconClass` label instead.
diff --git a/apps/server/src/assets/llm/skills/frontend_scripting.md b/apps/server/src/assets/llm/skills/frontend_scripting.md
index 24719483f4..0622538585 100644
--- a/apps/server/src/assets/llm/skills/frontend_scripting.md
+++ b/apps/server/src/assets/llm/skills/frontend_scripting.md
@@ -132,6 +132,13 @@ For rendering custom content inside a note:
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.
+IMPORTANT: To reference "this widget's note" from inside a render note component, use `originEntity` — for render notes it is the note being rendered (the one carrying the `~renderNote` relation). Do NOT use `getActiveContextNote()` for this: it returns the note the user currently has open in the UI, which is the render note's *host* (e.g. a dashboard or a book view), not the render note itself.
+
+```jsx
+import { originEntity } from "trilium:api";
+// originEntity = the render note hosting this component
+```
+
Stateless example:
```jsx
@@ -187,6 +194,11 @@ import { showMessage } from "trilium:api";
In JSX, use `import { method } from "trilium:api"`. In JavaScript (Trilium frontend), use the `api` global.
+### Script context
+- `startNote` - note where the script execution started (the entry point of the script bundle). All module notes loaded by the same execution share one `startNote`; `log()` messages are grouped under it.
+- `currentNote` - note containing the source code currently executing. Equal to `startNote` except inside child module notes. NOT the note open in the UI — that is `getActiveContextNote()`.
+- `originEntity` - note whose event triggered this execution, or `null`. Usually `null` (scripts started by the user or the UI). It is set for render notes — the note being rendered, i.e. the one carrying the `~renderNote` relation — and for `api.runOnFrontend()` calls from the backend (the backend's `originEntity`, if it was a note).
+
### Navigation & tabs
- `activateNote(notePath)` - navigate to a note
- `activateNewNote(notePath)` - navigate and wait for sync