@capybara-hq/plugin-browser 0.1.0-rc.8 → 0.1.0-rc.9

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.8",
4
+ "version": "0.1.0-rc.9",
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.8",
20
+ "version": "0.1.0-rc.9",
21
21
  "sdk": "^0.1.0-rc.75",
22
22
  "provides": [],
23
23
  "requires": [],
package/README.md CHANGED
@@ -162,9 +162,10 @@ shared by every conversation, the tabs are not.
162
162
 
163
163
  - **Owned per conversation.** Every tab belongs to one conversation, which alone acts on it, and
164
164
  each conversation has its own active tab and its own action queue. A conversation's first acting
165
- call takes the unowned blank tab if there is one, else opens a new tab; `tabs list`, `adopt` and
166
- the handoff bookkeeping never open one. A popup belongs to the owner of the page that opened it.
167
- `switch` and `close` take only the conversation's own tabs.
165
+ call takes the browser's blank start tab if it is free, else opens a new tab (never another
166
+ unowned page); `tabs list`, `adopt`, `wait` and the handoff bookkeeping never open one. When a
167
+ conversation loses its active tab, the tab it gained last becomes active. A popup belongs to the
168
+ owner of the page that opened it. `switch` and `close` take only the conversation's own tabs.
168
169
  - **Seen per task tree.** A conversation and its sub-agent conversations form a tree whose root is
169
170
  the conversation the person looks at. `tabs list` shows the conversation's own tabs, then the
