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

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/ui/panel.js CHANGED
@@ -61,6 +61,8 @@
61
61
  };
62
62
  }
63
63
  var DEFAULT_FRAME_MAX_BYTES = 256 * 1024;
64
+ var CONVERSATION_IDLE_MS = 15 * 6e4;
65
+ var EVICT_MIN_IDLE_MS = 5 * 6e4;
64
66
  var HANDOFF_KEEPALIVE_MS = 24 * 60 * 6e4;
65
67
  var FAVICON_MAX_BYTES = 8 * 1024;
66
68
  var isIconDataUrl = (s) => typeof s === "string" && s.length <= 64 + Math.ceil(FAVICON_MAX_BYTES * 4 / 3) && /^data:image\/(png|x-icon|gif|jpeg|webp|svg\+xml);base64,[A-Za-z0-9+/=]+$/.test(s);
@@ -105,7 +107,14 @@
105
107
  secure: "Secure connection",
106
108
  fileDialog: "The page opened a file chooser; the agent can pick a file with its upload tool.",
107
109
  allowFailed: "Could not allow the host:",
108
- answerFailed: "Could not send your answer:"
110
+ answerFailed: "Could not send your answer:",
111
+ subTask: "Sub-task",
112
+ otherConversations: "Other conversations",
113
+ moveHere: "Move here",
114
+ tabInUse: "Another conversation is using this tab; try again shortly",
115
+ tabCap: "This conversation has the most tabs it can keep; close one first",
116
+ treeEmptyTitle: "The agent has not opened a page in this conversation yet",
117
+ treeEmptyBody: "Its pages show here as it opens them."
109
118
  },
110
119
  zh: {
111
120
  title: "浏览器",
@@ -145,7 +154,14 @@
145
154
  secure: "安全连接",
146
155
  fileDialog: "页面打开了文件选择框;智能体可以用上传工具选择文件。",
147
156
  allowFailed: "无法允许该主机:",
148
- answerFailed: "无法发送你的回答:"
157
+ answerFailed: "无法发送你的回答:",
158
+ subTask: "子任务",
159
+ otherConversations: "其他对话",
160
+ moveHere: "移到此对话",
161
+ tabInUse: "该标签页正被其他对话使用,请稍后再试",
162
+ tabCap: "此对话的标签页已达上限,请先关闭一个",
163
+ treeEmptyTitle: "智能体尚未在此对话中打开网页",
164
+ treeEmptyBody: "它打开的网页会显示在这里。"
149
165
  }
150
166
  };
