/** * @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; /** 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 }} 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} */ #pending = new Map(); /** * @param {object} io * @param {(content: string, meta: Record) => 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} */ 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 */ reply(chat_id, text, end = false) { 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`; this.#pending.delete(chat_id); p.resolve({ text: String(text), end: end === true }); return end === true ? 'spoken; the conversation is over' : 'spoken'; } /** 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); } }