samagotchi 0.97.0 → 0.98.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 (46) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +30 -1
  3. data/docs/architecture.md +10 -2
  4. data/docs/bundle-authoring.md +5 -3
  5. data/docs/cli.md +8 -3
  6. data/docs/hooks.md +59 -0
  7. data/docs/internals/bundles.md +1 -1
  8. data/docs/internals/engine.md +18 -0
  9. data/docs/plugins.md +8 -1
  10. data/docs/sessions.md +1 -1
  11. data/lib/samagotchi/answer_refs.rb +16 -9
  12. data/lib/samagotchi/bundle_command.rb +2 -2
  13. data/lib/samagotchi/bundles/dev/manifest.yml +2 -2
  14. data/lib/samagotchi/bundles/source-links/hooks/source_links.rb +39 -5
  15. data/lib/samagotchi/bundles/source-links/manifest.yml +5 -5
  16. data/lib/samagotchi/bundles/system/manifest.yml +1 -1
  17. data/lib/samagotchi/engine.rb +26 -2
  18. data/lib/samagotchi/hooks/bundle_loader.rb +18 -15
  19. data/lib/samagotchi/hooks.rb +4 -1
  20. data/lib/samagotchi/llm/errors.rb +51 -17
  21. data/lib/samagotchi/memory_bundle/bundle_hook.rb +28 -9
  22. data/lib/samagotchi/memory_bundle/manifest.rb +17 -0
  23. data/lib/samagotchi/output_formatter.rb +8 -2
  24. data/lib/samagotchi/pending_question.rb +7 -3
  25. data/lib/samagotchi/plugin/api.rb +5 -0
  26. data/lib/samagotchi/question_asked.rb +83 -0
  27. data/lib/samagotchi/question_desk.rb +3 -2
  28. data/lib/samagotchi/testing/bundle.rb +18 -1
  29. data/lib/samagotchi/version.rb +1 -1
  30. data/lib/samagotchi/web/app.rb +30 -9
  31. data/lib/samagotchi/web/public/app.js +44 -11
  32. data/lib/samagotchi/web/public/index.html +36 -13
  33. data/lib/samagotchi/web/public/info_bar_model.js +0 -2
  34. data/lib/samagotchi/web/public/lists_view.js +32 -8
  35. data/lib/samagotchi/web/public/markdown_place.js +25 -8
  36. data/lib/samagotchi/web/public/question_cards.js +23 -6
  37. data/lib/samagotchi/web/public/session_rename.js +88 -15
  38. data/lib/samagotchi/web/public/session_title.js +6 -0
  39. data/lib/samagotchi/web/public/stage_frame.js +24 -14
  40. data/lib/samagotchi/web/public/stage_model.js +4 -2
  41. data/lib/samagotchi/web/public/stage_slots.js +41 -9
  42. data/lib/samagotchi/web/public/stage_view.js +149 -8
  43. data/lib/samagotchi/web/public/stream_handlers.js +5 -1
  44. data/lib/samagotchi/web/public/turn_model.js +20 -0
  45. data/lib/samagotchi/web/public/turn_view.js +22 -9
  46. metadata +2 -1
@@ -1,15 +1,25 @@
1
- // The info bar's ✎ (Rename this session): a small input in its place,
1
+ // The title's ✎ (Rename this session): a small input over the title as it
2
+ // shows on screen (the active strip card's title row, or the top bar's),
2
3
  // prefilled with the session's title ("" for none). Enter sends
3
4
  // `/title TEXT` (blank: `/title --clear`) through the chips' command path
4
5
  // (composer.js run: the reply shows as a command bubble, a refusal in the
5
6
  // failed bubble, the composer untouched); leaving the field saves the same
6
- // way (a phone's keyboard dismissed keeps what was typed). Esc, or opening
7
- // another session, closes it unsent. The cards follow from the saved file
8
- // (the hub's scan), as for any title.
7
+ // way (a phone's keyboard dismissed keeps what was typed). Esc, opening
8
+ // another session, or the title leaving the screen closes it unsent. The
9
+ // cards follow from the saved file (the hub's scan), as for any title.
10
+ //
11
+ // The input lives at the end of body, fixed over its anchor's rect: the
12
+ // strip draws every card from new nodes on each session event, so an input
13
+ // inside a card would be thrown away mid-typing.
9
14
  import { normalize } from "./format.js";
10
15
 
11
16
  // As long as a title may be (TitleCommand::LIMIT).
12
17
  const TITLE_CHARS = 80;
