Files
pdtv26/box/ask-client.js
T
dckc 190600a696 feat(box): the box's NixOS system as a module, and lib.mkBox from a few names
The box within reach of the couch: a Raspberry Pi 3B with the AIY Voice
HAT, from the first household's configuration, made generic.

- box/body: the button service, in Python: press and talk, turns by
  voice, a press interrupts; the look signal
- box/*.js: the ir and look capabilities, behind gates, for the brain;
  the ask client, the box's side of a conversation
- box/module.nix: the services, each confined to the devices and
  addresses it needs; the private network as an option (a tailnet by
  default); the Pico found by its USB IDs, as /dev/pdtv26-ir
- box/voicehat.nix: the Voice HAT's device tree overlay and ALSA setup
- lib.mkBox: a box's whole system from its name, the home server's,
  the Wi-Fi network's, and who looks after it
- checks: box-js, box-py (doctests and pyflakes), and box-system (an
  example box's system evaluates)

Generated-by: Claude Opus 5.5
2026-12-02 18:57:03 -06:00

59 lines
2.3 KiB
JavaScript
Executable File

#!/usr/bin/env node
/**
* @file ask-client: the box's side of a conversation with the brain, through its ask capability.
* usage: ask-client.js URL CONVERSATION < question.txt
* prints JSON lines: {"say": "..."} for each thing the brain says meanwhile,
* then {"reply": "...", "end": false}; end is true when the brain ends the conversation.
* usage: ask-client.js URL CONVERSATION end
* tells the brain the conversation is over.
* The key is in $CREDENTIALS_DIRECTORY/ask-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 { text } from 'node:stream/consumers';
/**
* Ask the brain at URL the question on stdin, or tell it the conversation is over.
*
* @param {object} [io] what it reaches; the process's own, by default
* @param {string[]} [io.argv] URL CONVERSATION [end]
* @param {Record<string, string | undefined>} [io.env] for CREDENTIALS_DIRECTORY
* @param {typeof readFile} [io.read] reads the key
* @param {AsyncIterable<Uint8Array>} [io.stdin] the question
* @param {{ write(s: string): unknown }} [io.stdout] the answer goes here, as JSON lines
* @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, conversation, verb] = argv;
const key = (await read(`${env.CREDENTIALS_DIRECTORY}/ask-key`, 'utf8')).trim();
/** @param {object} obj */
const line = (obj) => stdout.write(`${JSON.stringify(obj)}\n`);
const gate = connect(url);
try {
const asker = gate.open(key);
if (verb === 'end') {
await asker.end(conversation);
return;
}
const question = (await text(stdin)).trim();
// Cap'n Web passes the function by reference: the brain calls it back over this session.
const { text: reply, end } = await asker.ask(question, conversation, (/** @type {string} */ said) => { line({ say: said }); });
line({ reply, end });
} finally {
gate[Symbol.dispose]();
}
};
main().catch((err) => {
console.error(`ask-client: ${err.message ?? err}`);
process.exitCode = 1;
});