@lincyaw/workbench 0.2.0-rc.12 → 0.2.0-rc.120

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 (124) hide show
  1. package/README.md +1 -1
  2. package/package.json +5 -4
  3. package/src/AgentActivityBar.tsx +25 -0
  4. package/src/MediaClipEditor.tsx +7 -6
  5. package/src/StepProductionEditor.tsx +16 -7
  6. package/src/agent-activity-messages.ts +14 -0
  7. package/src/agent-activity.ts +38 -0
  8. package/src/approvals.tsx +6 -1
  9. package/src/audio-recording-messages.ts +5 -5
  10. package/src/audio-recording.ts +27 -5
  11. package/src/authoring-client.ts +10 -10
  12. package/src/authoring-draft-fragment.ts +3 -1
  13. package/src/authoring-window.ts +100 -0
  14. package/src/base-controls.tsx +28 -7
  15. package/src/body-draft-actions.ts +60 -0
  16. package/src/caption-timing-drafts.ts +20 -3
  17. package/src/caption-timing-messages.ts +2 -2
  18. package/src/caption-timing.ts +2 -2
  19. package/src/component-draft-actions.ts +1 -1
  20. package/src/component-properties.ts +3 -3
  21. package/src/component-property-drafts.ts +6 -6
  22. package/src/components.tsx +43 -38
  23. package/src/container-layout.ts +21 -0
  24. package/src/context.tsx +5 -3
  25. package/src/diagnostics.ts +1 -1
  26. package/src/document-draft-store.ts +45 -18
  27. package/src/document-drafts.ts +30 -21
  28. package/src/document-editing.ts +485 -0
  29. package/src/document-editor/BlockNoteSurface.tsx +40 -14
  30. package/src/document-editor/CommentComposer.tsx +24 -0
  31. package/src/document-editor/ComponentMaterials.tsx +4 -10
  32. package/src/document-editor/ComponentPropertiesDialog.tsx +12 -12
  33. package/src/document-editor/DocumentBlockFallback.tsx +4 -3
  34. package/src/document-editor/DocumentBlockPreview.tsx +11 -4
  35. package/src/document-editor/DocumentComments.tsx +87 -17
  36. package/src/document-editor/DocumentEditor.tsx +66 -14
  37. package/src/document-editor/DocumentStepNarration.tsx +39 -27
  38. package/src/document-editor/NarrationEditor.tsx +55 -21
  39. package/src/document-editor/NewCommentDrafts.tsx +120 -37
  40. package/src/document-editor/PlaceComment.tsx +252 -0
  41. package/src/document-editor/agent-targets.ts +34 -0
  42. package/src/document-editor/blocknote-messages.ts +2 -0
  43. package/src/document-editor/comment-draft-actions.ts +40 -20
  44. package/src/document-editor/comment-draft-inspection.ts +2 -2
  45. package/src/document-editor/comment-drafts.ts +75 -27
  46. package/src/document-editor/comments.ts +34 -14
  47. package/src/document-editor/component-label.ts +11 -0
  48. package/src/document-editor/file-input.ts +2 -2
  49. package/src/document-editor/focus.ts +0 -2
  50. package/src/document-editor/inline.ts +1 -0
  51. package/src/document-editor/insertion-place.ts +60 -0
  52. package/src/document-editor/messages.ts +3 -3
  53. package/src/document-editor/narration-schema.tsx +12 -6
  54. package/src/document-editor/narration.ts +19 -3
  55. package/src/document-editor/page-body.ts +60 -0
  56. package/src/document-editor/popover.tsx +4 -2
  57. package/src/document-editor/projection.ts +21 -19
  58. package/src/document-editor/rich-text.ts +3 -2
  59. package/src/document-editor/selection.ts +13 -0
  60. package/src/document-editor/style.css +8 -3
  61. package/src/document-editor/table-identities.ts +2 -1
  62. package/src/document-preview.ts +3 -2
  63. package/src/document-production.ts +492 -0
  64. package/src/document-return-point.ts +2 -2
  65. package/src/document-session.ts +216 -67
  66. package/src/document-timing-drafts.ts +26 -6
  67. package/src/effective-settings.ts +12 -0
  68. package/src/history.ts +5 -5
  69. package/src/import-messages.ts +40 -0
  70. package/src/index.ts +11 -5
  71. package/src/jobs.ts +1 -1
  72. package/src/mcp-settings-messages.ts +39 -0
  73. package/src/media-drafts.ts +43 -1
  74. package/src/messages.ts +39 -18
  75. package/src/outline.ts +5 -5
  76. package/src/panels/DocumentImportDialog.tsx +59 -0
  77. package/src/panels/DocumentNarrationPanel.tsx +2 -2
  78. package/src/panels/DocumentView.tsx +106 -77
  79. package/src/panels/DocumentVoiceDialog.tsx +29 -12
  80. package/src/panels/DocumentVoiceField.tsx +3 -2
  81. package/src/panels/ExportPanel.tsx +2 -10
  82. package/src/panels/HistoryPanel.tsx +36 -10
  83. package/src/panels/HomePanel.tsx +12 -1
  84. package/src/panels/McpSettingsPanel.tsx +86 -0
  85. package/src/panels/PageDeleteDialog.tsx +4 -4
  86. package/src/panels/PageRenameDialog.tsx +1 -1
  87. package/src/panels/PreviewPanel.tsx +4 -7
  88. package/src/panels/ProductionCaptionSize.tsx +20 -0
  89. package/src/panels/ProductionCaptionTiming.tsx +12 -9
  90. package/src/panels/ProductionMediaClips.tsx +25 -9
  91. package/src/panels/ProductionPanel.tsx +21 -17
  92. package/src/panels/ProductionRecording.tsx +86 -9
  93. package/src/panels/ProductionRecordingRange.tsx +26 -23
  94. package/src/panels/ProductionTimeline.tsx +6 -1
  95. package/src/panels/ProductionTranscription.tsx +8 -6
  96. package/src/panels/SourceDocumentPanel.tsx +24 -40
  97. package/src/panels/SpeechSettingsPanel.tsx +2 -2
  98. package/src/panels/TranscriptionAdoption.tsx +42 -36
  99. package/src/panels/VoiceCloneDialog.tsx +2 -1
  100. package/src/panels/VoicePreviewDialog.tsx +1 -1
  101. package/src/panels/VoiceScopeField.tsx +2 -2
  102. package/src/panels/VoiceoverDialog.tsx +11 -10
  103. package/src/places.ts +1 -36
  104. package/src/preview-playback.ts +1 -1
  105. package/src/production-draft-actions.ts +37 -0
  106. package/src/production-messages.ts +6 -0
  107. package/src/production-playback.ts +1 -1
  108. package/src/production-voice-playback.ts +3 -2
  109. package/src/recording-adoption.ts +4 -4
  110. package/src/sha256.ts +9 -0
  111. package/src/speech-settings-messages.ts +6 -6
  112. package/src/step-production.ts +4 -4
  113. package/src/styles.css +43 -92
  114. package/src/transcription-drafts.ts +124 -0
  115. package/src/use-recording-adoption.ts +5 -4
  116. package/src/voice-drafts.ts +32 -8
  117. package/src/voice-messages.ts +9 -9
  118. package/src/voiceover-jobs.ts +1 -1
  119. package/src/workbench-state.ts +159 -0
  120. package/src/workbench.ts +323 -956
  121. package/src/workspace-client.ts +41 -12
  122. package/src/assistant-mascot.ts +0 -62
  123. package/src/mascot-preference.ts +0 -37
  124. package/src/panels/ChangeTrail.tsx +0 -79
