feat(insights): fix DBSCAN, Persian embeddings crash, D3 physics layouts, and D3 node not found runtime error
This commit is contained in:
@@ -7,6 +7,7 @@ import { bridgeNotesService } from '@/lib/ai/services/bridge-notes.service'
|
||||
/**
|
||||
* GET /api/clusters
|
||||
* Get all clusters for the current user.
|
||||
* Returns cached clusters + bridge notes enriched with cluster names.
|
||||
*/
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
@@ -17,9 +18,10 @@ export async function GET(request: NextRequest) {
|
||||
|
||||
const userId = session.user.id
|
||||
|
||||
// Check for cached results
|
||||
const cached = await clusteringService.getCachedClusters(userId)
|
||||
if (cached) {
|
||||
// Check for stored results (even if stale/périmés)
|
||||
const stored = await clusteringService.getStoredClusters(userId)
|
||||
if (stored) {
|
||||
const cached = stored.clusters
|
||||
// Fetch notes with their cluster assignments
|
||||
const notes = await prisma.note.findMany({
|
||||
where: { userId, trashedAt: null },
|
||||
@@ -29,20 +31,65 @@ export async function GET(request: NextRequest) {
|
||||
// Get cluster member mappings
|
||||
const clusterMembers = await prisma.clusterMember.findMany({
|
||||
where: { userId },
|
||||
select: { noteId: true, clusterId: true }
|
||||
select: { noteId: true, clusterId: true, isCentral: true }
|
||||
})
|
||||
|
||||
const noteClusterMap = new Map(clusterMembers.map(cm => [cm.noteId, cm.clusterId]))
|
||||
const notesWithClusters = notes.map(n => ({
|
||||
...n,
|
||||
clusterId: noteClusterMap.get(n.id)
|
||||
}))
|
||||
const noteClusterMap = new Map(clusterMembers.map(cm => [cm.noteId, { clusterId: cm.clusterId, isCentral: cm.isCentral }]))
|
||||
const notesWithClusters = notes.map(n => {
|
||||
const mapping = noteClusterMap.get(n.id)
|
||||
return {
|
||||
...n,
|
||||
clusterId: mapping?.clusterId,
|
||||
isCentral: mapping?.isCentral || false
|
||||
}
|
||||
})
|
||||
|
||||
// Fetch bridge notes with enrichment (cluster names + note details)
|
||||
const bridgeNotesData = await prisma.bridgeNote.findMany({
|
||||
where: { userId },
|
||||
orderBy: { bridgeScore: 'desc' },
|
||||
take: 10
|
||||
})
|
||||
|
||||
let enrichedBridgeNotes: object[] = []
|
||||
if (bridgeNotesData.length > 0) {
|
||||
const bridgeNoteIds = bridgeNotesData.map(b => b.noteId)
|
||||
const bridgeNoteDetails = await prisma.note.findMany({
|
||||
where: { id: { in: bridgeNoteIds } },
|
||||
select: { id: true, title: true, content: true }
|
||||
})
|
||||
const bridgeNoteDetailsMap = new Map(bridgeNoteDetails.map(n => [n.id, n]))
|
||||
|
||||
enrichedBridgeNotes = bridgeNotesData.map(b => {
|
||||
const clustersConnected = JSON.parse(b.clustersConnected) as number[]
|
||||
return {
|
||||
noteId: b.noteId,
|
||||
bridgeScore: b.bridgeScore,
|
||||
clustersConnected,
|
||||
clusterNames: clustersConnected.map(
|
||||
cid => cached.find(c => c.clusterId === cid)?.name || `Cluster ${cid}`
|
||||
),
|
||||
note: bridgeNoteDetailsMap.get(b.noteId)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
const embeddingCountRow = await prisma.$queryRawUnsafe<Array<{ count: bigint }>>(
|
||||
`SELECT COUNT(*) FROM "NoteEmbedding" ne
|
||||
INNER JOIN "Note" n ON n.id = ne."noteId"
|
||||
WHERE n."userId" = $1 AND n."trashedAt" IS NULL`,
|
||||
userId
|
||||
)
|
||||
|
||||
return NextResponse.json({
|
||||
clusters: cached,
|
||||
notes: notesWithClusters,
|
||||
bridgeNotes: enrichedBridgeNotes,
|
||||
cached: true,
|
||||
totalNotes: cached.reduce((sum, c) => sum + c.noteIds.length, 0)
|
||||
stale: stored.stale,
|
||||
lastCalculated: stored.lastCalculated,
|
||||
totalNotes: notes.length,
|
||||
embeddingCount: Number(embeddingCountRow[0]?.count || 0),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -61,6 +108,7 @@ export async function GET(request: NextRequest) {
|
||||
return NextResponse.json({
|
||||
clusters: [],
|
||||
notes: [],
|
||||
bridgeNotes: [],
|
||||
totalNotes: notesCount,
|
||||
embeddingCount: Number(embeddingCount[0]?.count || 0),
|
||||
needsCalculation: true
|
||||
@@ -77,6 +125,7 @@ export async function GET(request: NextRequest) {
|
||||
/**
|
||||
* POST /api/clusters
|
||||
* Trigger a full recalculation of clusters and bridge notes.
|
||||
* Returns clusters + bridge notes enriched for immediate display.
|
||||
*/
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
@@ -86,66 +135,77 @@ export async function POST(request: NextRequest) {
|
||||
}
|
||||
|
||||
const userId = session.user.id
|
||||
const body = await request.json().catch(() => ({}))
|
||||
const force = Boolean(body?.force)
|
||||
|
||||
// Use the PROPER clustering service (DBSCAN algorithm)
|
||||
// 0. Indexer / réindexer les embeddings (texte complet, multi-chunks)
|
||||
await clusteringService.ensureEmbeddings(userId, { force: force || true })
|
||||
|
||||
// 1. Run DBSCAN clustering
|
||||
const results = await clusteringService.clusterNotes(userId)
|
||||
|
||||
if (results.clusters.length === 0) {
|
||||
return NextResponse.json({
|
||||
clusters: [],
|
||||
bridgeNotes: [],
|
||||
message: 'Could not generate clusters. Notes may be too diverse or not enough.',
|
||||
noiseCount: results.noiseCount
|
||||
})
|
||||
}
|
||||
|
||||
// Generate cluster names using AI
|
||||
// 2. Generate cluster names with AI
|
||||
for (const cluster of results.clusters) {
|
||||
cluster.name = await clusteringService.generateClusterName(cluster.clusterId, userId)
|
||||
}
|
||||
|
||||
// Save clustering results
|
||||
// 3. Save clustering results
|
||||
await clusteringService.saveClusteringResults(userId, results)
|
||||
|
||||
// Detect and save bridge notes
|
||||
// 4. Detect and save bridge notes
|
||||
const bridgeNotes = await bridgeNotesService.detectBridgeNotes(userId)
|
||||
await bridgeNotesService.saveBridgeNotes(userId, bridgeNotes)
|
||||
|
||||
// Generate and save bridge suggestions
|
||||
// 5. Generate and save bridge suggestions
|
||||
const suggestions = await bridgeNotesService.generateBridgeSuggestions(userId)
|
||||
await bridgeNotesService.saveBridgeSuggestions(userId, suggestions)
|
||||
|
||||
// Fetch notes with their cluster assignments
|
||||
// 6. Fetch notes with cluster assignments
|
||||
const notes = await prisma.note.findMany({
|
||||
where: { userId, trashedAt: null },
|
||||
select: { id: true, title: true, content: true }
|
||||
})
|
||||
|
||||
// Get cluster member mappings
|
||||
const clusterMembers = await prisma.clusterMember.findMany({
|
||||
where: { userId },
|
||||
select: { noteId: true, clusterId: true }
|
||||
select: { noteId: true, clusterId: true, isCentral: true }
|
||||
})
|
||||
|
||||
const noteClusterMap = new Map(clusterMembers.map(cm => [cm.noteId, cm.clusterId]))
|
||||
const notesWithClusters = notes.map(n => ({
|
||||
...n,
|
||||
clusterId: noteClusterMap.get(n.id)
|
||||
}))
|
||||
const noteClusterMap = new Map(clusterMembers.map(cm => [cm.noteId, { clusterId: cm.clusterId, isCentral: cm.isCentral }]))
|
||||
const notesWithClusters = notes.map(n => {
|
||||
const mapping = noteClusterMap.get(n.id)
|
||||
return {
|
||||
...n,
|
||||
clusterId: mapping?.clusterId,
|
||||
isCentral: mapping?.isCentral || false
|
||||
}
|
||||
})
|
||||
|
||||
// Get enriched bridge notes with note details
|
||||
// 7. Enrich bridge notes with cluster names + note details
|
||||
const noteMap = new Map(notes.map(n => [n.id, n]))
|
||||
const enrichedBridgeNotes = bridgeNotes.slice(0, 10).map(b => ({
|
||||
noteId: b.noteId,
|
||||
bridgeScore: b.bridgeScore,
|
||||
clustersConnected: b.clustersConnected,
|
||||
clusterNames: b.clusterNames,
|
||||
note: notes.find(n => n.id === b.noteId)
|
||||
note: noteMap.get(b.noteId)
|
||||
}))
|
||||
|
||||
return NextResponse.json({
|
||||
clusters: results.clusters,
|
||||
notes: notesWithClusters,
|
||||
bridgeNotes: enrichedBridgeNotes,
|
||||
totalNotes: results.clusters.reduce((sum, c) => sum + c.noteIds.length, 0) + results.noiseCount,
|
||||
totalNotes:
|
||||
results.clusters.reduce((sum, c) => sum + c.noteIds.length, 0) + results.noiseCount,
|
||||
noiseCount: results.noiseCount,
|
||||
message: `Generated ${results.clusters.length} clusters with ${bridgeNotes.length} bridge notes`
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user