@capyclaw/browser-workbench 0.1.20 → 0.1.21
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/contract/browser-contract.json +1 -1
- package/node_modules/@capyclaw/asset-service/dsh-suite.json +1 -1
- package/node_modules/@capyclaw/asset-service/package.json +1 -1
- package/node_modules/@capyclaw/asset-service/src/index.mjs +4 -3
- package/node_modules/@capyclaw/document-workbench/package.json +6 -3
- package/node_modules/@capyclaw/document-workbench/src/client/index.mjs +56 -3
- package/node_modules/@capyclaw/document-workbench/src/client/visibility.mjs +112 -0
- package/node_modules/@capyclaw/document-workbench/src/model/manifest.mjs +6 -4
- package/node_modules/@capyclaw/document-workbench/src/model/ops.mjs +7 -2
- package/node_modules/@capyclaw/document-workbench/src/session/index.mjs +8 -0
- package/node_modules/@capyclaw/document-workbench/src/store/file-backend.mjs +17 -10
- package/node_modules/@capyclaw/document-workbench/src/store/projection.mjs +39 -0
- package/node_modules/@capyclaw/document-workbench/src/testing/index.mjs +14 -11
- package/node_modules/@capyclaw/foundation/abi/assets.mjs +26 -0
- package/node_modules/@capyclaw/foundation/abi/bridge.mjs +3 -1
- package/node_modules/@capyclaw/foundation/abi/index.mjs +15 -0
- package/node_modules/@capyclaw/foundation/abi/visibility.mjs +90 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/lifecycle-hook.mjs +21 -4
- package/node_modules/@capyclaw/foundation/lib/host/caps.mjs +5 -1
- package/node_modules/@capyclaw/foundation/lib/host/contract.mjs +5 -2
- package/node_modules/@capyclaw/foundation/lib/host/dsh.mjs +4 -0
- package/node_modules/@capyclaw/foundation/lib/kernel.mjs +13 -3
- package/node_modules/@capyclaw/foundation/package.json +1 -1
- package/node_modules/@capyclaw/plugin-kit/package.json +3 -1
- package/node_modules/@capyclaw/plugin-kit/src/editor/heartbeat-worker.js +5 -2
- package/node_modules/@capyclaw/plugin-kit/src/editor/heartbeat.mjs +26 -0
- package/node_modules/@capyclaw/plugin-kit/src/service/static.mjs +79 -0
- package/node_modules/@capyclaw/plugin-kit/src/workspace/manager.mjs +13 -5
- package/node_modules/@capyclaw/service-host/dsh-suite.json +1 -1
- package/node_modules/@capyclaw/service-host/package.json +1 -1
- package/node_modules/@capyclaw/service-host/src/index.mjs +13 -22
- package/node_modules/@capyclaw/tool-host/dsh-suite.json +1 -1
- package/node_modules/@capyclaw/tool-host/lib/host.mjs +15 -4
- package/node_modules/@capyclaw/tool-host/package.json +1 -1
- package/node_modules/@capyclaw/tool-host/src/index.mjs +19 -5
- package/node_modules/@capyclaw/tool-host/src/mode-gate.mjs +71 -3
- package/package.json +7 -7
- package/plugin.mjs +13 -1
- package/server/activate-log.mjs +66 -0
- package/server/contract.mjs +1 -1
- package/server/embedded-host.mjs +13 -1
- package/server/engine.mjs +11 -4
- package/server/routes/api.mjs +2 -2
- package/server/shared/SHARED_MANIFEST.json +4 -4
- package/ui/app.js +87 -10
|
@@ -358,9 +358,21 @@ export function buildInstructions({
|
|
|
358
358
|
*/
|
|
359
359
|
export function registerWithDsh(ctx, registry, { namespace = '', domain = '', modeGate = null } = {}) {
|
|
360
360
|
if (!ctx?.tools?.register) throw new Error('registerWithDsh: 当前 DSH 没有 ctx.tools.register')
|
|
361
|
-
/* 领域工具一旦注册即声明给模式锁,并挂全局 guard 兜底(幂等)。
|
|
362
|
-
|
|
361
|
+
/* 领域工具一旦注册即声明给模式锁,并挂全局 guard 兜底(幂等)。
|
|
362
|
+
* install 的引用计数 disposer 必须随本次注册一起释放(旧写法丢弃⇒核心工具/guard 泄漏)。 */
|
|
363
|
+
const gateDispose = modeGate ? modeGate.install(ctx.tools) : null
|
|
363
364
|
const disposers = []
|
|
365
|
+
if (typeof gateDispose === 'function') disposers.push(gateDispose)
|
|
366
|
+
/* 感知闸补挂(幂等):driver apply 那一刻 systemPrompt 可能尚未就绪,套件注册时再挂一次。
|
|
367
|
+
* 引用计数 disposer 随本次注册一起释放。cordis 服务缺席时访问可能抛错,try 包裹降级。 */
|
|
368
|
+
if (modeGate && typeof modeGate.installPrompt === 'function') {
|
|
369
|
+
let systemPrompt = null
|
|
370
|
+
try { systemPrompt = ctx.systemPrompt } catch { systemPrompt = null }
|
|
371
|
+
if (systemPrompt && typeof systemPrompt.section === 'function') {
|
|
372
|
+
const promptDispose = modeGate.installPrompt(systemPrompt)
|
|
373
|
+
if (typeof promptDispose === 'function') disposers.push(promptDispose)
|
|
374
|
+
}
|
|
375
|
+
}
|
|
364
376
|
try {
|
|
365
377
|
for (const tool of registry?.TOOLS || []) {
|
|
366
378
|
const impl = registry.TOOL_IMPL?.[tool.name]
|
|
@@ -420,8 +432,10 @@ export function createToolHost(config = {}) {
|
|
|
420
432
|
// (driver 注入 () => safeGet(ctx,'workStore'));底座缺席则降级为恒可见、不拒。
|
|
421
433
|
const modeGate = createModeGate({ getWorkStore: config.getWorkStore })
|
|
422
434
|
return {
|
|
423
|
-
|
|
424
|
-
version
|
|
435
|
+
// M12 对齐:自报以 package.json 为权威——dsh.apiVersion=1.1.0(对外契约,
|
|
436
|
+
// 消费者 assertServiceApi 只断言 major 1),version=0.3.1。旧自报 1.2.0/0.3.0 是漂移。
|
|
437
|
+
api: createServiceApi('toolHost', '1.1.0', ['dsl', 'registry', 'validate', 'contract', 'mcp', 'instructions', 'native-tools', 'descriptors', 'mode-gate']),
|
|
438
|
+
version: '0.3.1',
|
|
425
439
|
defineTool,
|
|
426
440
|
defineCapability,
|
|
427
441
|
createRegistry: (capabilities, options = {}) => createRegistry(capabilities, { ...config, ...options }),
|
|
@@ -429,7 +443,7 @@ export function createToolHost(config = {}) {
|
|
|
429
443
|
createToolDispatcher,
|
|
430
444
|
createCapabilityReport,
|
|
431
445
|
buildInstructions,
|
|
432
|
-
// 服务级注册自动带会话模式锁(when 可见性 + guard 硬拒 + 调用者会话 ALS),套件零接线。
|
|
446
|
+
// 服务级注册自动带会话模式锁(when 可见性 + guard 硬拒 + 感知提示 + 调用者会话 ALS),套件零接线。
|
|
433
447
|
registerWithDsh: (ctx, registry, options = {}) => registerWithDsh(ctx, registry, { modeGate, ...options }),
|
|
434
448
|
validateToolArguments,
|
|
435
449
|
modeGate,
|
|
@@ -5,11 +5,15 @@
|
|
|
5
5
|
* 该领域的工具;其余已注册能力(browser_* 等)从工具声明里消失,且即便被强行
|
|
6
6
|
* 调用也在分发层硬拒。所有经 tool-host 注册的能力自动受此约束。
|
|
7
7
|
*
|
|
8
|
-
*
|
|
8
|
+
* 三道原生闸在此全部落地:
|
|
9
9
|
* · when(可见性):registerWithDsh / bridge host 给每个领域工具挂 whenFor(name),
|
|
10
10
|
* DSH 每步 resolve() 求值,false 同时从声明与分发中排除该工具。
|
|
11
11
|
* · guard(分发硬拒,兜底):install(tools) 挂一个全局 ToolGuard,异领域调用返回
|
|
12
12
|
* denial。单调不可翻回,兜住任何绕过 when 的路径。
|
|
13
|
+
* · awareness(模型感知):installPrompt(systemPrompt) 注册一个动态提示 section,
|
|
14
|
+
* 每次组装按会话读锁状态,锁定即告知模型「主区工作台是唯一交付界面、用本领域
|
|
15
|
+
* 工具、勿用文件工具绕过」。缺了这道闸,when/guard 只挡得住领域工具,模型仍会
|
|
16
|
+
* 用无 domain 的内置文件工具(write/pwsh/office_*)绕过主区(2026-11-23 根因)。
|
|
13
17
|
*
|
|
14
18
|
* 领域来源:TOOL_META.domain(defineTool/defineCapability/createRegistry 声明)。
|
|
15
19
|
* 存储真源:work-store.getSessionMode(sid)(终生固化,无 unlock)。
|
|
@@ -28,6 +32,10 @@ export function createModeGate({ getWorkStore } = {}) {
|
|
|
28
32
|
const guardDisposers = []
|
|
29
33
|
let coreToolsRegistered = false
|
|
30
34
|
const coreDisposers = []
|
|
35
|
+
let installRefs = 0 // 安装方引用计数(见 install 注释)
|
|
36
|
+
let promptInstalled = false // 感知闸:动态提示 section 是否已注册
|
|
37
|
+
const promptDisposers = []
|
|
38
|
+
let promptRefs = 0 // 感知闸安装方引用计数(与 install 同语义)
|
|
31
39
|
|
|
32
40
|
function store() {
|
|
33
41
|
try { return typeof getWorkStore === 'function' ? getWorkStore() : null } catch { return null }
|
|
@@ -153,7 +161,10 @@ export function createModeGate({ getWorkStore } = {}) {
|
|
|
153
161
|
}
|
|
154
162
|
|
|
155
163
|
/** 把 guard + 核心锁定工具挂到 DSH tools 服务(幂等)。每个套件在 tools 就绪时都会调用,
|
|
156
|
-
* 故核心工具注册不依赖 tool-host driver apply 那一刻 tools 是否已就绪(加载顺序鲁棒)。返回 disposer。
|
|
164
|
+
* 故核心工具注册不依赖 tool-host driver apply 那一刻 tools 是否已就绪(加载顺序鲁棒)。返回 disposer。
|
|
165
|
+
* 引用计数:多个安装方(driver apply / 各套件 registerWithDsh)共享同一份核心注册,
|
|
166
|
+
* 任一安装方卸载只减引用;最后一个释放才真正摘 guard + 核心工具,避免「丢 disposer」泄漏
|
|
167
|
+
* 与「先卸载者拆掉幸存者底座」两种事故。 */
|
|
157
168
|
function install(tools) {
|
|
158
169
|
if (!tools) return () => {}
|
|
159
170
|
if (!guardInstalled && typeof tools.guard === 'function') {
|
|
@@ -174,7 +185,13 @@ export function createModeGate({ getWorkStore } = {}) {
|
|
|
174
185
|
}
|
|
175
186
|
coreToolsRegistered = true
|
|
176
187
|
}
|
|
188
|
+
installRefs++
|
|
189
|
+
let released = false
|
|
177
190
|
return () => {
|
|
191
|
+
if (released) return
|
|
192
|
+
released = true
|
|
193
|
+
installRefs--
|
|
194
|
+
if (installRefs > 0) return
|
|
178
195
|
for (const dispose of guardDisposers.splice(0)) {
|
|
179
196
|
try { dispose() } catch { /* 忽略重复释放 */ }
|
|
180
197
|
}
|
|
@@ -186,6 +203,57 @@ export function createModeGate({ getWorkStore } = {}) {
|
|
|
186
203
|
}
|
|
187
204
|
}
|
|
188
205
|
|
|
206
|
+
/**
|
|
207
|
+
* 感知闸文本:锁定会话里注入系统提示,让模型知道主区工作台的存在与唯一交付地位。
|
|
208
|
+
* 只约束「工作台内容任务」的交付路径,不禁止为修插件/查资料等元任务读文件。
|
|
209
|
+
*/
|
|
210
|
+
function awarenessText(kind) {
|
|
211
|
+
return `【会话工作模式锁】本会话已锁定为「${kind}」工作模式:GUI 主区界面就是「${kind}」工作台,它是本会话工作台内容任务的指定交付界面。\n`
|
|
212
|
+
+ `- 用户要求创作/编辑文档、稿件等工作台内容时,必须用 ${kind}_* 专用工具直接读写主区工作台(先加载 ${kind} 相关 skill),让用户在主区实时看到结果。\n`
|
|
213
|
+
+ `- 不得改用 write/pwsh/office_* 等通用文件工具生成独立文件来绕过主区交付;其他领域的工具与技能不适用于本会话。\n`
|
|
214
|
+
+ `- 锁终生固化、不可解锁;确需其他领域,请提示用户新开会话。锁状态可用 mode_status 查询。`
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* 感知闸:把锁状态作为动态系统提示 section 挂到 DSH systemPrompt 服务(幂等)。
|
|
219
|
+
* text 回调每次 assemble 求值(DSH 每步组装提示时调用),按 context.agent.session.id
|
|
220
|
+
* 读锁:锁定 → 注入感知文本;未锁定/无会话 → 返回 ''(空 section 自动消失,零开销)。
|
|
221
|
+
* 锁可在会话中途被认领(墙上点选),回调动态读取故无需监听 agent 生命周期。
|
|
222
|
+
* 引用计数与 install(tools) 同语义:driver apply 与各套件 registerWithDsh 共享一份注册。
|
|
223
|
+
* 底座缺席(无 systemPrompt / 不支持 section)→ 降级为仅 when/guard 生效。
|
|
224
|
+
* @returns {() => void} 引用计数 disposer
|
|
225
|
+
*/
|
|
226
|
+
function installPrompt(systemPrompt) {
|
|
227
|
+
if (!systemPrompt || typeof systemPrompt.section !== 'function') return () => {}
|
|
228
|
+
if (!promptInstalled) {
|
|
229
|
+
try {
|
|
230
|
+
const dispose = systemPrompt.section({
|
|
231
|
+
name: 'capyclaw:session-mode-lock',
|
|
232
|
+
order: 900,
|
|
233
|
+
interpolate: false,
|
|
234
|
+
text: (context) => {
|
|
235
|
+
const kind = modeOf(context?.agent?.session?.id)
|
|
236
|
+
return kind ? awarenessText(kind) : ''
|
|
237
|
+
},
|
|
238
|
+
})
|
|
239
|
+
if (typeof dispose === 'function') promptDisposers.push(dispose)
|
|
240
|
+
promptInstalled = true
|
|
241
|
+
} catch { /* 底座不支持 section 时降级:仅 when/guard 生效 */ }
|
|
242
|
+
}
|
|
243
|
+
promptRefs++
|
|
244
|
+
let released = false
|
|
245
|
+
return () => {
|
|
246
|
+
if (released) return
|
|
247
|
+
released = true
|
|
248
|
+
promptRefs--
|
|
249
|
+
if (promptRefs > 0) return
|
|
250
|
+
for (const dispose of promptDisposers.splice(0)) {
|
|
251
|
+
try { dispose() } catch { /* 忽略重复释放 */ }
|
|
252
|
+
}
|
|
253
|
+
promptInstalled = false
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
189
257
|
/**
|
|
190
258
|
* 能力发现面过滤:锁定会话里剔除异领域描述符(conductor_discover 自动受约束)。
|
|
191
259
|
* 无 domain 的描述符(编排/核心基建)保留。未锁定 → 原样返回。
|
|
@@ -200,5 +268,5 @@ export function createModeGate({ getWorkStore } = {}) {
|
|
|
200
268
|
})
|
|
201
269
|
}
|
|
202
270
|
|
|
203
|
-
return { declareTools, domainOf, modeOf, isVisible, whenFor, guard, install, filterEntries }
|
|
271
|
+
return { declareTools, domainOf, modeOf, isVisible, whenFor, guard, install, installPrompt, filterEntries }
|
|
204
272
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@capyclaw/browser-workbench",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.21",
|
|
4
4
|
"description": "浏览器操控工作台运行时:18 个 browser_* MCP 工具(导航/快照/点击/填写/按键/下拉/上传/滚动/批量连做/拖拽/标签页/截图)+ Playwright 引擎(有头可见、登录态持久化)+ 可视化工作台。",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./plugin.mjs",
|
|
@@ -46,12 +46,12 @@
|
|
|
46
46
|
"license": "MIT",
|
|
47
47
|
"scripts": {},
|
|
48
48
|
"dependencies": {
|
|
49
|
-
"@capyclaw/asset-service": "^0.1.
|
|
50
|
-
"@capyclaw/document-workbench": "^0.2.
|
|
51
|
-
"@capyclaw/foundation": "^0.1.
|
|
52
|
-
"@capyclaw/plugin-kit": "^0.2.
|
|
53
|
-
"@capyclaw/service-host": "^0.1.
|
|
54
|
-
"@capyclaw/tool-host": "^0.3.
|
|
49
|
+
"@capyclaw/asset-service": "^0.1.6",
|
|
50
|
+
"@capyclaw/document-workbench": "^0.2.4",
|
|
51
|
+
"@capyclaw/foundation": "^0.1.11",
|
|
52
|
+
"@capyclaw/plugin-kit": "^0.2.8",
|
|
53
|
+
"@capyclaw/service-host": "^0.1.7",
|
|
54
|
+
"@capyclaw/tool-host": "^0.3.2",
|
|
55
55
|
"playwright": "^1.64.0"
|
|
56
56
|
},
|
|
57
57
|
"author": {
|
package/plugin.mjs
CHANGED
|
@@ -1 +1,13 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* foundation ABI raw@1 wrapper:准入闸扩面后宿主必须走 ABI(见 bridge-build 注释)。
|
|
3
|
+
* raw@1 逃生舱承载多面共生命周期(webServer 路由 + toolHost 工具 + 描述符/表面/portal,
|
|
4
|
+
* 共享 workId/relay);额外 inject 仅 raw@1 可声明。core/os.skills 是套件运行时前提。
|
|
5
|
+
*/
|
|
6
|
+
import { createPlugin, rawPlugin } from '@capyclaw/foundation/abi'
|
|
7
|
+
import { name as hostName, apply as applyHost } from './server/embedded-host.mjs'
|
|
8
|
+
|
|
9
|
+
export const { name, inject, apply } = createPlugin({
|
|
10
|
+
name: hostName,
|
|
11
|
+
inject: ['webServer'],
|
|
12
|
+
implements: [rawPlugin(({ ctx, config }) => applyHost(ctx, config))],
|
|
13
|
+
}, import.meta.url)
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 激活失败诊断日志的有界写入(防 activate-error.log 无界增长)。
|
|
3
|
+
*
|
|
4
|
+
* 旧实现直接 fs.appendFileSync 追加完整栈,长期反复激活失败会让日志无限膨胀。
|
|
5
|
+
* 这里改为「读尾部 + 追加 + 超限裁剪 + 整体重写」:
|
|
6
|
+
* - 行数硬上限 ACTIVATE_LOG_MAX_LINES(200):每次追加只保留最近 200 行;
|
|
7
|
+
* - 字节硬上限 ACTIVATE_LOG_MAX_BYTES(256KB):仍超则按字节取尾部并丢掉
|
|
8
|
+
* 首条不完整行,保证不截断出半行乱码。
|
|
9
|
+
* 只读取文件尾部 maxBytes 字节,内存占用有界;失败信息(最近若干条栈)始终可诊断。
|
|
10
|
+
*
|
|
11
|
+
* 本模块不依赖构建期才生成的 ./shared/*,可被 src 直接 import 做回归测试。
|
|
12
|
+
*/
|
|
13
|
+
import fs from 'node:fs'
|
|
14
|
+
|
|
15
|
+
export const ACTIVATE_LOG_MAX_BYTES = 256 * 1024
|
|
16
|
+
export const ACTIVATE_LOG_MAX_LINES = 200
|
|
17
|
+
|
|
18
|
+
/** 只读文件尾部 maxBytes 字节;若从中间读起则丢掉首条不完整行。读失败返回空串。 */
|
|
19
|
+
function readTail(filePath, maxBytes) {
|
|
20
|
+
let fd
|
|
21
|
+
try {
|
|
22
|
+
fd = fs.openSync(filePath, 'r')
|
|
23
|
+
const size = fs.fstatSync(fd).size
|
|
24
|
+
const start = Math.max(0, size - maxBytes)
|
|
25
|
+
const len = size - start
|
|
26
|
+
const buf = Buffer.alloc(len)
|
|
27
|
+
fs.readSync(fd, buf, 0, len, start)
|
|
28
|
+
let text = buf.toString('utf8')
|
|
29
|
+
if (start > 0) {
|
|
30
|
+
const i = text.indexOf('\n')
|
|
31
|
+
text = i >= 0 ? text.slice(i + 1) : ''
|
|
32
|
+
}
|
|
33
|
+
return text
|
|
34
|
+
} catch {
|
|
35
|
+
return ''
|
|
36
|
+
} finally {
|
|
37
|
+
if (fd !== undefined) { try { fs.closeSync(fd) } catch { /* 忽略 */ } }
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* 向 filePath 有界追加一条 entry(自动补尾换行)。超限按行/按字节裁剪后整体重写。
|
|
43
|
+
* @param {string} filePath 目标日志文件绝对路径
|
|
44
|
+
* @param {string} entry 要追加的一条诊断文本(通常含时间戳与错误栈)
|
|
45
|
+
* @param {{maxBytes?:number, maxLines?:number}} [opts] 覆盖默认上限
|
|
46
|
+
*/
|
|
47
|
+
export function appendBoundedLog(filePath, entry, opts = {}) {
|
|
48
|
+
const maxBytes = opts.maxBytes ?? ACTIVATE_LOG_MAX_BYTES
|
|
49
|
+
const maxLines = opts.maxLines ?? ACTIVATE_LOG_MAX_LINES
|
|
50
|
+
const line = String(entry)
|
|
51
|
+
const text = line.endsWith('\n') ? line : `${line}\n`
|
|
52
|
+
let combined = readTail(filePath, maxBytes) + text
|
|
53
|
+
/* 行数硬上限:每次追加都只保留最近 maxLines 行(最新一条必在末尾)。 */
|
|
54
|
+
const lines = combined.split('\n').filter((l) => l.length > 0)
|
|
55
|
+
if (lines.length > maxLines) combined = `${lines.slice(-maxLines).join('\n')}\n`
|
|
56
|
+
/* 字节硬上限:仍超则按字节取尾部并丢首条不完整行,避免截出半行乱码。 */
|
|
57
|
+
if (Buffer.byteLength(combined, 'utf8') > maxBytes) {
|
|
58
|
+
let tail = Buffer.from(combined, 'utf8').subarray(-maxBytes).toString('utf8')
|
|
59
|
+
const i = tail.indexOf('\n')
|
|
60
|
+
tail = i >= 0 ? tail.slice(i + 1) : tail
|
|
61
|
+
/* 极端:单条 entry 本身就超过 maxBytes,按字节裁剪可能整条丢空——
|
|
62
|
+
兜底保留 entry 尾部,确保最新失败信息不丢。 */
|
|
63
|
+
combined = tail.length > 0 ? tail : text.slice(-maxBytes)
|
|
64
|
+
}
|
|
65
|
+
fs.writeFileSync(filePath, combined)
|
|
66
|
+
}
|
package/server/contract.mjs
CHANGED
|
@@ -10,7 +10,7 @@ import { createCapabilityReport } from './shared/tool-host.mjs'
|
|
|
10
10
|
export const CONTRACT_VERSION = '1.3.0'
|
|
11
11
|
|
|
12
12
|
/** 工作台包版本。 */
|
|
13
|
-
export const PACKAGE_VERSION = '0.1.
|
|
13
|
+
export const PACKAGE_VERSION = '0.1.21'
|
|
14
14
|
|
|
15
15
|
/** 生成模型可读的能力报告。 */
|
|
16
16
|
export function capabilityReport(args, workbenchUrl = '', runtime = {}) {
|
package/server/embedded-host.mjs
CHANGED
|
@@ -15,6 +15,7 @@ import path from 'node:path'
|
|
|
15
15
|
import fs from 'node:fs'
|
|
16
16
|
import { fileURLToPath, pathToFileURL } from 'node:url'
|
|
17
17
|
import { assertServiceApi } from './shared/kit/service/api.mjs'
|
|
18
|
+
import { appendBoundedLog } from './activate-log.mjs'
|
|
18
19
|
|
|
19
20
|
export const name = 'browser-workbench-host'
|
|
20
21
|
/* 依赖倒置:业务等的是「宿主给的 HTTP 路由」,不是端口。 */
|
|
@@ -68,7 +69,8 @@ export async function apply(ctx, config) {
|
|
|
68
69
|
const fallbackDir = path.join(os.homedir(), '.dsh-browser-bridge')
|
|
69
70
|
const hostDir = (config && config.hostDir) || process.env.BROWSER_BRIDGE_HOME || fallbackDir
|
|
70
71
|
fs.mkdirSync(hostDir, { recursive: true })
|
|
71
|
-
|
|
72
|
+
/* 有界写入:防反复激活失败把 activate-error.log 撑到无界(保留最近若干条栈)。 */
|
|
73
|
+
appendBoundedLog(
|
|
72
74
|
path.join(hostDir, 'activate-error.log'),
|
|
73
75
|
`[${new Date().toISOString()}] ${(error && error.stack) || error}\n`,
|
|
74
76
|
)
|
|
@@ -136,6 +138,16 @@ async function activate(ctx, config) {
|
|
|
136
138
|
modeGate.declareTools('browser', registry.TOOLS.map((t) => t.name), 'browser')
|
|
137
139
|
if (toolState.hasTools) modeGate.install(tools)
|
|
138
140
|
}
|
|
141
|
+
/* 感知闸补挂(幂等):锁定会话的系统提示里注入主区工作台感知,让模型知道主区界面
|
|
142
|
+
* 的存在与唯一交付地位(缺这道闸时模型会用内置文件工具绕过主区)。systemPrompt
|
|
143
|
+
* 缺席(老宿主)降级;引用计数 disposer 随本插件卸载释放。 */
|
|
144
|
+
if (modeGate && typeof modeGate.installPrompt === 'function') {
|
|
145
|
+
const systemPrompt = safeGet(ctx, 'systemPrompt')
|
|
146
|
+
if (systemPrompt && typeof systemPrompt.section === 'function') {
|
|
147
|
+
const promptDispose = modeGate.installPrompt(systemPrompt)
|
|
148
|
+
if (typeof promptDispose === 'function') disposers.push(promptDispose)
|
|
149
|
+
}
|
|
150
|
+
}
|
|
139
151
|
const OUTPUT_SCHEMA = { type: 'object', additionalProperties: true }
|
|
140
152
|
|
|
141
153
|
function toolText(value) {
|
package/server/engine.mjs
CHANGED
|
@@ -1784,6 +1784,8 @@ export async function createBrowserEngine({ hostDir, config, playwright, process
|
|
|
1784
1784
|
const current = caster
|
|
1785
1785
|
if (!current) return
|
|
1786
1786
|
caster = null
|
|
1787
|
+
/* 摘掉 attachCast 注册的 close 监听,否则每次切页/停播都遗留一个 once 闭包。 */
|
|
1788
|
+
try { if (current.closeHandler) current.page.off?.('close', current.closeHandler) } catch { /* 页面已关闭 */ }
|
|
1787
1789
|
try { current.session.removeAllListeners?.('Page.screencastFrame') } catch { /* 已 detach */ }
|
|
1788
1790
|
try { await current.session.send('Page.stopScreencast') } catch { /* 页面已关闭 */ }
|
|
1789
1791
|
try { await current.session.detach() } catch { /* 已断开 */ }
|
|
@@ -1801,7 +1803,10 @@ export async function createBrowserEngine({ hostDir, config, playwright, process
|
|
|
1801
1803
|
await stopCast()
|
|
1802
1804
|
const session = await context.newCDPSession(page)
|
|
1803
1805
|
const tabId = activeTabId
|
|
1804
|
-
|
|
1806
|
+
const closeHandler = () => {
|
|
1807
|
+
if (caster && caster.session === session) stopCast().catch(() => {})
|
|
1808
|
+
}
|
|
1809
|
+
caster = { page, tabId, session, closeHandler }
|
|
1805
1810
|
session.on('Page.screencastFrame', (event) => {
|
|
1806
1811
|
if (!caster || caster.session !== session || !frameSubs.size) return
|
|
1807
1812
|
/* 先 ack 再广播:ack 是「可以给下一帧了」的背压阀,晚 ack 会直接掉帧率。 */
|
|
@@ -1813,9 +1818,7 @@ export async function createBrowserEngine({ hostDir, config, playwright, process
|
|
|
1813
1818
|
height: Number(event.metadata?.deviceHeight) || cfg.viewportHeight,
|
|
1814
1819
|
})
|
|
1815
1820
|
})
|
|
1816
|
-
page.once('close',
|
|
1817
|
-
if (caster && caster.session === session) stopCast().catch(() => {})
|
|
1818
|
-
})
|
|
1821
|
+
page.once('close', closeHandler)
|
|
1819
1822
|
await session.send('Page.startScreencast', {
|
|
1820
1823
|
format: 'jpeg',
|
|
1821
1824
|
quality: Math.max(10, Math.min(Number(cfg.screencastQuality) || 60, 95)),
|
|
@@ -1933,6 +1936,10 @@ export async function createBrowserEngine({ hostDir, config, playwright, process
|
|
|
1933
1936
|
/* 无论是否落盘,都先掐掉防抖计时器:避免关闭后残留计时器再写一次日志。 */
|
|
1934
1937
|
if (journalTimer) { clearTimeout(journalTimer); journalTimer = null }
|
|
1935
1938
|
if (vaultTimer) { clearTimeout(vaultTimer); vaultTimer = null }
|
|
1939
|
+
/* 停播轮询计时器与帧订阅一并清空:stopCast 只摘当前 caster,
|
|
1940
|
+
castTimer 若还在跑会继续 attachCast,frameSubs 残留会让回收后仍以为有观众。 */
|
|
1941
|
+
if (castTimer !== null) { clearInterval(castTimer); castTimer = null }
|
|
1942
|
+
frameSubs.clear()
|
|
1936
1943
|
if (!context) {
|
|
1937
1944
|
if (!persist) { try { fs.rmSync(tabsFile, { force: true }) } catch { /* 忽略 */ } }
|
|
1938
1945
|
return false
|
package/server/routes/api.mjs
CHANGED
|
@@ -184,7 +184,7 @@ export function createApiRoutes({ engine: fallbackEngine, getEngine, embedded })
|
|
|
184
184
|
return true
|
|
185
185
|
},
|
|
186
186
|
|
|
187
|
-
/* POST /api/control { action: 'close' | 'headless', value? }
|
|
187
|
+
/* POST /api/control { action: 'close' | 'resume' | 'headless', value? }
|
|
188
188
|
* headless 切完会关掉运行中的浏览器,下次动作按新模式重启(登录态保留)。 */
|
|
189
189
|
async (req, res, u) => {
|
|
190
190
|
if (u.pathname !== '/api/control' || req.method !== 'POST') return false
|
|
@@ -208,7 +208,7 @@ export function createApiRoutes({ engine: fallbackEngine, getEngine, embedded })
|
|
|
208
208
|
}
|
|
209
209
|
send(req, res, 400, {
|
|
210
210
|
ok: false,
|
|
211
|
-
error: { code: 'ARG_INVALID', message: `不支持 ${action || '(空)'}`, hint: '可用:close / headless' },
|
|
211
|
+
error: { code: 'ARG_INVALID', message: `不支持 ${action || '(空)'}`, hint: '可用:close / resume / headless' },
|
|
212
212
|
})
|
|
213
213
|
} catch (error) {
|
|
214
214
|
sendEngineError(req, res, error)
|
|
@@ -4,28 +4,28 @@
|
|
|
4
4
|
"packages": {
|
|
5
5
|
"service-host": {
|
|
6
6
|
"package": "@capyclaw/service-host",
|
|
7
|
-
"version": "0.1.
|
|
7
|
+
"version": "0.1.7",
|
|
8
8
|
"apiVersion": "1.0.0",
|
|
9
9
|
"spec": "@capyclaw/service-host/src",
|
|
10
10
|
"sha256": "af0a57d779bfe6a66dc935320178d591ef799a7e037f7a5823cdfa1fe5878eeb"
|
|
11
11
|
},
|
|
12
12
|
"tool-host": {
|
|
13
13
|
"package": "@capyclaw/tool-host",
|
|
14
|
-
"version": "0.3.
|
|
14
|
+
"version": "0.3.2",
|
|
15
15
|
"apiVersion": "1.1.0",
|
|
16
16
|
"spec": "@capyclaw/tool-host/src",
|
|
17
17
|
"sha256": "23256dbcd5cbee0b96b33c97e4eb4e74d0e169cbdde52f91d4526f2fc0f60d3e"
|
|
18
18
|
},
|
|
19
19
|
"asset-service": {
|
|
20
20
|
"package": "@capyclaw/asset-service",
|
|
21
|
-
"version": "0.1.
|
|
21
|
+
"version": "0.1.6",
|
|
22
22
|
"apiVersion": "1.0.0",
|
|
23
23
|
"spec": "@capyclaw/asset-service/src",
|
|
24
24
|
"sha256": "311a83e95d56dbb69b739d61aced0889b64e85eddd95b883e322e0fa5be8bd6f"
|
|
25
25
|
},
|
|
26
26
|
"editor-host": {
|
|
27
27
|
"package": "@capyclaw/editor-host",
|
|
28
|
-
"version": "0.1.
|
|
28
|
+
"version": "0.1.7",
|
|
29
29
|
"apiVersion": "1.0.0",
|
|
30
30
|
"spec": "@capyclaw/editor-host/src",
|
|
31
31
|
"sha256": "c8b80dbd4f1b2bee9ef9001423a013031a21ea9a3b8897b5536fe6e5687f01b3"
|
package/ui/app.js
CHANGED
|
@@ -27,6 +27,7 @@
|
|
|
27
27
|
lastFrameAt: 0,
|
|
28
28
|
hasFrame: false,
|
|
29
29
|
fallbackTimer: null,
|
|
30
|
+
hintTimer: null,
|
|
30
31
|
statusBusy: false,
|
|
31
32
|
tabsSig: null,
|
|
32
33
|
}
|
|
@@ -81,12 +82,15 @@
|
|
|
81
82
|
els.placeholder.hidden = true
|
|
82
83
|
els.frame.classList.add('on')
|
|
83
84
|
els.hint.hidden = false
|
|
84
|
-
/* 提示只在第一帧后露 5
|
|
85
|
-
|
|
85
|
+
/* 提示只在第一帧后露 5 秒,之后让位给画面本身。句柄存起来,hideFrame 时取消,
|
|
86
|
+
避免画面已隐藏后旧定时器又把 hint 状态改回去(竞态)。 */
|
|
87
|
+
if (state.hintTimer) clearTimeout(state.hintTimer)
|
|
88
|
+
state.hintTimer = setTimeout(function () { els.hint.hidden = true; state.hintTimer = null }, 5000)
|
|
86
89
|
}
|
|
87
90
|
|
|
88
91
|
function hideFrame() {
|
|
89
92
|
state.hasFrame = false
|
|
93
|
+
if (state.hintTimer) { clearTimeout(state.hintTimer); state.hintTimer = null }
|
|
90
94
|
els.frame.classList.remove('on')
|
|
91
95
|
els.frame.removeAttribute('src')
|
|
92
96
|
els.placeholder.hidden = false
|
|
@@ -107,6 +111,9 @@
|
|
|
107
111
|
|
|
108
112
|
function connectLive() {
|
|
109
113
|
if (live) return
|
|
114
|
+
/* 渲染面挂起时不建帧订阅:SSE 关闭使 watching=frameSubs.size 归零,
|
|
115
|
+
* 引擎可空闲回收(标准 §5 的期望行为)。恢复前台由 startRenderLoops 重开。 */
|
|
116
|
+
if (renderSuspended) return
|
|
110
117
|
if (typeof EventSource !== 'function') { startFallback(); return }
|
|
111
118
|
try {
|
|
112
119
|
live = new EventSource(apiPath('/api/live'))
|
|
@@ -126,6 +133,7 @@
|
|
|
126
133
|
/* SSE 被中间层吃掉(或桌面 webview 不支持)时,退回 1.2s 截图,至少还能看。 */
|
|
127
134
|
function startFallback() {
|
|
128
135
|
if (state.fallbackTimer !== null) return
|
|
136
|
+
if (renderSuspended) return
|
|
129
137
|
state.fallbackTimer = setInterval(function () {
|
|
130
138
|
if (!state.running) return
|
|
131
139
|
fetch(apiPath('/api/screenshot')).then(function (r) {
|
|
@@ -148,6 +156,72 @@
|
|
|
148
156
|
state.fallbackTimer = null
|
|
149
157
|
}
|
|
150
158
|
|
|
159
|
+
/* ---------- 表面可见性(渲染面生命周期,标准 §2/§5) ---------- */
|
|
160
|
+
/* 本 UI 无构建、原样拷贝部署(build-plugin.mjs 的 copyDirs),不能 import 共享包,
|
|
161
|
+
* 故内联一个最小 visibility 订阅器(纯订阅,无桥)。协议常量真源:
|
|
162
|
+
* C/packages/capyclaw-foundation/abi/visibility.mjs —— 改那边必须同步这里。
|
|
163
|
+
* iframe 内 document.hidden 只反映顶层 tab,被宿主藏起来不触发 visibilitychange,
|
|
164
|
+
* 表面可见性只能由宿主 postMessage 告知;hidden 必须挂起渲染面。 */
|
|
165
|
+
var VISIBILITY_MESSAGE = 'capyclaw:workbench:visibility'
|
|
166
|
+
var VISIBILITY_ACK_MESSAGE = 'capyclaw:workbench:visibility-ack'
|
|
167
|
+
var VISIBILITY_STATES = ['foreground', 'background', 'hidden', 'detached']
|
|
168
|
+
|
|
169
|
+
var renderSuspended = false
|
|
170
|
+
var statusTimer = null
|
|
171
|
+
var watchdogTimer = null
|
|
172
|
+
|
|
173
|
+
/* watchdog:SSE 断流超过 3s 且浏览器在跑 → 退回截图轮询。 */
|
|
174
|
+
function watchdogTick() {
|
|
175
|
+
if (!state.running) { stopFallback(); return }
|
|
176
|
+
if (state.fallbackTimer !== null) return
|
|
177
|
+
if (Date.now() - state.lastFrameAt > 3000) startFallback()
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/* 渲染面循环的唯一起停点:句柄 null-guard,快速切换不会叠出重复定时器。
|
|
181
|
+
* 起 = 状态轮询 + SSE 看门狗 + 帧订阅(SSE,无 SSE 时兜底截屏)。 */
|
|
182
|
+
function startRenderLoops() {
|
|
183
|
+
if (statusTimer === null) statusTimer = setInterval(pollStatus, 2000)
|
|
184
|
+
if (watchdogTimer === null) watchdogTimer = setInterval(watchdogTick, 2000)
|
|
185
|
+
connectLive()
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/* 停 = 状态轮询 + SSE 看门狗 + 兜底截屏 + 帧订阅全清。SSE 关闭使
|
|
189
|
+
* watching=frameSubs.size 归零 → 引擎可空闲回收(期望行为,标准 §5)。 */
|
|
190
|
+
function stopRenderLoops() {
|
|
191
|
+
if (statusTimer !== null) { clearInterval(statusTimer); statusTimer = null }
|
|
192
|
+
if (watchdogTimer !== null) { clearInterval(watchdogTimer); watchdogTimer = null }
|
|
193
|
+
stopFallback()
|
|
194
|
+
if (live) { try { live.close() } catch (e) { /* ignore */ } live = null }
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/* 幂等:同态重复消息不重启/重停。hidden/detached 挂起渲染面;
|
|
198
|
+
* foreground/background 恢复(background 只是失焦、仍可见,渲染面保持运转)。 */
|
|
199
|
+
function setRenderVisibility(next) {
|
|
200
|
+
var suspend = next === 'hidden' || next === 'detached'
|
|
201
|
+
if (suspend === renderSuspended) return
|
|
202
|
+
renderSuspended = suspend
|
|
203
|
+
if (suspend) { stopRenderLoops(); return }
|
|
204
|
+
startRenderLoops()
|
|
205
|
+
/* 回前台立即补一次状态:隐藏期间引擎可能已被空闲回收,
|
|
206
|
+
* 靠这次 pollStatus 的 restorable→resume 把上次的页面找回来。 */
|
|
207
|
+
pollStatus()
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function onVisibilityMessage(event) {
|
|
211
|
+
/* 只认同源宿主(浮标/portal 与本页同 origin);跨源消息一律丢弃。 */
|
|
212
|
+
if (event.origin !== location.origin) return
|
|
213
|
+
var data = event.data
|
|
214
|
+
if (!data || data.type !== VISIBILITY_MESSAGE) return
|
|
215
|
+
if (VISIBILITY_STATES.indexOf(data.state) === -1) return
|
|
216
|
+
setRenderVisibility(data.state)
|
|
217
|
+
try {
|
|
218
|
+
if (event.source) {
|
|
219
|
+
event.source.postMessage({ type: VISIBILITY_ACK_MESSAGE, state: data.state, sid: data.sid }, event.origin)
|
|
220
|
+
}
|
|
221
|
+
} catch (e) { /* 宿主已关闭等场景忽略 */ }
|
|
222
|
+
}
|
|
223
|
+
window.addEventListener('message', onVisibilityMessage)
|
|
224
|
+
|
|
151
225
|
/* ---------- 坐标映射与输入回放 ---------- */
|
|
152
226
|
|
|
153
227
|
/** object-fit: contain 的内容框 → 页面 CSS 像素坐标。 */
|
|
@@ -433,15 +507,18 @@
|
|
|
433
507
|
.catch(function () { /* 恢复失败不阻塞工作台 */ })
|
|
434
508
|
}
|
|
435
509
|
})
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
510
|
+
/* 渲染面循环统一由 startRenderLoops 起(状态轮询 2s + SSE 看门狗 2s + 帧订阅),
|
|
511
|
+
* 宿主 visibility 消息 hidden 时经 stopRenderLoops 全停、foreground 时重建。 */
|
|
512
|
+
startRenderLoops()
|
|
513
|
+
|
|
514
|
+
/* 真实卸载:清渲染面循环并摘订阅监听(新增定时器/监听皆可清理);
|
|
515
|
+
* bfcache 冻结(persisted)保持原样——定时器被冻结而非销毁,恢复时自会续跑。 */
|
|
516
|
+
window.addEventListener('pagehide', function (event) {
|
|
517
|
+
if (event && event.persisted) return
|
|
518
|
+
stopRenderLoops()
|
|
519
|
+
window.removeEventListener('message', onVisibilityMessage)
|
|
520
|
+
})
|
|
443
521
|
|
|
444
|
-
connectLive()
|
|
445
522
|
// 提示条:画面出现后第一次点击就藏起来
|
|
446
523
|
els.frame.addEventListener('focus', function () { els.hint.hidden = true }, { once: true })
|
|
447
524
|
})()
|