All checks were successful
Deploy to Production / Build and Deploy (push) Successful in 45s
- Agent-created notes are now type 'markdown' by default (raw markdown content) - Switching note type from markdown → richtext auto-converts content to HTML via /api/ai/convert-markdown endpoint using the existing markdownToHtml utility - Users can freely switch between markdown and richtext in the note type selector Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
107 lines
3.5 KiB
TypeScript
107 lines
3.5 KiB
TypeScript
/**
|
|
* Note CRUD Tools
|
|
* note_create, note_read, note_update
|
|
*/
|
|
|
|
import { tool } from 'ai'
|
|
import { z } from 'zod'
|
|
import { toolRegistry } from './registry'
|
|
import { prisma } from '@/lib/prisma'
|
|
import { markdownToHtml } from '@/lib/markdown-to-html'
|
|
|
|
// --- note_read ---
|
|
toolRegistry.register({
|
|
name: 'note_read',
|
|
description: 'Read a specific note by its ID. Returns the full note content.',
|
|
isInternal: true,
|
|
buildTool: (ctx) =>
|
|
tool({
|
|
description: 'Read a specific note by ID. Returns the full content.',
|
|
inputSchema: z.object({
|
|
noteId: z.string().describe('The ID of the note to read'),
|
|
}),
|
|
execute: async ({ noteId }) => {
|
|
try {
|
|
const note = await prisma.note.findFirst({
|
|
where: { id: noteId, userId: ctx.userId },
|
|
select: { id: true, title: true, content: true, isMarkdown: true, createdAt: true, updatedAt: true },
|
|
})
|
|
if (!note) return { error: 'Note not found' }
|
|
return note
|
|
} catch (e: any) {
|
|
return { error: `Read note failed: ${e.message}` }
|
|
}
|
|
},
|
|
}),
|
|
})
|
|
|
|
// --- note_create ---
|
|
toolRegistry.register({
|
|
name: 'note_create',
|
|
description: 'Create a new note with a title and content.',
|
|
isInternal: true,
|
|
buildTool: (ctx) =>
|
|
tool({
|
|
description: 'Create a new note.',
|
|
inputSchema: z.object({
|
|
title: z.string().describe('Title for the note'),
|
|
content: z.string().describe('Content of the note (markdown supported)'),
|
|
notebookId: z.string().optional().describe('Optional notebook ID to place the note in'),
|
|
images: z.array(z.string()).optional().describe('Optional array of local image URL paths to attach to the note (e.g. ["/uploads/notes/abc.jpg"])'),
|
|
}),
|
|
execute: async ({ title, content, notebookId, images }) => {
|
|
try {
|
|
const note = await prisma.note.create({
|
|
data: {
|
|
title,
|
|
content,
|
|
type: 'markdown',
|
|
isMarkdown: true,
|
|
autoGenerated: true,
|
|
userId: ctx.userId,
|
|
notebookId: notebookId || null,
|
|
images: images && images.length > 0 ? JSON.stringify(images) : null,
|
|
},
|
|
select: { id: true, title: true },
|
|
})
|
|
return { success: true, noteId: note.id, title: note.title }
|
|
} catch (e: any) {
|
|
return { error: `Create note failed: ${e.message}` }
|
|
}
|
|
},
|
|
}),
|
|
})
|
|
|
|
// --- note_update ---
|
|
toolRegistry.register({
|
|
name: 'note_update',
|
|
description: 'Update an existing note\'s content.',
|
|
isInternal: true,
|
|
buildTool: (ctx) =>
|
|
tool({
|
|
description: 'Update an existing note.',
|
|
inputSchema: z.object({
|
|
noteId: z.string().describe('The ID of the note to update'),
|
|
title: z.string().optional().describe('New title (optional)'),
|
|
content: z.string().optional().describe('New content (optional)'),
|
|
}),
|
|
execute: async ({ noteId, title, content }) => {
|
|
try {
|
|
const existing = await prisma.note.findFirst({
|
|
where: { id: noteId, userId: ctx.userId },
|
|
})
|
|
if (!existing) return { error: 'Note not found' }
|
|
|
|
const data: Record<string, any> = {}
|
|
if (title !== undefined) data.title = title
|
|
if (content !== undefined) data.content = content
|
|
|
|
await prisma.note.update({ where: { id: noteId }, data })
|
|
return { success: true, noteId }
|
|
} catch (e: any) {
|
|
return { error: `Update note failed: ${e.message}` }
|
|
}
|
|
},
|
|
}),
|
|
})
|