wezard 2.9.0 → 3.0.2

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 (477) hide show
  1. package/CLAUDE.md +43 -21
  2. package/README.md +37 -50
  3. package/cli/wezard.sh +59 -20
  4. package/config.example.jsonc +5 -8
  5. package/dist/cli/audit.js +11 -13
  6. package/dist/cli/audit.js.map +1 -1
  7. package/dist/cli/codex-hook.js +20 -17
  8. package/dist/cli/codex-hook.js.map +1 -1
  9. package/dist/cli/ext.js +271 -0
  10. package/dist/cli/ext.js.map +1 -0
  11. package/dist/cli/init.js +44 -13
  12. package/dist/cli/init.js.map +1 -1
  13. package/dist/cli/skills.js +113 -0
  14. package/dist/cli/skills.js.map +1 -0
  15. package/dist/cli/sync.js +3 -2
  16. package/dist/cli/sync.js.map +1 -1
  17. package/dist/cli/wx.js +7 -6
  18. package/dist/cli/wx.js.map +1 -1
  19. package/dist/daemon/aibot-media.js +28 -20
  20. package/dist/daemon/aibot-media.js.map +1 -1
  21. package/dist/daemon/apprentice.js +75 -0
  22. package/dist/daemon/apprentice.js.map +1 -0
  23. package/dist/daemon/approval.js +75 -33
  24. package/dist/daemon/approval.js.map +1 -1
  25. package/dist/daemon/channel-context.js +7 -0
  26. package/dist/daemon/channel-context.js.map +1 -1
  27. package/dist/daemon/channel-onboarding.js +61 -0
  28. package/dist/daemon/channel-onboarding.js.map +1 -0
  29. package/dist/daemon/channel-posts.js +93 -0
  30. package/dist/daemon/channel-posts.js.map +1 -0
  31. package/dist/daemon/channel-rebind.js +100 -0
  32. package/dist/daemon/channel-rebind.js.map +1 -0
  33. package/dist/daemon/channel-store.js.map +1 -1
  34. package/dist/daemon/channel.js +273 -409
  35. package/dist/daemon/channel.js.map +1 -1
  36. package/dist/daemon/chat-name.js +24 -161
  37. package/dist/daemon/chat-name.js.map +1 -1
  38. package/dist/daemon/claim.js +1 -45
  39. package/dist/daemon/claim.js.map +1 -1
  40. package/dist/daemon/classes.js +416 -0
  41. package/dist/daemon/classes.js.map +1 -0
  42. package/dist/daemon/config-api.js +6 -30
  43. package/dist/daemon/config-api.js.map +1 -1
  44. package/dist/daemon/control-socket.js +93 -0
  45. package/dist/daemon/control-socket.js.map +1 -0
  46. package/dist/daemon/danger-scrub.js +315 -0
  47. package/dist/daemon/danger-scrub.js.map +1 -0
  48. package/dist/daemon/danger.js +146 -16
  49. package/dist/daemon/danger.js.map +1 -1
  50. package/dist/daemon/detail.js +50 -22
  51. package/dist/daemon/detail.js.map +1 -1
  52. package/dist/daemon/exec-surface.js +108 -0
  53. package/dist/daemon/exec-surface.js.map +1 -0
  54. package/dist/daemon/external.js +595 -0
  55. package/dist/daemon/external.js.map +1 -0
  56. package/dist/daemon/handoff.js +51 -50
  57. package/dist/daemon/handoff.js.map +1 -1
  58. package/dist/daemon/http.js +119 -31
  59. package/dist/daemon/http.js.map +1 -1
  60. package/dist/daemon/idle-reap.js +30 -0
  61. package/dist/daemon/idle-reap.js.map +1 -0
  62. package/dist/daemon/inbound.js +436 -207
  63. package/dist/daemon/inbound.js.map +1 -1
  64. package/dist/daemon/index.js +3353 -2760
  65. package/dist/daemon/index.js.map +1 -1
  66. package/dist/daemon/last-response.js +51 -25
  67. package/dist/daemon/last-response.js.map +1 -1
  68. package/dist/daemon/legacy-panes.js +67 -0
  69. package/dist/daemon/legacy-panes.js.map +1 -0
  70. package/dist/daemon/mcp-http.js.map +1 -1
  71. package/dist/daemon/me-room.js +137 -0
  72. package/dist/daemon/me-room.js.map +1 -0
  73. package/dist/daemon/media.js +14 -0
  74. package/dist/daemon/media.js.map +1 -1
  75. package/dist/daemon/memory-steward.js +1 -1
  76. package/dist/daemon/memory-steward.js.map +1 -1
  77. package/dist/daemon/message-format.js +15 -2
  78. package/dist/daemon/message-format.js.map +1 -1
  79. package/dist/daemon/mirror-bridge.js +583 -488
  80. package/dist/daemon/mirror-bridge.js.map +1 -1
  81. package/dist/daemon/mirror-store.js +24 -0
  82. package/dist/daemon/mirror-store.js.map +1 -1
  83. package/dist/daemon/notices.js +5 -6
  84. package/dist/daemon/notices.js.map +1 -1
  85. package/dist/daemon/onboarding.js +459 -0
  86. package/dist/daemon/onboarding.js.map +1 -0
  87. package/dist/daemon/outbound.js +21 -11
  88. package/dist/daemon/outbound.js.map +1 -1
  89. package/dist/daemon/peers.js +39 -231
  90. package/dist/daemon/peers.js.map +1 -1
  91. package/dist/daemon/plan-http.js +2 -3
  92. package/dist/daemon/plan-http.js.map +1 -1
  93. package/dist/daemon/plans.js.map +1 -1
  94. package/dist/daemon/reach.js +1 -5
  95. package/dist/daemon/reach.js.map +1 -1
  96. package/dist/daemon/room-auto-adhoc.js +18 -0
  97. package/dist/daemon/room-auto-adhoc.js.map +1 -0
  98. package/dist/daemon/room-bind.js +297 -0
  99. package/dist/daemon/room-bind.js.map +1 -0
  100. package/dist/daemon/room-facts.js +35 -0
  101. package/dist/daemon/room-facts.js.map +1 -0
  102. package/dist/daemon/room-hub.js +569 -0
  103. package/dist/daemon/room-hub.js.map +1 -0
  104. package/dist/daemon/room-overdue.js +160 -0
  105. package/dist/daemon/room-overdue.js.map +1 -0
  106. package/dist/daemon/room-prune.js +74 -0
  107. package/dist/daemon/room-prune.js.map +1 -0
  108. package/dist/daemon/room-spawn.js +20 -0
  109. package/dist/daemon/room-spawn.js.map +1 -0
  110. package/dist/daemon/room-watchdog.js +175 -0
  111. package/dist/daemon/room-watchdog.js.map +1 -0
  112. package/dist/daemon/rooms.js +204 -0
  113. package/dist/daemon/rooms.js.map +1 -0
  114. package/dist/daemon/route.js +7 -18
  115. package/dist/daemon/route.js.map +1 -1
  116. package/dist/daemon/session-scan.js +1 -1
  117. package/dist/daemon/session-scan.js.map +1 -1
  118. package/dist/daemon/short-id-log.js +43 -0
  119. package/dist/daemon/short-id-log.js.map +1 -0
  120. package/dist/daemon/spawn-tmux.js +60 -45
  121. package/dist/daemon/spawn-tmux.js.map +1 -1
  122. package/dist/daemon/steward-guard.js +3 -3
  123. package/dist/daemon/steward-guard.js.map +1 -1
  124. package/dist/daemon/steward-model.js +17 -3
  125. package/dist/daemon/steward-model.js.map +1 -1
  126. package/dist/daemon/steward-repair.js +171 -0
  127. package/dist/daemon/steward-repair.js.map +1 -0
  128. package/dist/daemon/steward.js +171 -0
  129. package/dist/daemon/steward.js.map +1 -0
  130. package/dist/daemon/tasks.js +4 -14
  131. package/dist/daemon/tasks.js.map +1 -1
  132. package/dist/daemon/tool-face.js +31 -0
  133. package/dist/daemon/tool-face.js.map +1 -0
  134. package/dist/daemon/turn-opening.js +30 -0
  135. package/dist/daemon/turn-opening.js.map +1 -0
  136. package/dist/daemon/wake-tail.js +55 -0
  137. package/dist/daemon/wake-tail.js.map +1 -0
  138. package/dist/daemon/weixin-cmd.js +10 -14
  139. package/dist/daemon/weixin-cmd.js.map +1 -1
  140. package/dist/daemon/wezard-session.js +6 -2
  141. package/dist/daemon/wezard-session.js.map +1 -1
  142. package/dist/daemon/wizard-memory.js +4 -10
  143. package/dist/daemon/wizard-memory.js.map +1 -1
  144. package/dist/daemon/wizard-names.js +0 -22
  145. package/dist/daemon/wizard-names.js.map +1 -1
  146. package/dist/daemon/wizard.js +683 -159
  147. package/dist/daemon/wizard.js.map +1 -1
  148. package/dist/daemon/ws.js +1 -1
  149. package/dist/daemon/ws.js.map +1 -1
  150. package/dist/mcp/output.js +30 -7
  151. package/dist/mcp/output.js.map +1 -1
  152. package/dist/mcp/relay.js +152 -0
  153. package/dist/mcp/relay.js.map +1 -0
  154. package/dist/mcp/tools.js +318 -265
  155. package/dist/mcp/tools.js.map +1 -1
  156. package/dist/shared/allow-rules.js +1 -7
  157. package/dist/shared/allow-rules.js.map +1 -1
  158. package/dist/shared/charter-rearm.js +179 -0
  159. package/dist/shared/charter-rearm.js.map +1 -0
  160. package/dist/shared/chat-http.js +412 -480
  161. package/dist/shared/chat-http.js.map +1 -1
  162. package/dist/shared/chat-record.js +55 -0
  163. package/dist/shared/chat-record.js.map +1 -0
  164. package/dist/shared/chat-render.js +7 -4
  165. package/dist/shared/chat-render.js.map +1 -1
  166. package/dist/shared/chat-view.js +2 -41
  167. package/dist/shared/chat-view.js.map +1 -1
  168. package/dist/shared/cli-backends.js +13 -5
  169. package/dist/shared/cli-backends.js.map +1 -1
  170. package/dist/shared/cli-local-status.js +28 -0
  171. package/dist/shared/cli-local-status.js.map +1 -0
  172. package/dist/shared/cli-socket.js +14 -0
  173. package/dist/shared/cli-socket.js.map +1 -0
  174. package/dist/shared/codex-hooks.js +5 -2
  175. package/dist/shared/codex-hooks.js.map +1 -1
  176. package/dist/shared/config.js +90 -66
  177. package/dist/shared/config.js.map +1 -1
  178. package/dist/shared/daemon-addr.js +23 -0
  179. package/dist/shared/daemon-addr.js.map +1 -0
  180. package/dist/shared/daemon-client.js +41 -0
  181. package/dist/shared/daemon-client.js.map +1 -0
  182. package/dist/shared/daemon-token.js +2 -2
  183. package/dist/shared/daemon-token.js.map +1 -1
  184. package/dist/shared/detail-render.js +98 -21
  185. package/dist/shared/detail-render.js.map +1 -1
  186. package/dist/shared/detail-store.js.map +1 -1
  187. package/dist/shared/external.js +402 -0
  188. package/dist/shared/external.js.map +1 -0
  189. package/dist/shared/group-members.js +35 -0
  190. package/dist/shared/group-members.js.map +1 -0
  191. package/dist/shared/inject-view.js +143 -0
  192. package/dist/shared/inject-view.js.map +1 -0
  193. package/dist/shared/instance-id.js +21 -0
  194. package/dist/shared/instance-id.js.map +1 -0
  195. package/dist/shared/legacy-routes.js +199 -0
  196. package/dist/shared/legacy-routes.js.map +1 -0
  197. package/dist/shared/mem-guard.js +32 -0
  198. package/dist/shared/mem-guard.js.map +1 -0
  199. package/dist/shared/message-footer.js +11 -3
  200. package/dist/shared/message-footer.js.map +1 -1
  201. package/dist/shared/model-tiers.js +39 -1
  202. package/dist/shared/model-tiers.js.map +1 -1
  203. package/dist/shared/ports.js +56 -0
  204. package/dist/shared/ports.js.map +1 -0
  205. package/dist/shared/reminder.js +30 -38
  206. package/dist/shared/reminder.js.map +1 -1
  207. package/dist/shared/role-render.js +210 -210
  208. package/dist/shared/role-render.js.map +1 -1
  209. package/dist/shared/role-view.js +65 -300
  210. package/dist/shared/role-view.js.map +1 -1
  211. package/dist/shared/room-label.js +13 -0
  212. package/dist/shared/room-label.js.map +1 -0
  213. package/dist/shared/room-migrate.js +404 -0
  214. package/dist/shared/room-migrate.js.map +1 -0
  215. package/dist/shared/room-overdue.js +153 -0
  216. package/dist/shared/room-overdue.js.map +1 -0
  217. package/dist/shared/room-search.js +102 -0
  218. package/dist/shared/room-search.js.map +1 -0
  219. package/dist/shared/room-view.js +275 -0
  220. package/dist/shared/room-view.js.map +1 -0
  221. package/dist/shared/room.js +827 -0
  222. package/dist/shared/room.js.map +1 -0
  223. package/dist/shared/session-hooks.js +20 -8
  224. package/dist/shared/session-hooks.js.map +1 -1
  225. package/dist/shared/session-label.js +82 -84
  226. package/dist/shared/session-label.js.map +1 -1
  227. package/dist/shared/session-mcp.js +36 -40
  228. package/dist/shared/session-mcp.js.map +1 -1
  229. package/dist/shared/session-skills.js +8 -0
  230. package/dist/shared/session-skills.js.map +1 -1
  231. package/dist/shared/skill-sync.js +229 -0
  232. package/dist/shared/skill-sync.js.map +1 -0
  233. package/dist/shared/smarts.js +24 -0
  234. package/dist/shared/smarts.js.map +1 -0
  235. package/dist/shared/stream-page.js +22 -0
  236. package/dist/shared/stream-page.js.map +1 -0
  237. package/dist/shared/task-file.js +0 -5
  238. package/dist/shared/task-file.js.map +1 -1
  239. package/dist/shared/task-template.js +1 -1
  240. package/dist/shared/task-template.js.map +1 -1
  241. package/dist/shared/tool-hints.js +20 -11
  242. package/dist/shared/tool-hints.js.map +1 -1
  243. package/dist/shared/workspace-trust.js +8 -0
  244. package/dist/shared/workspace-trust.js.map +1 -1
  245. package/dist/shared/world.js +101 -166
  246. package/dist/shared/world.js.map +1 -1
  247. package/dist/svr/facts-store.js +26 -0
  248. package/dist/svr/facts-store.js.map +1 -0
  249. package/dist/svr/index.js +114 -16
  250. package/dist/svr/index.js.map +1 -1
  251. package/hooks/pre-tool-use.sh +14 -10
  252. package/hooks/stop.sh +28 -0
  253. package/launchd/com.wezard.svr.plist.template +2 -0
  254. package/package.json +9 -2
  255. package/scripts/install.sh +23 -0
  256. package/skills/LICENSE.wecom-unified.txt +21 -0
  257. package/skills/wecom-unified/SKILL.md +151 -0
  258. package/skills/wecom-unified/assets/templates/broadside/DESIGN.preset.md +172 -0
  259. package/skills/wecom-unified/assets/templates/broadside/shell/content.jsx +31 -0
  260. package/skills/wecom-unified/assets/templates/broadside/slides/01_cover.slide +48 -0
  261. package/skills/wecom-unified/assets/templates/broadside/slides/02_section.slide +58 -0
  262. package/skills/wecom-unified/assets/templates/broadside/slides/03_cards.slide +53 -0
  263. package/skills/wecom-unified/assets/templates/broadside/slides/04_image.slide +64 -0
  264. package/skills/wecom-unified/assets/templates/cartesian/DESIGN.preset.md +139 -0
  265. package/skills/wecom-unified/assets/templates/cartesian/shell/content.jsx +28 -0
  266. package/skills/wecom-unified/assets/templates/cartesian/slides/01_cover.slide +44 -0
  267. package/skills/wecom-unified/assets/templates/cartesian/slides/02_compare.slide +79 -0
  268. package/skills/wecom-unified/assets/templates/cartesian/slides/03_table.slide +79 -0
  269. package/skills/wecom-unified/assets/templates/cobalt-grid/DESIGN.preset.md +118 -0
  270. package/skills/wecom-unified/assets/templates/cobalt-grid/shell/content.jsx +24 -0
  271. package/skills/wecom-unified/assets/templates/cobalt-grid/slides/01_cover.slide +61 -0
  272. package/skills/wecom-unified/assets/templates/cobalt-grid/slides/02_structure.slide +85 -0
  273. package/skills/wecom-unified/assets/templates/cobalt-grid/slides/03_data.slide +60 -0
  274. package/skills/wecom-unified/assets/templates/editorial-forest/DESIGN.preset.md +135 -0
  275. package/skills/wecom-unified/assets/templates/editorial-forest/shell/content.jsx +19 -0
  276. package/skills/wecom-unified/assets/templates/editorial-forest/slides/01_cover.slide +29 -0
  277. package/skills/wecom-unified/assets/templates/editorial-forest/slides/02_cards.slide +60 -0
  278. package/skills/wecom-unified/assets/templates/editorial-forest/slides/03_data.slide +49 -0
  279. package/skills/wecom-unified/assets/templates/editorial-forest/slides/04_image.slide +46 -0
  280. package/skills/wecom-unified/assets/templates/emerald-editorial/DESIGN.preset.md +119 -0
  281. package/skills/wecom-unified/assets/templates/emerald-editorial/shell/content.jsx +18 -0
  282. package/skills/wecom-unified/assets/templates/emerald-editorial/slides/01_cover.slide +36 -0
  283. package/skills/wecom-unified/assets/templates/emerald-editorial/slides/02_section.slide +39 -0
  284. package/skills/wecom-unified/assets/templates/emerald-editorial/slides/03_data.slide +79 -0
  285. package/skills/wecom-unified/assets/templates/emerald-editorial/slides/04_steps.slide +53 -0
  286. package/skills/wecom-unified/assets/templates/grove/DESIGN.preset.md +159 -0
  287. package/skills/wecom-unified/assets/templates/grove/shell/content.jsx +18 -0
  288. package/skills/wecom-unified/assets/templates/grove/shell/content_light.jsx +18 -0
  289. package/skills/wecom-unified/assets/templates/grove/slides/01_cover.slide +26 -0
  290. package/skills/wecom-unified/assets/templates/grove/slides/02_compare.slide +92 -0
  291. package/skills/wecom-unified/assets/templates/grove/slides/03_table.slide +68 -0
  292. package/skills/wecom-unified/assets/templates/index.json +280 -0
  293. package/skills/wecom-unified/assets/templates/long-table/DESIGN.preset.md +135 -0
  294. package/skills/wecom-unified/assets/templates/long-table/shell/content.jsx +36 -0
  295. package/skills/wecom-unified/assets/templates/long-table/slides/01_cover.slide +71 -0
  296. package/skills/wecom-unified/assets/templates/long-table/slides/02_cards.slide +72 -0
  297. package/skills/wecom-unified/assets/templates/long-table/slides/03_timeline.slide +81 -0
  298. package/skills/wecom-unified/assets/templates/long-table/slides/04_image.slide +60 -0
  299. package/skills/wecom-unified/assets/templates/sakura-chroma/DESIGN.preset.md +121 -0
  300. package/skills/wecom-unified/assets/templates/sakura-chroma/shell/content.jsx +13 -0
  301. package/skills/wecom-unified/assets/templates/sakura-chroma/slides/01_cover.slide +54 -0
  302. package/skills/wecom-unified/assets/templates/sakura-chroma/slides/02_products.slide +134 -0
  303. package/skills/wecom-unified/assets/templates/sakura-chroma/slides/03_section.slide +30 -0
  304. package/skills/wecom-unified/assets/templates/sakura-chroma/slides/04_schedule.slide +65 -0
  305. package/skills/wecom-unified/assets/templates/signal/DESIGN.preset.md +121 -0
  306. package/skills/wecom-unified/assets/templates/signal/shell/content.jsx +18 -0
  307. package/skills/wecom-unified/assets/templates/signal/shell/content_dark.jsx +23 -0
  308. package/skills/wecom-unified/assets/templates/signal/slides/01_cover.slide +48 -0
  309. package/skills/wecom-unified/assets/templates/signal/slides/02_cards.slide +67 -0
  310. package/skills/wecom-unified/assets/templates/signal/slides/03_data.slide +68 -0
  311. package/skills/wecom-unified/references/wecomcli-calendar-agenda.md +227 -0
  312. package/skills/wecom-unified/references/wecomcli-calendar-cancel.md +108 -0
  313. package/skills/wecom-unified/references/wecomcli-calendar-create.md +238 -0
  314. package/skills/wecom-unified/references/wecomcli-calendar-freebusy.md +207 -0
  315. package/skills/wecom-unified/references/wecomcli-calendar-meeting-room.md +170 -0
  316. package/skills/wecom-unified/references/wecomcli-calendar-search.md +209 -0
  317. package/skills/wecom-unified/references/wecomcli-calendar-update.md +272 -0
  318. package/skills/wecom-unified/references/wecomcli-calendar.md +296 -0
  319. package/skills/wecom-unified/references/wecomcli-contact.md +48 -0
  320. package/skills/wecom-unified/references/wecomcli-disk.md +380 -0
  321. package/skills/wecom-unified/references/wecomcli-doc-contents-append.md +20 -0
  322. package/skills/wecom-unified/references/wecomcli-doc-contents-overwrite.md +27 -0
  323. package/skills/wecom-unified/references/wecomcli-doc-create.md +162 -0
  324. package/skills/wecom-unified/references/wecomcli-doc-manage-doc-members-update.md +24 -0
  325. package/skills/wecom-unified/references/wecomcli-doc-manage-doc-names-update.md +20 -0
  326. package/skills/wecom-unified/references/wecomcli-doc-manage-doc-rules-update.md +22 -0
  327. package/skills/wecom-unified/references/wecomcli-doc-manage.md +127 -0
  328. package/skills/wecom-unified/references/wecomcli-doc.md +135 -0
  329. package/skills/wecom-unified/references/wecomcli-email-forward-mail.md +131 -0
  330. package/skills/wecom-unified/references/wecomcli-email-get-mail.md +187 -0
  331. package/skills/wecom-unified/references/wecomcli-email-reply-mail.md +138 -0
  332. package/skills/wecom-unified/references/wecomcli-email-search-mail.md +114 -0
  333. package/skills/wecom-unified/references/wecomcli-email-security.md +52 -0
  334. package/skills/wecom-unified/references/wecomcli-email-send-mail.md +186 -0
  335. package/skills/wecom-unified/references/wecomcli-email-send-schedule.md +83 -0
  336. package/skills/wecom-unified/references/wecomcli-email.md +206 -0
  337. package/skills/wecom-unified/references/wecomcli-media.md +89 -0
  338. package/skills/wecom-unified/references/wecomcli-meeting-cancel.md +113 -0
  339. package/skills/wecom-unified/references/wecomcli-meeting-create.md +167 -0
  340. package/skills/wecom-unified/references/wecomcli-meeting-list.md +228 -0
  341. package/skills/wecom-unified/references/wecomcli-meeting-original-get.md +101 -0
  342. package/skills/wecom-unified/references/wecomcli-meeting-search.md +176 -0
  343. package/skills/wecom-unified/references/wecomcli-meeting-update.md +217 -0
  344. package/skills/wecom-unified/references/wecomcli-meeting.md +366 -0
  345. package/skills/wecom-unified/references/wecomcli-message.md +190 -0
  346. package/skills/wecom-unified/references/wecomcli-pptx-component-animation.md +62 -0
  347. package/skills/wecom-unified/references/wecomcli-pptx-component-box.md +102 -0
  348. package/skills/wecom-unified/references/wecomcli-pptx-component-chart.md +261 -0
  349. package/skills/wecom-unified/references/wecomcli-pptx-component-codeblock.md +70 -0
  350. package/skills/wecom-unified/references/wecomcli-pptx-component-diagram.md +54 -0
  351. package/skills/wecom-unified/references/wecomcli-pptx-component-faicon.md +71 -0
  352. package/skills/wecom-unified/references/wecomcli-pptx-component-hyperlink.md +55 -0
  353. package/skills/wecom-unified/references/wecomcli-pptx-component-image.md +107 -0
  354. package/skills/wecom-unified/references/wecomcli-pptx-component-math.md +65 -0
  355. package/skills/wecom-unified/references/wecomcli-pptx-component-qrcode.md +22 -0
  356. package/skills/wecom-unified/references/wecomcli-pptx-component-slide.md +74 -0
  357. package/skills/wecom-unified/references/wecomcli-pptx-component-svg.md +95 -0
  358. package/skills/wecom-unified/references/wecomcli-pptx-component-table.md +72 -0
  359. package/skills/wecom-unified/references/wecomcli-pptx-component-text.md +96 -0
  360. package/skills/wecom-unified/references/wecomcli-pptx-content-contract.md +73 -0
  361. package/skills/wecom-unified/references/wecomcli-pptx-create-from-material.md +67 -0
  362. package/skills/wecom-unified/references/wecomcli-pptx-create-from-scratch.md +178 -0
  363. package/skills/wecom-unified/references/wecomcli-pptx-create-from-template.md +123 -0
  364. package/skills/wecom-unified/references/wecomcli-pptx-design-principle.md +65 -0
  365. package/skills/wecom-unified/references/wecomcli-pptx-government-contract.md +40 -0
  366. package/skills/wecom-unified/references/wecomcli-pptx-human-alignment.md +140 -0
  367. package/skills/wecom-unified/references/wecomcli-pptx-pptx-api.md +161 -0
  368. package/skills/wecom-unified/references/wecomcli-pptx-pptx-edit.md +37 -0
  369. package/skills/wecom-unified/references/wecomcli-pptx-story-principle.md +184 -0
  370. package/skills/wecom-unified/references/wecomcli-pptx-templates-README.md +165 -0
  371. package/skills/wecom-unified/references/wecomcli-pptx-templates-anti-ai-slop.md +52 -0
  372. package/skills/wecom-unified/references/wecomcli-pptx.md +158 -0
  373. package/skills/wecom-unified/references/wecomcli-sheet-conditional-formatting.md +203 -0
  374. package/skills/wecom-unified/references/wecomcli-sheet-filter.md +179 -0
  375. package/skills/wecom-unified/references/wecomcli-sheet-float-image.md +235 -0
  376. package/skills/wecom-unified/references/wecomcli-sheet-formulas.md +71 -0
  377. package/skills/wecom-unified/references/wecomcli-sheet-history.md +118 -0
  378. package/skills/wecom-unified/references/wecomcli-sheet-protection.md +170 -0
  379. package/skills/wecom-unified/references/wecomcli-sheet-range-operations.md +306 -0
  380. package/skills/wecom-unified/references/wecomcli-sheet-read-data.md +151 -0
  381. package/skills/wecom-unified/references/wecomcli-sheet-search-replace.md +121 -0
  382. package/skills/wecom-unified/references/wecomcli-sheet-structure-operations.md +394 -0
  383. package/skills/wecom-unified/references/wecomcli-sheet-style.md +246 -0
  384. package/skills/wecom-unified/references/wecomcli-sheet-table-style.md +199 -0
  385. package/skills/wecom-unified/references/wecomcli-sheet-validation.md +217 -0
  386. package/skills/wecom-unified/references/wecomcli-sheet-visual-standards.md +166 -0
  387. package/skills/wecom-unified/references/wecomcli-sheet-workbook.md +264 -0
  388. package/skills/wecom-unified/references/wecomcli-sheet-write-cells.md +320 -0
  389. package/skills/wecom-unified/references/wecomcli-sheet-xlsx-process.md +324 -0
  390. package/skills/wecom-unified/references/wecomcli-sheet.md +96 -0
  391. package/skills/wecom-unified/references/wecomcli-smartpage-data-driven-pages.md +50 -0
  392. package/skills/wecom-unified/references/wecomcli-smartpage-edit.md +526 -0
  393. package/skills/wecom-unified/references/wecomcli-smartpage-formula-arraylist.md +369 -0
  394. package/skills/wecom-unified/references/wecomcli-smartpage-formula-datetime.md +283 -0
  395. package/skills/wecom-unified/references/wecomcli-smartpage-formula-logic.md +247 -0
  396. package/skills/wecom-unified/references/wecomcli-smartpage-formula-math.md +362 -0
  397. package/skills/wecom-unified/references/wecomcli-smartpage-formula-operators.md +246 -0
  398. package/skills/wecom-unified/references/wecomcli-smartpage-formula-pageblock.md +76 -0
  399. package/skills/wecom-unified/references/wecomcli-smartpage-formula-reference.md +192 -0
  400. package/skills/wecom-unified/references/wecomcli-smartpage-formula-templates.md +410 -0
  401. package/skills/wecom-unified/references/wecomcli-smartpage-formula-text.md +377 -0
  402. package/skills/wecom-unified/references/wecomcli-smartpage-formula-user.md +22 -0
  403. package/skills/wecom-unified/references/wecomcli-smartpage-mdx-syntax.md +739 -0
  404. package/skills/wecom-unified/references/wecomcli-smartpage.md +179 -0
  405. package/skills/wecom-unified/references/wecomcli-smartsheet-chart-types.md +95 -0
  406. package/skills/wecom-unified/references/wecomcli-smartsheet-common.md +160 -0
  407. package/skills/wecom-unified/references/wecomcli-smartsheet-edit.md +609 -0
  408. package/skills/wecom-unified/references/wecomcli-smartsheet-field-types.md +438 -0
  409. package/skills/wecom-unified/references/wecomcli-smartsheet-formula.md +845 -0
  410. package/skills/wecom-unified/references/wecomcli-smartsheet-read.md +411 -0
  411. package/skills/wecom-unified/references/wecomcli-smartsheet-record-values.md +218 -0
  412. package/skills/wecom-unified/references/wecomcli-smartsheet-template-ai_efficiency.md +709 -0
  413. package/skills/wecom-unified/references/wecomcli-smartsheet-template-connect_to_app.md +380 -0
  414. package/skills/wecom-unified/references/wecomcli-smartsheet-template-financial_accounting.md +369 -0
  415. package/skills/wecom-unified/references/wecomcli-smartsheet-template-hr_and_administration.md +475 -0
  416. package/skills/wecom-unified/references/wecomcli-smartsheet-template-ledger_records.md +156 -0
  417. package/skills/wecom-unified/references/wecomcli-smartsheet-template-manufacturing.md +395 -0
  418. package/skills/wecom-unified/references/wecomcli-smartsheet-template-marketing.md +186 -0
  419. package/skills/wecom-unified/references/wecomcli-smartsheet-template-office_essentials.md +299 -0
  420. package/skills/wecom-unified/references/wecomcli-smartsheet-template-personal_efficiency.md +70 -0
  421. package/skills/wecom-unified/references/wecomcli-smartsheet-template-procurement_logistics.md +325 -0
  422. package/skills/wecom-unified/references/wecomcli-smartsheet-template-project_management.md +564 -0
  423. package/skills/wecom-unified/references/wecomcli-smartsheet-template-rd_process_customer.md +222 -0
  424. package/skills/wecom-unified/references/wecomcli-smartsheet-template-rd_process_ops.md +105 -0
  425. package/skills/wecom-unified/references/wecomcli-smartsheet-template-rd_process_project.md +109 -0
  426. package/skills/wecom-unified/references/wecomcli-smartsheet-template-rd_process_research.md +92 -0
  427. package/skills/wecom-unified/references/wecomcli-smartsheet-template-sales_and_operations.md +446 -0
  428. package/skills/wecom-unified/references/wecomcli-smartsheet-template-store_management.md +431 -0
  429. package/skills/wecom-unified/references/wecomcli-smartsheet-template-team_tasks.md +274 -0
  430. package/skills/wecom-unified/references/wecomcli-smartsheet-template-wechat_customer.md +384 -0
  431. package/skills/wecom-unified/references/wecomcli-smartsheet-template-work_report.md +100 -0
  432. package/skills/wecom-unified/references/wecomcli-smartsheet-templates.md +53 -0
  433. package/skills/wecom-unified/references/wecomcli-smartsheet-view-types.md +373 -0
  434. package/skills/wecom-unified/references/wecomcli-smartsheet-webhook-examples.md +176 -0
  435. package/skills/wecom-unified/references/wecomcli-smartsheet-webhook.md +186 -0
  436. package/skills/wecom-unified/references/wecomcli-smartsheet.md +147 -0
  437. package/skills/wecom-unified/references/wecomcli-todo-create.md +137 -0
  438. package/skills/wecom-unified/references/wecomcli-todo-delete.md +63 -0
  439. package/skills/wecom-unified/references/wecomcli-todo-finish.md +72 -0
  440. package/skills/wecom-unified/references/wecomcli-todo-get.md +69 -0
  441. package/skills/wecom-unified/references/wecomcli-todo-list.md +136 -0
  442. package/skills/wecom-unified/references/wecomcli-todo-update.md +112 -0
  443. package/skills/wecom-unified/references/wecomcli-todo.md +68 -0
  444. package/skills/wecom-unified/scripts/build_docx.py +1274 -0
  445. package/skills/wecom-unified/scripts/build_page.py +2449 -0
  446. package/skills/wecom-unified/scripts/collect.py +868 -0
  447. package/skills/wecom-unified/scripts/doc_image_extractor.py +498 -0
  448. package/skills/wecom-unified/scripts/pptx2image.py +268 -0
  449. package/skills/wecom-unified/scripts/recalc.py +403 -0
  450. package/systemd/wezard-svr.service.template +2 -0
  451. package/systemd/wezard.service.template +6 -0
  452. package/web/chat.css +70 -273
  453. package/web/chat.js +2269 -2190
  454. package/dist/daemon/bot-mux.js +0 -161
  455. package/dist/daemon/bot-mux.js.map +0 -1
  456. package/dist/daemon/chat-log.js +0 -130
  457. package/dist/daemon/chat-log.js.map +0 -1
  458. package/dist/daemon/dispatch.js +0 -6
  459. package/dist/daemon/dispatch.js.map +0 -1
  460. package/dist/daemon/idle-nudge.js +0 -90
  461. package/dist/daemon/idle-nudge.js.map +0 -1
  462. package/dist/daemon/message-events.js +0 -1155
  463. package/dist/daemon/message-events.js.map +0 -1
  464. package/dist/daemon/pending-items.js +0 -160
  465. package/dist/daemon/pending-items.js.map +0 -1
  466. package/dist/daemon/router.js +0 -742
  467. package/dist/daemon/router.js.map +0 -1
  468. package/dist/daemon/tips.js +0 -25
  469. package/dist/daemon/tips.js.map +0 -1
  470. package/dist/daemon/wedoc.js +0 -210
  471. package/dist/daemon/wedoc.js.map +0 -1
  472. package/dist/shared/role-search.js +0 -100
  473. package/dist/shared/role-search.js.map +0 -1
  474. package/dist/shared/startup-notice.js +0 -21
  475. package/dist/shared/startup-notice.js.map +0 -1
  476. package/dist/shared/turn-state.js +0 -27
  477. package/dist/shared/turn-state.js.map +0 -1
