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
refactor(llm): extract system prompt into a dedicated module
Elian Dorane0afd8296ded
diff --git a/apps/server/src/services/llm/providers/base_provider.spec.ts b/apps/server/src/services/llm/providers/base_provider.spec.tsindex 5b13192475..6ac27825e9 100644--- a/apps/server/src/services/llm/providers/base_provider.spec.ts+++ b/apps/server/src/services/llm/providers/base_provider.spec.ts@@ -67,9 +67,6 @@ class TestProvider extends BaseProvider {}// Expose protected helpers for direct assertions.- public callBuildSystemPrompt(m: LlmMessage[], c: LlmProviderConfig) {- return this.buildSystemPrompt(m, c);- }public callApplyNoteHint(m: LlmMessage[], c: LlmProviderConfig) {return this.applyNoteHint(m, c);}@@ -262,60 +259,6 @@ describe("buildModelMessage", () => {});});-describe("BaseProvider system prompt", () => {- const provider = new TestProvider();-- it("uses config.systemPrompt over the message system prompt and emits the no-note-access notice", () => {- const prompt = provider.callBuildSystemPrompt(- [{ role: "system", content: "FROM MESSAGE" }],- { systemPrompt: "FROM CONFIG" }- )!;- expect(prompt).toContain("FROM CONFIG");- expect(prompt).not.toContain("FROM MESSAGE");- expect(prompt).toContain("do not have access to the user's notes");- });-- it("falls back to the system message content when no config prompt is set", () => {- const prompt = provider.callBuildSystemPrompt([{ role: "system", content: "FROM MESSAGE" }], {})!;- expect(prompt).toContain("FROM MESSAGE");- });-- it("includes the note-tools guidance when note tools are enabled", () => {- const prompt = provider.callBuildSystemPrompt([], { enableNoteTools: true, enableWebSearch: true })!;- expect(prompt).toContain("load_skill");- expect(prompt).toContain("wiki-link format [[noteId]]");- // Parallel tool-call hint appears when any tool capability is on.- expect(prompt).toContain("issue the tool calls in parallel");- // Web search enabled → no "web search is disabled" notice.- expect(prompt).not.toContain("do not have access to web search");- });-- it("emits the context-note-only notice when a context note is set but note tools are off", () => {- const prompt = provider.callBuildSystemPrompt([], { contextNoteId: "ctx" })!;- expect(prompt).toContain("cannot search or access other notes");- expect(prompt).toContain("do not have access to web search");- });-- it("always appends the markdown formatting hints", () => {- const prompt = provider.callBuildSystemPrompt([], {})!;- expect(prompt).toContain("Admonitions");- expect(prompt).toContain("Mermaid diagrams");- expect(prompt).toContain("Blockquotes");- expect(prompt).toContain("Tables");- expect(prompt).toContain("Collapsible blocks");- expect(prompt).toContain("Keyboard keys");- });-- it("lists the workspace's custom task-state markers so the model can recognize them", () => {- // No _taskStates container in the test becca → getTaskStates falls back to the defaults.- const prompt = provider.callBuildSystemPrompt([], {})!;- expect(prompt).toContain("Task lists");- expect(prompt).toContain("`- [/]` — Doing");- expect(prompt).toContain("`- [?]` — Maybe");- expect(prompt).toContain("`- [-]` — Cancelled");- });-});-describe("BaseProvider applyNoteHint", () => {const provider = new TestProvider();diff --git a/apps/server/src/services/llm/providers/base_provider.ts b/apps/server/src/services/llm/providers/base_provider.tsindex 441431b34e..4a63194692 100644--- a/apps/server/src/services/llm/providers/base_provider.ts+++ b/apps/server/src/services/llm/providers/base_provider.ts@@ -3,15 +3,16 @@* tool assembly, model pricing, and title generation.*/-import { isAnchorState, type LlmMessage, type LlmMessagePart } from "@triliumnext/commons";+import { type LlmMessage, type LlmMessagePart } from "@triliumnext/commons";+import { becca, getLog } from "@triliumnext/core";import { decodeUtf8 } from "@triliumnext/core/src/services/utils/binary.js";import { type FilePart, generateText, type ImagePart, type LanguageModel, type ModelMessage, stepCountIs, streamText, type SystemModelMessage, type TextPart, type ToolSet } from "ai";import { dump } from "js-yaml";-import { becca, getLog, task_states } from "@triliumnext/core";import { getNoteMeta,SYSTEM_PROMPT_LIMITS } from "../tools/helpers.js";import { allToolRegistries } from "../tools/index.js";import type { LlmProvider, LlmProviderConfig, ModelInfo, ModelPricing, StreamResult } from "../types.js";+import { buildSystemPrompt as composeSystemPrompt } from "./system_prompt.js";const DEFAULT_MAX_TOKENS = 8096;const TITLE_MAX_TOKENS = 30;@@ -162,21 +163,6 @@ export function buildModelList(baseModels: Omit<ModelInfo, "costMultiplier">[]):return { models, pricing };}-/**- * The task-list formatting hint, extended with the workspace's user-defined multi-state checkboxes so- * the model recognizes their markers (e.g. `- [/]` = "Doing") in the user's notes and can produce them.- * Falls back to just the native open/completed states when no custom states are configured.- */-function buildTaskListHint(): string {- const base = "**Task lists** — use `- [ ]` for an open task and `- [x]` for a completed one.";- const custom = task_states.getTaskStates().filter(s => !isAnchorState(s.name) && !s.isHidden && s.markdownSymbol);- if (custom.length === 0) {- return base;- }- const lines = custom.map(s => `- \`- [${s.markdownSymbol}]\` — ${s.title}${s.isCompleted ? " (completed)" : ""}`);- return `${base} This workspace also defines extra task states — recognize these markers in the user's notes, and use them when a task fits:\n${lines.join("\n")}`;-}-export abstract class BaseProvider implements LlmProvider {abstract name: string;@@ -189,95 +175,12 @@ export abstract class BaseProvider implements LlmProvider {protected abstract createModel(modelId: string): LanguageModel;/**- * Build the system prompt with note hints and skills summary.+ * Build the system prompt. Delegates to the shared `system_prompt` module;+ * kept as an overridable method so providers can post-process the result+ * (e.g. Google appends a web-search/note-tool conflict notice).*/protected buildSystemPrompt(messages: LlmMessage[], config: LlmProviderConfig): string | undefined {- const parts: string[] = [];-- // Base system prompt from config or messages. System messages only ever- // carry string content — multimodal parts apply to user/assistant turns.- const systemMessage = messages.find(m => m.role === "system");- const messageSystemPrompt = typeof systemMessage?.content === "string" ? systemMessage.content : undefined;- const basePrompt = config.systemPrompt || messageSystemPrompt;- if (basePrompt) {- parts.push(basePrompt);- }-- // Note tools hint- if (config.enableNoteTools) {- parts.push(- [- `Before calling create_note (or any write tool) for Trilium-specific code or queries, you MUST call load_skill first — guessing the API produces broken code that doesn't run:`,- `- Frontend code notes (mime "text/jsx" or "application/javascript;env=frontend") or render notes (type "render") → load_skill with name "frontend_scripting". Trilium uses Preact, NOT React, with imports from "trilium:preact".`,- `- Backend code notes (mime "application/javascript;env=backend") → load_skill with name "backend_scripting".`,- `- Search queries using boolean logic, attribute filters, relations, ordering, or regex → load_skill with name "search_syntax".`,- `Loading is one cheap tool call. Skipping it wastes the user's time.`- ].join("\n")- );- parts.push(- `When referring to notes in your responses, use the wiki-link format [[noteId]] to create clickable internal links. Use the note ID (not the title) from tool results. The link will automatically display the note's title and icon, so don't repeat the title in your text. For example: "You can find more details in [[ZjSfLhzlqNY6]]" instead of "You can find more details in the Meeting Notes note ([[ZjSfLhzlqNY6]])".`- );- parts.push(- `Do not create, modify, or delete notes unless the user explicitly asks you to (e.g. "create a note", "save this to a note"). The chat supports rich Markdown rendering including code blocks, math equations, mermaid diagrams, and tables — so always present content directly in your response rather than creating a note for it. For example, if asked to "visualize an algorithm", render a mermaid diagram in the chat, don't create a note.`- );- parts.push(- `After a successful write tool call (set_note_content, append_to_note, edit_note_content, create_note), the tool's result already contains the resulting content of the note. Do not call get_note_content to verify the write — trust the returned content.`- );- parts.push(- `Never prepend emojis or other decorative characters to note titles. To give a note a visual marker, find a fitting icon with search_icons and assign it via set_attribute as the note's 'iconClass' label (e.g. value 'bx bx-rocket').`- );- } else if (config.contextNoteId) {- parts.push(- `You can see the current note's metadata above, but you cannot search or access other notes. If the user asks about other notes, inform them that "Note access" is disabled and they need to enable it in the chat settings (click on the model name dropdown and toggle "Note access").`- );- } else {- parts.push(- `You do not have access to the user's notes. If the user asks about their notes, inform them that "Note access" is disabled and they need to enable it in the chat settings (click on the model name dropdown and toggle "Note access").`- );- }-- // Web search hint- if (!config.enableWebSearch) {- parts.push(- `You do not have access to web search. If the user asks for current/real-time information, news, or anything that requires searching the web, inform them that "Web search" is disabled and they need to enable it in the chat settings (click on the model name dropdown and toggle "Web search").`- );- }-- // Parallel tool-call hint- if (config.enableNoteTools || config.enableWebSearch) {- parts.push(- `When you need several independent pieces of information, issue the tool calls in parallel within the same turn instead of waiting for each result before requesting the next. Only chain calls sequentially when a later call genuinely depends on the output of an earlier one.`- );- }-- // Markdown formatting hints- parts.push(- `Your responses are rendered as Markdown with extended features. Use them when appropriate:\n\n`- + `**Admonitions** — GitHub-style callout blocks. Use sparingly, only when a plain paragraph would under-sell the point:\n`- + `- \`> [!NOTE]\` — neutral side information worth highlighting\n`- + `- \`> [!TIP]\` — an optional improvement or shortcut\n`- + `- \`> [!IMPORTANT]\` — information the user should not miss\n`- + `- \`> [!WARNING]\` — something that may cause problems or surprise\n`- + `- \`> [!CAUTION]\` — a destructive or irreversible action\n`- + `Syntax: the marker must be on its own line, and every content line must start with \`>\`.\n\n`- + `**Blockquotes** — prefix lines with \`>\` (without a \`[!TYPE]\` marker) to quote text.\n\n`- + `**Math equations** — KaTeX (LaTeX subset). Use \`$...$\` for inline math and \`$$...$$\` for display (block) math. Example: \`$E = mc^2$\` or:\n`- + `$$\n\\int_0^\\infty e^{-x^2} dx = \\frac{\\sqrt{\\pi}}{2}\n$$\n\n`- + `**Mermaid diagrams** — use a fenced code block with the \`mermaid\` language tag. Example:\n`- + "```mermaid\ngraph LR\n A --> B\n```\n\n"- + `**Code blocks** — use fenced code blocks with a language tag for syntax highlighting (e.g. \`\`\`js, \`\`\`python).\n\n`- + `**Tables** — GitHub-style pipe tables: a header row, a \`---\` separator row, then the data rows.\n\n`- + `**Collapsible blocks** — use the standard HTML \`<details>\`/\`<summary>\` form; the \`<summary>\` is the always-visible title. Placed back-to-back with nothing between them, consecutive collapsible blocks are grouped into an accordion — handy when presenting several options or alternatives the user can expand one at a time. Example:\n`- + `<details><summary>Option A</summary>\nDetails about the first option.\n</details>\n<details><summary>Option B</summary>\nDetails about the second option.\n</details>\n\n`- + `**Footnotes** — use \`[^1]\` in text and \`[^1]: explanation\` at the bottom.\n\n`- + `**Keyboard keys** — wrap each key in a \`<kbd>\` tag when documenting shortcuts, e.g. \`<kbd>Ctrl</kbd>+<kbd>Alt</kbd>+<kbd>Del</kbd>\`.\n\n`- + buildTaskListHint()- );-- // The markdown formatting hints above are pushed unconditionally, so- // `parts` is never empty — the `: undefined` arm is unreachable defence.- /* v8 ignore next */- return parts.length > 0 ? parts.join("\n\n") : undefined;+ return composeSystemPrompt(messages, config);}/**diff --git a/apps/server/src/services/llm/providers/system_prompt.spec.ts b/apps/server/src/services/llm/providers/system_prompt.spec.tsnew file mode 100644index 0000000000..2646e5775b--- /dev/null+++ b/apps/server/src/services/llm/providers/system_prompt.spec.ts@@ -0,0 +1,55 @@+import { describe, expect, it } from "vitest";++import { buildSystemPrompt } from "./system_prompt.js";++describe("buildSystemPrompt", () => {+ it("uses config.systemPrompt over the message system prompt and emits the no-note-access notice", () => {+ const prompt = buildSystemPrompt(+ [{ role: "system", content: "FROM MESSAGE" }],+ { systemPrompt: "FROM CONFIG" }+ ) ?? "";+ expect(prompt).toContain("FROM CONFIG");+ expect(prompt).not.toContain("FROM MESSAGE");+ expect(prompt).toContain("do not have access to the user's notes");+ });++ it("falls back to the system message content when no config prompt is set", () => {+ const prompt = buildSystemPrompt([{ role: "system", content: "FROM MESSAGE" }], {}) ?? "";+ expect(prompt).toContain("FROM MESSAGE");+ });++ it("includes the note-tools guidance when note tools are enabled", () => {+ const prompt = buildSystemPrompt([], { enableNoteTools: true, enableWebSearch: true }) ?? "";+ expect(prompt).toContain("load_skill");+ expect(prompt).toContain("wiki-link format [[noteId]]");+ // Parallel tool-call hint appears when any tool capability is on.+ expect(prompt).toContain("issue the tool calls in parallel");+ // Web search enabled → no "web search is disabled" notice.+ expect(prompt).not.toContain("do not have access to web search");+ });++ it("emits the context-note-only notice when a context note is set but note tools are off", () => {+ const prompt = buildSystemPrompt([], { contextNoteId: "ctx" }) ?? "";+ expect(prompt).toContain("cannot search or access other notes");+ expect(prompt).toContain("do not have access to web search");+ });++ it("always appends the markdown formatting hints", () => {+ const prompt = buildSystemPrompt([], {}) ?? "";+ expect(prompt).toContain("Admonitions");+ expect(prompt).toContain("Mermaid diagrams");+ expect(prompt).toContain("Blockquotes");+ expect(prompt).toContain("Tables");+ expect(prompt).toContain("Collapsible blocks");+ expect(prompt).toContain("Keyboard keys");+ });++ it("lists the workspace's custom task-state markers so the model can recognize them", () => {+ // No _taskStates container in the test becca → getTaskStates falls back to the defaults.+ const prompt = buildSystemPrompt([], {}) ?? "";+ expect(prompt).toContain("Task lists");+ expect(prompt).toContain("`- [/]` — Doing");+ expect(prompt).toContain("`- [?]` — Maybe");+ expect(prompt).toContain("`- [-]` — Cancelled");+ });+});diff --git a/apps/server/src/services/llm/providers/system_prompt.ts b/apps/server/src/services/llm/providers/system_prompt.tsnew file mode 100644index 0000000000..5702634f36--- /dev/null+++ b/apps/server/src/services/llm/providers/system_prompt.ts@@ -0,0 +1,126 @@+/**+ * Builds the Trilium-specific system prompt shared by every LLM provider:+ * the note-tools guidance (when to load_skill, the [[noteId]] wiki-link+ * convention, write-safety rules), the note/web-access notices, and the+ * Markdown-rendering capabilities of the chat surface.+ *+ * Kept provider-agnostic (no `ai` SDK, no becca beyond task states) so it can+ * be reused by both the AI-SDK providers and the Claude Agent provider, which+ * runs its own loop and needs the same prompt to reach behavioural parity.+ */++import { isAnchorState, type LlmMessage } from "@triliumnext/commons";+import { task_states } from "@triliumnext/core";++import type { LlmProviderConfig } from "../types.js";++/**+ * Build the system prompt from the conversation's system message / config plus+ * the note-tools, web-search, and Markdown-formatting guidance.+ *+ * Returns `undefined` only in the (unreachable) case of no parts — the+ * Markdown hints are always appended.+ */+export function buildSystemPrompt(messages: LlmMessage[], config: LlmProviderConfig): string | undefined {+ const parts: string[] = [];++ // Base system prompt from config or messages. System messages only ever+ // carry string content — multimodal parts apply to user/assistant turns.+ const systemMessage = messages.find(m => m.role === "system");+ const messageSystemPrompt = typeof systemMessage?.content === "string" ? systemMessage.content : undefined;+ const basePrompt = config.systemPrompt || messageSystemPrompt;+ if (basePrompt) {+ parts.push(basePrompt);+ }++ // Note tools hint+ if (config.enableNoteTools) {+ parts.push(+ [+ `Before calling create_note (or any write tool) for Trilium-specific code or queries, you MUST call load_skill first — guessing the API produces broken code that doesn't run:`,+ `- Frontend code notes (mime "text/jsx" or "application/javascript;env=frontend") or render notes (type "render") → load_skill with name "frontend_scripting". Trilium uses Preact, NOT React, with imports from "trilium:preact".`,+ `- Backend code notes (mime "application/javascript;env=backend") → load_skill with name "backend_scripting".`,+ `- Search queries using boolean logic, attribute filters, relations, ordering, or regex → load_skill with name "search_syntax".`,+ `Loading is one cheap tool call. Skipping it wastes the user's time.`+ ].join("\n")+ );+ parts.push(+ `When referring to notes in your responses, use the wiki-link format [[noteId]] to create clickable internal links. Use the note ID (not the title) from tool results. The link will automatically display the note's title and icon, so don't repeat the title in your text. For example: "You can find more details in [[ZjSfLhzlqNY6]]" instead of "You can find more details in the Meeting Notes note ([[ZjSfLhzlqNY6]])".`+ );+ parts.push(+ `Do not create, modify, or delete notes unless the user explicitly asks you to (e.g. "create a note", "save this to a note"). The chat supports rich Markdown rendering including code blocks, math equations, mermaid diagrams, and tables — so always present content directly in your response rather than creating a note for it. For example, if asked to "visualize an algorithm", render a mermaid diagram in the chat, don't create a note.`+ );+ parts.push(+ `After a successful write tool call (set_note_content, append_to_note, edit_note_content, create_note), the tool's result already contains the resulting content of the note. Do not call get_note_content to verify the write — trust the returned content.`+ );+ parts.push(+ `Never prepend emojis or other decorative characters to note titles. To give a note a visual marker, find a fitting icon with search_icons and assign it via set_attribute as the note's 'iconClass' label (e.g. value 'bx bx-rocket').`+ );+ } else if (config.contextNoteId) {+ parts.push(+ `You can see the current note's metadata above, but you cannot search or access other notes. If the user asks about other notes, inform them that "Note access" is disabled and they need to enable it in the chat settings (click on the model name dropdown and toggle "Note access").`+ );+ } else {+ parts.push(+ `You do not have access to the user's notes. If the user asks about their notes, inform them that "Note access" is disabled and they need to enable it in the chat settings (click on the model name dropdown and toggle "Note access").`+ );+ }++ // Web search hint+ if (!config.enableWebSearch) {+ parts.push(+ `You do not have access to web search. If the user asks for current/real-time information, news, or anything that requires searching the web, inform them that "Web search" is disabled and they need to enable it in the chat settings (click on the model name dropdown and toggle "Web search").`+ );+ }++ // Parallel tool-call hint+ if (config.enableNoteTools || config.enableWebSearch) {+ parts.push(+ `When you need several independent pieces of information, issue the tool calls in parallel within the same turn instead of waiting for each result before requesting the next. Only chain calls sequentially when a later call genuinely depends on the output of an earlier one.`+ );+ }++ // Markdown formatting hints+ parts.push(+ `Your responses are rendered as Markdown with extended features. Use them when appropriate:\n\n`+ + `**Admonitions** — GitHub-style callout blocks. Use sparingly, only when a plain paragraph would under-sell the point:\n`+ + `- \`> [!NOTE]\` — neutral side information worth highlighting\n`+ + `- \`> [!TIP]\` — an optional improvement or shortcut\n`+ + `- \`> [!IMPORTANT]\` — information the user should not miss\n`+ + `- \`> [!WARNING]\` — something that may cause problems or surprise\n`+ + `- \`> [!CAUTION]\` — a destructive or irreversible action\n`+ + `Syntax: the marker must be on its own line, and every content line must start with \`>\`.\n\n`+ + `**Blockquotes** — prefix lines with \`>\` (without a \`[!TYPE]\` marker) to quote text.\n\n`+ + `**Math equations** — KaTeX (LaTeX subset). Use \`$...$\` for inline math and \`$$...$$\` for display (block) math. Example: \`$E = mc^2$\` or:\n`+ + `$$\n\\int_0^\\infty e^{-x^2} dx = \\frac{\\sqrt{\\pi}}{2}\n$$\n\n`+ + `**Mermaid diagrams** — use a fenced code block with the \`mermaid\` language tag. Example:\n`+ + "```mermaid\ngraph LR\n A --> B\n```\n\n"+ + `**Code blocks** — use fenced code blocks with a language tag for syntax highlighting (e.g. \`\`\`js, \`\`\`python).\n\n`+ + `**Tables** — GitHub-style pipe tables: a header row, a \`---\` separator row, then the data rows.\n\n`+ + `**Collapsible blocks** — use the standard HTML \`<details>\`/\`<summary>\` form; the \`<summary>\` is the always-visible title. Placed back-to-back with nothing between them, consecutive collapsible blocks are grouped into an accordion — handy when presenting several options or alternatives the user can expand one at a time. Example:\n`+ + `<details><summary>Option A</summary>\nDetails about the first option.\n</details>\n<details><summary>Option B</summary>\nDetails about the second option.\n</details>\n\n`+ + `**Footnotes** — use \`[^1]\` in text and \`[^1]: explanation\` at the bottom.\n\n`+ + `**Keyboard keys** — wrap each key in a \`<kbd>\` tag when documenting shortcuts, e.g. \`<kbd>Ctrl</kbd>+<kbd>Alt</kbd>+<kbd>Del</kbd>\`.\n\n`+ + buildTaskListHint()+ );++ // The markdown formatting hints above are pushed unconditionally, so+ // `parts` is never empty — the `: undefined` arm is unreachable defence.+ /* v8 ignore next */+ return parts.length > 0 ? parts.join("\n\n") : undefined;+}++/**+ * The task-list formatting hint, extended with the workspace's user-defined multi-state checkboxes so+ * the model recognizes their markers (e.g. `- [/]` = "Doing") in the user's notes and can produce them.+ * Falls back to just the native open/completed states when no custom states are configured.+ */+function buildTaskListHint(): string {+ const base = "**Task lists** — use `- [ ]` for an open task and `- [x]` for a completed one.";+ const custom = task_states.getTaskStates().filter(s => !isAnchorState(s.name) && !s.isHidden && s.markdownSymbol);+ if (custom.length === 0) {+ return base;+ }+ const lines = custom.map(s => `- \`- [${s.markdownSymbol}]\` — ${s.title}${s.isCompleted ? " (completed)" : ""}`);+ return `${base} This workspace also defines extra task states — recognize these markers in the user's notes, and use them when a task fits:\n${lines.join("\n")}`;+}