feat(speech): the speech capability over Cap'n Web, behind a gate
check / check (push) Canceled after 0s
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:
1 parent
4a42c896db
commit
2e7156dc36
20 files changed
+621
No files matched your search
Executable
+46
@@ -0,0 +1,46 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* @file hear-client: send one clip to the speech capability and print the transcript.
|
||||
* usage: hear-client.js URL < clip.wav
|
||||
* The key is in $CREDENTIALS_DIRECTORY/speech-key (systemd LoadCredential), not argv, where ps would show it.
|
||||
*/
|
||||
import { newWebSocketRpcSession } from 'capnweb';
|
||||
import { readFile } from 'node:fs/promises';
|
||||
import process from 'node:process';
|
||||
import { buffer } from 'node:stream/consumers';
|
||||
|
||||
/**
|
||||
* Transcribe the clip on stdin, by way of the speech capability at URL.
|
||||
*
|
||||
* @param {object} [io] what it reaches; the process's own, by default
|
||||
* @param {string[]} [io.argv] [URL]
|
||||
* @param {Record<string, string | undefined>} [io.env] for CREDENTIALS_DIRECTORY
|
||||
* @param {typeof readFile} [io.read] reads the key
|
||||
* @param {AsyncIterable<Uint8Array>} [io.stdin] the clip, a WAV file
|
||||
* @param {{ write(s: string): unknown }} [io.stdout] the transcript goes here
|
||||
* @param {typeof newWebSocketRpcSession} [io.connect] opens a session with the gate at URL
|
||||
*/
|
||||
const main = async ({
|
||||
argv = process.argv.slice(2),
|
||||
env = process.env,
|
||||
read = readFile,
|
||||
stdin = process.stdin,
|
||||
stdout = process.stdout,
|
||||
connect = newWebSocketRpcSession,
|
||||
} = {}) => {
|
||||
const [url] = argv;
|
||||
const key = (await read(`${env.CREDENTIALS_DIRECTORY}/speech-key`, 'utf8')).trim();
|
||||
const clip = new Uint8Array(await buffer(stdin));
|
||||
const gate = connect(url);
|
||||
try {
|
||||
// Pipelined: open and transcribe in one round trip.
|
||||
stdout.write(`${await gate.open(key).transcribe(clip)}\n`);
|
||||
} finally {
|
||||
gate[Symbol.dispose]();
|
||||
}
|
||||
};
|
||||
|
||||
main().catch((err) => {
|
||||
console.error(`hear-client: ${err.message ?? err}`);
|
||||
process.exitCode = 1;
|
||||
});
|
||||
Reference in new issue
Block a user