feat: queue countdown, hold on stop, reply when blocked

Co-Authored-By: Ovis Fast (Claude) <[email protected]>
This commit is contained in:
FactorandOvis Fast committed 2026-11-19 11:22:30 +01:00
1 parent 0558449439
commit 7ec35215c6
9 files changed
+361 -44

No files matched your search

+37 -5
View File
@@ -59,8 +59,9 @@ import { reduce } from "./state.js";
* @typedef {object} QueueItem
* @property {string} id
* @property {string} text
* @property {"queued"|"sending"|"held"|"uncertain"} state
* @property {"queued"|"countdown"|"sending"|"held"|"uncertain"} state
* @property {number} createdAt
* @property {number} [left] - seconds to the send, while the state is "countdown"
* @property {string} [reason]
*/
@@ -78,7 +79,10 @@ export function decide(status) {
case "working":
return { kind: "queue" };
case "blocked":
return { kind: "hold" };
// The agent waits for an answer: what the person writes IS the answer,
// so it goes out at once. (The queue does not start on its own while
// the agent is blocked - see `shouldDrain`.)
return { kind: "send" };
default:
// A status we do not know (the core also reports "error") - wait for a
// known one instead of pasting into an agent that may not be there.
@@ -86,6 +90,25 @@ export function decide(status) {
}
}
/**
* Does a change of the agent's status start the queue (the countdown)?
*
* Only the end of a turn does: the agent was working (or waiting for the
* person) and is idle or done now. A turn that the composer stopped itself
* (the Stop button, Esc) never starts anything.
*
* @param {Status|string} prev
* @param {Status|string} next
* @param {boolean} stoppedByComposer
* @returns {boolean}
*/
export function shouldDrain(prev, next, stoppedByComposer) {
if (stoppedByComposer) return false;
const was = prev === "working" || prev === "blocked";
const is = next === "idle" || next === "done";
return was && is;
}
/**
* The turn gate: the next message goes out only after a whole turn was seen -
* the agent was working and then came back to idle/done. Without that
@@ -205,7 +228,15 @@ export async function drain(ctx, state) {
*/
export function onRestart(queue) {
if (!Array.isArray(queue)) return [];
return queue.map((item) => (item.state === "sending" ? { ...item, state: "uncertain" } : item));
return queue.map((item) => {
if (item.state === "sending") return { ...item, state: "uncertain" };
if (item.state === "countdown") {
// Nothing goes out on its own after a restart.
const { left, ...rest } = item;
return { ...rest, state: "held" };
}
return item;
});
}
/** The agent can take the next message when it is not working and not blocked. */
@@ -217,9 +248,10 @@ function ready(status) {
return status === "idle" || status === "done";
}
/** Only an entry that waits in the queue may go out. */
/** Only an entry that waits in the queue, or whose countdown ran out, may go out. */
function sendable(item) {
return item?.state === "queued";
if (item?.state === "queued") return true;
return item?.state === "countdown" && !(item.left > 0);
}
/** Run `hc` and normalize what comes back. */