@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/src/document-session.ts
CHANGED
|
@@ -1,15 +1,19 @@
|
|
|
1
|
-
import { hasRole, validateDocument } from '@lincyaw/kernel'
|
|
2
|
-
import type { DocumentAnswer, DocumentChangeAnswer, DocumentImageImportAnswer, DocumentIssue, Member,
|
|
1
|
+
import { hasRole, isRecord, randomUUID, validateDocument } from '@lincyaw/kernel'
|
|
2
|
+
import type { CanonicalSource, ClientBodyDraftReference, DocumentAnswer, DocumentChangeAnswer, DocumentImageImportAnswer, DocumentIssue, Member, PresmiumDocument, DocumentDefaults } from '@lincyaw/kernel'
|
|
3
3
|
import { WorkspaceServerError } from './workspace-client.ts'
|
|
4
4
|
import type { WorkspaceClient } from './workspace-client.ts'
|
|
5
|
+
import { waitForDraft } from './document-draft-store.ts'
|
|
5
6
|
import { DocumentDrafts } from './document-drafts.ts'
|
|
7
|
+
import { sha256 } from './sha256.ts'
|
|
6
8
|
import type { DocumentDraftChoice, DocumentDraftCommit, DocumentDraftReference, StoredDocumentDraft } from './document-drafts.ts'
|
|
7
9
|
|
|
8
10
|
export interface DocumentSessionState {
|
|
9
11
|
directory: string
|
|
10
12
|
member?: Member
|
|
11
13
|
identity?: string
|
|
12
|
-
content?:
|
|
14
|
+
content?: PresmiumDocument
|
|
15
|
+
/** What the document inherits from its ancestors and the workspace defaults; `effectiveOf` fills it into `content`. */
|
|
16
|
+
inherited?: DocumentDefaults
|
|
13
17
|
version?: string
|
|
14
18
|
fingerprint?: string
|
|
15
19
|
projectRevision?: number
|
|
@@ -28,12 +32,16 @@ export interface DocumentSessionState {
|
|
|
28
32
|
recoveryDrafts?: DocumentDraftChoice[]
|
|
29
33
|
recoveryChanged?: boolean
|
|
30
34
|
}
|
|
35
|
+
export interface DocumentDraftOperation { signal: AbortSignal, guard(): void, source: CanonicalSource, localDraft?: true }
|
|
36
|
+
export interface BodyDraftCandidate { draft: DocumentDraftReference & { current: boolean }, content: PresmiumDocument, storage: 'durable' | 'rescue' | 'session', updatedAt: number, baseVersion: string, composing: boolean, pendingSubmission: boolean }
|
|
31
37
|
interface Slot {
|
|
38
|
+
observedSource?: CanonicalSource
|
|
32
39
|
state: DocumentSessionState
|
|
33
40
|
read: number
|
|
34
41
|
compositions: Set<string>
|
|
35
42
|
input: number
|
|
36
43
|
baseHash: string
|
|
44
|
+
baseContent: PresmiumDocument
|
|
37
45
|
storage: string
|
|
38
46
|
drafts: DocumentDrafts
|
|
39
47
|
backup?: DocumentDraftCommit
|
|
@@ -44,7 +52,7 @@ interface Slot {
|
|
|
44
52
|
legacyFailure?: 'read' | 'write'
|
|
45
53
|
persistFailure?: boolean
|
|
46
54
|
recoveryOperation?: number
|
|
47
|
-
flight?: { id: string, input: number, content:
|
|
55
|
+
flight?: { id: string, input: number, content: PresmiumDocument, baseVersion: string, hash?: string }
|
|
48
56
|
assetFlight?: Promise<DocumentChangeAnswer>
|
|
49
57
|
timer?: ReturnType<typeof setTimeout>
|
|
50
58
|
refreshed?: boolean
|
|
@@ -54,9 +62,10 @@ interface Slot {
|
|
|
54
62
|
}
|
|
55
63
|
|
|
56
64
|
const serialize = (value: unknown) => JSON.stringify(value, (_, entry) => entry && typeof entry === 'object' && !Array.isArray(entry) ? Object.fromEntries(Object.entries(entry).sort(([a], [b]) => a.localeCompare(b))) : entry)
|
|
65
|
+
const sameSource = (a: CanonicalSource | undefined, b: CanonicalSource) => a?.documentId === b.documentId && a.version === b.version && a.fingerprint === b.fingerprint
|
|
57
66
|
/** Non-body authoring changes keep native selection, comment drafts and body history. */
|
|
58
|
-
function keepsEditorContent(before:
|
|
59
|
-
const editing = ({ assets: _assets, production: _production, comments: _comments, definitions: _definitions, ...document }:
|
|
67
|
+
function keepsEditorContent(before: PresmiumDocument, after: PresmiumDocument): boolean {
|
|
68
|
+
const editing = ({ assets: _assets, production: _production, comments: _comments, definitions: _definitions, ...document }: PresmiumDocument) => {
|
|
60
69
|
const resolved = new Map<string, Map<string, { blockId: string, visible: boolean, state?: string }>>()
|
|
61
70
|
const visiting = new Set<string>()
|
|
62
71
|
const visuals = (id?: string): Map<string, { blockId: string, visible: boolean, state?: string }> => {
|
|
@@ -87,11 +96,22 @@ function keepsEditorContent(before: TeachingDocument, after: TeachingDocument):
|
|
|
87
96
|
&& Object.entries(before.assets).every(([id, asset]) => Object.hasOwn(after.assets, id) && serialize(asset) === serialize(after.assets[id]))
|
|
88
97
|
}
|
|
89
98
|
|
|
90
|
-
async function digest(value:
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
99
|
+
async function digest(value: PresmiumDocument): Promise<string> {
|
|
100
|
+
return sha256(serialize(value))
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** Replay pending author input on a newer saved document; an overlapping value belongs to the author. */
|
|
104
|
+
function rebaseDocument(base: PresmiumDocument, local: PresmiumDocument, remote: PresmiumDocument): PresmiumDocument {
|
|
105
|
+
const merge = (before: unknown, author: unknown, saved: unknown): unknown => {
|
|
106
|
+
if (serialize(author) === serialize(before)) return structuredClone(saved)
|
|
107
|
+
if (serialize(saved) === serialize(before) || serialize(author) === serialize(saved)) return structuredClone(author)
|
|
108
|
+
if (isRecord(before) && isRecord(author) && isRecord(saved)) return Object.fromEntries([...new Set([...Object.keys(before), ...Object.keys(author), ...Object.keys(saved)])]
|
|
109
|
+
.map(key => [key, merge(before[key], author[key], saved[key])] as const).filter(([, value]) => value !== undefined))
|
|
110
|
+
return structuredClone(author)
|
|
111
|
+
}
|
|
112
|
+
const merged = validateDocument(merge(base, local, remote))
|
|
113
|
+
// A structural conflict is one document operation: preserve the author's valid complete document.
|
|
114
|
+
return merged.document && !merged.issues.some(issue => issue.severity === 'error') ? merged.document : structuredClone(local)
|
|
95
115
|
}
|
|
96
116
|
|
|
97
117
|
/** One source actor per document. Runtime compilation and iframe lifecycle never own its input. */
|
|
@@ -125,7 +145,7 @@ export class DocumentSession {
|
|
|
125
145
|
this.#leaveTarget = target
|
|
126
146
|
}
|
|
127
147
|
|
|
128
|
-
async open(directory: string, expectedIdentity?: string): Promise<'identity-changed' | void> {
|
|
148
|
+
async open(directory: string, expectedIdentity?: string, signal?: AbortSignal): Promise<'identity-changed' | void> {
|
|
129
149
|
if (this.#active !== directory) this.interrupt()
|
|
130
150
|
const opening = ++this.#opening
|
|
131
151
|
this.#active = directory
|
|
@@ -134,7 +154,7 @@ export class DocumentSession {
|
|
|
134
154
|
if (cached) { cached.read++; cached.state = { ...cached.state, loading: true } }
|
|
135
155
|
this.#changed(cached ? { ...cached.state } : { directory, loading: true, dirty: false, saving: false, restored: false, composing: false, storageFailure: false, reset: 0, issues: [] })
|
|
136
156
|
try {
|
|
137
|
-
const [info, answer] = await Promise.all([this.#client.readWorkbenchInfo(), this.#client.readDocument(directory)])
|
|
157
|
+
const [info, answer] = await waitForDraft(Promise.all([this.#client.readWorkbenchInfo(), this.#client.readDocument(directory, signal)]), signal)
|
|
138
158
|
if (this.#active !== directory || opening !== this.#opening) return
|
|
139
159
|
if (expectedIdentity !== undefined && JSON.stringify([info.root, answer.content.id]) !== expectedIdentity) return 'identity-changed'
|
|
140
160
|
let slot = this.#slots.get(directory)
|
|
@@ -142,7 +162,7 @@ export class DocumentSession {
|
|
|
142
162
|
const baseHash = await digest(answer.content)
|
|
143
163
|
if (this.#disposed || this.#active !== directory || opening !== this.#opening) return
|
|
144
164
|
const drafts = new DocumentDrafts(() => { if (slot && !this.#disposed) void this.#readDrafts(slot) })
|
|
145
|
-
slot = { input: 0, read: 0, compositions: new Set(), drafts, baseHash, storage: `presmium.document.v1:${JSON.stringify([info.root, answer.content.id])}`, state: { directory, member: info.member, identity: JSON.stringify([info.root, answer.content.id]), content: answer.content, version: answer.version, fingerprint: answer.fingerprint, loading: true, dirty: false, saving: false, restored: false, composing: false, storageFailure: false, reset: 0, issues: answer.issues } }
|
|
165
|
+
slot = { observedSource: this.#source(answer), input: 0, read: 0, compositions: new Set(), drafts, baseHash, baseContent: structuredClone(answer.content), storage: `presmium.document.v1:${JSON.stringify([info.root, answer.content.id])}`, state: { directory, member: info.member, identity: JSON.stringify([info.root, answer.content.id]), content: answer.content, ...answer.inherited ? { inherited: answer.inherited } : {}, version: answer.version, fingerprint: answer.fingerprint, loading: true, dirty: false, saving: false, restored: false, composing: false, storageFailure: false, reset: 0, issues: answer.issues } }
|
|
146
166
|
this.#slots.set(directory, slot)
|
|
147
167
|
await this.#restore(slot, answer)
|
|
148
168
|
} else {
|
|
@@ -193,23 +213,77 @@ export class DocumentSession {
|
|
|
193
213
|
for (const changed of slot.saveWaiters ?? []) changed()
|
|
194
214
|
if (this.#active === slot.state.directory && this.#slots.get(slot.state.directory) === slot) this.#changed({ ...slot.state })
|
|
195
215
|
}
|
|
216
|
+
bodyDraftRevision(): number { const slot = this.#active === undefined ? undefined : this.#slots.get(this.#active); return slot?.drafts.revision ?? 0 }
|
|
217
|
+
hasPendingAssetWrite(): boolean { return Boolean(this.#active !== undefined && this.#slots.get(this.#active)?.assetFlight) }
|
|
218
|
+
bodyDraftCapabilities(): { read: boolean, mutate: boolean, current: boolean } {
|
|
219
|
+
const slot = this.#active === undefined ? undefined : this.#slots.get(this.#active)
|
|
220
|
+
const read = Boolean(slot?.state.identity && slot.state.content && slot.state.version && !slot.state.loading && !this.#disposed)
|
|
221
|
+
return { read, mutate: Boolean(read && slot && !this.#readOnly(slot) && !slot.state.composing && !slot.flight && !slot.assetFlight && !slot.state.recovering),
|
|
222
|
+
current: Boolean(slot && (slot.state.dirty || slot.state.restored || slot.state.composing || slot.state.error)) }
|
|
223
|
+
}
|
|
224
|
+
async readBodyDrafts(signal: AbortSignal, selected?: ClientBodyDraftReference): Promise<{ candidates: BodyDraftCandidate[], coverage: 'stored-and-session' | 'partial', guard(): void }> {
|
|
225
|
+
const slot = this.#active === undefined ? undefined : this.#slots.get(this.#active)
|
|
226
|
+
if (!slot || !this.bodyDraftCapabilities().read || !slot.state.identity || !slot.state.content || !slot.state.version) throw new Error('client-body-drafts-unavailable')
|
|
227
|
+
const identity = slot.state.identity, documentId = slot.state.content.id, input = slot.input, read = slot.read, content = slot.state.content, version = slot.state.version,
|
|
228
|
+
revision = slot.drafts.revision, composing = slot.state.composing, source = JSON.stringify(slot.observedSource), own = slot.draft, flight = slot.flight, assetFlight = slot.assetFlight
|
|
229
|
+
const guard = () => {
|
|
230
|
+
signal.throwIfAborted()
|
|
231
|
+
if (this.#disposed || this.#active !== slot.state.directory || this.#slots.get(slot.state.directory) !== slot || slot.state.identity !== identity
|
|
232
|
+
|| slot.input !== input || slot.read !== read || slot.state.content !== content || slot.state.version !== version || slot.state.composing !== composing
|
|
233
|
+
|| JSON.stringify(slot.observedSource) !== source || slot.draft !== own || slot.flight !== flight || slot.assetFlight !== assetFlight || slot.drafts.revision !== revision) throw new Error('client-body-drafts-changed')
|
|
234
|
+
}
|
|
235
|
+
guard()
|
|
236
|
+
const stored = await slot.drafts.read(identity, documentId, signal)
|
|
237
|
+
guard()
|
|
238
|
+
let candidates: BodyDraftCandidate[] = stored.records.filter(record => !(record.owned && record.id === slot.draft?.id)).map(record => ({
|
|
239
|
+
draft: { id: record.id, revision: record.revision, token: record.token, current: false }, content: record.content, storage: record.source,
|
|
240
|
+
updatedAt: record.updatedAt, baseVersion: record.baseVersion, composing: Boolean(record.composing), pendingSubmission: false,
|
|
241
|
+
}))
|
|
242
|
+
if (this.bodyDraftCapabilities().current) {
|
|
243
|
+
const token = await sha256(JSON.stringify({ domain: 'body-current', identity, id: slot.draft?.id ?? 'current', content, version, input, composing, pendingHash: slot.pendingHash }))
|
|
244
|
+
guard()
|
|
245
|
+
candidates.push({ draft: { id: slot.draft?.id ?? 'current', revision: input + 1, token, current: true }, content: structuredClone(content),
|
|
246
|
+
storage: stored.records.some(record => record.source === 'durable' && record.owned && record.id === slot.draft?.id && record.revision === slot.draft.revision && record.baseVersion === version && Boolean(record.composing) === composing && JSON.stringify(record.content) === JSON.stringify(content)) ? 'durable' : 'session', updatedAt: slot.draft?.updatedAt ?? 0,
|
|
247
|
+
baseVersion: version, composing, pendingSubmission: Boolean(flight || assetFlight) })
|
|
248
|
+
}
|
|
249
|
+
candidates.sort((a, b) => a.draft.token.localeCompare(b.draft.token))
|
|
250
|
+
if (selected) {
|
|
251
|
+
candidates = candidates.filter(value => value.draft.token === selected)
|
|
252
|
+
if (candidates.length !== 1) throw new Error('client-body-drafts-changed')
|
|
253
|
+
}
|
|
254
|
+
guard()
|
|
255
|
+
return { candidates, coverage: stored.failed ? 'partial' : 'stored-and-session', guard }
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
#source(answer: DocumentAnswer): CanonicalSource { return { documentId: answer.content.id, version: answer.version, fingerprint: answer.fingerprint } }
|
|
259
|
+
canonicalSource(): CanonicalSource | undefined { const slot = this.#active === undefined ? undefined : this.#slots.get(this.#active); return slot?.observedSource && { ...slot.observedSource } }
|
|
260
|
+
#checkOperation(operation?: DocumentDraftOperation, answer?: DocumentAnswer): void {
|
|
261
|
+
operation?.signal.throwIfAborted(); operation?.guard()
|
|
262
|
+
if (operation && !operation.localDraft && answer && !sameSource(this.#source(answer), operation.source)) throw new Error('client-source-changed')
|
|
263
|
+
}
|
|
196
264
|
#error(error: unknown): string { return error instanceof WorkspaceServerError ? error.answer.error ?? 'document-request-failed' : 'document-disconnected' }
|
|
197
265
|
#readOnly(slot: Slot): boolean { return this.#client.capabilities?.readOnly === true || Boolean(slot.state.member && !hasRole(slot.state.member, 'editor')) }
|
|
198
266
|
#issues(error: unknown): DocumentIssue[] { const value = error instanceof WorkspaceServerError ? (error.answer as { issues?: DocumentIssue[] }).issues : undefined; return Array.isArray(value) ? value : [] }
|
|
199
267
|
|
|
200
|
-
async refresh(directory = this.#active): Promise<void> {
|
|
268
|
+
async refresh(directory = this.#active, operation?: DocumentDraftOperation): Promise<void> {
|
|
201
269
|
if (directory === undefined) return
|
|
202
270
|
const slot = this.#slots.get(directory)
|
|
203
271
|
if (!slot) { if (directory === this.#active) await this.open(directory); return }
|
|
204
|
-
if (slot.flight || slot.assetFlight) { slot.refreshed = true; return }
|
|
205
|
-
const read = ++slot.read, current = () =>
|
|
272
|
+
if (slot.flight || slot.assetFlight) { slot.refreshed = true; if (operation) throw new Error('client-body-drafts-changed'); return }
|
|
273
|
+
const read = ++slot.read, current = () => {
|
|
274
|
+
const matches = slot.read === read && this.#slots.get(directory) === slot
|
|
275
|
+
if (operation && !matches) throw new Error('client-body-drafts-changed')
|
|
276
|
+
return matches
|
|
277
|
+
}
|
|
206
278
|
try {
|
|
207
|
-
const answer = await this.#client.readDocument(directory)
|
|
279
|
+
const answer = await this.#client.readDocument(directory, operation?.signal)
|
|
208
280
|
if (!current()) return
|
|
209
|
-
if (slot.flight || slot.assetFlight) { slot.refreshed = true; return }
|
|
210
|
-
|
|
281
|
+
if (slot.flight || slot.assetFlight) { slot.refreshed = true; if (operation) throw new Error('client-body-drafts-changed'); return }
|
|
282
|
+
this.#checkOperation(operation, answer)
|
|
283
|
+
await this.#adopt(slot, answer, () => { this.#checkOperation(operation); return current() })
|
|
211
284
|
if (current()) { this.#persist(slot); this.#emit(slot); void this.#readDrafts(slot) }
|
|
212
285
|
} catch (error) {
|
|
286
|
+
if (operation) throw error
|
|
213
287
|
if (current()) { slot.state = { ...slot.state, error: this.#error(error), issues: this.#issues(error) }; this.#emit(slot) }
|
|
214
288
|
}
|
|
215
289
|
}
|
|
@@ -217,6 +291,8 @@ export class DocumentSession {
|
|
|
217
291
|
async #adopt(slot: Slot, answer: DocumentAnswer, current = () => true): Promise<void> {
|
|
218
292
|
if (!current()) return
|
|
219
293
|
if (answer.content.id !== slot.state.content?.id) { slot.state = { ...slot.state, error: 'document-identity-changed' }; return }
|
|
294
|
+
if (answer.inherited) slot.state = { ...slot.state, inherited: answer.inherited }
|
|
295
|
+
slot.observedSource = this.#source(answer)
|
|
220
296
|
if (answer.version === slot.state.version) {
|
|
221
297
|
if (answer.fingerprint !== slot.state.fingerprint) slot.state = { ...slot.state, fingerprint: answer.fingerprint, issues: answer.issues }
|
|
222
298
|
return
|
|
@@ -235,15 +311,24 @@ export class DocumentSession {
|
|
|
235
311
|
slot.baseHash = remoteHash; slot.pendingHash = undefined
|
|
236
312
|
slot.state = { ...slot.state, version: answer.version, fingerprint: answer.fingerprint, error: undefined, issues: answer.issues }
|
|
237
313
|
} else if (remoteHash === slot.baseHash) slot.state = { ...slot.state, version: answer.version, fingerprint: answer.fingerprint, issues: answer.issues }
|
|
238
|
-
else
|
|
314
|
+
else if (!slot.state.composing) {
|
|
315
|
+
const content = rebaseDocument(slot.baseContent, slot.state.content!, answer.content)
|
|
316
|
+
const reset = slot.state.reset + (keepsEditorContent(slot.state.content!, content) ? 0 : 1)
|
|
317
|
+
slot.baseHash = remoteHash
|
|
318
|
+
slot.pendingHash = undefined
|
|
319
|
+
slot.state = { ...slot.state, content, reset, version: answer.version, fingerprint: answer.fingerprint, error: undefined, dirty: true, issues: answer.issues }
|
|
320
|
+
}
|
|
321
|
+
if (slot.state.version === answer.version) slot.baseContent = structuredClone(answer.content)
|
|
239
322
|
return
|
|
240
323
|
}
|
|
241
324
|
const reset = slot.state.reset + (keepsEditorContent(slot.state.content!, answer.content) ? 0 : 1)
|
|
242
325
|
slot.baseHash = remoteHash
|
|
326
|
+
slot.baseContent = structuredClone(answer.content)
|
|
327
|
+
slot.observedSource = this.#source(answer)
|
|
243
328
|
slot.state = { ...slot.state, version: answer.version, fingerprint: answer.fingerprint, content: answer.content, reset, error: undefined, issues: answer.issues }
|
|
244
329
|
}
|
|
245
330
|
|
|
246
|
-
record(content:
|
|
331
|
+
record(content: PresmiumDocument): void {
|
|
247
332
|
const slot = this.#active === undefined ? undefined : this.#slots.get(this.#active)
|
|
248
333
|
if (!slot || this.#readOnly(slot) || content.id !== slot.state.content?.id) return
|
|
249
334
|
slot.input++
|
|
@@ -258,7 +343,7 @@ export class DocumentSession {
|
|
|
258
343
|
if (slot.state.dirty && !slot.state.restored && !slot.state.error && !slot.state.composing) slot.timer = setTimeout(() => { slot.timer = undefined; void this.#save(slot) }, 600)
|
|
259
344
|
}
|
|
260
345
|
|
|
261
|
-
replace(content:
|
|
346
|
+
replace(content: PresmiumDocument): void {
|
|
262
347
|
const current = this.#active === undefined ? undefined : this.#slots.get(this.#active)
|
|
263
348
|
if (current && this.#readOnly(current)) return
|
|
264
349
|
this.record(content)
|
|
@@ -283,41 +368,59 @@ export class DocumentSession {
|
|
|
283
368
|
this.#persist(slot); this.#emit(slot)
|
|
284
369
|
}
|
|
285
370
|
|
|
286
|
-
async #save(slot: Slot, immediate = false): Promise<void> {
|
|
371
|
+
async #save(slot: Slot, immediate = false, operation?: DocumentDraftOperation): Promise<void> {
|
|
287
372
|
if (immediate) { clearTimeout(slot.timer); slot.timer = undefined }
|
|
288
373
|
else if (slot.timer !== undefined) return
|
|
289
374
|
if (this.#readOnly(slot) || slot.flight || slot.assetFlight || !slot.state.dirty || slot.state.composing || slot.state.restored || slot.state.error || !slot.state.content || !slot.state.version) return
|
|
375
|
+
const check = () => {
|
|
376
|
+
this.#checkOperation(operation)
|
|
377
|
+
if (operation && (slot.state.version !== operation.source.version || !sameSource(slot.observedSource, operation.source))) throw new Error('client-source-changed')
|
|
378
|
+
}
|
|
379
|
+
check()
|
|
290
380
|
slot.read++
|
|
291
|
-
const flight = { id:
|
|
381
|
+
const flight = { id: randomUUID(), input: slot.input, content: structuredClone(slot.state.content), baseVersion: slot.state.version, hash: undefined as string | undefined }
|
|
292
382
|
slot.flight = flight
|
|
383
|
+
let completed = false
|
|
293
384
|
slot.state = { ...slot.state, saving: true }; this.#emit(slot)
|
|
294
385
|
try {
|
|
295
386
|
flight.hash = await digest(flight.content)
|
|
387
|
+
check()
|
|
388
|
+
if (operation && (slot.input !== flight.input || slot.state.composing)) throw new Error('client-body-drafts-changed')
|
|
296
389
|
slot.pendingHash = flight.hash
|
|
297
390
|
this.#persist(slot)
|
|
298
|
-
const answer = await this.#client.changeDocument(slot.state.directory, { id: flight.id, baseDocumentVersion: flight.baseVersion, replacement: flight.content })
|
|
391
|
+
const answer = await this.#client.changeDocument(slot.state.directory, { id: flight.id, baseDocumentVersion: flight.baseVersion, replacement: flight.content }, operation?.signal)
|
|
299
392
|
if (answer.changeId !== flight.id || answer.document !== slot.state.directory || answer.content.id !== flight.content.id) throw new Error('document-confirmation')
|
|
300
393
|
this.#ack(slot, flight, answer)
|
|
394
|
+
completed = true
|
|
301
395
|
} catch (error) {
|
|
302
|
-
|
|
396
|
+
if (!operation && this.#error(error) === 'document-version-changed' && !slot.state.composing) {
|
|
397
|
+
try {
|
|
398
|
+
await this.#adopt(slot, await this.#client.readDocument(slot.state.directory))
|
|
399
|
+
slot.state = { ...slot.state, saving: false }
|
|
400
|
+
} catch (refreshError) { slot.state = { ...slot.state, saving: false, error: this.#error(refreshError), issues: this.#issues(refreshError) } }
|
|
401
|
+
} else slot.state = { ...slot.state, saving: false, error: this.#error(error), issues: this.#issues(error) }
|
|
402
|
+
if (operation) { slot.state = { ...slot.state, restored: true }; throw error }
|
|
303
403
|
} finally {
|
|
304
404
|
if (slot.flight === flight) slot.flight = undefined
|
|
305
405
|
this.#persist(slot); this.#emit(slot)
|
|
306
406
|
void this.#readDrafts(slot)
|
|
307
|
-
if (slot.refreshed) { slot.refreshed = false; await this.refresh(slot.state.directory) }
|
|
308
|
-
if (!slot.state.error) void this.#save(slot)
|
|
407
|
+
if (slot.refreshed && (!operation || completed)) { slot.refreshed = false; await this.refresh(slot.state.directory) }
|
|
408
|
+
if (!operation && !slot.state.error) void this.#save(slot)
|
|
409
|
+
else if (operation && completed && slot.input !== flight.input && !slot.state.error) this.#schedule(slot)
|
|
309
410
|
}
|
|
310
411
|
}
|
|
311
412
|
#ack(slot: Slot, flight: NonNullable<Slot['flight']>, answer: DocumentChangeAnswer): void {
|
|
413
|
+
slot.observedSource = this.#source(answer)
|
|
312
414
|
slot.baseHash = flight.hash!
|
|
415
|
+
slot.baseContent = structuredClone(answer.content)
|
|
313
416
|
slot.pendingHash = undefined
|
|
314
417
|
const same = slot.input === flight.input
|
|
315
418
|
slot.state = { ...slot.state, version: answer.version, fingerprint: answer.fingerprint, ...(same ? { content: answer.content } : {}), dirty: !same, saving: false, error: undefined, issues: answer.issues }
|
|
316
419
|
}
|
|
317
420
|
|
|
318
421
|
/** Adopt a verified asset-only transaction without replacing later native text. */
|
|
319
|
-
async #adoptImportedAssets(slot: Slot, before:
|
|
320
|
-
const withoutAssets = ({ assets: _assets, ...content }:
|
|
422
|
+
async #adoptImportedAssets(slot: Slot, before: PresmiumDocument, input: number, answer: DocumentAnswer, owns: () => boolean): Promise<boolean> {
|
|
423
|
+
const withoutAssets = ({ assets: _assets, ...content }: PresmiumDocument) => serialize(content)
|
|
321
424
|
if (!owns() || answer.document !== slot.state.directory || withoutAssets(before) !== withoutAssets(answer.content)
|
|
322
425
|
|| !Object.entries(before.assets).every(([id, asset]) => serialize(asset) === serialize(answer.content.assets[id]))) return false
|
|
323
426
|
const baseHash = await digest(answer.content)
|
|
@@ -328,11 +431,13 @@ export class DocumentSession {
|
|
|
328
431
|
const same = slot.input === input, content = same ? answer.content : structuredClone(current)
|
|
329
432
|
if (!same) for (const [id, asset] of added) content.assets[id] = structuredClone(asset)
|
|
330
433
|
slot.baseHash = baseHash
|
|
434
|
+
slot.baseContent = structuredClone(answer.content)
|
|
435
|
+
slot.observedSource = this.#source(answer)
|
|
331
436
|
slot.state = { ...slot.state, content, version: answer.version, fingerprint: answer.fingerprint, dirty: !same, error: undefined, issues: answer.issues }
|
|
332
437
|
return true
|
|
333
438
|
}
|
|
334
439
|
|
|
335
|
-
async #adoptExternal(slot: Slot, before:
|
|
440
|
+
async #adoptExternal(slot: Slot, before: PresmiumDocument, input: number, answer: DocumentAnswer, owns: () => boolean, reconcile: (before: PresmiumDocument, answer: DocumentAnswer, current: PresmiumDocument) => PresmiumDocument | undefined): Promise<boolean> {
|
|
336
441
|
if (!owns() || answer.document !== slot.state.directory || answer.content.id !== before.id || !slot.state.content) return false
|
|
337
442
|
if (!reconcile(before, answer, slot.state.content)) return false
|
|
338
443
|
const baseHash = await digest(answer.content)
|
|
@@ -342,14 +447,16 @@ export class DocumentSession {
|
|
|
342
447
|
const checked = validateDocument(content)
|
|
343
448
|
if (!checked.document || checked.issues.some(issue => issue.severity === 'error')) return false
|
|
344
449
|
slot.baseHash = baseHash
|
|
450
|
+
slot.baseContent = structuredClone(answer.content)
|
|
345
451
|
slot.pendingHash = undefined
|
|
452
|
+
slot.observedSource = this.#source(answer)
|
|
346
453
|
slot.state = { ...slot.state, content: structuredClone(checked.document), version: answer.version, fingerprint: answer.fingerprint,
|
|
347
454
|
dirty: slot.input !== input || slot.state.composing || serialize(content) !== serialize(answer.content), error: undefined, issues: answer.issues }
|
|
348
455
|
return true
|
|
349
456
|
}
|
|
350
457
|
|
|
351
458
|
/** Reconcile an external source transaction without replacing later native input. */
|
|
352
|
-
async commitExternal<T extends DocumentChangeAnswer>(expected: { identity: string, version: string }, submit: (before:
|
|
459
|
+
async commitExternal<T extends DocumentChangeAnswer>(expected: { identity: string, version: string }, submit: (before: PresmiumDocument, signal?: AbortSignal) => Promise<T>, reconcile: (before: PresmiumDocument, answer: DocumentAnswer, current: PresmiumDocument) => PresmiumDocument | undefined, signal?: AbortSignal): Promise<T> {
|
|
353
460
|
signal?.throwIfAborted()
|
|
354
461
|
const slot = this.#active === undefined ? undefined : this.#slots.get(this.#active)
|
|
355
462
|
if (slot && this.#readOnly(slot)) throw new WorkspaceServerError(403, { error: 'permission-denied' })
|
|
@@ -406,7 +513,7 @@ export class DocumentSession {
|
|
|
406
513
|
const before = structuredClone(slot.state.content), input = slot.input, version = slot.state.version
|
|
407
514
|
let request = this.#imageImports.get(file)
|
|
408
515
|
if (!request || request.identity !== identity || request.version !== version) {
|
|
409
|
-
request = { identity, version, id:
|
|
516
|
+
request = { identity, version, id: randomUUID() }
|
|
410
517
|
this.#imageImports.set(file, request)
|
|
411
518
|
}
|
|
412
519
|
const read = ++slot.read
|
|
@@ -443,39 +550,58 @@ export class DocumentSession {
|
|
|
443
550
|
}
|
|
444
551
|
}
|
|
445
552
|
|
|
446
|
-
async retry(): Promise<void> {
|
|
553
|
+
async retry(operation?: DocumentDraftOperation): Promise<void> {
|
|
447
554
|
if (this.#disposed || this.#active === undefined) return
|
|
448
555
|
const slot = this.#slots.get(this.#active)
|
|
449
556
|
if (!slot) { await this.open(this.#active); return }
|
|
450
557
|
if (slot.flight || slot.assetFlight || slot.state.composing) return
|
|
451
|
-
|
|
452
|
-
slot.state =
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
558
|
+
this.#checkOperation(operation)
|
|
559
|
+
const previousError = slot.state.error, restored = slot.state.restored, input = slot.input
|
|
560
|
+
if (!operation) slot.state = { ...slot.state, restored: false, error: undefined }
|
|
561
|
+
try {
|
|
562
|
+
await this.refresh(slot.state.directory, operation)
|
|
563
|
+
this.#checkOperation(operation)
|
|
564
|
+
if (operation && slot.input !== input) throw new Error('client-body-drafts-changed')
|
|
565
|
+
if (operation) slot.state = { ...slot.state, restored: false, error: undefined }
|
|
566
|
+
if (slot.state.error) { if (!previousError) this.#emit(slot); return }
|
|
567
|
+
await this.#save(slot, true, operation)
|
|
568
|
+
} catch (error) {
|
|
569
|
+
if (operation && slot.input === input && this.#slots.get(slot.state.directory) === slot) { slot.state = { ...slot.state, restored: restored || slot.state.restored, error: slot.state.error ?? previousError }; this.#emit(slot) }
|
|
570
|
+
throw error
|
|
571
|
+
}
|
|
456
572
|
}
|
|
457
|
-
async discard(): Promise<void> {
|
|
573
|
+
async discard(operation?: DocumentDraftOperation): Promise<boolean | void> {
|
|
458
574
|
const slot = this.#active === undefined ? undefined : this.#slots.get(this.#active)
|
|
459
575
|
if (!slot || slot.flight || slot.assetFlight || slot.state.composing) return
|
|
576
|
+
const pendingSave = slot.timer !== undefined
|
|
577
|
+
clearTimeout(slot.timer); slot.timer = undefined
|
|
578
|
+
let discarded = false
|
|
460
579
|
const directory = slot.state.directory, identity = slot.state.identity, documentId = slot.state.content?.id, input = slot.input, read = ++slot.read
|
|
461
|
-
const current = () => !this.#disposed && this.#slots.get(directory) === slot && slot.read === read && slot.input === input
|
|
462
|
-
&& slot.state.identity === identity && !slot.state.composing && !slot.flight && !slot.assetFlight
|
|
580
|
+
const current = () => { this.#checkOperation(operation); return !this.#disposed && this.#slots.get(directory) === slot && slot.read === read && slot.input === input
|
|
581
|
+
&& slot.state.identity === identity && !slot.state.composing && !slot.flight && !slot.assetFlight }
|
|
463
582
|
try {
|
|
464
|
-
const answer = await this.#client.readDocument(directory)
|
|
583
|
+
const answer = await this.#client.readDocument(directory, operation?.signal)
|
|
465
584
|
if (!current()) return
|
|
466
585
|
if (answer.document !== directory || answer.content.id !== documentId) {
|
|
467
586
|
slot.state = { ...slot.state, error: 'document-identity-changed' }; this.#emit(slot)
|
|
468
587
|
return
|
|
469
588
|
}
|
|
589
|
+
this.#checkOperation(operation, answer)
|
|
470
590
|
const baseHash = await digest(answer.content)
|
|
591
|
+
this.#checkOperation(operation)
|
|
471
592
|
if (!current()) return
|
|
593
|
+
slot.observedSource = this.#source(answer)
|
|
594
|
+
discarded = true
|
|
472
595
|
slot.input++; slot.baseHash = baseHash
|
|
473
596
|
slot.pendingHash = undefined
|
|
474
597
|
slot.compositions.clear()
|
|
475
598
|
slot.state = { ...slot.state, content: answer.content, version: answer.version, fingerprint: answer.fingerprint, dirty: false, restored: false, composing: false, reset: slot.state.reset + 1, error: undefined, issues: answer.issues }
|
|
476
599
|
this.#persist(slot); this.#emit(slot)
|
|
600
|
+
if (operation && slot.backup) await waitForDraft(slot.backup.done, operation.signal)
|
|
477
601
|
void this.#readDrafts(slot)
|
|
478
|
-
|
|
602
|
+
return true
|
|
603
|
+
} catch (error) { if (operation) throw error; if (current()) { slot.state = { ...slot.state, error: this.#error(error) }; this.#emit(slot) } }
|
|
604
|
+
finally { if (!discarded && pendingSave && this.#slots.get(directory) === slot && slot.input === input && slot.timer === undefined) this.#schedule(slot) }
|
|
479
605
|
}
|
|
480
606
|
async flush(signal?: AbortSignal): Promise<void> {
|
|
481
607
|
signal?.throwIfAborted()
|
|
@@ -584,7 +710,7 @@ export class DocumentSession {
|
|
|
584
710
|
}
|
|
585
711
|
|
|
586
712
|
/** Selecting a candidate never writes the source or replaces this tab's only snapshot. */
|
|
587
|
-
async recoverDraft(value: DocumentDraftReference): Promise<void> {
|
|
713
|
+
async recoverDraft(value: DocumentDraftReference, request?: DocumentDraftOperation): Promise<'restored' | 'already-saved' | void> {
|
|
588
714
|
const slot = this.#active === undefined ? undefined : this.#slots.get(this.#active)
|
|
589
715
|
if (slot && this.#readOnly(slot)) return
|
|
590
716
|
if (!slot?.state.identity || !slot.state.content || slot.state.loading || slot.flight || slot.assetFlight || slot.state.composing || this.#disposed) return
|
|
@@ -594,47 +720,61 @@ export class DocumentSession {
|
|
|
594
720
|
clearTimeout(slot.timer); slot.timer = undefined
|
|
595
721
|
let adopted = false
|
|
596
722
|
let protectedRecord: { id?: string, revision?: number } | undefined
|
|
597
|
-
const current = () => !this.#disposed && this.#active === slot.state.directory && this.#slots.get(slot.state.directory) === slot && slot.state.identity === identity && slot.read === read && slot.input === input && slot.recoveryOperation === operation
|
|
598
|
-
&& !slot.state.composing && !slot.flight && !slot.assetFlight && (!protectedRecord || slot.draft?.id === protectedRecord.id && slot.draft?.revision === protectedRecord.revision)
|
|
723
|
+
const current = () => { this.#checkOperation(request); return !this.#disposed && this.#active === slot.state.directory && this.#slots.get(slot.state.directory) === slot && slot.state.identity === identity && slot.read === read && slot.input === input && slot.recoveryOperation === operation
|
|
724
|
+
&& !slot.state.composing && !slot.flight && !slot.assetFlight && (!protectedRecord || slot.draft?.id === protectedRecord.id && slot.draft?.revision === protectedRecord.revision) }
|
|
599
725
|
slot.state = { ...slot.state, recovering: true, recoveryChanged: false }; this.#emit(slot)
|
|
600
726
|
try {
|
|
727
|
+
this.#checkOperation(request)
|
|
601
728
|
if (!this.#persist(slot)) { this.#emit(slot); return }
|
|
602
729
|
const backup = slot.backup
|
|
603
|
-
if (backup && await backup.done !== 'committed') return
|
|
730
|
+
if (backup && await waitForDraft(backup.done, request?.signal) !== 'committed') return
|
|
604
731
|
if (!current()) return
|
|
605
732
|
protectedRecord = { id: slot.draft?.id, revision: slot.draft?.revision }
|
|
606
|
-
const candidate = (await slot.drafts.read(identity, documentId)).records.find(record => record.id === value.id && record.revision === value.revision && record.token === value.token)
|
|
733
|
+
const candidate = (await slot.drafts.read(identity, documentId, request?.signal)).records.find(record => record.id === value.id && record.revision === value.revision && record.token === value.token)
|
|
607
734
|
if (!current()) return
|
|
735
|
+
this.#checkOperation(request)
|
|
608
736
|
if (!candidate) { slot.state = { ...slot.state, recoveryChanged: true }; return }
|
|
609
|
-
const answer = await this.#client.readDocument(slot.state.directory)
|
|
737
|
+
const answer = await this.#client.readDocument(slot.state.directory, request?.signal)
|
|
610
738
|
if (!current()) return
|
|
611
739
|
if (answer.document !== slot.state.directory || answer.content.id !== documentId) { slot.state = { ...slot.state, error: 'document-identity-changed' }; return }
|
|
612
|
-
|
|
740
|
+
this.#checkOperation(request, answer)
|
|
741
|
+
slot.observedSource = this.#source(answer)
|
|
742
|
+
// Older LAN HTTP drafts stored canonical JSON in place of a SHA-256 fingerprint.
|
|
743
|
+
const [localHash, remoteHash, candidateBaseHash, pendingHash] = await Promise.all([
|
|
744
|
+
digest(candidate.content), digest(answer.content),
|
|
745
|
+
candidate.baseHash.startsWith('json:') ? sha256(candidate.baseHash.slice(5)) : candidate.baseHash,
|
|
746
|
+
candidate.pendingHash?.startsWith('json:') ? sha256(candidate.pendingHash.slice(5)) : candidate.pendingHash,
|
|
747
|
+
])
|
|
613
748
|
if (!current()) return
|
|
749
|
+
this.#checkOperation(request)
|
|
614
750
|
const origins = [...new Map([...(candidate.origins ?? []), { id: candidate.id, revision: candidate.revision, token: candidate.token }].map(value => [JSON.stringify([value.id, value.revision, value.token]), value])).values()]
|
|
615
751
|
if (localHash === remoteHash && !candidate.composing) {
|
|
616
|
-
const settled = await slot.drafts.settle(identity, origins)
|
|
752
|
+
const settled = await slot.drafts.settle(identity, origins, request && { ...request, guard: () => { if (!current()) throw new Error('client-body-drafts-changed') } })
|
|
617
753
|
if (!current()) return
|
|
618
754
|
slot.persistFailure = !settled
|
|
619
|
-
return
|
|
755
|
+
if (!settled) return
|
|
756
|
+
return 'already-saved'
|
|
620
757
|
}
|
|
621
|
-
const conflict = answer.version !== candidate.baseVersion && remoteHash !==
|
|
622
|
-
const version = remoteHash ===
|
|
623
|
-
const baseHash = remoteHash ===
|
|
758
|
+
const conflict = answer.version !== candidate.baseVersion && remoteHash !== candidateBaseHash && remoteHash !== pendingHash
|
|
759
|
+
const version = remoteHash === pendingHash || remoteHash === candidateBaseHash ? answer.version : candidate.baseVersion
|
|
760
|
+
const baseHash = remoteHash === pendingHash ? remoteHash : candidateBaseHash
|
|
624
761
|
const record = await slot.drafts.fork(identity, { content: candidate.content, baseVersion: version, baseHash, input: input + 1,
|
|
625
|
-
pendingHash
|
|
762
|
+
pendingHash, composing: Boolean(candidate.composing), origins }, request && { ...request, guard: () => { if (!current()) throw new Error('client-body-drafts-changed') } })
|
|
626
763
|
if (!current()) { if (record) slot.drafts.archive(record); return }
|
|
627
764
|
if (!record) { slot.persistFailure = true; slot.state = { ...slot.state, storageFailure: true }; return }
|
|
628
765
|
clearTimeout(slot.timer); slot.timer = undefined
|
|
766
|
+
this.#checkOperation(request)
|
|
629
767
|
if (slot.draft) slot.drafts.archive(slot.draft)
|
|
630
|
-
slot.input = input + 1; slot.baseHash = baseHash; slot.pendingHash =
|
|
768
|
+
slot.input = input + 1; slot.baseHash = baseHash; slot.pendingHash = pendingHash; slot.draft = record; slot.draftOrigins = origins
|
|
631
769
|
slot.backup = undefined; slot.persistFailure = false
|
|
632
770
|
adopted = true
|
|
633
771
|
slot.compositions.clear()
|
|
634
772
|
slot.state = { ...slot.state, content: candidate.content, version, fingerprint: answer.fingerprint, dirty: true, restored: true, composing: false,
|
|
635
773
|
backupPending: false, storageFailure: slot.legacyFailure === 'write', reset: slot.state.reset + 1,
|
|
636
774
|
error: conflict ? 'document-version-changed' : candidate.composing ? 'document-composition-interrupted' : undefined, issues: answer.issues }
|
|
775
|
+
return 'restored'
|
|
637
776
|
} catch (error) {
|
|
777
|
+
if (request) throw error
|
|
638
778
|
if (current()) slot.state = { ...slot.state, error: this.#error(error), issues: this.#issues(error) }
|
|
639
779
|
} finally {
|
|
640
780
|
if (!this.#disposed && this.#slots.get(slot.state.directory) === slot && slot.state.identity === identity && slot.recoveryOperation === operation) {
|
|
@@ -644,25 +784,34 @@ export class DocumentSession {
|
|
|
644
784
|
}
|
|
645
785
|
}
|
|
646
786
|
|
|
647
|
-
async forgetDraft(value: DocumentDraftReference): Promise<void> {
|
|
787
|
+
async forgetDraft(value: DocumentDraftReference, request?: DocumentDraftOperation): Promise<boolean | void> {
|
|
788
|
+
return this.forgetDrafts([value], request)
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
/** Retire only the displayed revisions together; another tab's newer input stays recoverable. */
|
|
792
|
+
async forgetDrafts(values: readonly DocumentDraftReference[], request?: DocumentDraftOperation): Promise<boolean | void> {
|
|
648
793
|
const slot = this.#active === undefined ? undefined : this.#slots.get(this.#active)
|
|
649
|
-
if (!slot?.state.identity || !slot.state.content || slot.state.recovering || slot.flight || slot.assetFlight || slot.state.composing || this.#disposed) return
|
|
794
|
+
if (!values.length || !slot?.state.identity || !slot.state.content || slot.state.recovering || slot.flight || slot.assetFlight || slot.state.composing || this.#disposed) return
|
|
650
795
|
const identity = slot.state.identity, documentId = slot.state.content.id, input = slot.input, read = slot.read
|
|
651
796
|
const operation = slot.recoveryOperation = (slot.recoveryOperation ?? 0) + 1
|
|
652
|
-
const current = () => !this.#disposed && this.#active === slot.state.directory && this.#slots.get(slot.state.directory) === slot && slot.state.identity === identity && slot.input === input && slot.read === read && slot.recoveryOperation === operation && !slot.state.composing && !slot.flight && !slot.assetFlight
|
|
797
|
+
const current = () => { this.#checkOperation(request); return !this.#disposed && this.#active === slot.state.directory && this.#slots.get(slot.state.directory) === slot && slot.state.identity === identity && slot.input === input && slot.read === read && slot.recoveryOperation === operation && !slot.state.composing && !slot.flight && !slot.assetFlight }
|
|
653
798
|
slot.state = { ...slot.state, recovering: true }; this.#emit(slot)
|
|
654
799
|
try {
|
|
655
|
-
|
|
800
|
+
this.#checkOperation(request)
|
|
801
|
+
const records = (await slot.drafts.read(identity, documentId, request?.signal)).records
|
|
802
|
+
const candidates = values.map(value => records.find(record => record.id === value.id && record.revision === value.revision && record.token === value.token))
|
|
656
803
|
if (!current()) return
|
|
657
|
-
|
|
658
|
-
if (
|
|
804
|
+
this.#checkOperation(request)
|
|
805
|
+
if (candidates.some(candidate => candidate?.owned && candidate.id === slot.draft?.id)) return
|
|
806
|
+
if (candidates.some(candidate => !candidate)) slot.state = { ...slot.state, recoveryChanged: true }
|
|
659
807
|
else {
|
|
660
|
-
const settled = await slot.drafts.settle(identity,
|
|
808
|
+
const settled = await slot.drafts.settle(identity, values, request && { ...request, guard: () => { if (!current()) throw new Error('client-body-drafts-changed') } })
|
|
661
809
|
if (!current()) return
|
|
662
810
|
slot.persistFailure = !settled
|
|
663
811
|
slot.state = { ...slot.state, recoveryChanged: false, storageFailure: Boolean(slot.persistFailure || slot.legacyFailure === 'write') }
|
|
812
|
+
return settled
|
|
664
813
|
}
|
|
665
|
-
} catch { if (current()) { slot.persistFailure = true; slot.state = { ...slot.state, storageFailure: true } } }
|
|
814
|
+
} catch (error) { if (request) throw error; if (current()) { slot.persistFailure = true; slot.state = { ...slot.state, storageFailure: true } } }
|
|
666
815
|
finally {
|
|
667
816
|
if (!this.#disposed && this.#slots.get(slot.state.directory) === slot && slot.state.identity === identity && slot.recoveryOperation === operation) {
|
|
668
817
|
slot.state = { ...slot.state, recovering: false }; this.#emit(slot); void this.#readDrafts(slot)
|