@lincyaw/workbench 0.2.0-rc.42 → 0.2.0-rc.44

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lincyaw/workbench",
3
- "version": "0.2.0-rc.42",
3
+ "version": "0.2.0-rc.44",
4
4
  "description": "Presmium's workbench panels in React (the documents, a document edited on its rendered page with comments and version history) with the workspace service's client and the preview protocol's workbench side; @lincyaw/dsh places them in presmium studio (experimental)",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -55,8 +55,8 @@
55
55
  "@tiptap/pm": "3.32.0",
56
56
  "katex": "0.18.10",
57
57
  "react-resizable-panels": "4.14.5",
58
- "@lincyaw/design": "1.0.0-rc.42",
59
- "@lincyaw/kernel": "1.0.0-rc.42"
58
+ "@lincyaw/kernel": "1.0.0-rc.44",
59
+ "@lincyaw/design": "1.0.0-rc.44"
60
60
  },
61
61
  "peerDependencies": {
62
62
  "react": "^18.3.1",
@@ -67,6 +67,6 @@
67
67
  "@types/react-dom": "18.3.7",
68
68
  "react": "^18.3.1",
69
69
  "react-dom": "^18.3.1",
70
- "@lincyaw/compiler": "1.0.0-rc.42"
70
+ "@lincyaw/compiler": "1.0.0-rc.44"
71
71
  }
72
72
  }
@@ -10,6 +10,21 @@ const MAX_BYTES = 16 << 20
10
10
  const MAX_DURATION_MS = 10 * 60_000
11
11
  const MIME_TYPES = ['audio/webm;codecs=opus', 'audio/ogg;codecs=opus', 'audio/mp4']
12
12
 
