@capyclaw/foundation 0.1.3 → 0.1.9
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/README.md +23 -6
- package/abi/bridge.mjs +86 -0
- package/abi/errors.mjs +4 -0
- package/abi/index.mjs +16 -0
- package/abi/kinds.mjs +90 -2
- package/abi/loader.mjs +277 -181
- package/abi/spec.mjs +62 -5
- package/kit/index.mjs +3 -4
- package/lib/adapters/client-panel.mjs +98 -0
- package/lib/adapters/host-event.mjs +35 -0
- package/lib/adapters/http-router.mjs +27 -5
- package/lib/adapters/lifecycle-hook.mjs +6 -13
- package/lib/adapters/own-server.mjs +113 -0
- package/lib/adapters/raw.mjs +68 -0
- package/lib/adapters/runtime-driver.mjs +49 -17
- package/lib/adapters/service-provider.mjs +77 -0
- package/lib/adapters/skill-provider.mjs +2 -3
- package/lib/adapters/tool-provider.mjs +14 -8
- package/lib/adapters/ui-panel.mjs +14 -8
- package/lib/ctx-service.mjs +21 -0
- package/lib/host/caps.mjs +34 -0
- package/lib/host/contract.mjs +85 -0
- package/lib/host/dsh.mjs +50 -0
- package/lib/host/memory.mjs +160 -0
- package/lib/host.mjs +6 -5
- package/lib/kernel.mjs +125 -35
- package/lib/subsystems/events.mjs +6 -1
- package/lib/subsystems/log.mjs +24 -3
- package/lib/subsystems/net.mjs +160 -22
- package/node_modules/@capyclaw/plugin-kit/INTEGRATION.md +100 -0
- package/node_modules/@capyclaw/plugin-kit/README.md +70 -0
- package/node_modules/@capyclaw/plugin-kit/bin/sync.mjs +103 -0
- package/node_modules/@capyclaw/plugin-kit/package.json +69 -0
- package/node_modules/@capyclaw/plugin-kit/src/cli.mjs +56 -0
- package/node_modules/@capyclaw/plugin-kit/src/config.mjs +142 -0
- package/node_modules/@capyclaw/plugin-kit/src/editor/heartbeat-worker.js +129 -0
- package/node_modules/@capyclaw/plugin-kit/src/editor/lease.mjs +139 -0
- package/node_modules/@capyclaw/plugin-kit/src/editor/shadow.mjs +219 -0
- package/node_modules/@capyclaw/plugin-kit/src/editor/transport.js +13 -0
- package/node_modules/@capyclaw/plugin-kit/src/index.mjs +13 -0
- package/node_modules/@capyclaw/plugin-kit/src/pack.mjs +230 -0
- package/node_modules/@capyclaw/plugin-kit/src/runtime/context.mjs +128 -0
- package/node_modules/@capyclaw/plugin-kit/src/self-contained.mjs +307 -0
- package/node_modules/@capyclaw/plugin-kit/src/service/api.mjs +45 -0
- package/node_modules/@capyclaw/plugin-kit/src/service/http.mjs +113 -0
- package/node_modules/@capyclaw/plugin-kit/src/service/mcp-http.mjs +150 -0
- package/node_modules/@capyclaw/plugin-kit/src/service/server.mjs +131 -0
- package/node_modules/@capyclaw/plugin-kit/src/skills/provider.mjs +82 -0
- package/node_modules/@capyclaw/plugin-kit/src/store/blob-store.mjs +109 -0
- package/node_modules/@capyclaw/plugin-kit/src/store/content-store.mjs +17 -0
- package/node_modules/@capyclaw/plugin-kit/src/store/journal-store.mjs +89 -0
- package/node_modules/@capyclaw/plugin-kit/src/tools/result.mjs +57 -0
- package/node_modules/@capyclaw/plugin-kit/src/tools/validate.mjs +150 -0
- package/node_modules/@capyclaw/plugin-kit/src/tuning.mjs +82 -0
- package/node_modules/@capyclaw/plugin-kit/src/util/fs-lock.mjs +132 -0
- package/node_modules/@capyclaw/plugin-kit/src/util/http.mjs +130 -0
- package/node_modules/@capyclaw/plugin-kit/src/workspace/manager.mjs +656 -0
- package/node_modules/@capyclaw/plugin-kit/src/zip.mjs +181 -0
- package/package.json +6 -3
- package/runtime/index.mjs +4 -1
package/README.md
CHANGED
|
@@ -14,21 +14,38 @@ export const { name, inject, apply } = createPlugin({
|
|
|
14
14
|
}, import.meta.url)
|
|
15
15
|
```
|
|
16
16
|
|
|
17
|
-
##
|
|
17
|
+
## 四个入口
|
|
18
18
|
|
|
19
19
|
| 入口 | 内容 | 谁用 |
|
|
20
20
|
|---|---|---|
|
|
21
21
|
| `.` | Cordis 插件,产出 `ctx.os` | dsh profile |
|
|
22
22
|
| `./abi` | 描述符构建器、加载器、错误码、版本工具 | 应用插件 |
|
|
23
|
-
| `./kit` |
|
|
23
|
+
| `./kit` | plugin-kit 兼容导出,包含 Node/dsh 耦合,非纯函数库 | 兼容调用方 |
|
|
24
24
|
| `./runtime` | `createKernel` 等宿主 API | dsh 之外的宿主(独立技能服务、测试夹具) |
|
|
25
25
|
|
|
26
26
|
## 状态
|
|
27
27
|
|
|
28
|
-
-
|
|
29
|
-
|
|
30
|
-
|
|
28
|
+
- 包清单当前为 `0.1.6`;0.1.6 修的是「关掉插件再启用撞 E_DUP_PLUGIN」(disposer 未登记 + 失败
|
|
29
|
+
记录占名),详见下面的「生命周期」。
|
|
30
|
+
- 分层:应用插件 → ABI → 内核 → Host 适配器 → 本地 Node.js agent 运行时。
|
|
31
|
+
dsh 服务访问位于 Host 适配层;memory 只是参考实现,不证明第二个真实 agent 兼容。
|
|
32
|
+
HTTP/资源/进程仍有本地 Node 协议约束,详见 `docs/HOST-ADAPTER.md`。
|
|
33
|
+
- ABI v1,六个标准接口 + `host-event@1` + `client-panel@1` + `service-provider@1` +
|
|
34
|
+
`own-server@1` + 可执行 `raw@1`;基建插件使用 `defineDriver` 驱动入口。
|
|
31
35
|
- 能力表按 `ctx` 实际服务探测(见 `lib/kernel.mjs` 的 `detectCapabilities`),不做空头承诺。
|
|
32
|
-
-
|
|
36
|
+
- 当前仓库:27 个插件走标准接口,`raw@1` 当前为空,6 个基建插件走
|
|
37
|
+
`defineDriver` 驱动层;`meta/foundation.allowlist.json` 的模糊豁免为 0。
|
|
38
|
+
详见 `docs/POLICY.md` 与 `docs/spec/plugin-abi.md`。
|
|
33
39
|
|
|
34
40
|
设计规范见 `docs/spec/plugin-abi.md`,强制规则见 `docs/POLICY.md`。
|
|
41
|
+
|
|
42
|
+
## 生命周期(0.1.6 起)
|
|
43
|
+
|
|
44
|
+
`createPlugin().apply` 会把 `os.mount()` 的 disposer 通过 `ctx.effect(() => () => dispose())`
|
|
45
|
+
登记到本次激活的 ctx 上——**不能只靠 apply 的返回值**:Cordis 只认 `ctx.effect` 回调返回的
|
|
46
|
+
disposer,返回值不可靠。漏登记时,关掉插件后内核里的记录不会摘除,再次启用就会撞
|
|
47
|
+
`E_DUP_PLUGIN: 插件 xxx 已挂载`。
|
|
48
|
+
|
|
49
|
+
加载器侧另有两条兜底:`mount()` 遇到 `disposed` / `failed` 的旧记录会先摘掉并尽力回收;
|
|
50
|
+
`start` 失败清理完成后立即让出插件名。两者共同保证「关掉 → 再启用」可重复无数次。
|
|
51
|
+
|
package/abi/bridge.mjs
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 桥(editor bridge)与文档模型的标准声明。
|
|
3
|
+
*
|
|
4
|
+
* 这一层只放「协议常量 + 声明校验」,不放运行时:
|
|
5
|
+
* - 常量:协议版本、模型版本、能力名(suite 清单与 healthz 都用它)
|
|
6
|
+
* - declareBridge():把工作台的声明冻结成标准形状
|
|
7
|
+
* - assertBridgeRuntime():启动/巡检时把「声明」与「运行时实际上报」对齐
|
|
8
|
+
*
|
|
9
|
+
* 运行时(relay / 队列 / 租约)仍在 @capyclaw/document-workbench + service-host,
|
|
10
|
+
* 不进 ABI 层:foundation 是所有插件的地基,不该背 HTTP/store 依赖。
|
|
11
|
+
*
|
|
12
|
+
* 能力名(与 suite 清单的 requires/provides.capabilities 对应):
|
|
13
|
+
* editor.bridge —— 插件提供一个编辑器权威桥(/healthz + /status + /watch + /ack)
|
|
14
|
+
* doc.model —— 插件提供文档模型(稳定 ID / 清单 / 定位 / 编辑计划 / 校验)
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/** 桥协议版本:端点集合与状态机语义的主版本。不兼容变更才升 major。 */
|
|
18
|
+
export const BRIDGE_PROTOCOL_VERSION = '1.0.0'
|
|
19
|
+
|
|
20
|
+
/** 文档模型版本:清单字段、定位器语义、编辑 op 的主版本。 */
|
|
21
|
+
export const DOC_MODEL_VERSION = '1.0.0'
|
|
22
|
+
|
|
23
|
+
export const BRIDGE_CAPABILITY = 'editor.bridge'
|
|
24
|
+
export const DOC_MODEL_CAPABILITY = 'doc.model'
|
|
25
|
+
|
|
26
|
+
/** healthz 里桥声明的字段名(避免与既有的 bridge: '<name>' 字符串冲突)。 */
|
|
27
|
+
export const BRIDGE_INFO_FIELD = 'bridgeInfo'
|
|
28
|
+
|
|
29
|
+
const ID_RE = /^[a-z0-9][a-z0-9-]*$/
|
|
30
|
+
const VERSION_RE = /^\d+\.\d+\.\d+$/
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* 校验并冻结一份桥声明。
|
|
34
|
+
* @param {{ id: string, protocol?: string, model?: string|null, editor?: 'required'|'optional' }} input
|
|
35
|
+
*/
|
|
36
|
+
export function declareBridge(input = {}) {
|
|
37
|
+
const { id, protocol = BRIDGE_PROTOCOL_VERSION, model = null, editor = 'required' } = input
|
|
38
|
+
const errors = []
|
|
39
|
+
if (typeof id !== 'string' || !ID_RE.test(id)) errors.push(`id 必须是 kebab-case:${id}`)
|
|
40
|
+
if (!VERSION_RE.test(protocol)) errors.push(`protocol 版本非法:${protocol}`)
|
|
41
|
+
if (model !== null && !VERSION_RE.test(model)) errors.push(`model 版本非法:${model}`)
|
|
42
|
+
if (editor !== 'required' && editor !== 'optional') errors.push(`editor 只能是 required/optional:${editor}`)
|
|
43
|
+
if (errors.length) throw new Error(`桥声明非法:${errors.join(';')}`)
|
|
44
|
+
return Object.freeze({ id, protocol, model, editor })
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** 桥声明 → suite 清单里的 provides.capabilities。 */
|
|
48
|
+
export function bridgeCapabilities(declaration) {
|
|
49
|
+
const decl = declaration && declaration.protocol ? declaration : declareBridge(declaration)
|
|
50
|
+
return Object.freeze({
|
|
51
|
+
[BRIDGE_CAPABILITY]: decl.protocol,
|
|
52
|
+
...(decl.model ? { [DOC_MODEL_CAPABILITY]: decl.model } : {}),
|
|
53
|
+
})
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** 桥声明 → suite 清单里工具面对运行时的 requires.capabilities(skill 包用)。 */
|
|
57
|
+
export function bridgeConsumerRequirements(ns, { needsModel = false } = {}) {
|
|
58
|
+
const out = { [`${ns}.runtime`]: `^${BRIDGE_PROTOCOL_VERSION}` }
|
|
59
|
+
if (needsModel) out[DOC_MODEL_CAPABILITY] = `^${DOC_MODEL_VERSION}`
|
|
60
|
+
return Object.freeze(out)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* 声明与运行时对账:启动或巡检时调用。
|
|
65
|
+
*
|
|
66
|
+
* @param {object} declaration declareBridge 的结果(来自插件代码/清单)
|
|
67
|
+
* @param {object} runtime healthz 里的 bridgeInfo(实际运行值)
|
|
68
|
+
* @returns {{ ok: boolean, issues: string[] }}
|
|
69
|
+
*/
|
|
70
|
+
export function assertBridgeRuntime(declaration, runtime) {
|
|
71
|
+
const issues = []
|
|
72
|
+
const decl = declaration && declaration.id ? declaration : declareBridge(declaration || {})
|
|
73
|
+
if (!runtime || typeof runtime !== 'object') {
|
|
74
|
+
return { ok: false, issues: [`运行时没有上报 ${BRIDGE_INFO_FIELD}`] }
|
|
75
|
+
}
|
|
76
|
+
if (runtime.id !== decl.id) issues.push(`id 不一致:声明 ${decl.id} / 运行 ${runtime.id}`)
|
|
77
|
+
if (runtime.protocol !== decl.protocol) {
|
|
78
|
+
issues.push(`协议版本不一致:声明 ${decl.protocol} / 运行 ${runtime.protocol}`)
|
|
79
|
+
}
|
|
80
|
+
const declaredModel = decl.model || null
|
|
81
|
+
const runtimeModel = runtime.model || null
|
|
82
|
+
if (declaredModel !== runtimeModel) {
|
|
83
|
+
issues.push(`模型版本不一致:声明 ${declaredModel || '无'} / 运行 ${runtimeModel || '无'}`)
|
|
84
|
+
}
|
|
85
|
+
return { ok: issues.length === 0, issues }
|
|
86
|
+
}
|
package/abi/errors.mjs
CHANGED
|
@@ -31,6 +31,10 @@ export const ERROR_CODES = Object.freeze({
|
|
|
31
31
|
E_BASE_URL_MISSING: 'E_BASE_URL_MISSING',
|
|
32
32
|
/** 运行时没有 ctx.os:foundation 未安装或未启用。 */
|
|
33
33
|
E_KERNEL_MISSING: 'E_KERNEL_MISSING',
|
|
34
|
+
E_KERNEL_CLOSED: 'E_KERNEL_CLOSED',
|
|
35
|
+
E_NOT_READY: 'E_NOT_READY',
|
|
36
|
+
E_CAP_RESERVED: 'E_CAP_RESERVED',
|
|
37
|
+
E_CAP_CONFLICT: 'E_CAP_CONFLICT',
|
|
34
38
|
})
|
|
35
39
|
|
|
36
40
|
export class AbiError extends Error {
|
package/abi/index.mjs
CHANGED
|
@@ -10,9 +10,25 @@ export {
|
|
|
10
10
|
httpRouter,
|
|
11
11
|
uiPanel,
|
|
12
12
|
lifecycleHook,
|
|
13
|
+
hostEvent,
|
|
14
|
+
clientPanel,
|
|
15
|
+
ownServer,
|
|
16
|
+
serviceProvider,
|
|
17
|
+
defineDriver,
|
|
13
18
|
rawPlugin,
|
|
14
19
|
} from './spec.mjs'
|
|
15
20
|
export { createLoader } from './loader.mjs'
|
|
21
|
+
export {
|
|
22
|
+
BRIDGE_CAPABILITY,
|
|
23
|
+
BRIDGE_INFO_FIELD,
|
|
24
|
+
BRIDGE_PROTOCOL_VERSION,
|
|
25
|
+
DOC_MODEL_CAPABILITY,
|
|
26
|
+
DOC_MODEL_VERSION,
|
|
27
|
+
assertBridgeRuntime,
|
|
28
|
+
bridgeCapabilities,
|
|
29
|
+
bridgeConsumerRequirements,
|
|
30
|
+
declareBridge,
|
|
31
|
+
} from './bridge.mjs'
|
|
16
32
|
export { validateInterface } from './validate.mjs'
|
|
17
33
|
export { AbiError, ERROR_CODES, isAbiError, toAbiError } from './errors.mjs'
|
|
18
34
|
export { validateSchema } from './schema.mjs'
|
package/abi/kinds.mjs
CHANGED
|
@@ -48,6 +48,7 @@ export const INTERFACE_KINDS = Object.freeze({
|
|
|
48
48
|
additionalProperties: false,
|
|
49
49
|
properties: {
|
|
50
50
|
namespace: kebab,
|
|
51
|
+
prefixNamespace: { type: 'boolean' },
|
|
51
52
|
provide: { type: 'function' },
|
|
52
53
|
instructions: nonEmpty,
|
|
53
54
|
capabilities: { type: 'object' },
|
|
@@ -69,8 +70,9 @@ export const INTERFACE_KINDS = Object.freeze({
|
|
|
69
70
|
properties: {
|
|
70
71
|
id: kebab,
|
|
71
72
|
entry: nonEmpty,
|
|
73
|
+
when: { type: 'function' },
|
|
72
74
|
args: { type: 'array', items: { type: 'string' } },
|
|
73
|
-
port: { type: ['number', 'string'] },
|
|
75
|
+
port: { type: ['number', 'string', 'function'] },
|
|
74
76
|
portArg: nonEmpty,
|
|
75
77
|
dir: nonEmpty,
|
|
76
78
|
env: { type: 'function' },
|
|
@@ -156,11 +158,97 @@ export const INTERFACE_KINDS = Object.freeze({
|
|
|
156
158
|
},
|
|
157
159
|
}),
|
|
158
160
|
|
|
161
|
+
'host-event@1': Object.freeze({
|
|
162
|
+
kind: 'host-event@1',
|
|
163
|
+
major: 1,
|
|
164
|
+
title: '宿主事件订阅',
|
|
165
|
+
summary: '订阅宿主生命周期/界面注入事件;适配器负责在插件自己的 ctx 上注册与注销。',
|
|
166
|
+
unique: false,
|
|
167
|
+
schema: Object.freeze({
|
|
168
|
+
type: 'object',
|
|
169
|
+
required: ['event', 'handler'],
|
|
170
|
+
additionalProperties: false,
|
|
171
|
+
properties: {
|
|
172
|
+
event: nonEmpty,
|
|
173
|
+
handler: { type: 'function' },
|
|
174
|
+
},
|
|
175
|
+
}),
|
|
176
|
+
}),
|
|
177
|
+
|
|
178
|
+
'client-panel@1': Object.freeze({
|
|
179
|
+
kind: 'client-panel@1',
|
|
180
|
+
major: 1,
|
|
181
|
+
title: '客户端面板',
|
|
182
|
+
summary: '声明页面开机全局变量与可选静态目录;适配器负责 webserver/index-inject 注入与静态挂载。',
|
|
183
|
+
unique: false,
|
|
184
|
+
schema: Object.freeze({
|
|
185
|
+
type: 'object',
|
|
186
|
+
additionalProperties: false,
|
|
187
|
+
properties: {
|
|
188
|
+
mount: { type: 'string', pattern: '^/' },
|
|
189
|
+
root: nonEmpty,
|
|
190
|
+
index: nonEmpty,
|
|
191
|
+
prepare: { type: 'function' },
|
|
192
|
+
bootGlobals: { type: 'function' },
|
|
193
|
+
},
|
|
194
|
+
}),
|
|
195
|
+
extra(impl, errors) {
|
|
196
|
+
if (!impl.root && !impl.prepare && !impl.bootGlobals) {
|
|
197
|
+
errors.push('$ 至少需要 root / prepare / bootGlobals 之一')
|
|
198
|
+
}
|
|
199
|
+
if (impl.root && !impl.mount) errors.push('$ 提供 root 时必须同时提供 mount')
|
|
200
|
+
},
|
|
201
|
+
}),
|
|
202
|
+
|
|
203
|
+
'own-server@1': Object.freeze({
|
|
204
|
+
kind: 'own-server@1',
|
|
205
|
+
major: 1,
|
|
206
|
+
title: '自有 HTTP 面',
|
|
207
|
+
summary: '宿主有网页端口时挂到该端口的前缀下(不新起端口),否则由 service-host 托管独立 loopback origin。',
|
|
208
|
+
unique: true,
|
|
209
|
+
uniqueBy: 'id',
|
|
210
|
+
schema: Object.freeze({
|
|
211
|
+
type: 'object',
|
|
212
|
+
required: ['id', 'handler'],
|
|
213
|
+
additionalProperties: false,
|
|
214
|
+
properties: {
|
|
215
|
+
id: kebab,
|
|
216
|
+
mount: { type: 'string', pattern: '^/' },
|
|
217
|
+
port: { type: ['number', 'string', 'function'] },
|
|
218
|
+
host: nonEmpty,
|
|
219
|
+
handler: { type: 'function' },
|
|
220
|
+
start: { type: 'function' },
|
|
221
|
+
stop: { type: 'function' },
|
|
222
|
+
},
|
|
223
|
+
}),
|
|
224
|
+
}),
|
|
225
|
+
|
|
226
|
+
'service-provider@1': Object.freeze({
|
|
227
|
+
kind: 'service-provider@1',
|
|
228
|
+
major: 1,
|
|
229
|
+
title: '宿主服务提供者',
|
|
230
|
+
summary: '向宿主 ctx 提供一个具名服务;服务工厂必须同步,异步启动放在 start 钩子里。',
|
|
231
|
+
unique: true,
|
|
232
|
+
uniqueBy: 'service',
|
|
233
|
+
schema: Object.freeze({
|
|
234
|
+
type: 'object',
|
|
235
|
+
required: ['service', 'provide'],
|
|
236
|
+
additionalProperties: false,
|
|
237
|
+
properties: {
|
|
238
|
+
service: nonEmpty,
|
|
239
|
+
provide: { type: 'function' },
|
|
240
|
+
when: { type: 'function' },
|
|
241
|
+
start: { type: 'function' },
|
|
242
|
+
stop: { type: 'function' },
|
|
243
|
+
},
|
|
244
|
+
}),
|
|
245
|
+
}),
|
|
246
|
+
|
|
159
247
|
'raw@1': Object.freeze({
|
|
160
248
|
kind: 'raw@1',
|
|
161
249
|
major: 1,
|
|
162
250
|
title: '原生插件(逃生舱)',
|
|
163
|
-
summary: '极少数需要自定义装配的插件;必须在 foundation.allowlist.json 登记,且不得与其他接口混用。',
|
|
251
|
+
summary: '极少数需要自定义装配的插件;必须在 meta/foundation.allowlist.json 登记,且不得与其他接口混用。',
|
|
164
252
|
unique: false,
|
|
165
253
|
exclusive: true,
|
|
166
254
|
schema: Object.freeze({
|