@liyuera/dsh-chrome-browser 0.6.3 → 0.6.4

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/README.md CHANGED
@@ -278,7 +278,7 @@ Chrome 不会重读「加载已解压」扩展的文件,所以插件自己补了
278
278
 
279
279
  于是 **npm 升级插件后不用再碰 `chrome://extensions`**。
280
280
 
281
- **扩展 0.5.0 这次升级会自动重载**:它只加了 `webmcp-list` / `webmcp-call` 两个 op、改了 `BUILD_HASH`,`WS_URLS` 与桥端口没变(仍拨 21987);已经连着 21987 的扩展会被现有自动更新握手用 `chrome.runtime.reload()` 重读磁盘,升到 0.5.0。下面的手动重载只针对从 0.4.1/19387 跨端口升级的那一次。
281
+ **扩展 0.5.0 / 0.5.1 升级会自动重载**:0.5.0 加了 `webmcp-list` / `webmcp-call` 两个 op,0.5.1 改了重连逻辑;两次都没动 `WS_URLS` 与桥端口(仍拨 21987),改的只是各自的代码与 `BUILD_HASH`。已经连着 21987 的扩展会被现有自动更新握手用 `chrome.runtime.reload()` 重读磁盘,升到对应版本。下面的手动重载只针对从 0.4.1/19387 跨端口升级的那一次。
282
282
 
