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): allow reading attachment content with OCR integration
Elian Doran058518fcba6e
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex 1eb35e1314..5797d8516f 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -2356,7 +2356,8 @@"web_search": "Web search","note_in_parent": "<Note/> in <Parent/>","get_note_attachments": "Get note attachments",- "get_attachment": "Get attachment"+ "get_attachment": "Get attachment",+ "get_attachment_content": "Read attachment content"}}}diff --git a/apps/server/src/services/llm/tools/attachment_tools.ts b/apps/server/src/services/llm/tools/attachment_tools.tsindex 43b5860a86..1d3273ae8b 100644--- a/apps/server/src/services/llm/tools/attachment_tools.ts+++ b/apps/server/src/services/llm/tools/attachment_tools.ts@@ -33,5 +33,39 @@ export const attachmentTools = defineTools({contentLength: attachment.contentLength};}+ },++ get_attachment_content: {+ description: "Read the text content of an attachment. Works for text-based attachments (code, SVG, plain text) and binary attachments that have OCR/extracted text (PDF, images). Check contentAvailability from get_note_attachments first — attachments with 'none' have no readable content.",+ inputSchema: z.object({+ attachmentId: z.string().describe("The ID of the attachment to read")+ }),+ execute: async ({ attachmentId }) => {+ const attachment = becca.getAttachment(attachmentId);+ if (!attachment) {+ return { error: "Attachment not found" };+ }++ if (attachment.hasStringContent()) {+ const content = attachment.getContent();+ return {+ attachmentId: attachment.attachmentId,+ source: "text" as const,+ content: typeof content === "string" ? content : content.toString("utf-8")+ };+ }++ // For binary attachments, try OCR/extracted text from the blob.+ const blob = attachment.blobId ? becca.getBlob({ blobId: attachment.blobId }) : null;+ if (blob?.textRepresentation) {+ return {+ attachmentId: attachment.attachmentId,+ source: "ocr" as const,+ content: blob.textRepresentation+ };+ }++ return { error: "Attachment has no readable text content" };+ }}});diff --git a/apps/server/src/services/llm/tools/note_tools.ts b/apps/server/src/services/llm/tools/note_tools.tsindex 59d85a55ba..1554717aad 100644--- a/apps/server/src/services/llm/tools/note_tools.ts+++ b/apps/server/src/services/llm/tools/note_tools.ts@@ -4,6 +4,7 @@import { z } from "zod";+import type BAttachment from "../../../becca/entities/battachment.js";import type BNote from "../../../becca/entities/bnote.js";import becca from "../../../becca/becca.js";import markdownExport from "../../export/markdown.js";@@ -103,6 +104,25 @@ function setNoteContentFromLlm(note: { type: string; title: string; setContent:}}+/**+ * Determine how an attachment's content can be read by the LLM:+ * - "text": the attachment has string content (e.g. code, SVG, plain text)+ * - "ocr": the attachment is binary but has OCR/extracted text available+ * - "none": binary content with no text representation+ */+function getAttachmentContentAvailability(att: BAttachment): "text" | "ocr" | "none" {+ if (att.hasStringContent()) {+ return "text";+ }++ const blob = att.blobId ? becca.getBlob({ blobId: att.blobId }) : null;+ if (blob?.textRepresentation) {+ return "ocr";+ }++ return "none";+}+export const noteTools = defineTools({search_notes: {description: [@@ -307,7 +327,7 @@ export const noteTools = defineTools({},get_note_attachments: {- description: "List all attachments of a note by its ID. Returns metadata for each attachment.",+ description: "List all attachments of a note by its ID. Returns metadata for each attachment, including how its content can be read (text, ocr, or none).",inputSchema: z.object({noteId: z.string().describe("The ID of the note whose attachments to list")}),@@ -328,7 +348,8 @@ export const noteTools = defineTools({dateModified: att.dateModified,utcDateModified: att.utcDateModified,utcDateScheduledForErasureSince: att.utcDateScheduledForErasureSince,- contentLength: att.contentLength+ contentLength: att.contentLength,+ contentAvailability: getAttachmentContentAvailability(att)}));}}