Files
sitemente/app/api/ai/voice-chat/route.ts
T

99 lines
3.1 KiB
TypeScript

import { NextRequest, NextResponse } from "next/server";
// MiniMax API configuration
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;
// Build conversation context
const systemPrompt = `You are a friendly AI assistant for ${businessName}, a ${businessType} business.
IMPORTANT RULES:
- Always respond in the SAME language the user uses
- Default to Spanish (respond in Spanish unless user clearly speaks English)
- Keep responses SHORT: 1-3 sentences max
- Be helpful, friendly, and professional
- If asked about pricing, mention: €299-€1,950/month depending on plan
- If asked about booking, say you'll help them book
- Never make up specific prices or details you don't know
Current language mode: ${language}`;
// Build messages array
const messages: Message[] = [
{ role: "assistant", content: systemPrompt }
];
// Add history (last 5 messages for context)
if (history.length > 0) {
messages.push(...history.slice(-5));
}
// Add current message
messages.push({ role: "user", content: message });
// Call MiniMax API
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: 500
})
});
if (!response.ok) {
const error = await response.text();
console.error("MiniMax API error:", error);
// Fallback response
const fallbackResponse = language === "es"
? "Lo siento, tuve un problema técnico. ¿Puedes repetir tu pregunta?"
: "Sorry, I had a technical issue. Can you repeat your question?";
return NextResponse.json({ response: fallbackResponse });
}
const data = await response.json();
const aiResponse = data.choices?.[0]?.message?.content ||
data.reply ||
(language === "es"
? "Gracias por tu mensaje. ¿En qué más puedo ayudarte?"
: "Thanks for your message. How else can I help you?");
return NextResponse.json({ response: aiResponse });
} catch (error) {
console.error("Voice chat API error:", error);
return NextResponse.json(
{ error: "Internal server error" },
{ status: 500 }
);
}
}
// Disable body parsing for streaming if needed
export const runtime = "nodejs";