@capyclaw/ppt-suite 2.5.70 → 2.5.72

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 (109) hide show
  1. package/composition.lock.json +27 -27
  2. package/contract/ppt-contract.json +1 -1
  3. package/dsh-suite.json +27 -17
  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 +4 -3
  7. package/node_modules/@capyclaw/document-workbench/package.json +6 -3
  8. package/node_modules/@capyclaw/document-workbench/src/client/index.mjs +56 -3
  9. package/node_modules/@capyclaw/document-workbench/src/client/visibility.mjs +112 -0
  10. package/node_modules/@capyclaw/document-workbench/src/model/manifest.mjs +6 -4
  11. package/node_modules/@capyclaw/document-workbench/src/model/ops.mjs +7 -2
  12. package/node_modules/@capyclaw/document-workbench/src/session/index.mjs +8 -0
  13. package/node_modules/@capyclaw/document-workbench/src/store/file-backend.mjs +17 -10
  14. package/node_modules/@capyclaw/document-workbench/src/store/projection.mjs +39 -0
  15. package/node_modules/@capyclaw/document-workbench/src/testing/index.mjs +14 -11
  16. package/node_modules/@capyclaw/editor-host/dsh-suite.json +1 -1
  17. package/node_modules/@capyclaw/editor-host/package.json +1 -1
  18. package/node_modules/@capyclaw/editor-host/src/index.mjs +12 -21
  19. package/node_modules/@capyclaw/foundation/abi/assets.mjs +26 -0
  20. package/node_modules/@capyclaw/foundation/abi/bridge.mjs +3 -1
  21. package/node_modules/@capyclaw/foundation/abi/index.mjs +15 -0
  22. package/node_modules/@capyclaw/foundation/abi/visibility.mjs +90 -0
  23. package/node_modules/@capyclaw/foundation/lib/adapters/lifecycle-hook.mjs +21 -4
  24. package/node_modules/@capyclaw/foundation/lib/host/caps.mjs +5 -1
  25. package/node_modules/@capyclaw/foundation/lib/host/contract.mjs +5 -2
  26. package/node_modules/@capyclaw/foundation/lib/host/dsh.mjs +4 -0
  27. package/node_modules/@capyclaw/foundation/lib/kernel.mjs +13 -3
  28. package/node_modules/@capyclaw/foundation/package.json +1 -1
  29. package/node_modules/@capyclaw/plugin-kit/package.json +4 -1
  30. package/node_modules/@capyclaw/plugin-kit/src/editor/heartbeat-worker.js +5 -2
  31. package/node_modules/@capyclaw/plugin-kit/src/editor/heartbeat.mjs +26 -0
  32. package/node_modules/@capyclaw/plugin-kit/src/ledger.mjs +151 -0
  33. package/node_modules/@capyclaw/plugin-kit/src/service/static.mjs +79 -0
  34. package/node_modules/@capyclaw/plugin-kit/src/workspace/manager.mjs +13 -5
  35. package/node_modules/@capyclaw/portal/package.json +1 -1
  36. package/node_modules/@capyclaw/ppt-editor/app.js +458 -10
  37. package/node_modules/@capyclaw/ppt-editor/contract/ppt-contract.json +1 -1
  38. package/node_modules/@capyclaw/ppt-editor/heartbeat-worker.js +5 -2
  39. package/node_modules/@capyclaw/ppt-editor/index.html +4 -0
  40. package/node_modules/@capyclaw/ppt-editor/package.json +2 -2
  41. package/node_modules/@capyclaw/ppt-skill-api/contract/ppt-contract.json +1 -1
  42. package/node_modules/@capyclaw/ppt-skill-api/package.json +1 -1
  43. package/node_modules/@capyclaw/ppt-skill-api/skills/pptist-api/references/tool-contract.md +1 -1
  44. package/node_modules/@capyclaw/ppt-skill-proposal/contract/ppt-contract.json +1 -1
  45. package/node_modules/@capyclaw/ppt-skill-proposal/package.json +1 -1
  46. package/node_modules/@capyclaw/ppt-workbench/contract/ppt-contract.json +1 -1
  47. package/node_modules/@capyclaw/ppt-workbench/core/deck-layout.js +3 -1
  48. package/node_modules/@capyclaw/ppt-workbench/core/ops/slide.js +9 -3
  49. package/node_modules/@capyclaw/ppt-workbench/core/text.js +14 -2
  50. package/node_modules/@capyclaw/ppt-workbench/package.json +10 -8
  51. package/node_modules/@capyclaw/ppt-workbench/plugin.mjs +13 -1
  52. package/node_modules/@capyclaw/ppt-workbench/server/contract.mjs +1 -1
  53. package/node_modules/@capyclaw/ppt-workbench/server/embedded-host.mjs +200 -29
  54. package/node_modules/@capyclaw/ppt-workbench/server/relay.mjs +10 -7
  55. package/node_modules/@capyclaw/ppt-workbench/server/shared/SHARED_MANIFEST.json +4 -4
  56. package/node_modules/@capyclaw/ppt-workbench/server/store/backends/file-backend.mjs +3 -0
  57. package/node_modules/@capyclaw/ppt-workbench/server/store/checkpoints.mjs +4 -1
  58. package/node_modules/@capyclaw/ppt-workbench/server/store/index.mjs +4 -0
  59. package/node_modules/@capyclaw/ppt-workbench/server/store/sessions.mjs +139 -4
  60. package/node_modules/@capyclaw/ppt-workbench/server/store/shape.mjs +37 -0
  61. package/node_modules/@capyclaw/service-broker/README.md +1 -1
  62. package/node_modules/@capyclaw/service-broker/lib/host.mjs +11 -6
  63. package/node_modules/@capyclaw/service-broker/lib/skills-client.mjs +13 -5
  64. package/node_modules/@capyclaw/service-broker/package.json +1 -1
  65. package/node_modules/@capyclaw/service-host/dsh-suite.json +1 -1
  66. package/node_modules/@capyclaw/service-host/package.json +2 -2
  67. package/node_modules/@capyclaw/service-host/src/index.mjs +45 -27
  68. package/node_modules/@capyclaw/skills-service/README.md +42 -10
  69. package/node_modules/@capyclaw/skills-service/package.json +6 -3
  70. package/node_modules/@capyclaw/skills-service/server.mjs +129 -51
  71. package/node_modules/@capyclaw/suite-kit/README.md +289 -0
  72. package/node_modules/@capyclaw/suite-kit/bin/dsh-domain.mjs +98 -0
  73. package/node_modules/@capyclaw/suite-kit/bin/dsh-suite.mjs +174 -0
  74. package/node_modules/@capyclaw/suite-kit/client/embed.js +35 -0
  75. package/node_modules/@capyclaw/suite-kit/domain.schema.json +129 -0
  76. package/node_modules/@capyclaw/suite-kit/package.json +58 -0
  77. package/node_modules/@capyclaw/suite-kit/src/activation-registry.mjs +184 -0
  78. package/node_modules/@capyclaw/suite-kit/src/artifacts.mjs +131 -0
  79. package/node_modules/@capyclaw/suite-kit/src/capability.mjs +181 -0
  80. package/node_modules/@capyclaw/suite-kit/src/compose.mjs +201 -0
  81. package/node_modules/@capyclaw/suite-kit/src/domain.mjs +138 -0
  82. package/node_modules/@capyclaw/suite-kit/src/embed.mjs +81 -0
  83. package/node_modules/@capyclaw/suite-kit/src/host.mjs +251 -0
  84. package/node_modules/@capyclaw/suite-kit/src/index.mjs +24 -0
  85. package/node_modules/@capyclaw/suite-kit/src/manifest.mjs +263 -0
  86. package/node_modules/@capyclaw/suite-kit/src/orchestration.mjs +100 -0
  87. package/node_modules/@capyclaw/suite-kit/src/profile.mjs +584 -0
  88. package/node_modules/@capyclaw/suite-kit/src/surface.mjs +28 -0
  89. package/node_modules/@capyclaw/suite-kit/suite.schema.json +128 -0
  90. package/node_modules/@capyclaw/tool-host/dsh-suite.json +1 -1
  91. package/node_modules/@capyclaw/tool-host/lib/host.mjs +15 -4
  92. package/node_modules/@capyclaw/tool-host/package.json +1 -1
  93. package/node_modules/@capyclaw/tool-host/src/index.mjs +19 -5
  94. package/node_modules/@capyclaw/tool-host/src/mode-gate.mjs +71 -3
  95. package/node_modules/@capyclaw/work-store/README.md +83 -0
  96. package/node_modules/@capyclaw/work-store/STANDARD.md +263 -0
  97. package/node_modules/@capyclaw/work-store/cordis.patch.yml +3 -0
  98. package/node_modules/@capyclaw/work-store/dsh-suite.json +33 -0
  99. package/node_modules/@capyclaw/work-store/icon.svg +7 -0
  100. package/node_modules/@capyclaw/work-store/lib/host.mjs +21 -0
  101. package/node_modules/@capyclaw/work-store/package.json +68 -0
  102. package/node_modules/@capyclaw/work-store/src/backend.mjs +90 -0
  103. package/node_modules/@capyclaw/work-store/src/db.mjs +199 -0
  104. package/node_modules/@capyclaw/work-store/src/envelope.mjs +132 -0
  105. package/node_modules/@capyclaw/work-store/src/index.mjs +571 -0
  106. package/node_modules/@capyclaw/work-store/src/interop.mjs +70 -0
  107. package/node_modules/@capyclaw/work-store/src/refs.mjs +99 -0
  108. package/node_modules/@capyclaw/work-store/src/routes.mjs +135 -0
  109. package/package.json +16 -14
