@lincyaw/workspace 1.0.0-rc.61 → 1.0.0-rc.63
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 +6 -6
- package/src/node/document-voice.ts +52 -15
- package/src/node/serve.ts +2 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lincyaw/workspace",
|
|
3
|
-
"version": "1.0.0-rc.
|
|
3
|
+
"version": "1.0.0-rc.63",
|
|
4
4
|
"description": "Presmium's workspace service (presmium serve): documents, their history, reviews and the workspace site",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -34,13 +34,13 @@
|
|
|
34
34
|
"mime-types": "3.0.2",
|
|
35
35
|
"playwright-chromium": "1.64.1",
|
|
36
36
|
"yaml": "^2.9.1",
|
|
37
|
-
"@lincyaw/compiler": "1.0.0-rc.
|
|
38
|
-
"@lincyaw/kernel": "1.0.0-rc.
|
|
39
|
-
"@lincyaw/server": "1.0.0-rc.
|
|
40
|
-
"@lincyaw/design": "1.0.0-rc.
|
|
37
|
+
"@lincyaw/compiler": "1.0.0-rc.63",
|
|
38
|
+
"@lincyaw/kernel": "1.0.0-rc.63",
|
|
39
|
+
"@lincyaw/server": "1.0.0-rc.63",
|
|
40
|
+
"@lincyaw/design": "1.0.0-rc.63"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
43
|
"@types/mime-types": "^3.0.1",
|
|
44
|
-
"@lincyaw/mcp": "1.0.0-rc.
|
|
44
|
+
"@lincyaw/mcp": "1.0.0-rc.63"
|
|
45
45
|
}
|
|
46
46
|
}
|
|
@@ -1,24 +1,34 @@
|
|
|
1
1
|
import { DOCUMENT_ENTRY, VOICE_CLONE_MAX_AUDIO_BYTES, fnv1a64, hasRole, speechEngineId } from '@lincyaw/kernel'
|
|
2
|
-
import type { CanonicalSource, DocumentVoiceAuthoring, Finding, Member, PreparedVoiceClone, SpeechService, VoiceoverExecution, VoiceoverJob } from '@lincyaw/kernel'
|
|
2
|
+
import type { CanonicalSource, DocumentVoiceAuthoring, DocumentVoiceCloneAudio, Finding, Member, PreparedVoiceClone, SpeechService, VoiceoverExecution, VoiceoverJob } from '@lincyaw/kernel'
|
|
3
3
|
import { createVoiceoverOwner, createVoiceoverRoutes, validateDocumentVoiceCatalogQuery, validateDocumentVoiceClone, validateDocumentVoiceCloneQuery, validateVoicePreviewStart } from '@lincyaw/kernel/node'
|
|
4
4
|
import { extname } from 'node:path'
|
|
5
|
+
import { randomBytes } from 'node:crypto'
|
|
5
6
|
import { readDocumentAudioBytes } from './document-audio.ts'
|
|
6
7
|
import { readAuthoringDocument } from './document-editor.ts'
|
|
7
8
|
import { RequestError } from './errors.ts'
|
|
8
9
|
import type { SourceEditor } from './source-editor.ts'
|
|
10
|
+
import type { MediaUploadsOwner } from './media-uploads.ts'
|
|
9
11
|
|
|
10
12
|
export interface DocumentVoiceServices { speech?: SpeechService, voiceover?: { version: 1, preview: true }, voiceoverExecution?: VoiceoverExecution, findings?: Finding[] }
|
|
11
13
|
export interface DocumentVoiceAuthoringOptions {
|
|
12
14
|
editor: SourceEditor
|
|
13
15
|
services(document: string, source: CanonicalSource): Promise<DocumentVoiceServices>
|
|
14
16
|
revision?(document: string): string | undefined
|
|
17
|
+
uploads?: MediaUploadsOwner
|
|
15
18
|
}
|
|
16
19
|
const same = (a: CanonicalSource, b: CanonicalSource) => a.documentId === b.documentId && a.version === b.version && a.fingerprint === b.fingerprint
|
|
17
20
|
|
|
18
|
-
export function createDocumentVoiceAuthoring({ editor, services, revision }: DocumentVoiceAuthoringOptions) {
|
|
21
|
+
export function createDocumentVoiceAuthoring({ editor, services, revision, uploads }: DocumentVoiceAuthoringOptions) {
|
|
19
22
|
const pending = new Set<{ controller: AbortController, finished: Promise<unknown> }>()
|
|
20
23
|
const owners = new Map<string, ReturnType<typeof createVoiceoverOwner>>()
|
|
21
|
-
const clones = new
|
|
24
|
+
const clones = new Map<string, { document: string, member: string, request: string, prepared: PreparedVoiceClone }>()
|
|
25
|
+
const cloneEntry = (document: string, id: string, member: Member) => {
|
|
26
|
+
if (!hasRole(member, 'owner')) throw new RequestError(403, 'voice-clone-permission-denied')
|
|
27
|
+
authorize(document, member)
|
|
28
|
+
const entry = clones.get(id)
|
|
29
|
+
if (entry && (entry.document !== document || entry.member !== member.id)) throw new RequestError(403, 'voice-clone-permission-denied')
|
|
30
|
+
return entry
|
|
31
|
+
}
|
|
22
32
|
let closed = false
|
|
23
33
|
const authorize = (document: string, member: Member) => {
|
|
24
34
|
if (!hasRole(member, 'editor')) throw new RequestError(403, 'voice-authoring-permission-denied')
|
|
@@ -70,25 +80,33 @@ export function createDocumentVoiceAuthoring({ editor, services, revision }: Doc
|
|
|
70
80
|
if (clones.size >= 4) throw new RequestError(503, 'voice-authoring-busy')
|
|
71
81
|
const request = structuredClone(value), loaded = await load(document, member, signal, request.source)
|
|
72
82
|
const speech = loaded.speech, engine = request.engine ?? await speech?.defaultSynthesisEngine?.()
|
|
73
|
-
const
|
|
74
|
-
|
|
83
|
+
const read = async (input: DocumentVoiceCloneAudio) => {
|
|
84
|
+
const path = typeof input === 'string' ? input : undefined
|
|
85
|
+
if (path !== undefined && path.split(/[\\/]/).some(part => part.startsWith('.'))) throw new RequestError(403, 'audio-file-path')
|
|
86
|
+
const uploaded = typeof input !== 'string' ? uploads?.read(document, loaded.source.documentId, member, input.upload, 'audio') : undefined
|
|
87
|
+
if (path === undefined && !uploaded) throw new RequestError(503, 'media-upload-unavailable')
|
|
75
88
|
const types: Record<string, string> = { '.wav': 'audio/wav', '.mp3': 'audio/mpeg', '.webm': 'audio/webm', '.ogg': 'audio/ogg', '.m4a': 'audio/mp4', '.mp4': 'audio/mp4', '.flac': 'audio/flac', '.aac': 'audio/aac' }
|
|
76
|
-
const type = types[extname(path).toLowerCase()]
|
|
89
|
+
const type = types[extname(path ?? uploaded!.name).toLowerCase()]
|
|
77
90
|
if (!type) throw new RequestError(422, 'voice-clone-audio-type')
|
|
78
|
-
editor.assertEditable({ document, home: false }, path, member)
|
|
79
|
-
|
|
91
|
+
if (path !== undefined) editor.assertEditable({ document, home: false }, path, member)
|
|
92
|
+
const bytes = uploaded?.bytes ?? await readDocumentAudioBytes(editor, { document, member, rest: '' }, path!, signal, VOICE_CLONE_MAX_AUDIO_BYTES)
|
|
93
|
+
if (bytes.length > VOICE_CLONE_MAX_AUDIO_BYTES) throw new RequestError(413, 'production.asset-size')
|
|
94
|
+
return { bytes, type }
|
|
80
95
|
}
|
|
81
|
-
const
|
|
82
|
-
|
|
96
|
+
const [sampleRead, statementRead] = await Promise.allSettled([read(request.sample), read(request.consent.statement)])
|
|
97
|
+
if (sampleRead.status === 'rejected') throw sampleRead.reason
|
|
98
|
+
if (statementRead.status === 'rejected') throw statementRead.reason
|
|
99
|
+
const sampleInput = sampleRead.value, statementInput = statementRead.value
|
|
83
100
|
current(document, member, loaded.source); signal.throwIfAborted()
|
|
84
101
|
if (!speech?.cloneVoice || !engine || !(await speech.engines()).some(info => info.id === speechEngineId(engine) && info.clone)) throw new RequestError(409, 'voice-clone-unavailable')
|
|
85
102
|
current(document, member, loaded.source); signal.throwIfAborted()
|
|
86
103
|
// Several readers may finish concurrently; the retained byte budget is checked again before retaining blobs.
|
|
87
104
|
if (clones.size >= 4) throw new RequestError(503, 'voice-authoring-busy')
|
|
88
|
-
let sample: Blob | undefined = new Blob([
|
|
89
|
-
let statement: Blob | undefined = new Blob([
|
|
105
|
+
let sample: Blob | undefined = new Blob([sampleInput.bytes as Uint8Array<ArrayBuffer>], { type: sampleInput.type })
|
|
106
|
+
let statement: Blob | undefined = new Blob([statementInput.bytes as Uint8Array<ArrayBuffer>], { type: statementInput.type })
|
|
90
107
|
let started: ReturnType<NonNullable<SpeechService['cloneVoice']>> | undefined
|
|
91
108
|
let expired = false
|
|
109
|
+
const id = randomBytes(32).toString('hex')
|
|
92
110
|
const prepared: PreparedVoiceClone = { engine: speechEngineId(engine), voice: request.voice,
|
|
93
111
|
start: startSignal => {
|
|
94
112
|
if (started) return started
|
|
@@ -103,13 +121,32 @@ export function createDocumentVoiceAuthoring({ editor, services, revision }: Doc
|
|
|
103
121
|
}).finally(() => prepared.close())
|
|
104
122
|
return started
|
|
105
123
|
},
|
|
106
|
-
close: () => { expired = true; clearTimeout(timer); sample = undefined; statement = undefined; clones.delete(
|
|
124
|
+
close: () => { expired = true; clearTimeout(timer); sample = undefined; statement = undefined; clones.delete(id) },
|
|
107
125
|
}
|
|
108
126
|
const timer = setTimeout(() => prepared.close(), 90000)
|
|
109
127
|
timer.unref()
|
|
110
|
-
clones.
|
|
128
|
+
clones.set(id, { document, member: member.id, request: JSON.stringify(request), prepared })
|
|
111
129
|
return prepared
|
|
112
130
|
}),
|
|
131
|
+
prepareCloneReference: async (document, request, member, signal) => {
|
|
132
|
+
const prepared = await service.prepareClone!(document, request, member, signal)
|
|
133
|
+
const found = [...clones].find(([, entry]) => entry.prepared === prepared)
|
|
134
|
+
if (!found) { prepared.close(); throw new RequestError(409, 'voice-clone-expired') }
|
|
135
|
+
if (signal?.aborted) { prepared.close(); signal.throwIfAborted() }
|
|
136
|
+
return { id: found[0], engine: prepared.engine, voice: prepared.voice }
|
|
137
|
+
},
|
|
138
|
+
runPreparedClone: (document, id, request, member, signal) => {
|
|
139
|
+
const entry = cloneEntry(document, id, member)
|
|
140
|
+
if (!entry) throw new RequestError(409, 'voice-clone-expired')
|
|
141
|
+
if (entry.request !== JSON.stringify(request)) throw new RequestError(409, 'voice-clone-preparation-changed')
|
|
142
|
+
try { return entry.prepared.start(signal) }
|
|
143
|
+
catch (error) { entry.prepared.close(); throw error }
|
|
144
|
+
},
|
|
145
|
+
releasePreparedClone: (document, id, member) => {
|
|
146
|
+
const entry = cloneEntry(document, id, member)
|
|
147
|
+
entry?.prepared.close()
|
|
148
|
+
return Boolean(entry)
|
|
149
|
+
},
|
|
113
150
|
cloneStatus: (document, query, member, signal) => work(signal, async signal => {
|
|
114
151
|
if (!hasRole(member, 'owner')) throw new RequestError(403, 'voice-clone-permission-denied')
|
|
115
152
|
if (!validateDocumentVoiceCloneQuery(query)) throw new RequestError(400, 'voice-clone-invalid')
|
|
@@ -168,5 +205,5 @@ export function createDocumentVoiceAuthoring({ editor, services, revision }: Doc
|
|
|
168
205
|
},
|
|
169
206
|
readPreview: async (document, id, member) => ownerOf(document, member).readPreview(id),
|
|
170
207
|
}
|
|
171
|
-
return { service, routes: createVoiceoverRoutes(service), async close() { closed = true; for (const prepared of clones) prepared.close(); for (const operation of pending) operation.controller.abort(); await Promise.allSettled([...owners.values()].map(owner => owner.close())); await Promise.allSettled([...pending].map(operation => operation.finished)); owners.clear() } }
|
|
208
|
+
return { service, routes: createVoiceoverRoutes(service), async close() { closed = true; for (const { prepared } of clones.values()) prepared.close(); for (const operation of pending) operation.controller.abort(); await Promise.allSettled([...owners.values()].map(owner => owner.close())); await Promise.allSettled([...pending].map(operation => operation.finished)); owners.clear() } }
|
|
172
209
|
}
|
package/src/node/serve.ts
CHANGED
|
@@ -397,7 +397,8 @@ export async function serveWorkspace(options: ServeOptions): Promise<WorkspaceSe
|
|
|
397
397
|
})
|
|
398
398
|
const running = server
|
|
399
399
|
serverServices = running.services
|
|
400
|
-
|
|
400
|
+
mediaUploads = createMediaUploads({ editor, url: () => `${publicOrigin()}${basePath.replace(/\/$/, '')}` })
|
|
401
|
+
voiceAuthoring = createDocumentVoiceAuthoring({ editor, uploads: mediaUploads, services: authoringServices, revision: document => { const latest = compileSessions.get(document)?.latest; return latest ? irRevision(latest.doc) : undefined } })
|
|
401
402
|
workbench = createWorkbenchBackend({ root, compileSessionOf, serverHost, voices: voiceAuthoring.service, diagnostics: running.diagnostics, url: () => options.publicUrl ? new URL(options.publicUrl).origin : running.url, basePath, preview, log, warmSite: dir => site?.warm(dir) ?? Promise.resolve(), viewerPage: (dir, wait) => site?.app(dir, wait) ?? Promise.resolve(undefined) })
|
|
402
403
|
history = openHistory()
|
|
403
404
|
workspaceChanges = createWorkspaceChanges({ config, editor, locks, revisions: history?.revisions, changed: documentsChanged })
|
|
@@ -413,7 +414,6 @@ export async function serveWorkspace(options: ServeOptions): Promise<WorkspaceSe
|
|
|
413
414
|
documentHistory = createDocumentHistory({ config, editor, locks, revisions: history?.revisions, reviewRequired: config.settings['presmium.assistant']?.review === 'required', changed: (before, after) => documentsChanged(documentsDiff(before, after)), reverted: (target, revision) => history?.agentSessionReviews.reverted(target, revision) })
|
|
414
415
|
transcriptions = createDocumentTranscriptions({ root, editor, services: authoringServices })
|
|
415
416
|
transcriptionAdoption = createTranscriptionAdoption({ documentEditor, transcriptions })
|
|
416
|
-
mediaUploads = createMediaUploads({ editor, url: () => `${publicOrigin()}${basePath.replace(/\/$/, '')}` })
|
|
417
417
|
audioAuthoring = createDocumentAudioAuthoring({ root, editor, uploads: mediaUploads, revisions: history?.revisions, services: authoringServices, reviewRequired: config.settings['presmium.assistant']?.review === 'required' })
|
|
418
418
|
imageAuthoring = createDocumentImageAuthoring({ root, editor, uploads: mediaUploads, revisions: history?.revisions, reviewRequired: config.settings['presmium.assistant']?.review === 'required' })
|
|
419
419
|
const routeOptions = {
|