@capyclaw/capyclaw-workbench 0.0.0-stage → 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.
Files changed (119) hide show
  1. package/README.md +56 -2
  2. package/contract/capabilities.json +178 -0
  3. package/lib/descriptors.mjs +33 -0
  4. package/lib/host.mjs +106 -0
  5. package/lib/session-port.mjs +178 -0
  6. package/lib/session-watch.mjs +82 -0
  7. package/lib/tools.mjs +166 -0
  8. package/lib/workbench.mjs +133 -0
  9. package/lib/workspace-attach.mjs +147 -0
  10. package/node_modules/@capyclaw/foundation/README.md +51 -0
  11. package/node_modules/@capyclaw/foundation/abi/assets.mjs +26 -0
  12. package/node_modules/@capyclaw/foundation/abi/bridge.mjs +88 -0
  13. package/node_modules/@capyclaw/foundation/abi/errors.mjs +63 -0
  14. package/node_modules/@capyclaw/foundation/abi/index.mjs +50 -0
  15. package/node_modules/@capyclaw/foundation/abi/kinds.mjs +271 -0
  16. package/node_modules/@capyclaw/foundation/abi/loader.mjs +352 -0
  17. package/node_modules/@capyclaw/foundation/abi/schema.mjs +98 -0
  18. package/node_modules/@capyclaw/foundation/abi/spec.mjs +258 -0
  19. package/node_modules/@capyclaw/foundation/abi/validate.mjs +24 -0
  20. package/node_modules/@capyclaw/foundation/abi/version.mjs +140 -0
  21. package/node_modules/@capyclaw/foundation/abi/visibility.mjs +90 -0
  22. package/node_modules/@capyclaw/foundation/cordis.patch.yml +5 -0
  23. package/node_modules/@capyclaw/foundation/kit/index.mjs +6 -0
  24. package/node_modules/@capyclaw/foundation/lib/adapters/client-panel.mjs +98 -0
  25. package/node_modules/@capyclaw/foundation/lib/adapters/host-event.mjs +35 -0
  26. package/node_modules/@capyclaw/foundation/lib/adapters/http-router.mjs +69 -0
  27. package/node_modules/@capyclaw/foundation/lib/adapters/lifecycle-hook.mjs +72 -0
  28. package/node_modules/@capyclaw/foundation/lib/adapters/own-server.mjs +113 -0
  29. package/node_modules/@capyclaw/foundation/lib/adapters/raw.mjs +69 -0
  30. package/node_modules/@capyclaw/foundation/lib/adapters/runtime-driver.mjs +120 -0
  31. package/node_modules/@capyclaw/foundation/lib/adapters/service-provider.mjs +77 -0
  32. package/node_modules/@capyclaw/foundation/lib/adapters/skill-provider.mjs +56 -0
  33. package/node_modules/@capyclaw/foundation/lib/adapters/tool-provider.mjs +50 -0
  34. package/node_modules/@capyclaw/foundation/lib/adapters/ui-panel.mjs +64 -0
  35. package/node_modules/@capyclaw/foundation/lib/ctx-service.mjs +42 -0
  36. package/node_modules/@capyclaw/foundation/lib/host/caps.mjs +38 -0
  37. package/node_modules/@capyclaw/foundation/lib/host/contract.mjs +88 -0
  38. package/node_modules/@capyclaw/foundation/lib/host/dsh.mjs +54 -0
  39. package/node_modules/@capyclaw/foundation/lib/host/memory.mjs +187 -0
  40. package/node_modules/@capyclaw/foundation/lib/host.mjs +23 -0
  41. package/node_modules/@capyclaw/foundation/lib/kernel.mjs +195 -0
  42. package/node_modules/@capyclaw/foundation/lib/skill/scan.mjs +115 -0
  43. package/node_modules/@capyclaw/foundation/lib/subsystems/events.mjs +60 -0
  44. package/node_modules/@capyclaw/foundation/lib/subsystems/log.mjs +51 -0
  45. package/node_modules/@capyclaw/foundation/lib/subsystems/net.mjs +198 -0
  46. package/node_modules/@capyclaw/foundation/package.json +53 -0
  47. package/node_modules/@capyclaw/foundation/runtime/index.mjs +20 -0
  48. package/node_modules/@capyclaw/plugin-kit/INTEGRATION.md +100 -0
  49. package/node_modules/@capyclaw/plugin-kit/README.md +70 -0
  50. package/node_modules/@capyclaw/plugin-kit/bin/sync.mjs +103 -0
  51. package/node_modules/@capyclaw/plugin-kit/package.json +71 -0
  52. package/node_modules/@capyclaw/plugin-kit/src/cli.mjs +56 -0
  53. package/node_modules/@capyclaw/plugin-kit/src/config.mjs +142 -0
  54. package/node_modules/@capyclaw/plugin-kit/src/editor/heartbeat-worker.js +132 -0
  55. package/node_modules/@capyclaw/plugin-kit/src/editor/heartbeat.mjs +26 -0
  56. package/node_modules/@capyclaw/plugin-kit/src/editor/lease.mjs +60 -0
  57. package/node_modules/@capyclaw/plugin-kit/src/editor/shadow.mjs +219 -0
  58. package/node_modules/@capyclaw/plugin-kit/src/editor/transport.js +13 -0
  59. package/node_modules/@capyclaw/plugin-kit/src/index.mjs +13 -0
  60. package/node_modules/@capyclaw/plugin-kit/src/pack.mjs +230 -0
  61. package/node_modules/@capyclaw/plugin-kit/src/runtime/context.mjs +128 -0
  62. package/node_modules/@capyclaw/plugin-kit/src/self-contained.mjs +307 -0
  63. package/node_modules/@capyclaw/plugin-kit/src/service/api.mjs +45 -0
  64. package/node_modules/@capyclaw/plugin-kit/src/service/http.mjs +113 -0
  65. package/node_modules/@capyclaw/plugin-kit/src/service/mcp-http.mjs +150 -0
  66. package/node_modules/@capyclaw/plugin-kit/src/service/server.mjs +131 -0
  67. package/node_modules/@capyclaw/plugin-kit/src/service/static.mjs +79 -0
  68. package/node_modules/@capyclaw/plugin-kit/src/skills/provider.mjs +82 -0
  69. package/node_modules/@capyclaw/plugin-kit/src/store/blob-store.mjs +109 -0
  70. package/node_modules/@capyclaw/plugin-kit/src/store/content-store.mjs +17 -0
  71. package/node_modules/@capyclaw/plugin-kit/src/store/journal-store.mjs +89 -0
  72. package/node_modules/@capyclaw/plugin-kit/src/tools/result.mjs +57 -0
  73. package/node_modules/@capyclaw/plugin-kit/src/tools/validate.mjs +150 -0
  74. package/node_modules/@capyclaw/plugin-kit/src/tuning.mjs +82 -0
  75. package/node_modules/@capyclaw/plugin-kit/src/util/fs-lock.mjs +132 -0
  76. package/node_modules/@capyclaw/plugin-kit/src/util/http.mjs +130 -0
  77. package/node_modules/@capyclaw/plugin-kit/src/workspace/manager.mjs +664 -0
  78. package/node_modules/@capyclaw/plugin-kit/src/zip.mjs +181 -0
  79. package/node_modules/@capyclaw/service-host/README.md +86 -0
  80. package/node_modules/@capyclaw/service-host/cordis.patch.yml +4 -0
  81. package/node_modules/@capyclaw/service-host/dsh-suite.json +29 -0
  82. package/node_modules/@capyclaw/service-host/lib/host.mjs +22 -0
  83. package/node_modules/@capyclaw/service-host/package.json +60 -0
  84. package/node_modules/@capyclaw/service-host/src/index.mjs +570 -0
  85. package/node_modules/@capyclaw/service-host/src/tuning.mjs +63 -0
  86. package/node_modules/@capyclaw/suite-kit/README.md +289 -0
  87. package/node_modules/@capyclaw/suite-kit/bin/dsh-domain.mjs +98 -0
  88. package/node_modules/@capyclaw/suite-kit/bin/dsh-suite.mjs +174 -0
  89. package/node_modules/@capyclaw/suite-kit/client/embed.js +35 -0
  90. package/node_modules/@capyclaw/suite-kit/domain.schema.json +129 -0
  91. package/node_modules/@capyclaw/suite-kit/package.json +58 -0
  92. package/node_modules/@capyclaw/suite-kit/src/activation-registry.mjs +184 -0
  93. package/node_modules/@capyclaw/suite-kit/src/artifacts.mjs +131 -0
  94. package/node_modules/@capyclaw/suite-kit/src/capability.mjs +181 -0
  95. package/node_modules/@capyclaw/suite-kit/src/compose.mjs +201 -0
  96. package/node_modules/@capyclaw/suite-kit/src/domain.mjs +138 -0
  97. package/node_modules/@capyclaw/suite-kit/src/embed.mjs +81 -0
  98. package/node_modules/@capyclaw/suite-kit/src/host.mjs +230 -0
  99. package/node_modules/@capyclaw/suite-kit/src/index.mjs +24 -0
  100. package/node_modules/@capyclaw/suite-kit/src/manifest.mjs +263 -0
  101. package/node_modules/@capyclaw/suite-kit/src/orchestration.mjs +100 -0
  102. package/node_modules/@capyclaw/suite-kit/src/profile.mjs +584 -0
  103. package/node_modules/@capyclaw/suite-kit/src/surface.mjs +28 -0
  104. package/node_modules/@capyclaw/suite-kit/suite.schema.json +128 -0
  105. package/node_modules/@capyclaw/tool-host/README.md +56 -0
  106. package/node_modules/@capyclaw/tool-host/cordis.patch.yml +4 -0
  107. package/node_modules/@capyclaw/tool-host/dsh-suite.json +30 -0
  108. package/node_modules/@capyclaw/tool-host/lib/host.mjs +46 -0
  109. package/node_modules/@capyclaw/tool-host/package.json +59 -0
  110. package/node_modules/@capyclaw/tool-host/src/admit-images.mjs +80 -0
  111. package/node_modules/@capyclaw/tool-host/src/caller-session.mjs +25 -0
  112. package/node_modules/@capyclaw/tool-host/src/descriptors.mjs +166 -0
  113. package/node_modules/@capyclaw/tool-host/src/index.mjs +461 -0
  114. package/node_modules/@capyclaw/tool-host/src/mode-gate.mjs +272 -0
  115. package/package.json +68 -4
  116. package/plugin.mjs +17 -0
  117. package/ui/app.js +114 -0
  118. package/ui/index.html +64 -0
  119. package/ui/style.css +54 -0
