@capyclaw/capyclaw-workbench 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +29 -1
- package/lib/host.mjs +84 -80
- package/lib/session-port.mjs +89 -15
- package/lib/session-watch.mjs +82 -0
- package/lib/workspace-attach.mjs +147 -0
- 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/loader.mjs +8 -1
- package/node_modules/@capyclaw/foundation/abi/spec.mjs +5 -2
- package/node_modules/@capyclaw/foundation/abi/version.mjs +63 -5
- 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/adapters/raw.mjs +11 -10
- package/node_modules/@capyclaw/foundation/lib/adapters/runtime-driver.mjs +8 -1
- 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/host/memory.mjs +29 -2
- package/node_modules/@capyclaw/foundation/lib/kernel.mjs +13 -3
- package/node_modules/@capyclaw/foundation/lib/skill/scan.mjs +31 -3
- package/node_modules/@capyclaw/foundation/lib/subsystems/net.mjs +12 -2
- 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/editor/lease.mjs +3 -82
- package/node_modules/@capyclaw/plugin-kit/src/service/mcp-http.mjs +13 -6
- package/node_modules/@capyclaw/plugin-kit/src/service/static.mjs +79 -0
- package/node_modules/@capyclaw/plugin-kit/src/tools/result.mjs +31 -15
- 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 +18 -19
- package/node_modules/@capyclaw/suite-kit/README.md +52 -1
- package/node_modules/@capyclaw/suite-kit/package.json +5 -1
- package/node_modules/@capyclaw/suite-kit/src/host.mjs +230 -0
- package/node_modules/@capyclaw/suite-kit/src/index.mjs +14 -0
- package/node_modules/@capyclaw/suite-kit/suite.schema.json +16 -0
- package/node_modules/@capyclaw/tool-host/dsh-suite.json +2 -2
- package/node_modules/@capyclaw/tool-host/lib/host.mjs +34 -4
- package/node_modules/@capyclaw/tool-host/package.json +1 -1
- package/node_modules/@capyclaw/tool-host/src/admit-images.mjs +80 -0
- package/node_modules/@capyclaw/tool-host/src/caller-session.mjs +25 -0
- package/node_modules/@capyclaw/tool-host/src/descriptors.mjs +28 -4
- package/node_modules/@capyclaw/tool-host/src/index.mjs +90 -19
- package/node_modules/@capyclaw/tool-host/src/mode-gate.mjs +272 -0
- package/package.json +2 -2
- package/plugin.mjs +17 -1
|
@@ -12,8 +12,14 @@ import { validateToolArguments } from '@capyclaw/plugin-kit/tools/validate'
|
|
|
12
12
|
import { normalizeToolResult, resultText } from '@capyclaw/plugin-kit/tools/result'
|
|
13
13
|
import { createServiceApi } from '@capyclaw/plugin-kit/service/api'
|
|
14
14
|
import { createDescriptorRegistry } from './descriptors.mjs'
|
|
15
|
+
import { createModeGate } from './mode-gate.mjs'
|
|
16
|
+
import { runWithCallerSession, currentCallerSession } from './caller-session.mjs'
|
|
17
|
+
import { admitToolResultImages, resolveAttachments } from './admit-images.mjs'
|
|
15
18
|
|
|
19
|
+
export { admitToolResultImages, resolveAttachments } from './admit-images.mjs'
|
|
16
20
|
export { createDescriptorRegistry } from './descriptors.mjs'
|
|
21
|
+
export { createModeGate } from './mode-gate.mjs'
|
|
22
|
+
export { runWithCallerSession, currentCallerSession } from './caller-session.mjs'
|
|
17
23
|
|
|
18
24
|
const KINDS = new Set(['read', 'write', 'control'])
|
|
19
25
|
|
|
@@ -23,12 +29,13 @@ export function defineTool({
|
|
|
23
29
|
inputSchema = { type: 'object', properties: {}, additionalProperties: false },
|
|
24
30
|
kind = 'write',
|
|
25
31
|
since = '1.0.0',
|
|
32
|
+
domain = '',
|
|
26
33
|
handler,
|
|
27
34
|
} = {}) {
|
|
28
35
|
if (!name || typeof name !== 'string') throw new Error('defineTool 需要 name')
|
|
29
36
|
if (typeof handler !== 'function') throw new Error(`工具 ${name} 需要 handler`)
|
|
30
37
|
if (!KINDS.has(kind)) throw new Error(`工具 ${name} 的 kind 非法: ${kind}`)
|
|
31
|
-
return Object.freeze({ name, description, inputSchema, kind, since, handler })
|
|
38
|
+
return Object.freeze({ name, description, inputSchema, kind, since, domain: String(domain || ''), handler })
|
|
32
39
|
}
|
|
33
40
|
|
|
34
41
|
export function defineCapability({
|
|
@@ -40,6 +47,7 @@ export function defineCapability({
|
|
|
40
47
|
dependsOn = [],
|
|
41
48
|
provides = {},
|
|
42
49
|
features = [],
|
|
50
|
+
domain = '',
|
|
43
51
|
tools = [],
|
|
44
52
|
} = {}) {
|
|
45
53
|
if (!id || typeof id !== 'string') throw new Error('defineCapability 需要 id')
|
|
@@ -53,6 +61,7 @@ export function defineCapability({
|
|
|
53
61
|
dependsOn: Object.freeze([...dependsOn]),
|
|
54
62
|
provides: Object.freeze({ ...provides }),
|
|
55
63
|
features: Object.freeze([...features]),
|
|
64
|
+
domain: String(domain || ''),
|
|
56
65
|
tools: Object.freeze([...tools]),
|
|
57
66
|
})
|
|
58
67
|
}
|
|
@@ -99,7 +108,7 @@ function selectCapabilities(capabilities, enabled) {
|
|
|
99
108
|
* @param {Array} capabilities
|
|
100
109
|
* @param {{ enabled?: string[], ports?: object, report?: Function, reserved?: string[] }} options
|
|
101
110
|
*/
|
|
102
|
-
export function createRegistry(capabilities, { enabled = null, ports = null, report = null, reserved = [] } = {}) {
|
|
111
|
+
export function createRegistry(capabilities, { enabled = null, ports = null, report = null, reserved = [], domain = '' } = {}) {
|
|
103
112
|
validateCapabilities(capabilities)
|
|
104
113
|
const selected = selectCapabilities(capabilities, enabled)
|
|
105
114
|
|
|
@@ -145,11 +154,12 @@ export function createRegistry(capabilities, { enabled = null, ports = null, rep
|
|
|
145
154
|
title: cap.title,
|
|
146
155
|
description: cap.description,
|
|
147
156
|
tags: [...cap.tags],
|
|
157
|
+
domain: cap.domain || domain || '',
|
|
148
158
|
tools: cap.tools.map((tool) => tool.name),
|
|
149
159
|
})
|
|
150
160
|
for (const tool of cap.tools) {
|
|
151
161
|
TOOLS.push({ name: tool.name, description: tool.description, inputSchema: tool.inputSchema })
|
|
152
|
-
TOOL_META[tool.name] = { kind: tool.kind, since: tool.since, capability: cap.id }
|
|
162
|
+
TOOL_META[tool.name] = { kind: tool.kind, since: tool.since, capability: cap.id, domain: tool.domain || cap.domain || domain || '' }
|
|
153
163
|
TOOL_IMPL[tool.name] = (args) => tool.handler(args || {}, ctx)
|
|
154
164
|
}
|
|
155
165
|
}
|
|
@@ -166,7 +176,28 @@ export function createRegistry(capabilities, { enabled = null, ports = null, rep
|
|
|
166
176
|
}),
|
|
167
177
|
}
|
|
168
178
|
|
|
169
|
-
|
|
179
|
+
/* 动态增删工具:配合 MCP 活引用(tools/list 读 registry.TOOLS),注册/注销后
|
|
180
|
+
* 客户端重新 list 即见最新工具集,无需重建 handler。旧 registry 是纯静态,
|
|
181
|
+
* 套件热重载/能力开关无法反映到工具面。 */
|
|
182
|
+
function registerTool(tool, capabilityId = '') {
|
|
183
|
+
if (!tool || typeof tool.name !== 'string' || !tool.name) throw new Error('registerTool 需要 tool.name')
|
|
184
|
+
if (typeof tool.handler !== 'function') throw new Error(`工具 ${tool.name} 需要 handler`)
|
|
185
|
+
if (TOOL_IMPL[tool.name]) throw new Error(`工具已存在: ${tool.name}`)
|
|
186
|
+
TOOLS.push({ name: tool.name, description: tool.description || '', inputSchema: tool.inputSchema || { type: 'object', properties: {} } })
|
|
187
|
+
TOOL_META[tool.name] = { kind: tool.kind || 'write', since: tool.since || '1.0.0', capability: capabilityId, domain: tool.domain || domain || '' }
|
|
188
|
+
TOOL_IMPL[tool.name] = (args) => tool.handler(args || {}, ctx)
|
|
189
|
+
return true
|
|
190
|
+
}
|
|
191
|
+
function unregisterTool(name) {
|
|
192
|
+
const index = TOOLS.findIndex((tool) => tool.name === name)
|
|
193
|
+
if (index < 0) return false
|
|
194
|
+
TOOLS.splice(index, 1)
|
|
195
|
+
delete TOOL_IMPL[name]
|
|
196
|
+
delete TOOL_META[name]
|
|
197
|
+
return true
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
return { TOOLS, TOOL_IMPL, TOOL_META, FEATURES, CAPABILITIES, ctx, registerTool, unregisterTool }
|
|
170
201
|
}
|
|
171
202
|
|
|
172
203
|
/** 与 MCP 工具面解耦:调用前统一校验。 */
|
|
@@ -199,21 +230,26 @@ export function createToolDispatcher({ tools, callTool }) {
|
|
|
199
230
|
* { tools, callTool }(旧 relay 形态)
|
|
200
231
|
* @param {{ registry?: object, tools?: Array, toolImpl?: object, callTool?: Function, serverInfo?: object, instructions?: string }} options
|
|
201
232
|
*/
|
|
202
|
-
export function createMcpHandler({ registry, tools, toolImpl, callTool, serverInfo, instructions } = {}) {
|
|
203
|
-
|
|
233
|
+
export function createMcpHandler({ registry, tools, toolImpl, callTool, serverInfo, instructions, listChanged } = {}) {
|
|
234
|
+
/* registry 形态走活引用:tools/list 每次读 registry.TOOLS,callTool 读 registry.TOOL_IMPL,
|
|
235
|
+
* 于是 registerTool/unregisterTool 的动态增删立刻可见,无需重建 handler。 */
|
|
236
|
+
const useRegistry = !tools && !!registry
|
|
204
237
|
const impl = toolImpl || registry?.TOOL_IMPL || null
|
|
205
|
-
|
|
238
|
+
const staticList = tools || null
|
|
239
|
+
if (!useRegistry && (!Array.isArray(staticList) || !staticList.length)) throw new Error('createMcpHandler: 没有工具可暴露')
|
|
206
240
|
if (!impl && typeof callTool !== 'function') throw new Error('createMcpHandler: 需要 toolImpl 或 callTool')
|
|
207
241
|
return createKitMcpHandler({
|
|
208
|
-
tools:
|
|
242
|
+
tools: useRegistry ? () => registry.TOOLS : staticList,
|
|
209
243
|
callTool: (name, args, context) => {
|
|
210
244
|
if (typeof callTool === 'function' && !impl) return callTool(name, args, context)
|
|
211
|
-
const
|
|
245
|
+
const source = useRegistry ? registry.TOOL_IMPL : impl
|
|
246
|
+
const fn = source[name]
|
|
212
247
|
if (!fn) throw new Error(`未知工具: ${name}`)
|
|
213
248
|
return fn(args, context)
|
|
214
249
|
},
|
|
215
250
|
serverInfo: serverInfo || { name: 'dsh-tool-host', version: '0.1.0' },
|
|
216
251
|
instructions,
|
|
252
|
+
listChanged,
|
|
217
253
|
})
|
|
218
254
|
}
|
|
219
255
|
|
|
@@ -315,17 +351,39 @@ export function buildInstructions({
|
|
|
315
351
|
*
|
|
316
352
|
* @param {object} ctx DSH cordis context
|
|
317
353
|
* @param {object} registry createRegistry() 的返回值
|
|
318
|
-
* @param {{ namespace?: string }} options
|
|
354
|
+
* @param {{ namespace?: string, domain?: string, modeGate?: object }} options
|
|
355
|
+
* domain:本 registry 的兜底领域(工具/能力未各自声明时用);
|
|
356
|
+
* modeGate:会话模式锁闸(createToolHost 服务级自动注入;独立调用可显式传)。
|
|
319
357
|
* @returns {() => void} 反注册全部工具
|
|
320
358
|
*/
|
|
321
|
-
export function registerWithDsh(ctx, registry, { namespace = '' } = {}) {
|
|
359
|
+
export function registerWithDsh(ctx, registry, { namespace = '', domain = '', modeGate = null } = {}) {
|
|
322
360
|
if (!ctx?.tools?.register) throw new Error('registerWithDsh: 当前 DSH 没有 ctx.tools.register')
|
|
361
|
+
/* 领域工具一旦注册即声明给模式锁,并挂全局 guard 兜底(幂等)。
|
|
362
|
+
* install 的引用计数 disposer 必须随本次注册一起释放(旧写法丢弃⇒核心工具/guard 泄漏)。 */
|
|
363
|
+
const gateDispose = modeGate ? modeGate.install(ctx.tools) : null
|
|
323
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
|
+
}
|
|
324
376
|
try {
|
|
325
377
|
for (const tool of registry?.TOOLS || []) {
|
|
326
378
|
const impl = registry.TOOL_IMPL?.[tool.name]
|
|
327
379
|
if (typeof impl !== 'function') throw new Error(`registerWithDsh: 工具缺少实现 ${tool.name}`)
|
|
328
380
|
const name = namespace ? `${namespace}_${tool.name}` : tool.name
|
|
381
|
+
const toolDomain = registry.TOOL_META?.[tool.name]?.domain || domain || ''
|
|
382
|
+
let when
|
|
383
|
+
if (modeGate && toolDomain) {
|
|
384
|
+
modeGate.declareTools(toolDomain, [name])
|
|
385
|
+
when = modeGate.whenFor(name)
|
|
386
|
+
}
|
|
329
387
|
disposers.push(ctx.tools.register({
|
|
330
388
|
name,
|
|
331
389
|
description: tool.description || '',
|
|
@@ -338,16 +396,20 @@ export function registerWithDsh(ctx, registry, { namespace = '' } = {}) {
|
|
|
338
396
|
? value.content
|
|
339
397
|
: normalizeToolResult(value).content),
|
|
340
398
|
},
|
|
341
|
-
execute: async (args, exec) => {
|
|
399
|
+
execute: async (args, exec) => runWithCallerSession(exec?.agent?.session?.id, async () => {
|
|
342
400
|
const normalized = normalizeToolResult(await impl(args, exec))
|
|
343
401
|
/* DSH 原生面成功恒 isError:false,失败必须 throw 才走错误路径(B 轴 P0-1)。
|
|
344
402
|
* 把工具自报的 isError:true 转成 throw,与 MCP 面的失败语义对齐。 */
|
|
345
403
|
if (normalized.isError === true) {
|
|
346
404
|
throw new Error(resultText(normalized) || `工具 ${name} 执行失败`)
|
|
347
405
|
}
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
406
|
+
/* 图像准入:裸 base64 图像块(rsp_screen/browser_screenshot 等)必须先持久化成
|
|
407
|
+
* attachment ref 再入 session 日志,否则下一步序列化读 block.attachment.attachmentId
|
|
408
|
+
* 抛错、永久毒化该会话(2026-11-22 根因)。MCP 面不走这里,保留 wire 形态。 */
|
|
409
|
+
const content = await admitToolResultImages(normalized.content, resolveAttachments(exec, ctx), { name })
|
|
410
|
+
return { ...normalized, content }
|
|
411
|
+
}),
|
|
412
|
+
}, when ? { when } : {}))
|
|
351
413
|
}
|
|
352
414
|
} catch (error) {
|
|
353
415
|
/* 全有或全无:任一工具注册失败,逆序释放已注册的,避免部分注册泄漏(B 轴 P0-6)。 */
|
|
@@ -366,9 +428,14 @@ export function registerWithDsh(ctx, registry, { namespace = '' } = {}) {
|
|
|
366
428
|
export function createToolHost(config = {}) {
|
|
367
429
|
// 跨套件能力发现注册表(互联标准 §A.4/§E)。深度校验器可选注入(suite-kit)。
|
|
368
430
|
const descriptors = createDescriptorRegistry({ validate: config.validateDescriptor })
|
|
431
|
+
// 会话模式锁:全服务共享一个闸实例(所有套件的注册都经此)。getWorkStore 惰性解析
|
|
432
|
+
// (driver 注入 () => safeGet(ctx,'workStore'));底座缺席则降级为恒可见、不拒。
|
|
433
|
+
const modeGate = createModeGate({ getWorkStore: config.getWorkStore })
|
|
369
434
|
return {
|
|
370
|
-
|
|
371
|
-
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',
|
|
372
439
|
defineTool,
|
|
373
440
|
defineCapability,
|
|
374
441
|
createRegistry: (capabilities, options = {}) => createRegistry(capabilities, { ...config, ...options }),
|
|
@@ -376,12 +443,16 @@ export function createToolHost(config = {}) {
|
|
|
376
443
|
createToolDispatcher,
|
|
377
444
|
createCapabilityReport,
|
|
378
445
|
buildInstructions,
|
|
379
|
-
|
|
446
|
+
// 服务级注册自动带会话模式锁(when 可见性 + guard 硬拒 + 感知提示 + 调用者会话 ALS),套件零接线。
|
|
447
|
+
registerWithDsh: (ctx, registry, options = {}) => registerWithDsh(ctx, registry, { modeGate, ...options }),
|
|
380
448
|
validateToolArguments,
|
|
449
|
+
modeGate,
|
|
381
450
|
// 能力描述符注册/发现(编排器按类型动态发现,套件零互依赖)。
|
|
382
451
|
registerCapabilityDescriptor: (descriptor, options) => descriptors.register(descriptor, options),
|
|
383
452
|
unregisterCapabilityDescriptor: (name) => descriptors.unregister(name),
|
|
384
|
-
|
|
453
|
+
// 发现面按调用者会话过滤:原生工具调用里自动从 ALS 取会话(conductor_discover 无需接线);
|
|
454
|
+
// HTTP/墙等管理面无会话 → 不过滤(全局视图)。query.sessionId 可显式覆盖。
|
|
455
|
+
discoverCapabilities: (query = {}) => modeGate.filterEntries(descriptors.discover(query), query.sessionId || currentCallerSession()),
|
|
385
456
|
capabilityCatalog: () => descriptors.catalog(),
|
|
386
457
|
listSurfaces: () => descriptors.listSurfaces(),
|
|
387
458
|
}
|
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 会话模式锁 —— 工具面标准框架的强制闸(基础设施能力,非各套件自接)。
|
|
3
|
+
*
|
|
4
|
+
* 设计目标(用户诉求):锁定一种工作模式(如 rsp)后,本会话只看得见、调得动
|
|
5
|
+
* 该领域的工具;其余已注册能力(browser_* 等)从工具声明里消失,且即便被强行
|
|
6
|
+
* 调用也在分发层硬拒。所有经 tool-host 注册的能力自动受此约束。
|
|
7
|
+
*
|
|
8
|
+
* 三道原生闸在此全部落地:
|
|
9
|
+
* · when(可见性):registerWithDsh / bridge host 给每个领域工具挂 whenFor(name),
|
|
10
|
+
* DSH 每步 resolve() 求值,false 同时从声明与分发中排除该工具。
|
|
11
|
+
* · guard(分发硬拒,兜底):install(tools) 挂一个全局 ToolGuard,异领域调用返回
|
|
12
|
+
* denial。单调不可翻回,兜住任何绕过 when 的路径。
|
|
13
|
+
* · awareness(模型感知):installPrompt(systemPrompt) 注册一个动态提示 section,
|
|
14
|
+
* 每次组装按会话读锁状态,锁定即告知模型「主区工作台是唯一交付界面、用本领域
|
|
15
|
+
* 工具、勿用文件工具绕过」。缺了这道闸,when/guard 只挡得住领域工具,模型仍会
|
|
16
|
+
* 用无 domain 的内置文件工具(write/pwsh/office_*)绕过主区(2026-11-23 根因)。
|
|
17
|
+
*
|
|
18
|
+
* 领域来源:TOOL_META.domain(defineTool/defineCapability/createRegistry 声明)。
|
|
19
|
+
* 存储真源:work-store.getSessionMode(sid)(终生固化,无 unlock)。
|
|
20
|
+
* 降级:无 workStore / 无 sid / 未锁定 → 恒可见、不拒(与既有“底座缺席不启用”一致)。
|
|
21
|
+
* 无 domain 的工具(conductor/capyclaw/框架核心 mode_*)恒可见,不受锁限制。
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* @param {{ getWorkStore?: () => object|null }} deps
|
|
26
|
+
*/
|
|
27
|
+
export function createModeGate({ getWorkStore } = {}) {
|
|
28
|
+
const domainByTool = new Map() // 原生工具名 → domain
|
|
29
|
+
const domainBySuite = new Map() // 套件名 → domain(描述符发现面按 suite 兜底)
|
|
30
|
+
const lockCache = new Map() // sid → kind(锁终生固化,一旦落定永不变更,可缓存)
|
|
31
|
+
let guardInstalled = false
|
|
32
|
+
const guardDisposers = []
|
|
33
|
+
let coreToolsRegistered = false
|
|
34
|
+
const coreDisposers = []
|
|
35
|
+
let installRefs = 0 // 安装方引用计数(见 install 注释)
|
|
36
|
+
let promptInstalled = false // 感知闸:动态提示 section 是否已注册
|
|
37
|
+
const promptDisposers = []
|
|
38
|
+
let promptRefs = 0 // 感知闸安装方引用计数(与 install 同语义)
|
|
39
|
+
|
|
40
|
+
function store() {
|
|
41
|
+
try { return typeof getWorkStore === 'function' ? getWorkStore() : null } catch { return null }
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** 当前会话锁定的工作模式 kind;未锁定/底座缺席/无 sid → null。 */
|
|
45
|
+
function modeOf(sessionId) {
|
|
46
|
+
const sid = String(sessionId || '').trim()
|
|
47
|
+
if (!sid) return null
|
|
48
|
+
if (lockCache.has(sid)) return lockCache.get(sid)
|
|
49
|
+
const ws = store()
|
|
50
|
+
if (!ws || typeof ws.getSessionMode !== 'function') return null
|
|
51
|
+
let rec = null
|
|
52
|
+
try { rec = ws.getSessionMode(sid) } catch { return null }
|
|
53
|
+
const kind = rec && rec.kind ? String(rec.kind) : null
|
|
54
|
+
if (kind) lockCache.set(sid, kind) // 只缓存已落定的锁;null 不缓存(可能稍后被认领)
|
|
55
|
+
return kind
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** 领域工具在指定会话里是否可见。无 domain / 未锁定 / 同领域 → 可见。 */
|
|
59
|
+
function isVisible(domain, sessionId) {
|
|
60
|
+
if (!domain) return true
|
|
61
|
+
const lock = modeOf(sessionId)
|
|
62
|
+
return !lock || domain === lock
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** 声明一组工具属于某领域(registerWithDsh / bridge host 注册时调用)。 */
|
|
66
|
+
function declareTools(domain, names = [], suite = domain) {
|
|
67
|
+
const d = String(domain || '').trim()
|
|
68
|
+
if (!d) return
|
|
69
|
+
for (const name of names || []) {
|
|
70
|
+
if (name) domainByTool.set(String(name), d)
|
|
71
|
+
}
|
|
72
|
+
if (suite) domainBySuite.set(String(suite), d)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** 某工具的领域;未声明 → ''(恒可见)。 */
|
|
76
|
+
function domainOf(name) {
|
|
77
|
+
return domainByTool.get(String(name)) || ''
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* DSH register 的 when 条件。未声明领域的工具返回 undefined(不挂 when,恒可见,
|
|
82
|
+
* 也避免给内置/核心工具增加每步求值开销)。
|
|
83
|
+
* @returns {((context: object) => boolean)|undefined}
|
|
84
|
+
*/
|
|
85
|
+
function whenFor(name) {
|
|
86
|
+
const domain = domainOf(name)
|
|
87
|
+
if (!domain) return undefined
|
|
88
|
+
return (context) => isVisible(domain, context?.agent?.session?.id)
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* 全局 ToolGuard(分发硬拒兜底)。非领域工具立即放行(不读 store,内置工具零开销);
|
|
93
|
+
* 领域工具在异领域锁定的会话里返回 denial。
|
|
94
|
+
* @returns {{reason: string, info: object}|undefined}
|
|
95
|
+
*/
|
|
96
|
+
function guard(execution) {
|
|
97
|
+
const name = execution?.name
|
|
98
|
+
const domain = domainByTool.get(String(name))
|
|
99
|
+
if (!domain) return undefined
|
|
100
|
+
const lock = modeOf(execution?.agent?.session?.id)
|
|
101
|
+
if (!lock || domain === lock) return undefined
|
|
102
|
+
return {
|
|
103
|
+
reason: `本会话已锁定为「${lock}」工作模式,「${name}」属于「${domain}」领域,已被框架屏蔽。`
|
|
104
|
+
+ `请只用「${lock}」领域的工具完成任务;确需其他领域,请新开一个会话。`,
|
|
105
|
+
info: { name: 'SessionModeLocked', code: 'SESSION_MODE_LOCKED', reason: `locked to ${lock}, tool domain ${domain}` },
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** 框架核心工具 mode_lock / mode_status(无领域,恒可见)——显式锁定入口与状态查询。
|
|
110
|
+
* 存储经 modeGate 的 getWorkStore 解析(不依赖插件 ctx),故可在任何 tools 就绪时机注册。 */
|
|
111
|
+
function coreTools() {
|
|
112
|
+
const textRender = (_args, value) => [{ type: 'text', text: typeof value === 'string' ? value : JSON.stringify(value, null, 2) }]
|
|
113
|
+
const OUTPUT_SCHEMA = { type: 'object', additionalProperties: true }
|
|
114
|
+
const modeLock = {
|
|
115
|
+
name: 'mode_lock',
|
|
116
|
+
description: '把当前会话锁定到一种工作模式(领域),如 rsp/browser/ppt/canvas。锁定后本会话只看得见、调得动该领域的工具,'
|
|
117
|
+
+ '其余领域能力被框架从工具面屏蔽并在分发层硬拒。锁终生固化、不可解锁;确需其他领域请新开一个会话。',
|
|
118
|
+
parameters: {
|
|
119
|
+
type: 'object',
|
|
120
|
+
properties: { kind: { type: 'string', description: '要锁定的工作模式/领域,如 rsp、browser、ppt、canvas' } },
|
|
121
|
+
required: ['kind'],
|
|
122
|
+
additionalProperties: false,
|
|
123
|
+
},
|
|
124
|
+
output: { schema: OUTPUT_SCHEMA, render: textRender },
|
|
125
|
+
execute: async (args, exec) => {
|
|
126
|
+
const kind = String(args?.kind || '').trim()
|
|
127
|
+
if (!kind) return { ok: false, error: '需要 kind(要锁定的工作模式/领域)' }
|
|
128
|
+
const sid = exec?.agent?.session?.id
|
|
129
|
+
if (!sid) return { ok: false, error: '无法识别当前会话,不能锁定' }
|
|
130
|
+
const ws = store()
|
|
131
|
+
if (!ws || typeof ws.claimSessionMode !== 'function') return { ok: false, error: 'work-store 底座缺席,无法锁定' }
|
|
132
|
+
const res = ws.claimSessionMode(sid, kind, 'mode-lock', { explicit: true })
|
|
133
|
+
if (res && res.ok === false) {
|
|
134
|
+
return { ok: false, lockedTo: res.lockedTo, error: `本会话已固化为「${res.lockedTo}」模式,不能改为「${kind}」。确需其他领域请新开一个会话。` }
|
|
135
|
+
}
|
|
136
|
+
return {
|
|
137
|
+
ok: true, kind: res.kind, fresh: !!res.fresh, explicit: true,
|
|
138
|
+
message: res.fresh ? `已锁定为「${res.kind}」工作模式,本会话此后只用该领域工具。` : `本会话已处于「${res.kind}」工作模式。`,
|
|
139
|
+
}
|
|
140
|
+
},
|
|
141
|
+
}
|
|
142
|
+
const modeStatus = {
|
|
143
|
+
name: 'mode_status',
|
|
144
|
+
description: '查询当前会话的工作模式锁状态:是否锁定、锁定到哪个领域、由谁何时认领。未锁定时所有领域工具可见。',
|
|
145
|
+
parameters: { type: 'object', properties: {}, additionalProperties: false },
|
|
146
|
+
output: { schema: OUTPUT_SCHEMA, render: textRender },
|
|
147
|
+
execute: async (_args, exec) => {
|
|
148
|
+
const sid = exec?.agent?.session?.id
|
|
149
|
+
const ws = store()
|
|
150
|
+
if (!ws || typeof ws.getSessionMode !== 'function') return { locked: false, reason: 'work-store 底座缺席' }
|
|
151
|
+
if (!sid) return { locked: false, reason: '无法识别当前会话' }
|
|
152
|
+
const rec = ws.getSessionMode(sid)
|
|
153
|
+
if (!rec) return { locked: false, kind: null, message: '本会话未锁定工作模式,所有领域工具可见。' }
|
|
154
|
+
return {
|
|
155
|
+
locked: true, kind: rec.kind, claimedBy: rec.claimedBy || '', claimedAt: rec.claimedAt || '', explicit: !!rec.explicit,
|
|
156
|
+
message: `本会话已锁定为「${rec.kind}」工作模式,其余领域工具已被屏蔽。`,
|
|
157
|
+
}
|
|
158
|
+
},
|
|
159
|
+
}
|
|
160
|
+
return [modeLock, modeStatus]
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** 把 guard + 核心锁定工具挂到 DSH tools 服务(幂等)。每个套件在 tools 就绪时都会调用,
|
|
164
|
+
* 故核心工具注册不依赖 tool-host driver apply 那一刻 tools 是否已就绪(加载顺序鲁棒)。返回 disposer。
|
|
165
|
+
* 引用计数:多个安装方(driver apply / 各套件 registerWithDsh)共享同一份核心注册,
|
|
166
|
+
* 任一安装方卸载只减引用;最后一个释放才真正摘 guard + 核心工具,避免「丢 disposer」泄漏
|
|
167
|
+
* 与「先卸载者拆掉幸存者底座」两种事故。 */
|
|
168
|
+
function install(tools) {
|
|
169
|
+
if (!tools) return () => {}
|
|
170
|
+
if (!guardInstalled && typeof tools.guard === 'function') {
|
|
171
|
+
try {
|
|
172
|
+
const dispose = tools.guard(guard)
|
|
173
|
+
if (typeof dispose === 'function') guardDisposers.push(dispose)
|
|
174
|
+
guardInstalled = true
|
|
175
|
+
} catch { /* 底座不支持 guard 时降级:仅 when 生效 */ }
|
|
176
|
+
}
|
|
177
|
+
if (!coreToolsRegistered && typeof tools.register === 'function') {
|
|
178
|
+
for (const tool of coreTools()) {
|
|
179
|
+
try {
|
|
180
|
+
const d = tools.register(tool)
|
|
181
|
+
if (typeof d === 'function') coreDisposers.push(d)
|
|
182
|
+
} catch (err) {
|
|
183
|
+
console.warn(`[tool-host] 注册核心工具 ${tool.name} 失败:`, err?.message || err)
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
coreToolsRegistered = true
|
|
187
|
+
}
|
|
188
|
+
installRefs++
|
|
189
|
+
let released = false
|
|
190
|
+
return () => {
|
|
191
|
+
if (released) return
|
|
192
|
+
released = true
|
|
193
|
+
installRefs--
|
|
194
|
+
if (installRefs > 0) return
|
|
195
|
+
for (const dispose of guardDisposers.splice(0)) {
|
|
196
|
+
try { dispose() } catch { /* 忽略重复释放 */ }
|
|
197
|
+
}
|
|
198
|
+
for (const dispose of coreDisposers.splice(0)) {
|
|
199
|
+
try { dispose() } catch { /* 忽略重复释放 */ }
|
|
200
|
+
}
|
|
201
|
+
guardInstalled = false
|
|
202
|
+
coreToolsRegistered = false
|
|
203
|
+
}
|
|
204
|
+
}
|
|
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
|
+
|
|
257
|
+
/**
|
|
258
|
+
* 能力发现面过滤:锁定会话里剔除异领域描述符(conductor_discover 自动受约束)。
|
|
259
|
+
* 无 domain 的描述符(编排/核心基建)保留。未锁定 → 原样返回。
|
|
260
|
+
*/
|
|
261
|
+
function filterEntries(entries, sessionId) {
|
|
262
|
+
const lock = modeOf(sessionId)
|
|
263
|
+
if (!lock) return entries
|
|
264
|
+
return (entries || []).filter((entry) => {
|
|
265
|
+
const d = entry?.descriptor || {}
|
|
266
|
+
const domain = domainByTool.get(String(d.tool || d.name)) || domainBySuite.get(String(entry?.suite)) || ''
|
|
267
|
+
return !domain || domain === lock
|
|
268
|
+
})
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
return { declareTools, domainOf, modeOf, isVisible, whenFor, guard, install, installPrompt, filterEntries }
|
|
272
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@capyclaw/capyclaw-workbench",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "CapyClaw 子会话工作台运行时:7 个 capyclaw_* 工具(创建/驱动/查询/关闭 DSH 子会话 + 读写设置)+ 俄罗斯套娃控制台(同源嵌入 DSH GUI)。挂在 dsh 的 /capyclaw/capyclaw。",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./plugin.mjs",
|
|
@@ -49,7 +49,7 @@
|
|
|
49
49
|
"@capyclaw/plugin-kit": "^0.2.6",
|
|
50
50
|
"@capyclaw/service-host": "^0.1.5",
|
|
51
51
|
"@capyclaw/suite-kit": "^0.2.0",
|
|
52
|
-
"@capyclaw/tool-host": "^0.
|
|
52
|
+
"@capyclaw/tool-host": "^0.3.0"
|
|
53
53
|
},
|
|
54
54
|
"author": {
|
|
55
55
|
"name": "CapyClaw",
|
package/plugin.mjs
CHANGED
|
@@ -1 +1,17 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* @capyclaw/capyclaw-workbench 宿主入口:foundation ABI raw@1 逃生舱。
|
|
3
|
+
*
|
|
4
|
+
* 宿主在一次 apply 里同时装配共享 webServer 路由面、toolHost 工具面(capyclaw_* 会话工具)、
|
|
5
|
+
* 能力描述符/表面/portal,多面共享同一工作台状态;own-server@1/http-router@1 单 mount、
|
|
6
|
+
* tool-provider@1 拆分装配表达不了这种单 apply 多面形态,故用 raw@1 声明 inject:['webServer']。
|
|
7
|
+
* 已在 meta/foundation.allowlist.json raw 段登记(until P5)。skill 包已硬依赖 os.skills,
|
|
8
|
+
* @capyclaw/core 本就是套件运行时前提,不引入新依赖。业务装配仍在 lib/host.mjs(可单测/standalone)。
|
|
9
|
+
*/
|
|
10
|
+
import { createPlugin, rawPlugin } from '@capyclaw/foundation/abi'
|
|
11
|
+
import { name as hostName, apply as applyHost } from './lib/host.mjs'
|
|
12
|
+
|
|
13
|
+
export const { name, inject, apply } = createPlugin({
|
|
14
|
+
name: hostName,
|
|
15
|
+
inject: ['webServer'],
|
|
16
|
+
implements: [rawPlugin(({ ctx, config }) => applyHost(ctx, config))],
|
|
17
|
+
}, import.meta.url)
|