dsh-laivox-tts 0.7.20 → 0.7.22

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.en.md CHANGED
@@ -38,7 +38,7 @@ This installs the prebuilt package from npm—nothing is compiled locally and no
38
38
 
39
39
  ### Install from GitHub Release or offline package
40
40
 
41
- If npm is unavailable or you prefer pinning a release tarball, install via URL or download `dsh-laivox-tts.tgz` from [Releases](https://github.com/xiaolai181/dsh_laivox_tts/releases) (replace `latest/download` with `download/v0.7.20` to pin a version):
41
+ If npm is unavailable or you prefer pinning a release tarball, install via URL or download `dsh-laivox-tts.tgz` from [Releases](https://github.com/xiaolai181/dsh_laivox_tts/releases) (replace `latest/download` with `download/v0.7.22` to pin a version):
42
42
 
43
43
  ```sh
44
44
  dsh plugin --profile desktop add "https://github.com/xiaolai181/dsh_laivox_tts/releases/latest/download/dsh-laivox-tts.tgz"
package/README.md CHANGED
@@ -40,7 +40,7 @@ dsh plugin --profile desktop add dsh-laivox-tts
40
40
 
41
41
  ### GitHub Release 链接或离线安装包安装
42
42
 
43
- 如需锁定版本或无法直连 npm,可使用 Release 下载链接(将 `latest/download` 替换为 `download/v0.7.20` 可锁定版本),或从 [Releases](https://github.com/xiaolai181/dsh_laivox_tts/releases) 下载 `dsh-laivox-tts.tgz` 到本地后安装:
43
+ 如需锁定版本或无法直连 npm,可使用 Release 下载链接(将 `latest/download` 替换为 `download/v0.7.22` 可锁定版本),或从 [Releases](https://github.com/xiaolai181/dsh_laivox_tts/releases) 下载 `dsh-laivox-tts.tgz` 到本地后安装:
44
44
 
45
45
  ```sh
46
46
  dsh plugin --profile desktop add "https://github.com/xiaolai181/dsh_laivox_tts/releases/latest/download/dsh-laivox-tts.tgz"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-laivox-tts",
3
- "version": "0.7.20",
3
+ "version": "0.7.22",
4
4
  "private": false,
5
5
  "description": "把 TXT、EPUB、PDF 小说做成单人朗读或多角色有声书,也能一键朗读对话里的 AI 回答,支持本地书架与 MP3/WAV 导出。",
6
6
  "license": "MIT",
@@ -6,6 +6,14 @@ export const SCRIPT_VERSION = 3
6
6
 
7
7
  const openers = { '「': '」', '『': '』', '“': '”' }
8
8
  const bareName = /^[^\s,。!?;:、]{1,6}$/u
9
+ const bareSpeakerTag = /^[^\s,。!?;:、]{1,6}[::]$/u
10
+ const spokenAttribution = /[说問问答道喊叫答叹嘆]/u
11
+
12
+ // A chat label such as "林夏:" is not spoken. "他说:" still is: it names how the line was said.
13
+ export function isBareSpeakerTag(text) {
14
+ const trimmed = String(text ?? '').trim()
15
+ return bareSpeakerTag.test(trimmed) && !spokenAttribution.test(trimmed)
16
+ }
9
17
 
10
18
  function foldIndex(text) {
11
19
  const chars = []
@@ -270,7 +278,10 @@ export function uncoveredSpeakable(text, lines, removed = []) {
270
278
  }
271
279
  if (hole) gaps.push(hole)
272
280
  }
273
- return gaps.filter(gap => hasSpeakable(body.slice(gap.start, gap.end))).map(gap => ({
281
+ return gaps.filter(gap => {
282
+ const slice = body.slice(gap.start, gap.end)
283
+ return hasSpeakable(slice) && !isBareSpeakerTag(slice)
284
+ }).map(gap => ({
274
285
  start: gap.start,
275
286
  end: gap.end,
276
287
  preview: [...body.slice(gap.start, gap.end)].slice(0, 20).join(''),
package/src/host/cast.mjs CHANGED
@@ -1,6 +1,7 @@
1
1
  import { createHash } from 'node:crypto'
2
2
  import { normalizeInstruct } from '../domain/voice-direction.mjs'
3
3
  import { ROLE_AGES } from '../domain/voice-assignment.mjs'
4
+ import { dialogueMatches, hasSpeakable } from '../domain/segment.mjs'
4
5
  import { alignLine, uncoveredSpeakable } from '../domain/script-lines.mjs'
5
6
 
6
7
  export const EMOTIONS = ['neutral', 'happy', 'sad', 'angry', 'afraid', 'tense', 'gentle']
@@ -212,7 +213,7 @@ export const EXTRACT = [
212
213
  '【说话人】',
213
214
  '1. 只有角色说出口的话,或明确的内心独白,归该角色。动作、神态、环境和“他说”“她问道”归 narrator。',
214
215
  '2. 没有写名字的引号对白不能标成 narrator。根据上一句动作或对话的另一方判断;不知道真名时新建“男人”“女人”“陌生人”。',
215
- '3. 段首如果只是纯名字加冒号,丢掉这个名字,后面的话以及同一人紧接着说的后文都归这个人。名字后面还带着动作或修饰时,那半句(含冒号)归 narrator。',
216
+ '3. 段首 AAAA:BBBB。AAAA 只是纯名字、没有动作神态和环境时,丢掉这个名字,BBBB 以及同一人紧接着说的后文都归这个人;名字在 roles 里就用对应 ref,不在才新建。禁止把「审问者:姓名?」整句标成 narrator。AAAA 带动作则归旁白:这半句连冒号归 narrator,后面的话另成一条。',
216
217
  '4. 复用 roles 里的 ref。新角色先声明 {"ref":"new1","name":"角色名","gender":"female|male|","age":"child|teen|young|middle|elder|"},最多 12 个。',
217
218
  '',
218
219
  '【正文与语气】',
@@ -289,6 +290,91 @@ export function extractResult(reply, windowText, windowStart, bookRoles, occupie
289
290
  return { lines, roles: [...added.values()] }
290
291
  }
291
292
 
293
+ function hasQuotedSpeech(text) {
294
+ for (const _match of dialogueMatches(text)) return true
295
+ return false
296
+ }
297
+
298
+ function narrationInstruct(lines, at) {
299
+ let before = null
300
+ let after = null
301
+ for (const line of lines) {
302
+ if (line?.roleId !== NARRATOR.id || !line.instruct) continue
303
+ if (line.end <= at && (!before || line.end > before.end)) before = line
304
+ else if (line.start >= at && (!after || line.start < after.start)) after = line
305
+ }
306
+ return before?.instruct || after?.instruct || '客观叙述,语气平稳。'
307
+ }
308
+
309
+ // "叶文洁:我没走。" is a labeled turn. "雷志成压低了声音说:" is not: nothing speakable follows the colon.
310
+ function colonTail(text) {
311
+ const match = /^([^\s,。!?;:、]{1,12})[::][ \t]*([\s\S]*)$/u.exec(String(text ?? '').trim())
312
+ if (!match || !hasSpeakable(match[2])) return null
313
+ return { name: match[1].normalize('NFKC'), speech: match[2].trim() }
314
+ }
315
+
316
+ // A known role wins even when the name contains 问 or 说, as in 审问者.
317
+ function labeledByRole(text, roles) {
318
+ const trimmed = String(text ?? '').trim()
319
+ let found = null
320
+ const seen = new Set()
321
+ for (const role of roles ?? []) {
322
+ if (!role || role.id === NARRATOR.id || seen.has(role.id)) continue
323
+ seen.add(role.id)
324
+ const name = roleName(role.name)
325
+ if (!name || [...name].length > 12 || !trimmed.startsWith(name)) continue
326
+ const rest = trimmed.slice(name.length).trimStart()
327
+ if (!/^[::]/u.test(rest)) continue
328
+ const speech = rest.replace(/^[::][ \t]*/u, '').trim()
329
+ if (!hasSpeakable(speech)) continue
330
+ if (!found || [...name].length > [...found.name].length) found = { role, name, speech }
331
+ }
332
+ return found
333
+ }
334
+
335
+ // Quote-free narration the model skipped is copied back as narrator. A line that
336
+ // still says "名字:台词" goes back to the model. splitKnown is only for the pass
337
+ // after that one repair: a known speaker is split instead of being read as narrator.
338
+ function fillNarrationGaps(windowText, windowStart, lines, roles = [], splitKnown = false) {
339
+ const local = line => ({ start: line.start - windowStart, end: line.end - windowStart })
340
+ const gaps = uncoveredSpeakable(windowText, lines.map(local))
341
+ const dialogue = []
342
+ const next = [...lines]
343
+ for (const gap of gaps) {
344
+ const text = windowText.slice(gap.start, gap.end)
345
+ const labeled = labeledByRole(text, roles)
346
+ if (labeled && splitKnown) {
347
+ const start = windowStart + gap.start
348
+ next.push({
349
+ roleId: labeled.role.id, content: labeled.speech, source: text, start, end: windowStart + gap.end,
350
+ emotion: 'neutral', intensity: 0,
351
+ })
352
+ continue
353
+ }
354
+ if (labeled || hasQuotedSpeech(text) || colonTail(text)) {
355
+ dialogue.push({ start: gap.start, end: gap.end, text })
356
+ continue
357
+ }
358
+ const start = windowStart + gap.start
359
+ const spoken = text.replace(/^[「」『』“”\s]+/u, '').replace(/[「」『』“”\s]+$/u, '')
360
+ if (!spoken) continue
361
+ const instruct = normalizeInstruct(narrationInstruct(next, start))
362
+ next.push({
363
+ roleId: NARRATOR.id, content: spoken, source: text, start, end: windowStart + gap.end,
364
+ emotion: 'neutral', intensity: 0, ...(instruct ? { instruct } : {}),
365
+ })
366
+ }
367
+ next.sort((a, b) => a.start - b.start || a.end - b.end)
368
+ return { lines: next, dialogue }
369
+ }
370
+
371
+ // A hole inside a window that already succeeded. Only plain narration is copied
372
+ // back here. A labeled or quoted hole returns null so the caller asks the model.
373
+ export function narrationSlice(text, start, roles = []) {
374
+ const filled = fillNarrationGaps(text, start, [], roles, false)
375
+ return filled.dialogue.length ? null : filled.lines
376
+ }
377
+
292
378
  export async function extractWindow(windowText, windowStart, bookRoles, context, request, palette = { narrator: [], roles: [] }, lookupRoles = bookRoles) {
293
379
  const base = { roles: promptRoles(bookRoles), palette, context: Array.isArray(context) ? context.slice(-2) : [] }
294
380
  let reply
@@ -305,28 +391,29 @@ export async function extractWindow(windowText, windowStart, bookRoles, context,
305
391
  if (error?.message === 'MODEL_OUTPUT_INVALID') return { truncated: true, lines: [], gaps: [], roles: [], complete: false }
306
392
  throw error
307
393
  }
308
- const local = line => ({ start: line.start - windowStart, end: line.end - windowStart })
309
- let gaps = uncoveredSpeakable(windowText, first.lines.map(local))
310
- if (!gaps.length) return { lines: first.lines, roles: first.roles, gaps: [], complete: true, truncated: false }
394
+ const knownRoles = [...bookRoles, ...first.roles]
395
+ const absorbed = fillNarrationGaps(windowText, windowStart, first.lines, knownRoles, false)
396
+ if (!absorbed.dialogue.length) return { lines: absorbed.lines, roles: first.roles, gaps: [], complete: true, truncated: false }
397
+ const settle = (lines, roleList) => fillNarrationGaps(windowText, windowStart, lines, roleList, true)
311
398
  let repair
312
399
  try {
313
400
  repair = await request(EXTRACT + '\n' + REPAIR_EXTRACT, {
314
401
  ...base,
315
- segments: gaps.map(gap => ({ start: windowStart + gap.start, text: windowText.slice(gap.start, gap.end) })),
402
+ segments: absorbed.dialogue.map(gap => ({ start: windowStart + gap.start, text: gap.text })),
316
403
  })
317
404
  } catch (error) {
318
405
  if (error?.message === 'MODEL_RESPONSE_TRUNCATED' || error?.message === 'MODEL_OUTPUT_INVALID') {
319
- return { lines: first.lines, roles: first.roles, gaps, complete: false, truncated: false }
406
+ const settled = settle(absorbed.lines, knownRoles)
407
+ return { lines: settled.lines, roles: first.roles, gaps: settled.dialogue, complete: settled.dialogue.length === 0, truncated: false }
320
408
  }
321
409
  throw error
322
410
  }
323
- const lines = [...first.lines]
411
+ const lines = [...absorbed.lines]
324
412
  const roles = new Map(first.roles.map(role => [role.id, role]))
325
- for (const gap of gaps) {
326
- const text = windowText.slice(gap.start, gap.end)
413
+ for (const gap of absorbed.dialogue) {
327
414
  let extra
328
415
  try {
329
- extra = extractResult(repair, text, windowStart + gap.start, [...bookRoles, ...first.roles], [], lookupRoles)
416
+ extra = extractResult(repair, gap.text, windowStart + gap.start, [...bookRoles, ...first.roles], [], lookupRoles)
330
417
  } catch (error) {
331
418
  if (error?.message === 'MODEL_OUTPUT_INVALID') continue
332
419
  throw error
@@ -338,7 +425,6 @@ export async function extractWindow(windowText, windowStart, bookRoles, context,
338
425
  }
339
426
  for (const role of extra.roles) roles.set(role.id, role)
340
427
  }
341
- lines.sort((a, b) => a.start - b.start || a.end - b.end)
342
- gaps = uncoveredSpeakable(windowText, lines.map(local))
343
- return { lines, roles: [...roles.values()], gaps, complete: gaps.length === 0, truncated: false }
428
+ const filled = settle(lines, [...knownRoles, ...roles.values()])
429
+ return { lines: filled.lines, roles: [...roles.values()], gaps: filled.dialogue, complete: filled.dialogue.length === 0, truncated: false }
344
430
  }
@@ -7,7 +7,7 @@ import { voiceAssignments } from '../domain/voice-assignment.mjs'
7
7
  import { messageFor } from '../domain/messages.mjs'
8
8
  import { addModelUsage, normalizeModelUsage } from './model-usage.mjs'
9
9
 
10
- import { NARRATOR, extractWindow, recoverCastBatch } from './cast.mjs'
10
+ import { NARRATOR, extractWindow, narrationSlice, recoverCastBatch } from './cast.mjs'
11
11
  import { EXTRACT_SCRIPT, partsFromLines, splitOversized, uncoveredSpeakable } from '../domain/script-lines.mjs'
12
12
  import { assertModelProviderReady, createStreamGate, MODEL_IDLE_MS, MODEL_TOTAL_MS, readModelStream } from './model-stream.mjs'
13
13
 
@@ -264,20 +264,26 @@ function editorialSignature(value) {
264
264
  return JSON.stringify([segmentVersion(value), removed, value.cast === true, annotations])
265
265
  }
266
266
 
267
+ // A hole does not reopen the window that already produced lines. Resume asks only
268
+ // for the missing slice. A window that never produced lines is that slice itself.
267
269
  export function reopenGappedWindows(progress) {
268
270
  if (progress?.script !== 'extract-v1' || !progress.gaps?.length || !Array.isArray(progress.windows)) return
269
- const reopened = []
270
- for (const item of progress.windows) {
271
- if (progress.gaps.some(gap => item.start < gap.end && item.end > gap.start)) {
272
- item.state = 'pending'
273
- item.splits = 0
274
- reopened.push(item)
271
+ const added = []
272
+ for (const gap of progress.gaps) {
273
+ if (!Number.isSafeInteger(gap?.start) || !Number.isSafeInteger(gap?.end) || gap.end <= gap.start) continue
274
+ if (progress.windows.some(item => item.state === 'pending' && item.start === gap.start && item.end === gap.end)) continue
275
+ const parent = progress.windows.find(item => item.start <= gap.start && item.end >= gap.end && typeof item.text === 'string')
276
+ if (!parent) continue
277
+ if (parent.state === 'gap' && parent.start === gap.start && parent.end === gap.end) {
278
+ parent.state = 'pending'
279
+ parent.splits = 0
280
+ continue
275
281
  }
282
+ const text = parent.text.slice(gap.start - parent.start, gap.end - parent.start)
283
+ if (!text) continue
284
+ added.push({ start: gap.start, end: gap.end, text, state: 'pending', splits: 0 })
276
285
  }
277
- if (reopened.length) {
278
- const inReopened = line => reopened.some(item => line.start < item.end && line.end > item.start)
279
- progress.cast = (progress.cast ?? []).map(batch => (batch ?? []).filter(line => line && !inReopened(line))).filter(batch => batch.length)
280
- }
286
+ progress.windows.push(...added)
281
287
  progress.gaps = []
282
288
  }
283
289
 
@@ -490,7 +496,9 @@ export function createEditorialService(llm, library, {
490
496
  names.set(NARRATOR.id, NARRATOR.name)
491
497
  const context = doneLines.slice(-2).map(line => ({ source: line.source, roleName: names.get(line.roleId) ?? '' }))
492
498
  const knownIds = new Set(known.map(role => role.id))
493
- const output = await extractWindow(item.text, item.start, known, context, (prompt, payload) => request(prompt, payload, index), withSeed(paletteFrom(progress.cast, known), progress.seed, knownIds), [NARRATOR, ...allRoles])
499
+ const nested = progress.windows.some(row => row !== item && row.state === 'done' && row.start <= item.start && row.end >= item.end)
500
+ const local = nested ? narrationSlice(item.text, item.start, allRoles) : null
501
+ const output = local != null ? { lines: local, roles: [], truncated: false } : await extractWindow(item.text, item.start, known, context, (prompt, payload) => request(prompt, payload, index), withSeed(paletteFrom(progress.cast, known), progress.seed, knownIds), [NARRATOR, ...allRoles])
494
502
  if (output.truncated) {
495
503
  if ((item.splits ?? 0) >= 1) {
496
504
  item.state = 'gap'