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

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 (54) 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 +2 -1
  15. package/src/document-editor/NewCommentDrafts.tsx +4 -3
  16. package/src/document-editor/PlaceComment.tsx +150 -0
  17. package/src/document-editor/comment-drafts.ts +16 -14
  18. package/src/document-editor/comments.ts +30 -10
  19. package/src/document-editor/component-label.ts +11 -0
  20. package/src/document-editor/file-input.ts +2 -2
  21. package/src/document-editor/insertion-place.ts +60 -0
  22. package/src/document-editor/messages.ts +3 -3
  23. package/src/document-editor/narration-schema.tsx +3 -2
  24. package/src/document-editor/page-body.ts +60 -0
  25. package/src/document-editor/projection.ts +2 -2
  26. package/src/document-editor/rich-text.ts +3 -2
  27. package/src/document-editor/selection.ts +13 -0
  28. package/src/document-editor/style.css +12 -1
  29. package/src/document-editor/table-identities.ts +2 -1
  30. package/src/document-session.ts +50 -16
  31. package/src/history.ts +2 -2
  32. package/src/index.ts +0 -3
  33. package/src/messages.ts +19 -4
  34. package/src/panels/DocumentView.tsx +10 -10
  35. package/src/panels/ExportPanel.tsx +1 -6
  36. package/src/panels/PreviewPanel.tsx +1 -4
  37. package/src/panels/ProductionCaptionSize.tsx +20 -0
  38. package/src/panels/ProductionCaptionTiming.tsx +3 -3
  39. package/src/panels/ProductionMediaClips.tsx +2 -1
  40. package/src/panels/ProductionPanel.tsx +5 -1
  41. package/src/panels/ProductionRecordingRange.tsx +2 -2
  42. package/src/panels/ProductionTimeline.tsx +6 -1
  43. package/src/panels/ProductionTranscription.tsx +2 -1
  44. package/src/panels/SourceDocumentPanel.tsx +3 -6
  45. package/src/panels/TranscriptionAdoption.tsx +4 -4
  46. package/src/places.ts +1 -36
  47. package/src/production-messages.ts +6 -0
  48. package/src/sha256.ts +9 -0
  49. package/src/styles.css +16 -41
  50. package/src/workbench.ts +80 -33
  51. package/src/workspace-client.ts +2 -1
  52. package/src/assistant-mascot.ts +0 -62
  53. package/src/mascot-preference.ts +0 -37
  54. 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 } 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'
@@ -26,7 +27,8 @@ import type { DocumentReturnPoint } from './document-return-point.ts'
26
27
  import type { DocumentRecordingAdoption, VerifiedDocumentRecordingAdoption, DocumentAudioImportAnswer, DocumentAudioInspectionAnswer, DocumentChangeAnswer, MediaClip, NarrationNode, 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). */
