@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,338 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* work-store → ddr0 画廊桥接(本地作品浏览前端的后端)。
|
|
3
|
+
*
|
|
4
|
+
* 设计原则:
|
|
5
|
+
* - 前端原样复用 ddr0 单文件 SPA(static/index.html),不改不加料;
|
|
6
|
+
* - 完全离线、完全本地:数据只来自 work-store 的 SQLite(~/.dsh/works),不出网;
|
|
7
|
+
* - 免登录 / 免权限 / 免好友关系:auth/gate/社交由 mirror 的 guest/空载荷桩掉,
|
|
8
|
+
* 本桥只供 SPA 真正需要的面——列表 / 详情 / 装饰器代码 / viewSource / .works 包;
|
|
9
|
+
* - PPT 作品按 ddr0 本来的机制呈现:作品声明 view.decorator 钉住 PPTist 只读放映
|
|
10
|
+
* 装饰器(WORKS-3686),幻灯片数据走 view.source 分节(WORKFLOW-SOURCE)。
|
|
11
|
+
*
|
|
12
|
+
* 分层(STANDARD v2 附录 A):
|
|
13
|
+
* - wire 契约(字段映射 / 频道分类 / 分节标记)→ lib/wire.mjs(与本地 JSON 库共源);
|
|
14
|
+
* - mirror 清单索引 / 代码包读取 → lib/mirror-index.mjs(与 mirror.mjs 共读);
|
|
15
|
+
* - 原子写 → lib/atomic.mjs(与 store.mjs 共用,不再各造轮子)。
|
|
16
|
+
* 本模块只保留桥接状态:只读句柄、数字 id ↔ work-store id 映射、标题解析缓存。
|
|
17
|
+
* 只读 work-store,不写业务数据;唯一持久化的是 id 映射(ddr0 SPA 按数字 id 路由)。
|
|
18
|
+
*/
|
|
19
|
+
import fs from 'node:fs'
|
|
20
|
+
import path from 'node:path'
|
|
21
|
+
import { createWorkStore } from '@capyclaw/work-store/src'
|
|
22
|
+
import { writeJsonAtomic } from './atomic.mjs'
|
|
23
|
+
import { createMirrorIndex } from './mirror-index.mjs'
|
|
24
|
+
import {
|
|
25
|
+
WS_ID_BASE, DECORATOR_WORK, DECORATOR_REF,
|
|
26
|
+
SOURCE_SECTION, META_SECTION, CODE_SECTION,
|
|
27
|
+
CHANNEL_BY_DECORATOR, CHANNEL_LABELS, CHANNEL_EMOJI,
|
|
28
|
+
categoryOfItem, decoratorNum, workstoreWire,
|
|
29
|
+
} from './wire.mjs'
|
|
30
|
+
import { LOCAL_SUITE } from './workstore-repository.mjs'
|
|
31
|
+
|
|
32
|
+
// wire 契约工具按原出口 re-export(测试/消费方仍从本模块 import)。
|
|
33
|
+
export { categoryOfItem } from './wire.mjs'
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* 把本地作品合并进列表响应,并按频道分组:
|
|
37
|
+
* 分区列表只前置「属于该频道」的本地作品;收藏/搜索/总表前置全部。仅第一页前置,深翻页不重复。
|
|
38
|
+
*/
|
|
39
|
+
export function mergeLocalItems(pathname, body, search, locals) {
|
|
40
|
+
if (!body || !Array.isArray(body.items)) return body
|
|
41
|
+
const m = /^\/api\/(my\/favorites|community\/category\/([a-z_]+)|worklist|search)/.exec(pathname || '')
|
|
42
|
+
if (!m) return body
|
|
43
|
+
const offset = Number((/[?&]offset=(\d+)/.exec(search || '') || [])[1] || 0)
|
|
44
|
+
if (offset > 0) return body
|
|
45
|
+
const channel = m[2] || null
|
|
46
|
+
const picked = (locals || []).filter((it) => !channel || categoryOfItem(it) === channel)
|
|
47
|
+
if (!picked.length) return body
|
|
48
|
+
// 预装作品与镜像里同一 ddr0 作品去重:本地(数据库)版本优先,剔除镜像重复项。
|
|
49
|
+
const localDdr0 = new Set(picked.map((it) => it && it.ddr0_id).filter((v) => v != null))
|
|
50
|
+
const remoteRaw = localDdr0.size ? (body.items || []).filter((it) => !localDdr0.has(it && it.id)) : (body.items || [])
|
|
51
|
+
const removed = (body.items || []).length - remoteRaw.length
|
|
52
|
+
return { ...body, items: [...picked.map(truncDesc), ...remoteRaw.map(truncDesc)], total: (Number(body.total) || 0) + picked.length - removed }
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** 列表只带截断描述(参考服务端 description_truncated),详情端点才给全文。 */
|
|
56
|
+
function truncDesc(it) {
|
|
57
|
+
const d = (it && it.description) || ''
|
|
58
|
+
if (d.length <= 140) return it
|
|
59
|
+
return { ...it, description: d.slice(0, 140), description_truncated: true, description_next_offset: 140 }
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function readJsonSafe(file) {
|
|
63
|
+
try { return JSON.parse(fs.readFileSync(file, 'utf8')) } catch { return null }
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function createWorkStoreSource({ dir, mirrorDir, log, env, index, store: injectedStore } = {}) {
|
|
67
|
+
const idmapFile = path.join(dir || '.', 'ws-idmap.json')
|
|
68
|
+
try { fs.mkdirSync(path.dirname(idmapFile), { recursive: true }) } catch { /* ignore */ }
|
|
69
|
+
|
|
70
|
+
// mirror 清单索引:宿主半注入共享实例时整份 manifest 只解析一次;未注入则自建。
|
|
71
|
+
const mirrorIndex = index || (mirrorDir ? createMirrorIndex(mirrorDir, { log }) : null)
|
|
72
|
+
|
|
73
|
+
/* ---- work-store 句柄 ----
|
|
74
|
+
注入式(宿主提供 ctx.workStore,STANDARD v2):用共享服务,不自开句柄、不负责 close。
|
|
75
|
+
独立式(测试/旧用法):懒开只读句柄,失败降级为空,绝不让宿主崩。 */
|
|
76
|
+
let ownStore = null
|
|
77
|
+
let storeFailed = false
|
|
78
|
+
function getStore() {
|
|
79
|
+
if (injectedStore) return injectedStore
|
|
80
|
+
if (ownStore) return ownStore
|
|
81
|
+
if (storeFailed) return null
|
|
82
|
+
try {
|
|
83
|
+
ownStore = createWorkStore({ env })
|
|
84
|
+
return ownStore
|
|
85
|
+
} catch (err) {
|
|
86
|
+
storeFailed = true
|
|
87
|
+
log?.warn?.(`[workstore] 打不开本地作品库,画廊仅显示镜像/本地 JSON 作品:${err?.message || err}`)
|
|
88
|
+
return null
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/* ---- 数字 id ↔ work-store id 双向映射(持久化,稳定跨重启) ----
|
|
93
|
+
注入式落点 work_meta 'ddr0.wsIdmap'(与迁移一致);缺省回退旧 ws-idmap.json 再 fresh,
|
|
94
|
+
首次 flush 即把旧文件映射搬进 work_meta,id 不重排。独立式仍走文件。 */
|
|
95
|
+
function loadIdmap() {
|
|
96
|
+
if (injectedStore) {
|
|
97
|
+
const m = injectedStore.getMeta('ddr0.wsIdmap', null)
|
|
98
|
+
if (m && typeof m === 'object') return m
|
|
99
|
+
}
|
|
100
|
+
return readJsonSafe(idmapFile) || { next: WS_ID_BASE, byWork: {} }
|
|
101
|
+
}
|
|
102
|
+
let idmap = loadIdmap()
|
|
103
|
+
if (!idmap.byWork) idmap.byWork = {}
|
|
104
|
+
if (!Number.isFinite(idmap.next)) idmap.next = WS_ID_BASE
|
|
105
|
+
let byNumeric = new Map()
|
|
106
|
+
for (const [workId, n] of Object.entries(idmap.byWork)) byNumeric.set(Number(n), workId)
|
|
107
|
+
let idmapDirty = false
|
|
108
|
+
function flushIdmap() {
|
|
109
|
+
if (!idmapDirty) return
|
|
110
|
+
try {
|
|
111
|
+
if (injectedStore) injectedStore.setMeta('ddr0.wsIdmap', idmap)
|
|
112
|
+
else writeJsonAtomic(idmapFile, idmap)
|
|
113
|
+
idmapDirty = false
|
|
114
|
+
} catch { /* 读多写少,失败下次再试 */ }
|
|
115
|
+
}
|
|
116
|
+
function ensureId(workId) {
|
|
117
|
+
let n = idmap.byWork[workId]
|
|
118
|
+
if (!Number.isFinite(n)) {
|
|
119
|
+
n = idmap.next++
|
|
120
|
+
idmap.byWork[workId] = n
|
|
121
|
+
byNumeric.set(n, workId)
|
|
122
|
+
idmapDirty = true
|
|
123
|
+
}
|
|
124
|
+
return n
|
|
125
|
+
}
|
|
126
|
+
function workIdOf(numericId) {
|
|
127
|
+
return byNumeric.get(Number(numericId)) || null
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/* ---- mirror 代码读取:共享索引 O(1) 查 bundles,本地按作品号缓存(含 null) ----
|
|
131
|
+
缓存键带 manifest mtime:主站同步改写 manifest 后索引重建,旧缓存自动失效,
|
|
132
|
+
无需手动清(invalidateCodeCache 仍保留供显式调用)。 */
|
|
133
|
+
const mirrorCodeCache = new Map()
|
|
134
|
+
function mirrorMtime() {
|
|
135
|
+
try { return mirrorIndex?.load?.()?.mtime ?? 0 } catch { return 0 }
|
|
136
|
+
}
|
|
137
|
+
function readMirrorCode(workNum, exts) {
|
|
138
|
+
const key = `${mirrorMtime()}:${workNum}:${exts.join('|')}`
|
|
139
|
+
if (mirrorCodeCache.has(key)) return mirrorCodeCache.get(key)
|
|
140
|
+
let buf = null
|
|
141
|
+
try {
|
|
142
|
+
if (mirrorIndex) buf = mirrorIndex.codeFor(workNum, exts)
|
|
143
|
+
} catch (err) {
|
|
144
|
+
log?.warn?.(`[workstore] 读 mirror 代码 ${workNum} 失败:${err?.message || err}`)
|
|
145
|
+
}
|
|
146
|
+
if (mirrorCodeCache.size > 256) mirrorCodeCache.clear()
|
|
147
|
+
mirrorCodeCache.set(key, buf)
|
|
148
|
+
return buf
|
|
149
|
+
}
|
|
150
|
+
/** 显式清代码缓存(主站同步后调用;mtime 键已自动失效,此为兜底)。 */
|
|
151
|
+
function invalidateCodeCache() {
|
|
152
|
+
mirrorCodeCache.clear()
|
|
153
|
+
}
|
|
154
|
+
/** 作品挂的装饰器作品号:预装看 meta.ddr0.decorator,PPT 生成物默认 PPTist 3686。 */
|
|
155
|
+
function decoratorWorkNum(numericId) {
|
|
156
|
+
const workId = workIdOf(numericId)
|
|
157
|
+
const s = getStore()
|
|
158
|
+
let dec = ''
|
|
159
|
+
if (workId && s) {
|
|
160
|
+
try { const w = s.getWork(workId); dec = (w && w.meta && w.meta.ddr0 && w.meta.ddr0.decorator) || '' } catch { /* ignore */ }
|
|
161
|
+
}
|
|
162
|
+
if (!dec) dec = DECORATOR_REF
|
|
163
|
+
return decoratorNum(dec) || DECORATOR_WORK
|
|
164
|
+
}
|
|
165
|
+
function getDecoratorCode(numericId) {
|
|
166
|
+
const n = numericId == null ? DECORATOR_WORK : decoratorWorkNum(numericId)
|
|
167
|
+
return readMirrorCode(n, ['js', 'decorator.js'])
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/* ---- 标题解析:work.title 常为空(迁移只存了会话级标题),优先取 deck 内真实标题 ----
|
|
171
|
+
解析结果持久化进 ws-idmap.json 的 titles 段:冷启动不再为每个作品读整份 deck
|
|
172
|
+
(列表只要元数据,参考服务端「列表不取正文」),updatedAt 变了才重读一次。 */
|
|
173
|
+
const titleCache = new Map()
|
|
174
|
+
if (!idmap.titles) idmap.titles = {}
|
|
175
|
+
function resolveTitle(work) {
|
|
176
|
+
const t = work.title || ''
|
|
177
|
+
if (t && !/^session-/.test(t) && t !== work.sessionId) return t
|
|
178
|
+
const hit = titleCache.get(work.id) || idmap.titles[work.id]
|
|
179
|
+
if (hit && hit.updatedAt === work.updatedAt) return hit.title || work.brief || '未命名演示文稿'
|
|
180
|
+
let deckTitle = ''
|
|
181
|
+
const s = getStore()
|
|
182
|
+
if (s) { try { const d = s.getDocument(work.id, 'deck'); deckTitle = (d && d.title) || '' } catch { /* ignore */ } }
|
|
183
|
+
const rec = { updatedAt: work.updatedAt, title: deckTitle }
|
|
184
|
+
titleCache.set(work.id, rec)
|
|
185
|
+
idmap.titles[work.id] = rec
|
|
186
|
+
idmapDirty = true
|
|
187
|
+
return deckTitle || work.brief || '未命名演示文稿'
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/* ---- work-store 作品 → ddr0 item(字段映射在 lib/wire.mjs,这里只解析桥接状态) ---- */
|
|
191
|
+
function toItem(work, numericId, opts = {}) {
|
|
192
|
+
const id = numericId ?? ensureId(work.id)
|
|
193
|
+
return workstoreWire({ work, id, title: resolveTitle(work), truncateDesc: opts.truncateDesc })
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// 分页取全量:listWorks 单页上限 500,过去硬截断 500 会静默丢作品。
|
|
197
|
+
// 逐页 offset 推进直到取满 total 或末页不足 500;上限 200 页(10 万件)防失控。
|
|
198
|
+
function listItems() {
|
|
199
|
+
const s = getStore()
|
|
200
|
+
if (!s) return []
|
|
201
|
+
const works = []
|
|
202
|
+
try {
|
|
203
|
+
const PAGE = 500
|
|
204
|
+
for (let page = 0; page < 200; page += 1) {
|
|
205
|
+
const res = s.listWorks({ limit: PAGE, offset: page * PAGE })
|
|
206
|
+
const batch = (res && res.items) || []
|
|
207
|
+
works.push(...batch)
|
|
208
|
+
const total = Number(res && res.total)
|
|
209
|
+
if (batch.length < PAGE || (Number.isFinite(total) && works.length >= total)) break
|
|
210
|
+
}
|
|
211
|
+
} catch { return [] }
|
|
212
|
+
// 本地库作品(suite=ddr0-local)由 repository 供给,桥接这里剔除以免重复计数。
|
|
213
|
+
const galleryWorks = works.filter((w) => w.suite !== LOCAL_SUITE)
|
|
214
|
+
const items = galleryWorks.map((w) => toItem(w, undefined, { truncateDesc: true }))
|
|
215
|
+
flushIdmap()
|
|
216
|
+
return items
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
function getItem(numericId) {
|
|
220
|
+
const workId = workIdOf(numericId)
|
|
221
|
+
if (!workId) return null
|
|
222
|
+
const s = getStore()
|
|
223
|
+
if (!s) return null
|
|
224
|
+
let work = null
|
|
225
|
+
try { work = s.getWork(workId) } catch { return null }
|
|
226
|
+
return work ? toItem(work, Number(numericId)) : null
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
function has(numericId) {
|
|
230
|
+
return !!workIdOf(numericId)
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
function getDeck(numericId) {
|
|
234
|
+
const workId = workIdOf(numericId)
|
|
235
|
+
if (!workId) return null
|
|
236
|
+
const s = getStore()
|
|
237
|
+
if (!s) return null
|
|
238
|
+
try { return s.getDocument(workId, 'deck') } catch { return null }
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/** SPA WorkView 的 ctx.viewDoc:view.source 分节原文(幻灯片 JSON / 预装 view_source)。 */
|
|
242
|
+
function getViewSource(numericId) {
|
|
243
|
+
const deck = getDeck(numericId)
|
|
244
|
+
if (deck) return JSON.stringify(deck)
|
|
245
|
+
const workId = workIdOf(numericId)
|
|
246
|
+
const s = getStore()
|
|
247
|
+
if (workId && s) {
|
|
248
|
+
try { const doc = s.getDocument(workId); if (doc && typeof doc.view_source === 'string') return doc.view_source } catch { /* ignore */ }
|
|
249
|
+
}
|
|
250
|
+
return ''
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/** .works 单文件包:META(含 session_id,便于后续拓展)+ WORKFLOW-SOURCE(数据)+ CODE(代码,若有)。 */
|
|
254
|
+
function getBundle(numericId) {
|
|
255
|
+
const workId = workIdOf(numericId)
|
|
256
|
+
if (!workId) return null
|
|
257
|
+
const s = getStore()
|
|
258
|
+
if (!s) return null
|
|
259
|
+
let work = null
|
|
260
|
+
try { work = s.getWork(workId) } catch { return null }
|
|
261
|
+
if (!work) return null
|
|
262
|
+
const doc = (() => { try { return s.getDocument(workId) } catch { return null } })()
|
|
263
|
+
const deck = doc && doc.deck ? doc.deck : null
|
|
264
|
+
const ddr0 = (work.meta && work.meta.ddr0) || null
|
|
265
|
+
const decorator = ddr0 ? (ddr0.decorator || '') : DECORATOR_REF
|
|
266
|
+
const decNum = decoratorNum(decorator)
|
|
267
|
+
const category = (ddr0 && ddr0.category) || CHANNEL_BY_DECORATOR[decNum] || 'ppt'
|
|
268
|
+
const tagsArr = (ddr0 && Array.isArray(ddr0.tags) && ddr0.tags.length) ? ddr0.tags
|
|
269
|
+
: (Array.isArray(work.tags) && work.tags.length ? work.tags : [CHANNEL_LABELS[category] || 'PPT', '本地'])
|
|
270
|
+
const meta = {
|
|
271
|
+
name: resolveTitle(work),
|
|
272
|
+
brief: work.brief || '',
|
|
273
|
+
tags: tagsArr,
|
|
274
|
+
emoji: (ddr0 && ddr0.emoji) || CHANNEL_EMOJI[category] || '📑',
|
|
275
|
+
category,
|
|
276
|
+
view: decorator ? { kind: 'code', decorator, source: SOURCE_SECTION } : { kind: 'code' },
|
|
277
|
+
session_id: work.sessionId || '',
|
|
278
|
+
work_id: work.id,
|
|
279
|
+
suite: work.suite || 'ppt',
|
|
280
|
+
kind: work.kind || 'deck',
|
|
281
|
+
...(ddr0 ? { ddr0_id: ddr0.id, preinstalled: true } : {}),
|
|
282
|
+
}
|
|
283
|
+
const parts = [META_SECTION, JSON.stringify(meta, null, 2)]
|
|
284
|
+
if (deck) parts.push(SOURCE_SECTION, JSON.stringify(deck))
|
|
285
|
+
else if (doc && typeof doc.view_source === 'string' && doc.view_source) parts.push(SOURCE_SECTION, doc.view_source)
|
|
286
|
+
if (doc && typeof doc.code === 'string' && doc.code) parts.push(CODE_SECTION, doc.code)
|
|
287
|
+
return parts.join('\n') + '\n'
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/** 作品主体代码:优先库里存的 code(预装自包含),其次 mirror 同 id 代码包,最后占位。 */
|
|
291
|
+
function getCode(numericId) {
|
|
292
|
+
const workId = workIdOf(numericId)
|
|
293
|
+
const s = getStore()
|
|
294
|
+
if (workId && s) {
|
|
295
|
+
try {
|
|
296
|
+
const doc = s.getDocument(workId)
|
|
297
|
+
if (doc && typeof doc.code === 'string' && doc.code) return Buffer.from(doc.code, 'utf8')
|
|
298
|
+
const w = s.getWork(workId)
|
|
299
|
+
const ddr0 = w && w.meta && w.meta.ddr0
|
|
300
|
+
if (ddr0 && ddr0.id) {
|
|
301
|
+
const b = readMirrorCode(ddr0.id, ['js'])
|
|
302
|
+
if (b) return b
|
|
303
|
+
}
|
|
304
|
+
} catch { /* ignore */ }
|
|
305
|
+
}
|
|
306
|
+
return Buffer.from(getPlaceholderCode(numericId), 'utf8')
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/** 作品主体占位代码(work-v1 契约)。有装饰器时 SPA 走 decorator.js,此包仅兜底。 */
|
|
310
|
+
function getPlaceholderCode(numericId) {
|
|
311
|
+
const item = getItem(numericId)
|
|
312
|
+
const name = item ? item.name : '本地作品'
|
|
313
|
+
const safe = String(name).replace(/[<>&]/g, '')
|
|
314
|
+
return `(function () {
|
|
315
|
+
var DOC = ${JSON.stringify(`「${safe}」的看点在顶图:点顶图开 PPT 幻灯片只读放映(数据来自本地 work-store)。`)};
|
|
316
|
+
function mount(ctx) {
|
|
317
|
+
var stage = ctx && ctx.stage;
|
|
318
|
+
if (stage) stage.innerHTML = '<div style="padding:32px;font:14px/1.7 system-ui;color:#555">' + DOC + '</div>';
|
|
319
|
+
}
|
|
320
|
+
if (typeof window !== 'undefined' && window.Work && typeof window.Work.register === "function") {
|
|
321
|
+
window.Work.register({ mount: mount });
|
|
322
|
+
}
|
|
323
|
+
})();
|
|
324
|
+
`
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
function close() {
|
|
328
|
+
// 注入式共享服务由宿主驱动负责关闭(V2-1);独立式才关自己的句柄。
|
|
329
|
+
if (injectedStore) return
|
|
330
|
+
try { ownStore?.close() } catch { /* ignore */ }
|
|
331
|
+
ownStore = null
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
return {
|
|
335
|
+
listItems, getItem, has, getViewSource, getBundle, getPlaceholderCode, getCode, getDecoratorCode, invalidateCodeCache, close,
|
|
336
|
+
get enabled() { return !!getStore() },
|
|
337
|
+
}
|
|
338
|
+
}
|