18
+ // The narrowest field (a short title still gets room to type), the gap kept
19
+ // to the window's right edge, and the lowest field.
20
+ const MIN_WIDTH_PX = 192;
21
+ const EDGE_PX = 16;
22
+ const MIN_HEIGHT_PX = 24;
13
23
 
14
24
  // The line the input's +text+ sends.
15
25
  export function titleLine(text) {
@@ -17,21 +27,47 @@ export function titleLine(text) {
17
27
  return title ? `/title ${title}` : "/title --clear";
18
28
  }
19
29
 
20
- // @param button the ✎ button (it hides while the input shows)
30
+ // Where the input goes over +rect+ (the anchor's) in a window +innerWidth+
31
+ // wide: the anchor's box, at least MIN_WIDTH_PX wide and MIN_HEIGHT_PX tall
32
+ // (centred on it), never closer than EDGE_PX to the right edge.
33
+ export function inputBox(rect, innerWidth) {
34
+ const height = Math.max(rect.height, MIN_HEIGHT_PX);
35
+ const room = Math.max(innerWidth - rect.left - EDGE_PX, 0);
36
+ const width = Math.min(Math.max(rect.width, MIN_WIDTH_PX), room);
37
+ return { left: rect.left, top: rect.top + (rect.height - height) / 2, width, height };
38
+ }
39
+
40
+ // Whether a card's title row +rect+ has left its scroller's +clip+ (a
41
+ // phone's strip swiped sideways: a fixed input isn't clipped by its overflow).
42
+ export function outsideClip(rect, clip) {
43
+ return rect.left < clip.left - 1 || rect.right > clip.right + 1 || rect.bottom < clip.top || rect.top > clip.bottom;
44
+ }
45
+
46
+ // @param doc the document (the input goes at the end of its body)
47
+ // @param anchor () => the title row on screen for the open session (its ✎
48
+ // inside), or null when none shows
21
49
  // @param session () => the open session's id, or null
22
50
  // @param titleOf (id) => that session's title, or null
51
+ // @param shownTitle (id) => the title as the page shows it (the
52
+ // placeholder: an untitled chain link shows its chain's), or ""
53
+ // @param closePopovers () => close the family and chain popovers (their Esc
54
+ // would close the popover and refocus its chip: a blur, so a save)
55
+ // @param onClose () => after the input closed (a held strip catches up)
23
56
  // @param run (line) => composer.js run's promise
24
- export function createRename({ button, session, titleOf, run }) {
57
+ export function createRename({ doc = document, anchor, session, titleOf, shownTitle = () => "", closePopovers = () => {}, onClose = () => {}, run }) {
25
58
  let input = null;
26
59
  let openFor = null;
60
+ const win = doc.defaultView;
27
61
 
62
+ // Removing the input blurs it: input is cleared first, so that blur sends
63
+ // nothing (a close is always unsent).
28
64
  function close() {
29
65
  if (!input) return;
30
66
  const el = input;
31
67
  input = null;
32
68
  openFor = null;
33
69
  el.remove();
34
- button.hidden = false;
70
+ onClose();
35
71
  }
36
72
 
37
73
  // Enter's and blur's: once (closing it blurs it again), and only to the
@@ -46,14 +82,40 @@ export function createRename({ button, session, titleOf, run }) {
46
82
  return run(titleLine(text));
47
83
  }
48
84
 
85
+ // Over the anchor's title row, in its title's font size (CSS keeps a
86
+ // phone's at 16px: under that, iOS zooms the page on focus).
87
+ function place(a) {
88
+ const box = inputBox(a.getBoundingClientRect(), win.innerWidth);
89
+ Object.assign(input.style, {
90
+ left: `${box.left}px`, top: `${box.top}px`, width: `${box.width}px`, height: `${box.height}px`,
91
+ });
92
+ const text = a.firstElementChild;
93
+ const size = text && win.getComputedStyle ? win.getComputedStyle(text).fontSize : "";
94
+ if (size) input.style.setProperty("--rename-font", size);
95
+ // The top bar's row runs to the bell: CSS caps the input there, so it
96
+ // reads as the title edited, not a field across the bar.
97
+ input.classList.toggle("over-bar", !a.closest("#topStrip"));
98
+ }
99
+
100
+ // A card's row out of the strip's sight (swiped sideways).
101
+ function clipped(a) {
102
+ const strip = a.closest("#topStrip");
103
+ return !!strip && outsideClip(a.getBoundingClientRect(), strip.getBoundingClientRect());
104
+ }
105
+
49
106
  function open() {
50
107
  const id = session();
51
108
  if (input || !id) return;
52
- const el = document.createElement("input");
109
+ const a = anchor();
110
+ if (!a) return;
111
+ closePopovers();
112
+ // The active card may sit scrolled sideways out of a phone's strip.
113
+ a.scrollIntoView?.({ block: "nearest", inline: "nearest" });
114
+ const el = doc.createElement("input");
53
115
  el.type = "text";
54
116
  el.className = "rename-input";
55
117
  el.maxLength = TITLE_CHARS;
56
- el.placeholder = "Session title";
118
+ el.placeholder = normalize(shownTitle(id)) || "Session title";
57
119
  el.setAttribute("aria-label", "Session title (Enter saves, empty clears, Esc cancels)");
58
120
  el.value = titleOf(id) || "";
59
121
  el.dataset.before = el.value;
@@ -64,23 +126,34 @@ export function createRename({ button, session, titleOf, run }) {
64
126
  } else if (e.key === "Escape") {
65
127
  e.preventDefault();
66
128
  close();
129
+ // The ✎ as drawn now (the strip may have drawn its card again).
130
+ anchor()?.querySelector("button")?.focus();
67
131
  }
68
132
  });
69
133
  el.addEventListener("blur", () => submit());
70
134
  input = el;
71
135
  openFor = id;
72
- button.hidden = true;
73
- button.after(el);
136
+ doc.body.append(el);
137
+ place(a);
74
138
  el.focus();
75
139
  el.select();
76
140
  }
77
141
 
78
- // The info bar redrew for +id+ (app.js updateInfoBar): an input opened
79
- // for another session closes.
142
+ // The title moved (a strip draw, the strip hidden or shown, the page
143
+ // scrolled or resized): the input follows it. With no title on screen (the
144
+ // All sessions view, the title gone, the card swiped away) it closes unsent.
145
+ function reposition() {
146
+ if (!input) return;
147
+ const a = anchor();
148
+ if (!a || clipped(a)) close();
149
+ else place(a);
150
+ }
151
+
152
+ // The page shows +id+ (app.js updateSessionTitle): an input opened for
153
+ // another session closes.
80
154
  function follow(id) {
81
155
  if (input && openFor !== id) close();
82
156
  }
83
157
 
84
- button.addEventListener("click", () => open());
85
- return { open, close, follow, isOpen: () => !!input };
158
+ return { open, close, follow, reposition, isOpen: () => !!input };
86
159
  }
