Answers read aloud: Voice on the menu, ElevenLabs voices per agent via the bridge (0.5.0)

- bridge: GET /v1/speech (which agents have a voice) and GET /v1/turns/{id}/speech (MP3 of the
  caller's own finished answer, markup removed, first 900 characters). ElevenLabs adapter; key
  and voice ids from the environment; per-device budget; last three clips kept in memory.
  speech.done / speech.failed log ids, lengths and durations only.
- bridge: forceCloseConnections. A request the client had abandoned kept shutdown waiting for
  ever (also true of /v1/transcribe as deployed). Regression test for both routes.
- client: 'Voice: off/on' on the opening menu when the bridge offers it and the page can play
  sound; off at every launch. With it on, a finished answer is fetched and played; it stops
  when the answer leaves the screen; the app does not leave the glasses while it plays.
- tests: fake provider, fake loudspeaker (tests/integration/voice.test.ts).
- docs: 0006, security 3g-3i, device plan E, 0.4.1 rollout and Dan's sound check report.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
Dan
2026-11-29 12:11:38 -08:00
co-authored by Claude Fable 5.2
parent d65beeab77
commit 9874e1bb5b
25 changed files with 1199 additions and 17 deletions
+8
View File
@@ -158,6 +158,14 @@ export function mountPhoneUi(root: HTMLElement, controller: Controller, defaults
['Bridge', s.bridgeUrl],
['Agent', s.status ? (s.status.agent_reachable ? 'reachable' : 'not reachable from the bridge') : 'not checked'],
['Speech-to-text', s.status ? (s.status.stt_configured ? 'configured' : 'not configured (typed questions only)') : 'not checked'],
[
'Spoken answers',
!s.voice.offered
? 'not available (the bridge has no voices set up, or this page cannot play sound)'
: s.voice.on
? `on${s.voice.speaking ? ', speaking now' : ''}: answers are sent to the speech provider and played on whatever sound output the phone is using`
: 'off. Switch on with “Voice” on the glasses’ opening menu; it is off again at every launch.',
],
['Credential valid until', when(s.tokenExpiresAt)],
['Pairing valid until', when(s.pairingExpiresAt)],
['Stored on this phone', 'In the Even app’s app storage, which is not an encrypted vault. The credential is revocable and expires.'],
+58
View File
@@ -0,0 +1,58 @@
import type { SpeakerPort } from '../core/controller.ts';
/**
* Plays an answer's audio through the page's sound engine (Web Audio). Where it is heard is
* Android's choice, not ours: whatever output is selected (earbuds, car, or the phone itself).
* The page cannot tell which, so speech is only ever played after the user switched Voice on.
*
* Device evidence (docs/evidence/device/2026-11-29.md): in the Even app on Dan's phone a page
* may play sound without a touch, and did so 20 s after a press. Playing an MP3 this way has
* NOT been tried on the device.
*/
export function createSpeaker(win: Window): SpeakerPort | null {
const w = win as unknown as { AudioContext?: typeof AudioContext; webkitAudioContext?: typeof AudioContext };
const Ctor = w.AudioContext ?? w.webkitAudioContext;
if (!Ctor) return null;
let context: AudioContext | null = null;
let current: { source: AudioBufferSourceNode; done: () => void } | null = null;
const stop = () => {
const playing = current;
current = null;
if (!playing) return;
try {
playing.source.onended = null;
playing.source.stop();
} catch {
// Already stopped.
}
playing.done();
};
return {
async play(audio: Uint8Array): Promise<void> {
stop();
context ??= new Ctor();
const ctx = context;
await Promise.race([ctx.resume(), new Promise((r) => win.setTimeout(r, 800))]);
if (ctx.state !== 'running') throw new Error(`sound is blocked (${ctx.state})`);
// decodeAudioData takes ownership of the buffer it is given: hand it a copy.
const copy = audio.buffer.slice(audio.byteOffset, audio.byteOffset + audio.byteLength) as ArrayBuffer;
const buffer = await ctx.decodeAudioData(copy);
// Stopped, or replaced by a newer answer, while decoding.
if (current) stop();
const source = ctx.createBufferSource();
source.buffer = buffer;
source.connect(ctx.destination);
await new Promise<void>((resolve) => {
current = { source, done: resolve };
source.onended = () => {
if (current?.source === source) current = null;
resolve();
};
source.start();
});
},
stop,
};
}