samagotchi 0.167.0 → 0.169.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.
Files changed (54) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +64 -1
  3. data/docs/architecture.md +1 -1
  4. data/docs/cli.md +14 -10
  5. data/docs/configuration.md +1 -1
  6. data/docs/guardrails.md +2 -3
  7. data/docs/hooks.md +2 -2
  8. data/docs/internals/block-renderers.md +1 -1
  9. data/docs/internals/bridge.md +16 -4
  10. data/docs/internals/inline-widgets.md +1 -1
  11. data/docs/internals/objectives.md +2 -1
  12. data/docs/internals/remote-sessions.md +1 -1
  13. data/docs/internals/web-frontend.md +99 -19
  14. data/docs/plugins.md +1 -1
  15. data/docs/releasing.md +10 -6
  16. data/docs/testing.md +14 -3
  17. data/lib/samagotchi/bridge.rb +12 -2
  18. data/lib/samagotchi/bundle_command.rb +19 -4
  19. data/lib/samagotchi/bundles/system/manifest.yml +1 -1
  20. data/lib/samagotchi/client_id.rb +3 -0
  21. data/lib/samagotchi/command_intake.rb +7 -7
  22. data/lib/samagotchi/compat.rb +12 -7
  23. data/lib/samagotchi/engine/objectives.rb +10 -1
  24. data/lib/samagotchi/guardrails/approvals.rb +10 -29
  25. data/lib/samagotchi/session_continue.rb +8 -11
  26. data/lib/samagotchi/session_manager/removal.rb +3 -3
  27. data/lib/samagotchi/session_stores.rb +79 -0
  28. data/lib/samagotchi/turn_stages/objectives.rb +6 -8
  29. data/lib/samagotchi/version.rb +1 -1
  30. data/lib/samagotchi/web/public/app.js +3 -1
  31. data/lib/samagotchi/web/public/block_watch.js +1 -1
  32. data/lib/samagotchi/web/public/cards_place.js +5 -1
  33. data/lib/samagotchi/web/public/dock_reserve.js +12 -2
  34. data/lib/samagotchi/web/public/history_end.js +16 -0
  35. data/lib/samagotchi/web/public/index.html +89 -44
  36. data/lib/samagotchi/web/public/markdown_place.js +1 -1
  37. data/lib/samagotchi/web/public/question_cards.js +21 -7
  38. data/lib/samagotchi/web/public/refs.js +4 -1
  39. data/lib/samagotchi/web/public/reload_check.js +5 -4
  40. data/lib/samagotchi/web/public/stage_asking.js +283 -0
  41. data/lib/samagotchi/web/public/stage_cards.js +20 -0
  42. data/lib/samagotchi/web/public/stage_frame.js +17 -23
  43. data/lib/samagotchi/web/public/stage_model.js +48 -15
  44. data/lib/samagotchi/web/public/stage_opened.js +239 -0
  45. data/lib/samagotchi/web/public/stage_slots.js +13 -90
  46. data/lib/samagotchi/web/public/stage_view.js +339 -225
  47. data/lib/samagotchi/web/public/stage_waiting.js +180 -0
  48. data/lib/samagotchi/web/public/stage_watch.js +8 -4
  49. data/lib/samagotchi/web/public/stream_handlers.js +1 -1
  50. data/lib/samagotchi/web/public/turn_block.js +14 -2
  51. data/lib/samagotchi/web/public/turn_model.js +1 -1
  52. data/lib/samagotchi/web/session_show.rb +14 -4
  53. data/lib/samagotchi/worker.rb +3 -4
  54. metadata +7 -1
