dsh-podwave 0.0.0-stage → 0.10.19

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/setup.js ADDED
@@ -0,0 +1,539 @@
1
+ // 安装中心:扫描本机、推荐模型、下载环境与模型,并把结果写进任务记录。
2
+ import fs from 'node:fs'
3
+ import fsp from 'node:fs/promises'
4
+ import os from 'node:os'
5
+ import path from 'node:path'
6
+ import { execFile, spawn } from 'node:child_process'
7
+ import { DATA_DIR } from './state.js'
8
+ import { getSection, updateSection } from './config.js'
9
+ import { getJob, upsertJob } from './jobs.js'
10
+ import { CATALOG, FFMPEG_BINARY, LLAMA_BINARY, SOURCE_LABELS, artifactFor, ffmpegAsset, llamaAsset, sourcesFor } from './catalog.js'
11
+ import { downloadArtifact, extractArchive } from './downloader.js'
12
+ import { detectPlatform, findExecutable, ffmpegPath, ffprobePath, recommendEngine, venvDir, venvPython, isWindows, isMac } from './platform.js'
13
+
14
+ const RUNTIME_DIR = path.join(DATA_DIR, 'runtime')
15
+ const SETUP_PATH = path.join(DATA_DIR, 'setup.json')
16
+ const UV_VERSION = '0.11.5'
17
+ const JOB_KEY = 'setup'
18
+
19
+ let current = null
20
+
21
+ export function setupJob() {
22
+ const stored = getJob('setup')
23
+ if (stored) return { ...stored, running: Boolean(current?.running) }
24
+ return current ? { status: 'running', running: true, stage: '检查环境' } : null
25
+ }
26
+
27
+ export function cancelSetup() {
28
+ if (!current) return false
29
+ current.cancelled = true
30
+ current.child?.kill('SIGTERM')
31
+ return true
32
+ }
33
+
34
+ function readSetup() {
35
+ try {
36
+ return JSON.parse(fs.readFileSync(SETUP_PATH, 'utf8'))
37
+ } catch {
38
+ return {}
39
+ }
40
+ }
41
+
42
+ function writeSetup(patch) {
43
+ const next = { ...readSetup(), ...patch, updatedAt: Date.now() }
44
+ fs.mkdirSync(DATA_DIR, { recursive: true })
45
+ fs.writeFileSync(SETUP_PATH, JSON.stringify(next, null, 2), { mode: 0o600 })
46
+ return next
47
+ }
48
+
49
+ function venvSitePackages() {
50
+ const dir = venvDir()
51
+ if (isWindows) return path.join(dir, 'Lib', 'site-packages')
52
+ try {
53
+ const lib = path.join(dir, 'lib')
54
+ const versions = fs.readdirSync(lib).filter((name) => name.startsWith('python'))
55
+ if (versions.length) return path.join(lib, versions[0], 'site-packages')
56
+ } catch {
57
+ // fall through
58
+ }
59
+ return path.join(dir, 'lib', 'site-packages')
60
+ }
61
+
62
+ export function pythonModule(moduleName) {
63
+ return fs.existsSync(path.join(venvSitePackages(), moduleName))
64
+ }
65
+
66
+ function findUv() {
67
+ return process.env.PODWAVE_UV || findExecutable('uv', [path.join(os.homedir(), '.local', 'bin'), path.join(os.homedir(), '.cargo', 'bin'), path.join(RUNTIME_DIR, 'bin')])
68
+ }
69
+
70
+ export function uvAssetName(platform) {
71
+ const target = uvTarget(platform)
72
+ return platform.os === 'win32' ? `uv-${target}.zip` : `uv-${target}.tar.gz`
73
+ }
74
+
75
+ function uvTarget(platform) {
76
+ if (platform.os === 'darwin') return platform.arch === 'arm64' ? 'aarch64-apple-darwin' : 'x86_64-apple-darwin'
77
+ if (platform.os === 'win32') return platform.arch === 'arm64' ? 'aarch64-pc-windows-msvc' : 'x86_64-pc-windows-msvc'
78
+ return platform.arch === 'arm64' ? 'aarch64-unknown-linux-gnu' : 'x86_64-unknown-linux-gnu'
79
+ }
80
+
81
+ async function ensureUv(platform, source, onProgress) {
82
+ const existing = findUv()
83
+ if (existing) return existing
84
+ const asset = uvAssetName(platform)
85
+ const url = `https://github.com/astral-sh/uv/releases/download/${UV_VERSION}/${asset}`
86
+ const urls = source === 'cn' ? [`https://gh-proxy.com/${url}`] : source === 'intl' ? [url] : [url, `https://gh-proxy.com/${url}`]
87
+ const binDir = path.join(RUNTIME_DIR, 'bin')
88
+ await fsp.mkdir(binDir, { recursive: true })
89
+ const { extractArchive } = await import('./downloader.js')
90
+ for (const chosen of urls) {
91
+ try {
92
+ await downloadArtifact(
93
+ {
94
+ id: 'uv',
95
+ dir: '../runtime/bin',
96
+ source: { type: 'github-assets', assets: [{ url: chosen, file: asset }] },
97
+ approxBytes: 30_000_000,
98
+ },
99
+ { sourceId: 'intl', onProgress, signal: current?.signal },
100
+ )
101
+ const archivePath = path.join(binDir, asset)
102
+ if (fs.existsSync(archivePath)) {
103
+ await extractArchive(archivePath, binDir)
104
+ await fsp.rm(archivePath, { force: true })
105
+ }
106
+ if (findUv()) break
107
+ } catch {
108
+ // 换下一条线路
109
+ }
110
+ }
111
+ const found = findUv()
112
+ if (!found) throw new Error('uv 安装失败,请手动安装 uv 后重试(https://docs.astral.sh/uv/)')
113
+ return found
114
+ }
115
+
116
+ function run(cmd, args, { cwd, env, onLine } = {}) {
117
+ return new Promise((resolve, reject) => {
118
+ const child = spawn(cmd, args, { cwd, env: { ...process.env, ...env }, stdio: ['ignore', 'pipe', 'pipe'] })
119
+ if (current) current.child = child
120
+ let out = ''
121
+ const handle = (chunk) => {
122
+ const text = chunk.toString()
123
+ out += text
124
+ onLine?.(text)
125
+ }
126
+ child.stdout.on('data', handle)
127
+ child.stderr.on('data', handle)
128
+ child.on('error', reject)
129
+ child.on('exit', (code) => {
130
+ if (current) current.child = null
131
+ if (code === 0) resolve(out)
132
+ else reject(new Error(`${path.basename(cmd)} 退出码 ${code}:${out.slice(-400)}`))
133
+ })
134
+ })
135
+ }
136
+
137
+ export function runtimeReady(platform) {
138
+ const py = venvPython()
139
+ const needsMlx = platform.mlxCapable
140
+ return (
141
+ fs.existsSync(py) &&
142
+ pythonModule('sherpa_onnx') &&
143
+ (!needsMlx || (pythonModule('mlx_qwen3_asr') && pythonModule('mlx_lm')))
144
+ )
145
+ }
146
+
147
+ export function llamaBinary(platform) {
148
+ if (process.env.PODWAVE_LLAMA && fs.existsSync(process.env.PODWAVE_LLAMA)) return process.env.PODWAVE_LLAMA
149
+ const direct = findExecutable('llama-cli')
150
+ if (direct) return direct
151
+ const root = path.join(RUNTIME_DIR, 'llama')
152
+ const name = isWindows ? 'llama-cli.exe' : 'llama-cli'
153
+ const stack = [root]
154
+ while (stack.length) {
155
+ const dir = stack.pop()
156
+ let entries = []
157
+ try {
158
+ entries = fs.readdirSync(dir, { withFileTypes: true })
159
+ } catch {
160
+ continue
161
+ }
162
+ for (const entry of entries) {
163
+ const full = path.join(dir, entry.name)
164
+ if (entry.isDirectory()) stack.push(full)
165
+ else if (entry.name === name) return full
166
+ }
167
+ }
168
+ return null
169
+ }
170
+
171
+ export function modelState(artifact) {
172
+ const dir = path.join(DATA_DIR, 'models', artifact.dir)
173
+ if (artifact.kind === 'diarization') {
174
+ const seg = path.join(dir, 'sherpa-onnx-pyannote-segmentation-3-0', 'model.onnx')
175
+ const emb = path.join(dir, '3dspeaker_speech_campplus_sv_zh_en_16k-common_advanced.onnx')
176
+ return { ready: fs.existsSync(seg) && fs.existsSync(emb), dir }
177
+ }
178
+ if (artifact.file) {
179
+ return { ready: fs.existsSync(path.join(dir, artifact.file)), dir }
180
+ }
181
+ const safetensors = path.join(dir, 'model.safetensors')
182
+ const onnx = path.join(dir, 'model.int8.onnx')
183
+ return { ready: fs.existsSync(safetensors) || fs.existsSync(onnx), dir }
184
+ }
185
+
186
+ function runCapture(cmd, args, timeout = 15000) {
187
+ return new Promise((resolve) => {
188
+ execFile(cmd, args, { timeout, windowsHide: true }, (error, stdout, stderr) => {
189
+ const output = String(stdout || '').trim() || String(stderr || '').trim()
190
+ resolve({
191
+ ok: !error,
192
+ detail: output ? output.split('\n')[0].slice(0, 180) : String(error?.message || '').slice(0, 180),
193
+ })
194
+ })
195
+ })
196
+ }
197
+
198
+ export async function verify() {
199
+ const platform = detectPlatform()
200
+ const engine = recommendEngine(platform)
201
+ const artifacts = artifactFor(platform, engine)
202
+ const checks = []
203
+
204
+ const ffmpeg = ffmpegPath()
205
+ checks.push({ id: 'ffmpeg', label: 'ffmpeg 已找到', ok: Boolean(ffmpeg), detail: ffmpeg || '未找到' })
206
+ if (ffmpeg) {
207
+ const result = await runCapture(ffmpeg, ['-version'])
208
+ checks.push({ id: 'ffmpeg-run', label: 'ffmpeg 可执行', ok: result.ok, detail: result.detail })
209
+ }
210
+ const ffprobe = ffprobePath()
211
+ checks.push({ id: 'ffprobe', label: 'ffprobe 已找到', ok: Boolean(ffprobe), detail: ffprobe || '未找到' })
212
+ if (ffprobe) {
213
+ const result = await runCapture(ffprobe, ['-version'])
214
+ checks.push({ id: 'ffprobe-run', label: 'ffprobe 可执行', ok: result.ok, detail: result.detail })
215
+ }
216
+
217
+ const python = venvPython()
218
+ if (fs.existsSync(python)) {
219
+ checks.push({ id: 'python', label: 'Python 环境存在', ok: true, detail: python })
220
+ const result = await runCapture(python, ['-c', 'import sherpa_onnx, numpy; print("sherpa-onnx", getattr(sherpa_onnx, "__version__", "?"))'])
221
+ checks.push({ id: 'python-sherpa', label: 'sherpa-onnx 可导入', ok: result.ok, detail: result.detail })
222
+ if (platform.mlxCapable) {
223
+ const mlx = await runCapture(python, ['-c', 'import mlx_qwen3_asr, mlx_lm; print("mlx ok")'])
224
+ checks.push({ id: 'python-mlx', label: 'MLX 组件可导入', ok: mlx.ok, detail: mlx.detail })
225
+ }
226
+ } else {
227
+ checks.push({ id: 'python', label: 'Python 环境存在', ok: false, detail: '虚拟环境不存在,请先安装' })
228
+ }
229
+
230
+ if (engine.id !== 'mlx') {
231
+ const cli = llamaBinary(platform)
232
+ const server = cli ? path.join(path.dirname(cli), isWindows ? 'llama-server.exe' : 'llama-server') : null
233
+ checks.push({ id: 'llama', label: 'llama.cpp 已找到', ok: Boolean(server && fs.existsSync(server)), detail: server || '未找到' })
234
+ if (server && fs.existsSync(server)) {
235
+ const result = await runCapture(server, ['--version'])
236
+ checks.push({ id: 'llama-run', label: 'llama-server 可执行', ok: result.ok, detail: result.detail })
237
+ }
238
+ }
239
+
240
+ for (const artifact of [artifacts.asr, artifacts.translate, artifacts.diarization]) {
241
+ const state = modelState(artifact)
242
+ checks.push({
243
+ id: `model-${artifact.id}`,
244
+ label: `模型就绪:${artifact.label}`,
245
+ ok: state.ready,
246
+ detail: state.ready ? state.dir : artifact.note || '未下载',
247
+ })
248
+ }
249
+
250
+ const config = getSection('models')
251
+ checks.push({
252
+ id: 'config',
253
+ label: '模型配置已写入',
254
+ ok: Boolean(config.asrModel && config.translateModel),
255
+ detail: `${config.asrModel || '未设置'} / ${config.translateModel || '未设置'}`,
256
+ })
257
+
258
+ return {
259
+ ok: checks.every((check) => check.ok),
260
+ checkedAt: new Date().toISOString(),
261
+ platform,
262
+ engine,
263
+ checks,
264
+ }
265
+ }
266
+
267
+ export function plan({ source = 'auto', platform = detectPlatform() } = {}) {
268
+ const engine = recommendEngine(platform)
269
+ const artifacts = artifactFor(platform, engine)
270
+ const items = []
271
+ const ffAsset = !ffmpegPath() ? ffmpegAsset(platform) : null
272
+ if (ffAsset) {
273
+ items.push({
274
+ id: 'ffmpeg',
275
+ label: 'ffmpeg / ffprobe',
276
+ kind: 'tool',
277
+ bytes: ffAsset.bytes,
278
+ sources: ['GitHub', 'gh-proxy.com'],
279
+ })
280
+ }
281
+ if (!runtimeReady(platform)) {
282
+ items.push({
283
+ id: 'runtime',
284
+ label: 'Python 运行时 + 依赖(uv / PyPI)',
285
+ kind: 'runtime',
286
+ bytes: 80_000_000,
287
+ sources: ['PyPI 官方', '清华镜像'],
288
+ note: '按 60–100 MB 估算,实际以 pip 下载为准',
289
+ })
290
+ }
291
+ if (engine.id !== 'mlx' && !llamaBinary(platform)) {
292
+ const asset = llamaAsset(platform)
293
+ items.push({
294
+ id: 'llama',
295
+ label: 'llama.cpp 运行时',
296
+ kind: 'engine',
297
+ bytes: 25_000_000,
298
+ sources: ['GitHub', 'gh-proxy.com'],
299
+ })
300
+ void asset
301
+ }
302
+ for (const artifact of [artifacts.asr, artifacts.translate, artifacts.diarization]) {
303
+ if (modelState(artifact).ready) continue
304
+ items.push({
305
+ id: artifact.id,
306
+ label: artifact.label,
307
+ kind: 'model',
308
+ bytes: artifact.approxBytes || 0,
309
+ sources: sourcesFor(artifact.source).map((item) => item.label),
310
+ note: artifact.note,
311
+ })
312
+ }
313
+ const totalBytes = items.reduce((sum, item) => sum + (item.bytes || 0), 0)
314
+ return {
315
+ platform,
316
+ engine,
317
+ source,
318
+ items,
319
+ totalBytes,
320
+ note: engine.id === 'mlx' ? 'Apple Silicon 使用 MLX 模型;Windows / Linux 使用 ONNX + GGUF。' : 'Windows / Linux 使用 ONNX ASR + llama.cpp GGUF。',
321
+ }
322
+ }
323
+
324
+ export function scan() {
325
+ const platform = detectPlatform()
326
+ const engine = recommendEngine(platform)
327
+ const artifacts = artifactFor(platform, engine)
328
+ const pythonOk = fs.existsSync(venvPython())
329
+ const runtimeOk = runtimeReady(platform)
330
+ const llama = engine.id === 'mlx' ? null : llamaBinary(platform)
331
+ const components = []
332
+ const push = (item) => components.push(item)
333
+
334
+ const ffAsset = ffmpegAsset(platform)
335
+ push({
336
+ id: 'ffmpeg',
337
+ kind: 'tool',
338
+ label: 'ffmpeg / ffprobe',
339
+ required: true,
340
+ ready: Boolean(ffmpegPath() && ffprobePath()),
341
+ detail: ffmpegPath() || (ffAsset ? '未找到,将下载静态构建' : isMac && findExecutable('brew') ? '未找到,一键安装将通过 Homebrew 安装' : '未找到;macOS 请先 brew install ffmpeg'),
342
+ bytes: ffAsset?.bytes || 0,
343
+ })
344
+ push({
345
+ id: 'runtime',
346
+ kind: 'runtime',
347
+ label: platform.mlxCapable ? 'Python 运行时(sherpa-onnx + MLX)' : 'Python 运行时(sherpa-onnx)',
348
+ required: true,
349
+ ready: runtimeOk,
350
+ detail: pythonOk ? venvPython() : '未安装',
351
+ bytes: 0,
352
+ })
353
+ for (const artifact of [artifacts.asr, artifacts.translate, artifacts.diarization]) {
354
+ const state = modelState(artifact)
355
+ push({
356
+ id: artifact.kind,
357
+ kind: 'model',
358
+ label: artifact.label,
359
+ required: artifact.kind !== 'diarization',
360
+ ready: state.ready,
361
+ detail: state.ready ? state.dir : artifact.note,
362
+ bytes: artifact.approxBytes || 0,
363
+ artifactId: artifact.id,
364
+ })
365
+ }
366
+ if (engine.id !== 'mlx') {
367
+ const asset = llamaAsset(platform)
368
+ push({
369
+ id: 'llama',
370
+ kind: 'engine',
371
+ label: 'llama.cpp 运行时',
372
+ required: true,
373
+ ready: Boolean(llama),
374
+ detail: llama || (asset ? `${asset.name}(安装时下载)` : '该平台暂无预编译包'),
375
+ bytes: 0,
376
+ })
377
+ }
378
+ const missing = components.filter((item) => !item.ready && item.required)
379
+ const downloadBytes = components.filter((item) => item.kind === 'model' && !item.ready).reduce((sum, item) => sum + item.bytes, 0)
380
+ const disk = platform.freeDiskGB
381
+ return {
382
+ platform,
383
+ engine,
384
+ sourceLabels: SOURCE_LABELS,
385
+ components,
386
+ missing: missing.map((item) => item.id),
387
+ recommendedDownloads: components
388
+ .filter((item) => item.kind === 'model')
389
+ .map((item) => ({ id: item.id, label: item.label, bytes: item.bytes, ready: item.ready })),
390
+ downloadBytes,
391
+ diskWarning: disk != null && downloadBytes > 0 && disk * 2 ** 30 < downloadBytes * 1.25 + 1.5 * 2 ** 30,
392
+ setup: readSetup(),
393
+ }
394
+ }
395
+
396
+ function updateJob(patch) {
397
+ const job = upsertJob({
398
+ key: JOB_KEY,
399
+ type: 'setup',
400
+ label: '安装环境与模型',
401
+ status: 'running',
402
+ updatedAt: Date.now(),
403
+ ...patch,
404
+ })
405
+ return job
406
+ }
407
+
408
+ export async function install({ source = 'auto' } = {}) {
409
+ if (current?.running) throw new Error('安装已在进行中')
410
+ current = { running: true, cancelled: false, startedAt: Date.now(), child: null, signal: null }
411
+ const platform = detectPlatform()
412
+ const engine = recommendEngine(platform)
413
+ const artifacts = artifactFor(platform, engine)
414
+ const targets = [artifacts.asr, artifacts.translate, artifacts.diarization].filter((artifact) => !modelState(artifact).ready)
415
+ const ffAssetPending = !ffmpegPath() ? ffmpegAsset(platform) : null
416
+ const totalBytes =
417
+ targets.reduce((sum, artifact) => sum + (artifact.approxBytes || 0), 0) + (ffAssetPending?.bytes || 0)
418
+ let doneBytes = 0
419
+ const summary = []
420
+ updateJob({ status: 'running', stage: '检查环境', progress: 0, bytes: 0, expected: totalBytes, summary: [] })
421
+ const report = (extra) => {
422
+ const progress = totalBytes ? Math.min(99, Math.round((doneBytes / totalBytes) * 100)) : 0
423
+ updateJob({ status: 'running', ...extra, progress, bytes: doneBytes, expected: totalBytes, summary })
424
+ }
425
+
426
+ try {
427
+ // 0. ffmpeg(Windows / Linux 直接下载静态构建;macOS 优先用系统/Homebrew)
428
+ if (!ffmpegPath()) {
429
+ const ffAsset = ffmpegAsset(platform)
430
+ if (isMac) {
431
+ const brew = findExecutable('brew')
432
+ if (!brew) throw new Error('没有找到 ffmpeg,也没有 Homebrew;请安装 Homebrew 或把 ffmpeg/ffprobe 放进 PATH')
433
+ report({ stage: '通过 Homebrew 安装 ffmpeg' })
434
+ await run(brew, ['install', 'ffmpeg'])
435
+ if (!ffmpegPath()) throw new Error('Homebrew 安装后仍未找到 ffmpeg,请检查 brew 输出')
436
+ summary.push({ id: 'ffmpeg', label: 'ffmpeg / ffprobe(Homebrew)', bytes: 0 })
437
+ doneBytes += 0
438
+ report({ stage: '检查环境' })
439
+ } else if (!ffAsset) {
440
+ throw new Error('没有找到 ffmpeg:请安装 ffmpeg 或把它放进 PATH')
441
+ } else {
442
+ report({ stage: '下载 ffmpeg' })
443
+ await downloadArtifact(
444
+ {
445
+ id: 'ffmpeg',
446
+ dir: '../runtime/ffmpeg-download',
447
+ approxBytes: ffAsset.bytes,
448
+ source: { type: 'github-assets', assets: [{ url: ffAsset.url, file: ffAsset.file }] },
449
+ },
450
+ { sourceId: source, signal: current.signal, onProgress: () => {} },
451
+ )
452
+ const archive = path.join(RUNTIME_DIR, 'ffmpeg-download', ffAsset.file)
453
+ const target = path.join(RUNTIME_DIR, 'ffmpeg')
454
+ await fsp.mkdir(target, { recursive: true })
455
+ if (fs.existsSync(archive)) await extractArchive(archive, target)
456
+ doneBytes += ffAsset.bytes
457
+ summary.push({ id: 'ffmpeg', label: 'ffmpeg / ffprobe', bytes: ffAsset.bytes })
458
+ }
459
+ }
460
+
461
+ // 1. Python 运行时
462
+ if (!runtimeReady(platform)) {
463
+ report({ stage: '安装 uv 与 Python 运行时' })
464
+ const uv = await ensureUv(platform, source, () => {})
465
+ const venv = venvDir()
466
+ if (!fs.existsSync(venvPython())) {
467
+ await run(uv, ['venv', '--python', '3.12', venv])
468
+ }
469
+ const index = source === 'cn' ? 'https://pypi.tuna.tsinghua.edu.cn/simple' : undefined
470
+ const args = ['pip', 'install', '--python', venvPython(), 'sherpa-onnx', 'numpy']
471
+ if (engine.id === 'mlx') args.push('mlx-qwen3-asr', 'mlx-lm')
472
+ if (index) args.push('--index-url', index)
473
+ report({ stage: '安装 Python 依赖(sherpa-onnx' + (engine.id === 'mlx' ? '、mlx' : '') + ')' })
474
+ try {
475
+ await run(uv, args)
476
+ } catch (error) {
477
+ if (index) await run(uv, args.filter((part) => part !== '--index-url' && part !== index))
478
+ else throw error
479
+ }
480
+ summary.push({ id: 'runtime', label: 'Python 运行时 + 依赖', bytes: 0 })
481
+ }
482
+
483
+ // 2. llama.cpp 运行时
484
+ if (engine.id !== 'mlx' && !llamaBinary(platform)) {
485
+ const asset = llamaAsset(platform)
486
+ if (!asset) throw new Error('当前平台没有 llama.cpp 预编译包')
487
+ report({ stage: '下载 llama.cpp 运行时' })
488
+ await downloadArtifact(
489
+ {
490
+ id: 'llama-cpp',
491
+ dir: '../runtime/llama-download',
492
+ approxBytes: 40_000_000,
493
+ source: { type: 'github-assets', assets: [{ url: asset.url, file: asset.name }] },
494
+ },
495
+ { sourceId: source, signal: current.signal, onProgress: () => {} },
496
+ )
497
+ const { extractArchive } = await import('./downloader.js')
498
+ const archive = path.join(DATA_DIR, 'models', '..', 'runtime', 'llama-download', asset.name)
499
+ const target = path.join(RUNTIME_DIR, 'llama')
500
+ await fsp.mkdir(target, { recursive: true })
501
+ if (fs.existsSync(archive)) await extractArchive(archive, target)
502
+ summary.push({ id: 'llama', label: 'llama.cpp 运行时', bytes: 40_000_000 })
503
+ }
504
+
505
+ // 3. 模型
506
+ for (const artifact of targets) {
507
+ if (current.cancelled) throw new Error('用户取消了安装')
508
+ report({ stage: `下载 ${artifact.label}` })
509
+ const result = await downloadArtifact(artifact, {
510
+ sourceId: source,
511
+ signal: current.signal,
512
+ onProgress: (event) => {
513
+ doneBytes += event.delta || 0
514
+ report({ stage: `下载 ${artifact.label}` })
515
+ },
516
+ })
517
+ doneBytes = Math.max(doneBytes, summary.reduce((sum, item) => sum + item.bytes, 0) + result.bytes)
518
+ summary.push({ id: artifact.id, label: artifact.label, bytes: result.bytes, source: result.sourceLabel })
519
+ }
520
+
521
+ // 4. 写配置
522
+ updateSection('models', { asrModel: artifacts.asr.dir, translateModel: artifacts.translate.dir })
523
+ writeSetup({ engine: engine.id, source, installedAt: Date.now(), lastSummary: summary })
524
+ const job = updateJob({ status: 'done', stage: '完成', progress: 100, bytes: doneBytes, expected: totalBytes || doneBytes, summary })
525
+ current.running = false
526
+ return job
527
+ } catch (error) {
528
+ current.running = false
529
+ const job = updateJob({
530
+ status: 'error',
531
+ stage: '失败',
532
+ error: String(error.message || error),
533
+ summary,
534
+ })
535
+ return job
536
+ }
537
+ }
538
+
539
+ export { CATALOG, LLAMA_BINARY, sourcesFor }
package/src/state.js ADDED
@@ -0,0 +1,111 @@
1
+ import fs from 'node:fs'
2
+ import fsp from 'node:fs/promises'
3
+ import path from 'node:path'
4
+ import os from 'node:os'
5
+
6
+ // 允许用 PODWAVE_DATA_DIR 覆盖数据目录(测试、便携安装或自定义位置)。
7
+ export const DATA_DIR = process.env.PODWAVE_DATA_DIR
8
+ ? path.resolve(process.env.PODWAVE_DATA_DIR)
9
+ : path.join(os.homedir(), '.podwave')
10
+ export const COVERS_DIR = path.join(DATA_DIR, 'covers')
11
+ const STATE_FILE = path.join(DATA_DIR, 'state.json')
12
+
13
+ const DEFAULT_STATE = {
14
+ folders: [],
15
+ progress: {},
16
+ favorites: [],
17
+ hidden: [],
18
+ settings: { rate: 1, volume: 0.9, lastEpisodeId: null },
19
+ }
20
+
21
+ let state = structuredClone(DEFAULT_STATE)
22
+ let saveTimer = null
23
+ let saveChain = Promise.resolve()
24
+
25
+ export async function initState() {
26
+ await fsp.mkdir(DATA_DIR, { recursive: true })
27
+ await fsp.mkdir(COVERS_DIR, { recursive: true })
28
+ try {
29
+ const raw = await fsp.readFile(STATE_FILE, 'utf8')
30
+ const parsed = JSON.parse(raw)
31
+ state = {
32
+ ...structuredClone(DEFAULT_STATE),
33
+ ...parsed,
34
+ settings: { ...DEFAULT_STATE.settings, ...(parsed.settings || {}) },
35
+ }
36
+ } catch {
37
+ // First run, or the file was removed by the user.
38
+ }
39
+ return state
40
+ }
41
+
42
+ export function getState() {
43
+ return state
44
+ }
45
+
46
+ export function scheduleSave() {
47
+ if (saveTimer) clearTimeout(saveTimer)
48
+ saveTimer = setTimeout(() => {
49
+ saveTimer = null
50
+ flushState().catch((err) => console.error('[podwave] failed to save state:', err.message))
51
+ }, 250)
52
+ }
53
+
54
+ export function flushState() {
55
+ saveChain = saveChain.then(async () => {
56
+ const tmp = `${STATE_FILE}.tmp`
57
+ await fsp.writeFile(tmp, JSON.stringify(state, null, 2))
58
+ await fsp.rename(tmp, STATE_FILE)
59
+ })
60
+ return saveChain
61
+ }
62
+
63
+ export async function closeState() {
64
+ if (saveTimer) {
65
+ clearTimeout(saveTimer)
66
+ saveTimer = null
67
+ }
68
+ await flushState()
69
+ }
70
+
71
+ export function updateState(patch) {
72
+ state = { ...state, ...patch }
73
+ scheduleSave()
74
+ }
75
+
76
+ export function setProgress(id, position, duration) {
77
+ state.progress = {
78
+ ...state.progress,
79
+ [id]: { position, duration, updatedAt: Date.now() },
80
+ }
81
+ scheduleSave()
82
+ }
83
+
84
+ export function setSettings(patch) {
85
+ state.settings = { ...state.settings, ...patch }
86
+ scheduleSave()
87
+ }
88
+
89
+ export function addFolderState(folder) {
90
+ if (state.folders.includes(folder)) return
91
+ state.folders = [...state.folders, folder]
92
+ scheduleSave()
93
+ }
94
+
95
+ export function removeFolderState(folder) {
96
+ state.folders = state.folders.filter((entry) => entry !== folder)
97
+ scheduleSave()
98
+ }
99
+
100
+ export function removeProgressFor(predicate) {
101
+ const next = {}
102
+ for (const [id, value] of Object.entries(state.progress)) {
103
+ if (!predicate(id)) next[id] = value
104
+ }
105
+ state.progress = next
106
+ scheduleSave()
107
+ }
108
+
109
+ export function stateFileExists() {
110
+ return fs.existsSync(STATE_FILE)
111
+ }