@capyclaw/browser-workbench 0.1.0 → 0.1.19

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.
Files changed (161) hide show
  1. package/README.md +29 -7
  2. package/contract/browser-contract.json +484 -25
  3. package/demo/form.html +27 -0
  4. package/lib/host.mjs +1 -68
  5. package/node_modules/@capyclaw/asset-service/README.md +48 -0
  6. package/node_modules/@capyclaw/asset-service/cordis.patch.yml +4 -0
  7. package/node_modules/@capyclaw/asset-service/dsh-suite.json +29 -0
  8. package/node_modules/@capyclaw/asset-service/lib/host.mjs +14 -0
  9. package/node_modules/@capyclaw/asset-service/package.json +59 -0
  10. package/node_modules/@capyclaw/asset-service/src/index.mjs +304 -0
  11. package/node_modules/@capyclaw/document-workbench/README.md +125 -0
  12. package/node_modules/@capyclaw/document-workbench/package.json +44 -0
  13. package/node_modules/@capyclaw/document-workbench/src/bridge/index.mjs +321 -0
  14. package/node_modules/@capyclaw/document-workbench/src/client/index.mjs +625 -0
  15. package/node_modules/@capyclaw/document-workbench/src/client/screenshot.mjs +98 -0
  16. package/node_modules/@capyclaw/document-workbench/src/index.mjs +9 -0
  17. package/node_modules/@capyclaw/document-workbench/src/model/audit.mjs +101 -0
  18. package/node_modules/@capyclaw/document-workbench/src/model/conformance.mjs +92 -0
  19. package/node_modules/@capyclaw/document-workbench/src/model/ids.mjs +56 -0
  20. package/node_modules/@capyclaw/document-workbench/src/model/index.mjs +18 -0
  21. package/node_modules/@capyclaw/document-workbench/src/model/locate.mjs +118 -0
  22. package/node_modules/@capyclaw/document-workbench/src/model/manifest.mjs +148 -0
  23. package/node_modules/@capyclaw/document-workbench/src/model/ops.mjs +160 -0
  24. package/node_modules/@capyclaw/document-workbench/src/model/validate.mjs +72 -0
  25. package/node_modules/@capyclaw/document-workbench/src/ops/index.mjs +93 -0
  26. package/node_modules/@capyclaw/document-workbench/src/session/context.mjs +103 -0
  27. package/node_modules/@capyclaw/document-workbench/src/session/index.mjs +394 -0
  28. package/node_modules/@capyclaw/document-workbench/src/session/lease.mjs +105 -0
  29. package/node_modules/@capyclaw/document-workbench/src/session/mode.mjs +80 -0
  30. package/node_modules/@capyclaw/document-workbench/src/session/sanitize.mjs +107 -0
  31. package/node_modules/@capyclaw/document-workbench/src/session/state.mjs +159 -0
  32. package/node_modules/@capyclaw/document-workbench/src/session/watch.mjs +63 -0
  33. package/{server/shared/state-service.mjs → node_modules/@capyclaw/document-workbench/src/store/file-backend.mjs} +6 -97
  34. package/node_modules/@capyclaw/document-workbench/src/testing/index.mjs +75 -0
  35. package/node_modules/@capyclaw/foundation/README.md +51 -0
  36. package/node_modules/@capyclaw/foundation/abi/bridge.mjs +86 -0
  37. package/node_modules/@capyclaw/foundation/abi/errors.mjs +63 -0
  38. package/node_modules/@capyclaw/foundation/abi/index.mjs +35 -0
  39. package/node_modules/@capyclaw/foundation/abi/kinds.mjs +271 -0
  40. package/node_modules/@capyclaw/foundation/abi/loader.mjs +345 -0
  41. package/node_modules/@capyclaw/foundation/abi/schema.mjs +98 -0
  42. package/node_modules/@capyclaw/foundation/abi/spec.mjs +255 -0
  43. package/node_modules/@capyclaw/foundation/abi/validate.mjs +24 -0
  44. package/node_modules/@capyclaw/foundation/abi/version.mjs +82 -0
  45. package/node_modules/@capyclaw/foundation/cordis.patch.yml +5 -0
  46. package/node_modules/@capyclaw/foundation/kit/index.mjs +6 -0
  47. package/node_modules/@capyclaw/foundation/lib/adapters/client-panel.mjs +98 -0
  48. package/node_modules/@capyclaw/foundation/lib/adapters/host-event.mjs +35 -0
  49. package/node_modules/@capyclaw/foundation/lib/adapters/http-router.mjs +69 -0
  50. package/node_modules/@capyclaw/foundation/lib/adapters/lifecycle-hook.mjs +55 -0
  51. package/node_modules/@capyclaw/foundation/lib/adapters/own-server.mjs +113 -0
  52. package/node_modules/@capyclaw/foundation/lib/adapters/raw.mjs +69 -0
  53. package/node_modules/@capyclaw/foundation/lib/adapters/runtime-driver.mjs +120 -0
  54. package/node_modules/@capyclaw/foundation/lib/adapters/service-provider.mjs +77 -0
  55. package/node_modules/@capyclaw/foundation/lib/adapters/skill-provider.mjs +56 -0
  56. package/node_modules/@capyclaw/foundation/lib/adapters/tool-provider.mjs +50 -0
  57. package/node_modules/@capyclaw/foundation/lib/adapters/ui-panel.mjs +64 -0
  58. package/node_modules/@capyclaw/foundation/lib/ctx-service.mjs +42 -0
  59. package/node_modules/@capyclaw/foundation/lib/host/caps.mjs +34 -0
  60. package/node_modules/@capyclaw/foundation/lib/host/contract.mjs +85 -0
  61. package/node_modules/@capyclaw/foundation/lib/host/dsh.mjs +50 -0
  62. package/node_modules/@capyclaw/foundation/lib/host/memory.mjs +160 -0
  63. package/node_modules/@capyclaw/foundation/lib/host.mjs +23 -0
  64. package/node_modules/@capyclaw/foundation/lib/kernel.mjs +185 -0
  65. package/node_modules/@capyclaw/foundation/lib/skill/scan.mjs +115 -0
  66. package/node_modules/@capyclaw/foundation/lib/subsystems/events.mjs +60 -0
  67. package/node_modules/@capyclaw/foundation/lib/subsystems/log.mjs +51 -0
  68. package/node_modules/@capyclaw/foundation/lib/subsystems/net.mjs +188 -0
  69. package/node_modules/@capyclaw/foundation/package.json +53 -0
  70. package/node_modules/@capyclaw/foundation/runtime/index.mjs +20 -0
  71. package/node_modules/@capyclaw/plugin-kit/INTEGRATION.md +100 -0
  72. package/node_modules/@capyclaw/plugin-kit/README.md +70 -0
  73. package/node_modules/@capyclaw/plugin-kit/bin/sync.mjs +103 -0
  74. package/node_modules/@capyclaw/plugin-kit/package.json +69 -0
  75. package/node_modules/@capyclaw/plugin-kit/src/cli.mjs +56 -0
  76. package/node_modules/@capyclaw/plugin-kit/src/config.mjs +142 -0
  77. package/node_modules/@capyclaw/plugin-kit/src/editor/heartbeat-worker.js +129 -0
  78. package/node_modules/@capyclaw/plugin-kit/src/editor/lease.mjs +58 -0
  79. package/node_modules/@capyclaw/plugin-kit/src/editor/shadow.mjs +219 -0
  80. package/node_modules/@capyclaw/plugin-kit/src/editor/transport.js +13 -0
  81. package/node_modules/@capyclaw/plugin-kit/src/index.mjs +13 -0
  82. package/node_modules/@capyclaw/plugin-kit/src/pack.mjs +230 -0
  83. package/node_modules/@capyclaw/plugin-kit/src/runtime/context.mjs +128 -0
  84. package/node_modules/@capyclaw/plugin-kit/src/self-contained.mjs +307 -0
  85. package/node_modules/@capyclaw/plugin-kit/src/service/api.mjs +45 -0
  86. package/node_modules/@capyclaw/plugin-kit/src/service/http.mjs +113 -0
  87. package/node_modules/@capyclaw/plugin-kit/src/service/mcp-http.mjs +150 -0
  88. package/node_modules/@capyclaw/plugin-kit/src/service/server.mjs +131 -0
  89. package/node_modules/@capyclaw/plugin-kit/src/skills/provider.mjs +82 -0
  90. package/node_modules/@capyclaw/plugin-kit/src/store/blob-store.mjs +109 -0
  91. package/node_modules/@capyclaw/plugin-kit/src/store/content-store.mjs +17 -0
  92. package/node_modules/@capyclaw/plugin-kit/src/store/journal-store.mjs +89 -0
  93. package/node_modules/@capyclaw/plugin-kit/src/tools/result.mjs +57 -0
  94. package/node_modules/@capyclaw/plugin-kit/src/tools/validate.mjs +150 -0
  95. package/node_modules/@capyclaw/plugin-kit/src/tuning.mjs +82 -0
  96. package/node_modules/@capyclaw/plugin-kit/src/util/fs-lock.mjs +132 -0
  97. package/node_modules/@capyclaw/plugin-kit/src/util/http.mjs +130 -0
  98. package/node_modules/@capyclaw/plugin-kit/src/workspace/manager.mjs +656 -0
  99. package/node_modules/@capyclaw/plugin-kit/src/zip.mjs +181 -0
  100. package/node_modules/@capyclaw/service-host/README.md +86 -0
  101. package/node_modules/@capyclaw/service-host/cordis.patch.yml +4 -0
  102. package/node_modules/@capyclaw/service-host/dsh-suite.json +29 -0
  103. package/node_modules/@capyclaw/service-host/lib/host.mjs +22 -0
  104. package/node_modules/@capyclaw/service-host/package.json +60 -0
  105. package/node_modules/@capyclaw/service-host/src/index.mjs +571 -0
  106. package/node_modules/@capyclaw/service-host/src/tuning.mjs +63 -0
  107. package/node_modules/@capyclaw/tool-host/README.md +56 -0
  108. package/node_modules/@capyclaw/tool-host/cordis.patch.yml +4 -0
  109. package/node_modules/@capyclaw/tool-host/dsh-suite.json +30 -0
  110. package/node_modules/@capyclaw/tool-host/lib/host.mjs +16 -0
  111. package/node_modules/@capyclaw/tool-host/package.json +59 -0
  112. package/node_modules/@capyclaw/tool-host/src/descriptors.mjs +166 -0
  113. package/node_modules/@capyclaw/tool-host/src/index.mjs +416 -0
  114. package/package.json +28 -12
  115. package/plugin.mjs +1 -0
  116. package/server/contract.mjs +3 -3
  117. package/server/embedded-host.mjs +247 -0
  118. package/server/engine.mjs +1906 -115
  119. package/server/governor.mjs +189 -0
  120. package/server/http-utils.mjs +1 -1
  121. package/server/human.mjs +628 -0
  122. package/server/infra/http.mjs +2 -113
  123. package/server/infra/tuning.mjs +2 -82
  124. package/server/input.mjs +154 -0
  125. package/server/login-vault.mjs +109 -0
  126. package/server/mcp/handler.mjs +1 -1
  127. package/server/mcp/instructions.mjs +5 -2
  128. package/server/orphans.mjs +163 -0
  129. package/server/relay.mjs +160 -188
  130. package/server/routes/api.mjs +126 -13
  131. package/server/routes/assets.mjs +1 -1
  132. package/server/session-engines.mjs +223 -0
  133. package/server/shared/SHARED_MANIFEST.json +47 -24
  134. package/server/shared/asset-service.mjs +2 -217
  135. package/server/shared/editor-host.mjs +2 -125
  136. package/server/shared/kit/editor/lease.mjs +2 -139
  137. package/server/shared/kit/service/api.mjs +2 -45
  138. package/server/shared/kit/service/http.mjs +2 -113
  139. package/server/shared/kit/service/mcp-http.mjs +2 -142
  140. package/server/shared/kit/tools/validate.mjs +2 -115
  141. package/server/shared/kit/tuning.mjs +2 -82
  142. package/server/shared/kit/util/fs-lock.mjs +2 -132
  143. package/server/shared/service-host.mjs +2 -511
  144. package/server/shared/tool-host.mjs +2 -341
  145. package/server/shared/tuning.mjs +2 -63
  146. package/server/tools/capabilities/contract.mjs +25 -5
  147. package/server/tools/capabilities/interaction.mjs +113 -9
  148. package/server/tools/capabilities/navigation.mjs +5 -2
  149. package/server/tools/capabilities/screenshot.mjs +25 -8
  150. package/server/tools/capabilities/session.mjs +13 -7
  151. package/server/tools/capabilities/snapshot.mjs +1 -1
  152. package/server/tools/capabilities/tabs.mjs +2 -2
  153. package/server/tools/kernel/define.mjs +1 -1
  154. package/server/tools/kernel/output.mjs +8 -0
  155. package/server/tools/kernel/ports.mjs +12 -7
  156. package/server/tools/kernel/registry.mjs +1 -1
  157. package/server/workbench-framework.mjs +305 -0
  158. package/ui/app.js +409 -119
  159. package/ui/index.html +29 -39
  160. package/ui/style.css +131 -63
  161. package/cordis.patch.yml +0 -25
