The speech model now lives in a worker thread (whisperWorker.mjs), so loading and transcribing no longer block the bridge's HTTP server or its Hermes polling. Measured longest event-loop pause during transcription: 387 ms before, 3 ms after. A dead worker is replaced on the next request; closing the bridge terminates it. Polling: bridge to Hermes every 150 ms (was 500). The app polls the bridge every 200 ms for the first 8 s of a turn, 400 ms until 30 s, then 1 s (was a fixed 700 ms). Tests: 131 unit/integration, 7 real-speech, 9 real-Hermes, 6 simulator, all passing. Effect on a phone or a droplet is not measured. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
91 lines
2.8 KiB
TypeScript
91 lines
2.8 KiB
TypeScript
import type { FastifyInstance } from 'fastify';
|
|
import { pino, type Logger } from 'pino';
|
|
import type { BridgeConfig } from './config.ts';
|
|
import { openDb, type Db } from './db.ts';
|
|
import { Devices } from './devices.ts';
|
|
import type { HermesAdapter } from './hermes/adapter.ts';
|
|
import { HermesRunsAdapter } from './hermes/runsAdapter.ts';
|
|
import { buildServer } from './server.ts';
|
|
import { NoSttAdapter, type SttAdapter } from './stt/adapter.ts';
|
|
import { BuiltinWhisperStt } from './stt/builtinWhisper.ts';
|
|
import { OpenAiCompatibleStt } from './stt/openaiCompatible.ts';
|
|
import { TurnEngine, type EngineLog, type EngineTiming } from './turns.ts';
|
|
|
|
export interface Bridge {
|
|
app: FastifyInstance;
|
|
stt: SttAdapter;
|
|
db: Db;
|
|
devices: Devices;
|
|
engine: TurnEngine;
|
|
close(): Promise<void>;
|
|
}
|
|
|
|
/** Pino redaction: belt and braces, since we never pass these fields to the logger anyway. */
|
|
export const LOG_REDACT = [
|
|
'req.headers.authorization',
|
|
'headers.authorization',
|
|
'token',
|
|
'code',
|
|
'text',
|
|
'input_text',
|
|
'answer_text',
|
|
'pending_body',
|
|
'body',
|
|
];
|
|
|
|
export function sttFromConfig(config: BridgeConfig): SttAdapter {
|
|
if (config.stt.provider === 'openai-compatible') {
|
|
return new OpenAiCompatibleStt({ baseUrl: config.stt.baseUrl, apiKey: config.stt.apiKey, model: config.stt.model });
|
|
}
|
|
if (config.stt.provider === 'builtin') {
|
|
return new BuiltinWhisperStt({ model: config.stt.model, dtype: config.stt.dtype, cacheDir: config.stt.cacheDir });
|
|
}
|
|
return new NoSttAdapter();
|
|
}
|
|
|
|
export async function createBridge(
|
|
config: BridgeConfig,
|
|
overrides: { hermes?: HermesAdapter; stt?: SttAdapter; timing?: EngineTiming; logger?: object | boolean } = {},
|
|
): Promise<Bridge> {
|
|
const db = openDb(config.dbPath);
|
|
const hermes =
|
|
overrides.hermes ??
|
|
new HermesRunsAdapter({
|
|
baseUrl: config.hermes.baseUrl,
|
|
apiKey: config.hermes.apiKey,
|
|
requestTimeoutMs: config.hermes.requestTimeoutMs,
|
|
});
|
|
const stt = overrides.stt ?? sttFromConfig(config);
|
|
const devices = new Devices(db, config);
|
|
|
|
const given = overrides.logger;
|
|
const logger: Logger =
|
|
given && typeof given === 'object'
|
|
? typeof (given as Logger).child === 'function'
|
|
? (given as Logger)
|
|
: pino(given as object)
|
|
: pino({ level: 'silent' });
|
|
const engine = new TurnEngine({ db, config, hermes, stt, log: logger as unknown as EngineLog, timing: overrides.timing });
|
|
const app = await buildServer({ config, devices, hermes, stt, engine, logger });
|
|
|
|
engine.recover();
|
|
engine.purgeExpired();
|
|
const purge = setInterval(() => engine.purgeExpired(), 60_000);
|
|
purge.unref();
|
|
|
|
return {
|
|
app,
|
|
stt,
|
|
db,
|
|
devices,
|
|
engine,
|
|
async close() {
|
|
clearInterval(purge);
|
|
await app.close();
|
|
await engine.shutdown();
|
|
await stt.close?.();
|
|
db.close();
|
|
},
|
|
};
|
|
}
|