@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.
Files changed (53) hide show
  1. package/CHANGELOG.md +44 -0
  2. package/CHANGELOG.zh.md +44 -0
  3. package/README.md +81 -86
  4. package/README.zh.md +135 -0
  5. package/dist/build.json +1 -1
  6. package/dist/client.js +202 -28
  7. package/docs/contributing/development.md +51 -28
  8. package/docs/images/plugin-install.png +0 -0
  9. package/docs/images/sonicloud-device.png +0 -0
  10. package/docs/specs/ci-cd.md +24 -37
  11. package/docs/specs/mouse-settings.md +7 -34
  12. package/docs/specs/smart-mouse-plugin.md +51 -32
  13. package/native/README.md +4 -0
  14. package/native/desktop/VOICE_SOURCE.json +16 -0
  15. package/native/desktop/VoicePanel.cs +191 -59
  16. package/native/desktop/VoicePanel.swift +27 -0
  17. package/native/desktop/darwin-arm64/Sonicloud Voice.app/Contents/MacOS/sonicloud-voice +0 -0
  18. package/native/desktop/darwin-arm64/Sonicloud Voice.app/Contents/_CodeSignature/CodeResources +115 -0
  19. package/native/desktop/typer.dll +0 -0
  20. package/native/desktop/windows.ps1 +1 -1
  21. package/native/screenshot/README.md +34 -0
  22. package/native/screenshot/SOURCE.json +218 -0
  23. package/native/screenshot/darwin-arm64/screentool-macos-1.2.2.zip +0 -0
  24. package/native/screenshot/patches/hide-cloud-actions.patch +202 -0
  25. package/native/screenshot/patches/mac-permissions.patch +545 -0
  26. package/native/screenshot/patches/upstream-files.json +22 -0
  27. package/native/screenshot/win32-x64/PixPinScrollCapture.dll +0 -0
  28. package/native/screenshot/win32-x64/Qt5Core.dll +0 -0
  29. package/native/screenshot/win32-x64/Qt5Gui.dll +0 -0
  30. package/native/screenshot/win32-x64/Qt5Network.dll +0 -0
  31. package/native/screenshot/win32-x64/Qt5Svg.dll +0 -0
  32. package/native/screenshot/win32-x64/Qt5Widgets.dll +0 -0
  33. package/native/screenshot/win32-x64/iconengines/qsvgicon.dll +0 -0
  34. package/native/screenshot/win32-x64/imageformats/qjpeg.dll +0 -0
  35. package/native/screenshot/win32-x64/imageformats/qsvg.dll +0 -0
  36. package/native/screenshot/win32-x64/libcrypto-1_1-x64.dll +0 -0
  37. package/native/screenshot/win32-x64/libssl-1_1-x64.dll +0 -0
  38. package/native/screenshot/win32-x64/msvcp140.dll +0 -0
  39. package/native/screenshot/win32-x64/platforms/qwindows.dll +0 -0
  40. package/native/screenshot/win32-x64/qwindowsvistastyle.dll +0 -0
  41. package/native/screenshot/win32-x64/screenhelper.exe +0 -0
  42. package/native/screenshot/win32-x64/screentool.dll +0 -0
  43. package/native/screenshot/win32-x64/vcruntime140.dll +0 -0
  44. package/native/screenshot/win32-x64/vcruntime140_1.dll +0 -0
  45. package/package.json +2 -1
  46. package/src/desktop/presentation.mjs +18 -4
  47. package/src/desktop/screenshot-worker.mjs +225 -0
  48. package/src/desktop/screenshot.mjs +185 -0
  49. package/src/devices/actions.mjs +7 -1
  50. package/src/devices/manager.mjs +145 -81
  51. package/src/host/index.mjs +5 -1
  52. package/src/shared/remote.mjs +5 -0
  53. /package/{src/client/assets → docs/images}/contact-wechat.jpg +0 -0
