appport-bridge 0.4.0__tar.gz

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 (33) hide show
  1. appport_bridge-0.4.0/LICENSE +21 -0
  2. appport_bridge-0.4.0/PKG-INFO +353 -0
  3. appport_bridge-0.4.0/README.md +304 -0
  4. appport_bridge-0.4.0/pyproject.toml +58 -0
  5. appport_bridge-0.4.0/setup.cfg +4 -0
  6. appport_bridge-0.4.0/src/appport_bridge/__init__.py +3 -0
  7. appport_bridge-0.4.0/src/appport_bridge/cli.py +732 -0
  8. appport_bridge-0.4.0/src/appport_bridge/config.py +385 -0
  9. appport_bridge-0.4.0/src/appport_bridge/diagnostics.py +261 -0
  10. appport_bridge-0.4.0/src/appport_bridge/humanize.py +253 -0
  11. appport_bridge-0.4.0/src/appport_bridge/mcp_server.py +441 -0
  12. appport_bridge-0.4.0/src/appport_bridge/meta_tools.py +518 -0
  13. appport_bridge-0.4.0/src/appport_bridge/recorder.py +1113 -0
  14. appport_bridge-0.4.0/src/appport_bridge/solidify.py +1035 -0
  15. appport_bridge-0.4.0/src/appport_bridge/tools.py +472 -0
  16. appport_bridge-0.4.0/src/appport_bridge/uia.py +2182 -0
  17. appport_bridge-0.4.0/src/appport_bridge.egg-info/PKG-INFO +353 -0
  18. appport_bridge-0.4.0/src/appport_bridge.egg-info/SOURCES.txt +31 -0
  19. appport_bridge-0.4.0/src/appport_bridge.egg-info/dependency_links.txt +1 -0
  20. appport_bridge-0.4.0/src/appport_bridge.egg-info/entry_points.txt +3 -0
  21. appport_bridge-0.4.0/src/appport_bridge.egg-info/requires.txt +16 -0
  22. appport_bridge-0.4.0/src/appport_bridge.egg-info/top_level.txt +1 -0
  23. appport_bridge-0.4.0/tests/test_cli.py +399 -0
  24. appport_bridge-0.4.0/tests/test_config_humanize.py +243 -0
  25. appport_bridge-0.4.0/tests/test_diagnostics.py +270 -0
  26. appport_bridge-0.4.0/tests/test_locate_cache.py +349 -0
  27. appport_bridge-0.4.0/tests/test_mcp_server.py +221 -0
  28. appport_bridge-0.4.0/tests/test_meta_tools.py +507 -0
  29. appport_bridge-0.4.0/tests/test_recorder.py +780 -0
  30. appport_bridge-0.4.0/tests/test_smoke.py +26 -0
  31. appport_bridge-0.4.0/tests/test_solidify.py +961 -0
  32. appport_bridge-0.4.0/tests/test_tools.py +180 -0
  33. appport_bridge-0.4.0/tests/test_uia.py +729 -0
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 AppPort contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,353 @@
1
+ Metadata-Version: 2.4
2
+ Name: appport-bridge
3
+ Version: 0.4.0
4
+ Summary: 给没有 API 的 Windows 桌面软件开一个 MCP 端口:UIA → ToolDefinition → MCP/CLI 双出口
5
+ Author: RookieApe
6
+ License: MIT License
7
+
8
+ Copyright (c) 2026 AppPort contributors
9
+
10
+ Permission is hereby granted, free of charge, to any person obtaining a copy
11
+ of this software and associated documentation files (the "Software"), to deal
12
+ in the Software without restriction, including without limitation the rights
13
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
+ copies of the Software, and to permit persons to whom the Software is
15
+ furnished to do so, subject to the following conditions:
16
+
17
+ The above copyright notice and this permission notice shall be included in all
18
+ copies or substantial portions of the Software.
19
+
20
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26
+ SOFTWARE.
27
+ Project-URL: Homepage, https://github.com/RookieApe-tao/appport-bridge
28
+ Project-URL: Source, https://github.com/RookieApe-tao/appport-bridge
29
+ Keywords: mcp,ui-automation,windows,automation,llm
30
+ Classifier: Programming Language :: Python :: 3.11
31
+ Classifier: Programming Language :: Python :: 3.12
32
+ Classifier: Environment :: Win32 (MS Windows)
33
+ Classifier: Operating System :: Microsoft :: Windows
34
+ Classifier: License :: OSI Approved :: MIT License
35
+ Requires-Python: >=3.11
36
+ Description-Content-Type: text/markdown
37
+ License-File: LICENSE
38
+ Requires-Dist: mcp<3,>=2
39
+ Requires-Dist: pywinauto>=0.6.9; sys_platform == "win32"
40
+ Requires-Dist: typer>=0.12
41
+ Requires-Dist: pyyaml>=6.0
42
+ Provides-Extra: uiautomation
43
+ Requires-Dist: uiautomation>=2.0; sys_platform == "win32" and extra == "uiautomation"
44
+ Provides-Extra: dev
45
+ Requires-Dist: pytest>=8; extra == "dev"
46
+ Requires-Dist: pytest-cov>=5; extra == "dev"
47
+ Requires-Dist: ruff>=0.6; extra == "dev"
48
+ Dynamic: license-file
49
+
50
+ # appport-bridge
51
+
52
+ [![CI](https://github.com/RookieApe-tao/appport-bridge/actions/workflows/ci.yml/badge.svg)](https://github.com/RookieApe-tao/appport-bridge/actions/workflows/ci.yml)
53
+
54
+ 给没有 API 的 Windows 桌面软件开一个 **MCP 端口** —— 让 AI agent 通过 MCP 工具直接驱动它们。
55
+
56
+ 业务模式:开源内核 + 企业特性。姊妹项目 [appport-sdk](https://github.com/RookieApe-tao/appport-sdk) 是对应的 SDK:用 Python 定义一次工具,同时得到 MCP server 和 CLI。
57
+
58
+ ## 架构
59
+
60
+ ```
61
+ Windows 桌面软件
62
+ ▲
63
+ │ UIA(pywinauto 优先 / uiautomation 备选)
64
+ ┌─────┴──────────────────────┐
65
+ │ uia.py —— 原语引擎 │ list_windows / inspect_window / click / set_text / read_value / press_key
66
+ └─────┬──────────────────────┘
67
+ │ 只被 solidify 调用
68
+ ┌─────▼──────────────────────┐
69
+ │ solidify.py —— 声明式固化层 │ adapters/notepad.yaml ──► write_note(text) / read_note() / clear_note()
70
+ │ │ adapters/calculator.yaml ──► calculate(a,b,operator) / read_display() / ...
71
+ └─────▲──────────┬───────────┘
72
+ │ │ 统一是 ToolDefinition v0.1
73
+ │ ┌──────▼──────────┬──────────┐
74
+ │ │ MCP server │ CLI │ 两个出口共用同一份定义、schema 生成与返回包装
75
+ │ │ (stdio) │ (typer) │
76
+ │ └─────────────────┴──────────┘
77
+ │
78
+ ┌─────┴──────────────────────┐
79
+ │ recorder.py —— 录制器 │ 操作 → session JSON → solidify → adapter YAML
80
+ └────────────────────────────┘
81
+ ```
82
+
83
+ 一句话:**UIA → ToolDefinition → MCP / CLI 双出口**。工具的声明、参数校验、返回包装只有一份。
84
+
85
+ 模型看到的常是**语义工具**(`write_note`),不是 `click_control(name="保存")` 这种原语 —— 操作路径由 adapter 文件固化,模型不必猜控件名。原语仍然保留,用于调试和写新 adapter。
86
+
87
+ 手写 YAML 不是唯一入口:`recorder.py` 把一次真实操作录成 session,再经 `solidify` 生成 adapter 草稿(见「录制」一节)。
88
+
89
+ ## 安装
90
+
91
+ 需要 Windows 10/11 与 Python 3.11+。
92
+
93
+ ```powershell
94
+ git clone https://github.com/RookieApe-tao/appport-bridge.git
95
+ cd appport-bridge
96
+ python -m venv .venv
97
+ .\.venv\Scripts\python.exe -m pip install -e ".[dev]"
98
+ ```
99
+
100
+ 只装运行时依赖(`mcp`、`pywinauto`、`typer`、`pyyaml`):
101
+
102
+ ```powershell
103
+ .\.venv\Scripts\python.exe -m pip install -e .
104
+ ```
105
+
106
+ 想要 `uiautomation` 作为备选后端:`pip install -e ".[uiautomation]"`。
107
+
108
+ ## 快速开始
109
+
110
+ ### 1. 声明一个 adapter(可选)
111
+
112
+ 已有 `adapters/notepad.yaml`、`adapters/calculator.yaml` 可直接用,先校验:
113
+
114
+ ```powershell
115
+ appport-bridge validate --adapter adapters\notepad.yaml --adapter adapters\calculator.yaml
116
+ ```
117
+
118
+ ```json
119
+ {"ok": true, "data": {"adapters": [{"adapter": "notepad", "version": "1.0",
120
+ "tools": [{"name": "write_note", "params": ["text"], "required": ["text"],
121
+ "steps": 3, "actions": ["set_text", "read_value", "read_value"]}, ...]}]}}
122
+ ```
123
+
124
+ 写法见 [docs/adapter-guide.md](docs/adapter-guide.md)。
125
+
126
+ ### 2. MCP 出口
127
+
128
+ 唯一入口是 stdio:
129
+
130
+ ```powershell
131
+ python -m appport_bridge.mcp_server --adapter adapters\notepad.yaml
132
+ # 或安装后:
133
+ appport-bridge-mcp --adapter adapters\notepad.yaml
134
+ ```
135
+
136
+ 注册到 MCP 客户端(以 Claude Desktop 风格为例):
137
+
138
+ ```json
139
+ {
140
+ "mcpServers": {
141
+ "appport-bridge": {
142
+ "command": "<仓库路径>\\.venv\\Scripts\\python.exe",
143
+ "args": ["-m", "appport_bridge.mcp_server",
144
+ "--adapter", "<仓库路径>\\adapters\\notepad.yaml"],
145
+ "env": { "PYTHONPATH": "<仓库路径>\\src" }
146
+ }
147
+ }
148
+ }
149
+ ```
150
+
151
+ 把 `<仓库路径>` 换成你 clone 下来的目录(例如 `%USERPROFILE%\\src\\appport-bridge`)。
152
+ 如果已经 `pip install -e .`,可以直接用 `"command": "appport-bridge-mcp"`,
153
+ 此时 `PYTHONPATH` 可省略,`--adapter` 建议仍写绝对路径(MCP 客户端的工作目录不固定)。
154
+
155
+ - **有 `--adapter`**:只暴露语义工具(记事本 = `write_note` / `read_note` / `clear_note`)。
156
+ - **加 `--primitives`**:语义工具 + 5 个原语工具 + 4 个 meta 工具一起暴露,方便调试。
157
+ - **两个都不给(默认档)**:暴露 5 个原语 `list_windows`、`inspect_window`、`click_control`、`set_text`、`read_value` **加** 4 个 meta 工具,共 9 个——纯语义出口是「有 adapter」那一档的待遇,没有 adapter 时只给原语等于连固化入口都不给。
158
+
159
+ `--primitives` 会带出 4 个 **meta 工具**,让模型可以只靠 MCP 走完「探索 → 固化」闭环:
160
+ `read_session`(分页读 record session)、`solidify_from_session`(session → adapter YAML 文本)、
161
+ `validate_adapter`(校验 YAML 文本)、`run_adapter_tool`(就地回放 YAML 文本)。
162
+ 配合 `--record-file <路径>` 使用(**必须与 `--primitives` 一起给**),所有原语调用会自动
163
+ 追加进同一个 session 格式,模型探索完直接 `solidify_from_session` 就能拿到 adapter:
164
+
165
+ ```powershell
166
+ python -m appport_bridge.mcp_server --primitives --record-file $env:TEMP\probe.json
167
+ ```
168
+
169
+ 给 AI 用的完整操作手册(可直接当系统提示词):[docs/agent-workflow.md](docs/agent-workflow.md);
170
+ 一份真机端到端实测记录(模型独立为 7-Zip 产出 `list_files`)见 [docs/agent-e2e.md](docs/agent-e2e.md)。
171
+
172
+ `inputSchema` 完全由 ToolDefinition 生成,未声明的参数会被拒绝(`INVALID_ARGUMENT`)。
173
+
174
+ ### 3. CLI 出口
175
+
176
+ ```powershell
177
+ # 语义工具(推荐)
178
+ appport-bridge run write-note --adapter adapters\notepad.yaml --arg "text=你好"
179
+ appport-bridge run calculate --adapter adapters\calculator.yaml --arg a=12 --arg b=34 --arg operator=+
180
+
181
+ # 原语(调试用)
182
+ appport-bridge list-windows
183
+ appport-bridge inspect-window --window 记事本 --max-depth 3
184
+ appport-bridge set-text --window 记事本 --text "你好" --mode value
185
+ appport-bridge read-value --window 记事本
186
+ appport-bridge click-control --window 记事本 --name 保存
187
+ ```
188
+
189
+ 参数名 / 类型 / 是否必填都从同一份 ToolDefinition 映射而来;输出恒为单个 JSON 对象,`ok=false` 时进程退出码为 1。
190
+
191
+ 窗口不唯一时用 `--pid` / `--window-title` 收窄(会覆盖 adapter 的 `target`):
192
+
193
+ ```powershell
194
+ appport-bridge run write-note --adapter adapters\notepad.yaml --pid 12345 --arg "text=hi"
195
+ ```
196
+
197
+ `--pid` 是**硬约束**而不是兜底:给定时只会在这个进程的窗口里找。同时给 `--window` 与 `--pid` 时两者都必须匹配;只给 `--pid` 而该进程有多个窗口时会返回 `WINDOW_AMBIGUOUS` 让你补标题,而不会挑一个继续——挑错窗口就是操作别人的软件。
198
+
199
+ `diagnose` 同理:`diagnose --pid 12345` 只诊断该进程的窗口;`--pid` 指向不存在的进程时直接报 `WINDOW_NOT_FOUND`,不会退回前台窗口。
200
+
201
+ 点不动的时候先诊断一次——它把「控件树有多大」和「是不是 UWP 被挂起了」一起报出来:
202
+
203
+ ```powershell
204
+ appport-bridge diagnose --window 计算器
205
+ # {"ok": true, "data": {"window": "计算器", "pid": 4242, "backend": "pywinauto",
206
+ # "node_count": 51, "plm_suspected": false, "suspended": []}}
207
+ ```
208
+
209
+ `node_count` 只有个位数、`plm_suspected=true` 时,说明应用被 PLM 挂起、内容树没挂载,
210
+ 先手动把窗口点到前台等界面画出来再重试,而不是去改 `locate`。
211
+
212
+ ### 4. 录制 → 固化 → 回放
213
+
214
+ 不想手写 YAML 时,把一次真实操作录下来,再把 session 固化成 adapter 草稿。
215
+
216
+ 最省事的方式是给任意 CLI 动作加 `--record`:动作照常执行,成功后追加进 session。
217
+
218
+ ```powershell
219
+ # 1) 录制:--record 只给短名,默认落到 %LOCALAPPDATA%\AppPort\sessions\notepad-demo.json
220
+ appport-bridge set-text --window "无标题" --control-type Document --text "你好" --record notepad-demo
221
+ appport-bridge read-value --window "无标题" --control-type Document --record notepad-demo
222
+
223
+ # 2) 固化:第 1 步的文本参数化成 {{text}}
224
+ appport-bridge solidify --from "$env:LOCALAPPDATA\AppPort\sessions\notepad-demo.json" `
225
+ --out examples\adapters\recorded.yaml --tool append_note --parametrize 1=text `
226
+ --window-title-contains "记事本"
227
+
228
+ # 3) 回放
229
+ appport-bridge run append-note --adapter examples\adapters\recorded.yaml --arg "text=第二次"
230
+ ```
231
+
232
+ 也可以进一个交互循环,一步步试、试完存(`quit` 退出):
233
+
234
+ ```powershell
235
+ appport-bridge record notepad-demo --window "记事本"
236
+ ```
237
+
238
+ 固化产物的定位是**级联**的:同一份 `locate` 里同时写 `automation_id`、`name` + `control_type` + `class_name`、父链 `path`,回放时按这个顺序逐档降级,实际命中的档位报在 `steps[].located_by` 里(`automation_id` / `name` / `path` / `all`)。所以控件名被软件改掉、或名字里混进了用户键入的文本,都还能按父链点回去。
239
+
240
+ 几点约定:
241
+
242
+ > **分享的边界**:session JSON 是**本地工作产物**,按设计保留键入明文(方便回看与排障),默认落 `%LOCALAPPDATA%`,不进 git。
243
+ > **不要分享 session**。可交换的是 **adapter YAML**,而它要先用 `--parametrize` 把值抽成参数、再用 `--redact-defaults` 抹掉剩余默认值。一句话:session 自己留着,adapter 才是拿来交换的。
244
+
245
+ - **session 不进仓库、不进项目目录**。文件里有你键入的文本,默认写 `%LOCALAPPDATA%\AppPort\sessions\`(`sessions/` 已在 `.gitignore` 兜底),可用 `APPPORT_SESSION_DIR` 或 `--record <路径>` 覆盖。固化出 adapter 后 session 可以直接删。
246
+ - **不做加密存储**。session 是排障用的中间稿,加密只会让"为什么回放失败"更难查;靠改默认目录 + 不进 git + 用完删来控制暴露面。
247
+ - **密码框不落盘**。`IsPassword=True` 的控件不记录键入内容,固化成 `{{secret_N}}` 参数,结果字段里的值也会被替换成 `<secret>`。
248
+ - **导出的 adapter 可能写死了你的数据**。未参与 `--parametrize` 的文本值会被列出来警告;分享前加 `--redact-defaults`,它会把这些值连同参数默认值一起抹掉。
249
+
250
+ 示例产物见 [`examples/adapters/recorded-notepad.yaml`](examples/adapters/recorded-notepad.yaml),完整说明见 [docs/recorder.md](docs/recorder.md)。
251
+
252
+ 另一个例子是第三方软件 **Everything**:把「搜文件」录成 2 步、固化成单个 `search_files` 工具,回放和 MCP 真机调用都拿到真实结果:
253
+
254
+ ```powershell
255
+ appport-bridge run search-files --adapter examples\adapters\everything-search.yaml `
256
+ --arg "query=appport" --arg "limit=5"
257
+ ```
258
+
259
+ adapter 见 [`examples/adapters/everything-search.yaml`](examples/adapters/everything-search.yaml),从探索到 MCP 调用每一步的真实输出见 [docs/everything-search.md](docs/everything-search.md)。
260
+
261
+ 这个例子还顺带暴露了一个通用问题:Everything 的窗口标题跟着搜索词走,第 1 步写完词标题就变了,第 2 步按旧标题定位会失配。固化的执行器因此采用**步级懒重解析**——正常路径不额外开销,撞到 `WINDOW_NOT_FOUND` 才按 target 重新解析该步(再不行按旧 pid 收敛),并在 `steps[]` 里标 `window_reresolved: true`。另存为、切标签页这类会改标题的软件同理,细节见 [docs/everything-search.md 第 10 节](docs/everything-search.md)。
262
+
263
+ 第三个例子是 **7-Zip**,也是唯一一个"模型自己产出"的:只通过 MCP 出口(`--primitives --record-file`)加一份 [docs/agent-workflow.md](docs/agent-workflow.md),从零探索把「浏览目录」固化成 `list_files(path, limit)`——三步(地址栏写路径 → **回车确认** → 按行读列表),三个选择器全是 `automation_id`:
264
+
265
+ > `press_key` 是 adapter 引擎支持的**动作**,但不在 MCP 原语出口里(出口只有那 5 个原语),所以录制阶段录不到它,第 2 步是固化成 YAML 时手工补的。这一条也写进了 [docs/agent-workflow.md](docs/agent-workflow.md) §7。
266
+
267
+ ```powershell
268
+ appport-bridge run list-files --adapter examples\adapters\sevenzip-list.yaml `
269
+ --arg "path=D:\Project\appport-bridge" --arg limit=3
270
+ ```
271
+
272
+ adapter 见 [`examples/adapters/sevenzip-list.yaml`](examples/adapters/sevenzip-list.yaml),完整过程(含真机踩坑、**点地址栏下拉箭头只会展开历史列表而不会导航**这个假阳性、以及由此修掉的产品缺陷)见 [docs/agent-e2e.md](docs/agent-e2e.md)。
273
+
274
+ ### 5. 真机演示
275
+
276
+ ```powershell
277
+ .\.venv\Scripts\python.exe examples\demo_notepad.py
278
+ ```
279
+
280
+ 启动记事本(临时文件)→ 枚举窗口 → 打印控件树摘要 → `write_note` 写入 → `read_note` 回读校验 → `clear_note` 清空 → 关闭进程。每一步打印一段 JSON。
281
+
282
+ ### 6. 多软件兼容性
283
+
284
+ ```powershell
285
+ .\.venv\Scripts\python.exe scripts\compat_suite.py all
286
+ ```
287
+
288
+ 对记事本 / 计算器 / mspaint / 资源管理器 / Windows Terminal / Everything 各跑一遍核心链路,结果见 [docs/compatibility.md](docs/compatibility.md)。
289
+
290
+ ## 配置
291
+
292
+ 项目根目录的 `config.toml`(可用 `--config` 指定别的路径):
293
+
294
+ | 段 | 键 | 默认 | 说明 |
295
+ |----|----|------|------|
296
+ | `timeouts` | `com_timeout_ms` | 8000 | 单次 COM/UIA 调用超时 |
297
+ | | `search_timeout_ms` | 3000 | 控件查找超时 |
298
+ | `humanize` | `enabled` | true | 拟人化动作开关 |
299
+ | | `click_jitter_px` | 3 | 点击抖动量 |
300
+ | | `pre_click_delay_ms` / `post_click_delay_ms` | 50–200 / 50–150 | 点击前后随机延迟区间 |
301
+ | | `typing_delay_ms` | 30–120 | 逐字符键入延迟区间 |
302
+ | | `focus_settle_ms` | 80 | 抢前台后的等待 |
303
+ | `inspect` | `max_depth` | 4 | **展示**用控件树深度 |
304
+ | | `include_disabled` | false | 是否包含不可用控件 |
305
+ | `locate` | `search_depth` | 8 | **定位**用搜索深度(与展示深度分开) |
306
+ | | `priority` | `["automation_id", "name"]` | 定位字段优先级 |
307
+ | `tree` | `max_children` | 100 | 单节点最多取多少个子节点,`0` 为不限制 |
308
+ | `cache` | `enabled` | true | 定位缓存开关,`false` 回到每次重走控件树 |
309
+ | | `ttl_ms` | 5000 | 缓存条目寿命,`0` 为不过期 |
310
+ | | `max_entries` | 256 | 控件条目上限(LRU),`0` 关闭缓存 |
311
+
312
+ `cache` 段是 0.4.0 加的定位缓存:同一窗口重复定位从 ~180 ms 降到 ~3 ms。
313
+ 它**只加速、不改变语义**——命中后先做一次廉价的活体校验(`runtime_id` + 进程 + 四个身份字段),
314
+ 任何一项对不上就丢条目回退全量枚举,所以不会点到已经消失或换了身份的控件。
315
+ 详见 [docs/adapter-guide.md](docs/adapter-guide.md) 的定位一节。
316
+
317
+ `tree.max_children` 是为"大结果集窗口"准备的:Everything 会把 9000+ 个搜索结果当成 UIA 子项暴露,全量枚举一次就超过 `com_timeout_ms`。设上限后按需取前 N 个,被裁剪的节点在快照里标 `truncated: true`——裁剪是可见的,不会把"没走到那么深"伪装成"没有这个控件"。
318
+
319
+ `inspect.max_depth` 与 `locate.search_depth` 分开是刻意的:控件树给人看要浅,点击定位要钻到底。
320
+
321
+ ## 已知限制
322
+
323
+ 完整的多软件实测与分析见 [docs/compatibility.md](docs/compatibility.md)。
324
+
325
+ - **仅 Windows**。依赖 UI Automation,macOS / Linux 不支持。
326
+ - **自绘窗口不可读**。DirectX / canvas / 自绘画布(画图、部分游戏)没有 ValuePattern,能点到但**无法验证操作结果**。
327
+ - **无法验证像素**。没有截图比对能力,不能判断「画得对不对」。
328
+ - **模态对话框会挡住一切**。窗口 `enabled=False` 通常意味着有对话框在前面,需要先关掉它。
329
+ - **单个进程的 UIA 卡死会拖垮全局枚举**。第三方软件(实测 Everything 建索引时)会让 `list_windows` 从 0.8s 拖到 11s+,超时后会直接丢失窗口列表。
330
+ - **模拟键入依赖前台窗口**。`set_text(mode="type")` 会短暂抢焦点(已用 `AttachThreadInput` 处理,并做回读校验),无人值守场景优先用 `mode="value"`。
331
+ - **读值是文本快照,不是结构化语义**。容器控件(List / StatusBar)返回的是子项文本拼接。
332
+ - **定位依赖 UIA 语义**。原生控件经常 `Name` 为空、只有数字 `automation_id`;XAML 应用的 `name` 会被本地化和状态污染,优先用 `automation_id`。
333
+ - **长会话下 UWP 内容树可能整体降级**。UWP 应用(计算器、设置、画图等)长时间运行后,UIA 可能只暴露外壳 8 个节点,应用内容树整个消失,此时点击会命中不到控件。重启 `explorer.exe` 可恢复。
334
+
335
+ ### adapter 层的限制
336
+
337
+ - **一个 adapter 只服务一款软件的一个窗口**。多标签页软件(浏览器、记事本)靠 `window_title_contains` 区分,命中多个会返回 `WINDOW_AMBIGUOUS`。
338
+ - **adapter 是"写死的流程"**。界面改版(改控件名、加一步确认框)就要改 YAML;这正是它可校验、可复现的代价。
339
+ - **不支持循环、条件、超时重试**。MVP 只有顺序 steps;需要"最多等 5 秒直到出现"这类逻辑时,等后续版本。
340
+ - **`press_key` 走按键语法**(`ctrl+s`、`enter`),`type_text` 才是字面量输入。要给计算器输入 `12+34=` 必须用 `type_text`,因为 `+` 在按键语法里是 Shift 修饰符(已做转义,但仍建议区分)。
341
+ - **expect 是断言不是修复**。第 N 步校验失败会整体 `ok=false` 并报「第 N 步校验失败」,不会自动重试。
342
+
343
+ ## 开发
344
+
345
+ ```powershell
346
+ .\.venv\Scripts\python.exe -m pytest tests/ -q
347
+ ```
348
+
349
+ 测试全部基于内存假后端(`tests/conftest.py::FakeBackend`),不需要真实窗口,可在 CI 里跑。adapter 的解析 / 校验 / 步骤执行也全部离线覆盖(`tests/test_solidify.py`)。
350
+
351
+ ## License
352
+
353
+ MIT