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,775 @@
1
+ /**
2
+ * dsh-agent-md —— 宿主半:人格文件服务 + 系统提示注入 + 同源 HTTP API。
3
+ *
4
+ * 注入机制(关键):
5
+ * `systemPrompt.context({ text: (context) => ... })` 的 **text 是函数**,每次装配
6
+ * 提示词都会被调用,并且拿到 `context.agent`;`context.agent.session.header.cwd`
7
+ * 就是该会话的工作目录。于是「同一个 DSH 里,不同项目不同人格」是天然成立的——
8
+ * 不需要任何会话钩子,也没有缓存过期问题。
9
+ *
10
+ * 渲染语义:DSH 把 context 段materialize 成一条 user 角色尾消息,且**只在渲染
11
+ * 文本变化时**重新追加 —— 所以改完 AGENT.md 会在下一个回合生效,而稳定前缀
12
+ * (系统提示 + 历史)的缓存不会被无谓打断。
13
+ *
14
+ * 本文件只 import node: 内置模块与同目录 ./persona.js(本地 link: 安装时 Node
15
+ * 以真实路径解析,裸包名会解析失败)。
16
+ */
17
+
18
+ import { spawn } from 'node:child_process'
19
+ import { appendFileSync, promises as fs } from 'node:fs'
20
+ import path from 'node:path'
21
+
22
+ import {
23
+ MAX_FILE_BYTES,
24
+ agentsRoot,
25
+ buildTemplate,
26
+ composePersona,
27
+ displayNameFor,
28
+ ensureDirs,
29
+ fileInfo,
30
+ globalPath,
31
+ isEffectivelyEmpty,
32
+ listProjects,
33
+ listTemplates,
34
+ listWorkspaces,
35
+ normalizePathKey,
36
+ normalizeText,
37
+ projectsDir,
38
+ readTextFile,
39
+ registerProject,
40
+ resolveProjectPersona,
41
+ slugFor,
42
+ statePath,
43
+ writeTextFile,
44
+ } from './persona.js'
45
+ import { bindQqPeer, parsePeerKey, qqStatus, unbindQqPeer } from './qq.js'
46
+
47
+ export const name = 'agent-md'
48
+
49
+ /**
50
+ * 声明式依赖:只有 systemPrompt(注入)是必需的。
51
+ * webServer 走可选读取(ctx.get)——它只服务「设置页的同源 API」,在 headless
52
+ * 宿主(QQ / 微信 / TUI 这类没有网页的 profile)里根本不注册这个服务;把它写成
53
+ * 必需依赖会让整个插件在那些 profile 里一直挂在「等待服务」状态,注入也不生效。
54
+ * sessions 同样走可选读取:它用于反向解析「某个会话的工作目录」,
55
+ * 缺失时设置页退化成「只能编辑已登记项目」,注入本身完全不受影响。
56
+ */
57
+ export const inject = ['systemPrompt']
58
+
59
+ /** 同源 API 前缀(浏览器半侧 fetch 它)。 */
60
+ const API_PREFIX = '/api/agent-md'
61
+
62
+ /** 默认配置。 */
63
+ const DEFAULT_CONFIG = {
64
+ enabled: true,
65
+ includeGlobal: false,
66
+ projectPersonaEnabled: true,
67
+ localNames: ['AGENT.md'],
68
+ showInSettings: true,
69
+ }
70
+
71
+ /** 内存态配置(启动时从 statePath() 读回)。 */
72
+ let config = { ...DEFAULT_CONFIG }
73
+
74
+ /**
75
+ * 浏览器半侧的握手回执。
76
+ *
77
+ * 页面一打开设置页就会 fetch `/state`,所以「最近一次 /state 请求」就是
78
+ * 「客户端 bundle 真的被服务给页面、并且页面真的连上了宿主」的硬证据。
79
+ * 这既是我交付时的自检信号,也是用户排障时的第一手线索:面板顶部会据此
80
+ * 显示「本页已连接宿主」,页面没连上时能一眼看出来。
81
+ */
82
+ const bridge = { lastSeen: null, count: 0, userAgent: '', build: 'v0.2.0' }
83
+
84
+ /**
85
+ * 注入记录:每次装配系统提示词时,把人数组件**实际吐出的内容**记一行。
86
+ *
87
+ * 「人格到底生效了没有」是这份插件最容易被误解的问题,而它唯一的硬证据就是这次
88
+ * 装配实际注入了什么。记录写在 `<base>/inject-log.jsonl`(文本没变就不重复记),
89
+ * 设置页显示最后几条——排障不用再靠猜。
90
+ */
91
+ const injectLog = []
92
+ let injectLogLast = ''
93
+
94
+ function injectLogPath() {
95
+ return path.join(agentsRoot(), 'inject-log.jsonl')
96
+ }
97
+
98
+ function recordInjection(entry) {
99
+ try {
100
+ const line = JSON.stringify(entry)
101
+ if (line === injectLogLast) return
102
+ injectLogLast = line
103
+ injectLog.push(entry)
104
+ while (injectLog.length > 20) injectLog.shift()
105
+ appendFileSync(injectLogPath(), `${line}\n`, 'utf8')
106
+ const raw = readTextFile(injectLogPath())
107
+ if (raw !== null) {
108
+ const lines = raw.split('\n').filter((l) => l.trim() !== '')
109
+ if (lines.length > 200) writeTextFile(injectLogPath(), `${lines.slice(-100).join('\n')}\n`)
110
+ }
111
+ } catch {
112
+ // 记录失败绝不能影响装配
113
+ }
114
+ }
115
+
116
+ /** 读配置(失败回默认:坏掉的配置不能让插件整个失效)。 */
117
+ function loadConfig() {
118
+ const raw = readTextFile(statePath())
119
+ if (raw === null) return
120
+ try {
121
+ const parsed = JSON.parse(raw)
122
+ if (parsed === null || typeof parsed !== 'object') return
123
+ if (typeof parsed.enabled === 'boolean') config.enabled = parsed.enabled
124
+ // 全局人格已移除:无论旧配置写什么,都强制不注入。
125
+ config.includeGlobal = false
126
+ if (typeof parsed.projectPersonaEnabled === 'boolean') config.projectPersonaEnabled = parsed.projectPersonaEnabled
127
+ if (typeof parsed.showInSettings === 'boolean') config.showInSettings = parsed.showInSettings
128
+ if (Array.isArray(parsed.localNames)) {
129
+ const names = parsed.localNames.filter((n) => typeof n === 'string' && n.trim() !== '' && !n.includes('/') && !n.includes('\\') && !n.includes('..'))
130
+ if (names.length > 0) config.localNames = names.slice(0, 5)
131
+ }
132
+ } catch {
133
+ // 损坏 → 保持默认
134
+ }
135
+ }
136
+
137
+ /** 写配置(原子写)。 */
138
+ async function persistConfig() {
139
+ try {
140
+ await fs.mkdir(path.dirname(statePath()), { recursive: true })
141
+ const tmp = `${statePath()}.tmp-${process.pid}`
142
+ await fs.writeFile(tmp, `${JSON.stringify(config, null, 2)}\n`, 'utf8')
143
+ await fs.rename(tmp, statePath())
144
+ } catch (error) {
145
+ console.error(`[agent-md] 配置写入失败: ${String((error && error.message) || error)}`)
146
+ }
147
+ }
148
+
149
+ /** 在系统文件管理器中选中一个文件(跨平台)。 */
150
+ function openInFileManager(filePath) {
151
+ const dir = path.dirname(filePath)
152
+ const plat = process.platform
153
+ const cmd = plat === 'win32' ? 'explorer' : plat === 'darwin' ? 'open' : 'xdg-open'
154
+ const args = plat === 'win32'
155
+ ? ['/select,', filePath]
156
+ : plat === 'darwin'
157
+ ? ['-R', filePath]
158
+ : [dir]
159
+ return new Promise((resolve, reject) => {
160
+ const child = spawn(cmd, args, { detached: true, stdio: 'ignore' })
161
+ const timer = setTimeout(() => {
162
+ try {
163
+ child.kill()
164
+ } catch {
165
+ // 已经退出
166
+ }
167
+ resolve()
168
+ }, 3000)
169
+ child.on('error', (err) => {
170
+ clearTimeout(timer)
171
+ reject(err)
172
+ })
173
+ child.on('exit', () => {
174
+ clearTimeout(timer)
175
+ resolve()
176
+ })
177
+ })
178
+ }
179
+
180
+ /**
181
+ * 弹一个系统「选择文件夹」对话框,返回选中的目录(取消返回空串)。
182
+ *
183
+ * 为什么要它:设置页里「项目人格」原本只能从**登记过的**工作区里挑,用户想让
184
+ * 任意目录有自己的人格时无路可走。浏览器拿不到原生目录选择器,所以这一步由宿主
185
+ * (Node 侧)代劳。三条平台分支各用系统自带工具,没有额外依赖。
186
+ */
187
+ function pickFolder() {
188
+ return new Promise((resolve, reject) => {
189
+ const plat = process.platform
190
+ let cmd
191
+ let args
192
+ if (plat === 'win32') {
193
+ cmd = 'powershell'
194
+ args = [
195
+ '-NoProfile',
196
+ '-STA',
197
+ '-Command',
198
+ // ① [Console]::OutputEncoding 必须先设成 UTF-8:否则中文路径(例 D:\肥鱼)会以
199
+ // 控制台代码页(CP936)吐出来,Node 侧按 UTF-8 解码就变成乱码。
200
+ // ② -STA 是 WinForms 对话框的要求,缺了会直接抛。
201
+ "[Console]::OutputEncoding = [System.Text.Encoding]::UTF8; Add-Type -AssemblyName System.Windows.Forms | Out-Null; $d = New-Object System.Windows.Forms.FolderBrowserDialog; $d.Description = '选择要用项目人格的工作目录'; $d.ShowNewFolderButton = $true; if ($d.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK) { [Console]::Out.Write($d.SelectedPath) }",
202
+ ]
203
+ } else if (plat === 'darwin') {
204
+ cmd = 'osascript'
205
+ args = ['-e', 'POSIX path of (choose folder with prompt "选择要用项目人格的工作目录")']
206
+ } else {
207
+ cmd = 'zenity'
208
+ args = ['--file-selection', '--directory', '--title=选择要用项目人格的工作目录']
209
+ }
210
+ let child
211
+ try {
212
+ child = spawn(cmd, args, { windowsHide: true })
213
+ } catch (error) {
214
+ reject(error)
215
+ return
216
+ }
217
+ let out = ''
218
+ child.stdout?.on('data', (chunk) => {
219
+ out += String(chunk)
220
+ })
221
+ child.on('error', reject)
222
+ child.on('exit', () => {
223
+ resolve(out.trim())
224
+ })
225
+ })
226
+ }
227
+
228
+ /** JSON 响应。 */function sendJson(res, status, body) {
229
+ const text = JSON.stringify(body)
230
+ const bytes = Buffer.from(text, 'utf8')
231
+ res.writeHead(status, {
232
+ 'content-type': 'application/json; charset=utf-8',
233
+ 'content-length': String(bytes.length),
234
+ 'cache-control': 'no-store',
235
+ })
236
+ res.end(bytes)
237
+ }
238
+
239
+ /** 读 JSON 请求体(上限略高于单文件上限,留出 JSON 包装的余量)。 */
240
+ function readBody(req, maxBytes = MAX_FILE_BYTES + 64 * 1024) {
241
+ return new Promise((resolve, reject) => {
242
+ const chunks = []
243
+ let size = 0
244
+ req.on('data', (chunk) => {
245
+ size += chunk.length
246
+ if (size > maxBytes) {
247
+ req.destroy()
248
+ reject(new Error('请求体过大'))
249
+ return
250
+ }
251
+ chunks.push(chunk)
252
+ })
253
+ req.on('end', () => {
254
+ try {
255
+ const text = Buffer.concat(chunks).toString('utf8')
256
+ resolve(text.trim() === '' ? {} : JSON.parse(text))
257
+ } catch (error) {
258
+ reject(error)
259
+ }
260
+ })
261
+ req.on('error', reject)
262
+ })
263
+ }
264
+
265
+ /** 从 URL 里取 query 参数。 */
266
+ function queryOf(req) {
267
+ try {
268
+ const url = new URL(req.url ?? '/', 'http://localhost')
269
+ return url.searchParams
270
+ } catch {
271
+ return new URLSearchParams()
272
+ }
273
+ }
274
+
275
+ /** 一个人格文件的完整状态(含正文,供设置页编辑器使用)。 */
276
+ function personaFileDetail(file, label) {
277
+ const raw = readTextFile(file)
278
+ return {
279
+ ...fileInfo(file, label),
280
+ content: raw === null ? '' : normalizeText(raw),
281
+ }
282
+ }
283
+
284
+ export function apply(ctx) {
285
+ ensureDirs()
286
+ loadConfig()
287
+
288
+ /** 会话 id → 工作目录(会话服务缺失时返回空串)。 */
289
+ const cwdOfSession = (sessionId) => {
290
+ if (typeof sessionId !== 'string' || sessionId === '') return ''
291
+ const sessions = typeof ctx.get === 'function' ? ctx.get('sessions') : ctx.sessions
292
+ if (sessions === undefined || sessions === null || typeof sessions.get !== 'function') return ''
293
+ try {
294
+ const session = sessions.get(sessionId)
295
+ const cwd = session?.header?.cwd
296
+ return typeof cwd === 'string' ? cwd : ''
297
+ } catch {
298
+ return ''
299
+ }
300
+ }
301
+
302
+ /**
303
+ * 会话服务里「最近活跃的那个会话」。
304
+ *
305
+ * 设置页弹层是全局的,某些 DSH 版本不给 `settings.section` 传 sessionId,
306
+ * 于是「当前工作目录」解析成空、面板直接死路。这里退一步:从会话服务里挑一个
307
+ * 最近活跃且有 cwd 的会话,作为**兜底**(面板会标明这是兜底值,不是当前会话)。
308
+ */
309
+ const recentSession = () => {
310
+ const sessions = typeof ctx.get === 'function' ? ctx.get('sessions') : ctx.sessions
311
+ if (sessions === null || sessions === undefined || typeof sessions.list !== 'function') return null
312
+ try {
313
+ const list = sessions.list()
314
+ if (!Array.isArray(list)) return null
315
+ let best = null
316
+ for (const session of list) {
317
+ const cwd = session?.header?.cwd
318
+ if (typeof cwd !== 'string' || cwd === '') continue
319
+ const stamp = String(session?.header?.updatedAt ?? session?.header?.createdAt ?? session?.updatedAt ?? session?.createdAt ?? '')
320
+ if (best === null || stamp > best.stamp) {
321
+ best = { stamp, cwd, id: String(session?.header?.id ?? session?.id ?? '') }
322
+ }
323
+ }
324
+ return best
325
+ } catch {
326
+ return null
327
+ }
328
+ }
329
+
330
+ /**
331
+ * 把「这一回合实际会注入什么」说成人话。
332
+ *
333
+ * ⚠️ 早先的设置页横幅只看 `current.resolved`(项目人格解析结果),于是「只写了
334
+ * 全局人格」的用户会看到「尚未写入任何人格」——明明写了、也确实注入了。这里把
335
+ * 全局与项目一起算,并且把「两个开关都关着」这种配置错误直说。
336
+ */
337
+ const describeEffective = (composed, cwd) => {
338
+ if (!config.enabled) return { kind: 'off', label: '总开关已关闭 · 人格未注入' }
339
+ const sources = Array.isArray(composed.sources) ? composed.sources : []
340
+ if (sources.length === 0) {
341
+ let why = '还没有写过人格(写一份即生效)'
342
+ if (!config.includeGlobal && !config.projectPersonaEnabled) {
343
+ why = '两个开关都关着(全局人格一起注入 / 启用项目人格)——当前不会注入任何人格'
344
+ } else if (cwd === '') {
345
+ why = '没解析到工作目录,且全局人格是空的'
346
+ } else if (!config.includeGlobal) {
347
+ why = '已关闭全局人格注入,且这个工作区没有项目人格'
348
+ } else if (!config.projectPersonaEnabled) {
349
+ why = '已关闭项目人格,且全局人格是空的'
350
+ }
351
+ return { kind: 'none', label: why }
352
+ }
353
+ const head = sources[0]
354
+ const kind = head.label === '项目' ? (head.kind === 'local' ? 'local' : 'project') : 'global'
355
+ const name = kind === 'local' ? '项目内 AGENT.md' : kind === 'project' ? '项目人格' : '全局人格'
356
+ return {
357
+ kind,
358
+ chars: composed.text.length,
359
+ sources,
360
+ label: `${name} · ${head.path}(${head.chars} 字)`,
361
+ extra: sources.slice(1).map((s) => (s.label === '全局' ? '+(叠加)全局人格' : '+(叠加)项目人格')),
362
+ }
363
+ }
364
+
365
+ // ---- 1. 人格注入 ------------------------------------------------------
366
+ ctx.effect(() => {
367
+ try {
368
+ return ctx.systemPrompt.context({
369
+ name: 'agent-md:persona',
370
+ // 200 段之后(VCP 视觉协议 200 之后、记忆快照之后):人格是「你是谁」,
371
+ // 放在能力说明之后更贴近用户消息,同时不与既有段抢位。
372
+ order: 260,
373
+ text: (context) => {
374
+ if (!config.enabled) return ''
375
+ const cwd = context?.agent?.session?.header?.cwd
376
+ const resolvedCwd = typeof cwd === 'string' ? cwd : ''
377
+ const composed = composePersona({ cwd: resolvedCwd, config })
378
+ // 留痕:这一回合到底注入了什么(文本没变就不重复记)
379
+ if (composed.text !== '') {
380
+ recordInjection({ at: new Date().toISOString(), cwd: resolvedCwd, chars: composed.text.length, sources: composed.sources })
381
+ }
382
+ return composed.text
383
+ },
384
+ })
385
+ } catch (error) {
386
+ // 幂等保护:DSH 的 loader 曾把插件在无 scope 标签的 ctx 上装配两次,
387
+ // 同名重复注册会抛 "already registered";命中时跳过,别连带 API 一起失效。
388
+ if (error instanceof Error && error.message.includes('already registered')) {
389
+ console.warn('[agent-md] agent-md:persona 已注册,跳过重复注册')
390
+ return () => {}
391
+ }
392
+ throw error
393
+ }
394
+ }, 'dsh-agent-md: persona context')
395
+
396
+ // ---- 2. 同源 API(仅网页宿主有 webServer;headless 宿主跳过) ------------
397
+ // webServer 是可选依赖:它只服务设置页。cordis 里没写进 inject 的服务,ctx.get 与
398
+ // ctx.webServer 都拿不到(回 undefined / 抛异常),所以只能走 ctx.inject 延迟注册——
399
+ // 服务在这个宿主里永远不出现时,回调根本不会执行,插件照常只做人格注入。
400
+ const apiRoute = {
401
+ kind: 'prefix',
402
+ path: API_PREFIX,
403
+ handler: async (req, res) => {
404
+ const route = (req.url ?? '').split('?')[0].slice(API_PREFIX.length) || '/'
405
+ try {
406
+ if (route === '/state' && req.method === 'GET') {
407
+ // 记录这次握手:这是「页面真的连上了」的证据(自检与排障都用它)
408
+ bridge.lastSeen = new Date().toISOString()
409
+ bridge.count += 1
410
+ const ua = req.headers !== undefined && req.headers !== null ? req.headers['user-agent'] : ''
411
+ if (typeof ua === 'string' && ua !== '') bridge.userAgent = ua.slice(0, 160)
412
+
413
+ const query = queryOf(req)
414
+ let sessionId = query.get('session') ?? ''
415
+ let cwd = cwdOfSession(sessionId)
416
+ let cwdSource = cwd === '' ? 'none' : 'session'
417
+ // 会话不可解析时允许显式传 path(设置页里手选一个项目)
418
+ if (cwd === '') {
419
+ const explicit = query.get('path') ?? ''
420
+ if (explicit !== '') {
421
+ cwd = explicit
422
+ cwdSource = 'path'
423
+ }
424
+ }
425
+ // 再退一步:用「最近活跃会话」的工作目录兜底,别让面板变成死路
426
+ if (cwd === '') {
427
+ const recent = recentSession()
428
+ if (recent !== null && recent !== undefined) {
429
+ cwd = recent.cwd
430
+ cwdSource = 'recent'
431
+ if (sessionId === '') sessionId = recent.id
432
+ }
433
+ }
434
+ if (cwd !== '') registerProject(cwd, { active: true })
435
+ const resolved = cwd === '' ? null : resolveProjectPersona(cwd, config.localNames)
436
+ const stored = cwd === '' ? null : slugFor(cwd)
437
+ const composed = composePersona({ cwd, config })
438
+ return sendJson(res, 200, {
439
+ enabled: config.enabled,
440
+ includeGlobal: config.includeGlobal,
441
+ projectPersonaEnabled: config.projectPersonaEnabled,
442
+ localNames: config.localNames,
443
+ showInSettings: config.showInSettings,
444
+ baseDir: agentsRoot(),
445
+ statePath: statePath(),
446
+ global: personaFileDetail(globalPath(), '全局人格'),
447
+ // 「这一回合实际会注入什么」的结论(全局 + 项目一起算)
448
+ effective: describeEffective(composed, cwd),
449
+ bridge: { lastSeen: bridge.lastSeen, count: bridge.count, build: bridge.build },
450
+ current: {
451
+ sessionId,
452
+ cwd,
453
+ cwdSource,
454
+ displayName: cwd === '' ? '' : displayNameFor(cwd),
455
+ projectFile: stored === null ? '' : stored.path,
456
+ projectFileExists: stored === null ? false : readTextFile(stored.path) !== null,
457
+ projectContent: stored === null ? '' : normalizeText(readTextFile(stored.path) ?? ''),
458
+ resolved: resolved === null
459
+ ? null
460
+ : { kind: resolved.kind, path: resolved.path, chars: normalizeText(resolved.content).trim().length },
461
+ },
462
+ projects: listProjects(),
463
+ // 本机所有工作区(不只是登记过的):设置页的项目下拉用它
464
+ workspaces: listWorkspaces(),
465
+ injectLog: injectLog.slice(-5).reverse(),
466
+ templates: listTemplates(),
467
+ // QQ 网关联动:这个进程有没有加载网关、记下了哪些 QQ 账号、绑到了哪个对话
468
+ qq: qqStatus(ctx),
469
+ })
470
+ }
471
+
472
+ if (route === '/save' && req.method === 'POST') {
473
+ const body = await readBody(req)
474
+ const scope = String(body.scope ?? '')
475
+ const mode = String(body.mode ?? 'save')
476
+ const content = typeof body.content === 'string' ? body.content : ''
477
+
478
+ /** 写一个受管人格文件(带 .bak 备份)。 */
479
+ const writeManaged = async (file) => {
480
+ if (Buffer.byteLength(content, 'utf8') > MAX_FILE_BYTES) {
481
+ throw Object.assign(new Error(`内容超过上限 ${Math.round(MAX_FILE_BYTES / 1024)} KB`), { status: 413 })
482
+ }
483
+ const normalized = normalizeText(content)
484
+ if (mode === 'delete') {
485
+ try {
486
+ await fs.unlink(file)
487
+ } catch {
488
+ // 本来就不存在
489
+ }
490
+ return
491
+ }
492
+ if (mode === 'revert') {
493
+ try {
494
+ await fs.writeFile(file, '', 'utf8')
495
+ } catch {
496
+ // 文件不存在即已是空
497
+ }
498
+ return
499
+ }
500
+ // 备份上一次内容(丢了也能救回来;只留一份 .bak)
501
+ const previous = readTextFile(file)
502
+ if (previous !== null && previous.trim() !== '') {
503
+ try {
504
+ await fs.mkdir(path.dirname(file), { recursive: true })
505
+ await fs.writeFile(`${file}.bak`, previous, 'utf8')
506
+ } catch {
507
+ // 备份失败不阻断保存
508
+ }
509
+ }
510
+ await fs.mkdir(path.dirname(file), { recursive: true })
511
+ const tmp = `${file}.tmp-${process.pid}`
512
+ await fs.writeFile(tmp, normalized, 'utf8')
513
+ await fs.rename(tmp, file)
514
+ }
515
+
516
+ /** 删掉项目内 AGENT.md(只允许删除我们探测到的那份)。 */
517
+ const removeLocal = async (cwd) => {
518
+ const names = config.localNames
519
+ for (const n of names) {
520
+ const candidate = path.join(cwd, n)
521
+ if (readTextFile(candidate) !== null) {
522
+ await fs.unlink(candidate)
523
+ return candidate
524
+ }
525
+ }
526
+ throw Object.assign(new Error('项目内没有找到可删除的 AGENT.md'), { status: 404 })
527
+ }
528
+
529
+ if (scope === 'global') {
530
+ return sendJson(res, 410, { error: '全局人格已移除;只支持项目人格' })
531
+ }
532
+
533
+ if (scope === 'project') {
534
+ const cwd = String(body.cwd ?? '').trim()
535
+ const kind = String(body.kind ?? 'stored')
536
+ if (cwd === '') throw Object.assign(new Error('缺少 cwd'), { status: 400 })
537
+ registerProject(cwd, { active: true })
538
+ if (kind === 'local') {
539
+ if (mode === 'revert' || mode === 'delete') {
540
+ const deleted = await removeLocal(cwd)
541
+ return sendJson(res, 200, { saved: true, file: deleted, scope, kind, deleted: true })
542
+ }
543
+ const target = path.join(cwd, config.localNames[0])
544
+ if (Buffer.byteLength(content, 'utf8') > MAX_FILE_BYTES) {
545
+ throw Object.assign(new Error(`内容超过上限 ${Math.round(MAX_FILE_BYTES / 1024)} KB`), { status: 413 })
546
+ }
547
+ const previous = readTextFile(target)
548
+ if (previous !== null && previous.trim() !== '') {
549
+ try {
550
+ await fs.writeFile(`${target}.bak`, previous, 'utf8')
551
+ } catch {
552
+ // 忽略
553
+ }
554
+ }
555
+ await fs.mkdir(path.dirname(target), { recursive: true })
556
+ const tmpLocal = `${target}.tmp-${process.pid}`
557
+ await fs.writeFile(tmpLocal, normalizeText(content), 'utf8')
558
+ await fs.rename(tmpLocal, target)
559
+ return sendJson(res, 200, { saved: true, file: target, scope, kind })
560
+ }
561
+ const file = slugFor(cwd).path
562
+ await writeManaged(file)
563
+ return sendJson(res, 200, { saved: true, file, scope, kind: 'stored' })
564
+ }
565
+
566
+ if (scope === 'template') {
567
+ const id = String(body.id ?? '').replace(/[^0-9a-zA-Z\u4e00-\u9fa5._-]/g, '')
568
+ if (id === '') throw Object.assign(new Error('缺少模板 id'), { status: 400 })
569
+ const file = path.join(agentsRoot(), 'templates', `${id}.md`)
570
+ await writeManaged(file)
571
+ return sendJson(res, 200, { saved: true, file, scope })
572
+ }
573
+
574
+ throw Object.assign(new Error(`未知 scope: ${scope}`), { status: 400 })
575
+ }
576
+
577
+ if (route === '/create' && req.method === 'POST') {
578
+ const body = await readBody(req)
579
+ const scope = String(body.scope ?? 'global')
580
+ const overwrite = body.overwrite === true
581
+ if (scope === 'global') {
582
+ return sendJson(res, 410, { error: '全局人格已移除;只支持项目人格' })
583
+ }
584
+ if (scope === 'project') {
585
+ const cwd = String(body.cwd ?? '').trim()
586
+ if (cwd === '') throw Object.assign(new Error('缺少 cwd'), { status: 400 })
587
+ registerProject(cwd, { active: true })
588
+ const kind = String(body.kind ?? 'stored')
589
+ const file = kind === 'local' ? path.join(cwd, config.localNames[0]) : slugFor(cwd).path
590
+ if (!overwrite && readTextFile(file) !== null && !isEffectivelyEmpty(readTextFile(file) ?? '')) {
591
+ return sendJson(res, 200, { created: false, reason: 'exists', file })
592
+ }
593
+ writeTextFile(file, buildTemplate('project', displayNameFor(cwd)))
594
+ return sendJson(res, 200, { created: true, file, kind })
595
+ }
596
+ if (scope === 'template') {
597
+ const id = String(body.id ?? '').trim().replace(/[^0-9a-zA-Z\u4e00-\u9fa5._-]/g, '')
598
+ if (id === '') throw Object.assign(new Error('缺少模板名'), { status: 400 })
599
+ const file = path.join(agentsRoot(), 'templates', `${id}.md`)
600
+ if (!overwrite && readTextFile(file) !== null) {
601
+ return sendJson(res, 200, { created: false, reason: 'exists', file })
602
+ }
603
+ writeTextFile(file, buildTemplate('project', id))
604
+ return sendJson(res, 200, { created: true, file })
605
+ }
606
+ throw Object.assign(new Error(`未知 scope: ${scope}`), { status: 400 })
607
+ }
608
+
609
+ if (route === '/config' && req.method === 'POST') {
610
+ const body = await readBody(req)
611
+ if (typeof body.enabled === 'boolean') config.enabled = body.enabled
612
+ // 全局人格已移除:忽略外部的 includeGlobal 写入。
613
+ config.includeGlobal = false
614
+ if (typeof body.projectPersonaEnabled === 'boolean') config.projectPersonaEnabled = body.projectPersonaEnabled
615
+ if (typeof body.showInSettings === 'boolean') config.showInSettings = body.showInSettings
616
+ if (Array.isArray(body.localNames)) {
617
+ const names = body.localNames
618
+ .filter((n) => typeof n === 'string' && n.trim() !== '' && !n.includes('/') && !n.includes('\\') && !n.includes('..'))
619
+ .map((n) => n.trim())
620
+ config.localNames = names.length > 0 ? names.slice(0, 5) : DEFAULT_CONFIG.localNames
621
+ }
622
+ await persistConfig()
623
+ return sendJson(res, 200, { ok: true, config: { ...config } })
624
+ }
625
+
626
+ if (route === '/open' && req.method === 'POST') {
627
+ const body = await readBody(req)
628
+ const scope = String(body.scope ?? 'global')
629
+ let target = ''
630
+ if (scope === 'global') {
631
+ return sendJson(res, 410, { error: '全局人格已移除;只支持项目人格' })
632
+ } else if (scope === 'project') {
633
+ const cwd = String(body.cwd ?? '').trim()
634
+ if (cwd === '') throw Object.assign(new Error('缺少 cwd'), { status: 400 })
635
+ const kind = String(body.kind ?? 'stored')
636
+ if (kind === 'local') {
637
+ target = path.join(cwd, config.localNames[0])
638
+ if (readTextFile(target) === null) writeTextFile(target, buildTemplate('project', displayNameFor(cwd)))
639
+ } else {
640
+ target = slugFor(cwd).path
641
+ if (readTextFile(target) === null) writeTextFile(target, buildTemplate('project', displayNameFor(cwd)))
642
+ }
643
+ } else if (scope === 'template') {
644
+ const id = String(body.id ?? '').trim().replace(/[^0-9a-zA-Z\u4e00-\u9fa5._-]/g, '')
645
+ target = path.join(agentsRoot(), 'templates', `${id}.md`)
646
+ } else if (scope === 'dir') {
647
+ target = projectsDir()
648
+ } else {
649
+ throw Object.assign(new Error(`未知 scope: ${scope}`), { status: 400 })
650
+ }
651
+ try {
652
+ await openInFileManager(target)
653
+ return sendJson(res, 200, { opened: target })
654
+ } catch (error) {
655
+ return sendJson(res, 500, { error: `无法打开文件管理器:${String((error && error.message) || error)}` })
656
+ }
657
+ }
658
+
659
+ // 项目/会话两级登记:设置页用它把「当前会话」之外的会话也拉进来
660
+ if (route === '/register' && req.method === 'POST') {
661
+ const body = await readBody(req)
662
+ const cwd = String(body.cwd ?? '').trim()
663
+ if (cwd === '') throw Object.assign(new Error('缺少 cwd'), { status: 400 })
664
+ const registry = registerProject(cwd, { active: body.active === true })
665
+ return sendJson(res, 200, { ok: true, registry, slug: slugFor(cwd) })
666
+ }
667
+
668
+ // 系统「选择文件夹」:让任意目录都能成为项目人格的归属
669
+ if (route === '/pick' && req.method === 'POST') {
670
+ try {
671
+ const picked = await pickFolder()
672
+ if (picked === '') return sendJson(res, 200, { picked: '', cancelled: true })
673
+ registerProject(picked, { active: false })
674
+ return sendJson(res, 200, { picked, cancelled: false, slug: slugFor(picked) })
675
+ } catch (error) {
676
+ return sendJson(res, 500, { error: `打不开系统文件夹选择器:${String((error && error.message) || error)};可以直接把路径粘到输入框里。` })
677
+ }
678
+ }
679
+
680
+ // ---- QQ 网关联动 ------------------------------------------------
681
+ // 网关(@tencent-connect/dsh-qqbot)只有在**本进程**里跑,才能和网页
682
+ // 共享同一个 live agent:QQ 消息进这个对话、这个对话的回复再发回 QQ。
683
+ // 绑定关系落在网关自己的 prefs(~/.dsh-qqbot/model-prefs.json)里。
684
+ if (route === '/qq' && req.method === 'GET') {
685
+ return sendJson(res, 200, { qq: qqStatus(ctx) })
686
+ }
687
+
688
+ if (route === '/qq/bind' && req.method === 'POST') {
689
+ const body = await readBody(req)
690
+ const sessionId = String(body.sessionId ?? '').trim()
691
+ if (!/^[A-Za-z0-9._:-]{1,160}$/.test(sessionId)) {
692
+ throw Object.assign(new Error('缺少或非法的 sessionId'), { status: 400 })
693
+ }
694
+ const status = qqStatus(ctx)
695
+ if (status.installed !== true) {
696
+ throw Object.assign(new Error('这个 DSH 进程没有加载 QQ 网关(@tencent-connect/dsh-qqbot)——先把网关装进网页 profile 并重启,再来绑定。'), { status: 409 })
697
+ }
698
+ const peers = Array.isArray(status.peers) ? status.peers : []
699
+ let peerKey = String(body.peerKey ?? '').trim()
700
+ if (peerKey === '') {
701
+ const c2c = peers.filter((p) => p.scope === 'c2c')
702
+ if (c2c.length === 1) {
703
+ peerKey = c2c[0].key
704
+ } else if (c2c.length === 0) {
705
+ throw Object.assign(new Error('还没有记录到你的 QQ 账号:先在 QQ 里给机器人发一条消息(或发 /new),让网关把账号落盘,再回来绑定。'), { status: 409 })
706
+ } else {
707
+ throw Object.assign(new Error('有多个 QQ 私聊账号,请在界面上指定要绑定的那一个。'), { status: 409 })
708
+ }
709
+ }
710
+ const info = parsePeerKey(peerKey)
711
+ if (info === null) throw Object.assign(new Error(`不是合法的 QQ peer key:${peerKey}`), { status: 400 })
712
+ const result = await bindQqPeer(ctx, { peerKey: info.key, sessionId, cwd: String(body.cwd ?? '').trim() })
713
+ return sendJson(res, 200, {
714
+ ok: result.reload.ok === true,
715
+ bound: true,
716
+ peerKey,
717
+ sessionId,
718
+ previousSessionId: result.previousSessionId,
719
+ reload: result.reload,
720
+ qq: qqStatus(ctx),
721
+ })
722
+ }
723
+
724
+ if (route === '/qq/unbind' && req.method === 'POST') {
725
+ const body = await readBody(req)
726
+ const status = qqStatus(ctx)
727
+ let peerKey = String(body.peerKey ?? '').trim()
728
+ if (peerKey === '') {
729
+ const bound = (Array.isArray(status.peers) ? status.peers : []).filter((p) => p.binding !== null && p.binding !== undefined)
730
+ if (bound.length === 1) {
731
+ peerKey = bound[0].key
732
+ } else if (bound.length === 0) {
733
+ return sendJson(res, 200, { ok: true, unbound: false, reason: 'not-bound', qq: status })
734
+ } else {
735
+ throw Object.assign(new Error('有多条绑定,请指定要解除的那一个。'), { status: 400 })
736
+ }
737
+ }
738
+ if (parsePeerKey(peerKey) === null) throw Object.assign(new Error(`不是合法的 QQ peer key:${peerKey}`), { status: 400 })
739
+ const result = await unbindQqPeer(ctx, peerKey)
740
+ return sendJson(res, 200, {
741
+ ok: result.reload.ok === true,
742
+ unbound: result.record !== null,
743
+ peerKey,
744
+ restoredSessionId: result.restoredSessionId,
745
+ reload: result.reload,
746
+ qq: qqStatus(ctx),
747
+ })
748
+ }
749
+
750
+ return sendJson(res, 404, { error: `未知接口: ${route}` })
751
+ } catch (error) {
752
+ const status = typeof error?.status === 'number' ? error.status : 500
753
+ return sendJson(res, status, { error: String((error && error.message) || error) })
754
+ }
755
+ },
756
+ }
757
+ // 注册绑在 ctx.effect 上:插件重载/禁用时自动摘掉路由,否则 webserver 会以
758
+ // "duplicate prefix route" 拒绝下一次注册。
759
+ const attachApi = (target) => { target.effect(() => target.webServer.register(apiRoute)) }
760
+ if (typeof ctx.inject === 'function') {
761
+ ctx.inject(['webServer'], attachApi)
762
+ } else if (ctx.webServer !== undefined && ctx.webServer !== null) {
763
+ // 测试替身没有 inject,只挂了属性:直接挂,保持断言可以观察路由
764
+ attachApi(ctx)
765
+ }
766
+
767
+ ctx.logger?.info?.(`AGENT.md 人格配置:${config.enabled ? '开' : '关'}(全局 ${globalPath()};项目目录 ${projectsDir()})`)
768
+ }
769
+
770
+ /** 供测试使用:把配置重置为默认(不落盘)。 */
771
+ export function __resetConfigForTest() {
772
+ config = { ...DEFAULT_CONFIG }
773
+ }
774
+
775
+ // global removed 1793149026