@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.
@@ -4,13 +4,13 @@
4
4
 
5
5
  目标宿主:DeepSeek Harness `0.2.1-rc.5`。包名:`@sonicloud/dsh-sonicloud-device`;插件 ID:`dsh-sonicloud-device`。当前版本以 `package.json` 为准。
6
6
 
7
- 本期接入通过 BLE 或 2.4G USB 接收器连接的智能鼠标,提供多设备设置、语音打字、翻译、搜索、DPI 与按键配置。设备模型预留其他类型的扩展能力。
7
+ 接入通过 BLE 或 2.4G USB 接收器连接的智能鼠标,提供多设备设置、语音打字、翻译、搜索、DPI 与按键配置。设备模型预留其他类型的扩展能力。
8
8
 
9
- 语音会话由插件后台管理,录音和识别不依赖 Harness 主界面、对话页或前端订阅。AI 键通过宿主前端创建对话和填充输入框,连接不可用时保留结果供复制。独立原生浮层显示中间结果;主窗口最小化或隐藏时仍可用,完全退出宿主后后台停止。本次不提供独立于宿主进程的安装程序。
9
+ 语音会话由插件后台管理,录音和识别不依赖 Harness 主界面、对话页或前端订阅。AI 键通过宿主前端创建对话和填充输入框,连接不可用时保留结果供复制。独立原生浮层显示中间结果;主窗口最小化或隐藏时仍可用,完全退出宿主后后台停止。不提供独立于宿主进程的安装程序。
10
10
 
11
- 语音仅使用鼠标麦克风;移除输入框长按录音及电脑麦克风采集,保留宿主点击、长按和拖选行为。仅 AI 键支持可关闭的自动发送;不包含语音播报、音频导入、历史录音和会议管理。
11
+ 语音仅使用鼠标麦克风,不改变宿主输入框的点击、长按和拖选行为。仅 AI 键支持可关闭的自动发送;不包含语音播报、音频导入、历史录音和会议管理。
12
12
 
13
- 本文定义目标要求;实现状态见第 9 节。设置选项、默认值、语言映射统一维护在[鼠标设置](mouse-settings.md)。
13
+ 本文定义行为契约与验收要求;实现状态见第 9 节。设置选项、默认值、语言映射统一维护在[鼠标设置](mouse-settings.md)。
14
14
 
15
15
  ## 2. 设备与配置
16
16
 
@@ -31,7 +31,7 @@
31
31
 
32
32
  ### 3.1 页面
33
33
 
34
- 以用户提供的截图为布局基准,复用宿主设置组件、主题和滚动行为。侧栏仅保留“声云设备”入口,不展示 ASR 菜单、服务管理或鉴权配置。
34
+ 复用宿主设置组件、主题和滚动行为。侧栏仅保留“声云设备”入口,不展示 ASR 菜单、服务管理或鉴权配置。
35
35
 
36
36
  | 区域 | 要求 |
37
37
  | --- | --- |
@@ -40,7 +40,9 @@
40
40
  | 基本信息 | 依次显示 SN、连接方式、电量;连接方式显示“蓝牙”或“USB 接收器”。 |
41
41
  | 无设备/离线 | 无设备时显示“暂无设备连接”及连接提示;当前设备离线后停止下发指令,允许切换其他在线设备。 |
42
42
 