@@ -1,23 +1,21 @@
1
1
  // 频道(topic)桥: SDK 1.0.7 的 dispatch 表里没有 aibot_topic_event_callback,
2
- // 这一层在 SDK 丢掉帧之前接住它,把「频道里 @ 了机器人」翻译成一条普通的文本消息
3
- // 交给 inbound / mirror;出站再把 topic_id 的 markdown 换成频道的 send_post /
4
- // send_reply。群聊 / 单聊的路径完全不变。
2
+ // 这一层在 SDK 丢掉帧之前接住它。
5
3
  //
6
- // 频道里没有被 @ 的新帖子 / 新回复也会进来(topic_event / topic_reply_event)。
7
- // 这些不直接答,先交给每个 bot 的「判官」:判官是一个只读最近几条消息的轻量 wizard,
8
- // 只回 JSON {respond, reason};respond 才把原帖交给主 wizard。
9
- import { spawn } from "node:child_process";
4
+ // 2026-11-23 起频道 / 帖子都是 room (docs/room/channel-monitor.md):
5
+ // - 频道 room id = chat:<topic_id>; 帖子 room id = chat:<opk…> (原生帧的 chatid)。
6
+ // - 没被 @ 的新帖 / 还没配上 opk 的评论 → 频道 room 的一行人话 (带 post);
7
+ // 配上 opk 之后的评论 → 帖子 room 的一行人话。
8
+ // - @ 机器人的原生帧照旧由 inbound 送进帖子会话 (也就是帖子 room 的那条人话);
9
+ // 同一句的 topic_at_bot 事件只用来学配对 (post_id ↔ opk ↔ topic_id) 与兜底。
10
+ // - 判官 (一次性 JSON 判定) 已删: 每个 room 一个常驻 monitor 成员, 由它决定激活谁
11
+ // (shared/room.ts 的 activate 行); 这里只负责把话落进对的 room。
10
12
  import { join } from "node:path";