@@ -307,6 +311,7 @@ export class Workbench {
307
311
  readonly #jobs: DocumentJobs
308
312
  readonly #voiceover: VoiceoverJobs
309
313
  readonly #askers = new Set<(request: AgentRequest) => Promise<AgentAnswer | void>>()
314
+ readonly #drafters = new Set<(draft: AgentDraft) => void | Promise<void>>()
310
315
  readonly #previewSinks = new Set<PreviewSink>()
311
316
  readonly #playbackKeys = new Set<(command: PlaybackKey) => void>()
312
317
  #unsubscribeIr?: () => void
@@ -329,7 +334,6 @@ export class Workbench {
329
334
  #pendingReveal?: PreviewAnchor
330
335
  /** The AI's changes to show on the document once the preview has them. */
331
336
  #pendingFlash?: ReviewChange[]
332
- #trail = 0
333
337
  #lastPosition?: string
334
338
  #awaitingView?: WebView
335
339
  /** The forms asked for ahead of the author (`web:<dir>`, `slides:<dir>`), and the wait before asking for the slides. */
@@ -416,12 +420,6 @@ export class Workbench {
416
420
  && previous?.identity === authoring.identity && Boolean(previous?.version && authoring.version)
417
421
  && (previous?.version !== authoring.version || previous?.fingerprint !== authoring.fingerprint)
418
422
  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
423
  })
426
424
  this.#diagnostics = new DocumentDiagnostics(this.client, store)
427
425
  this.#jobs = new DocumentJobs(this.client, store)
@@ -506,6 +504,12 @@ export class Workbench {
506
504
  return () => this.#askers.delete(asker)
507
505
  }
508
506
 
507
+ /** The host's way to put a request in the conversation's composer for the author to finish; nothing is sent. */
508
+ onAgentDraft = (drafter: (draft: AgentDraft) => void | Promise<void>): (() => void) => {
509
+ this.#drafters.add(drafter)
510
+ return () => this.#drafters.delete(drafter)
511
+ }
512
+
509
513
  #nextDocumentPosition(): string { return `${this.#positionSeed}:${++this.#positionSequence}` }
510
514
 
511
515
  #restoredDocumentPosition(source: DocumentSessionState): boolean {
@@ -557,7 +561,7 @@ export class Workbench {
557
561
  }
558
562
 
559
563
  planDocumentOperation(operation: ProjectOperation): Promise<ProjectPlan> {
560
- return this.client.planProjectChange({ id: crypto.randomUUID(), operation })
564
+ return this.client.planProjectChange({ id: randomUUID(), operation })
561
565
  }
562
566
 
563
567
  operateDocument(operation: ProjectOperation, planned?: ProjectPlan): Promise<OperationAnswer> {
@@ -790,7 +794,6 @@ export class Workbench {
790
794
  this.#pendingFlash = undefined
791
795
  const targets = changedPlaces(ir.doc, changes)
792
796
  if (targets.length === 0) return
793
- this.#trail++
794
797
  this.#broadcast({ type: 'flash', targets })
795
798
  this.#record('ai.change.shown', { 'presmium.places': targets.map(target => [target.section, target.block].filter(Boolean).join(':')).join('\n') })
796
799
  }
@@ -1400,7 +1403,7 @@ export class Workbench {
1400
1403
  let request: { identity: string, version: string, id: string } | undefined
1401
1404
  const answer = await this.#writeDocumentMedia(expected, async directory => {
1402
1405
  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!
1406
+ request = first ? { identity: expected.identity, version: expected.version, id: randomUUID() } : existing!
1404
1407
  this.#audioImports.set(file, request)
1405
1408
  try { return await this.client.importDocumentAudio(directory, file, { id: request.id, baseDocumentVersion: request.version }, signal) }
1406
1409
  catch (error) {
@@ -1418,7 +1421,7 @@ export class Workbench {
1418
1421
  if (!file.size || file.size > DOCUMENT_AUDIO_MAX_BYTES) throw new Error('production.asset-size')
1419
1422
  let pending = this.#audioFingerprints.get(file)
1420
1423
  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(''))
1424
+ pending = file.arrayBuffer().then(sha256)
1422
1425
  this.#audioFingerprints.set(file, pending)
1423
1426
  }
1424
1427
  const hash = await pending
@@ -1469,13 +1472,13 @@ export class Workbench {
1469
1472
  async applyDocumentMediaClip(clip: MediaClip, expected: { identity: string, version: string, binding: ProductionBinding }, signal?: AbortSignal): Promise<DocumentChangeAnswer> {
1470
1473
  this.#mediaSource(expected)
1471
1474
  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)
1475
+ const id = randomUUID(), value = structuredClone(clip)
1473
1476
  const answer = await this.#writeDocumentMedia(expected, directory => this.client.changeDocumentMediaClip(directory, { id, baseDocumentVersion: expected.version, binding: expected.binding, clip: value }, signal), signal)
1474
1477
  if (answer.changeId !== id) throw new Error('production-source-changed')
1475
1478
  return answer
1476
1479
  }
1477
1480
  async removeDocumentMediaClip(clipId: string, expected: { identity: string, version: string }, signal?: AbortSignal): Promise<DocumentChangeAnswer> {
1478
- const id = crypto.randomUUID()
1481
+ const id = randomUUID()
1479
1482
  const answer = await this.#writeDocumentMedia(expected, directory => this.client.changeDocumentMediaClip(directory, { id, baseDocumentVersion: expected.version, removeClipId: clipId }, signal), signal)
1480
1483
  if (answer.changeId !== id) throw new Error('production-source-changed')
1481
1484
  return answer
@@ -1584,6 +1587,14 @@ export class Workbench {
1584
1587
  this.stopAuditions()
1585
1588
  this.#documentSession.record(next)
1586
1589
  }
1590
+ /** The whole document's caption size (`production.captions.scale`), recorded like a step's timing. */
1591
+ setDocumentCaptionScale(scale: number, identity?: string): void {
1592
+ const document = this.getDocument(identity)
1593
+ if (!document || isAgentChanging(this.#state)) return
1594
+ const next = changeDocumentCaptionScale(document, scale)
1595
+ if (next === document) return
1596
+ this.#documentSession.record(next)
1597
+ }
1587
1598
  documentTimingDraft(stepId: string, key: StepDurationKey, identity: string, reset: number): StepDurationDraft | undefined {
1588
1599
  const document = this.getDocument(identity)
1589
1600
  return document && this.#state.authoring?.reset === reset ? this.#documentTimingDrafts.get(identity, document, stepId, key) : undefined
@@ -1625,7 +1636,7 @@ export class Workbench {
1625
1636
  const existing = target !== undefined && Object.hasOwn(document.narrations, target) ? document.narrations[target] : undefined
1626
1637
  if (existing && !step.narrationIds.includes(existing.id)) throw new Error('document-narration-target')
1627
1638
  if (!existing && !meaningful) return undefined
1628
- const id = target ?? crypto.randomUUID()
1639
+ const id = target ?? randomUUID()
1629
1640
  if (!id || id.length > 200 || /[\u0000-\u001f]/.test(id) || ['__proto__', 'constructor', 'prototype'].includes(id)) throw new Error('document-narration-target')
1630
1641
  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
1642
  if (existing && JSON.stringify(existing.content) === JSON.stringify(content)) return id
@@ -1749,12 +1760,16 @@ export class Workbench {
1749
1760
  if (editor.prosemirrorView.hasFocus()) focus()
1750
1761
  const stopSelection = editor.onSelectionChange(() => { this.#documentSelectionGeneration++ })
1751
1762
  this.#unsubscribeDocumentSelection = () => { this.#stopDocumentLayoutScroll?.(); stopSelection(); element.removeEventListener('focusin', focus) }
1763
+ commands?.handleAnimationRequests({ available: () => this.#drafters.size > 0, request: place => this.draftAnimation(place) })
1752
1764
  } else this.#unsubscribeDocumentSelection = undefined
1753
1765
  this.#finishOpeningDocument()
1754
1766
  }
1755
- #focusDocumentNode(editor: NativeDocumentEditor, nodeId: string): void {
1767
+ /** `selectText`: the node's whole text is selected, so typing replaces it. */
1768
+ #focusDocumentNode(editor: NativeDocumentEditor, nodeId: string, selectText = false): void {
1756
1769
  this.#stopDocumentLayoutScroll?.(); this.#stopDocumentLayoutScroll = undefined
1757
1770
  editor.setTextCursorPosition(nodeId, 'start')
1771
+ const text = selectText ? nativeBlockText(editor, nodeId) : undefined
1772
+ if (text) editor.transact(transaction => transaction.setSelection(TextSelection.create(transaction.doc, text.from, text.to)))
1758
1773
  editor.focus()
1759
1774
  editor.prosemirrorView.dispatch(editor.prosemirrorState.tr.scrollIntoView())
1760
1775
  // React node views can finish sizing after the native editor mounts.
@@ -1838,7 +1853,7 @@ export class Workbench {
1838
1853
  if (scope !== this.#documentScope || identity !== this.#state.authoring?.identity) throw new Error('page-context-changed')
1839
1854
  const page = document.sections[section]
1840
1855
  if (!page) throw new Error('page-boundary')
1841
- const copy = duplicateDocumentPage(document, section, contentText(document.metadata.lang).copyTitle(page.title), () => crypto.randomUUID())
1856
+ const copy = duplicateDocumentPage(document, section, contentText(document.metadata.lang).copyTitle(page.title), () => randomUUID())
1842
1857
  copied = copy.section
1843
1858
  Object.assign(document, copy.document)
1844
1859
  if (generation === this.#documentSelectionGeneration) this.#pendingDocumentFocus = { documentId: document.id, nodeId: `page:${copied}`, generation, afterSave: true }
@@ -1874,8 +1889,8 @@ export class Workbench {
1874
1889
  if (this.#state.authoring?.version !== plan.source.version) throw new Error('revision-conflict')
1875
1890
  const current = pageDeletion(document, plan.document, plan.source.version, plan.section)
1876
1891
  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() } })
1892
+ Object.assign(document, removeDocumentPage(document, plan.section, () => randomUUID()))
1893
+ this.#set({ position: { section: document.sectionOrder[0]!, step: 0, trail: [], view: 'document' }, documentNavigation: { section: document.sectionOrder[0]!, request: randomUUID() } })
1879
1894
  })
1880
1895
  }
1881
1896
  createPage(after = this.#state.position?.section): boolean {
@@ -1887,8 +1902,9 @@ export class Workbench {
1887
1902
  this.#set({ pageEditFailure: undefined })
1888
1903
  const created = commands.insertPage(after, contentText(source.content.metadata.lang).newPage)
1889
1904
  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)
1905
+ this.#set({ position: { section: created.sectionId, step: 0, trail: [], view: 'document' }, documentNavigation: { section: created.sectionId, request: randomUUID() } })
1906
+ // The default title is selected: the author's first words name the page, and Enter goes on to its body.
1907
+ this.#focusDocumentNode(editor, `page:${created.sectionId}`, true)
1892
1908
  return true
1893
1909
  } catch (error) { this.#set({ pageEditFailure: this.#pageEditMessage(errorMessage(error)) }); return false }
1894
1910
  }
@@ -1966,6 +1982,40 @@ export class Workbench {
1966
1982
  }
1967
1983
  }
1968
1984
 
1985
+ /**
1986
+ * Opens the conversation with a request, for the author to describe and send, to add an animation at `place` of the
1987
+ * open document; the document's focus points the AI at the place's block. Nothing is inserted or sent.
1988
+ */
1989
+ draftAnimation(place: DocumentInsertionPlace): boolean {
1990
+ const document = this.#state.document, content = this.#state.authoring?.content, drafter = [...this.#drafters][0]
1991
+ const section = content?.sections[place.sectionId]
1992
+ if (document === undefined || !content || !section || !drafter) return false
1993
+ const report = (block?: string): FocusReport => ({ id: 'insertion', from: this.#state.previewRenderer, section: place.sectionId, ...block ? { block } : {}, ...place.text ? { quote: place.text.slice(0, 2000) } : {} })
1994
+ const text = this.#messages.animationRequest(content.sectionOrder.indexOf(place.sectionId) + 1, section.title, place)
1995
+ const focus = [report(place.blocks[0])]
1996
+ void this.#focusFirst(document, [...place.blocks.map(block => [report(block)]), [report()], []])
1997
+ 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 })
1998
+ this.#set({ askFailure: undefined })
1999
+ void (async () => drafter({ text, focus }))().then(() => drafted({ 'presmium.outcome': 'drafted' }), (error: unknown) => {
2000
+ drafted({ 'presmium.outcome': 'failed', 'presmium.error': errorMessage(error) })
2001
+ if (this.#state.document === document) this.#set({ askFailure: errorMessage(error) })
2002
+ })
2003
+ return true
2004
+ }
2005
+
2006
+ /**
2007
+ * Sets the first of `choices` the service can locate as the document's focus. Its runtime snapshot is compiled from the
2008
+ * saved source, which may not have a block or page created moments ago yet; the last choice leaves no stale focus.
2009
+ */
2010
+ async #focusFirst(document: string, choices: FocusReport[][]): Promise<void> {
2011
+ for (const focus of choices) {
2012
+ this.setFocus(focus)
2013
+ const version = this.#focusVersion
2014
+ await this.#focusWriting
2015
+ if (version !== this.#focusVersion || this.#state.document !== document || !this.#state.focusFailure) return
2016
+ }
2017
+ }
2018
+
1969
2019
  /** A place as the author sees it: the section's title and the passage. */
1970
2020
  #describe(item: FocusReport & Partial<Focus>): AgentPlace {
1971
2021
  const doc = this.#state.ir?.doc
@@ -1989,7 +2039,7 @@ export class Workbench {
1989
2039
  /** Shows a section in the preview: from the navigation pane, the section to read or the slide to show. */
1990
2040
  goToSection(section: string): void {
1991
2041
  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
2042
+ this.#set({ position: { section, step: 0, trail: [], view: 'document' }, documentPositionFailure: undefined, documentNavigation: { section, request: randomUUID() } }); return
1993
2043
  }
1994
2044
  this.goToStep(section, this.#selectedSteps.get(`${this.#state.document}\0${section}`) ?? 0)
1995
2045
  }
@@ -2003,7 +2053,7 @@ export class Workbench {
2003
2053
  const visible = (index: number) => Boolean(section.stepOrder[index] && effectiveComponentParameters(document, blockId, section.stepOrder[index]).visual?.visible)
2004
2054
  const step = visible(current) ? current : Math.max(0, section.stepOrder.findIndex((_, index) => visible(index)))
2005
2055
  this.#set({ position: { section: sectionId, step, trail: [], view: 'document' }, documentPositionFailure: undefined,
2006
- documentNavigation: { section: sectionId, block: blockId, request: crypto.randomUUID() } })
2056
+ documentNavigation: { section: sectionId, block: blockId, request: randomUUID() } })
2007
2057
  }
2008
2058
 
2009
2059
  observeDocumentSection(section: string): void {
@@ -2236,9 +2286,6 @@ export class Workbench {
2236
2286
  } else if (this.#state.pointer?.quote) this.point(undefined)
2237
2287
  break
2238
2288
  }
2239
- case 'flashed':
2240
- if (this.#trail) this.#set({ changeTrail: { id: this.#trail, rects: report.rects } })
2241
- break
2242
2289
  case 'rect':
2243
2290
  if (this.#state.pointer) this.#set({ pointer: { ...this.#state.pointer, rect: report.rect ?? undefined, obstacles: report.obstacles } })
2244
2291
  break
@@ -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
- }
@@ -1,79 +0,0 @@
1
- import { useEffect, useRef, useState } from 'react'
2
- import type { CSSProperties, RefObject } from 'react'
3
- import type { MascotAction } from '@lincyaw/design/mascot'
4
- import { PmMascot } from '../components.tsx'
5
- import { useMascotEnabled } from '../mascot-preference.ts'
6
- import { useWorkbenchState } from '../context.tsx'
7
- import { pageId, richNodeIds } from '../document-editor/projection.ts'
8
-
9
- /** How long the kitten walks from one changed place to the next, pats it, and stays when it is done (ms). */
10
- const WALK_MS = 700, PAT_MS = 900, DONE_MS = 2200
11
- /** At most this many changed places are visited. */
12
- const STOPS = 6
13
-
14
- type Point = { x: number, y: number }
15
- type Phase = 'walk' | 'pat' | 'done' | 'gone'
16
-
17
- /**
18
- * The kitten visiting a list of points in its host (each the lower left of a changed place): it walks to each, pats it,
19
- * hops when it is done and leaves. A new `id` starts it again from the first point; `where(i)` is asked on every
20
- * render, so the kitten follows the places as the host lays out again. Nothing shows when the kitten is turned off.
21
- */
22
- function KittenTrail({ id, count, where }: { id: number | undefined, count: number, where(index: number): Point | undefined }) {
23
- const enabled = useMascotEnabled()
24
- const [step, setStep] = useState<{ id: number, stop: number, phase: Phase }>()
25
- const facing = useRef<'left' | 'right'>('right'), last = useRef<Point>()
26
- useEffect(() => {
27
- if (id !== undefined && count > 0) setStep(current => current?.id === id ? current : { id, stop: 0, phase: 'walk' })
28
- }, [id, count])
29
- useEffect(() => {
30
- if (!step || step.phase === 'gone') return
31
- const stops = Math.min(STOPS, count)
32
- const next: typeof step = step.phase === 'walk' ? { ...step, phase: 'pat' }
33
- : step.phase === 'pat' ? (step.stop + 1 < stops ? { ...step, stop: step.stop + 1, phase: 'walk' } : { ...step, phase: 'done' })
34
- : { ...step, phase: 'gone' }
35
- const timer = setTimeout(() => setStep(next), step.phase === 'walk' ? WALK_MS : step.phase === 'pat' ? PAT_MS : DONE_MS)
36
- return () => clearTimeout(timer)
37
- }, [step, count])
38
- if (!enabled || !step || step.phase === 'gone' || step.id !== id) return null
39
- const point = where(Math.min(step.stop, count - 1))
40
- if (!point) return null
41
- if (last.current && Math.abs(point.x - last.current.x) > 2 && step.phase === 'walk') facing.current = point.x < last.current.x ? 'left' : 'right'
42
- if (step.phase === 'walk') last.current = point
43
- const action: MascotAction = step.phase === 'walk' ? 'walk' : step.phase === 'pat' ? 'write' : 'done'
44
- return (
45
- <div className="pmw-change-trail" data-phase={step.phase} style={{ '--pmw-trail-x': `${point.x}px`, '--pmw-trail-y': `${point.y}px` } as CSSProperties}>
46
- <div className="pmw-change-trail__facing" data-facing={facing.current}><PmMascot size="small" action={action} /></div>
47
- </div>
48
- )
49
- }
50
-
51
- /** In the preview: the blocks the preview reports blinking, in the active frame's viewport. */
52
- export function PreviewChangeTrail({ stage, frames }: { stage: RefObject<HTMLDivElement | null>, frames: RefObject<HTMLDivElement | null> }) {
53
- const trail = useWorkbenchState(state => state.changeTrail)
54
- return <KittenTrail id={trail?.id} count={trail?.rects.length ?? 0} where={index => {
55
- const rect = trail?.rects[index]
56
- const frame = frames.current?.querySelector<HTMLIFrameElement>('iframe[data-active="true"]:not([hidden])')
57
- const host = stage.current
58
- if (!rect || !frame || !host) return undefined
59
- const offset = frame.getBoundingClientRect(), origin = host.getBoundingClientRect()
60
- return { x: offset.left - origin.left + rect.x, y: offset.top - origin.top + rect.y + rect.height }
61
- }} />
62
- }
63
-
64
- /** In the editor: each changed block (a text block by its first paragraph), else its page's title, on the sheet. */
65
- export function EditorChangeTrail({ sheet }: { sheet: RefObject<HTMLDivElement | null> }) {
66
- const places = useWorkbenchState(state => state.changedPlaces)
67
- const content = useWorkbenchState(state => state.authoring?.content)
68
- return <KittenTrail id={places?.id} count={places?.targets.length ?? 0} where={index => {
69
- const target = places?.targets[index], host = sheet.current
70
- if (!target || !host) return undefined
71
- const block = target.block ? content?.blocks[target.block] : undefined
72
- const ids = [...target.block ? [target.block] : [], ...block?.type === 'text' ? richNodeIds(block.content) : [], pageId(target.section)]
73
- const element = ids.map(id => host.querySelector<HTMLElement>(`[data-id="${CSS.escape(id)}"]`)).find(Boolean)
74
- if (!element) return undefined
75
- const box = element.getBoundingClientRect(), origin = host.getBoundingClientRect()
76
- // the kitten stands on the place's first line, at its left edge
77
- return { x: box.left - origin.left, y: box.top - origin.top + Math.min(box.height, 28) }
78
- }} />
79
- }