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): move the message-building modules out of providers/
Elian Doran61275513588f
diff --git a/apps/server/src/services/llm/providers/attachment_content.ts b/apps/server/src/services/llm/attachment_content.tssimilarity index 97%rename from apps/server/src/services/llm/providers/attachment_content.tsrename to apps/server/src/services/llm/attachment_content.tsindex 568d26d384..6acf896ce5 100644--- a/apps/server/src/services/llm/providers/attachment_content.ts+++ b/apps/server/src/services/llm/attachment_content.ts@@ -1,7 +1,7 @@/*** Provider-neutral resolution of multimodal message parts.*- * Both the AI-SDK providers ({@link ../base_provider}) and the Claude Agent+ * Both the AI-SDK providers ({@link ./providers/base_provider}) and the Claude Agent* provider need the same thing from an attachment part: load the bytes out of* Becca and apply the conventions that don't depend on the provider — an SVG is* rendered as its XML source (no vision model accepts SVG, but all can read thediff --git a/apps/server/src/services/llm/providers/note_hint.ts b/apps/server/src/services/llm/note_hint.tssimilarity index 95%rename from apps/server/src/services/llm/providers/note_hint.tsrename to apps/server/src/services/llm/note_hint.tsindex ea306f93da..8687ec9bc4 100644--- a/apps/server/src/services/llm/providers/note_hint.ts+++ b/apps/server/src/services/llm/note_hint.ts@@ -9,7 +9,7 @@import { becca } from "@triliumnext/core";import { dump } from "js-yaml";-import { getNoteMeta, SYSTEM_PROMPT_LIMITS } from "../tools/helpers.js";+import { getNoteMeta, SYSTEM_PROMPT_LIMITS } from "./tools/helpers.js";/*** Build a context hint about the current note with full metadata (same shape asdiff --git a/apps/server/src/services/llm/providers/base_provider.ts b/apps/server/src/services/llm/providers/base_provider.tsindex 92ae3e3c19..9cfc163f8c 100644--- a/apps/server/src/services/llm/providers/base_provider.ts+++ b/apps/server/src/services/llm/providers/base_provider.ts@@ -6,12 +6,12 @@import { type LlmMessage, type LlmMessagePart } from "@triliumnext/commons";import { type FilePart, generateText, type ImagePart, type LanguageModel, type ModelMessage, stepCountIs, streamText, type SystemModelMessage, type TextPart, type ToolSet } from "ai";+import { resolveAttachmentPart } from "../attachment_content.js";+import { buildNoteHint } from "../note_hint.js";+import { buildSystemPrompt as composeSystemPrompt } from "../system_prompt.js";import { allToolRegistries } from "../tools/index.js";import type { LlmProvider, LlmProviderConfig, ModelInfo, ModelPricing, StreamResult } from "../types.js";-import { resolveAttachmentPart } from "./attachment_content.js";import MODEL_PRICES_JSON from "./model_prices.json" with { type: "json" };-import { buildNoteHint } from "./note_hint.js";-import { buildSystemPrompt as composeSystemPrompt } from "./system_prompt.js";const DEFAULT_MAX_TOKENS = 8096;const TITLE_MAX_TOKENS = 30;diff --git a/apps/server/src/services/llm/providers/claude_agent.spec.ts b/apps/server/src/services/llm/providers/claude_agent.spec.tsindex 67e0c2a272..c5c104d521 100644--- a/apps/server/src/services/llm/providers/claude_agent.spec.ts+++ b/apps/server/src/services/llm/providers/claude_agent.spec.ts@@ -29,7 +29,7 @@ vi.mock("../../data_dir.js", async () => {vi.mock("../../port.js", () => ({ default: 8080 }));const buildNoteHintMock = vi.hoisted(() => vi.fn((noteId: string): string | null => `NOTE_META(${noteId})`));-vi.mock("./note_hint.js", () => ({ buildNoteHint: buildNoteHintMock }));+vi.mock("../note_hint.js", () => ({ buildNoteHint: buildNoteHintMock }));// BYO binary resolution shells out to the user's `claude`; stub it so tests// don't depend on a real install. Resolves a path by default; can be made to@@ -40,7 +40,7 @@ vi.mock("./claude_binary.js", () => ({ resolveClaudeBinaryPath: resolveClaudeBin// Attachment resolution reads bytes out of Becca, which the core mock above// omits — stub it so the multimodal tests drive block construction directly.const resolveAttachmentPartMock = vi.hoisted(() => vi.fn());-vi.mock("./attachment_content.js", () => ({ resolveAttachmentPart: resolveAttachmentPartMock }));+vi.mock("../attachment_content.js", () => ({ resolveAttachmentPart: resolveAttachmentPartMock }));// The Windows `.cmd` shim delegates to child_process.spawn; the provider never// spawns otherwise, so mocking the whole module is safe.diff --git a/apps/server/src/services/llm/providers/claude_agent.ts b/apps/server/src/services/llm/providers/claude_agent.tsindex e8a7987c2b..c1a1e94e58 100644--- a/apps/server/src/services/llm/providers/claude_agent.ts+++ b/apps/server/src/services/llm/providers/claude_agent.ts@@ -32,13 +32,13 @@ import path from "path";import dataDirs from "../../data_dir.js";import { createMcpServer } from "../../mcp/mcp_server.js";+import { resolveAttachmentPart } from "../attachment_content.js";+import { buildNoteHint } from "../note_hint.js";+import { buildSystemPrompt } from "../system_prompt.js";import type { LlmProvider, LlmProviderConfig, ModelInfo, ModelPricing, StreamResult } from "../types.js";import { anthropicRecommendedIds } from "./anthropic.js";-import { resolveAttachmentPart } from "./attachment_content.js";import { buildModelList, mergeModelLists, type RemoteModel } from "./base_provider.js";import { resolveClaudeBinaryPath } from "./claude_binary.js";-import { buildNoteHint } from "./note_hint.js";-import { buildSystemPrompt } from "./system_prompt.js";/** Image media types Anthropic accepts as a base64 image block. */type SupportedImageMime = "image/png" | "image/jpeg" | "image/gif" | "image/webp";diff --git a/apps/server/src/services/llm/providers/system_prompt.spec.ts b/apps/server/src/services/llm/system_prompt.spec.tssimilarity index 100%rename from apps/server/src/services/llm/providers/system_prompt.spec.tsrename to apps/server/src/services/llm/system_prompt.spec.tsdiff --git a/apps/server/src/services/llm/providers/system_prompt.ts b/apps/server/src/services/llm/system_prompt.tssimilarity index 99%rename from apps/server/src/services/llm/providers/system_prompt.tsrename to apps/server/src/services/llm/system_prompt.tsindex 2cc138a115..877baea305 100644--- a/apps/server/src/services/llm/providers/system_prompt.ts+++ b/apps/server/src/services/llm/system_prompt.ts@@ -12,7 +12,7 @@import { isAnchorState, type LlmMessage } from "@triliumnext/commons";import { task_states } from "@triliumnext/core";-import type { LlmProviderConfig } from "../types.js";+import type { LlmProviderConfig } from "./types.js";/*** Build the system prompt from the conversation's system message / config plus