feat: persistent per-agent drafts with identity check
Co-Authored-By: Ovis Fast (Claude) <[email protected]>
This commit is contained in:
1 parent
187a385f6f
commit
2ce1b45ee4
5 files changed
+367
-10
No files matched your search
@@ -42,6 +42,7 @@ import { onRestart } from "./delivery.js";
|
||||
*
|
||||
* @typedef {object} State
|
||||
* @property {Record<string,string>} drafts
|
||||
* @property {Record<string,number>} carets
|
||||
* @property {Record<string,Attachment[]>} attachments
|
||||
* @property {Record<string,QueueItem[]>} queue
|
||||
* @property {Notice|null} notice
|
||||
@@ -53,6 +54,7 @@ import { onRestart } from "./delivery.js";
|
||||
export function createState() {
|
||||
return {
|
||||
drafts: {},
|
||||
carets: {},
|
||||
attachments: {},
|
||||
queue: {},
|
||||
notice: null,
|
||||
@@ -84,7 +86,16 @@ export function isSending(state, agent) {
|
||||
export function reduce(state, action) {
|
||||
switch (action.type) {
|
||||
case "draft":
|
||||
return { ...state, drafts: { ...state.drafts, [action.agent]: action.text } };
|
||||
return {
|
||||
...state,
|
||||
drafts: { ...state.drafts, [action.agent]: action.text },
|
||||
...(Number.isInteger(action.caret)
|
||||
? { carets: { ...(state.carets ?? {}), [action.agent]: action.caret } }
|
||||
: {}),
|
||||
};
|
||||
|
||||
case "restore_draft":
|
||||
return restoreDraft(state, action);
|
||||
|
||||
case "send":
|
||||
return { ...state, drafts: { ...state.drafts, [action.agent]: "" } };
|
||||
@@ -142,6 +153,46 @@ export function reduce(state, action) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The draft of an agent comes back from the disk.
|
||||
*
|
||||
* The draft carries the identity of the terminal it was written for. The
|
||||
* address (`t2`) can be reused by another terminal after a restart: when the
|
||||
* start time differs, the draft is still restored - the text is the person's
|
||||
* - but it says so, and the whole queue of that address is held, so nothing
|
||||
* goes on its own to an agent it was not written for. An `uncertain` entry
|
||||
* stays `uncertain`: it has no "send now" button, and "held" has.
|
||||
*/
|
||||
function restoreDraft(state, action) {
|
||||
const draft = action.draft;
|
||||
if (!draft || typeof draft.text !== "string") return state;
|
||||
|
||||
const agent = action.agent;
|
||||
const same = (draft.identity?.created_ms ?? null) === (action.identity?.created_ms ?? null);
|
||||
|
||||
const next = {
|
||||
...state,
|
||||
drafts: { ...state.drafts, [agent]: draft.text },
|
||||
carets: { ...(state.carets ?? {}), [agent]: Number.isInteger(draft.caret) ? draft.caret : draft.text.length },
|
||||
attachments: {
|
||||
...state.attachments,
|
||||
[agent]: (draft.attachments ?? []).map((file) => ({ ...file })),
|
||||
},
|
||||
};
|
||||
if (same) return next;
|
||||
|
||||
return {
|
||||
...next,
|
||||
notice: { text: "Szkic z poprzedniej sesji agenta", level: "warn" },
|
||||
queue: {
|
||||
...next.queue,
|
||||
[agent]: list(next.queue, agent).map((item) =>
|
||||
item.state === "uncertain" || item.state === "held" ? item : { ...item, state: "held" },
|
||||
),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* The queue of an agent comes back from the disk.
|
||||
*
|
||||
|
||||
Reference in new issue
Block a user