@capyclaw/browser-workbench 0.0.0-stage → 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 (168) hide show
  1. package/README.md +88 -2
  2. package/contract/browser-contract.json +1349 -0
  3. package/demo/form.html +112 -0
  4. package/lib/host.mjs +1 -0
  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/node_modules/@capyclaw/document-workbench/src/store/file-backend.mjs +158 -0
  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 +77 -4
  115. package/plugin.mjs +1 -0
  116. package/server/constants.mjs +16 -0
  117. package/server/contract-snapshots.mjs +81 -0
  118. package/server/contract.mjs +24 -0
  119. package/server/embedded-host.mjs +247 -0
  120. package/server/engine.mjs +2466 -0
  121. package/server/governor.mjs +189 -0
  122. package/server/http-utils.mjs +2 -0
  123. package/server/human.mjs +628 -0
  124. package/server/infra/http.mjs +2 -0
  125. package/server/infra/tuning.mjs +2 -0
  126. package/server/input.mjs +154 -0
  127. package/server/login-vault.mjs +109 -0
  128. package/server/mcp/handler.mjs +2 -0
  129. package/server/mcp/instructions.mjs +31 -0
  130. package/server/orphans.mjs +163 -0
  131. package/server/relay.mjs +198 -0
  132. package/server/routes/api.mjs +233 -0
  133. package/server/routes/assets.mjs +2 -0
  134. package/server/routes/mcp.mjs +17 -0
  135. package/server/routes/workbench.mjs +33 -0
  136. package/server/session-engines.mjs +223 -0
  137. package/server/shared/SHARED_MANIFEST.json +70 -0
  138. package/server/shared/asset-service.mjs +2 -0
  139. package/server/shared/editor-host.mjs +2 -0
  140. package/server/shared/kit/editor/lease.mjs +2 -0
  141. package/server/shared/kit/service/api.mjs +2 -0
  142. package/server/shared/kit/service/http.mjs +2 -0
  143. package/server/shared/kit/service/mcp-http.mjs +2 -0
  144. package/server/shared/kit/tools/validate.mjs +2 -0
  145. package/server/shared/kit/tuning.mjs +2 -0
  146. package/server/shared/kit/util/fs-lock.mjs +2 -0
  147. package/server/shared/service-host.mjs +2 -0
  148. package/server/shared/tool-host.mjs +2 -0
  149. package/server/shared/tuning.mjs +2 -0
  150. package/server/tools/capabilities/contract.mjs +55 -0
  151. package/server/tools/capabilities/evaluate.mjs +36 -0
  152. package/server/tools/capabilities/index.mjs +26 -0
  153. package/server/tools/capabilities/interaction.mjs +215 -0
  154. package/server/tools/capabilities/navigation.mjs +35 -0
  155. package/server/tools/capabilities/screenshot.mjs +50 -0
  156. package/server/tools/capabilities/session.mjs +81 -0
  157. package/server/tools/capabilities/snapshot.mjs +30 -0
  158. package/server/tools/capabilities/tabs.mjs +35 -0
  159. package/server/tools/index.mjs +12 -0
  160. package/server/tools/kernel/define.mjs +5 -0
  161. package/server/tools/kernel/output.mjs +30 -0
  162. package/server/tools/kernel/ports.mjs +59 -0
  163. package/server/tools/kernel/registry.mjs +17 -0
  164. package/server/tools.mjs +3 -0
  165. package/server/workbench-framework.mjs +305 -0
  166. package/ui/app.js +447 -0
  167. package/ui/index.html +46 -0
  168. package/ui/style.css +144 -0
