@capyclaw/wall-suite 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.
Files changed (63) hide show
  1. package/composition.lock.json +15 -15
  2. package/contract/wall-contract.json +1 -1
  3. package/dsh-suite.json +19 -9
  4. package/node_modules/@capyclaw/asset-service/dsh-suite.json +1 -1
  5. package/node_modules/@capyclaw/asset-service/package.json +1 -1
  6. package/node_modules/@capyclaw/asset-service/src/index.mjs +96 -9
  7. package/node_modules/@capyclaw/foundation/abi/assets.mjs +26 -0
  8. package/node_modules/@capyclaw/foundation/abi/bridge.mjs +3 -1
  9. package/node_modules/@capyclaw/foundation/abi/index.mjs +15 -0
  10. package/node_modules/@capyclaw/foundation/abi/loader.mjs +8 -1
  11. package/node_modules/@capyclaw/foundation/abi/spec.mjs +5 -2
  12. package/node_modules/@capyclaw/foundation/abi/version.mjs +63 -5
  13. package/node_modules/@capyclaw/foundation/abi/visibility.mjs +90 -0
  14. package/node_modules/@capyclaw/foundation/lib/adapters/lifecycle-hook.mjs +21 -4
  15. package/node_modules/@capyclaw/foundation/lib/adapters/raw.mjs +11 -10
  16. package/node_modules/@capyclaw/foundation/lib/adapters/runtime-driver.mjs +8 -1
  17. package/node_modules/@capyclaw/foundation/lib/host/caps.mjs +5 -1
  18. package/node_modules/@capyclaw/foundation/lib/host/contract.mjs +5 -2
  19. package/node_modules/@capyclaw/foundation/lib/host/dsh.mjs +4 -0
  20. package/node_modules/@capyclaw/foundation/lib/host/memory.mjs +29 -2
  21. package/node_modules/@capyclaw/foundation/lib/kernel.mjs +13 -3
  22. package/node_modules/@capyclaw/foundation/lib/skill/scan.mjs +31 -3
  23. package/node_modules/@capyclaw/foundation/lib/subsystems/net.mjs +12 -2
  24. package/node_modules/@capyclaw/foundation/package.json +1 -1
  25. package/node_modules/@capyclaw/plugin-kit/package.json +3 -1
  26. package/node_modules/@capyclaw/plugin-kit/src/editor/heartbeat-worker.js +5 -2
  27. package/node_modules/@capyclaw/plugin-kit/src/editor/heartbeat.mjs +26 -0
  28. package/node_modules/@capyclaw/plugin-kit/src/editor/lease.mjs +3 -82
  29. package/node_modules/@capyclaw/plugin-kit/src/service/mcp-http.mjs +13 -6
  30. package/node_modules/@capyclaw/plugin-kit/src/service/static.mjs +79 -0
  31. package/node_modules/@capyclaw/plugin-kit/src/tools/result.mjs +31 -15
  32. package/node_modules/@capyclaw/plugin-kit/src/workspace/manager.mjs +13 -5
  33. package/node_modules/@capyclaw/portal/package.json +1 -1
  34. package/node_modules/@capyclaw/service-broker/README.md +1 -1
  35. package/node_modules/@capyclaw/service-broker/lib/control.mjs +23 -7
  36. package/node_modules/@capyclaw/service-broker/lib/engine.mjs +60 -6
  37. package/node_modules/@capyclaw/service-broker/lib/host.mjs +11 -6
  38. package/node_modules/@capyclaw/service-broker/lib/manifest.mjs +15 -2
  39. package/node_modules/@capyclaw/service-broker/lib/recovery.mjs +0 -50
  40. package/node_modules/@capyclaw/service-broker/lib/skills-client.mjs +30 -10
  41. package/node_modules/@capyclaw/service-broker/package.json +1 -1
  42. package/node_modules/@capyclaw/service-host/dsh-suite.json +1 -1
  43. package/node_modules/@capyclaw/service-host/package.json +1 -1
  44. package/node_modules/@capyclaw/service-host/src/index.mjs +18 -19
  45. package/node_modules/@capyclaw/tool-host/dsh-suite.json +2 -2
  46. package/node_modules/@capyclaw/tool-host/lib/host.mjs +34 -4
  47. package/node_modules/@capyclaw/tool-host/package.json +1 -1
  48. package/node_modules/@capyclaw/tool-host/src/admit-images.mjs +80 -0
  49. package/node_modules/@capyclaw/tool-host/src/caller-session.mjs +25 -0
  50. package/node_modules/@capyclaw/tool-host/src/descriptors.mjs +28 -4
  51. package/node_modules/@capyclaw/tool-host/src/index.mjs +90 -19
  52. package/node_modules/@capyclaw/tool-host/src/mode-gate.mjs +272 -0
  53. package/node_modules/@capyclaw/wall-workbench/contract/wall-contract.json +1 -1
  54. package/node_modules/@capyclaw/wall-workbench/package.json +6 -6
  55. package/node_modules/@capyclaw/wall-workbench/plugin.mjs +13 -1
  56. package/node_modules/@capyclaw/wall-workbench/server/activate-log.mjs +66 -0
  57. package/node_modules/@capyclaw/wall-workbench/server/contract.mjs +1 -1
  58. package/node_modules/@capyclaw/wall-workbench/server/embedded-host.mjs +3 -1
  59. package/node_modules/@capyclaw/wall-workbench/server/shared/SHARED_MANIFEST.json +4 -4
  60. package/node_modules/@capyclaw/wall-workbench/server/workbench-framework.mjs +34 -0
  61. package/node_modules/@capyclaw/wall-workbench/ui/app.js +83 -0
  62. package/node_modules/@capyclaw/wall-workbench/ui/style.css +14 -11
  63. package/package.json +8 -8