43
- 截图数据不作为真实状态或默认值。不支持的设置禁用并说明原因;DPI、M、语音及 AI 键在标题下方以通用设置的小号灰字说明用途。M 键的搜索引擎仅显示下拉选择框,不显示文字标签;链接输入框右侧提供“确认”按钮,不显示地址标签或行内校验提示。子配置与 M 键之间不加分割线。
43
+ 不支持的设置禁用并说明原因;DPI、M、语音及 AI 键在标题下方以通用设置的小号灰字说明用途。M 键子控件的显示和确认规则见[鼠标设置](mouse-settings.md#m-键设置)。子配置与 M 键之间不加分割线。
44
+
45
+ 标题行右侧提供“侧边栏”开关:默认开启,在左侧导航栏注册 `sidebar.panellist` 行与 `main` 同 id 全局面板(内容复用本页组件)。面板页面遵循宿主插件页结构:根容器占满主区域并自带纵向滚动,内容限宽居中,顶部安全距离仅在 macOS 追加。开关状态持久化在本机 localStorage,按机器记忆;关闭仅注销侧边栏注册,不影响设置页;面板被隐藏时若正处于选中状态,由宿主布局自动回退到对话页。
44
46
 
45
47
  ### 3.2 设置项
46
48
 
@@ -63,7 +65,7 @@ DPI 保存值须映射为固件档位;首次保存前不下发默认 DPI,重
63
65
 
64
66
  ### 3.3 M 键动作
65
67
 
66
- 动作值、菜单顺序和搜索引擎列表统一见[鼠标设置](mouse-settings.md#m-键设置)。
68
+ 动作值、菜单顺序和搜索引擎列表统一见[鼠标设置](mouse-settings.md#m-键设置)。M 键语音动作按 SDK `down` 开始、`up` 结束,其他动作在 `up` 执行;重复 `hold` 不重复触发。
67
69
 
68
70
  | 动作 | 行为 |
69
71
  | --- | --- |
@@ -73,40 +75,38 @@ DPI 保存值须映射为固件档位;首次保存前不下发默认 DPI,重
73
75
  | 语音搜索 | 按语音打字配置识别,将完整最终文本经 URL 查询参数编码后追加到所选搜索引擎前缀,打开一次。 |
74
76
  | 一键链接 | 打开已配置的 HTTP/HTTPS 地址,不采集音频。 |
75
77
 
76
- 搜索和链接不依赖输入框、不回填文本。链接仅在点击“确认”时去除首尾空白、校验网址(HTTP/HTTPS 协议可省略,保存和打开均不补写协议),保存成功后 toast 提示并用于后续按键;缺失、无效或保存失败时 toast 报错,当前修改不生效。搜索仅在识别成功且文本非空时执行;浏览器打开失败时提示,不重复打开或换用其他地址。
78
+ 搜索和链接不依赖输入框、不回填文本。链接校验、确认和保存反馈见[鼠标设置](mouse-settings.md#一键链接)。搜索仅在识别成功且文本非空时执行;浏览器打开失败时提示,不重复打开或换用其他地址。
77
79
 
78
80
  ### 3.4 AI 键动作
79
81
 
80
- AI 键仅作用于 DeepSeek Harness,语音结果定向填充 AI 对话输入框,不写入其他应用或其他输入框。设置文案与保留的配置值见[鼠标设置](mouse-settings.md#语音键与-ai-键)。
82
+ AI 键仅作用于已运行且插件已启用的 DeepSeek Harness,语音结果定向填充 AI 对话输入框,不主动写入其他应用或其他输入框(Windows 填充不可达时的“一键提交”回退见本节末段);宿主完全退出后无法触发。设置文案与保留的配置值见[鼠标设置](mouse-settings.md#语音键与-ai-键)。
81
83
 
82
- 长按 AI 键开始语音打字,松开结束录音;使用来源设备的识别语言和标点配置。触发时按以下规则确定目标:
84
+ AI 键按 SDK `ai down/up` 开始和结束录音,忽略重复 `down`、`hold` 和 `click`;使用来源设备的识别语言和标点配置。触发时按以下规则确定目标:
83
85
 
84
86
  | DeepSeek Harness 状态 | 行为 |
85
87
  | --- | --- |
86
88
  | 位于前台且在 AI 对话页 | 使用当前对话,不新建对话;无论输入框是否获得焦点,最终识别文本均自动填充到该对话输入框。 |
87
89
  | 位于前台但不在 AI 对话页 | 新建对话并进入 AI 对话页,最终识别文本自动填充到新对话输入框。 |
88
- | 不在前台,包括隐藏或最小化 | 自动将 DeepSeek Harness 调至前台,再按上述页面规则使用当前对话或新建对话,并自动填充。 |
90
+ | 不在前台,包括隐藏或最小化 | 自动将 DeepSeek Harness 调至前台,再按上述页面规则使用当前对话或新建对话,并自动填充。Windows 唤起分两级:有可见窗口句柄时直接 `ShowWindow` 还原(含最小化)并置前;托盘隐藏或窗口已销毁时,剥离 node 模式环境变量(`ELECTRON_RUN_AS_NODE` 等)后二次启动运行中的程序,由宿主单实例处理器在应用内部还原或重建窗口,`dsh://` 协议仅作兜底。 |
89
91
 
90
92
  设置弹窗打开时视为非对话页;先关闭设置,再新建对话。新对话归属当前对话所在工作区;无法确定时使用当前工作目录对应或最近使用的工作区,无工作区时尝试宿主默认初始化。没有可用工作区则提示失败并保留识别结果,不创建不可输入的空白对话。页面切换后等待目标输入组件就绪再填充。
91
93
 
92
94
  填充不以系统焦点或 AI 对话输入框焦点为前提,无需用户预先点击输入框。每次操作固定目标对话,最多填充一次;已有草稿和附件保留,识别文字追加到末尾。录音期间切换页面或对话,完成后仍回到本次目标对话并填充,不将结果写入其他对话或应用。
93
95
 
94
- 唤起、创建对话、填充或调用发送接口失败时提示并保留已识别结果供复制。对话正在提交、前端断开或操作超时,不强行覆盖草稿、不重新分配给其他窗口;创建或填充的单次前端请求最多等待 10 秒。取消、设备掉线或插件禁用后不处理迟到结果。
95
-
96
- 松开后完成识别并自动填充。“自动发送”开启时,通过目标输入组件的宿主发送接口提交追加语音后的完整草稿(含已有文字及附件),每次语音会话最多调用一次;关闭时保留草稿,不调用发送接口。开关默认值、存储与生效时机见[鼠标设置](mouse-settings.md#语音键与-ai-键)。空结果、取消、识别失败或填充失败均不发送;发送由宿主负责校验、排队和错误提示,插件不自动重试。
96
+ 唤起失败本身不中断填充:托盘隐藏时渲染进程仍在,对话照常创建与填充,浮层提示“窗口未唤起,文字已填入对话草稿,可复制结果”。唤起后创建对话、填充或调用发送接口失败时提示并保留已识别结果供复制。对话正在提交、前端断开或操作超时,不强行覆盖草稿、不重新分配给其他窗口;创建或填充的单次前端请求最多等待 10 秒。取消、设备掉线或插件禁用后不处理迟到结果。
97
97
 
98
- ### 3.5 AI 键适用边界
98
+ Windows 回退:目标对话填充失败时,浮层保留完整识别文本并提示“请打开需要输入界面”,按钮区提供“一键提交”;用户把焦点放到任意应用的输入框后点击,文本经 typer 逐字注入点击时聚焦的窗口(注入、密码框核对与提权拒绝规则同第 4.2 节 Windows 条目)。回退浮层不自动隐藏,关闭、提交或下一次录音开始时清除;“自动发送”在回退中不适用,仅注入文本。macOS 无此回退,保持提示加复制。
99
99
 
100
- 以上唤起规则适用于 DeepSeek Harness 已运行且插件后台可接收设备事件的情况。当前后台随宿主退出,不支持宿主完全退出后通过 AI 键启动。
100
+ 松开后完成识别并自动填充。“自动发送”开启时,通过目标输入组件的宿主发送接口提交追加语音后的完整草稿(含已有文字及附件),每次语音会话最多调用一次;关闭时保留草稿,不调用发送接口。开关默认值、存储与生效时机见[鼠标设置](mouse-settings.md#语音键与-ai-键)。空结果、取消、识别失败或填充失败均不发送;发送由宿主负责校验、排队和错误提示,插件不自动重试。
101
101
 
102
102
  ## 4. 语音会话与输出
103
103
 
104
104
  ### 4.1 调度与流程
105
105
 
106
- 语音键、M 键和 AI 键共用单会话调度,覆盖连接、录音、识别、翻译或搜索全过程。AI 键的宿主唤起与输出目标遵循第 3.4 节。
106
+ 语音键、M 键和 AI 键共用单会话调度,覆盖连接、录音、识别、翻译或搜索全过程;独立翻译键映射为翻译动作。AI 键的宿主唤起与输出目标遵循第 3.4 节。
107
107
 
108
108
  - 开始时固定来源 SN、连接、触发键、动作、配置和输出目标,并锁定 SDK 音频源;只接收该来源音频,空闲音频不上传。
109
- - 忙碌时其他设备触发提示“其他设备正在使用语音,请稍后重试”,不抢占、不排队;当前设备重复触发不新建会话。
109
+ - 忙碌时其他设备触发提示“其他语音会话正在进行,请稍后重试”,不抢占、不排队;当前设备重复触发不新建会话。
110
110
  - 仅所属设备及触发键的结束事件可以结束录音;页面切换、其他设备事件和设置修改不影响当前会话。一次物理操作只执行一次动作。
111
111
 
112
112
  | 阶段 | 行为 |
@@ -125,9 +125,9 @@ AI 键仅作用于 DeepSeek Harness,语音结果定向填充 AI 对话输入
125
125
 
126
126
  以下系统输入框回填与焦点校验规则适用于语音键和 M 键;AI 键按第 3.4 节定向填充。会话取消、最终文本为空及识别失败的处理适用于所有语音触发键。
127
127
 
128
- - 回填不依赖 Harness 输入框插槽,不强行切回原应用、不自动发送。目标变化、权限不足、已识别的密码框或无法安全回填时保留结果供复制。
128
+ - 回填不依赖 Harness 输入框插槽,不强行切回原应用、不自动发送。权限不足、已识别的密码框或无法注入时保留结果供复制。
129
129
  - macOS 默认使用兼容粘贴,不要求应用开放辅助功能写入接口。回填前核对前台进程、窗口、标题、文档和焦点;可读取的内容与选区也须一致,且录音后无键盘、修饰键、鼠标点击或滚动活动。临时保存剪贴板全部数据类型,粘贴后恢复;其间剪贴板已更新则不覆盖。正常退出清理临时剪贴板。不提供内容与选区接口的应用无法核对外部程序造成的内容或选区变化;快捷键已投递不代表应用一定接受粘贴。
130
- - Windows 仍使用 UI Automation 写入可编辑文本;回填前核对焦点、内容和选区。不支持该接口的控件保留结果供复制。
130
+ - Windows 使用 typer.dll 标准注入库(纯 C ABI,随包分发于 `native/desktop/typer.dll`,x64):会话式逐字注入(SendInput Unicode),说话期间的中间结果实时打进当前焦点窗口并以退格增量更正,逐字模式不读取、不写入剪贴板。翻译仅在译文就绪后逐字打出,不实时注入原文。开始前核对焦点元素密码标记(UIA 不可达时放行);密码框、库缺失、加载失败或注入被拒(如提权窗口 UIPI)时不打字,浮层显示“未回填:原因,可复制结果”。取消与异常路径立即中止会话并清空未打队列;最终文本为空时以空增量撤回已打出的中间结果。会话期间注入目标是当前焦点窗口,浮窗自身不抢焦点。
131
131
  - 打字仅回填识别文本,翻译仅回填有效译文;每个会话最多输出一次。成功后隐藏浮层,无法回填及异常结果保留供复制。
132
132
  - 识别完成但最终文本为空时直接隐藏浮层,不显示“未识别到语音”,不回填、不翻译、不搜索。识别失败、音频丢失、断线或超时不输出不完整结果,也不进入后续翻译/搜索;翻译失败保留识别原文,不冒充译文。不得自动切换设备或服务。
133
133
 
@@ -218,22 +218,11 @@ ASR 成功后请求一次,不翻译临时句。仅 HTTP 成功、`code=0` 且
218
218
 
219
219
  - 硬件:M 键时序、尾帧边界、DPI 固件映射、系统按键效果、设备名称/颜色/图片来源。
220
220
  - 产品:默认值是否按型号区分,异常重复设备身份的处置。
221
- - AI 键联调:真实鼠标长按、松开及尾音;后台、隐藏、最小化窗口的唤起和填充,自动发送开关的实机行为及 Windows 实机体验。
221
+ - AI 键联调:真实鼠标长按、松开及尾音;后台、隐藏、最小化及托盘隐藏窗口的唤起和填充,填充失败时“一键提交”回退注入任意应用输入框,自动发送开关的实机行为及 Windows 实机体验。
222
222
  - 服务与平台:真实设备鉴权及 License 行为、语言/语言对实网验收、实际连接上限、多 BLE 链路、Windows 及其他目标平台。
223
223
 
224
224
  ## 9. 实现状态
225
225
 
226
- 截至 2026-11-09,已实现下列能力;自动化验证不代表真实设备或实网验收。
226
+ 截至 2026-11-12,已实现设备设置与偏好持久化、SDK 接入、后台单语音会话、M/AI 键动作、DPI 同步、桌面浮层、系统输入框回填、侧边栏声云设备面板、Windows typer.dll 会话式逐字注入(实时更正、不动剪贴板)、AI 键唤回加固(还原最小化窗口、托盘隐藏时二次启动还原)及填充失败的“一键提交”回退。真机已验证 AI 键目标对话填充、自动发送与最小化/托盘唤回;Windows 逐字注入清单(实时出字、翻译终稿、取消、提权、剪贴板、密码框)批量验收待完成。
227
227
 
228
- | 状态 | 内容 |
229
- | --- | --- |
230
- | AI 键已实现 | 长按录音、松开收尾,唤起 Harness,按页面复用或新建对话,通过宿主接口定向追加文字、不依赖焦点;“自动发送”默认开启,填充后调用宿主发送接口,关闭时仅保留草稿;失败保留结果供复制。开关按设备保存,兼容旧配置并随恢复默认开启。 |
231
- | 自动发送验证(2026-11-09) | 构建及全量 108 项测试通过,覆盖默认发送、关闭后仅填充、当前/新建目标、已有草稿、设备隔离、录音配置快照、旧配置迁移、恢复默认、重复事件去重及失败不发送;Gateway 已验证发送标志传递。真实鼠标与宿主界面的自动发送联调待验证。 |
232
- | AI 键历史验证 | 此前全量 104 项测试通过,包含当前/新建对话、已有草稿、切换页面、重复事件、取消与迟到创建、多设备隔离、空结果、识别失败、前端断开、过期请求及 Gateway 通道;此前安装包已实测设置弹窗关闭、从设置页及插件页新建对话后填充且不发送,测试草稿已清空;输入模型已验证无焦点追加,macOS ARM64 浮层已重新编译,包内容检查通过。真实鼠标及 Windows 实机待验证。 |
233
- | 已实现 | 设备页面、按 SN 保存偏好、SDK 桥接、共享语音会话、M 键动作、DPI 下发与回填;已移除电脑麦克风和输入框长按入口。 |
234
- | 本次实现 | 后台独立管理语音会话;macOS/Windows 原生浮层在屏幕底部居中显示预览及处理状态,提供取消、错误提示及结果复制;系统输入框安全回填,移除 Harness 内嵌状态条。 |
235
- | 历史验证 | 构建、自动化、Harness Gateway/模拟服务、macOS ARM64 SDK 与安装包;合成语音抽样覆盖普通话、英语、日语、粤语、法语及普通话繁体。 |
236
- | 本次验证 | 自动化覆盖无前端语音、前端断开、取消与迟到结果、原生进程失败及清理;macOS ARM64 浮层已编译并检查实际显示,主窗口最小化操作后仍可见;Zed 临时文档已验证中英文、多行回填,用户已确认真实鼠标在 Zed 正常回填;默认兼容粘贴已在 VS Code 输入框验证中英文和多行文字;80 项回归测试通过,兼容策略测试覆盖窗口、文档和输入活动变化及剪贴板新写入保护。 |
237
- | 未验证 | 其他应用回填、Windows 原生浮层实机、多屏/全屏切换及完整翻译实网链路。 |
238
-
239
- M 键按 SDK `m` 事件执行:语音动作按下开始、抬起结束,其他动作抬起执行;重复 `hold` 不重复触发。尾部音频窗口默认 150 ms,独立翻译键映射为翻译动作。AI 键按 SDK `ai down/up` 开始和结束录音,忽略重复 `down`、`hold` 和 `click`,不重复创建对话、填充或发送。具体配置及验证记录见[开发说明](../contributing/development.md)。
228
+ 历史验证和具体边界统一见[开发说明](../contributing/development.md#已有验证记录),发布平台记录见[CI/CD 规格](ci-cd.md#6-验收与平台状态)。自动化通过不代表全部实机和实网场景已验收;第 7 节列出验收要求,第 8 节保留待确认事项。
@@ -2,58 +2,98 @@ using System;
2
2
  using System.Collections.Generic;
3
3
  using System.Drawing;
4
4
  using System.Drawing.Drawing2D;
5
+ using System.Runtime.InteropServices;
6
+ using System.Text;
5
7
  using System.Text.RegularExpressions;
6
8
  using System.Linq;
7
9
  using System.Threading;
8
10
  using System.Windows.Automation;
9
- using System.Windows.Automation.Text;
10
11
  using System.Windows.Forms;
11
12
  using System.Web.Script.Serialization;
12
13
 
13
14
  namespace Sonicloud {
14
- sealed class Insertion {
15
- AutomationElement element;
16
- string original;
17
- int[] runtime;
18
- int start, length;
19
- static bool Read(AutomationElement target, out string value, out int offset, out int count) {
20
- value = ""; offset = count = 0;
21
- object vp, tp;
22
- if (target.Current.IsPassword || !target.TryGetCurrentPattern(ValuePattern.Pattern, out vp)
23
- || ((ValuePattern)vp).Current.IsReadOnly || !target.TryGetCurrentPattern(TextPattern.Pattern, out tp)) return false;
24
- value = ((ValuePattern)vp).Current.Value;
25
- var pattern = (TextPattern)tp;
26
- var selected = pattern.GetSelection();
27
- if (selected.Length != 1) return false;
28
- var before = pattern.DocumentRange.Clone();
29
- before.MoveEndpointByRange(TextPatternRangeEndpoint.End, selected[0], TextPatternRangeEndpoint.Start);
30
- offset = before.GetText(-1).Length; count = selected[0].GetText(-1).Length;
31
- // UIA text/value views must agree before making any replacement.
32
- return pattern.DocumentRange.GetText(-1) == value && offset + count <= value.Length;
33
- }
34
- public static Insertion Capture() {
35
- try {
36
- var target = AutomationElement.FocusedElement;
37
- string value; int offset, count;
38
- if (target == null || !Read(target, out value, out offset, out count)) return null;
39
- return new Insertion { element = target, runtime = target.GetRuntimeId(), original = value, start = offset, length = count };
40
- } catch { return null; }
15
+ // typer.dll 标准注入库(纯 C ABI):会话式逐字打字,实时退格更正,逐字模式不占用剪贴板。
16
+ static class Typer {
17
+ public const int ModePerChar = 0;
18
+ public const int ErrInvalidArg = -1, ErrNotSupported = -2, ErrAccessDenied = -4, ErrWrongState = -6, ErrQueueFull = -8;
19
+ public delegate void ErrorCallback(IntPtr user, int code);
20
+ // typer_config v1.1 布局(x64 自然对齐与 C 一致);drained_cb 不用,置空。
21
+ [StructLayout(LayoutKind.Sequential)]
22
+ public struct Config {
23
+ public IntPtr size, drained_cb, drained_user;
24
+ public int xkb_bind_delay_ms, xkb_char_interval_ms, xdotool_char_delay_ms, xclip_settle_ms,
25
+ paste_settle_ms, ime_settle_ms, proc_timeout_ms, queue_limit;
26
+ public IntPtr error_cb, error_user;
27
+ }
28
+ // typer_caps 完整布局(含尾部 char ime_tool_path[260],探测要求 size 覆盖全结构)。
29
+ [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
30
+ public struct Caps {
31
+ public IntPtr size;
32
+ public int session_type, text_typing, increment_typing, whole_phrase_paste, ime_guard,
33
+ input_permitted, has_xdotool, has_xclip, has_fcitx_remote;
34
+ [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)] public string ime_tool_path;
41
35
  }
42
- public bool Insert(string text) {
36
+ [DllImport("kernel32.dll", CharSet = CharSet.Unicode)] static extern IntPtr LoadLibrary(string path);
37
+ [DllImport("typer.dll", CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr typer_create(ref Config config);
38
+ [DllImport("typer.dll", CallingConvention = CallingConvention.Cdecl)] public static extern void typer_destroy(IntPtr ctx);
39
+ [DllImport("typer.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int typer_session_begin(IntPtr ctx, int mode);
40
+ [DllImport("typer.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int typer_feed_partial(IntPtr ctx, byte[] utf8);
41
+ [DllImport("typer.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int typer_feed_final(IntPtr ctx, byte[] utf8);
42
+ [DllImport("typer.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int typer_session_end(IntPtr ctx);
43
+ [DllImport("typer.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int typer_session_abort(IntPtr ctx);
44
+ [DllImport("typer.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int typer_probe(ref Caps caps);
45
+ [DllImport("typer.dll", CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr typer_version();
46
+ [DllImport("typer.dll", CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr typer_strerror(int code);
47
+ public static bool loaded;
48
+ public static void Load(string directory) {
43
49
  try {
44
- var current = AutomationElement.FocusedElement;
45
- string value; int offset, count;
46
- if (current == null || !current.GetRuntimeId().SequenceEqual(runtime)
47
- || !Read(current, out value, out offset, out count) || value != original || offset != start || count != length) return false;
48
- ((ValuePattern)element.GetCurrentPattern(ValuePattern.Pattern)).SetValue(original.Substring(0, start) + text + original.Substring(start + length));
49
- // Setting value succeeded; caret placement is best effort, never repeat insertion.
50
- try {
51
- var range = ((TextPattern)element.GetCurrentPattern(TextPattern.Pattern)).DocumentRange;
52
- range.MoveEndpointByRange(TextPatternRangeEndpoint.End, range, TextPatternRangeEndpoint.Start);
53
- range.Move(TextUnit.Character, start + text.Length); range.Select();
54
- } catch { }
55
- return true;
56
- } catch { return false; }
50
+ if (LoadLibrary(System.IO.Path.Combine(directory, "typer.dll")) == IntPtr.Zero) return;
51
+ StringAt(typer_version()); // 触发一次导出调用,确认模块与 ABI 可用
52
+ loaded = true;
53
+ } catch { loaded = false; }
54
+ }
55
+ public static string StringAt(IntPtr pointer) { return pointer == IntPtr.Zero ? "" : Marshal.PtrToStringAnsi(pointer); }
56
+ }
57
+ // 面板持有的 typer 会话封装;error 回调来自库内打字线程,只做投递,不在回调内再调 typer_*。
58
+ sealed class TyperEngine {
59
+ static Action<int> notify;
60
+ static readonly Typer.ErrorCallback errorCallback = OnNativeError;
61
+ static void OnNativeError(IntPtr user, int code) { Action<int> handler = notify; if (handler != null) handler(code); }
62
+ public IntPtr context;
63
+ public bool Open(Action<int> onError) {
64
+ if (!Typer.loaded) return false;
65
+ var config = new Typer.Config();
66
+ config.size = (IntPtr)Marshal.SizeOf(typeof(Typer.Config));
67
+ config.queue_limit = 1024; // 默认 256,长语音的中间结果可能更多
68
+ config.error_cb = Marshal.GetFunctionPointerForDelegate(errorCallback);
69
+ try { context = Typer.typer_create(ref config); } catch { context = IntPtr.Zero; }
70
+ if (context == IntPtr.Zero) return false;
71
+ notify = onError;
72
+ return true;
73
+ }
74
+ public void Close() {
75
+ if (context != IntPtr.Zero) { Typer.typer_destroy(context); context = IntPtr.Zero; }
76
+ notify = null;
77
+ }
78
+ public int Begin(int mode) { return context == IntPtr.Zero ? Typer.ErrNotSupported : Typer.typer_session_begin(context, mode); }
79
+ public int Partial(string text) { return context == IntPtr.Zero ? Typer.ErrNotSupported : Typer.typer_feed_partial(context, Bytes(text)); }
80
+ public int Final(string text) { return context == IntPtr.Zero ? Typer.ErrNotSupported : Typer.typer_feed_final(context, Bytes(text)); }
81
+ public int End() { return context == IntPtr.Zero ? Typer.ErrNotSupported : Typer.typer_session_end(context); }
82
+ public int Abort() { return context == IntPtr.Zero ? Typer.ErrNotSupported : Typer.typer_session_abort(context); }
83
+ public static string ErrorText(int code) {
84
+ switch (code) {
85
+ case Typer.ErrInvalidArg: return "文本包含不支持的字符";
86
+ case Typer.ErrAccessDenied: return "目标窗口拒绝模拟输入";
87
+ case Typer.ErrWrongState: return "打字会话已中断";
88
+ case Typer.ErrQueueFull: return "打字任务过多,请稍候";
89
+ default: return "打字失败(代码 " + code + ")";
90
+ }
91
+ }
92
+ static byte[] Bytes(string text) {
93
+ if (string.IsNullOrEmpty(text)) return new byte[] { 0 };
94
+ byte[] data = Encoding.UTF8.GetBytes(text);
95
+ Array.Resize(ref data, data.Length + 1); // 追加 NUL 终止
96
+ return data;
57
97
  }
58
98
  }
59
99
  static class CapsuleShape {
@@ -90,12 +130,19 @@ namespace Sonicloud {
90
130
  public sealed class VoicePanel : Form {
91
131
  readonly SingleLineText text = new SingleLineText();
92
132
  readonly Waveform waveform = new Waveform();
93
- readonly Button close = new Button(), copy = new Button();
133
+ readonly Button close = new Button(), copy = new Button(), submit = new Button();
94
134
  readonly JavaScriptSerializer json = new JavaScriptSerializer { MaxJsonLength = 300000 };
95
135
  readonly System.Windows.Forms.Timer pulse = new System.Windows.Forms.Timer { Interval = 40 };
96
136
  string session = "", preview = "", phase = "", message = "";
97
137
  bool active;
98
- Insertion insertion;
138
+ TyperEngine typer;
139
+ bool sessionTyping; // typer 会话已 begin 且未 end/abort
140
+ bool typingLive; // voice_input:中间结果实时逐字打进目标窗口
141
+ bool typingBlocked; // 密码框 / 组件不可用,本次会话拒绝打字
142
+ string blockedReason = "";
143
+ bool typingError; // 注入失败已闩(error_cb),后续 feed/end 只会返回 WRONG_STATE
144
+ int typingErrorCode;
145
+ bool pendingSubmit; // 宿主 submitOffer:文本留在浮窗,等用户放好焦点后一键提交
99
146
  Screen screen;
100
147
  protected override bool ShowWithoutActivation { get { return true; } }
101
148
  protected override CreateParams CreateParams {
@@ -120,16 +167,19 @@ namespace Sonicloud {
120
167
  }
121
168
  close.Image = icon; close.AccessibleName = "取消";
122
169
  copy.Text = "复制结果";
123
- Controls.AddRange(new Control[] { text, waveform, close, copy });
170
+ submit.Text = "一键提交"; submit.AccessibleName = "一键提交";
171
+ Controls.AddRange(new Control[] { text, waveform, close, copy, submit });
124
172
  close.Click += delegate {
125
173
  if (active) Emit(new Dictionary<string, object> { { "event", "cancel" }, { "sessionId", session } });
126
- active = false; insertion = null; preview = message = ""; Hide();
174
+ EndSession(); preview = message = ""; pendingSubmit = false; Hide();
127
175
  };
176
+ submit.Click += delegate { SubmitPending(); };
128
177
  copy.Click += delegate { try { Clipboard.SetText(preview); copy.Text = "已复制"; } catch { copy.Text = "复制失败"; } };
129
178
  pulse.Tick += delegate { waveform.Phase += 0.25; waveform.Invalidate(); };
130
179
  VisibleChanged += delegate { pulse.Enabled = Visible && active; };
131
180
  Microsoft.Win32.SystemEvents.DisplaySettingsChanged += DisplayChanged;
132
181
  FormClosed += delegate { pulse.Dispose(); close.Image.Dispose(); Microsoft.Win32.SystemEvents.DisplaySettingsChanged -= DisplayChanged; };
182
+ Disposed += delegate { EndSession(); if (typer != null) { typer.Close(); typer = null; } };
133
183
  }
134
184
  void DisplayChanged(object sender, EventArgs e) { if (!IsDisposed && Visible) BeginInvoke((Action)Render); }
135
185
  void Render() {
@@ -140,44 +190,126 @@ namespace Sonicloud {
140
190
  var previous = Region; Region = new Region(outline); if (previous != null) previous.Dispose();
141
191
  }
142
192
  waveform.SetBounds(20, (height - 26) / 2, 23, 26);
143
- close.SetBounds(width - 44, (height - 28) / 2, 28, 28); copy.SetBounds(width - 146, (height - 32) / 2, 96, 32);
144
- waveform.Visible = active; copy.Visible = !active && preview.Length > 0; close.AccessibleName = active ? "取消" : "关闭";
193
+ close.SetBounds(width - 44, (height - 28) / 2, 28, 28);
194
+ if (pendingSubmit) {
195
+ submit.SetBounds(width - 148, (height - 32) / 2, 96, 32);
196
+ copy.SetBounds(width - 252, (height - 32) / 2, 96, 32);
197
+ } else copy.SetBounds(width - 146, (height - 32) / 2, 96, 32);
198
+ waveform.Visible = active; submit.Visible = pendingSubmit;
199
+ copy.Visible = !active && preview.Length > 0; close.AccessibleName = active ? "取消" : "关闭";
145
200
  int left = active ? 54 : 22, right = copy.Visible ? copy.Left : close.Left;
146
201
  text.SetBounds(left, 0, Math.Max(0, right - left - 12), height);
147
202
  string status = message.Length > 0 ? message : phase == "finishing" ? "正在整理…" : phase == "translating" ? "正在翻译…" : "";
148
203
  string displayText = (status + " " + preview).Trim();
149
204
  text.Text = Regex.Replace(displayText, @"[\r\n\t\u0085\u2028\u2029]+", " ");
150
- pulse.Enabled = active;
205
+ pulse.Enabled = Visible && active;
151
206
  Show();
152
207
  }
153
208
  static string Get(Dictionary<string, object> item, string key) { object value; return item.TryGetValue(key, out value) ? Convert.ToString(value) : ""; }
154
209
  void Emit(Dictionary<string, object> value) { Console.WriteLine(json.Serialize(value)); Console.Out.Flush(); }
210
+ // UIA 不可达时放行(fail-open):最坏情况等于无密码检查的直接注入。
211
+ static bool FocusedElementIsPassword() {
212
+ try { var element = AutomationElement.FocusedElement; return element != null && element.Current.IsPassword; }
213
+ catch { return false; }
214
+ }
215
+ void EndSession() {
216
+ active = false; typingLive = false;
217
+ if (sessionTyping) { sessionTyping = false; if (typer != null) typer.Abort(); }
218
+ }
219
+ void HandleTypingError(int code) {
220
+ typingError = true; typingErrorCode = code;
221
+ message = "未回填:" + TyperEngine.ErrorText(code) + ",可复制结果";
222
+ Render();
223
+ }
224
+ // 一键提交:把保留的文本一次性逐字注入点击时聚焦的窗口(浮窗不抢焦点,焦点仍在用户摆好的目标上)。
225
+ void SubmitPending() {
226
+ if (!pendingSubmit) return;
227
+ pendingSubmit = false;
228
+ string reason = null;
229
+ if (typer == null || typer.context == IntPtr.Zero) reason = "打字组件不可用";
230
+ else if (FocusedElementIsPassword()) reason = "目标为密码输入框";
231
+ else {
232
+ int rc = typer.Begin(Typer.ModePerChar);
233
+ if (rc != 0) reason = TyperEngine.ErrorText(rc);
234
+ else {
235
+ sessionTyping = true;
236
+ rc = typer.Final(preview);
237
+ if (rc == 0) { typer.End(); sessionTyping = false; }
238
+ else { typer.Abort(); sessionTyping = false; reason = TyperEngine.ErrorText(rc); }
239
+ }
240
+ }
241
+ if (reason == null) { preview = message = ""; Hide(); }
242
+ else { message = "未回填:" + reason + ",可复制结果"; Render(); }
243
+ }
155
244
  void HandleCommand(Dictionary<string, object> item) {
156
245
  string command = Get(item, "command"), incoming = Get(item, "sessionId"); bool ok = true, inserted = false;
157
246
  if (command == "begin") {
158
247
  session = incoming; preview = message = ""; phase = "connecting"; active = true;
159
- insertion = Get(item, "action") == "speechSearch" || Get(item, "action") == "ai_input" ? null : Insertion.Capture();
248
+ string action = Get(item, "action");
249
+ typingBlocked = false; typingError = false; sessionTyping = false; typingLive = false; pendingSubmit = false;
250
+ if (action == "voice_input" || action == "translate") {
251
+ typingLive = action == "voice_input"; // 翻译只在最终译文出来后打字,避免先打原文再整段退格
252
+ if (typer == null || typer.context == IntPtr.Zero) { typingBlocked = true; blockedReason = "打字组件不可用"; }
253
+ else if (FocusedElementIsPassword()) { typingBlocked = true; blockedReason = "目标为密码输入框"; }
254
+ else {
255
+ int rc = typer.Begin(Typer.ModePerChar);
256
+ if (rc == 0) sessionTyping = true;
257
+ else { typingBlocked = true; blockedReason = TyperEngine.ErrorText(rc); }
258
+ }
259
+ }
160
260
  screen = Screen.FromPoint(Cursor.Position); copy.Text = "复制结果"; Render();
161
- } else if (incoming.Length > 0 && incoming != session) ok = false;
261
+ } else if (incoming.Length > 0 && incoming != session) { ok = false; }
162
262
  else if (command == "result" && active) {
163
- preview = Get(item, "text"); inserted = insertion != null && insertion.Insert(preview);
164
- insertion = null; active = false;
165
- if (inserted) Hide(); else { message = "未回填,可复制结果"; Render(); }
263
+ preview = Get(item, "text");
264
+ string reason = null;
265
+ if (typingBlocked) reason = blockedReason;
266
+ else if (typingError) reason = TyperEngine.ErrorText(typingErrorCode);
267
+ else if (sessionTyping) {
268
+ int rc = typer.Final(preview);
269
+ if (rc != 0) reason = TyperEngine.ErrorText(rc);
270
+ else { typer.End(); sessionTyping = false; }
271
+ }
272
+ EndSession();
273
+ if (reason == null) { inserted = true; Hide(); }
274
+ else { message = "未回填:" + reason + ",可复制结果"; Render(); }
166
275
  } else if (command == "event") {
167
276
  switch (Get(item, "type")) {
168
- case "preview": if (active) { preview = Get(item, "text"); Render(); } break;
277
+ case "preview":
278
+ if (active) {
279
+ preview = Get(item, "text");
280
+ if (typingLive && sessionTyping && !typingError) typer.Partial(preview);
281
+ Render();
282
+ }
283
+ break;
169
284
  case "phase": if (active) { phase = Get(item, "phase"); Render(); } break;
170
285
  case "notice": message = Get(item, "message"); Render(); break;
171
- case "failed": active = false; insertion = null; message = Get(item, "message"); Render(); break;
172
- case "cancelled": case "finished": active = false; insertion = null; preview = message = ""; Hide(); break;
286
+ case "failed": EndSession(); message = Get(item, "message"); Render(); break;
287
+ case "submitOffer":
288
+ // 宿主无法填入 Harness 对话(窗口/渲染不可达):文本留在浮窗,交给一键提交注入当前焦点窗口。
289
+ preview = Get(item, "text");
290
+ EndSession();
291
+ typingError = false; // 清闩,本次提交允许重新注入
292
+ pendingSubmit = preview.Length > 0;
293
+ message = pendingSubmit ? Get(item, "message") : "没有可输入的内容";
294
+ if (message.Length == 0) message = "请打开需要输入界面";
295
+ copy.Text = "复制结果"; Render();
296
+ break;
297
+ case "cancelled": case "finished":
298
+ // 空结果收尾(无 result):实时打出的中间结果用空 partial 修正掉,再结束会话。
299
+ if (sessionTyping && typingLive && !typingError) { typer.Partial(""); typer.End(); sessionTyping = false; }
300
+ EndSession(); preview = message = ""; Hide();
301
+ break;
173
302
  }
174
303
  } else ok = false;
175
304
  object id; if (item.TryGetValue("id", out id)) Emit(new Dictionary<string, object> { { "id", id }, { "ok", ok }, { "value", inserted } });
176
305
  }
177
- [STAThread] public static void Run() {
306
+ [STAThread] public static void Run(string dllDirectory) {
307
+ Typer.Load(dllDirectory);
178
308
  Application.EnableVisualStyles();
179
309
  using (var form = new VoicePanel()) {
180
- var handle = form.Handle;
310
+ var handle = form.Handle; // 先建句柄,后台线程的 BeginInvoke 才有目标
311
+ form.typer = new TyperEngine();
312
+ form.typer.Open(code => { if (!form.IsDisposed) form.BeginInvoke((Action)(() => form.HandleTypingError(code))); });
181
313
  var input = new Thread(() => {
182
314
  string line;
183
315
  while ((line = Console.ReadLine()) != null) {
Binary file
@@ -6,4 +6,4 @@ $references = @('System.Windows.Forms', 'System.Drawing', 'System.Web.Extensions
6
6
  [System.Windows.Automation.AutomationElement].Assembly.Location,
7
7
  [System.Windows.Automation.Text.TextPatternRangeEndpoint].Assembly.Location)
8
8
  Add-Type -Path (Join-Path $PSScriptRoot 'VoicePanel.cs') -ReferencedAssemblies $references
9
- [Sonicloud.VoicePanel]::Run()
9
+ [Sonicloud.VoicePanel]::Run($PSScriptRoot)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sonicloud/dsh-sonicloud-device",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "DeepSeek Harness 智能鼠标、多设备设置与共享语音服务",
5
5
  "icon": "./src/client/assets/sonicloud.png",
6
6
  "type": "module",
@@ -18,6 +18,7 @@
18
18
  "dist",
19
19
  "cordis.patch.yml",
20
20
  "README.md",
21
+ "README.zh.md",
21
22
  "src/devices",
22
23
  "src/desktop",
23
24
  "native",
@@ -36,7 +36,13 @@ export async function systemAction(action, value, { platform = process.platform,
36
36
  const scripts = {
37
37
  enter: '$s=New-Object -ComObject WScript.Shell; $s.SendKeys("{ENTER}")',
38
38
  backspace: '$s=New-Object -ComObject WScript.Shell; $s.SendKeys("{BACKSPACE}")',
39
- openAi: '$s=New-Object -ComObject WScript.Shell; if(-not $s.AppActivate("DeepSeek Harness")){exit 1}',
39
+ // The window title varies with the active page, so title matching fails intermittently; activate by handle.
40
+ // Minimized windows need ShowWindow(SW_RESTORE) because AppActivate never restores them. When no visible handle
41
+ // exists (hidden to the tray), a second launch of the running exe reaches the single-instance handler, which
42
+ // restores or recreates the window from inside the app. The plugin backend itself runs the app exe as node via
43
+ // ELECTRON_RUN_AS_NODE, and a second launch inheriting that variable would also start as plain node and exit
44
+ // without any window, so the Electron overrides must be stripped before handing off.
45
+ openAi: "$p=Get-Process -Name 'DeepSeek Harness' -ErrorAction SilentlyContinue | Where-Object { $_.MainWindowHandle -ne 0 } | Select-Object -First 1; if($p){ Add-Type -Namespace Win -Name Native -MemberDefinition '[DllImport(\"user32.dll\")] public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);[DllImport(\"user32.dll\")] public static extern bool SetForegroundWindow(IntPtr hWnd);'; [Win.Native]::ShowWindow($p.MainWindowHandle, 9) | Out-Null; if([Win.Native]::SetForegroundWindow($p.MainWindowHandle)){ exit 0 } }; Remove-Item Env:\\ELECTRON_RUN_AS_NODE, Env:\\ELECTRON_NO_ATTACH_CONSOLE, Env:\\ELECTRON_NO_ASAR -ErrorAction SilentlyContinue; $exe=(Get-Process -Name 'DeepSeek Harness' -ErrorAction SilentlyContinue | Select-Object -First 1).Path; if($exe){ Start-Process -FilePath $exe -ArgumentList 'dsh://open'; exit 0 }; Start-Process -FilePath rundll32.exe -ArgumentList 'url.dll,FileProtocolHandler','dsh://open' -WindowStyle Hidden; exit 0",
40
46
  };
41
47
  if (!scripts[action]) throw new Error('不支持此按键动作');
42
48
  args = ['-NoProfile', '-NonInteractive', '-EncodedCommand', Buffer.from(scripts[action], 'utf16le').toString('base64')];