@copylee/dsh-computer-use 0.8.2 → 0.8.4
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/README.md +45 -138
- package/helper/CuHelper.cs +188 -47
- package/lib/client.js +1 -1
- package/lib/index.js +210 -67
- package/lib/types/browser-host.d.ts +5 -1
- package/lib/types/computer.d.ts +3 -1
- package/lib/types/coords.d.ts +1 -1
- package/lib/types/helper-client.d.ts +1 -0
- package/lib/types/overlay.d.ts +25 -6
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -3,142 +3,49 @@
|
|
|
3
3
|
[](https://awesome-dsh-plugin.com)
|
|
4
4
|
[](https://www.npmjs.com/package/@copylee/dsh-computer-use)
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
仅支持 Windows。
|
|
9
|
-
|
|
10
|
-

|
|
11
|
-
|
|
12
|
-
上图是一次真实操作的录屏(只录了被操控的记事本窗口):AI 打开记事本并逐行输入文字。下图是对话里看到的过程,以及设置页。
|
|
13
|
-
|
|
14
|
-
| 对话中的操作过程 | 设置页 |
|
|
15
|
-
|---|---|
|
|
16
|
-
|  |  |
|
|
17
|
-
|
|
18
|
-
## 功能
|
|
19
|
-
|
|
20
|
-
| 功能 | 说明 |
|
|
21
|
-
|---|---|
|
|
22
|
-
| 截图即坐标系 | 模型只用最近一张截图的像素坐标,DPI、缩放、多显示器偏移全部由插件换算,高分屏(200% 缩放)也点得准 |
|
|
23
|
-
| 动作后自动回传截图 | 点击 / 输入 / 按键 / 滚动后,**等画面不再变化**(页面加载、动画结束,最多约 2.5 秒)再截图;`wait` 也直接带回截图,“等一下再看”只需一步,而且画面一静止就提前结束,不会死等满时长;画面与上一张截图完全相同时不再发图,只告诉模型“没有可见变化”,省 token |
|
|
24
|
-
| 批量动作 | `computer_batch` 一次执行“点输入框 → 输入 → 回车”等多步,最后只截一张图 |
|
|
25
|
-
| 按名称点控件 | 动作里用 `target`(控件的可见名称,如 `"保存"`、`"按钮:确定"`)代替坐标。上一张截图里还没出现的控件也能点,比如上一步才打开的菜单项、才弹出的对话框按钮;最多等 3 秒它出现。重名时不猜,直接把候选和坐标告诉模型;`ui_elements` 现在会返回 `automationId`,重名时可用 `target: "id:<AutomationId>"`(或 `"#<AutomationId>"`)明确指定 |
|
|
26
|
-
| 等条件而不是等时间 | `wait` 加 `until`(`"window:另存为"`、`"target:保存"`):窗口或控件一出现(或一消失)就继续,超时才报错。配合 `target`,“按 Ctrl+S → 等另存为 → 填文件名 → 点保存”可以写进同一个批量调用 |
|
|
27
|
-
| 把窗口读成文本 | `ui_elements` 加 `text: true` 直接读出文档、网页、聊天回复或日志的文字(默认取末尾 4000 字),比滚动加放大截图快,也能逐字引用;批量调用加 `finish: "text"` 可以用文字代替结尾的截图 |
|
|
28
|
-
| 按应用授权 | 先 `request_access` 列出要操控的应用,经 DSH 原生审批后才能操作;也可改为“全部允许” |
|
|
29
|
-
| 不会操作自己 | 永远拒绝向 DeepSeek Harness 自身窗口输入;常见密码管理器默认禁止,可再自定义禁用列表 |
|
|
30
|
-
| 正在控制的提示 | 橙色光晕 + 胶囊提示条(显示当前动作),**不会出现在截图里**;AI 还在忙但已不再操作电脑时(约 90 秒没有电脑操作,或连续调用了 3 个其他工具)自动收起,下次操作电脑时再出现 |
|
|
31
|
-
| 迷你进度卡片(默认) | 操控期间 DeepSeek Harness 最小化,右下角显示一张半透明小卡片:标题行有 DeepSeek 图标和“输入”“展开”“暂停”“停止”四个圆形按钮;第一行滚动显示回复正文(实时流式),第二行显示思考过程,或最近几步的小时间线(已完成的步骤打勾,如“打开 Word › 输入正文 › 另存为”)。点“展开”变成长卡片:多行回复、思考、最近 5 步时间线。点“输入”在卡片下方弹出输入框,AI 自动暂停,输入指令按 Enter 发送(与在 DSH 里插话一样,AI 下一步就会看到),Esc 取消。任务结束时卡片显示“已完成”几秒再消失(鼠标停在上面会一直保留)。卡片可拖动、跟随系统深浅色,AI 的截图里看不到它,点击也会穿透它。操控中你把 DSH 窗口打开,它会自动变成下面的半透明悬浮卡片,不会挡住 AI |
|
|
32
|
-
| 悬浮卡片 | 操控期间 DeepSeek Harness 窗口缩成右下角置顶卡片,结束后自动恢复原尺寸、位置和最大化 / 最小化状态(也可选最小化或不动)。卡片只给你看:AI 的截图里看到的是卡片下面的内容(通过 Windows 放大镜接口截图时把卡片排除在外,不闪烁;该接口不可用时退回为截图瞬间让卡片透明约 20 毫秒),点击和滚动也直接穿透卡片,卡片不会挪位置。卡片默认 80% 不透明,可以透过它看清下面的操作过程;鼠标在卡片上停一下就变回不透明,方便阅读和点击(透明度可在设置里调) |
|
|
33
|
-
| 暂停与停止 | 你亲手按 **Esc** 暂停(AI 停在下一步之前),再按 Esc 或点“继续”恢复,对话不会被中断;点“停止”才结束本轮。插件自己发出的 Esc 不会误触发 |
|
|
34
|
-
| 你随时可以插手 | 你用键盘打字时 AI 自动暂停,停手 1.5 秒后继续(单按 Shift / Ctrl 等修饰键不算);你切换或打开了别的窗口,AI 下一步不会盲目执行,而是先看新截图再决定。你只是点进 DSH 卡片看对话时不算切换,AI 的按键会自动回到它正在操作的窗口。鼠标移动不会触发,避免误触 |
|
|
35
|
-
| 文字输入 | Unicode 直接输入,不受输入法状态影响。短文字逐字打出来;长文本默认“流式”:按段落逐段粘贴(代码块不拆开),看得到一段段出现,又不会被 Typora、Obsidian、Word 的自动补全打乱。可在设置里改成逐字输入或一次粘贴。剪贴板文本统一为 CRLF,不丢换行 |
|
|
36
|
-
| 不弄乱剪贴板 | 插件写入剪贴板的内容不进 Windows 剪贴板历史(Win+V)和云剪贴板;粘贴后按原样恢复你的剪贴板(图片、文件也保留);AI 用 `clipboard` 工具写过剪贴板的,本轮结束后自动还原(期间你自己复制了新内容则不动) |
|
|
37
|
-
| 应用技能 | AI 对各个应用的操作笔记,每个应用一份 Markdown(`~/.dsh/computer-use-skills/`)。AI 摸索清楚一个不熟悉的应用后用 `app_skill` 自己记下来(只记操作规律,不记任务内容),以后 `open_application` 打开该应用时自动附带,不占系统提示。自带 Word、PowerPoint、Excel、Chrome、文件资源管理器、Obsidian、Typora、微信、QQ、记事本十份内置技能(含各应用的快捷键清单,PowerPoint 含原生公式和动画的做法);AI 在内置技能上追加的笔记单独保存,内置部分随插件更新;设置页“应用技能”里可以查看、修改、删除、新建,内置技能可覆盖和恢复 |
|
|
38
|
-
| 打开应用 | `open_application` 同时搜索开始菜单和桌面快捷方式(含公共桌面),便携软件也能按名字打开。已经关到系统托盘的应用(QQ、微信等)会像人一样点托盘图标把窗口叫回来(必要时先展开“显示隐藏的图标”),不会再启动第二个实例、也不会重复登录;你明确要多开时(如微信双开)AI 会用 `new_instance` 另开一个。任务结束时 AI 会收拾现场:关掉自己打开、你不再需要的窗口,`windows tidy` 把原本最小化或在托盘里的应用重新最小化 |
|
|
39
|
-
| 看不见图的模型 | `ui_elements` 用 Windows UI Automation 列出可点击元素和坐标,纯文本模型也能用。Chromium / Electron 会先唤醒无障碍树,同时保留浏览器标签栏、地址栏等控件;支持识别常见网页编辑器,并返回应用提供的 AutomationId、焦点能力和编辑器标记 |
|
|
40
|
-
| 返回 DSH | 操控结束时恢复桌面窗口并切回前台。若从 DSH Web 标签开始,会返回记录的浏览器窗口并尝试选回该标签。反代或自定义域名可在插件配置中将 `harnessUrl` 设为实际 Web GUI 地址 |
|
|
41
|
-
| UIA 动作 | 按名称或 ID 单击时,支持 UIA Invoke 的控件优先执行语义动作,输入框可通过 UIA 聚焦。缺少对应能力时重新确认控件后使用坐标。多击、右键和组合键点击仍使用坐标 |
|
|
42
|
-
| 零原生依赖 | 截图、窗口查询和输入直接调用 DeepSeek Harness 自带的 koffi,不需要额外安装;浮层、UI Automation、剪贴板等由一个小 helper 负责,首次使用时用 Windows 自带的 .NET Framework `csc.exe` 编译(约 1 秒,之后缓存)。不需要 node-gyp,也不受 Electron ABI 影响。开启了智能应用控制(Smart App Control)时,helper 改为在系统自带的 PowerShell 里运行 |
|
|
43
|
-
|
|
44
|
-
## 安装
|
|
45
|
-
|
|
46
|
-
DSH 桌面版:**插件 → 添加插件**,输入 `@copylee/dsh-computer-use`,安装后启用。
|
|
47
|
-
|
|
48
|
-
命令行(web 等其他 profile):
|
|
49
|
-
|
|
50
|
-
```bash
|
|
51
|
-
dsh plugin --profile web add @copylee/dsh-computer-use@latest
|
|
52
|
-
```
|
|
53
|
-
|
|
54
|
-
要求:Windows 10 2004+ / Windows 11(自带 .NET Framework 4.8),DSH 0.2.0-rc.2 或更高。截图需要**支持图片输入的模型**(如 DeepSeek V4.1 Flash);纯文本模型会自动改用 `ui_elements`。
|
|
55
|
-
|
|
56
|
-
> 如果之前装过其他 computer-use 插件或 skill(例如 `dsh-computer-use-win`、Hermes 的 computer-use skill),建议先停用,避免工具和说明互相冲突。
|
|
57
|
-
|
|
58
|
-
## 使用
|
|
59
|
-
|
|
60
|
-
直接说要做什么就行,例如:
|
|
61
|
-
|
|
62
|
-
- 用 Chrome 打开 github.com
|
|
63
|
-
- 打开记事本,写一段会议纪要并保存到桌面
|
|
64
|
-
- 用计算器算一下 1234×5678
|
|
65
|
-
|
|
66
|
-
按应用授权模式下,模型会先请求操控对应应用,你在 DSH 里批准即可。会话权限为“完全权限”或“自动审查”(不弹审批)时,请求会被自动批准,但 DeepSeek Harness 自身和禁用列表里的应用仍然不可操作。
|
|
67
|
-
|
|
68
|
-
**随时按 Esc 暂停**,再按一次继续;点提示条上的“停止”结束本轮。想临时帮一把也可以直接上手:打字时提示条显示“你正在操作,已暂停”,AI 会等你停手;切到别的窗口后 AI 会先重新看屏幕。
|
|
69
|
-
|
|
70
|
-
## Agent 工具
|
|
71
|
-
|
|
72
|
-
| 工具 | 作用 |
|
|
73
|
-
|---|---|
|
|
74
|
-
| `computer` | 单个动作:`screenshot` `left_click` `double_click` `triple_click` `right_click` `middle_click` `mouse_move` `left_click_drag` `left_mouse_down/up` `scroll` `type` `key` `hold_key` `wait` `zoom` `cursor_position`(沿用通用 computer-use 动作命名,模型上手即会) |
|
|
75
|
-
| `computer_batch` | 顺序执行多个动作,遇错即停,最后回传一张截图;最后一步可以是 `zoom`(回传放大区域),`finish` 可选 `screenshot` / `text` / `none` |
|
|
76
|
-
| `app_skill` | 应用技能:`list` 列出、`read` 读取、`append` 追加、`write` 重写某个应用的操作笔记(单份上限 4000 字) |
|
|
77
|
-
| `open_application` | 按名称(“Chrome”“记事本”“微信”)或 exe 路径打开应用,已运行则切到前台,在托盘里则恢复。HTTP(S) 网址在受支持的默认浏览器中显式新开窗口 |
|
|
78
|
-
| `windows` | 列出窗口(含进程 exe 名)、聚焦 / 最小化 / 最大化 / 还原 / 关闭 |
|
|
79
|
-
| `ui_elements` | 前台窗口(含已打开的菜单)的 UI Automation 元素列表,坐标已换算到截图坐标系;`text: true` 改为读出窗口的文字内容 |
|
|
80
|
-
| `clipboard` | 读 / 写剪贴板文本 |
|
|
81
|
-
| `switch_display` | 多显示器时切换截图所在屏幕 |
|
|
82
|
-
| `request_access` / `list_granted_applications` | 按应用授权 |
|
|
83
|
-
|
|
84
|
-
## 设置
|
|
85
|
-
|
|
86
|
-
在 **设置 → 电脑控制** 中修改,保存后下一步操作即生效。页面上还能看到运行状态(屏幕分辨率、缩放比例和实际发给模型的截图尺寸),并可点“预览效果”看 3 秒光晕和提示条:
|
|
87
|
-
|
|
88
|
-
| 选项 | 默认 | 说明 |
|
|
89
|
-
|---|---|---|
|
|
90
|
-
| 授权方式 | 按应用授权 | 或“全部允许” |
|
|
91
|
-
| 光晕与提示条 | 开 | 控制时显示橙色边框和提示条 |
|
|
92
|
-
| 提示条名称 | DeepSeek Harness | 显示为“<名称> 正在操控你的电脑” |
|
|
93
|
-
| 操控期间 DSH 窗口 | 迷你进度卡片 | 迷你进度卡片 / 悬浮卡片 / 最小化 / 保持不变,结束后自动恢复 |
|
|
94
|
-
| 卡片透明度 | 80% | 100%–50%;迷你卡片只让背景变透明,文字始终清晰;DSH 悬浮卡片在鼠标停留时变回不透明 |
|
|
95
|
-
| 操作后自动截图 | 开 | 关闭可省 token,但模型需要自己截图 |
|
|
96
|
-
| 等待稳定 | 400 ms | 动作后至少等这么久,之后画面静止即截图(最多约 2.5 秒) |
|
|
97
|
-
| 截图最长边 / 总像素 / JPEG 质量 | 1366 / 1.15MP / 70 | 越大越清晰、越费 token |
|
|
98
|
-
| 操作时不深度思考(Beta) | 关 | 试验功能,一般不用开。开始操作电脑后,本轮改用模型的最低思考强度,每一步快约 1 秒;下一轮恢复会话里选的思考强度。不思考时模型更容易出错、多走几步;工具参数写坏时会自动重发那一步,为此本轮的回复文字整段出现而不是逐字流出。目前只在 DeepSeek 官方模型上有效 |
|
|
99
|
-
| 检测到你在操作时让出控制 | 开 | 键盘输入暂停、窗口被切换时重新看屏幕 |
|
|
100
|
-
| 文字输入方式 | 流式 | 流式(长文本逐段出现)/ 逐字输入(最像打字,最慢)/ 一次粘贴(最快) |
|
|
101
|
-
| 键盘静止多久后恢复 | 1500 ms | |
|
|
102
|
-
| 禁止操控的应用 | 空 | 名称或 exe,如 `alipay` |
|
|
103
|
-
| 进程内截图与输入 | 开 | 截图、显示器和窗口查询,以及浮层未显示时的输入,改由 DSH 自带的 koffi 在进程内完成,启动更快,也少一次进程间往返。出问题时可以关掉,全部交给 helper |
|
|
104
|
-
| 强调色 | 陶土橙 | 陶土橙 / 蓝色 / 黑色,决定设置页主按钮和开关的颜色,与 copylee 的其他插件共用同一个选择。操控时的橙色光晕不随它变化 |
|
|
105
|
-
|
|
106
|
-
## 数据与隐私
|
|
107
|
-
|
|
108
|
-
- 截图只作为当前会话的图片附件发给你选择的模型,插件不上传、不保存到别处。
|
|
109
|
-
- 截图附件存放在 `~/.dsh/attachments`,随会话历史保留(DSH 会校验附件,删掉仍被会话引用的截图会让该会话无法继续,所以插件不会动它们)。会话删除后留下的“孤儿”截图会在 DSH 启动几分钟后及之后每 12 小时自动清理;设置页“截图缓存”可查看占用并立即清理。
|
|
110
|
-
- helper 源码 `helper/CuHelper.cs` 随包发布,可自行审阅;编译产物缓存在 `%LOCALAPPDATA%\dsh-computer-use\`。进程内的部分(`src/native/`)只加载 DSH 自带的 koffi.node 和 Windows 系统 DLL。
|
|
111
|
-
- 以管理员身份运行的窗口、UAC 弹窗、锁屏无法被控制(Windows UIPI 限制),遇到时模型会请你手动处理。
|
|
112
|
-
- 插件注入的系统提示要求模型不代填密码、支付信息、不处理验证码,遇到时交还给你。
|
|
113
|
-
|
|
114
|
-
## 工作原理
|
|
115
|
-
|
|
116
|
-
```
|
|
117
|
-
DSH Host (Electron, Node 模式)
|
|
118
|
-
└─ lib/index.js 工具 / 审批 / 系统提示 / overlay 生命周期
|
|
119
|
-
├─ lib/native-worker.js(worker 线程,DSH 自带的 koffi)
|
|
120
|
-
│ 截图 · 显示器 / 窗口查询 · 浮层未显示时的鼠标键盘
|
|
121
|
-
└─ cu-helper.exe(常驻,stdin/stdout 每行一个 JSON;SAC 下在 powershell.exe 里加载)
|
|
122
|
-
Per-Monitor-V2 DPI 感知 · GDI 截图 + 缩放 + JPEG · SendInput 鼠标键盘
|
|
123
|
-
窗口枚举与前置 · shell:AppsFolder 启动应用 · 剪贴板 · UI Automation
|
|
124
|
-
分层窗口 overlay(WDA_EXCLUDEFROMCAPTURE)· 低级键盘钩子(只认物理 Esc)
|
|
125
|
-
```
|
|
126
|
-
|
|
127
|
-
## 开发
|
|
128
|
-
|
|
129
|
-
```bash
|
|
130
|
-
pnpm install
|
|
131
|
-
pnpm typecheck
|
|
132
|
-
pnpm test # vitest(mock helper,跨平台)
|
|
133
|
-
pnpm build # tsc → lib/types,tsdown → lib/index.js
|
|
134
|
-
node scripts/helper-smoke.mjs # 本机实测:编译 helper、截图、overlay
|
|
135
|
-
node scripts/helper-smoke.mjs --notepad # 另外用临时文件测试中文输入
|
|
136
|
-
node scripts/native-smoke.mjs --notepad # koffi 路径与 helper 对比;需在 DSH 运行时下跑或设 DSH_KOFFI_PATH
|
|
137
|
-
pnpm pack:check
|
|
138
|
-
```
|
|
139
|
-
|
|
140
|
-
本地联调:DSH 桌面版“添加插件”里填本仓库目录路径即可(以 link 方式安装),改完 `pnpm build` 后重启 DSH 生效。
|
|
141
|
-
|
|
142
|
-
## 许可证
|
|
6
|
+
在 DeepSeek Harness 里交代一个任务,让模型用你已经安装的 Windows 应用完成它。它可以打开应用、读界面、点击和输入,也可以在操作后检查结果。你可以在屏幕上看着它做,随时暂停或接手。
|
|
143
7
|
|
|
144
|
-
|
|
8
|
+
> **可以这样说**:「打开记事本,把下面三条待办写进去,保存到桌面,最后告诉我文件名。」
|
|
9
|
+
|
|
10
|
+

|
|
11
|
+
|
|
12
|
+
*演示中,插件按控件名填写报名表,中途暂停,继续后提交并读出确认文字。画面取自隔离 Chrome 配置中的实际操作,动作由脚本发起,卡片使用演示文案。*
|
|
13
|
+
|
|
14
|
+
## 可以用它做什么
|
|
15
|
+
|
|
16
|
+
| 场景 | 可以交给 DSH 的任务 |
|
|
17
|
+
| --- | --- |
|
|
18
|
+
| 浏览器 | 用当前已打开的 Chrome,在同一窗口的新标签打开网址,填写表单并检查确认提示。 |
|
|
19
|
+
| 办公软件 | 在 Word、WPS 或记事本中输入和整理内容,按指定位置保存,再确认文件名。 |
|
|
20
|
+
| 桌面应用 | 打开计算器、文件资源管理器等应用,操作菜单或对话框,再读出屏幕上的结果。 |
|
|
21
|
+
|
|
22
|
+
## 一次操作是怎么完成的
|
|
23
|
+
|
|
24
|
+
- **先看清界面。** 模型可以读截图,也可以通过 Windows UI Automation 获取按钮、输入框及其内容。例如,直接定位「按钮:提交报名」;同名控件还能用 AutomationId 区分。截图上的坐标由插件换算,适配 Windows 的缩放和多显示器。
|
|
25
|
+
- **连续完成几步。** 「填字段 → 提交 → 等成功提示」可以放进同一个批次。等待条件满足就继续,结束时等画面稳定再回传截图,也可以只读出窗口文字。
|
|
26
|
+
- **处理常用应用时参考操作笔记。** 插件内置 Word、Excel、Chrome 等应用的技能,模型打开对应应用时会收到常用快捷键和操作步骤。你可以在设置页查看或编辑这些笔记。
|
|
27
|
+
|
|
28
|
+
## 操控期间,你仍然掌握主动权
|
|
29
|
+
|
|
30
|
+
默认按应用授权,DSH 会在需要时请求操作目标应用。开始操控后,橙色光晕标出操控状态,迷你卡片上方显示 DSH 提供的思考内容,下方保留操作时间线。按 **Esc** 暂停或继续,点 **停止** 结束本轮。你也可以直接在卡片上输入新指令,或者亲自接手键盘。
|
|
31
|
+
|
|
32
|
+
正文一开始输出,就回到 DSH 对话阅读。桌面版会恢复窗口并切回前台,Web GUI 会尝试回到任务开始时识别到的活动 DSH 标签。可查看[暂停时的卡片](assets/demo-paused.png)和[任务结束的画面](assets/demo-complete.png)。授权范围、截图和剪贴板的处理规则见[数据与权限](docs/privacy.md)。
|
|
33
|
+
|
|
34
|
+
## 安装并试一次
|
|
35
|
+
|
|
36
|
+
需要 **Windows 10 2004+ 或 Windows 11**,以及 **DeepSeek Harness 0.2.0-rc.2 或更新版本**。在 DSH 桌面版的 **插件 → 添加插件** 中输入 `@copylee/dsh-computer-use`,安装后启用。
|
|
37
|
+
|
|
38
|
+
第一次可以试试「打开计算器,算 1234 × 5678,然后读出结果」。支持图片输入的模型可以看截图;纯文本模型可以读取应用提供的无障碍控件和窗口文字。游戏、画布和部分自绘界面可能需要截图定位。Web GUI 安装方式和常见问题见[安装与排障](docs/getting-started.md)。
|
|
39
|
+
|
|
40
|
+
## 文档
|
|
41
|
+
|
|
42
|
+
| 文档 | 内容 |
|
|
43
|
+
| --- | --- |
|
|
44
|
+
| [安装与排障](docs/getting-started.md) | 桌面版与 Web 安装、第一次操作和常见问题 |
|
|
45
|
+
| [操作指南](docs/usage.md) | 示例任务、授权、暂停与接管、应用技能 |
|
|
46
|
+
| [设置参考](docs/settings.md) | 截图、输入方式、浮层和 Web GUI 地址 |
|
|
47
|
+
| [Agent 工具](docs/tools.md) | 工具清单、控件定位、批量动作与文字结果 |
|
|
48
|
+
| [数据与权限](docs/privacy.md) | 模型可见数据、截图缓存、剪贴板与 Windows 限制 |
|
|
49
|
+
| [开发与验证](docs/development.md) | 架构、构建、Windows 冒烟测试和本地联调 |
|
|
50
|
+
|
|
51
|
+
[MIT License](LICENSE)
|
package/helper/CuHelper.cs
CHANGED
|
@@ -82,6 +82,10 @@ namespace DshComputerUse
|
|
|
82
82
|
[DllImport("user32.dll")] public static extern IntPtr GetWindow(IntPtr hwnd, uint cmd);
|
|
83
83
|
[DllImport("user32.dll")] public static extern int GetWindowLong(IntPtr hwnd, int index);
|
|
84
84
|
[DllImport("user32.dll")] public static extern int SetWindowLong(IntPtr hwnd, int index, int value);
|
|
85
|
+
[DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)] public static extern bool SetProp(IntPtr hwnd, string name, IntPtr value);
|
|
86
|
+
[DllImport("user32.dll", CharSet = CharSet.Unicode)] public static extern IntPtr GetProp(IntPtr hwnd, string name);
|
|
87
|
+
[DllImport("user32.dll", CharSet = CharSet.Unicode)] public static extern IntPtr RemoveProp(IntPtr hwnd, string name);
|
|
88
|
+
[DllImport("user32.dll")] public static extern bool GetLayeredWindowAttributes(IntPtr hwnd, out uint key, out byte alpha, out uint flags);
|
|
85
89
|
[DllImport("user32.dll")] public static extern bool SetLayeredWindowAttributes(IntPtr hwnd, uint key, byte alpha, uint flags);
|
|
86
90
|
[DllImport("user32.dll")] public static extern uint GetWindowThreadProcessId(IntPtr hwnd, out uint pid);
|
|
87
91
|
[DllImport("user32.dll")] public static extern IntPtr GetForegroundWindow();
|
|
@@ -205,11 +209,13 @@ namespace DshComputerUse
|
|
|
205
209
|
var parser = new JavaScriptSerializer();
|
|
206
210
|
parser.MaxJsonLength = int.MaxValue;
|
|
207
211
|
Apps.Warm();
|
|
212
|
+
Windows.RecoverCards(false);
|
|
208
213
|
|
|
209
214
|
var hello = new Dictionary<string, object>();
|
|
210
215
|
hello["event"] = "ready"; hello["version"] = Version; hello["pid"] = Process.GetCurrentProcess().Id;
|
|
211
216
|
Emit(hello);
|
|
212
217
|
|
|
218
|
+
try {
|
|
213
219
|
string line;
|
|
214
220
|
while ((line = input.ReadLine()) != null)
|
|
215
221
|
{
|
|
@@ -232,7 +238,8 @@ namespace DshComputerUse
|
|
|
232
238
|
}
|
|
233
239
|
Emit(reply);
|
|
234
240
|
}
|
|
235
|
-
|
|
241
|
+
}
|
|
242
|
+
finally { Windows.RecoverCards(true); Overlay.Shutdown(); }
|
|
236
243
|
return 0;
|
|
237
244
|
}
|
|
238
245
|
|
|
@@ -283,6 +290,7 @@ namespace DshComputerUse
|
|
|
283
290
|
case "clipboard_release": Clip.Release(); return null;
|
|
284
291
|
case "paste": return Input.Paste(Args.Str(r, "text", ""), Args.Bool(r, "restore", true), Args.Int(r, "waitMs", 250));
|
|
285
292
|
case "windows": return Windows.List();
|
|
293
|
+
case "window_recover": Windows.RecoverCards(false); return null;
|
|
286
294
|
case "background": return Windows.Background(Args.Bool(r, "tray", true));
|
|
287
295
|
case "tray_restore": return Tray.Restore(Args.Str(r, "exe", ""));
|
|
288
296
|
case "card_opacity": CardLayer.SetOpacity(Args.Int(r, "opacity", 100)); return null;
|
|
@@ -299,6 +307,7 @@ namespace DshComputerUse
|
|
|
299
307
|
case "launch": return Apps.Launch(r);
|
|
300
308
|
case "open_url": return Apps.OpenUrl(Args.Str(r, "url", ""));
|
|
301
309
|
case "browser_tab": return Uia.BrowserTab(r);
|
|
310
|
+
case "browser_open_tab": return Uia.OpenBrowserTab(r);
|
|
302
311
|
case "act": return Uia.Act(r);
|
|
303
312
|
case "clipboard_get": { var d = new Dictionary<string, object>(); d["text"] = Clip.Get(); return d; }
|
|
304
313
|
case "clipboard_set": if (Args.Bool(r, "agent", false)) Clip.AgentSet(Args.Str(r, "text", "")); else Clip.Set(Args.Str(r, "text", ""), false); return null;
|
|
@@ -310,8 +319,8 @@ namespace DshComputerUse
|
|
|
310
319
|
case "window_card": CardLayer.SetOpacity(Args.Int(r, "opacity", 100)); return Windows.Card(new IntPtr(Convert.ToInt64(r["hwnd"])), Args.Int(r, "x", 0), Args.Int(r, "y", 0), Args.Int(r, "width", 0), Args.Int(r, "height", 0));
|
|
311
320
|
case "window_uncard": return Windows.Uncard(new IntPtr(Convert.ToInt64(r["hwnd"])));
|
|
312
321
|
case "overlay_status": Overlay.Status(Args.Str(r, "status", "")); return null;
|
|
313
|
-
case "pet_update": Overlay.PetUpdate(Args.Str(r, "
|
|
314
|
-
case "pet_finish": Overlay.Finish(Args.
|
|
322
|
+
case "pet_update": Overlay.PetUpdate(Args.Str(r, "thinking", ""), Args.Strs(r, "steps")); return null;
|
|
323
|
+
case "pet_finish": Overlay.Finish(Args.Int(r, "holdMs", 4000)); return null;
|
|
315
324
|
case "overlay_hide": Overlay.Hide(); return null;
|
|
316
325
|
default: throw new Exception("unknown command: " + cmd);
|
|
317
326
|
}
|
|
@@ -959,7 +968,9 @@ namespace DshComputerUse
|
|
|
959
968
|
{
|
|
960
969
|
var hwnd = Hwnd;
|
|
961
970
|
Hwnd = IntPtr.Zero;
|
|
962
|
-
|
|
971
|
+
lock (Gate) {
|
|
972
|
+
if (hwnd != IntPtr.Zero && Native.IsWindow(hwnd)) Native.SetWindowLong(hwnd, -20, originalEx);
|
|
973
|
+
}
|
|
963
974
|
}
|
|
964
975
|
|
|
965
976
|
public static Rectangle Frame(IntPtr hwnd)
|
|
@@ -1379,20 +1390,60 @@ namespace DshComputerUse
|
|
|
1379
1390
|
return d;
|
|
1380
1391
|
}
|
|
1381
1392
|
|
|
1382
|
-
|
|
1393
|
+
// Values live on the target HWND, not in helper memory. A replacement
|
|
1394
|
+
// helper can restore them after a timeout/kill, without stale HWND files.
|
|
1395
|
+
const string CardPrefix = "DSH.ComputerUse.Card.v1.";
|
|
1396
|
+
static readonly string[] CardFields = { "flags", "show", "minX", "minY", "maxX", "maxY", "left", "top", "right", "bottom", "ex", "key", "alpha", "layerFlags" };
|
|
1397
|
+
|
|
1398
|
+
static void SaveCard(IntPtr hwnd)
|
|
1399
|
+
{
|
|
1400
|
+
if (Native.GetProp(hwnd, CardPrefix + "owner") != IntPtr.Zero) return;
|
|
1401
|
+
var wp = new Native.WINDOWPLACEMENT(); wp.length = Marshal.SizeOf(wp);
|
|
1402
|
+
if (!Native.GetWindowPlacement(hwnd, ref wp)) throw new Exception("Cannot save host window placement");
|
|
1403
|
+
uint key, flags; byte alpha;
|
|
1404
|
+
Native.GetLayeredWindowAttributes(hwnd, out key, out alpha, out flags);
|
|
1405
|
+
int[] values = { wp.flags, wp.showCmd, wp.ptMinPosition.X, wp.ptMinPosition.Y, wp.ptMaxPosition.X, wp.ptMaxPosition.Y,
|
|
1406
|
+
wp.rcNormalPosition.Left, wp.rcNormalPosition.Top, wp.rcNormalPosition.Right, wp.rcNormalPosition.Bottom,
|
|
1407
|
+
Native.GetWindowLong(hwnd, -20), unchecked((int)key), alpha, unchecked((int)flags) };
|
|
1408
|
+
try {
|
|
1409
|
+
for (int i = 0; i < values.Length; i++)
|
|
1410
|
+
if (!Native.SetProp(hwnd, CardPrefix + CardFields[i], new IntPtr(values[i]))) throw new Exception("Cannot save host window state");
|
|
1411
|
+
// Commit marker is written last, before any window mutation.
|
|
1412
|
+
if (!Native.SetProp(hwnd, CardPrefix + "owner", new IntPtr(Process.GetCurrentProcess().Id))) throw new Exception("Cannot save host window owner");
|
|
1413
|
+
}
|
|
1414
|
+
catch { ClearCard(hwnd); throw; }
|
|
1415
|
+
}
|
|
1416
|
+
|
|
1417
|
+
static void ClearCard(IntPtr hwnd)
|
|
1418
|
+
{
|
|
1419
|
+
Native.RemoveProp(hwnd, CardPrefix + "owner");
|
|
1420
|
+
foreach (string field in CardFields) Native.RemoveProp(hwnd, CardPrefix + field);
|
|
1421
|
+
}
|
|
1422
|
+
|
|
1423
|
+
public static void RecoverCards(bool own)
|
|
1424
|
+
{
|
|
1425
|
+
int self = Process.GetCurrentProcess().Id;
|
|
1426
|
+
Native.EnumWindowsProc cb = delegate(IntPtr hwnd, IntPtr data) {
|
|
1427
|
+
int owner = Native.GetProp(hwnd, CardPrefix + "owner").ToInt32();
|
|
1428
|
+
if (owner == 0) return true;
|
|
1429
|
+
bool restore = own && owner == self;
|
|
1430
|
+
if (!own && owner != self) {
|
|
1431
|
+
try { using (var p = Process.GetProcessById(owner)) restore = p.HasExited; }
|
|
1432
|
+
catch (ArgumentException) { restore = true; }
|
|
1433
|
+
catch (Exception) { return true; } // do not touch a live/inaccessible helper
|
|
1434
|
+
}
|
|
1435
|
+
if (restore) { try { Uncard(hwnd); } catch (Exception) { } }
|
|
1436
|
+
return true;
|
|
1437
|
+
};
|
|
1438
|
+
Native.EnumWindows(cb, IntPtr.Zero); GC.KeepAlive(cb);
|
|
1439
|
+
}
|
|
1383
1440
|
|
|
1384
1441
|
/// Shrink a window into an always-on-top card; Uncard restores it exactly.
|
|
1385
1442
|
public static object Card(IntPtr hwnd, int x, int y, int w, int h)
|
|
1386
1443
|
{
|
|
1387
1444
|
if (!Native.IsWindow(hwnd)) throw new Exception("window no longer exists");
|
|
1445
|
+
SaveCard(hwnd);
|
|
1388
1446
|
CardLayer.Attach(hwnd);
|
|
1389
|
-
if (!cards.ContainsKey(hwnd.ToInt64()))
|
|
1390
|
-
{
|
|
1391
|
-
var wp = new Native.WINDOWPLACEMENT();
|
|
1392
|
-
wp.length = Marshal.SizeOf(typeof(Native.WINDOWPLACEMENT));
|
|
1393
|
-
Native.GetWindowPlacement(hwnd, ref wp);
|
|
1394
|
-
cards[hwnd.ToInt64()] = wp;
|
|
1395
|
-
}
|
|
1396
1447
|
// A minimized window restores to its previous state, which may be maximized: restore until normal.
|
|
1397
1448
|
for (int i = 0; i < 2 && (Native.IsIconic(hwnd) || Native.IsZoomed(hwnd)); i++) { Native.ShowWindow(hwnd, 9); Thread.Sleep(150); }
|
|
1398
1449
|
Native.SetWindowPos(hwnd, new IntPtr(-1), x, y, w, h, 0x0010 | 0x0040); // TOPMOST, NOACTIVATE | SHOWWINDOW
|
|
@@ -1409,19 +1460,25 @@ namespace DshComputerUse
|
|
|
1409
1460
|
public static object Uncard(IntPtr hwnd)
|
|
1410
1461
|
{
|
|
1411
1462
|
if (CardLayer.Hwnd == hwnd) CardLayer.Detach();
|
|
1412
|
-
Native.WINDOWPLACEMENT wp;
|
|
1413
|
-
bool known = cards.TryGetValue(hwnd.ToInt64(), out wp);
|
|
1414
|
-
cards.Remove(hwnd.ToInt64());
|
|
1415
1463
|
if (!Native.IsWindow(hwnd)) return null;
|
|
1416
|
-
Native.
|
|
1417
|
-
|
|
1418
|
-
|
|
1464
|
+
if (Native.GetProp(hwnd, CardPrefix + "owner") != IntPtr.Zero)
|
|
1465
|
+
{
|
|
1466
|
+
int[] v = new int[CardFields.Length];
|
|
1467
|
+
for (int i = 0; i < v.Length; i++) v[i] = Native.GetProp(hwnd, CardPrefix + CardFields[i]).ToInt32();
|
|
1468
|
+
var wp = new Native.WINDOWPLACEMENT(); wp.length = Marshal.SizeOf(wp);
|
|
1469
|
+
wp.flags = v[0]; wp.showCmd = v[1];
|
|
1470
|
+
wp.ptMinPosition = new Native.POINT(v[2], v[3]); wp.ptMaxPosition = new Native.POINT(v[4], v[5]);
|
|
1471
|
+
wp.rcNormalPosition = new Native.RECT { Left = v[6], Top = v[7], Right = v[8], Bottom = v[9] };
|
|
1472
|
+
Native.SetWindowLong(hwnd, -20, v[10]);
|
|
1473
|
+
if ((v[10] & 0x80000) != 0 && v[13] != 0) Native.SetLayeredWindowAttributes(hwnd, unchecked((uint)v[11]), (byte)v[12], unchecked((uint)v[13]));
|
|
1474
|
+
if (!Native.SetWindowPos(hwnd, new IntPtr((v[10] & 8) != 0 ? -1 : -2), 0, 0, 0, 0, 0x0001 | 0x0002 | 0x0010)) throw new Exception("Cannot restore host window style");
|
|
1419
1475
|
// Restore the normal rectangle first, then the original show state (maximized etc.).
|
|
1420
1476
|
int show = wp.showCmd;
|
|
1421
1477
|
wp.showCmd = 1; // SW_SHOWNORMAL
|
|
1422
|
-
Native.SetWindowPlacement(hwnd, ref wp);
|
|
1478
|
+
if (!Native.SetWindowPlacement(hwnd, ref wp)) throw new Exception("Cannot restore host window placement");
|
|
1423
1479
|
if (show == 3) Native.ShowWindow(hwnd, 3);
|
|
1424
1480
|
else if (show == 2) Native.ShowWindow(hwnd, 6);
|
|
1481
|
+
ClearCard(hwnd);
|
|
1425
1482
|
}
|
|
1426
1483
|
return Describe(hwnd);
|
|
1427
1484
|
}
|
|
@@ -1841,6 +1898,85 @@ namespace DshComputerUse
|
|
|
1841
1898
|
return false;
|
|
1842
1899
|
}
|
|
1843
1900
|
|
|
1901
|
+
static bool BrowserContentFocused()
|
|
1902
|
+
{
|
|
1903
|
+
for (var el = AutomationElement.FocusedElement; el != null; el = TreeWalker.ControlViewWalker.GetParent(el))
|
|
1904
|
+
{
|
|
1905
|
+
if (el.Current.ControlType == ControlType.Document) return true;
|
|
1906
|
+
if (el.Current.ControlType == ControlType.Window) return false;
|
|
1907
|
+
}
|
|
1908
|
+
return false;
|
|
1909
|
+
}
|
|
1910
|
+
|
|
1911
|
+
public static object OpenBrowserTab(Dictionary<string, object> r)
|
|
1912
|
+
{
|
|
1913
|
+
var hwnd = new IntPtr(Convert.ToInt64(r["hwnd"]));
|
|
1914
|
+
uint pid = Convert.ToUInt32(r["pid"]);
|
|
1915
|
+
string expected = Args.Str(r, "tab", "");
|
|
1916
|
+
Uri url;
|
|
1917
|
+
if (!Uri.TryCreate(Args.Str(r, "url", ""), UriKind.Absolute, out url) || (url.Scheme != "http" && url.Scheme != "https"))
|
|
1918
|
+
throw new Exception("Expected an HTTP(S) URL");
|
|
1919
|
+
Func<bool> current = delegate { return SameWindow(hwnd, pid) && Native.GetForegroundWindow() == hwnd; };
|
|
1920
|
+
Func<Dictionary<string, object>> read = delegate { return BrowserTab(r) as Dictionary<string, object>; };
|
|
1921
|
+
var initial = new Dictionary<string, object>(r) { { "listTabs", true } };
|
|
1922
|
+
var before = current() ? BrowserTab(initial) as Dictionary<string, object> : null;
|
|
1923
|
+
object tabIds;
|
|
1924
|
+
if (expected.Length == 0 || before == null || Args.Str(before, "tab", "") != expected || !current())
|
|
1925
|
+
throw new Exception("The browser window or active tab changed before opening a new tab");
|
|
1926
|
+
if (!before.TryGetValue("tabs", out tabIds) || !(tabIds is List<string>))
|
|
1927
|
+
throw new Exception("Could not verify the browser's existing tabs");
|
|
1928
|
+
var existing = new HashSet<string>((List<string>)tabIds);
|
|
1929
|
+
if (!existing.Contains(expected)) throw new Exception("The active browser tab could not be verified");
|
|
1930
|
+
|
|
1931
|
+
Input.Keys(new Dictionary<string, object> { { "combos", new[] { new[] { 0x11, 0x54 } } } });
|
|
1932
|
+
var clock = Stopwatch.StartNew();
|
|
1933
|
+
string created = "";
|
|
1934
|
+
while (clock.ElapsedMilliseconds < 2400 && current())
|
|
1935
|
+
{
|
|
1936
|
+
var fresh = read();
|
|
1937
|
+
if (fresh != null && !existing.Contains(Args.Str(fresh, "tab", ""))
|
|
1938
|
+
&& Args.Str(fresh, "url", "unavailable") == "")
|
|
1939
|
+
{
|
|
1940
|
+
created = Args.Str(fresh, "tab", "");
|
|
1941
|
+
if (created.Length > 0) break;
|
|
1942
|
+
}
|
|
1943
|
+
Thread.Sleep(65);
|
|
1944
|
+
}
|
|
1945
|
+
if (created.Length == 0 || !current())
|
|
1946
|
+
throw new Exception("Could not confirm a new blank tab in the original browser window");
|
|
1947
|
+
Input.Keys(new Dictionary<string, object> { { "combos", new[] { new[] { 0x11, 0x4C } } } });
|
|
1948
|
+
clock.Restart();
|
|
1949
|
+
bool addressReady = false;
|
|
1950
|
+
while (clock.ElapsedMilliseconds < 1200 && current())
|
|
1951
|
+
{
|
|
1952
|
+
var blank = read();
|
|
1953
|
+
if (blank != null)
|
|
1954
|
+
{
|
|
1955
|
+
if (Args.Str(blank, "tab", "") != created || Args.Str(blank, "url", "unavailable") != "") break;
|
|
1956
|
+
if (Args.Bool(blank, "addressFocused", false)) { addressReady = true; break; }
|
|
1957
|
+
}
|
|
1958
|
+
Thread.Sleep(65);
|
|
1959
|
+
}
|
|
1960
|
+
if (!addressReady || !current()) throw new Exception("Could not verify focus in the new tab's address bar");
|
|
1961
|
+
Input.Paste(url.AbsoluteUri, true, 120);
|
|
1962
|
+
clock.Restart();
|
|
1963
|
+
while (clock.ElapsedMilliseconds < 1200 && current())
|
|
1964
|
+
{
|
|
1965
|
+
var address = read();
|
|
1966
|
+
if (address != null)
|
|
1967
|
+
{
|
|
1968
|
+
if (Args.Str(address, "tab", "") != created || !Args.Bool(address, "addressFocused", false)) break;
|
|
1969
|
+
if (Args.Str(address, "url", "") == url.AbsoluteUri && current())
|
|
1970
|
+
{
|
|
1971
|
+
Input.Keys(new Dictionary<string, object> { { "combos", new[] { new[] { 0x0D } } } });
|
|
1972
|
+
return new Dictionary<string, object> { { "tab", created } };
|
|
1973
|
+
}
|
|
1974
|
+
}
|
|
1975
|
+
Thread.Sleep(65);
|
|
1976
|
+
}
|
|
1977
|
+
throw new Exception("Could not verify the new tab's address before navigation");
|
|
1978
|
+
}
|
|
1979
|
+
|
|
1844
1980
|
public static object BrowserTab(Dictionary<string, object> r)
|
|
1845
1981
|
{
|
|
1846
1982
|
IntPtr hwnd = new IntPtr(Convert.ToInt64(r["hwnd"]));
|
|
@@ -1858,9 +1994,10 @@ namespace DshComputerUse
|
|
|
1858
1994
|
new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit),
|
|
1859
1995
|
new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.TabItem)));
|
|
1860
1996
|
var state = new Dictionary<string, object>();
|
|
1997
|
+
var tabs = Args.Bool(r, "listTabs", false) ? new List<string>() : null;
|
|
1861
1998
|
foreach (AutomationElement el in controls)
|
|
1862
1999
|
{
|
|
1863
|
-
if (el.Current.IsOffscreen) continue;
|
|
2000
|
+
if (el.Current.IsOffscreen && (tabs == null || el.Current.ControlType != ControlType.TabItem)) continue;
|
|
1864
2001
|
object pattern;
|
|
1865
2002
|
if (el.Current.ControlType == ControlType.Edit)
|
|
1866
2003
|
{
|
|
@@ -1868,12 +2005,16 @@ namespace DshComputerUse
|
|
|
1868
2005
|
string name = el.Current.Name;
|
|
1869
2006
|
if (id != "view_1012" && id != "urlbar-input" && name != "Address and search bar" && name != "地址和搜索栏") continue;
|
|
1870
2007
|
if (BrowserChrome(el) && el.TryGetCurrentPattern(ValuePattern.Pattern, out pattern))
|
|
2008
|
+
{
|
|
1871
2009
|
state["url"] = ((ValuePattern)pattern).Current.Value;
|
|
2010
|
+
state["addressFocused"] = el.Current.HasKeyboardFocus;
|
|
2011
|
+
}
|
|
1872
2012
|
}
|
|
1873
2013
|
else if (el.TryGetCurrentPattern(SelectionItemPattern.Pattern, out pattern) && BrowserChrome(el))
|
|
1874
2014
|
{
|
|
1875
2015
|
var tab = (SelectionItemPattern)pattern;
|
|
1876
2016
|
string id = String.Join(",", el.GetRuntimeId());
|
|
2017
|
+
if (tabs != null) tabs.Add(id);
|
|
1877
2018
|
if (select.Length > 0 && id == select)
|
|
1878
2019
|
{
|
|
1879
2020
|
if (clock.ElapsedMilliseconds < 1200 && SameWindow(hwnd, pid) && Native.GetForegroundWindow() == hwnd)
|
|
@@ -1886,6 +2027,17 @@ namespace DshComputerUse
|
|
|
1886
2027
|
if (tab.Current.IsSelected) state["tab"] = id;
|
|
1887
2028
|
}
|
|
1888
2029
|
}
|
|
2030
|
+
if (tabs != null) state["tabs"] = tabs;
|
|
2031
|
+
if (Args.Bool(r, "details", false) && Native.GetForegroundWindow() == hwnd)
|
|
2032
|
+
{
|
|
2033
|
+
var render = ChromiumRoot(hwnd);
|
|
2034
|
+
Native.RECT rect;
|
|
2035
|
+
if (render != hwnd && Native.GetWindowRect(render, out rect) && rect.Right > rect.Left && rect.Bottom > rect.Top)
|
|
2036
|
+
state["content"] = new Dictionary<string, object> {
|
|
2037
|
+
{ "x", rect.Left }, { "y", rect.Top }, { "width", rect.Right - rect.Left }, { "height", rect.Bottom - rect.Top },
|
|
2038
|
+
};
|
|
2039
|
+
state["contentFocused"] = BrowserContentFocused();
|
|
2040
|
+
}
|
|
1889
2041
|
result = state;
|
|
1890
2042
|
}
|
|
1891
2043
|
catch (Exception) { }
|
|
@@ -2314,9 +2466,8 @@ namespace DshComputerUse
|
|
|
2314
2466
|
static bool petMode;
|
|
2315
2467
|
static LayeredForm pet;
|
|
2316
2468
|
public static volatile IntPtr PetHwnd = IntPtr.Zero;
|
|
2317
|
-
static string
|
|
2469
|
+
static string petThinking = "";
|
|
2318
2470
|
static List<string> petSteps = new List<string>();
|
|
2319
|
-
static bool petThinkingLatest;
|
|
2320
2471
|
/** Bottom-right corner of the card body; the card grows upward when expanded. */
|
|
2321
2472
|
static Point petAnchor = Point.Empty;
|
|
2322
2473
|
static int petShadow;
|
|
@@ -2393,7 +2544,8 @@ namespace DshComputerUse
|
|
|
2393
2544
|
if (visible && !petFinished) { RenderPill(); return; }
|
|
2394
2545
|
if (visible) HideCore(); // the previous run's "done" card is still up
|
|
2395
2546
|
petMode = Args.Bool(r, "pet", false);
|
|
2396
|
-
|
|
2547
|
+
petThinking = Args.Str(r, "thinking", "");
|
|
2548
|
+
petSteps = new List<string>();
|
|
2397
2549
|
if (status.Length > 0) petSteps.Add(status);
|
|
2398
2550
|
pillAtBottom = false;
|
|
2399
2551
|
paused = false;
|
|
@@ -2461,23 +2613,21 @@ namespace DshComputerUse
|
|
|
2461
2613
|
Ui(delegate { status = newStatus; RenderPill(); });
|
|
2462
2614
|
}
|
|
2463
2615
|
|
|
2464
|
-
/**
|
|
2465
|
-
public static void PetUpdate(string
|
|
2616
|
+
/** Latest host-provided reasoning and recent operations, oldest first. */
|
|
2617
|
+
public static void PetUpdate(string thinking, List<string> steps)
|
|
2466
2618
|
{
|
|
2467
2619
|
if (thread == null || !visible || !petMode || petFinished) return;
|
|
2468
2620
|
invoker.BeginInvoke((MethodInvoker)delegate
|
|
2469
2621
|
{
|
|
2470
2622
|
if (petFinished) return;
|
|
2471
|
-
petText = text;
|
|
2472
2623
|
petThinking = thinking;
|
|
2473
2624
|
if (steps.Count > 0) petSteps = steps;
|
|
2474
|
-
petThinkingLatest = thinkingLatest;
|
|
2475
2625
|
RenderPill();
|
|
2476
2626
|
});
|
|
2477
2627
|
}
|
|
2478
2628
|
|
|
2479
2629
|
/** The run ended: the card says so for a moment (longer while the pointer rests on it), then everything hides. */
|
|
2480
|
-
public static void Finish(
|
|
2630
|
+
public static void Finish(int holdMs)
|
|
2481
2631
|
{
|
|
2482
2632
|
if (thread == null) return;
|
|
2483
2633
|
Ui(delegate
|
|
@@ -2488,7 +2638,6 @@ namespace DshComputerUse
|
|
|
2488
2638
|
glows.Clear();
|
|
2489
2639
|
RemoveHook();
|
|
2490
2640
|
paused = false;
|
|
2491
|
-
if (text.Length > 0) petText = text;
|
|
2492
2641
|
petFinished = true;
|
|
2493
2642
|
finishedAt = DateTime.UtcNow;
|
|
2494
2643
|
finishHoldMs = holdMs;
|
|
@@ -2791,6 +2940,7 @@ namespace DshComputerUse
|
|
|
2791
2940
|
petShadow = shadow;
|
|
2792
2941
|
float bodyW = 420 * s, pad = 16 * s;
|
|
2793
2942
|
float headH = 44 * s, replyH = 26 * s;
|
|
2943
|
+
string progress = petFinished ? "已交还电脑控制" : petThinking.Length > 0 ? "思考 · " + petThinking : "正在操作,等待下一步…";
|
|
2794
2944
|
if (petExpanded)
|
|
2795
2945
|
{
|
|
2796
2946
|
// Grow with the reply, up to about eight lines.
|
|
@@ -2798,14 +2948,12 @@ namespace DshComputerUse
|
|
|
2798
2948
|
using (var pg = Graphics.FromImage(probe))
|
|
2799
2949
|
using (var pf = new Font("Microsoft YaHei UI", 10.5f * 1.333f * scale, FontStyle.Regular, GraphicsUnit.Pixel))
|
|
2800
2950
|
{
|
|
2801
|
-
|
|
2802
|
-
float need = pg.MeasureString(t, pf, new SizeF(420 * scale - 32 * scale, 100000), new StringFormat(StringFormat.GenericTypographic)).Height + 6 * scale;
|
|
2951
|
+
float need = pg.MeasureString(progress, pf, new SizeF(420 * scale - 32 * scale, 100000), new StringFormat(StringFormat.GenericTypographic)).Height + 6 * scale;
|
|
2803
2952
|
replyH = Math.Max(26 * scale, Math.Min(172 * scale, need));
|
|
2804
2953
|
}
|
|
2805
2954
|
}
|
|
2806
2955
|
int stepRows = petExpanded ? Math.Min(5, Math.Max(1, petSteps.Count)) : 0;
|
|
2807
|
-
|
|
2808
|
-
float listH = petExpanded ? 8 * s + (thinkingRow ? 24 * s : 0) + stepRows * 22 * s : 24 * s;
|
|
2956
|
+
float listH = petExpanded ? 8 * s + stepRows * 22 * s : 24 * s;
|
|
2809
2957
|
float inputH = input != null ? 46 * s : 0;
|
|
2810
2958
|
float bodyH = headH + replyH + listH + inputH + 12 * s;
|
|
2811
2959
|
int W = (int)Math.Ceiling(bodyW) + 2 * shadow, H = (int)Math.Ceiling(bodyH) + 2 * shadow;
|
|
@@ -2885,17 +3033,18 @@ namespace DshComputerUse
|
|
|
2885
3033
|
using (var b = new SolidBrush(paused ? Accent : muted)) g.DrawString(st, fState, b, tx, hy + (hh - stSize.Height) / 2f, fmt);
|
|
2886
3034
|
}
|
|
2887
3035
|
|
|
2888
|
-
//
|
|
3036
|
+
// Reasoning and operations use separate rows throughout.
|
|
2889
3037
|
float lineW = bodyW - 2 * pad;
|
|
2890
3038
|
float ry = body.Y + headH;
|
|
2891
|
-
|
|
2892
|
-
using (var b = new SolidBrush(petText.Length > 0 ? ink : muted))
|
|
3039
|
+
using (var b = new SolidBrush(petThinking.Length > 0 && !petFinished ? ink : muted))
|
|
2893
3040
|
{
|
|
2894
3041
|
if (petExpanded)
|
|
2895
|
-
g.DrawString(TailWrapped(g,
|
|
3042
|
+
g.DrawString(TailWrapped(g, progress, fText, lineW, replyH, wrap), fText, b, new RectangleF(body.X + pad, ry, lineW, replyH), wrap);
|
|
2896
3043
|
else
|
|
2897
3044
|
{
|
|
2898
|
-
string shown =
|
|
3045
|
+
string shown = petThinking.Length > 0 && !petFinished
|
|
3046
|
+
? "思考 · " + Tail(g, petThinking, fText, lineW - g.MeasureString("思考 · ", fText, 100000, fmt).Width, fmt)
|
|
3047
|
+
: Tail(g, progress, fText, lineW, fmt);
|
|
2899
3048
|
SizeF size = g.MeasureString(shown.Length > 0 ? shown : " ", fText, 100000, fmt);
|
|
2900
3049
|
g.DrawString(shown, fText, b, body.X + pad, ry + (replyH - size.Height) / 2f, fmt);
|
|
2901
3050
|
}
|
|
@@ -2908,13 +3057,6 @@ namespace DshComputerUse
|
|
|
2908
3057
|
ly += 4 * s;
|
|
2909
3058
|
using (var pen = new Pen(edge, Math.Max(1f, s))) g.DrawLine(pen, body.X + pad, ly, body.Right - pad, ly);
|
|
2910
3059
|
ly += 4 * s;
|
|
2911
|
-
if (thinkingRow)
|
|
2912
|
-
{
|
|
2913
|
-
float prefixW = g.MeasureString("思考:", fAct, 100000, fmt).Width;
|
|
2914
|
-
string th = Tail(g, petThinking, fAct, lineW - prefixW, fmt);
|
|
2915
|
-
using (var b = new SolidBrush(muted)) { g.DrawString("思考:", fAct, b, body.X + pad, ly + 3 * s, fmt); g.DrawString(th, fAct, b, body.X + pad + prefixW, ly + 3 * s, fmt); }
|
|
2916
|
-
ly += 24 * s;
|
|
2917
|
-
}
|
|
2918
3060
|
int first = Math.Max(0, petSteps.Count - stepRows);
|
|
2919
3061
|
for (int i = first; i < petSteps.Count; i++)
|
|
2920
3062
|
{
|
|
@@ -2930,8 +3072,7 @@ namespace DshComputerUse
|
|
|
2930
3072
|
else
|
|
2931
3073
|
{
|
|
2932
3074
|
string act;
|
|
2933
|
-
if (petFinished) act = petSteps.Count > 0 ? "
|
|
2934
|
-
else if (petThinkingLatest && petThinking.Length > 0) act = "思考:" + Tail(g, petThinking, fAct, lineW - 14 * s - g.MeasureString("思考:", fAct, 100000, fmt).Width, fmt);
|
|
3075
|
+
if (petFinished) act = petSteps.Count > 0 ? "最后操作 · " + petSteps[petSteps.Count - 1] : "";
|
|
2935
3076
|
else
|
|
2936
3077
|
{
|
|
2937
3078
|
var parts = new List<string>();
|