feat: inbound calls in cal ai (#23890)
* feat: lang support * fix: type errors * feat: select voice agent * refactor: address feedback * refactor: address feedback * refactor: missing import * fix: types * feat: add inbound calls * chore: formatting * chore * feat: finish inbound call * chore: formatting * fix: update bug * fix: types * refactor: Agent Configuration Sheet (#23930) * refactor: agent configuration sheet * chore: use default phone numbre * refactor: improvements * refactor: improvements * fix: types * fix: feedback * chore: * fix: feedback * fix: prompt * fix: review * fix: review * refactor: class * refactor: class * refactor: rename * Update apps/web/public/static/locales/en/common.json * Update apps/web/public/static/locales/en/common.json * chore: update set value * fix: remove index * fix: type error * fix: update tetss * fix: use logger * refactor: don't use static * fix: type * fix: schema * refactor: --------- Co-authored-by: Carina Wollendorfer <[email protected]>
This commit is contained in:
1 parent
bec49ef417
commit
aa48e72c12
74 files changed
+2980
-1680
No files matched your search
@@ -2,8 +2,6 @@ import type { NextRequest } from "next/server";
|
||||
import { Retell } from "retell-sdk";
|
||||
import { describe, it, expect, vi, beforeEach } from "vitest";
|
||||
|
||||
import { PrismaAgentRepository } from "@calcom/lib/server/repository/PrismaAgentRepository";
|
||||
import { PrismaPhoneNumberRepository } from "@calcom/lib/server/repository/PrismaPhoneNumberRepository";
|
||||
import type { CalAiPhoneNumber, User, Team, Agent } from "@calcom/prisma/client";
|
||||
|
||||
import { POST } from "../route";
|
||||
@@ -81,16 +79,19 @@ vi.mock("@calcom/features/ee/billing/credit-service", () => ({
|
||||
})),
|
||||
}));
|
||||
|
||||
const mockFindByPhoneNumber = vi.fn();
|
||||
const mockFindByProviderAgentId = vi.fn();
|
||||
|
||||
vi.mock("@calcom/lib/server/repository/PrismaPhoneNumberRepository", () => ({
|
||||
PrismaPhoneNumberRepository: {
|
||||
findByPhoneNumber: vi.fn(),
|
||||
},
|
||||
PrismaPhoneNumberRepository: vi.fn().mockImplementation(() => ({
|
||||
findByPhoneNumber: mockFindByPhoneNumber,
|
||||
})),
|
||||
}));
|
||||
|
||||
vi.mock("@calcom/lib/server/repository/PrismaAgentRepository", () => ({
|
||||
PrismaAgentRepository: {
|
||||
findByProviderAgentId: vi.fn(),
|
||||
},
|
||||
PrismaAgentRepository: vi.fn().mockImplementation(() => ({
|
||||
findByProviderAgentId: mockFindByProviderAgentId,
|
||||
})),
|
||||
}));
|
||||
|
||||
vi.mock("next/server", () => ({
|
||||
@@ -196,7 +197,7 @@ describe("Retell AI Webhook Handler", () => {
|
||||
team: null,
|
||||
};
|
||||
|
||||
vi.mocked(PrismaPhoneNumberRepository.findByPhoneNumber).mockResolvedValue(mockPhoneNumber);
|
||||
mockFindByPhoneNumber.mockResolvedValue(mockPhoneNumber);
|
||||
|
||||
mockHasAvailableCredits.mockResolvedValue(true);
|
||||
mockChargeCredits.mockResolvedValue(undefined);
|
||||
@@ -255,7 +256,7 @@ describe("Retell AI Webhook Handler", () => {
|
||||
user: null,
|
||||
};
|
||||
|
||||
vi.mocked(PrismaPhoneNumberRepository.findByPhoneNumber).mockResolvedValue(mockTeamPhoneNumber);
|
||||
mockFindByPhoneNumber.mockResolvedValue(mockTeamPhoneNumber);
|
||||
|
||||
mockHasAvailableCredits.mockResolvedValue(true);
|
||||
mockChargeCredits.mockResolvedValue(undefined);
|
||||
@@ -314,12 +315,12 @@ describe("Retell AI Webhook Handler", () => {
|
||||
const response = await callPOST(request);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(PrismaPhoneNumberRepository.findByPhoneNumber).not.toHaveBeenCalled();
|
||||
expect(mockFindByPhoneNumber).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("should handle phone number not found", async () => {
|
||||
vi.mocked(Retell.verify).mockReturnValue(true);
|
||||
vi.mocked(PrismaPhoneNumberRepository.findByPhoneNumber).mockResolvedValue(null);
|
||||
mockFindByPhoneNumber.mockResolvedValue(null);
|
||||
|
||||
const body: RetellWebhookBody = {
|
||||
event: "call_analyzed",
|
||||
@@ -365,7 +366,7 @@ describe("Retell AI Webhook Handler", () => {
|
||||
team: null,
|
||||
};
|
||||
|
||||
vi.mocked(PrismaPhoneNumberRepository.findByPhoneNumber).mockResolvedValue(mockPhoneNumber);
|
||||
mockFindByPhoneNumber.mockResolvedValue(mockPhoneNumber);
|
||||
|
||||
mockHasAvailableCredits.mockResolvedValue(false);
|
||||
|
||||
@@ -440,7 +441,7 @@ describe("Retell AI Webhook Handler", () => {
|
||||
team: null,
|
||||
};
|
||||
|
||||
vi.mocked(PrismaPhoneNumberRepository.findByPhoneNumber).mockResolvedValue(mockPhoneNumber);
|
||||
mockFindByPhoneNumber.mockResolvedValue(mockPhoneNumber);
|
||||
mockHasAvailableCredits.mockResolvedValue(true);
|
||||
mockChargeCredits.mockResolvedValue(undefined);
|
||||
|
||||
@@ -494,7 +495,7 @@ describe("Retell AI Webhook Handler", () => {
|
||||
user: { id: 42, email: "[email protected]", name: "U" },
|
||||
team: null,
|
||||
};
|
||||
vi.mocked(PrismaPhoneNumberRepository.findByPhoneNumber).mockResolvedValue(mockPhoneNumber);
|
||||
mockFindByPhoneNumber.mockResolvedValue(mockPhoneNumber);
|
||||
mockHasAvailableCredits.mockResolvedValue(true);
|
||||
mockChargeCredits.mockResolvedValue(undefined);
|
||||
|
||||
@@ -544,7 +545,7 @@ describe("Retell AI Webhook Handler", () => {
|
||||
user: { id: 1, email: "[email protected]", name: "Test User" },
|
||||
team: null,
|
||||
};
|
||||
vi.mocked(PrismaPhoneNumberRepository.findByPhoneNumber).mockResolvedValue(mockPhoneNumber);
|
||||
mockFindByPhoneNumber.mockResolvedValue(mockPhoneNumber);
|
||||
mockChargeCredits.mockResolvedValue({ userId: 1 });
|
||||
|
||||
const body: RetellWebhookBody = {
|
||||
@@ -596,7 +597,7 @@ describe("Retell AI Webhook Handler", () => {
|
||||
user: { id: 1, email: "[email protected]", name: "Test User" },
|
||||
team: null,
|
||||
};
|
||||
vi.mocked(PrismaPhoneNumberRepository.findByPhoneNumber).mockResolvedValue(mockPhoneNumber);
|
||||
mockFindByPhoneNumber.mockResolvedValue(mockPhoneNumber);
|
||||
|
||||
const body: RetellWebhookBody = {
|
||||
event: "call_analyzed",
|
||||
@@ -661,7 +662,7 @@ describe("Retell AI Webhook Handler", () => {
|
||||
user: { id: 1, email: "[email protected]", name: "Test User" },
|
||||
team: null,
|
||||
};
|
||||
vi.mocked(PrismaPhoneNumberRepository.findByPhoneNumber).mockResolvedValue(mockPhoneNumber);
|
||||
mockFindByPhoneNumber.mockResolvedValue(mockPhoneNumber);
|
||||
|
||||
// Mock chargeCredits to throw an error
|
||||
mockChargeCredits.mockRejectedValue(new Error("Credit service error"));
|
||||
@@ -695,7 +696,7 @@ describe("Retell AI Webhook Handler", () => {
|
||||
describe("Web Call Tests", () => {
|
||||
const mockAgent: Pick<
|
||||
Agent,
|
||||
"id" | "name" | "providerAgentId" | "enabled" | "userId" | "teamId" | "createdAt" | "updatedAt"
|
||||
"id" | "name" | "providerAgentId" | "enabled" | "userId" | "teamId" | "createdAt" | "updatedAt" | "inboundEventTypeId"
|
||||
> = {
|
||||
id: "agent-123",
|
||||
name: "Test Agent",
|
||||
@@ -705,6 +706,7 @@ describe("Retell AI Webhook Handler", () => {
|
||||
teamId: null,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
inboundEventTypeId: null,
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
@@ -713,7 +715,7 @@ describe("Retell AI Webhook Handler", () => {
|
||||
});
|
||||
|
||||
it("should process web call with valid agent and charge credits", async () => {
|
||||
vi.mocked(PrismaAgentRepository.findByProviderAgentId).mockResolvedValue(mockAgent);
|
||||
mockFindByProviderAgentId.mockResolvedValue(mockAgent);
|
||||
mockChargeCredits.mockResolvedValue(undefined);
|
||||
|
||||
const body: RetellWebhookBody = {
|
||||
@@ -739,7 +741,7 @@ describe("Retell AI Webhook Handler", () => {
|
||||
const data = await response.json();
|
||||
expect(data.success).toBe(true);
|
||||
|
||||
expect(PrismaAgentRepository.findByProviderAgentId).toHaveBeenCalledWith({
|
||||
expect(mockFindByProviderAgentId).toHaveBeenCalledWith({
|
||||
providerAgentId: "agent_5e3e0d29d692172c2c24d8f9a7",
|
||||
});
|
||||
|
||||
@@ -755,8 +757,8 @@ describe("Retell AI Webhook Handler", () => {
|
||||
});
|
||||
|
||||
it("should handle web call with team agent", async () => {
|
||||
const teamAgent = { ...mockAgent, userId: 2, teamId: 10 };
|
||||
vi.mocked(PrismaAgentRepository.findByProviderAgentId).mockResolvedValue(teamAgent);
|
||||
const teamAgent = { ...mockAgent, userId: 2, teamId: 10, inboundEventTypeId: null };
|
||||
mockFindByProviderAgentId.mockResolvedValue(teamAgent);
|
||||
mockChargeCredits.mockResolvedValue(undefined);
|
||||
|
||||
const body: RetellWebhookBody = {
|
||||
@@ -789,7 +791,7 @@ describe("Retell AI Webhook Handler", () => {
|
||||
});
|
||||
|
||||
it("should handle web call without from_number", async () => {
|
||||
vi.mocked(PrismaAgentRepository.findByProviderAgentId).mockResolvedValue(mockAgent);
|
||||
mockFindByProviderAgentId.mockResolvedValue(mockAgent);
|
||||
mockChargeCredits.mockResolvedValue(undefined);
|
||||
|
||||
const body: RetellWebhookBody = {
|
||||
@@ -810,8 +812,8 @@ describe("Retell AI Webhook Handler", () => {
|
||||
const response = await callPOST(request);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(PrismaAgentRepository.findByProviderAgentId).toHaveBeenCalled();
|
||||
expect(PrismaPhoneNumberRepository.findByPhoneNumber).not.toHaveBeenCalled();
|
||||
expect(mockFindByProviderAgentId).toHaveBeenCalled();
|
||||
expect(mockFindByPhoneNumber).not.toHaveBeenCalled();
|
||||
expect(mockChargeCredits).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
@@ -833,12 +835,12 @@ describe("Retell AI Webhook Handler", () => {
|
||||
const response = await callPOST(request);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(PrismaAgentRepository.findByProviderAgentId).not.toHaveBeenCalled();
|
||||
expect(mockFindByProviderAgentId).not.toHaveBeenCalled();
|
||||
expect(mockChargeCredits).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("should handle web call with agent not found", async () => {
|
||||
vi.mocked(PrismaAgentRepository.findByProviderAgentId).mockResolvedValue(null);
|
||||
mockFindByProviderAgentId.mockResolvedValue(null);
|
||||
|
||||
const body: RetellWebhookBody = {
|
||||
event: "call_analyzed",
|
||||
@@ -860,7 +862,7 @@ describe("Retell AI Webhook Handler", () => {
|
||||
const response = await callPOST(request);
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
expect(PrismaAgentRepository.findByProviderAgentId).toHaveBeenCalledWith({
|
||||
expect(mockFindByProviderAgentId).toHaveBeenCalledWith({
|
||||
providerAgentId: "non-existent-agent",
|
||||
});
|
||||
expect(mockChargeCredits).not.toHaveBeenCalled();
|
||||
|
||||
@@ -9,6 +9,7 @@ import logger from "@calcom/lib/logger";
|
||||
import { safeStringify } from "@calcom/lib/safeStringify";
|
||||
import { PrismaAgentRepository } from "@calcom/lib/server/repository/PrismaAgentRepository";
|
||||
import { PrismaPhoneNumberRepository } from "@calcom/lib/server/repository/PrismaPhoneNumberRepository";
|
||||
import prisma from "@calcom/prisma";
|
||||
import { CreditUsageType } from "@calcom/prisma/enums";
|
||||
|
||||
const log = logger.getSubLogger({ prefix: ["retell-ai-webhook"] });
|
||||
@@ -133,7 +134,7 @@ async function handleCallAnalyzed(callData: any) {
|
||||
);
|
||||
return {
|
||||
success: true,
|
||||
message: `Invalid or missing call_cost.total_duration_seconds for call ${call_id}`
|
||||
message: `Invalid or missing call_cost.total_duration_seconds for call ${call_id}`,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -146,11 +147,12 @@ async function handleCallAnalyzed(callData: any) {
|
||||
log.error(`Web call ${call_id} missing agent_id, cannot charge credits`);
|
||||
return {
|
||||
success: false,
|
||||
message: `Web call ${call_id} missing agent_id, cannot charge credits`
|
||||
message: `Web call ${call_id} missing agent_id, cannot charge credits`,
|
||||
};
|
||||
}
|
||||
|
||||
const agent = await PrismaAgentRepository.findByProviderAgentId({
|
||||
const agentRepo = new PrismaAgentRepository(prisma);
|
||||
const agent = await agentRepo.findByProviderAgentId({
|
||||
providerAgentId: agent_id,
|
||||
});
|
||||
|
||||
@@ -158,17 +160,17 @@ async function handleCallAnalyzed(callData: any) {
|
||||
log.error(`No agent found for providerAgentId ${agent_id}, call ${call_id}`);
|
||||
return {
|
||||
success: false,
|
||||
message: `No agent found for providerAgentId ${agent_id}, call ${call_id}`
|
||||
message: `No agent found for providerAgentId ${agent_id}, call ${call_id}`,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
userId = agent.userId ?? undefined;
|
||||
teamId = agent.teamId ?? undefined;
|
||||
|
||||
log.info(`Processing web call ${call_id} for agent ${agent_id}, user ${userId}, team ${teamId}`);
|
||||
} else {
|
||||
const phoneNumber = await PrismaPhoneNumberRepository.findByPhoneNumber({
|
||||
const phoneNumberRepo = new PrismaPhoneNumberRepository(prisma);
|
||||
const phoneNumber = await phoneNumberRepo.findByPhoneNumber({
|
||||
phoneNumber: from_number,
|
||||
});
|
||||
|
||||
|
||||
@@ -525,6 +525,11 @@
|
||||
<path d="M14.05 2a9 9 0 0 1 8 7.94" ></path>
|
||||
<path d="M14.05 6A5 5 0 0 1 18 10" ></path>
|
||||
</symbol>
|
||||
<symbol class="lucide lucide-phone-incoming" viewBox="0 0 24 24" fill="inherit" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" id="phone-incoming">
|
||||
<polyline points="16 2 16 8 22 8" ></polyline>
|
||||
<line x1="22" x2="16" y1="2" y2="8" ></line>
|
||||
<path d="M22 16.92v3a2 2 0 0 1-2.18 2 19.79 19.79 0 0 1-8.63-3.07 19.5 19.5 0 0 1-6-6 19.79 19.79 0 0 1-3.07-8.67A2 2 0 0 1 4.11 2h3a2 2 0 0 1 2 1.72 12.84 12.84 0 0 0 .7 2.81 2 2 0 0 1-.45 2.11L8.09 9.91a16 16 0 0 0 6 6l1.27-1.27a2 2 0 0 1 2.11-.45 12.84 12.84 0 0 0 2.81.7A2 2 0 0 1 22 16.92z" ></path>
|
||||
</symbol>
|
||||
<symbol class="lucide lucide-phone-off" viewBox="0 0 24 24" fill="inherit" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" id="phone-off">
|
||||
<path d="M10.68 13.31a16 16 0 0 0 3.41 2.6l1.27-1.27a2 2 0 0 1 2.11-.45 12.84 12.84 0 0 0 2.81.7 2 2 0 0 1 1.72 2v3a2 2 0 0 1-2.18 2 19.79 19.79 0 0 1-8.63-3.07 19.42 19.42 0 0 1-3.33-2.67m-2.67-3.34a19.79 19.79 0 0 1-3.07-8.63A2 2 0 0 1 4.11 2h3a2 2 0 0 1 2 1.72 12.84 12.84 0 0 0 .7 2.81 2 2 0 0 1-.45 2.11L8.09 9.91" ></path>
|
||||
<line x1="22" x2="2" y1="2" y2="22" ></line>
|
||||
|
||||
|
Before Width: | Height: | Size: 54 KiB After Width: | Height: | Size: 55 KiB |
@@ -310,6 +310,8 @@
|
||||
"register_now": "Register now",
|
||||
"register": "Register",
|
||||
"page_doesnt_exist": "This page does not exist.",
|
||||
"no_active_phone_number_available": "No active phone number available",
|
||||
"workflow_step_not_configured": "Workflow step not configured",
|
||||
"check_spelling_mistakes_or_go_back": "Check for spelling mistakes or go back to the previous page.",
|
||||
"404_page_not_found": "404: This page could not be found.",
|
||||
"booker_event_not_found": "We could not find the event you are trying to book.",
|
||||
@@ -837,6 +839,7 @@
|
||||
"please_enter_phone_number": "Please enter a phone number",
|
||||
"agent_updated_successfully": "Agent updated successfully",
|
||||
"agent_created_successfully": "Agent created successfully",
|
||||
"agent_event_type_updated_successfully": "Event type added successfully",
|
||||
"phone_number_unsubscribed_successfully": "Phone number unsubscribed successfully",
|
||||
"general_prompt_description": "This prompt defines the agent's role and primary objectives",
|
||||
"prompt": "Prompt",
|
||||
@@ -3756,5 +3759,19 @@
|
||||
"voice_id": "Voice ID",
|
||||
"use_voice": "Use Voice",
|
||||
"current_voice": "Current Voice",
|
||||
"setup_incoming_agent": "Set up incoming agent",
|
||||
"setup_incoming_agent_description": "Configure an AI agent to handle incoming calls on your phone number",
|
||||
"connect_a_phone_number_first": "Connect a Phone Number first",
|
||||
"setup_agent_for_incoming_calls": "Set up Agent for incoming calls",
|
||||
"configure_agent_to_handle_incoming_calls": "Configure agent to handle incoming calls",
|
||||
"incoming_calls": "Incoming Calls",
|
||||
"outgoing_calls": "Outgoing Calls",
|
||||
"inbound_agent_setup_success": "Inbound agent setup successful",
|
||||
"inbound_agent_configured": "Inbound agent configured",
|
||||
"setup_inbound_agent": "Set up Inbound Agent",
|
||||
"please_select_event_type_first": "Please select an event type first",
|
||||
"edit_configuration": "Edit Configuration",
|
||||
"select_event_type_for_inbound_calls": "Inbound calls can book only one event type. Select the event type where meetings will be scheduled when callers reach your agent.",
|
||||
"setup_inbound_agent_for_incoming_calls": "Set up inbound agent for incoming calls",
|
||||
"ADD_NEW_STRINGS_ABOVE_THIS_LINE_TO_PREVENT_MERGE_CONFLICTS": "↑↑↑↑↑↑↑↑↑↑↑↑↑ Add your new strings above here ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑"
|
||||
}
|
||||
Reference in new issue
Block a user