@@ -1,48 +1,48 @@
1
1
  {
2
2
  "suite": "capyclaw-wall-suite",
3
- "suiteVersion": "0.1.0",
3
+ "suiteVersion": "0.1.1",
4
4
  "packages": {
5
5
  "@capyclaw/asset-service": {
6
- "version": "0.1.4",
6
+ "version": "0.1.6",
7
7
  "apiVersion": "1.0.0",
8
8
  "path": "..\\..\\..\\..\\..\\..\\C\\packages\\capyclaw-asset-service",
9
- "sha256": "a3fe6f1b0a37514a396d91f06dee214f18ea19fb4ecebfb994cf59a16420853d"
9
+ "sha256": "f776c6f40bb3ffb8511e7c471d53779e81efb29a4aa40c14172ea153d4071a9f"
10
10
  },
11
11
  "@capyclaw/foundation": {
12
- "version": "0.1.9",
12
+ "version": "0.1.11",
13
13
  "apiVersion": "1.0.0",
14
14
  "path": "../../../../../../C/packages/capyclaw-foundation",
15
- "sha256": "5e7bab76caa09bee0a94a03650b2447cc880d743231c73695f49b7a92077d5fa"
15
+ "sha256": "2b1860bd9081f8ebeade500255bf908fe5d62740d2394c7d0b0f65149a2c82b5"
16
16
  },
17
17
  "@capyclaw/plugin-kit": {
18
- "version": "0.2.6",
18
+ "version": "0.2.8",
19
19
  "apiVersion": null,
20
20
  "path": "../../../../../../C/packages/capyclaw-plugin-kit",
21
- "sha256": "02f74f615cd6baea890f768a141167d5623e8b8cfe4c50ab8639046231c98d33"
21
+ "sha256": "5b9dd6bdadee6ecb32efcb59b59e7d6bbcd770fdb7762fc8f5aa90eedfee6186"
22
22
  },
23
23
  "@capyclaw/service-broker": {
24
- "version": "0.2.25",
24
+ "version": "0.2.27",
25
25
  "apiVersion": "1.0.0",
26
26
  "path": "../../../../../../C/packages/capyclaw-service-broker",
27
- "sha256": "0efd962a02fb2d6213cba67778b5e34ec2f57b8e9f50b4c94374e02d1dc9b6c5"
27
+ "sha256": "75df80d0e2f8df30138937fb601607a2ddade449c64f12e08a45e48cff1712e1"
28
28
  },
29
29
  "@capyclaw/service-host": {
30
- "version": "0.1.5",
30
+ "version": "0.1.7",
31
31
  "apiVersion": "1.0.0",
32
32
  "path": "..\\..\\..\\..\\..\\..\\C\\packages\\capyclaw-service-host",
33
- "sha256": "be7ef447a8c2dc6005edb0b28d0b171c7bc5c773185ad971811896bb9c6388ba"
33
+ "sha256": "be173a38c0087fa6fdb06a10e57356a02afe836ac9994b905eb00abe6516ce45"
34
34
  },
35
35
  "@capyclaw/tool-host": {
36
- "version": "0.2.0",
36
+ "version": "0.3.2",
37
37
  "apiVersion": "1.1.0",
38
38
  "path": "..\\..\\..\\..\\..\\..\\C\\packages\\capyclaw-tool-host",
39
- "sha256": "93e6fe760f2d421c0767058bec55fc735f82453285e81be905d9fb0fdd103647"
39
+ "sha256": "2e4927e7281457602a57eadff33ae25ea4b5d60539d319d9819d86c0db92aadd"
40
40
  },
41
41
  "@capyclaw/wall-workbench": {
42
- "version": "0.1.0",
42
+ "version": "0.1.2",
43
43
  "apiVersion": null,
44
44
  "path": "../capyclaw-wall-workbench",
45
- "sha256": "d8ee17ed863b9f581bfb5b8f63a9db4accbeda5774be814cb260b651af8edd86"
45
+ "sha256": "d6a93b79d0d0b92b35a0c597261a3dd3eab2ed694c9acda74f436a7886d1daca"
46
46
  }
47
47
  }
48
48
  }
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "contractVersion": "1.0.0",
3
- "packageVersion": "0.1.0",
3
+ "packageVersion": "0.1.2",
4
4
  "toolCount": 0,
5
5
  "tools": [],
6
6
  "features": [],
