dsh-folia 4.26.0-pre.17 → 4.26.0-pre.18
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/lib/routes/pet-chat.js +27 -3
- package/lib/voice/record.js +6 -1
- package/package.json +1 -1
package/lib/routes/pet-chat.js
CHANGED
|
@@ -24,8 +24,8 @@
|
|
|
24
24
|
// 「语音输入」实验特性)—— 那种情况由路由回 `asr-not-ready`,而不是启动失败。
|
|
25
25
|
export const DEPS = ['disposers', 'base', 'log', 'readSettings', 'speechToText'];
|
|
26
26
|
|
|
27
|
-
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
28
|
-
import { homedir } from 'node:os';
|
|
27
|
+
import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
|
|
28
|
+
import { homedir, tmpdir } from 'node:os';
|
|
29
29
|
import { dirname, join } from 'node:path';
|
|
30
30
|
|
|
31
31
|
import {
|
|
@@ -85,7 +85,7 @@ const ASR_NOT_READY_HINT = '语音输入还没就绪:模型还没下载好,
|
|
|
85
85
|
* 安装根:`~/.dsh-folia/asr/v1`。
|
|
86
86
|
* 首次点桌宠说话时从站点下载(约 340MB;node.exe 复用 tts 包),之后完全离线。
|
|
87
87
|
*/
|
|
88
|
-
const ASR_ROOT = join(homedir(), '.dsh-folia', 'asr', 'v1');
|
|
88
|
+
const ASR_ROOT = process.env.FOLIA_ASR_ROOT || join(homedir(), '.dsh-folia', 'asr', 'v1');
|
|
89
89
|
/** 下载/就绪状态(GET /pet-chat/asr 直接回它)。 */
|
|
90
90
|
let asrState = { phase: 'idle', doneBytes: 0, totalBytes: 0, error: null };
|
|
91
91
|
/** worker 实例(懒启动,常驻)。 */
|
|
@@ -141,6 +141,12 @@ function ensureLocalAsr() {
|
|
|
141
141
|
return asrInstallPromise;
|
|
142
142
|
}
|
|
143
143
|
|
|
144
|
+
/** 本地 STT 是否已就绪(已装或已启动)—— 决定 /transcribe 走本地还是 DSH 服务。 */
|
|
145
|
+
async function localAsrReady() {
|
|
146
|
+
if (asrWorker) return true;
|
|
147
|
+
return asrInstalledOnDisk();
|
|
148
|
+
}
|
|
149
|
+
|
|
144
150
|
/**
|
|
145
151
|
* 解析 16kHz 单声道 PCM16 WAV 的时长(秒)。
|
|
146
152
|
*
|
|
@@ -741,6 +747,24 @@ export function registerPetChatRoutes(webServer, c) {
|
|
|
741
747
|
asrLastAt.set(ip, nowMs);
|
|
742
748
|
|
|
743
749
|
// ① 服务可能压根没装(用户没启用「语音输入」实验特性)
|
|
750
|
+
// 本地 STT 优先:收到的是 WAV 字节 ⇒ 落临时文件交给 worker 识别
|
|
751
|
+
if (await localAsrReady()) {
|
|
752
|
+
try {
|
|
753
|
+
const dir = mkdtempSync(join(tmpdir(), 'folia-asr-'));
|
|
754
|
+
const wavPath = join(dir, 'input.wav');
|
|
755
|
+
writeFileSync(wavPath, Buffer.from(audio));
|
|
756
|
+
const worker = await ensureLocalAsr();
|
|
757
|
+
const spoken = await worker.request(wavPath, ASR_TIMEOUT_MS);
|
|
758
|
+
try { rmSync(dir, { recursive: true, force: true }); } catch { /* ignore */ }
|
|
759
|
+
if (!spoken || !spoken.ok) { send({ ok: false, code: 'asr-failed', hint: '识别失败,再试一次' }, 502); return; }
|
|
760
|
+
const text = String(spoken.text || '').trim();
|
|
761
|
+
if (!text) { send({ ok: false, code: 'no-speech', hint: '没听清,再说一次' }, 422); return; }
|
|
762
|
+
send({ ok: true, text, ms: spoken.ms, seconds });
|
|
763
|
+
return;
|
|
764
|
+
} catch {
|
|
765
|
+
// 落到下面的 DSH 服务路径
|
|
766
|
+
}
|
|
767
|
+
}
|
|
744
768
|
const service = speechToText;
|
|
745
769
|
if (!service || typeof service.snapshot !== 'function' || typeof service.transcribe !== 'function') {
|
|
746
770
|
send({ ok: false, code: 'asr-not-ready', hint: ASR_NOT_READY_HINT });
|
package/lib/voice/record.js
CHANGED
|
@@ -162,8 +162,13 @@ export async function defaultResolveFfmpeg() {
|
|
|
162
162
|
|
|
163
163
|
/** 默认执行器:spawn + 收集 stderr(silencedetect 的输出在这里),超时强杀。 */
|
|
164
164
|
export function defaultRunFfmpeg(ff, args, timeoutMs = 30000) {
|
|
165
|
+
/**
|
|
166
|
+
* 为什么 detached:dsh web(Electron 主进程)里 spawn 控制台程序,
|
|
167
|
+
* 子进程可能在启动阶段就死掉(本仓 transcode.js 记录过同一现象:退出码 0xFFFFFFEA)。
|
|
168
|
+
* 给它独立进程组后能正常跑;仍失败时由调用方重试/报错。
|
|
169
|
+
*/
|
|
165
170
|
return new Promise((resolve) => {
|
|
166
|
-
const child = spawn(ff, args, { windowsHide: true, stdio: ['ignore', 'ignore', 'pipe'] });
|
|
171
|
+
const child = spawn(ff, args, { windowsHide: true, detached: true, stdio: ['ignore', 'ignore', 'pipe'] });
|
|
167
172
|
let stderr = '';
|
|
168
173
|
const timer = setTimeout(() => {
|
|
169
174
|
// 先确认 pid 落地再 kill:spawn 失败时 pid 是 undefined,直接 kill 会抛(守卫 verify-child-kill)
|