@amaster.ai/pi-browser-use 0.1.0
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/LICENSE +201 -0
- package/README.md +156 -0
- package/dist/analyze-screenshot.d.ts +33 -0
- package/dist/analyze-screenshot.d.ts.map +1 -0
- package/dist/analyze-screenshot.js +140 -0
- package/dist/analyze-screenshot.js.map +1 -0
- package/dist/cli.d.ts +11 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +133 -0
- package/dist/cli.js.map +1 -0
- package/dist/config.d.ts +37 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +86 -0
- package/dist/config.js.map +1 -0
- package/dist/index.d.ts +39 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +239 -0
- package/dist/index.js.map +1 -0
- package/dist/tool-augment.d.ts +14 -0
- package/dist/tool-augment.d.ts.map +1 -0
- package/dist/tool-augment.js +68 -0
- package/dist/tool-augment.js.map +1 -0
- package/package.json +71 -0
- package/preview.png +0 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
# @amaster.ai/pi-browser-use
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+
|
|
5
|
+
pi-coding-agent extension that wraps [chrome-devtools-mcp](https://github.com/ChromeDevTools/chrome-devtools-mcp), exposing all browser automation tools with a unified `browser_` prefix.
|
|
6
|
+
|
|
7
|
+
## Features
|
|
8
|
+
|
|
9
|
+
- **pi-coding-agent extension** — registers tools via `pi.registerTool()`, managed by the agent lifecycle
|
|
10
|
+
- **Dynamic tool discovery** — automatically proxies all upstream chrome-devtools-mcp tools with `browser_` prefix
|
|
11
|
+
- **Tool description augmentation** — adds usage hints for key tools (click, fill, press_key, etc.)
|
|
12
|
+
- **Result post-processing** — strips embedded snapshots, detects overlay/stale element issues
|
|
13
|
+
- **Optional visual analysis** — `browser_analyze_screenshot` via configurable vision model
|
|
14
|
+
- **Standalone mode** — also runnable as an independent MCP server via CLI
|
|
15
|
+
|
|
16
|
+
## Install
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
bun add @amaster.ai/pi-browser-use
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Requires Node.js >= 20, Chrome (stable or newer), and `@earendil-works/pi-coding-agent >= 0.74.0`.
|
|
23
|
+
|
|
24
|
+
## Usage
|
|
25
|
+
|
|
26
|
+
### As pi-coding-agent Extension (Recommended)
|
|
27
|
+
|
|
28
|
+
Install the package and pi-coding-agent will automatically discover and load the extension. All browser tools are registered on `session_start`.
|
|
29
|
+
|
|
30
|
+
```bash
|
|
31
|
+
bun add @amaster.ai/pi-browser-use
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Configure via `.pi/settings.json` (project-level) or `~/.pi/agent/settings.json` (user-level) under the `"pi-browser-use"` key:
|
|
35
|
+
|
|
36
|
+
```json
|
|
37
|
+
{
|
|
38
|
+
"pi-browser-use": {
|
|
39
|
+
"headless": true,
|
|
40
|
+
"channel": "stable",
|
|
41
|
+
"viewport": "1280x720",
|
|
42
|
+
"experimentalVision": true
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### As Standalone MCP Server (CLI)
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
npx @amaster.ai/pi-browser-use --headless --viewport=1280x720
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
Or with a config file:
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
npx @amaster.ai/pi-browser-use --config path/to/config.json
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## Configuration
|
|
60
|
+
|
|
61
|
+
### Session Mode
|
|
62
|
+
|
|
63
|
+
| Mode | Description |
|
|
64
|
+
|------|-------------|
|
|
65
|
+
| `persistent` (default) | Reuses a shared browser profile at `~/.pi/browser-profile`. Cookies, logins, and extensions persist across sessions. |
|
|
66
|
+
| `isolated` | Launches a fresh ephemeral browser profile each session. No state carried over. |
|
|
67
|
+
| `existing` | Connects to an already-running browser instance (via `browserUrl`, `wsEndpoint`, or auto-discovery). |
|
|
68
|
+
|
|
69
|
+
```json
|
|
70
|
+
{
|
|
71
|
+
"pi-browser-use": {
|
|
72
|
+
"sessionMode": "persistent"
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### Browser
|
|
78
|
+
|
|
79
|
+
| Option | Type | Default | Description |
|
|
80
|
+
|--------|------|---------|-------------|
|
|
81
|
+
| `headless` | `boolean` | `false` | Run browser in headless mode |
|
|
82
|
+
| `channel` | `string` | — | Chrome channel: `canary`, `dev`, `beta`, `stable` |
|
|
83
|
+
| `browserUrl` | `string` | — | Connect to existing browser via URL |
|
|
84
|
+
| `wsEndpoint` | `string` | — | Connect via WebSocket endpoint |
|
|
85
|
+
| `executablePath` | `string` | — | Path to Chrome executable |
|
|
86
|
+
| `viewport` | `string` | — | Viewport size, e.g. `1280x720` |
|
|
87
|
+
| `isolated` | `boolean` | `false` | Use isolated browser profile |
|
|
88
|
+
| `userDataDir` | `string` | — | Custom user data directory |
|
|
89
|
+
| `autoConnect` | `boolean` | `false` | Auto-connect to running browser |
|
|
90
|
+
|
|
91
|
+
### Categories
|
|
92
|
+
|
|
93
|
+
| Option | Type | Default | Description |
|
|
94
|
+
|--------|------|---------|-------------|
|
|
95
|
+
| `categoryPerformance` | `boolean` | `false` | Enable performance tools |
|
|
96
|
+
| `categoryNetwork` | `boolean` | `true` | Enable network tools |
|
|
97
|
+
| `categoryEmulation` | `boolean` | `true` | Enable emulation tools |
|
|
98
|
+
| `categoryExtensions` | `boolean` | `false` | Enable extension tools |
|
|
99
|
+
|
|
100
|
+
### Experimental
|
|
101
|
+
|
|
102
|
+
| Option | Type | Default | Description |
|
|
103
|
+
|--------|------|---------|-------------|
|
|
104
|
+
| `experimentalVision` | `boolean` | `true` | Enable vision tools (click_at) |
|
|
105
|
+
| `experimentalScreencast` | `boolean` | `false` | Enable screencast |
|
|
106
|
+
| `experimentalMemory` | `boolean` | `false` | Enable memory snapshots |
|
|
107
|
+
|
|
108
|
+
### Privacy
|
|
109
|
+
|
|
110
|
+
| Option | Type | Default | Description |
|
|
111
|
+
|--------|------|---------|-------------|
|
|
112
|
+
| `usageStatistics` | `boolean` | `false` | Send usage statistics |
|
|
113
|
+
| `performanceCrux` | `boolean` | `false` | Enable CrUX performance data |
|
|
114
|
+
|
|
115
|
+
### Vision Model (Optional)
|
|
116
|
+
|
|
117
|
+
Enable `browser_analyze_screenshot` by referencing a model already configured in Pi's model registry (`models.json`):
|
|
118
|
+
|
|
119
|
+
```json
|
|
120
|
+
{
|
|
121
|
+
"pi-browser-use": {
|
|
122
|
+
"visionModel": {
|
|
123
|
+
"provider": "openai",
|
|
124
|
+
"model": "gpt-4o"
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
The extension resolves API key, base URL, and headers from the model registry automatically — no need to duplicate credentials here.
|
|
131
|
+
|
|
132
|
+
## Tool Augmentation
|
|
133
|
+
|
|
134
|
+
Key tools receive additional usage hints in their descriptions:
|
|
135
|
+
|
|
136
|
+
| Tool | Hint |
|
|
137
|
+
|------|------|
|
|
138
|
+
| `browser_click` | Use element uid from snapshot; UIDs invalidated after action |
|
|
139
|
+
| `browser_fill` | Does not work on canvas/custom widgets |
|
|
140
|
+
| `browser_press_key` | Accepts single key name only |
|
|
141
|
+
| `browser_take_snapshot` | Call first to get uids, and after every state-changing action |
|
|
142
|
+
| `browser_navigate_page` | Call take_snapshot after navigation |
|
|
143
|
+
|
|
144
|
+
## Result Post-Processing
|
|
145
|
+
|
|
146
|
+
- **Snapshot stripping** — Removes embedded accessibility tree snapshots from non-snapshot tool responses to prevent token bloat
|
|
147
|
+
- **Overlay detection** — When a click is blocked by an overlay/popup, appends a hint to dismiss it first
|
|
148
|
+
- **Stale element detection** — When element references are stale, appends a hint to refresh the snapshot
|
|
149
|
+
|
|
150
|
+
## Excluded Tools
|
|
151
|
+
|
|
152
|
+
- `lighthouse_audit` — Filtered out at the proxy level
|
|
153
|
+
|
|
154
|
+
## License
|
|
155
|
+
|
|
156
|
+
Apache-2.0
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Optional vision-model integration.
|
|
3
|
+
*
|
|
4
|
+
* Takes a screenshot via the upstream take_screenshot tool, sends it to a
|
|
5
|
+
* vision model, and returns the analysis as text. This lets the LLM identify
|
|
6
|
+
* elements by visual attributes (color, layout, coordinates) when the
|
|
7
|
+
* accessibility tree is insufficient.
|
|
8
|
+
*
|
|
9
|
+
* Both extension mode and CLI mode resolve credentials from Pi's model registry.
|
|
10
|
+
*/
|
|
11
|
+
import type { VisionModelConfig } from './config.js';
|
|
12
|
+
import type { DevToolsClient } from './index.js';
|
|
13
|
+
/** System prompt for the vision analysis model. */
|
|
14
|
+
export declare const VISUAL_SYSTEM_PROMPT = "You are a Visual Analysis Agent. You receive a screenshot of a browser page and an instruction.\n\nYour job is to ANALYZE the screenshot and provide precise information that a browser automation agent can act on.\n\nCOORDINATE SYSTEM:\n- Coordinates are pixel-based relative to the viewport\n- (0,0) is top-left of the visible area\n- Estimate element positions from the screenshot\n\nRESPONSE FORMAT:\n- For coordinate identification: provide exact (x, y) pixel coordinates\n- For element identification: describe the element's visual location and appearance\n- For layout analysis: describe the spatial relationships between elements\n- Be concise and actionable\n\nIMPORTANT:\n- You are NOT performing actions \u2014 you are only providing visual analysis\n- Include coordinates when possible so the caller can use click_at(x, y)\n- If the element is not visible in the screenshot, say so explicitly";
|
|
15
|
+
/**
|
|
16
|
+
* Abstraction for calling a vision model.
|
|
17
|
+
* Extension mode and CLI mode each provide their own implementation.
|
|
18
|
+
*/
|
|
19
|
+
export type VisionCaller = (instruction: string, imageBase64: string, mimeType: string) => Promise<string>;
|
|
20
|
+
/** Create a VisionCaller that resolves credentials from Pi's model registry. Used in CLI standalone mode. */
|
|
21
|
+
export declare function createFetchVisionCaller(visionConfig: VisionModelConfig): VisionCaller;
|
|
22
|
+
/**
|
|
23
|
+
* Capture a screenshot via the upstream tool and analyze it with the provided vision caller.
|
|
24
|
+
* The caller abstraction allows extension mode (pi-ai) and CLI mode (raw fetch) to share logic.
|
|
25
|
+
*/
|
|
26
|
+
export declare function handleAnalyzeScreenshot(client: DevToolsClient, callVision: VisionCaller, args: Record<string, unknown>): Promise<{
|
|
27
|
+
content: Array<{
|
|
28
|
+
type: string;
|
|
29
|
+
text: string;
|
|
30
|
+
}>;
|
|
31
|
+
isError?: boolean;
|
|
32
|
+
}>;
|
|
33
|
+
//# sourceMappingURL=analyze-screenshot.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"analyze-screenshot.d.ts","sourceRoot":"","sources":["../src/analyze-screenshot.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAIH,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AACrD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAEjD,mDAAmD;AACnD,eAAO,MAAM,oBAAoB,24BAkBoC,CAAC;AAEtE;;;GAGG;AACH,MAAM,MAAM,YAAY,GAAG,CACzB,WAAW,EAAE,MAAM,EACnB,WAAW,EAAE,MAAM,EACnB,QAAQ,EAAE,MAAM,KACb,OAAO,CAAC,MAAM,CAAC,CAAC;AAErB,6GAA6G;AAC7G,wBAAgB,uBAAuB,CAAC,YAAY,EAAE,iBAAiB,GAAG,YAAY,CAkDrF;AAMD;;;GAGG;AACH,wBAAsB,uBAAuB,CAC3C,MAAM,EAAE,cAAc,EACtB,UAAU,EAAE,YAAY,EACxB,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAC5B,OAAO,CAAC;IAAE,OAAO,EAAE,KAAK,CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;IAAC,OAAO,CAAC,EAAE,OAAO,CAAA;CAAE,CAAC,CAiEhF"}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Optional vision-model integration.
|
|
3
|
+
*
|
|
4
|
+
* Takes a screenshot via the upstream take_screenshot tool, sends it to a
|
|
5
|
+
* vision model, and returns the analysis as text. This lets the LLM identify
|
|
6
|
+
* elements by visual attributes (color, layout, coordinates) when the
|
|
7
|
+
* accessibility tree is insufficient.
|
|
8
|
+
*
|
|
9
|
+
* Both extension mode and CLI mode resolve credentials from Pi's model registry.
|
|
10
|
+
*/
|
|
11
|
+
import { complete } from '@earendil-works/pi-ai';
|
|
12
|
+
import { AuthStorage, ModelRegistry } from '@earendil-works/pi-coding-agent';
|
|
13
|
+
/** System prompt for the vision analysis model. */
|
|
14
|
+
export const VISUAL_SYSTEM_PROMPT = `You are a Visual Analysis Agent. You receive a screenshot of a browser page and an instruction.
|
|
15
|
+
|
|
16
|
+
Your job is to ANALYZE the screenshot and provide precise information that a browser automation agent can act on.
|
|
17
|
+
|
|
18
|
+
COORDINATE SYSTEM:
|
|
19
|
+
- Coordinates are pixel-based relative to the viewport
|
|
20
|
+
- (0,0) is top-left of the visible area
|
|
21
|
+
- Estimate element positions from the screenshot
|
|
22
|
+
|
|
23
|
+
RESPONSE FORMAT:
|
|
24
|
+
- For coordinate identification: provide exact (x, y) pixel coordinates
|
|
25
|
+
- For element identification: describe the element's visual location and appearance
|
|
26
|
+
- For layout analysis: describe the spatial relationships between elements
|
|
27
|
+
- Be concise and actionable
|
|
28
|
+
|
|
29
|
+
IMPORTANT:
|
|
30
|
+
- You are NOT performing actions — you are only providing visual analysis
|
|
31
|
+
- Include coordinates when possible so the caller can use click_at(x, y)
|
|
32
|
+
- If the element is not visible in the screenshot, say so explicitly`;
|
|
33
|
+
/** Create a VisionCaller that resolves credentials from Pi's model registry. Used in CLI standalone mode. */
|
|
34
|
+
export function createFetchVisionCaller(visionConfig) {
|
|
35
|
+
const authStorage = AuthStorage.create();
|
|
36
|
+
const registry = ModelRegistry.create(authStorage);
|
|
37
|
+
return async (instruction, imageBase64, mimeType) => {
|
|
38
|
+
const model = registry.find(visionConfig.provider, visionConfig.model);
|
|
39
|
+
if (!model) {
|
|
40
|
+
throw new Error(`Vision model "${visionConfig.provider}/${visionConfig.model}" not found in model registry.`);
|
|
41
|
+
}
|
|
42
|
+
const auth = await registry.getApiKeyAndHeaders(model);
|
|
43
|
+
if (!auth.ok) {
|
|
44
|
+
throw new Error(`Auth failed for vision model: ${auth.error}`);
|
|
45
|
+
}
|
|
46
|
+
const options = {
|
|
47
|
+
temperature: 0,
|
|
48
|
+
maxTokens: 2048,
|
|
49
|
+
};
|
|
50
|
+
if (auth.apiKey)
|
|
51
|
+
options.apiKey = auth.apiKey;
|
|
52
|
+
if (auth.headers)
|
|
53
|
+
options.headers = auth.headers;
|
|
54
|
+
const result = await complete(model, {
|
|
55
|
+
systemPrompt: VISUAL_SYSTEM_PROMPT,
|
|
56
|
+
messages: [
|
|
57
|
+
{
|
|
58
|
+
role: 'user',
|
|
59
|
+
content: [
|
|
60
|
+
{
|
|
61
|
+
type: 'text',
|
|
62
|
+
text: `Analyze this screenshot and respond to the following instruction:\n\n${instruction}`,
|
|
63
|
+
},
|
|
64
|
+
{ type: 'image', data: imageBase64, mimeType },
|
|
65
|
+
],
|
|
66
|
+
timestamp: Date.now(),
|
|
67
|
+
},
|
|
68
|
+
],
|
|
69
|
+
}, options);
|
|
70
|
+
return result.content
|
|
71
|
+
.filter((c) => c.type === 'text')
|
|
72
|
+
.map((c) => c.text)
|
|
73
|
+
.join('');
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
// ---------------------------------------------------------------------------
|
|
77
|
+
// Main handler (framework-agnostic)
|
|
78
|
+
// ---------------------------------------------------------------------------
|
|
79
|
+
/**
|
|
80
|
+
* Capture a screenshot via the upstream tool and analyze it with the provided vision caller.
|
|
81
|
+
* The caller abstraction allows extension mode (pi-ai) and CLI mode (raw fetch) to share logic.
|
|
82
|
+
*/
|
|
83
|
+
export async function handleAnalyzeScreenshot(client, callVision, args) {
|
|
84
|
+
const instruction = String(args.instruction ?? '');
|
|
85
|
+
try {
|
|
86
|
+
const screenshotResult = await client.callTool('take_screenshot', {});
|
|
87
|
+
let imageBase64 = '';
|
|
88
|
+
let mimeType = 'image/png';
|
|
89
|
+
if (screenshotResult.content && Array.isArray(screenshotResult.content)) {
|
|
90
|
+
for (const item of screenshotResult.content) {
|
|
91
|
+
if (item.type === 'image' && item.data) {
|
|
92
|
+
imageBase64 = item.data;
|
|
93
|
+
mimeType = item.mimeType ?? 'image/png';
|
|
94
|
+
break;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
if (!imageBase64) {
|
|
99
|
+
return {
|
|
100
|
+
content: [
|
|
101
|
+
{
|
|
102
|
+
type: 'text',
|
|
103
|
+
text: 'Failed to capture screenshot for visual analysis. Use accessibility tree elements instead.',
|
|
104
|
+
},
|
|
105
|
+
],
|
|
106
|
+
isError: true,
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
const analysis = await callVision(instruction, imageBase64, mimeType);
|
|
110
|
+
if (!analysis) {
|
|
111
|
+
return {
|
|
112
|
+
content: [
|
|
113
|
+
{
|
|
114
|
+
type: 'text',
|
|
115
|
+
text: 'Visual model returned no analysis. Use accessibility tree elements instead.',
|
|
116
|
+
},
|
|
117
|
+
],
|
|
118
|
+
isError: true,
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
return {
|
|
122
|
+
content: [{ type: 'text', text: `Visual Analysis Result:\n${analysis}` }],
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
catch (error) {
|
|
126
|
+
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
127
|
+
const isModelError = errorMsg.includes('404') ||
|
|
128
|
+
errorMsg.includes('403') ||
|
|
129
|
+
errorMsg.includes('not found') ||
|
|
130
|
+
errorMsg.includes('permission');
|
|
131
|
+
const fallbackMsg = isModelError
|
|
132
|
+
? 'Visual analysis model is not available. Use accessibility tree elements (uids from take_snapshot) for all interactions instead.'
|
|
133
|
+
: `Visual analysis failed: ${errorMsg}. Use accessibility tree elements instead.`;
|
|
134
|
+
return {
|
|
135
|
+
content: [{ type: 'text', text: fallbackMsg }],
|
|
136
|
+
isError: true,
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
//# sourceMappingURL=analyze-screenshot.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"analyze-screenshot.js","sourceRoot":"","sources":["../src/analyze-screenshot.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,EAAqC,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AACpF,OAAO,EAAE,WAAW,EAAE,aAAa,EAAE,MAAM,iCAAiC,CAAC;AAI7E,mDAAmD;AACnD,MAAM,CAAC,MAAM,oBAAoB,GAAG;;;;;;;;;;;;;;;;;;qEAkBiC,CAAC;AAYtE,6GAA6G;AAC7G,MAAM,UAAU,uBAAuB,CAAC,YAA+B;IACrE,MAAM,WAAW,GAAG,WAAW,CAAC,MAAM,EAAE,CAAC;IACzC,MAAM,QAAQ,GAAG,aAAa,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC;IAEnD,OAAO,KAAK,EAAE,WAAmB,EAAE,WAAmB,EAAE,QAAgB,EAAmB,EAAE;QAC3F,MAAM,KAAK,GAAG,QAAQ,CAAC,IA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EAAE,MAAM;wBACZ,IAAI,EAAE,6EAA6E;qBACpF;iBACF;gBACD,OAAO,EAAE,IAAI;aACd,CAAC;QACJ,CAAC;QAED,OAAO;YACL,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,4BAA4B,QAAQ,EAAE,EAAE,CAAC;SAC1E,CAAC;IACJ,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,QAAQ,GAAG,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QAExE,MAAM,YAAY,GAChB,QAAQ,CAAC,QAAQ,CAAC,KAAK,CAAC;YACxB,QAAQ,CAAC,QAAQ,CAAC,KAAK,CAAC;YACxB,QAAQ,CAAC,QAAQ,CAAC,WAAW,CAAC;YAC9B,QAAQ,CAAC,QAAQ,CAAC,YAAY,CAAC,CAAC;QAElC,MAAM,WAAW,GAAG,YAAY;YAC9B,CAAC,CAAC,iIAAiI;YACnI,CAAC,CAAC,2BAA2B,QAAQ,4CAA4C,CAAC;QAEpF,OAAO;YACL,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,WAAW,EAAE,CAAC;YAC9C,OAAO,EAAE,IAAI;SACd,CAAC;IACJ,CAAC;AACH,CAAC"}
|
package/dist/cli.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* Standalone MCP server mode.
|
|
4
|
+
*
|
|
5
|
+
* Runs pi-browser-use as a standalone process that speaks MCP over stdio.
|
|
6
|
+
* Used when pi-browser-use is configured as an external MCP server (e.g. in mcp.json)
|
|
7
|
+
* rather than loaded as a pi-coding-agent extension.
|
|
8
|
+
*/
|
|
9
|
+
import type { BrowserUseConfig } from './config.js';
|
|
10
|
+
export declare function parseArgs(argv: string[]): BrowserUseConfig;
|
|
11
|
+
//# sourceMappingURL=cli.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cli.d.ts","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":";AAEA;;;;;;GAMG;AAOH,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAuBpD,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,gBAAgB,CAmC1D"}
|