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:
Udit TakkarandCarina Wollendorfer authored and GitHub committed 2025-10-09 11:40:37 +01:00
1 parent bec49ef417
commit aa48e72c12
74 files changed
+2980 -1680

No files matched your search

@@ -1,5 +1,4 @@
import prisma from "@calcom/prisma";
import { Prisma } from "@calcom/prisma/client";
import { Prisma, PrismaClient } from "@calcom/prisma/client";
import { MembershipRole } from "@calcom/prisma/enums";
interface _AgentRawResult {
@@ -9,6 +8,7 @@ interface _AgentRawResult {
enabled: boolean;
userId: number;
teamId: number | null;
inboundEventTypeId?: number | null;
createdAt: Date;
updatedAt: Date;
user_id?: number;
@@ -29,8 +29,10 @@ interface _PhoneNumberRawResult {
}
export class PrismaAgentRepository {
private static async getUserAccessibleTeamIds(userId: number): Promise<number[]> {
const memberships = await prisma.membership.findMany({
constructor(private prismaClient: PrismaClient) {}
private async getUserAccessibleTeamIds(userId: number): Promise<number[]> {
const memberships = await this.prismaClient.membership.findMany({
where: {
userId,
accepted: true,
@@ -43,8 +45,8 @@ export class PrismaAgentRepository {
return memberships.map((membership) => membership.teamId);
}
private static async getUserAdminTeamIds(userId: number): Promise<number[]> {
const memberships = await prisma.membership.findMany({
private async getUserAdminTeamIds(userId: number): Promise<number[]> {
const memberships = await this.prismaClient.membership.findMany({
where: {
userId,
accepted: true,
@@ -60,7 +62,7 @@ export class PrismaAgentRepository {
return memberships.map((membership) => membership.teamId);
}
static async findByIdWithUserAccess({
async findByIdWithUserAccess({
agentId,
userId,
teamId,
@@ -98,6 +100,7 @@ export class PrismaAgentRepository {
enabled,
"userId",
"teamId",
"inboundEventTypeId",
"createdAt",
"updatedAt"
FROM "Agent"
@@ -105,12 +108,12 @@ export class PrismaAgentRepository {
LIMIT 1
`;
const agents = await prisma.$queryRaw<_AgentRawResult[]>(query);
const agents = await this.prismaClient.$queryRaw<_AgentRawResult[]>(query);
return agents.length > 0 ? agents[0] : null;
}
static async findByProviderAgentIdWithUserAccess({
async findByProviderAgentIdWithUserAccess({
providerAgentId,
userId,
}: {
@@ -136,6 +139,7 @@ export class PrismaAgentRepository {
enabled,
"userId",
"teamId",
"inboundEventTypeId",
"createdAt",
"updatedAt"
FROM "Agent"
@@ -143,13 +147,13 @@ export class PrismaAgentRepository {
LIMIT 1
`;
const agents = await prisma.$queryRaw<_AgentRawResult[]>(query);
const agents = await this.prismaClient.$queryRaw<_AgentRawResult[]>(query);
return agents.length > 0 ? agents[0] : null;
}
static async findById({ id }: { id: string }) {
return await prisma.agent.findUnique({
async findById({ id }: { id: string }) {
return await this.prismaClient.agent.findUnique({
select: {
id: true,
name: true,
@@ -157,6 +161,7 @@ export class PrismaAgentRepository {
enabled: true,
userId: true,
teamId: true,
inboundEventTypeId: true,
createdAt: true,
updatedAt: true,
},
@@ -166,8 +171,8 @@ export class PrismaAgentRepository {
});
}
static async findByProviderAgentId({ providerAgentId }: { providerAgentId: string }) {
return await prisma.agent.findUnique({
async findByProviderAgentId({ providerAgentId }: { providerAgentId: string }) {
return await this.prismaClient.agent.findUnique({
select: {
id: true,
name: true,
@@ -175,6 +180,7 @@ export class PrismaAgentRepository {
enabled: true,
userId: true,
teamId: true,
inboundEventTypeId: true,
createdAt: true,
updatedAt: true,
},
@@ -184,7 +190,7 @@ export class PrismaAgentRepository {
});
}
static async findManyWithUserAccess({
async findManyWithUserAccess({
userId,
teamId,
scope = "all",
@@ -239,6 +245,7 @@ export class PrismaAgentRepository {
a.enabled,
a."userId",
a."teamId",
a."inboundEventTypeId",
a."createdAt",
a."updatedAt",
u.id as user_id,
@@ -255,13 +262,13 @@ export class PrismaAgentRepository {
ORDER BY a."teamId" ASC, a."createdAt" DESC
`;
const agents = await prisma.$queryRaw<_AgentRawResult[]>(query);
const agents = await this.prismaClient.$queryRaw<_AgentRawResult[]>(query);
// Get phone numbers for each agent in a separate query to avoid N+1
const agentIds = agents.map((agent) => agent.id);
const phoneNumbers =
agentIds.length > 0
? await prisma.$queryRaw<_PhoneNumberRawResult[]>`
? await this.prismaClient.$queryRaw<_PhoneNumberRawResult[]>`
SELECT
pn.id,
pn."phoneNumber",
@@ -298,6 +305,7 @@ export class PrismaAgentRepository {
enabled: agent.enabled,
userId: agent.userId,
teamId: agent.teamId,
inboundEventTypeId: agent.inboundEventTypeId,
createdAt: agent.createdAt,
updatedAt: agent.updatedAt,
user: agent.user_id
@@ -319,7 +327,7 @@ export class PrismaAgentRepository {
}));
}
static async findByIdWithUserAccessAndDetails({
async findByIdWithUserAccessAndDetails({
id,
userId,
teamId,
@@ -357,6 +365,7 @@ export class PrismaAgentRepository {
a.enabled,
a."userId",
a."teamId",
a."inboundEventTypeId",
a."createdAt",
a."updatedAt",
u.id as user_id,
@@ -372,7 +381,7 @@ export class PrismaAgentRepository {
LIMIT 1
`;
const agents = await prisma.$queryRaw<_AgentRawResult[]>(query);
const agents = await this.prismaClient.$queryRaw<_AgentRawResult[]>(query);
if (agents.length === 0) {
return null;
@@ -380,7 +389,7 @@ export class PrismaAgentRepository {
const agent = agents[0];
const phoneNumbers = await prisma.$queryRaw<_PhoneNumberRawResult[]>`
const phoneNumbers = await this.prismaClient.$queryRaw<_PhoneNumberRawResult[]>`
SELECT
pn.id,
pn."phoneNumber",
@@ -398,6 +407,7 @@ export class PrismaAgentRepository {
enabled: agent.enabled,
userId: agent.userId,
teamId: agent.teamId,
inboundEventTypeId: agent.inboundEventTypeId,
createdAt: agent.createdAt,
updatedAt: agent.updatedAt,
user: agent.user_id
@@ -423,7 +433,7 @@ export class PrismaAgentRepository {
};
}
static async create({
async create({
name,
providerAgentId,
userId,
@@ -434,7 +444,7 @@ export class PrismaAgentRepository {
userId: number;
teamId?: number;
}) {
return await prisma.agent.create({
return await this.prismaClient.agent.create({
data: {
name,
providerAgentId,
@@ -444,7 +454,7 @@ export class PrismaAgentRepository {
});
}
static async findByIdWithAdminAccess({
async findByIdWithAdminAccess({
id,
userId,
teamId,
@@ -481,18 +491,19 @@ export class PrismaAgentRepository {
"userId",
"teamId",
"createdAt",
"updatedAt"
"updatedAt",
"inboundEventTypeId"
FROM "Agent"
WHERE ${whereCondition}
LIMIT 1
`;
const agents = await prisma.$queryRaw<_AgentRawResult[]>(query);
const agents = await this.prismaClient.$queryRaw<_AgentRawResult[]>(query);
return agents.length > 0 ? agents[0] : null;
}
static async findByIdWithCallAccess({ id, userId }: { id: string; userId: number }) {
async findByIdWithCallAccess({ id, userId }: { id: string; userId: number }) {
const accessibleTeamIds = await this.getUserAccessibleTeamIds(userId);
let whereCondition: Prisma.Sql;
@@ -512,6 +523,7 @@ export class PrismaAgentRepository {
a.enabled,
a."userId",
a."teamId",
a."inboundEventTypeId",
a."createdAt",
a."updatedAt"
FROM "Agent" a
@@ -519,7 +531,7 @@ export class PrismaAgentRepository {
LIMIT 1
`;
const agents = await prisma.$queryRaw<_AgentRawResult[]>(query);
const agents = await this.prismaClient.$queryRaw<_AgentRawResult[]>(query);
if (agents.length === 0) {
return null;
@@ -527,7 +539,7 @@ export class PrismaAgentRepository {
const agent = agents[0];
const phoneNumbers = await prisma.$queryRaw<{ phoneNumber: string }[]>`
const phoneNumbers = await this.prismaClient.$queryRaw<{ phoneNumber: string }[]>`
SELECT "phoneNumber"
FROM "CalAiPhoneNumber"
WHERE "outboundAgentId" = ${agent.id}
@@ -539,27 +551,61 @@ export class PrismaAgentRepository {
};
}
static async delete({ id }: { id: string }) {
return await prisma.agent.delete({
async delete({ id }: { id: string }) {
return await this.prismaClient.agent.delete({
where: { id },
});
}
static async linkToWorkflowStep({ workflowStepId, agentId }: { workflowStepId: number; agentId: string }) {
return await prisma.workflowStep.update({
async linkOutboundAgentToWorkflow({
workflowStepId,
agentId,
}: {
workflowStepId: number;
agentId: string;
}) {
return await this.prismaClient.workflowStep.update({
where: { id: workflowStepId },
data: { agentId },
});
}
static async canManageTeamResources({
async linkInboundAgentToWorkflow({
workflowStepId,
agentId,
}: {
workflowStepId: number;
agentId: string;
}) {
return await this.prismaClient.workflowStep.update({
where: {
id: workflowStepId,
},
data: {
inboundAgentId: agentId,
},
});
}
async updateEventTypeId({ agentId, eventTypeId }: { agentId: string; eventTypeId: number }) {
return await this.prismaClient.agent.update({
where: {
id: agentId,
},
data: {
inboundEventTypeId: eventTypeId,
},
});
}
async canManageTeamResources({
userId,
teamId,
}: {
userId: number;
teamId: number;
}): Promise<boolean> {
const result = await prisma.$queryRaw<{ count: bigint }[]>`
const result = await this.prismaClient.$queryRaw<{ count: bigint }[]>`
SELECT COUNT(*) as count
FROM "Membership"
WHERE "userId" = ${userId}
@@ -1,4 +1,4 @@
import { prisma } from "@calcom/prisma";
import { PrismaClient } from "@calcom/prisma";
import { Prisma } from "@calcom/prisma/client";
import { PhoneNumberSubscriptionStatus } from "@calcom/prisma/enums";
@@ -24,8 +24,10 @@ export interface AgentRawResult {
}
export class PrismaPhoneNumberRepository {
private static async getUserAccessibleTeamIds(userId: number): Promise<number[]> {
const memberships = await prisma.membership.findMany({
constructor(private prismaClient: PrismaClient) {}
private async getUserAccessibleTeamIds(userId: number): Promise<number[]> {
const memberships = await this.prismaClient.membership.findMany({
where: {
userId,
accepted: true,
@@ -38,8 +40,8 @@ export class PrismaPhoneNumberRepository {
return memberships.map((membership) => membership.teamId);
}
static async findByPhoneNumberAndUserId({ phoneNumber, userId }: { phoneNumber: string; userId: number }) {
return await prisma.calAiPhoneNumber.findFirstOrThrow({
async findByPhoneNumberAndUserId({ phoneNumber, userId }: { phoneNumber: string; userId: number }) {
return await this.prismaClient.calAiPhoneNumber.findFirstOrThrow({
where: {
phoneNumber,
userId,
@@ -61,8 +63,8 @@ export class PrismaPhoneNumberRepository {
});
}
static async findPhoneNumbersFromUserId({ userId }: { userId: number }) {
const phoneNumbers = await prisma.$queryRaw<_PhoneNumberRawResult[]>`
async findPhoneNumbersFromUserId({ userId }: { userId: number }) {
const phoneNumbers = await this.prismaClient.$queryRaw<_PhoneNumberRawResult[]>`
SELECT
pn.id,
pn."phoneNumber",
@@ -84,7 +86,7 @@ export class PrismaPhoneNumberRepository {
const phoneNumberIds = phoneNumbers.map((pn) => pn.id);
const agents =
phoneNumberIds.length > 0
? await prisma.$queryRaw<(AgentRawResult & { phoneNumberId: number; agentType: string })[]>`
? await this.prismaClient.$queryRaw<(AgentRawResult & { phoneNumberId: number; agentType: string })[]>`
SELECT
a.id,
a.name,
@@ -137,7 +139,7 @@ export class PrismaPhoneNumberRepository {
}));
}
static async createPhoneNumber({
async createPhoneNumber({
phoneNumber,
provider,
userId,
@@ -156,7 +158,7 @@ export class PrismaPhoneNumberRepository {
subscriptionStatus?: PhoneNumberSubscriptionStatus;
providerPhoneNumberId?: string;
}) {
return await prisma.calAiPhoneNumber.create({
return await this.prismaClient.calAiPhoneNumber.create({
select: {
id: true,
phoneNumber: true,
@@ -185,16 +187,16 @@ export class PrismaPhoneNumberRepository {
});
}
static async deletePhoneNumber({ phoneNumber }: { phoneNumber: string }) {
return await prisma.calAiPhoneNumber.delete({
async deletePhoneNumber({ phoneNumber }: { phoneNumber: string }) {
return await this.prismaClient.calAiPhoneNumber.delete({
where: {
phoneNumber,
},
});
}
static async findByStripeSubscriptionId({ stripeSubscriptionId }: { stripeSubscriptionId: string }) {
return await prisma.calAiPhoneNumber.findFirst({
async findByStripeSubscriptionId({ stripeSubscriptionId }: { stripeSubscriptionId: string }) {
return await this.prismaClient.calAiPhoneNumber.findFirst({
where: {
stripeSubscriptionId,
},
@@ -211,8 +213,8 @@ export class PrismaPhoneNumberRepository {
});
}
static async findByIdAndUserId({ id, userId }: { id: number; userId: number }) {
return await prisma.calAiPhoneNumber.findFirst({
async findByIdAndUserId({ id, userId }: { id: number; userId: number }) {
return await this.prismaClient.calAiPhoneNumber.findFirst({
where: {
id,
userId,
@@ -234,7 +236,7 @@ export class PrismaPhoneNumberRepository {
});
}
static async findByIdWithTeamAccess({
async findByIdWithTeamAccess({
id,
teamId,
userId,
@@ -249,7 +251,7 @@ export class PrismaPhoneNumberRepository {
return null;
}
return await prisma.calAiPhoneNumber.findFirst({
return await this.prismaClient.calAiPhoneNumber.findFirst({
where: {
id,
teamId,
@@ -271,7 +273,7 @@ export class PrismaPhoneNumberRepository {
});
}
static async findByPhoneNumberAndTeamId({
async findByPhoneNumberAndTeamId({
phoneNumber,
teamId,
userId,
@@ -286,7 +288,7 @@ export class PrismaPhoneNumberRepository {
return null;
}
return await prisma.calAiPhoneNumber.findFirst({
return await this.prismaClient.calAiPhoneNumber.findFirst({
where: {
phoneNumber,
teamId,
@@ -308,7 +310,7 @@ export class PrismaPhoneNumberRepository {
});
}
static async findManyWithUserAccess({
async findManyWithUserAccess({
userId,
teamId,
scope = "all",
@@ -373,12 +375,12 @@ export class PrismaPhoneNumberRepository {
ORDER BY pn."createdAt" DESC
`;
const phoneNumbers = await prisma.$queryRaw<_PhoneNumberRawResult[]>(query);
const phoneNumbers = await this.prismaClient.$queryRaw<_PhoneNumberRawResult[]>(query);
const phoneNumberIds = phoneNumbers.map((pn) => pn.id);
const agents =
phoneNumberIds.length > 0
? await prisma.$queryRaw<(AgentRawResult & { phoneNumberId: number; agentType: string })[]>`
? await this.prismaClient.$queryRaw<(AgentRawResult & { phoneNumberId: number; agentType: string })[]>`
SELECT
a.id,
a.name,
@@ -431,26 +433,29 @@ export class PrismaPhoneNumberRepository {
}));
}
static async updateSubscriptionStatus({
async updateSubscriptionStatus({
id,
subscriptionStatus,
disconnectOutboundAgent = false,
disconnectAgents = false,
}: {
id: number;
subscriptionStatus: PhoneNumberSubscriptionStatus;
disconnectOutboundAgent?: boolean;
disconnectAgents?: boolean;
}) {
const updateData: Prisma.CalAiPhoneNumberUpdateInput = {
subscriptionStatus,
};
if (disconnectOutboundAgent) {
if (disconnectAgents) {
updateData.outboundAgent = {
disconnect: true,
};
updateData.inboundAgent = {
disconnect: true,
};
}
return await prisma.calAiPhoneNumber.update({
return await this.prismaClient.calAiPhoneNumber.update({
where: {
id,
},
@@ -458,7 +463,7 @@ export class PrismaPhoneNumberRepository {
});
}
static async updateAgents({
async updateAgents({
id,
inboundProviderAgentId,
outboundProviderAgentId,
@@ -471,7 +476,7 @@ export class PrismaPhoneNumberRepository {
if (inboundProviderAgentId !== undefined) {
if (inboundProviderAgentId) {
const agent = await prisma.agent.findFirst({
const agent = await this.prismaClient.agent.findFirst({
where: {
providerAgentId: inboundProviderAgentId,
},
@@ -491,7 +496,7 @@ export class PrismaPhoneNumberRepository {
if (outboundProviderAgentId !== undefined) {
if (outboundProviderAgentId) {
const agent = await prisma.agent.findFirst({
const agent = await this.prismaClient.agent.findFirst({
where: {
providerAgentId: outboundProviderAgentId,
},
@@ -509,7 +514,7 @@ export class PrismaPhoneNumberRepository {
}
}
return await prisma.calAiPhoneNumber.update({
return await this.prismaClient.calAiPhoneNumber.update({
where: {
id,
},
@@ -517,8 +522,32 @@ export class PrismaPhoneNumberRepository {
});
}
static async findByPhoneNumber({ phoneNumber }: { phoneNumber: string }) {
return await prisma.calAiPhoneNumber.findFirst({
async updateInboundAgentId({
id,
agentId,
}: {
id: number;
agentId: string;
}) {
// Atomic update: only set if inboundAgentId is currently null
return await this.prismaClient.calAiPhoneNumber.updateMany({
where: {
id,
inboundAgentId: null,
},
data: { inboundAgentId: agentId },
});
}
async findInboundAgentIdByPhoneNumberId({ phoneNumberId }: { phoneNumberId: number }) {
return await this.prismaClient.calAiPhoneNumber.findUnique({
where: { id: phoneNumberId },
select: { inboundAgentId: true },
});
}
async findByPhoneNumber({ phoneNumber }: { phoneNumber: string }) {
return await this.prismaClient.calAiPhoneNumber.findFirst({
where: {
phoneNumber,
},