Files
Momento/memento-note/app/api/admin/embeddings/validate/route.ts
Antigravity 4c1359ee39
All checks were successful
Deploy to Production / Build and Deploy (push) Successful in 4s
Revert "fix: switch embedding dimension from 1536 to 2560 for qwen-embedding-4b"
This reverts commit e09ea3a145.
2026-05-12 09:19:01 +00:00

62 lines
1.9 KiB
TypeScript

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) != 1536`
)
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 }
)
}
}