11
13
  import { generateReqId } from "@wecom/aibot-node-sdk";
12
- import { pickTier } from "../shared/model-tiers.js";
13
- import { resolveCliBackend } from "../shared/cli-backends.js";
14
- import { augmentedPath } from "../shared/exec-path.js";
15
- import { baseOfKey, keyOf } from "../shared/session-label.js";
16
14
  import { sleep } from "../shared/std.js";
17
- import { wizardStore } from "./wizard.js";
15
+ import { learnChannelPost, postBaseOf } from "./channel-posts.js";
18
16
  import { expandHome } from "../shared/paths.js";
17
+ import { inboxName, saveToInbox, sniffExt } from "./media.js";
19
18
  import { ChannelStore } from "./channel-store.js";
20
- import { formatChannelContext, pickQuery, wantsHistory, withoutMsg } from "./channel-context.js";
21
19
  const TOPIC_EVENT = "aibot_topic_event_callback";
22
20
  const CMD_POST = "aibot_topic_send_post";
23
21
  const CMD_REPLY = "aibot_topic_send_reply";
@@ -27,25 +25,130 @@ const CMD_SEARCH = "aibot_topic_search";
27
25
  const CMD_SEARCH_RESULT = "get_search_result";
28
26
  /** 已知 topic_id。sendMessage 用它区分「频道」和普通群 chatid。 */
