Files
Momento/memento-note/app/api/ai/organize-notebook/route.ts
Antigravity ee70e74bf5
All checks were successful
CI / Lint, Unit Tests & Build (push) Successful in 5m39s
CI / Deploy production (on server) (push) Successful in 22s
fix: 5 bugs critiques de l'éditeur (Phase 1 audit)
1. replaceAll (Find & Replace) — une seule transaction ProseMirror
   au lieu d'un forEach cassé. Tous les matchs sont maintenant remplacés.

2. Link Preview unwrap — deleteNode() au lieu de clearer les attrs
   qui laissaient un nœud fantôme invisible dans le document.

3. Conversion Markdown → richtext — breaks: true dans marked.parse()
   Les simple newlines sont maintenant convertis en <br>.
   + préserve les blocs custom (toggle, callout, math, columns,
   outline, link-preview) en commentaires HTML lors de l'export MD.

4. emitNoteChange exercices — shape corrigée (type:'created' attend
   un objet Note, pas noteId/notebookId séparés).

5. Raccourcis clavier sans conflit :
   Cmd+Shift+C → Cmd+Alt+C (callout, avant: copier)
   Cmd+Shift+O → Cmd+Alt+O (outline, avant: historique/signets)
   Cmd+Shift+L → Cmd+Alt+L (colonnes, avant: lock screen macOS)
2026-06-20 15:48:18 +00:00

96 lines
3.2 KiB
TypeScript

import { NextRequest, NextResponse } from 'next/server'
import { auth } from '@/auth'
import prisma from '@/lib/prisma'
import { notebookOrganizerService } from '@/lib/ai/services/notebook-organizer.service'
import { reserveUsageOrThrow, QuotaExceededError } from '@/lib/entitlements'
import { syncNoteLabels } from '@/app/actions/notes'
export async function POST(request: NextRequest) {
try {
const session = await auth()
if (!session?.user?.id) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
}
const { notebookId } = await request.json()
if (!notebookId) {
return NextResponse.json({ error: 'notebookId is required' }, { status: 400 })
}
try {
await reserveUsageOrThrow(session.user.id, 'reformulate')
} catch (err) {
if (err instanceof QuotaExceededError) {
const isTierLocked = err.currentQuota === 0
return NextResponse.json(
{ error: isTierLocked ? 'feature_locked' : 'quota_exceeded', errorKey: isTierLocked ? 'ai.featureLocked' : 'ai.quotaExceeded' },
{ status: 402 },
)
}
throw err
}
const notes = await prisma.note.findMany({
where: { notebookId, trashedAt: null, userId: session.user.id },
select: { id: true, title: true, content: true },
orderBy: { order: 'asc' },
})
if (notes.length < 2) {
return NextResponse.json({ error: 'Need at least 2 notes to organize' }, { status: 400 })
}
const notesForAnalysis = notes.map(n => ({
id: n.id,
title: n.title || 'Sans titre',
contentPreview: n.content.replace(/<[^>]+>/g, ' ').replace(/\s+/g, ' ').trim().slice(0, 300),
}))
const result = await notebookOrganizerService.analyze(notesForAnalysis)
return NextResponse.json(result)
} catch (error: any) {
console.error('[Notebook Organizer] Error:', error)
return NextResponse.json({ error: error.message || 'Failed to organize notebook' }, { status: 500 })
}
}
export async function PATCH(request: NextRequest) {
try {
const session = await auth()
if (!session?.user?.id) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
}
const { action, tagName, noteIds, notebookId } = await request.json()
if (action === 'apply_tag' && tagName && Array.isArray(noteIds)) {
// Fetch existing labels for each note, merge with new tag
for (const noteId of noteIds) {
const note = await prisma.note.findUnique({
where: { id: noteId },
select: { labels: true, notebookId: true },
})
if (!note) continue
let existingLabels: string[] = []
try { existingLabels = note.labels ? JSON.parse(note.labels) : [] } catch {}
if (!existingLabels.includes(tagName)) {
existingLabels.push(tagName)
}
await syncNoteLabels(noteId, existingLabels, note.notebookId, session.user.id)
}
return NextResponse.json({ success: true, applied: noteIds.length })
}
return NextResponse.json({ error: 'Invalid action' }, { status: 400 })
} catch (error: any) {
console.error('[Notebook Organizer PATCH] Error:', error)
return NextResponse.json({ error: error.message }, { status: 500 })
}
}