@lincyaw/workbench 0.2.0-rc.5 → 0.2.0-rc.51
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +5 -4
- package/src/MediaClipEditor.tsx +1 -1
- 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/body-draft-actions.ts +60 -0
- package/src/component-property-drafts.ts +5 -5
- package/src/components.tsx +10 -28
- package/src/context.tsx +2 -1
- package/src/diagnostics.ts +1 -1
- package/src/document-draft-store.ts +45 -18
- package/src/document-drafts.ts +28 -19
- package/src/document-editing.ts +484 -0
- package/src/document-editor/BlockNoteSurface.tsx +25 -9
- package/src/document-editor/ComponentMaterials.tsx +3 -9
- package/src/document-editor/ComponentPropertiesDialog.tsx +4 -4
- package/src/document-editor/DocumentBlockPreview.tsx +4 -2
- package/src/document-editor/DocumentComments.tsx +58 -9
- package/src/document-editor/DocumentEditor.tsx +44 -7
- package/src/document-editor/DocumentStepNarration.tsx +30 -25
- package/src/document-editor/NarrationEditor.tsx +54 -20
- package/src/document-editor/NewCommentDrafts.tsx +4 -3
- package/src/document-editor/PlaceComment.tsx +150 -0
- package/src/document-editor/comment-drafts.ts +16 -14
- package/src/document-editor/comments.ts +30 -10
- 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/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/projection.ts +2 -2
- package/src/document-editor/rich-text.ts +3 -2
- package/src/document-editor/selection.ts +13 -0
- package/src/document-editor/style.css +12 -1
- package/src/document-editor/table-identities.ts +2 -1
- package/src/document-production.ts +339 -0
- package/src/document-session.ts +193 -53
- package/src/history.ts +5 -5
- package/src/index.ts +6 -5
- package/src/jobs.ts +1 -1
- package/src/messages.ts +19 -4
- package/src/panels/DocumentView.tsx +66 -26
- package/src/panels/DocumentVoiceDialog.tsx +2 -2
- package/src/panels/DocumentVoiceField.tsx +1 -1
- package/src/panels/ExportPanel.tsx +1 -6
- package/src/panels/HistoryPanel.tsx +1 -1
- package/src/panels/PageDeleteDialog.tsx +4 -4
- package/src/panels/PageRenameDialog.tsx +1 -1
- package/src/panels/PreviewPanel.tsx +1 -4
- package/src/panels/ProductionCaptionSize.tsx +20 -0
- package/src/panels/ProductionCaptionTiming.tsx +7 -7
- package/src/panels/ProductionMediaClips.tsx +6 -5
- package/src/panels/ProductionPanel.tsx +8 -4
- package/src/panels/ProductionRecording.tsx +86 -9
- package/src/panels/ProductionRecordingRange.tsx +19 -19
- package/src/panels/ProductionTimeline.tsx +6 -1
- package/src/panels/ProductionTranscription.tsx +5 -4
- package/src/panels/SourceDocumentPanel.tsx +12 -15
- package/src/panels/TranscriptionAdoption.tsx +9 -8
- package/src/panels/VoiceCloneDialog.tsx +2 -1
- package/src/panels/VoiceoverDialog.tsx +1 -1
- package/src/places.ts +1 -36
- package/src/preview-playback.ts +1 -1
- package/src/production-messages.ts +6 -0
- package/src/production-playback.ts +1 -1
- package/src/production-voice-playback.ts +3 -2
- package/src/sha256.ts +9 -0
- package/src/styles.css +16 -41
- package/src/use-recording-adoption.ts +2 -2
- package/src/voiceover-jobs.ts +1 -1
- package/src/workbench-state.ts +157 -0
- package/src/workbench.ts +246 -948
- package/src/workspace-client.ts +21 -11
- 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,8 +1,10 @@
|
|
|
1
|
-
import { hasRole, validateDocument } from '@lincyaw/kernel'
|
|
2
|
-
import type { DocumentAnswer, DocumentChangeAnswer, DocumentImageImportAnswer, DocumentIssue, Member, TeachingDocument } from '@lincyaw/kernel'
|
|
1
|
+
import { hasRole, isRecord, randomUUID, validateDocument } from '@lincyaw/kernel'
|
|
2
|
+
import type { CanonicalSource, ClientBodyDraftReference, DocumentAnswer, DocumentChangeAnswer, DocumentImageImportAnswer, DocumentIssue, Member, TeachingDocument } 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 {
|
|
@@ -28,12 +30,16 @@ export interface DocumentSessionState {
|
|
|
28
30
|
recoveryDrafts?: DocumentDraftChoice[]
|
|
29
31
|
recoveryChanged?: boolean
|
|
30
32
|
}
|
|
33
|
+
export interface DocumentDraftOperation { signal: AbortSignal, guard(): void, source: CanonicalSource, localDraft?: true }
|
|
34
|
+
export interface BodyDraftCandidate { draft: DocumentDraftReference & { current: boolean }, content: TeachingDocument, storage: 'durable' | 'rescue' | 'session', updatedAt: number, baseVersion: string, composing: boolean, pendingSubmission: boolean }
|
|
31
35
|
interface Slot {
|
|
36
|
+
observedSource?: CanonicalSource
|
|
32
37
|
state: DocumentSessionState
|
|
33
38
|
read: number
|
|
34
39
|
compositions: Set<string>
|
|
35
40
|
input: number
|
|
36
41
|
baseHash: string
|
|
42
|
+
baseContent: TeachingDocument
|
|
37
43
|
storage: string
|
|
38
44
|
drafts: DocumentDrafts
|
|
39
45
|
backup?: DocumentDraftCommit
|
|
@@ -54,6 +60,7 @@ interface Slot {
|
|
|
54
60
|
}
|
|
55
61
|
|
|
56
62
|
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)
|
|
63
|
+
const sameSource = (a: CanonicalSource | undefined, b: CanonicalSource) => a?.documentId === b.documentId && a.version === b.version && a.fingerprint === b.fingerprint
|
|
57
64
|
/** Non-body authoring changes keep native selection, comment drafts and body history. */
|
|
58
65
|
function keepsEditorContent(before: TeachingDocument, after: TeachingDocument): boolean {
|
|
59
66
|
const editing = ({ assets: _assets, production: _production, comments: _comments, definitions: _definitions, ...document }: TeachingDocument) => {
|
|
@@ -88,10 +95,21 @@ function keepsEditorContent(before: TeachingDocument, after: TeachingDocument):
|
|
|
88
95
|
}
|
|
89
96
|
|
|
90
97
|
async function digest(value: TeachingDocument): Promise<string> {
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
98
|
+
return sha256(serialize(value))
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** Replay pending author input on a newer saved document; an overlapping value belongs to the author. */
|
|
102
|
+
function rebaseDocument(base: TeachingDocument, local: TeachingDocument, remote: TeachingDocument): TeachingDocument {
|
|
103
|
+
const merge = (before: unknown, author: unknown, saved: unknown): unknown => {
|
|
104
|
+
if (serialize(author) === serialize(before)) return structuredClone(saved)
|
|
105
|
+
if (serialize(saved) === serialize(before) || serialize(author) === serialize(saved)) return structuredClone(author)
|
|
106
|
+
if (isRecord(before) && isRecord(author) && isRecord(saved)) return Object.fromEntries([...new Set([...Object.keys(before), ...Object.keys(author), ...Object.keys(saved)])]
|
|
107
|
+
.map(key => [key, merge(before[key], author[key], saved[key])] as const).filter(([, value]) => value !== undefined))
|
|
108
|
+
return structuredClone(author)
|
|
109
|
+
}
|
|
110
|
+
const merged = validateDocument(merge(base, local, remote))
|
|
111
|
+
// A structural conflict is one document operation: preserve the author's valid complete document.
|
|
112
|
+
return merged.document && !merged.issues.some(issue => issue.severity === 'error') ? merged.document : structuredClone(local)
|
|
95
113
|
}
|
|
96
114
|
|
|
97
115
|
/** One source actor per document. Runtime compilation and iframe lifecycle never own its input. */
|
|
@@ -125,7 +143,7 @@ export class DocumentSession {
|
|
|
125
143
|
this.#leaveTarget = target
|
|
126
144
|
}
|
|
127
145
|
|
|
128
|
-
async open(directory: string, expectedIdentity?: string): Promise<'identity-changed' | void> {
|
|
146
|
+
async open(directory: string, expectedIdentity?: string, signal?: AbortSignal): Promise<'identity-changed' | void> {
|
|
129
147
|
if (this.#active !== directory) this.interrupt()
|
|
130
148
|
const opening = ++this.#opening
|
|
131
149
|
this.#active = directory
|
|
@@ -134,7 +152,7 @@ export class DocumentSession {
|
|
|
134
152
|
if (cached) { cached.read++; cached.state = { ...cached.state, loading: true } }
|
|
135
153
|
this.#changed(cached ? { ...cached.state } : { directory, loading: true, dirty: false, saving: false, restored: false, composing: false, storageFailure: false, reset: 0, issues: [] })
|
|
136
154
|
try {
|
|
137
|
-
const [info, answer] = await Promise.all([this.#client.readWorkbenchInfo(), this.#client.readDocument(directory)])
|
|
155
|
+
const [info, answer] = await waitForDraft(Promise.all([this.#client.readWorkbenchInfo(), this.#client.readDocument(directory, signal)]), signal)
|
|
138
156
|
if (this.#active !== directory || opening !== this.#opening) return
|
|
139
157
|
if (expectedIdentity !== undefined && JSON.stringify([info.root, answer.content.id]) !== expectedIdentity) return 'identity-changed'
|
|
140
158
|
let slot = this.#slots.get(directory)
|
|
@@ -142,7 +160,7 @@ export class DocumentSession {
|
|
|
142
160
|
const baseHash = await digest(answer.content)
|
|
143
161
|
if (this.#disposed || this.#active !== directory || opening !== this.#opening) return
|
|
144
162
|
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 } }
|
|
163
|
+
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, version: answer.version, fingerprint: answer.fingerprint, loading: true, dirty: false, saving: false, restored: false, composing: false, storageFailure: false, reset: 0, issues: answer.issues } }
|
|
146
164
|
this.#slots.set(directory, slot)
|
|
147
165
|
await this.#restore(slot, answer)
|
|
148
166
|
} else {
|
|
@@ -193,23 +211,77 @@ export class DocumentSession {
|
|
|
193
211
|
for (const changed of slot.saveWaiters ?? []) changed()
|
|
194
212
|
if (this.#active === slot.state.directory && this.#slots.get(slot.state.directory) === slot) this.#changed({ ...slot.state })
|
|
195
213
|
}
|
|
214
|
+
bodyDraftRevision(): number { const slot = this.#active === undefined ? undefined : this.#slots.get(this.#active); return slot?.drafts.revision ?? 0 }
|
|
215
|
+
hasPendingAssetWrite(): boolean { return Boolean(this.#active !== undefined && this.#slots.get(this.#active)?.assetFlight) }
|
|
216
|
+
bodyDraftCapabilities(): { read: boolean, mutate: boolean, current: boolean } {
|
|
217
|
+
const slot = this.#active === undefined ? undefined : this.#slots.get(this.#active)
|
|
218
|
+
const read = Boolean(slot?.state.identity && slot.state.content && slot.state.version && !slot.state.loading && !this.#disposed)
|
|
219
|
+
return { read, mutate: Boolean(read && slot && !this.#readOnly(slot) && !slot.state.composing && !slot.flight && !slot.assetFlight && !slot.state.recovering),
|
|
220
|
+
current: Boolean(slot && (slot.state.dirty || slot.state.restored || slot.state.composing || slot.state.error)) }
|
|
221
|
+
}
|
|
222
|
+
async readBodyDrafts(signal: AbortSignal, selected?: ClientBodyDraftReference): Promise<{ candidates: BodyDraftCandidate[], coverage: 'stored-and-session' | 'partial', guard(): void }> {
|
|
223
|
+
const slot = this.#active === undefined ? undefined : this.#slots.get(this.#active)
|
|
224
|
+
if (!slot || !this.bodyDraftCapabilities().read || !slot.state.identity || !slot.state.content || !slot.state.version) throw new Error('client-body-drafts-unavailable')
|
|
225
|
+
const identity = slot.state.identity, documentId = slot.state.content.id, input = slot.input, read = slot.read, content = slot.state.content, version = slot.state.version,
|
|
226
|
+
revision = slot.drafts.revision, composing = slot.state.composing, source = JSON.stringify(slot.observedSource), own = slot.draft, flight = slot.flight, assetFlight = slot.assetFlight
|
|
227
|
+
const guard = () => {
|
|
228
|
+
signal.throwIfAborted()
|
|
229
|
+
if (this.#disposed || this.#active !== slot.state.directory || this.#slots.get(slot.state.directory) !== slot || slot.state.identity !== identity
|
|
230
|
+
|| slot.input !== input || slot.read !== read || slot.state.content !== content || slot.state.version !== version || slot.state.composing !== composing
|
|
231
|
+
|| 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')
|
|
232
|
+
}
|
|
233
|
+
guard()
|
|
234
|
+
const stored = await slot.drafts.read(identity, documentId, signal)
|
|
235
|
+
guard()
|
|
236
|
+
let candidates: BodyDraftCandidate[] = stored.records.filter(record => !(record.owned && record.id === slot.draft?.id)).map(record => ({
|
|
237
|
+
draft: { id: record.id, revision: record.revision, token: record.token, current: false }, content: record.content, storage: record.source,
|
|
238
|
+
updatedAt: record.updatedAt, baseVersion: record.baseVersion, composing: Boolean(record.composing), pendingSubmission: false,
|
|
239
|
+
}))
|
|
240
|
+
if (this.bodyDraftCapabilities().current) {
|
|
241
|
+
const token = await sha256(JSON.stringify({ domain: 'body-current', identity, id: slot.draft?.id ?? 'current', content, version, input, composing, pendingHash: slot.pendingHash }))
|
|
242
|
+
guard()
|
|
243
|
+
candidates.push({ draft: { id: slot.draft?.id ?? 'current', revision: input + 1, token, current: true }, content: structuredClone(content),
|
|
244
|
+
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,
|
|
245
|
+
baseVersion: version, composing, pendingSubmission: Boolean(flight || assetFlight) })
|
|
246
|
+
}
|
|
247
|
+
candidates.sort((a, b) => a.draft.token.localeCompare(b.draft.token))
|
|
248
|
+
if (selected) {
|
|
249
|
+
candidates = candidates.filter(value => value.draft.token === selected)
|
|
250
|
+
if (candidates.length !== 1) throw new Error('client-body-drafts-changed')
|
|
251
|
+
}
|
|
252
|
+
guard()
|
|
253
|
+
return { candidates, coverage: stored.failed ? 'partial' : 'stored-and-session', guard }
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
#source(answer: DocumentAnswer): CanonicalSource { return { documentId: answer.content.id, version: answer.version, fingerprint: answer.fingerprint } }
|
|
257
|
+
canonicalSource(): CanonicalSource | undefined { const slot = this.#active === undefined ? undefined : this.#slots.get(this.#active); return slot?.observedSource && { ...slot.observedSource } }
|
|
258
|
+
#checkOperation(operation?: DocumentDraftOperation, answer?: DocumentAnswer): void {
|
|
259
|
+
operation?.signal.throwIfAborted(); operation?.guard()
|
|
260
|
+
if (operation && !operation.localDraft && answer && !sameSource(this.#source(answer), operation.source)) throw new Error('client-source-changed')
|
|
261
|
+
}
|
|
196
262
|
#error(error: unknown): string { return error instanceof WorkspaceServerError ? error.answer.error ?? 'document-request-failed' : 'document-disconnected' }
|
|
197
263
|
#readOnly(slot: Slot): boolean { return this.#client.capabilities?.readOnly === true || Boolean(slot.state.member && !hasRole(slot.state.member, 'editor')) }
|
|
198
264
|
#issues(error: unknown): DocumentIssue[] { const value = error instanceof WorkspaceServerError ? (error.answer as { issues?: DocumentIssue[] }).issues : undefined; return Array.isArray(value) ? value : [] }
|
|
199
265
|
|
|
200
|
-
async refresh(directory = this.#active): Promise<void> {
|
|
266
|
+
async refresh(directory = this.#active, operation?: DocumentDraftOperation): Promise<void> {
|
|
201
267
|
if (directory === undefined) return
|
|
202
268
|
const slot = this.#slots.get(directory)
|
|
203
269
|
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 = () =>
|
|
270
|
+
if (slot.flight || slot.assetFlight) { slot.refreshed = true; if (operation) throw new Error('client-body-drafts-changed'); return }
|
|
271
|
+
const read = ++slot.read, current = () => {
|
|
272
|
+
const matches = slot.read === read && this.#slots.get(directory) === slot
|
|
273
|
+
if (operation && !matches) throw new Error('client-body-drafts-changed')
|
|
274
|
+
return matches
|
|
275
|
+
}
|
|
206
276
|
try {
|
|
207
|
-
const answer = await this.#client.readDocument(directory)
|
|
277
|
+
const answer = await this.#client.readDocument(directory, operation?.signal)
|
|
208
278
|
if (!current()) return
|
|
209
|
-
if (slot.flight || slot.assetFlight) { slot.refreshed = true; return }
|
|
210
|
-
|
|
279
|
+
if (slot.flight || slot.assetFlight) { slot.refreshed = true; if (operation) throw new Error('client-body-drafts-changed'); return }
|
|
280
|
+
this.#checkOperation(operation, answer)
|
|
281
|
+
await this.#adopt(slot, answer, () => { this.#checkOperation(operation); return current() })
|
|
211
282
|
if (current()) { this.#persist(slot); this.#emit(slot); void this.#readDrafts(slot) }
|
|
212
283
|
} catch (error) {
|
|
284
|
+
if (operation) throw error
|
|
213
285
|
if (current()) { slot.state = { ...slot.state, error: this.#error(error), issues: this.#issues(error) }; this.#emit(slot) }
|
|
214
286
|
}
|
|
215
287
|
}
|
|
@@ -217,6 +289,7 @@ export class DocumentSession {
|
|
|
217
289
|
async #adopt(slot: Slot, answer: DocumentAnswer, current = () => true): Promise<void> {
|
|
218
290
|
if (!current()) return
|
|
219
291
|
if (answer.content.id !== slot.state.content?.id) { slot.state = { ...slot.state, error: 'document-identity-changed' }; return }
|
|
292
|
+
slot.observedSource = this.#source(answer)
|
|
220
293
|
if (answer.version === slot.state.version) {
|
|
221
294
|
if (answer.fingerprint !== slot.state.fingerprint) slot.state = { ...slot.state, fingerprint: answer.fingerprint, issues: answer.issues }
|
|
222
295
|
return
|
|
@@ -235,11 +308,20 @@ export class DocumentSession {
|
|
|
235
308
|
slot.baseHash = remoteHash; slot.pendingHash = undefined
|
|
236
309
|
slot.state = { ...slot.state, version: answer.version, fingerprint: answer.fingerprint, error: undefined, issues: answer.issues }
|
|
237
310
|
} else if (remoteHash === slot.baseHash) slot.state = { ...slot.state, version: answer.version, fingerprint: answer.fingerprint, issues: answer.issues }
|
|
238
|
-
else
|
|
311
|
+
else if (!slot.state.composing) {
|
|
312
|
+
const content = rebaseDocument(slot.baseContent, slot.state.content!, answer.content)
|
|
313
|
+
const reset = slot.state.reset + (keepsEditorContent(slot.state.content!, content) ? 0 : 1)
|
|
314
|
+
slot.baseHash = remoteHash
|
|
315
|
+
slot.pendingHash = undefined
|
|
316
|
+
slot.state = { ...slot.state, content, reset, version: answer.version, fingerprint: answer.fingerprint, error: undefined, dirty: true, issues: answer.issues }
|
|
317
|
+
}
|
|
318
|
+
if (slot.state.version === answer.version) slot.baseContent = structuredClone(answer.content)
|
|
239
319
|
return
|
|
240
320
|
}
|
|
241
321
|
const reset = slot.state.reset + (keepsEditorContent(slot.state.content!, answer.content) ? 0 : 1)
|
|
242
322
|
slot.baseHash = remoteHash
|
|
323
|
+
slot.baseContent = structuredClone(answer.content)
|
|
324
|
+
slot.observedSource = this.#source(answer)
|
|
243
325
|
slot.state = { ...slot.state, version: answer.version, fingerprint: answer.fingerprint, content: answer.content, reset, error: undefined, issues: answer.issues }
|
|
244
326
|
}
|
|
245
327
|
|
|
@@ -283,33 +365,51 @@ export class DocumentSession {
|
|
|
283
365
|
this.#persist(slot); this.#emit(slot)
|
|
284
366
|
}
|
|
285
367
|
|
|
286
|
-
async #save(slot: Slot, immediate = false): Promise<void> {
|
|
368
|
+
async #save(slot: Slot, immediate = false, operation?: DocumentDraftOperation): Promise<void> {
|
|
287
369
|
if (immediate) { clearTimeout(slot.timer); slot.timer = undefined }
|
|
288
370
|
else if (slot.timer !== undefined) return
|
|
289
371
|
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
|
|
372
|
+
const check = () => {
|
|
373
|
+
this.#checkOperation(operation)
|
|
374
|
+
if (operation && (slot.state.version !== operation.source.version || !sameSource(slot.observedSource, operation.source))) throw new Error('client-source-changed')
|
|
375
|
+
}
|
|
376
|
+
check()
|
|
290
377
|
slot.read++
|
|
291
|
-
const flight = { id:
|
|
378
|
+
const flight = { id: randomUUID(), input: slot.input, content: structuredClone(slot.state.content), baseVersion: slot.state.version, hash: undefined as string | undefined }
|
|
292
379
|
slot.flight = flight
|
|
380
|
+
let completed = false
|
|
293
381
|
slot.state = { ...slot.state, saving: true }; this.#emit(slot)
|
|
294
382
|
try {
|
|
295
383
|
flight.hash = await digest(flight.content)
|
|
384
|
+
check()
|
|
385
|
+
if (operation && (slot.input !== flight.input || slot.state.composing)) throw new Error('client-body-drafts-changed')
|
|
296
386
|
slot.pendingHash = flight.hash
|
|
297
387
|
this.#persist(slot)
|
|
298
|
-
const answer = await this.#client.changeDocument(slot.state.directory, { id: flight.id, baseDocumentVersion: flight.baseVersion, replacement: flight.content })
|
|
388
|
+
const answer = await this.#client.changeDocument(slot.state.directory, { id: flight.id, baseDocumentVersion: flight.baseVersion, replacement: flight.content }, operation?.signal)
|
|
299
389
|
if (answer.changeId !== flight.id || answer.document !== slot.state.directory || answer.content.id !== flight.content.id) throw new Error('document-confirmation')
|
|
300
390
|
this.#ack(slot, flight, answer)
|
|
391
|
+
completed = true
|
|
301
392
|
} catch (error) {
|
|
302
|
-
|
|
393
|
+
if (!operation && this.#error(error) === 'document-version-changed' && !slot.state.composing) {
|
|
394
|
+
try {
|
|
395
|
+
await this.#adopt(slot, await this.#client.readDocument(slot.state.directory))
|
|
396
|
+
slot.state = { ...slot.state, saving: false }
|
|
397
|
+
} catch (refreshError) { slot.state = { ...slot.state, saving: false, error: this.#error(refreshError), issues: this.#issues(refreshError) } }
|
|
398
|
+
} else slot.state = { ...slot.state, saving: false, error: this.#error(error), issues: this.#issues(error) }
|
|
399
|
+
if (operation) { slot.state = { ...slot.state, restored: true }; throw error }
|
|
303
400
|
} finally {
|
|
304
401
|
if (slot.flight === flight) slot.flight = undefined
|
|
305
402
|
this.#persist(slot); this.#emit(slot)
|
|
306
403
|
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)
|
|
404
|
+
if (slot.refreshed && (!operation || completed)) { slot.refreshed = false; await this.refresh(slot.state.directory) }
|
|
405
|
+
if (!operation && !slot.state.error) void this.#save(slot)
|
|
406
|
+
else if (operation && completed && slot.input !== flight.input && !slot.state.error) this.#schedule(slot)
|
|
309
407
|
}
|
|
310
408
|
}
|
|
311
409
|
#ack(slot: Slot, flight: NonNullable<Slot['flight']>, answer: DocumentChangeAnswer): void {
|
|
410
|
+
slot.observedSource = this.#source(answer)
|
|
312
411
|
slot.baseHash = flight.hash!
|
|
412
|
+
slot.baseContent = structuredClone(answer.content)
|
|
313
413
|
slot.pendingHash = undefined
|
|
314
414
|
const same = slot.input === flight.input
|
|
315
415
|
slot.state = { ...slot.state, version: answer.version, fingerprint: answer.fingerprint, ...(same ? { content: answer.content } : {}), dirty: !same, saving: false, error: undefined, issues: answer.issues }
|
|
@@ -328,6 +428,8 @@ export class DocumentSession {
|
|
|
328
428
|
const same = slot.input === input, content = same ? answer.content : structuredClone(current)
|
|
329
429
|
if (!same) for (const [id, asset] of added) content.assets[id] = structuredClone(asset)
|
|
330
430
|
slot.baseHash = baseHash
|
|
431
|
+
slot.baseContent = structuredClone(answer.content)
|
|
432
|
+
slot.observedSource = this.#source(answer)
|
|
331
433
|
slot.state = { ...slot.state, content, version: answer.version, fingerprint: answer.fingerprint, dirty: !same, error: undefined, issues: answer.issues }
|
|
332
434
|
return true
|
|
333
435
|
}
|
|
@@ -342,7 +444,9 @@ export class DocumentSession {
|
|
|
342
444
|
const checked = validateDocument(content)
|
|
343
445
|
if (!checked.document || checked.issues.some(issue => issue.severity === 'error')) return false
|
|
344
446
|
slot.baseHash = baseHash
|
|
447
|
+
slot.baseContent = structuredClone(answer.content)
|
|
345
448
|
slot.pendingHash = undefined
|
|
449
|
+
slot.observedSource = this.#source(answer)
|
|
346
450
|
slot.state = { ...slot.state, content: structuredClone(checked.document), version: answer.version, fingerprint: answer.fingerprint,
|
|
347
451
|
dirty: slot.input !== input || slot.state.composing || serialize(content) !== serialize(answer.content), error: undefined, issues: answer.issues }
|
|
348
452
|
return true
|
|
@@ -406,7 +510,7 @@ export class DocumentSession {
|
|
|
406
510
|
const before = structuredClone(slot.state.content), input = slot.input, version = slot.state.version
|
|
407
511
|
let request = this.#imageImports.get(file)
|
|
408
512
|
if (!request || request.identity !== identity || request.version !== version) {
|
|
409
|
-
request = { identity, version, id:
|
|
513
|
+
request = { identity, version, id: randomUUID() }
|
|
410
514
|
this.#imageImports.set(file, request)
|
|
411
515
|
}
|
|
412
516
|
const read = ++slot.read
|
|
@@ -443,39 +547,58 @@ export class DocumentSession {
|
|
|
443
547
|
}
|
|
444
548
|
}
|
|
445
549
|
|
|
446
|
-
async retry(): Promise<void> {
|
|
550
|
+
async retry(operation?: DocumentDraftOperation): Promise<void> {
|
|
447
551
|
if (this.#disposed || this.#active === undefined) return
|
|
448
552
|
const slot = this.#slots.get(this.#active)
|
|
449
553
|
if (!slot) { await this.open(this.#active); return }
|
|
450
554
|
if (slot.flight || slot.assetFlight || slot.state.composing) return
|
|
451
|
-
|
|
452
|
-
slot.state =
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
555
|
+
this.#checkOperation(operation)
|
|
556
|
+
const previousError = slot.state.error, restored = slot.state.restored, input = slot.input
|
|
557
|
+
if (!operation) slot.state = { ...slot.state, restored: false, error: undefined }
|
|
558
|
+
try {
|
|
559
|
+
await this.refresh(slot.state.directory, operation)
|
|
560
|
+
this.#checkOperation(operation)
|
|
561
|
+
if (operation && slot.input !== input) throw new Error('client-body-drafts-changed')
|
|
562
|
+
if (operation) slot.state = { ...slot.state, restored: false, error: undefined }
|
|
563
|
+
if (slot.state.error) { if (!previousError) this.#emit(slot); return }
|
|
564
|
+
await this.#save(slot, true, operation)
|
|
565
|
+
} catch (error) {
|
|
566
|
+
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) }
|
|
567
|
+
throw error
|
|
568
|
+
}
|
|
456
569
|
}
|
|
457
|
-
async discard(): Promise<void> {
|
|
570
|
+
async discard(operation?: DocumentDraftOperation): Promise<boolean | void> {
|
|
458
571
|
const slot = this.#active === undefined ? undefined : this.#slots.get(this.#active)
|
|
459
572
|
if (!slot || slot.flight || slot.assetFlight || slot.state.composing) return
|
|
573
|
+
const pendingSave = slot.timer !== undefined
|
|
574
|
+
clearTimeout(slot.timer); slot.timer = undefined
|
|
575
|
+
let discarded = false
|
|
460
576
|
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
|
|
577
|
+
const current = () => { this.#checkOperation(operation); return !this.#disposed && this.#slots.get(directory) === slot && slot.read === read && slot.input === input
|
|
578
|
+
&& slot.state.identity === identity && !slot.state.composing && !slot.flight && !slot.assetFlight }
|
|
463
579
|
try {
|
|
464
|
-
const answer = await this.#client.readDocument(directory)
|
|
580
|
+
const answer = await this.#client.readDocument(directory, operation?.signal)
|
|
465
581
|
if (!current()) return
|
|
466
582
|
if (answer.document !== directory || answer.content.id !== documentId) {
|
|
467
583
|
slot.state = { ...slot.state, error: 'document-identity-changed' }; this.#emit(slot)
|
|
468
584
|
return
|
|
469
585
|
}
|
|
586
|
+
this.#checkOperation(operation, answer)
|
|
470
587
|
const baseHash = await digest(answer.content)
|
|
588
|
+
this.#checkOperation(operation)
|
|
471
589
|
if (!current()) return
|
|
590
|
+
slot.observedSource = this.#source(answer)
|
|
591
|
+
discarded = true
|
|
472
592
|
slot.input++; slot.baseHash = baseHash
|
|
473
593
|
slot.pendingHash = undefined
|
|
474
594
|
slot.compositions.clear()
|
|
475
595
|
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
596
|
this.#persist(slot); this.#emit(slot)
|
|
597
|
+
if (operation && slot.backup) await waitForDraft(slot.backup.done, operation.signal)
|
|
477
598
|
void this.#readDrafts(slot)
|
|
478
|
-
|
|
599
|
+
return true
|
|
600
|
+
} catch (error) { if (operation) throw error; if (current()) { slot.state = { ...slot.state, error: this.#error(error) }; this.#emit(slot) } }
|
|
601
|
+
finally { if (!discarded && pendingSave && this.#slots.get(directory) === slot && slot.input === input && slot.timer === undefined) this.#schedule(slot) }
|
|
479
602
|
}
|
|
480
603
|
async flush(signal?: AbortSignal): Promise<void> {
|
|
481
604
|
signal?.throwIfAborted()
|
|
@@ -584,7 +707,7 @@ export class DocumentSession {
|
|
|
584
707
|
}
|
|
585
708
|
|
|
586
709
|
/** Selecting a candidate never writes the source or replaces this tab's only snapshot. */
|
|
587
|
-
async recoverDraft(value: DocumentDraftReference): Promise<void> {
|
|
710
|
+
async recoverDraft(value: DocumentDraftReference, request?: DocumentDraftOperation): Promise<'restored' | 'already-saved' | void> {
|
|
588
711
|
const slot = this.#active === undefined ? undefined : this.#slots.get(this.#active)
|
|
589
712
|
if (slot && this.#readOnly(slot)) return
|
|
590
713
|
if (!slot?.state.identity || !slot.state.content || slot.state.loading || slot.flight || slot.assetFlight || slot.state.composing || this.#disposed) return
|
|
@@ -594,47 +717,61 @@ export class DocumentSession {
|
|
|
594
717
|
clearTimeout(slot.timer); slot.timer = undefined
|
|
595
718
|
let adopted = false
|
|
596
719
|
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)
|
|
720
|
+
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
|
|
721
|
+
&& !slot.state.composing && !slot.flight && !slot.assetFlight && (!protectedRecord || slot.draft?.id === protectedRecord.id && slot.draft?.revision === protectedRecord.revision) }
|
|
599
722
|
slot.state = { ...slot.state, recovering: true, recoveryChanged: false }; this.#emit(slot)
|
|
600
723
|
try {
|
|
724
|
+
this.#checkOperation(request)
|
|
601
725
|
if (!this.#persist(slot)) { this.#emit(slot); return }
|
|
602
726
|
const backup = slot.backup
|
|
603
|
-
if (backup && await backup.done !== 'committed') return
|
|
727
|
+
if (backup && await waitForDraft(backup.done, request?.signal) !== 'committed') return
|
|
604
728
|
if (!current()) return
|
|
605
729
|
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)
|
|
730
|
+
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
731
|
if (!current()) return
|
|
732
|
+
this.#checkOperation(request)
|
|
608
733
|
if (!candidate) { slot.state = { ...slot.state, recoveryChanged: true }; return }
|
|
609
|
-
const answer = await this.#client.readDocument(slot.state.directory)
|
|
734
|
+
const answer = await this.#client.readDocument(slot.state.directory, request?.signal)
|
|
610
735
|
if (!current()) return
|
|
611
736
|
if (answer.document !== slot.state.directory || answer.content.id !== documentId) { slot.state = { ...slot.state, error: 'document-identity-changed' }; return }
|
|
612
|
-
|
|
737
|
+
this.#checkOperation(request, answer)
|
|
738
|
+
slot.observedSource = this.#source(answer)
|
|
739
|
+
// Older LAN HTTP drafts stored canonical JSON in place of a SHA-256 fingerprint.
|
|
740
|
+
const [localHash, remoteHash, candidateBaseHash, pendingHash] = await Promise.all([
|
|
741
|
+
digest(candidate.content), digest(answer.content),
|
|
742
|
+
candidate.baseHash.startsWith('json:') ? sha256(candidate.baseHash.slice(5)) : candidate.baseHash,
|
|
743
|
+
candidate.pendingHash?.startsWith('json:') ? sha256(candidate.pendingHash.slice(5)) : candidate.pendingHash,
|
|
744
|
+
])
|
|
613
745
|
if (!current()) return
|
|
746
|
+
this.#checkOperation(request)
|
|
614
747
|
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
748
|
if (localHash === remoteHash && !candidate.composing) {
|
|
616
|
-
const settled = await slot.drafts.settle(identity, origins)
|
|
749
|
+
const settled = await slot.drafts.settle(identity, origins, request && { ...request, guard: () => { if (!current()) throw new Error('client-body-drafts-changed') } })
|
|
617
750
|
if (!current()) return
|
|
618
751
|
slot.persistFailure = !settled
|
|
619
|
-
return
|
|
752
|
+
if (!settled) return
|
|
753
|
+
return 'already-saved'
|
|
620
754
|
}
|
|
621
|
-
const conflict = answer.version !== candidate.baseVersion && remoteHash !==
|
|
622
|
-
const version = remoteHash ===
|
|
623
|
-
const baseHash = remoteHash ===
|
|
755
|
+
const conflict = answer.version !== candidate.baseVersion && remoteHash !== candidateBaseHash && remoteHash !== pendingHash
|
|
756
|
+
const version = remoteHash === pendingHash || remoteHash === candidateBaseHash ? answer.version : candidate.baseVersion
|
|
757
|
+
const baseHash = remoteHash === pendingHash ? remoteHash : candidateBaseHash
|
|
624
758
|
const record = await slot.drafts.fork(identity, { content: candidate.content, baseVersion: version, baseHash, input: input + 1,
|
|
625
|
-
pendingHash
|
|
759
|
+
pendingHash, composing: Boolean(candidate.composing), origins }, request && { ...request, guard: () => { if (!current()) throw new Error('client-body-drafts-changed') } })
|
|
626
760
|
if (!current()) { if (record) slot.drafts.archive(record); return }
|
|
627
761
|
if (!record) { slot.persistFailure = true; slot.state = { ...slot.state, storageFailure: true }; return }
|
|
628
762
|
clearTimeout(slot.timer); slot.timer = undefined
|
|
763
|
+
this.#checkOperation(request)
|
|
629
764
|
if (slot.draft) slot.drafts.archive(slot.draft)
|
|
630
|
-
slot.input = input + 1; slot.baseHash = baseHash; slot.pendingHash =
|
|
765
|
+
slot.input = input + 1; slot.baseHash = baseHash; slot.pendingHash = pendingHash; slot.draft = record; slot.draftOrigins = origins
|
|
631
766
|
slot.backup = undefined; slot.persistFailure = false
|
|
632
767
|
adopted = true
|
|
633
768
|
slot.compositions.clear()
|
|
634
769
|
slot.state = { ...slot.state, content: candidate.content, version, fingerprint: answer.fingerprint, dirty: true, restored: true, composing: false,
|
|
635
770
|
backupPending: false, storageFailure: slot.legacyFailure === 'write', reset: slot.state.reset + 1,
|
|
636
771
|
error: conflict ? 'document-version-changed' : candidate.composing ? 'document-composition-interrupted' : undefined, issues: answer.issues }
|
|
772
|
+
return 'restored'
|
|
637
773
|
} catch (error) {
|
|
774
|
+
if (request) throw error
|
|
638
775
|
if (current()) slot.state = { ...slot.state, error: this.#error(error), issues: this.#issues(error) }
|
|
639
776
|
} finally {
|
|
640
777
|
if (!this.#disposed && this.#slots.get(slot.state.directory) === slot && slot.state.identity === identity && slot.recoveryOperation === operation) {
|
|
@@ -644,25 +781,28 @@ export class DocumentSession {
|
|
|
644
781
|
}
|
|
645
782
|
}
|
|
646
783
|
|
|
647
|
-
async forgetDraft(value: DocumentDraftReference): Promise<void> {
|
|
784
|
+
async forgetDraft(value: DocumentDraftReference, request?: DocumentDraftOperation): Promise<boolean | void> {
|
|
648
785
|
const slot = this.#active === undefined ? undefined : this.#slots.get(this.#active)
|
|
649
786
|
if (!slot?.state.identity || !slot.state.content || slot.state.recovering || slot.flight || slot.assetFlight || slot.state.composing || this.#disposed) return
|
|
650
787
|
const identity = slot.state.identity, documentId = slot.state.content.id, input = slot.input, read = slot.read
|
|
651
788
|
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
|
|
789
|
+
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
790
|
slot.state = { ...slot.state, recovering: true }; this.#emit(slot)
|
|
654
791
|
try {
|
|
655
|
-
|
|
792
|
+
this.#checkOperation(request)
|
|
793
|
+
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)
|
|
656
794
|
if (!current()) return
|
|
795
|
+
this.#checkOperation(request)
|
|
657
796
|
if (candidate?.owned && candidate.id === slot.draft?.id) return
|
|
658
797
|
if (!candidate) slot.state = { ...slot.state, recoveryChanged: true }
|
|
659
798
|
else {
|
|
660
|
-
const settled = await slot.drafts.settle(identity, [value])
|
|
799
|
+
const settled = await slot.drafts.settle(identity, [value], request && { ...request, guard: () => { if (!current()) throw new Error('client-body-drafts-changed') } })
|
|
661
800
|
if (!current()) return
|
|
662
801
|
slot.persistFailure = !settled
|
|
663
802
|
slot.state = { ...slot.state, recoveryChanged: false, storageFailure: Boolean(slot.persistFailure || slot.legacyFailure === 'write') }
|
|
803
|
+
return settled
|
|
664
804
|
}
|
|
665
|
-
} catch { if (current()) { slot.persistFailure = true; slot.state = { ...slot.state, storageFailure: true } } }
|
|
805
|
+
} catch (error) { if (request) throw error; if (current()) { slot.persistFailure = true; slot.state = { ...slot.state, storageFailure: true } } }
|
|
666
806
|
finally {
|
|
667
807
|
if (!this.#disposed && this.#slots.get(slot.state.directory) === slot && slot.state.identity === identity && slot.recoveryOperation === operation) {
|
|
668
808
|
slot.state = { ...slot.state, recovering: false }; this.#emit(slot); void this.#readDrafts(slot)
|
package/src/history.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { errorMessage } from '@lincyaw/kernel'
|
|
1
|
+
import { errorMessage, randomUUID } from '@lincyaw/kernel'
|
|
2
2
|
import type { RevertResult, ReviewChange, ReviewSnapshot, ReviewUpdate } from '@lincyaw/kernel'
|
|
3
|
-
import type { WorkbenchStore } from './workbench.ts'
|
|
3
|
+
import type { WorkbenchStore } from './workbench-state.ts'
|
|
4
4
|
import { WorkspaceServerError } from './workspace-client.ts'
|
|
5
5
|
import type { WorkspaceClient } from './workspace-client.ts'
|
|
6
6
|
|
|
@@ -42,13 +42,13 @@ export class DocumentHistory {
|
|
|
42
42
|
this.#review = undefined
|
|
43
43
|
}
|
|
44
44
|
|
|
45
|
-
async read(dir: string): Promise<void> {
|
|
45
|
+
async read(dir: string, signal?: AbortSignal): Promise<void> {
|
|
46
46
|
this.#offsets = [0]
|
|
47
47
|
this.#page = 0
|
|
48
48
|
const read = ++this.#reads, scope = this.#scope
|
|
49
49
|
const current = () => this.#store.state.document === dir && read === this.#reads && scope === this.#scope
|
|
50
50
|
try {
|
|
51
|
-
const [history, { files }] = await Promise.all([this.#client.readRevisions(dir), this.#client.readChanges(dir)])
|
|
51
|
+
const [history, { files }] = await Promise.all([this.#client.readRevisions(dir, undefined, signal), this.#client.readChanges(dir, signal)])
|
|
52
52
|
if (current()) {
|
|
53
53
|
const historyFailure = this.#store.state.historyFailure
|
|
54
54
|
this.#store.set({ history, outside: files, ...this.#readFailure === historyFailure ? { historyFailure: undefined } : {} })
|
|
@@ -129,7 +129,7 @@ export class DocumentHistory {
|
|
|
129
129
|
#change(which: 'undo' | 'redo' | 'revertRevision', expect: string | undefined): Promise<boolean> {
|
|
130
130
|
const binding = this.#store.state.history?.historyBinding
|
|
131
131
|
if (!binding || !expect) return Promise.resolve(false)
|
|
132
|
-
const request = { id:
|
|
132
|
+
const request = { id: randomUUID(), ...binding, expect }
|
|
133
133
|
return this.#run(dir => this.#client[which](dir, request))
|
|
134
134
|
}
|
|
135
135
|
|
package/src/index.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
export { DocumentEditing } from './document-editing.ts'
|
|
2
|
+
export { DocumentProduction } from './document-production.ts'
|
|
1
3
|
export * from './components.tsx'
|
|
2
4
|
export { PmSchemaForm, schemaFormPropertyNames } from './schema-form/PmSchemaForm.tsx'
|
|
3
5
|
export type { PmSchemaFormProps } from './schema-form/PmSchemaForm.tsx'
|
|
@@ -14,8 +16,9 @@ export { findPlaceAtLine, findPlaceSource } from './places.ts'
|
|
|
14
16
|
export type { PlaceWithSource } from './places.ts'
|
|
15
17
|
export { connectPreview } from './preview.ts'
|
|
16
18
|
export type { PreviewConnection, PreviewHandlers } from './preview.ts'
|
|
17
|
-
export { documentDirFor, hasSlides,
|
|
18
|
-
export
|
|
19
|
+
export { documentDirFor, hasSlides, Workbench } from './workbench.ts'
|
|
20
|
+
export { isAgentChanging, selectedDocument } from './workbench-state.ts'
|
|
21
|
+
export type { AgentAnswer, AgentRequest, Pointer, PreviewSink, SaveState, WebView, WorkbenchMode, WorkbenchState } from './workbench-state.ts'
|
|
19
22
|
export { OperationLog } from './telemetry.ts'
|
|
20
23
|
export type { OperationBatch, OperationValue } from './telemetry.ts'
|
|
21
24
|
export { isObservedWorkspaceFailure, workspaceClient, WorkspaceServerError } from './workspace-client.ts'
|
|
@@ -26,11 +29,9 @@ export { HistoryPanel } from './panels/HistoryPanel.tsx'
|
|
|
26
29
|
export type { HistoryPanelProps, HistoryRevisionAction } from './panels/HistoryPanel.tsx'
|
|
27
30
|
export { HomePanel } from './panels/HomePanel.tsx'
|
|
28
31
|
export { WorkbenchApprovals } from './approvals.tsx'
|
|
29
|
-
export { assistantMascotAction, useAssistantMascot } from './assistant-mascot.ts'
|
|
30
|
-
export type { AssistantActivity } from './assistant-mascot.ts'
|
|
31
|
-
export { mascotEnabled, setMascotEnabled, useMascotEnabled } from './mascot-preference.ts'
|
|
32
32
|
export { PreviewPanel } from './panels/PreviewPanel.tsx'
|
|
33
33
|
export { SpeechSettingsPanel } from './panels/SpeechSettingsPanel.tsx'
|
|
34
34
|
export { SPEECH_SETTINGS_MESSAGES } from './speech-settings-messages.ts'
|
|
35
35
|
export { connectAuthoringClient } from './authoring-client.ts'
|
|
36
|
+
export { connectAuthoringWindow } from './authoring-window.ts'
|
|
36
37
|
export { SHORTCUTS } from './shortcuts.ts'
|
package/src/jobs.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { errorMessage } from '@lincyaw/kernel'
|
|
2
2
|
import type { CompileJob, JobSource } from '@lincyaw/kernel'
|
|
3
|
-
import type { WorkbenchStore } from './workbench.ts'
|
|
3
|
+
import type { WorkbenchStore } from './workbench-state.ts'
|
|
4
4
|
import type { WorkspaceClient } from './workspace-client.ts'
|
|
5
5
|
|
|
6
6
|
/** Of two copies of a job (an event and an answer cross on the way), the one of the later change. */
|
package/src/messages.ts
CHANGED
|
@@ -12,6 +12,11 @@ export interface AgentPlace {
|
|
|
12
12
|
|
|
13
13
|
// A Latin word or a number set after Chinese text is spaced from it (docs/glossary.md 2).
|
|
14
14
|
const spaced = (text: string) => (/^[A-Za-z0-9]/.test(text) ? ` ${text}` : text)
|
|
15
|
+
// The start of a passage, enough to recognize it.
|
|
16
|
+
const excerpt = (text: string, length: number) => [...text].length > length ? `${[...text].slice(0, length).join('').trimEnd()}…` : text
|
|
17
|
+
|
|
18
|
+
/** Where on a page the author asks for new content: after a passage (its text, '' for content without text), at the start (none) or at the end. */
|
|
19
|
+
interface AgentInsertion { after?: string, end: boolean }
|
|
15
20
|
|
|
16
21
|
// Names of commands follow Microsoft Word's (撤消/Undo, 新建批注/New Comment, 版本历史/Version History…).
|
|
17
22
|
const zh = {
|
|
@@ -325,11 +330,11 @@ const zh = {
|
|
|
325
330
|
'history-result-no-longer-current': '之前的结果已不是当前版本,请查看最新的历史。',
|
|
326
331
|
'authoring-review-required': '有修改等待你审阅,请先接受或放弃。',
|
|
327
332
|
} as Record<string, string>,
|
|
328
|
-
agentWorking: 'AI 正在修改文稿…',
|
|
329
333
|
agentWaiting: (n: number) => `AI 修改了 ${n} 处内容,要接受这些修改吗?`,
|
|
330
334
|
accept: '接受',
|
|
331
335
|
reject: '拒绝',
|
|
332
|
-
problems: (n: number) =>
|
|
336
|
+
problems: (n: number) => `文稿有 ${n} 个问题需要处理。`,
|
|
337
|
+
problemsTitle: '文稿中的问题',
|
|
333
338
|
fixWithAi: '用 AI 修复',
|
|
334
339
|
fixing: '正在发送给 AI…',
|
|
335
340
|
fixFailed: (reason: string) => `无法发送给 AI。${reason}`,
|
|
@@ -356,6 +361,11 @@ const zh = {
|
|
|
356
361
|
place.quote ? `\n原文:“${place.quote}”` : '',
|
|
357
362
|
].join('')).filter(Boolean),
|
|
358
363
|
].join('\n\n'),
|
|
364
|
+
animationRequest: (page: number, title: string, place: AgentInsertion) => {
|
|
365
|
+
const at = `第 ${page} 页${title ? `“${title}”` : ''}`
|
|
366
|
+
return place.end ? `在${at}的末尾加一个动画:` : place.after === undefined ? `在${at}的开头加一个动画:`
|
|
367
|
+
: place.after ? `在${at}中“${excerpt(place.after, 16)}”这一段之后加一个动画:` : `在${at}的这个位置加一个动画:`
|
|
368
|
+
},
|
|
359
369
|
commentsRequest: (comments: (AgentPlace & { text: string })[]) => [
|
|
360
370
|
comments.length === 1 ? '我在文稿上写了一条批注:' : `我在文稿上写了 ${comments.length} 条批注:`,
|
|
361
371
|
...comments.map((comment, i) => [
|
|
@@ -689,11 +699,11 @@ const en: WorkbenchMessages = {
|
|
|
689
699
|
'history-result-no-longer-current': 'The earlier result is no longer the current version. Review the latest history.',
|
|
690
700
|
'authoring-review-required': 'Changes are waiting for your review. Accept or discard them first.',
|
|
691
701
|
},
|
|
692
|
-
agentWorking: 'The AI is editing the document…',
|
|
693
702
|
agentWaiting: (n: number) => `The AI changed ${n} file${n === 1 ? '' : 's'}. Accept the changes?`,
|
|
694
703
|
accept: 'Accept',
|
|
695
704
|
reject: 'Reject',
|
|
696
|
-
problems: (n: number) =>
|
|
705
|
+
problems: (n: number) => `The document has ${n} problem${n === 1 ? '' : 's'} to fix.`,
|
|
706
|
+
problemsTitle: 'Problems in the Document',
|
|
697
707
|
fixWithAi: 'Fix with AI',
|
|
698
708
|
fixing: 'Sending to the AI…',
|
|
699
709
|
fixFailed: (reason: string) => `Couldn’t send to the AI. ${reason}`,
|
|
@@ -720,6 +730,11 @@ const en: WorkbenchMessages = {
|
|
|
720
730
|
place.quote ? `\nText: “${place.quote}”` : '',
|
|
721
731
|
].join('')).filter(Boolean),
|
|
722
732
|
].join('\n\n'),
|
|
733
|
+
animationRequest: (page: number, title: string, place: AgentInsertion) => {
|
|
734
|
+
const at = `page ${page}${title ? `, “${title}”` : ''}`
|
|
735
|
+
return place.end ? `Add an animation at the end of ${at}: ` : place.after === undefined ? `Add an animation at the start of ${at}: `
|
|
736
|
+
: place.after ? `Add an animation on ${at}, after the paragraph “${excerpt(place.after, 40)}”: ` : `Add an animation at this point on ${at}: `
|
|
737
|
+
},
|
|
723
738
|
commentsRequest: (comments: (AgentPlace & { text: string })[]) => [
|
|
724
739
|
comments.length === 1 ? 'I left a comment on the document:' : `I left ${comments.length} comments on the document:`,
|
|
725
740
|
...comments.map((comment, i) => [
|