Files
aa48e72c12 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]>
2025-10-09 11:40:37 +01:00

70 lines
2.1 KiB
TypeScript

import { createDefaultAIPhoneServiceProvider } from "@calcom/features/calAIPhone";
import { FeaturesRepository } from "@calcom/features/flags/features.repository";
import logger from "@calcom/lib/logger";
import { PrismaAgentRepository } from "@calcom/lib/server/repository/PrismaAgentRepository";
import prisma from "@calcom/prisma";
import { TRPCError } from "@trpc/server";
import type { TrpcSessionUser } from "../../../types";
import type { TTestCallInputSchema } from "./testCall.schema";
type TestCallHandlerOptions = {
ctx: {
user: NonNullable<TrpcSessionUser>;
};
input: TTestCallInputSchema;
};
export const testCallHandler = async ({ ctx, input }: TestCallHandlerOptions) => {
const timeZone = ctx.user.timeZone ?? "Europe/London";
const eventTypeId = input.eventTypeId;
const featuresRepository = new FeaturesRepository(prisma);
const calAIVoiceAgents = await featuresRepository.checkIfFeatureIsEnabledGlobally("cal-ai-voice-agents");
if (!calAIVoiceAgents) {
logger.warn("Cal AI voice agents are disabled - skipping AI phone call scheduling");
return;
}
const agentRepo = new PrismaAgentRepository(prisma);
const agent = await agentRepo.findByIdWithCallAccess({
id: input.agentId,
userId: ctx.user.id,
});
if (!agent?.providerAgentId) {
logger.error(`Agent not found with agentId ${input.agentId}`);
throw new TRPCError({
code: "BAD_REQUEST",
message: "Agent not found",
});
}
const aiService = createDefaultAIPhoneServiceProvider();
try {
await aiService.updateToolsFromAgentId(agent.providerAgentId, {
eventTypeId,
timeZone,
userId: ctx.user.id,
teamId: input.teamId,
});
} catch (error) {
logger.error(`Failed to update tools for agent ${agent.providerAgentId}: ${error}`);
throw new TRPCError({
code: "UNAUTHORIZED",
message: "You are not authorized to test the event type. Failed to update tools for agent",
});
}
return await aiService.createTestCall({
agentId: input.agentId,
phoneNumber: input.phoneNumber,
userId: ctx.user.id,
teamId: input.teamId,
timeZone,
eventTypeId,
});
};