@capyclaw/offline-ddr0 0.0.0-stage → 1.9.0
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/LICENSE +21 -0
- package/README.md +441 -2
- package/client/client.js +603 -0
- package/cordis.patch.yml +13 -0
- package/docs/STORAGE-MIGRATION.md +136 -0
- package/lib/atomic.mjs +40 -0
- package/lib/http-cache.mjs +42 -0
- package/lib/inbox.mjs +64 -0
- package/lib/media.mjs +242 -0
- package/lib/migrate-json.mjs +101 -0
- package/lib/mirror-index.mjs +105 -0
- package/lib/mirror.mjs +123 -0
- package/lib/proxy.mjs +188 -0
- package/lib/remote.mjs +158 -0
- package/lib/render.mjs +93 -0
- package/lib/repository.mjs +71 -0
- package/lib/store.mjs +610 -0
- package/lib/surface.mjs +74 -0
- package/lib/sync-ui.mjs +108 -0
- package/lib/sync.mjs +433 -0
- package/lib/wire.mjs +211 -0
- package/lib/workstore-repository.mjs +316 -0
- package/lib/workstore.mjs +338 -0
- package/mirror/files/00001.html +2045 -0
- package/mirror/files/00002.json +1 -0
- package/mirror/files/00003.json +1 -0
- package/mirror/files/00004.json +1 -0
- package/mirror/files/00005.json +1 -0
- package/mirror/files/00006.json +1 -0
- package/mirror/files/00007.json +1 -0
- package/mirror/files/00008.json +1 -0
- package/mirror/files/00009.json +1 -0
- package/mirror/files/00010.json +1 -0
- package/mirror/files/00011.json +1 -0
- package/mirror/files/00012.json +1 -0
- package/mirror/files/00013.json +1 -0
- package/mirror/files/00014.json +1 -0
- package/mirror/files/00015.json +1 -0
- package/mirror/files/00016.json +1 -0
- package/mirror/files/00017.json +1 -0
- package/mirror/files/00018.json +1 -0
- package/mirror/files/00019.json +1 -0
- package/mirror/files/00020.json +1 -0
- package/mirror/files/00021.json +1 -0
- package/mirror/files/00022.json +1 -0
- package/mirror/files/00023.js +205 -0
- package/mirror/files/00024.json +1 -0
- package/mirror/files/00025.json +1 -0
- package/mirror/files/00026.json +1 -0
- package/mirror/files/00027.js +51 -0
- package/mirror/files/00028.json +1 -0
- package/mirror/files/00029.json +1 -0
- package/mirror/files/00030.json +1 -0
- package/mirror/files/00031.json +1 -0
- package/mirror/files/00032.json +1 -0
- package/mirror/files/00033.json +1 -0
- package/mirror/files/00034.json +1 -0
- package/mirror/files/00035.json +1 -0
- package/mirror/files/00036.json +1 -0
- package/mirror/files/00037.json +1 -0
- package/mirror/files/00038.json +1 -0
- package/mirror/files/00039.json +1 -0
- package/mirror/files/00040.js +2311 -0
- package/mirror/files/00041.json +1 -0
- package/mirror/files/00042.json +1 -0
- package/mirror/files/00043.json +1 -0
- package/mirror/files/00044.json +1 -0
- package/mirror/files/00045.js +575 -0
- package/mirror/files/00046.json +1 -0
- package/mirror/files/00047.js +575 -0
- package/mirror/files/00048.js +575 -0
- package/mirror/files/00049.json +1 -0
- package/mirror/files/00050.json +1 -0
- package/mirror/files/00051.json +1 -0
- package/mirror/files/00052.json +1 -0
- package/mirror/files/00053.js +575 -0
- package/mirror/files/00054.json +1 -0
- package/mirror/files/00055.json +1 -0
- package/mirror/files/00056.js +575 -0
- package/mirror/files/00057.json +1 -0
- package/mirror/files/00058.json +1 -0
- package/mirror/files/00059.js +360 -0
- package/mirror/files/00060.json +1 -0
- package/mirror/files/00061.json +1 -0
- package/mirror/files/00062.json +1 -0
- package/mirror/files/00063.json +1 -0
- package/mirror/files/00064.json +1 -0
- package/mirror/files/00065.js +144 -0
- package/mirror/files/00066.js +7 -0
- package/mirror/files/00067.json +1 -0
- package/mirror/files/00068.json +1 -0
- package/mirror/files/00069.json +1 -0
- package/mirror/files/00070.json +1 -0
- package/mirror/files/00071.json +1 -0
- package/mirror/files/00072.json +1 -0
- package/mirror/files/00073.js +1 -0
- package/mirror/files/00074.json +1 -0
- package/mirror/files/00075.json +1 -0
- package/mirror/files/00076.json +1 -0
- package/mirror/files/00077.js +45 -0
- package/mirror/files/00078.js +1348 -0
- package/mirror/files/00079.json +1 -0
- package/mirror/files/00080.json +1 -0
- package/mirror/files/00081.js +1 -0
- package/mirror/files/00082.json +1 -0
- package/mirror/files/00083.json +1 -0
- package/mirror/files/00084.js +7 -0
- package/mirror/files/00085.json +1 -0
- package/mirror/files/00086.json +1 -0
- package/mirror/files/00087.js +7 -0
- package/mirror/files/00088.json +1 -0
- package/mirror/files/00089.json +1 -0
- package/mirror/files/00090.js +7 -0
- package/mirror/files/00091.json +1 -0
- package/mirror/files/00092.json +1 -0
- package/mirror/files/00093.js +102 -0
- package/mirror/files/00094.json +1 -0
- package/mirror/files/00095.json +1 -0
- package/mirror/files/00096.json +1 -0
- package/mirror/files/00097.json +1 -0
- package/mirror/files/00098.json +1 -0
- package/mirror/files/00099.json +1 -0
- package/mirror/files/00100.json +1 -0
- package/mirror/files/00101.js +102 -0
- package/mirror/files/00102.json +1 -0
- package/mirror/files/00103.json +1 -0
- package/mirror/files/00104.json +1 -0
- package/mirror/files/00105.json +1 -0
- package/mirror/files/00106.json +1 -0
- package/mirror/files/00107.js +110 -0
- package/mirror/files/00108.json +1 -0
- package/mirror/files/00109.json +1 -0
- package/mirror/files/00110.json +1 -0
- package/mirror/files/00111.json +1 -0
- package/mirror/files/00112.json +1 -0
- package/mirror/files/00113.json +1 -0
- package/mirror/files/00114.json +1 -0
- package/mirror/files/00115.json +1 -0
- package/mirror/files/00116.json +1 -0
- package/mirror/files/00117.json +1 -0
- package/mirror/files/00118.json +1 -0
- package/mirror/files/00119.json +1 -0
- package/mirror/files/00120.json +1 -0
- package/mirror/files/00121.json +1 -0
- package/mirror/files/00122.json +1 -0
- package/mirror/files/00123.json +1 -0
- package/mirror/files/00124.json +1 -0
- package/mirror/files/00125.json +1 -0
- package/mirror/files/00126.json +1 -0
- package/mirror/files/00127.json +1 -0
- package/mirror/files/00128.json +1 -0
- package/mirror/files/00129.json +1 -0
- package/mirror/files/00130.json +1 -0
- package/mirror/files/00131.json +1 -0
- package/mirror/files/00132.js +110 -0
- package/mirror/files/00133.json +1 -0
- package/mirror/files/00134.json +1 -0
- package/mirror/files/00135.json +1 -0
- package/mirror/files/00136.json +1 -0
- package/mirror/files/00137.js +26 -0
- package/mirror/files/00138.json +1 -0
- package/mirror/files/00139.json +1 -0
- package/mirror/files/00140.json +1 -0
- package/mirror/files/00141.json +1 -0
- package/mirror/files/00142.json +1 -0
- package/mirror/files/00143.json +1 -0
- package/mirror/files/00144.js +26 -0
- package/mirror/files/00145.json +1 -0
- package/mirror/files/00146.json +1 -0
- package/mirror/files/00147.js +29 -0
- package/mirror/files/00148.json +1 -0
- package/mirror/files/00149.json +1 -0
- package/mirror/files/00150.js +23 -0
- package/mirror/files/00151.json +1 -0
- package/mirror/files/00152.json +1 -0
- package/mirror/files/00153.js +719 -0
- package/mirror/files/00154.js +9575 -0
- package/mirror/files/00155.js +108 -0
- package/mirror/files/00156.js +379 -0
- package/mirror/files/00157.js +48 -0
- package/mirror/files/00158.json +16 -0
- package/mirror/manifest.json +1582 -0
- package/node_modules/@capyclaw/asset-service/README.md +48 -0
- package/node_modules/@capyclaw/asset-service/cordis.patch.yml +4 -0
- package/node_modules/@capyclaw/asset-service/dsh-suite.json +29 -0
- package/node_modules/@capyclaw/asset-service/lib/host.mjs +14 -0
- package/node_modules/@capyclaw/asset-service/package.json +59 -0
- package/node_modules/@capyclaw/asset-service/src/index.mjs +223 -0
- 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 +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/node_modules/@capyclaw/service-host/README.md +86 -0
- package/node_modules/@capyclaw/service-host/cordis.patch.yml +4 -0
- package/node_modules/@capyclaw/service-host/dsh-suite.json +29 -0
- package/node_modules/@capyclaw/service-host/lib/host.mjs +22 -0
- package/node_modules/@capyclaw/service-host/package.json +60 -0
- package/node_modules/@capyclaw/service-host/src/index.mjs +571 -0
- package/node_modules/@capyclaw/service-host/src/tuning.mjs +63 -0
- package/node_modules/@capyclaw/suite-kit/README.md +238 -0
- package/node_modules/@capyclaw/suite-kit/bin/dsh-domain.mjs +98 -0
- package/node_modules/@capyclaw/suite-kit/bin/dsh-suite.mjs +174 -0
- package/node_modules/@capyclaw/suite-kit/client/embed.js +35 -0
- package/node_modules/@capyclaw/suite-kit/domain.schema.json +129 -0
- package/node_modules/@capyclaw/suite-kit/package.json +54 -0
- package/node_modules/@capyclaw/suite-kit/src/activation-registry.mjs +184 -0
- package/node_modules/@capyclaw/suite-kit/src/artifacts.mjs +131 -0
- package/node_modules/@capyclaw/suite-kit/src/capability.mjs +181 -0
- package/node_modules/@capyclaw/suite-kit/src/compose.mjs +201 -0
- package/node_modules/@capyclaw/suite-kit/src/domain.mjs +138 -0
- package/node_modules/@capyclaw/suite-kit/src/embed.mjs +81 -0
- package/node_modules/@capyclaw/suite-kit/src/index.mjs +10 -0
- package/node_modules/@capyclaw/suite-kit/src/manifest.mjs +263 -0
- package/node_modules/@capyclaw/suite-kit/src/orchestration.mjs +100 -0
- package/node_modules/@capyclaw/suite-kit/src/profile.mjs +584 -0
- package/node_modules/@capyclaw/suite-kit/src/surface.mjs +28 -0
- package/node_modules/@capyclaw/suite-kit/suite.schema.json +112 -0
- package/node_modules/@capyclaw/work-store/README.md +83 -0
- package/node_modules/@capyclaw/work-store/STANDARD.md +257 -0
- package/node_modules/@capyclaw/work-store/cordis.patch.yml +3 -0
- package/node_modules/@capyclaw/work-store/dsh-suite.json +26 -0
- package/node_modules/@capyclaw/work-store/icon.svg +7 -0
- package/node_modules/@capyclaw/work-store/lib/host.mjs +21 -0
- package/node_modules/@capyclaw/work-store/package.json +68 -0
- package/node_modules/@capyclaw/work-store/src/backend.mjs +89 -0
- package/node_modules/@capyclaw/work-store/src/db.mjs +199 -0
- package/node_modules/@capyclaw/work-store/src/envelope.mjs +132 -0
- package/node_modules/@capyclaw/work-store/src/index.mjs +570 -0
- package/node_modules/@capyclaw/work-store/src/interop.mjs +70 -0
- package/node_modules/@capyclaw/work-store/src/refs.mjs +99 -0
- package/node_modules/@capyclaw/work-store/src/routes.mjs +135 -0
- package/package.json +82 -4
- package/plugin.mjs +636 -0
- package/static/index.html +2068 -0
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# @capyclaw/asset-service
|
|
2
|
+
|
|
3
|
+
DSH 公共资产插件。功能插件通过 `ctx.assets` 获得统一的 sha256 对象仓库和 `/assets` 路由。
|
|
4
|
+
|
|
5
|
+
## 使用
|
|
6
|
+
|
|
7
|
+
```js
|
|
8
|
+
export const inject = ['assets']
|
|
9
|
+
|
|
10
|
+
export function apply(ctx) {
|
|
11
|
+
const app = ctx.serviceHost.createApp({ id: 'my-plugin' })
|
|
12
|
+
app.use(ctx.assets.routes())
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// 或在插件内部写入
|
|
16
|
+
const asset = await ctx.assets.put(buffer, 'image/png')
|
|
17
|
+
// { id, key, url: '/assets/<sha256>', size, contentType }
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
独立运行或测试:
|
|
21
|
+
|
|
22
|
+
```js
|
|
23
|
+
import { createFileAssetStore, createAssetRoutes } from '@capyclaw/asset-service/src'
|
|
24
|
+
const store = createFileAssetStore({ dir: './assets' })
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## 路由归属
|
|
28
|
+
|
|
29
|
+
多个插件共用一个 service-host app 时,用 `ctx.assets.mount(app)` 只挂一次 `/assets`:
|
|
30
|
+
|
|
31
|
+
```js
|
|
32
|
+
ctx.assets.mount(app, { store: assetStore })
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
重复 mount 会被忽略;不要再手写 `app.use(ctx.assets.routes())`。
|
|
36
|
+
|
|
37
|
+
## 提供什么
|
|
38
|
+
|
|
39
|
+
- `createWorkspaceAssetStore({ storeClient, workId })`:资产归当前 work
|
|
40
|
+
- `createFileAssetStore({ dir })`:本地目录后端
|
|
41
|
+
- `createAssetRoutes({ assetStore, basePath })`:GET/HEAD `/assets/<id>`、POST `<basePath>/assets`
|
|
42
|
+
- `ctx.assets.put/get`:默认 store 快捷方法
|
|
43
|
+
|
|
44
|
+
## 契约
|
|
45
|
+
|
|
46
|
+
- 资产按 sha256 去重,不可变
|
|
47
|
+
- url 固定为 `/assets/<id>`,可长期缓存
|
|
48
|
+
- 内容类型由上传方声明,读取时原样返回
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "../capyclaw-suite-kit/suite.schema.json",
|
|
3
|
+
"name": "dsh-asset-service-suite",
|
|
4
|
+
"version": "0.1.0",
|
|
5
|
+
"title": "Asset Service",
|
|
6
|
+
"description": "通用对象资产:sha256 仓库、/assets 路由、上传。",
|
|
7
|
+
"packages": {
|
|
8
|
+
"@capyclaw/asset-service": {
|
|
9
|
+
"role": "provider",
|
|
10
|
+
"version": "^0.1.4",
|
|
11
|
+
"path": ".",
|
|
12
|
+
"provides": {
|
|
13
|
+
"services": [
|
|
14
|
+
"assets"
|
|
15
|
+
],
|
|
16
|
+
"capabilities": {
|
|
17
|
+
"assets.store": "1.0.0"
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"rows": [
|
|
23
|
+
{
|
|
24
|
+
"id": "asset-service",
|
|
25
|
+
"name": "@capyclaw/asset-service"
|
|
26
|
+
}
|
|
27
|
+
],
|
|
28
|
+
"checks": []
|
|
29
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/** @capyclaw/asset-service 插件:向同进程插件提供 ctx.assets。 */
|
|
2
|
+
import { defineDriver } from '@capyclaw/foundation/abi'
|
|
3
|
+
import { createAssetHost } from '../src/index.mjs'
|
|
4
|
+
|
|
5
|
+
export const { name, inject, apply } = defineDriver({
|
|
6
|
+
name: 'asset-service',
|
|
7
|
+
service: 'assets',
|
|
8
|
+
apply(ctx, config = {}) {
|
|
9
|
+
const service = createAssetHost(config)
|
|
10
|
+
const dispose = ctx.provide('assets', service)
|
|
11
|
+
console.log('[asset-service] ready')
|
|
12
|
+
return () => { if (typeof dispose === 'function') dispose() }
|
|
13
|
+
},
|
|
14
|
+
})
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@capyclaw/asset-service",
|
|
3
|
+
"version": "0.1.4",
|
|
4
|
+
"description": "DSH 公共资产插件:sha256 对象仓库、/assets 路由、base64 上传与 content-type,向插件提供 ctx.assets。",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./lib/host.mjs",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./lib/host.mjs",
|
|
9
|
+
"./src": "./src/index.mjs",
|
|
10
|
+
"./package.json": "./package.json"
|
|
11
|
+
},
|
|
12
|
+
"engines": {
|
|
13
|
+
"node": ">=18",
|
|
14
|
+
"dsh": "^0.2.0-rc.2"
|
|
15
|
+
},
|
|
16
|
+
"publishConfig": {
|
|
17
|
+
"access": "public"
|
|
18
|
+
},
|
|
19
|
+
"dsh": {
|
|
20
|
+
"bundle": {
|
|
21
|
+
"patch": "./cordis.patch.yml"
|
|
22
|
+
},
|
|
23
|
+
"suite": "./dsh-suite.json",
|
|
24
|
+
"apiVersion": "1.0.0",
|
|
25
|
+
"category": "service",
|
|
26
|
+
"platform": "both",
|
|
27
|
+
"driver": {
|
|
28
|
+
"service": "assets"
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
"meta": {
|
|
32
|
+
"title": "公共资产服务",
|
|
33
|
+
"description": "sha256 对象仓库、/assets 路由、base64 上传与 content-type,向插件提供 ctx.assets"
|
|
34
|
+
},
|
|
35
|
+
"files": [
|
|
36
|
+
"lib/",
|
|
37
|
+
"src/",
|
|
38
|
+
"cordis.patch.yml",
|
|
39
|
+
"dsh-suite.json",
|
|
40
|
+
"README.md"
|
|
41
|
+
],
|
|
42
|
+
"license": "MIT",
|
|
43
|
+
"scripts": {
|
|
44
|
+
"test": "node --test test/*.test.mjs"
|
|
45
|
+
},
|
|
46
|
+
"dependencies": {
|
|
47
|
+
"@capyclaw/plugin-kit": "^0.2.0",
|
|
48
|
+
"@capyclaw/foundation": "^0.1.1"
|
|
49
|
+
},
|
|
50
|
+
"repository": {
|
|
51
|
+
"type": "git",
|
|
52
|
+
"url": "git+https://github.com/Maple496/capyclaw.git",
|
|
53
|
+
"directory": "C/packages/capyclaw-asset-service"
|
|
54
|
+
},
|
|
55
|
+
"bugs": {
|
|
56
|
+
"url": "https://github.com/Maple496/capyclaw/issues"
|
|
57
|
+
},
|
|
58
|
+
"homepage": "https://github.com/Maple496/capyclaw/tree/main/C/packages/capyclaw-asset-service#readme"
|
|
59
|
+
}
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @capyclaw/asset-service —— 通用对象资产仓库。
|
|
3
|
+
*
|
|
4
|
+
* 功能插件不再各自实现 `/assets/<sha256>`、base64 上传、content-type 与缓存头。
|
|
5
|
+
* 资产按 sha256 去重,url 为 `/assets/<id>`。
|
|
6
|
+
*
|
|
7
|
+
* 两种后端:
|
|
8
|
+
* - workspace:storeClient(ctx.workspace 的 scoped client),资产归当前 work;
|
|
9
|
+
* - file:本地目录,适合无 workspace 的独立运行/测试。
|
|
10
|
+
*/
|
|
11
|
+
import crypto from 'node:crypto'
|
|
12
|
+
import fs from 'node:fs'
|
|
13
|
+
import path from 'node:path'
|
|
14
|
+
import { send, readBody } from '@capyclaw/plugin-kit/service/http'
|
|
15
|
+
import { createServiceApi } from '@capyclaw/plugin-kit/service/api'
|
|
16
|
+
|
|
17
|
+
function sha256(buffer) {
|
|
18
|
+
return crypto.createHash('sha256').update(buffer).digest('hex')
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const ASSET_ID_RE = /^[a-f0-9]{64}$/
|
|
22
|
+
|
|
23
|
+
function validId(id) {
|
|
24
|
+
return ASSET_ID_RE.test(String(id || ''))
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function normalizeBuffer(value) {
|
|
28
|
+
if (Buffer.isBuffer(value)) return value
|
|
29
|
+
if (value instanceof Uint8Array) return Buffer.from(value)
|
|
30
|
+
throw new Error('asset: 数据必须是 Buffer/Uint8Array')
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** 基于 ctx.workspace 的 scoped WorkspaceClient。 */
|
|
34
|
+
export function createWorkspaceAssetStore({ storeClient, workId = 'main' } = {}) {
|
|
35
|
+
if (!storeClient || typeof storeClient.put !== 'function') throw new Error('createWorkspaceAssetStore 需要 storeClient')
|
|
36
|
+
return {
|
|
37
|
+
kind: 'workspace',
|
|
38
|
+
async put(value, contentType = 'application/octet-stream') {
|
|
39
|
+
const buffer = normalizeBuffer(value)
|
|
40
|
+
const id = sha256(buffer)
|
|
41
|
+
const key = `assets/${id}`
|
|
42
|
+
storeClient.put(workId, key, buffer, { contentType })
|
|
43
|
+
return { id, key, url: `/assets/${id}`, size: buffer.length, contentType }
|
|
44
|
+
},
|
|
45
|
+
get(id) {
|
|
46
|
+
if (!validId(id)) return null
|
|
47
|
+
const key = `assets/${id}`
|
|
48
|
+
try {
|
|
49
|
+
const data = storeClient.get(workId, key)
|
|
50
|
+
if (!data) return null
|
|
51
|
+
const meta = storeClient.list(workId).find((item) => item.key === key)
|
|
52
|
+
return { data, contentType: meta?.contentType || 'application/octet-stream' }
|
|
53
|
+
} catch {
|
|
54
|
+
/* 缺失/权限错误按 404 处理,不能把 500 泄漏给资产路由。 */
|
|
55
|
+
return null
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
has(id) {
|
|
59
|
+
if (!validId(id)) return false
|
|
60
|
+
try { return !!storeClient.get(workId, `assets/${id}`) } catch { return false }
|
|
61
|
+
},
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** 本地目录后端:<dir>/<sha256> + <dir>/<sha256>.meta.json。 */
|
|
66
|
+
export function createFileAssetStore({ dir } = {}) {
|
|
67
|
+
if (!dir) throw new Error('createFileAssetStore 需要 dir')
|
|
68
|
+
fs.mkdirSync(dir, { recursive: true })
|
|
69
|
+
return {
|
|
70
|
+
kind: 'file',
|
|
71
|
+
async put(value, contentType = 'application/octet-stream') {
|
|
72
|
+
const buffer = normalizeBuffer(value)
|
|
73
|
+
const id = sha256(buffer)
|
|
74
|
+
const file = path.join(dir, id)
|
|
75
|
+
const metaFile = file + '.meta.json'
|
|
76
|
+
const existingOk = (() => {
|
|
77
|
+
try { return fs.statSync(file).size === buffer.length } catch { return false }
|
|
78
|
+
})()
|
|
79
|
+
if (!existingOk) {
|
|
80
|
+
const tmp = `${file}.tmp-${process.pid}-${Date.now()}-${Math.random().toString(16).slice(2)}`
|
|
81
|
+
fs.writeFileSync(tmp, buffer)
|
|
82
|
+
fs.renameSync(tmp, file)
|
|
83
|
+
}
|
|
84
|
+
const metaTmp = `${metaFile}.tmp-${process.pid}-${Date.now()}-${Math.random().toString(16).slice(2)}`
|
|
85
|
+
fs.writeFileSync(metaTmp, JSON.stringify({ contentType, size: buffer.length }))
|
|
86
|
+
fs.renameSync(metaTmp, metaFile)
|
|
87
|
+
return { id, key: id, url: `/assets/${id}`, size: buffer.length, contentType }
|
|
88
|
+
},
|
|
89
|
+
get(id) {
|
|
90
|
+
if (!validId(id)) return null
|
|
91
|
+
const file = path.join(dir, path.basename(String(id)))
|
|
92
|
+
try {
|
|
93
|
+
const data = fs.readFileSync(file)
|
|
94
|
+
/* 内容寻址仓库必须复核 hash:截断/篡改的对象不能静默返回。 */
|
|
95
|
+
if (sha256(data) !== id) {
|
|
96
|
+
console.error(`[asset-service] 对象校验失败,拒绝返回: ${id}`)
|
|
97
|
+
return null
|
|
98
|
+
}
|
|
99
|
+
let contentType = 'application/octet-stream'
|
|
100
|
+
try { contentType = JSON.parse(fs.readFileSync(file + '.meta.json', 'utf8')).contentType || contentType } catch { /* 默认 */ }
|
|
101
|
+
return { data, contentType }
|
|
102
|
+
} catch {
|
|
103
|
+
return null
|
|
104
|
+
}
|
|
105
|
+
},
|
|
106
|
+
has(id) {
|
|
107
|
+
if (!validId(id)) return false
|
|
108
|
+
try { return fs.existsSync(path.join(dir, path.basename(String(id)))) } catch { return false }
|
|
109
|
+
},
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** 防止 text/html / svg 在 loopback 同源执行脚本;图片仍按原 MIME 内联。 */
|
|
114
|
+
function safeContentType(type) {
|
|
115
|
+
const base = String(type || '').split(';')[0].trim().toLowerCase() || 'application/octet-stream'
|
|
116
|
+
if (base === 'text/html' || base === 'application/xhtml+xml' || base === 'image/svg+xml') {
|
|
117
|
+
return 'text/plain; charset=utf-8'
|
|
118
|
+
}
|
|
119
|
+
return base
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* service-host 兼容的资产路由。
|
|
124
|
+
* GET/HEAD `/assets/<id>`(同时兼容 `<basePath>/assets/<id>`);
|
|
125
|
+
* POST `/assets` 或 `<basePath>/assets`(JSON: { data: base64, contentType })。
|
|
126
|
+
*/
|
|
127
|
+
export function createAssetRoutes({ assetStore, basePath = '', maxUploadBytes = 10 * 1024 * 1024 } = {}) {
|
|
128
|
+
const prefix = basePath === '/' ? '' : String(basePath || '').replace(/\/+$/, '')
|
|
129
|
+
const getPrefixes = ['/assets/', prefix ? `${prefix}/assets/` : null].filter(Boolean)
|
|
130
|
+
const postPaths = new Set(['/assets', prefix ? `${prefix}/assets` : null].filter(Boolean))
|
|
131
|
+
return async function assetRoutes(req, res, u) {
|
|
132
|
+
const p = u.pathname
|
|
133
|
+
if (req.method === 'GET' || req.method === 'HEAD') {
|
|
134
|
+
const getPrefix = getPrefixes.find((candidate) => p.startsWith(candidate))
|
|
135
|
+
if (getPrefix) {
|
|
136
|
+
if (!assetStore) { send(req, res, 404, { error: 'asset store unavailable' }); return true }
|
|
137
|
+
const id = decodeURIComponent(p.slice(getPrefix.length))
|
|
138
|
+
if (!validId(id)) { send(req, res, 404, { error: 'asset not found' }); return true }
|
|
139
|
+
const asset = assetStore.get(id)
|
|
140
|
+
if (!asset) { send(req, res, 404, { error: 'asset not found' }); return true }
|
|
141
|
+
res.writeHead(200, {
|
|
142
|
+
'content-type': safeContentType(asset.contentType),
|
|
143
|
+
'content-length': asset.data.length,
|
|
144
|
+
'cache-control': 'public, max-age=31536000, immutable',
|
|
145
|
+
'x-content-type-options': 'nosniff',
|
|
146
|
+
'referrer-policy': 'no-referrer',
|
|
147
|
+
})
|
|
148
|
+
res.end(req.method === 'HEAD' ? undefined : asset.data)
|
|
149
|
+
return true
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
if (postPaths.has(p) && req.method === 'POST') {
|
|
153
|
+
if (!assetStore) { send(req, res, 503, { error: 'asset store unavailable' }); return true }
|
|
154
|
+
let body
|
|
155
|
+
try { body = await readBody(req, maxUploadBytes) } catch (error) {
|
|
156
|
+
const status = error?.code === 'BODY_TOO_LARGE' ? 413 : 400
|
|
157
|
+
send(req, res, status, { error: String(error.message || error) })
|
|
158
|
+
return true
|
|
159
|
+
}
|
|
160
|
+
if (!body || typeof body.data !== 'string' || !body.data) {
|
|
161
|
+
send(req, res, 400, { error: '缺少图片数据' })
|
|
162
|
+
return true
|
|
163
|
+
}
|
|
164
|
+
const buffer = Buffer.from(body.data, 'base64')
|
|
165
|
+
/* 严格 base64:非法字符 / 错误填充 / 非规范编码一律 400,不再宽松解码把损坏
|
|
166
|
+
* 字节静默入库(B 轴 P0-7)。round-trip 比对同时挡住非规范填充位。 */
|
|
167
|
+
if (!/^[A-Za-z0-9+/]*={0,2}$/.test(body.data) || body.data.length % 4 !== 0 || buffer.toString('base64') !== body.data) {
|
|
168
|
+
send(req, res, 400, { error: '图片数据不是合法 base64' })
|
|
169
|
+
return true
|
|
170
|
+
}
|
|
171
|
+
if (!buffer.length) { send(req, res, 400, { error: '图片数据为空' }); return true }
|
|
172
|
+
const asset = await assetStore.put(buffer, body.contentType || 'application/octet-stream')
|
|
173
|
+
send(req, res, 200, { ok: true, ...asset })
|
|
174
|
+
return true
|
|
175
|
+
}
|
|
176
|
+
return false
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export function createAssetHost(config = {}) {
|
|
181
|
+
const defaultStore = config.storeClient
|
|
182
|
+
? createWorkspaceAssetStore({ storeClient: config.storeClient, workId: config.workId })
|
|
183
|
+
: config.dir
|
|
184
|
+
? createFileAssetStore({ dir: config.dir })
|
|
185
|
+
: null
|
|
186
|
+
|
|
187
|
+
return {
|
|
188
|
+
api: createServiceApi('assets', '1.0.0', ['workspace-store', 'file-store', 'routes', 'sha256', 'mount']),
|
|
189
|
+
version: '0.1.0',
|
|
190
|
+
createStore: createWorkspaceAssetStore,
|
|
191
|
+
createFileStore: createFileAssetStore,
|
|
192
|
+
routes: createAssetRoutes,
|
|
193
|
+
store: defaultStore,
|
|
194
|
+
/**
|
|
195
|
+
* 每个 service-host app 只挂一次资产路由,避免多个插件重复争抢 /assets。
|
|
196
|
+
* @param {object} hostApp createServiceApp() 返回值
|
|
197
|
+
* @param {{ store?: object, basePath?: string }} options
|
|
198
|
+
*/
|
|
199
|
+
mount(hostApp, { store = defaultStore, basePath = '' } = {}) {
|
|
200
|
+
if (!hostApp || typeof hostApp.use !== 'function') throw new Error('assets.mount 需要 service-host app')
|
|
201
|
+
if (!store) throw new Error('assets.mount: 没有可用 store')
|
|
202
|
+
if (hostApp.__dshAssetsMounted) return () => {}
|
|
203
|
+
hostApp.__dshAssetsMounted = true
|
|
204
|
+
const unuse = hostApp.use(createAssetRoutes({ assetStore: store, basePath }))
|
|
205
|
+
let disposed = false
|
|
206
|
+
return () => {
|
|
207
|
+
if (disposed) return
|
|
208
|
+
disposed = true
|
|
209
|
+
/* 真正卸载路由;否则热重载后旧 store 仍会抢答 /assets。 */
|
|
210
|
+
if (typeof unuse === 'function') unuse()
|
|
211
|
+
hostApp.__dshAssetsMounted = false
|
|
212
|
+
}
|
|
213
|
+
},
|
|
214
|
+
async put(value, contentType) {
|
|
215
|
+
if (!defaultStore) throw new Error('asset-service: 未配置 storeClient/dir')
|
|
216
|
+
return defaultStore.put(value, contentType)
|
|
217
|
+
},
|
|
218
|
+
get(id) {
|
|
219
|
+
if (!defaultStore) throw new Error('asset-service: 未配置 storeClient/dir')
|
|
220
|
+
return defaultStore.get(id)
|
|
221
|
+
},
|
|
222
|
+
}
|
|
223
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# @capyclaw/foundation
|
|
2
|
+
|
|
3
|
+
dsh 插件基石层:接口 ABI、插件加载器与内核服务 `ctx.os`。
|
|
4
|
+
|
|
5
|
+
应用插件不再写 `inject` / `apply` / provider 装配,只声明「我实现了哪个接口」:
|
|
6
|
+
|
|
7
|
+
```js
|
|
8
|
+
import { createPlugin, skillProvider } from '@capyclaw/foundation/abi'
|
|
9
|
+
|
|
10
|
+
export const { name, inject, apply } = createPlugin({
|
|
11
|
+
name: 'canvas',
|
|
12
|
+
requires: ['canvas.runtime@^1'],
|
|
13
|
+
implements: [skillProvider({ skill: './skills/canvas' })],
|
|
14
|
+
}, import.meta.url)
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## 四个入口
|
|
18
|
+
|
|
19
|
+
| 入口 | 内容 | 谁用 |
|
|
20
|
+
|---|---|---|
|
|
21
|
+
| `.` | Cordis 插件,产出 `ctx.os` | dsh profile |
|
|
22
|
+
| `./abi` | 描述符构建器、加载器、错误码、版本工具 | 应用插件 |
|
|
23
|
+
| `./kit` | plugin-kit 兼容导出,包含 Node/dsh 耦合,非纯函数库 | 兼容调用方 |
|
|
24
|
+
| `./runtime` | `createKernel` 等宿主 API | dsh 之外的宿主(独立技能服务、测试夹具) |
|
|
25
|
+
|
|
26
|
+
## 状态
|
|
27
|
+
|
|
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` 驱动入口。
|
|
35
|
+
- 能力表按 `ctx` 实际服务探测(见 `lib/kernel.mjs` 的 `detectCapabilities`),不做空头承诺。
|
|
36
|
+
- 当前仓库:27 个插件走标准接口,`raw@1` 当前为空,6 个基建插件走
|
|
37
|
+
`defineDriver` 驱动层;`meta/foundation.allowlist.json` 的模糊豁免为 0。
|
|
38
|
+
详见 `docs/POLICY.md` 与 `docs/spec/plugin-abi.md`。
|
|
39
|
+
|
|
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
|
+
|
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ABI 错误模型:稳定的错误码 + 结构化 detail。
|
|
3
|
+
*
|
|
4
|
+
* 约定:
|
|
5
|
+
* - 应用插件按 code 分支,不解析 message 文案;
|
|
6
|
+
* - message 面向人,detail 面向程序;
|
|
7
|
+
* - 所有加载期失败都必须是 AbiError,不允许裸 Error 穿透。
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/** 稳定错误码。新增只能追加,不得改语义。 */
|
|
11
|
+
export const ERROR_CODES = Object.freeze({
|
|
12
|
+
/** 插件入口形状不对:缺 name / 没有 implements / 导出了 apply 等。 */
|
|
13
|
+
E_PLUGIN_SHAPE: 'E_PLUGIN_SHAPE',
|
|
14
|
+
/** 插件声明的 ABI 主版本与内核不兼容。 */
|
|
15
|
+
E_ABI_MISMATCH: 'E_ABI_MISMATCH',
|
|
16
|
+
/** 描述符的 kind 不在内核认识的接口表里。 */
|
|
17
|
+
E_KIND_UNKNOWN: 'E_KIND_UNKNOWN',
|
|
18
|
+
/** 描述符字段不满足接口 schema。 */
|
|
19
|
+
E_SCHEMA: 'E_SCHEMA',
|
|
20
|
+
/** 内核没有提供该能力(通常是 os.* 子系统缺失)。 */
|
|
21
|
+
E_CAP_MISSING: 'E_CAP_MISSING',
|
|
22
|
+
/** 唯一性接口被第二个插件实现(例如同 id 的 runtime-driver)。 */
|
|
23
|
+
E_DUP_INTERFACE: 'E_DUP_INTERFACE',
|
|
24
|
+
/** 同名插件被重复挂载。 */
|
|
25
|
+
E_DUP_PLUGIN: 'E_DUP_PLUGIN',
|
|
26
|
+
/** 内核没有为这个 kind 注册适配器(接口定义存在但实现未就绪)。 */
|
|
27
|
+
E_KIND_UNAVAILABLE: 'E_KIND_UNAVAILABLE',
|
|
28
|
+
/** 适配器在编译期抛错。 */
|
|
29
|
+
E_ADAPTER_THREW: 'E_ADAPTER_THREW',
|
|
30
|
+
/** 插件入口没有拿到 baseUrl,相对路径无法解析。 */
|
|
31
|
+
E_BASE_URL_MISSING: 'E_BASE_URL_MISSING',
|
|
32
|
+
/** 运行时没有 ctx.os:foundation 未安装或未启用。 */
|
|
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',
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
export class AbiError extends Error {
|
|
41
|
+
/**
|
|
42
|
+
* @param {string} code ERROR_CODES 中的一项
|
|
43
|
+
* @param {string} message 面向人的说明
|
|
44
|
+
* @param {Record<string, unknown>} [detail] 面向程序的结构化信息
|
|
45
|
+
*/
|
|
46
|
+
constructor(code, message, detail = {}) {
|
|
47
|
+
super(`[${code}] ${message}`)
|
|
48
|
+
this.name = 'AbiError'
|
|
49
|
+
this.code = code
|
|
50
|
+
this.detail = detail
|
|
51
|
+
if (detail.cause instanceof Error) this.cause = detail.cause
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function isAbiError(error) {
|
|
56
|
+
return error instanceof AbiError
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** 把任意异常归一成 AbiError(已归一的原样返回)。 */
|
|
60
|
+
export function toAbiError(error, code = ERROR_CODES.E_ADAPTER_THREW, detail = {}) {
|
|
61
|
+
if (error instanceof AbiError) return error
|
|
62
|
+
return new AbiError(code, error?.message || String(error), { ...detail, cause: error })
|
|
63
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/** 接口 ABI 的唯一入口:应用插件只需要 import 这个模块。 */
|
|
2
|
+
export { ABI_MAJOR, INTERFACE_KINDS, INTERFACE_KIND_NAMES, getInterfaceKind } from './kinds.mjs'
|
|
3
|
+
export {
|
|
4
|
+
defineInterface,
|
|
5
|
+
definePlugin,
|
|
6
|
+
createPlugin,
|
|
7
|
+
skillProvider,
|
|
8
|
+
toolProvider,
|
|
9
|
+
runtimeDriver,
|
|
10
|
+
httpRouter,
|
|
11
|
+
uiPanel,
|
|
12
|
+
lifecycleHook,
|
|
13
|
+
hostEvent,
|
|
14
|
+
clientPanel,
|
|
15
|
+
ownServer,
|
|
16
|
+
serviceProvider,
|
|
17
|
+
defineDriver,
|
|
18
|
+
rawPlugin,
|
|
19
|
+
} from './spec.mjs'
|
|
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'
|
|
32
|
+
export { validateInterface } from './validate.mjs'
|
|
33
|
+
export { AbiError, ERROR_CODES, isAbiError, toAbiError } from './errors.mjs'
|
|
34
|
+
export { validateSchema } from './schema.mjs'
|
|
35
|
+
export { parseVersion, compareVersions, satisfies } from './version.mjs'
|