@lincyaw/workbench 0.2.0-rc.3 → 0.2.0-rc.31

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 (57) hide show
  1. package/package.json +5 -4
  2. package/src/MediaClipEditor.tsx +1 -1
  3. package/src/authoring-client.ts +3 -3
  4. package/src/authoring-draft-fragment.ts +3 -1
  5. package/src/component-property-drafts.ts +5 -5
  6. package/src/components.tsx +10 -28
  7. package/src/document-drafts.ts +6 -7
  8. package/src/document-editor/BlockNoteSurface.tsx +25 -9
  9. package/src/document-editor/ComponentMaterials.tsx +3 -9
  10. package/src/document-editor/ComponentPropertiesDialog.tsx +3 -3
  11. package/src/document-editor/DocumentBlockPreview.tsx +2 -0
  12. package/src/document-editor/DocumentComments.tsx +58 -9
  13. package/src/document-editor/DocumentEditor.tsx +44 -7
  14. package/src/document-editor/DocumentStepNarration.tsx +24 -19
  15. package/src/document-editor/NarrationEditor.tsx +54 -20
  16. package/src/document-editor/NewCommentDrafts.tsx +4 -3
  17. package/src/document-editor/PlaceComment.tsx +150 -0
  18. package/src/document-editor/comment-drafts.ts +16 -14
  19. package/src/document-editor/comments.ts +30 -10
  20. package/src/document-editor/component-label.ts +11 -0
  21. package/src/document-editor/file-input.ts +2 -2
  22. package/src/document-editor/focus.ts +0 -2
  23. package/src/document-editor/insertion-place.ts +60 -0
  24. package/src/document-editor/messages.ts +3 -3
  25. package/src/document-editor/narration-schema.tsx +12 -6
  26. package/src/document-editor/narration.ts +19 -3
  27. package/src/document-editor/page-body.ts +60 -0
  28. package/src/document-editor/projection.ts +2 -2
  29. package/src/document-editor/rich-text.ts +3 -2
  30. package/src/document-editor/selection.ts +13 -0
  31. package/src/document-editor/style.css +12 -1
  32. package/src/document-editor/table-identities.ts +2 -1
  33. package/src/document-session.ts +50 -16
  34. package/src/history.ts +2 -2
  35. package/src/index.ts +0 -3
  36. package/src/messages.ts +19 -4
  37. package/src/panels/DocumentView.tsx +10 -10
  38. package/src/panels/ExportPanel.tsx +1 -6
  39. package/src/panels/PreviewPanel.tsx +1 -4
  40. package/src/panels/ProductionCaptionSize.tsx +20 -0
  41. package/src/panels/ProductionCaptionTiming.tsx +3 -3
  42. package/src/panels/ProductionMediaClips.tsx +2 -1
  43. package/src/panels/ProductionPanel.tsx +5 -1
  44. package/src/panels/ProductionRecordingRange.tsx +2 -2
  45. package/src/panels/ProductionTimeline.tsx +6 -1
  46. package/src/panels/ProductionTranscription.tsx +2 -1
  47. package/src/panels/SourceDocumentPanel.tsx +3 -6
  48. package/src/panels/TranscriptionAdoption.tsx +7 -6
  49. package/src/places.ts +1 -36
  50. package/src/production-messages.ts +6 -0
  51. package/src/sha256.ts +9 -0
  52. package/src/styles.css +16 -41
  53. package/src/workbench.ts +124 -88
  54. package/src/workspace-client.ts +2 -1
  55. package/src/assistant-mascot.ts +0 -62
  56. package/src/mascot-preference.ts +0 -37
  57. package/src/panels/ChangeTrail.tsx +0 -79
package/src/workbench.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { applyDocumentCaptionTiming } from '@lincyaw/kernel'
1
+ import { applyDocumentCaptionTiming, changeDocumentCaptionScale, randomUUID, replaceStepNarrations, validateDocument } from '@lincyaw/kernel'
2
2
  import type { DocumentCaptionTimingAction } from '@lincyaw/kernel'
3
3
  import { reconcileCaptionTiming } from './caption-timing.ts'
4
4
  import type { ProjectOperation, ProjectPlan } from '@lincyaw/kernel'
@@ -15,6 +15,7 @@ import { mutateCommentDrafts } from './document-editor/comment-draft-actions.ts'
15
15
  import { inspectComponentDrafts } from './component-draft-inspection.ts'
16
16
  import { mutateComponentDrafts } from './component-draft-actions.ts'
17
17
  import { DocumentSession } from './document-session.ts'
18
+ import { sha256 } from './sha256.ts'
18
19
  import { reconcileRecordingAdoption } from './recording-adoption.ts'
19
20
  import { disposeTranscriptionSessions } from './transcription-session.ts'
20
21
  import { documentRecordingActive, disposeDocumentRecordings } from './audio-recording.ts'
@@ -23,10 +24,11 @@ import type { DocumentSessionState } from './document-session.ts'
23
24
  import type { DocumentDraftReference } from './document-drafts.ts'
24
25
  import { DocumentReturnPoints, documentReturnStep, documentViewportOffset, restoreDocumentViewport } from './document-return-point.ts'
25
26
  import type { DocumentReturnPoint } from './document-return-point.ts'
26
- import type { DocumentRecordingAdoption, VerifiedDocumentRecordingAdoption, DocumentAudioImportAnswer, DocumentAudioInspectionAnswer, DocumentChangeAnswer, MediaClip, NarrationNode, TeachingDocument, DocumentTranscriptionRequest, TranscriptionTask, TranscriptionAdoptionDraft, TranscriptionAdoptionApply, TranscriptionAdoptionPlan, TranscriptionAdoptionAnswer } from '@lincyaw/kernel'
27
+ import type { DocumentRecordingAdoption, VerifiedDocumentRecordingAdoption, DocumentAudioImportAnswer, DocumentAudioInspectionAnswer, DocumentChangeAnswer, MediaClip, Narration, TeachingDocument, DocumentTranscriptionRequest, TranscriptionTask, TranscriptionAdoptionDraft, TranscriptionAdoptionApply, TranscriptionAdoptionPlan, TranscriptionAdoptionAnswer } from '@lincyaw/kernel'
27
28
  import type { NativeDocumentEditor } from './document-editor/schema.tsx'
