@capyclaw/skills-service 0.1.4 → 0.2.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 +3 -3
- package/node_modules/@capyclaw/foundation/README.md +51 -0
- package/node_modules/@capyclaw/foundation/abi/bridge.mjs +86 -0
- package/node_modules/@capyclaw/foundation/abi/errors.mjs +63 -0
- package/node_modules/@capyclaw/foundation/abi/index.mjs +35 -0
- package/node_modules/@capyclaw/foundation/abi/kinds.mjs +271 -0
- package/node_modules/@capyclaw/foundation/abi/loader.mjs +345 -0
- package/node_modules/@capyclaw/foundation/abi/schema.mjs +98 -0
- package/node_modules/@capyclaw/foundation/abi/spec.mjs +255 -0
- package/node_modules/@capyclaw/foundation/abi/validate.mjs +24 -0
- package/node_modules/@capyclaw/foundation/abi/version.mjs +82 -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 +55 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/own-server.mjs +113 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/raw.mjs +68 -0
- package/node_modules/@capyclaw/foundation/lib/adapters/runtime-driver.mjs +113 -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 +34 -0
- package/node_modules/@capyclaw/foundation/lib/host/contract.mjs +85 -0
- package/node_modules/@capyclaw/foundation/lib/host/dsh.mjs +50 -0
- package/node_modules/@capyclaw/foundation/lib/host/memory.mjs +160 -0
- package/node_modules/@capyclaw/foundation/lib/host.mjs +23 -0
- package/node_modules/@capyclaw/foundation/lib/kernel.mjs +185 -0
- package/node_modules/@capyclaw/foundation/lib/skill/scan.mjs +87 -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 +188 -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 +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 +143 -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 +41 -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 +10 -3
- package/server.mjs +49 -13
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* own-server@1 适配器:托管一个 HTTP 面。
|
|
3
|
+
*
|
|
4
|
+
* 宿主提供 `web()`(dsh 即 `ctx.webServer`)时,这个面挂到宿主网页端口的路径前缀
|
|
5
|
+
* (`impl.mount`,默认 `/plugins/<id>`)下,**不再新起端口**;只有拿不到 web() 的
|
|
6
|
+
* 宿主才退回 service-host 的独立 loopback origin(凭据中转等必须独立 origin 的场景)。
|
|
7
|
+
*
|
|
8
|
+
* 应用只提供 handler(req,res,api) 与可选 start/stop;监听、端口、锁、健康检查、
|
|
9
|
+
* 关闭全部由宿主 service-host 负责。
|
|
10
|
+
*/
|
|
11
|
+
import { AbiError, ERROR_CODES } from '../../abi/errors.mjs'
|
|
12
|
+
|
|
13
|
+
export const kind = 'own-server@1'
|
|
14
|
+
|
|
15
|
+
function resolvePort(impl, spec, config, fallback) {
|
|
16
|
+
const value = typeof impl.port === 'function' ? impl.port({ spec, config }) : impl.port
|
|
17
|
+
if (value === 'auto') return 0
|
|
18
|
+
if (Number.isInteger(value) && value >= 0 && value <= 65535) return value
|
|
19
|
+
throw new AbiError(ERROR_CODES.E_PLUGIN_SHAPE,
|
|
20
|
+
`own-server port 必须是 0-65535 整数或 "auto",收到 ${String(value)}`,
|
|
21
|
+
{ plugin: spec.name, id: impl.id, port: value, fallback })
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function createOwnServerAdapter({ host, log }) {
|
|
25
|
+
return {
|
|
26
|
+
kind,
|
|
27
|
+
compile(impl, { spec, config, context }) {
|
|
28
|
+
const serviceHost = host.http()
|
|
29
|
+
if (!serviceHost?.createApp) {
|
|
30
|
+
throw new AbiError(ERROR_CODES.E_CAP_MISSING,
|
|
31
|
+
'ctx.serviceHost 不可用:无法托管自有 origin',
|
|
32
|
+
{ plugin: spec.name, capability: 'os.net' })
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const app = serviceHost.createApp({ id: impl.id, title: spec.name })
|
|
36
|
+
const web = typeof host.web === 'function' ? host.web() : null
|
|
37
|
+
const canEmbed = !!(web && typeof web.register === 'function')
|
|
38
|
+
const mount = typeof impl.mount === 'string' && impl.mount
|
|
39
|
+
? impl.mount
|
|
40
|
+
: `/plugins/${impl.id}`
|
|
41
|
+
/* 错误响应必须防 write-after-end:handler 已 res.end() 后再失败(流式响应后
|
|
42
|
+
* 异步出错是常见形态)时,再写会抛 ERR_STREAM_WRITE_AFTER_END——在 .catch 回调
|
|
43
|
+
* 里就是未捕获 rejection,可拖垮整个宿主。已结束只记日志;写本身也包 try/catch。 */
|
|
44
|
+
const failResponse = (res, error) => {
|
|
45
|
+
log?.error?.(`[foundation] ${spec.name} own-server handler 失败:${error?.message || error}`)
|
|
46
|
+
try {
|
|
47
|
+
if (res.writableEnded || res.destroyed) return
|
|
48
|
+
if (!res.headersSent) res.writeHead(500, { 'content-type': 'application/json' })
|
|
49
|
+
res.end(JSON.stringify({ error: 'handler failed' }))
|
|
50
|
+
} catch (writeError) {
|
|
51
|
+
log?.error?.(`[foundation] ${spec.name} own-server 错误响应写入失败:${writeError?.message || writeError}`)
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
app.use((req, res, url) => {
|
|
55
|
+
try {
|
|
56
|
+
const result = impl.handler(req, res, {
|
|
57
|
+
spec, config, log, os: context?.os, url, mount, embedded: canEmbed,
|
|
58
|
+
})
|
|
59
|
+
if (result && typeof result.then === 'function') {
|
|
60
|
+
result.catch((error) => failResponse(res, error))
|
|
61
|
+
}
|
|
62
|
+
} catch (error) {
|
|
63
|
+
failResponse(res, error)
|
|
64
|
+
}
|
|
65
|
+
return true
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
let info
|
|
69
|
+
let started = false
|
|
70
|
+
let stopped = false
|
|
71
|
+
return {
|
|
72
|
+
app,
|
|
73
|
+
/** true = 挂在宿主网页端口的前缀下,没有新起端口。 */
|
|
74
|
+
embedded: canEmbed,
|
|
75
|
+
mount,
|
|
76
|
+
async start() {
|
|
77
|
+
started = true
|
|
78
|
+
info = await app.listen(canEmbed
|
|
79
|
+
? { webServer: web, basePath: mount }
|
|
80
|
+
: { port: resolvePort(impl, spec, config), host: impl.host })
|
|
81
|
+
if (typeof impl.start === 'function') {
|
|
82
|
+
await impl.start({ app, info, spec, config, log, os: context?.os, embedded: canEmbed, mount })
|
|
83
|
+
}
|
|
84
|
+
log?.info?.(`[foundation] ${spec.name} own-server ${impl.id} ${canEmbed ? '挂载' : '监听'} ${info.url}`)
|
|
85
|
+
},
|
|
86
|
+
async dispose() {
|
|
87
|
+
if (stopped) return
|
|
88
|
+
stopped = true
|
|
89
|
+
/* 资源闭合必须在 finally:impl.stop 抛错不能跳过 app.close,否则端口/
|
|
90
|
+
* 监听器永久泄漏且 stopped=true 使重试 no-op(HMR 重挂 EADDRINUSE)。
|
|
91
|
+
* 与 service-provider 的 try{stop}finally{withdraw} 同构。错误仍上抛,
|
|
92
|
+
* 让 loader 记 cleanupErrors(保留所有权语义),但资源已经释放。 */
|
|
93
|
+
const errors = []
|
|
94
|
+
try {
|
|
95
|
+
if (typeof impl.stop === 'function') {
|
|
96
|
+
await impl.stop({ app, info, spec, config, log, os: context?.os, embedded: canEmbed, mount })
|
|
97
|
+
}
|
|
98
|
+
} catch (error) {
|
|
99
|
+
errors.push(error)
|
|
100
|
+
} finally {
|
|
101
|
+
if (started) {
|
|
102
|
+
try { await app.close() } catch (error) { errors.push(error) }
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
if (errors.length > 1) {
|
|
106
|
+
log?.error?.(`[foundation] ${spec.name} own-server dispose 多个错误:${errors.map((e) => e?.message || e).join(';')}`)
|
|
107
|
+
}
|
|
108
|
+
if (errors.length) throw errors[0]
|
|
109
|
+
},
|
|
110
|
+
}
|
|
111
|
+
},
|
|
112
|
+
}
|
|
113
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* raw@1 适配器:逃生舱。
|
|
3
|
+
*
|
|
4
|
+
* 描述符只提供 apply;适配器把插件自己的 Cordis ctx 交给它,并统一处理
|
|
5
|
+
* 同步/异步返回值与 dispose。raw 插件必须登记在 meta/foundation.allowlist.json
|
|
6
|
+
* 的 raw 段,准入闸会把它与普通应用插件分开统计。
|
|
7
|
+
*/
|
|
8
|
+
import { AbiError, ERROR_CODES } from '../../abi/errors.mjs'
|
|
9
|
+
|
|
10
|
+
export const kind = 'raw@1'
|
|
11
|
+
|
|
12
|
+
const thenable = (value) => value && typeof value.then === 'function'
|
|
13
|
+
|
|
14
|
+
function normalize(value) {
|
|
15
|
+
if (typeof value === 'function') return { dispose: value }
|
|
16
|
+
if (value && typeof value === 'object') {
|
|
17
|
+
const instance = {}
|
|
18
|
+
if (typeof value.start === 'function') instance.start = value.start.bind(value)
|
|
19
|
+
if (typeof value.dispose === 'function') instance.dispose = value.dispose.bind(value)
|
|
20
|
+
return instance
|
|
21
|
+
}
|
|
22
|
+
return {}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function createRawAdapter({ log }) {
|
|
26
|
+
return {
|
|
27
|
+
kind,
|
|
28
|
+
compile(impl, { spec, config, context }) {
|
|
29
|
+
const ctx = context?.ctx
|
|
30
|
+
if (!ctx) {
|
|
31
|
+
throw new AbiError(ERROR_CODES.E_ADAPTER_THREW,
|
|
32
|
+
'raw@1 需要 Cordis ctx:请通过 createPlugin 挂载', { plugin: spec.name })
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
let result
|
|
36
|
+
try {
|
|
37
|
+
result = impl.apply({
|
|
38
|
+
ctx, spec, config, log, baseUrl: context.baseUrl, os: context.os,
|
|
39
|
+
})
|
|
40
|
+
} catch (error) {
|
|
41
|
+
throw new AbiError(ERROR_CODES.E_ADAPTER_THREW,
|
|
42
|
+
`raw apply 失败:${error.message}`, { plugin: spec.name, cause: error })
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (thenable(result)) {
|
|
46
|
+
let instance = {}
|
|
47
|
+
let settled = false
|
|
48
|
+
const started = Promise.resolve(result).then((value) => {
|
|
49
|
+
instance = normalize(value)
|
|
50
|
+
settled = true
|
|
51
|
+
return instance.start ? instance.start() : undefined
|
|
52
|
+
})
|
|
53
|
+
return {
|
|
54
|
+
start: () => started,
|
|
55
|
+
dispose: async () => {
|
|
56
|
+
if (!settled) {
|
|
57
|
+
try { await started } catch { /* start 失败由 loader 记录 */ }
|
|
58
|
+
}
|
|
59
|
+
if (typeof instance.dispose === 'function') return instance.dispose()
|
|
60
|
+
},
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
log?.debug?.(`[foundation] ${spec.name} 使用 raw@1 逃生舱`)
|
|
65
|
+
return normalize(result)
|
|
66
|
+
},
|
|
67
|
+
}
|
|
68
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* runtime-driver@1 适配器:把描述符翻译成 broker 的服务规格。
|
|
3
|
+
*
|
|
4
|
+
* 应用侧不 spawn、不管重试、不做健康检查;这些由 service-broker 的 engine 负责。
|
|
5
|
+
* 端口由描述符或内核配置决定,应用不自己选端口。
|
|
6
|
+
*/
|
|
7
|
+
import { fileURLToPath } from 'node:url'
|
|
8
|
+
|
|
9
|
+
import { AbiError, ERROR_CODES } from '../../abi/errors.mjs'
|
|
10
|
+
|
|
11
|
+
export const kind = 'runtime-driver@1'
|
|
12
|
+
|
|
13
|
+
export function createRuntimeDriverAdapter({ host, config, log }) {
|
|
14
|
+
const defaultPort = config.driverPort ?? 'auto'
|
|
15
|
+
|
|
16
|
+
return {
|
|
17
|
+
kind,
|
|
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
|
+
|
|
33
|
+
const broker = host.proc()
|
|
34
|
+
if (!broker?.start || !broker?.stop) {
|
|
35
|
+
throw new AbiError(ERROR_CODES.E_CAP_MISSING,
|
|
36
|
+
'ctx.serviceBroker 不可用:无法拉起长驻进程',
|
|
37
|
+
{ plugin: mountContext.spec.name, capability: 'os.proc' })
|
|
38
|
+
}
|
|
39
|
+
if (!mountContext?.context?.baseUrl) {
|
|
40
|
+
throw new AbiError(ERROR_CODES.E_BASE_URL_MISSING,
|
|
41
|
+
`插件 ${mountContext.spec.name} 缺少 baseUrl,无法解析 ${impl.entry}`,
|
|
42
|
+
{ plugin: mountContext.spec.name })
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const entryPath = fileURLToPath(new URL(impl.entry, mountContext.context.baseUrl))
|
|
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
|
|
56
|
+
const portArg = impl.portArg || '--port'
|
|
57
|
+
const cmd = [
|
|
58
|
+
process.execPath,
|
|
59
|
+
entryPath,
|
|
60
|
+
portArg,
|
|
61
|
+
port === 'auto' ? '${PORT}' : String(port),
|
|
62
|
+
...(impl.args || []),
|
|
63
|
+
]
|
|
64
|
+
|
|
65
|
+
const serviceSpec = {
|
|
66
|
+
id: impl.id,
|
|
67
|
+
cmd,
|
|
68
|
+
ports: { main: port },
|
|
69
|
+
url: 'http://127.0.0.1:${PORT}/',
|
|
70
|
+
keepAlive: true,
|
|
71
|
+
}
|
|
72
|
+
if (impl.health?.path) serviceSpec.healthPath = impl.health.path
|
|
73
|
+
if (impl.dir) {
|
|
74
|
+
serviceSpec.cwd = fileURLToPath(new URL(impl.dir, mountContext.context.baseUrl))
|
|
75
|
+
}
|
|
76
|
+
const env = impl.env?.({
|
|
77
|
+
spec: mountContext.spec,
|
|
78
|
+
config: mountContext.config,
|
|
79
|
+
os: mountContext.context?.os,
|
|
80
|
+
})
|
|
81
|
+
if (env && typeof env === 'object') serviceSpec.env = env
|
|
82
|
+
|
|
83
|
+
let entered = false
|
|
84
|
+
let stopPromise
|
|
85
|
+
return {
|
|
86
|
+
serviceSpec,
|
|
87
|
+
async start({ signal } = {}) {
|
|
88
|
+
entered = true
|
|
89
|
+
const result = await broker.start(serviceSpec, { signal })
|
|
90
|
+
const failure = (result?.errors || []).find((item) => item.id === impl.id)
|
|
91
|
+
if (failure) {
|
|
92
|
+
throw new AbiError(ERROR_CODES.E_ADAPTER_THREW,
|
|
93
|
+
`长驻进程 ${impl.id} 启动失败:${failure.error}`, { plugin: mountContext.spec.name, id: impl.id })
|
|
94
|
+
}
|
|
95
|
+
log?.info?.(`[foundation] ${impl.id} 已由 broker 拉起(port ${port})`)
|
|
96
|
+
},
|
|
97
|
+
dispose() {
|
|
98
|
+
if (!entered) return
|
|
99
|
+
if (!stopPromise) stopPromise = Promise.resolve().then(async () => {
|
|
100
|
+
const result = await broker.stop(impl.id)
|
|
101
|
+
if (result?.stopped === false) {
|
|
102
|
+
throw new AbiError(ERROR_CODES.E_ADAPTER_THREW,
|
|
103
|
+
`长驻进程 ${impl.id} 未停止:${result.reason || 'broker rejected stop'}`,
|
|
104
|
+
{ plugin: mountContext.spec.name, id: impl.id, result })
|
|
105
|
+
}
|
|
106
|
+
return result
|
|
107
|
+
})
|
|
108
|
+
return stopPromise
|
|
109
|
+
},
|
|
110
|
+
}
|
|
111
|
+
},
|
|
112
|
+
}
|
|
113
|
+
}
|
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* skill-provider@1 的适配器:目录 → provider → ctx.skills。
|
|
3
|
+
*
|
|
4
|
+
* 应用包只写 `skillProvider({ skill: './skills/canvas' })`,
|
|
5
|
+
* frontmatter 解析、契约注记、注册与释放都在这里完成。
|
|
6
|
+
*/
|
|
7
|
+
import { readFileSync } from 'node:fs'
|
|
8
|
+
import { basename, dirname } from 'node:path'
|
|
9
|
+
import { fileURLToPath } from 'node:url'
|
|
10
|
+
|
|
11
|
+
import { AbiError, ERROR_CODES } from '../../abi/errors.mjs'
|
|
12
|
+
import { createSkillProvider } from '../skill/scan.mjs'
|
|
13
|
+
|
|
14
|
+
export const kind = 'skill-provider@1'
|
|
15
|
+
|
|
16
|
+
export function createSkillAdapter({ host, log }) {
|
|
17
|
+
return {
|
|
18
|
+
kind,
|
|
19
|
+
compile(impl, { spec, context }) {
|
|
20
|
+
const skills = host.skills()
|
|
21
|
+
if (!skills || typeof skills.registerProvider !== 'function') {
|
|
22
|
+
throw new AbiError(ERROR_CODES.E_CAP_MISSING,
|
|
23
|
+
'ctx.skills.registerProvider 不可用:skills 服务未加载', { plugin: spec.name })
|
|
24
|
+
}
|
|
25
|
+
if (!context?.baseUrl) {
|
|
26
|
+
throw new AbiError(ERROR_CODES.E_BASE_URL_MISSING,
|
|
27
|
+
`插件 ${spec.name} 缺少 baseUrl,无法解析 ${impl.skill}`, { plugin: spec.name })
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const skillPath = fileURLToPath(new URL(impl.skill, context.baseUrl))
|
|
31
|
+
let contract = null
|
|
32
|
+
if (impl.contract) {
|
|
33
|
+
const contractPath = fileURLToPath(new URL(impl.contract, context.baseUrl))
|
|
34
|
+
try {
|
|
35
|
+
contract = JSON.parse(readFileSync(contractPath, 'utf8'))
|
|
36
|
+
} catch (error) {
|
|
37
|
+
throw new AbiError(ERROR_CODES.E_ADAPTER_THREW,
|
|
38
|
+
`契约快照不可读:${contractPath}`, { plugin: spec.name, cause: error })
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const provider = createSkillProvider({
|
|
43
|
+
assetRoot: dirname(skillPath),
|
|
44
|
+
skillDirs: [basename(skillPath)],
|
|
45
|
+
providerName: impl.name || `dsh-${spec.name}`,
|
|
46
|
+
rank: impl.rank,
|
|
47
|
+
contract,
|
|
48
|
+
contractNote: impl.note,
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
const dispose = skills.registerProvider(() => provider)
|
|
52
|
+
log?.debug?.(`[foundation] ${spec.name} 注册 skill ${impl.skill}`)
|
|
53
|
+
return { provider, dispose: typeof dispose === 'function' ? dispose : () => {} }
|
|
54
|
+
},
|
|
55
|
+
}
|
|
56
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* tool-provider@1 适配器:描述符里的 provide() 产出注册表,内核负责注册与反注册。
|
|
3
|
+
*
|
|
4
|
+
* 应用侧写法:
|
|
5
|
+
* toolProvider({ namespace: 'browser', provide: () => registry })
|
|
6
|
+
*
|
|
7
|
+
* 工具实现不经过 foundation,foundation 只决定「什么时候挂上去、挂到哪个命名空间」。
|
|
8
|
+
*/
|
|
9
|
+
import { AbiError, ERROR_CODES } from '../../abi/errors.mjs'
|
|
10
|
+
|
|
11
|
+
export const kind = 'tool-provider@1'
|
|
12
|
+
|
|
13
|
+
export function createToolProviderAdapter({ host, log }) {
|
|
14
|
+
return {
|
|
15
|
+
kind,
|
|
16
|
+
compile(impl, { spec, config, context }) {
|
|
17
|
+
const tools = host.tools()
|
|
18
|
+
if (!tools?.register) {
|
|
19
|
+
throw new AbiError(ERROR_CODES.E_CAP_MISSING,
|
|
20
|
+
'ctx.toolHost 不可用:无法注册工具面', { plugin: spec.name, capability: 'os.tools' })
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
let registry
|
|
24
|
+
try {
|
|
25
|
+
registry = impl.provide({ spec, config, log, os: context?.os })
|
|
26
|
+
} catch (error) {
|
|
27
|
+
throw new AbiError(ERROR_CODES.E_ADAPTER_THREW,
|
|
28
|
+
`工具注册表构建失败:${error.message}`, { plugin: spec.name, cause: error })
|
|
29
|
+
}
|
|
30
|
+
if (!registry || !Array.isArray(registry.TOOLS)) {
|
|
31
|
+
throw new AbiError(ERROR_CODES.E_ADAPTER_THREW,
|
|
32
|
+
'provide() 必须返回 { TOOLS, TOOL_IMPL } 形状的注册表', { plugin: spec.name })
|
|
33
|
+
}
|
|
34
|
+
|
|
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 || '无前缀'})`)
|
|
44
|
+
return {
|
|
45
|
+
registry,
|
|
46
|
+
dispose: typeof dispose === 'function' ? dispose : () => {},
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
}
|
|
50
|
+
}
|
|
@@ -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,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 宿主能力表:内核承诺给应用插件的 os.* 能力。
|
|
3
|
+
*
|
|
4
|
+
* BASE 是内核自身就能提供的;其余取决于宿主实际接入了哪些服务。
|
|
5
|
+
* 这里是唯一允许出现宿主服务名(skills / serviceHost / toolHost / serviceBroker /
|
|
6
|
+
* editorHost)的地方——它们属于 dsh 适配层,不属于 ABI。
|
|
7
|
+
*/
|
|
8
|
+
import { getService } from '../ctx-service.mjs'
|
|
9
|
+
|
|
10
|
+
export const BASE_CAPABILITIES = Object.freeze({
|
|
11
|
+
'os.events': '1.0.0',
|
|
12
|
+
'os.log': '1.0.0',
|
|
13
|
+
'os.caps': '1.0.0',
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
/** dsh 宿主:按 ctx 里实际存在的服务生成能力表。 */
|
|
17
|
+
export function detectDshCapabilities(ctx) {
|
|
18
|
+
const capabilities = { ...BASE_CAPABILITIES }
|
|
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'
|
|
21
|
+
const serviceHost = getService(ctx, 'serviceHost')
|
|
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'
|
|
33
|
+
return capabilities
|
|
34
|
+
}
|