samagotchi 0.186.0 → 0.188.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +68 -1
- data/bin/chi +4 -0
- data/docs/architecture.md +8 -4
- data/docs/bundles.md +124 -120
- data/docs/cli.md +26 -5
- data/docs/configuration.md +4 -4
- data/docs/context.md +18 -162
- data/docs/guardrails.md +3 -1
- data/docs/hooks.md +2 -1
- data/docs/internals/attached-context.md +19 -37
- data/docs/internals/bridge.md +1 -0
- data/docs/internals/engine.md +2 -2
- data/docs/internals/inline-widgets.md +18 -0
- data/docs/internals/llm-context-forget.md +5 -6
- data/docs/internals/objectives.md +1 -0
- data/docs/internals/pages.md +175 -0
- data/docs/internals/peers.md +149 -0
- data/docs/internals/remote-sessions.md +7 -4
- data/docs/internals/web-frontend.md +65 -40
- data/docs/internals/web-push.md +1 -1
- data/docs/memory.md +8 -1
- data/docs/pages.md +151 -0
- data/docs/peers.md +141 -0
- data/docs/plugins.md +52 -27
- data/docs/sessions.md +8 -4
- data/docs/sub-agent.md +5 -5
- data/lib/samagotchi/bridge.rb +3 -3
- data/lib/samagotchi/bundle_command/api_calls.rb +2 -1
- data/lib/samagotchi/bundle_command/check.rb +1 -1
- data/lib/samagotchi/bundles/claude-peer/manifest.yml +11 -18
- data/lib/samagotchi/bundles/claude-peer/plugin.rb +22 -16
- data/lib/samagotchi/bundles/claude-peer/scripts/chi-peer-SKILL.md +48 -39
- data/lib/samagotchi/bundles/claude-peer/scripts/pair +15 -73
- data/lib/samagotchi/bundles/claude-peer/skill_claude_peer.md +16 -16
- data/lib/samagotchi/bundles/codex-peer/manifest.yml +11 -15
- data/lib/samagotchi/bundles/codex-peer/plugin.rb +35 -24
- data/lib/samagotchi/bundles/codex-peer/scripts/chi-peer-SKILL.md +51 -40
- data/lib/samagotchi/bundles/codex-peer/scripts/pair +18 -54
- data/lib/samagotchi/bundles/codex-peer/skill_codex_peer.md +16 -16
- data/lib/samagotchi/bundles/dev/manifest.yml +2 -2
- data/lib/samagotchi/bundles/skills/manifest.yml +2 -2
- data/lib/samagotchi/bundles/skills/plugin.rb +14 -8
- data/lib/samagotchi/bundles/system/manifest.yml +5 -2
- data/lib/samagotchi/bundles/system/self_map.md +5 -1
- data/lib/samagotchi/bundles/system/skill_pages.md +93 -0
- data/lib/samagotchi/client_id.rb +15 -1
- data/lib/samagotchi/config.rb +8 -7
- data/lib/samagotchi/context_absorber.rb +23 -277
- data/lib/samagotchi/context_sources.rb +17 -69
- data/lib/samagotchi/engine/messages.rb +12 -1
- data/lib/samagotchi/engine/plugins.rb +1 -1
- data/lib/samagotchi/engine.rb +7 -6
- data/lib/samagotchi/engine_peers.rb +0 -3
- data/lib/samagotchi/guardrail_wiring.rb +2 -0
- data/lib/samagotchi/guardrails/chi_dirs.rb +4 -3
- data/lib/samagotchi/guardrails/parent_approvals.rb +6 -5
- data/lib/samagotchi/guardrails/protected_paths.rb +6 -3
- data/lib/samagotchi/inline_widgets.rb +73 -0
- data/lib/samagotchi/log_line.rb +1 -1
- data/lib/samagotchi/note_move.rb +3 -1
- data/lib/samagotchi/page_command.rb +243 -0
- data/lib/samagotchi/pages/builder.rb +186 -0
- data/lib/samagotchi/pages/launch.rb +193 -0
- data/lib/samagotchi/pages/runner.rb +154 -0
- data/lib/samagotchi/pages/store.rb +152 -0
- data/lib/samagotchi/pages.rb +119 -0
- data/lib/samagotchi/peer_command.rb +192 -0
- data/lib/samagotchi/peer_continue.rb +99 -0
- data/lib/samagotchi/peer_exchanges.rb +3 -3
- data/lib/samagotchi/peer_mailbox.rb +360 -0
- data/lib/samagotchi/peer_note.rb +208 -0
- data/lib/samagotchi/peer_receipts.rb +247 -0
- data/lib/samagotchi/peer_registry.rb +44 -0
- data/lib/samagotchi/peer_roster.rb +79 -0
- data/lib/samagotchi/peer_say.rb +168 -0
- data/lib/samagotchi/plugin/api.rb +29 -21
- data/lib/samagotchi/plugin/context.rb +7 -0
- data/lib/samagotchi/plugin/loader.rb +3 -3
- data/lib/samagotchi/plugin/peer.rb +175 -64
- data/lib/samagotchi/plugin/peers.rb +70 -0
- data/lib/samagotchi/run_options.rb +3 -1
- data/lib/samagotchi/schedule_command.rb +10 -3
- data/lib/samagotchi/schedule_display.rb +32 -0
- data/lib/samagotchi/session_access/contract.rb +19 -2
- data/lib/samagotchi/session_access/local.rb +132 -14
- data/lib/samagotchi/session_access/remote.rb +4 -2
- data/lib/samagotchi/session_access/remote_peers.rb +75 -0
- data/lib/samagotchi/session_continue.rb +12 -0
- data/lib/samagotchi/session_inbox_delivery.rb +5 -2
- data/lib/samagotchi/session_manager/delivery.rb +5 -3
- data/lib/samagotchi/session_manager/removal.rb +2 -0
- data/lib/samagotchi/session_manager/spawn.rb +2 -1
- data/lib/samagotchi/session_manager.rb +1 -0
- data/lib/samagotchi/session_stores.rb +17 -0
- data/lib/samagotchi/steer.rb +1 -1
- data/lib/samagotchi/system_prompt.rb +1 -1
- data/lib/samagotchi/terminal_ui/attached_loop.rb +1 -1
- data/lib/samagotchi/terminal_ui/formatting.rb +4 -1
- data/lib/samagotchi/testing/boundary.rb +4 -13
- data/lib/samagotchi/testing/bundle.rb +40 -5
- data/lib/samagotchi/testing/install.rb +4 -4
- data/lib/samagotchi/testing/recording_api.rb +2 -1
- data/lib/samagotchi/tools/context_read.rb +0 -14
- data/lib/samagotchi/tools/list_schedules.rb +8 -3
- data/lib/samagotchi/version.rb +1 -1
- data/lib/samagotchi/web/app.rb +7 -18
- data/lib/samagotchi/web/message_display.rb +4 -2
- data/lib/samagotchi/web/page_routes.rb +116 -0
- data/lib/samagotchi/web/peer_routes.rb +133 -0
- data/lib/samagotchi/web/public/app.js +35 -95
- data/lib/samagotchi/web/public/bubbles.js +2 -2
- data/lib/samagotchi/web/public/cards_place.js +20 -1
- data/lib/samagotchi/web/public/context_chips.js +45 -46
- data/lib/samagotchi/web/public/history_reveal.js +4 -1
- data/lib/samagotchi/web/public/history_shape.js +3 -2
- data/lib/samagotchi/web/public/index.html +16 -0
- data/lib/samagotchi/web/public/notify.js +1 -1
- data/lib/samagotchi/web/public/page_blocks.js +58 -0
- data/lib/samagotchi/web/public/page_links.js +33 -0
- data/lib/samagotchi/web/public/page_model.js +68 -0
- data/lib/samagotchi/web/public/page_route.js +24 -0
- data/lib/samagotchi/web/public/page_table.js +74 -0
- data/lib/samagotchi/web/public/page_table_model.js +76 -0
- data/lib/samagotchi/web/public/page_view.js +219 -0
- data/lib/samagotchi/web/public/pages.css +77 -0
- data/lib/samagotchi/web/public/pages_group.js +65 -0
- data/lib/samagotchi/web/public/peer_chip.js +60 -74
- data/lib/samagotchi/web/public/peer_data.js +13 -11
- data/lib/samagotchi/web/public/peer_links.js +12 -7
- data/lib/samagotchi/web/public/peer_mailbox_api.js +20 -0
- data/lib/samagotchi/web/public/peer_note.js +7 -3
- data/lib/samagotchi/web/public/peer_text.js +4 -4
- data/lib/samagotchi/web/public/quiet_notes.js +199 -0
- data/lib/samagotchi/web/public/revise_stack.js +4 -3
- data/lib/samagotchi/web/public/session_view.js +2 -2
- data/lib/samagotchi/web/public/stream_handlers.js +3 -2
- data/lib/samagotchi/web/public/turn_block.js +7 -3
- data/lib/samagotchi/web/public/turn_events.js +6 -0
- data/lib/samagotchi/web/public/views.js +161 -0
- data/lib/samagotchi/web/push/attention.rb +3 -2
- data/lib/samagotchi/web/session_access_routes.rb +3 -20
- data/lib/samagotchi/web/viewer.rb +1 -1
- data/lib/samagotchi/worker/leave.rb +3 -0
- data/lib/samagotchi/worker/mail.rb +73 -0
- data/lib/samagotchi/worker/startup.rb +3 -0
- data/lib/samagotchi/worker/wake_turns.rb +20 -5
- data/lib/samagotchi/worker.rb +12 -6
- data/lib/samagotchi/worker_inbound/mail.rb +214 -0
- data/lib/samagotchi/worker_inbound.rb +43 -46
- data/lib/samagotchi/worker_wakes.rb +26 -21
- metadata +39 -2
- data/lib/samagotchi/peer_sources.rb +0 -58
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
// A paired peer in the session bar:
|
|
2
|
-
//
|
|
3
|
-
// (paired, waiting, answered, can't send,
|
|
4
|
-
// popover (how chi and the peer reach each
|
|
1
|
+
// A paired peer in the session bar: one chip per peer the peers model
|
|
2
|
+
// (peer_data.js, GET …/peers: the session's mailbox) names as paired, that
|
|
3
|
+
// says how the pairing stands (paired, waiting, answered, can't send,
|
|
4
|
+
// answers only), and that chip's popover (how chi and the peer reach each
|
|
5
|
+
// other, the last exchanges with what chi did with each message, View
|
|
5
6
|
// latest answer, Status card, Unpair). Pure templates and models: the
|
|
6
|
-
// chips' controller (context_chips.js) draws them
|
|
7
|
-
//
|
|
8
|
-
//
|
|
7
|
+
// chips' controller (context_chips.js) draws them. docs/internals/
|
|
8
|
+
// web-frontend.md, The peer exchange. Its own module: new exports go in new
|
|
9
|
+
// modules.
|
|
9
10
|
|
|
10
11
|
import { html, nothing } from "./vendor/lit-html.js";
|
|
11
12
|
import { spanText, verdictWord } from "./peer_exchange.js";
|
|
@@ -18,46 +19,29 @@ export const peerKey = (name) => `peer:${name}`;
|
|
|
18
19
|
// How many exchanges the popover lists (the newest).
|
|
19
20
|
export const POPOVER_EXCHANGES = 8;
|
|
20
21
|
|
|
21
|
-
//
|
|
22
|
-
//
|
|
23
|
-
//
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
const rest = [];
|
|
28
|
-
for (const row of rows || []) {
|
|
29
|
-
if (!row) continue;
|
|
30
|
-
if (!row.peer || !["inbox", "outbox"].includes(row.leg)) {
|
|
31
|
-
rest.push(row);
|
|
32
|
-
continue;
|
|
33
|
-
}
|
|
34
|
-
let group = peers.get(row.peer);
|
|
35
|
-
if (!group) {
|
|
36
|
-
group = { key: peerKey(row.peer), name: row.peer, label: row.label || row.peer, inbox: null, outbox: null };
|
|
37
|
-
peers.set(row.peer, group);
|
|
38
|
-
}
|
|
39
|
-
group[row.leg] = row;
|
|
40
|
-
}
|
|
41
|
-
return { peers: [...peers.values()], rest };
|
|
22
|
+
// The peers model's rows as chips' groups: {key, name, label, peer} for each
|
|
23
|
+
// paired one (its link.json: paired_at), in the model's order. A peer that
|
|
24
|
+
// was unpaired keeps its messages but has no chip.
|
|
25
|
+
export function peerGroups(peers) {
|
|
26
|
+
return (peers || []).filter((peer) => peer?.name && peer.paired_at)
|
|
27
|
+
.map((peer) => ({ key: peerKey(peer.name), name: peer.name, label: peer.label || peer.name, peer }));
|
|
42
28
|
}
|
|
43
29
|
|
|
44
|
-
// A
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
// (a group whose legs are all muted is gone from the bar, as a muted row is).
|
|
48
|
-
export const peerShown = (group) => !!(live(group.inbox) || live(group.outbox));
|
|
30
|
+
// A message no turn has read yet: no receipt (chi hasn't taken it), or one
|
|
31
|
+
// that holds it as a note (held, noted).
|
|
32
|
+
const unread = (message) => !message?.receipt || ["held", "noted"].includes(message.receipt.state);
|
|
49
33
|
|
|
50
34
|
// The sends among +exchanges+ (newest first): an unclaimed answer has no id.
|
|
51
35
|
const sends = (exchanges) => (exchanges || []).filter((ex) => ex?.id);
|
|
52
36
|
const ASKS = ["validate", "question"];
|
|
37
|
+
const messagesOf = (group) => group?.peer?.messages || [];
|
|
53
38
|
|
|
54
39
|
// The newest send failed and the peer hasn't paired again since: pairing
|
|
55
|
-
// again
|
|
56
|
-
// fetched_at past the failed send's time.
|
|
40
|
+
// again writes a new link (paired_at) after the failed send's time.
|
|
57
41
|
export function cantSend(group, exchanges) {
|
|
58
42
|
const newest = sends(exchanges)[0];
|
|
59
43
|
if (newest?.state !== "failed") return false;
|
|
60
|
-
const paired = Date.parse(
|
|
44
|
+
const paired = Date.parse(group?.peer?.paired_at || "");
|
|
61
45
|
const failed = Date.parse(newest.at || "");
|
|
62
46
|
return !(paired > failed);
|
|
63
47
|
}
|
|
@@ -67,26 +51,24 @@ const since = (iso, now) => {
|
|
|
67
51
|
};
|
|
68
52
|
|
|
69
53
|
// The chip of +group+: the first state that applies.
|
|
70
|
-
// - answers-only: no
|
|
54
|
+
// - answers-only: the pairing has no address (chi can't send);
|
|
71
55
|
// - cant-send: the newest send failed (the transport refused it);
|
|
72
56
|
// - waiting: the newest validate or question send has no answer (its age the send's);
|
|
73
|
-
// - answered:
|
|
57
|
+
// - answered: its newest message no turn has read (its age the message's), with the dot;
|
|
74
58
|
// - paired otherwise.
|
|
75
|
-
// +exchanges+: the peer's from GET …/peers, newest first
|
|
76
|
-
// an older chi web, a peer the read doesn't name: nothing then says failed).
|
|
59
|
+
// +exchanges+: the peer's from GET …/peers, newest first.
|
|
77
60
|
// @return {key, name, state, label, title, dot}
|
|
78
61
|
export function peerChipModel(group, exchanges = [], now = Date.now()) {
|
|
79
|
-
const inbox = live(group.inbox);
|
|
80
|
-
const outbox = live(group.outbox);
|
|
81
62
|
const all = sends(exchanges);
|
|
82
63
|
const ask = all.find((ex) => ASKS.includes(ex.kind));
|
|
64
|
+
const newest = messagesOf(group)[0];
|
|
83
65
|
let state = "paired";
|
|
84
66
|
let words = "paired";
|
|
85
|
-
let why = "paired: chi can send to it and it answers
|
|
86
|
-
if (!
|
|
67
|
+
let why = "paired: chi can send to it and it answers with chi peer say";
|
|
68
|
+
if (!group.peer?.can_send) {
|
|
87
69
|
state = "answers-only";
|
|
88
70
|
words = "answers only";
|
|
89
|
-
why = "no
|
|
71
|
+
why = "paired with no address chi can reach: it answers, chi can't send to it";
|
|
90
72
|
} else if (cantSend(group, exchanges)) {
|
|
91
73
|
state = "cant-send";
|
|
92
74
|
words = "can't send";
|
|
@@ -97,11 +79,11 @@ export function peerChipModel(group, exchanges = [], now = Date.now()) {
|
|
|
97
79
|
words = age === null ? "waiting" : `waiting ${spanText(age)}`;
|
|
98
80
|
why = [ask.kind, ask.title].filter(Boolean).join(" · ") || "a send";
|
|
99
81
|
why = `waiting for its answer to ${why}`;
|
|
100
|
-
} else if (
|
|
101
|
-
const age = since(
|
|
82
|
+
} else if (newest && unread(newest)) {
|
|
83
|
+
const age = since(newest.at, now);
|
|
102
84
|
state = "answered";
|
|
103
85
|
words = age === null ? "answered" : `answered ${spanText(age)}`;
|
|
104
|
-
why = "
|
|
86
|
+
why = "a message the agent hasn't read yet";
|
|
105
87
|
}
|
|
106
88
|
return { key: group.key, name: group.key, state, label: `${group.label} · ${words}`, title: why,
|
|
107
89
|
dot: state === "answered", peer: true };
|
|
@@ -109,58 +91,62 @@ export function peerChipModel(group, exchanges = [], now = Date.now()) {
|
|
|
109
91
|
|
|
110
92
|
// An exchange's state as the popover's list says it: "waiting 2m",
|
|
111
93
|
// "changes · 11m", "ok · 4m", "answered · 4m", "no answer expected",
|
|
112
|
-
// "not sent"
|
|
94
|
+
// "not sent", then what chi did with the answer's message (+words+, the
|
|
95
|
+
// server's: "held · peer turns", "read", "completed"); +kind+ its tint.
|
|
113
96
|
// @return {text, kind}
|
|
114
|
-
export function exchangeState(ex, now = Date.now()) {
|
|
97
|
+
export function exchangeState(ex, now = Date.now(), words = null) {
|
|
115
98
|
if (ex.state === "failed") return { text: "not sent", kind: "failed" };
|
|
116
99
|
if (ex.state === "none_expected") return { text: "no answer expected", kind: "none" };
|
|
117
100
|
if (ex.state === "waiting") {
|
|
118
101
|
const age = since(ex.at, now);
|
|
119
102
|
return { text: age === null ? "waiting" : `waiting ${spanText(age)}`, kind: "waiting" };
|
|
120
103
|
}
|
|
121
|
-
const took = ex.answer?.at ? since(ex.at, Date.parse(ex.answer.at)) : null;
|
|
104
|
+
const took = ex.id && ex.answer?.at ? since(ex.at, Date.parse(ex.answer.at)) : null;
|
|
122
105
|
const verdict = verdictWord(ex.answer?.verdict);
|
|
123
106
|
const word = verdict || "answered";
|
|
124
|
-
|
|
107
|
+
const text = [took === null ? word : `${word} · ${spanText(took)}`, words].filter(Boolean).join(" · ");
|
|
108
|
+
return { text, kind: verdict || "answered" };
|
|
125
109
|
}
|
|
126
110
|
|
|
127
111
|
// The popover's inside for +group+ (scene 2). +exchanges+ newest first;
|
|
128
|
-
// +text+ the
|
|
129
|
-
// +status+ the status card's command ("/claude") when the session
|
|
130
|
-
// else null; +drawn+(id) whether that send's row is in the
|
|
131
|
-
// (its list row then jumps to it).
|
|
112
|
+
// +text+ the newest message's text once View latest answer read it (null
|
|
113
|
+
// before); +status+ the status card's command ("/claude") when the session
|
|
114
|
+
// has it, else null; +drawn+(id) whether that send's row is in the
|
|
115
|
+
// conversation (its list row then jumps to it).
|
|
132
116
|
export function peerPopoverTemplate(group, exchanges = [], { text = null, status = null, drawn = () => false, now = Date.now() } = {}) {
|
|
133
|
-
const
|
|
134
|
-
const
|
|
117
|
+
const peer = group.peer || {};
|
|
118
|
+
const messages = messagesOf(group);
|
|
119
|
+
const words = new Map(messages.map((m) => [String(m.serial), m.words]));
|
|
135
120
|
const all = sends(exchanges);
|
|
136
121
|
const lastSent = all.find((ex) => ex.state !== "failed");
|
|
137
|
-
const
|
|
138
|
-
|
|
139
|
-
:
|
|
122
|
+
const failing = peer.can_send && cantSend(group, exchanges);
|
|
123
|
+
const sendsLine = !peer.can_send
|
|
124
|
+
? "can't: it paired with no address chi can reach"
|
|
125
|
+
: failing
|
|
140
126
|
? `the last send didn't go through: pair again from ${group.label}`
|
|
141
|
-
: [`to
|
|
142
|
-
const answersLine =
|
|
143
|
-
|
|
144
|
-
: "no outbox attached";
|
|
145
|
-
const list = all.slice(0, POPOVER_EXCHANGES);
|
|
127
|
+
: [`to ${peer.address}`, lastSent ? `last sent ${wallClock(lastSent.at, now)}` : ""].filter(Boolean).join(" · ");
|
|
128
|
+
const answersLine = messages.length ? `with chi peer say · last ${wallClock(messages[0].at, now)}` : "nothing yet";
|
|
129
|
+
const list = (exchanges || []).filter((ex) => ex?.id || ex?.answer).slice(0, POPOVER_EXCHANGES);
|
|
146
130
|
const rowInside = (ex) => {
|
|
147
|
-
const
|
|
148
|
-
|
|
149
|
-
|
|
131
|
+
const serial = ex.answer?.serial;
|
|
132
|
+
const state = exchangeState(ex, now, serial === undefined || serial === null ? null : words.get(String(serial)));
|
|
133
|
+
const what = ex.id ? [ex.kind, ex.title].filter(Boolean).join(" · ") || "a send"
|
|
134
|
+
: [`#${serial ?? "?"}`, ex.answer?.summary].filter(Boolean).join(" · ");
|
|
135
|
+
return html`<span class="pp-x-at">${wallClock(ex.id ? ex.at : ex.answer?.at || ex.at, now)}</span><span class="pp-x-what">${
|
|
136
|
+
what}</span><span class=${`pp-x-state ${state.kind}`}>${state.text}</span>`;
|
|
150
137
|
};
|
|
151
|
-
const legs = [group.inbox, group.outbox].filter(Boolean);
|
|
152
138
|
return html`<div class="ctx-pop-head"><strong>${group.label}</strong
|
|
153
139
|
><span class="ctx-pop-kind">paired peer</span
|
|
154
140
|
><button type="button" class="ctx-pop-close" data-act="close" aria-label="Close">×</button></div
|
|
155
|
-
><div class="peer-pop-legs"><div><span>chi sends</span><span class=${
|
|
141
|
+
><div class="peer-pop-legs"><div><span>chi sends</span><span class=${failing ? "warn" : ""}>${sendsLine}</span></div
|
|
156
142
|
><div><span>it answers</span><span>${answersLine}</span></div></div
|
|
157
|
-
>${list.length ? html`<div class="peer-pop-list"><div class="peer-pop-list-head">Exchanges</div>${list.map((ex) => (drawn(ex.id)
|
|
143
|
+
>${list.length ? html`<div class="peer-pop-list"><div class="peer-pop-list-head">Exchanges</div>${list.map((ex) => (ex.id && drawn(ex.id)
|
|
158
144
|
? html`<button type="button" class="peer-pop-x" data-act="jump" data-peer-id=${ex.id}>${rowInside(ex)}</button>`
|
|
159
145
|
: html`<div class="peer-pop-x">${rowInside(ex)}</div>`))}</div>` : nothing
|
|
160
146
|
}<pre class="ctx-pop-text" ?hidden=${text === null}>${text ?? ""}</pre
|
|
161
|
-
><div class="ctx-pop-actions">${
|
|
147
|
+
><div class="ctx-pop-actions">${messages.length && text === null ? html`<button type="button" data-act="view">View latest answer</button>` : nothing
|
|
162
148
|
}${status ? html`<button type="button" data-act="status">Status card (${status})</button>` : nothing
|
|
163
|
-
}
|
|
149
|
+
}<button type="button" class="ctx-pop-detach" data-act="unpair" title="End the pairing (its messages stay)">Unpair</button></div>`;
|
|
164
150
|
}
|
|
165
151
|
|
|
166
152
|
// The peer's status card command ("/claude") when the session's commands
|
|
@@ -1,15 +1,17 @@
|
|
|
1
1
|
// The open session's peers as the page knows them: GET …/peers (owner only,
|
|
2
|
-
// read from the session's files: its
|
|
3
|
-
// one small model of it that the conversation (peer_links.js) and
|
|
4
|
-
// the peer chips share. docs/internals/web-frontend.md, The peer exchange.
|
|
2
|
+
// read from the session's files: its mailbox, the receipts and the exchange
|
|
3
|
+
// log), and one small model of it that the conversation (peer_links.js) and
|
|
4
|
+
// the peer chips (context_chips.js, peer_chip.js) share. docs/internals/web-frontend.md, The peer exchange.
|
|
5
5
|
// Its own module: new exports go in new modules (web-frontend.md).
|
|
6
6
|
|
|
7
7
|
import { api } from "./data.js";
|
|
8
8
|
import { createLatest } from "./latest.js";
|
|
9
9
|
|
|
10
|
-
// GET /api/sessions/:id/peers → {peers: [{name, label,
|
|
11
|
-
//
|
|
12
|
-
//
|
|
10
|
+
// GET /api/sessions/:id/peers → {peers: [{name, label, paired_at, from,
|
|
11
|
+
// address, can_send, messages: [newest first, {serial, summary, re, at,
|
|
12
|
+
// wake, receipt, words}], exchanges: [newest first]}]}. An older chi web
|
|
13
|
+
// has no route: its 404 is an empty list here, on its own (any other
|
|
14
|
+
// failure throws).
|
|
13
15
|
export async function listPeers(id, opts = {}) {
|
|
14
16
|
try {
|
|
15
17
|
return await api(`/api/sessions/${encodeURIComponent(id)}/peers`, opts);
|
|
@@ -26,9 +28,9 @@ export const WAITING_TICK_MS = 60_000;
|
|
|
26
28
|
// last: 20): a full list may leave older sends out.
|
|
27
29
|
export const EXCHANGES_CAP = 20;
|
|
28
30
|
|
|
29
|
-
// The model. +sessionId+() the open session; +hasPeer+() whether
|
|
30
|
-
//
|
|
31
|
-
//
|
|
31
|
+
// The model. +sessionId+() the open session; +hasPeer+() whether to read
|
|
32
|
+
// at all (the page: always, GET …/peers is the peer chip's only feed):
|
|
33
|
+
// without, refresh asks nothing. +onChange+() after each read for the open
|
|
32
34
|
// session (also a failed one: what is drawn may follow it). +list+: the
|
|
33
35
|
// fetch (listPeers). +win+: focus and the waiting tick.
|
|
34
36
|
//
|
|
@@ -97,8 +99,8 @@ export function createPeers({ sessionId, hasPeer = () => false, onChange = () =>
|
|
|
97
99
|
}
|
|
98
100
|
|
|
99
101
|
// The send answer +serial+ of peer +name+ answers: the exchange its
|
|
100
|
-
// serial (its
|
|
101
|
-
// send's; else the send +re+ names (a second answer to one send is an
|
|
102
|
+
// serial (its mailbox message's, or an older outbox revision's: the
|
|
103
|
+
// note's) is in, when that one is a send's; else the send +re+ names (a second answer to one send is an
|
|
102
104
|
// unclaimed row of its own on the server); null for an answer no send
|
|
103
105
|
// claims.
|
|
104
106
|
function answer(name, serial, re = null) {
|
|
@@ -29,21 +29,24 @@ export function upTemplate(exchange) {
|
|
|
29
29
|
aria-label=${`${what}: go to the send`}>${what}<span aria-hidden="true"> ↑</span></button>`;
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
-
// "Read the full answer":
|
|
33
|
-
//
|
|
32
|
+
// "Read the full answer": not one whose bubble holds its text
|
|
33
|
+
// (data-peer-inline); for an older context answer, its peer's newest only
|
|
34
|
+
// (its outbox kept one text); for a mailbox message, each (it reads its own).
|
|
34
35
|
export function fullTemplate(newest) {
|
|
35
36
|
return newest ? html`<button type="button" class="peer-full" data-peer-full="1">Read the full answer</button>` : nothing;
|
|
36
37
|
}
|
|
37
38
|
|
|
38
39
|
// +peers+ the model (peer_data.js); +roots+() the elements a turn's rows can
|
|
39
40
|
// be in (the history and the stage); +running+() whether a turn runs;
|
|
40
|
-
// +showText+(source) opens that context source's text (
|
|
41
|
+
// +showText+(source) opens that context source's text, +showMessage+(name,
|
|
42
|
+
// serial) a peer's mailbox message (context_chips.js);
|
|
41
43
|
// +renderMarkdown+(text) a text's sanitized markdown HTML or null
|
|
42
44
|
// (markdown_place.js; none: the peer's text stays plain); +behavior+() the
|
|
43
45
|
// scroll's.
|
|
44
46
|
//
|
|
45
47
|
// @return {draw(), drawn(), hasPeer(), hasSend(id), showSend(id), reset()}
|
|
46
|
-
export function createPeerLinks({ peers, roots, running = () => false, showText = () => {},
|
|
48
|
+
export function createPeerLinks({ peers, roots, running = () => false, showText = () => {}, showMessage = () => {}, renderMarkdown = null,
|
|
49
|
+
behavior = smooth } = {}) {
|
|
47
50
|
const all = (selector) => roots().filter(Boolean).flatMap((root) => [...root.querySelectorAll(selector)]);
|
|
48
51
|
// The peer texts already asked for (each is asked once; a failed read
|
|
49
52
|
// keeps the plain text).
|
|
@@ -86,7 +89,8 @@ export function createPeerLinks({ peers, roots, running = () => false, showText
|
|
|
86
89
|
const up = note.querySelector(":scope > .peer-head > .peer-up-slot");
|
|
87
90
|
if (up) render(upTemplate(answerOf(note)), up);
|
|
88
91
|
const full = note.querySelector(":scope > .peer-foot > .peer-full-slot");
|
|
89
|
-
const
|
|
92
|
+
const mailbox = !!note.dataset.peerBox;
|
|
93
|
+
const last = mailbox || (newest.get(note.dataset.peerSource || "") === note && !!note.dataset.peerSource);
|
|
90
94
|
if (full) render(fullTemplate(last && !note.dataset.peerInline), full);
|
|
91
95
|
const text = note.querySelector(":scope > .peer-text[data-peer-md]:not(.markdown)");
|
|
92
96
|
if (text) renderText(text);
|
|
@@ -156,8 +160,9 @@ export function createPeerLinks({ peers, roots, running = () => false, showText
|
|
|
156
160
|
if (slot) toAnswer(slot);
|
|
157
161
|
} else if (jump?.dataset.peerJump === "send") toSend(jump);
|
|
158
162
|
else if (e.target.closest?.("[data-peer-full]")) {
|
|
159
|
-
const
|
|
160
|
-
if (
|
|
163
|
+
const note = e.target.closest(ANSWERS);
|
|
164
|
+
if (note?.dataset.peerBox) showMessage(note.dataset.peerName, note.dataset.peerSerial, note.dataset.peerBox);
|
|
165
|
+
else if (note?.dataset.peerSource) showText(note.dataset.peerSource);
|
|
161
166
|
}
|
|
162
167
|
}
|
|
163
168
|
for (const root of roots().filter(Boolean)) root.addEventListener("click", onClick);
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// A session's peer mailbox through chi web, for the peer chip
|
|
2
|
+
// (context_chips.js): one message's text, and Unpair. Owner only (chi web's
|
|
3
|
+
// …/peers routes). docs/internals/web-frontend.md, The peer exchange.
|
|
4
|
+
// Its own module: new exports go in new modules (web-frontend.md).
|
|
5
|
+
|
|
6
|
+
import { api } from "./data.js";
|
|
7
|
+
|
|
8
|
+
const base = (id, name) => `/api/sessions/${encodeURIComponent(id)}/peers/${encodeURIComponent(name)}`;
|
|
9
|
+
|
|
10
|
+
// GET …/peers/NAME/messages/SERIAL → {serial, text, summary, re, at, wake, receipt}.
|
|
11
|
+
export function getPeerMessage(id, name, serial, box = null, opts = {}) {
|
|
12
|
+
if (box && typeof box === "object") { opts = box; box = null; }
|
|
13
|
+
const query = box ? `?box=${encodeURIComponent(box)}` : "";
|
|
14
|
+
return api(`${base(id, name)}/messages/${encodeURIComponent(serial)}${query}`, opts);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// DELETE …/peers/NAME (Unpair) → {unpaired}: the pairing ends, its messages stay.
|
|
18
|
+
export function unpairPeer(id, name, opts = {}) {
|
|
19
|
+
return api(base(id, name), { ...opts, method: "DELETE" });
|
|
20
|
+
}
|
|
@@ -5,7 +5,9 @@
|
|
|
5
5
|
// classes `bubble note` and `turn-start`, data-opening-id, the time title):
|
|
6
6
|
// TURN_PROMPT (bubbles.js), card_stamps.js, stage_handoff.js OPENING,
|
|
7
7
|
// reload_check.js and revise_stack.js (its `.note-text`, the summary) read
|
|
8
|
-
// it as any wake note.
|
|
8
|
+
// it as any wake note. A mailbox message's note has a `box` in its peer
|
|
9
|
+
// hash (data-peer-box); an older context answer's has none. The ↑ to the
|
|
10
|
+
// send and "Read the full answer" are
|
|
9
11
|
// slots peer_links.js fills (data-peer-slot: history_shape.js leaves them
|
|
10
12
|
// out, so live and an offscreen reload read alike). A note whose peer hash
|
|
11
13
|
// says `inline` holds the peer's text (peer_text.js): the bubble shows it
|
|
@@ -52,7 +54,9 @@ export function peerNoteTemplate(note) {
|
|
|
52
54
|
const name = peer.label || peer.peer || "Your peer";
|
|
53
55
|
const cls = note.turn_start ? "bubble note turn-start peer-answer" : "bubble note peer-answer";
|
|
54
56
|
const opening = note.turn_start && note.turn_id ? String(note.turn_id) : nothing;
|
|
55
|
-
|
|
57
|
+
// A mailbox message's note (its peer hash has a box) reads its text by
|
|
58
|
+
// its number; an older context answer's by its outbox source.
|
|
59
|
+
const source = note.context_source || (peer.peer && !peer.box ? `${peer.peer}-outbox` : "");
|
|
56
60
|
const attr = (value) => (value === null || value === undefined || value === "" ? nothing : String(value));
|
|
57
61
|
const block = peer.inline ? peerBlock(note.content) : null;
|
|
58
62
|
// Sanitized on the server (MarkdownRenderer), as an answer's html.
|
|
@@ -61,5 +65,5 @@ export function peerNoteTemplate(note) {
|
|
|
61
65
|
: rendered ? html`<div class="peer-text markdown md-compact" data-peer-md=${block.text}>${unsafeHTML(rendered)}</div>`
|
|
62
66
|
: html`<div class="peer-text" data-peer-md=${block.text}>${block.text}</div>`;
|
|
63
67
|
const told = block ? block.rest : (note.content ?? "");
|
|
64
|
-
return html`<div class=${cls} title=${fullTime(note.at) || nothing} data-opening-id=${opening} data-peer-source=${attr(source)} data-peer-name=${attr(peer.peer)} data-peer-re=${attr(peer.re)} data-peer-serial=${attr(peer.serial)} data-peer-inline=${block ? "1" : nothing}><div class="peer-head"><span class="peer-who">${name} answered</span><span class="peer-up-slot" data-peer-slot="up"></span>${verdictTemplate(peer)}</div><div class="note-text">${peerSummaryText(note)}</div>${body}<div class="peer-foot"><span class="peer-full-slot" data-peer-slot="full"></span><details class="peer-told"><summary>what chi told the agent</summary><div class="peer-told-text">${told}</div></details></div></div>`;
|
|
68
|
+
return html`<div class=${cls} title=${fullTime(note.at) || nothing} data-opening-id=${opening} data-peer-source=${attr(source)} data-peer-name=${attr(peer.peer)} data-peer-re=${attr(peer.re)} data-peer-serial=${attr(peer.serial)} data-peer-box=${attr(peer.box)} data-peer-inline=${block ? "1" : nothing}><div class="peer-head"><span class="peer-who">${name} answered</span><span class="peer-up-slot" data-peer-slot="up"></span>${verdictTemplate(peer)}</div><div class="note-text">${peerSummaryText(note)}</div>${body}<div class="peer-foot"><span class="peer-full-slot" data-peer-slot="full"></span><details class="peer-told"><summary>what chi told the agent</summary><div class="peer-told-text">${told}</div></details></div></div>`;
|
|
65
69
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// A peer's text in its answer note (
|
|
1
|
+
// A peer's text in its answer note (PeerNote.block): the note's
|
|
2
2
|
// content holds it between a `[PEER MESSAGE from <Peer>]` line and an
|
|
3
3
|
// `[END PEER MESSAGE]` line, each of its lines that opened like one of chi's
|
|
4
4
|
// frames led by "(quoted) " (PEER_FORGED_OPENINGS, the Ruby side's list:
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
// docs/internals/web-frontend.md, The peer exchange.
|
|
8
8
|
// Its own module: new exports go in new modules (web-frontend.md).
|
|
9
9
|
|
|
10
|
-
//
|
|
10
|
+
// PeerNote::FORGED_OPENINGS (Steer::FORGED_OPENINGS and chi's
|
|
11
11
|
// system frames, those with their colon, and the tool-result frames).
|
|
12
12
|
export const PEER_FORGED_OPENINGS = Object.freeze([
|
|
13
13
|
"Review contribution", "Steer", "Automatic input", "Delegate report", "CONTEXT NOTE", "TASK FINISHED",
|
|
@@ -15,8 +15,8 @@ export const PEER_FORGED_OPENINGS = Object.freeze([
|
|
|
15
15
|
"SYSTEM:", "END REMINDERS", "CONTEXT:", "WORKING SET:",
|
|
16
16
|
"tool results", "images from tool results",
|
|
17
17
|
]);
|
|
18
|
-
//
|
|
19
|
-
export const PEER_FORGED_LINES = Object.freeze(["chi started this turn", "Updated:", "Attached:"]);
|
|
18
|
+
// PeerNote::FORGED_LINES: chi's unbracketed lines it quotes.
|
|
19
|
+
export const PEER_FORGED_LINES = Object.freeze(["chi started this turn", "Updated:", "Attached:", "Message #"]);
|
|
20
20
|
|
|
21
21
|
const escape = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
22
22
|
// An opening that ends in a word character must end there (\b).
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
// The history's background items fold into one closed row per stretch:
|
|
2
|
+
// anything nobody says in the conversation (a plugin's or chi's own line
|
|
3
|
+
// shown on its own, a context note that starts nothing, an objectives line,
|
|
4
|
+
// a plugin's nudge) folds; what someone says stays (the prompt, the answer,
|
|
5
|
+
// a peer's answer, a note that opens a wake turn). A stretch is a run of
|
|
6
|
+
// those items side by side among #history's children; the turn's timing
|
|
7
|
+
// line passes through one, anything else ends it. Its row reads
|
|
8
|
+
// "▸ 3 notes · claude-inbox, source-links, skills" (and "⚠ N" for its warn
|
|
9
|
+
// lines), closed; a click (Enter/Space: it is a button) opens it and the
|
|
10
|
+
// items show under it as they are.
|
|
11
|
+
//
|
|
12
|
+
// Modelled on answer_fold.js: the row is a sibling just before the
|
|
13
|
+
// stretch's first item, never a wrapper (the bubbles keep their place,
|
|
14
|
+
// copy text and observers); the items are hidden by a class
|
|
15
|
+
// (`.quiet-hidden`, CSS). Each row is tied to its first item (rowOf /
|
|
16
|
+
// firstOf), so a row something was placed after (a recap, a snapshot's
|
|
17
|
+
// card) goes back before its item, open or not. The open state is the
|
|
18
|
+
// row's class only, never stored: a re-render draws the rows closed.
|
|
19
|
+
//
|
|
20
|
+
// One observer on #history (its own children only) settles every path that
|
|
21
|
+
// fills it; a settle that changes nothing writes nothing (the other
|
|
22
|
+
// observers on #history see class and subtree writes).
|
|
23
|
+
//
|
|
24
|
+
// A module of its own: static JS is cached at unversioned URLs, and a new
|
|
25
|
+
// export on an old module would blank the page.
|
|
26
|
+
import { html, render, nothing } from "./vendor/lit-html.js";
|
|
27
|
+
|
|
28
|
+
// What folds: a direct child of #history that is one of these.
|
|
29
|
+
export const QUIET = ".bubble.hook-notice:not(.empty-answer), .bubble.note:not(.turn-start):not(.peer-answer), .bubble.steer-row";
|
|
30
|
+
// What a stretch passes over (it stays visible).
|
|
31
|
+
export const PASS = ".turn-timing";
|
|
32
|
+
export const ROW_CLASS = "quiet-notes";
|
|
33
|
+
export const HIDDEN_CLASS = "quiet-hidden";
|
|
34
|
+
|
|
35
|
+
const ARRIVE = "ts-arrive";
|
|
36
|
+
const ROW = `.${ROW_CLASS}`;
|
|
37
|
+
|
|
38
|
+
// The stretches over +children+ (an element list, in order): [{ items }].
|
|
39
|
+
// Pure.
|
|
40
|
+
export function quietRuns(children) {
|
|
41
|
+
const runs = [];
|
|
42
|
+
let run = null;
|
|
43
|
+
for (const el of children) {
|
|
44
|
+
if (el.nodeType !== 1 || el.matches(ROW) || el.matches(PASS)) continue;
|
|
45
|
+
if (!el.matches(QUIET)) {
|
|
46
|
+
run = null;
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
if (!run) runs.push((run = { items: [] }));
|
|
50
|
+
run.items.push(el);
|
|
51
|
+
}
|
|
52
|
+
return runs;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const squash = (text) => String(text ?? "").replace(/\s+/g, " ").trim();
|
|
56
|
+
|
|
57
|
+
// One item's name on the row.
|
|
58
|
+
function itemName(el) {
|
|
59
|
+
if (el.matches(".objectives-entry")) return "objectives";
|
|
60
|
+
if (el.matches(".steer-row")) {
|
|
61
|
+
const said = squash(el.querySelector("summary")?.textContent);
|
|
62
|
+
return said.endsWith(" nudged the model") ? said.slice(0, -" nudged the model".length) : "nudge";
|
|
63
|
+
}
|
|
64
|
+
if (el.matches(".hook-notice")) {
|
|
65
|
+
// "<bundle or hook>: text"; chi's own lines ("↻ empty answer, asking
|
|
66
|
+
// again") name no one.
|
|
67
|
+
const text = squash(el.textContent);
|
|
68
|
+
const at = text.indexOf(": ");
|
|
69
|
+
const who = at > 0 ? text.slice(0, at) : "";
|
|
70
|
+
return /^[A-Za-z0-9][\w./-]*$/.test(who) ? who : "chi";
|
|
71
|
+
}
|
|
72
|
+
const line = squash(el.querySelector(".note-line")?.textContent) || "note";
|
|
73
|
+
if (line === "note") return "context";
|
|
74
|
+
const from = line.replace(/^note from /, "").replace(/^context /, "");
|
|
75
|
+
// Another session's note (ContextNote.label): "session <id6> (<~cwd>)".
|
|
76
|
+
const session = /^(session \S+) \(.*\)$/.exec(from);
|
|
77
|
+
return session ? session[1] : from;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// A row's words: how many items, their names (unique, in order) and how
|
|
81
|
+
// many are warnings (the `warn` class, never the text). Pure.
|
|
82
|
+
export function rowLabel(items) {
|
|
83
|
+
const names = [];
|
|
84
|
+
let warn = 0;
|
|
85
|
+
for (const el of items) {
|
|
86
|
+
const name = itemName(el);
|
|
87
|
+
if (!names.includes(name)) names.push(name);
|
|
88
|
+
if (el.classList.contains("warn")) warn += 1;
|
|
89
|
+
}
|
|
90
|
+
return { count: items.length, names, warn };
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export function rowTemplate({ count, names, warn }) {
|
|
94
|
+
return html`<span class="qn-count">${count} ${count === 1 ? "note" : "notes"}</span><span class="qn-names">· ${names.join(", ")}</span>${warn > 0 ? html`<span class="qn-warn">⚠ ${warn}</span>` : nothing}`;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// +el+ hidden in a closed row: that row opens. Nothing otherwise. A jump
|
|
98
|
+
// goes through history_reveal.js revealInHistory, which calls this.
|
|
99
|
+
export function revealQuiet(el) {
|
|
100
|
+
const hidden = el?.closest?.(`.${HIDDEN_CLASS}`);
|
|
101
|
+
if (!hidden) return;
|
|
102
|
+
let row = hidden.previousElementSibling;
|
|
103
|
+
while (row && !row.matches(ROW)) row = row.previousElementSibling;
|
|
104
|
+
if (row && !row.classList.contains("open")) row.click();
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// @param historyEl the history (#history)
|
|
108
|
+
// @return {settle()} (the observer runs it; specs call it)
|
|
109
|
+
export function installQuietNotes({ historyEl }) {
|
|
110
|
+
// Each stretch's first item's row, and each row's first item.
|
|
111
|
+
const rowOf = new WeakMap();
|
|
112
|
+
const firstOf = new WeakMap();
|
|
113
|
+
// The words a row was last drawn with.
|
|
114
|
+
const drawn = new WeakMap();
|
|
115
|
+
|
|
116
|
+
function makeRow(first) {
|
|
117
|
+
const row = historyEl.ownerDocument.createElement("button");
|
|
118
|
+
row.type = "button";
|
|
119
|
+
row.className = ROW_CLASS;
|
|
120
|
+
row.setAttribute("aria-expanded", "false");
|
|
121
|
+
// The stretch's arrival plays on its row (its items are hidden).
|
|
122
|
+
if (first.classList.contains(ARRIVE)) {
|
|
123
|
+
row.classList.add(ARRIVE);
|
|
124
|
+
row.addEventListener("animationend", () => row.classList.remove(ARRIVE), { once: true });
|
|
125
|
+
}
|
|
126
|
+
return row;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// A row already in the stretch's span (between its items, or just before
|
|
130
|
+
// its first) that no other stretch took: the stretch's first item changed.
|
|
131
|
+
function rowInSpan(items, taken) {
|
|
132
|
+
const last = items[items.length - 1];
|
|
133
|
+
const before = items[0].previousElementSibling;
|
|
134
|
+
if (before?.matches(ROW) && firstOf.has(before) && !taken.has(before)) return before;
|
|
135
|
+
for (let el = items[0]; el && el !== last; el = el.nextElementSibling) {
|
|
136
|
+
if (el.matches(ROW) && firstOf.has(el) && !taken.has(el)) return el;
|
|
137
|
+
}
|
|
138
|
+
return null;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function drawRow(row, items) {
|
|
142
|
+
const label = rowLabel(items);
|
|
143
|
+
const words = JSON.stringify(label);
|
|
144
|
+
if (drawn.get(row) !== words) {
|
|
145
|
+
render(rowTemplate(label), row);
|
|
146
|
+
drawn.set(row, words);
|
|
147
|
+
}
|
|
148
|
+
const open = row.classList.contains("open");
|
|
149
|
+
const expanded = open ? "true" : "false";
|
|
150
|
+
if (row.getAttribute("aria-expanded") !== expanded) row.setAttribute("aria-expanded", expanded);
|
|
151
|
+
if (row.classList.contains("warn") !== label.warn > 0) row.classList.toggle("warn", label.warn > 0);
|
|
152
|
+
for (const item of items) {
|
|
153
|
+
if (item.classList.contains(HIDDEN_CLASS) === !open) continue;
|
|
154
|
+
item.classList.toggle(HIDDEN_CLASS, !open);
|
|
155
|
+
// A hidden element never fires animationend: its slide-up would play
|
|
156
|
+
// the first time the row opens.
|
|
157
|
+
if (!open && item.classList.contains(ARRIVE)) item.classList.remove(ARRIVE);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function settle() {
|
|
162
|
+
const taken = new Set();
|
|
163
|
+
const shown = new Set();
|
|
164
|
+
for (const { items } of quietRuns(historyEl.children)) {
|
|
165
|
+
const first = items[0];
|
|
166
|
+
let row = rowOf.get(first);
|
|
167
|
+
if (!row || row.parentElement !== historyEl || taken.has(row)) row = rowInSpan(items, taken);
|
|
168
|
+
if (!row) row = makeRow(first);
|
|
169
|
+
if (firstOf.get(row) !== first) {
|
|
170
|
+
firstOf.set(row, first);
|
|
171
|
+
rowOf.set(first, row);
|
|
172
|
+
}
|
|
173
|
+
if (first.previousElementSibling !== row) historyEl.insertBefore(row, first);
|
|
174
|
+
taken.add(row);
|
|
175
|
+
drawRow(row, items);
|
|
176
|
+
for (const item of items) shown.add(item);
|
|
177
|
+
}
|
|
178
|
+
for (const row of historyEl.querySelectorAll(`:scope > ${ROW}`)) if (!taken.has(row)) row.remove();
|
|
179
|
+
// An item no stretch holds any more (it changed kind): shown again.
|
|
180
|
+
for (const el of historyEl.querySelectorAll(`:scope > .${HIDDEN_CLASS}`)) if (!shown.has(el)) el.classList.remove(HIDDEN_CLASS);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const ours = (n) => n.nodeType !== 1 || n.classList.contains(ROW_CLASS);
|
|
184
|
+
new MutationObserver((records) => {
|
|
185
|
+
if (records.some((r) => [...r.addedNodes, ...r.removedNodes].some((n) => !ours(n)))) settle();
|
|
186
|
+
}).observe(historyEl, { childList: true });
|
|
187
|
+
settle();
|
|
188
|
+
|
|
189
|
+
historyEl.addEventListener("click", (e) => {
|
|
190
|
+
const row = e.target.closest?.(ROW);
|
|
191
|
+
if (!row || row.parentElement !== historyEl || !firstOf.has(row)) return;
|
|
192
|
+
e.preventDefault();
|
|
193
|
+
const open = !row.classList.contains("open");
|
|
194
|
+
row.classList.toggle("open", open);
|
|
195
|
+
settle();
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
return { settle };
|
|
199
|
+
}
|