@@ -0,0 +1,492 @@
1
+ import { applyDocumentCaptionTiming, changeDocumentCaptionScale, DOCUMENT_AUDIO_MAX_BYTES, randomUUID, replaceStepNarrations, sameProductionBinding, validateDocument, DOCUMENT_ENTRY } from '@lincyaw/kernel'
2
+ import type { DocumentAudioImportAnswer, DocumentAudioInspectionAnswer, DocumentCaptionTimingAction, DocumentChangeAnswer, DocumentRecordingAdoption, DocumentTranscriptionRequest, MediaClip, Narration, ProductionBinding, PresmiumDocument, TimeRange, TranscriptionAdoptionAnswer, TranscriptionAdoptionApply, TranscriptionAdoptionDraft, TranscriptionAdoptionPlan, TranscriptionTask, VerifiedDocumentRecordingAdoption, VoiceSource } from '@lincyaw/kernel'
3
+ import { isAgentChanging } from './workbench-state.ts'
4
+ import type { WorkbenchStore } from './workbench-state.ts'
5
+ import type { DocumentSession, DocumentSessionState } from './document-session.ts'
6
+ import { WorkspaceServerError } from './workspace-client.ts'
7
+ import type { WorkspaceClient } from './workspace-client.ts'
8
+ import { sha256 } from './sha256.ts'
9
+ import { reconcileRecordingAdoption } from './recording-adoption.ts'
10
+ import { changeDocumentVoice } from './document-voices.ts'
11
+ import type { DocumentVoiceOptions, DocumentVoiceScope } from './document-voices.ts'
12
+ import { componentSource, sameComponentSource } from './component-properties.ts'
13
+ import { reconcileCaptionTiming } from './caption-timing.ts'
14
+ import { changeStepTiming } from './step-production.ts'
15
+ import type { StepDurationDraft, StepDurationKey, StepTimingPatch } from './step-production.ts'
16
+ import { DocumentTimingDrafts } from './document-timing-drafts.ts'
17
+ import { CaptionTimingDrafts } from './caption-timing-drafts.ts'
18
+ import type { CaptionTimingDraft } from './caption-timing-drafts.ts'
19
+ import type { ClientProductionDraftMetadata } from '@lincyaw/kernel'
20
+ import { clearVoiceDraft, keepVoiceDraft, voiceDraftSnapshot } from './voice-drafts.ts'
21
+ import type { VoiceDraft } from './voice-drafts.ts'
22
+ import { discardMediaDraft, keepMediaDrafts, mediaDraftSnapshot } from './media-drafts.ts'
23
+ import type { MediaDrafts } from './media-drafts.ts'
24
+ import { TranscriptionCorrections } from './transcription-drafts.ts'
25
+ import type { TranscriptionCorrection, TranscriptionCorrectionTarget } from './transcription-drafts.ts'
26
+ import { effectiveOf } from './effective-settings.ts'
27
+
28
+ interface ProductionOptions {
29
+ store: WorkbenchStore
30
+ session: DocumentSession
31
+ client: WorkspaceClient
32
+ scope(): number
33
+ document(identity?: string): PresmiumDocument | undefined
34
+ flush(signal?: AbortSignal): Promise<void>
35
+ pause(): Promise<void>
36
+ stopAuditions(): void
37
+ stopPreviewOutputs(): void
38
+ sourceCurrent(binding: ProductionBinding): boolean
39
+ }
40
+ export interface RecordingRangeDraft { version: string, voice: Extract<VoiceSource, { type: 'recording' }>, range?: TimeRange, startText?: string, endText?: string }
41
+ export interface ProductionDraftView {
42
+ identity: string
43
+ kind: ClientProductionDraftMetadata['kind']
44
+ has(key: string): boolean
45
+ blocked(): boolean
46
+ clear(key: string): void
47
+ }
48
+ export interface ProductionDraftSnapshot {
49
+ stamp: string
50
+ coverage: 'tab-state' | 'partial'
51
+ candidates: { key: string, metadata: Omit<ClientProductionDraftMetadata, 'draft'>, value: unknown, discard(): void }[]
52
+ guard(): void
53
+ }
54
+
55
+ /** Source-bound media transactions, timing drafts and narration edits share the document actor. */
56
+ export class DocumentProduction {
57
+ readonly #audioImports = new WeakMap<File, { identity: string, version: string, id: string }>()
58
+ readonly #audioFingerprints = new WeakMap<File, Promise<string>>()
59
+ readonly #documentTimingDrafts = new DocumentTimingDrafts()
60
+ readonly #captionTimingDrafts = new CaptionTimingDrafts()
61
+ readonly #recordingRangeDrafts = new Map<string, Map<string, RecordingRangeDraft>>()
62
+ readonly #transcriptionCorrections = new TranscriptionCorrections()
63
+ readonly #draftViews = new Set<ProductionDraftView>()
64
+ readonly #draftSalt = randomUUID()
65
+ #draftRevision = 0
66
+ #unknownDraftViews = 0
67
+ #closed = false
68
+ readonly #removedCueTimings = new Map<string, { pauseMs: number }>()
69
+ readonly #removedNarrations = new Map<string, { narration: Narration, override?: { narrationStart: 'with-animation' | 'after-animation' } }>()
70
+ #cueTimingScope?: string
71
+ readonly #options: ProductionOptions
72
+ constructor(options: ProductionOptions) { this.#options = options }
73
+ draftState(authoring: DocumentSessionState) {
74
+ const scope = JSON.stringify([authoring.identity, authoring.reset])
75
+ if (this.#cueTimingScope !== scope) { this.#removedCueTimings.clear(); this.#removedNarrations.clear(); this.#cueTimingScope = scope }
76
+ return {
77
+ hasDocumentTimingDrafts: Boolean(authoring.identity && authoring.content && this.#documentTimingDrafts.has(authoring.identity, authoring.content)),
78
+ documentTimingDraftStorageFailure: this.#documentTimingDrafts.failed,
79
+ captionTimingDraftSteps: authoring.identity && authoring.member?.id && authoring.content ? this.#captionTimingDrafts.steps({ identity: authoring.identity, memberId: authoring.member.id }, authoring.content) : [],
80
+ captionTimingDraftStorageFailure: Boolean(authoring.identity && authoring.member?.id && this.#captionTimingDrafts.failed({ identity: authoring.identity, memberId: authoring.member.id })),
81
+ }
82
+ }
83
+ reset(): void { this.#removedCueTimings.clear(); this.#removedNarrations.clear(); this.#cueTimingScope = undefined }
84
+ dispose(): void { this.#closed = true; this.reset(); this.#captionTimingDrafts.close(); this.#transcriptionCorrections.close(); this.#recordingRangeDrafts.clear(); this.#draftViews.clear() }
85
+ get draftRevision(): number { return this.#draftRevision }
86
+ get productionDraftsBlocked(): boolean {
87
+ const identity = this.#options.store.state.authoring?.identity
88
+ return this.#closed || Boolean(this.#unknownDraftViews) || this.#options.session.hasPendingAssetWrite()
89
+ || [...this.#draftViews].some(view => view.identity === identity && view.blocked())
90
+ }
91
+ #draftChanged(): void {
92
+ if (this.#closed) return
93
+ this.#draftRevision++
94
+ const source = this.#options.store.state.authoring
95
+ if (source) this.#options.store.set({ authoring: { ...source }, ...this.draftState(source) })
96
+ }
97
+ registerDraftView(view: ProductionDraftView): () => void {
98
+ if (this.#closed) return () => {}
99
+ let closed = false
100
+ if (this.#draftViews.size >= 128) {
101
+ this.#unknownDraftViews++; this.#draftChanged()
102
+ return () => { if (closed) return; closed = true; this.#unknownDraftViews--; this.#draftChanged() }
103
+ }
104
+ this.#draftViews.add(view); this.#draftChanged()
105
+ return () => { if (closed) return; closed = true; this.#draftViews.delete(view); this.#draftChanged() }
106
+ }
107
+ keepVoiceDraft(identity: string, stepId: string, value: VoiceDraft): boolean {
108
+ const kept = keepVoiceDraft(this.#options.client, identity, stepId, value)
109
+ this.#draftChanged(); return kept
110
+ }
111
+ clearVoiceDraft(identity: string, stepId: string, expected: VoiceDraft): boolean {
112
+ const cleared = clearVoiceDraft(this.#options.client, identity, stepId, expected)
113
+ this.#draftChanged(); return cleared
114
+ }
115
+ keepMediaDrafts(identity: string, stepId: string, value: MediaDrafts): boolean {
116
+ const kept = keepMediaDrafts(this.#options.client, identity, stepId, value)
117
+ this.#draftChanged(); return kept
118
+ }
119
+ transcriptionCorrection(target: TranscriptionCorrectionTarget) { return this.#transcriptionCorrections.read(target) }
120
+ keepTranscriptionCorrection(target: TranscriptionCorrectionTarget, draft: TranscriptionCorrection): boolean {
121
+ const kept = this.#transcriptionCorrections.keep(target, draft)
122
+ this.#draftChanged(); return kept
123
+ }
124
+ completeTranscriptionCorrection(target: TranscriptionCorrectionTarget, expected: TranscriptionCorrection, next: TranscriptionCorrection): boolean {
125
+ const completed = this.#transcriptionCorrections.complete(target, expected, next)
126
+ this.#draftChanged(); return completed
127
+ }
128
+ /** Native pending range input survives mode and document changes within this workbench. */
129
+ recordingRangeDrafts(identity: string): ReadonlyMap<string, RecordingRangeDraft> {
130
+ let values = this.#recordingRangeDrafts.get(identity)
131
+ if (!values) { values = new Map(); this.#recordingRangeDrafts.set(identity, values) }
132
+ return values
133
+ }
134
+ setRecordingRangeDraft(identity: string, key: string, value: RecordingRangeDraft | undefined): void {
135
+ this.recordingRangeDrafts(identity)
136
+ const values = this.#recordingRangeDrafts.get(identity)!
137
+ if (value) values.set(key, value); else values.delete(key)
138
+ this.#draftChanged()
139
+ }
140
+
141
+ /** Native banks remain the sole content owners. A snapshot exposes metadata until an explicit read. */
142
+ productionDraftSnapshot(): ProductionDraftSnapshot {
143
+ const state = this.#options.store.state, source = state.authoring, identity = source?.identity, memberId = source?.member?.id, content = source?.content
144
+ if (this.#closed || !source || !identity || !memberId || !content || !source.version || !source.fingerprint || source.loading) throw new Error('client-production-drafts-unavailable')
145
+ const revision = this.#draftRevision, version = source.version, reset = source.reset
146
+ const corrections = this.#transcriptionCorrections.snapshot(identity)
147
+ const views = [...this.#draftViews].filter(view => view.identity === identity)
148
+ const viewState = () => JSON.stringify(views.map(view => view.blocked()))
149
+ const blocked = viewState()
150
+ const clean = () => {
151
+ const value = this.#options.store.state, current = value.authoring
152
+ return Boolean(this.#options.client.capabilities?.readOnly !== true && current && !current.loading && !current.dirty && !current.saving && !current.composing && !current.restored && !current.recovering && !current.error
153
+ && !isAgentChanging(value) && !value.isCreatingPage && !value.isEditingPage && !value.isHistoryBusy && !value.isHistoryPreparing && !this.productionDraftsBlocked)
154
+ }
155
+ const guard = () => {
156
+ corrections.guard()
157
+ const current = this.#options.store.state.authoring
158
+ if (!current || current.identity !== identity || current.member?.id !== memberId || current.content !== content || current.version !== version || current.reset !== reset
159
+ || current.loading || revision !== this.#draftRevision || viewState() !== blocked) throw new Error('client-production-drafts-changed')
160
+ }
161
+ const candidates: ProductionDraftSnapshot['candidates'] = []
162
+ const add = (kind: ClientProductionDraftMetadata['kind'], key: string, stepId: string, value: unknown, requiresReview: boolean, storage: 'tab' | 'memory', remove: () => boolean, extra: { field?: StepDurationKey, assetId?: string, clipId?: string } = {}, readable = true) => {
163
+ const matches = views.filter(view => view.kind === kind && view.has(key))
164
+ const discardable = readable && clean() && !this.#unknownDraftViews && !matches.some(view => view.blocked())
165
+ candidates.push({ key: JSON.stringify([kind, key]), metadata: { kind, stepId, ...extra, targetAvailable: Object.hasOwn(content.steps, stepId), requiresReview, storage, discardable }, value,
166
+ discard: () => {
167
+ guard()
168
+ const activeViews = views.filter(view => view.kind === kind && view.has(key))
169
+ if (!discardable || !clean() || activeViews.some(view => view.blocked())) throw new Error('client-production-draft-needs-human')
170
+ if (!remove()) { this.#draftChanged(); throw new Error('client-production-draft-storage-unavailable') }
171
+ for (const view of activeViews) view.clear(key)
172
+ this.#draftChanged()
173
+ } })
174
+ }
175
+ const timingPartial = this.#documentTimingDrafts.partial(identity), scope = { identity, memberId }
176
+ for (const [key, value] of this.#documentTimingDrafts.entries(identity)) {
177
+ const [stepId, field] = JSON.parse(key) as [string, StepDurationKey]
178
+ add('step-timing', key, stepId, value, !Object.hasOwn(content.steps, stepId) || (this.#documentTimingDrafts.get(identity, content, stepId, field)?.review ?? true), this.#documentTimingDrafts.failed || timingPartial ? 'memory' : 'tab', () => this.#documentTimingDrafts.discard(identity, key), { field }, !timingPartial)
179
+ }
180
+ const captionPartial = this.#captionTimingDrafts.partial(scope)
181
+ for (const [stepId, value] of this.#captionTimingDrafts.entries(scope)) {
182
+ const basis = state.productionPlan?.pages.flatMap(page => page.steps).find(step => step.stepId === stepId)?.captionTiming
183
+ add('caption-timing', stepId, stepId, value, value.version !== version || !basis || JSON.stringify(basis) !== JSON.stringify(value.basis), this.#captionTimingDrafts.failed(scope) || captionPartial ? 'memory' : 'tab', () => this.#captionTimingDrafts.discard(scope, stepId), {}, !captionPartial)
184
+ }
185
+ for (const [key, value] of this.#recordingRangeDrafts.get(identity) ?? []) {
186
+ const [stepId] = JSON.parse(key) as [string]
187
+ add('recording-range', key, stepId, value, value.version !== version, 'memory', () => this.#recordingRangeDrafts.get(identity)!.delete(key), { assetId: value.voice.assetId })
188
+ }
189
+ const voices = voiceDraftSnapshot(this.#options.client, identity)
190
+ for (const { stepId, value, persisted } of voices.entries) {
191
+ add('voice', stepId, stepId, value, value.version !== version, persisted ? 'tab' : 'memory', () => clearVoiceDraft(this.#options.client, identity, stepId, value), {}, !voices.partial)
192
+ }
193
+ const media = mediaDraftSnapshot(this.#options.client, identity)
194
+ for (const { stepId, value, persisted } of media.entries) for (const [name, draft] of Object.entries(value.entries)) {
195
+ const original = content.production.mediaClips.find(clip => clip.id === draft.clip.id)
196
+ add('media-clip', JSON.stringify([stepId, name]), stepId, draft, draft.version !== version || Boolean(draft.baseClip && JSON.stringify(draft.baseClip) !== JSON.stringify(original)), persisted ? 'tab' : 'memory',
197
+ () => discardMediaDraft(this.#options.client, identity, stepId, name, value), { clipId: draft.clip.id, ...draft.clip.assetId ? { assetId: draft.clip.assetId } : {} }, !media.partial)
198
+ }
199
+ for (const { key, target, draft, persisted } of corrections.entries) {
200
+ const { identity: _identity, ...binding } = target
201
+ add('transcription', key, target.stepId, { ...binding, draft }, true, persisted ? 'tab' : 'memory',
202
+ () => this.#transcriptionCorrections.discard(key, draft), {}, !corrections.partial)
203
+ }
204
+ candidates.sort((a, b) => a.key.localeCompare(b.key))
205
+ guard()
206
+ return { stamp: JSON.stringify([this.#draftSalt, identity, memberId, version, reset, revision]), coverage: timingPartial || captionPartial || voices.partial || media.partial || corrections.partial ? 'partial' : 'tab-state', candidates, guard }
207
+ }
208
+
209
+ source(expected: { identity: string, version: string }): DocumentSessionState {
210
+ const source = this.#options.store.state.authoring
211
+ if (!source || !this.#options.document(expected.identity) || source.version !== expected.version || !source.fingerprint
212
+ || source.dirty || source.saving || source.composing || source.restored || source.error || isAgentChanging(this.#options.store.state)) throw new Error('production-source-changed')
213
+ return source
214
+ }
215
+ /** Inspects the saved declaration, including a local asset not yet attached to a step. */
216
+ async inspectAudio(assetId: string, expected: { identity: string, version: string }, signal?: AbortSignal): Promise<DocumentAudioInspectionAnswer> {
217
+ const source = this.source(expected), scope = this.#options.scope()
218
+ const answer = await this.#options.client.readDocumentAudio(source.directory, assetId, expected.version, signal)
219
+ signal?.throwIfAborted()
220
+ if (scope !== this.#options.scope() || answer.document !== source.directory || answer.version !== expected.version || answer.fingerprint !== source.fingerprint || answer.inspection.assetId !== assetId) throw new Error('production-source-changed')
221
+ this.source(expected)
222
+ return answer
223
+ }
224
+ /** Recognition keeps its captured source even if subsequent author input moves to a newer version. */
225
+ async startTranscription(request: Omit<DocumentTranscriptionRequest, 'source'>, expected: { identity: string, version: string }, signal?: AbortSignal): Promise<TranscriptionTask> {
226
+ const source = this.source(expected)
227
+ if (!source.content || !source.fingerprint) throw new Error('production-source-changed')
228
+ const sent: DocumentTranscriptionRequest = { ...structuredClone(request), source: { documentId: source.content.id, version: expected.version, fingerprint: source.fingerprint } }
229
+ const answer = await this.#options.client.startTranscription(source.directory, sent, signal)
230
+ signal?.throwIfAborted()
231
+ if (answer.document !== source.directory || answer.intent !== sent.intent || answer.source.documentId !== sent.source.documentId
232
+ || answer.source.version !== sent.source.version || answer.source.fingerprint !== sent.source.fingerprint || answer.audio.assetId !== sent.assetId || answer.audio.sha256 !== sent.audioSha256) throw new Error('transcription-response-changed')
233
+ return answer
234
+ }
235
+ async planTranscription(draft: TranscriptionAdoptionDraft, expected: { identity: string, version: string }, signal?: AbortSignal): Promise<TranscriptionAdoptionPlan> {
236
+ const source = this.source(expected), sent = structuredClone(draft)
237
+ if (sent.source.documentId !== source.content?.id || sent.source.version !== expected.version || sent.source.fingerprint !== source.fingerprint) throw new Error('production-source-changed')
238
+ const answer = await this.#options.client.planTranscriptionAdoption(source.directory, sent, signal)
239
+ signal?.throwIfAborted()
240
+ this.source(expected)
241
+ if (answer.document !== source.directory || answer.changeId !== sent.id || answer.source.documentId !== sent.source.documentId
242
+ || answer.source.version !== sent.source.version || answer.source.fingerprint !== sent.source.fingerprint || answer.candidate.id !== sent.candidate.id
243
+ || answer.candidate.revision !== sent.candidate.revision || answer.draftRevision !== sent.draftRevision) throw new Error('transcription-response-changed')
244
+ return answer
245
+ }
246
+ async adoptTranscription(request: TranscriptionAdoptionApply, expected: { identity: string, version: string }, signal?: AbortSignal): Promise<TranscriptionAdoptionAnswer> {
247
+ const source = this.source(expected), sent = structuredClone(request)
248
+ if (sent.source.documentId !== source.content?.id || sent.source.version !== expected.version || sent.source.fingerprint !== source.fingerprint) throw new Error('production-source-changed')
249
+ const answer = await this.#writeDocumentMedia(expected, directory => this.#options.client.applyTranscriptionAdoption(directory, sent, signal), signal)
250
+ if (answer.adoption.planId !== sent.planId || answer.adoption.candidate.id !== sent.candidate.id || answer.adoption.candidate.revision !== sent.candidate.revision
251
+ || answer.adoption.draftRevision !== sent.draftRevision) throw new Error('transcription-response-changed')
252
+ return answer
253
+ }
254
+ async #writeDocumentMedia<T extends DocumentChangeAnswer>(expected: { identity: string, version: string }, write: (directory: string) => Promise<T>, signal?: AbortSignal): Promise<T> {
255
+ const scope = this.#options.scope(), source = this.source(expected)
256
+ this.#options.stopAuditions()
257
+ await this.#options.pause()
258
+ await this.#options.flush(signal)
259
+ signal?.throwIfAborted()
260
+ if (scope !== this.#options.scope()) throw new Error('production-source-changed')
261
+ this.source(expected)
262
+ let answer: T
263
+ try { answer = await write(source.directory) }
264
+ finally { await this.#options.session.refresh(source.directory) }
265
+ signal?.throwIfAborted()
266
+ if (scope !== this.#options.scope() || answer.document !== source.directory || answer.content.id !== source.content?.id) throw new Error('production-source-changed')
267
+ this.source({ identity: expected.identity, version: answer.version })
268
+ return answer
269
+ }
270
+ /** Import commits the original file and its declaration together, without creating a clip or narration. */
271
+ async importAudio(file: File, expected: { identity: string, version: string }, signal?: AbortSignal): Promise<DocumentAudioImportAnswer> {
272
+ let request: { identity: string, version: string, id: string } | undefined
273
+ const answer = await this.#writeDocumentMedia(expected, async directory => {
274
+ const existing = this.#audioImports.get(file), first = !existing || existing.identity !== expected.identity
275
+ request = first ? { identity: expected.identity, version: expected.version, id: randomUUID() } : existing!
276
+ this.#audioImports.set(file, request)
277
+ try { return await this.#options.client.importDocumentAudio(directory, file, { id: request.id, baseDocumentVersion: request.version }, signal) }
278
+ catch (error) {
279
+ if (first && error instanceof WorkspaceServerError && error.status >= 400 && error.status < 500 && this.#audioImports.get(file) === request) this.#audioImports.delete(file)
280
+ throw error
281
+ }
282
+ }, signal)
283
+ if (answer.changeId !== request?.id || answer.inspection.assetId !== answer.assetId || !answer.content.assets[answer.assetId]) throw new Error('production-source-changed')
284
+ if (this.#audioImports.get(file) === request) this.#audioImports.delete(file)
285
+ return answer
286
+ }
287
+ /** Reconcile an ambiguous save against real asset bytes before admitting a new source-bound import. */
288
+ async ensureAudio(file: File, expected: { identity: string, version: string }, signal?: AbortSignal): Promise<DocumentAudioInspectionAnswer> {
289
+ const source = this.source(expected)
290
+ if (!file.size || file.size > DOCUMENT_AUDIO_MAX_BYTES) throw new Error('production.asset-size')
291
+ let pending = this.#audioFingerprints.get(file)
292
+ if (!pending) {
293
+ pending = file.arrayBuffer().then(sha256)
294
+ this.#audioFingerprints.set(file, pending)
295
+ }
296
+ const hash = await pending
297
+ signal?.throwIfAborted(); this.source(expected)
298
+ for (const asset of Object.values(source.content?.assets ?? {})) {
299
+ if (asset.kind !== 'audio' || asset.fingerprint !== `sha256:${hash}`) continue
300
+ try {
301
+ const answer = await this.inspectAudio(asset.id, expected, signal)
302
+ if (answer.inspection.sha256 === hash) return answer
303
+ } catch (error) {
304
+ if (!(error instanceof WorkspaceServerError) || ![404, 413, 422].includes(error.status)) throw error
305
+ }
306
+ }
307
+ signal?.throwIfAborted(); this.source(expected)
308
+ const previous = this.#audioImports.get(file)
309
+ if (previous?.identity === expected.identity && previous.version !== expected.version) this.#audioImports.delete(file)
310
+ let imported: DocumentAudioImportAnswer
311
+ try { imported = await this.importAudio(file, expected, signal) }
312
+ catch (error) {
313
+ if (!previous || this.#audioImports.get(file) !== previous || !(error instanceof WorkspaceServerError)
314
+ || error.status !== 409 || error.message !== 'document-version-changed') throw error
315
+ signal?.throwIfAborted(); this.source(expected)
316
+ this.#audioImports.delete(file)
317
+ imported = await this.importAudio(file, expected, signal)
318
+ }
319
+ return { document: imported.document, version: imported.version, fingerprint: imported.fingerprint, inspection: imported.inspection }
320
+ }
321
+ /** The source actor owns this external transaction; capture is independent from preview output. */
322
+ async adoptRecording(request: DocumentRecordingAdoption, expected: { identity: string, version: string }, signal?: AbortSignal): Promise<VerifiedDocumentRecordingAdoption> {
323
+ const scope = this.#options.scope(), source = this.source(expected), sent = structuredClone(request)
324
+ if (sent.source.documentId !== source.content?.id || sent.source.version !== source.version || sent.source.fingerprint !== source.fingerprint
325
+ || !source.content.steps[sent.stepId] || !source.content.assets[sent.assetId]) throw new Error('production-source-changed')
326
+ this.#options.stopPreviewOutputs()
327
+ await this.#options.pause()
328
+ await this.#options.flush(signal)
329
+ signal?.throwIfAborted()
330
+ if (scope !== this.#options.scope()) throw new Error('production-source-changed')
331
+ this.source(expected)
332
+ const answer = await this.#options.session.commitExternal(expected, async (_before, ownedSignal) => {
333
+ const saved = await this.#options.client.adoptRecording(source.directory, sent, ownedSignal)
334
+ if (saved.changeId !== sent.id || saved.verification.stepId !== sent.stepId || saved.verification.assetId !== sent.assetId || saved.verification.sha256 !== sent.sha256) throw new Error('document-confirmation')
335
+ return saved
336
+ }, (before, remote, current) => reconcileRecordingAdoption(sent, before, remote, current), signal)
337
+ signal?.throwIfAborted()
338
+ if (scope !== this.#options.scope() || this.#options.store.state.authoring?.identity !== expected.identity) throw new Error('production-source-changed')
339
+ return answer
340
+ }
341
+ async applyMediaClip(clip: MediaClip, expected: { identity: string, version: string, binding: ProductionBinding }, signal?: AbortSignal): Promise<DocumentChangeAnswer> {
342
+ this.source(expected)
343
+ if (!this.#options.store.state.productionPlan || !sameProductionBinding(this.#options.store.state.productionPlan.binding, expected.binding) || !this.#options.sourceCurrent(expected.binding)) throw new Error('production-source-changed')
344
+ const id = randomUUID(), value = structuredClone(clip)
345
+ const answer = await this.#writeDocumentMedia(expected, directory => this.#options.client.changeDocumentMediaClip(directory, { id, baseDocumentVersion: expected.version, binding: expected.binding, clip: value }, signal), signal)
346
+ if (answer.changeId !== id) throw new Error('production-source-changed')
347
+ return answer
348
+ }
349
+ async removeMediaClip(clipId: string, expected: { identity: string, version: string }, signal?: AbortSignal): Promise<DocumentChangeAnswer> {
350
+ const id = randomUUID()
351
+ const answer = await this.#writeDocumentMedia(expected, directory => this.#options.client.changeDocumentMediaClip(directory, { id, baseDocumentVersion: expected.version, removeClipId: clipId }, signal), signal)
352
+ if (answer.changeId !== id) throw new Error('production-source-changed')
353
+ return answer
354
+ }
355
+ /** Applies a reviewed voice scope to the current source actor, without replacing body or narration. */
356
+ async applyVoice(scope: DocumentVoiceScope, value: VoiceSource | null, options: DocumentVoiceOptions, expected: { identity: string, version: string, fingerprint: string }): Promise<boolean> {
357
+ const source = this.#options.store.state.authoring, document = this.#options.document(expected.identity), context = this.#options.scope()
358
+ if (!source || !document || source.version !== expected.version || source.fingerprint !== expected.fingerprint
359
+ || source.dirty || source.saving || source.composing || source.restored || source.error || isAgentChanging(this.#options.store.state)) throw new Error('voice-context-changed')
360
+ const next = changeDocumentVoice(document, scope, value, options)
361
+ if (next === document) return true
362
+ this.#options.stopAuditions()
363
+ this.#options.session.record(next)
364
+ await this.#options.flush()
365
+ if (context !== this.#options.scope() || this.#options.store.state.authoring?.identity !== expected.identity) return false
366
+ const saved = this.#options.store.state.authoring
367
+ if (!saved || saved.dirty || saved.saving || saved.composing || saved.restored || saved.error) throw new Error('voice-save-unconfirmed')
368
+ return true
369
+ }
370
+
371
+ /** Manual caption times reuse the canonical action plan, media preflight and original source actor. */
372
+ async applyCaptionTiming(action: DocumentCaptionTimingAction, id: string, expected: { identity: string, version: string, fingerprint: string }, signal?: AbortSignal): Promise<DocumentChangeAnswer> {
373
+ const source = this.source(expected), scope = this.#options.scope()
374
+ if (!source.content || source.fingerprint !== expected.fingerprint) throw new Error('caption-source-changed')
375
+ const request = { id, source: componentSource({ content: source.content, version: source.version!, fingerprint: source.fingerprint! }), action }
376
+ const plan = await this.#options.client.planDocumentAction(source.directory, request, signal)
377
+ signal?.throwIfAborted()
378
+ if (scope !== this.#options.scope() || this.#options.store.state.authoring?.identity !== expected.identity || plan.effect.kind !== 'caption'
379
+ || plan.changeId !== id || JSON.stringify(plan.action) !== JSON.stringify(action) || !sameComponentSource(plan.source, request.source)) throw new Error('caption-source-changed')
380
+ this.#options.stopPreviewOutputs()
381
+ await this.#options.pause()
382
+ try {
383
+ return await this.#options.session.commitExternal(expected, async (_before, ownedSignal) => {
384
+ const receipt = await this.#options.client.applyDocumentAction(source.directory, { planId: plan.planId, digest: plan.digest, source: plan.source }, ownedSignal)
385
+ const actual = await this.#options.client.readDocument(source.directory)
386
+ if (receipt.planId !== plan.planId || receipt.digest !== plan.digest || receipt.changeId !== id || !receipt.verification.saved || !receipt.verification.actionStateVerified
387
+ || !sameComponentSource(receipt.verification.source, componentSource(actual))) throw new Error('document-confirmation')
388
+ return { ...actual, changeId: id, files: receipt.files }
389
+ }, (before, answer, current) => reconcileCaptionTiming(action, before, answer, current), signal)
390
+ } catch (error) {
391
+ const current = this.#options.store.state.authoring, file = plan.files.find(file => file.path === (source.directory ? `${source.directory}/${DOCUMENT_ENTRY}` : DOCUMENT_ENTRY))
392
+ // Lost ACK was independently read by commitExternal; only the exact predicted saved version can settle it.
393
+ if (scope === this.#options.scope() && current?.identity === expected.identity && current.content && current.version === file?.version && current.fingerprint && !current.error) {
394
+ try {
395
+ if (!applyDocumentCaptionTiming(current.content, action).effect.changed) return { document: source.directory, content: plan.content, version: current.version!, fingerprint: current.fingerprint, changeId: id, issues: plan.issues, files: plan.files.map(({ path, version }) => ({ path, version })) }
396
+ } catch { /* A newer target stays pending with the actual request error. */ }
397
+ }
398
+ throw error
399
+ }
400
+ }
401
+ /** Uses the current actor revision so timing edits cannot replace later body or narration input. */
402
+ setStepTiming(stepId: string, patch: StepTimingPatch, identity?: string): void {
403
+ const document = this.#options.document(identity)
404
+ if (!document || isAgentChanging(this.#options.store.state)) return
405
+ const next = changeStepTiming(document, stepId, patch)
406
+ if (next === document) return
407
+ this.#options.stopAuditions()
408
+ this.#options.session.record(next)
409
+ }
410
+ /** The whole document's caption size (`production.captions.scale`), recorded like a step's timing. */
411
+ setCaptionScale(scale: number, identity?: string): void {
412
+ const document = this.#options.document(identity)
413
+ if (!document || isAgentChanging(this.#options.store.state)) return
414
+ const next = changeDocumentCaptionScale(document, scale)
415
+ if (next === document) return
416
+ this.#options.session.record(next)
417
+ }
418
+ timingDraft(stepId: string, key: StepDurationKey, identity: string, reset: number): StepDurationDraft | undefined {
419
+ const document = this.#options.document(identity)
420
+ return document && this.#options.store.state.authoring?.reset === reset ? this.#documentTimingDrafts.get(identity, effectiveOf(document, this.#options.store.state.authoring.inherited), stepId, key) : undefined
421
+ }
422
+ /** Retains unaccepted numeric input by stable target; it is not authoring content or a second timing source. */
423
+ setTimingDraft(stepId: string, key: StepDurationKey, draft: StepDurationDraft | undefined, identity: string, reset: number): void {
424
+ const source = this.#options.store.state.authoring, document = this.#options.document(identity)
425
+ if (!document?.steps[stepId] || source?.reset !== reset || isAgentChanging(this.#options.store.state)) return
426
+ if (!this.#documentTimingDrafts.set(identity, effectiveOf(document, source.inherited), stepId, key, draft)) return
427
+ this.#draftRevision++
428
+ this.#options.store.set({ authoring: { ...source }, hasDocumentTimingDrafts: this.#documentTimingDrafts.has(identity, document), documentTimingDraftStorageFailure: this.#documentTimingDrafts.failed })
429
+ }
430
+ captionTimingDraft(stepId: string, identity: string): CaptionTimingDraft | undefined {
431
+ const source = this.#options.store.state.authoring
432
+ return source?.identity === identity && source.member?.id ? this.#captionTimingDrafts.get({ identity, memberId: source.member.id }, stepId) : undefined
433
+ }
434
+ setCaptionTimingDraft(stepId: string, draft: CaptionTimingDraft | undefined, identity: string): void {
435
+ const source = this.#options.store.state.authoring, document = this.#options.document(identity)
436
+ if (!document?.steps[stepId] || source?.identity !== identity || !source.member?.id) return
437
+ const scope = { identity, memberId: source.member.id }
438
+ this.#captionTimingDrafts.set(scope, stepId, draft)
439
+ this.#draftRevision++
440
+ this.#options.store.set({ captionTimingDraftSteps: this.#captionTimingDrafts.steps(scope, document), captionTimingDraftStorageFailure: this.#captionTimingDrafts.failed(scope) })
441
+ }
442
+ discardCaptionTimingDraft(stepId: string, identity: string): boolean {
443
+ const source = this.#options.store.state.authoring
444
+ if (source?.identity !== identity || !source.member?.id) return false
445
+ const discarded = this.#captionTimingDrafts.discard({ identity, memberId: source.member.id }, stepId)
446
+ this.#draftChanged()
447
+ return discarded
448
+ }
449
+ copyNarrationCueTiming(from: string, to: string, identity: string): void {
450
+ const document = this.#options.document(identity)
451
+ if (!document || isAgentChanging(this.#options.store.state)) return
452
+ const timing = document.production.cueTimings?.[from] ?? this.#removedCueTimings.get(from)
453
+ if (timing) this.#removedCueTimings.set(to, { ...timing })
454
+ }
455
+ /** Native block identity and order own the current step's complete script. */
456
+ setNarrations(stepId: string, narrations: Narration[], identity?: string): void {
457
+ const document = this.#options.document(identity)
458
+ if (!document || isAgentChanging(this.#options.store.state) || !Object.hasOwn(document.steps, stepId)) return
459
+ const step = document.steps[stepId]!
460
+ if (!document.sections[step.sectionId]?.stepOrder.includes(stepId)) return
461
+ const values = narrations.map(narration => {
462
+ const previous = document.narrations[narration.id] ?? this.#removedNarrations.get(narration.id)?.narration
463
+ return { ...previous, ...narration }
464
+ })
465
+ if (JSON.stringify(step.narrationIds.map(id => document.narrations[id])) === JSON.stringify(values)) return
466
+ const next = replaceStepNarrations(document, stepId, values)
467
+ for (const narration of values) {
468
+ const override = this.#removedNarrations.get(narration.id)?.override
469
+ if (override && !document.narrations[narration.id]) next.production.narrationOverrides = { ...next.production.narrationOverrides, [narration.id]: { ...override } }
470
+ }
471
+ const cueIds = new Set(values.flatMap(narration => narration.content.flatMap(node => node.type === 'cue' ? [node.id] : [])))
472
+ for (const cue of cueIds) {
473
+ const timing = this.#removedCueTimings.get(cue)
474
+ if (timing && !Object.hasOwn(next.production.cueTimings ?? {}, cue)) next.production.cueTimings = { ...next.production.cueTimings, [cue]: { ...timing } }
475
+ }
476
+ const references = (values: Narration[]) => JSON.stringify(values.map(narration => [narration.id, narration.provenance, narration.content.filter(node => node.type === 'cue')]))
477
+ if (references(step.narrationIds.map(id => document.narrations[id]!)) !== references(values)
478
+ && validateDocument(next).issues.some(issue => issue.severity === 'error')) throw new Error('document-narration-content')
479
+ for (const id of step.narrationIds) if (!next.steps[stepId]!.narrationIds.includes(id)) {
480
+ this.#removedNarrations.delete(id)
481
+ this.#removedNarrations.set(id, { narration: document.narrations[id]!, override: document.production.narrationOverrides?.[id] })
482
+ }
483
+ for (const [id, timing] of Object.entries(document.production.cueTimings ?? {})) if (!Object.hasOwn(next.production.cueTimings ?? {}, id)) {
484
+ this.#removedCueTimings.delete(id)
485
+ this.#removedCueTimings.set(id, { ...timing })
486
+ }
487
+ while (this.#removedNarrations.size > 512) this.#removedNarrations.delete(this.#removedNarrations.keys().next().value!)
488
+ while (this.#removedCueTimings.size > 512) this.#removedCueTimings.delete(this.#removedCueTimings.keys().next().value!)
489
+ this.#options.stopAuditions()
490
+ this.#options.session.record(next)
491
+ }
492
+ }
@@ -1,4 +1,4 @@
1
- import type { TeachingDocument } from '@lincyaw/kernel'
1
+ import type { PresmiumDocument } from '@lincyaw/kernel'
2
2
 
3
3
  export interface DocumentReturnPoint {
4
4
  sectionId: string
@@ -38,7 +38,7 @@ export class DocumentReturnPoints {
38
38
  }
39
39
  }
40
40
 
41
- export function documentReturnStep(document: TeachingDocument, point: DocumentReturnPoint): number | undefined {
41
+ export function documentReturnStep(document: PresmiumDocument, point: DocumentReturnPoint): number | undefined {
42
42
  const section = document.sections[point.sectionId]
43
43
  if (!section || !document.sectionOrder.includes(point.sectionId)) return
44
44
  if (point.stepId === undefined) return section.stepOrder.length === 0 ? 0 : undefined