Files
Momento/memento-note/app/api/brainstorm/[sessionId]/finalize/route.ts
Antigravity 1fcea6ed7d
All checks were successful
Deploy to Production / Build and Deploy (push) Successful in 7s
feat: brainstorm sessions, PDF document Q&A, embedding fixes, and UI improvements
- Add brainstorm feature with collaborative canvas, AI idea generation, live cursors, playback, and export
- Add PDF upload/extraction/ingestion pipeline with pgvector document search (RAG)
- Add document Q&A overlay with streaming chat and PDF preview
- Add note attachments UI with status polling, grid layout, and auto-scroll
- Add task extraction AI tool and agent executor improvements
- Fix NoteEmbedding missing updatedAt column, re-index 66 notes with 1536-dim embeddings
- Fix brainstorm 'Create Note' button: add success toast and redirect to created note
- Fix memory echo notification infinite polling
- Fix chat route to always include document_search tool
- Add brainstorm i18n keys across all 14 locales
- Add socket server for real-time brainstorm collaboration
- Add hierarchical notebook selector and organize notebook dialog improvements
- Add sidebar brainstorm section with session management
- Update prisma schema with brainstorm tables, attachments, and document chunks
2026-05-14 17:43:21 +00:00

99 lines
2.9 KiB
TypeScript

import { NextRequest, NextResponse } from 'next/server'
import prisma from '@/lib/prisma'
import { auth } from '@/auth'
export async function POST(
request: NextRequest,
{ params }: { params: Promise<{ sessionId: string }> }
) {
const session = await auth()
if (!session?.user?.id) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
}
try {
const { sessionId } = await params
const brainstormSession = await prisma.brainstormSession.findFirst({
where: {
id: sessionId,
OR: [
{ userId: session.user.id },
{ participants: { some: { userId: session.user.id } } },
],
},
include: {
ideas: {
include: {
noteRefs: {
include: {
note: { select: { id: true, title: true, labels: true } },
},
},
},
},
},
})
if (!brainstormSession) {
return NextResponse.json({ error: 'Session not found' }, { status: 404 })
}
const noteImpactMap = new Map<string, { title: string; labels: string[]; acceptedCount: number; dismissedCount: number }>()
for (const idea of brainstormSession.ideas) {
for (const ref of idea.noteRefs) {
if (!ref.noteId || !ref.note) continue
const existing = noteImpactMap.get(ref.noteId) || {
title: ref.note.title || 'Untitled',
labels: (() => { try { return JSON.parse(ref.note.labels || '[]') } catch { return [] } })(),
acceptedCount: 0,
dismissedCount: 0,
}
if (ref.verdict === 'accepted') existing.acceptedCount++
else if (ref.verdict === 'dismissed') existing.dismissedCount++
noteImpactMap.set(ref.noteId, existing)
}
}
const updatePromises: Promise<any>[] = []
for (const [noteId, impact] of noteImpactMap) {
if (impact.acceptedCount === 0 && impact.dismissedCount > 0) {
if (!impact.labels.includes('brainstorm-dry')) {
impact.labels.push('brainstorm-dry')
updatePromises.push(
prisma.note.update({
where: { id: noteId },
data: { labels: JSON.stringify(impact.labels) },
})
)
}
}
}
if (updatePromises.length > 0) {
await prisma.$transaction(updatePromises)
}
const fruitful = Array.from(noteImpactMap.values()).filter(n => n.acceptedCount > 0).length
const dry = Array.from(noteImpactMap.values()).filter(n => n.acceptedCount === 0 && n.dismissedCount > 0).length
const totalRefs = Array.from(noteImpactMap.values()).length
return NextResponse.json({
success: true,
impact: {
notesSolicited: totalRefs,
notesEnriched: fruitful,
notesMarkedDry: dry,
},
})
} catch (error) {
console.error('Error finalizing brainstorm:', error)
return NextResponse.json(
{ error: 'Failed to finalize brainstorm' },
{ status: 500 }
)
}
}