283
283
  **例外:首次升级到 21987 这一版**。自动重载握手要求扩展已经连着桥;旧扩展(0.4.1)拨的还是 19387/3080,桌面应用的 dsh 端口每次启动随机,旧扩展没有可用地址,够不到 21987,连不上就收不到握手,所以桌面应用升级后第一次要手动重载(步骤见[安装 → 2](#2-装配套-chrome-扩展));源码启动的 `pnpm dsh web` 在 3080 上仍可能让旧扩展经备用地址连上并自动完成重载,没自动恢复时同样手动重载一次。重载后扩展拨 21987,后续升级恢复自动;新装用户加载的已经是新扩展,没有这一步。
284
284
 
@@ -288,6 +288,17 @@ Chrome 不会重读「加载已解压」扩展的文件,所以插件自己补了
288
288
 
289
289
  之后扩展通过本地 WebSocket(`/chrome-browser/ext/ws`)与插件通信(走插件的固定桥端口,见上):列真实标签页(自带站点图标)、读取/点击/输入/求值/导航/新开/截图。扩展没连上时 `@` 菜单不出现标签页分组,`/chrome-browser/ext/status` 会报 `connected:false`。
290
290
 
291
+ ### 重启 dsh 后:自动重连
292
+
293
+ 宿主重启后扩展会自己重连,一般几秒内恢复,**不需要手动 reload 扩展**。
294
+
295
+ - 扩展断线后进入 **5 分钟 warm 窗口**:重试间隔上限 5 秒;每次重试都会调一次 `chrome.alarms` API 重置 Chrome 的 30 秒空闲计时,重试定时器能跨宿主重启存活,慢启动的宿主还在启动时不会因为 MV3 挂起 worker 而断掉重试。
296
+ - 5 分钟后回到冷重试:重试间隔上限 15 秒,由每 30 秒一次的 `dsh-bridge` alarm 兜底;宿主长时间关着不会把 worker 一直保活。
297
+ - Chrome 的活动会立即触发重连:`chrome.tabs.onActivated`、`chrome.tabs.onUpdated`(`status` 变 `complete` 或 URL 变化)与 `chrome.windows.onFocusChanged` 都会调 `wakeBridge()` 立即试连,不用等下一次 alarm。
298
+ - 旧的扩展版本(0.5.0/0.4.x)只有 30 秒一次的 alarm 重试,宿主重启后的重连空窗按 alarm 相位最多约 30 秒量级,所以会给人「必须手动 reload 才能读标签页」的观感;0.5.1 起是上面的行为。
299
+ - **升级到 0.6.4 后的第一次重启是例外**:那时正在运行的还是扩展 0.5.0(没有本次重连修复),它得先在新宿主上重连一次、被指纹握手自动重载成 0.5.1,才具备上面的行为;所以这一次重启可能仍按旧的 30 秒 alarm 路径恢复(最多约 30 秒量级),之后的每次重启都会秒级恢复。
300
+ - 只有长时间(例如几分钟)仍未连上时,才去 `chrome://extensions` 手动「重新加载」扩展当兜底;先看 `/chrome-browser/ext/status`。
301
+
291
302
  **CDP 后端(可选,`backend: 'cdp'`)**——原有能力保留:
292
303
 
293
304
  - **owned 模式**:插件第一次使用工具时启动**独立 Chrome**(专用 profile 持久化在 `$DSH_HOME/chrome-browser/profile-<port>`,窗口/标签在重启后保留)。你在里面打开什么,AI 就能看什么。插件启动/关闭只作用于这个专用实例,永不触碰你日常浏览器的窗口。
@@ -2,7 +2,7 @@
2
2
  "manifest_version": 3,
3
3
  "name": "dsh Chrome Control",
4
4
  "description": "Lets the local dsh agent drive this Chrome: list tabs, read pages, click and type, run JS, capture screenshots, read console output. Talks to dsh on this machine only.",
5
- "version": "0.5.0",
5
+ "version": "0.5.1",
6
6
  "permissions": [
7
7
  "tabs",
8
8
  "scripting",
@@ -23,6 +23,13 @@ let hostOrigin = 'http://127.0.0.1:21987'
23
23
  let socket = null
24
24
  let retryMs = 1000
25
25
  let closed = false
26
+ /**
27
+ * When the last live socket closed; 0 while connected. The worker only stays
28
+ * warm for a bounded window after this: a restarted dsh host takes seconds to
29
+ * bind its bridge, but a host that stays down for hours must not pin the
30
+ * service worker awake.
31
+ */
32
+ let disconnectedAt = 0
26
33
 
27
34
  function connect() {
28
35
  if (closed) return
@@ -42,6 +49,7 @@ function connect() {
42
49
  socket.onopen = () => {
43
50
  opened = true
44
51
  retryMs = 1000
52
+ disconnectedAt = 0
45
53
  hostOrigin = url.replace(/^ws/u, 'http').replace(/\/chrome-browser\/ext\/ws$/u, '')
46
54
  trace('open ' + url)
47
55
  hello()
@@ -54,6 +62,7 @@ function connect() {
54
62
  socket.onclose = (event) => {
55
63
  trace('close ' + (event && event.code !== undefined ? event.code : '?'))
56
64
  socket = null
65
+ disconnectedAt = nextDisconnectAt(opened, disconnectedAt, Date.now())
57
66
  // Nothing answered at this origin: advance before retrying, so a machine
58
67
  // running only the other profile connects without an edit. A socket that
59
68
  // did open keeps its origin — that host is the right one to reconnect to.
@@ -62,9 +71,76 @@ function connect() {
62
71
  }
63
72
  }
64
73
 
74
+ /**
75
+ * Record when the current warm-window started.
76
+ *
77
+ * Only a live socket loss starts the window: a failed attempt is evidence that
78
+ * the host is still down, not a new loss, and re-stamping it every attempt would
79
+ * keep the worker warm forever. Passing `previous` back also keeps a fresh
80
+ * worker cold until it has actually lost a connection.
81
+ * @param opened - whether the socket that closed had opened.
82
+ * @param previous - the loss time already recorded; 0 when none.
83
+ * @param now - close time, milliseconds.
84
+ * @returns the loss time to record.
85
+ */
86
+ function nextDisconnectAt(opened, previous, now) {
87
+ return opened ? now : previous
88
+ }
89
+
90
+ /**
91
+ * Plan one bridge retry.
92
+ *
93
+ * The worker stays warm (snappy retries, bounded interval) only shortly after
94
+ * losing a live connection: that is when a restarted host is booting. Past that
95
+ * window, retries fall back to the cold cadence and the 30s alarm drives
96
+ * reconnection, so a host that stays down does not keep the worker awake.
97
+ * @param disconnectedAt - when the last live socket closed; 0 while connected.
98
+ * @param attemptMs - the delay queued for this attempt.
99
+ * @param now - current time, milliseconds.
100
+ * @returns the delay to use now, whether to reset Chrome's idle clock, and the
101
+ * delay for the next attempt.
102
+ */
103
+ function retryPlan(disconnectedAt, attemptMs, now) {
104
+ const warm = disconnectedAt !== 0 && now - disconnectedAt < 300000
105
+ return { delay: attemptMs, warm, nextAttemptMs: Math.min(attemptMs * 2, warm ? 5000 : 15000) }
106
+ }
107
+
108
+ /**
109
+ * Reset Chrome's 30s idle clock.
110
+ *
111
+ * A suspended service worker cannot run its own retry timer, so without an
112
+ * extension API call the warm retries below die exactly when a slow host boot
113
+ * would have supplied the bridge. Calling an API resets the idle clock; this is
114
+ * deliberately limited to the warm window in {@link retryPlan}.
115
+ */
116
+ function touchWorker() {
117
+ try {
118
+ const pending = chrome.alarms.get('dsh-bridge')
119
+ if (pending !== undefined && typeof pending.catch === 'function') pending.catch(() => {})
120
+ } catch {
121
+ // No chrome.alarms in a plain test scope: the retry timer still runs.
122
+ }
123
+ }
124
+
65
125
  function scheduleRetry() {
66
- setTimeout(connect, retryMs)
67
- retryMs = Math.min(retryMs * 2, 15000)
126
+ const plan = retryPlan(disconnectedAt, retryMs, Date.now())
127
+ if (plan.warm) touchWorker()
128
+ setTimeout(connect, plan.delay)
129
+ retryMs = plan.nextAttemptMs
130
+ }
131
+
132
+ /**
133
+ * Wake the bridge on ordinary Chrome activity.
134
+ *
135
+ * A restarted host can only be found by an attempt; any tab switch, navigation
136
+ * or window focus wakes the suspended worker, so the bridge comes back as soon
137
+ * as the user touches Chrome instead of waiting for the 30s alarm.
138
+ */
139
+ function wakeBridge() {
140
+ // The same duplicate-socket guard `connect` applies: an in-flight connect or
141
+ // a live socket must not be replaced by a second one.
142
+ if (socket !== null && (socket.readyState === 0 || socket.readyState === 1)) return
143
+ connect()
68
144
  }
69
145
 
70
146
  // MV3 service workers are suspended by Chrome after idle. The browser
@@ -85,6 +161,11 @@ chrome.alarms.onAlarm.addListener(alarm => {
85
161
  })
86
162
  chrome.runtime.onStartup.addListener(() => connect())
87
163
  chrome.runtime.onInstalled.addListener(() => connect())
164
+ chrome.tabs.onActivated.addListener(() => wakeBridge())
165
+ chrome.tabs.onUpdated.addListener((_tabId, change) => {
166
+ if (change.status === 'complete' || typeof change.url === 'string') wakeBridge()
167
+ })
168
+ chrome.windows.onFocusChanged.addListener(() => wakeBridge())
88
169
 
89
170
  /**
90
171
  * Ops this worker answers, sent to the host on connect and on every heartbeat.
@@ -112,7 +193,7 @@ const SUPPORTED_OPS = [
112
193
  * user stayed on a stale extension with nothing to show for it. Only a value
113
194
  * carried by the loaded code can tell the host which code is loaded.
114
195
  */
115
- const BUILD_HASH = '6d025c12bba2e73376b264ef911282489baadba72c42e29cb419a5ec7ccdd407'
196
+ const BUILD_HASH = 'd842f7fb0da47b0025c90035ad7f5a5dc5fc8743acd4c7bc85f8b4b231be38f2'
116
197
 
117
198
  /** Introduce this worker: what it runs, what it can answer, and which code it is. */
118
199
  async function hello() {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@liyuera/dsh-chrome-browser",
3
3
  "description": "Chrome browser control for dsh: list the user's tabs, read page text or the actionable elements, navigate, click/hover/type, wait for elements, evaluate JS, capture screenshots and read console output through the extension bridge or the Chrome DevTools Protocol, plus an @ menu source listing the live tabs and a browser skill",
4
- "version": "0.6.3",
4
+ "version": "0.6.4",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",
7
7
  "types": "lib/index.d.ts",