@capyclaw/wall-suite 0.0.0-stage → 0.1.0

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 (148) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +20 -2
  3. package/composition.lock.json +48 -0
  4. package/contract/wall-contract.json +8 -0
  5. package/cordis.patch.yml +6 -0
  6. package/dsh-suite.json +118 -0
  7. package/lib/index.mjs +6 -0
  8. package/node_modules/@capyclaw/asset-service/README.md +48 -0
  9. package/node_modules/@capyclaw/asset-service/cordis.patch.yml +4 -0
  10. package/node_modules/@capyclaw/asset-service/dsh-suite.json +29 -0
  11. package/node_modules/@capyclaw/asset-service/lib/host.mjs +14 -0
  12. package/node_modules/@capyclaw/asset-service/package.json +59 -0
  13. package/node_modules/@capyclaw/asset-service/src/index.mjs +223 -0
  14. package/node_modules/@capyclaw/foundation/README.md +51 -0
  15. package/node_modules/@capyclaw/foundation/abi/bridge.mjs +86 -0
  16. package/node_modules/@capyclaw/foundation/abi/errors.mjs +63 -0
  17. package/node_modules/@capyclaw/foundation/abi/index.mjs +35 -0
  18. package/node_modules/@capyclaw/foundation/abi/kinds.mjs +271 -0
  19. package/node_modules/@capyclaw/foundation/abi/loader.mjs +345 -0
  20. package/node_modules/@capyclaw/foundation/abi/schema.mjs +98 -0
  21. package/node_modules/@capyclaw/foundation/abi/spec.mjs +255 -0
  22. package/node_modules/@capyclaw/foundation/abi/validate.mjs +24 -0
  23. package/node_modules/@capyclaw/foundation/abi/version.mjs +82 -0
  24. package/node_modules/@capyclaw/foundation/cordis.patch.yml +5 -0
  25. package/node_modules/@capyclaw/foundation/kit/index.mjs +6 -0
  26. package/node_modules/@capyclaw/foundation/lib/adapters/client-panel.mjs +98 -0
  27. package/node_modules/@capyclaw/foundation/lib/adapters/host-event.mjs +35 -0
  28. package/node_modules/@capyclaw/foundation/lib/adapters/http-router.mjs +69 -0
  29. package/node_modules/@capyclaw/foundation/lib/adapters/lifecycle-hook.mjs +55 -0
  30. package/node_modules/@capyclaw/foundation/lib/adapters/own-server.mjs +113 -0
  31. package/node_modules/@capyclaw/foundation/lib/adapters/raw.mjs +68 -0
  32. package/node_modules/@capyclaw/foundation/lib/adapters/runtime-driver.mjs +113 -0
  33. package/node_modules/@capyclaw/foundation/lib/adapters/service-provider.mjs +77 -0
  34. package/node_modules/@capyclaw/foundation/lib/adapters/skill-provider.mjs +56 -0
  35. package/node_modules/@capyclaw/foundation/lib/adapters/tool-provider.mjs +50 -0
  36. package/node_modules/@capyclaw/foundation/lib/adapters/ui-panel.mjs +64 -0
  37. package/node_modules/@capyclaw/foundation/lib/ctx-service.mjs +42 -0
  38. package/node_modules/@capyclaw/foundation/lib/host/caps.mjs +34 -0
  39. package/node_modules/@capyclaw/foundation/lib/host/contract.mjs +85 -0
  40. package/node_modules/@capyclaw/foundation/lib/host/dsh.mjs +50 -0
  41. package/node_modules/@capyclaw/foundation/lib/host/memory.mjs +160 -0
  42. package/node_modules/@capyclaw/foundation/lib/host.mjs +23 -0
  43. package/node_modules/@capyclaw/foundation/lib/kernel.mjs +185 -0
  44. package/node_modules/@capyclaw/foundation/lib/skill/scan.mjs +87 -0
  45. package/node_modules/@capyclaw/foundation/lib/subsystems/events.mjs +60 -0
  46. package/node_modules/@capyclaw/foundation/lib/subsystems/log.mjs +51 -0
  47. package/node_modules/@capyclaw/foundation/lib/subsystems/net.mjs +188 -0
  48. package/node_modules/@capyclaw/foundation/package.json +53 -0
  49. package/node_modules/@capyclaw/foundation/runtime/index.mjs +20 -0
  50. package/node_modules/@capyclaw/plugin-kit/INTEGRATION.md +100 -0
  51. package/node_modules/@capyclaw/plugin-kit/README.md +70 -0
  52. package/node_modules/@capyclaw/plugin-kit/bin/sync.mjs +103 -0
  53. package/node_modules/@capyclaw/plugin-kit/package.json +69 -0
  54. package/node_modules/@capyclaw/plugin-kit/src/cli.mjs +56 -0
  55. package/node_modules/@capyclaw/plugin-kit/src/config.mjs +142 -0
  56. package/node_modules/@capyclaw/plugin-kit/src/editor/heartbeat-worker.js +129 -0
  57. package/node_modules/@capyclaw/plugin-kit/src/editor/lease.mjs +139 -0
  58. package/node_modules/@capyclaw/plugin-kit/src/editor/shadow.mjs +219 -0
  59. package/node_modules/@capyclaw/plugin-kit/src/editor/transport.js +13 -0
  60. package/node_modules/@capyclaw/plugin-kit/src/index.mjs +13 -0
  61. package/node_modules/@capyclaw/plugin-kit/src/pack.mjs +230 -0
  62. package/node_modules/@capyclaw/plugin-kit/src/runtime/context.mjs +128 -0
  63. package/node_modules/@capyclaw/plugin-kit/src/self-contained.mjs +307 -0
  64. package/node_modules/@capyclaw/plugin-kit/src/service/api.mjs +45 -0
  65. package/node_modules/@capyclaw/plugin-kit/src/service/http.mjs +113 -0
  66. package/node_modules/@capyclaw/plugin-kit/src/service/mcp-http.mjs +143 -0
  67. package/node_modules/@capyclaw/plugin-kit/src/service/server.mjs +131 -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 +41 -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 +656 -0
  78. package/node_modules/@capyclaw/plugin-kit/src/zip.mjs +181 -0
  79. package/node_modules/@capyclaw/portal/README.md +70 -0
  80. package/node_modules/@capyclaw/portal/lib/index.mjs +387 -0
  81. package/node_modules/@capyclaw/portal/lib/ledger.mjs +194 -0
  82. package/node_modules/@capyclaw/portal/lib/portal-agent.ps1 +811 -0
  83. package/node_modules/@capyclaw/portal/lib/portal-darwin.mjs +183 -0
  84. package/node_modules/@capyclaw/portal/package.json +40 -0
  85. package/node_modules/@capyclaw/service-broker/README.md +104 -0
  86. package/node_modules/@capyclaw/service-broker/cordis.patch.yml +8 -0
  87. package/node_modules/@capyclaw/service-broker/dsh-suite.json +21 -0
  88. package/node_modules/@capyclaw/service-broker/lib/control.mjs +193 -0
  89. package/node_modules/@capyclaw/service-broker/lib/discovery.mjs +109 -0
  90. package/node_modules/@capyclaw/service-broker/lib/engine.mjs +1236 -0
  91. package/node_modules/@capyclaw/service-broker/lib/host.mjs +182 -0
  92. package/node_modules/@capyclaw/service-broker/lib/manifest.mjs +249 -0
  93. package/node_modules/@capyclaw/service-broker/lib/recovery.mjs +325 -0
  94. package/node_modules/@capyclaw/service-broker/lib/skills-client.mjs +28 -0
  95. package/node_modules/@capyclaw/service-broker/lib/spec.mjs +78 -0
  96. package/node_modules/@capyclaw/service-broker/package.json +65 -0
  97. package/node_modules/@capyclaw/service-broker/server.mjs +81 -0
  98. package/node_modules/@capyclaw/service-broker/service.schema.json +62 -0
  99. package/node_modules/@capyclaw/service-broker/services.json +3 -0
  100. package/node_modules/@capyclaw/service-host/README.md +86 -0
  101. package/node_modules/@capyclaw/service-host/cordis.patch.yml +4 -0
  102. package/node_modules/@capyclaw/service-host/dsh-suite.json +29 -0
  103. package/node_modules/@capyclaw/service-host/lib/host.mjs +22 -0
  104. package/node_modules/@capyclaw/service-host/package.json +60 -0
  105. package/node_modules/@capyclaw/service-host/src/index.mjs +571 -0
  106. package/node_modules/@capyclaw/service-host/src/tuning.mjs +63 -0
  107. package/node_modules/@capyclaw/tool-host/README.md +56 -0
  108. package/node_modules/@capyclaw/tool-host/cordis.patch.yml +4 -0
  109. package/node_modules/@capyclaw/tool-host/dsh-suite.json +30 -0
  110. package/node_modules/@capyclaw/tool-host/lib/host.mjs +16 -0
  111. package/node_modules/@capyclaw/tool-host/package.json +59 -0
  112. package/node_modules/@capyclaw/tool-host/src/descriptors.mjs +142 -0
  113. package/node_modules/@capyclaw/tool-host/src/index.mjs +390 -0
  114. package/node_modules/@capyclaw/wall-workbench/README.md +58 -0
  115. package/node_modules/@capyclaw/wall-workbench/contract/wall-contract.json +8 -0
  116. package/node_modules/@capyclaw/wall-workbench/lib/host.mjs +1 -0
  117. package/node_modules/@capyclaw/wall-workbench/package.json +63 -0
  118. package/node_modules/@capyclaw/wall-workbench/plugin.mjs +1 -0
  119. package/node_modules/@capyclaw/wall-workbench/server/constants.mjs +12 -0
  120. package/node_modules/@capyclaw/wall-workbench/server/contract.mjs +12 -0
  121. package/node_modules/@capyclaw/wall-workbench/server/embedded-host.mjs +160 -0
  122. package/node_modules/@capyclaw/wall-workbench/server/infra/http.mjs +2 -0
  123. package/node_modules/@capyclaw/wall-workbench/server/infra/tuning.mjs +2 -0
  124. package/node_modules/@capyclaw/wall-workbench/server/relay.mjs +68 -0
  125. package/node_modules/@capyclaw/wall-workbench/server/routes/claim.mjs +46 -0
  126. package/node_modules/@capyclaw/wall-workbench/server/routes/workbench.mjs +29 -0
  127. package/node_modules/@capyclaw/wall-workbench/server/shared/SHARED_MANIFEST.json +70 -0
  128. package/node_modules/@capyclaw/wall-workbench/server/shared/asset-service.mjs +2 -0
  129. package/node_modules/@capyclaw/wall-workbench/server/shared/editor-host.mjs +2 -0
  130. package/node_modules/@capyclaw/wall-workbench/server/shared/kit/editor/lease.mjs +2 -0
  131. package/node_modules/@capyclaw/wall-workbench/server/shared/kit/service/api.mjs +2 -0
  132. package/node_modules/@capyclaw/wall-workbench/server/shared/kit/service/http.mjs +2 -0
  133. package/node_modules/@capyclaw/wall-workbench/server/shared/kit/service/mcp-http.mjs +2 -0
  134. package/node_modules/@capyclaw/wall-workbench/server/shared/kit/tools/validate.mjs +2 -0
  135. package/node_modules/@capyclaw/wall-workbench/server/shared/kit/tuning.mjs +2 -0
  136. package/node_modules/@capyclaw/wall-workbench/server/shared/kit/util/fs-lock.mjs +2 -0
  137. package/node_modules/@capyclaw/wall-workbench/server/shared/service-host.mjs +2 -0
  138. package/node_modules/@capyclaw/wall-workbench/server/shared/tool-host.mjs +2 -0
  139. package/node_modules/@capyclaw/wall-workbench/server/shared/tuning.mjs +2 -0
  140. package/node_modules/@capyclaw/wall-workbench/server/tools/capabilities/index.mjs +2 -0
  141. package/node_modules/@capyclaw/wall-workbench/server/tools/index.mjs +12 -0
  142. package/node_modules/@capyclaw/wall-workbench/server/tools.mjs +2 -0
  143. package/node_modules/@capyclaw/wall-workbench/server/workbench-framework.mjs +305 -0
  144. package/node_modules/@capyclaw/wall-workbench/ui/app.js +142 -0
  145. package/node_modules/@capyclaw/wall-workbench/ui/index.html +20 -0
  146. package/node_modules/@capyclaw/wall-workbench/ui/style.css +74 -0
  147. package/package.json +72 -4
  148. package/services.json +3 -0
