feat: brainstorm sessions, PDF document Q&A, embedding fixes, and UI improvements
All checks were successful
Deploy to Production / Build and Deploy (push) Successful in 7s
All checks were successful
Deploy to Production / Build and Deploy (push) Successful in 7s
- 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
This commit is contained in:
224
memento-note/app/api/brainstorm/[sessionId]/manual-idea/route.ts
Normal file
224
memento-note/app/api/brainstorm/[sessionId]/manual-idea/route.ts
Normal file
@@ -0,0 +1,224 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import prisma from '@/lib/prisma'
|
||||
import { auth } from '@/auth'
|
||||
import { z } from 'zod'
|
||||
import { verifyParticipant, logActivity, resolveAiContextUserId, captureSnapshot } from '@/lib/brainstorm-collab'
|
||||
import { embeddingService } from '@/lib/ai/services/embedding.service'
|
||||
import { getTagsProvider } from '@/lib/ai/factory'
|
||||
import { getSystemConfig } from '@/lib/config'
|
||||
import { emitToSession } from '@/lib/socket-emit'
|
||||
|
||||
const manualSchema = z.object({
|
||||
title: z.string().min(1),
|
||||
description: z.string().optional(),
|
||||
parentIdeaId: z.string().optional(),
|
||||
locale: z.string().optional(),
|
||||
})
|
||||
|
||||
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 { isParticipant } = await verifyParticipant(sessionId, session.user.id, 'editor')
|
||||
|
||||
if (!isParticipant) {
|
||||
return NextResponse.json({ error: 'No edit permission' }, { status: 403 })
|
||||
}
|
||||
|
||||
const body = await request.json()
|
||||
const { title, description, parentIdeaId, locale } = manualSchema.parse(body)
|
||||
|
||||
const brainstormSession = await prisma.brainstormSession.findFirst({
|
||||
where: { id: sessionId },
|
||||
})
|
||||
|
||||
if (!brainstormSession) {
|
||||
return NextResponse.json({ error: 'Session not found' }, { status: 404 })
|
||||
}
|
||||
|
||||
let wave = 1
|
||||
let parentIdea: any = null
|
||||
if (parentIdeaId) {
|
||||
parentIdea = await prisma.brainstormIdea.findFirst({
|
||||
where: { id: parentIdeaId, sessionId },
|
||||
})
|
||||
if (parentIdea) {
|
||||
wave = Math.min((parentIdea.waveNumber || 1) + 1, 3)
|
||||
}
|
||||
}
|
||||
|
||||
const existingIdeas = await prisma.brainstormIdea.count({
|
||||
where: { sessionId, waveNumber: wave },
|
||||
})
|
||||
const angle = (existingIdeas % 4) * (2 * Math.PI / 4) + Math.random() * 0.5
|
||||
const radius = wave * 200 + Math.random() * 50
|
||||
|
||||
const idea = await prisma.brainstormIdea.create({
|
||||
data: {
|
||||
sessionId,
|
||||
waveNumber: wave,
|
||||
title,
|
||||
description: description || '',
|
||||
connectionToSeed: parentIdea
|
||||
? `Manual response to "${(parentIdea.title || '').substring(0, 40)}"`
|
||||
: 'Manual idea added by participant',
|
||||
noveltyScore: null,
|
||||
parentIdeaId: parentIdeaId || null,
|
||||
createdBy: session.user.id,
|
||||
createdByType: 'human',
|
||||
positionX: Math.cos(angle) * radius,
|
||||
positionY: Math.sin(angle) * radius,
|
||||
},
|
||||
})
|
||||
|
||||
// [UPDATE - SÉCURITÉ] Recherche vectorielle guest-safe
|
||||
let relatedNoteIds: string[] = []
|
||||
try {
|
||||
const { isGuest, publicNoteIds, aiUserId } = await resolveAiContextUserId(sessionId, session.user.id)
|
||||
|
||||
if (isGuest && (!publicNoteIds || publicNoteIds.length === 0)) {
|
||||
// Invité sans notes publiques → skip la recherche vectorielle
|
||||
relatedNoteIds = []
|
||||
} else {
|
||||
const embedding = await embeddingService.generateEmbedding(`${title} ${description || ''}`)
|
||||
const vectorStr = embeddingService.toVectorString(embedding.embedding)
|
||||
|
||||
let results: any[]
|
||||
if (isGuest && publicNoteIds && publicNoteIds.length > 0) {
|
||||
// Invité : restreindre aux notes publiques uniquement
|
||||
const idList = publicNoteIds.map(id => `'${id}'`).join(',')
|
||||
results = await prisma.$queryRawUnsafe(
|
||||
`SELECT n.id
|
||||
FROM "NoteEmbedding" e
|
||||
JOIN "Note" n ON n.id = e."noteId"
|
||||
WHERE n.id IN (${idList}) AND n."trashedAt" IS NULL
|
||||
ORDER BY e.embedding <=> $1::vector
|
||||
LIMIT 3`,
|
||||
vectorStr
|
||||
) as any[]
|
||||
} else {
|
||||
// Hôte : accès complet
|
||||
results = await prisma.$queryRawUnsafe(
|
||||
`SELECT n.id
|
||||
FROM "NoteEmbedding" e
|
||||
JOIN "Note" n ON n.id = e."noteId"
|
||||
WHERE n."userId" = $1 AND n."trashedAt" IS NULL
|
||||
ORDER BY e.embedding <=> $2::vector
|
||||
LIMIT 3`,
|
||||
aiUserId, vectorStr
|
||||
) as any[]
|
||||
}
|
||||
relatedNoteIds = results.map((r: any) => r.id)
|
||||
}
|
||||
} catch {}
|
||||
|
||||
if (relatedNoteIds.length > 0) {
|
||||
const notes = await prisma.note.findMany({
|
||||
where: { id: { in: relatedNoteIds } },
|
||||
select: { id: true, title: true },
|
||||
})
|
||||
for (const note of notes) {
|
||||
await prisma.brainstormNoteRef.create({
|
||||
data: {
|
||||
ideaId: idea.id,
|
||||
noteId: note.id,
|
||||
relation: 'extends',
|
||||
explanation: `Manual idea — related to your note "${note.title}"`,
|
||||
},
|
||||
})
|
||||
}
|
||||
await prisma.brainstormIdea.update({
|
||||
where: { id: idea.id },
|
||||
data: { relatedNoteIds: JSON.stringify(relatedNoteIds) },
|
||||
})
|
||||
}
|
||||
|
||||
await logActivity(sessionId, 'manual_idea', session.user.id, { ideaTitle: title, ideaId: idea.id })
|
||||
|
||||
await captureSnapshot(sessionId, `Manual idea: ${title}`).catch(() => {})
|
||||
|
||||
// [UPDATE - TEMPS RÉEL] Retourner immédiatement, enrichissement IA en arrière-plan
|
||||
// Le client est notifié via Socket : idea:ai_processing → idea:ai_completed | idea:ai_failed
|
||||
const immediateResponse = NextResponse.json(
|
||||
{ success: true, data: { ideaId: idea.id, title, status: 'ai_processing' } },
|
||||
{ status: 201 }
|
||||
)
|
||||
|
||||
// Capturer les valeurs avant la closure asynchrone (session.user peut être undefined plus tard)
|
||||
const requestingUserId = session.user!.id
|
||||
|
||||
const enrichAsync = async () => {
|
||||
try {
|
||||
// Notifier le room que l'IA traite ce nœud (bordure pulsante violet)
|
||||
await emitToSession(sessionId, 'idea:ai_processing', {
|
||||
ideaId: idea.id,
|
||||
triggeredBy: requestingUserId,
|
||||
})
|
||||
|
||||
const config = await getSystemConfig()
|
||||
const provider = getTagsProvider(config)
|
||||
const lang = locale === 'fr' ? 'French' : locale === 'es' ? 'Spanish' : locale === 'de' ? 'German' : locale === 'it' ? 'Italian' : locale === 'pt' ? 'Portuguese' : locale === 'ja' ? 'Japanese' : locale === 'ko' ? 'Korean' : locale === 'zh' ? 'Chinese' : locale === 'ar' ? 'Arabic' : "the user's language"
|
||||
|
||||
const enrichPrompt = `You are an idea enrichment assistant. Given a user's raw brainstorm idea and context, produce a JSON object with:
|
||||
- "enrichedTitle": a polished, concise version of the title (max 60 chars)
|
||||
- "enrichedDescription": an expanded 2-3 sentence description that develops the idea further
|
||||
- "connectionToSeed": a 1-sentence explanation of how this idea connects to the seed topic
|
||||
- "noveltyScore": a number 1-10 rating how novel/original this idea is
|
||||
|
||||
IMPORTANT: You MUST write ALL text in ${lang}.
|
||||
|
||||
Seed topic: "${brainstormSession.seedIdea}"
|
||||
${parentIdea ? `Parent idea this responds to: "${parentIdea.title}"` : ''}
|
||||
User's raw idea title: "${title}"
|
||||
User's raw description: "${description || 'none provided'}"
|
||||
|
||||
Respond ONLY with the JSON object, no markdown.`
|
||||
|
||||
const raw = await provider.generateText(enrichPrompt)
|
||||
const cleaned = raw.replace(/```json\n?/g, '').replace(/```\n?/g, '').trim()
|
||||
const enriched = JSON.parse(cleaned)
|
||||
|
||||
const enrichedTitle = enriched.enrichedTitle?.substring(0, 60) || title
|
||||
const enrichedDescription = enriched.enrichedDescription || description || ''
|
||||
const connectionToSeed = enriched.connectionToSeed || 'Manual idea added by participant'
|
||||
const noveltyScore = enriched.noveltyScore || null
|
||||
|
||||
await prisma.brainstormIdea.update({
|
||||
where: { id: idea.id },
|
||||
data: { title: enrichedTitle, description: enrichedDescription, connectionToSeed, noveltyScore },
|
||||
})
|
||||
|
||||
// [UPDATE - TEMPS RÉEL] Notifier la complétion avec les données enrichies
|
||||
await emitToSession(sessionId, 'idea:ai_completed', {
|
||||
ideaId: idea.id,
|
||||
title: enrichedTitle,
|
||||
description: enrichedDescription,
|
||||
noveltyScore,
|
||||
connectionToSeed,
|
||||
})
|
||||
} catch (enrichError) {
|
||||
console.error('Enrichment failed, keeping raw idea:', enrichError)
|
||||
// [UPDATE - TEMPS RÉEL] Notifier l'échec — le nœud reste en état dégradé
|
||||
await emitToSession(sessionId, 'idea:ai_failed', { ideaId: idea.id })
|
||||
}
|
||||
}
|
||||
|
||||
// setImmediate pour ne pas bloquer la réponse HTTP
|
||||
setImmediate(() => { enrichAsync() })
|
||||
|
||||
return immediateResponse
|
||||
} catch (error: any) {
|
||||
if (error instanceof z.ZodError) {
|
||||
return NextResponse.json({ error: error.issues }, { status: 400 })
|
||||
}
|
||||
console.error('Error adding manual idea:', error)
|
||||
return NextResponse.json({ error: 'Failed to add idea' }, { status: 500 })
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user