turno-cli 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 +140 -0
- package/README.md +9 -5
- package/dist/cli/args.js +2 -78
- package/dist/cli/commands/browser-do-session.js +69 -0
- package/dist/cli/commands/browser-do.js +241 -0
- package/dist/cli/commands/browser.js +134 -303
- package/dist/cli/commands/identity.js +101 -94
- package/dist/cli/commands/mcp-call.js +4 -40
- 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 +17 -0
- package/dist/core/paths.js +2 -9
- package/dist/mcpd/health.js +1 -1
- package/dist/mcpd/instances.js +11 -1
- package/dist/mcpd/middleware.js +15 -3
- package/dist/mcpd/peerpid.js +52 -18
- package/npm-shrinkwrap.json +5 -5
- package/package.json +3 -3
- package/dist/cli/commands/browser-identity.js +0 -514
- package/dist/cli/commands/browser-session.js +0 -102
- package/dist/cli/commands/browser-vault.js +0 -193
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,145 @@
|
|
|
1
1
|
# turno-cli
|
|
2
2
|
|
|
3
|
+
## 0.5.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- df52afc: turno-browser has its own `turno-browser` command: add, list, show, set, login, vault, export, import, flows, replay, record, traces, live, pause, resume, doctor and reputation work without turno, with `--version` and a help grouped by section. `turno-browser add` prints how to connect an agent instead of an `mcp.toml` block, and `turno-browser do` says it needs turno for now. `turno browser ...` and the browser part of `turno identity ...` now run turno-browser's commands through its typed api (API_VERSION 1.12, new member `cli`) with the same flags, output and exit codes, so turno-cli needs turno-browser 1.12 or a later 1.x. turno-core gains `turno-core/cli`, the command line toolkit (arguments, output, styles, exit codes, command groups, hints, hidden input) that both programs share.
|
|
8
|
+
|
|
9
|
+
### Patch Changes
|
|
10
|
+
|
|
11
|
+
- 44fb809: `turno-browser-mcp --identity <id> --background` serves an identity to the shell without turno: MCP over streamable HTTP on a Unix socket in a private folder of the identity's state (or 127.0.0.1 with a per-server token where a socket cannot be used), one Chrome shared by every connection with the identity's sharing rules, and an exit after 15 minutes without a request (`--background-idle`, `TURNO_BROWSER_BACKGROUND_IDLE`). While it runs it alone drives the identity's browser: a stdio server of the same identity refuses calls that need the browser with a message that names it, and it does not start while another process has the identity's Chrome open. The package exports a client for it (`startBackground`, `backgroundStatus`, `openBackgroundSession`, `stopBackground`), also in the typed api, now version 1.11. turno-cli now asks for turno-browser 1.11 or a later 1.x.
|
|
12
|
+
- e2b1779: `turno-browser do` works without turno: it starts the identity's background server on the first action, keeps one session per agent or shell between commands, and ends it with `end`, `close` or when that process exits. `turno browser do` keeps going through mcpd.
|
|
13
|
+
|
|
14
|
+
New `turno-browser` commands: `setup <claude-code|codex|cursor|copilot|gemini|json> [--identity <id>]` prints the MCP config that gives an agent an identity (it writes nothing; `--json` for scripts), `background status|start|stop` shows and controls the background servers, and `secret set|has|list|remove` stores the `keychain:<item>` secrets identity.json names, in the same store and items as `turno secret`.
|
|
15
|
+
|
|
16
|
+
Hints, errors and doctor fixes name the commands of the program you typed: `turno-browser ...` on its own (the MCP server's messages too), `turno browser ...` and `turno identity ... browser:<id>` inside turno. `turno-browser doctor` lists a running background server, and a stdio server refused by a background server names `turno-browser background stop <id>`.
|
|
17
|
+
|
|
18
|
+
turno-core exports the owner, name and lock of a shell session of browser commands (`turno-core/cli`), shared by `turno browser do` and `turno-browser do`.
|
|
19
|
+
|
|
20
|
+
The npm description, keywords and MCP Registry entry follow the new tagline. Wording: the help no longer labels the off-by-default settings (natural language actions, captcha solving, network and user agent, Camoufox) as "not recommended"; they stay off by default and the help still says what each one sends where.
|
|
21
|
+
- Updated dependencies [df52afc]
|
|
22
|
+
- Updated dependencies [e2b1779]
|
|
23
|
+
- turno-core@0.2.0
|
|
24
|
+
|
|
25
|
+
## 0.4.0
|
|
26
|
+
|
|
27
|
+
### Minor Changes
|
|
28
|
+
|
|
29
|
+
- 50facc9: Record and replay. `browser_record` saves an agent's actions on an identity as a named flow, each
|
|
30
|
+
element kept four ways (Playwright's locator, role and name, text, CSS path) and typed text as
|
|
31
|
+
parameters; passwords, one-time codes and values marked sensitive are never saved, and a vault
|
|
32
|
+
sign-in is kept as `browser_login` with the site name. `browser_replay` runs a flow with
|
|
33
|
+
parameters (`keychain:<item>` reads one from the secret store) and, when a step no longer fits,
|
|
34
|
+
stops with a report the agent can continue from; `browser_flows` lists them. Opt-in per identity
|
|
35
|
+
(`record` in identity.json, `turno browser record <id>`): every session as a flow, Playwright
|
|
36
|
+
traces and a screenshot per action under a size cap, with the trace stopped around sign-ins.
|
|
37
|
+
New commands `turno browser flows|replay|record|traces`; `traces --open` uses the trace viewer
|
|
38
|
+
bundled with patchright-core.
|
|
39
|
+
With `"sharing": "tabs"` a recording holds only the session that started it and replay runs in
|
|
40
|
+
that session's tabs; traces and screenshots are refused in attach mode and in read mode.
|
|
41
|
+
- e94a153: Natural language actions and captcha solving for browser identities, both not recommended and off by default.
|
|
42
|
+
|
|
43
|
+
- `turno identity set browser:<id> --act` lists `browser_act` (an instruction carried out one stock browser action at a time, every step shown), `browser_extract` (data off the page as JSON) and `browser_observe` (proposed calls, not run). The model is your client's, through MCP sampling, or a chat completions API you name with `--act-provider chat-completions --act-endpoint <url> --act-model <name> --act-key keychain:<item>`. The model sees the instruction and a snapshot of the current tab with vault values and password, one-time code and card fields hidden; it can only click, type, select, press keys, hover, drag, wait and go back on that tab; steps, tokens and time are capped per call (`--act-max-steps`, `--act-max-tokens`, `--act-timeout`). A recorded flow keeps the stock steps it chose, never the instruction or the page.
|
|
44
|
+
- `--captcha-provider task-api --captcha-endpoint <url> --captcha-key keychain:<item>` lists `browser_solve_captcha`, which sends a reCAPTCHA v2, hCaptcha or Turnstile widget and the page address to that solver service and puts the token into the page. The service sees the page, and solving a site's captchas by machine may break its terms: it is meant for your own accounts. Every attempt is a line in the identity's reputation log, and `turno browser reputation` shows a `solved` column once a solver answered. Read mode refuses it.
|
|
45
|
+
- Keys are only `keychain:` references read from the secret store at use time, refused as literal values without being echoed, and hidden from every result.
|
|
46
|
+
- identity.json format 8 (`act`, `captcha`); turno-browser API 1.7, which turno-cli now requires.
|
|
47
|
+
- dcc2b97: Two opt-in engines for browser identities and servers, both off by default.
|
|
48
|
+
|
|
49
|
+
Read mode runs an identity (or one server) on Lightpanda instead of Chrome, for reading pages
|
|
50
|
+
with a fraction of the memory: `turno browser set <id> --engine lightpanda`, or
|
|
51
|
+
`turno-browser-mcp --engine lightpanda`. turno never downloads Lightpanda; it runs the
|
|
52
|
+
`lightpanda` on your PATH (or at `TURNO_LIGHTPANDA_PATH`), on a private Unix socket with
|
|
53
|
+
Lightpanda's telemetry off. The tools that work there run; the others (screenshots, dialogs,
|
|
54
|
+
drag and drop, uploads, `browser_login`) are refused with the reason before they reach the
|
|
55
|
+
browser. An identity in read mode keeps its own cookie jar, apart from its Chrome profile.
|
|
56
|
+
|
|
57
|
+
Attach mode connects to the Chrome you already have open, so the agent works in your signed-in
|
|
58
|
+
browser: `turno browser set <id> --attach auto` (after you turn on remote debugging at
|
|
59
|
+
chrome://inspect/#remote-debugging, Chrome 144 or newer), a Chrome user data folder, or a
|
|
60
|
+
loopback endpoint; `turno-browser-mcp --attach <target>` for one server. Chrome asks you to allow
|
|
61
|
+
each connection. turno works in a tab of its own, closes only the tabs it opened (on
|
|
62
|
+
`browser_close`, at the end of the session and at the idle limit) and never closes Chrome; the
|
|
63
|
+
tab governor leaves your tabs alone, and `browser_run_code_unsafe` is refused.
|
|
64
|
+
|
|
65
|
+
`turno browser doctor` gains an Engines section (Lightpanda found or how to install it, a
|
|
66
|
+
Lightpanda left running, which `--fix` stops) and checks each identity's attach target without
|
|
67
|
+
connecting. identity.json is now format 6 (`engine`, `attach`), and the turno-browser API is 1.5.
|
|
68
|
+
|
|
69
|
+
Read mode works with `"sharing": "tabs"`. Two pairs are refused with the reason, in identity.json
|
|
70
|
+
and from server flags: attach mode with `"sharing": "tabs"` (turno keeps one agent's tabs apart
|
|
71
|
+
from yours in your Chrome, not several sessions apart), and read mode with `humanize` (Lightpanda
|
|
72
|
+
lays out no page, so there is no pointer path to play). Attach mode works with `humanize`.
|
|
73
|
+
- 4814fb8: Human-like input for browser identities, off by default. `turno identity set browser:<id> --humanize light|full|adaptive` (or `turno-browser-mcp --humanize <level>`, which wins) makes the stock click, hover, type, fill form, select, drag, press key and coordinate tools, and `browser_batch`, move the pointer along a curve from where it last was to a point inside the target, hold the button for tens of milliseconds and type short text key by key with per-key delays. `full` adds a move time that follows distance and target size, overshoot and correction, a reading pause on a new page, longer gaps between words and wheel scrolling; `adaptive` is `light`, and `full` on a site where the identity met a challenge page. Tool names, arguments and results do not change. The setting is the `humanize` key of identity.json (format 4, with an optional `seed`), and turno-cli needs turno-browser API 1.3 for it.
|
|
74
|
+
- bf1cafb: `turno browser do <id> <action>` drives a browser identity from the shell, for coding agents that
|
|
75
|
+
prefer commands to MCP tools: `open`, `back`, `snapshot`, `read`, `find`, `click`, `type`,
|
|
76
|
+
`press`, `select`, `hover`, `wait`, `screenshot`, `tabs`, `login` (a vault sign-in), `call` (any
|
|
77
|
+
tool of the server), `close`, `session` and `end`. Each action is one tool call through mcpd to the
|
|
78
|
+
identity's turno-browser-mcp, so the tab locks or the one-session lease, attach mode, read mode's
|
|
79
|
+
refusals, humanize, recordings and the vault apply as they do to an agent. The commands one agent
|
|
80
|
+
or one terminal shell runs are one session and keep their tab: turno keeps the mcpd session open
|
|
81
|
+
between them and ties it to the agent (or the shell), and `end` closes it; `--session <name>` or
|
|
82
|
+
`TURNO_BROWSER_SESSION` keeps several apart. Output is `url:` and `title:` lines and what the
|
|
83
|
+
action returned, errors on stderr with exit 1, or one object with `--json`. `turno browser <id>
|
|
84
|
+
<action>` is the short form when the id is not a subcommand. The Claude Code plugin adds a short
|
|
85
|
+
`browser-identity` skill that teaches the loop and reads the installed help first.
|
|
86
|
+
- 6698608: Live view and take-over for browser identities, off by default. `turno browser set <id> --live true` (or `turno-browser-mcp --live`) serves a view of the agent's tab on 127.0.0.1 with a per-server token; `turno browser live <id>` opens it through a link that works once and is never printed. `turno browser pause <id>` (or Pause in the view) holds the agent's next call, up to `live.wait` (2 minutes by default), while you act in the window or through the view; `turno browser resume <id>` lets it go, and each held call is told what changed (URL, title, tabs, your input) and a call that would act on stale refs is not run: it returns an error with a fresh snapshot. The natural language and captcha tools are held too, and a `browser_act` run stops at its next step. A page that holds a vault value or a password is never shown, including a sign-in typed key by key with humanize. Read mode and attach mode refuse the view and keep pause and resume. turno-browser API 1.9, identity.json format 10.
|
|
87
|
+
- c716051: Opt-in network and user agent settings, and a Camoufox engine, all off by default and not
|
|
88
|
+
recommended for your own accounts. identity.json format 11 adds `userAgent` and `proxy`, and the
|
|
89
|
+
`engine` key takes `"camoufox"` (also format 11); API_VERSION is 1.10 and turno-cli needs it.
|
|
90
|
+
`"userAgent": "coherent"` takes no typed value: a headless Chrome gets the user agent a headed one
|
|
91
|
+
of the same version sends, with its real client hints metadata, and a launch check refuses any
|
|
92
|
+
contradiction between the user agent, client hints, platform, headers, languages and time zone.
|
|
93
|
+
`proxy` sends the identity's traffic (sign-ins included) through an http, https or SOCKS proxy,
|
|
94
|
+
with its password only from the secret store, hidden from every tool result, and WebRTC limited
|
|
95
|
+
to the proxy. `--engine camoufox` runs an identity on an installed Camoufox through stock
|
|
96
|
+
playwright-core, with its own Firefox profile; turno never downloads it, and every stock tool but
|
|
97
|
+
`browser_run_code_unsafe` runs, as do natural language actions, `browser_login` and
|
|
98
|
+
`turno browser do`. Captcha solving is refused with it, and the live view shows no picture there
|
|
99
|
+
(pause and resume work). The act model's prompt and the live view's tab list hide the proxy
|
|
100
|
+
credentials. New flags on `turno identity set` (`--user-agent`,
|
|
101
|
+
`--proxy-server`, `--proxy-username`, `--proxy-password`, `--no-proxy`), doctor checks for
|
|
102
|
+
Camoufox and the proxy credentials, and fingerprint bench checks that compare headers and a
|
|
103
|
+
worker with the page.
|
|
104
|
+
- 4b9556f: Several agent sessions can now share one browser identity, each in its own tabs. Turn it on per identity with `"sharing": "tabs"` in identity.json (`turno identity set browser:<id> --sharing tabs`, or the server's `--sharing tabs`) and serve the identity with `exclusive = false` in mcp.toml; the default stays one session at a time. Each session gets its own current tab and holds the tabs it works on: another session's call on one of them waits up to 10 seconds and then fails with a message that names the holder. A session lets go of its tabs when it ends (mcpd now tells servers that declare the `io.turno/sessions` capability), when a tab closes, or after 5 minutes without a call. `browser_close` closes only the calling session's tabs, tab lists mark the tabs other sessions hold, and the tab governor's cap counts each session's tabs apart. `turno identity add browser:<id> --sharing tabs` prints the matching mcp.toml block. turno-browser's API version is now 1.4.
|
|
105
|
+
- 3bd7bc2: Follow-ups for browser identities.
|
|
106
|
+
|
|
107
|
+
A tool call no longer starts while the tab governor is still closing a popup or a parked tab, so
|
|
108
|
+
the call cannot fail with "Target page, context or browser has been closed". A Chrome that does
|
|
109
|
+
not stop after SIGKILL gets 5 seconds instead of 2 before turno calls it a survivor.
|
|
110
|
+
|
|
111
|
+
A server in read mode stops a Lightpanda that a killed server left running on the same cookie jar
|
|
112
|
+
when it starts, and removes its socket folder.
|
|
113
|
+
|
|
114
|
+
`turno browser doctor` warns when an identity that serves one session at a time
|
|
115
|
+
(`"sharing": "session"`) is served by an mcp.toml server with `exclusive = false`, and says how to
|
|
116
|
+
switch it to `"sharing": "tabs"`.
|
|
117
|
+
|
|
118
|
+
The tab lock wait (10 seconds) and the idle release (5 minutes) of `"sharing": "tabs"` can be set
|
|
119
|
+
per identity: `"tabLocks": { "wait": "30s", "release": "10m" }` in identity.json, or
|
|
120
|
+
`turno browser set <id> --tab-lock-wait 30s --tab-lock-release 10m`. identity.json is now format
|
|
121
|
+
9 and the turno-browser API is 1.8.
|
|
122
|
+
|
|
123
|
+
The `light` humanize level moves the pointer on a visible arc (some 4 to 8% longer than the
|
|
124
|
+
straight line) instead of an almost straight path. With humanize on, `browser_login` clicks into
|
|
125
|
+
each field and types the value key by key; the keys go to Chrome only, never to a result, a
|
|
126
|
+
recording or a log.
|
|
127
|
+
|
|
128
|
+
When another element covers the target of a click, hover, type, select or drag before the call,
|
|
129
|
+
and still covers it 300 ms later, the error names that element at once instead of after the 5
|
|
130
|
+
second action timeout.
|
|
131
|
+
|
|
132
|
+
Chrome keeps its third-party storage partitioning on, as a person's Chrome does: Playwright turns
|
|
133
|
+
it off, and turno now passes Playwright's list of disabled features without it.
|
|
134
|
+
|
|
135
|
+
### Patch Changes
|
|
136
|
+
|
|
137
|
+
- 31a9a36: `turno doctor`, `turno status`, `turno mcp daemon status` and `turno doctor --security` no longer
|
|
138
|
+
wait for lsof to tell who holds mcpd's port. A free port is told by a refused connection, and a
|
|
139
|
+
running mcpd's port is checked against the pid it reports, so lsof's scan of every process (which
|
|
140
|
+
takes seconds on a busy Mac) runs only when another process holds the port. When lsof cannot say
|
|
141
|
+
which process that is, doctor still reports the port as in use.
|
|
142
|
+
|
|
3
143
|
## 0.3.0
|
|
4
144
|
|
|
5
145
|
### Minor Changes
|
package/README.md
CHANGED
|
@@ -258,6 +258,7 @@ change, and print the `turno undo <stamp>` that puts them back. `turno mcp impor
|
|
|
258
258
|
| `turno browser identities\|add\|show\|set\|login\|remove` | browser identities and their settings (needs `turno-browser`) |
|
|
259
259
|
| `turno browser vault add\|list\|remove\|submit` | sign-in credentials per site in the OS secret store, filled by the `browser_login` tool |
|
|
260
260
|
| `turno browser export <id> --out <file>`, `turno browser import <file>` | move an identity's session to another machine as one encrypted file |
|
|
261
|
+
| `turno browser flows\|replay <id>`, `turno browser record\|traces <id>` | saved flows and replaying them; opt-in recordings (flows, traces, screenshots) and the trace viewer |
|
|
261
262
|
| `turno browser doctor [--fix]`, `turno browser reputation [<id>]` | check Chrome and every identity; how often each site challenged it |
|
|
262
263
|
| `turno browser bench` | developer diagnostic: browser engine compatibility |
|
|
263
264
|
|
|
@@ -349,7 +350,7 @@ turno identity login browser:school # sign in by hand once; cookies persist
|
|
|
349
350
|
```
|
|
350
351
|
|
|
351
352
|
`turno-browser` is its own MCP server. It drives the installed Google Chrome, headed, with a
|
|
352
|
-
persistent profile per identity
|
|
353
|
+
persistent profile per identity; each identity keeps its time zone,
|
|
353
354
|
languages and locale fixed (`turno browser show|set <id>`).
|
|
354
355
|
|
|
355
356
|
- **It cleans up after itself.** At most 8 tabs per session's browser, an unused tab is parked
|
|
@@ -370,12 +371,14 @@ languages and locale fixed (`turno browser show|set <id>`).
|
|
|
370
371
|
- **Health you can read.** `turno browser doctor [--fix]` checks Chrome and every identity, and
|
|
371
372
|
`turno browser reputation` shows how often each site challenged it.
|
|
372
373
|
|
|
373
|
-
More in [Browser identities](https://turno.wimo.dev/docs/
|
|
374
|
+
More in [Browser identities](https://turno.wimo.dev/docs/browser/identities) and
|
|
374
375
|
[`turno browser`](https://turno.wimo.dev/docs/reference/cli/browser).
|
|
375
376
|
|
|
376
377
|
**Acceptable use.** Use it with your own accounts, on sites whose terms allow automated access
|
|
377
|
-
by you, and at a human pace. Do not use it to get around access controls, paywalls
|
|
378
|
-
|
|
378
|
+
by you, and at a human pace. Do not use it to get around access controls, paywalls or rate
|
|
379
|
+
limits, to scrape at scale, or to access accounts that are not yours. Captcha solving and natural
|
|
380
|
+
language actions are off unless you turn them on per identity; both send page content to a third
|
|
381
|
+
party you pick, and captcha solving may break a site's terms.
|
|
379
382
|
|
|
380
383
|
### Accounts bridge
|
|
381
384
|
|
|
@@ -403,7 +406,8 @@ Details: [docs/ui.md](https://github.com/wimo-dev/turno/blob/main/docs/ui.md).
|
|
|
403
406
|
### Claude Code plugin
|
|
404
407
|
|
|
405
408
|
`plugins/turno` adds session handoff and fork skills, a `copy-artifact` skill that copies a
|
|
406
|
-
claude.ai artifact (with its files and assets) from one profile's account to another,
|
|
409
|
+
claude.ai artifact (with its files and assets) from one profile's account to another, a
|
|
410
|
+
`browser-identity` skill that drives a browser identity with `turno browser do`, and a hook
|
|
407
411
|
that records when a profile reaches its usage limit, so `turno status` can show it:
|
|
408
412
|
|
|
409
413
|
```
|
package/dist/cli/args.js
CHANGED
|
@@ -1,79 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
/** Flags shared by every command, so the same words mean the same thing everywhere. */
|
|
4
|
-
export const JSON_FLAG = { json: { type: "boolean" } };
|
|
5
|
-
export const DRY_RUN = { "dry-run": { type: "boolean" } };
|
|
6
|
-
export const FORCE = { force: { type: "boolean" } };
|
|
7
|
-
export const TAIL = { tail: { type: "string", short: "n" } };
|
|
8
|
-
export const WATCH = { watch: { type: "boolean" } };
|
|
9
|
-
/**
|
|
10
|
-
* Completion flags of a spec (`--name` for each long flag), so the parser and the shell
|
|
11
|
-
* completion never disagree. Use it as `flags: flagsOf(SPEC)` next to `parseFlags(args, SPEC)`.
|
|
12
|
-
*/
|
|
13
|
-
export function flagsOf(...specs) {
|
|
14
|
-
return specs.flatMap((s) => Object.keys(s)).map((k) => `--${k}`);
|
|
15
|
-
}
|
|
16
|
-
/**
|
|
17
|
-
* Take `--json=<value>` out of `args` when the spec has a boolean --json: it turns
|
|
18
|
-
* --json on and keeps the value for `emit`. Only a command's own flags count, not those after --.
|
|
19
|
-
*/
|
|
20
|
-
function takeJsonValue(args, spec) {
|
|
21
|
-
if (spec.json?.type !== "boolean")
|
|
22
|
-
return { args };
|
|
23
|
-
let json;
|
|
24
|
-
const out = [];
|
|
25
|
-
let i = 0;
|
|
26
|
-
for (; i < args.length && args[i] !== "--"; i++) {
|
|
27
|
-
const a = args[i];
|
|
28
|
-
if (a.startsWith("--json=")) {
|
|
29
|
-
json = a.slice("--json=".length);
|
|
30
|
-
out.push("--json");
|
|
31
|
-
}
|
|
32
|
-
else {
|
|
33
|
-
if (a === "--json")
|
|
34
|
-
json ??= "";
|
|
35
|
-
out.push(a);
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
return { args: [...out, ...args.slice(i)], ...(json !== undefined ? { json } : {}) };
|
|
39
|
-
}
|
|
40
|
-
/**
|
|
41
|
-
* Parse `args` against `spec` (long flags, `--flag=value`, short aliases). Unknown flags and
|
|
42
|
-
* positionals beyond `max` are usage errors with a plain message.
|
|
43
|
-
*/
|
|
44
|
-
export function parseFlags(given, spec, max = Number.POSITIVE_INFINITY) {
|
|
45
|
-
const { args, json } = takeJsonValue(given, spec);
|
|
46
|
-
let parsed;
|
|
47
|
-
try {
|
|
48
|
-
parsed = parseArgs({ args, options: spec, allowPositionals: true, strict: true });
|
|
49
|
-
if (json !== undefined)
|
|
50
|
-
parsed.json = json;
|
|
51
|
-
}
|
|
52
|
-
catch (err) {
|
|
53
|
-
const msg = err.message
|
|
54
|
-
.replace(/^Unknown option '(.+?)'.*$/s, "unknown option $1")
|
|
55
|
-
.replace(/\. To specify.*$/s, "");
|
|
56
|
-
return fail(msg.replace(/^Option '(--[\w-]+)(?: <value>)?' argument missing$/, "$1 needs a value"));
|
|
57
|
-
}
|
|
58
|
-
if (parsed.positionals.length > max)
|
|
59
|
-
fail(`unexpected argument '${parsed.positionals[max]}'`);
|
|
60
|
-
return parsed;
|
|
61
|
-
}
|
|
62
|
-
export function bool(p, name) {
|
|
63
|
-
return p.values[name] === true;
|
|
64
|
-
}
|
|
65
|
-
export function str(p, name) {
|
|
66
|
-
const v = p.values[name];
|
|
67
|
-
return typeof v === "string" ? v : undefined;
|
|
68
|
-
}
|
|
69
|
-
/** A positive integer flag such as --tail. */
|
|
70
|
-
export function count(p, name, fallback) {
|
|
71
|
-
const v = str(p, name);
|
|
72
|
-
if (v === undefined)
|
|
73
|
-
return fallback;
|
|
74
|
-
const n = Number.parseInt(v, 10);
|
|
75
|
-
if (!Number.isInteger(n) || n < 1 || String(n) !== v.trim())
|
|
76
|
-
fail(`--${name} must be a positive integer`);
|
|
77
|
-
return n;
|
|
78
|
-
}
|
|
1
|
+
// Moved to turno-core so turno-browser shares it; re-exported here so commands keep their imports.
|
|
2
|
+
export * from "turno-core/cli/args";
|
|
79
3
|
//# sourceMappingURL=args.js.map
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { readFileSync, readdirSync, rmSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { writePrivateFile } from "turno-core/paths";
|
|
4
|
+
import { pidAlive } from "turno-core/process";
|
|
5
|
+
import { paths } from "../../core/paths.js";
|
|
6
|
+
/**
|
|
7
|
+
* The session of `turno browser do`. Each command is one call through mcpd, the way an agent's
|
|
8
|
+
* tool call goes, so the identity's browser, its tab locks ("sharing": "tabs") or its one-session
|
|
9
|
+
* lease ("sharing": "session"), humanize and recordings treat it like an agent session.
|
|
10
|
+
*
|
|
11
|
+
* What makes several commands one session is the mcpd session itself: the first command opens
|
|
12
|
+
* one and saves its id in a private file, the next commands reconnect to it instead of opening
|
|
13
|
+
* another, and mcpd keeps giving the browser the same `io.turno/session` id, so the commands keep
|
|
14
|
+
* their tab. mcpd drops a session when the process named by its X-Turno-Pid header exits, so that
|
|
15
|
+
* header names the process the commands belong to (`sessionOwner`): the coding agent that runs
|
|
16
|
+
* them through a shell, or the terminal's shell. `turno browser do <id> end` ends it at once.
|
|
17
|
+
*/
|
|
18
|
+
// The owner of a session, its name and the lock moved to turno-core so turno-browser's own `do`
|
|
19
|
+
// shares them; re-exported here so turno's imports stay.
|
|
20
|
+
export { ownerName, passesOn, sessionName, sessionOwner, withLock } from "turno-core/cli";
|
|
21
|
+
/** Where CLI sessions are saved: <state>/browser-do. */
|
|
22
|
+
export function sessionsDir(env) {
|
|
23
|
+
return join(paths(env).state, "browser-do");
|
|
24
|
+
}
|
|
25
|
+
export function sessionFile(env, identity, owner, name) {
|
|
26
|
+
return join(sessionsDir(env), `${identity}.${owner}.${name}.json`);
|
|
27
|
+
}
|
|
28
|
+
export function readSaved(file) {
|
|
29
|
+
try {
|
|
30
|
+
const s = JSON.parse(readFileSync(file, "utf8"));
|
|
31
|
+
if (s.v !== 1 || typeof s.sessionId !== "string" || typeof s.port !== "number" || !s.owner)
|
|
32
|
+
return undefined;
|
|
33
|
+
return s;
|
|
34
|
+
}
|
|
35
|
+
catch {
|
|
36
|
+
return undefined;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
export function writeSaved(file, saved, env) {
|
|
40
|
+
writePrivateFile(file, `${JSON.stringify(saved, null, 2)}\n`, env);
|
|
41
|
+
}
|
|
42
|
+
export function dropSaved(file) {
|
|
43
|
+
rmSync(file, { force: true });
|
|
44
|
+
}
|
|
45
|
+
/** Removes the saved sessions whose owner exited (mcpd dropped those sessions already). */
|
|
46
|
+
export function sweepSaved(env, alive = pidAlive) {
|
|
47
|
+
let names;
|
|
48
|
+
try {
|
|
49
|
+
names = readdirSync(sessionsDir(env));
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
for (const n of names) {
|
|
55
|
+
const m = /^[^.]+\.(\d+)\.[^.]+\.json$/.exec(n);
|
|
56
|
+
if (m && !alive(Number(m[1])))
|
|
57
|
+
dropSaved(join(sessionsDir(env), n));
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* The mcp.toml server that serves identity `id`: a stdio server with `--identity <id>` in its
|
|
62
|
+
* args (`browser-<id>` first, as `turno browser add` prints it).
|
|
63
|
+
*/
|
|
64
|
+
export function identityServer(manifest, id) {
|
|
65
|
+
const serves = (args) => args.some((a, i) => (a === "--identity" && args[i + 1] === id) || a === `--identity=${id}`);
|
|
66
|
+
const found = Object.values(manifest.servers).filter((s) => s.kind === "stdio" && serves(s.args));
|
|
67
|
+
return found.find((s) => s.name === `browser-${id}`) ?? found.sort((a, b) => a.name.localeCompare(b.name))[0];
|
|
68
|
+
}
|
|
69
|
+
//# sourceMappingURL=browser-do-session.js.map
|
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
import { readProcTable } from "turno-core/process";
|
|
2
|
+
import { gatewayUrl } from "../../agents/adapter.js";
|
|
3
|
+
import { loadManifest, servesProfile } from "../../core/manifest.js";
|
|
4
|
+
import { tildify } from "../../core/paths.js";
|
|
5
|
+
import { packageInfo } from "../../core/version.js";
|
|
6
|
+
import { str } from "../args.js";
|
|
7
|
+
import { Exit } from "../exit.js";
|
|
8
|
+
import { CliError } from "../io.js";
|
|
9
|
+
import { dropSaved, identityServer, ownerName, readSaved, sessionFile, sessionName, sessionOwner, sweepSaved, withLock, writeSaved, } from "./browser-do-session.js";
|
|
10
|
+
import { callerProfile, gatewayMessage } from "./mcp-call.js";
|
|
11
|
+
export const defaultDoDeps = {
|
|
12
|
+
owner: (env) => sessionOwner(readProcTable(), process.ppid, Number(env.TURNO_SESSION_PID) || undefined),
|
|
13
|
+
};
|
|
14
|
+
const HELP = {
|
|
15
|
+
intro: `Runs one action on the identity's browser through mcpd and turno-browser-mcp, the server
|
|
16
|
+
agents use, so the identity's rules apply as they do to an agent's tool call. The identity must
|
|
17
|
+
be served by mcpd (its [server.browser-<id>] block in mcp.toml, which turno browser add prints).`,
|
|
18
|
+
session: `Session: the commands one agent or one terminal shell runs are one session, so they keep their
|
|
19
|
+
tab and their place in the tab locks ("sharing": "tabs") or hold the identity's one-session lease
|
|
20
|
+
("sharing": "session"). The session belongs to the process that runs the commands: the coding
|
|
21
|
+
agent (each of its shell commands passes through a short-lived shell) or your terminal's shell.
|
|
22
|
+
It ends when that process exits, with 'end' or 'close', or when mcpd restarts. --session <name>
|
|
23
|
+
(or TURNO_BROWSER_SESSION) keeps several sessions apart under one owner, for example two
|
|
24
|
+
subagents each in its own tab.`,
|
|
25
|
+
output: `Output: url: and title: lines, then what the action returned; errors go to stderr with exit 1.
|
|
26
|
+
--snapshot after an action prints the snapshot too; --json prints one object.
|
|
27
|
+
--timeout <seconds> (default 120) bounds one action; attach mode can wait for Chrome's consent
|
|
28
|
+
dialog. --profile picks the profile whose token and tool filter apply (default: this folder's).`,
|
|
29
|
+
};
|
|
30
|
+
const down = () => new CliError("mcpd is not running (turno mcp daemon start)", Exit.gatewayDown);
|
|
31
|
+
async function open(t, saved) {
|
|
32
|
+
const { Client } = await import("@modelcontextprotocol/sdk/client/index.js");
|
|
33
|
+
const { StreamableHTTPClientTransport } = await import("@modelcontextprotocol/sdk/client/streamableHttp.js");
|
|
34
|
+
const transport = new StreamableHTTPClientTransport(new URL(gatewayUrl(t.server, t.port)), {
|
|
35
|
+
...(saved ? { sessionId: saved.sessionId } : {}),
|
|
36
|
+
requestInit: {
|
|
37
|
+
headers: {
|
|
38
|
+
Authorization: `Bearer ${t.token}`,
|
|
39
|
+
"X-Turno-Profile": t.profile,
|
|
40
|
+
// The session's owner, not this short-lived process: mcpd keeps the session while it lives.
|
|
41
|
+
"X-Turno-Pid": String(t.owner.pid),
|
|
42
|
+
"X-Turno-Cwd-B64": Buffer.from(process.cwd(), "utf8").toString("base64url"),
|
|
43
|
+
},
|
|
44
|
+
},
|
|
45
|
+
});
|
|
46
|
+
if (saved?.protocolVersion)
|
|
47
|
+
transport.setProtocolVersion(saved.protocolVersion);
|
|
48
|
+
const client = new Client({ name: "turno-browser-do", version: packageInfo().version }, { capabilities: {} });
|
|
49
|
+
try {
|
|
50
|
+
await client.connect(transport);
|
|
51
|
+
}
|
|
52
|
+
catch (err) {
|
|
53
|
+
const g = gatewayMessage(err);
|
|
54
|
+
if (g.status === undefined && /ECONNREFUSED|fetch failed|connect/i.test(g.message))
|
|
55
|
+
throw down();
|
|
56
|
+
throw new CliError(`mcpd refused the session for ${t.server} as profile ${t.profile}: ${g.message}`);
|
|
57
|
+
}
|
|
58
|
+
if (!saved && client.getServerVersion()?.version === "hidden")
|
|
59
|
+
throw new CliError(client.getInstructions() ?? `${t.server} is hidden in this folder`);
|
|
60
|
+
return { client, transport };
|
|
61
|
+
}
|
|
62
|
+
function freshSaved(t, transport, previous) {
|
|
63
|
+
const now = new Date().toISOString();
|
|
64
|
+
return {
|
|
65
|
+
v: 1,
|
|
66
|
+
identity: t.identity,
|
|
67
|
+
server: t.server,
|
|
68
|
+
profile: t.profile,
|
|
69
|
+
port: t.port,
|
|
70
|
+
sessionId: transport.sessionId,
|
|
71
|
+
...(transport.protocolVersion ? { protocolVersion: transport.protocolVersion } : {}),
|
|
72
|
+
owner: t.owner,
|
|
73
|
+
name: t.name,
|
|
74
|
+
createdAt: previous?.createdAt ?? now,
|
|
75
|
+
usedAt: now,
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
/** A saved session this command can continue: same mcpd, server, profile and owner. */
|
|
79
|
+
function usable(saved, t) {
|
|
80
|
+
return (saved !== undefined &&
|
|
81
|
+
saved.port === t.port &&
|
|
82
|
+
saved.server === t.server &&
|
|
83
|
+
saved.profile === t.profile &&
|
|
84
|
+
saved.owner.pid === t.owner.pid);
|
|
85
|
+
}
|
|
86
|
+
/** mcpd no longer knows the session (it answers 404): the command opens a new one. */
|
|
87
|
+
function sessionGone(err) {
|
|
88
|
+
const g = gatewayMessage(err);
|
|
89
|
+
return g.status === 404 || /session not found|unknown session/i.test(g.message);
|
|
90
|
+
}
|
|
91
|
+
async function terminate(t, saved) {
|
|
92
|
+
const { transport, client } = await open(t, saved).catch(() => ({ transport: undefined, client: undefined }));
|
|
93
|
+
await transport?.terminateSession().catch(() => { });
|
|
94
|
+
await client?.close().catch(() => { });
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Ends a saved session when the mcpd that holds it still runs (it may hold the identity's
|
|
98
|
+
* lease), with the token of the profile that opened it. Another mcpd forgot it already.
|
|
99
|
+
*/
|
|
100
|
+
async function endSaved(t, saved) {
|
|
101
|
+
if (saved.port !== t.port || saved.owner.pid !== t.owner.pid)
|
|
102
|
+
return;
|
|
103
|
+
await terminate({ ...t, server: saved.server, profile: saved.profile, token: t.tokenFor(saved.profile) }, saved);
|
|
104
|
+
}
|
|
105
|
+
async function resolveTarget(id, p, io, deps) {
|
|
106
|
+
const manifest = loadManifest(io.env);
|
|
107
|
+
const def = identityServer(manifest, id);
|
|
108
|
+
if (!def)
|
|
109
|
+
throw new CliError(`browser identity "${id}" is not served by mcpd: add its block to mcp.toml (turno browser add ${id} prints it, or use [server.browser-${id}] with args = ["--identity", "${id}"]) and run turno mcp sync`);
|
|
110
|
+
const profile = callerProfile(str(p, "profile"), io.env, process.cwd());
|
|
111
|
+
if (!servesProfile(def, profile))
|
|
112
|
+
throw new CliError(`${def.name} is not served to profile ${profile} (profiles = ${JSON.stringify(def.profiles)} in mcp.toml); pick another with --profile`);
|
|
113
|
+
const { mcpdPort, readToken } = await import("../../mcpd/state.js");
|
|
114
|
+
const { profileToken } = await import("../../mcpd/tokens.js");
|
|
115
|
+
const master = readToken(io.env);
|
|
116
|
+
if (!master)
|
|
117
|
+
throw down();
|
|
118
|
+
const owner = deps.owner(io.env);
|
|
119
|
+
const name = sessionName(str(p, "session"), io.env);
|
|
120
|
+
return {
|
|
121
|
+
identity: id,
|
|
122
|
+
server: def.name,
|
|
123
|
+
profile,
|
|
124
|
+
port: mcpdPort(io.env),
|
|
125
|
+
token: profileToken(master, profile),
|
|
126
|
+
owner,
|
|
127
|
+
name,
|
|
128
|
+
file: sessionFile(io.env, id, owner.pid, name),
|
|
129
|
+
tokenFor: (other) => profileToken(master, other),
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
/** Run `fn` in the saved session of `t`, or a new one, under the session file's lock. */
|
|
133
|
+
async function inSession(t, io, fn) {
|
|
134
|
+
return await withLock(t.file, io.env, async () => {
|
|
135
|
+
let saved = readSaved(t.file);
|
|
136
|
+
if (saved && !usable(saved, t)) {
|
|
137
|
+
// Another mcpd, server, profile or owner: that session is not this one's to continue.
|
|
138
|
+
await endSaved(t, saved);
|
|
139
|
+
dropSaved(t.file);
|
|
140
|
+
saved = undefined;
|
|
141
|
+
}
|
|
142
|
+
let resumed = saved !== undefined;
|
|
143
|
+
let used = false;
|
|
144
|
+
let ended = false;
|
|
145
|
+
let conn = await open(t, saved);
|
|
146
|
+
const raw = async (name, a, timeout) => (await conn.client.callTool({ name, arguments: a }, undefined, {
|
|
147
|
+
timeout,
|
|
148
|
+
resetTimeoutOnProgress: true,
|
|
149
|
+
}));
|
|
150
|
+
// A session this command opened and could not use (a lease another session holds, a
|
|
151
|
+
// refused call) goes at once instead of waiting for its owner to exit.
|
|
152
|
+
const giveUp = async (tool, err) => {
|
|
153
|
+
if (!resumed && !used)
|
|
154
|
+
await conn.transport.terminateSession().catch(() => { });
|
|
155
|
+
throw new CliError(`${tool}: ${gatewayMessage(err).message}`);
|
|
156
|
+
};
|
|
157
|
+
const session = {
|
|
158
|
+
get resumed() {
|
|
159
|
+
return resumed;
|
|
160
|
+
},
|
|
161
|
+
async call(tool, args, timeout) {
|
|
162
|
+
let result;
|
|
163
|
+
try {
|
|
164
|
+
result = await raw(tool, args, timeout);
|
|
165
|
+
}
|
|
166
|
+
catch (err) {
|
|
167
|
+
if (!resumed || used || !sessionGone(err))
|
|
168
|
+
return await giveUp(tool, err);
|
|
169
|
+
// mcpd forgot the session (it restarted and could not rebuild it): start a new one.
|
|
170
|
+
await conn.client.close().catch(() => { });
|
|
171
|
+
dropSaved(t.file);
|
|
172
|
+
resumed = false;
|
|
173
|
+
conn = await open(t);
|
|
174
|
+
try {
|
|
175
|
+
result = await raw(tool, args, timeout);
|
|
176
|
+
}
|
|
177
|
+
catch (again) {
|
|
178
|
+
return await giveUp(tool, again);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
if (!used && !ended)
|
|
182
|
+
writeSaved(t.file, freshSaved(t, conn.transport, saved), io.env);
|
|
183
|
+
used = true;
|
|
184
|
+
return result;
|
|
185
|
+
},
|
|
186
|
+
async end() {
|
|
187
|
+
ended = true;
|
|
188
|
+
await conn.transport.terminateSession().catch(() => { });
|
|
189
|
+
dropSaved(t.file);
|
|
190
|
+
},
|
|
191
|
+
};
|
|
192
|
+
try {
|
|
193
|
+
return await fn(session);
|
|
194
|
+
}
|
|
195
|
+
finally {
|
|
196
|
+
// Never terminateSession here: the next command of this session reconnects to it.
|
|
197
|
+
await conn.client.close().catch(() => { });
|
|
198
|
+
}
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
/** The mcpd transport of `turno browser do`. */
|
|
202
|
+
export function mcpdTransport(deps = defaultDoDeps) {
|
|
203
|
+
return {
|
|
204
|
+
help: HELP,
|
|
205
|
+
flags: { profile: { type: "string" } },
|
|
206
|
+
async target(id, p, io) {
|
|
207
|
+
const t = await resolveTarget(id, p, io, deps);
|
|
208
|
+
sweepSaved(io.env);
|
|
209
|
+
const identity = `browser:${id}`;
|
|
210
|
+
return {
|
|
211
|
+
head: { identity, server: t.server, profile: t.profile },
|
|
212
|
+
info: (resumed) => ({ name: t.name, ownerPid: t.owner.pid, owner: ownerName(t.owner), resumed }),
|
|
213
|
+
describe() {
|
|
214
|
+
const found = readSaved(t.file);
|
|
215
|
+
const saved = usable(found, t) ? found : undefined;
|
|
216
|
+
return {
|
|
217
|
+
open: saved !== undefined,
|
|
218
|
+
lines: [
|
|
219
|
+
`identity: ${identity} (server ${t.server}, profile ${t.profile})`,
|
|
220
|
+
`session: ${t.name}, owned by ${ownerName(t.owner)} (pid ${t.owner.pid})`,
|
|
221
|
+
saved
|
|
222
|
+
? `state: open since ${saved.createdAt}, last used ${saved.usedAt}`
|
|
223
|
+
: "state: none yet; the next action opens it",
|
|
224
|
+
`ends: when pid ${t.owner.pid} exits, with 'end' or 'close', or when mcpd restarts`,
|
|
225
|
+
`file: ${tildify(t.file, io.env)}`,
|
|
226
|
+
],
|
|
227
|
+
};
|
|
228
|
+
},
|
|
229
|
+
async end() {
|
|
230
|
+
const saved = readSaved(t.file);
|
|
231
|
+
if (saved)
|
|
232
|
+
await endSaved(t, saved);
|
|
233
|
+
dropSaved(t.file);
|
|
234
|
+
return saved !== undefined;
|
|
235
|
+
},
|
|
236
|
+
session: (fn) => inSession(t, io, fn),
|
|
237
|
+
};
|
|
238
|
+
},
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
//# sourceMappingURL=browser-do.js.map
|