@humanjs/playwright 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/dist/index.js CHANGED
@@ -1,5 +1,12 @@
1
- import { resolvePersonality, createRng, planScroll, countWords, computeReadingDwellMs, planReadingScan, planTypeKeystrokes, bezierPath, humanizePath } from '@humanjs/core';
2
- export { applyMicroJitter, applyVelocityProfile, bezierPath, blend, careful, computeReadingDwellMs, countWords, createRng, distracted, fast, humanizePath, planScroll, planTypeKeystrokes, precise, resolvePersonality } from '@humanjs/core';
1
+ import { resolvePersonality, createRng, sleep as sleep$1, planScroll, countWords, computeReadingDwellMs, planReadingScan, planTypeKeystrokes, bezierPath, humanizePath } from '@humanjs/core';
2
+ export { applyMicroJitter, applyVelocityProfile, bezierPath, blend, careful, computeReadingDwellMs, countWords, createRng, distracted, fast, humanizePath, planScroll, planTypeKeystrokes, precise, resolvePersonality, sleep } from '@humanjs/core';
3
+ import { spawn } from 'child_process';
4
+ import { rmSync } from 'fs';
5
+ import { mkdir, writeFile, mkdtemp, rm } from 'fs/promises';
6
+ import { dirname, extname, join } from 'path';
7
+ import ffmpegStatic from 'ffmpeg-static';
8
+ import { tmpdir } from 'os';
9
+ export { chromium, firefox, webkit } from 'playwright';
3
10
 
4
11
  // src/index.ts
5
12
 
@@ -56,6 +63,73 @@ async function dispatchKey(page, key) {
56
63
  await page.keyboard.insertText(key);
57
64
  }
58
65
  }
66
+ async function executePaste(target, value, ctx) {
67
+ if (value.length === 0) return { characters: 0 };
68
+ const locator = typeof target === "string" ? ctx.page.locator(target) : target;
69
+ await locator.focus();
70
+ await ctx.page.keyboard.insertText(value);
71
+ return { characters: value.length };
72
+ }
73
+ async function executePress(key, ctx) {
74
+ const dispatched = resolveChord(key);
75
+ await ctx.page.keyboard.press(dispatched);
76
+ return { dispatched };
77
+ }
78
+ function resolveChord(key) {
79
+ const parts = key.split("+").map((p) => p.trim()).filter((p) => p.length > 0);
80
+ if (parts.length === 0) {
81
+ throw new Error(`Invalid key: ${JSON.stringify(key)} \u2014 empty or only separators`);
82
+ }
83
+ const keyToken = parts[parts.length - 1];
84
+ if (keyToken === void 0) {
85
+ throw new Error(`Invalid key: ${JSON.stringify(key)} \u2014 missing key`);
86
+ }
87
+ const modifierTokens = parts.slice(0, -1);
88
+ const modifiers = [];
89
+ for (const token of modifierTokens) {
90
+ const resolved = resolveModifier(token);
91
+ if (resolved === null) {
92
+ throw new Error(
93
+ `Invalid key modifier: ${JSON.stringify(token)} in ${JSON.stringify(key)}. Use one of: Mod/CmdOrCtrl/CommandOrControl, Cmd/Command/Meta/Win/Super, Ctrl/Control, Alt/Option/Opt, Shift.`
94
+ );
95
+ }
96
+ modifiers.push(resolved);
97
+ }
98
+ return [...modifiers, normalizeKey(keyToken)].join("+");
99
+ }
100
+ function resolveModifier(token) {
101
+ const lower = token.toLowerCase();
102
+ switch (lower) {
103
+ case "mod":
104
+ case "cmdorctrl":
105
+ case "commandorcontrol":
106
+ return isMac() ? "Meta" : "Control";
107
+ case "cmd":
108
+ case "command":
109
+ case "meta":
110
+ case "win":
111
+ case "super":
112
+ return "Meta";
113
+ case "ctrl":
114
+ case "control":
115
+ return "Control";
116
+ case "alt":
117
+ case "option":
118
+ case "opt":
119
+ return "Alt";
120
+ case "shift":
121
+ return "Shift";
122
+ default:
123
+ return null;
124
+ }
125
+ }
126
+ function normalizeKey(key) {
127
+ if (key.length === 1) return key.toUpperCase();
128
+ return key.charAt(0).toUpperCase() + key.slice(1);
129
+ }
130
+ function isMac() {
131
+ return process.platform === "darwin";
132
+ }
59
133
 
60
134
  // src/internal/mouse-walk.ts
61
135
  async function walkMouseAlongPath(page, path, durationMs) {
@@ -70,168 +144,6 @@ async function walkMouseAlongPath(page, path, durationMs) {
70
144
  }
71
145
  }
72
146
  }