13
+ /** getUserMedia has no AbortSignal; late permission grants must release their stream. */
14
+ function recordingWait<T>(work: Promise<T>, signal: AbortSignal | undefined, releaseLate: (value: T) => void): Promise<T> {
15
+ if (!signal) return work
16
+ return new Promise((resolve, reject) => {
17
+ const abort = () => { signal.removeEventListener('abort', abort); reject(signal.reason ?? new Error('recording-cancelled')) }
18
+ signal.addEventListener('abort', abort, { once: true })
19
+ work.then(value => {
20
+ signal.removeEventListener('abort', abort)
21
+ if (signal.aborted) { releaseLate(value); return }
22
+ resolve(value)
23
+ }, error => { signal.removeEventListener('abort', abort); reject(error) })
24
+ if (signal.aborted) abort()
25
+ })
26
+ }
27
+
13
28
  export function recordingUnavailable(): string | undefined {
14
29
  if (!globalThis.isSecureContext || !navigator.mediaDevices?.getUserMedia) return 'recording-context'
15
30
  if (typeof MediaRecorder === 'undefined' || !MIME_TYPES.some(type => MediaRecorder.isTypeSupported(type))) return 'recording-unsupported'
@@ -36,6 +51,7 @@ export class AudioRecording {
36
51
  #generation = 0
37
52
  #disposed = false
38
53
  #take?: Take
54
+ #request?: AbortController
39
55
  #release?: () => void
40
56
  #warning = false
41
57
  readonly #beforeUnload = (event: BeforeUnloadEvent) => { event.preventDefault(); event.returnValue = '' }
@@ -57,19 +73,24 @@ export class AudioRecording {
57
73
  this.#set({ phase: error ? 'failed' : this.#state.candidate ? 'ready' : 'idle', candidate: this.#state.candidate, elapsedMs: this.#state.candidate?.elapsedMs ?? 0, ...error ? { error } : {} })
58
74
  }
59
75
 
60
- async start(prepare: () => Promise<() => void>): Promise<void> {
76
+ async start(prepare: (signal: AbortSignal) => Promise<() => void>, signal?: AbortSignal): Promise<void> {
77
+ if (signal?.aborted) return
61
78
  if (this.#disposed || this.#take || this.#state.phase === 'requesting') return
62
79
  const unavailable = recordingUnavailable()
63
80
  if (unavailable) { this.#settle(unavailable); return }
64
81
  const generation = ++this.#generation
82
+ const request = this.#request = new AbortController()
83
+ const pendingSignal = signal ? AbortSignal.any([signal, request.signal]) : request.signal
84
+ const abort = () => { if (generation === this.#generation) this.cancelTake() }
85
+ signal?.addEventListener('abort', abort, { once: true })
65
86
  this.#set({ ...this.#state, phase: 'requesting', error: undefined })
66
87
  let stream: MediaStream | undefined
67
88
  let release: (() => void) | undefined
68
89
  try {
69
- release = await prepare()
90
+ release = await recordingWait(prepare(pendingSignal), pendingSignal, release => release())
70
91
  if (generation !== this.#generation) { release(); return }
71
92
  this.#release = release
72
- stream = await navigator.mediaDevices.getUserMedia({ audio: { channelCount: { ideal: 1 } }, video: false })
93
+ stream = await recordingWait(navigator.mediaDevices.getUserMedia({ audio: { channelCount: { ideal: 1 } }, video: false }), pendingSignal, stream => stream.getTracks().forEach(track => track.stop()))
73
94
  if (generation !== this.#generation) { stream.getTracks().forEach(track => track.stop()); return }
74
95
  const mimeType = MIME_TYPES.find(type => MediaRecorder.isTypeSupported(type))!
75
96
  const recorder = new MediaRecorder(stream, { mimeType, audioBitsPerSecond: 128_000 })
@@ -102,13 +123,13 @@ export class AudioRecording {
102
123
  this.#take = undefined
103
124
  const name = error instanceof DOMException ? error.name : ''
104
125
  this.#settle(name === 'NotAllowedError' || name === 'SecurityError' ? 'recording-permission' : name === 'NotFoundError' ? 'recording-device' : name === 'NotReadableError' ? 'recording-device-busy' : name === 'NotSupportedError' ? 'recording-device-unsupported' : 'recording-failed')
105
- }
126
+ } finally { signal?.removeEventListener('abort', abort); if (this.#request === request) this.#request = undefined }
106
127
  }
107
128
 
108
129
  stop(error?: string): void {
109
130
  const take = this.#take
110
131
  if (!take) {
111
- if (this.#state.phase === 'requesting') { this.#generation++; this.#release?.(); this.#release = undefined; this.#settle(error) }
132
+ if (this.#state.phase === 'requesting') { this.#generation++; this.#request?.abort(new Error('recording-cancelled')); this.#request = undefined; this.#release?.(); this.#release = undefined; this.#settle(error) }
112
133
  return
113
134
  }
114
135
  if (error) take.error ??= error
@@ -159,6 +180,7 @@ export class AudioRecording {
159
180
  if (this.#disposed) return
160
181
  this.#disposed = true
161
182
  this.#generation++
183
+ this.#request?.abort(new Error('recording-cancelled')); this.#request = undefined
162
184
  const take = this.#take
163
185
  if (take) { take.discard = true; this.stop(); this.#finish(take) }
164
186
  this.#release?.(); this.#release = undefined
@@ -5,7 +5,7 @@ import type { AuthoringClientChannel } from './workspace-client.ts'
5
5
 
6
6
  const sameSource = (a: CanonicalSource, b: CanonicalSource) => a.documentId === b.documentId && a.version === b.version && a.fingerprint === b.fingerprint
7
7
  // Clock samples are observations, not user input revisions. Never push at animation-frame frequency.
8
- const signature = (value: AuthoringClientSnapshot) => JSON.stringify({ ...value, revision: 0, playback: value.playback && { ...value.playback,
8
+ const signature = (value: AuthoringClientSnapshot) => JSON.stringify({ ...value, revision: 0, recording: value.recording && { ...value.recording, elapsedMs: 0 }, playback: value.playback && { ...value.playback,
9
9
  position: value.playback.position && { ...value.playback.position, timeMs: 0, stepTimeMs: 0 } } })
10
10
  type Attachment = { directory: string, identity: string, epoch: string, channel: AuthoringClientChannel, connectionId?: string }
11
11
 
@@ -71,7 +71,7 @@ export function connectAuthoringClient(workbench: Workbench): () => void {
71
71
  if (value) publishState(owner, event.connectionId, value)
72
72
  return
73
73
  }
74
- if (event.type === 'cancel') { if (event.connectionId === owner.connectionId && event.requestId === active?.requestId) active.controller.abort(); return }
74
+ if (event.type === 'cancel') { if (event.connectionId === owner.connectionId && event.requestId === active?.requestId) active.controller.abort(new Error('client-request-cancelled')); return }
75
75
  const request = event.request, connectionId = owner.connectionId
76
76
  if (!connectionId || request.clientId !== clientId || request.epoch !== owner.epoch || request.expectedClientState.connectionId !== connectionId || seen.has(request.requestId)) return
77
77
  seen.add(request.requestId)
@@ -110,7 +110,7 @@ export function connectAuthoringClient(workbench: Workbench): () => void {
110
110
  } catch (error) {
111
111
  const result = snapshot()
112
112
  const code = error instanceof Error && /^(client|production)-[a-z-]{1,100}$/.test(error.message) ? error.message : 'client-action-not-verified'
113
- if (!controller.signal.aborted && attached === owner && owner.connectionId === connectionId && result) await send(owner, connectionId, result, { requestId: request.requestId, state: 'failed', error: { code } }).catch(() => {})
113
+ if (attached === owner && owner.connectionId === connectionId && result) await send(owner, connectionId, result, { requestId: request.requestId, state: 'failed', error: { code } }).catch(() => {})
114
114
  } finally { clearTimeout(timeout); if (active === task) active = undefined }
115
115
  }
116
116
  const changed = () => {
@@ -55,6 +55,7 @@ export function DocumentView({ active = true, framed = false, onBack, actions, h
55
55
  const [commentsPortal, setCommentsPortal] = useState<HTMLDivElement | null>(null)
56
56
  const t = useMessages()
57
57
  const document = useWorkbenchState(state => state.document)
58
+ const sourceIdentity = useWorkbenchState(state => state.authoring?.identity)
58
59
  const view = useWorkbenchState(state => state.webView)
59
60
  const previousView = useRef({ document, workspace, view, active })
60
61
  const currentScope = useRef({ document, active })
@@ -72,7 +73,7 @@ export function DocumentView({ active = true, framed = false, onBack, actions, h
72
73
  setNavigationDocument(document)
73
74
  if (navigation && typeof matchMedia === 'function' && !matchMedia(ROOMY).matches) setNavigation(false)
74
75
  }
75
- const compilePresentation = async (ready?: () => void, forReading = false): Promise<void> => {
76
+ const compilePresentation = async (ready?: () => void, forReading = false, signal?: AbortSignal, deadline?: number): Promise<void> => {
76
77
  if (preparing.current || workbench.getState().isPreparingPresentation) return
77
78
  preparing.current = true
78
79
  const generation = ++transition.current, identity = workbench.getState().authoring?.identity
@@ -80,34 +81,39 @@ export function DocumentView({ active = true, framed = false, onBack, actions, h
80
81
  setChangingView(true)
81
82
  const current = () => generation === transition.current && currentScope.current.active && currentScope.current.document === document && workbench.getState().authoring?.identity === identity
82
83
  try {
83
- const prepared = await workbench.preparePresentation()
84
+ const prepared = await workbench.preparePresentation(signal, deadline)
85
+ signal?.throwIfAborted()
84
86
  if (!current()) return
85
87
  if (prepared) ready?.()
86
88
  else setViewFailure(forReading ? t.readingFailed : workbench.getState().presentationFailure)
87
89
  } catch { if (current()) setViewFailure(forReading ? t.readingFailed : t.presentationCompileFailed) }
88
90
  finally { preparing.current = false; if (current()) setChangingView(false) }
89
91
  }
90
- const openProduction = async () => {
92
+ const openProduction = async (signal?: AbortSignal) => {
91
93
  if (changingView || workbench.client.capabilities?.production === false) return
92
94
  const generation = ++transition.current, identity = workbench.getState().authoring?.identity
93
95
  const current = () => generation === transition.current && currentScope.current.active && currentScope.current.document === document && workbench.getState().authoring?.identity === identity
94
96
  setChangingView(true); setViewFailure(undefined)
95
97
  try {
98
+ signal?.throwIfAborted()
96
99
  await workbench.pausePlayback()
97
- await workbench.flushPendingEdits()
100
+ await workbench.flushPendingEdits(signal)
101
+ signal?.throwIfAborted()
98
102
  if (current()) { workbench.setMode('editing'); setWorkspace('production') }
99
103
  } catch { if (current()) setViewFailure(t.presentationPending) }
100
104
  finally { if (current()) setChangingView(false) }
101
105
  }
102
- const exitPresent = (): Promise<void> => {
106
+ const exitPresent = (signal?: AbortSignal): Promise<void> => {
103
107
  if (exiting.current) return exiting.current
104
108
  const generation = ++transition.current, identity = workbench.getState().authoring?.identity
105
109
  setChangingView(true); setViewFailure(undefined)
106
110
  const current = () => generation === transition.current && currentScope.current.active && currentScope.current.document === document && workbench.getState().authoring?.identity === identity
107
111
  const work = Promise.resolve().then(async () => {
108
112
  try {
113
+ signal?.throwIfAborted()
109
114
  // The child flushes its latest position before acknowledging this pause.
110
115
  await workbench.pausePlayback()
116
+ signal?.throwIfAborted()
111
117
  if (!current()) return
112
118
  setWorkspace('document')
113
119
  workbench.setMode('editing')
@@ -120,6 +126,40 @@ export function DocumentView({ active = true, framed = false, onBack, actions, h
120
126
  exiting.current = work
121
127
  return work
122
128
  }
129
+ const controls = useRef({ workspace, changingView, viewFailure, generation: transition.current, compilePresentation, openProduction, exitPresent, setWorkspace })
130
+ controls.current = { workspace, changingView, viewFailure, generation: transition.current, compilePresentation, openProduction, exitPresent, setWorkspace }
131
+ useEffect(() => {
132
+ if (!active || !sourceIdentity) return
133
+ return workbench.registerAuthoringClientControls({ identity: sourceIdentity,
134
+ actions: () => {
135
+ const state = workbench.getState(), source = state.authoring
136
+ return currentScope.current.active && source?.identity === sourceIdentity && !controls.current.changingView
137
+ && !source.loading && !source.dirty && !source.saving && !source.composing && !source.restored && !source.error && !isAgentChanging(state)
138
+ && !state.isCreatingPage && !state.isEditingPage && !state.isHistoryBusy && !state.isHistoryPreparing ? ['view.set'] : []
139
+ },
140
+ snapshot: () => ({ mode: controls.current.workspace }),
141
+ execute: async (action, _source, signal, deadline) => {
142
+ if (action.type !== 'view.set') throw new Error('client-action-unavailable')
143
+ if (preparing.current || controls.current.changingView) throw new Error('client-action-unavailable')
144
+ if (action.view !== controls.current.workspace) {
145
+ if (action.view === 'document') await controls.current.exitPresent(signal)
146
+ else if (action.view === 'production') await controls.current.openProduction(signal)
147
+ else await controls.current.compilePresentation(() => {
148
+ workbench.setMode('viewing'); workbench.setWebView(action.view === 'presentation' ? 'present' : 'document')
149
+ controls.current.setWorkspace(action.view)
150
+ }, action.view === 'reading', signal, deadline)
151
+ }
152
+ const generation = transition.current
153
+ await workbench.waitForAuthoringClientState(() => {
154
+ if (!currentScope.current.active || workbench.getState().authoring?.identity !== sourceIdentity) throw new Error('client-document-changed')
155
+ if (generation !== transition.current) throw new Error('client-view-changed')
156
+ if (controls.current.generation === generation && controls.current.viewFailure && controls.current.workspace !== action.view) throw new Error('client-view-not-verified')
157
+ return controls.current.workspace === action.view && !controls.current.changingView
158
+ }, signal)
159
+ },
160
+ })
161
+ }, [workbench, active, sourceIdentity])
162
+ useEffect(() => workbench.refreshAuthoringClientControls(), [workbench, active, sourceIdentity, workspace, changingView])
123
163
  useEffect(() => {
124
164
  transition.current++; exiting.current = undefined
125
165
  setWorkspace('document')
@@ -244,7 +244,7 @@ export function ProductionPanel({ onClose, closing, active = true }: { onClose()
244
244
  </div></PmTabPanel>
245
245
  <PmTabPanel value="voice" keepMounted><div className="pmw-production__fields">
246
246
  <DocumentVoiceField key={`${source.identity}:${stepId}`} stepId={stepId} disabled={blocked || closing} onGenerate={() => { voicePlayback.stop(); workbench.stopAuditions(); setGenerateStep(stepId) }} />
247
- <ProductionRecording key={source.identity} identity={source.identity} sectionId={sectionId} stepId={stepId} active={!outputsOpen && (wide || tab === 'properties') && inspectorTab === 'voice'} disabled={blocked || closing}
247
+ <ProductionRecording key={source.identity} identity={source.identity} sectionId={sectionId} stepId={stepId} active={!outputsOpen && (wide || tab === 'properties') && inspectorTab === 'voice'} available={active && !outputsOpen} onReveal={() => { setInspectorTab('voice'); setTab('properties') }} disabled={blocked || closing}
248
248
  onImportStarted={() => transcriptionSelection.current}
249
249
  onImported={(assetId, selection) => {
250
250
  setMediaRefresh(true)
@@ -8,13 +8,14 @@ import { AUDIO_RECORDING_MESSAGES } from '../audio-recording-messages.ts'
8
8
  import { PmButton, PmCard, PmDialog, PmNotice, PmSelect } from '../components.tsx'
9
9
  import { useWorkbench, useWorkbenchState } from '../context.tsx'
10
10
  import { mediaFailure } from '../media-messages.ts'
11
+ import type { AuthoringClientActionType } from '@lincyaw/kernel'
11
12
  import { ProductionRecordingRange } from './ProductionRecordingRange.tsx'
12
13
 
13
- interface Props { identity: string, sectionId: string, stepId: string, active: boolean, disabled: boolean, onImportStarted(): number, onImported(assetId: string, selection: number): void, onAdopted(): void }
14
+ interface Props { identity: string, sectionId: string, stepId: string, active: boolean, available: boolean, disabled: boolean, onReveal(): void, onImportStarted(): number, onImported(assetId: string, selection: number): void, onAdopted(): void }
14
15
  const elapsed = (ms: number) => `${String(Math.floor(ms / 60000)).padStart(2, '0')}:${String(Math.floor(ms / 1000) % 60).padStart(2, '0')}`
15
16
 
16
17
  /** Video-only microphone material. The document actor and existing binary transaction still own every save. */
17
- export function ProductionRecording({ identity, sectionId, stepId, active, disabled, onImportStarted, onImported, onAdopted }: Props) {
18
+ export function ProductionRecording({ identity, sectionId, stepId, active, available, disabled, onReveal, onImportStarted, onImported, onAdopted }: Props) {
18
19
  const workbench = useWorkbench(), locale = useWorkbenchState(state => state.locale), t = AUDIO_RECORDING_MESSAGES[locale]
19
20
  const source = useWorkbenchState(state => state.authoring)
20
21
  const [recording] = useState(() => documentRecording(workbench, identity))
@@ -36,14 +37,15 @@ export function ProductionRecording({ identity, sectionId, stepId, active, disab
36
37
  if (!active) { recording.stop(); audition.current?.stop() }
37
38
  if (candidate?.assetId && source?.identity === identity && source.content && !source.content.assets[candidate.assetId]) recording.forgetImport(candidate.assetId)
38
39
  }, [active, candidate?.assetId, source?.content, source?.identity, identity, recording])
39
- const start = () => {
40
+ const start = (signal?: AbortSignal) => {
40
41
  setFailure(undefined)
41
- void recording.start(async () => {
42
+ return recording.start(async signal => {
42
43
  workbench.stopAuditions()
43
44
  await workbench.pausePlayback(true)
45
+ signal.throwIfAborted()
44
46
  if (!current() || !active) throw new Error('recording-failed')
45
47
  return workbench.registerAudioCapture(() => recording.stop())
46
- })
48
+ }, signal)
47
49
  }
48
50
  const listen = () => {
49
51
  if (!candidate) return
@@ -54,9 +56,12 @@ export function ProductionRecording({ identity, sectionId, stepId, active, disab
54
56
  if (listening.status === 'playing' || listening.status === 'loading') audition.current.stop()
55
57
  else void audition.current.play(async () => ({ audio: candidate.file }))
56
58
  }
57
- const save = async () => {
59
+ const save = async (signal?: AbortSignal) => {
58
60
  if (!candidate || saving || !current()) return
59
61
  const file = candidate.file, selection = adoption.revision(), transcriptionSelection = onImportStarted(), controller = new AbortController()
62
+ const abort = () => controller.abort(signal?.reason)
63
+ signal?.addEventListener('abort', abort, { once: true })
64
+ if (signal?.aborted) abort()
60
65
  request.current = controller; setSaving(true); setFailure(undefined)
61
66
  const owns = () => current() && !controller.signal.aborted && request.current === controller && recording.snapshot().candidate?.file === file
62
67
  try {
@@ -70,9 +75,81 @@ export function ProductionRecording({ identity, sectionId, stepId, active, disab
70
75
  } catch (error) {
71
76
  if (owns()) setFailure(`${t.saveFailed} ${mediaFailure(error, locale)}`)
72
77
  } finally {
78
+ signal?.removeEventListener('abort', abort)
73
79
  if (request.current === controller) { request.current = undefined; if (current()) setSaving(false) }
74
80
  }
75
81
  }
82
+ const controls = useRef({ start, save, disabled, saving, active, available, onReveal })
83
+ controls.current = { start, save, disabled, saving, active, available, onReveal }
84
+ useEffect(() => {
85
+ if (!available) return
86
+ const owns = () => current() && controls.current.available && workbench.getState().authoring?.identity === identity
87
+ return workbench.registerAuthoringClientControls({ identity,
88
+ actions: () => {
89
+ if (!owns()) return []
90
+ const actual = recording.snapshot(), take = actual.candidate, player = audition.current?.snapshot()
91
+ const capturing = ['requesting', 'recording', 'stopping'].includes(actual.phase)
92
+ const source = workbench.getState().authoring, clean = source && !source.loading && !source.dirty && !source.saving && !source.composing && !source.restored && !source.error
93
+ const actions: AuthoringClientActionType[] = []
94
+ if (capturing) actions.push('recording.cancel', 'recording.stop')
95
+ if (player?.status === 'playing' || player?.status === 'loading') actions.push('recording.pause')
96
+ if (!controls.current.disabled && !controls.current.saving && clean) {
97
+ if (!capturing && !recordingUnavailable()) actions.push('recording.start')
98
+ if (take && !capturing) actions.push('recording.discard', 'recording.listen', ...!take.assetId ? ['recording.save' as const] : [])
99
+ }
100
+ return actions
101
+ },
102
+ snapshot: () => {
103
+ const actual = recording.snapshot(), take = actual.candidate, player = audition.current?.snapshot()
104
+ return { recording: { phase: actual.phase, elapsedMs: actual.elapsedMs,
105
+ ...take ? { candidate: { byteLength: take.file.size, mime: take.file.type, ...take.assetId ? { assetId: take.assetId } : {} } } : {},
106
+ ...actual.error ? { error: actual.error } : {}, audition: player?.status ?? 'idle' } }
107
+ },
108
+ execute: async (action, _source, signal) => {
109
+ const owned = () => { signal.throwIfAborted(); if (!owns()) throw new Error('client-recording-unavailable') }
110
+ owned()
111
+ if (!controls.current.active) {
112
+ controls.current.onReveal()
113
+ await workbench.waitForAuthoringClientState(() => { owned(); return controls.current.active }, signal)
114
+ }
115
+ if (action.type === 'recording.start') {
116
+ await controls.current.start(signal); owned()
117
+ if (recording.snapshot().phase !== 'recording') throw new Error(`client-${recording.snapshot().error ?? 'recording-unavailable'}`)
118
+ return
119
+ }
120
+ if (action.type === 'recording.stop' || action.type === 'recording.cancel') {
121
+ action.type === 'recording.cancel' ? recording.cancelTake() : recording.stop()
122
+ await workbench.waitForAuthoringClientState(() => { owned(); return !['requesting', 'recording', 'stopping'].includes(recording.snapshot().phase) }, signal)
123
+ return
124
+ }
125
+ if (action.type === 'recording.discard') {
126
+ audition.current?.stop(); recording.discard(); setFailure(undefined)
127
+ return
128
+ }
129
+ if (action.type === 'recording.pause') { audition.current?.stop(); return }
130
+ const take = recording.snapshot().candidate
131
+ if (!take) throw new Error('client-recording-unavailable')
132
+ if (action.type === 'recording.save') {
133
+ await controls.current.save(signal); owned()
134
+ if (recording.snapshot().candidate?.file !== take.file || !recording.snapshot().candidate?.assetId) throw new Error('client-recording-save-unverified')
135
+ return
136
+ }
137
+ if (action.type === 'recording.listen') {
138
+ if (!audition.current) {
139
+ const player = audition.current = new Audition(workbench)
140
+ player.subscribe(() => { if (current()) setListening(player.snapshot()) })
141
+ }
142
+ const player = audition.current, stop = () => player.stop()
143
+ signal.addEventListener('abort', stop, { once: true })
144
+ try { await player.play(async () => ({ audio: take.file })); owned(); if (player.snapshot().status !== 'playing') throw new Error('client-recording-listen-unverified') }
145
+ finally { signal.removeEventListener('abort', stop) }
146
+ return
147
+ }
148
+ throw new Error('client-action-unavailable')
149
+ },
150
+ })
151
+ }, [workbench, identity, available, recording])
152
+ useEffect(() => workbench.refreshAuthoringClientControls(), [workbench, state.phase, state.error, candidate?.file, candidate?.assetId, saving, disabled, active, available, listening.status])
76
153
  return <PmCard className="pmw-audio-recording" data-recording={state.phase}>
77
154
  <strong>{t.title}</strong>
78
155
  {status && !(status === t.imported && adoption.adopted && candidate?.assetId === adoption.selected) && <span className="pm-ui-hint"><span role="status">{saving ? t.saving : status}</span>{state.phase === 'recording' && <span aria-hidden="true"> · {elapsed(state.elapsedMs)}</span>}</span>}
@@ -80,12 +157,12 @@ export function ProductionRecording({ identity, sectionId, stepId, active, disab
80
157
  {failure && <PmNotice tone="danger">{failure}</PmNotice>}
81
158
  {listening.status === 'failed' && <PmNotice tone="warn">{t.failures[listening.error ?? 'audition-request'] ?? t.failures['audition-request']}</PmNotice>}
82
159
  <div className="pmw-voice__actions">
83
- {!capturing && !candidate && <PmButton small variant="primary" disabled={disabled || saving || !active || Boolean(unavailable)} onClick={start}>{t.start}</PmButton>}
160
+ {!capturing && !candidate && <PmButton small variant="primary" disabled={disabled || saving || !active || Boolean(unavailable)} onClick={() => void start()}>{t.start}</PmButton>}
84
161
  {state.phase === 'requesting' && <PmButton small onClick={() => recording.cancelTake()}>{t.cancel}</PmButton>}
85
162
  {state.phase === 'recording' && <><PmButton small onClick={() => recording.stop()}>{t.stop}</PmButton><PmButton small onClick={() => recording.cancelTake()}>{t.cancelTake}</PmButton></>}
86
163
  {candidate && !capturing && <PmButton small disabled={!active || saving} loading={listening.status === 'loading'} onClick={listen}>{listening.status === 'playing' ? t.stopAudition : t.audition}</PmButton>}
87
164
  {candidate && !candidate.assetId && !capturing && <PmButton small variant="primary" disabled={disabled || !active} loading={saving} onClick={() => void save()}>{failure ? t.retry : t.save}</PmButton>}
88
- {candidate && !capturing && <PmButton small disabled={disabled || saving || !active || Boolean(unavailable)} onClick={start}>{t.rerecord}</PmButton>}
165
+ {candidate && !capturing && <PmButton small disabled={disabled || saving || !active || Boolean(unavailable)} onClick={() => void start()}>{t.rerecord}</PmButton>}
89
166
  {saving && <PmButton small onClick={() => { request.current?.abort(); setSaving(false) }}>{t.cancelSave}</PmButton>}
90
167
  {candidate && !capturing && !saving && <PmButton small onClick={() => setDiscard(true)}>{t.discard}</PmButton>}
91
168
  </div>
@@ -40,9 +40,10 @@ function RecordingSlot({ label, hint, value, disabled, onChange }: { label: stri
40
40
  const capturing = ['requesting', 'recording', 'stopping'].includes(state.phase)
41
41
  const start = () => {
42
42
  setFailure(undefined)
43
- void recording.start(async () => {
43
+ void recording.start(async signal => {
44
44
  workbench.stopAuditions()
45
45
  await workbench.pausePlayback()
46
+ signal.throwIfAborted()
46
47
  return workbench.registerAudioCapture(() => recording.stop())
47
48
  })
48
49
  }
package/src/workbench.ts CHANGED
@@ -2,7 +2,7 @@ import { DocumentProduction } from './document-production.ts'
2
2
  import { DocumentEditing } from './document-editing.ts'
3
3
  import { isAgentChanging } from './workbench-state.ts'
4
4
  import type { AgentAnswer, AgentDraft, AgentRequest, PlaybackKey, Pointer, PreviewSink, WebView, WorkbenchMode, WorkbenchState, WorkbenchStore } from './workbench-state.ts'
5
- import { randomUUID } from '@lincyaw/kernel'
5
+ import { enableDocumentProduction, randomUUID } from '@lincyaw/kernel'
6
6
  import type { ProjectOperation, ProjectPlan } from '@lincyaw/kernel'
7
7
  import { CommentDraftBank } from './document-editor/comment-drafts.ts'
8
8
  import { ComponentPropertyDrafts } from './component-property-drafts.ts'
@@ -41,10 +41,17 @@ import { ProductionPlayback } from './production-playback.ts'
41
41
  import type { ProductionBinding, ProductionPlan, ProductionPlaybackCommand, ProductionPlaybackState } from '@lincyaw/kernel'
42
42
  import { workspaceClient, WorkspaceServerError } from './workspace-client.ts'
43
43
  import type { WorkspaceClient } from './workspace-client.ts'
44
- import type { AuthoringClientAction, AuthoringClientActionResult, AuthoringClientSnapshot } from '@lincyaw/kernel'
44
+ import type { AuthoringClientAction, AuthoringClientActionType, AuthoringClientActionResult, AuthoringClientSnapshot } from '@lincyaw/kernel'
45
45
  import { PRODUCTION_PLAYBACK_RATES } from '@lincyaw/kernel'
46
46
 
47
47
  type Listener = () => void
48
+ /** Mounted native controls advertise their own availability and execute the same human actions. */
49
+ export interface AuthoringClientControls {
50
+ identity: string
51
+ actions(): readonly AuthoringClientActionType[]
52
+ snapshot?(): Pick<AuthoringClientSnapshot, 'mode' | 'recording'>
53
+ execute(action: AuthoringClientAction, source: CanonicalSource, signal: AbortSignal, deadline?: number): Promise<void>
54
+ }
48
55
  const sameSource = (a: CanonicalSource, b: CanonicalSource) => a.documentId === b.documentId && a.version === b.version && a.fingerprint === b.fingerprint
49
56
 
50
57
  // The export of the lecture as a video (`@lincyaw/addon-lecture`).
@@ -109,6 +116,9 @@ export class Workbench {
109
116
  readonly editing: DocumentEditing
110
117
  #state: WorkbenchState
111
118
  readonly #listeners = new Set<Listener>()
119
+ readonly #clientControls = new Set<AuthoringClientControls>()
120
+ readonly #clientControlListeners = new Set<Listener>()
121
+ #clientControlRevision = 0
112
122
  readonly #commentDrafts = new CommentDraftBank()
113
123
  readonly #componentPropertyDrafts = new ComponentPropertyDrafts()
114
124
  readonly #positionSeed = Array.from(crypto.getRandomValues(new Uint32Array(2)), value => value.toString(36)).join('-')
@@ -280,11 +290,31 @@ export class Workbench {
280
290
  /** Only counters enter passive client state; no new form payload is exposed to MCP. */
281
291
  authoringDraftRevision(): string {
282
292
  const source = this.#state.authoring
283
- return JSON.stringify([this.authoringCommentDraftRevision(), source?.identity && source.member?.id ? this.#componentPropertyDrafts.revision(source.identity, source.member.id) : 0])
293
+ return JSON.stringify([this.authoringCommentDraftRevision(), source?.identity && source.member?.id ? this.#componentPropertyDrafts.revision(source.identity, source.member.id) : 0, this.#clientControlRevision])
284
294
  }
285
295
  subscribeAuthoringDraftChanges(listener: () => void): () => void {
286
296
  const comments = this.#commentDrafts.subscribe(listener), properties = this.#componentPropertyDrafts.subscribe(listener)
287
- return () => { comments(); properties() }
297
+ this.#clientControlListeners.add(listener)
298
+ return () => { comments(); properties(); this.#clientControlListeners.delete(listener) }
299
+ }
300
+
301
+ registerAuthoringClientControls(controls: AuthoringClientControls): () => void {
302
+ this.#clientControls.add(controls); this.refreshAuthoringClientControls()
303
+ return () => { this.#clientControls.delete(controls); this.refreshAuthoringClientControls() }
304
+ }
305
+ refreshAuthoringClientControls(): void {
306
+ this.#clientControlRevision++
307
+ for (const listener of this.#clientControlListeners) listener()
308
+ }
309
+ waitForAuthoringClientState(ready: () => boolean, signal: AbortSignal): Promise<void> {
310
+ return new Promise((resolve, reject) => {
311
+ const finish = (error?: unknown) => { stop(); stopControls(); signal.removeEventListener('abort', aborted); error ? reject(error) : resolve() }
312
+ const check = () => { try { signal.throwIfAborted(); if (ready()) finish() } catch (error) { finish(error) } }
313
+ const aborted = () => finish(signal.reason ?? new Error('client-request-expired'))
314
+ const stop = this.subscribe(check), stopControls = this.subscribeAuthoringDraftChanges(check)
315
+ signal.addEventListener('abort', aborted, { once: true })
316
+ check()
317
+ })
288
318
  }
289
319
 
290
320
  getState = (): WorkbenchState => this.#state
@@ -753,13 +783,21 @@ export class Workbench {
753
783
  const prepared = state.presentationSnapshot, actual = { documentId: content.id, version: source.version, fingerprint: source.fingerprint }
754
784
  const preparedCurrent = clean && prepared && prepared.version === source.version && prepared.fingerprint === source.fingerprint && prepared.project === (source.projectRevision ?? 0)
755
785
  && state.ir?.revision === prepared.revision && state.ir.doc.source?.version === source.version && state.ir.doc.source.fingerprint === source.fingerprint
786
+ const controls = [...this.#clientControls].filter(owner => owner.identity === source.identity)
787
+ const advertised = controls.flatMap(owner => owner.actions()), unique = advertised.filter((type, index) => advertised.indexOf(type) === index && advertised.lastIndexOf(type) === index)
788
+ const reported = controls.map(owner => owner.snapshot?.()).filter(value => value !== undefined)
789
+ const mode = reported.find(value => value.mode)?.mode, recording = reported.find(value => value.recording)?.recording
756
790
  return { revision, source: { documentId: content.id, version: source.version, fingerprint: source.fingerprint }, view: state.webView, editing,
791
+ ...mode ? { mode } : {}, ...recording ? { recording } : {},
757
792
  interaction: state.documentPositionToken,
758
793
  ...page && position ? { position: { sectionId: page.id, ...page.stepOrder[position.step] ? { stepId: page.stepOrder[position.step] } : {} } } : {},
759
794
  ...plan ? { binding: plan.binding } : {}, ...playback ? { playback } : {},
760
795
  preview: preparedCurrent ? { status: 'prepared', source: actual, irRevision: prepared.revision, projectRevision: prepared.project }
761
796
  : { status: state.isPreparingPresentation ? 'preparing' : state.presentationFailure ? 'unavailable' : 'unprepared' },
762
797
  actions: ['state.read', 'playback.pause', ...safe ? ['source.flush' as const] : [],
798
+ ...safe && state.mode === 'editing' && state.editorHistory?.undo ? ['native-history.undo' as const] : [],
799
+ ...safe && state.mode === 'editing' && state.editorHistory?.redo ? ['native-history.redo' as const] : [],
800
+ ...unique,
763
801
  ...commentDrafts?.readable ? ['comment-drafts.inspect' as const, 'comment-drafts.read' as const] : [],
764
802
  ...clean && !state.isHistoryBusy && !state.isHistoryPreparing && commentDrafts?.readable && commentDrafts.ownersKnown ? ['comment-drafts.discard' as const, ...state.webView === 'document' && commentDrafts.hasRestoreComposer() ? ['comment-drafts.restore' as const] : []] : [],
765
803
  ...source.member?.id && this.#componentPropertyDrafts.readable(source.identity, source.member.id) ? ['component-drafts.inspect' as const, 'component-drafts.read' as const] : [],
@@ -776,6 +814,19 @@ export class Workbench {
776
814
  const current = () => { owned(); const state = this.authoringClientSnapshot(0); if (!state || !sameSource(state.source, expected) || !state.actions.includes(action.type)) throw new Error('client-action-unavailable'); return state }
777
815
  current()
778
816
  if (action.type === 'state.read') return
817
+ if (action.type === 'native-history.undo' || action.type === 'native-history.redo') {
818
+ if (this.editing.nativeHistory(action.type === 'native-history.undo' ? 'undo' : 'redo') !== true) throw new Error('client-native-history-unavailable')
819
+ owned(); return
820
+ }
821
+ const controls = [...this.#clientControls].filter(owner => owner.identity === identity && owner.actions().includes(action.type))
822
+ if (controls.length) {
823
+ if (controls.length !== 1) throw new Error('client-action-ambiguous')
824
+ const control = controls[0]!
825
+ await control.execute(action, expected, signal, deadline)
826
+ owned()
827
+ if (!this.#clientControls.has(control)) throw new Error('client-action-unavailable')
828
+ return
829
+ }
779
830
  if (action.type === 'comment-drafts.inspect' || action.type === 'comment-drafts.read') {
780
831
  const drafts = identity ? this.documentCommentDrafts(identity) : undefined
781
832
  if (!drafts?.readable || !fits) throw new Error('client-comment-drafts-unavailable')
@@ -855,6 +906,7 @@ export class Workbench {
855
906
  || present && (!result || result.status !== 'ready' || result.position?.sectionId !== action.sectionId || result.position.stepId !== action.stepId || result.position.playing)) throw new Error('client-step-unverified')
856
907
  return
857
908
  }
909
+ if (action.type !== 'playback.play' && action.type !== 'playback.seek' && action.type !== 'playback.rate') throw new Error('client-action-unavailable')
858
910
  const plan = this.#state.productionPlan
859
911
  if (!plan || !sameProductionBinding(plan.binding, action.binding)) throw new Error('client-playback-binding-changed')
860
912
  if (action.type === 'playback.seek' && (plan.timing !== 'confirmed' || plan.durationMs === undefined || action.timeMs > plan.durationMs)) throw new Error('client-seek-out-of-range')
@@ -922,9 +974,7 @@ export class Workbench {
922
974
  await this.flushPendingEdits()
923
975
  const source = this.#state.authoring, document = this.getDocument(identity)
924
976
  if (scope !== this.#documentScope || !document || !source?.version || !source.fingerprint || source.dirty || source.saving || source.composing || source.restored || source.error || isAgentChanging(this.#state)) throw new Error('production-source-changed')
925
- const plugins = [...new Set([...(document.metadata.plugins ?? []), '@lincyaw/addon-lecture', '@lincyaw/addon-speech'])]
926
- const next = { ...document, metadata: { ...document.metadata, plugins, renderers: { ...document.metadata.renderers, lecture: document.metadata.renderers?.lecture ?? {} } } }
927
- this.#documentSession.record(next)
977
+ this.#documentSession.record(enableDocumentProduction(document))
928
978
  await this.flushPendingEdits()
929
979
  if (scope !== this.#documentScope || this.#state.authoring?.identity !== identity || this.#state.authoring.dirty || this.#state.authoring.saving || this.#state.authoring.error) throw new Error('production-source-changed')
930
980
  }
@@ -1493,5 +1543,6 @@ export class Workbench {
1493
1543
  this.production.dispose()
1494
1544
  disposeTranscriptionSessions(this.client)
1495
1545
  disposeDocumentRecordings(this)
1546
+ this.#clientControls.clear(); this.#clientControlListeners.clear()
1496
1547
  }
1497
1548
  }