29
27
  const topics = new Set();
30
- /** 入站事件 msgid → 它所属的帖子;replyStream 用 streamId 找回来。 */
31
- const byStream = new Map();
32
- /** topic 上最近一条帖子,主动消息尽量挂成它的评论。 */
33
- const lastPost = new Map();
28
+ /** 把 topic_id 登记为已知频道 (onTopic 里自动做; 导出给测试 / 显式注册面用)。 */
29
+ export const registerTopic = (topicId) => { topics.add(topicId); };
34
30
  /** 频道里最近一个说话的人;审批卡改投他的私聊,频道不支持按钮卡片。 */
35
31
  const lastAsker = new Map();
36
32
  /** 事件可能重投;msgid 去重表有界。 */
37
33
  const seen = new Set();
38
- /** 已经交给主 wizard 的 post / reply;判官与直接 @ 可能同时到,谁先到谁答。 */
39
- const dispatched = new Set();
40
- /** post_id → 是否收到过 topic_at_bot。只有被 @ 过的帖子才能用 send_reply 评论;
41
- * 未被 @ 的新帖只能 send_post 主动发(官方“回复被@的内容” vs “主动发内容”)。 */
42
- const replyOk = new Map();
43
- /** 判官串行:同一 topic 一次只跑一个判断,一次 -p 进程更不该并发抢占。 */
44
- const judgeQueues = new Map();
34
+ /** 企微原生入站已带上的频道 @ (aibot_msg_callback, compat_info.source=channel): asker → 到达记录。
35
+ * 企微把频道里 @ 机器人的话**两路**同时推: 原生帧 (chatid = 该帖子自己的 opk… 群 id) 与
36
+ * topic_at_bot 事件 (topic_id = 频道)。原生帧由 inbound 记一笔, topic_at_bot 等一拍认领
37
+ * (一帧只抵一个事件); 等不到 (企微没推原生帧) 才由桥自己落一行, 兜底不丢话。 */
38
+ const nativeFrames = new Map();
39
+ const NATIVE_WAIT_MS = 3000;
40
+ /** 原生帧可能比事件先到: 认领窗口往前放这么多。 */
41
+ const NATIVE_SKEW_MS = 2000;
42
+ /** inbound 收到原生频道帧时调: 记下发话人与帖子 chatid。 */
43
+ export const noteNativeChannelFrame = (asker, chat, at = Date.now()) => {
44
+ if (!asker)
45
+ return;
46
+ const fresh = (nativeFrames.get(asker) ?? []).filter((n) => at - n.at < NATIVE_WAIT_MS + NATIVE_SKEW_MS);
47
+ nativeFrames.set(asker, [...fresh, { at, chat }]);
48
+ };
49
+ /** 认领一条 since 之后到达的原生帧 (认领即消耗); 没有 = ""。 */
50
+ const takeNative = (asker, since) => {
51
+ const list = nativeFrames.get(asker) ?? [];
52
+ const i = list.findIndex((n) => n.at >= since);
53
+ if (i < 0)
54
+ return "";
55
+ nativeFrames.set(asker, list.filter((_, j) => j !== i));
56
+ return list[i].chat;
57
+ };
58
+ /** 最多等 waitMs 看原生帧到没到。 */
59
+ const awaitNative = async (asker, since, waitMs) => {
60
+ for (let left = waitMs;; left -= 100) {
61
+ const hit = takeNative(asker, since);
62
+ if (hit || left <= 0)
63
+ return hit;
64
+ await sleep(100);
65
+ }
66
+ };
45
67
  const MENTION_RE = /\$\{inline_mention(?::[^}]*)?\}/g;
46
68
  const FILE_RE = /\$\{inline_file(?::[^}]*)?\}/g;
47
69
  const cleanContent = (s) => String(s ?? "").replace(MENTION_RE, "").replace(FILE_RE, "").trim();
48
70
  const clip = (s, n = 300) => (s.length > n ? s.slice(0, n) + "…" : s);
71
+ const NO_MEDIA = { images: [], files: [] };
72
+ /** ChannelHuman 的媒体字段: 没有就不写 (账本行不为空字段多一截)。 */
73
+ const mediaOf = (m) => ({
74
+ ...(m.images.length ? { images: m.images } : {}), ...(m.files.length ? { files: m.files } : {}),
75
+ });
76
+ /** 回包里有几份媒体 (纯读字段, 不下载) —— 日志与「空正文但有图」的判定用。 */
77
+ export const mediaCountOf = (item) => {
78
+ const it = item;
79
+ return {
80
+ images: Array.isArray(it?.image_list) ? it.image_list.length : 0,
81
+ files: Array.isArray(it?.inline_file_list) ? it.inline_file_list.length : 0,
82
+ };
83
+ };
84
+ const cookieOf = (x) => x.cookie_name && x.cookie_value ? { cookieName: String(x.cookie_name), cookieValue: String(x.cookie_value) } : {};
85
+ const mediaRefsOf = (item) => {
86
+ const it = item;
87
+ const images = (Array.isArray(it?.image_list) ? it.image_list : [])
88
+ .filter((m) => m?.download_url)
89
+ .map((m) => ({ url: String(m.download_url), ...cookieOf(m) }));
90
+ const files = (Array.isArray(it?.inline_file_list) ? it.inline_file_list : [])
91
+ .filter((f) => f?.download_url)
92
+ .map((f) => ({ url: String(f.download_url), ...cookieOf(f), name: String(f.file_name || "file") }));
93
+ return { images, files };
94
+ };
95
+ /** 拉一份频道媒体: 普通 HTTPS GET + cookie (频道素材没有 aeskey, 不是长连接加密媒体,
96
+ * 不能走 client.downloadFile)。失败只 warn —— 一条拉不到不该把整句话吞掉。 */
97
+ const fetchMedia = async (ref, log, msgid) => {
98
+ try {
99
+ const res = await fetch(ref.url, {
100
+ headers: ref.cookieName && ref.cookieValue ? { cookie: ref.cookieName + "=" + ref.cookieValue } : {},
101
+ signal: AbortSignal.timeout(20_000),
102
+ });
103
+ if (!res.ok) {
104
+ log.warn({ msgid, status: res.status }, "channel media: http not ok");
105
+ return undefined;
106
+ }
107
+ return Buffer.from(await res.arrayBuffer());
108
+ }
109
+ catch (e) {
110
+ log.warn({ msgid, err: e.message }, "channel media: download failed");
111
+ return undefined;
112
+ }
113
+ };
114
+ const downloadMedia = async (cfg, log, item, msgid) => {
115
+ const refs = mediaRefsOf(item);
116
+ const n = mediaCountOf(item);
117
+ if (!refs.images.length && !refs.files.length) {
118
+ // debug 口径: 回包到底带没带媒体 (字段长度), 用来在真机上确认企微真的把图放在 image_list 里。
119
+ if (n.images || n.files)
120
+ log.warn({ msgid, ...n }, "channel media: refs unreadable (no download_url)");
121
+ return NO_MEDIA;
122
+ }
123
+ const dir = expandHome(cfg.wrc.mirror.inboxDir);
124
+ const images = [];
125
+ const files = [];
126
+ let i = 0;
127
+ for (const ref of refs.images) {
128
+ const buf = await fetchMedia(ref, log, msgid);
129
+ if (!buf)
130
+ continue;
131
+ const saved = saveToInbox(dir, inboxName(msgid, i++, "image" + sniffExt(buf)), buf);
132
+ if ("reason" in saved) {
133
+ log.warn({ msgid, reason: saved.reason }, "channel media: not saved");
134
+ continue;
135
+ }
136
+ images.push(saved.path);
137
+ }
138
+ for (const ref of refs.files) {
139
+ const buf = await fetchMedia(ref, log, msgid);
140
+ if (!buf)
141
+ continue;
142
+ const saved = saveToInbox(dir, inboxName(msgid, i++, ref.name), buf);
143
+ if ("reason" in saved) {
144
+ log.warn({ msgid, reason: saved.reason }, "channel media: not saved");
145
+ continue;
146
+ }
147
+ files.push({ path: saved.path, name: ref.name });
148
+ }
149
+ log.info({ msgid, ...n, savedImages: images.length, savedFiles: files.length }, "channel media saved");
150
+ return { images, files };
151
+ };
49
152
  const replyBody = (frame) => frame?.body ?? frame;