@@ -16,6 +16,12 @@ export function topBarTitle({ title, cardShown, stripHidden, allView }) {
16
16
  return text;
17
17
  }
18
18
 
19
+ // Whether the title's ✎ shows (session_rename.js): any session but a plain
20
+ // terminal's (`owner: "tui"`: it takes no commands from the web).
21
+ export function renameShown(owner) {
22
+ return owner !== "tui";
23
+ }
24
+
19
25
  // The browser tab: "<title, cut at 40> · <base>", the base alone without a
20
26
  // session title (base = "Chi · <project>" or "Chi").
21
27
  export function tabTitle({ title, base }) {
@@ -1,10 +1,12 @@
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), and never above the session
3
+ // ~60% of the page (~80% with the cloud open, 85% for the rest of a turn
4
+ // whose question got the question room), and never above the session
4
5
  // panel. The frame: held and grow-only while the stage holds a turn
5
6
  // (stage_model.js frameTarget), its growth eased. The stage's state comes
6
- // in as getters (+isHolding+, +isCollapsed+, +isCloudOpen+); what the stage
7
- // changes of the frame's goes through resetHeld and forgetContent.
7
+ // in as getters (+isHolding+, +isCollapsed+, +isCloudOpen+, +isRoomCap+);
8
+ // what the stage changes of the frame's goes through resetHeld and
9
+ // forgetContent.
8
10
  import { frameTarget } from "./stage_model.js";
9
11
  import { easeScrollTo } from "./scroll.js";
10
12
 
@@ -14,6 +16,11 @@ const GROW_MS = 180;
14
16
  const GROW_EASE = "cubic-bezier(.2,.7,.3,1)";
15
17
  // The room between the stage and the composer card (index.html's margin-bottom).
16
18
  export const STAGE_GAP = 10;
19
+ // The cap's share of the page: the stage's own, with the cloud open, and
20
+ // with the question room (a long reply that asks, stage_view.js).
21
+ const SHARE = 0.6;
22
+ const CLOUD_SHARE = 0.8;
23
+ const ROOM_SHARE = 0.85;
17
24
 
