import { env } from '@/lib/env'; const OPENROUTER_URL = 'https://openrouter.ai/api/v1/chat/completions'; export function openrouterConfigurado(): boolean { return Boolean(env.OPENROUTER_API_KEY); } // Llamada de chat que espera una respuesta JSON. Parseo robusto (tolera fences ```json). export async function chatJSON(system: string, user: string, model?: string): Promise { const res = await fetch(OPENROUTER_URL, { method: 'POST', headers: { Authorization: `Bearer ${env.OPENROUTER_API_KEY}`, 'Content-Type': 'application/json', 'HTTP-Referer': 'https://reformix.es', 'X-Title': 'Reformix App', }, body: JSON.stringify({ model: model || env.OPENROUTER_MODEL_ANALISIS || 'anthropic/claude-haiku-4.5', messages: [ { role: 'system', content: system }, { role: 'user', content: user }, ], temperature: 0.1, max_tokens: 700, }), }); if (!res.ok) { throw new Error(`OpenRouter ${res.status}: ${(await res.text()).slice(0, 300)}`); } const data = await res.json(); const content: string = data?.choices?.[0]?.message?.content ?? ''; try { return JSON.parse(content); } catch { const m = content.match(/\{[\s\S]*\}/); if (m) return JSON.parse(m[0]); throw new Error('La respuesta del modelo no es JSON'); } }