dsh-podwave 0.11.19 → 0.11.21

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/src/transcribe.js CHANGED
@@ -9,15 +9,17 @@ import { copyFile, mkdir, readFile, readdir, rename, rm, writeFile } from 'node:
9
9
  import { fileURLToPath } from 'node:url'
10
10
  import { DATA_DIR } from './state.js'
11
11
  import { loadModelConfig, modelDir } from './models.js'
12
- import { ffmpegPath, ffprobePath, venvPython } from './platform.js'
12
+ import { ffmpegPath, ffprobePath, killTree, venvBin, venvPython } from './platform.js'
13
13
  import { getSection } from './config.js'
14
+ import { artifactByDir } from './catalog.js'
14
15
  import { assignSpeakers, diarizeInfo, runDiarization } from './diarize.js'
15
- import { flushJobs, getJob, jobKey, removeJob, upsertJob } from './jobs.js'
16
+ import { flushJobs, getJob, getJobFresh, jobKey, removeJob, upsertJob } from './jobs.js'
16
17
 
17
18
  export const TRANSCRIPTS_DIR = path.join(DATA_DIR, 'transcripts')
18
19
  const WORK_DIR = path.join(TRANSCRIPTS_DIR, '.work')
19
- const RUNTIME_DIR = path.join(DATA_DIR, 'runtime')
20
- const CLI = path.join(RUNTIME_DIR, '.venv', 'bin', 'mlx-qwen3-asr')
20
+ // Windows 的虚拟环境是 .venv\Scripts\mlx-qwen3-asr.exe,不是 .venv/bin/。
21
+ // 用 venvBin() 拼平台路径,否则 Windows 上永远判定「运行环境未安装」。
22
+ const CLI = venvBin('mlx-qwen3-asr')
21
23
  const SHERPA_SCRIPT = path.join(path.dirname(fileURLToPath(import.meta.url)), '..', 'scripts', 'asr_sherpa_batch.py')
22
24
  const FFMPEG = process.env.PODWAVE_FFMPEG || ffmpegPath() || 'ffmpeg'
23
25
  const FFPROBE = process.env.PODWAVE_FFPROBE || ffprobePath() || 'ffprobe'
@@ -30,6 +32,13 @@ let active = null // { key, episodeId }
30
32
  let activeChild = null
31
33
  const cancelRequested = new Set()
32
34
 
35
+ // 宿主热重载后,新模块实例拿不到旧实例的 activeChild,取消就杀不掉那个进程。
36
+ // 所以把当前工作进程的 pid 也写进 job,取消时先按内存找,找不到再按 pid 兜底。
37
+ // Windows 上 SIGTERM 等于硬杀,交给 killTree 用 taskkill /T 连子孙一起收。
38
+ function stopChildByPid(pid) {
39
+ return killTree(pid)
40
+ }
41
+
33
42
  function transcriptPath(id) {
34
43
  return path.join(TRANSCRIPTS_DIR, `${id}.json`)
35
44
  }
