Initial commit

This commit is contained in:
Haitham Khalifa
2026-02-16 12:18:06 +01:00
commit b538d84e17
63 changed files with 15059 additions and 0 deletions
+95
View File
@@ -0,0 +1,95 @@
import { NextResponse } from 'next/server';
import { createCallRecord, getAgentSettings, getUser } from '@/lib/firestore';
const VAPI_BASE_URL = 'https://api.vapi.ai';
const vapiPrivateKey = process.env.VAPI_PRIVATE_KEY;
export async function POST(request: Request) {
try {
if (!vapiPrivateKey) {
return NextResponse.json({ error: 'Vapi not configured.' }, { status: 500 });
}
const body = await request.json();
const phoneNumber = body?.phoneNumber as string | undefined;
const scheduledTaskId = body?.scheduledTaskId as string | undefined;
if (!phoneNumber) {
return NextResponse.json({ error: 'Phone number is required.' }, { status: 400 });
}
// TODO: Get userId from auth token
const userId = body?.userId ?? 'test-user-id';
const user = await getUser(userId);
if (!user || user.creditBalance < 1) {
return NextResponse.json({ error: 'Insufficient credits.' }, { status: 400 });
}
const agentSettings = await getAgentSettings(userId);
const assistantPayload = {
name: agentSettings?.agentName ?? 'HolaCompi',
instructions:
agentSettings?.agentInstructions ??
'You are HolaCompi, a helpful Spanish friend who makes phone calls on behalf of users.',
model: 'gpt-4o-mini',
voice: {
provider: 'openai',
voice: 'alloy',
},
language: agentSettings?.primaryCallLanguage ?? 'English',
};
// Create assistant dynamically based on agent settings
const assistantResponse = await fetch(`${VAPI_BASE_URL}/assistant`, {
method: 'POST',
headers: {
Authorization: `Bearer ${vapiPrivateKey}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(assistantPayload),
});
if (!assistantResponse.ok) {
const errorText = await assistantResponse.text();
return NextResponse.json({ error: errorText }, { status: 500 });
}
const assistant = await assistantResponse.json();
// Initiate outbound call via Vapi API
const callResponse = await fetch(`${VAPI_BASE_URL}/call`, {
method: 'POST',
headers: {
Authorization: `Bearer ${vapiPrivateKey}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
assistantId: assistant.id,
phoneNumber,
// TODO: Adjust payload based on Vapi call schema (customer/from numbers)
}),
});
if (!callResponse.ok) {
const errorText = await callResponse.text();
return NextResponse.json({ error: errorText }, { status: 500 });
}
const callData = await callResponse.json();
await createCallRecord({
userId,
phoneNumber,
status: 'initiated',
vapiCallId: callData.id,
assistantId: assistant.id,
scheduledTaskId,
});
return NextResponse.json({ status: callData.status ?? 'initiated', callId: callData.id });
} catch (error) {
console.error('Vapi create call error:', error);
return NextResponse.json({ error: 'Failed to create call.' }, { status: 500 });
}
}
+63
View File
@@ -0,0 +1,63 @@
import { NextResponse } from 'next/server';
import { Timestamp } from 'firebase/firestore';
import { getCallByVapiCallId, getUser, updateCallRecord, updateUserCredits } from '@/lib/firestore';
export async function POST(request: Request) {
try {
const payload = await request.json();
const eventType = payload?.event || payload?.type;
const callPayload = payload?.call || payload?.data?.call || payload;
const vapiCallId = callPayload?.id as string | undefined;
if (!vapiCallId) {
return NextResponse.json({ error: 'Missing call id.' }, { status: 400 });
}
const callRecord = await getCallByVapiCallId(vapiCallId);
if (!callRecord) {
return NextResponse.json({ error: 'Call record not found.' }, { status: 404 });
}
if (eventType === 'call-started' || eventType === 'call.started') {
await updateCallRecord(callRecord.id, {
status: 'started',
startedAt: Timestamp.now(),
});
}
if (eventType === 'call-failed' || eventType === 'call.failed') {
await updateCallRecord(callRecord.id, {
status: 'failed',
endedAt: Timestamp.now(),
});
}
if (eventType === 'call-ended' || eventType === 'call.ended') {
const durationSeconds =
Number(callPayload?.durationSeconds) ||
Number(callPayload?.duration) ||
0;
const creditsUsed = Math.ceil(durationSeconds / 60);
await updateCallRecord(callRecord.id, {
status: 'ended',
endedAt: Timestamp.now(),
durationSeconds,
creditsUsed,
recordingUrl: callPayload?.recordingUrl || callPayload?.recording?.url,
transcript: callPayload?.transcript || callPayload?.summary,
});
const user = await getUser(callRecord.userId);
if (user) {
const newBalance = Math.max(0, user.creditBalance - creditsUsed);
await updateUserCredits(callRecord.userId, newBalance);
}
}
return NextResponse.json({ received: true });
} catch (error) {
console.error('Vapi webhook error:', error);
return NextResponse.json({ error: 'Webhook processing failed.' }, { status: 500 });
}
}