@lincyaw/workbench 0.2.0-rc.5 → 0.2.0-rc.51

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 (80) hide show
  1. package/package.json +5 -4
  2. package/src/MediaClipEditor.tsx +1 -1
  3. package/src/audio-recording.ts +27 -5
  4. package/src/authoring-client.ts +10 -10
  5. package/src/authoring-draft-fragment.ts +3 -1
  6. package/src/authoring-window.ts +100 -0
  7. package/src/body-draft-actions.ts +60 -0
  8. package/src/component-property-drafts.ts +5 -5
  9. package/src/components.tsx +10 -28
  10. package/src/context.tsx +2 -1
  11. package/src/diagnostics.ts +1 -1
  12. package/src/document-draft-store.ts +45 -18
  13. package/src/document-drafts.ts +28 -19
  14. package/src/document-editing.ts +484 -0
  15. package/src/document-editor/BlockNoteSurface.tsx +25 -9
  16. package/src/document-editor/ComponentMaterials.tsx +3 -9
  17. package/src/document-editor/ComponentPropertiesDialog.tsx +4 -4
  18. package/src/document-editor/DocumentBlockPreview.tsx +4 -2
  19. package/src/document-editor/DocumentComments.tsx +58 -9
  20. package/src/document-editor/DocumentEditor.tsx +44 -7
  21. package/src/document-editor/DocumentStepNarration.tsx +30 -25
  22. package/src/document-editor/NarrationEditor.tsx +54 -20
  23. package/src/document-editor/NewCommentDrafts.tsx +4 -3
  24. package/src/document-editor/PlaceComment.tsx +150 -0
  25. package/src/document-editor/comment-drafts.ts +16 -14
  26. package/src/document-editor/comments.ts +30 -10
  27. package/src/document-editor/component-label.ts +11 -0
  28. package/src/document-editor/file-input.ts +2 -2
  29. package/src/document-editor/focus.ts +0 -2
  30. package/src/document-editor/insertion-place.ts +60 -0
  31. package/src/document-editor/messages.ts +3 -3
  32. package/src/document-editor/narration-schema.tsx +12 -6
  33. package/src/document-editor/narration.ts +19 -3
  34. package/src/document-editor/page-body.ts +60 -0
  35. package/src/document-editor/projection.ts +2 -2
  36. package/src/document-editor/rich-text.ts +3 -2
  37. package/src/document-editor/selection.ts +13 -0
  38. package/src/document-editor/style.css +12 -1
  39. package/src/document-editor/table-identities.ts +2 -1
  40. package/src/document-production.ts +339 -0
  41. package/src/document-session.ts +193 -53
  42. package/src/history.ts +5 -5
  43. package/src/index.ts +6 -5
  44. package/src/jobs.ts +1 -1
  45. package/src/messages.ts +19 -4
  46. package/src/panels/DocumentView.tsx +66 -26
  47. package/src/panels/DocumentVoiceDialog.tsx +2 -2
  48. package/src/panels/DocumentVoiceField.tsx +1 -1
  49. package/src/panels/ExportPanel.tsx +1 -6
  50. package/src/panels/HistoryPanel.tsx +1 -1
  51. package/src/panels/PageDeleteDialog.tsx +4 -4
  52. package/src/panels/PageRenameDialog.tsx +1 -1
  53. package/src/panels/PreviewPanel.tsx +1 -4
  54. package/src/panels/ProductionCaptionSize.tsx +20 -0
  55. package/src/panels/ProductionCaptionTiming.tsx +7 -7
  56. package/src/panels/ProductionMediaClips.tsx +6 -5
  57. package/src/panels/ProductionPanel.tsx +8 -4
  58. package/src/panels/ProductionRecording.tsx +86 -9
  59. package/src/panels/ProductionRecordingRange.tsx +19 -19
  60. package/src/panels/ProductionTimeline.tsx +6 -1
  61. package/src/panels/ProductionTranscription.tsx +5 -4
  62. package/src/panels/SourceDocumentPanel.tsx +12 -15
  63. package/src/panels/TranscriptionAdoption.tsx +9 -8
  64. package/src/panels/VoiceCloneDialog.tsx +2 -1
  65. package/src/panels/VoiceoverDialog.tsx +1 -1
  66. package/src/places.ts +1 -36
  67. package/src/preview-playback.ts +1 -1
  68. package/src/production-messages.ts +6 -0
  69. package/src/production-playback.ts +1 -1
  70. package/src/production-voice-playback.ts +3 -2
  71. package/src/sha256.ts +9 -0
  72. package/src/styles.css +16 -41
  73. package/src/use-recording-adoption.ts +2 -2
  74. package/src/voiceover-jobs.ts +1 -1
  75. package/src/workbench-state.ts +157 -0
  76. package/src/workbench.ts +246 -948
  77. package/src/workspace-client.ts +21 -11
  78. package/src/assistant-mascot.ts +0 -62
  79. package/src/mascot-preference.ts +0 -37
  80. package/src/panels/ChangeTrail.tsx +0 -79