28
29
  import type { DocumentEditorCommands } from './document-editor/DocumentEditor.tsx'
29
- import { nativeTextPoint, nativeTextPosition } from './document-editor/selection.ts'
30
+ import type { DocumentInsertionPlace } from './document-editor/insertion-place.ts'
31
+ import { nativeBlockText, nativeTextPoint, nativeTextPosition } from './document-editor/selection.ts'
30
32
  import type { NativeTextPoint } from './document-editor/selection.ts'
31
33
  import type { DocumentFieldBookmark, DocumentFieldRegistration, DocumentHistoryEditor } from './document-editor/focus.ts'
32
34
  import { duplicateDocumentPage, moveDocumentPage, pageDeletion, removeDocumentPage, renameDocumentPage } from '@lincyaw/kernel'
@@ -48,7 +50,7 @@ import { DocumentJobs } from './jobs.ts'
48
50
  import { VoiceoverJobs } from './voiceover-jobs.ts'
49
51
  import { contentText, localeOf, MESSAGES } from './messages.ts'
50
52
  import type { AgentPlace, Locale } from './messages.ts'
51
- import { changedBlocks, changedPlaces, findPlaceAtLine, findPlaceSource } from './places.ts'
53
+ import { changedPlaces, findPlaceAtLine, findPlaceSource } from './places.ts'
52
54
  import { OperationLog } from './telemetry.ts'
53
55
  import type { OperationValue } from './telemetry.ts'
54
56
  import { PreviewPlayback } from './preview-playback.ts'
@@ -99,6 +101,12 @@ export interface AgentRequest {
99
101
  focus: FocusReport[]
100
102
  }
101
103
 
104
+ /** A request for the author to finish and send in the conversation, and what it is about: the host only shows it. */
105
+ export interface AgentDraft {
106
+ text: string
107
+ focus: FocusReport[]
108
+ }
109
+
102
110
  /** Where the host put a request: the conversation's session and the request's id, which its user message carries. */
