@william_jin/autobrowse 0.8.47 → 0.8.48

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.
@@ -22,6 +22,7 @@ import { SecretLeak, urlWithoutQuery } from "../auth/guard.js";
22
22
  import { credentialFor } from "../auth/login.js";
23
23
  import { SITE_LOGINS } from "../auth/sites.js";
24
24
  import { articleText } from "../browser/article.js";
25
+ import { failCode } from "../browser/captcha/index.js";
25
26
  import { cardMask, cardShown } from "../browser/card-mask.js";
26
27
  import { cleanStrings } from "../browser/clean-text.js";
27
28
  import { CodeFailed, planScope, runCode } from "../browser/code.js";
@@ -162,6 +163,11 @@ function leanUrls(answer) {
162
163
  ...(Array.isArray(a.done) ? { done: a.done.map(step) } : {}),
163
164
  };
164
165
  }
166
+ /** Captcha solves a page gets (`open`'s own included) before it goes to a person. */
167
+ const CAPTCHA_TRIES = 2;
168
+ const TO_PERSON = "a person: say what the step is and wait";
169
+ /** Commands that answer their own captcha: no second look after them. */
170
+ const CAPTCHA_SELF = new Set(["open", "captcha", "flow"]);
165
171
  /** A wall for the agent: a sign-in wall names the site's `<site>-login` tool (the provider's on its products). */