73
-
74
- // src/mouse/index.ts
75
- async function executeClick(target, ctx) {
76
- const locator = typeof target === "string" ? ctx.page.locator(target) : target;
77
- if (ctx.speed === "instant") {
78
- const box2 = await locator.boundingBox();
79
- await locator.click();
80
- const center = box2 ? { x: box2.x + box2.width / 2, y: box2.y + box2.height / 2 } : ctx.getMousePosition();
81
- ctx.setMousePosition(center);
82
- return { target: center };
83
- }
84
- const box = await locator.boundingBox();
85
- if (!box) {
86
- throw new Error(
87
- `Cannot click: element not found or has no bounding box (target: ${describeTarget(target)})`
88
- );
89
- }
90
- const targetPoint = pickClickPoint(box, ctx.rng);
91
- const startPoint = ctx.getMousePosition();
92
- const rawPath = bezierPath(startPoint, targetPoint, ctx.rng, {
93
- curvature: ctx.personality.mouse.curvature
94
- });
95
- const path = humanizePath(rawPath, ctx.rng);
96
- const travelMs = computeTravelTime(path, ctx.personality, ctx.speed, ctx.rng);
97
- await walkMouseAlongPath(ctx.page, path, travelMs);
98
- const preClickMs = computeDwellTime(
99
- ctx.personality.dwell.preClickMs,
100
- ctx.personality.dwell.preClickJitter,
101
- ctx.personality,
102
- ctx.speed,
103
- ctx.rng
104
- );
105
- if (preClickMs > 0) await sleep(preClickMs);
106
- ctx.setMousePosition(targetPoint);
107
- await ctx.page.mouse.click(targetPoint.x, targetPoint.y);
108
- const postActionMs = computeDwellTime(
109
- ctx.personality.dwell.postActionMs,
110
- ctx.personality.dwell.postActionJitter,
111
- ctx.personality,
112
- ctx.speed,
113
- ctx.rng
114
- );
115
- if (postActionMs > 0) await sleep(postActionMs);
116
- return { target: targetPoint };
117
- }
118
- function pickClickPoint(box, rng) {
119
- const cx = box.x + box.width / 2;
120
- const cy = box.y + box.height / 2;
121
- const x = clamp(cx + rng.nextGaussian(0, box.width / 8), box.x, box.x + box.width);
122
- const y = clamp(cy + rng.nextGaussian(0, box.height / 8), box.y, box.y + box.height);
123
- return { x, y };
124
- }
125
- function computeTravelTime(path, personality, speed, rng) {
126
- let distance = 0;
127
- for (let i = 1; i < path.length; i++) {
128
- const prev = path[i - 1];
129
- const curr = path[i];
130
- if (!prev || !curr) continue;
131
- distance += Math.hypot(curr.x - prev.x, curr.y - prev.y);
132
- }
133
- const baseTime = distance / 1e3 * personality.mouse.travelTimeMs;
134
- const jitterMag = baseTime * personality.mouse.travelTimeJitter;
135
- const jitter = rng.nextFloat(-jitterMag, jitterMag);
136
- const total = (baseTime + jitter) * personality.speed * speedModeFactor(speed);
137
- return Math.max(0, total);
138
- }
139
- function describeTarget(target) {
140
- return typeof target === "string" ? target : target.toString?.() ?? "locator";
141
- }
142
- function clamp(value, min, max) {
143
- return value < min ? min : value > max ? max : value;
144
- }
145
- async function executeRead(target, ctx, options = {}) {
146
- let words = 0;
147
- let locator;
148
- if (typeof target === "string") {
149
- locator = ctx.page.locator(target);
150
- } else if ("words" in target) {
151
- words = target.words;
152
- } else if ("text" in target) {
153
- words = countWords(target.text);
154
- } else {
155
- locator = target;
156
- }
157
- let autoDetectedKind;
158
- if (locator) {
159
- if (options.scrollIntoView) {
160
- await locator.scrollIntoViewIfNeeded();
161
- }
162
- const text = await locator.innerText().catch(() => "");
163
- words = countWords(text);
164
- if (options.kind === void 0) {
165
- autoDetectedKind = await detectKindFromTag(locator);
166
- }
167
- }
168
- const kind = options.kind ?? autoDetectedKind ?? "prose";
169
- const durationMs = computeReadingDwellMs(words, ctx.personality.reading, ctx.rng, {
170
- kind,
171
- wpmMultiplier: options.wpmMultiplier,
172
- personalitySpeed: ctx.personality.speed,
173
- speedFactor: speedModeFactor(ctx.speed)
174
- });
175
- if (options.withMotion && locator && durationMs > 0) {
176
- const box = await locator.boundingBox().catch(() => null);
177
- if (box) {
178
- const lineRects = await getLineRects(locator).catch(() => []);
179
- const path = planReadingScan(box, ctx.rng, {
180
- start: ctx.getMousePosition(),
181
- lineRects: lineRects.length > 0 ? lineRects : void 0
182
- });
183
- await walkMouseAlongPath(ctx.page, path, durationMs);
184
- const final = path[path.length - 1];
185
- if (final) ctx.setMousePosition(final);
186
- return { words, durationMs, kind };
187
- }
188
- }
189
- if (durationMs > 0) await sleep(durationMs);
190
- return { words, durationMs, kind };
191
- }
192
- async function getLineRects(locator) {
193
- const result = await locator.evaluate((el) => {
194
- const walker = el.ownerDocument.createTreeWalker(el, 4);
195
- const rects = [];
196
- let node = walker.nextNode();
197
- while (node) {
198
- const text = node.textContent ?? "";
199
- if (text.trim().length > 0) {
200
- const range = el.ownerDocument.createRange();
201
- range.selectNodeContents(node);
202
- for (const r of Array.from(range.getClientRects())) {
203
- if (r.width > 0 && r.height > 0) {
204
- rects.push({ x: r.x, y: r.y, width: r.width, height: r.height });
205
- }
206
- }
207
- }
208
- node = walker.nextNode();
209
- }
210
- rects.sort((a, b) => a.y - b.y || a.x - b.x);
211
- const merged = [];
212
- for (const r of rects) {
213
- const last = merged[merged.length - 1];
214
- if (last && Math.abs(last.y - r.y) < 1 && r.x - (last.x + last.width) < 6) {
215
- const right = Math.max(last.x + last.width, r.x + r.width);
216
- const bottom = Math.max(last.y + last.height, r.y + r.height);
217
- last.width = right - last.x;
218
- last.height = bottom - last.y;
219
- } else {
220
- merged.push({ ...r });
221
- }
222
- }
223
- return merged;
224
- });
225
- if (!Array.isArray(result)) return [];
226
- return result.filter(
227
- (r) => r != null && typeof r === "object" && typeof r.x === "number" && typeof r.y === "number" && typeof r.width === "number" && typeof r.height === "number"
228
- );
229
- }
230
- async function detectKindFromTag(locator) {
231
- const tag = await locator.evaluate((el) => el.tagName?.toLowerCase() ?? "").catch(() => "");
232
- if (tag === "pre" || tag === "code") return "code";
233
- return void 0;
234
- }
235
147
  var RESERVED_TARGETS = /* @__PURE__ */ new Set(["natural", "end", "top"]);
236
148
  async function executeScroll(target, ctx, options = {}) {
237
149
  const { page, personality, rng, speed } = ctx;
@@ -244,7 +156,7 @@ async function executeScroll(target, ctx, options = {}) {
244
156
  }
245
157
  const from = geom.current;
246
158
  const targetPos = await resolveTarget(target, ctx, geom, container, axis, options.block);
247
- const to = clamp2(targetPos, 0, Math.max(0, geom.total - geom.viewport));
159
+ const to = clamp(targetPos, 0, Math.max(0, geom.total - geom.viewport));
248
160
  const distance = to - from;
249
161
  if (distance === 0) {
250
162
  return { from, to, distance: 0, durationMs: 0 };
@@ -401,13 +313,797 @@ async function walkSegments(page, segments, axis, container) {
401
313
  }
402
314
  }
403
315
  }