103
111
  export interface AgentAnswer {
104
112
  session: string
@@ -162,10 +170,6 @@ export interface WorkbenchState {
162
170
  isRepairingDiagnostics: boolean
163
171
  diagnosticsRepairFailure?: string
164
172
  pointer?: Pointer
165
- /** Where the blocks the AI just changed are in the preview, for the kitten to walk to; `id` is new for each change. */
166
- changeTrail?: { id: number, rects: PreviewRect[] }
167
- /** The places the AI just changed, for the kitten in the editor to walk to; `id` is new for each change. */
168
- changedPlaces?: { id: number, targets: PreviewAnchor[] }
169
173
  save: SaveState
170
174
  /** What the conversation's agent is told the author points at. */
171
175
  focus: FocusReport[]
@@ -213,7 +217,7 @@ export function selectedDocument(state: WorkbenchState): string | null {
213
217
 
214
218
  /** Whether the AI is changing the workspace now, or its change waits for the author. */
215
219
  export function isAgentChanging(state: WorkbenchState): boolean {
216
- return Boolean(state.review && (state.review.running || state.review.changes.length > 0))
220
+ return Boolean(state.review?.engine === 'git' && (state.review.running || state.review.changes.length > 0))
217
221
  }
218
222
 
219
223
  /** Whether the document has a slides form (no renderers listed: every form). */
@@ -275,6 +279,7 @@ export class Workbench {
275
279
  readonly #documentTimingDrafts = new DocumentTimingDrafts()
276
280
  readonly #captionTimingDrafts = new CaptionTimingDrafts()
277
281
  readonly #removedCueTimings = new Map<string, { pauseMs: number }>()
282
+ readonly #removedNarrations = new Map<string, { narration: Narration, override?: { narrationStart: 'with-animation' | 'after-animation' } }>()
278
283
  #cueTimingScope?: string
279
284
  #documentEditor?: NativeDocumentEditor
280
285
  #documentEditorCommands?: DocumentEditorCommands
@@ -307,6 +312,7 @@ export class Workbench {
307
312
  readonly #jobs: DocumentJobs
308
313
  readonly #voiceover: VoiceoverJobs
309
314
  readonly #askers = new Set<(request: AgentRequest) => Promise<AgentAnswer | void>>()
315
+ readonly #drafters = new Set<(draft: AgentDraft) => void | Promise<void>>()
310
316
  readonly #previewSinks = new Set<PreviewSink>()
311
317
  readonly #playbackKeys = new Set<(command: PlaybackKey) => void>()
312
318
  #unsubscribeIr?: () => void
@@ -329,7 +335,6 @@ export class Workbench {
329
335
  #pendingReveal?: PreviewAnchor
330
336
  /** The AI's changes to show on the document once the preview has them. */
331
337
  #pendingFlash?: ReviewChange[]
332
- #trail = 0
333
338
  #lastPosition?: string
334
339
  #awaitingView?: WebView
335
340
  /** The forms asked for ahead of the author (`web:<dir>`, `slides:<dir>`), and the wait before asking for the slides. */
@@ -374,7 +379,7 @@ export class Workbench {
374
379
  || pendingField.field.blockId !== undefined && !authoring.content?.blocks[pendingField.field.blockId]
375
380
  || !authoring.content?.steps[pendingField.field.stepId])) this.#pendingDocumentField = undefined
376
381
  const cueTimingScope = JSON.stringify([authoring.identity, authoring.reset])
377
- if (this.#cueTimingScope !== cueTimingScope) { this.#removedCueTimings.clear(); this.#cueTimingScope = cueTimingScope }
382
+ if (this.#cueTimingScope !== cueTimingScope) { this.#removedCueTimings.clear(); this.#removedNarrations.clear(); this.#cueTimingScope = cueTimingScope }
378
383
  const previous = this.#state.authoring
379
384
  // A saved timestamp belongs to this client's acknowledged source version.
380
385
  // Remote adoption has no server modification time to display in its place.
@@ -416,12 +421,6 @@ export class Workbench {
416
421
  && previous?.identity === authoring.identity && Boolean(previous?.version && authoring.version)
417
422
  && (previous?.version !== authoring.version || previous?.fingerprint !== authoring.fingerprint)
418
423
  if (!authoring.dirty && previous?.dirty || acknowledgedSourceChanged) { void this.#history.read(authoring.directory); void this.loadDocuments() }
419
- // The source changed under an author with nothing unsaved while the AI holds a review: the AI changed it. The
420
- // editor's kitten walks to the blocks whose content differs.
421
- if (acknowledgedSourceChanged && !previous?.dirty && !previous?.saving && this.#state.review && previous?.content && authoring.content) {
422
- const targets = changedBlocks(previous.content, authoring.content)
423
- if (targets.length) this.#set({ changedPlaces: { id: ++this.#trail, targets } })
424
- }
425
424
  })
426
425
  this.#diagnostics = new DocumentDiagnostics(this.client, store)
427
426
  this.#jobs = new DocumentJobs(this.client, store)
@@ -506,6 +505,12 @@ export class Workbench {
506
505
  return () => this.#askers.delete(asker)
507
506
  }
508
507
 
508
+ /** The host's way to put a request in the conversation's composer for the author to finish; nothing is sent. */
509
+ onAgentDraft = (drafter: (draft: AgentDraft) => void | Promise<void>): (() => void) => {
510
+ this.#drafters.add(drafter)
511
+ return () => this.#drafters.delete(drafter)
512
+ }
513
+
509
514
  #nextDocumentPosition(): string { return `${this.#positionSeed}:${++this.#positionSequence}` }
510
515
 
511
516
  #restoredDocumentPosition(source: DocumentSessionState): boolean {
@@ -557,7 +562,7 @@ export class Workbench {
557
562
  }
558
563
 
559
564
  planDocumentOperation(operation: ProjectOperation): Promise<ProjectPlan> {
560
- return this.client.planProjectChange({ id: crypto.randomUUID(), operation })
565
+ return this.client.planProjectChange({ id: randomUUID(), operation })
561
566
  }
562
567
 
563
568
  operateDocument(operation: ProjectOperation, planned?: ProjectPlan): Promise<OperationAnswer> {
@@ -790,7 +795,6 @@ export class Workbench {
790
795
  this.#pendingFlash = undefined
791
796
  const targets = changedPlaces(ir.doc, changes)
792
797
  if (targets.length === 0) return
793
- this.#trail++
794
798
  this.#broadcast({ type: 'flash', targets })
795
799
  this.#record('ai.change.shown', { 'presmium.places': targets.map(target => [target.section, target.block].filter(Boolean).join(':')).join('\n') })
796
800
  }
@@ -1400,7 +1404,7 @@ export class Workbench {
1400
1404
  let request: { identity: string, version: string, id: string } | undefined
1401
1405
  const answer = await this.#writeDocumentMedia(expected, async directory => {
1402
1406
  const existing = this.#audioImports.get(file), first = !existing || existing.identity !== expected.identity
1403
- request = first ? { identity: expected.identity, version: expected.version, id: crypto.randomUUID() } : existing!
1407
+ request = first ? { identity: expected.identity, version: expected.version, id: randomUUID() } : existing!
1404
1408
  this.#audioImports.set(file, request)
1405
1409
  try { return await this.client.importDocumentAudio(directory, file, { id: request.id, baseDocumentVersion: request.version }, signal) }
1406
1410
  catch (error) {
@@ -1418,7 +1422,7 @@ export class Workbench {
1418
1422
  if (!file.size || file.size > DOCUMENT_AUDIO_MAX_BYTES) throw new Error('production.asset-size')
1419
1423
  let pending = this.#audioFingerprints.get(file)
1420
1424
  if (!pending) {
1421
- pending = file.arrayBuffer().then(bytes => crypto.subtle.digest('SHA-256', bytes)).then(bytes => [...new Uint8Array(bytes)].map(value => value.toString(16).padStart(2, '0')).join(''))
1425
+ pending = file.arrayBuffer().then(sha256)
1422
1426
  this.#audioFingerprints.set(file, pending)
1423
1427
  }
1424
1428
  const hash = await pending
@@ -1469,13 +1473,13 @@ export class Workbench {
1469
1473
  async applyDocumentMediaClip(clip: MediaClip, expected: { identity: string, version: string, binding: ProductionBinding }, signal?: AbortSignal): Promise<DocumentChangeAnswer> {
1470
1474
  this.#mediaSource(expected)
1471
1475
  if (!this.#state.productionPlan || !sameProductionBinding(this.#state.productionPlan.binding, expected.binding) || !this.#productionSourceCurrent(expected.binding)) throw new Error('production-source-changed')
1472
- const id = crypto.randomUUID(), value = structuredClone(clip)
1476
+ const id = randomUUID(), value = structuredClone(clip)
1473
1477
  const answer = await this.#writeDocumentMedia(expected, directory => this.client.changeDocumentMediaClip(directory, { id, baseDocumentVersion: expected.version, binding: expected.binding, clip: value }, signal), signal)
1474
1478
  if (answer.changeId !== id) throw new Error('production-source-changed')
1475
1479
  return answer
1476
1480
  }
1477
1481
  async removeDocumentMediaClip(clipId: string, expected: { identity: string, version: string }, signal?: AbortSignal): Promise<DocumentChangeAnswer> {
1478
- const id = crypto.randomUUID()
1482
+ const id = randomUUID()
1479
1483
  const answer = await this.#writeDocumentMedia(expected, directory => this.client.changeDocumentMediaClip(directory, { id, baseDocumentVersion: expected.version, removeClipId: clipId }, signal), signal)
1480
1484
  if (answer.changeId !== id) throw new Error('production-source-changed')
1481
1485
  return answer
@@ -1584,6 +1588,14 @@ export class Workbench {
1584
1588
  this.stopAuditions()
1585
1589
  this.#documentSession.record(next)
1586
1590
  }
1591
+ /** The whole document's caption size (`production.captions.scale`), recorded like a step's timing. */
1592
+ setDocumentCaptionScale(scale: number, identity?: string): void {
1593
+ const document = this.getDocument(identity)
1594
+ if (!document || isAgentChanging(this.#state)) return
1595
+ const next = changeDocumentCaptionScale(document, scale)
1596
+ if (next === document) return
1597
+ this.#documentSession.record(next)
1598
+ }
1587
1599
  documentTimingDraft(stepId: string, key: StepDurationKey, identity: string, reset: number): StepDurationDraft | undefined {
1588
1600
  const document = this.getDocument(identity)
1589
1601
  return document && this.#state.authoring?.reset === reset ? this.#documentTimingDrafts.get(identity, document, stepId, key) : undefined
@@ -1606,59 +1618,48 @@ export class Workbench {
1606
1618
  this.#captionTimingDrafts.set(scope, stepId, draft)
1607
1619
  this.#set({ captionTimingDraftSteps: this.#captionTimingDrafts.steps(scope, document), captionTimingDraftStorageFailure: this.#captionTimingDrafts.failed(scope) })
1608
1620
  }
1609
- /** Changes one stable narration target without replacing the body editor or derived captions. */
1610
- setDocumentNarration(stepId: string, narrationId: string | undefined, content: NarrationNode[], identity?: string): string | undefined {
1621
+ copyNarrationCueTiming(from: string, to: string, identity: string): void {
1611
1622
  const document = this.getDocument(identity)
1612
- if (!document || isAgentChanging(this.#state) || !Object.hasOwn(document.steps, stepId)) return undefined
1623
+ if (!document || isAgentChanging(this.#state)) return
1624
+ const timing = document.production.cueTimings?.[from] ?? this.#removedCueTimings.get(from)
1625
+ if (timing) this.#removedCueTimings.set(to, { ...timing })
1626
+ }
1627
+ /** Native block identity and order own the current step's complete script. */
1628
+ setDocumentNarrations(stepId: string, narrations: Narration[], identity?: string): void {
1629
+ const document = this.getDocument(identity)
1630
+ if (!document || isAgentChanging(this.#state) || !Object.hasOwn(document.steps, stepId)) return
1613
1631
  const step = document.steps[stepId]!
1614
- if (!document.sections[step.sectionId]?.stepOrder.includes(stepId)) return undefined
1615
- if (!Array.isArray(content)) throw new Error('document-narration-content')
1616
- let meaningful = false
1617
- for (const node of content) {
1618
- if (!node || typeof node !== 'object') throw new Error('document-narration-content')
1619
- if (node.type === 'text' && typeof node.text === 'string') meaningful ||= Boolean(node.text.trim())
1620
- else if (node.type === 'pronunciation' && typeof node.display === 'string' && typeof node.spoken === 'string') meaningful ||= Boolean(node.display.trim() || node.spoken.trim())
1621
- else if (node.type === 'cue' && typeof node.id === 'string' && node.id && typeof node.action === 'string' && node.action) meaningful = true
1622
- else throw new Error('document-narration-content')
1623
- }
1624
- const target = narrationId ?? step.narrationIds[0]
1625
- const existing = target !== undefined && Object.hasOwn(document.narrations, target) ? document.narrations[target] : undefined
1626
- if (existing && !step.narrationIds.includes(existing.id)) throw new Error('document-narration-target')
1627
- if (!existing && !meaningful) return undefined
1628
- const id = target ?? crypto.randomUUID()
1629
- if (!id || id.length > 200 || /[\u0000-\u001f]/.test(id) || ['__proto__', 'constructor', 'prototype'].includes(id)) throw new Error('document-narration-target')
1630
- if (!existing && (document.id === id || [document.sections, document.blocks, document.steps, document.assets, document.definitions].some(map => Object.hasOwn(map, id)))) throw new Error('document-narration-target')
1631
- if (existing && JSON.stringify(existing.content) === JSON.stringify(content)) return id
1632
- const next: TeachingDocument = { ...document,
1633
- narrations: { ...document.narrations, [id]: { ...existing, id, content: structuredClone(content) } },
1634
- ...step.narrationIds.includes(id) ? {} : { steps: { ...document.steps, [stepId]: { ...step, narrationIds: [...step.narrationIds, id] } } },
1635
- }
1636
- const cueIds = new Set(content.flatMap(node => node.type === 'cue' ? [node.id] : []))
1637
- const previousCues = new Set(existing?.content.flatMap(node => node.type === 'cue' ? [node.id] : []) ?? [])
1638
- if (document.production.cueTimings) {
1639
- const removed = [...previousCues].filter(cue => !cueIds.has(cue) && Object.hasOwn(document.production.cueTimings!, cue)
1640
- && !Object.values(next.narrations).some(narration => narration.content.some(node => node.type === 'cue' && node.id === cue)))
1641
- if (removed.length) {
1642
- const cueTimings = { ...document.production.cueTimings }
1643
- for (const cue of removed) {
1644
- this.#removedCueTimings.delete(cue)
1645
- this.#removedCueTimings.set(cue, { ...cueTimings[cue]! })
1646
- delete cueTimings[cue]
1647
- }
1648
- while (this.#removedCueTimings.size > 512) this.#removedCueTimings.delete(this.#removedCueTimings.keys().next().value!)
1649
- next.production = { ...document.production, cueTimings }
1650
- }
1632
+ if (!document.sections[step.sectionId]?.stepOrder.includes(stepId)) return
1633
+ const values = narrations.map(narration => {
1634
+ const previous = document.narrations[narration.id] ?? this.#removedNarrations.get(narration.id)?.narration
1635
+ return { ...previous, ...narration }
1636
+ })
1637
+ if (JSON.stringify(step.narrationIds.map(id => document.narrations[id])) === JSON.stringify(values)) return
1638
+ const next = replaceStepNarrations(document, stepId, values)
1639
+ for (const narration of values) {
1640
+ const override = this.#removedNarrations.get(narration.id)?.override
1641
+ if (override && !document.narrations[narration.id]) next.production.narrationOverrides = { ...next.production.narrationOverrides, [narration.id]: { ...override } }
1651
1642
  }
1643
+ const cueIds = new Set(values.flatMap(narration => narration.content.flatMap(node => node.type === 'cue' ? [node.id] : [])))
1652
1644
  for (const cue of cueIds) {
1653
1645
  const timing = this.#removedCueTimings.get(cue)
1654
- if (!previousCues.has(cue) && timing && !Object.hasOwn(next.production.cueTimings ?? {}, cue)) {
1655
- next.production = { ...next.production, cueTimings: { ...next.production.cueTimings, [cue]: { ...timing } } }
1656
- this.#removedCueTimings.delete(cue)
1657
- }
1646
+ if (timing && !Object.hasOwn(next.production.cueTimings ?? {}, cue)) next.production.cueTimings = { ...next.production.cueTimings, [cue]: { ...timing } }
1647
+ }
1648
+ const references = (values: Narration[]) => JSON.stringify(values.map(narration => [narration.id, narration.provenance, narration.content.filter(node => node.type === 'cue')]))
1649
+ if (references(step.narrationIds.map(id => document.narrations[id]!)) !== references(values)
1650
+ && validateDocument(next).issues.some(issue => issue.severity === 'error')) throw new Error('document-narration-content')
1651
+ for (const id of step.narrationIds) if (!next.steps[stepId]!.narrationIds.includes(id)) {
1652
+ this.#removedNarrations.delete(id)
1653
+ this.#removedNarrations.set(id, { narration: document.narrations[id]!, override: document.production.narrationOverrides?.[id] })
1654
+ }
1655
+ for (const [id, timing] of Object.entries(document.production.cueTimings ?? {})) if (!Object.hasOwn(next.production.cueTimings ?? {}, id)) {
1656
+ this.#removedCueTimings.delete(id)
1657
+ this.#removedCueTimings.set(id, { ...timing })
1658
1658
  }
1659
+ while (this.#removedNarrations.size > 512) this.#removedNarrations.delete(this.#removedNarrations.keys().next().value!)
1660
+ while (this.#removedCueTimings.size > 512) this.#removedCueTimings.delete(this.#removedCueTimings.keys().next().value!)
1659
1661
  this.stopAuditions()
1660
1662
  this.#documentSession.record(next)
1661
- return id
1662
1663
  }
1663
1664
  recordDocument(content: TeachingDocument): void { if (!isAgentChanging(this.#state)) this.#documentSession.record(content) }
1664
1665
  documentComposition(composing: boolean, owner = 'body', identity?: string): void { this.#documentSession.composition(composing, owner, identity) }
@@ -1675,31 +1676,30 @@ export class Workbench {
1675
1676
  this.#documentSelectionGeneration++; this.#cancelOpeningDocument(); this.#stopDocumentReturnScroll?.()
1676
1677
  if (this.#state.documentPositionFailure) this.#set({ documentPositionFailure: undefined })
1677
1678
  }
1678
- pendingDocumentField(blockId: string | undefined, identity?: string): Pick<DocumentFieldRegistration, 'stepId' | 'narrationId' | 'slot'> | undefined {
1679
+ pendingDocumentField(blockId: string | undefined, identity?: string): Pick<DocumentFieldRegistration, 'stepId'> | undefined {
1679
1680
  const pending = this.#pendingDocumentField, source = this.#state.authoring
1680
1681
  return pending && pending.generation === this.#documentSelectionGeneration && pending.field.blockId === blockId
1681
1682
  && pending.field.identity === (identity ?? source?.identity) && pending.reset === source?.reset ? pending.field : undefined
1682
1683
  }
1683
1684
  /** A live native field owns its target only while it still has focus in this source. */
1684
- focusedDocumentField(): Readonly<Pick<DocumentFieldRegistration, 'identity' | 'reset' | 'blockId' | 'stepId' | 'narrationId' | 'slot'>> & { readonly editor: DocumentHistoryEditor } | undefined {
1685
+ focusedDocumentField(): Readonly<Pick<DocumentFieldRegistration, 'identity' | 'reset' | 'blockId' | 'stepId'>> & { readonly editor: DocumentHistoryEditor } | undefined {
1685
1686
  const source = this.#state.authoring, editor = this.#activeDocumentHistory
1686
1687
  if (!source?.identity || !source.content || !editor || editor === this.#documentEditor || editor.prosemirrorView.isDestroyed || !editor.prosemirrorView.hasFocus()) return
1687
1688
  for (const field of this.#documentFields.values()) {
1688
1689
  if (field.editor !== editor || field.identity !== source.identity || field.reset !== source.reset) continue
1689
1690
  const step = source.content.steps[field.stepId], section = step && source.content.sections[step.sectionId]
1690
- if (!step || !section?.stepOrder.includes(field.stepId) || field.slot < 0 || field.slot >= Math.max(1, step.narrationIds.length)
1691
- || step.narrationIds[field.slot] !== undefined && step.narrationIds[field.slot] !== field.narrationId) return
1692
- const { identity, reset, blockId, stepId, narrationId, slot } = field
1693
- return { identity, reset, blockId, stepId, narrationId, slot, editor }
1691
+ if (!step || !section?.stepOrder.includes(field.stepId)) return
1692
+ const { identity, reset, blockId, stepId } = field
1693
+ return { identity, reset, blockId, stepId, editor }
1694
1694
  }
1695
1695
  return undefined
1696
1696
  }
1697
1697
  registerDocumentField(field: DocumentFieldRegistration): () => void {
1698
1698
  if (field.identity !== this.#state.authoring?.identity || field.reset !== this.#state.authoring.reset) return () => {}
1699
- const key = JSON.stringify([field.identity, field.blockId, field.stepId, field.slot, field.narrationId])
1699
+ const key = JSON.stringify([field.identity, field.blockId, field.stepId])
1700
1700
  this.#documentFields.set(key, field)
1701
1701
  const pending = this.#pendingDocumentField
1702
- if (pending && pending.reset === field.reset && JSON.stringify([pending.field.identity, pending.field.blockId, pending.field.stepId, pending.field.slot, pending.field.narrationId]) === key) {
1702
+ if (pending && pending.reset === field.reset && JSON.stringify([pending.field.identity, pending.field.blockId, pending.field.stepId]) === key) {
1703
1703
  this.#pendingDocumentField = undefined
1704
1704
  if (pending.generation === this.#documentSelectionGeneration && field.identity === this.#state.authoring?.identity && field.reset === this.#state.authoring.reset) field.focus.restore(pending.bookmark)
1705
1705
  }
@@ -1749,12 +1749,16 @@ export class Workbench {
1749
1749
  if (editor.prosemirrorView.hasFocus()) focus()
1750
1750
  const stopSelection = editor.onSelectionChange(() => { this.#documentSelectionGeneration++ })
1751
1751
  this.#unsubscribeDocumentSelection = () => { this.#stopDocumentLayoutScroll?.(); stopSelection(); element.removeEventListener('focusin', focus) }
1752
+ commands?.handleAnimationRequests({ available: () => this.#drafters.size > 0, request: place => this.draftAnimation(place) })
1752
1753
  } else this.#unsubscribeDocumentSelection = undefined
1753
1754
  this.#finishOpeningDocument()
1754
1755
  }
1755
- #focusDocumentNode(editor: NativeDocumentEditor, nodeId: string): void {
1756
+ /** `selectText`: the node's whole text is selected, so typing replaces it. */
1757
+ #focusDocumentNode(editor: NativeDocumentEditor, nodeId: string, selectText = false): void {
1756
1758
  this.#stopDocumentLayoutScroll?.(); this.#stopDocumentLayoutScroll = undefined
1757
1759
  editor.setTextCursorPosition(nodeId, 'start')
1760
+ const text = selectText ? nativeBlockText(editor, nodeId) : undefined
1761
+ if (text) editor.transact(transaction => transaction.setSelection(TextSelection.create(transaction.doc, text.from, text.to)))
1758
1762
  editor.focus()
1759
1763
  editor.prosemirrorView.dispatch(editor.prosemirrorState.tr.scrollIntoView())
1760
1764
  // React node views can finish sizing after the native editor mounts.
@@ -1838,7 +1842,7 @@ export class Workbench {
1838
1842
  if (scope !== this.#documentScope || identity !== this.#state.authoring?.identity) throw new Error('page-context-changed')
1839
1843
  const page = document.sections[section]
1840
1844
  if (!page) throw new Error('page-boundary')
1841
- const copy = duplicateDocumentPage(document, section, contentText(document.metadata.lang).copyTitle(page.title), () => crypto.randomUUID())
1845
+ const copy = duplicateDocumentPage(document, section, contentText(document.metadata.lang).copyTitle(page.title), () => randomUUID())
1842
1846
  copied = copy.section
1843
1847
  Object.assign(document, copy.document)
1844
1848
  if (generation === this.#documentSelectionGeneration) this.#pendingDocumentFocus = { documentId: document.id, nodeId: `page:${copied}`, generation, afterSave: true }
@@ -1874,8 +1878,8 @@ export class Workbench {
1874
1878
  if (this.#state.authoring?.version !== plan.source.version) throw new Error('revision-conflict')
1875
1879
  const current = pageDeletion(document, plan.document, plan.source.version, plan.section)
1876
1880
  if (!current.allowed) throw new Error(current.reason)
1877
- Object.assign(document, removeDocumentPage(document, plan.section, () => crypto.randomUUID()))
1878
- this.#set({ position: { section: document.sectionOrder[0]!, step: 0, trail: [], view: 'document' }, documentNavigation: { section: document.sectionOrder[0]!, request: crypto.randomUUID() } })
1881
+ Object.assign(document, removeDocumentPage(document, plan.section, () => randomUUID()))
1882
+ this.#set({ position: { section: document.sectionOrder[0]!, step: 0, trail: [], view: 'document' }, documentNavigation: { section: document.sectionOrder[0]!, request: randomUUID() } })
1879
1883
  })
1880
1884
  }
1881
1885
  createPage(after = this.#state.position?.section): boolean {
@@ -1887,8 +1891,9 @@ export class Workbench {
1887
1891
  this.#set({ pageEditFailure: undefined })
1888
1892
  const created = commands.insertPage(after, contentText(source.content.metadata.lang).newPage)
1889
1893
  this.#pendingDocumentField = undefined; this.#pendingDocumentSelection = undefined; this.#pendingDocumentFocus = undefined
1890
- this.#set({ position: { section: created.sectionId, step: 0, trail: [], view: 'document' }, documentNavigation: { section: created.sectionId, request: crypto.randomUUID() } })
1891
- this.#focusDocumentNode(editor, created.nodeId)
1894
+ this.#set({ position: { section: created.sectionId, step: 0, trail: [], view: 'document' }, documentNavigation: { section: created.sectionId, request: randomUUID() } })
1895
+ // The default title is selected: the author's first words name the page, and Enter goes on to its body.
1896
+ this.#focusDocumentNode(editor, `page:${created.sectionId}`, true)
1892
1897
  return true
1893
1898
  } catch (error) { this.#set({ pageEditFailure: this.#pageEditMessage(errorMessage(error)) }); return false }
1894
1899
  }
@@ -1966,6 +1971,40 @@ export class Workbench {
1966
1971
  }
1967
1972
  }
1968
1973
 
1974
+ /**
1975
+ * Opens the conversation with a request, for the author to describe and send, to add an animation at `place` of the
1976
+ * open document; the document's focus points the AI at the place's block. Nothing is inserted or sent.
1977
+ */
1978
+ draftAnimation(place: DocumentInsertionPlace): boolean {
1979
+ const document = this.#state.document, content = this.#state.authoring?.content, drafter = [...this.#drafters][0]
1980
+ const section = content?.sections[place.sectionId]
1981
+ if (document === undefined || !content || !section || !drafter) return false
1982
+ const report = (block?: string): FocusReport => ({ id: 'insertion', from: this.#state.previewRenderer, section: place.sectionId, ...block ? { block } : {}, ...place.text ? { quote: place.text.slice(0, 2000) } : {} })
1983
+ const text = this.#messages.animationRequest(content.sectionOrder.indexOf(place.sectionId) + 1, section.title, place)
1984
+ const focus = [report(place.blocks[0])]
1985
+ void this.#focusFirst(document, [...place.blocks.map(block => [report(block)]), [report()], []])
1986
+ const drafted = (attributes: Record<string, OperationValue>) => this.#record('agent.draft', { 'presmium.ask.kind': 'animation', 'presmium.ask.text': text, 'presmium.section': place.sectionId, 'presmium.block': place.blocks[0], ...attributes })
1987
+ this.#set({ askFailure: undefined })
1988
+ void (async () => drafter({ text, focus }))().then(() => drafted({ 'presmium.outcome': 'drafted' }), (error: unknown) => {
1989
+ drafted({ 'presmium.outcome': 'failed', 'presmium.error': errorMessage(error) })
1990
+ if (this.#state.document === document) this.#set({ askFailure: errorMessage(error) })
1991
+ })
1992
+ return true
1993
+ }
1994
+
1995
+ /**
1996
+ * Sets the first of `choices` the service can locate as the document's focus. Its runtime snapshot is compiled from the
1997
+ * saved source, which may not have a block or page created moments ago yet; the last choice leaves no stale focus.
1998
+ */
1999
+ async #focusFirst(document: string, choices: FocusReport[][]): Promise<void> {
2000
+ for (const focus of choices) {
2001
+ this.setFocus(focus)
2002
+ const version = this.#focusVersion
2003
+ await this.#focusWriting
2004
+ if (version !== this.#focusVersion || this.#state.document !== document || !this.#state.focusFailure) return
2005
+ }
2006
+ }
2007
+
1969
2008
  /** A place as the author sees it: the section's title and the passage. */
1970
2009
  #describe(item: FocusReport & Partial<Focus>): AgentPlace {
1971
2010
  const doc = this.#state.ir?.doc
@@ -1989,7 +2028,7 @@ export class Workbench {
1989
2028
  /** Shows a section in the preview: from the navigation pane, the section to read or the slide to show. */
1990
2029
  goToSection(section: string): void {
1991
2030
  if (this.#state.webView === 'document' && this.#state.authoring?.content?.sections[section]) {
1992
- this.#set({ position: { section, step: 0, trail: [], view: 'document' }, documentPositionFailure: undefined, documentNavigation: { section, request: crypto.randomUUID() } }); return
2031
+ this.#set({ position: { section, step: 0, trail: [], view: 'document' }, documentPositionFailure: undefined, documentNavigation: { section, request: randomUUID() } }); return
1993
2032
  }
1994
2033
  this.goToStep(section, this.#selectedSteps.get(`${this.#state.document}\0${section}`) ?? 0)
1995
2034
  }
@@ -2003,7 +2042,7 @@ export class Workbench {
2003
2042
  const visible = (index: number) => Boolean(section.stepOrder[index] && effectiveComponentParameters(document, blockId, section.stepOrder[index]).visual?.visible)
2004
2043
  const step = visible(current) ? current : Math.max(0, section.stepOrder.findIndex((_, index) => visible(index)))
2005
2044
  this.#set({ position: { section: sectionId, step, trail: [], view: 'document' }, documentPositionFailure: undefined,
2006
- documentNavigation: { section: sectionId, block: blockId, request: crypto.randomUUID() } })
2045
+ documentNavigation: { section: sectionId, block: blockId, request: randomUUID() } })
2007
2046
  }
2008
2047
 
2009
2048
  observeDocumentSection(section: string): void {
@@ -2236,9 +2275,6 @@ export class Workbench {
2236
2275
  } else if (this.#state.pointer?.quote) this.point(undefined)
2237
2276
  break
2238
2277
  }
2239
- case 'flashed':
2240
- if (this.#trail) this.#set({ changeTrail: { id: this.#trail, rects: report.rects } })
2241
- break
2242
2278
  case 'rect':
2243
2279
  if (this.#state.pointer) this.#set({ pointer: { ...this.#state.pointer, rect: report.rect ?? undefined, obstacles: report.obstacles } })
2244
2280
  break
@@ -2293,7 +2329,7 @@ export class Workbench {
2293
2329
  this.#voiceover.stop()
2294
2330
  this.#production.reset()
2295
2331
  this.#set({ isPreparingPresentation: false, presentationFailure: undefined, presentationSnapshot: undefined, productionPlan: undefined, productionFailure: undefined })
2296
- this.#removedCueTimings.clear(); this.#cueTimingScope = undefined
2332
+ this.#removedCueTimings.clear(); this.#removedNarrations.clear(); this.#cueTimingScope = undefined
2297
2333
  this.#documentSession.close()
2298
2334
  this.#unsubscribeDocumentHistory?.(); this.#unsubscribeDocumentHistory = undefined; this.#activeDocumentHistory = undefined; this.#documentEditor = undefined
2299
2335
  this.#unsubscribeDocumentSelection?.(); this.#unsubscribeDocumentSelection = undefined
@@ -13,6 +13,7 @@ import type { DocumentRecordingAdoption, VerifiedDocumentRecordingAdoption, Docu
13
13
  import type { DocumentImageImport, DocumentImageImportAnswer } from '@lincyaw/kernel'
14
14
  import type { ProductionAssetAnswer, ProductionAssetQuery, ProductionMediaQuery, ProductionPlan, ProductionPlanQuery } from '@lincyaw/kernel'
15
15
  import type { DocumentTranscriptionRequest, TranscriptionCapabilities, TranscriptionTask, TranscriptionListAnswer, TranscriptionRevision, TranscriptionCancellation, TranscriptionAdoptionDraft, TranscriptionAdoptionPlan, TranscriptionAdoptionApply, TranscriptionAdoptionAnswer } from '@lincyaw/kernel'
16
+ import { randomUUID } from '@lincyaw/kernel'
16
17
 
17
18
  /** A change of the host's pending approvals that this author may answer. */
18
19
  export type ApprovalEvent =
@@ -341,7 +342,7 @@ export function workspaceClient(base = 'api/presmium/', subscription?: Workspace
341
342
  if (operation.op === 'delete' && (!planned || JSON.stringify(planned.operation) !== JSON.stringify(operation))) throw new Error('project-deletion-plan-required')
342
343
  const key = JSON.stringify(operation)
343
344
  let intent = projectIntents.get(key)
344
- if (!intent) { intent = { id: crypto.randomUUID() }; projectIntents.set(key, intent) }
345
+ if (!intent) { intent = { id: randomUUID() }; projectIntents.set(key, intent) }
345
346
  while (projectIntents.size > 64) projectIntents.delete(projectIntents.keys().next().value!)
346
347
  try {
347
348
  intent.plan ??= planned
@@ -1,62 +0,0 @@
1
- // What the kitten beside the assistant does, from what the assistant is doing: asking the author to approve
2
- // something, reading the document it was asked about, writing into it, thinking, just finished, or idle (sitting,
3
- // then lying down, then asleep the longer nothing happens).
4
- import { useEffect, useState } from 'react'
5
- import type { MascotAction } from '@lincyaw/design/mascot'
6
- import { useWorkbench, useWorkbenchState } from './context.tsx'
7
-
8
- const DONE_MS = 2400
9
- const LOAF_MS = 90_000
10
- const SLEEP_MS = 300_000
11
-
12
- export interface AssistantActivity {
13
- /** The assistant's turn is running. */
14
- running: boolean
15
- /** It holds a review of the open document: still working in it, and how many changes it has made. */
16
- review?: { running: boolean, changes: number }
17
- /** Operations wait for the author's approval. */
18
- approvals: number
19
- /** How long ago the assistant last stopped, in milliseconds (Infinity if it has not run). */
20
- sinceStopped: number
21
- }
22
-
23
- export function assistantMascotAction(a: AssistantActivity): MascotAction {
24
- if (a.approvals > 0) return 'ask'
25
- if (a.review?.running) return a.review.changes > 0 ? 'write' : 'read'
26
- if (a.running) return 'think'
27
- if (a.review && a.review.changes > 0) return 'ask'
28
- if (a.sinceStopped < DONE_MS) return 'done'
29
- if (a.sinceStopped > SLEEP_MS) return 'sleep'
30
- if (a.sinceStopped > LOAF_MS) return 'loaf'
31
- return 'sit'
32
- }
33
-
34
- /** The kitten's action for the assistant, given whether its turn is running; follows the workbench's review and the
35
- * operations waiting for approval, and moves on by itself as time passes. */
36
- export function useAssistantMascot(running: boolean): MascotAction {
37
- const { client } = useWorkbench()
38
- // two plain values rather than one object, so the selection compares equal while nothing changes
39
- const reviewRunning = useWorkbenchState(state => state.review?.running)
40
- const changes = useWorkbenchState(state => state.review?.changes.length)
41
- const review = reviewRunning === undefined ? undefined : { running: reviewRunning, changes: changes ?? 0 }
42
- const [approvals, setApprovals] = useState<string[]>([])
43
- const [stoppedAt, setStoppedAt] = useState<number>(() => performance.now() - LOAF_MS / 2)
44
- const [, setNow] = useState(0)
45
- const busy = running || Boolean(review?.running)
46
- useEffect(() => client.subscribeApprovals(event => {
47
- if (event.type === 'reset') setApprovals([])
48
- else if (event.type === 'settled') setApprovals(list => list.filter(id => id !== event.id))
49
- else setApprovals(list => list.includes(event.id) ? list : [...list, event.id])
50
- }), [client])
51
- useEffect(() => { if (!busy) setStoppedAt(performance.now()) }, [busy])
52
- // Wake up at the next moment the idle kitten changes what it does.
53
- const since = performance.now() - stoppedAt
54
- useEffect(() => {
55
- if (busy) return
56
- const next = [DONE_MS, LOAF_MS, SLEEP_MS].find(ms => ms > since)
57
- if (next === undefined) return
58
- const timer = setTimeout(() => setNow(performance.now()), next - since + 50)
59
- return () => clearTimeout(timer)
60
- }, [busy, since])
61
- return assistantMascotAction({ running, review, approvals: approvals.length, sinceStopped: busy ? 0 : since })
62
- }
@@ -1,37 +0,0 @@
1
- // Whether the author wants the kitten: one choice for every place it shows, kept in this browser and followed by
2
- // every open tab. It is on until the author turns it off.
3
- import { useSyncExternalStore } from 'react'
4
-
5
- const KEY = 'presmium.mascot'
6
- const listeners = new Set<() => void>()
7
-
8
- function read(): boolean {
9
- try { return localStorage.getItem(KEY) !== 'off' } catch { return true }
10
- }
11
-
12
- let enabled = typeof localStorage === 'undefined' ? true : read()
13
-
14
- function subscribe(listener: () => void): () => void {
15
- listeners.add(listener)
16
- const storage = (event: StorageEvent) => {
17
- if (event.key !== KEY) return
18
- enabled = read()
19
- for (const each of listeners) each()
20
- }
21
- window.addEventListener('storage', storage)
22
- return () => { listeners.delete(listener); window.removeEventListener('storage', storage) }
23
- }
24
-
25
- export function mascotEnabled(): boolean {
26
- return enabled
27
- }
28
-
29
- export function setMascotEnabled(on: boolean): void {
30
- enabled = on
31
- try { localStorage.setItem(KEY, on ? 'on' : 'off') } catch { /* The choice holds for this page only. */ }
32
- for (const listener of listeners) listener()
33
- }
34
-
35
- export function useMascotEnabled(): boolean {
36
- return useSyncExternalStore(subscribe, mascotEnabled, () => true)
37
- }