@intentius/behold 0.30.0 → 0.30.2
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.
- package/AGENTS.md +97 -33
- package/README.md +78 -56
- package/dist/cli.js +6913 -2448
- package/example-choudoufu-estate/chant.workspace.json +1 -1
- package/example-terraform-estate/README.md +1 -1
- package/example-terraform-estate/chant.workspace.json +1 -1
- package/example-terralith/README.md +1 -1
- package/example-three-cloud/package-lock.json +19 -19
- package/example-three-cloud/package.json +4 -4
- package/package.json +5 -5
- package/web/app.js +330 -19
- package/web/embed.js +2 -2
- package/web/event-help-modal.js +18 -12
- package/web/event-help-modal.test.js +28 -1
- package/web/index.html +18 -0
- package/web/kiosk-ask-layout.js +85 -0
- package/web/kiosk-ask-layout.test.js +83 -0
- package/web/kiosk-ask-tips.js +467 -0
- package/web/kiosk-ask-tips.test.js +359 -0
- package/web/kiosk-ask.js +774 -0
- package/web/kiosk-ask.test.js +593 -0
- package/web/kiosk-banner.js +108 -0
- package/web/kiosk-banner.test.js +72 -0
- package/web/kiosk-center-cards.test.js +110 -0
- package/web/kiosk-estate-scene.js +203 -0
- package/web/kiosk-estate-scene.test.js +21 -0
- package/web/kiosk-event-help.js +4 -1
- package/web/kiosk-graph-pane.js +63 -0
- package/web/kiosk-graph-pane.test.js +105 -0
- package/web/kiosk-light-frame.js +196 -0
- package/web/kiosk-light.js +509 -0
- package/web/kiosk-light.test.js +297 -0
- package/web/kiosk-metrics.js +7 -4
- package/web/kiosk-modal-sticky.test.js +2 -1
- package/web/kiosk-refit.test.js +81 -0
- package/web/kiosk-reset.js +49 -0
- package/web/kiosk-reset.test.js +11 -0
- package/web/kiosk-shot.js +40 -2
- package/web/kiosk-shot.test.js +23 -1
- package/web/kiosk-vendor.js +85 -7
- package/web/kiosk-vendor.test.js +9 -9
- package/web/kiosk-watch.js +8 -4
- package/web/kiosk.html +107 -1
- package/web/kiosk.js +149 -23
- package/web/kiosk.test.js +25 -1
- package/web/layout-store.js +3 -3
- package/web/small-stage.test.js +4 -0
- package/web/terragucci-change.js +30 -13
- package/web/tool-mark.js +4 -4
- package/web/tool-mark.test.js +1 -1
- package/web/vendors/atmos.svg +1 -0
- package/web/vendors/chant.svg +1 -0
- package/web/vendors/choudoufu.svg +1 -0
- package/web/vendors/terragrunt.svg +9 -0
- package/web/vendors/terragucci.svg +1 -0
- package/web/vendors/choudoufu-128.png +0 -0
- package/web/vendors/choudoufu-64.png +0 -0
package/web/kiosk-ask.js
ADDED
|
@@ -0,0 +1,774 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* behold#936: the kiosk's prompt pane. While a demo step has an `ask` (or
|
|
3
|
+
* `asks`), a big monospace pane at the bottom of the screen replaces the
|
|
4
|
+
* header and the caption. It reads like a coding agent's terminal: every step
|
|
5
|
+
* is a turn, and the whole scenario stays on screen as one session.
|
|
6
|
+
*
|
|
7
|
+
* A turn goes through these states, in order:
|
|
8
|
+
*
|
|
9
|
+
* 1. ready: the pane's top edge is lit with a soft glow and an empty `>` line
|
|
10
|
+
* shows a blinking block cursor. It shows whenever the pane waits for the
|
|
11
|
+
* next prompt (before the first one, between beats, after a reply), and
|
|
12
|
+
* goes dim when typing starts;
|
|
13
|
+
* 2. typing: the person's prompt (or an action line, a check mark) is typed;
|
|
14
|
+
* 3. busy: a line in the agent's colour, a spinner glyph, the label (the
|
|
15
|
+
* beat's or step's `status`, else the step's label), an animated ellipsis
|
|
16
|
+
* and a dim elapsed `(m:ss)`. A beat with a prompt and a reply shows it for
|
|
17
|
+
* at least {@link BUSY_MIN_MS} before the reply types. A step with only a
|
|
18
|
+
* status shows it for the whole step, under the turn it belongs to;
|
|
19
|
+
* 4. replied: the busy line collapses into a dim "check label (m:ss)" line
|
|
20
|
+
* and the reply types below it.
|
|
21
|
+
*
|
|
22
|
+
* A beat whose ask has `diff` (short lines starting `+`, `~` or `-`) shows
|
|
23
|
+
* them under the finished reply as a small code block, one line every
|
|
24
|
+
* {@link DIFF_MS}, all at once under reduced motion.
|
|
25
|
+
*
|
|
26
|
+
* The graph changes with the words: `behold:ask-light` fires the moment a
|
|
27
|
+
* beat's reply starts to print (a beat with no reply: once its line is done),
|
|
28
|
+
* and a status-only step's `behold:ask-status` the moment its busy line shows.
|
|
29
|
+
* Nothing happens by magic: the graph holds still (`behold:ask-hold`, then
|
|
30
|
+
* `behold:ask-start`) until then. `behold:ask-done` still fires once the reply
|
|
31
|
+
* has printed. A step that arrives while the one before is still playing (a
|
|
32
|
+
* status-only step's busy line shows for at least {@link BUSY_MIN_MS}) waits in
|
|
33
|
+
* a queue for it to finish.
|
|
34
|
+
*
|
|
35
|
+
* Each new line (a typed prompt, a reply, a busy line, the ready light) is
|
|
36
|
+
* briefly underlined with a sweep ({@link EMPH_MS}; a still underline under
|
|
37
|
+
* reduced motion) and the lines before it in its turn dim a little, and
|
|
38
|
+
* `behold:ask-line` fires with `{ kind, step, scenario }` (the tip cards give
|
|
39
|
+
* way on it).
|
|
40
|
+
*
|
|
41
|
+
* The timeline, the typing and the template filling are pure functions, so
|
|
42
|
+
* they are tested without a page. {@link createAskPane} is the one part that
|
|
43
|
+
* touches the document, and it tells the rest of the page through window
|
|
44
|
+
* events (the shared interface with behold#937's lighting):
|
|
45
|
+
*
|
|
46
|
+
* - `behold:ask-hold` with `{ step, scenario }` when a step begins;
|
|
47
|
+
* - `behold:ask-start` before a beat is typed, `behold:ask-light` when its
|
|
48
|
+
* reply starts, `behold:ask-done` when its reply has printed, each with
|
|
49
|
+
* detail `{ ask, step, scenario }`;
|
|
50
|
+
* - `behold:ask-clear` on a step with no ask, as its busy line shows;
|
|
51
|
+
* - `behold:ask-status` with `{ status, step, scenario }` as a status line
|
|
52
|
+
* shows (a step's `ask.status`, or its label's first sentence);
|
|
53
|
+
* - `behold:ask-line` when a new line starts, `behold:ask-busy` when the busy
|
|
54
|
+
* line changes.
|
|
55
|
+
*
|
|
56
|
+
* `{dependents}` is not filled here: behold#937 sets
|
|
57
|
+
* `window.beholdAskTokens = { dependents: <n> }` when it hears ask-start, and
|
|
58
|
+
* the reply reads that object when it prints.
|
|
59
|
+
*/
|
|
60
|
+
|
|
61
|
+
/** Milliseconds per typed character of a prompt (a phone viewer has to be able to read it as it comes). */
|
|
62
|
+
export const TYPE_MS = 90;
|
|
63
|
+
/** The blinking cursor shows alone this long before a prompt starts to type. */
|
|
64
|
+
export const CURSOR_PAUSE_MS = 600;
|
|
65
|
+
/** Milliseconds per character of a reply, which types faster than the person. */
|
|
66
|
+
export const REPLY_TYPE_MS = 35;
|
|
67
|
+
/** The busy line shows at least this long before a reply types, and before a status-only step lets the graph go. */
|
|
68
|
+
export const BUSY_MIN_MS = 1200;
|
|
69
|
+
/** From a beat's end to the next beat's start. */
|
|
70
|
+
export const BEAT_GAP_MS = 1500;
|
|
71
|
+
/** Turns dimmed in steps above the newest one; older ones stay at the faintest. */
|
|
72
|
+
export const KEEP = 3;
|
|
73
|
+
/** Turns kept in the transcript. */
|
|
74
|
+
export const MAX_TURNS = 40;
|
|
75
|
+
/** Steps waiting for the one on screen to finish. */
|
|
76
|
+
export const MAX_QUEUE = 8;
|
|
77
|
+
/** The busy line's spinner glyphs, and how long each shows; the ellipsis steps every ELLIPSIS_MS. */
|
|
78
|
+
export const SPINNER = ["✻", "✶", "✳", "✢", "·"];
|
|
79
|
+
export const SPIN_MS = 140;
|
|
80
|
+
export const ELLIPSIS_MS = 400;
|
|
81
|
+
/** A reply's diff lines show one by one, this far apart, once the reply has finished typing. */
|
|
82
|
+
export const DIFF_MS = 120;
|
|
83
|
+
export const MARKS = { prompt: ">", action: "✓" };
|
|
84
|
+
/** How long a new line's underline sweep lasts. */
|
|
85
|
+
export const EMPH_MS = 900;
|
|
86
|
+
|
|
87
|
+
/** A demo's beats in order: `asks` when it has some, else `ask`, else none. A beat is a prompt, an action, a reply or a status (a status alone is valid). */
|
|
88
|
+
export function askBeats(demo) {
|
|
89
|
+
if (!demo) return [];
|
|
90
|
+
const ok = (a) => a && (a.prompt || a.action || a.reply || a.status);
|
|
91
|
+
if (Array.isArray(demo.asks) && demo.asks.length) return demo.asks.filter(ok);
|
|
92
|
+
return ok(demo.ask) ? [demo.ask] : [];
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** Whether a demo step carries a prompt-driven beat (`ask` or `asks`): the one thing that turns ask mode on for a view. */
|
|
96
|
+
export const hasAsk = (demo) => askBeats(demo).length > 0;
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* behold#939: ask mode, while the pane shows in Watch. The story is the
|
|
100
|
+
* prompts and the graph, so what contradicts or covers it is off: the approval
|
|
101
|
+
* card and modal, the result card ("3 waves applied" against "one apply"), the
|
|
102
|
+
* wave badges, the log drawer, Tips' bubbles and the "This cycle" strip. A step
|
|
103
|
+
* without an ask is not in it, and looks as it always did.
|
|
104
|
+
*/
|
|
105
|
+
export const askMode = (s) => !!(s && s.askOn && s.watchMode);
|
|
106
|
+
|
|
107
|
+
/** event-help's read of the page, quieted for ask mode: no bubble, no modal, no strip, no waiting line, no result to defer to. */
|
|
108
|
+
export function askEventHelp(h, on) {
|
|
109
|
+
return on && h ? { ...h, held: true, hideTrail: true, waiting: null, result: null } : h;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** A step's shot in ask mode: the whole graph (the resources of every root), asked to fill the room. */
|
|
113
|
+
export function askShot(shot, on) {
|
|
114
|
+
return on && shot ? { ...shot, kind: "whole", roots: [], waves: [], whole: true, ask: true } : shot;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* The pane's height only grows within a scenario: sized to its content (the
|
|
119
|
+
* last entries and the current one), capped by the stylesheet, and never
|
|
120
|
+
* shorter than the tallest it has been, so the graph's room does not breathe
|
|
121
|
+
* as lines come and go.
|
|
122
|
+
*/
|
|
123
|
+
export function paneHeight(peak, now) {
|
|
124
|
+
return Number.isFinite(now) && now > peak ? now : peak;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** Fill `{token}`s from `values`. A token that is unknown, or has no value, is the empty string. */
|
|
128
|
+
export function fillReply(template, values = {}) {
|
|
129
|
+
if (typeof template !== "string") return "";
|
|
130
|
+
const v = values || {};
|
|
131
|
+
return template.replace(/\{(\w+)\}/g, (_, name) => {
|
|
132
|
+
const x = Object.prototype.hasOwnProperty.call(v, name) ? v[name] : null;
|
|
133
|
+
return typeof x === "number" && Number.isFinite(x) ? String(x) : typeof x === "string" ? x : "";
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* The tokens a pull request's report gives (terragucci's `totals`,
|
|
139
|
+
* `totals_roots`), and the failed root's error. A count absent from a report
|
|
140
|
+
* that has totals is 0 (a report lists the kinds that are not zero).
|
|
141
|
+
*/
|
|
142
|
+
export function replyValues(pr, error = null, extra = {}) {
|
|
143
|
+
const t = pr && pr.totals && typeof pr.totals === "object" ? pr.totals : null;
|
|
144
|
+
const n = (k) => (t ? (Number.isInteger(t[k]) ? t[k] : 0) : null);
|
|
145
|
+
const out = {
|
|
146
|
+
roots: pr && Number.isInteger(pr.totals_roots) ? pr.totals_roots : null,
|
|
147
|
+
creates: n("create"),
|
|
148
|
+
updates: n("update"),
|
|
149
|
+
deletes: n("delete"),
|
|
150
|
+
error: typeof error === "string" && error ? error : null,
|
|
151
|
+
};
|
|
152
|
+
return { ...out, ...extra };
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** How many of `len` characters are typed `elapsedMs` after the line began. Reduced motion shows all at once. */
|
|
156
|
+
export function typedChars(len, elapsedMs, { reduced = false, msPerChar = TYPE_MS } = {}) {
|
|
157
|
+
if (reduced || msPerChar <= 0) return len;
|
|
158
|
+
return Math.max(0, Math.min(len, Math.floor(elapsedMs / msPerChar)));
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// A note (the opening, the closing) is a line with no mark, shown at once and announced by no event.
|
|
162
|
+
// A reply with neither a prompt nor an action has no line of its own: the reply prints alone.
|
|
163
|
+
const lineOf = (beat) =>
|
|
164
|
+
beat.note
|
|
165
|
+
? { mark: "", text: beat.note, typed: false }
|
|
166
|
+
: beat.prompt
|
|
167
|
+
? { mark: MARKS.prompt, text: beat.prompt, typed: true }
|
|
168
|
+
: beat.action
|
|
169
|
+
? { mark: MARKS.action, text: beat.action, typed: false }
|
|
170
|
+
: { mark: "", text: "", typed: false };
|
|
171
|
+
|
|
172
|
+
/** The beat's diff lines (strings only); empty when the beat has no reply to hang them under. */
|
|
173
|
+
export const diffLines = (beat) => (beat && beat.reply && Array.isArray(beat.diff) ? beat.diff.filter((x) => typeof x === "string" && x.trim() !== "") : []);
|
|
174
|
+
|
|
175
|
+
/** A diff line split into its sign column (`+`, `~`, `-`, else blank) and the rest. */
|
|
176
|
+
export function diffParts(line) {
|
|
177
|
+
const m = /^([+~-])\s?(.*)$/s.exec(line);
|
|
178
|
+
return m ? { sign: m[1], rest: m[2] } : { sign: "", rest: line };
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** The beat's reply, as the pane has it: the filled text once known, else the template (its length is a fair guess for timing). */
|
|
182
|
+
const replyOf = (beat, filled) => (typeof filled === "string" ? filled : typeof beat.reply === "string" ? beat.reply : "");
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* One beat's timing: the cursor's pause, how long its line takes to type, when
|
|
187
|
+
* its busy line starts (`busyAt`, as soon as the line is done), when its reply
|
|
188
|
+
* starts (after the busy line has shown for BUSY_MIN_MS, when the beat has a
|
|
189
|
+
* line and a reply), how long the reply takes to type, and when the beat is
|
|
190
|
+
* over. `reply` is the filled text when the pane has it.
|
|
191
|
+
*/
|
|
192
|
+
export function beatTiming(beat, { reduced = false, msPerChar = TYPE_MS, replyMs = REPLY_TYPE_MS, reply } = {}) {
|
|
193
|
+
const line = lineOf(beat);
|
|
194
|
+
const typed = line.typed && !reduced;
|
|
195
|
+
const cursor = typed ? CURSOR_PAUSE_MS : 0;
|
|
196
|
+
const typing = typed ? line.text.length * msPerChar : 0;
|
|
197
|
+
const busyAt = cursor + typing;
|
|
198
|
+
const busy = beat.reply && hasLine(beat) && !reduced ? BUSY_MIN_MS : 0;
|
|
199
|
+
const replyAt = busyAt + busy;
|
|
200
|
+
const replyType = beat.reply && !reduced ? replyOf(beat, reply).length * replyMs : 0;
|
|
201
|
+
const diffMs = reduced ? 0 : diffLines(beat).length * DIFF_MS;
|
|
202
|
+
return { cursor, typing, busyAt, replyAt, replyType, diffMs, end: replyAt + replyType + diffMs };
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/** Whether a beat has a prompt or an action line of its own (a note and a lone reply do not). */
|
|
206
|
+
const hasLine = (beat) => !beat.note && !!(beat.prompt || beat.action);
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Where a step's beats stand `elapsedMs` after the step began: for each beat
|
|
210
|
+
* started, its line as typed so far, and its state. `ready` is a prompt whose
|
|
211
|
+
* first character is not typed yet (the cursor's pause); `busy` is the busy
|
|
212
|
+
* line showing (`busySince` is when it began, in ms from the step's start);
|
|
213
|
+
* `sumSecs` is the length of a busy phase that has ended (a reply came, or the
|
|
214
|
+
* next beat began), else null; then the reply's progress, and `over`.
|
|
215
|
+
* `gap` is true while a later beat has not begun. `end` is when the last beat
|
|
216
|
+
* is over. Beats play in order with {@link BEAT_GAP_MS} between.
|
|
217
|
+
* `opts.replies[i]` is beat i's filled reply once the pane has it.
|
|
218
|
+
*/
|
|
219
|
+
export function playback(beats, elapsedMs, opts = {}) {
|
|
220
|
+
const timings = [];
|
|
221
|
+
const starts = [];
|
|
222
|
+
let at = 0;
|
|
223
|
+
beats.forEach((beat, i) => {
|
|
224
|
+
const t = beatTiming(beat, { ...opts, reply: opts.replies ? opts.replies[i] : undefined });
|
|
225
|
+
timings.push(t);
|
|
226
|
+
starts.push(at);
|
|
227
|
+
at += t.end + BEAT_GAP_MS;
|
|
228
|
+
});
|
|
229
|
+
const end = beats.length ? at - BEAT_GAP_MS : 0;
|
|
230
|
+
const frames = [];
|
|
231
|
+
let done = true;
|
|
232
|
+
for (let i = 0; i < beats.length; i++) {
|
|
233
|
+
const beat = beats[i];
|
|
234
|
+
const t = timings[i];
|
|
235
|
+
const local = elapsedMs - starts[i];
|
|
236
|
+
if (local < 0) {
|
|
237
|
+
done = false;
|
|
238
|
+
break;
|
|
239
|
+
}
|
|
240
|
+
const line = lineOf(beat);
|
|
241
|
+
const n = line.typed ? typedChars(line.text.length, local - t.cursor, opts) : line.text.length;
|
|
242
|
+
const lined = hasLine(beat);
|
|
243
|
+
const replied = !!beat.reply && local >= t.replyAt;
|
|
244
|
+
const full = replyOf(beat, opts.replies ? opts.replies[i] : undefined);
|
|
245
|
+
const replyChars = replied ? typedChars(full.length, local - t.replyAt, { reduced: opts.reduced, msPerChar: opts.replyMs ?? REPLY_TYPE_MS }) : 0;
|
|
246
|
+
const diffAll = diffLines(beat).length;
|
|
247
|
+
const diffAt = t.replyAt + t.replyType;
|
|
248
|
+
const diffShown = !replied || !diffAll || local < diffAt ? 0 : opts.reduced ? diffAll : Math.min(diffAll, Math.floor((local - diffAt) / DIFF_MS) + 1);
|
|
249
|
+
const nextStarted = i + 1 < beats.length && elapsedMs >= starts[i + 1];
|
|
250
|
+
const over = local >= t.end;
|
|
251
|
+
if (!over) done = false;
|
|
252
|
+
const busy = lined && local >= t.busyAt && !replied && !nextStarted;
|
|
253
|
+
let sumSecs = null;
|
|
254
|
+
if (lined && replied) sumSecs = Math.round((t.replyAt - t.busyAt) / 1000);
|
|
255
|
+
else if (lined && nextStarted) sumSecs = Math.round((starts[i + 1] - starts[i] - t.busyAt) / 1000);
|
|
256
|
+
frames.push({
|
|
257
|
+
index: i,
|
|
258
|
+
mark: line.mark,
|
|
259
|
+
text: line.text.slice(0, n),
|
|
260
|
+
typing: n < line.text.length,
|
|
261
|
+
ready: line.typed && n === 0,
|
|
262
|
+
lined,
|
|
263
|
+
busy,
|
|
264
|
+
busySince: starts[i] + t.busyAt,
|
|
265
|
+
// When this busy line is due to end, from the step's start: its reply, or the next beat; null when the step's end decides.
|
|
266
|
+
busyUntil: beat.reply && lined ? starts[i] + t.replyAt : i + 1 < beats.length ? starts[i + 1] : null,
|
|
267
|
+
sumSecs,
|
|
268
|
+
replied,
|
|
269
|
+
replyChars,
|
|
270
|
+
replyTyping: replied && replyChars < full.length,
|
|
271
|
+
diffShown,
|
|
272
|
+
over,
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
return { frames, done, gap: frames.length < beats.length, end };
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/** m:ss */
|
|
279
|
+
const clock = (secs) => `${Math.floor(secs / 60)}:${String(secs % 60).padStart(2, "0")}`;
|
|
280
|
+
|
|
281
|
+
/** A label's first sentence, without its closing full stop. */
|
|
282
|
+
export function firstSentence(label) {
|
|
283
|
+
if (typeof label !== "string") return "";
|
|
284
|
+
const t = label.trim();
|
|
285
|
+
const m = t.match(/^.*?[.!?](?=\s|$)/s);
|
|
286
|
+
return (m ? m[0] : t).replace(/\.$/, "").trim();
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* The text of the status line a step shows under the transcript: the last
|
|
291
|
+
* `status` its beats carry, else, when the step has no ask at all, the step's
|
|
292
|
+
* label cut to its first sentence. Null when the step is a prompt with nothing
|
|
293
|
+
* to wait for.
|
|
294
|
+
*/
|
|
295
|
+
export function statusText(demo) {
|
|
296
|
+
if (!demo) return null;
|
|
297
|
+
const beats = askBeats(demo);
|
|
298
|
+
for (let i = beats.length - 1; i >= 0; i--) if (typeof beats[i].status === "string" && beats[i].status.trim()) return beats[i].status.trim();
|
|
299
|
+
if (beats.length || demo.opening || demo.closing) return null;
|
|
300
|
+
return firstSentence(demo.label) || null;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/** The seconds since the step began: the driver's `started_at` when it has one, else `since` (ms). Never negative. */
|
|
304
|
+
export function elapsedSecs(demo, nowMs, since) {
|
|
305
|
+
const from = demo && typeof demo.started_at === "string" ? Date.parse(demo.started_at) : NaN;
|
|
306
|
+
return Math.max(0, Math.round((nowMs - (Number.isFinite(from) ? from : since)) / 1000));
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
export const elapsedClock = clock;
|
|
310
|
+
|
|
311
|
+
/** Whether a step is the first in which a scenario's opening line shows, and the one its closing shows in. */
|
|
312
|
+
export const OPENING_STEPS = ["pull-request", "planned"];
|
|
313
|
+
export const CLOSING_STEP = "applied";
|
|
314
|
+
|
|
315
|
+
|
|
316
|
+
/**
|
|
317
|
+
* The newest line of a view: `{ key, kind, turn, part }`. `turn` is its turn's index in `turns`, `part` the index of
|
|
318
|
+
* its part (-1: the turn's own prompt or action line), `kind` one of prompt, line, busy, sum, reply, ready. A diff
|
|
319
|
+
* belongs to the reply above it. The key changes only when a new line starts, not as one types.
|
|
320
|
+
*/
|
|
321
|
+
export function newestLine(turns, ready) {
|
|
322
|
+
const n = turns.length;
|
|
323
|
+
if (ready) return { key: `${n}|ready`, kind: "ready", turn: n, part: -1 };
|
|
324
|
+
if (!n) return null;
|
|
325
|
+
const last = turns[n - 1];
|
|
326
|
+
let i = last.parts.length - 1;
|
|
327
|
+
while (i >= 0 && last.parts[i].k === "diff") i--;
|
|
328
|
+
if (i >= 0) return { key: `${n}|${i}|${last.parts[i].k}`, kind: last.parts[i].k, turn: n - 1, part: i };
|
|
329
|
+
if (last.text === "" && !last.mark && !last.typing) return null;
|
|
330
|
+
return { key: `${n}|-1|${last.mark}`, kind: last.mark === MARKS.prompt ? "prompt" : "line", turn: n - 1, part: -1 };
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
const STYLE_ID = "k-ask-style";
|
|
334
|
+
/**
|
|
335
|
+
* The pane's own look: the ready light on its top edge, the cursor, the busy
|
|
336
|
+
* line and the dimmed turns. Layout (size, place) stays with kiosk.html.
|
|
337
|
+
*/
|
|
338
|
+
export const PANE_CSS = `
|
|
339
|
+
#k-ask { --ask-accent: var(--pending, #e0a030); --ask-agent: var(--ink2, var(--fg)); overflow-y: auto; overflow-x: hidden; justify-content: flex-start; scrollbar-width: none; border-top: 3px solid var(--rule); transition: border-color 0.3s ease, box-shadow 0.3s ease; }
|
|
340
|
+
#k-ask > :first-child { margin-top: auto; }
|
|
341
|
+
#k-ask::-webkit-scrollbar { display: none; }
|
|
342
|
+
#k-ask.ask-ready { border-top-color: var(--ask-accent); animation: ask-glow 1.8s ease-in-out infinite; }
|
|
343
|
+
#k-ask .ask-typing .ask-text::after, #k-ask .ask-reply-typing::after { content: "\\2588"; margin-left: 0.1em; }
|
|
344
|
+
#k-ask .ask-waiting .ask-text::after, #k-ask .ask-ready-line .ask-text::after { animation: ask-blink 1s steps(1) infinite; }
|
|
345
|
+
#k-ask .ask-ready-line .ask-text::after { content: "\\2588"; }
|
|
346
|
+
#k-ask .ask-ready-line .ask-mark { color: var(--ask-accent); }
|
|
347
|
+
#k-ask .ask-diff { margin: 0.25em 0 0.1em 1.6em; padding: 0.35em 0.7em; font: 500 0.8em/1.45 var(--font-mono, ui-monospace, monospace); background: var(--well, rgba(0,0,0,0.25)); border: 1px solid var(--rule); border-radius: 4px; min-width: 0; }
|
|
348
|
+
#k-ask .ask-diff-line { display: grid; grid-template-columns: 1.4ch minmax(0, 1fr); column-gap: 0.6ch; white-space: pre; }
|
|
349
|
+
#k-ask .ask-diff-sign { text-align: center; font-weight: 700; }
|
|
350
|
+
#k-ask .ask-diff-rest { overflow: hidden; text-overflow: ellipsis; white-space: pre; min-width: 0; }
|
|
351
|
+
#k-ask .ask-diff-add .ask-diff-sign, #k-ask .ask-diff-add .ask-diff-rest { color: var(--managed-ink, var(--managed)); }
|
|
352
|
+
#k-ask .ask-diff-chg .ask-diff-sign, #k-ask .ask-diff-chg .ask-diff-rest { color: var(--foreign-ink, var(--foreign)); }
|
|
353
|
+
#k-ask .ask-diff-del .ask-diff-sign, #k-ask .ask-diff-del .ask-diff-rest { color: var(--degraded-ink, var(--degraded)); }
|
|
354
|
+
#k-ask .ask-sum, #k-ask .ask-busy { display: flex; align-items: baseline; gap: 0.5em; padding-left: 0; font-size: 0.72em; font-weight: 500; }
|
|
355
|
+
#k-ask .ask-busy { font-size: 1em; padding-left: 1.6em; } /* the agent at work reads as large as its reply */
|
|
356
|
+
#k-ask .ask-sum { color: var(--muted); opacity: 0.8; }
|
|
357
|
+
#k-ask .ask-busy { color: var(--ask-agent); }
|
|
358
|
+
#k-ask .ask-spinner { flex: none; width: 1.2ch; text-align: center; }
|
|
359
|
+
#k-ask .ask-busy-text { min-width: 0; overflow-wrap: anywhere; }
|
|
360
|
+
#k-ask .ask-dots { flex: none; display: inline-block; min-width: 3ch; margin-left: -0.5em; }
|
|
361
|
+
#k-ask .ask-timer { flex: none; margin-left: auto; color: var(--muted); font-variant-numeric: tabular-nums; }
|
|
362
|
+
#k-ask .ask-new { background: linear-gradient(var(--ask-accent), var(--ask-accent)) no-repeat left bottom / 100% 2px; }
|
|
363
|
+
#k-ask .ask-cur > .ask-before { opacity: 0.78; }
|
|
364
|
+
@media (prefers-reduced-motion: no-preference) {
|
|
365
|
+
#k-ask .ask-new { animation: ask-sweep ${EMPH_MS}ms ease-out both; }
|
|
366
|
+
}
|
|
367
|
+
@keyframes ask-sweep { 0% { background-size: 0% 2px; } 55% { background-size: 100% 2px; } 100% { background-size: 100% 0px; } }
|
|
368
|
+
@keyframes ask-blink { 50% { opacity: 0; } }
|
|
369
|
+
@keyframes ask-glow { 0%, 100% { box-shadow: 0 -2px 10px color-mix(in srgb, var(--ask-accent) 25%, transparent); } 50% { box-shadow: 0 -4px 22px color-mix(in srgb, var(--ask-accent) 70%, transparent); } }
|
|
370
|
+
@media (prefers-reduced-motion: reduce) {
|
|
371
|
+
#k-ask { transition: none; }
|
|
372
|
+
#k-ask.ask-ready { animation: none; box-shadow: 0 -3px 14px color-mix(in srgb, var(--ask-accent) 45%, transparent) !important; }
|
|
373
|
+
#k-ask .ask-waiting .ask-text::after, #k-ask .ask-ready-line .ask-text::after { animation: none; }
|
|
374
|
+
}
|
|
375
|
+
`;
|
|
376
|
+
|
|
377
|
+
function injectStyle(doc) {
|
|
378
|
+
if (!doc || !doc.head || (doc.getElementById && doc.getElementById(STYLE_ID))) return;
|
|
379
|
+
const st = doc.createElement("style");
|
|
380
|
+
st.id = STYLE_ID;
|
|
381
|
+
st.textContent = PANE_CSS;
|
|
382
|
+
doc.head.appendChild(st);
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
/** A busy line's label: the status or the label, without its closing full stop or ellipsis. */
|
|
386
|
+
export const busyLabel = (text) => (typeof text === "string" ? text.replace(/(\.{1,3}|…)\s*$/, "").trim() : "") || "Working";
|
|
387
|
+
|
|
388
|
+
/** The spinner glyph and the ellipsis at time `ms`; reduced motion keeps them still. */
|
|
389
|
+
export const spinnerAt = (ms, reduced = false) => (reduced ? SPINNER[0] : SPINNER[Math.floor(ms / SPIN_MS) % SPINNER.length]);
|
|
390
|
+
export const ellipsisAt = (ms, reduced = false) => (reduced ? "…" : ".".repeat((Math.floor(ms / ELLIPSIS_MS) % 3) + 1));
|
|
391
|
+
|
|
392
|
+
/**
|
|
393
|
+
* The turns a transcript shows once a step's turns follow the earlier ones: a
|
|
394
|
+
* turn with no line of its own (`cont`: a lone reply, or a status-only step's
|
|
395
|
+
* busy line) continues the last turn.
|
|
396
|
+
*/
|
|
397
|
+
export function mergeTurns(done, more) {
|
|
398
|
+
const out = done.map((t) => ({ ...t, parts: [...t.parts] }));
|
|
399
|
+
for (const t of more) {
|
|
400
|
+
if (t.cont && out.length) out[out.length - 1].parts.push(...t.parts);
|
|
401
|
+
else out.push({ ...t, parts: [...t.parts] });
|
|
402
|
+
}
|
|
403
|
+
return out;
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
/** A turn whose busy line has ended becomes the dim summary line, `secs` long. */
|
|
407
|
+
export const settleTurns = (turns, nowMs) => turns.map((t) => ({ ...t, ready: false, typing: false, parts: t.parts.map((p) => (p.k === "busy" ? { k: "sum", label: p.label, secs: Math.max(0, Math.floor((nowMs - p.since) / 1000)) } : p)) }));
|
|
408
|
+
|
|
409
|
+
/**
|
|
410
|
+
* The pane. `host` is its element; `fill()` gives the reply tokens from the
|
|
411
|
+
* report as it stands; `now()` the time. `win` is where events go and
|
|
412
|
+
* `reduced()` says whether motion is reduced. Once a scenario's step has put
|
|
413
|
+
* something in the pane, the pane stays until a reset (no demo) or another
|
|
414
|
+
* scenario. Steps that arrive while one is still playing wait their turn.
|
|
415
|
+
* Returns:
|
|
416
|
+
* - `setStep(demo)`: take a narration; returns whether the pane shows;
|
|
417
|
+
* - `active()`: whether it shows now.
|
|
418
|
+
*/
|
|
419
|
+
export function createAskPane({ host, win = typeof window !== "undefined" ? window : null, now = () => new Date(), fill = () => ({}), reduced = () => false, onActive = () => {}, setTimer = setInterval, clearTimer = clearInterval }) {
|
|
420
|
+
const doc = host.ownerDocument;
|
|
421
|
+
let key = null;
|
|
422
|
+
let cycleKey = null;
|
|
423
|
+
let openedFor = null;
|
|
424
|
+
let demo = null;
|
|
425
|
+
let beats = [];
|
|
426
|
+
let wanted = null;
|
|
427
|
+
// The status that is a busy line of its own: one that no beat with a line or a reply carries (that beat's busy line shows it).
|
|
428
|
+
let tail = null;
|
|
429
|
+
let tailTips;
|
|
430
|
+
let busyId = null;
|
|
431
|
+
let startedAt = 0;
|
|
432
|
+
let started = -1;
|
|
433
|
+
let finished = -1;
|
|
434
|
+
let replies = [];
|
|
435
|
+
let transcript = [];
|
|
436
|
+
let queue = [];
|
|
437
|
+
let timer = null;
|
|
438
|
+
let shown = false;
|
|
439
|
+
let engaged = false;
|
|
440
|
+
let peak = 0;
|
|
441
|
+
let sig = "";
|
|
442
|
+
let live = [];
|
|
443
|
+
let released = false;
|
|
444
|
+
let lit = -1;
|
|
445
|
+
// The newest line and when it started (its sweep), and the node that carries the sweep now.
|
|
446
|
+
let emph = null;
|
|
447
|
+
let emphNode = null;
|
|
448
|
+
let lastView = { turns: [], ready: false, hostReady: false };
|
|
449
|
+
|
|
450
|
+
const emit = (type, detail) => {
|
|
451
|
+
if (win && typeof win.dispatchEvent === "function" && typeof CustomEvent === "function") win.dispatchEvent(new CustomEvent(type, { detail }));
|
|
452
|
+
};
|
|
453
|
+
/** `behold:ask-busy` whenever the busy line changes: `{ busy, id, tips, until, cycle, scenario, step }` (id null when none shows; `until`: when it is due to end, ms, or null). */
|
|
454
|
+
const setBusy = (id, part) => {
|
|
455
|
+
if (id === busyId) return;
|
|
456
|
+
busyId = id;
|
|
457
|
+
emit("behold:ask-busy", { busy: id !== null, id, tips: part && part.tips ? part.tips : null, until: part && Number.isFinite(part.until) ? part.until : null, cycle: demo ? demo.cycle : null, scenario: demo ? demo.scenario : null, step: demo ? demo.step : null });
|
|
458
|
+
};
|
|
459
|
+
const detailOf = (beat) => ({ ask: beat, step: demo.step, scenario: demo.scenario });
|
|
460
|
+
const el = (cls, text) => {
|
|
461
|
+
const n = doc.createElement("div");
|
|
462
|
+
n.className = cls;
|
|
463
|
+
if (text !== undefined) n.textContent = text;
|
|
464
|
+
return n;
|
|
465
|
+
};
|
|
466
|
+
const span = (cls, text) => {
|
|
467
|
+
const n = doc.createElement("span");
|
|
468
|
+
n.className = cls;
|
|
469
|
+
n.textContent = text;
|
|
470
|
+
return n;
|
|
471
|
+
};
|
|
472
|
+
const addClass = (n, c) => n.classList && n.classList.add && n.classList.add(c);
|
|
473
|
+
const lineNode = (mark, text) => {
|
|
474
|
+
const line = el("ask-line");
|
|
475
|
+
line.append(span("ask-mark", mark), span("ask-text", text));
|
|
476
|
+
return line;
|
|
477
|
+
};
|
|
478
|
+
const busyNode = (p, t) => {
|
|
479
|
+
const n = el("ask-busy");
|
|
480
|
+
const sp = span("ask-spinner", spinnerAt(t, reduced()));
|
|
481
|
+
const tx = span("ask-busy-text", p.label);
|
|
482
|
+
const dots = span("ask-dots", ellipsisAt(t, reduced()));
|
|
483
|
+
const tm = span("ask-timer", "");
|
|
484
|
+
// The glyph, the dots and the timer turn every tick; screen readers get the label once.
|
|
485
|
+
for (const x of [sp, dots, tm]) x.setAttribute && x.setAttribute("aria-hidden", "true");
|
|
486
|
+
n.append(sp, tx, dots, tm);
|
|
487
|
+
live.push({ sp, dots, tm, since: p.since });
|
|
488
|
+
return n;
|
|
489
|
+
};
|
|
490
|
+
const refresh = (t) => {
|
|
491
|
+
const r = reduced();
|
|
492
|
+
if (emphNode && emph && t - emph.at >= EMPH_MS) {
|
|
493
|
+
if (emphNode.classList && emphNode.classList.remove) emphNode.classList.remove("ask-new");
|
|
494
|
+
emphNode = null;
|
|
495
|
+
}
|
|
496
|
+
for (const l of live) {
|
|
497
|
+
l.sp.textContent = spinnerAt(t, r);
|
|
498
|
+
l.dots.textContent = ellipsisAt(t, r);
|
|
499
|
+
l.tm.textContent = `(${clock(Math.max(0, Math.floor((t - l.since) / 1000)))})`;
|
|
500
|
+
}
|
|
501
|
+
};
|
|
502
|
+
const diffNode = (lines) => {
|
|
503
|
+
const box = el("ask-diff");
|
|
504
|
+
for (const l of lines) {
|
|
505
|
+
const { sign, rest } = diffParts(l);
|
|
506
|
+
const row = el(`ask-diff-line ${sign === "+" ? "ask-diff-add" : sign === "~" ? "ask-diff-chg" : sign === "-" ? "ask-diff-del" : ""}`.trim());
|
|
507
|
+
const r = span("ask-diff-rest", rest);
|
|
508
|
+
r.title = rest;
|
|
509
|
+
row.append(span("ask-diff-sign", sign), r);
|
|
510
|
+
box.appendChild(row);
|
|
511
|
+
}
|
|
512
|
+
return box;
|
|
513
|
+
};
|
|
514
|
+
const turnNode = (tn, cls, t) => {
|
|
515
|
+
const e = el(`ask-entry ${cls}`);
|
|
516
|
+
if (tn.text !== "" || tn.mark) e.appendChild(lineNode(tn.mark, tn.text));
|
|
517
|
+
if (tn.typing) addClass(e, "ask-typing");
|
|
518
|
+
if (tn.ready) addClass(e, "ask-waiting");
|
|
519
|
+
for (const p of tn.parts) {
|
|
520
|
+
if (p.k === "busy") e.appendChild(busyNode(p, t));
|
|
521
|
+
else if (p.k === "sum") e.appendChild(el("ask-sum", `✓ ${p.label} (${clock(p.secs)})`));
|
|
522
|
+
else if (p.k === "diff") e.appendChild(diffNode(p.lines));
|
|
523
|
+
else e.appendChild(el(p.typing ? "ask-reply ask-reply-typing" : "ask-reply", p.text));
|
|
524
|
+
}
|
|
525
|
+
return e;
|
|
526
|
+
};
|
|
527
|
+
|
|
528
|
+
/** What the current step shows now: its turns, whether the pane waits for a prompt, and when it lets the graph go. */
|
|
529
|
+
function build(t) {
|
|
530
|
+
const reducedNow = reduced();
|
|
531
|
+
const elapsed = t - startedAt;
|
|
532
|
+
const pb = playback(beats, elapsed, { reduced: reducedNow, replies });
|
|
533
|
+
const label = (beat) => busyLabel((beat && beat.status) || wanted || firstSentence(demo && demo.label));
|
|
534
|
+
const turns = [];
|
|
535
|
+
for (const f of pb.frames) {
|
|
536
|
+
const beat = beats[f.index];
|
|
537
|
+
const tn = f.lined || beat.note ? { mark: f.mark, text: f.text, note: !!beat.note, typing: f.typing || f.ready, ready: f.ready, parts: [] } : { cont: true, mark: "", text: "", parts: [] };
|
|
538
|
+
if (f.busy) tn.parts.push({ k: "busy", label: label(beat), since: startedAt + f.busySince, until: f.busyUntil === null ? nextAt() : startedAt + f.busyUntil, tips: beat.tips, bid: `b${f.index}` });
|
|
539
|
+
else if (f.sumSecs !== null) tn.parts.push({ k: "sum", label: label(beat), secs: f.sumSecs });
|
|
540
|
+
if (f.replied) tn.parts.push({ k: "reply", text: (replies[f.index] || "").slice(0, f.replyChars), typing: f.replyTyping });
|
|
541
|
+
if (f.diffShown > 0) tn.parts.push({ k: "diff", lines: diffLines(beat).slice(0, f.diffShown) });
|
|
542
|
+
turns.push(tn);
|
|
543
|
+
}
|
|
544
|
+
const last = pb.frames[pb.frames.length - 1];
|
|
545
|
+
let busyAt = last && last.busy ? last.busySince : null;
|
|
546
|
+
// A status after the beats (or a step that is only a status) is a busy line under the turn it belongs to.
|
|
547
|
+
if (pb.done && tail && !(last && last.busy)) {
|
|
548
|
+
const since = beats.length ? startedAt + pb.end : Math.min(t, elapsedSince());
|
|
549
|
+
busyAt = since - startedAt;
|
|
550
|
+
const part = { k: "busy", label: busyLabel(tail), since, until: nextAt(), tips: tailTips, bid: "tail" };
|
|
551
|
+
if (turns.length) turns[turns.length - 1].parts.push(part);
|
|
552
|
+
else turns.push({ cont: true, mark: "", text: "", parts: [part] });
|
|
553
|
+
}
|
|
554
|
+
const busyNow = turns.some((x) => x.parts.some((p) => p.k === "busy"));
|
|
555
|
+
const closing = !!(demo && demo.closing && demo.step === CLOSING_STEP);
|
|
556
|
+
const typingNext = !!(last && last.ready);
|
|
557
|
+
// Ready: waiting for the next prompt, between beats or once this step has said all it will.
|
|
558
|
+
const ready = !typingNext && ((pb.gap && !busyNow) || (pb.done && !busyNow && !closing));
|
|
559
|
+
return { pb, turns, ready, hostReady: ready || typingNext, busyAt: busyAt === null ? (pb.done ? pb.end : null) : busyAt };
|
|
560
|
+
}
|
|
561
|
+
/** When the driver expects the next step (demo.next.at), in ms, or null. */
|
|
562
|
+
const nextAt = () => {
|
|
563
|
+
const at = demo && demo.next && typeof demo.next.at === "string" ? Date.parse(demo.next.at) : NaN;
|
|
564
|
+
return Number.isFinite(at) ? at : null;
|
|
565
|
+
};
|
|
566
|
+
const elapsedSince = () => {
|
|
567
|
+
const from = demo && typeof demo.started_at === "string" ? Date.parse(demo.started_at) : NaN;
|
|
568
|
+
return Number.isFinite(from) ? from : startedAt;
|
|
569
|
+
};
|
|
570
|
+
|
|
571
|
+
/** When, in ms from the step's start, a step lets the graph go (null: its beats' own events do). */
|
|
572
|
+
function releaseAt(b) {
|
|
573
|
+
const real = beats.some((x) => !x.note && (x.prompt || x.action || x.reply));
|
|
574
|
+
if (real && !tail) return null;
|
|
575
|
+
const from = b.pb.done ? Math.max(b.pb.end, b.busyAt === null ? 0 : b.busyAt) : null;
|
|
576
|
+
return from === null ? null : from + BUSY_MIN_MS;
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
function draw() {
|
|
580
|
+
const t = now().getTime();
|
|
581
|
+
let b = build(t);
|
|
582
|
+
let pb = b.pb;
|
|
583
|
+
let filled = false;
|
|
584
|
+
for (const f of pb.frames) {
|
|
585
|
+
const beat = beats[f.index];
|
|
586
|
+
if (f.index > started) {
|
|
587
|
+
started = f.index;
|
|
588
|
+
if (!beat.note) emit("behold:ask-start", detailOf(beat));
|
|
589
|
+
}
|
|
590
|
+
if (f.replied && replies[f.index] === undefined) {
|
|
591
|
+
replies[f.index] = fillReply(beat.reply, fill());
|
|
592
|
+
filled = true;
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
if (filled) {
|
|
596
|
+
b = build(t);
|
|
597
|
+
pb = b.pb;
|
|
598
|
+
}
|
|
599
|
+
// A status-only step (or one with no ask at all) waits for its busy line to have shown BUSY_MIN_MS before the next step plays.
|
|
600
|
+
const due = releaseAt(b);
|
|
601
|
+
const settled = pb.done && (due === null || t - startedAt >= due);
|
|
602
|
+
// behold#tips: the busy line showing now (a status-only step's, or a turn's wait), for the tip cards (web/kiosk-ask-tips.js).
|
|
603
|
+
const bp = b.turns.flatMap((x) => x.parts).find((p) => p.k === "busy");
|
|
604
|
+
setBusy(bp ? `${key}|${bp.bid}` : null, bp);
|
|
605
|
+
const view = { turns: mergeTurns(transcript, b.turns), ready: b.ready, hostReady: b.hostReady };
|
|
606
|
+
lastView = view;
|
|
607
|
+
const shownTurns = view.turns.slice(-MAX_TURNS);
|
|
608
|
+
const newest = newestLine(shownTurns, view.ready);
|
|
609
|
+
let fresh = false;
|
|
610
|
+
if (newest && (!emph || emph.key !== newest.key)) {
|
|
611
|
+
emph = { key: newest.key, kind: newest.kind, at: t };
|
|
612
|
+
fresh = true;
|
|
613
|
+
}
|
|
614
|
+
const next = JSON.stringify([view.turns.slice(-(MAX_TURNS)).map((x) => [x.mark, x.text, x.typing, x.ready, x.parts.map((p) => [p.k, p.label, p.secs, p.text, p.typing, p.lines ? p.lines.length : 0])]), view.ready, view.hostReady]);
|
|
615
|
+
if (next !== sig) {
|
|
616
|
+
// Rebuilt only when something changed, so the cursor keeps blinking.
|
|
617
|
+
sig = next;
|
|
618
|
+
live = [];
|
|
619
|
+
const rows = shownTurns.map((x, i, all) => turnNode(x, i === all.length - 1 ? "ask-cur" : `ask-old ask-old-${Math.min(KEEP, all.length - 1 - i)}`, t));
|
|
620
|
+
if (view.ready) {
|
|
621
|
+
const r = el("ask-entry ask-ready-line");
|
|
622
|
+
r.appendChild(lineNode(MARKS.prompt, ""));
|
|
623
|
+
rows.push(r);
|
|
624
|
+
}
|
|
625
|
+
// The newest line: underlined while it is new (the sweep picks up where it was), the lines before it in its turn a little dimmer.
|
|
626
|
+
emphNode = null;
|
|
627
|
+
if (newest && emph) {
|
|
628
|
+
let node = null;
|
|
629
|
+
if (newest.kind === "ready") node = rows[rows.length - 1];
|
|
630
|
+
else {
|
|
631
|
+
const entry = rows[newest.turn];
|
|
632
|
+
const kids = entry && entry.children ? [...entry.children] : [];
|
|
633
|
+
const tn = shownTurns[newest.turn];
|
|
634
|
+
const offset = tn.text !== "" || tn.mark ? 1 : 0;
|
|
635
|
+
node = kids[newest.part + offset] || null;
|
|
636
|
+
if (node) for (const k of kids.slice(0, kids.indexOf(node))) addClass(k, "ask-before");
|
|
637
|
+
}
|
|
638
|
+
if (node && t - emph.at < EMPH_MS) {
|
|
639
|
+
addClass(node, "ask-new");
|
|
640
|
+
if (node.style) node.style.animationDelay = `${-(t - emph.at)}ms`;
|
|
641
|
+
emphNode = node;
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
host.replaceChildren(...rows);
|
|
645
|
+
if (host.classList && host.classList.toggle) host.classList.toggle("ask-ready", view.hostReady);
|
|
646
|
+
// The newest line is always in view.
|
|
647
|
+
if ("scrollTop" in host) host.scrollTop = host.scrollHeight;
|
|
648
|
+
// Sized to its content, never shorter than the tallest this scenario has needed.
|
|
649
|
+
if (host.style) {
|
|
650
|
+
host.style.minHeight = "";
|
|
651
|
+
peak = paneHeight(peak, host.offsetHeight || 0);
|
|
652
|
+
if (peak > 0) host.style.minHeight = `${peak}px`;
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
// What the graph and the tips hear, now that the words are on screen.
|
|
656
|
+
if (fresh) emit("behold:ask-line", { kind: emph.kind, step: demo ? demo.step : null, scenario: demo ? demo.scenario : null });
|
|
657
|
+
for (const f of pb.frames) {
|
|
658
|
+
const beat = beats[f.index];
|
|
659
|
+
if (beat.note) continue;
|
|
660
|
+
// The graph lights as the reply starts to print (a beat with no reply: once its line is done).
|
|
661
|
+
if ((beat.reply ? f.replied : f.over) && f.index > lit) {
|
|
662
|
+
lit = f.index;
|
|
663
|
+
emit("behold:ask-light", detailOf(beat));
|
|
664
|
+
}
|
|
665
|
+
if (f.over && f.index > finished) {
|
|
666
|
+
finished = f.index;
|
|
667
|
+
emit("behold:ask-done", detailOf(beat));
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
// A step with only a status (or no ask at all) lights the graph as its busy line shows.
|
|
671
|
+
const statusShown = b.turns.some((x) => x.parts.some((p) => p.k === "busy" && p.bid === "tail"));
|
|
672
|
+
if (!released && due !== null && (statusShown || settled)) {
|
|
673
|
+
released = true;
|
|
674
|
+
if (tail) emit("behold:ask-status", { status: tail, step: demo.step, scenario: demo.scenario });
|
|
675
|
+
else if (!beats.some((x) => !x.note && (x.prompt || x.action || x.reply))) emit("behold:ask-clear", { step: demo.step, scenario: demo.scenario });
|
|
676
|
+
}
|
|
677
|
+
refresh(t);
|
|
678
|
+
if (settled && queue.length) {
|
|
679
|
+
begin(queue.shift());
|
|
680
|
+
return;
|
|
681
|
+
}
|
|
682
|
+
if (settled && !live.length && !emphNode && timer !== null) {
|
|
683
|
+
clearTimer(timer);
|
|
684
|
+
timer = null;
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
/** What the step shown joins the transcript, its busy line collapsed into a summary. */
|
|
689
|
+
function commit() {
|
|
690
|
+
if (!demo) return;
|
|
691
|
+
const t = now().getTime();
|
|
692
|
+
const b = build(t);
|
|
693
|
+
transcript = mergeTurns(transcript, settleTurns(b.turns, t)).slice(-MAX_TURNS);
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
function begin(d) {
|
|
697
|
+
if (demo) commit();
|
|
698
|
+
key = d ? `${d.cycle}|${d.scenario}|${d.step}` : null;
|
|
699
|
+
const sc = d ? `${d.cycle}|${d.scenario}` : null;
|
|
700
|
+
if (sc !== cycleKey) {
|
|
701
|
+
// A reset (no demo) or another scenario clears the pane.
|
|
702
|
+
cycleKey = sc;
|
|
703
|
+
transcript = [];
|
|
704
|
+
queue = [];
|
|
705
|
+
peak = 0;
|
|
706
|
+
engaged = false;
|
|
707
|
+
}
|
|
708
|
+
demo = d;
|
|
709
|
+
const asked = askBeats(d);
|
|
710
|
+
// A status alone has no line to play: it is the status line.
|
|
711
|
+
beats = asked.filter((a) => a.prompt || a.action || a.reply);
|
|
712
|
+
// The opening shows once, at the scenario's first step; the closing after the last step's own beats.
|
|
713
|
+
if (d && d.opening && OPENING_STEPS.includes(d.step) && openedFor !== sc) {
|
|
714
|
+
openedFor = sc;
|
|
715
|
+
beats.unshift({ note: d.opening });
|
|
716
|
+
}
|
|
717
|
+
if (d && d.closing && d.step === CLOSING_STEP) beats.push({ note: d.closing });
|
|
718
|
+
wanted = statusText(d);
|
|
719
|
+
const lastStatus = asked.filter((a) => typeof a.status === "string" && a.status.trim()).pop();
|
|
720
|
+
const withTips = asked.filter((a) => a.tips && a.tips.length).pop();
|
|
721
|
+
tailTips = (lastStatus && lastStatus.tips) || (withTips && withTips.tips) || undefined;
|
|
722
|
+
tail = wanted && !(lastStatus && (lastStatus.prompt || lastStatus.action || lastStatus.reply)) ? wanted : null;
|
|
723
|
+
started = -1;
|
|
724
|
+
finished = -1;
|
|
725
|
+
lit = -1;
|
|
726
|
+
replies = [];
|
|
727
|
+
sig = "";
|
|
728
|
+
live = [];
|
|
729
|
+
released = false;
|
|
730
|
+
startedAt = now().getTime();
|
|
731
|
+
// The graph holds still from the first moment of a step until the turn that asks for a change has replied.
|
|
732
|
+
if (d) emit("behold:ask-hold", { step: d.step, scenario: d.scenario });
|
|
733
|
+
if (beats.length || asked.length) engaged = true;
|
|
734
|
+
const on = !!d && engaged;
|
|
735
|
+
shown = on;
|
|
736
|
+
if (timer !== null) clearTimer(timer);
|
|
737
|
+
timer = null;
|
|
738
|
+
host.hidden = !on;
|
|
739
|
+
if (on) {
|
|
740
|
+
injectStyle(doc);
|
|
741
|
+
draw();
|
|
742
|
+
if (timer === null && demo === d) timer = setTimer(draw, 40);
|
|
743
|
+
} else {
|
|
744
|
+
setBusy(null, null);
|
|
745
|
+
host.replaceChildren();
|
|
746
|
+
if (host.classList && host.classList.remove) host.classList.remove("ask-ready");
|
|
747
|
+
if (host.style) host.style.minHeight = "";
|
|
748
|
+
}
|
|
749
|
+
onActive(on);
|
|
750
|
+
}
|
|
751
|
+
|
|
752
|
+
function setStep(next) {
|
|
753
|
+
const d = next && next.demo !== undefined ? next.demo : next;
|
|
754
|
+
const nextKey = d ? `${d.cycle}|${d.scenario}|${d.step}` : null;
|
|
755
|
+
const lastKey = queue.length ? `${queue[queue.length - 1].cycle}|${queue[queue.length - 1].scenario}|${queue[queue.length - 1].step}` : key;
|
|
756
|
+
if (nextKey === lastKey) return shown;
|
|
757
|
+
const sc = d ? `${d.cycle}|${d.scenario}` : null;
|
|
758
|
+
if (sc === cycleKey && demo && shown) {
|
|
759
|
+
// Same scenario: the step on screen plays out first.
|
|
760
|
+
const b = build(now().getTime());
|
|
761
|
+
const due = releaseAt(b);
|
|
762
|
+
const settled = b.pb.done && (due === null || now().getTime() - startedAt >= due);
|
|
763
|
+
if (!settled || queue.length) {
|
|
764
|
+
queue = [...queue, d].slice(-MAX_QUEUE);
|
|
765
|
+
return shown;
|
|
766
|
+
}
|
|
767
|
+
}
|
|
768
|
+
queue = [];
|
|
769
|
+
begin(d);
|
|
770
|
+
return shown;
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
return { setStep, active: () => shown, draw, view: () => lastView };
|
|
774
|
+
}
|