@@ -0,0 +1,247 @@
1
+ /**
2
+ * @capyclaw/browser-workbench 宿主插件(cordis 入口,loader 经包名加载本模块)。
3
+ *
4
+ * 承载形态由**基建**决定,业务插件不选端口:
5
+ * - 有 ctx.webServer(生产):挂到宿主 HTTP 路由前缀下,不监听任何端口,
6
+ * 生命周期随 DSH 启停;前缀由基建给(默认 /capyclaw/browser)。
7
+ * - 没有 webServer(开发/测试):才退回独立 loopback 端口。
8
+ * 工具面也在这里注册:relay 的工具注册表直接进 ctx.tools,不再经过 MCP 桥。
9
+ *
10
+ * 与 ppt/canvas 的公共 host 模板不同,本文件为手写精简版:浏览器工作台没有
11
+ * 文档 store/journal/workspace 依赖,装配面只有公共服务契约 + 引擎透传配置。
12
+ */
13
+ import os from 'node:os'
14
+ import path from 'node:path'
15
+ import fs from 'node:fs'
16
+ import { fileURLToPath, pathToFileURL } from 'node:url'
17
+ import { assertServiceApi } from './shared/kit/service/api.mjs'
18
+
19
+ export const name = 'browser-workbench-host'
20
+ /* 依赖倒置:业务等的是「宿主给的 HTTP 路由」,不是端口。 */
21
+ export const inject = ['webServer']
22
+
23
+ function safeGet(ctx, serviceName) {
24
+ // 原生插件里 ctx.get(name) 只对已 inject 的服务放行;可选依赖用 reflect.get
25
+ // 才不会因为「没在 inject 列表里」被拒绝(core 也是这么取依赖的)。
26
+ try {
27
+ if (ctx && ctx.reflect && typeof ctx.reflect.get === 'function') return ctx.reflect.get(serviceName)
28
+ return ctx?.get ? ctx.get(serviceName) : null
29
+ } catch { return null }
30
+ }
31
+
32
+ function requireApi(service, name) {
33
+ if (service) assertServiceApi(service, { name, major: 1 })
34
+ return service
35
+ }
36
+
37
+ /** 基建依赖(@capyclaw/portal)不保证在工作台包的依赖树里,按约定位置兜底解析。 */
38
+ async function loadPortal() {
39
+ try {
40
+ return await import('@capyclaw/portal')
41
+ } catch { /* 落到 profile 扫描 */ }
42
+ const home = process.env.DSH_HOME || path.join(os.homedir(), '.dsh')
43
+ const roots = []
44
+ try {
45
+ for (const dir of fs.readdirSync(path.join(home, 'profiles'))) roots.push(path.join(home, 'profiles', dir, 'node_modules'))
46
+ } catch { /* 没有 profiles 目录 */ }
47
+ for (const root of roots) {
48
+ for (const scope of ['core', 'ppt-suite', 'canvas-suite', 'browser-suite']) {
49
+ const file = path.join(root, '@capyclaw', scope, 'node_modules', '@capyclaw', 'portal', 'lib', 'index.mjs')
50
+ if (!fs.existsSync(file)) continue
51
+ try {
52
+ return await import(pathToFileURL(file).href)
53
+ } catch { /* 试下一个 */ }
54
+ }
55
+ }
56
+ return null
57
+ }
58
+
59
+ /**
60
+ * 激活失败的完整栈只留在宿主日志里,插件管理器/Web UI 只显示第一行;
61
+ * 这里把栈落到 hostDir,下次「1 entry did not activate」时能直接看到根因。
62
+ */
63
+ export async function apply(ctx, config) {
64
+ try {
65
+ return await activate(ctx, config)
66
+ } catch (error) {
67
+ try {
68
+ const fallbackDir = path.join(os.homedir(), '.dsh-browser-bridge')
69
+ const hostDir = (config && config.hostDir) || process.env.BROWSER_BRIDGE_HOME || fallbackDir
70
+ fs.mkdirSync(hostDir, { recursive: true })
71
+ fs.appendFileSync(
72
+ path.join(hostDir, 'activate-error.log'),
73
+ `[${new Date().toISOString()}] ${(error && error.stack) || error}\n`,
74
+ )
75
+ } catch { /* 诊断写盘失败不能覆盖真正的激活错误 */ }
76
+ throw error
77
+ }
78
+ }
79
+
80
+ async function activate(ctx, config) {
81
+ const packageDir = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..')
82
+ const { startRelay } = await import('./relay.mjs')
83
+ const hostDir = (config && config.hostDir)
84
+ || process.env.BROWSER_BRIDGE_HOME
85
+ || path.join(os.homedir(), '.dsh-browser-bridge')
86
+ /* 公共插件 API 契约:存在则校验 major,避免静默不兼容。 */
87
+ const serviceHost = requireApi(safeGet(ctx, 'serviceHost'), 'serviceHost')
88
+ const toolHost = requireApi(safeGet(ctx, 'toolHost'), 'toolHost')
89
+ const assetService = requireApi(safeGet(ctx, 'assets'), 'assets')
90
+ let assetStore = null
91
+ if (assetService?.createFileStore) {
92
+ assetStore = assetService.createFileStore({ dir: path.join(hostDir, 'assets') })
93
+ }
94
+ /* cordis 配置透传引擎项(headless / allowEvaluate / channel / 超时…),
95
+ * 环境变量(DSH_BROWSER_*)与默认值由 relay 内 resolveEngineConfig 兜底。 */
96
+ const engineConfig = (config && config.engine) || null
97
+
98
+ const webServer = safeGet(ctx, 'webServer')
99
+ const mountPath = (config && config.mountPath) || '/capyclaw/browser'
100
+ const relayOptions = {
101
+ packageDir,
102
+ hostDir,
103
+ assetStore,
104
+ assetService,
105
+ engineConfig,
106
+ serviceHost,
107
+ toolHost,
108
+ strictPort: config?.strictPort != null ? !!config.strictPort : false,
109
+ allowReuse: config?.allowReuse != null ? !!config.allowReuse : true,
110
+ /* F3 会话模式锁的底座懒解析器(works.db 单一真源);底座缺席时守卫自动跳过。 */
111
+ getWorkStore: () => safeGet(ctx, 'workStore'),
112
+ }
113
+ if (webServer) {
114
+ relayOptions.webServer = webServer
115
+ relayOptions.mountPrefix = mountPath
116
+ } else {
117
+ /* 仅开发/测试:没有宿主路由时才自己听一个 loopback 端口。
118
+ 端口写 0 = 交给操作系统分配;真实地址由 relay listen 之后给出。 */
119
+ relayOptions.port = (config && Number(config.port)) || 0
120
+ }
121
+ const relay = await startRelay(relayOptions)
122
+
123
+ /* 工具面:注册进宿主原生工具注册表,名字保持 browser_*(不再有 mcp__browser__ 前缀)。
124
+ DSH 的 ToolDefinition 是 name / description / parameters(JSON Schema) /
125
+ output { schema, render } / execute;缺 output 时 register 直接抛 TypeError。
126
+ 逐个 try,一个工具不被支持时不影响其余工具。 */
127
+ const disposers = []
128
+ const registry = relay.tools
129
+ const tools = safeGet(ctx, 'tools')
130
+ const toolState = { registered: [], errors: [], hasTools: !!(tools && typeof tools.register === 'function') }
131
+ const OUTPUT_SCHEMA = { type: 'object', additionalProperties: true }
132
+
133
+ function toolText(value) {
134
+ if (value == null) return 'ok'
135
+ if (typeof value === 'string') return value
136
+ if (Array.isArray(value && value.content)) {
137
+ const parts = value.content
138
+ .filter((part) => part && part.type === 'text' && typeof part.text === 'string')
139
+ .map((part) => part.text)
140
+ if (parts.length) return parts.join('\n')
141
+ }
142
+ if (value && typeof value.text === 'string') return value.text
143
+ try { return JSON.stringify(value, null, 2) } catch { return String(value) }
144
+ }
145
+
146
+ if (registry && Array.isArray(registry.TOOLS) && toolState.hasTools) {
147
+ for (const tool of registry.TOOLS) {
148
+ try {
149
+ const dispose = tools.register({
150
+ name: tool.name,
151
+ description: tool.description,
152
+ parameters: tool.inputSchema,
153
+ output: {
154
+ schema: OUTPUT_SCHEMA,
155
+ render: (_args, value) => [{ type: 'text', text: toolText(value) }],
156
+ },
157
+ /* 第二个参数是 DSH 的 exec(含 exec.agent.session.id):工具靠它知道是哪个
158
+ * 会话在调用,多会话工作台才能把读写落到调用者自己那份文档上。 */
159
+ execute: async (args, exec) => registry.TOOL_IMPL[tool.name](args || {}, exec),
160
+ })
161
+ if (typeof dispose === 'function') disposers.push(dispose)
162
+ toolState.registered.push(tool.name)
163
+ } catch (error) {
164
+ toolState.errors.push(`${tool.name}: ${error && error.message ? error.message : String(error)}`)
165
+ }
166
+ }
167
+ if (toolState.errors.length) console.warn(`[browser-workbench] 工具注册失败 ${toolState.errors.length} 个: ${toolState.errors.join(' | ')}`)
168
+ } else if (registry) {
169
+ console.log('[browser-workbench] 提示:宿主没有 tools 服务,工具面未注册(只提供工作台页面)')
170
+ }
171
+
172
+ /* 能力描述符注册(互联标准 v0.3 单一真源):把工具面带 surface 注册进 tool-host
173
+ * 描述符注册表——展示墙 / 编排器 / AI 读的是同一个注册表,注册了带 surface 的能力
174
+ * 必然自动上墙,卸载即下墙。老宿主没有描述符面时优雅降级,不影响工具面与页面。 */
175
+ const descriptorState = { registered: 0, available: !!(toolHost && typeof toolHost.registerCapabilityDescriptor === 'function') }
176
+ if (registry && Array.isArray(registry.TOOLS) && descriptorState.available) {
177
+ const surface = { standalone: true, embeddable: true, embedPath: mountPath, title: (config && config.title) || '浏览器工作台' }
178
+ for (const tool of registry.TOOLS) {
179
+ try {
180
+ toolHost.registerCapabilityDescriptor({
181
+ name: tool.name,
182
+ suite: 'browser',
183
+ capability: tool.name,
184
+ tool: tool.name,
185
+ controlMode: 'browser',
186
+ kind: tool.kind || 'write',
187
+ produces: [],
188
+ consumes: [],
189
+ surface,
190
+ }, { baseUrl: relay.url })
191
+ descriptorState.registered += 1
192
+ disposers.push(() => { try { toolHost.unregisterCapabilityDescriptor(tool.name) } catch { /* 忽略 */ } })
193
+ } catch (error) {
194
+ console.warn(`[browser-workbench] 描述符注册失败 ${tool.name}: ${error && error.message ? error.message : String(error)}`)
195
+ }
196
+ }
197
+ }
198
+
199
+ /* 自检端点:GET <前缀>/__host__ 直接看工具注册结果,不用翻宿主日志。 */
200
+ if (webServer && typeof webServer.register === 'function') {
201
+ const dispose = webServer.register({
202
+ kind: 'exact',
203
+ path: `${mountPath}/__host__`,
204
+ handler: (req, res) => {
205
+ const body = JSON.stringify({
206
+ workbench: 'browser',
207
+ mode: relay.mode,
208
+ url: relay.url,
209
+ editorUrl: relay.editorUrl,
210
+ mountPath,
211
+ tools: toolState,
212
+ descriptors: descriptorState,
213
+ }, null, 2)
214
+ res.writeHead(200, { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' })
215
+ res.end(body)
216
+ },
217
+ })
218
+ if (typeof dispose === 'function') disposers.push(dispose)
219
+ }
220
+
221
+ /* 活动台账:embedded 形态没有端口,记 url 即可。台账只供主区舞台
222
+ * (@capyclaw/portal-hud)补标题/最近使用时间,开不开窗由舞台的内容闸决定。 */
223
+ try {
224
+ const portal = await loadPortal()
225
+ if (portal && typeof portal.recordActivity === 'function') {
226
+ portal.recordActivity({
227
+ id: 'browser-workbench',
228
+ url: relay.editorUrl,
229
+ title: (config && config.title) || '浏览器工作台',
230
+ kind: 'service',
231
+ })
232
+ }
233
+ } catch { /* 台账失败不影响服务 */ }
234
+
235
+ console.log(relay.reused
236
+ ? `[browser-workbench] 复用已运行的桥: ${relay.url}`
237
+ : `[browser-workbench] 桥已就绪: ${relay.url}`)
238
+ console.log(`[browser-workbench] 工作台: ${relay.editorUrl}(${relay.mode === 'embedded' ? '挂在宿主路由,无端口' : '独立端口'})`)
239
+
240
+ return async () => {
241
+ for (const dispose of disposers) {
242
+ try { dispose() } catch { /* 忽略 */ }
243
+ }
244
+ await relay.stop()
245
+ console.log('[browser-workbench] 桥已停止')
246
+ }
247
+ }