The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(web): speak-stream start frame carries the rate learned from the endpoint
teknium12090217284ea
diff --git a/hermes_cli/web_routers/audio.py b/hermes_cli/web_routers/audio.pyindex 09a9a4c4699..776eac11082 100644--- a/hermes_cli/web_routers/audio.py+++ b/hermes_cli/web_routers/audio.py@@ -382,7 +382,8 @@ async def speak_stream_ws(ws: "WebSocket") -> None:client → ``{"text": "..."}`` frames (incremental; may combine with done),``{"done": true}`` when the reply is complete,``{"stop": true}`` or disconnect = barge-in- server → ``{"type": "start", "sample_rate": N, "channels": 1}``,+ server → ``{"type": "start", "sample_rate": N, "channels": 1}`` (sent+ with the first PCM frame, once the provider's rate is final),binary PCM frames, then ``{"type": "end"}``server → ``{"type": "fallback"}`` when the configured provider has nochunked API — the client uses the POST endpoint instead.@@ -422,9 +423,20 @@ async def speak_stream_ws(ws: "WebSocket") -> None:await ws.close()return- await ws.send_json(- {"type": "start", "sample_rate": streamer.sample_rate, "channels": streamer.channels}- )+ # The start frame is deferred until the first PCM chunk (or end-of-speech):+ # the OpenAI-compatible streamer only learns the endpoint's real rate from+ # the response headers inside stream(), and the client opens its+ # AudioContext at whatever rate the start frame carries.+ start_sent = False++ async def _send_start():+ nonlocal start_sent+ if start_sent:+ return+ start_sent = True+ await ws.send_json(+ {"type": "start", "sample_rate": streamer.sample_rate, "channels": streamer.channels}+ )stop = threading.Event()text_q: queue.Queue = queue.Queue() # str deltas; None = end-of-text@@ -511,8 +523,10 @@ async def speak_stream_ws(ws: "WebSocket") -> None:chunk = await chunks.get()if chunk is None:break+ await _send_start()await ws.send_bytes(chunk)if not stop.is_set():+ await _send_start()await ws.send_json({"type": "end"})except (WebSocketDisconnect, RuntimeError):passdiff --git a/tests/hermes_cli/test_web_server_speak_stream.py b/tests/hermes_cli/test_web_server_speak_stream.pyindex 33a9e079527..af2ef2e17b5 100644--- a/tests/hermes_cli/test_web_server_speak_stream.py+++ b/tests/hermes_cli/test_web_server_speak_stream.py@@ -57,8 +57,24 @@ def _patch_provider(monkeypatch, streamer, cap=4000):monkeypatch.setattr("tools.tts_tool._resolve_max_text_length", lambda provider, cfg: cap)+class _RateLearningStreamer(_FakeStreamer):+ """Mimics the OpenAI-compatible streamer: the true PCM rate is only known once+ the endpoint's response headers arrive inside stream()."""++ def stream(self, text):+ self.sample_rate = 44100+ yield from super().stream(text)+def test_start_frame_carries_rate_learned_during_first_stream(stream_client, monkeypatch):+ streamer = _RateLearningStreamer([b"\x01\x02"])+ _patch_provider(monkeypatch, streamer)++ with stream_client.websocket_connect(_url()) as conn:+ conn.send_text(json.dumps({"text": "Hello there.", "done": True}))+ assert conn.receive_json() == {"type": "start", "sample_rate": 44100, "channels": 1}+ assert conn.receive_bytes() == b"\x01\x02"+ assert conn.receive_json() == {"type": "end"}def test_streams_pcm_frames_then_end(stream_client, monkeypatch):@@ -66,10 +82,10 @@ def test_streams_pcm_frames_then_end(stream_client, monkeypatch):_patch_provider(monkeypatch, streamer)with stream_client.websocket_connect(_url()) as conn:+ conn.send_text(json.dumps({"text": "Hello there.", "done": True}))start = conn.receive_json()assert start == {"type": "start", "sample_rate": 24000, "channels": 1}- conn.send_text(json.dumps({"text": "Hello there.", "done": True}))assert conn.receive_bytes() == b"\x01\x02\x03\x04"assert conn.receive_bytes() == b"\x05\x06"assert conn.receive_json() == {"type": "end"}@@ -88,12 +104,12 @@ def test_long_text_is_split_across_provider_requests(stream_client, monkeypatch)_patch_provider(monkeypatch, streamer, cap=24)with stream_client.websocket_connect(_url()) as conn:- assert conn.receive_json()["type"] == "start"conn.send_text(json.dumps({"text": "First sentence here. Second sentence here. Third one.", "done": True}))+ assert conn.receive_json()["type"] == "start"# One PCM frame per split piece, then end.frames = 0while True: