@marver-design/marver 0.25.1 → 0.26.1

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 (95) hide show
  1. package/CHANGELOG.md +116 -0
  2. package/README.md +5 -3
  3. package/dist/{anchor-DVV732zR.mjs → anchor-5H1vnoLu.mjs} +6 -1
  4. package/dist/{anchor-check-DGZnYviN.mjs → anchor-check-l-ny3UzH.mjs} +1 -1
  5. package/dist/audio-Dw1lfMdb.mjs +443 -0
  6. package/dist/{auth-DdUKeVKt.mjs → auth-Dix8wrXV.mjs} +1 -1
  7. package/dist/{bake-BpxMZfij.mjs → bake-C-awKgLA.mjs} +1 -1
  8. package/dist/{boards-Csxj2WJn.mjs → boards-DYG22tOe.mjs} +2 -2
  9. package/dist/{build-DKaGhoVN.mjs → build-BDRiO_mP.mjs} +16 -9
  10. package/dist/capture-vadKjuCo.mjs +730 -0
  11. package/dist/cli.mjs +24 -18
  12. package/dist/{collab-gqNdScUE.mjs → collab-Dxg2trOK.mjs} +4 -3
  13. package/dist/{comments-Zoch254k.mjs → comments-0BSbb04K.mjs} +14 -3
  14. package/dist/{comments-pTLlhjh4.mjs → comments-BqXua-XL.mjs} +7 -7
  15. package/dist/{config-cKgZBRj0.mjs → config-Cf7EjAMf.mjs} +12 -1
  16. package/dist/{context-D2ugHqwO.mjs → context-HQ4Giw58.mjs} +1 -1
  17. package/dist/daemon-DL6Fwq6D.mjs +2569 -0
  18. package/dist/{dev-Diqx1Rt8.mjs → dev-Bw_qYWWq.mjs} +202 -18
  19. package/dist/dev-lock-DWSpa6nj.mjs +179 -0
  20. package/dist/{events-DnK_AnrW.mjs → events-CLH4ijKP.mjs} +14 -4
  21. package/dist/{init-DO4DhUt_.mjs → init-D8DtrK_F.mjs} +2 -2
  22. package/dist/{ledger-BNXW14Xw.mjs → ledger-D54iROST.mjs} +17 -1
  23. package/dist/{link-C-qUIuKU.mjs → link-BNzLvdhI.mjs} +2 -2
  24. package/dist/live-Cgzsicwi.mjs +4 -0
  25. package/dist/{manifest-DnAte_fB.mjs → manifest-Nl3KSVOk.mjs} +1 -1
  26. package/dist/{marks-koRsbHfe.mjs → marks-CKgzucHZ.mjs} +2 -2
  27. package/dist/{marks-DFuintmJ.mjs → marks-DyWMv_Z1.mjs} +23 -5
  28. package/dist/{marver-id-gate-DGANtVyC.mjs → marver-id-gate-BjF6v4bT.mjs} +4 -4
  29. package/dist/perf-C75MZ73h.mjs +167 -0
  30. package/dist/{perf-CCisg-ny.mjs → perf-DPbLGhhO.mjs} +1 -1
  31. package/dist/perf-jam-BU3TVrZu.mjs +239 -0
  32. package/dist/{plugin-dTF5reck.mjs → plugin-DkGmZs_r.mjs} +298 -38
  33. package/dist/{poster-D-zjYpgk.mjs → poster-BdtSKMDm.mjs} +2 -1
  34. package/dist/{profile-Do0nGkXO.mjs → profile-WU9qcbIo.mjs} +1 -1
  35. package/dist/{publish-bakes-C_9VZ-Mq.mjs → publish-bakes-9RzRToXV.mjs} +3 -3
  36. package/dist/queue-29ok5_eZ.mjs +1319 -0
  37. package/dist/reserve-BURNQ2Y3.mjs +53 -0
  38. package/dist/{rolldown-runtime-D7D4PA-g.mjs → rolldown-runtime-8H4AJuhK.mjs} +1 -0
  39. package/dist/{serve-wv2QWdxB.mjs → serve-C8rVheYk.mjs} +12 -12
  40. package/dist/session-BAaJsK5c.mjs +191 -0
  41. package/dist/{share-ScamJcks.mjs → share--oFJ1bCI.mjs} +2 -2
  42. package/dist/{shot-CUlmkqFX.mjs → shot-DQeR_Pfn.mjs} +339 -20
  43. package/dist/shot-XQV7HC9A.mjs +139 -0
  44. package/dist/speech-models-DomiLQcY.mjs +299 -0
  45. package/dist/speech-worker.mjs +354 -0
  46. package/dist/{summary-CrW88e3q.mjs → summary-Y8MY1Wuc.mjs} +2 -2
  47. package/dist/{sync-C3035OnJ.mjs → sync-DKk-yT1V.mjs} +9 -8
  48. package/dist/{work-GE88wb_3.mjs → work-F5u-twpJ.mjs} +28 -5
  49. package/dist/{work-CpJn1gwY.mjs → work-VvX_JIgl.mjs} +2 -2
  50. package/docs/jam-session.md +194 -0
  51. package/docs/live-jam.md +29 -3
  52. package/package.json +7 -1
  53. package/src/client/frame-host/bridge.js +20 -2
  54. package/src/client/frame-host/inspect.d.ts +4 -0
  55. package/src/client/frame-host/inspect.js +96 -6
  56. package/src/client/frame-host/keys.d.ts +6 -0
  57. package/src/client/frame-host/keys.js +59 -0
  58. package/src/client/frame-host/trail.d.ts +7 -0
  59. package/src/client/frame-host/trail.js +204 -0
  60. package/src/client/shell/App.tsx +52 -8
  61. package/src/client/shell/Comments.tsx +20 -6
  62. package/src/client/shell/JamSurface.tsx +346 -0
  63. package/src/client/shell/Play.tsx +32 -9
  64. package/src/client/shell/Tip.tsx +8 -3
  65. package/src/client/shell/Toolbar.tsx +5 -4
  66. package/src/client/shell/canvas/Canvas.tsx +12 -2
  67. package/src/client/shell/canvas/FrameNode.tsx +14 -4
  68. package/src/client/shell/comments-store.ts +23 -16
  69. package/src/client/shell/icons.tsx +17 -0
  70. package/src/client/shell/jam-capture.ts +1005 -0
  71. package/src/client/shell/jam-keys.ts +358 -0
  72. package/src/client/shell/jam-lane.tsx +249 -0
  73. package/src/client/shell/jam-lines.ts +115 -0
  74. package/src/client/shell/jam-panel.tsx +556 -0
  75. package/src/client/shell/jam-queue.ts +126 -0
  76. package/src/client/shell/jam-session.tsx +389 -0
  77. package/src/client/shell/jam-state.ts +103 -0
  78. package/src/client/shell/jam-surface-parts.tsx +106 -0
  79. package/src/client/shell/jam-ui.tsx +104 -0
  80. package/src/client/shell/jam-voice.ts +479 -0
  81. package/src/client/shell/sound.ts +22 -0
  82. package/src/client/shell/stage-size.ts +11 -0
  83. package/src/client/shell/store.ts +47 -17
  84. package/src/client/shell/styles.css +564 -2
  85. package/src/client/stage/main.tsx +18 -2
  86. package/src/shared/capture-frames.ts +41 -0
  87. package/src/shared/events.ts +19 -4
  88. package/src/shared/speech.ts +16 -0
  89. package/templates/AGENTS-embedded.md +10 -2
  90. package/templates/AGENTS-studio.md +10 -2
  91. package/templates/instructions/iterate.md +8 -6
  92. package/templates/instructions/jam.md +131 -59
  93. package/dist/daemon-Cl8KXGW4.mjs +0 -1523
  94. package/dist/perf-QJIWtTw1.mjs +0 -68
  95. package/dist/shot-jdO_B6Qw.mjs +0 -86
@@ -0,0 +1,1005 @@
1
+ /**
2
+ * Captures in the shell (spec 27, Part 3): `S` starts one, `E` ends it - everything you say, write
3
+ * and do while it is open is one timeline, on one clock.
4
+ *
5
+ * The frames record what happens inside them (frame-host/trail.js) and post it here; this module
6
+ * owns everything else: the capture ids, the load-bound handshake per mounted instance (a frame can
7
+ * show twice; a document replaced mid-capture never binds), canvas-mode clicks and hovers (the
8
+ * shell's overlay keeps the click and asks the frame what is under it), the UI steps the shell
9
+ * itself takes (select, device, theme, copy, interactive mode), notes, pictures - and the end: the
10
+ * flush, the drain, the seal, and the post of the sealed record.
11
+ *
12
+ * A capture is recording, then draining, then sealed. One records at a time: `S` while one records
13
+ * starts nothing - it is ended (E) or dropped (Esc) first. Each message lands in the capture whose
14
+ * id it carries.
15
+ */
16
+ import { create } from 'zustand'
17
+ import { ROUTE, slideSize } from '../const.ts'
18
+ import { CONFIG, SLIDE_INTRINSIC, useStore, type Node } from './store.ts'
19
+ import { UNPLACED } from './hash.ts'
20
+ import type { Bundle, CaptureRecord, Step, View } from '../../server/jam/types.ts'
21
+ import { forgetHeard, RATE, setWordsListener, voiceBegin, voiceDrop, voiceEnd } from './jam-voice.ts'
22
+ import { touchedFrames } from '../../shared/capture-frames.ts'
23
+ import { onStageSize, stageSize } from './stage-size.ts'
24
+
25
+ const csrf = () => /(?:^|;\s*)mv_c=([\w-]+)/.exec(document.cookie)?.[1] ?? ''
26
+ const now = () => performance.timeOrigin + performance.now()
27
+ const rand = (n = 6) => Math.random().toString(36).slice(2, 2 + n).padEnd(n, '0')
28
+ /** One recorder answers a flush within this; one that does not is noted in the record. */
29
+ const FLUSH_MS = 500
30
+ const PROBE_MS = 500
31
+ /** A picture is a render on the server: two at once in a cold browser on a busy Mac (a model downloading,
32
+ * words being transcribed) took 5-6 s in a trial, so the page waits long enough not to throw a good one away. */
33
+ const PICTURE_MS = 20_000
34
+ const MAX_PICTURES = 12
35
+
36
+ // ---- mounted instances: one per canvas node, and the play stage ------------------------------------
37
+
38
+ interface Instance { key: string; iframe: HTMLIFrameElement; nonce?: string; doc?: Document | null; captureDoc?: string }
39
+ const instances = new Map<string, Instance>()
40
+ const post = (inst: Instance, msg: unknown) => { try { inst.iframe.contentWindow?.postMessage(msg, location.origin) } catch { /* gone */ } }
41
+ /** Is this message from the document the instance bound to? Checked on EVERY message: a document
42
+ * replaced since - even before its `load` fired - is dropped. */
43
+ const current = (inst: Instance) => !!inst.doc && inst.iframe.contentDocument === inst.doc
44
+
45
+ /** The instance's iframe fired `load` - a new document: nothing from it is accepted until it answers
46
+ * a hello carrying the nonce sent now, while that exact Document is still current. */
47
+ export function frameLoaded(key: string, iframe: HTMLIFrameElement | null) {
48
+ if (!iframe) return
49
+ const inst: Instance = { key, iframe, nonce: `n-${rand(10)}`, doc: iframe.contentDocument }
50
+ instances.set(key, inst)
51
+ post(inst, { type: 'sh:capture-hello', nonce: inst.nonce })
52
+ }
53
+ export function frameGone(key: string) { instances.delete(key) }
54
+
55
+ /** A frame (or the stage) became ready: tell it whether a capture records, and the keys owned now. */
56
+ export function frameReady(key: string) {
57
+ const inst = instances.get(key)
58
+ if (!inst) return
59
+ // a recorder installed after `load` (the stage mounts late) missed the hello: ask again, while the
60
+ // Document the hello was bound to is still the one in the iframe
61
+ if (inst.nonce && current(inst)) post(inst, { type: 'sh:capture-hello', nonce: inst.nonce })
62
+ const cur = useJamCapture.getState().current
63
+ if (cur) post(inst, { type: 'sh:capture', id: cur, on: true })
64
+ post(inst, { type: 'sh:keys', owned: ownedNow })
65
+ }
66
+
67
+ // ---- owned keys, mirrored into every frame ------------------------------------------------------------
68
+
69
+ let ownedNow: string[] = []
70
+ /** The keys the shell owns right now (`[Ctrl+][Meta+][Alt+][Shift+]key`), posted to every instance on
71
+ * change: a frame consumes them itself (before a focused button sees ⏎) and forwards them. */
72
+ export function setOwnedKeys(keys: string[]) {
73
+ const next = [...new Set(keys)].sort()
74
+ if (next.join() === ownedNow.join()) return
75
+ ownedNow = next
76
+ for (const inst of instances.values()) post(inst, { type: 'sh:keys', owned: ownedNow })
77
+ }
78
+
79
+ // ---- the captures -------------------------------------------------------------------------------------
80
+
81
+ export interface Point { n: number; key: string; x: number; y: number } // a numbered point, in frame-local px
82
+ interface Cap {
83
+ id: string
84
+ state: 'recording' | 'draining'
85
+ board: string
86
+ t0: number
87
+ steps: (Step & { seq?: number; src?: string })[]
88
+ seen: Set<string> // `${instance}|${captureDoc}|${seq}` - dedupe
89
+ points: Point[]
90
+ pictures: string[]
91
+ probes: number // probe answers still out
92
+ shots: number // picture requests still out
93
+ /** its timing (spec 27a Part D): when E was pressed, the wait for its last words, each picture's time, the missing ones */
94
+ timing?: { endAt?: number; wordsMs?: number; pics: number[]; late: number; failed: number }
95
+ shotFrames: string[] // frames pictured (or being pictured) in it: an automatic picture is taken once per frame
96
+ endShot?: boolean // its end pictures were asked for (once its steps were all in)
97
+ inherited?: string[] // Resume: the frames the capture it continues was about (its card's)
98
+ endSizes?: Record<string, { w: number; h: number; play?: true }> // the sizes as they were at E (what it was about, then)
99
+ endNodes?: { key: string; frame: string; w: number; h: number; visible: boolean }[] // every instance at E (a step answering late is sized by these)
100
+ sizes: Record<string, { w: number; h: number; play?: true }> // each frame's size when it was last touched (`play`: on the stage's device)
101
+ keys: Record<string, string> // each frame's instance last touched (a frame can sit on a board twice)
102
+ sizedAt: Record<string, number> // when the touch that sized each frame happened (a late answer of an older one is ignored)
103
+ hearing: boolean // its words are still coming (the microphone's `final` not in)
104
+ flushWait: Map<string, string> // instance -> the captureDoc whose flush we wait for
105
+ reviewing?: { card: string; batch: string; rev: number }
106
+ /** Resume: the draft this capture continues, and when recording resumed */
107
+ resumes?: { id: string; at: number }
108
+ /** the Jam session it was made in (Part 5) */
109
+ session?: string
110
+ }
111
+
112
+ interface CaptureState {
113
+ /** the capture recording now */
114
+ current: string | null
115
+ caps: Record<string, Cap>
116
+ /** the notes card: open, and the text not yet added (kept until the capture ends) */
117
+ noteOpen: boolean
118
+ noteDraft: string
119
+ /** a number that changes on every capture change - for components to redraw (timer, counts) */
120
+ tick: number
121
+ /** Start one - never while one records (it says so instead) */
122
+ /** `session`: the session it belongs to, when the caller knows it before the queue shows it (Start) */
123
+ start(reviewing?: Cap['reviewing'], session?: string): void
124
+ end(): void
125
+ drop(): void
126
+ note(text: string): void
127
+ setNoteOpen(open: boolean): void
128
+ setNoteDraft(text: string): void
129
+ picture(frame?: string): void
130
+ /** the drafts sealed, in order - their read-backs open when capturing stops */
131
+ sealed: { id: string; card?: { id: string; rev: number } }[]
132
+ /** sealed captures the server does not hold yet (kept, sent again until it takes them) */
133
+ unsent: string[]
134
+ /** Continue a draft that ended too soon: its record is loaded and recording goes on from where it
135
+ * stopped, under a new id that takes the draft's place when it seals. Dropping it leaves the draft. */
136
+ resume(draft: string, ready?: () => Promise<boolean>): Promise<boolean>
137
+ /** drafts being continued right now (recording, sealing or waiting to send) - the lane hides them */
138
+ resuming: string[]
139
+ /** the draft whose Resume is settling (its edits saving, its record loading): its read-back's words are
140
+ * locked meanwhile, so nothing typed can race the start - set by the caller (resumeDraft) before its
141
+ * first wait, cleared when the Resume is over */
142
+ settling: string | null
143
+ /** the capture ended last (E): its read-back is the one that opens, whatever seals first */
144
+ lastEnded: string | null
145
+ }
146
+
147
+ const mutate = (fn: (s: CaptureState) => void) => useJamCapture.setState((s) => { fn(s); return { caps: { ...s.caps }, tick: s.tick + 1 } })
148
+ const capOf = (id: string): Cap | undefined => useJamCapture.getState().caps[id]
149
+
150
+ /** Add a step to a capture (in place; the timeline is sorted at the seal, by `at` then `seq`). */
151
+ function addStep(id: string, step: Step & { seq?: number; src?: string }, keys?: string[], auto = false) {
152
+ mutate((s) => { const c = s.caps[id]; if (c) { c.steps.push(step); sample(c, step, keys, auto) } })
153
+ }
154
+ /** The stage laid out again (a resize, a device, Fill, a document's width): the frame on it, while a capture records,
155
+ * is measured as it is now - the last measurement before it leaves the stage is the one kept. */
156
+ if (typeof window !== 'undefined') onStageSize(() => {
157
+ const id = useJamCapture.getState().current
158
+ const st = useStore.getState()
159
+ if (!id || !st.play || st.play.at !== stageSize.at) return
160
+ mutate((s) => { const c = s.caps[id]; if (c && c.state === 'recording') c.sizes[stageSize.at] = { w: stageSize.w, h: stageSize.h, play: true } })
161
+ })
162
+ const sampleSize = sample // (the watcher has a `sample` of its own)
163
+ /** Leaving play selects the frame it ended on: that selection is the shell's, not a touch (until then) */
164
+ let playLeftAt = 0
165
+ /** A frame's size on screen now: in play the stage's device, on the canvas its node (the one in view). */
166
+ function sizeNow(f: string, key?: string): { w: number; h: number; play?: true } | undefined {
167
+ const st = useStore.getState()
168
+ if (st.play) {
169
+ if (st.play.at !== f) return undefined
170
+ // the stage as play last laid it out (a document's reading width, a slide's stage, the window when it fills);
171
+ // before play has laid this screen out, the same rules play follows: a slide's own stage, the window, the preset
172
+ if (stageSize.at === f && stageSize.w > 0 && stageSize.h > 0) return { w: stageSize.w, h: stageSize.h, play: true }
173
+ const entry = st.manifest?.frames.find((x) => x.id === f)
174
+ const slide = (entry && slideSize(entry, CONFIG.viewports)) ?? (st.play.slides ? SLIDE_INTRINSIC : null)
175
+ if (slide) return { w: Math.round(slide.width), h: Math.round(slide.height), play: true }
176
+ if (st.play.device === 'fill') return { w: window.innerWidth, h: window.innerHeight, play: true }
177
+ const v = CONFIG.viewports[st.play.device]
178
+ return v ? { w: Math.round(v.width), h: Math.round(v.height), play: true } : undefined
179
+ }
180
+ // the instance touched first; else the one in view; else any
181
+ const n = (key ? st.nodes.find((x) => x.key === key && x.frame === f) : undefined) ?? visibleNodes().find((x) => x.frame === f) ?? st.nodes.find((x) => x.frame === f)
182
+ return n && n.w > 0 && n.h > 0 ? { w: Math.round(n.w), h: Math.round(n.h) } : undefined
183
+ }
184
+ /** A frame as it was at E (a capture finishing): its touched instance, else the one then in view, else any. */
185
+ function sizeAtEnd(c: Cap, f: string, key?: string): { w: number; h: number; play?: true } | undefined {
186
+ const ns = (c.endNodes ?? []).filter((n) => n.frame === f)
187
+ // a touch of a canvas instance (it names one): that instance as it was at E - over a size seen on the stage
188
+ const touched = key ? ns.find((x) => x.key === key) : undefined
189
+ if (touched && touched.w > 0 && touched.h > 0) return { w: touched.w, h: touched.h }
190
+ if (c.sizes[f]?.play) return c.sizes[f]
191
+ const n = ns.find((x) => x.key === c.keys[f]) ?? ns.find((x) => x.visible) ?? ns[0]
192
+ return n && n.w > 0 && n.h > 0 ? { w: n.w, h: n.h } : undefined
193
+ }
194
+ /** Each frame a step touches, at the size it is as it is touched (a frame visited on a phone in play keeps the
195
+ * phone's size, wherever the capture ends). */
196
+ function sample(c: Cap, step: { at?: number; event: string; frame?: string; to?: string; frames?: string[]; src?: string; view?: { frames?: string[] } }, keys?: string[], auto = false) {
197
+ const nodes = useStore.getState().nodes
198
+ // (a view of one frame - the stage as play opens, a frame zoomed to - is a frame it is about)
199
+ const viewed = step.event === 'view' && step.view?.frames?.length === 1 ? step.view.frames : []
200
+ for (const f of [step.frame, step.to, ...(step.event === 'select' ? step.frames ?? [] : []), ...viewed]) {
201
+ if (!f) continue
202
+ // a touch older than the one that sized this frame (its answer came late) changes nothing
203
+ if (step.at !== undefined && (c.sizedAt[f] ?? -Infinity) > step.at) continue
204
+ // the instance it came from (a step's own frame document), or the selected one showing this frame
205
+ const k = [step.src, ...(keys ?? [])].find((x) => x && nodes.some((n) => n.key === x && n.frame === f))
206
+ if (k) c.keys[f] = k
207
+ const z = c.state === 'draining' && c.endNodes ? sizeAtEnd(c, f, k) : sizeNow(f, c.keys[f])
208
+ // a view (play left, a fit) is not a touch: what was seen on the stage keeps the stage's size until the frame is
209
+ // used on the canvas
210
+ if (z && !((auto || step.event === 'view') && c.sizes[f]?.play && !z.play)) {
211
+ c.sizes[f] = z
212
+ if (step.at !== undefined) c.sizedAt[f] = step.at
213
+ // a step answering after E (a hover in flight) was a touch at E: the size kept for E follows the instance it
214
+ // touched - for the seal and the end pictures alike
215
+ if (c.state === 'draining' && c.endSizes && !auto && step.event !== 'view') c.endSizes[f] = z
216
+ }
217
+ }
218
+ }
219
+
220
+ const frameIdOf = (key: string): string | undefined => useStore.getState().nodes.find((n) => n.key === key)?.frame
221
+ /** The view: in play the stage; on the canvas the frames on screen - or, at the start only, the frames
222
+ * selected (a selection is the capture's focus, not what it keeps seeing). */
223
+ const viewNow = (focus = false): View => {
224
+ const s = useStore.getState()
225
+ // in play or slides the surface is the stage: its screen, its device, its theme
226
+ if (s.play) {
227
+ const vp = CONFIG.viewports[s.play.device]
228
+ return { board: s.board, frames: [s.play.at], theme: s.play.theme, viewport: s.play.device, ...(vp ? { width: vp.width, height: vp.height } : {}), mode: s.play.slides ? 'slides' : 'play' }
229
+ }
230
+ const sel = s.selection.map((k) => s.nodes.find((n) => n.key === k)).filter((n): n is Node => !!n)
231
+ const shown = focus && sel.length ? sel : visibleNodes()
232
+ const first = shown[0]
233
+ return {
234
+ board: s.board, frames: [...new Set(shown.map((n) => n.frame))], theme: first?.theme ?? s.viewTheme,
235
+ ...(first ? { width: Math.round(first.w), height: Math.round(first.h) } : {}),
236
+ zoom: Math.round((s.scale ?? 1) * 100) / 100, mode: s.interact ? 'interact' : 'canvas',
237
+ }
238
+ }
239
+
240
+ /** The nodes actually on screen now - the canvas's instances whose box meets the viewport, left of
241
+ * nothing that covers it (the panel and the lane leave their own room). */
242
+ export function visibleNodes(): Node[] {
243
+ const s = useStore.getState()
244
+ const W = window.innerWidth, H = window.innerHeight
245
+ const out: Node[] = []
246
+ for (const n of s.nodes) {
247
+ if (n.missing) continue
248
+ const el = document.querySelector(`.sh-node[data-node="${CSS.escape(n.key)}"]`)
249
+ const r = el?.getBoundingClientRect()
250
+ if (r && r.right > 0 && r.bottom > 0 && r.left < W && r.top < H) out.push(n)
251
+ }
252
+ return out.slice(0, 20)
253
+ }
254
+
255
+ // ---- UI steps: the shell's own, written the moment they happen, only while recording ----------------
256
+
257
+ let unwatch: (() => void) | null = null
258
+ function watchUi() {
259
+ unwatch?.()
260
+ // the frames in view, once the camera has settled (two looks a beat apart agree): a pan to another
261
+ // frame, described without a click, still names it
262
+ const sig = () => { const v = viewNow(); return `${(v.frames ?? []).join()}@${Math.round((v.zoom ?? 1) * 20) / 20}` } // frames and zoom
263
+ let lastView = sig()
264
+ let sample = lastView
265
+ const settle = setInterval(() => {
266
+ const id = useJamCapture.getState().current
267
+ if (!id || useStore.getState().play) return
268
+ const now_ = sig()
269
+ if (now_ === sample && now_ !== lastView) { lastView = now_; addStep(id, { at: now(), event: 'view', view: viewNow() }) }
270
+ sample = now_
271
+ }, 600)
272
+ let prev = useStore.getState()
273
+ const unsub = useStore.subscribe((s) => {
274
+ const id = useJamCapture.getState().current
275
+ const p = prev
276
+ prev = s
277
+ if (!id) return
278
+ const at = now()
279
+ if (p.play && !s.play) playLeftAt = performance.now()
280
+ if (s.selection.join() !== p.selection.join() && s.selection.length) {
281
+ addStep(id, { at, event: 'select', frames: [...new Set(s.selection.map((k) => frameIdOf(k)).filter((f): f is string => !!f))] }, s.selection, performance.now() - playLeftAt < 200)
282
+ }
283
+ for (const n of s.nodes) {
284
+ const o = p.nodes.find((x) => x.key === n.key)
285
+ if (!o) continue
286
+ // a device change: the node took a preset's size (its width names the viewport)
287
+ if (o.w !== n.w && n.sizeMode === 'device') {
288
+ const vp = Object.entries(CONFIG.viewports).find(([, v]) => v.width === Math.round(n.w))
289
+ addStep(id, { at, frame: n.frame, event: 'device', viewport: vp?.[0] ?? 'custom', width: Math.round(n.w), height: Math.round(n.h), src: n.key })
290
+ } else if ((Math.round(o.w) !== Math.round(n.w) || Math.round(o.h) !== Math.round(n.h)) && n.sizeMode !== 'device' && s.resized !== p.resized && s.resized?.key === n.key) {
291
+ // (a person resized it - never a measurement: only a resize sets `resized`)
292
+ // dragged to a size of its own: a step like a device, and the size it is about now - one step per drag (a
293
+ // drag moves it on every frame: the last step of it, still going, takes the new size)
294
+ const lastSteps = capOf(id)?.steps ?? []
295
+ const last = lastSteps[lastSteps.length - 1] as (Step & { src?: string }) | undefined
296
+ if (last && last.event === 'device' && last.src === n.key && last.viewport === 'custom' && at - last.at < 1500) {
297
+ mutate((st) => { const c = st.caps[id]; const l = c?.steps[c.steps.length - 1] as (Step & { src?: string; width?: number; height?: number }) | undefined; if (c && l) { l.width = Math.round(n.w); l.height = Math.round(n.h); l.at = at; sampleSize(c, l) } })
298
+ } else addStep(id, { at, frame: n.frame, event: 'device', viewport: 'custom', width: Math.round(n.w), height: Math.round(n.h), src: n.key })
299
+ }
300
+ if (o.theme !== n.theme && s.viewTheme === p.viewTheme && (n.theme === 'light' || n.theme === 'dark')) addStep(id, { at, frame: n.frame, event: 'theme', theme: n.theme, src: n.key })
301
+ }
302
+ if (s.viewTheme !== p.viewTheme && (s.viewTheme === 'light' || s.viewTheme === 'dark')) addStep(id, { at, event: 'theme', theme: s.viewTheme })
303
+ // play: the stage's device and theme through the shell's controls (its screen comes from the stage)
304
+ if (s.play && p.play) {
305
+ if (s.play.theme !== p.play.theme && (s.play.theme === 'light' || s.play.theme === 'dark')) addStep(id, { at, frame: s.play.at, event: 'theme', theme: s.play.theme })
306
+ if (s.play.device !== p.play.device) { const vp = CONFIG.viewports[s.play.device]; addStep(id, { at, frame: s.play.at, event: 'device', viewport: s.play.device, width: vp?.width ?? 0, height: vp?.height ?? 0 }) }
307
+ }
308
+ // a screen change the SHELL made (arrows, the walker, a variant): the stage reports its own
309
+ // navigation itself, so a change it reported (sh:stage-at) is not recorded twice
310
+ if (s.play && p.play && s.play.at !== p.play.at) {
311
+ if (stageNav !== s.play.at) screenStep(id, at, s.play.at)
312
+ }
313
+ if (!!s.play !== !!p.play) addStep(id, { at, event: 'view', view: viewNow() })
314
+ if (!!s.interact !== !!p.interact) addStep(id, { at, ...(s.interact || p.interact ? { frame: frameIdOf((s.interact ?? p.interact)!), src: (s.interact ?? p.interact)! } : {}), event: 'interact', on: !!s.interact })
315
+ })
316
+ unwatch = () => { unsub(); clearInterval(settle) }
317
+ }
318
+
319
+ /** Words heard for a capture (recording or still draining): one `words` step per phrase, at its first
320
+ * word, each word with its time on the capture's clock (sample 0 = the capture's start). */
321
+ setWordsListener((capture, words) => {
322
+ const c = capOf(capture)
323
+ if (!c || !words.length) return
324
+ const ms = (sample: number) => c.t0 + (sample / RATE) * 1000
325
+ addStep(capture, { at: ms(words[0].at), event: 'words', text: words.map((w) => w.w).join(' '), words: words.map((w) => ({ w: w.w, at: ms(w.at) })) })
326
+ })
327
+
328
+ /** A copy happened (path or image, of which frame) - never what the clipboard holds. */
329
+ export function recordCopy(what: 'path' | 'image', frame: string) {
330
+ const id = useJamCapture.getState().current
331
+ if (id) addStep(id, { at: now(), frame, event: 'copied', what })
332
+ }
333
+
334
+ // ---- trail from the frames ---------------------------------------------------------------------------
335
+
336
+ /** A frame or the stage posted something about a capture. `key` is the instance it came from. Returns
337
+ * true when the message was a capture message (handled or dropped). */
338
+ export function onFrameMessage(key: string, data: Record<string, any>): boolean {
339
+ const t = data.type
340
+ if (t !== 'sh:capture-hello' && t !== 'sh:trail' && t !== 'sh:probe' && t !== 'sh:capture-flushed') return false
341
+ const inst = instances.get(key)
342
+ if (!inst || !current(inst)) return true
343
+ if (t === 'sh:capture-hello') {
344
+ if (inst.nonce && data.nonce === inst.nonce && typeof data.captureDoc === 'string') {
345
+ inst.captureDoc = data.captureDoc; inst.nonce = undefined
346
+ // bound: tell this document what it may have missed (a frame can be ready before its load fires)
347
+ frameReady(key)
348
+ }
349
+ return true
350
+ }
351
+ if (!inst.captureDoc || data.captureDoc !== inst.captureDoc) return true
352
+ if (t === 'sh:probe') { probeAnswered(key, data); return true }
353
+ const cap = typeof data.id === 'string' ? capOf(data.id) : undefined
354
+ if (!cap) return true // a capture neither recording nor draining
355
+ if (t === 'sh:capture-flushed') {
356
+ if (cap.flushWait.get(key) === data.captureDoc) { mutate((s) => { s.caps[cap.id]?.flushWait.delete(key) }); trySeal(cap.id) }
357
+ return true
358
+ }
359
+ const dedupe = `${key}|${data.captureDoc}|${data.seq}`
360
+ if (cap.seen.has(dedupe)) return true
361
+ cap.seen.add(dedupe)
362
+ const at = Number(data.at)
363
+ if (!Number.isFinite(at)) return true
364
+ const frame = typeof data.frame === 'string' ? data.frame : frameIdOf(key)
365
+ const base = { at, seq: Number(data.seq) || 0, src: key, ...(frame ? { frame } : {}) }
366
+ const mode = data.mode === 'play' ? 'play' : data.mode === 'canvas' ? 'canvas' : 'interact'
367
+ switch (data.event) {
368
+ case 'pick': case 'click': case 'hover': {
369
+ const pt = data.point && Number.isFinite(data.point.x) ? { x: data.point.x, y: data.point.y } : null
370
+ const n = data.event === 'hover' ? undefined : cap.points.length + 1
371
+ mutate((s) => {
372
+ const c = s.caps[cap.id]
373
+ if (!c) return
374
+ if (n && pt) c.points.push({ n, key, x: pt.x, y: pt.y })
375
+ c.steps.push({ ...base, event: data.event, bundle: cleanBundle(data.bundle), mode, ...(n ? { point: n } : {}) }); sample(c, { at: base.at, event: data.event, frame: base.frame, src: key })
376
+ })
377
+ if (data.event === 'pick' && frame) shoot(cap.id, frame, 'pick', key === 'stage' ? undefined : key)
378
+ return true
379
+ }
380
+ case 'field': addStep(cap.id, { ...base, event: 'field', bundle: cleanBundle(data.bundle), value: String(data.value ?? '') }); return true
381
+ case 'screen': screenStep(cap.id, at, String(data.to ?? ''), base.seq); return true
382
+ case 'scroll': if (Number.isFinite(Number(data.y))) addStep(cap.id, { ...base, event: 'scroll', y: Number(data.y) }); return true
383
+ default: return true
384
+ }
385
+ }
386
+
387
+ /** A frame's bundle (`{ el: { semantics: { tag, quote, testId, ariaLabel, role }, cssPath, source } }`,
388
+ * safeBundle) as the record keeps it: flat, strings only - a masked one keeps its '•••'. */
389
+ /** One screen change - each navigation once, by whoever made it (revisits are their own lines). */
390
+ function screenStep(id: string, at: number, to: string, seq?: number) {
391
+ if (!capOf(id) || !to) return
392
+ addStep(id, { at, frame: to, event: 'screen', to, ...(seq !== undefined ? { seq } : {}) })
393
+ }
394
+ /** The stage moved itself to `at` (sh:stage-at): its recorder has the step; the shell's play state
395
+ * follows without a second line. */
396
+ let stageNav: string | null = null
397
+ /** Run the play-state update the stage's own navigation causes: during it (and only during it) the
398
+ * shell does not record a second line. */
399
+ export function stageNavigated(at: string, update: () => void) { stageNav = at; try { update() } finally { stageNav = null } }
400
+
401
+ const cleanBundle = (b: unknown): Bundle => {
402
+ const o = (b && typeof b === 'object' ? b : {}) as Record<string, any>
403
+ const el = (o.el && typeof o.el === 'object' ? o.el : o) as Record<string, any>
404
+ const sem = (el.semantics && typeof el.semantics === 'object' ? el.semantics : el) as Record<string, unknown>
405
+ const out: Bundle = {}
406
+ for (const k of ['tag', 'quote', 'testId', 'ariaLabel', 'role'] as const) if (typeof sem[k] === 'string') out[k] = sem[k] as string
407
+ for (const k of ['cssPath', 'source'] as const) if (typeof el[k] === 'string') out[k] = el[k] as string
408
+ return out
409
+ }
410
+
411
+ // ---- canvas-mode clicks and hovers: the overlay keeps the click and asks the frame ------------------
412
+
413
+ const probes = new Map<string, { cap: string; key: string; at: number; event: 'click' | 'hover'; x: number; y: number; timer: ReturnType<typeof setTimeout>; selectStep?: Step }>()
414
+
415
+ const local = (iframe: HTMLIFrameElement, clientX: number, clientY: number) => {
416
+ const r = iframe.getBoundingClientRect()
417
+ const sx = iframe.clientWidth ? r.width / iframe.clientWidth : 1
418
+ const sy = iframe.clientHeight ? r.height / iframe.clientHeight : 1
419
+ return { x: (clientX - r.left) / sx, y: (clientY - r.top) / sy }
420
+ }
421
+
422
+ function probe(key: string, event: 'click' | 'hover', clientX: number, clientY: number, pressAt?: number) {
423
+ const id = useJamCapture.getState().current
424
+ const inst = instances.get(key)
425
+ if (!id || !inst || !current(inst)) return
426
+ const at = now()
427
+ const { x, y } = local(inst.iframe, clientX, clientY)
428
+ const req = `r-${rand(8)}`
429
+ // one act, one line: the select this very click made (the overlay selects on pointerdown) is folded
430
+ // into it when it answers - found now, by reference, whatever lands in the timeline meanwhile
431
+ const frameNow = frameIdOf(key)
432
+ const capNow = capOf(id)
433
+ // the select made between this press and its release (the overlay selects on pointerdown) - however
434
+ // long the press; a selection made before the press is its own line
435
+ const selectStep = event === 'click' && capNow && pressAt !== undefined ? [...capNow.steps].reverse().find((x) => x.event === 'select' && x.at >= pressAt - 5 && x.at <= at && x.frames.length === 1 && x.frames[0] === frameNow) : undefined
436
+ const timer = setTimeout(() => {
437
+ probes.delete(req)
438
+ mutate((s) => { const c = s.caps[id]; if (c) { c.probes -= 1; c.steps.push({ at, frame: frameIdOf(key), event: 'missing', what: `the ${event} at ${Math.round(x)},${Math.round(y)} never answered` }) } })
439
+ trySeal(id)
440
+ }, PROBE_MS)
441
+ probes.set(req, { cap: id, key, at, event, x, y, timer, selectStep })
442
+ mutate((s) => { const c = s.caps[id]; if (c) c.probes += 1 })
443
+ post(inst, { type: 'sh:probe', id, req, x, y })
444
+ }
445
+
446
+ function probeAnswered(key: string, data: Record<string, any>) {
447
+ const p = probes.get(String(data.req ?? ''))
448
+ if (!p || p.key !== key) return
449
+ probes.delete(String(data.req))
450
+ clearTimeout(p.timer)
451
+ const frame = frameIdOf(key)
452
+ mutate((s) => {
453
+ const c = s.caps[p.cap]
454
+ if (!c) return
455
+ c.probes -= 1
456
+ if (!data.bundle) return
457
+ const n = p.event === 'click' ? c.points.length + 1 : undefined
458
+ if (n) c.points.push({ n, key, x: p.x, y: p.y })
459
+ // one act, one line: a click that also selected its frame is one click that names the selection
460
+ const i = p.selectStep ? c.steps.indexOf(p.selectStep as (typeof c.steps)[number]) : -1
461
+ const selects = i >= 0
462
+ if (selects) c.steps.splice(i, 1)
463
+ c.steps.push({ at: p.at, ...(frame ? { frame } : {}), event: p.event, bundle: cleanBundle(data.bundle), mode: 'canvas', ...(n ? { point: n } : {}), ...(selects ? { selects: true } : {}) }); sample(c, { at: p.at, event: p.event, frame, src: key })
464
+ })
465
+ trySeal(p.cap)
466
+ // a click on a field: you mean to type there - the frame turns interactive and the caret goes in the
467
+ // field, so the letters you type are the field's and never end, restart or drop the capture
468
+ if (p.event === 'click' && data.field === true && useJamCapture.getState().current === p.cap) focusField(p.key, p.x, p.y)
469
+ }
470
+
471
+ function focusField(key: string, x: number, y: number) {
472
+ const st = useStore.getState()
473
+ if (st.play || !st.nodes.some((n) => n.key === key)) return
474
+ if (st.interact !== key) st.setInteract(key)
475
+ // after the overlay has stepped aside (interact removes it): focus the iframe, then the field in it
476
+ requestAnimationFrame(() => requestAnimationFrame(() => {
477
+ const inst = instances.get(key)
478
+ if (!inst || !current(inst)) return
479
+ try { inst.iframe.focus() } catch { /* gone */ }
480
+ post(inst, { type: 'sh:focus-at', x, y })
481
+ }))
482
+ }
483
+
484
+ /** The canvas overlay saw a click (not a drag) on a node while a capture records. */
485
+ export function canvasClick(key: string, clientX: number, clientY: number, pressAt?: number) { cancelDwell(); probe(key, 'click', clientX, clientY, pressAt) }
486
+ /** When the press began, on the capture clock - the overlay calls this on pointerdown. */
487
+ export const pressNow = (): number => now()
488
+
489
+ let dwell: { key: string; cap: string; x: number; y: number; timer: ReturnType<typeof setTimeout> } | null = null
490
+ /** A capture ending (or dropped) takes its pending hover with it: a dwell belongs to one capture. */
491
+ const cancelDwell = () => { if (dwell) { clearTimeout(dwell.timer); dwell = null } }
492
+ let lastHover = 0
493
+ /** The pointer moved over a node in canvas mode: a hold of 0.8 s without moving past 4 px is a hover
494
+ * (at most one every 2 s); moving or leaving cancels it. */
495
+ export function canvasPointer(key: string | null, clientX = 0, clientY = 0) {
496
+ if (dwell && (!key || key !== dwell.key || Math.hypot(clientX - dwell.x, clientY - dwell.y) > 4)) { clearTimeout(dwell.timer); dwell = null }
497
+ const cur = useJamCapture.getState().current
498
+ if (!key || dwell || !cur) return
499
+ dwell = { key, cap: cur, x: clientX, y: clientY, timer: setTimeout(() => {
500
+ const d = dwell
501
+ dwell = null
502
+ if (!d || d.cap !== useJamCapture.getState().current || performance.now() - lastHover < 2_000) return
503
+ lastHover = performance.now()
504
+ probe(d.key, 'hover', d.x, d.y)
505
+ }, 800) }
506
+ pointerAt = { key, at: performance.now() }
507
+ }
508
+ /** the node under the pointer lately - where `I` shoots */
509
+ let pointerAt: { key: string; at: number } | null = null
510
+
511
+ // ---- pictures ------------------------------------------------------------------------------------------
512
+
513
+ function shoot(id: string, frame: string, why: 'key' | 'pick' | 'end', key?: string) {
514
+ const c = capOf(id)
515
+ if (!c || c.pictures.length + c.shots >= MAX_PICTURES) return
516
+ // each picture is a render on the server, and the capture waits for it as it ends: a point or the end
517
+ // pictures a frame once - only I asks for another
518
+ if (why !== 'key' && c.shotFrames.includes(frame)) return
519
+ // the instance pictured is the one this frame is about (its size for the run's pictures)
520
+ // (an I picture is a touch at this instant: a hover from before it, answering late, never overrides it)
521
+ const touchAt = now()
522
+ mutate((s) => { const x = s.caps[id]; if (x) { if (!x.shotFrames.includes(frame)) x.shotFrames.push(frame); if (why === 'key') sample(x, { at: touchAt, event: 'picture', frame, src: key }) } })
523
+ const at = now()
524
+ // the instance it was seen in: its size (a responsive defect is shot as seen) and its theme
525
+ const st = useStore.getState()
526
+ const n = (key ? st.nodes.find((x) => x.key === key && x.frame === frame) : undefined) ?? st.nodes.find((x) => x.frame === frame)
527
+ // as it was seen: on the stage's device now, or as it was on the stage (left play since), or its node
528
+ const seen = c.sizes[frame]
529
+ // after E: as the frame was at E (a resize while it finishes is not part of it); else as it is seen now
530
+ const atEnd = c.state === 'draining' ? c.endSizes?.[frame] ?? (c.endNodes ? sizeAtEnd(c, frame) : undefined) ?? seen : undefined
531
+ const onStage = st.play && st.play.at === frame ? sizeNow(frame) : undefined
532
+ const size = atEnd ? { width: atEnd.w, height: atEnd.h } : onStage ? { width: onStage.w, height: onStage.h } : seen?.play && why !== 'key' ? { width: seen.w, height: seen.h } : n ? { width: n.w, height: n.h } : undefined
533
+ const theme = st.play && st.play.at === frame ? st.play.theme : n?.theme ?? st.viewTheme
534
+ mutate((s) => { const x = s.caps[id]; if (x) x.shots += 1 })
535
+ const t0 = performance.now()
536
+ const done = (pid: string | null, late: boolean) => {
537
+ mutate((s) => {
538
+ const x = s.caps[id]
539
+ if (!x) return
540
+ x.shots -= 1
541
+ const tm = (x.timing ??= { pics: [], late: 0, failed: 0 })
542
+ if (pid) tm.pics.push(Math.round(performance.now() - t0)); else if (late) tm.late++; else tm.failed++
543
+ if (pid) { x.pictures.push(pid); x.steps.push({ at, frame, event: 'picture', id: pid, why }) }
544
+ else x.steps.push({ at, frame, event: 'missing', what: `no picture of ${frame} - ${late ? `it took over ${PICTURE_MS / 1000} s` : 'the render failed'}` })
545
+ })
546
+ trySeal(id)
547
+ }
548
+ const ctl = new AbortController()
549
+ const timer = setTimeout(() => ctl.abort(), PICTURE_MS)
550
+ // settled exactly once - a picture, or a missing one - whatever fails: the request, its body, the abort
551
+ let settled = false
552
+ const finish = (pid: string | null) => { if (settled) return; settled = true; clearTimeout(timer); done(pid, ctl.signal.aborted) }
553
+ fetch(`${ROUTE}/api/jam/picture`, {
554
+ method: 'POST', signal: ctl.signal, headers: { 'content-type': 'application/json', 'x-mv-c': csrf() },
555
+ body: JSON.stringify({ capture: id, frame, theme, ...(size ? { w: Math.round(size.width), h: Math.round(size.height) } : {}) }),
556
+ }).then(async (r) => (r.ok ? ((await r.json()) as { id?: string }).id ?? null : null))
557
+ .catch(() => null)
558
+ .then((pid) => finish(typeof pid === 'string' ? pid : null))
559
+ }
560
+
561
+ // ---- start, end, seal ----------------------------------------------------------------------------------
562
+
563
+ const broadcast = (msg: unknown) => { for (const inst of instances.values()) post(inst, msg) }
564
+
565
+ /** The session is being wrapped up (its talk flushing, its record being written): no capture starts. */
566
+ let sessionEnding = false
567
+ export const setSessionEnding = (on: boolean) => { sessionEnding = on }
568
+
569
+ /** A capture of a session tells the server it is open - recording, sealing, waiting to send - and renews
570
+ * that every 20 s until it lands or is dropped: the session's record waits for it, whatever tab ends it. */
571
+ const openOnServer = new Map<string, { session: string; timer: ReturnType<typeof setInterval> }>()
572
+ const tellOpen = (capture: string, session: string, on: boolean) => {
573
+ void fetch(`${ROUTE}/api/jam/capture-open`, { method: 'POST', headers: { 'content-type': 'application/json', 'x-mv-c': csrf() }, body: JSON.stringify({ capture, session, on }) })
574
+ .then(async (r) => { if (on && r.status === 409 && (await r.json().catch(() => null))?.ended) leftOutOfSession(capture) }, () => {})
575
+ }
576
+ /** The server had already written the session's record (in another tab) when it heard of this capture: it is
577
+ * not in that record - it goes on as a capture of its own, and you are told. */
578
+ const toldOutside = new Set<string>()
579
+ function leftOutOfSession(capture: string) {
580
+ // said once, whichever answer says it first (the registration's, or the upload's) - whatever order they come in
581
+ if (toldOutside.has(capture)) return
582
+ toldOutside.add(capture)
583
+ releaseOpen(capture, false)
584
+ useJamCapture.setState((st) => {
585
+ const c = st.caps[capture]
586
+ if (!c?.session) return {}
587
+ const { session: _gone, ...rest } = c
588
+ return { caps: { ...st.caps, [capture]: rest as Cap } }
589
+ })
590
+ useStore.getState().toast('the session ended in another tab - this capture is on its own, not in its record')
591
+ }
592
+ function holdOpen(capture: string, session: string) {
593
+ tellOpen(capture, session, true)
594
+ const timer = setInterval(() => {
595
+ const st = useJamCapture.getState()
596
+ if (st.current === capture || st.caps[capture] || st.unsent.includes(capture) || pendingUploads.has(capture)) tellOpen(capture, session, true)
597
+ else releaseOpen(capture, false)
598
+ }, 20_000)
599
+ openOnServer.set(capture, { session, timer })
600
+ }
601
+ /** Stop renewing (it landed - the server knows - or it was dropped: say so). */
602
+ function releaseOpen(capture: string, tell: boolean) {
603
+ const o = openOnServer.get(capture)
604
+ if (!o) return
605
+ clearInterval(o.timer)
606
+ openOnServer.delete(capture)
607
+ if (tell) tellOpen(capture, o.session, false)
608
+ }
609
+
610
+ /** The session running now (the queue view holds it), if any. */
611
+ let sessionNow: () => string | undefined = () => undefined
612
+ /** A card's frames as the server holds them (a resumed capture counts the ones it carries on). */
613
+ let cardFrames: (id: string) => string[] = () => []
614
+ void import('./jam-queue.ts').then(({ useJamQueue }) => {
615
+ sessionNow = () => useJamQueue.getState().session?.id
616
+ cardFrames = (id) => useJamQueue.getState().cards?.find((c) => c.id === id)?.frames ?? []
617
+ })
618
+
619
+ function begin(reviewing?: Cap['reviewing'], from?: CaptureRecord, session = sessionNow()): boolean {
620
+ const s = useStore.getState()
621
+ // checked here, as recording starts: a Resume that began loading before the session's wrap-up never
622
+ // starts recording into it
623
+ if (sessionEnding) { s.toast('the session is being wrapped up - a moment'); return false }
624
+ // a capture becomes a thread on a board: the Not on a board view takes none (as it takes no comment)
625
+ if (s.board === UNPLACED) { s.toast('captures need a board - open one first'); return false }
626
+ const id = `cap-${Date.now()}-${rand(6)}`
627
+ const t = now()
628
+ // Resume: the draft's steps, moved up so its last step meets this instant (the pause between the two
629
+ // is not part of the capture), its pictures and its numbered points; then recording goes on
630
+ // (to just before this instant: everything carried over is strictly before `resumes.at`, the new part after)
631
+ // (the latest instant in it: a step's, or a word's inside the last phrase - every carried word stays
632
+ // before the resume)
633
+ const latest = from ? Math.max(...from.timeline.flatMap((x) => [x.at, ...(x.event === 'words' && x.words ? x.words.map((w) => w.at) : [])]), -Infinity) : 0
634
+ const shift = from && Number.isFinite(latest) ? t - 1 - latest : 0
635
+ // (a phrase's words move with it - each word keeps its place in the phrase)
636
+ const steps = from ? from.timeline.map((x) => ({ ...x, at: x.at + shift, ...(x.event === 'words' && x.words ? { words: x.words.map((w) => ({ ...w, at: w.at + shift })) } : {}) }) as Step) : []
637
+ const cap: Cap = {
638
+ id, state: 'recording', board: s.board, t0: from ? (steps[0]?.at ?? t) : t, steps, seen: new Set(),
639
+ points: steps.flatMap((x) => ('point' in x && typeof x.point === 'number' ? [{ n: x.point, key: '', x: 0, y: 0 }] : [])), pictures: from ? [...from.pictures] : [],
640
+ sizes: { ...(from?.sizes ?? {}) }, keys: {}, sizedAt: {},
641
+ probes: 0, shots: 0, shotFrames: steps.flatMap((x) => (x.event === 'picture' && x.frame ? [x.frame] : [])), hearing: false, flushWait: new Map(), ...(reviewing ? { reviewing } : {}), ...(from ? { resumes: { id: from.id, at: t }, inherited: cardFrames(from.id) } : {}),
642
+ ...(session ? { session } : {}),
643
+ }
644
+ // what is on screen and selected as it starts - a resumed one too, just after the resume (you may have
645
+ // moved to another frame in the pause: the record names it)
646
+ const at0 = from ? t + 1 : cap.t0
647
+ cap.steps.push({ at: at0, event: 'view', view: viewNow(true) })
648
+ // starting with frames selected makes them its focus (on the canvas - in play the stage is the surface)
649
+ if (!s.play && s.selection.length) cap.steps.push({ at: at0, event: 'select', frames: [...new Set(s.selection.map((k) => frameIdOf(k)).filter((f): f is string => !!f))] })
650
+ // the sizes it starts with: the stage's device in play, the selected frames on the canvas
651
+ if (s.play) { const z = sizeNow(s.play.at); if (z) cap.sizes[s.play.at] = z } else if (s.selection.length) sample(cap, cap.steps[cap.steps.length - 1] as { event: string; frames?: string[] }, s.selection)
652
+ useJamCapture.setState((st) => ({ current: id, caps: { ...st.caps, [id]: cap }, tick: st.tick + 1, ...(from ? { resuming: [...st.resuming, from.id] } : {}) }))
653
+ if (cap.session) holdOpen(id, cap.session)
654
+ broadcast({ type: 'sh:capture', id, on: true })
655
+ watchUi()
656
+ void voiceBegin(id, cap.t0) // your words, if you said yes to the microphone (spec 27 Part 4)
657
+ return true
658
+ }
659
+
660
+ /** Stop recording `id`: flush every recorder that holds a captureDoc, wait for them, for every probe
661
+ * and picture still out, then seal. */
662
+ function drain(id: string) {
663
+ const c = capOf(id)
664
+ if (!c || c.state !== 'recording') return
665
+ const s = useJamCapture.getState()
666
+ // text written in the notes card and never added is the capture's last note
667
+ const draft = s.noteDraft.trim()
668
+ mutate((st) => {
669
+ const x = st.caps[id]
670
+ if (!x) return
671
+ x.state = 'draining'
672
+ ;(x.timing ??= { pics: [], late: 0, failed: 0 }).endAt = performance.now()
673
+ // its frames' sizes as they are at E - a resize while it finishes is not part of it; and every instance as it
674
+ // is at E, for a step that answers late (a hover still in flight names a frame then)
675
+ x.endSizes = framesSizes(x)
676
+ const vis = new Set(visibleNodes().map((n) => n.key))
677
+ x.endNodes = useStore.getState().nodes.map((n) => ({ key: n.key, frame: n.frame, w: Math.round(n.w), h: Math.round(n.h), visible: vis.has(n.key) }))
678
+ if (draft) x.steps.push({ at: now(), event: 'note', text: draft })
679
+ for (const inst of instances.values()) if (inst.captureDoc && current(inst)) x.flushWait.set(inst.key, inst.captureDoc)
680
+ })
681
+ useJamCapture.setState({ noteDraft: '', noteOpen: false })
682
+ // in play the stage is what it is about: pictured now. On the canvas the end pictures wait until every frame
683
+ // has flushed and every probe answered (trySeal) - a hover still in flight may name the frame to picture
684
+ const st = useStore.getState()
685
+ if (st.play) { mutate((s) => { const x = s.caps[id]; if (x) x.endShot = true }); shoot(id, st.play.at, 'end') }
686
+ broadcast({ type: 'sh:capture', id, on: false })
687
+ // the audio is cut at the key press, then its last words are awaited (3 s at most)
688
+ mutate((st) => { const x = st.caps[id]; if (x) x.hearing = true })
689
+ const tWords = performance.now()
690
+ void voiceEnd(id).then((complete) => {
691
+ mutate((st) => {
692
+ const x = st.caps[id]
693
+ if (!x) return
694
+ x.hearing = false
695
+ ;(x.timing ??= { pics: [], late: 0, failed: 0 }).wordsMs = Math.round(performance.now() - tWords)
696
+ if (!complete) x.steps.push({ at: now(), event: 'missing', what: 'words may be incomplete' })
697
+ })
698
+ trySeal(id)
699
+ })
700
+ setTimeout(() => {
701
+ mutate((st) => {
702
+ const x = st.caps[id]
703
+ if (!x || !x.flushWait.size) return
704
+ for (const k of x.flushWait.keys()) x.steps.push({ at: now(), frame: frameIdOf(k), event: 'missing', what: 'a frame never flushed' })
705
+ x.flushWait.clear()
706
+ })
707
+ trySeal(id)
708
+ }, FLUSH_MS)
709
+ trySeal(id)
710
+ }
711
+
712
+ /** The end pictures, once the timeline is whole: the frames it is about not pictured in it yet, three at most -
713
+ * each as the instance on screen shows it (a narrow, dark second copy as itself); never every frame that
714
+ * happens to be in view. None touched: the one frame it began on. Returns whether any was asked for. */
715
+ function endPictures(id: string): boolean {
716
+ const c = capOf(id)
717
+ if (!c || c.endShot) return false
718
+ mutate((s) => { const x = s.caps[id]; if (x) x.endShot = true })
719
+ const onScreen = visibleNodes()
720
+ // resumed: the new part's frames (what the part before was about was pictured then - its pictures are carried);
721
+ // none touched in a fresh capture: the one frame it began on
722
+ const since = c.resumes?.at
723
+ const touched = touchedFrames(since === undefined ? c.steps : c.steps.filter((x) => x.at >= since), undefined, false)
724
+ const frames = (touched.length || since !== undefined ? touched : touchedFrames(c.steps).slice(0, 1)).filter((f) => !c.shotFrames.includes(f)).slice(0, 3)
725
+ // each as the instance it was touched on (else the one in view)
726
+ for (const f of frames) shoot(id, f, 'end', c.keys[f] ?? onScreen.find((n) => n.frame === f)?.key)
727
+ return frames.length > 0
728
+ }
729
+
730
+ /** Each frame the capture is about, at the size it is on screen now (the instance in view, else any): a device,
731
+ * or a width you dragged it to. In play, the stage's device. Its run is pictured at these sizes. */
732
+ function framesSizes(c: Cap): Record<string, { w: number; h: number; play?: true }> {
733
+ const st = useStore.getState()
734
+ const onScreen = visibleNodes()
735
+ const frames = new Set([...(c.inherited ?? []), ...touchedFrames(c.steps, undefined, true, c.resumes?.at)])
736
+ const out: Record<string, { w: number; h: number; play?: true }> = {}
737
+ for (const f of frames) {
738
+ // seen in play: as it was on the stage's device; on the canvas: as its node is now (a resize after the last
739
+ // touch counts); else as it was sampled
740
+ const sampled = c.sizes[f]
741
+ if (sampled?.play) { out[f] = sampled; continue }
742
+ // no instance known for it in this recording (carried on by a Resume, or seen without one): as measured then
743
+ if (!c.keys[f] && sampled) { out[f] = sampled; continue }
744
+ if (st.play && st.play.at === f) { const z = sizeNow(f); if (z) out[f] = z; continue }
745
+ const k = c.keys[f]
746
+ const n = (k ? st.nodes.find((x) => x.key === k && x.frame === f) : undefined) ?? onScreen.find((x) => x.frame === f) ?? st.nodes.find((x) => x.frame === f)
747
+ if (n && n.w > 0 && n.h > 0) out[f] = { w: Math.round(n.w), h: Math.round(n.h) }
748
+ else if (sampled) out[f] = { w: sampled.w, h: sampled.h }
749
+ }
750
+ return out
751
+ }
752
+
753
+ /** The sizes it seals with: as they were at E; a frame only named by a step that came in after (a hover still in
754
+ * flight) as it was sampled when that step came - never as it is now. */
755
+ function sealedSizes(c: Cap): Record<string, { w: number; h: number; play?: true }> {
756
+ if (!c.endSizes) return framesSizes(c)
757
+ const out = { ...c.endSizes }
758
+ for (const f of new Set([...(c.inherited ?? []), ...touchedFrames(c.steps, undefined, true, c.resumes?.at)])) if (!out[f] && c.sizes[f]) out[f] = c.sizes[f]
759
+ return out
760
+ }
761
+
762
+ /** Jam timings (spec 27a Part D): numbers only, to the dev server's local log - best effort, never in the way. */
763
+ const perf = (body: Record<string, unknown>, keepalive = false) => {
764
+ try { void fetch(`${ROUTE}/api/perf/jam`, { method: 'POST', headers: { 'content-type': 'application/json', 'x-mv-c': csrf() }, body: JSON.stringify(body), ...(keepalive ? { keepalive: true } : {}) }).catch(() => {}) } catch { /* never in the way */ }
765
+ }
766
+ /** A capture's timing as it seals: E to sealed (none when the page sealed it), the wait for its words, its pictures. */
767
+ function timeCapture(c: Cap, interrupted: boolean) {
768
+ const t = c.timing
769
+ perf({ kind: 'capture', capture: c.id, ...(t?.endAt !== undefined && !interrupted ? { sealMs: performance.now() - t.endAt } : {}),
770
+ ...(t?.wordsMs !== undefined ? { wordsWaitMs: t.wordsMs } : {}),
771
+ pictures: { count: t?.pics.length ?? 0, ms: t?.pics ?? [], late: t?.late ?? 0, failed: t?.failed ?? 0 }, ...(interrupted ? { interrupted: true } : {}) }, interrupted)
772
+ }
773
+ const uploadTries = new Map<string, number>()
774
+
775
+ /** A capture's sealed record: its steps in time order, internal fields stripped, its context, view, pictures and
776
+ * sizes. `interrupted`: sealed by its page leaving, as it stood. */
777
+ function buildRecord(c: Cap, steps: Cap['steps'], interrupted = false): CaptureRecord {
778
+ const timeline = [...steps].sort((a, b) => a.at - b.at || (a.seq ?? 0) - (b.seq ?? 0)).map(({ seq: _s, src: _k, ...step }) => step as Step)
779
+ return {
780
+ format: 'marver.jam.capture', v: 1, id: c.id,
781
+ context: { canvas: location.host, board: c.board, ...(c.session ? { session: c.session } : {}), ...(c.reviewing ? { reviewing: c.reviewing } : {}), ...(c.resumes ? { resumes: c.resumes } : {}), ...(interrupted ? { interrupted: true as const } : {}) },
782
+ view: (timeline.find((x) => x.event === 'view') as Extract<Step, { event: 'view' }> | undefined)?.view ?? {},
783
+ timeline, pictures: c.pictures, ...(() => { const sizes = sealedSizes(c); return Object.keys(sizes).length ? { sizes } : {} })(),
784
+ }
785
+ }
786
+
787
+ function trySeal(id: string) {
788
+ const c = capOf(id)
789
+ if (!c || c.state !== 'draining' || c.flushWait.size || c.probes > 0) return
790
+ // every step is in: now the end pictures - sealing waits only for those actually under way (one refused at
791
+ // the picture limit is never waited for)
792
+ if (!c.endShot) endPictures(id)
793
+ const c2 = capOf(id)
794
+ if (!c2 || c2.shots > 0 || c2.hearing) return
795
+ const record = buildRecord(c2, c2.steps)
796
+ timeCapture(c2, false)
797
+ // sealed: drop everything for this id that arrives later
798
+ useJamCapture.setState((st) => { const caps = { ...st.caps }; delete caps[id]; return { caps, tick: st.tick + 1, sealed: [...st.sealed, { id }] } })
799
+ forgetHeard(id)
800
+ keepSealed(record)
801
+ void upload(record)
802
+ }
803
+
804
+ // ---- the sealed record is kept until the server has it ----------------------------------------------
805
+
806
+ /** Kept per workspace: another project served later on the same port never receives this one's captures. */
807
+ const KEPT = `mv-jam-sealed:${CONFIG.workspace ?? 'unknown'}`
808
+ const syncUnsent = () => useJamCapture.setState({ unsent: [...pendingUploads.keys()] })
809
+ const keepSealed = (r: CaptureRecord) => { try { const all = JSON.parse(localStorage.getItem(KEPT) ?? '{}'); all[r.id] = r; localStorage.setItem(KEPT, JSON.stringify(all)) } catch { /* memory only */ } pendingUploads.set(r.id, r); syncUnsent() }
810
+ const forgetSealed = (id: string) => { pendingUploads.delete(id); syncUnsent(); try { const all = JSON.parse(localStorage.getItem(KEPT) ?? '{}'); delete all[id]; localStorage.setItem(KEPT, JSON.stringify(all)) } catch { /* memory only */ } }
811
+ const pendingUploads = new Map<string, CaptureRecord>()
812
+ let retryTimer: ReturnType<typeof setTimeout> | null = null
813
+ /** Post a sealed record - the same id, idempotent on the server - until it takes. A refusal about the
814
+ * record itself (not the network) is final and said once; a dev server that is away is waited for. */
815
+ async function upload(record: CaptureRecord, leaving = false): Promise<void> {
816
+ type Verdict = { ok?: boolean; card?: { id: string; rev: number }; error?: string; outside?: boolean }
817
+ let r: Verdict | null = null
818
+ try {
819
+ // the workspace it was made in travels with it: another project served on this port refuses it
820
+ uploadTries.set(record.id, (uploadTries.get(record.id) ?? 0) + 1)
821
+ const tUp = performance.now()
822
+ const payload = JSON.stringify({ record, workspace: CONFIG.workspace })
823
+ // a page that is going sends with keepalive (64 kB at most) so the request outlives it; a bigger one, the next page
824
+ const res = await fetch(`${ROUTE}/api/jam/capture`, { method: 'POST', headers: { 'content-type': 'application/json', 'x-mv-c': csrf() }, body: payload, ...(leaving && payload.length < 60_000 ? { keepalive: true } : {}) })
825
+ const body = (await res.json().catch(() => null)) as Verdict | null
826
+ // only an explicit verdict on THIS record is final: taken (200, naming its card) or refused as a
827
+ // record (400, saying why). Anything else - jam not running yet, another workspace, an unreadable
828
+ // answer, a gate, a server error - is the server being away: keep it and try again
829
+ r = res.ok && body?.ok === true && body.card?.id === record.id ? body
830
+ : res.status === 400 && body && typeof body.error === 'string' ? body : null
831
+ if (r?.ok) { perf({ kind: 'upload', capture: record.id, ms: performance.now() - tUp, tries: uploadTries.get(record.id) ?? 1 }, leaving); uploadTries.delete(record.id) }
832
+ } catch { r = null }
833
+ if (!r) {
834
+ if (!retryTimer) useStore.getState().toast('the capture is kept here and will be sent when marver dev answers')
835
+ retryTimer ??= setTimeout(() => { retryTimer = null; for (const x of pendingUploads.values()) void upload(x) }, 5_000)
836
+ return
837
+ }
838
+ forgetSealed(record.id)
839
+ releaseOpen(record.id, !r.ok) // it landed (the server knows); refused, it is no longer open either
840
+ // the server holds it: a resumed draft is replaced now (or, refused, shows again)
841
+ const was = record.context.resumes?.id
842
+ useJamCapture.setState((st) => ({ sealed: st.sealed.map((x) => (x.id === record.id ? { ...x, ...(r!.card ? { card: r!.card } : {}) } : x)), ...(was ? { resuming: st.resuming.filter((x) => x !== was) } : {}) }))
843
+ void import('./jam-queue.ts').then(({ useJamQueue }) => useJamQueue.getState().load())
844
+ if (!r.ok) useStore.getState().toast(`the capture could not be kept: ${r.error ?? 'unknown error'}`)
845
+ // it reached the server after its session ended (another tab wrote the record): on its own
846
+ else if (r.outside) leftOutOfSession(record.id)
847
+ }
848
+
849
+ /** A capture still recording, or finishing (its pictures and last words coming in), lives only in this page until it
850
+ * seals; a sealed one is kept in localStorage until it lands. Leaving the page asks first - and if you leave anyway
851
+ * (`pagehide`: a close, a reload, a navigation, the back-forward cache alike), every capture not sealed yet is sealed
852
+ * at once, as it stood, for good: a missing step says so, the server keeps it as a draft, and it goes out like any
853
+ * sealed record not sent yet - from the next page, or this one if it comes back. Carrying on is Resume, with a new id.
854
+ * Best effort: a crash or a killed process fires nothing. */
855
+ let guarded = false
856
+ function guardUnload() {
857
+ if (guarded || typeof window === 'undefined') return
858
+ guarded = true
859
+ window.addEventListener('beforeunload', (e) => {
860
+ if (!Object.keys(useJamCapture.getState().caps).length) return
861
+ e.preventDefault()
862
+ e.returnValue = ''
863
+ })
864
+ window.addEventListener('pagehide', () => sealOnLeave())
865
+ // back from the back-forward cache: what was kept goes out now (never amended, never dropped)
866
+ window.addEventListener('pageshow', (e) => { if (e.persisted) for (const r of pendingUploads.values()) void upload(r) })
867
+ }
868
+
869
+ /** Seal every capture not sealed yet, as it stands, and tear it down as a drop does - synchronously: the page is going. */
870
+ export function sealOnLeave() {
871
+ const s = useJamCapture.getState()
872
+ const caps = Object.values(s.caps)
873
+ if (!caps.length) return
874
+ const draft = s.noteDraft.trim()
875
+ const records: CaptureRecord[] = []
876
+ for (const c of caps) {
877
+ const at = now()
878
+ const steps = [...c.steps]
879
+ // text written in the notes card and never added is the capture's last note, as at E
880
+ if (c.id === s.current && draft) steps.push({ at, event: 'note', text: draft })
881
+ steps.push({ at, event: 'missing', what: c.state === 'recording' ? 'the page closed while it recorded' : 'the page closed before it finished' })
882
+ records.push(buildRecord(c, steps, true))
883
+ timeCapture(c, true)
884
+ // its frames stop recording, its microphone is let go; a late picture or word finds nothing to change
885
+ broadcast({ type: 'sh:capture', id: c.id, on: false })
886
+ voiceDrop(c.id)
887
+ forgetHeard(c.id)
888
+ }
889
+ cancelDwell()
890
+ unwatch?.(); unwatch = null
891
+ useJamCapture.setState((st) => ({ current: null, caps: {}, noteOpen: false, noteDraft: '', tick: st.tick + 1, sealed: [...st.sealed, ...records.map((r) => ({ id: r.id }))] }))
892
+ for (const r of records) { keepSealed(r); void upload(r, true) }
893
+ }
894
+
895
+ /** Records sealed in an earlier page that never reached the server: sent again at boot. */
896
+ export function resendSealed() {
897
+ guardUnload()
898
+ try {
899
+ const all = JSON.parse(localStorage.getItem(KEPT) ?? '{}') as Record<string, CaptureRecord>
900
+ // a session's capture waiting to be sent is still open: the server is told again until it lands
901
+ for (const r of Object.values(all)) { pendingUploads.set(r.id, r); if (r.context?.session && !openOnServer.has(r.id)) holdOpen(r.id, r.context.session); void upload(r) }
902
+ syncUnsent()
903
+ // a resumed capture still to send keeps its draft off the lane until it lands
904
+ const was = Object.values(all).flatMap((r) => (r.context?.resumes?.id ? [r.context.resumes.id] : []))
905
+ if (was.length) useJamCapture.setState((st) => ({ resuming: [...new Set([...st.resuming, ...was])] }))
906
+ } catch { /* none */ }
907
+ }
908
+
909
+ /** Resume's work, while the caller holds `settling`: load the record, let the caller settle, start. */
910
+ async function resumeNow(draft: string, ready?: () => Promise<boolean>): Promise<boolean> {
911
+ let rec: CaptureRecord | null = null
912
+ try {
913
+ const r = await fetch(`${ROUTE}/api/jam/capture?id=${encodeURIComponent(draft)}`, { headers: { 'x-mv-c': csrf() } })
914
+ const body = r.ok ? ((await r.json()) as { record?: CaptureRecord; sealed?: boolean }) : null
915
+ if (body?.record && body.sealed !== true) { useStore.getState().toast('the capture changed after it was sealed - it cannot be resumed'); return false }
916
+ rec = body?.record ?? null
917
+ } catch { rec = null }
918
+ if (!rec) { useStore.getState().toast('the capture could not be loaded to resume it'); return false }
919
+ if (useJamCapture.getState().current) return false
920
+ if (rec.context.board !== useStore.getState().board) { useStore.getState().toast('open the capture\'s board to resume it'); return false }
921
+ // the caller's last word before recording starts (the draft's edit saved, it is still a draft)
922
+ if (ready && !(await ready())) return false
923
+ // as things are NOW: nothing recording, still on the capture's board
924
+ if (useJamCapture.getState().current) return false
925
+ if (rec.context.board !== useStore.getState().board) { useStore.getState().toast('open the capture\'s board to resume it'); return false }
926
+ return begin(rec.context.reviewing, rec)
927
+ }
928
+
929
+ export const useJamCapture = create<CaptureState>((set, get) => ({
930
+ current: null,
931
+ caps: {},
932
+ noteOpen: false,
933
+ noteDraft: '',
934
+ tick: 0,
935
+ sealed: [],
936
+ unsent: [],
937
+ resuming: [],
938
+ lastEnded: null,
939
+ settling: null,
940
+ async resume(draft, ready) {
941
+ if (get().current) { useStore.getState().toast('a capture is recording - press E to end it first'); return false }
942
+ if (sessionEnding) { useStore.getState().toast('the session is being wrapped up - a moment'); return false }
943
+ return resumeNow(draft, ready)
944
+ },
945
+ start(reviewing, session) {
946
+ // one capture at a time: the one recording is ended or dropped first
947
+ if (get().current) { useStore.getState().toast('a capture is recording - press E to end it first'); return }
948
+ if (sessionEnding) { useStore.getState().toast('the session is being wrapped up - a moment'); return }
949
+ begin(reviewing, undefined, session)
950
+ },
951
+ end() {
952
+ const id = get().current
953
+ if (!id) return
954
+ set({ current: null, lastEnded: id })
955
+ cancelDwell()
956
+ drain(id)
957
+ unwatch?.(); unwatch = null
958
+ },
959
+ drop() {
960
+ const id = get().current
961
+ if (!id) return
962
+ broadcast({ type: 'sh:capture', id, on: false })
963
+ cancelDwell()
964
+ voiceDrop(id)
965
+ releaseOpen(id, true)
966
+ // a resumed capture dropped: its draft is still there, as it was
967
+ const was = get().caps[id]?.resumes?.id
968
+ set((s) => { const caps = { ...s.caps }; delete caps[id]; return { current: null, caps, noteOpen: false, noteDraft: '', tick: s.tick + 1, ...(was ? { resuming: s.resuming.filter((x) => x !== was) } : {}) } })
969
+ unwatch?.(); unwatch = null
970
+ },
971
+ note(text) {
972
+ const id = get().current
973
+ const t = text.trim()
974
+ if (!id || !t) return
975
+ addStep(id, { at: now(), event: 'note', text: t })
976
+ set({ noteDraft: '' })
977
+ },
978
+ setNoteOpen(open) { set({ noteOpen: open && !!get().current }) },
979
+ setNoteDraft(text) { set({ noteDraft: text }) },
980
+ picture(frame) {
981
+ const id = get().current
982
+ if (!id) return
983
+ const s = useStore.getState()
984
+ if (s.play) { shoot(id, frame ?? s.play.at, 'key'); return } // in play: the screen on stage
985
+ const key = pointerAt && performance.now() - pointerAt.at < 5_000 ? pointerAt.key : s.selection[s.selection.length - 1]
986
+ const f = frame ?? (key ? frameIdOf(key) : undefined)
987
+ if (f) shoot(id, f, 'key', key)
988
+ },
989
+ }))
990
+
991
+ /** What the capture tile shows: the timer and the counts. */
992
+ export function captureCounts(c: Cap | undefined, t = now()): { time: string; counts: string; notes: number } {
993
+ if (!c) return { time: '0:00', counts: '', notes: 0 }
994
+ const sec = Math.max(0, Math.floor((t - c.t0) / 1000))
995
+ const steps = c.steps.filter((x) => ['select', 'device', 'theme', 'copied', 'interact'].includes(x.event)).length
996
+ const frames = new Set([...(c.inherited ?? []), ...touchedFrames(c.resumes ? c.steps.filter((x) => x.at >= c.resumes!.at) : c.steps, undefined, !c.resumes)]).size
997
+ const plural = (n: number, w: string) => (n ? `${n} ${w}${n === 1 ? '' : 's'}` : '')
998
+ return {
999
+ time: `${Math.floor(sec / 60)}:${String(sec % 60).padStart(2, '0')}`,
1000
+ counts: [plural(c.points.length, 'point'), plural(c.pictures.length, 'picture'), plural(frames, 'frame'), plural(steps, 'step')].filter(Boolean).join(' · '),
1001
+ notes: c.steps.filter((x) => x.event === 'note').length,
1002
+ }
1003
+ }
1004
+
1005
+ if (typeof window !== 'undefined' && (import.meta as any).env?.DEV) { (window as any).__mvJamCapture = useJamCapture; (window as any).__mvJamInstances = instances }