166
172
  export function wallAnswer(wall, url) {
167
173
  if (!wall || wall.kind === "captcha")
@@ -397,6 +403,8 @@ async function serve(opts, fp, finishFlow, sessionFiles, leg = { site: null, sto
397
403
  let stepBoxes = null;
398
404
  /** The command's typing, planned against real time: for its run row. */
399
405
  let stepTyped = null;
406
+ /** The command's captcha: what it saw or solved, for its run row. */
407
+ let stepCaptcha = null;
400
408
  /** The screen last pictured (URL shape and landmarks): a new one takes a picture. */
401
409
  let lastScreen = "";
402
410
  /** The Claude Code session that last sent a command: rows name a change. */
@@ -520,10 +528,12 @@ async function serve(opts, fp, finishFlow, sessionFiles, leg = { site: null, sto
520
528
  const pics = own ? stepPics : [];
521
529
  const boxes = own ? stepBoxes : null;
522
530
  const typed = own ? stepTyped : null;
531
+ const captchaRow = own ? stepCaptcha : null;
523
532
  if (own) {
524
533
  stepPics = [];
525
534
  stepBoxes = null;
526
535
  stepTyped = null;
536
+ stepCaptcha = null;
527
537
  }
528
538
  const h = history;
529
539
  if (!h || h.ended())
@@ -565,6 +575,7 @@ async function serve(opts, fp, finishFlow, sessionFiles, leg = { site: null, sto
565
575
  ...(pics.length ? { pics } : {}),
566
576
  ...(boxes ? { boxes } : {}),
567
577
  ...(typed ? { typed } : {}),
578
+ ...(captchaRow ? { captcha: captchaRow } : {}),
568
579
  ...(by ? { session: by } : {}),
569
580
  }));
570
581
  };
@@ -1130,16 +1141,8 @@ async function serve(opts, fp, finishFlow, sessionFiles, leg = { site: null, sto
1130
1141
  contacts: { ...card.tell, ...(card.taxId ? { taxId: card.taxId } : {}) },
1131
1142
  };
1132
1143
  const billing = await fillBilling();
1133
- // A card form often holds a captcha (Instacart's Add Payment): its submit fails until ticked.
1134
- const captcha = await captchaInView(page).catch(() => null);
1135
- return {
1136
- ok: true,
1137
- secret: c.secret,
1138
- ...(billing.length ? { billing } : {}),
1139
- ...(captcha
1140
- ? { captcha: `a captcha is on this form: {"cmd":"captcha"} before you submit (${captcha})` }
1141
- : {}),
1142
- };
1144
+ // A card form often holds a captcha (Instacart's Add Payment): `looked` names it after `place`.
1145
+ return { ok: true, secret: c.secret, ...(billing.length ? { billing } : {}) };
1143
1146
  };
1144
1147
  /**
1145
1148
  * The bank's confirmation code (3-D Secure, often in an iframe on the bank's
@@ -1480,6 +1483,88 @@ async function serve(opts, fp, finishFlow, sessionFiles, leg = { site: null, sto
1480
1483
  return wall;
1481
1484
  return { ...wall, signIn, accounts };
1482
1485
  };
1486
+ /** Solves tried per page (origin + path), `open`'s own try included. */
1487
+ const captchaTries = new Map();
1488
+ const pageKey = (url) => URL.canParse(url) ? new URL(url).origin + new URL(url).pathname : url;
1489
+ const triesLeft = (key = pageKey(page.url())) => Math.max(0, CAPTCHA_TRIES - (captchaTries.get(key) ?? 0));
1490
+ const captchaNext = (left, code, widget) => code === "no-eyes"
1491
+ ? `${TO_PERSON} (no model that sees: only checkboxes solve)`
1492
+ : left
1493
+ ? widget
1494
+ ? '{"cmd":"captcha"} before you submit'
1495
+ : '{"cmd":"captcha"}'
1496
+ : TO_PERSON;
1497
+ /**
1498
+ * One solve, counted against the page. The flow note replays it only when it worked.
1499
+ * Answers the outcome with `wallStill` (ticked, the wall's words stay), `triesLeft` and `next`.
1500
+ */
1501
+ const solveHere = async () => {
1502
+ const key = pageKey(page.url());
1503
+ captchaTries.set(key, (captchaTries.get(key) ?? 0) + 1);
1504
+ const got = await fp.captcha();
1505
+ page = fp.page;
1506
+ if (got.solved) {
1507
+ journal({ kind: "note", text: "solved a captcha here: `await fp.captcha()`" });
1508
+ // Cloudflare reloads the page once the box is ticked
1509
+ await page.waitForLoadState("networkidle", { timeout: 10_000 }).catch(() => undefined);
1510
+ }
1511
+ const wall = await looksLikeWall(page).catch(() => null);
1512
+ const code = failCode(got);
1513
+ const still = wall?.kind === "captcha";
1514
+ const left = triesLeft(key);
1515
+ stepCaptcha = {
1516
+ solved: got.solved,
1517
+ kind: got.kind,
1518
+ vendor: got.vendor,
1519
+ ...(code ? { code } : {}),
1520
+ ...(still ? { wallStill: true } : {}),
1521
+ triesLeft: left,
1522
+ };
1523
+ const answer = {
1524
+ ...got,
1525
+ ...(code ? { code } : {}),
1526
+ ...(got.solved && still ? { wallStill: true } : {}),
1527
+ triesLeft: left,
1528
+ // nothing found and no wall words: the page is past it
1529
+ ...((got.solved && !still) || (code === "none" && !still)
1530
+ ? {}
1531
+ : { next: captchaNext(left, code, false) }),
1532
+ };
1533
+ return { answer, wall };
1534
+ };
1535
+ /** After a load: a captcha wall solved once while tries are left, then the wall and any captcha still there. */
1536
+ const wallAndCaptcha = async () => {
1537
+ let wall = await looksLikeWall(page);
1538
+ let solved = null;
1539
+ if (wall?.kind === "captcha" && triesLeft() > 0) {
1540
+ // the widget's frame comes after the wall's words: a solve before it finds nothing
1541
+ await page.waitForLoadState("load", { timeout: 5_000 }).catch(() => undefined);
1542
+ ({ answer: solved, wall } = await solveHere());
1543
+ }
1544
+ const captcha = solved ?? (await captchaSeen(wall));
1545
+ return {
1546
+ ...(captcha ? { captcha } : {}),
1547
+ wall: await withAccounts(wallAnswer(wall, page.url())),
1548
+ };
1549
+ };
1550
+ /** A captcha the page shows after a command: a wall (its words) or a widget (its frame), with what to do. */
1551
+ const captchaSeen = async (known) => {
1552
+ const [wall, frame] = await Promise.all([
1553
+ known === undefined ? looksLikeWall(page).catch(() => null) : known,
1554
+ captchaInView(page).catch(() => null),
1555
+ ]);
1556
+ const seen = wall?.kind === "captcha" ? "wall" : frame ? "widget" : null;
1557
+ if (!seen)
1558
+ return null;
1559
+ const left = triesLeft();
1560
+ stepCaptcha ??= { seen, triesLeft: left };
1561
+ return {
1562
+ seen,
1563
+ ...(frame ? { frame } : {}),
1564
+ triesLeft: left,
1565
+ next: captchaNext(left, null, seen === "widget"),
1566
+ };
1567
+ };
1483
1568
  const looked = async (c, wait) => {
1484
1569
  if (!LOOKS.has(c.cmd))
1485
1570
  return { answer: await act(c, wait), rows: null };
@@ -1504,9 +1589,13 @@ async function serve(opts, fp, finishFlow, sessionFiles, leg = { site: null, sto
1504
1589
  const tabs = [...stepTabs]
1505
1590
  .filter((p) => !hadTabs.has(p) && !p.isClosed())
1506
1591
  .map((p) => ({ index: all.indexOf(p), url: p.url() }));
1592
+ const captcha = CAPTCHA_SELF.has(c.cmd) || (out && typeof out === "object" && "captcha" in out)
1593
+ ? null
1594
+ : await captchaSeen();
1507
1595
  return {
1508
1596
  answer: {
1509
1597
  ...out,
1598
+ ...(captcha ? { captcha } : {}),
1510
1599
  changed,
1511
1600
  ...(tabs.length ? { tabs } : {}),
1512
1601
  ...(opened.length ? { dialogs: opened } : {}),
@@ -1764,9 +1853,9 @@ async function serve(opts, fp, finishFlow, sessionFiles, leg = { site: null, sto
1764
1853
  const g = run.gate;
1765
1854
  const account = accountIn(raw);
1766
1855
  // a guarded leg stopped at a sign-in: the wall, its login and accounts, as `open` answers
1767
- const wall = status === "failed"
1768
- ? await withAccounts(wallAnswer(await looksLikeWall(page), page.url()))
1769
- : null;
1856
+ const shown = status === "failed" ? await looksLikeWall(page).catch(() => null) : null;
1857
+ const wall = shown ? await withAccounts(wallAnswer(shown, page.url())) : null;
1858
+ const captcha = status === "failed" ? await captchaSeen(shown) : null;
1770
1859
  return JSON.parse(redactText(JSON.stringify({
1771
1860
  flow: c.name,
1772
1861
  status,
@@ -1775,6 +1864,7 @@ async function serve(opts, fp, finishFlow, sessionFiles, leg = { site: null, sto
1775
1864
  ...(account ? { account } : {}),
1776
1865
  ...(crossSite.size ? { otherSite: [...crossSite] } : {}),
1777
1866
  ...(wall ? { wall } : {}),
1867
+ ...(captcha ? { captcha } : {}),
1778
1868
  url: page.url(),
1779
1869
  })));
1780
1870
  };
@@ -2009,11 +2099,7 @@ async function serve(opts, fp, finishFlow, sessionFiles, leg = { site: null, sto
2009
2099
  throw outsideSession(c.url);
2010
2100
  // start.sh <site> <url> loaded it; a second load is a flash on a film. Later opens always load (a reopen re-reads a page).
2011
2101
  if (!c.reload && asLoaded && samePage(page.url(), c.url))
2012
- return {
2013
- url: page.url(),
2014
- stayed: true,
2015
- wall: await withAccounts(wallAnswer(await looksLikeWall(page), page.url())),
2016
- };
2102
+ return { url: page.url(), stayed: true, ...(await wallAndCaptcha()) };
2017
2103
  // the main frame's last document answer: a 404 or 401 page reads like any other
2018
2104
  let status = 0;
2019
2105
  const onDoc = (r) => {
@@ -2038,24 +2124,12 @@ async function serve(opts, fp, finishFlow, sessionFiles, leg = { site: null, sto
2038
2124
  // `open` goes to the main page: the tabs earlier steps opened are behind it now.
2039
2125
  page = fp.page;
2040
2126
  const closedTabs = await closeStepTabs();
2041
- let wall = await looksLikeWall(page);
2042
- let captcha;
2043
- if (wall?.kind === "captcha") {
2044
- captcha = await fp.captcha();
2045
- page = fp.page;
2046
- if (captcha.solved) {
2047
- journal({ kind: "note", text: "solved a captcha here: `await fp.captcha()`" });
2048
- // Cloudflare reloads the page once the box is ticked
2049
- await page.waitForLoadState("networkidle", { timeout: 10_000 }).catch(() => undefined);
2050
- }
2051
- wall = await looksLikeWall(page);
2052
- }
2127
+ const after = await wallAndCaptcha();
2053
2128
  return {
2054
2129
  url: page.url(),
2055
2130
  ...(status >= 400 ? { status } : {}),
2056
2131
  ...(closedTabs ? { closedTabs } : {}),
2057
- ...(captcha ? { captcha } : {}),
2058
- wall: await withAccounts(wallAnswer(wall, page.url())),
2132
+ ...after,
2059
2133
  };
2060
2134
  }
2061
2135
  case "click": {
@@ -2595,12 +2669,8 @@ async function serve(opts, fp, finishFlow, sessionFiles, leg = { site: null, sto
2595
2669
  next: `place {"secret":"${name}.code"} into the code box`,
2596
2670
  };
2597
2671
  }
2598
- case "captcha": {
2599
- const got = await fp.captcha();
2600
- page = fp.page;
2601
- journal({ kind: "note", text: "solved a captcha here: `await fp.captcha()`" });
2602
- return got;
2603
- }
2672
+ case "captcha":
2673
+ return (await solveHere()).answer;
2604
2674
  case "note":
2605
2675
  journal({ kind: "note", text: c.text });
2606
2676
  return { ok: true };
@@ -2844,12 +2914,17 @@ async function serve(opts, fp, finishFlow, sessionFiles, leg = { site: null, sto
2844
2914
  const notes = overlay.takeNotes();
2845
2915
  // The browser went mid-act: the click or submit may have landed.
2846
2916
  const outcome = e.outcome ?? (LOOKS.has(parsed.cmd) && browserGone(err) ? "unknown" : null);
2917
+ // an act that failed on a captcha: the click that "did nothing" behind a challenge
2918
+ const captcha = LOOKS.has(parsed.cmd) && !CAPTCHA_SELF.has(parsed.cmd) && !browserGone(err)
2919
+ ? await captchaSeen().catch(() => null)
2920
+ : null;
2847
2921
  const body = JSON.stringify({
2848
2922
  error,
2849
2923
  code: e.code,
2850
2924
  retryable: e.retryable,
2851
2925
  ...(outcome ? { outcome } : {}),
2852
2926
  ...(notes.length ? { notes } : {}),
2927
+ ...(captcha ? { captcha } : {}),
2853
2928
  url: shortUrl(page.url()),
2854
2929
  ...(err instanceof PaymentGate ? { gate: err.gate, reason: err.reason } : {}),
2855
2930
  ...(err instanceof AmbiguousTarget
@@ -2899,6 +2974,9 @@ async function serve(opts, fp, finishFlow, sessionFiles, leg = { site: null, sto
2899
2974
  };
2900
2975
  writeFileSync(infoFile, JSON.stringify(info), { mode: 0o600 });
2901
2976
  }
2977
+ // start.sh prints `warning:` blocks: the agent learns before its first captcha
2978
+ if (!opts.captcha?.eyes || (Array.isArray(opts.captcha.eyes) && !opts.captcha.eyes.length))
2979
+ console.error("warning: captcha: no model that sees (eyesProvider, eyesPanel or ANTHROPIC_API_KEY); only checkbox captchas solve\n");
2902
2980
  if (opts.allowHosts) {
2903
2981
  const held = await fenceNavigations(page.context(), opts.allowHosts, {
2904
2982
  hops: hopsOf(opts),