"""UTF-8 JSON serializer and transport builder for public Realtime WebSockets.""" from __future__ import annotations import json from typing import Any from fastapi import WebSocket from pipecat.frames.frames import ( Frame, InputTransportMessageFrame, OutputTransportMessageFrame, OutputTransportMessageUrgentFrame, ) from pipecat.serializers.base_serializer import FrameSerializer from services.pipecat.transports import build_serialized_ws_transport class OpenAIRealtimeJSONSerializer(FrameSerializer): async def serialize(self, frame: Frame) -> str | bytes | None: if self.should_ignore_frame(frame): return None if isinstance( frame, (OutputTransportMessageFrame, OutputTransportMessageUrgentFrame), ) and isinstance(frame.message, dict): return json.dumps(frame.message, ensure_ascii=False, separators=(",", ":")) return None async def deserialize(self, data: str | bytes) -> Frame | None: if isinstance(data, bytes): message: Any = { "type": "_invalid_binary_frame", "event_id": None, } else: try: message = json.loads(data) except json.JSONDecodeError: message = {"type": "_invalid_json", "event_id": None} return InputTransportMessageFrame(message=message) def build_openai_websocket_transport(websocket: WebSocket): return build_serialized_ws_transport( websocket, serializer=OpenAIRealtimeJSONSerializer(), sample_rate=24000, )