@humanjs/playwright 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/dist/index.js CHANGED
@@ -1,7 +1,84 @@
1
- import { resolvePersonality, createRng, bezierPath, humanizePath } from '@humanjs/core';
2
- export { applyMicroJitter, applyVelocityProfile, bezierPath, blend, careful, createRng, distracted, fast, humanizePath, 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
12
+
13
+ // src/internal/timing.ts
14
+ function sleep(ms) {
15
+ return ms > 0 ? new Promise((resolve) => setTimeout(resolve, ms)) : Promise.resolve();
16
+ }
17
+ function speedModeFactor(speed) {
18
+ switch (speed) {
19
+ case "fast":
20
+ return 0.5;
21
+ case "instant":
22
+ return 0;
23
+ default:
24
+ return 1;
25
+ }
26
+ }
27
+ function computeDwellTime(meanMs, jitter, personality, speed, rng) {
28
+ if (meanMs <= 0) return 0;
29
+ const jitterMag = meanMs * jitter;
30
+ const offset = rng.nextFloat(-jitterMag, jitterMag);
31
+ return Math.max(0, (meanMs + offset) * personality.speed * speedModeFactor(speed));
32
+ }
33
+
34
+ // src/keyboard/index.ts
35
+ async function executeType(target, value, ctx) {
36
+ const locator = typeof target === "string" ? ctx.page.locator(target) : target;
37
+ if (value.length === 0) {
38
+ return { characters: 0, typos: 0, corrections: 0 };
39
+ }
40
+ if (ctx.speed === "instant") {
41
+ await locator.pressSequentially(value, { delay: 0 });
42
+ return { characters: value.length, typos: 0, corrections: 0 };
43
+ }
44
+ await locator.focus();
45
+ const plan = planTypeKeystrokes(value, ctx.personality.typing, ctx.rng, {
46
+ personalitySpeed: ctx.personality.speed,
47
+ speedFactor: speedModeFactor(ctx.speed)
48
+ });
49
+ let typos = 0;
50
+ let corrections = 0;
51
+ for (const step of plan) {
52
+ if (step.delayBeforeMs > 0) await sleep(step.delayBeforeMs);
53
+ await dispatchKey(ctx.page, step.key);
54
+ if (step.isTypo) typos++;
55
+ if (step.isCorrection) corrections++;
56
+ }
57
+ return { characters: value.length, typos, corrections };
58
+ }
59
+ async function dispatchKey(page, key) {
60
+ if (key.length > 1 || key.charCodeAt(0) < 128) {
61
+ await page.keyboard.press(key);
62
+ } else {
63
+ await page.keyboard.insertText(key);
64
+ }
65
+ }
66
+
67
+ // src/internal/mouse-walk.ts
68
+ async function walkMouseAlongPath(page, path, durationMs) {
69
+ if (path.length === 0) return;
70
+ const stepDelayMs = path.length > 1 && durationMs > 0 ? durationMs / (path.length - 1) : 0;
71
+ for (let i = 0; i < path.length; i++) {
72
+ const point = path[i];
73
+ if (!point) continue;
74
+ await page.mouse.move(point.x, point.y);
75
+ if (i < path.length - 1 && stepDelayMs > 0) {
76
+ await sleep(stepDelayMs);
77
+ }
78
+ }
79
+ }
80
+
81
+ // src/mouse/index.ts
5
82
  async function executeClick(target, ctx) {
6
83
  const locator = typeof target === "string" ? ctx.page.locator(target) : target;
7
84
  if (ctx.speed === "instant") {
@@ -23,7 +100,8 @@ async function executeClick(target, ctx) {
23
100
  curvature: ctx.personality.mouse.curvature
24
101
  });
25
102
  const path = humanizePath(rawPath, ctx.rng);
26
- await walkMouseAlongPath(ctx.page, path, ctx.personality, ctx.rng, ctx.speed);
103
+ const travelMs = computeTravelTime(path, ctx.personality, ctx.speed, ctx.rng);
104
+ await walkMouseAlongPath(ctx.page, path, travelMs);
27
105
  const preClickMs = computeDwellTime(
28
106
  ctx.personality.dwell.preClickMs,
29
107
  ctx.personality.dwell.preClickJitter,
@@ -51,19 +129,6 @@ function pickClickPoint(box, rng) {
51
129
  const y = clamp(cy + rng.nextGaussian(0, box.height / 8), box.y, box.y + box.height);
52
130
  return { x, y };
53
131
  }
54
- async function walkMouseAlongPath(page, path, personality, rng, speed) {
55
- if (path.length === 0) return;
56
- const totalTimeMs = computeTravelTime(path, personality, speed, rng);
57
- const stepDelayMs = path.length > 1 ? totalTimeMs / (path.length - 1) : 0;
58
- for (let i = 0; i < path.length; i++) {
59
- const point = path[i];
60
- if (!point) continue;
61
- await page.mouse.move(point.x, point.y);
62
- if (i < path.length - 1 && stepDelayMs > 0) {
63
- await sleep(stepDelayMs);
64
- }
65
- }
66
- }
67
132
  function computeTravelTime(path, personality, speed, rng) {
68
133
  let distance = 0;
69
134
  for (let i = 1; i < path.length; i++) {
@@ -78,31 +143,779 @@ function computeTravelTime(path, personality, speed, rng) {
78
143
  const total = (baseTime + jitter) * personality.speed * speedModeFactor(speed);
79
144
  return Math.max(0, total);
80
145
  }
81
- function computeDwellTime(meanMs, jitter, personality, speed, rng) {
82
- if (meanMs <= 0) return 0;
83
- const jitterMag = meanMs * jitter;
84
- const offset = rng.nextFloat(-jitterMag, jitterMag);
85
- return Math.max(0, (meanMs + offset) * personality.speed * speedModeFactor(speed));
86
- }
87
- function speedModeFactor(speed) {
88
- switch (speed) {
89
- case "fast":
90
- return 0.5;
91
- case "instant":
92
- return 0;
93
- default:
94
- return 1;
95
- }
96
- }
97
146
  function describeTarget(target) {
98
147
  return typeof target === "string" ? target : target.toString?.() ?? "locator";
99
148
  }
100
149
  function clamp(value, min, max) {
101
150
  return value < min ? min : value > max ? max : value;
102
151
  }
103
- function sleep(ms) {
104
- return new Promise((resolve) => setTimeout(resolve, ms));
152
+ async function executeRead(target, ctx, options = {}) {
153
+ let words = 0;
154
+ let locator;
155
+ if (typeof target === "string") {
156
+ locator = ctx.page.locator(target);
157
+ } else if ("words" in target) {
158
+ words = target.words;
159
+ } else if ("text" in target) {
160
+ words = countWords(target.text);
161
+ } else {
162
+ locator = target;
163
+ }
164
+ let autoDetectedKind;
165
+ if (locator) {
166
+ if (options.scrollIntoView) {
167
+ await locator.scrollIntoViewIfNeeded();
168
+ }
169
+ const text = await locator.innerText().catch(() => "");
170
+ words = countWords(text);
171
+ if (options.kind === void 0) {
172
+ autoDetectedKind = await detectKindFromTag(locator);
173
+ }
174
+ }
175
+ const kind = options.kind ?? autoDetectedKind ?? "prose";
176
+ const durationMs = computeReadingDwellMs(words, ctx.personality.reading, ctx.rng, {
177
+ kind,
178
+ wpmMultiplier: options.wpmMultiplier,
179
+ personalitySpeed: ctx.personality.speed,
180
+ speedFactor: speedModeFactor(ctx.speed)
181
+ });
182
+ const withMotion = options.withMotion ?? true;
183
+ if (withMotion && locator && durationMs > 0) {
184
+ const box = await locator.boundingBox().catch(() => null);
185
+ if (box) {
186
+ const lineRects = await getLineRects(locator).catch(() => []);
187
+ const path = planReadingScan(box, ctx.rng, {
188
+ start: ctx.getMousePosition(),
189
+ lineRects: lineRects.length > 0 ? lineRects : void 0
190
+ });
191
+ await walkMouseAlongPath(ctx.page, path, durationMs);
192
+ const final = path[path.length - 1];
193
+ if (final) ctx.setMousePosition(final);
194
+ return { words, durationMs, kind };
195
+ }
196
+ }
197
+ if (durationMs > 0) await sleep(durationMs);
198
+ return { words, durationMs, kind };
199
+ }
200
+ async function getLineRects(locator) {
201
+ const result = await locator.evaluate((el) => {
202
+ const walker = el.ownerDocument.createTreeWalker(el, 4);
203
+ const rects = [];
204
+ let node = walker.nextNode();
205
+ while (node) {
206
+ const text = node.textContent ?? "";
207
+ if (text.trim().length > 0) {
208
+ const range = el.ownerDocument.createRange();
209
+ range.selectNodeContents(node);
210
+ for (const r of Array.from(range.getClientRects())) {
211
+ if (r.width > 0 && r.height > 0) {
212
+ rects.push({ x: r.x, y: r.y, width: r.width, height: r.height });
213
+ }
214
+ }
215
+ }
216
+ node = walker.nextNode();
217
+ }
218
+ rects.sort((a, b) => a.y - b.y || a.x - b.x);
219
+ const merged = [];
220
+ for (const r of rects) {
221
+ const last = merged[merged.length - 1];
222
+ if (last && Math.abs(last.y - r.y) < 1 && r.x - (last.x + last.width) < 6) {
223
+ const right = Math.max(last.x + last.width, r.x + r.width);
224
+ const bottom = Math.max(last.y + last.height, r.y + r.height);
225
+ last.width = right - last.x;
226
+ last.height = bottom - last.y;
227
+ } else {
228
+ merged.push({ ...r });
229
+ }
230
+ }
231
+ return merged;
232
+ });
233
+ if (!Array.isArray(result)) return [];
234
+ return result.filter(
235
+ (r) => r != null && typeof r === "object" && typeof r.x === "number" && typeof r.y === "number" && typeof r.width === "number" && typeof r.height === "number"
236
+ );
105
237
  }
238
+ async function detectKindFromTag(locator) {
239
+ const tag = await locator.evaluate((el) => el.tagName?.toLowerCase() ?? "").catch(() => "");
240
+ if (tag === "pre" || tag === "code") return "code";
241
+ return void 0;
242
+ }
243
+ var pendingFrameCleanups = /* @__PURE__ */ new Set();
244
+ var exitHandlerInstalled = false;
245
+ function ensureExitHandler() {
246
+ if (exitHandlerInstalled) return;
247
+ exitHandlerInstalled = true;
248
+ process.on("exit", () => {
249
+ for (const dir of pendingFrameCleanups) {
250
+ try {
251
+ rmSync(dir, { recursive: true, force: true });
252
+ } catch {
253
+ }
254
+ }
255
+ pendingFrameCleanups.clear();
256
+ });
257
+ }
258
+ var FFMPEG_PATH = ffmpegStatic;
259
+ var QUALITY_PRESETS = {
260
+ fast: {
261
+ captureFormat: "jpeg",
262
+ captureJpegQuality: 85,
263
+ captureFps: 24,
264
+ crf: 23,
265
+ preset: "fast"
266
+ },
267
+ standard: {
268
+ captureFormat: "jpeg",
269
+ captureJpegQuality: 90,
270
+ captureFps: 30,
271
+ crf: 20,
272
+ preset: "fast"
273
+ },
274
+ high: {
275
+ captureFormat: "jpeg",
276
+ captureJpegQuality: 95,
277
+ captureFps: 30,
278
+ crf: 18,
279
+ preset: "slow",
280
+ // 'animation' suits screen content (large solid regions, sharp edges)
281
+ // better than 'film' which is tuned for live-action grain.
282
+ tune: "animation"
283
+ },
284
+ lossless: {
285
+ // PNG capture for perceptually lossless source frames. Temp files are
286
+ // 10-20× larger than JPEG; output mp4 still benefits from the extra
287
+ // headroom (no JPEG artifacts to preserve).
288
+ captureFormat: "png",
289
+ captureJpegQuality: 100,
290
+ captureFps: 30,
291
+ crf: 12,
292
+ preset: "veryslow",
293
+ tune: "animation"
294
+ }
295
+ };
296
+ function getCaptureSettingsForQuality(quality) {
297
+ const preset = QUALITY_PRESETS[quality];
298
+ return {
299
+ format: preset.captureFormat,
300
+ quality: preset.captureJpegQuality,
301
+ fps: preset.captureFps
302
+ };
303
+ }
304
+ var Recording = class {
305
+ #capture;
306
+ #windowStartMs;
307
+ #windowEndMs;
308
+ #timelineSource;
309
+ // Frames live on disk until `dispose()` is called. Exporters
310
+ // (`toVideo`, `toGif`) are repeatable and interleavable — they read the
311
+ // same frame source, they don't consume it.
312
+ #disposed = false;
313
+ constructor(capture, windowStartMs, windowEndMs, timelineSource) {
314
+ this.#capture = capture;
315
+ this.#windowStartMs = windowStartMs;
316
+ this.#windowEndMs = windowEndMs;
317
+ this.#timelineSource = timelineSource;
318
+ if (capture !== null) {
319
+ pendingFrameCleanups.add(capture.dir);
320
+ ensureExitHandler();
321
+ }
322
+ }
323
+ /** Wall-clock duration of the recorded window. */
324
+ get durationMs() {
325
+ return this.#windowEndMs - this.#windowStartMs;
326
+ }
327
+ /** True if frames were captured during this recording. */
328
+ get hasVideo() {
329
+ return this.#capture !== null;
330
+ }
331
+ /**
332
+ * The structured action timeline of this recording — same data that
333
+ * `toTimeline()` writes to disk.
334
+ */
335
+ get timeline() {
336
+ return {
337
+ version: 1,
338
+ personality: this.#timelineSource.personality,
339
+ seed: this.#timelineSource.seed,
340
+ speed: this.#timelineSource.speed,
341
+ durationMs: this.durationMs,
342
+ events: this.#timelineSource.events
343
+ };
344
+ }
345
+ /**
346
+ * Assembles the captured frames into a video at `outputPath`. The output
347
+ * format is inferred from the extension — `.mp4` (H.264, re-encoded
348
+ * with the configured quality) or `.webm` (VP9).
349
+ *
350
+ * Repeatable and interleavable with `toGif()` — the frame source is read,
351
+ * not consumed. Frames live until you call `dispose()` (or `await using`
352
+ * goes out of scope, or the process exits and the OS reaps `tmpdir`).
353
+ *
354
+ * @returns the resolved output path.
355
+ */
356
+ async toVideo(outputPath, options = {}) {
357
+ if (this.#disposed) {
358
+ throw new Error("Recording.toVideo() called after dispose() \u2014 the source frames are gone.");
359
+ }
360
+ if (this.#capture === null) {
361
+ throw new Error(
362
+ "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."
363
+ );
364
+ }
365
+ const preset = QUALITY_PRESETS[options.quality ?? "high"];
366
+ const crf = options.crf ?? preset.crf;
367
+ const ffmpegPreset = options.preset ?? preset.preset;
368
+ const tune = options.tune ?? preset.tune;
369
+ const { dir, frames, startedAtMs, stoppedAtMs } = this.#capture;
370
+ if (frames.length === 0) {
371
+ throw new Error(
372
+ "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."
373
+ );
374
+ }
375
+ await mkdir(dirname(outputPath), { recursive: true });
376
+ const ext = extname(outputPath).toLowerCase();
377
+ if (ext !== ".mp4" && ext !== ".webm") {
378
+ throw new Error(`Unsupported output extension: ${ext || "(none)"}. Use .mp4 or .webm.`);
379
+ }
380
+ const concatPath = `${dir}/concat.txt`;
381
+ const concatBody = buildConcatFile(frames, stoppedAtMs - startedAtMs);
382
+ await writeFile(concatPath, concatBody, "utf8");
383
+ const args = ["-y", "-f", "concat", "-safe", "0", "-i", concatPath, "-vsync", "vfr"];
384
+ if (ext === ".mp4") {
385
+ args.push(
386
+ "-c:v",
387
+ "libx264",
388
+ "-pix_fmt",
389
+ "yuv420p",
390
+ "-crf",
391
+ String(crf),
392
+ "-preset",
393
+ ffmpegPreset
394
+ );
395
+ if (tune) args.push("-tune", tune);
396
+ args.push("-movflags", "+faststart");
397
+ } else {
398
+ args.push(
399
+ "-c:v",
400
+ "libvpx-vp9",
401
+ "-pix_fmt",
402
+ "yuv420p",
403
+ "-crf",
404
+ String(crf),
405
+ "-b:v",
406
+ "0",
407
+ "-deadline",
408
+ ffmpegPreset === "fast" || ffmpegPreset === "veryfast" ? "realtime" : "good"
409
+ );
410
+ }
411
+ args.push(outputPath);
412
+ await runFfmpeg(args);
413
+ return outputPath;
414
+ }
415
+ /**
416
+ * Assembles the captured frames into an animated GIF at `outputPath`.
417
+ * Optimized for embedding in READMEs, PRs, Slack, and docs — uses a
418
+ * per-recording palette (`palettegen` + `paletteuse`) with Bayer dithering
419
+ * so gradients stay smooth without exploding the file size.
420
+ *
421
+ * Repeatable and interleavable with `toVideo()` — call them in any order,
422
+ * any number of times. Frames live until you call `dispose()`.
423
+ *
424
+ * @returns the resolved output path.
425
+ */
426
+ async toGif(outputPath, options = {}) {
427
+ if (this.#disposed) {
428
+ throw new Error("Recording.toGif() called after dispose() \u2014 the source frames are gone.");
429
+ }
430
+ if (this.#capture === null) {
431
+ throw new Error(
432
+ "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."
433
+ );
434
+ }
435
+ const fps = options.fps ?? 15;
436
+ const width = options.width;
437
+ const { dir, frames, startedAtMs, stoppedAtMs } = this.#capture;
438
+ if (frames.length === 0) {
439
+ throw new Error(
440
+ "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."
441
+ );
442
+ }
443
+ await mkdir(dirname(outputPath), { recursive: true });
444
+ const ext = extname(outputPath).toLowerCase();
445
+ if (ext !== ".gif") {
446
+ throw new Error(`Unsupported output extension: ${ext || "(none)"}. Use .gif.`);
447
+ }
448
+ const concatPath = `${dir}/concat.txt`;
449
+ const concatBody = buildConcatFile(frames, stoppedAtMs - startedAtMs);
450
+ await writeFile(concatPath, concatBody, "utf8");
451
+ const filterSteps = [`fps=${fps}`];
452
+ if (width !== void 0) {
453
+ filterSteps.push(`scale=${width}:-1:flags=lanczos`);
454
+ }
455
+ const preFilter = filterSteps.join(",");
456
+ const filterComplex = `${preFilter},split [a][b]; [a] palettegen=stats_mode=diff [p]; [b][p] paletteuse=dither=bayer:bayer_scale=5`;
457
+ const args = [
458
+ "-y",
459
+ "-f",
460
+ "concat",
461
+ "-safe",
462
+ "0",
463
+ "-i",
464
+ concatPath,
465
+ "-filter_complex",
466
+ filterComplex,
467
+ "-loop",
468
+ "0",
469
+ outputPath
470
+ ];
471
+ await runFfmpeg(args);
472
+ return outputPath;
473
+ }
474
+ /**
475
+ * Writes the structured action timeline to `outputPath` as JSON.
476
+ * Independent of `toVideo()` / `toGif()` — call before, after, in between,
477
+ * or instead. Safe to call multiple times. Unaffected by `dispose()`
478
+ * (the timeline lives in memory, not in the captured-frames temp dir).
479
+ *
480
+ * @returns the resolved output path.
481
+ */
482
+ async toTimeline(outputPath) {
483
+ await mkdir(dirname(outputPath), { recursive: true });
484
+ await writeFile(outputPath, `${JSON.stringify(this.timeline, null, 2)}
485
+ `, "utf8");
486
+ return outputPath;
487
+ }
488
+ /**
489
+ * Releases the captured-frames temp directory. After this call, `toVideo()`
490
+ * and `toGif()` throw — but `toTimeline()` and the in-memory `timeline`
491
+ * still work because those don't depend on the frames.
492
+ *
493
+ * **Optional.** A process-exit handler also sweeps any un-disposed frame
494
+ * dirs, so casual scripts can skip this entirely. Call it explicitly when
495
+ * you want to release frames proactively (long-running services, batch
496
+ * jobs, or anywhere you want predictable disk usage).
497
+ *
498
+ * Idempotent. Safe to call on a Recording that never had a capture
499
+ * (timeline-only mode) — no-op there.
500
+ *
501
+ * Also wired to `Symbol.asyncDispose`, so the explicit-resource-management
502
+ * `await using` syntax (TypeScript ≥ 5.2 / Node ≥ 20.4) works:
503
+ *
504
+ * ```ts
505
+ * await using rec = await human.record(fn);
506
+ * await rec.toVideo('demo.mp4');
507
+ * await rec.toGif('demo.gif');
508
+ * // frames cleaned up automatically when `rec` goes out of scope
509
+ * ```
510
+ */
511
+ async dispose() {
512
+ if (this.#disposed) return;
513
+ if (this.#capture !== null) {
514
+ await this.#capture.cleanup();
515
+ pendingFrameCleanups.delete(this.#capture.dir);
516
+ }
517
+ this.#disposed = true;
518
+ }
519
+ async [Symbol.asyncDispose]() {
520
+ await this.dispose();
521
+ }
522
+ };
523
+ function buildConcatFile(frames, totalMs) {
524
+ const lines = [];
525
+ for (let i = 0; i < frames.length; i++) {
526
+ const frame = frames[i];
527
+ const next = frames[i + 1];
528
+ const nextTMs = next ? next.tMs : totalMs;
529
+ const durationS = Math.max(1e-3, (nextTMs - frame.tMs) / 1e3);
530
+ lines.push(`file '${frame.path.replaceAll("'", "'\\''")}'`);
531
+ lines.push(`duration ${durationS.toFixed(6)}`);
532
+ }
533
+ const last = frames[frames.length - 1];
534
+ if (last) {
535
+ lines.push(`file '${last.path.replaceAll("'", "'\\''")}'`);
536
+ }
537
+ return `${lines.join("\n")}
538
+ `;
539
+ }
540
+ function runFfmpeg(args) {
541
+ if (!FFMPEG_PATH) {
542
+ return Promise.reject(
543
+ new Error(
544
+ "ffmpeg-static did not bundle a binary for this platform. Install system ffmpeg and set FFMPEG_PATH, or run on a supported platform."
545
+ )
546
+ );
547
+ }
548
+ return new Promise((resolve, reject) => {
549
+ const proc = spawn(FFMPEG_PATH, [...args]);
550
+ let stderr = "";
551
+ proc.stderr?.on("data", (chunk) => {
552
+ stderr += chunk.toString();
553
+ });
554
+ proc.on("error", reject);
555
+ proc.on("close", (code) => {
556
+ if (code === 0) resolve();
557
+ else reject(new Error(`ffmpeg exited with code ${code}
558
+ ${stderr.trim()}`));
559
+ });
560
+ });
561
+ }
562
+ async function startCapture(page, options = {}) {
563
+ const format = options.format ?? "jpeg";
564
+ const quality = options.quality ?? 95;
565
+ const fps = Math.max(1, Math.min(60, options.fps ?? 30));
566
+ const intervalMs = 1e3 / fps;
567
+ const dir = await mkdtemp(join(tmpdir(), "humanjs-capture-"));
568
+ const frames = [];
569
+ const ext = format === "png" ? "png" : "jpg";
570
+ let stopped = false;
571
+ let frameIndex = 0;
572
+ const writes = [];
573
+ const startedAtMs = Date.now();
574
+ const captureLoop = async () => {
575
+ while (!stopped) {
576
+ const loopStart = Date.now();
577
+ try {
578
+ const buf = await page.screenshot({
579
+ type: format,
580
+ quality: format === "jpeg" ? quality : void 0
581
+ });
582
+ if (stopped) return;
583
+ const idx = frameIndex++;
584
+ const path = join(dir, `frame_${String(idx).padStart(6, "0")}.${ext}`);
585
+ const tMs = loopStart - startedAtMs;
586
+ writes.push(
587
+ writeFile(path, buf).then(
588
+ () => {
589
+ frames.push({ path, tMs });
590
+ },
591
+ (err) => {
592
+ console.warn(`humanjs capture: write failed for frame ${idx}:`, err);
593
+ }
594
+ )
595
+ );
596
+ } catch (err) {
597
+ if (stopped) return;
598
+ console.warn("humanjs capture: screenshot failed, stopping loop:", err);
599
+ stopped = true;
600
+ return;
601
+ }
602
+ const elapsed = Date.now() - loopStart;
603
+ const wait = intervalMs - elapsed;
604
+ if (wait > 0) await sleep$1(wait);
605
+ }
606
+ };
607
+ const loopPromise = captureLoop();
608
+ const finish = async () => {
609
+ stopped = true;
610
+ await loopPromise;
611
+ await Promise.allSettled(writes);
612
+ };
613
+ return {
614
+ async stop() {
615
+ await finish();
616
+ const stoppedAtMs = Date.now();
617
+ return {
618
+ dir,
619
+ frames: [...frames].sort((a, b) => a.tMs - b.tMs),
620
+ startedAtMs,
621
+ stoppedAtMs,
622
+ format,
623
+ fps,
624
+ cleanup: () => rm(dir, { recursive: true, force: true }).then(() => void 0)
625
+ };
626
+ },
627
+ async abort() {
628
+ await finish();
629
+ await rm(dir, { recursive: true, force: true }).catch(() => void 0);
630
+ }
631
+ };
632
+ }
633
+ var RESERVED_TARGETS = /* @__PURE__ */ new Set(["natural", "end", "top"]);
634
+ async function executeScroll(target, ctx, options = {}) {
635
+ const { page, personality, rng, speed } = ctx;
636
+ const speedFactor = speedModeFactor(speed);
637
+ const axis = options.axis ?? "y";
638
+ const container = resolveWithin(options.within, ctx);
639
+ const geom = container ? await readContainerGeometry(container, axis) : await readWindowGeometry(page, axis);
640
+ if (!geom) {
641
+ return { from: 0, to: 0, distance: 0, durationMs: 0 };
642
+ }
643
+ const from = geom.current;
644
+ const targetPos = await resolveTarget(target, ctx, geom, container, axis, options.block);
645
+ const to = clamp2(targetPos, 0, Math.max(0, geom.total - geom.viewport));
646
+ const distance = to - from;
647
+ if (distance === 0) {
648
+ return { from, to, distance: 0, durationMs: 0 };
649
+ }
650
+ if (speed === "instant") {
651
+ if (container) {
652
+ await container.evaluate(
653
+ (el, args) => {
654
+ const a = args;
655
+ if (a.axis === "x") el.scrollTo(a.pos, el.scrollTop);
656
+ else el.scrollTo(el.scrollLeft, a.pos);
657
+ },
658
+ { axis, pos: to }
659
+ );
660
+ } else {
661
+ await page.evaluate(
662
+ (args) => {
663
+ if (args.axis === "x") window.scrollTo(args.pos, window.scrollY);
664
+ else window.scrollTo(window.scrollX, args.pos);
665
+ },
666
+ { axis, pos: to }
667
+ );
668
+ }
669
+ return { from, to, distance, durationMs: 0 };
670
+ }
671
+ const segments = planScroll(from, to, personality.scroll, rng, {
672
+ forceOvershoot: options.overshoot,
673
+ withPauses: options.withPauses,
674
+ personalitySpeed: personality.speed,
675
+ speedFactor
676
+ });
677
+ if (container && geom.hover) {
678
+ await page.mouse.move(geom.hover.x, geom.hover.y);
679
+ }
680
+ const startedAt = Date.now();
681
+ await walkSegments(page, segments, axis, container);
682
+ const durationMs = Date.now() - startedAt;
683
+ return { from, to, distance, durationMs };
684
+ }
685
+ function resolveWithin(within, ctx) {
686
+ if (!within) return null;
687
+ return typeof within === "string" ? ctx.page.locator(within) : within;
688
+ }
689
+ async function readWindowGeometry(page, axis) {
690
+ const g = await page.evaluate((a) => {
691
+ if (a === "x") {
692
+ return {
693
+ current: window.scrollX,
694
+ viewport: window.innerWidth,
695
+ total: Math.max(document.documentElement.scrollWidth, document.body?.scrollWidth ?? 0)
696
+ };
697
+ }
698
+ return {
699
+ current: window.scrollY,
700
+ viewport: window.innerHeight,
701
+ total: Math.max(document.documentElement.scrollHeight, document.body?.scrollHeight ?? 0)
702
+ };
703
+ }, axis);
704
+ return { current: g.current, viewport: g.viewport, total: g.total };
705
+ }
706
+ async function readContainerGeometry(container, axis) {
707
+ return container.evaluate((el, a) => {
708
+ const rect = el.getBoundingClientRect();
709
+ const isX = a === "x";
710
+ return {
711
+ current: isX ? el.scrollLeft : el.scrollTop,
712
+ viewport: isX ? el.clientWidth : el.clientHeight,
713
+ total: isX ? el.scrollWidth : el.scrollHeight,
714
+ hover: {
715
+ x: rect.left + rect.width / 2,
716
+ y: rect.top + rect.height / 2
717
+ }
718
+ };
719
+ }, axis).catch(() => null);
720
+ }
721
+ async function resolveTarget(target, ctx, geom, container, axis, block = "start") {
722
+ if (target === void 0 || target === "natural") return geom.current + geom.viewport;
723
+ if (target === "end") return geom.total;
724
+ if (target === "top") return 0;
725
+ if (typeof target === "object" && "by" in target) return geom.current + target.by;
726
+ if (typeof target === "object" && "to" in target) return target.to;
727
+ const elementLocator = typeof target === "string" && !RESERVED_TARGETS.has(target) ? ctx.page.locator(target) : typeof target === "string" ? null : target;
728
+ if (!elementLocator) return geom.current + geom.viewport;
729
+ return container ? resolveElementWithinContainer(elementLocator, container, geom, axis, block) : resolveElementInWindow(elementLocator, geom, axis, block);
730
+ }
731
+ async function resolveElementInWindow(elementLocator, geom, axis, block) {
732
+ const rect = await elementLocator.boundingBox().catch(() => null);
733
+ if (!rect) return geom.current;
734
+ const relStart = axis === "x" ? rect.x : rect.y;
735
+ const length = axis === "x" ? rect.width : rect.height;
736
+ const absoluteStart = geom.current + relStart;
737
+ const absoluteEnd = absoluteStart + length;
738
+ if (block === "start") return absoluteStart;
739
+ if (block === "end") return absoluteEnd - geom.viewport;
740
+ if (block === "nearest") {
741
+ if (relStart >= 0 && relStart + length <= geom.viewport) return geom.current;
742
+ if (relStart < 0) return absoluteStart;
743
+ return absoluteEnd - geom.viewport;
744
+ }
745
+ return absoluteStart - (geom.viewport - length) / 2;
746
+ }
747
+ async function resolveElementWithinContainer(elementLocator, container, geom, axis, block) {
748
+ const rects = await container.evaluate(
749
+ (containerEl, args) => {
750
+ const elementEl = args.sel ? document.querySelector(args.sel) : null;
751
+ const targetEl = elementEl ?? containerEl.querySelector(":scope > *");
752
+ if (!targetEl) return null;
753
+ const cRect = containerEl.getBoundingClientRect();
754
+ const eRect = targetEl.getBoundingClientRect();
755
+ return args.axis === "x" ? { relStart: eRect.left - cRect.left, length: eRect.width } : { relStart: eRect.top - cRect.top, length: eRect.height };
756
+ },
757
+ { sel: await locatorSelector(elementLocator), axis }
758
+ ).catch(() => null);
759
+ if (!rects) return geom.current;
760
+ const offsetStart = rects.relStart + geom.current;
761
+ const offsetEnd = offsetStart + rects.length;
762
+ if (block === "start") return offsetStart;
763
+ if (block === "end") return offsetEnd - geom.viewport;
764
+ if (block === "nearest") {
765
+ if (rects.relStart >= 0 && rects.relStart + rects.length <= geom.viewport) {
766
+ return geom.current;
767
+ }
768
+ if (rects.relStart < 0) return offsetStart;
769
+ return offsetEnd - geom.viewport;
770
+ }
771
+ return offsetStart - (geom.viewport - rects.length) / 2;
772
+ }
773
+ async function locatorSelector(locator) {
774
+ const s = locator.toString?.();
775
+ if (typeof s !== "string") return null;
776
+ const match = /locator\(['"](.+?)['"]/.exec(s);
777
+ if (!match) return null;
778
+ const raw = match[1] ?? "";
779
+ const eq = raw.indexOf("=");
780
+ return eq > 0 && /^[a-z]+$/.test(raw.slice(0, eq)) ? raw.slice(eq + 1) : raw;
781
+ }
782
+ async function walkSegments(page, segments, axis, container) {
783
+ for (const segment of segments) {
784
+ if (segment.delayBeforeMs > 0) await sleep(segment.delayBeforeMs);
785
+ if (segment.delta === 0) continue;
786
+ if (container) {
787
+ await container.evaluate(
788
+ (el, args) => {
789
+ const a = args;
790
+ if (a.axis === "x") el.scrollLeft += a.delta;
791
+ else el.scrollTop += a.delta;
792
+ },
793
+ { axis, delta: segment.delta }
794
+ );
795
+ } else if (axis === "x") {
796
+ await page.mouse.wheel(segment.delta, 0);
797
+ } else {
798
+ await page.mouse.wheel(0, segment.delta);
799
+ }
800
+ }
801
+ }
802
+ function clamp2(value, min, max) {
803
+ return value < min ? min : value > max ? max : value;
804
+ }
805
+
806
+ // src/mouse-helper/index.ts
807
+ var CURSOR_PATH = "M 0 0 L 16 6 L 8 9.5 L 5 19 Z";
808
+ var INSTALLED_FLAG = /* @__PURE__ */ Symbol.for("@humanjs/playwright:mouse-helper:installed");
809
+ async function installMouseHelper(target, options = {}) {
810
+ const tagged = target;
811
+ if (tagged[INSTALLED_FLAG]) return;
812
+ tagged[INSTALLED_FLAG] = true;
813
+ const config = {
814
+ color: options.color ?? "#f5a55c",
815
+ stroke: "#020203",
816
+ size: options.size ?? 22,
817
+ showClicks: options.showClicks ?? true,
818
+ haloOpacity: options.haloOpacity ?? 0.18,
819
+ path: CURSOR_PATH
820
+ };
821
+ await target.addInitScript(installScript, config);
822
+ const attachPageHooks = (page) => {
823
+ page.on("domcontentloaded", () => {
824
+ page.evaluate(installScript, config).catch(() => void 0);
825
+ });
826
+ };
827
+ const pages = "pages" in target ? target.pages() : [target];
828
+ for (const page of pages) attachPageHooks(page);
829
+ if ("on" in target && "newPage" in target) {
830
+ target.on("page", attachPageHooks);
831
+ }
832
+ await Promise.all(
833
+ pages.map((page) => page.evaluate(installScript, config).catch(() => void 0))
834
+ );
835
+ }
836
+ function installScript(config) {
837
+ if (document.querySelector("[data-humanjs-cursor]")) return;
838
+ const attach = () => {
839
+ const cursor = document.createElement("div");
840
+ cursor.setAttribute("aria-hidden", "true");
841
+ cursor.setAttribute("data-humanjs-cursor", "true");
842
+ cursor.style.cssText = [
843
+ "position: fixed",
844
+ "left: 0",
845
+ "top: 0",
846
+ `width: ${config.size}px`,
847
+ `height: ${config.size + 4}px`,
848
+ "pointer-events: none",
849
+ "z-index: 2147483647",
850
+ // Start visible at (0, 0) so the cursor is on screen from the moment
851
+ // the page loads — without this the helper looks like nothing happened
852
+ // until the first mousemove arrives.
853
+ "opacity: 1",
854
+ "transform: translate(0px, 0px)",
855
+ // CSS interpolates between successive `mousemove` updates so the
856
+ // cursor reads as continuous motion instead of discrete hops. Slightly
857
+ // longer than the path-walker's typical step interval (~30–80ms) so
858
+ // each tween is still settling when the next move lands → no pauses.
859
+ "transition: transform 110ms ease-out, opacity 0.18s ease-out",
860
+ "will-change: transform"
861
+ ].join("; ");
862
+ const haloRadius = Math.round(config.size * 0.6);
863
+ cursor.innerHTML = `
864
+ <svg width="${config.size}" height="${config.size + 4}" viewBox="0 0 22 24" style="overflow: visible;">
865
+ <circle cx="0" cy="0" r="${haloRadius}" fill="${config.color}" opacity="${config.haloOpacity}" />
866
+ <path d="${config.path}" fill="${config.color}" stroke="${config.stroke}" stroke-width="0.7" stroke-linejoin="round" />
867
+ </svg>
868
+ `;
869
+ document.body.appendChild(cursor);
870
+ let lastX = 0;
871
+ let lastY = 0;
872
+ const onMove = (e) => {
873
+ lastX = e.clientX;
874
+ lastY = e.clientY;
875
+ cursor.style.transform = `translate(${lastX}px, ${lastY}px)`;
876
+ cursor.style.opacity = "1";
877
+ };
878
+ window.addEventListener("mousemove", onMove, { capture: true, passive: true });
879
+ document.addEventListener("mousemove", onMove, { capture: true, passive: true });
880
+ document.addEventListener(
881
+ "mouseleave",
882
+ () => {
883
+ cursor.style.opacity = "0";
884
+ },
885
+ { capture: true, passive: true }
886
+ );
887
+ if (config.showClicks) {
888
+ const styleEl = document.createElement("style");
889
+ styleEl.textContent = "@keyframes humanjs-ripple { 0% { transform: translate(-50%, -50%) scale(0.4); opacity: 0.9; } 100% { transform: translate(-50%, -50%) scale(2); opacity: 0; } }";
890
+ document.head.appendChild(styleEl);
891
+ window.addEventListener(
892
+ "mousedown",
893
+ () => {
894
+ const ripple = document.createElement("div");
895
+ ripple.style.cssText = [
896
+ "position: fixed",
897
+ `left: ${lastX}px`,
898
+ `top: ${lastY}px`,
899
+ "width: 28px",
900
+ "height: 28px",
901
+ "border-radius: 50%",
902
+ `border: 1.5px solid ${config.color}`,
903
+ "pointer-events: none",
904
+ "z-index: 2147483646",
905
+ "animation: humanjs-ripple 0.45s ease-out forwards"
906
+ ].join("; ");
907
+ document.body.appendChild(ripple);
908
+ window.setTimeout(() => ripple.remove(), 500);
909
+ },
910
+ { capture: true, passive: true }
911
+ );
912
+ }
913
+ };
914
+ if (document.body) attach();
915
+ else document.addEventListener("DOMContentLoaded", attach, { once: true });
916
+ }
917
+
918
+ // src/index.ts
106
919
  async function createHuman(page, options = {}) {
107
920
  const personality = resolvePersonality(options.personality ?? "careful");
108
921
  const rng = createRng(options.seed);
@@ -112,6 +925,9 @@ async function createHuman(page, options = {}) {
112
925
  for (const plugin of plugins) {
113
926
  await plugin.install?.(context);
114
927
  }
928
+ let hasRecorded = false;
929
+ let activeRecordingEvents = null;
930
+ let activeRecordingStartMs = 0;
115
931
  async function performAction(action, actionFn) {
116
932
  for (const plugin of plugins) {
117
933
  await plugin.beforeAction?.(action);
@@ -119,15 +935,30 @@ async function createHuman(page, options = {}) {
119
935
  const startedAt = Date.now();
120
936
  try {
121
937
  const value = await actionFn();
122
- const result = {
123
- type: action.type,
124
- durationMs: Date.now() - startedAt
125
- };
938
+ const durationMs = Date.now() - startedAt;
939
+ const result = { type: action.type, durationMs };
940
+ if (activeRecordingEvents !== null && action.type !== "record") {
941
+ activeRecordingEvents.push({
942
+ type: action.type,
943
+ params: action.params ?? {},
944
+ tMs: startedAt - activeRecordingStartMs,
945
+ durationMs
946
+ });
947
+ }
126
948
  for (const plugin of plugins) {
127
949
  await plugin.afterAction?.(action, result);
128
950
  }
129
951
  return value;
130
952
  } catch (error) {
953
+ if (activeRecordingEvents !== null && action.type !== "record") {
954
+ activeRecordingEvents.push({
955
+ type: action.type,
956
+ params: action.params ?? {},
957
+ tMs: startedAt - activeRecordingStartMs,
958
+ durationMs: Date.now() - startedAt,
959
+ error: error instanceof Error ? error.message : String(error)
960
+ });
961
+ }
131
962
  for (const plugin of plugins) {
132
963
  await plugin.onError?.(action, error);
133
964
  }
@@ -157,10 +988,118 @@ async function createHuman(page, options = {}) {
157
988
  }
158
989
  });
159
990
  });
991
+ },
992
+ async type(target, value) {
993
+ const description = typeof target === "string" ? target : target.toString?.() ?? "locator";
994
+ await performAction(
995
+ { type: "type", params: { target: description, length: value.length } },
996
+ async () => {
997
+ await executeType(target, value, { page, personality, rng, speed });
998
+ }
999
+ );
1000
+ },
1001
+ async read(target, options2) {
1002
+ const description = describeReadTarget(target);
1003
+ return performAction(
1004
+ {
1005
+ type: "read",
1006
+ params: {
1007
+ target: description,
1008
+ kind: options2?.kind
1009
+ }
1010
+ },
1011
+ () => executeRead(
1012
+ target,
1013
+ {
1014
+ page,
1015
+ personality,
1016
+ rng,
1017
+ speed,
1018
+ // Read shares the session's tracked cursor position so an eye
1019
+ // scan starts from where the last click left off, and the next
1020
+ // click starts from where the scan ended.
1021
+ getMousePosition: () => lastMousePosition,
1022
+ setMousePosition: (point) => {
1023
+ lastMousePosition = point;
1024
+ }
1025
+ },
1026
+ options2
1027
+ )
1028
+ );
1029
+ },
1030
+ async scroll(target, options2) {
1031
+ const description = describeScrollTarget(target);
1032
+ return performAction(
1033
+ {
1034
+ type: "scroll",
1035
+ params: { target: description }
1036
+ },
1037
+ () => executeScroll(target, { page, personality, rng, speed }, options2)
1038
+ );
1039
+ },
1040
+ async sleep(ms) {
1041
+ await performAction({ type: "sleep", params: { ms } }, () => sleep$1(ms));
1042
+ },
1043
+ async record(optionsOrFn, maybeFn) {
1044
+ const [recordOptions, fn] = typeof optionsOrFn === "function" ? [{}, optionsOrFn] : [optionsOrFn, maybeFn];
1045
+ if (hasRecorded) {
1046
+ throw new Error(
1047
+ "human.record() can only be called once per session. Create a new browser context (and a new human session) to record a separate clip."
1048
+ );
1049
+ }
1050
+ hasRecorded = true;
1051
+ const captureEnabled = recordOptions.video !== false;
1052
+ const captureQuality = recordOptions.quality ?? "high";
1053
+ let captureSession = null;
1054
+ if (captureEnabled) {
1055
+ const { format, quality, fps } = getCaptureSettingsForQuality(captureQuality);
1056
+ captureSession = await startCapture(page, { format, quality, fps });
1057
+ }
1058
+ const events = [];
1059
+ const windowStartMs = Date.now();
1060
+ activeRecordingEvents = events;
1061
+ activeRecordingStartMs = windowStartMs;
1062
+ let windowEndMs = windowStartMs;
1063
+ try {
1064
+ await performAction({ type: "record", params: {} }, async () => {
1065
+ try {
1066
+ await fn();
1067
+ } finally {
1068
+ windowEndMs = Date.now();
1069
+ }
1070
+ });
1071
+ } catch (error) {
1072
+ if (captureSession) await captureSession.abort();
1073
+ throw error;
1074
+ } finally {
1075
+ activeRecordingEvents = null;
1076
+ }
1077
+ const captureResult = captureSession ? await captureSession.stop() : null;
1078
+ return new Recording(captureResult, windowStartMs, windowEndMs, {
1079
+ personality: personality.name,
1080
+ seed: options.seed === void 0 ? null : String(options.seed),
1081
+ speed,
1082
+ events
1083
+ });
160
1084
  }
161
1085
  };
162
1086
  }
1087
+ function describeScrollTarget(target) {
1088
+ if (target === void 0) return "natural";
1089
+ if (typeof target === "string") return target;
1090
+ if ("by" in target) return `by:${target.by}`;
1091
+ if ("to" in target) return `to:${target.to}`;
1092
+ return target.toString?.() ?? "locator";
1093
+ }
1094
+ function describeReadTarget(target) {
1095
+ if (typeof target === "string") return target;
1096
+ if ("words" in target && typeof target.words === "number") return `${target.words} words`;
1097
+ if ("text" in target && typeof target.text === "string") {
1098
+ return `text:${target.text.length} chars`;
1099
+ }
1100
+ return target.toString?.() ?? "locator";
1101
+ }
163
1102
 
164
- export { createHuman };
1103
+ export { Recording, createHuman, installMouseHelper };
165
1104
  //# sourceMappingURL=index.js.map
166
1105
  //# sourceMappingURL=index.js.map