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/package.json
CHANGED
package/scripts/diarize.py
CHANGED
|
@@ -42,7 +42,9 @@ def main():
|
|
|
42
42
|
parser.add_argument('--emb-model', required=True)
|
|
43
43
|
parser.add_argument('--threshold', type=float, default=0.6)
|
|
44
44
|
parser.add_argument('--num-speakers', type=int, default=-1)
|
|
45
|
-
|
|
45
|
+
# 默认走 PATH 上的 ffmpeg。之前写死 /opt/homebrew/bin/ffmpeg,
|
|
46
|
+
# 在 Windows 上是 [WinError 2] 系统找不到指定的文件。
|
|
47
|
+
parser.add_argument('--ffmpeg', default=os.environ.get('PODWAVE_FFMPEG') or 'ffmpeg')
|
|
46
48
|
args = parser.parse_args()
|
|
47
49
|
|
|
48
50
|
import sherpa_onnx as so
|
package/src/catalog.js
CHANGED
|
@@ -212,6 +212,29 @@ export function artifactFor(platform, engine) {
|
|
|
212
212
|
}
|
|
213
213
|
}
|
|
214
214
|
|
|
215
|
+
// 配置里存的是目录名(artifact.dir),不是 catalog 的 key。
|
|
216
|
+
// 靠正则猜「这是不是 SenseVoice」在换模型后必错,所以按元数据反查。
|
|
217
|
+
export function artifactByDir(dir, kind) {
|
|
218
|
+
if (!dir) return null
|
|
219
|
+
const name = String(dir)
|
|
220
|
+
for (const artifact of Object.values(CATALOG)) {
|
|
221
|
+
if (kind && artifact.kind !== kind) continue
|
|
222
|
+
if (artifact.dir === name || artifact.id === name) return artifact
|
|
223
|
+
}
|
|
224
|
+
return null
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
// 当前平台「应该用哪个模型/引擎」——配置默认值、安装检查、运行时判定都以它为准。
|
|
228
|
+
export function expectedModels(platform, engine) {
|
|
229
|
+
const artifacts = artifactFor(platform, engine)
|
|
230
|
+
return {
|
|
231
|
+
asrModel: artifacts.asr.dir,
|
|
232
|
+
translateModel: artifacts.translate.dir,
|
|
233
|
+
asrEngine: artifacts.asr.engine,
|
|
234
|
+
translateEngine: artifacts.translate.engine,
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
215
238
|
export function llamaAsset(platform) {
|
|
216
239
|
const name = LLAMA_BINARY.assets[platformKey(platform)]
|
|
217
240
|
if (!name) return null
|
package/src/config.js
CHANGED
|
@@ -1,11 +1,33 @@
|
|
|
1
1
|
// 统一配置:~/.podwave/config.json(schemaVersion 1)。
|
|
2
2
|
// 首次启动时把旧的 ui.json / translate.json / models.json 迁入,并把旧文件改名为 *.bak。
|
|
3
|
+
//
|
|
4
|
+
// 两条容易踩的坑,都在这里兜住:
|
|
5
|
+
// 1. 默认模型必须按平台算,不能写死 MLX——那套模型只有 Apple Silicon 能装。
|
|
6
|
+
// 2. 缓存不能盲信。宿主进程常驻,而「一键安装」是另一个进程写盘;如果保存时拿内存里的
|
|
7
|
+
// 旧快照整份写回,安装结果就被吞掉了。所以每次保存都先比对磁盘是否被别人改过。
|
|
3
8
|
import fs from 'node:fs'
|
|
4
9
|
import path from 'node:path'
|
|
5
10
|
import { DATA_DIR } from './state.js'
|
|
11
|
+
import { detectPlatform, recommendEngine } from './platform.js'
|
|
12
|
+
import { expectedModels } from './catalog.js'
|
|
6
13
|
|
|
7
14
|
export const CONFIG_PATH = path.join(DATA_DIR, 'config.json')
|
|
8
15
|
|
|
16
|
+
function defaultModels() {
|
|
17
|
+
const platform = detectPlatform()
|
|
18
|
+
const engine = recommendEngine(platform)
|
|
19
|
+
const models = expectedModels(platform, engine)
|
|
20
|
+
return { asrModel: models.asrModel, translateModel: models.translateModel }
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// detectPlatform() 会去探测 GPU(可能 spawn 子进程),不能放在模块顶层每次求值。
|
|
24
|
+
// 用惰性 getter + 缓存:既保证「默认模型按平台算」,又不会拖慢 import。
|
|
25
|
+
let defaultsCache = null
|
|
26
|
+
function cachedDefaultModels() {
|
|
27
|
+
if (!defaultsCache) defaultsCache = defaultModels()
|
|
28
|
+
return defaultsCache
|
|
29
|
+
}
|
|
30
|
+
|
|
9
31
|
export const CONFIG_DEFAULTS = {
|
|
10
32
|
schemaVersion: 1,
|
|
11
33
|
ui: { showSidebarPanel: true, sidebarCollapsed: false },
|
|
@@ -16,7 +38,11 @@ export const CONFIG_DEFAULTS = {
|
|
|
16
38
|
apiModel: 'deepseek-chat',
|
|
17
39
|
apiKey: '',
|
|
18
40
|
},
|
|
19
|
-
|
|
41
|
+
// 默认模型绝不能写死 MLX:那套模型 platform 是 darwin-arm64,Windows 装不了。
|
|
42
|
+
// dir 为空表示用默认目录(<数据目录>/models);用户改过则存绝对路径。
|
|
43
|
+
get models() {
|
|
44
|
+
return { dir: '', ...cachedDefaultModels() }
|
|
45
|
+
},
|
|
20
46
|
transcribe: { hotwords: [], chunkMinutes: 1, diarize: true },
|
|
21
47
|
import: { mode: 'copy' },
|
|
22
48
|
}
|
|
@@ -48,6 +74,16 @@ function readJson(file) {
|
|
|
48
74
|
}
|
|
49
75
|
}
|
|
50
76
|
|
|
77
|
+
// 磁盘指纹:别的进程改过 config.json 时,缓存必须失效。
|
|
78
|
+
function statKey() {
|
|
79
|
+
try {
|
|
80
|
+
const stat = fs.statSync(CONFIG_PATH)
|
|
81
|
+
return `${stat.mtimeMs}:${stat.size}`
|
|
82
|
+
} catch {
|
|
83
|
+
return 'missing'
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
51
87
|
function writeConfig(config) {
|
|
52
88
|
fs.mkdirSync(DATA_DIR, { recursive: true })
|
|
53
89
|
const tmp = `${CONFIG_PATH}.tmp`
|
|
@@ -74,19 +110,24 @@ function migrateLegacy() {
|
|
|
74
110
|
}
|
|
75
111
|
|
|
76
112
|
let cache = null
|
|
113
|
+
let cacheKey = null
|
|
77
114
|
|
|
78
115
|
export function loadConfig() {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
116
|
+
const key = statKey()
|
|
117
|
+
if (cache && cacheKey === key) return clone(cache)
|
|
118
|
+
const stored = key === 'missing' ? null : readJson(CONFIG_PATH)
|
|
119
|
+
cache = stored ? merge(clone(CONFIG_DEFAULTS), stored) : migrateLegacy()
|
|
120
|
+
cacheKey = statKey()
|
|
83
121
|
return clone(cache)
|
|
84
122
|
}
|
|
85
123
|
|
|
86
124
|
export function saveConfig(patch) {
|
|
125
|
+
// 先 loadConfig():它会比对磁盘指纹,把别的进程(比如安装器)刚写进去的模型名读进来,
|
|
126
|
+
// 再和这次 patch 合并,避免用旧快照覆盖安装结果。
|
|
87
127
|
const next = merge(loadConfig(), patch)
|
|
88
128
|
cache = next
|
|
89
129
|
writeConfig(next)
|
|
130
|
+
cacheKey = statKey()
|
|
90
131
|
return clone(next)
|
|
91
132
|
}
|
|
92
133
|
|
|
@@ -101,3 +142,11 @@ export function updateSection(name, patch) {
|
|
|
101
142
|
export function configFile() {
|
|
102
143
|
return CONFIG_PATH
|
|
103
144
|
}
|
|
145
|
+
|
|
146
|
+
// 测试与诊断用:当前平台推荐的模型组合。
|
|
147
|
+
export function platformDefaults() {
|
|
148
|
+
const platform = detectPlatform()
|
|
149
|
+
const engine = recommendEngine(platform)
|
|
150
|
+
const models = expectedModels(platform, engine)
|
|
151
|
+
return { platform: { os: platform.os, arch: platform.arch, osLabel: platform.osLabel }, engine: engine.id, models }
|
|
152
|
+
}
|
package/src/diarize.js
CHANGED
|
@@ -3,31 +3,40 @@ import fs from 'node:fs'
|
|
|
3
3
|
import path from 'node:path'
|
|
4
4
|
import { spawn } from 'node:child_process'
|
|
5
5
|
import { fileURLToPath } from 'node:url'
|
|
6
|
-
import { DATA_DIR } from './state.js'
|
|
7
6
|
import { ffmpegPath, venvPython } from './platform.js'
|
|
7
|
+
import { modelsDir } from './models.js'
|
|
8
8
|
|
|
9
9
|
const SCRIPT = path.join(path.dirname(fileURLToPath(import.meta.url)), '..', 'scripts', 'diarize.py')
|
|
10
10
|
const PYTHON = venvPython()
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
export
|
|
11
|
+
|
|
12
|
+
// 模型目录可配置,所以这些路径必须每次现算,不能在模块顶层固化成常量。
|
|
13
|
+
export function diarizationPaths() {
|
|
14
|
+
const dir = path.join(modelsDir(), 'diarization')
|
|
15
|
+
return {
|
|
16
|
+
dir,
|
|
17
|
+
segModel: path.join(dir, 'sherpa-onnx-pyannote-segmentation-3-0', 'model.onnx'),
|
|
18
|
+
embModel: path.join(dir, '3dspeaker_speech_campplus_sv_zh_en_16k-common_advanced.onnx'),
|
|
19
|
+
}
|
|
20
|
+
}
|
|
14
21
|
|
|
15
22
|
export function diarizeInfo() {
|
|
23
|
+
const { segModel, embModel } = diarizationPaths()
|
|
16
24
|
return {
|
|
17
25
|
installed: fs.existsSync(PYTHON) && fs.existsSync(SCRIPT),
|
|
18
|
-
modelsReady: fs.existsSync(
|
|
19
|
-
segModel
|
|
20
|
-
embModel
|
|
26
|
+
modelsReady: fs.existsSync(segModel) && fs.existsSync(embModel),
|
|
27
|
+
segModel,
|
|
28
|
+
embModel,
|
|
21
29
|
}
|
|
22
30
|
}
|
|
23
31
|
|
|
24
32
|
export function runDiarization(audioPath, { threshold = 0.6, numSpeakers = -1 } = {}) {
|
|
25
33
|
return new Promise((resolve, reject) => {
|
|
34
|
+
const { segModel, embModel } = diarizationPaths()
|
|
26
35
|
const args = [
|
|
27
36
|
SCRIPT,
|
|
28
37
|
'--audio', audioPath,
|
|
29
|
-
'--seg-model',
|
|
30
|
-
'--emb-model',
|
|
38
|
+
'--seg-model', segModel,
|
|
39
|
+
'--emb-model', embModel,
|
|
31
40
|
'--threshold', String(threshold),
|
|
32
41
|
]
|
|
33
42
|
if (Number(numSpeakers) > 0) args.push('--num-speakers', String(numSpeakers))
|
package/src/downloader.js
CHANGED
|
@@ -94,6 +94,10 @@ function runCapture(cmd, args) {
|
|
|
94
94
|
})
|
|
95
95
|
}
|
|
96
96
|
|
|
97
|
+
// 非 zip 压缩包(.tar.bz2 / .tar.gz / .tar.xz)的条目列表。
|
|
98
|
+
// GNU tar(Git for Windows、MSYS、Cygwin 会把它的目录排到 PATH 前面)会把 C:/... 当成
|
|
99
|
+
// host:path 远程语法,直接 `tar -tf C:/...` 会报 "Cannot connect to C: resolve failed"。
|
|
100
|
+
// 所以这里也要多候选,否则安全检查会被静默跳过。
|
|
97
101
|
async function listArchiveEntries(file) {
|
|
98
102
|
const lower = file.toLowerCase()
|
|
99
103
|
if (lower.endsWith('.zip')) {
|
|
@@ -108,8 +112,43 @@ async function listArchiveEntries(file) {
|
|
|
108
112
|
}
|
|
109
113
|
return null
|
|
110
114
|
}
|
|
111
|
-
const
|
|
112
|
-
|
|
115
|
+
for (const [cmd, args] of tarListAttempts(file)) {
|
|
116
|
+
const output = await runCapture(cmd, args)
|
|
117
|
+
if (output) return output.split(/\r?\n/).filter(Boolean)
|
|
118
|
+
}
|
|
119
|
+
return null
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function systemTar() {
|
|
123
|
+
if (process.platform !== 'win32') return null
|
|
124
|
+
const root = process.env.SystemRoot || process.env.windir || 'C:\\Windows'
|
|
125
|
+
return path.join(root, 'System32', 'tar.exe')
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// 解压/列目录的候选命令。顺序有意义:
|
|
129
|
+
// 1. 裸 tar:bsdtar(Windows 自带、macOS 自带)与 GNU tar 都能跑相对路径。
|
|
130
|
+
// 2. --force-local:只有 GNU tar 认,用来救「绝对路径被当成远程主机」的情况。
|
|
131
|
+
// bsdtar 会明确拒绝这个选项,所以必须放在裸 tar 之后、失败后才轮到。
|
|
132
|
+
// 3. Windows 自带的 System32\tar.exe:显式绕开 PATH 上被 Git-Bash 顶掉的 GNU tar。
|
|
133
|
+
// 4. python -m tarfile:最后兜底,不依赖任何外部 tar。
|
|
134
|
+
function tarAttempts(file, destDir) {
|
|
135
|
+
const list = [['tar', ['-xf', file, '-C', destDir]]]
|
|
136
|
+
list.push(['tar', ['--force-local', '-xf', file, '-C', destDir]])
|
|
137
|
+
const sysTar = systemTar()
|
|
138
|
+
if (sysTar) list.push([sysTar, ['-xf', file, '-C', destDir]])
|
|
139
|
+
list.push(['python', ['-m', 'tarfile', '-e', file, destDir]])
|
|
140
|
+
list.push(['python3', ['-m', 'tarfile', '-e', file, destDir]])
|
|
141
|
+
return list
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function tarListAttempts(file) {
|
|
145
|
+
const list = [['tar', ['-tf', file]]]
|
|
146
|
+
list.push(['tar', ['--force-local', '-tf', file]])
|
|
147
|
+
const sysTar = systemTar()
|
|
148
|
+
if (sysTar) list.push([sysTar, ['-tf', file]])
|
|
149
|
+
list.push(['python', ['-c', 'import sys,tarfile;print("\n".join(tarfile.open(sys.argv[1]).getnames()))', file]])
|
|
150
|
+
list.push(['python3', ['-c', 'import sys,tarfile;print("\n".join(tarfile.open(sys.argv[1]).getnames()))', file]])
|
|
151
|
+
return list
|
|
113
152
|
}
|
|
114
153
|
|
|
115
154
|
async function sha256File(file) {
|
|
@@ -243,7 +282,8 @@ export async function downloadArtifact(artifact, { sourceId = 'auto', onProgress
|
|
|
243
282
|
} else {
|
|
244
283
|
source = await pickSource(sources, (item) => sourceProbeUrl(item, artifact))
|
|
245
284
|
}
|
|
246
|
-
const
|
|
285
|
+
const { modelsDir } = await import('./models.js')
|
|
286
|
+
const destDir = path.join(modelsDir(), artifact.dir)
|
|
247
287
|
const result = { source: source.id, sourceLabel: source.label, files: [], bytes: 0 }
|
|
248
288
|
|
|
249
289
|
if (artifact.source.type === 'github-assets') {
|
|
@@ -343,8 +383,13 @@ export async function extractArchive(file, destDir) {
|
|
|
343
383
|
attempts.push(['tar', ['-xf', file, '-C', destDir]])
|
|
344
384
|
attempts.push(['powershell.exe', ['-NoProfile', '-Command', `Expand-Archive -LiteralPath '${file}' -DestinationPath '${destDir}' -Force`]])
|
|
345
385
|
attempts.push(['python3', ['-m', 'zipfile', '-e', file, destDir]])
|
|
386
|
+
attempts.push(['python', ['-m', 'zipfile', '-e', file, destDir]])
|
|
346
387
|
} else {
|
|
347
|
-
|
|
388
|
+
// 之前这里只有一次裸 tar:只要 PATH 上的 tar 是 GNU tar(Git for Windows / MSYS / Cygwin
|
|
389
|
+
// 都会排到前面),Windows 的 C:/ 绝对路径就会被当成 host:path,报
|
|
390
|
+
// "tar: Cannot connect to C: resolve failed"(exit 128)。而这发生在安装最后一步
|
|
391
|
+
// (说话人分离模型),前面 1.6 GB 都下完了,整次安装却被判失败。
|
|
392
|
+
attempts.push(...tarAttempts(file, destDir))
|
|
348
393
|
}
|
|
349
394
|
let lastError = null
|
|
350
395
|
for (const [cmd, args] of attempts) {
|
package/src/host.js
CHANGED
|
@@ -78,8 +78,10 @@ import { scan as scanSetup, plan as planSetup, install as installSetup, cancelSe
|
|
|
78
78
|
import { getSection, updateSection } from './config.js'
|
|
79
79
|
import { diarizeInfo } from './diarize.js'
|
|
80
80
|
import { getTranslationState, publicConfig, saveConfig, startTranslation } from './translate.js'
|
|
81
|
-
import { listModels, loadModelConfig, saveModelConfig } from './models.js'
|
|
81
|
+
import { listModels, loadModelConfig, modelsDir, modelsMoveState, modelsSummary, removeModel, saveModelConfig, setModelsDir } from './models.js'
|
|
82
82
|
import { findEpisode, getProviderById, listAllEpisodes } from './providers.js'
|
|
83
|
+
// 设置页每次打开都会拉 /api/update;漏了这个 import 会让整页 500(v0.11.10 起就漏了)。
|
|
84
|
+
import { checkUpdate } from './update.js'
|
|
83
85
|
|
|
84
86
|
export const name = 'dsh-podwave'
|
|
85
87
|
|
|
@@ -465,7 +467,7 @@ async function route(ctx, req, res) {
|
|
|
465
467
|
return json(res, 200, {
|
|
466
468
|
ok: true,
|
|
467
469
|
generatedAt: new Date().toISOString(),
|
|
468
|
-
version: '0.11.
|
|
470
|
+
version: '0.11.22',
|
|
469
471
|
platform,
|
|
470
472
|
engine: recommendEngine(platform),
|
|
471
473
|
paths: {
|
|
@@ -494,7 +496,7 @@ async function route(ctx, req, res) {
|
|
|
494
496
|
}
|
|
495
497
|
if ((method === 'GET' || method === 'HEAD') && routePath === 'api/health') {
|
|
496
498
|
await mediaReady
|
|
497
|
-
return json(res, 200, { ok: true, app: 'podwave', version: '0.11.
|
|
499
|
+
return json(res, 200, { ok: true, app: 'podwave', version: '0.11.22', mediaBase, transcribe: runtimeInfo(), diarize: diarizeInfo(), translate: publicConfig() })
|
|
498
500
|
}
|
|
499
501
|
if (method === 'GET' && routePath === 'api/state') {
|
|
500
502
|
const state = getState()
|
|
@@ -581,7 +583,14 @@ async function route(ctx, req, res) {
|
|
|
581
583
|
return json(res, 200, localAppStatus())
|
|
582
584
|
}
|
|
583
585
|
if (method === 'POST' && routePath === 'api/xyz/auth/from-app') {
|
|
584
|
-
|
|
586
|
+
try {
|
|
587
|
+
return json(res, 200, await loginFromLocalApp())
|
|
588
|
+
} catch (error) {
|
|
589
|
+
// 「这台机器上没有小宇宙 App / 登录已失效」是环境与凭据问题,不是服务器故障:
|
|
590
|
+
// 用 400 让前端能直接展示原因,而不是当成 500 崩掉。
|
|
591
|
+
if (!error.status) error.status = 400
|
|
592
|
+
throw error
|
|
593
|
+
}
|
|
585
594
|
}
|
|
586
595
|
if (method === 'POST' && routePath === 'api/xyz/auth/logout') {
|
|
587
596
|
return json(res, 200, logout())
|
|
@@ -1001,7 +1010,8 @@ async function route(ctx, req, res) {
|
|
|
1001
1010
|
}
|
|
1002
1011
|
|
|
1003
1012
|
if (routePath === 'api/models') {
|
|
1004
|
-
|
|
1013
|
+
const snapshot = () => ({ config: loadModelConfig(), models: listModels(), storage: modelsSummary(), transcribe: runtimeInfo(), translate: publicConfig() })
|
|
1014
|
+
if (method === 'GET') return json(res, 200, snapshot())
|
|
1005
1015
|
if (method === 'PUT') {
|
|
1006
1016
|
const body = await readJson(req)
|
|
1007
1017
|
const available = new Set(listModels().filter((model) => model.ready).map((model) => model.name))
|
|
@@ -1009,9 +1019,49 @@ async function route(ctx, req, res) {
|
|
|
1009
1019
|
if (typeof body?.asrModel === 'string' && available.has(body.asrModel)) patch.asrModel = body.asrModel
|
|
1010
1020
|
if (typeof body?.translateModel === 'string' && available.has(body.translateModel)) patch.translateModel = body.translateModel
|
|
1011
1021
|
if (!Object.keys(patch).length) return json(res, 400, { error: '没有可用的模型' })
|
|
1012
|
-
|
|
1013
|
-
return json(res, 200,
|
|
1022
|
+
saveModelConfig(patch)
|
|
1023
|
+
return json(res, 200, snapshot())
|
|
1024
|
+
}
|
|
1025
|
+
}
|
|
1026
|
+
|
|
1027
|
+
// 模型目录管理:看当前存放位置、换位置(可带迁移)、删掉不用的模型、在文件管理器里打开。
|
|
1028
|
+
if (routePath === 'api/models/dir') {
|
|
1029
|
+
if (method === 'GET') return json(res, 200, { storage: modelsSummary(), move: modelsMoveState() })
|
|
1030
|
+
if (method === 'POST') {
|
|
1031
|
+
const body = await readJson(req)
|
|
1032
|
+
if (typeof body?.dir !== 'string') return json(res, 400, { error: '缺少目标目录' })
|
|
1033
|
+
try {
|
|
1034
|
+
const result = setModelsDir(body.dir, { move: body?.move !== false })
|
|
1035
|
+
return json(res, 200, { ...result, storage: modelsSummary(), models: listModels() })
|
|
1036
|
+
} catch (error) {
|
|
1037
|
+
return json(res, 400, { error: String(error.message || error) })
|
|
1038
|
+
}
|
|
1039
|
+
}
|
|
1040
|
+
}
|
|
1041
|
+
if (method === 'POST' && routePath === 'api/models/remove') {
|
|
1042
|
+
const body = await readJson(req)
|
|
1043
|
+
try {
|
|
1044
|
+
const result = removeModel(body?.name)
|
|
1045
|
+
return json(res, 200, { ...result, models: listModels() })
|
|
1046
|
+
} catch (error) {
|
|
1047
|
+
return json(res, 400, { error: String(error.message || error) })
|
|
1048
|
+
}
|
|
1049
|
+
}
|
|
1050
|
+
if (method === 'POST' && routePath === 'api/models/open') {
|
|
1051
|
+
const dir = modelsDir()
|
|
1052
|
+
try {
|
|
1053
|
+
fs.mkdirSync(dir, { recursive: true })
|
|
1054
|
+
} catch {
|
|
1055
|
+
/* 打不开目录时下面会报错 */
|
|
1056
|
+
}
|
|
1057
|
+
const command = process.platform === 'darwin' ? 'open' : process.platform === 'win32' ? 'explorer' : 'xdg-open'
|
|
1058
|
+
try {
|
|
1059
|
+
const { spawn } = await import('node:child_process')
|
|
1060
|
+
spawn(command, [dir], { detached: true, stdio: 'ignore' }).unref()
|
|
1061
|
+
} catch {
|
|
1062
|
+
return json(res, 500, { error: '无法打开文件管理器' })
|
|
1014
1063
|
}
|
|
1064
|
+
return json(res, 200, { ok: true, dir })
|
|
1015
1065
|
}
|
|
1016
1066
|
|
|
1017
1067
|
if (routePath === 'api/jobs') {
|
package/src/library.js
CHANGED
|
@@ -19,11 +19,17 @@ const metaCache = new Map()
|
|
|
19
19
|
let scanPromise = null
|
|
20
20
|
let scanState = { running: false, startedAt: null, finishedAt: null, error: null }
|
|
21
21
|
|
|
22
|
+
// Windows 用户经常直接粘资源管理器地址栏里的 %USERPROFILE%\Music、%HOMEPATH%,
|
|
23
|
+
// 之前只认 ~ 和 ~/,这类路径会原样返回然后扫描失败。顺带支持 ~\ 与 %NAME%。
|
|
22
24
|
export function expandHome(input) {
|
|
23
|
-
|
|
25
|
+
let value = String(input || '').trim()
|
|
24
26
|
if (!value) return ''
|
|
27
|
+
value = value.replace(/%([^%\\/]+)%/g, (whole, name) => {
|
|
28
|
+
const resolved = process.env[name] || process.env[name.toUpperCase()] || process.env[name.toLowerCase()]
|
|
29
|
+
return resolved || whole
|
|
30
|
+
})
|
|
25
31
|
if (value === '~') return os.homedir()
|
|
26
|
-
if (value.startsWith('~/')) return path.join(os.homedir(), value.slice(2))
|
|
32
|
+
if (value.startsWith('~/') || value.startsWith('~\\')) return path.join(os.homedir(), value.slice(2))
|
|
27
33
|
return path.resolve(value)
|
|
28
34
|
}
|
|
29
35
|
|
package/src/llama.js
CHANGED
|
@@ -7,7 +7,8 @@ import { DATA_DIR } from './state.js'
|
|
|
7
7
|
import { getSection } from './config.js'
|
|
8
8
|
import { llamaBinary } from './setup.js'
|
|
9
9
|
import { CATALOG } from './catalog.js'
|
|
10
|
-
import {
|
|
10
|
+
import { modelsDir } from './models.js'
|
|
11
|
+
import { detectPlatform, isWindows, killTree } from './platform.js'
|
|
11
12
|
|
|
12
13
|
const TARGET_NAMES = { zh: 'Simplified Chinese', en: 'English' }
|
|
13
14
|
const IDLE_MS = 90_000
|
|
@@ -26,7 +27,7 @@ export function llamaServerPath() {
|
|
|
26
27
|
}
|
|
27
28
|
|
|
28
29
|
function modelFileFor(name) {
|
|
29
|
-
const dir = path.join(
|
|
30
|
+
const dir = path.join(modelsDir(), name)
|
|
30
31
|
if (!fs.existsSync(dir)) return null
|
|
31
32
|
const file = fs.readdirSync(dir).find((entry) => entry.endsWith('.gguf'))
|
|
32
33
|
return file ? path.join(dir, file) : null
|
|
@@ -91,11 +92,9 @@ export async function ensureLlamaServer() {
|
|
|
91
92
|
|
|
92
93
|
export function stopLlamaServer() {
|
|
93
94
|
if (!server) return
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
// ignore
|
|
98
|
-
}
|
|
95
|
+
// Windows 上 SIGTERM 等于 TerminateProcess,llama-server 的临时文件不会清理;
|
|
96
|
+
// killTree 在 Windows 走 taskkill /T,其它平台仍是优雅 SIGTERM。
|
|
97
|
+
killTree(server.child.pid)
|
|
99
98
|
server = null
|
|
100
99
|
}
|
|
101
100
|
|