@seekskyworld/open-browser 0.1.0 → 0.3.0
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/Docs/open-browser-api.d.ts +32 -10
- package/Docs//350/267/250/345/271/263/345/217/260/350/257/225/347/224/250/344/270/216/345/217/221/350/241/214.md +51 -3
- package/README.md +14 -5
- package/README.zh-CN.md +16 -5
- package/access-registry.mjs +13 -3
- package/action-execution.mjs +60 -24
- package/api-contract.mjs +19 -7
- package/browser-extension/manifest.json +1 -4
- package/browser-extension/newtab.css +6 -0
- package/browser-extension/newtab.js +54 -2
- package/browser-extension/phase-labels.js +1 -1
- package/browser-extension/popup.css +1 -1
- package/browser-extension/popup.html +16 -4
- package/browser-extension/popup.js +122 -21
- package/browser-extension/tab-action.js +4 -2
- package/browser-extension/worker.js +102 -9
- package/build-profile.mjs +23 -0
- package/capability-matrix.mjs +4 -4
- package/chromium-host.mjs +81 -25
- package/chromium-shell.mjs +78 -49
- package/cli.mjs +63 -5
- package/contract-validation.mjs +1 -1
- package/desktop/Info.plist +2 -2
- package/desktop/OBControlBar.m +7 -3
- package/desktop/WorkspacePanel.m +4 -2
- package/desktop/brand-keychain.py +20 -4
- package/desktop/build-macos.mjs +31 -12
- package/desktop/build-managed-input.mjs +37 -0
- package/desktop/build-profile.mjs +6 -0
- package/desktop/build.mjs +8 -0
- package/desktop/macos-entitlements.plist +12 -0
- package/desktop/notarize-macos.mjs +189 -0
- package/desktop/package-macos.mjs +54 -0
- package/desktop/patches/chromium-managed-input.patch +144 -0
- package/desktop/sign.mjs +63 -4
- package/desktop/start-profile.mjs +5 -0
- package/desktop/start.mjs +25 -3
- package/desktop/verify-macos-release.mjs +69 -0
- package/desktop/verify-version.mjs +22 -2
- package/diagnostics.mjs +9 -0
- package/engine-service.mjs +244 -29
- package/managed-input.mjs +155 -0
- package/mcp-http.mjs +84 -0
- package/mcp-server.mjs +57 -0
- package/mcp.mjs +3 -40
- package/package.json +24 -3
- package/page-activity.mjs +97 -29
- package/page-observation.mjs +44 -3
- package/paths.mjs +5 -3
- package/receipt-store.mjs +24 -7
- package/runtime.mjs +10 -1
- package/sdk.mjs +53 -26
- package/service.mjs +45 -10
- package/skill-links.mjs +144 -0
- package/skills/open-browser/SKILL.md +8 -76
- package/skills/open-browser/references/macos.md +3 -0
- package/skills/open-browser/references/operations.md +59 -0
- package/skills/open-browser/references/recovery.md +36 -0
- package/skills/open-browser/references/windows.md +3 -0
- package/skills/open-browser/scripts/bootstrap.mjs +112 -45
- package/skills/open-browser/scripts/guide.mjs +17 -0
- package/studio/studio.js +2 -2
- package/tab-adoption.mjs +83 -0
- package/tab-recovery.mjs +100 -0
- package/uploads.mjs +4 -4
- package/visual-page.mjs +17 -7
- package/workspace-lifecycle.mjs +92 -8
- package/workspace-state.mjs +65 -12
|
@@ -3,84 +3,16 @@ name: open-browser
|
|
|
3
3
|
description: 用户要求用 open-browser 浏览、观察页面、操作工作区或验证登录时,通过 open-browser CLI 控制其独立 Chromium。
|
|
4
4
|
---
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
## 从当前任务开始
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
1. **继续已有页面。** 用 `getWorkspace`(或 `listWorkspaces` 找回任务)取得页面句柄,用 `reattachPage` 读当前观察。普通已登录标签需要一个命名的空工作区。创建后先保存 `eventCursor.nextCursor`,请用户在目标标签的扩展中选择任务并点击「让 Agent 操作此页」。用 `waitForEvent` 传 `workspaceId`、`kind:"page_bound"`、`after`,再用事件的 `pageId` 调用 `reattachPage`。等待超时可沿用原游标。
|
|
9
|
+
2. **新开页面。** 用 `open` 传明确的任务 `name` 和 `url`,保存返回的工作区与页面 ID,再 `observe`。在已有任务中增加页面用 `openPage`。并行 Agent 可以传各自的 `agentName`。
|
|
10
|
+
3. **人工交回。** 用户处理登录或弹窗时用 `handOff`。用原句柄调用 `reattachPage` 并传 `waitMs:30000`,用户点击「交回 Agent」后直接使用返回的观察。工作区的 `mode` 表示控制权,页面的 `readyForAgent` 表示当前是否可操作。
|
|
9
11
|
|
|
10
|
-
|
|
12
|
+
## 执行动作
|
|
11
13
|
|
|
12
|
-
|
|
13
|
-
ob="$PWD/skills/open-browser/scripts/open-browser"
|
|
14
|
-
page=$(printf '%s\n' '{"url":"https://example.org","name":"页面检查"}' | "$ob" open)
|
|
15
|
-
printf '%s\n' "$page"
|
|
16
|
-
# 从上一个 JSON 结果取 workspaceId 和 pageId,再执行 observe
|
|
17
|
-
printf '%s\n' '{"workspaceId":"WORKSPACE_ID","pageId":"PAGE_ID"}' | "$ob" observe
|
|
18
|
-
```
|
|
14
|
+
macOS 和 Linux 使用本 Skill 的 `scripts/open-browser`,Windows 使用 `scripts/open-browser.cmd`。首次调用会探测本机服务并按需启动应用。连续任务优先复用 SDK 客户端或运行启动器的 `session` 命令,每行发送 `{"id":1,"method":"…","params":{…}}`。
|
|
19
15
|
|
|
20
|
-
|
|
16
|
+
用最新观察的控件 ref 调用 `observeAndAct`,传唯一 `operationId`、`actions`、`observeAfter:"delta"` 和 `waitMs:3000`。完成时直接读取回执的变化。回执仍为 queued 或 running 时,用同一个操作号调用 `getOperation` 并传 `waitMs`(最多 10000 毫秒)。`outcome_unknown` 先核对站点结果。密码使用工作区绑定的 `credentialRef`。任务完成前调用 `finishWorkspace`,通过 `keepPageIds` 指定交付页面。
|
|
21
17
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
若只使用 SDK,可用 `openAndObserve` 省去首次句柄拼接,用 `actAfterObserve` 自动以最新文档和控制代次执行一批动作,用 `finish` 自动生成交付操作号。这些辅助方法仍执行服务端的工作区权限、来源和过期引用检查。
|
|
25
|
-
|
|
26
|
-
1. 运行 `"$ob" listWorkspaces </dev/null`。继续已有任务时使用该任务的工作区。新任务通过 `createWorkspace` 创建,必须填写清晰的 `name` 任务名称,并保存返回的 `workspaceId`。`name` 填任务名称,`agentName` 填创建者的显示名(最多 80 个 Unicode 码点)。多个子代理并行时,每个子代理须显式传自己的名字。同一客户端的子代理通常共享环境或 MCP 握手,只能由子代理自身区分身份。
|
|
27
|
-
2. 调用 `openPage`,传入 `workspaceId` 和目标 `url`,保存返回的页面句柄。把完整句柄传给 `observe`,读取可见文本、元素的 `role/name/ref`、`documentId` 和 `controlGeneration`。
|
|
28
|
-
3. 调用 `act`,把页面句柄、刚取得的两个代次标识、唯一 `operationId` 和有序的 `actions` 放入同一个 JSON 对象。例如动作可为 `{"kind":"fill","ref":"实际输入框引用","value":"待填写的普通文本"}` 和 `{"kind":"click","ref":"实际按钮引用"}`。相同控件在连续观察中保留引用。节点被移除或替换后,收到 `stale_ref` 时重新观察并核对目标。可见标签返回 `role:"label"` 时用 `click` 勾选关联控件。密码使用 `fillCredential` 和当前工作区、站点已绑定的 `credentialRef`。
|
|
29
|
-
4. 对 `queued` 或 `running` 回执,使用同一个 `operationId` 和 `workspaceId` 调用 `getOperation`,直至终态。再次 `observe` 核对页面业务结果。若结果为 `outcome_unknown`,先核对页面;导航或用户交回控制权后,重新观察再构造新动作。
|
|
30
|
-
5. 已有会话或凭据可用时自动继续;缺少凭据时只提示缺少的引用,用户在浏览器工具栏的 **open-browser 工作区** 扩展中核对站点并点 **绑定凭据** 后继续。需要用户操作时用 `handOff`。在 macOS 桌面应用中,用户可点击受管页面底部的「人工接管」和「交回 Agent」。工具栏也可切换控制权;缺凭据时可从弹出的面板接管。接管覆盖整个工作区,用户点 **交回 Agent** 后重新观察。整项任务完成后,Agent 在向用户报告前调用 `finishWorkspace`,将结果页放入 `keepPageIds`;没有要交付的页面时传空数组。一次动作完成后仍需继续任务时沿用原工作区。
|
|
31
|
-
|
|
32
|
-
macOS 桌面版把任务页面放进 Chromium 原生标签组。新建工作区仍共享应用专用 profile 内同站点的登录态,判断当前账号时请以页面实际显示为准。仅操作任务返回的页面句柄。登录流程打开新窗口时,从工作区页面清单按 openerPageId 和 openedByOperationId 找到对应子页。脚本工作线程受受管页策略约束。
|
|
33
|
-
|
|
34
|
-
省略 `agentName` 时可通过 `OPEN_BROWSER_AGENT_NAME` 配置默认名称。MCP 优先读取握手中的客户端标题,再读取名称。CLI 和 SDK 在已知 Codex 会话环境下显示 Codex,其他情况下分别显示 CLI 和 SDK。显示名称用于识别工作区创建者,访问权限由本机服务验证。
|
|
35
|
-
|
|
36
|
-
在项目根目录创建试用工作区可运行以下命令。
|
|
37
|
-
|
|
38
|
-
```sh
|
|
39
|
-
ob="$PWD/skills/open-browser/scripts/open-browser"
|
|
40
|
-
printf '%s\n' '{"name":"CLI 试用","agentName":"页面核验助手"}' | "$ob" createWorkspace
|
|
41
|
-
```
|
|
42
|
-
|
|
43
|
-
需要同时使用两个账号时,先给服务和调用端设置相同的 `OPEN_BROWSER_PROFILE`。每个 profile 使用自己的 Chromium 登录资料、本机服务和回执;同一 profile 的工作区仍共享站点账号。已配对的客户端通过 `OPEN_BROWSER_CLIENT_TOKEN_FILE` 选择自己的令牌。管理者可从本机 CLI 签发、委托和撤销权限,详见仓库 README。
|
|
44
|
-
|
|
45
|
-
- `act` 支持 `select` 和 `wait`。选择项使用最近观察中 combobox 的 ref 和选项 value。等待条件任选可见文本 `text`、完整页面地址 `url` 或可见元素 `element:{role,name}`,动作等待的 `timeoutMs` 为 1 到 10000。动作请求还可传顶层 `timeoutMs`,它是覆盖排队和执行的 1 到 60000 毫秒 wall budget。提交后等结果页的条件可与点击放在一批。用操作回执和网站结果共同核对业务完成。
|
|
46
|
-
- 控件可返回 `checked`、`pressed`、`expanded`、`selected`。其中 `checked` 和 `pressed` 可为 `"mixed"`。用 `{"kind":"setChecked","ref":"…","checked":true}` 要求目标状态,成功后从观察或 `delta.changed` 读回。
|
|
47
|
-
- 精确寻找主页面按钮时,传 `observe.locator`,例如 `{"role":"button","name":"Save","within":{"role":"region","name":"Order B"}}`。仅 `match.status:"unique"` 返回可操作引用。重名时查看 `match.candidates` 的容器再缩小范围。存在模态对话框时,`foreground` 给出标题及边界,可操作列表只包含前景内容。
|
|
48
|
-
- 在同批点击后用 `{"kind":"wait","navigation":"commit","timeoutMs":3000}`,从回执 `navigation` 区分 `document_committed`、`same_document` 和超时的 `not_committed`。再用 `wait.element` 指定 `checked` 等期望状态,或用 `wait.absent:{role,name}` 等待可操作控件消失。已派发的提交发生超时时,先核对网站结果。
|
|
49
|
-
- `observeAfter:"full"` 返回当次操作后的观察,`"delta"` 返回变化的文字和控件。文档或基线更换时可收到全量观察;旧代次、历史回执或页面已变更时,请再次观察。`includeFrames:true` 用于读取已授权的可见一级 frame 文本。需要操作嵌入控件时,传 `frameControls:true`,使用返回的 `frames[].elements[].ref`。框架重新加载后重新观察。
|
|
50
|
-
- 交付调用 `finishWorkspace`,传 `workspaceId`、唯一 `operationId` 及 `keepPageIds` 数组。保留的页面脱离任务,未选中的受管页关闭。重试沿用同一操作 ID 和同一页集合;收到 `outcome_unknown` 时先核实页面。用户也可从工作区面板选择保留页。新打开的普通标签无需清理。
|
|
51
|
-
- `listOperations` 查询结束的工作区回执;`listEvents` 使用 `after`、`limit` 续读。事件序号从响应的 `nextCursor` 取得。默认保留 7 天。使用随机操作 ID,避免把账号、URL 或正文写入 ID。
|
|
52
|
-
|
|
53
|
-
画布、拖拽或触摸操作先运行 `capabilities`。具有页面像素权限时,调用 `captureVisual`,传页面句柄及可选的 `region:{x,y,width,height}` 或最新元素 `ref`。PNG 的左上角对应返回的 `clip.x,clip.y`,坐标均为视口 CSS 像素。返回的 `frameId` 有效 8 秒,可用一次。通过 `act` 传同一帧的文档和控制代次,以及 `{"kind":"spatial","frameId":"<帧 ID>","action":{"type":"click","x":50,"y":50}}`。可用类型包括 click、hover、tap、drag(toX/toY)、scroll(deltaX/deltaY)、key(keys)和 text(text)。表单控件及私有区域已遮盖,登录继续使用语义引用。遇到 `stale_visual_frame` 时重新截图;若操作回执为 `outcome_unknown`,先核对业务结果。像素权限由本机管理员通过 `admin.setPixelPermission` 授予到指定工作区、调用方及来源。
|
|
54
|
-
|
|
55
|
-
### 长页面、弹窗和下载
|
|
56
|
-
|
|
57
|
-
- `observe` 返回 `truncated:true` 和 `nextCursor` 时,把页面句柄与 `cursor:nextCursor` 一起传给下一次 `observe`。知道目标名称时可直接传 `name` 做子串匹配。也可以传 `region:{x,y,width,height}` 按页面视口 CSS 像素选区。每批控件数量可用 `limit` 指定,取值 1 到 180。收到 `stale_cursor` 时重新观察。
|
|
58
|
-
- 点击持续被遮罩挡住会以 `target_obscured` 结束。读取最新页面、等待遮罩消失后,用新的操作号点击。收到 `inspect_before_retry` 时,先查站点提交结果再决定下一步。
|
|
59
|
-
- 登录流程从一次受管点击打开获准的 HTTP(S) 地址后,读操作回执的 `openedPageIds`。延迟开窗时也可通过 `getWorkspace` 找到发起这次操作的子页,以其句柄继续登录。
|
|
60
|
-
- 下载发生后用 `listDownloads` 查询当前工作区,通过 `downloadId` 区分同名文件。`cancelDownload` 需要工作区控制权限。用户在工作区面板保留所需文件后再交付任务,查看文件时以面板显示的路径为准。
|
|
61
|
-
- 用户保留文件后,`listOutputs` 返回本地 manifest。Agent 可用 `readOutput` 按 `offset` 和 `limit`(最多 64 KiB)读取 base64 分块,使用 `nextOffset` 续读。输出默认只保留 24 小时,凭据和页面正文不会写入 manifest。
|
|
62
|
-
|
|
63
|
-
原生对话框出现时,读取 `getWorkspace.pendingDialogs`,按页面、操作号、来源及对话框类型核实用途。用 `respondDialog` 传工作区、页面、dialogId、controlGeneration、accept,prompt 可另传 text。每个对话框只回应一次。用户可以在工作区扩展接管并处理。Playwright 无法标识对话框的发起 frame,多来源的 frame 树按取消处理。
|
|
64
|
-
|
|
65
|
-
上传时,用户在工作区扩展选定页面、核对来源并选择文件。调用 `listUploads` 取得该工作区的 fileId,再发送 `act` 动作 `{"kind":"upload","ref":"最近观察中的文件选择按钮","fileId":"用户授权的 ID"}`。单文件至多 8 MiB,只在当前页面、文档、来源和控制代次有效,五分钟失效。发给站点后即消耗授权。结果待核实时应先检查站点。
|
|
66
|
-
|
|
67
|
-
点击前以 `eventCursor` 保存工作区 nextCursor,触发后调用 `waitForEvent`,传同一页面句柄、after、kind(popup / download / dialog)、可选 operationId 和 timeoutMs(1–10000)。也可先发起等待再点击。超时返回 event:null,可从原游标继续;收到事件后使用 nextCursor。resourceId 对应子页、下载或对话框 ID。异步下载只能可靠归到页面时,省略 operationId 过滤。只读委托可查看事件,响应对话框和上传仍需控制权。
|
|
68
|
-
|
|
69
|
-
- 中断后用 `getRecoverySummary` 传工作区 ID。对 `observe` 标记的页重新观察,`handle_unavailable` 表示旧句柄已失效。对 `verify_site` 先核对网站结果,`already_completed` 表示已记录完成。交付回执的 `keptPageIds` 记录已移交的标签,`deliveries` 列出已确认保留的下载 ID,`outputs` 列出可续读的本地输出 ID。回执与资料分页分别使用 `nextCursor → after` 和 `nextObjectCursor → objectsAfter`。
|
|
70
|
-
|
|
71
|
-
- 创建工作区可传 `profileId`,服务核对它与已连接账号资料一致,返回并在接管面板显示资料名。多个账号分别启动对应的 `OPEN_BROWSER_PROFILE` 实例。CLI、MCP 通过同名环境配置连接,SDK 可用 `new OpenBrowserClient({profileId:'work'})` 选择本机已启动的命名实例。
|
|
72
|
-
|
|
73
|
-
### 按网站运行流程
|
|
74
|
-
|
|
75
|
-
将 [登录后导出](workflows/login-export.json) 或 [上传后核对](workflows/upload-verify.json) 保存为本地模板,按实际站点填写精确的 `origin`、控件角色、名称和成功条件。调用 `runWorkflow` 时传页面句柄、最新 `documentId`、`controlGeneration`、唯一 `operationId`、模板对象 `workflow`,以及本次运行的 `bindings`。例如 `{"attachment":"已授权 fileId"}` 或 `{"password":"已绑定 credentialRef"}`。模板仅保存参数名,每次运行时提供授权引用。服务端逐步重新观察、核对来源和控制权。
|
|
76
|
-
|
|
77
|
-
用 `getOperation` 查看已完成步数。遇到 `target_ambiguous`、`target_missing` 或 `workflow_origin_changed` 时核对页面和已执行步骤。登录后导出还应读取当前工作区的下载回执,核对交付物。
|
|
78
|
-
|
|
79
|
-
- 使用 `capabilities` 确认服务提供的方法和限额,`--schema` 输出当前 CLI 的全部方法和参数。遇到不支持的方法时以服务端返回的能力为准。
|
|
80
|
-
- `diagnoseOperation` 接受 `workspaceId` 和 `operationId`,返回已完成步数、是否记录过派发、故障类别和建议处置。桌面工作区面板也可预览并保存同一份脱敏 JSON。结果为 `check_site_result` 时先用页面的业务结果核实是否生效。
|
|
81
|
-
|
|
82
|
-
单页快捷入口
|
|
83
|
-
|
|
84
|
-
只需打开一个明确网址时,可直接调用 `open`,它会创建工作区并返回页面句柄。需要继续在同一任务中打开页面时,再按上面的 `createWorkspace` 和 `openPage` 流程操作。
|
|
85
|
-
|
|
86
|
-
当 `observe` 返回 `blockedRequests` 或 `getWorkspace` 返回 `blockedOrigins` 时,先核对来源和资源类型。用户或 Agent 取得当前工作区控制权后,可用 `grantOrigin` 传入最新 `controlGeneration`、完整 `origin` 和 `scope`。`resource` 只放行图片、字体、样式和媒体,`navigation` 才允许文档和脚本等完整站点能力。授权后重新 `observe` 或重新执行原操作。
|
|
18
|
+
找不到工作区、授权失效或需要封装 CLI 时,参阅[页面恢复与错误处理](references/recovery.md)。画布、跨框架、上传下载及多账号操作见[操作与授权](references/operations.md)。用 `--schema` 查看本机版本支持的参数。
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# 操作与授权
|
|
2
|
+
|
|
3
|
+
## Agent 控制时的输入保护
|
|
4
|
+
|
|
5
|
+
工作区进入 `agent` 状态后,受管页面的内容区域会强制锁定人工鼠标、触控板和键盘输入。Agent 的每个写入动作在服务端打开一次短暂的派发窗口,动作结束后立即重新锁定。地址栏、标签栏和工作区控制条仍可使用。页面底部的 **人工接管** 是唯一的人工输入入口,接管完成后页面会显示 **交回 Agent**。
|
|
6
|
+
|
|
7
|
+
`getWorkspace` 的 `security.inputGuard` 会报告 `native` 或 `fallback`。`native` 使用带有 open-browser 输入补丁的 Chromium,`fallback` 使用受管页面的捕获层并保持相同的接管状态机。没有该补丁的 Dev 构建可启用 `fallback`。只有明确设置 `OPEN_BROWSER_INPUT_GUARD=off` 才会关闭保护,适合诊断场景。Agent 操作期间不要继续使用旧的人工输入结果,交回后先重新 `observe`。
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
单页授权绑定当前任务和站点。任务结束时原页自动保留。用 `previewFinish` 核对 `keptPageIds` 和 `closedPageIds`,将返回的 `previewId` 及原选择传入 `finishWorkspace`。遇到 `finish_preview_changed` 时重新预览。
|
|
11
|
+
|
|
12
|
+
对于 React Flow 画布或带消息角色标记的聊天区,`canvasSnapshot` 返回已渲染的节点、边、消息和下载链接标签。操作后可再读一次核对结果,`snapshotVersion` 随快照内容或控制代次变化。返回 `unsupported` 时使用普通观察或已授权的画面。
|
|
13
|
+
|
|
14
|
+
macOS 桌面版把任务页面放进 Chromium 原生标签组。新建工作区仍共享应用专用 profile 内同站点的登录态,判断当前账号时请以页面实际显示为准。仅操作任务返回的页面句柄。登录流程打开新窗口时,从工作区页面清单按 openerPageId 和 openedByOperationId 找到对应子页。脚本工作线程受受管页策略约束。
|
|
15
|
+
|
|
16
|
+
省略 `agentName` 时可通过 `OPEN_BROWSER_AGENT_NAME` 配置默认名称。MCP 优先读取握手中的客户端标题,再读取名称。CLI 和 SDK 在已知 Codex 会话环境下显示 Codex,其他情况下分别显示 CLI 和 SDK。显示名称用于识别工作区创建者,访问权限由本机服务验证。
|
|
17
|
+
|
|
18
|
+
需要同时使用两个账号时,先给服务和调用端设置相同的 `OPEN_BROWSER_PROFILE`。每个 profile 使用自己的 Chromium 登录资料、本机服务和回执;同一 profile 的工作区仍共享站点账号。已配对的客户端通过 `OPEN_BROWSER_CLIENT_TOKEN_FILE` 选择自己的令牌。管理者可从本机 CLI 签发、委托和撤销权限,详见仓库 README。
|
|
19
|
+
|
|
20
|
+
- `act` 支持 `select` 和 `wait`。选择项使用最近观察中 combobox 的 ref 和选项 value。等待条件任选可见文本 `text`、完整页面地址 `url` 或可见元素 `element:{role,name}`,动作等待的 `timeoutMs` 为 1 到 10000。动作请求还可传顶层 `timeoutMs`,它是覆盖排队和执行的 1 到 60000 毫秒 wall budget。提交后等结果页的条件可与点击放在一批。用操作回执和网站结果共同核对业务完成。
|
|
21
|
+
- 控件可返回 `checked`、`pressed`、`expanded`、`selected`。其中 `checked` 和 `pressed` 可为 `"mixed"`。用 `{"kind":"setChecked","ref":"…","checked":true}` 要求目标状态,成功后从观察或 `delta.changed` 读回。
|
|
22
|
+
- 精确寻找主页面按钮时,传 `observe.locator`,例如 `{"role":"button","name":"Save","within":{"role":"region","name":"Order B"}}`。仅 `match.status:"unique"` 返回可操作引用。重名时查看 `match.candidates` 的容器再缩小范围。存在模态对话框时,`foreground` 给出标题及边界,可操作列表只包含前景内容。
|
|
23
|
+
- 在同批点击后用 `{"kind":"wait","navigation":"commit","timeoutMs":3000}`,从回执 `navigation` 区分 `document_committed`、`same_document` 和超时的 `not_committed`。再用 `wait.element` 指定 `checked` 等期望状态,或用 `wait.absent:{role,name}` 等待可操作控件消失。已派发的提交发生超时时,先核对网站结果。
|
|
24
|
+
- `observeAfter:"full"` 返回当次操作后的观察,`"delta"` 返回变化的文字和控件。文档或基线更换时可收到全量观察;旧代次、历史回执或页面已变更时,请再次观察。`includeFrames:true` 用于读取已授权的可见一级 frame 文本。需要操作嵌入控件时,传 `frameControls:true`,使用返回的 `frames[].elements[].ref`。框架重新加载后重新观察。
|
|
25
|
+
- 交付调用 `finishWorkspace`,传 `workspaceId`、唯一 `operationId` 及 `keepPageIds` 数组。保留的页面脱离任务,未选中的受管页关闭。重试沿用同一操作 ID 和同一页集合;收到 `outcome_unknown` 时先核实页面。用户也可从工作区面板选择保留页。新打开的普通标签无需清理。
|
|
26
|
+
- `listOperations` 查询结束的工作区回执;`listEvents` 使用 `after`、`limit` 续读。事件序号从响应的 `nextCursor` 取得。默认保留 7 天。使用随机操作 ID,避免把账号、URL 或正文写入 ID。
|
|
27
|
+
|
|
28
|
+
Godot 等持续重绘且只有一个画布的游戏可在 captureVisual 中传 liveCanvas:true。此帧只接受命中同一画布的输入,键盘操作还要求画布已获得焦点。普通截图仍核对像素是否变化。
|
|
29
|
+
|
|
30
|
+
画布、拖拽或触摸操作先运行 `capabilities`。具有页面像素权限时,调用 `captureVisual`,传页面句柄及可选的 `region:{x,y,width,height}` 或最新元素 `ref`。PNG 的左上角对应返回的 `clip.x,clip.y`,坐标均为视口 CSS 像素。返回的 `frameId` 有效 8 秒,可用一次。通过 `act` 传同一帧的文档和控制代次,以及 `{"kind":"spatial","frameId":"<帧 ID>","action":{"type":"click","x":50,"y":50}}`。可用类型包括 click、hover、tap、drag(toX/toY)、scroll(deltaX/deltaY)、key(keys)和 text(text)。表单控件及私有区域已遮盖,登录继续使用语义引用。遇到 `stale_visual_frame` 时重新截图;若操作回执为 `outcome_unknown`,先核对业务结果。工作区所有者可请用户在所选页面的扩展内点击「允许本任务截图」,核对任务和站点,也可在同处撤销。委托调用方的像素权限由本机管理员通过 `admin.setPixelPermission` 授予到指定工作区、调用方及来源。
|
|
31
|
+
|
|
32
|
+
### 长页面、弹窗和下载
|
|
33
|
+
|
|
34
|
+
- `observe` 返回 `truncated:true` 和 `nextCursor` 时,把页面句柄与 `cursor:nextCursor` 一起传给下一次 `observe`。知道目标名称时可直接传 `name` 做子串匹配。也可以传 `region:{x,y,width,height}` 按页面视口 CSS 像素选区。每批控件数量可用 `limit` 指定,取值 1 到 180。收到 `stale_cursor` 时重新观察。
|
|
35
|
+
- 点击持续被遮罩挡住会以 `target_obscured` 结束。读取最新页面、等待遮罩消失后,用新的操作号点击。收到 `inspect_before_retry` 时,先查站点提交结果再决定下一步。
|
|
36
|
+
- 登录流程从一次受管点击打开获准的 HTTP(S) 地址后,读操作回执的 `openedPageIds`。延迟开窗时也可通过 `getWorkspace` 找到发起这次操作的子页,以其句柄继续登录。
|
|
37
|
+
- 下载发生后用 `listDownloads` 查询当前工作区,通过 `downloadId` 区分同名文件。`cancelDownload` 需要工作区控制权限。用户在工作区面板保留所需文件后再交付任务,查看文件时以面板显示的路径为准。
|
|
38
|
+
- 用户保留文件后,`listOutputs` 返回本地 manifest。Agent 可用 `readOutput` 按 `offset` 和 `limit`(最多 64 KiB)读取 base64 分块,使用 `nextOffset` 续读。输出默认只保留 24 小时,凭据和页面正文不会写入 manifest。
|
|
39
|
+
|
|
40
|
+
原生对话框出现时,读取 `getWorkspace.pendingDialogs`,按页面、操作号、来源及对话框类型核实用途。用 `respondDialog` 传工作区、页面、dialogId、controlGeneration、accept,prompt 可另传 text。每个对话框只回应一次。用户可以在工作区扩展接管并处理。Playwright 无法标识对话框的发起 frame,多来源的 frame 树按取消处理。
|
|
41
|
+
|
|
42
|
+
上传时,用户在工作区扩展选定页面、核对来源并选择文件。调用 `listUploads` 取得该工作区的 fileId,再发送 `act` 动作 `{"kind":"upload","ref":"最近观察中的文件选择按钮","fileId":"用户授权的 ID"}`。单文件至多 8 MiB,只在当前页面、文档、来源和控制代次有效,五分钟失效。发给站点后即消耗授权。结果待核实时应先检查站点。
|
|
43
|
+
|
|
44
|
+
点击前以 `eventCursor` 保存工作区 nextCursor,触发后调用 `waitForEvent`,传同一页面句柄、after、kind(popup / download / dialog)、可选 operationId 和 timeoutMs(1–10000)。也可先发起等待再点击。超时返回 event:null,可从原游标继续;收到事件后使用 nextCursor。resourceId 对应子页、下载或对话框 ID。异步下载只能可靠归到页面时,省略 operationId 过滤。只读委托可查看事件,响应对话框和上传仍需控制权。
|
|
45
|
+
|
|
46
|
+
- 中断后用 `getRecoverySummary` 传工作区 ID。对 `observe` 标记的页重新观察,`handle_unavailable` 表示旧句柄已失效。对 `verify_site` 先核对网站结果,`already_completed` 表示已记录完成。交付回执的 `keptPageIds` 记录已移交的标签,`deliveries` 列出已确认保留的下载 ID,`outputs` 列出可续读的本地输出 ID。回执与资料分页分别使用 `nextCursor → after` 和 `nextObjectCursor → objectsAfter`。
|
|
47
|
+
|
|
48
|
+
- 创建工作区可传 `profileId`,服务核对它与已连接账号资料一致,返回并在接管面板显示资料名。多个账号分别启动对应的 `OPEN_BROWSER_PROFILE` 实例。CLI、MCP 通过同名环境配置连接,SDK 可用 `new OpenBrowserClient({profileId:'work'})` 选择本机已启动的命名实例。
|
|
49
|
+
|
|
50
|
+
### 按网站运行流程
|
|
51
|
+
|
|
52
|
+
将 [登录后导出](../workflows/login-export.json) 或 [上传后核对](../workflows/upload-verify.json) 保存为本地模板,按实际站点填写精确的 `origin`、控件角色、名称和成功条件。调用 `runWorkflow` 时传页面句柄、最新 `documentId`、`controlGeneration`、唯一 `operationId`、模板对象 `workflow`,以及本次运行的 `bindings`。例如 `{"attachment":"已授权 fileId"}` 或 `{"password":"已绑定 credentialRef"}`。模板仅保存参数名,每次运行时提供授权引用。服务端逐步重新观察、核对来源和控制权。
|
|
53
|
+
|
|
54
|
+
用 `getOperation` 查看已完成步数。遇到 `target_ambiguous`、`target_missing` 或 `workflow_origin_changed` 时核对页面和已执行步骤。登录后导出还应读取当前工作区的下载回执,核对交付物。
|
|
55
|
+
|
|
56
|
+
- 使用 `capabilities` 确认服务提供的方法和限额,`--schema` 输出当前 CLI 的全部方法和参数。遇到不支持的方法时以服务端返回的能力为准。
|
|
57
|
+
- `diagnoseOperation` 接受 `workspaceId` 和 `operationId`,返回已完成步数、是否记录过派发、故障类别和建议处置。桌面工作区面板也可预览并保存同一份脱敏 JSON。结果为 `check_site_result` 时先用页面的业务结果核实是否生效。
|
|
58
|
+
|
|
59
|
+
当 `observe` 返回 `blockedRequests` 或 `getWorkspace` 返回 `blockedOrigins` 时,先核对来源和资源类型。用户或 Agent 取得当前工作区控制权后,可用 `grantOrigin` 传入最新 `controlGeneration`、完整 `origin` 和 `scope`。`resource` 只放行图片、字体、样式和媒体,`navigation` 才允许文档和脚本等完整站点能力。授权后重新 `observe` 或重新执行原操作。
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# 恢复页面与读取错误
|
|
2
|
+
|
|
3
|
+
| 返回 | 下一步 |
|
|
4
|
+
| --- | --- |
|
|
5
|
+
| `mode:agent` 且 `pages:[]` | 查看 `pageAvailability`。首次绑定时等待 `page_bound`,已关闭的页需要重新选择。 |
|
|
6
|
+
| `sync_pending` | 用原工作区和页面调用 `reattachPage` 重试同步。 |
|
|
7
|
+
| `workspace_not_found` | 核对 profile 和调用方,用原工作区 ID 调用 `getRecoverySummary`。读取 `closeReason`、页面处置和动作回执。历史记录没有可用绑定时,按 `nextAction:request_tab_selection` 请用户重新选页。旧记录可能返回 `unknown`。 |
|
|
8
|
+
| `service_shutdown` | 用户交付标签时可勾选重启恢复。服务在授权时记录浏览器生成的页面身份。正常退出或服务进程被强制终止后,原资料中的有效记录可在 24 小时内重新绑定 Chromium 恢复的同一标签。用原工作区和页面调用 `reattachPage` 取得新观察。人工接管、原标签未恢复、页面身份变化或提交仍待核实时,请按恢复摘要重新选页。 |
|
|
9
|
+
| `page_not_found`、`origin_denied` | 核对用户实际选中的标签和站点。恢复时按用户选择绑定,同一地址可能有多个标签。 |
|
|
10
|
+
| `stale_document`、`stale_control`、`stale_ref` | 对原页重新观察,核对控件再提交。 |
|
|
11
|
+
| `outcome_unknown`、请求超时 | 保留原 operationId,查询动作回执,核对站点结果后再决定是否重试。 |
|
|
12
|
+
| `RESPONSE_LOST` | 请求已发出,服务可能已执行。创建任务后用 `listWorkspaces` 核对名称和页面。有 operationId 时用 `getOperation` 核对回执。 |
|
|
13
|
+
| `target_obscured` | 查看回执的 obstruction 和观察中的前景弹窗,核对后再处理遮挡。 |
|
|
14
|
+
| `human_verification_required` | 用 `handOff` 请用户处理站点验证,用 `reattachPage` 等待交回。 |
|
|
15
|
+
|
|
16
|
+
宿主进程关闭会结束当前 Chromium 上下文。恢复标签会得到新文档,旧控件引用和一次性视觉帧随之失效。备用授权流程由用户点击「生成备用授权码」,然后调用 `adoptCurrentTab` 兑换,一次有效,期限两分钟。
|
|
17
|
+
|
|
18
|
+
## CLI 错误
|
|
19
|
+
|
|
20
|
+
连续调用使用 `session`,每行 stdout 按请求 id 返回 `{id,result}` 或 `{id,error}`。单次脚本调用可用 `--json <方法>`,统一从 stdout 读取 `{result}` 或 `{error}` 并检查退出码。兼容旧版时,单次 CLI 成功时在 stdout 返回结果,失败时以非零状态退出,并在 stderr 返回错误。下面的 Python 函数接收 Skill 启动器的绝对路径,保留错误码和原始启动错误。
|
|
21
|
+
|
|
22
|
+
```python
|
|
23
|
+
import json
|
|
24
|
+
import subprocess
|
|
25
|
+
|
|
26
|
+
def call(launcher, method, params):
|
|
27
|
+
proc = subprocess.run([launcher, method], input=json.dumps(params),
|
|
28
|
+
text=True, capture_output=True)
|
|
29
|
+
if proc.returncode == 0:
|
|
30
|
+
return json.loads(proc.stdout)
|
|
31
|
+
try:
|
|
32
|
+
detail = json.loads(proc.stderr)["error"]
|
|
33
|
+
except (ValueError, KeyError, TypeError):
|
|
34
|
+
detail = {"code": "CLI_EXIT", "message": proc.stderr.strip()}
|
|
35
|
+
raise RuntimeError(proc.returncode, detail)
|
|
36
|
+
```
|
|
@@ -1,16 +1,15 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import {
|
|
2
|
+
import { readFile, realpath, stat } from 'node:fs/promises';
|
|
3
3
|
import { spawn } from 'node:child_process';
|
|
4
|
+
import { once } from 'node:events';
|
|
5
|
+
import net from 'node:net';
|
|
6
|
+
import { setTimeout as delay } from 'node:timers/promises';
|
|
4
7
|
import path from 'node:path';
|
|
5
|
-
import { pathToFileURL } from 'node:url';
|
|
8
|
+
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
6
9
|
|
|
7
10
|
const defaultWaitMs = 10_000;
|
|
8
11
|
const retryDelayMs = 150;
|
|
9
12
|
|
|
10
|
-
function sleep(ms) {
|
|
11
|
-
return new Promise(resolve => setTimeout(resolve, ms));
|
|
12
|
-
}
|
|
13
|
-
|
|
14
13
|
function parseErrorCode(stderr) {
|
|
15
14
|
try {
|
|
16
15
|
const response = JSON.parse(stderr.trim());
|
|
@@ -20,54 +19,69 @@ function parseErrorCode(stderr) {
|
|
|
20
19
|
}
|
|
21
20
|
}
|
|
22
21
|
|
|
23
|
-
async function
|
|
24
|
-
|
|
25
|
-
await access(file);
|
|
26
|
-
return true;
|
|
27
|
-
} catch {
|
|
28
|
-
return false;
|
|
29
|
-
}
|
|
22
|
+
async function isFile(file) {
|
|
23
|
+
return stat(file).then(entry => entry.isFile(), () => false);
|
|
30
24
|
}
|
|
31
25
|
|
|
32
|
-
function detached(command, args, options = {}) {
|
|
26
|
+
async function detached(command, args, options = {}) {
|
|
33
27
|
const child = spawn(command, args, { ...options, detached: true, stdio: 'ignore' });
|
|
28
|
+
await once(child, 'spawn');
|
|
34
29
|
child.unref();
|
|
35
30
|
}
|
|
36
31
|
|
|
32
|
+
async function wakeExisting(root) {
|
|
33
|
+
try {
|
|
34
|
+
const { pidPath, socketPath } = await import(pathToFileURL(path.join(root, 'paths.mjs')).href);
|
|
35
|
+
const pid = Number((await readFile(pidPath, 'utf8')).trim());
|
|
36
|
+
if (!Number.isSafeInteger(pid) || pid <= 0) return false;
|
|
37
|
+
process.kill(pid, 0);
|
|
38
|
+
const active = await new Promise(resolve => {
|
|
39
|
+
const probe = net.connect(socketPath);
|
|
40
|
+
probe.setTimeout(1000, () => { probe.destroy(); resolve(false); });
|
|
41
|
+
probe.once('connect', () => { probe.destroy(); resolve(true); });
|
|
42
|
+
probe.once('error', () => resolve(false));
|
|
43
|
+
});
|
|
44
|
+
if (!active) return false;
|
|
45
|
+
process.kill(pid, 'SIGUSR2');
|
|
46
|
+
return true;
|
|
47
|
+
} catch { return false; }
|
|
48
|
+
}
|
|
49
|
+
|
|
37
50
|
async function startLocalBrowser(root) {
|
|
51
|
+
if (await wakeExisting(root)) return;
|
|
38
52
|
const appBundle = process.platform === 'darwin' && path.basename(root) === 'app'
|
|
39
53
|
? path.resolve(root, '../../../')
|
|
40
54
|
: undefined;
|
|
41
55
|
if (appBundle?.endsWith('.app')) {
|
|
42
|
-
detached('open', ['-g', appBundle]);
|
|
43
|
-
return;
|
|
56
|
+
return detached('open', ['-g', appBundle]);
|
|
44
57
|
}
|
|
45
58
|
|
|
46
59
|
const portableRoot = path.resolve(root, '..');
|
|
47
60
|
const portableLauncher = process.platform === 'win32'
|
|
48
61
|
? path.join(portableRoot, 'open-browser.cmd')
|
|
49
62
|
: path.join(portableRoot, 'open-browser');
|
|
50
|
-
if (await
|
|
63
|
+
if (await isFile(portableLauncher)) {
|
|
51
64
|
if (process.platform === 'win32') {
|
|
52
|
-
detached(process.env.ComSpec || 'cmd.exe', ['/d', '/s', '/c', `"${portableLauncher}"`], { cwd: portableRoot });
|
|
53
|
-
} else {
|
|
54
|
-
detached(portableLauncher, [], { cwd: portableRoot });
|
|
65
|
+
return detached(process.env.ComSpec || 'cmd.exe', ['/d', '/s', '/c', `"${portableLauncher}"`], { cwd: portableRoot });
|
|
55
66
|
}
|
|
56
|
-
return;
|
|
67
|
+
return detached(portableLauncher, [], { cwd: portableRoot });
|
|
57
68
|
}
|
|
58
69
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
70
|
+
if (process.platform === 'darwin' && process.env.OPEN_BROWSER_USE_SOURCE !== '1' &&
|
|
71
|
+
process.env.OPEN_BROWSER_BUILD_PROFILE !== 'dev') {
|
|
72
|
+
for (const app of [path.join(root, 'dist/open-browser.app'), '/Applications/open-browser.app']) {
|
|
73
|
+
if (await isFile(path.join(app, 'Contents/MacOS/open-browser'))) return detached('open', ['-g', app]);
|
|
74
|
+
}
|
|
63
75
|
}
|
|
76
|
+
const service = path.join(root, 'service.mjs');
|
|
77
|
+
if (await isFile(service)) return detached(process.execPath, [service, '--chromium'], { cwd: root });
|
|
64
78
|
|
|
65
79
|
throw new Error('OPEN_BROWSER_APP_NOT_FOUND');
|
|
66
80
|
}
|
|
67
81
|
|
|
68
82
|
function waitLimit() {
|
|
69
83
|
const value = Number(process.env.OPEN_BROWSER_AUTOSTART_TIMEOUT_MS ?? defaultWaitMs);
|
|
70
|
-
return Number.isFinite(value) ? Math.max(1_000, Math.min(30_000, value)) : defaultWaitMs;
|
|
84
|
+
return Number.isFinite(value) ? Math.max(1_000, Math.min(30_000, Math.trunc(value))) : defaultWaitMs;
|
|
71
85
|
}
|
|
72
86
|
|
|
73
87
|
async function run(node, root, args, input) {
|
|
@@ -91,33 +105,86 @@ async function run(node, root, args, input) {
|
|
|
91
105
|
});
|
|
92
106
|
}
|
|
93
107
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
108
|
+
// Only readiness is polled. One SDK client and one deadline cover every probe.
|
|
109
|
+
async function ensureService(root, start = startLocalBrowser, initialError) {
|
|
110
|
+
const { OpenBrowserClient, OpenBrowserError } = await import(pathToFileURL(path.join(root, 'sdk.mjs')).href);
|
|
111
|
+
const api = new OpenBrowserClient();
|
|
112
|
+
const signal = AbortSignal.timeout(waitLimit());
|
|
113
|
+
let error = initialError;
|
|
114
|
+
let launched = false;
|
|
115
|
+
try {
|
|
116
|
+
for (;;) {
|
|
117
|
+
if (error) {
|
|
118
|
+
if (process.env.OPEN_BROWSER_AUTOSTART === '0' ||
|
|
119
|
+
!['SERVICE_UNAVAILABLE', 'not_ready', 'RESPONSE_LOST'].includes(error.code)) throw error;
|
|
120
|
+
if (error.code === 'SERVICE_UNAVAILABLE' && !launched) {
|
|
121
|
+
await start(root);
|
|
122
|
+
launched = true;
|
|
123
|
+
} else {
|
|
124
|
+
await delay(retryDelayMs, undefined, { signal });
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
try {
|
|
128
|
+
await api.capabilities({ signal });
|
|
129
|
+
return;
|
|
130
|
+
} catch (cause) {
|
|
131
|
+
signal.throwIfAborted();
|
|
132
|
+
error = cause;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
} catch (cause) {
|
|
136
|
+
if (signal.aborted) throw new OpenBrowserError('REQUEST_TIMEOUT', '等待本机服务就绪超时。');
|
|
137
|
+
throw cause;
|
|
138
|
+
} finally {
|
|
139
|
+
api.close();
|
|
102
140
|
}
|
|
103
|
-
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
async function runSession(node, root, args) {
|
|
144
|
+
await ensureService(root);
|
|
145
|
+
const child = spawn(node, [path.join(root, 'cli.mjs'), ...args], {
|
|
146
|
+
cwd: root,
|
|
147
|
+
env: process.env,
|
|
148
|
+
stdio: 'inherit',
|
|
149
|
+
});
|
|
150
|
+
const [code] = await once(child, 'close');
|
|
151
|
+
return code ?? 1;
|
|
104
152
|
}
|
|
105
153
|
|
|
106
154
|
export async function invoke({ node, root, args, input, start = startLocalBrowser }) {
|
|
107
155
|
const first = await run(node, root, args, input);
|
|
108
156
|
if (first.code === 0) return first;
|
|
109
|
-
const errorCode = parseErrorCode(first.stderr);
|
|
110
|
-
if (
|
|
111
|
-
if (errorCode === '
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
157
|
+
const errorCode = parseErrorCode(args[0] === '--json' ? first.stdout : first.stderr);
|
|
158
|
+
if (process.env.OPEN_BROWSER_AUTOSTART === '0') return first;
|
|
159
|
+
if (errorCode === 'RESPONSE_LOST') {
|
|
160
|
+
const method = args[0] === '--json' ? args[1] : args[0];
|
|
161
|
+
const { readCapabilities } = await import(pathToFileURL(path.join(root, 'capability-matrix.mjs')).href);
|
|
162
|
+
// Capturing a frame issues a new one-time ticket. Retry only repeatable reads.
|
|
163
|
+
if (method === 'captureVisual' || !(readCapabilities.includes(method) ||
|
|
164
|
+
['capabilities', 'listWorkspaces', 'listUploads', 'canvasSnapshot', 'admin.list', 'admin.scheduler'].includes(method))) return first;
|
|
165
|
+
} else if (!['SERVICE_UNAVAILABLE', 'not_ready'].includes(errorCode)) return first;
|
|
166
|
+
try { await ensureService(root, start, { code: errorCode }); }
|
|
167
|
+
catch { return first; }
|
|
115
168
|
return run(node, root, args, input);
|
|
116
169
|
}
|
|
117
170
|
|
|
118
171
|
async function main() {
|
|
119
|
-
const
|
|
120
|
-
|
|
172
|
+
const root = fileURLToPath(new URL('../../..', import.meta.url));
|
|
173
|
+
const args = process.argv.slice(2);
|
|
174
|
+
// Older desktop launchers provide their own package directory as argv[2].
|
|
175
|
+
if (args[0] && path.isAbsolute(args[0]) && await realpath(args[0]).catch(() => null) === path.resolve(root)) args.shift();
|
|
176
|
+
if (args[0] === 'skill') {
|
|
177
|
+
if (args.length > 1) throw Object.assign(new Error('skill 不接受额外参数。'), { code: 'INVALID_PARAMS' });
|
|
178
|
+
const { readSkillGuide } = await import('./guide.mjs');
|
|
179
|
+
process.stdout.write(await readSkillGuide());
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
const { autoInstallSkillLinks } = await import('../../../skill-links.mjs');
|
|
183
|
+
await autoInstallSkillLinks({ useCache: true });
|
|
184
|
+
if (args[0] === 'session') {
|
|
185
|
+
process.exitCode = await runSession(process.execPath, root, args);
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
121
188
|
const chunks = [];
|
|
122
189
|
for await (const chunk of process.stdin) chunks.push(chunk);
|
|
123
190
|
const result = await invoke({ node: process.execPath, root, args, input: Buffer.concat(chunks) });
|
|
@@ -126,9 +193,9 @@ async function main() {
|
|
|
126
193
|
process.exitCode = result.code;
|
|
127
194
|
}
|
|
128
195
|
|
|
129
|
-
if (process.argv[1] &&
|
|
196
|
+
if (import.meta.main ?? (process.argv[1] && await realpath(process.argv[1]).catch(() => null) === fileURLToPath(import.meta.url))) {
|
|
130
197
|
main().catch(error => {
|
|
131
|
-
process.stderr.write(JSON.stringify({ error: { code: error.code ?? 'CLI_BOOTSTRAP_FAILED', message: error.message } }) + '\n');
|
|
198
|
+
process.stderr.write(JSON.stringify({ error: { code: error.code ?? 'CLI_BOOTSTRAP_FAILED', message: error.message, nextAction: error.nextAction } }) + '\n');
|
|
132
199
|
process.exitCode = 1;
|
|
133
200
|
});
|
|
134
201
|
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { readFile } from 'node:fs/promises';
|
|
2
|
+
|
|
3
|
+
export async function readSkillGuide(platform = process.platform) {
|
|
4
|
+
let body;
|
|
5
|
+
try {
|
|
6
|
+
body = await readFile(new URL('../SKILL.md', import.meta.url), 'utf8');
|
|
7
|
+
} catch (cause) {
|
|
8
|
+
throw Object.assign(new Error('未找到 skills/open-browser/SKILL.md。', {cause}), {code:'SKILL_GUIDE_NOT_FOUND'});
|
|
9
|
+
}
|
|
10
|
+
if (!body.endsWith('\n')) body += '\n';
|
|
11
|
+
const label = {darwin:'macOS', win32:'Windows'}[platform];
|
|
12
|
+
if (!label) return body;
|
|
13
|
+
const supplement = await readFile(new URL(`../references/${label.toLowerCase()}.md`, import.meta.url), 'utf8')
|
|
14
|
+
.catch(error => { if (error.code !== 'ENOENT') throw error; });
|
|
15
|
+
if (!supplement) return body;
|
|
16
|
+
return `[open-browser:skill] open-browser usage guide (${label})\n\n${body}\n${supplement}`;
|
|
17
|
+
}
|
package/studio/studio.js
CHANGED
|
@@ -10,7 +10,7 @@ const groups = [
|
|
|
10
10
|
{ id: 'human', title: '人工接管' }, { id: 'idle', title: '待命' },
|
|
11
11
|
];
|
|
12
12
|
const phases = { idle: '等待指令', queued: '已排队', running: '执行中', needs_credential: '等待凭据',
|
|
13
|
-
completed: '已完成', failed: '执行失败', cancelled: '已取消', outcome_unknown: '结果待核对' };
|
|
13
|
+
completed: '已完成', failed: '执行失败', needs_visual_permission: '需要授权截图', cancelled: '已取消', outcome_unknown: '结果待核对' };
|
|
14
14
|
const kinds = { openPage: '打开页面', observe: '观察页面', act: '页面操作' };
|
|
15
15
|
const stepKinds = { click: '点击', fill: '填写', fillCredential: '填写凭据' };
|
|
16
16
|
const errorText = {
|
|
@@ -75,7 +75,7 @@ function clearSensitiveInputs() {
|
|
|
75
75
|
function groupOf(w) {
|
|
76
76
|
if (w.mode === 'human' || w.mode === 'transferring') return 'human';
|
|
77
77
|
const a = w.activity || {};
|
|
78
|
-
if (a.needsCredential || a.phase === 'failed' || a.phase === 'outcome_unknown') return 'attention';
|
|
78
|
+
if (a.needsCredential || a.needsVisualPermission || a.phase === 'failed' || a.phase === 'outcome_unknown') return 'attention';
|
|
79
79
|
if (a.current || a.queued || a.phase === 'running' || a.phase === 'queued') return 'running';
|
|
80
80
|
return 'idle';
|
|
81
81
|
}
|
package/tab-adoption.mjs
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { randomBytes } from 'node:crypto';
|
|
2
|
+
import { fail, webOrigin } from './workspace-state.mjs';
|
|
3
|
+
import { refreshTitle } from './desktop-page.mjs';
|
|
4
|
+
|
|
5
|
+
// Only the packaged extension supplies a target ID. Agent clients receive an
|
|
6
|
+
// expiring capability scoped to one workspace and never enumerate ordinary tabs.
|
|
7
|
+
export async function offerTab(service, ws, {targetId, origin, accountAlias, resumeOnRestart = false}) {
|
|
8
|
+
if (!service.sharedContext || service.actors.getStore() !== 'desktop') fail('forbidden');
|
|
9
|
+
if (ws.everRegistered || ws.pending || ws.mode !== 'agent') fail('adoption_requires_empty_workspace');
|
|
10
|
+
if (webOrigin(origin) !== origin || typeof targetId !== 'string' ||
|
|
11
|
+
(accountAlias !== undefined && (typeof accountAlias !== 'string' || accountAlias.length > 48))) fail('invalid_params');
|
|
12
|
+
const context = service.sharedContext;
|
|
13
|
+
for (const candidate of context.pages()) {
|
|
14
|
+
if (URL.parse(candidate.url())?.origin !== origin) continue;
|
|
15
|
+
const session = await context.newCDPSession(candidate).catch(() => null);
|
|
16
|
+
if (!session) continue;
|
|
17
|
+
let info;
|
|
18
|
+
try { info = (await session.send('Target.getTargetInfo')).targetInfo; }
|
|
19
|
+
finally { await session.detach().catch(() => {}); }
|
|
20
|
+
if (info.targetId !== targetId) continue;
|
|
21
|
+
if (service.workspaces.get(ws.id) !== ws || ws.mode !== 'agent' || ws.everRegistered) fail('adoption_requires_empty_workspace');
|
|
22
|
+
if (candidate.isClosed() || webOrigin(candidate.url()) !== origin) break;
|
|
23
|
+
if ([...service.workspaces.values()].some(w => [...w.pages.values()].some(t => t.page === candidate)))
|
|
24
|
+
fail('page_already_managed');
|
|
25
|
+
for (const other of service.workspaces.values()) {
|
|
26
|
+
if (other.offer?.page === candidate) other.offer.revoke();
|
|
27
|
+
}
|
|
28
|
+
const token = randomBytes(32).toString('hex');
|
|
29
|
+
ws.offer?.revoke();
|
|
30
|
+
const revoke = () => {
|
|
31
|
+
clearTimeout(timer);
|
|
32
|
+
candidate.off('framenavigated', navigated);
|
|
33
|
+
candidate.off('close', revoke);
|
|
34
|
+
ws.offer = null;
|
|
35
|
+
};
|
|
36
|
+
const navigated = frame => { if (frame === candidate.mainFrame()) revoke(); };
|
|
37
|
+
const timer = setTimeout(revoke, 120_000).unref();
|
|
38
|
+
candidate.on('framenavigated', navigated);
|
|
39
|
+
candidate.once('close', revoke);
|
|
40
|
+
ws.offer = {revoke, resumeOnRestart, page:candidate, origin, token, targetId, accountAlias, expiresAt:Date.now() + 120_000};
|
|
41
|
+
return {adoptionToken:token, workspaceId:ws.id, expiresAt:ws.offer.expiresAt};
|
|
42
|
+
}
|
|
43
|
+
fail('adoption_page_changed');
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export async function adoptTab(service, ws, params) {
|
|
47
|
+
if (service.actors.getStore() !== ws.owner) fail('forbidden');
|
|
48
|
+
const offer = ws.offer;
|
|
49
|
+
if (!offer || offer.token !== params.adoptionToken || Date.now() >= offer.expiresAt) fail('adoption_expired');
|
|
50
|
+
offer.revoke();
|
|
51
|
+
if (ws.pending || ws.everRegistered || ws.mode !== 'agent') fail('adoption_requires_empty_workspace');
|
|
52
|
+
if (offer.page.isClosed() || URL.parse(offer.page.url())?.origin !== offer.origin) fail('adoption_page_changed');
|
|
53
|
+
ws.singleTab = true;
|
|
54
|
+
ws.networkMode = 'browser';
|
|
55
|
+
ws.origins = new Set([offer.origin]);
|
|
56
|
+
ws.resourceOrigins.clear();
|
|
57
|
+
ws.accountAlias = offer.accountAlias;
|
|
58
|
+
const tab = service.register(ws, offer.page, offer.restoredPageId);
|
|
59
|
+
tab.targetId = offer.targetId;
|
|
60
|
+
tab.createdByAgent = false;
|
|
61
|
+
// Bind before yielding so a concurrent redemption cannot claim this page.
|
|
62
|
+
return service.enqueue(ws, async () => {
|
|
63
|
+
await service.setInputGuard?.(ws, tab, true);
|
|
64
|
+
// Extension registration is retryable via reattachPage. Keep the selected
|
|
65
|
+
// page and its capability when only the extension acknowledgement is late.
|
|
66
|
+
try { await service.syncTab(ws, tab); }
|
|
67
|
+
catch (error) { if (error.code !== 'sync_pending') throw error; }
|
|
68
|
+
await refreshTitle(ws, tab);
|
|
69
|
+
service.requireControl(ws, ws.generation);
|
|
70
|
+
if (offer.page.isClosed() || URL.parse(offer.page.url())?.origin !== offer.origin) fail('adoption_page_changed');
|
|
71
|
+
const generation = ws.generation;
|
|
72
|
+
ws.resumeOnRestart = offer.resumeOnRestart === true;
|
|
73
|
+
await service.tabRecovery?.refresh(ws);
|
|
74
|
+
service.requireControl(ws, generation);
|
|
75
|
+
if (ws.pages.get(tab.id) !== tab || URL.parse(offer.page.url())?.origin !== offer.origin) fail('adoption_page_changed');
|
|
76
|
+
service.store.event(ws.owner, ws.id, 'page_bound', 'managed', null, null, {pageId:tab.id, resourceId:tab.id});
|
|
77
|
+
return {workspaceId:ws.id, pageId:tab.id};
|
|
78
|
+
}, {kind:'adoptCurrentTab', pageId:tab.id}).catch(async error => {
|
|
79
|
+
// A concurrent takeover/close owns the release sequence.
|
|
80
|
+
if (ws.mode === 'agent' && !ws.finishing) await service.call('closeWorkspace',{workspaceId:ws.id},ws.owner);
|
|
81
|
+
throw error;
|
|
82
|
+
});
|
|
83
|
+
}
|