in agents/live_api/app/server.py [0:0]
def get_connect_and_run_callable(websocket: WebSocket) -> Callable:
"""Create a callable that handles Gemini connection with retry logic.
Args:
websocket: The client websocket connection
Returns:
Callable: An async function that establishes and manages the Gemini connection
"""
async def on_backoff(details: backoff._typing.Details) -> None:
await websocket.send_json(
{
"status": f"Model connection error, retrying in {details['wait']} seconds..."
}
)
@backoff.on_exception(
backoff.expo, ConnectionClosedError, max_tries=10, on_backoff=on_backoff
)
async def connect_and_run() -> None:
async with genai_client.aio.live.connect(
model=MODEL_ID, config=live_connect_config
) as session:
await websocket.send_json({"status": "Backend is ready for conversation"})
gemini_session = GeminiSession(
session=session, websocket=websocket, tool_functions=tool_functions
)
logging.info("Starting bidirectional communication")
await asyncio.gather(
gemini_session.receive_from_client(),
gemini_session.receive_from_gemini(),
)
return connect_and_run