turno-browser 0.2.0 → 0.4.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 +228 -0
- package/README.md +205 -16
- package/dist/act-model.d.ts +85 -0
- package/dist/act-model.js +116 -0
- package/dist/act.d.ts +37 -0
- package/dist/act.js +599 -0
- package/dist/api.d.ts +96 -0
- package/dist/api.js +88 -0
- package/dist/argv.d.ts +55 -3
- package/dist/argv.js +190 -10
- package/dist/batch.d.ts +19 -0
- package/dist/batch.js +148 -0
- package/dist/bench.d.ts +1 -1
- package/dist/bench.js +21 -5
- package/dist/bin/mcp.js +108 -12
- package/dist/captcha.d.ts +77 -0
- package/dist/captcha.js +333 -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/detectors.d.ts +11 -0
- package/dist/detectors.js +46 -4
- package/dist/diagnose.d.ts +45 -0
- package/dist/diagnose.js +326 -0
- package/dist/doctor.d.ts +85 -0
- package/dist/doctor.js +556 -0
- 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 +270 -0
- package/dist/engine-lightpanda.d.ts +72 -0
- package/dist/engine-lightpanda.js +292 -0
- package/dist/engine.d.ts +119 -0
- package/dist/engine.js +355 -0
- package/dist/flows.d.ts +127 -0
- package/dist/flows.js +294 -0
- package/dist/governor.d.ts +119 -0
- package/dist/governor.js +325 -0
- package/dist/health.d.ts +60 -0
- package/dist/health.js +86 -0
- package/dist/host.d.ts +95 -0
- package/dist/host.js +1181 -0
- package/dist/human-input.d.ts +84 -0
- package/dist/human-input.js +443 -0
- package/dist/humanize.d.ts +123 -0
- package/dist/humanize.js +241 -0
- package/dist/identities.d.ts +23 -10
- package/dist/identities.js +45 -37
- package/dist/idle-timer.d.ts +12 -0
- package/dist/idle-timer.js +25 -0
- package/dist/ids.d.ts +9 -0
- package/dist/ids.js +34 -0
- package/dist/index.d.ts +21 -6
- package/dist/index.js +19 -18
- package/dist/lifecycle.d.ts +99 -0
- package/dist/lifecycle.js +172 -0
- package/dist/live-client.d.ts +22 -0
- package/dist/live-client.js +93 -0
- package/dist/live-page.d.ts +11 -0
- package/dist/live-page.js +143 -0
- package/dist/live-server.d.ts +173 -0
- package/dist/live-server.js +640 -0
- package/dist/live.d.ts +120 -0
- package/dist/live.js +203 -0
- package/dist/login-tool.d.ts +45 -0
- package/dist/login-tool.js +233 -0
- package/dist/login.d.ts +8 -3
- package/dist/login.js +65 -26
- 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 +263 -0
- package/dist/opt-in-settings.d.ts +69 -0
- package/dist/opt-in-settings.js +151 -0
- package/dist/patchright.d.ts +4 -2
- package/dist/patchright.js +4 -2
- 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 +34 -0
- package/dist/redact.js +135 -0
- package/dist/replay.d.ts +20 -0
- package/dist/replay.js +382 -0
- package/dist/reputation.d.ts +89 -0
- package/dist/reputation.js +203 -0
- package/dist/resources.d.ts +76 -0
- package/dist/resources.js +341 -0
- package/dist/runtime-lock.d.ts +59 -0
- package/dist/runtime-lock.js +131 -0
- package/dist/sealed.d.ts +8 -0
- package/dist/sealed.js +96 -0
- package/dist/session-export.d.ts +67 -0
- package/dist/session-export.js +194 -0
- package/dist/session.d.ts +20 -0
- package/dist/session.js +30 -0
- package/dist/settings.d.ts +246 -0
- package/dist/settings.js +530 -0
- package/dist/sharing.d.ts +96 -0
- package/dist/sharing.js +269 -0
- package/dist/snippet.d.ts +8 -0
- package/dist/snippet.js +22 -0
- package/dist/tab-locks.d.ts +63 -0
- package/dist/tab-locks.js +179 -0
- package/dist/tools.d.ts +111 -0
- package/dist/tools.js +59 -0
- package/dist/totp.d.ts +32 -0
- package/dist/totp.js +82 -0
- package/dist/tuning.d.ts +76 -0
- package/dist/tuning.js +69 -0
- package/dist/units.d.ts +8 -0
- package/dist/units.js +20 -0
- package/dist/upstream.d.ts +243 -0
- package/dist/upstream.js +46 -0
- package/dist/vault.d.ts +91 -0
- package/dist/vault.js +273 -0
- package/npm-shrinkwrap.json +1074 -3
- package/package.json +5 -4
- package/dist/paths.d.ts +0 -17
- package/dist/paths.js +0 -17
package/dist/tools.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { actTool, extractTool, observeTool } from "./act.js";
|
|
2
|
+
import { batchTool } from "./batch.js";
|
|
3
|
+
import { solveCaptchaTool } from "./captcha.js";
|
|
4
|
+
import { loginTool } from "./login-tool.js";
|
|
5
|
+
import { recordTool } from "./recorder.js";
|
|
6
|
+
import { flowsTool, replayTool } from "./replay.js";
|
|
7
|
+
import { upstream, } from "./upstream.js";
|
|
8
|
+
/** The tools turno adds to every session. */
|
|
9
|
+
export const TURNO_TOOLS = [batchTool(), loginTool(), recordTool(), replayTool(), flowsTool()];
|
|
10
|
+
/**
|
|
11
|
+
* The tools an identity lists only after the person turns their feature on in identity.json
|
|
12
|
+
* (format 8): natural language actions (`act`) and captcha solving (`captcha`). Off, they are
|
|
13
|
+
* not listed at all, so a default session's tool list stays as it was.
|
|
14
|
+
*/
|
|
15
|
+
export function optInTools(settings) {
|
|
16
|
+
return [
|
|
17
|
+
...(settings?.act ? [actTool(), extractTool(), observeTool()] : []),
|
|
18
|
+
...(settings?.captcha ? [solveCaptchaTool()] : []),
|
|
19
|
+
];
|
|
20
|
+
}
|
|
21
|
+
/** Every opt-in tool, for the name checks (contract test). */
|
|
22
|
+
export function allOptInTools() {
|
|
23
|
+
return optInTools({ act: {}, captcha: {} });
|
|
24
|
+
}
|
|
25
|
+
/** The zod of the bundle, to build a turno tool's `inputSchema` with. */
|
|
26
|
+
export function turnoZod() {
|
|
27
|
+
return upstream().utils.z;
|
|
28
|
+
}
|
|
29
|
+
const NAME = /^[a-z][a-z0-9_]*$/;
|
|
30
|
+
/** Throws when a turno tool would shadow an upstream tool or another turno tool. */
|
|
31
|
+
export function checkTurnoTools(turno, upstreamTools) {
|
|
32
|
+
const stock = new Set(upstreamTools.map((t) => t.schema.name));
|
|
33
|
+
const seen = new Set();
|
|
34
|
+
for (const tool of turno) {
|
|
35
|
+
const name = tool.schema.name;
|
|
36
|
+
if (!NAME.test(name))
|
|
37
|
+
throw new Error(`turno tool name "${name}" must match ${NAME}`);
|
|
38
|
+
if (stock.has(name))
|
|
39
|
+
throw new Error(`turno tool "${name}" has the name of a Playwright MCP tool`);
|
|
40
|
+
if (seen.has(name))
|
|
41
|
+
throw new Error(`turno tool "${name}" is registered twice`);
|
|
42
|
+
seen.add(name);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
/** Upstream's argument error, word for word, for a turno tool. */
|
|
46
|
+
export function parseArguments(tool, args) {
|
|
47
|
+
const { z } = upstream().utils;
|
|
48
|
+
const schema = tool.schema.inputSchema;
|
|
49
|
+
const { _meta: _ignored, ...rest } = args;
|
|
50
|
+
try {
|
|
51
|
+
return { params: schema.parse(rest) };
|
|
52
|
+
}
|
|
53
|
+
catch (error) {
|
|
54
|
+
if (error instanceof z.ZodError)
|
|
55
|
+
return { error: `Invalid arguments for tool "${tool.schema.name}":\n${z.prettifyError(error)}` };
|
|
56
|
+
throw error;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
//# sourceMappingURL=tools.js.map
|
package/dist/totp.d.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Time-based one-time passwords (RFC 6238, on HOTP of RFC 4226), with node:crypto only, for the
|
|
3
|
+
* credential vault. A secret comes as the base32 text a site shows next to its QR
|
|
4
|
+
* code, or as the `otpauth://totp/...` URI the QR code holds.
|
|
5
|
+
*/
|
|
6
|
+
export type TotpAlgorithm = "SHA1" | "SHA256" | "SHA512";
|
|
7
|
+
export interface TotpParams {
|
|
8
|
+
/** The shared key, base32 without padding, upper case. */
|
|
9
|
+
secret: string;
|
|
10
|
+
algorithm: TotpAlgorithm;
|
|
11
|
+
/** Code length, 6 to 8. */
|
|
12
|
+
digits: number;
|
|
13
|
+
/** Seconds per code. */
|
|
14
|
+
period: number;
|
|
15
|
+
}
|
|
16
|
+
export declare class TotpError extends Error {
|
|
17
|
+
constructor(message: string);
|
|
18
|
+
}
|
|
19
|
+
/** Decode RFC 4648 base32; spaces, dashes, lower case and padding are accepted. */
|
|
20
|
+
export declare function base32Decode(text: string): Buffer;
|
|
21
|
+
/**
|
|
22
|
+
* Parse what a person pastes for a site's authenticator: a base32 secret (SHA1, 6 digits,
|
|
23
|
+
* 30 seconds, what nearly every site uses) or an `otpauth://totp/` URI with its own algorithm,
|
|
24
|
+
* digits and period. Errors never quote the input.
|
|
25
|
+
*/
|
|
26
|
+
export declare function parseTotpSecret(input: string): TotpParams;
|
|
27
|
+
/** HOTP (RFC 4226): the code for one counter value. */
|
|
28
|
+
export declare function hotp(key: Buffer, counter: number, algorithm?: TotpAlgorithm, digits?: number): string;
|
|
29
|
+
/** The code at `now` (milliseconds since the epoch). */
|
|
30
|
+
export declare function totp(params: TotpParams, now?: number): string;
|
|
31
|
+
/** Seconds left before the code at `now` changes. */
|
|
32
|
+
export declare function totpSecondsLeft(params: TotpParams, now?: number): number;
|
package/dist/totp.js
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { createHmac } from "node:crypto";
|
|
2
|
+
export class TotpError extends Error {
|
|
3
|
+
constructor(message) {
|
|
4
|
+
super(message);
|
|
5
|
+
this.name = "TotpError";
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
const BASE32 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
|
|
9
|
+
/** Decode RFC 4648 base32; spaces, dashes, lower case and padding are accepted. */
|
|
10
|
+
export function base32Decode(text) {
|
|
11
|
+
const clean = text.replace(/[\s-]/g, "").replace(/=+$/, "").toUpperCase();
|
|
12
|
+
if (!clean || /[^A-Z2-7]/.test(clean))
|
|
13
|
+
throw new TotpError("the one-time code secret is not base32 (letters A to Z and digits 2 to 7)");
|
|
14
|
+
let bits = 0;
|
|
15
|
+
let value = 0;
|
|
16
|
+
const out = [];
|
|
17
|
+
for (const ch of clean) {
|
|
18
|
+
value = (value << 5) | BASE32.indexOf(ch);
|
|
19
|
+
bits += 5;
|
|
20
|
+
if (bits >= 8) {
|
|
21
|
+
out.push((value >>> (bits - 8)) & 0xff);
|
|
22
|
+
bits -= 8;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return Buffer.from(out);
|
|
26
|
+
}
|
|
27
|
+
function normalizeSecret(text) {
|
|
28
|
+
const key = base32Decode(text);
|
|
29
|
+
if (key.length < 10)
|
|
30
|
+
throw new TotpError("the one-time code secret is too short (at least 16 base32 characters)");
|
|
31
|
+
return text.replace(/[\s-]/g, "").replace(/=+$/, "").toUpperCase();
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Parse what a person pastes for a site's authenticator: a base32 secret (SHA1, 6 digits,
|
|
35
|
+
* 30 seconds, what nearly every site uses) or an `otpauth://totp/` URI with its own algorithm,
|
|
36
|
+
* digits and period. Errors never quote the input.
|
|
37
|
+
*/
|
|
38
|
+
export function parseTotpSecret(input) {
|
|
39
|
+
const text = input.trim();
|
|
40
|
+
if (!text.toLowerCase().startsWith("otpauth:"))
|
|
41
|
+
return { secret: normalizeSecret(text), algorithm: "SHA1", digits: 6, period: 30 };
|
|
42
|
+
let url;
|
|
43
|
+
try {
|
|
44
|
+
url = new URL(text);
|
|
45
|
+
}
|
|
46
|
+
catch {
|
|
47
|
+
throw new TotpError("the otpauth:// URI cannot be parsed");
|
|
48
|
+
}
|
|
49
|
+
if (url.host.toLowerCase() !== "totp")
|
|
50
|
+
throw new TotpError(`only otpauth://totp/ is supported (this is ${url.host || "an unknown type"})`);
|
|
51
|
+
const secret = url.searchParams.get("secret");
|
|
52
|
+
if (!secret)
|
|
53
|
+
throw new TotpError("the otpauth:// URI has no secret");
|
|
54
|
+
const algorithm = (url.searchParams.get("algorithm") ?? "SHA1").toUpperCase();
|
|
55
|
+
if (algorithm !== "SHA1" && algorithm !== "SHA256" && algorithm !== "SHA512")
|
|
56
|
+
throw new TotpError(`unsupported algorithm ${algorithm} (SHA1, SHA256 or SHA512)`);
|
|
57
|
+
const digits = Number(url.searchParams.get("digits") ?? 6);
|
|
58
|
+
if (!Number.isInteger(digits) || digits < 6 || digits > 8)
|
|
59
|
+
throw new TotpError("digits must be 6, 7 or 8");
|
|
60
|
+
const period = Number(url.searchParams.get("period") ?? 30);
|
|
61
|
+
if (!Number.isInteger(period) || period < 1 || period > 300)
|
|
62
|
+
throw new TotpError("period must be a whole number of seconds from 1 to 300");
|
|
63
|
+
return { secret: normalizeSecret(secret), algorithm, digits, period };
|
|
64
|
+
}
|
|
65
|
+
/** HOTP (RFC 4226): the code for one counter value. */
|
|
66
|
+
export function hotp(key, counter, algorithm = "SHA1", digits = 6) {
|
|
67
|
+
const msg = Buffer.alloc(8);
|
|
68
|
+
msg.writeBigUInt64BE(BigInt(counter));
|
|
69
|
+
const mac = createHmac(algorithm.toLowerCase(), key).update(msg).digest();
|
|
70
|
+
const offset = mac[mac.length - 1] & 0x0f;
|
|
71
|
+
const bin = ((mac[offset] & 0x7f) << 24) | (mac[offset + 1] << 16) | (mac[offset + 2] << 8) | mac[offset + 3];
|
|
72
|
+
return String(bin % 10 ** digits).padStart(digits, "0");
|
|
73
|
+
}
|
|
74
|
+
/** The code at `now` (milliseconds since the epoch). */
|
|
75
|
+
export function totp(params, now = Date.now()) {
|
|
76
|
+
return hotp(base32Decode(params.secret), Math.floor(now / 1000 / params.period), params.algorithm, params.digits);
|
|
77
|
+
}
|
|
78
|
+
/** Seconds left before the code at `now` changes. */
|
|
79
|
+
export function totpSecondsLeft(params, now = Date.now()) {
|
|
80
|
+
return params.period - (Math.floor(now / 1000) % params.period);
|
|
81
|
+
}
|
|
82
|
+
//# sourceMappingURL=totp.js.map
|
package/dist/tuning.d.ts
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import type { HumanizeLevel, HumanizeSettings } from "./humanize.js";
|
|
2
|
+
import { type ResourcePolicy } from "./resources.js";
|
|
3
|
+
import type { IdentitySettings } from "./settings.js";
|
|
4
|
+
import type { SharingMode } from "./sharing.js";
|
|
5
|
+
import type { TabLockPolicy } from "./tab-locks.js";
|
|
6
|
+
/**
|
|
7
|
+
* How the host runs one session's Chrome: the tab governor, background tab
|
|
8
|
+
* throttling, the wait after each action, and resource blocking and caching. Each value comes from, in order of precedence, a `turno-browser-mcp` flag, the
|
|
9
|
+
* identity's identity.json, then the defaults below. identity.json is read at every launch,
|
|
10
|
+
* like the rest of it.
|
|
11
|
+
*/
|
|
12
|
+
export interface TabPolicy {
|
|
13
|
+
/** Most pages open at once; past it the least recently used page closes. 0: no cap. */
|
|
14
|
+
max: number;
|
|
15
|
+
/** Close a popup the agent did not open (`"close"`), or leave it (`"keep"`). */
|
|
16
|
+
popups: "close" | "keep";
|
|
17
|
+
/** A page unused this long is parked on about:blank; 0: never. */
|
|
18
|
+
parkMs: number;
|
|
19
|
+
/** A page unused this long is closed; 0: never. */
|
|
20
|
+
closeMs: number;
|
|
21
|
+
}
|
|
22
|
+
export interface Tuning {
|
|
23
|
+
tabs: TabPolicy;
|
|
24
|
+
/**
|
|
25
|
+
* False (the default): Chrome runs every tab at full speed, as Playwright launches it. True:
|
|
26
|
+
* Chrome throttles the tabs that are not in front, and the host brings the agent's tab to the
|
|
27
|
+
* front before each call (see `launchTuning`).
|
|
28
|
+
*/
|
|
29
|
+
backgroundThrottling: boolean;
|
|
30
|
+
/**
|
|
31
|
+
* The wait after each action for the work it started (Playwright's `timeouts.settle`), from
|
|
32
|
+
* identity.json `settle`; undefined keeps Playwright's (500 ms, or the user's own setting).
|
|
33
|
+
*/
|
|
34
|
+
settleMs?: number;
|
|
35
|
+
/** Opt-in resource blocking and cache, from identity.json `block` and `cache`. */
|
|
36
|
+
resources: ResourcePolicy;
|
|
37
|
+
/** Human-like input (`humanize.ts`), from `--humanize` or identity.json `humanize`; off by default. */
|
|
38
|
+
humanize: HumanizeSettings;
|
|
39
|
+
/**
|
|
40
|
+
* `"session"` (the default): one backend serves every call, as mcpd's `exclusive = true` lets
|
|
41
|
+
* one session at a time. `"tabs"`: each agent session gets its own tabs, with locks (`sharing.ts`).
|
|
42
|
+
*/
|
|
43
|
+
sharing: SharingMode;
|
|
44
|
+
/** With `"sharing": "tabs"`: the lock wait and idle release of identity.json `tabLocks`. */
|
|
45
|
+
tabLocks: Partial<TabLockPolicy>;
|
|
46
|
+
}
|
|
47
|
+
/** The defaults, documented on the browser identities page. */
|
|
48
|
+
export declare const DEFAULT_TABS: Readonly<TabPolicy>;
|
|
49
|
+
/** What `turno-browser-mcp` flags set; each wins over identity.json. */
|
|
50
|
+
export interface TuningFlags {
|
|
51
|
+
maxTabs?: number;
|
|
52
|
+
popups?: "close" | "keep";
|
|
53
|
+
parkAfterMs?: number;
|
|
54
|
+
closeAfterMs?: number;
|
|
55
|
+
backgroundThrottling?: boolean;
|
|
56
|
+
humanize?: HumanizeLevel;
|
|
57
|
+
sharing?: SharingMode;
|
|
58
|
+
}
|
|
59
|
+
/** The tuning of a session: flags, then identity.json, then the defaults. */
|
|
60
|
+
export declare function resolveTuning(settings: IdentitySettings | undefined, flags?: TuningFlags): Tuning;
|
|
61
|
+
/**
|
|
62
|
+
* The three switches Playwright adds to every Chromium launch so that pages in the background
|
|
63
|
+
* keep running at full speed (`chromiumSwitches`, pinned by the contract test).
|
|
64
|
+
*/
|
|
65
|
+
export declare const BACKGROUND_SWITCHES: readonly ["--disable-background-timer-throttling", "--disable-backgrounding-occluded-windows", "--disable-renderer-backgrounding"];
|
|
66
|
+
/**
|
|
67
|
+
* Launch options for `backgroundThrottling`. Dropping the switches is not enough: Playwright
|
|
68
|
+
* also turns on focus emulation for every page (`Emulation.setFocusEmulationEnabled`), which
|
|
69
|
+
* makes a page in the background report itself visible and run at full speed. `focusControl`
|
|
70
|
+
* turns that off, so Chrome sees which tab is really in front (measured with Chrome 157: a busy
|
|
71
|
+
* background tab drops from about 200 timer ticks and 120 animation frames a second to 1 and 0).
|
|
72
|
+
*/
|
|
73
|
+
export declare function launchTuning(tuning: Tuning): {
|
|
74
|
+
ignoreDefaultArgs: string[];
|
|
75
|
+
contextOptions: Record<string, unknown>;
|
|
76
|
+
};
|
package/dist/tuning.js
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { DEFAULT_CACHE_BYTES } from "./resources.js";
|
|
2
|
+
import { parseDuration, parseSize } from "./units.js";
|
|
3
|
+
/** The defaults, documented on the browser identities page. */
|
|
4
|
+
export const DEFAULT_TABS = Object.freeze({
|
|
5
|
+
max: 8,
|
|
6
|
+
popups: "close",
|
|
7
|
+
parkMs: 5 * 60_000,
|
|
8
|
+
closeMs: 15 * 60_000,
|
|
9
|
+
});
|
|
10
|
+
/** The tuning of a session: flags, then identity.json, then the defaults. */
|
|
11
|
+
export function resolveTuning(settings, flags = {}) {
|
|
12
|
+
const t = settings?.tabs;
|
|
13
|
+
const ms = (v) => (v === undefined ? undefined : parseDuration(v));
|
|
14
|
+
// No flag of turno's: Playwright's --timeout-settle is the flag, and it wins (configForSession).
|
|
15
|
+
const settle = ms(settings?.settle);
|
|
16
|
+
return {
|
|
17
|
+
tabs: {
|
|
18
|
+
max: flags.maxTabs ?? t?.max ?? DEFAULT_TABS.max,
|
|
19
|
+
popups: flags.popups ?? t?.popups ?? DEFAULT_TABS.popups,
|
|
20
|
+
parkMs: flags.parkAfterMs ?? ms(t?.park) ?? DEFAULT_TABS.parkMs,
|
|
21
|
+
closeMs: flags.closeAfterMs ?? ms(t?.close) ?? DEFAULT_TABS.closeMs,
|
|
22
|
+
},
|
|
23
|
+
backgroundThrottling: flags.backgroundThrottling ?? settings?.backgroundThrottling ?? false,
|
|
24
|
+
...(settle === undefined ? {} : { settleMs: settle }),
|
|
25
|
+
resources: resourcePolicy(settings),
|
|
26
|
+
humanize: {
|
|
27
|
+
...settings?.humanize,
|
|
28
|
+
level: flags.humanize ?? settings?.humanize?.level ?? "off",
|
|
29
|
+
},
|
|
30
|
+
sharing: flags.sharing ?? settings?.sharing ?? "session",
|
|
31
|
+
tabLocks: {
|
|
32
|
+
...(settings?.tabLocks?.wait === undefined ? {} : { waitMs: ms(settings.tabLocks.wait) }),
|
|
33
|
+
...(settings?.tabLocks?.release === undefined ? {} : { releaseMs: ms(settings.tabLocks.release) }),
|
|
34
|
+
},
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
function resourcePolicy(settings) {
|
|
38
|
+
const out = {};
|
|
39
|
+
const b = settings?.block;
|
|
40
|
+
if (b)
|
|
41
|
+
out.block = { trackers: b.trackers ?? false, types: b.types ?? [], patterns: b.patterns ?? [] };
|
|
42
|
+
if (settings?.cache)
|
|
43
|
+
out.cacheBytes = settings.cache.maxSize
|
|
44
|
+
? (parseSize(settings.cache.maxSize) ?? DEFAULT_CACHE_BYTES)
|
|
45
|
+
: DEFAULT_CACHE_BYTES;
|
|
46
|
+
return out;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* The three switches Playwright adds to every Chromium launch so that pages in the background
|
|
50
|
+
* keep running at full speed (`chromiumSwitches`, pinned by the contract test).
|
|
51
|
+
*/
|
|
52
|
+
export const BACKGROUND_SWITCHES = [
|
|
53
|
+
"--disable-background-timer-throttling",
|
|
54
|
+
"--disable-backgrounding-occluded-windows",
|
|
55
|
+
"--disable-renderer-backgrounding",
|
|
56
|
+
];
|
|
57
|
+
/**
|
|
58
|
+
* Launch options for `backgroundThrottling`. Dropping the switches is not enough: Playwright
|
|
59
|
+
* also turns on focus emulation for every page (`Emulation.setFocusEmulationEnabled`), which
|
|
60
|
+
* makes a page in the background report itself visible and run at full speed. `focusControl`
|
|
61
|
+
* turns that off, so Chrome sees which tab is really in front (measured with Chrome 157: a busy
|
|
62
|
+
* background tab drops from about 200 timer ticks and 120 animation frames a second to 1 and 0).
|
|
63
|
+
*/
|
|
64
|
+
export function launchTuning(tuning) {
|
|
65
|
+
if (!tuning.backgroundThrottling)
|
|
66
|
+
return { ignoreDefaultArgs: [], contextOptions: {} };
|
|
67
|
+
return { ignoreDefaultArgs: [...BACKGROUND_SWITCHES], contextOptions: { focusControl: true } };
|
|
68
|
+
}
|
|
69
|
+
//# sourceMappingURL=tuning.js.map
|
package/dist/units.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Durations and sizes as people write them in identity.json and on the command line:
|
|
3
|
+
* `"15m"`, `"90s"`, `"250ms"`, `"1h"` and `"0"`; `"100MB"`, `"512KB"`, `"1GB"`.
|
|
4
|
+
*/
|
|
5
|
+
/** Milliseconds of a duration like `"15m"`, `"90s"`, `"250ms"`, `"1h"` or `"0"`; undefined when it is not one. */
|
|
6
|
+
export declare function parseDuration(value: string): number | undefined;
|
|
7
|
+
/** Bytes of a size like `"100MB"`, `"512KB"`, `"1GB"` (case does not matter); undefined when it is not one. */
|
|
8
|
+
export declare function parseSize(value: string): number | undefined;
|
package/dist/units.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Durations and sizes as people write them in identity.json and on the command line:
|
|
3
|
+
* `"15m"`, `"90s"`, `"250ms"`, `"1h"` and `"0"`; `"100MB"`, `"512KB"`, `"1GB"`.
|
|
4
|
+
*/
|
|
5
|
+
const TIME = { ms: 1, s: 1000, m: 60_000, h: 3_600_000 };
|
|
6
|
+
const SIZE = { b: 1, kb: 1024, mb: 1024 ** 2, gb: 1024 ** 3 };
|
|
7
|
+
/** Milliseconds of a duration like `"15m"`, `"90s"`, `"250ms"`, `"1h"` or `"0"`; undefined when it is not one. */
|
|
8
|
+
export function parseDuration(value) {
|
|
9
|
+
const v = value.trim();
|
|
10
|
+
if (v === "0")
|
|
11
|
+
return 0;
|
|
12
|
+
const m = /^(\d+)(ms|s|m|h)$/.exec(v);
|
|
13
|
+
return m ? Number(m[1]) * TIME[m[2]] : undefined;
|
|
14
|
+
}
|
|
15
|
+
/** Bytes of a size like `"100MB"`, `"512KB"`, `"1GB"` (case does not matter); undefined when it is not one. */
|
|
16
|
+
export function parseSize(value) {
|
|
17
|
+
const m = /^(\d+)\s*(b|kb|mb|gb)$/i.exec(value.trim());
|
|
18
|
+
return m ? Number(m[1]) * SIZE[m[2].toLowerCase()] : undefined;
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=units.js.map
|
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
import type { Browser, BrowserContext, Page } from "patchright-core";
|
|
2
|
+
/**
|
|
3
|
+
* The parts of the Playwright MCP tool backend in patchright-core that turno-browser uses as a
|
|
4
|
+
* library. They come from the package's own `exports` (`lib/coreBundle`,
|
|
5
|
+
* `lib/utilsBundle`), not from a file path, but they are not documented API: the exact version
|
|
6
|
+
* is pinned and `test/upstream-contract.test.ts` checks every name and signature used here.
|
|
7
|
+
*
|
|
8
|
+
* The types below describe only what turno touches. Everything else stays opaque.
|
|
9
|
+
*/
|
|
10
|
+
/** Resolved Playwright MCP config (`resolveCLIConfigForMCP`). Only the keys turno reads. */
|
|
11
|
+
export interface McpConfig {
|
|
12
|
+
browser: {
|
|
13
|
+
browserName: "chromium" | "firefox" | "webkit";
|
|
14
|
+
isolated?: boolean;
|
|
15
|
+
userDataDir?: string;
|
|
16
|
+
launchOptions: Record<string, unknown> & {
|
|
17
|
+
headless?: boolean;
|
|
18
|
+
channel?: string;
|
|
19
|
+
ignoreDefaultArgs?: boolean | string[];
|
|
20
|
+
};
|
|
21
|
+
contextOptions?: Record<string, unknown>;
|
|
22
|
+
cdpEndpoint?: string;
|
|
23
|
+
remoteEndpoint?: string | ({
|
|
24
|
+
endpoint: string;
|
|
25
|
+
headers?: Record<string, string>;
|
|
26
|
+
} & Record<string, unknown>);
|
|
27
|
+
remoteHeaders?: Record<string, string>;
|
|
28
|
+
};
|
|
29
|
+
extension?: boolean;
|
|
30
|
+
sharedBrowserContext?: boolean;
|
|
31
|
+
capabilities?: string[];
|
|
32
|
+
outputDir?: string;
|
|
33
|
+
skillMode?: boolean;
|
|
34
|
+
timeouts?: {
|
|
35
|
+
idle?: number;
|
|
36
|
+
settle?: number;
|
|
37
|
+
};
|
|
38
|
+
/** `snapshot.mode` is read on every call that includes a snapshot (`"full"` or `"none"`). */
|
|
39
|
+
snapshot?: {
|
|
40
|
+
mode?: string;
|
|
41
|
+
[key: string]: unknown;
|
|
42
|
+
};
|
|
43
|
+
server: {
|
|
44
|
+
port?: number;
|
|
45
|
+
host?: string;
|
|
46
|
+
allowedHosts?: string[];
|
|
47
|
+
};
|
|
48
|
+
configFile?: string;
|
|
49
|
+
}
|
|
50
|
+
/** What the server learns about the client on its first tool call. */
|
|
51
|
+
export interface ClientInfo {
|
|
52
|
+
/** First workspace root of the client, or the process cwd. */
|
|
53
|
+
cwd: string;
|
|
54
|
+
clientName: string;
|
|
55
|
+
}
|
|
56
|
+
/** A tool schema as upstream defines it: `inputSchema` is a zod object of the bundled zod. */
|
|
57
|
+
export interface ToolSchema {
|
|
58
|
+
name: string;
|
|
59
|
+
title: string;
|
|
60
|
+
description: string;
|
|
61
|
+
inputSchema: ZodObject;
|
|
62
|
+
type: "input" | "assertion" | "action" | "readOnly";
|
|
63
|
+
}
|
|
64
|
+
/** The subset of a zod v4 object schema turno calls. */
|
|
65
|
+
export interface ZodObject extends ZodType {
|
|
66
|
+
parse(value: unknown): Record<string, unknown>;
|
|
67
|
+
safeParse(value: unknown): {
|
|
68
|
+
success: true;
|
|
69
|
+
data: Record<string, unknown>;
|
|
70
|
+
} | {
|
|
71
|
+
success: false;
|
|
72
|
+
error: unknown;
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
export interface UpstreamTool {
|
|
76
|
+
capability: string;
|
|
77
|
+
schema: ToolSchema;
|
|
78
|
+
skillOnly?: boolean;
|
|
79
|
+
}
|
|
80
|
+
export interface ToolResult {
|
|
81
|
+
content: ({
|
|
82
|
+
type: "text";
|
|
83
|
+
text: string;
|
|
84
|
+
} | {
|
|
85
|
+
type: string;
|
|
86
|
+
[key: string]: unknown;
|
|
87
|
+
})[];
|
|
88
|
+
isError?: boolean;
|
|
89
|
+
[key: string]: unknown;
|
|
90
|
+
}
|
|
91
|
+
export interface IdleTimerLike {
|
|
92
|
+
poke(): void;
|
|
93
|
+
dispose(): void;
|
|
94
|
+
}
|
|
95
|
+
/** One MCP session's view of a browser context: upstream's `BrowserBackend`. */
|
|
96
|
+
export interface BrowserBackendInstance {
|
|
97
|
+
initialize(clientInfo: ClientInfo): Promise<void>;
|
|
98
|
+
callTool(name: string, args: Record<string, unknown>, signal?: AbortSignal): Promise<ToolResult>;
|
|
99
|
+
dynamicTools(): ToolSchemaJson[];
|
|
100
|
+
dispose(): Promise<void>;
|
|
101
|
+
on(event: "dynamictoolschange" | "disconnected", listener: () => void): unknown;
|
|
102
|
+
once(event: "disconnected", listener: () => void): unknown;
|
|
103
|
+
}
|
|
104
|
+
/** One tab of upstream's `Context` (`tab.ts`): only the page is read. */
|
|
105
|
+
interface UpstreamTab {
|
|
106
|
+
page: Page;
|
|
107
|
+
}
|
|
108
|
+
/** Upstream's per-session `Context` (`context.ts`), which `BrowserBackend` keeps in `_context`. */
|
|
109
|
+
export interface UpstreamContext {
|
|
110
|
+
/** Every tab, in the order `browser_tabs` numbers them. */
|
|
111
|
+
tabs(): UpstreamTab[];
|
|
112
|
+
/** The tab the stock tools act on. */
|
|
113
|
+
currentTab(): UpstreamTab | undefined;
|
|
114
|
+
/**
|
|
115
|
+
* Builds the tab list (every page of the browser context, then each new one) on first use.
|
|
116
|
+
* `"sharing": "tabs"` calls it as soon as a session's view exists, so the view knows every tab.
|
|
117
|
+
*/
|
|
118
|
+
ensureBrowserContext(): Promise<unknown>;
|
|
119
|
+
/** Opens a page and makes it the current tab (`browser_tabs` action `new`). */
|
|
120
|
+
newTab(): Promise<UpstreamTab>;
|
|
121
|
+
/** Points the stock tools at a tab without bringing it to the front (`"sharing": "tabs"`). */
|
|
122
|
+
_setCurrentTab(tab: UpstreamTab | undefined): void;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* The tabs of a `BrowserBackend`, for the tab governor: `initialize` builds the
|
|
126
|
+
* `Context` and keeps it in `_context` (pinned by the contract test). Undefined before that.
|
|
127
|
+
*/
|
|
128
|
+
export declare function backendContext(backend: BrowserBackendInstance): UpstreamContext | undefined;
|
|
129
|
+
/** A tool as it goes out in `tools/list`. */
|
|
130
|
+
export interface ToolSchemaJson {
|
|
131
|
+
name: string;
|
|
132
|
+
description?: string;
|
|
133
|
+
inputSchema: Record<string, unknown>;
|
|
134
|
+
annotations?: Record<string, unknown>;
|
|
135
|
+
[key: string]: unknown;
|
|
136
|
+
}
|
|
137
|
+
interface BrowserBackendCtor {
|
|
138
|
+
new (config: McpConfig, browserContext: BrowserContext, tools: UpstreamTool[], options?: {
|
|
139
|
+
idleTimer?: IdleTimerLike;
|
|
140
|
+
dispose?: () => Promise<void>;
|
|
141
|
+
}): BrowserBackendInstance;
|
|
142
|
+
}
|
|
143
|
+
/** What upstream's `createBrowserWithInfo` returns. */
|
|
144
|
+
interface UpstreamBrowserInfo {
|
|
145
|
+
browser: Browser;
|
|
146
|
+
endpoint: string;
|
|
147
|
+
ownership: "own" | "attached";
|
|
148
|
+
idleTimer?: IdleTimerLike;
|
|
149
|
+
}
|
|
150
|
+
/** The server backend factory upstream's `start` serves (one backend per MCP session). */
|
|
151
|
+
export interface ServerBackendFactory {
|
|
152
|
+
name: string;
|
|
153
|
+
nameInConfig: string;
|
|
154
|
+
version: string;
|
|
155
|
+
toolSchemas: ToolSchema[];
|
|
156
|
+
create(clientInfo: ClientInfo): Promise<ServerBackend>;
|
|
157
|
+
}
|
|
158
|
+
export interface ServerBackend {
|
|
159
|
+
initialize?(clientInfo: ClientInfo): Promise<void>;
|
|
160
|
+
callTool(name: string, args: Record<string, unknown>, signal?: AbortSignal): Promise<ToolResult>;
|
|
161
|
+
dynamicTools?(): ToolSchemaJson[];
|
|
162
|
+
dispose?(): Promise<void>;
|
|
163
|
+
on?(event: "dynamictoolschange", listener: () => void): unknown;
|
|
164
|
+
once(event: "disconnected", listener: () => void): unknown;
|
|
165
|
+
}
|
|
166
|
+
/** A commander `Command` (the bundled commander), only the calls turno makes. */
|
|
167
|
+
interface Command {
|
|
168
|
+
version(text: string): Command;
|
|
169
|
+
name(text: string): Command;
|
|
170
|
+
action(fn: (options: Record<string, unknown>) => Promise<void> | void): Command;
|
|
171
|
+
parseAsync(argv: string[], options: {
|
|
172
|
+
from: "user";
|
|
173
|
+
}): Promise<Command>;
|
|
174
|
+
}
|
|
175
|
+
export interface UpstreamTools {
|
|
176
|
+
BrowserBackend: BrowserBackendCtor;
|
|
177
|
+
browserTools: UpstreamTool[];
|
|
178
|
+
filteredTools(config: McpConfig): UpstreamTool[];
|
|
179
|
+
decorateMCPCommand(command: Command): void;
|
|
180
|
+
resolveCLIConfigForMCP(options: Record<string, unknown>, env?: NodeJS.ProcessEnv): Promise<McpConfig>;
|
|
181
|
+
createBrowserWithInfo(config: McpConfig, clientInfo: ClientInfo, cliOptions: Record<string, unknown>, bindOptions: {
|
|
182
|
+
title: string;
|
|
183
|
+
workspaceDir: string;
|
|
184
|
+
}): Promise<UpstreamBrowserInfo>;
|
|
185
|
+
setupExitWatchdog(): void;
|
|
186
|
+
start(factory: ServerBackendFactory, options: McpConfig["server"]): Promise<void>;
|
|
187
|
+
isProfileLocked(userDataDir: string): boolean;
|
|
188
|
+
outputDir(options: {
|
|
189
|
+
config: McpConfig;
|
|
190
|
+
cwd: string;
|
|
191
|
+
}): string;
|
|
192
|
+
}
|
|
193
|
+
interface CoreBundle {
|
|
194
|
+
tools: UpstreamTools;
|
|
195
|
+
utils: {
|
|
196
|
+
gracefullyProcessExitDoNotHang(code: number): void;
|
|
197
|
+
/** What upstream's exit watchdog awaits before the process exits (stdin closed, SIGTERM). */
|
|
198
|
+
gracefullyCloseSet: Set<() => Promise<void>>;
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
/** A zod schema of the bundled zod, as far as turno tools build one. */
|
|
202
|
+
interface ZodType {
|
|
203
|
+
describe(text: string): ZodType;
|
|
204
|
+
optional(): ZodType;
|
|
205
|
+
}
|
|
206
|
+
interface ZodArray extends ZodType {
|
|
207
|
+
min(n: number): ZodArray;
|
|
208
|
+
max(n: number): ZodArray;
|
|
209
|
+
}
|
|
210
|
+
/** The bundled zod v4: the constructors a turno tool uses, and what the host needs. */
|
|
211
|
+
export interface Zod {
|
|
212
|
+
object(shape: Record<string, ZodType>): ZodObject;
|
|
213
|
+
string(): ZodType;
|
|
214
|
+
number(): ZodType;
|
|
215
|
+
boolean(): ZodType;
|
|
216
|
+
unknown(): ZodType;
|
|
217
|
+
array(item: ZodType): ZodArray;
|
|
218
|
+
record(key: ZodType, value: ZodType): ZodType;
|
|
219
|
+
toJSONSchema(schema: ZodObject): Record<string, unknown>;
|
|
220
|
+
ZodError: new (...args: never[]) => Error;
|
|
221
|
+
prettifyError(error: unknown): string;
|
|
222
|
+
}
|
|
223
|
+
interface UtilsBundle {
|
|
224
|
+
program: Command & {
|
|
225
|
+
constructor: new () => Command;
|
|
226
|
+
};
|
|
227
|
+
z: Zod;
|
|
228
|
+
}
|
|
229
|
+
/** Loads the library once. `patchright-core` itself first: its entry runs `lib/bootstrap`. */
|
|
230
|
+
export declare function upstream(): {
|
|
231
|
+
tools: UpstreamTools;
|
|
232
|
+
core: CoreBundle;
|
|
233
|
+
utils: UtilsBundle;
|
|
234
|
+
version: string;
|
|
235
|
+
};
|
|
236
|
+
/** The in-process Playwright object of the same bundle (so upstream's exit hooks see it). */
|
|
237
|
+
export declare function playwright(): Record<McpConfig["browser"]["browserName"], typeof import("patchright-core").chromium>;
|
|
238
|
+
/**
|
|
239
|
+
* A tool schema as it goes out in `tools/list`, built the way upstream's `toMcpTool` builds it
|
|
240
|
+
* (the contract test compares the whole list with the stock program's).
|
|
241
|
+
*/
|
|
242
|
+
export declare function toMcpTool(schema: ToolSchema): ToolSchemaJson;
|
|
243
|
+
export {};
|
package/dist/upstream.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
/**
|
|
3
|
+
* The tabs of a `BrowserBackend`, for the tab governor: `initialize` builds the
|
|
4
|
+
* `Context` and keeps it in `_context` (pinned by the contract test). Undefined before that.
|
|
5
|
+
*/
|
|
6
|
+
export function backendContext(backend) {
|
|
7
|
+
return backend._context;
|
|
8
|
+
}
|
|
9
|
+
const require = createRequire(import.meta.url);
|
|
10
|
+
let loaded;
|
|
11
|
+
/** Loads the library once. `patchright-core` itself first: its entry runs `lib/bootstrap`. */
|
|
12
|
+
export function upstream() {
|
|
13
|
+
if (!loaded) {
|
|
14
|
+
require("patchright-core");
|
|
15
|
+
loaded = {
|
|
16
|
+
core: require("patchright-core/lib/coreBundle"),
|
|
17
|
+
utils: require("patchright-core/lib/utilsBundle"),
|
|
18
|
+
version: require("patchright-core/package.json").version,
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
return { tools: loaded.core.tools, ...loaded };
|
|
22
|
+
}
|
|
23
|
+
/** The in-process Playwright object of the same bundle (so upstream's exit hooks see it). */
|
|
24
|
+
export function playwright() {
|
|
25
|
+
upstream();
|
|
26
|
+
return require("patchright-core");
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* A tool schema as it goes out in `tools/list`, built the way upstream's `toMcpTool` builds it
|
|
30
|
+
* (the contract test compares the whole list with the stock program's).
|
|
31
|
+
*/
|
|
32
|
+
export function toMcpTool(schema) {
|
|
33
|
+
const readOnly = schema.type === "readOnly" || schema.type === "assertion";
|
|
34
|
+
return {
|
|
35
|
+
name: schema.name,
|
|
36
|
+
description: schema.description,
|
|
37
|
+
inputSchema: upstream().utils.z.toJSONSchema(schema.inputSchema),
|
|
38
|
+
annotations: {
|
|
39
|
+
title: schema.title,
|
|
40
|
+
readOnlyHint: readOnly,
|
|
41
|
+
destructiveHint: !readOnly,
|
|
42
|
+
openWorldHint: true,
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
//# sourceMappingURL=upstream.js.map
|