samagotchi 0.192.0 → 0.193.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 +52 -1
- data/docs/architecture.md +3 -3
- data/docs/bundles.md +45 -28
- data/docs/cli.md +22 -8
- data/docs/configuration.md +4 -3
- data/docs/context.md +8 -5
- data/docs/guardrails.md +9 -4
- data/docs/internals/bridge.md +3 -1
- data/docs/internals/bundles.md +2 -2
- data/docs/internals/pages.md +4 -4
- data/docs/internals/participants.md +5 -3
- data/docs/internals/peers.md +75 -22
- data/docs/internals/prompt-caching.md +2 -1
- data/docs/internals/remote-sessions.md +6 -0
- data/docs/internals/session-lifecycle.md +3 -2
- data/docs/internals/web-frontend.md +91 -19
- data/docs/internals/working-set.md +5 -1
- data/docs/peers.md +71 -22
- data/docs/plugins.md +7 -4
- data/docs/releasing.md +30 -2
- data/docs/scheduling.md +3 -2
- data/docs/sessions.md +1 -1
- data/docs/testing.md +35 -3
- data/docs/thin-clients.md +27 -11
- data/lib/samagotchi/bundles/claude-peer/manifest.yml +2 -2
- data/lib/samagotchi/bundles/claude-peer/scripts/chi-peer-SKILL.md +33 -17
- data/lib/samagotchi/bundles/codex-peer/manifest.yml +2 -2
- data/lib/samagotchi/bundles/codex-peer/scripts/chi-peer-SKILL.md +34 -17
- data/lib/samagotchi/bundles/coordinator/manifest.yml +2 -2
- data/lib/samagotchi/bundles/coordinator/skill_coordinator.md +31 -0
- data/lib/samagotchi/bundles/mcp/manifest.yml +2 -2
- data/lib/samagotchi/bundles/mcp/plugin.rb +0 -7
- data/lib/samagotchi/bundles/system/manifest.yml +1 -1
- data/lib/samagotchi/compat.rb +8 -1
- data/lib/samagotchi/config.rb +4 -0
- data/lib/samagotchi/continue_offer.rb +13 -1
- data/lib/samagotchi/delivery.rb +2 -2
- data/lib/samagotchi/engine/models.rb +17 -0
- data/lib/samagotchi/engine/plugins.rb +2 -0
- data/lib/samagotchi/guardrails/pair_boundary.rb +49 -18
- data/lib/samagotchi/jsonl_store.rb +13 -2
- data/lib/samagotchi/llm/anthropic_messages.rb +11 -0
- data/lib/samagotchi/llm/openai_chat.rb +5 -0
- data/lib/samagotchi/llm/prompt_cache.rb +5 -0
- data/lib/samagotchi/pages/launch.rb +1 -1
- data/lib/samagotchi/peer_cold.rb +92 -0
- data/lib/samagotchi/peer_command.rb +24 -9
- data/lib/samagotchi/peer_continue.rb +32 -16
- data/lib/samagotchi/peer_exchanges.rb +7 -0
- data/lib/samagotchi/peer_mailbox.rb +21 -4
- data/lib/samagotchi/peer_receipts.rb +33 -6
- data/lib/samagotchi/peer_say.rb +34 -16
- data/lib/samagotchi/plugin/api.rb +2 -1
- data/lib/samagotchi/plugin/context.rb +13 -1
- data/lib/samagotchi/plugin/peer.rb +118 -53
- data/lib/samagotchi/plugin/peers.rb +13 -6
- data/lib/samagotchi/session_access/local.rb +98 -37
- data/lib/samagotchi/session_access/remote_peers.rb +3 -3
- data/lib/samagotchi/session_chain/links.rb +8 -3
- data/lib/samagotchi/session_continue.rb +7 -6
- data/lib/samagotchi/session_manager/spawn.rb +6 -1
- data/lib/samagotchi/session_stores.rb +16 -11
- data/lib/samagotchi/system_one/runs.rb +5 -2
- data/lib/samagotchi/testing/boundary.rb +0 -1
- data/lib/samagotchi/testing/host.rb +1 -0
- data/lib/samagotchi/testing/recorder.rb +3 -0
- data/lib/samagotchi/version.rb +1 -1
- data/lib/samagotchi/web/peer_routes.rb +10 -5
- data/lib/samagotchi/web/public/agent_rows.js +206 -0
- data/lib/samagotchi/web/public/agent_runs.js +595 -0
- data/lib/samagotchi/web/public/answer_fold.js +41 -44
- data/lib/samagotchi/web/public/app.js +3 -3
- data/lib/samagotchi/web/public/block_fix.js +1 -1
- data/lib/samagotchi/web/public/cards_place.js +6 -4
- data/lib/samagotchi/web/public/history_reveal.js +5 -2
- data/lib/samagotchi/web/public/index.html +38 -2
- data/lib/samagotchi/web/public/page_view.js +1 -1
- data/lib/samagotchi/web/public/pages.css +1 -7
- data/lib/samagotchi/web/public/peer_mailbox_api.js +2 -1
- data/lib/samagotchi/web/public/peer_note.js +4 -2
- data/lib/samagotchi/web/public/peer_row.js +37 -0
- data/lib/samagotchi/web/public/quiet_notes.js +12 -1
- data/lib/samagotchi/web/public/revise_stack.js +5 -2
- data/lib/samagotchi/web/public/send_turn.js +11 -3
- data/lib/samagotchi/web/public/turn_block.js +4 -0
- data/lib/samagotchi/worker/mail.rb +11 -1
- data/lib/samagotchi/worker.rb +7 -5
- data/lib/samagotchi/worker_inbound/mail.rb +34 -6
- data/lib/samagotchi/worker_inbound.rb +11 -3
- data/lib/samagotchi/worker_link.rb +13 -1
- data/lib/samagotchi/worker_wakes.rb +33 -5
- metadata +5 -2
- data/lib/samagotchi/web/public/pages_group.js +0 -67
|
@@ -0,0 +1,595 @@
|
|
|
1
|
+
// The one owner of what agents write to each other and of the history's
|
|
2
|
+
// background items: delegate reports and peer answers (agent_rows.js draws
|
|
3
|
+
// their rows), a finished turn's steps block in a run (STEPS) and the quiet
|
|
4
|
+
// items (quiet_notes.js QUIET: a plugin's or chi's own line, a context note
|
|
5
|
+
// that starts nothing, an objectives line, a nudge). Only this module
|
|
6
|
+
// writes their closed state: a report's or a peer answer's `.folded` (and
|
|
7
|
+
// `.under-row`), their `.quiet-hidden`, a block's `.in-line`, and the state
|
|
8
|
+
// of their rows and lines.
|
|
9
|
+
//
|
|
10
|
+
// Three levels of the same content (docs/internals/web-frontend.md, The notes
|
|
11
|
+
// rows and the run line): a segment in a shared line, a row, the full text.
|
|
12
|
+
// - A report or a peer answer in #history, finished and keyed (answer_fold.js
|
|
13
|
+
// answerKey; a live merged report has none until a reload: it stays open
|
|
14
|
+
// with no row, and ends a run), gets a row: a sibling just before its
|
|
15
|
+
// bubble, closed by default, opened by hand kept per session in the fold
|
|
16
|
+
// store's `open` list (answer_fold.js readOpened / writeOpened, the same
|
|
17
|
+
// keys and cap). A tap opens or closes it, animated unless the viewer
|
|
18
|
+
// prefers no motion. A peer answer's bubble under its row is `.under-row`
|
|
19
|
+
// (CSS drops its own head and summary, which the row says).
|
|
20
|
+
// - A run is those items and the quiet items side by side among #history's
|
|
21
|
+
// children (the turn's timing line passes through it; anything else ends
|
|
22
|
+
// it: a typed prompt, chi's answer, a card, a time divider). A run of
|
|
23
|
+
// quiet items only gets today's notes row (a button, "▸ 3 notes · names ⚠
|
|
24
|
+
// 1"); a run of one report or one peer answer only, its row; anything else
|
|
25
|
+
// one line of segments (a div, `.quiet-notes.agent-line`): "1 report ·
|
|
26
|
+
// Claude: changes · 3 notes ⚠ 1", one segment per kind (segments). A tap on
|
|
27
|
+
// a segment shows that kind's rows (or the notes, as they are) under the
|
|
28
|
+
// line, one segment open at a time; a tap on a row opens its text.
|
|
29
|
+
// - A finished turn's steps block (done, ended completed, handed off, with a
|
|
30
|
+
// call) in a run that holds a report or a peer answer is the segment "N
|
|
31
|
+
// calls" (the calls turn_block.js stamped, data-calls); under the line it
|
|
32
|
+
// is its own row, as today: its summary line with the working set's
|
|
33
|
+
// footprint (one tap deeper than outside a line), a tap on it opens the
|
|
34
|
+
// steps. A block alone or beside notes only keeps today's look; a
|
|
35
|
+
// cancelled, failed or running one ends a run. An objectives line after a
|
|
36
|
+
// block passes through a run, as the timing line does.
|
|
37
|
+
// The line's and the notes row's open state is their own (the line's
|
|
38
|
+
// `data-open`, the row's `open` class), never stored: a reload draws them
|
|
39
|
+
// closed, and a row opened by hand inside a line shows open once its segment
|
|
40
|
+
// is.
|
|
41
|
+
//
|
|
42
|
+
// Never a wrapper: every row and line is a sibling tied to its item by
|
|
43
|
+
// WeakMaps, so the bubbles stay direct children of #history where every
|
|
44
|
+
// walker expects them (the line has the notes row's class, the item rows
|
|
45
|
+
// answer_fold.js's: what passes over those passes over them); a row or line
|
|
46
|
+
// something was placed after goes back before its item. A folded or hidden
|
|
47
|
+
// item's arrival (`ts-arrive`) plays on its row or line: a hidden element
|
|
48
|
+
// never fires animationend, so it would slide in the first time it opens.
|
|
49
|
+
//
|
|
50
|
+
// One observer on #history settles every path that fills it (a reload's
|
|
51
|
+
// render, a live turn's end, the stage's hand-off); a settle that changes
|
|
52
|
+
// nothing writes nothing (the copy, answer-fold, Revise and objectives
|
|
53
|
+
// observers see class and subtree writes). A jump into a hidden item opens
|
|
54
|
+
// its segment (revealRun, history_reveal.js) and the item; the row follows
|
|
55
|
+
// the bubble's class.
|
|
56
|
+
//
|
|
57
|
+
// A module of its own: static JS is cached at unversioned URLs, and a new
|
|
58
|
+
// export on an old module would blank the page. It imports only names
|
|
59
|
+
// answer_fold.js and quiet_notes.js exported in the last release.
|
|
60
|
+
import { animateHeight, canAnimate } from "./details_motion.js";
|
|
61
|
+
import { answerKey, keepRowInView, readOpened, writeOpened } from "./answer_fold.js";
|
|
62
|
+
import { HIDDEN_CLASS, PASS, QUIET, ROW_CLASS as NOTES_CLASS, rowLabel, rowTemplate } from "./quiet_notes.js";
|
|
63
|
+
import { html, nothing, render } from "./vendor/lit-html.js";
|
|
64
|
+
import { verdictWord } from "./peer_exchange.js";
|
|
65
|
+
import { AGENT_ITEMS, PEERS, REPORTS, drawAgentRow, drawNoteRow, isPeer, makeAgentRow, makeNoteRow, peerName, peerVerdict } from "./agent_rows.js";
|
|
66
|
+
|
|
67
|
+
// The row class every report and peer answer row carries beside
|
|
68
|
+
// answer_fold.js's own `.answer-fold` (whose walkers pass over it); a row
|
|
69
|
+
// under a line also has IN_LINE.
|
|
70
|
+
export const AGENT_ROW = "agent-row";
|
|
71
|
+
export const LINE_CLASS = "agent-line";
|
|
72
|
+
const IN_LINE = "in-line";
|
|
73
|
+
const NOTES = `.${NOTES_CLASS}`;
|
|
74
|
+
const ARRIVE = "ts-arrive";
|
|
75
|
+
const UNDER_ROW = "under-row";
|
|
76
|
+
// Not done yet: still streaming.
|
|
77
|
+
const UNFINISHED = ".streaming";
|
|
78
|
+
// The classes this module writes on the items: a change to only these is
|
|
79
|
+
// its own (no settle); `folded` alone is a jump's open (the row follows).
|
|
80
|
+
const OWN = new Set([HIDDEN_CLASS, UNDER_ROW, ARRIVE, IN_LINE]);
|
|
81
|
+
// A finished turn's steps block that ended completed (turn_block.js): a
|
|
82
|
+
// cancelled or failed one stays out of every line, and so does a running
|
|
83
|
+
// one. A turn opened in the history mid-turn (stage_asking.js openTurn marks
|
|
84
|
+
// its block data-opened-turn, stage_handoff.js clears it) ends before its
|
|
85
|
+
// hand-off: it joins at the hand-off, not at its end.
|
|
86
|
+
const STEPS = '.turn-work.done[data-end="completed"]:not([data-opened-turn])';
|
|
87
|
+
// Its tool calls, as turn_block.js stamps them at the turn's end (none on a
|
|
88
|
+
// block an older cached turn_block.js drew: it stays out of the line).
|
|
89
|
+
const callsOf = (el) => Number(el.dataset.calls) || 0;
|
|
90
|
+
// revise_stack.js HIDDEN_CLASS: an item of a superseded version, folded.
|
|
91
|
+
const STACKED = "stack-hidden";
|
|
92
|
+
// Passed over by a run, never ending one, and left on view: the turn's
|
|
93
|
+
// timing line (quiet_notes.js PASS) and an objectives line after a block
|
|
94
|
+
// (objectives_moves.js), so a turn draws the same with or without a move.
|
|
95
|
+
const PASS_BY = `${PASS}, .obj-moves, .obj-nomove`;
|
|
96
|
+
|
|
97
|
+
// A report or a peer answer that takes part: finished and keyed.
|
|
98
|
+
const agentItem = (el) => el.matches(AGENT_ITEMS) && !el.matches(UNFINISHED) && !!answerKey(el);
|
|
99
|
+
// Ours: an agent row or a line / notes row (they never take part).
|
|
100
|
+
const ownRow = (el) => el.matches(`.${AGENT_ROW}, ${NOTES}`);
|
|
101
|
+
|
|
102
|
+
const plural = (n, word) => `${n} ${word}${n === 1 ? "" : "s"}`;
|
|
103
|
+
|
|
104
|
+
// The kinds of item a run holds, in the line's order (the owner: the order
|
|
105
|
+
// is free; the rank that truncates first at a narrow width is CSS's, by each
|
|
106
|
+
// segment's `seg-<kind>` class). Per kind: +selector+, the elements it is
|
|
107
|
+
// (the reveal for a jump and the observer's text watch match the union);
|
|
108
|
+
// +takes(el)+, whether one of those takes part now; +key(el)+, its segment
|
|
109
|
+
// ("report", "peer:<name>", "calls", "notes"); +label(items)+, its
|
|
110
|
+
// segment's words {label, warn}; +row+, what stands for an item under its
|
|
111
|
+
// line: "agent" (a row of its own before it, agent_rows.js, which it keeps
|
|
112
|
+
// outside a line too), "note" (a compact row inside a line only) or "self"
|
|
113
|
+
// (the item is its own row: a steps block's summary line, footprint and
|
|
114
|
+
// all, as today). A "self" item takes part only in a run that holds an
|
|
115
|
+
// "agent" one (a report or a peer answer; owner decision (b)): beside notes
|
|
116
|
+
// only, or alone, a block stays as it is (agentRuns).
|
|
117
|
+
const KINDS = [
|
|
118
|
+
{ kind: "report", selector: REPORTS, takes: agentItem, key: () => "report", row: "agent",
|
|
119
|
+
label: (items) => ({ label: plural(items.length, "report"), warn: 0 }) },
|
|
120
|
+
{ kind: "peer", selector: PEERS, takes: agentItem, key: (el) => `peer:${peerName(el)}`, row: "agent", label: peerSegment },
|
|
121
|
+
{ kind: "calls", selector: STEPS, takes: (el) => callsOf(el) > 0, key: () => "calls", row: "self", label: callsSegment },
|
|
122
|
+
{ kind: "notes", selector: QUIET, takes: () => true, key: () => "notes", row: "note",
|
|
123
|
+
label: (items) => ({ label: plural(items.length, "note"), warn: rowLabel(items).warn }) },
|
|
124
|
+
];
|
|
125
|
+
// Every kind's elements: an item, or what holds one.
|
|
126
|
+
const ITEMS = KINDS.map((k) => k.selector).join(", ");
|
|
127
|
+
// +el+'s kind (by its selector), or undefined.
|
|
128
|
+
const kindOf = (el) => KINDS.find((k) => el.matches(k.selector));
|
|
129
|
+
// +el+'s kind when it takes part in a run now, else undefined.
|
|
130
|
+
const takingKind = (el) => {
|
|
131
|
+
const kind = kindOf(el);
|
|
132
|
+
return kind?.takes(el) ? kind : undefined;
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
// The calls segment: "N calls", the calls of its blocks on view (one a
|
|
136
|
+
// folded Revise version hides doesn't count, unless all are hidden).
|
|
137
|
+
function callsSegment(items) {
|
|
138
|
+
const shown = items.filter((el) => !el.classList.contains(STACKED));
|
|
139
|
+
return { label: plural((shown.length ? shown : items).reduce((n, el) => n + callsOf(el), 0), "call"), warn: 0 };
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// A peer's segment: "<name>: <verdict>" ("<name> answered" without one,
|
|
143
|
+
// "<name> ×N" for several).
|
|
144
|
+
function peerSegment(items) {
|
|
145
|
+
const name = peerName(items[0]);
|
|
146
|
+
const verdict = items.length === 1 ? verdictWord(peerVerdict(items[0])) : "";
|
|
147
|
+
return { label: items.length > 1 ? `${name} ×${items.length}` : verdict ? `${name}: ${verdict}` : `${name} answered`, warn: 0 };
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// The runs over +children+ (#history's, in order): [[item, …]]. +ownRow+
|
|
151
|
+
// tells this module's own rows (passed over). A run without a report or a
|
|
152
|
+
// peer answer is cut at its steps blocks, which leave it (they stay as they
|
|
153
|
+
// are). Pure but for the items' DOM.
|
|
154
|
+
export function agentRuns(children, { isOwn = ownRow } = {}) {
|
|
155
|
+
const runs = [];
|
|
156
|
+
let run = null;
|
|
157
|
+
for (const el of children) {
|
|
158
|
+
if (el.nodeType !== 1 || isOwn(el) || el.matches(PASS_BY)) continue;
|
|
159
|
+
if (!takingKind(el)) {
|
|
160
|
+
run = null;
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
if (!run) runs.push((run = []));
|
|
164
|
+
run.push(el);
|
|
165
|
+
}
|
|
166
|
+
return runs.flatMap((items) => {
|
|
167
|
+
if (items.some((el) => kindOf(el).row === "agent")) return [items];
|
|
168
|
+
const cut = [[]];
|
|
169
|
+
for (const el of items) {
|
|
170
|
+
if (kindOf(el).row === "self") cut.push([]);
|
|
171
|
+
else cut.at(-1).push(el);
|
|
172
|
+
}
|
|
173
|
+
return cut.filter((part) => part.length);
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// An item's segment: "report", "peer:<name>" (one per peer, by the name its
|
|
178
|
+
// segment says), "calls", "notes".
|
|
179
|
+
export function segmentKey(el) {
|
|
180
|
+
return (kindOf(el) || KINDS.at(-1)).key(el);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// A run's segments, in the mock-up's order (KINDS': reports, then each peer
|
|
184
|
+
// as it first comes, then the calls, then the notes): [{key, kind, items,
|
|
185
|
+
// label, warn}]. A report segment "N reports"; a peer's "<name>: <verdict>"
|
|
186
|
+
// ("<name> answered" without one, "<name> ×N" for several); the calls' "N
|
|
187
|
+
// calls"; the notes' "N notes" with its warn count (the `warn` class,
|
|
188
|
+
// rowLabel's).
|
|
189
|
+
export function segments(items) {
|
|
190
|
+
const by = new Map();
|
|
191
|
+
for (const el of items) {
|
|
192
|
+
const kind = kindOf(el) || KINDS.at(-1);
|
|
193
|
+
const key = kind.key(el);
|
|
194
|
+
if (!by.has(key)) by.set(key, { key, kind: kind.kind, items: [], of: kind });
|
|
195
|
+
by.get(key).items.push(el);
|
|
196
|
+
}
|
|
197
|
+
const rank = (seg) => KINDS.indexOf(seg.of);
|
|
198
|
+
return [...by.values()].sort((a, b) => rank(a) - rank(b)).map(({ of, ...seg }) => ({ ...seg, ...of.label(seg.items) }));
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// The line's segments as buttons (lit keeps them, so the focus stays on a
|
|
202
|
+
// redraw). +open+: the open segment's key, or null.
|
|
203
|
+
export function lineTemplate(segs, open) {
|
|
204
|
+
return segs.map((s) => html`<button type="button" class="qn-seg seg-${s.kind}" data-seg=${s.key}
|
|
205
|
+
aria-expanded=${open === s.key ? "true" : "false"}>${s.label}${s.warn > 0 ? html`<span class="qn-warn">⚠ ${s.warn}</span>` : nothing}</button>`);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// Moves a folded or hidden item's arrival onto +row+.
|
|
209
|
+
function arriveOn(row, el) {
|
|
210
|
+
if (!el?.classList.contains(ARRIVE)) return;
|
|
211
|
+
el.classList.remove(ARRIVE);
|
|
212
|
+
row.classList.add(ARRIVE);
|
|
213
|
+
row.addEventListener("animationend", () => row.classList.remove(ARRIVE), { once: true });
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// Each item in a line, its line; each line, how to open the segment of an
|
|
217
|
+
// item of it (and the item, a note). Module-wide, so revealRun needs no installed instance.
|
|
218
|
+
const lineOfItem = new WeakMap();
|
|
219
|
+
const openSegment = new WeakMap();
|
|
220
|
+
|
|
221
|
+
// +el+ (or the item that holds it, or the item an agent row stands for)
|
|
222
|
+
// hidden behind a closed line: that line's segment for it opens. True when
|
|
223
|
+
// it did. A jump goes through history_reveal.js revealInHistory, which calls
|
|
224
|
+
// this before quiet_notes.js revealQuiet (a notes row's).
|
|
225
|
+
export function revealRun(el) {
|
|
226
|
+
if (!el?.closest) return false;
|
|
227
|
+
// The outermost item that a line holds (a notice inside a steps block:
|
|
228
|
+
// the block).
|
|
229
|
+
let item = null;
|
|
230
|
+
for (let at = el.closest(ITEMS); at; at = at.parentElement?.closest(ITEMS)) if (lineOfItem.has(at)) item = at;
|
|
231
|
+
if (!item && el.classList?.contains(AGENT_ROW)) item = el.nextElementSibling;
|
|
232
|
+
const line = item && lineOfItem.get(item);
|
|
233
|
+
if (!line?.isConnected || !item.classList.contains(HIDDEN_CLASS)) return false;
|
|
234
|
+
openSegment.get(line)?.(item);
|
|
235
|
+
return true;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// @param historyEl the history (#history), also its scroller
|
|
239
|
+
// @param sessionId () => the open session's id
|
|
240
|
+
// @param projectRoot () => the open session's project root, or null
|
|
241
|
+
// @param motion (bubble) => whether an open or close animates (canAnimate)
|
|
242
|
+
// @return {settle()} (the observer runs it; specs call it)
|
|
243
|
+
export function installAgentRuns({ historyEl, sessionId = () => null, projectRoot = () => null, motion = canAnimate }) {
|
|
244
|
+
const doc = historyEl.ownerDocument;
|
|
245
|
+
// Each item's row, and each row's item.
|
|
246
|
+
const rowOf = new WeakMap();
|
|
247
|
+
const bubbleOf = new WeakMap();
|
|
248
|
+
// Each run's first item's notes row or line, and each one's first item.
|
|
249
|
+
const headOf = new WeakMap();
|
|
250
|
+
const firstOf = new WeakMap();
|
|
251
|
+
// The words a notes row or a line was last drawn with.
|
|
252
|
+
const drawn = new WeakMap();
|
|
253
|
+
// Inside a line, each note's compact row and each row's note; the notes
|
|
254
|
+
// opened under their rows (never stored).
|
|
255
|
+
const noteRowOf = new WeakMap();
|
|
256
|
+
const noteOf = new WeakMap();
|
|
257
|
+
const openNotes = new WeakSet();
|
|
258
|
+
// How each item row was last drawn ("<folded>|<in line>"), and the items
|
|
259
|
+
// whose text changed since (the observer's): a settle redraws only those.
|
|
260
|
+
const rowDrawn = new WeakMap();
|
|
261
|
+
const dirty = new Set();
|
|
262
|
+
|
|
263
|
+
const setClass = (el, name, on) => {
|
|
264
|
+
if (el.classList.contains(name) !== on) el.classList.toggle(name, on);
|
|
265
|
+
};
|
|
266
|
+
// +el+ shown or hidden by its run; a hidden one loses its arrival.
|
|
267
|
+
function setShown(el, on, shown) {
|
|
268
|
+
shown.add(el);
|
|
269
|
+
setClass(el, HIDDEN_CLASS, !on);
|
|
270
|
+
if (!on && el.classList.contains(ARRIVE)) el.classList.remove(ARRIVE);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
// ---- A report's or a peer answer's row ----
|
|
274
|
+
|
|
275
|
+
function ensureRow(bubble, stored) {
|
|
276
|
+
let row = rowOf.get(bubble);
|
|
277
|
+
if (!row) {
|
|
278
|
+
row = makeAgentRow(doc, bubble);
|
|
279
|
+
row.classList.add(AGENT_ROW);
|
|
280
|
+
rowOf.set(bubble, row);
|
|
281
|
+
bubbleOf.set(row, bubble);
|
|
282
|
+
// The stored choice, once: after that the bubble's class is the state
|
|
283
|
+
// (a jump that opens it, history_reveal.js, isn't saved).
|
|
284
|
+
const folded = !stored().includes(answerKey(bubble));
|
|
285
|
+
if (folded) bubble.classList.add("folded");
|
|
286
|
+
if (isPeer(bubble)) bubble.classList.add(UNDER_ROW);
|
|
287
|
+
if (folded) arriveOn(row, bubble);
|
|
288
|
+
}
|
|
289
|
+
if (bubble.previousElementSibling !== row) historyEl.insertBefore(row, bubble);
|
|
290
|
+
return row;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
// Draws an item's row when its state or its text changed since it was.
|
|
294
|
+
function drawRowOf(bubble, inLine) {
|
|
295
|
+
const row = rowOf.get(bubble);
|
|
296
|
+
const sig = `${bubble.classList.contains("folded")}|${inLine}`;
|
|
297
|
+
if (rowDrawn.get(row) === sig && !dirty.has(bubble)) return;
|
|
298
|
+
drawAgentRow(row, bubble, { inLine });
|
|
299
|
+
rowDrawn.set(row, sig);
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
// A note's compact row inside a line, just before it.
|
|
303
|
+
function ensureNoteRow(el) {
|
|
304
|
+
let row = noteRowOf.get(el);
|
|
305
|
+
if (!row) {
|
|
306
|
+
row = makeNoteRow(doc);
|
|
307
|
+
row.classList.add(AGENT_ROW, IN_LINE);
|
|
308
|
+
noteRowOf.set(el, row);
|
|
309
|
+
noteOf.set(row, el);
|
|
310
|
+
}
|
|
311
|
+
if (el.previousElementSibling !== row) historyEl.insertBefore(row, el);
|
|
312
|
+
const sig = String(openNotes.has(el));
|
|
313
|
+
if (rowDrawn.get(row) !== sig || dirty.has(el)) {
|
|
314
|
+
drawNoteRow(row, el, openNotes.has(el));
|
|
315
|
+
rowDrawn.set(row, sig);
|
|
316
|
+
}
|
|
317
|
+
return row;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
// ---- A run's head: a notes row (button) or a line (div) ----
|
|
321
|
+
|
|
322
|
+
// The head that stands before +first+ (its row when it has one).
|
|
323
|
+
const before = (first) => rowOf.get(first) || noteRowOf.get(first) || first;
|
|
324
|
+
|
|
325
|
+
function makeHead(first, items, line) {
|
|
326
|
+
const head = doc.createElement(line ? "div" : "button");
|
|
327
|
+
if (line) {
|
|
328
|
+
head.className = `${NOTES_CLASS} ${LINE_CLASS}`;
|
|
329
|
+
head.setAttribute("role", "group");
|
|
330
|
+
head.setAttribute("aria-label", "agent run");
|
|
331
|
+
openSegment.set(head, (item) => {
|
|
332
|
+
head.dataset.open = segmentKey(item);
|
|
333
|
+
if (kindOf(item)?.row === "note") openNotes.add(item);
|
|
334
|
+
settle();
|
|
335
|
+
});
|
|
336
|
+
} else {
|
|
337
|
+
head.type = "button";
|
|
338
|
+
head.className = NOTES_CLASS;
|
|
339
|
+
head.setAttribute("aria-expanded", "false");
|
|
340
|
+
}
|
|
341
|
+
// The run's arrival plays on its head (its items are hidden).
|
|
342
|
+
for (const el of items) {
|
|
343
|
+
arriveOn(head, el);
|
|
344
|
+
arriveOn(head, rowOf.get(el));
|
|
345
|
+
}
|
|
346
|
+
return head;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
// A head already in the run's span (between its items, or just before its
|
|
350
|
+
// first) that no other run took: the run's first item changed.
|
|
351
|
+
function headInSpan(items, taken) {
|
|
352
|
+
const last = items[items.length - 1];
|
|
353
|
+
const prev = before(items[0]).previousElementSibling;
|
|
354
|
+
if (prev?.matches(NOTES) && firstOf.has(prev) && !taken.has(prev)) return prev;
|
|
355
|
+
for (let el = before(items[0]); el && el !== last; el = el.nextElementSibling) {
|
|
356
|
+
if (el.matches(NOTES) && firstOf.has(el) && !taken.has(el)) return el;
|
|
357
|
+
}
|
|
358
|
+
return null;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
// The run's head, of the kind it needs now: a notes row that becomes a line
|
|
362
|
+
// (a report joined) gives its open state and its focus to the notes
|
|
363
|
+
// segment, and back.
|
|
364
|
+
function ensureHead(items, line, taken) {
|
|
365
|
+
const first = items[0];
|
|
366
|
+
let head = headOf.get(first);
|
|
367
|
+
if (!head || head.parentElement !== historyEl || taken.has(head)) head = headInSpan(items, taken);
|
|
368
|
+
if (head && head.classList.contains(LINE_CLASS) !== line) {
|
|
369
|
+
const old = head;
|
|
370
|
+
const focused = old.contains(doc.activeElement);
|
|
371
|
+
const notesOpen = line ? old.classList.contains("open") : old.dataset.open === "notes";
|
|
372
|
+
head = makeHead(first, items, line);
|
|
373
|
+
if (notesOpen && line) head.dataset.open = "notes";
|
|
374
|
+
else if (notesOpen) head.classList.add("open");
|
|
375
|
+
old.replaceWith(head);
|
|
376
|
+
if (focused) queueMicrotask(() => (head.querySelector('[data-seg="notes"]') || head).focus?.());
|
|
377
|
+
}
|
|
378
|
+
if (!head) head = makeHead(first, items, line);
|
|
379
|
+
if (firstOf.get(head) !== first) {
|
|
380
|
+
firstOf.set(head, first);
|
|
381
|
+
headOf.set(first, head);
|
|
382
|
+
}
|
|
383
|
+
if (before(first).previousElementSibling !== head) historyEl.insertBefore(head, before(first));
|
|
384
|
+
taken.add(head);
|
|
385
|
+
return head;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
function drawNotesRow(row, items, shown) {
|
|
389
|
+
const label = rowLabel(items);
|
|
390
|
+
const words = JSON.stringify(label);
|
|
391
|
+
if (drawn.get(row) !== words) {
|
|
392
|
+
render(rowTemplate(label), row);
|
|
393
|
+
drawn.set(row, words);
|
|
394
|
+
}
|
|
395
|
+
const open = row.classList.contains("open");
|
|
396
|
+
const expanded = open ? "true" : "false";
|
|
397
|
+
if (row.getAttribute("aria-expanded") !== expanded) row.setAttribute("aria-expanded", expanded);
|
|
398
|
+
setClass(row, "warn", label.warn > 0);
|
|
399
|
+
for (const item of items) setShown(item, open, shown);
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
function drawLine(line, items, shown, live, inLine) {
|
|
403
|
+
const segs = segments(items);
|
|
404
|
+
const open = segs.some((s) => s.key === line.dataset.open) ? line.dataset.open : null;
|
|
405
|
+
if (!open && "open" in line.dataset) delete line.dataset.open;
|
|
406
|
+
const words = JSON.stringify([segs.map((s) => [s.key, s.label, s.warn]), open]);
|
|
407
|
+
if (drawn.get(line) !== words) {
|
|
408
|
+
render(lineTemplate(segs, open), line);
|
|
409
|
+
drawn.set(line, words);
|
|
410
|
+
}
|
|
411
|
+
setClass(line, "warn", segs.some((s) => s.warn > 0));
|
|
412
|
+
for (const seg of segs) {
|
|
413
|
+
const on = seg.key === open;
|
|
414
|
+
for (const el of seg.items) {
|
|
415
|
+
lineOfItem.set(el, line);
|
|
416
|
+
const row = rowOf.get(el);
|
|
417
|
+
if (kindOf(el).row === "self") {
|
|
418
|
+
// Its own row: shown under the line with its segment.
|
|
419
|
+
setClass(el, IN_LINE, true);
|
|
420
|
+
inLine.add(el);
|
|
421
|
+
setShown(el, on, shown);
|
|
422
|
+
} else if (kindOf(el).row === "agent") {
|
|
423
|
+
setClass(row, IN_LINE, true);
|
|
424
|
+
drawRowOf(el, true);
|
|
425
|
+
setShown(row, on, shown);
|
|
426
|
+
setShown(el, on, shown);
|
|
427
|
+
} else {
|
|
428
|
+
// A note: its compact row; the note itself once its row is open.
|
|
429
|
+
const noteRow = ensureNoteRow(el);
|
|
430
|
+
live.add(noteRow);
|
|
431
|
+
setShown(noteRow, on, shown);
|
|
432
|
+
setShown(el, on && openNotes.has(el), shown);
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
// The Revise stub nearest before +el+ (the version it folds), or null.
|
|
439
|
+
function stubBefore(el) {
|
|
440
|
+
for (let at = el.previousElementSibling; at; at = at.previousElementSibling) if (at.matches(".stack-stub")) return at;
|
|
441
|
+
return null;
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
function settle() {
|
|
445
|
+
let opened = null;
|
|
446
|
+
const stored = () => (opened ??= readOpened(sessionId()));
|
|
447
|
+
const live = new Set();
|
|
448
|
+
for (const el of [...historyEl.children]) if (takingKind(el)?.row === "agent") live.add(ensureRow(el, stored));
|
|
449
|
+
const taken = new Set();
|
|
450
|
+
const shown = new Set();
|
|
451
|
+
// The items that are their own row under a line this time (steps blocks).
|
|
452
|
+
const inLine = new Set();
|
|
453
|
+
for (const items of agentRuns(historyEl.children)) {
|
|
454
|
+
const agents = items.filter((el) => rowOf.has(el));
|
|
455
|
+
if (items.length === 1 && agents.length === 1) {
|
|
456
|
+
// One report or one peer answer: its own row, no line.
|
|
457
|
+
const [el] = items;
|
|
458
|
+
lineOfItem.delete(el);
|
|
459
|
+
setClass(rowOf.get(el), IN_LINE, false);
|
|
460
|
+
drawRowOf(el, false);
|
|
461
|
+
setShown(rowOf.get(el), true, shown);
|
|
462
|
+
setShown(el, true, shown);
|
|
463
|
+
continue;
|
|
464
|
+
}
|
|
465
|
+
const line = items.some((el) => kindOf(el).row !== "note");
|
|
466
|
+
const head = ensureHead(items, line, taken);
|
|
467
|
+
if (line) drawLine(head, items, shown, live, inLine);
|
|
468
|
+
else {
|
|
469
|
+
for (const el of items) lineOfItem.delete(el);
|
|
470
|
+
drawNotesRow(head, items, shown);
|
|
471
|
+
}
|
|
472
|
+
// A run inside a superseded Revise version that is folded to its stub
|
|
473
|
+
// (revise_stack.js hides every item): its head goes with them; a
|
|
474
|
+
// segment that had the focus gives it to the stub.
|
|
475
|
+
const stacked = items.every((el) => el.classList.contains(STACKED));
|
|
476
|
+
if (stacked && !head.classList.contains(HIDDEN_CLASS) && head.contains(doc.activeElement)) stubBefore(head)?.focus();
|
|
477
|
+
setShown(head, !stacked, shown);
|
|
478
|
+
}
|
|
479
|
+
for (const head of historyEl.querySelectorAll(`:scope > ${NOTES}`)) if (!taken.has(head)) head.remove();
|
|
480
|
+
// An item that was its own row under a line and is in none now.
|
|
481
|
+
for (const el of historyEl.querySelectorAll(`:scope > .${IN_LINE}:not(.${AGENT_ROW})`)) {
|
|
482
|
+
if (inLine.has(el)) continue;
|
|
483
|
+
el.classList.remove(IN_LINE);
|
|
484
|
+
lineOfItem.delete(el);
|
|
485
|
+
}
|
|
486
|
+
// Rows whose item left (a re-render, a replaced bubble) or left its line
|
|
487
|
+
// (a note's) go; a bubble still here that no longer takes part shows as
|
|
488
|
+
// it is.
|
|
489
|
+
for (const row of historyEl.querySelectorAll(`:scope > .${AGENT_ROW}`)) {
|
|
490
|
+
if (live.has(row)) continue;
|
|
491
|
+
row.remove();
|
|
492
|
+
if (noteOf.has(row)) {
|
|
493
|
+
noteRowOf.delete(noteOf.get(row));
|
|
494
|
+
noteOf.delete(row);
|
|
495
|
+
continue;
|
|
496
|
+
}
|
|
497
|
+
const bubble = bubbleOf.get(row);
|
|
498
|
+
if (bubble?.parentElement !== historyEl) continue;
|
|
499
|
+
bubble.classList.remove("folded", UNDER_ROW);
|
|
500
|
+
rowOf.delete(bubble);
|
|
501
|
+
}
|
|
502
|
+
dirty.clear();
|
|
503
|
+
// An item no run holds any more (it changed kind): shown again.
|
|
504
|
+
for (const el of historyEl.querySelectorAll(`:scope > .${HIDDEN_CLASS}`)) if (!shown.has(el)) el.classList.remove(HIDDEN_CLASS);
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
// Ours: a row or head this module made (its own inserts never settle again).
|
|
508
|
+
const ours = (n) => n.nodeType !== 1 || bubbleOf.has(n) || noteOf.has(n) || firstOf.has(n);
|
|
509
|
+
// A class change on one of #history's children that isn't only ours: it
|
|
510
|
+
// may change what the child is (a note opens a turn now, a report
|
|
511
|
+
// finished streaming).
|
|
512
|
+
function classChanged(record) {
|
|
513
|
+
const now = new Set(record.target.classList);
|
|
514
|
+
const was = new Set(String(record.oldValue ?? "").split(/\s+/).filter(Boolean));
|
|
515
|
+
const diff = [...now].filter((c) => !was.has(c)).concat([...was].filter((c) => !now.has(c)));
|
|
516
|
+
return diff.some((c) => !OWN.has(c) && c !== "folded");
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
new MutationObserver((records) => {
|
|
520
|
+
let again = false;
|
|
521
|
+
const redraw = new Set();
|
|
522
|
+
for (const r of records) {
|
|
523
|
+
if (r.type === "childList") {
|
|
524
|
+
if (r.target === historyEl) {
|
|
525
|
+
if ([...r.addedNodes, ...r.removedNodes].some((n) => !ours(n))) again = true;
|
|
526
|
+
} else {
|
|
527
|
+
// Inside an item (its text drawn later): its row's words follow.
|
|
528
|
+
const item = r.target.closest?.(ITEMS);
|
|
529
|
+
if (item && (rowOf.has(item) || noteRowOf.has(item))) redraw.add(item);
|
|
530
|
+
}
|
|
531
|
+
continue;
|
|
532
|
+
}
|
|
533
|
+
const el = r.target;
|
|
534
|
+
if (el.parentElement !== historyEl || ours(el)) continue;
|
|
535
|
+
if (r.attributeName !== "class") again = true;
|
|
536
|
+
else if (classChanged(r)) again = true;
|
|
537
|
+
else if (rowOf.has(el)) redraw.add(el);
|
|
538
|
+
}
|
|
539
|
+
for (const el of redraw) dirty.add(el);
|
|
540
|
+
if (again || redraw.size) settle();
|
|
541
|
+
}).observe(historyEl, {
|
|
542
|
+
childList: true, subtree: true, attributes: true, attributeOldValue: true, attributeFilter: ["class", "data-turn-id", "data-line-key", "data-opened-turn"],
|
|
543
|
+
});
|
|
544
|
+
settle();
|
|
545
|
+
|
|
546
|
+
historyEl.addEventListener("click", (e) => {
|
|
547
|
+
const seg = e.target.closest?.("[data-seg]");
|
|
548
|
+
const line = seg?.parentElement;
|
|
549
|
+
if (seg && line?.parentElement === historyEl && firstOf.has(line)) {
|
|
550
|
+
e.preventDefault();
|
|
551
|
+
if (line.dataset.open === seg.dataset.seg) delete line.dataset.open;
|
|
552
|
+
else line.dataset.open = seg.dataset.seg;
|
|
553
|
+
settle();
|
|
554
|
+
// A row that held the focus went hidden with its segment: the focus
|
|
555
|
+
// goes to the segment tapped.
|
|
556
|
+
if (doc.activeElement?.closest?.(`.${HIDDEN_CLASS}`)) seg.focus();
|
|
557
|
+
return;
|
|
558
|
+
}
|
|
559
|
+
const noteRow = e.target.closest?.(".note-row");
|
|
560
|
+
if (noteRow && noteOf.has(noteRow)) {
|
|
561
|
+
e.preventDefault();
|
|
562
|
+
const note = noteOf.get(noteRow);
|
|
563
|
+
if (openNotes.has(note)) openNotes.delete(note);
|
|
564
|
+
else openNotes.add(note);
|
|
565
|
+
settle();
|
|
566
|
+
return;
|
|
567
|
+
}
|
|
568
|
+
const notes = e.target.closest?.(NOTES);
|
|
569
|
+
if (notes && notes.parentElement === historyEl && firstOf.has(notes)) {
|
|
570
|
+
if (notes.classList.contains(LINE_CLASS)) return;
|
|
571
|
+
e.preventDefault();
|
|
572
|
+
notes.classList.toggle("open", !notes.classList.contains("open"));
|
|
573
|
+
settle();
|
|
574
|
+
return;
|
|
575
|
+
}
|
|
576
|
+
const row = e.target.closest?.(`.${AGENT_ROW}`);
|
|
577
|
+
const bubble = row && bubbleOf.get(row);
|
|
578
|
+
if (!bubble) return;
|
|
579
|
+
e.preventDefault();
|
|
580
|
+
const folded = !bubble.classList.contains("folded");
|
|
581
|
+
const flip = () => keepRowInView(historyEl, row, () => {
|
|
582
|
+
bubble.classList.toggle("folded", folded);
|
|
583
|
+
drawRowOf(bubble, row.classList.contains(IN_LINE));
|
|
584
|
+
});
|
|
585
|
+
if (!motion(bubble)) flip();
|
|
586
|
+
else if (folded) animateHeight(bubble, bubble.offsetHeight, 0, { fade: true }).then(flip);
|
|
587
|
+
else {
|
|
588
|
+
flip();
|
|
589
|
+
animateHeight(bubble, 0, bubble.offsetHeight, { fade: true });
|
|
590
|
+
}
|
|
591
|
+
writeOpened(sessionId(), answerKey(bubble), !folded, { root: projectRoot() });
|
|
592
|
+
});
|
|
593
|
+
|
|
594
|
+
return { settle };
|
|
595
|
+
}
|