|
| 1 | +"""Agent launcher with warmup support.""" |
| 2 | + |
| 3 | +import asyncio |
| 4 | +import logging |
| 5 | +from typing import Optional, TYPE_CHECKING, Callable, Awaitable, Union, cast |
| 6 | + |
| 7 | +if TYPE_CHECKING: |
| 8 | + from .agents import Agent |
| 9 | + |
| 10 | +logger = logging.getLogger(__name__) |
| 11 | + |
| 12 | +class AgentProcess: |
| 13 | + """ |
| 14 | + Add info here about the thread/process. Enabling warm up to work well in a multiprocess env |
| 15 | + """ |
| 16 | + pass |
| 17 | + |
| 18 | +class AgentLauncher: |
| 19 | + """ |
| 20 | + Agent launcher that handles warmup and lifecycle management. |
| 21 | + |
| 22 | + The launcher ensures all components (LLM, TTS, STT, turn detection) |
| 23 | + are warmed up before the agent is launched. |
| 24 | + """ |
| 25 | + |
| 26 | + def __init__( |
| 27 | + self, |
| 28 | + create_agent: Callable[..., Union["Agent", Awaitable["Agent"]]], |
| 29 | + join_call: Optional[Callable[..., Union[None, Awaitable[None]]]] = None, |
| 30 | + ): |
| 31 | + """ |
| 32 | + Initialize the agent launcher. |
| 33 | + |
| 34 | + Args: |
| 35 | + create_agent: A function that creates and returns an Agent instance |
| 36 | + join_call: Optional function that handles joining a call with the agent |
| 37 | + """ |
| 38 | + self.create_agent = create_agent |
| 39 | + self.join_call = join_call |
| 40 | + self._agent: Optional["Agent"] = None |
| 41 | + self._warmed_up = False |
| 42 | + self._warmup_lock = asyncio.Lock() |
| 43 | + |
| 44 | + async def warmup(self, **kwargs) -> None: |
| 45 | + """ |
| 46 | + Warm up all agent components. |
| 47 | + |
| 48 | + This method creates the agent and calls warmup on LLM, TTS, STT, |
| 49 | + and turn detection components if they exist. It ensures warmup is |
| 50 | + only called once. |
| 51 | + |
| 52 | + Args: |
| 53 | + **kwargs: Additional keyword arguments to pass to create_agent |
| 54 | + """ |
| 55 | + async with self._warmup_lock: |
| 56 | + if self._warmed_up: |
| 57 | + logger.debug("Agent already warmed up, skipping") |
| 58 | + return |
| 59 | + |
| 60 | + logger.info("Creating agent...") |
| 61 | + |
| 62 | + # Create the agent |
| 63 | + result = self.create_agent(**kwargs) |
| 64 | + if asyncio.iscoroutine(result): |
| 65 | + agent: "Agent" = await result |
| 66 | + else: |
| 67 | + agent = cast("Agent", result) |
| 68 | + |
| 69 | + self._agent = agent |
| 70 | + |
| 71 | + logger.info("Warming up agent components...") |
| 72 | + |
| 73 | + # Warmup tasks to run in parallel |
| 74 | + warmup_tasks = [] |
| 75 | + |
| 76 | + # Warmup LLM (including Realtime) |
| 77 | + if agent.llm and hasattr(agent.llm, 'warmup'): |
| 78 | + logger.debug("Warming up LLM: %s", agent.llm.__class__.__name__) |
| 79 | + warmup_tasks.append(agent.llm.warmup()) |
| 80 | + |
| 81 | + # Warmup TTS |
| 82 | + if agent.tts and hasattr(agent.tts, 'warmup'): |
| 83 | + logger.debug("Warming up TTS: %s", agent.tts.__class__.__name__) |
| 84 | + warmup_tasks.append(agent.tts.warmup()) |
| 85 | + |
| 86 | + # Warmup STT |
| 87 | + if agent.stt and hasattr(agent.stt, 'warmup'): |
| 88 | + logger.debug("Warming up STT: %s", agent.stt.__class__.__name__) |
| 89 | + warmup_tasks.append(agent.stt.warmup()) |
| 90 | + |
| 91 | + # Warmup turn detection |
| 92 | + if agent.turn_detection and hasattr(agent.turn_detection, 'warmup'): |
| 93 | + logger.debug("Warming up turn detection: %s", agent.turn_detection.__class__.__name__) |
| 94 | + warmup_tasks.append(agent.turn_detection.warmup()) |
| 95 | + |
| 96 | + # Run all warmups in parallel |
| 97 | + if warmup_tasks: |
| 98 | + await asyncio.gather(*warmup_tasks) |
| 99 | + |
| 100 | + self._warmed_up = True |
| 101 | + logger.info("Agent warmup completed") |
| 102 | + |
| 103 | + async def launch(self, **kwargs) -> "Agent": |
| 104 | + """ |
| 105 | + Launch the agent with warmup. |
| 106 | + |
| 107 | + This ensures warmup is called before returning the agent. |
| 108 | + |
| 109 | + Args: |
| 110 | + **kwargs: Additional keyword arguments to pass to create_agent |
| 111 | + |
| 112 | + Returns: |
| 113 | + The warmed-up agent instance |
| 114 | + """ |
| 115 | + await self.warmup(**kwargs) |
| 116 | + assert self._agent is not None, "Agent should be created during warmup" |
| 117 | + return self._agent |
| 118 | + |
0 commit comments