@@ -0,0 +1,305 @@
1
+ /**
2
+ * B 段提供的运行时框架:工作台 relay 的骨架。
3
+ *
4
+ * 这个文件由 build/bridge-build.mjs 从本目录拷进每个包的 server/workbench-framework.mjs。
5
+ * A 段(projects/<id>/src)只写业务钩子,不再自己拼 HTTP/MCP/锁/发现/独立入口。
6
+ *
7
+ * 框架职责(A 不再考虑):
8
+ * - serviceHost / 内置 createServiceApp 的选择与降级
9
+ * - 端口复用、单实例锁、优雅关闭、请求超时
10
+ * - MCP Streamable HTTP handler 装配与契约拒绝
11
+ * - 对象仓库(/assets)兜底
12
+ * - bridge.json 发现文件写入与清理
13
+ * - 独立运行入口(node server/relay.mjs --port --dir)
14
+ *
15
+ * A 段职责(全部是业务决策):
16
+ * prepare / createStore / routes / health / metrics / tools / instructions / getTuning
17
+ *
18
+ * 两种承载形态,都由框架决定,A 段不选端口:
19
+ * embedded(默认,DSH 宿主进程内)—— 传 `webServer`,应用挂到宿主 HTTP 路由的
20
+ * 前缀下(默认 `/capyclaw/<id>`),**不监听任何端口**,生命周期随 DSH。
21
+ * standalone(仅开发/测试)—— 没传 `webServer` 时按 `port` 自己监听 loopback。
22
+ *
23
+ * 窗口不在这里:桌面窗口是基建能力(@capyclaw/portal-hud 主区舞台)。workbench
24
+ * 只提供页面,就绪时把编辑地址记进活动台账;开不开窗由舞台的内容闸与链接拦截决定。
25
+ */
26
+ import fs from 'node:fs'
27
+ import os from 'node:os'
28
+ import path from 'node:path'
29
+ import { fileURLToPath } from 'node:url'
30
+
31
+ const PKG_ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..')
32
+
33
+ /**
34
+ * 返回与历史签名兼容的 startRelay。
35
+ *
36
+ * spec:
37
+ * id / title / packageVersion / contractVersion
38
+ * port / basePath / homeEnv / defaultHome
39
+ * getTuning() 调优参数(A 的业务模块;缺省用框架默认值)
40
+ * contract { version, inspect(dir, version), describe(state) }
41
+ * createStore({ hostDir, backend, packageDir }) => store
42
+ * createAssetStore({ hostDir }) => assetStore | null
43
+ * prepare(ctx) => 额外业务资源(例如浏览器引擎),合并进 ctx
44
+ * routes(ctx) => route[](业务路由,按声明顺序挂载)
45
+ * health(ctx) / metrics(ctx) 业务健康与指标
46
+ * tools(ctx) => { TOOLS, TOOL_IMPL, CAPABILITIES, exemptTool? }
47
+ * instructions(ctx) => string
48
+ * onListening(ctx, info, url) 监听后、装载工具前(设 URL 环境变量、崩溃恢复)
49
+ * resultExtras(ctx) 追加进返回对象的字段(历史形状兼容)
50
+ * beforeStop(ctx) 停服前的业务清理(例如关浏览器)
51
+ */
52
+ export function createWorkbench(spec) {
53
+ return async function startRelay(options = {}) {
54
+ const {
55
+ port = spec.port,
56
+ /* embedded 形态:宿主(DSH)把 HTTP 路由借给我们,不监听端口。 */
57
+ webServer = null,
58
+ mountPrefix = spec.mountPrefix || `/capyclaw/${spec.id}`,
59
+ hostDir: hostDirOption = null,
60
+ packageDir = spec.packageDir || PKG_ROOT,
61
+ scanLimit = 50,
62
+ lockFile,
63
+ strictPort = false,
64
+ allowReuse = true,
65
+ backend = null,
66
+ assetStore: givenAssetStore = null,
67
+ assetService = null,
68
+ capabilities = null,
69
+ serviceHost = null,
70
+ toolHost = null,
71
+ editorHost = null,
72
+ ...extra
73
+ } = options
74
+
75
+ /* 承载形态决定状态目录:embedded 归 DSH 的 `~/.dsh/workbenches/<id>`,
76
+ * standalone(开发/测试)沿用历史 `~/.dsh-<id>-bridge` 之类的位置。 */
77
+ const hostDir = hostDirOption
78
+ || (spec.homeEnv && process.env[spec.homeEnv])
79
+ || (webServer
80
+ ? path.join(os.homedir(), '.dsh', 'workbenches', spec.id)
81
+ : path.join(os.homedir(), spec.defaultHome || `.dsh-${spec.id}`))
82
+
83
+ const { createServiceApp } = await import('./shared/service-host.mjs')
84
+ const { createMcpHandler } = await import('./shared/kit/service/mcp-http.mjs')
85
+
86
+ const contractState = spec.contract
87
+ ? spec.contract.inspect(packageDir, spec.contract.version)
88
+ : { ok: true, checked: 0 }
89
+ if (!contractState.ok) {
90
+ console.error(`[${spec.id}] 契约快照不一致,工具调用将被拒绝:`, spec.contract.describe?.(contractState) || '')
91
+ }
92
+
93
+ const store = await spec.createStore({ hostDir, backend, packageDir })
94
+ let assetStore = givenAssetStore
95
+ if (!assetStore && !assetService && spec.createAssetStore) {
96
+ try {
97
+ assetStore = spec.createAssetStore({ hostDir })
98
+ } catch (error) {
99
+ console.error(`[${spec.id}] 对象仓库不可用(/assets 将返回 503):`, error.message)
100
+ }
101
+ }
102
+
103
+ const ctx = {
104
+ id: spec.id, hostDir, store, capabilities, contractState, packageDir,
105
+ serviceHost, toolHost, editorHost, assetService, ...extra,
106
+ }
107
+ Object.defineProperty(ctx, 'assetStore', { get: () => assetStore, enumerable: true })
108
+
109
+ /* 资源闭合:createStore/createAssetStore/prepare 都发生在 listen 之前。任何
110
+ * 「没进入正常服务」的路径(prepare 失败、listen 抛错、复用命中)都必须回收
111
+ * 已创建的资源——否则引擎/仓库泄漏在仍存活的进程里(reused 分支的 stop 是
112
+ * no-op,不会有人替它清理)。外部传入的 assetStore 不归本框架关。 */
113
+ let app
114
+ const closeCreatedResources = async (why) => {
115
+ try { if (spec.beforeStop) await spec.beforeStop(ctx) } catch (error) { console.error(`[${spec.id}] ${why}:beforeStop 失败`, error?.message || error) }
116
+ try { if (!givenAssetStore) await assetStore?.close?.() } catch (error) { console.error(`[${spec.id}] ${why}:assetStore 关闭失败`, error?.message || error) }
117
+ try { await store?.close?.() } catch (error) { console.error(`[${spec.id}] ${why}:store 关闭失败`, error?.message || error) }
118
+ try { await app?.close?.() } catch (error) { console.error(`[${spec.id}] ${why}:app 关闭失败`, error?.message || error) }
119
+ }
120
+
121
+ /* 业务资源准备(例如浏览器引擎、进程内 session 注入):失败即启动失败。 */
122
+ if (spec.prepare) {
123
+ try {
124
+ Object.assign(ctx, (await spec.prepare(ctx)) || {})
125
+ } catch (error) {
126
+ await closeCreatedResources('prepare 失败')
127
+ throw error
128
+ }
129
+ }
130
+
131
+ const createApp = serviceHost?.createApp ? (opts) => serviceHost.createApp(opts) : createServiceApp
132
+ /* 桥声明(来自 foundation declareBridge):对外暴露成 bridgeInfo,
133
+ * 供 suite 清单校验与巡检对齐「声明 vs 运行时」。 */
134
+ const bridgeInfo = spec.bridge
135
+ ? { id: spec.bridge.id, protocol: spec.bridge.protocol, model: spec.bridge.model || null, editor: spec.bridge.editor || 'required' }
136
+ : null
137
+ app = createApp({
138
+ id: spec.id,
139
+ title: spec.title,
140
+ version: spec.packageVersion,
141
+ contractVersion: spec.contractVersion,
142
+ health: () => ({
143
+ ...(bridgeInfo ? { bridgeInfo } : {}),
144
+ ...(spec.health ? spec.health(ctx) : {}),
145
+ contractsOk: contractState.ok,
146
+ contractsChecked: contractState.checked,
147
+ }),
148
+ metrics: () => (spec.metrics ? spec.metrics(ctx) : []),
149
+ })
150
+
151
+ /* routes 里可能要用 app 本身(例如公共 asset-service 的 mount)。 */
152
+ ctx.app = app
153
+ /* 挂载前缀必须在 route 工厂**之前**可见:稿件/图片路由会把给浏览器的媒体地址
154
+ * 拼成 `<mountPath><basePath>/media/<sha>`;embedded 下晚一步设,就会漏掉
155
+ * `/capyclaw/<ns>`,编辑器和导出取图全部 404(2026-11-10 实测)。 */
156
+ const embeddable = !!(webServer && typeof webServer.register === 'function')
157
+ ctx.mountPath = embeddable ? mountPrefix : ''
158
+ for (const route of spec.routes(ctx) || []) app.use(route)
159
+
160
+ const tuning = spec.getTuning ? spec.getTuning() : {}
161
+ let info
162
+ try {
163
+ info = embeddable
164
+ ? await app.listen({
165
+ webServer,
166
+ basePath: mountPrefix,
167
+ requestTimeoutMs: Number(tuning.httpRequestTimeoutMs) || 30000,
168
+ headersTimeoutMs: Number(tuning.httpHeadersTimeoutMs) || 10000,
169
+ keepAliveTimeoutMs: Number(tuning.httpKeepAliveTimeoutMs) || 5000,
170
+ })
171
+ : await app.listen({
172
+ port,
173
+ hostDir,
174
+ scanLimit,
175
+ lockFile: lockFile || path.join(hostDir, '.relay-instance.lock'),
176
+ strictPort,
177
+ allowReuse,
178
+ discoveryFile: '',
179
+ requestTimeoutMs: Number(tuning.httpRequestTimeoutMs) || 30000,
180
+ headersTimeoutMs: Number(tuning.httpHeadersTimeoutMs) || 10000,
181
+ keepAliveTimeoutMs: Number(tuning.httpKeepAliveTimeoutMs) || 5000,
182
+ })
183
+ } catch (error) {
184
+ /* listen 抛错(端口冲突/锁失败等)同样要回收已建的 store/引擎,
185
+ * 这里在下方 try 之外,旧代码会让它们永久泄漏。 */
186
+ await closeCreatedResources('listen 失败')
187
+ throw error
188
+ }
189
+
190
+ /* info.url 在 embedded 下已经是「origin + 挂载前缀」,standalone 下是 origin。
191
+ * 业务只认 ctx.editorUrl / ctx.url(接口基址),不需要知道端口或前缀。 */
192
+ const base = info.url
193
+ const url = `${base}${spec.basePath || ''}`
194
+ ctx.port = info.port
195
+ ctx.embedded = info.mode === 'embedded'
196
+ ctx.origin = info.mode === 'embedded' ? `http://127.0.0.1:${info.port}` : info.url
197
+ ctx.mountPath = info.mode === 'embedded' ? new URL(base).pathname.replace(/\/$/, '') : ''
198
+ ctx.editorUrl = `${base}/`
199
+ ctx.url = url
200
+
201
+ if (info.reused) {
202
+ /* 复用命中:本进程不提供服务,必须回收已创建的 store/引擎等资源,
203
+ * 否则它们泄漏在仍存活的进程里(下面的 stop 是 no-op,无人清理)。 */
204
+ await closeCreatedResources('复用既有实例')
205
+ return {
206
+ url,
207
+ editorUrl: ctx.editorUrl,
208
+ port: info.port,
209
+ mode: info.mode || 'standalone',
210
+ reused: true,
211
+ ...(spec.resultExtras ? spec.resultExtras(ctx) : {}),
212
+ stop: async () => {},
213
+ }
214
+ }
215
+
216
+ try {
217
+ /* 业务钩子:设置 URL 环境变量、崩溃恢复等(必须在工具装载之前)。 */
218
+ if (spec.onListening) await spec.onListening(ctx, info, url)
219
+
220
+ if (spec.tools) {
221
+ const registry = (await spec.tools(ctx)) || {}
222
+ const TOOLS = registry.TOOLS || []
223
+ ctx.tools = TOOLS
224
+ ctx.toolsRegistry = registry
225
+ const mcpHandler = createMcpHandler({
226
+ tools: TOOLS,
227
+ callTool: (name, args) => {
228
+ if (!contractState.ok && !registry.exemptTool?.(name)) {
229
+ throw new Error(
230
+ `契约快照与运行时 major 不一致,已拒绝 ${name}:` +
231
+ `${spec.contract.describe?.(contractState) || ''}。请重装/升级对应 suite 后重试。`,
232
+ )
233
+ }
234
+ const impl = registry.TOOL_IMPL[name]
235
+ if (!impl) throw new Error('未知工具: ' + name)
236
+ return impl(args)
237
+ },
238
+ serverInfo: { name: spec.id, version: spec.packageVersion },
239
+ instructions: spec.instructions ? spec.instructions(ctx) : undefined,
240
+ maxBodyBytes: Number(tuning.bodyLimitBytes) || 12 * 1024 * 1024,
241
+ maxBatch: Number(tuning.mcpBatchLimit) || 100,
242
+ })
243
+ ctx.getMcpHandler = () => mcpHandler
244
+ }
245
+
246
+ spec.onStarted?.({ url, port: info.port, hostDir, store, ctx })
247
+ /* 独立端口形态才写发现文件;embedded 的生命周期由宿主 ctx.effect 管。 */
248
+ if (!ctx.embedded) writeDiscovery(hostDir, spec.id, url, info.port)
249
+ return {
250
+ url,
251
+ editorUrl: ctx.editorUrl,
252
+ port: info.port,
253
+ mode: info.mode || 'standalone',
254
+ reused: false,
255
+ tools: ctx.toolsRegistry || null,
256
+ ...(spec.resultExtras ? spec.resultExtras(ctx) : {}),
257
+ stop: async () => {
258
+ if (!ctx.embedded) removeDiscovery(hostDir)
259
+ if (spec.beforeStop) await spec.beforeStop(ctx)
260
+ await info.stop()
261
+ },
262
+ }
263
+ } catch (error) {
264
+ try { if (!ctx.embedded) removeDiscovery(hostDir) } catch { /* 忽略 */ }
265
+ try { if (spec.beforeStop) await spec.beforeStop(ctx) } catch { /* 忽略 */ }
266
+ try { await info.stop() } catch { /* 忽略 */ }
267
+ throw error
268
+ }
269
+ }
270
+ }
271
+
272
+ /** 独立运行入口:node server/relay.mjs --port <n> --dir <hostDir>(符号链接安装下也判得准)。 */
273
+ export function runIfMain(moduleUrl, main) {
274
+ if (!process.argv[1]) return false
275
+ let isMain = false
276
+ try {
277
+ isMain = fs.realpathSync(path.resolve(process.argv[1])) === fileURLToPath(moduleUrl)
278
+ } catch {
279
+ isMain = path.resolve(process.argv[1]) === fileURLToPath(moduleUrl)
280
+ }
281
+ if (!isMain) return false
282
+ const arg = (flag, fallback) => {
283
+ const i = process.argv.indexOf(flag)
284
+ return i >= 0 && process.argv[i + 1] ? process.argv[i + 1] : fallback
285
+ }
286
+ main({ port: Number(arg('--port', undefined)) || undefined, dir: arg('--dir', undefined) })
287
+ return true
288
+ }
289
+
290
+ function writeDiscovery(hostDir, id, url, port) {
291
+ try {
292
+ fs.mkdirSync(hostDir, { recursive: true })
293
+ fs.writeFileSync(path.join(hostDir, 'bridge.json'), JSON.stringify({
294
+ bridge: id, url, port, pid: process.pid, updated: new Date().toISOString(),
295
+ }, null, 2))
296
+ } catch { /* 发现文件写失败不影响服务 */ }
297
+ }
298
+
299
+ function removeDiscovery(hostDir) {
300
+ const file = path.join(hostDir, 'bridge.json')
301
+ try {
302
+ const data = JSON.parse(fs.readFileSync(file, 'utf8'))
303
+ if (data.pid === process.pid) fs.rmSync(file, { force: true })
304
+ } catch { /* 不存在或不是本进程写的 */ }
305
+ }
@@ -0,0 +1,142 @@
1
+ /**
2
+ * 展示墙选择器页:
3
+ * - 从主区舞台 /__portal_hud__/state 读已注册的工作台类型(类型无关,不硬编码清单);
4
+ * - 点选 → POST claim 显式认领会话模式(explicit),舞台随后收掉墙、摆出真实工作台;
5
+ * - 舞台换会话时 postMessage 通知本页(retarget):立即回 ack,再按新会话重取清单。
6
+ *
7
+ * standalone(无 portal-hud)时 state 取不到 → 降级为提示,不报错。
8
+ */
9
+ (function () {
10
+ 'use strict'
11
+
12
+ /* 与 service-host 的会话段规则一致:/s/<sid>(sid 字母数字开头,可含 . _ ~ -)。 */
13
+ var SESSION_RE = /\/s\/([A-Za-z0-9][A-Za-z0-9._~-]{0,127})(?=\/|$)/
14
+
15
+ var cardsEl = document.getElementById('cards')
16
+ var statusEl = document.getElementById('status')
17
+ var sid = currentSid()
18
+ var busy = false
19
+ var locked = false
20
+
21
+ function currentSid() {
22
+ var m = SESSION_RE.exec(location.pathname)
23
+ return m ? m[1] : ''
24
+ }
25
+
26
+ /* 挂载前缀 = 去掉会话段的路径(embedded: /capyclaw/wall;standalone: '')。 */
27
+ function mountBase() {
28
+ return location.pathname.replace(SESSION_RE, '').replace(/\/$/, '')
29
+ }
30
+
31
+ function setStatus(text, tone) {
32
+ statusEl.textContent = text || ''
33
+ statusEl.className = 'status' + (tone ? ' status-' + tone : '')
34
+ }
35
+
36
+ function claimUrl() {
37
+ return mountBase() + (sid ? '/s/' + encodeURIComponent(sid) : '') + '/claim'
38
+ }
39
+
40
+ function render(surfaces) {
41
+ cardsEl.textContent = ''
42
+ var others = surfaces.filter(function (row) {
43
+ return row && row.slug !== 'wall' && row.id !== 'wall-workbench'
44
+ })
45
+ if (!others.length) {
46
+ setStatus('当前 profile 没有注册其他工作台。安装相应套件(如 ppt-suite / browser-suite)后刷新。', 'warn')
47
+ return
48
+ }
49
+ others.forEach(function (row) {
50
+ var card = document.createElement('button')
51
+ card.type = 'button'
52
+ card.className = 'card'
53
+ card.dataset.kind = row.slug
54
+ var title = document.createElement('span')
55
+ title.className = 'card-title'
56
+ title.textContent = row.title || row.slug
57
+ var slug = document.createElement('span')
58
+ slug.className = 'card-slug'
59
+ slug.textContent = row.slug
60
+ card.appendChild(title)
61
+ card.appendChild(slug)
62
+ card.addEventListener('click', function () { choose(row.slug, card) })
63
+ cardsEl.appendChild(card)
64
+ })
65
+ }
66
+
67
+ function lockCards() {
68
+ locked = true
69
+ var cards = cardsEl.querySelectorAll('.card')
70
+ for (var i = 0; i < cards.length; i++) cards[i].disabled = true
71
+ }
72
+
73
+ function unlockCards() {
74
+ locked = false
75
+ var cards = cardsEl.querySelectorAll('.card')
76
+ for (var i = 0; i < cards.length; i++) cards[i].disabled = false
77
+ }
78
+
79
+ function choose(kind, card) {
80
+ if (busy || locked) return
81
+ busy = true
82
+ card.classList.add('card-busy')
83
+ setStatus('正在锁定「' + kind + '」…')
84
+ fetch(claimUrl(), {
85
+ method: 'POST',
86
+ headers: { 'content-type': 'application/json' },
87
+ body: JSON.stringify({ sid: sid, kind: kind }),
88
+ }).then(function (res) {
89
+ return res.json().catch(function () { return { ok: false, error: '响应不是 JSON' } })
90
+ }).then(function (result) {
91
+ busy = false
92
+ card.classList.remove('card-busy')
93
+ if (result && result.ok) {
94
+ lockCards()
95
+ if (result.skipped) {
96
+ setStatus('已选择「' + kind + '」(standalone 形态没有会话模式底座,未启用锁定)。', 'warn')
97
+ } else {
98
+ setStatus('已锁定「' + kind + '」。主区将切换到对应工作台。', 'ok')
99
+ }
100
+ } else if (result && result.lockedTo) {
101
+ setStatus('本会话已固化为「' + result.lockedTo + '」,不能再选「' + kind + '」。请新开一个会话。', 'warn')
102
+ } else {
103
+ setStatus('认领失败:' + ((result && result.error) || '未知错误'), 'warn')
104
+ }
105
+ }).catch(function (err) {
106
+ busy = false
107
+ card.classList.remove('card-busy')
108
+ setStatus('认领请求失败:' + (err && err.message ? err.message : err), 'warn')
109
+ })
110
+ }
111
+
112
+ function loadState() {
113
+ var url = '/__portal_hud__/state' + (sid ? '?sid=' + encodeURIComponent(sid) : '')
114
+ return fetch(url).then(function (res) {
115
+ if (!res.ok) throw new Error('HTTP ' + res.status)
116
+ return res.json()
117
+ }).then(function (data) {
118
+ render((data && data.surfaces) || [])
119
+ if (!locked) setStatus('')
120
+ }).catch(function () {
121
+ cardsEl.textContent = ''
122
+ setStatus('读不到已注册的工作台(standalone 形态没有主区舞台注册信息)。请在 DSH 宿主内打开本页。', 'warn')
123
+ })
124
+ }
125
+
126
+ /* 舞台 retarget:同工作台换会话不重载 iframe,只 postMessage。
127
+ * 必须立刻回 ack(父侧 600ms 无 ack 会硬重载),再按新会话重取清单。 */
128
+ window.addEventListener('message', function (e) {
129
+ if (e.origin !== location.origin) return
130
+ var d = e.data
131
+ if (!d || d.type !== 'capyclaw:workbench:session') return
132
+ sid = String(d.sid || '')
133
+ if (e.source && typeof e.source.postMessage === 'function') {
134
+ e.source.postMessage({ type: 'capyclaw:workbench:session-ack', sid: sid }, location.origin)
135
+ }
136
+ unlockCards()
137
+ setStatus('')
138
+ loadState()
139
+ })
140
+
141
+ loadState()
142
+ })()
@@ -0,0 +1,20 @@
1
+ <!doctype html>
2
+ <html lang="zh-CN">
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1">
6
+ <title>展示墙 · 选择要做什么</title>
7
+ <link rel="stylesheet" href="style.css">
8
+ </head>
9
+ <body>
10
+ <main class="wall">
11
+ <header class="wall-head">
12
+ <h1>选择要做什么</h1>
13
+ <p class="wall-sub">这个会话还没有绑定工作台。选一个类型,本会话将固定使用它。</p>
14
+ </header>
15
+ <div id="cards" class="cards" aria-live="polite"></div>
16
+ <p id="status" class="status" role="status"></p>
17
+ </main>
18
+ <script src="app.js"></script>
19
+ </body>
20
+ </html>
@@ -0,0 +1,74 @@
1
+ /* 展示墙选择器:中性纸面 + 单一强调色,铺满主区容器(无窗壳)。 */
2
+ :root {
3
+ --paper: #f7f7f5;
4
+ --ink: #1c1e21;
5
+ --muted: #6b7075;
6
+ --line: #e3e4e0;
7
+ --accent: #2f6f4f;
8
+ --accent-soft: #eaf2ec;
9
+ --warn: #8a5a00;
10
+ --ok: #2f6f4f;
11
+ }
12
+
13
+ * { box-sizing: border-box; }
14
+
15
+ html, body {
16
+ margin: 0;
17
+ height: 100%;
18
+ background: var(--paper);
19
+ color: var(--ink);
20
+ font-family: "Segoe UI", "Microsoft YaHei", system-ui, sans-serif;
21
+ }
22
+
23
+ .wall {
24
+ min-height: 100%;
25
+ display: flex;
26
+ flex-direction: column;
27
+ align-items: center;
28
+ justify-content: center;
29
+ padding: 48px 24px 96px; /* 底部留白避开抬高的输入框底座 */
30
+ }
31
+
32
+ .wall-head { text-align: center; margin-bottom: 32px; }
33
+ .wall-head h1 { font-size: 28px; font-weight: 600; margin: 0 0 8px; letter-spacing: .02em; }
34
+ .wall-sub { margin: 0; color: var(--muted); font-size: 14px; }
35
+
36
+ .cards {
37
+ display: grid;
38
+ grid-template-columns: repeat(auto-fit, minmax(200px, 260px));
39
+ gap: 16px;
40
+ justify-content: center;
41
+ width: 100%;
42
+ max-width: 900px;
43
+ }
44
+
45
+ .card {
46
+ display: flex;
47
+ flex-direction: column;
48
+ gap: 6px;
49
+ align-items: flex-start;
50
+ padding: 20px 22px;
51
+ background: #fff;
52
+ border: 1px solid var(--line);
53
+ border-radius: 12px;
54
+ cursor: pointer;
55
+ text-align: left;
56
+ font: inherit;
57
+ color: inherit;
58
+ transition: border-color .15s ease, box-shadow .15s ease, transform .15s ease;
59
+ }
60
+ .card:hover:not(:disabled) {
61
+ border-color: var(--accent);
62
+ box-shadow: 0 4px 16px rgba(28, 30, 33, .08);
63
+ transform: translateY(-2px);
64
+ }
65
+ .card:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
66
+ .card:disabled { opacity: .55; cursor: default; }
67
+ .card-busy { border-color: var(--accent); background: var(--accent-soft); }
68
+
69
+ .card-title { font-size: 17px; font-weight: 600; }
70
+ .card-slug { font-size: 12px; color: var(--muted); font-family: Consolas, monospace; }
71
+
72
+ .status { margin-top: 28px; min-height: 20px; font-size: 14px; color: var(--muted); text-align: center; }
73
+ .status-ok { color: var(--ok); }
74
+ .status-warn { color: var(--warn); }
package/package.json CHANGED
@@ -1,6 +1,74 @@
1
1
  {
2
2
  "name": "@capyclaw/wall-suite",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "主区空态选择器(挂在 dsh 的 /capyclaw/wall,路由与端口由基建决定):会话未绑定模式时列出已注册的工作台类型,点选即显式认领会话模式;零工具零技能。公共基建(os / toolHost / serviceHost / assets / serviceBroker)由 @capyclaw/core 统一提供,需同一 profile 启用它。",
5
+ "type": "module",
6
+ "main": "./lib/index.mjs",
7
+ "exports": {
8
+ ".": "./lib/index.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
+ "dependencies": {
19
+ "@capyclaw/asset-service": "^0.1.4",
20
+ "@capyclaw/foundation": "^0.1.9",
21
+ "@capyclaw/plugin-kit": "^0.2.6",
22
+ "@capyclaw/service-broker": "^0.2.25",
23
+ "@capyclaw/service-host": "^0.1.5",
24
+ "@capyclaw/tool-host": "^0.2.0",
25
+ "@capyclaw/wall-workbench": "^0.1.0"
26
+ },
27
+ "dsh": {
28
+ "bundle": {
29
+ "patch": "./cordis.patch.yml"
30
+ },
31
+ "suite": "./dsh-suite.json",
32
+ "category": "service",
33
+ "platform": "both"
34
+ },
35
+ "meta": {
36
+ "title": "展示墙工作台套件",
37
+ "description": "主区空态选择器(挂在 dsh 的 /capyclaw/wall,路由与端口由基建决定):会话未绑定模式时列出已注册的工作台类型,点选即显式认领会话模式;零工具零技能。公共基建(os / toolHost / serviceHost / assets / serviceBroker)由 @capyclaw/core 统一提供,需同一 profile 启用它。"
38
+ },
39
+ "files": [
40
+ "lib/",
41
+ "contract/",
42
+ "dsh-suite.json",
43
+ "composition.lock.json",
44
+ "cordis.patch.yml",
45
+ "README.md",
46
+ "services.json"
47
+ ],
48
+ "license": "MIT",
49
+ "scripts": {},
50
+ "author": {
51
+ "name": "CapyClaw",
52
+ "url": "https://github.com/capyclaw",
53
+ "email": "support@capyclaw.dev"
54
+ },
55
+ "repository": {
56
+ "type": "git",
57
+ "url": "git+https://github.com/Maple496/capyclaw.git",
58
+ "directory": "A/projects/wall/artifacts/plugin/capyclaw-wall-suite"
59
+ },
60
+ "bugs": {
61
+ "url": "https://github.com/Maple496/capyclaw/issues"
62
+ },
63
+ "homepage": "https://github.com/Maple496/capyclaw/tree/main/A/projects/wall/artifacts/plugin/capyclaw-wall-suite#readme",
64
+ "bundledDependencies": [
65
+ "@capyclaw/asset-service",
66
+ "@capyclaw/foundation",
67
+ "@capyclaw/plugin-kit",
68
+ "@capyclaw/portal",
69
+ "@capyclaw/service-broker",
70
+ "@capyclaw/service-host",
71
+ "@capyclaw/tool-host",
72
+ "@capyclaw/wall-workbench"
73
+ ]
74
+ }
package/services.json ADDED
@@ -0,0 +1,3 @@
1
+ {
2
+ "services": []
3
+ }