@sonicloud/dsh-sonicloud-device 0.1.1 → 0.1.3
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 +44 -0
- package/CHANGELOG.zh.md +44 -0
- package/README.md +81 -86
- package/README.zh.md +135 -0
- package/dist/build.json +1 -1
- package/dist/client.js +202 -28
- package/docs/contributing/development.md +51 -28
- 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 +51 -32
- package/native/README.md +4 -0
- package/native/desktop/VOICE_SOURCE.json +16 -0
- package/native/desktop/VoicePanel.cs +191 -59
- package/native/desktop/VoicePanel.swift +27 -0
- package/native/desktop/darwin-arm64/Sonicloud Voice.app/Contents/MacOS/sonicloud-voice +0 -0
- package/native/desktop/darwin-arm64/Sonicloud Voice.app/Contents/_CodeSignature/CodeResources +115 -0
- package/native/desktop/typer.dll +0 -0
- package/native/desktop/windows.ps1 +1 -1
- package/native/screenshot/README.md +34 -0
- package/native/screenshot/SOURCE.json +218 -0
- package/native/screenshot/darwin-arm64/screentool-macos-1.2.2.zip +0 -0
- package/native/screenshot/patches/hide-cloud-actions.patch +202 -0
- package/native/screenshot/patches/mac-permissions.patch +545 -0
- package/native/screenshot/patches/upstream-files.json +22 -0
- package/native/screenshot/win32-x64/PixPinScrollCapture.dll +0 -0
- package/native/screenshot/win32-x64/Qt5Core.dll +0 -0
- package/native/screenshot/win32-x64/Qt5Gui.dll +0 -0
- package/native/screenshot/win32-x64/Qt5Network.dll +0 -0
- package/native/screenshot/win32-x64/Qt5Svg.dll +0 -0
- package/native/screenshot/win32-x64/Qt5Widgets.dll +0 -0
- package/native/screenshot/win32-x64/iconengines/qsvgicon.dll +0 -0
- package/native/screenshot/win32-x64/imageformats/qjpeg.dll +0 -0
- package/native/screenshot/win32-x64/imageformats/qsvg.dll +0 -0
- package/native/screenshot/win32-x64/libcrypto-1_1-x64.dll +0 -0
- package/native/screenshot/win32-x64/libssl-1_1-x64.dll +0 -0
- package/native/screenshot/win32-x64/msvcp140.dll +0 -0
- package/native/screenshot/win32-x64/platforms/qwindows.dll +0 -0
- package/native/screenshot/win32-x64/qwindowsvistastyle.dll +0 -0
- package/native/screenshot/win32-x64/screenhelper.exe +0 -0
- package/native/screenshot/win32-x64/screentool.dll +0 -0
- package/native/screenshot/win32-x64/vcruntime140.dll +0 -0
- package/native/screenshot/win32-x64/vcruntime140_1.dll +0 -0
- package/package.json +2 -1
- package/src/desktop/presentation.mjs +18 -4
- package/src/desktop/screenshot-worker.mjs +225 -0
- package/src/desktop/screenshot.mjs +185 -0
- package/src/devices/actions.mjs +7 -1
- package/src/devices/manager.mjs +145 -81
- package/src/host/index.mjs +5 -1
- package/src/shared/remote.mjs +5 -0
- /package/{src/client/assets → docs/images}/contact-wechat.jpg +0 -0
package/src/devices/manager.mjs
CHANGED
|
@@ -10,8 +10,8 @@ import { translateText } from './translate.mjs';
|
|
|
10
10
|
|
|
11
11
|
const VOICE = new Set(['voice_input', 'translate', 'speechSearch', 'ai_input']);
|
|
12
12
|
export class DeviceManager {
|
|
13
|
-
constructor({ credentials, recognize, prepare = async () => undefined, sdk = new MouseSdk(), action = systemAction, translate = translateText, desktop, aiConversation, config = {} }) {
|
|
14
|
-
Object.assign(this, { sdk, recognize, prepare, action, translate, desktop, aiConversation, config });
|
|
13
|
+
constructor({ credentials, recognize, prepare = async () => undefined, sdk = new MouseSdk(), action = systemAction, translate = translateText, desktop, screenshot, aiConversation, config = {} }) {
|
|
14
|
+
Object.assign(this, { sdk, recognize, prepare, action, translate, desktop, screenshot, aiConversation, config });
|
|
15
15
|
this.store = new DeviceStore(credentials);
|
|
16
16
|
this.devices = new Map(); this.clients = new Set(); this.pressed = new Map(); this.pending = Promise.resolve();
|
|
17
17
|
this.status = '尚未连接 SDK'; this.disposed = false;
|
|
@@ -31,6 +31,8 @@ export class DeviceManager {
|
|
|
31
31
|
desktop.onCancel = id => this.cancel(id);
|
|
32
32
|
desktop.onFault = () => this.cancel(undefined, '桌面语音浮层已断开,请重新触发');
|
|
33
33
|
}
|
|
34
|
+
this.onScreenshotNotice = message => { if (!this.disposed) this.publish({ type: 'notice', message }); };
|
|
35
|
+
screenshot?.on('notice', this.onScreenshotNotice);
|
|
34
36
|
sdk.on('event', this.onEvent); sdk.on('fault', this.onFault);
|
|
35
37
|
}
|
|
36
38
|
async start() {
|
|
@@ -74,59 +76,73 @@ export class DeviceManager {
|
|
|
74
76
|
}
|
|
75
77
|
async event(event) {
|
|
76
78
|
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
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
this.
|
|
128
|
-
|
|
79
|
+
switch (event.type) {
|
|
80
|
+
case 'connected': return this.connectDevice(event);
|
|
81
|
+
case 'disconnected': return this.disconnectDevice(event);
|
|
82
|
+
case 'message': return this.deviceMessage(event);
|
|
83
|
+
case 'overflow': return this.cancelFor(event.id, '鼠标音频处理过慢,本次识别已停止');
|
|
84
|
+
case 'warning': return this.publish({ type: 'notice', message: event.message });
|
|
85
|
+
case 'diagnostic':
|
|
86
|
+
if (!this.devices.size) { this.status = event.message; this.snapshot(); }
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
async connectDevice(event) {
|
|
90
|
+
const { id } = event;
|
|
91
|
+
if (this.devices.has(id)) this.cancelFor(id);
|
|
92
|
+
const stable = isStableSn(id);
|
|
93
|
+
const category = event.deviceType ?? 'smart-mouse';
|
|
94
|
+
const saved = stable && category === 'smart-mouse' ? await this.store.get(id) : null;
|
|
95
|
+
const device = { id, sn: stable ? id : '', type: category, name: event.name || (category === 'smart-mouse' ? '智能鼠标' : '设备'),
|
|
96
|
+
connection: event.mode === 1 ? 'ble' : 'usb', connectionId: randomUUID(), battery: null,
|
|
97
|
+
identityConfirmed: stable, dpiState: 'unknown', ...saved };
|
|
98
|
+
this.devices.set(id, device);
|
|
99
|
+
this.clearPressed(id);
|
|
100
|
+
this.status = '设备已连接';
|
|
101
|
+
if (saved?.revision > 0) await this.applyDpi(device);
|
|
102
|
+
this.snapshot();
|
|
103
|
+
}
|
|
104
|
+
disconnectDevice({ id, mode }) {
|
|
105
|
+
const device = this.devices.get(id);
|
|
106
|
+
if (!device || device.connection !== (mode === 1 ? 'ble' : 'usb')) return;
|
|
107
|
+
this.cancelFor(id, '设备已断开,语音已取消'); this.devices.delete(id); this.clearPressed(id);
|
|
108
|
+
if (!this.devices.size) this.status = '设备已断开,正在重新查找智能鼠标…';
|
|
109
|
+
this.snapshot();
|
|
110
|
+
}
|
|
111
|
+
async deviceMessage({ id, message }) {
|
|
112
|
+
const device = this.devices.get(id);
|
|
113
|
+
if (message.type === 'warning') { this.publish({ type: 'notice', message: message.text || '鼠标连接异常' }); return; }
|
|
114
|
+
if (message.type === 'queueOverflow') { this.cancel(undefined, '鼠标音频丢失,请重新录音'); return; }
|
|
115
|
+
if (!device) return;
|
|
116
|
+
switch (message.type) {
|
|
117
|
+
case 'deviceInfo': return this.updateDeviceInfo(device, message);
|
|
118
|
+
case 'dpiChanged': return this.reportDpi(device, message.level);
|
|
119
|
+
case 'keyEvent': return this.key(device, message);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
async updateDeviceInfo(device, message) {
|
|
123
|
+
if (isStableSn(message.sn) && message.sn !== device.sn) {
|
|
124
|
+
// 只补全临时身份,不合并已确认的设备。
|
|
125
|
+
if (device.identityConfirmed || this.devices.has(message.sn)) {
|
|
126
|
+
device.identityConfirmed = false; this.cancelFor(device.id, '设备 SN 冲突,请重连');
|
|
127
|
+
} else {
|
|
128
|
+
this.devices.delete(device.id); device.sn = message.sn; device.identityConfirmed = true;
|
|
129
|
+
if (device.type === 'smart-mouse') Object.assign(device, await this.store.get(message.sn));
|
|
130
|
+
this.devices.set(device.id, device);
|
|
131
|
+
}
|
|
129
132
|
}
|
|
133
|
+
if (Number.isFinite(message.battery) && message.battery >= 0 && message.battery <= 100) device.battery = message.battery;
|
|
134
|
+
this.snapshot();
|
|
135
|
+
}
|
|
136
|
+
async reportDpi(device, level) {
|
|
137
|
+
if (!Number.isInteger(level) || level < 0 || level > 255) return;
|
|
138
|
+
device.dpiRaw = level;
|
|
139
|
+
// 硬件上报映射与软件下发字节独立。
|
|
140
|
+
const matches = Object.entries(this.dpiReportLevels).filter(([, value]) => value === level);
|
|
141
|
+
if (matches.length === 1 && device.type === 'smart-mouse' && device.identityConfirmed) {
|
|
142
|
+
Object.assign(device, await this.store.reportDpi(device.sn, matches[0][0]));
|
|
143
|
+
device.dpiState = 'reported';
|
|
144
|
+
} else if (device.dpiState === 'reported') device.dpiState = 'unknown';
|
|
145
|
+
this.snapshot();
|
|
130
146
|
}
|
|
131
147
|
clearPressed(id) { for (const key of this.pressed.keys()) if (key.startsWith(id + ':')) this.pressed.delete(key); }
|
|
132
148
|
save(sn, revision, preferences) {
|
|
@@ -158,12 +174,27 @@ export class DeviceManager {
|
|
|
158
174
|
} catch (error) { device.dpiState = 'failed'; return error.message; }
|
|
159
175
|
}
|
|
160
176
|
async key(device, event) {
|
|
161
|
-
if (device.type !== 'smart-mouse'
|
|
177
|
+
if (this.disposed || device.type !== 'smart-mouse') return;
|
|
178
|
+
if (event.key !== 'capture' && !this.owner && !this.desktop) return;
|
|
162
179
|
if (!device.identityConfirmed) {
|
|
163
180
|
if (event.action === 'down' || event.action === 'click') this.publish({ type: 'notice', message: '设备身份未确认,请重新连接鼠标' });
|
|
164
181
|
return;
|
|
165
182
|
}
|
|
166
183
|
const key = `${device.id}:${event.key}:${event.index ?? 0}`;
|
|
184
|
+
if (event.key === 'capture') {
|
|
185
|
+
const pressed = this.pressed.get(key);
|
|
186
|
+
if (event.action === 'up') {
|
|
187
|
+
if (pressed) pressed.releasedAt = Date.now();
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
if (event.action !== 'click' && event.action !== 'down') return;
|
|
191
|
+
// SDK 固件可能同时上报 down 与 click;松键后短暂保留标记以吸收尾随 click。
|
|
192
|
+
if (pressed && (!pressed.releasedAt || (event.action === 'click' && Date.now() - pressed.releasedAt < 300))) return;
|
|
193
|
+
if (event.action === 'down') this.pressed.set(key, { capture: true });
|
|
194
|
+
else this.pressed.delete(key);
|
|
195
|
+
void this.captureScreenshot('mouse').catch(error => this.onScreenshotNotice(error.message));
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
167
198
|
if (event.action === 'up') {
|
|
168
199
|
const pressed = this.pressed.get(key); this.pressed.delete(key);
|
|
169
200
|
if (this.run?.key === key) this.release(this.run);
|
|
@@ -195,10 +226,26 @@ export class DeviceManager {
|
|
|
195
226
|
if (action === 'ai_input') void this.begin(run, device);
|
|
196
227
|
else await this.begin(run, device);
|
|
197
228
|
}
|
|
229
|
+
async captureScreenshot(trigger = 'ui') {
|
|
230
|
+
if (this.disposed) throw new Error('插件已关闭,请重新启用后截图');
|
|
231
|
+
if (!this.screenshot) throw new Error('截图服务不可用,请重新加载插件');
|
|
232
|
+
return this.screenshot.capture(trigger);
|
|
233
|
+
}
|
|
234
|
+
async screenshotPermissions(action = 'check') {
|
|
235
|
+
if (this.disposed) throw new Error('插件已关闭,请重新启用后检查截图权限');
|
|
236
|
+
if (!this.screenshot?.permissions) throw new Error('截图权限检查不可用,请重新加载插件');
|
|
237
|
+
return this.screenshot.permissions(action);
|
|
238
|
+
}
|
|
239
|
+
async typingPermissions(action = 'check') {
|
|
240
|
+
if (this.disposed) throw new Error('插件已关闭,请重新启用后检查打字权限');
|
|
241
|
+
if (!this.desktop?.permissions) throw new Error('打字权限检查不可用,请重新加载插件');
|
|
242
|
+
return this.desktop.permissions(action);
|
|
243
|
+
}
|
|
198
244
|
async prepareAi(run) {
|
|
199
245
|
try {
|
|
200
246
|
if (!this.aiConversation || !this.desktop) throw new Error('AI 对话功能不可用,请重新加载插件');
|
|
201
|
-
|
|
247
|
+
// Activation failure alone must not abort: with the window tray-hidden the renderer lives on and insertion still works.
|
|
248
|
+
try { await this.action('openAi'); } catch { run.aiActivateFailed = true; }
|
|
202
249
|
run.lifetime.signal.throwIfAborted();
|
|
203
250
|
await this.aiConversation.prepare(run);
|
|
204
251
|
return {};
|
|
@@ -271,35 +318,50 @@ export class DeviceManager {
|
|
|
271
318
|
}
|
|
272
319
|
if (this.run !== run || run.closing) return;
|
|
273
320
|
if (!complete || !run.released) throw new Error('ASR 连接中断,识别结果未完成');
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
321
|
+
await this.complete(run);
|
|
322
|
+
} catch (error) { this.fail(run, error.message || '语音处理失败,请重试'); }
|
|
323
|
+
}
|
|
324
|
+
async complete(run) {
|
|
325
|
+
let text = formatTranscript(run.transcript.finalText(), run.preferences, run.action);
|
|
326
|
+
if (!text.trim()) {
|
|
327
|
+
this.publish({ type: 'finished', sessionId: run.id });
|
|
328
|
+
await this.finish(run);
|
|
329
|
+
return;
|
|
330
|
+
}
|
|
331
|
+
if (run.action === 'translate') {
|
|
332
|
+
this.publish({ type: 'phase', sessionId: run.id, phase: 'translating' });
|
|
333
|
+
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 });
|
|
334
|
+
}
|
|
335
|
+
if (this.run !== run || run.closing) return;
|
|
336
|
+
if (run.action === 'ai_input') {
|
|
337
|
+
const prepared = await run.aiReady;
|
|
284
338
|
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 });
|
|
339
|
+
// Keep the complete text available in the native panel if target preparation/insertion fails.
|
|
340
|
+
this.publish({ type: 'preview', sessionId: run.id, text });
|
|
341
|
+
try {
|
|
290
342
|
if (prepared.error) throw prepared.error;
|
|
291
343
|
await this.aiConversation.insert(run, text);
|
|
292
344
|
if (this.run !== run || run.closing) return;
|
|
293
|
-
this.publish(
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
345
|
+
this.publish(run.aiActivateFailed
|
|
346
|
+
? { type: 'failed', sessionId: run.id, message: '窗口未唤起,文字已填入对话草稿,可复制结果' }
|
|
347
|
+
: { type: 'finished', sessionId: run.id });
|
|
348
|
+
} catch (error) {
|
|
349
|
+
// On Windows the panel keeps the text so the user can focus any input and submit it there.
|
|
350
|
+
if (this.desktop?.platform === 'win32') {
|
|
351
|
+
this.publish({ type: 'submitOffer', sessionId: run.id, text, message: '请打开需要输入界面' });
|
|
352
|
+
await this.finish(run);
|
|
353
|
+
return;
|
|
354
|
+
}
|
|
355
|
+
throw error;
|
|
356
|
+
}
|
|
357
|
+
} else if (run.action === 'speechSearch') {
|
|
358
|
+
await this.action('openUrl', searchAddress(run.preferences.searchUrl, text));
|
|
359
|
+
if (this.run !== run || run.closing) return;
|
|
360
|
+
this.publish({ type: 'finished', sessionId: run.id });
|
|
361
|
+
} else if (this.desktop) {
|
|
362
|
+
await this.desktop.complete(run, text);
|
|
363
|
+
} else this.publish({ type: 'result', sessionId: run.id, text });
|
|
364
|
+
await this.finish(run);
|
|
303
365
|
}
|
|
304
366
|
fail(run, message) {
|
|
305
367
|
if (this.run !== run || run.closing) return;
|
|
@@ -328,7 +390,9 @@ export class DeviceManager {
|
|
|
328
390
|
for (const queue of this.clients) queue.end(true);
|
|
329
391
|
this.clients.clear(); this.owner = undefined;
|
|
330
392
|
this.sdk.off('event', this.onEvent); this.sdk.off('fault', this.onFault);
|
|
393
|
+
this.screenshot?.off('notice', this.onScreenshotNotice);
|
|
331
394
|
this.aiConversation?.close();
|
|
395
|
+
try { await this.screenshot?.close(); } catch { /* 截图进程退出失败不阻断设备和语音清理。 */ }
|
|
332
396
|
await this.desktop?.close();
|
|
333
397
|
await this.sdk.close();
|
|
334
398
|
}
|
package/src/host/index.mjs
CHANGED
|
@@ -7,6 +7,7 @@ import { TYPERT } from '../shared/remote.mjs';
|
|
|
7
7
|
import { DeviceManager } from '../devices/manager.mjs';
|
|
8
8
|
import { MouseSdk } from '../devices/native.mjs';
|
|
9
9
|
import { DesktopPresentation } from '../desktop/presentation.mjs';
|
|
10
|
+
import { ScreenshotService } from '../desktop/screenshot.mjs';
|
|
10
11
|
import { pluginConfigSchema } from '../devices/config.mjs';
|
|
11
12
|
import { AiConversationBridge } from './ai-conversation.mjs';
|
|
12
13
|
|
|
@@ -34,6 +35,9 @@ export class MouseController extends TypertRemoteService {
|
|
|
34
35
|
async service() { await this.initialized; return { ...await this.store.resolve(), ...this.dependencies.endpoints }; }
|
|
35
36
|
catalog() { return this.store.catalog(); }
|
|
36
37
|
devices() { return this.manager.view(); }
|
|
38
|
+
captureScreenshot() { return this.manager.captureScreenshot(); }
|
|
39
|
+
screenshotPermissions(action) { return this.manager.screenshotPermissions(action); }
|
|
40
|
+
typingPermissions(action) { return this.manager.typingPermissions(action); }
|
|
37
41
|
saveDevice(sn, revision, preferences) { return this.manager.save(sn, revision, preferences); }
|
|
38
42
|
watch(clientId, signal) { return this.manager.watch(clientId, signal); }
|
|
39
43
|
accept(clientId, sessionId) { return this.manager.accept(clientId, sessionId); }
|
|
@@ -90,7 +94,7 @@ export class MouseController extends TypertRemoteService {
|
|
|
90
94
|
}
|
|
91
95
|
export async function apply(ctx, config = {}) {
|
|
92
96
|
ctx.effect(() => ctx.typert.register(TYPERT));
|
|
93
|
-
const controller = new MouseController(ctx, config, { desktop: new DesktopPresentation() });
|
|
97
|
+
const controller = new MouseController(ctx, config, { desktop: new DesktopPresentation(), screenshot: new ScreenshotService() });
|
|
94
98
|
await controller.initialized;
|
|
95
99
|
void controller.manager.start();
|
|
96
100
|
}
|
package/src/shared/remote.mjs
CHANGED
|
@@ -37,6 +37,11 @@ export const REMOTE = {
|
|
|
37
37
|
descriptors: [
|
|
38
38
|
define('catalog', [], catalog),
|
|
39
39
|
define('devices', [], devices),
|
|
40
|
+
define('captureScreenshot', [], z.boolean()),
|
|
41
|
+
define('screenshotPermissions', [['action', z.enum(['check', 'screen', 'accessibility', 'refresh'])]],
|
|
42
|
+
z.object({ supported: z.boolean(), screenRecording: z.boolean(), accessibility: z.boolean(), helperApp: text }).strict()),
|
|
43
|
+
define('typingPermissions', [['action', z.enum(['check', 'accessibility', 'refresh'])]],
|
|
44
|
+
z.object({ supported: z.boolean(), accessibility: z.boolean(), helperApp: text }).strict()),
|
|
40
45
|
define('saveDevice', [['sn', snSchema], ['revision', z.number().int().nonnegative()], ['preferences', preferencesSchema]],
|
|
41
46
|
z.object({ revision: z.number(), preferences: preferencesSchema, message: text })),
|
|
42
47
|
define('watch', [['clientId', id]], event, { mode: 'stream', cancellation: { parameter: 'signal' } }),
|
|
File without changes
|