turno-cli 0.4.0 → 0.5.1
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 +36 -0
- package/README.md +2 -2
- package/dist/cli/args.js +2 -78
- package/dist/cli/commands/browser-do-session.js +16 -117
- package/dist/cli/commands/browser-do.js +99 -598
- package/dist/cli/commands/browser.js +130 -440
- package/dist/cli/commands/completion.js +3 -37
- package/dist/cli/commands/identity.js +101 -96
- package/dist/cli/commands/mcp-call.js +2 -38
- package/dist/cli/commands/mcpd.js +8 -0
- package/dist/cli/exit.js +2 -64
- package/dist/cli/group.js +2 -61
- package/dist/cli/highlight.js +2 -235
- package/dist/cli/hints.js +5 -12
- package/dist/cli/io.js +2 -206
- package/dist/cli/output.js +2 -122
- package/dist/cli/registry.js +3 -2
- package/dist/cli/secret-input.js +2 -62
- package/dist/cli/style.js +2 -289
- package/dist/cli/topics.js +4 -0
- package/dist/core/paths.js +2 -9
- package/dist/mcpd/daemon.js +13 -0
- package/npm-shrinkwrap.json +5 -5
- package/package.json +3 -3
- package/dist/cli/commands/browser-flows.js +0 -300
- package/dist/cli/commands/browser-identity.js +0 -952
- package/dist/cli/commands/browser-live.js +0 -118
- package/dist/cli/commands/browser-session.js +0 -102
- package/dist/cli/commands/browser-vault.js +0 -193
|
@@ -1,300 +0,0 @@
|
|
|
1
|
-
import { tildify } from "../../core/paths.js";
|
|
2
|
-
import { DRY_RUN, JSON_FLAG, bool, parseFlags, str } from "../args.js";
|
|
3
|
-
import { Exit } from "../exit.js";
|
|
4
|
-
import { fail, needsAnswer, noInputReason } from "../io.js";
|
|
5
|
-
import { emit, jsonOutput } from "../output.js";
|
|
6
|
-
import { style } from "../style.js";
|
|
7
|
-
import { describeRecord } from "./browser-identity.js";
|
|
8
|
-
import { terminalInput } from "./browser-vault.js";
|
|
9
|
-
/**
|
|
10
|
-
* `turno browser flows|replay|traces|record`: the saved flows of an identity, running one
|
|
11
|
-
* outside an agent, its recordings (traces and screenshots), and what it records. The flows and
|
|
12
|
-
* recordings themselves are turno-browser's (`flows.ts`, `recordings.ts`); this file only shows
|
|
13
|
-
* and drives them.
|
|
14
|
-
*/
|
|
15
|
-
/** `turno browser flows <id> --json`: one element per flow. */
|
|
16
|
-
export const FLOWS_JSON = jsonOutput(["name", "description", "steps", "params", "start", "auto", "updated", "file"]);
|
|
17
|
-
/** `turno browser replay --json`. */
|
|
18
|
-
export const REPLAY_JSON = jsonOutput(["identity", "flow", "ok", "report"]);
|
|
19
|
-
/** `turno browser traces <id> --json`: one element per recording. */
|
|
20
|
-
export const TRACES_JSON = jsonOutput([
|
|
21
|
-
"name",
|
|
22
|
-
"dir",
|
|
23
|
-
"title",
|
|
24
|
-
"started",
|
|
25
|
-
"ended",
|
|
26
|
-
"steps",
|
|
27
|
-
"screenshots",
|
|
28
|
-
"traces",
|
|
29
|
-
"bytes",
|
|
30
|
-
"notes",
|
|
31
|
-
]);
|
|
32
|
-
/** `turno browser record <id> --json`. */
|
|
33
|
-
export const RECORD_JSON = jsonOutput(["id", "dryRun", "record", "flowsDir", "recordingsDir"]);
|
|
34
|
-
function needIdentity(mod, id, sub, io) {
|
|
35
|
-
if (!id)
|
|
36
|
-
return fail(`browser ${sub} needs an identity id (for example: turno browser ${sub} school)`);
|
|
37
|
-
mod.assertIdentityId(id);
|
|
38
|
-
if (!mod.identityExists(id, io.env))
|
|
39
|
-
return fail(`identity "${id}" does not exist (turno identity add browser:${id})`);
|
|
40
|
-
return id;
|
|
41
|
-
}
|
|
42
|
-
function size(bytes) {
|
|
43
|
-
if (bytes < 1024)
|
|
44
|
-
return `${bytes} B`;
|
|
45
|
-
if (bytes < 1024 ** 2)
|
|
46
|
-
return `${(bytes / 1024).toFixed(0)} KB`;
|
|
47
|
-
return `${(bytes / 1024 ** 2).toFixed(1)} MB`;
|
|
48
|
-
}
|
|
49
|
-
const FLOWS_FLAGS = { remove: { type: "boolean" }, ...DRY_RUN, ...JSON_FLAG };
|
|
50
|
-
/** A flow's steps in a few words each (the package's own wording is in the tool; this mirrors it). */
|
|
51
|
-
function stepLine(step) {
|
|
52
|
-
const t = step.targets?.target ?? step.targets?.startTarget;
|
|
53
|
-
const a = step.args;
|
|
54
|
-
if (step.tool === "browser_login")
|
|
55
|
-
return `browser_login site "${String(a.site)}"`;
|
|
56
|
-
if (step.tool === "browser_replay")
|
|
57
|
-
return `browser_replay flow "${String(a.flow)}"`;
|
|
58
|
-
const what = t ? (t.description ?? t.locators[0]?.selector) : (a.url ?? a.key ?? a.action ?? a.time);
|
|
59
|
-
const text = a.text;
|
|
60
|
-
return `${step.tool}${what === undefined ? "" : ` ${String(what)}`}${text?.$param ? ` with {${text.$param}}` : ""}`;
|
|
61
|
-
}
|
|
62
|
-
export function flows(mod, args, io) {
|
|
63
|
-
const p = parseFlags(args, FLOWS_FLAGS, 2);
|
|
64
|
-
const id = needIdentity(mod, p.positionals[0], "flows", io);
|
|
65
|
-
const name = p.positionals[1];
|
|
66
|
-
if (bool(p, "remove")) {
|
|
67
|
-
if (!name)
|
|
68
|
-
return fail("--remove needs a flow name: turno browser flows <id> <flow> --remove");
|
|
69
|
-
mod.readFlow(id, name, io.env);
|
|
70
|
-
if (bool(p, "dry-run")) {
|
|
71
|
-
io.out(`(dry run) would delete flow "${name}" of "${id}"`);
|
|
72
|
-
return Exit.ok;
|
|
73
|
-
}
|
|
74
|
-
mod.removeFlow(id, name, io.env);
|
|
75
|
-
io.out(`deleted flow "${name}" of "${id}"`);
|
|
76
|
-
return Exit.ok;
|
|
77
|
-
}
|
|
78
|
-
const s = style(io);
|
|
79
|
-
if (name) {
|
|
80
|
-
const flow = mod.readFlow(id, name, io.env);
|
|
81
|
-
if (bool(p, "json")) {
|
|
82
|
-
io.out(JSON.stringify({ schemaVersion: 1, ...flow }, null, 2));
|
|
83
|
-
return Exit.ok;
|
|
84
|
-
}
|
|
85
|
-
io.out(s.bold(`${flow.name}`) + (flow.description ? ` ${s.muted(flow.description)}` : ""));
|
|
86
|
-
for (const [i, step] of flow.steps.entries())
|
|
87
|
-
io.out(` ${String(i + 1).padStart(2)}. ${stepLine(step)}`);
|
|
88
|
-
if (flow.params.length) {
|
|
89
|
-
io.out("");
|
|
90
|
-
io.out(s.header("Parameters"));
|
|
91
|
-
for (const param of flow.params)
|
|
92
|
-
io.out(` ${param.name} ${s.muted(param.sensitive ? "sensitive: give it at replay (keychain:<item>)" : param.default !== undefined ? "has a default" : "required")}`);
|
|
93
|
-
}
|
|
94
|
-
return Exit.ok;
|
|
95
|
-
}
|
|
96
|
-
const list = mod.listFlows(id, io.env);
|
|
97
|
-
if (bool(p, "json")) {
|
|
98
|
-
emit(io, p, FLOWS_JSON, list.map((f) => ({ ...f, file: tildify(f.file, io.env) })));
|
|
99
|
-
return Exit.ok;
|
|
100
|
-
}
|
|
101
|
-
if (!list.length) {
|
|
102
|
-
io.out(`No saved flows for "${id}". An agent records one with the browser_record tool.`);
|
|
103
|
-
return Exit.ok;
|
|
104
|
-
}
|
|
105
|
-
const head = ["flow", "steps", "params", "updated"].map((h) => s.muted(h));
|
|
106
|
-
const rows = list.map((f) => [
|
|
107
|
-
f.auto ? s.muted(f.name) : f.name,
|
|
108
|
-
String(f.steps),
|
|
109
|
-
f.params.map((x) => `${x.name}${x.sensitive ? "*" : ""}`).join(", ") || "-",
|
|
110
|
-
f.updated.slice(0, 16).replace("T", " "),
|
|
111
|
-
]);
|
|
112
|
-
for (const l of s.table([head, ...rows], { columns: [{}, { align: "right" }, {}, { drop: 1 }] }))
|
|
113
|
-
io.out(l);
|
|
114
|
-
if (list.some((f) => f.params.some((x) => x.sensitive)))
|
|
115
|
-
io.out(s.muted("* sensitive: no value is saved"));
|
|
116
|
-
return Exit.ok;
|
|
117
|
-
}
|
|
118
|
-
const REPLAY_FLAGS = {
|
|
119
|
-
param: { type: "string", multiple: true },
|
|
120
|
-
from: { type: "string" },
|
|
121
|
-
headless: { type: "boolean" },
|
|
122
|
-
...JSON_FLAG,
|
|
123
|
-
};
|
|
124
|
-
/**
|
|
125
|
-
* Runs a saved flow through turno-browser-mcp on the identity. A sensitive parameter on the
|
|
126
|
-
* command line must be a `keychain:<item>` reference (a value would sit in the shell history and
|
|
127
|
-
* the process list); on a terminal a missing one is asked for, hidden.
|
|
128
|
-
*/
|
|
129
|
-
export async function replay(mod, args, io, input = terminalInput) {
|
|
130
|
-
const p = parseFlags(args, REPLAY_FLAGS, 2);
|
|
131
|
-
const id = needIdentity(mod, p.positionals[0], "replay", io);
|
|
132
|
-
const name = p.positionals[1];
|
|
133
|
-
if (!name)
|
|
134
|
-
return fail("usage: turno browser replay <id> <flow> [--param name=value]... [--from <step>] [--headless]");
|
|
135
|
-
const flow = mod.readFlow(id, name, io.env);
|
|
136
|
-
const params = {};
|
|
137
|
-
for (const pair of p.values.param ?? []) {
|
|
138
|
-
const eq = pair.indexOf("=");
|
|
139
|
-
if (eq <= 0)
|
|
140
|
-
return fail(`--param takes name=value, not "${pair}"`);
|
|
141
|
-
params[pair.slice(0, eq)] = pair.slice(eq + 1);
|
|
142
|
-
}
|
|
143
|
-
for (const param of flow.params) {
|
|
144
|
-
const given = params[param.name];
|
|
145
|
-
if (param.sensitive && given !== undefined && !given.startsWith("keychain:"))
|
|
146
|
-
return fail(`"${param.name}" is sensitive: pass it as --param ${param.name}=keychain:<item> (turno secret set <item>), not as a value on the command line`);
|
|
147
|
-
if (param.sensitive && given === undefined) {
|
|
148
|
-
if (!input.isTty() || noInputReason(io.env))
|
|
149
|
-
needsAnswer(`browser replay (parameter ${param.name})`, `--param ${param.name}=keychain:<item>`);
|
|
150
|
-
params[param.name] = await input.hidden(`${param.name} (input hidden): `);
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
const from = str(p, "from");
|
|
154
|
-
if (from !== undefined && !/^\d+$/.test(from))
|
|
155
|
-
return fail(`--from takes a step number, not "${from}"`);
|
|
156
|
-
const holder = mod.inUseBy(id, io.env);
|
|
157
|
-
if (holder)
|
|
158
|
-
return fail(holder);
|
|
159
|
-
const result = await mod.replayFlow(id, name, {
|
|
160
|
-
params,
|
|
161
|
-
...(from ? { from: Number(from) } : {}),
|
|
162
|
-
headless: bool(p, "headless"),
|
|
163
|
-
env: io.env,
|
|
164
|
-
});
|
|
165
|
-
if (bool(p, "json")) {
|
|
166
|
-
emit(io, p, REPLAY_JSON, { identity: `browser:${id}`, flow: name, ok: result.ok, report: result.text });
|
|
167
|
-
return result.ok ? Exit.ok : Exit.failure;
|
|
168
|
-
}
|
|
169
|
-
const text = result.text.replace(/^### /gm, "");
|
|
170
|
-
if (result.ok)
|
|
171
|
-
io.out(text);
|
|
172
|
-
else
|
|
173
|
-
io.err(text);
|
|
174
|
-
return result.ok ? Exit.ok : Exit.failure;
|
|
175
|
-
}
|
|
176
|
-
const TRACES_FLAGS = { open: { type: "boolean" }, segment: { type: "string" }, ...JSON_FLAG };
|
|
177
|
-
export async function traces(mod, args, io) {
|
|
178
|
-
const p = parseFlags(args, TRACES_FLAGS, 2);
|
|
179
|
-
const id = needIdentity(mod, p.positionals[0], "traces", io);
|
|
180
|
-
const wanted = p.positionals[1];
|
|
181
|
-
const list = mod.listRecordings(id, io.env);
|
|
182
|
-
const s = style(io);
|
|
183
|
-
if (bool(p, "open")) {
|
|
184
|
-
const rec = wanted ? list.find((r) => r.name === wanted) : list.find((r) => r.traces.length);
|
|
185
|
-
if (!rec)
|
|
186
|
-
return fail(wanted ? `identity "${id}" has no recording "${wanted}"` : `identity "${id}" has no traces yet`);
|
|
187
|
-
if (!rec.traces.length)
|
|
188
|
-
return fail(`recording "${rec.name}" has no trace (screenshots are in ${tildify(rec.dir, io.env)})`);
|
|
189
|
-
const segment = str(p, "segment");
|
|
190
|
-
const n = segment === undefined ? rec.traces.length : Number(segment);
|
|
191
|
-
const trace = rec.traces[n - 1];
|
|
192
|
-
if (!trace || !Number.isInteger(n))
|
|
193
|
-
return fail(`--segment takes 1 to ${rec.traces.length} for recording "${rec.name}"`);
|
|
194
|
-
const { command, args: viewer } = mod.traceViewerCommand(trace.file);
|
|
195
|
-
io.err(s.muted(`opening ${tildify(trace.file, io.env)} in the trace viewer; press Ctrl-C to stop it`));
|
|
196
|
-
io.err(s.muted(`(the same by hand: ${[command, ...viewer].map((a) => (/\s/.test(a) ? `"${a}"` : a)).join(" ")})`));
|
|
197
|
-
return (await mod.openTraceViewer(trace.file, io.env)) === 0 ? Exit.ok : Exit.failure;
|
|
198
|
-
}
|
|
199
|
-
const shown = wanted ? list.filter((r) => r.name === wanted) : list;
|
|
200
|
-
if (wanted && !shown.length)
|
|
201
|
-
return fail(`identity "${id}" has no recording "${wanted}"`);
|
|
202
|
-
if (bool(p, "json")) {
|
|
203
|
-
emit(io, p, TRACES_JSON, shown.map((r) => ({
|
|
204
|
-
...r,
|
|
205
|
-
dir: tildify(r.dir, io.env),
|
|
206
|
-
ended: r.ended ?? null,
|
|
207
|
-
traces: r.traces.map((t) => ({ ...t, file: tildify(t.file, io.env) })),
|
|
208
|
-
})));
|
|
209
|
-
return Exit.ok;
|
|
210
|
-
}
|
|
211
|
-
if (!shown.length) {
|
|
212
|
-
io.out(`No recordings for "${id}". Turn them on with: turno browser record ${id} --traces --screenshots (they start with the next Chrome)`);
|
|
213
|
-
return Exit.ok;
|
|
214
|
-
}
|
|
215
|
-
for (const [i, r] of shown.entries()) {
|
|
216
|
-
if (i)
|
|
217
|
-
io.out("");
|
|
218
|
-
io.out(`${s.bold(r.name)} ${s.muted(`${r.title}, ${r.started.slice(0, 19).replace("T", " ")}${r.ended ? "" : " (open or cut short)"}`)}`);
|
|
219
|
-
io.out(` ${r.steps} step${r.steps === 1 ? "" : "s"}, ${r.screenshots} screenshot${r.screenshots === 1 ? "" : "s"}, ${r.traces.length} trace${r.traces.length === 1 ? "" : "s"}, ${size(r.bytes)} ${s.muted(tildify(r.dir, io.env))}`);
|
|
220
|
-
for (const [n, t] of r.traces.entries())
|
|
221
|
-
io.out(` ${s.muted(`trace ${n + 1}:`)} ${tildify(t.file, io.env)} ${s.muted(`(steps ${t.from}-${t.to}, ${size(t.bytes)})`)}`);
|
|
222
|
-
for (const note of r.notes)
|
|
223
|
-
io.out(s.note("info", note, 2));
|
|
224
|
-
}
|
|
225
|
-
io.out("");
|
|
226
|
-
io.out(s.muted(`Open a trace: turno browser traces ${id} [<recording>] --open [--segment <n>]`));
|
|
227
|
-
return Exit.ok;
|
|
228
|
-
}
|
|
229
|
-
const RECORD_FLAGS = {
|
|
230
|
-
flows: { type: "boolean" },
|
|
231
|
-
traces: { type: "boolean" },
|
|
232
|
-
screenshots: { type: "boolean" },
|
|
233
|
-
"max-size": { type: "string" },
|
|
234
|
-
off: { type: "boolean" },
|
|
235
|
-
...DRY_RUN,
|
|
236
|
-
...JSON_FLAG,
|
|
237
|
-
};
|
|
238
|
-
/** `turno browser record <id>`: what the identity records, and turning it on or off. */
|
|
239
|
-
export function record(mod, args, io) {
|
|
240
|
-
const p = parseFlags(args, RECORD_FLAGS, 1);
|
|
241
|
-
const id = needIdentity(mod, p.positionals[0], "record", io);
|
|
242
|
-
const current = mod.checkIdentitySettings(id, io.env).settings?.record;
|
|
243
|
-
const on = ["flows", "traces", "screenshots"].filter((k) => bool(p, k));
|
|
244
|
-
const maxSize = str(p, "max-size");
|
|
245
|
-
const off = bool(p, "off");
|
|
246
|
-
if (off && (on.length || maxSize !== undefined))
|
|
247
|
-
return fail("--off does not go with --flows, --traces, --screenshots or --max-size");
|
|
248
|
-
const dryRun = bool(p, "dry-run");
|
|
249
|
-
let next = current;
|
|
250
|
-
if (off || on.length || maxSize !== undefined) {
|
|
251
|
-
const patch = off
|
|
252
|
-
? { record: null }
|
|
253
|
-
: {
|
|
254
|
-
record: {
|
|
255
|
-
...current,
|
|
256
|
-
...Object.fromEntries(on.map((k) => [k, true])),
|
|
257
|
-
...(maxSize !== undefined ? { maxSize } : {}),
|
|
258
|
-
},
|
|
259
|
-
};
|
|
260
|
-
if (!dryRun) {
|
|
261
|
-
try {
|
|
262
|
-
next = mod.writeIdentitySettings(id, patch, io.env).record;
|
|
263
|
-
}
|
|
264
|
-
catch (err) {
|
|
265
|
-
if (err.name === "IdentityError")
|
|
266
|
-
return fail(err.message.replace(/^identity "[^"]*": /, "").replace(/record\.maxSize/, "--max-size"));
|
|
267
|
-
throw err;
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
else
|
|
271
|
-
next = patch.record ?? undefined;
|
|
272
|
-
}
|
|
273
|
-
const flowsDir = mod.flowsDir(id, io.env);
|
|
274
|
-
const recordingsDir = mod.recordingsDir(id, io.env);
|
|
275
|
-
if (bool(p, "json")) {
|
|
276
|
-
emit(io, p, RECORD_JSON, {
|
|
277
|
-
id: `browser:${id}`,
|
|
278
|
-
dryRun,
|
|
279
|
-
record: next ?? null,
|
|
280
|
-
flowsDir: tildify(flowsDir, io.env),
|
|
281
|
-
recordingsDir: tildify(recordingsDir, io.env),
|
|
282
|
-
});
|
|
283
|
-
return Exit.ok;
|
|
284
|
-
}
|
|
285
|
-
const s = style(io);
|
|
286
|
-
if (dryRun)
|
|
287
|
-
io.out(`(dry run) would set record of "${id}" to ${JSON.stringify(next ?? null)}`);
|
|
288
|
-
else if (off || on.length || maxSize !== undefined)
|
|
289
|
-
io.out(`updated identity.json of "${id}"; the next Chrome start of browser-${id} uses it`);
|
|
290
|
-
const rows = [
|
|
291
|
-
["records", describeRecord(next) ?? s.muted("nothing (the agent can still record a flow with browser_record)")],
|
|
292
|
-
["flows", tildify(flowsDir, io.env)],
|
|
293
|
-
["recordings", tildify(recordingsDir, io.env)],
|
|
294
|
-
];
|
|
295
|
-
const width = Math.max(...rows.map(([k]) => k.length));
|
|
296
|
-
for (const [k, v] of rows)
|
|
297
|
-
io.out(s.kv(k, v, width));
|
|
298
|
-
return Exit.ok;
|
|
299
|
-
}
|
|
300
|
-
//# sourceMappingURL=browser-flows.js.map
|