95 lines
3.6 KiB
TypeScript
95 lines
3.6 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { GoogleGenAI } from '@google/genai'
|
|
import { getAuthUser } from '@/lib/auth-utils'
|
|
|
|
const RATE_LIMIT_WINDOW_MS = 60 * 1000 // 1 minute
|
|
const MAX_REQUESTS_PER_WINDOW = 10
|
|
const rateLimitMap = new Map<string, { count: number; resetAt: number }>()
|
|
|
|
function checkRateLimit(key: string): boolean {
|
|
const now = Date.now()
|
|
const entry = rateLimitMap.get(key)
|
|
if (!entry || now > entry.resetAt) {
|
|
rateLimitMap.set(key, { count: 1, resetAt: now + RATE_LIMIT_WINDOW_MS })
|
|
return true
|
|
}
|
|
if (entry.count >= MAX_REQUESTS_PER_WINDOW) {
|
|
return false
|
|
}
|
|
entry.count += 1
|
|
return true
|
|
}
|
|
|
|
// POST /api/gemini — AI content generation
|
|
export async function POST(request: NextRequest) {
|
|
try {
|
|
const authUser = await getAuthUser(request)
|
|
if (!authUser) {
|
|
return NextResponse.json(
|
|
{ error: 'Authentification requise' },
|
|
{ status: 401 }
|
|
)
|
|
}
|
|
|
|
if (!checkRateLimit(authUser.id)) {
|
|
return NextResponse.json(
|
|
{ error: 'Trop de requêtes IA. Veuillez patienter une minute.' },
|
|
{ status: 429 }
|
|
)
|
|
}
|
|
|
|
const apiKey = process.env.GEMINI_API_KEY
|
|
if (!apiKey || apiKey === 'PLACEHOLDER_API_KEY') {
|
|
return NextResponse.json(
|
|
{ error: 'Clé API Gemini non configurée' },
|
|
{ status: 503 }
|
|
)
|
|
}
|
|
|
|
const ai = new GoogleGenAI({ apiKey })
|
|
const body = await request.json()
|
|
const action = typeof body.action === 'string' ? body.action : ''
|
|
const name = typeof body.name === 'string' ? body.name.trim().slice(0, 100) : ''
|
|
const category = typeof body.category === 'string' ? body.category.trim().slice(0, 100) : ''
|
|
const keywords = typeof body.keywords === 'string' ? body.keywords.trim().slice(0, 300) : ''
|
|
|
|
if (action === 'description') {
|
|
const prompt = `
|
|
Tu es un expert en copywriting marketing pour Afrohub.
|
|
Rédige une description professionnelle, attrayante et optimisée SEO pour une entreprise.
|
|
|
|
Nom de l'entreprise : ${name}
|
|
Secteur : ${category}
|
|
Mots-clés/Services : ${keywords}
|
|
|
|
La description doit faire environ 80-100 mots, être en français, inspirer confiance et professionnalisme.
|
|
Ne mets pas de guillemets au début ou à la fin.
|
|
`
|
|
const response = await ai.models.generateContent({
|
|
model: 'gemini-2.5-flash',
|
|
contents: prompt,
|
|
})
|
|
return NextResponse.json({ text: response.text || '' })
|
|
}
|
|
|
|
if (action === 'slogans') {
|
|
const prompt = `Donne-moi 3 idées de slogans courts et percutants pour une entreprise dans le secteur : ${category}. Retourne uniquement une liste JSON de chaînes de caractères.`
|
|
const response = await ai.models.generateContent({
|
|
model: 'gemini-2.5-flash',
|
|
contents: prompt,
|
|
config: { responseMimeType: 'application/json' },
|
|
})
|
|
const json = JSON.parse(response.text || '[]')
|
|
return NextResponse.json({ slogans: Array.isArray(json) ? json : [] })
|
|
}
|
|
|
|
return NextResponse.json({ error: 'Action inconnue' }, { status: 400 })
|
|
} catch (error) {
|
|
console.error('POST /api/gemini error:', error)
|
|
return NextResponse.json(
|
|
{ error: 'Erreur lors de la génération IA' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|