feat: history and favorites model

Co-Authored-By: Ovis Fast (Claude) <[email protected]>
This commit is contained in:
FactorandOvis Fast committed 2026-11-19 11:31:37 +01:00
1 parent 86ca415d96
commit c655e46dbf
4 files changed
+353 -25

No files matched your search

+41 -3
View File
@@ -41,6 +41,7 @@ import { detect as detectTrigger, rank, merge, startsLine } from "./suggest.js";
import * as catalog from "./catalog.js";
import { LIMITS, validateMany, mention, removeMention } from "./attachments.js";
import * as storage from "./storage.js";
import { record } from "./history.js";
import {
agentKey,
agentName,
@@ -87,6 +88,11 @@ export default class Composer extends View {
this.state = createState();
this.store = guardedStore({ get: shared, set: setShared });
// The one place every send of a message passes: the direct send, the
// queue, the countdown and "Wyślij teraz" all call `hc send` through it,
// so the history gets exactly one entry per send that `hc` accepted.
this.sendHc = (args) => this.runSend(args);
// Per agent: the turn gate, whether a drain is in flight, and whether the
// queue of that agent was read from the disk.
this.gates = new Map();
@@ -736,7 +742,7 @@ export default class Composer extends View {
this.persistQueue(key);
try {
const result = await sendToTerminal({ terminal: agent.address, hc }, message);
const result = await sendToTerminal({ terminal: agent.address, hc: this.sendHc }, message);
if (result.ok) {
this.state = reduce(this.state, { type: "settle", agent: key, id, outcome: "sent" });
this.persistQueue(key);
@@ -892,7 +898,7 @@ export default class Composer extends View {
{
agent: key,
terminal: agent.address,
hc,
hc: this.sendHc,
status: () => this.statusOf(agent),
// One message per turn: after the first send the gate is shut.
gate: () => !sent && gatePasses(this.gateFor(key), this.statusOf(agent)),
@@ -997,7 +1003,7 @@ export default class Composer extends View {
this.persistQueue(key);
try {
const result = await sendToTerminal({ terminal: agent.address, hc }, item.text);
const result = await sendToTerminal({ terminal: agent.address, hc: this.sendHc }, item.text);
if (result.ok) {
this.state = reduce(this.state, { type: "settle", agent: key, id, outcome: "sent" });
this.persistQueue(key);
@@ -1294,6 +1300,38 @@ export default class Composer extends View {
// --- the state on the disk -----------------------------------------------
/**
* `hc` for the sends: runs the call and, when it was a `send` that `hc`
* accepted (exit code 0 and no "error" in the JSON - `hcOk`), writes the
* message into the history. A history that cannot be written does not
* change the result of the send.
*/
async runSend(args) {
const res = await hc(args);
if (args[0] === "send" && hcOk(res)) this.recordSent(String(args[1]), String(args[2] ?? ""));
return res;
}
/** The history gets the message that went to the terminal. */
recordSent(address, text) {
try {
const agent = liveAgents().find((entry) => entry.address === address);
const entry = {
id: this.newId(),
text,
sentAt: Date.now(),
agent: address,
agentName: agent ? agentName(agent) : "",
space: agent?.space ?? "",
spaceName: agent?.space_name ?? "",
tabName: agent?.tab_name ?? "",
};
storage.saveHistory(this.store, record(storage.loadHistory(this.store), entry));
} catch (err) {
this.notice(`Nie udało się zapisać historii: ${errorText(err)}`, "error");
}
}
/** The queue of an agent goes to the disk at once - a crash must not send a message twice. */
persistQueue(key) {
try {