@@ -0,0 +1,239 @@
1
+ // A running turn opened in the history (stage_view.js). When a card waits
2
+ // for the user (stage_asking.js docks), the turn's prompt, its steps block
3
+ // and the rows that arrive are the history's from then to the turn's end;
4
+ // only what waits for the user (the card), the live slots and the answer
5
+ // stay in the stage. chi's own question or approval after a reply gets that
6
+ // reply (the reason, its lead-in) as a conversation bubble at the history's
7
+ // end. This module draws the reason
8
+ // bubble and moves things into the history (history_end.js), following a
9
+ // reader who was at the history's end (a reader scrolled up is never
10
+ // moved). At the hand-off the reasons go (end): each answered card's own
11
+ // two-line clamp says it then, as a finished turn and its reload have it.
12
+ //
13
+ // The reason bubble (.bubble.said, live only): a label line ("chi · step
14
+ // N", "asking you" while its card waits: .pending), the text raw at once,
15
+ // then its markdown when the page's render answers, refs linked on both
16
+ // draws. It carries data-question-id and data-said-turn (never
17
+ // data-turn-id: that marks a turn's opening, turn_range.js).
18
+ import { moveToHistoryEnd } from "./history_end.js";
19
+ import { revealInHistory } from "./history_reveal.js";
20
+ import { easeScrollTo } from "./scroll.js";
21
+
22
+ // What the history scrolls to sits this far under its top edge.
23
+ const READ_GAP = 8;
24
+
25
+ // +historyEl+: the history; +atHistoryEnd()+: the reader is at its end;
26
+ // +renderMarkdown(text)+: a promise of the text's HTML (null: keep it raw);
27
+ // +linkRefs(el, refs)+: the page's ref pass; +reduce()+: reduced motion;
28
+ // +afterSettle(fn)+: +fn+ once the stage has settled for a change made now
29
+ // (stage_frame.js afterGrowth: the asking form's shrink moves the history's
30
+ // bottom reserve, whose own follow of the end must not win over the
31
+ // reason's first line).
32
+ export function createOpenedTurn({
33
+ historyEl, atHistoryEnd, renderMarkdown = () => null, linkRefs = () => {}, reduce = () => false,
34
+ afterSettle = (fn) => requestAnimationFrame(() => requestAnimationFrame(fn)),
35
+ }) {
36
+ // card key (stage_waiting.js waitingKey) → its reason bubble, for the turn
37
+ // in the stage.
38
+ const reasons = new Map();
39
+ // The asking steps whose text a reason shows (data-said: their text is
40
+ // hidden in the block meanwhile, index.html), to clear at the end.
41
+ const saidSteps = new Set();
42
+
43
+ // The asking step: the last step of the history's last steps block (in
44
+ // order from the opening on, turn_block.js inOrder).
45
+ function askingStep() {
46
+ const block = [...historyEl.querySelectorAll(":scope > .turn-work")].at(-1);
47
+ return block?.querySelector(":scope > details.gen:last-of-type") || null;
48
+ }
49
+
50
+ // The reasons go: their steps show their own text again.
51
+ function clearSaid() {
52
+ for (const step of saidSteps) delete step.dataset.said;
53
+ saidSteps.clear();
54
+ }
55
+
56
+ // +el+'s top in the history's scroll.
57
+ function topIn(el) {
58
+ return historyEl.scrollTop + el.getBoundingClientRect().top - historyEl.getBoundingClientRect().top;
59
+ }
60
+
61
+ // The history to +el+'s top (none: its end). An +el+ gone meanwhile (a
62
+ // session switch drew another history) moves nothing: the scroll stays.
63
+ function scrollTo(el = null) {
64
+ return easeScrollTo(historyEl, () => {
65
+ if (el && !el.isConnected) return historyEl.scrollTop;
66
+ const end = historyEl.scrollHeight - historyEl.clientHeight;
67
+ return el ? Math.max(0, Math.min(end, topIn(el) - READ_GAP)) : end;
68
+ }, { reduce: reduce() });
69
+ }
70
+
71
+ // +pieces+ to the history's end in order; a reader at the end follows
72
+ // (+follow+: false when a reason's read follows instead).
73
+ function add(pieces, { follow = atHistoryEnd() } = {}) {
74
+ moveToHistoryEnd(historyEl, pieces);
75
+ if (follow) scrollTo();
76
+ }
77
+
78
+ function reasonBubble({ text, refs, step, turnId, questionId }) {
79
+ const bubble = document.createElement("div");
80
+ bubble.className = "bubble said pending";
81
+ if (questionId != null) bubble.dataset.questionId = String(questionId);
82
+ if (turnId) bubble.dataset.saidTurn = turnId;
83
+ const label = document.createElement("div");
84
+ label.className = "said-label";
85
+ const who = document.createElement("span");
86
+ who.textContent = step ? `chi · step ${step}` : "chi";
87
+ const asking = document.createElement("span");
88
+ asking.className = "said-asking";
89
+ asking.textContent = "asking you";
90
+ label.append(who, asking);
91
+ const body = document.createElement("div");
92
+ body.className = "said-text";
93
+ body.textContent = text;
94
+ bubble.append(label, body);
95
+ linkRefs(body, refs);
96
+ // Its markdown, under the reader's first lines (the bubble's one
97
+ // height change).
98
+ Promise.resolve()
99
+ .then(() => renderMarkdown(text))
100
+ .catch(() => null)
101
+ .then((html) => {
102
+ if (!html || !bubble.isConnected) return;
103
+ // Sanitised on the server (POST /api/render).
104
+ body.innerHTML = html;
105
+ body.classList.add("markdown", "md-compact");
106
+ linkRefs(body, refs);
107
+ });
108
+ return bubble;
109
+ }
110
+
111
+ // The card's way back to its reason, at its title line's right.
112
+ function reasonLink(card, bubble) {
113
+ const summary = card.querySelector(":scope > summary");
114
+ if (!summary) return;
115
+ const link = document.createElement("button");
116
+ link.type = "button";
117
+ link.className = "question-reason-link";
118
+ link.textContent = "↑ its reason";
119
+ link.title = "Show the reply that asked this";
120
+ link.addEventListener("click", (e) => {
121
+ // Inside the summary: no toggle.
122
+ e.preventDefault();
123
+ e.stopPropagation();
124
+ if (bubble.isConnected) scrollTo(bubble);
125
+ });
126
+ summary.appendChild(link);
127
+ }
128
+
129
+ // The first of the history's rows after one of +going+ (they move when
130
+ // it goes) whose bottom is below the view's top; null: none (what shows
131
+ // is above them all, and stays).
132
+ function firstInView(going) {
133
+ const top = historyEl.getBoundingClientRect().top;
134
+ let after = false;
135
+ for (const el of historyEl.children) {
136
+ if (going.includes(el)) {
137
+ after = true;
138
+ continue;
139
+ }
140
+ if (after && el.getBoundingClientRect().bottom > top) return el;
141
+ }
142
+ return null;
143
+ }
144
+
145
+ return {
146
+ // The turn's pieces so far (handOffOrder's order) into the history; a
147
+ // reason's read (ask) does the follow, else +follow+ (a reader who was at
148
+ // the end before the move).
149
+ open: (pieces, { follow = false } = {}) => add(pieces, { follow }),
150
+ // A row of the opened turn: to the history's end.
151
+ add: (el) => add([el]),
152
+ // +card+ blocks the turn: its reason's bubble at the history's end, in
153
+ // the card's task; a reader at the end reads it from its first line.
154
+ // +follow+: the reader was at the end before the turn opened.
155
+ // +key+: the card's key, the bubble's for has and settle.
156
+ ask({ card, key, text, refs = null, step = 0, turnId = null, questionId = null, follow = atHistoryEnd() }) {
157
+ const bubble = reasonBubble({ text, refs, step, turnId, questionId });
158
+ moveToHistoryEnd(historyEl, [bubble]);
159
+ reasons.set(key, bubble);
160
+ // The step's own text is the reason: not twice (and raw while the
161
+ // step runs). data-said, not a class: lit owns the step's class.
162
+ const stepEl = askingStep();
163
+ if (stepEl) {
164
+ stepEl.dataset.said = "1";
165
+ saidSteps.add(stepEl);
166
+ }
167
+ reasonLink(card, bubble);
168
+ // The reader at the end reads it from its first line: meanwhile the
169
+ // history is marked data-reading, so the bottom reserve's follow
170
+ // (dock_reserve.js) doesn't take the end back.
171
+ if (follow) {
172
+ historyEl.dataset.reading = "";
173
+ afterSettle(() => {
174
+ const done = () => { delete historyEl.dataset.reading; };
175
+ if (bubble.isConnected) scrollTo(bubble).then(done, done);
176
+ else done();
177
+ });
178
+ }
179
+ return bubble;
180
+ },
181
+ has: (key) => key != null && reasons.has(key),
182
+ // +card+ drawn again with +key+ (the same question, a new element): its
183
+ // way back to the reason it already has.
184
+ relink(key, card) {
185
+ const bubble = reasons.get(key);
186
+ if (bubble && !card.querySelector(":scope > summary > .question-reason-link")) reasonLink(card, bubble);
187
+ },
188
+ // +card+ (its +key+) no longer waits (answered, cancelled): it goes right
189
+ // under its reason, which stops saying "asking you"; another card of the
190
+ // turn to the history's end. A reader at the end follows.
191
+ settle(key, card) {
192
+ const bubble = reasons.get(key);
193
+ if (!bubble?.isConnected) return add([card]);
194
+ const follow = atHistoryEnd();
195
+ bubble.classList.remove("pending");
196
+ bubble.after(card);
197
+ if (follow) scrollTo();
198
+ },
199
+ // The turn's steps block (or one of its steps, +el+) in the history:
200
+ // unfolded and brought into view.
201
+ reveal(el) {
202
+ if (!el) return;
203
+ revealInHistory(el);
204
+ if (el.matches?.("details") && !el.open) el.open = true;
205
+ scrollTo(el);
206
+ },
207
+ // The reasons drawn (the hand-off waits while one is in use).
208
+ watched: () => [...reasons.values()].filter((el) => el.isConnected),
209
+ // The hand-off: the reasons go; each card's own clamp says it now. The
210
+ // reader keeps what they see: the first row in view that stays (the
211
+ // card under a reason they read) stays where it is (the history's own
212
+ // scroll anchoring has none to keep when the row it anchored on goes);
213
+ // a reader at the end then follows the turn's arrival (stage_view.js
214
+ // finishFold).
215
+ end() {
216
+ const gone = [...reasons.values()].filter((el) => el.isConnected);
217
+ reasons.clear();
218
+ clearSaid();
219
+ if (!gone.length) return;
220
+ const anchor = firstInView(gone);
221
+ const before = anchor?.getBoundingClientRect().top;
222
+ for (const bubble of gone) bubble.remove();
223
+ if (anchor) historyEl.scrollTop += anchor.getBoundingClientRect().top - before;
224
+ },
225
+ // The turn left the stage without a hand-off (a reset): forgotten, the
226
+ // history is drawn anew.
227
+ reset() {
228
+ reasons.clear();
229
+ saidSteps.clear();
230
+ },
231
+ // The turn was released without its end (a lost turn_completed, the
232
+ // next turn started): its reasons go, nothing else moves.
233
+ drop() {
234
+ for (const bubble of reasons.values()) bubble.remove();
235
+ reasons.clear();
236
+ clearSaid();
237
+ },
238
+ };
239
+ }
@@ -1,5 +1,5 @@
1
- // The stage view's live slots (stage_view.js): the headline, the asking
2
- // text, the "now" row, the trail and the chip's ticks, each drawn from
1
+ // The stage view's live slots (stage_view.js): the headline, the "now"
2
+ // row, the trail and the chip's ticks, each drawn from
3
3
  // liveSlots (stage_model.js) and patched only when what it shows changes
