turno-browser 0.3.0 → 0.5.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/CHANGELOG.md +122 -0
- package/README.md +95 -123
- package/dist/act-model.d.ts +85 -0
- package/dist/act-model.js +117 -0
- package/dist/act.d.ts +37 -0
- package/dist/act.js +600 -0
- package/dist/api.d.ts +90 -2
- package/dist/api.js +41 -4
- package/dist/argv.d.ts +16 -2
- package/dist/argv.js +100 -3
- package/dist/background-state.d.ts +54 -0
- package/dist/background-state.js +97 -0
- package/dist/background.d.ts +69 -0
- package/dist/background.js +458 -0
- package/dist/batch.d.ts +5 -0
- package/dist/batch.js +3 -3
- package/dist/bin/mcp.js +103 -6
- package/dist/captcha.d.ts +77 -0
- package/dist/captcha.js +334 -0
- package/dist/cdp-relay.d.ts +20 -0
- package/dist/cdp-relay.js +101 -0
- package/dist/chrome-features.d.ts +23 -0
- package/dist/chrome-features.js +44 -0
- package/dist/cli/background.d.ts +11 -0
- package/dist/cli/background.js +132 -0
- package/dist/cli/commands.d.ts +16 -0
- package/dist/cli/commands.js +515 -0
- package/dist/cli/context.d.ts +75 -0
- package/dist/cli/context.js +44 -0
- package/dist/cli/do-background.d.ts +27 -0
- package/dist/cli/do-background.js +212 -0
- package/dist/cli/do.d.ts +85 -0
- package/dist/cli/do.js +558 -0
- package/dist/cli/flows.d.ts +28 -0
- package/dist/cli/flows.js +303 -0
- package/dist/cli/identity.d.ts +37 -0
- package/dist/cli/identity.js +906 -0
- package/dist/cli/live.d.ts +7 -0
- package/dist/cli/live.js +99 -0
- package/dist/cli/main.d.ts +16 -0
- package/dist/cli/main.js +171 -0
- package/dist/cli/secret.d.ts +4 -0
- package/dist/cli/secret.js +151 -0
- package/dist/cli/session.d.ts +6 -0
- package/dist/cli/session.js +106 -0
- package/dist/cli/setup.d.ts +7 -0
- package/dist/cli/setup.js +180 -0
- package/dist/cli/vault.d.ts +5 -0
- package/dist/cli/vault.js +192 -0
- package/dist/client.d.ts +82 -0
- package/dist/client.js +303 -0
- package/dist/diagnose.d.ts +7 -0
- package/dist/diagnose.js +61 -6
- package/dist/doctor.d.ts +37 -3
- package/dist/doctor.js +264 -6
- package/dist/engine-attach.d.ts +27 -0
- package/dist/engine-attach.js +165 -0
- package/dist/engine-camoufox.d.ts +32 -0
- package/dist/engine-camoufox.js +271 -0
- package/dist/engine-lightpanda.d.ts +72 -0
- package/dist/engine-lightpanda.js +293 -0
- package/dist/engine.d.ts +63 -11
- package/dist/engine.js +106 -29
- package/dist/flows.d.ts +127 -0
- package/dist/flows.js +294 -0
- package/dist/governor.d.ts +42 -7
- package/dist/governor.js +82 -32
- package/dist/health.js +2 -1
- package/dist/host.d.ts +38 -2
- package/dist/host.js +689 -52
- package/dist/human-input.d.ts +84 -0
- package/dist/human-input.js +448 -0
- package/dist/humanize.d.ts +123 -0
- package/dist/humanize.js +241 -0
- package/dist/identities.js +4 -1
- package/dist/idle-timer.d.ts +12 -0
- package/dist/idle-timer.js +25 -0
- package/dist/index.d.ts +13 -4
- package/dist/index.js +7 -1
- package/dist/lifecycle.d.ts +11 -1
- package/dist/lifecycle.js +11 -2
- package/dist/live-client.d.ts +22 -0
- package/dist/live-client.js +95 -0
- package/dist/live-page.d.ts +4 -0
- package/dist/live-page.js +144 -0
- package/dist/live-server.d.ts +173 -0
- package/dist/live-server.js +643 -0
- package/dist/live.d.ts +120 -0
- package/dist/live.js +204 -0
- package/dist/login-tool.d.ts +13 -0
- package/dist/login-tool.js +8 -3
- package/dist/login.d.ts +4 -1
- package/dist/login.js +12 -2
- package/dist/names.d.ts +28 -0
- package/dist/names.js +25 -0
- package/dist/network-chrome.d.ts +74 -0
- package/dist/network-chrome.js +178 -0
- package/dist/network.d.ts +125 -0
- package/dist/network.js +264 -0
- package/dist/opt-in-settings.d.ts +69 -0
- package/dist/opt-in-settings.js +152 -0
- package/dist/recorder.d.ts +88 -0
- package/dist/recorder.js +538 -0
- package/dist/recordings.d.ts +116 -0
- package/dist/recordings.js +474 -0
- package/dist/redact.d.ts +7 -0
- package/dist/redact.js +18 -0
- package/dist/replay.d.ts +20 -0
- package/dist/replay.js +383 -0
- package/dist/reputation.d.ts +24 -2
- package/dist/reputation.js +63 -17
- package/dist/runtime-lock.js +3 -2
- package/dist/session-export.js +1 -1
- package/dist/session.d.ts +10 -2
- package/dist/session.js +10 -2
- package/dist/settings.d.ts +104 -2
- package/dist/settings.js +204 -6
- package/dist/sharing.d.ts +96 -0
- package/dist/sharing.js +269 -0
- package/dist/snippet.d.ts +8 -2
- package/dist/snippet.js +10 -4
- package/dist/tab-locks.d.ts +63 -0
- package/dist/tab-locks.js +179 -0
- package/dist/tools.d.ts +38 -0
- package/dist/tools.js +20 -1
- package/dist/tuning.d.ts +14 -0
- package/dist/tuning.js +9 -0
- package/dist/upstream.d.ts +11 -0
- package/dist/vault.js +5 -4
- package/npm-shrinkwrap.json +6 -5
- package/package.json +13 -7
package/dist/cli/do.js
ADDED
|
@@ -0,0 +1,558 @@
|
|
|
1
|
+
import { writeFileSync } from "node:fs";
|
|
2
|
+
import { tmpdir } from "node:os";
|
|
3
|
+
import { join, resolve } from "node:path";
|
|
4
|
+
import { CliError, Exit, JSON_FLAG, bool, callArguments, columns, emit, examplesBlock, fail, jsonArg, jsonOutput, parseFlags, str, } from "turno-core/cli";
|
|
5
|
+
/**
|
|
6
|
+
* `<browser> do <id> <action>`: one browser action on an identity's browser from the shell, for
|
|
7
|
+
* coding agents that prefer commands over MCP tools. Each action is one stock (or turno) tool call
|
|
8
|
+
* to the identity's turno-browser-mcp, the server agents use, so every rule of the identity
|
|
9
|
+
* applies as it does to an agent: the tab locks or the one-session lease, attach mode, read mode's
|
|
10
|
+
* refusals, humanize, recordings, the vault. How the call reaches the server, and what makes
|
|
11
|
+
* several commands one session, is the host's transport (`DoTransport`): inside turno, mcpd.
|
|
12
|
+
*
|
|
13
|
+
* The output is short and stable: `url:` and `title:` lines, then what the action returned. The
|
|
14
|
+
* code upstream ran and the snapshot file an action saves on the server are left out; a snapshot
|
|
15
|
+
* comes from `snapshot` (or `--snapshot` after an action).
|
|
16
|
+
*/
|
|
17
|
+
/** `do --json`: one object per command. */
|
|
18
|
+
export const DO_JSON = jsonOutput([
|
|
19
|
+
"identity",
|
|
20
|
+
"server",
|
|
21
|
+
"profile",
|
|
22
|
+
"session",
|
|
23
|
+
"action",
|
|
24
|
+
"tool",
|
|
25
|
+
"ok",
|
|
26
|
+
"page",
|
|
27
|
+
"result",
|
|
28
|
+
"snapshot",
|
|
29
|
+
"file",
|
|
30
|
+
"notes",
|
|
31
|
+
"error",
|
|
32
|
+
]);
|
|
33
|
+
/** The generic paragraph about output and flags, for a transport without flags of its own. */
|
|
34
|
+
const DO_OUTPUT = `Output: url: and title: lines, then what the action returned; errors go to stderr with exit 1.
|
|
35
|
+
--snapshot after an action prints the snapshot too; --json prints one object.
|
|
36
|
+
--timeout <seconds> (default 120) bounds one action; attach mode can wait for Chrome's consent
|
|
37
|
+
dialog.`;
|
|
38
|
+
async function readStdin() {
|
|
39
|
+
const chunks = [];
|
|
40
|
+
for await (const c of process.stdin)
|
|
41
|
+
chunks.push(c);
|
|
42
|
+
return Buffer.concat(chunks).toString("utf8");
|
|
43
|
+
}
|
|
44
|
+
/** `read`: the visible text of the page, or of one element. Works in read mode too (no layout: textContent). */
|
|
45
|
+
const READ_FUNCTION = "(element) => { const node = element || document.body || document.documentElement; if (!node) return ''; const text = typeof node.innerText === 'string' ? node.innerText : node.textContent; return String(text || '').replace(/[ \\t]+\\n/g, '\\n').replace(/\\n{3,}/g, '\\n\\n').trim(); }";
|
|
46
|
+
const DEFAULT_MAX_CHARS = 20_000;
|
|
47
|
+
function int(p, name) {
|
|
48
|
+
const v = str(p, name);
|
|
49
|
+
if (v === undefined)
|
|
50
|
+
return undefined;
|
|
51
|
+
const n = Number(v);
|
|
52
|
+
if (!Number.isInteger(n) || n < 0)
|
|
53
|
+
fail(`--${name} must be a whole number, not "${v}"`);
|
|
54
|
+
return n;
|
|
55
|
+
}
|
|
56
|
+
const ACTIONS = {
|
|
57
|
+
open: {
|
|
58
|
+
synopsis: "<url>",
|
|
59
|
+
summary: "go to a URL in the session's tab",
|
|
60
|
+
min: 1,
|
|
61
|
+
max: 1,
|
|
62
|
+
shape: "page",
|
|
63
|
+
build: ([url]) => ({ tool: "browser_navigate", args: { url } }),
|
|
64
|
+
},
|
|
65
|
+
back: {
|
|
66
|
+
synopsis: "",
|
|
67
|
+
summary: "go back in the tab's history",
|
|
68
|
+
min: 0,
|
|
69
|
+
max: 0,
|
|
70
|
+
shape: "page",
|
|
71
|
+
build: () => ({ tool: "browser_navigate_back", args: {} }),
|
|
72
|
+
},
|
|
73
|
+
snapshot: {
|
|
74
|
+
synopsis: "[<ref>] [--depth <n>]",
|
|
75
|
+
summary: "the page's accessibility tree with refs (e12) to act on; <ref> limits it to one element",
|
|
76
|
+
flags: { depth: { type: "string" } },
|
|
77
|
+
min: 0,
|
|
78
|
+
max: 1,
|
|
79
|
+
shape: "snapshot",
|
|
80
|
+
build: ([ref], p) => {
|
|
81
|
+
const depth = int(p, "depth");
|
|
82
|
+
return {
|
|
83
|
+
tool: "browser_snapshot",
|
|
84
|
+
args: { ...(ref ? { target: ref } : {}), ...(depth !== undefined ? { depth } : {}) },
|
|
85
|
+
};
|
|
86
|
+
},
|
|
87
|
+
},
|
|
88
|
+
read: {
|
|
89
|
+
synopsis: "[<ref>] [--max-chars <n>]",
|
|
90
|
+
summary: `the visible text of the page or one element (first ${DEFAULT_MAX_CHARS} characters; --max-chars 0 for all)`,
|
|
91
|
+
flags: { "max-chars": { type: "string" } },
|
|
92
|
+
min: 0,
|
|
93
|
+
max: 1,
|
|
94
|
+
shape: "read",
|
|
95
|
+
build: ([ref]) => ({
|
|
96
|
+
tool: "browser_evaluate",
|
|
97
|
+
args: { function: READ_FUNCTION, ...(ref ? { target: ref } : {}) },
|
|
98
|
+
}),
|
|
99
|
+
},
|
|
100
|
+
find: {
|
|
101
|
+
synopsis: "<text>",
|
|
102
|
+
summary: "elements whose text matches, with their refs",
|
|
103
|
+
min: 1,
|
|
104
|
+
max: 1,
|
|
105
|
+
shape: "result",
|
|
106
|
+
build: ([text]) => ({ tool: "browser_find", args: { text } }),
|
|
107
|
+
},
|
|
108
|
+
click: {
|
|
109
|
+
synopsis: "<ref> [--double] [--right]",
|
|
110
|
+
summary: "click an element",
|
|
111
|
+
flags: { double: { type: "boolean" }, right: { type: "boolean" } },
|
|
112
|
+
min: 1,
|
|
113
|
+
max: 1,
|
|
114
|
+
shape: "page",
|
|
115
|
+
build: ([target], p) => ({
|
|
116
|
+
tool: "browser_click",
|
|
117
|
+
args: {
|
|
118
|
+
target,
|
|
119
|
+
...(bool(p, "double") ? { doubleClick: true } : {}),
|
|
120
|
+
...(bool(p, "right") ? { button: "right" } : {}),
|
|
121
|
+
},
|
|
122
|
+
}),
|
|
123
|
+
},
|
|
124
|
+
type: {
|
|
125
|
+
synopsis: "<ref> <text|-> [--submit] [--slowly]",
|
|
126
|
+
summary: "fill a field (- reads the text from stdin); --submit presses Enter after",
|
|
127
|
+
flags: { submit: { type: "boolean" }, slowly: { type: "boolean" } },
|
|
128
|
+
min: 2,
|
|
129
|
+
max: 2,
|
|
130
|
+
shape: "page",
|
|
131
|
+
build: async ([target, text], p, stdin) => ({
|
|
132
|
+
tool: "browser_type",
|
|
133
|
+
args: {
|
|
134
|
+
target,
|
|
135
|
+
text: text === "-" ? (await stdin()).replace(/\r?\n$/, "") : text,
|
|
136
|
+
...(bool(p, "submit") ? { submit: true } : {}),
|
|
137
|
+
...(bool(p, "slowly") ? { slowly: true } : {}),
|
|
138
|
+
},
|
|
139
|
+
}),
|
|
140
|
+
},
|
|
141
|
+
press: {
|
|
142
|
+
synopsis: "<key>",
|
|
143
|
+
summary: "press a key (Enter, Escape, ArrowDown, Control+a)",
|
|
144
|
+
min: 1,
|
|
145
|
+
max: 1,
|
|
146
|
+
shape: "page",
|
|
147
|
+
build: ([key]) => ({ tool: "browser_press_key", args: { key } }),
|
|
148
|
+
},
|
|
149
|
+
select: {
|
|
150
|
+
synopsis: "<ref> <value>...",
|
|
151
|
+
summary: "choose options of a select element",
|
|
152
|
+
min: 2,
|
|
153
|
+
max: 50,
|
|
154
|
+
shape: "page",
|
|
155
|
+
build: ([target, ...values]) => ({ tool: "browser_select_option", args: { target, values } }),
|
|
156
|
+
},
|
|
157
|
+
hover: {
|
|
158
|
+
synopsis: "<ref>",
|
|
159
|
+
summary: "move the pointer over an element",
|
|
160
|
+
min: 1,
|
|
161
|
+
max: 1,
|
|
162
|
+
shape: "page",
|
|
163
|
+
build: ([target]) => ({ tool: "browser_hover", args: { target } }),
|
|
164
|
+
},
|
|
165
|
+
wait: {
|
|
166
|
+
synopsis: "[--text <t>] [--gone <t>] [--seconds <n>]",
|
|
167
|
+
summary: "wait for text to appear or go, or for some seconds",
|
|
168
|
+
flags: { text: { type: "string" }, gone: { type: "string" }, seconds: { type: "string" } },
|
|
169
|
+
min: 0,
|
|
170
|
+
max: 0,
|
|
171
|
+
shape: "page",
|
|
172
|
+
build: (_pos, p) => {
|
|
173
|
+
const text = str(p, "text");
|
|
174
|
+
const gone = str(p, "gone");
|
|
175
|
+
const seconds = str(p, "seconds");
|
|
176
|
+
if (text === undefined && gone === undefined && seconds === undefined)
|
|
177
|
+
fail("wait needs --text, --gone or --seconds");
|
|
178
|
+
const time = seconds === undefined ? undefined : Number(seconds);
|
|
179
|
+
if (time !== undefined && !(time >= 0))
|
|
180
|
+
fail(`--seconds must be a number, not "${seconds}"`);
|
|
181
|
+
return {
|
|
182
|
+
tool: "browser_wait_for",
|
|
183
|
+
args: {
|
|
184
|
+
...(text !== undefined ? { text } : {}),
|
|
185
|
+
...(gone !== undefined ? { textGone: gone } : {}),
|
|
186
|
+
...(time !== undefined ? { time } : {}),
|
|
187
|
+
},
|
|
188
|
+
};
|
|
189
|
+
},
|
|
190
|
+
},
|
|
191
|
+
screenshot: {
|
|
192
|
+
synopsis: "[--out <file.png>] [--full]",
|
|
193
|
+
summary: "save a PNG of the tab (default: a file in the temporary folder) and print its path",
|
|
194
|
+
flags: { out: { type: "string" }, full: { type: "boolean" } },
|
|
195
|
+
min: 0,
|
|
196
|
+
max: 0,
|
|
197
|
+
shape: "screenshot",
|
|
198
|
+
build: (_pos, p) => ({
|
|
199
|
+
tool: "browser_take_screenshot",
|
|
200
|
+
args: { type: "png", scale: "css", ...(bool(p, "full") ? { fullPage: true } : {}) },
|
|
201
|
+
}),
|
|
202
|
+
},
|
|
203
|
+
tabs: {
|
|
204
|
+
synopsis: "[new [<url>] | select <n> | close <n>]",
|
|
205
|
+
summary: "the browser's tabs (others' marked in use), or open, pick or close one",
|
|
206
|
+
min: 0,
|
|
207
|
+
max: 2,
|
|
208
|
+
shape: "result",
|
|
209
|
+
build: ([what, value]) => {
|
|
210
|
+
if (what === undefined || what === "list")
|
|
211
|
+
return { tool: "browser_tabs", args: { action: "list" } };
|
|
212
|
+
if (what === "new")
|
|
213
|
+
return { tool: "browser_tabs", args: { action: "new", ...(value ? { url: value } : {}) } };
|
|
214
|
+
if (what === "select" || what === "close") {
|
|
215
|
+
const index = Number(value);
|
|
216
|
+
if (value === undefined || !Number.isInteger(index) || index < 0)
|
|
217
|
+
fail(`tabs ${what} needs a tab number from 'tabs', for example: tabs ${what} 1`);
|
|
218
|
+
return { tool: "browser_tabs", args: { action: what, index } };
|
|
219
|
+
}
|
|
220
|
+
return fail(`tabs takes new, select <n> or close <n>, not "${what}"`);
|
|
221
|
+
},
|
|
222
|
+
},
|
|
223
|
+
login: {
|
|
224
|
+
synopsis: "<site>",
|
|
225
|
+
summary: "sign in with a vault credential (<browser> vault); the value never reaches the shell",
|
|
226
|
+
min: 1,
|
|
227
|
+
max: 1,
|
|
228
|
+
shape: "result",
|
|
229
|
+
build: ([site]) => ({ tool: "browser_login", args: { site } }),
|
|
230
|
+
},
|
|
231
|
+
call: {
|
|
232
|
+
synopsis: "<tool> [--args <json>] [--arg key=value]...",
|
|
233
|
+
summary: "any tool of the identity's server, in this session (browser_batch, browser_fill_form...)",
|
|
234
|
+
flags: { args: { type: "string" }, arg: { type: "string", multiple: true } },
|
|
235
|
+
min: 1,
|
|
236
|
+
max: 1,
|
|
237
|
+
shape: "raw",
|
|
238
|
+
build: ([tool], p) => ({
|
|
239
|
+
tool: tool,
|
|
240
|
+
args: callArguments(str(p, "args"), p.values.arg ?? []),
|
|
241
|
+
}),
|
|
242
|
+
},
|
|
243
|
+
close: {
|
|
244
|
+
synopsis: "",
|
|
245
|
+
summary: "close the session's tabs (the whole browser when it serves one session) and end the session",
|
|
246
|
+
min: 0,
|
|
247
|
+
max: 0,
|
|
248
|
+
shape: "result",
|
|
249
|
+
build: () => ({ tool: "browser_close", args: {} }),
|
|
250
|
+
},
|
|
251
|
+
};
|
|
252
|
+
/** Actions that make no tool call. */
|
|
253
|
+
const LOCAL = {
|
|
254
|
+
session: { synopsis: "", summary: "which session these commands use, who owns it and how it ends" },
|
|
255
|
+
end: { synopsis: "", summary: "end the session now: its tabs stay open and free, the browser keeps running" },
|
|
256
|
+
};
|
|
257
|
+
// ---- results ---------------------------------------------------------------------------
|
|
258
|
+
const ANSI = new RegExp(`${String.fromCharCode(27)}\\[[0-9;]*m`, "g");
|
|
259
|
+
/** The sections of an upstream result text (`### Title`, then the body). */
|
|
260
|
+
function sections(text) {
|
|
261
|
+
const out = new Map();
|
|
262
|
+
const parts = text.split(/^### /m);
|
|
263
|
+
const head = parts[0].trim();
|
|
264
|
+
if (head)
|
|
265
|
+
out.set("", head);
|
|
266
|
+
for (const part of parts.slice(1)) {
|
|
267
|
+
const nl = part.indexOf("\n");
|
|
268
|
+
const title = nl < 0 ? part.trim() : part.slice(0, nl).trim();
|
|
269
|
+
out.set(title, nl < 0 ? "" : part.slice(nl + 1).trim());
|
|
270
|
+
}
|
|
271
|
+
return out;
|
|
272
|
+
}
|
|
273
|
+
function unfence(body) {
|
|
274
|
+
return body.replace(/^```[a-z]*\n/, "").replace(/\n?```$/, "");
|
|
275
|
+
}
|
|
276
|
+
function pageOf(parts) {
|
|
277
|
+
const body = parts.get("Page");
|
|
278
|
+
if (!body)
|
|
279
|
+
return null;
|
|
280
|
+
const url = /^- Page URL: (.*)$/m.exec(body)?.[1]?.trim() ?? "";
|
|
281
|
+
const title = /^- Page Title: (.*)$/m.exec(body)?.[1]?.trim() ?? "";
|
|
282
|
+
return url || title ? { url, title } : null;
|
|
283
|
+
}
|
|
284
|
+
/** Upstream's error without the wrapper names and terminal colors. */
|
|
285
|
+
function errorText(parts, whole) {
|
|
286
|
+
const raw = parts.get("Error") ?? whole;
|
|
287
|
+
return raw
|
|
288
|
+
.replace(ANSI, "")
|
|
289
|
+
.replace(/^(Error: )?browserBackend\.callTool: (Error: )?/, "")
|
|
290
|
+
.trim();
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* Notes worth a line: a pause by the user (the live view: how long, what changed while the
|
|
294
|
+
* command waited), a dialog, new tabs and downloads, what the tab governor did.
|
|
295
|
+
*/
|
|
296
|
+
function notesOf(parts) {
|
|
297
|
+
const out = [];
|
|
298
|
+
for (const title of ["Paused by the user", "Modal state", "Events", "Tab governor", "Downloads"]) {
|
|
299
|
+
const body = parts.get(title);
|
|
300
|
+
if (body)
|
|
301
|
+
out.push(...body
|
|
302
|
+
.split("\n")
|
|
303
|
+
.map((l) => l.replace(/^- /, "").trim())
|
|
304
|
+
.filter(Boolean));
|
|
305
|
+
}
|
|
306
|
+
return out;
|
|
307
|
+
}
|
|
308
|
+
function textOf(content) {
|
|
309
|
+
return content
|
|
310
|
+
.filter((c) => c.type === "text")
|
|
311
|
+
.map((c) => c.text ?? "")
|
|
312
|
+
.join("\n");
|
|
313
|
+
}
|
|
314
|
+
function render(shape, content, isError, opts) {
|
|
315
|
+
const whole = textOf(content);
|
|
316
|
+
const parts = sections(whole);
|
|
317
|
+
const base = {
|
|
318
|
+
ok: !isError,
|
|
319
|
+
page: pageOf(parts),
|
|
320
|
+
result: null,
|
|
321
|
+
snapshot: null,
|
|
322
|
+
file: null,
|
|
323
|
+
notes: notesOf(parts),
|
|
324
|
+
error: null,
|
|
325
|
+
warnings: [],
|
|
326
|
+
};
|
|
327
|
+
if (isError)
|
|
328
|
+
return { ...base, error: errorText(parts, whole) };
|
|
329
|
+
const result = parts.get("Result");
|
|
330
|
+
switch (shape) {
|
|
331
|
+
case "snapshot": {
|
|
332
|
+
const snap = parts.get("Snapshot");
|
|
333
|
+
return { ...base, snapshot: snap === undefined ? null : unfence(snap) };
|
|
334
|
+
}
|
|
335
|
+
case "read": {
|
|
336
|
+
let text = result ?? "";
|
|
337
|
+
try {
|
|
338
|
+
const v = JSON.parse(text);
|
|
339
|
+
text = typeof v === "string" ? v : JSON.stringify(v);
|
|
340
|
+
}
|
|
341
|
+
catch {
|
|
342
|
+
/* not JSON: as it came */
|
|
343
|
+
}
|
|
344
|
+
if (opts.maxChars > 0 && text.length > opts.maxChars) {
|
|
345
|
+
const full = text.length;
|
|
346
|
+
text = text.slice(0, opts.maxChars);
|
|
347
|
+
base.warnings.push(`text cut at ${opts.maxChars} of ${full} characters (--max-chars 0 for all)`);
|
|
348
|
+
}
|
|
349
|
+
return { ...base, result: text };
|
|
350
|
+
}
|
|
351
|
+
case "screenshot": {
|
|
352
|
+
const image = content.find((c) => c.type === "image" && c.data);
|
|
353
|
+
if (!image)
|
|
354
|
+
return { ...base, ok: false, error: "the server returned no image" };
|
|
355
|
+
const ext = image.mimeType === "image/jpeg" ? "jpg" : "png";
|
|
356
|
+
const file = resolve(opts.out ?? join(tmpdir(), `turno-${opts.identity}-${Date.now()}.${ext}`));
|
|
357
|
+
writeFileSync(file, Buffer.from(image.data, "base64"));
|
|
358
|
+
return { ...base, file };
|
|
359
|
+
}
|
|
360
|
+
case "raw":
|
|
361
|
+
return { ...base, result: whole.replace(ANSI, "") || null };
|
|
362
|
+
default:
|
|
363
|
+
return { ...base, result: result ?? null };
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
function printText(io, r, json, program) {
|
|
367
|
+
if (json)
|
|
368
|
+
return;
|
|
369
|
+
if (r.page) {
|
|
370
|
+
io.out(`url: ${r.page.url}`);
|
|
371
|
+
if (r.page.title)
|
|
372
|
+
io.out(`title: ${r.page.title}`);
|
|
373
|
+
}
|
|
374
|
+
if (r.result !== null && r.result !== "")
|
|
375
|
+
io.out(r.result);
|
|
376
|
+
if (r.file)
|
|
377
|
+
io.out(`file: ${r.file}`);
|
|
378
|
+
for (const n of r.notes)
|
|
379
|
+
io.out(`note: ${n}`);
|
|
380
|
+
if (r.snapshot !== null)
|
|
381
|
+
io.out(r.snapshot);
|
|
382
|
+
for (const w of r.warnings)
|
|
383
|
+
io.err(`${program}: ${w}`);
|
|
384
|
+
if (r.error)
|
|
385
|
+
io.err(`error: ${r.error}`);
|
|
386
|
+
}
|
|
387
|
+
// ---- help --------------------------------------------------------------------------------
|
|
388
|
+
const COMMON = {
|
|
389
|
+
session: { type: "string" },
|
|
390
|
+
timeout: { type: "string" },
|
|
391
|
+
snapshot: { type: "boolean" },
|
|
392
|
+
...JSON_FLAG,
|
|
393
|
+
};
|
|
394
|
+
/** Every action name, for the short form and for completion. */
|
|
395
|
+
export const ACTION_NAMES = [...Object.keys(ACTIONS), ...Object.keys(LOCAL)];
|
|
396
|
+
/** The flags of `do`, for completion: the common ones, the transport's, then the actions'. */
|
|
397
|
+
export function doFlagNames(c) {
|
|
398
|
+
return [
|
|
399
|
+
"--session",
|
|
400
|
+
...Object.keys(c.host.doTransport?.flags ?? {}).map((f) => `--${f}`),
|
|
401
|
+
"--timeout",
|
|
402
|
+
"--snapshot",
|
|
403
|
+
"--json",
|
|
404
|
+
...new Set(Object.values(ACTIONS)
|
|
405
|
+
.flatMap((a) => Object.keys(a.flags ?? {}))
|
|
406
|
+
.map((f) => `--${f}`)),
|
|
407
|
+
];
|
|
408
|
+
}
|
|
409
|
+
export function doSynopsis(_c) {
|
|
410
|
+
return "<id> <action> [args] [--session <name>] [--json]";
|
|
411
|
+
}
|
|
412
|
+
/** An action's summary, with the host's command names. */
|
|
413
|
+
function summaryOf(c, summary) {
|
|
414
|
+
return summary.replace("<browser>", c.names.browser);
|
|
415
|
+
}
|
|
416
|
+
/** What `do` says when the host gives no way to reach the identity's server. */
|
|
417
|
+
function noTransport(c) {
|
|
418
|
+
return `${c.names.browser} do cannot reach a browser here: run \`turno-browser do <id> <action>\` (through the identity's background server) or \`turno browser do <id> <action>\` (through turno's gateway).`;
|
|
419
|
+
}
|
|
420
|
+
export function doDetails(c) {
|
|
421
|
+
const t = c.host.doTransport;
|
|
422
|
+
const actions = `Actions:
|
|
423
|
+
${columns([
|
|
424
|
+
...Object.entries(ACTIONS).map(([n, a]) => [`${n}${a.synopsis ? ` ${a.synopsis}` : ""}`, summaryOf(c, a.summary)]),
|
|
425
|
+
...Object.entries(LOCAL).map(([n, a]) => [`${n}${a.synopsis ? ` ${a.synopsis}` : ""}`, a.summary]),
|
|
426
|
+
], 2, 40)}
|
|
427
|
+
|
|
428
|
+
Refs (e12) come from the last snapshot; take a new one after the page changes.`;
|
|
429
|
+
if (!t)
|
|
430
|
+
return `${noTransport(c)}\n\n${actions}\n\n${DO_OUTPUT}`;
|
|
431
|
+
return `${t.help.intro}\n\n${actions}\n\n${t.help.session}\n\n${t.help.output}`;
|
|
432
|
+
}
|
|
433
|
+
function doExamples(c) {
|
|
434
|
+
const b = c.names.browser;
|
|
435
|
+
return [
|
|
436
|
+
`${b} do school open https://example.com`,
|
|
437
|
+
`${b} do school snapshot`,
|
|
438
|
+
`${b} do school click e12`,
|
|
439
|
+
`${b} do school type e5 'alice@example.com' --submit`,
|
|
440
|
+
`${b} do school read`,
|
|
441
|
+
`${b} do school screenshot --out page.png`,
|
|
442
|
+
`${b} do school end`,
|
|
443
|
+
];
|
|
444
|
+
}
|
|
445
|
+
function doUsage(c) {
|
|
446
|
+
return `Usage:\n ${c.names.browser} do ${doSynopsis(c)}\n\n${doDetails(c)}\n\n${examplesBlock(doExamples(c))}`;
|
|
447
|
+
}
|
|
448
|
+
function actionUsage(c, name) {
|
|
449
|
+
const a = ACTIONS[name] ?? LOCAL[name];
|
|
450
|
+
// The action is the word after `<browser> do <id>`.
|
|
451
|
+
const at = c.names.browser.split(" ").length + 2;
|
|
452
|
+
const mine = doExamples(c).filter((e) => e.split(" ")[at] === name);
|
|
453
|
+
return `Usage:\n ${c.names.browser} do <id> ${name}${a.synopsis ? ` ${a.synopsis}` : ""} [--session <name>] [--json]\n\n${summaryOf(c, a.summary)}\n\n${examplesBlock(mine.length ? mine : [`${c.names.browser} do school ${name}`])}`;
|
|
454
|
+
}
|
|
455
|
+
// ---- the command -----------------------------------------------------------------------
|
|
456
|
+
const IDENTITY_ID = /^[a-z0-9][a-z0-9_-]*$/;
|
|
457
|
+
/** `<browser> do <id> <action> [args]`. */
|
|
458
|
+
export async function browserDo(c, args, io) {
|
|
459
|
+
const b = c.names.browser;
|
|
460
|
+
const [id, action, ...rest] = args;
|
|
461
|
+
if (id === undefined || id === "--help" || id === "-h" || id === "help") {
|
|
462
|
+
io.out(doUsage(c));
|
|
463
|
+
return Exit.ok;
|
|
464
|
+
}
|
|
465
|
+
const lister = c.names.subject ? `${b} identities` : `${b} list`;
|
|
466
|
+
if (!IDENTITY_ID.test(id) || id.length > 64)
|
|
467
|
+
return fail(`invalid identity "${id}": use lowercase letters, digits, "-" and "_" (${lister} lists them)`);
|
|
468
|
+
if (action === undefined || action.startsWith("-")) {
|
|
469
|
+
if (action === "--help" || action === "-h") {
|
|
470
|
+
io.out(doUsage(c));
|
|
471
|
+
return Exit.ok;
|
|
472
|
+
}
|
|
473
|
+
return fail(`${c.names.subject}do needs an action: ${b} do ${id} <action> (${ACTION_NAMES.join(", ")})`);
|
|
474
|
+
}
|
|
475
|
+
const act = ACTIONS[action];
|
|
476
|
+
if (!act && !LOCAL[action]) {
|
|
477
|
+
return fail(`unknown action "${action}" (actions: ${ACTION_NAMES.join(", ")}; ${b} do --help)`);
|
|
478
|
+
}
|
|
479
|
+
if (rest.includes("--help") || rest.includes("-h")) {
|
|
480
|
+
io.out(actionUsage(c, action));
|
|
481
|
+
return Exit.ok;
|
|
482
|
+
}
|
|
483
|
+
const transport = c.host.doTransport;
|
|
484
|
+
if (!transport)
|
|
485
|
+
throw new CliError(noTransport(c));
|
|
486
|
+
const p = parseFlags(rest, { ...COMMON, ...transport.flags, ...(act?.flags ?? {}) }, act ? act.max : 0);
|
|
487
|
+
if (act && p.positionals.length < act.min)
|
|
488
|
+
return fail(`usage: ${b} do ${id} ${action} ${act.synopsis}`.trimEnd());
|
|
489
|
+
const json = jsonArg(rest) !== undefined;
|
|
490
|
+
const timeoutFlag = str(p, "timeout");
|
|
491
|
+
const timeoutS = timeoutFlag === undefined ? 120 : Number(timeoutFlag);
|
|
492
|
+
if (!(timeoutS > 0))
|
|
493
|
+
return fail(`--timeout takes seconds, not "${timeoutFlag}"`);
|
|
494
|
+
const maxChars = int(p, "max-chars") ?? DEFAULT_MAX_CHARS;
|
|
495
|
+
// Build the call before anything connects: a bad argument opens no session.
|
|
496
|
+
const call = act ? await act.build(p.positionals, p, c.deps.stdin ?? readStdin) : undefined;
|
|
497
|
+
const t = await transport.target(id, p, io);
|
|
498
|
+
const head = t.head;
|
|
499
|
+
const blank = { tool: null, ok: true, page: null, snapshot: null, file: null, notes: [], error: null };
|
|
500
|
+
if (action === "session") {
|
|
501
|
+
const { open, lines } = t.describe(io);
|
|
502
|
+
if (json) {
|
|
503
|
+
emit(io, p, DO_JSON, { ...head, session: t.info(open), action, ...blank, result: open ? "open" : "none" });
|
|
504
|
+
return Exit.ok;
|
|
505
|
+
}
|
|
506
|
+
for (const l of lines)
|
|
507
|
+
io.out(l);
|
|
508
|
+
return Exit.ok;
|
|
509
|
+
}
|
|
510
|
+
if (action === "end") {
|
|
511
|
+
const ended = await t.end();
|
|
512
|
+
const { name } = t.info(null);
|
|
513
|
+
if (json)
|
|
514
|
+
emit(io, p, DO_JSON, { ...head, session: t.info(null), action, ...blank, result: ended ? "ended" : "none" });
|
|
515
|
+
else
|
|
516
|
+
io.out(ended ? `ended session ${name} of ${head.identity}` : `no session ${name} of ${head.identity} to end`);
|
|
517
|
+
return Exit.ok;
|
|
518
|
+
}
|
|
519
|
+
const cl = call;
|
|
520
|
+
const timeout = timeoutS * 1000;
|
|
521
|
+
return await t.session(async (s) => {
|
|
522
|
+
const result = await s.call(cl.tool, cl.args, timeout);
|
|
523
|
+
const closing = action === "close" && result.isError !== true;
|
|
524
|
+
if (closing)
|
|
525
|
+
await s.end();
|
|
526
|
+
const content = (Array.isArray(result.content) ? result.content : []);
|
|
527
|
+
const r = render(act.shape, content, result.isError === true, {
|
|
528
|
+
maxChars,
|
|
529
|
+
out: str(p, "out"),
|
|
530
|
+
identity: id,
|
|
531
|
+
});
|
|
532
|
+
if (r.ok && bool(p, "snapshot") && act.shape === "page" && !closing) {
|
|
533
|
+
const snap = await s.call("browser_snapshot", {}, timeout).catch(() => undefined);
|
|
534
|
+
const sc = (Array.isArray(snap?.content) ? snap.content : []);
|
|
535
|
+
const sr = render("snapshot", sc, snap?.isError === true, { maxChars, identity: id });
|
|
536
|
+
r.snapshot = sr.snapshot;
|
|
537
|
+
if (sr.page)
|
|
538
|
+
r.page = sr.page;
|
|
539
|
+
}
|
|
540
|
+
if (json)
|
|
541
|
+
emit(io, p, DO_JSON, {
|
|
542
|
+
...head,
|
|
543
|
+
session: t.info(s.resumed),
|
|
544
|
+
action,
|
|
545
|
+
tool: cl.tool,
|
|
546
|
+
ok: r.ok,
|
|
547
|
+
page: r.page,
|
|
548
|
+
result: r.result,
|
|
549
|
+
snapshot: r.snapshot,
|
|
550
|
+
file: r.file,
|
|
551
|
+
notes: r.notes,
|
|
552
|
+
error: r.error,
|
|
553
|
+
});
|
|
554
|
+
printText(io, r, json, c.names.program);
|
|
555
|
+
return r.ok ? Exit.ok : Exit.failure;
|
|
556
|
+
});
|
|
557
|
+
}
|
|
558
|
+
//# sourceMappingURL=do.js.map
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { type Io } from "turno-core/cli";
|
|
2
|
+
import type { Ctx } from "./context.js";
|
|
3
|
+
/**
|
|
4
|
+
* `flows|replay|traces|record`: the saved flows of an identity, running one
|
|
5
|
+
* outside an agent, its recordings (traces and screenshots), and what it records. The flows and
|
|
6
|
+
* recordings themselves are turno-browser's (`flows.ts`, `recordings.ts`); this file only shows
|
|
7
|
+
* and drives them.
|
|
8
|
+
*/
|
|
9
|
+
/** `flows <id> --json`: one element per flow. */
|
|
10
|
+
export declare const FLOWS_JSON: import("turno-core/cli").JsonOutput;
|
|
11
|
+
/** `replay --json`. */
|
|
12
|
+
export declare const REPLAY_JSON: import("turno-core/cli").JsonOutput;
|
|
13
|
+
/** `traces <id> --json`: one element per recording. */
|
|
14
|
+
export declare const TRACES_JSON: import("turno-core/cli").JsonOutput;
|
|
15
|
+
/** `record <id> --json`. */
|
|
16
|
+
export declare const RECORD_JSON: import("turno-core/cli").JsonOutput;
|
|
17
|
+
/** The identity a subcommand names, which must exist. */
|
|
18
|
+
export declare function needIdentity(c: Ctx, id: string | undefined, sub: string, io: Io): string;
|
|
19
|
+
export declare function flows(c: Ctx, args: string[], io: Io): number;
|
|
20
|
+
/**
|
|
21
|
+
* Runs a saved flow through turno-browser-mcp on the identity. A sensitive parameter on the
|
|
22
|
+
* command line must be a `keychain:<item>` reference (a value would sit in the shell history and
|
|
23
|
+
* the process list); on a terminal a missing one is asked for, hidden.
|
|
24
|
+
*/
|
|
25
|
+
export declare function replay(c: Ctx, args: string[], io: Io): Promise<number>;
|
|
26
|
+
export declare function traces(c: Ctx, args: string[], io: Io): Promise<number>;
|
|
27
|
+
/** `record <id>`: what the identity records, and turning it on or off. */
|
|
28
|
+
export declare function record(c: Ctx, args: string[], io: Io): number;
|