@becrafter/prompt-manager 0.1.22 → 0.1.31
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/package.json +23 -23
- package/packages/resources/tools/agent-browser/README.md +640 -0
- package/packages/resources/tools/agent-browser/agent-browser.tool.js +1388 -0
- package/packages/resources/tools/thinking/README.md +324 -0
- package/packages/resources/tools/thinking/thinking.tool.js +911 -0
- package/packages/server/README.md +3 -4
- package/packages/server/api/admin.routes.js +668 -664
- package/packages/server/api/open.routes.js +68 -67
- package/packages/server/api/surge.routes.js +5 -6
- package/packages/server/api/tool.routes.js +70 -71
- package/packages/server/app.js +70 -73
- package/packages/server/configs/authors.json +40 -0
- package/packages/server/configs/models/built-in/bigmodel.yaml +6 -6
- package/packages/server/configs/models/providers.yaml +4 -4
- package/packages/server/configs/templates/built-in/general-iteration.yaml +1 -1
- package/packages/server/configs/templates/built-in/general-optimize.yaml +1 -1
- package/packages/server/configs/templates/built-in/output-format-optimize.yaml +1 -1
- package/packages/server/index.js +3 -9
- package/packages/server/mcp/heartbeat-patch.js +1 -3
- package/packages/server/mcp/mcp.server.js +64 -134
- package/packages/server/mcp/prompt.handler.js +101 -95
- package/packages/server/middlewares/auth.middleware.js +31 -31
- package/packages/server/server.js +60 -45
- package/packages/server/services/TerminalService.js +156 -70
- package/packages/server/services/WebSocketService.js +35 -34
- package/packages/server/services/author-config.service.js +199 -0
- package/packages/server/services/manager.js +66 -60
- package/packages/server/services/model.service.js +5 -9
- package/packages/server/services/optimization.service.js +25 -22
- package/packages/server/services/template.service.js +3 -8
- package/packages/server/toolm/author-sync.service.js +97 -0
- package/packages/server/toolm/index.js +1 -2
- package/packages/server/toolm/package-installer.service.js +47 -50
- package/packages/server/toolm/tool-context.service.js +64 -62
- package/packages/server/toolm/tool-dependency.service.js +28 -30
- package/packages/server/toolm/tool-description-generator-optimized.service.js +55 -55
- package/packages/server/toolm/tool-description-generator.service.js +20 -23
- package/packages/server/toolm/tool-environment.service.js +45 -44
- package/packages/server/toolm/tool-execution.service.js +49 -48
- package/packages/server/toolm/tool-loader.service.js +13 -18
- package/packages/server/toolm/tool-logger.service.js +33 -39
- package/packages/server/toolm/tool-manager.handler.js +17 -15
- package/packages/server/toolm/tool-manual-generator.service.js +107 -87
- package/packages/server/toolm/tool-mode-handlers.service.js +52 -59
- package/packages/server/toolm/tool-storage.service.js +11 -12
- package/packages/server/toolm/tool-sync.service.js +36 -39
- package/packages/server/toolm/tool-utils.js +0 -1
- package/packages/server/toolm/tool-yaml-parser.service.js +12 -11
- package/packages/server/toolm/validate-system.js +56 -84
- package/packages/server/utils/config.js +97 -12
- package/packages/server/utils/logger.js +1 -1
- package/packages/server/utils/port-checker.js +8 -8
- package/packages/server/utils/util.js +470 -467
- package/packages/resources/tools/cognitive-thinking/README.md +0 -284
- package/packages/resources/tools/cognitive-thinking/cognitive-thinking.tool.js +0 -837
- package/packages/server/mcp/sequential-thinking.handler.js +0 -318
- package/packages/server/mcp/think-plan.handler.js +0 -274
- package/packages/server/mcp/thinking-toolkit.handler.js +0 -380
- package/packages/web/0.d1c5a72339dfc32ad86a.js +0 -1
- package/packages/web/112.8807b976372b2b0541a8.js +0 -1
- package/packages/web/130.584c7e365da413f5d9be.js +0 -1
- package/packages/web/142.72c985bc29720f975cca.js +0 -1
- package/packages/web/165.a05fc53bf84d18db36b8.js +0 -2
- package/packages/web/165.a05fc53bf84d18db36b8.js.LICENSE.txt +0 -9
- package/packages/web/203.724ab9f717b80554c397.js +0 -1
- package/packages/web/241.bf941d4f02866795f64a.js +0 -1
- package/packages/web/249.54cfb224af63f5f5ec55.js +0 -1
- package/packages/web/291.6df35042f8f296fca7cd.js +0 -1
- package/packages/web/319.2fab900a31b29873f666.js +0 -1
- package/packages/web/32.c78d866281995ec33a7b.js +0 -1
- package/packages/web/325.9ca297d0f73f38468ce9.js +0 -1
- package/packages/web/366.2f9b48fdbf8eee039e57.js +0 -1
- package/packages/web/378.6be08c612cd5a3ef97dc.js +0 -1
- package/packages/web/393.7a2f817515c5e90623d7.js +0 -1
- package/packages/web/412.062df5f732d5ba203415.js +0 -1
- package/packages/web/426.08656fef4918b3fb19ad.js +0 -1
- package/packages/web/465.2be8018327130a3bd798.js +0 -1
- package/packages/web/48.8ca96fc93667a715e67a.js +0 -1
- package/packages/web/480.44c1f1a2927486ac3d4f.js +0 -1
- package/packages/web/489.e041a8d0db15dc96d607.js +0 -1
- package/packages/web/490.9ffb26c907de020d671b.js +0 -1
- package/packages/web/492.58781369e348d91fc06a.js +0 -1
- package/packages/web/495.ed63e99791a87167c6b3.js +0 -1
- package/packages/web/510.4cc07ab7d30d5c1cd17f.js +0 -1
- package/packages/web/543.3af155ed4fa237664308.js +0 -1
- package/packages/web/567.f04ab60f8e2c2fb0745a.js +0 -1
- package/packages/web/592.f3ad085fa9c1849daa06.js +0 -1
- package/packages/web/616.b03fb801b3433b17750f.js +0 -1
- package/packages/web/617.d88def54921d2c4dc44c.js +0 -1
- package/packages/web/641.d30787d674f548928261.js +0 -1
- package/packages/web/672.5269c8399fa42a5af95d.js +0 -1
- package/packages/web/731.97cab92b71811c502bda.js +0 -1
- package/packages/web/746.3947c6f0235407e420fb.js +0 -1
- package/packages/web/756.a53233b3f3913900d5ac.js +0 -1
- package/packages/web/77.68801af593a28a631fbf.js +0 -1
- package/packages/web/802.53b2bff3cf2a69f7b80c.js +0 -1
- package/packages/web/815.b6dfab82265f56c7e046.js +0 -1
- package/packages/web/821.f5a13e5c735aac244eb9.js +0 -1
- package/packages/web/846.b9bf97d5f559270675ce.js +0 -1
- package/packages/web/869.7c10403f500e6201407f.js +0 -1
- package/packages/web/885.135050364f99e6924fb5.js +0 -1
- package/packages/web/901.fd5aeb9df630609a2b43.js +0 -1
- package/packages/web/928.f67e590de3caa4daa3ae.js +0 -1
- package/packages/web/955.d833403521ba4dd567ee.js +0 -1
- package/packages/web/981.a45cb745cf424044c8c8.js +0 -1
- package/packages/web/992.645320b60c74c8787482.js +0 -1
- package/packages/web/996.ed9a963dc9e7439eca9a.js +0 -1
- package/packages/web/css/codemirror-theme_xq-light.css +0 -43
- package/packages/web/css/codemirror.css +0 -344
- package/packages/web/css/main.196f434e6a88cd448158.css +0 -7278
- package/packages/web/css/terminal-fix.css +0 -571
- package/packages/web/index.html +0 -3
- package/packages/web/main.dceff50c7307dda04873.js +0 -2
- package/packages/web/main.dceff50c7307dda04873.js.LICENSE.txt +0 -3
|
@@ -0,0 +1,640 @@
|
|
|
1
|
+
# agent-browser 工具
|
|
2
|
+
|
|
3
|
+
AI 友好的浏览器自动化工具,基于 Vercel Labs 的 agent-browser npm 包,提供基于引用的确定性元素选择和 93% 上下文节省。
|
|
4
|
+
|
|
5
|
+
## 功能特性
|
|
6
|
+
|
|
7
|
+
- ✅ **AI 优先设计** - 专为 Claude Code、Cursor、Copilot 等 AI 智能体优化
|
|
8
|
+
- ✅ **93% 上下文节省** - 通过 Snapshot + Refs 机制,仅返回简化的元素引用列表
|
|
9
|
+
- ✅ **确定性选择** - 使用 `@e1`、`@e2` 引用代替易变的 CSS 选择器
|
|
10
|
+
- ✅ **完整功能** - 支持 50+ 命令,涵盖导航、表单、截图、网络、存储等
|
|
11
|
+
- ✅ **会话管理** - 支持多个隔离的浏览器实例
|
|
12
|
+
- ✅ **多浏览器支持** - Chromium、Firefox、WebKit
|
|
13
|
+
- ✅ **直接 API** - 使用 BrowserManager 类,避免 CLI 解析开销
|
|
14
|
+
|
|
15
|
+
## 安装
|
|
16
|
+
|
|
17
|
+
此工具会自动安装 `agent-browser` npm 包到工具的 node_modules 目录。
|
|
18
|
+
|
|
19
|
+
首次使用时可能需要安装 Chromium 浏览器:
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
# 自动安装(首次启动时)
|
|
23
|
+
# 或手动安装
|
|
24
|
+
npm install agent-browser
|
|
25
|
+
npx agent-browser install
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## 核心概念
|
|
29
|
+
|
|
30
|
+
### Refs(元素引用)
|
|
31
|
+
|
|
32
|
+
Ref 系统是 agent-browser 的核心特性,提供确定性的元素选择:
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
# 1. 获取快照(带 refs)
|
|
36
|
+
action: snapshot
|
|
37
|
+
|
|
38
|
+
# 输出示例:
|
|
39
|
+
# - heading "Example Domain" [ref=e1]
|
|
40
|
+
# - button "Submit" [ref=e2]
|
|
41
|
+
# - textbox "Email" [ref=e3]
|
|
42
|
+
# - link "Learn more" [ref=e4]
|
|
43
|
+
|
|
44
|
+
# 2. 使用 refs 进行交互
|
|
45
|
+
action: click
|
|
46
|
+
selector: "@e2" # 点击按钮
|
|
47
|
+
|
|
48
|
+
action: fill
|
|
49
|
+
selector: "@e3" # 填充邮箱字段
|
|
50
|
+
text: "test@example.com"
|
|
51
|
+
|
|
52
|
+
action: click
|
|
53
|
+
selector: "@e2" # 提交表单
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
**为什么使用 refs?**
|
|
57
|
+
- ✅ **确定性** - Ref 指向快照中的确切元素
|
|
58
|
+
- ✅ **快速** - 无需重新查询 DOM
|
|
59
|
+
- ✅ **AI 友好** - Snapshot + ref 工作流对 LLM 最优
|
|
60
|
+
|
|
61
|
+
## 使用方式
|
|
62
|
+
|
|
63
|
+
### 方式一:通过 Prompt Manager 工具系统调用
|
|
64
|
+
|
|
65
|
+
#### 基本工作流
|
|
66
|
+
|
|
67
|
+
```yaml
|
|
68
|
+
# 1. 启动浏览器
|
|
69
|
+
tool: tool://agent-browser
|
|
70
|
+
mode: execute
|
|
71
|
+
parameters:
|
|
72
|
+
action: launch
|
|
73
|
+
options:
|
|
74
|
+
headless: false # 显示浏览器窗口用于调试
|
|
75
|
+
|
|
76
|
+
# 2. 导航到 URL
|
|
77
|
+
tool: tool://agent-browser
|
|
78
|
+
mode: execute
|
|
79
|
+
parameters:
|
|
80
|
+
action: navigate
|
|
81
|
+
url: "https://example.com"
|
|
82
|
+
waitUntil: "load"
|
|
83
|
+
|
|
84
|
+
# 3. 获取快照(交互元素)
|
|
85
|
+
tool: tool://agent-browser
|
|
86
|
+
mode: execute
|
|
87
|
+
parameters:
|
|
88
|
+
action: snapshot
|
|
89
|
+
interactive: true
|
|
90
|
+
|
|
91
|
+
# 4. 使用 refs 点击元素
|
|
92
|
+
tool: tool://agent-browser
|
|
93
|
+
mode: execute
|
|
94
|
+
parameters:
|
|
95
|
+
action: click
|
|
96
|
+
selector: "@e2"
|
|
97
|
+
|
|
98
|
+
# 5. 填充表单
|
|
99
|
+
tool: tool://agent-browser
|
|
100
|
+
mode: execute
|
|
101
|
+
parameters:
|
|
102
|
+
action: fill
|
|
103
|
+
selector: "@e3"
|
|
104
|
+
value: "test@example.com"
|
|
105
|
+
|
|
106
|
+
# 6. 截图
|
|
107
|
+
tool: tool://agent-browser
|
|
108
|
+
mode: execute
|
|
109
|
+
parameters:
|
|
110
|
+
action: screenshot
|
|
111
|
+
screenshotPath: "/path/to/screenshot.png"
|
|
112
|
+
fullPage: true
|
|
113
|
+
|
|
114
|
+
# 7. 关闭浏览器
|
|
115
|
+
tool: tool://agent-browser
|
|
116
|
+
mode: execute
|
|
117
|
+
parameters:
|
|
118
|
+
action: close
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
#### 表单填充示例
|
|
122
|
+
|
|
123
|
+
```yaml
|
|
124
|
+
# 填充登录表单
|
|
125
|
+
tool: tool://agent-browser
|
|
126
|
+
mode: execute
|
|
127
|
+
parameters:
|
|
128
|
+
action: fill
|
|
129
|
+
selector: "#username"
|
|
130
|
+
value: "myusername"
|
|
131
|
+
|
|
132
|
+
tool: tool://agent-browser
|
|
133
|
+
mode: execute
|
|
134
|
+
parameters:
|
|
135
|
+
action: fill
|
|
136
|
+
selector: "#password"
|
|
137
|
+
value: "mypassword"
|
|
138
|
+
|
|
139
|
+
tool: tool://agent-browser
|
|
140
|
+
mode: execute
|
|
141
|
+
parameters:
|
|
142
|
+
action: click
|
|
143
|
+
selector: "#login-button"
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
#### 语义定位器示例
|
|
147
|
+
|
|
148
|
+
```yaml
|
|
149
|
+
# 通过角色查找并点击
|
|
150
|
+
tool: tool://agent-browser
|
|
151
|
+
mode: execute
|
|
152
|
+
parameters:
|
|
153
|
+
action: getbyrole
|
|
154
|
+
role: "button"
|
|
155
|
+
name: "Submit"
|
|
156
|
+
subaction: "click"
|
|
157
|
+
|
|
158
|
+
# 通过文本查找并填充
|
|
159
|
+
tool: tool://agent-browser
|
|
160
|
+
mode: execute
|
|
161
|
+
parameters:
|
|
162
|
+
action: getbytext
|
|
163
|
+
text: "Email"
|
|
164
|
+
subaction: "fill"
|
|
165
|
+
value: "user@example.com"
|
|
166
|
+
|
|
167
|
+
# 通过标签查找并操作
|
|
168
|
+
tool: tool://agent-browser
|
|
169
|
+
mode: execute
|
|
170
|
+
parameters:
|
|
171
|
+
action: getbylabel
|
|
172
|
+
label: "Password"
|
|
173
|
+
subaction: "fill"
|
|
174
|
+
value: "secret123"
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
### 方式二:高级方法调用
|
|
178
|
+
|
|
179
|
+
工具内部也提供高级方法,可以直接调用:
|
|
180
|
+
|
|
181
|
+
```javascript
|
|
182
|
+
// 获取带引用的快照
|
|
183
|
+
const snapshot = await tool.getSnapshotWithRefs({
|
|
184
|
+
interactive: true,
|
|
185
|
+
maxDepth: 5
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
// 导航
|
|
189
|
+
await tool.navigate('https://example.com', {
|
|
190
|
+
headless: false
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
// 使用引用交互
|
|
194
|
+
await tool.click('@e2');
|
|
195
|
+
await tool.fill('@e3', 'test@example.com');
|
|
196
|
+
|
|
197
|
+
// 截图
|
|
198
|
+
await tool.screenshot('/path/to/screenshot.png', {
|
|
199
|
+
fullPage: true
|
|
200
|
+
});
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
## 完整操作列表
|
|
204
|
+
|
|
205
|
+
### 核心命令
|
|
206
|
+
|
|
207
|
+
| Action | 描述 | 参数 |
|
|
208
|
+
|--------|------|------|
|
|
209
|
+
| `launch` | 启动浏览器 | `headless`, `viewport`, `device`, `executablePath` |
|
|
210
|
+
| `navigate` | 导航到 URL | `url`, `waitUntil`, `headers` |
|
|
211
|
+
| `click` | 点击元素 | `selector`, `button`, `clickCount`, `delay` |
|
|
212
|
+
| `type` | 输入文本 | `selector`, `text`, `delay`, `clear` |
|
|
213
|
+
| `fill` | 填充输入框 | `selector`, `value` |
|
|
214
|
+
| `check` | 勾选复选框 | `selector` |
|
|
215
|
+
| `uncheck` | 取消勾选 | `selector` |
|
|
216
|
+
| `upload` | 上传文件 | `selector`, `files` |
|
|
217
|
+
| `dblclick` | 双击 | `selector` |
|
|
218
|
+
| `focus` | 聚焦元素 | `selector` |
|
|
219
|
+
| `drag` | 拖拽 | `source`, `target` |
|
|
220
|
+
| `screenshot` | 截图 | `screenshotPath`, `fullPage`, `screenshotFormat`, `quality` |
|
|
221
|
+
| `snapshot` | 获取快照 | `interactive`, `maxDepth`, `compact`, `selector` |
|
|
222
|
+
| `evaluate` | 执行 JavaScript | `script`, `scriptArgs` |
|
|
223
|
+
| `wait` | 等待 | `selector`, `timeout`, `state` |
|
|
224
|
+
| `scroll` | 滚动 | `selector`, `direction`, `amount`, `x`, `y` |
|
|
225
|
+
| `select` | 选择下拉选项 | `selector`, `values` |
|
|
226
|
+
| `hover` | 悬停 | `selector` |
|
|
227
|
+
| `content` | 获取内容 | `selector` |
|
|
228
|
+
| `close` | 关闭浏览器 | - |
|
|
229
|
+
| `back` | 后退 | - |
|
|
230
|
+
| `forward` | 前进 | - |
|
|
231
|
+
| `reload` | 刷新 | - |
|
|
232
|
+
|
|
233
|
+
### 语义定位器
|
|
234
|
+
|
|
235
|
+
| Action | 描述 | 参数 |
|
|
236
|
+
|--------|------|------|
|
|
237
|
+
| `getbyrole` | 按 ARIA 角色查找 | `role`, `name`, `subaction`, `value` |
|
|
238
|
+
| `getbytext` | 按文本内容查找 | `text`, `exact`, `subaction` |
|
|
239
|
+
| `getbylabel` | 按标签查找 | `label`, `subaction`, `value` |
|
|
240
|
+
| `getbyplaceholder` | 按占位符查找 | `placeholder`, `subaction`, `value` |
|
|
241
|
+
| `getbyalttext` | 按 alt 文本查找 | `altText`, `exact`, `subaction` |
|
|
242
|
+
| `getbytitle` | 按 title 属性查找 | `name`, `exact`, `subaction` |
|
|
243
|
+
| `getbytestid` | 按 data-testid 查找 | `testId`, `subaction`, `value` |
|
|
244
|
+
| `nth` | 第 N 个元素 | `selector`, `index`, `subaction`, `value` |
|
|
245
|
+
|
|
246
|
+
### 信息获取
|
|
247
|
+
|
|
248
|
+
| Action | 描述 | 参数 |
|
|
249
|
+
|--------|------|------|
|
|
250
|
+
| `gettext` | 获取文本 | `selector` |
|
|
251
|
+
| `gethtml` | 获取 HTML | `selector` |
|
|
252
|
+
| `getattribute` | 获取属性 | `selector`, `attribute` |
|
|
253
|
+
| `isvisible` | 检查可见性 | `selector` |
|
|
254
|
+
| `isenabled` | 检查是否启用 | `selector` |
|
|
255
|
+
| `ischecked` | 检查是否勾选 | `selector` |
|
|
256
|
+
| `count` | 计数元素 | `selector` |
|
|
257
|
+
| `boundingbox` | 获取边界框 | `selector` |
|
|
258
|
+
| `url` | 获取当前 URL | - |
|
|
259
|
+
| `title` | 获取页面标题 | - |
|
|
260
|
+
|
|
261
|
+
### 标签页和窗口
|
|
262
|
+
|
|
263
|
+
| Action | 描述 | 参数 |
|
|
264
|
+
|--------|------|------|
|
|
265
|
+
| `tab_new` | 新建标签页 | - |
|
|
266
|
+
| `tab_list` | 列出标签页 | - |
|
|
267
|
+
| `tab_switch` | 切换标签页 | `index` |
|
|
268
|
+
| `tab_close` | 关闭标签页 | `index` |
|
|
269
|
+
| `window_new` | 新建窗口 | `viewport` |
|
|
270
|
+
|
|
271
|
+
### Cookie 和存储
|
|
272
|
+
|
|
273
|
+
| Action | 描述 | 参数 |
|
|
274
|
+
|--------|------|------|
|
|
275
|
+
| `cookies_get` | 获取 Cookies | `urls` |
|
|
276
|
+
| `cookies_set` | 设置 Cookies | `cookies` |
|
|
277
|
+
| `cookies_clear` | 清除 Cookies | - |
|
|
278
|
+
| `storage_get` | 获取存储 | `key`, `storageType` |
|
|
279
|
+
| `storage_set` | 设置存储 | `key`, `value`, `storageType` |
|
|
280
|
+
| `storage_clear` | 清除存储 | `storageType` |
|
|
281
|
+
|
|
282
|
+
### 网络和路由
|
|
283
|
+
|
|
284
|
+
| Action | 描述 | 参数 |
|
|
285
|
+
|--------|------|------|
|
|
286
|
+
| `route` | 添加路由 | `routeUrl`, `responseBody`, `abort` |
|
|
287
|
+
| `unroute` | 移除路由 | `routeUrl` |
|
|
288
|
+
| `requests` | 查看请求 | `filter`, `clearRequests` |
|
|
289
|
+
| `download` | 下载文件 | `selector`, `downloadPath` |
|
|
290
|
+
|
|
291
|
+
### 设置和模拟
|
|
292
|
+
|
|
293
|
+
| Action | 描述 | 参数 |
|
|
294
|
+
|--------|------|------|
|
|
295
|
+
| `viewport` | 设置视口 | `width`, `height` |
|
|
296
|
+
| `device` | 设备模拟 | `device` |
|
|
297
|
+
| `geolocation` | 地理位置 | `latitude`, `longitude`, `accuracy` |
|
|
298
|
+
| `permissions` | 权限设置 | `permissions`, `grant` |
|
|
299
|
+
| `useragent` | User Agent | `userAgent` |
|
|
300
|
+
| `timezone` | 时区 | `timezone` |
|
|
301
|
+
| `locale` | 区域设置 | `locale` |
|
|
302
|
+
| `offline` | 离线模式 | `offline` |
|
|
303
|
+
| `headers` | HTTP 头部 | `headers` |
|
|
304
|
+
|
|
305
|
+
### 高级功能
|
|
306
|
+
|
|
307
|
+
| Action | 描述 | 参数 |
|
|
308
|
+
|--------|------|------|
|
|
309
|
+
| `dialog` | 处理对话框 | `dialogResponse`, `promptText` |
|
|
310
|
+
| `pdf` | 保存为 PDF | `pdfPath`, `format` |
|
|
311
|
+
| `tracing` | 追踪 | `tracePath` |
|
|
312
|
+
| `har` | HAR 录制 | `harPath` |
|
|
313
|
+
| `console` | 控制台消息 | `clearConsole` |
|
|
314
|
+
| `errors` | 页面错误 | `clearErrors` |
|
|
315
|
+
|
|
316
|
+
## 完整示例
|
|
317
|
+
|
|
318
|
+
### 示例 1:登录流程
|
|
319
|
+
|
|
320
|
+
```yaml
|
|
321
|
+
# 1. 导航到登录页
|
|
322
|
+
tool: tool://agent-browser
|
|
323
|
+
mode: execute
|
|
324
|
+
parameters:
|
|
325
|
+
action: navigate
|
|
326
|
+
url: "https://example.com/login"
|
|
327
|
+
|
|
328
|
+
# 2. 获取快照
|
|
329
|
+
tool: tool://agent-browser
|
|
330
|
+
mode: execute
|
|
331
|
+
parameters:
|
|
332
|
+
action: snapshot
|
|
333
|
+
interactive: true
|
|
334
|
+
|
|
335
|
+
# 3. 填充用户名
|
|
336
|
+
tool: tool://agent-browser
|
|
337
|
+
mode: execute
|
|
338
|
+
parameters:
|
|
339
|
+
action: fill
|
|
340
|
+
selector: "@e3"
|
|
341
|
+
value: "myusername"
|
|
342
|
+
|
|
343
|
+
# 4. 填充密码
|
|
344
|
+
tool: tool://agent-browser
|
|
345
|
+
mode: execute
|
|
346
|
+
parameters:
|
|
347
|
+
action: fill
|
|
348
|
+
selector: "@e4"
|
|
349
|
+
value: "mypassword"
|
|
350
|
+
|
|
351
|
+
# 5. 点击登录按钮
|
|
352
|
+
tool: tool://agent-browser
|
|
353
|
+
mode: execute
|
|
354
|
+
parameters:
|
|
355
|
+
action: click
|
|
356
|
+
selector: "@e5"
|
|
357
|
+
|
|
358
|
+
# 6. 等待跳转
|
|
359
|
+
tool: tool://agent-browser
|
|
360
|
+
mode: execute
|
|
361
|
+
parameters:
|
|
362
|
+
action: wait
|
|
363
|
+
state: "visible"
|
|
364
|
+
selector: "#dashboard"
|
|
365
|
+
```
|
|
366
|
+
|
|
367
|
+
### 示例 2:数据抓取
|
|
368
|
+
|
|
369
|
+
```yaml
|
|
370
|
+
# 导航到目标页面
|
|
371
|
+
tool: tool://agent-browser
|
|
372
|
+
mode: execute
|
|
373
|
+
parameters:
|
|
374
|
+
action: navigate
|
|
375
|
+
url: "https://example.com/products"
|
|
376
|
+
|
|
377
|
+
# 获取快照
|
|
378
|
+
tool: tool://agent-browser
|
|
379
|
+
mode: execute
|
|
380
|
+
parameters:
|
|
381
|
+
action: snapshot
|
|
382
|
+
|
|
383
|
+
# 获取产品列表
|
|
384
|
+
tool: tool://agent-browser
|
|
385
|
+
mode: execute
|
|
386
|
+
parameters:
|
|
387
|
+
action: count
|
|
388
|
+
selector: ".product-item"
|
|
389
|
+
|
|
390
|
+
# 获取第一个产品信息
|
|
391
|
+
tool: tool://agent-browser
|
|
392
|
+
mode: execute
|
|
393
|
+
parameters:
|
|
394
|
+
action: gettext
|
|
395
|
+
selector: ".product-item:first-child .title"
|
|
396
|
+
|
|
397
|
+
# 截图保存
|
|
398
|
+
tool: tool://agent-browser
|
|
399
|
+
mode: execute
|
|
400
|
+
parameters:
|
|
401
|
+
action: screenshot
|
|
402
|
+
screenshotPath: "/products.png"
|
|
403
|
+
fullPage: false
|
|
404
|
+
```
|
|
405
|
+
|
|
406
|
+
### 示例 3:使用语义定位器
|
|
407
|
+
|
|
408
|
+
```yaml
|
|
409
|
+
# 点击提交按钮(通过角色)
|
|
410
|
+
tool: tool://agent-browser
|
|
411
|
+
mode: execute
|
|
412
|
+
parameters:
|
|
413
|
+
action: getbyrole
|
|
414
|
+
role: "button"
|
|
415
|
+
name: "Submit"
|
|
416
|
+
subaction: "click"
|
|
417
|
+
|
|
418
|
+
# 填充邮箱输入框(通过标签)
|
|
419
|
+
tool: tool://agent-browser
|
|
420
|
+
mode: execute
|
|
421
|
+
parameters:
|
|
422
|
+
action: getbylabel
|
|
423
|
+
label: "Email"
|
|
424
|
+
subaction: "fill"
|
|
425
|
+
value: "user@example.com"
|
|
426
|
+
|
|
427
|
+
# 点击登录链接(通过文本)
|
|
428
|
+
tool: tool://agent-browser
|
|
429
|
+
mode: execute
|
|
430
|
+
parameters:
|
|
431
|
+
action: getbytext
|
|
432
|
+
text: "Sign In"
|
|
433
|
+
exact: true
|
|
434
|
+
subaction: "click"
|
|
435
|
+
```
|
|
436
|
+
|
|
437
|
+
### 示例 4:多标签页管理
|
|
438
|
+
|
|
439
|
+
```yaml
|
|
440
|
+
# 打开第一个网站
|
|
441
|
+
tool: tool://agent-browser
|
|
442
|
+
mode: execute
|
|
443
|
+
parameters:
|
|
444
|
+
action: navigate
|
|
445
|
+
url: "https://site-a.com"
|
|
446
|
+
|
|
447
|
+
# 新建标签页打开第二个网站
|
|
448
|
+
tool: tool://agent-browser
|
|
449
|
+
mode: execute
|
|
450
|
+
parameters:
|
|
451
|
+
action: tab_new
|
|
452
|
+
|
|
453
|
+
# 导航到第二个网站
|
|
454
|
+
tool: tool://agent-browser
|
|
455
|
+
mode: execute
|
|
456
|
+
parameters:
|
|
457
|
+
action: navigate
|
|
458
|
+
url: "https://site-b.com"
|
|
459
|
+
|
|
460
|
+
# 切换回第一个标签页
|
|
461
|
+
tool: tool://agent-browser
|
|
462
|
+
mode: execute
|
|
463
|
+
parameters:
|
|
464
|
+
action: tab_switch
|
|
465
|
+
index: 0
|
|
466
|
+
|
|
467
|
+
# 关闭第二个标签页
|
|
468
|
+
tool: tool://agent-browser
|
|
469
|
+
mode: execute
|
|
470
|
+
parameters:
|
|
471
|
+
action: tab_close
|
|
472
|
+
index: 1
|
|
473
|
+
```
|
|
474
|
+
|
|
475
|
+
## 环境变量
|
|
476
|
+
|
|
477
|
+
| 环境变量 | 描述 | 默认值 |
|
|
478
|
+
|----------|------|---------|
|
|
479
|
+
| `AGENT_BROWSER_EXECUTABLE_PATH` | 浏览器可执行文件路径 | - |
|
|
480
|
+
| `AGENT_BROWSER_HEADLESS` | 是否使用无头模式 | `true` |
|
|
481
|
+
|
|
482
|
+
## 配置选项
|
|
483
|
+
|
|
484
|
+
### 启动选项
|
|
485
|
+
|
|
486
|
+
```yaml
|
|
487
|
+
options:
|
|
488
|
+
headless: false # 显示浏览器窗口
|
|
489
|
+
viewport: # 视口大小
|
|
490
|
+
width: 1920
|
|
491
|
+
height: 1080
|
|
492
|
+
device: "iPhone 14" # 使用预定义设备
|
|
493
|
+
executablePath: "/path/to/chrome" # 自定义浏览器路径
|
|
494
|
+
```
|
|
495
|
+
|
|
496
|
+
### 快照选项
|
|
497
|
+
|
|
498
|
+
```yaml
|
|
499
|
+
interactive: true # 仅包含交互元素
|
|
500
|
+
maxDepth: 3 # 限制深度
|
|
501
|
+
compact: true # 紧凑模式
|
|
502
|
+
selector: "#main" # 限制范围
|
|
503
|
+
```
|
|
504
|
+
|
|
505
|
+
### 截图选项
|
|
506
|
+
|
|
507
|
+
```yaml
|
|
508
|
+
screenshotPath: "/path/to/file.png"
|
|
509
|
+
fullPage: true # 整页截图
|
|
510
|
+
screenshotFormat: "jpeg" # png 或 jpeg
|
|
511
|
+
quality: 90 # JPEG 质量(0-100)
|
|
512
|
+
```
|
|
513
|
+
|
|
514
|
+
## 错误处理
|
|
515
|
+
|
|
516
|
+
工具会返回标准化的错误响应:
|
|
517
|
+
|
|
518
|
+
```yaml
|
|
519
|
+
success: false
|
|
520
|
+
error:
|
|
521
|
+
message: "错误描述"
|
|
522
|
+
code: "ERROR_CODE" # 如 BROWSER_NOT_LAUNCHED, ELEMENT_NOT_FOUND
|
|
523
|
+
solution: "解决方案建议"
|
|
524
|
+
retryable: true/false
|
|
525
|
+
```
|
|
526
|
+
|
|
527
|
+
### 常见错误码
|
|
528
|
+
|
|
529
|
+
| 错误码 | 描述 | 解决方案 |
|
|
530
|
+
|---------|------|---------|
|
|
531
|
+
| `BROWSER_NOT_LAUNCHED` | 浏览器未启动 | 先调用 `launch` 操作 |
|
|
532
|
+
| `ELEMENT_NOT_FOUND` | 元素未找到 | 使用 `snapshot` 获取有效的元素引用 |
|
|
533
|
+
| `NAVIGATION_FAILED` | 导航失败 | 检查 URL 是否有效 |
|
|
534
|
+
| `BROWSER_LAUNCH_FAILED` | 浏览器启动失败 | 检查浏览器安装或使用 `executablePath` |
|
|
535
|
+
| `TIMEOUT` | 操作超时 | 增加 `timeout` 参数或检查网络连接 |
|
|
536
|
+
| `FRAME_NOT_FOUND` | 框架未找到 | 检查框架选择器、名称或 URL |
|
|
537
|
+
|
|
538
|
+
## 高级技巧
|
|
539
|
+
|
|
540
|
+
### 1. 调试模式
|
|
541
|
+
|
|
542
|
+
使用 `headed: false` 显示浏览器窗口进行调试:
|
|
543
|
+
|
|
544
|
+
```yaml
|
|
545
|
+
parameters:
|
|
546
|
+
action: launch
|
|
547
|
+
options:
|
|
548
|
+
headless: false
|
|
549
|
+
```
|
|
550
|
+
|
|
551
|
+
### 2. 性能优化
|
|
552
|
+
|
|
553
|
+
使用 `interactive: true` 仅获取交互元素,减少上下文使用:
|
|
554
|
+
|
|
555
|
+
```yaml
|
|
556
|
+
parameters:
|
|
557
|
+
action: snapshot
|
|
558
|
+
interactive: true
|
|
559
|
+
maxDepth: 3
|
|
560
|
+
```
|
|
561
|
+
|
|
562
|
+
### 3. Cookie 持久化
|
|
563
|
+
|
|
564
|
+
保存和加载认证状态:
|
|
565
|
+
|
|
566
|
+
```yaml
|
|
567
|
+
# 保存状态
|
|
568
|
+
tool: tool://agent-browser
|
|
569
|
+
mode: execute
|
|
570
|
+
parameters:
|
|
571
|
+
action: storage_state_save
|
|
572
|
+
statePath: "/path/to/auth-state.json"
|
|
573
|
+
|
|
574
|
+
# 加载状态
|
|
575
|
+
tool: tool://agent-browser
|
|
576
|
+
mode: execute
|
|
577
|
+
parameters:
|
|
578
|
+
action: storage_state_load
|
|
579
|
+
statePath: "/path/to/auth-state.json"
|
|
580
|
+
```
|
|
581
|
+
|
|
582
|
+
### 4. 设备模拟
|
|
583
|
+
|
|
584
|
+
快速模拟移动设备:
|
|
585
|
+
|
|
586
|
+
```yaml
|
|
587
|
+
parameters:
|
|
588
|
+
action: device
|
|
589
|
+
device: "iPhone 14" # 或 "Pixel 5", "iPad Pro"
|
|
590
|
+
```
|
|
591
|
+
|
|
592
|
+
### 5. 网络请求拦截
|
|
593
|
+
|
|
594
|
+
模拟 API 响应:
|
|
595
|
+
|
|
596
|
+
```yaml
|
|
597
|
+
# 拦截并返回模拟数据
|
|
598
|
+
tool: tool://agent-browser
|
|
599
|
+
mode: execute
|
|
600
|
+
parameters:
|
|
601
|
+
action: route
|
|
602
|
+
routeUrl: "**/api/**"
|
|
603
|
+
responseBody:
|
|
604
|
+
status: 200
|
|
605
|
+
contentType: "application/json"
|
|
606
|
+
body: '{"success": true, "data": [...] }'
|
|
607
|
+
```
|
|
608
|
+
|
|
609
|
+
## 与现有工具的对比
|
|
610
|
+
|
|
611
|
+
| 特性 | agent-browser | playwright | chrome-devtools |
|
|
612
|
+
|--------|---------------|------------|----------------|
|
|
613
|
+
| **AI 优化** | ✅ Ref 系统 | ❌ 完整 DOM | ❌ 完整 DOM |
|
|
614
|
+
| **上下文效率** | ✅ 93% 节省 | ❌ 大量上下文 | ❌ 大量上下文 |
|
|
615
|
+
| **浏览器支持** | Chromium, Firefox, WebKit | 3 种 | 仅 Chromium |
|
|
616
|
+
| **网络监控** | ✅ 完整 | ❌ 基础 | ✅ 高级 |
|
|
617
|
+
| **性能分析** | ✅ 追踪/HAR | ❌ | ✅ 完整 |
|
|
618
|
+
| **实现方式** | 直接 API(高效) | Playwright 包装 | chrome-devtools-mcp 适配器 |
|
|
619
|
+
|
|
620
|
+
## 参考资源
|
|
621
|
+
|
|
622
|
+
- [agent-browser 官方文档](https://agent-browser.dev)
|
|
623
|
+
- [GitHub 仓库](https://github.com/vercel-labs/agent-browser)
|
|
624
|
+
- [npm 包](https://www.npmjs.com/package/agent-browser)
|
|
625
|
+
- [API 文档](https://unpkg.com/browse/agent-browser@0.5.0/dist/browser.d.ts)
|
|
626
|
+
|
|
627
|
+
## 注意事项
|
|
628
|
+
|
|
629
|
+
1. **浏览器安装**:首次使用时可能需要下载 Chromium 浏览器
|
|
630
|
+
2. **Headless vs Headed**:生产环境建议使用 `headless: true`,调试时可设为 `false`
|
|
631
|
+
3. **Ref 有效性**:Ref 仅在获取快照后有效,页面变化后需要重新获取快照
|
|
632
|
+
4. **内存管理**:长时间运行建议定期关闭浏览器或使用会话隔离
|
|
633
|
+
5. **超时设置**:对于慢速网站,适当增加 `timeout` 参数
|
|
634
|
+
|
|
635
|
+
## 版本信息
|
|
636
|
+
|
|
637
|
+
- **工具版本**: 1.0.0
|
|
638
|
+
- **agent-browser 版本**: ^0.5.0
|
|
639
|
+
- **基础库**: Playwright 1.57.0
|
|
640
|
+
- **最后更新**: 2026-01-16
|