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
feat(llm): tell Codex where Trilium's note tools are inside `exec`
Elian Doran1d723c24026b
diff --git a/apps/server/src/services/llm/providers/acp_agent.ts b/apps/server/src/services/llm/providers/acp_agent.tsindex 93a8f4ee55..f4c4153372 100644--- a/apps/server/src/services/llm/providers/acp_agent.ts+++ b/apps/server/src/services/llm/providers/acp_agent.ts@@ -269,6 +269,14 @@ export abstract class AcpAgentProvider implements LlmProvider {return denyPermission(request, this.logLabel);}+ /**+ * A line the system prompt of a chat with note access gains, for how this agent reaches+ * Trilium's note tools. None by default.+ */+ protected noteToolsHint(): string | undefined {+ return undefined;+ }+/*** The configuration of the chat turn running in `sessionId` on the pooled* agent, for a hook that decides a tool call outside the ACP connection.@@ -552,7 +560,13 @@ export abstract class AcpAgentProvider implements LlmProvider {const hasAttachments = Array.isArray(lastMessage.content) && lastMessage.content.some(p => p.type !== "text");const noteHint = config.contextNoteId ? buildNoteHint(config.contextNoteId, hasAttachments) : null;const prefix = [- isFreshSession ? wrapSystemInstructions(composeSystemPrompt(messages, { ...config, enableNoteTools: noteToolsEnabled })) : null,+ isFreshSession+ ? wrapSystemInstructions(composeSystemPrompt(+ messages,+ { ...config, enableNoteTools: noteToolsEnabled },+ noteToolsEnabled ? this.noteToolsHint() : undefined+ ))+ : null,(isFreshSession && history.length > 0) ? buildHistoryReplay(history) : null,noteHint].filter((s): s is string => Boolean(s)).join("\n\n");@@ -1151,11 +1165,12 @@ async function buildMcpServersConfig(noteToolsEnabled: boolean): Promise<AcpMcpS* system-prompt parameter, so it is delivered as a `<system_instructions>`* block leading the first prompt of each session.*/-function composeSystemPrompt(messages: LlmMessage[], config: LlmProviderConfig): string {+function composeSystemPrompt(messages: LlmMessage[], config: LlmProviderConfig, agentHint?: string): string {// buildSystemPrompt only returns undefined in its own documented-unreachable// no-parts case (the markdown hints are always appended)./* v8 ignore next */- return buildSystemPrompt(messages, config) ?? "";+ const systemPrompt = buildSystemPrompt(messages, config) ?? "";+ return agentHint ? `${systemPrompt}\n\n${agentHint}` : systemPrompt;}/** Wrap the Trilium system prompt for delivery inside the first user prompt. */diff --git a/apps/server/src/services/llm/providers/codex_agent.spec.ts b/apps/server/src/services/llm/providers/codex_agent.spec.tsindex 4d81c43259..1a983b1f82 100644--- a/apps/server/src/services/llm/providers/codex_agent.spec.ts+++ b/apps/server/src/services/llm/providers/codex_agent.spec.ts@@ -311,6 +311,20 @@ const ALLOWED = { outcome: { outcome: "selected", optionId: "allow_once" } };const DENIED = { outcome: { outcome: "selected", optionId: "cancel" } };describe("CodexAgentProvider note tools", () => {+ it("tells the model where its note tools are inside `exec`, only in a chat with note access", async () => {+ const firstPrompt = async (config: Record<string, unknown>) => {+ resetAcpAgentStateForTests();+ await collect(new CodexAgentProvider().chatChunks([{ role: "user", content: "hi" }], config));+ const prompt = FakeAcpClient.current?.requests.find(r => r.method === "session/prompt")?.params as { prompt?: { text?: string }[] } | undefined;+ return prompt?.prompt?.map(block => block.text ?? "").join("") ?? "";+ };++ const withTools = await firstPrompt({ enableNoteTools: true });+ expect(withTools).toContain("tools.mcp__trilium__get_note_content");+ expect(withTools.indexOf("tools.mcp__trilium__")).toBeLessThan(withTools.indexOf("</system_instructions>"));+ expect(await firstPrompt({})).not.toContain("tools.mcp__trilium__");+ });+it("runs Trilium's note tools, shown by their name and result, and denies calls to any other MCP server", async () => {const answers: unknown[] = [];FakeAcpClient.onPrompt = async client => {diff --git a/apps/server/src/services/llm/providers/codex_agent.ts b/apps/server/src/services/llm/providers/codex_agent.tsindex de9521342d..3dbb13f242 100644--- a/apps/server/src/services/llm/providers/codex_agent.ts+++ b/apps/server/src/services/llm/providers/codex_agent.ts@@ -145,6 +145,14 @@ export class CodexAgentProvider extends AcpAgentProvider {return buildCodexModelList(remote);}+ /**+ * Codex lists MCP tools to the model only inside its `exec` tool, so the system prompt names+ * them there. Without it, the model finds them only by searching `ALL_TOOLS` first.+ */+ protected noteToolsHint(): string {+ return `Trilium's note tools (search_notes, get_note, get_note_content, create_note and the rest) are inside your exec tool, as tools.mcp__${NOTE_TOOLS_MCP_SERVER_NAME}__<tool name>. Call them there directly, for example: const note = await tools.mcp__${NOTE_TOOLS_MCP_SERVER_NAME}__get_note_content({ noteId: "abc123" }); text(note). Do not search ALL_TOOLS for them first.`;+ }+protected decidePermission(request: AcpPermissionRequest,config?: LlmProviderConfig,