Add automatic note clustering using density-based algorithm (DBSCAN variant) and bridge notes detection for connecting different thematic clusters. Features: - NoteCluster, ClusterMember, BridgeNote, BridgeSuggestion models - Clustering service with pgvector cosine similarity - Bridge notes detection (notes connecting >=2 clusters) - AI-powered suggestions for missing cluster connections - /insights page with React Flow visualization - Cron endpoint for automatic recalculation Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
86 lines
2.4 KiB
TypeScript
86 lines
2.4 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { auth } from '@/auth'
|
|
import prisma from '@/lib/prisma'
|
|
import { bridgeNotesService } from '@/lib/ai/services/bridge-notes.service'
|
|
|
|
/**
|
|
* GET /api/bridge-notes
|
|
* Get all bridge notes for the current user.
|
|
*/
|
|
export async function GET(request: NextRequest) {
|
|
try {
|
|
const session = await auth()
|
|
if (!session?.user?.id) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
}
|
|
|
|
const userId = session.user.id
|
|
const { searchParams } = new URL(request.url)
|
|
const includeDetails = searchParams.get('details') === 'true'
|
|
|
|
const bridgeNotes = await bridgeNotesService.getBridgeNotes(userId)
|
|
|
|
// Optionally include note details
|
|
let enrichedNotes = bridgeNotes
|
|
if (includeDetails && bridgeNotes.length > 0) {
|
|
const noteIds = bridgeNotes.map(b => b.noteId)
|
|
const notes = await prisma.note.findMany({
|
|
where: { id: { in: noteIds } },
|
|
select: { id: true, title: true, content: true, notebookId: true }
|
|
})
|
|
|
|
const noteMap = new Map(notes.map(n => [n.id, n]))
|
|
|
|
enrichedNotes = bridgeNotes.map(b => ({
|
|
...b,
|
|
note: noteMap.get(b.noteId)
|
|
}))
|
|
}
|
|
|
|
return NextResponse.json({
|
|
bridgeNotes: enrichedNotes,
|
|
count: enrichedNotes.length
|
|
})
|
|
} catch (error) {
|
|
console.error('Error fetching bridge notes:', error)
|
|
return NextResponse.json(
|
|
{ error: 'Failed to fetch bridge notes' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|
|
|
|
/**
|
|
* DELETE /api/bridge-notes
|
|
* Dismiss a bridge suggestion.
|
|
*/
|
|
export async function DELETE(request: NextRequest) {
|
|
try {
|
|
const session = await auth()
|
|
if (!session?.user?.id) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
}
|
|
|
|
const userId = session.user.id
|
|
const body = await request.json()
|
|
const { clusterAId, clusterBId } = body
|
|
|
|
if (typeof clusterAId !== 'number' || typeof clusterBId !== 'number') {
|
|
return NextResponse.json(
|
|
{ error: 'Invalid cluster IDs' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
await bridgeNotesService.dismissSuggestion(userId, clusterAId, clusterBId)
|
|
|
|
return NextResponse.json({ success: true })
|
|
} catch (error) {
|
|
console.error('Error dismissing suggestion:', error)
|
|
return NextResponse.json(
|
|
{ error: 'Failed to dismiss suggestion' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|