@agent-om/cli 0.0.0-stage → 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 +64 -0
- package/README.md +87 -2
- package/dist/agent-om.mjs +498 -0
- package/dist/server-BydUJYxP.mjs +4751 -0
- package/dist/session-CeDFN-1x.mjs +658 -0
- package/package.json +54 -3
- package/skill/SKILL.md +64 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
Copyright 2026 Sumeet Debnath and Prathmesh Vhatkar
|
|
2
|
+
|
|
3
|
+
@agent-om/cli is source-available, not open source. It is licensed under the
|
|
4
|
+
PolyForm Strict License 1.0.0, whose full text follows.
|
|
5
|
+
|
|
6
|
+
# PolyForm Strict License 1.0.0
|
|
7
|
+
|
|
8
|
+
<https://polyformproject.org/licenses/strict/1.0.0>
|
|
9
|
+
|
|
10
|
+
## Acceptance
|
|
11
|
+
|
|
12
|
+
In order to get any license under these terms, you must agree to them as both strict obligations and conditions to all your licenses.
|
|
13
|
+
|
|
14
|
+
## Copyright License
|
|
15
|
+
|
|
16
|
+
The licensor grants you a copyright license for the software to do everything you might do with the software that would otherwise infringe the licensor's copyright in it for any permitted purpose, other than distributing the software or making changes or new works based on the software.
|
|
17
|
+
|
|
18
|
+
## Patent License
|
|
19
|
+
|
|
20
|
+
The licensor grants you a patent license for the software that covers patent claims the licensor can license, or becomes able to license, that you would infringe by using the software.
|
|
21
|
+
|
|
22
|
+
## Noncommercial Purposes
|
|
23
|
+
|
|
24
|
+
Any noncommercial purpose is a permitted purpose.
|
|
25
|
+
|
|
26
|
+
## Personal Uses
|
|
27
|
+
|
|
28
|
+
Personal use for research, experiment, and testing for the benefit of public knowledge, personal study, private entertainment, hobby projects, amateur pursuits, or religious observance, without any anticipated commercial application, is use for a permitted purpose.
|
|
29
|
+
|
|
30
|
+
## Noncommercial Organizations
|
|
31
|
+
|
|
32
|
+
Use by any charitable organization, educational institution, public research organization, public safety or health organization, environmental protection organization, or government institution is use for a permitted purpose regardless of the source of funding or obligations resulting from the funding.
|
|
33
|
+
|
|
34
|
+
## Fair Use
|
|
35
|
+
|
|
36
|
+
You may have "fair use" rights for the software under the law. These terms do not limit them.
|
|
37
|
+
|
|
38
|
+
## No Other Rights
|
|
39
|
+
|
|
40
|
+
These terms do not allow you to sublicense or transfer any of your licenses to anyone else, or prevent the licensor from granting licenses to anyone else. These terms do not imply any other licenses.
|
|
41
|
+
|
|
42
|
+
## Patent Defense
|
|
43
|
+
|
|
44
|
+
If you make any written claim that the software infringes or contributes to infringement of any patent, your patent license for the software granted under these terms ends immediately. If your company makes such a claim, your patent license ends immediately for work on behalf of your company.
|
|
45
|
+
|
|
46
|
+
## Violations
|
|
47
|
+
|
|
48
|
+
The first time you are notified in writing that you have violated any of these terms, or done anything with the software not covered by your licenses, your licenses can nonetheless continue if you come into full compliance with these terms, and take practical steps to correct past violations, within 32 days of receiving notice. Otherwise, all your licenses end immediately.
|
|
49
|
+
|
|
50
|
+
## No Liability
|
|
51
|
+
|
|
52
|
+
***As far as the law allows, the software comes as is, without any warranty or condition, and the licensor will not be liable to you for any damages arising out of these terms or the use or nature of the software, under any kind of legal claim.***
|
|
53
|
+
|
|
54
|
+
## Definitions
|
|
55
|
+
|
|
56
|
+
The **licensor** is the individual or entity offering these terms, and the **software** is the software the licensor makes available under these terms.
|
|
57
|
+
|
|
58
|
+
**You** refers to the individual or entity agreeing to these terms.
|
|
59
|
+
|
|
60
|
+
**Your company** is any legal entity, sole proprietorship, or other kind of organization that you work for, plus all organizations that have control over, are under the control of, or are under common control with that organization. **Control** means ownership of substantially all the assets of an entity, or the power to direct its management and policies by vote, contract, or otherwise. Control can be direct or indirect.
|
|
61
|
+
|
|
62
|
+
**Your licenses** are all the licenses granted to you for the software under these terms.
|
|
63
|
+
|
|
64
|
+
**Use** means anything you do with the software requiring one of your licenses.
|
package/README.md
CHANGED
|
@@ -1,3 +1,88 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @agent-om/cli
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
`agent-om`: a command-line tool any AI agent drives from a shell, on any model, to use a
|
|
4
|
+
web page: a browser kept open between calls, the page read as the
|
|
5
|
+
[Agent Object Model](https://github.com/TheAsyncLooop/agent-om)'s view
|
|
6
|
+
(Chrome's accessibility tree and the DOM, merged, secrets removed), and acted
|
|
7
|
+
on by ref. It works on any site, built for agents or not.
|
|
8
|
+
|
|
9
|
+
> **Status:** 0.x. Commands and output can still change.
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
npm install -g @agent-om/cli
|
|
13
|
+
agent-om install # once: the browser it drives
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
Node 22 or later.
|
|
17
|
+
|
|
18
|
+
## Use
|
|
19
|
+
|
|
20
|
+
```console
|
|
21
|
+
$ agent-om open http://localhost:3000/
|
|
22
|
+
Page: Monitors (/)
|
|
23
|
+
- main [e7]
|
|
24
|
+
- heading "Monitors" [e8] level=1
|
|
25
|
+
- button "Add monitor" [e9]
|
|
26
|
+
- text: Checkout API
|
|
27
|
+
|
|
28
|
+
$ agent-om click e9
|
|
29
|
+
clicked button "Add monitor" [e9]
|
|
30
|
+
Changes on Monitors:
|
|
31
|
+
+ new in list [e10]:
|
|
32
|
+
- text: Orders DB
|
|
33
|
+
|
|
34
|
+
$ agent-om stop
|
|
35
|
+
stopped
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Every action prints what it did and what changed on the page. `agent-om help`
|
|
39
|
+
lists every command: reading (`open`, `snapshot`, `changes`, `inspect`),
|
|
40
|
+
acting (`click`, `fill`, `type`, `select`, `check`, `press`, `scroll`,
|
|
41
|
+
`upload`, `fill-secret`, …) and the session (`start`, `stop`, `status`).
|
|
42
|
+
|
|
43
|
+
- **What the page says, nothing more**: Chrome's tree, the DOM's facts and the
|
|
44
|
+
app's `data-agentom-*` markup; never guesses from an app's ids or class names.
|
|
45
|
+
- **Errors that say what to try**: what covers an element, the field inside a
|
|
46
|
+
box, the choices a drop-down has.
|
|
47
|
+
- **Secrets stay with the user.** A credential is filled only from an
|
|
48
|
+
environment variable the user set and tied to its site
|
|
49
|
+
(`fill-secret <ref> <VARIABLE>`, with `<VARIABLE>_ORIGIN=https://…`), only
|
|
50
|
+
on that site, and never shown; what the page marks redact or ignore never appears; an action the
|
|
51
|
+
page asks to confirm needs `--confirm`.
|
|
52
|
+
- **Many steps in one call.** `agent-om script` runs JavaScript that drives
|
|
53
|
+
the tool: `om.click(ref)`, `om.find({ role, name })`, `om.table(ref)` and
|
|
54
|
+
every other command, in loops, printing only what's needed. It runs in a
|
|
55
|
+
sandbox of its own: it reaches the page only through the tool, never the
|
|
56
|
+
page's DOM, Node or the network.
|
|
57
|
+
- **`eval`, unless you turn it off.** The agent can run JavaScript in the
|
|
58
|
+
page (`agent-om eval "<script>"`, or `--ref e40` to run it on an element
|
|
59
|
+
the view found) for what the view doesn't hold.
|
|
60
|
+
What it returns has secret fields' values and what the page keeps out taken
|
|
61
|
+
out, but a script can read anything the page's scripts can, cookies and
|
|
62
|
+
storage included: where that isn't acceptable, start the session with
|
|
63
|
+
`AGENT_OM_ALLOW_EVAL=0` in its environment, and no later command turns it
|
|
64
|
+
back on. A secret is taken out as the page writes it: a script that
|
|
65
|
+
rewrites one before returning it (reversed, encoded) gets it through. In a
|
|
66
|
+
session attached to your own browser (`--cdp`, `--auto-connect`), where a
|
|
67
|
+
script would read everything you are signed in to, `eval` is off unless the
|
|
68
|
+
session starts with `AGENT_OM_ALLOW_EVAL=1`.
|
|
69
|
+
|
|
70
|
+
## For an agent
|
|
71
|
+
|
|
72
|
+
The package ships a skill text that teaches any model the workflow (skill
|
|
73
|
+
name `agent-om`); `agent-om skill` prints the one for the installed version.
|
|
74
|
+
For Claude Code:
|
|
75
|
+
|
|
76
|
+
```sh
|
|
77
|
+
mkdir -p .claude/skills/agent-om
|
|
78
|
+
agent-om skill > .claude/skills/agent-om/SKILL.md
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
## Documentation
|
|
82
|
+
|
|
83
|
+
- [How it works](https://github.com/TheAsyncLooop/agent-om/blob/master/docs/semantic-projection.md)
|
|
84
|
+
- [The repository](https://github.com/TheAsyncLooop/agent-om)
|
|
85
|
+
|
|
86
|
+
## License
|
|
87
|
+
|
|
88
|
+
Source-available, not open source. Licensed under the [PolyForm Strict License 1.0.0](https://github.com/TheAsyncLooop/agent-om/blob/master/packages/cli/LICENSE). Copyright 2026 Sumeet Debnath and Prathmesh Vhatkar.
|
|
@@ -0,0 +1,498 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { _ as LOCATOR_ARGS, a as ALLOW_EVAL, d as screenshotOn, f as ATTACH_MS, h as discover, i as sessionsHome, l as UsageError, m as TAB_LEVELS, n as sessionDir, o as ALLOW_SCREENSHOT, s as SCREENSHOT_WARNING, t as readSession, u as evalOn, v as commandsFor } from "./session-CeDFN-1x.mjs";
|
|
3
|
+
import { createRequire } from "node:module";
|
|
4
|
+
import { spawn, spawnSync } from "node:child_process";
|
|
5
|
+
import { existsSync, mkdirSync, openSync, readFileSync, readdirSync, statSync } from "node:fs";
|
|
6
|
+
import { dirname, join, resolve } from "node:path";
|
|
7
|
+
import { createInterface } from "node:readline/promises";
|
|
8
|
+
import { fileURLToPath } from "node:url";
|
|
9
|
+
// src/topics.ts
|
|
10
|
+
const TOPICS = {
|
|
11
|
+
session: {
|
|
12
|
+
about: "staying signed in, the user's own browser, tabs, downloads, the sites a session may open",
|
|
13
|
+
text: `agent-om help session
|
|
14
|
+
|
|
15
|
+
Staying signed in. A session's sign-in ends with it. To keep one,
|
|
16
|
+
agent-om save-state <file.json> saves what is signed in to a file only the user can read
|
|
17
|
+
agent-om start --state <file.json> starts signed in the same
|
|
18
|
+
agent-om start --profile <folder> keeps a browser profile in that folder from one session to the next
|
|
19
|
+
The file and the folder hold the user's sign-ins: never print them or send them anywhere. In the user's own browser
|
|
20
|
+
(a session started with --cdp or --auto-connect) there is nothing to save: it keeps its own sign-ins, and save-state
|
|
21
|
+
refuses.
|
|
22
|
+
|
|
23
|
+
The user's own browser. agent-om start --auto-connect (or --cdp <port>) works in a browser the user already has open,
|
|
24
|
+
with their sign-ins, when the user asks for that. Chrome asks the user to allow it each time a session starts: tell
|
|
25
|
+
them to expect that, and to click Allow. How much of it you may touch is the user's to say, with --tabs: own (the
|
|
26
|
+
default) is only the tabs you open and those they open; active also the tab in front; all every tab. Use only the
|
|
27
|
+
level the user gave: never start again with a wider one yourself. stop closes your tabs and leaves their browser open.
|
|
28
|
+
|
|
29
|
+
Tabs. agent-om tabs lists the tabs this session's pages opened; agent-om switch-tab <n> works in another. When an
|
|
30
|
+
action opens a tab (a sign-in window, a link to a new tab), you are in it after that action, and read it whole. When
|
|
31
|
+
it closes itself (a sign-in window does, once you've signed in), you are back in the tab that opened it, and read
|
|
32
|
+
what changed there.
|
|
33
|
+
|
|
34
|
+
Downloads. A file the page downloads (an export, a report) is saved in the session's folder, and the action says its
|
|
35
|
+
name, size and path: read it from there.
|
|
36
|
+
|
|
37
|
+
Sites. When the user started the session with --allow-origin, a page load to any other site is stopped, and the
|
|
38
|
+
action says so: stay on those sites.
|
|
39
|
+
`
|
|
40
|
+
},
|
|
41
|
+
trouble: {
|
|
42
|
+
about: "the tool doesn't start, a page stops answering, something fails without saying why",
|
|
43
|
+
text: `agent-om help trouble
|
|
44
|
+
|
|
45
|
+
The tool doesn't start. agent-om doctor checks what the tool needs and says what to do about anything missing;
|
|
46
|
+
agent-om install downloads its browser.
|
|
47
|
+
|
|
48
|
+
A page stops answering (a script on it never ends). It is given up after a while: its tab is closed, a new one takes
|
|
49
|
+
its place, and the answer says how to load the page again.
|
|
50
|
+
|
|
51
|
+
Something didn't happen, and the page doesn't say why. agent-om console shows what the page logged since you last
|
|
52
|
+
asked (its errors too; --errors for only those and warnings), and agent-om network the requests it made (--failed
|
|
53
|
+
for those that failed or answered an error), each address without its query. Use them to find out why something
|
|
54
|
+
didn't happen, not instead of reading the page.
|
|
55
|
+
`
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
// src/main.ts
|
|
59
|
+
/** The opt-in commands currently on in this environment: eval (unless AGENT_OM_ALLOW_EVAL=0) and screenshot (only when AGENT_OM_ALLOW_SCREENSHOT=1). */
|
|
60
|
+
const turnedOn = () => [...evalOn(process.env) ? ["eval"] : [], ...screenshotOn(process.env) ? ["screenshot"] : []];
|
|
61
|
+
const wrap = (text, indent, width = 120) => {
|
|
62
|
+
const lines = [];
|
|
63
|
+
let line = "";
|
|
64
|
+
for (const word of text.split(" ")) if (line && indent + line.length + 1 + word.length > width) {
|
|
65
|
+
lines.push(line);
|
|
66
|
+
line = word;
|
|
67
|
+
} else line = line ? `${line} ${word}` : word;
|
|
68
|
+
if (line) lines.push(line);
|
|
69
|
+
return lines.join("\n" + " ".repeat(indent));
|
|
70
|
+
};
|
|
71
|
+
const COL = 39;
|
|
72
|
+
/** One command's help: how it's written, what it does, and its options. */
|
|
73
|
+
const entry = (c) => {
|
|
74
|
+
const head = ` ${[c.name, ...c.args.map((a) => a.cli)].join(" ")}`;
|
|
75
|
+
const options = c.args.filter((a) => a.cli.includes("-") || a.cli.startsWith("[")).filter((a) => a.about);
|
|
76
|
+
const about = [c.about, ...options.map((a) => `${a.cli.replace(/[[\]]/g, "").split(" ")[0]}: ${a.about}`)].join("; ");
|
|
77
|
+
return head.length + 2 <= COL ? head.padEnd(COL) + wrap(about, COL) : `${head}\n${" ".repeat(COL)}${wrap(about, COL)}`;
|
|
78
|
+
};
|
|
79
|
+
/** A command's own help (help <command>, <command> --help); an opt-in command that is off says so. */
|
|
80
|
+
function commandHelp(name) {
|
|
81
|
+
const c = commandsFor("cli", ["eval", "screenshot"]).find((x) => x.name === name);
|
|
82
|
+
if (!c) return void 0;
|
|
83
|
+
if (c.optIn && !turnedOn().includes(name)) {
|
|
84
|
+
if (name === "screenshot") return `screenshot is off by default: Agent OM works from text projection without screenshots. Start a session with --allow-screenshot (and confirm the warning) or set ${ALLOW_SCREENSHOT}=1 to enable it.\n`;
|
|
85
|
+
return `${name} is off in this session: the user turned it off (${ALLOW_EVAL}=0 where the session starts)\n`;
|
|
86
|
+
}
|
|
87
|
+
return `${entry(c)}\n`;
|
|
88
|
+
}
|
|
89
|
+
usage();
|
|
90
|
+
function usage() {
|
|
91
|
+
const on = turnedOn();
|
|
92
|
+
const commands = commandsFor("cli", on);
|
|
93
|
+
const section = (group) => commands.filter((c) => c.group === group).map(entry).join("\n");
|
|
94
|
+
return [
|
|
95
|
+
"agent-om <command> [arguments] [--session <name>]",
|
|
96
|
+
"",
|
|
97
|
+
"Reading",
|
|
98
|
+
section("read"),
|
|
99
|
+
"",
|
|
100
|
+
`Acting (a ref from the page, or ${LOCATOR_ARGS.map((a) => a.cli).join(" ")} for something with no ref);`,
|
|
101
|
+
"each prints what it did and what changed on the page",
|
|
102
|
+
section("act"),
|
|
103
|
+
"",
|
|
104
|
+
" --confirm take an action the page says must be confirmed (only when the user's request asks for it)",
|
|
105
|
+
" --accept accept the browser's own confirm dialog if the action opens one",
|
|
106
|
+
"",
|
|
107
|
+
"Session",
|
|
108
|
+
section("session"),
|
|
109
|
+
"",
|
|
110
|
+
"Help",
|
|
111
|
+
" help this text",
|
|
112
|
+
...Object.entries(TOPICS).map(([name, t]) => ` ${`help ${name}`.padEnd(37)}${t.about}`),
|
|
113
|
+
" skill the skill an agent reads to use this tool, for this version (save it as",
|
|
114
|
+
" .claude/skills/agent-om/SKILL.md, or wherever the agent reads skills)",
|
|
115
|
+
""
|
|
116
|
+
].join("\n");
|
|
117
|
+
}
|
|
118
|
+
/** The options of `start`, which a first command may carry for the session it starts. */
|
|
119
|
+
const START_OPTIONS = commandsFor("cli").find((c) => c.name === "start").args.flatMap((a) => [...a.cli.matchAll(/--([a-z-]+)/g)].map(([, name]) => name));
|
|
120
|
+
/**
|
|
121
|
+
* The options the table of commands names (opt-in commands too: one turned off is refused by name, not as an
|
|
122
|
+
* unknown option), each taking a value (`--ref <ref>`, `--text "<what it shows>"`, `--tabs own|active|all`) or a
|
|
123
|
+
* switch (`--exact`, `--up|--down`); with those every command takes (`--session`, `--help`) and a few more.
|
|
124
|
+
*/
|
|
125
|
+
const OPTIONS = [...commandsFor("cli", ["eval", "screenshot"]).flatMap((c) => c.args), ...LOCATOR_ARGS].flatMap((a) => [...a.cli.matchAll(/--([a-z-]+)(?:[ =]([<"][^>"]*[>"]|[a-z]+(?:\|[a-z]+)+))?/g)]);
|
|
126
|
+
const BOOLEAN = /* @__PURE__ */ new Set([
|
|
127
|
+
"interactive",
|
|
128
|
+
"i",
|
|
129
|
+
"confirm",
|
|
130
|
+
"accept",
|
|
131
|
+
"help",
|
|
132
|
+
...OPTIONS.filter(([, , value]) => !value).map(([, name]) => name)
|
|
133
|
+
]);
|
|
134
|
+
const VALUE = /* @__PURE__ */ new Set(["session", ...OPTIONS.filter(([, , value]) => value).map(([, name]) => name)]);
|
|
135
|
+
/** A command line's command, arguments and options. An option the tool hasn't got, or one missing its value, is refused. */
|
|
136
|
+
function parse(argv) {
|
|
137
|
+
const args = {
|
|
138
|
+
_: [],
|
|
139
|
+
flags: {}
|
|
140
|
+
};
|
|
141
|
+
let cmd;
|
|
142
|
+
const word = (a) => {
|
|
143
|
+
if (cmd === void 0) cmd = a;
|
|
144
|
+
else args._.push(a);
|
|
145
|
+
};
|
|
146
|
+
for (let i = 0; i < argv.length; i++) {
|
|
147
|
+
const a = argv[i];
|
|
148
|
+
if (a === "--") {
|
|
149
|
+
argv.slice(i + 1).forEach(word);
|
|
150
|
+
break;
|
|
151
|
+
}
|
|
152
|
+
if (a === "-i") {
|
|
153
|
+
args.flags.interactive = true;
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
if (!a.startsWith("--")) {
|
|
157
|
+
word(a);
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
const name = a.slice(2);
|
|
161
|
+
if (BOOLEAN.has(name)) args.flags[name] = true;
|
|
162
|
+
else if (VALUE.has(name)) {
|
|
163
|
+
const value = argv[++i];
|
|
164
|
+
if (value === void 0) throw new UsageError(`${a} needs a value (agent-om help)`);
|
|
165
|
+
args.flags[name] = value;
|
|
166
|
+
} else throw new UsageError(`no option ${a} (agent-om help); text that starts with -- goes after a -- of its own`);
|
|
167
|
+
}
|
|
168
|
+
return {
|
|
169
|
+
cmd,
|
|
170
|
+
args
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
/** All of stdin, as text. */
|
|
174
|
+
async function stdin() {
|
|
175
|
+
const chunks = [];
|
|
176
|
+
for await (const c of process.stdin) chunks.push(c);
|
|
177
|
+
return Buffer.concat(chunks).toString("utf8");
|
|
178
|
+
}
|
|
179
|
+
const nameOf = (args) => typeof args.flags.session === "string" && args.flags.session || process.env.AGENT_OM_SESSION || "default";
|
|
180
|
+
async function send(name, cmd, args, env) {
|
|
181
|
+
const s = readSession(name);
|
|
182
|
+
if (!s) return {
|
|
183
|
+
ok: false,
|
|
184
|
+
error: "no session"
|
|
185
|
+
};
|
|
186
|
+
const body = {
|
|
187
|
+
token: s.token,
|
|
188
|
+
cmd,
|
|
189
|
+
args,
|
|
190
|
+
...env ? { env } : {}
|
|
191
|
+
};
|
|
192
|
+
try {
|
|
193
|
+
return await (await fetch(`http://127.0.0.1:${s.port}/`, {
|
|
194
|
+
method: "POST",
|
|
195
|
+
body: JSON.stringify(body)
|
|
196
|
+
})).json();
|
|
197
|
+
} catch {
|
|
198
|
+
return {
|
|
199
|
+
ok: false,
|
|
200
|
+
error: "no session"
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
/** Ask the user to confirm enabling screenshots after showing the warning. */
|
|
205
|
+
async function confirmScreenshots(input = process.stdin, output = process.stderr) {
|
|
206
|
+
if (input === process.stdin && !process.stdin.isTTY) return {
|
|
207
|
+
ok: false,
|
|
208
|
+
error: `${SCREENSHOT_WARNING}\n\nCannot prompt for confirmation in a non-interactive terminal. Re-run interactively to confirm, or set ${ALLOW_SCREENSHOT}=1 to explicitly enable screenshots.`
|
|
209
|
+
};
|
|
210
|
+
output.write(`${SCREENSHOT_WARNING}\n\n`);
|
|
211
|
+
const rl = createInterface({
|
|
212
|
+
input,
|
|
213
|
+
output,
|
|
214
|
+
terminal: false
|
|
215
|
+
});
|
|
216
|
+
try {
|
|
217
|
+
const answer = (await rl.question("Enable screenshots for this session? [y/N]: ").catch(() => "")).trim().toLowerCase();
|
|
218
|
+
if (answer === "y" || answer === "yes") return { ok: true };
|
|
219
|
+
return {
|
|
220
|
+
ok: false,
|
|
221
|
+
error: "Cancelled: screenshots remain off."
|
|
222
|
+
};
|
|
223
|
+
} finally {
|
|
224
|
+
rl.close();
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
async function start(name, args, input = process.stdin, promptOut = process.stderr) {
|
|
228
|
+
const running = await send(name, "status", args);
|
|
229
|
+
if (running.ok) {
|
|
230
|
+
if (args.flags["allow-screenshot"] === true) return {
|
|
231
|
+
ok: false,
|
|
232
|
+
error: "a session is already running: run agent-om stop first, then start with --allow-screenshot"
|
|
233
|
+
};
|
|
234
|
+
return {
|
|
235
|
+
ok: true,
|
|
236
|
+
out: `already running: ${running.out}`
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
const viewport = typeof args.flags.viewport === "string" ? args.flags.viewport : "1280x900";
|
|
240
|
+
if (!/^\d{2,5}x\d{2,5}$/.test(viewport)) return {
|
|
241
|
+
ok: false,
|
|
242
|
+
error: `--viewport <width>x<height>, in pixels (1280x900): '${viewport}'`
|
|
243
|
+
};
|
|
244
|
+
if (typeof args.flags.state === "string" && typeof args.flags.profile === "string") return {
|
|
245
|
+
ok: false,
|
|
246
|
+
error: "--state or --profile, not both: a profile keeps its own sign-ins"
|
|
247
|
+
};
|
|
248
|
+
const attach = typeof args.flags.cdp === "string" || args.flags["auto-connect"] === true;
|
|
249
|
+
if (attach && (args.flags.state !== void 0 || args.flags.profile !== void 0 || args.flags.executable !== void 0 || args.flags.headed !== void 0)) return {
|
|
250
|
+
ok: false,
|
|
251
|
+
error: "--cdp and --auto-connect attach to a browser that is running, with its own sign-ins and window: not with --state, --profile, --executable or --headed"
|
|
252
|
+
};
|
|
253
|
+
const tabs = args.flags.tabs;
|
|
254
|
+
if (tabs !== void 0 && !attach) return {
|
|
255
|
+
ok: false,
|
|
256
|
+
error: "--tabs is for a browser attached to (--cdp or --auto-connect): a browser of its own is all the agent's"
|
|
257
|
+
};
|
|
258
|
+
if (tabs !== void 0 && !TAB_LEVELS.includes(tabs)) return {
|
|
259
|
+
ok: false,
|
|
260
|
+
error: `--tabs own, active or all: '${String(tabs)}'`
|
|
261
|
+
};
|
|
262
|
+
if (args.flags["allow-origin"] !== void 0) {
|
|
263
|
+
const bad = String(args.flags["allow-origin"]).split(",").find((o) => !/^https?:\/\/[^/\s]+$/.test(o.trim().replace(/\/$/, "")));
|
|
264
|
+
if (bad !== void 0) return {
|
|
265
|
+
ok: false,
|
|
266
|
+
error: `--allow-origin takes sites as https://example.com, separated by commas: '${bad}'`
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
if (args.flags["allow-screenshot"] === true) {
|
|
270
|
+
const confirmed = await confirmScreenshots(input, promptOut);
|
|
271
|
+
if (!confirmed.ok) return confirmed;
|
|
272
|
+
}
|
|
273
|
+
const dir = sessionDir(name);
|
|
274
|
+
mkdirSync(dir, {
|
|
275
|
+
recursive: true,
|
|
276
|
+
mode: 448
|
|
277
|
+
});
|
|
278
|
+
const logFile = join(dir, "server.log");
|
|
279
|
+
const from = existsSync(logFile) ? statSync(logFile).size : 0;
|
|
280
|
+
const log = openSync(logFile, "a");
|
|
281
|
+
const bin = process.argv[1] ?? fileURLToPath(new URL("../bin/agent-om.mjs", import.meta.url));
|
|
282
|
+
const pass = (flag, path = false) => typeof args.flags[flag] === "string" ? [`--${flag}`, path ? resolve(args.flags[flag]) : args.flags[flag]] : [];
|
|
283
|
+
const child = spawn(process.execPath, [
|
|
284
|
+
bin,
|
|
285
|
+
"__serve",
|
|
286
|
+
"--session",
|
|
287
|
+
name,
|
|
288
|
+
"--viewport",
|
|
289
|
+
viewport,
|
|
290
|
+
...args.flags.headed ? ["--headed"] : [],
|
|
291
|
+
...pass("state", true),
|
|
292
|
+
...pass("profile", true),
|
|
293
|
+
...pass("executable"),
|
|
294
|
+
...pass("cdp"),
|
|
295
|
+
...pass("tabs"),
|
|
296
|
+
...pass("allow-origin"),
|
|
297
|
+
...args.flags["auto-connect"] ? ["--auto-connect"] : [],
|
|
298
|
+
...args.flags["allow-screenshot"] ? ["--allow-screenshot"] : []
|
|
299
|
+
], {
|
|
300
|
+
detached: true,
|
|
301
|
+
stdio: [
|
|
302
|
+
"ignore",
|
|
303
|
+
log,
|
|
304
|
+
log
|
|
305
|
+
]
|
|
306
|
+
});
|
|
307
|
+
child.unref();
|
|
308
|
+
let exited = false;
|
|
309
|
+
child.once("exit", () => void (exited = true));
|
|
310
|
+
const deadline = Date.now() + (attach ? ATTACH_MS + 5e3 : 3e4);
|
|
311
|
+
while (Date.now() < deadline && !exited) {
|
|
312
|
+
await new Promise((r) => setTimeout(r, 200));
|
|
313
|
+
const s = readSession(name);
|
|
314
|
+
if (s && s.pid === child.pid) {
|
|
315
|
+
const status = await send(name, "status", args);
|
|
316
|
+
if (status.ok) return {
|
|
317
|
+
ok: true,
|
|
318
|
+
out: `started: ${status.out}`
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
const other = await send(name, "status", args);
|
|
323
|
+
if (other.ok) return {
|
|
324
|
+
ok: true,
|
|
325
|
+
out: `already running: ${other.out}`
|
|
326
|
+
};
|
|
327
|
+
const said = existsSync(logFile) ? readFileSync(logFile).subarray(from).toString("utf8").trim() : "";
|
|
328
|
+
return {
|
|
329
|
+
ok: false,
|
|
330
|
+
error: `the session didn't start${said ? `:\n${said}` : ""}\n(its log: ${logFile})`
|
|
331
|
+
};
|
|
332
|
+
}
|
|
333
|
+
const require = createRequire(import.meta.url);
|
|
334
|
+
/** Playwright's own command, from the version this one is tested with. */
|
|
335
|
+
const playwrightCli = () => join(dirname(require.resolve("playwright/package.json")), "cli.js");
|
|
336
|
+
/** Download the browser this version is tested with: Playwright's Chromium, for its version. */
|
|
337
|
+
function install(dryRun) {
|
|
338
|
+
const r = spawnSync(process.execPath, [
|
|
339
|
+
playwrightCli(),
|
|
340
|
+
"install",
|
|
341
|
+
"chromium",
|
|
342
|
+
...dryRun ? ["--dry-run"] : []
|
|
343
|
+
], { stdio: "inherit" });
|
|
344
|
+
if (dryRun) return r.status === 0 ? {
|
|
345
|
+
ok: true,
|
|
346
|
+
out: "nothing was downloaded (--dry-run)"
|
|
347
|
+
} : {
|
|
348
|
+
ok: false,
|
|
349
|
+
error: "Playwright's installer failed (it said why, above)"
|
|
350
|
+
};
|
|
351
|
+
return r.status === 0 ? {
|
|
352
|
+
ok: true,
|
|
353
|
+
out: "the browser is installed: agent-om open <address> starts it"
|
|
354
|
+
} : {
|
|
355
|
+
ok: false,
|
|
356
|
+
error: `the browser didn't install (Playwright's installer said why, above)`
|
|
357
|
+
};
|
|
358
|
+
}
|
|
359
|
+
/** What the tool needs, checked one by one: each line says it's there, or what to do. */
|
|
360
|
+
async function doctor() {
|
|
361
|
+
const lines = [];
|
|
362
|
+
let bad = false;
|
|
363
|
+
const ok = (line) => lines.push(`ok ${line}`);
|
|
364
|
+
const no = (line) => {
|
|
365
|
+
bad = true;
|
|
366
|
+
lines.push(`no ${line}`);
|
|
367
|
+
};
|
|
368
|
+
const [major] = process.versions.node.split(".").map(Number);
|
|
369
|
+
if (major >= 22) ok(`Node ${process.versions.node}`);
|
|
370
|
+
else no(`Node ${process.versions.node}: this tool needs Node 22 or later`);
|
|
371
|
+
const { chromium } = await import("playwright");
|
|
372
|
+
const version = require("playwright/package.json").version;
|
|
373
|
+
const browser = chromium.executablePath();
|
|
374
|
+
if (existsSync(browser)) {
|
|
375
|
+
ok(`Playwright ${version}, with its Chromium at ${browser}`);
|
|
376
|
+
const started = await chromium.launch().then(async (b) => (await b.close(), void 0), (e) => e.message.split("\n")[0]);
|
|
377
|
+
if (started) no(`the browser doesn't start: ${started}`);
|
|
378
|
+
else ok("the browser starts");
|
|
379
|
+
} else no(`Playwright ${version}'s Chromium isn't installed: agent-om install`);
|
|
380
|
+
const home = sessionsHome();
|
|
381
|
+
const running = [];
|
|
382
|
+
for (const name of existsSync(home) ? readdirSync(home) : []) if (readSession(name) && (await send(name, "status", {
|
|
383
|
+
_: [],
|
|
384
|
+
flags: {}
|
|
385
|
+
})).ok) running.push(name);
|
|
386
|
+
ok(running.length ? `sessions running: ${running.join(", ")} (in ${home})` : `no session running (they are kept in ${home})`);
|
|
387
|
+
const found = discover();
|
|
388
|
+
lines.push(found.length ? `ok a browser with remote debugging on, for --auto-connect: ${found.length === 1 ? "one" : found.length}` : "-- no running browser with remote debugging on (only --auto-connect needs one: Chrome 144 or later, at chrome://inspect/#remote-debugging)");
|
|
389
|
+
return bad ? {
|
|
390
|
+
ok: false,
|
|
391
|
+
error: lines.join("\n")
|
|
392
|
+
} : {
|
|
393
|
+
ok: true,
|
|
394
|
+
out: lines.join("\n")
|
|
395
|
+
};
|
|
396
|
+
}
|
|
397
|
+
async function main(argv, input = process.stdin) {
|
|
398
|
+
process.stdout.on("error", (e) => {
|
|
399
|
+
if (e.code === "EPIPE") process.exit(0);
|
|
400
|
+
throw e;
|
|
401
|
+
});
|
|
402
|
+
let parsed;
|
|
403
|
+
try {
|
|
404
|
+
parsed = parse(argv);
|
|
405
|
+
} catch (e) {
|
|
406
|
+
process.stderr.write(`${e.message}\n`);
|
|
407
|
+
process.exitCode = 1;
|
|
408
|
+
return;
|
|
409
|
+
}
|
|
410
|
+
const { cmd, args } = parsed;
|
|
411
|
+
const name = nameOf(args);
|
|
412
|
+
if (cmd === "__serve") {
|
|
413
|
+
const { serve } = await import("./server-BydUJYxP.mjs");
|
|
414
|
+
const [width, height] = String(args.flags.viewport ?? "1280x900").split("x").map(Number);
|
|
415
|
+
try {
|
|
416
|
+
await serve({
|
|
417
|
+
name,
|
|
418
|
+
headed: args.flags.headed === true,
|
|
419
|
+
viewport: {
|
|
420
|
+
width,
|
|
421
|
+
height
|
|
422
|
+
},
|
|
423
|
+
...typeof args.flags.state === "string" ? { state: args.flags.state } : {},
|
|
424
|
+
...typeof args.flags.profile === "string" ? { profile: args.flags.profile } : {},
|
|
425
|
+
...typeof args.flags.cdp === "string" ? { cdp: args.flags.cdp } : {},
|
|
426
|
+
...args.flags["auto-connect"] === true ? { autoConnect: true } : {},
|
|
427
|
+
...typeof args.flags.tabs === "string" ? { tabs: args.flags.tabs } : {},
|
|
428
|
+
...typeof args.flags["allow-origin"] === "string" ? { allowedOrigins: args.flags["allow-origin"].split(",").map((o) => new URL(o.trim()).origin) } : {},
|
|
429
|
+
...typeof args.flags.executable === "string" ? { executable: args.flags.executable } : {},
|
|
430
|
+
...args.flags["allow-screenshot"] === true ? { allowScreenshot: true } : {}
|
|
431
|
+
});
|
|
432
|
+
} catch (e) {
|
|
433
|
+
process.stderr.write(`${e instanceof Error ? e.message : String(e)}\n`);
|
|
434
|
+
process.exit(1);
|
|
435
|
+
}
|
|
436
|
+
return;
|
|
437
|
+
}
|
|
438
|
+
if (cmd === "skill") {
|
|
439
|
+
process.stdout.write(readFileSync(new URL("../skill/SKILL.md", import.meta.url), "utf8"));
|
|
440
|
+
return;
|
|
441
|
+
}
|
|
442
|
+
const own = cmd === "help" ? args._[0] : args.flags.help ? cmd : void 0;
|
|
443
|
+
const ownHelp = own && commandHelp(own);
|
|
444
|
+
if (ownHelp) {
|
|
445
|
+
process.stdout.write(ownHelp);
|
|
446
|
+
return;
|
|
447
|
+
}
|
|
448
|
+
if (cmd === "help" && args._[0] && args._[0] !== "help") {
|
|
449
|
+
if (!Object.hasOwn(TOPICS, args._[0])) {
|
|
450
|
+
process.stderr.write(`no help on '${args._[0]}': agent-om help, agent-om help <command>, or agent-om help ${Object.keys(TOPICS).join(" | ")}\n`);
|
|
451
|
+
process.exitCode = 1;
|
|
452
|
+
return;
|
|
453
|
+
}
|
|
454
|
+
process.stdout.write(TOPICS[args._[0]].text);
|
|
455
|
+
return;
|
|
456
|
+
}
|
|
457
|
+
if (!cmd || cmd === "help" || args.flags.help) {
|
|
458
|
+
process.stdout.write(usage());
|
|
459
|
+
return;
|
|
460
|
+
}
|
|
461
|
+
let reply;
|
|
462
|
+
if (cmd === "install") reply = install(args.flags["dry-run"] === true);
|
|
463
|
+
else if (cmd === "doctor") reply = await doctor();
|
|
464
|
+
else if (cmd === "start") reply = await start(name, args, input);
|
|
465
|
+
else if (cmd === "stop" || cmd === "status") {
|
|
466
|
+
reply = await send(name, cmd, args);
|
|
467
|
+
if (!reply.ok && reply.error === "no session") reply = {
|
|
468
|
+
ok: true,
|
|
469
|
+
out: "no session running"
|
|
470
|
+
};
|
|
471
|
+
} else {
|
|
472
|
+
const variable = cmd === "fill-secret" ? typeof args.flags.text === "string" ? args._[0] : args._[1] : void 0;
|
|
473
|
+
const site = variable ? process.env[`${variable}_ORIGIN`] : void 0;
|
|
474
|
+
const env = variable ? {
|
|
475
|
+
...process.env[variable] !== void 0 ? { [variable]: process.env[variable] } : {},
|
|
476
|
+
...site !== void 0 ? { [`${variable}_ORIGIN`]: site } : {}
|
|
477
|
+
} : void 0;
|
|
478
|
+
if (cmd === "save-state" && args._[0]) args._[0] = resolve(args._[0]);
|
|
479
|
+
if (cmd === "script" && !args._.length && !process.stdin.isTTY) args._.push(await stdin());
|
|
480
|
+
reply = await send(name, cmd, args, env);
|
|
481
|
+
if (!reply.ok && reply.error === "no session") {
|
|
482
|
+
const started = await start(name, args, input);
|
|
483
|
+
if (!started.ok) reply = started;
|
|
484
|
+
else {
|
|
485
|
+
for (const option of START_OPTIONS) delete args.flags[option];
|
|
486
|
+
reply = await send(name, cmd, args, env);
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
if (reply.ok) process.stdout.write(`${reply.out}\n`);
|
|
491
|
+
else {
|
|
492
|
+
process.stderr.write(`${reply.error}\n`);
|
|
493
|
+
process.exitCode = 1;
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
// src/bin.ts
|
|
497
|
+
await main(process.argv.slice(2));
|
|
498
|
+
export {};
|