@capybara-hq/plugin-browser 0.1.0-rc.13 → 0.1.0-rc.14

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.
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "schemaVersion": 1,
3
3
  "name": "browser",
4
- "version": "0.1.0-rc.13",
4
+ "version": "0.1.0-rc.14",
5
5
  "publisher": "Capybara",
6
6
  "description": {
7
7
  "en": "A real browser in the conversation owner's device for agents: navigate, read and act on pages through tools, with a live view in the conversation panel and takeover by the person",
@@ -17,7 +17,7 @@
17
17
  "entry": "./dist/index.mjs",
18
18
  "manifest": {
19
19
  "name": "browser",
20
- "version": "0.1.0-rc.13",
20
+ "version": "0.1.0-rc.14",
21
21
  "sdk": "^0.1.0-rc.83",
22
22
  "provides": [],
23
23
  "requires": [],
package/dist/index.mjs CHANGED
@@ -1514,7 +1514,7 @@ function registerBrowserTools(ctx, deps) {
1514
1514
  // src/index.ts
1515
1515
  var index_default = defineExtension({
1516
1516
  name: "browser",
1517
- version: "0.1.0-rc.13",
1517
+ version: "0.1.0-rc.14",
1518
1518
  sdk: "^0.1.0-rc.83",
1519
1519
  provides: [],
1520
1520
  ui: [
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capybara-hq/plugin-browser",
3
- "version": "0.1.0-rc.13",
3
+ "version": "0.1.0-rc.14",
4
4
  "type": "module",
5
5
  "license": "Apache-2.0",
6
6
  "description": "A real browser in the person's device for agents: navigate, read and act on pages through tools, with a live view in the conversation and takeover by the person",
@@ -1524,6 +1524,7 @@ async function captureFitting(cdp, opts) {
1524
1524
  const area = opts.fullPage ? { x: 0, y: 0, width: content.width, height: Math.min(content.height, 16384) } : { x: vv.pageX, y: vv.pageY, width: vv.clientWidth, height: vv.clientHeight };
1525
1525
  const maxScale = Math.min(1, opts.maxScale ?? 1);
1526
1526
  const zoom = opts.fullPage === true ? 1 : vv.scale > 0 && vv.scale < 1 ? vv.scale : 1;
1527
+ const device = opts.deviceScale !== void 0 && opts.deviceScale > 0 ? opts.deviceScale : 1;
1527
1528
  for (const s of SCALES) {
1528
1529
  const scale = s * maxScale * zoom;
1529
1530
  if (scale * area.width < 16) break;
@@ -1531,7 +1532,7 @@ async function captureFitting(cdp, opts) {
1531
1532
  const shot = await cdp.send("Page.captureScreenshot", {
1532
1533
  format: "jpeg",
1533
1534
  quality,
1534
- clip: { ...area, scale },
1535
+ clip: { ...area, scale: scale / device },
1535
1536
  captureBeyondViewport: opts.fullPage === true
1536
1537
  });
1537
1538
  const bytes = Buffer.from(shot.data, "base64");
@@ -1809,6 +1810,12 @@ var BrowserSession = class {
1809
1810
  profileSessions = /* @__PURE__ */ new Map();
1810
1811
  /** Every agent request waits for this (a restore to desktop after hand back). */
1811
1812
  viewportGate = Promise.resolve();
1813
+ /** The clipped captures queued or running (a profile change waits for them: see `capture`). */
1814
+ captures = /* @__PURE__ */ new Set();
1815
+ /** The profile sessions that sent device metrics (a capture through one puts them back). */
1816
+ withMetrics = /* @__PURE__ */ new WeakSet();
1817
+ /** The newest clipped capture (they run one at a time). */
1818
+ captureTail = Promise.resolve();
1812
1819
  mobileUa;
1813
1820
  /** The pages' own user agent (given back when the phone profile ends). */
1814
1821
  desktopUa;
@@ -2232,7 +2239,7 @@ var BrowserSession = class {
2232
2239
  const id = `t${this.nextId++}`;
2233
2240
  this.ids.set(page, id);
2234
2241
  this.pages.set(id, page);
2235
- if (this.profile === "phone") void this.applyPhone(page);
2242
+ if (this.profile === "phone") this.queueProfileChange(() => this.applyPhone(page));
2236
2243
  const refresh = () => {
2237
2244
  page.title().then((t) => this.titles.set(id, t)).catch(() => void 0).then(() => this.checkVerification(page, id)).finally(() => this.emit({ type: "state" }));
2238
2245
  };
@@ -2698,13 +2705,11 @@ var BrowserSession = class {
2698
2705
  this.profile = profile;
2699
2706
  this.touchTranslator.reset();
2700
2707
  const pages = this.live().map(([, p]) => p);
2701
- const run = async () => {
2708
+ this.queueProfileChange(async () => {
2702
2709
  if (profile === "phone") await Promise.all(pages.map((p) => this.applyPhone(p)));
2703
2710
  else await this.restoreAll();
2704
2711
  if (this.profile === profile) this.applied = profile;
2705
- };
2706
- const next = this.viewportGate.then(run, run);
2707
- this.viewportGate = next.catch(() => void 0);
2712
+ });
2708
2713
  this.emit({ type: "state" });
2709
2714
  void this.viewportGate.then(() => this.emit({ type: "state" }));
2710
2715
  return this.viewportGate;
@@ -2765,6 +2770,7 @@ var BrowserSession = class {
2765
2770
  screenWidth: p.width,
2766
2771
  screenHeight: p.height
2767
2772
  });
2773
+ this.withMetrics.add(cdp);
2768
2774
  await cdp.send("Emulation.setTouchEmulationEnabled", { enabled: true, maxTouchPoints: 5 });
2769
2775
  await cdp.send("Emulation.setUserAgentOverride", {
2770
2776
  userAgent: this.mobileUa ?? mobileUserAgent("")
@@ -2794,7 +2800,7 @@ var BrowserSession = class {
2794
2800
  mobile: false,
2795
2801
  screenWidth: VIEWPORT.width,
2796
2802
  screenHeight: VIEWPORT.height
2797
- }).catch(() => void 0);
2803
+ }).then(() => this.withMetrics.add(cdp)).catch(() => void 0);
2798
2804
  await cdp.send("Emulation.setTouchEmulationEnabled", { enabled: false }).catch(() => void 0);
2799
2805
  if (this.desktopUa !== void 0)
2800
2806
  await cdp.send("Emulation.setUserAgentOverride", { userAgent: this.desktopUa }).catch(() => void 0);
@@ -2808,9 +2814,75 @@ var BrowserSession = class {
2808
2814
  */
2809
2815
  reapplyPhone(page) {
2810
2816
  if (this.profile !== "phone") return;
2811
- const run = () => this.applyPhone(page, true).then(() => void 0);
2817
+ this.queueProfileChange(() => this.applyPhone(page, true));
2818
+ }
2819
+ /**
2820
+ * Queues a change of the pages' device metrics in the profile chain (the viewport gate): it runs
2821
+ * after the changes before it and once the clipped captures in flight are done (bounded: a
2822
+ * capture of a page a JS dialog holds never answers).
2823
+ */
2824
+ queueProfileChange(change) {
2825
+ const run = async () => {
2826
+ await this.bounded(Promise.allSettled([...this.captures]), []);
2827
+ await change();
2828
+ };
2812
2829
  const next = this.viewportGate.then(run, run);
2813
- this.viewportGate = next.catch(() => void 0);
2830
+ this.viewportGate = next.then(
2831
+ () => void 0,
2832
+ () => void 0
2833
+ );
2834
+ }
2835
+ /**
2836
+ * A clipped capture of a page (`captureFitting`, plugins#298). Chromium captures a clip by
2837
+ * replacing the page's device emulation with a temporary one built from the capturing session's
2838
+ * own metrics and, when done, puts back that session's metrics as they were at the start: from
2839
+ * a session without metrics the page is left with none (at the view size of the start, the
2840
+ * window's on macOS), a profile change in between is lost, and a second capture meanwhile reads
2841
+ * the temporary metrics. So a capture goes through the page's metrics session, runs alone (each
2842
+ * waits for the one before, bounded: a capture of a page a JS dialog holds never answers),
2843
+ * starts once no profile change is queued, and a change waits for the captures queued before it.
2844
+ */
2845
+ async capture(page, opts) {
2846
+ for (; ; ) {
2847
+ const gate = this.viewportGate;
2848
+ await gate;
2849
+ if (gate === this.viewportGate) break;
2850
+ }
2851
+ const before = this.captureTail;
2852
+ const shot = (async () => {
2853
+ await this.bounded(before, void 0);
2854
+ const cdp = await this.metricsSession(page);
2855
+ if (cdp === void 0) throw new ActionError("failed", "The tab is gone.");
2856
+ const scale = VIEWPORT_PROFILES[this.applied].scale;
2857
+ return captureFitting(cdp, { ...opts, deviceScale: scale });
2858
+ })();
2859
+ this.captureTail = shot.catch(() => void 0);
2860
+ this.captures.add(shot);
2861
+ try {
2862
+ return await shot;
2863
+ } finally {
2864
+ this.captures.delete(shot);
2865
+ }
2866
+ }
2867
+ /**
2868
+ * The session a page's captures go through: its profile session, holding the metrics of the
2869
+ * profile applied (sent now if it never sent any). A capture puts back the metrics of its own
2870
+ * session when done, so on this one it puts back the page's real metrics.
2871
+ */
2872
+ async metricsSession(page) {
2873
+ const cdp = await this.profileSession(page);
2874
+ if (cdp === void 0 || this.withMetrics.has(cdp)) return cdp;
2875
+ const p = VIEWPORT_PROFILES[this.applied];
2876
+ await cdp.send("Emulation.setDeviceMetricsOverride", {
2877
+ width: p.width,
2878
+ height: p.height,
2879
+ deviceScaleFactor: p.scale,
2880
+ mobile: p.mobile,
2881
+ screenWidth: p.width,
2882
+ screenHeight: p.height
2883
+ });
2884
+ this.withMetrics.add(cdp);
2885
+ return cdp;
2814
2886
  }
2815
2887
  /** Every page that got the phone profile back on desktop. */
2816
2888
  async restoreAll() {
@@ -3732,25 +3804,20 @@ var BrowserSession = class {
3732
3804
  const maxBytes = Math.min(MAX_SCREENSHOT_BYTES, Math.floor(req.maxBytes));
3733
3805
  if (!(maxBytes > 0))
3734
3806
  throw new ActionError("failed", "screenshot needs a positive maxBytes.");
3735
- const cdp = await this.ctx.newCDPSession(page);
3736
- try {
3737
- const shot = await captureFitting(cdp, {
3738
- maxBytes,
3739
- ...req.fullPage === true ? { fullPage: true } : {}
3740
- });
3741
- if (shot === null) {
3742
- throw new ActionError(
3743
- "failed",
3744
- `The page does not fit in a ${maxBytes}-byte screenshot even at the smallest size; use snapshot instead.`
3745
- );
3746
- }
3747
- return {
3748
- image: shot.data,
3749
- text: `Screenshot ${shot.width}x${shot.height} (viewport ${VIEWPORT.width}x${VIEWPORT.height}).`
3750
- };
3751
- } finally {
3752
- await cdp.detach().catch(() => void 0);
3807
+ const shot = await this.capture(page, {
3808
+ maxBytes,
3809
+ ...req.fullPage === true ? { fullPage: true } : {}
3810
+ });
3811
+ if (shot === null) {
3812
+ throw new ActionError(
3813
+ "failed",
3814
+ `The page does not fit in a ${maxBytes}-byte screenshot even at the smallest size; use snapshot instead.`
3815
+ );
3753
3816
  }
3817
+ return {
3818
+ image: shot.data,
3819
+ text: `Screenshot ${shot.width}x${shot.height} (viewport ${VIEWPORT.width}x${VIEWPORT.height}).`
3820
+ };
3754
3821
  }
3755
3822
  }
3756
3823
  throw new ActionError(
@@ -5402,7 +5469,7 @@ var ViewerSession = class {
5402
5469
  this.cursorResolver = new CursorResolver(this.browser.context, page, cdp);
5403
5470
  let timer;
5404
5471
  const first = await Promise.race([
5405
- captureFitting(cdp, { maxBytes: this.jpegBudget, maxScale: this.scale }).catch(() => null),
5472
+ this.browser.capture(page, { maxBytes: this.jpegBudget, maxScale: this.scale }).catch(() => null),
5406
5473
  new Promise((r) => {
5407
5474
  timer = this.clock.schedule(() => r(null), FIRST_FRAME_MS);
5408
5475
  })