Send a phone call from AI agent, in an API call. Or, directly call the bot from the configured phone number!
Python
fix: Close call properly after hanging up
Clémence Lesnéd38dc68df4ae
diff --git a/app/helpers/call_events.py b/app/helpers/call_events.pyindex c7738ca..211a484 100644--- a/app/helpers/call_events.py+++ b/app/helpers/call_events.py@@ -106,6 +106,7 @@ async def on_call_connected() -> None:logger.info("Call connected, asking for language")call.recognition_retry = 0 # Reset recognition retry counter+ call.in_progress = Truecall.messages.append(MessageModel(action=MessageActionEnum.CALL,@@ -171,6 +172,7 @@ async def on_recognize_error(call: CallStateModel,client: CallAutomationClient,contexts: set[CallContextEnum] | None,+ post_callback: Callable[[CallStateModel], Awaitable[None]],) -> None:# Retry IVR recognitionif contexts and CallContextEnum.IVR_LANG_SELECT in contexts:@@ -190,6 +192,7 @@ async def on_recognize_error(await _handle_goodbye(call=call,client=client,+ post_callback=post_callback,)return@@ -199,6 +202,7 @@ async def on_recognize_error(await _handle_goodbye(call=call,client=client,+ post_callback=post_callback,)return@@ -217,8 +221,9 @@ async def on_recognize_error(async def _handle_goodbye(call: CallStateModel,client: CallAutomationClient,+ post_callback: Callable[[CallStateModel], Awaitable[None]],) -> None:- await handle_play_text(+ res = await handle_play_text(call=call,client=client,context=CallContextEnum.GOODBYE,@@ -226,6 +231,14 @@ async def _handle_goodbye(text=await CONFIG.prompts.tts.goodbye(call),)+ if not res:+ logger.info("Failed to play goodbye prompt, ending call now")+ await _handle_hangup(+ call=call,+ client=client,+ post_callback=post_callback,+ )+@tracer.start_as_current_span("on_play_completed")async def on_play_completed(@@ -393,6 +406,7 @@ async def _handle_hangup(post_callback: Callable[[CallStateModel], Awaitable[None]],) -> None:await handle_hangup(client=client, call=call)+ call.in_progress = Falsecall.messages.append(MessageModel(action=MessageActionEnum.HANGUP,diff --git a/app/helpers/call_llm.py b/app/helpers/call_llm.pyindex 5ef3418..005cba6 100644--- a/app/helpers/call_llm.py+++ b/app/helpers/call_llm.py@@ -130,6 +130,7 @@ async def load_llm_chat( # noqa: PLR0913call=call,client=automation_client,contexts=None,+ post_callback=post_callback,))@@ -171,7 +172,10 @@ async def load_llm_chat( # noqa: PLR0913))- # Add recognition to the call history+ # Clear the recognition buffer+ recognizer_buffer.clear()++ # Store recognitio tasknonlocal last_responselast_response = await scheduler.spawn(_out_answer(@@ -183,11 +187,12 @@ async def load_llm_chat( # noqa: PLR0913))- # Clear the recognition buffer- recognizer_buffer.clear()+ # Wait for the response to be processed+ await last_response.wait()await _in_audio(bits_per_sample=audio_bits_per_sample,+ call=call,channels=audio_channels,clear_audio_callback=_clear_audio_callback,in_stream=audio_stream,@@ -552,6 +557,7 @@ async def _execute_llm_chat( # noqa: PLR0911, PLR0912, PLR0915# TODO: Refacto and simplifyasync def _in_audio( # noqa: PLR0913bits_per_sample: int,+ call: CallStateModel,channels: int,clear_audio_callback: Callable[[], Awaitable[None]],in_stream: asyncio.Queue[bytes],@@ -561,59 +567,33 @@ async def _in_audio( # noqa: PLR0913timeout_callback: Callable[[], Awaitable[None]],) -> None:clear_tts_task: asyncio.Task | None = None- no_voice_task: asyncio.Task | None = None+ flush_task: asyncio.Task | None = Nonevad = Vad(# Aggressiveness mode (0, 1, 2, or 3)# Sets the VAD operating mode. A more aggressive (higher mode) VAD is more restrictive in reporting speech. Put in other words the probability of being speech when the VAD returns 1 is increased with increasing mode. As a consequence also the missed detection rate goes up.mode=3,)- async def _timeout_callback() -> None:- """- Alert the user that the call is about to be cut off.- """- timeout_sec = await phone_silence_timeout_sec()-- while True:- logger.debug(- "Wait foor %i sec before cutting off the call",- timeout_sec,- )-- # Wait for the timeout- await asyncio.sleep(timeout_sec)-- logger.info("Phone silence timeout triggered")-- # Execute the callback- await timeout_callback()-async def _flush_callback() -> None:"""Flush the audio buffer if no audio is detected for a while."""+ # Wait for the timeoutnonlocal clear_tts_task-timeout_ms = await vad_silence_timeout_ms()-- # Wait for the timeoutawait asyncio.sleep(timeout_ms / 1000)-- # Cancel the TTS clear task if anyif clear_tts_task:clear_tts_task.cancel()clear_tts_task = None-logger.debug("Flushing audio buffer after %i ms", timeout_ms)-- # Commit the bufferawait response_callback()- async def _no_voice_callback() -> None:- await asyncio.gather(- _flush_callback(),- _timeout_callback(),- )+ # Wait for the timeout, if any+ timeout_sec = await phone_silence_timeout_sec()+ while call.in_progress:+ await asyncio.sleep(timeout_sec)+ logger.info("Silence triggered after %i sec", timeout_sec)+ await timeout_callback()async def _clear_tts_callback() -> None:"""@@ -663,17 +643,17 @@ async def _in_audio( # noqa: PLR0913):in_empty = True# Start timeout if not already started- if not no_voice_task:- no_voice_task = asyncio.create_task(_no_voice_callback())+ if not flush_task:+ flush_task = asyncio.create_task(_flush_callback())if in_empty:# Continue to the next audio packetcontinue# Voice detected, cancel the timeout if any- if no_voice_task:- no_voice_task.cancel()- no_voice_task = None+ if flush_task:+ flush_task.cancel()+ flush_task = None# Start the TTS clear taskif not clear_tts_task:diff --git a/app/helpers/call_utils.py b/app/helpers/call_utils.pyindex 3b01727..5f605dc 100644--- a/app/helpers/call_utils.py+++ b/app/helpers/call_utils.py@@ -91,11 +91,13 @@ async def _handle_play_text(context: ContextEnum | None,style: MessageStyleEnum,text: str,-) -> None:+) -> bool:"""Play a text to a call participant.If `context` is provided, it will be used to track the operation.++ Returns `True` if the text was played, `False` otherwise."""logger.info("Playing TTS: %s", text)try:@@ -109,6 +111,7 @@ async def _handle_play_text(text=text,),)+ return Trueexcept ResourceNotFoundError:logger.debug("Call hung up before playing")except HttpResponseError as e:@@ -116,6 +119,7 @@ async def _handle_play_text(logger.debug("Call hung up before playing")else:raise e+ return Falseasync def handle_media(@@ -152,11 +156,13 @@ async def handle_play_text( # noqa: PLR0913context: ContextEnum | None = None,store: bool = True,style: MessageStyleEnum = MessageStyleEnum.NONE,-) -> None:+) -> bool:"""Play a text to a call participant.If `store` is `True`, the text will be stored in the call messages.++ Returns `True` if the text was played, `False` otherwise."""# Split text in chunkschunks = await _chunk_before_tts(@@ -168,13 +174,16 @@ async def handle_play_text( # noqa: PLR0913# Play each chunkfor chunk in chunks:- await _handle_play_text(+ res = await _handle_play_text(call=call,client=client,context=context,style=style,text=chunk,)+ if not res:+ return False+ return Trueasync def handle_clear_queue(diff --git a/app/main.py b/app/main.pyindex 8b36473..ba55c27 100644--- a/app/main.py+++ b/app/main.py@@ -735,6 +735,7 @@ async def _communicationservices_event_worker(call=call,client=automation_client,contexts=operation_contexts,+ post_callback=_trigger_post_event,)case "Microsoft.Communication.PlayCompleted": # Media playeddiff --git a/app/models/call.py b/app/models/call.pyindex cd93ce2..b8df7df 100644--- a/app/models/call.py+++ b/app/models/call.py@@ -14,7 +14,6 @@ from app.helpers.config_models.conversation import (from app.helpers.monitoring import tracerfrom app.helpers.pydantic_types.phone_numbers import PhoneNumberfrom app.models.message import (- ActionEnum as MessageActionEnum,MessageModel,PersonaEnum as MessagePersonaEnum,StyleEnum as MessageStyleEnum,@@ -34,36 +33,16 @@ class CallGetModel(BaseModel):call_id: UUID = Field(default_factory=uuid4, frozen=True)created_at: datetime = Field(default_factory=lambda: datetime.now(UTC), frozen=True)# Editable fields- initiate: CallInitiateModel = Field(frozen=True)claim: dict[str, Any] = {} # Place after "initiate" as it depends on it for validation+ in_progress: bool = False+ initiate: CallInitiateModel = Field(frozen=True)messages: list[MessageModel] = []next: NextModel | None = Nonereminders: list[ReminderModel] = []synthesis: SynthesisModel | None = None- @computed_field- @property- def in_progress(self) -> bool:- """- Check if the call is in progress.-- The call is in progress if the most recent message action status (CALL or HANGUP) is CALL. Otherwise, it is not in progress.- """- # Reverse- inverted_messages = self.messages.copy()- inverted_messages.reverse()- # Search for the first action we want- for message in inverted_messages:- match message.action:- case MessageActionEnum.CALL:- return True- case MessageActionEnum.HANGUP:- return False- # Otherwise, we assume the call is completed- return False-@field_validator("claim")@classmethoddef _validate_claim(