dsh-chat-comprehension 0.2.0 → 0.3.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.
Files changed (364) hide show
  1. package/NOTICE.md +20 -0
  2. package/README.md +20 -9
  3. package/client.js +30 -9
  4. package/lib/engines.mjs +136 -0
  5. package/lib/index.js +37 -4
  6. package/lib/qqbot-0.5.3-patches.json +75 -70
  7. package/lib/qqbot-patch.mjs +12 -7
  8. package/lib/video.mjs +11 -2
  9. package/package.json +7 -23
  10. package/vendor/dsh-agent-md/LICENSE +21 -0
  11. package/vendor/dsh-agent-md/cordis.patch.yml +8 -0
  12. package/vendor/dsh-agent-md/lib/index.js +775 -0
  13. package/vendor/dsh-agent-md/lib/persona.js +634 -0
  14. package/vendor/dsh-agent-md/lib/qq.js +527 -0
  15. package/vendor/dsh-agent-md/package.json +50 -0
  16. package/vendor/dsh-bili-scroll/LICENSE +21 -0
  17. package/vendor/dsh-bili-scroll/NOTICE.md +18 -0
  18. package/vendor/dsh-bili-scroll/cordis.patch.yml +20 -0
  19. package/vendor/dsh-bili-scroll/index.js +3278 -0
  20. package/vendor/dsh-bili-scroll/lib/asr.py +141 -0
  21. package/vendor/dsh-bili-scroll/lib/diagnose.mjs +95 -0
  22. package/vendor/dsh-bili-scroll/lib/dm.mjs +110 -0
  23. package/vendor/dsh-bili-scroll/lib/favops.py +90 -0
  24. package/vendor/dsh-bili-scroll/lib/hostdeps.mjs +148 -0
  25. package/vendor/dsh-bili-scroll/lib/login.mjs +141 -0
  26. package/vendor/dsh-bili-scroll/lib/mcp.mjs +144 -0
  27. package/vendor/dsh-bili-scroll/lib/state.mjs +236 -0
  28. package/vendor/dsh-bili-scroll/lib/uid.mjs +61 -0
  29. package/vendor/dsh-bili-scroll/lib/uvfetch.mjs +217 -0
  30. package/vendor/dsh-bili-scroll/lib/verify.mjs +184 -0
  31. package/vendor/dsh-bili-scroll/package.json +84 -0
  32. package/vendor/dsh-bili-scroll/profile-template/bili-duty/cordis.patch.yml +116 -0
  33. package/vendor/dsh-bili-scroll/profile-template/bili-duty/cordis.yml +4 -0
  34. package/vendor/dsh-bili-scroll/profile-template/bili-duty/package.json +13 -0
  35. package/vendor/dsh-bili-scroll/prompts/duty.md +296 -0
  36. package/vendor/dsh-bili-scroll/vendor/bilibili-mcp/LICENSE +21 -0
  37. package/vendor/dsh-bili-scroll/vendor/bilibili-mcp/README.md +176 -0
  38. package/vendor/dsh-bili-scroll/vendor/bilibili-mcp/mcp_server.py +2015 -0
  39. package/vendor/dsh-bili-scroll/vendor/bilibili-mcp/requirements.txt +10 -0
  40. package/vendor/dsh-meme/.dsh/skills/dsh-meme-maintenance/SKILL.md +50 -0
  41. package/vendor/dsh-meme/LICENSE +21 -0
  42. package/vendor/dsh-meme/anim.js +588 -0
  43. package/vendor/dsh-meme/cordis.patch.yml +13 -0
  44. package/vendor/dsh-meme/icon.jpg +0 -0
  45. package/vendor/dsh-meme/index.js +2616 -0
  46. package/vendor/dsh-meme/locale/en.json +6 -0
  47. package/vendor/dsh-meme/locale/zh.json +6 -0
  48. package/vendor/dsh-meme/memes/dafeiyu-001/index.db +0 -0
  49. package/vendor/dsh-meme/memes/dafeiyu-001/manifest.json +13 -0
  50. package/vendor/dsh-meme/memes/dafeiyu-001/memes/angry/shengqi.jpg +0 -0
  51. package/vendor/dsh-meme/memes/dafeiyu-001/memes/baka/xianqi.jpg +0 -0
  52. package/vendor/dsh-meme/memes/dafeiyu-001/memes/color/huachi.jpg +0 -0
  53. package/vendor/dsh-meme/memes/dafeiyu-001/memes/confused/jiazai.jpg +0 -0
  54. package/vendor/dsh-meme/memes/dafeiyu-001/memes/confused/wenhao.jpg +0 -0
  55. package/vendor/dsh-meme/memes/dafeiyu-001/memes/cpu/jiaban.jpg +0 -0
  56. package/vendor/dsh-meme/memes/dafeiyu-001/memes/daily/ganfan.jpg +0 -0
  57. package/vendor/dsh-meme/memes/dafeiyu-001/memes/fool/buyao.jpg +0 -0
  58. package/vendor/dsh-meme/memes/dafeiyu-001/memes/givemoney/yaoqian.jpg +0 -0
  59. package/vendor/dsh-meme/memes/dafeiyu-001/memes/happy/deyi.jpg +0 -0
  60. package/vendor/dsh-meme/memes/dafeiyu-001/memes/happy/ok.jpg +0 -0
  61. package/vendor/dsh-meme/memes/dafeiyu-001/memes/happy/piaole.jpg +0 -0
  62. package/vendor/dsh-meme/memes/dafeiyu-001/memes/like/bixin.jpg +0 -0
  63. package/vendor/dsh-meme/memes/dafeiyu-001/memes/meow/maozhua.jpg +0 -0
  64. package/vendor/dsh-meme/memes/dafeiyu-001/memes/morning/zaoshanghao.jpg +0 -0
  65. package/vendor/dsh-meme/memes/dafeiyu-001/memes/sad/qiurao.jpg +0 -0
  66. package/vendor/dsh-meme/memes/dafeiyu-001/memes/sad/yun.jpg +0 -0
  67. package/vendor/dsh-meme/memes/dafeiyu-001/memes/see/zhenjing.jpg +0 -0
  68. package/vendor/dsh-meme/memes/dafeiyu-001/memes/shy/haixiu.jpg +0 -0
  69. package/vendor/dsh-meme/memes/dafeiyu-001/memes/shy/toukan.jpg +0 -0
  70. package/vendor/dsh-meme/memes/dafeiyu-001/memes/sigh/wuyu.jpg +0 -0
  71. package/vendor/dsh-meme/memes/dafeiyu-001/memes/sleep/shuijiao.jpg +0 -0
  72. package/vendor/dsh-meme/memes/dafeiyu-001/memes/surprised/xia.jpg +0 -0
  73. package/vendor/dsh-meme/memes/dafeiyu-001/memes/work/tangping.jpg +0 -0
  74. package/vendor/dsh-meme/memes.js +998 -0
  75. package/vendor/dsh-meme/package.json +67 -0
  76. package/vendor/dsh-meme/panel.html +210 -0
  77. package/vendor/dsh-video-storyboard/LICENSE +21 -0
  78. package/vendor/dsh-video-storyboard/NOTICE +12 -0
  79. package/vendor/dsh-video-storyboard/bin/_args.mjs +16 -0
  80. package/vendor/dsh-video-storyboard/bin/asr-check.mjs +85 -0
  81. package/vendor/dsh-video-storyboard/bin/scan-stats.mjs +43 -0
  82. package/vendor/dsh-video-storyboard/bin/scan-video.mjs +38 -0
  83. package/vendor/dsh-video-storyboard/bin/storyboard.mjs +195 -0
  84. package/vendor/dsh-video-storyboard/cordis.patch.yml +3 -0
  85. package/vendor/dsh-video-storyboard/dsh.plugin.json +12 -0
  86. package/vendor/dsh-video-storyboard/package.json +88 -0
  87. package/vendor/dsh-video-storyboard/scripts/check-syntax.mjs +56 -0
  88. package/vendor/dsh-video-storyboard/scripts/install-dev.mjs +104 -0
  89. package/vendor/dsh-video-storyboard/src/flicker.js +104 -0
  90. package/vendor/dsh-video-storyboard/src/index.js +82 -0
  91. package/vendor/dsh-video-storyboard/src/ocr_bridge.py +103 -0
  92. package/vendor/dsh-video-storyboard/src/storyboard/asr-lifecycle.js +262 -0
  93. package/vendor/dsh-video-storyboard/src/storyboard/asr.js +488 -0
  94. package/vendor/dsh-video-storyboard/src/storyboard/audio.js +168 -0
  95. package/vendor/dsh-video-storyboard/src/storyboard/binaries.js +69 -0
  96. package/vendor/dsh-video-storyboard/src/storyboard/board.js +121 -0
  97. package/vendor/dsh-video-storyboard/src/storyboard/browser-time.js +504 -0
  98. package/vendor/dsh-video-storyboard/src/storyboard/cache.js +143 -0
  99. package/vendor/dsh-video-storyboard/src/storyboard/cells.js +70 -0
  100. package/vendor/dsh-video-storyboard/src/storyboard/density.js +545 -0
  101. package/vendor/dsh-video-storyboard/src/storyboard/direct-url.js +130 -0
  102. package/vendor/dsh-video-storyboard/src/storyboard/fetch.js +373 -0
  103. package/vendor/dsh-video-storyboard/src/storyboard/flash.js +180 -0
  104. package/vendor/dsh-video-storyboard/src/storyboard/format.js +573 -0
  105. package/vendor/dsh-video-storyboard/src/storyboard/frames.js +212 -0
  106. package/vendor/dsh-video-storyboard/src/storyboard/index.js +32 -0
  107. package/vendor/dsh-video-storyboard/src/storyboard/injection.js +76 -0
  108. package/vendor/dsh-video-storyboard/src/storyboard/layout-order.js +176 -0
  109. package/vendor/dsh-video-storyboard/src/storyboard/locate.js +113 -0
  110. package/vendor/dsh-video-storyboard/src/storyboard/media-store.js +106 -0
  111. package/vendor/dsh-video-storyboard/src/storyboard/memory.js +94 -0
  112. package/vendor/dsh-video-storyboard/src/storyboard/ocr-subs.js +107 -0
  113. package/vendor/dsh-video-storyboard/src/storyboard/ocr.js +167 -0
  114. package/vendor/dsh-video-storyboard/src/storyboard/pipeline.js +853 -0
  115. package/vendor/dsh-video-storyboard/src/storyboard/plan.js +400 -0
  116. package/vendor/dsh-video-storyboard/src/storyboard/probe.js +132 -0
  117. package/vendor/dsh-video-storyboard/src/storyboard/provenance.js +44 -0
  118. package/vendor/dsh-video-storyboard/src/storyboard/python.js +84 -0
  119. package/vendor/dsh-video-storyboard/src/storyboard/register.js +32 -0
  120. package/vendor/dsh-video-storyboard/src/storyboard/scan.js +486 -0
  121. package/vendor/dsh-video-storyboard/src/storyboard/segments.js +117 -0
  122. package/vendor/dsh-video-storyboard/src/storyboard/sheet.js +507 -0
  123. package/vendor/dsh-video-storyboard/src/storyboard/storage.js +93 -0
  124. package/vendor/dsh-video-storyboard/src/storyboard/subtitles.js +181 -0
  125. package/vendor/dsh-video-storyboard/src/storyboard/tool-spec.js +544 -0
  126. package/vendor/dsh-video-storyboard/src/storyboard/transcript-file.js +146 -0
  127. package/vendor/dsh-video-storyboard/src/storyboard/url.js +69 -0
  128. package/vendor/dsh-video-storyboard/src/storyboard/ytdlp.js +289 -0
  129. package/vendor/dsh-video-storyboard/src/tool-spec.js +17 -0
  130. package/vendor/dsh-video-storyboard/src/tools/locate.js +39 -0
  131. package/vendor/dsh-video-storyboard/src/tools/probe.js +23 -0
  132. package/vendor/dsh-video-storyboard/src/tools/shared.js +39 -0
  133. package/vendor/dsh-video-storyboard/src/tools/storyboard.js +20 -0
  134. package/vendor/dsh-video-storyboard/src/tools/zoom.js +56 -0
  135. package/vendor/dsh-world/LICENSE +21 -0
  136. package/vendor/dsh-world/assets/qq-card.png +0 -0
  137. package/vendor/dsh-world/cordis.patch.yml +6 -0
  138. package/vendor/dsh-world/lib/archive.d.ts +6 -0
  139. package/vendor/dsh-world/lib/archive.js +49 -0
  140. package/vendor/dsh-world/lib/archiveSelection.d.ts +8 -0
  141. package/vendor/dsh-world/lib/archiveSelection.js +20 -0
  142. package/vendor/dsh-world/lib/compaction.d.ts +124 -0
  143. package/vendor/dsh-world/lib/compaction.js +273 -0
  144. package/vendor/dsh-world/lib/compactionRegion.d.ts +42 -0
  145. package/vendor/dsh-world/lib/compactionRegion.js +210 -0
  146. package/vendor/dsh-world/lib/console/routes.d.ts +57 -0
  147. package/vendor/dsh-world/lib/console/routes.js +129 -0
  148. package/vendor/dsh-world/lib/console/snapshot.d.ts +39 -0
  149. package/vendor/dsh-world/lib/console/snapshot.js +51 -0
  150. package/vendor/dsh-world/lib/ecosystem/bridge/bridge.d.ts +36 -0
  151. package/vendor/dsh-world/lib/ecosystem/bridge/bridge.js +140 -0
  152. package/vendor/dsh-world/lib/ecosystem/bridge/route.d.ts +9 -0
  153. package/vendor/dsh-world/lib/ecosystem/bridge/route.js +46 -0
  154. package/vendor/dsh-world/lib/ecosystem/qq/bridge.d.ts +123 -0
  155. package/vendor/dsh-world/lib/ecosystem/qq/bridge.js +380 -0
  156. package/vendor/dsh-world/lib/ecosystem/qq/panel.d.ts +75 -0
  157. package/vendor/dsh-world/lib/ecosystem/qq/panel.js +187 -0
  158. package/vendor/dsh-world/lib/ecosystem/qq/route.d.ts +32 -0
  159. package/vendor/dsh-world/lib/ecosystem/qq/route.js +134 -0
  160. package/vendor/dsh-world/lib/ecosystem/qq/supervisor.d.ts +57 -0
  161. package/vendor/dsh-world/lib/ecosystem/qq/supervisor.js +188 -0
  162. package/vendor/dsh-world/lib/ecosystem/shared/context.d.ts +14 -0
  163. package/vendor/dsh-world/lib/ecosystem/shared/context.js +42 -0
  164. package/vendor/dsh-world/lib/ecosystem/shared/identity.d.ts +15 -0
  165. package/vendor/dsh-world/lib/ecosystem/shared/identity.js +36 -0
  166. package/vendor/dsh-world/lib/ecosystem/shared/outlets.d.ts +43 -0
  167. package/vendor/dsh-world/lib/ecosystem/shared/outlets.js +70 -0
  168. package/vendor/dsh-world/lib/ecosystem/shared/sessions.d.ts +16 -0
  169. package/vendor/dsh-world/lib/ecosystem/shared/sessions.js +52 -0
  170. package/vendor/dsh-world/lib/ecosystem/wechat/archive.d.ts +24 -0
  171. package/vendor/dsh-world/lib/ecosystem/wechat/archive.js +62 -0
  172. package/vendor/dsh-world/lib/ecosystem/wechat/home.d.ts +21 -0
  173. package/vendor/dsh-world/lib/ecosystem/wechat/home.js +111 -0
  174. package/vendor/dsh-world/lib/ecosystem/wechat/images.d.ts +5 -0
  175. package/vendor/dsh-world/lib/ecosystem/wechat/images.js +43 -0
  176. package/vendor/dsh-world/lib/ecosystem/wechat/index.d.ts +41 -0
  177. package/vendor/dsh-world/lib/ecosystem/wechat/index.js +166 -0
  178. package/vendor/dsh-world/lib/ecosystem/wechat/messages.d.ts +24 -0
  179. package/vendor/dsh-world/lib/ecosystem/wechat/messages.js +35 -0
  180. package/vendor/dsh-world/lib/ecosystem/wechat/sending.d.ts +30 -0
  181. package/vendor/dsh-world/lib/ecosystem/wechat/sending.js +216 -0
  182. package/vendor/dsh-world/lib/ecosystem/wechat/supervisor.d.ts +39 -0
  183. package/vendor/dsh-world/lib/ecosystem/wechat/supervisor.js +138 -0
  184. package/vendor/dsh-world/lib/faults.d.ts +10 -0
  185. package/vendor/dsh-world/lib/faults.js +13 -0
  186. package/vendor/dsh-world/lib/history.d.ts +16 -0
  187. package/vendor/dsh-world/lib/history.js +80 -0
  188. package/vendor/dsh-world/lib/hostContract.d.ts +19 -0
  189. package/vendor/dsh-world/lib/hostContract.js +1 -0
  190. package/vendor/dsh-world/lib/http.d.ts +18 -0
  191. package/vendor/dsh-world/lib/http.js +39 -0
  192. package/vendor/dsh-world/lib/index.d.ts +108 -0
  193. package/vendor/dsh-world/lib/index.js +356 -0
  194. package/vendor/dsh-world/lib/life/activity.d.ts +14 -0
  195. package/vendor/dsh-world/lib/life/activity.js +58 -0
  196. package/vendor/dsh-world/lib/life/alarm.d.ts +39 -0
  197. package/vendor/dsh-world/lib/life/alarm.js +132 -0
  198. package/vendor/dsh-world/lib/life/alarmTimer.d.ts +22 -0
  199. package/vendor/dsh-world/lib/life/alarmTimer.js +49 -0
  200. package/vendor/dsh-world/lib/life/diary.d.ts +4 -0
  201. package/vendor/dsh-world/lib/life/diary.js +9 -0
  202. package/vendor/dsh-world/lib/life/earlyStop.d.ts +19 -0
  203. package/vendor/dsh-world/lib/life/earlyStop.js +25 -0
  204. package/vendor/dsh-world/lib/life/home.d.ts +34 -0
  205. package/vendor/dsh-world/lib/life/home.js +91 -0
  206. package/vendor/dsh-world/lib/life/host.d.ts +22 -0
  207. package/vendor/dsh-world/lib/life/host.js +24 -0
  208. package/vendor/dsh-world/lib/life/journal.d.ts +20 -0
  209. package/vendor/dsh-world/lib/life/journal.js +59 -0
  210. package/vendor/dsh-world/lib/life/lifeStore.d.ts +16 -0
  211. package/vendor/dsh-world/lib/life/lifeStore.js +105 -0
  212. package/vendor/dsh-world/lib/life/messages.d.ts +17 -0
  213. package/vendor/dsh-world/lib/life/messages.js +53 -0
  214. package/vendor/dsh-world/lib/life/notify.d.ts +15 -0
  215. package/vendor/dsh-world/lib/life/notify.js +40 -0
  216. package/vendor/dsh-world/lib/life/pricing.d.ts +4 -0
  217. package/vendor/dsh-world/lib/life/pricing.js +30 -0
  218. package/vendor/dsh-world/lib/life/runtime.d.ts +52 -0
  219. package/vendor/dsh-world/lib/life/runtime.js +376 -0
  220. package/vendor/dsh-world/lib/life/toolRegistry.d.ts +22 -0
  221. package/vendor/dsh-world/lib/life/toolRegistry.js +64 -0
  222. package/vendor/dsh-world/lib/life/tools.d.ts +24 -0
  223. package/vendor/dsh-world/lib/life/tools.js +47 -0
  224. package/vendor/dsh-world/lib/life/types.d.ts +67 -0
  225. package/vendor/dsh-world/lib/life/types.js +1 -0
  226. package/vendor/dsh-world/lib/persona.d.ts +10 -0
  227. package/vendor/dsh-world/lib/persona.js +29 -0
  228. package/vendor/dsh-world/lib/promptSection.d.ts +30 -0
  229. package/vendor/dsh-world/lib/promptSection.js +81 -0
  230. package/vendor/dsh-world/lib/qqScope.d.ts +11 -0
  231. package/vendor/dsh-world/lib/qqScope.js +39 -0
  232. package/vendor/dsh-world/lib/runtime.d.ts +23 -0
  233. package/vendor/dsh-world/lib/runtime.js +206 -0
  234. package/vendor/dsh-world/lib/self.d.ts +10 -0
  235. package/vendor/dsh-world/lib/self.js +36 -0
  236. package/vendor/dsh-world/lib/sourceArchive.d.ts +28 -0
  237. package/vendor/dsh-world/lib/sourceArchive.js +156 -0
  238. package/vendor/dsh-world/lib/store.d.ts +30 -0
  239. package/vendor/dsh-world/lib/store.js +191 -0
  240. package/vendor/dsh-world/lib/timeContext.d.ts +3 -0
  241. package/vendor/dsh-world/lib/timeContext.js +13 -0
  242. package/vendor/dsh-world/lib/types.d.ts +118 -0
  243. package/vendor/dsh-world/lib/types.js +1 -0
  244. package/vendor/dsh-world/lib/worldPrompt.d.ts +47 -0
  245. package/vendor/dsh-world/lib/worldPrompt.js +49 -0
  246. package/vendor/dsh-world/lib/worldSwitch.d.ts +44 -0
  247. package/vendor/dsh-world/lib/worldSwitch.js +78 -0
  248. package/vendor/dsh-world/package-lock.json +1088 -0
  249. package/vendor/dsh-world/package.json +88 -0
  250. package/vendor/dsh-world/scripts/enable-tool-free-compaction.ps1 +33 -0
  251. package/vendor/dsh-world/src/archive.ts +45 -0
  252. package/vendor/dsh-world/src/archiveSelection.ts +27 -0
  253. package/vendor/dsh-world/src/compaction.ts +260 -0
  254. package/vendor/dsh-world/src/compactionRegion.ts +172 -0
  255. package/vendor/dsh-world/src/console/routes.ts +125 -0
  256. package/vendor/dsh-world/src/console/snapshot.ts +84 -0
  257. package/vendor/dsh-world/src/ecosystem/README.md +14 -0
  258. package/vendor/dsh-world/src/ecosystem/bridge/bridge.ts +121 -0
  259. package/vendor/dsh-world/src/ecosystem/bridge/route.ts +35 -0
  260. package/vendor/dsh-world/src/ecosystem/qq/bridge.ts +456 -0
  261. package/vendor/dsh-world/src/ecosystem/qq/panel.ts +178 -0
  262. package/vendor/dsh-world/src/ecosystem/qq/route.ts +130 -0
  263. package/vendor/dsh-world/src/ecosystem/qq/supervisor.ts +262 -0
  264. package/vendor/dsh-world/src/ecosystem/shared/context.ts +38 -0
  265. package/vendor/dsh-world/src/ecosystem/shared/identity.ts +42 -0
  266. package/vendor/dsh-world/src/ecosystem/shared/outlets.ts +75 -0
  267. package/vendor/dsh-world/src/ecosystem/shared/sessions.ts +47 -0
  268. package/vendor/dsh-world/src/ecosystem/wechat/archive.ts +66 -0
  269. package/vendor/dsh-world/src/ecosystem/wechat/home.ts +94 -0
  270. package/vendor/dsh-world/src/ecosystem/wechat/images.ts +38 -0
  271. package/vendor/dsh-world/src/ecosystem/wechat/index.ts +101 -0
  272. package/vendor/dsh-world/src/ecosystem/wechat/messages.ts +43 -0
  273. package/vendor/dsh-world/src/ecosystem/wechat/sending.ts +177 -0
  274. package/vendor/dsh-world/src/ecosystem/wechat/supervisor.ts +116 -0
  275. package/vendor/dsh-world/src/ecosystem/wechat/windows/README.md +117 -0
  276. package/vendor/dsh-world/src/ecosystem/wechat/windows/adapter.py +572 -0
  277. package/vendor/dsh-world/src/ecosystem/wechat/windows/binding.py +52 -0
  278. package/vendor/dsh-world/src/ecosystem/wechat/windows/engine/LICENSE +43 -0
  279. package/vendor/dsh-world/src/ecosystem/wechat/windows/engine/NOTICE.md +10 -0
  280. package/vendor/dsh-world/src/ecosystem/wechat/windows/engine/bridge.cpp +229 -0
  281. package/vendor/dsh-world/src/ecosystem/wechat/windows/engine/build.ps1 +19 -0
  282. package/vendor/dsh-world/src/ecosystem/wechat/windows/engine/profile.json +28 -0
  283. package/vendor/dsh-world/src/ecosystem/wechat/windows/groups.py +42 -0
  284. package/vendor/dsh-world/src/ecosystem/wechat/windows/incoming.py +157 -0
  285. package/vendor/dsh-world/src/ecosystem/wechat/windows/native.py +168 -0
  286. package/vendor/dsh-world/src/ecosystem/wechat/windows/outgoing.py +95 -0
  287. package/vendor/dsh-world/src/ecosystem/wechat/windows/requirements.txt +1 -0
  288. package/vendor/dsh-world/src/ecosystem/wechat/windows/sender.py +157 -0
  289. package/vendor/dsh-world/src/ecosystem/wechat/windows/snapshot.py +32 -0
  290. package/vendor/dsh-world/src/ecosystem/wechat/windows/stickers.py +60 -0
  291. package/vendor/dsh-world/src/ecosystem/wechat/windows/wechat.ps1 +75 -0
  292. package/vendor/dsh-world/src/faults.ts +12 -0
  293. package/vendor/dsh-world/src/history.ts +77 -0
  294. package/vendor/dsh-world/src/hostContract.ts +9 -0
  295. package/vendor/dsh-world/src/http.ts +34 -0
  296. package/vendor/dsh-world/src/index.ts +286 -0
  297. package/vendor/dsh-world/src/life/activity.ts +48 -0
  298. package/vendor/dsh-world/src/life/alarm.ts +119 -0
  299. package/vendor/dsh-world/src/life/alarmTimer.ts +45 -0
  300. package/vendor/dsh-world/src/life/diary.ts +10 -0
  301. package/vendor/dsh-world/src/life/earlyStop.ts +27 -0
  302. package/vendor/dsh-world/src/life/home.ts +85 -0
  303. package/vendor/dsh-world/src/life/host.ts +50 -0
  304. package/vendor/dsh-world/src/life/journal.ts +44 -0
  305. package/vendor/dsh-world/src/life/lifeStore.ts +70 -0
  306. package/vendor/dsh-world/src/life/messages.ts +44 -0
  307. package/vendor/dsh-world/src/life/notify.ts +47 -0
  308. package/vendor/dsh-world/src/life/pricing.ts +37 -0
  309. package/vendor/dsh-world/src/life/runtime.ts +318 -0
  310. package/vendor/dsh-world/src/life/toolRegistry.ts +48 -0
  311. package/vendor/dsh-world/src/life/tools.ts +61 -0
  312. package/vendor/dsh-world/src/life/types.ts +19 -0
  313. package/vendor/dsh-world/src/persona.ts +26 -0
  314. package/vendor/dsh-world/src/promptSection.ts +71 -0
  315. package/vendor/dsh-world/src/qqScope.ts +51 -0
  316. package/vendor/dsh-world/src/runtime.ts +172 -0
  317. package/vendor/dsh-world/src/self.ts +32 -0
  318. package/vendor/dsh-world/src/sourceArchive.ts +132 -0
  319. package/vendor/dsh-world/src/store.ts +170 -0
  320. package/vendor/dsh-world/src/timeContext.ts +13 -0
  321. package/vendor/dsh-world/src/types.ts +112 -0
  322. package/vendor/dsh-world/src/worldPrompt.ts +64 -0
  323. package/vendor/dsh-world/src/worldSwitch.ts +82 -0
  324. package/vendor/dsh-world/templates/alarm.md +49 -0
  325. package/vendor/dsh-world/templates/world-prompt.md +7 -0
  326. package/vendor/dsh-world/tests/archive.test.ts +57 -0
  327. package/vendor/dsh-world/tests/archiveScope.test.ts +86 -0
  328. package/vendor/dsh-world/tests/bridge.test.ts +211 -0
  329. package/vendor/dsh-world/tests/channelSessions.test.ts +79 -0
  330. package/vendor/dsh-world/tests/clientHooks.test.ts +103 -0
  331. package/vendor/dsh-world/tests/compaction.test.ts +141 -0
  332. package/vendor/dsh-world/tests/console.test.ts +202 -0
  333. package/vendor/dsh-world/tests/consoleClient.test.ts +101 -0
  334. package/vendor/dsh-world/tests/enableCompaction.test.ts +19 -0
  335. package/vendor/dsh-world/tests/fixtures.ts +6 -0
  336. package/vendor/dsh-world/tests/history.test.ts +100 -0
  337. package/vendor/dsh-world/tests/hostFixture.ts +46 -0
  338. package/vendor/dsh-world/tests/lifeActivity.test.ts +70 -0
  339. package/vendor/dsh-world/tests/lifeAlarm.test.ts +87 -0
  340. package/vendor/dsh-world/tests/lifeDiary.test.ts +33 -0
  341. package/vendor/dsh-world/tests/lifeMessages.test.ts +61 -0
  342. package/vendor/dsh-world/tests/lifeNotify.test.ts +46 -0
  343. package/vendor/dsh-world/tests/lifeParts.test.ts +45 -0
  344. package/vendor/dsh-world/tests/lifeRuntime.test.ts +660 -0
  345. package/vendor/dsh-world/tests/lifeStore.test.ts +90 -0
  346. package/vendor/dsh-world/tests/lifeTools.test.ts +53 -0
  347. package/vendor/dsh-world/tests/outlets.test.ts +85 -0
  348. package/vendor/dsh-world/tests/panel.test.ts +85 -0
  349. package/vendor/dsh-world/tests/plugin.test.ts +614 -0
  350. package/vendor/dsh-world/tests/pricing.test.ts +21 -0
  351. package/vendor/dsh-world/tests/qq.test.ts +121 -0
  352. package/vendor/dsh-world/tests/qqSessionMode.test.ts +97 -0
  353. package/vendor/dsh-world/tests/qqSupervisor.test.ts +149 -0
  354. package/vendor/dsh-world/tests/sourceArchive.test.ts +58 -0
  355. package/vendor/dsh-world/tests/store.test.ts +82 -0
  356. package/vendor/dsh-world/tests/storeWriter.ts +5 -0
  357. package/vendor/dsh-world/tests/timeContext.test.ts +15 -0
  358. package/vendor/dsh-world/tests/wechat.test.ts +254 -0
  359. package/vendor/dsh-world/tests/wechatImages.test.ts +130 -0
  360. package/vendor/dsh-world/tests/wechatSending.test.ts +142 -0
  361. package/vendor/dsh-world/tests/wechatSupervisor.test.ts +91 -0
  362. package/vendor/dsh-world/tests/worldPrompt.test.ts +201 -0
  363. package/vendor/dsh-world/tsconfig.build.json +5 -0
  364. package/vendor/dsh-world/tsconfig.json +8 -0
