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): limit number of results in note meta
Elian Doran7440e4a6109e
diff --git a/apps/server/src/services/llm/providers/base_provider.ts b/apps/server/src/services/llm/providers/base_provider.tsindex 44662ae1e7..1155a0ad96 100644--- a/apps/server/src/services/llm/providers/base_provider.ts+++ b/apps/server/src/services/llm/providers/base_provider.ts@@ -12,7 +12,7 @@ import yaml from "js-yaml";import becca from "../../../becca/becca.js";import { getSkillsSummary } from "../skills/index.js";import { allToolRegistries } from "../tools/index.js";-import { getNoteMeta } from "../tools/helpers.js";+import { SYSTEM_PROMPT_LIMITS, getNoteMeta } from "../tools/helpers.js";import type { LlmProvider, LlmProviderConfig, ModelInfo, ModelPricing, StreamResult } from "../types.js";const DEFAULT_MAX_TOKENS = 8096;@@ -35,7 +35,7 @@ function buildNoteHint(noteId: string): string | null {return null;}- const metadata = yaml.dump(getNoteMeta(note), { lineWidth: -1 });+ const metadata = yaml.dump(getNoteMeta(note, SYSTEM_PROMPT_LIMITS), { lineWidth: -1 });return ["The user is currently viewing the following note.","Use this metadata (including contentPreview) to answer questions about the note without calling tools when possible.",diff --git a/apps/server/src/services/llm/tools/helpers.ts b/apps/server/src/services/llm/tools/helpers.tsindex 7e3ec39cdf..3043ddeee6 100644--- a/apps/server/src/services/llm/tools/helpers.ts+++ b/apps/server/src/services/llm/tools/helpers.ts@@ -92,11 +92,77 @@ export function getAttachmentContentPreview(att: BAttachment): string | null {return `${text.slice(0, ATTACHMENT_PREVIEW_MAX_LENGTH)}…`;}+/** Limits for collections returned in system prompt context. */+export const SYSTEM_PROMPT_LIMITS = {+ childNotes: 20,+ attributes: 20,+ attachments: 20+} as const;++/** Limits for collections returned by the get_note tool. */+export const TOOL_LIMITS = {+ childNotes: 50,+ attributes: 50,+ attachments: 50+} as const;++interface NoteMetaLimits {+ childNotes: number;+ attributes: number;+ attachments: number;+}++/**+ * Truncate an array and return it with total count metadata.+ * If the array exceeds `limit`, only the first `limit` items are returned.+ */+function truncated<T>(items: T[], limit: number) {+ return {+ totalCount: items.length,+ results: items.slice(0, limit)+ };+}+/*** Build the full metadata object for a note. Used by both the `get_note` tool* and the system prompt.+ *+ * @param limits — controls how many child notes, attributes, and attachments+ * are included. Use {@link SYSTEM_PROMPT_LIMITS} for the system prompt and+ * {@link TOOL_LIMITS} for the `get_note` tool.*/-export function getNoteMeta(note: BNote) {+export function getNoteMeta(note: BNote, limits: NoteMetaLimits) {+ const allChildNotes = note.getChildNotes().map((ch) => ({+ noteId: ch.noteId,+ title: ch.getTitleOrProtected()+ }));++ const allAttributes = note.getAttributes().map((attr) => ({+ attributeId: attr.attributeId,+ noteId: attr.noteId,+ type: attr.type,+ name: attr.name,+ value: attr.value,+ position: attr.position,+ isInheritable: attr.isInheritable,+ utcDateModified: attr.utcDateModified+ }));++ const allAttachments = note.getAttachments().map((att) => ({+ attachmentId: att.attachmentId,+ ownerId: att.ownerId,+ role: att.role,+ mime: att.mime,+ title: att.title,+ position: att.position,+ blobId: att.blobId,+ dateModified: att.dateModified,+ utcDateModified: att.utcDateModified,+ utcDateScheduledForErasureSince: att.utcDateScheduledForErasureSince,+ contentLength: att.contentLength,+ contentPreview: getAttachmentContentPreview(att)+ }));+return {noteId: note.noteId,isProtected: note.isProtected,@@ -109,36 +175,11 @@ export function getNoteMeta(note: BNote) {utcDateCreated: note.utcDateCreated,utcDateModified: note.utcDateModified,parentNoteIds: note.getParentNotes().map((p) => p.noteId),- childNotes: note.getChildNotes().map((ch) => ({- noteId: ch.noteId,- title: ch.getTitleOrProtected()- })),+ childNotes: truncated(allChildNotes, limits.childNotes),parentBranchIds: note.getParentBranches().map((p) => p.branchId),childBranchIds: note.getChildBranches().map((ch) => ch.branchId),- attributes: note.getAttributes().map((attr) => ({- attributeId: attr.attributeId,- noteId: attr.noteId,- type: attr.type,- name: attr.name,- value: attr.value,- position: attr.position,- isInheritable: attr.isInheritable,- utcDateModified: attr.utcDateModified- })),+ attributes: truncated(allAttributes, limits.attributes),contentPreview: getContentPreview(note),- attachments: note.getAttachments().map((att) => ({- attachmentId: att.attachmentId,- ownerId: att.ownerId,- role: att.role,- mime: att.mime,- title: att.title,- position: att.position,- blobId: att.blobId,- dateModified: att.dateModified,- utcDateModified: att.utcDateModified,- utcDateScheduledForErasureSince: att.utcDateScheduledForErasureSince,- contentLength: att.contentLength,- contentPreview: getAttachmentContentPreview(att)- }))+ attachments: truncated(allAttachments, limits.attachments)};}diff --git a/apps/server/src/services/llm/tools/note_tools.ts b/apps/server/src/services/llm/tools/note_tools.tsindex 6de3516849..b7b3e4c603 100644--- a/apps/server/src/services/llm/tools/note_tools.ts+++ b/apps/server/src/services/llm/tools/note_tools.ts@@ -9,7 +9,7 @@ import markdownImport from "../../import/markdown.js";import noteService from "../../notes.js";import SearchContext from "../../search/search_context.js";import searchService from "../../search/services/search.js";-import { getContentPreview, getNoteContentForLlm, getNoteMeta, setNoteContentFromLlm } from "./helpers.js";+import { TOOL_LIMITS, getContentPreview, getNoteContentForLlm, getNoteMeta, setNoteContentFromLlm } from "./helpers.js";import { defineTools } from "./tool_registry.js";export const noteTools = defineTools({@@ -71,7 +71,7 @@ export const noteTools = defineTools({return { error: "Note not found" };}- return getNoteMeta(note);+ return getNoteMeta(note, TOOL_LIMITS);}},