feat: queue countdown, hold on stop, reply when blocked
Co-Authored-By: Ovis Fast (Claude) <[email protected]>
This commit is contained in:
1 parent
0558449439
commit
7ec35215c6
9 files changed
+361
-44
No files matched your search
@@ -22,12 +22,13 @@ import { onRestart } from "./delivery.js";
|
||||
* @property {AttachmentState} state
|
||||
* @property {string} [error]
|
||||
*
|
||||
* @typedef {"queued"|"sending"|"held"|"uncertain"} QueueItemState
|
||||
* @typedef {"queued"|"countdown"|"sending"|"held"|"uncertain"} QueueItemState
|
||||
* @typedef {object} QueueItem
|
||||
* @property {string} id
|
||||
* @property {string} text
|
||||
* @property {QueueItemState} state
|
||||
* @property {number} createdAt
|
||||
* @property {number} [left]
|
||||
* @property {string} [reason]
|
||||
*
|
||||
* @typedef {object} Notice
|
||||
@@ -50,6 +51,9 @@ import { onRestart } from "./delivery.js";
|
||||
* @property {null} conflict
|
||||
*/
|
||||
|
||||
/** Seconds between the end of the agent's turn and the send of the queue. */
|
||||
export const COUNTDOWN_SECONDS = 3;
|
||||
|
||||
/** @returns {State} */
|
||||
export function createState() {
|
||||
return {
|
||||
@@ -100,6 +104,23 @@ export function reduce(state, action) {
|
||||
case "hold_queue":
|
||||
return holdQueue(state, action.agent);
|
||||
|
||||
case "countdown_start":
|
||||
return countdownStart(state, action);
|
||||
|
||||
case "countdown_tick":
|
||||
return mapItem(state, action, (item) =>
|
||||
item.state === "countdown" ? { ...item, left: Math.max(0, (item.left ?? 0) - 1) } : item,
|
||||
);
|
||||
|
||||
case "countdown_cancel":
|
||||
return mapItem(state, action, (item) => (item.state === "countdown" ? toState(item, "held") : item));
|
||||
|
||||
case "countdown_reset":
|
||||
return mapItem(state, action, (item) => (item.state === "countdown" ? toState(item, "queued") : item));
|
||||
|
||||
case "hold_all":
|
||||
return holdQueue(state, action.agent, { sending: false });
|
||||
|
||||
case "send":
|
||||
return { ...state, drafts: { ...state.drafts, [action.agent]: "" } };
|
||||
|
||||
@@ -203,21 +224,59 @@ function restoreDraft(state, action) {
|
||||
return held;
|
||||
}
|
||||
|
||||
/** Everything that could go out on its own waits for the person. */
|
||||
function holdQueue(state, agent) {
|
||||
/**
|
||||
* Everything that could go out on its own waits for the person.
|
||||
*
|
||||
* `hold_queue` (identity doubt) also takes the entry that is on the wire;
|
||||
* `hold_all` (the person stopped the agent) leaves `sending` alone - its fate
|
||||
* is not known yet and `settle` decides it.
|
||||
*/
|
||||
function holdQueue(state, agent, { sending = true } = {}) {
|
||||
const items = list(state.queue, agent);
|
||||
if (!items.some((item) => item.state !== "uncertain" && item.state !== "held")) return state;
|
||||
const holdable = (item) =>
|
||||
item.state === "queued" || item.state === "countdown" || (sending && item.state === "sending");
|
||||
if (!items.some(holdable)) return state;
|
||||
return {
|
||||
...state,
|
||||
queue: {
|
||||
...state.queue,
|
||||
[agent]: items.map((item) =>
|
||||
item.state === "uncertain" || item.state === "held" ? item : { ...item, state: "held" },
|
||||
),
|
||||
[agent]: items.map((item) => (holdable(item) ? toState(item, "held") : item)),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/** The entry in another state, without the seconds of a countdown. */
|
||||
function toState(item, next) {
|
||||
const { left, ...rest } = item;
|
||||
return { ...rest, state: next };
|
||||
}
|
||||
|
||||
/** One entry of the queue changes; the rest stays. Nothing changes - same state back. */
|
||||
function mapItem(state, action, change) {
|
||||
const items = list(state.queue, action.agent);
|
||||
const item = items.find((entry) => entry.id === action.id);
|
||||
if (!item) return state;
|
||||
const changed = change(item);
|
||||
if (changed === item) return state;
|
||||
return {
|
||||
...state,
|
||||
queue: { ...state.queue, [action.agent]: items.map((entry) => (entry.id === action.id ? changed : entry)) },
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* The countdown starts for the first `queued` entry - and only when no other
|
||||
* entry is counting or on the wire. An `uncertain` entry before it does not
|
||||
* matter: it is never sent on its own and is left as it is.
|
||||
*/
|
||||
function countdownStart(state, action) {
|
||||
const items = list(state.queue, action.agent);
|
||||
if (items.some((item) => item.state === "countdown" || item.state === "sending")) return state;
|
||||
const first = items.find((item) => item.state === "queued");
|
||||
if (!first || first.id !== action.id) return state;
|
||||
return mapItem(state, action, (item) => ({ ...item, state: "countdown", left: COUNTDOWN_SECONDS }));
|
||||
}
|
||||
|
||||
/**
|
||||
* The queue of an agent comes back from the disk.
|
||||
*
|
||||
@@ -294,13 +353,13 @@ function withoutReason(item) {
|
||||
function claim(state, action) {
|
||||
const items = list(state.queue, action.agent);
|
||||
const item = items.find((entry) => entry.id === action.id);
|
||||
if (!item || item.state !== "queued") return state;
|
||||
if (!item || (item.state !== "queued" && item.state !== "countdown")) return state;
|
||||
return {
|
||||
...state,
|
||||
queue: {
|
||||
...state.queue,
|
||||
[action.agent]: items.map((entry) =>
|
||||
entry.id === action.id ? { ...entry, state: "sending" } : entry,
|
||||
entry.id === action.id ? { ...toState(entry, "sending") } : entry,
|
||||
),
|
||||
},
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user