11import { v4 as uuidv4 } from "uuid" ;
22
3+ import { RETELL_AI_TEST_MODE , RETELL_AI_TEST_EVENT_TYPE_MAP } from "@calcom/lib/constants" ;
34import { timeZoneSchema } from "@calcom/lib/dayjs/timeZone.schema" ;
45import { HttpError } from "@calcom/lib/http-error" ;
56import logger from "@calcom/lib/logger" ;
@@ -81,6 +82,13 @@ export class AgentService {
8182 } ) ;
8283 }
8384
85+ let eventTypeId = data . eventTypeId ;
86+
87+ if ( RETELL_AI_TEST_MODE && RETELL_AI_TEST_EVENT_TYPE_MAP ) {
88+ const mappedId = RETELL_AI_TEST_EVENT_TYPE_MAP [ String ( data . eventTypeId ) ] ;
89+ eventTypeId = mappedId ? Number ( mappedId ) : data . eventTypeId ;
90+ }
91+
8492 try {
8593 const agent = await this . getAgent ( agentId ) ;
8694 const llmId = getLlmId ( agent ) ;
@@ -99,8 +107,8 @@ export class AgentService {
99107
100108 const existing = llmDetails ?. general_tools ?? [ ] ;
101109
102- const hasCheck = existing . some ( ( t ) => t . name === `check_availability_${ data . eventTypeId } ` ) ;
103- const hasBook = existing . some ( ( t ) => t . name === `book_appointment_${ data . eventTypeId } ` ) ;
110+ const hasCheck = existing . some ( ( t ) => t . name === `check_availability_${ eventTypeId } ` ) ;
111+ const hasBook = existing . some ( ( t ) => t . name === `book_appointment_${ eventTypeId } ` ) ;
104112 // If both already exist and end_call also exists, nothing to do
105113 const hasEndCallAlready = existing . some ( ( t ) => t . type === "end_call" ) ;
106114 if ( hasCheck && hasBook && hasEndCallAlready ) {
@@ -113,27 +121,29 @@ export class AgentService {
113121 ) ?. cal_api_key ;
114122
115123 const apiKey =
116- reusableKey ??
117- ( await this . createApiKey ( {
118- userId : data . userId ,
119- teamId : data . teamId || undefined ,
120- } ) ) ;
124+ RETELL_AI_TEST_MODE && process . env . RETELL_AI_TEST_CAL_API_KEY
125+ ? process . env . RETELL_AI_TEST_CAL_API_KEY
126+ : reusableKey ??
127+ ( await this . createApiKey ( {
128+ userId : data . userId ,
129+ teamId : data . teamId || undefined ,
130+ } ) ) ;
121131
122132 const newEventTools : NonNullable < AIPhoneServiceTools < AIPhoneServiceProviderType . RETELL_AI > > = [ ] ;
123133 if ( ! hasCheck ) {
124134 newEventTools . push ( {
125- name : `check_availability_${ data . eventTypeId } ` ,
135+ name : `check_availability_${ eventTypeId } ` ,
126136 type : "check_availability_cal" ,
127- event_type_id : data . eventTypeId ,
137+ event_type_id : eventTypeId ,
128138 cal_api_key : apiKey ,
129139 timezone : data . timeZone ,
130140 } ) ;
131141 }
132142 if ( ! hasBook ) {
133143 newEventTools . push ( {
134- name : `book_appointment_${ data . eventTypeId } ` ,
144+ name : `book_appointment_${ eventTypeId } ` ,
135145 type : "book_appointment_cal" ,
136- event_type_id : data . eventTypeId ,
146+ event_type_id : eventTypeId ,
137147 cal_api_key : apiKey ,
138148 timezone : data . timeZone ,
139149 } ) ;
@@ -180,6 +190,15 @@ export class AgentService {
180190 return ;
181191 }
182192
193+ let mappedEventTypeIds = eventTypeIds ;
194+
195+ if ( RETELL_AI_TEST_MODE && RETELL_AI_TEST_EVENT_TYPE_MAP ) {
196+ mappedEventTypeIds = eventTypeIds . map ( ( id ) => {
197+ const mappedId = RETELL_AI_TEST_EVENT_TYPE_MAP [ String ( id ) ] ;
198+ return mappedId ? Number ( mappedId ) : id ;
199+ } ) ;
200+ }
201+
183202 try {
184203 const agent = await this . getAgent ( agentId ) ;
185204 const llmId = getLlmId ( agent ) ;
@@ -199,7 +218,7 @@ export class AgentService {
199218
200219 const existing = llmDetails ?. general_tools ?? [ ] ;
201220
202- const toolNamesToRemove = eventTypeIds . flatMap ( ( eventTypeId ) => [
221+ const toolNamesToRemove = mappedEventTypeIds . flatMap ( ( eventTypeId ) => [
203222 `check_availability_${ eventTypeId } ` ,
204223 `book_appointment_${ eventTypeId } ` ,
205224 ] ) ;
@@ -212,6 +231,7 @@ export class AgentService {
212231 agentId,
213232 llmId,
214233 removedEventTypes : eventTypeIds ,
234+ mappedEventTypes : RETELL_AI_TEST_MODE ? mappedEventTypeIds : undefined ,
215235 toolsRemoved : existing . length - filteredTools . length ,
216236 } ) ;
217237 }
@@ -240,6 +260,15 @@ export class AgentService {
240260 } ) ;
241261 }
242262
263+ let mappedActiveEventTypeIds = activeEventTypeIds ;
264+
265+ if ( RETELL_AI_TEST_MODE && RETELL_AI_TEST_EVENT_TYPE_MAP ) {
266+ mappedActiveEventTypeIds = activeEventTypeIds . map ( ( id ) => {
267+ const mappedId = RETELL_AI_TEST_EVENT_TYPE_MAP [ String ( id ) ] ;
268+ return mappedId ? Number ( mappedId ) : id ;
269+ } ) ;
270+ }
271+
243272 try {
244273 const agent = await this . getAgent ( agentId ) ;
245274 const llmId = getLlmId ( agent ) ;
@@ -269,7 +298,7 @@ export class AgentService {
269298 if ( ! eventTypeIdMatch ) return false ;
270299
271300 const eventTypeId = parseInt ( eventTypeIdMatch [ 1 ] ) ;
272- return ! activeEventTypeIds . includes ( eventTypeId ) ;
301+ return ! mappedActiveEventTypeIds . includes ( eventTypeId ) ;
273302 } ) ;
274303
275304 if ( toolsToRemove . length > 0 ) {
@@ -303,6 +332,7 @@ export class AgentService {
303332 this . logger . error ( "Failed to cleanup unused tools for agent" , {
304333 agentId,
305334 activeEventTypeIds,
335+ mappedActiveEventTypeIds : RETELL_AI_TEST_MODE ? mappedActiveEventTypeIds : undefined ,
306336 error,
307337 } ) ;
308338 throw new HttpError ( {
0 commit comments