151
167
  function t(locale, key) {
@@ -446,7 +462,12 @@
446
462
  .row { flex: none; display: flex; align-items: center; padding: 0 12px; }
447
463
 
448
464
  /* tab strip */
449
- #tabs { height: 32px; align-items: stretch; gap: 2px; overflow-x: auto; scrollbar-width: none; }
465
+ #tabs { height: 32px; align-items: stretch; gap: 4px; }
466
+ #tablist { flex: 1 1 auto; min-width: 0; display: flex; align-items: stretch; gap: 2px; overflow-x: auto; scrollbar-width: none; }
467
+ /* a group of the tree's other conversations: a quiet label before its tabs, set apart by space */
468
+ #tablist .group { flex: none; align-self: center; margin-left: 12px; padding: 0 2px; font-size: var(--cb-type-micro-size, 11px); line-height: var(--cb-type-micro-line, 14px); color: var(--cb-muted, #5E5E5A); white-space: nowrap; }
469
+ #others { align-self: center; height: 24px; padding: 0 8px; font-size: var(--cb-type-micro-size, 11px); color: var(--cb-muted, #5E5E5A); }
470
+ #others[aria-expanded="true"] { background: var(--cb-hover, rgba(26,26,24,.04)); }
450
471
  .tab { display: flex; align-items: center; gap: 6px; flex: 0 1 auto; min-width: 64px; max-width: 200px; padding: 0 10px; color: var(--cb-muted, #5E5E5A); }
451
472
  .tab[data-active="true"] { color: var(--cb-ink, #1A1A18); font-weight: 500; box-shadow: inset 0 -2px 0 var(--cb-accent, #CC4609); padding-right: 6px; }
452
473
  .fav { width: 14px; height: 14px; border-radius: 3px; flex: none; background: var(--cb-line-soft, #E8E7E2); }
@@ -456,7 +477,7 @@
456
477
  .tab .label:hover:not(:disabled) { background: transparent; }
457
478
  .tab .label:disabled { color: inherit; }
458
479
  .tab .x { width: 20px; min-width: 20px; height: 20px; border-radius: 4px; color: var(--cb-muted, #5E5E5A); }
459
- #tabs .add { align-self: center; color: var(--cb-muted, #5E5E5A); }
480
+ #tablist .add { align-self: center; color: var(--cb-muted, #5E5E5A); }
460
481
 
461
482
  /* toolbar */
462
483
  #bar { height: 44px; gap: 4px; }
@@ -523,6 +544,10 @@
523
544
  .menu { position: absolute; z-index: 2; min-width: 180px; max-width: calc(100% - 24px); max-height: 60%; overflow: auto; padding: 4px; border-radius: var(--cb-radius-md, 8px); background: var(--cb-raised, #fff); box-shadow: var(--cb-shadow-float, 0 8px 24px rgba(26,26,24,.08)), 0 0 0 1px var(--cb-line-soft, #E8E7E2); }
524
545
  .menu button { display: flex; width: 100%; justify-content: flex-start; gap: 8px; padding: 0 8px; text-align: left; overflow: hidden; text-overflow: ellipsis; }
525
546
  .menu button[aria-current="true"] { font-weight: 600; }
547
+ .menu .heading { margin: 0; padding: 6px 8px 2px; font-size: var(--cb-type-micro-size, 11px); line-height: var(--cb-type-micro-line, 14px); color: var(--cb-muted, #5E5E5A); }
548
+ .menu .heading:not(:first-child) { margin-top: 4px; }
549
+ .menu button .name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; }
550
+ .menu button .hint { flex: none; margin-left: 8px; color: var(--cb-accent, #CC4609); }
526
551
 
527
552
  /* the skeleton: the panel's own shape, quietly pulsing */
528
553
  .bone { display: block; flex: none; border-radius: 2px; background: var(--bone); animation: pulse 1.6s ease-in-out infinite; }
@@ -547,7 +572,7 @@
547
572
  </div>
548
573
  </div>
549
574
  <div id="app" class="layer" data-loading>
550
- <div id="tabs" class="row" role="tablist"></div>
575
+ <div id="tabs" class="row"><div id="tablist" role="tablist"></div><button type="button" id="others" aria-haspopup="menu" hidden></button></div>
551
576
  <div id="bar" class="row">
552
577
  <button type="button" id="back">${ICONS.back}</button>
553
578
  <button type="button" id="forward">${ICONS.forward}</button>
@@ -575,6 +600,7 @@
575
600
  <h2 id="empty-title"></h2>
576
601
  <p id="empty-body"></p>
577
602
  <button type="button" id="reconnect" class="secondary"></button>
603
+ <button type="button" id="empty-others" class="secondary" aria-haspopup="menu" hidden></button>
578
604
  </div>
579
605
  <div id="menu" class="menu" role="menu" hidden></div>
580
606
  <script src="panel.js"><\/script>
@@ -629,6 +655,7 @@
629
655
  let sock = null;
630
656
  let mode = "connecting";
631
657
  let tabs = [];
658
+ let otherTabs = [];
632
659
  let control = "agent";
633
660
  let lastState = null;
634
661
  let retried401 = false;
@@ -652,13 +679,14 @@
652
679
  let pendingHandoff = null;
653
680
  let sentSize = "";
654
681
  let image = null;
655
- let shown = "skeleton";
682
+ let layer = "skeleton";
656
683
  const compact = () => {
657
684
  const sw = win.screen?.width ?? 0;
658
685
  return sw > 0 && sw < COMPACT_BELOW;
659
686
  };
660
687
  const text = (key) => t(locale, key);
661
688
  const interactive = () => !compact() && control === "user";
689
+ const treeEmpty = () => mode === "live" && lastState !== null && tabs.length === 0;
662
690
  function send(msg) {
663
691
  if (sock === null) return;
664
692
  sock.send(new TextEncoder().encode(JSON.stringify(msg)));
@@ -761,7 +789,6 @@
761
789
  for (const id of ["back", "forward", "reload"])
762
790
  $(id).disabled = readOnly;
763
791
  $("more").disabled = readOnly;
764
- $("tabsbtn").disabled = readOnly;
765
792
  url.readOnly = readOnly;
766
793
  input.sync();
767
794
  $("lock").hidden = !isLive || lastState?.secure !== true;
@@ -770,9 +797,13 @@
770
797
  const keys = {
771
798
  empty: ["emptyTitle", "emptyBody"],
772
799
  offline: ["offlineTitle", "offlineBody"],
773
- error: ["errorTitle", "errorBody"]
800
+ error: ["errorTitle", "errorBody"],
801
+ tree: ["treeEmptyTitle", "treeEmptyBody"]
774
802
  };
775
- const k = keys[mode];
803
+ const k = keys[treeEmpty() ? "tree" : mode];
804
+ const emptyOthers = $("empty-others");
805
+ emptyOthers.hidden = !treeEmpty() || otherTabs.length === 0;
806
+ emptyOthers.textContent = othersLabel();
776
807
  if (k !== void 0) {
777
808
  $("empty-title").textContent = text(k[0]);
778
809
  $("empty-body").textContent = text(k[1]);
@@ -783,13 +814,13 @@
783
814
  renderLoading();
784
815
  }
785
816
  function renderLoading() {
786
- const idleMode = mode === "empty" || mode === "offline" || mode === "error";
817
+ const idleMode = mode === "empty" || mode === "offline" || mode === "error" || treeEmpty();
787
818
  let next;
788
819
  if (idleMode) next = "idle";
789
820
  else if (mode === "live" && image !== null) next = "app";
790
- else if (mode === "live" && shown === "idle") next = "idle";
821
+ else if (mode === "live" && layer === "idle") next = "idle";
791
822
  else next = "skeleton";
792
- shown = next;
823
+ layer = next;
793
824
  $("app").toggleAttribute("data-loading", next === "skeleton");
794
825
  $("app").toggleAttribute("data-idle", next === "idle");
795
826
  $("skeleton").hidden = next !== "skeleton";
@@ -797,14 +828,27 @@
797
828
  if (next === "app") onBoxChange();
798
829
  }
799
830
  function renderTabs() {
800
- const box = $("tabs");
831
+ const box = $("tablist");
801
832
  box.textContent = "";
802
833
  const watchOnly = !interactive();
834
+ const subs = subGroups();
835
+ let group = "own";
803
836
  for (const tab of tabs) {
837
+ if (tab.group !== group) {
838
+ group = tab.group;
839
+ if (group !== "own") {
840
+ const label2 = doc.createElement("span");
841
+ label2.className = "group";
842
+ label2.setAttribute("role", "presentation");
843
+ label2.textContent = groupLabel(group, subs);
844
+ box.appendChild(label2);
845
+ }
846
+ }
804
847
  const el = doc.createElement("div");
805
848
  el.className = "tab";
806
849
  el.setAttribute("role", "tab");
807
850
  el.setAttribute("data-active", String(tab.active));
851
+ el.setAttribute("data-group", tab.group);
808
852
  el.setAttribute("aria-selected", String(tab.active));
809
853
  el.appendChild(favOf(tab));
810
854
  const label = doc.createElement("button");
@@ -812,8 +856,7 @@
812
856
  label.className = "label";
813
857
  label.textContent = tabName(tab);
814
858
  label.title = tabName(tab);
815
- label.disabled = watchOnly;
816
- label.addEventListener("click", () => send({ t: "tab", action: "switch", tabId: tab.id }));
859
+ label.addEventListener("click", () => pickTab(tab));
817
860
  el.appendChild(label);
818
861
  if (!watchOnly && tab.active && control === "user") {
819
862
  const close = doc.createElement("button");
@@ -835,12 +878,38 @@
835
878
  add.addEventListener("click", () => send({ t: "tab", action: "open" }));
836
879
  box.appendChild(add);
837
880
  }
881
+ const others = $("others");
882
+ others.hidden = otherTabs.length === 0;
883
+ others.textContent = othersLabel();
838
884
  const active = tabs.find((x) => x.active);
839
885
  $("tabsbtn").querySelector(".fav")?.replaceWith(favOf(active));
840
886
  $("tabsbtn-name").textContent = active === void 0 ? "" : hostOf(active.url) || tabName(active);
841
887
  $("tabsbtn-count").textContent = String(tabs.length);
842
888
  $("tabsbtn").setAttribute("aria-label", `${text("tabs")}: ${tabs.length}`);
843
889
  }
890
+ function pickTab(tab) {
891
+ if (interactive()) send({ t: "tab", action: "switch", tabId: tab.id });
892
+ else if (!tab.active) send({ t: "show", tabId: tab.id });
893
+ }
894
+ const subGroups = () => [
895
+ ...new Set(tabs.map((x) => x.group).filter((g) => g !== "own"))
896
+ ];
897
+ const groupLabel = (group, subs) => {
898
+ if (subs.length <= 1) return text("subTask");
899
+ const n = /^sub(\d+)$/.exec(group)?.[1];
900
+ return n === void 0 ? text("subTask") : `${text("subTask")} ${n}`;
901
+ };
902
+ const othersLabel = () => `${text("otherConversations")} (${otherTabs.length})`;
903
+ function otherItems() {
904
+ return otherTabs.map((tab) => ({
905
+ label: tab.title !== "" ? tab.title : tab.url === "about:blank" ? text("newTab") : tab.url,
906
+ hint: text("moveHere"),
907
+ run: () => send({ t: "tab", action: "adopt", tabId: tab.id })
908
+ }));
909
+ }
910
+ function openOthers(anchor) {
911
+ openMenu(anchor, [{ label: othersLabel(), heading: true }, ...otherItems()]);
912
+ }
844
913
  const tabName = (tab) => tab.title !== "" ? tab.title : tab.url === "about:blank" ? text("newTab") : tab.url;
845
914
  function favOf(tab) {
846
915
  if (tab !== void 0 && isIconDataUrl(tab.favicon)) {
@@ -880,35 +949,59 @@
880
949
  const menu = $("menu");
881
950
  menu.textContent = "";
882
951
  for (const it of items) {
952
+ if (it.heading === true || it.run === void 0) {
953
+ const h = doc.createElement("p");
954
+ h.className = "heading";
955
+ h.setAttribute("role", "presentation");
956
+ h.textContent = it.label;
957
+ menu.appendChild(h);
958
+ continue;
959
+ }
960
+ const run = it.run;
883
961
  const b = doc.createElement("button");
884
962
  b.type = "button";
885
963
  b.setAttribute("role", "menuitem");
886
- b.textContent = it.label;
964
+ const name = doc.createElement("span");
965
+ name.className = "name";
966
+ name.textContent = it.label;
967
+ b.appendChild(name);
968
+ if (it.hint !== void 0) {
969
+ const hint = doc.createElement("span");
970
+ hint.className = "hint";
971
+ hint.textContent = it.hint;
972
+ b.appendChild(hint);
973
+ }
887
974
  if (it.current) b.setAttribute("aria-current", "true");
888
975
  b.addEventListener("click", () => {
889
976
  closeMenu();
890
- it.run();
977
+ run();
891
978
  });
892
979
  menu.appendChild(b);
893
980
  }
894
981
  const r = anchor.getBoundingClientRect();
895
982
  menu.style.top = `${Math.round(r.bottom + 4)}px`;
896
- menu.style.right = `${Math.max(12, Math.round(win.innerWidth - r.right))}px`;
983
+ menu.style.maxWidth = `${Math.max(0, win.innerWidth - 24)}px`;
984
+ menu.style.right = "12px";
897
985
  menu.hidden = false;
986
+ const width = menu.getBoundingClientRect().width;
987
+ const right = Math.max(12, Math.round(win.innerWidth - r.right));
988
+ menu.style.right = `${Math.max(12, Math.min(right, Math.round(win.innerWidth - 12 - width)))}px`;
898
989
  anchor.setAttribute("aria-expanded", "true");
899
- menu.firstElementChild?.focus();
990
+ menu.querySelector("button")?.focus();
900
991
  }
901
992
  function closeMenu() {
902
993
  $("menu").hidden = true;
903
- $("tabsbtn").removeAttribute("aria-expanded");
904
- $("more").removeAttribute("aria-expanded");
994
+ for (const id of ["tabsbtn", "more", "others", "empty-others"])
995
+ $(id).removeAttribute("aria-expanded");
905
996
  }
906
997
  function applyState(s) {
907
998
  lastState = s;
908
- pendingHandoff = s.handoff ?? null;
999
+ pendingHandoff = s.handoffs.at(-1) ?? null;
909
1000
  $("handoff").hidden = pendingHandoff === null;
910
1001
  $("handoff-message").textContent = pendingHandoff?.message ?? "";
911
1002
  tabs = s.tabs;
1003
+ otherTabs = s.otherTabs;
1004
+ if (tabs.length === 0) image = null;
912
1005
  const was = control;
913
1006
  control = s.control;
914
1007
  if (full && was === "user" && control === "agent") askMode(false);
@@ -1016,13 +1109,15 @@
1016
1109
  ctx?.drawImage(image, 0, 0, w, h);
1017
1110
  }
1018
1111
  async function onFrame(m) {
1019
- try {
1020
- const first = image === null;
1021
- image = await decode(m.jpeg);
1022
- if (first) renderLoading();
1023
- draw();
1024
- input.frame();
1025
- } catch {
1112
+ if (!treeEmpty()) {
1113
+ try {
1114
+ const first = image === null;
1115
+ image = await decode(m.jpeg);
1116
+ if (first) renderLoading();
1117
+ draw();
1118
+ input.frame();
1119
+ } catch {
1120
+ }
1026
1121
  }
1027
1122
  send({ t: "frame-ack", seq: m.seq });
1028
1123
  }
@@ -1143,6 +1238,10 @@
1143
1238
  case "dialog":
1144
1239
  showDialog(msg);
1145
1240
  break;
1241
+ case "notice":
1242
+ if (msg.code === "tab_in_use") notice("tabInUse", "");
1243
+ else if (msg.code === "tab_cap") notice("tabCap", "");
1244
+ break;
1146
1245
  }
1147
1246
  }
1148
1247
  function onClose(code) {
@@ -1230,11 +1329,13 @@
1230
1329
  const res = await doFetch("handoff-answer", {
1231
1330
  method: "POST",
1232
1331
  headers: { authorization: `Bearer ${token}`, "content-type": "application/json" },
1233
- body: JSON.stringify({ callId: h.callId, done }),
1332
+ // the handoff's own conversation (a sub-agent's when it asked): the kernel looks the
1333
+ // question up by it and the callId, which is unique only within its run
1334
+ body: JSON.stringify({ callId: h.callId, done, handoffConversationId: h.conversationId }),
1234
1335
  credentials: "omit"
1235
1336
  });
1236
1337
  if (res.status === 404) {
1237
- if (pendingHandoff?.callId === h.callId) {
1338
+ if (pendingHandoff?.callId === h.callId && pendingHandoff.conversationId === h.conversationId) {
1238
1339
  pendingHandoff = null;
1239
1340
  $("handoff").hidden = true;
1240
1341
  }
@@ -1252,16 +1353,23 @@
1252
1353
  $("takeover").addEventListener("click", takeover);
1253
1354
  $("verify-go").addEventListener("click", takeover);
1254
1355
  $("tabsbtn").addEventListener("click", (e) => {
1255
- if (!interactive()) return;
1256
- openMenu(e.currentTarget, [
1257
- ...tabs.map((tab) => ({
1258
- label: tabName(tab),
1259
- current: tab.active,
1260
- run: () => send({ t: "tab", action: "switch", tabId: tab.id })
1261
- })),
1262
- { label: text("newTab"), run: () => send({ t: "tab", action: "open" }) }
1263
- ]);
1356
+ const items = [];
1357
+ const subs = subGroups();
1358
+ let group = "own";
1359
+ for (const tab of tabs) {
1360
+ if (tab.group !== group) {
1361
+ group = tab.group;
1362
+ if (group !== "own") items.push({ label: groupLabel(group, subs), heading: true });
1363
+ }
1364
+ items.push({ label: tabName(tab), current: tab.active, run: () => pickTab(tab) });
1365
+ }
1366
+ if (interactive())
1367
+ items.push({ label: text("newTab"), run: () => send({ t: "tab", action: "open" }) });
1368
+ if (otherTabs.length > 0) items.push({ label: othersLabel(), heading: true }, ...otherItems());
1369
+ if (items.length > 0) openMenu(e.currentTarget, items);
1264
1370
  });
1371
+ $("others").addEventListener("click", (e) => openOthers(e.currentTarget));
1372
+ $("empty-others").addEventListener("click", (e) => openOthers(e.currentTarget));
1265
1373
  $("more").addEventListener("click", (e) => {
1266
1374
  openMenu(e.currentTarget, [
1267
1375
  { label: text("forward"), run: () => send({ t: "nav", action: "forward" }) },
@@ -1271,7 +1379,7 @@
1271
1379
  doc.addEventListener("mousedown", (e) => {
1272
1380
  const m = $("menu");
1273
1381
  if (m.hidden || m.contains(e.target)) return;
1274
- if (e.target?.closest?.("#tabsbtn, #more")) return;
1382
+ if (e.target?.closest?.("#tabsbtn, #more, #others, #empty-others")) return;
1275
1383
  closeMenu();
1276
1384
  });
1277
1385
  $("handback").addEventListener("click", handback);