4
4
  // (+shown+), so an unchanged slot keeps its nodes: a selection in it
5
5
  // survives, and its animations never restart (the ticks' blink; the
@@ -15,29 +15,13 @@ export function node(tag, cls, parent = null) {
15
15
  return el;
16
16
  }
17
17
 
18
- // +headline+, +asking+, +now+, +trail+, +ticks+: the slots' elements.
19
- // +taskStop+: the page's stopper (task_stop.js), for the stop-task button
20
- // on a running task_wait; +gens()+ the running turn's gens (the button's
21
- // command, read at its click); +onChange+ redraws (the stage's
22
- // scheduleRefresh). +renderMarkdown(text)+: the asking text's HTML
23
- // (Promise<string|null>; null keeps the raw text: markdown off, a failed
24
- // read); +linkRefs(el, refs)+: the page's ref pass on the slot (a
25
- // plugin's +refs+ paired first, then the path:line ones).
26
- export function createStageSlots({
27
- headline, asking, now, trail, ticks, taskStop = null, gens, onChange,
28
- renderMarkdown = () => Promise.resolve(null), linkRefs = () => {},
29
- }) {
18
+ // +headline+, +now+, +trail+, +ticks+: the slots' elements. +taskStop+:
19
+ // the page's stopper (task_stop.js), for the stop-task button on a running
20
+ // task_wait; +gens()+ the running turn's gens (the button's command, read
21
+ // at its click); +onChange+ redraws (the stage's scheduleRefresh).
22
+ export function createStageSlots({ headline, now, trail, ticks, taskStop = null, gens, onChange }) {
30
23
  // What each slot shows, so an unchanged slot is left alone.
31
- const shown = { headline: null, now: null, trail: null, asking: null, askingRefs: "" };
32
- // The refs the asking text is paired with (its pending question's).
33
- let askingRefs = null;
34
- // The last asking text sent to render and its answer (in flight or
35
- // done, HTML only): the same question drawn again (after a reset, a
36
- // blank between) asks no second time.
37
- let rendering = { text: null, html: null };
38
- // The asking text shows in its final form (its markdown swapped in, or
39
- // a null render left the raw text): askingSettled.
40
- let askingFinal = false;
24
+ const shown = { headline: null, now: null, trail: null };
41
25
 
42
26
  function drawHeadline({ text, thinking }) {
43
27
  const key = `${thinking ? "t" : "n"}|${text}`;
@@ -55,62 +39,6 @@ export function createStageSlots({
55
39
  headline.classList.add("fresh");
56
40
  }
57
41
 
58
- // Patched only when the text changes (a late chunk to the closed gen
59
- // rewrites the same text): a selection in it survives. A new text reads
60
- // from its top: drawn raw at once, then swapped for its markdown when
61
- // that comes back while the slot still shows the same text (the text is
62
- // final: it shows only once the call that asks runs). +refs+: a plugin's
63
- // refs on it (the pending question's), paired on both draws; new refs on
64
- // the same text (a second question in the step) are paired in place.
65
- function drawAsking(text, refs = null) {
66
- asking.hidden = !text;
67
- const refsKey = text && Array.isArray(refs) && refs.length ? JSON.stringify(refs) : "";
68
- if (shown.asking === text) {
69
- if (shown.askingRefs === refsKey) return;
70
- shown.askingRefs = refsKey;
71
- askingRefs = refsKey ? refs : null;
72
- linkRefs(asking, askingRefs);
73
- return;
74
- }
75
- shown.asking = text;
76
- shown.askingRefs = refsKey;
77
- askingRefs = refsKey ? refs : null;
78
- asking.classList.remove("markdown", "md-compact");
79
- asking.textContent = text;
80
- asking.scrollTop = 0;
81
- askingFinal = false;
82
- if (!text) return;
83
- linkRefs(asking, askingRefs);
84
- if (rendering.text !== text) {
85
- let html;
86
- try { html = Promise.resolve(renderMarkdown(text)).catch(() => null); } catch (_) { html = Promise.resolve(null); }
87
- rendering = { text, html };
88
- }
89
- const pending = rendering;
90
- pending.html.then((html) => {
91
- // A null isn't kept: the next draw of this text asks again.
92
- if (!html && rendering === pending) rendering = { text: null, html: null };
93
- if (shown.asking !== text || askingFinal) return;
94
- // The raw text is final (markdown off, a failed read): the stage
95
- // measures it now.
96
- if (!html) {
97
- askingFinal = true;
98
- onChange?.();
99
- return;
100
- }
101
- // Swapped once: a blank-then-same draw while in flight waits twice.
102
- if (asking.classList.contains("markdown")) return;
103
- // Sanitised on the server (POST /api/render), as an answer's HTML is.
104
- asking.innerHTML = html;
105
- asking.classList.add("markdown", "md-compact");
106
- asking.scrollTop = 0;
107
- linkRefs(asking, askingRefs);
108
- askingFinal = true;
109
- // Its height changed: the stage measures it now (the question room).
110
- onChange?.();
111
- });
112
- }
113
-
114
42
  function drawNow(tool, quiet) {
115
43
  // A task_wait's stop button, as the row's (task_stop.js): in the key
116
44
  // with its in-flight state, so a redraw keeps a clicked one disabled.
@@ -178,17 +106,12 @@ export function createStageSlots({
178
106
  while (ticks.children.length > list.length) ticks.lastChild.remove();
179
107
  }
180
108
 
181
- // A new turn (or none): the slots empty, the asking text hidden, the next
182
- // draw of each slot a fresh one.
109
+ // A new turn (or none): the slots empty, the next draw of each slot a
110
+ // fresh one.
183
111
  function reset() {
184
- for (const slot of [headline, asking, now, trail, ticks]) slot.replaceChildren();
185
- asking.hidden = true;
186
- asking.classList.remove("markdown", "md-compact");
187
- shown.headline = shown.now = shown.trail = shown.asking = null;
188
- shown.askingRefs = "";
189
- askingRefs = null;
190
- askingFinal = false;
112
+ for (const slot of [headline, now, trail, ticks]) slot.replaceChildren();
113
+ shown.headline = shown.now = shown.trail = null;
191
114
  }
192
115
 
193
- return { drawHeadline, drawAsking, drawNow, drawTrail, drawTicks, reset, askingSettled: () => askingFinal };
116
+ return { drawHeadline, drawNow, drawTrail, drawTicks, reset };
194
117
  }