Files
Momento/memento-note/app/api/ai/notebook-slides/route.ts
Antigravity 324bf40658
All checks were successful
CI / Lint, Unit Tests & Build (push) Successful in 7m20s
CI / Deploy production (on server) (push) Successful in 24s
fix: replace raw ai_consent_required code with translatable message and consent prompt
- Centralize AI consent 403 responses in server-consent helpers
- Return human-readable message + code + i18n errorKey from all AI APIs
- Update dashboard and AI clients to detect code/errorKey and translate
- Add consent.ai.requiredToast i18n keys (fr/en)
2026-07-18 17:32:00 +00:00

63 lines
2.0 KiB
TypeScript

import { NextRequest, NextResponse } from 'next/server'
import { auth } from '@/auth'
import { withAiQuota, handleQuotaHttpError } from '@/lib/ai-quota'
import { hasUserAiConsent, aiConsentForbiddenResponse } from '@/lib/consent/server-consent'
import { generateSlideDeck } from '@/lib/ai/services/slide-deck-generator.service'
export async function POST(request: NextRequest) {
try {
const session = await auth()
if (!session?.user?.id) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
}
if (!(await hasUserAiConsent())) {
return aiConsentForbiddenResponse()
}
const userId = session.user.id
const body = await request.json()
const { notebookId, theme, purpose, audience, slideCount, language } = body
if (!notebookId || typeof notebookId !== 'string') {
return NextResponse.json({ error: 'notebookId is required' }, { status: 400 })
}
const intent =
purpose || audience || slideCount
? {
purpose: purpose || 'auto',
audience: audience || 'auto',
slideCount: typeof slideCount === 'number' ? slideCount : undefined,
template: 'auto' as const,
}
: null
const result = await withAiQuota(
userId,
'slide_generate',
() =>
generateSlideDeck({
userId,
sourceNotebookId: notebookId,
theme: theme && theme !== 'auto' ? theme : null,
lang: language === 'en' ? 'en' : 'fr',
intent,
}),
{ lane: 'chat' },
)
if (!result.success) {
return NextResponse.json({ error: result.error || 'Failed to generate slides' }, { status: 500 })
}
return NextResponse.json(result)
} catch (error: unknown) {
const quotaResp = handleQuotaHttpError(error)
if (quotaResp) return quotaResp
console.error('[NotebookSlides] Error:', error)
const message = error instanceof Error ? error.message : 'Failed to generate slides'
return NextResponse.json({ error: message }, { status: 500 })
}
}