316
+ function clamp(value, min, max) {
317
+ return value < min ? min : value > max ? max : value;
318
+ }
319
+
320
+ // src/mouse/index.ts
321
+ async function executeClick(target, ctx, options = {}) {
322
+ const button = options.button ?? "left";
323
+ const locator = typeof target === "string" ? ctx.page.locator(target) : target;
324
+ if (ctx.speed === "instant") {
325
+ const box = await locator.boundingBox();
326
+ await locator.click({ button });
327
+ const center = box ? { x: box.x + box.width / 2, y: box.y + box.height / 2 } : ctx.getMousePosition();
328
+ ctx.setMousePosition(center);
329
+ return { target: center };
330
+ }
331
+ const targetPoint = await moveToTarget(target, ctx, "click");
332
+ const preClickMs = computeDwellTime(
333
+ ctx.personality.dwell.preClickMs,
334
+ ctx.personality.dwell.preClickJitter,
335
+ ctx.personality,
336
+ ctx.speed,
337
+ ctx.rng
338
+ );
339
+ if (preClickMs > 0) await sleep(preClickMs);
340
+ ctx.setMousePosition(targetPoint);
341
+ await ctx.page.mouse.click(targetPoint.x, targetPoint.y, { button });
342
+ const postActionMs = computeDwellTime(
343
+ ctx.personality.dwell.postActionMs,
344
+ ctx.personality.dwell.postActionJitter,
345
+ ctx.personality,
346
+ ctx.speed,
347
+ ctx.rng
348
+ );
349
+ if (postActionMs > 0) await sleep(postActionMs);
350
+ return { target: targetPoint };
351
+ }
352
+ async function executeHover(target, ctx) {
353
+ if (ctx.speed === "instant") {
354
+ const box = await readBoxWithAutoScroll(target, ctx, "hover");
355
+ const center = { x: box.x + box.width / 2, y: box.y + box.height / 2 };
356
+ await ctx.page.mouse.move(center.x, center.y);
357
+ ctx.setMousePosition(center);
358
+ return { target: center };
359
+ }
360
+ const targetPoint = await moveToTarget(target, ctx, "hover");
361
+ const dwellMs = computeDwellTime(
362
+ ctx.personality.dwell.preClickMs,
363
+ ctx.personality.dwell.preClickJitter,
364
+ ctx.personality,
365
+ ctx.speed,
366
+ ctx.rng
367
+ );
368
+ if (dwellMs > 0) await sleep(dwellMs);
369
+ ctx.setMousePosition(targetPoint);
370
+ return { target: targetPoint };
371
+ }
372
+ async function executeDrag(from, to, ctx) {
373
+ const scrollYBeforeResolve = await readScrollY(ctx.page);
374
+ let { point: fromPoint, box: fromBox } = await resolveTargetPointAndBox(from, ctx, "drag");
375
+ let { point: toPoint, box: toBox } = await resolveTargetPointAndBox(to, ctx, "drag");
376
+ const resolveScrollDelta = await readScrollY(ctx.page) - scrollYBeforeResolve;
377
+ if (resolveScrollDelta !== 0) {
378
+ if (isPoint(from)) fromPoint = { x: fromPoint.x, y: fromPoint.y - resolveScrollDelta };
379
+ if (isPoint(to)) toPoint = { x: toPoint.x, y: toPoint.y - resolveScrollDelta };
380
+ }
381
+ if (ctx.speed === "instant") {
382
+ await ctx.page.mouse.move(fromPoint.x, fromPoint.y);
383
+ await ctx.page.mouse.down();
384
+ await ctx.page.mouse.move(toPoint.x, toPoint.y);
385
+ await ctx.page.mouse.up();
386
+ ctx.setMousePosition(toPoint);
387
+ return { from: fromPoint, to: toPoint };
388
+ }
389
+ if (fromBox && toBox) {
390
+ const scrollDelta = computeCurveScrollDelta(
391
+ fromPoint,
392
+ toPoint,
393
+ ctx.page.viewportSize(),
394
+ ctx.personality.mouse.curvature
395
+ );
396
+ if (scrollDelta !== 0) {
397
+ await executeScroll({ by: scrollDelta }, ctx, {});
398
+ const refreshedFrom = await resolveTargetPointAndBox(from, ctx, "drag");
399
+ fromPoint = refreshedFrom.point;
400
+ fromBox = refreshedFrom.box;
401
+ const refreshedTo = await resolveTargetPointAndBox(to, ctx, "drag");
402
+ toPoint = refreshedTo.point;
403
+ toBox = refreshedTo.box;
404
+ }
405
+ }
406
+ await maybeMisclickBeat(ctx, fromBox, fromPoint);
407
+ await walkBezierTo(fromPoint, ctx);
408
+ ctx.setMousePosition(fromPoint);
409
+ const preDragMs = computeDwellTime(
410
+ ctx.personality.dwell.preClickMs,
411
+ ctx.personality.dwell.preClickJitter,
412
+ ctx.personality,
413
+ ctx.speed,
414
+ ctx.rng
415
+ );
416
+ if (preDragMs > 0) await sleep(preDragMs);
417
+ await ctx.page.mouse.down();
418
+ await maybeMisclickBeat(ctx, toBox, toPoint);
419
+ await walkBezierTo(toPoint, ctx);
420
+ await ctx.page.mouse.up();
421
+ ctx.setMousePosition(toPoint);
422
+ const postActionMs = computeDwellTime(
423
+ ctx.personality.dwell.postActionMs,
424
+ ctx.personality.dwell.postActionJitter,
425
+ ctx.personality,
426
+ ctx.speed,
427
+ ctx.rng
428
+ );
429
+ if (postActionMs > 0) await sleep(postActionMs);
430
+ return { from: fromPoint, to: toPoint };
431
+ }
432
+ async function executeMove(target, ctx) {
433
+ const point = await resolveTargetPoint(target, ctx, "move");
434
+ if (ctx.speed === "instant") {
435
+ await ctx.page.mouse.move(point.x, point.y);
436
+ ctx.setMousePosition(point);
437
+ return { target: point };
438
+ }
439
+ await walkBezierTo(point, ctx);
440
+ ctx.setMousePosition(point);
441
+ return { target: point };
442
+ }
443
+ async function moveToTarget(target, ctx, action) {
444
+ const box = await readBoxWithAutoScroll(target, ctx, action);
445
+ const targetPoint = pickClickPoint(box, ctx.rng, ctx.personality.mouse.clickSpread);
446
+ if (action === "click") await maybeMisclickBeat(ctx, box, targetPoint);
447
+ await walkBezierTo(targetPoint, ctx);
448
+ return targetPoint;
449
+ }
450
+ async function walkBezierTo(to, ctx) {
451
+ const startPoint = ctx.getMousePosition();
452
+ const rawPath = bezierPath(startPoint, to, ctx.rng, {
453
+ curvature: ctx.personality.mouse.curvature
454
+ });
455
+ const path = humanizePath(rawPath, ctx.rng);
456
+ const travelMs = computeTravelTime(path, ctx.personality, ctx.speed, ctx.rng);
457
+ await walkMouseAlongPath(ctx.page, path, travelMs);
458
+ }
459
+ async function resolveTargetPoint(target, ctx, action) {
460
+ if (isPoint(target)) return target;
461
+ return resolveLocatorPoint(target, ctx, action);
462
+ }
463
+ async function resolveTargetPointAndBox(target, ctx, action) {
464
+ if (isPoint(target)) return { point: target, box: null };
465
+ const box = await readBoxWithAutoScroll(target, ctx, action);
466
+ const point = pickClickPoint(box, ctx.rng, ctx.personality.mouse.clickSpread);
467
+ return { point, box };
468
+ }
469
+ async function resolveLocatorPoint(target, ctx, action) {
470
+ const box = await readBoxWithAutoScroll(target, ctx, action);
471
+ return pickClickPoint(box, ctx.rng, ctx.personality.mouse.clickSpread);
472
+ }
473
+ async function readBoxWithAutoScroll(target, ctx, action) {
474
+ const locator = typeof target === "string" ? ctx.page.locator(target) : target;
475
+ let box = await locator.boundingBox();
476
+ if (!box) {
477
+ throw new Error(
478
+ `Cannot ${action}: element not found or has no bounding box (target: ${describeTarget(target)})`
479
+ );
480
+ }
481
+ const viewport = ctx.page.viewportSize();
482
+ if (viewport && !isBoxCenterInViewport(box, viewport)) {
483
+ if (ctx.speed === "instant") {
484
+ await locator.scrollIntoViewIfNeeded();
485
+ } else {
486
+ await executeScroll(locator, ctx, { block: "center" });
487
+ }
488
+ box = await locator.boundingBox();
489
+ if (!box) {
490
+ throw new Error(
491
+ `Cannot ${action}: element disappeared after scrolling into view (target: ${describeTarget(target)})`
492
+ );
493
+ }
494
+ }
495
+ return box;
496
+ }
497
+ function isBoxCenterInViewport(box, viewport) {
498
+ const cx = box.x + box.width / 2;
499
+ const cy = box.y + box.height / 2;
500
+ return cx >= 0 && cx <= viewport.width && cy >= 0 && cy <= viewport.height;
501
+ }
502
+ async function readScrollY(page) {
503
+ if (typeof page.evaluate !== "function") return 0;
504
+ return page.evaluate(() => window.scrollY);
505
+ }
506
+ var CURVE_VIEWPORT_MARGIN = 20;
507
+ function computeCurveScrollDelta(from, to, viewport, curvature) {
508
+ if (!viewport) return 0;
509
+ const distance = Math.hypot(to.x - from.x, to.y - from.y);
510
+ const perpendicularExtent = distance * curvature;
511
+ const minY = Math.min(from.y, to.y) - perpendicularExtent;
512
+ const maxY = Math.max(from.y, to.y) + perpendicularExtent;
513
+ const topOverflow = -minY + CURVE_VIEWPORT_MARGIN;
514
+ const bottomOverflow = maxY + CURVE_VIEWPORT_MARGIN - viewport.height;
515
+ if (bottomOverflow > 0 && bottomOverflow >= topOverflow) return bottomOverflow;
516
+ if (topOverflow > 0) return -topOverflow;
517
+ return 0;
518
+ }
519
+ function isPoint(target) {
520
+ return typeof target === "object" && target !== null && !("boundingBox" in target) && typeof target.x === "number" && typeof target.y === "number";
521
+ }
522
+ var MISCLICK_OFFSET_MIN = 5;
523
+ var MISCLICK_OFFSET_MAX = 15;
524
+ async function maybeMisclickBeat(ctx, box, targetPoint) {
525
+ if (!ctx.rng.chance(ctx.personality.mouse.misclickProbability)) return;
526
+ if (cursorAlreadyOnTarget(ctx.getMousePosition(), box, targetPoint)) return;
527
+ const viewport = ctx.page.viewportSize();
528
+ const misclickPoint = box ? pickMisclickOutsideBox(box, ctx.rng, viewport) : pickMisclickAroundPoint(targetPoint, ctx.rng, viewport);
529
+ if (misclickPoint === null) return;
530
+ await walkBezierTo(misclickPoint, ctx);
531
+ ctx.setMousePosition(misclickPoint);
532
+ const realizeMs = computeDwellTime(
533
+ ctx.personality.dwell.preClickMs,
534
+ ctx.personality.dwell.preClickJitter,
535
+ ctx.personality,
536
+ ctx.speed,
537
+ ctx.rng
538
+ );
539
+ if (realizeMs > 0) await sleep(realizeMs);
540
+ }
541
+ function cursorAlreadyOnTarget(current, box, targetPoint) {
542
+ if (box) {
543
+ return current.x >= box.x && current.x <= box.x + box.width && current.y >= box.y && current.y <= box.y + box.height;
544
+ }
545
+ const dx = current.x - targetPoint.x;
546
+ const dy = current.y - targetPoint.y;
547
+ return Math.hypot(dx, dy) < MISCLICK_OFFSET_MIN;
548
+ }
549
+ function pickMisclickOutsideBox(box, rng, viewport) {
550
+ const edge = rng.nextInt(0, 4);
551
+ const offset = rng.nextFloat(MISCLICK_OFFSET_MIN, MISCLICK_OFFSET_MAX);
552
+ const along = rng.nextFloat(0.2, 0.8);
553
+ let x;
554
+ let y;
555
+ if (edge === 0) {
556
+ x = box.x + box.width * along;
557
+ y = box.y - offset;
558
+ } else if (edge === 1) {
559
+ x = box.x + box.width + offset;
560
+ y = box.y + box.height * along;
561
+ } else if (edge === 2) {
562
+ x = box.x + box.width * along;
563
+ y = box.y + box.height + offset;
564
+ } else {
565
+ x = box.x - offset;
566
+ y = box.y + box.height * along;
567
+ }
568
+ if (viewport) {
569
+ x = clamp2(x, 0, viewport.width - 1);
570
+ y = clamp2(y, 0, viewport.height - 1);
571
+ }
572
+ const insideBox = x >= box.x && x <= box.x + box.width && y >= box.y && y <= box.y + box.height;
573
+ if (insideBox) return null;
574
+ return { x, y };
575
+ }
576
+ function pickMisclickAroundPoint(target, rng, viewport) {
577
+ const angle = rng.nextFloat(0, Math.PI * 2);
578
+ const distance = rng.nextFloat(MISCLICK_OFFSET_MIN, MISCLICK_OFFSET_MAX);
579
+ let x = target.x + Math.cos(angle) * distance;
580
+ let y = target.y + Math.sin(angle) * distance;
581
+ if (viewport) {
582
+ x = clamp2(x, 0, viewport.width - 1);
583
+ y = clamp2(y, 0, viewport.height - 1);
584
+ }
585
+ if (x === target.x && y === target.y) return null;
586
+ return { x, y };
587
+ }
588
+ function pickClickPoint(box, rng, clickSpread) {
589
+ const cx = box.x + box.width / 2;
590
+ const cy = box.y + box.height / 2;
591
+ const sigmaX = box.width * clickSpread;
592
+ const sigmaY = box.height * clickSpread;
593
+ const x = clamp2(cx + rng.nextGaussian(0, sigmaX), box.x, box.x + box.width);
594
+ const y = clamp2(cy + rng.nextGaussian(0, sigmaY), box.y, box.y + box.height);
595
+ return { x, y };
596
+ }
597
+ function computeTravelTime(path, personality, speed, rng) {
598
+ let distance = 0;
599
+ for (let i = 1; i < path.length; i++) {
600
+ const prev = path[i - 1];
601
+ const curr = path[i];
602
+ if (!prev || !curr) continue;
603
+ distance += Math.hypot(curr.x - prev.x, curr.y - prev.y);
604
+ }
605
+ const baseTime = distance / 1e3 * personality.mouse.travelTimeMs;
606
+ const jitterMag = baseTime * personality.mouse.travelTimeJitter;
607
+ const jitter = rng.nextFloat(-jitterMag, jitterMag);
608
+ const total = (baseTime + jitter) * personality.speed * speedModeFactor(speed);
609
+ return Math.max(0, total);
610
+ }
611
+ function describeTarget(target) {
612
+ if (isPoint(target)) return `point(${target.x}, ${target.y})`;
613
+ return typeof target === "string" ? target : target.toString?.() ?? "locator";
614
+ }
404
615
  function clamp2(value, min, max) {
405
616
  return value < min ? min : value > max ? max : value;
406
617
  }
