import { useState } from "react"; import type { UseFormReturn } from "react-hook-form"; import type { Language } from "@calcom/features/calAIPhone/providers/retellAI/types"; import { restorePromptComplexity } from "@calcom/features/calAIPhone/providers/retellAI/utils/promptUtils"; import type { FormValues } from "@calcom/features/ee/workflows/lib/types"; import { useLocale } from "@calcom/lib/hooks/useLocale"; import type { RouterOutputs } from "@calcom/trpc/react"; import { trpc } from "@calcom/trpc/react"; import { Button } from "@calcom/ui/components/button"; import { Dropdown, DropdownItem, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger, } from "@calcom/ui/components/dropdown"; import type { MultiSelectCheckboxesOptionType as Option } from "@calcom/ui/components/form"; import { ToggleGroup } from "@calcom/ui/components/form"; import { Sheet, SheetContent, SheetHeader, SheetTitle, SheetBody, SheetFooter, } from "@calcom/ui/components/sheet"; import { showToast } from "@calcom/ui/components/toast"; import { WebCallDialog } from "../WebCallDialog"; import { IncomingCallsTab } from "./components/tabs/IncomingCallsTab"; import { OutgoingCallsTab } from "./components/tabs/OutgoingCallsTab"; import { PhoneNumberTab } from "./components/tabs/PhoneNumberTab"; import { useAgentForms } from "./hooks/useAgentForms"; import type { AgentFormValues } from "./types/schemas"; type AgentConfigurationSheetProps = { open: boolean; onOpenChange: (open: boolean) => void; agentId?: string | null; inboundAgentId?: string | null; agentData?: RouterOutputs["viewer"]["aiVoiceAgent"]["get"]; inboundAgentData?: RouterOutputs["viewer"]["aiVoiceAgent"]["get"]; onUpdate: (data: AgentFormValues & { id: string }) => void; readOnly?: boolean; teamId?: number; isOrganization?: boolean; workflowId?: string; workflowStepId?: number; activeTab?: "outgoingCalls" | "phoneNumber" | "incomingCalls"; form: UseFormReturn; eventTypeOptions?: Option[]; }; export function AgentConfigurationSheet({ open, activeTab: _activeTab, onOpenChange, agentId, inboundAgentId, agentData, inboundAgentData, readOnly = false, teamId, isOrganization = false, workflowId, workflowStepId, form, eventTypeOptions = [], }: AgentConfigurationSheetProps) { const { t } = useLocale(); const utils = trpc.useUtils(); const [activeTab, setActiveTab] = useState<"outgoingCalls" | "phoneNumber" | "incomingCalls">( _activeTab ?? "outgoingCalls" ); const [isWebCallDialogOpen, setIsWebCallDialogOpen] = useState(false); const activeAgentId = activeTab === "incomingCalls" ? inboundAgentId : agentId; const { outboundAgentForm, inboundAgentForm } = useAgentForms({ agentData, inboundAgentData }); const trigger = form.watch("trigger"); const updateAgentMutation = trpc.viewer.aiVoiceAgent.update.useMutation({ onSuccess: async () => { if (activeAgentId) { await utils.viewer.aiVoiceAgent.get.invalidate({ id: activeAgentId }); } }, onError: (error: { message: string }) => { showToast(error.message, "error"); }, }); const handleAgentUpdate = async (data: AgentFormValues) => { if (!agentId) return; if (trigger === "FORM_SUBMITTED" && !data.outboundEventTypeId) { showToast(t("select_event_type_to_schedule_calls"), "error"); return; } const updatePayload = { generalPrompt: restorePromptComplexity(data.generalPrompt), beginMessage: data.beginMessage, language: data.language as Language, voiceId: data.voiceId, outboundEventTypeId: data.outboundEventTypeId, }; await updateAgentMutation.mutateAsync({ id: agentId, teamId: teamId, ...updatePayload, }); }; const handleInboundAgentUpdate = async (data: AgentFormValues) => { if (!inboundAgentId) return; const updatePayload = { generalPrompt: restorePromptComplexity(data.generalPrompt), beginMessage: data.beginMessage, language: data.language as Language, voiceId: data.voiceId, }; await updateAgentMutation.mutateAsync({ id: inboundAgentId, teamId: teamId, ...updatePayload, }); }; return ( <> {t("cal_ai_agent_configuration")} { setActiveTab((val || "outgoingCalls") as "outgoingCalls" | "phoneNumber" | "incomingCalls"); }} value={activeTab} options={[ { value: "outgoingCalls", label: t("outgoing_calls") }, { value: "phoneNumber", label: t("phone_number") }, { value: "incomingCalls", label: t("incoming_calls") }, ]} isFullWidth={true} /> {activeTab === "outgoingCalls" && ( )} {activeTab === "phoneNumber" && ( parseInt(opt.value, 10))} /> )} {activeTab === "incomingCalls" && ( )}
{ setIsWebCallDialogOpen(true); }}> {t("web_call")}
{activeAgentId && ( parseInt(opt.value, 10))} outboundEventTypeId={agentData?.outboundEventTypeId} /> )} {/* */} ); } // const ToolsDialog = ({ // open, // onOpenChange, // toolDraft, // setToolDraft, // onSave, // isEditing, // }: { // open: boolean; // onOpenChange: (open: boolean) => void; // toolDraft: ToolDraft | null; // setToolDraft: (draft: ToolDraft) => void; // onSave: () => void; // isEditing: boolean; // }) => { // const { t } = useLocale(); // const TOOL_TYPES = [ // { label: t("Check Availability"), value: "check_availability_cal" }, // { label: t("Book Appointment"), value: "book_appointment_cal" }, // { label: t("End Call"), value: "end_call" }, // ]; // return ( // // //
//
// //