Centralise la réserve via ai-quota, corrige admin unavailable (-1), brancher les routes sans quota et le host-pays brainstorm, avec usage-meter élargi, noms de clusters, MCP et ajustements dashboard/insights. Co-authored-by: Cursor <cursoragent@cursor.com>
59 lines
1.8 KiB
TypeScript
59 lines
1.8 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { auth } from '@/auth'
|
|
import { notebookSuggestionService } from '@/lib/ai/services/notebook-suggestion.service'
|
|
import { hasUserAiConsent } from '@/lib/consent/server-consent'
|
|
import { withAiQuota, handleQuotaHttpError } from '@/lib/ai-quota'
|
|
|
|
export async function POST(req: NextRequest) {
|
|
try {
|
|
const session = await auth()
|
|
if (!session?.user?.id) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
}
|
|
|
|
if (!(await hasUserAiConsent())) {
|
|
return NextResponse.json({ error: 'ai_consent_required' }, { status: 403 })
|
|
}
|
|
|
|
const body = await req.json()
|
|
const { noteContent, language = 'en' } = body
|
|
|
|
if (!noteContent || typeof noteContent !== 'string') {
|
|
return NextResponse.json({ error: 'noteContent is required' }, { status: 400 })
|
|
}
|
|
|
|
const wordCount = noteContent.trim().split(/\s+/).length
|
|
if (wordCount < 20) {
|
|
return NextResponse.json({
|
|
suggestion: null,
|
|
reason: 'content_too_short',
|
|
message: 'Note content too short for meaningful suggestion'
|
|
})
|
|
}
|
|
|
|
const suggestedNotebook = await withAiQuota(
|
|
session.user.id,
|
|
'auto_tag',
|
|
() => notebookSuggestionService.suggestNotebook(
|
|
noteContent,
|
|
session.user!.id,
|
|
language
|
|
),
|
|
{ lane: 'tags' },
|
|
)
|
|
|
|
return NextResponse.json({
|
|
suggestion: suggestedNotebook,
|
|
confidence: suggestedNotebook ? 0.8 : 0
|
|
})
|
|
} catch (error) {
|
|
const quotaResp = handleQuotaHttpError(error)
|
|
if (quotaResp) return quotaResp
|
|
console.error('[/api/ai/suggest-notebook] Error:', error)
|
|
return NextResponse.json(
|
|
{ error: 'Failed to generate suggestion' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|