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
+128
-9
@@ -94,6 +94,14 @@ export default class Composer extends View {
|
||||
this.waiting = new Set();
|
||||
this.restored = new Set();
|
||||
|
||||
// Drafts: the identity of each terminal (`hc get`, cached per address),
|
||||
// the agents whose draft was read from the disk (or is being read), and
|
||||
// the timer that writes the draft 500 ms after the last change.
|
||||
this.identities = new Map();
|
||||
this.draftReady = new Set();
|
||||
this.draftLoading = new Set();
|
||||
this.draftTimer = null;
|
||||
|
||||
// The menu of suggestions: what it shows, which row is picked.
|
||||
this.menu = null;
|
||||
this.debounce = null;
|
||||
@@ -178,6 +186,7 @@ export default class Composer extends View {
|
||||
/** The copy is gone - it must not take the keyboard with it. */
|
||||
close(cx) {
|
||||
this.closed = true;
|
||||
this.saveDraftNow();
|
||||
}
|
||||
|
||||
// --- the agent -----------------------------------------------------------
|
||||
@@ -199,24 +208,124 @@ export default class Composer extends View {
|
||||
const key = agent ? agentKey(agent) : "";
|
||||
if (key === this.currentKey) return;
|
||||
|
||||
if (this.currentKey) {
|
||||
this.state = reduce(this.state, {
|
||||
type: "draft",
|
||||
agent: this.currentKey,
|
||||
text: this.textarea.value(),
|
||||
});
|
||||
}
|
||||
// The draft of the agent we leave goes to the disk at once.
|
||||
this.saveDraftNow();
|
||||
this.currentKey = key;
|
||||
if (!key) return;
|
||||
|
||||
this.restoreQueue(key);
|
||||
this.showDraft(key);
|
||||
this.restoreDraft(agent, key, cx);
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
/** The draft of the agent (and its caret) goes into the field. */
|
||||
showDraft(key) {
|
||||
const draft = this.state.drafts[key] ?? "";
|
||||
if (draft !== this.textarea.value()) {
|
||||
this.textarea.set_value(draft);
|
||||
this.value = draft;
|
||||
}
|
||||
cx.notify();
|
||||
const caret = this.state.carets?.[key];
|
||||
this.restoreCaret(Number.isInteger(caret) ? Math.min(caret, draft.length) : draft.length);
|
||||
}
|
||||
|
||||
/**
|
||||
* The draft of an agent comes back from the disk - once per address.
|
||||
*
|
||||
* The identity of the terminal (`created_ms` from `hc get`) says whether it
|
||||
* is the terminal the draft was written for. Nothing is written to the disk
|
||||
* for the agent before this is done: a draft written with an identity that
|
||||
* is not known yet would wipe the one that is waiting there.
|
||||
*/
|
||||
restoreDraft(agent, key, cx) {
|
||||
if (this.draftReady.has(key) || this.draftLoading.has(key)) return;
|
||||
this.draftLoading.add(key);
|
||||
|
||||
cx.spawn(async (cx) => {
|
||||
try {
|
||||
const identity = await this.fetchIdentity(agent);
|
||||
this.identities.set(key, identity);
|
||||
|
||||
const stored = storage.loadDraft(this.store, key);
|
||||
const here = key === this.currentKey;
|
||||
const typed = here ? this.textarea.value() : (this.state.drafts[key] ?? "");
|
||||
const untouched = typed === "" && (this.state.attachments[key] ?? []).length === 0;
|
||||
|
||||
// What the person typed while the identity was read is not
|
||||
// overwritten.
|
||||
if (stored && untouched) {
|
||||
this.state = reduce(this.state, { type: "restore_draft", agent: key, draft: stored, identity });
|
||||
this.persistQueue(key);
|
||||
if (here) this.showDraft(key);
|
||||
}
|
||||
} catch {
|
||||
// The draft stays on the disk; the next switch to this agent tries again.
|
||||
this.draftLoading.delete(key);
|
||||
return;
|
||||
}
|
||||
this.draftLoading.delete(key);
|
||||
this.draftReady.add(key);
|
||||
cx.notify();
|
||||
});
|
||||
}
|
||||
|
||||
/** Who the terminal is: when it was created (`hc get`) and its program. */
|
||||
async fetchIdentity(agent) {
|
||||
const app = typeof agent.app === "string" && agent.app.trim() !== "" ? agent.app.trim() : null;
|
||||
try {
|
||||
const res = await hc(["get", agent.address, "--json"]);
|
||||
const payload = hcOk(res) ? parseJson(res.stdout) : null;
|
||||
const created = payload && Number.isFinite(payload.created_ms) ? payload.created_ms : null;
|
||||
return { created_ms: created, app };
|
||||
} catch {
|
||||
return { created_ms: null, app };
|
||||
}
|
||||
}
|
||||
|
||||
/** The draft goes to the disk 500 ms after the last change. */
|
||||
scheduleDraftSave(cx) {
|
||||
if (this.draftTimer) {
|
||||
this.draftTimer.cancel();
|
||||
this.draftTimer = null;
|
||||
}
|
||||
this.draftTimer = cx.timer.after(500, () => {
|
||||
this.draftTimer = null;
|
||||
this.saveDraftNow();
|
||||
});
|
||||
}
|
||||
|
||||
/** The draft of the agent under the focus goes to the disk now. */
|
||||
saveDraftNow() {
|
||||
if (this.draftTimer) {
|
||||
this.draftTimer.cancel();
|
||||
this.draftTimer = null;
|
||||
}
|
||||
const key = this.currentKey;
|
||||
if (!key) return;
|
||||
|
||||
// The field is the truth: the state follows it.
|
||||
this.state = reduce(this.state, {
|
||||
type: "draft",
|
||||
agent: key,
|
||||
text: this.textarea.value(),
|
||||
caret: this.caret(),
|
||||
});
|
||||
|
||||
const identity = this.identities.get(key);
|
||||
if (!identity || !this.draftReady.has(key)) return;
|
||||
|
||||
try {
|
||||
storage.saveDraft(this.store, key, {
|
||||
text: this.state.drafts[key] ?? "",
|
||||
caret: this.state.carets?.[key] ?? 0,
|
||||
attachments: this.state.attachments[key] ?? [],
|
||||
identity,
|
||||
savedAt: Date.now(),
|
||||
});
|
||||
} catch (err) {
|
||||
this.notice(`Nie udało się zapisać szkicu: ${errorText(err)}`, "error");
|
||||
}
|
||||
}
|
||||
|
||||
/** The queue of an agent comes back from the disk; what was on the wire is uncertain. */
|
||||
@@ -276,7 +385,13 @@ export default class Composer extends View {
|
||||
|
||||
const agent = this.target();
|
||||
if (agent) {
|
||||
this.state = reduce(this.state, { type: "draft", agent: agentKey(agent), text: next });
|
||||
this.state = reduce(this.state, {
|
||||
type: "draft",
|
||||
agent: agentKey(agent),
|
||||
text: next,
|
||||
caret: this.caret(),
|
||||
});
|
||||
this.scheduleDraftSave(cx);
|
||||
}
|
||||
|
||||
// The newline of the keyboard does not move the menu.
|
||||
@@ -500,6 +615,8 @@ export default class Composer extends View {
|
||||
this.value = "";
|
||||
this.enterText = null;
|
||||
this.closeMenu();
|
||||
// A sent draft must not come back after a restart.
|
||||
this.scheduleDraftSave(cx);
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
@@ -1023,6 +1140,7 @@ export default class Composer extends View {
|
||||
);
|
||||
this.textarea.set_value(result.value);
|
||||
this.value = result.value;
|
||||
this.scheduleDraftSave(cx);
|
||||
cx.notify();
|
||||
});
|
||||
}
|
||||
@@ -1040,6 +1158,7 @@ export default class Composer extends View {
|
||||
this.textarea.set_value(text);
|
||||
this.value = text;
|
||||
}
|
||||
this.scheduleDraftSave(cx);
|
||||
cx.notify();
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user