@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.
- package/composition.lock.json +15 -15
- package/contract/wall-contract.json +1 -1
- package/dsh-suite.json +19 -9
- package/node_modules/@capyclaw/asset-service/dsh-suite.json +1 -1
- package/node_modules/@capyclaw/asset-service/package.json +1 -1
- package/node_modules/@capyclaw/asset-service/src/index.mjs +96 -9
- package/node_modules/@capyclaw/foundation/abi/assets.mjs +26 -0
- package/node_modules/@capyclaw/foundation/abi/bridge.mjs +3 -1
- package/node_modules/@capyclaw/foundation/abi/index.mjs +15 -0
- package/node_modules/@capyclaw/foundation/abi/loader.mjs +8 -1
- package/node_modules/@capyclaw/foundation/abi/spec.mjs +5 -2
- package/node_modules/@capyclaw/foundation/abi/version.mjs +63 -5
- package/node_modules/@capyclaw/foundation/abi/visibility.mjs +90 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/lifecycle-hook.mjs +21 -4
- package/node_modules/@capyclaw/foundation/lib/adapters/raw.mjs +11 -10
- package/node_modules/@capyclaw/foundation/lib/adapters/runtime-driver.mjs +8 -1
- package/node_modules/@capyclaw/foundation/lib/host/caps.mjs +5 -1
- package/node_modules/@capyclaw/foundation/lib/host/contract.mjs +5 -2
- package/node_modules/@capyclaw/foundation/lib/host/dsh.mjs +4 -0
- package/node_modules/@capyclaw/foundation/lib/host/memory.mjs +29 -2
- package/node_modules/@capyclaw/foundation/lib/kernel.mjs +13 -3
- package/node_modules/@capyclaw/foundation/lib/skill/scan.mjs +31 -3
- package/node_modules/@capyclaw/foundation/lib/subsystems/net.mjs +12 -2
- package/node_modules/@capyclaw/foundation/package.json +1 -1
- package/node_modules/@capyclaw/plugin-kit/package.json +3 -1
- package/node_modules/@capyclaw/plugin-kit/src/editor/heartbeat-worker.js +5 -2
- package/node_modules/@capyclaw/plugin-kit/src/editor/heartbeat.mjs +26 -0
- package/node_modules/@capyclaw/plugin-kit/src/editor/lease.mjs +3 -82
- package/node_modules/@capyclaw/plugin-kit/src/service/mcp-http.mjs +13 -6
- package/node_modules/@capyclaw/plugin-kit/src/service/static.mjs +79 -0
- package/node_modules/@capyclaw/plugin-kit/src/tools/result.mjs +31 -15
- package/node_modules/@capyclaw/plugin-kit/src/workspace/manager.mjs +13 -5
- package/node_modules/@capyclaw/portal/package.json +1 -1
- package/node_modules/@capyclaw/service-broker/README.md +1 -1
- package/node_modules/@capyclaw/service-broker/lib/control.mjs +23 -7
- package/node_modules/@capyclaw/service-broker/lib/engine.mjs +60 -6
- package/node_modules/@capyclaw/service-broker/lib/host.mjs +11 -6
- package/node_modules/@capyclaw/service-broker/lib/manifest.mjs +15 -2
- package/node_modules/@capyclaw/service-broker/lib/recovery.mjs +0 -50
- package/node_modules/@capyclaw/service-broker/lib/skills-client.mjs +30 -10
- package/node_modules/@capyclaw/service-broker/package.json +1 -1
- package/node_modules/@capyclaw/service-host/dsh-suite.json +1 -1
- package/node_modules/@capyclaw/service-host/package.json +1 -1
- package/node_modules/@capyclaw/service-host/src/index.mjs +18 -19
- package/node_modules/@capyclaw/tool-host/dsh-suite.json +2 -2
- package/node_modules/@capyclaw/tool-host/lib/host.mjs +34 -4
- package/node_modules/@capyclaw/tool-host/package.json +1 -1
- package/node_modules/@capyclaw/tool-host/src/admit-images.mjs +80 -0
- package/node_modules/@capyclaw/tool-host/src/caller-session.mjs +25 -0
- package/node_modules/@capyclaw/tool-host/src/descriptors.mjs +28 -4
- package/node_modules/@capyclaw/tool-host/src/index.mjs +90 -19
- package/node_modules/@capyclaw/tool-host/src/mode-gate.mjs +272 -0
- package/node_modules/@capyclaw/wall-workbench/contract/wall-contract.json +1 -1
- package/node_modules/@capyclaw/wall-workbench/package.json +6 -6
- package/node_modules/@capyclaw/wall-workbench/plugin.mjs +13 -1
- package/node_modules/@capyclaw/wall-workbench/server/activate-log.mjs +66 -0
- package/node_modules/@capyclaw/wall-workbench/server/contract.mjs +1 -1
- package/node_modules/@capyclaw/wall-workbench/server/embedded-host.mjs +3 -1
- package/node_modules/@capyclaw/wall-workbench/server/shared/SHARED_MANIFEST.json +4 -4
- package/node_modules/@capyclaw/wall-workbench/server/workbench-framework.mjs +34 -0
- package/node_modules/@capyclaw/wall-workbench/ui/app.js +83 -0
- package/node_modules/@capyclaw/wall-workbench/ui/style.css +14 -11
- package/package.json +8 -8
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* lifecycle-hook@1 的适配器。
|
|
3
3
|
*
|
|
4
|
-
* 运行时只执行 onStart / onStop
|
|
5
|
-
*
|
|
6
|
-
*
|
|
4
|
+
* 运行时只执行 onStart / onStop。onInstall / onUpgrade 属于安装与升级流程:
|
|
5
|
+
* 这里把它们登记进 hook 注册表(os.hooks),保证「声明了就找得到」。
|
|
6
|
+
* 截至本版本仓内尚无自动 fire 它们的安装/升级消费者(见 plugin-abi.md §4.6)——
|
|
7
|
+
* 这是已声明的集成点,不是已落地的自动钩子。安装器可经 os.hooks.run('install'|'upgrade', args)
|
|
8
|
+
* 主动触发;run() 只提供调用 API,是否/何时调用由安装器决定。
|
|
7
9
|
*/
|
|
10
|
+
import { AbiError, ERROR_CODES } from '../../abi/errors.mjs'
|
|
11
|
+
|
|
8
12
|
export const kind = 'lifecycle-hook@1'
|
|
9
13
|
|
|
10
14
|
export function createHookRegistry() {
|
|
@@ -12,7 +16,7 @@ export function createHookRegistry() {
|
|
|
12
16
|
install: [],
|
|
13
17
|
upgrade: [],
|
|
14
18
|
register(phase, entry) {
|
|
15
|
-
if (!this[phase]) throw new
|
|
19
|
+
if (!this[phase]) throw new AbiError(ERROR_CODES.E_SCHEMA, `未知生命周期阶段 ${phase}`)
|
|
16
20
|
this[phase].push(entry)
|
|
17
21
|
return () => {
|
|
18
22
|
const index = this[phase].indexOf(entry)
|
|
@@ -25,6 +29,19 @@ export function createHookRegistry() {
|
|
|
25
29
|
upgrade: this.upgrade.map((entry) => entry.plugin),
|
|
26
30
|
}
|
|
27
31
|
},
|
|
32
|
+
/**
|
|
33
|
+
* 安装/升级集成点:按登记顺序触发某阶段全部钩子,逐个 await。
|
|
34
|
+
* 仓内暂无自动调用方(见头注);这是给安装器(plugin-sync / plugin-drop / publisher)
|
|
35
|
+
* 预留的调用 API。未知阶段抛稳定错误码 E_SCHEMA。
|
|
36
|
+
*/
|
|
37
|
+
async run(phase, args = {}) {
|
|
38
|
+
if (!this[phase]) throw new AbiError(ERROR_CODES.E_SCHEMA, `未知生命周期阶段 ${phase}`)
|
|
39
|
+
const results = []
|
|
40
|
+
for (const entry of [...this[phase]]) {
|
|
41
|
+
results.push({ plugin: entry.plugin, result: await entry.run(args) })
|
|
42
|
+
}
|
|
43
|
+
return results
|
|
44
|
+
},
|
|
28
45
|
}
|
|
29
46
|
}
|
|
30
47
|
|
|
@@ -44,18 +44,19 @@ export function createRawAdapter({ log }) {
|
|
|
44
44
|
|
|
45
45
|
if (thenable(result)) {
|
|
46
46
|
let instance = {}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
})
|
|
47
|
+
/* apply 结算只做归一化,绝不在此 eagerly 跑 instance.start()——旧实现
|
|
48
|
+
* 在 compile 返回的 promise resolve 时就启动,绕过 loader 的 start 阶段与
|
|
49
|
+
* stopRequested:loader 正在回滚/已请求卸载时 raw 仍会起进程/监听。
|
|
50
|
+
* start 交给 loader 在 start 阶段显式触发(此时才尊重 stopRequested)。 */
|
|
51
|
+
const ready = Promise.resolve(result).then((value) => { instance = normalize(value) })
|
|
53
52
|
return {
|
|
54
|
-
start: () =>
|
|
53
|
+
start: async ({ signal } = {}) => {
|
|
54
|
+
await ready
|
|
55
|
+
/* 转发 loader 的卸载 signal,让 raw 实例的 start 也能感知取消。 */
|
|
56
|
+
return instance.start ? instance.start({ signal }) : undefined
|
|
57
|
+
},
|
|
55
58
|
dispose: async () => {
|
|
56
|
-
|
|
57
|
-
try { await started } catch { /* start 失败由 loader 记录 */ }
|
|
58
|
-
}
|
|
59
|
+
try { await ready } catch { /* apply 失败由 loader 记录 */ }
|
|
59
60
|
if (typeof instance.dispose === 'function') return instance.dispose()
|
|
60
61
|
},
|
|
61
62
|
}
|
|
@@ -62,14 +62,21 @@ export function createRuntimeDriverAdapter({ host, config, log }) {
|
|
|
62
62
|
...(impl.args || []),
|
|
63
63
|
]
|
|
64
64
|
|
|
65
|
+
/* keepAlive 由声明推导:restart.max===0 表示「不要自动重启」,对应 keepAlive:false。
|
|
66
|
+
* 旧实现恒 true,应用声明 restart:{max:0} 仍被 broker 拉起,切换/回收行为与声明不符。 */
|
|
67
|
+
const keepAlive = !(impl.restart && impl.restart.max === 0)
|
|
65
68
|
const serviceSpec = {
|
|
66
69
|
id: impl.id,
|
|
67
70
|
cmd,
|
|
68
71
|
ports: { main: port },
|
|
69
72
|
url: 'http://127.0.0.1:${PORT}/',
|
|
70
|
-
keepAlive
|
|
73
|
+
keepAlive,
|
|
71
74
|
}
|
|
72
75
|
if (impl.health?.path) serviceSpec.healthPath = impl.health.path
|
|
76
|
+
/* 声明的健康期望/超时与重启策略必须透传给 broker,不能被静默丢弃。 */
|
|
77
|
+
if (impl.health?.expect && typeof impl.health.expect === 'object') serviceSpec.expect = impl.health.expect
|
|
78
|
+
if (impl.health?.timeoutMs) serviceSpec.waitMs = impl.health.timeoutMs
|
|
79
|
+
if (impl.restart) serviceSpec.restart = impl.restart
|
|
73
80
|
if (impl.dir) {
|
|
74
81
|
serviceSpec.cwd = fileURLToPath(new URL(impl.dir, mountContext.context.baseUrl))
|
|
75
82
|
}
|
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
* editorHost)的地方——它们属于 dsh 适配层,不属于 ABI。
|
|
7
7
|
*/
|
|
8
8
|
import { getService } from '../ctx-service.mjs'
|
|
9
|
+
import { HOST_SERVICE_CONTRACT } from './contract.mjs'
|
|
9
10
|
|
|
10
11
|
export const BASE_CAPABILITIES = Object.freeze({
|
|
11
12
|
'os.events': '1.0.0',
|
|
@@ -26,9 +27,12 @@ export function detectDshCapabilities(ctx) {
|
|
|
26
27
|
&& hasMethods(getService(ctx, 'tools'), 'register')) capabilities['os.tools'] = '1.0.0'
|
|
27
28
|
if (hasMethods(getService(ctx, 'serviceBroker'), 'start', 'stop')) capabilities['os.proc'] = '1.0.0'
|
|
28
29
|
if (capabilities['os.net'] && hasMethods(getService(ctx, 'editorHost'), 'createEditorHost')) capabilities['os.ui'] = '1.0.0'
|
|
29
|
-
if (hasMethods(getService(ctx, 'pluginManager'), '
|
|
30
|
+
if (hasMethods(getService(ctx, 'pluginManager'), ...HOST_SERVICE_CONTRACT['os.plugins'][1])) {
|
|
30
31
|
capabilities['os.plugins'] = '1.0.0'
|
|
31
32
|
}
|
|
32
33
|
if (hasMethods(getService(ctx, 'assets'), 'put', 'get')) capabilities['os.assets'] = '1.0.0'
|
|
34
|
+
// work-store 是可选持久化层(不在 core 六件内):探测到才 advertise os.works,
|
|
35
|
+
// 应用用 os.caps.has('os.works') 判断降级,不再 safeGet(ctx, 'workStore') 裸摸。
|
|
36
|
+
if (hasMethods(getService(ctx, 'workStore'), 'backendFor')) capabilities['os.works'] = '1.0.0'
|
|
33
37
|
return capabilities
|
|
34
38
|
}
|
|
@@ -3,7 +3,9 @@ import { parseVersion } from '../../abi/version.mjs'
|
|
|
3
3
|
import { AbiError, ERROR_CODES } from '../../abi/errors.mjs'
|
|
4
4
|
|
|
5
5
|
const ACCESSORS = ['capabilities', 'log', 'skills', 'tools', 'http', 'proc', 'ui']
|
|
6
|
-
|
|
6
|
+
/** Host v1 能力→[访问器, 必需方法] 契约。导出为单一真源:内核门面与能力探测都引用它,
|
|
7
|
+
* 不再各自硬编码 os.plugins 的 dsh 方法名(listBundles/setPluginEnabled…)。 */
|
|
8
|
+
export const HOST_SERVICE_CONTRACT = {
|
|
7
9
|
'os.skills': ['skills', ['registerProvider']],
|
|
8
10
|
'os.tools': ['tools', ['register']],
|
|
9
11
|
'os.net': ['http', ['createApp', 'createSharedHost']],
|
|
@@ -12,6 +14,7 @@ const SERVICES = {
|
|
|
12
14
|
'os.ui': ['ui', ['createEditorHost']],
|
|
13
15
|
'os.plugins': ['plugins', ['listBundles', 'listPlugins', 'setBundleEnabled', 'setPluginEnabled']],
|
|
14
16
|
'os.assets': ['assets', ['put', 'get']],
|
|
17
|
+
'os.works': ['works', ['backendFor']],
|
|
15
18
|
}
|
|
16
19
|
|
|
17
20
|
function invalid(path, expectation) {
|
|
@@ -68,7 +71,7 @@ export function assertHostContract(host) {
|
|
|
68
71
|
if (typeof logger[method] !== 'function') invalid(`host.log().${method}`, 'must be a function')
|
|
69
72
|
}
|
|
70
73
|
}
|
|
71
|
-
for (const [capability, [accessor, methods]] of Object.entries(
|
|
74
|
+
for (const [capability, [accessor, methods]] of Object.entries(HOST_SERVICE_CONTRACT)) {
|
|
72
75
|
if (!Object.hasOwn(capabilities, capability)) continue
|
|
73
76
|
if (typeof host[accessor] !== 'function') {
|
|
74
77
|
invalid(`host.${accessor}`, `must be a function when advertising ${capability}`)
|
|
@@ -15,6 +15,9 @@
|
|
|
15
15
|
* web() { register({kind, path, handler}) => dispose, port } | undefined
|
|
16
16
|
* proc() { start(serviceSpec) => Promise<{errors?}>, stop(id) }
|
|
17
17
|
* ui() { createEditorHost({ id, dir, basePath, index }) }
|
|
18
|
+
* assets() { put, get, ... } | undefined —— 资产字节仓(asset-service)
|
|
19
|
+
* works() { backendFor, claimSessionMode, ... } | undefined —— 作品目录(work-store,
|
|
20
|
+
* 可选持久化层:缺席时内核 os.works 返回 null,应用自行降级内存态)
|
|
18
21
|
*
|
|
19
22
|
* `web()` 是宿主网页端口本身(dsh 即 ctx.webServer)。拿到它时,内核一律把 HTTP
|
|
20
23
|
* 挂到该端口的路径前缀下,不再另起端口;拿不到才退回 os.net 的独立监听。
|
|
@@ -46,5 +49,6 @@ export function createDshHost(ctx) {
|
|
|
46
49
|
ui: () => getService(ctx, 'editorHost'),
|
|
47
50
|
plugins: () => getService(ctx, 'pluginManager'),
|
|
48
51
|
assets: () => getService(ctx, 'assets'),
|
|
52
|
+
works: () => getService(ctx, 'workStore'),
|
|
49
53
|
}
|
|
50
54
|
}
|
|
@@ -11,6 +11,10 @@ import { BASE_CAPABILITIES } from './caps.mjs'
|
|
|
11
11
|
|
|
12
12
|
const NOOP_LOG = { debug() {}, info() {}, warn() {}, error() {} }
|
|
13
13
|
|
|
14
|
+
/* 内存宿主也要有界:app 不 dispose、asset 只 put 不 delete 的用法会让数组/Map
|
|
15
|
+
* 无限增长。app 提供 dispose() 摘除自己;assets 提供 delete() 与上限 LRU 淘汰。 */
|
|
16
|
+
const DEFAULT_MAX_ASSETS = 1024
|
|
17
|
+
|
|
14
18
|
export function createMemoryHost(options = {}) {
|
|
15
19
|
const state = {
|
|
16
20
|
providers: [],
|
|
@@ -24,6 +28,9 @@ export function createMemoryHost(options = {}) {
|
|
|
24
28
|
plugins: [],
|
|
25
29
|
}
|
|
26
30
|
const log = options.log ?? NOOP_LOG
|
|
31
|
+
const maxAssets = Number(options.maxAssets) > 0 ? Number(options.maxAssets) : DEFAULT_MAX_ASSETS
|
|
32
|
+
/* 单调序号:不能用 size+1 拼 id——淘汰后 size 回落,同毫秒内会撞掉已有资产。 */
|
|
33
|
+
let assetSeq = 0
|
|
27
34
|
|
|
28
35
|
function http() {
|
|
29
36
|
return {
|
|
@@ -41,6 +48,11 @@ export function createMemoryHost(options = {}) {
|
|
|
41
48
|
}
|
|
42
49
|
},
|
|
43
50
|
async dispatch() {},
|
|
51
|
+
dispose() {
|
|
52
|
+
const index = state.apps.indexOf(app)
|
|
53
|
+
if (index >= 0) state.apps.splice(index, 1)
|
|
54
|
+
routes.length = 0
|
|
55
|
+
},
|
|
44
56
|
}
|
|
45
57
|
state.apps.push(app)
|
|
46
58
|
return app
|
|
@@ -149,12 +161,27 @@ export function createMemoryHost(options = {}) {
|
|
|
149
161
|
}),
|
|
150
162
|
assets: () => ({
|
|
151
163
|
async put(value, contentType = 'application/octet-stream') {
|
|
152
|
-
const id = `asset-${state.assets.size + 1}`
|
|
153
164
|
const buffer = Buffer.isBuffer(value) ? value : Buffer.from(String(value))
|
|
165
|
+
/* Map 保持插入序:超限淘汰最旧(LRU 近似),get 命中会刷新到队尾。 */
|
|
166
|
+
while (state.assets.size >= maxAssets) {
|
|
167
|
+
const oldest = state.assets.keys().next().value
|
|
168
|
+
state.assets.delete(oldest)
|
|
169
|
+
}
|
|
170
|
+
const id = `asset-${++assetSeq}`
|
|
154
171
|
state.assets.set(id, { data: buffer, contentType })
|
|
155
172
|
return { id, url: `/assets/${id}`, size: buffer.length, contentType }
|
|
156
173
|
},
|
|
157
|
-
get(id) {
|
|
174
|
+
get(id) {
|
|
175
|
+
const entry = state.assets.get(id)
|
|
176
|
+
if (entry) { state.assets.delete(id); state.assets.set(id, entry) }
|
|
177
|
+
return entry
|
|
178
|
+
},
|
|
179
|
+
delete(id) { return state.assets.delete(id) },
|
|
180
|
+
gc() {
|
|
181
|
+
const removed = state.assets.size
|
|
182
|
+
state.assets.clear()
|
|
183
|
+
return { removed }
|
|
184
|
+
},
|
|
158
185
|
}),
|
|
159
186
|
}
|
|
160
187
|
}
|
|
@@ -13,7 +13,8 @@ import { AbiError, ERROR_CODES, toAbiError } from '../abi/errors.mjs'
|
|
|
13
13
|
import { createLoader } from '../abi/loader.mjs'
|
|
14
14
|
import { BASE_CAPABILITIES, detectDshCapabilities } from './host/caps.mjs'
|
|
15
15
|
import { createDshHost } from './host/dsh.mjs'
|
|
16
|
-
import { assertHostContract } from './host/contract.mjs'
|
|
16
|
+
import { assertHostContract, HOST_SERVICE_CONTRACT } from './host/contract.mjs'
|
|
17
|
+
import { assetUrl } from '../abi/assets.mjs'
|
|
17
18
|
import { createSkillAdapter } from './adapters/skill-provider.mjs'
|
|
18
19
|
import { createToolProviderAdapter } from './adapters/tool-provider.mjs'
|
|
19
20
|
import { createRuntimeDriverAdapter } from './adapters/runtime-driver.mjs'
|
|
@@ -88,7 +89,8 @@ export function createKernel(ctx, config = {}, options = {}) {
|
|
|
88
89
|
|
|
89
90
|
function pluginManager() {
|
|
90
91
|
const service = hostService('plugins', 'os.plugins')
|
|
91
|
-
|
|
92
|
+
// 方法名单一真源 = Host 契约(contract.mjs),内核不再硬编码 dsh 方法名。
|
|
93
|
+
for (const method of HOST_SERVICE_CONTRACT['os.plugins'][1]) {
|
|
92
94
|
if (typeof service[method] !== 'function') {
|
|
93
95
|
throw new AbiError(ERROR_CODES.E_CAP_MISSING,
|
|
94
96
|
`os.plugins 不可用:pluginManager 缺少 ${method}`, { capability: 'os.plugins', method })
|
|
@@ -143,7 +145,15 @@ export function createKernel(ctx, config = {}, options = {}) {
|
|
|
143
145
|
assets: {
|
|
144
146
|
put: (value, contentType) => hostService('assets', 'os.assets').put(value, contentType),
|
|
145
147
|
get: (id) => hostService('assets', 'os.assets').get(id),
|
|
146
|
-
url: (id) =>
|
|
148
|
+
url: (id) => assetUrl(id),
|
|
149
|
+
},
|
|
150
|
+
|
|
151
|
+
/** 作品目录门面(可选持久化层):work-store 缺席时返回 null,应用自行降级内存态。
|
|
152
|
+
* 与 os.assets(必需字节仓,缺席抛 E_CAP_MISSING)不同——works 是可选增强,
|
|
153
|
+
* 不在 core 六件内。用 os.caps.has('os.works') 探测可用性,不再 safeGet(ctx,'workStore') 裸摸。 */
|
|
154
|
+
get works() {
|
|
155
|
+
const service = typeof host.works === 'function' ? host.works() : null
|
|
156
|
+
return service && typeof service.then !== 'function' ? service : null
|
|
147
157
|
},
|
|
148
158
|
|
|
149
159
|
/** 能力检查与自报。 */
|
|
@@ -13,12 +13,40 @@ const NAME_RE = /^[a-z0-9]+(?:-[a-z0-9]+)*$/
|
|
|
13
13
|
const DEFAULT_RANK = 600
|
|
14
14
|
|
|
15
15
|
export function parseSkill(raw, path) {
|
|
16
|
+
/* BOM 会让 /^---/ 匹配不到 frontmatter,误报「缺少 YAML frontmatter」。先剥掉。 */
|
|
17
|
+
raw = raw.replace(/^\uFEFF/, '')
|
|
16
18
|
const frontmatter = /^---\r?\n([\s\S]*?)\r?\n---(?:\r?\n|$)/u.exec(raw)
|
|
17
19
|
if (!frontmatter?.[1]) throw new Error(`skill ${path} 缺少 YAML frontmatter`)
|
|
18
20
|
const meta = {}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
21
|
+
const lines = frontmatter[1].split(/\r?\n/)
|
|
22
|
+
for (let i = 0; i < lines.length; i++) {
|
|
23
|
+
const match = /^([A-Za-z0-9_-]+):\s*(.*)$/.exec(lines[i])
|
|
24
|
+
if (!match) continue
|
|
25
|
+
const key = match[1]
|
|
26
|
+
let value = match[2].trim()
|
|
27
|
+
/* 块标量 description: | / > :旧实现把 '|' 本身当成描述直达 LLM,多行正文全丢。
|
|
28
|
+
* 这里聚合后续缩进行,| 保留换行、> 折叠为空格,按 chomping 处理尾部空白。 */
|
|
29
|
+
const block = /^([|>])([+-]?)$/.exec(value)
|
|
30
|
+
if (block) {
|
|
31
|
+
const body = []
|
|
32
|
+
let j = i + 1
|
|
33
|
+
for (; j < lines.length; j++) {
|
|
34
|
+
const ln = lines[j]
|
|
35
|
+
if (ln.trim() === '') { body.push(''); continue }
|
|
36
|
+
if (/^\s/.test(ln)) body.push(ln)
|
|
37
|
+
else break
|
|
38
|
+
}
|
|
39
|
+
i = j - 1
|
|
40
|
+
const nonEmpty = body.filter((l) => l.trim() !== '')
|
|
41
|
+
const minIndent = nonEmpty.length ? Math.min(...nonEmpty.map((l) => /^\s*/.exec(l)[0].length)) : 0
|
|
42
|
+
const stripped = body.map((l) => l.slice(minIndent))
|
|
43
|
+
let text = stripped.join(block[1] === '>' ? ' ' : '\n')
|
|
44
|
+
if (block[2] !== '+') text = text.replace(/\s+$/, '')
|
|
45
|
+
value = text
|
|
46
|
+
} else {
|
|
47
|
+
value = value.replace(/^['"]|['"]$/g, '').trim()
|
|
48
|
+
}
|
|
49
|
+
meta[key] = value
|
|
22
50
|
}
|
|
23
51
|
if (!meta.name || !meta.description) throw new Error(`skill ${path} 缺少 name/description`)
|
|
24
52
|
if (!NAME_RE.test(meta.name)) throw new Error(`skill ${path} name 必须是 kebab-case`)
|
|
@@ -91,7 +91,11 @@ export function createNet(host, config = {}) {
|
|
|
91
91
|
}
|
|
92
92
|
return
|
|
93
93
|
}
|
|
94
|
-
await new Promise((resolve) =>
|
|
94
|
+
await new Promise((resolve) => {
|
|
95
|
+
/* unref:迟到补挂的轮询定时器不得拖住进程退出(最长 60s 的尾延迟)。 */
|
|
96
|
+
const timer = setTimeout(resolve, WEB_LATE_POLL_MS)
|
|
97
|
+
timer.unref?.()
|
|
98
|
+
})
|
|
95
99
|
}
|
|
96
100
|
})().catch(() => {})
|
|
97
101
|
}
|
|
@@ -116,7 +120,10 @@ export function createNet(host, config = {}) {
|
|
|
116
120
|
if (!web && shouldWaitForWeb()) {
|
|
117
121
|
const deadline = Date.now() + WEB_WAIT_MS
|
|
118
122
|
while (!web && Date.now() < deadline && !closed) {
|
|
119
|
-
await new Promise((resolve) =>
|
|
123
|
+
await new Promise((resolve) => {
|
|
124
|
+
const timer = setTimeout(resolve, WEB_POLL_MS)
|
|
125
|
+
timer.unref?.()
|
|
126
|
+
})
|
|
120
127
|
web = webServer()
|
|
121
128
|
}
|
|
122
129
|
}
|
|
@@ -140,6 +147,9 @@ export function createNet(host, config = {}) {
|
|
|
140
147
|
disposal = (async () => {
|
|
141
148
|
if (!sharedHost) return
|
|
142
149
|
// listen 失败由 ready/start 报告;清理仍需执行,且 close 失败不能被吞掉。
|
|
150
|
+
// 注意:dispose 在 listen 失败时仍 RESOLVE(错误经 ready/start 上报)——这是
|
|
151
|
+
// 被 network-lifecycle 测试固化的契约,close 不再二次上抛 listen 错,避免
|
|
152
|
+
// 破坏「清理成功即 dispose 成功」的语义。
|
|
143
153
|
await listening.catch(() => {})
|
|
144
154
|
// 迟到补挂的路由要一起撤掉,否则共享宿主关了、宿主网页端口上还留着分发器。
|
|
145
155
|
try {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@capyclaw/plugin-kit",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.8",
|
|
4
4
|
"description": "DSH 插件基础设施标准件:HTTP/MCP、单实例锁、参数校验、技能 provider、journal 存储、编辑器传输与租约。",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./src/index.mjs",
|
|
@@ -10,11 +10,13 @@
|
|
|
10
10
|
"./service/mcp-http": "./src/service/mcp-http.mjs",
|
|
11
11
|
"./service/server": "./src/service/server.mjs",
|
|
12
12
|
"./service/api": "./src/service/api.mjs",
|
|
13
|
+
"./service/static": "./src/service/static.mjs",
|
|
13
14
|
"./tools/validate": "./src/tools/validate.mjs",
|
|
14
15
|
"./tools/result": "./src/tools/result.mjs",
|
|
15
16
|
"./skills/provider": "./src/skills/provider.mjs",
|
|
16
17
|
"./editor/transport": "./src/editor/transport.js",
|
|
17
18
|
"./editor/lease": "./src/editor/lease.mjs",
|
|
19
|
+
"./editor/heartbeat": "./src/editor/heartbeat.mjs",
|
|
18
20
|
"./editor/shadow": "./src/editor/shadow.mjs",
|
|
19
21
|
"./store/journal-store": "./src/store/journal-store.mjs",
|
|
20
22
|
"./store/blob-store": "./src/store/blob-store.mjs",
|
|
@@ -45,7 +45,10 @@ self.onmessage = function(e) {
|
|
|
45
45
|
function startHeartbeat() {
|
|
46
46
|
if (intervalHandle) clearInterval(intervalHandle);
|
|
47
47
|
|
|
48
|
-
//
|
|
48
|
+
// 心跳节奏统一为 3s:单一真源是 editor/heartbeat.mjs 的 HEARTBEAT_INTERVAL_MS。
|
|
49
|
+
// 本文件是原样拷贝的经典 Worker,运行时不能 import npm 模块,故以字面量承载节奏,
|
|
50
|
+
// 由 test/heartbeat-constants.test.mjs 钉死与常量一致,防止再次漂移。
|
|
51
|
+
// 心跳对象是编辑器「在场租约」(editor-host /claim);两层租约语义见 heartbeat.mjs 头注。
|
|
49
52
|
intervalHandle = setInterval(() => {
|
|
50
53
|
if (!active) return;
|
|
51
54
|
|
|
@@ -66,7 +69,7 @@ function startHeartbeat() {
|
|
|
66
69
|
});
|
|
67
70
|
}
|
|
68
71
|
});
|
|
69
|
-
},
|
|
72
|
+
}, 3000);
|
|
70
73
|
|
|
71
74
|
// 立即发送第一次
|
|
72
75
|
sendClaim().catch(() => {});
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 编辑器「在场租约」心跳节奏常量单一真源(P2-13b)。
|
|
3
|
+
*
|
|
4
|
+
* 背景:心跳节奏曾在两处手工对齐而漂移——plugin-kit 的 heartbeat-worker.js 写死
|
|
5
|
+
* 4000ms(还留着「避免 5 秒判过期」的过期注释),document-workbench 客户端却是
|
|
6
|
+
* 3000ms。两者必须同拍,否则编辑器判活与文档 owner 判活快慢不一。这里把节奏收敛
|
|
7
|
+
* 成一个常量,客户端直接 import;worker 是原样拷贝的经典 Web Worker(运行时不能
|
|
8
|
+
* import npm 模块),其 setInterval 字面量由 test/heartbeat-constants.test.mjs 钉死
|
|
9
|
+
* 等于本常量,防止再次漂移。统一取架构审查建议的 3s。
|
|
10
|
+
*
|
|
11
|
+
* 两层租约语义不同,都要保留(本常量只统一「心跳节奏」,不合并两层租约):
|
|
12
|
+
* 1. 在场租约(presence)——@capyclaw/plugin-kit/editor/lease.mjs 的 createEditorLease,
|
|
13
|
+
* 由 editor-host 的 /claim 消费,「显示即连接」:新窗口强制抢占、旧窗口静默待命;
|
|
14
|
+
* TTL 10s、无 epoch,靠客户端每 HEARTBEAT_INTERVAL_MS 心跳续租。
|
|
15
|
+
* 2. 所有权租约(owner)——@capyclaw/document-workbench/session 的 claimOwner/OWNER_TTL,
|
|
16
|
+
* deck/文档级所有权,带 epoch fencing(换人 +1、旧 owner 回执被丢弃)。PPT 的
|
|
17
|
+
* A/projects/ppt/src/server/store/lease.mjs 只是它的兼容再导出。此为套件领域件,
|
|
18
|
+
* 不在本次改造范围,仅在此说清两层,避免误把在场租约当成所有权租约。
|
|
19
|
+
* 两条租约线共用「心跳 3s、TTL 10s」的并发协议;TTL 的运行时真源仍是 lease.mjs 的
|
|
20
|
+
* DEFAULT_TTL 与 document-workbench 的 OWNER_TTL(均已为 10s,未漂移,不在本次改动范围)。
|
|
21
|
+
*
|
|
22
|
+
* 本文件零依赖、browser-safe(无 node:*),可被编辑器客户端 bundle 直接 import。
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
/** 心跳间隔(毫秒):在场租约与文档 owner 租约共用的续租节奏,单一真源。 */
|
|
26
|
+
export const HEARTBEAT_INTERVAL_MS = 3000
|
|
@@ -6,8 +6,9 @@
|
|
|
6
6
|
*/
|
|
7
7
|
import { tuning } from '../tuning.mjs'
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
/* 与文档 owner 租约(document-workbench OWNER_TTL)对齐为 10s:
|
|
10
|
+
* 两条租约线共用「心跳 3s、TTL 10s」的并发协议,避免编辑器判活比文档慢半拍。 */
|
|
11
|
+
const DEFAULT_TTL = 10 // 秒
|
|
11
12
|
|
|
12
13
|
/**
|
|
13
14
|
* 创建编辑器租约仲裁器。
|
|
@@ -57,83 +58,3 @@ export function createEditorLease(opts = {}) {
|
|
|
57
58
|
},
|
|
58
59
|
}
|
|
59
60
|
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* 编辑器端自动挂接循环。
|
|
63
|
-
* 首次 claim 强制抢占(后打开者赢),之后静默心跳。
|
|
64
|
-
* @param {{
|
|
65
|
-
* clientId: string,
|
|
66
|
-
* claim: (force: boolean) => Promise<boolean>,
|
|
67
|
-
* release?: () => void,
|
|
68
|
-
* onActive?: () => void,
|
|
69
|
-
* onStandby?: () => void,
|
|
70
|
-
* retryMs?: number,
|
|
71
|
-
* heartbeatMs?: number,
|
|
72
|
-
* setInterval?: (cb: () => void) => any,
|
|
73
|
-
* clearInterval?: (handle: any) => void
|
|
74
|
-
* }} o
|
|
75
|
-
*/
|
|
76
|
-
export function startAutoAttach(o) {
|
|
77
|
-
if (!o || !o.clientId) throw new Error('startAutoAttach: clientId 必填')
|
|
78
|
-
if (typeof o.claim !== 'function') throw new Error('startAutoAttach: claim 必须是函数')
|
|
79
|
-
|
|
80
|
-
const retryMs = o.retryMs || 1000
|
|
81
|
-
const heartbeatMs = o.heartbeatMs || DEFAULT_HEARTBEAT * 1000
|
|
82
|
-
const setI = o.setInterval || ((cb) => setInterval(cb, heartbeatMs))
|
|
83
|
-
const clearI = o.clearInterval || ((handle) => clearInterval(handle))
|
|
84
|
-
|
|
85
|
-
let active = false
|
|
86
|
-
let heartbeat = null
|
|
87
|
-
let disposed = false
|
|
88
|
-
|
|
89
|
-
async function tryClaim(force) {
|
|
90
|
-
try {
|
|
91
|
-
return await o.claim(force)
|
|
92
|
-
} catch {
|
|
93
|
-
return false
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
function goActive() {
|
|
98
|
-
if (active) return
|
|
99
|
-
active = true
|
|
100
|
-
try { o.onActive && o.onActive() } catch { /* 用户回调失败不阻塞挂接 */ }
|
|
101
|
-
heartbeat = setI(async () => {
|
|
102
|
-
if (disposed) return
|
|
103
|
-
const ok = await tryClaim(false)
|
|
104
|
-
if (!ok) goStandby()
|
|
105
|
-
})
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
function goStandby() {
|
|
109
|
-
if (!active) return
|
|
110
|
-
active = false
|
|
111
|
-
if (heartbeat != null) {
|
|
112
|
-
clearI(heartbeat)
|
|
113
|
-
heartbeat = null
|
|
114
|
-
}
|
|
115
|
-
try { o.onStandby && o.onStandby() } catch { /* 静默 */ }
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
;(async () => {
|
|
119
|
-
let first = true
|
|
120
|
-
while (!disposed) {
|
|
121
|
-
const ok = await tryClaim(first)
|
|
122
|
-
first = false
|
|
123
|
-
if (ok) {
|
|
124
|
-
goActive()
|
|
125
|
-
return
|
|
126
|
-
}
|
|
127
|
-
await new Promise((resolve) => setTimeout(resolve, retryMs))
|
|
128
|
-
}
|
|
129
|
-
})()
|
|
130
|
-
|
|
131
|
-
return {
|
|
132
|
-
get active() { return active },
|
|
133
|
-
dispose() {
|
|
134
|
-
disposed = true
|
|
135
|
-
goStandby()
|
|
136
|
-
try { o.release && o.release() } catch { /* 释放失败不阻塞退出 */ }
|
|
137
|
-
},
|
|
138
|
-
}
|
|
139
|
-
}
|
|
@@ -22,12 +22,19 @@ function reply(id, result) {
|
|
|
22
22
|
|
|
23
23
|
export function createMcpHandler({
|
|
24
24
|
tools = [], callTool, serverInfo, instructions,
|
|
25
|
-
maxBodyBytes = 10 * 1024 * 1024, maxBatch = 100,
|
|
25
|
+
maxBodyBytes = 10 * 1024 * 1024, maxBatch = 100, listChanged = false,
|
|
26
26
|
} = {}) {
|
|
27
|
-
|
|
27
|
+
/* tools 可以是静态数组,或一个返回当前数组的函数(活引用)。活引用让
|
|
28
|
+
* tools/list 每次读到最新工具集——registry 动态增删工具后无需重建 handler。 */
|
|
29
|
+
const liveProvider = typeof tools === 'function' ? tools : null
|
|
30
|
+
if (!liveProvider && !Array.isArray(tools)) throw new Error('createMcpHandler: tools 必须是数组或返回数组的函数')
|
|
28
31
|
if (typeof callTool !== 'function') throw new Error('createMcpHandler: callTool 必须是函数')
|
|
29
32
|
|
|
30
|
-
const
|
|
33
|
+
const currentTools = () => {
|
|
34
|
+
const list = liveProvider ? liveProvider() : tools
|
|
35
|
+
return Array.isArray(list) ? list : []
|
|
36
|
+
}
|
|
37
|
+
const toolMapNow = () => new Map(currentTools().map((tool) => [tool.name, tool]))
|
|
31
38
|
|
|
32
39
|
return async function handle(req, res) {
|
|
33
40
|
/* 长工具调用(如截图)期间客户端可能超时断连;socket 的 'error' 事件
|
|
@@ -89,7 +96,7 @@ export function createMcpHandler({
|
|
|
89
96
|
if (method === 'initialize') {
|
|
90
97
|
const result = {
|
|
91
98
|
protocolVersion: String(params?.protocolVersion || DEFAULT_PROTOCOL),
|
|
92
|
-
capabilities: { tools: { listChanged:
|
|
99
|
+
capabilities: { tools: { listChanged: !!listChanged } },
|
|
93
100
|
serverInfo: serverInfo || { name: 'dsh-plugin', version: '0.0.0' },
|
|
94
101
|
}
|
|
95
102
|
if (typeof instructions === 'string' && instructions) result.instructions = instructions
|
|
@@ -101,13 +108,13 @@ export function createMcpHandler({
|
|
|
101
108
|
if (method === 'ping') return isNotification ? null : reply(id, {})
|
|
102
109
|
|
|
103
110
|
if (method === 'tools/list') {
|
|
104
|
-
return isNotification ? null : reply(id, { tools })
|
|
111
|
+
return isNotification ? null : reply(id, { tools: currentTools() })
|
|
105
112
|
}
|
|
106
113
|
|
|
107
114
|
if (method === 'tools/call') {
|
|
108
115
|
const name = params?.name
|
|
109
116
|
const args = params?.arguments && typeof params.arguments === 'object' ? params.arguments : {}
|
|
110
|
-
const tool =
|
|
117
|
+
const tool = toolMapNow().get(name)
|
|
111
118
|
if (!tool) {
|
|
112
119
|
if (isNotification) return null
|
|
113
120
|
return reply(id, {
|