@sonicloud/dsh-sonicloud-device 0.1.1 → 0.1.2
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/CHANGELOG.md +16 -0
- package/CHANGELOG.zh.md +16 -0
- package/README.md +67 -95
- package/README.zh.md +112 -0
- package/dist/build.json +1 -1
- package/dist/client.js +103 -28
- package/docs/contributing/development.md +24 -29
- package/docs/images/plugin-install.png +0 -0
- package/docs/images/sonicloud-device.png +0 -0
- package/docs/specs/ci-cd.md +24 -37
- package/docs/specs/mouse-settings.md +7 -34
- package/docs/specs/smart-mouse-plugin.md +23 -34
- package/native/desktop/VoicePanel.cs +191 -59
- package/native/desktop/typer.dll +0 -0
- package/native/desktop/windows.ps1 +1 -1
- package/package.json +2 -1
- package/src/devices/actions.mjs +7 -1
- package/src/devices/manager.mjs +108 -78
- /package/{src/client/assets → docs/images}/contact-wechat.jpg +0 -0
package/src/devices/manager.mjs
CHANGED
|
@@ -74,60 +74,74 @@ export class DeviceManager {
|
|
|
74
74
|
}
|
|
75
75
|
async event(event) {
|
|
76
76
|
if (this.disposed) return;
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
} else if (message.type === 'dpiChanged') {
|
|
119
|
-
if (!Number.isInteger(message.level) || message.level < 0 || message.level > 255) return;
|
|
120
|
-
device.dpiRaw = message.level;
|
|
121
|
-
// Hardware broadcasts use a separate mapping from software command bytes.
|
|
122
|
-
const matches = Object.entries(this.dpiReportLevels).filter(([, level]) => level === message.level);
|
|
123
|
-
if (matches.length === 1 && device.type === 'smart-mouse' && device.identityConfirmed) {
|
|
124
|
-
Object.assign(device, await this.store.reportDpi(device.sn, matches[0][0]));
|
|
125
|
-
device.dpiState = 'reported';
|
|
126
|
-
} else if (device.dpiState === 'reported') device.dpiState = 'unknown';
|
|
127
|
-
this.snapshot();
|
|
128
|
-
} else if (message.type === 'keyEvent') await this.key(device, message);
|
|
77
|
+
switch (event.type) {
|
|
78
|
+
case 'connected': return this.connectDevice(event);
|
|
79
|
+
case 'disconnected': return this.disconnectDevice(event);
|
|
80
|
+
case 'message': return this.deviceMessage(event);
|
|
81
|
+
case 'overflow': return this.cancelFor(event.id, '鼠标音频处理过慢,本次识别已停止');
|
|
82
|
+
case 'warning': return this.publish({ type: 'notice', message: event.message });
|
|
83
|
+
case 'diagnostic':
|
|
84
|
+
if (!this.devices.size) { this.status = event.message; this.snapshot(); }
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
async connectDevice(event) {
|
|
88
|
+
const { id } = event;
|
|
89
|
+
if (this.devices.has(id)) this.cancelFor(id);
|
|
90
|
+
const stable = isStableSn(id);
|
|
91
|
+
const category = event.deviceType ?? 'smart-mouse';
|
|
92
|
+
const saved = stable && category === 'smart-mouse' ? await this.store.get(id) : null;
|
|
93
|
+
const device = { id, sn: stable ? id : '', type: category, name: event.name || (category === 'smart-mouse' ? '智能鼠标' : '设备'),
|
|
94
|
+
connection: event.mode === 1 ? 'ble' : 'usb', connectionId: randomUUID(), battery: null,
|
|
95
|
+
identityConfirmed: stable, dpiState: 'unknown', ...saved };
|
|
96
|
+
this.devices.set(id, device);
|
|
97
|
+
this.clearPressed(id);
|
|
98
|
+
this.status = '设备已连接';
|
|
99
|
+
if (saved?.revision > 0) await this.applyDpi(device);
|
|
100
|
+
this.snapshot();
|
|
101
|
+
}
|
|
102
|
+
disconnectDevice({ id, mode }) {
|
|
103
|
+
const device = this.devices.get(id);
|
|
104
|
+
if (!device || device.connection !== (mode === 1 ? 'ble' : 'usb')) return;
|
|
105
|
+
this.cancelFor(id, '设备已断开,语音已取消'); this.devices.delete(id); this.clearPressed(id);
|
|
106
|
+
if (!this.devices.size) this.status = '设备已断开,正在重新查找智能鼠标…';
|
|
107
|
+
this.snapshot();
|
|
108
|
+
}
|
|
109
|
+
async deviceMessage({ id, message }) {
|
|
110
|
+
const device = this.devices.get(id);
|
|
111
|
+
if (message.type === 'warning') { this.publish({ type: 'notice', message: message.text || '鼠标连接异常' }); return; }
|
|
112
|
+
if (message.type === 'queueOverflow') { this.cancel(undefined, '鼠标音频丢失,请重新录音'); return; }
|
|
113
|
+
if (!device) return;
|
|
114
|
+
switch (message.type) {
|
|
115
|
+
case 'deviceInfo': return this.updateDeviceInfo(device, message);
|
|
116
|
+
case 'dpiChanged': return this.reportDpi(device, message.level);
|
|
117
|
+
case 'keyEvent': return this.key(device, message);
|
|
129
118
|
}
|
|
130
119
|
}
|
|
120
|
+
async updateDeviceInfo(device, message) {
|
|
121
|
+
if (isStableSn(message.sn) && message.sn !== device.sn) {
|
|
122
|
+
// 只补全临时身份,不合并已确认的设备。
|
|
123
|
+
if (device.identityConfirmed || this.devices.has(message.sn)) {
|
|
124
|
+
device.identityConfirmed = false; this.cancelFor(device.id, '设备 SN 冲突,请重连');
|
|
125
|
+
} else {
|
|
126
|
+
this.devices.delete(device.id); device.sn = message.sn; device.identityConfirmed = true;
|
|
127
|
+
if (device.type === 'smart-mouse') Object.assign(device, await this.store.get(message.sn));
|
|
128
|
+
this.devices.set(device.id, device);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
if (Number.isFinite(message.battery) && message.battery >= 0 && message.battery <= 100) device.battery = message.battery;
|
|
132
|
+
this.snapshot();
|
|
133
|
+
}
|
|
134
|
+
async reportDpi(device, level) {
|
|
135
|
+
if (!Number.isInteger(level) || level < 0 || level > 255) return;
|
|
136
|
+
device.dpiRaw = level;
|
|
137
|
+
// 硬件上报映射与软件下发字节独立。
|
|
138
|
+
const matches = Object.entries(this.dpiReportLevels).filter(([, value]) => value === level);
|
|
139
|
+
if (matches.length === 1 && device.type === 'smart-mouse' && device.identityConfirmed) {
|
|
140
|
+
Object.assign(device, await this.store.reportDpi(device.sn, matches[0][0]));
|
|
141
|
+
device.dpiState = 'reported';
|
|
142
|
+
} else if (device.dpiState === 'reported') device.dpiState = 'unknown';
|
|
143
|
+
this.snapshot();
|
|
144
|
+
}
|
|
131
145
|
clearPressed(id) { for (const key of this.pressed.keys()) if (key.startsWith(id + ':')) this.pressed.delete(key); }
|
|
132
146
|
save(sn, revision, preferences) {
|
|
133
147
|
// Keep UI saves and hardware reports in order so neither overwrites newer state.
|
|
@@ -198,7 +212,8 @@ export class DeviceManager {
|
|
|
198
212
|
async prepareAi(run) {
|
|
199
213
|
try {
|
|
200
214
|
if (!this.aiConversation || !this.desktop) throw new Error('AI 对话功能不可用,请重新加载插件');
|
|
201
|
-
|
|
215
|
+
// Activation failure alone must not abort: with the window tray-hidden the renderer lives on and insertion still works.
|
|
216
|
+
try { await this.action('openAi'); } catch { run.aiActivateFailed = true; }
|
|
202
217
|
run.lifetime.signal.throwIfAborted();
|
|
203
218
|
await this.aiConversation.prepare(run);
|
|
204
219
|
return {};
|
|
@@ -271,35 +286,50 @@ export class DeviceManager {
|
|
|
271
286
|
}
|
|
272
287
|
if (this.run !== run || run.closing) return;
|
|
273
288
|
if (!complete || !run.released) throw new Error('ASR 连接中断,识别结果未完成');
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
289
|
+
await this.complete(run);
|
|
290
|
+
} catch (error) { this.fail(run, error.message || '语音处理失败,请重试'); }
|
|
291
|
+
}
|
|
292
|
+
async complete(run) {
|
|
293
|
+
let text = formatTranscript(run.transcript.finalText(), run.preferences, run.action);
|
|
294
|
+
if (!text.trim()) {
|
|
295
|
+
this.publish({ type: 'finished', sessionId: run.id });
|
|
296
|
+
await this.finish(run);
|
|
297
|
+
return;
|
|
298
|
+
}
|
|
299
|
+
if (run.action === 'translate') {
|
|
300
|
+
this.publish({ type: 'phase', sessionId: run.id, phase: 'translating' });
|
|
301
|
+
text = await this.translate({ sn: run.sn, service: run.service, text, from: run.preferences.from, to: run.preferences.to, signal: run.lifetime.signal, config: this.config.translation });
|
|
302
|
+
}
|
|
303
|
+
if (this.run !== run || run.closing) return;
|
|
304
|
+
if (run.action === 'ai_input') {
|
|
305
|
+
const prepared = await run.aiReady;
|
|
284
306
|
if (this.run !== run || run.closing) return;
|
|
285
|
-
if
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
// Keep the complete text available in the native panel if target preparation/insertion fails.
|
|
289
|
-
this.publish({ type: 'preview', sessionId: run.id, text });
|
|
307
|
+
// Keep the complete text available in the native panel if target preparation/insertion fails.
|
|
308
|
+
this.publish({ type: 'preview', sessionId: run.id, text });
|
|
309
|
+
try {
|
|
290
310
|
if (prepared.error) throw prepared.error;
|
|
291
311
|
await this.aiConversation.insert(run, text);
|
|
292
312
|
if (this.run !== run || run.closing) return;
|
|
293
|
-
this.publish(
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
313
|
+
this.publish(run.aiActivateFailed
|
|
314
|
+
? { type: 'failed', sessionId: run.id, message: '窗口未唤起,文字已填入对话草稿,可复制结果' }
|
|
315
|
+
: { type: 'finished', sessionId: run.id });
|
|
316
|
+
} catch (error) {
|
|
317
|
+
// On Windows the panel keeps the text so the user can focus any input and submit it there.
|
|
318
|
+
if (this.desktop?.platform === 'win32') {
|
|
319
|
+
this.publish({ type: 'submitOffer', sessionId: run.id, text, message: '请打开需要输入界面' });
|
|
320
|
+
await this.finish(run);
|
|
321
|
+
return;
|
|
322
|
+
}
|
|
323
|
+
throw error;
|
|
324
|
+
}
|
|
325
|
+
} else if (run.action === 'speechSearch') {
|
|
326
|
+
await this.action('openUrl', searchAddress(run.preferences.searchUrl, text));
|
|
327
|
+
if (this.run !== run || run.closing) return;
|
|
328
|
+
this.publish({ type: 'finished', sessionId: run.id });
|
|
329
|
+
} else if (this.desktop) {
|
|
330
|
+
await this.desktop.complete(run, text);
|
|
331
|
+
} else this.publish({ type: 'result', sessionId: run.id, text });
|
|
332
|
+
await this.finish(run);
|
|
303
333
|
}
|
|
304
334
|
fail(run, message) {
|
|
305
335
|
if (this.run !== run || run.closing) return;
|
|
File without changes
|