@lincyaw/cli 1.0.0-rc.11 → 1.0.0-rc.111
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 +2 -2
- package/bin/presmium.mjs +1 -1
- package/package.json +7 -6
- package/src/cli.ts +213 -7
- package/src/document-file.ts +59 -0
- package/src/import.ts +2 -0
- package/src/index.ts +17 -1
- package/src/markdown.ts +2 -0
- package/src/mcp-workspace.ts +77 -0
- package/src/messages.ts +357 -9
- package/src/script.ts +152 -0
- package/src/speech-key.ts +73 -0
- package/src/voiceover.ts +241 -0
package/src/script.ts
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import type { NarrationNode, TeachingDocument } from '@lincyaw/kernel'
|
|
2
|
+
|
|
3
|
+
// The narration of a document as one Markdown file to polish, and back; lecture.json stays the only source. Each step
|
|
4
|
+
// is a heading; under it each narration follows its marker line `<!-- n: <narration> -->` up to the next marker or
|
|
5
|
+
// heading. Each line break of a narration starts a new line of the file, and a blank line stands for a blank line
|
|
6
|
+
// (`\n\n`; several read back as one).
|
|
7
|
+
// A word shown one way and spoken another is `{shown|spoken}`, a cue `⟦cue:<id>⟧`; a backslash before `\`, `{`, `}`,
|
|
8
|
+
// `|`, `⟦` or `⟧` keeps that character as text, and a line that would read as a heading or a comment starts with one.
|
|
9
|
+
|
|
10
|
+
export class ScriptError extends Error {
|
|
11
|
+
readonly code: 'script-duplicate' | 'script-stray' | 'script-unknown' | 'script-missing' | 'script-empty' | 'script-cue-unknown' | 'script-cue-repeated' | 'script-cue-removed' | 'script-node'
|
|
12
|
+
readonly detail: string[]
|
|
13
|
+
readonly line?: number
|
|
14
|
+
constructor(code: ScriptError['code'], detail: string[], line?: number) {
|
|
15
|
+
super(code)
|
|
16
|
+
this.code = code
|
|
17
|
+
this.detail = detail
|
|
18
|
+
if (line !== undefined) this.line = line
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const MARKER = /^<!-- n: (\S+) -->$/
|
|
23
|
+
const HEADING = /^#{1,6}(?:\s|$)/
|
|
24
|
+
const COMMENT = /^<!--[\s\S]*-->$/
|
|
25
|
+
// An escaped character, a pronunciation (its parts may hold escaped characters) or a cue; anything else is text.
|
|
26
|
+
const TOKEN = /\\([\\{}|⟦⟧#<])|\{((?:\\[\s\S]|[^\\{}|])+)\|((?:\\[\s\S]|[^\\{}])+)\}|⟦cue:([^⟧]+)⟧/g
|
|
27
|
+
|
|
28
|
+
const escaped = (text: string, special: RegExp) => text.replace(special, '\\$&')
|
|
29
|
+
const unescaped = (text: string) => text.replace(/\\([\\{}|⟦⟧#<])/g, '$1')
|
|
30
|
+
|
|
31
|
+
/** A narration's content as the lines of the file: escaped, trimmed, a line that reads as a heading or comment marked. */
|
|
32
|
+
export function scriptLines(content: NarrationNode[]): string[] {
|
|
33
|
+
return content.map((node) => {
|
|
34
|
+
if (node.type === 'text') return escaped(node.text, /[\\{}⟦⟧]/g)
|
|
35
|
+
if (node.type === 'pronunciation') return `{${escaped(node.display, /[\\{}|⟦⟧]/g)}|${escaped(node.spoken, /[\\{}|⟦⟧]/g)}}`
|
|
36
|
+
if (node.type === 'cue') return `⟦cue:${node.id}⟧`
|
|
37
|
+
throw new ScriptError('script-node', [(node as { type: string }).type])
|
|
38
|
+
}).join('').split('\n').map(line => line.trim()).map(line => /^(?:#|<!--)/.test(line) ? `\\${line}` : line)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** A narration's lines read back: joined by line breaks, a run of blank lines one blank line, none at either end. */
|
|
42
|
+
function linesToText(lines: readonly string[]): string {
|
|
43
|
+
return lines.join('\n').replace(/\n{3,}/g, '\n\n').replace(/^\n+|\n+$/g, '')
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** The narrated steps in reading order, each with the narrations it is the first to say (a shared one is listed once). */
|
|
47
|
+
export function narratedSteps(document: TeachingDocument): { section: string, title: string, step: string, stepTitle: string, narrations: string[] }[] {
|
|
48
|
+
const seen = new Set<string>()
|
|
49
|
+
return document.sectionOrder.flatMap(sectionId => {
|
|
50
|
+
const section = document.sections[sectionId]!
|
|
51
|
+
return section.stepOrder.flatMap(stepId => {
|
|
52
|
+
const step = document.steps[stepId]!
|
|
53
|
+
const narrations = step.narrationIds.filter(id => document.narrations[id] && !seen.has(id))
|
|
54
|
+
for (const id of narrations) seen.add(id)
|
|
55
|
+
return narrations.length ? [{ section: sectionId, title: section.title || sectionId, step: stepId, stepTitle: step.title || stepId, narrations }] : []
|
|
56
|
+
})
|
|
57
|
+
})
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function exportScript(document: TeachingDocument, instructions: string): { text: string, steps: number, narrations: number } {
|
|
61
|
+
const steps = narratedSteps(document)
|
|
62
|
+
const parts = [`# ${document.metadata.title}`, '', `<!-- ${instructions} -->`, '']
|
|
63
|
+
let page: string | undefined
|
|
64
|
+
for (const step of steps) {
|
|
65
|
+
if (step.section !== page) { parts.push(`## ${step.title}`, ''); page = step.section }
|
|
66
|
+
parts.push(`### ${step.stepTitle}`, '')
|
|
67
|
+
for (const id of step.narrations) {
|
|
68
|
+
parts.push(`<!-- n: ${id} -->`, ...scriptLines(document.narrations[id]!.content), '')
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return { text: parts.join('\n'), steps: steps.length, narrations: steps.reduce((n, step) => n + step.narrations.length, 0) }
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** The narration below each marker; text outside a narration (under a heading, before the first marker) is refused. */
|
|
75
|
+
export function readScript(text: string): Map<string, { body: string, line: number }> {
|
|
76
|
+
const found = new Map<string, { body: string, line: number }>()
|
|
77
|
+
let current: { id: string, line: number, lines: string[] } | undefined
|
|
78
|
+
const close = () => { if (current) found.set(current.id, { body: linesToText(current.lines), line: current.line }); current = undefined }
|
|
79
|
+
const lines = text.replace(/\r\n/g, '\n').split('\n')
|
|
80
|
+
lines.forEach((raw, index) => {
|
|
81
|
+
const line = raw.trim()
|
|
82
|
+
const marker = MARKER.exec(line)
|
|
83
|
+
if (marker) {
|
|
84
|
+
close()
|
|
85
|
+
if (found.has(marker[1]!)) throw new ScriptError('script-duplicate', [marker[1]!], index + 1)
|
|
86
|
+
current = { id: marker[1]!, line: index + 1, lines: [] }
|
|
87
|
+
} else if (HEADING.test(line)) close()
|
|
88
|
+
else if (COMMENT.test(line)) return
|
|
89
|
+
else if (!line) current?.lines.push(line)
|
|
90
|
+
else if (!current) throw new ScriptError('script-stray', [line.slice(0, 40)], index + 1)
|
|
91
|
+
else current.lines.push(line)
|
|
92
|
+
})
|
|
93
|
+
close()
|
|
94
|
+
return found
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** One edited narration back to its content; cues are taken from the narration it came from. */
|
|
98
|
+
function contentOf(id: string, text: string, before: NarrationNode[]): NarrationNode[] {
|
|
99
|
+
const cues = new Map(before.flatMap(node => node.type === 'cue' ? [[node.id, node] as const] : []))
|
|
100
|
+
const used = new Set<string>()
|
|
101
|
+
const nodes: NarrationNode[] = []
|
|
102
|
+
const plain = (value: string) => {
|
|
103
|
+
if (!value) return
|
|
104
|
+
const last = nodes.at(-1)
|
|
105
|
+
if (last?.type === 'text') last.text += value
|
|
106
|
+
else nodes.push({ type: 'text', text: value })
|
|
107
|
+
}
|
|
108
|
+
let from = 0
|
|
109
|
+
for (const match of text.matchAll(TOKEN)) {
|
|
110
|
+
plain(text.slice(from, match.index))
|
|
111
|
+
from = match.index + match[0].length
|
|
112
|
+
const [, character, display, spoken, cue] = match
|
|
113
|
+
if (character) plain(character)
|
|
114
|
+
else if (display !== undefined && spoken !== undefined) nodes.push({ type: 'pronunciation', display: unescaped(display), spoken: unescaped(spoken) })
|
|
115
|
+
else if (cue) {
|
|
116
|
+
if (used.has(cue)) throw new ScriptError('script-cue-repeated', [id, cue])
|
|
117
|
+
const known = cues.get(cue)
|
|
118
|
+
if (!known) throw new ScriptError('script-cue-unknown', [id, cue])
|
|
119
|
+
nodes.push(structuredClone(known))
|
|
120
|
+
used.add(cue)
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
plain(text.slice(from))
|
|
124
|
+
const removed = [...cues.keys()].filter(cue => !used.has(cue))
|
|
125
|
+
if (removed.length) throw new ScriptError('script-cue-removed', [id, ...removed])
|
|
126
|
+
return nodes
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Applies the file to the document: every narrated step's markers must be there exactly once; only the narrations whose
|
|
131
|
+
* text differs from what an untouched export reads back as are changed. Returns the same document when nothing changed.
|
|
132
|
+
*/
|
|
133
|
+
export function importScript(document: TeachingDocument, text: string): { document: TeachingDocument, changed: string[] } {
|
|
134
|
+
const found = readScript(text)
|
|
135
|
+
const expected = narratedSteps(document).flatMap(step => step.narrations)
|
|
136
|
+
const unknown = [...found.keys()].filter(id => !expected.includes(id))
|
|
137
|
+
if (unknown.length) throw new ScriptError('script-unknown', unknown, found.get(unknown[0]!)!.line)
|
|
138
|
+
const missing = expected.filter(id => !found.has(id))
|
|
139
|
+
if (missing.length) throw new ScriptError('script-missing', missing)
|
|
140
|
+
const changed: string[] = []
|
|
141
|
+
let narrations = document.narrations
|
|
142
|
+
for (const id of expected) {
|
|
143
|
+
const { body, line } = found.get(id)!
|
|
144
|
+
const before = document.narrations[id]!
|
|
145
|
+
if (linesToText(scriptLines(before.content)) === body) continue
|
|
146
|
+
if (!body) throw new ScriptError('script-empty', [id], line)
|
|
147
|
+
if (narrations === document.narrations) narrations = { ...narrations }
|
|
148
|
+
narrations[id] = { ...before, content: contentOf(id, body, before.content) }
|
|
149
|
+
changed.push(id)
|
|
150
|
+
}
|
|
151
|
+
return { document: changed.length ? { ...document, narrations } : document, changed }
|
|
152
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { join } from 'node:path'
|
|
2
|
+
import { findWorkspaceRoot, operatorDir } from '@lincyaw/compiler'
|
|
3
|
+
import { OperatorSpeechSettings, SpeechSettingsError } from '@lincyaw/kernel/node'
|
|
4
|
+
import type { CliMessages } from './messages.ts'
|
|
5
|
+
|
|
6
|
+
const KEY_VARIABLE = 'PRESMIUM_SPEECH_VOLCENGINE_KEY'
|
|
7
|
+
|
|
8
|
+
export class SpeechKeyFailure extends Error {}
|
|
9
|
+
|
|
10
|
+
/** A key typed at the terminal is not echoed; from a pipe or a file the whole input is the key. */
|
|
11
|
+
async function readKey(prompt: string, cancelled: string): Promise<string> {
|
|
12
|
+
const input = process.stdin
|
|
13
|
+
if (!input.isTTY) {
|
|
14
|
+
const chunks: Buffer[] = []
|
|
15
|
+
for await (const chunk of input) chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(String(chunk)))
|
|
16
|
+
return Buffer.concat(chunks).toString('utf8').trim()
|
|
17
|
+
}
|
|
18
|
+
process.stderr.write(prompt)
|
|
19
|
+
input.setRawMode(true)
|
|
20
|
+
input.setEncoding('utf8')
|
|
21
|
+
input.resume()
|
|
22
|
+
return new Promise<string>((resolve, reject) => {
|
|
23
|
+
let value = ''
|
|
24
|
+
const finish = (error?: Error) => {
|
|
25
|
+
input.off('data', typed)
|
|
26
|
+
input.setRawMode(false)
|
|
27
|
+
input.pause()
|
|
28
|
+
process.stderr.write('\n')
|
|
29
|
+
if (error) reject(error)
|
|
30
|
+
else resolve(value.trim())
|
|
31
|
+
}
|
|
32
|
+
const typed = (text: string) => {
|
|
33
|
+
for (const char of text) {
|
|
34
|
+
if (char === '\r' || char === '\n' || char === '\u0004') return finish()
|
|
35
|
+
if (char === '\u0003') return finish(new SpeechKeyFailure(cancelled))
|
|
36
|
+
if (char === '\u007f' || char === '\b') value = value.slice(0, -1)
|
|
37
|
+
else if (char >= ' ') value += char
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
input.on('data', typed)
|
|
41
|
+
})
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** `presmium speech key set|clear|status [<workspace>]`, on the file the Studio's Settings → Voice Service writes. */
|
|
45
|
+
export async function runSpeechKey(action: 'set' | 'clear' | 'status', dir: string, t: CliMessages, env: NodeJS.ProcessEnv = process.env): Promise<number> {
|
|
46
|
+
const root = await findWorkspaceRoot(dir)
|
|
47
|
+
const operator = operatorDir(root, env.PRESMIUM_OPERATOR_DIR)
|
|
48
|
+
const file = join(operator, 'speech', 'credentials.json')
|
|
49
|
+
const settings = new OperatorSpeechSettings(operator, name => env[name])
|
|
50
|
+
const failure = (error: unknown) => error instanceof SpeechSettingsError ? new SpeechKeyFailure(t.speechKeyErrors[error.code](file)) : error
|
|
51
|
+
try {
|
|
52
|
+
process.stdout.write(`${t.speechKeyLabels.workspace} ${root}\n`)
|
|
53
|
+
if (action === 'status') {
|
|
54
|
+
const state = settings.read()
|
|
55
|
+
process.stdout.write(`${t.speechKeyLabels.key} ${state.source === 'saved' ? t.speechKeySaved(state.appIdConfigured === true) : state.source === 'environment' ? t.speechKeyEnvironment(KEY_VARIABLE) : t.speechKeyNone(KEY_VARIABLE)}\n`)
|
|
56
|
+
if (state.source === 'saved') process.stdout.write(`${t.speechKeyLabels.file} ${file}\n`)
|
|
57
|
+
return 0
|
|
58
|
+
}
|
|
59
|
+
if (action === 'set') {
|
|
60
|
+
const key = await readKey(t.speechKeyPrompt, t.speechKeyCancelled)
|
|
61
|
+
if (!key) throw new SpeechKeyFailure(t.speechKeyEmpty)
|
|
62
|
+
const saved = await settings.change({ action: 'save', revision: settings.read().revision, apiKey: key })
|
|
63
|
+
if (!saved.stored) throw new SpeechKeyFailure(t.speechKeyErrors['speech-settings-unavailable'](file))
|
|
64
|
+
process.stdout.write(`${t.speechKeyStored(file)}\n`)
|
|
65
|
+
return 0
|
|
66
|
+
}
|
|
67
|
+
const cleared = await settings.change({ action: 'clear', revision: settings.read().revision })
|
|
68
|
+
process.stdout.write(`${cleared.source === 'environment' ? t.speechKeyClearedEnvironment(KEY_VARIABLE) : t.speechKeyCleared}\n`)
|
|
69
|
+
return 0
|
|
70
|
+
} catch (error) {
|
|
71
|
+
throw failure(error)
|
|
72
|
+
}
|
|
73
|
+
}
|
package/src/voiceover.ts
ADDED
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
import { randomBytes } from 'node:crypto'
|
|
2
|
+
import { relative } from 'node:path'
|
|
3
|
+
import { changeDocumentVoice, errorMessage, SPEECH, speechEngineId, speechFailureCode, VOICEOVER_EXECUTION } from '@lincyaw/kernel'
|
|
4
|
+
import type { SpeechCachePrune, SpeechEngineInfo, SpeechService, SpeechVoice, TeachingDocument, VoiceoverExecution, VoiceoverRunResult, VoiceoverSentence, VoiceoverSentences, VoiceScope } from '@lincyaw/kernel'
|
|
5
|
+
import { Compiler } from '@lincyaw/compiler'
|
|
6
|
+
import type { Compilation } from '@lincyaw/compiler'
|
|
7
|
+
import { readDocumentFile, saveDocumentFile } from './document-file.ts'
|
|
8
|
+
import type { CliMessages } from './messages.ts'
|
|
9
|
+
|
|
10
|
+
export interface VoiceoverOptions {
|
|
11
|
+
entry: string
|
|
12
|
+
/** Make this the document's default voice first. */
|
|
13
|
+
voice?: SpeechVoice
|
|
14
|
+
/** Consent to the paid generation and to the changes; without it the command only reports. */
|
|
15
|
+
yes: boolean
|
|
16
|
+
prune: boolean
|
|
17
|
+
json: boolean
|
|
18
|
+
bundled: string
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** A refusal or failure the author can act on: exit code 1, unlike a usage or internal error. */
|
|
22
|
+
export class VoiceoverFailure extends Error {}
|
|
23
|
+
|
|
24
|
+
/** Sentences that are not generated by design: their step is silent or uses a recording. */
|
|
25
|
+
const BY_DESIGN = new Set(['voice-none', 'recording-unavailable'])
|
|
26
|
+
|
|
27
|
+
interface Tally {
|
|
28
|
+
total: number
|
|
29
|
+
cached: number
|
|
30
|
+
missing: VoiceoverSentence[]
|
|
31
|
+
skipped: number
|
|
32
|
+
blocked: VoiceoverSentence[]
|
|
33
|
+
characters: number
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function tally(sentences: VoiceoverSentence[]): Tally {
|
|
37
|
+
const missing = sentences.filter(sentence => !sentence.cached && !sentence.reason)
|
|
38
|
+
return {
|
|
39
|
+
total: sentences.length,
|
|
40
|
+
cached: sentences.filter(sentence => sentence.cached).length,
|
|
41
|
+
missing,
|
|
42
|
+
skipped: sentences.filter(sentence => sentence.reason && BY_DESIGN.has(sentence.reason)).length,
|
|
43
|
+
blocked: sentences.filter(sentence => !sentence.cached && sentence.reason && !BY_DESIGN.has(sentence.reason)),
|
|
44
|
+
characters: missing.reduce((sum, sentence) => sum + [...sentence.text].length, 0),
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function voiceLabel(voice: SpeechVoice): string {
|
|
49
|
+
return `${voice.engine ? `${voice.engine}:` : ''}${voice.voice}${voice.rate !== undefined && voice.rate !== 1 ? ` ×${voice.rate}` : ''}${voice.style ? ` (${voice.style})` : ''}`
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const sameVoice = (a: SpeechVoice, b: SpeechVoice) => a.voice === b.voice && speechEngineId(a.engine ?? '') === speechEngineId(b.engine ?? '')
|
|
53
|
+
|
|
54
|
+
/** The document's default voice, or why it has none. */
|
|
55
|
+
function defaultVoice(document: TeachingDocument): SpeechVoice | 'none' | undefined {
|
|
56
|
+
const source = document.production.defaults.voiceSource
|
|
57
|
+
return source?.type === 'tts' ? source.voice : source?.type === 'none' ? 'none' : undefined
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function services(compilation: Compilation, t: CliMessages): { execution: VoiceoverExecution & Required<Pick<VoiceoverExecution, 'sentences'>>, speech?: SpeechService } {
|
|
61
|
+
const execution = compilation.service(VOICEOVER_EXECUTION)
|
|
62
|
+
if (!execution?.sentences) throw new VoiceoverFailure(t.voiceoverUnavailable)
|
|
63
|
+
const speech = compilation.service(SPEECH)
|
|
64
|
+
return { execution: execution as VoiceoverExecution & Required<Pick<VoiceoverExecution, 'sentences'>>, ...speech ? { speech } : {} }
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Where a sentence is, as the author numbers pages and steps. */
|
|
68
|
+
function place(compilation: Compilation, sentence: VoiceoverSentence, t: CliMessages): string {
|
|
69
|
+
const index = compilation.doc.sections.findIndex(section => section.id === sentence.section)
|
|
70
|
+
return t.voiceoverPlace(index + 1, sentence.step + 1)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function reasonText(reason: string, t: CliMessages): string {
|
|
74
|
+
const code = speechFailureCode(reason) ?? reason
|
|
75
|
+
return t.voiceoverReasons[code] ?? t.voiceoverReasonOther(reason.length > 80 ? `${reason.slice(0, 80)}…` : reason)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const megabytes = (bytes: number) => (bytes / 1048576).toFixed(1)
|
|
79
|
+
|
|
80
|
+
interface Engines { list: SpeechEngineInfo[], fallback?: string }
|
|
81
|
+
|
|
82
|
+
/** The voice services the sentences use, with whether each is configured and runs on this machine. */
|
|
83
|
+
function usedServices(sentences: VoiceoverSentence[], engines: Engines): { id: string, info?: SpeechEngineInfo }[] {
|
|
84
|
+
const ids = [...new Set(sentences.flatMap(sentence => sentence.request ? [speechEngineId(sentence.request.engine ?? engines.fallback ?? '')] : []))].filter(Boolean)
|
|
85
|
+
return ids.map(id => ({ id, ...engines.list.find(engine => engine.id === id) ? { info: engines.list.find(engine => engine.id === id)! } : {} }))
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function voicesOf(sentences: VoiceoverSentence[], engines: Engines): string[] {
|
|
89
|
+
return [...new Set(sentences.flatMap(sentence => sentence.request ? [voiceLabel({ ...sentence.request, engine: sentence.request.engine ?? engines.fallback })] : []))]
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** On a terminal one line that updates; elsewhere a line at the start, at most every five seconds, and at the end. */
|
|
93
|
+
class Progress {
|
|
94
|
+
readonly #tty = Boolean(process.stderr.isTTY)
|
|
95
|
+
#last = ''
|
|
96
|
+
#at = 0
|
|
97
|
+
show(text: string, final = false): void {
|
|
98
|
+
if (text === this.#last) return
|
|
99
|
+
if (!this.#tty && this.#last && !final && Date.now() - this.#at < 5000) return
|
|
100
|
+
this.#last = text
|
|
101
|
+
this.#at = Date.now()
|
|
102
|
+
process.stderr.write(this.#tty ? `\r\x1b[2K${text}` : `${text}\n`)
|
|
103
|
+
}
|
|
104
|
+
end(): void {
|
|
105
|
+
if (this.#tty && this.#last) process.stderr.write('\n')
|
|
106
|
+
this.#last = ''
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* `presmium voiceover`: what generating the document's voiceover would do, and with consent doing it in this process
|
|
112
|
+
* through the plugins' voiceover execution, the one the workspace service runs for Presmium Studio.
|
|
113
|
+
*/
|
|
114
|
+
export async function runVoiceover(options: VoiceoverOptions, t: CliMessages): Promise<number> {
|
|
115
|
+
const controller = new AbortController()
|
|
116
|
+
const interrupt = () => controller.abort()
|
|
117
|
+
process.once('SIGINT', interrupt)
|
|
118
|
+
try {
|
|
119
|
+
return await voiceover(options, t, controller.signal)
|
|
120
|
+
} finally {
|
|
121
|
+
process.off('SIGINT', interrupt)
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
async function voiceover(options: VoiceoverOptions, t: CliMessages, signal: AbortSignal): Promise<number> {
|
|
126
|
+
const say = (line: string) => { if (!options.json) process.stdout.write(`${line}\n`) }
|
|
127
|
+
const file = readDocumentFile(options.entry)
|
|
128
|
+
const compiler = new Compiler(options.entry, { bundled: options.bundled })
|
|
129
|
+
let compilation = await compiler.compile({ signal })
|
|
130
|
+
let { execution, speech } = services(compilation, t)
|
|
131
|
+
const engines: Engines = { list: await speech?.engines().catch(() => []) ?? [] }
|
|
132
|
+
const fallback = await speech?.defaultSynthesisEngine?.().catch(() => undefined)
|
|
133
|
+
if (fallback) engines.fallback = fallback
|
|
134
|
+
|
|
135
|
+
const before = defaultVoice(file.document)
|
|
136
|
+
const changesVoice = options.voice !== undefined && !(typeof before === 'object' && sameVoice(before, options.voice))
|
|
137
|
+
const planned = changesVoice ? changeDocumentVoice(file.document, { kind: 'document' }, { type: 'tts', voice: options.voice! }) : file.document
|
|
138
|
+
let listing: VoiceoverSentences = await execution.sentences(changesVoice ? { defaultVoice: options.voice! } : {}, signal)
|
|
139
|
+
let counts = tally(listing.sentences)
|
|
140
|
+
|
|
141
|
+
const json: Record<string, unknown> = { document: compilation.deckDir }
|
|
142
|
+
say(`${t.voiceoverLabels.document} ${relative(process.cwd(), compilation.deckDir) || '.'}`)
|
|
143
|
+
const now = defaultVoice(planned)
|
|
144
|
+
const shown = typeof now === 'object' ? voiceLabel(now) : now === 'none' ? t.voiceoverNoVoice : t.voiceoverUnset
|
|
145
|
+
say(`${t.voiceoverLabels.voice} ${changesVoice ? t.voiceoverVoiceChange(shown, typeof before === 'object' ? voiceLabel(before) : before === 'none' ? t.voiceoverNoVoice : t.voiceoverUnset) : t.voiceoverVoiceDefault(shown)}`)
|
|
146
|
+
json.voice = { default: typeof now === 'object' ? now : null, changed: changesVoice }
|
|
147
|
+
const used = usedServices(counts.missing.length ? counts.missing : listing.sentences, engines)
|
|
148
|
+
for (const { id, info } of used) say(`${t.voiceoverLabels.service} ${info ? t.voiceoverService(id, info.provider, info.local) : t.voiceoverServiceMissing(id)}`)
|
|
149
|
+
json.services = used.map(({ id, info }) => ({ id, configured: Boolean(info), ...info ? { provider: info.provider, local: info.local } : {} }))
|
|
150
|
+
say(`${t.voiceoverLabels.sentences} ${t.voiceoverCounts(counts.total, counts.cached, counts.missing.length, counts.skipped, counts.blocked.length)}`)
|
|
151
|
+
const sentencesJson = (value: Tally) => ({ total: value.total, cached: value.cached, missing: value.missing.length, characters: value.characters, skipped: value.skipped, blocked: value.blocked.map(sentence => ({ section: sentence.section, step: sentence.step, ...sentence.stepId ? { stepId: sentence.stepId } : {}, reason: sentence.reason })) })
|
|
152
|
+
json.sentences = sentencesJson(counts)
|
|
153
|
+
const cloud = used.filter(({ info }) => info && !info.local).map(({ info }) => info!.provider)
|
|
154
|
+
if (counts.missing.length) {
|
|
155
|
+
say(`${t.voiceoverLabels.missing} ${t.voiceoverMissing(counts.missing.length, counts.characters, voicesOf(counts.missing, engines))}`)
|
|
156
|
+
say(cloud.length ? t.voiceoverCharges([...new Set(cloud)]) : t.voiceoverLocal)
|
|
157
|
+
}
|
|
158
|
+
const reasons = new Map<string, VoiceoverSentence[]>()
|
|
159
|
+
for (const sentence of counts.blocked) reasons.set(sentence.reason!, [...reasons.get(sentence.reason!) ?? [], sentence])
|
|
160
|
+
for (const [reason, group] of reasons) say(`${t.voiceoverLabels.blocked} ${t.voiceoverBlocked(group.length, reasonText(reason, t), group.slice(0, 3).map(sentence => place(compilation, sentence, t)), group.length > 3)}`)
|
|
161
|
+
|
|
162
|
+
let prune: SpeechCachePrune | undefined
|
|
163
|
+
const pruneCache = async (value: VoiceoverSentences, remove: boolean) => {
|
|
164
|
+
if (compilation.doc.diagnostics.some(finding => finding.severity === 'error')) throw new VoiceoverFailure(t.voiceoverPruneErrors)
|
|
165
|
+
if (!speech?.pruneCache) throw new VoiceoverFailure(t.voiceoverPruneUnavailable)
|
|
166
|
+
try {
|
|
167
|
+
return await speech.pruneCache(value.sentences.flatMap(sentence => sentence.request ? [sentence.request] : []), { remove, signal })
|
|
168
|
+
} catch (error) {
|
|
169
|
+
if (errorMessage(error) === 'speech-prune-engine-unknown') throw new VoiceoverFailure(t.voiceoverPruneEngineUnknown)
|
|
170
|
+
throw error
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
if (options.prune) {
|
|
174
|
+
prune = await pruneCache(listing, false)
|
|
175
|
+
const bytes = prune.unused.reduce((sum, entry) => sum + entry.bytes, 0)
|
|
176
|
+
say(`${t.voiceoverLabels.prune} ${t.voiceoverPruneWould(prune.unused.length, megabytes(bytes), prune.kept)}`)
|
|
177
|
+
json.prune = { unused: prune.unused.length, bytes, kept: prune.kept, removed: false }
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const work = counts.missing.length > 0 || changesVoice || Boolean(prune?.unused.length)
|
|
181
|
+
if (!options.yes) {
|
|
182
|
+
say(work ? t.voiceoverDryRun : counts.blocked.length ? t.voiceoverNothingElse : t.voiceoverNothing)
|
|
183
|
+
if (options.json) process.stdout.write(`${JSON.stringify({ ...json, yes: false }, null, 2)}\n`)
|
|
184
|
+
return 0
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
if (changesVoice) {
|
|
188
|
+
const refused = counts.blocked.find(sentence => sentence.request && sameVoice(sentence.request, options.voice!))
|
|
189
|
+
if (refused) throw new VoiceoverFailure(t.voiceoverVoiceRefused(voiceLabel(options.voice!), reasonText(refused.reason!, t)))
|
|
190
|
+
saveDocumentFile(file, planned)
|
|
191
|
+
say(t.voiceoverVoiceSaved(voiceLabel(options.voice!)))
|
|
192
|
+
compilation = await compiler.compile({ signal })
|
|
193
|
+
;({ execution, speech } = services(compilation, t))
|
|
194
|
+
listing = await execution.sentences({}, signal)
|
|
195
|
+
counts = tally(listing.sentences)
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
let result: VoiceoverRunResult | undefined
|
|
199
|
+
let after = counts
|
|
200
|
+
if (counts.missing.length) {
|
|
201
|
+
const intent = randomBytes(18).toString('base64url')
|
|
202
|
+
// By step when every missing sentence has one, so that only the steps with missing audio are gone through.
|
|
203
|
+
const targets = [...new Map(counts.missing.flatMap(sentence => sentence.target ? [[JSON.stringify(sentence.target), sentence.target] as const] : [])).values()]
|
|
204
|
+
const scope: VoiceScope = counts.missing.every(sentence => sentence.target) ? { kind: 'steps', targets } : { kind: 'document' }
|
|
205
|
+
const prepared = await execution.prepare({ intent, source: listing.source, revision: listing.revision, scope }, signal)
|
|
206
|
+
const progress = new Progress()
|
|
207
|
+
try {
|
|
208
|
+
progress.show(t.voiceoverGenerating(0, prepared.total))
|
|
209
|
+
result = await prepared.run(signal, value => progress.show(t.voiceoverGenerating(value.completed + value.failed, prepared.total), value.completed + value.failed === prepared.total))
|
|
210
|
+
} finally {
|
|
211
|
+
progress.end()
|
|
212
|
+
prepared.dispose()
|
|
213
|
+
}
|
|
214
|
+
// Read the cache again, also after a cancel, to report what was really made; a changed document can't be read.
|
|
215
|
+
if (result.state !== 'stale') {
|
|
216
|
+
try {
|
|
217
|
+
listing = await execution.sentences({}, signal.aborted ? new AbortController().signal : signal)
|
|
218
|
+
after = tally(listing.sentences)
|
|
219
|
+
} catch (error) {
|
|
220
|
+
if (!signal.aborted) throw error
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
const generated = Math.max(0, after.cached - counts.cached)
|
|
224
|
+
say(result.state === 'cancelled' ? t.voiceoverCancelled(generated) : result.state === 'stale' ? t.voiceoverStale(generated) : t.voiceoverGenerated(generated))
|
|
225
|
+
if (result.error && result.state !== 'cancelled') say(t.voiceoverRunError(reasonText(result.error, t)))
|
|
226
|
+
json.generation = { state: result.state, generated, ...result.error ? { error: result.error } : {} }
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
if (options.prune && !signal.aborted && result?.state !== 'stale') {
|
|
230
|
+
prune = await pruneCache(listing, true)
|
|
231
|
+
const bytes = prune.unused.reduce((sum, entry) => sum + entry.bytes, 0)
|
|
232
|
+
say(t.voiceoverPruned(prune.unused.length, megabytes(bytes)))
|
|
233
|
+
json.prune = { unused: prune.unused.length, bytes, kept: prune.kept, removed: true }
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
const left = after.missing.length + after.blocked.length
|
|
237
|
+
if (left) say(t.voiceoverLeft(after.missing.length, after.blocked.length))
|
|
238
|
+
json.remaining = sentencesJson(after)
|
|
239
|
+
if (options.json) process.stdout.write(`${JSON.stringify({ ...json, yes: true }, null, 2)}\n`)
|
|
240
|
+
return left || (result && result.state !== 'done' && result.state !== 'failed') ? 1 : 0
|
|
241
|
+
}
|