18
25
  export function reducedMotion() {
19
26
  return matchMedia("(prefers-reduced-motion: reduce)").matches;
@@ -21,7 +28,7 @@ export function reducedMotion() {
21
28
 
22
29
  // +observed+: the elements whose size changes refit the frame (the stage's
23
30
  // slots, in the order they are observed).
24
- export function createStageFrame({ stage, scroller, status, cloud, queuedSlot, holdEl, asking, extras, dockEl, centerEl, composerEl, observed, isHolding, isCollapsed, isCloudOpen }) {
31
+ export function createStageFrame({ stage, scroller, status, cloud, queuedSlot, holdEl, asking, extras, dockEl, centerEl, composerEl, observed, isHolding, isCollapsed, isCloudOpen, isRoomCap = () => false }) {
25
32
  // The frame (fitFrame): the height held while the turn runs and the
26
33
  // tallest content seen.
27
34
  let held = null;
@@ -45,25 +52,28 @@ export function createStageFrame({ stage, scroller, status, cloud, queuedSlot, h
45
52
  }));
46
53
  }
47
54
 
48
- // ── the cap: at most ~60% of the page (~80% with the cloud open), and
49
- // never above the session panel (the dock is absolute: a tall composer or
50
- // a phone's keyboard would push the stage's top out) ────────────────────
55
+ // ── the cap: at most ~60% of the page (~80% with the cloud open, 85%
56
+ // once the turn's question got the room), and never above the session
57
+ // panel (the dock is absolute: a tall composer or a phone's keyboard
58
+ // would push the stage's top out) ──────────────────────────────────────
51
59
 
52
60
  // Where the stage's scroll goes so +slot+ (the scroller's child) shows:
53
61
  // its end in sight, and the asking text's top too when it and the slot
54
62
  // (a waiting card) fit together (the reply reads from its start, the card
55
- // still in sight). null: nothing to show.
56
- function revealTop(slot) {
63
+ // still in sight). +fromStart+ (the question room's arrival): the asking
64
+ // text's top even when they don't (the card is reached by the stage's
65
+ // jump pill, stage_view.js). null: nothing to show.
66
+ function revealTop(slot, { fromStart = false } = {}) {
57
67
  if (stage.hidden || slot.hidden) return null;
58
68
  let top = Math.max(scroller.scrollTop, slot.offsetTop + slot.offsetHeight - scroller.clientHeight);
59
- if (!asking.hidden && asking.offsetTop < top && slot.offsetTop + slot.offsetHeight - asking.offsetTop <= scroller.clientHeight) top = asking.offsetTop;
69
+ if (!asking.hidden && asking.offsetTop < top && (fromStart || slot.offsetTop + slot.offsetHeight - asking.offsetTop <= scroller.clientHeight)) top = asking.offsetTop;
60
70
  return top;
61
71
  }
62
72
 
63
73
  // Scrolls the stage to show +slot+: eased, or at once (+now+: the cap
64
- // dropped under it).
65
- function reveal(slot, { now = false } = {}) {
66
- const top = revealTop(slot);
74
+ // dropped under it). +fromStart+: revealTop's.
75
+ function reveal(slot, { now = false, fromStart = false } = {}) {
76
+ const top = revealTop(slot, { fromStart });
67
77
  if (top === null) return;
68
78
  if (now) scroller.scrollTop = top;
69
79
  else easeStage(top);
@@ -75,7 +85,7 @@ export function createStageFrame({ stage, scroller, status, cloud, queuedSlot, h
75
85
  // out of the dock's flow).
76
86
  const rest = dockEl.offsetHeight + STAGE_GAP;
77
87
  const room = centerEl.clientHeight - rest - 32;
78
- const share = window.innerHeight * (isCloudOpen() ? 0.8 : 0.6);
88
+ const share = window.innerHeight * (isRoomCap() ? ROOM_SHARE : isCloudOpen() ? CLOUD_SHARE : SHARE);
79
89
  const cap = Math.max(96, Math.round(Math.min(share, room)));
80
90
  const was = capPx();
81
91
  if (cap === was) return;
@@ -6,7 +6,7 @@ import { toolName } from "./activity.js";
6
6
  import { splitSentences } from "./sentences.js";
7
7
  import { steerSender } from "./format.js";
8
8
  import { hookNoticeLabel } from "./turn_events.js";
9
- import { askingText } from "./turn_model.js";
9
+ import { askingRefs, askingText } from "./turn_model.js";
10
10
  import { plainLine } from "./plain_line.js";
11
11
 
12
12
  const READ_LIKE = new Set(["read", "memory_read", "web_fetch", "task_get", "task_list", "list_sessions"]);
@@ -156,7 +156,8 @@ const NO_ANSWER = "no answer";
156
156
  // without its dir; command as for tool);
157
157
  // ticks one per call: {kind, status, step (the gen's index)};
158
158
  // asking the whole text of the reply whose call a card blocks
159
- // (turn_model.js askingText), "" when none is blocked.
159
+ // (turn_model.js askingText), "" when none is blocked;
160
+ // askingRefs a plugin's refs on it (turn_model.js askingRefs), or null.
160
161
  // +pending+: a card waits for the user; +blocked+: a card that blocks the
161
162
  // turn (a question, an approval; not an action card) waits; +ended+: the
162
163
  // turn's end kind;
@@ -191,6 +192,7 @@ export function liveSlots(turn, { narration = "", thinking = "", pending = false
191
192
  trail,
192
193
  ticks: rows.map(({ row, step }) => ({ kind: toolKind(row.tool), status: row.status, step })),
193
194
  asking: blocked && !ended ? askingText(turn) : "",
195
+ askingRefs: blocked && !ended ? askingRefs(turn) : null,
194
196
  };
195
197
  }
196
198
 
@@ -21,17 +21,23 @@ export function node(tag, cls, parent = null) {
21
21
  // command, read at its click); +onChange+ redraws (the stage's
22
22
  // scheduleRefresh). +renderMarkdown(text)+: the asking text's HTML
23
23
  // (Promise<string|null>; null keeps the raw text: markdown off, a failed
24
- // read); +linkRefs(el)+: the page's path:line ref pass on the slot.
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).
25
26
  export function createStageSlots({
26
27
  headline, asking, now, trail, ticks, taskStop = null, gens, onChange,
27
28
  renderMarkdown = () => Promise.resolve(null), linkRefs = () => {},
28
29
  }) {
29
30
  // What each slot shows, so an unchanged slot is left alone.
30
- const shown = { headline: null, now: null, trail: null, asking: null };
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;
31
34
  // The last asking text sent to render and its answer (in flight or
32
35
  // done, HTML only): the same question drawn again (after a reset, a
33
36
  // blank between) asks no second time.
34
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;
35
41
 
36
42
  function drawHeadline({ text, thinking }) {
37
43
  const key = `${thinking ? "t" : "n"}|${text}`;
@@ -53,16 +59,28 @@ export function createStageSlots({
53
59
  // rewrites the same text): a selection in it survives. A new text reads
54
60
  // from its top: drawn raw at once, then swapped for its markdown when
55
61
  // that comes back while the slot still shows the same text (the text is
56
- // final: it shows only once the call that asks runs).
57
- function drawAsking(text) {
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) {
58
66
  asking.hidden = !text;
59
- if (shown.asking === text) return;
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
+ }
60
75
  shown.asking = text;
76
+ shown.askingRefs = refsKey;
77
+ askingRefs = refsKey ? refs : null;
61
78
  asking.classList.remove("markdown", "md-compact");
62
79
  asking.textContent = text;
63
80
  asking.scrollTop = 0;
81
+ askingFinal = false;
64
82
  if (!text) return;
65
- linkRefs(asking);
83
+ linkRefs(asking, askingRefs);
66
84
  if (rendering.text !== text) {
67
85
  let html;
68
86
  try { html = Promise.resolve(renderMarkdown(text)).catch(() => null); } catch (_) { html = Promise.resolve(null); }
@@ -72,13 +90,24 @@ export function createStageSlots({
72
90
  pending.html.then((html) => {
73
91
  // A null isn't kept: the next draw of this text asks again.
74
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
+ }
75
101
  // Swapped once: a blank-then-same draw while in flight waits twice.
76
- if (!html || shown.asking !== text || asking.classList.contains("markdown")) return;
102
+ if (asking.classList.contains("markdown")) return;
77
103
  // Sanitised on the server (POST /api/render), as an answer's HTML is.
78
104
  asking.innerHTML = html;
79
105
  asking.classList.add("markdown", "md-compact");
80
106
  asking.scrollTop = 0;
81
- linkRefs(asking);
107
+ linkRefs(asking, askingRefs);
108
+ askingFinal = true;
109
+ // Its height changed: the stage measures it now (the question room).
110
+ onChange?.();
82
111
  });
83
112
  }
84
113
 
@@ -156,7 +185,10 @@ export function createStageSlots({
156
185
  asking.hidden = true;
157
186
  asking.classList.remove("markdown", "md-compact");
158
187
  shown.headline = shown.now = shown.trail = shown.asking = null;
188
+ shown.askingRefs = "";
189
+ askingRefs = null;
190
+ askingFinal = false;
159
191
  }
160
192
 
161
- return { drawHeadline, drawAsking, drawNow, drawTrail, drawTicks, reset };
193
+ return { drawHeadline, drawAsking, drawNow, drawTrail, drawTicks, reset, askingSettled: () => askingFinal };
162
194
  }
@@ -40,6 +40,10 @@ const PENDING = ".bubble.question:not(.answered):not(.cancelled), .bubble.contin
40
40
  // asks the user too, but the turn goes on: it waits only while the turn runs
41
41
  // and never holds the hand-off (a turn cut short can leave it open).
42
42
  const ASKS = ".plugin-card[data-in-turn] .card-action";
43
+ // The model's own question (question_cards.js data-kind; not an approval,
44
+ // the step limit, a hook's question or a delegate's relayed one): the one
45
+ // card whose long asking text gets the question room.
46
+ const ASK_CARD = '.bubble.question[data-kind="question"]:not([data-relay])';
43
47
 
44
48
  function readCollapsed() {
45
49
  return readStored(COLLAPSE_KEY) === "1";
@@ -113,6 +117,14 @@ export function createStageView({
113
117
  chip.type = "button";
114
118
  const chipLabel = node("span", "ts-chip-label", chip);
115
119
  const ticks = node("span", "ts-ticks", chip);
120
+ // The question room's jump pill, at the scroller's bottom edge (absolute,
121
+ // above the chip): while the room's card is out of the stage's view it
122
+ // eases the stage to it (drawPill).
123
+ const pill = node("button", "ts-room-pill", cloud);
124
+ pill.type = "button";
125
+ pill.setAttribute("aria-label", "Go to the question");
126
+ pill.hidden = true;
127
+ pill.textContent = "❓ Answer ↓";
116
128
  // Below the turn's card: the lines queued for after it (placeKind
117
129
  // "queued"), each to run as a turn of its own; they hand off last.
118
130
  const queuedSlot = node("div", "ts-queued", stage);
@@ -140,6 +152,21 @@ export function createStageView({
140
152
  let revealed = false;
141
153
  let rafPending = false;
142
154
  let foldTimer = null;
155
+ // The question room (drawRoom): the pending ask card whose asking text
156
+ // overflowed its cap, and the turn's raised cap (stage_frame.js; kept
157
+ // until the next turn: the frame is grow-only).
158
+ let roomCard = null;
159
+ let roomCap = false;
160
+ // What the overflow was measured for ({ card, text }): measured once the
161
+ // asking text shows in its final form (its markdown, or the raw text when
162
+ // there is none: the raw text is taller, and a short question's could
163
+ // overflow where its markdown fits), again only for a new card or text,
164
+ // never at a cap's change (a phone's keyboard).
165
+ let measured = null;
166
+ // The ask card whose arrival reveal has run (no pill while the stage
167
+ // grows to it), and whether the room's card is in the stage's view.
168
+ let arrived = null;
169
+ let cardInSight = true;
143
170
 
144
171
  const view = createTurnView({
145
172
  historyEl,
@@ -181,6 +208,69 @@ export function createStageView({
181
208
  return !!(extras.querySelector(PENDING) || tail.querySelector(PENDING));
182
209
  }
183
210
 
211
+ // The pending card in the extras when it is the model's own question
212
+ // (never the tail's: a post-end card, the step limit; asking is empty then).
213
+ function askCard() {
214
+ const card = extras.querySelector(PENDING);
215
+ return card?.matches(ASK_CARD) ? card : null;
216
+ }
217
+
218
+ // The asking text overflows its cap (+1 px of rounding), measured with
219
+ // the room's class off.
220
+ function askingOverflows() {
221
+ return !asking.hidden && asking.scrollHeight > asking.clientHeight + 1;
222
+ }
223
+
224
+ // The question room: while a model's own question waits and its asking
225
+ // text didn't fit its cap, the slot loses its cap and the stage's one
226
+ // scroll reads the whole reply (index.html .question-room); the cap rises
227
+ // for the rest of the turn. The card keeps the room until it stops
228
+ // pending (+text+: the asking text drawn).
229
+ function drawRoom(blocked, text) {
230
+ const ask = blocked ? askCard() : null;
231
+ if (roomCard && ask === roomCard) return;
232
+ if (roomCard) {
233
+ cardSight.unobserve(roomCard);
234
+ stage.classList.remove("question-room");
235
+ roomCard = null;
236
+ }
237
+ if (!ask || !slotsView.askingSettled() || (measured && measured.card === ask && measured.text === text)) return;
238
+ measured = { card: ask, text };
239
+ if (!askingOverflows()) return;
240
+ roomCard = ask;
241
+ stage.classList.add("question-room");
242
+ cardInSight = true;
243
+ cardSight.observe(roomCard);
244
+ if (!roomCap) {
245
+ roomCap = true;
246
+ frame.fitCap();
247
+ }
248
+ // Decided after the card's arrival reveal (a slow markdown read): the
249
+ // reply reads from its start now, the pill only after that.
250
+ if (arrived === ask) {
251
+ arrived = null;
252
+ frame.afterGrowth(() => {
253
+ if (roomCard !== ask) return;
254
+ frame.reveal(extras, { fromStart: true });
255
+ arrived = ask;
256
+ drawPill();
257
+ });
258
+ }
259
+ }
260
+
261
+ // The pill: the room's card out of the stage's view, once its arrival
262
+ // reveal has run, the stage open.
263
+ function drawPill() {
264
+ pill.hidden = !(roomCard && !collapsed && arrived === roomCard && !cardInSight);
265
+ }
266
+
267
+ // The room's card in the stage's view: ~40 px of it shows above the
268
+ // scroller's bottom edge (where the pill sits).
269
+ const cardSight = new IntersectionObserver((entries) => {
270
+ for (const entry of entries) if (entry.target === roomCard) cardInSight = entry.isIntersecting;
271
+ drawPill();
272
+ }, { root: scroller, rootMargin: "0px 0px -40px 0px", threshold: 0 });
273
+
184
274
  function asksCard() {
185
275
  return !endKind && !!extras.querySelector(ASKS);
186
276
  }
@@ -214,7 +304,10 @@ export function createStageView({
214
304
  // next step (held-wait, set with waiting): the card under them never
215
305
  // moves at the answer. The waiting height is measured before the swap.
216
306
  const wasWaiting = stage.classList.contains("waiting");
217
- if (blocked && !wasWaiting) stage.classList.add("held-wait");
307
+ if (blocked && !wasWaiting) {
308
+ stage.classList.add("held-wait");
309
+ live.style.minHeight = "";
310
+ }
218
311
  if (!blocked && wasWaiting) stage.style.setProperty("--live-held", `${live.offsetHeight}px`);
219
312
  stage.classList.toggle("plain", plain);
220
313
  stage.classList.toggle("ended", !!endKind);
@@ -225,7 +318,9 @@ export function createStageView({
225
318
  phaseEl.textContent = slots.phase;
226
319
 
227
320
  slotsView.drawHeadline(slots.headline);
228
- slotsView.drawAsking(slots.asking);
321
+ slotsView.drawAsking(slots.asking, slots.askingRefs);
322
+ drawRoom(blocked, slots.asking);
323
+ drawPill();
229
324
  slotsView.drawNow(slots.tool, slots.quiet);
230
325
  slotsView.drawTrail(slots.trail, flash);
231
326
  slotsView.drawTicks(slots.ticks);
@@ -253,6 +348,25 @@ export function createStageView({
253
348
  tail.hidden = !tail.firstChild;
254
349
  }
255
350
 
351
+ // The next step: the live slots' waiting height is let go. In a turn
352
+ // with the question room that height was the whole reply's: it goes as
353
+ // far as the stage's scroll absorbs it (the scroll moves up by as much,
354
+ // in the same task: the card in view stays put), the rest kept as the
355
+ // live slots' least height until the next wait or turn.
356
+ function releaseHeldWait() {
357
+ const held = roomCap && stage.classList.contains("held-wait") && !stage.classList.contains("waiting");
358
+ const before = held ? live.offsetHeight : 0;
359
+ const top = scroller.scrollTop;
360
+ stage.classList.remove("held-wait");
361
+ stage.style.removeProperty("--live-held");
362
+ live.style.minHeight = "";
363
+ if (!held) return;
364
+ const after = live.offsetHeight;
365
+ const absorbed = Math.min(Math.max(0, before - after), top);
366
+ if (before - absorbed > after) live.style.minHeight = `${before - absorbed}px`;
367
+ if (absorbed) scroller.scrollTop = top - absorbed;
368
+ }
369
+
256
370
  // One line in the trail for FLASH_MS, ahead of the calls (a newer flash
257
371
  // takes its place).
258
372
  function flashTrail(what) {
@@ -339,6 +453,13 @@ export function createStageView({
339
453
  else setCloud(!cloudOpen);
340
454
  });
341
455
  headline.addEventListener("click", peek);
456
+ // To the card: its end in sight (the arrival's rule), or back up to it
457
+ // for a reader below it (the cloud open under the card).
458
+ pill.addEventListener("click", () => {
459
+ if (!roomCard) return;
460
+ if (roomCard.offsetTop < scroller.scrollTop) frame.easeStage(Math.max(0, roomCard.offsetTop - 8));
461
+ else frame.reveal(extras);
462
+ });
342
463
  // A ref's "show in turn" (refs.js) names a row in the cloud: open the
343
464
  // stage and the cloud to it, as a hand toggle would.
344
465
  // A collapsed stage opens for this turn only: the kept choice
@@ -378,6 +499,7 @@ export function createStageView({
378
499
  isHolding: () => holding,
379
500
  isCollapsed: () => collapsed,
380
501
  isCloudOpen: () => cloudOpen,
502
+ isRoomCap: () => roomCap,
381
503
  });
382
504
 
383
505
  // The in-use watch (stage_watch.js): an ended turn hands off once the
@@ -421,9 +543,16 @@ export function createStageView({
421
543
  thinkingLine = null;
422
544
  flash = null;
423
545
  clearTimeout(flashTimer);
546
+ if (roomCard) cardSight.unobserve(roomCard);
547
+ roomCard = null;
548
+ roomCap = false;
549
+ measured = null;
550
+ arrived = null;
551
+ pill.hidden = true;
424
552
  setCloud(false);
425
- stage.classList.remove("plain", "ended", "waiting", "held-wait", "asking", "in-use");
553
+ stage.classList.remove("plain", "ended", "waiting", "held-wait", "asking", "in-use", "question-room");
426
554
  stage.style.removeProperty("--live-held");
555
+ live.style.minHeight = "";
427
556
  }
428
557
 
429
558
  // The turn leaves the stage's care (the hand-off's start, a reset, a new
@@ -546,9 +675,7 @@ export function createStageView({
546
675
  refresh();
547
676
  },
548
677
  generationStarted(data, now = null) {
549
- // The next step: the live slots' waiting height is let go.
550
- stage.classList.remove("held-wait");
551
- stage.style.removeProperty("--live-held");
678
+ releaseHeldWait();
552
679
  view.generationStarted(data, now);
553
680
  },
554
681
  chunk: (data) => view.chunk(data),
@@ -600,6 +727,11 @@ export function createStageView({
600
727
  },
601
728
  containsLive: (el) => view.containsLive(el),
602
729
  askingText: () => view.askingText(),
730
+ // The asking slot pairs them on its next draw.
731
+ questionRefs(refs) {
732
+ view.questionRefs(refs);
733
+ scheduleRefresh();
734
+ },
603
735
  // The turn model and its live phase as the status row reads it (the
604
736
  // composer's delivery line).
605
737
  current: () => view.current(),
@@ -637,8 +769,17 @@ export function createStageView({
637
769
  else scheduleRefresh();
638
770
  // A card that waits for the user is brought into the stage's view: in
639
771
  // a short window the prompt and headline fill it and the card sat
640
- // below, out of sight (once the stage has grown for it).
641
- if (waits) frame.afterGrowth(() => frame.reveal(slot));
772
+ // below, out of sight (once the stage has grown for it). A question
773
+ // with the room reads from the reply's start instead (its pill leads
774
+ // to the card); its pill shows from here on.
775
+ if (waits) {
776
+ frame.afterGrowth(() => {
777
+ const ask = askCard();
778
+ frame.reveal(slot, { fromStart: !!ask && ask === roomCard });
779
+ arrived = ask;
780
+ drawPill();
781
+ });
782
+ }
642
783
  return true;
643
784
  },
644
785
  // Where the live timing line goes (the status row), null: the history.
@@ -332,7 +332,11 @@ export function createStreamHandlers(deps) {
332
332
  },
333
333
  question_requested: (data) => {
334
334
  const pq = data.pending_question;
335
- if (pq && pq.id) questionCards.renderQuestion(pq);
335
+ if (!pq || !pq.id) return;
336
+ // A plugin's refs on the reply that asks: on its step before the card
337
+ // draws the asking slot.
338
+ if (pq.refs) turnDom.questionRefs?.(pq.refs);
339
+ questionCards.renderQuestion(pq);
336
340
  },
337
341
  question_answered: (data) => {
338
342
  questionCards.resolveQuestion(data.id, { answer: data.answer });