50
153
  const call = async (client, cmd, body) => {
51
154
  // reply() 只是「带 cmd 的 WS 请求」;req_id 自造,服务端回执按它匹配。
@@ -82,7 +185,7 @@ const ensureStore = (cfg) => {
82
185
  const h = cfg.wrc.channel.history;
83
186
  if (!h.enabled)
84
187
  return undefined;
85
- const key = `${cfg.daemon.stateDir}|${h.keepDays}|${h.maxMessages}`;
188
+ const key = cfg.daemon.stateDir + "|" + h.keepDays + "|" + h.maxMessages;
86
189
  if (!store || storeKey !== key) {
87
190
  store = new ChannelStore({
88
191
  dir: join(expandHome(cfg.daemon.stateDir), "channels"),
@@ -103,23 +206,40 @@ const atOf = (t) => {
103
206
  };
104
207
  const authorOf = (creator) => {
105
208
  const c = creator ?? {};
106
- return Number(c.type) === 1 ? `bot:${String(c.aibotid ?? "?").slice(0, 10)}` : String(c.userid ?? "user");
209
+ return Number(c.type) === 1 ? "bot:" + String(c.aibotid ?? "?").slice(0, 10) : String(c.userid ?? "user");
107
210
  };
108
- const postMsg = (p) => {
211
+ const postMsg = (p, media = NO_MEDIA) => {
109
212
  const id = String(p?.post_id ?? "");
110
213
  const text = cleanContent(p?.content);
111
- if (!id || !text)
214
+ if (!id || (!text && !media.images.length && !media.files.length))
112
215
  return undefined;
113
- return { id, kind: "post", postId: id, at: atOf(p?.create_time), author: authorOf(p?.creator), text };
216
+ return { id, kind: "post", postId: id, at: atOf(p?.create_time), author: authorOf(p?.creator), text,
217
+ ...(media.images.length ? { images: media.images } : {}), ...(media.files.length ? { files: media.files } : {}) };
114
218
  };
115
- const replyMsg = (r, postId) => {
219
+ const replyMsg = (r, postId, media = NO_MEDIA) => {
116
220
  const id = String(r?.reply_id ?? "");
117
221
  const text = cleanContent(r?.content);
118
- if (!id || !text)
222
+ if (!id || (!text && !media.images.length && !media.files.length))
119
223
  return undefined;
120
- return { id, kind: "reply", postId: String(r?.post_id ?? postId), at: atOf(r?.create_time), author: authorOf(r?.creator), text };
224
+ return { id, kind: "reply", postId: String(r?.post_id ?? postId), at: atOf(r?.create_time), author: authorOf(r?.creator), text,
225
+ ...(media.images.length ? { images: media.images } : {}), ...(media.files.length ? { files: media.files } : {}) };
121
226
  };
122
- /** 记一条进本地缓存: 判官上下文 / 主 wizard 上下文 / read_channel 共用这一份。 */
227
+ /** 最近消息里活跃的其他机器人 (§6 邻居感知): bot:<id> → 条数, 自己 (selfBot) 已剔除。 */
228
+ export const botAuthors = (msgs, selfBot = "") => {
229
+ const self = selfBot ? "bot:" + selfBot.slice(0, 10) : "";
230
+ const n = new Map();
231
+ for (const m of msgs) {
232
+ const a = m?.author ?? "";
233
+ if (!a.startsWith("bot:") || a === self || a === "bot:?")
234
+ continue;
235
+ n.set(a, (n.get(a) ?? 0) + 1);
236
+ }
237
+ return [...n.entries()]
238
+ .sort((a, b) => b[1] - a[1] || (a[0] < b[0] ? -1 : 1))
239
+ .slice(0, 8)
240
+ .map(([id, count]) => ({ id, n: count }));
241
+ };
242
+ /** 记一条进本地缓存: read_channel / 回填共用这一份。 */
123
243
  const record = async (cfg, topicId, msg) => {
124
244
  if (!msg)
125
245
  return;
@@ -135,18 +255,10 @@ const backfill = async (client, cfg, log, topicId) => {
135
255
  if ((await s.count(topicId)) >= h.mainPosts)
136
256
  return;
137
257
  const posts = await fetchRecent(client, topicId, h.mainPosts, log);
138
- for (const m of posts.map(postMsg).filter((x) => !!x))
258
+ for (const m of posts.map((p) => postMsg(p)).filter((x) => !!x))
139
259
  await s.append(topicId, m);
140
260
  log.info({ topicId, n: posts.length }, "channel history backfilled");
141
261
  };
142
- /** 判官 / 上下文要看的最近帖子: 本地够用就不打接口。 */
143
- const recentPosts = async (client, cfg, log, topicId, limit) => {
144
- const local = (await ensureStore(cfg)?.recentPosts(topicId, limit)) ?? [];
145
- if (local.length >= Math.min(limit, 3))
146
- return local;
147
- const posts = await fetchRecent(client, topicId, limit, log);
148
- return posts.map(postMsg).filter((m) => !!m);
149
- };
150
262
  /** 官方搜索: 两步 (search 出 jobid → get_search_result 取摘要), 摘要只是 ID, 正文按 post 再取一次。 */
151
263
  const searchRemote = async (d, topicId, query, limit) => {
152
264
  const body = await call(d.client, CMD_SEARCH, { keyword_list: [query], topic_id_list: [topicId], limit });
@@ -175,7 +287,7 @@ const searchRemote = async (d, topicId, query, limit) => {
175
287
  return [];
176
288
  const b = await call(d.client, CMD_GET_POSTS, { topic_id: topicId, type: 0, post_id_list: ids.slice(0, Math.max(1, limit)) });
177
289
  const posts = b?.post_list ?? [];
178
- return posts.map(postMsg).filter((m) => !!m);
290
+ return posts.map((p) => postMsg(p)).filter((m) => !!m);
179
291
  };
180
292
  /** 先本地 grep (便宜、立刻), 不够再走官方搜索 (语义索引只在服务端)。 */
181
293
  const searchChannel = async (d, topicId, query, limit) => {
@@ -188,269 +300,96 @@ const searchChannel = async (d, topicId, query, limit) => {
188
300
  const remote = (await searchRemote(d, topicId, query, limit)).filter((m) => !seen.has(m.id));
189
301
  return [...local, ...remote].slice(0, limit);
190
302
  };
191
- /** 主 wizard 的上下文: 当前线程 + 最近 + (这句话有指代时才) 搜索命中。
192
- * 入站帧的身份 (topic / post / msgid) 永远写在首行 —— 即使 history 关掉、没有历史可带,
193
- * 主 agent 也必须能指回原始消息。 */
194
- const contextFor = async (d, topicId, postId, triggerId, text, extra = {}) => {
195
- const h = d.cfg.wrc.channel.history;
196
- // 触发这条上下文的回复 id 与线程根不同 (评论) 时一并带上: 目标 agent 要能指回
197
- // 那一句评论, 而不只是它所属的帖子。
198
- const replyId = triggerId && triggerId !== postId ? triggerId : "";
199
- const id = { topicId, postId, ...(replyId ? { replyId } : {}), ...(extra.msgid ? { msgid: extra.msgid } : {}) };
200
- const s = ensureStore(d.cfg);
201
- if (!s)
202
- return formatChannelContext({ ...id, budget: h.contextChars });
203
- const thread = withoutMsg(await s.thread(topicId, postId, h.mainReplies), triggerId);
204
- const recent = withoutMsg(await s.recentPosts(topicId, h.mainPosts), triggerId);
205
- const query = h.searchLimit > 0 && wantsHistory(text) ? pickQuery(text) : "";
206
- const hits = query ? (await searchChannel(d, topicId, query, h.searchLimit)).filter((m) => m.id !== triggerId) : [];
207
- return formatChannelContext({
208
- ...id,
209
- thread,
210
- recent,
211
- hits,
212
- ...(query ? { query } : {}),
213
- ...(extra.reason ? { reason: extra.reason } : {}),
214
- ...(extra.intent ? { intent: extra.intent } : {}),
215
- budget: h.contextChars,
216
- });
217
- };
218
303
  const stamp = (ms) => {
219
304
  const d = new Date(ms);
220
- return `${String(d.getHours()).padStart(2, "0")}:${String(d.getMinutes()).padStart(2, "0")}`;
305
+ const hh = String(d.getHours()).padStart(2, "0");
306
+ const mm = String(d.getMinutes()).padStart(2, "0");
307
+ return hh + ":" + mm;
221
308
  };
222
- const renderLines = (list) => list.map((m) => `[${stamp(m.at)}] ${m.author}: ${clip(m.text, 400)}`).join("\n");
309
+ /** read_channel 的一行: 正文 + 媒体 (图片 / 文件给本地路径, agent 直接 Read)。 */
310
+ const mediaTag = (m) => [...(m.images ?? []).map((p) => "[图: " + p + "]"), ...(m.files ?? []).map((f) => "[文件: " + f.name + " " + f.path + "]")].join(" ");
311
+ const renderLines = (list) => list.map((m) => {
312
+ const tag = mediaTag(m);
313
+ return "[" + stamp(m.at) + "] " + m.author + ": " + clip(m.text, 400) + (tag ? " " + tag : "");
314
+ }).join("\n");
223
315
  /** read_channel 的守护进程侧: 本地缓存优先, 搜不到 / 没缓存才问官方。 */
224
316
  export const readChannel = async (d, req) => {
225
317
  const topicId = req.topic.replace(/^chat:/, "").trim();
226
318
  if (!topicId)
227
- return { ok: false, text: "给一个频道: `chat:<topic_id>` 或裸 topic_id" };
319
+ return { ok: false, text: "给一个频道: chat:<topic_id> 或裸 topic_id" };
228
320
  const limit = Math.min(Math.max(Number(req.limit ?? 20) || 20, 1), 100);
229
321
  const query = (req.query ?? "").trim();
230
322
  if (query) {
231
323
  const hits = await searchChannel(d, topicId, query, limit);
232
- return { ok: true, text: hits.length ? `频道 ${topicId} · 搜「${query}」· ${hits.length} 条\n${renderLines(hits)}` : `频道 ${topicId} 没搜到「${query}」` };
324
+ return { ok: true, text: hits.length ? "频道 " + topicId + " · 搜「" + query + "」· " + hits.length + " 条\n" + renderLines(hits) : "频道 " + topicId + " 没搜到「" + query + "」" };
233
325
  }
234
326
  const local = (await ensureStore(d.cfg)?.slice(topicId, { ...(req.since !== undefined ? { since: req.since } : {}), ...(req.until !== undefined ? { until: req.until } : {}), limit })) ?? [];
235
327
  const list = local.length
236
328
  ? local
237
- : (await fetchRecent(d.client, topicId, Math.min(limit, 20), d.log)).map(postMsg).filter((m) => !!m);
238
- return { ok: true, text: list.length ? `频道 ${topicId} · ${list.length} 条\n${renderLines(list)}` : `频道 ${topicId} 没有读到消息 (没见过这个频道, 或还没缓存)` };
329
+ : (await fetchRecent(d.client, topicId, Math.min(limit, 20), d.log)).map((p) => postMsg(p)).filter((m) => !!m);
330
+ return { ok: true, text: list.length ? "频道 " + topicId + " · " + list.length + " 条\n" + renderLines(list) : "频道 " + topicId + " 没有读到消息 (没见过这个频道, 或还没缓存)" };
239
331
  };
240
- const emptyFrame = () => ({ headers: { req_id: "" } });
241
- const serial = (key, job) => {
242
- const prev = judgeQueues.get(key) ?? Promise.resolve();
243
- const next = prev.then(job, job);
244
- judgeQueues.set(key, next.catch(() => undefined).finally(() => {
245
- if (judgeQueues.get(key) === next)
246
- judgeQueues.delete(key);
247
- }));
248
- return next;
249
- };
250
- /** 逐个 `{` 数括号取 JSON 对象 (跳过字符串里的括号),比非贪婪正则可容下 rewrite 里的代码块。 */
251
- const jsonObjects = (text) => {
252
- const out = [];
253
- for (let i = 0; i < text.length; i += 1) {
254
- if (text[i] !== "{")
255
- continue;
256
- let depth = 0;
257
- let inStr = false;
258
- let esc = false;
259
- for (let j = i; j < text.length; j += 1) {
260
- const c = text[j];
261
- if (inStr) {
262
- if (esc)
263
- esc = false;
264
- else if (c === "\\")
265
- esc = true;
266
- else if (c === '"')
267
- inStr = false;
268
- }
269
- else if (c === '"')
270
- inStr = true;
271
- else if (c === "{")
272
- depth += 1;
273
- else if (c === "}") {
274
- depth -= 1;
275
- if (depth === 0) {
276
- out.push(text.slice(i, j + 1));
277
- i = j;
278
- break;
279
- }
280
- }
332
+ // 出站传输补丁必须早于 installResponseTracker 安装:tracker 会把当时的
333
+ // client.sendMessage / replyStream 抓成 orig*,晚装就绕过了频道分支。
334
+ /** base principal 是否是一个已登记的频道 (chat:<topic_id>)。 */
335
+ export const isTopicChat = (base) => base.startsWith("chat:") && topics.has(base.slice("chat:".length));
336
+ /** 频道 room 的出站: 一律发新帖 (send_post)。 */
337
+ /** 频道 room 的出站选择 (§2.3): 交代 / 回话挂在那条评论行上 (原话行带 post) → 评论回那个帖子;
338
+ * 其余 (chat_say 主动说 / re 的不是评论) → 发新帖。选择编码在动作里, 不给工具加参数。
339
+ * postOf = 按消息 id 查那条原话行的频道 post_id (频道 room 的人话行带, 帖子 room 的行不带)。 */
340
+ export const channelOutboundOf = (row, postOf) => {
341
+ if (row.kind === "reply")
342
+ for (const id of row.re ?? []) {
343
+ const post = postOf(id);
344
+ if (post)
345
+ return { action: "reply", post };
281
346
  }
282
- }
283
- return out;
347
+ return { action: "post" };
284
348
  };
285
- const asText = (v) => (typeof v === "string" ? v.trim() : "");
286
- /** 字段容错: 只认字符串 (对象 / 数组当没写),并按第一个非空别名取值 —— 模型多吐、少吐都不炸。 */
287
- const asField = (o, keys) => keys.map((k) => asText(o[k])).find(Boolean) ?? "";
288
- /** respond 也容忍模型写成字符串 "true" / "false"。 */
289
- const asReply = (v) => typeof v === "boolean" ? v : v === "true" ? true : v === "false" ? false : undefined;
290
- export const parseVerdict = (text) => {
291
- for (const b of jsonObjects(text).reverse()) {
292
- try {
293
- const o = JSON.parse(b);
294
- const respond = asReply(o.respond);
295
- if (respond === undefined)
296
- continue;
297
- const intent = asField(o, ["intent", "意图"]);
298
- const rewrite = asField(o, ["rewrite", "rewritten", "instruction", "改写"]);
299
- return {
300
- respond,
301
- reason: asField(o, ["reason", "理由"]),
302
- ...(respond && intent ? { intent } : {}),
303
- ...(respond && rewrite ? { rewrite } : {}),
304
- };
305
- }
306
- catch { /* 不是这一块 */ }
307
- }
308
- const plain = text.trim();
309
- if (/^yes\b/i.test(plain))
310
- return { respond: true, reason: plain.slice(0, 120) };
311
- if (/^no\b/i.test(plain))
312
- return { respond: false, reason: plain.slice(0, 120) };
313
- return undefined;
349
+ const topicBody = (content, media) => {
350
+ const files = media?.files ?? [];
351
+ const inline = files.map((f, i) => ({ placeholder: "p" + (i + 1), media_id: f.mediaId }));
352
+ const withInline = files.length
353
+ ? content + "\n" + files.map((_, i) => "${inline_file:p" + (i + 1) + "}").join(" ")
354
+ : content;
355
+ const images = (media?.imageIds ?? []).map((id) => ({ media_id: id }));
356
+ return { content: withInline, ...(images.length ? { image_list: images } : {}), ...(inline.length ? { inline_file_list: inline } : {}) };
314
357
  };
315
- /** 判官放行后交给主 agent 的正文: 改写优先, 原文永远可回溯。
316
- * 改写缺失 / 为空 / 与原文相同 → 原样用原帖, 改写这一层坏了也不丢消息。 */
317
- const bodyFor = (text, verdict) => {
318
- const rewrite = verdict.respond ? (verdict.rewrite ?? "").trim() : "";
319
- if (!rewrite || rewrite === text.trim())
320
- return text;
321
- return `${rewrite}\n——\n原帖:${text}`;
358
+ export const sendTopicPost = async (client, log, topicId, content, media) => {
359
+ const body = await call(client, CMD_POST, { topic_id: topicId, ...topicBody(content, media) });
360
+ if (body?.errcode)
361
+ log.warn({ errcode: body.errcode, errmsg: body.errmsg, topicId }, "channel tool post rejected");
362
+ else
363
+ log.info({ topicId, via: "post", len: content.length }, "channel tool sent");
364
+ return "post";
322
365
  };
323
- /** 判官是一次性判定, 不是一个住下来的会话: 一个 `-p` 进程进, stdout 出一行 JSON。
324
- * 不落会话 (--no-session-persistence)、不给工具 (--tools "")、不带 --settings /
325
- * --session-id —— 判官不该在 projects/ 里留痕被当成可镜像的会话, 也不该出审批卡。 */
326
- export const runJudgeOnce = (deps, prompt) => {
327
- const { cfg, log } = deps;
328
- const backend = resolveCliBackend(cfg.wrc);
329
- if (!backend.oneShotArgs) {
330
- log.warn({ backend: backend.name }, "channel judge: backend has no one-shot mode");
331
- return Promise.resolve(undefined);
366
+ /** 帖子 room 的出站: 一律评论到根帖 (send_reply)。企微拒 (errcode) 时回 "rejected" —— 调用方决定回退。 */
367
+ export const sendTopicReply = async (client, log, topicId, postId, content, media) => {
368
+ const body = await call(client, CMD_REPLY, { topic_id: topicId, post_id: postId, ...topicBody(content, media) });
369
+ if (body?.errcode) {
370
+ log.warn({ errcode: body.errcode, errmsg: body.errmsg, topicId, postId }, "channel tool reply rejected");
371
+ return "rejected";
332
372
  }
333
- const pick = pickTier(cfg, cfg.wrc.channel.judge.tier, {});
334
- const args = backend.oneShotArgs(prompt, {
335
- ...(pick.ok && pick.model ? { model: pick.model } : {}),
336
- ...(pick.ok && pick.effort ? { effort: pick.effort } : {}),
337
- });
338
- const timeoutMs = cfg.wrc.channel.judge.timeoutMs;
339
- return new Promise((resolve) => {
340
- const started = Date.now();
341
- const proc = spawn(backend.bin, args, {
342
- cwd: expandHome(cfg.wrc.cwd),
343
- env: { ...process.env, PATH: augmentedPath(process.env.PATH) },
344
- stdio: ["ignore", "pipe", "pipe"],
345
- });
346
- let out = "";
347
- let errTail = "";
348
- let settled = false;
349
- const finish = (text, why) => {
350
- if (settled)
351
- return;
352
- settled = true;
353
- clearTimeout(timer);
354
- const ms = Date.now() - started;
355
- if (text)
356
- log.info({ ms, tier: pick.ok ? pick.tier : undefined, model: pick.ok ? pick.model : undefined }, "channel judge ran");
357
- else
358
- log.warn({ ms, why, bin: backend.bin, stderrTail: errTail.slice(-200) }, "channel judge: no verdict");
359
- resolve(text);
360
- };
361
- const timer = setTimeout(() => {
362
- proc.kill("SIGKILL");
363
- finish(undefined, `timeout ${timeoutMs}ms`);
364
- }, timeoutMs);
365
- proc.stdout?.on("data", (c) => { out = (out + c.toString("utf8")).slice(-8000); });
366
- proc.stderr?.on("data", (c) => { errTail = (errTail + c.toString("utf8")).slice(-800); });
367
- proc.on("error", (e) => finish(undefined, `spawn ${e.message}`));
368
- proc.on("close", (code) => {
369
- if (code !== 0)
370
- return finish(undefined, `exit ${code}`);
371
- finish(out.trim() || undefined, "empty output");
372
- });
373
- });
374
- };
375
- const askJudge = async (deps, prompt) => {
376
- const text = await runJudgeOnce(deps, prompt);
377
- if (!text)
378
- return undefined;
379
- const verdict = parseVerdict(text);
380
- if (!verdict)
381
- deps.log.warn({ chars: text.length, head: text.slice(0, 200) }, "channel judge: unparsable output");
382
- return verdict;
373
+ log.info({ topicId, postId, via: "reply", len: content.length }, "channel tool sent");
374
+ return "reply";
383
375
  };
384
- const botName = (cfg) => cfg.wrc.botNames.find(Boolean) ?? "wezard";
385
- /** 每条历史都带 id + 时刻: 判官只做判定不改身份, 但它引用的那一句必须能被
386
- * 目标 agent 直接指回去, 所以 id 不能被压缩成 "第 N 条"。 */
387
- const judgeLine = (m, mark = " ") => `${mark}${m.kind === "reply" ? "reply" : "post"}=${m.id} [${stamp(m.at)}] ${m.author}: ${clip(m.text, 300)}`;
388
- export const buildJudgePrompt = (cfg, i) => {
389
- const base = `chat:${i.topicId}`;
390
- const me = botName(cfg);
391
- const job = wizardStore()?.get(base)?.description ?? "这个频道的助手";
392
- const others = (wizardStore()?.all() ?? [])
393
- .filter((w) => baseOfKey(w.target) === base && w.target !== base && w.target !== keyOf(base, "judge"))
394
- .map((w) => `${w.name || w.target}${w.description ? `(${w.description})` : ""}`)
395
- .slice(0, 12);
396
- const isReply = i.triggerId && i.triggerId !== i.postId;
397
- return [
398
- `你是 ${me} 的频道判官。判断最新一条消息是否应该由 ${me} 回应;该回应时,把它整理成一条给主 agent 的指令。`,
399
- `定位:topic=${i.topicId} · thread post=${i.postId}${isReply ? ` · 本次消息 reply=${i.triggerId}` : ""} · msgid=${i.msgid}`,
400
- `你的职责:${job}`,
401
- others.length ? `频道里其他机器人:${others.join("、")}` : "",
402
- i.thread.length ? "这条消息所在的线程(父帖 + 评论,都带 id):" : "",
403
- ...i.thread.map((m) => judgeLine(m, " ")),
404
- i.recent.length ? "最近的频道消息(旧→新):" : "",
405
- ...i.recent.map((m, k) => judgeLine(m, `[${k + 1}] `)),
406
- `最新消息(${i.latestAuthor}):${clip(i.latest, 600)}`,
407
- "",
408
- "规则:",
409
- "- 最新消息 @ 了别的机器人、明显属于别人的职责、或只是闲聊 → respond=false",
410
- "- 最新消息在向本频道求助、需要你回答 → respond=true",
411
- "- 拿不准也 respond=false;不要重复别人已经回答的内容",
412
- "- respond=false 时不要写 intent / rewrite,留空字符串",
413
- "",
414
- "respond=true 时还要给出两步(只整理,不回答):",
415
- "1. intent:一句话说清这条消息想要什么(问什么 / 要你做什么 / 报了什么故障)。",
416
- "2. rewrite:把这条消息改写成一条主 agent 能直接执行的自洽指令 ——",
417
- " 补全省略的主语和指代对象(必要时代上线程里被回复的那一句),把零散的一句压成一条完整指令;",
418
- " 原文里的路径、命令、报错、数字、ID、链接一律原样保留,不添加原文没有的事实,不替用户补需求,不写答复内容;",
419
- " 用与原文相同的语言,只写改写后的正文,不要加「改写:」之类的前缀。",
420
- "",
421
- "这是一次性请求:不要调用工具,不要回答用户,不要解释。只输出下面这一行 JSON(不要代码围栏):",
422
- '{"respond":true|false,"reason":"一句话判断依据","intent":"一句话意图(respond=false 时留空)","rewrite":"改写后给主 agent 的指令正文(respond=false 时留空)"}',
423
- ].filter(Boolean).join("\n");
376
+ /** 评论回帖, 被企微拒时回退发新帖 (2026-11-23 人报纠正: 没 @ 过机器人的帖子实测也能评论, 早先
377
+ * 「只能评论 @ 过它的帖子」的说法不成立; 回退仅作真被拒的兜底, 不按 @ 与否预判。原先只 warn → 交代
378
+ * 静默丢失)。fallback 是新帖正文 (通常带引用标注); 没给就用原正文。 */
379
+ export const sendTopicReplyOrPost = async (client, log, topicId, postId, content, fallback, media) => {
380
+ const r = await sendTopicReply(client, log, topicId, postId, content, media);
381
+ if (r === "reply")
382
+ return "reply";
383
+ log.warn({ topicId, postId }, "channel reply rejected → fallback to post");
384
+ await sendTopicPost(client, log, topicId, fallback ?? content, media);
385
+ return "post";
424
386
  };
425
- // 出站传输补丁必须早于 installResponseTracker 安装:tracker 会把当时的
426
- // client.sendMessage / replyStream 抓成 orig*,晚装就绕过了频道分支。
427
387
  export const installChannelTransport = (client, log) => {
428
388
  log.info("channel transport installed");
429
- const sendPost = (topicId, content) => call(client, CMD_POST, { topic_id: topicId, content })
430
- .then((body) => {
431
- if (body?.errcode)
432
- log.warn({ errcode: body.errcode, errmsg: body.errmsg, topicId }, "channel post rejected");
433
- else
434
- log.info({ topicId, len: content.length }, "channel post sent");
435
- const id = String(body?.post_id ?? "");
436
- if (id)
437
- lastPost.set(topicId, id);
438
- return body;
439
- })
440
- .catch((e) => { log.warn({ err: e.message, topicId }, "channel post failed"); return undefined; });
441
- const sendReply = (ctx, content) => call(client, CMD_REPLY, { topic_id: ctx.topicId, post_id: ctx.postId, content })
442
- .then((body) => {
443
- if (body?.errcode)
444
- log.warn({ errcode: body.errcode, errmsg: body.errmsg, topicId: ctx.topicId }, "channel reply rejected");
445
- else
446
- log.info({ topicId: ctx.topicId, postId: ctx.postId, len: content.length }, "channel reply sent");
447
- return body;
448
- })
449
- .catch((e) => { log.warn({ err: e.message, topicId: ctx.topicId }, "channel reply failed"); return undefined; });
450
- // ── 出站:topic 不走群 chatid,改成频道帖子 / 评论 ──
389
+ // ── 出站:topic 不走群 chatid,改成频道新帖 ──
451
390
  const origSend = client.sendMessage.bind(client);
452
391
  client.sendMessage = (chatid, body) => {
453
- // mirror 有时把 “chat:<topic>” 原样递进来;topic 集合里存的是裸 topic_id。
392
+ // mirror 有时把 "chat:<topic>" 原样递进来;topic 集合里存的是裸 topic_id。
454
393
  const bare = String(chatid).replace(/^(user|chat|group):/, "");
455
394
  const topicId = topics.has(chatid) ? chatid : topics.has(bare) ? bare : "";
456
395
  if (!topicId)
@@ -463,126 +402,31 @@ export const installChannelTransport = (client, log) => {
463
402
  return origSend(asker, body);
464
403
  }
465
404
  log.warn({ topicId }, "channel: template_card dropped (no asker)");
466
- return Promise.resolve(emptyFrame());
405
+ return Promise.resolve({ headers: { req_id: "" } });
467
406
  }
468
407
  const content = body?.msgtype === "markdown" ? String(body.markdown?.content ?? "") : "";
469
408
  if (!content) {
470
409
  log.warn({ topicId, msgtype: body?.msgtype }, "channel: unsupported outbound");
471
- return Promise.resolve(emptyFrame());
410
+ return Promise.resolve({ headers: { req_id: "" } });
472
411
  }
473
- const postId = lastPost.get(topicId);
474
- // 只有被 @ 过的帖子才能评论;未被 @ 的新帖只能主动发一条新帖。
475
- return postId && replyOk.get(postId) === true
476
- ? sendReply({ topicId, postId, asker: lastAsker.get(topicId) ?? "" }, content)
477
- : sendPost(topicId, content);
478
- };
479
- // replyStream 的气泡只对群聊有意义;频道里把最终帧落成一条评论,中间帧丢掉。
480
- const origStream = client.replyStream.bind(client);
481
- client.replyStream = (frame, streamId, content, finish = false, ...rest) => {
482
- // 事件流里的 streamId 可能是 route eventId,而 fake frame 的 req_id 才是频道 msgid —— 两边都认。
483
- const ctx = byStream.get(streamId) ?? byStream.get(String(frame?.headers?.req_id ?? ""));
484
- if (!ctx)
485
- return origStream(frame, streamId, content, finish, ...rest);
486
- if (!finish)
487
- return Promise.resolve(emptyFrame());
488
- return replyOk.get(ctx.postId) === true ? sendReply(ctx, content) : sendPost(ctx.topicId, content);
412
+ // 频道 room 只发新帖; 帖子 room 不走这里 (chat:<opk…> 不在 topics, 由 index 的 roomMirror 走 send_reply)。
413
+ return sendTopicPost(client, log, topicId, content).then(() => ({ headers: { req_id: "" } }));
489
414
  };
490
415
  };
491
- export const installChannelEvents = (client, cfg, log) => {
416
+ export const installChannelEvents = (client, cfg, log, facts) => {
492
417
  const wsm = client.wsManager;
493
418
  if (!wsm || typeof wsm.handleFrame !== "function") {
494
419
  log.warn("channel: wsManager.handleFrame unavailable; topic events will be ignored");
495
420
  return;
496
421
  }
497
- const dispatch = (topicId, postId, msgid, fromUser, text, at, allowReply) => {
498
- if (dispatched.has(postId))
499
- return;
500
- dispatched.add(postId);
501
- if (dispatched.size > 3000)
502
- dispatched.delete(dispatched.values().next().value);
503
- replyOk.set(postId, allowReply);
504
- if (replyOk.size > 3000)
505
- replyOk.delete(replyOk.keys().next().value);
506
- lastPost.set(topicId, postId);
507
- byStream.set(msgid, { topicId, postId, asker: fromUser });
508
- if (byStream.size > 3000)
509
- byStream.delete(byStream.keys().next().value);
510
- const fake = {
511
- headers: { req_id: msgid },
512
- body: {
513
- msgid,
514
- chattype: "group",
515
- chatid: topicId,
516
- from: { userid: fromUser },
517
- create_time: at,
518
- msgtype: "text",
519
- text: { content: text },
520
- },
521
- };
522
- log.info({ topicId, postId, msgid, len: text.length }, "channel rx");
523
- client.emit("message.text", fake);
524
- };
525
- const mentions = (post) => {
526
- const list = post?.inline_mention_list ?? post?.mention_list ?? [];
527
- if (!Array.isArray(list))
528
- return [];
529
- return list.map((x) => {
530
- const m = x?.member ?? x ?? {};
531
- return { type: Number(m.type ?? 0), bot: typeof m.aibotid === "string" ? m.aibotid : undefined };
532
- });
533
- };
534
- const judgeAndMaybe = async (topicId, postId, msgid, fromUser, text, at, selfBot, triggerId) => {
535
- if (!cfg.wrc.channel.judge.enabled) {
536
- log.info({ topicId, postId }, "channel judge disabled");
537
- return;
538
- }
539
- if (dispatched.has(postId)) {
540
- log.info({ topicId, postId }, "channel judge skipped: already dispatched");
422
+ /** 落一行人话; 没有 ingest (旧接线 / 测试桩) 就只记日志 —— 静默丢话是不允许的。 */
423
+ const ingest = (e) => {
424
+ if (!facts?.ingest) {
425
+ log.warn({ room: e.room, post: e.post, msgid: e.msgid }, "channel: no room ingest wired, message not delivered");
541
426
  return;
542
427
  }
543
- const post = await fetchPost(client, topicId, postId, log);
544
- const ms = mentions(post);
545
- // 不能因为“提到自己”就跳过:回复/引用机器人时 inline_mention 也可能带上自己,
546
- // 但这时不一定有 topic_at_bot。交给判官,dispatched 会保证不会双答。
547
- if (ms.some((m) => m.type === 1 && m.bot === selfBot)) {
548
- log.info({ topicId, postId }, "channel judge: self-mention, wait for topic_at_bot");
549
- // 先等一拍:真有 topic_at_bot 就让直接 @ 路径答(评论);没有才走判官。
550
- await sleep(1200);
551
- if (dispatched.has(postId)) {
552
- log.info({ topicId, postId }, "channel judge skipped: self-mention handled by topic_at_bot");
553
- return;
554
- }
555
- }
556
- if (ms.some((m) => m.type === 1 && m.bot && m.bot !== selfBot)) {
557
- log.info({ topicId, postId }, "channel judge skipped: addressed to another bot");
558
- return;
559
- }
560
- const recent = await recentPosts(client, cfg, log, topicId, cfg.wrc.channel.judge.maxPosts);
561
- const thread = withoutMsg((await ensureStore(cfg)?.thread(topicId, postId, 4)) ?? [], triggerId);
562
- const prompt = buildJudgePrompt(cfg, {
563
- topicId, postId, triggerId, msgid,
564
- recent: withoutMsg(recent, triggerId),
565
- thread,
566
- latest: text,
567
- latestAuthor: fromUser,
568
- });
569
- // 一次 -p 进程也串行:同一 topic 两条消息同时到不该各起一个, 判词会互相盖。
570
- const verdict = await serial(`judge:${topicId}`, () => askJudge({ cfg, log }, prompt));
571
- if (!verdict?.respond) {
572
- // 超时 / 进程失败 / 输出解析不了都落在这里: 判官是个减噪层, 它的结论是「不回应」。
573
- // 回落 = 不投递 (频道里不会有人收到这条), 但要留得下可查的一行 —— 不静默丢。
574
- log.warn({ topicId, postId, msgid, triggerId, reason: verdict?.reason ?? "timeout/unparsable" }, "channel judge: no dispatch (verdict absent or respond=false)");
575
- return;
576
- }
577
- log.info({ topicId, postId, reason: verdict.reason, intent: verdict.intent, rewritten: !!verdict.rewrite }, "channel judge: yes");
578
- // 判官说 YES 才拼展开版上下文: 线程 + 最近 + (这句话有指代时) 搜索命中。
579
- // 入站帧的 msgid 随首行一起下去 —— 判官只加意图 / 改写,不替换身份。
580
- const ctx = await contextFor({ client, cfg, log }, topicId, postId, triggerId, text, {
581
- reason: verdict.reason,
582
- msgid,
583
- ...(verdict.intent ? { intent: verdict.intent } : {}),
584
- });
585
- dispatch(topicId, postId, msgid, fromUser, `${ctx}\n——\n${bodyFor(text, verdict)}`, at, false);
428
+ log.info({ room: e.room, post: e.post, msgid: e.msgid, len: e.text.length, mention: e.mention }, "channel rx");
429
+ facts.ingest(e);
586
430
  };
587
431
  const onTopic = async (frame) => {
588
432
  const body = frame?.body ?? {};
@@ -590,16 +434,18 @@ export const installChannelEvents = (client, cfg, log) => {
590
434
  const topicId = String(body.topic_id ?? "");
591
435
  const from = body.from ?? {};
592
436
  const asker = String(from.userid ?? "");
593
- const selfBot = String(body.aibotid ?? "");
594
437
  const at = Number(body.create_time ?? 0) * 1000 || Date.now();
438
+ const recvAt = Date.now();
595
439
  if (!topicId)
596
440
  return;
597
- topics.add(topicId);
441
+ registerTopic(topicId);
598
442
  if (asker)
599
443
  lastAsker.set(topicId, asker);
444
+ facts?.channelSeen?.(topicId);
600
445
  // 第一次见到的频道从官方补最近 N 条 (reload / 断线空窗期的事件本地没有)。
601
446
  void backfill(client, cfg, log, topicId).catch((e) => log.warn({ err: e.message, topicId }, "channel backfill failed"));
602
447
  log.info({ topicId, type: ev.eventtype, postId: ev.topic_event?.post_id ?? ev.topic_reply_event?.post_id ?? ev.topic_at_bot?.post_id, asker }, "channel event");
448
+ const channelRoom = "chat:" + topicId;
603
449
  if (ev.eventtype === "topic_at_bot") {
604
450
  const postId = String(ev.topic_at_bot?.post_id ?? "");
605
451
  const replyId = String(ev.topic_at_bot?.reply_id ?? "");
@@ -611,18 +457,36 @@ export const installChannelEvents = (client, cfg, log) => {
611
457
  seen.add(msgid);
612
458
  if (seen.size > 3000)
613
459
  seen.delete(seen.values().next().value);
614
- if (dispatched.has(postId))
460
+ const native = await awaitNative(asker, recvAt - NATIVE_SKEW_MS, facts?.nativeWaitMs ?? NATIVE_WAIT_MS);
461
+ if (native) {
462
+ // 原生帧已把这句话投给帖子会话 (= 帖子 room 的那条人话); 桥只记账。
463
+ // 配上的这一对就是「帖子 → 频道」的唯一判据 (原生帧里没有频道标识)。
464
+ if (learnChannelPost(cfg, native, topicId, postId)) {
465
+ log.info({ topicId, post: native }, "channel post learned");
466
+ facts?.postTouched?.(topicId, native);
467
+ }
468
+ log.info({ topicId, postId, msgid, nativeChat: native }, "channel at_bot: carried by native frame, bridge skip");
469
+ await record(cfg, topicId, postMsg(await fetchPost(client, topicId, postId, log)));
470
+ if (replyId)
471
+ await record(cfg, topicId, replyMsg(await fetchReply(client, topicId, postId, replyId, log), postId));
615
472
  return;
473
+ }
616
474
  const post = await fetchPost(client, topicId, postId, log);
617
475
  const reply = replyId ? await fetchReply(client, topicId, postId, replyId, log) : null;
618
476
  if (!post)
619
477
  log.warn({ topicId, postId }, "channel: post content not found");
620
- await record(cfg, topicId, postMsg(post));
478
+ const hit = reply ?? post;
479
+ const media = await downloadMedia(cfg, log, hit, msgid);
480
+ await record(cfg, topicId, postMsg(post, reply ? NO_MEDIA : media));
621
481
  if (reply)
622
- await record(cfg, topicId, replyMsg(reply, postId));
623
- const text = cleanContent(reply?.content ?? post?.content) || "(在频道里 @ 了你,没有正文)";
624
- const ctx = await contextFor({ client, cfg, log }, topicId, postId, replyId || postId, text, { msgid });
625
- dispatch(topicId, postId, msgid, asker, `${ctx}\n——\n${text}`, at, true);
482
+ await record(cfg, topicId, replyMsg(reply, postId, media));
483
+ const text = cleanContent(hit?.content) || "(在频道里 @ 了你,没有正文)";
484
+ // 兜底: 帖子已配对 → 帖子 room (它就是那个帖子); 没配对 → 频道 room。这一路是 topic_at_bot
485
+ // (人 @ 了机器人), 落到哪个 room 都一样记 mention:"bot" —— 直达 host, 不交给 room 的 monitor。
486
+ const paired = postBaseOf(cfg, topicId, postId);
487
+ ingest(paired
488
+ ? { room: paired, author: asker, text, at, msgid, mention: "bot", ...mediaOf(media) }
489
+ : { room: channelRoom, post: postId, author: asker, text, at, msgid, mention: "bot", ...mediaOf(media) });
626
490
  return;
627
491
  }
628
492
  if (ev.eventtype === "topic_event" && Number(ev.topic_event?.type ?? 0) === 1) {
@@ -630,8 +494,8 @@ export const installChannelEvents = (client, cfg, log) => {
630
494
  const msgid = String(body.msgid ?? postId);
631
495
  if (!postId || !msgid)
632
496
  return;
633
- if (seen.has(msgid) || dispatched.has(postId)) {
634
- log.info({ topicId, postId, msgid }, "channel topic_event skipped: seen/dispatched");
497
+ if (seen.has(msgid)) {
498
+ log.info({ topicId, postId, msgid }, "channel topic_event skipped: seen");
635
499
  return;
636
500
  }
637
501
  if (!asker) {
@@ -642,12 +506,13 @@ export const installChannelEvents = (client, cfg, log) => {
642
506
  if (seen.size > 3000)
643
507
  seen.delete(seen.values().next().value);
644
508
  const post = await fetchPost(client, topicId, postId, log);
645
- await record(cfg, topicId, postMsg(post));
509
+ const media = await downloadMedia(cfg, log, post, msgid);
510
+ await record(cfg, topicId, postMsg(post, media));
646
511
  const text = cleanContent(post?.content);
647
- if (!text)
512
+ if (!text && !media.images.length && !media.files.length)
648
513
  return;
649
- void judgeAndMaybe(topicId, postId, msgid, asker, text, at, selfBot, postId)
650
- .catch((e) => log.warn({ err: e.message, topicId, postId }, "channel judge failed"));
514
+ // 没被 @ 的新帖: 频道 room 的一行人话 (带 post); 不 @ 机器人就不过 host, 由 monitor 判激活。
515
+ ingest({ room: channelRoom, post: postId, author: asker, text, at, msgid, ...mediaOf(media) });
651
516
  return;
652
517
  }
653
518
  if (ev.eventtype === "topic_reply_event" && Number(ev.topic_reply_event?.type ?? 0) === 1) {
@@ -656,7 +521,7 @@ export const installChannelEvents = (client, cfg, log) => {
656
521
  const msgid = String(body.msgid ?? replyId);
657
522
  if (!postId || !replyId || !msgid)
658
523
  return;
659
- if (seen.has(replyId) || dispatched.has(replyId) || !asker) {
524
+ if (seen.has(replyId) || !asker) {
660
525
  log.info({ topicId, postId, replyId, msgid, asker }, "channel topic_reply_event skipped");
661
526
  return;
662
527
  }
@@ -664,12 +529,16 @@ export const installChannelEvents = (client, cfg, log) => {
664
529
  if (seen.size > 3000)
665
530
  seen.delete(seen.values().next().value);
666
531
  const reply = await fetchReply(client, topicId, postId, replyId, log);
667
- await record(cfg, topicId, replyMsg(reply, postId));
532
+ const media = await downloadMedia(cfg, log, reply, msgid);
533
+ await record(cfg, topicId, replyMsg(reply, postId, media));
668
534
  const text = cleanContent(reply?.content);
669
- if (!text)
535
+ if (!text && !media.images.length && !media.files.length)
670
536
  return;
671
- void judgeAndMaybe(topicId, postId, msgid, asker, text, at, selfBot, replyId)
672
- .catch((e) => log.warn({ err: e.message, topicId, postId }, "channel judge failed"));
537
+ // 评论: 帖子已配对 → 帖子 room; 还没配对 → 频道 room (带 post)。
538
+ const paired = postBaseOf(cfg, topicId, postId);
539
+ ingest(paired
540
+ ? { room: paired, author: asker, text, at, msgid, ...mediaOf(media) }
541
+ : { room: channelRoom, post: postId, author: asker, text, at, msgid, ...mediaOf(media) });
673
542
  return;
674
543
  }
675
544
  log.debug({ type: ev.eventtype, topicId }, "channel event ignored");
@@ -685,9 +554,4 @@ export const installChannelEvents = (client, cfg, log) => {
685
554
  prev(frame);
686
555
  };
687
556
  };
688
- // 兼容入口:新代码应分别早装 transport、晚装 events。
689
- export const installChannelBridge = (client, cfg, log) => {
690
- installChannelTransport(client, log);
691
- installChannelEvents(client, cfg, log);
692
- };
693
557
  //# sourceMappingURL=channel.js.map