package/ui/app.js CHANGED
@@ -1,157 +1,447 @@
1
1
  /**
2
- * 浏览器操控工作台前端(无框架无构建)。
2
+ * 浏览器工作台前端(无框架无构建)——真实浏览器外观:标签栏 + 地址栏 + 整幅画面。
3
3
  *
4
- * 数据面全部来自本服务的 /api/*:状态轮询 2s;浏览器运行时实时画面轮询
5
- * 1.2s(未运行不请求)。所有按钮只是便捷入口,与 AI 的 browser_* 工具
6
- * 共用同一引擎实例与动作互斥队列。
4
+ * 两条数据通道:
5
+ * 1. /api/live(SSE)推当前活动页的 JPEG 帧 —— 画面是「真」的,不是 1.2s 轮询截图;
6
+ * 2. /api/input(POST)把画面里的鼠标/滚轮/键盘回放成页面里的真实输入。
7
+ * 其余(状态、标签页、导航、历史)都走 /api/*,与 AI 的 browser_* 工具共用同一引擎实例。
8
+ *
9
+ * 窄栏(DSH 右侧边栏)与宽窗口共用同一份布局:@media 只在细节上收边。
7
10
  */
8
11
  (function () {
9
12
  'use strict'
10
13
 
11
14
  var $ = function (id) { return document.getElementById(id) }
12
15
  var els = {
13
- engine: $('badge-engine'), mode: $('badge-mode'), tabs: $('tabs'),
14
- view: $('view'), log: $('log'), snapshot: $('snapshot'), counters: $('counters'),
15
- urlInput: $('url-input'),
16
- btnOpen: $('btn-open'), btnDemo: $('btn-demo'), btnSnapshot: $('btn-snapshot'), btnClose: $('btn-close'),
16
+ tabs: $('tabs'), newTab: $('btn-newtab'),
17
+ stage: $('stage'), frame: $('frame'), placeholder: $('placeholder'), hint: $('hint'),
18
+ wall: $('wall'), wallText: $('wall-text'),
19
+ urlInput: $('url-input'), kbd: $('kbd'),
20
+ btnBack: $('btn-back'), btnForward: $('btn-forward'), btnReload: $('btn-reload'),
17
21
  }
18
- var running = false
19
- var shotBusy = false
20
- var statusBusy = false
21
22
 
22
- function fmtUrl(url) {
23
- if (!url || url === 'about:blank') return '(空白页)'
24
- return url.replace(/^https?:\/\//, '').slice(0, 40)
23
+ var state = {
24
+ running: false,
25
+ frameW: 1280,
26
+ frameH: 800,
27
+ lastFrameAt: 0,
28
+ hasFrame: false,
29
+ fallbackTimer: null,
30
+ statusBusy: false,
31
+ tabsSig: null,
25
32
  }
26
33
 
27
- function renderStatus(data) {
28
- var st = data.status || {}
29
- running = !!st.running
30
- els.engine.textContent = running
31
- ? (st.launching ? '浏览器启动中…' : '浏览器运行中 · ' + (st.channel || 'chromium'))
32
- : '浏览器未启动'
33
- els.engine.className = 'badge ' + (running ? 'on' : 'off')
34
- els.mode.hidden = !running
35
- els.mode.textContent = st.headless ? '无头' : '有头'
36
- els.counters.textContent = st.counters
37
- ? '动作 ' + st.counters.actions + ' · 失败 ' + st.counters.errors + ' · 快照 ' + st.counters.snapshots
38
- : ''
39
- /* 标签页 */
40
- els.tabs.innerHTML = ''
41
- var tabs = st.tabs || []
42
- if (tabs.length > 1 || (tabs.length && !running)) {
43
- tabs.forEach(function (tab) {
44
- var el = document.createElement('span')
45
- el.className = 'tab' + (tab.active ? ' active' : '')
46
- el.textContent = (tab.id || '?') + ' ' + fmtUrl(tab.url)
47
- el.title = tab.url
48
- el.onclick = function () { post('/api/tabs', { action: 'select', tab: tab.id }) }
49
- els.tabs.appendChild(el)
50
- })
51
- }
52
- /* 动作日志 */
53
- var actions = data.actions || []
54
- if (actions.length) {
55
- els.log.innerHTML = ''
56
- actions.forEach(function (a) {
57
- var li = document.createElement('li')
58
- var t = document.createElement('span')
59
- t.className = 't'
60
- t.textContent = (a.at || '').slice(11, 19)
61
- var name = document.createElement('span')
62
- name.className = a.ok ? 'ok' : 'fail'
63
- name.textContent = (a.ok ? '✓ ' : '✗ ') + a.tool + (a.detail ? ' · ' + a.detail : '')
64
- if (!a.ok && a.error) name.title = a.error
65
- var ms = document.createElement('span')
66
- ms.className = 'ms'
67
- ms.textContent = a.ms + 'ms'
68
- li.appendChild(t); li.appendChild(name); li.appendChild(ms)
69
- els.log.appendChild(li)
70
- })
71
- }
72
- }
34
+ /* ---------- 基础请求 ---------- */
73
35
 
74
- function pollStatus() {
75
- if (statusBusy) return
76
- statusBusy = true
77
- fetch('/api/status').then(function (r) { return r.json() }).then(function (data) {
78
- renderStatus(data)
79
- }).catch(function () { /* 服务重启间隙 */ }).finally(function () { statusBusy = false })
80
- }
81
-
82
- function pollScreenshot() {
83
- if (!running || shotBusy) return
84
- shotBusy = true
85
- fetch('/api/screenshot').then(function (r) {
86
- if (!r.ok) throw new Error('shot ' + r.status)
87
- return r.blob()
88
- }).then(function (blob) {
89
- var img = els.view.querySelector('img')
90
- if (!img) {
91
- els.view.innerHTML = ''
92
- img = document.createElement('img')
93
- img.alt = '当前页面实时画面'
94
- els.view.appendChild(img)
95
- }
96
- var url = URL.createObjectURL(blob)
97
- var prev = img.src
98
- img.src = url
99
- if (prev && prev.startsWith('blob:')) URL.revokeObjectURL(prev)
100
- }).catch(function () { /* 引擎未运行/页面关闭 */ }).finally(function () { shotBusy = false })
36
+ /* 端点跟着页面走:embedded 在 /capyclaw/browser/,standalone 在 /,
37
+ * 同一份 UI 不用知道基建把服务放在哪。 */
38
+ var API_ROOT = new URL('.', document.baseURI).pathname
39
+ function apiPath(path) { return API_ROOT + String(path).replace(/^\//, '') }
40
+
41
+ /* 内联提示条:替代 alert()。alert 是同步模态,会冻住整个工作台 JS(轮询/SSE 全停),
42
+ 一次失败就把界面卡死;toast 非阻塞,6s 自动消失,轮询照常。 */
43
+ var toastTimer = null
44
+ function toast(msg) {
45
+ var el = document.getElementById('toast')
46
+ if (!el) return
47
+ el.textContent = msg
48
+ el.hidden = false
49
+ if (toastTimer) clearTimeout(toastTimer)
50
+ toastTimer = setTimeout(function () { el.hidden = true }, 6000)
101
51
  }
102
52
 
103
53
  function post(path, body) {
104
54
  return fetch(path, {
105
55
  method: 'POST',
106
56
  headers: { 'content-type': 'application/json' },
107
- body: JSON.stringify(body),
57
+ body: JSON.stringify(body || {}),
108
58
  }).then(function (r) { return r.json() }).then(function (data) {
109
- pollStatus()
110
59
  if (data && data.ok === false && data.error) {
111
- alert('[' + (data.error.code || 'ERROR') + '] ' + data.error.message +
112
- (data.error.hint ? '\n' + data.error.hint : ''))
60
+ toast('[' + (data.error.code || 'ERROR') + '] ' + data.error.message +
61
+ (data.error.hint ? ' ' + data.error.hint : ''))
113
62
  }
114
63
  return data
115
- }).catch(function (e) { alert('请求失败: ' + e.message) })
64
+ }).catch(function (e) { toast('请求失败: ' + e.message) })
116
65
  }
117
66
 
118
- function openUrl(url) {
119
- els.btnOpen.disabled = true
120
- post('/api/navigate', { url: url }).finally(function () {
121
- els.btnOpen.disabled = false
122
- pollScreenshot()
67
+ /** 输入是高频通道:失败静默(画面照常,下一次事件会补上)。 */
68
+ function sendInput(payload) {
69
+ return fetch(apiPath('/api/input'), {
70
+ method: 'POST',
71
+ headers: { 'content-type': 'application/json' },
72
+ body: JSON.stringify(payload),
73
+ }).catch(function () {})
74
+ }
75
+
76
+ /* ---------- 实时画面 ---------- */
77
+
78
+ function showFrame() {
79
+ if (state.hasFrame) return
80
+ state.hasFrame = true
81
+ els.placeholder.hidden = true
82
+ els.frame.classList.add('on')
83
+ els.hint.hidden = false
84
+ /* 提示只在第一帧后露 5 秒,之后让位给画面本身。 */
85
+ setTimeout(function () { els.hint.hidden = true }, 5000)
86
+ }
87
+
88
+ function hideFrame() {
89
+ state.hasFrame = false
90
+ els.frame.classList.remove('on')
91
+ els.frame.removeAttribute('src')
92
+ els.placeholder.hidden = false
93
+ }
94
+
95
+ function drawFrame(msg) {
96
+ if (!msg || !msg.img) return
97
+ if (msg.w) state.frameW = msg.w
98
+ if (msg.h) state.frameH = msg.h
99
+ els.frame.width = state.frameW
100
+ els.frame.height = state.frameH
101
+ els.frame.src = 'data:image/jpeg;base64,' + msg.img
102
+ state.lastFrameAt = Date.now()
103
+ showFrame()
104
+ }
105
+
106
+ var live = null
107
+
108
+ function connectLive() {
109
+ if (live) return
110
+ if (typeof EventSource !== 'function') { startFallback(); return }
111
+ try {
112
+ live = new EventSource(apiPath('/api/live'))
113
+ } catch (e) {
114
+ live = null
115
+ startFallback()
116
+ return
117
+ }
118
+ live.onmessage = function (event) {
119
+ stopFallback()
120
+ try { drawFrame(JSON.parse(event.data)) } catch (e) { /* 坏帧直接丢 */ }
121
+ }
122
+ /* EventSource 自己会重连;这里只在长时间没有帧时退到轮询兜底。 */
123
+ live.onerror = function () { /* 由 watchdog 决定是否兜底 */ }
124
+ }
125
+
126
+ /* SSE 被中间层吃掉(或桌面 webview 不支持)时,退回 1.2s 截图,至少还能看。 */
127
+ function startFallback() {
128
+ if (state.fallbackTimer !== null) return
129
+ state.fallbackTimer = setInterval(function () {
130
+ if (!state.running) return
131
+ fetch(apiPath('/api/screenshot')).then(function (r) {
132
+ if (!r.ok) throw new Error('shot ' + r.status)
133
+ return r.blob()
134
+ }).then(function (blob) {
135
+ var url = URL.createObjectURL(blob)
136
+ var prev = els.frame.getAttribute('src') || ''
137
+ els.frame.src = url
138
+ if (prev.indexOf('blob:') === 0) URL.revokeObjectURL(prev)
139
+ state.lastFrameAt = Date.now()
140
+ showFrame()
141
+ }).catch(function () { /* 引擎未运行 / 页面关闭 */ })
142
+ }, 1200)
143
+ }
144
+
145
+ function stopFallback() {
146
+ if (state.fallbackTimer === null) return
147
+ clearInterval(state.fallbackTimer)
148
+ state.fallbackTimer = null
149
+ }
150
+
151
+ /* ---------- 坐标映射与输入回放 ---------- */
152
+
153
+ /** object-fit: contain 的内容框 → 页面 CSS 像素坐标。 */
154
+ function toPage(clientX, clientY) {
155
+ var rect = els.frame.getBoundingClientRect()
156
+ if (!rect.width || !rect.height) return { x: 0, y: 0 }
157
+ var scale = Math.min(rect.width / state.frameW, rect.height / state.frameH)
158
+ var dispW = state.frameW * scale
159
+ var dispH = state.frameH * scale
160
+ var ox = rect.left + (rect.width - dispW) / 2
161
+ var oy = rect.top + (rect.height - dispH) / 2
162
+ return {
163
+ x: Math.round((clientX - ox) / scale),
164
+ y: Math.round((clientY - oy) / scale),
165
+ }
166
+ }
167
+
168
+ function buttonName(button) {
169
+ if (button === 2) return 'right'
170
+ if (button === 1) return 'middle'
171
+ return 'left'
172
+ }
173
+
174
+ var pointerDown = false
175
+ var lastMoveAt = 0
176
+ var queuedMove = null
177
+
178
+ function flushMove() {
179
+ if (!queuedMove) return
180
+ var point = queuedMove
181
+ queuedMove = null
182
+ sendInput({ type: 'pointer', kind: 'move', x: point.x, y: point.y })
183
+ }
184
+
185
+ els.frame.addEventListener('pointerdown', function (event) {
186
+ if (!state.running) return
187
+ event.preventDefault()
188
+ els.frame.focus()
189
+ els.hint.hidden = true
190
+ if (els.kbd) els.kbd.focus({ preventScroll: true })
191
+ pointerDown = true
192
+ try { els.frame.setPointerCapture(event.pointerId) } catch (e) { /* 老 webview */ }
193
+ var point = toPage(event.clientX, event.clientY)
194
+ sendInput({ type: 'pointer', kind: 'down', x: point.x, y: point.y, button: buttonName(event.button) })
195
+ })
196
+
197
+ els.frame.addEventListener('pointermove', function (event) {
198
+ if (!state.running) return
199
+ var point = toPage(event.clientX, event.clientY)
200
+ queuedMove = point
201
+ /* 移动节流到 ~40fps:报文太多会把 loopback 和 CDP 都拖慢。 */
202
+ if (Date.now() - lastMoveAt < 24) return
203
+ lastMoveAt = Date.now()
204
+ flushMove()
205
+ })
206
+
207
+ els.frame.addEventListener('pointerup', function (event) {
208
+ if (!state.running) return
209
+ event.preventDefault()
210
+ pointerDown = false
211
+ try { els.frame.releasePointerCapture(event.pointerId) } catch (e) { /* 忽略 */ }
212
+ var point = toPage(event.clientX, event.clientY)
213
+ sendInput({ type: 'pointer', kind: 'up', x: point.x, y: point.y, button: buttonName(event.button) })
214
+ })
215
+
216
+ els.frame.addEventListener('pointercancel', function () {
217
+ if (!pointerDown) return
218
+ pointerDown = false
219
+ sendInput({ type: 'pointer', kind: 'up', x: 0, y: 0, button: 'left' })
220
+ })
221
+
222
+ els.frame.addEventListener('contextmenu', function (event) { event.preventDefault() })
223
+
224
+ els.frame.addEventListener('wheel', function (event) {
225
+ if (!state.running) return
226
+ event.preventDefault()
227
+ var point = toPage(event.clientX, event.clientY)
228
+ sendInput({
229
+ type: 'pointer', kind: 'wheel',
230
+ x: point.x, y: point.y,
231
+ deltaX: Math.round(event.deltaX), deltaY: Math.round(event.deltaY),
232
+ })
233
+ }, { passive: false })
234
+
235
+ /* 键盘:隐藏 textarea 负责接收,特殊键走 press,可打印字符走 text(中文走 insertText)。 */
236
+ var SPECIAL_KEYS = {
237
+ Enter: 'Enter', Tab: 'Tab', Backspace: 'Backspace', Delete: 'Delete', Escape: 'Escape',
238
+ ArrowUp: 'ArrowUp', ArrowDown: 'ArrowDown', ArrowLeft: 'ArrowLeft', ArrowRight: 'ArrowRight',
239
+ Home: 'Home', End: 'End', PageUp: 'PageUp', PageDown: 'PageDown', Insert: 'Insert',
240
+ }
241
+ var composing = false
242
+
243
+ function resetKbd() { if (els.kbd) els.kbd.value = '' }
244
+
245
+ els.kbd.addEventListener('keydown', function (event) {
246
+ if (event.isComposing || composing) return
247
+ if (event.ctrlKey || event.metaKey) {
248
+ var base = event.key.length === 1 ? event.key.toLowerCase() : event.key
249
+ event.preventDefault()
250
+ sendInput({ type: 'key', kind: 'press', key: (event.ctrlKey ? 'Control+' : 'Meta+') + base })
251
+ return
252
+ }
253
+ var special = SPECIAL_KEYS[event.key]
254
+ if (special) {
255
+ event.preventDefault()
256
+ sendInput({ type: 'key', kind: 'press', key: special })
257
+ }
258
+ })
259
+
260
+ els.kbd.addEventListener('beforeinput', function (event) {
261
+ if (composing) return
262
+ if (event.inputType === 'insertText' && event.data) {
263
+ event.preventDefault()
264
+ sendInput({ type: 'key', kind: 'text', text: event.data })
265
+ resetKbd()
266
+ return
267
+ }
268
+ if (event.inputType === 'insertLineBreak' || event.inputType === 'insertParagraph') {
269
+ event.preventDefault()
270
+ sendInput({ type: 'key', kind: 'press', key: 'Enter' })
271
+ resetKbd()
272
+ }
273
+ })
274
+
275
+ els.kbd.addEventListener('compositionstart', function () { composing = true })
276
+ els.kbd.addEventListener('compositionend', function (event) {
277
+ composing = false
278
+ if (event.data) sendInput({ type: 'key', kind: 'text', text: event.data })
279
+ resetKbd()
280
+ })
281
+
282
+ /* 粘贴:textarea 的 paste 不会走 beforeinput.insertText,单独接一次。 */
283
+ els.kbd.addEventListener('paste', function (event) {
284
+ var data = event.clipboardData || window.clipboardData
285
+ var text = data ? data.getData('text') : ''
286
+ if (!text) return
287
+ event.preventDefault()
288
+ sendInput({ type: 'key', kind: 'text', text: text })
289
+ })
290
+
291
+ /* ---------- 状态与标签页 ---------- */
292
+
293
+ function tabLabel(tab) {
294
+ if (tab.title) return tab.title.slice(0, 30)
295
+ var url = tab.url
296
+ if (!url || url === 'about:blank') return '新标签页'
297
+ return url.replace(/^https?:\/\//, '').slice(0, 30)
298
+ }
299
+
300
+ /* 标签栏像真实浏览器:始终可见,单击切换、× 关闭、+ 新建。
301
+ * 引擎还没标签时放一个「新标签页」占位标签,点它聚焦地址栏。
302
+ * 签名不变就不重建 DOM:否则 2s 轮询会在用户点击途中换掉节点,丢掉 click。 */
303
+ function renderTabs(tabs) {
304
+ var sig = tabs.map(function (t) { return t.id + '|' + (t.url || '') + '|' + (t.title || '') + '|' + (t.active ? 1 : 0) }).join(',')
305
+ if (sig === state.tabsSig) return
306
+ state.tabsSig = sig
307
+ els.tabs.innerHTML = ''
308
+ if (!tabs.length) {
309
+ var ghost = document.createElement('span')
310
+ ghost.className = 'tab active'
311
+ ghost.title = '在地址栏输入网址,回车打开'
312
+ var gl = document.createElement('span')
313
+ gl.className = 'tab-label'
314
+ gl.textContent = '新标签页'
315
+ ghost.appendChild(gl)
316
+ ghost.onclick = function () { els.urlInput.focus(); els.urlInput.select() }
317
+ els.tabs.appendChild(ghost)
318
+ return
319
+ }
320
+ tabs.forEach(function (tab) {
321
+ var el = document.createElement('span')
322
+ el.className = 'tab' + (tab.active ? ' active' : '')
323
+ el.title = (tab.title ? tab.title + '\n' : '') + (tab.url || 'about:blank')
324
+ var label = document.createElement('span')
325
+ label.className = 'tab-label'
326
+ label.textContent = tabLabel(tab)
327
+ el.appendChild(label)
328
+ var close = document.createElement('button')
329
+ close.type = 'button'
330
+ close.className = 'tab-close'
331
+ close.title = '关闭标签页'
332
+ close.textContent = '×'
333
+ close.onclick = function (event) {
334
+ event.stopPropagation()
335
+ post(apiPath('/api/tabs'), { action: 'close', tab: tab.id }).then(pollStatus)
336
+ }
337
+ el.appendChild(close)
338
+ el.onclick = function () { post(apiPath('/api/tabs'), { action: 'select', tab: tab.id }).then(pollStatus) }
339
+ els.tabs.appendChild(el)
123
340
  })
124
341
  }
125
342
 
126
- els.btnOpen.onclick = function () {
127
- var url = els.urlInput.value.trim()
343
+ function renderStatus(data) {
344
+ var st = data.status || {}
345
+ var wasRunning = state.running
346
+ state.running = !!st.running
347
+ state.restorable = !!st.restorable
348
+ if (st.viewport && st.viewport.width) {
349
+ state.frameW = st.viewport.width
350
+ state.frameH = st.viewport.height
351
+ }
352
+
353
+ /* 活动页停在 DSH Web 自身 401 鉴权页:盖解释层,藏掉「点画面」提示。 */
354
+ var wall = st.authWall || ''
355
+ els.wall.hidden = !wall
356
+ if (wall) { els.wallText.textContent = wall; els.hint.hidden = true }
357
+
358
+ if (!state.running && wasRunning) hideFrame()
359
+ if (state.running && !state.hasFrame) connectLive()
360
+
361
+ renderTabs(st.tabs || [])
362
+
363
+ var active = (st.tabs || []).filter(function (t) { return t.active })[0]
364
+ if (active && document.activeElement !== els.urlInput && active.url && active.url !== 'about:blank') {
365
+ els.urlInput.value = active.url
366
+ }
367
+ }
368
+
369
+ function pollStatus() {
370
+ if (state.statusBusy) return Promise.resolve()
371
+ state.statusBusy = true
372
+ return fetch(apiPath('/api/status')).then(function (r) { return r.json() }).then(function (data) {
373
+ renderStatus(data)
374
+ }).catch(function () { /* 服务重启间隙 */ }).finally(function () { state.statusBusy = false })
375
+ }
376
+
377
+ /* ---------- 地址栏与历史 ---------- */
378
+
379
+ function openUrl(url) {
128
380
  if (!url) return
129
381
  if (!/^https?:\/\//i.test(url)) url = 'https://' + url
130
- openUrl(url)
382
+ post(apiPath('/api/navigate'), { url: url }).then(function (data) {
383
+ if (data && data.ok !== false) connectLive()
384
+ }).finally(function () {
385
+ pollStatus()
386
+ })
131
387
  }
132
- els.urlInput.addEventListener('keydown', function (e) {
133
- if (e.key === 'Enter') els.btnOpen.click()
388
+
389
+ $('nav').addEventListener('submit', function (event) {
390
+ event.preventDefault()
391
+ openUrl(els.urlInput.value.trim())
392
+ els.urlInput.blur()
134
393
  })
135
- els.btnDemo.onclick = function () { openUrl(location.origin + '/demo/form.html') }
136
- els.btnSnapshot.onclick = function () {
137
- fetch('/api/snapshot').then(function (r) { return r.json() }).then(function (data) {
138
- if (data.ok === false) {
139
- els.snapshot.textContent = (data.error ? data.error.message : '快照失败')
140
- return
394
+
395
+ els.btnBack.onclick = function () { historyAction('back') }
396
+ els.btnForward.onclick = function () { historyAction('forward') }
397
+ els.btnReload.onclick = function () { historyAction('reload') }
398
+
399
+ /* 后退/前进/刷新走一条与 browser_history 同引擎的最小接口。 */
400
+ function historyAction(action) {
401
+ fetch(apiPath('/api/history'), {
402
+ method: 'POST',
403
+ headers: { 'content-type': 'application/json' },
404
+ body: JSON.stringify({ action: action }),
405
+ }).then(function (r) { return r.json() }).then(function (data) {
406
+ if (data && data.ok === false && data.error) toast('[' + data.error.code + '] ' + data.error.message)
407
+ pollStatus()
408
+ }).catch(function () { /* 忽略 */ })
409
+ }
410
+
411
+ /* 新建标签页:与 browser_tabs(action=new) 同源;开完聚焦地址栏,像真实浏览器。 */
412
+ els.newTab.onclick = function () {
413
+ post(apiPath('/api/tabs'), { action: 'new' }).then(function (data) {
414
+ if (data && data.ok !== false) {
415
+ connectLive()
416
+ els.urlInput.value = ''
417
+ els.urlInput.focus()
141
418
  }
142
- els.snapshot.textContent =
143
- '# ' + (data.title || '(无标题)') + '\n' + data.url + '(' + data.elements + ' 个元素' +
144
- (data.truncated ? ',已截断' : '') + ')\n\n' + data.text
145
- }).catch(function (e) { els.snapshot.textContent = '快照失败: ' + e.message })
146
- }
147
- els.btnClose.onclick = function () {
148
- if (!confirm('关闭浏览器?登录态会持久化,下次打开网址自动重启。')) return
149
- post('/api/control', { action: 'close' }).then(function () {
150
- els.view.innerHTML = '<div class="placeholder"><p>浏览器已关闭。</p><p>登录态已持久化;下次打开网址时自动重启。</p></div>'
419
+ pollStatus()
151
420
  })
152
421
  }
153
422
 
154
- pollStatus()
423
+ /* ---------- 启动 ---------- */
424
+
425
+ /* 先读一次状态:若浏览器已被空闲回收/停服挂起,但留有上次的标签页,
426
+ 就自动恢复一次(resume = 懒启动 + 标签恢复,不附带新导航),
427
+ 于是「重开这个会话还能看到上次的页面」。只在打开工作台时触发一次;
428
+ 面板开着时实时帧订阅(watching>0)会保护浏览器不被空闲回收。 */
429
+ pollStatus().then(function () {
430
+ if (!state.running && state.restorable) {
431
+ post(apiPath('/api/control'), { action: 'resume' })
432
+ .then(function () { pollStatus() })
433
+ .catch(function () { /* 恢复失败不阻塞工作台 */ })
434
+ }
435
+ })
155
436
  setInterval(pollStatus, 2000)
156
- setInterval(pollScreenshot, 1200)
437
+ /* watchdog:SSE 断流超过 3s 且浏览器在跑 → 退回截图轮询。 */
438
+ setInterval(function () {
439
+ if (!state.running) { stopFallback(); return }
440
+ if (state.fallbackTimer !== null) return
441
+ if (Date.now() - state.lastFrameAt > 3000) startFallback()
442
+ }, 2000)
443
+
444
+ connectLive()
445
+ // 提示条:画面出现后第一次点击就藏起来
446
+ els.frame.addEventListener('focus', function () { els.hint.hidden = true }, { once: true })
157
447
  })()
package/ui/index.html CHANGED
@@ -3,54 +3,44 @@
3
3
  <head>
4
4
  <meta charset="utf-8">
5
5
  <meta name="viewport" content="width=device-width, initial-scale=1">
6
- <title>DSH 浏览器操控工作台</title>
7
- <link rel="stylesheet" href="/style.css">
6
+ <title>DSH 浏览器</title>
7
+ <link rel="stylesheet" href="style.css">
8
8
  </head>
9
9
  <body>
10
- <header>
11
- <h1>DSH 浏览器操控工作台</h1>
12
- <div class="badges">
13
- <span id="badge-engine" class="badge off">浏览器未启动</span>
14
- <span id="badge-mode" class="badge" hidden>有头</span>
15
- </div>
16
- <div class="actions">
17
- <input id="url-input" type="text" placeholder="输入网址,回车打开(如 https://example.com/)">
18
- <button id="btn-open">打开</button>
19
- <button id="btn-demo" class="ghost" title="内置离线演示页">演示页</button>
20
- <button id="btn-snapshot" class="ghost">快照</button>
21
- <button id="btn-close" class="danger ghost">关闭浏览器</button>
22
- </div>
23
- </header>
10
+ <!-- 标签栏:像真实浏览器一样始终在顶部,可切换、可新建、可关闭 -->
11
+ <div class="tabstrip">
12
+ <div id="tabs" class="tabs"></div>
13
+ <button type="button" id="btn-newtab" class="newtab" title="新建标签页">+</button>
14
+ </div>
15
+
16
+ <!-- 工具栏:后退 / 前进 / 刷新 + 地址栏(回车打开) -->
17
+ <form id="nav" class="toolbar" autocomplete="off">
18
+ <button type="button" id="btn-back" class="icon" title="后退">←</button>
19
+ <button type="button" id="btn-forward" class="icon" title="前进">→</button>
20
+ <button type="button" id="btn-reload" class="icon" title="刷新">⟳</button>
21
+ <input id="url-input" type="text" placeholder="输入网址,回车打开" spellcheck="false" autocomplete="off">
22
+ </form>
24
23
 
25
24
  <main>
26
- <section id="view-panel" class="panel">
27
- <div class="panel-head">
28
- <h2>实时画面</h2>
29
- <nav id="tabs" class="tabs"></nav>
25
+ <section id="stage" class="stage">
26
+ <img id="frame" alt="当前页面实时画面(可直接点击、滚动、打字)" tabindex="0">
27
+ <div id="placeholder" class="placeholder">
28
+ <p>在地址栏输入网址,回车打开。</p>
29
+ <p class="muted">AI 调用 <code>browser_navigate</code> 时,这里也会实时跟上。</p>
30
30
  </div>
31
- <div id="view" class="view">
32
- <div class="placeholder">
33
- <p>浏览器尚未启动。</p>
34
- <p>在上方输入网址回车,或点「演示页」离线体验。<br>AI 在 DSH 里调用 <code>browser_navigate</code> 等工具时,这里会实时展示。</p>
31
+ <div id="hint" class="hint" hidden>点一下画面即可点击 / 输入</div>
32
+ <div id="wall" class="wall" hidden>
33
+ <div class="wall-card">
34
+ <h2>这是 DSH Web 的鉴权页,不是目标网站</h2>
35
+ <p id="wall-text"></p>
36
+ <p class="muted">受控浏览器与宿主浏览器登录态独立。要看 DSH 界面请用宿主浏览器;这里继续做互联网任务。</p>
35
37
  </div>
36
38
  </div>
37
39
  </section>
38
-
39
- <section id="log-panel" class="panel">
40
- <div class="panel-head"><h2>动作日志</h2><span id="counters" class="muted"></span></div>
41
- <ul id="log" class="log"><li class="muted">暂无动作。AI 或人工操作后这里按时间倒序显示。</li></ul>
42
- </section>
43
-
44
- <section id="snapshot-panel" class="panel">
45
- <div class="panel-head"><h2>元素快照</h2><span class="muted">与 browser_snapshot 同源 · ref 可直接用于工具</span></div>
46
- <pre id="snapshot" class="snapshot">点「快照」观察当前页面。</pre>
47
- </section>
48
40
  </main>
49
41
 
50
- <footer>
51
- <span>AI 侧工具:<code>mcp__browser__browser_*</code>(15 个)· MCP 端点 <code>/mcp</code> · 服务契约 <code>/healthz</code></span>
52
- </footer>
53
-
54
- <script src="/app.js"></script>
42
+ <textarea id="kbd" aria-hidden="true" tabindex="-1"></textarea>
43
+ <div id="toast" class="toast" role="status" hidden></div>
44
+ <script src="app.js"></script>
55
45
  </body>
56
46
  </html>