@capyclaw/offline-ddr0 0.0.0-stage → 1.9.1

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.
Files changed (300) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +441 -2
  3. package/client/client.js +603 -0
  4. package/cordis.patch.yml +13 -0
  5. package/docs/STORAGE-MIGRATION.md +136 -0
  6. package/lib/atomic.mjs +40 -0
  7. package/lib/http-cache.mjs +42 -0
  8. package/lib/inbox.mjs +64 -0
  9. package/lib/media.mjs +242 -0
  10. package/lib/migrate-json.mjs +101 -0
  11. package/lib/mirror-index.mjs +105 -0
  12. package/lib/mirror.mjs +123 -0
  13. package/lib/proxy.mjs +188 -0
  14. package/lib/remote.mjs +158 -0
  15. package/lib/render.mjs +93 -0
  16. package/lib/repository.mjs +71 -0
  17. package/lib/store.mjs +610 -0
  18. package/lib/surface.mjs +74 -0
  19. package/lib/sync-ui.mjs +108 -0
  20. package/lib/sync.mjs +433 -0
  21. package/lib/wire.mjs +211 -0
  22. package/lib/workstore-repository.mjs +316 -0
  23. package/lib/workstore.mjs +348 -0
  24. package/mirror/files/00001.html +2045 -0
  25. package/mirror/files/00002.json +1 -0
  26. package/mirror/files/00003.json +1 -0
  27. package/mirror/files/00004.json +1 -0
  28. package/mirror/files/00005.json +1 -0
  29. package/mirror/files/00006.json +1 -0
  30. package/mirror/files/00007.json +1 -0
  31. package/mirror/files/00008.json +1 -0
  32. package/mirror/files/00009.json +1 -0
  33. package/mirror/files/00010.json +1 -0
  34. package/mirror/files/00011.json +1 -0
  35. package/mirror/files/00012.json +1 -0
  36. package/mirror/files/00013.json +1 -0
  37. package/mirror/files/00014.json +1 -0
  38. package/mirror/files/00015.json +1 -0
  39. package/mirror/files/00016.json +1 -0
  40. package/mirror/files/00017.json +1 -0
  41. package/mirror/files/00018.json +1 -0
  42. package/mirror/files/00019.json +1 -0
  43. package/mirror/files/00020.json +1 -0
  44. package/mirror/files/00021.json +1 -0
  45. package/mirror/files/00022.json +1 -0
  46. package/mirror/files/00023.js +205 -0
  47. package/mirror/files/00024.json +1 -0
  48. package/mirror/files/00025.json +1 -0
  49. package/mirror/files/00026.json +1 -0
  50. package/mirror/files/00027.js +51 -0
  51. package/mirror/files/00028.json +1 -0
  52. package/mirror/files/00029.json +1 -0
  53. package/mirror/files/00030.json +1 -0
  54. package/mirror/files/00031.json +1 -0
  55. package/mirror/files/00032.json +1 -0
  56. package/mirror/files/00033.json +1 -0
  57. package/mirror/files/00034.json +1 -0
  58. package/mirror/files/00035.json +1 -0
  59. package/mirror/files/00036.json +1 -0
  60. package/mirror/files/00037.json +1 -0
  61. package/mirror/files/00038.json +1 -0
  62. package/mirror/files/00039.json +1 -0
  63. package/mirror/files/00040.js +2311 -0
  64. package/mirror/files/00041.json +1 -0
  65. package/mirror/files/00042.json +1 -0
  66. package/mirror/files/00043.json +1 -0
  67. package/mirror/files/00044.json +1 -0
  68. package/mirror/files/00045.js +575 -0
  69. package/mirror/files/00046.json +1 -0
  70. package/mirror/files/00047.js +575 -0
  71. package/mirror/files/00048.js +575 -0
  72. package/mirror/files/00049.json +1 -0
  73. package/mirror/files/00050.json +1 -0
  74. package/mirror/files/00051.json +1 -0
  75. package/mirror/files/00052.json +1 -0
  76. package/mirror/files/00053.js +575 -0
  77. package/mirror/files/00054.json +1 -0
  78. package/mirror/files/00055.json +1 -0
  79. package/mirror/files/00056.js +575 -0
  80. package/mirror/files/00057.json +1 -0
  81. package/mirror/files/00058.json +1 -0
  82. package/mirror/files/00059.js +360 -0
  83. package/mirror/files/00060.json +1 -0
  84. package/mirror/files/00061.json +1 -0
  85. package/mirror/files/00062.json +1 -0
  86. package/mirror/files/00063.json +1 -0
  87. package/mirror/files/00064.json +1 -0
  88. package/mirror/files/00065.js +144 -0
  89. package/mirror/files/00066.js +7 -0
  90. package/mirror/files/00067.json +1 -0
  91. package/mirror/files/00068.json +1 -0
  92. package/mirror/files/00069.json +1 -0
  93. package/mirror/files/00070.json +1 -0
  94. package/mirror/files/00071.json +1 -0
  95. package/mirror/files/00072.json +1 -0
  96. package/mirror/files/00073.js +1 -0
  97. package/mirror/files/00074.json +1 -0
  98. package/mirror/files/00075.json +1 -0
  99. package/mirror/files/00076.json +1 -0
  100. package/mirror/files/00077.js +45 -0
  101. package/mirror/files/00078.js +1348 -0
  102. package/mirror/files/00079.json +1 -0
  103. package/mirror/files/00080.json +1 -0
  104. package/mirror/files/00081.js +1 -0
  105. package/mirror/files/00082.json +1 -0
  106. package/mirror/files/00083.json +1 -0
  107. package/mirror/files/00084.js +7 -0
  108. package/mirror/files/00085.json +1 -0
  109. package/mirror/files/00086.json +1 -0
  110. package/mirror/files/00087.js +7 -0
  111. package/mirror/files/00088.json +1 -0
  112. package/mirror/files/00089.json +1 -0
  113. package/mirror/files/00090.js +7 -0
  114. package/mirror/files/00091.json +1 -0
  115. package/mirror/files/00092.json +1 -0
  116. package/mirror/files/00093.js +102 -0
  117. package/mirror/files/00094.json +1 -0
  118. package/mirror/files/00095.json +1 -0
  119. package/mirror/files/00096.json +1 -0
  120. package/mirror/files/00097.json +1 -0
  121. package/mirror/files/00098.json +1 -0
  122. package/mirror/files/00099.json +1 -0
  123. package/mirror/files/00100.json +1 -0
  124. package/mirror/files/00101.js +102 -0
  125. package/mirror/files/00102.json +1 -0
  126. package/mirror/files/00103.json +1 -0
  127. package/mirror/files/00104.json +1 -0
  128. package/mirror/files/00105.json +1 -0
  129. package/mirror/files/00106.json +1 -0
  130. package/mirror/files/00107.js +110 -0
  131. package/mirror/files/00108.json +1 -0
  132. package/mirror/files/00109.json +1 -0
  133. package/mirror/files/00110.json +1 -0
  134. package/mirror/files/00111.json +1 -0
  135. package/mirror/files/00112.json +1 -0
  136. package/mirror/files/00113.json +1 -0
  137. package/mirror/files/00114.json +1 -0
  138. package/mirror/files/00115.json +1 -0
  139. package/mirror/files/00116.json +1 -0
  140. package/mirror/files/00117.json +1 -0
  141. package/mirror/files/00118.json +1 -0
  142. package/mirror/files/00119.json +1 -0
  143. package/mirror/files/00120.json +1 -0
  144. package/mirror/files/00121.json +1 -0
  145. package/mirror/files/00122.json +1 -0
  146. package/mirror/files/00123.json +1 -0
  147. package/mirror/files/00124.json +1 -0
  148. package/mirror/files/00125.json +1 -0
  149. package/mirror/files/00126.json +1 -0
  150. package/mirror/files/00127.json +1 -0
  151. package/mirror/files/00128.json +1 -0
  152. package/mirror/files/00129.json +1 -0
  153. package/mirror/files/00130.json +1 -0
  154. package/mirror/files/00131.json +1 -0
  155. package/mirror/files/00132.js +110 -0
  156. package/mirror/files/00133.json +1 -0
  157. package/mirror/files/00134.json +1 -0
  158. package/mirror/files/00135.json +1 -0
  159. package/mirror/files/00136.json +1 -0
  160. package/mirror/files/00137.js +26 -0
  161. package/mirror/files/00138.json +1 -0
  162. package/mirror/files/00139.json +1 -0
  163. package/mirror/files/00140.json +1 -0
  164. package/mirror/files/00141.json +1 -0
  165. package/mirror/files/00142.json +1 -0
  166. package/mirror/files/00143.json +1 -0
  167. package/mirror/files/00144.js +26 -0
  168. package/mirror/files/00145.json +1 -0
  169. package/mirror/files/00146.json +1 -0
  170. package/mirror/files/00147.js +29 -0
  171. package/mirror/files/00148.json +1 -0
  172. package/mirror/files/00149.json +1 -0
  173. package/mirror/files/00150.js +23 -0
  174. package/mirror/files/00151.json +1 -0
  175. package/mirror/files/00152.json +1 -0
  176. package/mirror/files/00153.js +719 -0
  177. package/mirror/files/00154.js +9575 -0
  178. package/mirror/files/00155.js +108 -0
  179. package/mirror/files/00156.js +379 -0
  180. package/mirror/files/00157.js +48 -0
  181. package/mirror/files/00158.json +16 -0
  182. package/mirror/manifest.json +1582 -0
  183. package/node_modules/@capyclaw/asset-service/README.md +48 -0
  184. package/node_modules/@capyclaw/asset-service/cordis.patch.yml +4 -0
  185. package/node_modules/@capyclaw/asset-service/dsh-suite.json +29 -0
  186. package/node_modules/@capyclaw/asset-service/lib/host.mjs +14 -0
  187. package/node_modules/@capyclaw/asset-service/package.json +59 -0
  188. package/node_modules/@capyclaw/asset-service/src/index.mjs +310 -0
  189. package/node_modules/@capyclaw/foundation/README.md +51 -0
  190. package/node_modules/@capyclaw/foundation/abi/assets.mjs +26 -0
  191. package/node_modules/@capyclaw/foundation/abi/bridge.mjs +88 -0
  192. package/node_modules/@capyclaw/foundation/abi/errors.mjs +63 -0
  193. package/node_modules/@capyclaw/foundation/abi/index.mjs +50 -0
  194. package/node_modules/@capyclaw/foundation/abi/kinds.mjs +271 -0
  195. package/node_modules/@capyclaw/foundation/abi/loader.mjs +352 -0
  196. package/node_modules/@capyclaw/foundation/abi/schema.mjs +98 -0
  197. package/node_modules/@capyclaw/foundation/abi/spec.mjs +258 -0
  198. package/node_modules/@capyclaw/foundation/abi/validate.mjs +24 -0
  199. package/node_modules/@capyclaw/foundation/abi/version.mjs +140 -0
  200. package/node_modules/@capyclaw/foundation/abi/visibility.mjs +90 -0
  201. package/node_modules/@capyclaw/foundation/cordis.patch.yml +5 -0
  202. package/node_modules/@capyclaw/foundation/kit/index.mjs +6 -0
  203. package/node_modules/@capyclaw/foundation/lib/adapters/client-panel.mjs +98 -0
  204. package/node_modules/@capyclaw/foundation/lib/adapters/host-event.mjs +35 -0
  205. package/node_modules/@capyclaw/foundation/lib/adapters/http-router.mjs +69 -0
  206. package/node_modules/@capyclaw/foundation/lib/adapters/lifecycle-hook.mjs +72 -0
  207. package/node_modules/@capyclaw/foundation/lib/adapters/own-server.mjs +113 -0
  208. package/node_modules/@capyclaw/foundation/lib/adapters/raw.mjs +69 -0
  209. package/node_modules/@capyclaw/foundation/lib/adapters/runtime-driver.mjs +120 -0
  210. package/node_modules/@capyclaw/foundation/lib/adapters/service-provider.mjs +77 -0
  211. package/node_modules/@capyclaw/foundation/lib/adapters/skill-provider.mjs +56 -0
  212. package/node_modules/@capyclaw/foundation/lib/adapters/tool-provider.mjs +50 -0
  213. package/node_modules/@capyclaw/foundation/lib/adapters/ui-panel.mjs +64 -0
  214. package/node_modules/@capyclaw/foundation/lib/ctx-service.mjs +42 -0
  215. package/node_modules/@capyclaw/foundation/lib/host/caps.mjs +38 -0
  216. package/node_modules/@capyclaw/foundation/lib/host/contract.mjs +88 -0
  217. package/node_modules/@capyclaw/foundation/lib/host/dsh.mjs +54 -0
  218. package/node_modules/@capyclaw/foundation/lib/host/memory.mjs +187 -0
  219. package/node_modules/@capyclaw/foundation/lib/host.mjs +23 -0
  220. package/node_modules/@capyclaw/foundation/lib/kernel.mjs +195 -0
  221. package/node_modules/@capyclaw/foundation/lib/skill/scan.mjs +115 -0
  222. package/node_modules/@capyclaw/foundation/lib/subsystems/events.mjs +60 -0
  223. package/node_modules/@capyclaw/foundation/lib/subsystems/log.mjs +51 -0
  224. package/node_modules/@capyclaw/foundation/lib/subsystems/net.mjs +198 -0
  225. package/node_modules/@capyclaw/foundation/package.json +53 -0
  226. package/node_modules/@capyclaw/foundation/runtime/index.mjs +20 -0
  227. package/node_modules/@capyclaw/plugin-kit/INTEGRATION.md +100 -0
  228. package/node_modules/@capyclaw/plugin-kit/README.md +70 -0
  229. package/node_modules/@capyclaw/plugin-kit/bin/sync.mjs +103 -0
  230. package/node_modules/@capyclaw/plugin-kit/package.json +71 -0
  231. package/node_modules/@capyclaw/plugin-kit/src/cli.mjs +56 -0
  232. package/node_modules/@capyclaw/plugin-kit/src/config.mjs +142 -0
  233. package/node_modules/@capyclaw/plugin-kit/src/editor/heartbeat-worker.js +132 -0
  234. package/node_modules/@capyclaw/plugin-kit/src/editor/heartbeat.mjs +26 -0
  235. package/node_modules/@capyclaw/plugin-kit/src/editor/lease.mjs +60 -0
  236. package/node_modules/@capyclaw/plugin-kit/src/editor/shadow.mjs +219 -0
  237. package/node_modules/@capyclaw/plugin-kit/src/editor/transport.js +13 -0
  238. package/node_modules/@capyclaw/plugin-kit/src/index.mjs +13 -0
  239. package/node_modules/@capyclaw/plugin-kit/src/pack.mjs +230 -0
  240. package/node_modules/@capyclaw/plugin-kit/src/runtime/context.mjs +128 -0
  241. package/node_modules/@capyclaw/plugin-kit/src/self-contained.mjs +307 -0
  242. package/node_modules/@capyclaw/plugin-kit/src/service/api.mjs +45 -0
  243. package/node_modules/@capyclaw/plugin-kit/src/service/http.mjs +113 -0
  244. package/node_modules/@capyclaw/plugin-kit/src/service/mcp-http.mjs +150 -0
  245. package/node_modules/@capyclaw/plugin-kit/src/service/server.mjs +131 -0
  246. package/node_modules/@capyclaw/plugin-kit/src/service/static.mjs +79 -0
  247. package/node_modules/@capyclaw/plugin-kit/src/skills/provider.mjs +82 -0
  248. package/node_modules/@capyclaw/plugin-kit/src/store/blob-store.mjs +109 -0
  249. package/node_modules/@capyclaw/plugin-kit/src/store/content-store.mjs +17 -0
  250. package/node_modules/@capyclaw/plugin-kit/src/store/journal-store.mjs +89 -0
  251. package/node_modules/@capyclaw/plugin-kit/src/tools/result.mjs +57 -0
  252. package/node_modules/@capyclaw/plugin-kit/src/tools/validate.mjs +150 -0
  253. package/node_modules/@capyclaw/plugin-kit/src/tuning.mjs +82 -0
  254. package/node_modules/@capyclaw/plugin-kit/src/util/fs-lock.mjs +132 -0
  255. package/node_modules/@capyclaw/plugin-kit/src/util/http.mjs +130 -0
  256. package/node_modules/@capyclaw/plugin-kit/src/workspace/manager.mjs +664 -0
  257. package/node_modules/@capyclaw/plugin-kit/src/zip.mjs +181 -0
  258. package/node_modules/@capyclaw/service-host/README.md +86 -0
  259. package/node_modules/@capyclaw/service-host/cordis.patch.yml +4 -0
  260. package/node_modules/@capyclaw/service-host/dsh-suite.json +29 -0
  261. package/node_modules/@capyclaw/service-host/lib/host.mjs +22 -0
  262. package/node_modules/@capyclaw/service-host/package.json +60 -0
  263. package/node_modules/@capyclaw/service-host/src/index.mjs +570 -0
  264. package/node_modules/@capyclaw/service-host/src/tuning.mjs +63 -0
  265. package/node_modules/@capyclaw/suite-kit/README.md +289 -0
  266. package/node_modules/@capyclaw/suite-kit/bin/dsh-domain.mjs +98 -0
  267. package/node_modules/@capyclaw/suite-kit/bin/dsh-suite.mjs +174 -0
  268. package/node_modules/@capyclaw/suite-kit/client/embed.js +35 -0
  269. package/node_modules/@capyclaw/suite-kit/domain.schema.json +129 -0
  270. package/node_modules/@capyclaw/suite-kit/package.json +58 -0
  271. package/node_modules/@capyclaw/suite-kit/src/activation-registry.mjs +184 -0
  272. package/node_modules/@capyclaw/suite-kit/src/artifacts.mjs +131 -0
  273. package/node_modules/@capyclaw/suite-kit/src/capability.mjs +181 -0
  274. package/node_modules/@capyclaw/suite-kit/src/compose.mjs +201 -0
  275. package/node_modules/@capyclaw/suite-kit/src/domain.mjs +138 -0
  276. package/node_modules/@capyclaw/suite-kit/src/embed.mjs +81 -0
  277. package/node_modules/@capyclaw/suite-kit/src/host.mjs +230 -0
  278. package/node_modules/@capyclaw/suite-kit/src/index.mjs +24 -0
  279. package/node_modules/@capyclaw/suite-kit/src/manifest.mjs +263 -0
  280. package/node_modules/@capyclaw/suite-kit/src/orchestration.mjs +100 -0
  281. package/node_modules/@capyclaw/suite-kit/src/profile.mjs +584 -0
  282. package/node_modules/@capyclaw/suite-kit/src/surface.mjs +28 -0
  283. package/node_modules/@capyclaw/suite-kit/suite.schema.json +128 -0
  284. package/node_modules/@capyclaw/work-store/README.md +83 -0
  285. package/node_modules/@capyclaw/work-store/STANDARD.md +263 -0
  286. package/node_modules/@capyclaw/work-store/cordis.patch.yml +3 -0
  287. package/node_modules/@capyclaw/work-store/dsh-suite.json +33 -0
  288. package/node_modules/@capyclaw/work-store/icon.svg +7 -0
  289. package/node_modules/@capyclaw/work-store/lib/host.mjs +21 -0
  290. package/node_modules/@capyclaw/work-store/package.json +68 -0
  291. package/node_modules/@capyclaw/work-store/src/backend.mjs +90 -0
  292. package/node_modules/@capyclaw/work-store/src/db.mjs +199 -0
  293. package/node_modules/@capyclaw/work-store/src/envelope.mjs +132 -0
  294. package/node_modules/@capyclaw/work-store/src/index.mjs +571 -0
  295. package/node_modules/@capyclaw/work-store/src/interop.mjs +70 -0
  296. package/node_modules/@capyclaw/work-store/src/refs.mjs +99 -0
  297. package/node_modules/@capyclaw/work-store/src/routes.mjs +135 -0
  298. package/package.json +82 -4
  299. package/plugin.mjs +644 -0
  300. package/static/index.html +2068 -0
