import { streamText, UIMessage } from 'ai' import { getChatProvider } from '@/lib/ai/factory' import { getSystemConfig } from '@/lib/config' import { semanticSearchService } from '@/lib/ai/services/semantic-search.service' import { prisma } from '@/lib/prisma' import { auth } from '@/auth' import { loadTranslations, getTranslationValue, SupportedLanguage } from '@/lib/i18n' import { toolRegistry } from '@/lib/ai/tools' import { readFile } from 'fs/promises' import path from 'path' export const maxDuration = 60 /** * Extract text content from a UIMessage's parts array. */ function extractTextFromUIMessage(msg: { parts?: Array<{ type: string; text?: string }>; content?: string }): string { if (typeof msg.content === 'string') return msg.content if (msg.parts && Array.isArray(msg.parts)) { return msg.parts .filter((p) => p.type === 'text' && typeof p.text === 'string') .map((p) => p.text!) .join('') } return '' } /** * Convert an array of UIMessages (from the client) to CoreMessage[] for streamText. */ function toCoreMessages(uiMessages: UIMessage[]): Array<{ role: 'user' | 'assistant'; content: string }> { return uiMessages .filter((m) => m.role === 'user' || m.role === 'assistant') .map((m) => ({ role: m.role as 'user' | 'assistant', content: extractTextFromUIMessage(m), })) .filter((m) => m.content.length > 0) } export async function POST(req: Request) { // 1. Auth check const session = await auth() if (!session?.user?.id) { return new Response('Unauthorized', { status: 401 }) } const userId = session.user.id // 2. Parse request body const body = await req.json() const { messages: rawMessages, conversationId, notebookId, language, webSearch, noteContext, format } = body as { messages: UIMessage[] conversationId?: string notebookId?: string language?: string webSearch?: boolean noteContext?: { title: string; content: string; tone: string; images?: string[] } format?: 'html' | 'markdown' } const incomingMessages = toCoreMessages(rawMessages) // 3. Manage conversation let conversation: { id: string; messages: Array<{ role: string; content: string }> } if (conversationId) { const existing = await prisma.conversation.findUnique({ where: { id: conversationId, userId }, include: { messages: { orderBy: { createdAt: 'asc' } } }, }) if (!existing) return new Response('Conversation not found', { status: 404 }) conversation = existing } else { const userMessage = incomingMessages[incomingMessages.length - 1]?.content || 'New conversation' conversation = await prisma.conversation.create({ data: { userId, notebookId: notebookId || null, title: userMessage.substring(0, 50) + (userMessage.length > 50 ? '...' : ''), }, include: { messages: true }, }) } // 4. RAG retrieval const currentMessage = incomingMessages[incomingMessages.length - 1]?.content || '' const lang = (language || 'en') as SupportedLanguage const translations = await loadTranslations(lang) const untitledText = getTranslationValue(translations, 'notes.untitled') || 'Untitled' let notebookContext = '' let searchNotes = '' if (!noteContext) { if (notebookId) { const notebookNotes = await prisma.note.findMany({ where: { notebookId, userId, trashedAt: null }, orderBy: { updatedAt: 'desc' }, take: 20, select: { id: true, title: true, content: true, updatedAt: true }, }) if (notebookNotes.length > 0) { notebookContext = notebookNotes .map(n => `NOTE [${n.title || untitledText}] (updated ${n.updatedAt.toLocaleDateString()}):\n${(n.content || '').substring(0, 1500)}`) .join('\n\n---\n\n') } } let searchResults: any[] = [] try { searchResults = await semanticSearchService.search(currentMessage, { notebookId, limit: notebookId ? 10 : 5, threshold: notebookId ? 0.3 : 0.5, defaultTitle: untitledText, }) } catch {} searchNotes = searchResults .map((r) => `NOTE [${r.title || untitledText}]: ${r.content}`) .join('\n\n---\n\n') } const contextNotes = [notebookContext, searchNotes].filter(Boolean).join('\n\n---\n\n') // 5. System prompt synthesis const promptLang: Record = { en: { contextWithNotes: `## User's notes\n\n${contextNotes}\n\nWhen using info from the notes above, cite the source note title in parentheses, e.g.: "Deployment is done via Docker (💻 Development Guide)". Don't copy word for word — rephrase. If the notes don't cover the topic, say so and supplement with your general knowledge.`, contextNoNotes: "No relevant notes found for this question. Answer with your general knowledge.", system: `You are the AI assistant of Memento. The user asks you questions about their projects, technical docs, and notes. You must respond in a structured and helpful way. ## Format rules - ${format === 'html' ? `Respond MANDATORILY using valid HTML fragments (e.g.,

, , ,