squidclaw 3.0.3 → 3.0.4

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 (240) hide show
  1. package/dist/{audio-preflight-BTYxAJjy.js → audio-preflight-BKgdc7dS.js} +4 -4
  2. package/dist/{audio-preflight-Dkl6Z32z.js → audio-preflight-DpCWFB4z.js} +4 -4
  3. package/dist/{audio-transcription-runner-Gi_h5HEE.js → audio-transcription-runner-B2BdTEps.js} +1 -1
  4. package/dist/{audio-transcription-runner-DBkDgluo.js → audio-transcription-runner-BnbdYMDl.js} +1 -1
  5. package/dist/build-info.json +3 -3
  6. package/dist/bundled/boot-md/handler.js +6 -6
  7. package/dist/bundled/session-memory/handler.js +6 -6
  8. package/dist/canvas-host/a2ui/.bundle.hash +1 -1
  9. package/dist/{chrome-CAd6FQEn.js → chrome-BQDCalPp.js} +8 -8
  10. package/dist/{chrome-pBkBuWci.js → chrome-CjxCwFA9.js} +8 -8
  11. package/dist/{command-registry-CvgCFxfY.js → command-registry-CxiSVXru.js} +6 -6
  12. package/dist/{completion-cli-BGM1V6EN.js → completion-cli-s0fxD0OE.js} +2 -2
  13. package/dist/{completion-cli-OrgUDc2S.js → completion-cli-slzdOlRV.js} +1 -1
  14. package/dist/{config-cli-DbBvjvpS.js → config-cli-C0mk9eRl.js} +1 -1
  15. package/dist/{config-cli-uIP4r17f.js → config-cli-DhHEA_Nc.js} +1 -1
  16. package/dist/{configure-BGvoAfbs.js → configure-CeLdVVyh.js} +8 -1
  17. package/dist/{configure-BOsTXjBw.js → configure-FRd92XZ8.js} +8 -1
  18. package/dist/{deliver-DBXe-ZmL.js → deliver-D-f6Wa3i.js} +1 -1
  19. package/dist/{deliver-BJuiq0GS.js → deliver-aL8yOYS1.js} +1 -1
  20. package/dist/{deliver-runtime-DHKcNQzq.js → deliver-runtime-ChVR6sR3.js} +3 -3
  21. package/dist/{deliver-runtime-GlnBJNCj.js → deliver-runtime-_egya0QZ.js} +3 -3
  22. package/dist/{deps-send-whatsapp.runtime-DdxKewuy.js → deps-send-whatsapp.runtime-CZj97m5A.js} +7 -7
  23. package/dist/{deps-send-whatsapp.runtime-CslTuV47.js → deps-send-whatsapp.runtime-CiG6xd2e.js} +7 -7
  24. package/dist/{doctor-completion-42wcUATu.js → doctor-completion-I-WDZUs1.js} +1 -1
  25. package/dist/{doctor-completion-D6RGDBD5.js → doctor-completion-mnJOkoQ_.js} +1 -1
  26. package/dist/entry.js +2 -2
  27. package/dist/extensionAPI.js +6 -6
  28. package/dist/{gateway-cli-CG3mshpO.js → gateway-cli-B5rTNvd7.js} +1 -1
  29. package/dist/{gateway-cli-BbPfLLT6.js → gateway-cli-BHfkbI0u.js} +1 -1
  30. package/dist/{image-j_UomzVG.js → image-C-C7hQ26.js} +1 -1
  31. package/dist/{image-sRW3RpTY.js → image-DKZCmkET.js} +1 -1
  32. package/dist/{image-runtime-BNh3IfMj.js → image-runtime-D7n4dID4.js} +3 -3
  33. package/dist/{image-runtime-SUtf9jqh.js → image-runtime-poRypm-b.js} +3 -3
  34. package/dist/index.js +1 -1
  35. package/dist/llm-slug-generator.js +6 -6
  36. package/dist/{onboard-ChxvwUze.js → onboard-BP2Cr_Xy.js} +1 -1
  37. package/dist/{onboard-1KfKwvMR.js → onboard-CCJCvPgf.js} +1 -1
  38. package/dist/{onboarding-D6kMb3yv.js → onboarding-D5G87dvM.js} +1 -1
  39. package/dist/{onboarding-DuUMPrqA.js → onboarding-D7CIbxzd.js} +1 -1
  40. package/dist/{onboarding.finalize-KTOhO1-l.js → onboarding.finalize-B1MmYTFV.js} +4 -4
  41. package/dist/{onboarding.finalize-bphDUwZy.js → onboarding.finalize-Cxzl-fYU.js} +3 -3
  42. package/dist/{pi-embedded-BP2UlUm_.js → pi-embedded-CHzwPt6X.js} +24 -24
  43. package/dist/{pi-embedded-BGz_qdCc.js → pi-embedded-MktS4l8v.js} +24 -24
  44. package/dist/{pi-embedded-helpers-CmLnmKlb.js → pi-embedded-helpers-Bse_QhEf.js} +3 -3
  45. package/dist/{pi-embedded-helpers-BruaFB5l.js → pi-embedded-helpers-DYWYzEOC.js} +3 -3
  46. package/dist/plugin-sdk/accounts-BNuRM3rG.js +288 -0
  47. package/dist/plugin-sdk/accounts-CGTYP7Rh.js +46 -0
  48. package/dist/plugin-sdk/accounts-CcS9IAhD.js +35 -0
  49. package/dist/plugin-sdk/{accounts-YTdQYQFr.js → accounts-CxUSDHsT.js} +3 -3
  50. package/dist/plugin-sdk/{accounts-h__dTrLK.js → accounts-PSzw-z3S.js} +2 -2
  51. package/dist/plugin-sdk/{accounts-DghIDNk2.js → accounts-kr-Gz1hk.js} +2 -2
  52. package/dist/plugin-sdk/{active-listener-_PRYjtJv.js → active-listener-BQNrTcR3.js} +2 -2
  53. package/dist/plugin-sdk/active-listener-CTsLn1AX.js +50 -0
  54. package/dist/plugin-sdk/{api-key-rotation-mVDSAkKQ.js → api-key-rotation-Bhck7wki.js} +2 -2
  55. package/dist/plugin-sdk/api-key-rotation-DE4gr5YM.js +181 -0
  56. package/dist/plugin-sdk/audio-preflight-CRGLqp-g.js +69 -0
  57. package/dist/plugin-sdk/{audio-preflight-BZlQM-qX.js → audio-preflight-_xgGaeho.js} +26 -26
  58. package/dist/plugin-sdk/{audio-transcription-runner-CrYTX8py.js → audio-transcription-runner-Dwc0Eh-B.js} +11 -11
  59. package/dist/plugin-sdk/audio-transcription-runner-RXsskMMk.js +2176 -0
  60. package/dist/plugin-sdk/audit-membership-runtime-B9b-zRwg.js +58 -0
  61. package/dist/plugin-sdk/{audit-membership-runtime-Xl20kCBe.js → audit-membership-runtime-DHQDvH4u.js} +2 -2
  62. package/dist/plugin-sdk/{channel-activity-gwxRn4wF.js → channel-activity-XajEg_DL.js} +3 -3
  63. package/dist/plugin-sdk/channel-activity-gPvD1D7S.js +94 -0
  64. package/dist/plugin-sdk/{channel-web-1WF-Nabe.js → channel-web-KtqCp4mz.js} +18 -18
  65. package/dist/plugin-sdk/channel-web-LGl1zPJt.js +2256 -0
  66. package/dist/plugin-sdk/chrome-9Y_LcUg1.js +2415 -0
  67. package/dist/plugin-sdk/{chrome-BXbYwXRH.js → chrome-diV5m81I.js} +6 -6
  68. package/dist/plugin-sdk/commands-registry-CcdEPxVg.js +1125 -0
  69. package/dist/plugin-sdk/{commands-registry-0w-aZenK.js → commands-registry-DwZAJuut.js} +4 -4
  70. package/dist/plugin-sdk/{common-DBOCt6Yv.js → common-CqnO92P8.js} +2 -2
  71. package/dist/plugin-sdk/config-CrQ5bCrw.js +17912 -0
  72. package/dist/plugin-sdk/{config-pRtEoVyZ.js → config-DYbtdrsT.js} +7 -7
  73. package/dist/plugin-sdk/deliver-D3xr5AkB.js +1694 -0
  74. package/dist/plugin-sdk/{deliver-FjlJrtZk.js → deliver-DG_7Uagn.js} +10 -10
  75. package/dist/plugin-sdk/deliver-runtime-B79ZQu69.js +32 -0
  76. package/dist/plugin-sdk/deliver-runtime-BdTC7uKE.js +32 -0
  77. package/dist/plugin-sdk/deps-send-discord.runtime-BOQZIqC8.js +23 -0
  78. package/dist/plugin-sdk/deps-send-discord.runtime-CObCNMt3.js +23 -0
  79. package/dist/plugin-sdk/deps-send-imessage.runtime-CuHOc9Ka.js +22 -0
  80. package/dist/plugin-sdk/deps-send-imessage.runtime-DlWgi2DH.js +22 -0
  81. package/dist/plugin-sdk/deps-send-signal.runtime-Cz7FT8J8.js +21 -0
  82. package/dist/plugin-sdk/deps-send-signal.runtime-iPynghkE.js +21 -0
  83. package/dist/plugin-sdk/deps-send-slack.runtime-D4vDoRsg.js +19 -0
  84. package/dist/plugin-sdk/deps-send-slack.runtime-DNTbE5jS.js +19 -0
  85. package/dist/plugin-sdk/deps-send-telegram.runtime-7CR-xtCF.js +24 -0
  86. package/dist/plugin-sdk/deps-send-telegram.runtime-DjTVED_m.js +24 -0
  87. package/dist/plugin-sdk/deps-send-whatsapp.runtime-CRWOIKRC.js +57 -0
  88. package/dist/plugin-sdk/deps-send-whatsapp.runtime-bUi8kghi.js +57 -0
  89. package/dist/plugin-sdk/diagnostic-BXkLYs_9.js +319 -0
  90. package/dist/plugin-sdk/{diagnostic-Dt2i3afe.js → diagnostic-CT7v_kM2.js} +2 -2
  91. package/dist/plugin-sdk/{errors-CgRPdp3o.js → errors-9oVz7reJ.js} +1 -1
  92. package/dist/plugin-sdk/errors-B8oJXuCF.js +54 -0
  93. package/dist/plugin-sdk/fetch-guard-C55uvn27.js +156 -0
  94. package/dist/plugin-sdk/{fetch-guard-DyPZh8r2.js → fetch-guard-Or5BCq0E.js} +2 -2
  95. package/dist/plugin-sdk/{fs-safe-DqCO1D4C.js → fs-safe-DFbwq9CS.js} +3 -3
  96. package/dist/plugin-sdk/fs-safe-Dqmpk-Fr.js +352 -0
  97. package/dist/plugin-sdk/image-3xW7IJdq.js +2310 -0
  98. package/dist/plugin-sdk/image-ops-BjK2qZZn.js +584 -0
  99. package/dist/plugin-sdk/{image-ops-sw0uZ0GN.js → image-ops-CMsocOob.js} +2 -2
  100. package/dist/plugin-sdk/image-runtime-CZZJJqcW.js +25 -0
  101. package/dist/plugin-sdk/image-runtime-Cjz368oj.js +25 -0
  102. package/dist/plugin-sdk/{image-CQ9TZ9vq.js → image-rycGCqJO.js} +6 -6
  103. package/dist/plugin-sdk/index.js +50 -50
  104. package/dist/plugin-sdk/ir-CS7uuQhN.js +1296 -0
  105. package/dist/plugin-sdk/{ir-BVZ5kUMb.js → ir-DihI2SIz.js} +7 -7
  106. package/dist/plugin-sdk/{local-roots-fO3ZgW3G.js → local-roots-1xVosTZ4.js} +4 -4
  107. package/dist/plugin-sdk/local-roots-DmOKwiNW.js +186 -0
  108. package/dist/plugin-sdk/{logger-DIb2cGHp.js → logger-Bg4vIUJn.js} +2 -2
  109. package/dist/plugin-sdk/logger-DDdrdbDu.js +1163 -0
  110. package/dist/plugin-sdk/login-BSEeU27Y.js +57 -0
  111. package/dist/plugin-sdk/{login-Dg5cxB_3.js → login-YhFrVUWo.js} +4 -4
  112. package/dist/plugin-sdk/login-qr-BwWJsDSj.js +320 -0
  113. package/dist/plugin-sdk/{login-qr-C3Vn30cq.js → login-qr-SpUTuwYv.js} +5 -5
  114. package/dist/plugin-sdk/manager-DiXPCubI.js +3917 -0
  115. package/dist/plugin-sdk/{manager-BR-TwWTH.js → manager-DrzOPeMD.js} +8 -8
  116. package/dist/plugin-sdk/manager-runtime-CF55pBNe.js +15 -0
  117. package/dist/plugin-sdk/manager-runtime-Ct0m9UJC.js +15 -0
  118. package/dist/plugin-sdk/mattermost.js +3 -3
  119. package/dist/plugin-sdk/{outbound-1a3Z_QJ2.js → outbound-Cc4cUn9K.js} +5 -5
  120. package/dist/plugin-sdk/outbound-attachment-BoFx05zw.js +19 -0
  121. package/dist/plugin-sdk/{outbound-attachment-BTQjD4YE.js → outbound-attachment-Dtp3hQgc.js} +2 -2
  122. package/dist/plugin-sdk/outbound-cpqK1GFe.js +212 -0
  123. package/dist/plugin-sdk/{path-alias-guards-TnxupPQC.js → path-alias-guards-DA0MhfkG.js} +1 -1
  124. package/dist/plugin-sdk/path-alias-guards-gBhrAn14.js +43 -0
  125. package/dist/plugin-sdk/paths-C6W4VHoa.js +166 -0
  126. package/dist/plugin-sdk/{paths-B7_75Pdr.js → paths-CP67O8eN.js} +1 -1
  127. package/dist/plugin-sdk/{pi-embedded-helpers-DZRNadD8.js → pi-embedded-helpers-BDJ_4Plh.js} +16 -16
  128. package/dist/plugin-sdk/pi-embedded-helpers-C-B9B6Sp.js +9627 -0
  129. package/dist/plugin-sdk/pi-model-discovery-BGEeoPzN.js +134 -0
  130. package/dist/plugin-sdk/{pi-model-discovery-DGh6xekX.js → pi-model-discovery-Mk0GTDJl.js} +1 -1
  131. package/dist/plugin-sdk/pi-model-discovery-runtime-BHZ_Htob.js +8 -0
  132. package/dist/plugin-sdk/pi-model-discovery-runtime-BrwtJHPU.js +8 -0
  133. package/dist/plugin-sdk/pi-tools.before-tool-call.runtime-ByN_xThw.js +354 -0
  134. package/dist/plugin-sdk/{pi-tools.before-tool-call.runtime-BZ9XgG_x.js → pi-tools.before-tool-call.runtime-DV72wTDb.js} +4 -4
  135. package/dist/plugin-sdk/plugins-D5cdn70e.js +864 -0
  136. package/dist/plugin-sdk/{plugins-B8pWVYug.js → plugins-DSs2-fnK.js} +4 -4
  137. package/dist/plugin-sdk/{proxy-env-BOlkiW1-.js → proxy-env-Ib4-LUh-.js} +1 -1
  138. package/dist/plugin-sdk/{proxy-fetch-Dt5BedH8.js → proxy-fetch-Cf3IUSDw.js} +1 -1
  139. package/dist/plugin-sdk/proxy-fetch-ZPEvp58f.js +38 -0
  140. package/dist/plugin-sdk/pw-ai-C_QOIuin.js +1938 -0
  141. package/dist/plugin-sdk/{pw-ai-C17A1o4w.js → pw-ai-DIx2wpkY.js} +9 -9
  142. package/dist/plugin-sdk/qmd-manager-6bozlfFg.js +1448 -0
  143. package/dist/plugin-sdk/{qmd-manager-Bei6TaFq.js → qmd-manager-Ov9ElEfG.js} +7 -7
  144. package/dist/plugin-sdk/{query-expansion-POz2za8a.js → query-expansion-CzjwW461.js} +4 -4
  145. package/dist/plugin-sdk/query-expansion-eeVz_aEm.js +1011 -0
  146. package/dist/plugin-sdk/{redact-9WsNyb7S.js → redact-BoNEjbpF.js} +1 -1
  147. package/dist/plugin-sdk/redact-DfACyt0X.js +319 -0
  148. package/dist/plugin-sdk/reply-CQUX_haM.js +98828 -0
  149. package/dist/plugin-sdk/{reply-BFbijn6_.js → reply-CWWUd_JS.js} +73 -73
  150. package/dist/plugin-sdk/{resolve-outbound-target-B9iFEh0y.js → resolve-outbound-target-BOkvxZtM.js} +2 -2
  151. package/dist/plugin-sdk/resolve-outbound-target-Dbz0O8cR.js +40 -0
  152. package/dist/plugin-sdk/run-with-concurrency-5DMu9szx.js +1994 -0
  153. package/dist/plugin-sdk/{run-with-concurrency-DmTrN5JG.js → run-with-concurrency-kVooFCVo.js} +1 -1
  154. package/dist/plugin-sdk/runtime-whatsapp-login.runtime-DitS0I1z.js +10 -0
  155. package/dist/plugin-sdk/runtime-whatsapp-login.runtime-OthrtsLL.js +10 -0
  156. package/dist/plugin-sdk/runtime-whatsapp-outbound.runtime-CYCr6A3v.js +19 -0
  157. package/dist/plugin-sdk/runtime-whatsapp-outbound.runtime-Q2HL0zL3.js +19 -0
  158. package/dist/plugin-sdk/send-BACEu1Un.js +414 -0
  159. package/dist/plugin-sdk/{send-BGZo6HW1.js → send-BP1fSEBR.js} +5 -5
  160. package/dist/plugin-sdk/send-BU4OoR7u.js +2587 -0
  161. package/dist/plugin-sdk/{send-BqkUDZed.js → send-BeLBlAsQ.js} +13 -13
  162. package/dist/plugin-sdk/{send-BisREGBZ.js → send-D9CSOGul.js} +6 -6
  163. package/dist/plugin-sdk/{send-D6_nNvi0.js → send-DLKxJJYV.js} +8 -8
  164. package/dist/plugin-sdk/send-DbxOJ_BC.js +3135 -0
  165. package/dist/plugin-sdk/{send-Dj7XEcZN.js → send-XZ6IXCtL.js} +7 -7
  166. package/dist/plugin-sdk/send-n932vjT5.js +540 -0
  167. package/dist/plugin-sdk/send-uCPS53j8.js +503 -0
  168. package/dist/plugin-sdk/session-DenDKR_-.js +169 -0
  169. package/dist/plugin-sdk/{session-D4KDs7Hq.js → session-DtLUYWvY.js} +3 -3
  170. package/dist/plugin-sdk/skill-commands-BK1KDKmS.js +342 -0
  171. package/dist/plugin-sdk/{skill-commands-D_xeseiI.js → skill-commands-Bv7EZypt.js} +4 -4
  172. package/dist/plugin-sdk/{skills-Bs2b3JfV.js → skills-BzXN4uev.js} +6 -6
  173. package/dist/plugin-sdk/skills-D4am-zkO.js +1428 -0
  174. package/dist/plugin-sdk/slash-commands.runtime-Bx1K1iqP.js +13 -0
  175. package/dist/plugin-sdk/slash-commands.runtime-DWfFqMZw.js +13 -0
  176. package/dist/plugin-sdk/slash-dispatch.runtime-DVn338JI.js +52 -0
  177. package/dist/plugin-sdk/slash-dispatch.runtime-pnWH5AjM.js +52 -0
  178. package/dist/plugin-sdk/slash-skill-commands.runtime-Dbi_YzPO.js +16 -0
  179. package/dist/plugin-sdk/slash-skill-commands.runtime-DxvNWv_E.js +16 -0
  180. package/dist/plugin-sdk/ssrf-2WBi1Tzx.js +202 -0
  181. package/dist/plugin-sdk/store-BKDMuvyn.js +81 -0
  182. package/dist/plugin-sdk/{store-B7ESm9_L.js → store-DnJhFFW5.js} +2 -2
  183. package/dist/plugin-sdk/subagent-registry-runtime-FhP0l-Rw.js +52 -0
  184. package/dist/plugin-sdk/subagent-registry-runtime-hH9ADku1.js +52 -0
  185. package/dist/plugin-sdk/{tables-1vhBJPK_.js → tables-CpmqssLF.js} +1 -1
  186. package/dist/plugin-sdk/tables-CrDYcv_b.js +55 -0
  187. package/dist/plugin-sdk/target-errors-aOwE-MIU.js +195 -0
  188. package/dist/plugin-sdk/{thinking-DjaClmzi.js → thinking-1UCPuD9d.js} +7 -7
  189. package/dist/plugin-sdk/thinking-D41FMh9T.js +1206 -0
  190. package/dist/plugin-sdk/{tokens-CLE20fRI.js → tokens-CTIYTLWu.js} +1 -1
  191. package/dist/plugin-sdk/tokens-DAL_5WHL.js +52 -0
  192. package/dist/plugin-sdk/{tool-images-B95xcwiR.js → tool-images-CWc54lpI.js} +2 -2
  193. package/dist/plugin-sdk/tool-images-RX4QTMnt.js +274 -0
  194. package/dist/plugin-sdk/web-AtEy-48y.js +56 -0
  195. package/dist/plugin-sdk/web-DjKONHqF.js +56 -0
  196. package/dist/plugin-sdk/{whatsapp-actions-BYpcWkTN.js → whatsapp-actions-BF6ih4Gi.js} +17 -17
  197. package/dist/plugin-sdk/whatsapp-actions-DEZcm_CZ.js +80 -0
  198. package/dist/plugin-sdk/whatsapp.js +50 -50
  199. package/dist/{program-xNEHPhT8.js → program-2J-jgdfk.js} +2 -2
  200. package/dist/{program-context-J_FyEsaS.js → program-context-SjYSWx_N.js} +8 -8
  201. package/dist/{prompt-select-styled-B1LjjgQ0.js → prompt-select-styled-Baiu3mAU.js} +1 -1
  202. package/dist/{prompt-select-styled-BRiogP_P.js → prompt-select-styled-DPnVmH8f.js} +1 -1
  203. package/dist/{pw-ai-7kHgUGj0.js → pw-ai-BwRP3TWc.js} +1 -1
  204. package/dist/{pw-ai-BmGrTicP.js → pw-ai-zFPBSxaL.js} +1 -1
  205. package/dist/{register.configure-DezZ4Q1p.js → register.configure-Ao1K2uze.js} +1 -1
  206. package/dist/{register.configure-C4p9ad2q.js → register.configure-CCkfhF-7.js} +1 -1
  207. package/dist/{register.maintenance-CzMKTC2a.js → register.maintenance-DMbs8w2m.js} +4 -4
  208. package/dist/{register.maintenance-CTvFmkAm.js → register.maintenance-pLpE2oF-.js} +5 -5
  209. package/dist/{register.onboard-C39xhpv1.js → register.onboard-Cb8xLIye.js} +2 -2
  210. package/dist/{register.onboard-DZt2kSAg.js → register.onboard-IMrHOeW_.js} +2 -2
  211. package/dist/{register.setup-04L_8wfA.js → register.setup-CX8IUmew.js} +2 -2
  212. package/dist/{register.setup-DWctFmOd.js → register.setup-DU7IDzAv.js} +2 -2
  213. package/dist/{register.subclis-C3TphbCF.js → register.subclis-BIbL6FBV.js} +3 -3
  214. package/dist/{run-main-7tknx04F.js → run-main-CpDZPsC_.js} +5 -5
  215. package/dist/{slash-dispatch.runtime-BL3qA1O3.js → slash-dispatch.runtime-DkcAYuyK.js} +6 -6
  216. package/dist/{slash-dispatch.runtime-Dh2L_3Tg.js → slash-dispatch.runtime-DuJRl-LD.js} +6 -6
  217. package/dist/{subagent-registry-runtime-MtjBCcgn.js → subagent-registry-runtime-BlRAnw80.js} +6 -6
  218. package/dist/{subagent-registry-runtime-BRNDawlJ.js → subagent-registry-runtime-COygB9b1.js} +6 -6
  219. package/dist/{update-cli-0UiUaT3q.js → update-cli-BwhvSo1R.js} +5 -5
  220. package/dist/{update-cli-C-uyQcFS.js → update-cli-TQEfxhWF.js} +4 -4
  221. package/dist/{web-D1ZoRVB0.js → web-DddJa7ZT.js} +6 -6
  222. package/dist/{web-B7kbCskR.js → web-DyCuTR9b.js} +6 -6
  223. package/package.json +7 -7
  224. package/dist/plugin-sdk/deliver-runtime-DEzvpBW1.js +0 -32
  225. package/dist/plugin-sdk/deps-send-discord.runtime-Bhusa_Hi.js +0 -23
  226. package/dist/plugin-sdk/deps-send-imessage.runtime-bmakPm5f.js +0 -22
  227. package/dist/plugin-sdk/deps-send-signal.runtime-n00sfFto.js +0 -21
  228. package/dist/plugin-sdk/deps-send-slack.runtime-BvM3Z-Mr.js +0 -19
  229. package/dist/plugin-sdk/deps-send-telegram.runtime-CPuMkcmo.js +0 -24
  230. package/dist/plugin-sdk/deps-send-whatsapp.runtime-BzO6S-KX.js +0 -57
  231. package/dist/plugin-sdk/image-runtime-17_mTqsy.js +0 -25
  232. package/dist/plugin-sdk/manager-runtime-CvI9wF8N.js +0 -15
  233. package/dist/plugin-sdk/pi-model-discovery-runtime-DjjBdPYt.js +0 -8
  234. package/dist/plugin-sdk/runtime-whatsapp-login.runtime-DzhkSmLi.js +0 -10
  235. package/dist/plugin-sdk/runtime-whatsapp-outbound.runtime-DyILWezU.js +0 -19
  236. package/dist/plugin-sdk/slash-commands.runtime-CUb5sqqf.js +0 -13
  237. package/dist/plugin-sdk/slash-dispatch.runtime-DCB6bGjB.js +0 -52
  238. package/dist/plugin-sdk/slash-skill-commands.runtime-BqEweE4K.js +0 -16
  239. package/dist/plugin-sdk/subagent-registry-runtime-CCUW4SbM.js +0 -52
  240. package/dist/plugin-sdk/web-DeRmHQ4_.js +0 -56