@@ -32,11 +32,20 @@ import fs from 'node:fs'
32
32
  import os from 'node:os'
33
33
  import path from 'node:path'
34
34
  import { BridgeStore } from './index.mjs'
35
+ import { projectPptCheckpoint } from './shape.mjs'
35
36
  import { currentSession, sessionContext } from '../session-context.mjs'
37
+ import { isOwnerLive, isShadowOwner } from '@capyclaw/document-workbench/session'
36
38
 
37
39
  export const DEFAULT_SESSION = 'default'
38
40
  export const DECK_FILE = 'deck.json'
39
41
 
42
+ /* 空闲 TTL 驱逐:store 常驻整份内存 deck(含 base64 图时可能很大),旧实现缓存只增不减,
43
+ * 内存随会话数无界。这里周期性驱逐「空闲足够久」的实例,但驱逐前排空写队列并 fsync 落盘,
44
+ * 驱逐后保留 metadata-only 快照,让 sessions() 列表与影子对账仍看得到该会话(不复活实例)。
45
+ * 活跃保护:有 pending 写或挂着长轮询 watcher 的实例永不驱逐;单实例由「flush 后才删」保证。 */
46
+ export const IDLE_TTL_MS = 5 * 60 * 1000
47
+ export const EVICT_INTERVAL_MS = 60 * 1000
48
+
40
49
  /** 会话目录的根:DSH 里是 `$DSH_HOME/ppt-sessions`,独立运行是 `<hostDir>/sessions`。 */
