Translate pipeline state changes into the old websocket control protocol.
| 86 | |
| 87 | |
| 88 | class RealtimeOutputControlProcessor(FrameProcessor): |
| 89 | """Translate pipeline state changes into the old websocket control protocol.""" |
| 90 | |
| 91 | def __init__(self): |
| 92 | super().__init__() |
| 93 | self._response_started = False |
| 94 | |
| 95 | async def process_frame(self, frame: Frame, direction: FrameDirection): |
| 96 | await super().process_frame(frame, direction) |
| 97 | |
| 98 | if direction is FrameDirection.DOWNSTREAM: |
| 99 | if isinstance(frame, (UserStoppedSpeakingFrame, VADUserStoppedSpeakingFrame)): |
| 100 | await self.push_frame( |
| 101 | OutputTransportMessageFrame(message={"type": "server", "msg": "AUDIO.COMMITTED"}), |
| 102 | direction, |
| 103 | ) |
| 104 | elif isinstance(frame, OutputAudioRawFrame) and not self._response_started: |
| 105 | self._response_started = True |
| 106 | logger.debug("Sending RESPONSE.CREATED before first audio packet") |
| 107 | await self.push_frame(STTMuteFrame(mute=True), direction) |
| 108 | await self.push_frame( |
| 109 | OutputTransportMessageFrame(message={"type": "server", "msg": "RESPONSE.CREATED"}), |
| 110 | direction, |
| 111 | ) |
| 112 | elif isinstance(frame, (TTSStoppedFrame, BotStoppedSpeakingFrame)): |
| 113 | self._response_started = False |
| 114 | logger.debug("Sending RESPONSE.COMPLETE after TTS stop") |
| 115 | await self.push_frame(STTMuteFrame(mute=False), direction) |
| 116 | await self.push_frame(frame, direction) |
| 117 | await self.push_frame( |
| 118 | OutputTransportMessageFrame(message={"type": "server", "msg": "RESPONSE.COMPLETE"}), |
| 119 | direction, |
| 120 | ) |
| 121 | return |
| 122 | elif isinstance(frame, ErrorFrame): |
| 123 | self._response_started = False |
| 124 | await self.push_frame(STTMuteFrame(mute=False), direction) |
| 125 | await self.push_frame( |
| 126 | OutputTransportMessageFrame(message={"type": "server", "msg": "RESPONSE.ERROR"}), |
| 127 | direction, |
| 128 | ) |
| 129 | |
| 130 | await self.push_frame(frame, direction) |
| 131 | |
| 132 | |
| 133 | def create_esp32_auth_message() -> dict: |