@@ -0,0 +1,2256 @@
1
+ import { $ as normalizeAgentId, J as buildAgentMainSessionKey, X as buildGroupHistoryKey, at as DEFAULT_ACCOUNT_ID, q as DEFAULT_MAIN_KEY } from "./run-with-concurrency-5DMu9szx.js";
2
+ import { i as resolveWhatsAppAccount, p as readWebSelfId, s as getWebAuthAgeMs } from "./accounts-BNuRM3rG.js";
3
+ import { Et as resolveMessagePrefix, Gt as resolveOpenProviderRuntimeGroupPolicy, It as resolveDmGroupAccessWithCommandGate, Lt as resolveDmGroupAccessWithLists, M as enqueueSystemEvent, N as finalizeInboundContext, Nt as readStoreAllowFromForDmPolicy, Tt as resolveIdentityNamePrefix, Wt as resolveDefaultGroupPolicy, X as createReplyPrefixOptions, _ as buildAgentSessionKey, at as DEFAULT_GROUP_HISTORY_LIMIT, b as shouldComputeCommandAuthorized, d as computeBackoff, et as shouldAckReactionForWhatsApp, f as sleepWithAbort, ft as recordPendingHistoryEntryIfEnabled, g as resolveInboundDebounceMs, h as createInboundDebouncer, ht as createDedupeCache, i as parseActivationCommand, j as formatDurationPrecise, jt as buildPairingReply, mt as normalizeMentionText, o as resolveInboundSessionEnvelopeContext, ot as buildHistoryContextFromEntries, p as dispatchReplyWithBufferedBlockDispatcher, pt as buildMentionRegexes, qt as warnMissingProviderGroupPolicyFallbackOnce, r as normalizeGroupActivation, t as getReplyFromConfig, tt as resolveMentionGating, v as resolveAgentRoute, y as hasControlCommand, yt as formatInboundEnvelope, zt as resolvePinnedMainDmOwnerFromAllowlist } from "./reply-CQUX_haM.js";
4
+ import { Ct as normalizeChatChannelId, Pr as formatCliCommand, n as loadConfig } from "./config-CrQ5bCrw.js";
5
+ import { A as sleep, G as getChildLogger, L as logVerbose, M as toWhatsappJid, S as normalizeE164, T as resolveJidToE164, a as createSubsystemLogger, b as isSelfChatMode, c as defaultRuntime, p as clamp, x as jidToE164, z as shouldLogVerbose } from "./logger-DDdrdbDu.js";
6
+ import { U as resolveChannelGroupPolicy, W as resolveChannelGroupRequireMention } from "./thinking-D41FMh9T.js";
7
+ import { _t as loadSessionStore, bt as updateLastRoute, in as resolveGroupSessionKey, yt as recordSessionMetaFromInbound } from "./pi-embedded-helpers-C-B9B6Sp.js";
8
+ import { s as resolveStorePath } from "./paths-C6W4VHoa.js";
9
+ import { n as recordChannelActivity } from "./channel-activity-gPvD1D7S.js";
10
+ import { t as getAgentScopedMediaLocalRoots } from "./local-roots-DmOKwiNW.js";
11
+ import { c as chunkMarkdownTextWithMode, d as resolveChunkMode, f as resolveTextChunkLimit, i as resolveMarkdownTableMode, v as loadWebMedia } from "./ir-CS7uuQhN.js";
12
+ import { t as convertMarkdownTables } from "./tables-CrDYcv_b.js";
13
+ import { $ as readChannelAllowFromStoreSync, K as formatLocationText, q as toLocationContext, tt as upsertChannelPairingRequest } from "./send-BU4OoR7u.js";
14
+ import { d as registerUnhandledRejectionHandler } from "./audio-transcription-runner-RXsskMMk.js";
15
+ import { a as saveMediaBuffer } from "./store-BKDMuvyn.js";
16
+ import { r as setActiveWebListener } from "./active-listener-CTsLn1AX.js";
17
+ import { i as markdownToWhatsApp, r as sendReactionWhatsApp } from "./outbound-cpqK1GFe.js";
18
+ import { i as waitForWaConnection, n as formatError, r as getStatusCode, t as createWaSocket } from "./session-DenDKR_-.js";
19
+ import { randomUUID } from "node:crypto";
20
+ import { DisconnectReason, downloadMediaMessage, extractMessageContent, getContentType, isJidGroup, normalizeMessageContent } from "@whiskeysockets/baileys";
21
+
22
+ //#region src/web/auto-reply/constants.ts
23
+ const DEFAULT_WEB_MEDIA_BYTES = 5 * 1024 * 1024;
24
+
25
+ //#endregion
26
+ //#region src/channels/plugins/whatsapp-heartbeat.ts
27
+ function getSessionRecipients(cfg) {
28
+ if ((cfg.session?.scope ?? "per-sender") === "global") return [];
29
+ const store = loadSessionStore(resolveStorePath(cfg.session?.store));
30
+ const isGroupKey = (key) => key.includes(":group:") || key.includes(":channel:") || key.includes("@g.us");
31
+ const isCronKey = (key) => key.startsWith("cron:");
32
+ const recipients = Object.entries(store).filter(([key]) => key !== "global" && key !== "unknown").filter(([key]) => !isGroupKey(key) && !isCronKey(key)).map(([_, entry]) => ({
33
+ to: normalizeChatChannelId(entry?.lastChannel) === "whatsapp" && entry?.lastTo ? normalizeE164(entry.lastTo) : "",
34
+ updatedAt: entry?.updatedAt ?? 0
35
+ })).filter(({ to }) => to.length > 1).toSorted((a, b) => b.updatedAt - a.updatedAt);
36
+ const seen = /* @__PURE__ */ new Set();
37
+ return recipients.filter((r) => {
38
+ if (seen.has(r.to)) return false;
39
+ seen.add(r.to);
40
+ return true;
41
+ });
42
+ }
43
+ function resolveWhatsAppHeartbeatRecipients(cfg, opts = {}) {
44
+ if (opts.to) return {
45
+ recipients: [normalizeE164(opts.to)],
46
+ source: "flag"
47
+ };
48
+ const sessionRecipients = getSessionRecipients(cfg);
49
+ const configuredAllowFrom = Array.isArray(cfg.channels?.whatsapp?.allowFrom) && cfg.channels.whatsapp.allowFrom.length > 0 ? cfg.channels.whatsapp.allowFrom.filter((v) => v !== "*").map(normalizeE164) : [];
50
+ const storeAllowFrom = readChannelAllowFromStoreSync("whatsapp", process.env, DEFAULT_ACCOUNT_ID).map(normalizeE164);
51
+ const unique = (list) => [...new Set(list.filter(Boolean))];
52
+ const allowFrom = unique([...configuredAllowFrom, ...storeAllowFrom]);
53
+ if (opts.all) return {
54
+ recipients: unique([...sessionRecipients.map((s) => s.to), ...allowFrom]),
55
+ source: "all"
56
+ };
57
+ if (allowFrom.length > 0) {
58
+ const allowSet = new Set(allowFrom);
59
+ const authorizedSessionRecipients = sessionRecipients.map((entry) => entry.to).filter((recipient) => allowSet.has(recipient));
60
+ if (authorizedSessionRecipients.length === 1) return {
61
+ recipients: [authorizedSessionRecipients[0]],
62
+ source: "session-single"
63
+ };
64
+ if (authorizedSessionRecipients.length > 1) return {
65
+ recipients: authorizedSessionRecipients,
66
+ source: "session-ambiguous"
67
+ };
68
+ return {
69
+ recipients: allowFrom,
70
+ source: "allowFrom"
71
+ };
72
+ }
73
+ if (sessionRecipients.length === 1) return {
74
+ recipients: [sessionRecipients[0].to],
75
+ source: "session-single"
76
+ };
77
+ if (sessionRecipients.length > 1) return {
78
+ recipients: sessionRecipients.map((s) => s.to),
79
+ source: "session-ambiguous"
80
+ };
81
+ return {
82
+ recipients: allowFrom,
83
+ source: "allowFrom"
84
+ };
85
+ }
86
+
87
+ //#endregion
88
+ //#region src/web/reconnect.ts
89
+ const DEFAULT_HEARTBEAT_SECONDS = 60;
90
+ const DEFAULT_RECONNECT_POLICY = {
91
+ initialMs: 2e3,
92
+ maxMs: 3e4,
93
+ factor: 1.8,
94
+ jitter: .25,
95
+ maxAttempts: 12
96
+ };
97
+ function resolveHeartbeatSeconds(cfg, overrideSeconds) {
98
+ const candidate = overrideSeconds ?? cfg.web?.heartbeatSeconds;
99
+ if (typeof candidate === "number" && candidate > 0) return candidate;
100
+ return DEFAULT_HEARTBEAT_SECONDS;
101
+ }
102
+ function resolveReconnectPolicy(cfg, overrides) {
103
+ const reconnectOverrides = cfg.web?.reconnect ?? {};
104
+ const overrideConfig = overrides ?? {};
105
+ const merged = {
106
+ ...DEFAULT_RECONNECT_POLICY,
107
+ ...reconnectOverrides,
108
+ ...overrideConfig
109
+ };
110
+ merged.initialMs = Math.max(250, merged.initialMs);
111
+ merged.maxMs = Math.max(merged.initialMs, merged.maxMs);
112
+ merged.factor = clamp(merged.factor, 1.1, 10);
113
+ merged.jitter = clamp(merged.jitter, 0, 1);
114
+ merged.maxAttempts = Math.max(0, Math.floor(merged.maxAttempts));
115
+ return merged;
116
+ }
117
+ function newConnectionId() {
118
+ return randomUUID();
119
+ }
120
+
121
+ //#endregion
122
+ //#region src/web/auto-reply/loggers.ts
123
+ const whatsappLog = createSubsystemLogger("gateway/channels/whatsapp");
124
+ const whatsappInboundLog = whatsappLog.child("inbound");
125
+ const whatsappOutboundLog = whatsappLog.child("outbound");
126
+ const whatsappHeartbeatLog = whatsappLog.child("heartbeat");
127
+
128
+ //#endregion
129
+ //#region src/cli/wait.ts
130
+ function waitForever() {
131
+ setInterval(() => {}, 1e6).unref();
132
+ return new Promise(() => {});
133
+ }
134
+
135
+ //#endregion
136
+ //#region src/web/inbound/dedupe.ts
137
+ const recentInboundMessages = createDedupeCache({
138
+ ttlMs: 20 * 6e4,
139
+ maxSize: 5e3
140
+ });
141
+ function isRecentInboundMessage(key) {
142
+ return recentInboundMessages.check(key);
143
+ }
144
+
145
+ //#endregion
146
+ //#region src/web/vcard.ts
147
+ const ALLOWED_VCARD_KEYS = new Set([
148
+ "FN",
149
+ "N",
150
+ "TEL"
151
+ ]);
152
+ function parseVcard(vcard) {
153
+ if (!vcard) return { phones: [] };
154
+ const lines = vcard.split(/\r?\n/);
155
+ let nameFromN;
156
+ let nameFromFn;
157
+ const phones = [];
158
+ for (const rawLine of lines) {
159
+ const line = rawLine.trim();
160
+ if (!line) continue;
161
+ const colonIndex = line.indexOf(":");
162
+ if (colonIndex === -1) continue;
163
+ const key = line.slice(0, colonIndex).toUpperCase();
164
+ const rawValue = line.slice(colonIndex + 1).trim();
165
+ if (!rawValue) continue;
166
+ const baseKey = normalizeVcardKey(key);
167
+ if (!baseKey || !ALLOWED_VCARD_KEYS.has(baseKey)) continue;
168
+ const value = cleanVcardValue(rawValue);
169
+ if (!value) continue;
170
+ if (baseKey === "FN" && !nameFromFn) {
171
+ nameFromFn = normalizeVcardName(value);
172
+ continue;
173
+ }
174
+ if (baseKey === "N" && !nameFromN) {
175
+ nameFromN = normalizeVcardName(value);
176
+ continue;
177
+ }
178
+ if (baseKey === "TEL") {
179
+ const phone = normalizeVcardPhone(value);
180
+ if (phone) phones.push(phone);
181
+ }
182
+ }
183
+ return {
184
+ name: nameFromFn ?? nameFromN,
185
+ phones
186
+ };
187
+ }
188
+ function normalizeVcardKey(key) {
189
+ const [primary] = key.split(";");
190
+ if (!primary) return;
191
+ const segments = primary.split(".");
192
+ return segments[segments.length - 1] || void 0;
193
+ }
194
+ function cleanVcardValue(value) {
195
+ return value.replace(/\\n/gi, " ").replace(/\\,/g, ",").replace(/\\;/g, ";").trim();
196
+ }
197
+ function normalizeVcardName(value) {
198
+ return value.replace(/;/g, " ").replace(/\s+/g, " ").trim();
199
+ }
200
+ function normalizeVcardPhone(value) {
201
+ const trimmed = value.trim();
202
+ if (!trimmed) return "";
203
+ if (trimmed.toLowerCase().startsWith("tel:")) return trimmed.slice(4).trim();
204
+ return trimmed;
205
+ }
206
+
207
+ //#endregion
208
+ //#region src/web/inbound/extract.ts
209
+ function unwrapMessage$1(message) {
210
+ return normalizeMessageContent(message);
211
+ }
212
+ function extractContextInfo(message) {
213
+ if (!message) return;
214
+ const contentType = getContentType(message);
215
+ const candidate = contentType ? message[contentType] : void 0;
216
+ const contextInfo = candidate && typeof candidate === "object" && "contextInfo" in candidate ? candidate.contextInfo : void 0;
217
+ if (contextInfo) return contextInfo;
218
+ const fallback = message.extendedTextMessage?.contextInfo ?? message.imageMessage?.contextInfo ?? message.videoMessage?.contextInfo ?? message.documentMessage?.contextInfo ?? message.audioMessage?.contextInfo ?? message.stickerMessage?.contextInfo ?? message.buttonsResponseMessage?.contextInfo ?? message.listResponseMessage?.contextInfo ?? message.templateButtonReplyMessage?.contextInfo ?? message.interactiveResponseMessage?.contextInfo ?? message.buttonsMessage?.contextInfo ?? message.listMessage?.contextInfo;
219
+ if (fallback) return fallback;
220
+ for (const value of Object.values(message)) {
221
+ if (!value || typeof value !== "object") continue;
222
+ if (!("contextInfo" in value)) continue;
223
+ const candidateContext = value.contextInfo;
224
+ if (candidateContext) return candidateContext;
225
+ }
226
+ }
227
+ function extractMentionedJids(rawMessage) {
228
+ const message = unwrapMessage$1(rawMessage);
229
+ if (!message) return;
230
+ const flattened = [
231
+ message.extendedTextMessage?.contextInfo?.mentionedJid,
232
+ message.extendedTextMessage?.contextInfo?.quotedMessage?.extendedTextMessage?.contextInfo?.mentionedJid,
233
+ message.imageMessage?.contextInfo?.mentionedJid,
234
+ message.videoMessage?.contextInfo?.mentionedJid,
235
+ message.documentMessage?.contextInfo?.mentionedJid,
236
+ message.audioMessage?.contextInfo?.mentionedJid,
237
+ message.stickerMessage?.contextInfo?.mentionedJid,
238
+ message.buttonsResponseMessage?.contextInfo?.mentionedJid,
239
+ message.listResponseMessage?.contextInfo?.mentionedJid
240
+ ].flatMap((arr) => arr ?? []).filter(Boolean);
241
+ if (flattened.length === 0) return;
242
+ return Array.from(new Set(flattened));
243
+ }
244
+ function extractText(rawMessage) {
245
+ const message = unwrapMessage$1(rawMessage);
246
+ if (!message) return;
247
+ const extracted = extractMessageContent(message);
248
+ const candidates = [message, extracted && extracted !== message ? extracted : void 0];
249
+ for (const candidate of candidates) {
250
+ if (!candidate) continue;
251
+ if (typeof candidate.conversation === "string" && candidate.conversation.trim()) return candidate.conversation.trim();
252
+ const extended = candidate.extendedTextMessage?.text;
253
+ if (extended?.trim()) return extended.trim();
254
+ const caption = candidate.imageMessage?.caption ?? candidate.videoMessage?.caption ?? candidate.documentMessage?.caption;
255
+ if (caption?.trim()) return caption.trim();
256
+ }
257
+ const contactPlaceholder = extractContactPlaceholder(message) ?? (extracted && extracted !== message ? extractContactPlaceholder(extracted) : void 0);
258
+ if (contactPlaceholder) return contactPlaceholder;
259
+ }
260
+ function extractMediaPlaceholder(rawMessage) {
261
+ const message = unwrapMessage$1(rawMessage);
262
+ if (!message) return;
263
+ if (message.imageMessage) return "<media:image>";
264
+ if (message.videoMessage) return "<media:video>";
265
+ if (message.audioMessage) return "<media:audio>";
266
+ if (message.documentMessage) return "<media:document>";
267
+ if (message.stickerMessage) return "<media:sticker>";
268
+ }
269
+ function extractContactPlaceholder(rawMessage) {
270
+ const message = unwrapMessage$1(rawMessage);
271
+ if (!message) return;
272
+ const contact = message.contactMessage ?? void 0;
273
+ if (contact) {
274
+ const { name, phones } = describeContact({
275
+ displayName: contact.displayName,
276
+ vcard: contact.vcard
277
+ });
278
+ return formatContactPlaceholder(name, phones);
279
+ }
280
+ const contactsArray = message.contactsArrayMessage?.contacts ?? void 0;
281
+ if (!contactsArray || contactsArray.length === 0) return;
282
+ return formatContactsPlaceholder(contactsArray.map((entry) => describeContact({
283
+ displayName: entry.displayName,
284
+ vcard: entry.vcard
285
+ })).map((entry) => formatContactLabel(entry.name, entry.phones)).filter((value) => Boolean(value)), contactsArray.length);
286
+ }
287
+ function describeContact(input) {
288
+ const displayName = (input.displayName ?? "").trim();
289
+ const parsed = parseVcard(input.vcard ?? void 0);
290
+ return {
291
+ name: displayName || parsed.name,
292
+ phones: parsed.phones
293
+ };
294
+ }
295
+ function formatContactPlaceholder(name, phones) {
296
+ const label = formatContactLabel(name, phones);
297
+ if (!label) return "<contact>";
298
+ return `<contact: ${label}>`;
299
+ }
300
+ function formatContactsPlaceholder(labels, total) {
301
+ const cleaned = labels.map((label) => label.trim()).filter(Boolean);
302
+ if (cleaned.length === 0) return `<contacts: ${total} ${total === 1 ? "contact" : "contacts"}>`;
303
+ const remaining = Math.max(total - cleaned.length, 0);
304
+ const suffix = remaining > 0 ? ` +${remaining} more` : "";
305
+ return `<contacts: ${cleaned.join(", ")}${suffix}>`;
306
+ }
307
+ function formatContactLabel(name, phones) {
308
+ const parts = [name, formatPhoneList(phones)].filter((value) => Boolean(value));
309
+ if (parts.length === 0) return;
310
+ return parts.join(", ");
311
+ }
312
+ function formatPhoneList(phones) {
313
+ const cleaned = phones?.map((phone) => phone.trim()).filter(Boolean) ?? [];
314
+ if (cleaned.length === 0) return;
315
+ const { shown, remaining } = summarizeList(cleaned, cleaned.length, 1);
316
+ const [primary] = shown;
317
+ if (!primary) return;
318
+ if (remaining === 0) return primary;
319
+ return `${primary} (+${remaining} more)`;
320
+ }
321
+ function summarizeList(values, total, maxShown) {
322
+ const shown = values.slice(0, maxShown);
323
+ return {
324
+ shown,
325
+ remaining: Math.max(total - shown.length, 0)
326
+ };
327
+ }
328
+ function extractLocationData(rawMessage) {
329
+ const message = unwrapMessage$1(rawMessage);
330
+ if (!message) return null;
331
+ const live = message.liveLocationMessage ?? void 0;
332
+ if (live) {
333
+ const latitudeRaw = live.degreesLatitude;
334
+ const longitudeRaw = live.degreesLongitude;
335
+ if (latitudeRaw != null && longitudeRaw != null) {
336
+ const latitude = Number(latitudeRaw);
337
+ const longitude = Number(longitudeRaw);
338
+ if (Number.isFinite(latitude) && Number.isFinite(longitude)) return {
339
+ latitude,
340
+ longitude,
341
+ accuracy: live.accuracyInMeters ?? void 0,
342
+ caption: live.caption ?? void 0,
343
+ source: "live",
344
+ isLive: true
345
+ };
346
+ }
347
+ }
348
+ const location = message.locationMessage ?? void 0;
349
+ if (location) {
350
+ const latitudeRaw = location.degreesLatitude;
351
+ const longitudeRaw = location.degreesLongitude;
352
+ if (latitudeRaw != null && longitudeRaw != null) {
353
+ const latitude = Number(latitudeRaw);
354
+ const longitude = Number(longitudeRaw);
355
+ if (Number.isFinite(latitude) && Number.isFinite(longitude)) {
356
+ const isLive = Boolean(location.isLive);
357
+ return {
358
+ latitude,
359
+ longitude,
360
+ accuracy: location.accuracyInMeters ?? void 0,
361
+ name: location.name ?? void 0,
362
+ address: location.address ?? void 0,
363
+ caption: location.comment ?? void 0,
364
+ source: isLive ? "live" : location.name || location.address ? "place" : "pin",
365
+ isLive
366
+ };
367
+ }
368
+ }
369
+ }
370
+ return null;
371
+ }
372
+ function describeReplyContext(rawMessage) {
373
+ const message = unwrapMessage$1(rawMessage);
374
+ if (!message) return null;
375
+ const contextInfo = extractContextInfo(message);
376
+ const quoted = normalizeMessageContent(contextInfo?.quotedMessage);
377
+ if (!quoted) return null;
378
+ const location = extractLocationData(quoted);
379
+ const locationText = location ? formatLocationText(location) : void 0;
380
+ let body = [extractText(quoted), locationText].filter(Boolean).join("\n").trim();
381
+ if (!body) body = extractMediaPlaceholder(quoted);
382
+ if (!body) {
383
+ const quotedType = quoted ? getContentType(quoted) : void 0;
384
+ logVerbose(`Quoted message missing extractable body${quotedType ? ` (type ${quotedType})` : ""}`);
385
+ return null;
386
+ }
387
+ const senderJid = contextInfo?.participant ?? void 0;
388
+ const senderE164 = senderJid ? jidToE164(senderJid) ?? senderJid : void 0;
389
+ const sender = senderE164 ?? "unknown sender";
390
+ return {
391
+ id: contextInfo?.stanzaId ? String(contextInfo.stanzaId) : void 0,
392
+ body,
393
+ sender,
394
+ senderJid,
395
+ senderE164
396
+ };
397
+ }
398
+
399
+ //#endregion
400
+ //#region src/web/inbound/access-control.ts
401
+ const PAIRING_REPLY_HISTORY_GRACE_MS = 3e4;
402
+ function resolveWhatsAppRuntimeGroupPolicy(params) {
403
+ return resolveOpenProviderRuntimeGroupPolicy({
404
+ providerConfigPresent: params.providerConfigPresent,
405
+ groupPolicy: params.groupPolicy,
406
+ defaultGroupPolicy: params.defaultGroupPolicy
407
+ });
408
+ }
409
+ async function checkInboundAccessControl(params) {
410
+ const cfg = loadConfig();
411
+ const account = resolveWhatsAppAccount({
412
+ cfg,
413
+ accountId: params.accountId
414
+ });
415
+ const dmPolicy = account.dmPolicy ?? "pairing";
416
+ const configuredAllowFrom = account.allowFrom ?? [];
417
+ const storeAllowFrom = await readStoreAllowFromForDmPolicy({
418
+ provider: "whatsapp",
419
+ accountId: account.accountId,
420
+ dmPolicy
421
+ });
422
+ const defaultAllowFrom = configuredAllowFrom.length === 0 && params.selfE164 ? [params.selfE164] : [];
423
+ const dmAllowFrom = configuredAllowFrom.length > 0 ? configuredAllowFrom : defaultAllowFrom;
424
+ const groupAllowFrom = account.groupAllowFrom ?? (configuredAllowFrom.length > 0 ? configuredAllowFrom : void 0);
425
+ const isSamePhone = params.from === params.selfE164;
426
+ const isSelfChat = account.selfChatMode ?? isSelfChatMode(params.selfE164, configuredAllowFrom);
427
+ const pairingGraceMs = typeof params.pairingGraceMs === "number" && params.pairingGraceMs > 0 ? params.pairingGraceMs : PAIRING_REPLY_HISTORY_GRACE_MS;
428
+ const suppressPairingReply = typeof params.connectedAtMs === "number" && typeof params.messageTimestampMs === "number" && params.messageTimestampMs < params.connectedAtMs - pairingGraceMs;
429
+ const defaultGroupPolicy = resolveDefaultGroupPolicy(cfg);
430
+ const { groupPolicy, providerMissingFallbackApplied } = resolveWhatsAppRuntimeGroupPolicy({
431
+ providerConfigPresent: cfg.channels?.whatsapp !== void 0,
432
+ groupPolicy: account.groupPolicy,
433
+ defaultGroupPolicy
434
+ });
435
+ warnMissingProviderGroupPolicyFallbackOnce({
436
+ providerMissingFallbackApplied,
437
+ providerKey: "whatsapp",
438
+ accountId: account.accountId,
439
+ log: (message) => logVerbose(message)
440
+ });
441
+ const normalizedDmSender = normalizeE164(params.from);
442
+ const normalizedGroupSender = typeof params.senderE164 === "string" ? normalizeE164(params.senderE164) : null;
443
+ const access = resolveDmGroupAccessWithLists({
444
+ isGroup: params.group,
445
+ dmPolicy,
446
+ groupPolicy,
447
+ allowFrom: params.group ? configuredAllowFrom : dmAllowFrom,
448
+ groupAllowFrom,
449
+ storeAllowFrom,
450
+ isSenderAllowed: (allowEntries) => {
451
+ if (allowEntries.includes("*")) return true;
452
+ const normalizedEntrySet = new Set(allowEntries.map((entry) => normalizeE164(String(entry))).filter((entry) => Boolean(entry)));
453
+ if (!params.group && isSamePhone) return true;
454
+ return params.group ? Boolean(normalizedGroupSender && normalizedEntrySet.has(normalizedGroupSender)) : normalizedEntrySet.has(normalizedDmSender);
455
+ }
456
+ });
457
+ if (params.group && access.decision !== "allow") {
458
+ if (access.reason === "groupPolicy=disabled") logVerbose("Blocked group message (groupPolicy: disabled)");
459
+ else if (access.reason === "groupPolicy=allowlist (empty allowlist)") logVerbose("Blocked group message (groupPolicy: allowlist, no groupAllowFrom)");
460
+ else logVerbose(`Blocked group message from ${params.senderE164 ?? "unknown sender"} (groupPolicy: allowlist)`);
461
+ return {
462
+ allowed: false,
463
+ shouldMarkRead: false,
464
+ isSelfChat,
465
+ resolvedAccountId: account.accountId
466
+ };
467
+ }
468
+ if (!params.group) {
469
+ if (params.isFromMe && !isSamePhone) {
470
+ logVerbose("Skipping outbound DM (fromMe); no pairing reply needed.");
471
+ return {
472
+ allowed: false,
473
+ shouldMarkRead: false,
474
+ isSelfChat,
475
+ resolvedAccountId: account.accountId
476
+ };
477
+ }
478
+ if (access.decision === "block" && access.reason === "dmPolicy=disabled") {
479
+ logVerbose("Blocked dm (dmPolicy: disabled)");
480
+ return {
481
+ allowed: false,
482
+ shouldMarkRead: false,
483
+ isSelfChat,
484
+ resolvedAccountId: account.accountId
485
+ };
486
+ }
487
+ if (access.decision === "pairing" && !isSamePhone) {
488
+ const candidate = params.from;
489
+ if (suppressPairingReply) logVerbose(`Skipping pairing reply for historical DM from ${candidate}.`);
490
+ else {
491
+ const { code, created } = await upsertChannelPairingRequest({
492
+ channel: "whatsapp",
493
+ id: candidate,
494
+ accountId: account.accountId,
495
+ meta: { name: (params.pushName ?? "").trim() || void 0 }
496
+ });
497
+ if (created) {
498
+ logVerbose(`whatsapp pairing request sender=${candidate} name=${params.pushName ?? "unknown"}`);
499
+ try {
500
+ await params.sock.sendMessage(params.remoteJid, { text: buildPairingReply({
501
+ channel: "whatsapp",
502
+ idLine: `Your WhatsApp phone number: ${candidate}`,
503
+ code
504
+ }) });
505
+ } catch (err) {
506
+ logVerbose(`whatsapp pairing reply failed for ${candidate}: ${String(err)}`);
507
+ }
508
+ }
509
+ }
510
+ return {
511
+ allowed: false,
512
+ shouldMarkRead: false,
513
+ isSelfChat,
514
+ resolvedAccountId: account.accountId
515
+ };
516
+ }
517
+ if (access.decision !== "allow") {
518
+ logVerbose(`Blocked unauthorized sender ${params.from} (dmPolicy=${dmPolicy})`);
519
+ return {
520
+ allowed: false,
521
+ shouldMarkRead: false,
522
+ isSelfChat,
523
+ resolvedAccountId: account.accountId
524
+ };
525
+ }
526
+ }
527
+ return {
528
+ allowed: true,
529
+ shouldMarkRead: true,
530
+ isSelfChat,
531
+ resolvedAccountId: account.accountId
532
+ };
533
+ }
534
+
535
+ //#endregion
536
+ //#region src/web/inbound/media.ts
537
+ function unwrapMessage(message) {
538
+ return normalizeMessageContent(message);
539
+ }
540
+ /**
541
+ * Resolve the MIME type for an inbound media message.
542
+ * Falls back to WhatsApp's standard formats when Baileys omits the MIME.
543
+ */
544
+ function resolveMediaMimetype(message) {
545
+ const explicit = message.imageMessage?.mimetype ?? message.videoMessage?.mimetype ?? message.documentMessage?.mimetype ?? message.audioMessage?.mimetype ?? message.stickerMessage?.mimetype ?? void 0;
546
+ if (explicit) return explicit;
547
+ if (message.audioMessage) return "audio/ogg; codecs=opus";
548
+ if (message.imageMessage) return "image/jpeg";
549
+ if (message.videoMessage) return "video/mp4";
550
+ if (message.stickerMessage) return "image/webp";
551
+ }
552
+ async function downloadInboundMedia(msg, sock) {
553
+ const message = unwrapMessage(msg.message);
554
+ if (!message) return;
555
+ const mimetype = resolveMediaMimetype(message);
556
+ const fileName = message.documentMessage?.fileName ?? void 0;
557
+ if (!message.imageMessage && !message.videoMessage && !message.documentMessage && !message.audioMessage && !message.stickerMessage) return;
558
+ try {
559
+ return {
560
+ buffer: await downloadMediaMessage(msg, "buffer", {}, {
561
+ reuploadRequest: sock.updateMediaMessage,
562
+ logger: sock.logger
563
+ }),
564
+ mimetype,
565
+ fileName
566
+ };
567
+ } catch (err) {
568
+ logVerbose(`downloadMediaMessage failed: ${String(err)}`);
569
+ return;
570
+ }
571
+ }
572
+
573
+ //#endregion
574
+ //#region src/web/inbound/send-api.ts
575
+ function recordWhatsAppOutbound(accountId) {
576
+ recordChannelActivity({
577
+ channel: "whatsapp",
578
+ accountId,
579
+ direction: "outbound"
580
+ });
581
+ }
582
+ function resolveOutboundMessageId(result) {
583
+ return typeof result === "object" && result && "key" in result ? String(result.key?.id ?? "unknown") : "unknown";
584
+ }
585
+ function createWebSendApi(params) {
586
+ return {
587
+ sendMessage: async (to, text, mediaBuffer, mediaType, sendOptions) => {
588
+ const jid = toWhatsappJid(to);
589
+ let payload;
590
+ if (mediaBuffer && mediaType) if (mediaType.startsWith("image/")) payload = {
591
+ image: mediaBuffer,
592
+ caption: text || void 0,
593
+ mimetype: mediaType
594
+ };
595
+ else if (mediaType.startsWith("audio/")) payload = {
596
+ audio: mediaBuffer,
597
+ ptt: true,
598
+ mimetype: mediaType
599
+ };
600
+ else if (mediaType.startsWith("video/")) {
601
+ const gifPlayback = sendOptions?.gifPlayback;
602
+ payload = {
603
+ video: mediaBuffer,
604
+ caption: text || void 0,
605
+ mimetype: mediaType,
606
+ ...gifPlayback ? { gifPlayback: true } : {}
607
+ };
608
+ } else payload = {
609
+ document: mediaBuffer,
610
+ fileName: sendOptions?.fileName?.trim() || "file",
611
+ caption: text || void 0,
612
+ mimetype: mediaType
613
+ };
614
+ else payload = { text };
615
+ const result = await params.sock.sendMessage(jid, payload);
616
+ recordWhatsAppOutbound(sendOptions?.accountId ?? params.defaultAccountId);
617
+ return { messageId: resolveOutboundMessageId(result) };
618
+ },
619
+ sendPoll: async (to, poll) => {
620
+ const jid = toWhatsappJid(to);
621
+ const result = await params.sock.sendMessage(jid, { poll: {
622
+ name: poll.question,
623
+ values: poll.options,
624
+ selectableCount: poll.maxSelections ?? 1
625
+ } });
626
+ recordWhatsAppOutbound(params.defaultAccountId);
627
+ return { messageId: resolveOutboundMessageId(result) };
628
+ },
629
+ sendReaction: async (chatJid, messageId, emoji, fromMe, participant) => {
630
+ const jid = toWhatsappJid(chatJid);
631
+ await params.sock.sendMessage(jid, { react: {
632
+ text: emoji,
633
+ key: {
634
+ remoteJid: jid,
635
+ id: messageId,
636
+ fromMe,
637
+ participant: participant ? toWhatsappJid(participant) : void 0
638
+ }
639
+ } });
640
+ },
641
+ sendComposingTo: async (to) => {
642
+ const jid = toWhatsappJid(to);
643
+ await params.sock.sendPresenceUpdate("composing", jid);
644
+ }
645
+ };
646
+ }
647
+
648
+ //#endregion
649
+ //#region src/web/inbound/monitor.ts
650
+ async function monitorWebInbox(options) {
651
+ const inboundLogger = getChildLogger({ module: "web-inbound" });
652
+ const inboundConsoleLog = createSubsystemLogger("gateway/channels/whatsapp").child("inbound");
653
+ const sock = await createWaSocket(false, options.verbose, { authDir: options.authDir });
654
+ await waitForWaConnection(sock);
655
+ const connectedAtMs = Date.now();
656
+ let onCloseResolve = null;
657
+ const onClose = new Promise((resolve) => {
658
+ onCloseResolve = resolve;
659
+ });
660
+ const resolveClose = (reason) => {
661
+ if (!onCloseResolve) return;
662
+ const resolver = onCloseResolve;
663
+ onCloseResolve = null;
664
+ resolver(reason);
665
+ };
666
+ try {
667
+ await sock.sendPresenceUpdate("available");
668
+ if (shouldLogVerbose()) logVerbose("Sent global 'available' presence on connect");
669
+ } catch (err) {
670
+ logVerbose(`Failed to send 'available' presence on connect: ${String(err)}`);
671
+ }
672
+ const selfJid = sock.user?.id;
673
+ const selfE164 = selfJid ? jidToE164(selfJid) : null;
674
+ const debouncer = createInboundDebouncer({
675
+ debounceMs: options.debounceMs ?? 0,
676
+ buildKey: (msg) => {
677
+ const senderKey = msg.chatType === "group" ? msg.senderJid ?? msg.senderE164 ?? msg.senderName ?? msg.from : msg.from;
678
+ if (!senderKey) return null;
679
+ const conversationKey = msg.chatType === "group" ? msg.chatId : msg.from;
680
+ return `${msg.accountId}:${conversationKey}:${senderKey}`;
681
+ },
682
+ shouldDebounce: options.shouldDebounce,
683
+ onFlush: async (entries) => {
684
+ const last = entries.at(-1);
685
+ if (!last) return;
686
+ if (entries.length === 1) {
687
+ await options.onMessage(last);
688
+ return;
689
+ }
690
+ const mentioned = /* @__PURE__ */ new Set();
691
+ for (const entry of entries) for (const jid of entry.mentionedJids ?? []) mentioned.add(jid);
692
+ const combinedBody = entries.map((entry) => entry.body).filter(Boolean).join("\n");
693
+ const combinedMessage = {
694
+ ...last,
695
+ body: combinedBody,
696
+ mentionedJids: mentioned.size > 0 ? Array.from(mentioned) : void 0
697
+ };
698
+ await options.onMessage(combinedMessage);
699
+ },
700
+ onError: (err) => {
701
+ inboundLogger.error({ error: String(err) }, "failed handling inbound web message");
702
+ inboundConsoleLog.error(`Failed handling inbound web message: ${String(err)}`);
703
+ }
704
+ });
705
+ const groupMetaCache = /* @__PURE__ */ new Map();
706
+ const GROUP_META_TTL_MS = 300 * 1e3;
707
+ const lidLookup = sock.signalRepository?.lidMapping;
708
+ const resolveInboundJid = async (jid) => resolveJidToE164(jid, {
709
+ authDir: options.authDir,
710
+ lidLookup
711
+ });
712
+ const getGroupMeta = async (jid) => {
713
+ const cached = groupMetaCache.get(jid);
714
+ if (cached && cached.expires > Date.now()) return cached;
715
+ try {
716
+ const meta = await sock.groupMetadata(jid);
717
+ const participants = (await Promise.all(meta.participants?.map(async (p) => {
718
+ return await resolveInboundJid(p.id) ?? p.id;
719
+ }) ?? [])).filter(Boolean) ?? [];
720
+ const entry = {
721
+ subject: meta.subject,
722
+ participants,
723
+ expires: Date.now() + GROUP_META_TTL_MS
724
+ };
725
+ groupMetaCache.set(jid, entry);
726
+ return entry;
727
+ } catch (err) {
728
+ logVerbose(`Failed to fetch group metadata for ${jid}: ${String(err)}`);
729
+ return { expires: Date.now() + GROUP_META_TTL_MS };
730
+ }
731
+ };
732
+ const normalizeInboundMessage = async (msg) => {
733
+ const id = msg.key?.id ?? void 0;
734
+ const remoteJid = msg.key?.remoteJid;
735
+ if (!remoteJid) return null;
736
+ if (remoteJid.endsWith("@status") || remoteJid.endsWith("@broadcast")) return null;
737
+ const group = isJidGroup(remoteJid) === true;
738
+ if (id) {
739
+ if (isRecentInboundMessage(`${options.accountId}:${remoteJid}:${id}`)) return null;
740
+ }
741
+ const participantJid = msg.key?.participant ?? void 0;
742
+ const from = group ? remoteJid : await resolveInboundJid(remoteJid);
743
+ if (!from) return null;
744
+ const senderE164 = group ? participantJid ? await resolveInboundJid(participantJid) : null : from;
745
+ let groupSubject;
746
+ let groupParticipants;
747
+ if (group) {
748
+ const meta = await getGroupMeta(remoteJid);
749
+ groupSubject = meta.subject;
750
+ groupParticipants = meta.participants;
751
+ }
752
+ const messageTimestampMs = msg.messageTimestamp ? Number(msg.messageTimestamp) * 1e3 : void 0;
753
+ const access = await checkInboundAccessControl({
754
+ accountId: options.accountId,
755
+ from,
756
+ selfE164,
757
+ senderE164,
758
+ group,
759
+ pushName: msg.pushName ?? void 0,
760
+ isFromMe: Boolean(msg.key?.fromMe),
761
+ messageTimestampMs,
762
+ connectedAtMs,
763
+ sock: { sendMessage: (jid, content) => sock.sendMessage(jid, content) },
764
+ remoteJid
765
+ });
766
+ if (!access.allowed) return null;
767
+ return {
768
+ id,
769
+ remoteJid,
770
+ group,
771
+ participantJid,
772
+ from,
773
+ senderE164,
774
+ groupSubject,
775
+ groupParticipants,
776
+ messageTimestampMs,
777
+ access
778
+ };
779
+ };
780
+ const maybeMarkInboundAsRead = async (inbound) => {
781
+ const { id, remoteJid, participantJid, access } = inbound;
782
+ if (id && !access.isSelfChat && options.sendReadReceipts !== false) try {
783
+ await sock.readMessages([{
784
+ remoteJid,
785
+ id,
786
+ participant: participantJid,
787
+ fromMe: false
788
+ }]);
789
+ if (shouldLogVerbose()) logVerbose(`Marked message ${id} as read for ${remoteJid}${participantJid ? ` (participant ${participantJid})` : ""}`);
790
+ } catch (err) {
791
+ logVerbose(`Failed to mark message ${id} read: ${String(err)}`);
792
+ }
793
+ else if (id && access.isSelfChat && shouldLogVerbose()) logVerbose(`Self-chat mode: skipping read receipt for ${id}`);
794
+ };
795
+ const enrichInboundMessage = async (msg) => {
796
+ const location = extractLocationData(msg.message ?? void 0);
797
+ const locationText = location ? formatLocationText(location) : void 0;
798
+ let body = extractText(msg.message ?? void 0);
799
+ if (locationText) body = [body, locationText].filter(Boolean).join("\n").trim();
800
+ if (!body) {
801
+ body = extractMediaPlaceholder(msg.message ?? void 0);
802
+ if (!body) return null;
803
+ }
804
+ const replyContext = describeReplyContext(msg.message);
805
+ let mediaPath;
806
+ let mediaType;
807
+ let mediaFileName;
808
+ try {
809
+ const inboundMedia = await downloadInboundMedia(msg, sock);
810
+ if (inboundMedia) {
811
+ const maxBytes = (typeof options.mediaMaxMb === "number" && options.mediaMaxMb > 0 ? options.mediaMaxMb : 50) * 1024 * 1024;
812
+ mediaPath = (await saveMediaBuffer(inboundMedia.buffer, inboundMedia.mimetype, "inbound", maxBytes, inboundMedia.fileName)).path;
813
+ mediaType = inboundMedia.mimetype;
814
+ mediaFileName = inboundMedia.fileName;
815
+ }
816
+ } catch (err) {
817
+ logVerbose(`Inbound media download failed: ${String(err)}`);
818
+ }
819
+ return {
820
+ body,
821
+ location: location ?? void 0,
822
+ replyContext,
823
+ mediaPath,
824
+ mediaType,
825
+ mediaFileName
826
+ };
827
+ };
828
+ const enqueueInboundMessage = async (msg, inbound, enriched) => {
829
+ const chatJid = inbound.remoteJid;
830
+ const sendComposing = async () => {
831
+ try {
832
+ await sock.sendPresenceUpdate("composing", chatJid);
833
+ } catch (err) {
834
+ logVerbose(`Presence update failed: ${String(err)}`);
835
+ }
836
+ };
837
+ const reply = async (text) => {
838
+ await sock.sendMessage(chatJid, { text });
839
+ };
840
+ const sendMedia = async (payload) => {
841
+ await sock.sendMessage(chatJid, payload);
842
+ };
843
+ const timestamp = inbound.messageTimestampMs;
844
+ const mentionedJids = extractMentionedJids(msg.message);
845
+ const senderName = msg.pushName ?? void 0;
846
+ inboundLogger.info({
847
+ from: inbound.from,
848
+ to: selfE164 ?? "me",
849
+ body: enriched.body,
850
+ mediaPath: enriched.mediaPath,
851
+ mediaType: enriched.mediaType,
852
+ mediaFileName: enriched.mediaFileName,
853
+ timestamp
854
+ }, "inbound message");
855
+ const inboundMessage = {
856
+ id: inbound.id,
857
+ from: inbound.from,
858
+ conversationId: inbound.from,
859
+ to: selfE164 ?? "me",
860
+ accountId: inbound.access.resolvedAccountId,
861
+ body: enriched.body,
862
+ pushName: senderName,
863
+ timestamp,
864
+ chatType: inbound.group ? "group" : "direct",
865
+ chatId: inbound.remoteJid,
866
+ senderJid: inbound.participantJid,
867
+ senderE164: inbound.senderE164 ?? void 0,
868
+ senderName,
869
+ replyToId: enriched.replyContext?.id,
870
+ replyToBody: enriched.replyContext?.body,
871
+ replyToSender: enriched.replyContext?.sender,
872
+ replyToSenderJid: enriched.replyContext?.senderJid,
873
+ replyToSenderE164: enriched.replyContext?.senderE164,
874
+ groupSubject: inbound.groupSubject,
875
+ groupParticipants: inbound.groupParticipants,
876
+ mentionedJids: mentionedJids ?? void 0,
877
+ selfJid,
878
+ selfE164,
879
+ fromMe: Boolean(msg.key?.fromMe),
880
+ location: enriched.location ?? void 0,
881
+ sendComposing,
882
+ reply,
883
+ sendMedia,
884
+ mediaPath: enriched.mediaPath,
885
+ mediaType: enriched.mediaType,
886
+ mediaFileName: enriched.mediaFileName
887
+ };
888
+ try {
889
+ Promise.resolve(debouncer.enqueue(inboundMessage)).catch((err) => {
890
+ inboundLogger.error({ error: String(err) }, "failed handling inbound web message");
891
+ inboundConsoleLog.error(`Failed handling inbound web message: ${String(err)}`);
892
+ });
893
+ } catch (err) {
894
+ inboundLogger.error({ error: String(err) }, "failed handling inbound web message");
895
+ inboundConsoleLog.error(`Failed handling inbound web message: ${String(err)}`);
896
+ }
897
+ };
898
+ const handleMessagesUpsert = async (upsert) => {
899
+ if (upsert.type !== "notify" && upsert.type !== "append") return;
900
+ for (const msg of upsert.messages ?? []) {
901
+ recordChannelActivity({
902
+ channel: "whatsapp",
903
+ accountId: options.accountId,
904
+ direction: "inbound"
905
+ });
906
+ const inbound = await normalizeInboundMessage(msg);
907
+ if (!inbound) continue;
908
+ await maybeMarkInboundAsRead(inbound);
909
+ if (upsert.type === "append") continue;
910
+ const enriched = await enrichInboundMessage(msg);
911
+ if (!enriched) continue;
912
+ await enqueueInboundMessage(msg, inbound, enriched);
913
+ }
914
+ };
915
+ sock.ev.on("messages.upsert", handleMessagesUpsert);
916
+ const handleConnectionUpdate = (update) => {
917
+ try {
918
+ if (update.connection === "close") {
919
+ const status = getStatusCode(update.lastDisconnect?.error);
920
+ resolveClose({
921
+ status,
922
+ isLoggedOut: status === DisconnectReason.loggedOut,
923
+ error: update.lastDisconnect?.error
924
+ });
925
+ }
926
+ } catch (err) {
927
+ inboundLogger.error({ error: String(err) }, "connection.update handler error");
928
+ resolveClose({
929
+ status: void 0,
930
+ isLoggedOut: false,
931
+ error: err
932
+ });
933
+ }
934
+ };
935
+ sock.ev.on("connection.update", handleConnectionUpdate);
936
+ return {
937
+ close: async () => {
938
+ try {
939
+ const ev = sock.ev;
940
+ const messagesUpsertHandler = handleMessagesUpsert;
941
+ const connectionUpdateHandler = handleConnectionUpdate;
942
+ if (typeof ev.off === "function") {
943
+ ev.off("messages.upsert", messagesUpsertHandler);
944
+ ev.off("connection.update", connectionUpdateHandler);
945
+ } else if (typeof ev.removeListener === "function") {
946
+ ev.removeListener("messages.upsert", messagesUpsertHandler);
947
+ ev.removeListener("connection.update", connectionUpdateHandler);
948
+ }
949
+ sock.ws?.close();
950
+ } catch (err) {
951
+ logVerbose(`Socket close failed: ${String(err)}`);
952
+ }
953
+ },
954
+ onClose,
955
+ signalClose: (reason) => {
956
+ resolveClose(reason ?? {
957
+ status: void 0,
958
+ isLoggedOut: false,
959
+ error: "closed"
960
+ });
961
+ },
962
+ ...createWebSendApi({
963
+ sock: {
964
+ sendMessage: (jid, content) => sock.sendMessage(jid, content),
965
+ sendPresenceUpdate: (presence, jid) => sock.sendPresenceUpdate(presence, jid)
966
+ },
967
+ defaultAccountId: options.accountId
968
+ })
969
+ };
970
+ }
971
+
972
+ //#endregion
973
+ //#region src/web/auto-reply/mentions.ts
974
+ function buildMentionConfig(cfg, agentId) {
975
+ return {
976
+ mentionRegexes: buildMentionRegexes(cfg, agentId),
977
+ allowFrom: cfg.channels?.whatsapp?.allowFrom
978
+ };
979
+ }
980
+ function resolveMentionTargets(msg, authDir) {
981
+ const jidOptions = authDir ? { authDir } : void 0;
982
+ return {
983
+ normalizedMentions: msg.mentionedJids?.length ? msg.mentionedJids.map((jid) => jidToE164(jid, jidOptions) ?? jid).filter(Boolean) : [],
984
+ selfE164: msg.selfE164 ?? (msg.selfJid ? jidToE164(msg.selfJid, jidOptions) : null),
985
+ selfJid: msg.selfJid ? msg.selfJid.replace(/:\\d+/, "") : null
986
+ };
987
+ }
988
+ function isBotMentionedFromTargets(msg, mentionCfg, targets) {
989
+ const clean = (text) => normalizeMentionText(text);
990
+ const isSelfChat = isSelfChatMode(targets.selfE164, mentionCfg.allowFrom);
991
+ const hasMentions = (msg.mentionedJids?.length ?? 0) > 0;
992
+ if (hasMentions && !isSelfChat) {
993
+ if (targets.selfE164 && targets.normalizedMentions.includes(targets.selfE164)) return true;
994
+ if (targets.selfJid) {
995
+ if (targets.normalizedMentions.includes(targets.selfJid)) return true;
996
+ }
997
+ return false;
998
+ } else if (hasMentions && isSelfChat) {}
999
+ const bodyClean = clean(msg.body);
1000
+ if (mentionCfg.mentionRegexes.some((re) => re.test(bodyClean))) return true;
1001
+ if (targets.selfE164) {
1002
+ const selfDigits = targets.selfE164.replace(/\D/g, "");
1003
+ if (selfDigits) {
1004
+ if (bodyClean.replace(/[^\d]/g, "").includes(selfDigits)) return true;
1005
+ const bodyNoSpace = msg.body.replace(/[\s-]/g, "");
1006
+ if (new RegExp(`\\+?${selfDigits}`, "i").test(bodyNoSpace)) return true;
1007
+ }
1008
+ }
1009
+ return false;
1010
+ }
1011
+ function debugMention(msg, mentionCfg, authDir) {
1012
+ const mentionTargets = resolveMentionTargets(msg, authDir);
1013
+ return {
1014
+ wasMentioned: isBotMentionedFromTargets(msg, mentionCfg, mentionTargets),
1015
+ details: {
1016
+ from: msg.from,
1017
+ body: msg.body,
1018
+ bodyClean: normalizeMentionText(msg.body),
1019
+ mentionedJids: msg.mentionedJids ?? null,
1020
+ normalizedMentionedJids: mentionTargets.normalizedMentions.length ? mentionTargets.normalizedMentions : null,
1021
+ selfJid: msg.selfJid ?? null,
1022
+ selfJidBare: mentionTargets.selfJid,
1023
+ selfE164: msg.selfE164 ?? null,
1024
+ resolvedSelfE164: mentionTargets.selfE164
1025
+ }
1026
+ };
1027
+ }
1028
+ function resolveOwnerList(mentionCfg, selfE164) {
1029
+ const allowFrom = mentionCfg.allowFrom;
1030
+ return (Array.isArray(allowFrom) && allowFrom.length > 0 ? allowFrom : selfE164 ? [selfE164] : []).filter((entry) => Boolean(entry && entry !== "*")).map((entry) => normalizeE164(entry)).filter((entry) => Boolean(entry));
1031
+ }
1032
+
1033
+ //#endregion
1034
+ //#region src/web/auto-reply/monitor/echo.ts
1035
+ function createEchoTracker(params) {
1036
+ const recentlySent = /* @__PURE__ */ new Set();
1037
+ const maxItems = Math.max(1, params.maxItems ?? 100);
1038
+ const buildCombinedKey = (p) => `combined:${p.sessionKey}:${p.combinedBody}`;
1039
+ const trim = () => {
1040
+ while (recentlySent.size > maxItems) {
1041
+ const firstKey = recentlySent.values().next().value;
1042
+ if (!firstKey) break;
1043
+ recentlySent.delete(firstKey);
1044
+ }
1045
+ };
1046
+ const rememberText = (text, opts) => {
1047
+ if (!text) return;
1048
+ recentlySent.add(text);
1049
+ if (opts.combinedBody && opts.combinedBodySessionKey) recentlySent.add(buildCombinedKey({
1050
+ sessionKey: opts.combinedBodySessionKey,
1051
+ combinedBody: opts.combinedBody
1052
+ }));
1053
+ if (opts.logVerboseMessage) params.logVerbose?.(`Added to echo detection set (size now: ${recentlySent.size}): ${text.substring(0, 50)}...`);
1054
+ trim();
1055
+ };
1056
+ return {
1057
+ rememberText,
1058
+ has: (key) => recentlySent.has(key),
1059
+ forget: (key) => {
1060
+ recentlySent.delete(key);
1061
+ },
1062
+ buildCombinedKey
1063
+ };
1064
+ }
1065
+
1066
+ //#endregion
1067
+ //#region src/web/auto-reply/monitor/broadcast.ts
1068
+ async function maybeBroadcastMessage(params) {
1069
+ const broadcastAgents = params.cfg.broadcast?.[params.peerId];
1070
+ if (!broadcastAgents || !Array.isArray(broadcastAgents)) return false;
1071
+ if (broadcastAgents.length === 0) return false;
1072
+ const strategy = params.cfg.broadcast?.strategy || "parallel";
1073
+ whatsappInboundLog.info(`Broadcasting message to ${broadcastAgents.length} agents (${strategy})`);
1074
+ const agentIds = params.cfg.agents?.list?.map((agent) => normalizeAgentId(agent.id));
1075
+ const hasKnownAgents = (agentIds?.length ?? 0) > 0;
1076
+ const groupHistorySnapshot = params.msg.chatType === "group" ? params.groupHistories.get(params.groupHistoryKey) ?? [] : void 0;
1077
+ const processForAgent = async (agentId) => {
1078
+ const normalizedAgentId = normalizeAgentId(agentId);
1079
+ if (hasKnownAgents && !agentIds?.includes(normalizedAgentId)) {
1080
+ whatsappInboundLog.warn(`Broadcast agent ${agentId} not found in agents.list; skipping`);
1081
+ return false;
1082
+ }
1083
+ const agentRoute = {
1084
+ ...params.route,
1085
+ agentId: normalizedAgentId,
1086
+ sessionKey: buildAgentSessionKey({
1087
+ agentId: normalizedAgentId,
1088
+ channel: "whatsapp",
1089
+ accountId: params.route.accountId,
1090
+ peer: {
1091
+ kind: params.msg.chatType === "group" ? "group" : "direct",
1092
+ id: params.peerId
1093
+ },
1094
+ dmScope: params.cfg.session?.dmScope,
1095
+ identityLinks: params.cfg.session?.identityLinks
1096
+ }),
1097
+ mainSessionKey: buildAgentMainSessionKey({
1098
+ agentId: normalizedAgentId,
1099
+ mainKey: DEFAULT_MAIN_KEY
1100
+ })
1101
+ };
1102
+ try {
1103
+ return await params.processMessage(params.msg, agentRoute, params.groupHistoryKey, {
1104
+ groupHistory: groupHistorySnapshot,
1105
+ suppressGroupHistoryClear: true
1106
+ });
1107
+ } catch (err) {
1108
+ whatsappInboundLog.error(`Broadcast agent ${agentId} failed: ${formatError(err)}`);
1109
+ return false;
1110
+ }
1111
+ };
1112
+ if (strategy === "sequential") for (const agentId of broadcastAgents) await processForAgent(agentId);
1113
+ else await Promise.allSettled(broadcastAgents.map(processForAgent));
1114
+ if (params.msg.chatType === "group") params.groupHistories.set(params.groupHistoryKey, []);
1115
+ return true;
1116
+ }
1117
+
1118
+ //#endregion
1119
+ //#region src/web/auto-reply/monitor/commands.ts
1120
+ function stripMentionsForCommand(text, mentionRegexes, selfE164) {
1121
+ let result = text;
1122
+ for (const re of mentionRegexes) result = result.replace(re, " ");
1123
+ if (selfE164) {
1124
+ const digits = selfE164.replace(/\D/g, "");
1125
+ if (digits) {
1126
+ const pattern = new RegExp(`\\+?${digits}`, "g");
1127
+ result = result.replace(pattern, " ");
1128
+ }
1129
+ }
1130
+ return result.replace(/\s+/g, " ").trim();
1131
+ }
1132
+
1133
+ //#endregion
1134
+ //#region src/web/auto-reply/monitor/group-activation.ts
1135
+ function resolveGroupPolicyFor(cfg, conversationId) {
1136
+ const groupId = resolveGroupSessionKey({
1137
+ From: conversationId,
1138
+ ChatType: "group",
1139
+ Provider: "whatsapp"
1140
+ })?.id;
1141
+ const whatsappCfg = cfg.channels?.whatsapp;
1142
+ const hasGroupAllowFrom = Boolean(whatsappCfg?.groupAllowFrom?.length || whatsappCfg?.allowFrom?.length);
1143
+ return resolveChannelGroupPolicy({
1144
+ cfg,
1145
+ channel: "whatsapp",
1146
+ groupId: groupId ?? conversationId,
1147
+ hasGroupAllowFrom
1148
+ });
1149
+ }
1150
+ function resolveGroupRequireMentionFor(cfg, conversationId) {
1151
+ const groupId = resolveGroupSessionKey({
1152
+ From: conversationId,
1153
+ ChatType: "group",
1154
+ Provider: "whatsapp"
1155
+ })?.id;
1156
+ return resolveChannelGroupRequireMention({
1157
+ cfg,
1158
+ channel: "whatsapp",
1159
+ groupId: groupId ?? conversationId
1160
+ });
1161
+ }
1162
+ function resolveGroupActivationFor(params) {
1163
+ const entry = loadSessionStore(resolveStorePath(params.cfg.session?.store, { agentId: params.agentId }))[params.sessionKey];
1164
+ const defaultActivation = !resolveGroupRequireMentionFor(params.cfg, params.conversationId) ? "always" : "mention";
1165
+ return normalizeGroupActivation(entry?.groupActivation) ?? defaultActivation;
1166
+ }
1167
+
1168
+ //#endregion
1169
+ //#region src/web/auto-reply/monitor/group-members.ts
1170
+ function appendNormalizedUnique(entries, seen, ordered) {
1171
+ for (const entry of entries) {
1172
+ const normalized = normalizeE164(entry) ?? entry;
1173
+ if (!normalized || seen.has(normalized)) continue;
1174
+ seen.add(normalized);
1175
+ ordered.push(normalized);
1176
+ }
1177
+ }
1178
+ function noteGroupMember(groupMemberNames, conversationId, e164, name) {
1179
+ if (!e164 || !name) return;
1180
+ const key = normalizeE164(e164) ?? e164;
1181
+ if (!key) return;
1182
+ let roster = groupMemberNames.get(conversationId);
1183
+ if (!roster) {
1184
+ roster = /* @__PURE__ */ new Map();
1185
+ groupMemberNames.set(conversationId, roster);
1186
+ }
1187
+ roster.set(key, name);
1188
+ }
1189
+ function formatGroupMembers(params) {
1190
+ const { participants, roster, fallbackE164 } = params;
1191
+ const seen = /* @__PURE__ */ new Set();
1192
+ const ordered = [];
1193
+ if (participants?.length) appendNormalizedUnique(participants, seen, ordered);
1194
+ if (roster) appendNormalizedUnique(roster.keys(), seen, ordered);
1195
+ if (ordered.length === 0 && fallbackE164) {
1196
+ const normalized = normalizeE164(fallbackE164) ?? fallbackE164;
1197
+ if (normalized) ordered.push(normalized);
1198
+ }
1199
+ if (ordered.length === 0) return;
1200
+ return ordered.map((entry) => {
1201
+ const name = roster?.get(entry);
1202
+ return name ? `${name} (${entry})` : entry;
1203
+ }).join(", ");
1204
+ }
1205
+
1206
+ //#endregion
1207
+ //#region src/web/auto-reply/monitor/group-gating.ts
1208
+ function isOwnerSender(baseMentionConfig, msg) {
1209
+ const sender = normalizeE164(msg.senderE164 ?? "");
1210
+ if (!sender) return false;
1211
+ return resolveOwnerList(baseMentionConfig, msg.selfE164 ?? void 0).includes(sender);
1212
+ }
1213
+ function recordPendingGroupHistoryEntry(params) {
1214
+ const sender = params.msg.senderName && params.msg.senderE164 ? `${params.msg.senderName} (${params.msg.senderE164})` : params.msg.senderName ?? params.msg.senderE164 ?? "Unknown";
1215
+ recordPendingHistoryEntryIfEnabled({
1216
+ historyMap: params.groupHistories,
1217
+ historyKey: params.groupHistoryKey,
1218
+ limit: params.groupHistoryLimit,
1219
+ entry: {
1220
+ sender,
1221
+ body: params.msg.body,
1222
+ timestamp: params.msg.timestamp,
1223
+ id: params.msg.id,
1224
+ senderJid: params.msg.senderJid
1225
+ }
1226
+ });
1227
+ }
1228
+ function skipGroupMessageAndStoreHistory(params, verboseMessage) {
1229
+ params.logVerbose(verboseMessage);
1230
+ recordPendingGroupHistoryEntry({
1231
+ msg: params.msg,
1232
+ groupHistories: params.groupHistories,
1233
+ groupHistoryKey: params.groupHistoryKey,
1234
+ groupHistoryLimit: params.groupHistoryLimit
1235
+ });
1236
+ return { shouldProcess: false };
1237
+ }
1238
+ function applyGroupGating(params) {
1239
+ const groupPolicy = resolveGroupPolicyFor(params.cfg, params.conversationId);
1240
+ if (groupPolicy.allowlistEnabled && !groupPolicy.allowed) {
1241
+ params.logVerbose(`Skipping group message ${params.conversationId} (not in allowlist)`);
1242
+ return { shouldProcess: false };
1243
+ }
1244
+ noteGroupMember(params.groupMemberNames, params.groupHistoryKey, params.msg.senderE164, params.msg.senderName);
1245
+ const mentionConfig = buildMentionConfig(params.cfg, params.agentId);
1246
+ const commandBody = stripMentionsForCommand(params.msg.body, mentionConfig.mentionRegexes, params.msg.selfE164);
1247
+ const activationCommand = parseActivationCommand(commandBody);
1248
+ const owner = isOwnerSender(params.baseMentionConfig, params.msg);
1249
+ const shouldBypassMention = owner && hasControlCommand(commandBody, params.cfg);
1250
+ if (activationCommand.hasCommand && !owner) return skipGroupMessageAndStoreHistory(params, `Ignoring /activation from non-owner in group ${params.conversationId}`);
1251
+ const mentionDebug = debugMention(params.msg, mentionConfig, params.authDir);
1252
+ params.replyLogger.debug({
1253
+ conversationId: params.conversationId,
1254
+ wasMentioned: mentionDebug.wasMentioned,
1255
+ ...mentionDebug.details
1256
+ }, "group mention debug");
1257
+ const wasMentioned = mentionDebug.wasMentioned;
1258
+ const requireMention = resolveGroupActivationFor({
1259
+ cfg: params.cfg,
1260
+ agentId: params.agentId,
1261
+ sessionKey: params.sessionKey,
1262
+ conversationId: params.conversationId
1263
+ }) !== "always";
1264
+ const selfJid = params.msg.selfJid?.replace(/:\\d+/, "");
1265
+ const replySenderJid = params.msg.replyToSenderJid?.replace(/:\\d+/, "");
1266
+ const selfE164 = params.msg.selfE164 ? normalizeE164(params.msg.selfE164) : null;
1267
+ const replySenderE164 = params.msg.replyToSenderE164 ? normalizeE164(params.msg.replyToSenderE164) : null;
1268
+ const mentionGate = resolveMentionGating({
1269
+ requireMention,
1270
+ canDetectMention: true,
1271
+ wasMentioned,
1272
+ implicitMention: Boolean(selfJid && replySenderJid && selfJid === replySenderJid || selfE164 && replySenderE164 && selfE164 === replySenderE164),
1273
+ shouldBypassMention
1274
+ });
1275
+ params.msg.wasMentioned = mentionGate.effectiveWasMentioned;
1276
+ if (!shouldBypassMention && requireMention && mentionGate.shouldSkip) return skipGroupMessageAndStoreHistory(params, `Group message stored for context (no mention detected) in ${params.conversationId}: ${params.msg.body}`);
1277
+ return { shouldProcess: true };
1278
+ }
1279
+
1280
+ //#endregion
1281
+ //#region src/web/auto-reply/monitor/last-route.ts
1282
+ function trackBackgroundTask(backgroundTasks, task) {
1283
+ backgroundTasks.add(task);
1284
+ task.finally(() => {
1285
+ backgroundTasks.delete(task);
1286
+ });
1287
+ }
1288
+ function updateLastRouteInBackground(params) {
1289
+ const storePath = resolveStorePath(params.cfg.session?.store, { agentId: params.storeAgentId });
1290
+ const task = updateLastRoute({
1291
+ storePath,
1292
+ sessionKey: params.sessionKey,
1293
+ deliveryContext: {
1294
+ channel: params.channel,
1295
+ to: params.to,
1296
+ accountId: params.accountId
1297
+ },
1298
+ ctx: params.ctx
1299
+ }).catch((err) => {
1300
+ params.warn({
1301
+ error: formatError(err),
1302
+ storePath,
1303
+ sessionKey: params.sessionKey,
1304
+ to: params.to
1305
+ }, "failed updating last route");
1306
+ });
1307
+ trackBackgroundTask(params.backgroundTasks, task);
1308
+ }
1309
+
1310
+ //#endregion
1311
+ //#region src/web/auto-reply/monitor/peer.ts
1312
+ function resolvePeerId(msg) {
1313
+ if (msg.chatType === "group") return msg.conversationId ?? msg.from;
1314
+ if (msg.senderE164) return normalizeE164(msg.senderE164) ?? msg.senderE164;
1315
+ if (msg.from.includes("@")) return jidToE164(msg.from) ?? msg.from;
1316
+ return normalizeE164(msg.from) ?? msg.from;
1317
+ }
1318
+
1319
+ //#endregion
1320
+ //#region src/web/auto-reply/util.ts
1321
+ function elide(text, limit = 400) {
1322
+ if (!text) return text;
1323
+ if (text.length <= limit) return text;
1324
+ return `${text.slice(0, limit)}… (truncated ${text.length - limit} chars)`;
1325
+ }
1326
+ function isLikelyWhatsAppCryptoError(reason) {
1327
+ const formatReason = (value) => {
1328
+ if (value == null) return "";
1329
+ if (typeof value === "string") return value;
1330
+ if (value instanceof Error) return `${value.message}\n${value.stack ?? ""}`;
1331
+ if (typeof value === "object") try {
1332
+ return JSON.stringify(value);
1333
+ } catch {
1334
+ return Object.prototype.toString.call(value);
1335
+ }
1336
+ if (typeof value === "number") return String(value);
1337
+ if (typeof value === "boolean") return String(value);
1338
+ if (typeof value === "bigint") return String(value);
1339
+ if (typeof value === "symbol") return value.description ?? value.toString();
1340
+ if (typeof value === "function") return value.name ? `[function ${value.name}]` : "[function]";
1341
+ return Object.prototype.toString.call(value);
1342
+ };
1343
+ const haystack = (reason instanceof Error ? `${reason.message}\n${reason.stack ?? ""}` : formatReason(reason)).toLowerCase();
1344
+ if (!(haystack.includes("unsupported state or unable to authenticate data") || haystack.includes("bad mac"))) return false;
1345
+ return haystack.includes("@whiskeysockets/baileys") || haystack.includes("baileys") || haystack.includes("noise-handler") || haystack.includes("aesdecryptgcm");
1346
+ }
1347
+
1348
+ //#endregion
1349
+ //#region src/web/auto-reply/deliver-reply.ts
1350
+ const REASONING_PREFIX = "reasoning:";
1351
+ function shouldSuppressReasoningReply(payload) {
1352
+ if (payload.isReasoning === true) return true;
1353
+ const text = payload.text;
1354
+ if (typeof text !== "string") return false;
1355
+ return text.trimStart().toLowerCase().startsWith(REASONING_PREFIX);
1356
+ }
1357
+ async function deliverWebReply(params) {
1358
+ const { replyResult, msg, maxMediaBytes, textLimit, replyLogger, connectionId, skipLog } = params;
1359
+ const replyStarted = Date.now();
1360
+ if (shouldSuppressReasoningReply(replyResult)) {
1361
+ whatsappOutboundLog.debug(`Suppressed reasoning payload to ${msg.from}`);
1362
+ return;
1363
+ }
1364
+ const tableMode = params.tableMode ?? "code";
1365
+ const chunkMode = params.chunkMode ?? "length";
1366
+ const textChunks = chunkMarkdownTextWithMode(markdownToWhatsApp(convertMarkdownTables(replyResult.text || "", tableMode)), textLimit, chunkMode);
1367
+ const mediaList = replyResult.mediaUrls?.length ? replyResult.mediaUrls : replyResult.mediaUrl ? [replyResult.mediaUrl] : [];
1368
+ const sendWithRetry = async (fn, label, maxAttempts = 3) => {
1369
+ let lastErr;
1370
+ for (let attempt = 1; attempt <= maxAttempts; attempt++) try {
1371
+ return await fn();
1372
+ } catch (err) {
1373
+ lastErr = err;
1374
+ const errText = formatError(err);
1375
+ const isLast = attempt === maxAttempts;
1376
+ if (!/closed|reset|timed\s*out|disconnect/i.test(errText) || isLast) throw err;
1377
+ const backoffMs = 500 * attempt;
1378
+ logVerbose(`Retrying ${label} to ${msg.from} after failure (${attempt}/${maxAttempts - 1}) in ${backoffMs}ms: ${errText}`);
1379
+ await sleep(backoffMs);
1380
+ }
1381
+ throw lastErr;
1382
+ };
1383
+ if (mediaList.length === 0 && textChunks.length) {
1384
+ const totalChunks = textChunks.length;
1385
+ for (const [index, chunk] of textChunks.entries()) {
1386
+ const chunkStarted = Date.now();
1387
+ await sendWithRetry(() => msg.reply(chunk), "text");
1388
+ if (!skipLog) {
1389
+ const durationMs = Date.now() - chunkStarted;
1390
+ whatsappOutboundLog.debug(`Sent chunk ${index + 1}/${totalChunks} to ${msg.from} (${durationMs.toFixed(0)}ms)`);
1391
+ }
1392
+ }
1393
+ replyLogger.info({
1394
+ correlationId: msg.id ?? newConnectionId(),
1395
+ connectionId: connectionId ?? null,
1396
+ to: msg.from,
1397
+ from: msg.to,
1398
+ text: elide(replyResult.text, 240),
1399
+ mediaUrl: null,
1400
+ mediaSizeBytes: null,
1401
+ mediaKind: null,
1402
+ durationMs: Date.now() - replyStarted
1403
+ }, "auto-reply sent (text)");
1404
+ return;
1405
+ }
1406
+ const remainingText = [...textChunks];
1407
+ for (const [index, mediaUrl] of mediaList.entries()) {
1408
+ const caption = index === 0 ? remainingText.shift() || void 0 : void 0;
1409
+ try {
1410
+ const media = await loadWebMedia(mediaUrl, {
1411
+ maxBytes: maxMediaBytes,
1412
+ localRoots: params.mediaLocalRoots
1413
+ });
1414
+ if (shouldLogVerbose()) {
1415
+ logVerbose(`Web auto-reply media size: ${(media.buffer.length / (1024 * 1024)).toFixed(2)}MB`);
1416
+ logVerbose(`Web auto-reply media source: ${mediaUrl} (kind ${media.kind})`);
1417
+ }
1418
+ if (media.kind === "image") await sendWithRetry(() => msg.sendMedia({
1419
+ image: media.buffer,
1420
+ caption,
1421
+ mimetype: media.contentType
1422
+ }), "media:image");
1423
+ else if (media.kind === "audio") await sendWithRetry(() => msg.sendMedia({
1424
+ audio: media.buffer,
1425
+ ptt: true,
1426
+ mimetype: media.contentType,
1427
+ caption
1428
+ }), "media:audio");
1429
+ else if (media.kind === "video") await sendWithRetry(() => msg.sendMedia({
1430
+ video: media.buffer,
1431
+ caption,
1432
+ mimetype: media.contentType
1433
+ }), "media:video");
1434
+ else {
1435
+ const fileName = media.fileName ?? mediaUrl.split("/").pop() ?? "file";
1436
+ const mimetype = media.contentType ?? "application/octet-stream";
1437
+ await sendWithRetry(() => msg.sendMedia({
1438
+ document: media.buffer,
1439
+ fileName,
1440
+ caption,
1441
+ mimetype
1442
+ }), "media:document");
1443
+ }
1444
+ whatsappOutboundLog.info(`Sent media reply to ${msg.from} (${(media.buffer.length / (1024 * 1024)).toFixed(2)}MB)`);
1445
+ replyLogger.info({
1446
+ correlationId: msg.id ?? newConnectionId(),
1447
+ connectionId: connectionId ?? null,
1448
+ to: msg.from,
1449
+ from: msg.to,
1450
+ text: caption ?? null,
1451
+ mediaUrl,
1452
+ mediaSizeBytes: media.buffer.length,
1453
+ mediaKind: media.kind,
1454
+ durationMs: Date.now() - replyStarted
1455
+ }, "auto-reply sent (media)");
1456
+ } catch (err) {
1457
+ whatsappOutboundLog.error(`Failed sending web media to ${msg.from}: ${formatError(err)}`);
1458
+ replyLogger.warn({
1459
+ err,
1460
+ mediaUrl
1461
+ }, "failed to send web media reply");
1462
+ if (index === 0) {
1463
+ const warning = err instanceof Error ? `⚠️ Media failed: ${err.message}` : "⚠️ Media failed.";
1464
+ const fallbackText = [remainingText.shift() ?? caption ?? "", warning].filter(Boolean).join("\n");
1465
+ if (fallbackText) {
1466
+ whatsappOutboundLog.warn(`Media skipped; sent text-only to ${msg.from}`);
1467
+ await msg.reply(fallbackText);
1468
+ }
1469
+ }
1470
+ }
1471
+ }
1472
+ for (const chunk of remainingText) await msg.reply(chunk);
1473
+ }
1474
+
1475
+ //#endregion
1476
+ //#region src/web/auto-reply/monitor/ack-reaction.ts
1477
+ function maybeSendAckReaction(params) {
1478
+ if (!params.msg.id) return;
1479
+ const ackConfig = params.cfg.channels?.whatsapp?.ackReaction;
1480
+ const emoji = (ackConfig?.emoji ?? "").trim();
1481
+ const directEnabled = ackConfig?.direct ?? true;
1482
+ const groupMode = ackConfig?.group ?? "mentions";
1483
+ const conversationIdForCheck = params.msg.conversationId ?? params.msg.from;
1484
+ const activation = params.msg.chatType === "group" ? resolveGroupActivationFor({
1485
+ cfg: params.cfg,
1486
+ agentId: params.agentId,
1487
+ sessionKey: params.sessionKey,
1488
+ conversationId: conversationIdForCheck
1489
+ }) : null;
1490
+ const shouldSendReaction = () => shouldAckReactionForWhatsApp({
1491
+ emoji,
1492
+ isDirect: params.msg.chatType === "direct",
1493
+ isGroup: params.msg.chatType === "group",
1494
+ directEnabled,
1495
+ groupMode,
1496
+ wasMentioned: params.msg.wasMentioned === true,
1497
+ groupActivated: activation === "always"
1498
+ });
1499
+ if (!shouldSendReaction()) return;
1500
+ params.info({
1501
+ chatId: params.msg.chatId,
1502
+ messageId: params.msg.id,
1503
+ emoji
1504
+ }, "sending ack reaction");
1505
+ sendReactionWhatsApp(params.msg.chatId, params.msg.id, emoji, {
1506
+ verbose: params.verbose,
1507
+ fromMe: false,
1508
+ participant: params.msg.senderJid,
1509
+ accountId: params.accountId
1510
+ }).catch((err) => {
1511
+ params.warn({
1512
+ error: formatError(err),
1513
+ chatId: params.msg.chatId,
1514
+ messageId: params.msg.id
1515
+ }, "failed to send ack reaction");
1516
+ logVerbose(`WhatsApp ack reaction failed for chat ${params.msg.chatId}: ${formatError(err)}`);
1517
+ });
1518
+ }
1519
+
1520
+ //#endregion
1521
+ //#region src/web/auto-reply/monitor/message-line.ts
1522
+ function formatReplyContext(msg) {
1523
+ if (!msg.replyToBody) return null;
1524
+ return `[Replying to ${msg.replyToSender ?? "unknown sender"}${msg.replyToId ? ` id:${msg.replyToId}` : ""}]\n${msg.replyToBody}\n[/Replying]`;
1525
+ }
1526
+ function buildInboundLine(params) {
1527
+ const { cfg, msg, agentId, previousTimestamp, envelope } = params;
1528
+ const messagePrefix = resolveMessagePrefix(cfg, agentId, {
1529
+ configured: cfg.channels?.whatsapp?.messagePrefix,
1530
+ hasAllowFrom: (cfg.channels?.whatsapp?.allowFrom?.length ?? 0) > 0
1531
+ });
1532
+ const prefixStr = messagePrefix ? `${messagePrefix} ` : "";
1533
+ const replyContext = formatReplyContext(msg);
1534
+ const baseLine = `${prefixStr}${msg.body}${replyContext ? `\n\n${replyContext}` : ""}`;
1535
+ return formatInboundEnvelope({
1536
+ channel: "WhatsApp",
1537
+ from: msg.chatType === "group" ? msg.from : msg.from?.replace(/^whatsapp:/, ""),
1538
+ timestamp: msg.timestamp,
1539
+ body: baseLine,
1540
+ chatType: msg.chatType,
1541
+ sender: {
1542
+ name: msg.senderName,
1543
+ e164: msg.senderE164,
1544
+ id: msg.senderJid
1545
+ },
1546
+ previousTimestamp,
1547
+ envelope,
1548
+ fromMe: msg.fromMe
1549
+ });
1550
+ }
1551
+
1552
+ //#endregion
1553
+ //#region src/web/auto-reply/monitor/process-message.ts
1554
+ async function resolveWhatsAppCommandAuthorized(params) {
1555
+ const useAccessGroups = params.cfg.commands?.useAccessGroups !== false;
1556
+ if (!useAccessGroups) return true;
1557
+ const isGroup = params.msg.chatType === "group";
1558
+ const senderE164 = normalizeE164(isGroup ? params.msg.senderE164 ?? "" : params.msg.senderE164 ?? params.msg.from ?? "");
1559
+ if (!senderE164) return false;
1560
+ const account = resolveWhatsAppAccount({
1561
+ cfg: params.cfg,
1562
+ accountId: params.msg.accountId
1563
+ });
1564
+ const dmPolicy = account.dmPolicy ?? "pairing";
1565
+ const groupPolicy = account.groupPolicy ?? "allowlist";
1566
+ const configuredAllowFrom = account.allowFrom ?? [];
1567
+ const configuredGroupAllowFrom = account.groupAllowFrom ?? (configuredAllowFrom.length > 0 ? configuredAllowFrom : void 0);
1568
+ const storeAllowFrom = isGroup ? [] : await readStoreAllowFromForDmPolicy({
1569
+ provider: "whatsapp",
1570
+ accountId: params.msg.accountId,
1571
+ dmPolicy
1572
+ });
1573
+ return resolveDmGroupAccessWithCommandGate({
1574
+ isGroup,
1575
+ dmPolicy,
1576
+ groupPolicy,
1577
+ allowFrom: configuredAllowFrom.length > 0 ? configuredAllowFrom : params.msg.selfE164 ? [params.msg.selfE164] : [],
1578
+ groupAllowFrom: configuredGroupAllowFrom,
1579
+ storeAllowFrom,
1580
+ isSenderAllowed: (allowEntries) => {
1581
+ if (allowEntries.includes("*")) return true;
1582
+ return allowEntries.map((entry) => normalizeE164(String(entry))).filter((entry) => Boolean(entry)).includes(senderE164);
1583
+ },
1584
+ command: {
1585
+ useAccessGroups,
1586
+ allowTextCommands: true,
1587
+ hasControlCommand: true
1588
+ }
1589
+ }).commandAuthorized;
1590
+ }
1591
+ function resolvePinnedMainDmRecipient(params) {
1592
+ const account = resolveWhatsAppAccount({
1593
+ cfg: params.cfg,
1594
+ accountId: params.msg.accountId
1595
+ });
1596
+ return resolvePinnedMainDmOwnerFromAllowlist({
1597
+ dmScope: params.cfg.session?.dmScope,
1598
+ allowFrom: account.allowFrom,
1599
+ normalizeEntry: (entry) => normalizeE164(entry)
1600
+ });
1601
+ }
1602
+ async function processMessage(params) {
1603
+ const conversationId = params.msg.conversationId ?? params.msg.from;
1604
+ const { storePath, envelopeOptions, previousTimestamp } = resolveInboundSessionEnvelopeContext({
1605
+ cfg: params.cfg,
1606
+ agentId: params.route.agentId,
1607
+ sessionKey: params.route.sessionKey
1608
+ });
1609
+ let combinedBody = buildInboundLine({
1610
+ cfg: params.cfg,
1611
+ msg: params.msg,
1612
+ agentId: params.route.agentId,
1613
+ previousTimestamp,
1614
+ envelope: envelopeOptions
1615
+ });
1616
+ let shouldClearGroupHistory = false;
1617
+ if (params.msg.chatType === "group") {
1618
+ const history = params.groupHistory ?? params.groupHistories.get(params.groupHistoryKey) ?? [];
1619
+ if (history.length > 0) combinedBody = buildHistoryContextFromEntries({
1620
+ entries: history.map((m) => ({
1621
+ sender: m.sender,
1622
+ body: m.body,
1623
+ timestamp: m.timestamp
1624
+ })),
1625
+ currentMessage: combinedBody,
1626
+ excludeLast: false,
1627
+ formatEntry: (entry) => {
1628
+ return formatInboundEnvelope({
1629
+ channel: "WhatsApp",
1630
+ from: conversationId,
1631
+ timestamp: entry.timestamp,
1632
+ body: entry.body,
1633
+ chatType: "group",
1634
+ senderLabel: entry.sender,
1635
+ envelope: envelopeOptions
1636
+ });
1637
+ }
1638
+ });
1639
+ shouldClearGroupHistory = !(params.suppressGroupHistoryClear ?? false);
1640
+ }
1641
+ const combinedEchoKey = params.buildCombinedEchoKey({
1642
+ sessionKey: params.route.sessionKey,
1643
+ combinedBody
1644
+ });
1645
+ if (params.echoHas(combinedEchoKey)) {
1646
+ logVerbose("Skipping auto-reply: detected echo for combined message");
1647
+ params.echoForget(combinedEchoKey);
1648
+ return false;
1649
+ }
1650
+ maybeSendAckReaction({
1651
+ cfg: params.cfg,
1652
+ msg: params.msg,
1653
+ agentId: params.route.agentId,
1654
+ sessionKey: params.route.sessionKey,
1655
+ conversationId,
1656
+ verbose: params.verbose,
1657
+ accountId: params.route.accountId,
1658
+ info: params.replyLogger.info.bind(params.replyLogger),
1659
+ warn: params.replyLogger.warn.bind(params.replyLogger)
1660
+ });
1661
+ const correlationId = params.msg.id ?? newConnectionId();
1662
+ params.replyLogger.info({
1663
+ connectionId: params.connectionId,
1664
+ correlationId,
1665
+ from: params.msg.chatType === "group" ? conversationId : params.msg.from,
1666
+ to: params.msg.to,
1667
+ body: elide(combinedBody, 240),
1668
+ mediaType: params.msg.mediaType ?? null,
1669
+ mediaPath: params.msg.mediaPath ?? null
1670
+ }, "inbound web message");
1671
+ const fromDisplay = params.msg.chatType === "group" ? conversationId : params.msg.from;
1672
+ const kindLabel = params.msg.mediaType ? `, ${params.msg.mediaType}` : "";
1673
+ whatsappInboundLog.info(`Inbound message ${fromDisplay} -> ${params.msg.to} (${params.msg.chatType}${kindLabel}, ${combinedBody.length} chars)`);
1674
+ if (shouldLogVerbose()) whatsappInboundLog.debug(`Inbound body: ${elide(combinedBody, 400)}`);
1675
+ const dmRouteTarget = params.msg.chatType !== "group" ? (() => {
1676
+ if (params.msg.senderE164) return normalizeE164(params.msg.senderE164);
1677
+ if (params.msg.from.includes("@")) return jidToE164(params.msg.from);
1678
+ return normalizeE164(params.msg.from);
1679
+ })() : void 0;
1680
+ const textLimit = params.maxMediaTextChunkLimit ?? resolveTextChunkLimit(params.cfg, "whatsapp");
1681
+ const chunkMode = resolveChunkMode(params.cfg, "whatsapp", params.route.accountId);
1682
+ const tableMode = resolveMarkdownTableMode({
1683
+ cfg: params.cfg,
1684
+ channel: "whatsapp",
1685
+ accountId: params.route.accountId
1686
+ });
1687
+ const mediaLocalRoots = getAgentScopedMediaLocalRoots(params.cfg, params.route.agentId);
1688
+ let didLogHeartbeatStrip = false;
1689
+ let didSendReply = false;
1690
+ const commandAuthorized = shouldComputeCommandAuthorized(params.msg.body, params.cfg) ? await resolveWhatsAppCommandAuthorized({
1691
+ cfg: params.cfg,
1692
+ msg: params.msg
1693
+ }) : void 0;
1694
+ const configuredResponsePrefix = params.cfg.messages?.responsePrefix;
1695
+ const { onModelSelected, ...prefixOptions } = createReplyPrefixOptions({
1696
+ cfg: params.cfg,
1697
+ agentId: params.route.agentId,
1698
+ channel: "whatsapp",
1699
+ accountId: params.route.accountId
1700
+ });
1701
+ const isSelfChat = params.msg.chatType !== "group" && Boolean(params.msg.selfE164) && normalizeE164(params.msg.from) === normalizeE164(params.msg.selfE164 ?? "");
1702
+ const responsePrefix = prefixOptions.responsePrefix ?? (configuredResponsePrefix === void 0 && isSelfChat ? resolveIdentityNamePrefix(params.cfg, params.route.agentId) ?? "[squidclaw]" : void 0);
1703
+ const inboundHistory = params.msg.chatType === "group" ? (params.groupHistory ?? params.groupHistories.get(params.groupHistoryKey) ?? []).map((entry) => ({
1704
+ sender: entry.sender,
1705
+ body: entry.body,
1706
+ timestamp: entry.timestamp
1707
+ })) : void 0;
1708
+ const ctxPayload = finalizeInboundContext({
1709
+ Body: combinedBody,
1710
+ BodyForAgent: params.msg.body,
1711
+ InboundHistory: inboundHistory,
1712
+ RawBody: params.msg.body,
1713
+ CommandBody: params.msg.body,
1714
+ From: params.msg.from,
1715
+ To: params.msg.to,
1716
+ SessionKey: params.route.sessionKey,
1717
+ AccountId: params.route.accountId,
1718
+ MessageSid: params.msg.id,
1719
+ ReplyToId: params.msg.replyToId,
1720
+ ReplyToBody: params.msg.replyToBody,
1721
+ ReplyToSender: params.msg.replyToSender,
1722
+ MediaPath: params.msg.mediaPath,
1723
+ MediaUrl: params.msg.mediaUrl,
1724
+ MediaType: params.msg.mediaType,
1725
+ ChatType: params.msg.chatType,
1726
+ ConversationLabel: params.msg.chatType === "group" ? conversationId : params.msg.from,
1727
+ GroupSubject: params.msg.groupSubject,
1728
+ GroupMembers: formatGroupMembers({
1729
+ participants: params.msg.groupParticipants,
1730
+ roster: params.groupMemberNames.get(params.groupHistoryKey),
1731
+ fallbackE164: params.msg.senderE164
1732
+ }),
1733
+ SenderName: params.msg.senderName,
1734
+ SenderId: params.msg.senderJid?.trim() || params.msg.senderE164,
1735
+ SenderE164: params.msg.senderE164,
1736
+ CommandAuthorized: commandAuthorized,
1737
+ WasMentioned: params.msg.wasMentioned,
1738
+ ...params.msg.location ? toLocationContext(params.msg.location) : {},
1739
+ Provider: "whatsapp",
1740
+ Surface: "whatsapp",
1741
+ OriginatingChannel: "whatsapp",
1742
+ OriginatingTo: params.msg.from
1743
+ });
1744
+ const pinnedMainDmRecipient = resolvePinnedMainDmRecipient({
1745
+ cfg: params.cfg,
1746
+ msg: params.msg
1747
+ });
1748
+ const shouldUpdateMainLastRoute = !pinnedMainDmRecipient || pinnedMainDmRecipient === dmRouteTarget;
1749
+ if (dmRouteTarget && params.route.sessionKey === params.route.mainSessionKey && shouldUpdateMainLastRoute) updateLastRouteInBackground({
1750
+ cfg: params.cfg,
1751
+ backgroundTasks: params.backgroundTasks,
1752
+ storeAgentId: params.route.agentId,
1753
+ sessionKey: params.route.mainSessionKey,
1754
+ channel: "whatsapp",
1755
+ to: dmRouteTarget,
1756
+ accountId: params.route.accountId,
1757
+ ctx: ctxPayload,
1758
+ warn: params.replyLogger.warn.bind(params.replyLogger)
1759
+ });
1760
+ else if (dmRouteTarget && params.route.sessionKey === params.route.mainSessionKey && pinnedMainDmRecipient) logVerbose(`Skipping main-session last route update for ${dmRouteTarget} (pinned owner ${pinnedMainDmRecipient})`);
1761
+ const metaTask = recordSessionMetaFromInbound({
1762
+ storePath,
1763
+ sessionKey: params.route.sessionKey,
1764
+ ctx: ctxPayload
1765
+ }).catch((err) => {
1766
+ params.replyLogger.warn({
1767
+ error: formatError(err),
1768
+ storePath,
1769
+ sessionKey: params.route.sessionKey
1770
+ }, "failed updating session meta");
1771
+ });
1772
+ trackBackgroundTask(params.backgroundTasks, metaTask);
1773
+ const { queuedFinal } = await dispatchReplyWithBufferedBlockDispatcher({
1774
+ ctx: ctxPayload,
1775
+ cfg: params.cfg,
1776
+ replyResolver: params.replyResolver,
1777
+ dispatcherOptions: {
1778
+ ...prefixOptions,
1779
+ responsePrefix,
1780
+ onHeartbeatStrip: () => {
1781
+ if (!didLogHeartbeatStrip) {
1782
+ didLogHeartbeatStrip = true;
1783
+ logVerbose("Stripped stray HEARTBEAT_OK token from web reply");
1784
+ }
1785
+ },
1786
+ deliver: async (payload, info) => {
1787
+ if (info.kind !== "final") return;
1788
+ await deliverWebReply({
1789
+ replyResult: payload,
1790
+ msg: params.msg,
1791
+ mediaLocalRoots,
1792
+ maxMediaBytes: params.maxMediaBytes,
1793
+ textLimit,
1794
+ chunkMode,
1795
+ replyLogger: params.replyLogger,
1796
+ connectionId: params.connectionId,
1797
+ skipLog: false,
1798
+ tableMode
1799
+ });
1800
+ didSendReply = true;
1801
+ const shouldLog = payload.text ? true : void 0;
1802
+ params.rememberSentText(payload.text, {
1803
+ combinedBody,
1804
+ combinedBodySessionKey: params.route.sessionKey,
1805
+ logVerboseMessage: shouldLog
1806
+ });
1807
+ const fromDisplay = params.msg.chatType === "group" ? conversationId : params.msg.from ?? "unknown";
1808
+ const hasMedia = Boolean(payload.mediaUrl || payload.mediaUrls?.length);
1809
+ whatsappOutboundLog.info(`Auto-replied to ${fromDisplay}${hasMedia ? " (media)" : ""}`);
1810
+ if (shouldLogVerbose()) {
1811
+ const preview = payload.text != null ? elide(payload.text, 400) : "<media>";
1812
+ whatsappOutboundLog.debug(`Reply body: ${preview}${hasMedia ? " (media)" : ""}`);
1813
+ }
1814
+ },
1815
+ onError: (err, info) => {
1816
+ const label = info.kind === "tool" ? "tool update" : info.kind === "block" ? "block update" : "auto-reply";
1817
+ whatsappOutboundLog.error(`Failed sending web ${label} to ${params.msg.from ?? conversationId}: ${formatError(err)}`);
1818
+ },
1819
+ onReplyStart: params.msg.sendComposing
1820
+ },
1821
+ replyOptions: {
1822
+ disableBlockStreaming: true,
1823
+ onModelSelected
1824
+ }
1825
+ });
1826
+ if (!queuedFinal) {
1827
+ if (shouldClearGroupHistory) params.groupHistories.set(params.groupHistoryKey, []);
1828
+ logVerbose("Skipping auto-reply: silent token or no text/media returned from resolver");
1829
+ return false;
1830
+ }
1831
+ if (shouldClearGroupHistory) params.groupHistories.set(params.groupHistoryKey, []);
1832
+ return didSendReply;
1833
+ }
1834
+
1835
+ //#endregion
1836
+ //#region src/web/auto-reply/monitor/on-message.ts
1837
+ function createWebOnMessageHandler(params) {
1838
+ const processForRoute = async (msg, route, groupHistoryKey, opts) => processMessage({
1839
+ cfg: params.cfg,
1840
+ msg,
1841
+ route,
1842
+ groupHistoryKey,
1843
+ groupHistories: params.groupHistories,
1844
+ groupMemberNames: params.groupMemberNames,
1845
+ connectionId: params.connectionId,
1846
+ verbose: params.verbose,
1847
+ maxMediaBytes: params.maxMediaBytes,
1848
+ replyResolver: params.replyResolver,
1849
+ replyLogger: params.replyLogger,
1850
+ backgroundTasks: params.backgroundTasks,
1851
+ rememberSentText: params.echoTracker.rememberText,
1852
+ echoHas: params.echoTracker.has,
1853
+ echoForget: params.echoTracker.forget,
1854
+ buildCombinedEchoKey: params.echoTracker.buildCombinedKey,
1855
+ groupHistory: opts?.groupHistory,
1856
+ suppressGroupHistoryClear: opts?.suppressGroupHistoryClear
1857
+ });
1858
+ return async (msg) => {
1859
+ const conversationId = msg.conversationId ?? msg.from;
1860
+ const peerId = resolvePeerId(msg);
1861
+ const route = resolveAgentRoute({
1862
+ cfg: loadConfig(),
1863
+ channel: "whatsapp",
1864
+ accountId: msg.accountId,
1865
+ peer: {
1866
+ kind: msg.chatType === "group" ? "group" : "direct",
1867
+ id: peerId
1868
+ }
1869
+ });
1870
+ const groupHistoryKey = msg.chatType === "group" ? buildGroupHistoryKey({
1871
+ channel: "whatsapp",
1872
+ accountId: route.accountId,
1873
+ peerKind: "group",
1874
+ peerId
1875
+ }) : route.sessionKey;
1876
+ if (msg.from === msg.to) logVerbose(`📱 Same-phone mode detected (from === to: ${msg.from})`);
1877
+ if (params.echoTracker.has(msg.body)) {
1878
+ logVerbose("Skipping auto-reply: detected echo (message matches recently sent text)");
1879
+ params.echoTracker.forget(msg.body);
1880
+ return;
1881
+ }
1882
+ if (msg.chatType === "group") {
1883
+ const metaCtx = {
1884
+ From: msg.from,
1885
+ To: msg.to,
1886
+ SessionKey: route.sessionKey,
1887
+ AccountId: route.accountId,
1888
+ ChatType: msg.chatType,
1889
+ ConversationLabel: conversationId,
1890
+ GroupSubject: msg.groupSubject,
1891
+ SenderName: msg.senderName,
1892
+ SenderId: msg.senderJid?.trim() || msg.senderE164,
1893
+ SenderE164: msg.senderE164,
1894
+ Provider: "whatsapp",
1895
+ Surface: "whatsapp",
1896
+ OriginatingChannel: "whatsapp",
1897
+ OriginatingTo: conversationId
1898
+ };
1899
+ updateLastRouteInBackground({
1900
+ cfg: params.cfg,
1901
+ backgroundTasks: params.backgroundTasks,
1902
+ storeAgentId: route.agentId,
1903
+ sessionKey: route.sessionKey,
1904
+ channel: "whatsapp",
1905
+ to: conversationId,
1906
+ accountId: route.accountId,
1907
+ ctx: metaCtx,
1908
+ warn: params.replyLogger.warn.bind(params.replyLogger)
1909
+ });
1910
+ if (!applyGroupGating({
1911
+ cfg: params.cfg,
1912
+ msg,
1913
+ conversationId,
1914
+ groupHistoryKey,
1915
+ agentId: route.agentId,
1916
+ sessionKey: route.sessionKey,
1917
+ baseMentionConfig: params.baseMentionConfig,
1918
+ authDir: params.account.authDir,
1919
+ groupHistories: params.groupHistories,
1920
+ groupHistoryLimit: params.groupHistoryLimit,
1921
+ groupMemberNames: params.groupMemberNames,
1922
+ logVerbose,
1923
+ replyLogger: params.replyLogger
1924
+ }).shouldProcess) return;
1925
+ } else if (!msg.senderE164 && peerId && peerId.startsWith("+")) msg.senderE164 = normalizeE164(peerId) ?? msg.senderE164;
1926
+ if (await maybeBroadcastMessage({
1927
+ cfg: params.cfg,
1928
+ msg,
1929
+ peerId,
1930
+ route,
1931
+ groupHistoryKey,
1932
+ groupHistories: params.groupHistories,
1933
+ processMessage: processForRoute
1934
+ })) return;
1935
+ await processForRoute(msg, route, groupHistoryKey);
1936
+ };
1937
+ }
1938
+
1939
+ //#endregion
1940
+ //#region src/web/auto-reply/monitor.ts
1941
+ function isNonRetryableWebCloseStatus(statusCode) {
1942
+ return statusCode === 440;
1943
+ }
1944
+ async function monitorWebChannel(verbose, listenerFactory = monitorWebInbox, keepAlive = true, replyResolver = getReplyFromConfig, runtime = defaultRuntime, abortSignal, tuning = {}) {
1945
+ const runId = newConnectionId();
1946
+ const replyLogger = getChildLogger({
1947
+ module: "web-auto-reply",
1948
+ runId
1949
+ });
1950
+ const heartbeatLogger = getChildLogger({
1951
+ module: "web-heartbeat",
1952
+ runId
1953
+ });
1954
+ const reconnectLogger = getChildLogger({
1955
+ module: "web-reconnect",
1956
+ runId
1957
+ });
1958
+ const status = {
1959
+ running: true,
1960
+ connected: false,
1961
+ reconnectAttempts: 0,
1962
+ lastConnectedAt: null,
1963
+ lastDisconnect: null,
1964
+ lastMessageAt: null,
1965
+ lastEventAt: null,
1966
+ lastError: null
1967
+ };
1968
+ const emitStatus = () => {
1969
+ tuning.statusSink?.({
1970
+ ...status,
1971
+ lastDisconnect: status.lastDisconnect ? { ...status.lastDisconnect } : null
1972
+ });
1973
+ };
1974
+ emitStatus();
1975
+ const baseCfg = loadConfig();
1976
+ const account = resolveWhatsAppAccount({
1977
+ cfg: baseCfg,
1978
+ accountId: tuning.accountId
1979
+ });
1980
+ const cfg = {
1981
+ ...baseCfg,
1982
+ channels: {
1983
+ ...baseCfg.channels,
1984
+ whatsapp: {
1985
+ ...baseCfg.channels?.whatsapp,
1986
+ ackReaction: account.ackReaction,
1987
+ messagePrefix: account.messagePrefix,
1988
+ allowFrom: account.allowFrom,
1989
+ groupAllowFrom: account.groupAllowFrom,
1990
+ groupPolicy: account.groupPolicy,
1991
+ textChunkLimit: account.textChunkLimit,
1992
+ chunkMode: account.chunkMode,
1993
+ mediaMaxMb: account.mediaMaxMb,
1994
+ blockStreaming: account.blockStreaming,
1995
+ groups: account.groups
1996
+ }
1997
+ }
1998
+ };
1999
+ const configuredMaxMb = cfg.agents?.defaults?.mediaMaxMb;
2000
+ const maxMediaBytes = typeof configuredMaxMb === "number" && configuredMaxMb > 0 ? configuredMaxMb * 1024 * 1024 : DEFAULT_WEB_MEDIA_BYTES;
2001
+ const heartbeatSeconds = resolveHeartbeatSeconds(cfg, tuning.heartbeatSeconds);
2002
+ const reconnectPolicy = resolveReconnectPolicy(cfg, tuning.reconnect);
2003
+ const baseMentionConfig = buildMentionConfig(cfg);
2004
+ const groupHistoryLimit = cfg.channels?.whatsapp?.accounts?.[tuning.accountId ?? ""]?.historyLimit ?? cfg.channels?.whatsapp?.historyLimit ?? cfg.messages?.groupChat?.historyLimit ?? DEFAULT_GROUP_HISTORY_LIMIT;
2005
+ const groupHistories = /* @__PURE__ */ new Map();
2006
+ const groupMemberNames = /* @__PURE__ */ new Map();
2007
+ const echoTracker = createEchoTracker({
2008
+ maxItems: 100,
2009
+ logVerbose
2010
+ });
2011
+ const sleep = tuning.sleep ?? ((ms, signal) => sleepWithAbort(ms, signal ?? abortSignal));
2012
+ const stopRequested = () => abortSignal?.aborted === true;
2013
+ const abortPromise = abortSignal && new Promise((resolve) => abortSignal.addEventListener("abort", () => resolve("aborted"), { once: true }));
2014
+ const currentMaxListeners = process.getMaxListeners?.() ?? 10;
2015
+ if (process.setMaxListeners && currentMaxListeners < 50) process.setMaxListeners(50);
2016
+ let sigintStop = false;
2017
+ const handleSigint = () => {
2018
+ sigintStop = true;
2019
+ };
2020
+ process.once("SIGINT", handleSigint);
2021
+ let reconnectAttempts = 0;
2022
+ while (true) {
2023
+ if (stopRequested()) break;
2024
+ const connectionId = newConnectionId();
2025
+ const startedAt = Date.now();
2026
+ let heartbeat = null;
2027
+ let watchdogTimer = null;
2028
+ let lastMessageAt = null;
2029
+ let handledMessages = 0;
2030
+ let unregisterUnhandled = null;
2031
+ const MESSAGE_TIMEOUT_MS = tuning.messageTimeoutMs ?? 1800 * 1e3;
2032
+ const WATCHDOG_CHECK_MS = tuning.watchdogCheckMs ?? 60 * 1e3;
2033
+ const backgroundTasks = /* @__PURE__ */ new Set();
2034
+ const onMessage = createWebOnMessageHandler({
2035
+ cfg,
2036
+ verbose,
2037
+ connectionId,
2038
+ maxMediaBytes,
2039
+ groupHistoryLimit,
2040
+ groupHistories,
2041
+ groupMemberNames,
2042
+ echoTracker,
2043
+ backgroundTasks,
2044
+ replyResolver: replyResolver ?? getReplyFromConfig,
2045
+ replyLogger,
2046
+ baseMentionConfig,
2047
+ account
2048
+ });
2049
+ const inboundDebounceMs = resolveInboundDebounceMs({
2050
+ cfg,
2051
+ channel: "whatsapp"
2052
+ });
2053
+ const shouldDebounce = (msg) => {
2054
+ if (msg.mediaPath || msg.mediaType) return false;
2055
+ if (msg.location) return false;
2056
+ if (msg.replyToId || msg.replyToBody) return false;
2057
+ return !hasControlCommand(msg.body, cfg);
2058
+ };
2059
+ const listener = await (listenerFactory ?? monitorWebInbox)({
2060
+ verbose,
2061
+ accountId: account.accountId,
2062
+ authDir: account.authDir,
2063
+ mediaMaxMb: account.mediaMaxMb,
2064
+ sendReadReceipts: account.sendReadReceipts,
2065
+ debounceMs: inboundDebounceMs,
2066
+ shouldDebounce,
2067
+ onMessage: async (msg) => {
2068
+ handledMessages += 1;
2069
+ lastMessageAt = Date.now();
2070
+ status.lastMessageAt = lastMessageAt;
2071
+ status.lastEventAt = lastMessageAt;
2072
+ emitStatus();
2073
+ await onMessage(msg);
2074
+ }
2075
+ });
2076
+ status.connected = true;
2077
+ status.lastConnectedAt = Date.now();
2078
+ status.lastEventAt = status.lastConnectedAt;
2079
+ status.lastError = null;
2080
+ emitStatus();
2081
+ const { e164: selfE164 } = readWebSelfId(account.authDir);
2082
+ const connectRoute = resolveAgentRoute({
2083
+ cfg,
2084
+ channel: "whatsapp",
2085
+ accountId: account.accountId
2086
+ });
2087
+ enqueueSystemEvent(`WhatsApp gateway connected${selfE164 ? ` as ${selfE164}` : ""}.`, { sessionKey: connectRoute.sessionKey });
2088
+ setActiveWebListener(account.accountId, listener);
2089
+ unregisterUnhandled = registerUnhandledRejectionHandler((reason) => {
2090
+ if (!isLikelyWhatsAppCryptoError(reason)) return false;
2091
+ const errorStr = formatError(reason);
2092
+ reconnectLogger.warn({
2093
+ connectionId,
2094
+ error: errorStr
2095
+ }, "web reconnect: unhandled rejection from WhatsApp socket; forcing reconnect");
2096
+ listener.signalClose?.({
2097
+ status: 499,
2098
+ isLoggedOut: false,
2099
+ error: reason
2100
+ });
2101
+ return true;
2102
+ });
2103
+ const closeListener = async () => {
2104
+ setActiveWebListener(account.accountId, null);
2105
+ if (unregisterUnhandled) {
2106
+ unregisterUnhandled();
2107
+ unregisterUnhandled = null;
2108
+ }
2109
+ if (heartbeat) clearInterval(heartbeat);
2110
+ if (watchdogTimer) clearInterval(watchdogTimer);
2111
+ if (backgroundTasks.size > 0) {
2112
+ await Promise.allSettled(backgroundTasks);
2113
+ backgroundTasks.clear();
2114
+ }
2115
+ try {
2116
+ await listener.close();
2117
+ } catch (err) {
2118
+ logVerbose(`Socket close failed: ${formatError(err)}`);
2119
+ }
2120
+ };
2121
+ if (keepAlive) {
2122
+ heartbeat = setInterval(() => {
2123
+ const authAgeMs = getWebAuthAgeMs(account.authDir);
2124
+ const minutesSinceLastMessage = lastMessageAt ? Math.floor((Date.now() - lastMessageAt) / 6e4) : null;
2125
+ const logData = {
2126
+ connectionId,
2127
+ reconnectAttempts,
2128
+ messagesHandled: handledMessages,
2129
+ lastMessageAt,
2130
+ authAgeMs,
2131
+ uptimeMs: Date.now() - startedAt,
2132
+ ...minutesSinceLastMessage !== null && minutesSinceLastMessage > 30 ? { minutesSinceLastMessage } : {}
2133
+ };
2134
+ if (minutesSinceLastMessage && minutesSinceLastMessage > 30) heartbeatLogger.warn(logData, "⚠️ web gateway heartbeat - no messages in 30+ minutes");
2135
+ else heartbeatLogger.info(logData, "web gateway heartbeat");
2136
+ }, heartbeatSeconds * 1e3);
2137
+ watchdogTimer = setInterval(() => {
2138
+ if (!lastMessageAt) return;
2139
+ const timeSinceLastMessage = Date.now() - lastMessageAt;
2140
+ if (timeSinceLastMessage <= MESSAGE_TIMEOUT_MS) return;
2141
+ const minutesSinceLastMessage = Math.floor(timeSinceLastMessage / 6e4);
2142
+ heartbeatLogger.warn({
2143
+ connectionId,
2144
+ minutesSinceLastMessage,
2145
+ lastMessageAt: new Date(lastMessageAt),
2146
+ messagesHandled: handledMessages
2147
+ }, "Message timeout detected - forcing reconnect");
2148
+ whatsappHeartbeatLog.warn(`No messages received in ${minutesSinceLastMessage}m - restarting connection`);
2149
+ closeListener().catch((err) => {
2150
+ logVerbose(`Close listener failed: ${formatError(err)}`);
2151
+ });
2152
+ listener.signalClose?.({
2153
+ status: 499,
2154
+ isLoggedOut: false,
2155
+ error: "watchdog-timeout"
2156
+ });
2157
+ }, WATCHDOG_CHECK_MS);
2158
+ }
2159
+ whatsappLog.info("Listening for personal WhatsApp inbound messages.");
2160
+ if (process.stdout.isTTY || process.stderr.isTTY) whatsappLog.raw("Ctrl+C to stop.");
2161
+ if (!keepAlive) {
2162
+ await closeListener();
2163
+ process.removeListener("SIGINT", handleSigint);
2164
+ return;
2165
+ }
2166
+ const reason = await Promise.race([listener.onClose?.catch((err) => {
2167
+ reconnectLogger.error({ error: formatError(err) }, "listener.onClose rejected");
2168
+ return {
2169
+ status: 500,
2170
+ isLoggedOut: false,
2171
+ error: err
2172
+ };
2173
+ }) ?? waitForever(), abortPromise ?? waitForever()]);
2174
+ if (Date.now() - startedAt > heartbeatSeconds * 1e3) reconnectAttempts = 0;
2175
+ status.reconnectAttempts = reconnectAttempts;
2176
+ emitStatus();
2177
+ if (stopRequested() || sigintStop || reason === "aborted") {
2178
+ await closeListener();
2179
+ break;
2180
+ }
2181
+ const statusCode = (typeof reason === "object" && reason && "status" in reason ? reason.status : void 0) ?? "unknown";
2182
+ const loggedOut = typeof reason === "object" && reason && "isLoggedOut" in reason && reason.isLoggedOut;
2183
+ const errorStr = formatError(reason);
2184
+ status.connected = false;
2185
+ status.lastEventAt = Date.now();
2186
+ status.lastDisconnect = {
2187
+ at: status.lastEventAt,
2188
+ status: typeof statusCode === "number" ? statusCode : void 0,
2189
+ error: errorStr,
2190
+ loggedOut: Boolean(loggedOut)
2191
+ };
2192
+ status.lastError = errorStr;
2193
+ status.reconnectAttempts = reconnectAttempts;
2194
+ emitStatus();
2195
+ reconnectLogger.info({
2196
+ connectionId,
2197
+ status: statusCode,
2198
+ loggedOut,
2199
+ reconnectAttempts,
2200
+ error: errorStr
2201
+ }, "web reconnect: connection closed");
2202
+ enqueueSystemEvent(`WhatsApp gateway disconnected (status ${statusCode ?? "unknown"})`, { sessionKey: connectRoute.sessionKey });
2203
+ if (loggedOut) {
2204
+ runtime.error(`WhatsApp session logged out. Run \`${formatCliCommand("squidclaw channels login --channel web")}\` to relink.`);
2205
+ await closeListener();
2206
+ break;
2207
+ }
2208
+ if (isNonRetryableWebCloseStatus(statusCode)) {
2209
+ reconnectLogger.warn({
2210
+ connectionId,
2211
+ status: statusCode,
2212
+ error: errorStr
2213
+ }, "web reconnect: non-retryable close status; stopping monitor");
2214
+ runtime.error(`WhatsApp Web connection closed (status ${statusCode}: session conflict). Resolve conflicting WhatsApp Web sessions, then relink with \`${formatCliCommand("squidclaw channels login --channel web")}\`. Stopping web monitoring.`);
2215
+ await closeListener();
2216
+ break;
2217
+ }
2218
+ reconnectAttempts += 1;
2219
+ status.reconnectAttempts = reconnectAttempts;
2220
+ emitStatus();
2221
+ if (reconnectPolicy.maxAttempts > 0 && reconnectAttempts >= reconnectPolicy.maxAttempts) {
2222
+ reconnectLogger.warn({
2223
+ connectionId,
2224
+ status: statusCode,
2225
+ reconnectAttempts,
2226
+ maxAttempts: reconnectPolicy.maxAttempts
2227
+ }, "web reconnect: max attempts reached; continuing in degraded mode");
2228
+ runtime.error(`WhatsApp Web reconnect: max attempts reached (${reconnectAttempts}/${reconnectPolicy.maxAttempts}). Stopping web monitoring.`);
2229
+ await closeListener();
2230
+ break;
2231
+ }
2232
+ const delay = computeBackoff(reconnectPolicy, reconnectAttempts);
2233
+ reconnectLogger.info({
2234
+ connectionId,
2235
+ status: statusCode,
2236
+ reconnectAttempts,
2237
+ maxAttempts: reconnectPolicy.maxAttempts || "unlimited",
2238
+ delayMs: delay
2239
+ }, "web reconnect: scheduling retry");
2240
+ runtime.error(`WhatsApp Web connection closed (status ${statusCode}). Retry ${reconnectAttempts}/${reconnectPolicy.maxAttempts || "∞"} in ${formatDurationPrecise(delay)}… (${errorStr})`);
2241
+ await closeListener();
2242
+ try {
2243
+ await sleep(delay, abortSignal);
2244
+ } catch {
2245
+ break;
2246
+ }
2247
+ }
2248
+ status.running = false;
2249
+ status.connected = false;
2250
+ status.lastEventAt = Date.now();
2251
+ emitStatus();
2252
+ process.removeListener("SIGINT", handleSigint);
2253
+ }
2254
+
2255
+ //#endregion
2256
+ export { monitorWebInbox as n, resolveWhatsAppHeartbeatRecipients as r, monitorWebChannel as t };