Send a phone call from AI agent, in an API call. Or, directly call the bot from the configured phone number!
Python
fix: Retry and hangup if successive timeouts
Clémence Lesné61f88bb44f5b
diff --git a/helpers/call_events.py b/helpers/call_events.pyindex ec0a49b..a8aa8c6 100644--- a/helpers/call_events.py+++ b/helpers/call_events.py@@ -21,6 +21,7 @@ from helpers.call_utils import (ContextEnum as CallContextEnum,handle_clear_queue,handle_hangup,+ handle_play_text,handle_recognize_ivr,handle_recognize_text,handle_transfer,@@ -160,46 +161,46 @@ async def on_recognize_timeout_error()return- if not (- contexts and CallContextEnum.LAST_CHUNK in contexts- ): # Not the last chunk, ignore- logger.debug("Ignoring timeout if bot is still speaking")- return-if (- call.recognition_retry < CONFIG.workflow.max_voice_recognition_retry- ): # Retry voice recognition- call.recognition_retry += 1- logger.info(- f"Timeout, retrying voice recognition ({call.recognition_retry}/{CONFIG.workflow.max_voice_recognition_retry})"- )- await handle_recognize_text(- call=call,- client=client,- store=False, # Do not store timeout prompt as it perturbs the LLM and makes it hallucinate- text=await CONFIG.prompts.tts.timeout_silence(call),- timeout_error=True, # Should re-trigger an error or the LLM- )-- else: # Voice retries are exhausted, end call+ call.recognition_retry >= CONFIG.workflow.max_voice_recognition_retry+ ): # Voice retries are exhausted, end calllogger.info("Timeout, ending call")await _handle_goodbye(call=call,client=client,)+ return++ if not (+ contexts and CallContextEnum.LAST_CHUNK in contexts+ ): # Not the last chunk, ignore+ logger.debug("Ignoring timeout if bot is still speaking")+ return++ # Retry voice recognition+ call.recognition_retry += 1+ logger.info(+ f"Timeout, retrying voice recognition ({call.recognition_retry}/{CONFIG.workflow.max_voice_recognition_retry})"+ )+ await handle_recognize_text(+ call=call,+ client=client,+ store=False, # Do not store timeout prompt as it perturbs the LLM and makes it hallucinate+ text=await CONFIG.prompts.tts.timeout_silence(call),+ timeout_error=True, # Should re-trigger an error or the LLM+ )async def _handle_goodbye(call: CallStateModel,client: CallAutomationClient,) -> None:- await handle_recognize_text(+ await handle_play_text(call=call,client=client,context=CallContextEnum.GOODBYE,store=False, # Do not store timeout prompt as it perturbs the LLM and makes it hallucinatetext=await CONFIG.prompts.tts.goodbye(call),- timeout_error=False,)diff --git a/helpers/call_utils.py b/helpers/call_utils.pyindex 4695c5f..1be7f21 100644--- a/helpers/call_utils.py+++ b/helpers/call_utils.py@@ -75,8 +75,10 @@ async def _handle_recognize_media() -> None:"""Play a media to a call participant and start recognizing the response.++ If `context` is provided, it will be used to track the operation."""- logger.debug(f"Recognizing media")+ logger.debug(f"Recognizing voice with text: {text}")try:assert call.voice_id, "Voice ID is required for recognizing media"async with _use_call_client(client, call.voice_id) as call_client:@@ -106,6 +108,39 @@ async def _handle_recognize_media(raise e+async def _handle_play_text(+ call: CallStateModel,+ client: CallAutomationClient,+ text: str,+ context: Optional[ContextEnum] = None,+ style: MessageStyleEnum = MessageStyleEnum.NONE,+) -> None:+ """+ Play a text to a call participant.++ If `context` is provided, it will be used to track the operation.+ """+ logger.debug(f"Playing text: {text}")+ try:+ assert call.voice_id, "Voice ID is required for playing text"+ async with _use_call_client(client, call.voice_id) as call_client:+ await call_client.play_media(+ operation_context=json.dumps([context]) if context else None,+ play_source=_audio_from_text(+ call=call,+ style=style,+ text=text,+ ),+ )+ except ResourceNotFoundError:+ logger.debug(f"Call hung up before playing")+ except HttpResponseError as e:+ if "call already terminated" in e.message.lower():+ logger.debug(f"Call hung up before playing")+ else:+ raise e++async def handle_media(client: CallAutomationClient,call: CallStateModel,@@ -169,7 +204,6 @@ async def handle_recognize_text(text=text,)for i, chunk in enumerate(chunks):- context = Noneend_silence = Noneif i == len(chunks) - 1: # Last chunkend_silence = timeout_value@@ -185,6 +219,35 @@ async def handle_recognize_text()+async def handle_play_text(+ call: CallStateModel,+ client: CallAutomationClient,+ text: str,+ context: Optional[ContextEnum] = None,+ store: bool = True,+ style: MessageStyleEnum = MessageStyleEnum.NONE,+) -> None:+ """+ Play a text to a call participant.++ If `store` is `True`, the text will be stored in the call messages.+ """+ chunks = await _chunk_before_tts(+ call=call,+ store=store,+ style=style,+ text=text,+ )+ for chunk in chunks:+ await _handle_play_text(+ call=call,+ client=client,+ context=context,+ style=style,+ text=chunk,+ )++async def handle_clear_queue(client: CallAutomationClient,call: CallStateModel,