@capyclaw/foundation 0.1.4 → 0.1.10
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 -9
- 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 +285 -184
- package/abi/spec.mjs +67 -7
- package/abi/version.mjs +63 -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 +26 -3
- package/lib/adapters/lifecycle-hook.mjs +6 -13
- package/lib/adapters/own-server.mjs +113 -0
- package/lib/adapters/raw.mjs +69 -0
- package/lib/adapters/runtime-driver.mjs +55 -15
- package/lib/adapters/service-provider.mjs +77 -0
- package/lib/adapters/tool-provider.mjs +11 -4
- package/lib/adapters/ui-panel.mjs +11 -4
- package/lib/ctx-service.mjs +21 -0
- package/lib/host/caps.mjs +13 -5
- package/lib/host/contract.mjs +85 -0
- package/lib/host/dsh.mjs +10 -1
- package/lib/host/memory.mjs +71 -0
- package/lib/host.mjs +6 -5
- package/lib/kernel.mjs +92 -7
- package/lib/skill/scan.mjs +31 -3
- package/lib/subsystems/events.mjs +6 -1
- package/lib/subsystems/log.mjs +23 -1
- package/lib/subsystems/net.mjs +164 -15
- 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 +60 -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
|
@@ -11,11 +11,25 @@ import { AbiError, ERROR_CODES } from '../../abi/errors.mjs'
|
|
|
11
11
|
export const kind = 'runtime-driver@1'
|
|
12
12
|
|
|
13
13
|
export function createRuntimeDriverAdapter({ host, config, log }) {
|
|
14
|
-
const defaultPort = config.driverPort ??
|
|
14
|
+
const defaultPort = config.driverPort ?? 'auto'
|
|
15
15
|
|
|
16
16
|
return {
|
|
17
17
|
kind,
|
|
18
18
|
compile(impl, mountContext) {
|
|
19
|
+
let enabled = true
|
|
20
|
+
if (typeof impl.when === 'function') {
|
|
21
|
+
try {
|
|
22
|
+
enabled = impl.when({ spec: mountContext.spec, config: mountContext.config }) !== false
|
|
23
|
+
} catch (error) {
|
|
24
|
+
throw new AbiError(ERROR_CODES.E_ADAPTER_THREW,
|
|
25
|
+
`runtime-driver when() 失败:${error.message}`, { plugin: mountContext.spec.name, id: impl.id, cause: error })
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
if (!enabled) {
|
|
29
|
+
log?.info?.(`[foundation] ${mountContext.spec.name} 跳过 runtime-driver ${impl.id}(when=false)`)
|
|
30
|
+
return { skipped: true, dispose() {} }
|
|
31
|
+
}
|
|
32
|
+
|
|
19
33
|
const broker = host.proc()
|
|
20
34
|
if (!broker?.start || !broker?.stop) {
|
|
21
35
|
throw new AbiError(ERROR_CODES.E_CAP_MISSING,
|
|
@@ -29,35 +43,57 @@ export function createRuntimeDriverAdapter({ host, config, log }) {
|
|
|
29
43
|
}
|
|
30
44
|
|
|
31
45
|
const entryPath = fileURLToPath(new URL(impl.entry, mountContext.context.baseUrl))
|
|
32
|
-
const
|
|
46
|
+
const resolvedPort = typeof impl.port === 'function'
|
|
47
|
+
? impl.port({ spec: mountContext.spec, config: mountContext.config })
|
|
48
|
+
: impl.port
|
|
49
|
+
if (resolvedPort !== undefined && resolvedPort !== 'auto'
|
|
50
|
+
&& !(Number.isInteger(resolvedPort) && resolvedPort > 0 && resolvedPort <= 65535)) {
|
|
51
|
+
throw new AbiError(ERROR_CODES.E_PLUGIN_SHAPE,
|
|
52
|
+
`runtime-driver port 必须是 1-65535 整数或 "auto",收到 ${String(resolvedPort)}`,
|
|
53
|
+
{ plugin: mountContext.spec.name, id: impl.id, port: resolvedPort })
|
|
54
|
+
}
|
|
55
|
+
const port = resolvedPort ?? defaultPort
|
|
33
56
|
const portArg = impl.portArg || '--port'
|
|
34
57
|
const cmd = [
|
|
35
58
|
process.execPath,
|
|
36
59
|
entryPath,
|
|
37
60
|
portArg,
|
|
38
|
-
String(port),
|
|
61
|
+
port === 'auto' ? '${PORT}' : String(port),
|
|
39
62
|
...(impl.args || []),
|
|
40
63
|
]
|
|
41
64
|
|
|
65
|
+
/* keepAlive 由声明推导:restart.max===0 表示「不要自动重启」,对应 keepAlive:false。
|
|
66
|
+
* 旧实现恒 true,应用声明 restart:{max:0} 仍被 broker 拉起,切换/回收行为与声明不符。 */
|
|
67
|
+
const keepAlive = !(impl.restart && impl.restart.max === 0)
|
|
42
68
|
const serviceSpec = {
|
|
43
69
|
id: impl.id,
|
|
44
70
|
cmd,
|
|
45
71
|
ports: { main: port },
|
|
46
72
|
url: 'http://127.0.0.1:${PORT}/',
|
|
47
|
-
keepAlive
|
|
73
|
+
keepAlive,
|
|
48
74
|
}
|
|
49
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
|
|
50
80
|
if (impl.dir) {
|
|
51
81
|
serviceSpec.cwd = fileURLToPath(new URL(impl.dir, mountContext.context.baseUrl))
|
|
52
82
|
}
|
|
53
|
-
const env = impl.env?.({
|
|
83
|
+
const env = impl.env?.({
|
|
84
|
+
spec: mountContext.spec,
|
|
85
|
+
config: mountContext.config,
|
|
86
|
+
os: mountContext.context?.os,
|
|
87
|
+
})
|
|
54
88
|
if (env && typeof env === 'object') serviceSpec.env = env
|
|
55
89
|
|
|
56
|
-
let
|
|
90
|
+
let entered = false
|
|
91
|
+
let stopPromise
|
|
57
92
|
return {
|
|
58
93
|
serviceSpec,
|
|
59
|
-
async start() {
|
|
60
|
-
|
|
94
|
+
async start({ signal } = {}) {
|
|
95
|
+
entered = true
|
|
96
|
+
const result = await broker.start(serviceSpec, { signal })
|
|
61
97
|
const failure = (result?.errors || []).find((item) => item.id === impl.id)
|
|
62
98
|
if (failure) {
|
|
63
99
|
throw new AbiError(ERROR_CODES.E_ADAPTER_THREW,
|
|
@@ -66,13 +102,17 @@ export function createRuntimeDriverAdapter({ host, config, log }) {
|
|
|
66
102
|
log?.info?.(`[foundation] ${impl.id} 已由 broker 拉起(port ${port})`)
|
|
67
103
|
},
|
|
68
104
|
dispose() {
|
|
69
|
-
if (
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
105
|
+
if (!entered) return
|
|
106
|
+
if (!stopPromise) stopPromise = Promise.resolve().then(async () => {
|
|
107
|
+
const result = await broker.stop(impl.id)
|
|
108
|
+
if (result?.stopped === false) {
|
|
109
|
+
throw new AbiError(ERROR_CODES.E_ADAPTER_THREW,
|
|
110
|
+
`长驻进程 ${impl.id} 未停止:${result.reason || 'broker rejected stop'}`,
|
|
111
|
+
{ plugin: mountContext.spec.name, id: impl.id, result })
|
|
112
|
+
}
|
|
113
|
+
return result
|
|
114
|
+
})
|
|
115
|
+
return stopPromise
|
|
76
116
|
},
|
|
77
117
|
}
|
|
78
118
|
},
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* service-provider@1 适配器:向插件自己的 Cordis ctx 提供一个具名服务。
|
|
3
|
+
*
|
|
4
|
+
* 服务工厂必须同步;需要异步初始化的资源放在 start 钩子里。
|
|
5
|
+
* 注销顺序:先 stop(如果提供),再撤销 ctx.provide。
|
|
6
|
+
*/
|
|
7
|
+
import { AbiError, ERROR_CODES } from '../../abi/errors.mjs'
|
|
8
|
+
|
|
9
|
+
export const kind = 'service-provider@1'
|
|
10
|
+
|
|
11
|
+
const thenable = (value) => value && typeof value.then === 'function'
|
|
12
|
+
|
|
13
|
+
export function createServiceProviderAdapter({ log }) {
|
|
14
|
+
return {
|
|
15
|
+
kind,
|
|
16
|
+
compile(impl, { spec, config, context }) {
|
|
17
|
+
if (typeof impl.when === 'function') {
|
|
18
|
+
let enabled
|
|
19
|
+
try {
|
|
20
|
+
enabled = impl.when({ spec, config, log, os: context.os }) !== false
|
|
21
|
+
} catch (error) {
|
|
22
|
+
throw new AbiError(ERROR_CODES.E_ADAPTER_THREW,
|
|
23
|
+
`service-provider when() 失败:${error.message}`, { plugin: spec.name, service: impl.service, cause: error })
|
|
24
|
+
}
|
|
25
|
+
if (!enabled) {
|
|
26
|
+
log?.info?.(`[foundation] ${spec.name} 跳过 service-provider ${impl.service}(when=false)`)
|
|
27
|
+
return { skipped: true, dispose() {} }
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
const ctx = context?.ctx
|
|
31
|
+
if (!ctx || typeof ctx.provide !== 'function') {
|
|
32
|
+
throw new AbiError(ERROR_CODES.E_ADAPTER_THREW,
|
|
33
|
+
'service-provider@1 需要支持 ctx.provide 的 Cordis 宿主', { plugin: spec.name, service: impl.service })
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
let service
|
|
37
|
+
try {
|
|
38
|
+
service = impl.provide({ spec, config, log, os: context.os })
|
|
39
|
+
} catch (error) {
|
|
40
|
+
throw new AbiError(ERROR_CODES.E_ADAPTER_THREW,
|
|
41
|
+
`服务 ${impl.service} 构建失败:${error.message}`, { plugin: spec.name, service: impl.service, cause: error })
|
|
42
|
+
}
|
|
43
|
+
if (thenable(service)) {
|
|
44
|
+
/* 异步 provide 不被允许;但被拒的 promise 必须先接住,否则抛出
|
|
45
|
+
* E_PLUGIN_SHAPE 之外还泄漏一个 unhandledRejection(可崩宿主)。 */
|
|
46
|
+
service.catch(() => {})
|
|
47
|
+
throw new AbiError(ERROR_CODES.E_PLUGIN_SHAPE,
|
|
48
|
+
`服务 ${impl.service} 的 provide() 必须同步返回服务对象`, { plugin: spec.name, service: impl.service })
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
let withdraw
|
|
52
|
+
try {
|
|
53
|
+
withdraw = ctx.provide(impl.service, service)
|
|
54
|
+
} catch (error) {
|
|
55
|
+
throw new AbiError(ERROR_CODES.E_ADAPTER_THREW,
|
|
56
|
+
`注册服务 ${impl.service} 失败:${error.message}`, { plugin: spec.name, service: impl.service, cause: error })
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
let stopped = false
|
|
60
|
+
return {
|
|
61
|
+
service,
|
|
62
|
+
start: typeof impl.start === 'function'
|
|
63
|
+
? () => impl.start({ service, spec, config, log, os: context.os })
|
|
64
|
+
: undefined,
|
|
65
|
+
async dispose() {
|
|
66
|
+
if (stopped) return
|
|
67
|
+
stopped = true
|
|
68
|
+
try {
|
|
69
|
+
if (typeof impl.stop === 'function') await impl.stop({ service, spec, config, log, os: context.os })
|
|
70
|
+
} finally {
|
|
71
|
+
if (typeof withdraw === 'function') withdraw()
|
|
72
|
+
}
|
|
73
|
+
},
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
}
|
|
77
|
+
}
|
|
@@ -13,7 +13,7 @@ export const kind = 'tool-provider@1'
|
|
|
13
13
|
export function createToolProviderAdapter({ host, log }) {
|
|
14
14
|
return {
|
|
15
15
|
kind,
|
|
16
|
-
compile(impl, { spec, config }) {
|
|
16
|
+
compile(impl, { spec, config, context }) {
|
|
17
17
|
const tools = host.tools()
|
|
18
18
|
if (!tools?.register) {
|
|
19
19
|
throw new AbiError(ERROR_CODES.E_CAP_MISSING,
|
|
@@ -22,7 +22,7 @@ export function createToolProviderAdapter({ host, log }) {
|
|
|
22
22
|
|
|
23
23
|
let registry
|
|
24
24
|
try {
|
|
25
|
-
registry = impl.provide({ spec, config, log })
|
|
25
|
+
registry = impl.provide({ spec, config, log, os: context?.os })
|
|
26
26
|
} catch (error) {
|
|
27
27
|
throw new AbiError(ERROR_CODES.E_ADAPTER_THREW,
|
|
28
28
|
`工具注册表构建失败:${error.message}`, { plugin: spec.name, cause: error })
|
|
@@ -32,8 +32,15 @@ export function createToolProviderAdapter({ host, log }) {
|
|
|
32
32
|
'provide() 必须返回 { TOOLS, TOOL_IMPL } 形状的注册表', { plugin: spec.name })
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
-
|
|
36
|
-
|
|
35
|
+
/* prefixNamespace=false 用于工具名已经自带命名空间的存量注册表,
|
|
36
|
+
* 避免 browser_navigate 被注册成 browser_browser_navigate。 */
|
|
37
|
+
const namespace = impl.prefixNamespace === false ? '' : impl.namespace
|
|
38
|
+
const dispose = tools.register(registry, {
|
|
39
|
+
namespace,
|
|
40
|
+
instructions: impl.instructions,
|
|
41
|
+
capabilities: impl.capabilities,
|
|
42
|
+
})
|
|
43
|
+
log?.debug?.(`[foundation] ${spec.name} 注册 ${registry.TOOLS.length} 个工具(${namespace || '无前缀'})`)
|
|
37
44
|
return {
|
|
38
45
|
registry,
|
|
39
46
|
dispose: typeof dispose === 'function' ? dispose : () => {},
|
|
@@ -29,17 +29,20 @@ export function createUiPanelAdapter({ host, net, log }) {
|
|
|
29
29
|
const editor = editorHost.createEditorHost({
|
|
30
30
|
id: spec.name,
|
|
31
31
|
dir,
|
|
32
|
-
|
|
32
|
+
// shared-host 已剥离挂载前缀,子 app 中的路由使用相对根路径。
|
|
33
|
+
basePath: '',
|
|
33
34
|
index: impl.index,
|
|
34
35
|
})
|
|
35
36
|
|
|
36
37
|
const app = serviceHost.createApp({ id: spec.name, title: spec.name })
|
|
37
|
-
for (const route of editor.routes()) app.use(route)
|
|
38
|
-
|
|
39
38
|
let unmount
|
|
40
39
|
try {
|
|
40
|
+
for (const route of editor.routes()) app.use(route)
|
|
41
41
|
unmount = net.mount(impl.mount, app)
|
|
42
42
|
} catch (error) {
|
|
43
|
+
/* 路由装配/挂载失败时 app 已创建:此 entry 尚未入 loader,回滚够不到,
|
|
44
|
+
* 就地关闭 app(editor 为静态宿主无 close,解引用后由 GC 回收)。 */
|
|
45
|
+
try { app.close?.() } catch { /* 尽力回收 */ }
|
|
43
46
|
throw new AbiError(ERROR_CODES.E_ADAPTER_THREW,
|
|
44
47
|
`挂载面板 ${impl.mount} 失败:${error.message}`, { plugin: spec.name, prefix: impl.mount, cause: error })
|
|
45
48
|
}
|
|
@@ -48,8 +51,12 @@ export function createUiPanelAdapter({ host, net, log }) {
|
|
|
48
51
|
return {
|
|
49
52
|
app,
|
|
50
53
|
editor,
|
|
54
|
+
start: () => net.ready(),
|
|
51
55
|
dispose: () => {
|
|
52
|
-
|
|
56
|
+
const errors = []
|
|
57
|
+
try { if (typeof unmount === 'function') unmount() } catch (error) { errors.push(error) }
|
|
58
|
+
try { app.close?.() } catch (error) { errors.push(error) }
|
|
59
|
+
if (errors.length) throw errors[0]
|
|
53
60
|
},
|
|
54
61
|
}
|
|
55
62
|
},
|
package/lib/ctx-service.mjs
CHANGED
|
@@ -13,9 +13,30 @@ export function getService(ctx, name) {
|
|
|
13
13
|
if (service !== undefined) return service
|
|
14
14
|
} catch { /* 继续尝试属性访问 */ }
|
|
15
15
|
}
|
|
16
|
+
const shared = findServiceByStore(ctx, name)
|
|
17
|
+
if (shared !== undefined) return shared
|
|
16
18
|
try {
|
|
17
19
|
return ctx[name]
|
|
18
20
|
} catch {
|
|
19
21
|
return undefined
|
|
20
22
|
}
|
|
21
23
|
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* 跨隔离查找服务实例:Cordis 的 `ctx.get(name)` 只认「本层隔离键」指向的实现,
|
|
27
|
+
* 同一个服务若在别的隔离分支里提供,就永远查不到(桌面宿主里的 webServer 就是这样:
|
|
28
|
+
* `ctx.get('webServer')` 恒 undefined,而服务本身活得好好的)。
|
|
29
|
+
* store 里的实现项都带 `name`,按名字扫一遍即可拿到同一个实例。
|
|
30
|
+
*/
|
|
31
|
+
export function findServiceByStore(ctx, name) {
|
|
32
|
+
try {
|
|
33
|
+
const store = ctx?.root?.store
|
|
34
|
+
if (!store || typeof store !== 'object') return undefined
|
|
35
|
+
// store 的键是隔离用的 Symbol,Object.values 看不到它们,必须走 Reflect.ownKeys。
|
|
36
|
+
for (const key of Reflect.ownKeys(store)) {
|
|
37
|
+
const impl = store[key]
|
|
38
|
+
if (impl?.name === name && impl.value !== undefined && impl.value !== null) return impl.value
|
|
39
|
+
}
|
|
40
|
+
} catch { /* 结构不同就当没有,交给调用方的兜底 */ }
|
|
41
|
+
return undefined
|
|
42
|
+
}
|
package/lib/host/caps.mjs
CHANGED
|
@@ -16,11 +16,19 @@ export const BASE_CAPABILITIES = Object.freeze({
|
|
|
16
16
|
/** dsh 宿主:按 ctx 里实际存在的服务生成能力表。 */
|
|
17
17
|
export function detectDshCapabilities(ctx) {
|
|
18
18
|
const capabilities = { ...BASE_CAPABILITIES }
|
|
19
|
-
|
|
19
|
+
const hasMethods = (service, ...methods) => methods.every((method) => typeof service?.[method] === 'function')
|
|
20
|
+
if (hasMethods(getService(ctx, 'skills'), 'registerProvider')) capabilities['os.skills'] = '1.0.0'
|
|
20
21
|
const serviceHost = getService(ctx, 'serviceHost')
|
|
21
|
-
if (serviceHost
|
|
22
|
-
|
|
23
|
-
if (getService(ctx, '
|
|
24
|
-
if (getService(ctx, '
|
|
22
|
+
if (hasMethods(serviceHost, 'createSharedHost', 'createApp')) capabilities['os.net'] = '1.0.0'
|
|
23
|
+
// 宿主网页端口本身(dsh 的 ctx.webServer)。有它就不该再另起端口。
|
|
24
|
+
if (hasMethods(getService(ctx, 'webServer'), 'register')) capabilities['os.web'] = '1.0.0'
|
|
25
|
+
if (hasMethods(getService(ctx, 'toolHost'), 'registerWithDsh')
|
|
26
|
+
&& hasMethods(getService(ctx, 'tools'), 'register')) capabilities['os.tools'] = '1.0.0'
|
|
27
|
+
if (hasMethods(getService(ctx, 'serviceBroker'), 'start', 'stop')) capabilities['os.proc'] = '1.0.0'
|
|
28
|
+
if (capabilities['os.net'] && hasMethods(getService(ctx, 'editorHost'), 'createEditorHost')) capabilities['os.ui'] = '1.0.0'
|
|
29
|
+
if (hasMethods(getService(ctx, 'pluginManager'), 'listBundles', 'listPlugins', 'setBundleEnabled', 'setPluginEnabled')) {
|
|
30
|
+
capabilities['os.plugins'] = '1.0.0'
|
|
31
|
+
}
|
|
32
|
+
if (hasMethods(getService(ctx, 'assets'), 'put', 'get')) capabilities['os.assets'] = '1.0.0'
|
|
25
33
|
return capabilities
|
|
26
34
|
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/** Executable Host v1 boundary; validation never registers, listens or starts resources. */
|
|
2
|
+
import { parseVersion } from '../../abi/version.mjs'
|
|
3
|
+
import { AbiError, ERROR_CODES } from '../../abi/errors.mjs'
|
|
4
|
+
|
|
5
|
+
const ACCESSORS = ['capabilities', 'log', 'skills', 'tools', 'http', 'proc', 'ui']
|
|
6
|
+
const SERVICES = {
|
|
7
|
+
'os.skills': ['skills', ['registerProvider']],
|
|
8
|
+
'os.tools': ['tools', ['register']],
|
|
9
|
+
'os.net': ['http', ['createApp', 'createSharedHost']],
|
|
10
|
+
'os.web': ['web', ['register']],
|
|
11
|
+
'os.proc': ['proc', ['start', 'stop']],
|
|
12
|
+
'os.ui': ['ui', ['createEditorHost']],
|
|
13
|
+
'os.plugins': ['plugins', ['listBundles', 'listPlugins', 'setBundleEnabled', 'setPluginEnabled']],
|
|
14
|
+
'os.assets': ['assets', ['put', 'get']],
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function invalid(path, expectation) {
|
|
18
|
+
/* 归一成 AbiError(稳定 code + detail),不再让裸 TypeError 穿透 mount/report。
|
|
19
|
+
* message 保留 path+expectation 原文,既有按文案断言的测试不受影响。 */
|
|
20
|
+
throw new AbiError(ERROR_CODES.E_KERNEL_MISSING, `Host v1 contract: ${path} ${expectation}`, { contract: 'host-v1', path, expectation })
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function record(value) {
|
|
24
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function version(value, path) {
|
|
28
|
+
// Use the same version vocabulary as ABI capability matching, but require a
|
|
29
|
+
// complete version rather than accepting ranges or abbreviated declarations.
|
|
30
|
+
const parsed = parseVersion(value)
|
|
31
|
+
if (!parsed || !/^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/.test(value)) {
|
|
32
|
+
invalid(path, 'must be a version such as 1.0.0 (not a range)')
|
|
33
|
+
}
|
|
34
|
+
return parsed
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Validate a live capability snapshot and its advertised service methods.
|
|
39
|
+
* Returns host unchanged; throws AbiError (E_KERNEL_MISSING) with the failing path. All accessors
|
|
40
|
+
* are synchronous, side-effect-free lookups. Unadvertised optional services are
|
|
41
|
+
* not probed, so late service injection remains possible. Call at construction
|
|
42
|
+
* and before consuming a fresh capabilities() snapshot, not just once at boot.
|
|
43
|
+
* Resource-return values/disposers are checked by the shared behavior suite.
|
|
44
|
+
*/
|
|
45
|
+
export function assertHostContract(host) {
|
|
46
|
+
if (!record(host)) invalid('host', 'must be an object')
|
|
47
|
+
if (typeof host.kind !== 'string' || !host.kind.trim()) invalid('host.kind', 'must be a non-empty string')
|
|
48
|
+
if (version(host.version, 'host.version')[0] !== 1) invalid('host.version', 'must be compatible with major version 1')
|
|
49
|
+
for (const method of ACCESSORS) {
|
|
50
|
+
if (typeof host[method] !== 'function') invalid(`host.${method}`, 'must be a function')
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const capabilities = host.capabilities()
|
|
54
|
+
if (!record(capabilities) || typeof capabilities.then === 'function') {
|
|
55
|
+
invalid('host.capabilities()', 'must synchronously return a capability map')
|
|
56
|
+
}
|
|
57
|
+
for (const [name, value] of Object.entries(capabilities)) {
|
|
58
|
+
if (!/^os\.[A-Za-z][A-Za-z0-9_.-]*$/.test(name)) invalid(`capabilities[${JSON.stringify(name)}]`, 'must have an os.* name')
|
|
59
|
+
version(value, `capabilities[${JSON.stringify(name)}]`)
|
|
60
|
+
}
|
|
61
|
+
if (Object.hasOwn(capabilities, 'os.ui') && !Object.hasOwn(capabilities, 'os.net')) {
|
|
62
|
+
invalid('capabilities.os.ui', 'requires os.net and its HTTP service')
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const logger = host.log()
|
|
66
|
+
if (logger != null) {
|
|
67
|
+
for (const method of ['debug', 'info', 'warn', 'error']) {
|
|
68
|
+
if (typeof logger[method] !== 'function') invalid(`host.log().${method}`, 'must be a function')
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
for (const [capability, [accessor, methods]] of Object.entries(SERVICES)) {
|
|
72
|
+
if (!Object.hasOwn(capabilities, capability)) continue
|
|
73
|
+
if (typeof host[accessor] !== 'function') {
|
|
74
|
+
invalid(`host.${accessor}`, `must be a function when advertising ${capability}`)
|
|
75
|
+
}
|
|
76
|
+
const service = host[accessor]()
|
|
77
|
+
if (!record(service) || typeof service.then === 'function') {
|
|
78
|
+
invalid(`host.${accessor}()`, `must synchronously return a service when advertising ${capability}`)
|
|
79
|
+
}
|
|
80
|
+
for (const method of methods) {
|
|
81
|
+
if (typeof service[method] !== 'function') invalid(`host.${accessor}().${method}`, `must be a function when advertising ${capability}`)
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return host
|
|
85
|
+
}
|
package/lib/host/dsh.mjs
CHANGED
|
@@ -12,8 +12,12 @@
|
|
|
12
12
|
* skills() { registerProvider(factory) => dispose }
|
|
13
13
|
* tools() { registerWithDsh(ctx, registry, { namespace }) => dispose }
|
|
14
14
|
* http() { createApp({id}), createSharedHost({id}) }
|
|
15
|
+
* web() { register({kind, path, handler}) => dispose, port } | undefined
|
|
15
16
|
* proc() { start(serviceSpec) => Promise<{errors?}>, stop(id) }
|
|
16
17
|
* ui() { createEditorHost({ id, dir, basePath, index }) }
|
|
18
|
+
*
|
|
19
|
+
* `web()` 是宿主网页端口本身(dsh 即 ctx.webServer)。拿到它时,内核一律把 HTTP
|
|
20
|
+
* 挂到该端口的路径前缀下,不再另起端口;拿不到才退回 os.net 的独立监听。
|
|
17
21
|
*/
|
|
18
22
|
import { getService } from '../ctx-service.mjs'
|
|
19
23
|
import { detectDshCapabilities } from './caps.mjs'
|
|
@@ -31,11 +35,16 @@ export function createDshHost(ctx) {
|
|
|
31
35
|
const toolHost = getService(ctx, 'toolHost')
|
|
32
36
|
if (!toolHost?.registerWithDsh) return null
|
|
33
37
|
return {
|
|
34
|
-
|
|
38
|
+
// tool-host reads ctx.tools directly. Pass only the resolved native tool
|
|
39
|
+
// service, not the foundation fiber's non-injected Cordis proxy.
|
|
40
|
+
register: (registry, options) => toolHost.registerWithDsh({ tools: getService(ctx, 'tools') }, registry, options),
|
|
35
41
|
}
|
|
36
42
|
},
|
|
37
43
|
http: () => getService(ctx, 'serviceHost'),
|
|
44
|
+
web: () => getService(ctx, 'webServer'),
|
|
38
45
|
proc: () => getService(ctx, 'serviceBroker'),
|
|
39
46
|
ui: () => getService(ctx, 'editorHost'),
|
|
47
|
+
plugins: () => getService(ctx, 'pluginManager'),
|
|
48
|
+
assets: () => getService(ctx, 'assets'),
|
|
40
49
|
}
|
|
41
50
|
}
|
package/lib/host/memory.mjs
CHANGED
|
@@ -11,15 +11,26 @@ 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: [],
|
|
17
21
|
tools: [],
|
|
18
22
|
apps: [],
|
|
19
23
|
mounts: new Map(),
|
|
24
|
+
web: [],
|
|
20
25
|
services: new Map(),
|
|
26
|
+
assets: new Map(),
|
|
27
|
+
bundles: [],
|
|
28
|
+
plugins: [],
|
|
21
29
|
}
|
|
22
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
|
|
23
34
|
|
|
24
35
|
function http() {
|
|
25
36
|
return {
|
|
@@ -37,6 +48,11 @@ export function createMemoryHost(options = {}) {
|
|
|
37
48
|
}
|
|
38
49
|
},
|
|
39
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
|
+
},
|
|
40
56
|
}
|
|
41
57
|
state.apps.push(app)
|
|
42
58
|
return app
|
|
@@ -57,6 +73,21 @@ export function createMemoryHost(options = {}) {
|
|
|
57
73
|
}
|
|
58
74
|
}
|
|
59
75
|
|
|
76
|
+
/** 参考宿主的「网页端口」:内存里只记录注册,证明内核走了同源前缀而非新端口。 */
|
|
77
|
+
function web() {
|
|
78
|
+
return {
|
|
79
|
+
port: 0,
|
|
80
|
+
register({ kind, path, handler } = {}) {
|
|
81
|
+
const entry = { kind, path, handler }
|
|
82
|
+
state.web.push(entry)
|
|
83
|
+
return () => {
|
|
84
|
+
const index = state.web.indexOf(entry)
|
|
85
|
+
if (index >= 0) state.web.splice(index, 1)
|
|
86
|
+
}
|
|
87
|
+
},
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
60
91
|
return {
|
|
61
92
|
kind: 'memory',
|
|
62
93
|
version: '1.0.0',
|
|
@@ -65,9 +96,12 @@ export function createMemoryHost(options = {}) {
|
|
|
65
96
|
...BASE_CAPABILITIES,
|
|
66
97
|
'os.skills': '1.0.0',
|
|
67
98
|
'os.net': '1.0.0',
|
|
99
|
+
'os.web': '1.0.0',
|
|
68
100
|
'os.tools': '1.0.0',
|
|
69
101
|
'os.proc': '1.0.0',
|
|
70
102
|
'os.ui': '1.0.0',
|
|
103
|
+
'os.plugins': '1.0.0',
|
|
104
|
+
'os.assets': '1.0.0',
|
|
71
105
|
}),
|
|
72
106
|
log: () => log,
|
|
73
107
|
skills: () => ({
|
|
@@ -91,6 +125,7 @@ export function createMemoryHost(options = {}) {
|
|
|
91
125
|
},
|
|
92
126
|
}),
|
|
93
127
|
http,
|
|
128
|
+
web,
|
|
94
129
|
proc: () => ({
|
|
95
130
|
async start(serviceSpec) {
|
|
96
131
|
state.services.set(serviceSpec.id, serviceSpec)
|
|
@@ -112,5 +147,41 @@ export function createMemoryHost(options = {}) {
|
|
|
112
147
|
}
|
|
113
148
|
},
|
|
114
149
|
}),
|
|
150
|
+
plugins: () => ({
|
|
151
|
+
async listBundles() { return [...state.bundles] },
|
|
152
|
+
async listPlugins() { return [...state.plugins] },
|
|
153
|
+
async setBundleEnabled(name, enabled) {
|
|
154
|
+
state.bundles.push({ name, enabled, application: 'applied', changed: true })
|
|
155
|
+
return { application: 'applied', changed: true }
|
|
156
|
+
},
|
|
157
|
+
async setPluginEnabled(entryId, enabled) {
|
|
158
|
+
state.plugins.push({ entryId, enabled, application: 'applied', changed: true })
|
|
159
|
+
return { application: 'applied', changed: true }
|
|
160
|
+
},
|
|
161
|
+
}),
|
|
162
|
+
assets: () => ({
|
|
163
|
+
async put(value, contentType = 'application/octet-stream') {
|
|
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}`
|
|
171
|
+
state.assets.set(id, { data: buffer, contentType })
|
|
172
|
+
return { id, url: `/assets/${id}`, size: buffer.length, contentType }
|
|
173
|
+
},
|
|
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
|
+
},
|
|
185
|
+
}),
|
|
115
186
|
}
|
|
116
187
|
}
|
package/lib/host.mjs
CHANGED
|
@@ -12,11 +12,12 @@ export function apply(ctx, config = {}) {
|
|
|
12
12
|
const kernel = createKernel(ctx, config)
|
|
13
13
|
const dispose = ctx.provide('os', kernel.os)
|
|
14
14
|
console.log('[foundation] ready(abi v' + kernel.os.abi + ')')
|
|
15
|
+
let cleanup
|
|
15
16
|
return () => {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
17
|
+
if (cleanup) return cleanup
|
|
18
|
+
// Withdraw the service immediately; retained references reject new mounts.
|
|
19
|
+
cleanup = kernel.dispose()
|
|
20
|
+
if (typeof dispose === 'function') dispose()
|
|
21
|
+
return cleanup
|
|
21
22
|
}
|
|
22
23
|
}
|