feat(cluster): implement cluster detection and bridge notes discovery
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>
This commit is contained in:
85
memento-note/app/api/bridge-notes/route.ts
Normal file
85
memento-note/app/api/bridge-notes/route.ts
Normal file
@@ -0,0 +1,85 @@
|
||||
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 }
|
||||
)
|
||||
}
|
||||
}
|
||||
66
memento-note/app/api/bridge-notes/suggestions/route.ts
Normal file
66
memento-note/app/api/bridge-notes/suggestions/route.ts
Normal file
@@ -0,0 +1,66 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { auth } from '@/auth'
|
||||
import { bridgeNotesService } from '@/lib/ai/services/bridge-notes.service'
|
||||
|
||||
/**
|
||||
* GET /api/bridge-notes/suggestions
|
||||
* Get AI-powered bridge note suggestions for unconnected clusters.
|
||||
*/
|
||||
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 includeDismissed = searchParams.get('dismissed') === 'true'
|
||||
|
||||
const suggestions = await bridgeNotesService.getBridgeSuggestions(userId, includeDismissed)
|
||||
|
||||
return NextResponse.json({
|
||||
suggestions,
|
||||
count: suggestions.length
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('Error fetching bridge suggestions:', error)
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to fetch suggestions' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* POST /api/bridge-notes/suggestions/generate
|
||||
* Generate new bridge suggestions (force regeneration).
|
||||
*/
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const session = await auth()
|
||||
if (!session?.user?.id) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
}
|
||||
|
||||
const userId = session.user.id
|
||||
|
||||
// Generate new suggestions
|
||||
const suggestions = await bridgeNotesService.generateBridgeSuggestions(userId)
|
||||
|
||||
// Save to database
|
||||
await bridgeNotesService.saveBridgeSuggestions(userId, suggestions)
|
||||
|
||||
return NextResponse.json({
|
||||
suggestions,
|
||||
count: suggestions.length,
|
||||
message: `Generated ${suggestions.length} bridge suggestions`
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('Error generating bridge suggestions:', error)
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to generate suggestions' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user