dsh-mobile-plugin 0.5.138 → 0.5.139

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.
Files changed (35) hide show
  1. package/config/agent-presets/standard/skills/dsh-mobile-kb/references/pairing.md +2 -2
  2. package/lib/impl.js +8 -5
  3. package/lib/mobile/permissions.js +10 -0
  4. package/lib/mobile/session-step.js +17 -11
  5. package/lib/mobile/voice/frames.js +198 -0
  6. package/lib/mobile/voice/gateway.js +746 -0
  7. package/lib/mobile/voice/lang.js +347 -0
  8. package/lib/mobile/voice/lexicon-store.js +189 -0
  9. package/lib/mobile/voice/lexicon.js +508 -0
  10. package/lib/mobile/voice/panel-api.js +88 -0
  11. package/lib/mobile/voice/postprocess.js +551 -0
  12. package/lib/mobile/voice/protocol.js +306 -0
  13. package/lib/mobile/voice/providers/cn/aliyun.js +401 -0
  14. package/lib/mobile/voice/providers/cn/tencent.js +402 -0
  15. package/lib/mobile/voice/providers/cn/volc.js +328 -0
  16. package/lib/mobile/voice/providers/cn/xfyun.js +290 -0
  17. package/lib/mobile/voice/providers/host.js +190 -0
  18. package/lib/mobile/voice/providers/intl/_common.js +346 -0
  19. package/lib/mobile/voice/providers/intl/assemblyai.js +353 -0
  20. package/lib/mobile/voice/providers/intl/azure.js +444 -0
  21. package/lib/mobile/voice/providers/intl/deepgram.js +366 -0
  22. package/lib/mobile/voice/providers/intl/openai.js +389 -0
  23. package/lib/mobile/voice/providers/intl/speechmatics.js +405 -0
  24. package/lib/mobile/voice/providers/local/ondevice.js +129 -0
  25. package/lib/mobile/voice/providers/registry.js +277 -0
  26. package/lib/mobile/voice/routes.js +278 -0
  27. package/lib/mobile/voice/runtime.js +84 -0
  28. package/lib/mobile/voice/settings.js +206 -0
  29. package/lib/mobile/voice/stream.js +415 -0
  30. package/lib/mobile/voice/voice-provider.js +268 -0
  31. package/lib/panel/index.html +17 -17
  32. package/lib/routes/control-plane.js +19 -0
  33. package/lib/routes/mobile-api.js +33 -1
  34. package/lib/sidecar-facade-worker.cjs +1 -1
  35. package/package.json +13 -14