41
50
  export function sessionsHomeFor({ hostDir, embedded = false, env = process.env } = {}) {
42
51
  const explicit = String(env.PPT_SESSIONS_HOME || '').trim()
@@ -78,7 +87,8 @@ export function scopedWorkspaceClient(client, prefix) {
78
87
  }
79
88
 
80
89
  export class SessionStores {
81
- constructor({ hostDir, backend = null, sessionsHome = '', getWorkStore = null } = {}) {
90
+ constructor({ hostDir, backend = null, sessionsHome = '', getWorkStore = null,
91
+ idleTtlMs = IDLE_TTL_MS, evictIntervalMs = EVICT_INTERVAL_MS, autoEvict = true } = {}) {
82
92
  if (!hostDir) throw new Error('SessionStores 需要 hostDir')
83
93
  this.hostDir = hostDir
84
94
  /* 真机形态下 DSH 会塞一个 workspace 后端进来。它现在只当「旧数据的来源」用:
@@ -98,6 +108,21 @@ export class SessionStores {
98
108
  this.getWorkStore = getWorkStore
99
109
  this._workStore = null
100
110
  this.sanitizer = null
111
+ /* 空闲驱逐跟踪:_lastUsed 记每键最近一次 forSession 访问;_known 是 metadata-only 注册表
112
+ * (永不缩,sessions() 由它派生);_meta 存被驱逐会话的轻量快照(内容计数 + owner),
113
+ * 供影子对账在不复活实例的前提下仍能看到该会话。 */
114
+ this.idleTtlMs = Number.isFinite(idleTtlMs) ? idleTtlMs : IDLE_TTL_MS
115
+ this.evictIntervalMs = Number.isFinite(evictIntervalMs) ? evictIntervalMs : EVICT_INTERVAL_MS
116
+ this._lastUsed = new Map()
117
+ this._known = new Set()
118
+ this._meta = new Map()
119
+ this._evicting = false
120
+ this._disposed = false
121
+ this._evictTimer = null
122
+ if (autoEvict && this.idleTtlMs > 0 && this.evictIntervalMs > 0) {
123
+ this._evictTimer = setInterval(() => { this.evictIdleNow().catch(() => {}) }, this.evictIntervalMs)
124
+ this._evictTimer.unref?.()
125
+ }
101
126
  }
102
127
 
103
128
  /** relay prepare 钩子在 createStore 之后、首个请求之前塞入懒解析器。 */
@@ -105,7 +130,9 @@ export class SessionStores {
105
130
  this.getWorkStore = getter || null
106
131
  this._workStore = null
107
132
  /* 解析器换了,旧句柄绑定的每会话 store 一律作废,下次访问重建。 */
133
+ this._snapshotAllToMeta()
108
134
  this.cache.clear()
135
+ this._lastUsed.clear()
109
136
  }
110
137
 
111
138
  /** 落盘净化器(base64 闸):由 relay prepare 注入,绑定 assetStore + urlBase。 */
@@ -132,14 +159,17 @@ export class SessionStores {
132
159
  } catch { /* 解析失败就当没有底座,降级文件 */ }
133
160
  if (ws && ws !== this._workStore) {
134
161
  this._workStore = ws
162
+ this._snapshotAllToMeta()
135
163
  this.cache.clear()
164
+ this._lastUsed.clear()
136
165
  }
137
166
  return this._workStore || null
138
167
  }
139
168
 
140
- /** 已经出现过的会话(不含 fallback)。 */
169
+ /** 已经出现过的会话(不含 fallback)。由 _known 注册表派生:实例被空闲驱逐后
170
+ * 会话仍列得出(metadata-only),不会从列表里静默消失。 */
141
171
  sessions() {
142
- return [...this.cache.keys()].filter((key) => key !== DEFAULT_SESSION)
172
+ return [...this._known].filter((key) => key !== DEFAULT_SESSION)
143
173
  }
144
174
 
145
175
  /** 拆库前那份全局稿子归了哪个会话(没迁过就是空串)。 */
@@ -174,7 +204,11 @@ export class SessionStores {
174
204
  if (!store) {
175
205
  store = this.create(key)
176
206
  this.cache.set(key, store)
207
+ /* 活实例接管:丢弃驱逐期的元数据快照,对账改读实时 doc。 */
208
+ this._meta.delete(key)
177
209
  }
210
+ this._known.add(key)
211
+ this._lastUsed.set(key, Date.now())
178
212
  return store
179
213
  }
180
214
 
@@ -191,7 +225,7 @@ export class SessionStores {
191
225
  /* 底座形态:deck 落 SQLite 作品库(自然键 ppt:deck:<会话id>)。
192
226
  * 旧文件数据在首次访问时一次性导入(只读种子,不覆盖已有库数据),
193
227
  * 旧 deck.json 保留作备份。文件专属的崩溃安全由 SQLite WAL 接管。 */
194
- const backend = ws.backendFor({ suite: 'ppt', kind: 'deck', sessionId: key, transform: this.sanitizer })
228
+ const backend = ws.backendFor({ suite: 'ppt', kind: 'deck', sessionId: key, transform: this.sanitizer, project: projectPptCheckpoint })
195
229
  this.seedIntoDb(key, file, backend)
196
230
  return new BridgeStore({ backend })
197
231
  }
@@ -257,6 +291,107 @@ export class SessionStores {
257
291
  current() {
258
292
  return this.forSession(this.currentKey())
259
293
  }
294
+
295
+ /* ---- 空闲 TTL 驱逐 ---- */
296
+
297
+ /** 是否挂着长轮询 watcher(有则永不驱逐,避免 watcher 成孤儿/收到旧实例通知)。 */
298
+ _hasWaiters(store) {
299
+ return !!(store && store._waiters && store._waiters.size > 0)
300
+ }
301
+
302
+ /** 驱逐/清缓存时的轻量元数据快照:只留内容计数 + owner,释放整份内存 deck。 */
303
+ _metaOf(doc) {
304
+ return {
305
+ contentCount: (doc && doc.deck && Array.isArray(doc.deck.slides)) ? doc.deck.slides.length : 0,
306
+ owner: doc && doc.owner ? { ...doc.owner } : null,
307
+ }
308
+ }
309
+
310
+ /** 底座重载清缓存前,把活实例快照成元数据,保证对账在重建前仍看得到每个会话。 */
311
+ _snapshotAllToMeta() {
312
+ for (const [key, store] of this.cache) {
313
+ try { this._meta.set(key, this._metaOf(store.get())) } catch { /* ignore */ }
314
+ }
315
+ }
316
+
317
+ /** 影子对账投影:不复活实例、不刷新 idle 钟。活实例从实时 doc 算;被驱逐会话从
318
+ * 元数据快照算(owner 存活按时间现算,随 TTL 自然 stale)。无该会话则回 null。 */
319
+ reconcileInfo(sid) {
320
+ const key = normalizeSessionId(sid) || DEFAULT_SESSION
321
+ const store = this.cache.get(key)
322
+ if (store) {
323
+ const doc = store.get()
324
+ return {
325
+ hasContent: !!(doc && doc.deck && Array.isArray(doc.deck.slides) && doc.deck.slides.length),
326
+ ownerLive: isOwnerLive(doc),
327
+ ownerShadow: isShadowOwner(doc),
328
+ }
329
+ }
330
+ const meta = this._meta.get(key)
331
+ if (!meta) return null
332
+ const ownerDoc = { owner: meta.owner }
333
+ return {
334
+ hasContent: meta.contentCount > 0,
335
+ ownerLive: isOwnerLive(ownerDoc),
336
+ ownerShadow: isShadowOwner(ownerDoc),
337
+ }
338
+ }
339
+
340
+ /** 驱逐单键:排空写队列 → 重验 → 快照元数据 → fsync 落盘 → 才从缓存移除。
341
+ * store 全程留在 cache,forSession 不会建第二实例 → 同一 id 任何时刻只有一个活实例。 */
342
+ async _evict(key) {
343
+ const store = this.cache.get(key)
344
+ if (!store) return false
345
+ if (this._hasWaiters(store)) return false
346
+ /* 排空串行写队列:基类 _tail 恒 resolve(错误被吞),循环等到链尾不再增长。
347
+ * 有界轮数:期间持续被写说明会话活跃,放弃本轮驱逐(下一轮再试)。 */
348
+ let drained = false
349
+ for (let pass = 0; pass < 8; pass += 1) {
350
+ const tail = store._tail
351
+ await tail
352
+ if (store._tail === tail) { drained = true; break }
353
+ }
354
+ if (!drained) return false
355
+ /* await 之后重新校验:实例没被换、没挂上 watch、确实空闲够久(期间被复用则放弃)。 */
356
+ if (this.cache.get(key) !== store) return false
357
+ if (this._hasWaiters(store)) return false
358
+ const last = this._lastUsed.get(key) || 0
359
+ if (Date.now() - last < this.idleTtlMs) return false
360
+ this._meta.set(key, this._metaOf(store.get()))
361
+ /* fsync 落盘(文件后端有 flush();SQLite 底座 WAL 已持久,无 flush 则跳过)。 */
362
+ try { if (typeof store.backend?.flush === 'function') store.backend.flush() } catch { /* fsync 失败不阻塞驱逐 */ }
363
+ this.cache.delete(key)
364
+ this._lastUsed.delete(key)
365
+ return true
366
+ }
367
+
368
+ /** 一轮驱逐巡检(重入守卫)。定时器周期驱动;测试可手动调用。返回驱逐数量。 */
369
+ async evictIdleNow() {
370
+ if (this._evicting || this._disposed) return 0
371
+ this._evicting = true
372
+ let removed = 0
373
+ try {
374
+ const now = Date.now()
375
+ const candidates = []
376
+ for (const [key, store] of this.cache) {
377
+ if (this._hasWaiters(store)) continue
378
+ const last = this._lastUsed.get(key) || 0
379
+ if (now - last >= this.idleTtlMs) candidates.push(key)
380
+ }
381
+ for (const key of candidates) {
382
+ if (await this._evict(key)) removed += 1
383
+ }
384
+ } finally {
385
+ this._evicting = false
386
+ }
387
+ return removed
388
+ }
389
+
390
+ /** 卸载:清驱逐定时器(relay beforeStop 调用)。不影响已落盘数据。 */
391
+ dispose() {
392
+ this._disposed = true
393
+ if (this._evictTimer) { clearInterval(this._evictTimer); this._evictTimer = null }
394
+ }
260
395
  }
261
396
 
262
397
  function readJson(file) {
@@ -0,0 +1,37 @@
1
+ /**
2
+ * PPT 存储适配层的形状声明(M5 / checkpoint 投影通用化)。
3
+ *
4
+ * 内核(document-workbench / work-store)不再硬编码 deck.slides:
5
+ * PPT 的页解释与 checkpoint 投影在这里显式声明,再传给内核——
6
+ * - PPT_SHAPE:写入 checkpoint payload 的形状标签(新格式);旧 payload 无标签,
7
+ * 投影按 deck 键兜底,保证旧 checkpoint 仍可读。
8
+ * - deckPages:PPT 页解释(模型侧 schema.pages 同语义,mcp 层 deck-schema.mjs 是模型入口)。
9
+ * - projectPptCheckpoint:checkpoint 列表投影 = 会话协议字段 + PPT 业务字段(title/slides),
10
+ * 兼容三种历史形状:
11
+ * 1) 新:payload.slides 为数字(summary 写入)+ payload.shape 标签;
12
+ * 2) 旧:仅 payload.deck.slides 数组(无顶层 slides);
13
+ * 3) 更旧:payload.slides 本身是数组。
14
+ */
15
+ import { sessionCheckpointFields } from '@capyclaw/document-workbench/store/projection'
16
+
17
+ export const PPT_SHAPE = 'ppt.deck'
18
+
19
+ /** PPT 页解释:接受 deck 本体({slides})或会话信封({deck:{slides}})。 */
20
+ export function deckPages(doc) {
21
+ const deck = doc && Array.isArray(doc.slides) ? doc : (doc && doc.deck)
22
+ return deck && Array.isArray(deck.slides) ? deck.slides : []
23
+ }
24
+
25
+ /** checkpoint 列表投影:传给 backendFor / FileStateBackend 的 project(形状声明)。 */
26
+ export function projectPptCheckpoint(payload) {
27
+ const p = payload && typeof payload === 'object' ? payload : {}
28
+ const out = sessionCheckpointFields(p)
29
+ const deck = p.deck && typeof p.deck === 'object' ? p.deck : null
30
+ const title = p.title || (deck && deck.title) || ''
31
+ if (title) out.title = title
32
+ if (typeof p.slides === 'number') out.slides = p.slides | 0
33
+ else if (Array.isArray(p.slides)) out.slides = p.slides.length
34
+ else if (deck && Array.isArray(deck.slides)) out.slides = deck.slides.length
35
+ else out.slides = 0
36
+ return out
37
+ }
@@ -45,7 +45,7 @@ cordis insert 唯一一行(`cordis.patch.yml`),config:
45
45
  controlPort: 0 # >0 时embedded 也开控制端点
46
46
  stopOnUnload: false # true=插件卸载时全停(默认服务独立存活)
47
47
  skills:
48
- url: "http://127.0.0.1:<真实端口>" # 可选:ctx.skills 瘦客户端数据源(从 discovery 读回)
48
+ url: "http://127.0.0.1:<真实端口>" # 可选 standalone 模式:ctx.skillsHttp 瘦客户端数据源(默认不启用;技能标准路径是 skillProvider@1 进程内注册,不占用原生 ctx.skills)
49
49
  ```
50
50
 
51
51
  ## 服务规格 spec(与桌面后端 works_services 契约对齐)
@@ -5,8 +5,11 @@
5
5
  * 只做三件事:
6
6
  * 1. 按清单(config.manifest 指向 services.json)把各服务作为独立进程拉起;
7
7
  * 2. 向同进程提供 ctx.serviceBroker(list/start/stop/apply 控制面);
8
- * 3. 可选:config.skills.url 指向技能服务时,提供 ctx.skills 瘦客户端
9
- * (数据来自 HTTP,技能内容不再进程内注册)。
8
+ * 3. 可选:config.skills.url 指向技能服务时,提供 ctx.skillsHttp 瘦客户端
9
+ * (数据来自 HTTP)。技能的标准路径是 skillProvider@1 进程内注册 →
10
+ * DSH 原生 SkillRegistry(ctx.skills);此 HTTP 路是 standalone 可选模式,
11
+ * 默认不启用。服务名用 skillsHttp 而非 skills,避免遮蔽原生注册表
12
+ * (详见 lib/skills-client.mjs 头注释,P1-10 决策)。
10
13
  *
11
14
  * 生命周期:keepAlive 服务(默认)不随插件卸载停止——服务独立于 dsh;
12
15
  * config.stopOnUnload === true 时才随卸载全停。
@@ -40,13 +43,15 @@ export const { name, inject, apply } = defineDriver({
40
43
  }
41
44
  const disposeService = ctx.provide('serviceBroker', api)
42
45
 
43
- // 技能瘦客户端:ctx.skills 形状与原进程内注册表一致(list/get),
44
- // 数据源改为 HTTP 技能服务——技能内容不再进 dsh 进程。
46
+ // 技能瘦客户端(可选 standalone 模式,默认不启用):list/get 形状与进程内
47
+ // 注册表一致,数据源改为 HTTP 技能服务。服务名 skillsHttp——'skills' 归 DSH
48
+ // 原生 SkillRegistry,provide('skills') 会遮蔽它并令 skillProvider@1 插件
49
+ // 因缺 registerProvider 挂载失败(P1-10 决策,见 lib/skills-client.mjs)。
45
50
  let disposeSkills = null
46
51
  if (config.skills?.url) {
47
52
  const client = createSkillsClient(config.skills.url)
48
- disposeSkills = ctx.provide('skills', client)
49
- console.log(`[service-broker] skills ← ${config.skills.url}`)
53
+ disposeSkills = ctx.provide('skillsHttp', client)
54
+ console.log(`[service-broker] skillsHttp ← ${config.skills.url}`)
50
55
  }
51
56
 
52
57
  // 开机清单:有清单就按 up 命令投递给自己(幂等,复用优先)
@@ -1,14 +1,22 @@
1
1
  /**
2
- * 技能瘦客户端:把「技能服务」的 HTTP 面适配成原 ctx.skills 的进程内形状。
2
+ * 技能瘦客户端:把独立技能服务的 HTTP 面适配成进程内形状(list/get)。
3
3
  *
4
- * 技能内容(SKILL.md 正文)不再通过 ctx.skills.registerProvider 进程内注册,
5
- * 而是从独立技能服务拉取——dsh 进程里只剩这一个瘦适配层。
6
- * 服务端形状见 packages/capyclaw-skills-service(GET /skills、GET /skills/:name)。
4
+ * 定位(P1-10 决策):技能的标准路径是 skillProvider@1 进程内注册 →
5
+ * DSH 原生 SkillRegistry(ctx.skills);本客户端只在显式配置 skills.url 的
6
+ * standalone 模式下启用,默认不启用。
7
+ *
8
+ * 服务名刻意叫 **skillsHttp** 而不是 skills:'skills' 是 DSH 原生 SkillRegistry
9
+ * 的服务名(foundation 经 getService(ctx,'skills') 取用,skillProvider@1 靠它的
10
+ * registerProvider 挂载)。若本客户端 provide('skills') 会遮蔽原生注册表,且瘦
11
+ * 客户端没有 registerProvider,全部 skillProvider@1 插件将 E_CAP_MISSING 挂载
12
+ * 失败。改名后原生 ctx.skills 永不被遮蔽,两条路可共存。
13
+ *
14
+ * 服务端形状见 C/packages/capyclaw-skills-service(GET /skills、GET /skills/:name)。
7
15
  */
8
16
  export function createSkillsClient(baseUrl) {
9
17
  const root = String(baseUrl || '').replace(/\/$/, '')
10
18
  return {
11
- api: { name: 'skills', version: '1.0.0', kind: 'thin-client' },
19
+ api: { name: 'skillsHttp', version: '1.0.0', kind: 'thin-client' },
12
20
  async list(signal) {
13
21
  let res
14
22
  try {
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capyclaw/service-broker",
3
- "version": "0.2.25",
3
+ "version": "0.2.28",
4
4
  "description": "DSH 服务中间件(唯一进程内插件):按清单/结构化命令把各服务作为独立进程拉起,负责守护、发现(discovery)、控制端点;服务与服务之间不互相带动,跨端口组织一律经过这里。",
5
5
  "type": "module",
6
6
  "main": "./lib/host.mjs",
@@ -7,7 +7,7 @@
7
7
  "packages": {
8
8
  "@capyclaw/service-host": {
9
9
  "role": "provider",
10
- "version": "^0.1.5",
10
+ "version": "^0.1.9",
11
11
  "path": ".",
12
12
  "provides": {
13
13
  "services": [
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capyclaw/service-host",
3
- "version": "0.1.5",
3
+ "version": "0.1.9",
4
4
  "description": "DSH 公共服务宿主插件:统一 HTTP 路由、健康检查、指标、单实例锁、端口复用与静态资源,向插件提供 ctx.serviceHost。",
5
5
  "type": "module",
6
6
  "main": "./lib/host.mjs",
@@ -45,7 +45,7 @@
45
45
  "test": "node --test test/*.test.mjs"
46
46
  },
47
47
  "dependencies": {
48
- "@capyclaw/plugin-kit": "^0.2.0",
48
+ "@capyclaw/plugin-kit": "^0.2.9",
49
49
  "@capyclaw/foundation": "^0.1.1"
50
50
  },
51
51
  "repository": {
@@ -19,6 +19,8 @@ import crypto from 'node:crypto'
19
19
  import { listen, send, sendText, readBody } from '@capyclaw/plugin-kit/service/http'
20
20
  import { createServiceApi } from '@capyclaw/plugin-kit/service/api'
21
21
  import { isLoopbackOrigin as sharedIsLoopbackOrigin, mimeFor } from '@capyclaw/plugin-kit/util/http'
22
+ import { safeJoin, cleanStaticPrefix, staticPrefixHit, staticRelRaw, decodeStaticRel, staticFileHit, staticHitNameType, staticBaseHeaders } from '@capyclaw/plugin-kit/service/static'
23
+ import { appendLedger, ledgerFileFor } from '@capyclaw/plugin-kit/ledger'
22
24
  import { tuning } from './tuning.mjs'
23
25
 
24
26
  function slugOf(id) {
@@ -133,11 +135,7 @@ async function probeExisting(origin, healthPath, id) {
133
135
  }
134
136
  }
135
137
 
136
- function safeJoin(root, rel) {
137
- const target = path.resolve(root, `.${path.sep}${rel}`)
138
- const base = path.resolve(root)
139
- return target === base || target.startsWith(base + path.sep) ? target : null
140
- }
138
+ /* safeJoin 已收敛到 @capyclaw/plugin-kit/service/static(P2-13a 静态公共核心)。 */
141
139
 
142
140
  /**
143
141
  * @param {{
@@ -169,6 +167,11 @@ export function createServiceApp(options = {}) {
169
167
  let closed = false
170
168
  let workdir = ''
171
169
 
170
+ /* 统一台账(2026-11-25):服务生命周期/运行时错误追加到 profile 台账,best-effort
171
+ * 绝不反噬服务;listenOptions.ledgerFile 可覆盖(suite-kit 传自己解析的路径)。 */
172
+ const defaultLedgerFile = ledgerFileFor(process.env.DSH_PROFILE_DIR || null)
173
+ const record = (action, ok, extra = {}, file = defaultLedgerFile) => appendLedger({ source: 'service-host', name: id, version, phase: 'runtime', action, ok, ...extra }, { file })
174
+
172
175
  const app = {
173
176
  id,
174
177
  title,
@@ -204,17 +207,17 @@ export function createServiceApp(options = {}) {
204
207
  })
205
208
  },
206
209
 
207
- /** 静态目录挂载。files 可给显式映射:{ '/': ['index.html','text/html'] }。 */
210
+ /** 静态目录挂载。files 可给显式映射:{ '/': ['index.html','text/html'] }。
211
+ * 路径/头部计算走 @capyclaw/plugin-kit/service/static 公共核心(P2-13a);
212
+ * 本宿主保留自己的响应政策:畸形编码回 400、content-type 取 mimeFor(请求 rel)、每次读盘。 */
208
213
  static(prefix, dir, { files = null, index = 'index.html' } = {}) {
209
- const cleanPrefix = prefix === '/' ? '' : prefix.replace(/\/+$/, '')
214
+ const cleanPrefix = cleanStaticPrefix(prefix)
210
215
  return app.use((req, res, u) => {
211
216
  if (req.method !== 'GET' && req.method !== 'HEAD') return false
212
- if (cleanPrefix && u.pathname !== cleanPrefix && !u.pathname.startsWith(cleanPrefix + '/')) return false
213
- const relRaw = cleanPrefix ? u.pathname.slice(cleanPrefix.length) : u.pathname
214
- let rel
215
- try {
216
- rel = relRaw === '' || relRaw === '/' ? index : decodeURIComponent(relRaw.replace(/^\/+/, ''))
217
- } catch {
217
+ if (!staticPrefixHit(u.pathname, cleanPrefix)) return false
218
+ const relRaw = staticRelRaw(u.pathname, cleanPrefix)
219
+ const rel = decodeStaticRel(relRaw, index)
220
+ if (rel === null) {
218
221
  /* 畸形百分号编码 → URIError,不包会冒成 500;客户端错误回 400。 */
219
222
  res.writeHead(400, { 'content-type': 'text/plain; charset=utf-8' })
220
223
  res.end(req.method === 'HEAD' ? undefined : 'malformed path encoding')
@@ -223,9 +226,9 @@ export function createServiceApp(options = {}) {
223
226
  let file = null
224
227
  let type = mimeFor(rel)
225
228
  if (files) {
226
- const hit = files[relRaw === '' ? '/' : relRaw] || files[relRaw]
229
+ const hit = staticFileHit(files, relRaw)
227
230
  if (!hit) return false
228
- const [name, explicitType] = Array.isArray(hit) ? hit : [hit]
231
+ const [name, explicitType] = staticHitNameType(hit)
229
232
  file = safeJoin(dir, name)
230
233
  type = explicitType || type
231
234
  } else {
@@ -234,13 +237,7 @@ export function createServiceApp(options = {}) {
234
237
  if (!file) return false
235
238
  try {
236
239
  const data = fs.readFileSync(file)
237
- res.writeHead(200, {
238
- 'content-type': type,
239
- 'content-length': data.length,
240
- 'x-content-type-options': 'nosniff',
241
- 'referrer-policy': 'no-referrer',
242
- 'cache-control': 'no-cache',
243
- })
240
+ res.writeHead(200, staticBaseHeaders(type, data.length))
244
241
  res.end(req.method === 'HEAD' ? undefined : data)
245
242
  return true
246
243
  } catch {
@@ -370,13 +367,25 @@ export function createServiceApp(options = {}) {
370
367
  const webServer = listenOptions.webServer
371
368
  if (webServer && typeof webServer.register === 'function') {
372
369
  const prefix = basePath || `/plugins/${id}`
373
- embeddedDispose = webServer.register({
374
- kind: 'prefix',
375
- path: prefix,
376
- handler: (req, res) => app.dispatch(req, res, prefix),
377
- })
370
+ try {
371
+ embeddedDispose = webServer.register({
372
+ kind: 'prefix',
373
+ path: prefix,
374
+ handler: (req, res) => Promise.resolve(app.dispatch(req, res, prefix)).catch((error) => {
375
+ record('dispatch-error', false, { code: 'dispatch-error', message: String((error && error.message) || error), stackTop: (error && error.stack) || null, url: req.url }, listenOptions.ledgerFile)
376
+ try {
377
+ if (!res.headersSent) send(req, res, 500, { ok: false, error: String((error && error.message) || error) })
378
+ else res.destroy()
379
+ } catch { /* 连接已断开 */ }
380
+ }),
381
+ })
382
+ } catch (error) {
383
+ record('listen-error', false, { code: 'register-conflict', message: String((error && error.message) || error), stackTop: (error && error.stack) || null, mountPath: prefix }, listenOptions.ledgerFile)
384
+ throw error
385
+ }
378
386
  listeningPort = Number(webServer.port) || 0
379
387
  url = `http://127.0.0.1:${listeningPort}${prefix}`
388
+ record('listen', true, { mode: 'embedded', url, mountPath: prefix }, listenOptions.ledgerFile)
380
389
  return { url, port: listeningPort, reused: false, mode: 'embedded', stop: app.close }
381
390
  }
382
391
 
@@ -399,10 +408,12 @@ export function createServiceApp(options = {}) {
399
408
  if (existing && sameContract && sameVersion && sameWorkdir) {
400
409
  listeningPort = port
401
410
  url = `http://127.0.0.1:${port}${publicBasePath}`
411
+ record('listen', true, { mode: 'standalone', reused: true, url, port }, listenOptions.ledgerFile)
402
412
  return { url, port, reused: true, mode: 'standalone', stop: async () => {} }
403
413
  }
404
414
  if (existing && !(sameContract && sameVersion && sameWorkdir)) {
405
415
  const old = `pid ${existing.pid || '?'},version ${existing.version || '?'},contract ${existing.contractVersion || '?'},workdir ${existing.workdir || '?'}`
416
+ record('listen-error', false, { code: 'identity-conflict', message: `端口 ${port} 被不同身份实例占用(${old})`, mountPath: publicBasePath }, listenOptions.ledgerFile)
406
417
  throw new Error(
407
418
  `${id} 端口 ${port} 被不同身份的实例占用(${old};当前 ${version} / ${contractVersion} / ${workdir})。` +
408
419
  `为避免“新套件 + 旧进程”或“同版本 + 错数据目录”的假启动,已拒绝复用:请结束 ${existing.pid || '该'} 进程或重启 DSH 后再启动。`,
@@ -412,6 +423,7 @@ export function createServiceApp(options = {}) {
412
423
 
413
424
  const lock = await acquireLock(lockFile, { healthPath, id })
414
425
  if (!lock) {
426
+ record('listen-error', false, { code: 'lock-held', message: `已有另一个实例在运行: ${lockFile}` }, listenOptions.ledgerFile)
415
427
  throw new Error(`${id} 已有另一个实例在运行;如确认没有,请删除锁文件: ${lockFile}`)
416
428
  }
417
429
 
@@ -430,12 +442,14 @@ export function createServiceApp(options = {}) {
430
442
  lastError = error
431
443
  if (port === 0 || strictPort || attempt === scanLimit) {
432
444
  releaseLock(lock)
445
+ record('listen-error', false, { code: 'port-unavailable', message: `端口 ${tryPort} 不可用: ${(error && error.message) || error}` }, listenOptions.ledgerFile)
433
446
  throw new Error(`端口 ${tryPort} 不可用${strictPort ? '(strictPort)' : ''}: ${(error && error.message) || error}`)
434
447
  }
435
448
  }
436
449
  }
437
450
  if (!server) {
438
451
  releaseLock(lock)
452
+ record('listen-error', false, { code: 'port-unavailable', message: String((lastError && lastError.message) || lastError || '无法监听端口') }, listenOptions.ledgerFile)
439
453
  throw lastError || new Error('无法监听端口')
440
454
  }
441
455
 
@@ -445,6 +459,7 @@ export function createServiceApp(options = {}) {
445
459
  try { server.close() } catch { /* 忽略 */ }
446
460
  server = null
447
461
  releaseLock(lock)
462
+ record('listen-error', false, { code: 'write-lock-failed', message: String((error && error.message) || error), stackTop: (error && error.stack) || null }, listenOptions.ledgerFile)
448
463
  throw error
449
464
  }
450
465
  listeningPort = actualPort
@@ -452,6 +467,7 @@ export function createServiceApp(options = {}) {
452
467
 
453
468
  server.on('request', (req, res) => {
454
469
  Promise.resolve(app.dispatch(req, res, publicBasePath)).catch((error) => {
470
+ record('dispatch-error', false, { code: 'dispatch-error', message: String((error && error.message) || error), stackTop: (error && error.stack) || null, url: req.url })
455
471
  try {
456
472
  if (!res.headersSent) send(req, res, 500, { ok: false, error: String((error && error.message) || error) })
457
473
  else res.destroy()
@@ -473,12 +489,14 @@ export function createServiceApp(options = {}) {
473
489
 
474
490
  app._lock = lock
475
491
  app._discoveryFile = discoveryFile
492
+ record('listen', true, { mode: 'standalone', url, port: actualPort }, listenOptions.ledgerFile)
476
493
  return { url, port: actualPort, reused: false, mode: 'standalone', stop: app.close }
477
494
  },
478
495
 
479
496
  async close() {
480
497
  if (closed) return
481
498
  closed = true
499
+ record('closed', true, { phase: 'dispose', mode: embeddedDispose ? 'embedded' : 'standalone' })
482
500
  if (embeddedDispose) {
483
501
  try { embeddedDispose() } catch { /* 忽略 */ }
484
502
  embeddedDispose = null
@@ -1,7 +1,7 @@
1
1
  # capyclaw-skills-service
2
2
 
3
- DSH 独立技能服务(服务解耦标准形态)。把原「`ctx.skills` 进程内注册表」的
4
- 技能内容(SKILL.md)搬出 dsh 进程,作为独立端口服务:
3
+ 可选的 standalone 技能 HTTP 聚合服务:把技能内容(SKILL.md)搬出 dsh 进程,
4
+ 聚合后经独立端口对外提供:
5
5
 
6
6
  ```
7
7
  GET /healthz { ok, service:"dsh-skills", pid, port, providers, skills }
@@ -9,23 +9,55 @@ GET /skills 全量技能清单(聚合各 provider)
9
9
  GET /skills/:name 单技能正文(原 provider.get 形状)
10
10
  ```
11
11
 
12
+ ## 定位与标准路径(P1-10 决策记录)
13
+
14
+ - **技能的标准路径 = `skillProvider@1` 进程内注册 → DSH 原生 SkillRegistry
15
+ (`ctx.skills`)**。全部技能包(ppt/browser/canvas/rsp/conductor)默认走
16
+ 这条路:插件 `plugin.mjs` 经 foundation 的 skill-provider 适配器调用
17
+ `ctx.skills.registerProvider`,不经过本服务。
18
+ - 本服务是**可选 standalone 模式,默认不启用**:全仓没有任何 `skills.url`
19
+ 配置,HTTP 聚合路从未激活。仅当显式给 `@capyclaw/service-broker` 配置
20
+ `skills.url` 时才启用(broker 拉起本服务 + 提供瘦客户端)。
21
+ - **撞名解决**:broker 瘦客户端的服务名是 **`skillsHttp`**(`ctx.skillsHttp`),
22
+ 不是 `skills`。`'skills'` 归 DSH 原生 SkillRegistry;若瘦客户端
23
+ `provide('skills')` 会遮蔽原生注册表,且瘦客户端只有 list/get、没有
24
+ `registerProvider`,全部 `skillProvider@1` 插件会 E_CAP_MISSING 挂载失败。
25
+ 改名后两条路可共存,api 契约 major 保持 1 兼容。
26
+ - **为什么保留包而不是删除**:HTTP 聚合是服务解耦的合法形态(技能内容可独立
27
+ 于 dsh 进程演进/多实例共享),删除会丢掉这条可选路;故选择「保留 + 通用化
28
+ + 明确定位 + 默认不启用」。
29
+
12
30
  ## 技能数据源
13
31
 
14
- 自动扫描两类布局(都找 `capyclaw-ppt-skill-*` 目录里的 `lib/index.mjs`):
32
+ 按**套件清单的 `provides.skills` 声明**发现技能包目录(不再按
33
+ `capyclaw-ppt-skill-*` 等目录名前缀猜测——旧硬编码已去除):
34
+
35
+ - 清单方言与 `@capyclaw/suite-kit` 一致:`dsh-suite.json` / `suite.json`
36
+ (`packages` 为「包名 → entry」对象)与 `suite-compose.json`
37
+ (组合结果快照,`packages` 为 `[{name, entry, dir, …}]` 数组)。
38
+ - 只认 `entry.provides.skills` 为非空数组的条目;目录按 `entry.path` 相对
39
+ 清单目录解析(无 path 的 npm 解析风格声明则上溯找 `node_modules/<包名>`)。
40
+ - 每个技能包目录读 **`plugin.mjs`**(foundation ABI),宿主只提供 `ctx.skills`
41
+ 收集 provider——与 dsh 里的装配路径完全一致。
15
42
 
16
- - dev 树:`../plugin/capyclaw-ppt-skill-*`
17
- - 套件安装态:本目录同级(`node_modules/capyclaw-ppt-skill-*`)
43
+ 清单搜索覆盖四种布局:
18
44
 
19
- 可用 `--providers <dir>[[;|,]dir…]` 显式指定。
45
+ - dev 树:`A/projects/<项目>/`(项目根清单)与
46
+ `A/projects/<项目>/artifacts/plugin/`(成品套件包,如 capyclaw-ppt-suite)
47
+ - npm 扁平安装:`node_modules/@capyclaw/<套件包>`
48
+ - pnpm 提升可见:`<profile>/node_modules/@capyclaw/<套件包>`
49
+ - pnpm 严格布局:`.pnpm/@capyclaw+<name>@<ver>` 虚拟目录的真实树
50
+
51
+ 可用 `--providers <dir>[[;|,]dir…]` 显式指定技能包目录,跳过清单发现。
20
52
 
21
53
  ## 由谁拉起
22
54
 
23
- `@capyclaw/service-broker` 按清单以独立进程拉起(套件捆绑布局的清单见
24
- @capyclaw/ppt-suite 包根 `services.json`,端口 `"auto"` 由 broker 现分配)。dsh 侧经 broker 的
25
- skills 瘦客户端(`ctx.skills` 兼容形状)访问本服务。
55
+ 仅启用 standalone 模式时:`@capyclaw/service-broker` 按清单以独立进程拉起
56
+ (端口 `"auto"` 由 broker 现分配),dsh 侧经 broker 的 **`ctx.skillsHttp`**
57
+ 瘦客户端(list/get 形状)访问本服务。默认配置下 broker 不拉起本服务。
26
58
 
27
59
  ## 独立运行
28
60
 
29
61
  ```bash
30
62
  node server.mjs [--port 0] [--providers dir1,dir2] # 0 = OS 随机分配
31
- ```
63
+ ```
@@ -1,9 +1,12 @@
1
1
  {
2
2
  "name": "@capyclaw/skills-service",
3
- "version": "0.2.2",
4
- "description": "DSH 独立技能服务:聚合 @capyclaw/ppt-skill-* 的 SKILL.md 内容,HTTP 面暴露(GET /healthz、/skills、/skills/:name)。服务解耦标准形态:技能内容不再进程内注册,dsh 侧经 broker 的 skills 瘦客户端访问。",
3
+ "version": "0.2.4",
4
+ "description": "可选 standalone 技能 HTTP 聚合服务:按套件清单 provides.skills 声明发现技能包(不限套件),聚合 SKILL.md 内容经 HTTP 暴露(GET /healthz、/skills、/skills/:name)。技能标准路径是 skillProvider@1 进程内注册(DSH 原生 SkillRegistry);本服务默认不启用,仅显式配置 broker skills.url 时经 ctx.skillsHttp 瘦客户端访问。",
5
5
  "type": "module",
6
6
  "main": "./server.mjs",
7
+ "scripts": {
8
+ "test": "node --test test/*.test.mjs"
9
+ },
7
10
  "engines": {
8
11
  "node": ">=18"
9
12
  },
@@ -26,5 +29,5 @@
26
29
  "bugs": {
27
30
  "url": "https://github.com/Maple496/capyclaw/issues"
28
31
  },
29
- "homepage": "https://github.com/Maple496/capyclaw/tree/main/A/projects/ppt/capyclaw-skills-service#readme"
32
+ "homepage": "https://github.com/Maple496/capyclaw/tree/main/C/packages/capyclaw-skills-service#readme"
30
33
  }