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>
51 lines
1.8 KiB
TypeScript
51 lines
1.8 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { getMobileUserId } from '@/lib/mobile-auth'
|
|
import { getSystemConfig } from '@/lib/config'
|
|
import { reserveUsageOrThrow, QuotaExceededError } from '@/lib/entitlements'
|
|
|
|
export async function POST(req: NextRequest) {
|
|
const userId = await getMobileUserId(req)
|
|
if (!userId) return NextResponse.json({ error: 'Non autorisé' }, { status: 401 })
|
|
|
|
const formData = await req.formData().catch(() => null)
|
|
if (!formData) return NextResponse.json({ error: 'Fichier audio requis' }, { status: 400 })
|
|
|
|
const file = formData.get('audio')
|
|
if (!file || !(file instanceof Blob)) {
|
|
return NextResponse.json({ error: 'Fichier audio manquant' }, { status: 400 })
|
|
}
|
|
|
|
const config = await getSystemConfig()
|
|
const apiKey = config.OPENAI_API_KEY
|
|
if (!apiKey) return NextResponse.json({ error: 'Service non disponible' }, { status: 503 })
|
|
|
|
try {
|
|
await reserveUsageOrThrow(userId, 'voice_transcribe')
|
|
} catch (err) {
|
|
if (err instanceof QuotaExceededError) {
|
|
return NextResponse.json(err.toJSON(), { status: 429 })
|
|
}
|
|
throw err
|
|
}
|
|
|
|
const whisperForm = new FormData()
|
|
whisperForm.append('file', file, 'audio.m4a')
|
|
whisperForm.append('model', 'whisper-1')
|
|
whisperForm.append('response_format', 'json')
|
|
|
|
const whisperRes = await fetch('https://api.openai.com/v1/audio/transcriptions', {
|
|
method: 'POST',
|
|
headers: { Authorization: `Bearer ${apiKey}` },
|
|
body: whisperForm,
|
|
})
|
|
|
|
if (!whisperRes.ok) {
|
|
const err = await whisperRes.text()
|
|
console.error('[mobile/ai/transcribe] Whisper error:', err)
|
|
return NextResponse.json({ error: 'Erreur transcription' }, { status: 500 })
|
|
}
|
|
|
|
const { text } = await whisperRes.json()
|
|
return NextResponse.json({ text: text ?? '' })
|
|
}
|