samagotchi 0.167.0 → 0.168.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.
@@ -1,10 +1,11 @@
1
1
  // The stage view's cap and frame (stage_view.js): the one place that
2
2
  // writes the stage's height, and the stage's own scrolls. The cap: at most
3
- // ~60% of the page (~80% with the cloud open, 85% from a card that needs the
4
- // room until the next step: a long reply's question, a tall approval), and
5
- // never above the session panel. The frame: held and grow-only while the
6
- // stage holds a turn (stage_model.js frameTarget), its growth eased; the next
7
- // step after an answered card lets it go once (forgetContent). The stage's state comes
3
+ // ~60% of the page (~80% with the cloud open, 85% from a tall approval
4
+ // until the next step), and never above the session panel. The frame: held
5
+ // and grow-only while the stage holds a turn (stage_model.js frameTarget),
6
+ // its growth eased; it lets go in two places only (forgetContent): the
7
+ // asking form (a turn opened in the history: the card alone, stage_view.js
8
+ // place) and the next step after an answered card (releaseHeldWait). The stage's state comes
8
9
  // in as getters (+isHolding+, +isCollapsed+, +isCloudOpen+, +isRoomCap+);
9
10
  // what the stage changes of the frame's goes through resetHeld and
10
11
  // forgetContent. +waitingCard+: the card blocking the turn, or null.
@@ -18,7 +19,7 @@ const GROW_EASE = "cubic-bezier(.2,.7,.3,1)";
18
19
  // The room between the stage and the composer card (index.html's margin-bottom).
19
20
  export const STAGE_GAP = 10;
20
21
  // The cap's share of the page: the stage's own, with the cloud open, and
21
- // with the question room (a long reply that asks, stage_view.js).
22
+ // with a raised cap (a tall approval, stage_view.js raiseForApproval).
22
23
  const SHARE = 0.6;
23
24
  const CLOUD_SHARE = 0.8;
24
25
  const ROOM_SHARE = 0.85;
