Build speech-to-text into the bridge; add droplet deployment examples

STT_PROVIDER now defaults to "builtin": Whisper (onnx-community/whisper-base,
q8) runs in the bridge process on the CPU via @huggingface/transformers, so
no provider account is needed and recordings stay on the bridge host. Silent
captures are detected by level before the model runs. The external
OpenAI-compatible adapter remains available.

Tests: 130 unit/integration; 4 scenarios with the real model on a real
speech recording (npm run test:real-stt). Not tested on audio from the
glasses or on a droplet.

deploy/ and docs/deploy-droplet.md are examples that have not been run.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
Dan
2026-11-18 18:31:30 -08:00
co-authored by Claude Fable 5.2
parent 3c9e094977
commit 6d74f9e121
29 changed files with 1602 additions and 42 deletions
+1 -1
View File
@@ -68,7 +68,7 @@ export async function createHarness(
HERMEVEN_ALLOWED_ORIGINS: 'https://app.example',
HERMES_REQUEST_TIMEOUT_MS: '300',
...(opts.stt === false
? {}
? { STT_PROVIDER: 'none' }
: { STT_PROVIDER: 'openai-compatible', STT_BASE_URL: stt.url, STT_API_KEY: stt.apiKey, STT_MODEL: 'fake-whisper' }),
...opts.env,
};
+1
View File
@@ -126,6 +126,7 @@ beforeAll(async () => {
HERMES_BASE_URL: hermesUrl,
HERMES_API_KEY: API_KEY,
HERMES_POLL_INTERVAL_MS: '100',
STT_PROVIDER: 'none',
HERMEVEN_AGENT_DEADLINE_SECONDS: '60',
HERMEVEN_CANCEL_GRACE_SECONDS: '30',
});
+9
View File
@@ -0,0 +1,9 @@
# Speech fixture
`jfk.wav`: 11 seconds of John F. Kennedy's 1961 inaugural address, 16 kHz mono 16-bit PCM.
A work of the United States government (public domain), taken from the sample set distributed
with whisper.cpp (`samples/jfk.wav`). Expected words: "ask not what your country can do for you,
ask what you can do for your country".
It is a clean studio-quality recording. It says nothing about how well the glasses microphone,
a noisy street, or any particular accent will transcribe.
Binary file not shown.
+100
View File
@@ -0,0 +1,100 @@
import { readFileSync } from 'node:fs';
import { dirname, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
import { BuiltinWhisperStt } from '../../services/bridge/src/stt/builtinWhisper.ts';
import { createHarness, type Harness } from '../integration/harness.ts';
/**
* REAL SPEECH-TO-TEXT. The bridge's built-in Whisper model, really loaded and really run on
* this machine's CPU, transcribing a real human speech recording that is uploaded through the
* bridge's HTTP API exactly as the glasses app uploads a capture.
*
* Still fake: Hermes (tests/fakes/fakeHermes.ts) and the glasses. The recording is a clean
* archive clip, not audio from a G2 microphone.
*/
const here = dirname(fileURLToPath(import.meta.url));
const speech = readFileSync(resolve(here, 'fixtures/jfk.wav')).subarray(44); // strip the WAV header: raw PCM
let h: Harness;
let token: string;
let conversation: string;
beforeAll(async () => {
h = await createHarness({
stt: false,
env: {
STT_PROVIDER: 'builtin',
STT_CACHE_DIR: resolve(here, '.cache'),
HERMEVEN_CAPTURE_MAX_SECONDS: '30',
},
timing: { transcribeDeadlineMs: 120_000, agentDeadlineMs: 30_000 },
});
expect(h.bridge.stt).toBeInstanceOf(BuiltinWhisperStt);
const started = Date.now();
await (h.bridge.stt as BuiltinWhisperStt).warm();
console.info(`[real-stt] model ${h.config.stt.model} (${h.config.stt.dtype}) loaded in ${Date.now() - started} ms; rss ${Math.round(process.memoryUsage().rss / 1e6)} MB`);
token = (await h.pair()).token;
conversation = await h.conversation(token);
});
afterAll(async () => {
await h?.close();
});
describe('built-in Whisper speech-to-text', () => {
it('transcribes real speech uploaded as a capture and sends the words to the agent', async () => {
// Watch the event loop while the model runs: the bridge must stay responsive.
let worstLag = 0;
let last = performance.now();
const probe = setInterval(() => {
const now = performance.now();
worstLag = Math.max(worstLag, now - last - 20);
last = now;
}, 20);
const res = await h.audioTurn(token, conversation, Buffer.from(speech));
expect(res.status).toBe(202);
const health = await h.api('GET', '/healthz');
expect(health.status).toBe(200);
const done = await h.waitTerminal(token, res.body.request_id, 120_000);
clearInterval(probe);
console.info(
`[real-stt] ${(speech.length / 32000).toFixed(1)} s of speech transcribed in ${done.timings.transcribe_ms} ms; ` +
`worst event-loop stall ${Math.round(worstLag)} ms; rss ${Math.round(process.memoryUsage().rss / 1e6)} MB`,
);
console.info(`[real-stt] transcript: ${JSON.stringify(done.input_text)}`);
expect(done.outcome).toBe('succeeded');
const heard = (done.input_text ?? '').toLowerCase().replace(/[^a-z ]/g, '');
expect(heard).toContain('ask not what your country can do for you');
expect(heard).toContain('ask what you can do for your country');
// The agent received exactly the transcript.
expect(h.hermes.lastRun()!.input).toBe(done.input_text);
expect(h.hermes.executions).toBe(1);
});
it('a silent capture is reported as "no speech" and never reaches the agent', async () => {
const before = h.hermes.executions;
const res = await h.audioTurn(token, conversation, Buffer.alloc(3 * 32000));
const done = await h.waitTerminal(token, res.body.request_id, 60_000);
expect(done.outcome).toBe('failed');
expect(done.error?.code).toBe('stt_empty');
expect(h.hermes.executions).toBe(before);
});
it('a very quiet room-noise capture is also treated as no speech', async () => {
const noise = Buffer.alloc(3 * 32000);
for (let i = 0; i < noise.length / 2; i++) noise.writeInt16LE(Math.round((Math.random() - 0.5) * 40), i * 2);
const res = await h.audioTurn(token, conversation, noise);
const done = await h.waitTerminal(token, res.body.request_id, 60_000);
expect(done.error?.code).toBe('stt_empty');
});
it('a second recording is transcribed with the model already warm', async () => {
const res = await h.audioTurn(token, conversation, Buffer.from(speech.subarray(0, 6 * 32000)));
const done = await h.waitTerminal(token, res.body.request_id, 120_000);
console.info(`[real-stt] warm: 6.0 s of speech in ${done.timings.transcribe_ms} ms -> ${JSON.stringify(done.input_text)}`);
expect(done.outcome).toBe('succeeded');
expect((done.input_text ?? '').toLowerCase()).toContain('fellow americans');
});
});
+13
View File
@@ -0,0 +1,13 @@
import { defineConfig } from 'vitest/config';
// Opt-in: runs the real built-in Whisper model on the CPU. The first run downloads the model.
export default defineConfig({
test: {
name: 'real-stt',
root: new URL('../..', import.meta.url).pathname,
include: ['tests/real-stt/**/*.test.ts'],
testTimeout: 180_000,
hookTimeout: 600_000,
fileParallelism: false,
},
});