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,254 @@
1
+ import assert from 'node:assert/strict';
2
+ import test from 'node:test';
3
+ import { createServer } from 'node:http';
4
+ import { existsSync } from 'node:fs';
5
+ import { readFile, writeFile } from 'node:fs/promises';
6
+ import { join } from 'node:path';
7
+ import { createUserMessage, ToolCallId, type ContextFormed, type GenerateOptions, type StreamChunk } from '@deepseek-ai/dsh-llm';
8
+ import { SessionId } from '@deepseek-ai/dsh-session';
9
+ import { hostFixture, textResponse } from './hostFixture.ts';
10
+ import { createWechatHome } from '../src/ecosystem/wechat/home.ts';
11
+ import { createWechatModule } from '../src/ecosystem/wechat/index.ts';
12
+ import { replyMessages } from '../src/ecosystem/wechat/messages.ts';
13
+ import { createWechatRecorder } from '../src/ecosystem/wechat/archive.ts';
14
+ import { createArchiveStore } from '../src/store.ts';
15
+ import { defineTool } from '@deepseek-ai/dsh-tools';
16
+ import { channelContext } from '../src/ecosystem/shared/context.ts';
17
+
18
+ declare module '@deepseek-ai/dsh-llm' {
19
+ interface MessageSourceMap { 'runtime-context': { kind: 'runtime-context' } & ContextFormed }
20
+ interface MessageSourceMap { 'skill-catalog': { kind: 'skill-catalog'; form: 'catalog'; entries: { name: string; description: string }[] } }
21
+ }
22
+
23
+ const identity = { channel: 'wechat', accountId: 'wxid_bot', conversationType: 'group' as const, conversationId: 'family@chatroom', speakerId: 'wechat:wxid_mom', speakerName: '妈妈' };
24
+
25
+ test('group context preserves sender IDs without accepting another conversation or becoming a fresh user request', async t => {
26
+ const previous = { id: 'm.db:1', occurredAt: '2026-10-25T00:00:00Z', identity: { ...identity, speakerId: 'wechat:dad', speakerName: '爸爸' }, text: '前一句' };
27
+ assert.throws(() => channelContext([{ ...previous, identity: { ...identity, conversationId: 'other@chatroom' } }], identity));
28
+ assert.throws(() => channelContext(Array.from({ length: 9 }, () => previous), identity));
29
+ const requests: GenerateOptions[] = [];
30
+ const { ctx, cleanup } = await hostFixture(t, request => { requests.push(request); return textResponse('群回复'); });
31
+ const agent = (await ctx.agents.create({ sessionId: SessionId('group'), agentOptions: { provider: 'test', model: 'test' } })).agent;
32
+ const channel = createWechatHome({ ctx, home: async received => { assert.equal(received.conversationId, identity.conversationId); return agent; } }); cleanup.push(channel.stop);
33
+ await channel.ask('@她', identity, [previous]);
34
+ const context = requests[0].messages.find(message => message.source?.kind === 'dsh-world-channel-context');
35
+ assert.ok(context);
36
+ assert.match(JSON.stringify(context), /wechat:dad/);
37
+ assert.match(JSON.stringify(context), /前一句/);
38
+ });
39
+
40
+ test('delivery updates the same durable record, survives reopening and rejects stale or invalid receipts', async t => {
41
+ const { root, cleanup } = await hostFixture(t, () => textResponse('unused'));
42
+ const config = { root, worldId: 'test', agentPreset: 'test', timeZone: 'UTC' };
43
+ let store = createArchiveStore(config);
44
+ cleanup.push(() => store.close());
45
+ const recorder = createWechatRecorder(store, 'test');
46
+ const id = await recorder.record({ sessionId: 'home', messageId: 'answer', seq: 10, turn: 1, time: Date.now(), identity,
47
+ messages: [{ type: 'text', text: '第一句' }, { type: 'sticker', stickerId: 'st_hi' }, { type: 'text', text: '最后一句' }] });
48
+ await recorder.receipt({ id, confirmed: 1, status: 'uncertain' });
49
+ assert.match((await store.readPage({ worldId: 'test', limit: 10 })).records[0].content, /已确认发送 1\/3/);
50
+ store.close(); store = createArchiveStore(config);
51
+ const restored = createWechatRecorder(store, 'test');
52
+ await restored.receipt({ id, confirmed: 3, status: 'sent' });
53
+ await restored.receipt({ id, confirmed: 3, status: 'sent' });
54
+ await assert.rejects(restored.receipt({ id, confirmed: 1, status: 'uncertain' }));
55
+ await assert.rejects(restored.receipt({ id, confirmed: 4, status: 'sent' }));
56
+ const records = (await store.readPage({ worldId: 'test', limit: 10 })).records;
57
+ assert.equal(records.length, 1);
58
+ assert.match(records[0].content, /已确认发送 3\/3/);
59
+ assert.doesNotMatch(records[0].content, /待发送|送达尚未确认/);
60
+ const exported = await readFile(join(root, 'worlds/test/conversations', records[0].occurredAt.slice(0, 10) + '.jsonl'), 'utf8');
61
+ assert.equal(exported.trim().split('\n').length, 1);
62
+ assert.equal(JSON.parse(exported).content, records[0].content);
63
+ });
64
+
65
+ test('invalid outgoing payloads are rejected as a whole', () => {
66
+ assert.throws(() => replyMessages([{ type: 'text', text: 'ok' }, { type: 'image' }]));
67
+ assert.throws(() => replyMessages([{ type: 'text', text: '鱼'.repeat(1400) }]));
68
+ assert.throws(() => replyMessages(Array.from({ length: 17 }, () => ({ type: 'text', text: 'x' }))));
69
+ });
70
+
71
+ test('non-home group delivery cannot bypass the configured shared archive scope', async t => {
72
+ const { root, cleanup } = await hostFixture(t, () => textResponse('unused'));
73
+ const store = createArchiveStore({ root, worldId: 'test', agentPreset: 'test', timeZone: 'UTC' }); cleanup.push(() => store.close());
74
+ const recorder = createWechatRecorder(store, 'test', id => id === 'home');
75
+ const id = await recorder.record({ sessionId: 'group', messageId: 'group-answer', seq: 5, turn: 1, time: Date.now(), identity,
76
+ messages: [{ type: 'text', text: '只在群里的话' }] });
77
+ await recorder.receipt({ id, confirmed: 1, status: 'sent' });
78
+ assert.equal((await store.readPage({ worldId: 'test', limit: 10 })).records.length, 0);
79
+ assert.deepEqual(store.delivery(id), { status: 'sent', confirmed: 1, total: 1 });
80
+ });
81
+
82
+ test('an empty final answer does not send earlier tool commentary as the WeChat reply', async t => {
83
+ let calls = 0;
84
+ const { ctx, cleanup } = await hostFixture(t, () => (async function* (): AsyncIterable<StreamChunk> {
85
+ if (calls++ === 0) {
86
+ yield { type: 'block-start', index: 0, blockType: 'text' };
87
+ yield { type: 'text-delta', index: 0, text: '我先查一下' };
88
+ yield { type: 'block-end', index: 0, block: { type: 'text', text: '我先查一下' } };
89
+ const block = { type: 'tool-call' as const, id: ToolCallId('check'), name: 'check', arguments: '{}' };
90
+ yield { type: 'block-start', index: 1, blockType: 'tool-call' };
91
+ yield { type: 'tool-call-delta', index: 1, id: block.id, name: block.name, argumentsDelta: block.arguments };
92
+ yield { type: 'block-end', index: 1, block };
93
+ yield { type: 'finish', reason: { kind: 'tool-calls' } };
94
+ } else yield { type: 'finish', reason: { kind: 'stop' } };
95
+ })());
96
+ const home = (await ctx.agents.create({ sessionId: SessionId('home'), agentOptions: { provider: 'test', model: 'test' } })).agent;
97
+ cleanup.push(home.ctx.tools.register(defineTool({ name: 'check', description: 'check', parameters: {}, output: { schema: { type: 'string' }, render: (_args, value) => [{ type: 'text', text: value }] }, execute: async () => 'done' })));
98
+ const channel = createWechatHome({ ctx, home: async () => home }); cleanup.push(channel.stop);
99
+ const reply = await channel.ask('查一下', identity);
100
+ assert.equal(reply.text, '');
101
+ assert.equal('messages' in reply, false);
102
+ });
103
+
104
+ test('WeChat requests use the existing home and keep user-role sender identity', async t => {
105
+ const requests: GenerateOptions[] = [];
106
+ const { ctx, cleanup } = await hostFixture(t, request => { requests.push(request); return textResponse('最终答复'); });
107
+ const home = (await ctx.agents.create({ sessionId: SessionId('home'), agentOptions: { provider: 'test', model: 'test' } })).agent;
108
+ const channel = createWechatHome({ ctx, home: async () => home }); cleanup.push(channel.stop);
109
+ const reply = await channel.ask('今晚吃什么', identity);
110
+ assert.equal(reply.text, '最终答复');
111
+ assert.equal('deliveryId' in reply, false, 'ordinary final text is kept in the session, not automatically sent');
112
+ assert.equal('messages' in reply, false);
113
+ const sent = requests[0].messages.find(message => message.id === reply.id)!;
114
+ assert.equal(sent.role, 'user');
115
+ assert.deepEqual((sent.source as unknown as { identity: unknown }).identity, identity);
116
+ const event = home.session.snapshotEvents().find(event => event.type === 'user/message' && event.data.id === reply.id);
117
+ assert.ok(event, 'the actual home log contains the message');
118
+ });
119
+
120
+ test('WeChat conversations wait their turn and do not receive each other\'s reply', async t => {
121
+ const { ctx, cleanup } = await hostFixture(t, request => {
122
+ const last = request.messages.filter(message => message.role === 'user').at(-1)!;
123
+ return textResponse(last.content.filter(block => block.type === 'text').map(block => block.text).join('').split('\n').at(-1)!);
124
+ });
125
+ const home = (await ctx.agents.create({ sessionId: SessionId('home'), agentOptions: { provider: 'test', model: 'test' } })).agent;
126
+ const channel = createWechatHome({ ctx, home: async () => home }); cleanup.push(channel.stop);
127
+ const [a, b] = await Promise.all([channel.ask('第一人', identity), channel.ask('第二人', { ...identity, conversationId: 'private', speakerId: 'wechat:wxid_dad' })]);
128
+ assert.equal(a.text, '第一人'); assert.equal(b.text, '第二人'); assert.notEqual(a.id, b.id);
129
+ channel.stop(); await assert.rejects(channel.ask('停止后', identity), /stopped/);
130
+ });
131
+
132
+ test('a later desktop turn is not returned as the WeChat answer', async t => {
133
+ let release!: () => void, entered!: () => void;
134
+ const gate = new Promise<void>(resolve => { release = resolve; });
135
+ const running = new Promise<void>(resolve => { entered = resolve; });
136
+ let calls = 0;
137
+ const { ctx, cleanup } = await hostFixture(t, () => (async function* () {
138
+ const first = calls++ === 0;
139
+ if (first) { entered(); await gate; }
140
+ yield* textResponse(first ? '微信答复' : '桌面私密答复');
141
+ })());
142
+ const home = (await ctx.agents.create({ sessionId: SessionId('home'), agentOptions: { provider: 'test', model: 'test' } })).agent;
143
+ const channel = createWechatHome({ ctx, home: async () => home }); cleanup.push(channel.stop);
144
+ const reply = channel.ask('群里的问题', identity);
145
+ await running;
146
+ home.followup(createUserMessage({ source: { kind: 'user' }, content: [{ type: 'text', text: '桌面问题' }] }));
147
+ release();
148
+ assert.equal((await reply).text, '微信答复');
149
+ await home.whenIdle();
150
+ });
151
+
152
+ test('a cancelled home request is a failure, not a successful empty answer', async t => {
153
+ const { ctx, cleanup } = await hostFixture(t, () => textResponse('不应发送'));
154
+ const home = (await ctx.agents.create({ sessionId: SessionId('home'), agentOptions: { provider: 'test', model: 'test' } })).agent;
155
+ const channel = createWechatHome({ ctx, home: async () => home }); cleanup.push(channel.stop);
156
+ cleanup.push(ctx.on('session/event', (session, event) => {
157
+ if (session.id === home.id && event.type === 'user/message') home.cancel({ kind: 'user' });
158
+ }));
159
+ await assert.rejects(channel.ask('取消的消息', identity), /did not finish/);
160
+ });
161
+
162
+ test('a reply is not released for sending until its own session log is durable', async t => {
163
+ const { ctx, cleanup } = await hostFixture(t, () => textResponse('不能丢失的群回复'));
164
+ const agent = (await ctx.agents.create({ sessionId: SessionId('group'), agentOptions: { provider: 'test', model: 'test' } })).agent;
165
+ let ended = false;
166
+ const offEvent = ctx.on('session/event', (session, event) => { if (session.id === agent.id && event.type === 'turn/end') ended = true; });
167
+ const offFlush = ctx.on('session/flush', async session => { if (ended && session.id === agent.id) throw new Error('group log disk failure'); });
168
+ const channel = createWechatHome({ ctx, home: async () => agent }); cleanup.push(channel.stop);
169
+ try {
170
+ await assert.rejects(channel.ask('群问题', identity), /group log disk failure/);
171
+ } finally { offEvent(); offFlush(); }
172
+ });
173
+
174
+ test('desktop words steered into the same turn prevent a mixed answer from leaving on WeChat', async t => {
175
+ let release!: () => void, entered!: () => void;
176
+ const gate = new Promise<void>(resolve => { release = resolve; });
177
+ const running = new Promise<void>(resolve => { entered = resolve; });
178
+ const { ctx, cleanup } = await hostFixture(t, () => (async function* () {
179
+ entered(); await gate; yield* textResponse('混合内容');
180
+ })());
181
+ const home = (await ctx.agents.create({ sessionId: SessionId('home'), agentOptions: { provider: 'test', model: 'test' } })).agent;
182
+ const channel = createWechatHome({ ctx, home: async () => home }); cleanup.push(channel.stop);
183
+ const reply = channel.ask('微信问题', identity);
184
+ const rejected = assert.rejects(reply, /did not finish/);
185
+ await running;
186
+ home.steer(createUserMessage({ source: { kind: 'user' }, content: [{ type: 'text', text: '桌面内容' }] }));
187
+ release();
188
+ await rejected; await home.whenIdle();
189
+ });
190
+
191
+ test('host snapshots, the first-session skill catalog and memory recall do not count as another person speaking', async t => {
192
+ let injectSnapshot = () => {};
193
+ const { ctx, cleanup } = await hostFixture(t, () => { injectSnapshot(); return textResponse('微信正常答复'); });
194
+ const home = (await ctx.agents.create({ sessionId: SessionId('home'), agentOptions: { provider: 'test', model: 'test' } })).agent;
195
+ injectSnapshot = () => {
196
+ home.session.append('user/message', createUserMessage({ source: { kind: 'runtime-context', form: 'snapshot', sections: [{ name: 'time', text: '当前时间' }] }, content: [{ type: 'text', text: '当前时间' }] }), { surfaceOp: 'append' });
197
+ home.session.append('user/message', createUserMessage({ source: { kind: 'skill-catalog', form: 'catalog', entries: [{ name: 'test', description: '工具目录' }] }, content: [{ type: 'text', text: '可用技能目录' }] }), { surfaceOp: 'append' });
198
+ home.session.append('user/message', createUserMessage({ source: { kind: 'dsh-world-history', form: 'recall' }, content: [{ type: 'text', text: '压缩后重注入的记忆' }] }), { surfaceOp: 'append' });
199
+ };
200
+ const channel = createWechatHome({ ctx, home: async () => home }); cleanup.push(channel.stop);
201
+ assert.equal((await channel.ask('在吗', identity)).text, '微信正常答复');
202
+ });
203
+
204
+ test('her own alarm ringing during a WeChat reply does not make the request fail', async t => {
205
+ let release!: () => void, entered!: () => void;
206
+ const gate = new Promise<void>(resolve => { release = resolve; });
207
+ const running = new Promise<void>(resolve => { entered = resolve; });
208
+ const { ctx, cleanup } = await hostFixture(t, () => (async function* () {
209
+ entered(); await gate; yield* textResponse('回完了');
210
+ })());
211
+ const home = (await ctx.agents.create({ sessionId: SessionId('home'), agentOptions: { provider: 'test', model: 'test' } })).agent;
212
+ const channel = createWechatHome({ ctx, home: async () => home }); cleanup.push(channel.stop);
213
+ const reply = channel.ask('微信问题', identity);
214
+ await running;
215
+ home.steer(createUserMessage({ source: { kind: 'dsh-world-life' }, content: [{ type: 'text', text: '闹钟响了' }] }));
216
+ release();
217
+ assert.equal((await reply).text, '回完了');
218
+ });
219
+
220
+ test('the independent WeChat endpoint authenticates and validates before entering home; stop removes only its own discovery file', async t => {
221
+ let calls = 0;
222
+ const receipts: unknown[] = [];
223
+ const { ctx, root, cleanup } = await hostFixture(t, () => { calls++; return textResponse('已收到'); });
224
+ const agent = (await ctx.agents.create({ sessionId: SessionId('home'), agentOptions: { provider: 'test', model: 'test' } })).agent;
225
+ const file = join(root, 'wechat.json');
226
+ const channel = createWechatModule({ ctx, file, home: async () => agent, origin: () => 'http://127.0.0.1:1', allowedHosts: [], receipt: async receipt => { receipts.push(receipt); } });
227
+ channel.start(); cleanup.push(channel.stop);
228
+ const { token, path } = JSON.parse(await readFile(file, 'utf8'));
229
+ assert.equal(path, '/dsh-world/wechat/ask');
230
+ const server = createServer((req, res) => { void channel.route.handler(req, res); });
231
+ await new Promise<void>(resolve => server.listen(0, '127.0.0.1', resolve));
232
+ cleanup.push(() => new Promise<void>((resolve, reject) => server.close(error => error ? reject(error) : resolve())));
233
+ const address = server.address(); assert.ok(address && typeof address === 'object');
234
+ const url = `http://127.0.0.1:${address.port}${path}`;
235
+ const request = (body: unknown, auth = token) => fetch(url, { method: 'POST', headers: { 'content-type': 'application/json', 'x-dsh-world-token': auth }, body: JSON.stringify(body) });
236
+ const receipt = { id: 'a'.repeat(64), confirmed: 2, status: 'sent' };
237
+ assert.equal((await request({ receipt }, 'invalid')).status, 403);
238
+ assert.equal((await request({ receipt: { ...receipt, confirmed: -1 } })).status, 400);
239
+ assert.deepEqual(receipts, []);
240
+ assert.equal((await request({ receipt })).status, 200);
241
+ assert.deepEqual(receipts, [receipt]);
242
+ assert.equal((await request({ text: 'hello', identity }, 'invalid')).status, 403);
243
+ assert.equal((await request({ text: 'hello', identity: { ...identity, speakerId: 'qq:123' } })).status, 400);
244
+ assert.equal((await request({ text: 'hello', identity: { ...identity, channel: 'qq', speakerId: 'qq:123' } })).status, 400);
245
+ assert.equal((await request({ text: 'a'.repeat(40000), identity })).status, 400);
246
+ assert.equal(calls, 0);
247
+ const response = await request({ text: 'hello', identity });
248
+ assert.equal(response.status, 200);
249
+ assert.equal((await response.json()).text, '已收到');
250
+ channel.stop(); assert.equal(existsSync(file), false);
251
+ assert.equal((await request({ text: 'hello', identity })).status, 503);
252
+ await writeFile(file, JSON.stringify({ token: 'newer-module' }));
253
+ channel.stop(); assert.equal(existsSync(file), true);
254
+ });
@@ -0,0 +1,130 @@
1
+ import assert from 'node:assert/strict';
2
+ import test from 'node:test';
3
+ import { createHash } from 'node:crypto';
4
+ import { mkdir, readFile, writeFile } from 'node:fs/promises';
5
+ import { join } from 'node:path';
6
+ import { createServer } from 'node:http';
7
+ import { AttachmentId, type SaveImageAttachment } from '@deepseek-ai/dsh-attachment';
8
+ import { SessionId } from '@deepseek-ai/dsh-session';
9
+ import { admitContextImages } from '../src/ecosystem/wechat/images.ts';
10
+ import { createWechatHome } from '../src/ecosystem/wechat/home.ts';
11
+ import { createWechatModule } from '../src/ecosystem/wechat/index.ts';
12
+ import { channelContext } from '../src/ecosystem/shared/context.ts';
13
+ import { conversationKey } from '../src/ecosystem/shared/identity.ts';
14
+ import { extractConversationRecords } from '../src/archive.ts';
15
+ import { hostFixture, textResponse } from './hostFixture.ts';
16
+ import type { GenerateOptions } from '@deepseek-ai/dsh-llm';
17
+
18
+ const identity = { channel: 'wechat', accountId: 'bot', conversationType: 'group' as const, conversationId: 'group@chatroom', speakerId: 'wechat:owner' };
19
+ const png = Buffer.from('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+a15kAAAAASUVORK5CYII=', 'base64');
20
+ const digest = createHash('sha256').update(png).digest('hex');
21
+ const image = { file: digest + '.png', quality: 'cached' as const };
22
+ const previous = { id: 'm.db:4', occurredAt: '2026-10-25T00:00:00Z', identity, text: '【图片】', image };
23
+
24
+ test('group images are committed before their session event and never become shared memory records', async t => {
25
+ const requests: GenerateOptions[] = [];
26
+ const { ctx, root, cleanup } = await hostFixture(t, request => { requests.push(request); return textResponse('看到了'); });
27
+ const incoming = join(root, 'incoming');
28
+ const directory = join(incoming, createHash('sha256').update(conversationKey(identity)).digest('hex'));
29
+ await mkdir(directory, { recursive: true }); await writeFile(join(directory, image.file), png);
30
+ const saved = join(root, 'saved.png');
31
+ ctx.provide('attachments', {
32
+ imageLimits: { maxImageBytes: 1024 },
33
+ saveImages: async (inputs: readonly SaveImageAttachment[]) => {
34
+ assert.equal(inputs.length, 1); assert.deepEqual(inputs[0].data, png);
35
+ await writeFile(saved, inputs[0].data);
36
+ return [{ attachmentId: AttachmentId(digest), mediaType: 'image/png', bytes: png.length, width: 1, height: 1 }];
37
+ },
38
+ } as never);
39
+ const agent = (await ctx.agents.create({ sessionId: SessionId('group'), meta: { agentPreset: 'test' }, agentOptions: { provider: 'test', model: 'test' } })).agent;
40
+ const channel = createWechatHome({ ctx, home: async () => agent, images: (source, context) => admitContextImages(ctx, incoming, source, context) }); cleanup.push(channel.stop);
41
+ await channel.ask('这是什么图片', identity, [previous]);
42
+ assert.deepEqual(await readFile(saved), png);
43
+ const context = requests[0].messages.find(message => message.source?.kind === 'dsh-world-channel-context')!;
44
+ assert.ok(context.content.some(block => block.type === 'image' && block.attachment.attachmentId === digest));
45
+ const log = await ctx.sessionPersistence.open(agent.id, 'read');
46
+ try {
47
+ const { events } = await log.read(0, 128);
48
+ const pictureEvent = events.find(event => event.type === 'user/message' && event.data.source.kind === 'dsh-world-channel-context')!;
49
+ assert.match(JSON.stringify(pictureEvent), new RegExp(digest));
50
+ assert.deepEqual(extractConversationRecords(pictureEvent, { worldId: 'test', agentPreset: 'test', header: log.header, inheritedEventCount: 0, turn: 1 }), []);
51
+ const actualUser = events.find(event => event.type === 'user/message' && event.data.source.kind === 'user')!;
52
+ assert.doesNotMatch(JSON.stringify(actualUser), new RegExp(digest));
53
+ } finally { await log.close(); }
54
+ });
55
+
56
+ test('image admission refuses traversal, another group and changed bytes before committing attachments', async t => {
57
+ const { ctx, root } = await hostFixture(t, () => textResponse('unused'));
58
+ let saves = 0;
59
+ ctx.provide('attachments', { imageLimits: { maxImageBytes: 1024 }, saveImages: async () => { saves++; return []; } } as never);
60
+ assert.throws(() => channelContext([{ ...previous, image: { ...image, file: '../private.png' } }], identity));
61
+ const directory = join(root, createHash('sha256').update(conversationKey(identity)).digest('hex'));
62
+ await mkdir(directory); await writeFile(join(directory, image.file), Buffer.from('different bytes'));
63
+ await assert.rejects(admitContextImages(ctx, root, identity, [previous]), /digest/);
64
+ await assert.rejects(admitContextImages(ctx, root, { ...identity, conversationId: 'other@chatroom' }, [previous]));
65
+ assert.equal(saves, 0);
66
+ });
67
+
68
+ for (const caption of ['', '这张图里有什么']) test(`private images reach the model and only real caption text is archived: ${caption || 'image only'}`, async t => {
69
+ const requests: GenerateOptions[] = [];
70
+ const { ctx, root, cleanup } = await hostFixture(t, request => { requests.push(request); return textResponse('看到了'); });
71
+ const privateIdentity = { ...identity, conversationType: 'private' as const, conversationId: 'owner' };
72
+ const media = [{ ...previous, identity: privateIdentity }];
73
+ const incoming = join(root, 'incoming');
74
+ const directory = join(incoming, createHash('sha256').update(conversationKey(privateIdentity)).digest('hex'));
75
+ await mkdir(directory, { recursive: true }); await writeFile(join(directory, image.file), png);
76
+ let saved = false;
77
+ ctx.provide('attachments', {
78
+ imageLimits: { maxImageBytes: 1024 },
79
+ saveImages: async (inputs: readonly SaveImageAttachment[]) => {
80
+ assert.deepEqual(inputs[0].data, png); saved = true;
81
+ return [{ attachmentId: AttachmentId(digest), mediaType: 'image/png', bytes: png.length, width: 1, height: 1 }];
82
+ },
83
+ } as never);
84
+ const agent = (await ctx.agents.create({ sessionId: SessionId('private'), meta: { agentPreset: 'test' }, agentOptions: { provider: 'test', model: 'test' } })).agent;
85
+ const channel = createWechatHome({ ctx, home: async () => agent, images: (source, context) => admitContextImages(ctx, incoming, source, context) }); cleanup.push(channel.stop);
86
+ await channel.ask(caption, privateIdentity, undefined, media);
87
+ assert.equal(saved, true, 'the image must be saved before the model request');
88
+ assert.ok(requests[0].messages.some(message => message.content.some(block => block.type === 'image')));
89
+ const log = await ctx.sessionPersistence.open(agent.id, 'read');
90
+ try {
91
+ const { events } = await log.read(0, 128);
92
+ assert.ok(events.some(event => event.type === 'user/message' && JSON.stringify(event).includes(digest)));
93
+ const records = events.flatMap(event => extractConversationRecords(event, { worldId: 'test', agentPreset: 'test', header: log.header, inheritedEventCount: 0, turn: 1 }));
94
+ assert.deepEqual(records.filter(record => record.role === 'user').map(record => record.content), caption ? [caption] : []);
95
+ assert.doesNotMatch(JSON.stringify(records), new RegExp(digest));
96
+ } finally { await log.close(); }
97
+ await assert.rejects(admitContextImages(ctx, incoming, privateIdentity, [{ ...media[0], identity: { ...privateIdentity, speakerId: 'wechat:other' } }]));
98
+ if (!caption) {
99
+ await channel.ask('刚才这张图再说明一下', privateIdentity);
100
+ assert.ok(requests.at(-1)!.messages.some(message => message.content.some(block => block.type === 'image')), 'a later caption keeps the already admitted image context');
101
+ }
102
+ });
103
+
104
+ test('protocol 3 accepts an image-only private request and rejects another conversation or sender', async t => {
105
+ const requests: GenerateOptions[] = [];
106
+ const { ctx, root, cleanup } = await hostFixture(t, request => { requests.push(request); return textResponse('看到了'); });
107
+ const privateIdentity = { ...identity, conversationType: 'private' as const, conversationId: 'owner' };
108
+ const incoming = join(root, 'incoming');
109
+ const directory = join(incoming, createHash('sha256').update(conversationKey(privateIdentity)).digest('hex'));
110
+ await mkdir(directory, { recursive: true }); await writeFile(join(directory, image.file), png);
111
+ ctx.provide('attachments', { imageLimits: { maxImageBytes: 1024 }, saveImages: async () => [{ attachmentId: AttachmentId(digest), mediaType: 'image/png', bytes: png.length, width: 1, height: 1 }] } as never);
112
+ const agent = (await ctx.agents.create({ sessionId: SessionId('private-api'), agentOptions: { provider: 'test', model: 'test' } })).agent;
113
+ const file = join(root, 'wechat.json');
114
+ const module = createWechatModule({ ctx, file, home: async () => agent, origin: () => 'http://127.0.0.1:1', allowedHosts: [], incomingDir: incoming });
115
+ module.start(); cleanup.push(() => module.stop());
116
+ const server = createServer((req, res) => { void module.route.handler(req, res); });
117
+ await new Promise<void>(resolve => server.listen(0, '127.0.0.1', resolve));
118
+ cleanup.push(() => new Promise<void>(resolve => server.close(() => resolve())));
119
+ const address = server.address(); assert.ok(address && typeof address === 'object');
120
+ const { token, path, protocol } = JSON.parse(await readFile(file, 'utf8')); assert.equal(protocol, 3);
121
+ const post = (media: unknown, text = '') => fetch(`http://127.0.0.1:${address.port}${path}`, { method: 'POST', headers: { 'content-type': 'application/json', 'x-dsh-world-token': token }, body: JSON.stringify({ identity: privateIdentity, text, media }) });
122
+ assert.equal((await post([])).status, 400);
123
+ assert.equal((await post([{ ...previous, identity }])).status, 400);
124
+ assert.equal((await post([{ ...previous, identity: { ...privateIdentity, speakerId: 'wechat:other' } }])).status, 400);
125
+ const response = await post([{ ...previous, identity: privateIdentity }]);
126
+ assert.equal(response.status, 200);
127
+ assert.equal((await response.json()).text, '看到了');
128
+ assert.equal(requests.length, 1);
129
+ assert.ok(requests[0].messages.some(message => message.content.some(block => block.type === 'image')));
130
+ });
@@ -0,0 +1,142 @@
1
+ import assert from 'node:assert/strict';
2
+ import test from 'node:test';
3
+ import { createServer } from 'node:http';
4
+ import { join } from 'node:path';
5
+ import { readFile } from 'node:fs/promises';
6
+ import { createUserMessage, ToolCallId, type StreamChunk } from '@deepseek-ai/dsh-llm';
7
+ import { SessionId } from '@deepseek-ai/dsh-session';
8
+ import { createWechatModule } from '../src/ecosystem/wechat/index.ts';
9
+ import { createWechatRecorder } from '../src/ecosystem/wechat/archive.ts';
10
+ import { createArchiveStore } from '../src/store.ts';
11
+ import { createWorldRuntime } from '../src/runtime.ts';
12
+ import { createWechatSending } from '../src/ecosystem/wechat/sending.ts';
13
+ import { hostFixture, textResponse } from './hostFixture.ts';
14
+
15
+ const chats = [{ id: 'owner', name: '伙伴' }, { id: 'family@chatroom', name: '家庭群' }];
16
+ const registration = { accountId: 'wxid_bot', chats };
17
+ const words = [{ type: 'text', text: '想跟你说句话' }, { type: 'sticker', stickerId: 'st_hi' }, { type: 'text', text: '晚安' }];
18
+ async function* call(name: string, args: object): AsyncIterable<StreamChunk> {
19
+ const block = { type: 'tool-call' as const, id: ToolCallId('proactive'), name, arguments: JSON.stringify(args) };
20
+ yield { type: 'block-start', index: 0, blockType: 'tool-call' };
21
+ yield { type: 'tool-call-delta', index: 0, id: block.id, name, argumentsDelta: block.arguments };
22
+ yield { type: 'block-end', index: 0, block };
23
+ yield { type: 'finish', reason: { kind: 'tool-calls' } };
24
+ }
25
+
26
+ test('a desktop turn can send to either bound chat through persistent tools and one durable queue', async t => {
27
+ let calls = 0;
28
+ const { ctx, root, cleanup } = await hostFixture(t, () => calls++ === 0 ? call('wechat_send', { chatId: 'family@chatroom', messages: words }) : textResponse('这段留在家里'));
29
+ const store = createArchiveStore({ root, worldId: 'test', agentPreset: 'test', timeZone: 'UTC' }); cleanup.push(() => store.close());
30
+ const world = createWorldRuntime(ctx, { worldId: 'test', agentPreset: 'test', timeZone: 'UTC' }, store);
31
+ await world.start(); cleanup.push(() => world.stop());
32
+ const agent = (await ctx.agents.create({ sessionId: SessionId('home'), meta: { agentPreset: 'test' }, agentOptions: { provider: 'test', model: 'test' } })).agent;
33
+ const file = join(root, 'wechat.json');
34
+ const module = createWechatModule({ ctx, file, worldId: 'test', acceptAgent: candidate => candidate.id === agent.id, home: async () => agent, origin: () => 'http://127.0.0.1:1', allowedHosts: [], ...createWechatRecorder(store, 'test') });
35
+ module.start(); cleanup.push(() => module.stop());
36
+ assert.ok(agent.ctx.tools.get('wechat_send', agent), 'sending is available before any WeChat inbound message');
37
+ assert.ok(agent.ctx.tools.get('wechat_chats', agent));
38
+ assert.ok(agent.ctx.tools.get('wechat_delivery', agent));
39
+ const server = createServer((req, res) => { void module.route.handler(req, res); });
40
+ await new Promise<void>(resolve => server.listen(0, '127.0.0.1', resolve));
41
+ cleanup.push(() => new Promise<void>(resolve => server.close(() => resolve())));
42
+ const address = server.address(); assert.ok(address && typeof address === 'object');
43
+ const { token, path } = JSON.parse(await readFile(file, 'utf8'));
44
+ const post = (body: unknown) => fetch(`http://127.0.0.1:${address.port}${path}`, { method: 'POST', headers: { 'content-type': 'application/json', 'x-dsh-world-token': token }, body: JSON.stringify(body) });
45
+ assert.equal(module.status().lastContactAt, null);
46
+ const first = await (await post({ outbox: registration })).json();
47
+ assert.deepEqual(first.settings, { contextMessages: 8, bufferSeconds: 3 }, 'the adapter learns the defaults on its first poll');
48
+ assert.equal(module.status().accountId, 'wxid_bot');
49
+ assert.equal(module.status().account, 'online', 'a successful poll means the account is logged in');
50
+ assert.equal((await post({ status: { wechat: 'offline' } })).status, 200);
51
+ assert.equal(module.status().account, 'offline');
52
+ assert.equal((await post({ status: { wechat: 'maybe' } })).status, 400);
53
+ await post({ outbox: registration });
54
+ assert.equal(module.status().account, 'online');
55
+ assert.equal(module.status().pending, 0);
56
+ assert.ok(module.status().lastContactAt);
57
+ agent.followup(createUserMessage({ source: { kind: 'user' }, content: [{ type: 'text', text: '去跟家人打招呼' }] }));
58
+ await agent.whenIdle(); await ctx.sessions.flush(agent.session); await world.flush();
59
+ const result = await (await post({ outbox: registration })).json();
60
+ assert.equal(module.status().pending, 1, 'a claimed job still counts as waiting');
61
+ assert.equal(result.job.chatId, 'family@chatroom');
62
+ assert.equal(result.job.accountId, 'wxid_bot');
63
+ assert.deepEqual(result.job.messages, words);
64
+ assert.deepEqual((await (await post({ outbox: registration })).json()).job, result.job, 'an unacknowledged claim is returned with the same ID');
65
+ assert.ok(agent.ctx.tools.get('wechat_send', agent), 'the tool survives the end of the turn');
66
+ const record = (await store.readPage({ worldId: 'test', limit: 10 })).records.find(item => item.recordId === result.job.id);
67
+ assert.ok(record);
68
+ assert.match(record.content, /想跟你说句话[\s\S]*st_hi[\s\S]*晚安/);
69
+ assert.doesNotMatch(record.content, /arguments|tool-call/);
70
+ assert.equal(record.speakerId, 'wechat:wxid_bot');
71
+ assert.equal((await post({ receipt: { id: result.job.id, confirmed: 3, status: 'sent' } })).status, 200);
72
+ assert.equal((await (await post({ outbox: registration })).json()).job, undefined);
73
+ const confirmed = (await store.readPage({ worldId: 'test', limit: 10 })).records.find(item => item.recordId === result.job.id)!;
74
+ assert.equal(confirmed.wechatDelivery?.status, 'sent');
75
+ assert.equal((await post({ outbox: { ...registration, accountId: 'other_bot' } })).status, 400);
76
+ });
77
+
78
+ test('an unconfigured target is rejected without queuing or archiving words as sent', async t => {
79
+ let calls = 0;
80
+ const { ctx, root, cleanup } = await hostFixture(t, () => calls++ === 0 ? call('wechat_send', { chatId: 'stranger', messages: words }) : textResponse('没有发出'));
81
+ const agent = (await ctx.agents.create({ sessionId: SessionId('home'), agentOptions: { provider: 'test', model: 'test' } })).agent;
82
+ const sending = createWechatSending({ ctx, file: join(root, 'outbox.sqlite'), worldId: 'test' }); cleanup.push(() => sending.stop());
83
+ for (const tool of sending.tools) cleanup.push(agent.ctx.tools.register(tool));
84
+ await sending.poll(registration);
85
+ assert.ok(agent.ctx.tools.get('wechat_send', agent));
86
+ agent.followup(createUserMessage({ source: { kind: 'user' }, content: [{ type: 'text', text: '主动说话' }] })); await agent.whenIdle();
87
+ const failed = agent.session.snapshotEvents().find(event => event.type === 'tool/result');
88
+ assert.equal(failed?.type === 'tool/result' && failed.data.message.isError, true);
89
+ assert.ok(!agent.session.deriveMessages().some(message => message.source.kind === 'dsh-world-wechat-outgoing'));
90
+ assert.equal(await sending.poll(registration), undefined);
91
+ });
92
+
93
+ test('claimed sends survive a host restart and group-origin sends respect the home-only archive', async t => {
94
+ let calls = 0;
95
+ const { ctx, root, cleanup } = await hostFixture(t, () => calls++ === 0 ? call('wechat_send', { chatId: 'owner', messages: words }) : textResponse('已安排'));
96
+ const config = { root, worldId: 'test', agentPreset: 'test', timeZone: 'UTC', archiveScope: 'home' as const };
97
+ const store = createArchiveStore(config); cleanup.push(() => store.close());
98
+ const isHome = (id: string) => id === 'home';
99
+ const world = createWorldRuntime(ctx, config, store, { prompt: { names: { companion: '鲸鲸', partner: '伙伴' } }, isHome, archiveSession: isHome }); await world.start(); cleanup.push(() => world.stop());
100
+ const agent = (await ctx.agents.create({ sessionId: SessionId('group'), meta: { agentPreset: 'test' }, agentOptions: { provider: 'test', model: 'test' } })).agent;
101
+ const file = join(root, 'outbox.sqlite');
102
+ let sending = createWechatSending({ ctx, file, worldId: 'test', ...createWechatRecorder(store, 'test', isHome) });
103
+ cleanup.push(() => sending.stop());
104
+ for (const tool of sending.tools) cleanup.push(agent.ctx.tools.register(tool));
105
+ await sending.poll(registration);
106
+ agent.followup(createUserMessage({ source: { kind: 'user' }, content: [{ type: 'text', text: '从群会话主动联系伙伴' }] }));
107
+ await agent.whenIdle(); await world.flush();
108
+ const claimed = await sending.poll(registration); assert.ok(claimed);
109
+ assert.equal((await store.readPage({ worldId: 'test', limit: 10 })).records.length, 0);
110
+ assert.match(JSON.stringify(agent.session.snapshotEvents()), /dsh-world-wechat-outgoing/);
111
+ await sending.stop();
112
+ sending = createWechatSending({ ctx, file, worldId: 'test', ...createWechatRecorder(store, 'test', isHome) });
113
+ assert.deepEqual(await sending.poll(registration), claimed);
114
+ await sending.receipt({ id: claimed.id, confirmed: 3, status: 'sent' });
115
+ assert.equal(await sending.poll(registration), undefined);
116
+ assert.equal(store.delivery(claimed.id)?.status, 'sent');
117
+ assert.equal((await store.readPage({ worldId: 'test', limit: 10 })).records.length, 0);
118
+ await world.stop();
119
+ const expanded = createWorldRuntime(ctx, { ...config, archiveScope: 'workspace' }, store, { prompt: { names: { companion: '鲸鲸', partner: '伙伴' } }, isHome, archiveSession: () => true });
120
+ await expanded.start(); cleanup.push(() => expanded.stop()); await expanded.flush();
121
+ let recovered = (await store.readPage({ worldId: 'test', limit: 10 })).records.find(record => record.recordId === claimed.id);
122
+ assert.equal(recovered?.wechatDelivery?.status, 'sent', 'scope expansion preserves the receipt already confirmed outside shared memory');
123
+ await sending.receipt({ id: claimed.id, confirmed: 3, status: 'sent' });
124
+ recovered = (await store.readPage({ worldId: 'test', limit: 10 })).records.find(record => record.recordId === claimed.id);
125
+ assert.equal(recovered?.wechatDelivery?.confirmed, 3);
126
+ });
127
+
128
+ test('a send never becomes deliverable when its originating session cannot be flushed', async t => {
129
+ let calls = 0;
130
+ const { ctx, root, cleanup } = await hostFixture(t, () => calls++ === 0 ? call('wechat_send', { chatId: 'owner', messages: words }) : textResponse('留在家里'));
131
+ const agent = (await ctx.agents.create({ sessionId: SessionId('home'), agentOptions: { provider: 'test', model: 'test' } })).agent;
132
+ const sending = createWechatSending({ ctx, file: join(root, 'outbox.sqlite'), worldId: 'test' }); cleanup.push(() => sending.stop());
133
+ for (const tool of sending.tools) cleanup.push(agent.ctx.tools.register(tool));
134
+ await sending.poll(registration);
135
+ const off = ctx.on('session/flush', async session => {
136
+ if (session.id === agent.id && session.snapshotEvents().some(event => event.type === 'user/message' && event.data.source.kind === 'dsh-world-wechat-outgoing')) throw new Error('source disk failure');
137
+ });
138
+ try {
139
+ agent.followup(createUserMessage({ source: { kind: 'user' }, content: [{ type: 'text', text: '试着发送' }] })); await agent.whenIdle();
140
+ assert.equal(await sending.poll(registration), undefined);
141
+ } finally { off(); }
142
+ });
@@ -0,0 +1,91 @@
1
+ import assert from 'node:assert/strict';
2
+ import test from 'node:test';
3
+ import { EventEmitter } from 'node:events';
4
+ import { mkdtempSync, rmSync, writeFileSync, readdirSync } from 'node:fs';
5
+ import { tmpdir } from 'node:os';
6
+ import { join } from 'node:path';
7
+ import type { ChildProcess } from 'node:child_process';
8
+ import { createAdapterSupervisor } from '../src/ecosystem/wechat/supervisor.ts';
9
+
10
+ class FakeChild extends EventEmitter { pid: number; constructor(pid: number) { super(); this.pid = pid; } }
11
+ function setup(t: test.TestContext) {
12
+ const dir = mkdtempSync(join(tmpdir(), 'dsh-world-supervisor-')); t.after(() => rmSync(dir, { recursive: true, force: true }));
13
+ const python = join(dir, 'python.exe'), config = join(dir, 'config.json'), script = join(dir, 'adapter.py');
14
+ for (const file of [python, config, script]) writeFileSync(file, '');
15
+ const children: FakeChild[] = [], launches: { command: string; args: string[] }[] = [], logs: string[] = [], killed: number[] = [];
16
+ const supervisor = createAdapterSupervisor({
17
+ python, config, script, logFile: join(dir, 'adapter.log'), minDelayMs: 5, maxDelayMs: 20, log: message => logs.push(message),
18
+ spawnProcess: ((command: string, args: string[]) => { launches.push({ command, args }); const child = new FakeChild(100 + children.length); children.push(child); return child; }) as never,
19
+ killTree: (child: ChildProcess) => { killed.push(child.pid!); queueMicrotask(() => child.emit('exit', null, 'SIGKILL')); },
20
+ });
21
+ return { dir, python, config, script, children, launches, logs, killed, supervisor };
22
+ }
23
+ const wait = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));
24
+
25
+ test('the adapter starts with the bound config and an explicit send permission, and stops with its whole tree', async t => {
26
+ const { supervisor, launches, python, config, script, children, killed } = setup(t);
27
+ supervisor.start();
28
+ assert.equal(launches.length, 1);
29
+ assert.equal(launches[0]!.command, python);
30
+ assert.deepEqual(launches[0]!.args, ['-X', 'utf8', '-u', script, 'watch', '--config', config, '--allow-send']);
31
+ assert.deepEqual(supervisor.status(), { running: true, pid: 100, restarts: 0, lastExit: null, error: null, needsHuman: false });
32
+ await supervisor.stop();
33
+ assert.deepEqual(killed, [children[0]!.pid]);
34
+ assert.equal(supervisor.status().running, false);
35
+ await wait(40);
36
+ assert.equal(launches.length, 1, 'a stopped supervisor never restarts it');
37
+ });
38
+
39
+ test('an exit restarts the adapter after a delay and keeps count', async t => {
40
+ const { supervisor, launches, children, logs } = setup(t);
41
+ supervisor.start();
42
+ children[0]!.emit('exit', 1, null);
43
+ assert.equal(supervisor.status().running, false);
44
+ assert.equal(supervisor.status().lastExit, 'code 1');
45
+ assert.equal(launches.length, 1, 'not at once');
46
+ await wait(60);
47
+ assert.equal(launches.length, 2);
48
+ assert.equal(supervisor.status().restarts, 1);
49
+ assert.match(logs[0]!, /exited \(code 1\)/);
50
+ await supervisor.stop();
51
+ });
52
+
53
+ test('a file that is missing at start is retried and the adapter comes up once it is back', async t => {
54
+ const { supervisor, launches, python, dir } = setup(t);
55
+ rmSync(python);
56
+ supervisor.start();
57
+ assert.equal(launches.length, 0);
58
+ assert.match(supervisor.status().error ?? '', /Python不存在/);
59
+ writeFileSync(python, '');
60
+ await wait(60);
61
+ assert.equal(launches.length, 1);
62
+ assert.equal(supervisor.status().error, null);
63
+ assert.equal(supervisor.status().restarts, 0, 'waiting for a file is not a restart');
64
+ await supervisor.stop();
65
+ assert.ok(readdirSync(dir).includes('config.json'));
66
+ });
67
+
68
+ test('an exit that asks for a person is not retried until the console restarts it', async t => {
69
+ const { supervisor, launches, children } = setup(t);
70
+ supervisor.start();
71
+ children[0]!.emit('exit', 2, null);
72
+ assert.equal(supervisor.status().needsHuman, true);
73
+ await wait(60);
74
+ assert.equal(launches.length, 1, 'retrying cannot help');
75
+ await supervisor.restart();
76
+ assert.equal(launches.length, 2);
77
+ assert.equal(supervisor.status().needsHuman, false);
78
+ assert.equal(supervisor.status().running, true);
79
+ await supervisor.stop();
80
+ });
81
+
82
+ test('restarting a running adapter replaces it once, without a second automatic restart', async t => {
83
+ const { supervisor, launches, killed, children } = setup(t);
84
+ supervisor.start();
85
+ await supervisor.restart();
86
+ assert.deepEqual(killed, [children[0]!.pid]);
87
+ await wait(60);
88
+ assert.equal(launches.length, 2);
89
+ assert.equal(supervisor.status().restarts, 1);
90
+ await supervisor.stop();
91
+ });