170
171
  tree's other tabs, then other trees' tabs (titles cut to 120 characters, URLs without query or
package/dist/index.mjs CHANGED
@@ -1137,7 +1137,7 @@ function registerBrowserTools(ctx, deps) {
1137
1137
  // src/index.ts
1138
1138
  var index_default = defineExtension({
1139
1139
  name: "browser",
1140
- version: "0.1.0-rc.8",
1140
+ version: "0.1.0-rc.9",
1141
1141
  sdk: "^0.1.0-rc.75",
1142
1142
  provides: [],
1143
1143
  ui: [
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capybara-hq/plugin-browser",
3
- "version": "0.1.0-rc.8",
3
+ "version": "0.1.0-rc.9",
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",
@@ -769,6 +769,7 @@ function within(real, root) {
769
769
  var sameFile = (a, b) => a.dev === b.dev && a.ino === b.ino;
770
770
  var REF_RE = /^(f\d+)?e\d+$/;
771
771
  var PROXY_FAILURE_RE = /ERR_TUNNEL_CONNECTION_FAILED|ERR_PROXY_CONNECTION_FAILED/;
772
+ var NO_TAB_TO_WAIT_ON = "This conversation has no browser tab to wait on; call browser_navigate to open a page first.";
772
773
  var ERROR_PAGE_INTERRUPT_RE = /is interrupted by another navigation to "chrome-error:/;
773
774
  var ERROR_PAGE_WAIT_MS = 2e3;
774
775
  var isTimeout = (e) => e instanceof Error && e.name === "TimeoutError";
@@ -1089,12 +1090,15 @@ var BrowserSession = class {
1089
1090
  /** Tabs being opened by the session (not yet tracked): they count toward the device cap. */
1090
1091
  reserved = 0;
1091
1092
  /**
1092
- * Tabs of an idle-closed conversation whose close is under way: no conversation owns, claims,
1093
- * adopts or lists them any more.
1093
+ * Tabs whose close is under way (an idle-closed conversation's, an evicted one): no
1094
+ * conversation owns, claims, adopts or lists them any more, and the device cap leaves them out.
1094
1095
  */
1095
1096
  closing = /* @__PURE__ */ new Set();
1096
1097
  /** When an agent request last acted on each tab (the eviction's least recently used tab). */
1097
1098
  tabUsedAt = /* @__PURE__ */ new Map();
1099
+ /** When each tab went to its owner (the next active tab after a loss is the newest). */
1100
+ gainedAt = /* @__PURE__ */ new Map();
1101
+ gainSeq = 0;
1098
1102
  /** Order of the conversations' last acting requests (a viewer's default shown tab). */
1099
1103
  actedSeq = /* @__PURE__ */ new Map();
1100
1104
  actSeq = 0;
@@ -1122,7 +1126,7 @@ var BrowserSession = class {
1122
1126
  void this.allocate(async () => {
1123
1127
  const id = this.ids.get(p);
1124
1128
  if (id === void 0 || this.owners.has(id) || p.isClosed()) return;
1125
- if (this.pages.size > MAX_TABS) await p.close().catch(() => void 0);
1129
+ if (this.live().length > MAX_TABS) await p.close().catch(() => void 0);
1126
1130
  });
1127
1131
  });
1128
1132
  });
@@ -1212,6 +1216,7 @@ var BrowserSession = class {
1212
1216
  assign(tabId, to) {
1213
1217
  const before = this.owners.get(tabId);
1214
1218
  this.owners.set(tabId, to);
1219
+ this.gainedAt.set(tabId, ++this.gainSeq);
1215
1220
  if (before !== void 0 && before.owner !== to.owner) this.activeMovedOff(before.owner, tabId);
1216
1221
  return before;
1217
1222
  }
@@ -1221,11 +1226,24 @@ var BrowserSession = class {
1221
1226
  */
1222
1227
  activeMovedOff(conversationId, tabId) {
1223
1228
  if (this.activeOf.get(conversationId) !== tabId) return;
1224
- const next = this.tabsOf(conversationId).filter((id) => id !== tabId).at(-1);
1229
+ const next = this.newestTabOf(conversationId, tabId);
1225
1230
  if (next === void 0) this.activeOf.delete(conversationId);
1226
1231
  else this.activeOf.set(conversationId, next);
1227
1232
  this.emit({ type: "active", conversationId });
1228
1233
  }
1234
+ /**
1235
+ * The conversation's tab it gained last (opened, claimed, adopted or restored), besides
1236
+ * `except`.
1237
+ */
1238
+ newestTabOf(conversationId, except) {
1239
+ let best;
1240
+ for (const id of this.tabsOf(conversationId)) {
1241
+ if (id === except) continue;
1242
+ if (best === void 0 || (this.gainedAt.get(id) ?? 0) > (this.gainedAt.get(best) ?? 0))
1243
+ best = id;
1244
+ }
1245
+ return best;
1246
+ }
1229
1247
  /** Runs a tab allocation (claim, open, eviction) one at a time across conversations. */
1230
1248
  allocate(fn) {
1231
1249
  const run = this.allocChain.then(fn, fn);
@@ -1240,7 +1258,7 @@ var BrowserSession = class {
1240
1258
  async pageFor(conversationId, root) {
1241
1259
  const cur = this.currentPage(conversationId);
1242
1260
  if (cur !== void 0) return cur;
1243
- const own = this.tabsOf(conversationId).at(-1);
1261
+ const own = this.newestTabOf(conversationId);
1244
1262
  if (own !== void 0) {
1245
1263
  this.setActive(conversationId, own);
1246
1264
  const page = this.pageById(own);
@@ -1249,13 +1267,13 @@ var BrowserSession = class {
1249
1267
  let claim = this.claiming.get(conversationId);
1250
1268
  if (claim === void 0) {
1251
1269
  claim = this.allocate(async () => {
1252
- const now = this.currentPage(conversationId) ?? this.pageById(this.tabsOf(conversationId).at(-1));
1270
+ const now = this.currentPage(conversationId) ?? this.pageById(this.newestTabOf(conversationId));
1253
1271
  if (now !== void 0) {
1254
1272
  this.setActive(conversationId, this.tabIdOf(now));
1255
1273
  return now;
1256
1274
  }
1257
1275
  const unowned = this.live().find(
1258
- ([id]) => !this.owners.has(id) && !this.usage.tabParked(id)
1276
+ ([id, p]) => isStartBlank({ owner: this.owners.get(id)?.owner, url: p.url() }) && !this.usage.tabParked(id)
1259
1277
  );
1260
1278
  if (unowned !== void 0) {
1261
1279
  const [id, page] = unowned;
@@ -1281,7 +1299,7 @@ var BrowserSession = class {
1281
1299
  async openLocked(conversationId, root) {
1282
1300
  const own = this.countOf(conversationId);
1283
1301
  if (own >= MAX_TABS_PER_CONVERSATION) throw new ActionError("failed", CONVERSATION_CAP_MESSAGE);
1284
- if (this.pages.size + this.reserved >= MAX_TABS && !await this.evict(conversationId))
1302
+ if (this.live().length + this.reserved >= MAX_TABS && !await this.evict(conversationId))
1285
1303
  throw new ActionError("failed", deviceCapMessage(own));
1286
1304
  this.reserved++;
1287
1305
  let page;
@@ -1308,7 +1326,7 @@ var BrowserSession = class {
1308
1326
  const live = this.live().filter(([id]) => !this.usage.tabParked(id));
1309
1327
  const unowned = live.find(([id]) => !this.owners.has(id));
1310
1328
  if (unowned !== void 0) {
1311
- await unowned[1].close().catch(() => void 0);
1329
+ await this.closeEvicted(unowned[0], unowned[1]);
1312
1330
  return true;
1313
1331
  }
1314
1332
  const owners = /* @__PURE__ */ new Set();
@@ -1328,10 +1346,20 @@ var BrowserSession = class {
1328
1346
  )[0] ?? [];
1329
1347
  if (tabId === void 0 || page === void 0) return false;
1330
1348
  const title = clip(this.titles.get(tabId) ?? "", OTHER_TITLE_CHARS) || "no title";
1331
- await page.close().catch(() => void 0);
1349
+ await this.closeEvicted(tabId, page);
1332
1350
  this.noteTo(victim, tabEvicted(tabId, title));
1333
1351
  return true;
1334
1352
  }
1353
+ /**
1354
+ * Closes an evicted tab, closing from the start: no request or adopt reaches it while its close
1355
+ * runs (the close handler forgets it; a tab that would not close is kept as it was).
1356
+ */
1357
+ async closeEvicted(tabId, page) {
1358
+ this.closing.add(tabId);
1359
+ await page.close().catch(() => {
1360
+ this.closing.delete(tabId);
1361
+ });
1362
+ }
1335
1363
  /**
1336
1364
  * Moves a tab to the conversation and makes it its active tab (the tool's and a viewer's adopt).
1337
1365
  * `in_use`: its owner has a request in flight or a park (or a park pins the tab); `cap`: the
@@ -1505,6 +1533,7 @@ var BrowserSession = class {
1505
1533
  this.favicons.delete(id);
1506
1534
  this.docStatus.delete(id);
1507
1535
  this.tabUsedAt.delete(id);
1536
+ this.gainedAt.delete(id);
1508
1537
  this.clicks.delete(id);
1509
1538
  const o = this.owners.get(id);
1510
1539
  this.owners.delete(id);
@@ -1539,7 +1568,7 @@ var BrowserSession = class {
1539
1568
  this.emit({ type: "active", conversationId: o.owner });
1540
1569
  this.emit({ type: "state" });
1541
1570
  void this.allocate(async () => {
1542
- if (popup.isClosed() || this.pages.size <= MAX_TABS) return;
1571
+ if (popup.isClosed() || this.live().length <= MAX_TABS) return;
1543
1572
  if (!await this.evict(o.owner)) closeIt(`the browser keeps at most ${MAX_TABS} tabs`);
1544
1573
  });
1545
1574
  }
@@ -2177,7 +2206,8 @@ var BrowserSession = class {
2177
2206
  if (!ERROR_PAGE_INTERRUPT_RE.test(firstLine(e))) throw e;
2178
2207
  const out = await watch.follow(deadline);
2179
2208
  if (out.kind === "loaded") return out.response;
2180
- if (out.kind === "failed") throw new Error(`${out.error} at ${out.url}`);
2209
+ if (out.kind === "failed" && !out.error.includes("net::ERR_ABORTED"))
2210
+ throw new Error(`${out.error} at ${out.url}`);
2181
2211
  return page.goto(url, {
2182
2212
  waitUntil: "domcontentloaded",
2183
2213
  timeout: Math.max(1, deadline - Date.now())
@@ -2377,7 +2407,7 @@ var BrowserSession = class {
2377
2407
  return {};
2378
2408
  }
2379
2409
  case "wait":
2380
- return this.waitOp(req, await activePage());
2410
+ return this.waitOp(req, this.currentPage(conv));
2381
2411
  case "screenshot": {
2382
2412
  const page = await activePage();
2383
2413
  const maxBytes = Math.min(MAX_SCREENSHOT_BYTES, Math.floor(req.maxBytes));
@@ -2507,6 +2537,12 @@ var BrowserSession = class {
2507
2537
  async waitOp(req, page) {
2508
2538
  const asked = Number(req.timeoutMs);
2509
2539
  const timeout = Number.isFinite(asked) ? Math.max(0, Math.min(MAX_WAIT_MS, Math.floor(asked))) : DEFAULT_WAIT_MS;
2540
+ if (page === void 0) {
2541
+ if (req.text !== void 0 || req.ref !== void 0 || req.navigation === true)
2542
+ throw new ActionError("failed", NO_TAB_TO_WAIT_ON);
2543
+ await new Promise((r) => this.clock.schedule(r, timeout));
2544
+ return {};
2545
+ }
2510
2546
  if (req.text !== void 0) {
2511
2547
  await page.getByText(req.text).first().waitFor({ state: "visible", timeout }).catch((e) => {
2512
2548
  throw e instanceof Error && e.name === "TimeoutError" ? new ActionError(