* add trigger * small fixes * add missing workflow DTOs * small fixes * use activeOnWithChildren * fix active on when switching trigger type * remove add variable dropdown * feat: lang support * fix: type errors * feat: select voice agent * refactor: address feedback * refactor: address feedback * refactor: missing import * fix: types * add getAllWorkflowsFromRoutingForm to WorkflowService * fix error caused by undefined evt * fix type error * fix type error * fix tests * feat: add inbound calls * chore: formatting * chore * feat: finish inbound call * chore: formatting * fix: update bug * fix: types * code clean up * final fixes and clean up * remove console.log * remove template text form from triggers * add routing form repoditory function * refactor: Agent Configuration Sheet (#23930) * refactor: agent configuration sheet * chore: use default phone numbre * refactor: improvements * refactor: improvements * fix: types * fix: feedback * fix bug with key * chore: * fix: feedback * fix: prompt * add comments * fix: review * fix: review * refactor: class * refactor: class * fix test * allow cal ai action on form triggers * move any reusable code to scheduleAIPhoneCall * add missing await * use predefined FormSubmissionData type * add .trim() to sms message * pass contextData instead * finish base setup * add missing trigger in update-workflow.input.ts * allow cal.ai action for form triggers in handler * chore: add support for form workflows on api v2 * fixup! chore: add support for form workflows on api v2 * ai phone call on form submissions (WIP) * use existing type for Option array * pass chosen event type id * refactor: rename * Update apps/web/public/static/locales/en/common.json * Update apps/web/public/static/locales/en/common.json * add missing imports * chore: update set value * fix: remove index * fix: type error * fix: update tetss * use only repository functions in update handler * move all prisma queries from list.handler * review suggestions * fix: use logger * chore: handle workflows api v2 * chore: handle workflows api v2, split in 2 endpoints * fix workflow step creation * remove connect agent and fixes types * add type to workflow * chore: use workflow type in apiv2 WorkflowsOutputService * update worklfow type on update * chore: use workflow type in apiv2 WorkflowsOutputService * fix template body for torm trigger * some UI fixes for email subject/body * resetting email body when changing form triggers * use type field to query workflows * clean up all old active on values * remove responseId from all funciton calls * remove undefined from updateTemplate * refactor: don't use static * fix: type * refactor: split routing form and event-type workflows code * refactor: split routing form and event-type workflows code * fix template text when adding action * chore: don't rename WorkflowActivationDto to avoid ci blocking * refine update schedule to use only allowed actions * fix type error * don't allow whatsapp action with form trigger * fix type error * return early if activeOn array is empty * fix: from step type in BaseFormWorkflowStepDto * fixup! fix: from step type in BaseFormWorkflowStepDto * api v2 updates * move all prisma calls to repository (service/workflows.ts) * use FORM_TRIGGER_WORKFLOW_EVENTS for form queries * use userRepository * use FORM_TRIGGER_WORKFLOW_EVENTS in isFormTrigger * code clean up * code clean up * use repository functions in formSubmissionValidation.ts * fix: schema * refactor: * remove action check in update handler * add event type selection * event type selector improvements * adjust update.handler * set outboundEventTypeId * add back trpc import * fix agent repository functions * clean up * fix bugs caused by merge * pass eventTypeId to updateToolsFromAgentId * add migration for outboundEventTypeId * add SMS actions to allowed form action constants * add cal ai to allowed form actions * pick correct event type for web call * pass correct routed event type id * remove unsued import * fixes for offset api v2 * add missing responseId * fix failing test * fix failing test * improve error message * remove unused imports * chore: handle sms step action for form worklfow in dtos * fix typo * missing missing newStep * minor fixes * remove changes * add routedEventTypeId * fix type error * fix type error * fix typ error in executAPIPhoneCall.tsx * add back inboundEventTypeId * remove console.log * remove outdated code * small fixes * don't throw error for missing phone number * add back filtered triggerOptions * fix eventTypeId in testCall handler * fix type error * update migration * fix trigger is not defined * convert eventTypeId to string * only use outboundEventTypeId for FORM_SUBMITTED trigger * show toast when no event type selected * fix type errors * add missing translation * fix type error * remove callType * fix tests * small fixes * clean up AgentConfigurationSheet * remove EventTypeSelector file * code clean up * clean up * clean up * use resusable function for TestPhoneCallDialog and WebCallDialog * rename result * fix types for event type id * use repository runction in workflowReminder.ts * fix type error * pass eventTypeIds correctly * fix typo * Update apps/web/public/static/locales/en/common.json Co-authored-by: Udit Takkar <53316345+Udit-takkar@users.noreply.github.com> * use watch instead of getValues * change to z.record(z.unknown()) instead of any() * fix type of eventTypeId * check permissinon for outBoundEventTypeId * add isNaN check * improve function name * update tools when outbound agent event type id changes * pass missing outboundEventTypeId * update migration * fix test * remove cal-ai step from test --------- Co-authored-by: CarinaWolli <wollencarina@gmail.com> Co-authored-by: Udit Takkar <udit222001@gmail.com> Co-authored-by: Udit Takkar <53316345+Udit-takkar@users.noreply.github.com> Co-authored-by: Benny Joo <sldisek783@gmail.com> Co-authored-by: cal.com <morgan@cal.com> Co-authored-by: Morgan <33722304+ThyMinimalDev@users.noreply.github.com> Co-authored-by: Peer Richelsen <peeroke@gmail.com>
479 lines
14 KiB
TypeScript
479 lines
14 KiB
TypeScript
import { v4 as uuidv4 } from "uuid";
|
|
|
|
import type { FORM_SUBMITTED_WEBHOOK_RESPONSES } from "@calcom/app-store/routing-forms/lib/formSubmissionUtils";
|
|
import dayjs from "@calcom/dayjs";
|
|
import { FeaturesRepository } from "@calcom/features/flags/features.repository";
|
|
import tasker from "@calcom/features/tasker";
|
|
import { CAL_AI_AGENT_PHONE_NUMBER_FIELD } from "@calcom/lib/bookings/SystemField";
|
|
import { checkRateLimitAndThrowError } from "@calcom/lib/checkRateLimitAndThrowError";
|
|
import logger from "@calcom/lib/logger";
|
|
import prisma from "@calcom/prisma";
|
|
import { WorkflowMethods, WorkflowTriggerEvents } from "@calcom/prisma/enums";
|
|
import type { TimeUnit } from "@calcom/prisma/enums";
|
|
import { PhoneNumberSubscriptionStatus } from "@calcom/prisma/enums";
|
|
|
|
import type { FormSubmissionData, WorkflowContextData } from "./reminderScheduler";
|
|
import type { BookingInfo } from "./smsReminderManager";
|
|
|
|
type timeUnitLowerCase = "day" | "hour" | "minute";
|
|
|
|
function extractPhoneNumber(responses: BookingInfo["responses"]): string | undefined {
|
|
if (!responses) return undefined;
|
|
|
|
// Priority 1: CAL_AI_AGENT_PHONE_NUMBER_FIELD first
|
|
const aiAgentPhoneResponse = responses[CAL_AI_AGENT_PHONE_NUMBER_FIELD];
|
|
if (aiAgentPhoneResponse && typeof aiAgentPhoneResponse === "object" && "value" in aiAgentPhoneResponse) {
|
|
return aiAgentPhoneResponse.value as string;
|
|
}
|
|
|
|
// Priority 2: attendeePhoneNumber as fallback
|
|
const attendeePhoneResponse = responses.attendeePhoneNumber;
|
|
if (
|
|
attendeePhoneResponse &&
|
|
typeof attendeePhoneResponse === "object" &&
|
|
"value" in attendeePhoneResponse
|
|
) {
|
|
return attendeePhoneResponse.value as string;
|
|
}
|
|
|
|
return undefined;
|
|
}
|
|
|
|
type CreateWorkflowReminderAndExtractPhoneArgs = WorkflowContextData & {
|
|
workflowStepId: number;
|
|
scheduledDate: dayjs.Dayjs;
|
|
seatReferenceUid?: string;
|
|
submittedPhoneNumber?: string | null;
|
|
};
|
|
|
|
interface CreateWorkflowReminderAndExtractPhoneResult {
|
|
workflowReminder: { id: number; uuid: string | null };
|
|
attendeePhoneNumber: string;
|
|
}
|
|
|
|
const createWorkflowReminderAndExtractPhone = async (
|
|
args: CreateWorkflowReminderAndExtractPhoneArgs
|
|
): Promise<CreateWorkflowReminderAndExtractPhoneResult | null> => {
|
|
const { evt, workflowStepId, scheduledDate, seatReferenceUid, submittedPhoneNumber } = args;
|
|
// 1) Determine attendee phone first (fail early)
|
|
let attendeePhoneNumber = extractPhoneNumber(evt?.responses) || submittedPhoneNumber;
|
|
if (!attendeePhoneNumber) {
|
|
if (!evt) {
|
|
return null;
|
|
}
|
|
|
|
if (evt.attendees?.[0]?.phoneNumber) {
|
|
attendeePhoneNumber = evt.attendees?.[0]?.phoneNumber;
|
|
} else {
|
|
throw new Error(`No attendee phone number found for workflow step ${workflowStepId}`);
|
|
}
|
|
}
|
|
|
|
const workflowReminder = await prisma.workflowReminder.create({
|
|
data: {
|
|
bookingUid: evt?.uid,
|
|
workflowStepId,
|
|
method: WorkflowMethods.AI_PHONE_CALL,
|
|
scheduledDate: scheduledDate.toDate(),
|
|
scheduled: true,
|
|
seatReferenceId: seatReferenceUid,
|
|
},
|
|
});
|
|
|
|
return { workflowReminder, attendeePhoneNumber };
|
|
};
|
|
|
|
type ScheduleAIPhoneCallArgs = {
|
|
submittedPhoneNumber: string | null;
|
|
triggerEvent: WorkflowTriggerEvents;
|
|
timeSpan: {
|
|
time: number | null;
|
|
timeUnit: TimeUnit | null;
|
|
};
|
|
workflowStepId: number | undefined;
|
|
userId: number | null;
|
|
teamId: number | null;
|
|
seatReferenceUid?: string;
|
|
verifiedAt: Date | null;
|
|
routedEventTypeId: number | null;
|
|
} & WorkflowContextData;
|
|
|
|
export type ScheduleAIPhoneCallArgsWithRequiredFields = Omit<ScheduleAIPhoneCallArgs, "workflowStepId"> & {
|
|
workflowStepId: number; // Required
|
|
agent: Agent;
|
|
activePhoneNumber: string;
|
|
};
|
|
|
|
type Agent = {
|
|
id: string;
|
|
providerAgentId: string;
|
|
outboundPhoneNumbers: {
|
|
phoneNumber: string;
|
|
subscriptionStatus: PhoneNumberSubscriptionStatus | null;
|
|
}[];
|
|
};
|
|
|
|
export const scheduleAIPhoneCall = async (args: ScheduleAIPhoneCallArgs) => {
|
|
const { workflowStepId, verifiedAt } = args;
|
|
if (!workflowStepId) {
|
|
logger.warn(`Workflow step ID is required for AI phone call scheduling`);
|
|
return;
|
|
}
|
|
|
|
if (!verifiedAt) {
|
|
logger.warn(`Workflow step ${workflowStepId} not yet verified`);
|
|
return;
|
|
}
|
|
|
|
// Get the workflow step to check if it has an agent configured
|
|
const workflowStep = await prisma.workflowStep.findUnique({
|
|
where: { id: workflowStepId },
|
|
select: {
|
|
agent: {
|
|
select: {
|
|
id: true,
|
|
providerAgentId: true,
|
|
outboundPhoneNumbers: {
|
|
select: {
|
|
phoneNumber: true,
|
|
subscriptionStatus: true,
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
});
|
|
|
|
const activePhoneNumbers = workflowStep?.agent?.outboundPhoneNumbers?.filter(
|
|
(phoneNumber) =>
|
|
phoneNumber.subscriptionStatus === PhoneNumberSubscriptionStatus.ACTIVE ||
|
|
phoneNumber.subscriptionStatus === null ||
|
|
phoneNumber.subscriptionStatus === undefined
|
|
);
|
|
|
|
if (!workflowStep?.agent) {
|
|
logger.warn(`No agent configured for workflow step ${workflowStepId}`);
|
|
return;
|
|
}
|
|
|
|
if (!workflowStep.agent.outboundPhoneNumbers?.length || !activePhoneNumbers?.length) {
|
|
logger.warn(`No active outbound phone number configured for agent ${workflowStep.agent.id}`);
|
|
return;
|
|
}
|
|
|
|
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 params = { ...args, agent: workflowStep.agent, activePhoneNumber: activePhoneNumbers[0].phoneNumber };
|
|
|
|
if (params.evt) {
|
|
await scheduleAIPhoneCallForEvt(
|
|
params as ScheduleAIPhoneCallArgsWithRequiredFields & { evt: BookingInfo }
|
|
);
|
|
} else {
|
|
await scheduleAIPhoneCallForForm(
|
|
params as ScheduleAIPhoneCallArgsWithRequiredFields & {
|
|
formData: FormSubmissionData;
|
|
}
|
|
);
|
|
}
|
|
};
|
|
|
|
const scheduleAIPhoneCallForEvt = async (
|
|
args: ScheduleAIPhoneCallArgsWithRequiredFields & { evt: BookingInfo }
|
|
) => {
|
|
const {
|
|
evt,
|
|
triggerEvent,
|
|
timeSpan,
|
|
workflowStepId,
|
|
userId,
|
|
teamId,
|
|
seatReferenceUid,
|
|
agent,
|
|
activePhoneNumber,
|
|
} = args;
|
|
|
|
const { startTime, endTime } = evt;
|
|
const uid = evt.uid as string;
|
|
const currentDate = dayjs();
|
|
const timeUnit: timeUnitLowerCase | undefined = timeSpan.timeUnit?.toLocaleLowerCase() as timeUnitLowerCase;
|
|
let scheduledDate = null;
|
|
|
|
// Calculate when the AI phone call should be made
|
|
if (triggerEvent === WorkflowTriggerEvents.BEFORE_EVENT) {
|
|
scheduledDate = timeSpan.time && timeUnit ? dayjs(startTime).subtract(timeSpan.time, timeUnit) : null;
|
|
} else if (triggerEvent === WorkflowTriggerEvents.AFTER_EVENT) {
|
|
scheduledDate = timeSpan.time && timeUnit ? dayjs(endTime).add(timeSpan.time, timeUnit) : null;
|
|
}
|
|
|
|
// For immediate triggers (like NEW_EVENT, EVENT_CANCELLED, etc.), schedule immediately
|
|
if (!scheduledDate) {
|
|
scheduledDate = currentDate;
|
|
}
|
|
|
|
// Determine if we should execute immediately or schedule for later
|
|
const shouldExecuteImmediately =
|
|
// Immediate triggers (NEW_EVENT, EVENT_CANCELLED, etc.)
|
|
!timeSpan.time ||
|
|
!timeSpan.timeUnit ||
|
|
// Or if the scheduled time has already passed
|
|
(scheduledDate && currentDate.isAfter(scheduledDate));
|
|
|
|
if (!shouldExecuteImmediately) {
|
|
try {
|
|
const reminderAndPhone = await createWorkflowReminderAndExtractPhone({
|
|
evt,
|
|
workflowStepId,
|
|
scheduledDate,
|
|
seatReferenceUid,
|
|
});
|
|
|
|
if (!reminderAndPhone) {
|
|
logger.warn(`No phone number found for AI phone call workflow step ${workflowStepId} - skipping`);
|
|
return;
|
|
}
|
|
|
|
const { workflowReminder, attendeePhoneNumber } = reminderAndPhone;
|
|
|
|
// Schedule the actual AI phone call
|
|
await scheduleAIPhoneCallTask({
|
|
workflowReminderId: workflowReminder.id,
|
|
scheduledDate: scheduledDate.toDate(),
|
|
agentId: agent.id,
|
|
phoneNumber: activePhoneNumber,
|
|
attendeePhoneNumber,
|
|
bookingUid: uid,
|
|
userId,
|
|
teamId,
|
|
providerAgentId: agent.providerAgentId,
|
|
referenceUid: workflowReminder.uuid || uuidv4(),
|
|
formResponses: null,
|
|
routedEventTypeId: null,
|
|
});
|
|
|
|
logger.info(`AI phone call scheduled for workflow step ${workflowStepId} at ${scheduledDate}`);
|
|
} catch (error) {
|
|
logger.error(`Error scheduling AI phone call with error ${error}`);
|
|
}
|
|
} else {
|
|
// Execute immediately
|
|
try {
|
|
const reminderAndPhone = await createWorkflowReminderAndExtractPhone({
|
|
evt,
|
|
workflowStepId,
|
|
scheduledDate: currentDate,
|
|
seatReferenceUid,
|
|
});
|
|
|
|
if (!reminderAndPhone) {
|
|
logger.warn(`No phone number found for AI phone call workflow step ${workflowStepId} - skipping`);
|
|
return;
|
|
}
|
|
|
|
const { workflowReminder, attendeePhoneNumber } = reminderAndPhone;
|
|
|
|
// Schedule the actual AI phone call immediately
|
|
// Should i execute the task immediately or schedule it for later?
|
|
await scheduleAIPhoneCallTask({
|
|
workflowReminderId: workflowReminder.id,
|
|
scheduledDate: currentDate.toDate(),
|
|
agentId: agent.id,
|
|
phoneNumber: activePhoneNumber,
|
|
attendeePhoneNumber,
|
|
bookingUid: uid,
|
|
userId,
|
|
teamId,
|
|
providerAgentId: agent.providerAgentId,
|
|
referenceUid: workflowReminder.uuid || uuidv4(),
|
|
formResponses: null,
|
|
routedEventTypeId: null,
|
|
});
|
|
|
|
logger.info(`AI phone call scheduled for immediate execution for workflow step ${workflowStepId}`);
|
|
} catch (error) {
|
|
logger.error(`Error scheduling immediate AI phone call with error ${error}`);
|
|
}
|
|
}
|
|
};
|
|
|
|
// sends all immediately, no scheduling needed (tasker handles scheduling)
|
|
const scheduleAIPhoneCallForForm = async (
|
|
args: ScheduleAIPhoneCallArgsWithRequiredFields & {
|
|
formData: FormSubmissionData;
|
|
}
|
|
) => {
|
|
const {
|
|
formData,
|
|
workflowStepId,
|
|
userId,
|
|
teamId,
|
|
seatReferenceUid,
|
|
submittedPhoneNumber,
|
|
agent,
|
|
activePhoneNumber,
|
|
routedEventTypeId,
|
|
} = args;
|
|
|
|
try {
|
|
const reminderAndPhone = await createWorkflowReminderAndExtractPhone({
|
|
formData,
|
|
workflowStepId,
|
|
submittedPhoneNumber,
|
|
scheduledDate: dayjs(),
|
|
seatReferenceUid,
|
|
});
|
|
|
|
if (!reminderAndPhone) {
|
|
logger.warn(`No phone number found for AI phone call workflow step ${workflowStepId} - skipping`);
|
|
return;
|
|
}
|
|
|
|
const { workflowReminder, attendeePhoneNumber } = reminderAndPhone;
|
|
|
|
await scheduleAIPhoneCallTask({
|
|
workflowReminderId: workflowReminder.id,
|
|
agentId: agent.id,
|
|
phoneNumber: activePhoneNumber,
|
|
attendeePhoneNumber,
|
|
userId,
|
|
teamId,
|
|
scheduledDate: null,
|
|
formResponses: formData.responses,
|
|
bookingUid: null,
|
|
providerAgentId: agent.providerAgentId,
|
|
referenceUid: workflowReminder.uuid || uuidv4(),
|
|
routedEventTypeId: routedEventTypeId ?? null,
|
|
});
|
|
logger.info(`AI phone call scheduled for immediate execution for workflow step ${workflowStepId}`);
|
|
} catch (error) {
|
|
logger.error(`Error scheduling immediate AI phone call with error ${error}`);
|
|
}
|
|
};
|
|
|
|
interface ScheduleAIPhoneCallTaskArgs {
|
|
workflowReminderId: number;
|
|
scheduledDate: Date | null;
|
|
agentId: string;
|
|
phoneNumber: string;
|
|
attendeePhoneNumber: string;
|
|
bookingUid: string | null;
|
|
userId: number | null;
|
|
teamId: number | null;
|
|
providerAgentId: string;
|
|
referenceUid: string;
|
|
formResponses: FORM_SUBMITTED_WEBHOOK_RESPONSES | null;
|
|
routedEventTypeId: number | null;
|
|
}
|
|
|
|
const scheduleAIPhoneCallTask = async (args: ScheduleAIPhoneCallTaskArgs) => {
|
|
const {
|
|
workflowReminderId,
|
|
scheduledDate,
|
|
agentId,
|
|
phoneNumber,
|
|
attendeePhoneNumber,
|
|
bookingUid,
|
|
userId,
|
|
teamId,
|
|
providerAgentId,
|
|
referenceUid,
|
|
formResponses,
|
|
routedEventTypeId,
|
|
} = args;
|
|
|
|
if (!formResponses && !bookingUid) {
|
|
logger.warn("context missing for creating AI phone call task");
|
|
return;
|
|
}
|
|
|
|
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");
|
|
return;
|
|
}
|
|
|
|
if (userId) {
|
|
await checkRateLimitAndThrowError({
|
|
rateLimitingType: "core",
|
|
identifier: `ai-phone-call:${userId}`,
|
|
});
|
|
}
|
|
|
|
try {
|
|
await tasker.create(
|
|
"executeAIPhoneCall",
|
|
{
|
|
workflowReminderId,
|
|
agentId,
|
|
fromNumber: phoneNumber,
|
|
toNumber: attendeePhoneNumber,
|
|
bookingUid,
|
|
userId,
|
|
teamId,
|
|
providerAgentId,
|
|
responses: formResponses,
|
|
routedEventTypeId,
|
|
},
|
|
{
|
|
scheduledAt: scheduledDate || undefined,
|
|
maxAttempts: 1,
|
|
referenceUid,
|
|
}
|
|
);
|
|
} catch (error) {
|
|
console.error("Error creating AI phone call task:", error);
|
|
throw error;
|
|
}
|
|
};
|
|
|
|
export const deleteScheduledAIPhoneCall = async (reminderId: number, referenceId: string | null) => {
|
|
const workflowReminder = await prisma.workflowReminder.findUnique({
|
|
where: {
|
|
id: reminderId,
|
|
},
|
|
});
|
|
|
|
if (!workflowReminder) {
|
|
logger.error("AI Phone Call workflow reminder not found");
|
|
return;
|
|
}
|
|
|
|
const { uuid } = workflowReminder;
|
|
const taskReferenceId = referenceId || uuid;
|
|
|
|
if (taskReferenceId) {
|
|
try {
|
|
const taskId = await tasker.cancelWithReference(taskReferenceId, "executeAIPhoneCall");
|
|
if (taskId) {
|
|
await prisma.workflowReminder.delete({
|
|
where: {
|
|
id: reminderId,
|
|
},
|
|
});
|
|
|
|
logger.info(`AI phone call reminder ${reminderId} cancelled and deleted`);
|
|
return;
|
|
}
|
|
} catch (error) {
|
|
logger.error(`Error canceling/deleting AI phone call reminder with tasker. Error: ${error}`);
|
|
}
|
|
}
|
|
|
|
// Fallback: If tasker cancellation fails or uuid is not found, just delete the reminder
|
|
try {
|
|
await prisma.workflowReminder.delete({
|
|
where: {
|
|
id: reminderId,
|
|
},
|
|
});
|
|
logger.info(`AI phone call reminder ${reminderId} deleted (no task to cancel)`);
|
|
} catch (error) {
|
|
logger.error(`Error deleting AI phone call reminder ${reminderId}: ${error}`);
|
|
}
|
|
};
|