618
+ async function executeRead(target, ctx, options = {}) {
619
+ let words = 0;
620
+ let locator;
621
+ if (typeof target === "string") {
622
+ locator = ctx.page.locator(target);
623
+ } else if ("words" in target) {
624
+ words = target.words;
625
+ } else if ("text" in target) {
626
+ words = countWords(target.text);
627
+ } else {
628
+ locator = target;
629
+ }
630
+ let autoDetectedKind;
631
+ if (locator) {
632
+ if (options.scrollIntoView) {
633
+ await locator.scrollIntoViewIfNeeded();
634
+ }
635
+ const text = await locator.innerText().catch(() => "");
636
+ words = countWords(text);
637
+ if (options.kind === void 0) {
638
+ autoDetectedKind = await detectKindFromTag(locator);
639
+ }
640
+ }
641
+ const kind = options.kind ?? autoDetectedKind ?? "prose";
642
+ const durationMs = computeReadingDwellMs(words, ctx.personality.reading, ctx.rng, {
643
+ kind,
644
+ wpmMultiplier: options.wpmMultiplier,
645
+ personalitySpeed: ctx.personality.speed,
646
+ speedFactor: speedModeFactor(ctx.speed)
647
+ });
648
+ const withMotion = options.withMotion ?? true;
649
+ if (withMotion && locator && durationMs > 0) {
650
+ const box = await locator.boundingBox().catch(() => null);
651
+ if (box) {
652
+ const lineRects = await getLineRects(locator).catch(() => []);
653
+ const path = planReadingScan(box, ctx.rng, {
654
+ start: ctx.getMousePosition(),
655
+ lineRects: lineRects.length > 0 ? lineRects : void 0
656
+ });
657
+ await walkMouseAlongPath(ctx.page, path, durationMs);
658
+ const final = path[path.length - 1];
659
+ if (final) ctx.setMousePosition(final);
660
+ return { words, durationMs, kind };
661
+ }
662
+ }
663
+ if (durationMs > 0) await sleep(durationMs);
664
+ return { words, durationMs, kind };
665
+ }
666
+ async function getLineRects(locator) {
667
+ const result = await locator.evaluate((el) => {
668
+ const walker = el.ownerDocument.createTreeWalker(el, 4);
669
+ const rects = [];
670
+ let node = walker.nextNode();
671
+ while (node) {
672
+ const text = node.textContent ?? "";
673
+ if (text.trim().length > 0) {
674
+ const range = el.ownerDocument.createRange();
675
+ range.selectNodeContents(node);
676
+ for (const r of Array.from(range.getClientRects())) {
677
+ if (r.width > 0 && r.height > 0) {
678
+ rects.push({ x: r.x, y: r.y, width: r.width, height: r.height });
679
+ }
680
+ }
681
+ }
682
+ node = walker.nextNode();
683
+ }
684
+ rects.sort((a, b) => a.y - b.y || a.x - b.x);
685
+ const merged = [];
686
+ for (const r of rects) {
687
+ const last = merged[merged.length - 1];
688
+ if (last && Math.abs(last.y - r.y) < 1 && r.x - (last.x + last.width) < 6) {
689
+ const right = Math.max(last.x + last.width, r.x + r.width);
690
+ const bottom = Math.max(last.y + last.height, r.y + r.height);
691
+ last.width = right - last.x;
692
+ last.height = bottom - last.y;
693
+ } else {
694
+ merged.push({ ...r });
695
+ }
696
+ }
697
+ return merged;
698
+ });
699
+ if (!Array.isArray(result)) return [];
700
+ return result.filter(
701
+ (r) => r != null && typeof r === "object" && typeof r.x === "number" && typeof r.y === "number" && typeof r.width === "number" && typeof r.height === "number"
702
+ );
703
+ }
704
+ async function detectKindFromTag(locator) {
705
+ const tag = await locator.evaluate((el) => el.tagName?.toLowerCase() ?? "").catch(() => "");
706
+ if (tag === "pre" || tag === "code") return "code";
707
+ return void 0;
708
+ }
709
+ var pendingFrameCleanups = /* @__PURE__ */ new Set();
710
+ var exitHandlerInstalled = false;
711
+ function ensureExitHandler() {
712
+ if (exitHandlerInstalled) return;
713
+ exitHandlerInstalled = true;
714
+ process.on("exit", () => {
715
+ for (const dir of pendingFrameCleanups) {
716
+ try {
717
+ rmSync(dir, { recursive: true, force: true });
718
+ } catch {
719
+ }
720
+ }
721
+ pendingFrameCleanups.clear();
722
+ });
723
+ }
724
+ var FFMPEG_PATH = ffmpegStatic;
725
+ var QUALITY_PRESETS = {
726
+ fast: {
727
+ captureFormat: "jpeg",
728
+ captureJpegQuality: 85,
729
+ captureFps: 24,
730
+ crf: 23,
731
+ preset: "fast"
732
+ },
733
+ standard: {
734
+ captureFormat: "jpeg",
735
+ captureJpegQuality: 90,
736
+ captureFps: 30,
737
+ crf: 20,
738
+ preset: "fast"
739
+ },
740
+ high: {
741
+ captureFormat: "jpeg",
742
+ captureJpegQuality: 95,
743
+ captureFps: 30,
744
+ crf: 18,
745
+ preset: "slow",
746
+ // 'animation' suits screen content (large solid regions, sharp edges)
747
+ // better than 'film' which is tuned for live-action grain.
748
+ tune: "animation"
749
+ },
750
+ lossless: {
751
+ // PNG capture for perceptually lossless source frames. Temp files are
752
+ // 10-20× larger than JPEG; output mp4 still benefits from the extra
753
+ // headroom (no JPEG artifacts to preserve).
754
+ captureFormat: "png",
755
+ captureJpegQuality: 100,
756
+ captureFps: 30,
757
+ crf: 12,
758
+ preset: "veryslow",
759
+ tune: "animation"
760
+ }
761
+ };
762
+ function getCaptureSettingsForQuality(quality) {
763
+ const preset = QUALITY_PRESETS[quality];
764
+ return {
765
+ format: preset.captureFormat,
766
+ quality: preset.captureJpegQuality,
767
+ fps: preset.captureFps
768
+ };
769
+ }
770
+ var Recording = class {
771
+ #capture;
772
+ #windowStartMs;
773
+ #windowEndMs;
774
+ #timelineSource;
775
+ // Frames live on disk until `dispose()` is called. Exporters
776
+ // (`toVideo`, `toGif`) are repeatable and interleavable — they read the
777
+ // same frame source, they don't consume it.
778
+ #disposed = false;
779
+ constructor(capture, windowStartMs, windowEndMs, timelineSource) {
780
+ this.#capture = capture;
781
+ this.#windowStartMs = windowStartMs;
782
+ this.#windowEndMs = windowEndMs;
783
+ this.#timelineSource = timelineSource;
784
+ if (capture !== null) {
785
+ pendingFrameCleanups.add(capture.dir);
786
+ ensureExitHandler();
787
+ }
788
+ }
789
+ /** Wall-clock duration of the recorded window. */
790
+ get durationMs() {
791
+ return this.#windowEndMs - this.#windowStartMs;
792
+ }
793
+ /** True if frames were captured during this recording. */
794
+ get hasVideo() {
795
+ return this.#capture !== null;
796
+ }
797
+ /**
798
+ * The structured action timeline of this recording — same data that
799
+ * `toTimeline()` writes to disk.
800
+ */
801
+ get timeline() {
802
+ return {
803
+ version: 1,
804
+ personality: this.#timelineSource.personality,
805
+ seed: this.#timelineSource.seed,
806
+ speed: this.#timelineSource.speed,
807
+ durationMs: this.durationMs,
808
+ events: this.#timelineSource.events
809
+ };
810
+ }
811
+ /**
812
+ * Assembles the captured frames into a video at `outputPath`. The output
813
+ * format is inferred from the extension — `.mp4` (H.264, re-encoded
814
+ * with the configured quality) or `.webm` (VP9).
815
+ *
816
+ * Repeatable and interleavable with `toGif()` — the frame source is read,
817
+ * not consumed. Frames live until you call `dispose()` (or `await using`
818
+ * goes out of scope, or the process exits and the OS reaps `tmpdir`).
819
+ *
820
+ * @returns the resolved output path.
821
+ */
822
+ async toVideo(outputPath, options = {}) {
823
+ if (this.#disposed) {
824
+ throw new Error("Recording.toVideo() called after dispose() \u2014 the source frames are gone.");
825
+ }
826
+ if (this.#capture === null) {
827
+ throw new Error(
828
+ "Recording.toVideo() requires video capture, which was disabled for this recording. Call `human.record(cb)` (default captures video) or pass `output` to @humanjs/recorder's `record()`. `toTimeline()` and `.timeline` work without capture."
829
+ );
830
+ }
831
+ const preset = QUALITY_PRESETS[options.quality ?? "high"];
832
+ const crf = options.crf ?? preset.crf;
833
+ const ffmpegPreset = options.preset ?? preset.preset;
834
+ const tune = options.tune ?? preset.tune;
835
+ const { dir, frames, startedAtMs, stoppedAtMs } = this.#capture;
836
+ if (frames.length === 0) {
837
+ throw new Error(
838
+ "No frames were captured. The recording window may have been too short, or the page may not have rendered any frames before the callback completed."
839
+ );
840
+ }
841
+ await mkdir(dirname(outputPath), { recursive: true });
842
+ const ext = extname(outputPath).toLowerCase();
843
+ if (ext !== ".mp4" && ext !== ".webm") {
844
+ throw new Error(`Unsupported output extension: ${ext || "(none)"}. Use .mp4 or .webm.`);
845
+ }
846
+ const concatPath = `${dir}/concat.txt`;
847
+ const concatBody = buildConcatFile(frames, stoppedAtMs - startedAtMs);
848
+ await writeFile(concatPath, concatBody, "utf8");
849
+ const args = ["-y", "-f", "concat", "-safe", "0", "-i", concatPath, "-vsync", "vfr"];
850
+ if (ext === ".mp4") {
851
+ args.push(
852
+ "-c:v",
853
+ "libx264",
854
+ "-pix_fmt",
855
+ "yuv420p",
856
+ "-crf",
857
+ String(crf),
858
+ "-preset",
859
+ ffmpegPreset
860
+ );
861
+ if (tune) args.push("-tune", tune);
862
+ args.push("-movflags", "+faststart");
863
+ } else {
864
+ args.push(
865
+ "-c:v",
866
+ "libvpx-vp9",
867
+ "-pix_fmt",
868
+ "yuv420p",
869
+ "-crf",
870
+ String(crf),
871
+ "-b:v",
872
+ "0",
873
+ "-deadline",
874
+ ffmpegPreset === "fast" || ffmpegPreset === "veryfast" ? "realtime" : "good"
875
+ );
876
+ }
877
+ args.push(outputPath);
878
+ await runFfmpeg(args);
879
+ return outputPath;
880
+ }
881
+ /**
882
+ * Assembles the captured frames into an animated GIF at `outputPath`.
883
+ * Optimized for embedding in READMEs, PRs, Slack, and docs — uses a
884
+ * per-recording palette (`palettegen` + `paletteuse`) with Bayer dithering
885
+ * so gradients stay smooth without exploding the file size.
886
+ *
887
+ * Repeatable and interleavable with `toVideo()` — call them in any order,
888
+ * any number of times. Frames live until you call `dispose()`.
889
+ *
890
+ * @returns the resolved output path.
891
+ */
892
+ async toGif(outputPath, options = {}) {
893
+ if (this.#disposed) {
894
+ throw new Error("Recording.toGif() called after dispose() \u2014 the source frames are gone.");
895
+ }
896
+ if (this.#capture === null) {
897
+ throw new Error(
898
+ "Recording.toGif() requires video capture, which was disabled for this recording. Call `human.record(cb)` (default captures video) or pass `output` to @humanjs/recorder's `record()`. `toTimeline()` and `.timeline` work without capture."
899
+ );
900
+ }
901
+ const fps = options.fps ?? 15;
902
+ const width = options.width;
903
+ const { dir, frames, startedAtMs, stoppedAtMs } = this.#capture;
904
+ if (frames.length === 0) {
905
+ throw new Error(
906
+ "No frames were captured. The recording window may have been too short, or the page may not have rendered any frames before the callback completed."
907
+ );
908
+ }
909
+ await mkdir(dirname(outputPath), { recursive: true });
910
+ const ext = extname(outputPath).toLowerCase();
911
+ if (ext !== ".gif") {
912
+ throw new Error(`Unsupported output extension: ${ext || "(none)"}. Use .gif.`);
913
+ }
914
+ const concatPath = `${dir}/concat.txt`;
915
+ const concatBody = buildConcatFile(frames, stoppedAtMs - startedAtMs);
916
+ await writeFile(concatPath, concatBody, "utf8");
917
+ const filterSteps = [`fps=${fps}`];
918
+ if (width !== void 0) {
919
+ filterSteps.push(`scale=${width}:-1:flags=lanczos`);
920
+ }
921
+ const preFilter = filterSteps.join(",");
922
+ const filterComplex = `${preFilter},split [a][b]; [a] palettegen=stats_mode=diff [p]; [b][p] paletteuse=dither=bayer:bayer_scale=5`;
923
+ const args = [
924
+ "-y",
925
+ "-f",
926
+ "concat",
927
+ "-safe",
928
+ "0",
929
+ "-i",
930
+ concatPath,
931
+ "-filter_complex",
932
+ filterComplex,
933
+ "-loop",
934
+ "0",
935
+ outputPath
936
+ ];
937
+ await runFfmpeg(args);
938
+ return outputPath;
939
+ }
940
+ /**
941
+ * Writes the structured action timeline to `outputPath` as JSON.
942
+ * Independent of `toVideo()` / `toGif()` — call before, after, in between,
943
+ * or instead. Safe to call multiple times. Unaffected by `dispose()`
944
+ * (the timeline lives in memory, not in the captured-frames temp dir).
945
+ *
946
+ * @returns the resolved output path.
947
+ */
948
+ async toTimeline(outputPath) {
949
+ await mkdir(dirname(outputPath), { recursive: true });
950
+ await writeFile(outputPath, `${JSON.stringify(this.timeline, null, 2)}
951
+ `, "utf8");
952
+ return outputPath;
953
+ }
954
+ /**
955
+ * Releases the captured-frames temp directory. After this call, `toVideo()`
956
+ * and `toGif()` throw — but `toTimeline()` and the in-memory `timeline`
957
+ * still work because those don't depend on the frames.
958
+ *
959
+ * **Optional.** A process-exit handler also sweeps any un-disposed frame
960
+ * dirs, so casual scripts can skip this entirely. Call it explicitly when
961
+ * you want to release frames proactively (long-running services, batch
962
+ * jobs, or anywhere you want predictable disk usage).
963
+ *
964
+ * Idempotent. Safe to call on a Recording that never had a capture
965
+ * (timeline-only mode) — no-op there.
966
+ *
967
+ * Also wired to `Symbol.asyncDispose`, so the explicit-resource-management
968
+ * `await using` syntax (TypeScript ≥ 5.2 / Node ≥ 20.4) works:
969
+ *
970
+ * ```ts
971
+ * await using rec = await human.record(fn);
972
+ * await rec.toVideo('demo.mp4');
973
+ * await rec.toGif('demo.gif');
974
+ * // frames cleaned up automatically when `rec` goes out of scope
975
+ * ```
976
+ */
977
+ async dispose() {
978
+ if (this.#disposed) return;
979
+ if (this.#capture !== null) {
980
+ await this.#capture.cleanup();
981
+ pendingFrameCleanups.delete(this.#capture.dir);
982
+ }
983
+ this.#disposed = true;
984
+ }
985
+ async [Symbol.asyncDispose]() {
986
+ await this.dispose();
987
+ }
988
+ };
989
+ function buildConcatFile(frames, totalMs) {
990
+ const lines = [];
991
+ for (let i = 0; i < frames.length; i++) {
992
+ const frame = frames[i];
993
+ const next = frames[i + 1];
994
+ const nextTMs = next ? next.tMs : totalMs;
995
+ const durationS = Math.max(1e-3, (nextTMs - frame.tMs) / 1e3);
996
+ lines.push(`file '${frame.path.replaceAll("'", "'\\''")}'`);
997
+ lines.push(`duration ${durationS.toFixed(6)}`);
998
+ }
999
+ const last = frames[frames.length - 1];
1000
+ if (last) {
1001
+ lines.push(`file '${last.path.replaceAll("'", "'\\''")}'`);
1002
+ }
1003
+ return `${lines.join("\n")}
1004
+ `;
1005
+ }
1006
+ function runFfmpeg(args) {
1007
+ if (!FFMPEG_PATH) {
1008
+ return Promise.reject(
1009
+ new Error(
1010
+ "ffmpeg-static did not bundle a binary for this platform. Install system ffmpeg and set FFMPEG_PATH, or run on a supported platform."
1011
+ )
1012
+ );
1013
+ }
1014
+ return new Promise((resolve, reject) => {
1015
+ const proc = spawn(FFMPEG_PATH, [...args]);
1016
+ let stderr = "";
1017
+ proc.stderr?.on("data", (chunk) => {
1018
+ stderr += chunk.toString();
1019
+ });
1020
+ proc.on("error", reject);
1021
+ proc.on("close", (code) => {
1022
+ if (code === 0) resolve();
1023
+ else reject(new Error(`ffmpeg exited with code ${code}
1024
+ ${stderr.trim()}`));
1025
+ });
1026
+ });
1027
+ }
1028
+ async function startCapture(page, options = {}) {
1029
+ const format = options.format ?? "jpeg";
1030
+ const quality = options.quality ?? 95;
1031
+ const fps = Math.max(1, Math.min(60, options.fps ?? 30));
1032
+ const intervalMs = 1e3 / fps;
1033
+ const dir = await mkdtemp(join(tmpdir(), "humanjs-capture-"));
1034
+ const frames = [];
1035
+ const ext = format === "png" ? "png" : "jpg";
1036
+ let stopped = false;
1037
+ let frameIndex = 0;
1038
+ const writes = [];
1039
+ const startedAtMs = Date.now();
1040
+ const captureLoop = async () => {
1041
+ while (!stopped) {
1042
+ const loopStart = Date.now();
1043
+ try {
1044
+ const buf = await page.screenshot({
1045
+ type: format,
1046
+ quality: format === "jpeg" ? quality : void 0
1047
+ });
1048
+ if (stopped) return;
1049
+ const idx = frameIndex++;
1050
+ const path = join(dir, `frame_${String(idx).padStart(6, "0")}.${ext}`);
1051
+ const tMs = loopStart - startedAtMs;
1052
+ writes.push(
1053
+ writeFile(path, buf).then(
1054
+ () => {
1055
+ frames.push({ path, tMs });
1056
+ },
1057
+ (err) => {
1058
+ console.warn(`humanjs capture: write failed for frame ${idx}:`, err);
1059
+ }
1060
+ )
1061
+ );
1062
+ } catch (err) {
1063
+ if (stopped) return;
1064
+ console.warn("humanjs capture: screenshot failed, stopping loop:", err);
1065
+ stopped = true;
1066
+ return;
1067
+ }
1068
+ const elapsed = Date.now() - loopStart;
1069
+ const wait = intervalMs - elapsed;
1070
+ if (wait > 0) await sleep$1(wait);
1071
+ }
1072
+ };
1073
+ const loopPromise = captureLoop();
1074
+ const finish = async () => {
1075
+ stopped = true;
1076
+ await loopPromise;
1077
+ await Promise.allSettled(writes);
1078
+ };
1079
+ return {
1080
+ async stop() {
1081
+ await finish();
1082
+ const stoppedAtMs = Date.now();
1083
+ return {
1084
+ dir,
1085
+ frames: [...frames].sort((a, b) => a.tMs - b.tMs),
1086
+ startedAtMs,
1087
+ stoppedAtMs,
1088
+ format,
1089
+ fps,
1090
+ cleanup: () => rm(dir, { recursive: true, force: true }).then(() => void 0)
1091
+ };
1092
+ },
1093
+ async abort() {
1094
+ await finish();
1095
+ await rm(dir, { recursive: true, force: true }).catch(() => void 0);
1096
+ }
1097
+ };
1098
+ }
407
1099
 
408
1100
  // src/mouse-helper/index.ts
409
1101
  var CURSOR_PATH = "M 0 0 L 16 6 L 8 9.5 L 5 19 Z";
1102
+ var INSTALLED_FLAG = /* @__PURE__ */ Symbol.for("@humanjs/playwright:mouse-helper:installed");
410
1103
  async function installMouseHelper(target, options = {}) {
1104
+ const tagged = target;
1105
+ if (tagged[INSTALLED_FLAG]) return;
1106
+ tagged[INSTALLED_FLAG] = true;
411
1107
  const config = {
412
1108
  color: options.color ?? "#f5a55c",
413
1109
  stroke: "#020203",
@@ -417,16 +1113,22 @@ async function installMouseHelper(target, options = {}) {
417
1113
  path: CURSOR_PATH
418
1114
  };
419
1115
  await target.addInitScript(installScript, config);
1116
+ const attachPageHooks = (page) => {
1117
+ page.on("domcontentloaded", () => {
1118
+ page.evaluate(installScript, config).catch(() => void 0);
1119
+ });
1120
+ };
420
1121
  const pages = "pages" in target ? target.pages() : [target];
1122
+ for (const page of pages) attachPageHooks(page);
1123
+ if ("on" in target && "newPage" in target) {
1124
+ target.on("page", attachPageHooks);
1125
+ }
421
1126
  await Promise.all(
422
1127
  pages.map((page) => page.evaluate(installScript, config).catch(() => void 0))
423
1128
  );
424
1129
  }
425
1130
  function installScript(config) {
426
- const guardKey = "__humanjsMouseHelperInstalled";
427
- const w = window;
428
- if (w[guardKey]) return;
429
- w[guardKey] = true;
1131
+ if (document.querySelector("[data-humanjs-cursor]")) return;
430
1132
  const attach = () => {
431
1133
  const cursor = document.createElement("div");
432
1134
  cursor.setAttribute("aria-hidden", "true");
@@ -517,6 +1219,9 @@ async function createHuman(page, options = {}) {
517
1219
  for (const plugin of plugins) {
518
1220
  await plugin.install?.(context);
519
1221
  }
1222
+ let hasRecorded = false;
1223
+ let activeRecordingEvents = null;
1224
+ let activeRecordingStartMs = 0;
520
1225
  async function performAction(action, actionFn) {
521
1226
  for (const plugin of plugins) {
522
1227
  await plugin.beforeAction?.(action);
@@ -524,15 +1229,30 @@ async function createHuman(page, options = {}) {
524
1229
  const startedAt = Date.now();
525
1230
  try {
526
1231
  const value = await actionFn();
527
- const result = {
528
- type: action.type,
529
- durationMs: Date.now() - startedAt
530
- };
1232
+ const durationMs = Date.now() - startedAt;
1233
+ const result = { type: action.type, durationMs };
1234
+ if (activeRecordingEvents !== null && action.type !== "record") {
1235
+ activeRecordingEvents.push({
1236
+ type: action.type,
1237
+ params: action.params ?? {},
1238
+ tMs: startedAt - activeRecordingStartMs,
1239
+ durationMs
1240
+ });
1241
+ }
531
1242
  for (const plugin of plugins) {
532
1243
  await plugin.afterAction?.(action, result);
533
1244
  }
534
1245
  return value;
535
1246
  } catch (error) {
1247
+ if (activeRecordingEvents !== null && action.type !== "record") {
1248
+ activeRecordingEvents.push({
1249
+ type: action.type,
1250
+ params: action.params ?? {},
1251
+ tMs: startedAt - activeRecordingStartMs,
1252
+ durationMs: Date.now() - startedAt,
1253
+ error: error instanceof Error ? error.message : String(error)
1254
+ });
1255
+ }
536
1256
  for (const plugin of plugins) {
537
1257
  await plugin.onError?.(action, error);
538
1258
  }
@@ -540,6 +1260,23 @@ async function createHuman(page, options = {}) {
540
1260
  }
541
1261
  }
542
1262
  let lastMousePosition = options.initialMousePosition ?? { x: 0, y: 0 };
1263
+ const mouseCtx = () => ({
1264
+ page,
1265
+ personality,
1266
+ rng,
1267
+ speed,
1268
+ getMousePosition: () => lastMousePosition,
1269
+ setMousePosition: (point) => {
1270
+ lastMousePosition = point;
1271
+ }
1272
+ });
1273
+ const describeMouseTarget = (target) => {
1274
+ if (typeof target === "string") return target;
1275
+ if ("x" in target && "y" in target && typeof target.x === "number") {
1276
+ return `point(${target.x}, ${target.y})`;
1277
+ }
1278
+ return target.toString?.() ?? "locator";
1279
+ };
543
1280
  return {
544
1281
  personality,
545
1282
  speed,
@@ -549,29 +1286,65 @@ async function createHuman(page, options = {}) {
549
1286
  });
550
1287
  },
551
1288
  async click(target) {
552
- const description = typeof target === "string" ? target : target.toString?.() ?? "locator";
1289
+ const description = describeMouseTarget(target);
553
1290
  await performAction({ type: "click", params: { target: description } }, async () => {
554
- await executeClick(target, {
555
- page,
556
- personality,
557
- rng,
558
- speed,
559
- getMousePosition: () => lastMousePosition,
560
- setMousePosition: (point) => {
561
- lastMousePosition = point;
562
- }
563
- });
1291
+ await executeClick(target, mouseCtx());
1292
+ });
1293
+ },
1294
+ async rightClick(target) {
1295
+ const description = describeMouseTarget(target);
1296
+ await performAction({ type: "rightClick", params: { target: description } }, async () => {
1297
+ await executeClick(target, mouseCtx(), { button: "right" });
1298
+ });
1299
+ },
1300
+ async hover(target) {
1301
+ const description = describeMouseTarget(target);
1302
+ await performAction({ type: "hover", params: { target: description } }, async () => {
1303
+ await executeHover(target, mouseCtx());
1304
+ });
1305
+ },
1306
+ async move(target) {
1307
+ const description = describeMouseTarget(target);
1308
+ await performAction({ type: "move", params: { target: description } }, async () => {
1309
+ await executeMove(target, mouseCtx());
1310
+ });
1311
+ },
1312
+ async drag(from, to) {
1313
+ const fromDesc = describeMouseTarget(from);
1314
+ const toDesc = describeMouseTarget(to);
1315
+ await performAction({ type: "drag", params: { from: fromDesc, to: toDesc } }, async () => {
1316
+ await executeDrag(from, to, mouseCtx());
564
1317
  });
565
1318
  },
566
1319
  async type(target, value) {
567
- const description = typeof target === "string" ? target : target.toString?.() ?? "locator";
1320
+ const description = describeMouseTarget(target);
568
1321
  await performAction(
569
1322
  { type: "type", params: { target: description, length: value.length } },
570
1323
  async () => {
1324
+ if (speed !== "instant" && value.length > 0) {
1325
+ await executeClick(target, mouseCtx());
1326
+ }
571
1327
  await executeType(target, value, { page, personality, rng, speed });
572
1328
  }
573
1329
  );
574
1330
  },
1331
+ async paste(target, value) {
1332
+ const description = describeMouseTarget(target);
1333
+ await performAction(
1334
+ { type: "paste", params: { target: description, length: value.length } },
1335
+ async () => {
1336
+ if (speed !== "instant" && value.length > 0) {
1337
+ await executeClick(target, mouseCtx());
1338
+ }
1339
+ await executePaste(target, value, { page, personality, rng, speed });
1340
+ }
1341
+ );
1342
+ },
1343
+ async press(key) {
1344
+ await performAction({ type: "press", params: { key } }, async () => {
1345
+ await executePress(key, { page, personality, rng, speed });
1346
+ });
1347
+ },
575
1348
  async read(target, options2) {
576
1349
  const description = describeReadTarget(target);
577
1350
  return performAction(
@@ -610,6 +1383,51 @@ async function createHuman(page, options = {}) {
610
1383
  },
611
1384
  () => executeScroll(target, { page, personality, rng, speed }, options2)
612
1385
  );
1386
+ },
1387
+ async sleep(ms) {
1388
+ await performAction({ type: "sleep", params: { ms } }, () => sleep$1(ms));
1389
+ },
1390
+ async record(optionsOrFn, maybeFn) {
1391
+ const [recordOptions, fn] = typeof optionsOrFn === "function" ? [{}, optionsOrFn] : [optionsOrFn, maybeFn];
1392
+ if (hasRecorded) {
1393
+ throw new Error(
1394
+ "human.record() can only be called once per session. Create a new browser context (and a new human session) to record a separate clip."
1395
+ );
1396
+ }
1397
+ hasRecorded = true;
1398
+ const captureEnabled = recordOptions.video !== false;
1399
+ const captureQuality = recordOptions.quality ?? "high";
1400
+ let captureSession = null;
1401
+ if (captureEnabled) {
1402
+ const { format, quality, fps } = getCaptureSettingsForQuality(captureQuality);
1403
+ captureSession = await startCapture(page, { format, quality, fps });
1404
+ }
1405
+ const events = [];
1406
+ const windowStartMs = Date.now();
1407
+ activeRecordingEvents = events;
1408
+ activeRecordingStartMs = windowStartMs;
1409
+ let windowEndMs = windowStartMs;
1410
+ try {
1411
+ await performAction({ type: "record", params: {} }, async () => {
1412
+ try {
1413
+ await fn();
1414
+ } finally {
1415
+ windowEndMs = Date.now();
1416
+ }
1417
+ });
1418
+ } catch (error) {
1419
+ if (captureSession) await captureSession.abort();
1420
+ throw error;
1421
+ } finally {
1422
+ activeRecordingEvents = null;
1423
+ }
1424
+ const captureResult = captureSession ? await captureSession.stop() : null;
1425
+ return new Recording(captureResult, windowStartMs, windowEndMs, {
1426
+ personality: personality.name,
1427
+ seed: options.seed === void 0 ? null : String(options.seed),
1428
+ speed,
1429
+ events
1430
+ });
613
1431
  }
614
1432
  };
615
1433
  }
@@ -629,6 +1447,6 @@ function describeReadTarget(target) {
629
1447
  return target.toString?.() ?? "locator";
630
1448
  }
631
1449
 
632
- export { createHuman, installMouseHelper };
1450
+ export { Recording, createHuman, installMouseHelper };
633
1451
  //# sourceMappingURL=index.js.map
634
1452
  //# sourceMappingURL=index.js.map