ai-helper-agent 2.4.7 → 2.5.0
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 +33 -1
- package/README.zh-CN.md +33 -1
- package/bin/agent.js +78 -0
- package/dist/assets/{agent-store-Wu4cQfi-.js → agent-store-D0oEv0PW.js} +1 -1
- package/dist/assets/background.js +5 -5
- package/dist/assets/chat-export-MTTSSIwM.js +37 -0
- package/dist/assets/content.js +41 -30
- package/dist/assets/{dist-LQPh1k0y.js → dist-BUuoW_84.js} +1 -1
- package/dist/assets/i18n-Br_sF5pb.js +49 -0
- package/dist/assets/{markdown-render-DsMFYJ7j.js → markdown-render-Dq5F_dqP.js} +2 -2
- package/dist/assets/{model-profiles-DWY7gG5H.js → model-profiles-BdQaV4EZ.js} +1 -1
- package/dist/assets/{options-Dir2xv8X.css → options-JtYgxtsc.css} +1 -1
- package/dist/assets/options.js +297 -238
- package/dist/assets/{page-snapshot-renderer-C0SlAjsY.js → page-snapshot-renderer-Cl0qk7U1.js} +1 -1
- package/dist/assets/{session-store-Dtfmjrt-.js → session-store-KkrAlIc6.js} +3 -3
- package/dist/assets/side_panel.css +1 -1
- package/dist/assets/side_panel.js +400 -400
- package/dist/assets/{utils-S-aJS9gf.js → utils-ZJjxSlg_.js} +1 -1
- package/dist/assets/{version-CcGQQNRR.js → version-C0CY79zF.js} +1 -1
- package/dist/manifest.json +1 -1
- package/dist/offscreen/offscreen.js +9 -9
- package/dist/options.html +38 -4
- package/dist/side_panel.html +25 -7
- package/dist/voice-capture-worklet.js +47 -0
- package/package.json +4 -1
- package/src/asr/config.js +155 -0
- package/src/asr/detect.js +215 -0
- package/src/asr/engine.js +347 -0
- package/src/asr/install.js +439 -0
- package/src/asr/model-catalog.js +207 -0
- package/src/asr/model-download.js +461 -0
- package/src/asr/model-pack.js +398 -0
- package/src/asr/offline-pool.js +283 -0
- package/src/asr/offline-worker.js +97 -0
- package/src/asr/probe-child.mjs +31 -0
- package/src/asr/routes.js +385 -0
- package/src/asr/session.js +435 -0
- package/src/asr/transcribe.js +310 -0
- package/src/directory-picker.js +549 -70
- package/src/locales/cli.json +26 -0
- package/src/locales/en.json +22 -1
- package/src/locales/zh.json +22 -1
- package/src/rag/install.js +60 -2
- package/src/server.js +168 -7
- package/dist/assets/chat-export-DYpi1VYc.js +0 -37
- package/dist/assets/i18n-DkQH7l0R.js +0 -49
|
@@ -0,0 +1,347 @@
|
|
|
1
|
+
// agent/src/asr/engine.js - sherpa-onnx-node 引擎封装(懒加载 + 空闲卸载)
|
|
2
|
+
//
|
|
3
|
+
// 设计要点:
|
|
4
|
+
// - 依赖通过 createRequire 加载(sherpa-onnx-node 是 CJS 包,预编译原生模块)
|
|
5
|
+
// - 三类实例分别懒加载并缓存:offline(整段定稿)/ online(流式草稿)/ vad(断句)
|
|
6
|
+
// - 按 modelId 缓存:用户换模型即换 key,旧实例随空闲卸载释放
|
|
7
|
+
// - 加载失败抛出带原因的错误(绝不静默降级成"没识别到")
|
|
8
|
+
// - 空闲 idleUnloadMs 后释放引用并把缓存置空,避免 onnxruntime 常驻数百 MB
|
|
9
|
+
//
|
|
10
|
+
// 关于配置键名:不同 sherpa-onnx-node 版本对 config 的键名有差异(camelCase / snake_case)。
|
|
11
|
+
// 这里以官方 README 的 camelCase 为准构造,并在构造失败时按首错信息给出可读诊断;
|
|
12
|
+
// engine 层只依赖 manifest 声明的文件,不猜模型内部结构(M0 探针脚本负责在目标机核对键名)。
|
|
13
|
+
|
|
14
|
+
import { cpus } from 'os';
|
|
15
|
+
import { createRequire } from 'module';
|
|
16
|
+
|
|
17
|
+
const require = createRequire(import.meta.url);
|
|
18
|
+
|
|
19
|
+
// 特征维度(sherpa-onnx 全系列模型固定 80 维 fbank)
|
|
20
|
+
const FEATURE_DIM = 80;
|
|
21
|
+
|
|
22
|
+
// 原生线程数:clamp(1..4, cpus-1),单用户本机形态下留一个核给主进程 IO
|
|
23
|
+
function pickNumThreads() {
|
|
24
|
+
const n = cpus().length;
|
|
25
|
+
return Math.max(1, Math.min(4, (Number.isFinite(n) ? n : 2) - 1));
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// ---- 缓存:key = `${kind}:${modelId}` ----
|
|
29
|
+
const cache = new Map();
|
|
30
|
+
|
|
31
|
+
// 空闲卸载定时器
|
|
32
|
+
let idleTimer = null;
|
|
33
|
+
|
|
34
|
+
// 已加载的模块引用(首次成功加载后复用)
|
|
35
|
+
let sherpaModule = null;
|
|
36
|
+
|
|
37
|
+
// 可注入的模块获取器(测试用;生产不传)
|
|
38
|
+
let moduleLoader = null;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* 注入 sherpa 模块加载器(仅测试用:传 () => fakeModule)
|
|
42
|
+
* @param {(() => object)|null} loader
|
|
43
|
+
*/
|
|
44
|
+
export function setSherpaModuleLoader(loader) {
|
|
45
|
+
moduleLoader = typeof loader === 'function' ? loader : null;
|
|
46
|
+
cache.clear();
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* 加载 sherpa-onnx-node 模块(失败抛出带原因的错误)
|
|
51
|
+
* @returns {object}
|
|
52
|
+
*/
|
|
53
|
+
function loadSherpa() {
|
|
54
|
+
if (sherpaModule) return sherpaModule;
|
|
55
|
+
if (moduleLoader) {
|
|
56
|
+
sherpaModule = moduleLoader();
|
|
57
|
+
if (!sherpaModule) throw new Error('sherpa module loader returned nothing');
|
|
58
|
+
return sherpaModule;
|
|
59
|
+
}
|
|
60
|
+
try {
|
|
61
|
+
sherpaModule = require('sherpa-onnx-node');
|
|
62
|
+
} catch (err) {
|
|
63
|
+
throw new Error(`sherpa-onnx-node load failed: ${err.message}`);
|
|
64
|
+
}
|
|
65
|
+
if (!sherpaModule || typeof sherpaModule !== 'object') {
|
|
66
|
+
throw new Error('sherpa-onnx-node exports nothing');
|
|
67
|
+
}
|
|
68
|
+
return sherpaModule;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* 解析构造器:优先同名导出,其次 create<Name> 工厂(跨版本兼容)
|
|
73
|
+
* @param {object} mod
|
|
74
|
+
* @param {string} name 如 'OfflineRecognizer'
|
|
75
|
+
* @returns {Function}
|
|
76
|
+
*/
|
|
77
|
+
function resolveCtor(mod, name) {
|
|
78
|
+
if (typeof mod[name] === 'function') return mod[name];
|
|
79
|
+
const factory = `create${name}`;
|
|
80
|
+
if (typeof mod[factory] === 'function') return mod[factory];
|
|
81
|
+
throw new Error(`sherpa-onnx-node missing ${name} (exports: ${Object.keys(mod).join(', ')})`);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* 回收单条缓存实例(调用可选的 free()/close() 后置空)
|
|
86
|
+
* @param {string} key
|
|
87
|
+
*/
|
|
88
|
+
function dropEntry(key) {
|
|
89
|
+
const entry = cache.get(key);
|
|
90
|
+
if (!entry) return;
|
|
91
|
+
cache.delete(key);
|
|
92
|
+
const inst = entry.instance;
|
|
93
|
+
for (const method of ['free', 'close', 'dispose']) {
|
|
94
|
+
if (inst && typeof inst[method] === 'function') {
|
|
95
|
+
try { inst[method](); } catch { /* 原生释放失败不影响后续 */ }
|
|
96
|
+
break;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* 重置空闲卸载定时器(每次取用实例后调用)
|
|
103
|
+
* @param {number} idleUnloadMs
|
|
104
|
+
*/
|
|
105
|
+
function scheduleIdleUnload(idleUnloadMs) {
|
|
106
|
+
if (idleTimer) { clearTimeout(idleTimer); idleTimer = null; }
|
|
107
|
+
if (!(idleUnloadMs > 0)) return;
|
|
108
|
+
idleTimer = setTimeout(() => {
|
|
109
|
+
idleTimer = null;
|
|
110
|
+
for (const key of Array.from(cache.keys())) dropEntry(key);
|
|
111
|
+
}, idleUnloadMs);
|
|
112
|
+
// daemon 不应因为一个空闲定时器而无法退出
|
|
113
|
+
if (typeof idleTimer.unref === 'function') idleTimer.unref();
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* 主动释放所有引擎实例(停服 / 重新检测 / 内存保护时调用)
|
|
118
|
+
*/
|
|
119
|
+
export function disposeAllEngines() {
|
|
120
|
+
if (idleTimer) { clearTimeout(idleTimer); idleTimer = null; }
|
|
121
|
+
for (const key of Array.from(cache.keys())) dropEntry(key);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* 当前已加载的引擎信息(供 /api/asr/status 展示)
|
|
126
|
+
* @returns {{ offline: string|null, online: string|null, vad: string|null }}
|
|
127
|
+
*/
|
|
128
|
+
export function getLoadedEngineInfo() {
|
|
129
|
+
const info = { offline: null, online: null, vad: null };
|
|
130
|
+
for (const [key, entry] of cache.entries()) {
|
|
131
|
+
const [kind, modelId] = key.split(':');
|
|
132
|
+
if (kind in info) info[kind] = modelId;
|
|
133
|
+
}
|
|
134
|
+
return info;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* 构造 featConfig(所有模型共用)
|
|
139
|
+
*/
|
|
140
|
+
function buildFeatConfig(model) {
|
|
141
|
+
return {
|
|
142
|
+
sampleRate: model.sampleRate || 16000,
|
|
143
|
+
featureDim: FEATURE_DIM,
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* 构造 offline 模型的 modelConfig(按 manifest.arch 分派,默认 SenseVoice)
|
|
149
|
+
* @param {object} model
|
|
150
|
+
*/
|
|
151
|
+
function buildOfflineModelConfig(model) {
|
|
152
|
+
const base = {
|
|
153
|
+
tokens: model.tokensFile,
|
|
154
|
+
numThreads: pickNumThreads(),
|
|
155
|
+
provider: 'cpu',
|
|
156
|
+
debug: false,
|
|
157
|
+
};
|
|
158
|
+
switch (model.arch) {
|
|
159
|
+
case 'paraformer':
|
|
160
|
+
return { ...base, paraformer: { model: model.modelFile } };
|
|
161
|
+
case 'whisper':
|
|
162
|
+
return {
|
|
163
|
+
...base,
|
|
164
|
+
whisper: {
|
|
165
|
+
encoder: model.encoder,
|
|
166
|
+
decoder: model.decoder,
|
|
167
|
+
language: model.senseVoiceLanguage === 'auto' ? 'auto' : model.senseVoiceLanguage,
|
|
168
|
+
task: 'transcribe',
|
|
169
|
+
},
|
|
170
|
+
};
|
|
171
|
+
case 'senseVoice':
|
|
172
|
+
default:
|
|
173
|
+
return {
|
|
174
|
+
...base,
|
|
175
|
+
senseVoice: {
|
|
176
|
+
model: model.modelFile,
|
|
177
|
+
// 逆文本规整:把「一二三」规整成「123」、补标点(SenseVoice 内置能力)
|
|
178
|
+
useInverseTextNormalization: 1,
|
|
179
|
+
language: model.senseVoiceLanguage || 'auto',
|
|
180
|
+
},
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* 构造 online 模型的 modelConfig(按 manifest.arch 分派,默认 transducer)
|
|
187
|
+
* @param {object} model
|
|
188
|
+
*/
|
|
189
|
+
function buildOnlineModelConfig(model) {
|
|
190
|
+
const base = {
|
|
191
|
+
tokens: model.tokensFile,
|
|
192
|
+
numThreads: pickNumThreads(),
|
|
193
|
+
provider: 'cpu',
|
|
194
|
+
debug: false,
|
|
195
|
+
};
|
|
196
|
+
switch (model.arch) {
|
|
197
|
+
case 'paraformerStreaming':
|
|
198
|
+
return { ...base, paraformer: { model: model.modelFile } };
|
|
199
|
+
case 'zipformer2':
|
|
200
|
+
return {
|
|
201
|
+
...base,
|
|
202
|
+
zipformer2Ctc: { model: model.modelFile },
|
|
203
|
+
};
|
|
204
|
+
case 'transducer':
|
|
205
|
+
default:
|
|
206
|
+
return {
|
|
207
|
+
...base,
|
|
208
|
+
transducer: {
|
|
209
|
+
encoder: model.encoder,
|
|
210
|
+
decoder: model.decoder,
|
|
211
|
+
joiner: model.joiner,
|
|
212
|
+
},
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* 获取 offline 识别器(整段定稿;懒加载 + 缓存)
|
|
219
|
+
* @param {object} model model-pack 解析出的模型描述
|
|
220
|
+
* @returns {object} sherpa OfflineRecognizer 实例
|
|
221
|
+
*/
|
|
222
|
+
export function getOfflineRecognizer(model, opts = {}) {
|
|
223
|
+
const key = `offline:${model.id}`;
|
|
224
|
+
const hit = cache.get(key);
|
|
225
|
+
if (hit) {
|
|
226
|
+
scheduleIdleUnload(opts.idleUnloadMs);
|
|
227
|
+
return hit.instance;
|
|
228
|
+
}
|
|
229
|
+
const sherpa = loadSherpa();
|
|
230
|
+
const Ctor = resolveCtor(sherpa, 'OfflineRecognizer');
|
|
231
|
+
let instance;
|
|
232
|
+
try {
|
|
233
|
+
instance = new Ctor({
|
|
234
|
+
featConfig: buildFeatConfig(model),
|
|
235
|
+
modelConfig: buildOfflineModelConfig(model),
|
|
236
|
+
});
|
|
237
|
+
} catch (err) {
|
|
238
|
+
throw new Error(`offline recognizer init failed (${model.id}): ${err.message}`);
|
|
239
|
+
}
|
|
240
|
+
cache.set(key, { instance, kind: 'offline', modelId: model.id });
|
|
241
|
+
scheduleIdleUnload(opts.idleUnloadMs);
|
|
242
|
+
return instance;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* 获取 online 识别器(流式草稿;懒加载 + 缓存)
|
|
247
|
+
* @param {object} model
|
|
248
|
+
* @returns {object}
|
|
249
|
+
*/
|
|
250
|
+
export function getOnlineRecognizer(model, opts = {}) {
|
|
251
|
+
const key = `online:${model.id}`;
|
|
252
|
+
const hit = cache.get(key);
|
|
253
|
+
if (hit) {
|
|
254
|
+
scheduleIdleUnload(opts.idleUnloadMs);
|
|
255
|
+
return hit.instance;
|
|
256
|
+
}
|
|
257
|
+
const sherpa = loadSherpa();
|
|
258
|
+
const Ctor = resolveCtor(sherpa, 'OnlineRecognizer');
|
|
259
|
+
let instance;
|
|
260
|
+
try {
|
|
261
|
+
instance = new Ctor({
|
|
262
|
+
featConfig: buildFeatConfig(model),
|
|
263
|
+
modelConfig: buildOnlineModelConfig(model),
|
|
264
|
+
decodingMethod: model.decodingMethod || 'greedy_search',
|
|
265
|
+
// 端点检测:整句说完自动切段,草稿不必跨句累积(规则阈值取官方常用值)
|
|
266
|
+
enableEndpoint: true,
|
|
267
|
+
rule1MinTrailingSilence: 2.4,
|
|
268
|
+
rule2MinTrailingSilence: 1.2,
|
|
269
|
+
rule3MinUtteranceLength: 20,
|
|
270
|
+
});
|
|
271
|
+
} catch (err) {
|
|
272
|
+
throw new Error(`online recognizer init failed (${model.id}): ${err.message}`);
|
|
273
|
+
}
|
|
274
|
+
cache.set(key, { instance, kind: 'online', modelId: model.id });
|
|
275
|
+
scheduleIdleUnload(opts.idleUnloadMs);
|
|
276
|
+
return instance;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* 获取 VAD(断句;懒加载 + 缓存)
|
|
281
|
+
*
|
|
282
|
+
* ⚠️ 默认禁用(AI_HELPER_ASR_VAD=1 可显式开启):sherpa-onnx-node 2.0.1 在
|
|
283
|
+
* `vad.front()` 首次取段时直接 SIGSEGV(VoiceActivityDetectorFrontWrapper,
|
|
284
|
+
* EXC_BAD_ACCESS at 0x8)。isDetected() 转为 true 后一调 front() 必崩,属于原生层
|
|
285
|
+
* 崩溃:JS 的 try/catch 拦不住,整个代理进程会当场退出,且没有任何日志栈可追。
|
|
286
|
+
* 已实测排除:单参/两参构造、front()/front(true)/front(false)、silero_vad.onnx
|
|
287
|
+
* 与 silero_vad_v5.onnx —— 全部复现,故判定为库缺陷而非配置问题。
|
|
288
|
+
* 因此默认返回 null,转写走固定切分兜底(见 transcribe.js splitByVad),
|
|
289
|
+
* 需要在其它平台或更高版本 sherpa-onnx-node 上重新验证时再显式开启。
|
|
290
|
+
* @param {object} model
|
|
291
|
+
* @returns {object|null} 禁用/无 vad 模型时返回 null(调用方按不分段处理)
|
|
292
|
+
*/
|
|
293
|
+
export function getVad(model, opts = {}) {
|
|
294
|
+
if (!model) return null;
|
|
295
|
+
if (process.env.AI_HELPER_ASR_VAD !== '1') return null;
|
|
296
|
+
const key = `vad:${model.id}`;
|
|
297
|
+
const hit = cache.get(key);
|
|
298
|
+
if (hit) {
|
|
299
|
+
scheduleIdleUnload(opts.idleUnloadMs);
|
|
300
|
+
return hit.instance;
|
|
301
|
+
}
|
|
302
|
+
const sherpa = loadSherpa();
|
|
303
|
+
const Ctor = resolveCtor(sherpa, 'Vad');
|
|
304
|
+
let instance;
|
|
305
|
+
try {
|
|
306
|
+
// 注意:sherpa-onnx-node 的 Vad 构造签名是 (config, bufferSizeInSeconds),
|
|
307
|
+
// bufferSizeInSeconds 不是 config 字段(写进 config 会被忽略)
|
|
308
|
+
instance = new Ctor({
|
|
309
|
+
sileroVad: {
|
|
310
|
+
model: model.modelFile,
|
|
311
|
+
threshold: 0.5,
|
|
312
|
+
minSpeechDuration: 0.25,
|
|
313
|
+
minSilenceDuration: 0.5,
|
|
314
|
+
maxSpeechDuration: 8,
|
|
315
|
+
windowSize: 512,
|
|
316
|
+
},
|
|
317
|
+
sampleRate: model.sampleRate || 16000,
|
|
318
|
+
numThreads: 1,
|
|
319
|
+
provider: 'cpu',
|
|
320
|
+
debug: false,
|
|
321
|
+
}, 60);
|
|
322
|
+
} catch (err) {
|
|
323
|
+
throw new Error(`vad init failed (${model.id}): ${err.message}`);
|
|
324
|
+
}
|
|
325
|
+
cache.set(key, { instance, kind: 'vad', modelId: model.id });
|
|
326
|
+
scheduleIdleUnload(opts.idleUnloadMs);
|
|
327
|
+
return instance;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* 预热(启动期异步调用;失败只记日志,绝不阻塞启动)
|
|
332
|
+
* @param {{ offline: object|null, online: object|null }} models
|
|
333
|
+
* @returns {{ warmed: string[], failed: Array<{kind: string, error: string}> }}
|
|
334
|
+
*/
|
|
335
|
+
export function warmupEngines(models, opts = {}) {
|
|
336
|
+
const warmed = [];
|
|
337
|
+
const failed = [];
|
|
338
|
+
if (models?.online) {
|
|
339
|
+
try { getOnlineRecognizer(models.online, opts); warmed.push('online'); }
|
|
340
|
+
catch (err) { failed.push({ kind: 'online', error: err.message }); }
|
|
341
|
+
}
|
|
342
|
+
if (models?.offline) {
|
|
343
|
+
try { getOfflineRecognizer(models.offline, opts); warmed.push('offline'); }
|
|
344
|
+
catch (err) { failed.push({ kind: 'offline', error: err.message }); }
|
|
345
|
+
}
|
|
346
|
+
return { warmed, failed };
|
|
347
|
+
}
|