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>
43 lines
1.6 KiB
TypeScript
43 lines
1.6 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { auth } from '@/auth'
|
|
import { getTagsProvider } from '@/lib/ai/factory'
|
|
import { getSystemConfig } from '@/lib/config'
|
|
import { hasUserAiConsent } from '@/lib/consent/server-consent'
|
|
import { reserveUsageOrThrow, QuotaExceededError } from '@/lib/entitlements'
|
|
|
|
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 NextResponse.json({ error: 'ai_consent_required' }, { status: 403 })
|
|
}
|
|
|
|
const { text, targetLanguage } = await request.json()
|
|
|
|
if (!text || !targetLanguage) {
|
|
return NextResponse.json({ error: 'text and targetLanguage are required' }, { status: 400 })
|
|
}
|
|
|
|
const config = await getSystemConfig()
|
|
const provider = getTagsProvider(config)
|
|
|
|
await reserveUsageOrThrow(session.user.id, 'reformulate')
|
|
|
|
const prompt = `Translate the following text to ${targetLanguage}. Return ONLY the translated text, no explanation, no preamble, no quotes:\n\n${text}`
|
|
|
|
const translatedText = await provider.generateText(prompt)
|
|
|
|
return NextResponse.json({ translatedText: translatedText.trim() })
|
|
} catch (error: unknown) {
|
|
if (error instanceof QuotaExceededError) {
|
|
return NextResponse.json(error.toJSON(), { status: 429 })
|
|
}
|
|
const message = error instanceof Error ? error.message : 'Translation failed'
|
|
return NextResponse.json({ error: message }, { status: 500 })
|
|
}
|
|
}
|