Files
dckc 5191ee9931 feat: the brain can change the box's speaker volume
"Speak up" and "too loud" now work: the reply tool takes louder, in
percentage points, and the button service nudges the speaker's softvol
before it speaks that reply, so the person hears the change at once.
The box keeps the level between 10% and 80% (body/mixer.py).

No new port or key: the volume changes only with a reply, in a
conversation someone is listening to.

Generated-by: Claude Opus 5.5
2026-12-03 16:44:54 -06:00

126 lines
5.5 KiB
JavaScript

/**
* @file The ask capability's core: conversations between the box and the brain.
* The box's ask(text, conversation, onSay) notifies the brain and waits; the brain's say tool speaks
* through onSay meanwhile ("Let me check"), and its reply tool settles the ask, perhaps ending
* the conversation. The box's end(conversation) tells the brain a conversation is over.
* @see channel.js, which wires this to MCP (the brain's side) and Cap'n Web (the box's side)
*/
import { RpcTarget } from 'capnweb';
/** Spoken questions are short. */
export const MAX_QUESTION_CHARS = 2000;
/** The most a reply can change the box's speaker volume, in percentage points; the box keeps its own bounds. */
export const MAX_LOUDER = 30;
/** A conversation id: the box makes them up, so keep them tame. */
const CONVERSATION = /^[A-Za-z0-9-]{1,40}$/;
/**
* @typedef {{ text: string, end: boolean, louder?: number }} Answer
* @typedef {{ conversation?: string, onSay?: (text: string) => unknown }} AskOpts
* @typedef {{ resolve: (a: Answer) => void, reject: (e: Error) => void, onSay?: AskOpts['onSay'] }} Pending
*/
/** Questions waiting on the brain, each by its chat_id, and the brain's say and reply tools for them. */
export class Channel {
#notify;
#setTimeout;
#newId;
#timeoutMs;
/** @type {Map<string, Pending>} */
#pending = new Map();
/**
* @param {object} io
* @param {(content: string, meta: Record<string, string>) => void} io.notify sends a channel event to the brain
* @param {(ms: number, f: () => void) => unknown} io.setTimeout
* @param {() => string} io.newId
* @param {number} [timeoutMs]
*/
constructor({ notify, setTimeout, newId }, timeoutMs = 120_000) {
this.#notify = notify;
this.#setTimeout = setTimeout;
this.#newId = newId;
this.#timeoutMs = timeoutMs;
}
/** @param {string} text @param {string} user @param {AskOpts} [opts] */
ask(text, user, { conversation, onSay } = {}) {
if (typeof text !== 'string' || !text.trim()) throw new TypeError('ask needs text');
if (text.length > MAX_QUESTION_CHARS) throw new RangeError(`question over ${MAX_QUESTION_CHARS} chars`);
if (conversation !== undefined && !CONVERSATION.test(conversation)) throw new TypeError('conversation: a short id');
if (onSay !== undefined && typeof onSay !== 'function') throw new TypeError('onSay: a function');
const chat_id = this.#newId();
/** @type {Promise<Answer>} */
const answer = new Promise((resolve, reject) => this.#pending.set(chat_id, { resolve, reject, onSay }));
this.#setTimeout(this.#timeoutMs, () => {
const p = this.#pending.get(chat_id);
if (p) { this.#pending.delete(chat_id); p.reject(new Error(`no reply in ${this.#timeoutMs / 1000} s`)); }
});
this.#notify(text, { chat_id, user, ...(conversation ? { conversation } : {}) });
return answer;
}
/**
* The brain's say tool: speak now, and keep the turn. Returns what to tell the brain.
* @param {string} chat_id @param {string} text
*/
async say(chat_id, text) {
const p = this.#pending.get(chat_id);
if (!p) return `no one is waiting on chat_id ${chat_id}: it timed out or was already answered`;
if (!p.onSay) return 'this question came without a way to speak before the reply; use reply';
await p.onSay(String(text));
return 'spoken; the turn is still yours: reply when you have the answer';
}
/**
* The brain's reply tool. Returns what to tell the brain.
* @param {string} chat_id @param {string} text @param {boolean} [end] the conversation is over
* @param {number} [louder] change the speaker volume this many percentage points first; negative is softer
*/
reply(chat_id, text, end = false, louder = undefined) {
const p = this.#pending.get(chat_id);
if (!p) return `no one is waiting on chat_id ${chat_id}: it timed out or was already answered`;
const volumeOk = louder === undefined || (Number.isInteger(louder) && Math.abs(louder) <= MAX_LOUDER);
this.#pending.delete(chat_id);
p.resolve({ text: String(text), end: end === true, ...(volumeOk && louder ? { louder } : {}) });
const outcome = end === true ? 'spoken; the conversation is over' : 'spoken';
if (!volumeOk) return `${outcome}, but the volume is unchanged: louder must be a whole number from -${MAX_LOUDER} to ${MAX_LOUDER}`;
return louder ? `${outcome}, ${louder > 0 ? 'louder' : 'softer'} by ${Math.abs(louder)} points (within the box's bounds)` : outcome;
}
/** The box says a conversation is over: by goodbye, silence, or a press. @param {string} conversation @param {string} user */
end(conversation, user) {
if (!CONVERSATION.test(conversation)) throw new TypeError('conversation: a short id');
this.#notify('(the conversation is over; no reply needed)', { user, conversation, event: 'ended' });
}
waiting() { return this.#pending.size; }
}
/**
* The capability the box holds: ask(text, conversation, onSay) resolves to the brain's answer;
* end(conversation) says it's over.
*/
export class Asker extends RpcTarget {
#channel;
#user;
/**
* @param {Channel} channel
* @param {string} user who's speaking, as the brain sees it
*/
constructor(channel, user) {
super();
this.#channel = channel;
this.#user = user;
}
/** @param {string} text @param {string} [conversation] @param {(text: string) => unknown} [onSay] */
ask(text, conversation, onSay) { return this.#channel.ask(text, this.#user, { conversation, onSay }); }
/** @param {string} conversation */
end(conversation) { this.#channel.end(conversation, this.#user); }
}