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:
@@ -0,0 +1,87 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import prisma from '@/lib/prisma'
|
||||
import { auth } from '@/auth'
|
||||
import fs from 'fs'
|
||||
|
||||
export async function GET(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string; attachmentId: string }> }
|
||||
) {
|
||||
const session = await auth()
|
||||
if (!session?.user?.id) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
}
|
||||
|
||||
try {
|
||||
const { id: noteId, attachmentId } = await params
|
||||
|
||||
const download = request.nextUrl.searchParams.get('download')
|
||||
|
||||
const attachment = await prisma.noteAttachment.findFirst({
|
||||
where: { id: attachmentId, noteId, note: { userId: session.user.id } },
|
||||
include: download ? undefined : {
|
||||
chunks: {
|
||||
select: { id: true, chunkIndex: true, pageNumber: true },
|
||||
orderBy: { chunkIndex: 'asc' },
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
if (!attachment) {
|
||||
return NextResponse.json({ error: 'Not found' }, { status: 404 })
|
||||
}
|
||||
|
||||
if (download) {
|
||||
if (!fs.existsSync(attachment.filePath)) {
|
||||
return NextResponse.json({ error: 'File not found' }, { status: 404 })
|
||||
}
|
||||
const fileBuffer = fs.readFileSync(attachment.filePath)
|
||||
return new NextResponse(fileBuffer, {
|
||||
headers: {
|
||||
'Content-Type': attachment.mimeType || 'application/pdf',
|
||||
'Content-Disposition': `inline; filename="${attachment.fileName}"`,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
return NextResponse.json({ success: true, data: attachment })
|
||||
} catch (error) {
|
||||
console.error('Error fetching attachment:', error)
|
||||
return NextResponse.json({ error: 'Failed to fetch attachment' }, { status: 500 })
|
||||
}
|
||||
}
|
||||
|
||||
export async function DELETE(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string; attachmentId: string }> }
|
||||
) {
|
||||
const session = await auth()
|
||||
if (!session?.user?.id) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
}
|
||||
|
||||
try {
|
||||
const { id: noteId, attachmentId } = await params
|
||||
|
||||
const attachment = await prisma.noteAttachment.findFirst({
|
||||
where: { id: attachmentId, noteId, note: { userId: session.user.id } },
|
||||
})
|
||||
|
||||
if (!attachment) {
|
||||
return NextResponse.json({ error: 'Not found' }, { status: 404 })
|
||||
}
|
||||
|
||||
try {
|
||||
if (fs.existsSync(attachment.filePath)) {
|
||||
fs.unlinkSync(attachment.filePath)
|
||||
}
|
||||
} catch {}
|
||||
|
||||
await prisma.noteAttachment.delete({ where: { id: attachmentId } })
|
||||
|
||||
return NextResponse.json({ success: true })
|
||||
} catch (error) {
|
||||
console.error('Error deleting attachment:', error)
|
||||
return NextResponse.json({ error: 'Failed to delete attachment' }, { status: 500 })
|
||||
}
|
||||
}
|
||||
112
memento-note/app/api/notes/[id]/attachments/route.ts
Normal file
112
memento-note/app/api/notes/[id]/attachments/route.ts
Normal file
@@ -0,0 +1,112 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import prisma from '@/lib/prisma'
|
||||
import { auth } from '@/auth'
|
||||
import { documentIngestionService } from '@/lib/ai/services/document-ingestion.service'
|
||||
import fs from 'fs'
|
||||
import path from 'path'
|
||||
import { randomUUID } from 'crypto'
|
||||
|
||||
export async function POST(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string }> }
|
||||
) {
|
||||
const session = await auth()
|
||||
if (!session?.user?.id) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
}
|
||||
|
||||
try {
|
||||
const { id: noteId } = await params
|
||||
|
||||
const note = await prisma.note.findFirst({
|
||||
where: { id: noteId, userId: session.user.id, trashedAt: null },
|
||||
})
|
||||
if (!note) {
|
||||
return NextResponse.json({ error: 'Note not found' }, { status: 404 })
|
||||
}
|
||||
|
||||
const formData = await request.formData()
|
||||
const file = formData.get('file') as File | null
|
||||
if (!file) {
|
||||
return NextResponse.json({ error: 'No file provided' }, { status: 400 })
|
||||
}
|
||||
|
||||
if (file.size > 20 * 1024 * 1024) {
|
||||
return NextResponse.json({ error: 'File too large (max 20MB)' }, { status: 400 })
|
||||
}
|
||||
|
||||
if (file.type !== 'application/pdf') {
|
||||
return NextResponse.json({ error: 'Only PDF files are supported' }, { status: 400 })
|
||||
}
|
||||
|
||||
const dir = path.join(process.cwd(), 'data', 'uploads', 'attachments', noteId)
|
||||
fs.mkdirSync(dir, { recursive: true })
|
||||
const fileId = randomUUID()
|
||||
const filePath = path.join(dir, `${fileId}.pdf`)
|
||||
fs.writeFileSync(filePath, Buffer.from(await file.arrayBuffer()))
|
||||
|
||||
const attachment = await prisma.noteAttachment.create({
|
||||
data: {
|
||||
noteId,
|
||||
fileName: file.name,
|
||||
fileType: file.type,
|
||||
fileSize: file.size,
|
||||
filePath,
|
||||
mimeType: file.type,
|
||||
status: 'pending',
|
||||
},
|
||||
})
|
||||
|
||||
setImmediate(() => {
|
||||
documentIngestionService.ingest(attachment.id).catch((err) => {
|
||||
console.error('Document ingestion failed:', err)
|
||||
})
|
||||
})
|
||||
|
||||
return NextResponse.json({ success: true, data: attachment }, { status: 201 })
|
||||
} catch (error) {
|
||||
console.error('Error uploading attachment:', error)
|
||||
return NextResponse.json({ error: 'Upload failed' }, { status: 500 })
|
||||
}
|
||||
}
|
||||
|
||||
export async function GET(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string }> }
|
||||
) {
|
||||
const session = await auth()
|
||||
if (!session?.user?.id) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
}
|
||||
|
||||
try {
|
||||
const { id: noteId } = await params
|
||||
|
||||
const note = await prisma.note.findFirst({
|
||||
where: { id: noteId, userId: session.user.id, trashedAt: null },
|
||||
})
|
||||
if (!note) {
|
||||
return NextResponse.json({ error: 'Note not found' }, { status: 404 })
|
||||
}
|
||||
|
||||
const attachments = await prisma.noteAttachment.findMany({
|
||||
where: { noteId },
|
||||
orderBy: { createdAt: 'desc' },
|
||||
select: {
|
||||
id: true,
|
||||
fileName: true,
|
||||
fileSize: true,
|
||||
mimeType: true,
|
||||
status: true,
|
||||
pageCount: true,
|
||||
error: true,
|
||||
createdAt: true,
|
||||
},
|
||||
})
|
||||
|
||||
return NextResponse.json({ success: true, data: attachments })
|
||||
} catch (error) {
|
||||
console.error('Error fetching attachments:', error)
|
||||
return NextResponse.json({ error: 'Failed to fetch attachments' }, { status: 500 })
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user