mirror of
https://github.com/Kynde/airc.git
synced 2026-10-20 08:31:38 +02:00
The control bar can type, but a keypress at a time is all that is comfortable in a car — and a prompt for a coding agent is a sentence. The Android app has IME dictation; the car browser has none, so a spoken instruction today means walking back to the laptop. This pairs airc with dais, the local voice daemon on the same machine. The browser sends the pane its viewer is on; dais records, transcribes locally and types the transcript there. No audio crosses the network and the browser needs no microphone permission — the mic that opens is the LAPTOP's. Type only, on purpose: the text lands in the pane's input box and the user submits it with the Enter button already in the bar, so a misheard prompt is readable before an agent acts on it. The ✕ discards instead — dais throws the recording away rather than typing it. `src/dais.js` keeps the two channels apart. Control is one ndjson line over the Unix socket with a 2s timeout, resolving and never rejecting, because a missing daemon is an expected state. Status needs no dais op at all: the socket's existence says the daemon is live, the mic-recording marker says the mic is open, and state.json carries mode/target/muted/dry_run. Those files already exist for statusline use, so the poll behind the button costs no socket traffic; a state.json caught mid-rewrite is parsed in a try/catch, and the two filesystem facts still answer the question the button must not get wrong. $DAIS_SOCKET is deliberately ignored even though dais's own CLI honors it — if airc read the env, a private test daemon exported in a shell could silently re-aim the microphone of a running server. The button reports the state of the pairing rather than failing mysteriously. Not configured: no buttons at all, nothing to explain. Configured but the daemon is down: visible and DISABLED with the reason in the title, because a control that vanishes mid-drive is worse than one that says why it cannot help. Muted or dry-run gets appended to the title, since otherwise the tap looks like it worked and no text ever appears. The poll runs ~1/s while recording, ~4s otherwise, and not at all while the control bar is closed — airc's own no-work-when-nobody-is- watching rule. The browser always sends `toggle` and lets dais resolve it, so a missed poll cannot invert the latch. A mic tap drops auto mode, mirroring "the first keystroke drops auto": the transcript is typed but not submitted, so auto must not slide the view — and the user's Enter tap — to a different pane than the recording was aimed at. Both routes are control-gated and 404 below that, and both are gated on dais.enabled, which defaults to false. That default is a security decision, not tidiness: the control token was already a remote-shell credential, but with this on it also opens a microphone, which is a different class of capability. Status is control-level too, unlike /api/attention — "the mic on that laptop is open right now" is not something a read-only share link should report. /api/config exposes only dais.enabled, never the socket path, and the pane goes through the same resolveInputTarget as every keystroke so the ^%\d+$ trust boundary holds. 503 marks "could not reach dais" apart from 409 "dais refused", and refusals surface in the strip's note area — which on a phone becomes a full-width line, the first real use of that long-dormant element. On mobile the mic and ✕ take a row of their own so the arrow row stays the tidy six columns it was, and the mic gets four of those columns to the ✕'s two: one is aimed at a driver and the other throws work away, so they should not be equally easy to hit by accident. Verified end to end against an isolated pair — a private tmux server, a test dais daemon with its own runtime dirs, a test airc on another port — covering both token levels, all four actions, the delivery landing unsubmitted in the viewed pane, the button grey-out when the daemon is killed mid-session, and the 409/503 split. test/dais.test.js pins the socket-path precedence, the status reads including a torn state.json, the exact ndjson line, and the fail-soft. No Android change this round: app.js and MainActivity.kt are otherwise mirrors, so docs/dais.md records the divergence as deliberate — the gap this fills does not exist on a device that already has IME dictation.
111 lines
4.2 KiB
JavaScript
111 lines
4.2 KiB
JavaScript
"use strict";
|
|
|
|
const assert = require("node:assert");
|
|
const fs = require("node:fs");
|
|
const net = require("node:net");
|
|
const os = require("node:os");
|
|
const path = require("node:path");
|
|
const { test } = require("node:test");
|
|
|
|
const dais = require("../src/dais");
|
|
|
|
function tempDir() {
|
|
return fs.mkdtempSync(path.join(os.tmpdir(), "airc-dais-test-"));
|
|
}
|
|
|
|
// A stand-in for the dais daemon: accepts one ndjson request per connection and
|
|
// answers with `reply`, recording what it was asked. Unix socket paths are short
|
|
// on purpose (the sun_path limit is ~108 bytes).
|
|
function fakeDaemon(socketPath, reply) {
|
|
const seen = [];
|
|
const server = net.createServer((connection) => {
|
|
connection.setEncoding("utf8");
|
|
let buffer = "";
|
|
connection.on("data", (chunk) => {
|
|
buffer += chunk;
|
|
const newline = buffer.indexOf("\n");
|
|
if (newline < 0) {
|
|
return;
|
|
}
|
|
seen.push(JSON.parse(buffer.slice(0, newline)));
|
|
connection.write(`${JSON.stringify(reply)}\n`);
|
|
});
|
|
});
|
|
return new Promise((resolve) => {
|
|
server.listen(socketPath, () => resolve({
|
|
seen,
|
|
close: () => new Promise((done) => server.close(done)),
|
|
}));
|
|
});
|
|
}
|
|
|
|
test("socketPath falls back to the runtime dir, config wins", () => {
|
|
assert.strictEqual(dais.socketPath({ dais: { socket: "/run/custom/dais.sock" } }), "/run/custom/dais.sock");
|
|
assert.match(dais.socketPath({ dais: { socket: "" } }), /\/dais\/control\.sock$/);
|
|
});
|
|
|
|
test("status reads the marker and state.json next to the socket", () => {
|
|
const dir = tempDir();
|
|
const config = { dais: { enabled: true, socket: path.join(dir, "control.sock") } };
|
|
|
|
// Nothing there yet: unavailable, and no exception.
|
|
assert.deepStrictEqual(dais.status(config), {
|
|
available: false, micOpen: false, mode: "", target: "", dictateTarget: "",
|
|
muted: false, dryRun: false,
|
|
});
|
|
|
|
fs.writeFileSync(path.join(dir, "mic-recording"), "");
|
|
fs.writeFileSync(path.join(dir, "state.json"), JSON.stringify({
|
|
mode: "manual-recording",
|
|
target: "%19",
|
|
dictate_target: "%19",
|
|
muted: false,
|
|
dry_run: true,
|
|
}));
|
|
const hot = dais.status(config);
|
|
assert.strictEqual(hot.micOpen, true);
|
|
assert.strictEqual(hot.mode, "manual-recording");
|
|
assert.strictEqual(hot.dictateTarget, "%19");
|
|
assert.strictEqual(hot.dryRun, true);
|
|
// Still "unavailable": the socket is what says the daemon is live, and a stale
|
|
// state.json outliving a crashed daemon must not read as available.
|
|
assert.strictEqual(hot.available, false);
|
|
|
|
// A half-written state.json (dais rewrites it per transition) must not throw
|
|
// and must not lose the marker's answer.
|
|
fs.writeFileSync(path.join(dir, "state.json"), '{"mode":"manual-rec');
|
|
const torn = dais.status(config);
|
|
assert.strictEqual(torn.micOpen, true);
|
|
assert.strictEqual(torn.mode, "");
|
|
});
|
|
|
|
test("dictate sends one ndjson line and returns the reply", async () => {
|
|
const dir = tempDir();
|
|
const socketPath = path.join(dir, "control.sock");
|
|
const daemon = await fakeDaemon(socketPath, { ok: true, mode: "manual-recording", pane: "%19" });
|
|
const config = { dais: { enabled: true, socket: socketPath } };
|
|
|
|
assert.strictEqual(dais.status(config).available, true, "a listening socket is available");
|
|
|
|
const started = await dais.dictate(config, "start", "%19");
|
|
assert.deepStrictEqual(started, { ok: true, mode: "manual-recording", pane: "%19" });
|
|
assert.deepStrictEqual(daemon.seen[0], { op: "dictate", action: "start", pane: "%19" });
|
|
|
|
// A stop carries no pane: it acts on whatever is recording.
|
|
await dais.dictate(config, "stop");
|
|
assert.deepStrictEqual(daemon.seen[1], { op: "dictate", action: "stop" });
|
|
|
|
await daemon.close();
|
|
});
|
|
|
|
test("dictate fails soft when dais is not there, and refuses unknown actions", async () => {
|
|
const config = { dais: { enabled: true, socket: path.join(tempDir(), "nope.sock") } };
|
|
const missing = await dais.dictate(config, "toggle", "%19");
|
|
assert.strictEqual(missing.ok, false);
|
|
assert.strictEqual(missing.unavailable, true, "unreachable is flagged apart from a refusal");
|
|
|
|
const bogus = await dais.dictate(config, "record", "%19");
|
|
assert.strictEqual(bogus.ok, false);
|
|
assert.ok(!bogus.unavailable, "a bad action never reaches the socket");
|
|
});
|