Files
cal.diy/packages/features/calAIPhone/providers/retellAI/services/AgentService.ts
T
d4bff9d6b1 feat: Cal.ai Self Serve #2 (#22995)
* feat: Cal.ai Self Serve #2

* chore: fix import and remove logs

* fix: update checkout session

* fix: type errors and test

* fix: imports

* fix: type err

* fix: type error

* fix: tests

* chore: save progress

* fix: workflow flow

* fix: workflow update bug

* tests: add unit tests for retell ai webhoo

* fix: status code

* fix: test and delete bug

* fix: add dynamic variables

* fix: type err

* chore: update unit test

* fix: type error

* chore: update default prompt

* fix: type errors

* fix: workflow permissions

* fix: workflow page

* fix: translations

* feat: add call duration

* chore: add booking uid

* fix: button positioning

* chore: update tests

* chore: improvements

* chore: some more improvements

* refactor: improvements

* refactor: code feedback

* refactor: improvements

* feat: enable credits for orgs (#23077)

* Show credits UI for orgs

* fix stripe callback url when buying credits

* give orgs 20% credits

* add test for calulating credits

---------

Co-authored-by: CarinaWolli <[email protected]>
Co-authored-by: Udit Takkar <[email protected]>

* fix: types

* fix: types

* chore: error

* fix: type error

* fix: type error

* chore: mock env

* feat: add idempotency key to prevent double charging

* chore: add userId and teamId

* fix: skip inbound calls

* chore: update tests

* feat: add feature flag for voice agent

* feat: finish test call and other improvements

* chore: add alert

* chore: update .env.example

* chore: improvements

* fix: update tests

* refactor: remove un necessary

* feat: add setup badge

* chore: improvements

* fix: use referene id

* chore: improvements

* fix: type error

* fix: type

* refactor: change pricing logic

* refactor: update tests

* fix: conflicts

* fix: billing link for orgs

* fix: types

* refactor: move feature flag up

* fix: alert and test call credit check

* fix: update unit tests

* fix: feedback

* refactor: improvements

* refactor: move handlers to separate files

* fix: types

* fix: missing import

* fix: type

* refactor: change general tools functions handling

* refactor: use repository

* refactor: improvements

* fix: types

* fix: type errorr

* fix: auth check

* feat: add creditFor

* fix: update defualt prompt

* fix: throw error on frontend

* fix: update unit tests

* fix: use deleteAllWorkflowReminders

* refactor: add connect phone number

* refactor: improvements

* chore: translation

* chore: update message

* chore: translation

* design improvements buy number dialog

* add translation for error message

* use translation key in error message

* refactor: improve connect phone number tab

* feat: support un saved workflow to tests

* chore: remove un used

* fix: remove un used

* fix: remove un used

* refactor: similify billing

---------

Co-authored-by: Carina Wollendorfer <[email protected]>
Co-authored-by: CarinaWolli <[email protected]>
Co-authored-by: Peer Richelsen <[email protected]>
Co-authored-by: Keith Williams <[email protected]>
2025-08-29 05:04:05 +01:00

623 lines
17 KiB
TypeScript

import { v4 as uuidv4 } from "uuid";
import { generateUniqueAPIKey as generateHashedApiKey } from "@calcom/ee/api-keys/lib/apiKeys";
import { timeZoneSchema } from "@calcom/lib/dayjs/timeZone.schema";
import { HttpError } from "@calcom/lib/http-error";
import logger from "@calcom/lib/logger";
import prisma from "@calcom/prisma";
import type {
AIPhoneServiceUpdateModelParams,
AIPhoneServiceProviderType,
AIPhoneServiceAgent,
AIPhoneServiceModel,
AIPhoneServiceTools,
} from "../../../interfaces/AIPhoneService.interface";
import type { AgentRepositoryInterface } from "../../interfaces/AgentRepositoryInterface";
import { RetellAIServiceMapper } from "../RetellAIServiceMapper";
import type { RetellAIRepository, Language } from "../types";
import { getLlmId } from "../types";
export class AgentService {
private logger = logger.getSubLogger({ prefix: ["AgentService"] });
constructor(
private retellRepository: RetellAIRepository,
private agentRepository: AgentRepositoryInterface
) {}
private async createApiKey({ userId, teamId }: { userId: number; teamId?: number }) {
const [hashedApiKey, apiKey] = generateHashedApiKey();
await prisma.apiKey.create({
data: {
id: uuidv4(),
userId,
teamId,
// And here we pass a null to expiresAt if never expires is true
expiresAt: null,
hashedKey: hashedApiKey,
note: `Cal AI Phone API Key for agent ${userId} ${teamId ? `for team ${teamId}` : ""}`,
},
});
const apiKeyPrefix = process.env.API_KEY_PREFIX ?? "cal_";
const prefixedApiKey = `${apiKeyPrefix}${apiKey}`;
return prefixedApiKey;
}
async getAgent(agentId: string): Promise<AIPhoneServiceAgent<AIPhoneServiceProviderType.RETELL_AI>> {
if (!agentId?.trim()) {
throw new HttpError({
statusCode: 400,
message: "Agent ID is required and cannot be empty",
});
}
try {
return await this.retellRepository.getAgent(agentId);
} catch (error) {
this.logger.error("Failed to get agent from external AI service", {
agentId,
error,
});
throw new HttpError({
statusCode: 500,
message: `Failed to get agent ${agentId}`,
});
}
}
async updateToolsFromAgentId(
agentId: string,
data: { eventTypeId: number | null; timeZone: string; userId: number | null; teamId?: number | null }
) {
if (!agentId?.trim()) {
throw new HttpError({
statusCode: 400,
message: "Agent ID is required and cannot be empty",
});
}
if (!data.eventTypeId || !data.userId) {
throw new HttpError({
statusCode: 400,
message: "Event type ID and user ID are required",
});
}
if (!timeZoneSchema.safeParse(data.timeZone).success) {
throw new HttpError({
statusCode: 400,
message: "Invalid time zone",
});
}
try {
const agent = await this.getAgent(agentId);
const llmId = getLlmId(agent);
if (!llmId) {
throw new HttpError({
statusCode: 404,
message: "Agent does not have an LLM configured.",
});
}
const llmDetails = await this.retellRepository.getLLM(llmId);
if (!llmDetails) {
throw new HttpError({ statusCode: 404, message: "LLM details not found." });
}
const existing = llmDetails?.general_tools ?? [];
const hasCheck = existing.some((t) => t.name === `check_availability_${data.eventTypeId}`);
const hasBook = existing.some((t) => t.name === `book_appointment_${data.eventTypeId}`);
// If both already exist and end_call also exists, nothing to do
const hasEndCallAlready = existing.some((t) => t.type === "end_call");
if (hasCheck && hasBook && hasEndCallAlready) {
return;
}
const reusableKey = existing.find(
(t): t is Extract<typeof t, { cal_api_key: string }> =>
"cal_api_key" in t && typeof t.cal_api_key === "string"
)?.cal_api_key;
const apiKey =
reusableKey ??
(await this.createApiKey({
userId: data.userId,
teamId: data.teamId || undefined,
}));
const newEventTools: NonNullable<AIPhoneServiceTools<AIPhoneServiceProviderType.RETELL_AI>> = [];
if (!hasCheck) {
newEventTools.push({
name: `check_availability_${data.eventTypeId}`,
type: "check_availability_cal",
event_type_id: data.eventTypeId,
cal_api_key: apiKey,
timezone: data.timeZone,
});
}
if (!hasBook) {
newEventTools.push({
name: `book_appointment_${data.eventTypeId}`,
type: "book_appointment_cal",
event_type_id: data.eventTypeId,
cal_api_key: apiKey,
timezone: data.timeZone,
});
}
if (!hasEndCallAlready) {
newEventTools.unshift({
type: "end_call",
name: "end_call",
description: "Hang up the call, triggered only after appointment successfully scheduled.",
});
}
const updatedGeneralTools = [...existing, ...newEventTools];
await this.retellRepository.updateLLM(llmId, { general_tools: updatedGeneralTools });
} catch (error) {
if (error instanceof HttpError) {
throw error;
}
this.logger.error("Failed to update agent general tools in external AI service", {
agentId,
error,
});
throw new HttpError({
statusCode: 500,
message: `Failed to update agent general tools ${agentId}: ${
error instanceof Error ? error.message : "Unknown error"
}`,
});
}
}
async removeToolsForEventTypes(agentId: string, eventTypeIds: number[]) {
if (!agentId?.trim()) {
throw new HttpError({
statusCode: 400,
message: "Agent ID is required and cannot be empty",
});
}
if (!eventTypeIds.length) {
return;
}
try {
const agent = await this.getAgent(agentId);
const llmId = getLlmId(agent);
if (!llmId) {
throw new HttpError({
statusCode: 404,
message: "Agent does not have an LLM configured.",
});
}
const llmDetails = await this.retellRepository.getLLM(llmId);
if (!llmDetails) {
throw new HttpError({ statusCode: 404, message: "LLM details not found." });
}
const existing = llmDetails?.general_tools ?? [];
const toolNamesToRemove = eventTypeIds.flatMap((eventTypeId) => [
`check_availability_${eventTypeId}`,
`book_appointment_${eventTypeId}`,
]);
const filteredTools = existing.filter((tool) => !toolNamesToRemove.includes(tool.name));
if (filteredTools.length !== existing.length) {
await this.retellRepository.updateLLM(llmId, { general_tools: filteredTools });
this.logger.info("Removed event-specific tools from agent", {
agentId,
llmId,
removedEventTypes: eventTypeIds,
toolsRemoved: existing.length - filteredTools.length,
});
}
} catch (error) {
if (error instanceof HttpError) {
throw error;
}
this.logger.error("Failed to remove event-specific tools from agent", {
agentId,
eventTypeIds,
error,
});
throw new HttpError({
statusCode: 500,
message: `Failed to remove tools for agent ${agentId}`,
});
}
}
async cleanupUnusedTools(agentId: string, activeEventTypeIds: number[] = []) {
if (!agentId?.trim()) {
throw new HttpError({
statusCode: 400,
message: "Agent ID is required and cannot be empty",
});
}
try {
const agent = await this.getAgent(agentId);
const llmId = getLlmId(agent);
if (!llmId) {
throw new HttpError({
statusCode: 404,
message: "Agent does not have an LLM configured.",
});
}
const llmDetails = await this.retellRepository.getLLM(llmId);
if (!llmDetails) {
throw new HttpError({ statusCode: 404, message: "LLM details not found." });
}
const existing = llmDetails?.general_tools ?? [];
const eventSpecificTools = existing.filter(
(tool) => tool.name.includes("check_availability_") || tool.name.includes("book_appointment_")
);
const toolsToRemove = eventSpecificTools.filter((tool) => {
// Extract event type ID from tool name
const eventTypeIdMatch = tool.name.match(/_(\d+)$/);
if (!eventTypeIdMatch) return false;
const eventTypeId = parseInt(eventTypeIdMatch[1]);
return !activeEventTypeIds.includes(eventTypeId);
});
if (toolsToRemove.length > 0) {
const toolNamesToRemove = toolsToRemove.map((tool) => tool.name);
const filteredTools = existing.filter((tool) => !toolNamesToRemove.includes(tool.name));
await this.retellRepository.updateLLM(llmId, { general_tools: filteredTools });
this.logger.info("Cleaned up unused event-specific tools", {
agentId,
llmId,
removedTools: toolsToRemove.length,
toolNames: toolNamesToRemove,
});
return {
removedCount: toolsToRemove.length,
removedTools: toolNamesToRemove,
};
}
return {
removedCount: 0,
removedTools: [],
};
} catch (error) {
if (error instanceof HttpError) {
throw error;
}
this.logger.error("Failed to cleanup unused tools for agent", {
agentId,
activeEventTypeIds,
error,
});
throw new HttpError({
statusCode: 500,
message: `Failed to cleanup tools for agent ${agentId}`,
});
}
}
async updateAgent(
agentId: string,
data: {
agent_name?: string | null;
voice_id?: string;
language?: Language;
responsiveness?: number;
interruption_sensitivity?: number;
}
): Promise<AIPhoneServiceAgent<AIPhoneServiceProviderType.RETELL_AI>> {
if (!agentId?.trim()) {
throw new HttpError({ statusCode: 400, message: "Agent ID is required and cannot be empty" });
}
if (!data || Object.keys(data).length === 0) {
throw new HttpError({ statusCode: 400, message: "Update data is required" });
}
try {
const updateRequest = RetellAIServiceMapper.mapToUpdateAgentRequest(data);
return await this.retellRepository.updateAgent(agentId, updateRequest);
} catch (error) {
this.logger.error("Failed to update agent in external AI service", {
agentId,
data,
error,
});
throw new HttpError({
statusCode: 500,
message: `Failed to update agent ${agentId}`,
});
}
}
async listAgents({
userId,
teamId,
scope = "all",
}: {
userId: number;
teamId?: number;
scope?: "personal" | "team" | "all";
}) {
const agents = await this.agentRepository.findManyWithUserAccess({
userId,
teamId,
scope,
});
const formattedAgents = agents.map((agent) => RetellAIServiceMapper.formatAgentForList(agent));
return {
totalCount: formattedAgents.length,
filtered: formattedAgents,
};
}
async getAgentWithDetails({ id, userId, teamId }: { id: string; userId: number; teamId?: number }) {
const agent = await this.agentRepository.findByIdWithUserAccessAndDetails({
id,
userId,
teamId,
});
if (!agent) {
throw new HttpError({
statusCode: 404,
message: "Agent not found or you don't have permission to view it.",
});
}
try {
const retellAgent = await this.getAgent(agent.providerAgentId);
const llmId = getLlmId(retellAgent);
if (!llmId) {
throw new HttpError({
statusCode: 404,
message: "Agent does not have an LLM configured.",
});
}
const llmDetails = await this.retellRepository.getLLM(llmId);
return RetellAIServiceMapper.formatAgentDetails(agent, retellAgent, llmDetails);
} catch (error) {
if (error instanceof HttpError) {
throw error;
}
this.logger.error("Failed to get agent details from external AI service", {
agentId: id,
providerAgentId: agent.providerAgentId,
userId,
teamId,
error,
});
throw new HttpError({
statusCode: 500,
message: "Unable to fetch agent details. Please try again.",
});
}
}
async createAgent({
name: _name,
userId,
teamId,
workflowStepId,
setupAIConfiguration,
}: {
name?: string;
userId: number;
teamId?: number;
workflowStepId?: number;
setupAIConfiguration: () => Promise<{ llmId: string; agentId: string }>;
}) {
const agentName = _name || `Agent - ${userId} ${uuidv4()}`;
if (teamId) {
const canManage = await this.agentRepository.canManageTeamResources({
userId,
teamId,
});
if (!canManage) {
throw new HttpError({
statusCode: 403,
message: "You don't have permission to create agents for this team.",
});
}
}
const llmConfig = await setupAIConfiguration();
const agent = await this.agentRepository.create({
name: agentName,
providerAgentId: llmConfig.agentId,
userId,
teamId,
});
if (workflowStepId) {
await this.agentRepository.linkToWorkflowStep({
workflowStepId,
agentId: agent.id,
});
}
return {
id: agent.id,
providerAgentId: agent.providerAgentId,
message: "Agent created successfully",
};
}
async updateAgentConfiguration({
id,
userId,
teamId,
name,
generalPrompt,
beginMessage,
generalTools,
voiceId,
updateLLMConfiguration,
}: {
id: string;
userId: number;
teamId?: number;
name?: string;
generalPrompt?: string | null;
beginMessage?: string | null;
generalTools?: AIPhoneServiceTools<AIPhoneServiceProviderType.RETELL_AI>;
voiceId?: string;
updateLLMConfiguration: (
llmId: string,
data: AIPhoneServiceUpdateModelParams<AIPhoneServiceProviderType.RETELL_AI>
) => Promise<AIPhoneServiceModel<AIPhoneServiceProviderType.RETELL_AI>>;
}) {
const agent = await this.agentRepository.findByIdWithAdminAccess({
id,
userId,
teamId,
});
if (!agent) {
throw new HttpError({
statusCode: 404,
message: "Agent not found or you don't have permission to update it.",
});
}
const hasRetellUpdates =
generalPrompt !== undefined ||
beginMessage !== undefined ||
generalTools !== undefined ||
voiceId !== undefined;
if (hasRetellUpdates) {
try {
const retellAgent = await this.getAgent(agent.providerAgentId);
const llmId = getLlmId(retellAgent);
if (
llmId &&
(generalPrompt !== undefined || beginMessage !== undefined || generalTools !== undefined)
) {
const llmUpdateData = RetellAIServiceMapper.extractLLMUpdateData(
generalPrompt,
beginMessage,
generalTools
);
await updateLLMConfiguration(llmId, llmUpdateData);
}
if (voiceId) {
await this.updateAgent(agent.providerAgentId, {
voice_id: voiceId,
});
}
} catch (error) {
this.logger.error("Failed to update agent configuration in external AI service", {
agentId: id,
providerAgentId: agent.providerAgentId,
userId,
updates: {
generalPrompt: !!generalPrompt,
beginMessage: !!beginMessage,
generalTools: !!generalTools,
voiceId: !!voiceId,
},
error,
});
if (error instanceof HttpError) {
throw error;
}
throw new HttpError({
statusCode: 500,
message: "Unable to update agent configuration. Please try again.",
});
}
}
return { message: "Agent updated successfully" };
}
async deleteAgent({
id,
userId,
teamId,
deleteAIConfiguration,
}: {
id: string;
userId: number;
teamId?: number;
deleteAIConfiguration: (config: { agentId: string; llmId?: string }) => Promise<void>;
}) {
const agent = await this.agentRepository.findByIdWithAdminAccess({
id,
userId,
teamId,
});
if (!agent) {
throw new HttpError({
statusCode: 404,
message: "Agent not found or you don't have permission to delete it.",
});
}
try {
const retellAgent = await this.getAgent(agent.providerAgentId);
const llmId = getLlmId(retellAgent);
await deleteAIConfiguration({
agentId: agent.providerAgentId,
llmId: llmId || undefined,
});
} catch (error) {
this.logger.error("Failed to delete agent from external AI service", {
agentId: id,
providerAgentId: agent.providerAgentId,
userId,
teamId,
error,
});
}
await this.agentRepository.delete({ id });
return { message: "Agent deleted successfully" };
}
}