@@ -0,0 +1,268 @@
1
+ // 2026-11-28:语音 provider 适配器契约与校验,registry / 网关 / 面板只认这一份形状。
2
+ // 2026-11-28:能力表为只读条目(capabilityOnly)补「仅手机本地 / 不需密钥 / 不需电脑侧配置」字段。
3
+ 'use strict';
4
+
5
+ /**
6
+ * 语音 provider 适配器契约(`docs/voice-input-contract.md` §3 的落地版)。
7
+ *
8
+ * 契约给的是类型注释,这里把「怎么算一个合格适配器」也写成可执行的校验:registry 扫描到
9
+ * 坏文件时只跳过那一个(不让一个写错的适配器把整张能力表拖垮),面板要靠 `configFields`
10
+ * 画表单,网关要靠 `batch` / `streaming` 决定走哪条入口。所以形状必须在一个地方说死。
11
+ *
12
+ * 适配器对象的字段(全部必填,除非另注):
13
+ *
14
+ * ```js
15
+ * {
16
+ * id: 'volc', // 稳定 id,落配置、能力表、面板
17
+ * name: '火山引擎豆包', // 面板显示名
18
+ * kind: 'cn', // cn | intl | local | host
19
+ * batch: true, // 支持整段
20
+ * streaming: true, // 支持流式
21
+ * languages: ['zh', 'en'], // 支持自动检测就把 'auto' 放进去
22
+ * hotwords: true, // 能注入热词 / 上下文
23
+ * maxSeconds: 300,
24
+ * maxBytes: 8 * 1024 * 1024,
25
+ * configFields: [{ key: 'apiKey', label: 'API Key', secret: true, required: true }],
26
+ * ready(config), // 配置齐了就回 true
27
+ * async transcribe({ config, audio, hints, signal, log }), // 回 { text, lang? }
28
+ * stream({ config, hints, signal, onPartial, onFinal, onError }), // 回 { push, finish, cancel }
29
+ * }
30
+ * ```
31
+ *
32
+ * 几条硬规矩(网关会依赖它们,别在适配器里绕开):
33
+ * - 鉴权、签名、重试、超时全在适配器内部;网关只管路由、限额、错误映射与编排。
34
+ * - 失败一律抛 `protocol.voiceError(code, message)`,不要抛裸 Error(裸 Error 会被网关归成
35
+ * `voice-failed`,排障时看不出是哪一类)。
36
+ * - **key 不进日志、不进错误文案**:`message` 会进日志也可能原样回手机。
37
+ * - `stream()` 在收到 cancel / abort 后不再发回调;取消本身不属于失败。
38
+ */
39
+
40
+ const { VOICE_ERRORS, voiceError } = require('./protocol.js');
41
+
42
+ /** 适配器归属。`host` 是委托宿主 `ctx.speechToText` 的内置一家。 */
43
+ const PROVIDER_KINDS = Object.freeze(['cn', 'intl', 'local', 'host']);
44
+
45
+ /**
46
+ * 能力表里一行的状态:
47
+ * - `available`:已配置好且现在能用(宿主装着语音包 / key 填了);
48
+ * - `unconfigured`:适配器存在,但配置缺东西(面板要引导用户填);
49
+ * - `unavailable`:配置齐了也跑不了(宿主没装语音包、依赖的服务不可用)。
50
+ * App 只把 `available` 当可选 provider。
51
+ */
52
+ const PROVIDER_STATUS = Object.freeze({
53
+ AVAILABLE: 'available',
54
+ UNCONFIGURED: 'unconfigured',
55
+ UNAVAILABLE: 'unavailable',
56
+ });
57
+
58
+ /** 允许出现在 `id` 里的字符:它要进 URL 头(`x-dsh-voice-provider`)与配置文件键。 */
59
+ const PROVIDER_ID_PATTERN = /^[a-z][a-z0-9-]{0,31}$/;
60
+
61
+ /** 面板能画出来的配置控件类型;不认识的类型直接判适配器不合格,宁可不出表单。 */
62
+ const CONFIG_FIELD_TYPES = Object.freeze(['string', 'secret', 'number', 'boolean', 'select']);
63
+
64
+ // 语种数上限只挡畸形数据(真实 provider 最多几十种,speechmatics 就 33 种),
65
+ // 不是功能约束:能力表里这一格原样给 App 做选项。
66
+ const MAX_LANGUAGES = 128;
67
+ const MAX_CONFIG_FIELDS = 24;
68
+ const MIN_MAX_SECONDS = 1;
69
+ const MAX_MAX_SECONDS = 600;
70
+
71
+ function isPlainObject(value) {
72
+ return value !== null && typeof value === 'object' && !Array.isArray(value);
73
+ }
74
+
75
+ function positiveInt(value) {
76
+ return Number.isInteger(value) && value > 0;
77
+ }
78
+
79
+ /**
80
+ * 校验一个配置字段声明。
81
+ * `select` 类型必须给 `options`,否则面板画出一个没有选项的下拉框。
82
+ */
83
+ function validateConfigField(field) {
84
+ const errors = [];
85
+ if (!isPlainObject(field)) return ['配置字段必须是对象'];
86
+ const key = typeof field.key === 'string' ? field.key.trim() : '';
87
+ if (!/^[A-Za-z][A-Za-z0-9_]{0,63}$/.test(key)) errors.push(`配置字段 key 不合法:${JSON.stringify(field.key)}`);
88
+ if (typeof field.label !== 'string' || field.label.trim() === '') errors.push(`配置字段 ${key} 缺 label`);
89
+ const type = field.type || (field.secret === true ? 'secret' : 'string');
90
+ if (!CONFIG_FIELD_TYPES.includes(type)) errors.push(`配置字段 ${key} 的 type 不认识:${type}`);
91
+ if (type === 'select' && (!Array.isArray(field.options) || field.options.length === 0)) {
92
+ errors.push(`配置字段 ${key} 是 select,但没有 options`);
93
+ }
94
+ if (field.required !== undefined && typeof field.required !== 'boolean') errors.push(`配置字段 ${key} 的 required 必须是布尔`);
95
+ return errors;
96
+ }
97
+
98
+ /**
99
+ * 校验一个适配器对象,回 `{ ok, errors }`。
100
+ *
101
+ * 只校验**形状**,不调用 `ready()`:ready 可能去问宿主 / 读配置,那是 registry 的活。
102
+ * `batch` 与 `streaming` 至少一个为真:两个都假的适配器在能力表里没有任何意义。
103
+ */
104
+ function validateProvider(def) {
105
+ const errors = [];
106
+ if (!isPlainObject(def)) return { ok: false, errors: ['适配器必须是对象'] };
107
+ if (!PROVIDER_ID_PATTERN.test(typeof def.id === 'string' ? def.id : '')) {
108
+ errors.push(`id 不合法(要匹配 ${PROVIDER_ID_PATTERN}):${JSON.stringify(def.id)}`);
109
+ }
110
+ if (typeof def.name !== 'string' || def.name.trim() === '') errors.push('缺 name');
111
+ if (!PROVIDER_KINDS.includes(def.kind)) errors.push(`kind 不认识:${JSON.stringify(def.kind)}`);
112
+ if (typeof def.batch !== 'boolean') errors.push('batch 必须是布尔');
113
+ if (typeof def.streaming !== 'boolean') errors.push('streaming 必须是布尔');
114
+ if (def.batch === false && def.streaming === false) errors.push('batch 与 streaming 不能都为 false');
115
+ if (!Array.isArray(def.languages) || def.languages.length === 0) {
116
+ errors.push('languages 必须是非空数组');
117
+ } else if (def.languages.length > MAX_LANGUAGES) {
118
+ errors.push(`languages 超过 ${MAX_LANGUAGES} 项`);
119
+ } else if (def.languages.some((lang) => typeof lang !== 'string' || !/^[a-z]{2,5}$/.test(lang))) {
120
+ errors.push('languages 里只能放小写语言码(如 zh / en / auto)');
121
+ }
122
+ if (typeof def.hotwords !== 'boolean') errors.push('hotwords 必须是布尔');
123
+ if (!positiveInt(def.maxSeconds) || def.maxSeconds < MIN_MAX_SECONDS || def.maxSeconds > MAX_MAX_SECONDS) {
124
+ errors.push(`maxSeconds 必须是 ${MIN_MAX_SECONDS}~${MAX_MAX_SECONDS} 的整数`);
125
+ }
126
+ if (!positiveInt(def.maxBytes)) errors.push('maxBytes 必须是正整数');
127
+ if (!Array.isArray(def.configFields)) {
128
+ errors.push('configFields 必须是数组(没有配置项就给空数组)');
129
+ } else if (def.configFields.length > MAX_CONFIG_FIELDS) {
130
+ errors.push(`configFields 超过 ${MAX_CONFIG_FIELDS} 项`);
131
+ } else {
132
+ const seen = new Set();
133
+ for (const field of def.configFields) {
134
+ errors.push(...validateConfigField(field));
135
+ const key = field && typeof field.key === 'string' ? field.key : '';
136
+ if (key && seen.has(key)) errors.push(`configFields 里 ${key} 重复了`);
137
+ if (key) seen.add(key);
138
+ }
139
+ }
140
+ if (typeof def.ready !== 'function') errors.push('缺 ready(config)');
141
+ if (def.batch === true && typeof def.transcribe !== 'function') errors.push('batch:true 但没有 transcribe()');
142
+ if (def.streaming === true && typeof def.stream !== 'function') errors.push('streaming:true 但没有 stream()');
143
+ return { ok: errors.length === 0, errors };
144
+ }
145
+
146
+ /**
147
+ * 能力表的一行(严格按契约裁剪,不带 configFields / 密钥这类只该留在电脑侧的东西)。
148
+ * 面板要的字段另走 `providerConfigView`,不要往这条里塞。
149
+ */
150
+ function capabilityOf(provider, status) {
151
+ const row = {
152
+ id: provider.id,
153
+ name: provider.name,
154
+ batch: provider.batch === true,
155
+ streaming: provider.streaming === true,
156
+ languages: Array.isArray(provider.languages) ? provider.languages.slice() : [],
157
+ hotwords: provider.hotwords === true,
158
+ maxSeconds: provider.maxSeconds,
159
+ maxBytes: provider.maxBytes,
160
+ status: status || PROVIDER_STATUS.UNAVAILABLE,
161
+ };
162
+ // 只读条目(端侧这种跑在手机上的能力):多带几格让 App / 面板能显示「仅手机本地,
163
+ // 不需要密钥、不需要电脑侧配置」,同时用 selectable:false 明确它不是可调用的远端 provider。
164
+ if (provider.capabilityOnly === true) {
165
+ row.capabilityOnly = true;
166
+ row.selectable = false;
167
+ row.localOnly = true;
168
+ row.execution = typeof provider.execution === 'string' && provider.execution ? provider.execution : 'on-device';
169
+ row.requiresKey = false;
170
+ row.requiresHostConfig = false;
171
+ }
172
+ return row;
173
+ }
174
+
175
+ /**
176
+ * 问适配器「现在能不能用」。
177
+ *
178
+ * `ready()` 抛错按不可用处理:适配器读配置 / 探宿主时炸了,不该把整张能力表带崩,也不该
179
+ * 假装可用(那会让 App 画出一个点了就失败的按钮)。
180
+ */
181
+ function isReady(provider, config) {
182
+ if (!provider || typeof provider.ready !== 'function') return false;
183
+ try {
184
+ return provider.ready(config) === true;
185
+ } catch {
186
+ return false;
187
+ }
188
+ }
189
+
190
+ /** 一个 provider 的状态:配置齐了但宿主侧没有 → `unavailable`,配置不齐 → `unconfigured`。 */
191
+ function statusOf(provider, config, hostAvailable = true) {
192
+ if (!hostAvailable) return PROVIDER_STATUS.UNAVAILABLE;
193
+ const declared = Array.isArray(provider.configFields) ? provider.configFields : [];
194
+ const required = declared.filter((field) => field && field.required === true);
195
+ const configObject = isPlainObject(config) ? config : {};
196
+ // 必填项在配置里有没有**非空值**:适配器的 ready() 是最终判据,但先看一眼必填,
197
+ // 免得漏填的适配器把 ready 悄悄判成 true。
198
+ const missing = required.some((field) => {
199
+ const value = configObject[field.key];
200
+ return value === undefined || value === null || value === '';
201
+ });
202
+ if (missing) return PROVIDER_STATUS.UNCONFIGURED;
203
+ return isReady(provider, configObject) ? PROVIDER_STATUS.AVAILABLE : PROVIDER_STATUS.UNCONFIGURED;
204
+ }
205
+
206
+ /**
207
+ * 面板用的配置视图:字段定义 + 当前值(**secret 只给 `set: true/false`,绝不回原文**)。
208
+ * 网关与 registry 不碰这条;它只出现在本机控制面。
209
+ */
210
+ function providerConfigView(provider, config) {
211
+ const current = isPlainObject(config) ? config : {};
212
+ const fields = (Array.isArray(provider.configFields) ? provider.configFields : []).map((field) => {
213
+ const secret = field.secret === true || field.type === 'secret';
214
+ const raw = current[field.key];
215
+ return {
216
+ key: field.key,
217
+ label: field.label,
218
+ type: field.type || (secret ? 'secret' : 'string'),
219
+ secret,
220
+ required: field.required === true,
221
+ ...(Array.isArray(field.options) ? { options: field.options.slice(0, 50) } : {}),
222
+ // 秘密只看「设没设」,非秘密原样回(面板要回显 provider 的 endpoint 之类)。
223
+ ...(secret ? { set: typeof raw === 'string' && raw !== '' } : { value: raw === undefined ? '' : raw }),
224
+ };
225
+ });
226
+ return { id: provider.id, name: provider.name, kind: provider.kind, fields };
227
+ }
228
+
229
+ /**
230
+ * 按 `configFields` 收下用户提交的配置:只认声明过的字段,丢掉别的。
231
+ * 空串 = 清掉该字段(面板里把 key 清空就是取消配置)。
232
+ */
233
+ function pickConfig(provider, input) {
234
+ const source = isPlainObject(input) ? input : {};
235
+ const out = {};
236
+ for (const field of Array.isArray(provider.configFields) ? provider.configFields : []) {
237
+ if (!field || typeof field.key !== 'string') continue;
238
+ if (!Object.prototype.hasOwnProperty.call(source, field.key)) continue;
239
+ const value = source[field.key];
240
+ if (field.type === 'boolean' || typeof value === 'boolean') { out[field.key] = value === true; continue; }
241
+ if (field.type === 'number') {
242
+ const num = Number(value);
243
+ if (Number.isFinite(num)) out[field.key] = num;
244
+ continue;
245
+ }
246
+ if (typeof value === 'string') out[field.key] = value.trim().slice(0, 4096);
247
+ }
248
+ return out;
249
+ }
250
+
251
+ /** 统一的「这条 provider 现在用不了」错误,网关与适配器共用一句话。 */
252
+ function unavailableError(name) {
253
+ return voiceError(VOICE_ERRORS.PROVIDER_UNAVAILABLE, `${name || '该语音服务'}现在不可用`);
254
+ }
255
+
256
+ module.exports = {
257
+ PROVIDER_KINDS,
258
+ PROVIDER_STATUS,
259
+ PROVIDER_ID_PATTERN,
260
+ CONFIG_FIELD_TYPES,
261
+ validateProvider,
262
+ capabilityOf,
263
+ isReady,
264
+ statusOf,
265
+ providerConfigView,
266
+ pickConfig,
267
+ unavailableError,
268
+ };