package/lib/wire.mjs ADDED
@@ -0,0 +1,211 @@
1
+ /**
2
+ * ddr0 作品 wire 契约 —— 单一真源(STANDARD v2 附录 A:消费方 wire 层)。
3
+ *
4
+ * 前端 static/index.html 原样复用 ddr0 单文件 SPA,按这套字段形状路由/渲染。
5
+ * 本地 JSON 库(store.mjs)与 work-store 画廊桥(workstore.mjs)过去各维护一份
6
+ * ~45 字段映射,改一处忘另一处就漂移。这里把「共享骨架 + 各源差异」收敛到一处:
7
+ * - wireItem(fields):共享默认骨架,源只需覆盖差异字段;
8
+ * - localWire(input, id, ts):本地 JSON 作品(含会话型);
9
+ * - workstoreWire({work, id, title, truncateDesc}):work-store 作品(含预装);
10
+ * - launchRuntime(...):work-v1 启动契约;
11
+ * - 频道分类(categoryOfItem / 装饰器号解析)与 .works 分节标记也属 wire 契约。
12
+ *
13
+ * 纯函数、无 I/O、无存储状态:标题解析 / 数字 id 分配等桥接状态留在 workstore.mjs,
14
+ * 解析好后传进来。字段值与重构前逐一对齐(前端零改)。
15
+ */
16
+
17
+ /** 本地 work-store 作品的数字 id 基数。本地 JSON 库从 900000 起,互不撞。 */
18
+ export const WS_ID_BASE = 700000
19
+ /** PPTist 只读放映装饰器(mirror 里已打包,自包含离线)。 */
20
+ export const DECORATOR_WORK = 3686
21
+ export const DECORATOR_REF = 'WORKS-3686@15'
22
+ /** .works 单文件包分节标记。 */
23
+ export const SOURCE_SECTION = '%%%WORKS-WORKFLOW-SOURCE%%%'
24
+ export const META_SECTION = '%%%WORKS-META%%%'
25
+ export const CODE_SECTION = '%%%WORKS-CODE%%%'
26
+
27
+ /** ddr0 的频道(画廊左栏分组)。 */
28
+ export const CHANNELS = ['agent', 'game', 'ppt', 'word', 'excel', 'draw', 'skill', 'desktop_app']
29
+ /** 装饰器作品 → 频道:ddr0 真源站按「作品挂哪个装饰器」归类。 */
30
+ export const CHANNEL_BY_DECORATOR = { 3686: 'ppt', 3684: 'word', 3881: 'excel', 3691: 'draw', 3579: 'draw', 2592: 'agent' }
31
+ /** 频道 → 展示标签 / emoji(与真源站卡片观感一致)。 */
32
+ export const CHANNEL_LABELS = { ppt: 'PPT', word: 'WORD', excel: 'EXCEL', draw: '绘画', agent: '智能体', game: '游戏', desktop_app: '桌面应用', skill: '技能' }
33
+ export const CHANNEL_EMOJI = { ppt: '📑', word: '📃', excel: '📊', draw: '🎨', agent: '💬', game: '🎮', desktop_app: '💻', skill: '⚡' }
34
+ /** 无装饰器时(游戏/桌面应用/智能体等)按标签关键词归频道。 */
35
+ const TAG_CHANNEL = [
36
+ [/^(智能体|agent)$/i, 'agent'],
37
+ [/^(游戏|小游戏|game)$/i, 'game'],
38
+ [/^(ppt|演示|幻灯片)$/i, 'ppt'],
39
+ [/^(word|文档|canvas)$/i, 'word'],
40
+ [/^(excel|表格)$/i, 'excel'],
41
+ [/^(绘画|画板|笔迹|draw)$/i, 'draw'],
42
+ [/^(技能|skill)$/i, 'skill'],
43
+ [/^(桌面应用|桌面|desktop_app)$/i, 'desktop_app'],
44
+ ]
45
+
46
+ /** 'WORKS-3686@15' → 3686;空/非法 → 0。 */
47
+ export function decoratorNum(ref) {
48
+ if (!ref) return 0
49
+ return Number(String(ref).replace(/^WORKS-/i, '').split('@')[0]) || 0
50
+ }
51
+
52
+ /** 推导一个 ddr0 item 的频道:显式 category > 装饰器 > 标签关键词;都不命中返回 null。 */
53
+ export function categoryOfItem(item) {
54
+ if (!item || typeof item !== 'object') return null
55
+ if (CHANNELS.includes(item.category)) return item.category
56
+ const dec = item.view && item.view.decorator
57
+ if (dec) {
58
+ const n = decoratorNum(dec)
59
+ if (CHANNEL_BY_DECORATOR[n]) return CHANNEL_BY_DECORATOR[n]
60
+ }
61
+ for (const t of String(item.tags || '').split(/[,,\s]+/).filter(Boolean)) {
62
+ const hit = TAG_CHANNEL.find(([re]) => re.test(t))
63
+ if (hit) return hit[1]
64
+ }
65
+ return null
66
+ }
67
+
68
+ /** work-v1 启动契约(本地与 work-store 共用,仅 work_ref/code_url/decorator 不同)。 */
69
+ export function launchRuntime({ workRef, codeUrl, decorator = '' }) {
70
+ return {
71
+ type: 'runtime',
72
+ runtime: { contract: 'work-v1', work_ref: workRef, code_url: codeUrl, caps_url: '', decorator },
73
+ }
74
+ }
75
+
76
+ /** 两源共享的字段骨架(值完全一致的字段);源覆盖差异字段即可。 */
77
+ const WIRE_BASE = {
78
+ image_url: '',
79
+ thumb_url: '',
80
+ status: 3,
81
+ visible_to: [],
82
+ is_pinned: false,
83
+ is_locked: false,
84
+ ban_hidden: false,
85
+ author_id: 0,
86
+ author_username: 'local',
87
+ view_count: 0,
88
+ like_count: 0,
89
+ favorite_count: 0,
90
+ fork_count: 0,
91
+ referenced_count: 0,
92
+ latest_version: 1,
93
+ default_version: 1,
94
+ forked_from_ref: '',
95
+ fork_base_version: 0,
96
+ description_truncated: false,
97
+ description_next_offset: 0,
98
+ has_code: true,
99
+ item_type: 'works',
100
+ local: true,
101
+ }
102
+
103
+ export function wireItem(fields) {
104
+ return { ...WIRE_BASE, ...fields }
105
+ }
106
+
107
+ /**
108
+ * 本地 JSON 作品 → wire(原 store.buildItem)。
109
+ * input:{name, description, tags, emoji, author, session?, ingest_key?}。
110
+ */
111
+ export function localWire(input = {}, id, ts) {
112
+ const name = String(input.name || input.session?.title || '').trim() || `本地作品 ${id}`
113
+ const description = String(input.description || '')
114
+ const isSession = Boolean(input.session)
115
+ const codeUrl = `/api/local/works/${id}/code`
116
+ return wireItem({
117
+ id,
118
+ ref: `LOCAL-${id}`,
119
+ name,
120
+ tags: String(input.tags || (isSession ? '会话' : '本地')),
121
+ emoji: String(input.emoji || (isSession ? '💬' : '📦')),
122
+ author: String(input.author || '本地会话'),
123
+ author_avatar: 'emoji:📦',
124
+ description,
125
+ brief: description.replace(/\s+/g, ' ').slice(0, 80),
126
+ created_at: ts,
127
+ last_published_at: ts,
128
+ updated_at: ts,
129
+ code_url: codeUrl,
130
+ view: { kind: 'code' },
131
+ launch: launchRuntime({ workRef: `LOCAL-${id}`, codeUrl }),
132
+ local_kind: isSession ? 'session' : 'work',
133
+ delivery_adopted: false,
134
+ delivery_published: false,
135
+ is_favorited: true,
136
+ references: [],
137
+ attachments: [],
138
+ // 摄入幂等键(inbox sha256 / 显式 idempotency_key):重复投递据此去重。
139
+ ingest_key: String(input.ingest_key || ''),
140
+ })
141
+ }
142
+
143
+ /**
144
+ * work-store 作品 → wire(原 workstore.toItem)。
145
+ * title 由桥接层解析好传入(resolveTitle);id 为已分配的数字 id。
146
+ * 预装作品(work.meta.ddr0)带原站门类/装饰器/标签;PPT 生成物默认挂 PPTist 装饰器。
147
+ */
148
+ export function workstoreWire({ work, id, title, truncateDesc = false }) {
149
+ const ddr0 = (work.meta && work.meta.ddr0) || null
150
+ const decorator = ddr0 ? (ddr0.decorator || '') : DECORATOR_REF
151
+ const decNum = decoratorNum(decorator)
152
+ const category = (ddr0 && ddr0.category) || CHANNEL_BY_DECORATOR[decNum]
153
+ || (work.suite === 'ppt' ? 'ppt' : null)
154
+ || categoryOfItem({ tags: (work.tags || []).join(','), view: { decorator } }) || 'ppt'
155
+ const label = CHANNEL_LABELS[category] || 'PPT'
156
+ const tagsArr = (ddr0 && Array.isArray(ddr0.tags) && ddr0.tags.length) ? ddr0.tags
157
+ : (Array.isArray(work.tags) && work.tags.length ? work.tags : [label, '本地'])
158
+ const emoji = (ddr0 && ddr0.emoji) || CHANNEL_EMOJI[category] || '📑'
159
+ const author = (ddr0 && ddr0.author) || '本地'
160
+ const description = (ddr0 && ddr0.description) || ''
161
+ const codeUrl = `/api/works/works/${id}/version/1/js`
162
+ const updatedAt = iso(work.updatedAt)
163
+ const trunc = Boolean(truncateDesc && description.length > 140)
164
+ // 媒体作品(视频/音频,has_code:false):sync 物化后把 media/launch/view 存进 meta.ddr0,
165
+ // 这里原样透传(URL 已是本地直出路径),前端按 launch.inline 播放,不走代码运行时。
166
+ const media = ddr0 && ddr0.media && ddr0.media.url ? ddr0.media : null
167
+ const view = media
168
+ ? { kind: 'code', ...(ddr0.view || {}) }
169
+ : (decorator ? { kind: 'code', decorator, source: SOURCE_SECTION } : { kind: 'code' })
170
+ // 预装作品的真实版本(主站同步写入 meta.ddr0.latest_version);未同步过回落 1。
171
+ const latestVersion = (ddr0 && Number(ddr0.latest_version)) || 1
172
+ const defaultVersion = (ddr0 && Number(ddr0.default_version)) || latestVersion
173
+ return wireItem({
174
+ id,
175
+ ref: ddr0 ? (ddr0.ref || `WORKS-${ddr0.id}`) : `LOCAL-WS-${id}`,
176
+ name: title,
177
+ tags: tagsArr.join(','),
178
+ emoji,
179
+ author,
180
+ author_avatar: 'emoji:🗂️',
181
+ description: trunc ? description.slice(0, 140) : description,
182
+ description_truncated: trunc,
183
+ description_next_offset: trunc ? 140 : 0,
184
+ brief: work.brief || `${work.suite || 'ppt'} · 本地作品`,
185
+ created_at: iso(work.createdAt),
186
+ last_published_at: updatedAt,
187
+ updated_at: updatedAt,
188
+ latest_version: latestVersion,
189
+ default_version: defaultVersion,
190
+ code_url: media ? '' : codeUrl,
191
+ has_code: media ? false : true,
192
+ view,
193
+ launch: media
194
+ ? (ddr0.launch || { type: 'inline', media })
195
+ : launchRuntime({ workRef: `LOCAL-WS-${id}`, codeUrl, decorator: decorator || '' }),
196
+ ...(media ? { media, attachments: Array.isArray(ddr0.attachments) ? ddr0.attachments : [] } : {}),
197
+ delivery_adopted: true,
198
+ delivery_published: true,
199
+ is_favorited: false,
200
+ local_kind: ddr0 ? 'preinstalled' : 'workstore',
201
+ category,
202
+ session_id: work.sessionId || '',
203
+ work_id: work.id,
204
+ ...(ddr0 ? { ddr0_id: ddr0.id, preinstalled: true } : {}),
205
+ })
206
+ }
207
+
208
+ function iso(ms) {
209
+ if (!ms) return new Date().toISOString()
210
+ try { return new Date(ms).toISOString() } catch { return new Date().toISOString() }
211
+ }
@@ -0,0 +1,316 @@
1
+ /**
2
+ * WorkStoreRepository —— LocalWorkRepository 契约的 work-store(SQLite)实现。
3
+ *
4
+ * Phase 2:把本地作品从 JSON 文件库迁到 @capyclaw/work-store 唯一真源。
5
+ * 装配层(plugin.mjs)面向 repository.mjs 的同一份契约编程,JSON↔work-store 切换零改。
6
+ *
7
+ * 数据映射(docs/STORAGE-MIGRATION.md §3):
8
+ * - suite 固定 'ddr0-local'(区别预装 'ddr0' 与 PPT 'ppt',桥接层据此分流不重复计数)。
9
+ * - 显式 id = ddr0-local:<kind>:<N>,kind=session|work,N 为对外数字 id(900000+ 段)。
10
+ * 会话型也用数字 N 而非 sessionId:sessionId 可空/可重,作自然键会撞键(文档已注明修订)。
11
+ * - meta.ddr0 = 完整 wire item(localWire 产物):list/get 直接回它,保证 exportAll 逐字节兼容。
12
+ * - meta.ddr0_id = N:数字 id 冗余存一份,供 nextId 扫描与桥接分流。
13
+ * - doc = {title, code, session}:code 为当前码(live),session 为会话消息体。
14
+ * - 版本 k payload = {code}(第 k 版当时的当前码);current_version == meta.ddr0.latest_version。
15
+ * add→saveVersion 得 v1;update(新码)→saveVersion 升版(旧当前码自动成归档版);
16
+ * addMessages 只重写 live code 不升版(与 JSON 语义一致)。
17
+ * - highWater → work_meta 'ddr0.highWater'(id 单调不回用,与作品同事务提交不会滞后)。
18
+ * - 摄入幂等台账 → work_journal type:'ingest'(V2-6);addLocked 有 ingest_key 即记账,
19
+ * importAll/importInbox 据此去重,迁移时回填历史件。
20
+ * - remove → purgeWork(硬删 + tombstone);highWater 不回落,id 永不回用。
21
+ *
22
+ * 并发与原子性:所有组合写走 store.tx(单事务,V2-2);SQLite WAL + busy_timeout 兜底跨进程。
23
+ * 错误约定与 JSON 版一致:E_NO_CODE→400;写失败向上抛(tx 回滚),绝不静默。
24
+ */
25
+ import fs from 'node:fs'
26
+ import path from 'node:path'
27
+ import { createHash } from 'node:crypto'
28
+ import { wrapHtml, wrapSession, defaultContinueUrl } from './render.mjs'
29
+ import { localWire } from './wire.mjs'
30
+ import { inboxCandidates, parseInboxFile, archiveInbox } from './inbox.mjs'
31
+
32
+ const SUITE = 'ddr0-local'
33
+ const SCHEMA = 2
34
+ const ID_BASE = 900000
35
+ const nowIso = () => new Date().toISOString()
36
+ const sha256 = (text) => createHash('sha256').update(text, 'utf8').digest('hex')
37
+ const tagsArr = (item) => String((item && item.tags) || '').split(/[,,\s]+/).filter(Boolean)
38
+ const workIdOf = (kind, num) => `${SUITE}:${kind}:${num}`
39
+
40
+ /**
41
+ * @param {{ store: object, dir: string, log?: object }} opts
42
+ * store = workStore 服务(assertServiceApi 校验后的 work-store API);
43
+ * dir = profile 级 plugin-data/offline-ddr0(inbox/done/endpoint.json 仍落这里)。
44
+ */
45
+ export function createWorkStoreRepository({ store, dir, log } = {}) {
46
+ if (!store || typeof store.tx !== 'function') throw new Error('createWorkStoreRepository 需要 work-store 服务(含 tx)')
47
+ const inboxDir = path.join(dir, 'inbox')
48
+ const doneDir = path.join(inboxDir, 'done')
49
+ for (const d of [inboxDir, doneDir]) {
50
+ try { fs.mkdirSync(d, { recursive: true }) } catch (error) {
51
+ log?.warn?.(`offline-ddr0: 目录创建失败 ${d}: ${error.message}`)
52
+ }
53
+ }
54
+
55
+ /* ---- 目录读取 ---- */
56
+
57
+ // 分页取全量(listWorks 单页上限 500);上限 200 页防失控。
58
+ function allWorks() {
59
+ const out = []
60
+ const PAGE = 500
61
+ for (let page = 0; page < 200; page += 1) {
62
+ const res = store.listWorks({ suite: SUITE, limit: PAGE, offset: page * PAGE, sort: 'created' })
63
+ const batch = (res && res.items) || []
64
+ out.push(...batch)
65
+ const total = Number(res && res.total)
66
+ if (batch.length < PAGE || (Number.isFinite(total) && out.length >= total)) break
67
+ }
68
+ return out
69
+ }
70
+
71
+ // 数字 id → works 行:kind 未知,work/session 两个显式 id 各点查一次(N 全局唯一,仅一存在)。
72
+ function resolve(id) {
73
+ const n = Number(id)
74
+ if (!Number.isFinite(n)) return null
75
+ const w = store.getWork(workIdOf('work', n)) || store.getWork(workIdOf('session', n))
76
+ if (!w || w.deletedAt) return null
77
+ return w
78
+ }
79
+
80
+ function nextId() {
81
+ const hw = Number(store.getMeta('ddr0.highWater', ID_BASE)) || ID_BASE
82
+ let maxId = ID_BASE
83
+ for (const w of allWorks()) maxId = Math.max(maxId, Number(w.meta && w.meta.ddr0_id) || 0)
84
+ const id = Math.max(hw, maxId) + 1
85
+ // 先落 highWater 再写作品(同事务):崩溃最多留 id 空洞,绝不撞号/回用。
86
+ store.setMeta('ddr0.highWater', id)
87
+ return id
88
+ }
89
+
90
+ /* ---- 写操作(tx 内重读→修改→原子提交) ---- */
91
+
92
+ // tx 内实现,供 add / importAll / importInbox 复用(避免嵌套 tx)。
93
+ function addLocked(input = {}) {
94
+ let code = typeof input.code === 'string' ? input.code : ''
95
+ const html = typeof input.html === 'string' ? input.html : ''
96
+ const session = input.session && typeof input.session === 'object' ? input.session : null
97
+ if (session) {
98
+ session.continueUrl = defaultContinueUrl(session)
99
+ code = wrapSession(session)
100
+ } else if (!code && html) {
101
+ code = wrapHtml(html)
102
+ }
103
+ if (!code.trim()) {
104
+ const error = new Error('需要 code / html / session 之一')
105
+ error.code = 'E_NO_CODE'
106
+ throw error
107
+ }
108
+ const num = nextId()
109
+ const kind = session ? 'session' : 'work'
110
+ const id = workIdOf(kind, num)
111
+ const item = localWire(input, num, nowIso())
112
+ store.ensureWork({
113
+ id, suite: SUITE, kind, sessionId: String(num),
114
+ title: item.name, brief: item.brief, tags: tagsArr(item), status: 3,
115
+ meta: { ddr0: item, ddr0_id: num },
116
+ })
117
+ store.writeState(id, { title: item.name, code, session: session || undefined })
118
+ const v = store.saveVersion(id, { code })
119
+ item.latest_version = v.version
120
+ item.default_version = v.version
121
+ store.updateWork(id, { meta: { ddr0: item, ddr0_id: num } })
122
+ if (item.ingest_key) store.appendJournal(id, { type: 'ingest', ingest_key: item.ingest_key })
123
+ log?.info?.(`offline-ddr0: 本地作品入库 ${item.ref}「${item.name}」`)
124
+ return item
125
+ }
126
+
127
+ function add(input = {}) {
128
+ return store.tx(() => addLocked(input))
129
+ }
130
+
131
+ function update(id, patch = {}) {
132
+ return store.tx(() => {
133
+ const work = resolve(id)
134
+ if (!work) return null
135
+ const item = { ...work.meta.ddr0 }
136
+ for (const key of ['name', 'description', 'tags', 'emoji', 'author']) {
137
+ if (typeof patch[key] === 'string') item[key] = patch[key]
138
+ }
139
+ item.brief = String(item.description || '').replace(/\s+/g, ' ').slice(0, 80)
140
+ const newCode = typeof patch.code === 'string' ? patch.code : (typeof patch.html === 'string' ? wrapHtml(patch.html) : '')
141
+ if (newCode.trim()) {
142
+ const doc = store.readState(work.id) || {}
143
+ store.writeState(work.id, { ...doc, title: item.name, code: newCode })
144
+ // 旧当前码已是上一版快照;saveVersion 升版并把指针挪过去(单事务,无跨文件崩溃窗口)。
145
+ const v = store.saveVersion(work.id, { code: newCode })
146
+ item.latest_version = v.version
147
+ item.default_version = v.version
148
+ }
149
+ item.updated_at = nowIso()
150
+ store.updateWork(work.id, { title: item.name, brief: item.brief, tags: tagsArr(item), meta: { ...work.meta, ddr0: item } })
151
+ return item
152
+ })
153
+ }
154
+
155
+ function addMessages(id, messages) {
156
+ return store.tx(() => {
157
+ const work = resolve(id)
158
+ if (!work) return null
159
+ const item = { ...work.meta.ddr0 }
160
+ const doc = store.readState(work.id) || {}
161
+ const session = doc.session || { messages: [] }
162
+ session.messages = [...(session.messages || []), ...(Array.isArray(messages) ? messages : [])]
163
+ session.continueUrl = defaultContinueUrl(session)
164
+ const code = wrapSession(session)
165
+ store.writeState(work.id, { ...doc, title: item.name, code, session })
166
+ item.updated_at = nowIso()
167
+ store.updateWork(work.id, { meta: { ...work.meta, ddr0: item } })
168
+ return item
169
+ })
170
+ }
171
+
172
+ function remove(id) {
173
+ return store.tx(() => {
174
+ const work = resolve(id)
175
+ if (!work) return false
176
+ // 硬删 + tombstone;highWater 不回落,id 不回用。无 code/ 目录,故无「复活」问题。
177
+ store.purgeWork(work.id)
178
+ return true
179
+ })
180
+ }
181
+
182
+ /* ---- 读操作 ---- */
183
+
184
+ function get(id) {
185
+ const work = resolve(id)
186
+ return (work && work.meta && work.meta.ddr0) || null
187
+ }
188
+
189
+ function list() {
190
+ return allWorks()
191
+ .map((w) => w.meta && w.meta.ddr0)
192
+ .filter(Boolean)
193
+ .sort((a, b) => Number(b.id) - Number(a.id))
194
+ }
195
+
196
+ function search(q) {
197
+ const needle = String(q || '').trim().toLowerCase()
198
+ if (!needle) return list()
199
+ return list().filter((x) =>
200
+ [x.name, x.description, x.tags, x.ref].join(' ').toLowerCase().includes(needle))
201
+ }
202
+
203
+ function code(id, version) {
204
+ const work = resolve(id)
205
+ if (!work) return null
206
+ const ver = Number(version) || 0
207
+ if (ver && ver < work.currentVersion) {
208
+ const v = store.getVersion(work.id, ver)
209
+ const c = v && v.payload && v.payload.code
210
+ return typeof c === 'string' ? Buffer.from(c, 'utf8') : null
211
+ }
212
+ const doc = store.readState(work.id)
213
+ const c = doc && doc.code
214
+ return typeof c === 'string' ? Buffer.from(c, 'utf8') : null
215
+ }
216
+
217
+ function versions(id) {
218
+ const work = resolve(id)
219
+ if (!work) return []
220
+ return store.listVersions(work.id).map((v) => ({ version: v.version, current: v.version === work.currentVersion }))
221
+ }
222
+
223
+ function readSession(id) {
224
+ const work = resolve(id)
225
+ if (!work) return null
226
+ const doc = store.readState(work.id)
227
+ return (doc && doc.session) || null
228
+ }
229
+
230
+ /* ---- 批量 / 摄入 ---- */
231
+
232
+ function exportAll() {
233
+ return {
234
+ schema: SCHEMA,
235
+ exported_at: nowIso(),
236
+ works: list().map((it) => ({
237
+ ...it,
238
+ code: (code(it.id) || Buffer.from('')).toString('utf8'),
239
+ session: readSession(it.id),
240
+ })),
241
+ }
242
+ }
243
+
244
+ // 批量导入:整批一个 tx,ingest_key 去重(与 inbox 同一幂等语义),单条坏件跳过。
245
+ function importAll(data) {
246
+ const works = Array.isArray(data && data.works) ? data.works : []
247
+ if (!works.length) return 0
248
+ return store.tx(() => {
249
+ const keys = new Set(allWorks().map((w) => String((w.meta && w.meta.ddr0 && w.meta.ddr0.ingest_key) || '')).filter(Boolean))
250
+ let n = 0
251
+ for (const w of works) {
252
+ try {
253
+ const codeText = typeof w.code === 'string' ? w.code : ''
254
+ const key = String(w.idempotency_key || w.ingest_key
255
+ || sha256(codeText || JSON.stringify(w.session || {})))
256
+ if (keys.has(key)) continue
257
+ addLocked({
258
+ name: w.name, description: w.description, tags: w.tags, emoji: w.emoji, author: w.author,
259
+ code: codeText, session: w.session || undefined, ingest_key: key,
260
+ })
261
+ keys.add(key)
262
+ n += 1
263
+ } catch { /* 单条坏件跳过 */ }
264
+ }
265
+ return n
266
+ })
267
+ }
268
+
269
+ // inbox 摄入协议(V2-6):协议在 lib/inbox.mjs;「查重→入库→归档」原子提交在一个 tx 内。
270
+ function importInbox() {
271
+ let imported = 0
272
+ const keys = new Set(allWorks().map((w) => String((w.meta && w.meta.ddr0 && w.meta.ddr0.ingest_key) || '')).filter(Boolean))
273
+ for (const { name, full } of inboxCandidates(inboxDir)) {
274
+ try {
275
+ const { input, key } = parseInboxFile(full, name)
276
+ store.tx(() => {
277
+ const needle = `"ingest_key":${JSON.stringify(key)}`
278
+ const dup = keys.has(key) || store.findJournal('ingest', needle, 1).length > 0
279
+ if (!dup) {
280
+ addLocked({ ...input, ingest_key: key })
281
+ keys.add(key)
282
+ imported += 1
283
+ }
284
+ archiveInbox(doneDir, full, name)
285
+ })
286
+ } catch (error) {
287
+ log?.warn?.(`offline-ddr0: inbox 入库失败 ${name}: ${error.message}`)
288
+ try { archiveInbox(doneDir, full, name, { bad: true }) } catch { /* 留着下次再试 */ }
289
+ }
290
+ }
291
+ return imported
292
+ }
293
+
294
+ return {
295
+ dir,
296
+ inboxDir,
297
+ add,
298
+ update,
299
+ addMessages,
300
+ get,
301
+ remove,
302
+ code,
303
+ versions,
304
+ list,
305
+ search,
306
+ exportAll,
307
+ importAll,
308
+ importInbox,
309
+ readSession,
310
+ get count() {
311
+ return store.listWorks({ suite: SUITE, limit: 1 }).total
312
+ },
313
+ }
314
+ }
315
+
316
+ export { SUITE as LOCAL_SUITE }