POST /api/leads/:id/analizar lee toda la conversacion_whatsapp del lead, extrae con un LLM (OpenRouter) los datos clave (tipoReforma, m2, calidad, urgencia, presupuesto, viable + crudos) y los persiste en el lead de una pasada. Robusto frente a la extracción turno-a-turno frágil del bot. El bot lo llamará al cerrar la cualificación. Helper lib/ai/openrouter.ts + env OPENROUTER_API_KEY. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
42 lines
1.3 KiB
TypeScript
42 lines
1.3 KiB
TypeScript
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<unknown> {
|
|
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');
|
|
}
|
|
}
|