@lincyaw/workbench 0.2.0-rc.12 → 0.2.0-rc.120
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/package.json +5 -4
- package/src/AgentActivityBar.tsx +25 -0
- package/src/MediaClipEditor.tsx +7 -6
- package/src/StepProductionEditor.tsx +16 -7
- package/src/agent-activity-messages.ts +14 -0
- package/src/agent-activity.ts +38 -0
- package/src/approvals.tsx +6 -1
- package/src/audio-recording-messages.ts +5 -5
- package/src/audio-recording.ts +27 -5
- package/src/authoring-client.ts +10 -10
- package/src/authoring-draft-fragment.ts +3 -1
- package/src/authoring-window.ts +100 -0
- package/src/base-controls.tsx +28 -7
- package/src/body-draft-actions.ts +60 -0
- package/src/caption-timing-drafts.ts +20 -3
- package/src/caption-timing-messages.ts +2 -2
- package/src/caption-timing.ts +2 -2
- package/src/component-draft-actions.ts +1 -1
- package/src/component-properties.ts +3 -3
- package/src/component-property-drafts.ts +6 -6
- package/src/components.tsx +43 -38
- package/src/container-layout.ts +21 -0
- package/src/context.tsx +5 -3
- package/src/diagnostics.ts +1 -1
- package/src/document-draft-store.ts +45 -18
- package/src/document-drafts.ts +30 -21
- package/src/document-editing.ts +485 -0
- package/src/document-editor/BlockNoteSurface.tsx +40 -14
- package/src/document-editor/CommentComposer.tsx +24 -0
- package/src/document-editor/ComponentMaterials.tsx +4 -10
- package/src/document-editor/ComponentPropertiesDialog.tsx +12 -12
- package/src/document-editor/DocumentBlockFallback.tsx +4 -3
- package/src/document-editor/DocumentBlockPreview.tsx +11 -4
- package/src/document-editor/DocumentComments.tsx +87 -17
- package/src/document-editor/DocumentEditor.tsx +66 -14
- package/src/document-editor/DocumentStepNarration.tsx +39 -27
- package/src/document-editor/NarrationEditor.tsx +55 -21
- package/src/document-editor/NewCommentDrafts.tsx +120 -37
- package/src/document-editor/PlaceComment.tsx +252 -0
- package/src/document-editor/agent-targets.ts +34 -0
- package/src/document-editor/blocknote-messages.ts +2 -0
- package/src/document-editor/comment-draft-actions.ts +40 -20
- package/src/document-editor/comment-draft-inspection.ts +2 -2
- package/src/document-editor/comment-drafts.ts +75 -27
- package/src/document-editor/comments.ts +34 -14
- package/src/document-editor/component-label.ts +11 -0
- package/src/document-editor/file-input.ts +2 -2
- package/src/document-editor/focus.ts +0 -2
- package/src/document-editor/inline.ts +1 -0
- package/src/document-editor/insertion-place.ts +60 -0
- package/src/document-editor/messages.ts +3 -3
- package/src/document-editor/narration-schema.tsx +12 -6
- package/src/document-editor/narration.ts +19 -3
- package/src/document-editor/page-body.ts +60 -0
- package/src/document-editor/popover.tsx +4 -2
- package/src/document-editor/projection.ts +21 -19
- package/src/document-editor/rich-text.ts +3 -2
- package/src/document-editor/selection.ts +13 -0
- package/src/document-editor/style.css +8 -3
- package/src/document-editor/table-identities.ts +2 -1
- package/src/document-preview.ts +3 -2
- package/src/document-production.ts +492 -0
- package/src/document-return-point.ts +2 -2
- package/src/document-session.ts +216 -67
- package/src/document-timing-drafts.ts +26 -6
- package/src/effective-settings.ts +12 -0
- package/src/history.ts +5 -5
- package/src/import-messages.ts +40 -0
- package/src/index.ts +11 -5
- package/src/jobs.ts +1 -1
- package/src/mcp-settings-messages.ts +39 -0
- package/src/media-drafts.ts +43 -1
- package/src/messages.ts +39 -18
- package/src/outline.ts +5 -5
- package/src/panels/DocumentImportDialog.tsx +59 -0
- package/src/panels/DocumentNarrationPanel.tsx +2 -2
- package/src/panels/DocumentView.tsx +106 -77
- package/src/panels/DocumentVoiceDialog.tsx +29 -12
- package/src/panels/DocumentVoiceField.tsx +3 -2
- package/src/panels/ExportPanel.tsx +2 -10
- package/src/panels/HistoryPanel.tsx +36 -10
- package/src/panels/HomePanel.tsx +12 -1
- package/src/panels/McpSettingsPanel.tsx +86 -0
- package/src/panels/PageDeleteDialog.tsx +4 -4
- package/src/panels/PageRenameDialog.tsx +1 -1
- package/src/panels/PreviewPanel.tsx +4 -7
- package/src/panels/ProductionCaptionSize.tsx +20 -0
- package/src/panels/ProductionCaptionTiming.tsx +12 -9
- package/src/panels/ProductionMediaClips.tsx +25 -9
- package/src/panels/ProductionPanel.tsx +21 -17
- package/src/panels/ProductionRecording.tsx +86 -9
- package/src/panels/ProductionRecordingRange.tsx +26 -23
- package/src/panels/ProductionTimeline.tsx +6 -1
- package/src/panels/ProductionTranscription.tsx +8 -6
- package/src/panels/SourceDocumentPanel.tsx +24 -40
- package/src/panels/SpeechSettingsPanel.tsx +2 -2
- package/src/panels/TranscriptionAdoption.tsx +42 -36
- package/src/panels/VoiceCloneDialog.tsx +2 -1
- package/src/panels/VoicePreviewDialog.tsx +1 -1
- package/src/panels/VoiceScopeField.tsx +2 -2
- package/src/panels/VoiceoverDialog.tsx +11 -10
- package/src/places.ts +1 -36
- package/src/preview-playback.ts +1 -1
- package/src/production-draft-actions.ts +37 -0
- package/src/production-messages.ts +6 -0
- package/src/production-playback.ts +1 -1
- package/src/production-voice-playback.ts +3 -2
- package/src/recording-adoption.ts +4 -4
- package/src/sha256.ts +9 -0
- package/src/speech-settings-messages.ts +6 -6
- package/src/step-production.ts +4 -4
- package/src/styles.css +43 -92
- package/src/transcription-drafts.ts +124 -0
- package/src/use-recording-adoption.ts +5 -4
- package/src/voice-drafts.ts +32 -8
- package/src/voice-messages.ts +9 -9
- package/src/voiceover-jobs.ts +1 -1
- package/src/workbench-state.ts +159 -0
- package/src/workbench.ts +323 -956
- package/src/workspace-client.ts +41 -12
- package/src/assistant-mascot.ts +0 -62
- package/src/mascot-preference.ts +0 -37
- package/src/panels/ChangeTrail.tsx +0 -79
package/README.md
CHANGED
|
@@ -8,7 +8,7 @@ Presmium's workbench panels in React, for teachers who know no code. The panels
|
|
|
8
8
|
|
|
9
9
|
Under them, bound to no framework, are the client of the workspace service (`presmium serve`), the workbench's state, the mapping of an edit on the page back to the source, the preview protocol's workbench side, and the shared components of Presmium's design (`PmButton`, `PmDialog`, `PmMenu` and the others) in React. Experimental (0.x).
|
|
10
10
|
|
|
11
|
-
Part of [Presmium](https://github.com/Lincyaw/presmium), which compiles canonical
|
|
11
|
+
Part of [Presmium](https://github.com/Lincyaw/presmium), which compiles canonical index.pmd teaching documents into web pages, slides, Jupyter notebooks and narrated playback.
|
|
12
12
|
|
|
13
13
|
## Usage
|
|
14
14
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lincyaw/workbench",
|
|
3
|
-
"version": "0.2.0-rc.
|
|
3
|
+
"version": "0.2.0-rc.120",
|
|
4
4
|
"description": "Presmium's workbench panels in React (the documents, a document edited on its rendered page with comments and version history) with the workspace service's client and the preview protocol's workbench side; @lincyaw/dsh places them in presmium studio (experimental)",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -39,6 +39,7 @@
|
|
|
39
39
|
"@floating-ui/react": "0.27.20",
|
|
40
40
|
"@mantine/core": "8.3.11",
|
|
41
41
|
"@mantine/hooks": "8.3.11",
|
|
42
|
+
"@noble/hashes": "2.4.0",
|
|
42
43
|
"@radix-ui/react-dialog": "^1.3.1",
|
|
43
44
|
"@radix-ui/react-dropdown-menu": "^2.2.1",
|
|
44
45
|
"@radix-ui/react-popover": "^1.1.15",
|
|
@@ -54,8 +55,8 @@
|
|
|
54
55
|
"@tiptap/pm": "3.32.0",
|
|
55
56
|
"katex": "0.18.10",
|
|
56
57
|
"react-resizable-panels": "4.14.5",
|
|
57
|
-
"@lincyaw/kernel": "1.0.0-rc.
|
|
58
|
-
"@lincyaw/design": "1.0.0-rc.
|
|
58
|
+
"@lincyaw/kernel": "1.0.0-rc.119",
|
|
59
|
+
"@lincyaw/design": "1.0.0-rc.119"
|
|
59
60
|
},
|
|
60
61
|
"peerDependencies": {
|
|
61
62
|
"react": "^18.3.1",
|
|
@@ -66,6 +67,6 @@
|
|
|
66
67
|
"@types/react-dom": "18.3.7",
|
|
67
68
|
"react": "^18.3.1",
|
|
68
69
|
"react-dom": "^18.3.1",
|
|
69
|
-
"@lincyaw/compiler": "1.0.0-rc.
|
|
70
|
+
"@lincyaw/compiler": "1.0.0-rc.119"
|
|
70
71
|
}
|
|
71
72
|
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { PmAgentActivity, PmIconButton, PmTooltip } from './components.tsx'
|
|
2
|
+
import { useWorkbench, useWorkbenchState } from './context.tsx'
|
|
3
|
+
import { latestAgentActivity } from './agent-activity.ts'
|
|
4
|
+
import { AGENT_ACTIVITY_MESSAGES } from './agent-activity-messages.ts'
|
|
5
|
+
|
|
6
|
+
/** A compact presence row, not a second activity log or an editor lock. */
|
|
7
|
+
export function AgentActivityBar() {
|
|
8
|
+
const workbench = useWorkbench(), state = useWorkbenchState(value => value)
|
|
9
|
+
const activity = latestAgentActivity(state.agentActivities), t = AGENT_ACTIVITY_MESSAGES[state.locale]
|
|
10
|
+
if (!activity) return null
|
|
11
|
+
const title = state.documents?.documents.find(doc => doc.dir === activity.document)?.title
|
|
12
|
+
const content = state.document === activity.document ? state.authoring?.content : undefined, target = activity.targets[0]
|
|
13
|
+
const section = target?.section ?? (target?.step ? content?.steps[target.step]?.sectionId : undefined)
|
|
14
|
+
const page = section ? content?.sections[section]?.title : undefined
|
|
15
|
+
const location = [title, page].filter(Boolean).join(' · ')
|
|
16
|
+
const label = `${activity.state === 'running' ? t[activity.operation] : t[activity.state]}${location ? ` · ${location}` : ''}`
|
|
17
|
+
const count = state.agentActivities?.filter(value => value.state === 'running').length ?? 0
|
|
18
|
+
const detail = `${label}${count > 1 ? ` · ${t.concurrent(count)}` : ''}${!state.agentFollowing ? ` · ${t.paused}` : ''}`
|
|
19
|
+
const short = activity.state === 'running' ? t.compact[activity.operation] : t.compact[activity.state]
|
|
20
|
+
return <span className="pmw-agent-activity" data-agent-activity-controls="">
|
|
21
|
+
<PmTooltip label={detail}><span className="pmw-agent-activity__label"><PmAgentActivity compact state={activity.state} label={short} detail={detail} /></span></PmTooltip>
|
|
22
|
+
<PmIconButton small icon={state.agentFollowing ? 'pause' : 'fit'} label={state.agentFollowing ? t.pauseLabel : t.followLabel}
|
|
23
|
+
aria-pressed={Boolean(state.agentFollowing)} onClick={() => workbench.setAgentFollowing(!state.agentFollowing)} />
|
|
24
|
+
</span>
|
|
25
|
+
}
|
package/src/MediaClipEditor.tsx
CHANGED
|
@@ -45,6 +45,7 @@ export interface MediaClipEditorProps {
|
|
|
45
45
|
onReview(): void
|
|
46
46
|
onApply(clip: MediaClip): void
|
|
47
47
|
onRemove(): void
|
|
48
|
+
onInteraction?(busy: boolean): void
|
|
48
49
|
onEditStart(): void
|
|
49
50
|
}
|
|
50
51
|
|
|
@@ -72,7 +73,7 @@ export function MediaClipEditor(props: MediaClipEditorProps) {
|
|
|
72
73
|
props.onApply({ ...clip, sourceRange: { startMs: startMs!, endMs: endMs! }, attachment: { ...clip.attachment, offsetMs: offsetMs! } })
|
|
73
74
|
}
|
|
74
75
|
const change = (patch: Partial<MediaClipDraft>) => props.onChange({ ...draft, ...patch })
|
|
75
|
-
const composition = (field: string) => ({ onCompositionStart: () => composing.current.add(field), onCompositionEnd: () => { composing.current.delete(field) } })
|
|
76
|
+
const composition = (field: string) => ({ onCompositionStart: () => { composing.current.add(field); props.onInteraction?.(true) }, onCompositionEnd: () => { composing.current.delete(field); props.onInteraction?.(composing.current.size > 0 || volumePreview !== undefined) } })
|
|
76
77
|
return <section className="pm-ui-media-clip-editor" aria-label={t.title} data-density={density} onFocusCapture={props.onEditStart} onPointerDownCapture={props.onEditStart}
|
|
77
78
|
onKeyDown={event => {
|
|
78
79
|
event.stopPropagation()
|
|
@@ -101,20 +102,20 @@ export function MediaClipEditor(props: MediaClipEditorProps) {
|
|
|
101
102
|
{rangeError && <span id={rangeErrorId} className="pm-ui-field__error" role="alert">{rangeError}</span>}
|
|
102
103
|
<PmInput label={t.offset} density={density} width="number" inputMode="decimal" maxLength={32} value={draft.offsetText ?? (clip.attachment.offsetMs / 1000).toFixed(3)}
|
|
103
104
|
readOnly={disabled || !inspected} error={offsetMs === undefined ? t.invalidOffset : undefined} onChange={event => change({ offsetText: event.target.value })} {...composition('offset')} />
|
|
104
|
-
<PmField label={t.volume} density={density} group><div className="pm-ui-
|
|
105
|
-
<PmRange label={t.volume} density={density} value={clip.volume * 100} max={100} disabled={disabled || !inspected} valueText={value => `${Math.round(value)}%`} onPreviewChange={setVolumePreview}
|
|
105
|
+
<PmField label={t.volume} density={density} group><div className="pm-ui-range-row">
|
|
106
|
+
<PmRange label={t.volume} density={density} value={clip.volume * 100} max={100} disabled={disabled || !inspected} valueText={value => `${Math.round(value)}%`} onPreviewChange={value => { setVolumePreview(value); props.onInteraction?.(value !== undefined || composing.current.size > 0) }}
|
|
106
107
|
onValueCommit={value => change({ clip: { ...clip, volume: value / 100 } })} />
|
|
107
108
|
<output>{Math.round(volumePreview ?? clip.volume * 100)}%</output><PmButton density={density} aria-pressed={Boolean(clip.muted)} disabled={disabled || !inspected} onClick={() => change({ clip: { ...clip, muted: !clip.muted } })}>{clip.muted ? t.unmute : t.mute}</PmButton>
|
|
108
109
|
</div></PmField>
|
|
109
110
|
<div className="pm-ui-media-clip-editor__actions">
|
|
110
111
|
<PmButton density={density} variant="primary" loading={busy === 'apply'} disabled={disabled || Boolean(busy) || !props.canApply || review || !valid} onClick={submit}>{selectedClipId ? t.apply : t.add}</PmButton>
|
|
111
112
|
<PmButton density={density} disabled={disabled || !inspected} onClick={() => change({ clip: { ...clip, sourceRange: { startMs: 0, endMs: inspected!.durationMs } }, startText: undefined, endText: undefined })}>{t.restore}</PmButton>
|
|
112
|
-
{selectedClipId && <PmButton density={density}
|
|
113
|
+
{selectedClipId && <PmButton density={density} loading={busy === 'remove'} disabled={disabled || Boolean(busy) || originalRemoved} onClick={props.onRemove}>{t.remove}</PmButton>}
|
|
113
114
|
</div>
|
|
114
115
|
</>}
|
|
115
|
-
{originalRemoved ? <PmNotice tone="warn">{t.removed}</PmNotice> : review ? <PmNotice tone="warn"
|
|
116
|
+
{originalRemoved ? <PmNotice tone="warn">{t.removed}</PmNotice> : review ? <PmNotice tone="warn" action={<PmButton density={density} disabled={disabled || Boolean(busy) || !props.canApply || !inspected} onClick={props.onReview}>{t.check}</PmButton>}>{t.review}</PmNotice>
|
|
116
117
|
: clip.assetId && !props.canApply && !busy && <span className="pm-ui-field__hint">{t.unavailable}</span>}
|
|
117
|
-
{props.failure && <PmNotice tone="danger"
|
|
118
|
+
{props.failure && <PmNotice tone="danger" action={<PmButton density={density} disabled={disabled || Boolean(busy)} onClick={props.retryImport ? props.onRetryImport : props.onRead}>{props.retryImport ? t.retryImport : t.retry}</PmButton>}>{props.failure}</PmNotice>}
|
|
118
119
|
{props.storageFailed && <PmNotice tone="warn">{t.storageFailed}</PmNotice>}
|
|
119
120
|
{props.pending && !props.failure && <span className="pm-ui-field__hint" role="status">{t.pending}</span>}
|
|
120
121
|
{props.status && <span className="pm-ui-field__hint" role="status">{props.status}</span>}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { useEffect, useRef, useState } from 'react'
|
|
2
|
-
import type {
|
|
2
|
+
import type { PresmiumDocument } from '@lincyaw/kernel'
|
|
3
3
|
import { PmButton, PmField, PmInput, PmSegmented } from './components.tsx'
|
|
4
4
|
import type { Locale } from './messages.ts'
|
|
5
5
|
import { stepNarrationStarts, stepTimingValue } from './step-production.ts'
|
|
@@ -40,16 +40,21 @@ interface DurationFieldProps {
|
|
|
40
40
|
onDraft?(draft: StepDurationDraft | undefined): void
|
|
41
41
|
onChange(value: number | null): void
|
|
42
42
|
onComposition?(composing: boolean): void
|
|
43
|
+
controlKey?: string
|
|
44
|
+
registerControl?(control: StepDraftControl): () => void
|
|
43
45
|
}
|
|
46
|
+
export interface StepDraftControl { composing(): boolean, clear(): void }
|
|
44
47
|
|
|
45
48
|
/** Short author intent, never an estimated timestamp or a second playback clock. */
|
|
46
|
-
function DurationField({ label, value, source, locale, readOnly, draft, onDraft, onChange, onComposition }: DurationFieldProps) {
|
|
47
|
-
const t = MESSAGES[locale], current = useRef({ onComposition, onChange, onDraft, value })
|
|
48
|
-
current.current = { onComposition, onChange, onDraft, value }
|
|
49
|
+
function DurationField({ label, value, source, locale, readOnly, draft, onDraft, onChange, onComposition, controlKey, registerControl }: DurationFieldProps) {
|
|
50
|
+
const t = MESSAGES[locale], current = useRef({ onComposition, onChange, onDraft, value, registerControl })
|
|
51
|
+
current.current = { onComposition, onChange, onDraft, value, registerControl }
|
|
49
52
|
const [text, setText] = useState(draft?.text ?? (value === undefined ? '' : String(value))), [error, setError] = useState(draft?.error ?? false)
|
|
50
53
|
const focused = useRef(false), composing = useRef(false)
|
|
51
54
|
useEffect(() => { if (!focused.current) { setText(draft?.text ?? (value === undefined ? '' : String(value))); setError(draft?.error ?? false) } }, [value, source, draft?.text, draft?.error, draft?.review])
|
|
52
55
|
useEffect(() => () => { if (composing.current) current.current.onComposition?.(false) }, [])
|
|
56
|
+
useEffect(() => current.current.registerControl?.({ composing: () => composing.current,
|
|
57
|
+
clear: () => { setText(current.current.value === undefined ? '' : String(current.current.value)); setError(false) } }), [controlKey])
|
|
53
58
|
const commit = (input: string): boolean => {
|
|
54
59
|
const normalized = input.normalize('NFKC').trim()
|
|
55
60
|
if (!normalized) { setError(false); current.current.onDraft?.(undefined); current.current.onChange(null); return true }
|
|
@@ -80,7 +85,7 @@ function DurationField({ label, value, source, locale, readOnly, draft, onDraft,
|
|
|
80
85
|
}
|
|
81
86
|
|
|
82
87
|
export interface StepProductionEditorProps {
|
|
83
|
-
document:
|
|
88
|
+
document: PresmiumDocument
|
|
84
89
|
stepId: string
|
|
85
90
|
locale: Locale
|
|
86
91
|
readOnly?: boolean
|
|
@@ -92,10 +97,12 @@ export interface StepProductionEditorProps {
|
|
|
92
97
|
draftStorageFailure?: boolean
|
|
93
98
|
onDraft?(key: StepDurationKey, draft: StepDurationDraft | undefined): void
|
|
94
99
|
onComposition?(key: StepDurationKey, composing: boolean): void
|
|
100
|
+
draftControlKey?: string
|
|
101
|
+
registerDraftControl?(key: StepDurationKey, control: StepDraftControl): () => void
|
|
95
102
|
}
|
|
96
103
|
|
|
97
104
|
/** A reusable view of the canonical step production, including inherited and migrated settings. */
|
|
98
|
-
export function StepProductionEditor({ document, stepId, locale, readOnly, advanced = true, onPause, onInteraction, onChange, drafts, onDraft, draftStorageFailure, onComposition }: StepProductionEditorProps) {
|
|
105
|
+
export function StepProductionEditor({ document, stepId, locale, readOnly, advanced = true, onPause, onInteraction, onChange, drafts, onDraft, draftStorageFailure, onComposition, draftControlKey, registerDraftControl }: StepProductionEditorProps) {
|
|
99
106
|
const t = MESSAGES[locale], [unifying, setUnifying] = useState(false)
|
|
100
107
|
const start = stepTimingValue(document, stepId, 'narrationStart'), minimum = stepTimingValue(document, stepId, 'minimumHoldMs'), trailing = stepTimingValue(document, stepId, 'trailingPauseMs')
|
|
101
108
|
const narrations = stepNarrationStarts(document, stepId), hasOverrides = narrations.some(value => value.overridden)
|
|
@@ -106,7 +113,7 @@ export function StepProductionEditor({ document, stepId, locale, readOnly, advan
|
|
|
106
113
|
return <section className="pm-ui-step-production" contentEditable={false} data-step-production={stepId} aria-label={t.timing}
|
|
107
114
|
onFocus={() => { onInteraction?.(); onPause() }} onKeyDown={event => event.stopPropagation()} onPointerDown={event => event.stopPropagation()}>
|
|
108
115
|
{advanced && <div className="pm-ui-step-production__heading"><span className="pm-ui-field__label">{t.timing}</span>
|
|
109
|
-
<PmButton small variant="
|
|
116
|
+
<PmButton small variant="text" disabled={readOnly || !hasStepTiming && !drafts?.minimumHoldMs && !drafts?.trailingPauseMs} title={t.restoreHint} onClick={() => { onDraft?.('minimumHoldMs', undefined); onDraft?.('trailingPauseMs', undefined); apply({ narrationStart: null, minimumHoldMs: null, trailingPauseMs: null }) }}>{t.restore}</PmButton>
|
|
110
117
|
</div>}
|
|
111
118
|
{!advanced ? <PmField group label={t.start} density="compact" hint={mixed ? t.mixed : undefined}>
|
|
112
119
|
<div className="pm-ui-step-production__actions"><PmSegmented label={t.start} value={mixed ? '' : narrations[0]?.value ?? start.value ?? 'with-animation'} options={options} disabled={readOnly} onChange={value => apply({ narrationStart: value as 'with-animation' | 'after-animation' })} /></div>
|
|
@@ -120,8 +127,10 @@ export function StepProductionEditor({ document, stepId, locale, readOnly, advan
|
|
|
120
127
|
</PmField>
|
|
121
128
|
<div className="pm-ui-step-production__durations">
|
|
122
129
|
<DurationField label={t.minimum} value={minimum.value} source={minimum.source} locale={locale} readOnly={readOnly} draft={drafts?.minimumHoldMs} onDraft={draft => onDraft?.('minimumHoldMs', draft)}
|
|
130
|
+
controlKey={draftControlKey} registerControl={registerDraftControl && (control => registerDraftControl('minimumHoldMs', control))}
|
|
123
131
|
onChange={value => apply({ minimumHoldMs: value })} onComposition={composing => onComposition?.('minimumHoldMs', composing)} />
|
|
124
132
|
<DurationField label={t.trailing} value={trailing.value} source={trailing.source} locale={locale} readOnly={readOnly} draft={drafts?.trailingPauseMs} onDraft={draft => onDraft?.('trailingPauseMs', draft)}
|
|
133
|
+
controlKey={draftControlKey} registerControl={registerDraftControl && (control => registerDraftControl('trailingPauseMs', control))}
|
|
125
134
|
onChange={value => apply({ trailingPauseMs: value })} onComposition={composing => onComposition?.('trailingPauseMs', composing)} />
|
|
126
135
|
</div></>}
|
|
127
136
|
{!advanced && (drafts?.minimumHoldMs || drafts?.trailingPauseMs || draftStorageFailure) ? <span className="pm-ui-field__error" role="alert">{t.advancedDraft}</span>
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export const AGENT_ACTIVITY_MESSAGES = {
|
|
2
|
+
'zh-CN': {
|
|
3
|
+
compact: { read: '查看中', edit: '编辑中', navigate: '定位中', preview: '预览中', build: '构建中', voice: '配音中', workspace: '整理中', done: '已完成', failed: '失败', cancelled: '已取消' },
|
|
4
|
+
read: 'AI 助手正在查看', edit: 'AI 助手正在编辑', navigate: 'AI 助手正在定位', preview: 'AI 助手正在查看预览', build: 'AI 助手正在构建', voice: 'AI 助手正在处理配音', workspace: 'AI 助手正在整理工作区',
|
|
5
|
+
done: 'AI 助手已完成', failed: 'AI 助手操作失败', cancelled: 'AI 助手已取消', following: '正在跟随', resume: '跟随 AI 助手', paused: '跟随已暂停',
|
|
6
|
+
followLabel: '跟随 AI 助手的操作位置', pauseLabel: '暂停跟随 AI 助手', concurrent: (count: number) => `${count} 项操作进行中`,
|
|
7
|
+
},
|
|
8
|
+
en: {
|
|
9
|
+
compact: { read: 'Reading', edit: 'Editing', navigate: 'Locating', preview: 'Previewing', build: 'Building', voice: 'Voicing', workspace: 'Organizing', done: 'Done', failed: 'Failed', cancelled: 'Canceled' },
|
|
10
|
+
read: 'AI Assistant is reading', edit: 'AI Assistant is editing', navigate: 'AI Assistant is navigating', preview: 'AI Assistant is inspecting the preview', build: 'AI Assistant is building', voice: 'AI Assistant is working on voiceover', workspace: 'AI Assistant is updating the workspace',
|
|
11
|
+
done: 'AI Assistant finished', failed: 'AI Assistant operation failed', cancelled: 'AI Assistant canceled', following: 'Following', resume: 'Follow AI Assistant', paused: 'Following paused',
|
|
12
|
+
followLabel: 'Follow the AI Assistant’s location', pauseLabel: 'Pause following the AI Assistant', concurrent: (count: number) => `${count} operations running`,
|
|
13
|
+
},
|
|
14
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { AgentActivity } from '@lincyaw/kernel'
|
|
2
|
+
import type { Workbench } from './workbench.ts'
|
|
3
|
+
|
|
4
|
+
export const latestAgentActivity = (activities: readonly AgentActivity[] = []) => [...activities].sort((a, b) => Number(b.state === 'running') - Number(a.state === 'running') || b.startedAt - a.startedAt)[0]
|
|
5
|
+
|
|
6
|
+
/** One root-lifetime subscription; reconnect resets observations, and human gestures always win navigation. */
|
|
7
|
+
export function connectAgentActivity(workbench: Workbench): () => void {
|
|
8
|
+
const subscribe = workbench.client.subscribeAgentActivity
|
|
9
|
+
if (!subscribe) return () => {}
|
|
10
|
+
let closed = false, followed: string | undefined, following: AbortController | undefined
|
|
11
|
+
let navigating: { document?: string, controller: AbortController } | undefined
|
|
12
|
+
workbench.setAgentFollowing(true)
|
|
13
|
+
const follow = () => {
|
|
14
|
+
if (closed || !workbench.state.agentFollowing) { following?.abort(); followed = undefined; return }
|
|
15
|
+
const activity = latestAgentActivity(workbench.state.agentActivities)
|
|
16
|
+
if (!activity || activity.id === followed) return
|
|
17
|
+
if (navigating && navigating.document === activity.document && !navigating.controller.signal.aborted) return
|
|
18
|
+
followed = activity.id; following?.abort(); following = new AbortController()
|
|
19
|
+
const task = { document: activity.document, controller: following }; navigating = task
|
|
20
|
+
void workbench.followAgentActivity(activity, following.signal).catch(() => {}).finally(() => {
|
|
21
|
+
if (navigating === task) { navigating = undefined; follow() }
|
|
22
|
+
})
|
|
23
|
+
}
|
|
24
|
+
const stop = subscribe(snapshot => { if (!closed) { workbench.setAgentActivities(snapshot.activities); follow() } }, () => {
|
|
25
|
+
following?.abort(); followed = undefined; workbench.setAgentActivities([])
|
|
26
|
+
})
|
|
27
|
+
const stopState = workbench.subscribe(follow)
|
|
28
|
+
const interrupt = (event: Event) => {
|
|
29
|
+
if (!workbench.state.agentActivities?.length || event.target instanceof Element && event.target.closest('[data-agent-activity-controls]')) return
|
|
30
|
+
if (event instanceof KeyboardEvent && !['ArrowLeft', 'ArrowRight', 'ArrowUp', 'ArrowDown', 'PageUp', 'PageDown', 'Home', 'End', 'Escape'].includes(event.key)) return
|
|
31
|
+
following?.abort(); workbench.setAgentFollowing(false)
|
|
32
|
+
}
|
|
33
|
+
document.addEventListener('pointerdown', interrupt, true); document.addEventListener('input', interrupt, true); document.addEventListener('wheel', interrupt, true); document.addEventListener('keydown', interrupt, true)
|
|
34
|
+
return () => {
|
|
35
|
+
closed = true; following?.abort(); stop(); stopState(); workbench.setAgentActivities([])
|
|
36
|
+
document.removeEventListener('pointerdown', interrupt, true); document.removeEventListener('input', interrupt, true); document.removeEventListener('wheel', interrupt, true); document.removeEventListener('keydown', interrupt, true)
|
|
37
|
+
}
|
|
38
|
+
}
|
package/src/approvals.tsx
CHANGED
|
@@ -6,6 +6,11 @@ import { WorkspaceServerError } from './workspace-client.ts'
|
|
|
6
6
|
|
|
7
7
|
interface Pending { id: string, request: ApprovalRequest }
|
|
8
8
|
|
|
9
|
+
/** Approval controls belong to the operation's editing context, including their portaled panel. */
|
|
10
|
+
export function isApprovalTarget(target: EventTarget | null | undefined): boolean {
|
|
11
|
+
return target instanceof Element && Boolean(target.closest('[data-approval-trigger], .pmw-approvals'))
|
|
12
|
+
}
|
|
13
|
+
|
|
9
14
|
/**
|
|
10
15
|
* The host's operations waiting for the author, such as an external MCP client's import: shown only while one is
|
|
11
16
|
* pending, answered here as in the viewer's "Approvals". The host times them out; a late answer is reported, not retried.
|
|
@@ -38,7 +43,7 @@ export function WorkbenchApprovals({ density = 'compact' }: { density?: 'compact
|
|
|
38
43
|
}
|
|
39
44
|
return (
|
|
40
45
|
<PmPopover open={open} onOpenChange={setOpen} label={t.approvalsTitle} name="pmw-approvals" trigger={
|
|
41
|
-
<PmButton variant="
|
|
46
|
+
<PmButton data-approval-trigger="" variant="primary" density={density}>{t.approvals(pending.length)}</PmButton>
|
|
42
47
|
}>
|
|
43
48
|
<h2 className="pmw-approvals__heading">{t.approvalsTitle}</h2>
|
|
44
49
|
<ul className="pmw-approvals__list">
|
|
@@ -2,9 +2,9 @@ import type { Locale } from './messages.ts'
|
|
|
2
2
|
|
|
3
3
|
const zh = {
|
|
4
4
|
trim: '录音区间', captionReview: '字幕尚未按裁剪后的录音对齐,请核对。',
|
|
5
|
-
title: '原音频', source: '本步原音', choose: '选择已保存音频', removedAudio: '原音频已移除', adopt: '
|
|
6
|
-
audition: '试听', stopAudition: '停止试听', save: '
|
|
7
|
-
ready: '原音候选 · 尚未保存', imported: '
|
|
5
|
+
title: '原音频', source: '本步原音', choose: '选择已保存音频', removedAudio: '原音频已移除', adopt: '用作配音', adopted: '已采用', retryAdoption: '重试采用', cancelAdoption: '取消采用', adoptionSourceChanged: '文稿已更新,请重试。', adoptionFailed: '无法采用这段音频,请重试。', start: '开始录音', stop: '停止录音', cancel: '取消', cancelTake: '放弃录音', rerecord: '重新录音',
|
|
6
|
+
audition: '试听', stopAudition: '停止试听', save: '保存音频', saving: '正在保存原音…', retry: '重试', discard: '放弃候选',
|
|
7
|
+
ready: '原音候选 · 尚未保存', imported: '已保存音频', requesting: '正在等待麦克风许可', recording: '正在录音', stopping: '正在完成录音…',
|
|
8
8
|
cancelSave: '取消保存', currentSource: '文稿尚未保存,请先完成当前输入。', saveFailed: '无法保存原音。录音已保留,请重试。',
|
|
9
9
|
discardTitle: '放弃这份原音?', discardHint: '未保存的录音将被删除,已保存的音频素材不受影响。', keep: '保留原音',
|
|
10
10
|
failures: {
|
|
@@ -23,8 +23,8 @@ const zh = {
|
|
|
23
23
|
type Messages = typeof zh
|
|
24
24
|
const en: Messages = {
|
|
25
25
|
trim: 'Recording range', captionReview: 'Captions are not aligned to the trimmed recording. Review them.',
|
|
26
|
-
title: 'Original audio', source: 'Original audio for this step', choose: 'Choose Saved Audio', removedAudio: 'Original audio was removed', adopt: 'Use
|
|
27
|
-
audition: 'Play Sample', stopAudition: 'Stop Sample', save: 'Save
|
|
26
|
+
title: 'Original audio', source: 'Original audio for this step', choose: 'Choose Saved Audio', removedAudio: 'Original audio was removed', adopt: 'Use Voiceover', adopted: 'Applied', retryAdoption: 'Retry', cancelAdoption: 'Cancel', adoptionSourceChanged: 'The document changed. Retry.', adoptionFailed: 'Couldn’t use this audio. Try again.', start: 'Start Recording', stop: 'Stop Recording', cancel: 'Cancel', cancelTake: 'Discard Recording', rerecord: 'Record Again',
|
|
27
|
+
audition: 'Play Sample', stopAudition: 'Stop Sample', save: 'Save Audio', saving: 'Saving original audio…', retry: 'Retry', discard: 'Discard Candidate',
|
|
28
28
|
ready: 'Original audio candidate · Not saved', imported: 'Saved as an audio asset', requesting: 'Waiting for microphone permission', recording: 'Recording', stopping: 'Finishing recording…',
|
|
29
29
|
cancelSave: 'Cancel', currentSource: 'Finish and save the current document input first.', saveFailed: 'Couldn’t save the original audio. The recording is kept; try again.',
|
|
30
30
|
discardTitle: 'Discard this original audio?', discardHint: 'The recording that is not saved will be deleted. Saved audio assets are kept.', keep: 'Keep Original Audio',
|
package/src/audio-recording.ts
CHANGED
|
@@ -10,6 +10,21 @@ const MAX_BYTES = 16 << 20
|
|
|
10
10
|
const MAX_DURATION_MS = 10 * 60_000
|
|
11
11
|
const MIME_TYPES = ['audio/webm;codecs=opus', 'audio/ogg;codecs=opus', 'audio/mp4']
|
|
12
12
|
|
|
13
|
+
/** getUserMedia has no AbortSignal; late permission grants must release their stream. */
|
|
14
|
+
function recordingWait<T>(work: Promise<T>, signal: AbortSignal | undefined, releaseLate: (value: T) => void): Promise<T> {
|
|
15
|
+
if (!signal) return work
|
|
16
|
+
return new Promise((resolve, reject) => {
|
|
17
|
+
const abort = () => { signal.removeEventListener('abort', abort); reject(signal.reason ?? new Error('recording-cancelled')) }
|
|
18
|
+
signal.addEventListener('abort', abort, { once: true })
|
|
19
|
+
work.then(value => {
|
|
20
|
+
signal.removeEventListener('abort', abort)
|
|
21
|
+
if (signal.aborted) { releaseLate(value); return }
|
|
22
|
+
resolve(value)
|
|
23
|
+
}, error => { signal.removeEventListener('abort', abort); reject(error) })
|
|
24
|
+
if (signal.aborted) abort()
|
|
25
|
+
})
|
|
26
|
+
}
|
|
27
|
+
|
|
13
28
|
export function recordingUnavailable(): string | undefined {
|
|
14
29
|
if (!globalThis.isSecureContext || !navigator.mediaDevices?.getUserMedia) return 'recording-context'
|
|
15
30
|
if (typeof MediaRecorder === 'undefined' || !MIME_TYPES.some(type => MediaRecorder.isTypeSupported(type))) return 'recording-unsupported'
|
|
@@ -36,6 +51,7 @@ export class AudioRecording {
|
|
|
36
51
|
#generation = 0
|
|
37
52
|
#disposed = false
|
|
38
53
|
#take?: Take
|
|
54
|
+
#request?: AbortController
|
|
39
55
|
#release?: () => void
|
|
40
56
|
#warning = false
|
|
41
57
|
readonly #beforeUnload = (event: BeforeUnloadEvent) => { event.preventDefault(); event.returnValue = '' }
|
|
@@ -57,19 +73,24 @@ export class AudioRecording {
|
|
|
57
73
|
this.#set({ phase: error ? 'failed' : this.#state.candidate ? 'ready' : 'idle', candidate: this.#state.candidate, elapsedMs: this.#state.candidate?.elapsedMs ?? 0, ...error ? { error } : {} })
|
|
58
74
|
}
|
|
59
75
|
|
|
60
|
-
async start(prepare: () => Promise<() => void
|
|
76
|
+
async start(prepare: (signal: AbortSignal) => Promise<() => void>, signal?: AbortSignal): Promise<void> {
|
|
77
|
+
if (signal?.aborted) return
|
|
61
78
|
if (this.#disposed || this.#take || this.#state.phase === 'requesting') return
|
|
62
79
|
const unavailable = recordingUnavailable()
|
|
63
80
|
if (unavailable) { this.#settle(unavailable); return }
|
|
64
81
|
const generation = ++this.#generation
|
|
82
|
+
const request = this.#request = new AbortController()
|
|
83
|
+
const pendingSignal = signal ? AbortSignal.any([signal, request.signal]) : request.signal
|
|
84
|
+
const abort = () => { if (generation === this.#generation) this.cancelTake() }
|
|
85
|
+
signal?.addEventListener('abort', abort, { once: true })
|
|
65
86
|
this.#set({ ...this.#state, phase: 'requesting', error: undefined })
|
|
66
87
|
let stream: MediaStream | undefined
|
|
67
88
|
let release: (() => void) | undefined
|
|
68
89
|
try {
|
|
69
|
-
release = await prepare()
|
|
90
|
+
release = await recordingWait(prepare(pendingSignal), pendingSignal, release => release())
|
|
70
91
|
if (generation !== this.#generation) { release(); return }
|
|
71
92
|
this.#release = release
|
|
72
|
-
stream = await navigator.mediaDevices.getUserMedia({ audio: { channelCount: { ideal: 1 } }, video: false })
|
|
93
|
+
stream = await recordingWait(navigator.mediaDevices.getUserMedia({ audio: { channelCount: { ideal: 1 } }, video: false }), pendingSignal, stream => stream.getTracks().forEach(track => track.stop()))
|
|
73
94
|
if (generation !== this.#generation) { stream.getTracks().forEach(track => track.stop()); return }
|
|
74
95
|
const mimeType = MIME_TYPES.find(type => MediaRecorder.isTypeSupported(type))!
|
|
75
96
|
const recorder = new MediaRecorder(stream, { mimeType, audioBitsPerSecond: 128_000 })
|
|
@@ -102,13 +123,13 @@ export class AudioRecording {
|
|
|
102
123
|
this.#take = undefined
|
|
103
124
|
const name = error instanceof DOMException ? error.name : ''
|
|
104
125
|
this.#settle(name === 'NotAllowedError' || name === 'SecurityError' ? 'recording-permission' : name === 'NotFoundError' ? 'recording-device' : name === 'NotReadableError' ? 'recording-device-busy' : name === 'NotSupportedError' ? 'recording-device-unsupported' : 'recording-failed')
|
|
105
|
-
}
|
|
126
|
+
} finally { signal?.removeEventListener('abort', abort); if (this.#request === request) this.#request = undefined }
|
|
106
127
|
}
|
|
107
128
|
|
|
108
129
|
stop(error?: string): void {
|
|
109
130
|
const take = this.#take
|
|
110
131
|
if (!take) {
|
|
111
|
-
if (this.#state.phase === 'requesting') { this.#generation++; this.#release?.(); this.#release = undefined; this.#settle(error) }
|
|
132
|
+
if (this.#state.phase === 'requesting') { this.#generation++; this.#request?.abort(new Error('recording-cancelled')); this.#request = undefined; this.#release?.(); this.#release = undefined; this.#settle(error) }
|
|
112
133
|
return
|
|
113
134
|
}
|
|
114
135
|
if (error) take.error ??= error
|
|
@@ -159,6 +180,7 @@ export class AudioRecording {
|
|
|
159
180
|
if (this.#disposed) return
|
|
160
181
|
this.#disposed = true
|
|
161
182
|
this.#generation++
|
|
183
|
+
this.#request?.abort(new Error('recording-cancelled')); this.#request = undefined
|
|
162
184
|
const take = this.#take
|
|
163
185
|
if (take) { take.discard = true; this.stop(); this.#finish(take) }
|
|
164
186
|
this.#release?.(); this.#release = undefined
|
package/src/authoring-client.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { authoringClientResultFits } from '@lincyaw/kernel'
|
|
1
|
+
import { authoringClientActionMutatesDraft, authoringClientResultFits, randomUUID } from '@lincyaw/kernel'
|
|
2
2
|
import type { AuthoringClientActionResult, AuthoringClientEvent, AuthoringClientSnapshot, CanonicalSource } from '@lincyaw/kernel'
|
|
3
3
|
import type { Workbench } from './workbench.ts'
|
|
4
4
|
import type { AuthoringClientChannel } from './workspace-client.ts'
|
|
5
5
|
|
|
6
6
|
const sameSource = (a: CanonicalSource, b: CanonicalSource) => a.documentId === b.documentId && a.version === b.version && a.fingerprint === b.fingerprint
|
|
7
7
|
// Clock samples are observations, not user input revisions. Never push at animation-frame frequency.
|
|
8
|
-
const signature = (value: AuthoringClientSnapshot) => JSON.stringify({ ...value, revision: 0, playback: value.playback && { ...value.playback,
|
|
8
|
+
const signature = (value: AuthoringClientSnapshot) => JSON.stringify({ ...value, revision: 0, recording: value.recording && { ...value.recording, elapsedMs: 0 }, playback: value.playback && { ...value.playback,
|
|
9
9
|
position: value.playback.position && { ...value.playback.position, timeMs: 0, stepTimeMs: 0 } } })
|
|
10
10
|
type Attachment = { directory: string, identity: string, epoch: string, channel: AuthoringClientChannel, connectionId?: string }
|
|
11
11
|
|
|
@@ -13,7 +13,7 @@ type Attachment = { directory: string, identity: string, epoch: string, channel:
|
|
|
13
13
|
export function connectAuthoringClient(workbench: Workbench): () => void {
|
|
14
14
|
const open = workbench.client.openAuthoringClient
|
|
15
15
|
if (!open) return () => {}
|
|
16
|
-
const clientId =
|
|
16
|
+
const clientId = randomUUID()
|
|
17
17
|
let closed = false, revision = 0, sequence = 0
|
|
18
18
|
let attached: Attachment | undefined
|
|
19
19
|
let lastSignature: string | undefined, lastContent: object | undefined
|
|
@@ -71,7 +71,7 @@ export function connectAuthoringClient(workbench: Workbench): () => void {
|
|
|
71
71
|
if (value) publishState(owner, event.connectionId, value)
|
|
72
72
|
return
|
|
73
73
|
}
|
|
74
|
-
if (event.type === 'cancel') { if (event.connectionId === owner.connectionId && event.requestId === active?.requestId) active.controller.abort(); return }
|
|
74
|
+
if (event.type === 'cancel') { if (event.connectionId === owner.connectionId && event.requestId === active?.requestId) active.controller.abort(new Error('client-request-cancelled')); return }
|
|
75
75
|
const request = event.request, connectionId = owner.connectionId
|
|
76
76
|
if (!connectionId || request.clientId !== clientId || request.epoch !== owner.epoch || request.expectedClientState.connectionId !== connectionId || seen.has(request.requestId)) return
|
|
77
77
|
seen.add(request.requestId)
|
|
@@ -83,7 +83,7 @@ export function connectAuthoringClient(workbench: Workbench): () => void {
|
|
|
83
83
|
controller.signal.throwIfAborted()
|
|
84
84
|
if (closed || attached !== owner || owner.connectionId !== connectionId || active !== task || Date.now() >= Date.parse(event.expiresAt)) throw new Error('client-request-expired')
|
|
85
85
|
}
|
|
86
|
-
if (active || value.revision !== request.expectedClientState.revision || !sameSource(value.source, request.source) || !value.actions.includes(request.action.type) || Date.now() >= Date.parse(event.expiresAt)) {
|
|
86
|
+
if (active || value.revision !== request.expectedClientState.revision || (request.action.type === 'body-drafts.retry' ? value.source.documentId !== request.source.documentId : !sameSource(value.source, request.source)) || !value.actions.includes(request.action.type) || Date.now() >= Date.parse(event.expiresAt)) {
|
|
87
87
|
await send(owner, connectionId, value, { requestId: request.requestId, state: 'refused', error: { code: 'client-state-changed' } }).catch(() => {})
|
|
88
88
|
return
|
|
89
89
|
}
|
|
@@ -95,7 +95,7 @@ export function connectAuthoringClient(workbench: Workbench): () => void {
|
|
|
95
95
|
await send(owner, connectionId, value, { requestId: request.requestId, state: 'executing' })
|
|
96
96
|
owned()
|
|
97
97
|
const latest = snapshot()
|
|
98
|
-
if (!latest || latest.revision !== request.expectedClientState.revision || !sameSource(latest.source, request.source)) throw new Error('client-state-changed')
|
|
98
|
+
if (!latest || latest.revision !== request.expectedClientState.revision || (request.action.type === 'body-drafts.retry' ? latest.source.documentId !== request.source.documentId : !sameSource(latest.source, request.source))) throw new Error('client-state-changed')
|
|
99
99
|
const fits = (result: AuthoringClientActionResult) => authoringClientResultFits({ request, connection: { clientId, epoch: owner.epoch, connectionId }, document: owner.directory, snapshot: snapshot() ?? value }, result)
|
|
100
100
|
const actionResult = await workbench.executeAuthoringClientAction(request.action, request.source, controller.signal, expiresAt, fits)
|
|
101
101
|
owned()
|
|
@@ -104,13 +104,13 @@ export function connectAuthoringClient(workbench: Workbench): () => void {
|
|
|
104
104
|
await send(owner, connectionId, result, { requestId: request.requestId, state: 'done', ...actionResult ? { result: actionResult } : {} }, actionResult ? () => {
|
|
105
105
|
owned()
|
|
106
106
|
const latest = snapshot()
|
|
107
|
-
if (!latest || latest.revision !== result.revision || !
|
|
108
|
-
if (!fits(actionResult)) throw new Error(actionResult.family
|
|
107
|
+
if (!latest || latest.revision !== result.revision || !authoringClientActionMutatesDraft(actionResult.type) && latest.revision !== request.expectedClientState.revision) throw new Error(`client-${actionResult.family}-drafts-changed`)
|
|
108
|
+
if (!fits(actionResult)) throw new Error(`client-${actionResult.family}-result-too-large`)
|
|
109
109
|
} : undefined)
|
|
110
110
|
} catch (error) {
|
|
111
111
|
const result = snapshot()
|
|
112
112
|
const code = error instanceof Error && /^(client|production)-[a-z-]{1,100}$/.test(error.message) ? error.message : 'client-action-not-verified'
|
|
113
|
-
if (
|
|
113
|
+
if (attached === owner && owner.connectionId === connectionId && result) await send(owner, connectionId, result, { requestId: request.requestId, state: 'failed', error: { code } }).catch(() => {})
|
|
114
114
|
} finally { clearTimeout(timeout); if (active === task) active = undefined }
|
|
115
115
|
}
|
|
116
116
|
const changed = () => {
|
|
@@ -120,7 +120,7 @@ export function connectAuthoringClient(workbench: Workbench): () => void {
|
|
|
120
120
|
if (!value) { detach(); return }
|
|
121
121
|
if (attached && (attached.directory !== state.document || attached.identity !== source.identity)) detach()
|
|
122
122
|
if (!attached) {
|
|
123
|
-
const epoch =
|
|
123
|
+
const epoch = randomUUID()
|
|
124
124
|
// Callbacks run after the channel has been installed; the socket implementation is asynchronous.
|
|
125
125
|
const owner = { directory: state.document, identity: source.identity, epoch } as Attachment
|
|
126
126
|
owner.channel = open(state.document, { clientId, epoch, snapshot: value }, event => { void received(owner, event) }, () => {
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import type { AuthoringClientActionResult } from '@lincyaw/kernel'
|
|
2
|
+
import { sha256 as draftContentHash } from './sha256.ts'
|
|
3
|
+
|
|
4
|
+
export { draftContentHash }
|
|
2
5
|
|
|
3
6
|
type Fragment = Extract<AuthoringClientActionResult, { jsonText: string }>
|
|
4
7
|
type Fields = 'offset' | 'endOffset' | 'totalUtf16' | 'totalUtf8' | 'sha256' | 'jsonText'
|
|
5
8
|
type FragmentBase = Fragment extends infer Value ? Value extends Fragment ? Omit<Value, Fields> : never : never
|
|
6
|
-
export const draftContentHash = async (text: string): Promise<string> => [...new Uint8Array(await crypto.subtle.digest('SHA-256', new TextEncoder().encode(text)))].map(value => value.toString(16).padStart(2, '0')).join('')
|
|
7
9
|
const boundary = (text: string, offset: number) => !(offset > 0 && offset < text.length && /[\uD800-\uDBFF]/.test(text[offset - 1]!) && /[\uDC00-\uDFFF]/.test(text[offset]!))
|
|
8
10
|
|
|
9
11
|
/** Budget the complete client receipt and WS frame, preserving exact JSON text and Unicode progress. */
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { randomUUID } from '@lincyaw/kernel'
|
|
2
|
+
import type { AuthoringWindowAcknowledgement, AuthoringWindowEvent, AuthoringWindowSnapshot } from '@lincyaw/kernel'
|
|
3
|
+
import type { Workbench } from './workbench.ts'
|
|
4
|
+
|
|
5
|
+
/** One root-lifetime reader on the existing socket, independent of document attachments. */
|
|
6
|
+
export function connectAuthoringWindow(workbench: Workbench): () => void {
|
|
7
|
+
const open = workbench.client.openAuthoringWindow
|
|
8
|
+
if (!open) return () => {}
|
|
9
|
+
const clientId = randomUUID(), epoch = randomUUID(), seen = new Set<string>()
|
|
10
|
+
let closed = false, connectionId: string | undefined, revision = 0, sequence = 0, signature: string | undefined
|
|
11
|
+
const compositions = new Set<EventTarget>()
|
|
12
|
+
let inputRevision = 0, lastInputRevision = 0
|
|
13
|
+
let content: object | undefined, drafts: string | undefined, playback: number | undefined, view: string | undefined
|
|
14
|
+
let active: { requestId: string, controller: AbortController, navigation: boolean } | undefined
|
|
15
|
+
let reports: Promise<void> = Promise.resolve(), pending: AuthoringWindowSnapshot | undefined, sending = false
|
|
16
|
+
const snapshot = () => {
|
|
17
|
+
for (const target of compositions) if (target instanceof Node && !target.isConnected) compositions.delete(target)
|
|
18
|
+
const value = workbench.authoringWindowSnapshot(0), state = workbench.state, bank = workbench.authoringDraftRevision()
|
|
19
|
+
if (compositions.size) value.actions = ['window.read']
|
|
20
|
+
const next = JSON.stringify(value)
|
|
21
|
+
if (next !== signature || content !== state.authoring?.content || bank !== drafts || playback !== state.productionNavigationVersion || view !== state.webView || inputRevision !== lastInputRevision) {
|
|
22
|
+
revision++; signature = next; content = state.authoring?.content; drafts = bank; playback = state.productionNavigationVersion; view = state.webView; lastInputRevision = inputRevision
|
|
23
|
+
}
|
|
24
|
+
return { ...value, revision }
|
|
25
|
+
}
|
|
26
|
+
const send = (connection: string, value: AuthoringWindowSnapshot, acknowledgement?: Omit<AuthoringWindowAcknowledgement, 'sequence' | 'snapshot'>, guard?: () => void) => {
|
|
27
|
+
const report = { sequence: ++sequence, snapshot: value, ...acknowledgement }
|
|
28
|
+
const next = reports.then(() => {
|
|
29
|
+
if (closed || connectionId !== connection) throw new Error('window-connection-changed')
|
|
30
|
+
guard?.()
|
|
31
|
+
return acknowledgement ? channel.acknowledge(connection, report as AuthoringWindowAcknowledgement) : channel.publish(connection, report)
|
|
32
|
+
})
|
|
33
|
+
reports = next.catch(() => {}); return next
|
|
34
|
+
}
|
|
35
|
+
const publish = (value: AuthoringWindowSnapshot) => {
|
|
36
|
+
pending = value
|
|
37
|
+
if (sending) return
|
|
38
|
+
sending = true
|
|
39
|
+
void (async () => {
|
|
40
|
+
try {
|
|
41
|
+
while (pending && connectionId) { pending = undefined; const connection = connectionId; await send(connection, snapshot()).catch(() => {}) }
|
|
42
|
+
} finally { sending = false }
|
|
43
|
+
})()
|
|
44
|
+
}
|
|
45
|
+
const received = async (event: AuthoringWindowEvent) => {
|
|
46
|
+
if (closed) return
|
|
47
|
+
if (event.type === 'connected') { active?.controller.abort(); active = undefined; connectionId = event.connectionId; publish(snapshot()); return }
|
|
48
|
+
if (event.type === 'cancel') { if (event.connectionId === connectionId && active?.requestId === event.requestId) active.controller.abort(new Error('window-request-cancelled')); return }
|
|
49
|
+
const connection = connectionId, request = event.request
|
|
50
|
+
if (!connection || request.clientId !== clientId || request.epoch !== epoch || request.connectionId !== connection || seen.has(request.requestId)) return
|
|
51
|
+
seen.add(request.requestId); if (seen.size > 64) seen.delete(seen.values().next().value!)
|
|
52
|
+
const initial = snapshot()
|
|
53
|
+
if (active || initial.revision !== request.revision || !initial.actions.includes(request.action.type) || Date.now() >= Date.parse(event.expiresAt)) {
|
|
54
|
+
await send(connection, initial, { requestId: request.requestId, state: 'refused', error: { code: 'window-state-changed' } }).catch(() => {}); return
|
|
55
|
+
}
|
|
56
|
+
const controller = new AbortController(), task = { requestId: request.requestId, controller, navigation: request.action.type !== 'window.read' }
|
|
57
|
+
active = task
|
|
58
|
+
const owned = () => { controller.signal.throwIfAborted(); if (closed || connectionId !== connection || active !== task || Date.now() >= Date.parse(event.expiresAt)) throw new Error('window-request-expired') }
|
|
59
|
+
const timeout = setTimeout(() => controller.abort(new Error('window-request-expired')), Math.max(0, Date.parse(event.expiresAt) - Date.now()))
|
|
60
|
+
try {
|
|
61
|
+
await send(connection, initial, { requestId: request.requestId, state: 'executing' }, () => { owned(); if (snapshot().revision !== request.revision) throw new Error('window-state-changed') })
|
|
62
|
+
owned()
|
|
63
|
+
if (snapshot().revision !== request.revision) throw new Error('window-state-changed')
|
|
64
|
+
const outcome = await workbench.executeAuthoringWindowAction(request, controller.signal)
|
|
65
|
+
owned()
|
|
66
|
+
const result = snapshot()
|
|
67
|
+
await send(connection, result, { requestId: request.requestId, state: 'done', outcome }, () => { owned(); if (snapshot().revision !== result.revision) throw new Error('window-state-changed') })
|
|
68
|
+
} catch (error) {
|
|
69
|
+
const code = error instanceof Error && /^window-[a-z-]{1,100}$/.test(error.message) ? error.message : 'window-action-not-verified'
|
|
70
|
+
if (!closed && connectionId === connection) await send(connection, snapshot(), { requestId: request.requestId, state: 'failed', error: { code } }).catch(() => {})
|
|
71
|
+
} finally { clearTimeout(timeout); if (active === task) active = undefined }
|
|
72
|
+
}
|
|
73
|
+
const channel = open({ clientId, epoch, snapshot: snapshot() }, event => { void received(event) }, () => { connectionId = undefined; pending = undefined; active?.controller.abort(); active = undefined })
|
|
74
|
+
const changed = () => { if (closed) return; const before = revision, value = snapshot(); if (before !== revision && connectionId) publish(value) }
|
|
75
|
+
const stop = workbench.subscribe(changed), stopDrafts = workbench.subscribeAuthoringDraftChanges(changed)
|
|
76
|
+
const interrupt = () => { if (active?.navigation) active.controller.abort(new Error('window-input-changed')) }
|
|
77
|
+
const input = () => { inputRevision++; interrupt(); changed() }
|
|
78
|
+
const escape = (event: KeyboardEvent) => { if (event.key === 'Escape') interrupt() }
|
|
79
|
+
const compositionStart = (event: CompositionEvent) => { if (event.target) compositions.add(event.target); input() }
|
|
80
|
+
const compositionEnd = (event: CompositionEvent) => { if (event.target) compositions.delete(event.target); input() }
|
|
81
|
+
// Only modal semantics matter. Text mutations and iframe animation frames never trigger publications.
|
|
82
|
+
const modal = typeof MutationObserver === 'undefined' ? undefined : new MutationObserver(records => {
|
|
83
|
+
const modalChange = records.some(record => record.type === 'attributes' || [...record.addedNodes, ...record.removedNodes].some(node => {
|
|
84
|
+
return node instanceof Element && (node.matches('[role="dialog"]') || node.querySelector('[role="dialog"]'))
|
|
85
|
+
}))
|
|
86
|
+
if (modalChange || compositions.size > 0 && records.some(record => record.removedNodes.length > 0)) changed()
|
|
87
|
+
})
|
|
88
|
+
if (typeof document !== 'undefined') {
|
|
89
|
+
modal?.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['aria-modal', 'hidden', 'inert'] })
|
|
90
|
+
document.addEventListener('input', input, true); document.addEventListener('compositionstart', compositionStart, true); document.addEventListener('compositionend', compositionEnd, true)
|
|
91
|
+
document.addEventListener('pointerdown', interrupt, true); document.addEventListener('keydown', escape, true)
|
|
92
|
+
}
|
|
93
|
+
return () => {
|
|
94
|
+
closed = true; stop(); stopDrafts(); modal?.disconnect(); active?.controller.abort(); channel.close(); pending = undefined; compositions.clear()
|
|
95
|
+
if (typeof document !== 'undefined') {
|
|
96
|
+
document.removeEventListener('input', input, true); document.removeEventListener('compositionstart', compositionStart, true); document.removeEventListener('compositionend', compositionEnd, true)
|
|
97
|
+
document.removeEventListener('pointerdown', interrupt, true); document.removeEventListener('keydown', escape, true)
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|