package/dsh-suite.json CHANGED
@@ -1,23 +1,23 @@
1
1
  {
2
2
  "$schema": "../../../../../../C/packages/capyclaw-suite-kit/suite.schema.json",
3
3
  "name": "capyclaw-wall-suite",
4
- "version": "0.1.0",
4
+ "version": "0.1.1",
5
5
  "title": "展示墙工作台套件",
6
6
  "description": "主区空态选择器(挂在 dsh 的 /capyclaw/wall,路由与端口由基建决定):会话未绑定模式时列出已注册的工作台类型,点选即显式认领会话模式;零工具零技能。公共基建(os / toolHost / serviceHost / assets / serviceBroker)由 @capyclaw/core 统一提供,需同一 profile 启用它。",
7
7
  "packages": {
8
8
  "@capyclaw/foundation": {
9
9
  "role": "runtime",
10
- "version": "^0.1.9",
10
+ "version": "^0.1.11",
11
11
  "path": "./node_modules/@capyclaw/foundation"
12
12
  },
13
13
  "@capyclaw/plugin-kit": {
14
14
  "role": "support",
15
- "version": "^0.2.6",
15
+ "version": "^0.2.8",
16
16
  "path": "./node_modules/@capyclaw/plugin-kit"
17
17
  },
18
18
  "@capyclaw/service-broker": {
19
19
  "role": "runtime",
20
- "version": "^0.2.25",
20
+ "version": "^0.2.27",
21
21
  "path": "./node_modules/@capyclaw/service-broker",
22
22
  "provides": {
23
23
  "services": [
@@ -31,7 +31,7 @@
31
31
  },
32
32
  "@capyclaw/wall-workbench": {
33
33
  "role": "runtime",
34
- "version": "^0.1.0",
34
+ "version": "^0.1.2",
35
35
  "path": "./node_modules/@capyclaw/wall-workbench",
36
36
  "requires": {
37
37
  "capabilities": {
@@ -47,13 +47,23 @@
47
47
  "capabilities": {
48
48
  "editor.bridge": "1.0.0",
49
49
  "wall.runtime": "1.0.0",
50
- "wall.tools": "1.0.0"
50
+ "wall.tools": "1.0.0",
51
+ "wall.surfaceLifecycle": "1.0.0"
51
52
  }
53
+ },
54
+ "surfaceLifecycle": {
55
+ "hooks": [
56
+ "onVisibility",
57
+ "suspendRender",
58
+ "resumeRender",
59
+ "rehydrate"
60
+ ],
61
+ "headlessObservation": true
52
62
  }
53
63
  },
54
64
  "@capyclaw/service-host": {
55
65
  "role": "provider",
56
- "version": "^0.1.5",
66
+ "version": "^0.1.7",
57
67
  "path": "./node_modules/@capyclaw/service-host",
58
68
  "provides": {
59
69
  "services": [
@@ -66,7 +76,7 @@
66
76
  },
67
77
  "@capyclaw/tool-host": {
68
78
  "role": "provider",
69
- "version": "^0.2.0",
79
+ "version": "^0.3.2",
70
80
  "path": "./node_modules/@capyclaw/tool-host",
71
81
  "provides": {
72
82
  "services": [
@@ -80,7 +90,7 @@
80
90
  },
81
91
  "@capyclaw/asset-service": {
82
92
  "role": "provider",
83
- "version": "^0.1.4",
93
+ "version": "^0.1.6",
84
94
  "path": "./node_modules/@capyclaw/asset-service",
85
95
  "provides": {
86
96
  "services": [
@@ -7,7 +7,7 @@
7
7
  "packages": {
8
8
  "@capyclaw/asset-service": {
9
9
  "role": "provider",
10
- "version": "^0.1.4",
10
+ "version": "^0.1.6",
11
11
  "path": ".",
12
12
  "provides": {
13
13
  "services": [
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capyclaw/asset-service",
3
- "version": "0.1.4",
3
+ "version": "0.1.6",
4
4
  "description": "DSH 公共资产插件:sha256 对象仓库、/assets 路由、base64 上传与 content-type,向插件提供 ctx.assets。",
5
5
  "type": "module",
6
6
  "main": "./lib/host.mjs",
@@ -13,6 +13,7 @@ import fs from 'node:fs'
13
13
  import path from 'node:path'
14
14
  import { send, readBody } from '@capyclaw/plugin-kit/service/http'
15
15
  import { createServiceApi } from '@capyclaw/plugin-kit/service/api'
16
+ import { ASSET_URL_PREFIX } from '@capyclaw/foundation/abi'
16
17
 
17
18
  function sha256(buffer) {
18
19
  return crypto.createHash('sha256').update(buffer).digest('hex')
@@ -40,7 +41,7 @@ export function createWorkspaceAssetStore({ storeClient, workId = 'main' } = {})
40
41
  const id = sha256(buffer)
41
42
  const key = `assets/${id}`
42
43
  storeClient.put(workId, key, buffer, { contentType })
43
- return { id, key, url: `/assets/${id}`, size: buffer.length, contentType }
44
+ return { id, key, url: `${ASSET_URL_PREFIX}${id}`, size: buffer.length, contentType }
44
45
  },
45
46
  get(id) {
46
47
  if (!validId(id)) return null
@@ -84,7 +85,7 @@ export function createFileAssetStore({ dir } = {}) {
84
85
  const metaTmp = `${metaFile}.tmp-${process.pid}-${Date.now()}-${Math.random().toString(16).slice(2)}`
85
86
  fs.writeFileSync(metaTmp, JSON.stringify({ contentType, size: buffer.length }))
86
87
  fs.renameSync(metaTmp, metaFile)
87
- return { id, key: id, url: `/assets/${id}`, size: buffer.length, contentType }
88
+ return { id, key: id, url: `${ASSET_URL_PREFIX}${id}`, size: buffer.length, contentType }
88
89
  },
89
90
  get(id) {
90
91
  if (!validId(id)) return null
@@ -107,16 +108,82 @@ export function createFileAssetStore({ dir } = {}) {
107
108
  if (!validId(id)) return false
108
109
  try { return fs.existsSync(path.join(dir, path.basename(String(id)))) } catch { return false }
109
110
  },
111
+ /** 删除单个对象(data + meta)。内容寻址仓库此前只增不删,磁盘无界增长。 */
112
+ async delete(id) {
113
+ if (!validId(id)) return false
114
+ const file = path.join(dir, path.basename(String(id)))
115
+ let existed = false
116
+ try {
117
+ existed = fs.existsSync(file)
118
+ fs.rmSync(file, { force: true })
119
+ fs.rmSync(file + '.meta.json', { force: true })
120
+ } catch { /* 尽力删除 */ }
121
+ return existed
122
+ },
123
+ /**
124
+ * 垃圾回收:清崩溃残留的孤儿 .tmp、按 mtime 龄淘汰、按总量 LRU 淘汰。
125
+ * keepIds 里的对象(仍被引用)永不删。返回 {removed, freedBytes, orphanTmpRemoved}。
126
+ */
127
+ async gc({ maxAgeMs = 0, maxBytes = 0, keepIds = [] } = {}) {
128
+ const keep = new Set(keepIds)
129
+ const now = Date.now()
130
+ let removed = 0
131
+ let freedBytes = 0
132
+ let orphanTmpRemoved = 0
133
+ let names
134
+ try { names = fs.readdirSync(dir) } catch { return { removed, freedBytes, orphanTmpRemoved } }
135
+ /* 孤儿 tmp:put 中途崩溃留下的半成品,超过 1h 宽限才清(避免误删在途写入)。 */
136
+ for (const name of names) {
137
+ if (!name.includes('.tmp-')) continue
138
+ const full = path.join(dir, name)
139
+ try {
140
+ const st = fs.statSync(full)
141
+ if (now - st.mtimeMs > 3600000) { fs.rmSync(full, { force: true }); orphanTmpRemoved += 1 }
142
+ } catch { /* 忽略 */ }
143
+ }
144
+ const objects = []
145
+ for (const name of fs.readdirSync(dir)) {
146
+ if (!validId(name)) continue
147
+ const full = path.join(dir, name)
148
+ try { const st = fs.statSync(full); objects.push({ id: name, full, size: st.size, mtime: st.mtimeMs }) } catch { /* 忽略 */ }
149
+ }
150
+ const removeObject = (o) => {
151
+ try {
152
+ fs.rmSync(o.full, { force: true })
153
+ fs.rmSync(o.full + '.meta.json', { force: true })
154
+ removed += 1
155
+ freedBytes += o.size
156
+ } catch { /* 忽略 */ }
157
+ }
158
+ if (maxAgeMs > 0) {
159
+ for (const o of objects) {
160
+ if (!keep.has(o.id) && now - o.mtime > maxAgeMs) removeObject(o)
161
+ }
162
+ }
163
+ if (maxBytes > 0) {
164
+ const alive = objects.filter((o) => fs.existsSync(o.full)).sort((a, b) => a.mtime - b.mtime)
165
+ let total = alive.reduce((sum, o) => sum + o.size, 0)
166
+ for (const o of alive) {
167
+ if (total <= maxBytes) break
168
+ if (keep.has(o.id)) continue
169
+ removeObject(o)
170
+ total -= o.size
171
+ }
172
+ }
173
+ return { removed, freedBytes, orphanTmpRemoved }
174
+ },
110
175
  }
111
176
  }
112
177
 
113
- /** 防止 text/html / svg 在 loopback 同源执行脚本;图片仍按原 MIME 内联。 */
178
+ /** 防止 text/html / svg 在 loopback 同源执行脚本;图片仍按原 MIME 内联。
179
+ * 安全类型保留原始参数(charset 等),不再一刀切剥掉——否则文本资产 charset 丢失。 */
114
180
  function safeContentType(type) {
115
- const base = String(type || '').split(';')[0].trim().toLowerCase() || 'application/octet-stream'
181
+ const raw = String(type || '').replace(/[\r\n]/g, '').trim()
182
+ const base = raw.split(';')[0].trim().toLowerCase() || 'application/octet-stream'
116
183
  if (base === 'text/html' || base === 'application/xhtml+xml' || base === 'image/svg+xml') {
117
184
  return 'text/plain; charset=utf-8'
118
185
  }
119
- return base
186
+ return raw || 'application/octet-stream'
120
187
  }
121
188
 
122
189
  /**
@@ -126,7 +193,7 @@ function safeContentType(type) {
126
193
  */
127
194
  export function createAssetRoutes({ assetStore, basePath = '', maxUploadBytes = 10 * 1024 * 1024 } = {}) {
128
195
  const prefix = basePath === '/' ? '' : String(basePath || '').replace(/\/+$/, '')
129
- const getPrefixes = ['/assets/', prefix ? `${prefix}/assets/` : null].filter(Boolean)
196
+ const getPrefixes = [ASSET_URL_PREFIX, prefix ? `${prefix}${ASSET_URL_PREFIX}` : null].filter(Boolean)
130
197
  const postPaths = new Set(['/assets', prefix ? `${prefix}/assets` : null].filter(Boolean))
131
198
  return async function assetRoutes(req, res, u) {
132
199
  const p = u.pathname
@@ -134,7 +201,12 @@ export function createAssetRoutes({ assetStore, basePath = '', maxUploadBytes =
134
201
  const getPrefix = getPrefixes.find((candidate) => p.startsWith(candidate))
135
202
  if (getPrefix) {
136
203
  if (!assetStore) { send(req, res, 404, { error: 'asset store unavailable' }); return true }
137
- const id = decodeURIComponent(p.slice(getPrefix.length))
204
+ let id
205
+ try { id = decodeURIComponent(p.slice(getPrefix.length)) } catch {
206
+ /* 畸形百分号编码(如 /assets/%zz):decodeURIComponent 抛 URIError,
207
+ * 不包会一路冒成 500。这是客户端错误,回 400。 */
208
+ send(req, res, 400, { error: 'malformed asset id encoding' }); return true
209
+ }
138
210
  if (!validId(id)) { send(req, res, 404, { error: 'asset not found' }); return true }
139
211
  const asset = assetStore.get(id)
140
212
  if (!asset) { send(req, res, 404, { error: 'asset not found' }); return true }
@@ -170,7 +242,8 @@ export function createAssetRoutes({ assetStore, basePath = '', maxUploadBytes =
170
242
  }
171
243
  if (!buffer.length) { send(req, res, 400, { error: '图片数据为空' }); return true }
172
244
  const asset = await assetStore.put(buffer, body.contentType || 'application/octet-stream')
173
- send(req, res, 200, { ok: true, ...asset })
245
+ /* 回给与 GET 一致的 sanitized content-type(保留 charset、script 类转 text/plain)。 */
246
+ send(req, res, 200, { ok: true, ...asset, contentType: safeContentType(asset.contentType) })
174
247
  return true
175
248
  }
176
249
  return false
@@ -184,8 +257,14 @@ export function createAssetHost(config = {}) {
184
257
  ? createFileAssetStore({ dir: config.dir })
185
258
  : null
186
259
 
260
+ /* 能力标签如实申报:gc/delete 仅当底层 store 支持时才 advertise(file store 有,
261
+ * workspace store 无),避免「声明满足、活实例缺方法」。 */
262
+ const tags = ['workspace-store', 'file-store', 'routes', 'sha256', 'mount']
263
+ if (typeof defaultStore?.gc === 'function') tags.push('gc')
264
+ if (typeof defaultStore?.delete === 'function') tags.push('delete')
265
+
187
266
  return {
188
- api: createServiceApi('assets', '1.0.0', ['workspace-store', 'file-store', 'routes', 'sha256', 'mount']),
267
+ api: createServiceApi('assets', '1.0.0', tags),
189
268
  version: '0.1.0',
190
269
  createStore: createWorkspaceAssetStore,
191
270
  createFileStore: createFileAssetStore,
@@ -219,5 +298,13 @@ export function createAssetHost(config = {}) {
219
298
  if (!defaultStore) throw new Error('asset-service: 未配置 storeClient/dir')
220
299
  return defaultStore.get(id)
221
300
  },
301
+ async delete(id) {
302
+ if (typeof defaultStore?.delete !== 'function') throw new Error('asset-service: 当前 store 不支持 delete')
303
+ return defaultStore.delete(id)
304
+ },
305
+ async gc(options) {
306
+ if (typeof defaultStore?.gc !== 'function') throw new Error('asset-service: 当前 store 不支持 gc')
307
+ return defaultStore.gc(options)
308
+ },
222
309
  }
223
310
  }
@@ -0,0 +1,26 @@
1
+ /**
2
+ * 资产引用契约的单一真源(公共 ABI 常量)。
3
+ *
4
+ * 互操作标准里资产有两副面孔,前缀在这里定义一次,内核门面与资产服务都引用它,
5
+ * 避免「路由实现」与「URL 门面」各写一份 '/assets/' 而漂移:
6
+ * - 字节仓 HTTP 路由前缀 ASSET_URL_PREFIX = '/assets/' (asset-service 挂 GET/HEAD)
7
+ * - 结构化正文里的引用前缀 ASSET_REF_PREFIX = '@asset/' (work-store 信封/正文只存引用)
8
+ *
9
+ * 这是契约常量,不是宿主服务名——放 ABI 层,应用与驱动都可 import。
10
+ */
11
+
12
+ /** 资产字节仓的 HTTP 路由前缀(asset-service 实现,内核 os.assets.url 复用)。 */
13
+ export const ASSET_URL_PREFIX = '/assets/'
14
+
15
+ /** 结构化正文/产物信封里的内容寻址引用前缀(@asset/<sha256>)。 */
16
+ export const ASSET_REF_PREFIX = '@asset/'
17
+
18
+ /** 由资产 id 生成可加载 URL:/assets/<id>(id 做 URI 编码)。 */
19
+ export function assetUrl(id) {
20
+ return `${ASSET_URL_PREFIX}${encodeURIComponent(String(id))}`
21
+ }
22
+
23
+ /** 由资产 id 生成规范引用:@asset/<id>。 */
24
+ export function assetRef(id) {
25
+ return `${ASSET_REF_PREFIX}${String(id)}`
26
+ }
@@ -14,6 +14,8 @@
14
14
  * doc.model —— 插件提供文档模型(稳定 ID / 清单 / 定位 / 编辑计划 / 校验)
15
15
  */
16
16
 
17
+ import { AbiError, ERROR_CODES } from './errors.mjs'
18
+
17
19
  /** 桥协议版本:端点集合与状态机语义的主版本。不兼容变更才升 major。 */
18
20
  export const BRIDGE_PROTOCOL_VERSION = '1.0.0'
19
21
 
@@ -40,7 +42,7 @@ export function declareBridge(input = {}) {
40
42
  if (!VERSION_RE.test(protocol)) errors.push(`protocol 版本非法:${protocol}`)
41
43
  if (model !== null && !VERSION_RE.test(model)) errors.push(`model 版本非法:${model}`)
42
44
  if (editor !== 'required' && editor !== 'optional') errors.push(`editor 只能是 required/optional:${editor}`)
43
- if (errors.length) throw new Error(`桥声明非法:${errors.join(';')}`)
45
+ if (errors.length) throw new AbiError(ERROR_CODES.E_SCHEMA, `桥声明非法:${errors.join(';')}`, { errors })
44
46
  return Object.freeze({ id, protocol, model, editor })
45
47
  }
46
48
 
@@ -17,6 +17,8 @@ export {
17
17
  defineDriver,
18
18
  rawPlugin,
19
19
  } from './spec.mjs'
20
+ /* createLoader 是宿主/测试侧装配 API(内核 createKernel 与一致性测试用),虽从 abi 导出,
21
+ * 应用插件不应直接调用——应用只用 createPlugin + 接口 builders(见 plugin-abi.md §二)。 */
20
22
  export { createLoader } from './loader.mjs'
21
23
  export {
22
24
  BRIDGE_CAPABILITY,
@@ -29,6 +31,19 @@ export {
29
31
  bridgeConsumerRequirements,
30
32
  declareBridge,
31
33
  } from './bridge.mjs'
34
+ export {
35
+ SURFACE_LIFECYCLE_VERSION,
36
+ SURFACE_LIFECYCLE_CAPABILITY_SUFFIX,
37
+ VISIBILITY_ACK_MESSAGE,
38
+ VISIBILITY_DEFAULTS,
39
+ VISIBILITY_MESSAGE,
40
+ VISIBILITY_STATES,
41
+ assertSurfaceLifecycle,
42
+ isVisibilityState,
43
+ normalizeVisibilityState,
44
+ surfaceLifecycleCapability,
45
+ } from './visibility.mjs'
46
+ export { ASSET_URL_PREFIX, ASSET_REF_PREFIX, assetUrl, assetRef } from './assets.mjs'
32
47
  export { validateInterface } from './validate.mjs'
33
48
  export { AbiError, ERROR_CODES, isAbiError, toAbiError } from './errors.mjs'
34
49
  export { validateSchema } from './schema.mjs'
@@ -2,7 +2,7 @@
2
2
  import { ABI_MAJOR, getInterfaceKind } from './kinds.mjs'
3
3
  import { validateInterface } from './validate.mjs'
4
4
  import { AbiError, ERROR_CODES, toAbiError } from './errors.mjs'
5
- import { satisfies } from './version.mjs'
5
+ import { satisfies, isValidRange } from './version.mjs'
6
6
 
7
7
  const thenable = (value) => value && typeof value.then === 'function'
8
8
 
@@ -164,6 +164,13 @@ export function createLoader({ adapters = {}, capabilities = {}, log = null, abi
164
164
  plugins.delete(spec.name)
165
165
  void previous.handle?.dispose?.()
166
166
  }
167
+ /* 非法范围单独报 E_SCHEMA:旧实现把「range 写错」和「能力真缺」都归成
168
+ * E_CAP_MISSING「能力不可用」,误导排障。先区分两者。 */
169
+ const badRanges = spec.requires.filter((need) => need.name.startsWith('os.') && !isValidRange(need.range))
170
+ if (badRanges.length) {
171
+ throw error(ERROR_CODES.E_SCHEMA, `插件 ${spec.name} 能力范围非法:${badRanges.map((r) => r.raw).join(', ')}`,
172
+ { plugin: spec.name, invalid: badRanges })
173
+ }
167
174
  const missing = spec.requires.filter((need) => need.name.startsWith('os.')
168
175
  && !satisfies(capabilityMap()[need.name] || '', need.range))
169
176
  if (missing.length) {
@@ -191,9 +191,12 @@ export function definePlugin(options = {}) {
191
191
  const requiresEntries = requires.map((entry) => parseCapability(entry, 'requires'))
192
192
  const providesEntries = provides.map((entry) => parseCapability(entry, 'provides'))
193
193
  for (const entry of providesEntries) {
194
- if (/^[\^~<>=]/.test(entry.range)) {
194
+ /* 白名单而非黑名单:provides 必须是完整 x.y.z(可带 prerelease)。旧实现只拒
195
+ * ^~<>= 前缀,'1.x' / '1.*' / 裸 '1' 漏网被当成版本发布,消费方 parseVersion
196
+ * 解析不了 → satisfies 恒 false,能力永远「不可用」。 */
197
+ if (!/^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/.test(entry.range)) {
195
198
  throw new AbiError(ERROR_CODES.E_PLUGIN_SHAPE,
196
- `provides 必须写精确版本,不能写范围:${entry.raw}`, { entry: entry.raw })
199
+ `provides 必须写精确版本(x.y.z),不能写范围:${entry.raw}`, { entry: entry.raw })
197
200
  }
198
201
  }
199
202
 
@@ -2,25 +2,57 @@
2
2
  * 版本与范围:能力声明用 `^1` / `^1.2.0` / `>=1.2` / `1.x` / 精确版本。
3
3
  *
4
4
  * 只实现这份 ABI 需要的语义,不引入 semver 依赖:
5
- * - `^1.2.3` → >=1.2.3 且同主版本;`^0.2.3` → >=0.2.3 且同次版本(0.x 约定)
5
+ * - `^1.2.3` → >=1.2.3 且同主版本;`^0.2.3` → >=0.2.3 且同次版本;
6
+ * `^0.0.3` → >=0.0.3 且同补丁(0.0.x 约定,semver 标准)
6
7
  * - `~1.2.3` → >=1.2.3 且同次版本
7
8
  * - `1.x` / `1.*` → 同主版本
8
9
  * - `>=` `>` `<=` `<` `=` 前缀
9
10
  * - 裸版本 → 精确匹配(比较时忽略末位 0)
11
+ * - prerelease 保留并参与比较:1.0.0-beta < 1.0.0;带 prerelease 的版本只满足
12
+ * 同 [major,minor,patch] 且同样带 prerelease 的范围(semver 标准,避免预发布
13
+ * 版本被当成正式版本放行)。
10
14
  */
11
15
 
12
16
  export function parseVersion(text) {
13
17
  if (typeof text !== 'string') return null
14
- const match = /^v?(\d+)(?:\.(\d+))?(?:\.(\d+))?(?:-[0-9A-Za-z.-]+)?$/.exec(text.trim())
18
+ const match = /^v?(\d+)(?:\.(\d+))?(?:\.(\d+))?(?:-([0-9A-Za-z.-]+))?$/.exec(text.trim())
15
19
  if (!match) return null
16
- return [Number(match[1]), Number(match[2] || 0), Number(match[3] || 0)]
20
+ const parts = [Number(match[1]), Number(match[2] || 0), Number(match[3] || 0)]
21
+ parts.prerelease = match[4] || null
22
+ return parts
23
+ }
24
+
25
+ function comparePrerelease(a, b) {
26
+ /* 无 prerelease > 有 prerelease(1.0.0 > 1.0.0-beta)。 */
27
+ if (!a && !b) return 0
28
+ if (!a) return 1
29
+ if (!b) return -1
30
+ const left = a.split('.')
31
+ const right = b.split('.')
32
+ for (let i = 0; i < Math.max(left.length, right.length); i++) {
33
+ const x = left[i]
34
+ const y = right[i]
35
+ if (x === undefined) return -1
36
+ if (y === undefined) return 1
37
+ const xn = /^\d+$/.test(x)
38
+ const yn = /^\d+$/.test(y)
39
+ if (xn && yn) {
40
+ const d = Number(x) - Number(y)
41
+ if (d !== 0) return d < 0 ? -1 : 1
42
+ } else if (xn !== yn) {
43
+ return xn ? -1 : 1 // 数字标识符 < 字母数字标识符
44
+ } else if (x !== y) {
45
+ return x < y ? -1 : 1
46
+ }
47
+ }
48
+ return 0
17
49
  }
18
50
 
19
51
  function compareParts(a, b) {
20
52
  for (let i = 0; i < 3; i++) {
21
53
  if (a[i] !== b[i]) return a[i] < b[i] ? -1 : 1
22
54
  }
23
- return 0
55
+ return comparePrerelease(a.prerelease || null, b.prerelease || null)
24
56
  }
25
57
 
26
58
  export function compareVersions(a, b) {
@@ -44,12 +76,23 @@ export function satisfies(version, range) {
44
76
  if (raw.startsWith('^')) {
45
77
  const base = parseVersion(raw.slice(1))
46
78
  if (!base) return false
79
+ /* prerelease 只满足同 [major,minor,patch] 且带 prerelease 的范围(semver 标准),
80
+ * 避免 1.1.0-beta 被 ^1.0.0 当正式版本放行。 */
81
+ if (current.prerelease && (!base.prerelease
82
+ || current[0] !== base[0] || current[1] !== base[1] || current[2] !== base[2])) return false
47
83
  if (compareParts(current, base) < 0) return false
48
- return base[0] === 0 ? current[0] === 0 && current[1] === base[1] : current[0] === base[0]
84
+ if (base[0] === 0) {
85
+ /* 0.0.x caret 收紧到补丁位:^0.0.3 只容 0.0.3,不容 0.0.9。 */
86
+ if (base[1] === 0) return current[0] === 0 && current[1] === 0 && current[2] === base[2]
87
+ return current[0] === 0 && current[1] === base[1]
88
+ }
89
+ return current[0] === base[0]
49
90
  }
50
91
  if (raw.startsWith('~')) {
51
92
  const base = parseVersion(raw.slice(1))
52
93
  if (!base) return false
94
+ if (current.prerelease && (!base.prerelease
95
+ || current[0] !== base[0] || current[1] !== base[1] || current[2] !== base[2])) return false
53
96
  return compareParts(current, base) >= 0 && current[0] === base[0] && current[1] === base[1]
54
97
  }
55
98
  if (raw.startsWith('>=')) {
@@ -80,3 +123,18 @@ export function satisfies(version, range) {
80
123
  const exact = parseVersion(raw)
81
124
  return Boolean(exact) && compareParts(current, exact) === 0
82
125
  }
126
+
127
+ /**
128
+ * 范围是否「语义合法」(区别于「合法但不满足」)。供加载器把非法范围单独报
129
+ * E_SCHEMA,而不是误归因成「能力不可用」误导排障。空范围视为「任意」,合法。
130
+ */
131
+ export function isValidRange(range) {
132
+ const raw = String(range ?? '').trim()
133
+ if (!raw || raw === '*' || raw === 'x') return true
134
+ if (/^[\^~]/.test(raw)) return Boolean(parseVersion(raw.slice(1)))
135
+ if (/^(>=|<=)/.test(raw)) return Boolean(parseVersion(raw.slice(2)))
136
+ if (/^[><]/.test(raw)) return Boolean(parseVersion(raw.slice(1)))
137
+ if (raw.startsWith('=')) return isValidRange(raw.slice(1))
138
+ if (/^(\d+)(?:\.(\d+))?\.[x*]$/.test(raw)) return true
139
+ return Boolean(parseVersion(raw))
140
+ }
@@ -0,0 +1,90 @@
1
+ /**
2
+ * 表面生命周期(surface lifecycle)的标准声明。
3
+ *
4
+ * 与 bridge.mjs 同层:只放「协议常量 + 声明校验」,不放运行时。
5
+ * 依据 docs/SURFACE-LIFECYCLE-STANDARD.md:
6
+ * - 宿主(portal-hud + window-kit)是表面可见性的唯一真源,经统一 postMessage
7
+ * 信号通道下发给 iframe;iframe 内 document.hidden 只反映顶层 tab,不能用来
8
+ * 推断「被宿主藏起来」,所以表面可见性必须宿主显式告知。
9
+ * - 只有渲染面响应可见性;权威/数据面与桥接/仲裁面(租约心跳、apply/ack)
10
+ * 永远在线,与可见性无关。
11
+ *
12
+ * 消费方:
13
+ * - 宿主侧:@capyclaw/window-kit 的 visibility 驱动器(CJS 自包含叶子,无法
14
+ * import 本 ESM 模块,内联同名常量,由 parity 测试钉死一致)。
15
+ * - iframe 侧:@capyclaw/document-workbench/client 的 visibility 订阅器,
16
+ * 以及各编辑器前端(ppt/canvas/browser)。
17
+ * - 注册面:suite 清单 provides.capabilities 的 <ns>.surfaceLifecycle。
18
+ */
19
+
20
+ import { AbiError, ERROR_CODES } from './errors.mjs'
21
+
22
+ /** 表面生命周期契约版本:状态机语义与信号形状的主版本。不兼容变更才升 major。 */
23
+ export const SURFACE_LIFECYCLE_VERSION = '1.0.0'
24
+
25
+ /** 宿主 → iframe 的可见性信号:{ type, state, sid },targetOrigin 同源。 */
26
+ export const VISIBILITY_MESSAGE = 'capyclaw:workbench:visibility'
27
+
28
+ /** iframe → 宿主 的回执:{ type, state, sid }。detached 由宿主直接卸载,无需回执。 */
29
+ export const VISIBILITY_ACK_MESSAGE = 'capyclaw:workbench:visibility-ack'
30
+
31
+ /**
32
+ * 可见性状态(标准 §3):
33
+ * foreground 前台可见全速;background 可见非焦点(降速,SHOULD 档);
34
+ * hidden 不再绘制(渲染面必须挂起);detached 久藏卸载(影子编辑器接管)。
35
+ */
36
+ export const VISIBILITY_STATES = Object.freeze(['foreground', 'background', 'hidden', 'detached'])
37
+
38
+ /**
39
+ * 节奏/预算默认值(标准 §3/§4/§5)。运行时经 tuning 单一真源可调;
40
+ * 这里是协议默认,不是各插件私设的第二份。
41
+ * detachAfterMs hidden 持续多久才卸载 iframe(编辑器权威型可更长)
42
+ * visibilityAckTimeoutMs 信号发出后多久没回执算半挂起(宿主可强制重载)
43
+ * hiddenGraceMs shown→hidden 的去抖宽限,避免快速切换抖动
44
+ */
45
+ export const VISIBILITY_DEFAULTS = Object.freeze({
46
+ detachAfterMs: 120000,
47
+ visibilityAckTimeoutMs: 1500,
48
+ hiddenGraceMs: 250,
49
+ })
50
+
51
+ /** 能力名后缀:与 suite 清单 provides.capabilities 的 <ns>.surfaceLifecycle 对应。 */
52
+ export const SURFACE_LIFECYCLE_CAPABILITY_SUFFIX = 'surfaceLifecycle'
53
+
54
+ const STATE_SET = new Set(VISIBILITY_STATES)
55
+
56
+ /** 是否合法状态。 */
57
+ export function isVisibilityState(value) {
58
+ return typeof value === 'string' && STATE_SET.has(value)
59
+ }
60
+
61
+ /** 归一化:非法/缺省一律退回 foreground(宁可多渲染,不可假挂起)。 */
62
+ export function normalizeVisibilityState(value) {
63
+ return isVisibilityState(value) ? value : 'foreground'
64
+ }
65
+
66
+ /** 注册声明 → suite 清单 provides.capabilities(标准 §6)。 */
67
+ export function surfaceLifecycleCapability(ns, version = SURFACE_LIFECYCLE_VERSION) {
68
+ if (typeof ns !== 'string' || !/^[a-z0-9][a-z0-9-]*$/.test(ns)) {
69
+ throw new AbiError(ERROR_CODES.E_SCHEMA, `surfaceLifecycle 命名空间必须是 kebab-case:${ns}`)
70
+ }
71
+ return Object.freeze({ [`${ns}.${SURFACE_LIFECYCLE_CAPABILITY_SUFFIX}`]: version })
72
+ }
73
+
74
+ /**
75
+ * 校验一份表面生命周期声明(注册契约,标准 §6)。
76
+ * 渲染面四钩子必须齐;editor-gated 观察必须声明无头兜底。
77
+ * @param {{ hooks?: string[], headlessObservation?: boolean }} input
78
+ */
79
+ export function assertSurfaceLifecycle(input = {}) {
80
+ const required = ['onVisibility', 'suspendRender', 'resumeRender', 'rehydrate']
81
+ const hooks = Array.isArray(input.hooks) ? input.hooks : []
82
+ const issues = []
83
+ for (const hook of required) {
84
+ if (!hooks.includes(hook)) issues.push(`缺少渲染面钩子 ${hook}`)
85
+ }
86
+ if (input.headlessObservation !== true) {
87
+ issues.push('editor-gated 观察必须声明无头兜底(headlessObservation:true)')
88
+ }
89
+ return { ok: issues.length === 0, issues }
90
+ }