import { NextResponse } from 'next/server' import { prisma } from '@/lib/prisma' import { auth } from '@/auth' /** * Admin endpoint to validate all pgvector embeddings in the database. * Uses native SQL to check for valid vector format. */ export async function GET() { try { const session = await auth() if (!session?.user?.id) { return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) } const user = await prisma.user.findUnique({ where: { id: session.user.id }, select: { role: true } }) if (!user || user.role !== 'ADMIN') { return NextResponse.json({ error: 'Forbidden - Admin only' }, { status: 403 }) } const totalResult: Array<{ total: bigint }> = await prisma.$queryRawUnsafe( `SELECT COUNT(*)::bigint as total FROM "Note" WHERE "trashedAt" IS NULL` ) const total = Number(totalResult[0]?.total ?? 0) const withEmbedding: Array<{ count: bigint }> = await prisma.$queryRawUnsafe( `SELECT COUNT(*)::bigint as count FROM "NoteEmbedding"` ) const validCount = Number(withEmbedding[0]?.count ?? 0) const invalidResult: Array<{ count: bigint }> = await prisma.$queryRawUnsafe( `SELECT COUNT(*)::bigint as count FROM "NoteEmbedding" e WHERE e."embedding" IS NULL OR array_length(string_to_array(replace(replace(e."embedding"::text, '[', ''), ']', ''), ','), 1) != 2560` ) const invalidCount = Number(invalidResult[0]?.count ?? 0) const missingCount = total - validCount return NextResponse.json({ success: true, summary: { total, valid: validCount - invalidCount, missing: missingCount > 0 ? missingCount : 0, invalid: invalidCount }, invalidNotes: [] }) } catch (error) { console.error('[EMBEDDING_VALIDATION] Error:', error) return NextResponse.json( { success: false, error: String(error) }, { status: 500 } ) } }