@@ -59,23 +60,21 @@ export function createStageFrame({ stage, scroller, status, cloud, queuedSlot, h
59
60
  }
60
61
 
61
62
  // ── the cap: at most ~60% of the page (~80% with the cloud open, 85%
62
- // while a card has the room), and never above the session
63
+ // with a raised cap), and never above the session
63
64
  // panel (the dock is absolute: a tall composer or a phone's keyboard
64
65
  // would push the stage's top out) ──────────────────────────────────────
65
66
 
66
67
  // Where the stage's scroll goes so +slot+ (the scroller's child) shows:
67
68
  // its end in sight, and the asking text's top too when it and the slot
68
69
  // (a waiting card) fit together (the reply reads from its start, the card
69
- // still in sight). +fromStart+ (the question room's arrival): the asking
70
- // text's top even when they don't (the card is reached by the stage's
71
- // jump pill, stage_view.js). A waiting card in +slot+ taller than the
70
+ // still in sight). A waiting card in +slot+ taller than the
72
71
  // view reads from its top, never from its options: the question and what
73
72
  // it approves are what the answer is about; the options are a scroll away.
74
73
  // null: nothing to show.
75
- function revealTop(slot, { fromStart = false } = {}) {
74
+ function revealTop(slot) {
76
75
  if (stage.hidden || slot.hidden) return null;
77
76
  let top = Math.max(scroller.scrollTop, slot.offsetTop + slot.offsetHeight - scroller.clientHeight);
78
- if (!asking.hidden && asking.offsetTop < top && (fromStart || slot.offsetTop + slot.offsetHeight - asking.offsetTop <= scroller.clientHeight)) top = asking.offsetTop;
77
+ if (!asking.hidden && asking.offsetTop < top && slot.offsetTop + slot.offsetHeight - asking.offsetTop <= scroller.clientHeight) top = asking.offsetTop;
79
78
  const card = waitingCard();
80
79
  if (card && slot.contains(card)) top = Math.min(top, Math.max(0, card.offsetTop - CARD_TOP_GAP));
81
80
  return top;
@@ -83,10 +82,9 @@ export function createStageFrame({ stage, scroller, status, cloud, queuedSlot, h
83
82
 
84
83
  // Scrolls the stage to show +slot+: eased, or at once (+now+: the cap
85
84
  // dropped under it; a running ease stops, or its next frame would take
86
- // the scroll back toward its target for the taller stage). +fromStart+:
87
- // revealTop's.
88
- function reveal(slot, { now = false, fromStart = false } = {}) {
89
- const top = revealTop(slot, { fromStart });
85
+ // the scroll back toward its target for the taller stage).
86
+ function reveal(slot, { now = false } = {}) {
87
+ const top = revealTop(slot);
90
88
  if (top === null) return;
91
89
  if (now) easeScrollTo(scroller, top, { reduce: true });
92
90
  else easeStage(top);
@@ -9,6 +9,7 @@ import { hookNoticeLabel } from "./turn_events.js";
9
9
  import { askingRefs, askingText } from "./turn_model.js";
10
10
  import { plainLine } from "./plain_line.js";
11
11
  import { turnPieces } from "./turn_pieces.js";
12
+ import { waitsInStage } from "./stage_cards.js";
12
13
 
13
14
  const READ_LIKE = new Set(["read", "memory_read", "web_fetch", "task_get", "task_list", "list_sessions"]);
14
15
  const EDITS = new Set(["edit", "write", "memory_write"]);
@@ -244,10 +245,12 @@ export const QUEUED_ROWS = '.bubble.user.queued[data-delivery="queue"], .bubble.
244
245
  // QUEUED_ROWS among +parent+'s own children (querySelectorAll on it).
245
246
  export const QUEUED_CHILDREN = QUEUED_ROWS.split(", ").map((sel) => `:scope > ${sel}`).join(", ");
246
247
 
247
- // The kind placeFor reads off an element app.js places.
248
+ // The kind placeFor reads off an element app.js places: "waits" for a card
249
+ // that waits in the stage (stage_cards.js waitsInStage).
248
250
  export function placeKind(el) {
249
251
  if (el.matches?.(".bubble.recap")) return "recap";
250
252
  if (el.matches?.(QUEUED_ROWS)) return "queued";
253
+ if (waitsInStage(el)) return "waits";
251
254
  return "row";
252
255
  }
253
256
 
@@ -284,9 +287,13 @@ export function flashOf(what, data = {}) {
284
287
 
285
288
  // A queued row ("queued", placeKind) goes after the running turn, not
286
289
  // into it: the stage's queued slot below the turn's card while one is
287
- // live, else the history's end (after the turn there too).
288
- export function placeFor({ live, handingOff, kind }) {
290
+ // live, else the history's end (after the turn there too). +opened+: the
291
+ // live turn is opened in the history (stage_view.js): a row ("row") goes
292
+ // to the history's end as the turn's ("opened": the stage moves it there
293
+ // and follows); a card that waits ("waits") stays in the stage.
294
+ export function placeFor({ live, handingOff, kind, opened = false }) {
289
295
  if (kind === "recap") return "history";
290
296
  if (!live || handingOff) return "history";
291
- return kind === "queued" ? "queued" : "stage";
297
+ if (kind === "queued") return "queued";
298
+ return opened && kind === "row" ? "opened" : "stage";
292
299
  }
@@ -0,0 +1,229 @@
1
+ // A running turn opened in the history (stage_view.js). When the model's
2
+ // own question blocks the turn after a reply, the reply that asked it (the
3
+ // reason) becomes a conversation bubble at the history's end, and from then
4
+ // to the turn's end the turn's prompt, its steps block and the rows that
5
+ // arrive are the history's; only what waits for the user (the card), the
6
+ // live slots and the answer stay in the stage. This module draws the reason
7
+ // bubble and moves things into the history (history_end.js), following a
8
+ // reader who was at the history's end (a reader scrolled up is never
9
+ // moved). At the hand-off the reasons go (end): each answered card's own
10
+ // two-line clamp says it then, as a finished turn and its reload have it.
11
+ //
12
+ // The reason bubble (.bubble.said, live only): a label line ("chi · step
13
+ // N", "asking you" while its card waits: .pending), the text raw at once,
14
+ // then its markdown when the page's render answers, refs linked on both
15
+ // draws. It carries data-question-id and data-said-turn (never
16
+ // data-turn-id: that marks a turn's opening, turn_range.js).
17
+ import { moveToHistoryEnd } from "./history_end.js";
18
+ import { revealInHistory } from "./history_reveal.js";
19
+ import { easeScrollTo } from "./scroll.js";
20
+
21
+ // What the history scrolls to sits this far under its top edge.
22
+ const READ_GAP = 8;
23
+
24
+ // +historyEl+: the history; +atHistoryEnd()+: the reader is at its end;
25
+ // +renderMarkdown(text)+: a promise of the text's HTML (null: keep it raw);
26
+ // +linkRefs(el, refs)+: the page's ref pass; +reduce()+: reduced motion;
27
+ // +afterSettle(fn)+: +fn+ once the stage has settled for a change made now
28
+ // (stage_frame.js afterGrowth: the asking form's shrink moves the history's
29
+ // bottom reserve, whose own follow of the end must not win over the
30
+ // reason's first line).
31
+ export function createOpenedTurn({
32
+ historyEl, atHistoryEnd, renderMarkdown = () => null, linkRefs = () => {}, reduce = () => false,
33
+ afterSettle = (fn) => requestAnimationFrame(() => requestAnimationFrame(fn)),
34
+ }) {
35
+ // card → its reason bubble, for the turn in the stage.
36
+ const reasons = new Map();
37
+ // The asking steps whose text a reason shows (data-said: their text is
38
+ // hidden in the block meanwhile, index.html), to clear at the end.
39
+ const saidSteps = new Set();
40
+
41
+ // The asking step: the last step of the history's last steps block (in
42
+ // order from the opening on, turn_block.js inOrder).
43
+ function askingStep() {
44
+ const block = [...historyEl.querySelectorAll(":scope > .turn-work")].at(-1);
45
+ return block?.querySelector(":scope > details.gen:last-of-type") || null;
46
+ }
47
+
48
+ // The reasons go: their steps show their own text again.
49
+ function clearSaid() {
50
+ for (const step of saidSteps) delete step.dataset.said;
51
+ saidSteps.clear();
52
+ }
53
+
54
+ // +el+'s top in the history's scroll.
55
+ function topIn(el) {
56
+ return historyEl.scrollTop + el.getBoundingClientRect().top - historyEl.getBoundingClientRect().top;
57
+ }
58
+
59
+ // The history to +el+'s top (none: its end). An +el+ gone meanwhile (a
60
+ // session switch drew another history) moves nothing: the scroll stays.
61
+ function scrollTo(el = null) {
62
+ return easeScrollTo(historyEl, () => {
63
+ if (el && !el.isConnected) return historyEl.scrollTop;
64
+ const end = historyEl.scrollHeight - historyEl.clientHeight;
65
+ return el ? Math.max(0, Math.min(end, topIn(el) - READ_GAP)) : end;
66
+ }, { reduce: reduce() });
67
+ }
68
+
69
+ // +pieces+ to the history's end in order; a reader at the end follows
70
+ // (+follow+: false when a reason's read follows instead).
71
+ function add(pieces, { follow = atHistoryEnd() } = {}) {
72
+ moveToHistoryEnd(historyEl, pieces);
73
+ if (follow) scrollTo();
74
+ }
75
+
76
+ function reasonBubble({ text, refs, step, turnId, questionId }) {
77
+ const bubble = document.createElement("div");
78
+ bubble.className = "bubble said pending";
79
+ if (questionId != null) bubble.dataset.questionId = String(questionId);
80
+ if (turnId) bubble.dataset.saidTurn = turnId;
81
+ const label = document.createElement("div");
82
+ label.className = "said-label";
83
+ const who = document.createElement("span");
84
+ who.textContent = step ? `chi · step ${step}` : "chi";
85
+ const asking = document.createElement("span");
86
+ asking.className = "said-asking";
87
+ asking.textContent = "asking you";
88
+ label.append(who, asking);
89
+ const body = document.createElement("div");
90
+ body.className = "said-text";
91
+ body.textContent = text;
92
+ bubble.append(label, body);
93
+ linkRefs(body, refs);
94
+ // Its markdown, under the reader's first lines (the bubble's one
95
+ // height change).
96
+ Promise.resolve()
97
+ .then(() => renderMarkdown(text))
98
+ .catch(() => null)
99
+ .then((html) => {
100
+ if (!html || !bubble.isConnected) return;
101
+ // Sanitised on the server (POST /api/render).
102
+ body.innerHTML = html;
103
+ body.classList.add("markdown", "md-compact");
104
+ linkRefs(body, refs);
105
+ });
106
+ return bubble;
107
+ }
108
+
109
+ // The card's way back to its reason, at its title line's right.
110
+ function reasonLink(card, bubble) {
111
+ const summary = card.querySelector(":scope > summary");
112
+ if (!summary) return;
113
+ const link = document.createElement("button");
114
+ link.type = "button";
115
+ link.className = "question-reason-link";
116
+ link.textContent = "↑ its reason";
117
+ link.title = "Show the reply that asked this";
118
+ link.addEventListener("click", (e) => {
119
+ // Inside the summary: no toggle.
120
+ e.preventDefault();
121
+ e.stopPropagation();
122
+ if (bubble.isConnected) scrollTo(bubble);
123
+ });
124
+ summary.appendChild(link);
125
+ }
126
+
127
+ // The first of the history's rows after one of +going+ (they move when
128
+ // it goes) whose bottom is below the view's top; null: none (what shows
129
+ // is above them all, and stays).
130
+ function firstInView(going) {
131
+ const top = historyEl.getBoundingClientRect().top;
132
+ let after = false;
133
+ for (const el of historyEl.children) {
134
+ if (going.includes(el)) {
135
+ after = true;
136
+ continue;
137
+ }
138
+ if (after && el.getBoundingClientRect().bottom > top) return el;
139
+ }
140
+ return null;
141
+ }
142
+
143
+ return {
144
+ // The turn's pieces so far (handOffOrder's order) into the history; the
145
+ // reason's read (ask) does the follow.
146
+ open: (pieces) => add(pieces, { follow: false }),
147
+ // A row of the opened turn: to the history's end.
148
+ add: (el) => add([el]),
149
+ // +card+ blocks the turn: its reason's bubble at the history's end, in
150
+ // the card's task; a reader at the end reads it from its first line.
151
+ // +follow+: the reader was at the end before the turn opened.
152
+ ask({ card, text, refs = null, step = 0, turnId = null, questionId = null, follow = atHistoryEnd() }) {
153
+ const bubble = reasonBubble({ text, refs, step, turnId, questionId });
154
+ moveToHistoryEnd(historyEl, [bubble]);
155
+ reasons.set(card, bubble);
156
+ // The step's own text is the reason: not twice (and raw while the
157
+ // step runs). data-said, not a class: lit owns the step's class.
158
+ const stepEl = askingStep();
159
+ if (stepEl) {
160
+ stepEl.dataset.said = "1";
161
+ saidSteps.add(stepEl);
162
+ }
163
+ reasonLink(card, bubble);
164
+ // The reader at the end reads it from its first line: meanwhile the
165
+ // history is marked data-reading, so the bottom reserve's follow
166
+ // (dock_reserve.js) doesn't take the end back.
167
+ if (follow) {
168
+ historyEl.dataset.reading = "";
169
+ afterSettle(() => {
170
+ const done = () => { delete historyEl.dataset.reading; };
171
+ if (bubble.isConnected) scrollTo(bubble).then(done, done);
172
+ else done();
173
+ });
174
+ }
175
+ return bubble;
176
+ },
177
+ has: (card) => reasons.has(card),
178
+ // +card+ no longer waits (answered, cancelled): it goes right under its
179
+ // reason, which stops saying "asking you"; another card of the turn to
180
+ // the history's end. A reader at the end follows.
181
+ settle(card) {
182
+ const bubble = reasons.get(card);
183
+ if (!bubble?.isConnected) return add([card]);
184
+ const follow = atHistoryEnd();
185
+ bubble.classList.remove("pending");
186
+ bubble.after(card);
187
+ if (follow) scrollTo();
188
+ },
189
+ // The turn's steps block (or one of its steps, +el+) in the history:
190
+ // unfolded and brought into view.
191
+ reveal(el) {
192
+ if (!el) return;
193
+ revealInHistory(el);
194
+ if (el.matches?.("details") && !el.open) el.open = true;
195
+ scrollTo(el);
196
+ },
197
+ // The reasons drawn (the hand-off waits while one is in use).
198
+ watched: () => [...reasons.values()].filter((el) => el.isConnected),
199
+ // The hand-off: the reasons go; each card's own clamp says it now. The
200
+ // reader keeps what they see: the first row in view that stays (the
201
+ // card under a reason they read) stays where it is (the history's own
202
+ // scroll anchoring has none to keep when the row it anchored on goes);
203
+ // a reader at the end then follows the turn's arrival (stage_view.js
204
+ // finishFold).
205
+ end() {
206
+ const gone = [...reasons.values()].filter((el) => el.isConnected);
207
+ reasons.clear();
208
+ clearSaid();
209
+ if (!gone.length) return;
210
+ const anchor = firstInView(gone);
211
+ const before = anchor?.getBoundingClientRect().top;
212
+ for (const bubble of gone) bubble.remove();
213
+ if (anchor) historyEl.scrollTop += anchor.getBoundingClientRect().top - before;
214
+ },
215
+ // The turn left the stage without a hand-off (a reset): forgotten, the
216
+ // history is drawn anew.
217
+ reset() {
218
+ reasons.clear();
219
+ saidSteps.clear();
220
+ },
221
+ // The turn was released without its end (a lost turn_completed, the
222
+ // next turn started): its reasons go, nothing else moves.
223
+ drop() {
224
+ for (const bubble of reasons.values()) bubble.remove();
225
+ reasons.clear();
226
+ clearSaid();
227
+ },
228
+ };
229
+ }
@@ -91,8 +91,7 @@ export function createStageSlots({
91
91
  // A null isn't kept: the next draw of this text asks again.
92
92
  if (!html && rendering === pending) rendering = { text: null, html: null };
93
93
  if (shown.asking !== text || askingFinal) return;
94
- // The raw text is final (markdown off, a failed read): the stage
95
- // measures it now.
94
+ // The raw text is final (markdown off, a failed read).
96
95
  if (!html) {
97
96
  askingFinal = true;
98
97
  onChange?.();
@@ -106,7 +105,7 @@ export function createStageSlots({
106
105
  asking.scrollTop = 0;
107
106
  linkRefs(asking, askingRefs);
108
107
  askingFinal = true;
109
- // Its height changed: the stage measures it now (the question room).
108
+ // Its height changed: the stage refits.
110
109
  onChange?.();
111
110
  });
112
111
  }