@@ -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,70 @@
1
1
  {
2
2
  "name": "@capyclaw/capyclaw-workbench",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.1",
4
+ "description": "CapyClaw 子会话工作台运行时:7 个 capyclaw_* 工具(创建/驱动/查询/关闭 DSH 子会话 + 读写设置)+ 俄罗斯套娃控制台(同源嵌入 DSH GUI)。挂在 dsh 的 /capyclaw/capyclaw。",
5
+ "type": "module",
6
+ "main": "./plugin.mjs",
7
+ "exports": {
8
+ ".": "./plugin.mjs",
9
+ "./package.json": "./package.json"
10
+ },
11
+ "engines": {
12
+ "node": ">=18",
13
+ "dsh": "^0.2.0-rc.2"
14
+ },
15
+ "publishConfig": {
16
+ "access": "public"
17
+ },
18
+ "dsh": {
19
+ "member": true,
20
+ "category": "service",
21
+ "platform": "both",
22
+ "capabilities": {
23
+ "manifest": "contract/capabilities.json"
24
+ },
25
+ "requires": {
26
+ "capabilities": {
27
+ "tools.registry": "^1.0.0",
28
+ "tools.descriptors": "^1.1.0"
29
+ }
30
+ }
31
+ },
32
+ "meta": {
33
+ "title": "CapyClaw 子会话工作台运行时",
34
+ "description": "AI 在同一宿主进程内发起/驱动 DSH 子会话(俄罗斯套娃),并帮用户完成 DSH 设置;能力经描述符注册供上层编排器发现。"
35
+ },
36
+ "files": [
37
+ "plugin.mjs",
38
+ "lib/",
39
+ "ui/",
40
+ "contract/",
41
+ "README.md"
42
+ ],
43
+ "license": "MIT",
44
+ "scripts": {
45
+ "test": "node --test --test-isolation=none test/*.test.mjs"
46
+ },
47
+ "dependencies": {
48
+ "@capyclaw/foundation": "^0.1.9",
49
+ "@capyclaw/plugin-kit": "^0.2.6",
50
+ "@capyclaw/service-host": "^0.1.5",
51
+ "@capyclaw/suite-kit": "^0.2.0",
52
+ "@capyclaw/tool-host": "^0.3.0"
53
+ },
54
+ "author": {
55
+ "name": "CapyClaw",
56
+ "url": "https://github.com/capyclaw"
57
+ },
58
+ "repository": {
59
+ "type": "git",
60
+ "url": "git+https://github.com/Maple496/capyclaw.git",
61
+ "directory": "A/projects/capyclaw/workbench"
62
+ },
63
+ "bundledDependencies": [
64
+ "@capyclaw/foundation",
65
+ "@capyclaw/plugin-kit",
66
+ "@capyclaw/service-host",
67
+ "@capyclaw/suite-kit",
68
+ "@capyclaw/tool-host"
69
+ ]
70
+ }
package/plugin.mjs ADDED
@@ -0,0 +1,17 @@
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)
package/ui/app.js ADDED
@@ -0,0 +1,114 @@
1
+ /* CapyClaw 子会话工作台 UI。控制面与呈现面分离:工具/HTTP 同一接口,模式只是呈现层状态。 */
2
+ (function () {
3
+ const $ = (s, r) => (r || document).querySelector(s)
4
+ const $$ = (s, r) => [...(r || document).querySelectorAll(s)]
5
+ const esc = (s) => String(s ?? '').replace(/[&<>"]/g, (c) => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;' }[c]))
6
+ let CFG = { guiUrl: '/', mountPath: '/capyclaw/capyclaw', available: {} }
7
+
8
+ if (window.CapyclawEmbed) window.CapyclawEmbed.setSuite('capyclaw')
9
+ const emit = (kind, payload) => { try { window.CapyclawEmbed && window.CapyclawEmbed.sendEvent({ kind, suite: 'capyclaw', at: Date.now(), ...payload }) } catch {} }
10
+
11
+ function toast(msg, isErr) {
12
+ const t = $('#toast'); t.textContent = msg; t.className = 'toast show' + (isErr ? ' err' : '')
13
+ clearTimeout(t._h); t._h = setTimeout(() => { t.className = 'toast' }, 2600)
14
+ }
15
+ async function api(method, url, body) {
16
+ const opt = { method, headers: {} }
17
+ if (body !== undefined) { opt.headers['content-type'] = 'application/json'; opt.body = JSON.stringify(body) }
18
+ const res = await fetch(url, opt)
19
+ let data = {}; try { data = await res.json() } catch {}
20
+ if (!res.ok || data.ok === false) throw new Error(data.error || ('HTTP ' + res.status))
21
+ return data
22
+ }
23
+
24
+ // ---- 配置 / 可用性 ----
25
+ async function loadConfig() {
26
+ try {
27
+ CFG = await api('GET', './api/config')
28
+ $('#gui').src = CFG.guiUrl || '/'
29
+ $('#gui-open').href = CFG.guiUrl || '/'
30
+ const a = CFG.available || {}
31
+ const on = a.sessionController
32
+ const pill = $('#avail'); pill.textContent = on ? '会话服务可用' : '会话服务不可用'
33
+ pill.className = 'pill ' + (on ? 'ok' : 'bad')
34
+ } catch (e) { toast('配置加载失败:' + e.message, true) }
35
+ }
36
+
37
+ // ---- 子会话 ----
38
+ async function loadSessions() {
39
+ const box = $('#session-list')
40
+ try {
41
+ const { items } = await api('GET', './api/sessions')
42
+ if (!items.length) { box.innerHTML = '<div class="empty">还没有子会话。创建一个,让 AI 在同一宿主里替你干活。</div>'; return }
43
+ box.innerHTML = items.map((s) => {
44
+ const id = esc(s.sessionId || s.id)
45
+ return `<div class="session ${s.closed ? 'closed' : ''}" data-id="${id}">
46
+ <div class="s-title"><span>${esc(s.title || '子会话')}</span>${s.closed ? '<span class="pill">已关闭</span>' : ''}</div>
47
+ <div class="s-id">${id}</div>
48
+ <div class="s-prompt"><input placeholder="下发指令…" data-act="prompt-input"><button class="btn small" data-act="send">发送</button></div>
49
+ <div class="s-actions">
50
+ <button class="btn small" data-act="transcript">转录</button>
51
+ <button class="btn small" data-act="close">关闭</button>
52
+ </div>
53
+ <details hidden><pre class="code" data-out></pre></details>
54
+ </div>`
55
+ }).join('')
56
+ } catch (e) { box.innerHTML = '<div class="empty">加载失败:' + esc(e.message) + '</div>' }
57
+ }
58
+
59
+ $('#create-form').addEventListener('submit', async (ev) => {
60
+ ev.preventDefault()
61
+ const f = ev.target
62
+ const body = { title: f.title.value.trim() || undefined, cwd: f.cwd.value.trim() || undefined, prompt: f.prompt.value.trim() || undefined }
63
+ try {
64
+ const r = await api('POST', './api/sessions', body)
65
+ toast('已创建 ' + r.sessionId.slice(0, 8)); emit('result', { action: 'session.open', sessionId: r.sessionId })
66
+ f.reset(); await loadSessions()
67
+ } catch (e) { toast('创建失败:' + e.message, true); emit('error', { action: 'session.open', message: e.message }) }
68
+ })
69
+
70
+ $('#session-list').addEventListener('click', async (ev) => {
71
+ const btn = ev.target.closest('[data-act]'); if (!btn) return
72
+ const card = btn.closest('.session'); const id = card.dataset.id; const act = btn.dataset.act
73
+ try {
74
+ if (act === 'send') {
75
+ const input = $('[data-act="prompt-input"]', card); const text = input.value.trim()
76
+ if (!text) return toast('先写指令', true)
77
+ const r = await api('POST', './api/sessions/prompt', { sessionId: id, text })
78
+ input.value = ''; toast(r.accepted ? '已下发' : '已排队'); emit('progress', { action: 'session.prompt', sessionId: id })
79
+ } else if (act === 'transcript') {
80
+ const det = $('details', card); const out = $('[data-out]', card)
81
+ const r = await api('GET', './api/sessions/transcript?sessionId=' + encodeURIComponent(id))
82
+ out.textContent = JSON.stringify(r.transcript, null, 2); det.hidden = false
83
+ } else if (act === 'close') {
84
+ await api('POST', './api/sessions/close', { sessionId: id })
85
+ toast('已关闭'); emit('result', { action: 'session.close', sessionId: id }); await loadSessions()
86
+ }
87
+ } catch (e) { toast(e.message, true); emit('error', { action: act, sessionId: id, message: e.message }) }
88
+ })
89
+
90
+ // ---- 设置 ----
91
+ $('#settings-load').addEventListener('click', async () => {
92
+ try { const r = await api('GET', './api/settings'); $('#settings-view').textContent = JSON.stringify(r.settings, null, 2) }
93
+ catch (e) { toast('读取失败:' + e.message, true) }
94
+ })
95
+ $('#settings-form').addEventListener('submit', async (ev) => {
96
+ ev.preventDefault(); const f = ev.target
97
+ let patch; try { patch = JSON.parse(f.patch.value || '{}') } catch { return toast('patch 不是合法 JSON', true) }
98
+ if (!confirm('设置更新为高风险操作,确认提交?')) return
99
+ try {
100
+ const r = await api('POST', './api/settings', { ns: f.ns.value.trim(), patch })
101
+ $('#settings-view').textContent = JSON.stringify(r.value, null, 2)
102
+ toast('设置已更新'); emit('result', { action: 'settings.set', ns: f.ns.value.trim() })
103
+ } catch (e) { toast('更新失败:' + e.message, true); emit('error', { action: 'settings.set', message: e.message }) }
104
+ })
105
+
106
+ // ---- tabs / refresh ----
107
+ $$('.tab').forEach((t) => t.addEventListener('click', () => {
108
+ $$('.tab').forEach((x) => x.classList.toggle('active', x === t))
109
+ $$('.tabpane').forEach((p) => p.classList.toggle('active', p.id === 'tab-' + t.dataset.tab))
110
+ }))
111
+ $('#refresh').addEventListener('click', loadSessions)
112
+
113
+ loadConfig().then(loadSessions)
114
+ })()
package/ui/index.html ADDED
@@ -0,0 +1,64 @@
1
+ <!doctype html>
2
+ <html lang="zh">
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1">
6
+ <title>CapyClaw 子会话工作台</title>
7
+ <link rel="stylesheet" href="./style.css">
8
+ </head>
9
+ <body>
10
+ <header class="topbar standalone-chrome">
11
+ <div class="brand">🪆 CapyClaw <span class="sub">子会话工作台 · 同一宿主进程</span></div>
12
+ <div class="top-actions">
13
+ <span id="avail" class="pill">检测中…</span>
14
+ <button id="refresh" class="btn ghost">刷新</button>
15
+ </div>
16
+ </header>
17
+
18
+ <div class="layout">
19
+ <aside class="panel">
20
+ <nav class="tabs">
21
+ <button class="tab active" data-tab="sessions">子会话</button>
22
+ <button class="tab" data-tab="settings">DSH 设置</button>
23
+ </nav>
24
+
25
+ <section id="tab-sessions" class="tabpane active">
26
+ <form id="create-form" class="card">
27
+ <h3>发起子会话</h3>
28
+ <label>标题<input name="title" placeholder="例如:调研竞品"></label>
29
+ <label>工作目录 cwd<input name="cwd" placeholder="留空=宿主默认"></label>
30
+ <label>首条指令<textarea name="prompt" rows="2" placeholder="可选:创建后立即下发"></textarea></label>
31
+ <button class="btn primary" type="submit">创建</button>
32
+ </form>
33
+ <div id="session-list" class="list"></div>
34
+ </section>
35
+
36
+ <section id="tab-settings" class="tabpane">
37
+ <div class="card">
38
+ <h3>读取设置</h3>
39
+ <button id="settings-load" class="btn">加载 describe</button>
40
+ <pre id="settings-view" class="code"></pre>
41
+ </div>
42
+ <form id="settings-form" class="card">
43
+ <h3>更新设置(高风险,需确认)</h3>
44
+ <label>命名空间 ns<input name="ns" placeholder="profile 条目 id"></label>
45
+ <label>patch(JSON)<textarea name="patch" rows="3" placeholder='{"key":"value"}'></textarea></label>
46
+ <button class="btn danger" type="submit">提交更新</button>
47
+ </form>
48
+ </section>
49
+ </aside>
50
+
51
+ <main class="stage">
52
+ <div class="stage-head standalone-chrome">
53
+ <span>宿主 GUI(俄罗斯套娃 · 同源嵌入)</span>
54
+ <a id="gui-open" class="btn ghost" target="_blank" rel="noopener">新窗打开</a>
55
+ </div>
56
+ <iframe id="gui" class="gui" title="DSH GUI" src="about:blank"></iframe>
57
+ </main>
58
+ </div>
59
+
60
+ <div id="toast" class="toast"></div>
61
+ <script src="./embed.js"></script>
62
+ <script src="./app.js"></script>
63
+ </body>
64
+ </html>
package/ui/style.css ADDED
@@ -0,0 +1,54 @@
1
+ :root{
2
+ --bg:#0f1117; --panel:#161a23; --card:#1d2230; --line:#2a3040;
3
+ --ink:#e6e9f0; --mut:#8b93a7; --acc:#5b9bd5; --ok:#3fb96f; --warn:#e0a34e; --bad:#e05b6f;
4
+ --r:10px;
5
+ }
6
+ *{box-sizing:border-box}
7
+ html,body{height:100%;margin:0}
8
+ body{background:var(--bg);color:var(--ink);font:14px/1.5 system-ui,"Segoe UI",Roboto,"Noto Sans SC",sans-serif;display:flex;flex-direction:column}
9
+ .topbar{display:flex;align-items:center;justify-content:space-between;padding:10px 16px;border-bottom:1px solid var(--line);background:var(--panel)}
10
+ .brand{font-weight:700;font-size:16px}
11
+ .brand .sub{font-weight:400;color:var(--mut);font-size:12px;margin-left:8px}
12
+ .top-actions{display:flex;gap:8px;align-items:center}
13
+ .pill{font-size:12px;color:var(--mut);border:1px solid var(--line);border-radius:999px;padding:2px 10px}
14
+ .pill.ok{color:var(--ok);border-color:var(--ok)}
15
+ .pill.bad{color:var(--bad);border-color:var(--bad)}
16
+ .layout{flex:1;display:flex;min-height:0}
17
+ .panel{width:340px;min-width:280px;border-right:1px solid var(--line);background:var(--panel);display:flex;flex-direction:column;overflow:hidden}
18
+ .tabs{display:flex;border-bottom:1px solid var(--line)}
19
+ .tab{flex:1;background:none;border:none;color:var(--mut);padding:10px;cursor:pointer;font-size:14px;border-bottom:2px solid transparent}
20
+ .tab.active{color:var(--ink);border-bottom-color:var(--acc)}
21
+ .tabpane{display:none;overflow-y:auto;padding:12px;flex:1}
22
+ .tabpane.active{display:block}
23
+ .card{background:var(--card);border:1px solid var(--line);border-radius:var(--r);padding:12px;margin-bottom:12px}
24
+ .card h3{margin:0 0 10px;font-size:13px;color:var(--mut);font-weight:600}
25
+ label{display:block;font-size:12px;color:var(--mut);margin-bottom:8px}
26
+ input,textarea{width:100%;margin-top:4px;background:var(--bg);border:1px solid var(--line);border-radius:6px;color:var(--ink);padding:7px 9px;font:inherit;resize:vertical}
27
+ input:focus,textarea:focus{outline:none;border-color:var(--acc)}
28
+ .btn{background:var(--card);border:1px solid var(--line);color:var(--ink);border-radius:6px;padding:7px 12px;cursor:pointer;font:inherit}
29
+ .btn:hover{border-color:var(--acc)}
30
+ .btn.primary{background:var(--acc);border-color:var(--acc);color:#fff;width:100%}
31
+ .btn.danger{background:var(--bad);border-color:var(--bad);color:#fff;width:100%}
32
+ .btn.ghost{background:transparent}
33
+ .btn.small{padding:3px 8px;font-size:12px}
34
+ .list{display:flex;flex-direction:column;gap:8px}
35
+ .session{background:var(--card);border:1px solid var(--line);border-radius:var(--r);padding:10px}
36
+ .session.closed{opacity:.55}
37
+ .session .s-title{font-weight:600;display:flex;justify-content:space-between;align-items:center}
38
+ .session .s-id{font-size:11px;color:var(--mut);font-family:ui-monospace,monospace;word-break:break-all}
39
+ .session .s-actions{display:flex;gap:6px;margin-top:8px;flex-wrap:wrap}
40
+ .session .s-prompt{display:flex;gap:6px;margin-top:8px}
41
+ .session .s-prompt input{flex:1;margin:0}
42
+ .session details{margin-top:8px}
43
+ .session pre{max-height:220px;overflow:auto}
44
+ .code{background:var(--bg);border:1px solid var(--line);border-radius:6px;padding:8px;font:12px/1.5 ui-monospace,monospace;white-space:pre-wrap;word-break:break-word;max-height:260px;overflow:auto;margin:8px 0 0}
45
+ .stage{flex:1;display:flex;flex-direction:column;min-width:0}
46
+ .stage-head{display:flex;justify-content:space-between;align-items:center;padding:8px 14px;border-bottom:1px solid var(--line);color:var(--mut);font-size:12px;background:var(--panel)}
47
+ .gui{flex:1;border:none;width:100%;background:#fff}
48
+ .toast{position:fixed;left:50%;bottom:22px;transform:translateX(-50%);background:var(--card);border:1px solid var(--line);border-radius:8px;padding:9px 16px;opacity:0;pointer-events:none;transition:opacity .2s;max-width:80vw}
49
+ .toast.show{opacity:1}
50
+ .toast.err{border-color:var(--bad);color:var(--bad)}
51
+ .empty{color:var(--mut);text-align:center;padding:20px;font-size:13px}
52
+ /* 嵌入模式:隐藏 standalone chrome,交给父窗编排(标准 §C.7) */
53
+ body.capyclaw-embedded .standalone-chrome{display:none}
54
+ body.capyclaw-embedded .panel{width:300px}