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
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import type { Io } from "turno-core/cli";
|
|
2
|
+
import type { BrowserFunctions } from "../api.js";
|
|
3
|
+
import type { ServedIdentity } from "../doctor.js";
|
|
4
|
+
import type { SettingsPatch } from "../settings.js";
|
|
5
|
+
import { type CliNames } from "../names.js";
|
|
6
|
+
import type { DoTransport } from "./do.js";
|
|
7
|
+
import { type BackgroundDoDeps } from "./do-background.js";
|
|
8
|
+
/**
|
|
9
|
+
* What the identity commands need from the program that runs them. They run as `turno-browser
|
|
10
|
+
* <cmd>` on their own, and as `turno browser <cmd>` and `turno identity ... browser:<id>` inside
|
|
11
|
+
* turno-cli, which passes a host of its own for what only turno has: the mcp.toml block, the
|
|
12
|
+
* gateway that `do` goes through, and its own command names in hints and help.
|
|
13
|
+
*/
|
|
14
|
+
export type { CliNames } from "../names.js";
|
|
15
|
+
export interface BrowserCliHost {
|
|
16
|
+
names: CliNames;
|
|
17
|
+
/** The paragraph after the subcommands in the group's help. */
|
|
18
|
+
footer: string;
|
|
19
|
+
/** The summary of `add` in the help: what it prints once the identity exists. */
|
|
20
|
+
addSummary: string;
|
|
21
|
+
/** What `add` prints once the identity exists: how an agent reaches it. */
|
|
22
|
+
connect(id: string, settings: SettingsPatch, io: Io): void;
|
|
23
|
+
/** What `remove` prints once the identity is gone. */
|
|
24
|
+
removed?(id: string, io: Io): void;
|
|
25
|
+
/** What has to change outside identity.json after `set --sharing`. */
|
|
26
|
+
sharingNote?(id: string, sharing: "session" | "tabs" | undefined): string;
|
|
27
|
+
/** Who likely holds a busy identity, and what to wait for, for `login`. */
|
|
28
|
+
busy?: {
|
|
29
|
+
holder: string;
|
|
30
|
+
end: string;
|
|
31
|
+
};
|
|
32
|
+
/** How the server starts again, for set's notes: "a new agent session". */
|
|
33
|
+
restart: string;
|
|
34
|
+
/** The servers that run turno-browser-mcp for each identity, for the doctor's sharing check. */
|
|
35
|
+
servers?(env: NodeJS.ProcessEnv): ServedIdentity[];
|
|
36
|
+
/** How `do` reaches the identity's server. Without one, `do` says what it needs. */
|
|
37
|
+
doTransport?: DoTransport;
|
|
38
|
+
/** Whether `Next:` hints print (turno's config.toml `hints`); on when not given. */
|
|
39
|
+
hints?(env: NodeJS.ProcessEnv): boolean;
|
|
40
|
+
}
|
|
41
|
+
/** Opens a link in the default browser (`live`); tests inject a fake. */
|
|
42
|
+
export type OpenUrl = (url: string, io: Io) => Promise<void>;
|
|
43
|
+
/** How the vault and session commands read what a person types; tests replace it. */
|
|
44
|
+
export interface VaultInput {
|
|
45
|
+
/** True when a person types on a terminal (hidden prompts), false when stdin is a pipe. */
|
|
46
|
+
isTty(): boolean;
|
|
47
|
+
hidden(prompt: string): Promise<string>;
|
|
48
|
+
visible(prompt: string): Promise<string>;
|
|
49
|
+
/** All of stdin. */
|
|
50
|
+
stdin(): Promise<string>;
|
|
51
|
+
}
|
|
52
|
+
export interface CliDeps {
|
|
53
|
+
/** The functions the commands call: turno-browser's own, or a test's fakes. */
|
|
54
|
+
api: BrowserFunctions;
|
|
55
|
+
/** True when a person can answer a question (a terminal, and no --no-input or agent session). */
|
|
56
|
+
interactive(io: Io): boolean;
|
|
57
|
+
/** Ask a yes/no question; true only for an explicit yes. */
|
|
58
|
+
confirm(question: string, io: Io): Promise<boolean>;
|
|
59
|
+
openUrl?: OpenUrl;
|
|
60
|
+
input?: VaultInput;
|
|
61
|
+
/** All of stdin, for `do <id> type <ref> -`. */
|
|
62
|
+
stdin?: () => Promise<string>;
|
|
63
|
+
}
|
|
64
|
+
/** Everything a command gets: the host and the dependencies. */
|
|
65
|
+
export interface Ctx {
|
|
66
|
+
host: BrowserCliHost;
|
|
67
|
+
names: CliNames;
|
|
68
|
+
deps: CliDeps;
|
|
69
|
+
api: BrowserFunctions;
|
|
70
|
+
}
|
|
71
|
+
export declare function context(host: BrowserCliHost, deps: CliDeps): Ctx;
|
|
72
|
+
/** `open` on macOS, `xdg-open` elsewhere, detached so the command can exit. */
|
|
73
|
+
export declare const openInBrowser: OpenUrl;
|
|
74
|
+
/** turno-browser on its own: no mcp.toml, no gateway; `do` goes through the background server. */
|
|
75
|
+
export declare function standaloneHost(doDeps?: BackgroundDoDeps): BrowserCliHost;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import { STANDALONE_NAMES } from "../names.js";
|
|
3
|
+
import { backgroundTransport, defaultBackgroundDoDeps } from "./do-background.js";
|
|
4
|
+
export function context(host, deps) {
|
|
5
|
+
return { host, names: host.names, deps, api: deps.api };
|
|
6
|
+
}
|
|
7
|
+
/** `open` on macOS, `xdg-open` elsewhere, detached so the command can exit. */
|
|
8
|
+
export const openInBrowser = async (url, io) => {
|
|
9
|
+
if (!/^https?:\/\//.test(url))
|
|
10
|
+
throw new Error(`refusing to open ${JSON.stringify(url)}: only http(s) links`);
|
|
11
|
+
const command = process.platform === "darwin" ? "open" : "xdg-open";
|
|
12
|
+
await new Promise((resolve, reject) => {
|
|
13
|
+
const child = spawn(command, [url], { stdio: "ignore", env: io.env, detached: true });
|
|
14
|
+
child.on("error", reject);
|
|
15
|
+
child.on("spawn", () => {
|
|
16
|
+
child.unref();
|
|
17
|
+
resolve();
|
|
18
|
+
});
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
/** The line `add` prints for wiring an agent: `setup` prints the config it names. */
|
|
22
|
+
function setupLine(id) {
|
|
23
|
+
return `turno-browser setup claude-code --identity ${id}`;
|
|
24
|
+
}
|
|
25
|
+
/** turno-browser on its own: no mcp.toml, no gateway; `do` goes through the background server. */
|
|
26
|
+
export function standaloneHost(doDeps = defaultBackgroundDoDeps) {
|
|
27
|
+
return {
|
|
28
|
+
names: STANDALONE_NAMES,
|
|
29
|
+
footer: `Identities are persistent Chrome profiles that turno-browser-mcp serves to your coding agents.
|
|
30
|
+
Ids use lowercase letters, digits, "-" and "_", for example "school". Every identity keeps its
|
|
31
|
+
settings in identity.json (turno-browser show <id>), its sign-ins in its own profile, and its
|
|
32
|
+
credentials in the secret store (turno-browser vault).`,
|
|
33
|
+
addSummary: "create an identity (pinned to this time zone) and print how an agent uses it",
|
|
34
|
+
connect(id, _settings, io) {
|
|
35
|
+
io.out("\nConnect an agent to it:");
|
|
36
|
+
io.out(` ${setupLine(id)} # prints the config for Claude Code (codex, cursor, ...)`);
|
|
37
|
+
io.out(` turno-browser-mcp --identity ${id} # the stdio command any MCP client runs`);
|
|
38
|
+
io.out(`Sign in to your sites once with \`turno-browser login ${id}\`.`);
|
|
39
|
+
},
|
|
40
|
+
restart: "a new agent session",
|
|
41
|
+
doTransport: backgroundTransport(doDeps),
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
//# sourceMappingURL=context.js.map
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { type Owner } from "turno-core/cli";
|
|
2
|
+
import { backgroundStatus, openBackgroundSession } from "../client.js";
|
|
3
|
+
import { identityExists } from "../identities.js";
|
|
4
|
+
import type { DoTransport } from "./do.js";
|
|
5
|
+
/**
|
|
6
|
+
* How `turno-browser do` reaches an identity's browser without turno: through the identity's
|
|
7
|
+
* background server (`turno-browser-mcp --identity <id> --background`), which the first command
|
|
8
|
+
* starts and which exits after its idle time. Each command is one MCP session of that server,
|
|
9
|
+
* the way an agent's tool call goes, so the identity's rules apply (tab locks or the one-session
|
|
10
|
+
* lease, humanize, recordings, the vault).
|
|
11
|
+
*
|
|
12
|
+
* What makes several commands one session: the first command saves the server's session id in a
|
|
13
|
+
* private file, the next ones continue it, and the server ends it when the process the commands
|
|
14
|
+
* belong to exits (the coding agent or the terminal's shell, `sessionOwner`). Inside turno,
|
|
15
|
+
* `turno browser do` goes through mcpd instead, with the same owner and session rules.
|
|
16
|
+
*/
|
|
17
|
+
/** What the transport calls; tests replace them. */
|
|
18
|
+
export interface BackgroundDoDeps {
|
|
19
|
+
/** Who the commands belong to (`sessionOwner`). */
|
|
20
|
+
owner(env: NodeJS.ProcessEnv): Owner;
|
|
21
|
+
identityExists: typeof identityExists;
|
|
22
|
+
backgroundStatus: typeof backgroundStatus;
|
|
23
|
+
openBackgroundSession: typeof openBackgroundSession;
|
|
24
|
+
}
|
|
25
|
+
export declare const defaultBackgroundDoDeps: BackgroundDoDeps;
|
|
26
|
+
/** The background transport of `turno-browser do`. */
|
|
27
|
+
export declare function backgroundTransport(deps?: BackgroundDoDeps): DoTransport;
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
import { readFileSync, readdirSync, rmSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { CliError, ownerName, sessionName, sessionOwner, str, withLock } from "turno-core/cli";
|
|
4
|
+
import { tildify, writePrivateFile } from "turno-core/paths";
|
|
5
|
+
import { pidAlive, readProcTable } from "turno-core/process";
|
|
6
|
+
import { backgroundDir } from "../background-state.js";
|
|
7
|
+
import { backgroundStatus, openBackgroundSession } from "../client.js";
|
|
8
|
+
import { identityExists } from "../identities.js";
|
|
9
|
+
export const defaultBackgroundDoDeps = {
|
|
10
|
+
owner: (env) => sessionOwner(readProcTable(), process.ppid, Number(env.TURNO_SESSION_PID) || undefined),
|
|
11
|
+
identityExists,
|
|
12
|
+
backgroundStatus,
|
|
13
|
+
openBackgroundSession,
|
|
14
|
+
};
|
|
15
|
+
const HELP = {
|
|
16
|
+
intro: `Runs one action on the identity's browser through its background server, a turno-browser-mcp
|
|
17
|
+
that serves the identity to the shell (turno-browser background shows it). The first action starts
|
|
18
|
+
it; it exits after 15 minutes without a request (TURNO_BROWSER_BACKGROUND_IDLE changes that). While
|
|
19
|
+
it runs it alone drives the identity's browser: an agent's turno-browser-mcp for the same identity
|
|
20
|
+
answers with an error that names it. With turno installed, turno browser do goes through mcpd
|
|
21
|
+
instead.`,
|
|
22
|
+
session: `Session: the commands one agent or one terminal shell runs are one session, so they keep their
|
|
23
|
+
tab and their place in the tab locks ("sharing": "tabs") or hold the identity's one-session lease
|
|
24
|
+
("sharing": "session"). The session belongs to the process that runs the commands: the coding
|
|
25
|
+
agent (each of its shell commands passes through a short-lived shell) or your terminal's shell.
|
|
26
|
+
It ends when that process exits, with 'end' or 'close', or when the background server exits.
|
|
27
|
+
--session <name> (or TURNO_BROWSER_SESSION) keeps several sessions apart under one owner, for
|
|
28
|
+
example two subagents each in its own tab.`,
|
|
29
|
+
output: `Output: url: and title: lines, then what the action returned; errors go to stderr with exit 1.
|
|
30
|
+
--snapshot after an action prints the snapshot too; --json prints one object.
|
|
31
|
+
--timeout <seconds> (default 120) bounds one action; attach mode can wait for Chrome's consent
|
|
32
|
+
dialog.`,
|
|
33
|
+
};
|
|
34
|
+
/** Where the sessions of `do` are saved: the background server's private folder. */
|
|
35
|
+
function sessionsDir(id, env) {
|
|
36
|
+
return join(backgroundDir(id, env), "do");
|
|
37
|
+
}
|
|
38
|
+
function readSaved(file) {
|
|
39
|
+
try {
|
|
40
|
+
const s = JSON.parse(readFileSync(file, "utf8"));
|
|
41
|
+
if (s.v !== 1 || typeof s.sessionId !== "string" || typeof s.serverPid !== "number" || !s.owner)
|
|
42
|
+
return undefined;
|
|
43
|
+
return s;
|
|
44
|
+
}
|
|
45
|
+
catch {
|
|
46
|
+
return undefined;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
function dropSaved(file) {
|
|
50
|
+
rmSync(file, { force: true });
|
|
51
|
+
}
|
|
52
|
+
/** Removes the saved sessions whose owner exited (the server ended those sessions already). */
|
|
53
|
+
function sweepSaved(dir) {
|
|
54
|
+
let files;
|
|
55
|
+
try {
|
|
56
|
+
files = readdirSync(dir);
|
|
57
|
+
}
|
|
58
|
+
catch {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
for (const f of files) {
|
|
62
|
+
const m = /^(\d+)\.[^.]+\.json$/.exec(f);
|
|
63
|
+
if (m && !pidAlive(Number(m[1])))
|
|
64
|
+
dropSaved(join(dir, f));
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
function message(err) {
|
|
68
|
+
return (err?.message ?? String(err)).replace(/^(Error: )?/, "");
|
|
69
|
+
}
|
|
70
|
+
/** The background transport of `turno-browser do`. */
|
|
71
|
+
export function backgroundTransport(deps = defaultBackgroundDoDeps) {
|
|
72
|
+
return {
|
|
73
|
+
help: HELP,
|
|
74
|
+
flags: {},
|
|
75
|
+
async target(id, p, io) {
|
|
76
|
+
const api = deps;
|
|
77
|
+
const env = io.env;
|
|
78
|
+
if (!api.identityExists(id, env))
|
|
79
|
+
throw new CliError(`browser identity "${id}" does not exist (turno-browser add ${id})`);
|
|
80
|
+
const owner = deps.owner(env);
|
|
81
|
+
const name = sessionName(str(p, "session"), env);
|
|
82
|
+
const dir = sessionsDir(id, env);
|
|
83
|
+
const file = join(dir, `${owner.pid}.${name}.json`);
|
|
84
|
+
sweepSaved(dir);
|
|
85
|
+
/** The saved session when its server still runs, and that server's pid. */
|
|
86
|
+
const current = () => {
|
|
87
|
+
const saved = readSaved(file);
|
|
88
|
+
const server = api.backgroundStatus(id, { env });
|
|
89
|
+
return saved && server && saved.serverPid === server.pid && saved.owner.pid === owner.pid ? saved : undefined;
|
|
90
|
+
};
|
|
91
|
+
/** Open the saved session again (or a new one), without starting a server. */
|
|
92
|
+
const reopen = async (saved) => await api
|
|
93
|
+
.openBackgroundSession(id, {
|
|
94
|
+
env,
|
|
95
|
+
owner: owner.pid,
|
|
96
|
+
start: false,
|
|
97
|
+
session: {
|
|
98
|
+
id: saved.sessionId,
|
|
99
|
+
...(saved.protocolVersion ? { protocolVersion: saved.protocolVersion } : {}),
|
|
100
|
+
},
|
|
101
|
+
})
|
|
102
|
+
.catch(() => undefined);
|
|
103
|
+
return {
|
|
104
|
+
head: { identity: id, server: "background", profile: null },
|
|
105
|
+
info: (resumed) => ({ name, ownerPid: owner.pid, owner: ownerName(owner), resumed }),
|
|
106
|
+
describe() {
|
|
107
|
+
const saved = current();
|
|
108
|
+
const server = api.backgroundStatus(id, { env });
|
|
109
|
+
return {
|
|
110
|
+
open: saved !== undefined,
|
|
111
|
+
lines: [
|
|
112
|
+
`identity: ${id}`,
|
|
113
|
+
server
|
|
114
|
+
? `server: background server (pid ${server.pid}) since ${server.startedAt}`
|
|
115
|
+
: "server: not running; the next action starts it",
|
|
116
|
+
`session: ${name}, owned by ${ownerName(owner)} (pid ${owner.pid})`,
|
|
117
|
+
saved
|
|
118
|
+
? `state: open since ${saved.createdAt}, last used ${saved.usedAt}`
|
|
119
|
+
: "state: none yet; the next action opens it",
|
|
120
|
+
`ends: when pid ${owner.pid} exits, with 'end' or 'close', or when the background server exits`,
|
|
121
|
+
`file: ${tildify(file, env)}`,
|
|
122
|
+
],
|
|
123
|
+
};
|
|
124
|
+
},
|
|
125
|
+
async end() {
|
|
126
|
+
const saved = readSaved(file);
|
|
127
|
+
if (!saved)
|
|
128
|
+
return false;
|
|
129
|
+
const live = current();
|
|
130
|
+
if (live)
|
|
131
|
+
await (await reopen(live))?.end();
|
|
132
|
+
dropSaved(file);
|
|
133
|
+
return true;
|
|
134
|
+
},
|
|
135
|
+
session: (fn) => withLock(file, env, async () => {
|
|
136
|
+
let saved = current();
|
|
137
|
+
if (!saved)
|
|
138
|
+
dropSaved(file); // another server, or none: that session is gone
|
|
139
|
+
let s;
|
|
140
|
+
try {
|
|
141
|
+
s = await api.openBackgroundSession(id, {
|
|
142
|
+
env,
|
|
143
|
+
owner: owner.pid,
|
|
144
|
+
clientName: "turno-browser-do",
|
|
145
|
+
...(saved
|
|
146
|
+
? {
|
|
147
|
+
session: {
|
|
148
|
+
id: saved.sessionId,
|
|
149
|
+
...(saved.protocolVersion ? { protocolVersion: saved.protocolVersion } : {}),
|
|
150
|
+
},
|
|
151
|
+
}
|
|
152
|
+
: {}),
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
catch (err) {
|
|
156
|
+
throw new CliError(message(err));
|
|
157
|
+
}
|
|
158
|
+
if (!s.resumed)
|
|
159
|
+
saved = undefined;
|
|
160
|
+
let used = false;
|
|
161
|
+
let ended = false;
|
|
162
|
+
const session = {
|
|
163
|
+
resumed: s.resumed,
|
|
164
|
+
async call(tool, args, timeoutMs) {
|
|
165
|
+
let result;
|
|
166
|
+
try {
|
|
167
|
+
result = await s.callTool(tool, args, { timeoutMs });
|
|
168
|
+
}
|
|
169
|
+
catch (err) {
|
|
170
|
+
// A session this command opened and could not use goes at once.
|
|
171
|
+
if (!s.resumed && !used)
|
|
172
|
+
await s.end();
|
|
173
|
+
throw new CliError(`${tool}: ${message(err)}`);
|
|
174
|
+
}
|
|
175
|
+
if (!used && !ended) {
|
|
176
|
+
const now = new Date().toISOString();
|
|
177
|
+
const next = {
|
|
178
|
+
v: 1,
|
|
179
|
+
identity: id,
|
|
180
|
+
serverPid: s.state.pid,
|
|
181
|
+
sessionId: s.id,
|
|
182
|
+
...(s.protocolVersion ? { protocolVersion: s.protocolVersion } : {}),
|
|
183
|
+
owner,
|
|
184
|
+
name,
|
|
185
|
+
createdAt: saved?.createdAt ?? now,
|
|
186
|
+
usedAt: now,
|
|
187
|
+
};
|
|
188
|
+
writePrivateFile(file, `${JSON.stringify(next, null, 2)}\n`, env);
|
|
189
|
+
}
|
|
190
|
+
used = true;
|
|
191
|
+
return result;
|
|
192
|
+
},
|
|
193
|
+
async end() {
|
|
194
|
+
ended = true;
|
|
195
|
+
await s.end();
|
|
196
|
+
dropSaved(file);
|
|
197
|
+
},
|
|
198
|
+
};
|
|
199
|
+
try {
|
|
200
|
+
return await fn(session);
|
|
201
|
+
}
|
|
202
|
+
finally {
|
|
203
|
+
// Never end the session here: the next command of this owner continues it.
|
|
204
|
+
if (!ended)
|
|
205
|
+
await s.close();
|
|
206
|
+
}
|
|
207
|
+
}),
|
|
208
|
+
};
|
|
209
|
+
},
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
//# sourceMappingURL=do-background.js.map
|
package/dist/cli/do.d.ts
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { type FlagSpec, type Io, type Parsed } from "turno-core/cli";
|
|
2
|
+
import type { Ctx } from "./context.js";
|
|
3
|
+
/**
|
|
4
|
+
* `<browser> do <id> <action>`: one browser action on an identity's browser from the shell, for
|
|
5
|
+
* coding agents that prefer commands over MCP tools. Each action is one stock (or turno) tool call
|
|
6
|
+
* to the identity's turno-browser-mcp, the server agents use, so every rule of the identity
|
|
7
|
+
* applies as it does to an agent: the tab locks or the one-session lease, attach mode, read mode's
|
|
8
|
+
* refusals, humanize, recordings, the vault. How the call reaches the server, and what makes
|
|
9
|
+
* several commands one session, is the host's transport (`DoTransport`): inside turno, mcpd.
|
|
10
|
+
*
|
|
11
|
+
* The output is short and stable: `url:` and `title:` lines, then what the action returned. The
|
|
12
|
+
* code upstream ran and the snapshot file an action saves on the server are left out; a snapshot
|
|
13
|
+
* comes from `snapshot` (or `--snapshot` after an action).
|
|
14
|
+
*/
|
|
15
|
+
/** `do --json`: one object per command. */
|
|
16
|
+
export declare const DO_JSON: import("turno-core/cli").JsonOutput;
|
|
17
|
+
/** A tool call's result as an MCP client returns it. */
|
|
18
|
+
export interface ToolResult {
|
|
19
|
+
content?: unknown;
|
|
20
|
+
isError?: boolean | undefined;
|
|
21
|
+
}
|
|
22
|
+
/** The `session` field of `do --json`. */
|
|
23
|
+
export interface DoSessionInfo {
|
|
24
|
+
name: string;
|
|
25
|
+
ownerPid: number;
|
|
26
|
+
owner: string;
|
|
27
|
+
/** True when the command continued a saved session, false when it opened one, null for `end`. */
|
|
28
|
+
resumed: boolean | null;
|
|
29
|
+
}
|
|
30
|
+
/** One session of `do`, open for one command. */
|
|
31
|
+
export interface DoSession {
|
|
32
|
+
/** True when this continues a session an earlier command opened. */
|
|
33
|
+
readonly resumed: boolean;
|
|
34
|
+
/**
|
|
35
|
+
* Call one tool in the session, within `timeoutMs`. A call the server refuses or cannot take
|
|
36
|
+
* throws a CliError that names the tool; the session is kept for the next command.
|
|
37
|
+
*/
|
|
38
|
+
call(tool: string, args: Record<string, unknown>, timeoutMs: number): Promise<ToolResult>;
|
|
39
|
+
/** End the session now (after `close`): the next command opens a new one. */
|
|
40
|
+
end(): Promise<void>;
|
|
41
|
+
}
|
|
42
|
+
/** The server of one identity and the session the commands of this shell or agent belong to. */
|
|
43
|
+
export interface DoTarget {
|
|
44
|
+
/** What every `--json` object starts with: the identity, and the server and profile when there are. */
|
|
45
|
+
head: {
|
|
46
|
+
identity: string;
|
|
47
|
+
server: string | null;
|
|
48
|
+
profile: string | null;
|
|
49
|
+
};
|
|
50
|
+
/** The `session` field of `--json`. */
|
|
51
|
+
info(resumed: boolean | null): DoSessionInfo;
|
|
52
|
+
/** `do <id> session`: whether a session is saved, and the lines that describe it. */
|
|
53
|
+
describe(io: Io): {
|
|
54
|
+
open: boolean;
|
|
55
|
+
lines: string[];
|
|
56
|
+
};
|
|
57
|
+
/** `do <id> end`: end the saved session at once; false when there was none. */
|
|
58
|
+
end(): Promise<boolean>;
|
|
59
|
+
/** Open a session, or continue the saved one, run `fn` in it, and keep it for the next command. */
|
|
60
|
+
session<T>(fn: (s: DoSession) => Promise<T>): Promise<T>;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* How `do` reaches an identity's server: inside turno, mcpd (each command one gateway session of
|
|
64
|
+
* the profile); on its own, a background server per identity.
|
|
65
|
+
*/
|
|
66
|
+
export interface DoTransport {
|
|
67
|
+
/** Help paragraphs: how an action reaches the server, how a session lives, the output and flags. */
|
|
68
|
+
help: {
|
|
69
|
+
intro: string;
|
|
70
|
+
session: string;
|
|
71
|
+
output: string;
|
|
72
|
+
};
|
|
73
|
+
/** Flags of its own (turno's `--profile`), parsed with the action's. */
|
|
74
|
+
flags: FlagSpec;
|
|
75
|
+
/** The server that serves identity `id`, and this caller's session; throws when there is none. */
|
|
76
|
+
target(id: string, p: Parsed, io: Io): Promise<DoTarget>;
|
|
77
|
+
}
|
|
78
|
+
/** Every action name, for the short form and for completion. */
|
|
79
|
+
export declare const ACTION_NAMES: readonly string[];
|
|
80
|
+
/** The flags of `do`, for completion: the common ones, the transport's, then the actions'. */
|
|
81
|
+
export declare function doFlagNames(c: Ctx): string[];
|
|
82
|
+
export declare function doSynopsis(_c: Ctx): string;
|
|
83
|
+
export declare function doDetails(c: Ctx): string;
|
|
84
|
+
/** `<browser> do <id> <action> [args]`. */
|
|
85
|
+
export declare function browserDo(c: Ctx, args: string[], io: Io): Promise<number>;
|