dsh-live-browser 0.0.0-stage → 0.5.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/client.js ADDED
@@ -0,0 +1,1124 @@
1
+ /**
2
+ * dsh-live-browser client half.
3
+ *
4
+ * The right-sidebar browser you already see is DSH's own
5
+ * `@deepseek-ai/dsh-client-ui-sidebar-browser`: it owns the tab strip, the
6
+ * address bar, the guest <webview> and the browser session. This plugin adds the
7
+ * one thing that package cannot do — let the agent drive the page.
8
+ *
9
+ * How it attaches: the official plugin tags every guest with
10
+ * `data-sidebar-browser-frame="webview"`, so this bundle finds those elements in
11
+ * the live DOM, injects a command surface into each document, and polls the host
12
+ * for work. No DSH file is modified, so app upgrades cannot break it.
13
+ *
14
+ * Two command families, handled differently:
15
+ *
16
+ * - Page commands (click/type/snapshot/...) run *inside* the guest through the
17
+ * injected script.
18
+ * - Browser commands (navigate/back/forward/reload) drive the guest element
19
+ * itself, exactly the way the official plugin's toolbar does.
20
+ *
21
+ * Wrapped in an IIFE: DSH batches several plugin bundles into one combo
22
+ * <script>, so a top-level `const` here would collide with a sibling bundle.
23
+ */
24
+
25
+ ;(function () {
26
+ 'use strict'
27
+
28
+ /** Live guests are found by this attribute, owned by the official plugin. */
29
+ const GUEST_SELECTOR =
30
+ 'webview[data-sidebar-browser-frame="webview"], iframe[data-sidebar-browser-frame="iframe"]'
31
+
32
+ /**
33
+ * What to tell a user whose sidebar rendered the Web build's iframe.
34
+ *
35
+ * An iframe is cross-origin to the DSH page, so the parent cannot read its
36
+ * document or inject the agent script. That is a browser security boundary,
37
+ * not a missing feature in this plugin; the only full-capability path is the
38
+ * Desktop build, whose browser tab is an Electron <webview>.
39
+ */
40
+ const IFRAME_HINT =
41
+ '当前右侧栏打开的是 Web 版 iframe 浏览器,页面与 DSH 跨域,浏览器同源策略禁止插件注入脚本,点击/输入/读取/执行脚本都无法工作。' +
42
+ '请改用 DSH 桌面版的 webview 浏览器:1) 关闭浏览器里的 DSH 页面(地址形如 http://127.0.0.1:54249);' +
43
+ '2) 启动 DeepSeek Harness 桌面客户端(Windows 开始菜单里的 DeepSeek Harness,或 D:\\软件\\deepseekHARNESS\\DeepSeek Harness.exe);' +
44
+ '3) 在桌面客户端打开同一个会话,点右侧栏「浏览器」标签页。桌面版浏览器是 Electron <webview>,browser_* 工具即可正常操作。'
45
+
46
+ const guestKind = (element) =>
47
+ typeof element?.tagName === 'string' && element.tagName.toLowerCase() === 'iframe'
48
+ ? 'iframe'
49
+ : 'webview'
50
+
51
+ const isIframeGuest = (element) => guestKind(element) === 'iframe'
52
+
53
+ /** One per conversation: DSH keeps a sidebar mounted for each chat it visited. */
54
+ const SESSION_SELECTOR = '[data-sidebar-right-session]'
55
+
56
+ /**
57
+ * How long one poll request may be held open by the host.
58
+ *
59
+ * The old design woke the panel every 50 ms: every command paid an average
60
+ * 25 ms of pure hand-off latency and an idle panel generated 20 HTTP requests
61
+ * per second. A long poll removes both — the host answers the moment a
62
+ * command is queued. The value stays below the browser's request patience.
63
+ */
64
+ const POLL_WAIT_MS = 25000
65
+
66
+ /** Only used to retry after a failed poll, never on the happy path. */
67
+ const POLL_RETRY_MS = 250
68
+
69
+ /** Browser-level commands and the guest method that performs each. */
70
+ const GUEST_METHODS = { back: 'goBack', forward: 'goForward', reload: 'reload' }
71
+
72
+ /** Normalize a typed or model-supplied address. */
73
+ function normalizeAddress(raw) {
74
+ const text = String(raw ?? '').trim()
75
+ if (text === '') return undefined
76
+ // localhost / bare IP first: "localhost:5173" otherwise matches the scheme
77
+ // pattern and turns into a nonsense URL.
78
+ if (/^localhost(:\d+)?(\/|$)/i.test(text)) return `http://${text}`
79
+ if (/^\d{1,3}(\.\d{1,3}){3}(:\d+)?(\/|$)/.test(text)) return `http://${text}`
80
+ if (/^[a-z][a-z0-9+.-]*:/i.test(text)) return text
81
+ return `https://${text}`
82
+ }
83
+
84
+ /** Make a page-produced value safe to send back as JSON. */
85
+ function normalizeValue(value) {
86
+ if (value === undefined) return null
87
+ if (value === null || typeof value !== 'object') return value
88
+ try {
89
+ return JSON.parse(JSON.stringify(value))
90
+ } catch {
91
+ return String(value)
92
+ }
93
+ }
94
+
95
+ /** The bridge to the host half: ask for work, post results. */
96
+ function createRuntime() {
97
+ const channel = '/dsh-live-browser'
98
+ const call = async (endpoint, payload = {}) => {
99
+ const rpcId = `${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 10)}`
100
+ const response = await fetch(`${channel}/${endpoint}`, {
101
+ method: 'POST',
102
+ headers: { 'content-type': 'application/json' },
103
+ body: JSON.stringify({ type: 'client-request', rpcId, method: endpoint, payload }),
104
+ })
105
+ if (!response.ok) throw new Error(`浏览器桥接不可用:HTTP ${response.status}`)
106
+ const body = await response.json()
107
+ const result = body?.result
108
+ if (result?.ok === false) throw new Error(String(result?.error?.message ?? '浏览器操作失败'))
109
+ return result?.value
110
+ }
111
+ return {
112
+ // The panel announces which conversation it is rendering so the host can
113
+ // hand it only that conversation's commands.
114
+ poll: (panelId, sessionId) => call('poll', { panelId, sessionId, waitMs: POLL_WAIT_MS }),
115
+ settle: (panelId, id, outcome) => call('settle', { panelId, id, outcome }),
116
+ /** The injected script is served by the host half, so it always matches. */
117
+ source: () => call('source', {}).then((value) => String(value?.source ?? '')),
118
+ /** Behaviour flags the host owns; read right before an auto-open. */
119
+ config: () => call('config', {}),
120
+ /** Tab management is a host concern: only it can drive the sidebar. */
121
+ tabCommand: (action, params) => call('tabCommand', { action, params }),
122
+ /** Read a local file the agent wants to attach to a file input. */
123
+ readUpload: (path) => call('readUpload', { path }),
124
+ }
125
+ }
126
+
127
+ /**
128
+ * Drive whichever sidebar-browser guests exist.
129
+ *
130
+ * @param {{ bridge: ReturnType<typeof createRuntime>, loadSource: () => Promise<string>, openPanel?: (url?: string, sessionId?: string) => Promise<object>, documentRef?: Document, currentSessionId?: () => string | undefined }} options
131
+ */
132
+ function createDriver({ bridge, loadSource, openPanel, documentRef, currentSessionId, listSessions }) {
133
+ /** Stable prefix; each conversation gets its own panel id below. */
134
+ const basePanelId = `panel-${Math.random().toString(36).slice(2, 10)}`
135
+ /** @type {Map<Element, {ready: boolean, installed: boolean, url: string, failure?: string}>} */
136
+ const known = new Map()
137
+ /**
138
+ * One long-poll per conversation.
139
+ *
140
+ * A single poller used to follow whatever chat was on screen, so switching
141
+ * chats silently re-bound the only panel and commands queued for the other
142
+ * chat were rejected. DSH keeps each chat's sidebar mounted; giving every
143
+ * mounted sidebar its own panel keeps background conversations working.
144
+ *
145
+ * @type {Map<string, {panelId: string, timer: any, lastSeen: number, stopped: boolean}>}
146
+ */
147
+ const workers = new Map()
148
+ let source = ''
149
+ let stopped = false
150
+ let discoveryTimer
151
+
152
+ const scope = () => documentRef ?? (typeof document === 'undefined' ? undefined : document)
153
+
154
+ /** One small, dismissible notice for the Web build's iframe browser. */
155
+ let iframeNotice
156
+ let iframeNoticeDismissed = false
157
+ const hideIframeNotice = () => {
158
+ if (iframeNotice !== undefined && iframeNotice.isConnected) iframeNotice.remove()
159
+ iframeNotice = undefined
160
+ }
161
+ const dismissIframeNotice = () => {
162
+ iframeNoticeDismissed = true
163
+ hideIframeNotice()
164
+ }
165
+ const showIframeNotice = () => {
166
+ if (iframeNoticeDismissed) return
167
+ if (iframeNotice !== undefined && iframeNotice.isConnected) return
168
+ const doc = scope()
169
+ if (doc === undefined || doc.body === undefined || doc.body === null) return
170
+ const notice = doc.createElement('div')
171
+ notice.dataset.dshLiveBrowserIframeNotice = ''
172
+ notice.setAttribute('role', 'status')
173
+ notice.style.cssText =
174
+ 'position:fixed;right:16px;bottom:16px;z-index:2147483647;max-width:440px;' +
175
+ 'padding:12px 14px;border:1px solid #ffd591;border-radius:8px;background:#fff7e6;' +
176
+ 'color:#613400;box-shadow:0 6px 20px rgba(0,0,0,.16);font:13px/1.55 system-ui,sans-serif;'
177
+ const text = doc.createElement('div')
178
+ text.textContent =
179
+ '当前右侧栏是 iframe 浏览器,插件无法执行点击/输入/读取。请改用 DSH 桌面版 webview:' +
180
+ '关闭浏览器里的 DSH 页面 → 启动 DeepSeek Harness 桌面客户端 → 打开同一会话 → 点右侧栏「浏览器」。'
181
+ const close = doc.createElement('button')
182
+ close.type = 'button'
183
+ close.textContent = '知道了'
184
+ close.style.cssText =
185
+ 'margin-top:8px;padding:3px 10px;border:1px solid #ffd591;border-radius:5px;' +
186
+ 'background:#fff;color:#613400;font:12px system-ui,sans-serif;cursor:pointer;'
187
+ close.addEventListener('click', dismissIframeNotice)
188
+ notice.append(text, close)
189
+ doc.body.appendChild(notice)
190
+ iframeNotice = notice
191
+ }
192
+
193
+ /**
194
+ * The sidebar the human is looking at.
195
+ *
196
+ * Re-read on every tick rather than cached: switching chats swaps which
197
+ * session view is shown without this component remounting, and a stale
198
+ * element would let one chat's command run against another chat's page.
199
+ */
200
+ const view = () => foregroundSessionView(scope())
201
+
202
+ /**
203
+ * The conversation this panel is showing right now.
204
+ *
205
+ * The id and the guests are read from that same view, so the pair can never
206
+ * disagree: whatever gets reported belongs to the sidebar the agent will act
207
+ * on. Without a mounted sidebar the shell's current conversation is the only
208
+ * source left, and it is exactly where a cold start has to open the tab.
209
+ */
210
+ const currentSession = () => {
211
+ // The shell's own selection is authoritative for the visible tab. It is
212
+ // only used to decide which sidebar is on screen (and to make sure the
213
+ // visible chat has a worker before its first browser tab exists); every
214
+ // worker below polls for a fixed session id, so background chats keep
215
+ // working while the user is looking elsewhere.
216
+ const current = currentSessionId?.()
217
+ if (current !== undefined) return current
218
+ const shown = view()
219
+ if (shown !== undefined) return viewSessionId(shown)
220
+ return undefined
221
+ }
222
+
223
+ /**
224
+ * The sidebar of one conversation, or the one on screen when none is named.
225
+ *
226
+ * A command names its conversation; the guest has to come from that same
227
+ * chat's sidebar even when the user has switched to another one meanwhile.
228
+ */
229
+ const viewsFor = (target) => {
230
+ const all = sessionViews(scope())
231
+ if (target === undefined) {
232
+ const shown = view()
233
+ return shown === undefined ? [] : [shown]
234
+ }
235
+ return all.filter((candidate) => viewSessionId(candidate) === target)
236
+ }
237
+
238
+ const viewFor = (target) => {
239
+ const candidates = viewsFor(target)
240
+ // The same conversation can expose more than one
241
+ // [data-sidebar-right-session] node (the session wrapper and the panel
242
+ // itself, which may be portaled). Prefer the one that actually holds the
243
+ // guest, otherwise a wrapper without a browser would shadow the panel.
244
+ return (
245
+ candidates.find((candidate) => candidate.querySelector(GUEST_SELECTOR) !== null) ??
246
+ candidates[0]
247
+ )
248
+ }
249
+
250
+ const findGuests = (target) => {
251
+ const guests = []
252
+ for (const candidate of viewsFor(target)) {
253
+ guests.push(...candidate.querySelectorAll(GUEST_SELECTOR))
254
+ }
255
+ return [...new Set(guests)]
256
+ }
257
+
258
+ /**
259
+ * Whether the settings still allow opening the sidebar browser by ourselves.
260
+ *
261
+ * Read per attempt rather than cached: the setting lives on the host and can
262
+ * change without reloading the page. A failed read keeps the documented
263
+ * default (allowed), so a transient host hiccup does not disable the feature.
264
+ */
265
+ const canAutoOpen = async () => {
266
+ try {
267
+ return (await bridge.config())?.autoOpenPanel !== false
268
+ } catch {
269
+ return true
270
+ }
271
+ }
272
+
273
+ /**
274
+ * The guest the agent should act on: the one holding focus, else the last
275
+ * one that is actually laid out.
276
+ *
277
+ * Every visited tab keeps its body mounted (`keepMounted`), and DSH hides the
278
+ * inactive ones with the `hidden` attribute. Document order therefore says
279
+ * nothing about what the human sees: the last mounted guest is often a tab
280
+ * that is not on screen.
281
+ */
282
+ const targetGuest = (target) => {
283
+ const all = findGuests(target)
284
+ if (all.length === 0) return undefined
285
+ // A desktop build may have both kinds mounted while switching profiles.
286
+ // Prefer a webview: it is the one this plugin can actually drive.
287
+ const guests = all.some((element) => !isIframeGuest(element))
288
+ ? all.filter((element) => !isIframeGuest(element))
289
+ : all
290
+ const doc = scope()
291
+ const focused = guests.find((element) => element === doc?.activeElement)
292
+ if (focused !== undefined) return focused
293
+ const onScreen = guests.filter(isLaidOut)
294
+ if (onScreen.length > 0) return onScreen[onScreen.length - 1]
295
+ return guests[guests.length - 1]
296
+ }
297
+
298
+ /**
299
+ * Wait for a usable guest to appear, tracking anything new on the way.
300
+ *
301
+ * @param {number} timeoutMs how long to wait
302
+ * @param {string} [target] conversation whose sidebar to search
303
+ * @returns {Promise<Element | undefined>} a ready guest, or undefined
304
+ */
305
+ const waitForGuest = async (timeoutMs, target) => {
306
+ const deadline = Date.now() + timeoutMs
307
+ while (Date.now() < deadline) {
308
+ for (const candidate of findGuests(target)) {
309
+ if (!known.has(candidate)) track(candidate)
310
+ }
311
+ const candidates = findGuests(target)
312
+ const ready =
313
+ candidates.find(
314
+ (candidate) => !isIframeGuest(candidate) && known.get(candidate)?.ready === true,
315
+ ) ?? candidates.find((candidate) => known.get(candidate)?.ready === true)
316
+ if (ready !== undefined) return ready
317
+ await new Promise((resolve) => setTimeout(resolve, 100))
318
+ }
319
+ return undefined
320
+ }
321
+
322
+ /** Track a guest element and notice each new document it loads. */
323
+ const track = (element) => {
324
+ const state = { ready: false, installed: false, url: '', failure: undefined }
325
+ known.set(element, state)
326
+ if (isIframeGuest(element)) {
327
+ // An iframe has none of the <webview> methods. Its href is readable from
328
+ // the parent even when its document is not; that is enough to report
329
+ // "this tab is an iframe" and hand the caller the Desktop hint.
330
+ const refreshFrame = () => {
331
+ try {
332
+ state.url = typeof element.src === 'string' ? element.src : ''
333
+ } catch {
334
+ state.url = ''
335
+ }
336
+ state.ready = true
337
+ state.installed = false
338
+ state.failure = undefined
339
+ }
340
+ element.addEventListener('load', refreshFrame)
341
+ refreshFrame()
342
+ return
343
+ }
344
+ const refresh = () => {
345
+ try {
346
+ state.url = element.getURL()
347
+ state.ready = state.url !== ''
348
+ } catch {
349
+ /* the element is between documents */
350
+ }
351
+ // A new document means the injected surface is gone.
352
+ state.installed = false
353
+ state.failure = undefined
354
+ }
355
+ element.addEventListener('dom-ready', refresh)
356
+ element.addEventListener('did-navigate', refresh)
357
+ element.addEventListener('did-navigate-in-page', refresh)
358
+ element.addEventListener('did-start-navigation', () => { state.installed = false })
359
+ // A page that never loads otherwise reports "still loading, retry later"
360
+ // forever: the guest emits this instead of ever becoming ready.
361
+ element.addEventListener('did-fail-load', (event) => {
362
+ // ERR_ABORTED (-3) is what every ordinary redirect produces, so it is not
363
+ // a failure worth reporting.
364
+ if (event?.errorCode === -3) return
365
+ state.failure = String(event?.errorDescription ?? `加载失败(${event?.errorCode ?? '?'})`)
366
+ state.ready = false
367
+ state.installed = false
368
+ })
369
+ try {
370
+ if (typeof element.getURL === 'function') refresh()
371
+ } catch {
372
+ /* not attached yet */
373
+ }
374
+ }
375
+
376
+ /**
377
+ * Inject the command surface, skipping the work when it is already there.
378
+ *
379
+ * The bundle is ~29 KB of source. After a same-document navigation the
380
+ * global survives, so running a one-line probe first avoids re-sending and
381
+ * re-evaluating the whole script on every SPA route change.
382
+ */
383
+ const ensureInstalled = async (element) => {
384
+ // There is no cross-origin scripting surface for an iframe. Returning
385
+ // false lets `state`/`tabs` answer with the iframe description instead of
386
+ // pretending the page is merely loading.
387
+ if (isIframeGuest(element)) return false
388
+ const state = known.get(element)
389
+ if (state === undefined) return false
390
+ if (state.installed) return true
391
+ if (source === '') return false
392
+ let present = false
393
+ try {
394
+ present = await element.executeJavaScript(
395
+ "typeof window.__dshBrowserAgent === 'object' && typeof window.__dshBrowserAgent.state === 'function'",
396
+ false,
397
+ )
398
+ } catch {
399
+ present = false
400
+ }
401
+ if (present !== true) await element.executeJavaScript(source, false)
402
+ state.installed = true
403
+ return true
404
+ }
405
+
406
+ const describeGuest = (element) => {
407
+ if (isIframeGuest(element)) {
408
+ let sameOrigin = false
409
+ try {
410
+ sameOrigin = element.contentDocument !== null
411
+ } catch {
412
+ sameOrigin = false
413
+ }
414
+ return {
415
+ kind: 'iframe',
416
+ mode: 'iframe',
417
+ actionRequired: 'switch-to-desktop-webview',
418
+ id: typeof element.src === 'string' ? element.src : '',
419
+ url: typeof element.src === 'string' ? element.src : '',
420
+ title: element.getAttribute('title') ?? '',
421
+ canGoBack: false,
422
+ canGoForward: false,
423
+ loading: false,
424
+ sameOrigin,
425
+ automation: false,
426
+ hint: IFRAME_HINT,
427
+ }
428
+ }
429
+ const state = known.get(element)
430
+ let url = state?.url ?? ''
431
+ let title = ''
432
+ let canGoBack = false
433
+ let canGoForward = false
434
+ let loading = false
435
+ try {
436
+ url = element.getURL() || url
437
+ title = element.getTitle()
438
+ canGoBack = element.canGoBack()
439
+ canGoForward = element.canGoForward()
440
+ loading = element.isLoading()
441
+ } catch {
442
+ /* the element is between documents */
443
+ }
444
+ return { kind: 'webview', id: url, url, title, canGoBack, canGoForward, loading }
445
+ }
446
+
447
+ /** Perform one command on the chosen guest. */
448
+ const dispatch = async (element, command, target) => {
449
+ const method = String(command.method)
450
+
451
+ // Tab management goes through the host: the sidebar owns the tab strip, and
452
+ // a guest element cannot create or close its siblings.
453
+ if (method === 'tabNew' || method === 'tabClose' || method === 'tabActivate') {
454
+ return bridge.tabCommand(method, command.params ?? {})
455
+ }
456
+
457
+ // A Web-build iframe has no scriptable guest. It can still answer what it
458
+ // is (so browser_status can explain the situation), but every command that
459
+ // needs the page must stop with the Desktop/webview hint instead of
460
+ // reporting a generic "still loading".
461
+ if (isIframeGuest(element)) {
462
+ if (method === 'state') return describeGuest(element)
463
+ if (method === 'tabs') return { tabs: findGuests(target).map(describeGuest) }
464
+ throw new Error(IFRAME_HINT)
465
+ }
466
+
467
+ // Browser-level commands drive the element, like the toolbar does.
468
+ if (method in GUEST_METHODS) {
469
+ element[GUEST_METHODS[method]]()
470
+ return describeGuest(element)
471
+ }
472
+ if (method === 'navigate') {
473
+ const url = normalizeAddress(command.params?.url)
474
+ if (url === undefined) throw new Error('地址为空')
475
+ await element.loadURL(url)
476
+ return describeGuest(element)
477
+ }
478
+ if (method === 'tabs') {
479
+ return { tabs: findGuests(target).map(describeGuest) }
480
+ }
481
+ if (method === 'state') {
482
+ const guest = describeGuest(element ?? targetGuest(target))
483
+ // Page state adds the viewport and scroll offset.
484
+ if (await ensureInstalled(element)) {
485
+ const page = await element.executeJavaScript(
486
+ `window.__dshBrowserAgent.state()`,
487
+ true,
488
+ )
489
+ return { ...guest, ...normalizeValue(page) }
490
+ }
491
+ return guest
492
+ }
493
+
494
+ // Everything else runs inside the page.
495
+ if (!(await ensureInstalled(element))) {
496
+ throw new Error('无法在页面里注入操作脚本(页面可能禁止脚本执行)')
497
+ }
498
+ const params = { ...(command.params ?? {}) }
499
+ const expression = `(async () => { ${source}; return await window.__dshBrowserAgent[${JSON.stringify(method)}](${JSON.stringify(params)}); })()`
500
+ return element.executeJavaScript(expression, true)
501
+ }
502
+
503
+ /** Refresh the iframe notice for the sidebar currently on screen. */
504
+ const updateIframeNotice = (shownSession) => {
505
+ const guests = shownSession === undefined ? [] : findGuests(shownSession)
506
+ const hasWebview = guests.some((element) => !isIframeGuest(element))
507
+ const iframe = hasWebview ? undefined : guests.find((element) => isIframeGuest(element))
508
+ if (iframe === undefined) {
509
+ iframeNoticeDismissed = false
510
+ hideIframeNotice()
511
+ } else {
512
+ showIframeNotice()
513
+ }
514
+ }
515
+
516
+ const stopWorker = (sessionId) => {
517
+ const worker = workers.get(sessionId)
518
+ if (worker === undefined) return
519
+ worker.stopped = true
520
+ if (worker.timer !== undefined) clearTimeout(worker.timer)
521
+ workers.delete(sessionId)
522
+ }
523
+
524
+ /**
525
+ * One request loop for one conversation.
526
+ *
527
+ * The panel is bound to `sessionId` for its whole life. It never reads the
528
+ * chat currently on screen, so switching chats cannot steal or reject a
529
+ * command that belongs to this conversation.
530
+ */
531
+ const tickFor = async (sessionId, worker) => {
532
+ for (const element of findGuests(sessionId)) {
533
+ if (!known.has(element)) track(element)
534
+ }
535
+ for (const element of [...known.keys()]) {
536
+ if (!element.isConnected) known.delete(element)
537
+ }
538
+
539
+ let command
540
+ try {
541
+ command = await bridge.poll(worker.panelId, sessionId)
542
+ } catch {
543
+ // A failed long poll (host restarting, page hidden) backs off once.
544
+ return true
545
+ }
546
+ if (command === null || command === undefined) return false
547
+
548
+ if (command.sessionId !== undefined && command.sessionId !== sessionId) {
549
+ await bridge.settle(worker.panelId, command.id, {
550
+ ok: false,
551
+ error: `这条命令属于对话 ${command.sessionId},当前面板绑定的是 ${sessionId};已拒绝,避免操作到错误页面。`,
552
+ })
553
+ return false
554
+ }
555
+ const target = sessionId
556
+
557
+ // Explicit opener: create the browser tab in this conversation, even
558
+ // while a different conversation is on screen.
559
+ if (command.method === 'openPanel') {
560
+ if (!(await canAutoOpen())) {
561
+ await bridge.settle(worker.panelId, command.id, {
562
+ ok: false,
563
+ error:
564
+ '当前对话的右侧栏还没有网页,且插件设置里关闭了「自动打开面板」;请先手动打开「浏览器」标签页后重试',
565
+ })
566
+ return false
567
+ }
568
+ if (openPanel === undefined) {
569
+ await bridge.settle(worker.panelId, command.id, {
570
+ ok: false,
571
+ error: '右侧栏还没有打开任何网页,并且当前环境无法自动打开浏览器',
572
+ })
573
+ return false
574
+ }
575
+ try {
576
+ await openPanel(command.params?.url, target)
577
+ } catch (failure) {
578
+ await bridge.settle(worker.panelId, command.id, {
579
+ ok: false,
580
+ error: `无法自动打开侧边栏浏览器:${String(failure?.message ?? failure)}`,
581
+ })
582
+ return false
583
+ }
584
+ const opened = await waitForGuest(8000, target)
585
+ await bridge.settle(worker.panelId, command.id, {
586
+ ok: true,
587
+ value: { opened: true, hasPage: opened !== undefined },
588
+ })
589
+ return false
590
+ }
591
+
592
+ let element = targetGuest(target)
593
+ if (element === undefined) {
594
+ // `state`/`tabs` are status probes; they must not create tabs.
595
+ if (command.method === 'state') {
596
+ await bridge.settle(worker.panelId, command.id, { ok: true, value: null })
597
+ return false
598
+ }
599
+ if (command.method === 'tabs') {
600
+ await bridge.settle(worker.panelId, command.id, { ok: true, value: { tabs: [] } })
601
+ return false
602
+ }
603
+ if (!(await canAutoOpen())) {
604
+ await bridge.settle(worker.panelId, command.id, {
605
+ ok: false,
606
+ error:
607
+ '当前对话的右侧栏还没有网页,且插件设置里关闭了「自动打开面板」;请先手动打开「浏览器」标签页后重试',
608
+ })
609
+ return false
610
+ }
611
+ if (openPanel === undefined) {
612
+ await bridge.settle(worker.panelId, command.id, {
613
+ ok: false,
614
+ error: '右侧栏还没有打开任何网页,并且当前环境无法自动打开浏览器',
615
+ })
616
+ return false
617
+ }
618
+ try {
619
+ await openPanel(command.method === 'navigate' ? command.params?.url : undefined, target)
620
+ } catch (failure) {
621
+ await bridge.settle(worker.panelId, command.id, {
622
+ ok: false,
623
+ error: `无法自动打开侧边栏浏览器:${String(failure?.message ?? failure)}`,
624
+ })
625
+ return false
626
+ }
627
+ element = await waitForGuest(8000, target)
628
+ if (element === undefined) {
629
+ await bridge.settle(worker.panelId, command.id, {
630
+ ok: false,
631
+ error: '侧边栏浏览器已打开,但页面还没准备好,请稍后重试',
632
+ })
633
+ return false
634
+ }
635
+ }
636
+
637
+ const state = known.get(element)
638
+ if (state !== undefined && state.failure !== undefined) {
639
+ // Say what actually went wrong instead of "still loading".
640
+ await bridge.settle(worker.panelId, command.id, {
641
+ ok: false,
642
+ error: `页面加载失败:${state.failure}`,
643
+ })
644
+ return false
645
+ }
646
+ if (state === undefined || !state.ready) {
647
+ await bridge.settle(worker.panelId, command.id, {
648
+ ok: false,
649
+ error: '页面还在加载,请稍后重试',
650
+ })
651
+ return false
652
+ }
653
+
654
+ try {
655
+ const value = await dispatch(element, command, target)
656
+ await bridge.settle(worker.panelId, command.id, {
657
+ ok: true,
658
+ value: normalizeValue(value),
659
+ })
660
+ } catch (failure) {
661
+ await bridge.settle(worker.panelId, command.id, {
662
+ ok: false,
663
+ error: String(failure?.message ?? failure),
664
+ })
665
+ }
666
+ return false
667
+ }
668
+
669
+ const startWorker = (sessionId) => {
670
+ const worker = {
671
+ panelId: `${basePanelId}-${sessionId}`,
672
+ timer: undefined,
673
+ lastSeen: Date.now(),
674
+ stopped: false,
675
+ }
676
+ const loop = async () => {
677
+ if (stopped || worker.stopped) return
678
+ let retry = false
679
+ try {
680
+ retry = await tickFor(sessionId, worker)
681
+ } catch {
682
+ retry = true
683
+ }
684
+ if (stopped || worker.stopped) return
685
+ worker.timer = setTimeout(() => {
686
+ void loop()
687
+ }, retry ? POLL_RETRY_MS : 0)
688
+ }
689
+ void loop()
690
+ return worker
691
+ }
692
+
693
+ /**
694
+ * Keep one worker per mounted conversation.
695
+ *
696
+ * A conversation whose sidebar is hidden is still mounted by DSH; its
697
+ * worker keeps polling, which is what lets a command finish there while the
698
+ * user is reading another chat.
699
+ */
700
+ const syncWorkers = () => {
701
+ if (stopped) return
702
+ const visible = currentSession()
703
+ const ids = new Set()
704
+ if (visible !== undefined) ids.add(visible)
705
+ for (const view of sessionViews(scope())) {
706
+ const id = viewSessionId(view)
707
+ if (id !== undefined) ids.add(id)
708
+ }
709
+ // A conversation with no browser tab has no sidebar to discover. Include
710
+ // every live session so a cold-start navigate can create its tab even
711
+ // while another conversation is on screen.
712
+ try {
713
+ for (const session of listSessions?.() ?? []) {
714
+ const id = typeof session === 'string' ? session : session?.id
715
+ if (typeof id === 'string' && id !== '') ids.add(id)
716
+ }
717
+ } catch {
718
+ // Optional service; the DOM/current session still cover the common case.
719
+ }
720
+ for (const id of ids) {
721
+ let worker = workers.get(id)
722
+ if (worker === undefined) {
723
+ worker = startWorker(id)
724
+ workers.set(id, worker)
725
+ }
726
+ worker.lastSeen = Date.now()
727
+ }
728
+ // A closed chat leaves the DOM immediately but its worker may still hold
729
+ // a queued command for a few seconds; keep it through that grace period.
730
+ for (const [id, worker] of [...workers]) {
731
+ if (!ids.has(id) && Date.now() - worker.lastSeen > 30000) stopWorker(id)
732
+ }
733
+ updateIframeNotice(visible)
734
+ }
735
+
736
+ return {
737
+ async start() {
738
+ // The host may be restarting while the page reloads; keep asking for the
739
+ // injected script instead of leaving the panel dead until a manual reload.
740
+ while (!stopped) {
741
+ try {
742
+ source = await loadSource()
743
+ break
744
+ } catch {
745
+ await new Promise((resolve) => setTimeout(resolve, 1000))
746
+ }
747
+ }
748
+ if (stopped) return
749
+ syncWorkers()
750
+ discoveryTimer = setInterval(syncWorkers, 500)
751
+ },
752
+ stop() {
753
+ stopped = true
754
+ if (discoveryTimer !== undefined) clearInterval(discoveryTimer)
755
+ for (const id of [...workers.keys()]) stopWorker(id)
756
+ known.clear()
757
+ iframeNoticeDismissed = false
758
+ hideIframeNotice()
759
+ },
760
+ }
761
+ }
762
+
763
+ /**
764
+ * Every sidebar currently in the document.
765
+ *
766
+ * DSH marks each conversation's sidebar wrapper with
767
+ * `data-sidebar-right-session` — the same attribute the shipped sidebar uses to
768
+ * validate its own elements. It keeps one sidebar per conversation: the selected
769
+ * one plus every background chat that still holds `keepMounted` content, and
770
+ * the browser tab is one of those. They are ordered by session id, not by what
771
+ * is on screen.
772
+ *
773
+ * Never throws: this runs on every poll tick, and an unexpected DOM shape must
774
+ * degrade to "no sidebar" rather than break the loop.
775
+ *
776
+ * @param {Document | undefined} documentRef
777
+ * @returns {Element[]}
778
+ */
779
+ function sessionViews(documentRef) {
780
+ if (documentRef === undefined || documentRef === null) return []
781
+ try {
782
+ if (typeof documentRef.querySelectorAll !== 'function') return []
783
+ return [...documentRef.querySelectorAll(SESSION_SELECTOR)]
784
+ } catch {
785
+ return []
786
+ }
787
+ }
788
+
789
+ /**
790
+ * Which conversation one sidebar belongs to.
791
+ *
792
+ * @param {Element | undefined} view
793
+ * @returns {string | undefined}
794
+ */
795
+ function viewSessionId(view) {
796
+ const id = view?.dataset?.sidebarRightSession
797
+ return typeof id === 'string' && id !== '' ? id : undefined
798
+ }
799
+
800
+ /**
801
+ * The sidebar the human is looking at.
802
+ *
803
+ * DSH keeps the background conversations' sidebars mounted so their pages stay
804
+ * alive, and marks those `hidden`. Reading the first
805
+ * `[data-sidebar-right-session]` instead picked the alphabetically first
806
+ * session, which is why the agent could drive a page from another chat.
807
+ *
808
+ * @param {Document | undefined} documentRef
809
+ * @returns {Element | undefined}
810
+ */
811
+ function foregroundSessionView(documentRef) {
812
+ const views = sessionViews(documentRef).filter((view) => {
813
+ if (typeof view.hasAttribute === 'function' && view.hasAttribute('hidden')) return false
814
+ if (typeof view.closest === 'function' && view.closest('[hidden]') !== null) return false
815
+ // Background sessions may be hidden with CSS rather than the `hidden`
816
+ // attribute, and a portaled panel can keep a non-zero box there. A view
817
+ // that is not laid out is not the one the human is looking at.
818
+ return isLaidOut(view)
819
+ })
820
+ const active = documentRef?.activeElement
821
+ const focused =
822
+ active === undefined || active === null
823
+ ? undefined
824
+ : views.find((view) => typeof view.contains === 'function' && view.contains(active))
825
+ return focused ?? views[0]
826
+ }
827
+
828
+ /**
829
+ * Whether a guest has a box on screen.
830
+ *
831
+ * Inactive tabs stay mounted but hidden, so their guests keep existing while the
832
+ * human cannot see them. A laid-out box is the only reliable signal.
833
+ *
834
+ * @param {Element} element
835
+ */
836
+ function isLaidOut(element) {
837
+ try {
838
+ if (typeof element.closest === 'function' && element.closest('[hidden]') !== null) return false
839
+ const rect = element.getBoundingClientRect()
840
+ return rect.width > 1 && rect.height > 1
841
+ } catch {
842
+ return false
843
+ }
844
+ }
845
+
846
+ /**
847
+ * Ask the sidebar to show the browser tab in one specific conversation.
848
+ *
849
+ * `openTab` targets whichever Session is on screen; `openTabIn` targets the
850
+ * Session that asked. Using the latter is what keeps a browser in the chat
851
+ * that requested it when the user switches chats mid-request.
852
+ *
853
+ * @param {object} ctx cordis client context
854
+ * @param {string | undefined} sessionId conversation that asked
855
+ * @param {string} [url] address to open in the new tab
856
+ */
857
+ async function openSidebarBrowser(ctx, sessionId, url) {
858
+ const service = ctx.reflect?.get?.('sidebarRight') ?? ctx.get?.('sidebarRight')
859
+ if (service === undefined || service === null) {
860
+ throw new Error('拿不到侧边栏服务:这个 DSH 版本可能没有内置浏览器')
861
+ }
862
+ const options = url === undefined ? {} : { params: { url } }
863
+ const canTarget = typeof service.openTabIn === 'function'
864
+ if (canTarget && sessionId !== undefined) {
865
+ service.openTabIn(sessionId, 'browser', options)
866
+ return { opened: true, sessionId }
867
+ }
868
+ if (typeof service.openTab === 'function') {
869
+ service.openTab('browser', options)
870
+ return { opened: true, sessionId: undefined }
871
+ }
872
+ throw new Error('侧边栏服务没有 openTab,无法自动打开浏览器')
873
+ }
874
+
875
+ /**
876
+ * The conversation the shell is showing.
877
+ *
878
+ * Only needed while no sidebar is mounted: the tab a cold start opens has to
879
+ * land in the chat on screen, and the DOM has no marker to read yet.
880
+ *
881
+ * @param {object} ctx cordis client context
882
+ * @returns {string | undefined}
883
+ */
884
+ function currentSessionId(ctx) {
885
+ try {
886
+ const current = ctx.get?.('uiSession')?.adapter?.current
887
+ const key = current?.getSnapshot?.()?.key
888
+ return typeof key === 'string' && key !== '' ? key : undefined
889
+ } catch {
890
+ return undefined
891
+ }
892
+ }
893
+
894
+ /** Escape a value for an attribute selector (tab ids are plain, but be safe). */
895
+ function attributeValue(value) {
896
+ return String(value).replace(/["\\]/g, '\\$&')
897
+ }
898
+
899
+ /**
900
+ * Ids of the tabs in the strip that holds `tabId`, in the order the human sees.
901
+ *
902
+ * The menu is opened from a chip in the tab strip; reading that strip is the
903
+ * only way to know what "to the right" means, because the sidebar keeps its
904
+ * tab order internal. Returns [] when the tab is not in a strip (floating pane).
905
+ *
906
+ * @param {Document | undefined} documentRef
907
+ * @param {string} tabId
908
+ * @returns {string[]}
909
+ */
910
+ function tabStripIds(documentRef, tabId) {
911
+ if (documentRef === undefined || documentRef === null || tabId === undefined) return []
912
+ try {
913
+ if (typeof documentRef.querySelector !== 'function') return []
914
+ const chip = documentRef.querySelector(`[data-dockkit-tab="${attributeValue(tabId)}"]`)
915
+ const strip = chip?.closest?.('[data-dockkit-strip-tabs]')
916
+ if (strip === undefined || strip === null) return []
917
+ return [...strip.querySelectorAll('[data-dockkit-tab]')]
918
+ .map((element) => element.dataset?.dockkitTab)
919
+ .filter((id) => typeof id === 'string' && id !== '')
920
+ } catch {
921
+ return []
922
+ }
923
+ }
924
+
925
+ /**
926
+ * Which tabs each menu action closes, for the tab the menu was opened on.
927
+ *
928
+ * "To the right" excludes the tab itself, which is the convention every editor
929
+ * follows; "all" includes it, and the sidebar refuses to close the last guide
930
+ * tab on its own.
931
+ *
932
+ * @param {string[]} ids tab ids in strip order
933
+ * @param {string} currentId
934
+ * @returns {{others: string[], all: string[], right: string[]}}
935
+ */
936
+ function closingPlan(ids, currentId) {
937
+ const index = ids.indexOf(currentId)
938
+ return {
939
+ others: ids.filter((id) => id !== currentId),
940
+ all: [...ids],
941
+ right: index === -1 ? [] : ids.slice(index + 1),
942
+ }
943
+ }
944
+
945
+ /** Close one set of tabs through the sidebar controller, then hide the menu. */
946
+ function closeTabs(ctx, ids, dismiss) {
947
+ const service = ctx.reflect?.get?.('sidebarRight') ?? ctx.get?.('sidebarRight')
948
+ if (service === undefined || service === null || typeof service.close !== 'function') return
949
+ for (const id of ids) {
950
+ try {
951
+ service.close(id)
952
+ } catch {
953
+ /* the tab may already be gone; closing the rest still makes sense */
954
+ }
955
+ }
956
+ if (typeof dismiss === 'function') dismiss()
957
+ }
958
+
959
+ /** Styles that make our entries indistinguishable from the sidebar's own. */
960
+ const TAB_MENU_STYLE = `
961
+ .dsh-live-browser-menu-group{display:contents}
962
+ .dsh-live-browser-menu-item{padding:4px 7px;color:var(--dsw-alias-label-primary);font-size:calc(var(--dsh-content-font-size-secondary, 13px) - 1px);text-align:left;background:transparent;border:none;border-radius:var(--dsw-radius-md);cursor:pointer}
963
+ .dsh-live-browser-menu-item:hover:not(:disabled){background:var(--dsw-alias-interactive-bg-hover)}
964
+ .dsh-live-browser-menu-item:disabled{opacity:.45;cursor:default}
965
+ `
966
+
967
+ /** Marker on our group, so the two entry points never double up. */
968
+ const TAB_MENU_MARK = 'data-dsh-live-browser-menu'
969
+
970
+ /**
971
+ * Add the three close actions to the tab menu that just opened.
972
+ *
973
+ * The shipped sidebar exposes a React slot for extra menu entries, but that
974
+ * registration has to win a race against the sidebar declaring the slot, and
975
+ * when it loses it silently does nothing. The menu itself is a stable portal
976
+ * (`[data-dockkit-tab-menu]`), so extending it here works on every build while
977
+ * still leaving DSH unpatched.
978
+ *
979
+ * @param {object} ctx cordis client context
980
+ * @param {Element | undefined} menu the open tab menu
981
+ * @param {string | undefined} tabId the tab whose menu this is
982
+ */
983
+ function decorateTabMenu(ctx, menu, tabId) {
984
+ if (menu === undefined || menu === null || tabId === undefined) return
985
+ if (typeof menu.querySelector !== 'function') return
986
+ if (menu.querySelector(`[${TAB_MENU_MARK}]`) !== null) return
987
+
988
+ const doc = menu.ownerDocument ?? document
989
+ const plan = closingPlan(tabStripIds(doc, tabId), tabId)
990
+ const group = doc.createElement('div')
991
+ group.className = 'dsh-live-browser-menu-group'
992
+ group.setAttribute(TAB_MENU_MARK, '')
993
+
994
+ const entries = [
995
+ ['关闭其他标签页', plan.others],
996
+ ['关闭全部标签页', plan.all],
997
+ ['关闭右侧所有标签页', plan.right],
998
+ ]
999
+ for (const [label, targets] of entries) {
1000
+ const button = doc.createElement('button')
1001
+ button.type = 'button'
1002
+ button.setAttribute('role', 'menuitem')
1003
+ button.className = 'dsh-live-browser-menu-item'
1004
+ button.textContent = label
1005
+ button.disabled = targets.length === 0
1006
+ button.addEventListener('click', (event) => {
1007
+ event.stopPropagation()
1008
+ if (targets.length === 0) return
1009
+ closeTabs(ctx, targets, () => dismissTabMenu(menu))
1010
+ })
1011
+ group.appendChild(button)
1012
+ }
1013
+ menu.appendChild(group)
1014
+ }
1015
+
1016
+ /** Close the menu the way an outside click does; its React state follows. */
1017
+ function dismissTabMenu(menu) {
1018
+ const doc = menu.ownerDocument ?? document
1019
+ const view = doc.defaultView
1020
+ try {
1021
+ const Pointer = view?.PointerEvent ?? PointerEvent
1022
+ doc.dispatchEvent(new Pointer('pointerdown', { bubbles: true }))
1023
+ } catch {
1024
+ try {
1025
+ const Keyboard = view?.KeyboardEvent ?? KeyboardEvent
1026
+ doc.dispatchEvent(new Keyboard('keydown', { key: 'Escape', bubbles: true }))
1027
+ } catch {
1028
+ /* no DOM events: the menu will close on the next outside click */
1029
+ }
1030
+ }
1031
+ }
1032
+
1033
+ /**
1034
+ * Watch for tab context menus and decorate them.
1035
+ *
1036
+ * The menu is created in the same event as the right-click, so the tab id has
1037
+ * to be captured there; a zero-delay timer is enough time for React to commit
1038
+ * the portal.
1039
+ */
1040
+ function installTabMenu(ctx) {
1041
+ if (typeof document === 'undefined' || document === null) return undefined
1042
+ // Tests and non-DOM builds hand us a document without a head; the browser
1043
+ // tools must keep working there.
1044
+ if (typeof document.addEventListener !== 'function' || document.head === undefined || document.head === null) {
1045
+ return undefined
1046
+ }
1047
+
1048
+ let style
1049
+ try {
1050
+ style = document.createElement('style')
1051
+ style.dataset.plugin = 'dsh-live-browser:tab-menu'
1052
+ style.textContent = TAB_MENU_STYLE
1053
+ document.head.appendChild(style)
1054
+ } catch {
1055
+ style = undefined
1056
+ }
1057
+
1058
+ let pending
1059
+ const onContextMenu = (event) => {
1060
+ const target = event.target instanceof Element ? event.target.closest('[data-dockkit-tab]') : null
1061
+ const tabId = target?.dataset?.dockkitTab
1062
+ if (tabId === undefined) return
1063
+ pending = tabId
1064
+ setTimeout(() => {
1065
+ if (pending !== tabId) return
1066
+ pending = undefined
1067
+ decorateTabMenu(ctx, document.querySelector('[data-dockkit-tab-menu]'), tabId)
1068
+ }, 0)
1069
+ }
1070
+ document.addEventListener('contextmenu', onContextMenu, true)
1071
+
1072
+ return () => {
1073
+ document.removeEventListener('contextmenu', onContextMenu, true)
1074
+ style?.remove()
1075
+ }
1076
+ }
1077
+
1078
+ /** Cordis plugin entry for the client half. */
1079
+ function apply(ctx) {
1080
+ const bridge = createRuntime()
1081
+
1082
+ ctx.effect(() => installTabMenu(ctx), 'dsh-live-browser: tab menu')
1083
+
1084
+ // The loop is page-scoped, not component-scoped: the sidebar may unmount and
1085
+ // remount tabs without the page ever reloading.
1086
+ ctx.effect(() => {
1087
+ const driver = createDriver({
1088
+ bridge,
1089
+ loadSource: () => bridge.source(),
1090
+ // Auto-open: a tool call from a cold start must bring the browser up by
1091
+ // itself, inside the requesting conversation.
1092
+ openPanel: (url, sessionId) => openSidebarBrowser(ctx, sessionId, url),
1093
+ currentSessionId: () => currentSessionId(ctx),
1094
+ listSessions: () => {
1095
+ try {
1096
+ return ctx.get?.('sessions')?.list?.() ?? []
1097
+ } catch {
1098
+ return []
1099
+ }
1100
+ },
1101
+ })
1102
+ void driver.start()
1103
+ return () => driver.stop()
1104
+ }, 'dsh-live-browser: agent loop')
1105
+ }
1106
+
1107
+ if (typeof window !== 'undefined' && typeof window.__ModuleLoader__ === 'object') {
1108
+ window.__ModuleLoader__.load({
1109
+ id: 'dsh-live-browser',
1110
+ factory: () => ({ apply }),
1111
+ })
1112
+ } else if (typeof module !== 'undefined' && module.exports) {
1113
+ module.exports = {
1114
+ normalizeAddress,
1115
+ normalizeValue,
1116
+ GUEST_SELECTOR,
1117
+ GUEST_METHODS,
1118
+ tabStripIds,
1119
+ closingPlan,
1120
+ closeTabs,
1121
+ decorateTabMenu,
1122
+ }
1123
+ }
1124
+ })()