import { NextRequest, NextResponse } from 'next/server' import prisma from '@/lib/prisma' import { auth } from '@/auth' import { pickBestBlockForHint, pickBestPlainPassageForHint } from '@/lib/blocks/extract-blocks' /** * GET /api/blocks/resolve?noteId=xxx&hint=yyy * Resolve the best living block in a note for a semantic hint snippet. */ export async function GET(request: NextRequest) { const session = await auth() if (!session?.user?.id) { return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) } const noteId = request.nextUrl.searchParams.get('noteId') const hint = request.nextUrl.searchParams.get('hint') || '' if (!noteId) { return NextResponse.json({ error: 'noteId required' }, { status: 400 }) } const note = await prisma.note.findFirst({ where: { id: noteId, userId: session.user.id }, select: { id: true, title: true, content: true, notebookId: true, }, }) if (!note) { return NextResponse.json({ error: 'Note not found' }, { status: 404 }) } const liveBlock = pickBestBlockForHint(note.content, hint) const block = liveBlock ?? pickBestPlainPassageForHint(note.content, hint) if (!block) { return NextResponse.json({ error: 'no_block_found' }, { status: 404 }) } const mode = liveBlock?.blockId ? 'live' : 'citation' let notebookName = '' if (note.notebookId) { const notebook = await prisma.notebook.findFirst({ where: { id: note.notebookId, userId: session.user.id }, select: { name: true }, }) notebookName = notebook?.name || '' } const words = block.content.split(/\s+/) const snippet = words.slice(0, 30).join(' ') + (words.length > 30 ? '…' : '') return NextResponse.json({ mode, block: { blockId: block.blockId, noteId: note.id, noteTitle: note.title || '', notebookName, content: block.content, snippet, }, }) }