@@ -0,0 +1,60 @@
1
+ import { createExtension } from '@blocknote/core'
2
+ import { Plugin, TextSelection } from '@tiptap/pm/state'
3
+ import type { EditorState } from '@tiptap/pm/state'
4
+ import { Decoration, DecorationSet } from '@tiptap/pm/view'
5
+ import type { Node } from '@tiptap/pm/model'
6
+ import type { NativeDocumentEditor } from './schema.tsx'
7
+
8
+ // Pages are top-level blocks: doc > blockGroup > blockContainer > (content, children?).
9
+ function emptyParagraph(container: Node | null | undefined): boolean {
10
+ return container?.childCount === 1 && container.firstChild!.type.name === 'paragraph' && container.firstChild!.content.size === 0
11
+ }
12
+
13
+ function pageEnds(doc: Node, placeholder: string): DecorationSet {
14
+ const group = doc.firstChild, found: Decoration[] = []
15
+ if (!group) return DecorationSet.empty
16
+ group.forEach((container, offset, index) => {
17
+ const next = index + 1 < group.childCount ? group.child(index + 1) : undefined
18
+ if (!emptyParagraph(container) || next && next.firstChild?.type.name !== 'presmiumPage') return
19
+ const start = 1 + offset + 1
20
+ found.push(Decoration.node(start, start + container.firstChild!.nodeSize, { 'data-page-end': '', 'data-placeholder': placeholder }))
21
+ })
22
+ return DecorationSet.create(doc, found)
23
+ }
24
+
25
+ /**
26
+ * A page's empty last paragraph shows the editor's placeholder even without the caret, so an empty body is visible;
27
+ * Enter at the end of a title goes into that paragraph instead of adding another one before it.
28
+ */
29
+ export const documentPageBodies = createExtension(({ editor }: { editor: NativeDocumentEditor }) => ({
30
+ key: 'presmium-document-page-bodies',
31
+ prosemirrorPlugins: [new Plugin<DecorationSet>({
32
+ state: {
33
+ init: (_, state: EditorState) => pageEnds(state.doc, editor.dictionary.placeholders.default ?? ''),
34
+ apply: (transaction, value) => transaction.docChanged ? pageEnds(transaction.doc, editor.dictionary.placeholders.default ?? '') : value,
35
+ },
36
+ props: { decorations(state) { return editor.isEditable ? this.getState(state) : undefined } },
37
+ })],
38
+ keyboardShortcuts: {
39
+ Enter: () => {
40
+ const { selection } = editor.prosemirrorState, { $head } = selection
41
+ if (!editor.isEditable || !selection.empty || $head.parent.type.name !== 'presmiumPage' || $head.parentOffset !== $head.parent.content.size) return false
42
+ const after = $head.after($head.depth - 1)
43
+ if (!emptyParagraph(editor.prosemirrorState.doc.resolve(after).nodeAfter)) return false
44
+ editor.transact(transaction => transaction.setSelection(TextSelection.create(transaction.doc, after + 2)).scrollIntoView())
45
+ return true
46
+ },
47
+ },
48
+ }))
49
+
50
+ /** The caret at the end of the document: in its empty last paragraph, or in a new one after the last block. */
51
+ export function focusDocumentEnd(editor: NativeDocumentEditor): void {
52
+ const last = editor.document.at(-1)
53
+ if (!last) return
54
+ editor.transact(transaction => {
55
+ if (last.type === 'paragraph' && !last.children.length && !last.content.length) editor.setTextCursorPosition(last, 'end')
56
+ else editor.setTextCursorPosition(editor.insertBlocks([{ type: 'paragraph' }], last, 'after')[0]!, 'start')
57
+ transaction.scrollIntoView()
58
+ })
59
+ editor.focus()
60
+ }
@@ -1,4 +1,4 @@
1
- import { documentPageDefaults, DOCUMENT_PAGE_DEFAULT_TIMING } from '@lincyaw/kernel'
1
+ import { documentPageDefaults, DOCUMENT_PAGE_DEFAULT_TIMING, randomUUID } from '@lincyaw/kernel'
2
2
  import type { DocumentBlock, DocumentPageIds, NarrationNode, RichTextNode, Section, TeachingDocument, TextBlock } from '@lincyaw/kernel'
3
3
  import { productInlineToNative } from './inline.ts'
4
4
  import { captureRichBaseline, createRichProjection, nativeRichTextToProduct, productRichTextToNative } from './rich-text.ts'
@@ -17,7 +17,7 @@ export interface DocumentProjection extends RichProjection {
17
17
  componentConfiguration: Map<string, string>
18
18
  }
19
19
  export type NativePageCreation = DocumentPageIds
20
- const newId = () => crypto.randomUUID()
20
+ const newId = () => randomUUID()
21
21
  export const pageId = (sectionId: string) => `page:${sectionId}`
