@capyclaw/gateway 0.0.0-stage → 0.1.2
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 +89 -2
- package/cordis.patch.yml +4 -0
- package/lib/http.mjs +101 -0
- package/lib/index.mjs +714 -0
- package/lib/paths.mjs +171 -0
- package/lib/store.mjs +193 -0
- package/lib/upstream.mjs +111 -0
- package/node_modules/@capyclaw/foundation/README.md +51 -0
- package/node_modules/@capyclaw/foundation/abi/assets.mjs +26 -0
- package/node_modules/@capyclaw/foundation/abi/bridge.mjs +88 -0
- package/node_modules/@capyclaw/foundation/abi/errors.mjs +63 -0
- package/node_modules/@capyclaw/foundation/abi/index.mjs +50 -0
- package/node_modules/@capyclaw/foundation/abi/kinds.mjs +271 -0
- package/node_modules/@capyclaw/foundation/abi/loader.mjs +352 -0
- package/node_modules/@capyclaw/foundation/abi/schema.mjs +98 -0
- package/node_modules/@capyclaw/foundation/abi/spec.mjs +258 -0
- package/node_modules/@capyclaw/foundation/abi/validate.mjs +24 -0
- package/node_modules/@capyclaw/foundation/abi/version.mjs +140 -0
- package/node_modules/@capyclaw/foundation/abi/visibility.mjs +90 -0
- package/node_modules/@capyclaw/foundation/cordis.patch.yml +5 -0
- package/node_modules/@capyclaw/foundation/kit/index.mjs +6 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/client-panel.mjs +98 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/host-event.mjs +35 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/http-router.mjs +69 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/lifecycle-hook.mjs +72 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/own-server.mjs +113 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/raw.mjs +69 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/runtime-driver.mjs +120 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/service-provider.mjs +77 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/skill-provider.mjs +56 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/tool-provider.mjs +50 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/ui-panel.mjs +64 -0
- package/node_modules/@capyclaw/foundation/lib/ctx-service.mjs +42 -0
- package/node_modules/@capyclaw/foundation/lib/host/caps.mjs +38 -0
- package/node_modules/@capyclaw/foundation/lib/host/contract.mjs +88 -0
- package/node_modules/@capyclaw/foundation/lib/host/dsh.mjs +54 -0
- package/node_modules/@capyclaw/foundation/lib/host/memory.mjs +187 -0
- package/node_modules/@capyclaw/foundation/lib/host.mjs +23 -0
- package/node_modules/@capyclaw/foundation/lib/kernel.mjs +195 -0
- package/node_modules/@capyclaw/foundation/lib/skill/scan.mjs +115 -0
- package/node_modules/@capyclaw/foundation/lib/subsystems/events.mjs +60 -0
- package/node_modules/@capyclaw/foundation/lib/subsystems/log.mjs +51 -0
- package/node_modules/@capyclaw/foundation/lib/subsystems/net.mjs +198 -0
- package/node_modules/@capyclaw/foundation/package.json +53 -0
- package/node_modules/@capyclaw/foundation/runtime/index.mjs +20 -0
- 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 +71 -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 +132 -0
- package/node_modules/@capyclaw/plugin-kit/src/editor/heartbeat.mjs +26 -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/service/static.mjs +79 -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 +664 -0
- package/node_modules/@capyclaw/plugin-kit/src/zip.mjs +181 -0
- package/package.json +61 -4
- package/plugin.mjs +329 -0
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ui-panel@1 适配器:把静态目录挂到共享宿主的挂载点。
|
|
3
|
+
*
|
|
4
|
+
* 文件读取、路径穿越防护、缓存头由 editor-host 负责;内核只负责解析目录与挂载。
|
|
5
|
+
*/
|
|
6
|
+
import { fileURLToPath } from 'node:url'
|
|
7
|
+
|
|
8
|
+
import { AbiError, ERROR_CODES } from '../../abi/errors.mjs'
|
|
9
|
+
|
|
10
|
+
export const kind = 'ui-panel@1'
|
|
11
|
+
|
|
12
|
+
export function createUiPanelAdapter({ host, net, log }) {
|
|
13
|
+
return {
|
|
14
|
+
kind,
|
|
15
|
+
compile(impl, { spec, config, context }) {
|
|
16
|
+
const serviceHost = host.http()
|
|
17
|
+
const editorHost = host.ui()
|
|
18
|
+
if (!serviceHost?.createApp || !editorHost?.createEditorHost) {
|
|
19
|
+
throw new AbiError(ERROR_CODES.E_CAP_MISSING,
|
|
20
|
+
'ctx.editorHost / ctx.serviceHost 不可用:无法挂静态面板',
|
|
21
|
+
{ plugin: spec.name, capability: 'os.ui' })
|
|
22
|
+
}
|
|
23
|
+
if (!context?.baseUrl) {
|
|
24
|
+
throw new AbiError(ERROR_CODES.E_BASE_URL_MISSING,
|
|
25
|
+
`插件 ${spec.name} 缺少 baseUrl,无法解析 ${impl.root}`, { plugin: spec.name })
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const dir = fileURLToPath(new URL(impl.root, context.baseUrl))
|
|
29
|
+
const editor = editorHost.createEditorHost({
|
|
30
|
+
id: spec.name,
|
|
31
|
+
dir,
|
|
32
|
+
// shared-host 已剥离挂载前缀,子 app 中的路由使用相对根路径。
|
|
33
|
+
basePath: '',
|
|
34
|
+
index: impl.index,
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
const app = serviceHost.createApp({ id: spec.name, title: spec.name })
|
|
38
|
+
let unmount
|
|
39
|
+
try {
|
|
40
|
+
for (const route of editor.routes()) app.use(route)
|
|
41
|
+
unmount = net.mount(impl.mount, app)
|
|
42
|
+
} catch (error) {
|
|
43
|
+
/* 路由装配/挂载失败时 app 已创建:此 entry 尚未入 loader,回滚够不到,
|
|
44
|
+
* 就地关闭 app(editor 为静态宿主无 close,解引用后由 GC 回收)。 */
|
|
45
|
+
try { app.close?.() } catch { /* 尽力回收 */ }
|
|
46
|
+
throw new AbiError(ERROR_CODES.E_ADAPTER_THREW,
|
|
47
|
+
`挂载面板 ${impl.mount} 失败:${error.message}`, { plugin: spec.name, prefix: impl.mount, cause: error })
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
log?.debug?.(`[foundation] ${spec.name} 面板挂到 ${impl.mount}`)
|
|
51
|
+
return {
|
|
52
|
+
app,
|
|
53
|
+
editor,
|
|
54
|
+
start: () => net.ready(),
|
|
55
|
+
dispose: () => {
|
|
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]
|
|
60
|
+
},
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
}
|
|
64
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 服务探测:Cordis 的 ctx 是 Proxy,读取未在 `inject` 声明的服务会直接抛
|
|
3
|
+
* `cannot get property "x" without inject`。内核需要在 apply 时探测「哪些服务在场」,
|
|
4
|
+
* 因此统一走 `ctx.get(name)`(Cordis 官方 API:读取服务但不要求 inject,未提供返回 undefined)。
|
|
5
|
+
*
|
|
6
|
+
* 纯对象 ctx(测试、宿主)没有 get(),退化为直接属性访问;两种情况都不抛。
|
|
7
|
+
*/
|
|
8
|
+
export function getService(ctx, name) {
|
|
9
|
+
if (!ctx) return undefined
|
|
10
|
+
if (typeof ctx.get === 'function') {
|
|
11
|
+
try {
|
|
12
|
+
const service = ctx.get(name)
|
|
13
|
+
if (service !== undefined) return service
|
|
14
|
+
} catch { /* 继续尝试属性访问 */ }
|
|
15
|
+
}
|
|
16
|
+
const shared = findServiceByStore(ctx, name)
|
|
17
|
+
if (shared !== undefined) return shared
|
|
18
|
+
try {
|
|
19
|
+
return ctx[name]
|
|
20
|
+
} catch {
|
|
21
|
+
return undefined
|
|
22
|
+
}
|
|
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
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 宿主能力表:内核承诺给应用插件的 os.* 能力。
|
|
3
|
+
*
|
|
4
|
+
* BASE 是内核自身就能提供的;其余取决于宿主实际接入了哪些服务。
|
|
5
|
+
* 这里是唯一允许出现宿主服务名(skills / serviceHost / toolHost / serviceBroker /
|
|
6
|
+
* editorHost)的地方——它们属于 dsh 适配层,不属于 ABI。
|
|
7
|
+
*/
|
|
8
|
+
import { getService } from '../ctx-service.mjs'
|
|
9
|
+
import { HOST_SERVICE_CONTRACT } from './contract.mjs'
|
|
10
|
+
|
|
11
|
+
export const BASE_CAPABILITIES = Object.freeze({
|
|
12
|
+
'os.events': '1.0.0',
|
|
13
|
+
'os.log': '1.0.0',
|
|
14
|
+
'os.caps': '1.0.0',
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
/** dsh 宿主:按 ctx 里实际存在的服务生成能力表。 */
|
|
18
|
+
export function detectDshCapabilities(ctx) {
|
|
19
|
+
const capabilities = { ...BASE_CAPABILITIES }
|
|
20
|
+
const hasMethods = (service, ...methods) => methods.every((method) => typeof service?.[method] === 'function')
|
|
21
|
+
if (hasMethods(getService(ctx, 'skills'), 'registerProvider')) capabilities['os.skills'] = '1.0.0'
|
|
22
|
+
const serviceHost = getService(ctx, 'serviceHost')
|
|
23
|
+
if (hasMethods(serviceHost, 'createSharedHost', 'createApp')) capabilities['os.net'] = '1.0.0'
|
|
24
|
+
// 宿主网页端口本身(dsh 的 ctx.webServer)。有它就不该再另起端口。
|
|
25
|
+
if (hasMethods(getService(ctx, 'webServer'), 'register')) capabilities['os.web'] = '1.0.0'
|
|
26
|
+
if (hasMethods(getService(ctx, 'toolHost'), 'registerWithDsh')
|
|
27
|
+
&& hasMethods(getService(ctx, 'tools'), 'register')) capabilities['os.tools'] = '1.0.0'
|
|
28
|
+
if (hasMethods(getService(ctx, 'serviceBroker'), 'start', 'stop')) capabilities['os.proc'] = '1.0.0'
|
|
29
|
+
if (capabilities['os.net'] && hasMethods(getService(ctx, 'editorHost'), 'createEditorHost')) capabilities['os.ui'] = '1.0.0'
|
|
30
|
+
if (hasMethods(getService(ctx, 'pluginManager'), ...HOST_SERVICE_CONTRACT['os.plugins'][1])) {
|
|
31
|
+
capabilities['os.plugins'] = '1.0.0'
|
|
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'
|
|
37
|
+
return capabilities
|
|
38
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
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
|
+
/** Host v1 能力→[访问器, 必需方法] 契约。导出为单一真源:内核门面与能力探测都引用它,
|
|
7
|
+
* 不再各自硬编码 os.plugins 的 dsh 方法名(listBundles/setPluginEnabled…)。 */
|
|
8
|
+
export const HOST_SERVICE_CONTRACT = {
|
|
9
|
+
'os.skills': ['skills', ['registerProvider']],
|
|
10
|
+
'os.tools': ['tools', ['register']],
|
|
11
|
+
'os.net': ['http', ['createApp', 'createSharedHost']],
|
|
12
|
+
'os.web': ['web', ['register']],
|
|
13
|
+
'os.proc': ['proc', ['start', 'stop']],
|
|
14
|
+
'os.ui': ['ui', ['createEditorHost']],
|
|
15
|
+
'os.plugins': ['plugins', ['listBundles', 'listPlugins', 'setBundleEnabled', 'setPluginEnabled']],
|
|
16
|
+
'os.assets': ['assets', ['put', 'get']],
|
|
17
|
+
'os.works': ['works', ['backendFor']],
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function invalid(path, expectation) {
|
|
21
|
+
/* 归一成 AbiError(稳定 code + detail),不再让裸 TypeError 穿透 mount/report。
|
|
22
|
+
* message 保留 path+expectation 原文,既有按文案断言的测试不受影响。 */
|
|
23
|
+
throw new AbiError(ERROR_CODES.E_KERNEL_MISSING, `Host v1 contract: ${path} ${expectation}`, { contract: 'host-v1', path, expectation })
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function record(value) {
|
|
27
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function version(value, path) {
|
|
31
|
+
// Use the same version vocabulary as ABI capability matching, but require a
|
|
32
|
+
// complete version rather than accepting ranges or abbreviated declarations.
|
|
33
|
+
const parsed = parseVersion(value)
|
|
34
|
+
if (!parsed || !/^\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?$/.test(value)) {
|
|
35
|
+
invalid(path, 'must be a version such as 1.0.0 (not a range)')
|
|
36
|
+
}
|
|
37
|
+
return parsed
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Validate a live capability snapshot and its advertised service methods.
|
|
42
|
+
* Returns host unchanged; throws AbiError (E_KERNEL_MISSING) with the failing path. All accessors
|
|
43
|
+
* are synchronous, side-effect-free lookups. Unadvertised optional services are
|
|
44
|
+
* not probed, so late service injection remains possible. Call at construction
|
|
45
|
+
* and before consuming a fresh capabilities() snapshot, not just once at boot.
|
|
46
|
+
* Resource-return values/disposers are checked by the shared behavior suite.
|
|
47
|
+
*/
|
|
48
|
+
export function assertHostContract(host) {
|
|
49
|
+
if (!record(host)) invalid('host', 'must be an object')
|
|
50
|
+
if (typeof host.kind !== 'string' || !host.kind.trim()) invalid('host.kind', 'must be a non-empty string')
|
|
51
|
+
if (version(host.version, 'host.version')[0] !== 1) invalid('host.version', 'must be compatible with major version 1')
|
|
52
|
+
for (const method of ACCESSORS) {
|
|
53
|
+
if (typeof host[method] !== 'function') invalid(`host.${method}`, 'must be a function')
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const capabilities = host.capabilities()
|
|
57
|
+
if (!record(capabilities) || typeof capabilities.then === 'function') {
|
|
58
|
+
invalid('host.capabilities()', 'must synchronously return a capability map')
|
|
59
|
+
}
|
|
60
|
+
for (const [name, value] of Object.entries(capabilities)) {
|
|
61
|
+
if (!/^os\.[A-Za-z][A-Za-z0-9_.-]*$/.test(name)) invalid(`capabilities[${JSON.stringify(name)}]`, 'must have an os.* name')
|
|
62
|
+
version(value, `capabilities[${JSON.stringify(name)}]`)
|
|
63
|
+
}
|
|
64
|
+
if (Object.hasOwn(capabilities, 'os.ui') && !Object.hasOwn(capabilities, 'os.net')) {
|
|
65
|
+
invalid('capabilities.os.ui', 'requires os.net and its HTTP service')
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const logger = host.log()
|
|
69
|
+
if (logger != null) {
|
|
70
|
+
for (const method of ['debug', 'info', 'warn', 'error']) {
|
|
71
|
+
if (typeof logger[method] !== 'function') invalid(`host.log().${method}`, 'must be a function')
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
for (const [capability, [accessor, methods]] of Object.entries(HOST_SERVICE_CONTRACT)) {
|
|
75
|
+
if (!Object.hasOwn(capabilities, capability)) continue
|
|
76
|
+
if (typeof host[accessor] !== 'function') {
|
|
77
|
+
invalid(`host.${accessor}`, `must be a function when advertising ${capability}`)
|
|
78
|
+
}
|
|
79
|
+
const service = host[accessor]()
|
|
80
|
+
if (!record(service) || typeof service.then === 'function') {
|
|
81
|
+
invalid(`host.${accessor}()`, `must synchronously return a service when advertising ${capability}`)
|
|
82
|
+
}
|
|
83
|
+
for (const method of methods) {
|
|
84
|
+
if (typeof service[method] !== 'function') invalid(`host.${accessor}().${method}`, `must be a function when advertising ${capability}`)
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return host
|
|
88
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh 宿主适配器:把 Cordis ctx 翻译成内核认识的 Host 接口。
|
|
3
|
+
*
|
|
4
|
+
* 这是整个 foundation 里**唯一**知道 dsh 服务名的地方。换一个 agent 运行时,
|
|
5
|
+
* 只需要照着 Host 接口写一个新的适配器(lib/host/<runtime>.mjs),
|
|
6
|
+
* 内核、ABI 与应用插件都不改。
|
|
7
|
+
*
|
|
8
|
+
* Host 接口(v1):
|
|
9
|
+
* kind 宿主标识
|
|
10
|
+
* capabilities() 返回 { 'os.xxx': '1.0.0' },用于 mount 前的能力校验
|
|
11
|
+
* log() 返回 { debug/info/warn/error } 或 undefined
|
|
12
|
+
* skills() { registerProvider(factory) => dispose }
|
|
13
|
+
* tools() { registerWithDsh(ctx, registry, { namespace }) => dispose }
|
|
14
|
+
* http() { createApp({id}), createSharedHost({id}) }
|
|
15
|
+
* web() { register({kind, path, handler}) => dispose, port } | undefined
|
|
16
|
+
* proc() { start(serviceSpec) => Promise<{errors?}>, stop(id) }
|
|
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,应用自行降级内存态)
|
|
21
|
+
*
|
|
22
|
+
* `web()` 是宿主网页端口本身(dsh 即 ctx.webServer)。拿到它时,内核一律把 HTTP
|
|
23
|
+
* 挂到该端口的路径前缀下,不再另起端口;拿不到才退回 os.net 的独立监听。
|
|
24
|
+
*/
|
|
25
|
+
import { getService } from '../ctx-service.mjs'
|
|
26
|
+
import { detectDshCapabilities } from './caps.mjs'
|
|
27
|
+
|
|
28
|
+
export const HOST_VERSION = '1.0.0'
|
|
29
|
+
|
|
30
|
+
export function createDshHost(ctx) {
|
|
31
|
+
return {
|
|
32
|
+
kind: 'dsh',
|
|
33
|
+
version: HOST_VERSION,
|
|
34
|
+
capabilities: () => detectDshCapabilities(ctx),
|
|
35
|
+
log: () => getService(ctx, 'logger'),
|
|
36
|
+
skills: () => getService(ctx, 'skills'),
|
|
37
|
+
tools: () => {
|
|
38
|
+
const toolHost = getService(ctx, 'toolHost')
|
|
39
|
+
if (!toolHost?.registerWithDsh) return null
|
|
40
|
+
return {
|
|
41
|
+
// tool-host reads ctx.tools directly. Pass only the resolved native tool
|
|
42
|
+
// service, not the foundation fiber's non-injected Cordis proxy.
|
|
43
|
+
register: (registry, options) => toolHost.registerWithDsh({ tools: getService(ctx, 'tools') }, registry, options),
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
http: () => getService(ctx, 'serviceHost'),
|
|
47
|
+
web: () => getService(ctx, 'webServer'),
|
|
48
|
+
proc: () => getService(ctx, 'serviceBroker'),
|
|
49
|
+
ui: () => getService(ctx, 'editorHost'),
|
|
50
|
+
plugins: () => getService(ctx, 'pluginManager'),
|
|
51
|
+
assets: () => getService(ctx, 'assets'),
|
|
52
|
+
works: () => getService(ctx, 'workStore'),
|
|
53
|
+
}
|
|
54
|
+
}
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 内存宿主:Host 接口的参考实现,不依赖任何 agent 运行时。
|
|
3
|
+
*
|
|
4
|
+
* 用途有两个:
|
|
5
|
+
* 1. 一致性测试的基线——应用插件在这里跑通,说明它没有偷偷依赖 dsh;
|
|
6
|
+
* 2. 新宿主(别的 agent 系统)的模板——照这个接口实现即可,应用插件零改动。
|
|
7
|
+
*
|
|
8
|
+
* 它不承担真实网络/进程语义,只保证 Host 契约的形状与生命周期。
|
|
9
|
+
*/
|
|
10
|
+
import { BASE_CAPABILITIES } from './caps.mjs'
|
|
11
|
+
|
|
12
|
+
const NOOP_LOG = { debug() {}, info() {}, warn() {}, error() {} }
|
|
13
|
+
|
|
14
|
+
/* 内存宿主也要有界:app 不 dispose、asset 只 put 不 delete 的用法会让数组/Map
|
|
15
|
+
* 无限增长。app 提供 dispose() 摘除自己;assets 提供 delete() 与上限 LRU 淘汰。 */
|
|
16
|
+
const DEFAULT_MAX_ASSETS = 1024
|
|
17
|
+
|
|
18
|
+
export function createMemoryHost(options = {}) {
|
|
19
|
+
const state = {
|
|
20
|
+
providers: [],
|
|
21
|
+
tools: [],
|
|
22
|
+
apps: [],
|
|
23
|
+
mounts: new Map(),
|
|
24
|
+
web: [],
|
|
25
|
+
services: new Map(),
|
|
26
|
+
assets: new Map(),
|
|
27
|
+
bundles: [],
|
|
28
|
+
plugins: [],
|
|
29
|
+
}
|
|
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
|
|
34
|
+
|
|
35
|
+
function http() {
|
|
36
|
+
return {
|
|
37
|
+
createApp({ id, title } = {}) {
|
|
38
|
+
const routes = []
|
|
39
|
+
const app = {
|
|
40
|
+
id,
|
|
41
|
+
title: title || id,
|
|
42
|
+
routes,
|
|
43
|
+
use(handler) {
|
|
44
|
+
routes.push(handler)
|
|
45
|
+
return () => {
|
|
46
|
+
const index = routes.indexOf(handler)
|
|
47
|
+
if (index >= 0) routes.splice(index, 1)
|
|
48
|
+
}
|
|
49
|
+
},
|
|
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
|
+
},
|
|
56
|
+
}
|
|
57
|
+
state.apps.push(app)
|
|
58
|
+
return app
|
|
59
|
+
},
|
|
60
|
+
createSharedHost({ id } = {}) {
|
|
61
|
+
return {
|
|
62
|
+
id,
|
|
63
|
+
mount(prefix, child) {
|
|
64
|
+
if (state.mounts.has(prefix)) throw new Error(`前缀已挂载: ${prefix}`)
|
|
65
|
+
state.mounts.set(prefix, child.id)
|
|
66
|
+
return () => state.mounts.delete(prefix)
|
|
67
|
+
},
|
|
68
|
+
listen: () => Promise.resolve({ port: 0, url: `memory://${id || 'shared'}` }),
|
|
69
|
+
close: async () => {},
|
|
70
|
+
children: () => [...state.mounts.keys()],
|
|
71
|
+
}
|
|
72
|
+
},
|
|
73
|
+
}
|
|
74
|
+
}
|
|
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
|
+
|
|
91
|
+
return {
|
|
92
|
+
kind: 'memory',
|
|
93
|
+
version: '1.0.0',
|
|
94
|
+
state,
|
|
95
|
+
capabilities: () => ({
|
|
96
|
+
...BASE_CAPABILITIES,
|
|
97
|
+
'os.skills': '1.0.0',
|
|
98
|
+
'os.net': '1.0.0',
|
|
99
|
+
'os.web': '1.0.0',
|
|
100
|
+
'os.tools': '1.0.0',
|
|
101
|
+
'os.proc': '1.0.0',
|
|
102
|
+
'os.ui': '1.0.0',
|
|
103
|
+
'os.plugins': '1.0.0',
|
|
104
|
+
'os.assets': '1.0.0',
|
|
105
|
+
}),
|
|
106
|
+
log: () => log,
|
|
107
|
+
skills: () => ({
|
|
108
|
+
registerProvider(factory) {
|
|
109
|
+
const provider = factory()
|
|
110
|
+
state.providers.push(provider)
|
|
111
|
+
return () => {
|
|
112
|
+
const index = state.providers.indexOf(provider)
|
|
113
|
+
if (index >= 0) state.providers.splice(index, 1)
|
|
114
|
+
}
|
|
115
|
+
},
|
|
116
|
+
}),
|
|
117
|
+
tools: () => ({
|
|
118
|
+
register(registry, options2 = {}) {
|
|
119
|
+
const entry = { registry, namespace: options2.namespace || '' }
|
|
120
|
+
state.tools.push(entry)
|
|
121
|
+
return () => {
|
|
122
|
+
const index = state.tools.indexOf(entry)
|
|
123
|
+
if (index >= 0) state.tools.splice(index, 1)
|
|
124
|
+
}
|
|
125
|
+
},
|
|
126
|
+
}),
|
|
127
|
+
http,
|
|
128
|
+
web,
|
|
129
|
+
proc: () => ({
|
|
130
|
+
async start(serviceSpec) {
|
|
131
|
+
state.services.set(serviceSpec.id, serviceSpec)
|
|
132
|
+
return { services: [{ id: serviceSpec.id }], errors: [] }
|
|
133
|
+
},
|
|
134
|
+
stop(id) {
|
|
135
|
+
state.services.delete(id)
|
|
136
|
+
},
|
|
137
|
+
}),
|
|
138
|
+
ui: () => ({
|
|
139
|
+
createEditorHost({ id, dir, basePath, index }) {
|
|
140
|
+
return {
|
|
141
|
+
id,
|
|
142
|
+
dir,
|
|
143
|
+
basePath,
|
|
144
|
+
index,
|
|
145
|
+
routes: () => [async () => false],
|
|
146
|
+
status: () => ({ connected: false, lease: null }),
|
|
147
|
+
}
|
|
148
|
+
},
|
|
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
|
+
}),
|
|
186
|
+
}
|
|
187
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @capyclaw/foundation 的 Cordis 入口:向同进程插件提供 ctx.os。
|
|
3
|
+
*
|
|
4
|
+
* 这是整个工程里唯一一处「把内核交给 dsh」的接缝。应用插件不 import 这个文件,
|
|
5
|
+
* 它们只在自己的 apply 里拿到 ctx.os。
|
|
6
|
+
*/
|
|
7
|
+
import { createKernel } from './kernel.mjs'
|
|
8
|
+
|
|
9
|
+
export const name = 'capyclaw-foundation'
|
|
10
|
+
|
|
11
|
+
export function apply(ctx, config = {}) {
|
|
12
|
+
const kernel = createKernel(ctx, config)
|
|
13
|
+
const dispose = ctx.provide('os', kernel.os)
|
|
14
|
+
console.log('[foundation] ready(abi v' + kernel.os.abi + ')')
|
|
15
|
+
let cleanup
|
|
16
|
+
return () => {
|
|
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
|
|
22
|
+
}
|
|
23
|
+
}
|