dsh-podwave 0.11.20 → 0.11.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.md +6 -1
- package/dist/client.js +16 -3
- package/package.json +1 -1
- package/scripts/diarize.py +3 -1
- package/src/catalog.js +23 -0
- package/src/config.js +54 -5
- package/src/diarize.js +18 -9
- package/src/downloader.js +49 -4
- package/src/host.js +57 -7
- package/src/library.js +8 -2
- package/src/llama.js +6 -7
- package/src/models.js +215 -7
- package/src/platform.js +36 -1
- package/src/setup.js +60 -19
- package/src/transcribe.js +20 -40
- package/src/translate.js +8 -2
- package/src/xyz.js +31 -13
package/src/models.js
CHANGED
|
@@ -1,10 +1,30 @@
|
|
|
1
|
-
//
|
|
1
|
+
// 模型目录与选择:默认放 ~/.podwave/models/<name>,也可以在设置里改到任意目录
|
|
2
|
+
// (比如项目文件夹或外置硬盘),改完能把已有模型原样搬过去。
|
|
3
|
+
//
|
|
4
|
+
// 为什么不把目录写死:模型有 5 GB 量级,用户往往不想让它占家目录;
|
|
5
|
+
// 但也绝不能硬编码到「插件自己的目录」——桌面端装的是拷贝,升级会整个替换掉。
|
|
2
6
|
import fs from 'node:fs'
|
|
7
|
+
import os from 'node:os'
|
|
3
8
|
import path from 'node:path'
|
|
4
9
|
import { DATA_DIR } from './state.js'
|
|
5
10
|
import { getSection, updateSection } from './config.js'
|
|
11
|
+
import { CATALOG } from './catalog.js'
|
|
6
12
|
|
|
7
|
-
export const
|
|
13
|
+
export const DEFAULT_MODELS_DIR = path.join(DATA_DIR, 'models')
|
|
14
|
+
|
|
15
|
+
// 把 ~ / ~/x / %VAR% 解析成绝对路径;空串返回 ''。只接受绝对路径或 ~ 开头。
|
|
16
|
+
export function expandPath(input) {
|
|
17
|
+
let value = String(input || '').trim()
|
|
18
|
+
if (!value) return ''
|
|
19
|
+
value = value.replace(/%([^%\\/]+)%/g, (whole, name) => {
|
|
20
|
+
const resolved = process.env[name] || process.env[name.toUpperCase()] || process.env[name.toLowerCase()]
|
|
21
|
+
return resolved || whole
|
|
22
|
+
})
|
|
23
|
+
if (value === '~') return os.homedir()
|
|
24
|
+
if (value.startsWith('~/') || value.startsWith('~\\')) return path.join(os.homedir(), value.slice(2))
|
|
25
|
+
if (!path.isAbsolute(value)) throw new Error('请填写绝对路径,例如 ~/Models/podwave 或 /Volumes/SSD/models')
|
|
26
|
+
return path.normalize(value)
|
|
27
|
+
}
|
|
8
28
|
|
|
9
29
|
export function loadModelConfig() {
|
|
10
30
|
return getSection('models')
|
|
@@ -14,8 +34,19 @@ export function saveModelConfig(patch) {
|
|
|
14
34
|
return updateSection('models', patch)
|
|
15
35
|
}
|
|
16
36
|
|
|
37
|
+
// 当前生效的模型根目录(用户改过就用用户的,否则用默认)。
|
|
38
|
+
export function modelsDir() {
|
|
39
|
+
try {
|
|
40
|
+
const custom = expandPath(loadModelConfig()?.dir)
|
|
41
|
+
if (custom) return custom
|
|
42
|
+
} catch {
|
|
43
|
+
/* 配置里写了非法路径时退回默认,不能让整个插件起不来 */
|
|
44
|
+
}
|
|
45
|
+
return DEFAULT_MODELS_DIR
|
|
46
|
+
}
|
|
47
|
+
|
|
17
48
|
export function modelDir(name) {
|
|
18
|
-
return path.join(
|
|
49
|
+
return path.join(modelsDir(), String(name || ''))
|
|
19
50
|
}
|
|
20
51
|
|
|
21
52
|
export function modelReady(name) {
|
|
@@ -24,23 +55,200 @@ export function modelReady(name) {
|
|
|
24
55
|
if (!fs.existsSync(dir)) return false
|
|
25
56
|
if (fs.existsSync(path.join(dir, 'model.safetensors'))) return true
|
|
26
57
|
if (fs.existsSync(path.join(dir, 'model.int8.onnx'))) return true
|
|
27
|
-
|
|
58
|
+
// 说话人分离模型(pyannote + 3D-Speaker)就是裸 onnx 文件,
|
|
59
|
+
// 只认 safetensors/int8/gguf 会让它永远显示「未就绪」。
|
|
60
|
+
return fs.readdirSync(dir).some((file) => file.endsWith('.gguf') || file.endsWith('.onnx'))
|
|
28
61
|
} catch {
|
|
29
62
|
return false
|
|
30
63
|
}
|
|
31
64
|
}
|
|
32
65
|
|
|
66
|
+
// 目录体积:模型目录文件数很少(几十个),直接递归统计即可。
|
|
67
|
+
export function dirBytes(dir) {
|
|
68
|
+
let total = 0
|
|
69
|
+
const stack = [dir]
|
|
70
|
+
while (stack.length) {
|
|
71
|
+
const current = stack.pop()
|
|
72
|
+
let entries
|
|
73
|
+
try {
|
|
74
|
+
entries = fs.readdirSync(current, { withFileTypes: true })
|
|
75
|
+
} catch {
|
|
76
|
+
continue
|
|
77
|
+
}
|
|
78
|
+
for (const entry of entries) {
|
|
79
|
+
const full = path.join(current, entry.name)
|
|
80
|
+
if (entry.isDirectory()) {
|
|
81
|
+
stack.push(full)
|
|
82
|
+
} else if (entry.isFile()) {
|
|
83
|
+
try {
|
|
84
|
+
total += fs.statSync(full).size
|
|
85
|
+
} catch {
|
|
86
|
+
/* 跳过读不到的项 */
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return total
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// 目录名 → { kind, label },用于在设置页说清「这个是干什么用的」。
|
|
95
|
+
function artifactMeta() {
|
|
96
|
+
const map = new Map()
|
|
97
|
+
for (const artifact of Object.values(CATALOG)) {
|
|
98
|
+
if (artifact?.dir && !artifact.dir.includes('..')) map.set(artifact.dir, { kind: artifact.kind, label: artifact.label })
|
|
99
|
+
}
|
|
100
|
+
return map
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const KIND_LABELS = { asr: '转写', translate: '翻译', diarization: '说话人分离' }
|
|
104
|
+
|
|
33
105
|
export function listModels() {
|
|
106
|
+
const root = modelsDir()
|
|
34
107
|
let names = []
|
|
35
108
|
try {
|
|
36
109
|
names = fs
|
|
37
|
-
.readdirSync(
|
|
110
|
+
.readdirSync(root, { withFileTypes: true })
|
|
38
111
|
.filter((entry) => entry.isDirectory())
|
|
39
112
|
.map((entry) => entry.name)
|
|
40
113
|
} catch {
|
|
41
114
|
names = []
|
|
42
115
|
}
|
|
116
|
+
const config = loadModelConfig()
|
|
117
|
+
const meta = artifactMeta()
|
|
43
118
|
return names
|
|
44
|
-
.map((name) =>
|
|
45
|
-
|
|
119
|
+
.map((name) => {
|
|
120
|
+
const info = meta.get(name)
|
|
121
|
+
const kind = info?.kind || ''
|
|
122
|
+
const inUse = name === config.asrModel || name === config.translateModel || kind === 'diarization'
|
|
123
|
+
return {
|
|
124
|
+
name,
|
|
125
|
+
ready: modelReady(name),
|
|
126
|
+
bytes: dirBytes(modelDir(name)),
|
|
127
|
+
kind,
|
|
128
|
+
kindLabel: KIND_LABELS[kind] || '其它',
|
|
129
|
+
label: info?.label || name,
|
|
130
|
+
inUse,
|
|
131
|
+
}
|
|
132
|
+
})
|
|
133
|
+
.sort((a, b) => b.bytes - a.bytes || a.name.localeCompare(b.name))
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export function modelsSummary() {
|
|
137
|
+
const items = listModels()
|
|
138
|
+
const totalBytes = items.reduce((sum, item) => sum + item.bytes, 0)
|
|
139
|
+
return { dir: modelsDir(), defaultDir: DEFAULT_MODELS_DIR, custom: modelsDir() !== DEFAULT_MODELS_DIR, totalBytes, items }
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export function removeModel(name) {
|
|
143
|
+
const target = String(name || '').trim()
|
|
144
|
+
if (!target) throw new Error('缺少模型名')
|
|
145
|
+
if (target.includes('/') || target.includes('\\') || target === '.' || target === '..') throw new Error('模型名不合法')
|
|
146
|
+
const item = listModels().find((model) => model.name === target)
|
|
147
|
+
if (!item) throw new Error(`没有找到模型目录:${target}`)
|
|
148
|
+
if (item.inUse) throw new Error(`${item.label} 正在使用中,先在设置里换掉再删除`)
|
|
149
|
+
fs.rmSync(modelDir(target), { recursive: true, force: true })
|
|
150
|
+
return modelsSummary()
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// 迁移状态:跨卷复制很慢,前端轮询这个看进度。
|
|
154
|
+
let moveState = null
|
|
155
|
+
|
|
156
|
+
export function modelsMoveState() {
|
|
157
|
+
return moveState
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
function moveOne(from, to, onBytes) {
|
|
161
|
+
try {
|
|
162
|
+
fs.renameSync(from, to)
|
|
163
|
+
onBytes(dirBytes(to))
|
|
164
|
+
return 'moved'
|
|
165
|
+
} catch (error) {
|
|
166
|
+
if (error?.code !== 'EXDEV' && error?.code !== 'EPERM') throw error
|
|
167
|
+
}
|
|
168
|
+
// 跨卷:只能真复制,逐文件累计进度。
|
|
169
|
+
const stack = [[from, to]]
|
|
170
|
+
let bytes = 0
|
|
171
|
+
while (stack.length) {
|
|
172
|
+
const [src, dest] = stack.pop()
|
|
173
|
+
fs.mkdirSync(dest, { recursive: true })
|
|
174
|
+
for (const entry of fs.readdirSync(src, { withFileTypes: true })) {
|
|
175
|
+
const s = path.join(src, entry.name)
|
|
176
|
+
const d = path.join(dest, entry.name)
|
|
177
|
+
if (entry.isDirectory()) {
|
|
178
|
+
stack.push([s, d])
|
|
179
|
+
} else if (entry.isFile()) {
|
|
180
|
+
fs.copyFileSync(s, d)
|
|
181
|
+
bytes += fs.statSync(d).size
|
|
182
|
+
onBytes(bytes)
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
fs.rmSync(from, { recursive: true, force: true })
|
|
187
|
+
return 'copied'
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// 改模型目录。move=true 时把已有模型搬过去(同卷 rename 秒完成,跨卷复制)。
|
|
191
|
+
export function setModelsDir(input, { move = true } = {}) {
|
|
192
|
+
const raw = String(input || '').trim()
|
|
193
|
+
const next = raw ? expandPath(raw) : DEFAULT_MODELS_DIR
|
|
194
|
+
const current = modelsDir()
|
|
195
|
+
if (next === current) return { ...modelsSummary(), moved: [], skipped: [] }
|
|
196
|
+
|
|
197
|
+
if (next.startsWith(current + path.sep) || current.startsWith(next + path.sep)) {
|
|
198
|
+
throw new Error('新目录不能是当前模型目录的子目录,也不能反过来包含它')
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
fs.mkdirSync(next, { recursive: true })
|
|
202
|
+
try {
|
|
203
|
+
fs.accessSync(next, fs.constants.W_OK)
|
|
204
|
+
} catch {
|
|
205
|
+
throw new Error(`这个目录不可写:${next}`)
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
const moved = []
|
|
209
|
+
const skipped = []
|
|
210
|
+
moveState = { running: Boolean(move), doneBytes: 0, totalBytes: 0, current: '' }
|
|
211
|
+
if (move) {
|
|
212
|
+
let entries = []
|
|
213
|
+
try {
|
|
214
|
+
const root = modelsDir()
|
|
215
|
+
const total = dirBytes(root)
|
|
216
|
+
moveState.totalBytes = total
|
|
217
|
+
entries = fs
|
|
218
|
+
.readdirSync(root, { withFileTypes: true })
|
|
219
|
+
.filter((entry) => entry.isDirectory())
|
|
220
|
+
.map((entry) => entry.name)
|
|
221
|
+
let done = 0
|
|
222
|
+
for (const name of entries) {
|
|
223
|
+
moveState.current = name
|
|
224
|
+
const from = path.join(root, name)
|
|
225
|
+
const to = path.join(next, name)
|
|
226
|
+
if (fs.existsSync(to)) {
|
|
227
|
+
skipped.push(name)
|
|
228
|
+
done += dirBytes(from)
|
|
229
|
+
moveState.doneBytes = done
|
|
230
|
+
continue
|
|
231
|
+
}
|
|
232
|
+
const before = done
|
|
233
|
+
moveOne(from, to, (bytes) => {
|
|
234
|
+
moveState.doneBytes = before + bytes
|
|
235
|
+
})
|
|
236
|
+
done = moveState.doneBytes
|
|
237
|
+
moved.push(name)
|
|
238
|
+
}
|
|
239
|
+
// 原目录空了就删掉,避免留下空壳
|
|
240
|
+
try {
|
|
241
|
+
if (!fs.readdirSync(root).length) fs.rmdirSync(root)
|
|
242
|
+
} catch {
|
|
243
|
+
/* ignore */
|
|
244
|
+
}
|
|
245
|
+
} catch (error) {
|
|
246
|
+
moveState = null
|
|
247
|
+
throw new Error(`迁移失败:${error.message}`)
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
moveState = null
|
|
251
|
+
|
|
252
|
+
updateSection('models', { dir: next === DEFAULT_MODELS_DIR ? '' : next })
|
|
253
|
+
return { ...modelsSummary(), moved, skipped }
|
|
46
254
|
}
|
package/src/platform.js
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
import fs from 'node:fs'
|
|
4
4
|
import os from 'node:os'
|
|
5
5
|
import path from 'node:path'
|
|
6
|
-
import { execFileSync } from 'node:child_process'
|
|
6
|
+
import { execFileSync, spawn } from 'node:child_process'
|
|
7
7
|
|
|
8
8
|
export const isWindows = process.platform === 'win32'
|
|
9
9
|
export const isMac = process.platform === 'darwin'
|
|
@@ -109,6 +109,41 @@ export function venvBin(name, dir = venvDir()) {
|
|
|
109
109
|
return isWindows ? path.join(dir, 'Scripts', name + ext) : path.join(dir, 'bin', name + ext)
|
|
110
110
|
}
|
|
111
111
|
|
|
112
|
+
// Windows 上 SIGTERM 实际上是 TerminateProcess(硬杀),子进程(ffmpeg 之类)会变成孤儿。
|
|
113
|
+
// 统一走这里:Windows 用 taskkill /T 连子孙一起收,其它平台照常 SIGTERM,超时再 SIGKILL。
|
|
114
|
+
export function killTree(pid, { signal = 'SIGTERM', timeoutMs = 5000, onForce } = {}) {
|
|
115
|
+
const target = Number(pid)
|
|
116
|
+
if (!Number.isInteger(target) || target <= 1) return false
|
|
117
|
+
try {
|
|
118
|
+
process.kill(target, 0)
|
|
119
|
+
} catch {
|
|
120
|
+
return false
|
|
121
|
+
}
|
|
122
|
+
if (isWindows) {
|
|
123
|
+
try {
|
|
124
|
+
spawn('taskkill', ['/PID', String(target), '/T', '/F'], { stdio: 'ignore', windowsHide: true })
|
|
125
|
+
} catch {
|
|
126
|
+
/* ignore */
|
|
127
|
+
}
|
|
128
|
+
return true
|
|
129
|
+
}
|
|
130
|
+
try {
|
|
131
|
+
process.kill(target, signal)
|
|
132
|
+
} catch {
|
|
133
|
+
return false
|
|
134
|
+
}
|
|
135
|
+
const timer = setTimeout(() => {
|
|
136
|
+
try {
|
|
137
|
+
process.kill(target, 'SIGKILL')
|
|
138
|
+
} catch {
|
|
139
|
+
/* 已经退出 */
|
|
140
|
+
}
|
|
141
|
+
onForce?.()
|
|
142
|
+
}, timeoutMs)
|
|
143
|
+
timer.unref?.()
|
|
144
|
+
return true
|
|
145
|
+
}
|
|
146
|
+
|
|
112
147
|
export function detectGpu() {
|
|
113
148
|
if (isAppleSilicon()) return 'metal'
|
|
114
149
|
try {
|
package/src/setup.js
CHANGED
|
@@ -7,9 +7,10 @@ import { execFile, spawn } from 'node:child_process'
|
|
|
7
7
|
import { DATA_DIR } from './state.js'
|
|
8
8
|
import { getSection, updateSection } from './config.js'
|
|
9
9
|
import { getJob, upsertJob } from './jobs.js'
|
|
10
|
-
import { CATALOG, FFMPEG_BINARY, LLAMA_BINARY, SOURCE_LABELS, artifactFor, ffmpegAsset, llamaAsset, sourcesFor } from './catalog.js'
|
|
10
|
+
import { CATALOG, FFMPEG_BINARY, LLAMA_BINARY, SOURCE_LABELS, artifactFor, expectedModels, ffmpegAsset, llamaAsset, sourcesFor } from './catalog.js'
|
|
11
|
+
import { modelsDir } from './models.js'
|
|
11
12
|
import { downloadArtifact, extractArchive } from './downloader.js'
|
|
12
|
-
import { detectPlatform, findExecutable, ffmpegPath, ffprobePath, recommendEngine, venvDir, venvPython, isWindows, isMac } from './platform.js'
|
|
13
|
+
import { detectPlatform, findExecutable, ffmpegPath, ffprobePath, killTree, recommendEngine, venvDir, venvPython, isWindows, isMac } from './platform.js'
|
|
13
14
|
|
|
14
15
|
const RUNTIME_DIR = path.join(DATA_DIR, 'runtime')
|
|
15
16
|
const SETUP_PATH = path.join(DATA_DIR, 'setup.json')
|
|
@@ -27,7 +28,8 @@ export function setupJob() {
|
|
|
27
28
|
export function cancelSetup() {
|
|
28
29
|
if (!current) return false
|
|
29
30
|
current.cancelled = true
|
|
30
|
-
|
|
31
|
+
// Windows 上 SIGTERM 等于硬杀,uv/Python 拉起的子进程会变孤儿;killTree 处理平台差异。
|
|
32
|
+
killTree(current.child?.pid)
|
|
31
33
|
return true
|
|
32
34
|
}
|
|
33
35
|
|
|
@@ -169,7 +171,7 @@ export function llamaBinary(platform) {
|
|
|
169
171
|
}
|
|
170
172
|
|
|
171
173
|
export function modelState(artifact) {
|
|
172
|
-
const dir = path.join(
|
|
174
|
+
const dir = path.join(modelsDir(), artifact.dir)
|
|
173
175
|
if (artifact.kind === 'diarization') {
|
|
174
176
|
const seg = path.join(dir, 'sherpa-onnx-pyannote-segmentation-3-0', 'model.onnx')
|
|
175
177
|
const emb = path.join(dir, '3dspeaker_speech_campplus_sv_zh_en_16k-common_advanced.onnx')
|
|
@@ -247,12 +249,22 @@ export async function verify() {
|
|
|
247
249
|
})
|
|
248
250
|
}
|
|
249
251
|
|
|
252
|
+
// 只判「非空」等于没判:Mac 专用模型名、甚至是瞎编的名字都会亮绿,
|
|
253
|
+
// 用户看到「检查通过」却在转写时报「运行环境未安装」。这里比对本平台应有的模型。
|
|
250
254
|
const config = getSection('models')
|
|
255
|
+
const expected = expectedModels(platform, engine)
|
|
256
|
+
const problems = []
|
|
257
|
+
if (config.asrModel !== expected.asrModel) {
|
|
258
|
+
problems.push(`转写模型应为 ${expected.asrModel},当前 ${config.asrModel || '未设置'}`)
|
|
259
|
+
}
|
|
260
|
+
if (config.translateModel !== expected.translateModel) {
|
|
261
|
+
problems.push(`翻译模型应为 ${expected.translateModel},当前 ${config.translateModel || '未设置'}`)
|
|
262
|
+
}
|
|
251
263
|
checks.push({
|
|
252
264
|
id: 'config',
|
|
253
|
-
label: '
|
|
254
|
-
ok:
|
|
255
|
-
detail: `${config.asrModel
|
|
265
|
+
label: '模型配置与当前平台匹配',
|
|
266
|
+
ok: problems.length === 0,
|
|
267
|
+
detail: problems.length ? problems.join(';') : `${config.asrModel} / ${config.translateModel}`,
|
|
256
268
|
})
|
|
257
269
|
|
|
258
270
|
return {
|
|
@@ -495,7 +507,7 @@ export async function install({ source = 'auto' } = {}) {
|
|
|
495
507
|
{ sourceId: source, signal: current.signal, onProgress: () => {} },
|
|
496
508
|
)
|
|
497
509
|
const { extractArchive } = await import('./downloader.js')
|
|
498
|
-
const archive = path.join(
|
|
510
|
+
const archive = path.join(RUNTIME_DIR, 'llama-download', asset.name)
|
|
499
511
|
const target = path.join(RUNTIME_DIR, 'llama')
|
|
500
512
|
await fsp.mkdir(target, { recursive: true })
|
|
501
513
|
if (fs.existsSync(archive)) await extractArchive(archive, target)
|
|
@@ -503,25 +515,54 @@ export async function install({ source = 'auto' } = {}) {
|
|
|
503
515
|
}
|
|
504
516
|
|
|
505
517
|
// 3. 模型
|
|
518
|
+
// 说话人分离是可选组件:它失败不该把已经下完的 1.6 GB 判定成整次安装失败。
|
|
519
|
+
// 之前任何一个 tar 解压失败都会让整个 install 走到 catch,用户看到「安装失败」。
|
|
520
|
+
const optionalFailures = []
|
|
506
521
|
for (const artifact of targets) {
|
|
507
522
|
if (current.cancelled) throw new Error('用户取消了安装')
|
|
523
|
+
const optional = artifact.kind === 'diarization'
|
|
508
524
|
report({ stage: `下载 ${artifact.label}` })
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
525
|
+
try {
|
|
526
|
+
const result = await downloadArtifact(artifact, {
|
|
527
|
+
sourceId: source,
|
|
528
|
+
signal: current.signal,
|
|
529
|
+
onProgress: (event) => {
|
|
530
|
+
doneBytes += event.delta || 0
|
|
531
|
+
report({ stage: `下载 ${artifact.label}` })
|
|
532
|
+
},
|
|
533
|
+
})
|
|
534
|
+
doneBytes = Math.max(doneBytes, summary.reduce((sum, item) => sum + item.bytes, 0) + result.bytes)
|
|
535
|
+
summary.push({ id: artifact.id, label: artifact.label, bytes: result.bytes, source: result.sourceLabel })
|
|
536
|
+
} catch (error) {
|
|
537
|
+
if (current.cancelled) throw new Error('用户取消了安装')
|
|
538
|
+
if (!optional) throw error
|
|
539
|
+
const message = String(error?.message || error)
|
|
540
|
+
optionalFailures.push({ id: artifact.id, label: artifact.label, error: message })
|
|
541
|
+
summary.push({ id: artifact.id, label: artifact.label, bytes: 0, optional: true, skipped: true, error: message })
|
|
542
|
+
report({ stage: `跳过可选组件:${artifact.label}(${message})` })
|
|
543
|
+
}
|
|
519
544
|
}
|
|
520
545
|
|
|
521
546
|
// 4. 写配置
|
|
522
547
|
updateSection('models', { asrModel: artifacts.asr.dir, translateModel: artifacts.translate.dir })
|
|
523
548
|
writeSetup({ engine: engine.id, source, installedAt: Date.now(), lastSummary: summary })
|
|
524
|
-
const
|
|
549
|
+
const warning = optionalFailures.length
|
|
550
|
+
? `可选组件失败,已跳过:${optionalFailures.map((item) => `${item.label}(${item.error})`).join(';')}。核心转写与翻译已就绪,可以正常使用。`
|
|
551
|
+
: null
|
|
552
|
+
// 把落盘位置写进任务,安装完成后用户能直接看到「装在哪」。
|
|
553
|
+
const dir = modelsDir()
|
|
554
|
+
const job = updateJob({
|
|
555
|
+
status: 'done',
|
|
556
|
+
stage: warning ? '完成(部分可选组件已跳过)' : '完成',
|
|
557
|
+
progress: 100,
|
|
558
|
+
bytes: doneBytes,
|
|
559
|
+
expected: totalBytes || doneBytes,
|
|
560
|
+
summary,
|
|
561
|
+
modelsDir: dir,
|
|
562
|
+
error: null,
|
|
563
|
+
warning,
|
|
564
|
+
optionalFailures,
|
|
565
|
+
})
|
|
525
566
|
current.running = false
|
|
526
567
|
return job
|
|
527
568
|
} catch (error) {
|
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
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
|
-
|
|
20
|
-
|
|
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'
|
|
@@ -32,27 +34,9 @@ const cancelRequested = new Set()
|
|
|
32
34
|
|
|
33
35
|
// 宿主热重载后,新模块实例拿不到旧实例的 activeChild,取消就杀不掉那个进程。
|
|
34
36
|
// 所以把当前工作进程的 pid 也写进 job,取消时先按内存找,找不到再按 pid 兜底。
|
|
37
|
+
// Windows 上 SIGTERM 等于硬杀,交给 killTree 用 taskkill /T 连子孙一起收。
|
|
35
38
|
function stopChildByPid(pid) {
|
|
36
|
-
|
|
37
|
-
if (!Number.isInteger(target) || target <= 1) return false
|
|
38
|
-
try {
|
|
39
|
-
process.kill(target, 0)
|
|
40
|
-
} catch {
|
|
41
|
-
return false
|
|
42
|
-
}
|
|
43
|
-
try {
|
|
44
|
-
process.kill(target, 'SIGTERM')
|
|
45
|
-
} catch {
|
|
46
|
-
return false
|
|
47
|
-
}
|
|
48
|
-
setTimeout(() => {
|
|
49
|
-
try {
|
|
50
|
-
process.kill(target, 'SIGKILL')
|
|
51
|
-
} catch {
|
|
52
|
-
/* ignore */
|
|
53
|
-
}
|
|
54
|
-
}, 5000)
|
|
55
|
-
return true
|
|
39
|
+
return killTree(pid)
|
|
56
40
|
}
|
|
57
41
|
|
|
58
42
|
function transcriptPath(id) {
|
|
@@ -66,7 +50,11 @@ function round2(value) {
|
|
|
66
50
|
export function currentAsrModel() {
|
|
67
51
|
const name = loadModelConfig().asrModel
|
|
68
52
|
const dir = modelDir(name)
|
|
69
|
-
|
|
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'
|
|
70
58
|
const ready =
|
|
71
59
|
engine === 'sherpa'
|
|
72
60
|
? fs.existsSync(path.join(dir, 'model.int8.onnx'))
|
|
@@ -86,13 +74,17 @@ export function runtimeInfo() {
|
|
|
86
74
|
const model = currentAsrModel()
|
|
87
75
|
const config = transcribeConfig()
|
|
88
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)
|
|
89
81
|
return {
|
|
90
82
|
engine: model.engine,
|
|
91
|
-
installed
|
|
83
|
+
installed,
|
|
92
84
|
modelReady: model.ready,
|
|
93
85
|
model: model.dir,
|
|
94
86
|
modelName: model.name,
|
|
95
|
-
cli:
|
|
87
|
+
cli: cliPath,
|
|
96
88
|
hotwords: config.hotwords,
|
|
97
89
|
chunkMinutes: config.chunkMinutes,
|
|
98
90
|
diarize: config.diarize,
|
|
@@ -749,13 +741,7 @@ async function runJob(item) {
|
|
|
749
741
|
const cancelled = cancelRequested.has(key)
|
|
750
742
|
if (cancelled) {
|
|
751
743
|
// 已取消就别再写 progress 把磁盘上的 cancelling 覆盖回转写中。
|
|
752
|
-
if (child)
|
|
753
|
-
try {
|
|
754
|
-
child.kill('SIGTERM')
|
|
755
|
-
} catch {
|
|
756
|
-
/* ignore */
|
|
757
|
-
}
|
|
758
|
-
}
|
|
744
|
+
if (child) killTree(child.pid)
|
|
759
745
|
return
|
|
760
746
|
}
|
|
761
747
|
const current = plan.parts.find((part) => !hasResult(resultsDir, part.index)) || null
|
|
@@ -1336,13 +1322,7 @@ export async function diarizeTranscript(episode) {
|
|
|
1336
1322
|
}
|
|
1337
1323
|
|
|
1338
1324
|
export function stopAll() {
|
|
1339
|
-
if (activeChild)
|
|
1340
|
-
try {
|
|
1341
|
-
activeChild.kill('SIGTERM')
|
|
1342
|
-
} catch {
|
|
1343
|
-
/* ignore */
|
|
1344
|
-
}
|
|
1345
|
-
}
|
|
1325
|
+
if (activeChild) killTree(activeChild.pid)
|
|
1346
1326
|
// 插件被禁用或热重载时走到这里:把正在跑的任务在磁盘上收尾成「已停止」,
|
|
1347
1327
|
// 不然新实例读到的是一条永远 running 的僵尸任务,界面上点取消也没有对象。
|
|
1348
1328
|
if (active?.key && active?.episodeId) {
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
284
|
-
|
|
285
|
-
|
|
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
|
-
|
|
295
|
-
|
|
311
|
+
// options 只给测试用(模拟 darwin + 假 HOME),宿主调用时不传。
|
|
312
|
+
export function localAppStatus({ platform = process.platform, home = os.homedir() } = {}) {
|
|
296
313
|
if (platform !== 'darwin') {
|
|
297
|
-
return { available: false, reason:
|
|
314
|
+
return { available: false, reason: '仅支持 macOS 上的小宇宙 App' }
|
|
298
315
|
}
|
|
299
|
-
|
|
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(
|
|
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')
|