22
22
  function plain(nodes: readonly { type: string, text?: string, children?: readonly unknown[] }[]): string {
23
23
  return nodes.map(node => node.type === 'text' ? node.text ?? '' : node.type === 'break' ? '\n' : node.children ? plain(node.children as Parameters<typeof plain>[0]) : '').join('')
@@ -1,4 +1,5 @@
1
1
  import type { InlineNode, JsonValue, ListItemNode, RichTextNode, TableNode } from '@lincyaw/kernel'
2
+ import { randomUUID } from '@lincyaw/kernel'
2
3
  import type { TableCellProps } from '@blocknote/core'
3
4
  import { nativeInlineToProduct, nativeTextStyleToProduct, productInlineToNative, productTextStyleToNative } from './inline.ts'
4
5
  import type { InlineProjectionContext, NativeInline } from './inline.ts'
@@ -27,12 +28,12 @@ export interface RichProjection extends InlineProjectionContext {
27
28
  tables: Map<string, { id: string, cells: string[] }[]>
28
29
  }
29
30
  export function createRichProjection(): RichProjection {
30
- return { rich: new Map(), baseline: new Map(), tables: new Map(), opaqueInline: new Map(), newInlineId: () => crypto.randomUUID() }
31
+ return { rich: new Map(), baseline: new Map(), tables: new Map(), opaqueInline: new Map(), newInlineId: () => randomUUID() }
31
32
  }
32
33
  export function captureRichBaseline(blocks: readonly DocumentEditorBlock[], context: RichProjection): void {
33
34
  for (const block of blocks) { context.baseline.set(block.id, JSON.stringify(block)); captureRichBaseline(block.children, context) }
34
35
  }
35
- const id = () => crypto.randomUUID()
36
+ const id = () => randomUUID()
36
37
  const atom = (node: RichTextNode): DocumentEditorInput => ({ id: node.id, type: 'presmiumAtom', props: { entityId: node.id, kind: `rich:${node.type}`, label: node.type === 'extension' ? fallbackText(node.fallback) : fallbackText(node) } })
37
38
 
38
39
  /** The projection records structural identities; the saved document owns all content. */
@@ -120,6 +120,19 @@ export function nativeTextPosition(editor: NativeDocumentEditor, point: { blockI
120
120
  return nativeTextPositionInDocument(editor.prosemirrorState.doc, point)
121
121
  }
122
122
 
123
+ /** The native range of a block's whole inline text, to select it. */
124
+ export function nativeBlockText(editor: NativeDocumentEditor, blockId: string): { from: number, to: number } | undefined {
125
+ let range: { from: number, to: number } | undefined
126
+ editor.prosemirrorState.doc.descendants((node, beforePos) => {
127
+ if (range) return false
128
+ if (node.attrs.id !== blockId) return
129
+ const info = getBlockInfoFromNode(node, beforePos)
130
+ if (info.hasContent && info.contentKind === 'inline') range = { from: info.contentStart, to: info.contentEnd }
131
+ return false
132
+ })
133
+ return range
134
+ }
135
+
123
136
  /** Stable anchor lookup against a captured native transaction’s input document. */
124
137
  export function nativeTextPositionInDocument(document: ProseMirrorNode, point: { blockId: string, offset: number }): number | undefined {
125
138
  let position: number | undefined
@@ -1,4 +1,8 @@
1
- .pm-document-editor { min-width: 0; }
1
+ .pm-document-editor { display: flex; flex: 1 0 auto; flex-direction: column; min-width: 0; }
2
+ /* The page below the last block, down to the sheet's end: a press there continues the document at its end. */
3
+ .pm-document-editor__end { flex: 1 0 auto; min-height: var(--pm-space-4); }
4
+ .pm-document-editor__end[data-editable] { cursor: text; }
5
+ .pm-document-editor .bn-block-content[data-page-end]:has(.ProseMirror-trailingBreak:only-child)::after { content: attr(data-placeholder); }
2
6
  .pm-document-editor__file-errors { display: flex; align-items: flex-start; gap: var(--pm-space-2); }
3
7
  .pm-document-editor__file-errors ul { flex: 1; min-width: 0; margin: 0; padding-inline-start: var(--pm-space-4); overflow-wrap: anywhere; }
4
8
  .pm-document-editor .pm-document-editor__page-title { margin-block: var(--pm-space-5) var(--pm-space-3); font-family: var(--pm-font-sans); font-size: var(--pm-text-title); font-weight: var(--pm-weight-semibold); line-height: var(--pm-leading-tight); }
@@ -7,3 +11,10 @@
7
11
  .pm-document-editor__fallback { display: grid; gap: var(--pm-space-2); min-width: 0; }
8
12
  .pm-document-editor__fallback p { margin: 0; white-space: pre-wrap; }
9
13
  .pm-document-editor__comment-tools { position: sticky; top: var(--pm-space-2); z-index: var(--pm-z-site-header); width: fit-content; margin-inline-start: var(--pm-space-5); }
14
+
15
+ /* A place comment's popover: the comment editor, a position picker and its actions. */
16
+ .pm-document-place-comment { width: min(var(--pm-comment-width), calc(100vw - var(--pm-space-8))); }
17
+ .pm-document-place-comment__body { display: grid; gap: var(--pm-space-2); min-width: 0; }
18
+ .pm-document-place-comment__editor { min-width: 0; }
19
+ .pm-document-place-comment__editor .bn-editor { max-height: min(var(--pm-comment-editor-max-height), 45vh); overflow-y: auto; overflow-wrap: anywhere; }
20
+ .pm-document-place-comment__actions { display: flex; justify-content: end; gap: var(--pm-space-2); }
@@ -1,6 +1,7 @@
1
1
  import { createExtension, getBlockInfoNearPos, getNodeId } from '@blocknote/core'
2
2
  import { Extension } from '@tiptap/core'
3
3
  import { Plugin } from '@tiptap/pm/state'
4
+ import { randomUUID } from '@lincyaw/kernel'
4
5
  import type { NativeDocumentEditor } from './schema.tsx'
5
6
  import type { RichProjection } from './rich-text.ts'
6
7
 
@@ -24,7 +25,7 @@ export const tableIdentityExtension = createExtension({
24
25
  after.doc.descendants((node, position) => {
25
26
  if (!TYPES.includes(node.type.name)) return
26
27
  let id = node.attrs[ATTR] as string | null
27
- if (!id || used.has(id)) { id = crypto.randomUUID(); transaction.setNodeMarkup(position, undefined, { ...node.attrs, [ATTR]: id }) }
28
+ if (!id || used.has(id)) { id = randomUUID(); transaction.setNodeMarkup(position, undefined, { ...node.attrs, [ATTR]: id }) }
28
29
  used.add(id)
29
30
  })
30
31
  if (transaction.steps.length) return transaction.setMeta('addToHistory', false)
@@ -0,0 +1,339 @@
1
+ import { applyDocumentCaptionTiming, changeDocumentCaptionScale, DOCUMENT_AUDIO_MAX_BYTES, randomUUID, replaceStepNarrations, sameProductionBinding, validateDocument } from '@lincyaw/kernel'
2
+ import type { DocumentAudioImportAnswer, DocumentAudioInspectionAnswer, DocumentCaptionTimingAction, DocumentChangeAnswer, DocumentRecordingAdoption, DocumentTranscriptionRequest, MediaClip, Narration, ProductionBinding, TeachingDocument, 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
+
20
+ interface ProductionOptions {
21
+ store: WorkbenchStore
22
+ session: DocumentSession
23
+ client: WorkspaceClient
24
+ scope(): number
25
+ document(identity?: string): TeachingDocument | undefined
26
+ flush(signal?: AbortSignal): Promise<void>
27
+ pause(): Promise<void>
28
+ stopAuditions(): void
29
+ stopPreviewOutputs(): void
30
+ sourceCurrent(binding: ProductionBinding): boolean
31
+ }
32
+ export interface RecordingRangeDraft { version: string, voice: Extract<VoiceSource, { type: 'recording' }>, range?: TimeRange, startText?: string, endText?: string }
33
+
34
+ /** Source-bound media transactions, timing drafts and narration edits share the document actor. */
35
+ export class DocumentProduction {
36
+ readonly #audioImports = new WeakMap<File, { identity: string, version: string, id: string }>()
37
+ readonly #audioFingerprints = new WeakMap<File, Promise<string>>()
38
+ readonly #documentTimingDrafts = new DocumentTimingDrafts()
39
+ readonly #captionTimingDrafts = new CaptionTimingDrafts()
40
+ readonly #recordingRangeDrafts = new Map<string, Map<string, RecordingRangeDraft>>()
41
+ readonly #removedCueTimings = new Map<string, { pauseMs: number }>()
42
+ readonly #removedNarrations = new Map<string, { narration: Narration, override?: { narrationStart: 'with-animation' | 'after-animation' } }>()
43
+ #cueTimingScope?: string
44
+ readonly #options: ProductionOptions
45
+ constructor(options: ProductionOptions) { this.#options = options }
46
+ draftState(authoring: DocumentSessionState) {
47
+ const scope = JSON.stringify([authoring.identity, authoring.reset])
48
+ if (this.#cueTimingScope !== scope) { this.#removedCueTimings.clear(); this.#removedNarrations.clear(); this.#cueTimingScope = scope }
49
+ return {
50
+ hasDocumentTimingDrafts: Boolean(authoring.identity && authoring.content && this.#documentTimingDrafts.has(authoring.identity, authoring.content)),
51
+ documentTimingDraftStorageFailure: this.#documentTimingDrafts.failed,
52
+ captionTimingDraftSteps: authoring.identity && authoring.member?.id && authoring.content ? this.#captionTimingDrafts.steps({ identity: authoring.identity, memberId: authoring.member.id }, authoring.content) : [],
53
+ captionTimingDraftStorageFailure: Boolean(authoring.identity && authoring.member?.id && this.#captionTimingDrafts.failed({ identity: authoring.identity, memberId: authoring.member.id })),
54
+ }
55
+ }
56
+ reset(): void { this.#removedCueTimings.clear(); this.#removedNarrations.clear(); this.#cueTimingScope = undefined }
57
+ dispose(): void { this.reset(); this.#captionTimingDrafts.close(); this.#recordingRangeDrafts.clear() }
58
+ /** Native pending range input survives mode and document changes within this workbench. */
59
+ recordingRangeDrafts(identity: string): Map<string, RecordingRangeDraft> {
60
+ let values = this.#recordingRangeDrafts.get(identity)
61
+ if (!values) { values = new Map(); this.#recordingRangeDrafts.set(identity, values) }
62
+ return values
63
+ }
64
+
65
+ source(expected: { identity: string, version: string }): DocumentSessionState {
66
+ const source = this.#options.store.state.authoring
67
+ if (!source || !this.#options.document(expected.identity) || source.version !== expected.version || !source.fingerprint
68
+ || source.dirty || source.saving || source.composing || source.restored || source.error || isAgentChanging(this.#options.store.state)) throw new Error('production-source-changed')
69
+ return source
70
+ }
71
+ /** Inspects the saved declaration, including a local asset not yet attached to a step. */
72
+ async inspectAudio(assetId: string, expected: { identity: string, version: string }, signal?: AbortSignal): Promise<DocumentAudioInspectionAnswer> {
73
+ const source = this.source(expected), scope = this.#options.scope()
74
+ const answer = await this.#options.client.readDocumentAudio(source.directory, assetId, expected.version, signal)
75
+ signal?.throwIfAborted()
76
+ 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')
77
+ this.source(expected)
78
+ return answer
79
+ }
80
+ /** Recognition keeps its captured source even if subsequent author input moves to a newer version. */
81
+ async startTranscription(request: Omit<DocumentTranscriptionRequest, 'source'>, expected: { identity: string, version: string }, signal?: AbortSignal): Promise<TranscriptionTask> {
82
+ const source = this.source(expected)
83
+ if (!source.content || !source.fingerprint) throw new Error('production-source-changed')
84
+ const sent: DocumentTranscriptionRequest = { ...structuredClone(request), source: { documentId: source.content.id, version: expected.version, fingerprint: source.fingerprint } }
85
+ const answer = await this.#options.client.startTranscription(source.directory, sent, signal)
86
+ signal?.throwIfAborted()
87
+ if (answer.document !== source.directory || answer.intent !== sent.intent || answer.source.documentId !== sent.source.documentId
88
+ || 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')
89
+ return answer
90
+ }
91
+ async planTranscription(draft: TranscriptionAdoptionDraft, expected: { identity: string, version: string }, signal?: AbortSignal): Promise<TranscriptionAdoptionPlan> {
92
+ const source = this.source(expected), sent = structuredClone(draft)
93
+ if (sent.source.documentId !== source.content?.id || sent.source.version !== expected.version || sent.source.fingerprint !== source.fingerprint) throw new Error('production-source-changed')
94
+ const answer = await this.#options.client.planTranscriptionAdoption(source.directory, sent, signal)
95
+ signal?.throwIfAborted()
96
+ this.source(expected)
97
+ if (answer.document !== source.directory || answer.changeId !== sent.id || answer.source.documentId !== sent.source.documentId
98
+ || answer.source.version !== sent.source.version || answer.source.fingerprint !== sent.source.fingerprint || answer.candidate.id !== sent.candidate.id
99
+ || answer.candidate.revision !== sent.candidate.revision || answer.draftRevision !== sent.draftRevision) throw new Error('transcription-response-changed')
100
+ return answer
101
+ }
102
+ async adoptTranscription(request: TranscriptionAdoptionApply, expected: { identity: string, version: string }, signal?: AbortSignal): Promise<TranscriptionAdoptionAnswer> {
103
+ const source = this.source(expected), sent = structuredClone(request)
104
+ if (sent.source.documentId !== source.content?.id || sent.source.version !== expected.version || sent.source.fingerprint !== source.fingerprint) throw new Error('production-source-changed')
105
+ const answer = await this.#writeDocumentMedia(expected, directory => this.#options.client.applyTranscriptionAdoption(directory, sent, signal), signal)
106
+ if (answer.adoption.planId !== sent.planId || answer.adoption.candidate.id !== sent.candidate.id || answer.adoption.candidate.revision !== sent.candidate.revision
107
+ || answer.adoption.draftRevision !== sent.draftRevision) throw new Error('transcription-response-changed')
108
+ return answer
109
+ }
110
+ async #writeDocumentMedia<T extends DocumentChangeAnswer>(expected: { identity: string, version: string }, write: (directory: string) => Promise<T>, signal?: AbortSignal): Promise<T> {
111
+ const scope = this.#options.scope(), source = this.source(expected)
112
+ this.#options.stopAuditions()
113
+ await this.#options.pause()
114
+ await this.#options.flush(signal)
115
+ signal?.throwIfAborted()
116
+ if (scope !== this.#options.scope()) throw new Error('production-source-changed')
117
+ this.source(expected)
118
+ let answer: T
119
+ try { answer = await write(source.directory) }
120
+ finally { await this.#options.session.refresh(source.directory) }
121
+ signal?.throwIfAborted()
122
+ if (scope !== this.#options.scope() || answer.document !== source.directory || answer.content.id !== source.content?.id) throw new Error('production-source-changed')
123
+ this.source({ identity: expected.identity, version: answer.version })
124
+ return answer
125
+ }
126
+ /** Import commits the original file and its declaration together, without creating a clip or narration. */
127
+ async importAudio(file: File, expected: { identity: string, version: string }, signal?: AbortSignal): Promise<DocumentAudioImportAnswer> {
128
+ let request: { identity: string, version: string, id: string } | undefined
129
+ const answer = await this.#writeDocumentMedia(expected, async directory => {
130
+ const existing = this.#audioImports.get(file), first = !existing || existing.identity !== expected.identity
131
+ request = first ? { identity: expected.identity, version: expected.version, id: randomUUID() } : existing!
132
+ this.#audioImports.set(file, request)
133
+ try { return await this.#options.client.importDocumentAudio(directory, file, { id: request.id, baseDocumentVersion: request.version }, signal) }
134
+ catch (error) {
135
+ if (first && error instanceof WorkspaceServerError && error.status >= 400 && error.status < 500 && this.#audioImports.get(file) === request) this.#audioImports.delete(file)
136
+ throw error
137
+ }
138
+ }, signal)
139
+ if (answer.changeId !== request?.id || answer.inspection.assetId !== answer.assetId || !answer.content.assets[answer.assetId]) throw new Error('production-source-changed')
140
+ if (this.#audioImports.get(file) === request) this.#audioImports.delete(file)
141
+ return answer
142
+ }
143
+ /** Reconcile an ambiguous save against real asset bytes before admitting a new source-bound import. */
144
+ async ensureAudio(file: File, expected: { identity: string, version: string }, signal?: AbortSignal): Promise<DocumentAudioInspectionAnswer> {
145
+ const source = this.source(expected)
146
+ if (!file.size || file.size > DOCUMENT_AUDIO_MAX_BYTES) throw new Error('production.asset-size')
147
+ let pending = this.#audioFingerprints.get(file)
148
+ if (!pending) {
149
+ pending = file.arrayBuffer().then(sha256)
150
+ this.#audioFingerprints.set(file, pending)
151
+ }
152
+ const hash = await pending
153
+ signal?.throwIfAborted(); this.source(expected)
154
+ for (const asset of Object.values(source.content?.assets ?? {})) {
155
+ if (asset.kind !== 'audio' || asset.fingerprint !== `sha256:${hash}`) continue
156
+ try {
157
+ const answer = await this.inspectAudio(asset.id, expected, signal)
158
+ if (answer.inspection.sha256 === hash) return answer
159
+ } catch (error) {
160
+ if (!(error instanceof WorkspaceServerError) || ![404, 413, 422].includes(error.status)) throw error
161
+ }
162
+ }
163
+ signal?.throwIfAborted(); this.source(expected)
164
+ const previous = this.#audioImports.get(file)
165
+ if (previous?.identity === expected.identity && previous.version !== expected.version) this.#audioImports.delete(file)
166
+ let imported: DocumentAudioImportAnswer
167
+ try { imported = await this.importAudio(file, expected, signal) }
168
+ catch (error) {
169
+ if (!previous || this.#audioImports.get(file) !== previous || !(error instanceof WorkspaceServerError)
170
+ || error.status !== 409 || error.message !== 'document-version-changed') throw error
171
+ signal?.throwIfAborted(); this.source(expected)
172
+ this.#audioImports.delete(file)
173
+ imported = await this.importAudio(file, expected, signal)
174
+ }
175
+ return { document: imported.document, version: imported.version, fingerprint: imported.fingerprint, inspection: imported.inspection }
176
+ }
177
+ /** The source actor owns this external transaction; capture is independent from preview output. */
178
+ async adoptRecording(request: DocumentRecordingAdoption, expected: { identity: string, version: string }, signal?: AbortSignal): Promise<VerifiedDocumentRecordingAdoption> {
179
+ const scope = this.#options.scope(), source = this.source(expected), sent = structuredClone(request)
180
+ if (sent.source.documentId !== source.content?.id || sent.source.version !== source.version || sent.source.fingerprint !== source.fingerprint
181
+ || !source.content.steps[sent.stepId] || !source.content.assets[sent.assetId]) throw new Error('production-source-changed')
182
+ this.#options.stopPreviewOutputs()
183
+ await this.#options.pause()
184
+ await this.#options.flush(signal)
185
+ signal?.throwIfAborted()
186
+ if (scope !== this.#options.scope()) throw new Error('production-source-changed')
187
+ this.source(expected)
188
+ const answer = await this.#options.session.commitExternal(expected, async (_before, ownedSignal) => {
189
+ const saved = await this.#options.client.adoptRecording(source.directory, sent, ownedSignal)
190
+ 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')
191
+ return saved
192
+ }, (before, remote, current) => reconcileRecordingAdoption(sent, before, remote, current), signal)
193
+ signal?.throwIfAborted()
194
+ if (scope !== this.#options.scope() || this.#options.store.state.authoring?.identity !== expected.identity) throw new Error('production-source-changed')
195
+ return answer
196
+ }
197
+ async applyMediaClip(clip: MediaClip, expected: { identity: string, version: string, binding: ProductionBinding }, signal?: AbortSignal): Promise<DocumentChangeAnswer> {
198
+ this.source(expected)
199
+ 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')
200
+ const id = randomUUID(), value = structuredClone(clip)
201
+ const answer = await this.#writeDocumentMedia(expected, directory => this.#options.client.changeDocumentMediaClip(directory, { id, baseDocumentVersion: expected.version, binding: expected.binding, clip: value }, signal), signal)
202
+ if (answer.changeId !== id) throw new Error('production-source-changed')
203
+ return answer
204
+ }
205
+ async removeMediaClip(clipId: string, expected: { identity: string, version: string }, signal?: AbortSignal): Promise<DocumentChangeAnswer> {
206
+ const id = randomUUID()
207
+ const answer = await this.#writeDocumentMedia(expected, directory => this.#options.client.changeDocumentMediaClip(directory, { id, baseDocumentVersion: expected.version, removeClipId: clipId }, signal), signal)
208
+ if (answer.changeId !== id) throw new Error('production-source-changed')
209
+ return answer
210
+ }
211
+ /** Applies a reviewed voice scope to the current source actor, without replacing body or narration. */
212
+ async applyVoice(scope: DocumentVoiceScope, value: VoiceSource | null, options: DocumentVoiceOptions, expected: { identity: string, version: string, fingerprint: string }): Promise<boolean> {
213
+ const source = this.#options.store.state.authoring, document = this.#options.document(expected.identity), context = this.#options.scope()
214
+ if (!source || !document || source.version !== expected.version || source.fingerprint !== expected.fingerprint
215
+ || source.dirty || source.saving || source.composing || source.restored || source.error || isAgentChanging(this.#options.store.state)) throw new Error('voice-context-changed')
216
+ const next = changeDocumentVoice(document, scope, value, options)
217
+ if (next === document) return true
218
+ this.#options.stopAuditions()
219
+ this.#options.session.record(next)
220
+ await this.#options.flush()
221
+ if (context !== this.#options.scope() || this.#options.store.state.authoring?.identity !== expected.identity) return false
222
+ const saved = this.#options.store.state.authoring
223
+ if (!saved || saved.dirty || saved.saving || saved.composing || saved.restored || saved.error) throw new Error('voice-save-unconfirmed')
224
+ return true
225
+ }
226
+
227
+ /** Manual caption times reuse the canonical action plan, media preflight and original source actor. */
228
+ async applyCaptionTiming(action: DocumentCaptionTimingAction, id: string, expected: { identity: string, version: string, fingerprint: string }, signal?: AbortSignal): Promise<DocumentChangeAnswer> {
229
+ const source = this.source(expected), scope = this.#options.scope()
230
+ if (!source.content || source.fingerprint !== expected.fingerprint) throw new Error('caption-source-changed')
231
+ const request = { id, source: componentSource({ content: source.content, version: source.version!, fingerprint: source.fingerprint! }), action }
232
+ const plan = await this.#options.client.planDocumentAction(source.directory, request, signal)
233
+ signal?.throwIfAborted()
234
+ if (scope !== this.#options.scope() || this.#options.store.state.authoring?.identity !== expected.identity || plan.effect.kind !== 'caption'
235
+ || plan.changeId !== id || JSON.stringify(plan.action) !== JSON.stringify(action) || !sameComponentSource(plan.source, request.source)) throw new Error('caption-source-changed')
236
+ this.#options.stopPreviewOutputs()
237
+ await this.#options.pause()
238
+ try {
239
+ return await this.#options.session.commitExternal(expected, async (_before, ownedSignal) => {
240
+ const receipt = await this.#options.client.applyDocumentAction(source.directory, { planId: plan.planId, digest: plan.digest, source: plan.source }, ownedSignal)
241
+ const actual = await this.#options.client.readDocument(source.directory)
242
+ if (receipt.planId !== plan.planId || receipt.digest !== plan.digest || receipt.changeId !== id || !receipt.verification.saved || !receipt.verification.actionStateVerified
243
+ || !sameComponentSource(receipt.verification.source, componentSource(actual))) throw new Error('document-confirmation')
244
+ return { ...actual, changeId: id, files: receipt.files }
245
+ }, (before, answer, current) => reconcileCaptionTiming(action, before, answer, current), signal)
246
+ } catch (error) {
247
+ const current = this.#options.store.state.authoring, file = plan.files.find(file => file.path === (source.directory ? `${source.directory}/lecture.json` : 'lecture.json'))
248
+ // Lost ACK was independently read by commitExternal; only the exact predicted saved version can settle it.
249
+ if (scope === this.#options.scope() && current?.identity === expected.identity && current.content && current.version === file?.version && current.fingerprint && !current.error) {
250
+ try {
251
+ 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 })) }
252
+ } catch { /* A newer target stays pending with the actual request error. */ }
253
+ }
254
+ throw error
255
+ }
256
+ }
257
+ /** Uses the current actor revision so timing edits cannot replace later body or narration input. */
258
+ setStepTiming(stepId: string, patch: StepTimingPatch, identity?: string): void {
259
+ const document = this.#options.document(identity)
260
+ if (!document || isAgentChanging(this.#options.store.state)) return
261
+ const next = changeStepTiming(document, stepId, patch)
262
+ if (next === document) return
263
+ this.#options.stopAuditions()
264
+ this.#options.session.record(next)
265
+ }
266
+ /** The whole document's caption size (`production.captions.scale`), recorded like a step's timing. */
267
+ setCaptionScale(scale: number, identity?: string): void {
268
+ const document = this.#options.document(identity)
269
+ if (!document || isAgentChanging(this.#options.store.state)) return
270
+ const next = changeDocumentCaptionScale(document, scale)
271
+ if (next === document) return
272
+ this.#options.session.record(next)
273
+ }
274
+ timingDraft(stepId: string, key: StepDurationKey, identity: string, reset: number): StepDurationDraft | undefined {
275
+ const document = this.#options.document(identity)
276
+ return document && this.#options.store.state.authoring?.reset === reset ? this.#documentTimingDrafts.get(identity, document, stepId, key) : undefined
277
+ }
278
+ /** Retains unaccepted numeric input by stable target; it is not authoring content or a second timing source. */
279
+ setTimingDraft(stepId: string, key: StepDurationKey, draft: StepDurationDraft | undefined, identity: string, reset: number): void {
280
+ const source = this.#options.store.state.authoring, document = this.#options.document(identity)
281
+ if (!document?.steps[stepId] || source?.reset !== reset || isAgentChanging(this.#options.store.state)) return
282
+ if (!this.#documentTimingDrafts.set(identity, document, stepId, key, draft)) return
283
+ this.#options.store.set({ authoring: { ...source }, hasDocumentTimingDrafts: this.#documentTimingDrafts.has(identity, document), documentTimingDraftStorageFailure: this.#documentTimingDrafts.failed })
284
+ }
285
+ captionTimingDraft(stepId: string, identity: string): CaptionTimingDraft | undefined {
286
+ const source = this.#options.store.state.authoring
287
+ return source?.identity === identity && source.member?.id ? this.#captionTimingDrafts.get({ identity, memberId: source.member.id }, stepId) : undefined
288
+ }
289
+ setCaptionTimingDraft(stepId: string, draft: CaptionTimingDraft | undefined, identity: string): void {
290
+ const source = this.#options.store.state.authoring, document = this.#options.document(identity)
291
+ if (!document?.steps[stepId] || source?.identity !== identity || !source.member?.id) return
292
+ const scope = { identity, memberId: source.member.id }
293
+ this.#captionTimingDrafts.set(scope, stepId, draft)
294
+ this.#options.store.set({ captionTimingDraftSteps: this.#captionTimingDrafts.steps(scope, document), captionTimingDraftStorageFailure: this.#captionTimingDrafts.failed(scope) })
295
+ }
296
+ copyNarrationCueTiming(from: string, to: string, identity: string): void {
297
+ const document = this.#options.document(identity)
298
+ if (!document || isAgentChanging(this.#options.store.state)) return
299
+ const timing = document.production.cueTimings?.[from] ?? this.#removedCueTimings.get(from)
300
+ if (timing) this.#removedCueTimings.set(to, { ...timing })
301
+ }
302
+ /** Native block identity and order own the current step's complete script. */
303
+ setNarrations(stepId: string, narrations: Narration[], identity?: string): void {
304
+ const document = this.#options.document(identity)
305
+ if (!document || isAgentChanging(this.#options.store.state) || !Object.hasOwn(document.steps, stepId)) return
306
+ const step = document.steps[stepId]!
307
+ if (!document.sections[step.sectionId]?.stepOrder.includes(stepId)) return
308
+ const values = narrations.map(narration => {
309
+ const previous = document.narrations[narration.id] ?? this.#removedNarrations.get(narration.id)?.narration
310
+ return { ...previous, ...narration }
311
+ })
312
+ if (JSON.stringify(step.narrationIds.map(id => document.narrations[id])) === JSON.stringify(values)) return
313
+ const next = replaceStepNarrations(document, stepId, values)
314
+ for (const narration of values) {
315
+ const override = this.#removedNarrations.get(narration.id)?.override
316
+ if (override && !document.narrations[narration.id]) next.production.narrationOverrides = { ...next.production.narrationOverrides, [narration.id]: { ...override } }
317
+ }
318
+ const cueIds = new Set(values.flatMap(narration => narration.content.flatMap(node => node.type === 'cue' ? [node.id] : [])))
319
+ for (const cue of cueIds) {
320
+ const timing = this.#removedCueTimings.get(cue)
321
+ if (timing && !Object.hasOwn(next.production.cueTimings ?? {}, cue)) next.production.cueTimings = { ...next.production.cueTimings, [cue]: { ...timing } }
322
+ }
323
+ const references = (values: Narration[]) => JSON.stringify(values.map(narration => [narration.id, narration.provenance, narration.content.filter(node => node.type === 'cue')]))
324
+ if (references(step.narrationIds.map(id => document.narrations[id]!)) !== references(values)
325
+ && validateDocument(next).issues.some(issue => issue.severity === 'error')) throw new Error('document-narration-content')
326
+ for (const id of step.narrationIds) if (!next.steps[stepId]!.narrationIds.includes(id)) {
327
+ this.#removedNarrations.delete(id)
328
+ this.#removedNarrations.set(id, { narration: document.narrations[id]!, override: document.production.narrationOverrides?.[id] })
329
+ }
330
+ for (const [id, timing] of Object.entries(document.production.cueTimings ?? {})) if (!Object.hasOwn(next.production.cueTimings ?? {}, id)) {
331
+ this.#removedCueTimings.delete(id)
332
+ this.#removedCueTimings.set(id, { ...timing })
333
+ }
334
+ while (this.#removedNarrations.size > 512) this.#removedNarrations.delete(this.#removedNarrations.keys().next().value!)
335
+ while (this.#removedCueTimings.size > 512) this.#removedCueTimings.delete(this.#removedCueTimings.keys().next().value!)
336
+ this.#options.stopAuditions()
337
+ this.#options.session.record(next)
338
+ }
339
+ }