@mayurajs/browser-agent-browser 0.0.0-stage → 1.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/NOTICE +8 -0
- package/README.md +44 -2
- package/dist/index.d.ts +25 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +221 -0
- package/dist/index.js.map +1 -0
- package/package.json +43 -4
- package/src/index.ts +181 -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/NOTICE
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
Mayura
|
|
2
|
+
Copyright 2026 The Mayura Authors
|
|
3
|
+
|
|
4
|
+
This product includes software developed by the Mayura contributors.
|
|
5
|
+
|
|
6
|
+
Third-party dependencies retain their own copyright and license terms. Exact
|
|
7
|
+
resolved versions are recorded in pnpm-lock.yaml and reviewed by the release
|
|
8
|
+
artifact and dependency qualification procedures.
|
package/README.md
CHANGED
|
@@ -1,3 +1,45 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @mayurajs/browser-agent-browser
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Vercel Labs' [agent-browser](https://github.com/vercel-labs/agent-browser) command line as tools on a `mayura/browser`
|
|
4
|
+
browser: the agent reads pages as agent-browser snapshots with `@e` refs and acts on them, while the browser's origins,
|
|
5
|
+
lifetime and limits still hold. Node only; no dependency of its own.
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install mayura @mayurajs/browser-agent-browser
|
|
9
|
+
npm install -g agent-browser # the command line itself (Apache-2.0)
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
```ts
|
|
13
|
+
import { createBrowsers } from 'mayura/browser';
|
|
14
|
+
import { localBrowsers } from 'mayura/browser/local';
|
|
15
|
+
import { agentBrowserTools } from '@mayurajs/browser-agent-browser';
|
|
16
|
+
|
|
17
|
+
const browsers = createBrowsers(localBrowsers(), { maxBrowsers: 1, maxLifetimeMs: 30 * 60_000, origins: ['https://example.com'] });
|
|
18
|
+
const browser = await browsers.open();
|
|
19
|
+
await browser.goto('https://example.com/');
|
|
20
|
+
const tools = agentBrowserTools(browser, { act: true });
|
|
21
|
+
// agent.read (agent-browser:agent:read) and agent.act (agent-browser:agent:act).
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
| Tool | Commands | Permission | Option |
|
|
25
|
+
| --- | --- | --- | --- |
|
|
26
|
+
| `<name>.read` | `snapshot` (`-i`, `-c`, `-d <n>`, `-s <selector>`), `get text\|html\|value\|attr\|title\|url\|count\|box`, `is visible\|enabled\|checked` | `agent-browser:<name>:read` | always |
|
|
27
|
+
| `<name>.act` | `click`, `dblclick`, `fill`, `type`, `press`, `hover`, `focus`, `check`, `uncheck`, `select`, `scroll`, `scrollintoview`, `wait` | `agent-browser:<name>:act` | `act: true` |
|
|
28
|
+
| `<name>.eval` | `eval` | `agent-browser:<name>:evaluate` | `evaluate: true` |
|
|
29
|
+
|
|
30
|
+
- **Only these commands run**, with only the flags listed; no argument may start with `-` otherwise. Nothing that
|
|
31
|
+
writes files, reaches credentials, cookies or the network setup, or starts other servers is available, and
|
|
32
|
+
navigation stays with the browser's own `goto` (and `browserTools`), which keeps to its origins: agent-browser's
|
|
33
|
+
`read <url>` fetches from this machine directly, outside the browser, and its `open` did not complete over CDP in
|
|
34
|
+
testing (0.38.2 on Windows).
|
|
35
|
+
- The command line runs without a shell, driving the browser over `browser.cdp`, in an agent-browser session of its
|
|
36
|
+
own per browser so refs from a snapshot hold for the next command. The session is closed once its browser has
|
|
37
|
+
ended (on the next call). Results leave out agent-browser's bookkeeping and are bounded (`maxResultBytes`); what
|
|
38
|
+
agent-browser could not do comes back as `{ ok: false, error }`.
|
|
39
|
+
- It works with browsers that are Mayura's alone and whose CDP connection needs no headers (not BrowserView or
|
|
40
|
+
Cloudflare, nor Browserless, whose every connection is a browser of its own, nor a browser shared through
|
|
41
|
+
`cdpBrowsers`).
|
|
42
|
+
- Options: `name` (`agent`), `cli` (`['agent-browser']`), `act`, `evaluate`, `timeoutMs` (60 s), `maxResultBytes`.
|
|
43
|
+
|
|
44
|
+
Verified with agent-browser 0.38.2 against the Chrome installed here. See the
|
|
45
|
+
[browser guide](https://mayurajs.com/docs/guides/browsers/). Apache-2.0.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { type AnyTool } from 'mayura';
|
|
2
|
+
import type { BrowserSource } from 'mayura/browser';
|
|
3
|
+
export interface AgentBrowserToolsOptions {
|
|
4
|
+
/** Names the tools (`<name>.read`, ...) and their permissions (`agent-browser:<name>:read`, ...); `agent` by default. */
|
|
5
|
+
readonly name?: string;
|
|
6
|
+
/** The agent-browser command line, as program and arguments; `['agent-browser']` (on the PATH) by default. */
|
|
7
|
+
readonly cli?: readonly string[];
|
|
8
|
+
/** Make `<name>.act`, which clicks, fills, types and presses on the page; permission `agent-browser:<name>:act`. Off by default. */
|
|
9
|
+
readonly act?: boolean;
|
|
10
|
+
/** Make `<name>.eval`, which runs JavaScript in the page; permission `agent-browser:<name>:evaluate`. Off by default. */
|
|
11
|
+
readonly evaluate?: boolean;
|
|
12
|
+
/** The longest one command may take; 60 s by default. */
|
|
13
|
+
readonly timeoutMs?: number;
|
|
14
|
+
/** The most bytes of a command's result returned to the model; 64 KiB by default. */
|
|
15
|
+
readonly maxResultBytes?: number;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Vercel Labs' agent-browser as tools on a `mayura/browser` browser, the CLI driving it over its CDP endpoint:
|
|
19
|
+
* `<name>.read` (snapshot, get, is) needs `agent-browser:<name>:read`; `<name>.act` (click, fill, type, press, ...)
|
|
20
|
+
* and `<name>.eval` are off until enabled, each with its own permission. Navigation stays with the browser's own
|
|
21
|
+
* `goto`, which keeps to its origins. Each browser gets an agent-browser session of its own, so refs from a snapshot
|
|
22
|
+
* stay valid for the next command. Node only; the agent-browser command line must be installed.
|
|
23
|
+
*/
|
|
24
|
+
export declare function agentBrowserTools(source: BrowserSource, options?: AgentBrowserToolsOptions): AnyTool[];
|
|
25
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAA2B,KAAK,OAAO,EAA4C,MAAM,QAAQ,CAAC;AACzG,OAAO,KAAK,EAAW,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAE7D,MAAM,WAAW,wBAAwB;IACvC,yHAAyH;IACzH,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;IACvB,8GAA8G;IAC9G,QAAQ,CAAC,GAAG,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IACjC,oIAAoI;IACpI,QAAQ,CAAC,GAAG,CAAC,EAAE,OAAO,CAAC;IACvB,yHAAyH;IACzH,QAAQ,CAAC,QAAQ,CAAC,EAAE,OAAO,CAAC;IAC5B,yDAAyD;IACzD,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;IAC5B,qFAAqF;IACrF,QAAQ,CAAC,cAAc,CAAC,EAAE,MAAM,CAAC;CAClC;AAiFD;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,aAAa,EAAE,OAAO,GAAE,wBAA6B,GAAG,OAAO,EAAE,CA2E1G"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import { defineTool, MayuraError } from 'mayura';
|
|
3
|
+
/** Each command a tool runs, with how many arguments it takes, and the flags allowed. */
|
|
4
|
+
const readCommands = {
|
|
5
|
+
snapshot: { min: 0, max: 0, flags: ['-i', '-c', '-d', '-s'] },
|
|
6
|
+
get: { min: 1, max: 3 },
|
|
7
|
+
is: { min: 2, max: 2 },
|
|
8
|
+
};
|
|
9
|
+
const actCommands = {
|
|
10
|
+
click: { min: 1, max: 1 }, dblclick: { min: 1, max: 1 }, fill: { min: 2, max: 2 }, type: { min: 2, max: 2 }, press: { min: 1, max: 1 },
|
|
11
|
+
hover: { min: 1, max: 1 }, focus: { min: 1, max: 1 }, check: { min: 1, max: 1 }, uncheck: { min: 1, max: 1 }, select: { min: 2, max: 6 },
|
|
12
|
+
scroll: { min: 1, max: 2 }, scrollintoview: { min: 1, max: 1 }, wait: { min: 1, max: 1 },
|
|
13
|
+
};
|
|
14
|
+
const getWhat = ['text', 'html', 'value', 'attr', 'title', 'url', 'count', 'box'];
|
|
15
|
+
const isWhat = ['visible', 'enabled', 'checked'];
|
|
16
|
+
const flagsWithValue = new Set(['-d', '-s']);
|
|
17
|
+
/** The arguments for one command, checked; a refusal names what is wrong, for the model to put right. */
|
|
18
|
+
function commandArgs(command, args, allowed) {
|
|
19
|
+
const rule = allowed[command]; // the input schema admits only the listed commands
|
|
20
|
+
if (!Array.isArray(args) || args.length > 10 || args.some(arg => typeof arg !== 'string' || arg.length > 4_096 || arg.includes('\u0000')))
|
|
21
|
+
throw new MayuraError('INVALID_INPUT', 'args are at most 10 strings.');
|
|
22
|
+
const positional = [];
|
|
23
|
+
const flags = [];
|
|
24
|
+
for (let index = 0; index < args.length; index++) {
|
|
25
|
+
const arg = args[index];
|
|
26
|
+
if (arg.startsWith('-')) {
|
|
27
|
+
// Only the flags a command is listed with; anything else could point agent-browser elsewhere.
|
|
28
|
+
if (!rule.flags?.includes(arg))
|
|
29
|
+
throw new MayuraError('INVALID_INPUT', `${arg} is not allowed${rule.flags ? `; ${command} takes ${rule.flags.join(', ')}` : ''}. Arguments may not start with -.`);
|
|
30
|
+
flags.push(arg);
|
|
31
|
+
if (flagsWithValue.has(arg)) {
|
|
32
|
+
const value = args[++index];
|
|
33
|
+
if (value === undefined || value.startsWith('-'))
|
|
34
|
+
throw new MayuraError('INVALID_INPUT', `${arg} needs a value.`);
|
|
35
|
+
flags.push(value);
|
|
36
|
+
}
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
positional.push(arg);
|
|
40
|
+
}
|
|
41
|
+
if (positional.length < rule.min || positional.length > rule.max)
|
|
42
|
+
throw new MayuraError('INVALID_INPUT', `${command} takes ${rule.min === rule.max ? rule.min : `${rule.min} to ${rule.max}`} arguments.`);
|
|
43
|
+
if (command === 'get' && !getWhat.includes(positional[0]))
|
|
44
|
+
throw new MayuraError('INVALID_INPUT', `get takes one of ${getWhat.join(', ')}.`);
|
|
45
|
+
if (command === 'is' && !isWhat.includes(positional[0]))
|
|
46
|
+
throw new MayuraError('INVALID_INPUT', `is takes one of ${isWhat.join(', ')}.`);
|
|
47
|
+
if (command === 'scroll' && !['up', 'down', 'left', 'right'].includes(positional[0]))
|
|
48
|
+
throw new MayuraError('INVALID_INPUT', 'scroll takes up, down, left or right, and optionally pixels.');
|
|
49
|
+
return [...positional, ...flags];
|
|
50
|
+
}
|
|
51
|
+
/** Runs the CLI without a shell, keeping at most `max` bytes of what it prints. */
|
|
52
|
+
function runCli(command, args, timeoutMs, signal, max) {
|
|
53
|
+
return new Promise((resolve, reject) => {
|
|
54
|
+
let child;
|
|
55
|
+
try {
|
|
56
|
+
child = spawn(command[0], [...command.slice(1), ...args], { stdio: ['ignore', 'pipe', 'ignore'], windowsHide: true, shell: false });
|
|
57
|
+
}
|
|
58
|
+
catch {
|
|
59
|
+
reject(new MayuraError('INVALID_CONFIG', 'agent-browser could not be started.'));
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
const chunks = [];
|
|
63
|
+
let kept = 0;
|
|
64
|
+
let settled = false;
|
|
65
|
+
const stop = () => { child.kill(); };
|
|
66
|
+
const timer = setTimeout(stop, timeoutMs);
|
|
67
|
+
if (signal.aborted)
|
|
68
|
+
stop();
|
|
69
|
+
else
|
|
70
|
+
signal.addEventListener('abort', stop, { once: true });
|
|
71
|
+
child.stdout.on('data', (chunk) => { if (kept < max) {
|
|
72
|
+
chunks.push(chunk.subarray(0, max - kept));
|
|
73
|
+
kept += Math.min(chunk.byteLength, max - kept);
|
|
74
|
+
} });
|
|
75
|
+
child.on('error', error => {
|
|
76
|
+
if (settled)
|
|
77
|
+
return;
|
|
78
|
+
settled = true;
|
|
79
|
+
clearTimeout(timer);
|
|
80
|
+
signal.removeEventListener('abort', stop);
|
|
81
|
+
reject(error.code === 'ENOENT' ? new MayuraError('INVALID_CONFIG', `agent-browser was not found at ${command[0]}; install it (npm install -g agent-browser), or give cli.`) : new MayuraError('TOOL_FAILED', 'agent-browser could not be started.'));
|
|
82
|
+
});
|
|
83
|
+
// On exit, not close: agent-browser leaves a daemon holding its output open. What it printed arrives by the end
|
|
84
|
+
// of its output, or soon after it exits.
|
|
85
|
+
child.on('exit', code => {
|
|
86
|
+
if (settled)
|
|
87
|
+
return;
|
|
88
|
+
settled = true;
|
|
89
|
+
clearTimeout(timer);
|
|
90
|
+
signal.removeEventListener('abort', stop);
|
|
91
|
+
const finish = () => {
|
|
92
|
+
if (signal.aborted)
|
|
93
|
+
reject(new MayuraError('CANCELLED', 'The command was cancelled.'));
|
|
94
|
+
else
|
|
95
|
+
resolve({ code, stdout: Buffer.concat(chunks).toString('utf8') });
|
|
96
|
+
};
|
|
97
|
+
if (child.stdout.readableEnded)
|
|
98
|
+
finish();
|
|
99
|
+
else {
|
|
100
|
+
const grace = setTimeout(finish, 500);
|
|
101
|
+
child.stdout.once('end', () => { clearTimeout(grace); finish(); });
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
const anything = { '~standard': { version: 1, vendor: 'mayura-agent-browser', validate: (value) => ({ value }) } };
|
|
107
|
+
const commandInput = (commands) => ({ '~standard': { version: 1, vendor: 'mayura-agent-browser', validate: (value) => {
|
|
108
|
+
const input = value;
|
|
109
|
+
if (!input || typeof input !== 'object' || Array.isArray(input) || Object.keys(input).some(key => key !== 'command' && key !== 'args'))
|
|
110
|
+
return { issues: [{ message: 'Expected { command, args }.' }] };
|
|
111
|
+
if (typeof input.command !== 'string' || !commands.includes(input.command))
|
|
112
|
+
return { issues: [{ message: `command is one of ${commands.join(', ')}.` }] };
|
|
113
|
+
if (input.args !== undefined && (!Array.isArray(input.args) || input.args.some(arg => typeof arg !== 'string')))
|
|
114
|
+
return { issues: [{ message: 'args are strings.' }] };
|
|
115
|
+
return { value: input };
|
|
116
|
+
} } });
|
|
117
|
+
const correctable = new Set(['INVALID_INPUT']);
|
|
118
|
+
/**
|
|
119
|
+
* Vercel Labs' agent-browser as tools on a `mayura/browser` browser, the CLI driving it over its CDP endpoint:
|
|
120
|
+
* `<name>.read` (snapshot, get, is) needs `agent-browser:<name>:read`; `<name>.act` (click, fill, type, press, ...)
|
|
121
|
+
* and `<name>.eval` are off until enabled, each with its own permission. Navigation stays with the browser's own
|
|
122
|
+
* `goto`, which keeps to its origins. Each browser gets an agent-browser session of its own, so refs from a snapshot
|
|
123
|
+
* stay valid for the next command. Node only; the agent-browser command line must be installed.
|
|
124
|
+
*/
|
|
125
|
+
export function agentBrowserTools(source, options = {}) {
|
|
126
|
+
if (typeof source !== 'function' && (!source || typeof source.goto !== 'function'))
|
|
127
|
+
throw new MayuraError('INVALID_CONFIG', 'agentBrowserTools() needs a browser, or a function giving one.');
|
|
128
|
+
const name = options.name ?? 'agent';
|
|
129
|
+
if (typeof name !== 'string' || !/^[a-z][a-z0-9_-]{0,31}$/u.test(name))
|
|
130
|
+
throw new MayuraError('INVALID_CONFIG', 'agentBrowserTools(): name is lowercase letters, digits, _ and -, starting with a letter.');
|
|
131
|
+
const cli = options.cli ?? ['agent-browser'];
|
|
132
|
+
if (!Array.isArray(cli) || cli.length === 0 || cli.some(item => typeof item !== 'string' || item === '' || item.includes('\u0000')))
|
|
133
|
+
throw new MayuraError('INVALID_CONFIG', 'agentBrowserTools(): cli is the command line as program and arguments.');
|
|
134
|
+
for (const flag of ['act', 'evaluate']) {
|
|
135
|
+
if (options[flag] !== undefined && typeof options[flag] !== 'boolean')
|
|
136
|
+
throw new MayuraError('INVALID_CONFIG', `agentBrowserTools(): ${flag} must be a boolean.`);
|
|
137
|
+
}
|
|
138
|
+
const timeoutMs = options.timeoutMs ?? 60_000;
|
|
139
|
+
if (!Number.isSafeInteger(timeoutMs) || timeoutMs < 1_000 || timeoutMs > 600_000)
|
|
140
|
+
throw new MayuraError('INVALID_CONFIG', 'agentBrowserTools(): timeoutMs is 1,000 to 600,000.');
|
|
141
|
+
const maxResultBytes = options.maxResultBytes ?? 65_536;
|
|
142
|
+
if (!Number.isSafeInteger(maxResultBytes) || maxResultBytes < 1_024 || maxResultBytes > 4_194_304)
|
|
143
|
+
throw new MayuraError('INVALID_CONFIG', 'agentBrowserTools(): maxResultBytes is 1,024 to 4,194,304.');
|
|
144
|
+
// A session per browser; closed once its browser has ended.
|
|
145
|
+
const sessions = new Map();
|
|
146
|
+
const closeEnded = () => {
|
|
147
|
+
for (const [browser, session] of sessions) {
|
|
148
|
+
if (!browser.ended)
|
|
149
|
+
continue;
|
|
150
|
+
sessions.delete(browser);
|
|
151
|
+
void runCli(cli, ['--session', session, '--json', 'close'], 30_000, AbortSignal.timeout(30_000), 4_096).catch(() => undefined);
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
const run = async (args, context) => {
|
|
155
|
+
closeEnded();
|
|
156
|
+
const browser = typeof source === 'function' ? await source(context) : source;
|
|
157
|
+
if (!browser || typeof browser.goto !== 'function')
|
|
158
|
+
throw new MayuraError('INVALID_CONFIG', 'The browser source gave no browser.');
|
|
159
|
+
if (browser.ended)
|
|
160
|
+
throw new MayuraError('INVALID_INPUT', 'The browser has ended.');
|
|
161
|
+
// agent-browser needs to reach this very browser; it would act on other pages of a shared one; it sends no headers
|
|
162
|
+
// on its CDP connection.
|
|
163
|
+
const cdp = browser.cdp;
|
|
164
|
+
if (!cdp)
|
|
165
|
+
throw new MayuraError('INVALID_CONFIG', `agent-browser cannot join the ${browser.provider} provider's browsers: each connection starts a browser of its own.`);
|
|
166
|
+
if (cdp.isolated)
|
|
167
|
+
throw new MayuraError('INVALID_CONFIG', 'agent-browser cannot be kept to a browser context of its own: use a browser that is this one\'s alone.');
|
|
168
|
+
if (Object.keys(cdp.headers).length > 0)
|
|
169
|
+
throw new MayuraError('INVALID_CONFIG', `agent-browser cannot connect to the ${browser.provider} provider's browsers, which need headers on their CDP connection.`);
|
|
170
|
+
let session = sessions.get(browser);
|
|
171
|
+
if (!session) {
|
|
172
|
+
session = `mayura-${crypto.randomUUID()}`;
|
|
173
|
+
sessions.set(browser, session);
|
|
174
|
+
}
|
|
175
|
+
// What agent-browser prints is read whole (up to 16 MiB) so its JSON parses; the result is bounded after.
|
|
176
|
+
const ran = await runCli(cli, ['--cdp', cdp.url, '--session', session, '--json', ...args], timeoutMs, context.signal, 16 * 1_048_576);
|
|
177
|
+
let reply;
|
|
178
|
+
try {
|
|
179
|
+
reply = JSON.parse(ran.stdout);
|
|
180
|
+
}
|
|
181
|
+
catch {
|
|
182
|
+
throw new MayuraError(ran.code === null ? 'TIMEOUT' : 'TOOL_FAILED', ran.code === null ? 'agent-browser did not answer in time.' : 'agent-browser did not answer in JSON.');
|
|
183
|
+
}
|
|
184
|
+
// agent-browser's own report of what it did; its bookkeeping (lifecycle) is left out.
|
|
185
|
+
const { lifecycle: _lifecycle, ...data } = (reply.data && typeof reply.data === 'object' && !Array.isArray(reply.data) ? reply.data : { value: reply.data ?? null });
|
|
186
|
+
const result = reply.success === true ? { ok: true, ...data } : { ok: false, error: typeof reply.error === 'string' ? reply.error.slice(0, 1_000) : 'agent-browser did not succeed.' };
|
|
187
|
+
const text = JSON.stringify(result);
|
|
188
|
+
if (new TextEncoder().encode(text).byteLength > maxResultBytes) {
|
|
189
|
+
const snapshot = typeof data['snapshot'] === 'string' ? data['snapshot'] : undefined;
|
|
190
|
+
return snapshot !== undefined ? { ok: true, snapshot: snapshot.slice(0, Math.floor(maxResultBytes / 2)), truncated: true } : { ok: false, error: `The result is larger than ${maxResultBytes} bytes.` };
|
|
191
|
+
}
|
|
192
|
+
return result;
|
|
193
|
+
};
|
|
194
|
+
const tool = (id, permission, effects, description, commands) => defineTool({ id: `${name}.${id}`, version: '1', effects, capabilities: [`agent-browser:${name}:${permission}`], timeoutMs: timeoutMs + 15_000, description,
|
|
195
|
+
input: commandInput(Object.keys(commands)), output: anything,
|
|
196
|
+
inputJsonSchema: { type: 'object', additionalProperties: false, required: ['command'], properties: {
|
|
197
|
+
command: { type: 'string', enum: Object.keys(commands) }, args: { type: 'array', items: { type: 'string' }, maxItems: 10 }
|
|
198
|
+
} },
|
|
199
|
+
execute: async (request, context) => {
|
|
200
|
+
try {
|
|
201
|
+
return await run([request.command, ...commandArgs(request.command, request.args ?? [], commands)], context);
|
|
202
|
+
}
|
|
203
|
+
catch (error) {
|
|
204
|
+
// Mistakes the model can put right come back as the result, so it can try again.
|
|
205
|
+
if (error instanceof MayuraError && correctable.has(error.code))
|
|
206
|
+
return { error: error.code, message: error.message };
|
|
207
|
+
throw error;
|
|
208
|
+
}
|
|
209
|
+
} });
|
|
210
|
+
const tools = [
|
|
211
|
+
tool('read', 'read', 'read', `Read the page open in the ${name} browser with agent-browser: snapshot (an outline whose @e refs the act tool takes; flags -i interactive only, -c compact, -d depth, -s selector), get text|html|value|attr <name>|title|url|count|box [@ref or selector], or is visible|enabled|checked <@ref or selector>.`, readCommands),
|
|
212
|
+
];
|
|
213
|
+
if (options.act) {
|
|
214
|
+
tools.push(tool('act', 'act', 'write', `Act on the page open in the ${name} browser with agent-browser, by @ref from a snapshot or a CSS selector: click, dblclick, fill <ref> <text>, type <ref> <text>, press <key>, hover, focus, check, uncheck, select <ref> <value>, scroll <up|down|left|right> [px], scrollintoview, wait <ref or ms>. Arguments may not start with -. To open a page, use the browser's goto.`, actCommands));
|
|
215
|
+
}
|
|
216
|
+
if (options.evaluate) {
|
|
217
|
+
tools.push(tool('eval', 'evaluate', 'write', `Run a JavaScript expression in the page open in the ${name} browser with agent-browser, and get its value.`, { eval: { min: 1, max: 1 } }));
|
|
218
|
+
}
|
|
219
|
+
return tools;
|
|
220
|
+
}
|
|
221
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAC3C,OAAO,EAAE,UAAU,EAAE,WAAW,EAA0D,MAAM,QAAQ,CAAC;AAkBzG,yFAAyF;AACzF,MAAM,YAAY,GAAiH;IACjI,QAAQ,EAAE,EAAE,GAAG,EAAE,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,CAAC,EAAE;IAC7D,GAAG,EAAE,EAAE,GAAG,EAAE,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE;IACvB,EAAE,EAAE,EAAE,GAAG,EAAE,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE;CACvB,CAAC;AACF,MAAM,WAAW,GAA6E;IAC5F,KAAK,EAAE,EAAE,GAAG,EAAE,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,EAAE,QAAQ,EAAE,EAAE,GAAG,EAAE,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,EAAE,IAAI,EAAE,EAAE,GAAG,EAAE,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,EAAE,IAAI,EAAE,EAAE,GAAG,EAAE,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,EAAE,KAAK,EAAE,EAAE,GAAG,EAAE,CAAC,EAAE,GAAG,EAAE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';\nimport { defineTool, MayuraError, type AnyTool, type JsonObject, type Schema, type Scope } from 'mayura';\nimport type { Browser, BrowserSource } from 'mayura/browser';\n\nexport interface AgentBrowserToolsOptions {\n /** Names the tools (`<name>.read`, ...) and their permissions (`agent-browser:<name>:read`, ...); `agent` by default. */\n readonly name?: string;\n /** The agent-browser command line, as program and arguments; `['agent-browser']` (on the PATH) by default. */\n readonly cli?: readonly string[];\n /** Make `<name>.act`, which clicks, fills, types and presses on the page; permission `agent-browser:<name>:act`. Off by default. */\n readonly act?: boolean;\n /** Make `<name>.eval`, which runs JavaScript in the page; permission `agent-browser:<name>:evaluate`. Off by default. */\n readonly evaluate?: boolean;\n /** The longest one command may take; 60 s by default. */\n readonly timeoutMs?: number;\n /** The most bytes of a command's result returned to the model; 64 KiB by default. */\n readonly maxResultBytes?: number;\n}\n\n/** Each command a tool runs, with how many arguments it takes, and the flags allowed. */\nconst readCommands: Readonly<Record<string, { readonly min: number; readonly max: number; readonly flags?: readonly string[] }>> = {\n snapshot: { min: 0, max: 0, flags: ['-i', '-c', '-d', '-s'] },\n get: { min: 1, max: 3 },\n is: { min: 2, max: 2 },\n};\nconst actCommands: Readonly<Record<string, { readonly min: number; readonly max: number }>> = {\n click: { min: 1, max: 1 }, dblclick: { min: 1, max: 1 }, fill: { min: 2, max: 2 }, type: { min: 2, max: 2 }, press: { min: 1, max: 1 },\n hover: { min: 1, max: 1 }, focus: { min: 1, max: 1 }, check: { min: 1, max: 1 }, uncheck: { min: 1, max: 1 }, select: { min: 2, max: 6 },\n scroll: { min: 1, max: 2 }, scrollintoview: { min: 1, max: 1 }, wait: { min: 1, max: 1 },\n};\nconst getWhat = ['text', 'html', 'value', 'attr', 'title', 'url', 'count', 'box'];\nconst isWhat = ['visible', 'enabled', 'checked'];\nconst flagsWithValue = new Set(['-d', '-s']);\n\n/** The arguments for one command, checked; a refusal names what is wrong, for the model to put right. */\nfunction commandArgs(command: string, args: readonly string[], allowed: Readonly<Record<string, { readonly min: number; readonly max: number; readonly flags?: readonly string[] }>>): string[] {\n const rule = allowed[command]!; // the input schema admits only the listed commands\n if (!Array.isArray(args) || args.length > 10 || args.some(arg => typeof arg !== 'string' || arg.length > 4_096 || arg.includes('\\u0000'))) throw new MayuraError('INVALID_INPUT', 'args are at most 10 strings.');\n const positional: string[] = []; const flags: string[] = [];\n for (let index = 0; index < args.length; index++) {\n const arg = args[index]!;\n if (arg.startsWith('-')) {\n // Only the flags a command is listed with; anything else could point agent-browser elsewhere.\n if (!rule.flags?.includes(arg)) throw new MayuraError('INVALID_INPUT', `${arg} is not allowed${rule.flags ? `; ${command} takes ${rule.flags.join(', ')}` : ''}. Arguments may not start with -.`);\n flags.push(arg);\n if (flagsWithValue.has(arg)) { const value = args[++index]; if (value === undefined || value.startsWith('-')) throw new MayuraError('INVALID_INPUT', `${arg} needs a value.`); flags.push(value); }\n continue;\n }\n positional.push(arg);\n }\n if (positional.length < rule.min || positional.length > rule.max) throw new MayuraError('INVALID_INPUT', `${command} takes ${rule.min === rule.max ? rule.min : `${rule.min} to ${rule.max}`} arguments.`);\n if (command === 'get' && !getWhat.includes(positional[0]!)) throw new MayuraError('INVALID_INPUT', `get takes one of ${getWhat.join(', ')}.`);\n if (command === 'is' && !isWhat.includes(positional[0]!)) throw new MayuraError('INVALID_INPUT', `is takes one of ${isWhat.join(', ')}.`);\n if (command === 'scroll' && !['up', 'down', 'left', 'right'].includes(positional[0]!)) throw new MayuraError('INVALID_INPUT', 'scroll takes up, down, left or right, and optionally pixels.');\n return [...positional, ...flags];\n}\n\ninterface Ran { readonly code: number | null; readonly stdout: string }\n/** Runs the CLI without a shell, keeping at most `max` bytes of what it prints. */\nfunction runCli(command: readonly string[], args: readonly string[], timeoutMs: number, signal: AbortSignal, max: number): Promise<Ran> {\n return new Promise((resolve, reject) => {\n let child;\n try { child = spawn(command[0]!, [...command.slice(1), ...args], { stdio: ['ignore', 'pipe', 'ignore'], windowsHide: true, shell: false }); }\n catch { reject(new MayuraError('INVALID_CONFIG', 'agent-browser could not be started.')); return; }\n const chunks: Buffer[] = []; let kept = 0; let settled = false;\n const stop = () => { child.kill(); };\n const timer = setTimeout(stop, timeoutMs);\n if (signal.aborted) stop(); else signal.addEventListener('abort', stop, { once: true });\n child.stdout.on('data', (chunk: Buffer) => { if (kept < max) { chunks.push(chunk.subarray(0, max - kept)); kept += Math.min(chunk.byteLength, max - kept); } });\n child.on('error', error => {\n if (settled) return; settled = true; clearTimeout(timer); signal.removeEventListener('abort', stop);\n reject((error as NodeJS.ErrnoException).code === 'ENOENT' ? new MayuraError('INVALID_CONFIG', `agent-browser was not found at ${command[0]}; install it (npm install -g agent-browser), or give cli.`) : new MayuraError('TOOL_FAILED', 'agent-browser could not be started.'));\n });\n // On exit, not close: agent-browser leaves a daemon holding its output open. What it printed arrives by the end\n // of its output, or soon after it exits.\n child.on('exit', code => {\n if (settled) return; settled = true; clearTimeout(timer); signal.removeEventListener('abort', stop);\n const finish = () => {\n if (signal.aborted) reject(new MayuraError('CANCELLED', 'The command was cancelled.'));\n else resolve({ code, stdout: Buffer.concat(chunks).toString('utf8') });\n };\n if (child.stdout.readableEnded) finish();\n else { const grace = setTimeout(finish, 500); child.stdout.once('end', () => { clearTimeout(grace); finish(); }); }\n });\n });\n}\n\nconst anything: Schema<unknown> = { '~standard': { version: 1, vendor: 'mayura-agent-browser', validate: (value: unknown) => ({ value }) } } as Schema<unknown>;\nconst commandInput = (commands: readonly string[]): Schema<{ command: string; args?: string[] }> => ({ '~standard': { version: 1, vendor: 'mayura-agent-browser', validate: (value: unknown) => {\n const input = value as { command?: unknown; args?: unknown } | null;\n if (!input || typeof input !== 'object' || Array.isArray(input) || Object.keys(input).some(key => key !== 'command' && key !== 'args')) return { issues: [{ message: 'Expected { command, args }.' }] };\n if (typeof input.command !== 'string' || !commands.includes(input.command)) return { issues: [{ message: `command is one of ${commands.join(', ')}.` }] };\n if (input.args !== undefined && (!Array.isArray(input.args) || input.args.some(arg => typeof arg !== 'string'))) return { issues: [{ message: 'args are strings.' }] };\n return { value: input as { command: string; args?: string[] } };\n} } } as Schema<{ command: string; args?: string[] }>);\nconst correctable = new Set(['INVALID_INPUT']);\n\n\n/**\n * Vercel Labs' agent-browser as tools on a `mayura/browser` browser, the CLI driving it over its CDP endpoint:\n * `<name>.read` (snapshot, get, is) needs `agent-browser:<name>:read`; `<name>.act` (click, fill, type, press, ...)\n * and `<name>.eval` are off until enabled, each with its own permission. Navigation stays with the browser's own\n * `goto`, which keeps to its origins. Each browser gets an agent-browser session of its own, so refs from a snapshot\n * stay valid for the next command. Node only; the agent-browser command line must be installed.\n */\nexport function agentBrowserTools(source: BrowserSource, options: AgentBrowserToolsOptions = {}): AnyTool[] {\n if (typeof source !== 'function' && (!source || typeof source.goto !== 'function')) throw new MayuraError('INVALID_CONFIG', 'agentBrowserTools() needs a browser, or a function giving one.');\n const name = options.name ?? 'agent';\n if (typeof name !== 'string' || !/^[a-z][a-z0-9_-]{0,31}$/u.test(name)) throw new MayuraError('INVALID_CONFIG', 'agentBrowserTools(): name is lowercase letters, digits, _ and -, starting with a letter.');\n const cli = options.cli ?? ['agent-browser'];\n if (!Array.isArray(cli) || cli.length === 0 || cli.some(item => typeof item !== 'string' || item === '' || item.includes('\\u0000'))) throw new MayuraError('INVALID_CONFIG', 'agentBrowserTools(): cli is the command line as program and arguments.');\n for (const flag of ['act', 'evaluate'] as const) {\n if (options[flag] !== undefined && typeof options[flag] !== 'boolean') throw new MayuraError('INVALID_CONFIG', `agentBrowserTools(): ${flag} must be a boolean.`);\n }\n const timeoutMs = options.timeoutMs ?? 60_000;\n if (!Number.isSafeInteger(timeoutMs) || timeoutMs < 1_000 || timeoutMs > 600_000) throw new MayuraError('INVALID_CONFIG', 'agentBrowserTools(): timeoutMs is 1,000 to 600,000.');\n const maxResultBytes = options.maxResultBytes ?? 65_536;\n if (!Number.isSafeInteger(maxResultBytes) || maxResultBytes < 1_024 || maxResultBytes > 4_194_304) throw new MayuraError('INVALID_CONFIG', 'agentBrowserTools(): maxResultBytes is 1,024 to 4,194,304.');\n\n // A session per browser; closed once its browser has ended.\n const sessions = new Map<Browser, string>();\n const closeEnded = () => {\n for (const [browser, session] of sessions) {\n if (!browser.ended) continue;\n sessions.delete(browser);\n void runCli(cli, ['--session', session, '--json', 'close'], 30_000, AbortSignal.timeout(30_000), 4_096).catch(() => undefined);\n }\n };\n const run = async (args: readonly string[], context: { readonly runId: string; readonly scope: Scope; readonly signal: AbortSignal }): Promise<JsonObject> => {\n closeEnded();\n const browser = typeof source === 'function' ? await source(context) : source;\n if (!browser || typeof browser.goto !== 'function') throw new MayuraError('INVALID_CONFIG', 'The browser source gave no browser.');\n if (browser.ended) throw new MayuraError('INVALID_INPUT', 'The browser has ended.');\n // agent-browser needs to reach this very browser; it would act on other pages of a shared one; it sends no headers\n // on its CDP connection.\n const cdp = browser.cdp;\n if (!cdp) throw new MayuraError('INVALID_CONFIG', `agent-browser cannot join the ${browser.provider} provider's browsers: each connection starts a browser of its own.`);\n if (cdp.isolated) throw new MayuraError('INVALID_CONFIG', 'agent-browser cannot be kept to a browser context of its own: use a browser that is this one\\'s alone.');\n if (Object.keys(cdp.headers).length > 0) throw new MayuraError('INVALID_CONFIG', `agent-browser cannot connect to the ${browser.provider} provider's browsers, which need headers on their CDP connection.`);\n let session = sessions.get(browser);\n if (!session) { session = `mayura-${crypto.randomUUID()}`; sessions.set(browser, session); }\n // What agent-browser prints is read whole (up to 16 MiB) so its JSON parses; the result is bounded after.\n const ran = await runCli(cli, ['--cdp', cdp.url, '--session', session, '--json', ...args], timeoutMs, context.signal, 16 * 1_048_576);\n let reply: { success?: unknown; data?: unknown; error?: unknown };\n try { reply = JSON.parse(ran.stdout) as typeof reply; }\n catch { throw new MayuraError(ran.code === null ? 'TIMEOUT' : 'TOOL_FAILED', ran.code === null ? 'agent-browser did not answer in time.' : 'agent-browser did not answer in JSON.'); }\n // agent-browser's own report of what it did; its bookkeeping (lifecycle) is left out.\n const { lifecycle: _lifecycle, ...data } = (reply.data && typeof reply.data === 'object' && !Array.isArray(reply.data) ? reply.data : { value: reply.data ?? null }) as Record<string, unknown>;\n const result = reply.success === true ? { ok: true, ...data } : { ok: false, error: typeof reply.error === 'string' ? reply.error.slice(0, 1_000) : 'agent-browser did not succeed.' };\n const text = JSON.stringify(result);\n if (new TextEncoder().encode(text).byteLength > maxResultBytes) {\n const snapshot = typeof data['snapshot'] === 'string' ? data['snapshot'] : undefined;\n return snapshot !== undefined ? { ok: true, snapshot: snapshot.slice(0, Math.floor(maxResultBytes / 2)), truncated: true } : { ok: false, error: `The result is larger than ${maxResultBytes} bytes.` };\n }\n return result as JsonObject;\n };\n const tool = (id: string, permission: string, effects: 'read' | 'write', description: string, commands: Readonly<Record<string, { min: number; max: number; flags?: readonly string[] }>>) =>\n defineTool({ id: `${name}.${id}`, version: '1', effects, capabilities: [`agent-browser:${name}:${permission}`], timeoutMs: timeoutMs + 15_000, description,\n input: commandInput(Object.keys(commands)), output: anything,\n inputJsonSchema: { type: 'object', additionalProperties: false, required: ['command'], properties: {\n command: { type: 'string', enum: Object.keys(commands) }, args: { type: 'array', items: { type: 'string' }, maxItems: 10 } } } as unknown as JsonObject,\n execute: async (request: { command: string; args?: string[] }, context: { readonly runId: string; readonly scope: Scope; readonly signal: AbortSignal }) => {\n try { return await run([request.command, ...commandArgs(request.command, request.args ?? [], commands)], context); }\n catch (error) {\n // Mistakes the model can put right come back as the result, so it can try again.\n if (error instanceof MayuraError && correctable.has(error.code)) return { error: error.code, message: error.message } as JsonObject;\n throw error;\n }\n } } as never) as unknown as AnyTool;\n\n const tools: AnyTool[] = [\n tool('read', 'read', 'read', `Read the page open in the ${name} browser with agent-browser: snapshot (an outline whose @e refs the act tool takes; flags -i interactive only, -c compact, -d depth, -s selector), get text|html|value|attr <name>|title|url|count|box [@ref or selector], or is visible|enabled|checked <@ref or selector>.`, readCommands),\n ];\n if (options.act) {\n tools.push(tool('act', 'act', 'write', `Act on the page open in the ${name} browser with agent-browser, by @ref from a snapshot or a CSS selector: click, dblclick, fill <ref> <text>, type <ref> <text>, press <key>, hover, focus, check, uncheck, select <ref> <value>, scroll <up|down|left|right> [px], scrollintoview, wait <ref or ms>. Arguments may not start with -. To open a page, use the browser's goto.`, actCommands));\n }\n if (options.evaluate) {\n tools.push(tool('eval', 'evaluate', 'write', `Run a JavaScript expression in the page open in the ${name} browser with agent-browser, and get its value.`, { eval: { min: 1, max: 1 } }));\n }\n return tools;\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,45 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mayurajs/browser-agent-browser",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.1.0",
|
|
4
|
+
"description": "Vercel Labs' agent-browser CLI as tools on a mayura/browser browser.",
|
|
5
|
+
"author": {
|
|
6
|
+
"name": "Aryabh",
|
|
7
|
+
"email": "dev@rokad.co"
|
|
8
|
+
},
|
|
9
|
+
"license": "Apache-2.0",
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/KartikeyAI/mayura.git",
|
|
13
|
+
"directory": "extensions/browser-agent-browser"
|
|
14
|
+
},
|
|
15
|
+
"homepage": "https://github.com/KartikeyAI/mayura#readme",
|
|
16
|
+
"bugs": {
|
|
17
|
+
"url": "https://github.com/KartikeyAI/mayura/issues"
|
|
18
|
+
},
|
|
19
|
+
"type": "module",
|
|
20
|
+
"sideEffects": false,
|
|
21
|
+
"engines": {
|
|
22
|
+
"node": ">=22.12.0 <23 || >=24.14.1 <25"
|
|
23
|
+
},
|
|
24
|
+
"exports": {
|
|
25
|
+
".": {
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
27
|
+
"import": "./dist/index.js"
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"files": [
|
|
31
|
+
"dist",
|
|
32
|
+
"src/**/*.ts",
|
|
33
|
+
"README.md",
|
|
34
|
+
"LICENSE",
|
|
35
|
+
"NOTICE"
|
|
36
|
+
],
|
|
37
|
+
"dependencies": {},
|
|
38
|
+
"peerDependencies": {
|
|
39
|
+
"mayura": "^1.1.0"
|
|
40
|
+
},
|
|
41
|
+
"publishConfig": {
|
|
42
|
+
"access": "public",
|
|
43
|
+
"provenance": true
|
|
44
|
+
}
|
|
45
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import { defineTool, MayuraError, type AnyTool, type JsonObject, type Schema, type Scope } from 'mayura';
|
|
3
|
+
import type { Browser, BrowserSource } from 'mayura/browser';
|
|
4
|
+
|
|
5
|
+
export interface AgentBrowserToolsOptions {
|
|
6
|
+
/** Names the tools (`<name>.read`, ...) and their permissions (`agent-browser:<name>:read`, ...); `agent` by default. */
|
|
7
|
+
readonly name?: string;
|
|
8
|
+
/** The agent-browser command line, as program and arguments; `['agent-browser']` (on the PATH) by default. */
|
|
9
|
+
readonly cli?: readonly string[];
|
|
10
|
+
/** Make `<name>.act`, which clicks, fills, types and presses on the page; permission `agent-browser:<name>:act`. Off by default. */
|
|
11
|
+
readonly act?: boolean;
|
|
12
|
+
/** Make `<name>.eval`, which runs JavaScript in the page; permission `agent-browser:<name>:evaluate`. Off by default. */
|
|
13
|
+
readonly evaluate?: boolean;
|
|
14
|
+
/** The longest one command may take; 60 s by default. */
|
|
15
|
+
readonly timeoutMs?: number;
|
|
16
|
+
/** The most bytes of a command's result returned to the model; 64 KiB by default. */
|
|
17
|
+
readonly maxResultBytes?: number;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** Each command a tool runs, with how many arguments it takes, and the flags allowed. */
|
|
21
|
+
const readCommands: Readonly<Record<string, { readonly min: number; readonly max: number; readonly flags?: readonly string[] }>> = {
|
|
22
|
+
snapshot: { min: 0, max: 0, flags: ['-i', '-c', '-d', '-s'] },
|
|
23
|
+
get: { min: 1, max: 3 },
|
|
24
|
+
is: { min: 2, max: 2 },
|
|
25
|
+
};
|
|
26
|
+
const actCommands: Readonly<Record<string, { readonly min: number; readonly max: number }>> = {
|
|
27
|
+
click: { min: 1, max: 1 }, dblclick: { min: 1, max: 1 }, fill: { min: 2, max: 2 }, type: { min: 2, max: 2 }, press: { min: 1, max: 1 },
|
|
28
|
+
hover: { min: 1, max: 1 }, focus: { min: 1, max: 1 }, check: { min: 1, max: 1 }, uncheck: { min: 1, max: 1 }, select: { min: 2, max: 6 },
|
|
29
|
+
scroll: { min: 1, max: 2 }, scrollintoview: { min: 1, max: 1 }, wait: { min: 1, max: 1 },
|
|
30
|
+
};
|
|
31
|
+
const getWhat = ['text', 'html', 'value', 'attr', 'title', 'url', 'count', 'box'];
|
|
32
|
+
const isWhat = ['visible', 'enabled', 'checked'];
|
|
33
|
+
const flagsWithValue = new Set(['-d', '-s']);
|
|
34
|
+
|
|
35
|
+
/** The arguments for one command, checked; a refusal names what is wrong, for the model to put right. */
|
|
36
|
+
function commandArgs(command: string, args: readonly string[], allowed: Readonly<Record<string, { readonly min: number; readonly max: number; readonly flags?: readonly string[] }>>): string[] {
|
|
37
|
+
const rule = allowed[command]!; // the input schema admits only the listed commands
|
|
38
|
+
if (!Array.isArray(args) || args.length > 10 || args.some(arg => typeof arg !== 'string' || arg.length > 4_096 || arg.includes('\u0000'))) throw new MayuraError('INVALID_INPUT', 'args are at most 10 strings.');
|
|
39
|
+
const positional: string[] = []; const flags: string[] = [];
|
|
40
|
+
for (let index = 0; index < args.length; index++) {
|
|
41
|
+
const arg = args[index]!;
|
|
42
|
+
if (arg.startsWith('-')) {
|
|
43
|
+
// Only the flags a command is listed with; anything else could point agent-browser elsewhere.
|
|
44
|
+
if (!rule.flags?.includes(arg)) throw new MayuraError('INVALID_INPUT', `${arg} is not allowed${rule.flags ? `; ${command} takes ${rule.flags.join(', ')}` : ''}. Arguments may not start with -.`);
|
|
45
|
+
flags.push(arg);
|
|
46
|
+
if (flagsWithValue.has(arg)) { const value = args[++index]; if (value === undefined || value.startsWith('-')) throw new MayuraError('INVALID_INPUT', `${arg} needs a value.`); flags.push(value); }
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
positional.push(arg);
|
|
50
|
+
}
|
|
51
|
+
if (positional.length < rule.min || positional.length > rule.max) throw new MayuraError('INVALID_INPUT', `${command} takes ${rule.min === rule.max ? rule.min : `${rule.min} to ${rule.max}`} arguments.`);
|
|
52
|
+
if (command === 'get' && !getWhat.includes(positional[0]!)) throw new MayuraError('INVALID_INPUT', `get takes one of ${getWhat.join(', ')}.`);
|
|
53
|
+
if (command === 'is' && !isWhat.includes(positional[0]!)) throw new MayuraError('INVALID_INPUT', `is takes one of ${isWhat.join(', ')}.`);
|
|
54
|
+
if (command === 'scroll' && !['up', 'down', 'left', 'right'].includes(positional[0]!)) throw new MayuraError('INVALID_INPUT', 'scroll takes up, down, left or right, and optionally pixels.');
|
|
55
|
+
return [...positional, ...flags];
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
interface Ran { readonly code: number | null; readonly stdout: string }
|
|
59
|
+
/** Runs the CLI without a shell, keeping at most `max` bytes of what it prints. */
|
|
60
|
+
function runCli(command: readonly string[], args: readonly string[], timeoutMs: number, signal: AbortSignal, max: number): Promise<Ran> {
|
|
61
|
+
return new Promise((resolve, reject) => {
|
|
62
|
+
let child;
|
|
63
|
+
try { child = spawn(command[0]!, [...command.slice(1), ...args], { stdio: ['ignore', 'pipe', 'ignore'], windowsHide: true, shell: false }); }
|
|
64
|
+
catch { reject(new MayuraError('INVALID_CONFIG', 'agent-browser could not be started.')); return; }
|
|
65
|
+
const chunks: Buffer[] = []; let kept = 0; let settled = false;
|
|
66
|
+
const stop = () => { child.kill(); };
|
|
67
|
+
const timer = setTimeout(stop, timeoutMs);
|
|
68
|
+
if (signal.aborted) stop(); else signal.addEventListener('abort', stop, { once: true });
|
|
69
|
+
child.stdout.on('data', (chunk: Buffer) => { if (kept < max) { chunks.push(chunk.subarray(0, max - kept)); kept += Math.min(chunk.byteLength, max - kept); } });
|
|
70
|
+
child.on('error', error => {
|
|
71
|
+
if (settled) return; settled = true; clearTimeout(timer); signal.removeEventListener('abort', stop);
|
|
72
|
+
reject((error as NodeJS.ErrnoException).code === 'ENOENT' ? new MayuraError('INVALID_CONFIG', `agent-browser was not found at ${command[0]}; install it (npm install -g agent-browser), or give cli.`) : new MayuraError('TOOL_FAILED', 'agent-browser could not be started.'));
|
|
73
|
+
});
|
|
74
|
+
// On exit, not close: agent-browser leaves a daemon holding its output open. What it printed arrives by the end
|
|
75
|
+
// of its output, or soon after it exits.
|
|
76
|
+
child.on('exit', code => {
|
|
77
|
+
if (settled) return; settled = true; clearTimeout(timer); signal.removeEventListener('abort', stop);
|
|
78
|
+
const finish = () => {
|
|
79
|
+
if (signal.aborted) reject(new MayuraError('CANCELLED', 'The command was cancelled.'));
|
|
80
|
+
else resolve({ code, stdout: Buffer.concat(chunks).toString('utf8') });
|
|
81
|
+
};
|
|
82
|
+
if (child.stdout.readableEnded) finish();
|
|
83
|
+
else { const grace = setTimeout(finish, 500); child.stdout.once('end', () => { clearTimeout(grace); finish(); }); }
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const anything: Schema<unknown> = { '~standard': { version: 1, vendor: 'mayura-agent-browser', validate: (value: unknown) => ({ value }) } } as Schema<unknown>;
|
|
89
|
+
const commandInput = (commands: readonly string[]): Schema<{ command: string; args?: string[] }> => ({ '~standard': { version: 1, vendor: 'mayura-agent-browser', validate: (value: unknown) => {
|
|
90
|
+
const input = value as { command?: unknown; args?: unknown } | null;
|
|
91
|
+
if (!input || typeof input !== 'object' || Array.isArray(input) || Object.keys(input).some(key => key !== 'command' && key !== 'args')) return { issues: [{ message: 'Expected { command, args }.' }] };
|
|
92
|
+
if (typeof input.command !== 'string' || !commands.includes(input.command)) return { issues: [{ message: `command is one of ${commands.join(', ')}.` }] };
|
|
93
|
+
if (input.args !== undefined && (!Array.isArray(input.args) || input.args.some(arg => typeof arg !== 'string'))) return { issues: [{ message: 'args are strings.' }] };
|
|
94
|
+
return { value: input as { command: string; args?: string[] } };
|
|
95
|
+
} } } as Schema<{ command: string; args?: string[] }>);
|
|
96
|
+
const correctable = new Set(['INVALID_INPUT']);
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Vercel Labs' agent-browser as tools on a `mayura/browser` browser, the CLI driving it over its CDP endpoint:
|
|
101
|
+
* `<name>.read` (snapshot, get, is) needs `agent-browser:<name>:read`; `<name>.act` (click, fill, type, press, ...)
|
|
102
|
+
* and `<name>.eval` are off until enabled, each with its own permission. Navigation stays with the browser's own
|
|
103
|
+
* `goto`, which keeps to its origins. Each browser gets an agent-browser session of its own, so refs from a snapshot
|
|
104
|
+
* stay valid for the next command. Node only; the agent-browser command line must be installed.
|
|
105
|
+
*/
|
|
106
|
+
export function agentBrowserTools(source: BrowserSource, options: AgentBrowserToolsOptions = {}): AnyTool[] {
|
|
107
|
+
if (typeof source !== 'function' && (!source || typeof source.goto !== 'function')) throw new MayuraError('INVALID_CONFIG', 'agentBrowserTools() needs a browser, or a function giving one.');
|
|
108
|
+
const name = options.name ?? 'agent';
|
|
109
|
+
if (typeof name !== 'string' || !/^[a-z][a-z0-9_-]{0,31}$/u.test(name)) throw new MayuraError('INVALID_CONFIG', 'agentBrowserTools(): name is lowercase letters, digits, _ and -, starting with a letter.');
|
|
110
|
+
const cli = options.cli ?? ['agent-browser'];
|
|
111
|
+
if (!Array.isArray(cli) || cli.length === 0 || cli.some(item => typeof item !== 'string' || item === '' || item.includes('\u0000'))) throw new MayuraError('INVALID_CONFIG', 'agentBrowserTools(): cli is the command line as program and arguments.');
|
|
112
|
+
for (const flag of ['act', 'evaluate'] as const) {
|
|
113
|
+
if (options[flag] !== undefined && typeof options[flag] !== 'boolean') throw new MayuraError('INVALID_CONFIG', `agentBrowserTools(): ${flag} must be a boolean.`);
|
|
114
|
+
}
|
|
115
|
+
const timeoutMs = options.timeoutMs ?? 60_000;
|
|
116
|
+
if (!Number.isSafeInteger(timeoutMs) || timeoutMs < 1_000 || timeoutMs > 600_000) throw new MayuraError('INVALID_CONFIG', 'agentBrowserTools(): timeoutMs is 1,000 to 600,000.');
|
|
117
|
+
const maxResultBytes = options.maxResultBytes ?? 65_536;
|
|
118
|
+
if (!Number.isSafeInteger(maxResultBytes) || maxResultBytes < 1_024 || maxResultBytes > 4_194_304) throw new MayuraError('INVALID_CONFIG', 'agentBrowserTools(): maxResultBytes is 1,024 to 4,194,304.');
|
|
119
|
+
|
|
120
|
+
// A session per browser; closed once its browser has ended.
|
|
121
|
+
const sessions = new Map<Browser, string>();
|
|
122
|
+
const closeEnded = () => {
|
|
123
|
+
for (const [browser, session] of sessions) {
|
|
124
|
+
if (!browser.ended) continue;
|
|
125
|
+
sessions.delete(browser);
|
|
126
|
+
void runCli(cli, ['--session', session, '--json', 'close'], 30_000, AbortSignal.timeout(30_000), 4_096).catch(() => undefined);
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
const run = async (args: readonly string[], context: { readonly runId: string; readonly scope: Scope; readonly signal: AbortSignal }): Promise<JsonObject> => {
|
|
130
|
+
closeEnded();
|
|
131
|
+
const browser = typeof source === 'function' ? await source(context) : source;
|
|
132
|
+
if (!browser || typeof browser.goto !== 'function') throw new MayuraError('INVALID_CONFIG', 'The browser source gave no browser.');
|
|
133
|
+
if (browser.ended) throw new MayuraError('INVALID_INPUT', 'The browser has ended.');
|
|
134
|
+
// agent-browser needs to reach this very browser; it would act on other pages of a shared one; it sends no headers
|
|
135
|
+
// on its CDP connection.
|
|
136
|
+
const cdp = browser.cdp;
|
|
137
|
+
if (!cdp) throw new MayuraError('INVALID_CONFIG', `agent-browser cannot join the ${browser.provider} provider's browsers: each connection starts a browser of its own.`);
|
|
138
|
+
if (cdp.isolated) throw new MayuraError('INVALID_CONFIG', 'agent-browser cannot be kept to a browser context of its own: use a browser that is this one\'s alone.');
|
|
139
|
+
if (Object.keys(cdp.headers).length > 0) throw new MayuraError('INVALID_CONFIG', `agent-browser cannot connect to the ${browser.provider} provider's browsers, which need headers on their CDP connection.`);
|
|
140
|
+
let session = sessions.get(browser);
|
|
141
|
+
if (!session) { session = `mayura-${crypto.randomUUID()}`; sessions.set(browser, session); }
|
|
142
|
+
// What agent-browser prints is read whole (up to 16 MiB) so its JSON parses; the result is bounded after.
|
|
143
|
+
const ran = await runCli(cli, ['--cdp', cdp.url, '--session', session, '--json', ...args], timeoutMs, context.signal, 16 * 1_048_576);
|
|
144
|
+
let reply: { success?: unknown; data?: unknown; error?: unknown };
|
|
145
|
+
try { reply = JSON.parse(ran.stdout) as typeof reply; }
|
|
146
|
+
catch { throw new MayuraError(ran.code === null ? 'TIMEOUT' : 'TOOL_FAILED', ran.code === null ? 'agent-browser did not answer in time.' : 'agent-browser did not answer in JSON.'); }
|
|
147
|
+
// agent-browser's own report of what it did; its bookkeeping (lifecycle) is left out.
|
|
148
|
+
const { lifecycle: _lifecycle, ...data } = (reply.data && typeof reply.data === 'object' && !Array.isArray(reply.data) ? reply.data : { value: reply.data ?? null }) as Record<string, unknown>;
|
|
149
|
+
const result = reply.success === true ? { ok: true, ...data } : { ok: false, error: typeof reply.error === 'string' ? reply.error.slice(0, 1_000) : 'agent-browser did not succeed.' };
|
|
150
|
+
const text = JSON.stringify(result);
|
|
151
|
+
if (new TextEncoder().encode(text).byteLength > maxResultBytes) {
|
|
152
|
+
const snapshot = typeof data['snapshot'] === 'string' ? data['snapshot'] : undefined;
|
|
153
|
+
return snapshot !== undefined ? { ok: true, snapshot: snapshot.slice(0, Math.floor(maxResultBytes / 2)), truncated: true } : { ok: false, error: `The result is larger than ${maxResultBytes} bytes.` };
|
|
154
|
+
}
|
|
155
|
+
return result as JsonObject;
|
|
156
|
+
};
|
|
157
|
+
const tool = (id: string, permission: string, effects: 'read' | 'write', description: string, commands: Readonly<Record<string, { min: number; max: number; flags?: readonly string[] }>>) =>
|
|
158
|
+
defineTool({ id: `${name}.${id}`, version: '1', effects, capabilities: [`agent-browser:${name}:${permission}`], timeoutMs: timeoutMs + 15_000, description,
|
|
159
|
+
input: commandInput(Object.keys(commands)), output: anything,
|
|
160
|
+
inputJsonSchema: { type: 'object', additionalProperties: false, required: ['command'], properties: {
|
|
161
|
+
command: { type: 'string', enum: Object.keys(commands) }, args: { type: 'array', items: { type: 'string' }, maxItems: 10 } } } as unknown as JsonObject,
|
|
162
|
+
execute: async (request: { command: string; args?: string[] }, context: { readonly runId: string; readonly scope: Scope; readonly signal: AbortSignal }) => {
|
|
163
|
+
try { return await run([request.command, ...commandArgs(request.command, request.args ?? [], commands)], context); }
|
|
164
|
+
catch (error) {
|
|
165
|
+
// Mistakes the model can put right come back as the result, so it can try again.
|
|
166
|
+
if (error instanceof MayuraError && correctable.has(error.code)) return { error: error.code, message: error.message } as JsonObject;
|
|
167
|
+
throw error;
|
|
168
|
+
}
|
|
169
|
+
} } as never) as unknown as AnyTool;
|
|
170
|
+
|
|
171
|
+
const tools: AnyTool[] = [
|
|
172
|
+
tool('read', 'read', 'read', `Read the page open in the ${name} browser with agent-browser: snapshot (an outline whose @e refs the act tool takes; flags -i interactive only, -c compact, -d depth, -s selector), get text|html|value|attr <name>|title|url|count|box [@ref or selector], or is visible|enabled|checked <@ref or selector>.`, readCommands),
|
|
173
|
+
];
|
|
174
|
+
if (options.act) {
|
|
175
|
+
tools.push(tool('act', 'act', 'write', `Act on the page open in the ${name} browser with agent-browser, by @ref from a snapshot or a CSS selector: click, dblclick, fill <ref> <text>, type <ref> <text>, press <key>, hover, focus, check, uncheck, select <ref> <value>, scroll <up|down|left|right> [px], scrollintoview, wait <ref or ms>. Arguments may not start with -. To open a page, use the browser's goto.`, actCommands));
|
|
176
|
+
}
|
|
177
|
+
if (options.evaluate) {
|
|
178
|
+
tools.push(tool('eval', 'evaluate', 'write', `Run a JavaScript expression in the page open in the ${name} browser with agent-browser, and get its value.`, { eval: { min: 1, max: 1 } }));
|
|
179
|
+
}
|
|
180
|
+
return tools;
|
|
181
|
+
}
|