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): add tools to append or replace note content
Elian Doranb8f9916d1321
diff --git a/apps/server/src/services/llm/tools.ts b/apps/server/src/services/llm/tools.tsindex 5434ebdcfa..e55ac38020 100644--- a/apps/server/src/services/llm/tools.ts+++ b/apps/server/src/services/llm/tools.ts@@ -61,10 +61,87 @@ export const readNote = tool({}});+/**+ * Update the content of a note.+ */+export const updateNoteContent = tool({+ description: "Replace the entire content of a note. Use this to completely rewrite a note's content. For text notes, provide HTML content.",+ inputSchema: z.object({+ noteId: z.string().describe("The ID of the note to update"),+ content: z.string().describe("The new content for the note (HTML for text notes, plain text for code notes)")+ }),+ execute: async ({ noteId, content }) => {+ const note = becca.getNote(noteId);+ if (!note) {+ return { error: "Note not found" };+ }+ if (note.isProtected) {+ return { error: "Note is protected and cannot be modified" };+ }+ if (note.type !== "text" && note.type !== "code") {+ return { error: `Cannot update content for note type: ${note.type}` };+ }++ note.setContent(content);+ return {+ success: true,+ noteId: note.noteId,+ title: note.title+ };+ }+});++/**+ * Append content to a note.+ */+export const appendToNote = tool({+ description: "Append content to the end of an existing note. For text notes, the content will be added as a new paragraph.",+ inputSchema: z.object({+ noteId: z.string().describe("The ID of the note to append to"),+ content: z.string().describe("The content to append (HTML for text notes, plain text for code notes)")+ }),+ execute: async ({ noteId, content }) => {+ const note = becca.getNote(noteId);+ if (!note) {+ return { error: "Note not found" };+ }+ if (note.isProtected) {+ return { error: "Note is protected and cannot be modified" };+ }+ if (note.type !== "text" && note.type !== "code") {+ return { error: `Cannot append to note type: ${note.type}` };+ }++ const existingContent = note.getContent();+ if (typeof existingContent !== "string") {+ return { error: "Note has binary content" };+ }++ let newContent: string;+ if (note.type === "text") {+ // For text notes, wrap in paragraph if not already HTML+ const contentToAppend = content.startsWith("<") ? content : `<p>${content}</p>`;+ newContent = existingContent + contentToAppend;+ } else {+ // For code notes, just append with newline+ newContent = existingContent + (existingContent.endsWith("\n") ? "" : "\n") + content;+ }++ note.setContent(newContent);+ return {+ success: true,+ noteId: note.noteId,+ title: note.title+ };+ }+});+/*** All available note tools.*/export const noteTools = {search_notes: searchNotes,- read_note: readNote+ read_note: readNote,+ update_note_content: updateNoteContent,+ append_to_note: appendToNote};