100 lines
3.5 KiB
TypeScript
100 lines
3.5 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
|
|
const MINIMAX_API_KEY = process.env.MINIMAX_API_KEY || "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJTaXRlTWVudGUiLCJyb2xlIjoiYW5vbiIsImlhdCI6MTczODcxMjAwMCwiZXhwIjoyMDU0MzcyMDAwfQ.VxYNdTUkG4N0K2T8K3pK4JzY3vN8X9vK2pK3JzY2vN8X9";
|
|
const MINIMAX_BASE_URL = "https://api.minimax.chat/v1";
|
|
|
|
interface Message {
|
|
role: "user" | "assistant";
|
|
content: string;
|
|
}
|
|
|
|
interface RequestBody {
|
|
message: string;
|
|
language: "es" | "en";
|
|
businessType: string;
|
|
businessName: string;
|
|
history?: Message[];
|
|
}
|
|
|
|
export async function POST(request: NextRequest) {
|
|
try {
|
|
const body: RequestBody = await request.json();
|
|
const { message, language, businessType, businessName, history = [] } = body;
|
|
|
|
// STRICT RULES AS PER SPEC
|
|
const systemPrompt = `Eres el asistente de IA de SiteMente, una empresa que ayuda negocios locales en España a implementar inteligencia artificial.
|
|
|
|
REGLAS ESTRICTAS:
|
|
1. NUNCA digas que eres humano. Eres "el asistente de IA" o "inteligencia artificial".
|
|
2. NUNCA prometas precios exactos. Usa "desde €299/mes" o "depende del plan".
|
|
3. NUNCA prometas números garantizados (ventas, clientes, etc).
|
|
4. SIEMPRE guía a reservar una demo de 15 minutos con una persona real.
|
|
5. Mantén respuestas cortas (1-3 oraciones).
|
|
6. Si no entiendes, responde de forma simple.
|
|
|
|
IDIOMA: Responde en el mismo idioma que el usuario. Por defecto español.
|
|
|
|
EJEMPLO DE RESPUESTAS:
|
|
- "¿Cuánto cuesta?" → "Tenemos planes desde 299€/mes. ¿Te gustaría que te enviemos información?"
|
|
- "¿Puedes hacer esto?" → "Seguro que podemos ayudarte. ¿Por qué no agendamos una demo de 15 minutos para hablar?"
|
|
- "No entiendo" → "No he entendido del todo, ¿podrías repetirlo o escribirlo, por favor?"
|
|
|
|
Negocios: ${businessType}. Empresa: ${businessName}.`;
|
|
|
|
// Embed system prompt in first user message for MiniMax compatibility
|
|
const messages: Message[] = [
|
|
{ role: "user", content: `[INSTRUCCIONES DEL SISTEMA]\n${systemPrompt}\n\n[CONVERSACIÓN]\nUsuario: ${message}` }
|
|
];
|
|
|
|
// Add recent history
|
|
if (history.length > 0) {
|
|
messages.push(...history.slice(-4));
|
|
}
|
|
|
|
messages.push({ role: "user", content: message });
|
|
|
|
const response = await fetch(`${MINIMAX_BASE_URL}/text/chatcompletion_v2`, {
|
|
method: "POST",
|
|
headers: {
|
|
"Authorization": `Bearer ${MINIMAX_API_KEY}`,
|
|
"Content-Type": "application/json"
|
|
},
|
|
body: JSON.stringify({
|
|
model: "MiniMax-M2.5",
|
|
messages,
|
|
temperature: 0.7,
|
|
max_tokens: 300
|
|
})
|
|
});
|
|
|
|
if (!response.ok) {
|
|
const error = await response.text();
|
|
console.error("MiniMax error:", error);
|
|
|
|
const fallback = language === "es"
|
|
? "Lo siento, tuve un problema técnico. ¿Podrías escribir tu pregunta?"
|
|
: "Sorry, I had a technical issue. Could you type your question?";
|
|
|
|
return NextResponse.json({ response: fallback });
|
|
}
|
|
|
|
const data = await response.json();
|
|
const aiResponse = data.choices?.[0]?.message?.content ||
|
|
data.reply ||
|
|
(language === "es"
|
|
? "¿En qué más puedo ayudarte?"
|
|
: "How else can I help you?");
|
|
|
|
return NextResponse.json({ response: aiResponse });
|
|
|
|
} catch (error) {
|
|
console.error("Voice chat API error:", error);
|
|
return NextResponse.json(
|
|
{ response: "Lo siento, tuve un problema. ¿Puedes repetir?" },
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|
|
|
|
export const runtime = "nodejs";
|