11import asyncio
22import logging
3+ import os
34import warnings
45from typing import Optional
56from uuid import uuid4
2627
2728logger = logging .getLogger (__name__ )
2829
29- # TODO:
30- # - Figure out how to serialize the agent config into some dict
31- # - Docs
32-
3330asyncio_logger = logging .getLogger ("asyncio" )
3431
3532
@@ -127,12 +124,15 @@ async def _run():
127124 # Start the agent launcher.
128125 await self ._launcher .start ()
129126
130- # Create the agent
131- agent = await self ._launcher .launch ()
132-
133127 logger .info ("✅ Agent warmed up and ready" )
134128
129+ # Join call if join_call function is provided
130+ logger .info (f"📞 Joining call: { call_type } /{ call_id } " )
131+ session = await self ._launcher .start_session (
132+ call_id , call_type , video_track_override_path = video_track_override
133+ )
135134 # Open demo UI by default
135+ agent = session .agent
136136 if (
137137 not no_demo
138138 and hasattr (agent , "edge" )
@@ -141,11 +141,6 @@ async def _run():
141141 logger .info ("🌐 Opening demo UI..." )
142142 await agent .edge .open_demo_for_agent (agent , call_type , call_id )
143143
144- # Join call if join_call function is provided
145- logger .info (f"📞 Joining call: { call_type } /{ call_id } " )
146- session = await self ._launcher .start_session (
147- call_id , call_type , video_track_override_path = video_track_override
148- )
149144 await session .wait ()
150145 except asyncio .CancelledError :
151146 logger .info ("The session is cancelled, shutting down gracefully..." )
@@ -177,18 +172,17 @@ def serve(
177172 port : int = 8000 ,
178173 agents_log_level : str = "INFO" ,
179174 http_log_level : str = "INFO" ,
180- ):
175+ debug : bool = False ,
176+ ) -> None :
181177 """
182178 Start the HTTP server that spawns agents to the calls.
183179
184180 Args:
185- host:
186- port:
187- agents_log_level:
188- http_log_level:
189-
190- Returns:
191-
181+ host: Host address to bind the server to.
182+ port: Port number for the server.
183+ agents_log_level: Logging level for agent-related logs.
184+ http_log_level: Logging level for FastAPI and uvicorn logs.
185+ debug: Enable asyncio debug mode.
192186 """
193187 # Configure loggers if they're not already configured
194188 configure_sdk_logger (
@@ -203,9 +197,22 @@ def serve(
203197 warnings .filterwarnings (
204198 "ignore" , category = RuntimeWarning , module = "dataclasses_json.core"
205199 )
200+
201+ # Enable asyncio debug via environment variable before uvicorn creates its loop
202+ if debug :
203+ os .environ .setdefault ("PYTHONASYNCIODEBUG" , "1" )
206204 uvicorn .run (self .fast_api , host = host , port = port , log_config = None )
207205
208206 def _create_fastapi_app (self , options : ServeOptions ) -> FastAPI :
207+ """
208+ Create and configure a FastAPI application for serving agents.
209+
210+ Args:
211+ options: Configuration options for the server.
212+
213+ Returns:
214+ Configured FastAPI application instance.
215+ """
209216 app = FastAPI (lifespan = lifespan )
210217 app .state .launcher = self ._launcher
211218 app .state .options = self ._serve_options
@@ -228,9 +235,9 @@ def _create_fastapi_app(self, options: ServeOptions) -> FastAPI:
228235 )
229236 return app
230237
231- def cli (self ):
238+ def cli (self ) -> None :
232239 """
233- Run the CLI
240+ Run the command-line interface with `run` and `serve` subcommands.
234241 """
235242
236243 @click .group ()
@@ -326,11 +333,18 @@ def run_cmd(
326333 default = "INFO" ,
327334 help = "Set the logging level for FastAPI and uvicorn" ,
328335 )
336+ @click .option (
337+ "--debug" ,
338+ is_flag = True ,
339+ default = False ,
340+ help = "Enable asyncio debug mode" ,
341+ )
329342 def serve_cmd (
330343 host : str ,
331344 port : int ,
332345 agents_log_level : str ,
333346 http_log_level : str ,
347+ debug : bool ,
334348 ) -> None :
335349 """
336350 Start the HTTP server that spawns agents to the calls.
@@ -340,6 +354,7 @@ def serve_cmd(
340354 port = port ,
341355 agents_log_level = agents_log_level .upper (),
342356 http_log_level = http_log_level .upper (),
357+ debug = debug ,
343358 )
344359
345360 cli_ ()
0 commit comments