feat(speech): the speech capability over Cap'n Web, behind a gate
check / check (push) Canceled after 0s

The agent's ears and voice: a server on the home server transcribes
a clip (sox, whisper.cpp) and speaks text (piper), one job at a time.
A gate hands the capability to whoever presents its key; the key comes
from systemd's credentials, not argv.

make test runs the unit tests in Nix's sandbox; make smoke starts a
real server on localhost, speaks a phrase, and hears it back.

Generated-by: Claude Opus 5.5
This commit is contained in:
dckc committed 2026-12-02 15:52:23 -06:00
1 parent 646dfee4c6
commit 429bafe9c0
20 files changed
+621

No files matched your search

+62
View File
@@ -0,0 +1,62 @@
#!/usr/bin/env node
/**
* @file speech-server: the speech capability over Cap'n Web, behind a gate.
* usage: server.js HOST PORT HEAR_SH MODEL SPEAK_SH VOICE [PIPER_OPTION...]
* The key is in $CREDENTIALS_DIRECTORY/speech-key (systemd LoadCredential), not argv, where ps would show it.
* HEAR_SH MODEL reads a WAV on stdin and prints whisper's transcript (hear.sh).
* SPEAK_SH VOICE reads text on stdin and writes piper's WAV of it (speak.sh), with any PIPER_OPTIONs.
*/
import { newWebSocketRpcSession } from 'capnweb';
import { spawn as spawnChild } from 'node:child_process';
import { readFile } from 'node:fs/promises';
import process from 'node:process';
import { WebSocketServer } from 'ws';
import { Gate } from './gate.js';
import { makeCommand, timed } from './run.js';
import { Speech, MAX_CLIP_BYTES } from './speech.js';
/**
* Serve the speech capability, behind a gate, at HOST:PORT.
*
* @param {object} [io] what it reaches; the process's own, by default
* @param {string[]} [io.argv] HOST PORT HEAR_SH MODEL SPEAK_SH VOICE [PIPER_OPTION...]
* @param {Record<string, string | undefined>} [io.env] for CREDENTIALS_DIRECTORY
* @param {typeof readFile} [io.read] reads the key
* @param {typeof spawnChild} [io.spawn] runs HEAR_SH and SPEAK_SH, and nothing else
* @param {() => number} [io.now] the clock, for timing jobs
* @param {(options: import('ws').ServerOptions) => WebSocketServer} [io.listen]
* @param {typeof newWebSocketRpcSession} [io.serve] serves the gate on one connection
*/
const main = async ({
argv = process.argv.slice(2),
env = process.env,
read = readFile,
spawn = spawnChild,
now = Date.now,
listen = (options) => new WebSocketServer(options),
serve = newWebSocketRpcSession,
} = {}) => {
const [host, port, hearSh, model, speakSh, voice, ...piper] = argv;
const key = (await read(`${env.CREDENTIALS_DIRECTORY}/speech-key`, 'utf8')).trim();
const hear = makeCommand(spawn, hearSh, [model]);
const say = makeCommand(spawn, speakSh, [voice, ...piper]);
const speech = new Speech(
(clip) => timed(now, `heard ${clip.length} bytes`,
async () => (await hear(clip)).toString().trim(), (text) => text),
(text) => timed(now, `spoke ${text.length} chars`,
async () => new Uint8Array(await say(text)), (wav) => `${wav.length} bytes of WAV`),
);
const gate = new Gate(key, speech);
// A clip travels as base64 inside JSON: 4/3 of its size, plus a little.
const wss = listen({ host, port: Number(port), maxPayload: Math.ceil(MAX_CLIP_BYTES * 1.4) });
wss.on('connection', (ws, req) => {
console.log(`connection from ${req.socket.remoteAddress}`);
serve(ws, gate);
});
console.log(`speech-server: listening on ${host}:${port}`);
};
main().catch((err) => {
console.error(err);
process.exitCode = 1;
});