diff --git a/app/api/site-mente/voice/route.ts b/app/api/site-mente/voice/route.ts index cda5d04..8b34a6f 100644 --- a/app/api/site-mente/voice/route.ts +++ b/app/api/site-mente/voice/route.ts @@ -8,6 +8,13 @@ interface VapiMessage { type: string; role?: string; transcript?: string; + toolCallList?: Array<{ + id: string; + function: { + name: string; + arguments: string; + }; + }>; } interface VapiCall { @@ -22,27 +29,55 @@ interface VapiWebhookPayload { export async function POST(request: Request) { try { const body = (await request.json()) as VapiWebhookPayload; - - // Extract transcript from Vapi's format const message = body.message; - // Only process final transcripts + // Handle tool calls from Vapi + if (message.toolCallList && message.toolCallList.length > 0) { + const toolCall = message.toolCallList[0]; + const toolCallId = toolCall.id; + + // Get the arguments from the tool call + let args = {}; + try { + args = JSON.parse(toolCall.function.arguments || '{}'); + } catch (e) { + // If arguments is a string, use it directly + args = { query: toolCall.function.arguments }; + } + + // Process the voice request + const transcript = args.query || ''; + const response = await runSiteMenteVoiceTurn({ + transcript: transcript, + }); + + // Return EXACT Vapi format with toolCallId + return NextResponse.json({ + results: [ + { + toolCallId: toolCallId, + result: response.reply, + }, + ], + }); + } + + // Handle regular transcripts if (!message || message.type !== "transcript" || !message.transcript) { - // Return empty response for non-transcript messages return NextResponse.json({ results: [] }); } const transcript = message.transcript; - - // Call MiniMax brain const response = await runSiteMenteVoiceTurn({ transcript: transcript, }); - // Return in Vapi's expected format + // Return in Vapi's expected format - but we need a toolCallId + // For transcripts without tool calls, we generate one return NextResponse.json({ results: [ { + toolCallId: `call_${Date.now()}`, result: response.reply, }, ], @@ -51,8 +86,15 @@ export async function POST(request: Request) { } catch (error) { console.error("[SiteMente][Vapi] Voice route failed", error); return NextResponse.json( - { error: "Failed to generate voice response." }, - { status: 500 } + { + results: [ + { + toolCallId: `call_${Date.now()}`, + result: "Lo siento, tuve un problema. ¿Puedes repetir?" + } + ] + }, + { status: 200 } ); } }