@@ -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
- const id = event.id;
78
- if (event.type === 'diagnostic') {
79
- if (!this.devices.size) { this.status = event.message; this.snapshot(); }
80
- } else if (event.type === 'connected') {
81
- const previous = this.devices.get(id);
82
- if (previous) this.cancelFor(id);
83
- const stable = isStableSn(id);
84
- const category = event.deviceType ?? 'smart-mouse';
85
- const saved = stable && category === 'smart-mouse' ? await this.store.get(id) : null;
86
- this.devices.set(id, { id, sn: stable ? id : '', type: category, name: event.name || (category === 'smart-mouse' ? '智能鼠标' : '设备'),
87
- connection: event.mode === 1 ? 'ble' : 'usb', connectionId: randomUUID(), battery: null,
88
- identityConfirmed: stable, dpiState: 'unknown', ...saved });
89
- this.clearPressed(id);
90
- this.status = '设备已连接';
91
- if (saved?.revision > 0) await this.applyDpi(this.devices.get(id));
92
- this.snapshot();
93
- } else if (event.type === 'disconnected') {
94
- const device = this.devices.get(id);
95
- if (!device || device.connection !== (event.mode === 1 ? 'ble' : 'usb')) return;
96
- this.cancelFor(id, '设备已断开,语音已取消'); this.devices.delete(id); this.clearPressed(id);
97
- if (!this.devices.size) this.status = '设备已断开,正在重新查找智能鼠标…';
98
- this.snapshot();
99
- } else if (event.type === 'overflow') this.cancelFor(id, '鼠标音频处理过慢,本次识别已停止');
100
- else if (event.type === 'warning') this.publish({ type: 'notice', message: event.message });
101
- else if (event.type === 'message') {
102
- const device = this.devices.get(id), message = event.message;
103
- if (message.type === 'warning') { this.publish({ type: 'notice', message: message.text || '鼠标连接异常' }); return; }
104
- if (message.type === 'queueOverflow') { this.cancel(undefined, '鼠标音频丢失,请重新录音'); return; }
105
- if (!device) return;
106
- if (message.type === 'deviceInfo') {
107
- if (isStableSn(message.sn) && message.sn !== device.sn) {
108
- // Re-key only a temporary identity, never merge two real devices.
109
- if (device.identityConfirmed || this.devices.has(message.sn)) {
110
- device.identityConfirmed = false; this.cancelFor(id, '设备 SN 冲突,请重连');
111
- } else {
112
- this.devices.delete(id); device.sn = message.sn; device.identityConfirmed = true;
113
- if (device.type === 'smart-mouse') Object.assign(device, await this.store.get(message.sn)); this.devices.set(id, device);
114
- }
115
- }
116
- if (Number.isFinite(message.battery) && message.battery >= 0 && message.battery <= 100) device.battery = message.battery;
117
- this.snapshot();
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);
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' || (!this.owner && !this.desktop)) return;
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
- await this.action('openAi');
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
- let text = formatTranscript(run.transcript.finalText(), run.preferences, run.action);
275
- if (!text.trim()) {
276
- this.publish({ type: 'finished', sessionId: run.id });
277
- await this.finish(run);
278
- return;
279
- }
280
- if (run.action === 'translate') {
281
- this.publish({ type: 'phase', sessionId: run.id, phase: 'translating' });
282
- 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 });
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 (run.action === 'ai_input') {
286
- const prepared = await run.aiReady;
287
- if (this.run !== run || run.closing) return;
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({ type: 'finished', sessionId: run.id });
294
- } else if (run.action === 'speechSearch') {
295
- await this.action('openUrl', searchAddress(run.preferences.searchUrl, text));
296
- if (this.run !== run || run.closing) return;
297
- this.publish({ type: 'finished', sessionId: run.id });
298
- } else if (this.desktop) {
299
- await this.desktop.complete(run, text);
300
- } else this.publish({ type: 'result', sessionId: run.id, text });
301
- await this.finish(run);
302
- } catch (error) { this.fail(run, error.message || '语音处理失败,请重试'); }
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
  }
@@ -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
  }
@@ -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' } }),