Refactoring SmallWebRTCRequestHandler.

This commit is contained in:
Filipi Fuchter
2025-09-15 09:58:44 -03:00
parent 95f72f6dce
commit 11d0c3d46d
2 changed files with 2 additions and 18 deletions

View File

@@ -208,8 +208,6 @@ def _setup_webrtc_routes(app: FastAPI, esp32_mode: bool = False, host: str = "lo
@app.post("/api/offer")
async def offer(request: SmallWebRTCRequest, background_tasks: BackgroundTasks):
"""Handle WebRTC offer requests via SmallWebRTCRequestHandler."""
# Create a future to receive the answer from the handler
pending_answer: Future = Future()
# Prepare runner arguments with the callback to run your bot
async def webrtc_connection_callback(connection):
@@ -218,14 +216,10 @@ def _setup_webrtc_routes(app: FastAPI, esp32_mode: bool = False, host: str = "lo
background_tasks.add_task(bot_module.bot, runner_args)
# Delegate handling to SmallWebRTCRequestHandler
await small_webrtc_handler.handle_web_request(
answer = await small_webrtc_handler.handle_web_request(
request=request,
pending_answer=pending_answer,
webrtc_connection_callback=webrtc_connection_callback,
)
# Wait for the handler to resolve the answer
answer = await pending_answer
return answer
@asynccontextmanager

View File

@@ -72,7 +72,6 @@ class SmallWebRTCRequestHandler:
async def handle_web_request(
self,
request: SmallWebRTCRequest,
pending_answer: asyncio.Future,
webrtc_connection_callback: Callable[[Any], Awaitable[None]],
) -> None:
"""Handle a SmallWebRTC request and resolve the pending answer.
@@ -82,13 +81,10 @@ class SmallWebRTCRequestHandler:
- Otherwise, create a new `SmallWebRTCConnection`.
- Invoke the provided callback with the connection.
- Manage ESP32-specific munging if enabled.
- Resolve or reject the `pending_answer` future in the runner arguments.
Args:
request (SmallWebRTCRequest): The incoming WebRTC request, containing
SDP, type, and optionally a `pc_id`.
pending_answer (asyncio.Future): A future that will be resolved with the
SDP answer once the request is processed.
webrtc_connection_callback (Callable[[Any], Awaitable[None]]): An
asynchronous callback function that is invoked with the WebRTC connection.
@@ -136,16 +132,10 @@ class SmallWebRTCRequestHandler:
self._pcs_map[answer["pc_id"]] = pipecat_connection
# Resolve the pending answer future
if not pending_answer.done():
pending_answer.set_result(answer)
return answer
except Exception as e:
logger.error(f"Error processing SmallWebRTC request: {e}")
logger.debug(f"SmallWebRTC request details: {request}")
if not pending_answer.done():
pending_answer.set_exception(e)
raise
async def close(self):