|
| 1 | +// Proxy serverless — Vercel detecta automaticamente arquivos em /api |
| 2 | +// Guarda GEMINI_API_KEY nas variáveis de ambiente do Vercel (nunca no Git). |
| 3 | + |
| 4 | +export const config = { runtime: 'edge' }; |
| 5 | + |
| 6 | +interface ProxyBody { |
| 7 | + action: string; |
| 8 | + code: string; |
| 9 | + history?: { role: 'user' | 'model'; text: string }[]; |
| 10 | + systemInstruction: string; |
| 11 | + actionContext: string; |
| 12 | + model?: string; |
| 13 | +} |
| 14 | + |
| 15 | +export default async function handler(req: Request): Promise<Response> { |
| 16 | + const origin = req.headers.get('origin') ?? '*'; |
| 17 | + const cors = { |
| 18 | + 'Access-Control-Allow-Origin': origin, |
| 19 | + 'Access-Control-Allow-Methods': 'POST, OPTIONS', |
| 20 | + 'Access-Control-Allow-Headers': 'Content-Type', |
| 21 | + }; |
| 22 | + |
| 23 | + if (req.method === 'OPTIONS') return new Response(null, { headers: cors }); |
| 24 | + if (req.method !== 'POST') { |
| 25 | + return new Response(JSON.stringify({ error: 'Method not allowed' }), { |
| 26 | + status: 405, |
| 27 | + headers: { ...cors, 'Content-Type': 'application/json' }, |
| 28 | + }); |
| 29 | + } |
| 30 | + |
| 31 | + const apiKey = process.env.GEMINI_API_KEY; |
| 32 | + if (!apiKey) { |
| 33 | + return new Response(JSON.stringify({ error: 'GEMINI_API_KEY não configurada.' }), { |
| 34 | + status: 401, |
| 35 | + headers: { ...cors, 'Content-Type': 'application/json' }, |
| 36 | + }); |
| 37 | + } |
| 38 | + |
| 39 | + let body: ProxyBody; |
| 40 | + try { |
| 41 | + body = (await req.json()) as ProxyBody; |
| 42 | + } catch { |
| 43 | + return new Response(JSON.stringify({ error: 'JSON inválido.' }), { |
| 44 | + status: 400, |
| 45 | + headers: { ...cors, 'Content-Type': 'application/json' }, |
| 46 | + }); |
| 47 | + } |
| 48 | + |
| 49 | + const model = body.model ?? 'gemini-2.5-flash'; |
| 50 | + const url = `https://generativelanguage.googleapis.com/v1beta/models/${model}:generateContent?key=${apiKey}`; |
| 51 | + |
| 52 | + const contents = [ |
| 53 | + ...(body.history ?? []).map((h) => ({ |
| 54 | + role: h.role, |
| 55 | + parts: [{ text: h.text }], |
| 56 | + })), |
| 57 | + { |
| 58 | + role: 'user', |
| 59 | + parts: [{ text: `${body.actionContext}\n\nCódigo:\n${body.code}` }], |
| 60 | + }, |
| 61 | + ]; |
| 62 | + |
| 63 | + let upstream: Response; |
| 64 | + try { |
| 65 | + upstream = await fetch(url, { |
| 66 | + method: 'POST', |
| 67 | + headers: { 'Content-Type': 'application/json' }, |
| 68 | + body: JSON.stringify({ |
| 69 | + systemInstruction: { parts: [{ text: body.systemInstruction }] }, |
| 70 | + contents, |
| 71 | + }), |
| 72 | + }); |
| 73 | + } catch { |
| 74 | + return new Response(JSON.stringify({ error: 'Falha ao conectar à Gemini API.' }), { |
| 75 | + status: 502, |
| 76 | + headers: { ...cors, 'Content-Type': 'application/json' }, |
| 77 | + }); |
| 78 | + } |
| 79 | + |
| 80 | + if (!upstream.ok) { |
| 81 | + if (upstream.status === 429) { |
| 82 | + const text = await upstream.text(); |
| 83 | + const scope = /per day|daily/i.test(text) ? 'daily' : 'minute'; |
| 84 | + return new Response(JSON.stringify({ error: 'Rate limited' }), { |
| 85 | + status: 429, |
| 86 | + headers: { ...cors, 'Content-Type': 'application/json', 'X-Quota-Scope': scope }, |
| 87 | + }); |
| 88 | + } |
| 89 | + if (upstream.status === 401 || upstream.status === 403) { |
| 90 | + return new Response(JSON.stringify({ error: 'Chave inválida.' }), { |
| 91 | + status: upstream.status, |
| 92 | + headers: { ...cors, 'Content-Type': 'application/json' }, |
| 93 | + }); |
| 94 | + } |
| 95 | + return new Response(JSON.stringify({ error: `Gemini API retornou ${upstream.status}` }), { |
| 96 | + status: 502, |
| 97 | + headers: { ...cors, 'Content-Type': 'application/json' }, |
| 98 | + }); |
| 99 | + } |
| 100 | + |
| 101 | + const data = await upstream.json(); |
| 102 | + const text: string = |
| 103 | + data?.candidates?.[0]?.content?.parts |
| 104 | + ?.map((p: { text?: string }) => p.text ?? '') |
| 105 | + .join('') ?? ''; |
| 106 | + |
| 107 | + return new Response(JSON.stringify({ text }), { |
| 108 | + status: 200, |
| 109 | + headers: { ...cors, 'Content-Type': 'application/json' }, |
| 110 | + }); |
| 111 | +} |
0 commit comments