dsh-mobile-plugin 0.5.138 → 0.5.140
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/config/agent-presets/standard/skills/dsh-mobile-kb/references/pairing.md +2 -2
- package/lib/impl.js +8 -5
- package/lib/mobile/permissions.js +10 -0
- package/lib/mobile/session-step.js +17 -11
- package/lib/mobile/voice/frames.js +198 -0
- package/lib/mobile/voice/gateway.js +746 -0
- package/lib/mobile/voice/lang.js +347 -0
- package/lib/mobile/voice/lexicon-store.js +189 -0
- package/lib/mobile/voice/lexicon.js +508 -0
- package/lib/mobile/voice/panel-api.js +88 -0
- package/lib/mobile/voice/postprocess.js +551 -0
- package/lib/mobile/voice/protocol.js +306 -0
- package/lib/mobile/voice/providers/cn/aliyun.js +401 -0
- package/lib/mobile/voice/providers/cn/tencent.js +402 -0
- package/lib/mobile/voice/providers/cn/volc.js +328 -0
- package/lib/mobile/voice/providers/cn/xfyun.js +290 -0
- package/lib/mobile/voice/providers/host.js +190 -0
- package/lib/mobile/voice/providers/intl/_common.js +346 -0
- package/lib/mobile/voice/providers/intl/assemblyai.js +353 -0
- package/lib/mobile/voice/providers/intl/azure.js +444 -0
- package/lib/mobile/voice/providers/intl/deepgram.js +366 -0
- package/lib/mobile/voice/providers/intl/openai.js +389 -0
- package/lib/mobile/voice/providers/intl/speechmatics.js +405 -0
- package/lib/mobile/voice/providers/local/ondevice.js +129 -0
- package/lib/mobile/voice/providers/registry.js +277 -0
- package/lib/mobile/voice/routes.js +278 -0
- package/lib/mobile/voice/runtime.js +84 -0
- package/lib/mobile/voice/settings.js +206 -0
- package/lib/mobile/voice/stream.js +415 -0
- package/lib/mobile/voice/voice-provider.js +268 -0
- package/lib/panel/index.html +17 -17
- package/lib/routes/control-plane.js +19 -0
- package/lib/routes/mobile-api.js +33 -1
- package/lib/sidecar-facade-worker.cjs +1 -1
- package/lib/zeronews/runner.cjs +7 -4
- package/lib/zeronews/wiring.cjs +36 -43
- package/package.json +14 -15
|
@@ -0,0 +1,444 @@
|
|
|
1
|
+
// Azure AI Speech 适配器:整段走短音频 REST(Ocp-Apim-Subscription-Key),
|
|
2
|
+
// 流式走连续识别 WebSocket(先 issueToken 换 Bearer,再按 Speech SDK 的 2 字节头 + 头字段 + PCM 帧发送)。
|
|
3
|
+
'use strict';
|
|
4
|
+
|
|
5
|
+
const { randomUUID } = require('node:crypto');
|
|
6
|
+
|
|
7
|
+
const {
|
|
8
|
+
CODES,
|
|
9
|
+
VoiceError,
|
|
10
|
+
httpRequest,
|
|
11
|
+
httpJson,
|
|
12
|
+
openWs,
|
|
13
|
+
baseLang,
|
|
14
|
+
localeOf,
|
|
15
|
+
safeLog,
|
|
16
|
+
backoff,
|
|
17
|
+
scrub,
|
|
18
|
+
closeWs,
|
|
19
|
+
} = require('./_common');
|
|
20
|
+
|
|
21
|
+
const DEFAULT_REGION = 'eastasia';
|
|
22
|
+
const SAMPLE_RATE = 16000;
|
|
23
|
+
|
|
24
|
+
const LOCALES = {
|
|
25
|
+
zh: 'zh-CN',
|
|
26
|
+
en: 'en-US',
|
|
27
|
+
ja: 'ja-JP',
|
|
28
|
+
ko: 'ko-KR',
|
|
29
|
+
es: 'es-ES',
|
|
30
|
+
fr: 'fr-FR',
|
|
31
|
+
de: 'de-DE',
|
|
32
|
+
it: 'it-IT',
|
|
33
|
+
pt: 'pt-BR',
|
|
34
|
+
ru: 'ru-RU',
|
|
35
|
+
ar: 'ar-SA',
|
|
36
|
+
hi: 'hi-IN',
|
|
37
|
+
nl: 'nl-NL',
|
|
38
|
+
pl: 'pl-PL',
|
|
39
|
+
tr: 'tr-TR',
|
|
40
|
+
uk: 'uk-UA',
|
|
41
|
+
vi: 'vi-VN',
|
|
42
|
+
th: 'th-TH',
|
|
43
|
+
id: 'id-ID',
|
|
44
|
+
sv: 'sv-SE',
|
|
45
|
+
da: 'da-DK',
|
|
46
|
+
no: 'nb-NO',
|
|
47
|
+
fi: 'fi-FI',
|
|
48
|
+
cs: 'cs-CZ',
|
|
49
|
+
el: 'el-GR',
|
|
50
|
+
he: 'he-IL',
|
|
51
|
+
hu: 'hu-HU',
|
|
52
|
+
ro: 'ro-RO',
|
|
53
|
+
sk: 'sk-SK',
|
|
54
|
+
bg: 'bg-BG',
|
|
55
|
+
ca: 'ca-ES',
|
|
56
|
+
ms: 'ms-MY',
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
// 短音频整段接口必须指定语言;自动语种检测只在连续识别里按候选语言做,所以这里不写 'auto'。
|
|
60
|
+
const LANGUAGES = Object.keys(LOCALES);
|
|
61
|
+
|
|
62
|
+
const CONFIG_FIELDS = [
|
|
63
|
+
{ key: 'apiKey', label: '订阅密钥', secret: true, required: true },
|
|
64
|
+
{ key: 'region', label: '区域(如 eastasia / eastus)', secret: false, required: false, placeholder: DEFAULT_REGION },
|
|
65
|
+
{ key: 'baseUrl', label: '识别地址(可选)', secret: false, required: false, placeholder: '' },
|
|
66
|
+
{ key: 'tokenUrl', label: '令牌地址(可选)', secret: false, required: false, placeholder: '' },
|
|
67
|
+
{ key: 'wsUrl', label: '流式地址(可选)', secret: false, required: false, placeholder: '' },
|
|
68
|
+
{ key: 'lidLocales', label: '自动语种候选(逗号分隔,可选)', secret: false, required: false, placeholder: 'zh-CN,en-US,ja-JP' },
|
|
69
|
+
];
|
|
70
|
+
|
|
71
|
+
function keyOf(config) {
|
|
72
|
+
return String((config && config.apiKey) || '').trim();
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function regionOf(config) {
|
|
76
|
+
return String((config && config.region) || DEFAULT_REGION).trim();
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function trimSlash(value) {
|
|
80
|
+
return String(value || '').replace(/\/+$/, '');
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function baseUrlOf(config) {
|
|
84
|
+
return trimSlash((config && config.baseUrl) || `https://${regionOf(config)}.stt.speech.microsoft.com`);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function tokenUrlOf(config) {
|
|
88
|
+
return trimSlash((config && config.tokenUrl) || `https://${regionOf(config)}.api.cognitive.microsoft.com`);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function wsUrlOf(config) {
|
|
92
|
+
return trimSlash((config && config.wsUrl) || `wss://${regionOf(config)}.stt.speech.microsoft.com`);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function timeoutOf(config, fallback) {
|
|
96
|
+
const value = Number(config && config.timeoutMs);
|
|
97
|
+
return Number.isFinite(value) && value > 0 ? value : fallback;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function ready(config) {
|
|
101
|
+
return Boolean(keyOf(config));
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function localeFor(lang) {
|
|
105
|
+
const value = String(lang || '').trim();
|
|
106
|
+
if (!value || baseLang(value) === 'auto') return 'auto';
|
|
107
|
+
// 允许直接写 zh-CN 这类完整语言码。
|
|
108
|
+
if (/^[a-z]{2}-[A-Z]{2}$/.test(value)) return value;
|
|
109
|
+
return localeOf(value, LOCALES);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** 整段:短音频 REST,返回 RecognitionStatus + DisplayText。 */
|
|
113
|
+
async function transcribe({ config, audio, hints, signal, log } = {}) {
|
|
114
|
+
const cfg = config || {};
|
|
115
|
+
const key = keyOf(cfg);
|
|
116
|
+
if (!key) throw new VoiceError(CODES.UNAVAILABLE, 'Azure 未配置订阅密钥');
|
|
117
|
+
if (!audio || !audio.length) throw new VoiceError(CODES.INVALID_AUDIO, 'Azure 收到空音频');
|
|
118
|
+
const locale = localeFor(hints && hints.lang);
|
|
119
|
+
if (locale === 'auto') {
|
|
120
|
+
throw new VoiceError(CODES.UNAVAILABLE, 'Azure 整段识别必须指定语言(短音频接口不支持自动语种检测)');
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const params = new URLSearchParams({ language: locale, format: 'detailed' });
|
|
124
|
+
safeLog(log, `Azure 整段识别:locale=${locale} bytes=${audio.length}`);
|
|
125
|
+
const res = await httpJson(`${baseUrlOf(cfg)}/speech/recognition/conversation/cognitiveservices/v1?${params.toString()}`, {
|
|
126
|
+
headers: {
|
|
127
|
+
'Ocp-Apim-Subscription-Key': key,
|
|
128
|
+
'content-type': 'audio/wav; codec=audio/wav; samplerate=16000',
|
|
129
|
+
accept: 'application/json',
|
|
130
|
+
},
|
|
131
|
+
body: audio,
|
|
132
|
+
timeoutMs: timeoutOf(cfg, 60000),
|
|
133
|
+
label: 'Azure 识别',
|
|
134
|
+
secrets: [key],
|
|
135
|
+
signal,
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
const body = res.json || {};
|
|
139
|
+
const status = String(body.RecognitionStatus || '');
|
|
140
|
+
if (status === 'Success') {
|
|
141
|
+
const best = (body.NBest && body.NBest[0]) || {};
|
|
142
|
+
const text = String(body.DisplayText || best.Display || '').trim();
|
|
143
|
+
if (!text) throw new VoiceError(CODES.INVALID_AUDIO, 'Azure 没有识别到语音内容');
|
|
144
|
+
return { text };
|
|
145
|
+
}
|
|
146
|
+
if (status === 'NoMatch' || status === 'InitialSilenceTimeout' || status === 'BabbleTimeout') {
|
|
147
|
+
throw new VoiceError(CODES.INVALID_AUDIO, `Azure 未识别到有效语音(${status})`);
|
|
148
|
+
}
|
|
149
|
+
if (status === 'Error' || status === 'ServiceTimeout') {
|
|
150
|
+
throw new VoiceError(status === 'ServiceTimeout' ? CODES.TIMEOUT : CODES.FAILED, `Azure 识别失败(${status})`);
|
|
151
|
+
}
|
|
152
|
+
throw new VoiceError(CODES.FAILED, `Azure 识别返回未知状态(${status || '空'})`);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** 连续识别:issueToken → WS → speech.config → Path:audio 帧 → Path:turn.end。 */
|
|
156
|
+
function stream({ config, hints, signal, onPartial, onFinal, onError } = {}) {
|
|
157
|
+
const cfg = config || {};
|
|
158
|
+
const key = keyOf(cfg);
|
|
159
|
+
const locale = localeFor(hints && hints.lang);
|
|
160
|
+
const requestId = randomUUID();
|
|
161
|
+
const lidLocales = lidCandidates(cfg);
|
|
162
|
+
|
|
163
|
+
let ws = null;
|
|
164
|
+
let finished = false;
|
|
165
|
+
let cancelled = false;
|
|
166
|
+
let failed = false;
|
|
167
|
+
let ended = false;
|
|
168
|
+
const pending = [];
|
|
169
|
+
let pendingBytes = 0;
|
|
170
|
+
const PENDING_LIMIT = 4 * 1024 * 1024;
|
|
171
|
+
|
|
172
|
+
function emitPartial(text) {
|
|
173
|
+
if (cancelled || failed) return;
|
|
174
|
+
const value = String(text || '').trim();
|
|
175
|
+
if (!value) return;
|
|
176
|
+
if (typeof onPartial === 'function') {
|
|
177
|
+
try {
|
|
178
|
+
onPartial(value);
|
|
179
|
+
} catch {
|
|
180
|
+
// 回调抛错不影响连接
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function emitFinal(text) {
|
|
186
|
+
if (cancelled || failed) return;
|
|
187
|
+
const value = String(text || '').trim();
|
|
188
|
+
if (!value) return;
|
|
189
|
+
if (typeof onFinal === 'function') {
|
|
190
|
+
try {
|
|
191
|
+
onFinal(value);
|
|
192
|
+
} catch {
|
|
193
|
+
// 同上
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function emitError(err) {
|
|
199
|
+
if (cancelled || failed) return;
|
|
200
|
+
failed = true;
|
|
201
|
+
if (typeof onError === 'function') {
|
|
202
|
+
try {
|
|
203
|
+
onError(err);
|
|
204
|
+
} catch {
|
|
205
|
+
// 同上
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
async function fetchToken() {
|
|
211
|
+
const res = await httpRequest(`${tokenUrlOf(cfg)}/sts/v1.0/issueToken`, {
|
|
212
|
+
headers: { 'Ocp-Apim-Subscription-Key': key },
|
|
213
|
+
timeoutMs: timeoutOf(cfg, 15000),
|
|
214
|
+
label: 'Azure 换取令牌',
|
|
215
|
+
secrets: [key],
|
|
216
|
+
signal,
|
|
217
|
+
});
|
|
218
|
+
if (res.status < 200 || res.status >= 300) {
|
|
219
|
+
const detail = scrub(res.text || '', [key]);
|
|
220
|
+
const code = res.status === 401 || res.status === 403 ? CODES.UNAVAILABLE : res.status === 429 ? CODES.RATE_LIMITED : CODES.FAILED;
|
|
221
|
+
throw new VoiceError(code, `Azure 换取令牌失败(HTTP ${res.status}${detail ? `:${detail}` : ''})`);
|
|
222
|
+
}
|
|
223
|
+
const token = String(res.text || '').trim();
|
|
224
|
+
if (!token) throw new VoiceError(CODES.UNAVAILABLE, 'Azure 没有返回访问令牌');
|
|
225
|
+
return token;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
function speechConfig() {
|
|
229
|
+
if (locale !== 'auto' || !lidLocales.length) return null;
|
|
230
|
+
return {
|
|
231
|
+
context: {
|
|
232
|
+
system: {
|
|
233
|
+
languageIdentification: {
|
|
234
|
+
candidateLocales: lidLocales,
|
|
235
|
+
mode: 'DetectContinuous',
|
|
236
|
+
},
|
|
237
|
+
},
|
|
238
|
+
},
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
function parseTextFrame(raw) {
|
|
243
|
+
const index = raw.indexOf('\r\n\r\n');
|
|
244
|
+
const headerText = index >= 0 ? raw.slice(0, index) : raw;
|
|
245
|
+
const body = index >= 0 ? raw.slice(index + 4) : '';
|
|
246
|
+
const headers = {};
|
|
247
|
+
for (const line of headerText.split('\r\n')) {
|
|
248
|
+
const at = line.indexOf(':');
|
|
249
|
+
if (at <= 0) continue;
|
|
250
|
+
headers[line.slice(0, at).trim()] = line.slice(at + 1).trim();
|
|
251
|
+
}
|
|
252
|
+
let json = null;
|
|
253
|
+
if (body) {
|
|
254
|
+
try {
|
|
255
|
+
json = JSON.parse(body);
|
|
256
|
+
} catch {
|
|
257
|
+
json = null;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
return { path: headers.Path || headers.path || '', json };
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
function handleText(raw) {
|
|
264
|
+
const { path, json } = parseTextFrame(raw);
|
|
265
|
+
if (path === 'speech.hypothesis') {
|
|
266
|
+
emitPartial(json && json.DisplayText);
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
if (path === 'speech.phrase') {
|
|
270
|
+
if (!json) return;
|
|
271
|
+
const status = String(json.RecognitionStatus || '');
|
|
272
|
+
if (status === 'Success') {
|
|
273
|
+
const best = (json.NBest && json.NBest[0]) || {};
|
|
274
|
+
emitFinal(json.DisplayText || best.Display || '');
|
|
275
|
+
} else if (status === 'Error') {
|
|
276
|
+
emitError(new VoiceError(CODES.FAILED, `Azure 流式识别失败:${scrub(json.DisplayText || '未知错误', [key])}`));
|
|
277
|
+
}
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
if (path === 'speech.config') return;
|
|
281
|
+
if (path === 'turn.end') {
|
|
282
|
+
ended = true;
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
const readyPromise = (async () => {
|
|
288
|
+
if (!key) throw new VoiceError(CODES.UNAVAILABLE, 'Azure 未配置订阅密钥');
|
|
289
|
+
if (signal && signal.aborted) throw new VoiceError(CODES.CANCELLED, 'Azure 流式已取消', { cancelled: true });
|
|
290
|
+
const token = await fetchToken();
|
|
291
|
+
if (locale === 'auto' && !lidLocales.length) {
|
|
292
|
+
throw new VoiceError(CODES.UNAVAILABLE, 'Azure 自动语种检测需要配置候选语言(lidLocales)');
|
|
293
|
+
}
|
|
294
|
+
const params = new URLSearchParams(locale === 'auto' ? {} : { language: locale });
|
|
295
|
+
ws = await openWs(`${wsUrlOf(cfg)}/speech/recognition/conversation/cognitiveservices/v1?${params.toString()}`, {
|
|
296
|
+
headers: {
|
|
297
|
+
authorization: `Bearer ${token}`,
|
|
298
|
+
'x-timestamp': new Date().toISOString(),
|
|
299
|
+
'x-requestid': requestId,
|
|
300
|
+
},
|
|
301
|
+
signal,
|
|
302
|
+
timeoutMs: timeoutOf(cfg, 15000),
|
|
303
|
+
label: 'Azure 流式连接',
|
|
304
|
+
secrets: [key, token],
|
|
305
|
+
});
|
|
306
|
+
if (signal) {
|
|
307
|
+
// 契约要求 signal 能停掉已连上的会话;网关取消时还会再调一次 cancel()。
|
|
308
|
+
const onAbort = () => {
|
|
309
|
+
cancelled = true;
|
|
310
|
+
finished = true;
|
|
311
|
+
try {
|
|
312
|
+
ws.terminate();
|
|
313
|
+
} catch {
|
|
314
|
+
// 已经断了
|
|
315
|
+
}
|
|
316
|
+
};
|
|
317
|
+
signal.addEventListener('abort', onAbort, { once: true });
|
|
318
|
+
ws.once('close', () => signal.removeEventListener('abort', onAbort));
|
|
319
|
+
}
|
|
320
|
+
ws.on('message', (data, isBinary) => {
|
|
321
|
+
if (isBinary) return;
|
|
322
|
+
handleText(data.toString('utf8'));
|
|
323
|
+
});
|
|
324
|
+
ws.on('error', (err) => {
|
|
325
|
+
emitError(new VoiceError(CODES.FAILED, `Azure 流式连接中断:${scrub(err && err.message, [key])}`));
|
|
326
|
+
});
|
|
327
|
+
ws.on('close', (code, reason) => {
|
|
328
|
+
if (cancelled || failed || finished) return;
|
|
329
|
+
const detail = scrub(reason ? reason.toString('utf8') : '', [key]);
|
|
330
|
+
emitError(new VoiceError(CODES.UNAVAILABLE, `Azure 流式连接被关闭(${code}${detail ? `:${detail}` : ''})`));
|
|
331
|
+
});
|
|
332
|
+
|
|
333
|
+
const configMessage = speechConfig();
|
|
334
|
+
if (configMessage) sendText(ws, requestId, 'speech.config', JSON.stringify(configMessage));
|
|
335
|
+
for (const chunk of pending.splice(0)) ws.send(chunk);
|
|
336
|
+
pendingBytes = 0;
|
|
337
|
+
return true;
|
|
338
|
+
})();
|
|
339
|
+
|
|
340
|
+
readyPromise.catch((err) => {
|
|
341
|
+
if (cancelled) return;
|
|
342
|
+
emitError(err instanceof VoiceError ? err : new VoiceError(CODES.FAILED, scrub(err && err.message, [key])));
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
return {
|
|
346
|
+
ready: readyPromise,
|
|
347
|
+
push(pcm) {
|
|
348
|
+
if (cancelled || failed || finished) return;
|
|
349
|
+
if (!pcm || !pcm.length) return;
|
|
350
|
+
const chunk = Buffer.isBuffer(pcm) ? pcm : Buffer.from(pcm);
|
|
351
|
+
const frame = azureBinaryFrame('audio', requestId, chunk);
|
|
352
|
+
if (ws && ws.readyState === 1) {
|
|
353
|
+
ws.send(frame);
|
|
354
|
+
return;
|
|
355
|
+
}
|
|
356
|
+
if (pendingBytes + frame.length > PENDING_LIMIT) return;
|
|
357
|
+
pending.push(frame);
|
|
358
|
+
pendingBytes += frame.length;
|
|
359
|
+
},
|
|
360
|
+
async finish() {
|
|
361
|
+
if (finished) return;
|
|
362
|
+
finished = true;
|
|
363
|
+
try {
|
|
364
|
+
await readyPromise;
|
|
365
|
+
} catch {
|
|
366
|
+
return;
|
|
367
|
+
}
|
|
368
|
+
if (!ws) return;
|
|
369
|
+
try {
|
|
370
|
+
ws.send(azureTextFrame('turn.end', requestId, ''));
|
|
371
|
+
} catch {
|
|
372
|
+
return;
|
|
373
|
+
}
|
|
374
|
+
// 服务端回 turn.end 表示这一轮音频处理完了,客户端接着主动关连接(Speech SDK 也是这个顺序)。
|
|
375
|
+
const deadline = Date.now() + timeoutOf(cfg, 1500);
|
|
376
|
+
while (!ended && Date.now() < deadline) {
|
|
377
|
+
await backoff(25, signal).catch(() => {});
|
|
378
|
+
}
|
|
379
|
+
await closeWs(ws);
|
|
380
|
+
},
|
|
381
|
+
async cancel() {
|
|
382
|
+
if (cancelled) return;
|
|
383
|
+
cancelled = true;
|
|
384
|
+
finished = true;
|
|
385
|
+
pending.length = 0;
|
|
386
|
+
if (ws) {
|
|
387
|
+
try {
|
|
388
|
+
ws.terminate();
|
|
389
|
+
} catch {
|
|
390
|
+
// 已经断了
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
},
|
|
394
|
+
get socket() {
|
|
395
|
+
return ws;
|
|
396
|
+
},
|
|
397
|
+
};
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
/** 候选语言:配置 lidLocales(逗号分隔,可写 zh-CN 或 zh)→ 默认中英日韩等常用集合。 */
|
|
401
|
+
function lidCandidates(config) {
|
|
402
|
+
const raw = String((config && config.lidLocales) || '').trim();
|
|
403
|
+
if (raw) {
|
|
404
|
+
return raw
|
|
405
|
+
.split(',')
|
|
406
|
+
.map((item) => localeFor(item.trim()))
|
|
407
|
+
.filter((item) => item && item !== 'auto');
|
|
408
|
+
}
|
|
409
|
+
return ['zh-CN', 'en-US', 'ja-JP', 'ko-KR', 'es-ES', 'fr-FR', 'de-DE'];
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
function sendText(ws, requestId, path, body) {
|
|
413
|
+
ws.send(azureTextFrame(path, requestId, body));
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
/** 文本帧:Path/X-RequestId 等头按 CRLF 拼,空行后是 JSON 正文。 */
|
|
417
|
+
function azureTextFrame(path, requestId, body) {
|
|
418
|
+
const headers = ['Path:' + path, 'X-RequestId:' + requestId, 'Content-Type:application/json; charset=utf-8'].join('\r\n');
|
|
419
|
+
return `${headers}\r\n\r\n${body || ''}`;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/** 二进制帧:2 字节大端头长度 + 头字段(Path/X-RequestId CRLF)+ PCM。 */
|
|
423
|
+
function azureBinaryFrame(path, requestId, payload) {
|
|
424
|
+
const headerBytes = Buffer.from(`Path:${path}\r\nX-RequestId:${requestId}\r\n`, 'utf8');
|
|
425
|
+
const length = Buffer.alloc(2);
|
|
426
|
+
length.writeUInt16BE(headerBytes.length, 0);
|
|
427
|
+
return Buffer.concat([length, headerBytes, payload]);
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
module.exports = {
|
|
431
|
+
id: 'azure',
|
|
432
|
+
name: 'Azure AI Speech',
|
|
433
|
+
kind: 'intl',
|
|
434
|
+
batch: true,
|
|
435
|
+
streaming: true,
|
|
436
|
+
languages: LANGUAGES,
|
|
437
|
+
hotwords: false,
|
|
438
|
+
maxSeconds: 60,
|
|
439
|
+
maxBytes: 10 * 1024 * 1024,
|
|
440
|
+
configFields: CONFIG_FIELDS,
|
|
441
|
+
ready,
|
|
442
|
+
transcribe,
|
|
443
|
+
stream,
|
|
444
|
+
};
|