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

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.6",
4
+ "version": "0.1.0-rc.7",
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.6",
20
+ "version": "0.1.0-rc.7",
21
21
  "sdk": "^0.1.0-rc.69",
22
22
  "provides": [],
23
23
  "requires": [],
package/README.md CHANGED
@@ -28,8 +28,10 @@ A real browser in the person's device for agents (browser use spec,
28
28
  `reportBlockedHosts` (the kernel raises the network ask and reruns the call once allowed).
29
29
  - `browser_handoff` sends the daemon a handoff notice (the panels show a banner with 完成 / 取消),
30
30
  then raises an `InputRequired` question `{ done: boolean }` in the conversation, with `open`
31
- naming the `browser` panel so the person's conversation page opens it (a boolean, not
32
- a 完成 / 取消 enum: the kernel's `answerHandoff`, which the banner uses, answers `{ done }`); on
31
+ naming the `browser` panel so the person's conversation page opens it. The question's title is
32
+ 请在浏览器中完成:<the reason's first sentence> / Finish in the browser: …, the body is the full
33
+ reason, and `choices` draw 完成 (true, primary) and 取消 (false) as buttons. It stays a boolean
34
+ because the kernel's `answerHandoff`, which the banner uses, answers `{ done }`. On
33
35
  resume it clears the notice (with Done also handing the browser back, so a takeover the person
34
36
  still holds ends) and returns the page snapshot, or fails with "The user cancelled the handoff;
35
37
  ask the user how to continue, or try another approach.".