@@ -41,7 +50,11 @@ function round2(value) {
41
50
  export function currentAsrModel() {
42
51
  const name = loadModelConfig().asrModel
43
52
  const dir = modelDir(name)
44
- const engine = /sensevoice|sherpa/i.test(name) ? 'sherpa' : 'mlx'
53
+ // 不能靠模型名正则猜引擎:换个名字(比如 Windows 的 sensevoice-small-int8 之外的写法)
54
+ // 就会掉进 MLX 分支,然后去查只有 macOS 才有的 CLI,永远 installed:false。
55
+ // 以 catalog 元数据为准,查不到时再退回正则兜底。
56
+ const artifact = artifactByDir(name, 'asr')
57
+ const engine = artifact ? (artifact.engine === 'onnx' ? 'sherpa' : 'mlx') : /sensevoice|sherpa/i.test(name) ? 'sherpa' : 'mlx'
45
58
  const ready =
46
59
  engine === 'sherpa'
47
60
  ? fs.existsSync(path.join(dir, 'model.int8.onnx'))
@@ -61,13 +74,17 @@ export function runtimeInfo() {
61
74
  const model = currentAsrModel()
62
75
  const config = transcribeConfig()
63
76
  const sherpa = model.engine === 'sherpa'
77
+ // sherpa 走 venv 里的 python 跑脚本;MLX 走 venv 里的命令行工具。
78
+ // 两者都必须在 Windows 的 Scripts\ 下找,否则 installed 永远 false。
79
+ const cliPath = sherpa ? venvPython() : CLI
80
+ const installed = sherpa ? fs.existsSync(SHERPA_SCRIPT) && fs.existsSync(cliPath) : fs.existsSync(cliPath)
64
81
  return {
65
82
  engine: model.engine,
66
- installed: sherpa ? fs.existsSync(SHERPA_SCRIPT) && fs.existsSync(venvPython()) : fs.existsSync(CLI),
83
+ installed,
67
84
  modelReady: model.ready,
68
85
  model: model.dir,
69
86
  modelName: model.name,
70
- cli: sherpa ? venvPython() : CLI,
87
+ cli: cliPath,
71
88
  hotwords: config.hotwords,
72
89
  chunkMinutes: config.chunkMinutes,
73
90
  diarize: config.diarize,
@@ -696,6 +713,7 @@ async function runJob(item) {
696
713
  stdio: ['ignore', 'ignore', 'pipe'],
697
714
  })
698
715
  activeChild = child
716
+ upsertJob({ key, pid: child.pid || null })
699
717
  child.stderr.on('data', (chunk) => {
700
718
  stderrTail = (stderrTail + chunk.toString()).slice(-4000)
701
719
  })
@@ -714,6 +732,18 @@ async function runJob(item) {
714
732
  }
715
733
  lastIngestAt = Date.now()
716
734
  }
735
+ // 取消可能来自另一个模块实例(宿主热重载后旧实例还在跑这个任务),
736
+ // 它只能写磁盘;这里每轮读一次磁盘,看到取消意图就转成内存里的取消请求。
737
+ if (!cancelRequested.has(key)) {
738
+ const disk = getJobFresh(key)
739
+ if (disk && (disk.status === 'cancelled' || disk.stage === 'cancelling')) cancelRequested.add(key)
740
+ }
741
+ const cancelled = cancelRequested.has(key)
742
+ if (cancelled) {
743
+ // 已取消就别再写 progress 把磁盘上的 cancelling 覆盖回转写中。
744
+ if (child) killTree(child.pid)
745
+ return
746
+ }
717
747
  const current = plan.parts.find((part) => !hasResult(resultsDir, part.index)) || null
718
748
  const eta = current ? Math.min(current.duration * 0.96, Math.max(0, (Date.now() - lastIngestAt) / 1000) * SPEED) : 0
719
749
  upsertJob({
@@ -723,13 +753,6 @@ async function runJob(item) {
723
753
  progress: progressOf(plan, counts.seconds, eta),
724
754
  lineCount: lastLineCount,
725
755
  })
726
- if (cancelRequested.has(key) && child) {
727
- try {
728
- child.kill('SIGTERM')
729
- } catch {
730
- /* ignore */
731
- }
732
- }
733
756
  }
734
757
 
735
758
  const ticker = setInterval(() => {
@@ -742,15 +765,30 @@ async function runJob(item) {
742
765
  await ingest()
743
766
  const counts = countResults(resultsDir, plan)
744
767
 
768
+ // 子进程刚退出时,ticker 可能还来不及把磁盘上的取消意图转成内存标记,
769
+ // 所以这里再读一次磁盘,避免「已经取消了」被当成进程异常退出。
770
+ if (!cancelRequested.has(key)) {
771
+ const disk = getJobFresh(key)
772
+ if (disk && (disk.status === 'cancelled' || disk.stage === 'cancelling')) cancelRequested.add(key)
773
+ }
745
774
  if (cancelRequested.has(key)) {
746
775
  cancelRequested.delete(key)
747
776
  if (backedUp) await copyFile(backup, target).catch(() => {})
748
777
  else if (partialWritten) await rm(target, { force: true }).catch(() => {})
749
- upsertJob({ key, status: 'cancelled', stage: 'cancelled', finishedAt: Date.now(), error: '用户取消了转写' })
778
+ upsertJob({ key, status: 'cancelled', stage: 'cancelled', finishedAt: Date.now(), error: '用户取消了转写', pid: null })
750
779
  return
751
780
  }
752
781
  if (code !== 0) {
753
- const detail = stderrTail.trim().split('\n').slice(-6).join('\n')
782
+ if (code === null) {
783
+ // 进程被信号杀掉(宿主重启、插件重载、手动 kill)。分块结果都留着,可以续跑。
784
+ throw new Error('转写进程被中断,已完成的分块保留,可以点重试继续')
785
+ }
786
+ const detail = stderrTail
787
+ .trim()
788
+ .split('\n')
789
+ .filter((line) => line && !/MallocStackLogging|malloc stack logging/.test(line))
790
+ .slice(-6)
791
+ .join('\n')
754
792
  throw new Error(`转写进程退出码 ${code}${detail ? `\n${detail}` : ''}`)
755
793
  }
756
794
  if (counts.count < plan.parts.length) throw new Error('部分音频块没有转写结果')
@@ -810,9 +848,13 @@ async function runJob(item) {
810
848
  } catch (error) {
811
849
  if (backedUp) await copyFile(backup, target).catch(() => {})
812
850
  else if (partialWritten) await rm(target, { force: true }).catch(() => {})
813
- if (cancelRequested.has(key)) {
851
+ // 宿主热重载后,取消可能由新模块实例写进磁盘;本实例内存里的 cancelRequested 是空的。
852
+ // 所以收尾必须以磁盘上的真实状态为准,否则「已取消」会被覆写成「进程退出码 -15」。
853
+ const disk = getJobFresh(key)
854
+ const cancelledOnDisk = disk?.status === 'cancelled' || disk?.stage === 'cancelling'
855
+ if (cancelRequested.has(key) || cancelledOnDisk) {
814
856
  cancelRequested.delete(key)
815
- upsertJob({ key, status: 'cancelled', stage: 'cancelled', finishedAt: Date.now(), error: '用户取消了转写' })
857
+ upsertJob({ key, status: 'cancelled', stage: 'cancelled', finishedAt: Date.now(), error: '用户取消了转写', pid: null })
816
858
  } else {
817
859
  upsertJob({
818
860
  key,
@@ -826,6 +868,7 @@ async function runJob(item) {
826
868
  cancelRequested.delete(key)
827
869
  active = null
828
870
  activeChild = null
871
+ upsertJob({ key, pid: null, orphan: false })
829
872
  flushJobs()
830
873
  pump()
831
874
  }
@@ -1053,7 +1096,7 @@ export function cancelTranscription(id) {
1053
1096
  const key = jobId(id)
1054
1097
  const job = getJob(key)
1055
1098
  if (!job) return { status: 'none' }
1056
- if (job.status === 'done' || job.status === 'error' || job.status === 'cancelled' || job.status === 'interrupted') {
1099
+ if (job.status === 'done' || job.status === 'error' || job.status === 'cancelled') {
1057
1100
  return { status: job.status }
1058
1101
  }
1059
1102
  const index = queue.findIndex((item) => item.episode.id === id)
@@ -1065,24 +1108,20 @@ export function cancelTranscription(id) {
1065
1108
  return { status: 'cancelled' }
1066
1109
  }
1067
1110
  cancelRequested.add(key)
1068
- if (active && active.key === key && activeChild) {
1069
- const child = activeChild
1070
- try {
1071
- child.kill('SIGTERM')
1072
- } catch {
1073
- /* ignore */
1074
- }
1075
- setTimeout(() => {
1076
- try {
1077
- child.kill('SIGKILL')
1078
- } catch {
1079
- /* ignore */
1080
- }
1081
- }, 5000)
1111
+ const inMemory = active && active.key === key && activeChild
1112
+ const pid = inMemory ? activeChild.pid : job.pid
1113
+ if (stopChildByPid(pid)) {
1114
+ // 保留 cancelRequested:runJob 的 catch 看到它才会把任务标成「已取消」,
1115
+ // 否则进程被信号杀掉会被当成普通失败写成 error。
1082
1116
  upsertJob({ key, stage: 'cancelling' })
1083
1117
  return { status: 'cancelling' }
1084
1118
  }
1085
- return { status: job.status }
1119
+ // 进程已经不在了(宿主重启过、或者进程自己退了),任务状态同样要收尾,
1120
+ // 不能把 running/interrupted 留在磁盘上让界面一直转。
1121
+ cancelRequested.delete(key)
1122
+ upsertJob({ key, status: 'cancelled', stage: 'cancelled', finishedAt: Date.now(), error: '用户取消了转写', pid: null })
1123
+ flushJobs()
1124
+ return { status: 'cancelled' }
1086
1125
  }
1087
1126
 
1088
1127
  export async function getTranscriptionState(id) {
@@ -1283,11 +1322,21 @@ export async function diarizeTranscript(episode) {
1283
1322
  }
1284
1323
 
1285
1324
  export function stopAll() {
1286
- if (activeChild) {
1287
- try {
1288
- activeChild.kill('SIGTERM')
1289
- } catch {
1290
- /* ignore */
1325
+ if (activeChild) killTree(activeChild.pid)
1326
+ // 插件被禁用或热重载时走到这里:把正在跑的任务在磁盘上收尾成「已停止」,
1327
+ // 不然新实例读到的是一条永远 running 的僵尸任务,界面上点取消也没有对象。
1328
+ if (active?.key && active?.episodeId) {
1329
+ const disk = getJobFresh(active.key)
1330
+ if (disk && (disk.status === 'queued' || disk.status === 'running')) {
1331
+ upsertJob({
1332
+ key: active.key,
1333
+ status: 'cancelled',
1334
+ stage: 'cancelled',
1335
+ finishedAt: Date.now(),
1336
+ error: '转写已停止(插件重新加载),已完成的分块保留,可以重试续跑',
1337
+ pid: null,
1338
+ orphan: false,
1339
+ })
1291
1340
  }
1292
1341
  }
1293
1342
  cancelRequested.clear()
package/src/translate.js CHANGED
@@ -11,8 +11,11 @@ import { loadModelConfig, modelDir } from './models.js'
11
11
  import { translateWithLlama } from './llama.js'
12
12
  import { getSection, updateSection } from './config.js'
13
13
  import { getJob, jobKey, upsertJob } from './jobs.js'
14
+ import { venvPython } from './platform.js'
15
+ import { artifactByDir } from './catalog.js'
14
16
 
15
- const PYTHON = path.join(DATA_DIR, 'runtime', '.venv', 'bin', 'python')
17
+ // Windows 的虚拟环境是 .venv\Scripts\python.exe,不是 .venv/bin/python。
18
+ const PYTHON = venvPython()
16
19
  const SCRIPT = path.join(path.dirname(fileURLToPath(import.meta.url)), '..', 'scripts', 'translate_local.py')
17
20
 
18
21
  export function loadConfig() {
@@ -28,7 +31,10 @@ export function currentLocalModel() {
28
31
  const dir = modelDir(name)
29
32
  const gguf = fs.existsSync(dir) ? fs.readdirSync(dir).find((file) => file.endsWith('.gguf')) : null
30
33
  const mlx = fs.existsSync(path.join(dir, 'model.safetensors'))
31
- return { name, dir, ready: Boolean(mlx || gguf), engine: mlx ? 'mlx' : gguf ? 'llama' : 'none' }
34
+ // 同样以 catalog 元数据为准,别靠文件存在与否猜引擎(MLX 与 llama 目录都可能有两种文件)。
35
+ const artifact = artifactByDir(name, 'translate')
36
+ const engine = artifact ? (artifact.engine === 'mlx' ? 'mlx' : 'llama') : mlx ? 'mlx' : gguf ? 'llama' : 'none'
37
+ return { name, dir, ready: Boolean(mlx || gguf), engine }
32
38
  }
33
39
 
34
40
  export function publicConfig() {
package/src/xyz.js CHANGED
@@ -266,9 +266,9 @@ export function logout() {
266
266
 
267
267
  // 小宇宙桌面版是 iOS App(Apple Silicon 上直跑),登录态存在共享容器的偏好设置 plist 里。
268
268
  // 这样用户只要在小宇宙 App 里登录过,就能一键把登录态搬过来,不必再开网页、按 F12 抄 header。
269
- function appPlistPath() {
269
+ function appPlistPath(home = os.homedir()) {
270
270
  return path.join(
271
- os.homedir(),
271
+ home,
272
272
  'Library',
273
273
  'Group Containers',
274
274
  'group.app.podcast.cosmos',
@@ -280,9 +280,26 @@ function appPlistPath() {
280
280
 
281
281
  function readAppPlistText(file) {
282
282
  const { execFileSync } = childProcess
283
- return execFileSync('/usr/bin/plutil', ['-convert', 'xml1', '-o', '-', file], {
284
- maxBuffer: 16 * 1024 * 1024,
285
- }).toString('utf8')
283
+ try {
284
+ return execFileSync('/usr/bin/plutil', ['-convert', 'xml1', '-o', '-', file], {
285
+ maxBuffer: 16 * 1024 * 1024,
286
+ }).toString('utf8')
287
+ } catch (error) {
288
+ // 没有 plutil(非 macOS)或 plutil 打不开时,文件本身可能已经是 XML plist——直接当文本读。
289
+ // 这样这条链路在没有 plutil 的环境里也能被测到,而不是只能靠「macOS 上人工点一下」。
290
+ if (error?.code !== 'ENOENT') {
291
+ try {
292
+ const raw = fs.readFileSync(file, 'utf8')
293
+ if (raw.includes('<plist')) return raw
294
+ } catch {
295
+ /* 落到下面统一抛错 */
296
+ }
297
+ } else {
298
+ const raw = fs.readFileSync(file, 'utf8')
299
+ if (raw.includes('<plist')) return raw
300
+ }
301
+ throw error
302
+ }
286
303
  }
287
304
 
288
305
  function plistString(xml, key) {
@@ -291,16 +308,17 @@ function plistString(xml, key) {
291
308
  return match ? match[1].trim() : ''
292
309
  }
293
310
 
294
- export function localAppStatus() {
295
- const platform = process.platform
311
+ // options 只给测试用(模拟 darwin + 假 HOME),宿主调用时不传。
312
+ export function localAppStatus({ platform = process.platform, home = os.homedir() } = {}) {
296
313
  if (platform !== 'darwin') {
297
- return { available: false, reason: platform === 'win32' ? '仅支持 macOS 上的小宇宙 App' : '仅支持 macOS 上的小宇宙 App' }
314
+ return { available: false, reason: '仅支持 macOS 上的小宇宙 App' }
298
315
  }
299
- if (!fs.existsSync(appPlistPath())) {
316
+ const file = appPlistPath(home)
317
+ if (!fs.existsSync(file)) {
300
318
  return { available: false, reason: '没有找到小宇宙 App 的登录数据,请先打开小宇宙并登录一次' }
301
319
  }
302
320
  try {
303
- const xml = readAppPlistText(appPlistPath())
321
+ const xml = readAppPlistText(file)
304
322
  const accessToken = plistString(xml, 'default-apiAccessToken')
305
323
  const refreshToken = plistString(xml, 'default-apiRefreshToken')
306
324
  const deviceId = plistString(xml, 'app.podcast.cosmos.deviceId')
@@ -313,10 +331,10 @@ export function localAppStatus() {
313
331
  }
314
332
  }
315
333
 
316
- export async function loginFromLocalApp() {
317
- const state = localAppStatus()
334
+ export async function loginFromLocalApp(options = {}) {
335
+ const state = localAppStatus(options)
318
336
  if (!state.available) throw new Error(state.reason)
319
- const xml = readAppPlistText(appPlistPath())
337
+ const xml = readAppPlistText(appPlistPath(options.home ?? os.homedir()))
320
338
  const appAccess = plistString(xml, 'default-apiAccessToken')
321
339
  const appRefresh = plistString(xml, 'default-apiRefreshToken')
322
340
  const appDevice = plistString(xml, 'app.podcast.cosmos.deviceId')