@@ -0,0 +1,507 @@
1
+ /**
2
+ * sheet.js — 取帧 + 拼图 + 烧编号。零图像库:全部在 rawvideo RGB 缓冲上做。
3
+ *
4
+ * 为什么不用 ffmpeg 的 `tile` + `drawtext`:
5
+ * - drawtext 需要字体文件 + fontconfig + 平台相关的路径转义,装到别人机器上就得炸;
6
+ * - 我们只需要画 0-9 和一道横杠,用内置点阵字形更可控,且在低分辨率压缩后更抗糊。
7
+ *
8
+ * 布局约定(与 plan.js 一致):
9
+ * - 每格按 cover 方式缩放裁切(不变形,溢出部分裁掉)
10
+ * - 整图面积 <= budget(默认 640k),间隙 4px
11
+ * - 空位填中性灰 + “—”,且不编号
12
+ */
13
+
14
+ import { spawn } from 'node:child_process';
15
+
16
+ import { DEFAULT_FFMPEG } from './probe.js';
17
+
18
+ export const BACKGROUND = [18, 18, 20];
19
+ export const PAD_FILL = [42, 42, 46];
20
+ export const PAD_GLYPH = [110, 110, 116];
21
+ export const BADGE_BG = [0, 0, 0];
22
+ export const BADGE_FG = [255, 235, 59];
23
+ export const GUTTER = 4;
24
+ export const BADGE_ALPHA = 0.85;
25
+
26
+ /** 5×7 点阵,只有 0-9。scale 必须是整数,保证放大后边缘锐利。 */
27
+ export const DIGITS = {
28
+ 0: ['01110', '10001', '10001', '10001', '10001', '10001', '01110'],
29
+ 1: ['00100', '01100', '00100', '00100', '00100', '00100', '01110'],
30
+ 2: ['01110', '10001', '00001', '00010', '00100', '01000', '11111'],
31
+ 3: ['11111', '00010', '00100', '00010', '00001', '10001', '01110'],
32
+ 4: ['00010', '00110', '01010', '10010', '11111', '00010', '00010'],
33
+ 5: ['11111', '10000', '11110', '00001', '00001', '10001', '01110'],
34
+ 6: ['00110', '01000', '10000', '11110', '10001', '10001', '01110'],
35
+ 7: ['11111', '00001', '00010', '00100', '01000', '01000', '01000'],
36
+ 8: ['01110', '10001', '10001', '01110', '10001', '10001', '01110'],
37
+ 9: ['01110', '10001', '10001', '01111', '00001', '00010', '01100'],
38
+ };
39
+
40
+ export const DIGIT_W = 5;
41
+ export const DIGIT_H = 7;
42
+
43
+ /**
44
+ * 画布尺寸:整图面积 <= budget,格宽高按剩余空间均分。
45
+ * @returns {{width:number,height:number,cellW:number,cellH:number}}
46
+ */
47
+ export function layoutCanvas(cols, rows, cellAspect, budget = 640_000, gutter = GUTTER) {
48
+ const aspect = (cols * cellAspect) / rows;
49
+ const gw = Math.sqrt(budget * aspect);
50
+ const gh = gw / aspect;
51
+ const cellW = Math.max(1, Math.floor((gw - gutter * (cols + 1)) / cols));
52
+ const cellH = Math.max(1, Math.floor((gh - gutter * (rows + 1)) / rows));
53
+ return {
54
+ width: cols * cellW + gutter * (cols + 1),
55
+ height: rows * cellH + gutter * (rows + 1),
56
+ cellW,
57
+ cellH,
58
+ };
59
+ }
60
+
61
+ const clamp255 = (v) => (v < 0 ? 0 : v > 255 ? 255 : v | 0);
62
+
63
+ /** 在 RGB 画布上填一块矩形(支持 alpha 混合) */
64
+ export function fillRect(buf, width, height, x, y, w, h, color, alpha = 1) {
65
+ const [r, g, b] = color;
66
+ const x0 = Math.max(0, x);
67
+ const y0 = Math.max(0, y);
68
+ const x1 = Math.min(width, x + w);
69
+ const y1 = Math.min(height, y + h);
70
+ for (let py = y0; py < y1; py++) {
71
+ let idx = (py * width + x0) * 3;
72
+ for (let px = x0; px < x1; px++) {
73
+ if (alpha >= 1) {
74
+ buf[idx] = r; buf[idx + 1] = g; buf[idx + 2] = b;
75
+ } else {
76
+ buf[idx] = clamp255(buf[idx] * (1 - alpha) + r * alpha);
77
+ buf[idx + 1] = clamp255(buf[idx + 1] * (1 - alpha) + g * alpha);
78
+ buf[idx + 2] = clamp255(buf[idx + 2] * (1 - alpha) + b * alpha);
79
+ }
80
+ idx += 3;
81
+ }
82
+ }
83
+ }
84
+
85
+ /** 用点阵字形画一个字符串(只支持 0-9 与 "-"),返回实际占用尺寸 */
86
+ export function drawGlyphs(buf, width, height, text, x, y, scale, color) {
87
+ let cursor = x;
88
+ for (const ch of String(text)) {
89
+ if (ch === '-') {
90
+ fillRect(buf, width, height, cursor, y + Math.floor(DIGIT_H * scale * 0.6),
91
+ DIGIT_W * scale, Math.max(1, scale), color);
92
+ cursor += (DIGIT_W + 1) * scale;
93
+ continue;
94
+ }
95
+ const glyph = DIGITS[ch];
96
+ if (!glyph) continue;
97
+ for (let row = 0; row < DIGIT_H; row++) {
98
+ for (let col = 0; col < DIGIT_W; col++) {
99
+ if (glyph[row][col] === '1') {
100
+ fillRect(buf, width, height, cursor + col * scale, y + row * scale, scale, scale, color);
101
+ }
102
+ }
103
+ }
104
+ cursor += (DIGIT_W + 1) * scale;
105
+ }
106
+ return { width: cursor - x, height: DIGIT_H * scale };
107
+ }
108
+
109
+ /**
110
+ * 字形的整数缩放比:字号约占格高 22%,同时不超过格宽的 30%(两位编号时尤其重要)。
111
+ * 大一点更抗压缩——低分辨率下"认一个字符"比"数格子"稳得多。
112
+ */
113
+ export function badgeScale(cellH, cellW = Infinity, digits = 1, ratio = 0.18) {
114
+ const byHeight = Math.floor((cellH * ratio) / DIGIT_H);
115
+ const byWidth = Number.isFinite(cellW)
116
+ ? Math.floor((cellW * 0.26) / (Math.max(1, digits) * (DIGIT_W + 1)))
117
+ : byHeight;
118
+ return Math.max(2, Math.min(byHeight, byWidth));
119
+ }
120
+
121
+ /**
122
+ * 在一格里画编号徽标(黑底 + 黄字,左上角固定位置)。
123
+ * 不透明优先:半透明底块在白板/雪景上会直接消失(实测见设计方案 §3.5)。
124
+ */
125
+ export function drawBadge(buf, width, height, x, y, cellW, cellH, label, ratio = 0.18) {
126
+ const digits = String(label).length;
127
+ const scale = badgeScale(cellH, cellW, digits, ratio);
128
+ const pad = Math.max(2, Math.round(cellH * 0.025));
129
+ const textW = digits * (DIGIT_W + 1) * scale - scale;
130
+ const textH = DIGIT_H * scale;
131
+ fillRect(buf, width, height, x, y, textW + pad * 2, textH + pad * 2, BADGE_BG, BADGE_ALPHA);
132
+ drawGlyphs(buf, width, height, label, x + pad, y + pad, scale, BADGE_FG);
133
+ return { w: textW + pad * 2, h: textH + pad * 2 };
134
+ }
135
+
136
+ /** 空位:灰底 + 居中横杠,且绝对不编号 */
137
+ export function drawPadding(buf, width, height, x, y, cellW, cellH) {
138
+ fillRect(buf, width, height, x, y, cellW, cellH, PAD_FILL);
139
+ const barW = Math.max(6, Math.round(cellW * 0.32));
140
+ const barH = Math.max(2, Math.round(cellH * 0.03));
141
+ fillRect(buf, width, height,
142
+ x + Math.round((cellW - barW) / 2), y + Math.round((cellH - barH) / 2),
143
+ barW, barH, PAD_GLYPH);
144
+ }
145
+
146
+ /**
147
+ * 把若干帧(rawvideo rgb24)拼成一张画布。
148
+ * @param {{frames:Array<{buffer:Buffer, index:number}|null>, cols:number, rows:number,
149
+ * cellAspect:number, budget?:number, gutter?:number, burn?:'index'|'none'}} opts
150
+ * frames 顺序对应格位 1..cols*rows;null = 空位(补灰)
151
+ * @returns {{buffer:Buffer,width:number,height:number,cellW:number,cellH:number}}
152
+ */
153
+ export function composeSheet(opts) {
154
+ const {
155
+ frames, cols, rows, cellAspect, budget = 640_000, gutter = GUTTER,
156
+ burn = 'index', badgeRatio = 0.18,
157
+ } = opts;
158
+ if (!Array.isArray(frames)) throw new TypeError('frames must be an array');
159
+ if (frames.length > cols * rows) throw new RangeError('more frames than cells');
160
+ const { width, height, cellW, cellH } = layoutCanvas(cols, rows, cellAspect, budget, gutter);
161
+ const buf = Buffer.alloc(width * height * 3);
162
+ for (let i = 0; i < buf.length; i += 3) {
163
+ buf[i] = BACKGROUND[0]; buf[i + 1] = BACKGROUND[1]; buf[i + 2] = BACKGROUND[2];
164
+ }
165
+
166
+ for (let pos = 0; pos < cols * rows; pos++) {
167
+ const r = Math.floor(pos / cols);
168
+ const c = pos % cols;
169
+ const x = gutter + c * (cellW + gutter);
170
+ const y = gutter + r * (cellH + gutter);
171
+ const frame = frames[pos];
172
+ if (!frame) {
173
+ drawPadding(buf, width, height, x, y, cellW, cellH);
174
+ continue;
175
+ }
176
+ // 画面比格子小时(黑场格)居中摆放:黑屏里没有内容,占满一整格纯属浪费画布
177
+ const fw = frame.w ?? cellW;
178
+ const fh = frame.h ?? cellH;
179
+ if (fw < cellW || fh < cellH) {
180
+ const dx = x + Math.round((cellW - fw) / 2);
181
+ const dy = y + Math.round((cellH - fh) / 2);
182
+ blit(buf, width, height, dx, dy, fw, fh, frame.buffer, fw);
183
+ } else {
184
+ blit(buf, width, height, x, y, cellW, cellH, frame.buffer);
185
+ }
186
+ if (burn !== 'none') drawBadge(buf, width, height, x + 2, y + 2, cellW, cellH, frame.index, badgeRatio);
187
+ }
188
+ return { buffer: buf, width, height, cellW, cellH };
189
+ }
190
+
191
+ /**
192
+ * 把一格摊成 k×k 的小格(rawvideo rgb24),拼成一张 cellW×cellH 的图。
193
+ *
194
+ * 为什么这么做:快闪段里"这一格到底闪了几下"用一张代表帧说不清。把这一格再切成
195
+ * 3×3 或 4×4,同样一张图(同样 384 token)就能交代 9 个或 16 个瞬间。
196
+ * 代价是每个子帧只有 1/k² 的像素 —— 所以调用方必须先确认父格够大(见 chooseSubGrid)。
197
+ *
198
+ * @param {Buffer[]} tiles 长度必须等于 k*k,每个 tileW×tileH(rgb24)
199
+ */
200
+ export function tileSubFrames(tiles, k, cellW, cellH) {
201
+ const tileW = Math.floor(cellW / k);
202
+ const tileH = Math.floor(cellH / k);
203
+ const out = Buffer.alloc(cellW * cellH * 3);
204
+ for (let i = 0; i < k * k; i++) {
205
+ const src = tiles[i];
206
+ if (!src) continue;
207
+ const ox = (i % k) * tileW;
208
+ const oy = Math.floor(i / k) * tileH;
209
+ for (let row = 0; row < tileH; row++) {
210
+ src.copy(out, ((oy + row) * cellW + ox) * 3, row * tileW * 3, row * tileW * 3 + tileW * 3);
211
+ }
212
+ }
213
+ return out;
214
+ }
215
+
216
+ /** 把一格 rawvideo(尺寸必须等于 cellW×cellH)贴到画布上 */
217
+ /**
218
+ * 把一张 RGB 图贴到目标缓冲区的 (x,y),占 cellW×cellH。
219
+ *
220
+ * `srcW` 是**源图自己的行宽**。黑场格会取一张只占半格的小图(buffer 宽度就是 cellW/2),
221
+ * 靠它才能按小图的行宽正确拷贝 —— 否则每行都会错位成斜的。
222
+ */
223
+ export function blit(dst, dstW, dstH, x, y, cellW, cellH, src, srcW = cellW) {
224
+ const need = srcW * cellH * 3;
225
+ if (src.length < need) throw new RangeError(`frame buffer too small: ${src.length} < ${need}`);
226
+ for (let row = 0; row < cellH; row++) {
227
+ const dy = y + row;
228
+ if (dy < 0 || dy >= dstH) continue;
229
+ const sStart = row * srcW * 3;
230
+ const dStart = (dy * dstW + x) * 3;
231
+ src.copy(dst, dStart, sStart, sStart + cellW * 3);
232
+ }
233
+ }
234
+
235
+ /* ---------------------------------------------------------------- 音频条带 */
236
+
237
+ export const STRIP_BG = [11, 11, 13];
238
+ export const STRIP_BAR = [63, 184, 175];
239
+ export const STRIP_SILENCE = [42, 42, 46];
240
+ export const STRIP_TICK = [255, 235, 59];
241
+ export const STRIP_SEP = [60, 60, 66];
242
+
243
+ /**
244
+ * 在画布底部接一条音频带:响度包络(越高越响)+ 静音块 + 切点刻度。
245
+ *
246
+ * 关键价值是**同一条时间轴**:模型能把"第 5 格"和"这里最响"直接对上,
247
+ * 不用自己在脑子里做时间映射。同图内不额外花 token。
248
+ *
249
+ * @param {{buffer:Buffer,width:number,height:number}} sheet composeSheet 的产物
250
+ * @param {{loudness?:{series:number[],step:number}, silences?:Array<{from:number,to:number}>,
251
+ * cuts?:number[], window:{from:number,to:number}, stripH?:number}} opts
252
+ */
253
+ export function addAudioStrip(sheet, opts) {
254
+ const { buffer, width, height } = sheet;
255
+ const { loudness, silences = [], cuts = [], window: win } = opts;
256
+ const stripH = opts.stripH ?? Math.max(24, Math.round(width * 0.055));
257
+ const out = Buffer.alloc(width * (height + stripH) * 3);
258
+ buffer.copy(out, 0, 0, buffer.length);
259
+
260
+ const base = height * width * 3;
261
+ for (let i = base; i < out.length; i += 3) {
262
+ out[i] = STRIP_BG[0]; out[i + 1] = STRIP_BG[1]; out[i + 2] = STRIP_BG[2];
263
+ }
264
+
265
+ const series = loudness?.series ?? [];
266
+ const step = loudness?.step ?? 0.1;
267
+ const span = Math.max(0.001, win.to - win.from);
268
+ const t2x = (t) => Math.round(((t - win.from) / span) * (width - 1));
269
+
270
+ // 静音块:整段压暗(铺底),响度柱**画在它上面**。
271
+ // 早先试过"静音列不画柱子",图更纯净但很难看——灰块变成一根根实心竖条,
272
+ // 把波形切断。柱子压上去之后,灰块只在柱子上方露出来,读起来是"波形 + 底色"。
273
+ for (const s of silences) {
274
+ const x0 = Math.max(0, t2x(s.from));
275
+ const x1 = Math.min(width - 1, t2x(s.to));
276
+ for (let x = x0; x <= x1; x++) {
277
+ for (let y = 0; y < stripH; y++) {
278
+ const i = ((height + y) * width + x) * 3;
279
+ out[i] = STRIP_SILENCE[0]; out[i + 1] = STRIP_SILENCE[1]; out[i + 2] = STRIP_SILENCE[2];
280
+ }
281
+ }
282
+ }
283
+
284
+ // 响度包络:按列聚合,避免 600 列里丢细节
285
+ if (series.length > 0) {
286
+ const audible = series.filter((v) => v > -70);
287
+ const sorted = [...audible].sort((a, b) => a - b);
288
+ // 参照下限至少取到 hi-12dB:否则「全程恒定响度」的素材(如测试音、压得很平的成片)
289
+ // 会被归一化成一条几乎贴底的细线,看起来像坏了。
290
+ const hi = sorted[Math.floor(sorted.length * 0.97)] ?? -10;
291
+ const lo = Math.min(sorted[Math.floor(sorted.length * 0.1)] ?? -60, hi - 12);
292
+ const range = Math.max(6, hi - lo);
293
+ for (let x = 0; x < width; x++) {
294
+ const t0 = win.from + (x / width) * span;
295
+ const i0 = Math.max(0, Math.floor((t0 - win.from) / step));
296
+ const i1 = Math.max(i0 + 1, Math.min(series.length, Math.ceil(((t0 - win.from) + span / width) / step)));
297
+ let peak = -120;
298
+ for (let i = i0; i < i1; i++) peak = Math.max(peak, series[i]);
299
+ const norm = Math.max(0, Math.min(1, (peak - lo) / range));
300
+ const barH = Math.round(norm * (stripH - 4));
301
+ for (let y = 0; y < barH; y++) {
302
+ const py = height + stripH - 1 - y;
303
+ const i = (py * width + x) * 3;
304
+ out[i] = STRIP_BAR[0]; out[i + 1] = STRIP_BAR[1]; out[i + 2] = STRIP_BAR[2];
305
+ }
306
+ }
307
+ }
308
+
309
+ // 切点刻度:顶部短竖线
310
+ for (const t of cuts) {
311
+ if (t < win.from || t > win.to) continue;
312
+ const x = t2x(t);
313
+ for (let y = 0; y < Math.max(3, Math.round(stripH * 0.35)); y++) {
314
+ const i = ((height + y) * width + x) * 3;
315
+ out[i] = STRIP_TICK[0]; out[i + 1] = STRIP_TICK[1]; out[i + 2] = STRIP_TICK[2];
316
+ }
317
+ }
318
+
319
+ // 与宫格之间的分隔线
320
+ for (let x = 0; x < width; x++) {
321
+ const i = (height * width + x) * 3;
322
+ out[i] = STRIP_SEP[0]; out[i + 1] = STRIP_SEP[1]; out[i + 2] = STRIP_SEP[2];
323
+ }
324
+
325
+ return { buffer: out, width, height: height + stripH, stripH };
326
+ }
327
+
328
+ /* ---------------------------------------------------------------- 锐度 */
329
+
330
+ /**
331
+ * Laplacian 方差:越大越锐。用来避开"淡入/对焦中/高斯模糊转场"的中间帧。
332
+ *
333
+ * 为什么直方图不够:模糊不改变颜色分布,`thumbnail` 的"最接近均值"判据对
334
+ * 模糊→清晰这种渐变完全不敏感。实测概念 PV 的片尾人员表就中过招:
335
+ * 开头那 1 秒是高斯模糊,`thumbnail` 照样选它当代表帧。
336
+ */
337
+ export function sharpness(buf, width, height) {
338
+ const n = width * height;
339
+ const gray = new Float32Array(n);
340
+ for (let i = 0, p = 0; i < buf.length; i += 3, p++) {
341
+ gray[p] = 0.299 * buf[i] + 0.587 * buf[i + 1] + 0.114 * buf[i + 2];
342
+ }
343
+ let sum = 0;
344
+ let sum2 = 0;
345
+ let count = 0;
346
+ for (let y = 1; y < height - 1; y++) {
347
+ for (let x = 1; x < width - 1; x++) {
348
+ const i = y * width + x;
349
+ const v = -4 * gray[i] + gray[i - 1] + gray[i + 1] + gray[i - width] + gray[i + width];
350
+ sum += v;
351
+ sum2 += v * v;
352
+ count += 1;
353
+ }
354
+ }
355
+ if (count === 0) return 0;
356
+ const mean = sum / count;
357
+ return sum2 / count - mean * mean;
358
+ }
359
+
360
+ /**
361
+ * 段内取最锐的一帧:均匀取 candidates 个候选点,各抽一帧,比 Laplacian 方差。
362
+ *
363
+ * 成本:candidates 次「seek + 解 1 帧」,而不是解码整段。3 帧 ≈ 3 倍单帧耗时,
364
+ * 但仍是毫秒级——真正的开销是进程启动,不是解码。
365
+ */
366
+ export async function extractSharpest(opts) {
367
+ const {
368
+ input, at, dur, cellW, cellH, ffmpegPath = DEFAULT_FFMPEG,
369
+ candidates = 3, margin = 0.25, timeoutMs, http,
370
+ } = opts;
371
+ const safeDur = Math.max(0, dur || 0);
372
+ const pad = Math.min(margin, safeDur / 3);
373
+ const span = Math.max(0, safeDur - pad * 2);
374
+ const times = [];
375
+ if (candidates <= 1 || span < 0.1) {
376
+ times.push(safeDur > 0 ? at + safeDur / 2 : at);
377
+ } else {
378
+ for (let i = 0; i < candidates; i++) times.push(at + pad + (span * i) / (candidates - 1));
379
+ }
380
+ // 截断录制里末尾候选会越界(ffmpeg exit 0 + stdout 0 字节)。不能因为一个候选失败
381
+ // 就丢掉另外两个可解候选 —— 一次失败会让整张分镜归零(issue #30)。
382
+ const settled = await Promise.allSettled(times.map((t) =>
383
+ extractFrameRaw({ input, at: t, cellW, cellH, ffmpegPath, timeoutMs, http })
384
+ .then((f) => ({ ...f, sharp: sharpness(f.buffer, cellW, cellH) }))));
385
+ const picked = settled.filter((s) => s.status === 'fulfilled').map((s) => s.value);
386
+ if (picked.length === 0) {
387
+ const first = settled.find((s) => s.status === 'rejected');
388
+ throw first?.reason instanceof Error
389
+ ? first.reason
390
+ : new Error(`no readable frame around ${at.toFixed(2)}s`);
391
+ }
392
+ return picked.reduce((best, cur) => (cur.sharp > best.sharp ? cur : best));
393
+ }
394
+
395
+ /* ---------------------------------------------------------------- ffmpeg IO */
396
+
397
+ function exec(args, { input, timeoutMs = 120_000 } = {}) {
398
+ return new Promise((resolve, reject) => {
399
+ const child = spawn(args[0], args.slice(1), { stdio: ['pipe', 'pipe', 'pipe'] });
400
+ const out = [];
401
+ let err = '';
402
+ const timer = timeoutMs ? setTimeout(() => child.kill('SIGKILL'), timeoutMs) : null;
403
+ child.stdout.on('data', (d) => out.push(d));
404
+ child.stderr.on('data', (d) => { err += d.toString(); });
405
+ child.on('error', (e) => { clearTimeout(timer); reject(e); });
406
+ child.on('close', (code) => {
407
+ clearTimeout(timer);
408
+ if (code !== 0) reject(new Error(`ffmpeg exited ${code}: ${err.trim().split('\n').slice(-3).join(' | ')}`));
409
+ else resolve({ stdout: Buffer.concat(out), stderr: err });
410
+ });
411
+ if (input) child.stdin.end(input); else child.stdin.end();
412
+ });
413
+ }
414
+
415
+ /** cover 缩放:等比放大到完全覆盖 cell,再居中裁切(绝不变形) */
416
+ export const coverFilter = (cellW, cellH) =>
417
+ `scale=${cellW}:${cellH}:force_original_aspect_ratio=increase,crop=${cellW}:${cellH}`;
418
+
419
+ const PTS_RE = /pts_time:([0-9.]+)/g;
420
+
421
+ /**
422
+ * 远程取帧时要带的 HTTP 头。
423
+ * B 站 CDN 对**空 UA 直接 403**(实测),Referer 也校验;两个都必须放在 `-i` 之前。
424
+ * 本地路径不产生任何额外参数 —— 不能给所有调用都塞 header,那会让 ffmpeg 报未知选项以外的问题。
425
+ */
426
+ export function httpInputArgs(input, http) {
427
+ if (!http || !/^https?:\/\//i.test(String(input))) return [];
428
+ const args = [];
429
+ if (http.userAgent) args.push('-user_agent', http.userAgent);
430
+ if (http.referer) args.push('-referer', http.referer);
431
+ return args;
432
+ }
433
+
434
+ /**
435
+ * 取一帧,返回 rawvideo rgb24(正好 cellW*cellH*3 字节)与真实时刻。
436
+ * @param {{input:string, at:number, dur?:number, cellW:number, cellH:number,
437
+ * ffmpegPath?:string, mode?:'mid'|'thumb', timeoutMs?:number,
438
+ * http?:{userAgent?:string, referer?:string}}} opts
439
+ */
440
+ export async function extractFrameRaw(opts) {
441
+ const {
442
+ input, at, dur, cellW, cellH, ffmpegPath = DEFAULT_FFMPEG, mode = 'mid', timeoutMs, http,
443
+ } = opts;
444
+ const filter = coverFilter(cellW, cellH);
445
+ const netArgs = httpInputArgs(input, http);
446
+ const need = cellW * cellH * 3;
447
+ const baseSeek = mode === 'thumb' && dur > 0 ? at : (dur > 0 ? at + dur / 2 : at);
448
+ // 截断录制/容器比视频流长时,末尾 seek 可能越过最后一帧:ffmpeg 仍 exit 0、stdout 为空。
449
+ // 向段起点方向退几步再试,别让一格取不到帧拖垮整张图(issue #30)。
450
+ let last = { seek: baseSeek, length: 0 };
451
+ for (let attempt = 0; attempt <= 3; attempt += 1) {
452
+ const seek = Math.max(0, baseSeek - attempt * 0.5);
453
+ if (attempt > 0 && seek === last.seek) break;
454
+ let args;
455
+ if (mode === 'thumb' && dur > 0) {
456
+ // 段内取"直方图最接近该段均值"的帧:对溶解/过曝/空镜比硬取中点稳
457
+ const n = Math.max(2, Math.min(300, Math.round(dur * 25)));
458
+ args = ['-hide_banner', '-v', 'info', '-nostdin', '-ss', String(seek), '-t', String(dur), ...netArgs, '-i', input,
459
+ '-an', '-sn', '-vf', `thumbnail=n=${n},showinfo,${filter}`, '-frames:v', '1',
460
+ '-f', 'rawvideo', '-pix_fmt', 'rgb24', '-'];
461
+ } else {
462
+ args = ['-hide_banner', '-v', 'error', '-nostdin', '-ss', String(seek), ...netArgs, '-i', input,
463
+ '-an', '-sn', '-vf', filter, '-frames:v', '1',
464
+ '-f', 'rawvideo', '-pix_fmt', 'rgb24', '-'];
465
+ }
466
+ const { stdout, stderr } = await exec([ffmpegPath, ...args], { timeoutMs });
467
+ last = { seek, length: stdout.length };
468
+ if (stdout.length < need) continue;
469
+ let actual = seek;
470
+ if (mode === 'thumb') {
471
+ const hits = [...stderr.matchAll(PTS_RE)].map((m) => Number(m[1]));
472
+ if (hits.length > 0) actual = seek + hits[hits.length - 1];
473
+ }
474
+ return { buffer: Buffer.from(stdout.subarray(0, need)), t: actual };
475
+ }
476
+ throw new Error(`frame @${last.seek.toFixed(2)}s too small: ${last.length} < ${need}`);
477
+ }
478
+
479
+ /**
480
+ * 把 rawvideo 画布编码成 **PNG 字节**(内存里,不落盘)。
481
+ *
482
+ * 宿主 `ctx.attachments.saveImage()` 会**解码**校验传入字节,所以必须给真正的
483
+ * PNG,不能给 raw RGB(媒体类型写 image/png 但内容是裸像素 → "Unsupported or malformed image data")。
484
+ */
485
+ export async function encodePngBuffer(buffer, width, height, opts = {}) {
486
+ const { ffmpegPath = DEFAULT_FFMPEG, timeoutMs = 60_000 } = opts;
487
+ const { stdout } = await exec([ffmpegPath, '-hide_banner', '-v', 'error',
488
+ '-f', 'rawvideo', '-pix_fmt', 'rgb24', '-s', `${width}x${height}`, '-i', '-',
489
+ '-frames:v', '1', '-f', 'image2pipe', '-vcodec', 'png', 'pipe:1',
490
+ ], { input: buffer, timeoutMs });
491
+ if (stdout.length < 8 || stdout[0] !== 0x89 || stdout.subarray(1, 4).toString() !== 'PNG') {
492
+ throw new Error(`PNG encode produced ${stdout.length} bytes of non-PNG output`);
493
+ }
494
+ return stdout;
495
+ }
496
+
497
+ /** 把画布编码成图片(PNG 无损 / JPEG 体积小) */
498
+ export async function encodeRaw(buffer, width, height, out, opts = {}) {
499
+ const { ffmpegPath = DEFAULT_FFMPEG, quality, timeoutMs = 60_000 } = opts;
500
+ const args = [ffmpegPath, '-hide_banner', '-v', 'error', '-y',
501
+ '-f', 'rawvideo', '-pix_fmt', 'rgb24', '-s', `${width}x${height}`, '-i', '-',
502
+ '-frames:v', '1'];
503
+ if (quality !== undefined) args.push('-q:v', String(quality));
504
+ args.push(out);
505
+ await exec(args, { input: buffer, timeoutMs });
506
+ return out;
507
+ }
@@ -0,0 +1,93 @@
1
+ /**
2
+ * storage.js — 磁盘缓存只有一个总预算:**300 MB,全部加起来**。
3
+ *
4
+ * 之前是两个互不相干的盖子:
5
+ * - 扫描/ASR 的 JSON 缓存:50 MB(cache.js)
6
+ * - 下载下来的媒体文件:300 MB(fetch.js)
7
+ * 于是"上限"是 350 MB 起步,而且两边各自为政:媒体满了不会去动 JSON,JSON 满了也不会动媒体。
8
+ *
9
+ * 这里把它们当成一个池子:
10
+ * 1. 先算 JSON 占了多少,给媒体剩下的额度 = 预算 − JSON;
11
+ * 2. 媒体按"整条视频"从旧到新删(不留半条);
12
+ * 3. 还不够就把 JSON 按最久未用删。
13
+ *
14
+ * 为什么先动媒体:一条 40 分钟的片子低清就是 200–300 MB,删它是唯一有效的手段;
15
+ * 而 JSON 每个才 20 KB,删一千个也只是零头。反过来(先删 JSON)会白丢扫描结果。
16
+ *
17
+ * 台词和图片不在磁盘预算里:台词在 watch.json(几十 KB,受保护,永不裁),
18
+ * 图片只在内存里留最近 8 段、不落盘 —— 所以它们不会挤占这 300 MB。
19
+ */
20
+
21
+ import { readdir, stat } from 'node:fs/promises';
22
+ import { join } from 'node:path';
23
+
24
+ import { cacheDir, cacheBytes, pruneCache } from './cache.js';
25
+ import { mediaDir, pruneMedia } from './media-store.js';
26
+
27
+ /** 总预算:媒体 + JSON + 状态文件,加起来不超过这个数 */
28
+ export const STORAGE_BUDGET_BYTES = 300 * 1024 * 1024;
29
+
30
+ /** 媒体目录占用(每个 <key>/ 是一个视频的全部文件) */
31
+ async function mediaBytes(dir) {
32
+ let entries = [];
33
+ try {
34
+ entries = await readdir(dir, { withFileTypes: true });
35
+ } catch {
36
+ return 0;
37
+ }
38
+ let total = 0;
39
+ for (const entry of entries) {
40
+ if (!entry.isDirectory()) continue;
41
+ let files = [];
42
+ try {
43
+ files = await readdir(join(dir, entry.name), { withFileTypes: true });
44
+ } catch {
45
+ continue;
46
+ }
47
+ for (const f of files) {
48
+ if (!f.isFile()) continue;
49
+ try {
50
+ total += (await stat(join(dir, entry.name, f.name))).size;
51
+ } catch { /* 忽略 */ }
52
+ }
53
+ }
54
+ return total;
55
+ }
56
+
57
+ /** 当前占用(分开报,方便排查是谁吃掉的) */
58
+ export async function storageUsage({ dir = cacheDir(), media = mediaDir() } = {}) {
59
+ const json = await cacheBytes({ dir });
60
+ const mediaUsed = await mediaBytes(media);
61
+ return { json, media: mediaUsed, total: json + mediaUsed };
62
+ }
63
+
64
+ /**
65
+ * 按总预算裁剪。
66
+ * @param {{budgetBytes?: number, dir?: string, media?: string}} [o]
67
+ * @returns {Promise<{json:number, media:number, total:number, removedJson:number, removedMedia:number}>}
68
+ */
69
+ export async function pruneStorage({
70
+ budgetBytes = STORAGE_BUDGET_BYTES,
71
+ dir = cacheDir(),
72
+ media = mediaDir(),
73
+ } = {}) {
74
+ const before = await storageUsage({ dir, media });
75
+
76
+ // 1) 媒体先让位(它才是大头)
77
+ const mediaAllowed = Math.max(0, budgetBytes - before.json);
78
+ const mediaCut = await pruneMedia({ dir: media, maxBytes: mediaAllowed });
79
+
80
+ // 2) 还不够,再动 JSON
81
+ const afterMedia = await storageUsage({ dir, media });
82
+ const jsonAllowed = Math.max(0, budgetBytes - afterMedia.media);
83
+ const jsonCut = await pruneCache({ dir, maxBytes: jsonAllowed });
84
+
85
+ const after = await storageUsage({ dir, media });
86
+ return {
87
+ json: after.json,
88
+ media: after.media,
89
+ total: after.total,
90
+ removedJson: jsonCut.removed,
91
+ removedMedia: mediaCut.removed,
92
+ };
93
+ }