turno-browser 0.3.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +122 -0
- package/README.md +95 -123
- package/dist/act-model.d.ts +85 -0
- package/dist/act-model.js +117 -0
- package/dist/act.d.ts +37 -0
- package/dist/act.js +600 -0
- package/dist/api.d.ts +90 -2
- package/dist/api.js +41 -4
- package/dist/argv.d.ts +16 -2
- package/dist/argv.js +100 -3
- package/dist/background-state.d.ts +54 -0
- package/dist/background-state.js +97 -0
- package/dist/background.d.ts +69 -0
- package/dist/background.js +458 -0
- package/dist/batch.d.ts +5 -0
- package/dist/batch.js +3 -3
- package/dist/bin/mcp.js +103 -6
- package/dist/captcha.d.ts +77 -0
- package/dist/captcha.js +334 -0
- package/dist/cdp-relay.d.ts +20 -0
- package/dist/cdp-relay.js +101 -0
- package/dist/chrome-features.d.ts +23 -0
- package/dist/chrome-features.js +44 -0
- package/dist/cli/background.d.ts +11 -0
- package/dist/cli/background.js +132 -0
- package/dist/cli/commands.d.ts +16 -0
- package/dist/cli/commands.js +515 -0
- package/dist/cli/context.d.ts +75 -0
- package/dist/cli/context.js +44 -0
- package/dist/cli/do-background.d.ts +27 -0
- package/dist/cli/do-background.js +212 -0
- package/dist/cli/do.d.ts +85 -0
- package/dist/cli/do.js +558 -0
- package/dist/cli/flows.d.ts +28 -0
- package/dist/cli/flows.js +303 -0
- package/dist/cli/identity.d.ts +37 -0
- package/dist/cli/identity.js +906 -0
- package/dist/cli/live.d.ts +7 -0
- package/dist/cli/live.js +99 -0
- package/dist/cli/main.d.ts +16 -0
- package/dist/cli/main.js +171 -0
- package/dist/cli/secret.d.ts +4 -0
- package/dist/cli/secret.js +151 -0
- package/dist/cli/session.d.ts +6 -0
- package/dist/cli/session.js +106 -0
- package/dist/cli/setup.d.ts +7 -0
- package/dist/cli/setup.js +180 -0
- package/dist/cli/vault.d.ts +5 -0
- package/dist/cli/vault.js +192 -0
- package/dist/client.d.ts +82 -0
- package/dist/client.js +303 -0
- package/dist/diagnose.d.ts +7 -0
- package/dist/diagnose.js +61 -6
- package/dist/doctor.d.ts +37 -3
- package/dist/doctor.js +264 -6
- package/dist/engine-attach.d.ts +27 -0
- package/dist/engine-attach.js +165 -0
- package/dist/engine-camoufox.d.ts +32 -0
- package/dist/engine-camoufox.js +271 -0
- package/dist/engine-lightpanda.d.ts +72 -0
- package/dist/engine-lightpanda.js +293 -0
- package/dist/engine.d.ts +63 -11
- package/dist/engine.js +106 -29
- package/dist/flows.d.ts +127 -0
- package/dist/flows.js +294 -0
- package/dist/governor.d.ts +42 -7
- package/dist/governor.js +82 -32
- package/dist/health.js +2 -1
- package/dist/host.d.ts +38 -2
- package/dist/host.js +689 -52
- package/dist/human-input.d.ts +84 -0
- package/dist/human-input.js +448 -0
- package/dist/humanize.d.ts +123 -0
- package/dist/humanize.js +241 -0
- package/dist/identities.js +4 -1
- package/dist/idle-timer.d.ts +12 -0
- package/dist/idle-timer.js +25 -0
- package/dist/index.d.ts +13 -4
- package/dist/index.js +7 -1
- package/dist/lifecycle.d.ts +11 -1
- package/dist/lifecycle.js +11 -2
- package/dist/live-client.d.ts +22 -0
- package/dist/live-client.js +95 -0
- package/dist/live-page.d.ts +4 -0
- package/dist/live-page.js +144 -0
- package/dist/live-server.d.ts +173 -0
- package/dist/live-server.js +643 -0
- package/dist/live.d.ts +120 -0
- package/dist/live.js +204 -0
- package/dist/login-tool.d.ts +13 -0
- package/dist/login-tool.js +8 -3
- package/dist/login.d.ts +4 -1
- package/dist/login.js +12 -2
- package/dist/names.d.ts +28 -0
- package/dist/names.js +25 -0
- package/dist/network-chrome.d.ts +74 -0
- package/dist/network-chrome.js +178 -0
- package/dist/network.d.ts +125 -0
- package/dist/network.js +264 -0
- package/dist/opt-in-settings.d.ts +69 -0
- package/dist/opt-in-settings.js +152 -0
- package/dist/recorder.d.ts +88 -0
- package/dist/recorder.js +538 -0
- package/dist/recordings.d.ts +116 -0
- package/dist/recordings.js +474 -0
- package/dist/redact.d.ts +7 -0
- package/dist/redact.js +18 -0
- package/dist/replay.d.ts +20 -0
- package/dist/replay.js +383 -0
- package/dist/reputation.d.ts +24 -2
- package/dist/reputation.js +63 -17
- package/dist/runtime-lock.js +3 -2
- package/dist/session-export.js +1 -1
- package/dist/session.d.ts +10 -2
- package/dist/session.js +10 -2
- package/dist/settings.d.ts +104 -2
- package/dist/settings.js +204 -6
- package/dist/sharing.d.ts +96 -0
- package/dist/sharing.js +269 -0
- package/dist/snippet.d.ts +8 -2
- package/dist/snippet.js +10 -4
- package/dist/tab-locks.d.ts +63 -0
- package/dist/tab-locks.js +179 -0
- package/dist/tools.d.ts +38 -0
- package/dist/tools.js +20 -1
- package/dist/tuning.d.ts +14 -0
- package/dist/tuning.js +9 -0
- package/dist/upstream.d.ts +11 -0
- package/dist/vault.js +5 -4
- package/npm-shrinkwrap.json +6 -5
- package/package.json +13 -7
package/dist/humanize.js
ADDED
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Human-like input, the planning half: where the pointer goes and when, how long a key or a
|
|
3
|
+
* button stays down, how a scroll breaks into wheel notches. Pure functions of a seeded random
|
|
4
|
+
* source, so a test with a seed gets the same plan every run; `human-input.ts` plays a plan on a
|
|
5
|
+
* page.
|
|
6
|
+
*
|
|
7
|
+
* Levels (identity.json `humanize.level`, off by default):
|
|
8
|
+
*
|
|
9
|
+
* - `off`: Playwright's own input, as before: one `mousemove` to the exact center of the target,
|
|
10
|
+
* button down and up at once, text inserted in one go.
|
|
11
|
+
* - `light`: the pointer travels a curve from where it last was to a point inside the target, at
|
|
12
|
+
* a steady pace with small timing jitter; the button stays down for tens of milliseconds;
|
|
13
|
+
* short text is typed key by key with per-key delays.
|
|
14
|
+
* - `full`: `light` plus a duration that grows with distance and shrinks with target size
|
|
15
|
+
* (Fitts' law), a slow-fast-slow speed profile, overshoot and correction on long moves, a
|
|
16
|
+
* short pause to read a page the first time it is acted on, longer pauses between words, and
|
|
17
|
+
* scrolling to an element out of view in wheel notches instead of one jump.
|
|
18
|
+
* - `adaptive`: `light`, and `full` on a site after the identity met a challenge page there (the
|
|
19
|
+
* reputation log, or this session).
|
|
20
|
+
*
|
|
21
|
+
* The numbers come from published measurements: a mouse move to a 20 px target 630 px away takes
|
|
22
|
+
* a person about 750-850 ms; the average typist types 52 words a minute (about 230 ms a
|
|
23
|
+
* character, the fast ones about 100 ms); a button press lasts tens of milliseconds to about 150.
|
|
24
|
+
* Typing faster than a few milliseconds a key is physically impossible, so long text is pasted
|
|
25
|
+
* instead (`MAX_TYPED`), like a person pasting it.
|
|
26
|
+
*/
|
|
27
|
+
export const HUMANIZE_LEVELS = ["off", "light", "full", "adaptive"];
|
|
28
|
+
/** Text longer than this is pasted (Playwright's `fill`), not typed. */
|
|
29
|
+
export const MAX_TYPED = 80;
|
|
30
|
+
/** A seeded source of random numbers (mulberry32): small, fast and the same on every platform. */
|
|
31
|
+
export class Rng {
|
|
32
|
+
state;
|
|
33
|
+
constructor(seed) {
|
|
34
|
+
this.state = seed >>> 0;
|
|
35
|
+
}
|
|
36
|
+
/** A number in [0, 1). */
|
|
37
|
+
next() {
|
|
38
|
+
this.state = (this.state + 0x6d2b79f5) >>> 0;
|
|
39
|
+
let t = this.state;
|
|
40
|
+
t = Math.imul(t ^ (t >>> 15), t | 1);
|
|
41
|
+
t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
|
|
42
|
+
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
|
|
43
|
+
}
|
|
44
|
+
range(min, max) {
|
|
45
|
+
return min + (max - min) * this.next();
|
|
46
|
+
}
|
|
47
|
+
chance(p) {
|
|
48
|
+
return this.next() < p;
|
|
49
|
+
}
|
|
50
|
+
/** A normal draw (Box-Muller), clamped to [min, max]. */
|
|
51
|
+
normal(mean, sd, min = Number.NEGATIVE_INFINITY, max = Number.POSITIVE_INFINITY) {
|
|
52
|
+
const u = 1 - this.next();
|
|
53
|
+
const v = this.next();
|
|
54
|
+
const z = Math.sqrt(-2 * Math.log(u)) * Math.cos(2 * Math.PI * v);
|
|
55
|
+
return Math.min(max, Math.max(min, mean + sd * z));
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
/** A 32-bit FNV-1a hash, for a seed from a name. */
|
|
59
|
+
export function hashSeed(text) {
|
|
60
|
+
let h = 0x811c9dc5;
|
|
61
|
+
for (let i = 0; i < text.length; i++) {
|
|
62
|
+
h ^= text.charCodeAt(i);
|
|
63
|
+
h = Math.imul(h, 0x01000193);
|
|
64
|
+
}
|
|
65
|
+
return h >>> 0;
|
|
66
|
+
}
|
|
67
|
+
export function personaFor(seed) {
|
|
68
|
+
const rng = new Rng(seed ^ 0x9e3779b9);
|
|
69
|
+
return {
|
|
70
|
+
speed: Number(rng.range(0.85, 1.2).toFixed(3)),
|
|
71
|
+
keyMs: Math.round(rng.range(120, 190)),
|
|
72
|
+
bend: rng.chance(0.5) ? 1 : -1,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
const STEP_MS = 16;
|
|
76
|
+
function bezier(p0, p1, p2, p3, t) {
|
|
77
|
+
const u = 1 - t;
|
|
78
|
+
const a = u * u * u;
|
|
79
|
+
const b = 3 * u * u * t;
|
|
80
|
+
const c = 3 * u * t * t;
|
|
81
|
+
const d = t * t * t;
|
|
82
|
+
return { x: a * p0.x + b * p1.x + c * p2.x + d * p3.x, y: a * p0.y + b * p1.y + c * p2.y + d * p3.y };
|
|
83
|
+
}
|
|
84
|
+
/** Minimum-jerk position profile: slow start, fast middle, slow end. */
|
|
85
|
+
const minimumJerk = (t) => t * t * t * (10 - 15 * t + 6 * t * t);
|
|
86
|
+
/** How long a move takes at each level, before the persona's speed. */
|
|
87
|
+
export function moveDuration(distance, targetWidth, level, rng) {
|
|
88
|
+
if (distance < 1)
|
|
89
|
+
return 0;
|
|
90
|
+
if (level === "light")
|
|
91
|
+
return Math.min(420, Math.max(90, 80 + 0.2 * distance)) * rng.range(0.85, 1.15);
|
|
92
|
+
// Fitts' law, T = a + b log2(D / W + 1), with a and b in the range measured for a mouse.
|
|
93
|
+
const bits = Math.log2(distance / Math.max(8, targetWidth) + 1);
|
|
94
|
+
return (110 + 140 * bits) * rng.range(0.9, 1.15);
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* A curve from `from` to `to`: a cubic Bezier whose control points bend to one side. The bend is
|
|
98
|
+
* what a wrist pivoting makes: the path strays about a tenth to a sixth of the distance from the
|
|
99
|
+
* straight line, so it is some 3 to 9% longer than that line (a path within 1% of it, as an
|
|
100
|
+
* earlier version drew, reads as a straight line).
|
|
101
|
+
*/
|
|
102
|
+
function curve(from, to, duration, level, rng, persona) {
|
|
103
|
+
const dx = to.x - from.x;
|
|
104
|
+
const dy = to.y - from.y;
|
|
105
|
+
const distance = Math.hypot(dx, dy);
|
|
106
|
+
if (distance < 1 || duration <= 0)
|
|
107
|
+
return [{ x: to.x, y: to.y, wait: 0 }];
|
|
108
|
+
const nx = -dy / distance;
|
|
109
|
+
const ny = dx / distance;
|
|
110
|
+
const spread = Math.min(180, distance * (level === "full" ? 0.34 : 0.3));
|
|
111
|
+
const side = rng.chance(0.75) ? persona.bend : -persona.bend;
|
|
112
|
+
const c1 = rng.range(0.15, 0.45);
|
|
113
|
+
const c2 = rng.range(0.55, 0.85);
|
|
114
|
+
// Both control points on the same side: one smooth arc, never an S.
|
|
115
|
+
const h1 = rng.range(0.55, 1) * spread;
|
|
116
|
+
const h2 = rng.range(0.25, 0.75) * spread;
|
|
117
|
+
const p1 = { x: from.x + dx * c1 + nx * side * h1, y: from.y + dy * c1 + ny * side * h1 };
|
|
118
|
+
const p2 = { x: from.x + dx * c2 + nx * side * h2, y: from.y + dy * c2 + ny * side * h2 };
|
|
119
|
+
const count = Math.max(4, Math.round(duration / STEP_MS));
|
|
120
|
+
const steps = [];
|
|
121
|
+
for (let i = 1; i <= count; i++) {
|
|
122
|
+
const t = i / count;
|
|
123
|
+
const p = bezier(from, p1, p2, to, level === "full" ? minimumJerk(t) : t);
|
|
124
|
+
// A little hand tremor on the way, never on the last point.
|
|
125
|
+
const jitter = level === "full" && i < count ? 0.6 : 0;
|
|
126
|
+
steps.push({
|
|
127
|
+
x: p.x + (jitter ? rng.normal(0, jitter) : 0),
|
|
128
|
+
y: p.y + (jitter ? rng.normal(0, jitter) : 0),
|
|
129
|
+
wait: (duration / count) * rng.range(0.8, 1.2),
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
return steps;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* The pointer path from `from` to `to` (which ends exactly on `to`), with the wait before each
|
|
136
|
+
* position. `targetWidth` is the smaller side of the target, for Fitts' law.
|
|
137
|
+
*/
|
|
138
|
+
export function planMove(from, to, targetWidth, level, rng, persona) {
|
|
139
|
+
const distance = Math.hypot(to.x - from.x, to.y - from.y);
|
|
140
|
+
if (distance < 1)
|
|
141
|
+
return [];
|
|
142
|
+
const duration = moveDuration(distance, targetWidth, level, rng) / persona.speed;
|
|
143
|
+
if (level === "full" && distance > 250 && rng.chance(0.7)) {
|
|
144
|
+
// Overshoot: the fast first movement lands past the target, a short second one corrects.
|
|
145
|
+
const ux = (to.x - from.x) / distance;
|
|
146
|
+
const uy = (to.y - from.y) / distance;
|
|
147
|
+
const past = Math.min(40, distance * rng.range(0.03, 0.07));
|
|
148
|
+
const sideways = rng.normal(0, past / 3);
|
|
149
|
+
const over = { x: to.x + ux * past - uy * sideways, y: to.y + uy * past + ux * sideways };
|
|
150
|
+
const first = curve(from, over, duration * 0.85, level, rng, persona);
|
|
151
|
+
const back = curve(over, to, rng.range(90, 170) / persona.speed, "light", rng, persona);
|
|
152
|
+
const [start, ...rest] = back;
|
|
153
|
+
if (start)
|
|
154
|
+
start.wait += rng.range(40, 110); // the eye sees the miss before the hand corrects
|
|
155
|
+
return [...first, ...(start ? [start] : []), ...rest];
|
|
156
|
+
}
|
|
157
|
+
return curve(from, to, duration, level, rng, persona);
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* A point inside the box: near the middle, never the exact center and never on the edge (a
|
|
161
|
+
* person aims at the visible middle of a button, and lands a few pixels off).
|
|
162
|
+
*/
|
|
163
|
+
export function pickPoint(box, rng) {
|
|
164
|
+
const fx = rng.normal(0.5, 0.12, 0.2, 0.8);
|
|
165
|
+
const fy = rng.normal(0.5, 0.12, 0.25, 0.75);
|
|
166
|
+
return { x: box.x + box.width * fx, y: box.y + box.height * fy };
|
|
167
|
+
}
|
|
168
|
+
export function clickTiming(level, rng) {
|
|
169
|
+
if (level === "light")
|
|
170
|
+
return { beforePress: rng.range(10, 50), press: rng.range(40, 90) };
|
|
171
|
+
return { beforePress: rng.range(60, 200), press: rng.normal(90, 25, 45, 170) };
|
|
172
|
+
}
|
|
173
|
+
/** A pause to look at a page acted on for the first time (`full` only). */
|
|
174
|
+
export function readingPause(rng) {
|
|
175
|
+
return rng.range(250, 800);
|
|
176
|
+
}
|
|
177
|
+
/** How long a key stays down, in ms. */
|
|
178
|
+
export function keyHold(level, rng) {
|
|
179
|
+
return level === "light" ? rng.range(30, 70) : rng.normal(75, 20, 35, 140);
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* The wait before each character of `text` (the first one included: the hand moves to the
|
|
183
|
+
* keyboard), in ms. `light`: the persona's pace with jitter. `full`: a skewed draw around the
|
|
184
|
+
* pace, faster inside a word than across a space, longer after punctuation, and now and then a
|
|
185
|
+
* pause to think.
|
|
186
|
+
*/
|
|
187
|
+
export function keyDelays(text, level, rng, persona) {
|
|
188
|
+
const chars = [...text];
|
|
189
|
+
const out = [];
|
|
190
|
+
for (let i = 0; i < chars.length; i++) {
|
|
191
|
+
const prev = i ? chars[i - 1] : "";
|
|
192
|
+
if (level === "light") {
|
|
193
|
+
out.push(persona.keyMs * rng.range(0.6, 1.3) * 0.8);
|
|
194
|
+
continue;
|
|
195
|
+
}
|
|
196
|
+
// A log-normal draw: most keys near the pace, a long tail of slower ones.
|
|
197
|
+
let wait = persona.keyMs * Math.exp(rng.normal(-0.1, 0.35, -1, 1.2));
|
|
198
|
+
if (prev === " ")
|
|
199
|
+
wait += rng.range(40, 160);
|
|
200
|
+
else if (/[.,;:!?]/.test(prev))
|
|
201
|
+
wait += rng.range(120, 300);
|
|
202
|
+
if (i === 0)
|
|
203
|
+
wait += rng.range(80, 200);
|
|
204
|
+
else if (rng.chance(0.03))
|
|
205
|
+
wait += rng.range(300, 800);
|
|
206
|
+
out.push(wait);
|
|
207
|
+
}
|
|
208
|
+
return out;
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* A scroll of (dx, dy) in wheel notches of about 100 px, in short bursts with a pause between
|
|
212
|
+
* them, the way a mouse wheel turns (`full`). The notches add up to exactly (dx, dy).
|
|
213
|
+
*/
|
|
214
|
+
export function planWheel(dx, dy, rng) {
|
|
215
|
+
const total = Math.max(Math.abs(dx), Math.abs(dy));
|
|
216
|
+
if (total === 0)
|
|
217
|
+
return [];
|
|
218
|
+
const count = Math.max(1, Math.round(total / 100));
|
|
219
|
+
const out = [];
|
|
220
|
+
let burst = Math.round(rng.range(2, 6));
|
|
221
|
+
let sentX = 0;
|
|
222
|
+
let sentY = 0;
|
|
223
|
+
for (let i = 1; i <= count; i++) {
|
|
224
|
+
const x = Math.round((dx * i) / count) - sentX;
|
|
225
|
+
const y = Math.round((dy * i) / count) - sentY;
|
|
226
|
+
sentX += x;
|
|
227
|
+
sentY += y;
|
|
228
|
+
let wait = rng.range(25, 70);
|
|
229
|
+
if (--burst === 0) {
|
|
230
|
+
wait += rng.range(120, 350);
|
|
231
|
+
burst = Math.round(rng.range(2, 6));
|
|
232
|
+
}
|
|
233
|
+
out.push({ dx: x, dy: y, wait: i === 1 ? rng.range(20, 60) : wait });
|
|
234
|
+
}
|
|
235
|
+
return out;
|
|
236
|
+
}
|
|
237
|
+
/** The total time of a plan, in ms. */
|
|
238
|
+
export function planTime(steps) {
|
|
239
|
+
return steps.reduce((sum, s) => sum + s.wait, 0);
|
|
240
|
+
}
|
|
241
|
+
//# sourceMappingURL=humanize.js.map
|
package/dist/identities.js
CHANGED
|
@@ -3,9 +3,11 @@ import { join } from "node:path";
|
|
|
3
3
|
import { browserPaths as paths } from "turno-core/paths";
|
|
4
4
|
import { pidAlive } from "turno-core/process";
|
|
5
5
|
import { IdentityError, assertIdentityId, identityDir, identityStateDir, isValidIdentityId } from "./ids.js";
|
|
6
|
+
import { removeFlows } from "./flows.js";
|
|
6
7
|
import { inUseMessage, readRuntimeLock, runtimeLockAlive } from "./runtime-lock.js";
|
|
7
8
|
import { identitySettingsPath, systemTimezone, writeIdentitySettings } from "./settings.js";
|
|
8
9
|
import { vaultHasSites, vaultPath } from "./vault.js";
|
|
10
|
+
import { names } from "./names.js";
|
|
9
11
|
export { IdentityError, assertIdentityId, identityDir, identityStateDir, isValidIdentityId };
|
|
10
12
|
export function identityExists(id, env = process.env) {
|
|
11
13
|
const dir = identityDir(id, env);
|
|
@@ -61,11 +63,12 @@ export function removeIdentity(id, env = process.env) {
|
|
|
61
63
|
if (isInUse(id, env))
|
|
62
64
|
throw new IdentityError(`identity "${id}" is in use by a running Chrome; close it first`);
|
|
63
65
|
if (vaultHasSites(id, env))
|
|
64
|
-
throw new IdentityError(`identity "${id}" still has vault credentials; remove them first (
|
|
66
|
+
throw new IdentityError(`identity "${id}" still has vault credentials; remove them first (${names().browser} remove does)`);
|
|
65
67
|
rmSync(dir, { recursive: true, force: true });
|
|
66
68
|
rmSync(vaultPath(id, env), { force: true });
|
|
67
69
|
rmSync(identitySettingsPath(id, env), { force: true });
|
|
68
70
|
rmSync(identityStateDir(id, env), { recursive: true, force: true });
|
|
71
|
+
removeFlows(id, env);
|
|
69
72
|
}
|
|
70
73
|
/**
|
|
71
74
|
* Chrome writes `SingletonLock` as a symlink whose target is `<hostname>-<pid>`.
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { IdleTimerLike, McpConfig } from "./upstream.js";
|
|
2
|
+
/** Upstream's `IdleTimer`: every tool call restarts the countdown. */
|
|
3
|
+
export declare class IdleTimer implements IdleTimerLike {
|
|
4
|
+
private readonly ms;
|
|
5
|
+
private readonly onIdle;
|
|
6
|
+
private timer;
|
|
7
|
+
constructor(ms: number, onIdle: () => void);
|
|
8
|
+
poke(): void;
|
|
9
|
+
dispose(): void;
|
|
10
|
+
}
|
|
11
|
+
/** The idle limit of a browser turno starts or attaches to: the config's, else upstream's headless default. */
|
|
12
|
+
export declare function idleMs(config: McpConfig): number | undefined;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/** Upstream's default idle limit for a headless browser it owns (`defaultIdleTimeout`). */
|
|
2
|
+
const HEADLESS_IDLE_MS = 60 * 60 * 1000;
|
|
3
|
+
/** Upstream's `IdleTimer`: every tool call restarts the countdown. */
|
|
4
|
+
export class IdleTimer {
|
|
5
|
+
ms;
|
|
6
|
+
onIdle;
|
|
7
|
+
timer;
|
|
8
|
+
constructor(ms, onIdle) {
|
|
9
|
+
this.ms = ms;
|
|
10
|
+
this.onIdle = onIdle;
|
|
11
|
+
}
|
|
12
|
+
poke() {
|
|
13
|
+
this.dispose();
|
|
14
|
+
this.timer = setTimeout(this.onIdle, this.ms);
|
|
15
|
+
}
|
|
16
|
+
dispose() {
|
|
17
|
+
clearTimeout(this.timer);
|
|
18
|
+
this.timer = undefined;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
/** The idle limit of a browser turno starts or attaches to: the config's, else upstream's headless default. */
|
|
22
|
+
export function idleMs(config) {
|
|
23
|
+
return config.timeouts?.idle ?? (config.browser.launchOptions.headless ? HEADLESS_IDLE_MS : undefined);
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=idle-timer.js.map
|
package/dist/index.d.ts
CHANGED
|
@@ -1,20 +1,29 @@
|
|
|
1
|
-
export { API_VERSION, type ApiVersion, type BrowserApi, api } from "./api.js";
|
|
2
|
-
export {
|
|
1
|
+
export { API_VERSION, type ApiVersion, type BrowserApi, type BrowserFunctions, api } from "./api.js";
|
|
2
|
+
export { type BrowserCli, browserCli as cli } from "./cli/commands.js";
|
|
3
|
+
export type { BrowserCliHost, CliDeps, CliNames, OpenUrl, VaultInput } from "./cli/context.js";
|
|
4
|
+
export type { DoSession, DoSessionInfo, DoTarget, DoTransport, ToolResult } from "./cli/do.js";
|
|
5
|
+
export { SESSION_CAPABILITY, SESSION_ENDED_METHOD, SESSION_META_KEY } from "./session.js";
|
|
3
6
|
export { manifestSnippet } from "./snippet.js";
|
|
4
7
|
export { IdentityError, type IdentityInfo, addIdentity, assertIdentityId, identityDir, identityExists, identityStateDir, inUseBy, isInUse, isValidIdentityId, listIdentities, parseSingletonLock, removeIdentity, } from "./identities.js";
|
|
5
|
-
export { type HealthCheck, IDENTITY_FORMAT_VERSION, type IdentitySettings, type SettingsPatch, checkIdentitySettings, identitySettingsPath, readIdentitySettings, systemTimezone, writeIdentitySettings, } from "./settings.js";
|
|
8
|
+
export { type HealthCheck, IDENTITY_FORMAT_VERSION, type IdentitySettings, type LiveSettings, type RecordSettings, type SettingsPatch, type TabLockSettings, checkIdentitySettings, identitySettingsPath, readIdentitySettings, systemTimezone, writeIdentitySettings, } from "./settings.js";
|
|
6
9
|
export { IdentityInUseError, type RuntimeLock, readRuntimeLock, runtimeLockAlive, runtimeLockPath, } from "./runtime-lock.js";
|
|
7
10
|
export { type HealthRecord, readHealth } from "./health.js";
|
|
8
11
|
export { type ChallengeEvent, type IdentityReputation, type SiteSummary, readChallenges, reputationReport, } from "./reputation.js";
|
|
9
|
-
export { type ChromeInfo, type DoctorCheck, type DoctorOptions, type DoctorReport, findChrome, runDoctor, } from "./doctor.js";
|
|
12
|
+
export { type ChromeInfo, type DoctorCheck, type DoctorOptions, type DoctorReport, type ServedIdentity, findChrome, runDoctor, } from "./doctor.js";
|
|
10
13
|
export { type BuiltArgv, type PlaywrightMcpConfig, buildArgv, extractIdentity } from "./argv.js";
|
|
11
14
|
export { type BenchOptions, type BenchReport, DETECTORS, ENGINES, ENGINE_ALIASES, type EngineName, type EngineReport, type TargetResult, defaultEngines, engineId, formatBenchTable, runBench, unavailableReason, } from "./bench.js";
|
|
12
15
|
export { CHALLENGE_MARKERS, type ChallengeKind, classifyChallenge, detectChallenge, parseRebrowser, parseSannysoft, } from "./detectors.js";
|
|
13
16
|
export { type LoginOptions, loginIdentity } from "./login.js";
|
|
17
|
+
export { LiveError, liveLink, livePause, liveResume, liveStatus } from "./live-client.js";
|
|
18
|
+
export { BackgroundError, type BackgroundSession, type BackgroundState, type CallOptions, type OpenSessionOptions, type StartBackgroundOptions, type StartedBackground, backgroundStatus, openBackgroundSession, startBackground, stopBackground, } from "./client.js";
|
|
19
|
+
export type { LiveStatus } from "./live-server.js";
|
|
14
20
|
export { type TotpParams, TotpError, parseTotpSecret, totp } from "./totp.js";
|
|
15
21
|
export { SealError } from "./sealed.js";
|
|
16
22
|
export { type ExportOptions, type ImportOptions, type SessionSummary, exportSession, importSession, readSessionExport, } from "./session-export.js";
|
|
17
23
|
export { type SubmitPolicy, type VaultAddInput, VaultError, type VaultInfo, type VaultSite, vaultAdd, vaultClear, vaultList, vaultPath, vaultRemove, vaultSetSubmit, } from "./vault.js";
|
|
24
|
+
export { type Flow, FlowError, type FlowLocator, type FlowParam, type FlowStep, type FlowSummary, type FlowTarget, type ParamRef, flowsDir, listFlows, readFlow, removeFlow, } from "./flows.js";
|
|
25
|
+
export { type ReplayOptions, replayFlow } from "./replay.js";
|
|
26
|
+
export { type RecordingSummary, listRecordings, openTraceViewer, recordingsDir, traceViewerCommand, } from "./recordings.js";
|
|
18
27
|
export { mcpEntryPath } from "./patchright.js";
|
|
19
28
|
export { browserPaths as paths } from "turno-core/paths";
|
|
20
29
|
/** Absolute path of the `turno-browser-mcp` executable script of this install. */
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { fileURLToPath } from "node:url";
|
|
2
2
|
export { API_VERSION, api } from "./api.js";
|
|
3
|
-
export {
|
|
3
|
+
export { browserCli as cli } from "./cli/commands.js";
|
|
4
|
+
export { SESSION_CAPABILITY, SESSION_ENDED_METHOD, SESSION_META_KEY } from "./session.js";
|
|
4
5
|
export { manifestSnippet } from "./snippet.js";
|
|
5
6
|
export { IdentityError, addIdentity, assertIdentityId, identityDir, identityExists, identityStateDir, inUseBy, isInUse, isValidIdentityId, listIdentities, parseSingletonLock, removeIdentity, } from "./identities.js";
|
|
6
7
|
export { IDENTITY_FORMAT_VERSION, checkIdentitySettings, identitySettingsPath, readIdentitySettings, systemTimezone, writeIdentitySettings, } from "./settings.js";
|
|
@@ -12,10 +13,15 @@ export { buildArgv, extractIdentity } from "./argv.js";
|
|
|
12
13
|
export { DETECTORS, ENGINES, ENGINE_ALIASES, defaultEngines, engineId, formatBenchTable, runBench, unavailableReason, } from "./bench.js";
|
|
13
14
|
export { CHALLENGE_MARKERS, classifyChallenge, detectChallenge, parseRebrowser, parseSannysoft, } from "./detectors.js";
|
|
14
15
|
export { loginIdentity } from "./login.js";
|
|
16
|
+
export { LiveError, liveLink, livePause, liveResume, liveStatus } from "./live-client.js";
|
|
17
|
+
export { BackgroundError, backgroundStatus, openBackgroundSession, startBackground, stopBackground, } from "./client.js";
|
|
15
18
|
export { TotpError, parseTotpSecret, totp } from "./totp.js";
|
|
16
19
|
export { SealError } from "./sealed.js";
|
|
17
20
|
export { exportSession, importSession, readSessionExport, } from "./session-export.js";
|
|
18
21
|
export { VaultError, vaultAdd, vaultClear, vaultList, vaultPath, vaultRemove, vaultSetSubmit, } from "./vault.js";
|
|
22
|
+
export { FlowError, flowsDir, listFlows, readFlow, removeFlow, } from "./flows.js";
|
|
23
|
+
export { replayFlow } from "./replay.js";
|
|
24
|
+
export { listRecordings, openTraceViewer, recordingsDir, traceViewerCommand, } from "./recordings.js";
|
|
19
25
|
export { mcpEntryPath } from "./patchright.js";
|
|
20
26
|
export { browserPaths as paths } from "turno-core/paths";
|
|
21
27
|
/** Absolute path of the `turno-browser-mcp` executable script of this install. */
|
package/dist/lifecycle.d.ts
CHANGED
|
@@ -52,10 +52,18 @@ export interface FindOptions {
|
|
|
52
52
|
* lock points at some other program (a reused PID).
|
|
53
53
|
*/
|
|
54
54
|
export declare function findProfileChrome(userDataDir: string, opts?: FindOptions): ProfileChrome | undefined;
|
|
55
|
+
/**
|
|
56
|
+
* How long `stopChrome` waits for the group to go after SIGKILL. A SIGKILL is delivered at
|
|
57
|
+
* once, but the group is gone only when the kernel has torn down every member and init (or a
|
|
58
|
+
* subreaper) has reaped each one: until then `kill(-pgid, 0)` still finds the zombies. On a
|
|
59
|
+
* loaded two-core CI runner with several Chromes that took more than 2 seconds, and the sweep
|
|
60
|
+
* then reported a Chrome that "did not stop" while it was already dying.
|
|
61
|
+
*/
|
|
62
|
+
export declare const KILL_WAIT_MS = 5000;
|
|
55
63
|
export interface StopOptions {
|
|
56
64
|
/** How long the browser gets to quit after SIGTERM. */
|
|
57
65
|
graceMs?: number;
|
|
58
|
-
/** How long to wait for the group to go after SIGKILL. */
|
|
66
|
+
/** How long to wait for the group to go after SIGKILL (default `KILL_WAIT_MS`). */
|
|
59
67
|
killMs?: number;
|
|
60
68
|
signal?: Signal;
|
|
61
69
|
alive?: Alive;
|
|
@@ -79,6 +87,8 @@ export interface ShutdownOptions {
|
|
|
79
87
|
graceMs?: number;
|
|
80
88
|
/** Time Chrome gets after SIGTERM before this process exits and its group gets SIGKILL. */
|
|
81
89
|
termMs?: number;
|
|
90
|
+
/** False for a server without an MCP client on stdin (`--background`): its stdin closing means nothing. */
|
|
91
|
+
stdin?: boolean;
|
|
82
92
|
}
|
|
83
93
|
/**
|
|
84
94
|
* Bound the shutdown of this server. The program's own watchdog closes the browser when stdin
|
package/dist/lifecycle.js
CHANGED
|
@@ -85,6 +85,14 @@ export function findProfileChrome(userDataDir, opts = {}) {
|
|
|
85
85
|
const orphaned = members.every((m) => parentIsInit(m.ppid, table) || table.get(m.ppid)?.pgid === pid);
|
|
86
86
|
return { pid, pgid: pid, leaderAlive: false, owner: orphaned ? "orphan" : "other" };
|
|
87
87
|
}
|
|
88
|
+
/**
|
|
89
|
+
* How long `stopChrome` waits for the group to go after SIGKILL. A SIGKILL is delivered at
|
|
90
|
+
* once, but the group is gone only when the kernel has torn down every member and init (or a
|
|
91
|
+
* subreaper) has reaped each one: until then `kill(-pgid, 0)` still finds the zombies. On a
|
|
92
|
+
* loaded two-core CI runner with several Chromes that took more than 2 seconds, and the sweep
|
|
93
|
+
* then reported a Chrome that "did not stop" while it was already dying.
|
|
94
|
+
*/
|
|
95
|
+
export const KILL_WAIT_MS = 5_000;
|
|
88
96
|
async function waitUntil(cond, ms) {
|
|
89
97
|
const end = Date.now() + ms;
|
|
90
98
|
while (!cond()) {
|
|
@@ -110,7 +118,7 @@ export async function stopChrome(target, opts = {}) {
|
|
|
110
118
|
if (await waitUntil(gone, opts.graceMs ?? 3000))
|
|
111
119
|
return "exited";
|
|
112
120
|
signal(handle, "SIGKILL");
|
|
113
|
-
return (await waitUntil(gone, opts.killMs ??
|
|
121
|
+
return (await waitUntil(gone, opts.killMs ?? KILL_WAIT_MS)) ? "killed" : "survived";
|
|
114
122
|
}
|
|
115
123
|
/** Stop a Chrome that an earlier run left behind on this profile (see the module comment). */
|
|
116
124
|
export async function sweepOrphan(userDataDir, opts = {}) {
|
|
@@ -148,7 +156,8 @@ export function installShutdownGuard(userDataDir, opts = {}) {
|
|
|
148
156
|
setTimeout(() => process.exit(0), termMs).unref();
|
|
149
157
|
}, graceMs).unref();
|
|
150
158
|
};
|
|
151
|
-
|
|
159
|
+
if (opts.stdin !== false)
|
|
160
|
+
process.stdin.once("close", begin);
|
|
152
161
|
process.on("SIGTERM", begin);
|
|
153
162
|
process.on("SIGINT", begin);
|
|
154
163
|
process.on("SIGHUP", () => {
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { type LiveFile } from "./live.js";
|
|
2
|
+
import type { LiveStatus } from "./live-server.js";
|
|
3
|
+
/**
|
|
4
|
+
* What `turno browser live|pause|resume` use: the running server's live view, reached over its
|
|
5
|
+
* control socket (`live-server.ts`). Nothing here holds a token; a link for the view comes
|
|
6
|
+
* from the server with a code that works once.
|
|
7
|
+
*/
|
|
8
|
+
export declare class LiveError extends Error {
|
|
9
|
+
name: string;
|
|
10
|
+
}
|
|
11
|
+
/** The live.json of a running server, or undefined (a stale one is removed). */
|
|
12
|
+
export declare function readLiveFile(id: string, env?: NodeJS.ProcessEnv): LiveFile | undefined;
|
|
13
|
+
/** The live view's state, or undefined when no server serves it. */
|
|
14
|
+
export declare function liveStatus(id: string, env?: NodeJS.ProcessEnv): Promise<LiveStatus | undefined>;
|
|
15
|
+
/** Pauses the agent of `id`: its next call waits until `liveResume`. */
|
|
16
|
+
export declare function livePause(id: string, env?: NodeJS.ProcessEnv): Promise<LiveStatus>;
|
|
17
|
+
export declare function liveResume(id: string, env?: NodeJS.ProcessEnv): Promise<LiveStatus>;
|
|
18
|
+
/**
|
|
19
|
+
* A link that opens the view once, within two minutes. It carries a one-time code: open it,
|
|
20
|
+
* do not print or log it.
|
|
21
|
+
*/
|
|
22
|
+
export declare function liveLink(id: string, env?: NodeJS.ProcessEnv): Promise<string>;
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { readFileSync, rmSync } from "node:fs";
|
|
2
|
+
import { request } from "node:http";
|
|
3
|
+
import { pidAlive } from "turno-core/process";
|
|
4
|
+
import { assertIdentityId } from "./ids.js";
|
|
5
|
+
import { liveFilePath } from "./live.js";
|
|
6
|
+
import { names } from "./names.js";
|
|
7
|
+
/**
|
|
8
|
+
* What `turno browser live|pause|resume` use: the running server's live view, reached over its
|
|
9
|
+
* control socket (`live-server.ts`). Nothing here holds a token; a link for the view comes
|
|
10
|
+
* from the server with a code that works once.
|
|
11
|
+
*/
|
|
12
|
+
export class LiveError extends Error {
|
|
13
|
+
name = "LiveError";
|
|
14
|
+
}
|
|
15
|
+
/** The live.json of a running server, or undefined (a stale one is removed). */
|
|
16
|
+
export function readLiveFile(id, env = process.env) {
|
|
17
|
+
assertIdentityId(id);
|
|
18
|
+
const file = liveFilePath(id, env);
|
|
19
|
+
let data;
|
|
20
|
+
try {
|
|
21
|
+
data = JSON.parse(readFileSync(file, "utf8"));
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
return undefined;
|
|
25
|
+
}
|
|
26
|
+
if (data.version !== 1 || typeof data.pid !== "number" || typeof data.socket !== "string")
|
|
27
|
+
return undefined;
|
|
28
|
+
if (!pidAlive(data.pid)) {
|
|
29
|
+
rmSync(file, { force: true });
|
|
30
|
+
return undefined;
|
|
31
|
+
}
|
|
32
|
+
return data;
|
|
33
|
+
}
|
|
34
|
+
function call(socket, method, path, payload = {}) {
|
|
35
|
+
return new Promise((resolve, reject) => {
|
|
36
|
+
const req = request({ socketPath: socket, method, path, headers: { host: "turno-live" }, timeout: 10_000 }, (res) => {
|
|
37
|
+
const chunks = [];
|
|
38
|
+
res.on("data", (c) => chunks.push(c));
|
|
39
|
+
res.on("end", () => {
|
|
40
|
+
let body;
|
|
41
|
+
try {
|
|
42
|
+
body = JSON.parse(Buffer.concat(chunks).toString("utf8"));
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
body = undefined;
|
|
46
|
+
}
|
|
47
|
+
resolve({ status: res.statusCode ?? 0, body });
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
req.on("timeout", () => req.destroy(new Error("the live view server did not answer in 10s")));
|
|
51
|
+
req.on("error", reject);
|
|
52
|
+
if (method === "POST")
|
|
53
|
+
req.end(JSON.stringify(payload));
|
|
54
|
+
else
|
|
55
|
+
req.end();
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
async function ask(id, method, path, env, payload) {
|
|
59
|
+
const live = readLiveFile(id, env);
|
|
60
|
+
if (!live)
|
|
61
|
+
throw new LiveError(`no turno-browser-mcp serves "${id}" with the live view right now. It is off by default: turn it on with ${names().browser} set ${id} --live true, and it starts with the next server (an agent session starts one).`);
|
|
62
|
+
let out;
|
|
63
|
+
try {
|
|
64
|
+
out = await call(live.socket, method, path, payload);
|
|
65
|
+
}
|
|
66
|
+
catch (error) {
|
|
67
|
+
throw new LiveError(`cannot reach the live view of "${id}" (pid ${live.pid}): ${error.message}`);
|
|
68
|
+
}
|
|
69
|
+
const error = out.body?.error;
|
|
70
|
+
if (out.status !== 200)
|
|
71
|
+
throw new LiveError(typeof error === "string" ? error : `the live view answered ${out.status}`);
|
|
72
|
+
return out.body;
|
|
73
|
+
}
|
|
74
|
+
/** The live view's state, or undefined when no server serves it. */
|
|
75
|
+
export async function liveStatus(id, env = process.env) {
|
|
76
|
+
if (!readLiveFile(id, env))
|
|
77
|
+
return undefined;
|
|
78
|
+
return (await ask(id, "GET", "/state", env));
|
|
79
|
+
}
|
|
80
|
+
/** Pauses the agent of `id`: its next call waits until `liveResume`. */
|
|
81
|
+
export async function livePause(id, env = process.env) {
|
|
82
|
+
// The command the person typed, so the agent is told who paused it (`turno browser pause` inside turno).
|
|
83
|
+
return (await ask(id, "POST", "/pause", env, { by: `${names().browser} pause` }));
|
|
84
|
+
}
|
|
85
|
+
export async function liveResume(id, env = process.env) {
|
|
86
|
+
return (await ask(id, "POST", "/resume", env));
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* A link that opens the view once, within two minutes. It carries a one-time code: open it,
|
|
90
|
+
* do not print or log it.
|
|
91
|
+
*/
|
|
92
|
+
export async function liveLink(id, env = process.env) {
|
|
93
|
+
return (await ask(id, "POST", "/link", env)).url;
|
|
94
|
+
}
|
|
95
|
+
//# sourceMappingURL=live-client.js.map
|