package/dist/index.mjs CHANGED
@@ -207,16 +207,22 @@ function toResult(reply) {
207
207
 
208
208
  // src/i18n.ts
209
209
  var HANDOFF_FORM = {
210
+ titlePrefix: { zh: "请在浏览器中完成:", en: "Finish in the browser: " },
210
211
  label: { zh: "完成", en: "Done" },
211
- description: {
212
- zh: "完成后打开开关并提交,浏览器交还智能体;关闭开关提交或关闭此问题即为取消。",
213
- en: "Turn on and submit when you are done to hand the browser back; submit with it off, or dismiss the question, to cancel."
214
- }
212
+ done: { zh: "完成", en: "Done" },
213
+ cancel: { zh: "取消", en: "Cancel" }
215
214
  };
216
215
 
217
216
  // src/handoff.ts
218
217
  var PANEL = "browser";
219
218
  var MESSAGE_MAX = Math.min(2e3, HANDOFF_MAX_CHARS);
219
+ var SHORT_MAX = 40;
220
+ function shortReason(message) {
221
+ const first = message.trim().split(/(?<=[。!?.!?])\s*|\n/u)[0]?.trim() ?? "";
222
+ const chars = Array.from(first);
223
+ if (chars.length <= SHORT_MAX) return first.replace(/[。.]$/u, "");
224
+ return `${chars.slice(0, SHORT_MAX - 1).join("")}…`;
225
+ }
220
226
  var HANDOFF = {
221
227
  name: "browser_handoff",
222
228
  title: "Hand the browser to the user",
@@ -248,14 +254,22 @@ function handoffHandler(deps) {
248
254
  const asked = {
249
255
  kind: "input_required",
250
256
  form: {
251
- title: message,
257
+ title: {
258
+ zh: `${HANDOFF_FORM.titlePrefix.zh}${shortReason(message)}`,
259
+ en: `${HANDOFF_FORM.titlePrefix.en}${shortReason(message)}`
260
+ },
261
+ body: message,
252
262
  fields: [
253
263
  {
254
264
  key: "done",
255
265
  label: HANDOFF_FORM.label,
256
- description: HANDOFF_FORM.description,
257
266
  schema: { type: "boolean" },
258
- required: true
267
+ required: true,
268
+ // two buttons (完成 primary, 取消), the same answer the panel's banner sends
269
+ choices: [
270
+ { value: true, label: HANDOFF_FORM.done },
271
+ { value: false, label: HANDOFF_FORM.cancel }
272
+ ]
259
273
  }
260
274
  ]
261
275
  },
@@ -580,9 +594,19 @@ function registerRoutes(ctx, deps) {
580
594
  const body = await readJson(req);
581
595
  const callId = body?.callId;
582
596
  const done = body?.done;
583
- if (typeof callId !== "string" || callId === "" || callId.length > CALL_ID_MAX || typeof done !== "boolean")
584
- return error(400, "invalid_body", "the body must be { callId: string, done: boolean }");
585
- await deps.deviceAccess.answerHandoff({ ...scope, callId, done });
597
+ const raised = body?.handoffConversationId;
598
+ if (typeof callId !== "string" || callId === "" || callId.length > CALL_ID_MAX || typeof done !== "boolean" || raised !== void 0 && (typeof raised !== "string" || raised === "" || raised.length > CALL_ID_MAX))
599
+ return error(
600
+ 400,
601
+ "invalid_body",
602
+ "the body must be { callId: string, done: boolean, handoffConversationId?: string }"
603
+ );
604
+ await deps.deviceAccess.answerHandoff({
605
+ ...scope,
606
+ callId,
607
+ handoffConversationId: typeof raised === "string" ? raised : scope.conversationId,
608
+ done
609
+ });
586
610
  return json(200, { ok: true });
587
611
  })
588
612
  );
@@ -1065,7 +1089,7 @@ function registerBrowserTools(ctx, deps) {
1065
1089
  // src/index.ts
1066
1090
  var index_default = defineExtension({
1067
1091
  name: "browser",
1068
- version: "0.1.0-rc.6",
1092
+ version: "0.1.0-rc.7",
1069
1093
  sdk: "^0.1.0-rc.69",
1070
1094
  provides: [],
1071
1095
  ui: [
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capybara-hq/plugin-browser",
3
- "version": "0.1.0-rc.6",
3
+ "version": "0.1.0-rc.7",
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",
package/ui/panel.js CHANGED
@@ -841,7 +841,7 @@
841
841
  $("tabsbtn-count").textContent = String(tabs.length);
842
842
  $("tabsbtn").setAttribute("aria-label", `${text("tabs")}: ${tabs.length}`);
843
843
  }
844
- const tabName = (tab) => tab.title === "" ? tab.url : tab.title;
844
+ const tabName = (tab) => tab.title !== "" ? tab.title : tab.url === "about:blank" ? text("newTab") : tab.url;
845
845
  function favOf(tab) {
846
846
  if (tab !== void 0 && isIconDataUrl(tab.favicon)) {
847
847
  const img = doc.createElement("img");
@@ -1,6 +1,6 @@
1
1
  // @capybara-hq/plugin-browser daemon (generated by daemon/build.ts)
2
2
 
3
- // ../../node_modules/.pnpm/@capybara-hq+plugin-sdk@0.1.0-rc.74/node_modules/@capybara-hq/plugin-sdk/dist/device-service.js
3
+ // ../../node_modules/.pnpm/@capybara-hq+plugin-sdk@0.1.0-rc.75/node_modules/@capybara-hq/plugin-sdk/dist/device-service.js
4
4
  var DEVICE_SERVICE_SOCKET_ENV = "CAPYBARA_SERVICE_SOCKET";
5
5
  var DEVICE_SERVICE_STATE_ENV = "CAPYBARA_SERVICE_STATE";
6
6
  var DEVICE_SERVICE_MACH_ENV = "CAPYBARA_SERVICE_MACH";
@@ -818,6 +818,8 @@ var BrowserSession = class {
818
818
  nextId = 1;
819
819
  active;
820
820
  queue = Promise.resolve();
821
+ /** The blank tab {@link activePage} is opening because none was left. */
822
+ opening;
821
823
  /** The agent's latest click; `seq` grows with every click (viewers flash only new ones). */
822
824
  lastClick;
823
825
  clickSeq = 0;
@@ -854,19 +856,34 @@ var BrowserSession = class {
854
856
  this.listeners.add(fn);
855
857
  return () => this.listeners.delete(fn);
856
858
  }
857
- /** The active tab, opening a blank one when none is left. */
859
+ /**
860
+ * The active tab, opening a blank one when none is left. Callers that find no tab at the same
861
+ * time (two quick closes of the last tabs, a viewer's frame and a request) share that one
862
+ * opening, so the last close leaves exactly one blank tab.
863
+ */
858
864
  async activePage() {
859
865
  const cur = this.active === void 0 ? void 0 : this.pages.get(this.active);
860
866
  if (cur !== void 0 && !cur.isClosed()) return cur;
861
- const first = [...this.pages.values()].find((p2) => !p2.isClosed());
867
+ const first = [...this.pages.values()].find((p) => !p.isClosed());
862
868
  if (first !== void 0) {
863
869
  this.active = this.ids.get(first);
864
870
  return first;
865
871
  }
866
- const p = await this.ctx.newPage();
867
- this.track(p);
868
- this.active = this.ids.get(p);
869
- return p;
872
+ this.opening ??= this.ctx.newPage().then(
873
+ (p) => {
874
+ this.opening = void 0;
875
+ this.track(p);
876
+ this.active = this.ids.get(p);
877
+ this.emit({ type: "active" });
878
+ this.emit({ type: "state" });
879
+ return p;
880
+ },
881
+ (e) => {
882
+ this.opening = void 0;
883
+ throw e;
884
+ }
885
+ );
886
+ return this.opening;
870
887
  }
871
888
  tabIdOf(page) {
872
889
  return this.ids.get(page) ?? "";