Skip to content

The agent that grows with you

fix(desktop): share the native gateway socket rules with voice

unsupportedpastels62a9e0bbcc9a
diff --git a/apps/desktop/src/lib/voice-stream.test.ts b/apps/desktop/src/api/canonical-gateway-socket.test.ts
similarity index 63%
rename from apps/desktop/src/lib/voice-stream.test.ts
rename to apps/desktop/src/api/canonical-gateway-socket.test.ts
index 30b3ba35dc8..d6177cdcf64 100644
--- a/apps/desktop/src/lib/voice-stream.test.ts
+++ b/apps/desktop/src/api/canonical-gateway-socket.test.ts
@@ -1,10 +1,10 @@
import { afterEach, describe, expect, it, vi } from 'vitest'
-import { openVoiceSocket } from './voice-stream'
+import { openGatewaySocket } from './canonical-protocol'
-// A local gateway admits its voice sockets on the /api/ws dial's grant, in the gateway
-// subprotocol only (RT-71, RT-80); a remote route keeps its own query credential.
-describe('openVoiceSocket', () => {
+// Chat and the voice sockets beside it (RT-71, RT-80) share one dial rule: a local gateway's
+// ticket rides only in the gateway subprotocol; a remote route keeps its own query credential.
+describe('openGatewaySocket', () => {
const dials: unknown[][] = []
afterEach(() => {
@@ -16,7 +16,7 @@ describe('openVoiceSocket', () => {
vi.stubGlobal('WebSocket', function (...args: unknown[]) {
dials.push(args)
})
- openVoiceSocket(url)
+ openGatewaySocket(url)
return dials[0]
}
@@ -28,6 +28,11 @@ describe('openVoiceSocket', () => {
])
})
+ it('refuses a local dial without a ticket', () => {
+ expect(() => open('ws://127.0.0.1:5151/api/audio/transcribe-stream?native_dial=n1')).toThrow(/fresh private ticket/)
+ expect(dials).toEqual([])
+ })
+
it('leaves a remote credential where the remote gateway reads it', () => {
expect(open('wss://gateway.example/api/audio/transcribe-stream?ticket=fresh')).toEqual([
'wss://gateway.example/api/audio/transcribe-stream?ticket=fresh'
diff --git a/apps/desktop/src/api/canonical-protocol.ts b/apps/desktop/src/api/canonical-protocol.ts
index 8fc55915e53..0a16a701035 100644
--- a/apps/desktop/src/api/canonical-protocol.ts
+++ b/apps/desktop/src/api/canonical-protocol.ts
@@ -3,6 +3,20 @@
export const CANONICAL_GATEWAY_PROTOCOL = 'hermes-gateway-v1'
+/** Dial `/api/ws` or a sibling stream beside it. A local gateway's one-use ticket leaves the URL
+ * for the gateway subprotocol; a remote route keeps its own query credential. */
+export function openGatewaySocket(url: string): WebSocket {
+ const parsed = new URL(url)
+
+ if (!parsed.searchParams.has('native_dial')) {return new WebSocket(url)}
+ const ticket = parsed.searchParams.get('ticket')
+
+ if (!ticket) {throw new Error('Native gateway requires a fresh private ticket')}
+ parsed.searchParams.delete('ticket')
+
+ return new WebSocket(parsed.toString(), [CANONICAL_GATEWAY_PROTOCOL, `hermes-gateway-ticket.${ticket}`])
+}
+
export const canonicalProfile = (profile: unknown): string =>
typeof profile === 'string' && profile ? profile : 'default'
diff --git a/apps/desktop/src/api/client.ts b/apps/desktop/src/api/client.ts
index 68c5a3e1b74..28cd1562feb 100644
--- a/apps/desktop/src/api/client.ts
+++ b/apps/desktop/src/api/client.ts
@@ -9,7 +9,7 @@ import { map, type MapStore } from 'nanostores'
import type { HermesApiRequest } from '@/global'
-import { CANONICAL_GATEWAY_PROTOCOL, CanonicalDesktopProtocol, canonicalProfile, canonicalPromptParams, canonicalSessionKey, recordCanonicalOwner, splitRewindSubmit } from './canonical-protocol'
+import { CanonicalDesktopProtocol, canonicalProfile, canonicalPromptParams, canonicalSessionKey, openGatewaySocket, recordCanonicalOwner, splitRewindSubmit } from './canonical-protocol'
// Desktop startup fires a burst of read-only data calls (config, profiles,
// model info/options, cron) the moment the backend passes readiness. On a
@@ -528,17 +528,7 @@ export class HermesGateway extends JsonRpcGatewayClient {
onUnhandledRequest: request =>
console.warn(`[gateway] Hermes Desktop has no server-request registry for ${request.method} (${request.id})`),
requestTimeoutMs: DEFAULT_GATEWAY_REQUEST_TIMEOUT_MS,
- socketFactory: url => {
- const parsed = new URL(url)
-
- if (!parsed.searchParams.has('native_dial')) {return new WebSocket(url)}
- const ticket = parsed.searchParams.get('ticket')
-
- if (!ticket) {throw new Error('Native gateway requires a fresh private ticket')}
- parsed.searchParams.delete('ticket')
-
- return new WebSocket(parsed.toString(), [CANONICAL_GATEWAY_PROTOCOL, `hermes-gateway-ticket.${ticket}`])
- }
+ socketFactory: openGatewaySocket
})
this.onEvent(event => {
if (!this.canonical) { return }
diff --git a/apps/desktop/src/lib/voice-playback.ts b/apps/desktop/src/lib/voice-playback.ts
index a8a94b6401b..eea3b501b93 100644
--- a/apps/desktop/src/lib/voice-playback.ts
+++ b/apps/desktop/src/lib/voice-playback.ts
@@ -1,9 +1,9 @@
import { resolveGatewayWsUrl } from '@hermes/shared'
+import { openGatewaySocket } from '@/api/canonical-protocol'
import type { OwnerScope } from '@/api/client'
import { getApiRequestConnection, getApiRequestProfile, speakText } from '@/hermes'
import { directTtsConfig, type DirectTtsConfig, synthesizeSpeechClientDirect } from '@/lib/voice-client-direct'
-import { openVoiceSocket } from '@/lib/voice-stream'
import { RECONNECT_ATTEMPT_TIMEOUT_MS, withTimeout } from '@/lib/with-timeout'
import {
$voicePlayback,
@@ -390,7 +390,7 @@ function openClientDirectSpeechSession(tts: DirectTtsConfig, options: VoicePlayb
* stream (ChatGPT-style) with no per-sentence connection or synthesis gaps.
*/
function openSpeechStream(wsUrl: string, options: VoicePlaybackOptions): SpeechStreamSession {
- const ws = openVoiceSocket(wsUrl)
+ const ws = openGatewaySocket(wsUrl)
ws.binaryType = 'arraybuffer'
let context: AudioContext | null = null
diff --git a/apps/desktop/src/lib/voice-stream.ts b/apps/desktop/src/lib/voice-stream.ts
index 83e2d0fb601..f14d358611a 100644
--- a/apps/desktop/src/lib/voice-stream.ts
+++ b/apps/desktop/src/lib/voice-stream.ts
@@ -1,22 +1,7 @@
-import { CANONICAL_GATEWAY_PROTOCOL } from '@/api/canonical-protocol'
+import { openGatewaySocket } from '@/api/canonical-protocol'
import type { ResolvedOwner } from '@/hermes'
import { resolveSiblingWsUrl } from '@/lib/sibling-ws-url'
-/** Dial a voice socket beside `/api/ws`. A local gateway's one-use ticket leaves the URL for the
- * gateway subprotocol, as on the `/api/ws` dial itself (api/client.ts socketFactory). */
-export function openVoiceSocket(url: string): WebSocket {
- const parsed = new URL(url)
- const ticket = parsed.searchParams.get('ticket')
-
- if (!parsed.searchParams.has('native_dial') || !ticket) {
- return new WebSocket(url)
- }
-
- parsed.searchParams.delete('ticket')
-
- return new WebSocket(parsed.toString(), [CANONICAL_GATEWAY_PROTOCOL, `hermes-gateway-ticket.${ticket}`])
-}
-
/**
* Live dictation STT: mic PCM streams to the host's `/api/audio/transcribe-stream` WebSocket
* WHILE the user speaks; partial text arrives as it is recognized and the transcript lands right
@@ -59,7 +44,7 @@ export async function openDictationStream(
url.searchParams.set('profile', owner.profile)
}
- const ws = openVoiceSocket(url.toString())
+ const ws = openGatewaySocket(url.toString())
ws.binaryType = 'arraybuffer'
let settled = false
diff --git a/tests/hermes_cli/test_audio_stream_native_ws.py b/tests/hermes_cli/test_audio_stream_native_ws.py
index 474ae5df248..a1ea8c41ae6 100644
--- a/tests/hermes_cli/test_audio_stream_native_ws.py
+++ b/tests/hermes_cli/test_audio_stream_native_ws.py
@@ -25,30 +25,67 @@ def _dial(home, purpose="interactive"):
@pytest.fixture
-def stt_homes(monkeypatch):
+def scopes(daemon, monkeypatch):
+ """Fake live providers recording (stage, home, profile secret) at each provider stage."""
+ from agent.secret_scope import get_secret
from hermes_constants import get_hermes_home
- from tools import transcription_streaming
- homes = []
+ from tools import transcription_streaming, tts_streaming
+ (daemon.beta / ".env").write_text("VOICE_KEY=beta\n")
+ seen = []
- def open_streaming_session(**_): # no live STT: the socket answers an error frame and closes
- homes.append(get_hermes_home())
+ def record(stage):
+ seen.append((stage, get_hermes_home(), get_secret("VOICE_KEY")))
+
+ class Session:
+ set_input_rate = push_audio = cancel = lambda self, *_: None
+
+ def finalize(self):
+ record("stt.finalize")
+ return {"success": True, "transcript": "hi", "provider": "fake"}
+
+ class Streamer:
+ sample_rate, channels = 24000, 1
+
+ def stream(self, text):
+ record("tts.synthesize")
+ yield b"\0\0"
+
+ def open_streaming_session(**_):
+ record("stt.open")
+ return Session()
+
+ def resolve_streaming_provider(*_):
+ record("tts.resolve")
+ return Streamer()
monkeypatch.setattr(transcription_streaming, "open_streaming_session", open_streaming_session)
- return homes
+ monkeypatch.setattr(tts_streaming, "resolve_streaming_provider", resolve_streaming_provider)
+ return seen
[email protected]("path", [SPEAK, TRANSCRIBE])
-def test_desktop_grant_opens_the_voice_sockets(daemon, stt_homes, path):
- with daemon.client.websocket_connect(path, subprotocols=_dial(daemon.beta)) as ws:
+def test_spoken_reply_runs_in_the_grant_profile(daemon, scopes):
+ with daemon.client.websocket_connect(SPEAK, subprotocols=_dial(daemon.beta)) as ws:
assert ws.accepted_subprotocol == "hermes-gateway-v1" # the browser fails the handshake otherwise
- if path == TRANSCRIBE:
- assert ws.receive_json()["type"] == "error"
- if path == TRANSCRIBE:
- assert stt_homes == [daemon.beta] # the grant's profile, with no ?profile= selector
+ ws.send_json({"text": "Hello there.", "done": True})
+ assert ws.receive_json()["type"] == "start"
+ assert ws.receive_bytes() == b"\0\0"
+ assert ws.receive_json() == {"type": "end"}
+ # the grant's profile, with no ?profile= selector
+ assert scopes == [(stage, daemon.beta, "beta") for stage in ("tts.resolve", "tts.synthesize")]
+
+
+def test_dictation_runs_in_the_grant_profile(daemon, scopes):
+ with daemon.client.websocket_connect(TRANSCRIBE, subprotocols=_dial(daemon.beta)) as ws:
+ assert ws.accepted_subprotocol == "hermes-gateway-v1"
+ ws.send_json({"sample_rate": 16000})
+ ws.send_bytes(b"\0\0")
+ ws.send_json({"eos": True})
+ assert ws.receive_json() == {"type": "final", "transcript": "hi", "provider": "fake"}
+ assert scopes == [(stage, daemon.beta, "beta") for stage in ("stt.open", "stt.finalize")]
@pytest.mark.parametrize("case", ["no_credential", "other_profile", "browser_origin", "wrong_purpose"])
-def test_other_upgrades_are_still_refused(daemon, stt_homes, case):
+def test_other_upgrades_are_still_refused(daemon, scopes, case):
(daemon.launch / "profiles" / "gamma").mkdir()
path = TRANSCRIBE
kwargs: dict = {"subprotocols": _dial(daemon.beta)}
@@ -63,4 +100,4 @@ def test_other_upgrades_are_still_refused(daemon, stt_homes, case):
with pytest.raises(WebSocketDisconnect) as refused, daemon.client.websocket_connect(path, **kwargs):
pass
assert refused.value.code == 4401
- assert stt_homes == []
+ assert scopes == []