Files
Momento/memento-note/app/api/blocks/resolve/route.ts
Antigravity e2672cd2c2
Some checks failed
CI / Lint, Test & Build (push) Failing after 1m19s
CI / Deploy production (on server) (push) Has been skipped
feat(notes): liens internes, onglet Réseau, living blocks et consentement IA
Rend les liens entre notes visibles et persistants (sync NoteLink au save, auto-save, graphe réseau rafraîchi), ajoute living blocks, Memory Echo, recherche globale, consentement IA explicite et consolide les prototypes design en architectural-grid.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-24 14:27:29 +00:00

69 lines
1.9 KiB
TypeScript

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,
},
})
}