mirror of
https://github.com/Kynde/dais.git
synced 2026-10-20 08:32:28 +02:00
Durable persistence means a picked target stays picked even after its pane dies — which is right, since dais-driven panes come and go. Release is the missing complement: how you say you are done with one and hand the digit back to the picker. state/daemon: - state/release-slot: refuses an unknown slot and the LAST remaining slot, and moves :active-slot to the next survivor when the active one goes. load-targets only honors the durable overlay while (get targets active-slot) resolves, so a dangling active slot would silently discard every persisted target on the next daemon start. - target action "release"; persists through transition!'s existing :targets watch, so a release sticks across restarts exactly like a pick does. dais-ctl: target release <slot>, plus slot completion in _dais-ctl. dais-top: x then 1-5, sticky footer prompt, ? entry. It takes a digit rather than acting on the picker's highlighted row because the usual reason to release a slot is that its pane is GONE — and a gone pane has no row in the picker, which only lists live tmux panes. Also fixes the startup banner: it printed (:targets cfg) — config, not the durable overlay the daemon actually runs with — so it misreported every picked or released slot on every restart. Now reads the state atom, sorted, with * marking the active slot. config: drop the retired system:claude.0 slot. ai/gotchas.md records two things this turned up: the :active-slot-must-resolve invariant, and tmux pane_index being positional — closing a pane renumbers those after it, so a stored "s:w.2" silently retargets while still reading alive and green.
243 lines
8.6 KiB
JavaScript
Executable File
243 lines
8.6 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
|
// dais-ctl — thin client for the dais daemon socket. Dependency-free.
|
|
//
|
|
// Usage:
|
|
// dais-ctl status
|
|
// dais-ctl inject <text...> feed a fake transcript into the router
|
|
// dais-ctl brief [--full] deliver the voice-input briefing to the
|
|
// target: a pointer to BRIEF.md (default)
|
|
// or its full text (--full)
|
|
// dais-ctl vad toggle the VAD session (F9)
|
|
// dais-ctl rec toggle the manual latch (F10)
|
|
// dais-ctl esc send Escape to the target (F11)
|
|
// dais-ctl off force voice off
|
|
// dais-ctl arm next utterance must be a command
|
|
// dais-ctl mute | unmute warm pause: drop utterances until unmute
|
|
// dais-ctl dry-run toggle dry-run (route & plan, don't deliver)
|
|
// dais-ctl target set <slot> <session:window.pane|focus|current>
|
|
// dais-ctl target use <slot>
|
|
// dais-ctl target release <slot> forget a slot, freeing the digit for reuse
|
|
// (refuses the last remaining slot)
|
|
// dais-ctl target next | prev cycle to the next/previous slot
|
|
// dais-ctl target next-live next deliverable slot (skips dead targets)
|
|
// dais-ctl target list
|
|
// dais-ctl target panes list all tmux panes (annotated)
|
|
// dais-ctl target pick [slot] fzf-pick a pane into a slot
|
|
// (default: the active slot)
|
|
// dais-ctl set enter-mode <no-enter|enter-auto|enter-always>
|
|
// dais-ctl set strategy <whole-match|prefix|key-armed>
|
|
// dais-ctl set language <en|fi|...|auto> transcription language
|
|
// (needs a multilingual model for non-en/auto)
|
|
// session-only; config wins on restart
|
|
// dais-ctl events [n]
|
|
// dais-ctl replay <trace_id>
|
|
// dais-ctl shutdown
|
|
//
|
|
// Socket: $DAIS_SOCKET, else $XDG_RUNTIME_DIR/dais/control.sock
|
|
|
|
const net = require("node:net");
|
|
const { spawn } = require("node:child_process");
|
|
const { randomUUID } = require("node:crypto");
|
|
|
|
const SOCKET =
|
|
process.env.DAIS_SOCKET ??
|
|
`${process.env.XDG_RUNTIME_DIR ?? "/tmp"}/dais/control.sock`;
|
|
|
|
function usage(code = 1) {
|
|
const lines = require("node:fs")
|
|
.readFileSync(__filename, "utf8")
|
|
.split("\n")
|
|
.filter((l) => l.startsWith("// "))
|
|
.map((l) => l.slice(3));
|
|
console.error(lines.join("\n"));
|
|
process.exit(code);
|
|
}
|
|
|
|
// ---------- socket helper (multiple requests per connection) ----------
|
|
function connectSock() {
|
|
return new Promise((resolve, reject) => {
|
|
const sock = net.createConnection({ path: SOCKET });
|
|
sock.setEncoding("utf8");
|
|
const pending = [];
|
|
let buf = "";
|
|
sock.on("data", (chunk) => {
|
|
buf += chunk;
|
|
let nl;
|
|
while ((nl = buf.indexOf("\n")) >= 0) {
|
|
const line = buf.slice(0, nl);
|
|
buf = buf.slice(nl + 1);
|
|
const cb = pending.shift();
|
|
if (cb && line.trim()) {
|
|
try { cb(JSON.parse(line)); } catch { cb(null); }
|
|
}
|
|
}
|
|
});
|
|
sock.on("connect", () =>
|
|
resolve({
|
|
request: (req) =>
|
|
new Promise((res) => {
|
|
pending.push(res);
|
|
sock.write(JSON.stringify(req) + "\n");
|
|
}),
|
|
end: () => sock.end(),
|
|
}));
|
|
sock.on("error", (err) => reject(err));
|
|
});
|
|
}
|
|
|
|
function finish(resp) {
|
|
console.log(JSON.stringify(resp, null, 2));
|
|
process.exit(resp && resp.ok ? 0 : 1);
|
|
}
|
|
|
|
// ---------- interactive pane picker ----------
|
|
function fzf(lines, header) {
|
|
return new Promise((resolve) => {
|
|
const p = spawn("fzf",
|
|
["--height=40%", "--reverse", "--delimiter=\t", `--header=${header}`],
|
|
{ stdio: ["pipe", "pipe", "inherit"] });
|
|
let out = "";
|
|
p.stdout.on("data", (d) => (out += d));
|
|
p.on("close", () => resolve(out.trim() || null));
|
|
p.on("error", () => {
|
|
console.error("dais-ctl: fzf not available");
|
|
resolve(null);
|
|
});
|
|
p.stdin.end(lines.join("\n") + "\n");
|
|
});
|
|
}
|
|
|
|
async function pickFlow(slotArg) {
|
|
const sock = await connectSock();
|
|
const resp = await sock.request({ op: "target", action: "panes" });
|
|
if (!resp?.ok) finish(resp ?? { ok: false, error: "no response" });
|
|
const home = process.env.HOME;
|
|
const lines = resp.panes.map((p) => {
|
|
const tags = [
|
|
p.agent ? "★ agent" : null,
|
|
p.slot != null ? `= slot ${p.slot}` : null,
|
|
].filter(Boolean).join(", ");
|
|
const raw = p.path ?? "";
|
|
const path = home && raw.startsWith(home) ? "~" + raw.slice(home.length) : raw;
|
|
return `${p.pane}\t[${p.command}]\t${path}${tags ? `\t(${tags})` : ""}`;
|
|
});
|
|
const slot = slotArg ? Number(slotArg) : resp.active_slot;
|
|
const sel = await fzf(lines, `pick pane for target slot ${slot}`);
|
|
if (!sel) process.exit(1);
|
|
const pane = sel.split("\t")[0];
|
|
finish(await sock.request({ op: "target", action: "set", slot, pane }));
|
|
}
|
|
|
|
// ---------- request builder (single-shot verbs) ----------
|
|
function buildRequest(argv) {
|
|
const [cmd, ...rest] = argv;
|
|
switch (cmd) {
|
|
case "status":
|
|
return { op: "query", query: "status" };
|
|
case "inject": {
|
|
if (rest.length === 0) usage();
|
|
return {
|
|
op: "publish",
|
|
event: {
|
|
schema: "dais.event.v1",
|
|
id: randomUUID(),
|
|
time: new Date().toISOString(),
|
|
type: "voice.transcript",
|
|
source: { module: "dais-ctl", provider: "inject" },
|
|
payload: { text: rest.join(" ") },
|
|
},
|
|
};
|
|
}
|
|
case "brief": {
|
|
const fs = require("node:fs");
|
|
const path = require("node:path");
|
|
const briefPath = path.resolve(__dirname, "..", "BRIEF.md");
|
|
const text =
|
|
rest[0] === "--full"
|
|
? fs.readFileSync(briefPath, "utf8")
|
|
: `Voice input notice: parts of my input arrive via speech-to-text and may ` +
|
|
`contain recognition errors or noise. Read ${briefPath} for how to cope ` +
|
|
`(verify unclear intent, match dictated identifiers against the repo, ` +
|
|
`ignore noise fragments, prefer numbered options).`;
|
|
return {
|
|
op: "publish",
|
|
event: {
|
|
schema: "dais.event.v1",
|
|
id: randomUUID(),
|
|
time: new Date().toISOString(),
|
|
type: "voice.transcript",
|
|
source: { module: "dais-ctl", provider: "brief" },
|
|
payload: { text },
|
|
},
|
|
};
|
|
}
|
|
case "vad":
|
|
return { op: "control", action: "toggle-vad" };
|
|
case "rec":
|
|
return { op: "control", action: "toggle-record" };
|
|
case "esc":
|
|
return { op: "control", action: "esc" };
|
|
case "off":
|
|
return { op: "control", action: "voice-off" };
|
|
case "arm":
|
|
return { op: "control", action: "arm" };
|
|
case "mute":
|
|
return { op: "control", action: "mute" };
|
|
case "unmute":
|
|
return { op: "control", action: "unmute" };
|
|
case "dry-run":
|
|
return { op: "control", action: "toggle-dry-run" };
|
|
case "shutdown":
|
|
return { op: "control", action: "shutdown" };
|
|
case "target": {
|
|
const [action, slot, value] = rest;
|
|
if (action === "set" && slot && value)
|
|
return { op: "target", action: "set", slot: Number(slot), pane: value };
|
|
if (action === "use" && slot)
|
|
return { op: "target", action: "use", slot: Number(slot) };
|
|
if (action === "release" && slot)
|
|
return { op: "target", action: "release", slot: Number(slot) };
|
|
if (action === "next" || action === "prev")
|
|
return { op: "target", action };
|
|
if (action === "next-live") return { op: "target", action: "next-live" };
|
|
if (action === "list") return { op: "target", action: "list" };
|
|
if (action === "panes") return { op: "target", action: "panes" };
|
|
usage();
|
|
break;
|
|
}
|
|
case "set": {
|
|
const [key, value] = rest;
|
|
if (key === "enter-mode" && value) return { op: "settings", enter_mode: value };
|
|
if (key === "strategy" && value) return { op: "settings", strategy: value };
|
|
if (key === "language" && value) return { op: "settings", language: value };
|
|
usage();
|
|
break;
|
|
}
|
|
case "events":
|
|
return { op: "events", last: rest[0] ? Number(rest[0]) : 20 };
|
|
case "replay":
|
|
if (!rest[0]) usage();
|
|
return { op: "replay", trace_id: rest[0] };
|
|
default:
|
|
usage(cmd === "--help" || cmd === "-h" ? 0 : 1);
|
|
}
|
|
}
|
|
|
|
async function main() {
|
|
const argv = process.argv.slice(2);
|
|
try {
|
|
if (argv[0] === "target" && argv[1] === "pick") {
|
|
await pickFlow(argv[2]);
|
|
return;
|
|
}
|
|
const req = buildRequest(argv);
|
|
const sock = await connectSock();
|
|
finish(await sock.request(req));
|
|
} catch (err) {
|
|
console.error(`dais-ctl: ${err.message} (socket: ${SOCKET} — daemon running?)`);
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
main();
|