@spacebar_ai/moldclaw-core 2026.3.14 → 2026.3.16

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 (1074) hide show
  1. package/README.md +108 -3
  2. package/dist/account-id-plS5L20e.d.ts +1 -0
  3. package/dist/accounts-BAYVGC2k.js +109 -0
  4. package/dist/accounts-DrjRgReV.d.ts +103 -0
  5. package/dist/acp-cli-at_UYEOS.js +2088 -0
  6. package/dist/acpx-Chy1GQ_k.d.ts +5 -0
  7. package/dist/actions.runtime-C0F7dMfO.js +114 -0
  8. package/dist/actions.runtime-caI2LG9o.js +128 -0
  9. package/dist/agent-media-payload-CkpAqaOh.d.ts +16 -0
  10. package/dist/agents-B98yPGc5.js +853 -0
  11. package/dist/agents-BrLr08L3.js +217 -0
  12. package/dist/allow-from-BIwT4dl7.d.ts +42 -0
  13. package/dist/allow-list-CHt7yvAf.js +81 -0
  14. package/dist/allowlist-CxQo2wQc.js +142 -0
  15. package/dist/allowlist-resolution-B7ib7gye.d.ts +17 -0
  16. package/dist/api-Co7TNHbL.js +6953 -0
  17. package/dist/api-cEQ_ql_8.js +112 -0
  18. package/dist/audit-AnKnnlaZ.js +787 -0
  19. package/dist/audit-channel.collect.runtime-CAk1DFQ3.js +600 -0
  20. package/dist/audit-channel.runtime-5phdZp_m.js +116 -0
  21. package/dist/audit-extra.async-B8ZXFxic.js +813 -0
  22. package/dist/audit-hdKa3D-u.js +54 -0
  23. package/dist/audit-membership-runtime-CJV5XvGU.js +157 -0
  24. package/dist/audit.deep.runtime-DNMcRQrp.js +24 -0
  25. package/dist/audit.nondeep.runtime-DhNDL6yM.js +831 -0
  26. package/dist/audit.runtime-Bx7uWEh8.js +113 -0
  27. package/dist/auth-choice-C37W9MA7.js +268 -0
  28. package/dist/auth-choice-CNppOY_V.js +117 -0
  29. package/dist/auth-choice-XYFnp6fI.js +502 -0
  30. package/dist/auth-choice-options-D6oZY4Xo.js +123 -0
  31. package/dist/auth-choice-prompt-BhRqchJx.js +110 -0
  32. package/dist/auth-choice-prompt-C1xv0N08.js +36 -0
  33. package/dist/auth-choice.plugin-providers.runtime-DhLEtbmR.js +114 -0
  34. package/dist/auth-profiles-9zZdaXJK.js +127756 -0
  35. package/dist/auth-profiles.runtime-HONFDgiu.js +111 -0
  36. package/dist/bluebubbles-BY8JhO4y.js +64 -0
  37. package/dist/bluebubbles-CQjEnzK_.d.ts +6 -0
  38. package/dist/bluebubbles-RmcKgkBa.d.ts +45 -0
  39. package/dist/boolean-param-F1sMwnPu.d.ts +5 -0
  40. package/dist/bot-BGh-ATV7.d.ts +478 -0
  41. package/dist/brave-CljenznH.js +24 -0
  42. package/dist/browser-cli-CX8i0wf0.js +1492 -0
  43. package/dist/build-info.json +3 -3
  44. package/dist/bundled/boot-md/handler.d.ts +6 -0
  45. package/dist/bundled/boot-md/handler.js +26 -26
  46. package/dist/bundled/bootstrap-extra-files/handler.d.ts +6 -0
  47. package/dist/bundled/command-logger/handler.d.ts +9 -0
  48. package/dist/bundled/session-memory/handler.d.ts +9 -0
  49. package/dist/bundled/session-memory/handler.js +27 -27
  50. package/dist/call-Bc257L16.js +37 -0
  51. package/dist/call-DYFR7oGy.js +639 -0
  52. package/dist/canvas-host/a2ui/.bundle.hash +1 -1
  53. package/dist/channel-Bd-igGEW.js +803 -0
  54. package/dist/channel-BgRMb6bZ.js +575 -0
  55. package/dist/channel-BtcLrU6J.js +1598 -0
  56. package/dist/channel-Bwf6m_hD.js +538 -0
  57. package/dist/channel-C7-kgDBd.js +562 -0
  58. package/dist/channel-CEXOAxIc.js +949 -0
  59. package/dist/channel-CpZ3p9MJ.js +226 -0
  60. package/dist/channel-CqBlN6A2.js +619 -0
  61. package/dist/channel-DKhfHW4U.js +352 -0
  62. package/dist/channel-DS3t_KdJ2.js +316 -0
  63. package/dist/channel-DY24FA1v.js +4681 -0
  64. package/dist/channel-DYFGmImJ.js +542 -0
  65. package/dist/channel-DcyIqX5p.js +207 -0
  66. package/dist/channel-J-2XcAli.js +214 -0
  67. package/dist/channel-N616f4gZ.js +306 -0
  68. package/dist/channel-NY7aU2Gj.js +397 -0
  69. package/dist/channel-PNI8BOmm.js +1321 -0
  70. package/dist/channel-UcXepDJs.js +943 -0
  71. package/dist/channel-account-context-CL3hEq1j.js +103 -0
  72. package/dist/channel-config-schema-Q2nzcCCR.d.ts +1 -0
  73. package/dist/channel-jA_jodJo.js +920 -0
  74. package/dist/channel-options-CtgU5qkG.js +50 -0
  75. package/dist/channel-policy-7wXDp6d3.d.ts +1 -0
  76. package/dist/channel-rGI8uig4.js +497 -0
  77. package/dist/channel-summary-DGJZXo0r.js +106 -0
  78. package/dist/channel.runtime--WZvlNJM.js +413 -0
  79. package/dist/channel.runtime-B0ct42DL.js +122 -0
  80. package/dist/channel.runtime-BEZUZrYB.js +177 -0
  81. package/dist/channel.runtime-BMuWmsIC.js +166 -0
  82. package/dist/channel.runtime-BtvHP0po.js +4006 -0
  83. package/dist/channel.runtime-Cwf993pX.js +194 -0
  84. package/dist/channel.runtime-Cy4lEpTX.js +174 -0
  85. package/dist/channel.runtime-DAz6axda.js +865 -0
  86. package/dist/channel.runtime-DdQ2mOVh.js +236 -0
  87. package/dist/channel.runtime-Dy3HPgOU.js +399 -0
  88. package/dist/channel.runtime-iqfC25k7.js +213 -0
  89. package/dist/channel.setup-B4VYMZlQ.js +9 -0
  90. package/dist/channel.setup-BohGbCbI.js +57 -0
  91. package/dist/channel.setup-Bq2AQqqc.js +6 -0
  92. package/dist/channel.setup-BxiSfLp1.js +8 -0
  93. package/dist/channel.setup-DOUS6fjO.js +8 -0
  94. package/dist/channel.setup-DXhdYU3g.js +9 -0
  95. package/dist/channel.setup-N51CgfNy.js +11 -0
  96. package/dist/channels/plugins/actions/discord.d.ts +3 -0
  97. package/dist/channels/plugins/actions/discord.js +26 -26
  98. package/dist/channels/plugins/actions/signal.d.ts +2 -0
  99. package/dist/channels/plugins/actions/signal.js +26 -26
  100. package/dist/channels/plugins/actions/telegram.d.ts +3 -0
  101. package/dist/channels/plugins/actions/telegram.js +26 -26
  102. package/dist/channels/plugins/agent-tools/whatsapp-login.d.ts +4 -0
  103. package/dist/channels/plugins/agent-tools/whatsapp-login.js +26 -26
  104. package/dist/channels-CueeFf0q.js +404 -0
  105. package/dist/channels-PheAd73E.js +1113 -0
  106. package/dist/channels-cli-CXzVF84v.js +286 -0
  107. package/dist/channels-status-issues-BjWBQHhU.js +16 -0
  108. package/dist/chat-type-BlSN0vo4.d.ts +5 -0
  109. package/dist/clawbot-cli-BBehDXW1.js +113 -0
  110. package/dist/cli/daemon-cli.d.ts +58 -0
  111. package/dist/cli/daemon-cli.js +1 -1
  112. package/dist/cli-CIm7d5Id.js +149 -0
  113. package/dist/command-format-pq3tS8t2.d.ts +4 -0
  114. package/dist/command-registry-CDkp__KH.js +13 -0
  115. package/dist/command-registry-DSEkUBW1.js +212 -0
  116. package/dist/command-secret-gateway-CqP_o0n8.js +106 -0
  117. package/dist/compact.runtime-Qm_csEtG.js +111 -0
  118. package/dist/completion-cli-Ch1sgSLQ.js +445 -0
  119. package/dist/completion-cli-vF067Tso.js +16 -0
  120. package/dist/config-B2W1zTP1.js +44 -0
  121. package/dist/config-CMhKplgO.js +938 -0
  122. package/dist/config-DchtRsvs.js +30 -0
  123. package/dist/config-cli-C41d88_c.js +428 -0
  124. package/dist/config-guard-B_vjkXCQ.js +117 -0
  125. package/dist/config-schema-pPBCF4hz.js +31 -0
  126. package/dist/config-validation-6om9cBUx.js +262 -0
  127. package/dist/config-value-Dl3XEpA6.js +132 -0
  128. package/dist/configure-BxzvDSzu.js +1100 -0
  129. package/dist/configure-CLMLoWAn.js +238 -0
  130. package/dist/control-ui-shared-E8Nz6uKZ.js +29 -0
  131. package/dist/core-Cd3fMFKq.d.ts +87 -0
  132. package/dist/credentials-yYt6VWCq.js +268 -0
  133. package/dist/cron-cli-CA3lV3kh.js +634 -0
  134. package/dist/daemon-cli-BtQuIXEk.js +339 -0
  135. package/dist/daemon-install-BWKGzgMm.js +175 -0
  136. package/dist/deliver-CgMNmfTy.js +106 -0
  137. package/dist/deliver-runtime-Bn1KWoiQ.js +106 -0
  138. package/dist/devices-cli-D601npiL.js +340 -0
  139. package/dist/diagnostic-CkiYEGqt.js +310 -0
  140. package/dist/diffs-B5tZ8Coj.d.ts +1 -0
  141. package/dist/directory-cli-skEV8MT7.js +306 -0
  142. package/dist/directory-config-helpers-B-tiBKIv.d.ts +38 -0
  143. package/dist/directory-runtime-BEJ2fCIR.d.ts +1 -0
  144. package/dist/directory.static-CnyzoWbV.js +44 -0
  145. package/dist/discord-B_gbzPti.js +109 -0
  146. package/dist/discovery-CqI-e_Mv.js +48 -0
  147. package/dist/dm-policy-shared-nybkS1uP.d.ts +95 -0
  148. package/dist/dns-cli-Cjes3Ruw.js +216 -0
  149. package/dist/docs-cli-C3g3Gi_d.js +173 -0
  150. package/dist/doctor-completion-TvgV4SZH.js +90 -0
  151. package/dist/doctor-config-flow-0w9Ux7V8.js +107 -0
  152. package/dist/doctor-config-flow-DLzr8W7Y.js +2437 -0
  153. package/dist/enable-VYzv8b2z.js +24 -0
  154. package/dist/entry.d.ts +7 -0
  155. package/dist/entry.js +1 -1
  156. package/dist/env-overrides-DYVIkuvN.js +434 -0
  157. package/dist/env-overrides.runtime-6kijpIuu.js +17 -0
  158. package/dist/exec-approvals-cli-D_lkTG-l.js +419 -0
  159. package/dist/exec-sVmouhA9.d.ts +39 -0
  160. package/dist/extensions/acpx/index.d.ts +11 -0
  161. package/dist/extensions/acpx/index.js +1 -1
  162. package/dist/extensions/amazon-bedrock/index.d.ts +11 -0
  163. package/dist/extensions/anthropic/index.d.ts +11 -0
  164. package/dist/extensions/anthropic/index.js +26 -26
  165. package/dist/extensions/bluebubbles/index.d.ts +11 -0
  166. package/dist/extensions/bluebubbles/index.js +30 -30
  167. package/dist/extensions/bluebubbles/setup-entry.d.ts +59 -0
  168. package/dist/extensions/bluebubbles/setup-entry.js +30 -30
  169. package/dist/extensions/brave/index.d.ts +11 -0
  170. package/dist/extensions/brave/index.js +2 -2
  171. package/dist/extensions/byteplus/index.d.ts +11 -0
  172. package/dist/extensions/byteplus/index.js +26 -26
  173. package/dist/extensions/cloudflare-ai-gateway/index.d.ts +11 -0
  174. package/dist/extensions/cloudflare-ai-gateway/index.js +27 -27
  175. package/dist/extensions/copilot-proxy/index.d.ts +11 -0
  176. package/dist/extensions/device-pair/index.d.ts +12 -0
  177. package/dist/extensions/diagnostics-otel/index.d.ts +11 -0
  178. package/dist/extensions/diffs/index.d.ts +11 -0
  179. package/dist/extensions/discord/index.d.ts +11 -0
  180. package/dist/extensions/discord/index.js +31 -31
  181. package/dist/extensions/discord/setup-entry.d.ts +7 -0
  182. package/dist/extensions/discord/setup-entry.js +29 -29
  183. package/dist/extensions/elevenlabs/index.d.ts +11 -0
  184. package/dist/extensions/elevenlabs/index.js +26 -26
  185. package/dist/extensions/feishu/index.d.ts +229 -0
  186. package/dist/extensions/feishu/index.js +31 -31
  187. package/dist/extensions/feishu/setup-entry.d.ts +9 -0
  188. package/dist/extensions/feishu/setup-entry.js +28 -28
  189. package/dist/extensions/firecrawl/index.d.ts +11 -0
  190. package/dist/extensions/firecrawl/index.js +26 -26
  191. package/dist/extensions/github-copilot/index.d.ts +11 -0
  192. package/dist/extensions/github-copilot/index.js +27 -27
  193. package/dist/extensions/google/index.d.ts +11 -0
  194. package/dist/extensions/google/index.js +26 -26
  195. package/dist/extensions/googlechat/index.d.ts +11 -0
  196. package/dist/extensions/googlechat/index.js +30 -30
  197. package/dist/extensions/googlechat/setup-entry.d.ts +19 -0
  198. package/dist/extensions/googlechat/setup-entry.js +30 -30
  199. package/dist/extensions/huggingface/index.d.ts +11 -0
  200. package/dist/extensions/huggingface/index.js +26 -26
  201. package/dist/extensions/imessage/index.d.ts +11 -0
  202. package/dist/extensions/imessage/index.js +30 -30
  203. package/dist/extensions/imessage/setup-entry.d.ts +7 -0
  204. package/dist/extensions/imessage/setup-entry.js +30 -30
  205. package/dist/extensions/irc/index.d.ts +11 -0
  206. package/dist/extensions/irc/index.js +29 -29
  207. package/dist/extensions/irc/setup-entry.d.ts +8 -0
  208. package/dist/extensions/irc/setup-entry.js +29 -29
  209. package/dist/extensions/kakao-talkchannel/index.d.ts +19 -0
  210. package/dist/extensions/kakao-talkchannel/index.js +1762 -0
  211. package/dist/extensions/kakao-talkchannel/moldclaw.plugin.json +111 -0
  212. package/dist/extensions/kakao-talkchannel/package.json +12 -0
  213. package/dist/extensions/kilocode/index.d.ts +11 -0
  214. package/dist/extensions/kilocode/index.js +26 -26
  215. package/dist/extensions/kimi-coding/index.d.ts +11 -0
  216. package/dist/extensions/kimi-coding/index.js +26 -26
  217. package/dist/extensions/line/index.d.ts +11 -0
  218. package/dist/extensions/line/index.js +28 -28
  219. package/dist/extensions/line/setup-entry.d.ts +7 -0
  220. package/dist/extensions/line/setup-entry.js +28 -28
  221. package/dist/extensions/llm-task/index.d.ts +11 -0
  222. package/dist/extensions/llm-task/index.js +28 -28
  223. package/dist/extensions/lobster/index.d.ts +11 -0
  224. package/dist/extensions/matrix/index.d.ts +11 -0
  225. package/dist/extensions/matrix/index.js +31 -31
  226. package/dist/extensions/matrix/setup-entry.d.ts +20 -0
  227. package/dist/extensions/matrix/setup-entry.js +31 -31
  228. package/dist/extensions/mattermost/index.d.ts +11 -0
  229. package/dist/extensions/mattermost/index.js +28 -28
  230. package/dist/extensions/mattermost/setup-entry.d.ts +88 -0
  231. package/dist/extensions/mattermost/setup-entry.js +28 -28
  232. package/dist/extensions/memory-core/index.d.ts +11 -0
  233. package/dist/extensions/memory-lancedb/index.d.ts +25 -0
  234. package/dist/extensions/microsoft/index.d.ts +11 -0
  235. package/dist/extensions/microsoft/index.js +26 -26
  236. package/dist/extensions/minimax/index.d.ts +11 -0
  237. package/dist/extensions/minimax/index.js +26 -26
  238. package/dist/extensions/mistral/index.d.ts +11 -0
  239. package/dist/extensions/mistral/index.js +26 -26
  240. package/dist/extensions/modelstudio/index.d.ts +11 -0
  241. package/dist/extensions/modelstudio/index.js +26 -26
  242. package/dist/extensions/moonshot/index.d.ts +11 -0
  243. package/dist/extensions/moonshot/index.js +26 -26
  244. package/dist/extensions/msteams/index.d.ts +11 -0
  245. package/dist/extensions/msteams/index.js +31 -31
  246. package/dist/extensions/msteams/setup-entry.d.ts +11 -0
  247. package/dist/extensions/msteams/setup-entry.js +31 -31
  248. package/dist/extensions/nextcloud-talk/index.d.ts +11 -0
  249. package/dist/extensions/nextcloud-talk/index.js +28 -28
  250. package/dist/extensions/nextcloud-talk/setup-entry.d.ts +60 -0
  251. package/dist/extensions/nextcloud-talk/setup-entry.js +28 -28
  252. package/dist/extensions/nostr/index.d.ts +11 -0
  253. package/dist/extensions/nostr/index.js +28 -28
  254. package/dist/extensions/nostr/setup-entry.d.ts +49 -0
  255. package/dist/extensions/nostr/setup-entry.js +28 -28
  256. package/dist/extensions/nvidia/index.d.ts +11 -0
  257. package/dist/extensions/ollama/index.d.ts +11 -0
  258. package/dist/extensions/open-prose/index.d.ts +11 -0
  259. package/dist/extensions/openai/index.d.ts +11 -0
  260. package/dist/extensions/openai/index.js +26 -26
  261. package/dist/extensions/opencode/index.d.ts +11 -0
  262. package/dist/extensions/opencode/index.js +26 -26
  263. package/dist/extensions/opencode-go/index.d.ts +11 -0
  264. package/dist/extensions/opencode-go/index.js +26 -26
  265. package/dist/extensions/openrouter/index.d.ts +11 -0
  266. package/dist/extensions/openrouter/index.js +26 -26
  267. package/dist/extensions/openshell/index.d.ts +11 -0
  268. package/dist/extensions/openshell/index.js +26 -26
  269. package/dist/extensions/perplexity/index.d.ts +11 -0
  270. package/dist/extensions/perplexity/index.js +2 -2
  271. package/dist/extensions/phone-control/index.d.ts +12 -0
  272. package/dist/extensions/qianfan/index.d.ts +11 -0
  273. package/dist/extensions/qianfan/index.js +26 -26
  274. package/dist/extensions/qwen-portal-auth/index.d.ts +12 -0
  275. package/dist/extensions/qwen-portal-auth/index.js +26 -26
  276. package/dist/extensions/sglang/index.d.ts +11 -0
  277. package/dist/extensions/sglang/index.js +26 -26
  278. package/dist/extensions/signal/index.d.ts +11 -0
  279. package/dist/extensions/signal/index.js +29 -29
  280. package/dist/extensions/signal/setup-entry.d.ts +7 -0
  281. package/dist/extensions/signal/setup-entry.js +29 -29
  282. package/dist/extensions/slack/index.d.ts +11 -0
  283. package/dist/extensions/slack/index.js +30 -30
  284. package/dist/extensions/slack/setup-entry.d.ts +7 -0
  285. package/dist/extensions/slack/setup-entry.js +29 -29
  286. package/dist/extensions/synology-chat/index.d.ts +11 -0
  287. package/dist/extensions/synology-chat/index.js +28 -28
  288. package/dist/extensions/synology-chat/setup-entry.d.ts +138 -0
  289. package/dist/extensions/synology-chat/setup-entry.js +28 -28
  290. package/dist/extensions/synthetic/index.d.ts +11 -0
  291. package/dist/extensions/synthetic/index.js +26 -26
  292. package/dist/extensions/talk-voice/index.d.ts +12 -0
  293. package/dist/extensions/talk-voice/index.js +26 -26
  294. package/dist/extensions/telegram/index.d.ts +11 -0
  295. package/dist/extensions/telegram/index.js +29 -29
  296. package/dist/extensions/telegram/setup-entry.d.ts +7 -0
  297. package/dist/extensions/telegram/setup-entry.js +28 -28
  298. package/dist/extensions/thread-ownership/index.d.ts +12 -0
  299. package/dist/extensions/tlon/index.d.ts +11 -0
  300. package/dist/extensions/tlon/index.js +28 -28
  301. package/dist/extensions/tlon/setup-entry.d.ts +7 -0
  302. package/dist/extensions/tlon/setup-entry.js +28 -28
  303. package/dist/extensions/together/index.d.ts +11 -0
  304. package/dist/extensions/together/index.js +26 -26
  305. package/dist/extensions/twitch/index.d.ts +39 -0
  306. package/dist/extensions/twitch/index.js +28 -28
  307. package/dist/extensions/venice/index.d.ts +11 -0
  308. package/dist/extensions/venice/index.js +26 -26
  309. package/dist/extensions/vercel-ai-gateway/index.d.ts +11 -0
  310. package/dist/extensions/vercel-ai-gateway/index.js +26 -26
  311. package/dist/extensions/vllm/index.d.ts +11 -0
  312. package/dist/extensions/vllm/index.js +26 -26
  313. package/dist/extensions/voice-call/index.d.ts +11 -0
  314. package/dist/extensions/voice-call/index.js +26 -26
  315. package/dist/extensions/volcengine/index.d.ts +11 -0
  316. package/dist/extensions/volcengine/index.js +26 -26
  317. package/dist/extensions/whatsapp/index.d.ts +11 -0
  318. package/dist/extensions/whatsapp/index.js +29 -29
  319. package/dist/extensions/whatsapp/setup-entry.d.ts +7 -0
  320. package/dist/extensions/whatsapp/setup-entry.js +29 -29
  321. package/dist/extensions/xai/index.d.ts +11 -0
  322. package/dist/extensions/xai/index.js +26 -26
  323. package/dist/extensions/xiaomi/index.d.ts +11 -0
  324. package/dist/extensions/xiaomi/index.js +26 -26
  325. package/dist/extensions/zai/index.d.ts +11 -0
  326. package/dist/extensions/zai/index.js +26 -26
  327. package/dist/extensions/zalo/index.d.ts +11 -0
  328. package/dist/extensions/zalo/index.js +30 -30
  329. package/dist/extensions/zalo/setup-entry.d.ts +34 -0
  330. package/dist/extensions/zalo/setup-entry.js +30 -30
  331. package/dist/extensions/zalouser/index.d.ts +11 -0
  332. package/dist/extensions/zalouser/index.js +31 -31
  333. package/dist/extensions/zalouser/setup-entry.d.ts +42 -0
  334. package/dist/extensions/zalouser/setup-entry.js +31 -31
  335. package/dist/feishu-DCKEC3ao.d.ts +36 -0
  336. package/dist/gateway-cli-DN1Ii6J-.js +26432 -0
  337. package/dist/gateway-install-token-CJYFJBaC.js +163 -0
  338. package/dist/gateway-rpc-CroQg9MB.js +26 -0
  339. package/dist/gateway-runtime-D9FRZqKP.js +69 -0
  340. package/dist/googlechat-CBCkerAy.js +307 -0
  341. package/dist/googlechat-CSUNieHX.d.ts +12 -0
  342. package/dist/group-access-rSvkIglb.d.ts +61 -0
  343. package/dist/health-B6WwLJp4.js +570 -0
  344. package/dist/health-CAlJydXv.js +108 -0
  345. package/dist/history-BwNxb0sJ.d.ts +75 -0
  346. package/dist/hooks-BYlfU3Nf.d.ts +6 -0
  347. package/dist/hooks-cli-DuKmdo_H.js +995 -0
  348. package/dist/http-registry-DX_LVtuK.d.ts +20 -0
  349. package/dist/image-generation-DKkdRpve.d.ts +9 -0
  350. package/dist/imessage-7abjbe2Q.js +31 -0
  351. package/dist/imessage-DOH1yaDE.js +110 -0
  352. package/dist/inbound-envelope-CmvweL6U.d.ts +78 -0
  353. package/dist/inbound-reply-dispatch-BvnKTOec.js +71 -0
  354. package/dist/inbound-reply-dispatch-C7LjHRZN.d.ts +72 -0
  355. package/dist/index-DTQqfqj9.d.ts +1 -0
  356. package/dist/index.d.ts +27 -0
  357. package/dist/index.js +2 -2
  358. package/dist/infra/warning-filter.d.ts +10 -0
  359. package/dist/install-target-tXRD7VkM.js +574 -0
  360. package/dist/installs-C8fz8sm3.js +532 -0
  361. package/dist/io-C6XifaT4.js +9737 -0
  362. package/dist/io-C8awRnSW.js +28 -0
  363. package/dist/ipv4-d88_Jn2p.js +82 -0
  364. package/dist/irc-DpR6FXjN.js +672 -0
  365. package/dist/json-store-Sr_kk-II.d.ts +14 -0
  366. package/dist/keyed-async-queue-BA3BKukE.d.ts +19 -0
  367. package/dist/library-DOwowAGN.js +107 -0
  368. package/dist/lifecycle-core-BHHBoRTY.js +382 -0
  369. package/dist/line/accounts.d.ts +3 -0
  370. package/dist/line/send.d.ts +2 -0
  371. package/dist/line/send.js +4 -4
  372. package/dist/line/template-messages.d.ts +2 -0
  373. package/dist/line-8rsNbJCP.js +530 -0
  374. package/dist/line-D_cvIf6B.d.ts +75 -0
  375. package/dist/links-BOnvOj1z.d.ts +7 -0
  376. package/dist/llm-slug-generator-D9HjWtJT.js +67 -0
  377. package/dist/llm-slug-generator.d.ts +12 -0
  378. package/dist/llm-slug-generator.js +27 -27
  379. package/dist/logging-BhqLWxTD.js +13 -0
  380. package/dist/logging-DfaiL4OX.js +29 -0
  381. package/dist/login-qr-COBYR52w.js +233 -0
  382. package/dist/login-qr-xK4QIpPc.js +107 -0
  383. package/dist/logs-cli-RSSTw8L_.js +254 -0
  384. package/dist/manager-runtime-DL6JoSj9.js +106 -0
  385. package/dist/manager.runtime-Cbyhg1vB.js +710 -0
  386. package/dist/markdown-to-line-BTlEkOls.d.ts +91 -0
  387. package/dist/matrix-DX-jaB88.js +1490 -0
  388. package/dist/matrix-H6Yyj1QZ.d.ts +68 -0
  389. package/dist/matrix-J8s45tRw.js +1269 -0
  390. package/dist/mattermost-D75n6bRI.d.ts +6 -0
  391. package/dist/mcp-cli-CLc3_yCO.js +86 -0
  392. package/dist/media-understanding.runtime-BI0Lljbl.js +111 -0
  393. package/dist/memory-cli-CTp2cYrf.js +106 -0
  394. package/dist/method-scopes-Du8ODGFW.js +2586 -0
  395. package/dist/model-auth-markers-DEDakSUW.d.ts +20 -0
  396. package/dist/model-picker-CDBs7LJF.js +390 -0
  397. package/dist/model-picker-CRix4Wwv.js +107 -0
  398. package/dist/model-picker.runtime-CITyy3Rn.js +120 -0
  399. package/dist/model-suppression.runtime-Ce7D6QUT.js +111 -0
  400. package/dist/models-BK1eanuP.js +113 -0
  401. package/dist/models-X4Czy3uE.js +2514 -0
  402. package/dist/models-cli-C79Ulviy.js +304 -0
  403. package/dist/models-config-DALlu3S9.js +106 -0
  404. package/dist/models-config.providers.discovery-CSJ1STM1.d.ts +18 -0
  405. package/dist/monitor-B45a_RpX.js +3468 -0
  406. package/dist/monitor-C8KbJ-i0.js +767 -0
  407. package/dist/monitor-CIhrvegZ.js +3076 -0
  408. package/dist/monitor-CQut7klP.js +6823 -0
  409. package/dist/monitor-DZb5IJle.js +777 -0
  410. package/dist/monitor-DaFkdD27.js +108 -0
  411. package/dist/monitor-Do9Tp2Ii.js +110 -0
  412. package/dist/monitor-shared-CMK9cDOb.js +444 -0
  413. package/dist/msteams-A6H_wv5F.js +852 -0
  414. package/dist/net-DpMJgN-o.d.ts +19 -0
  415. package/dist/nextcloud-talk-f1pZ5Bge.d.ts +1 -0
  416. package/dist/node-cli-BXnmsjzL.js +2498 -0
  417. package/dist/node-resolve-CupmrA0Y.js +835 -0
  418. package/dist/nodes-cli-DZVrah_8.js +1375 -0
  419. package/dist/nostr-DMV534Ks.d.ts +7 -0
  420. package/dist/nostr-SAk3tjtR.js +8744 -0
  421. package/dist/npm-resolution-Dr9wssCY.js +60 -0
  422. package/dist/oauth-utils-DnyXdWU9.d.ts +10 -0
  423. package/dist/onboard-BE5pmb1g.js +589 -0
  424. package/dist/onboard-channels-3hNVY0E7.js +1241 -0
  425. package/dist/onboard-channels-vaO3nWLL.js +200 -0
  426. package/dist/onboard-custom-CI5uFyWH.js +571 -0
  427. package/dist/onboard-custom-eIvRswgv.js +109 -0
  428. package/dist/onboard-helpers-ChMWfUnl.js +335 -0
  429. package/dist/onboard-helpers-DRFi9oaD.js +108 -0
  430. package/dist/onboard-remote-BTspTgA4.js +112 -0
  431. package/dist/onboard-remote-so38yXlX.js +181 -0
  432. package/dist/onboard-search-DS0tZS24.js +297 -0
  433. package/dist/onboard-skills-B9DxCCiU.js +133 -0
  434. package/dist/onboard-skills-so0a_BJV.js +112 -0
  435. package/dist/outbound-media-BiJscGlR.js +11 -0
  436. package/dist/outbound-media-DJF-TuJu.d.ts +11 -0
  437. package/dist/pairing-access-CuiJP9xN.d.ts +21 -0
  438. package/dist/pairing-cli-DN0u1Cez.js +212 -0
  439. package/dist/parse-finite-number-B3FJTjyQ.d.ts +5 -0
  440. package/dist/perplexity-Bw1u3CAF.js +24 -0
  441. package/dist/persistent-dedupe-DR5Ka6BX.d.ts +26 -0
  442. package/dist/pi-model-discovery-runtime-iwKNCaYu.js +106 -0
  443. package/dist/pi-tools.before-tool-call.runtime-BM_N-JZe.js +380 -0
  444. package/dist/plugin-install--KVul05Z.js +184 -0
  445. package/dist/plugin-install-DVpPsLkS.js +112 -0
  446. package/dist/plugin-install-plan-Dwc6-coz.js +49 -0
  447. package/dist/plugin-registry-XRswugE9.js +108 -0
  448. package/dist/plugin-registry-jozQafRo.js +49 -0
  449. package/dist/plugin-sdk/account-resolution.js +26 -26
  450. package/dist/plugin-sdk/acp-runtime.js +26 -26
  451. package/dist/plugin-sdk/acpx.js +1 -1
  452. package/dist/plugin-sdk/agent-runtime.js +26 -26
  453. package/dist/plugin-sdk/bluebubbles.js +29 -29
  454. package/dist/plugin-sdk/channel-config-helpers.js +26 -26
  455. package/dist/plugin-sdk/channel-config-schema.js +2 -2
  456. package/dist/plugin-sdk/channel-policy.js +26 -26
  457. package/dist/plugin-sdk/channel-runtime.js +26 -26
  458. package/dist/plugin-sdk/compat.js +27 -27
  459. package/dist/plugin-sdk/config-runtime.js +28 -28
  460. package/dist/plugin-sdk/conversation-runtime.js +26 -26
  461. package/dist/plugin-sdk/discord.js +26 -26
  462. package/dist/plugin-sdk/feishu.js +27 -27
  463. package/dist/plugin-sdk/gateway-runtime.js +8 -8
  464. package/dist/plugin-sdk/googlechat.js +29 -29
  465. package/dist/plugin-sdk/image-generation-runtime.js +26 -26
  466. package/dist/plugin-sdk/image-generation.js +26 -26
  467. package/dist/plugin-sdk/imessage.js +27 -27
  468. package/dist/plugin-sdk/index.js +26 -26
  469. package/dist/plugin-sdk/infra-runtime.js +26 -26
  470. package/dist/plugin-sdk/irc.js +29 -29
  471. package/dist/plugin-sdk/line.js +27 -27
  472. package/dist/plugin-sdk/llm-task.js +26 -26
  473. package/dist/plugin-sdk/matrix.js +29 -29
  474. package/dist/plugin-sdk/mattermost.js +28 -28
  475. package/dist/plugin-sdk/media-runtime.js +26 -26
  476. package/dist/plugin-sdk/media-understanding-runtime.js +26 -26
  477. package/dist/plugin-sdk/media-understanding.js +26 -26
  478. package/dist/plugin-sdk/msteams.js +30 -30
  479. package/dist/plugin-sdk/nextcloud-talk.js +28 -28
  480. package/dist/plugin-sdk/nostr.js +27 -27
  481. package/dist/plugin-sdk/plugin-runtime.js +26 -26
  482. package/dist/plugin-sdk/provider-auth.js +28 -28
  483. package/dist/plugin-sdk/provider-setup.js +27 -27
  484. package/dist/plugin-sdk/provider-web-search.js +1 -1
  485. package/dist/plugin-sdk/qwen-portal-auth.js +26 -26
  486. package/dist/plugin-sdk/reply-history.js +26 -26
  487. package/dist/plugin-sdk/reply-runtime.js +26 -26
  488. package/dist/plugin-sdk/sandbox.js +26 -26
  489. package/dist/plugin-sdk/security-runtime.js +26 -26
  490. package/dist/plugin-sdk/self-hosted-provider-setup.js +27 -27
  491. package/dist/plugin-sdk/setup.js +27 -27
  492. package/dist/plugin-sdk/signal.js +26 -26
  493. package/dist/plugin-sdk/slack.js +26 -26
  494. package/dist/plugin-sdk/speech-runtime.js +26 -26
  495. package/dist/plugin-sdk/speech.js +26 -26
  496. package/dist/plugin-sdk/src/channels/plugins/setup-wizard-helpers.d.ts +3 -0
  497. package/dist/plugin-sdk/src/config/config-lock.d.ts +38 -0
  498. package/dist/plugin-sdk/src/config/config.d.ts +1 -1
  499. package/dist/plugin-sdk/src/config/io.d.ts +39 -0
  500. package/dist/plugin-sdk/src/config/types.gateway.d.ts +12 -0
  501. package/dist/plugin-sdk/src/config/types.secrets.d.ts +10 -0
  502. package/dist/plugin-sdk/src/config/zod-schema.d.ts +2 -0
  503. package/dist/plugin-sdk/src/gateway/credential-planner.d.ts +3 -1
  504. package/dist/plugin-sdk/src/secrets/provider-env-vars.d.ts +61 -0
  505. package/dist/plugin-sdk/src/secrets/sec1-placeholder.d.ts +181 -0
  506. package/dist/plugin-sdk/src/secrets/sec1-utils.d.ts +57 -0
  507. package/dist/plugin-sdk/synology-chat.js +27 -27
  508. package/dist/plugin-sdk/telegram.js +26 -26
  509. package/dist/plugin-sdk/text-runtime.js +4 -4
  510. package/dist/plugin-sdk/tlon.js +27 -27
  511. package/dist/plugin-sdk/twitch.js +26 -26
  512. package/dist/plugin-sdk/voice-call.js +26 -26
  513. package/dist/plugin-sdk/whatsapp.js +26 -26
  514. package/dist/plugin-sdk/zalo.js +30 -30
  515. package/dist/plugin-sdk/zalouser.js +29 -29
  516. package/dist/plugins/runtime/index.d.ts +22 -0
  517. package/dist/plugins/runtime/index.js +26 -26
  518. package/dist/plugins-C4PiDdjc.js +106 -0
  519. package/dist/plugins-cli-zhmliYNU.js +912 -0
  520. package/dist/policy-CcSolumc.js +143 -0
  521. package/dist/preflight-audio.runtime-BAbfqqzW.js +111 -0
  522. package/dist/probe-Bgt5c-cr.js +129 -0
  523. package/dist/probe-CPk5iGcg.js +47 -0
  524. package/dist/probe-DR4KRKXz.js +19 -0
  525. package/dist/probe-DnoCyJ_m.js +1793 -0
  526. package/dist/probe-VsLtK3vQ.js +6328 -0
  527. package/dist/probe-auth-BnsKrQt7.js +38 -0
  528. package/dist/probe-auth-DYdUG8l1.js +48 -0
  529. package/dist/program-8enYYBsc.js +247 -0
  530. package/dist/prompt-select-styled-DxBcUasv.js +2673 -0
  531. package/dist/provider-api-key-auth.runtime-DsLZyt6h.js +116 -0
  532. package/dist/provider-auth-choice-30EvRxqc.js +126 -0
  533. package/dist/provider-auth-choice-preference-DMr1WmRg.js +189 -0
  534. package/dist/provider-auth-choice.runtime-CI98BgQF.js +118 -0
  535. package/dist/provider-auth-guidance-WKDIi_wk.js +34 -0
  536. package/dist/provider-auth-result-Cs8wguSI.d.ts +18 -0
  537. package/dist/provider-models-EOys_Nvi.d.ts +867 -0
  538. package/dist/provider-ollama-setup-D89zlm9C.d.ts +32 -0
  539. package/dist/provider-onboard-BzOpgCLu.d.ts +40 -0
  540. package/dist/provider-runtime.runtime-Cm4as2KG.js +106 -0
  541. package/dist/provider-self-hosted-setup-Bmv_AQmw.d.ts +61 -0
  542. package/dist/provider-self-hosted-setup-CJwFVVB4.js +182 -0
  543. package/dist/provider-usage-CVNyLLDb.js +106 -0
  544. package/dist/provider-usage.types-CdTymHNu.d.ts +16 -0
  545. package/dist/provider-web-search-BJhXD5dH.js +2392 -0
  546. package/dist/provider-wizard-DMMYXjlW.js +152 -0
  547. package/dist/push-apns-BnWTdTEk.js +1038 -0
  548. package/dist/pw-ai-CtK_7Cy2.js +1866 -0
  549. package/dist/qr-cli-CA-BF0--.js +108 -0
  550. package/dist/qr-cli-D18HiUkh.js +369 -0
  551. package/dist/reactions-Df7XG8Uh.js +281 -0
  552. package/dist/read-only-account-inspect.discord.runtime-B-FP0mwb.js +111 -0
  553. package/dist/read-only-account-inspect.slack.runtime-DkWZ2ccW.js +111 -0
  554. package/dist/read-only-account-inspect.telegram.runtime-BnlTkn_e.js +111 -0
  555. package/dist/redact-snapshot-DVdstBvO.js +2661 -0
  556. package/dist/ref-contract-RPkB754Q.js +53 -0
  557. package/dist/register.agent-DVAxXQKW.js +434 -0
  558. package/dist/register.backup-CUuL5KUZ.js +624 -0
  559. package/dist/register.configure-bC0UEwfU.js +247 -0
  560. package/dist/register.maintenance-iIqvl_eT.js +569 -0
  561. package/dist/register.message-CEDd4z07.js +704 -0
  562. package/dist/register.onboard-Cejfnysy.js +187 -0
  563. package/dist/register.setup-DU7uHdYt.js +207 -0
  564. package/dist/register.status-health-sessions-BWphMXNR.js +493 -0
  565. package/dist/register.subclis-DnIweTEG.js +315 -0
  566. package/dist/register.subclis-gJX_Pbub.js +12 -0
  567. package/dist/registry-Dgwc-7eS.js +1183 -0
  568. package/dist/replies-D9PEZ8yn.js +110 -0
  569. package/dist/reply-history-lHgoC4l3.d.ts +1 -0
  570. package/dist/reply-payload-Bd2HuR4g.d.ts +46 -0
  571. package/dist/request-url-BcSJaiiu.d.ts +5 -0
  572. package/dist/resolve-BbsCHGLY.js +660 -0
  573. package/dist/resolve-channels-BtrGC95o.js +262 -0
  574. package/dist/resolve-channels-C1SthO1N.js +226 -0
  575. package/dist/resolve-users-CgSxHrU0.js +143 -0
  576. package/dist/routes-BZtqNrBf.js +7097 -0
  577. package/dist/rpc-D3KMxG4J.js +67 -0
  578. package/dist/run-command-C8b3dCZV.d.ts +16 -0
  579. package/dist/run-main-BlWJVotF.js +423 -0
  580. package/dist/runtime-RWGbO5Qy.d.ts +26 -0
  581. package/dist/runtime-discord-ops.runtime-DUXIYvQr.js +9073 -0
  582. package/dist/runtime-slack-ops.runtime-n1yFfyp1.js +4551 -0
  583. package/dist/runtime-telegram-ops.runtime-PZUWchjT.js +128 -0
  584. package/dist/runtime-whatsapp-login.runtime-xsuNyvGz.js +109 -0
  585. package/dist/runtime-whatsapp-outbound.runtime-5EfEyCsO.js +112 -0
  586. package/dist/sandbox-cli-Dw1nWNmQ.js +530 -0
  587. package/dist/search-manager-BJoRxOaf.js +15 -0
  588. package/dist/search-manager-DxkQvUrW.js +386 -0
  589. package/dist/secret-input-schema-Cp_La9qv.d.ts +19 -0
  590. package/dist/secrets-cli-BPyV2gSq.js +2065 -0
  591. package/dist/security-cli-EK4sSRfG.js +570 -0
  592. package/dist/send-B01Gvh9m.js +629 -0
  593. package/dist/send-B4L4wRJO.js +100 -0
  594. package/dist/send-BDcGrXt0.js +1025 -0
  595. package/dist/send-BRRtHxyR.js +283 -0
  596. package/dist/send-DU6dmMXW.js +631 -0
  597. package/dist/server-CWw5GFEg.js +106 -0
  598. package/dist/server-node-events-92cDVswC.js +501 -0
  599. package/dist/session-key-DbkfhOjM.d.ts +46 -0
  600. package/dist/sessions-B052uHA3.js +218 -0
  601. package/dist/sessions-Cef4dZNP.js +107 -0
  602. package/dist/setup-BlQPyDPy.js +387 -0
  603. package/dist/setup-DcSZ_pTn.d.ts +37 -0
  604. package/dist/setup-core-B9mdZYnU.js +166 -0
  605. package/dist/setup-core-Cj0sLkpP.js +47 -0
  606. package/dist/setup-core-CkZbebOv.js +143 -0
  607. package/dist/setup-core-MRNjnrJl.js +205 -0
  608. package/dist/setup-surface-3ZY0JtWE.js +490 -0
  609. package/dist/setup-wizard-helpers-Dwzb9Dcz.d.ts +203 -0
  610. package/dist/setup.finalize-B5ETm3Ui.js +517 -0
  611. package/dist/setup.gateway-config-C8hdtlbw.js +338 -0
  612. package/dist/setup.secret-input-BZSIeiqy.js +25 -0
  613. package/dist/shared--9_eQ_lc.js +75 -0
  614. package/dist/shared-CxkH3H0U.js +102 -0
  615. package/dist/shared-DTNL0hA9.js +298 -0
  616. package/dist/shared-HSP1OV-Q.js +96 -0
  617. package/dist/shared-UIjWb_3B.js +182 -0
  618. package/dist/signal-CTI6bSmB.js +109 -0
  619. package/dist/skills-4-r1mfJM.js +853 -0
  620. package/dist/skills-RNm54CBO.js +19 -0
  621. package/dist/skills-cli-te7dSs5p.js +291 -0
  622. package/dist/skills-install-Del-Ogv8.js +763 -0
  623. package/dist/skills-status-BZpoMXrR.js +169 -0
  624. package/dist/skills-status-Dq61Sz8U.js +20 -0
  625. package/dist/slack-oc-viUtl.js +109 -0
  626. package/dist/slash-commands.runtime-NdkD2LZV.js +123 -0
  627. package/dist/slash-dispatch.runtime-DQgeaF3J.js +136 -0
  628. package/dist/slash-skill-commands.runtime-DmOl2DnL.js +111 -0
  629. package/dist/src-0wtt7seR.js +1696 -0
  630. package/dist/status-5oR_gqv_.js +121 -0
  631. package/dist/status-BO8LY0hC.js +1599 -0
  632. package/dist/status-D_oHA9yO.js +126 -0
  633. package/dist/status-IrMacJRj.js +606 -0
  634. package/dist/status-Prdeg53E.js +43 -0
  635. package/dist/status-json-Da0hR-1Z.js +286 -0
  636. package/dist/status.link-channel-BgUJEZAz.js +138 -0
  637. package/dist/status.scan.deps.runtime-D9vHTxOW.js +121 -0
  638. package/dist/status.scan.runtime-D-EdD5CW.js +114 -0
  639. package/dist/status.summary--i6xduWH.js +592 -0
  640. package/dist/status.summary.runtime-BqMXjaBc.js +113 -0
  641. package/dist/subagent-orphan-recovery-DiRJcFQc.js +302 -0
  642. package/dist/subagent-registry-runtime-B66EYEYm.js +106 -0
  643. package/dist/synology-chat-BemXqdzG.js +297 -0
  644. package/dist/system-cli-CSuiia4-.js +92 -0
  645. package/dist/telegram/audit.d.ts +2 -0
  646. package/dist/telegram/audit.js +1 -1
  647. package/dist/telegram/token.d.ts +2 -0
  648. package/dist/telegram/token.js +26 -26
  649. package/dist/telegram-DLFcRv5a.js +109 -0
  650. package/dist/testing-DZrulv-n.d.ts +1755 -0
  651. package/dist/text-chunking-BaYBIUoR.d.ts +79 -0
  652. package/dist/text-chunking-C8kmbNfa.js +84 -0
  653. package/dist/thinking-D8aqmr3o.d.ts +13 -0
  654. package/dist/tlon-Bpr4f3yF.js +433 -0
  655. package/dist/tool-send-BHKm5ztm.d.ts +9 -0
  656. package/dist/tui-BY3QRgC1.js +3834 -0
  657. package/dist/tui-cli-CCfZOlV0.js +132 -0
  658. package/dist/types-CKx5nDZB.d.ts +45 -0
  659. package/dist/types-DBhDdMQd.d.ts +22670 -0
  660. package/dist/types.base-B_TkkSS8.d.ts +188 -0
  661. package/dist/types.secrets-Bojc4omL.js +92 -0
  662. package/dist/ui-1UpZZyI3.js +31 -0
  663. package/dist/update-BR4JvFpV.js +1036 -0
  664. package/dist/update-cli-BZv44lFq.js +1498 -0
  665. package/dist/update-offset-store-DGdBotIW.js +107 -0
  666. package/dist/update-runner-D34sooPe.js +1496 -0
  667. package/dist/vllm-defaults-BCGSJ7K0.d.ts +13 -0
  668. package/dist/wait-BU9vJv22.d.ts +4 -0
  669. package/dist/web-CXpU2D41.js +107 -0
  670. package/dist/web-shared-B4sL45ah.d.ts +45 -0
  671. package/dist/webhook-memory-guards-B7oLVseG.d.ts +43 -0
  672. package/dist/webhook-request-guards-CqIH7equ.d.ts +76 -0
  673. package/dist/webhook-targets-CAAGATtk.js +181 -0
  674. package/dist/webhook-targets-oQ0jd4r0.d.ts +106 -0
  675. package/dist/webhooks-cli-B46t2VT5.js +349 -0
  676. package/dist/whatsapp-Dniwd4Rv.js +109 -0
  677. package/dist/whatsapp-actions-fL46PsNs.js +162 -0
  678. package/dist/windows-spawn-DGeE98SH.d.ts +43 -0
  679. package/dist/workspace-dirs-d3Ms_ryk.js +2002 -0
  680. package/dist/zalo-Csulx0XK.d.ts +9 -0
  681. package/dist/zalo-gh0yAWmS.js +415 -0
  682. package/dist/zalouser-CuxRvztM.js +30911 -0
  683. package/dist/zod-schema.agent-runtime-B4MkB-_3.d.ts +10 -0
  684. package/dist/zod-schema.core-D5reNip6.js +541 -0
  685. package/dist/zod-schema.core-DN3RhEUG.d.ts +173 -0
  686. package/docs/SEC1.md +523 -0
  687. package/docs/SEC1_IMPLEMENTATION/CHANNELS_REPORT.md +173 -0
  688. package/docs/SEC1_IMPLEMENTATION/CORE_UTIL_REPORT.md +139 -0
  689. package/docs/SEC1_IMPLEMENTATION/DOCS_REPORT.md +134 -0
  690. package/docs/SEC1_IMPLEMENTATION/ENV_MAP_DRAFT.md +148 -0
  691. package/docs/SEC1_IMPLEMENTATION/INTEGRATION_REPORT.md +170 -0
  692. package/docs/SEC1_IMPLEMENTATION/PROVIDERS_REPORT.md +291 -0
  693. package/docs/SEC1_IMPLEMENTATION/QA_REPORT.md +249 -0
  694. package/docs/SEC1_IMPLEMENTATION/RECURSIVE_QA/wave1-channels.md +317 -0
  695. package/docs/SEC1_IMPLEMENTATION/RECURSIVE_QA/wave1-docs.md +212 -0
  696. package/docs/SEC1_IMPLEMENTATION/RECURSIVE_QA/wave1-security.md +368 -0
  697. package/docs/SEC1_IMPLEMENTATION/RECURSIVE_QA/wave2-critic-consolidated.md +195 -0
  698. package/docs/SEC1_IMPLEMENTATION/RECURSIVE_QA/wave3-fix-report.md +105 -0
  699. package/docs/SEC1_IMPLEMENTATION/STRATEGY.md +451 -0
  700. package/docs/SEC1_IMPLEMENTATION/TEST_REPORT.md +156 -0
  701. package/docs/pipeline-sdk/CLI_SPEC.md +609 -0
  702. package/docs/pipeline-sdk/PIPELINE_SDK_DESIGN.md +1372 -0
  703. package/extensions/kakao-talkchannel/MIGRATION_ARCH_ANALYSIS.md +455 -0
  704. package/extensions/kakao-talkchannel/MIGRATION_CODE_ANALYSIS.md +383 -0
  705. package/extensions/kakao-talkchannel/MIGRATION_STRATEGY.md +115 -0
  706. package/extensions/kakao-talkchannel/README.md +50 -0
  707. package/extensions/kakao-talkchannel/index.ts +20 -0
  708. package/extensions/kakao-talkchannel/moldclaw.plugin.json +98 -0
  709. package/extensions/kakao-talkchannel/package.json +12 -0
  710. package/extensions/kakao-talkchannel/src/adapters/config.ts +132 -0
  711. package/extensions/kakao-talkchannel/src/adapters/gateway.ts +974 -0
  712. package/extensions/kakao-talkchannel/src/adapters/outbound.ts +52 -0
  713. package/extensions/kakao-talkchannel/src/adapters/pairing.ts +35 -0
  714. package/extensions/kakao-talkchannel/src/adapters/security.ts +57 -0
  715. package/extensions/kakao-talkchannel/src/adapters/setup.ts +105 -0
  716. package/extensions/kakao-talkchannel/src/adapters/status.ts +117 -0
  717. package/extensions/kakao-talkchannel/src/channel.ts +58 -0
  718. package/extensions/kakao-talkchannel/src/commands/card.ts +413 -0
  719. package/extensions/kakao-talkchannel/src/config/schema.ts +129 -0
  720. package/extensions/kakao-talkchannel/src/kakao/callback.ts +133 -0
  721. package/extensions/kakao-talkchannel/src/kakao/limits.ts +129 -0
  722. package/extensions/kakao-talkchannel/src/kakao/payload.ts +138 -0
  723. package/extensions/kakao-talkchannel/src/kakao/response.ts +373 -0
  724. package/extensions/kakao-talkchannel/src/relay/client.ts +146 -0
  725. package/extensions/kakao-talkchannel/src/relay/session.ts +137 -0
  726. package/extensions/kakao-talkchannel/src/relay/sse.ts +258 -0
  727. package/extensions/kakao-talkchannel/src/relay/stream.ts +149 -0
  728. package/extensions/kakao-talkchannel/src/runtime.ts +21 -0
  729. package/extensions/kakao-talkchannel/src/types.ts +447 -0
  730. package/extensions/kakao-talkchannel/src/version.ts +3 -0
  731. package/extensions/kakao-talkchannel/tsconfig.json +19 -0
  732. package/package.json +23 -8
  733. package/skills/meshy/SKILL.md +69 -0
  734. package/skills/meshy/scripts/__pycache__/check_status.cpython-312.pyc +0 -0
  735. package/skills/meshy/scripts/__pycache__/image_to_3d.cpython-312.pyc +0 -0
  736. package/skills/meshy/scripts/__pycache__/text_to_3d.cpython-312.pyc +0 -0
  737. package/skills/meshy/scripts/check_status.py +147 -0
  738. package/skills/meshy/scripts/image_to_3d.py +229 -0
  739. package/skills/meshy/scripts/text_to_3d.py +214 -0
  740. package/skills/nano-banana-pro/scripts/generate_image.py +1 -1
  741. package/skills/openai-whisper-api/scripts/transcribe.sh +0 -0
  742. package/skills/tavily-search/SKILL.md +61 -0
  743. package/skills/tavily-search/scripts/__pycache__/search.cpython-312.pyc +0 -0
  744. package/skills/tavily-search/scripts/search.py +238 -0
  745. package/skills/video-frames/scripts/frame.sh +0 -0
  746. package/LICENSE +0 -21
  747. package/dist/accounts-UcSvD34O.js +0 -109
  748. package/dist/acp-cli-BPb8PgHP.js +0 -2088
  749. package/dist/actions.runtime-BL5QRooG.js +0 -114
  750. package/dist/actions.runtime-DSdfSo40.js +0 -128
  751. package/dist/agents-CHeX_5-H.js +0 -217
  752. package/dist/agents-DQRL9XKP.js +0 -853
  753. package/dist/allow-list-Boi79v-U.js +0 -81
  754. package/dist/allowlist-B2eBBeMF.js +0 -142
  755. package/dist/api-CFAtRSYL.js +0 -6953
  756. package/dist/api-D5JNJj8n.js +0 -112
  757. package/dist/audit-BM0GsdzV.js +0 -787
  758. package/dist/audit-BqRK9OSj.js +0 -54
  759. package/dist/audit-channel.collect.runtime-BPvDB8aq.js +0 -600
  760. package/dist/audit-channel.runtime-D3fzHiAo.js +0 -116
  761. package/dist/audit-extra.async-NveNIzX0.js +0 -813
  762. package/dist/audit-membership-runtime-mu470WFO.js +0 -157
  763. package/dist/audit.deep.runtime-RdxvW8Tj.js +0 -24
  764. package/dist/audit.nondeep.runtime-DDu8vA9Z.js +0 -831
  765. package/dist/audit.runtime-Y8C9W7s9.js +0 -113
  766. package/dist/auth-choice-C1CIxRsi.js +0 -268
  767. package/dist/auth-choice-CTvqWiDI.js +0 -117
  768. package/dist/auth-choice-Ddzko1B8.js +0 -502
  769. package/dist/auth-choice-options-BIAmAiCe.js +0 -123
  770. package/dist/auth-choice-prompt-B815kArz.js +0 -110
  771. package/dist/auth-choice-prompt-CGhTNCJx.js +0 -36
  772. package/dist/auth-choice.plugin-providers.runtime-AvAZ6S5W.js +0 -114
  773. package/dist/auth-profiles-BJcHzwPy.js +0 -127650
  774. package/dist/auth-profiles.runtime-CieFilK5.js +0 -111
  775. package/dist/bluebubbles-F8FGE9cH.js +0 -64
  776. package/dist/brave-BG5Yopn8.js +0 -24
  777. package/dist/browser-cli-Co7PJGZF.js +0 -1492
  778. package/dist/call-CoaQYq7c.js +0 -639
  779. package/dist/call-D3eu5Jjh.js +0 -37
  780. package/dist/channel-BftWD6yu.js +0 -1321
  781. package/dist/channel-Bub9U5Xg.js +0 -214
  782. package/dist/channel-C0oDs7TO.js +0 -4681
  783. package/dist/channel-C8CnEdkZ.js +0 -352
  784. package/dist/channel-CI-RC-xf.js +0 -497
  785. package/dist/channel-CY-hZCOJ.js +0 -397
  786. package/dist/channel-CbtGJB2x.js +0 -943
  787. package/dist/channel-CcfK3wP8.js +0 -803
  788. package/dist/channel-DBoDIeVj.js +0 -619
  789. package/dist/channel-DEq6Ecs-.js +0 -920
  790. package/dist/channel-DH4dhW1n.js +0 -226
  791. package/dist/channel-DQ_wdKg_.js +0 -575
  792. package/dist/channel-DT6qD1Ic.js +0 -207
  793. package/dist/channel-DZNAyxwr.js +0 -542
  794. package/dist/channel-DtakwAEe.js +0 -538
  795. package/dist/channel-DuYgH6p1.js +0 -562
  796. package/dist/channel-Hn-AN-d52.js +0 -316
  797. package/dist/channel-_R4hbD5h.js +0 -1598
  798. package/dist/channel-account-context-DXq8dlvI.js +0 -103
  799. package/dist/channel-kQmEVn3I.js +0 -306
  800. package/dist/channel-options-DHfxaklg.js +0 -50
  801. package/dist/channel-summary-DUpnoYhI.js +0 -106
  802. package/dist/channel-t-JxCWk6.js +0 -949
  803. package/dist/channel.runtime--GYriaXU.js +0 -213
  804. package/dist/channel.runtime-BJtn3GOH.js +0 -174
  805. package/dist/channel.runtime-BV7t_oNz.js +0 -166
  806. package/dist/channel.runtime-Bi8a3n9S.js +0 -865
  807. package/dist/channel.runtime-BjsYF0NN.js +0 -122
  808. package/dist/channel.runtime-BnI6YtmI.js +0 -413
  809. package/dist/channel.runtime-CQOftcCd.js +0 -194
  810. package/dist/channel.runtime-CuIAcPjZ.js +0 -4006
  811. package/dist/channel.runtime-DH1Q1G4k.js +0 -399
  812. package/dist/channel.runtime-DYYUPKxr.js +0 -236
  813. package/dist/channel.runtime-U5Gszsr5.js +0 -177
  814. package/dist/channel.setup-BQFHmgki.js +0 -9
  815. package/dist/channel.setup-BVoDwklu.js +0 -8
  816. package/dist/channel.setup-Bf73HsXr.js +0 -57
  817. package/dist/channel.setup-CblD4flM.js +0 -11
  818. package/dist/channel.setup-DgxlrPgz.js +0 -6
  819. package/dist/channel.setup-GLIAEVKL.js +0 -8
  820. package/dist/channel.setup-YTy5R1sz.js +0 -9
  821. package/dist/channels-CTL8iR9J.js +0 -404
  822. package/dist/channels-DBGvnjHY.js +0 -1113
  823. package/dist/channels-cli-BmVO5-sq.js +0 -286
  824. package/dist/channels-status-issues-kDtsWzA-.js +0 -16
  825. package/dist/clawbot-cli-DtcMJHqX.js +0 -113
  826. package/dist/cli-BNGECGVY.js +0 -149
  827. package/dist/command-registry-1SDrWgER.js +0 -13
  828. package/dist/command-registry-DNorYU4w.js +0 -212
  829. package/dist/command-secret-gateway-DqDZparO.js +0 -106
  830. package/dist/compact.runtime-C1ZN8UGb.js +0 -111
  831. package/dist/completion-cli-Q_Jt5Foc.js +0 -16
  832. package/dist/completion-cli-QkTXhuJh.js +0 -445
  833. package/dist/config-BbxrRaLf.js +0 -938
  834. package/dist/config-CkD8DJ7L.js +0 -44
  835. package/dist/config-cli-BoPrlYTp.js +0 -428
  836. package/dist/config-guard-CEhCvr_u.js +0 -117
  837. package/dist/config-schema-GQ6uWjXe.js +0 -31
  838. package/dist/config-validation-woE2_LpC.js +0 -262
  839. package/dist/config-value-Dh8m-CFf.js +0 -132
  840. package/dist/config-y4i5g7s4.js +0 -30
  841. package/dist/configure-DGRzwdFN.js +0 -1100
  842. package/dist/configure-S4AHE3k_.js +0 -238
  843. package/dist/control-ui-shared-kLBp4YlS.js +0 -29
  844. package/dist/credentials-D5uBf_C5.js +0 -265
  845. package/dist/cron-cli-lGupeVCW.js +0 -634
  846. package/dist/daemon-cli-Cs_edi0I.js +0 -339
  847. package/dist/daemon-install-DIFpP_qv.js +0 -175
  848. package/dist/deliver-DYa_DFZU.js +0 -106
  849. package/dist/deliver-runtime-DCW_o2Ot.js +0 -106
  850. package/dist/devices-cli-YsGOW2-w.js +0 -340
  851. package/dist/diagnostic-vMghIesG.js +0 -310
  852. package/dist/directory-cli-DtjMQjU5.js +0 -306
  853. package/dist/directory.static-DBZGvsdF.js +0 -44
  854. package/dist/discord-DYCu19HT.js +0 -109
  855. package/dist/discovery-DZYAoDF_.js +0 -48
  856. package/dist/dns-cli-DqW4pNgW.js +0 -216
  857. package/dist/docs-cli-Bu9TBlDU.js +0 -173
  858. package/dist/doctor-completion-B5hcQD5c.js +0 -90
  859. package/dist/doctor-config-flow-BBB2ZKfT.js +0 -107
  860. package/dist/doctor-config-flow-DDBYUS9f.js +0 -2437
  861. package/dist/enable-Tmsp8QuB.js +0 -24
  862. package/dist/env-overrides-BHxqjYZG.js +0 -434
  863. package/dist/env-overrides.runtime-Cz98bf-l.js +0 -17
  864. package/dist/exec-approvals-cli-wO5cYfMa.js +0 -419
  865. package/dist/gateway-cli-CFvDGhB9.js +0 -26429
  866. package/dist/gateway-install-token-CskJfo_N.js +0 -163
  867. package/dist/gateway-rpc-srYfBID9.js +0 -26
  868. package/dist/gateway-runtime-C76hUmUV.js +0 -69
  869. package/dist/googlechat-Cha5utST.js +0 -307
  870. package/dist/health-DDQYYsJy.js +0 -108
  871. package/dist/health-DXZykGaX.js +0 -570
  872. package/dist/hooks-cli-DfkurPYP.js +0 -995
  873. package/dist/imessage-B26k39pl.js +0 -110
  874. package/dist/imessage-Bp1_6cws.js +0 -31
  875. package/dist/inbound-reply-dispatch-DoIJLztA.js +0 -71
  876. package/dist/install-target-BjOuS4I8.js +0 -574
  877. package/dist/installs-Cz4k0W1Y.js +0 -532
  878. package/dist/io-B0OKifLZ.js +0 -28
  879. package/dist/io-DcoxdH6t.js +0 -9570
  880. package/dist/ipv4-CTQQ4_IW.js +0 -82
  881. package/dist/irc-B8vBDigm.js +0 -672
  882. package/dist/library-VCM_cQY4.js +0 -107
  883. package/dist/lifecycle-core-Ctz36PdQ.js +0 -382
  884. package/dist/line-B_uTLrdI.js +0 -530
  885. package/dist/llm-slug-generator-YWg0g2pj.js +0 -67
  886. package/dist/logging-S-5LPdfQ.js +0 -13
  887. package/dist/logging-ueBMCGMR.js +0 -29
  888. package/dist/login-qr-pcACm2Ng.js +0 -107
  889. package/dist/login-qr-pv-kxMfF.js +0 -233
  890. package/dist/logs-cli-RgADgSMO.js +0 -254
  891. package/dist/manager-runtime-BhTkoKmb.js +0 -106
  892. package/dist/manager.runtime-BjHzikoK.js +0 -710
  893. package/dist/matrix-C4EEu2Qp.js +0 -1490
  894. package/dist/matrix-Dfzcc5nV.js +0 -1269
  895. package/dist/mcp-cli-CJmOm9Oj.js +0 -86
  896. package/dist/media-understanding.runtime-DCETFCw_.js +0 -111
  897. package/dist/memory-cli-DFqd6tYx.js +0 -106
  898. package/dist/method-scopes-D-Q9dvbj.js +0 -2586
  899. package/dist/model-picker-Z-CUcuMr.js +0 -390
  900. package/dist/model-picker-v5mUsZ4J.js +0 -107
  901. package/dist/model-picker.runtime-A_z0dHfS.js +0 -120
  902. package/dist/model-suppression.runtime-QVWVJRr-.js +0 -111
  903. package/dist/models-Bbj0xV4F.js +0 -2514
  904. package/dist/models-D-OIjZqU.js +0 -113
  905. package/dist/models-cli-Bpn-5i4h.js +0 -304
  906. package/dist/models-config-Cwa5cJbC.js +0 -106
  907. package/dist/monitor-BchfCAaU.js +0 -6823
  908. package/dist/monitor-BydV44SP.js +0 -3076
  909. package/dist/monitor-CT8axwfm.js +0 -767
  910. package/dist/monitor-CZGWNOvn.js +0 -777
  911. package/dist/monitor-DN62r69g.js +0 -3468
  912. package/dist/monitor-DZ0fzJku.js +0 -110
  913. package/dist/monitor-DvNjzWFu.js +0 -108
  914. package/dist/monitor-shared-B-DBSlkQ.js +0 -444
  915. package/dist/msteams-Bf-wk2Rp.js +0 -852
  916. package/dist/node-cli-kH16TQI7.js +0 -2498
  917. package/dist/node-resolve-DfOpQmxm.js +0 -835
  918. package/dist/nodes-cli-CkAMXW5u.js +0 -1375
  919. package/dist/nostr-B8UGHclZ.js +0 -8744
  920. package/dist/npm-resolution-DmjlifII.js +0 -60
  921. package/dist/onboard-C883nfyw.js +0 -589
  922. package/dist/onboard-channels-Dc-BxN7p.js +0 -200
  923. package/dist/onboard-channels-j5EENtum.js +0 -1241
  924. package/dist/onboard-custom-0atne0C5.js +0 -571
  925. package/dist/onboard-custom-CWMqwjJx.js +0 -109
  926. package/dist/onboard-helpers-D3wWfH8F.js +0 -335
  927. package/dist/onboard-helpers-DZmRCe8l.js +0 -108
  928. package/dist/onboard-remote-Cn6kW-p0.js +0 -112
  929. package/dist/onboard-remote-Cx4w5VAk.js +0 -181
  930. package/dist/onboard-search-Ck9HRh2M.js +0 -297
  931. package/dist/onboard-skills-BtqrGioT.js +0 -133
  932. package/dist/onboard-skills-Dnw19Os8.js +0 -112
  933. package/dist/outbound-media-C5Nv4o18.js +0 -11
  934. package/dist/pairing-cli-Cwy9QZ_4.js +0 -212
  935. package/dist/perplexity-Brhpb45X.js +0 -24
  936. package/dist/pi-model-discovery-runtime-DIOdo6D8.js +0 -106
  937. package/dist/pi-tools.before-tool-call.runtime-CFM4gsDF.js +0 -380
  938. package/dist/plugin-install-BOV00hia.js +0 -112
  939. package/dist/plugin-install-Bak8fUBv.js +0 -184
  940. package/dist/plugin-install-plan-bKkEefRf.js +0 -49
  941. package/dist/plugin-registry-DxAXQUlZ.js +0 -108
  942. package/dist/plugin-registry-n0p3phem.js +0 -49
  943. package/dist/plugins-Ca3RK8Fi.js +0 -106
  944. package/dist/plugins-cli-BnC51H2R.js +0 -912
  945. package/dist/policy-BJv97w9e.js +0 -143
  946. package/dist/preflight-audio.runtime-BrFcf-6_.js +0 -111
  947. package/dist/probe-063xvvZc.js +0 -19
  948. package/dist/probe-BJEb2wGv.js +0 -1793
  949. package/dist/probe-CJQlxgsl.js +0 -47
  950. package/dist/probe-Caa2HznF.js +0 -6328
  951. package/dist/probe-CfL4tnJ6.js +0 -129
  952. package/dist/probe-auth-DN2Ec83-.js +0 -38
  953. package/dist/probe-auth-D_UKzu4m.js +0 -48
  954. package/dist/program-BOMdC7MC.js +0 -247
  955. package/dist/prompt-select-styled-DDnCfM3j.js +0 -2673
  956. package/dist/provider-api-key-auth.runtime-DUns3fwX.js +0 -116
  957. package/dist/provider-auth-choice-B_j1ctT2.js +0 -126
  958. package/dist/provider-auth-choice-preference-BaOBZ_Xn.js +0 -189
  959. package/dist/provider-auth-choice.runtime-DOako_zV.js +0 -118
  960. package/dist/provider-auth-guidance-CrjxnoNZ.js +0 -34
  961. package/dist/provider-runtime.runtime-BkOkgmTw.js +0 -106
  962. package/dist/provider-self-hosted-setup-BFDU6dRa.js +0 -182
  963. package/dist/provider-usage-CaDE0mqq.js +0 -106
  964. package/dist/provider-web-search-BR7etTjJ.js +0 -2392
  965. package/dist/provider-wizard-DCPdKUvb.js +0 -152
  966. package/dist/push-apns-B_OZjm4v.js +0 -1038
  967. package/dist/pw-ai-dG60P0hQ.js +0 -1866
  968. package/dist/qr-cli-DWfiw79I.js +0 -369
  969. package/dist/qr-cli-DwuKtyZQ.js +0 -108
  970. package/dist/reactions-CIGAPBn8.js +0 -281
  971. package/dist/read-only-account-inspect.discord.runtime-D54mnq8l.js +0 -111
  972. package/dist/read-only-account-inspect.slack.runtime-Bxs9ObMC.js +0 -111
  973. package/dist/read-only-account-inspect.telegram.runtime-UoVuf_Yo.js +0 -111
  974. package/dist/redact-snapshot-DZ3Vq-SC.js +0 -2657
  975. package/dist/ref-contract-D96lSYLs.js +0 -53
  976. package/dist/register.agent-2KmeahEL.js +0 -434
  977. package/dist/register.backup-ECBnWVR7.js +0 -624
  978. package/dist/register.configure-Doz1daCp.js +0 -247
  979. package/dist/register.maintenance-C33cV-WM.js +0 -569
  980. package/dist/register.message-CnL0NiF6.js +0 -704
  981. package/dist/register.onboard-BrYGZeQA.js +0 -187
  982. package/dist/register.setup-Bx6gEg6X.js +0 -207
  983. package/dist/register.status-health-sessions-FLb0CUOO.js +0 -493
  984. package/dist/register.subclis-BuqgaeIf.js +0 -12
  985. package/dist/register.subclis-DwdgfdnT.js +0 -315
  986. package/dist/registry-xhgvU89y.js +0 -1107
  987. package/dist/replies-hB2aipLu.js +0 -110
  988. package/dist/resolve-3ErMOltL.js +0 -660
  989. package/dist/resolve-channels-BV8GXuPe.js +0 -226
  990. package/dist/resolve-channels-CTY_XRIP.js +0 -262
  991. package/dist/resolve-users-DQ4Ne4Zc.js +0 -143
  992. package/dist/routes-BNDsNO_e.js +0 -7097
  993. package/dist/rpc-BLGTBWXq.js +0 -67
  994. package/dist/run-main-COAE4GlI.js +0 -423
  995. package/dist/runtime-discord-ops.runtime-Dxg-nlgd.js +0 -9073
  996. package/dist/runtime-slack-ops.runtime-Di474LJr.js +0 -4551
  997. package/dist/runtime-telegram-ops.runtime-Da8vgf3O.js +0 -128
  998. package/dist/runtime-whatsapp-login.runtime-DcouP4iF.js +0 -109
  999. package/dist/runtime-whatsapp-outbound.runtime-CYamaEJX.js +0 -112
  1000. package/dist/sandbox-cli-U5ZTxhxL.js +0 -530
  1001. package/dist/search-manager-CfizyEMk.js +0 -386
  1002. package/dist/search-manager-DaF2QP4s.js +0 -15
  1003. package/dist/secrets-cli-C0gytFip.js +0 -2065
  1004. package/dist/security-cli-C74EuLUO.js +0 -570
  1005. package/dist/send-BTLVBf_E.js +0 -631
  1006. package/dist/send-BlWWCEZE.js +0 -1025
  1007. package/dist/send-CfypD1B_.js +0 -100
  1008. package/dist/send-Cm9v3uhF.js +0 -283
  1009. package/dist/send-g2odQuYI.js +0 -629
  1010. package/dist/server-C8b5QJ2s.js +0 -106
  1011. package/dist/server-node-events-xqQe5xiu.js +0 -501
  1012. package/dist/sessions-CSSzvgPQ.js +0 -107
  1013. package/dist/sessions-z0GIvdKa.js +0 -218
  1014. package/dist/setup-D9XTmlF8.js +0 -387
  1015. package/dist/setup-core-BDrLOwYO.js +0 -143
  1016. package/dist/setup-core-CM7cY7_i.js +0 -166
  1017. package/dist/setup-core-CnmgANY-.js +0 -205
  1018. package/dist/setup-core-DgcjCKmG.js +0 -47
  1019. package/dist/setup-surface-DzRrVKYj.js +0 -490
  1020. package/dist/setup.finalize-UaPu_adv.js +0 -517
  1021. package/dist/setup.gateway-config-Djc1ceEh.js +0 -338
  1022. package/dist/setup.secret-input-BkczghbR.js +0 -25
  1023. package/dist/shared-BHizGoNk.js +0 -298
  1024. package/dist/shared-CUfYhQkP.js +0 -96
  1025. package/dist/shared-DYYqr9EC.js +0 -75
  1026. package/dist/shared-DthOxMRQ.js +0 -182
  1027. package/dist/shared-On_A5_hW.js +0 -102
  1028. package/dist/signal-D6px9PGZ.js +0 -109
  1029. package/dist/skills-B4h1k-SP.js +0 -853
  1030. package/dist/skills-Bto10BGB.js +0 -19
  1031. package/dist/skills-cli-CXGR3Y5j.js +0 -291
  1032. package/dist/skills-install-B1AlkK8C.js +0 -763
  1033. package/dist/skills-status-BsmJ_iSg.js +0 -20
  1034. package/dist/skills-status-DGdxY3OI.js +0 -169
  1035. package/dist/slack-B7vWFmxP.js +0 -109
  1036. package/dist/slash-commands.runtime-DXdAT84n.js +0 -123
  1037. package/dist/slash-dispatch.runtime-CNf2-9Aj.js +0 -136
  1038. package/dist/slash-skill-commands.runtime-CBjffHRX.js +0 -111
  1039. package/dist/src-Cp7P7T08.js +0 -1696
  1040. package/dist/status-158fWh4A.js +0 -43
  1041. package/dist/status-BJIVLJnb.js +0 -1599
  1042. package/dist/status-BQiBI6N9.js +0 -126
  1043. package/dist/status-CZipXGUu.js +0 -121
  1044. package/dist/status-ZZIVFLI-.js +0 -606
  1045. package/dist/status-json-BNUy5Mem.js +0 -286
  1046. package/dist/status.link-channel-B694y1Xu.js +0 -138
  1047. package/dist/status.scan.deps.runtime-BcoKEzQD.js +0 -121
  1048. package/dist/status.scan.runtime-CqScDt-p.js +0 -114
  1049. package/dist/status.summary-AMek7qvI.js +0 -592
  1050. package/dist/status.summary.runtime-XgkcQ_kr.js +0 -113
  1051. package/dist/subagent-orphan-recovery-CrCYTmFC.js +0 -302
  1052. package/dist/subagent-registry-runtime-Cg-YvLx3.js +0 -106
  1053. package/dist/synology-chat-0G85jIqQ.js +0 -297
  1054. package/dist/system-cli-kZtSxKNm.js +0 -92
  1055. package/dist/telegram-DV0Wy89w.js +0 -109
  1056. package/dist/text-chunking-C2J2Oeul.js +0 -84
  1057. package/dist/tlon-DmK1NUVP.js +0 -433
  1058. package/dist/tui-D3bNPLG7.js +0 -3834
  1059. package/dist/tui-cli-DtMp9k_s.js +0 -132
  1060. package/dist/types.secrets-DuSPmmWB.js +0 -80
  1061. package/dist/ui-CeGztSEL.js +0 -31
  1062. package/dist/update-De7VudzP.js +0 -1036
  1063. package/dist/update-cli-BH8Pb-So.js +0 -1498
  1064. package/dist/update-offset-store-syELkdEW.js +0 -107
  1065. package/dist/update-runner-Cq-Q40T9.js +0 -1496
  1066. package/dist/web-CjMtvfSq.js +0 -107
  1067. package/dist/webhook-targets-_jTR0Bb_.js +0 -181
  1068. package/dist/webhooks-cli-DQ6u2Qau.js +0 -349
  1069. package/dist/whatsapp-CyLk16SZ.js +0 -109
  1070. package/dist/whatsapp-actions-Dzr2Wzqw.js +0 -162
  1071. package/dist/workspace-dirs-L1_QQ9mB.js +0 -2002
  1072. package/dist/zalo-CrehfXvK.js +0 -415
  1073. package/dist/zalouser-D1QD-O-I.js +0 -30911
  1074. package/dist/zod-schema.core-CWxzqcUs.js +0 -541
@@ -1,3468 +0,0 @@
1
- import { $v as registerSessionBindingAdapter, Ah as createScopedPairingAccess, Ch as createFixedWindowRateLimiter, Dh as createPersistentDedupe, Fh as createEventDispatcher, Hh as resolveFeishuAccount, Ih as createFeishuClient, Lh as createFeishuWSClient, Oh as issuePairingChallenge, Ph as probeFeishu, Pt as resolveThreadBindingConversationIdFromBindingId, Qg as buildPendingHistoryContextFromMap, Rh as raceWithTimeoutAndAbort, Sh as WEBHOOK_RATE_LIMIT_DEFAULTS, Th as buildFeishuConversationId, WS as createDedupeCache, Wg as logTypingFailure, Zv as getSessionBindingService, _S as installRequestBodyLimitGuard, __ as resolveThreadBindingMaxAgeMsForChannel, ag as createTypingCallbacks, e_ as clearHistoryEntriesIfEnabled, ey as unregisterSessionBindingAdapter, fm as ensureConfiguredAcpRouteReady, gh as applyBasicWebhookRequestGuards, h_ as resolveThreadBindingIdleTimeoutMsForChannel, iS as resolveAgentOutboundIdentity, jh as buildAgentMediaPayload, og as createReplyPrefixContext, pm as resolveConfiguredAcpRoute, r_ as recordPendingHistoryEntryIfEnabled, wh as createWebhookAnomalyTracker, xh as WEBHOOK_ANOMALY_COUNTER_DEFAULTS, yS as readJsonBodyWithLimit, zh as listEnabledFeishuAccounts } from "./auth-profiles-BJcHzwPy.js";
2
- import { c as normalizeAgentId, u as resolveAgentIdFromSessionKey } from "./session-key-DyhRsRh-.js";
3
- import { n as normalizeAccountId } from "./account-id-O4Og6DrK.js";
4
- import { J as fetchWithSsrFGuard } from "./provider-web-search-BR7etTjJ.js";
5
- import { o as resolveGlobalSingleton } from "./text-runtime-CgEQ9Y9_.js";
6
- import { n as deriveLastRoutePolicy } from "./resolve-route-IkBfMjBz.js";
7
- import { f as warnMissingProviderGroupPolicyFallbackOnce, l as resolveDefaultGroupPolicy, u as resolveOpenProviderRuntimeGroupPolicy } from "./group-access-BWGTPTwF.js";
8
- import { t as readJsonFileWithFallback } from "./json-store-DlpyvQXN.js";
9
- import { a as resolveReceiveIdType, t as getFeishuRuntime } from "./runtime-CXlZEYly.js";
10
- import { a as resolveFeishuReplyPolicy, n as resolveFeishuAllowlistMatch, r as resolveFeishuGroupConfig, t as isFeishuGroupAllowed } from "./policy-DvbWFJX3.js";
11
- import { C as sendMediaFeishu, E as normalizeFeishuExternalKey, a as sendCardFeishu, b as downloadMessageResourceFeishu, c as sendStructuredCardFeishu, d as buildMentionedCardContent, i as resolveFeishuCardTemplate, n as getMessageFeishu, o as sendMarkdownCardFeishu, p as extractMentionTargets, r as listFeishuThreadMessages, s as sendMessageFeishu, u as parsePostContent, y as isMentionForwardRequest } from "./send-BlWWCEZE.js";
12
- import fs from "node:fs";
13
- import path from "node:path";
14
- import os from "node:os";
15
- import crypto from "node:crypto";
16
- import * as Lark from "@larksuiteoapi/node-sdk";
17
- import * as crypto$2 from "crypto";
18
- import * as http from "http";
19
- //#region extensions/feishu/src/thread-bindings.ts
20
- const state = resolveGlobalSingleton(Symbol.for("moldclaw.feishuThreadBindingsState"), () => ({
21
- managersByAccountId: /* @__PURE__ */ new Map(),
22
- bindingsByAccountConversation: /* @__PURE__ */ new Map()
23
- }));
24
- const MANAGERS_BY_ACCOUNT_ID = state.managersByAccountId;
25
- const BINDINGS_BY_ACCOUNT_CONVERSATION = state.bindingsByAccountConversation;
26
- function resolveBindingKey(params) {
27
- return `${params.accountId}:${params.conversationId}`;
28
- }
29
- function toSessionBindingTargetKind(raw) {
30
- return raw === "subagent" ? "subagent" : "session";
31
- }
32
- function toFeishuTargetKind(raw) {
33
- return raw === "subagent" ? "subagent" : "acp";
34
- }
35
- function toSessionBindingRecord(record, defaults) {
36
- const idleExpiresAt = defaults.idleTimeoutMs > 0 ? record.lastActivityAt + defaults.idleTimeoutMs : void 0;
37
- const maxAgeExpiresAt = defaults.maxAgeMs > 0 ? record.boundAt + defaults.maxAgeMs : void 0;
38
- const expiresAt = idleExpiresAt != null && maxAgeExpiresAt != null ? Math.min(idleExpiresAt, maxAgeExpiresAt) : idleExpiresAt ?? maxAgeExpiresAt;
39
- return {
40
- bindingId: resolveBindingKey({
41
- accountId: record.accountId,
42
- conversationId: record.conversationId
43
- }),
44
- targetSessionKey: record.targetSessionKey,
45
- targetKind: toSessionBindingTargetKind(record.targetKind),
46
- conversation: {
47
- channel: "feishu",
48
- accountId: record.accountId,
49
- conversationId: record.conversationId,
50
- parentConversationId: record.parentConversationId
51
- },
52
- status: "active",
53
- boundAt: record.boundAt,
54
- expiresAt,
55
- metadata: {
56
- agentId: record.agentId,
57
- label: record.label,
58
- boundBy: record.boundBy,
59
- deliveryTo: record.deliveryTo,
60
- deliveryThreadId: record.deliveryThreadId,
61
- lastActivityAt: record.lastActivityAt,
62
- idleTimeoutMs: defaults.idleTimeoutMs,
63
- maxAgeMs: defaults.maxAgeMs
64
- }
65
- };
66
- }
67
- function createFeishuThreadBindingManager(params) {
68
- const accountId = normalizeAccountId(params.accountId);
69
- const existing = MANAGERS_BY_ACCOUNT_ID.get(accountId);
70
- if (existing) return existing;
71
- const idleTimeoutMs = resolveThreadBindingIdleTimeoutMsForChannel({
72
- cfg: params.cfg,
73
- channel: "feishu",
74
- accountId
75
- });
76
- const maxAgeMs = resolveThreadBindingMaxAgeMsForChannel({
77
- cfg: params.cfg,
78
- channel: "feishu",
79
- accountId
80
- });
81
- const manager = {
82
- accountId,
83
- getByConversationId: (conversationId) => BINDINGS_BY_ACCOUNT_CONVERSATION.get(resolveBindingKey({
84
- accountId,
85
- conversationId
86
- })),
87
- listBySessionKey: (targetSessionKey) => [...BINDINGS_BY_ACCOUNT_CONVERSATION.values()].filter((record) => record.accountId === accountId && record.targetSessionKey === targetSessionKey),
88
- bindConversation: ({ conversationId, parentConversationId, targetKind, targetSessionKey, metadata }) => {
89
- const normalizedConversationId = conversationId.trim();
90
- if (!normalizedConversationId || !targetSessionKey.trim()) return null;
91
- const now = Date.now();
92
- const record = {
93
- accountId,
94
- conversationId: normalizedConversationId,
95
- parentConversationId: parentConversationId?.trim() || void 0,
96
- deliveryTo: typeof metadata?.deliveryTo === "string" && metadata.deliveryTo.trim() ? metadata.deliveryTo.trim() : void 0,
97
- deliveryThreadId: typeof metadata?.deliveryThreadId === "string" && metadata.deliveryThreadId.trim() ? metadata.deliveryThreadId.trim() : void 0,
98
- targetKind: toFeishuTargetKind(targetKind),
99
- targetSessionKey: targetSessionKey.trim(),
100
- agentId: typeof metadata?.agentId === "string" && metadata.agentId.trim() ? metadata.agentId.trim() : resolveAgentIdFromSessionKey(targetSessionKey),
101
- label: typeof metadata?.label === "string" && metadata.label.trim() ? metadata.label.trim() : void 0,
102
- boundBy: typeof metadata?.boundBy === "string" && metadata.boundBy.trim() ? metadata.boundBy.trim() : void 0,
103
- boundAt: now,
104
- lastActivityAt: now
105
- };
106
- BINDINGS_BY_ACCOUNT_CONVERSATION.set(resolveBindingKey({
107
- accountId,
108
- conversationId: normalizedConversationId
109
- }), record);
110
- return record;
111
- },
112
- touchConversation: (conversationId, at = Date.now()) => {
113
- const key = resolveBindingKey({
114
- accountId,
115
- conversationId
116
- });
117
- const existingRecord = BINDINGS_BY_ACCOUNT_CONVERSATION.get(key);
118
- if (!existingRecord) return null;
119
- const updated = {
120
- ...existingRecord,
121
- lastActivityAt: at
122
- };
123
- BINDINGS_BY_ACCOUNT_CONVERSATION.set(key, updated);
124
- return updated;
125
- },
126
- unbindConversation: (conversationId) => {
127
- const key = resolveBindingKey({
128
- accountId,
129
- conversationId
130
- });
131
- const existingRecord = BINDINGS_BY_ACCOUNT_CONVERSATION.get(key);
132
- if (!existingRecord) return null;
133
- BINDINGS_BY_ACCOUNT_CONVERSATION.delete(key);
134
- return existingRecord;
135
- },
136
- unbindBySessionKey: (targetSessionKey) => {
137
- const removed = [];
138
- for (const record of [...BINDINGS_BY_ACCOUNT_CONVERSATION.values()]) {
139
- if (record.accountId !== accountId || record.targetSessionKey !== targetSessionKey) continue;
140
- BINDINGS_BY_ACCOUNT_CONVERSATION.delete(resolveBindingKey({
141
- accountId,
142
- conversationId: record.conversationId
143
- }));
144
- removed.push(record);
145
- }
146
- return removed;
147
- },
148
- stop: () => {
149
- for (const key of [...BINDINGS_BY_ACCOUNT_CONVERSATION.keys()]) if (key.startsWith(`${accountId}:`)) BINDINGS_BY_ACCOUNT_CONVERSATION.delete(key);
150
- MANAGERS_BY_ACCOUNT_ID.delete(accountId);
151
- unregisterSessionBindingAdapter({
152
- channel: "feishu",
153
- accountId
154
- });
155
- }
156
- };
157
- registerSessionBindingAdapter({
158
- channel: "feishu",
159
- accountId,
160
- capabilities: { placements: ["current"] },
161
- bind: async (input) => {
162
- if (input.conversation.channel !== "feishu" || input.placement === "child") return null;
163
- const bound = manager.bindConversation({
164
- conversationId: input.conversation.conversationId,
165
- parentConversationId: input.conversation.parentConversationId,
166
- targetKind: input.targetKind,
167
- targetSessionKey: input.targetSessionKey,
168
- metadata: input.metadata
169
- });
170
- return bound ? toSessionBindingRecord(bound, {
171
- idleTimeoutMs,
172
- maxAgeMs
173
- }) : null;
174
- },
175
- listBySession: (targetSessionKey) => manager.listBySessionKey(targetSessionKey).map((entry) => toSessionBindingRecord(entry, {
176
- idleTimeoutMs,
177
- maxAgeMs
178
- })),
179
- resolveByConversation: (ref) => {
180
- if (ref.channel !== "feishu") return null;
181
- const found = manager.getByConversationId(ref.conversationId);
182
- return found ? toSessionBindingRecord(found, {
183
- idleTimeoutMs,
184
- maxAgeMs
185
- }) : null;
186
- },
187
- touch: (bindingId, at) => {
188
- const conversationId = resolveThreadBindingConversationIdFromBindingId({
189
- accountId,
190
- bindingId
191
- });
192
- if (conversationId) manager.touchConversation(conversationId, at);
193
- },
194
- unbind: async (input) => {
195
- if (input.targetSessionKey?.trim()) return manager.unbindBySessionKey(input.targetSessionKey.trim()).map((entry) => toSessionBindingRecord(entry, {
196
- idleTimeoutMs,
197
- maxAgeMs
198
- }));
199
- const conversationId = resolveThreadBindingConversationIdFromBindingId({
200
- accountId,
201
- bindingId: input.bindingId
202
- });
203
- if (!conversationId) return [];
204
- const removed = manager.unbindConversation(conversationId);
205
- return removed ? [toSessionBindingRecord(removed, {
206
- idleTimeoutMs,
207
- maxAgeMs
208
- })] : [];
209
- }
210
- });
211
- MANAGERS_BY_ACCOUNT_ID.set(accountId, manager);
212
- return manager;
213
- }
214
- function getFeishuThreadBindingManager(accountId) {
215
- return MANAGERS_BY_ACCOUNT_ID.get(normalizeAccountId(accountId)) ?? null;
216
- }
217
- //#endregion
218
- //#region extensions/feishu/src/dedup.ts
219
- const DEDUP_TTL_MS = 1440 * 60 * 1e3;
220
- const MEMORY_MAX_SIZE = 1e3;
221
- const FILE_MAX_ENTRIES = 1e4;
222
- const EVENT_DEDUP_TTL_MS = 300 * 1e3;
223
- const EVENT_MEMORY_MAX_SIZE = 2e3;
224
- const memoryDedupe = createDedupeCache({
225
- ttlMs: DEDUP_TTL_MS,
226
- maxSize: MEMORY_MAX_SIZE
227
- });
228
- const processingClaims = createDedupeCache({
229
- ttlMs: EVENT_DEDUP_TTL_MS,
230
- maxSize: EVENT_MEMORY_MAX_SIZE
231
- });
232
- function resolveStateDirFromEnv(env = process.env) {
233
- const stateOverride = env.MOLDCLAW_STATE_DIR?.trim() || env.CLAWDBOT_STATE_DIR?.trim();
234
- if (stateOverride) return stateOverride;
235
- if (env.VITEST || env.NODE_ENV === "test") return path.join(os.tmpdir(), ["moldclaw-vitest", String(process.pid)].join("-"));
236
- return path.join(os.homedir(), ".moldclaw");
237
- }
238
- function resolveNamespaceFilePath(namespace) {
239
- const safe = namespace.replace(/[^a-zA-Z0-9_-]/g, "_");
240
- return path.join(resolveStateDirFromEnv(), "feishu", "dedup", `${safe}.json`);
241
- }
242
- const persistentDedupe = createPersistentDedupe({
243
- ttlMs: DEDUP_TTL_MS,
244
- memoryMaxSize: MEMORY_MAX_SIZE,
245
- fileMaxEntries: FILE_MAX_ENTRIES,
246
- resolveFilePath: resolveNamespaceFilePath
247
- });
248
- function resolveEventDedupeKey(namespace, messageId) {
249
- const trimmed = messageId?.trim();
250
- if (!trimmed) return null;
251
- return `${namespace}:${trimmed}`;
252
- }
253
- function normalizeMessageId(messageId) {
254
- const trimmed = messageId?.trim();
255
- return trimmed ? trimmed : null;
256
- }
257
- function resolveMemoryDedupeKey(namespace, messageId) {
258
- const trimmed = normalizeMessageId(messageId);
259
- if (!trimmed) return null;
260
- return `${namespace}:${trimmed}`;
261
- }
262
- function tryBeginFeishuMessageProcessing(messageId, namespace = "global") {
263
- return !processingClaims.check(resolveEventDedupeKey(namespace, messageId));
264
- }
265
- function releaseFeishuMessageProcessing(messageId, namespace = "global") {
266
- processingClaims.delete(resolveEventDedupeKey(namespace, messageId));
267
- }
268
- async function finalizeFeishuMessageProcessing(params) {
269
- const { messageId, namespace = "global", log, claimHeld = false } = params;
270
- const normalizedMessageId = normalizeMessageId(messageId);
271
- const memoryKey = resolveMemoryDedupeKey(namespace, messageId);
272
- if (!memoryKey || !normalizedMessageId) return false;
273
- if (!claimHeld && !tryBeginFeishuMessageProcessing(normalizedMessageId, namespace)) return false;
274
- if (!tryRecordMessage(memoryKey)) {
275
- releaseFeishuMessageProcessing(normalizedMessageId, namespace);
276
- return false;
277
- }
278
- if (!await tryRecordMessagePersistent(normalizedMessageId, namespace, log)) {
279
- releaseFeishuMessageProcessing(normalizedMessageId, namespace);
280
- return false;
281
- }
282
- return true;
283
- }
284
- async function recordProcessedFeishuMessage(messageId, namespace = "global", log) {
285
- const normalizedMessageId = normalizeMessageId(messageId);
286
- const memoryKey = resolveMemoryDedupeKey(namespace, messageId);
287
- if (!memoryKey || !normalizedMessageId) return false;
288
- tryRecordMessage(memoryKey);
289
- return await tryRecordMessagePersistent(normalizedMessageId, namespace, log);
290
- }
291
- async function hasProcessedFeishuMessage(messageId, namespace = "global", log) {
292
- const normalizedMessageId = normalizeMessageId(messageId);
293
- const memoryKey = resolveMemoryDedupeKey(namespace, messageId);
294
- if (!memoryKey || !normalizedMessageId) return false;
295
- if (hasRecordedMessage(memoryKey)) return true;
296
- return hasRecordedMessagePersistent(normalizedMessageId, namespace, log);
297
- }
298
- /**
299
- * Synchronous dedup — memory only.
300
- * Kept for backward compatibility; prefer {@link tryRecordMessagePersistent}.
301
- */
302
- function tryRecordMessage(messageId) {
303
- return !memoryDedupe.check(messageId);
304
- }
305
- function hasRecordedMessage(messageId) {
306
- const trimmed = messageId.trim();
307
- if (!trimmed) return false;
308
- return memoryDedupe.peek(trimmed);
309
- }
310
- async function tryRecordMessagePersistent(messageId, namespace = "global", log) {
311
- return persistentDedupe.checkAndRecord(messageId, {
312
- namespace,
313
- onDiskError: (error) => {
314
- log?.(`feishu-dedup: disk error, falling back to memory: ${String(error)}`);
315
- }
316
- });
317
- }
318
- async function hasRecordedMessagePersistent(messageId, namespace = "global", log) {
319
- const trimmed = messageId.trim();
320
- if (!trimmed) return false;
321
- const now = Date.now();
322
- const filePath = resolveNamespaceFilePath(namespace);
323
- try {
324
- const { value } = await readJsonFileWithFallback(filePath, {});
325
- const seenAt = value[trimmed];
326
- if (typeof seenAt !== "number" || !Number.isFinite(seenAt)) return false;
327
- return DEDUP_TTL_MS <= 0 || now - seenAt < DEDUP_TTL_MS;
328
- } catch (error) {
329
- log?.(`feishu-dedup: persistent peek failed: ${String(error)}`);
330
- return false;
331
- }
332
- }
333
- async function warmupDedupFromDisk(namespace, log) {
334
- return persistentDedupe.warmup(namespace, (error) => {
335
- log?.(`feishu-dedup: warmup disk error: ${String(error)}`);
336
- });
337
- }
338
- //#endregion
339
- //#region extensions/feishu/src/dynamic-agent.ts
340
- /**
341
- * Check if a dynamic agent should be created for a DM user and create it if needed.
342
- * This creates a unique agent instance with its own workspace for each DM user.
343
- */
344
- async function maybeCreateDynamicAgent(params) {
345
- const { cfg, runtime, senderOpenId, dynamicCfg, log } = params;
346
- const existingBindings = cfg.bindings ?? [];
347
- if (existingBindings.some((b) => b.match?.channel === "feishu" && b.match?.peer?.kind === "direct" && b.match?.peer?.id === senderOpenId)) return {
348
- created: false,
349
- updatedCfg: cfg
350
- };
351
- if (dynamicCfg.maxAgents !== void 0) {
352
- if ((cfg.agents?.list ?? []).filter((a) => a.id.startsWith("feishu-")).length >= dynamicCfg.maxAgents) {
353
- log(`feishu: maxAgents limit (${dynamicCfg.maxAgents}) reached, not creating agent for ${senderOpenId}`);
354
- return {
355
- created: false,
356
- updatedCfg: cfg
357
- };
358
- }
359
- }
360
- const agentId = `feishu-${senderOpenId}`;
361
- if ((cfg.agents?.list ?? []).find((a) => a.id === agentId)) {
362
- log(`feishu: agent "${agentId}" exists, adding missing binding for ${senderOpenId}`);
363
- const updatedCfg = {
364
- ...cfg,
365
- bindings: [...existingBindings, {
366
- agentId,
367
- match: {
368
- channel: "feishu",
369
- peer: {
370
- kind: "direct",
371
- id: senderOpenId
372
- }
373
- }
374
- }]
375
- };
376
- await runtime.config.writeConfigFile(updatedCfg);
377
- return {
378
- created: true,
379
- updatedCfg,
380
- agentId
381
- };
382
- }
383
- const workspaceTemplate = dynamicCfg.workspaceTemplate ?? "~/.moldclaw/workspace-{agentId}";
384
- const agentDirTemplate = dynamicCfg.agentDirTemplate ?? "~/.moldclaw/agents/{agentId}/agent";
385
- const workspace = resolveUserPath(workspaceTemplate.replace("{userId}", senderOpenId).replace("{agentId}", agentId));
386
- const agentDir = resolveUserPath(agentDirTemplate.replace("{userId}", senderOpenId).replace("{agentId}", agentId));
387
- log(`feishu: creating dynamic agent "${agentId}" for user ${senderOpenId}`);
388
- log(` workspace: ${workspace}`);
389
- log(` agentDir: ${agentDir}`);
390
- await fs.promises.mkdir(workspace, { recursive: true });
391
- await fs.promises.mkdir(agentDir, { recursive: true });
392
- const updatedCfg = {
393
- ...cfg,
394
- agents: {
395
- ...cfg.agents,
396
- list: [...cfg.agents?.list ?? [], {
397
- id: agentId,
398
- workspace,
399
- agentDir
400
- }]
401
- },
402
- bindings: [...existingBindings, {
403
- agentId,
404
- match: {
405
- channel: "feishu",
406
- peer: {
407
- kind: "direct",
408
- id: senderOpenId
409
- }
410
- }
411
- }]
412
- };
413
- await runtime.config.writeConfigFile(updatedCfg);
414
- return {
415
- created: true,
416
- updatedCfg,
417
- agentId
418
- };
419
- }
420
- /**
421
- * Resolve a path that may start with ~ to the user's home directory.
422
- */
423
- function resolveUserPath(p) {
424
- if (p.startsWith("~/")) return path.join(os.homedir(), p.slice(2));
425
- return p;
426
- }
427
- //#endregion
428
- //#region extensions/feishu/src/streaming-card.ts
429
- const tokenCache = /* @__PURE__ */ new Map();
430
- function resolveApiBase(domain) {
431
- if (domain === "lark") return "https://open.larksuite.com/open-apis";
432
- if (domain && domain !== "feishu" && domain.startsWith("http")) return `${domain.replace(/\/+$/, "")}/open-apis`;
433
- return "https://open.feishu.cn/open-apis";
434
- }
435
- function resolveAllowedHostnames(domain) {
436
- if (domain === "lark") return ["open.larksuite.com"];
437
- if (domain && domain !== "feishu" && domain.startsWith("http")) try {
438
- return [new URL(domain).hostname];
439
- } catch {
440
- return [];
441
- }
442
- return ["open.feishu.cn"];
443
- }
444
- async function getToken(creds) {
445
- const key = `${creds.domain ?? "feishu"}|${creds.appId}`;
446
- const cached = tokenCache.get(key);
447
- if (cached && cached.expiresAt > Date.now() + 6e4) return cached.token;
448
- const { response, release } = await fetchWithSsrFGuard({
449
- url: `${resolveApiBase(creds.domain)}/auth/v3/tenant_access_token/internal`,
450
- init: {
451
- method: "POST",
452
- headers: { "Content-Type": "application/json" },
453
- body: JSON.stringify({
454
- app_id: creds.appId,
455
- app_secret: creds.appSecret
456
- })
457
- },
458
- policy: { allowedHostnames: resolveAllowedHostnames(creds.domain) },
459
- auditContext: "feishu.streaming-card.token"
460
- });
461
- if (!response.ok) {
462
- await release();
463
- throw new Error(`Token request failed with HTTP ${response.status}`);
464
- }
465
- const data = await response.json();
466
- await release();
467
- if (data.code !== 0 || !data.tenant_access_token) throw new Error(`Token error: ${data.msg}`);
468
- tokenCache.set(key, {
469
- token: data.tenant_access_token,
470
- expiresAt: Date.now() + (data.expire ?? 7200) * 1e3
471
- });
472
- return data.tenant_access_token;
473
- }
474
- function truncateSummary(text, max = 50) {
475
- if (!text) return "";
476
- const clean = text.replace(/\n/g, " ").trim();
477
- return clean.length <= max ? clean : clean.slice(0, max - 3) + "...";
478
- }
479
- function mergeStreamingText(previousText, nextText) {
480
- const previous = typeof previousText === "string" ? previousText : "";
481
- const next = typeof nextText === "string" ? nextText : "";
482
- if (!next) return previous;
483
- if (!previous || next === previous) return next;
484
- if (next.startsWith(previous)) return next;
485
- if (previous.startsWith(next)) return previous;
486
- if (next.includes(previous)) return next;
487
- if (previous.includes(next)) return previous;
488
- const maxOverlap = Math.min(previous.length, next.length);
489
- for (let overlap = maxOverlap; overlap > 0; overlap -= 1) if (previous.slice(-overlap) === next.slice(0, overlap)) return `${previous}${next.slice(overlap)}`;
490
- return `${previous}${next}`;
491
- }
492
- function resolveStreamingCardSendMode(options) {
493
- if (options?.replyToMessageId) return "reply";
494
- if (options?.rootId) return "root_create";
495
- return "create";
496
- }
497
- /** Streaming card session manager */
498
- var FeishuStreamingSession = class {
499
- constructor(client, creds, log) {
500
- this.state = null;
501
- this.queue = Promise.resolve();
502
- this.closed = false;
503
- this.lastUpdateTime = 0;
504
- this.pendingText = null;
505
- this.flushTimer = null;
506
- this.updateThrottleMs = 100;
507
- this.client = client;
508
- this.creds = creds;
509
- this.log = log;
510
- }
511
- async start(receiveId, receiveIdType = "chat_id", options) {
512
- if (this.state) return;
513
- const apiBase = resolveApiBase(this.creds.domain);
514
- const elements = [{
515
- tag: "markdown",
516
- content: "⏳ Thinking...",
517
- element_id: "content"
518
- }];
519
- if (options?.note) {
520
- elements.push({ tag: "hr" });
521
- elements.push({
522
- tag: "markdown",
523
- content: `<font color='grey'>${options.note}</font>`,
524
- element_id: "note"
525
- });
526
- }
527
- const cardJson = {
528
- schema: "2.0",
529
- config: {
530
- streaming_mode: true,
531
- summary: { content: "[Generating...]" },
532
- streaming_config: {
533
- print_frequency_ms: { default: 50 },
534
- print_step: { default: 1 }
535
- }
536
- },
537
- body: { elements }
538
- };
539
- if (options?.header) cardJson.header = {
540
- title: {
541
- tag: "plain_text",
542
- content: options.header.title
543
- },
544
- template: resolveFeishuCardTemplate(options.header.template) ?? "blue"
545
- };
546
- const { response: createRes, release: releaseCreate } = await fetchWithSsrFGuard({
547
- url: `${apiBase}/cardkit/v1/cards`,
548
- init: {
549
- method: "POST",
550
- headers: {
551
- Authorization: `Bearer ${await getToken(this.creds)}`,
552
- "Content-Type": "application/json"
553
- },
554
- body: JSON.stringify({
555
- type: "card_json",
556
- data: JSON.stringify(cardJson)
557
- })
558
- },
559
- policy: { allowedHostnames: resolveAllowedHostnames(this.creds.domain) },
560
- auditContext: "feishu.streaming-card.create"
561
- });
562
- if (!createRes.ok) {
563
- await releaseCreate();
564
- throw new Error(`Create card request failed with HTTP ${createRes.status}`);
565
- }
566
- const createData = await createRes.json();
567
- await releaseCreate();
568
- if (createData.code !== 0 || !createData.data?.card_id) throw new Error(`Create card failed: ${createData.msg}`);
569
- const cardId = createData.data.card_id;
570
- const cardContent = JSON.stringify({
571
- type: "card",
572
- data: { card_id: cardId }
573
- });
574
- let sendRes;
575
- const sendOptions = options ?? {};
576
- const sendMode = resolveStreamingCardSendMode(sendOptions);
577
- if (sendMode === "reply") sendRes = await this.client.im.message.reply({
578
- path: { message_id: sendOptions.replyToMessageId },
579
- data: {
580
- msg_type: "interactive",
581
- content: cardContent,
582
- ...sendOptions.replyInThread ? { reply_in_thread: true } : {}
583
- }
584
- });
585
- else if (sendMode === "root_create") sendRes = await this.client.im.message.create({
586
- params: { receive_id_type: receiveIdType },
587
- data: Object.assign({
588
- receive_id: receiveId,
589
- msg_type: "interactive",
590
- content: cardContent
591
- }, { root_id: sendOptions.rootId })
592
- });
593
- else sendRes = await this.client.im.message.create({
594
- params: { receive_id_type: receiveIdType },
595
- data: {
596
- receive_id: receiveId,
597
- msg_type: "interactive",
598
- content: cardContent
599
- }
600
- });
601
- if (sendRes.code !== 0 || !sendRes.data?.message_id) throw new Error(`Send card failed: ${sendRes.msg}`);
602
- this.state = {
603
- cardId,
604
- messageId: sendRes.data.message_id,
605
- sequence: 1,
606
- currentText: "",
607
- hasNote: !!options?.note
608
- };
609
- this.log?.(`Started streaming: cardId=${cardId}, messageId=${sendRes.data.message_id}`);
610
- }
611
- async updateCardContent(text, onError) {
612
- if (!this.state) return;
613
- const apiBase = resolveApiBase(this.creds.domain);
614
- this.state.sequence += 1;
615
- await fetchWithSsrFGuard({
616
- url: `${apiBase}/cardkit/v1/cards/${this.state.cardId}/elements/content/content`,
617
- init: {
618
- method: "PUT",
619
- headers: {
620
- Authorization: `Bearer ${await getToken(this.creds)}`,
621
- "Content-Type": "application/json"
622
- },
623
- body: JSON.stringify({
624
- content: text,
625
- sequence: this.state.sequence,
626
- uuid: `s_${this.state.cardId}_${this.state.sequence}`
627
- })
628
- },
629
- policy: { allowedHostnames: resolveAllowedHostnames(this.creds.domain) },
630
- auditContext: "feishu.streaming-card.update"
631
- }).then(async ({ release }) => {
632
- await release();
633
- }).catch((error) => onError?.(error));
634
- }
635
- async update(text) {
636
- if (!this.state || this.closed) return;
637
- const mergedInput = mergeStreamingText(this.pendingText ?? this.state.currentText, text);
638
- if (!mergedInput || mergedInput === this.state.currentText) return;
639
- const now = Date.now();
640
- if (now - this.lastUpdateTime < this.updateThrottleMs) {
641
- this.pendingText = mergedInput;
642
- return;
643
- }
644
- this.pendingText = null;
645
- this.lastUpdateTime = now;
646
- if (this.flushTimer) {
647
- clearTimeout(this.flushTimer);
648
- this.flushTimer = null;
649
- }
650
- this.queue = this.queue.then(async () => {
651
- if (!this.state || this.closed) return;
652
- const mergedText = mergeStreamingText(this.state.currentText, mergedInput);
653
- if (!mergedText || mergedText === this.state.currentText) return;
654
- this.state.currentText = mergedText;
655
- await this.updateCardContent(mergedText, (e) => this.log?.(`Update failed: ${String(e)}`));
656
- });
657
- await this.queue;
658
- }
659
- async updateNoteContent(note) {
660
- if (!this.state || !this.state.hasNote) return;
661
- const apiBase = resolveApiBase(this.creds.domain);
662
- this.state.sequence += 1;
663
- await fetchWithSsrFGuard({
664
- url: `${apiBase}/cardkit/v1/cards/${this.state.cardId}/elements/note/content`,
665
- init: {
666
- method: "PUT",
667
- headers: {
668
- Authorization: `Bearer ${await getToken(this.creds)}`,
669
- "Content-Type": "application/json"
670
- },
671
- body: JSON.stringify({
672
- content: `<font color='grey'>${note}</font>`,
673
- sequence: this.state.sequence,
674
- uuid: `n_${this.state.cardId}_${this.state.sequence}`
675
- })
676
- },
677
- policy: { allowedHostnames: resolveAllowedHostnames(this.creds.domain) },
678
- auditContext: "feishu.streaming-card.note-update"
679
- }).then(async ({ release }) => {
680
- await release();
681
- }).catch((e) => this.log?.(`Note update failed: ${String(e)}`));
682
- }
683
- async close(finalText, options) {
684
- if (!this.state || this.closed) return;
685
- this.closed = true;
686
- if (this.flushTimer) {
687
- clearTimeout(this.flushTimer);
688
- this.flushTimer = null;
689
- }
690
- await this.queue;
691
- const pendingMerged = mergeStreamingText(this.state.currentText, this.pendingText ?? void 0);
692
- const text = finalText ? mergeStreamingText(pendingMerged, finalText) : pendingMerged;
693
- const apiBase = resolveApiBase(this.creds.domain);
694
- if (text && text !== this.state.currentText) {
695
- await this.updateCardContent(text);
696
- this.state.currentText = text;
697
- }
698
- if (options?.note) await this.updateNoteContent(options.note);
699
- this.state.sequence += 1;
700
- await fetchWithSsrFGuard({
701
- url: `${apiBase}/cardkit/v1/cards/${this.state.cardId}/settings`,
702
- init: {
703
- method: "PATCH",
704
- headers: {
705
- Authorization: `Bearer ${await getToken(this.creds)}`,
706
- "Content-Type": "application/json; charset=utf-8"
707
- },
708
- body: JSON.stringify({
709
- settings: JSON.stringify({ config: {
710
- streaming_mode: false,
711
- summary: { content: truncateSummary(text) }
712
- } }),
713
- sequence: this.state.sequence,
714
- uuid: `c_${this.state.cardId}_${this.state.sequence}`
715
- })
716
- },
717
- policy: { allowedHostnames: resolveAllowedHostnames(this.creds.domain) },
718
- auditContext: "feishu.streaming-card.close"
719
- }).then(async ({ release }) => {
720
- await release();
721
- }).catch((e) => this.log?.(`Close failed: ${String(e)}`));
722
- const finalState = this.state;
723
- this.state = null;
724
- this.pendingText = null;
725
- this.log?.(`Closed streaming: cardId=${finalState.cardId}`);
726
- }
727
- isActive() {
728
- return this.state !== null && !this.closed;
729
- }
730
- };
731
- //#endregion
732
- //#region extensions/feishu/src/typing.ts
733
- const TYPING_EMOJI = "Typing";
734
- /**
735
- * Feishu API error codes that indicate the caller should back off.
736
- * These must propagate to the typing circuit breaker so the keepalive loop
737
- * can trip and stop retrying.
738
- *
739
- * - 99991400: Rate limit (too many requests per second)
740
- * - 99991403: Monthly API call quota exceeded
741
- * - 429: Standard HTTP 429 returned as a Feishu SDK error code
742
- *
743
- * @see https://open.feishu.cn/document/server-docs/api-call-guide/generic-error-code
744
- */
745
- const FEISHU_BACKOFF_CODES = new Set([
746
- 99991400,
747
- 99991403,
748
- 429
749
- ]);
750
- /**
751
- * Custom error class for Feishu backoff conditions detected from non-throwing
752
- * SDK responses. Carries a numeric `.code` so that `isFeishuBackoffError()`
753
- * recognises it when the error is caught downstream.
754
- */
755
- var FeishuBackoffError = class extends Error {
756
- constructor(code) {
757
- super(`Feishu API backoff: code ${code}`);
758
- this.name = "FeishuBackoffError";
759
- this.code = code;
760
- }
761
- };
762
- /**
763
- * Check whether an error represents a rate-limit or quota-exceeded condition
764
- * from the Feishu API that should stop the typing keepalive loop.
765
- *
766
- * Handles two shapes:
767
- * 1. AxiosError with `response.status` and `response.data.code`
768
- * 2. Feishu SDK error with a top-level `code` property
769
- */
770
- function isFeishuBackoffError(err) {
771
- if (typeof err !== "object" || err === null) return false;
772
- const response = err.response;
773
- if (response) {
774
- if (response.status === 429) return true;
775
- if (typeof response.data?.code === "number" && FEISHU_BACKOFF_CODES.has(response.data.code)) return true;
776
- }
777
- const code = err.code;
778
- if (typeof code === "number" && FEISHU_BACKOFF_CODES.has(code)) return true;
779
- return false;
780
- }
781
- /**
782
- * Check whether a Feishu SDK response object contains a backoff error code.
783
- *
784
- * The Feishu SDK sometimes returns a normal response (no throw) with an
785
- * API-level error code in the response body. This must be detected so the
786
- * circuit breaker can trip. See codex review on #28157.
787
- */
788
- function getBackoffCodeFromResponse(response) {
789
- if (typeof response !== "object" || response === null) return;
790
- const code = response.code;
791
- if (typeof code === "number" && FEISHU_BACKOFF_CODES.has(code)) return code;
792
- }
793
- /**
794
- * Add a typing indicator (reaction) to a message.
795
- *
796
- * Rate-limit and quota errors are re-thrown so the circuit breaker in
797
- * `createTypingCallbacks` (typing-start-guard) can trip and stop the
798
- * keepalive loop. See #28062.
799
- *
800
- * Also checks for backoff codes in non-throwing SDK responses (#28157).
801
- */
802
- async function addTypingIndicator(params) {
803
- const { cfg, messageId, accountId, runtime } = params;
804
- const account = resolveFeishuAccount({
805
- cfg,
806
- accountId
807
- });
808
- if (!account.configured) return {
809
- messageId,
810
- reactionId: null
811
- };
812
- const client = createFeishuClient(account);
813
- try {
814
- const response = await client.im.messageReaction.create({
815
- path: { message_id: messageId },
816
- data: { reaction_type: { emoji_type: TYPING_EMOJI } }
817
- });
818
- const backoffCode = getBackoffCodeFromResponse(response);
819
- if (backoffCode !== void 0) {
820
- if (getFeishuRuntime().logging.shouldLogVerbose()) runtime?.log?.(`[feishu] typing indicator response contains backoff code ${backoffCode}, stopping keepalive`);
821
- throw new FeishuBackoffError(backoffCode);
822
- }
823
- return {
824
- messageId,
825
- reactionId: response?.data?.reaction_id ?? null
826
- };
827
- } catch (err) {
828
- if (isFeishuBackoffError(err)) {
829
- if (getFeishuRuntime().logging.shouldLogVerbose()) runtime?.log?.("[feishu] typing indicator hit rate-limit/quota, stopping keepalive");
830
- throw err;
831
- }
832
- if (getFeishuRuntime().logging.shouldLogVerbose()) runtime?.log?.(`[feishu] failed to add typing indicator: ${String(err)}`);
833
- return {
834
- messageId,
835
- reactionId: null
836
- };
837
- }
838
- }
839
- /**
840
- * Remove a typing indicator (reaction) from a message.
841
- *
842
- * Rate-limit and quota errors are re-thrown for the same reason as above.
843
- */
844
- async function removeTypingIndicator(params) {
845
- const { cfg, state, accountId, runtime } = params;
846
- if (!state.reactionId) return;
847
- const account = resolveFeishuAccount({
848
- cfg,
849
- accountId
850
- });
851
- if (!account.configured) return;
852
- const client = createFeishuClient(account);
853
- try {
854
- const backoffCode = getBackoffCodeFromResponse(await client.im.messageReaction.delete({ path: {
855
- message_id: state.messageId,
856
- reaction_id: state.reactionId
857
- } }));
858
- if (backoffCode !== void 0) {
859
- if (getFeishuRuntime().logging.shouldLogVerbose()) runtime?.log?.(`[feishu] typing indicator removal response contains backoff code ${backoffCode}, stopping keepalive`);
860
- throw new FeishuBackoffError(backoffCode);
861
- }
862
- } catch (err) {
863
- if (isFeishuBackoffError(err)) {
864
- if (getFeishuRuntime().logging.shouldLogVerbose()) runtime?.log?.("[feishu] typing indicator removal hit rate-limit/quota, stopping keepalive");
865
- throw err;
866
- }
867
- if (getFeishuRuntime().logging.shouldLogVerbose()) runtime?.log?.(`[feishu] failed to remove typing indicator: ${String(err)}`);
868
- }
869
- }
870
- //#endregion
871
- //#region extensions/feishu/src/reply-dispatcher.ts
872
- /** Detect if text contains markdown elements that benefit from card rendering */
873
- function shouldUseCard(text) {
874
- return /```[\s\S]*?```/.test(text) || /\|.+\|[\r\n]+\|[-:| ]+\|/.test(text);
875
- }
876
- /** Maximum age (ms) for a message to receive a typing indicator reaction.
877
- * Messages older than this are likely replays after context compaction (#30418). */
878
- const TYPING_INDICATOR_MAX_AGE_MS = 2 * 6e4;
879
- const MS_EPOCH_MIN = 0xe8d4a51000;
880
- function normalizeEpochMs(timestamp) {
881
- if (!Number.isFinite(timestamp) || timestamp === void 0 || timestamp <= 0) return;
882
- return timestamp < MS_EPOCH_MIN ? timestamp * 1e3 : timestamp;
883
- }
884
- /** Build a card header from agent identity config. */
885
- function resolveCardHeader(agentId, identity) {
886
- const name = identity?.name?.trim() || agentId;
887
- const emoji = identity?.emoji?.trim();
888
- return {
889
- title: emoji ? `${emoji} ${name}` : name,
890
- template: identity?.theme ?? "blue"
891
- };
892
- }
893
- /** Build a card note footer from agent identity and model context. */
894
- function resolveCardNote(agentId, identity, prefixCtx) {
895
- const parts = [`Agent: ${identity?.name?.trim() || agentId}`];
896
- if (prefixCtx.model) parts.push(`Model: ${prefixCtx.model}`);
897
- if (prefixCtx.provider) parts.push(`Provider: ${prefixCtx.provider}`);
898
- return parts.join(" | ");
899
- }
900
- function createFeishuReplyDispatcher(params) {
901
- const core = getFeishuRuntime();
902
- const { cfg, agentId, chatId, replyToMessageId, skipReplyToInMessages, replyInThread, threadReply, rootId, mentionTargets, accountId, identity } = params;
903
- const sendReplyToMessageId = skipReplyToInMessages ? void 0 : replyToMessageId;
904
- const threadReplyMode = threadReply === true;
905
- const effectiveReplyInThread = threadReplyMode ? true : replyInThread;
906
- const account = resolveFeishuAccount({
907
- cfg,
908
- accountId
909
- });
910
- const prefixContext = createReplyPrefixContext({
911
- cfg,
912
- agentId
913
- });
914
- let typingState = null;
915
- const typingCallbacks = createTypingCallbacks({
916
- start: async () => {
917
- if (!(account.config.typingIndicator ?? true)) return;
918
- if (!replyToMessageId) return;
919
- const messageCreateTimeMs = normalizeEpochMs(params.messageCreateTimeMs);
920
- if (messageCreateTimeMs !== void 0 && Date.now() - messageCreateTimeMs > TYPING_INDICATOR_MAX_AGE_MS) return;
921
- if (typingState?.reactionId) return;
922
- typingState = await addTypingIndicator({
923
- cfg,
924
- messageId: replyToMessageId,
925
- accountId,
926
- runtime: params.runtime
927
- });
928
- },
929
- stop: async () => {
930
- if (!typingState) return;
931
- await removeTypingIndicator({
932
- cfg,
933
- state: typingState,
934
- accountId,
935
- runtime: params.runtime
936
- });
937
- typingState = null;
938
- },
939
- onStartError: (err) => logTypingFailure({
940
- log: (message) => params.runtime.log?.(message),
941
- channel: "feishu",
942
- action: "start",
943
- error: err
944
- }),
945
- onStopError: (err) => logTypingFailure({
946
- log: (message) => params.runtime.log?.(message),
947
- channel: "feishu",
948
- action: "stop",
949
- error: err
950
- })
951
- });
952
- const textChunkLimit = core.channel.text.resolveTextChunkLimit(cfg, "feishu", accountId, { fallbackLimit: 4e3 });
953
- const chunkMode = core.channel.text.resolveChunkMode(cfg, "feishu");
954
- const tableMode = core.channel.text.resolveMarkdownTableMode({
955
- cfg,
956
- channel: "feishu"
957
- });
958
- const renderMode = account.config?.renderMode ?? "auto";
959
- const streamingEnabled = !threadReplyMode && account.config?.streaming !== false && renderMode !== "raw";
960
- let streaming = null;
961
- let streamText = "";
962
- let lastPartial = "";
963
- let reasoningText = "";
964
- const deliveredFinalTexts = /* @__PURE__ */ new Set();
965
- let partialUpdateQueue = Promise.resolve();
966
- let streamingStartPromise = null;
967
- const formatReasoningPrefix = (thinking) => {
968
- if (!thinking) return "";
969
- return `> 💭 **Thinking**\n${thinking.replace(/^Reasoning:\n/, "").replace(/^_(.*)_$/gm, "$1").split("\n").map((line) => `> ${line}`).join("\n")}`;
970
- };
971
- const buildCombinedStreamText = (thinking, answer) => {
972
- const parts = [];
973
- if (thinking) parts.push(formatReasoningPrefix(thinking));
974
- if (thinking && answer) parts.push("\n\n---\n\n");
975
- if (answer) parts.push(answer);
976
- return parts.join("");
977
- };
978
- const flushStreamingCardUpdate = (combined) => {
979
- partialUpdateQueue = partialUpdateQueue.then(async () => {
980
- if (streamingStartPromise) await streamingStartPromise;
981
- if (streaming?.isActive()) await streaming.update(combined);
982
- });
983
- };
984
- const queueStreamingUpdate = (nextText, options) => {
985
- if (!nextText) return;
986
- if (options?.dedupeWithLastPartial && nextText === lastPartial) return;
987
- if (options?.dedupeWithLastPartial) lastPartial = nextText;
988
- streamText = (options?.mode ?? "snapshot") === "delta" ? `${streamText}${nextText}` : mergeStreamingText(streamText, nextText);
989
- flushStreamingCardUpdate(buildCombinedStreamText(reasoningText, streamText));
990
- };
991
- const queueReasoningUpdate = (nextThinking) => {
992
- if (!nextThinking) return;
993
- reasoningText = nextThinking;
994
- flushStreamingCardUpdate(buildCombinedStreamText(reasoningText, streamText));
995
- };
996
- const startStreaming = () => {
997
- if (!streamingEnabled || streamingStartPromise || streaming) return;
998
- streamingStartPromise = (async () => {
999
- const creds = account.appId && account.appSecret ? {
1000
- appId: account.appId,
1001
- appSecret: account.appSecret,
1002
- domain: account.domain
1003
- } : null;
1004
- if (!creds) return;
1005
- streaming = new FeishuStreamingSession(createFeishuClient(account), creds, (message) => params.runtime.log?.(`feishu[${account.accountId}] ${message}`));
1006
- try {
1007
- const cardHeader = resolveCardHeader(agentId, identity);
1008
- const cardNote = resolveCardNote(agentId, identity, prefixContext.prefixContext);
1009
- await streaming.start(chatId, resolveReceiveIdType(chatId), {
1010
- replyToMessageId,
1011
- replyInThread: effectiveReplyInThread,
1012
- rootId,
1013
- header: cardHeader,
1014
- note: cardNote
1015
- });
1016
- } catch (error) {
1017
- params.runtime.error?.(`feishu: streaming start failed: ${String(error)}`);
1018
- streaming = null;
1019
- streamingStartPromise = null;
1020
- }
1021
- })();
1022
- };
1023
- const closeStreaming = async () => {
1024
- if (streamingStartPromise) await streamingStartPromise;
1025
- await partialUpdateQueue;
1026
- if (streaming?.isActive()) {
1027
- let text = buildCombinedStreamText(reasoningText, streamText);
1028
- if (mentionTargets?.length) text = buildMentionedCardContent(mentionTargets, text);
1029
- const finalNote = resolveCardNote(agentId, identity, prefixContext.prefixContext);
1030
- await streaming.close(text, { note: finalNote });
1031
- }
1032
- streaming = null;
1033
- streamingStartPromise = null;
1034
- streamText = "";
1035
- lastPartial = "";
1036
- reasoningText = "";
1037
- };
1038
- const sendChunkedTextReply = async (params) => {
1039
- let first = true;
1040
- const chunkSource = params.useCard ? params.text : core.channel.text.convertMarkdownTables(params.text, tableMode);
1041
- for (const chunk of core.channel.text.chunkTextWithMode(chunkSource, textChunkLimit, chunkMode)) {
1042
- const message = {
1043
- cfg,
1044
- to: chatId,
1045
- text: chunk,
1046
- replyToMessageId: sendReplyToMessageId,
1047
- replyInThread: effectiveReplyInThread,
1048
- mentions: first ? mentionTargets : void 0,
1049
- accountId
1050
- };
1051
- if (params.useCard) await sendMarkdownCardFeishu(message);
1052
- else await sendMessageFeishu(message);
1053
- first = false;
1054
- }
1055
- if (params.infoKind === "final") deliveredFinalTexts.add(params.text);
1056
- };
1057
- const { dispatcher, replyOptions, markDispatchIdle } = core.channel.reply.createReplyDispatcherWithTyping({
1058
- responsePrefix: prefixContext.responsePrefix,
1059
- responsePrefixContextProvider: prefixContext.responsePrefixContextProvider,
1060
- humanDelay: core.channel.reply.resolveHumanDelayConfig(cfg, agentId),
1061
- onReplyStart: () => {
1062
- deliveredFinalTexts.clear();
1063
- if (streamingEnabled && renderMode === "card") startStreaming();
1064
- typingCallbacks.onReplyStart?.();
1065
- },
1066
- deliver: async (payload, info) => {
1067
- const text = payload.text ?? "";
1068
- const mediaList = payload.mediaUrls && payload.mediaUrls.length > 0 ? payload.mediaUrls : payload.mediaUrl ? [payload.mediaUrl] : [];
1069
- const hasText = Boolean(text.trim());
1070
- const hasMedia = mediaList.length > 0;
1071
- const skipTextForDuplicateFinal = info?.kind === "final" && hasText && deliveredFinalTexts.has(text);
1072
- const shouldDeliverText = hasText && !skipTextForDuplicateFinal;
1073
- if (!shouldDeliverText && !hasMedia) return;
1074
- if (shouldDeliverText) {
1075
- const useCard = renderMode === "card" || renderMode === "auto" && shouldUseCard(text);
1076
- let first = true;
1077
- if (info?.kind === "block") {
1078
- if (!(streamingEnabled && useCard)) return;
1079
- startStreaming();
1080
- if (streamingStartPromise) await streamingStartPromise;
1081
- }
1082
- if (info?.kind === "final" && streamingEnabled && useCard) {
1083
- startStreaming();
1084
- if (streamingStartPromise) await streamingStartPromise;
1085
- }
1086
- if (streaming?.isActive()) {
1087
- if (info?.kind === "block") queueStreamingUpdate(text, { mode: "delta" });
1088
- if (info?.kind === "final") {
1089
- streamText = mergeStreamingText(streamText, text);
1090
- await closeStreaming();
1091
- deliveredFinalTexts.add(text);
1092
- }
1093
- if (hasMedia) for (const mediaUrl of mediaList) await sendMediaFeishu({
1094
- cfg,
1095
- to: chatId,
1096
- mediaUrl,
1097
- replyToMessageId: sendReplyToMessageId,
1098
- replyInThread: effectiveReplyInThread,
1099
- accountId
1100
- });
1101
- return;
1102
- }
1103
- if (useCard) {
1104
- const cardHeader = resolveCardHeader(agentId, identity);
1105
- const cardNote = resolveCardNote(agentId, identity, prefixContext.prefixContext);
1106
- for (const chunk of core.channel.text.chunkTextWithMode(text, textChunkLimit, chunkMode)) {
1107
- await sendStructuredCardFeishu({
1108
- cfg,
1109
- to: chatId,
1110
- text: chunk,
1111
- replyToMessageId: sendReplyToMessageId,
1112
- replyInThread: effectiveReplyInThread,
1113
- mentions: first ? mentionTargets : void 0,
1114
- accountId,
1115
- header: cardHeader,
1116
- note: cardNote
1117
- });
1118
- first = false;
1119
- }
1120
- if (info?.kind === "final") deliveredFinalTexts.add(text);
1121
- } else await sendChunkedTextReply({
1122
- text,
1123
- useCard: false,
1124
- infoKind: info?.kind
1125
- });
1126
- }
1127
- if (hasMedia) for (const mediaUrl of mediaList) await sendMediaFeishu({
1128
- cfg,
1129
- to: chatId,
1130
- mediaUrl,
1131
- replyToMessageId: sendReplyToMessageId,
1132
- replyInThread: effectiveReplyInThread,
1133
- accountId
1134
- });
1135
- },
1136
- onError: async (error, info) => {
1137
- params.runtime.error?.(`feishu[${account.accountId}] ${info.kind} reply failed: ${String(error)}`);
1138
- await closeStreaming();
1139
- typingCallbacks.onIdle?.();
1140
- },
1141
- onIdle: async () => {
1142
- await closeStreaming();
1143
- typingCallbacks.onIdle?.();
1144
- },
1145
- onCleanup: () => {
1146
- typingCallbacks.onCleanup?.();
1147
- }
1148
- });
1149
- return {
1150
- dispatcher,
1151
- replyOptions: {
1152
- ...replyOptions,
1153
- onModelSelected: prefixContext.onModelSelected,
1154
- disableBlockStreaming: true,
1155
- onPartialReply: streamingEnabled ? (payload) => {
1156
- if (!payload.text) return;
1157
- queueStreamingUpdate(payload.text, {
1158
- dedupeWithLastPartial: true,
1159
- mode: "snapshot"
1160
- });
1161
- } : void 0,
1162
- onReasoningStream: streamingEnabled ? (payload) => {
1163
- if (!payload.text) return;
1164
- startStreaming();
1165
- queueReasoningUpdate(payload.text);
1166
- } : void 0,
1167
- onReasoningEnd: streamingEnabled ? () => {} : void 0
1168
- },
1169
- markDispatchIdle
1170
- };
1171
- }
1172
- //#endregion
1173
- //#region extensions/feishu/src/bot.ts
1174
- const IGNORED_PERMISSION_SCOPE_TOKENS = ["contact:contact.base:readonly"];
1175
- const FEISHU_SCOPE_CORRECTIONS = { "contact:contact.base:readonly": "contact:user.base:readonly" };
1176
- function correctFeishuScopeInUrl(url) {
1177
- let corrected = url;
1178
- for (const [wrong, right] of Object.entries(FEISHU_SCOPE_CORRECTIONS)) {
1179
- corrected = corrected.replaceAll(encodeURIComponent(wrong), encodeURIComponent(right));
1180
- corrected = corrected.replaceAll(wrong, right);
1181
- }
1182
- return corrected;
1183
- }
1184
- function shouldSuppressPermissionErrorNotice(permissionError) {
1185
- const message = permissionError.message.toLowerCase();
1186
- return IGNORED_PERMISSION_SCOPE_TOKENS.some((token) => message.includes(token));
1187
- }
1188
- function extractPermissionError(err) {
1189
- if (!err || typeof err !== "object") return null;
1190
- const data = err.response?.data;
1191
- if (!data || typeof data !== "object") return null;
1192
- const feishuErr = data;
1193
- if (feishuErr.code !== 99991672) return null;
1194
- const msg = feishuErr.msg ?? "";
1195
- const urlMatch = msg.match(/https:\/\/[^\s,]+\/app\/[^\s,]+/);
1196
- const grantUrl = urlMatch?.[0] ? correctFeishuScopeInUrl(urlMatch[0]) : void 0;
1197
- return {
1198
- code: feishuErr.code,
1199
- message: msg,
1200
- grantUrl
1201
- };
1202
- }
1203
- const SENDER_NAME_TTL_MS = 600 * 1e3;
1204
- const senderNameCache = /* @__PURE__ */ new Map();
1205
- const permissionErrorNotifiedAt = /* @__PURE__ */ new Map();
1206
- const PERMISSION_ERROR_COOLDOWN_MS = 300 * 1e3;
1207
- function resolveSenderLookupIdType(senderId) {
1208
- const trimmed = senderId.trim();
1209
- if (trimmed.startsWith("ou_")) return "open_id";
1210
- if (trimmed.startsWith("on_")) return "union_id";
1211
- return "user_id";
1212
- }
1213
- async function resolveFeishuSenderName(params) {
1214
- const { account, senderId, log } = params;
1215
- if (!account.configured) return {};
1216
- const normalizedSenderId = senderId.trim();
1217
- if (!normalizedSenderId) return {};
1218
- const cached = senderNameCache.get(normalizedSenderId);
1219
- const now = Date.now();
1220
- if (cached && cached.expireAt > now) return { name: cached.name };
1221
- try {
1222
- const client = createFeishuClient(account);
1223
- const userIdType = resolveSenderLookupIdType(normalizedSenderId);
1224
- const res = await client.contact.user.get({
1225
- path: { user_id: normalizedSenderId },
1226
- params: { user_id_type: userIdType }
1227
- });
1228
- const name = res?.data?.user?.name || res?.data?.user?.display_name || res?.data?.user?.nickname || res?.data?.user?.en_name;
1229
- if (name && typeof name === "string") {
1230
- senderNameCache.set(normalizedSenderId, {
1231
- name,
1232
- expireAt: now + SENDER_NAME_TTL_MS
1233
- });
1234
- return { name };
1235
- }
1236
- return {};
1237
- } catch (err) {
1238
- const permErr = extractPermissionError(err);
1239
- if (permErr) {
1240
- if (shouldSuppressPermissionErrorNotice(permErr)) {
1241
- log(`feishu: ignoring stale permission scope error: ${permErr.message}`);
1242
- return {};
1243
- }
1244
- log(`feishu: permission error resolving sender name: code=${permErr.code}`);
1245
- return { permissionError: permErr };
1246
- }
1247
- log(`feishu: failed to resolve sender name for ${normalizedSenderId}: ${String(err)}`);
1248
- return {};
1249
- }
1250
- }
1251
- function resolveFeishuGroupSession(params) {
1252
- const { chatId, senderOpenId, messageId, rootId, threadId, groupConfig, feishuCfg } = params;
1253
- const normalizedThreadId = threadId?.trim();
1254
- const normalizedRootId = rootId?.trim();
1255
- const threadReply = Boolean(normalizedThreadId || normalizedRootId);
1256
- const replyInThread = (groupConfig?.replyInThread ?? feishuCfg?.replyInThread ?? "disabled") === "enabled" || threadReply;
1257
- const legacyTopicSessionMode = groupConfig?.topicSessionMode ?? feishuCfg?.topicSessionMode ?? "disabled";
1258
- const groupSessionScope = groupConfig?.groupSessionScope ?? feishuCfg?.groupSessionScope ?? (legacyTopicSessionMode === "enabled" ? "group_topic" : "group");
1259
- const topicScope = groupSessionScope === "group_topic" || groupSessionScope === "group_topic_sender" ? normalizedRootId ?? normalizedThreadId ?? (replyInThread ? messageId : null) : null;
1260
- let peerId = chatId;
1261
- switch (groupSessionScope) {
1262
- case "group_sender":
1263
- peerId = buildFeishuConversationId({
1264
- chatId,
1265
- scope: "group_sender",
1266
- senderOpenId
1267
- });
1268
- break;
1269
- case "group_topic":
1270
- peerId = topicScope ? buildFeishuConversationId({
1271
- chatId,
1272
- scope: "group_topic",
1273
- topicId: topicScope
1274
- }) : chatId;
1275
- break;
1276
- case "group_topic_sender":
1277
- peerId = topicScope ? buildFeishuConversationId({
1278
- chatId,
1279
- scope: "group_topic_sender",
1280
- topicId: topicScope,
1281
- senderOpenId
1282
- }) : buildFeishuConversationId({
1283
- chatId,
1284
- scope: "group_sender",
1285
- senderOpenId
1286
- });
1287
- break;
1288
- default:
1289
- peerId = chatId;
1290
- break;
1291
- }
1292
- return {
1293
- peerId,
1294
- parentPeer: topicScope && (groupSessionScope === "group_topic" || groupSessionScope === "group_topic_sender") ? {
1295
- kind: "group",
1296
- id: chatId
1297
- } : null,
1298
- groupSessionScope,
1299
- replyInThread,
1300
- threadReply
1301
- };
1302
- }
1303
- function parseMessageContent(content, messageType) {
1304
- if (messageType === "post") {
1305
- const { textContent } = parsePostContent(content);
1306
- return textContent;
1307
- }
1308
- try {
1309
- const parsed = JSON.parse(content);
1310
- if (messageType === "text") return parsed.text || "";
1311
- if (messageType === "share_chat") {
1312
- if (parsed && typeof parsed === "object") {
1313
- const share = parsed;
1314
- if (typeof share.body === "string" && share.body.trim().length > 0) return share.body.trim();
1315
- if (typeof share.summary === "string" && share.summary.trim().length > 0) return share.summary.trim();
1316
- if (typeof share.share_chat_id === "string" && share.share_chat_id.trim().length > 0) return `[Forwarded message: ${share.share_chat_id.trim()}]`;
1317
- }
1318
- return "[Forwarded message]";
1319
- }
1320
- if (messageType === "merge_forward") return "[Merged and Forwarded Message - loading...]";
1321
- return content;
1322
- } catch {
1323
- return content;
1324
- }
1325
- }
1326
- /**
1327
- * Parse merge_forward message content and fetch sub-messages.
1328
- * Returns formatted text content of all sub-messages.
1329
- */
1330
- function parseMergeForwardContent(params) {
1331
- const { content, log } = params;
1332
- const maxMessages = 50;
1333
- log?.(`feishu: parsing merge_forward sub-messages from API response`);
1334
- let items;
1335
- try {
1336
- items = JSON.parse(content);
1337
- } catch {
1338
- log?.(`feishu: merge_forward items parse failed`);
1339
- return "[Merged and Forwarded Message - parse error]";
1340
- }
1341
- if (!Array.isArray(items) || items.length === 0) return "[Merged and Forwarded Message - no sub-messages]";
1342
- const subMessages = items.filter((item) => item.upper_message_id);
1343
- if (subMessages.length === 0) return "[Merged and Forwarded Message - no sub-messages found]";
1344
- log?.(`feishu: merge_forward contains ${subMessages.length} sub-messages`);
1345
- subMessages.sort((a, b) => {
1346
- return parseInt(a.create_time || "0", 10) - parseInt(b.create_time || "0", 10);
1347
- });
1348
- const lines = ["[Merged and Forwarded Messages]"];
1349
- const limitedMessages = subMessages.slice(0, maxMessages);
1350
- for (const item of limitedMessages) {
1351
- const formatted = formatSubMessageContent(item.body?.content || "", item.msg_type || "text");
1352
- lines.push(`- ${formatted}`);
1353
- }
1354
- if (subMessages.length > maxMessages) lines.push(`... and ${subMessages.length - maxMessages} more messages`);
1355
- return lines.join("\n");
1356
- }
1357
- /**
1358
- * Format sub-message content based on message type.
1359
- */
1360
- function formatSubMessageContent(content, contentType) {
1361
- try {
1362
- const parsed = JSON.parse(content);
1363
- switch (contentType) {
1364
- case "text": return parsed.text || content;
1365
- case "post": {
1366
- const { textContent } = parsePostContent(content);
1367
- return textContent;
1368
- }
1369
- case "image": return "[Image]";
1370
- case "file": return `[File: ${parsed.file_name || "unknown"}]`;
1371
- case "audio": return "[Audio]";
1372
- case "video": return "[Video]";
1373
- case "sticker": return "[Sticker]";
1374
- case "merge_forward": return "[Nested Merged Forward]";
1375
- default: return `[${contentType}]`;
1376
- }
1377
- } catch {
1378
- return content;
1379
- }
1380
- }
1381
- function checkBotMentioned(event, botOpenId) {
1382
- if (!botOpenId) return false;
1383
- if ((event.message.content ?? "").includes("@_all")) return true;
1384
- const mentions = event.message.mentions ?? [];
1385
- if (mentions.length > 0) return mentions.some((m) => m.id.open_id === botOpenId);
1386
- if (event.message.message_type === "post") {
1387
- const { mentionedOpenIds } = parsePostContent(event.message.content);
1388
- return mentionedOpenIds.some((id) => id === botOpenId);
1389
- }
1390
- return false;
1391
- }
1392
- function normalizeMentions(text, mentions, botStripId) {
1393
- if (!mentions || mentions.length === 0) return text;
1394
- const escaped = (value) => value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
1395
- const escapeName = (value) => value.replace(/</g, "&lt;").replace(/>/g, "&gt;");
1396
- let result = text;
1397
- for (const mention of mentions) {
1398
- const mentionId = mention.id.open_id;
1399
- const replacement = botStripId && mentionId === botStripId ? "" : mentionId ? `<at user_id="${mentionId}">${escapeName(mention.name)}</at>` : `@${mention.name}`;
1400
- result = result.replace(new RegExp(escaped(mention.key), "g"), () => replacement).trim();
1401
- }
1402
- return result;
1403
- }
1404
- function normalizeFeishuCommandProbeBody(text) {
1405
- if (!text) return "";
1406
- return text.replace(/<at\b[^>]*>[^<]*<\/at>/giu, " ").replace(/(^|\s)@[^/\s]+(?=\s|$|\/)/gu, "$1").replace(/\s+/g, " ").trim();
1407
- }
1408
- /**
1409
- * Parse media keys from message content based on message type.
1410
- */
1411
- function parseMediaKeys(content, messageType) {
1412
- try {
1413
- const parsed = JSON.parse(content);
1414
- const imageKey = normalizeFeishuExternalKey(parsed.image_key);
1415
- const fileKey = normalizeFeishuExternalKey(parsed.file_key);
1416
- switch (messageType) {
1417
- case "image": return {
1418
- imageKey,
1419
- fileName: parsed.file_name
1420
- };
1421
- case "file": return {
1422
- fileKey,
1423
- fileName: parsed.file_name
1424
- };
1425
- case "audio": return {
1426
- fileKey,
1427
- fileName: parsed.file_name
1428
- };
1429
- case "video":
1430
- case "media": return {
1431
- fileKey,
1432
- imageKey,
1433
- fileName: parsed.file_name
1434
- };
1435
- case "sticker": return {
1436
- fileKey,
1437
- fileName: parsed.file_name
1438
- };
1439
- default: return {};
1440
- }
1441
- } catch {
1442
- return {};
1443
- }
1444
- }
1445
- /**
1446
- * Map Feishu message type to messageResource.get resource type.
1447
- * Feishu messageResource API supports only: image | file.
1448
- */
1449
- function toMessageResourceType(messageType) {
1450
- return messageType === "image" ? "image" : "file";
1451
- }
1452
- /**
1453
- * Infer placeholder text based on message type.
1454
- */
1455
- function inferPlaceholder(messageType) {
1456
- switch (messageType) {
1457
- case "image": return "<media:image>";
1458
- case "file": return "<media:document>";
1459
- case "audio": return "<media:audio>";
1460
- case "video":
1461
- case "media": return "<media:video>";
1462
- case "sticker": return "<media:sticker>";
1463
- default: return "<media:document>";
1464
- }
1465
- }
1466
- /**
1467
- * Resolve media from a Feishu message, downloading and saving to disk.
1468
- * Similar to Discord's resolveMediaList().
1469
- */
1470
- async function resolveFeishuMediaList(params) {
1471
- const { cfg, messageId, messageType, content, maxBytes, log, accountId } = params;
1472
- if (![
1473
- "image",
1474
- "file",
1475
- "audio",
1476
- "video",
1477
- "media",
1478
- "sticker",
1479
- "post"
1480
- ].includes(messageType)) return [];
1481
- const out = [];
1482
- const core = getFeishuRuntime();
1483
- if (messageType === "post") {
1484
- const { imageKeys, mediaKeys: postMediaKeys } = parsePostContent(content);
1485
- if (imageKeys.length === 0 && postMediaKeys.length === 0) return [];
1486
- if (imageKeys.length > 0) log?.(`feishu: post message contains ${imageKeys.length} embedded image(s)`);
1487
- if (postMediaKeys.length > 0) log?.(`feishu: post message contains ${postMediaKeys.length} embedded media file(s)`);
1488
- for (const imageKey of imageKeys) try {
1489
- const result = await downloadMessageResourceFeishu({
1490
- cfg,
1491
- messageId,
1492
- fileKey: imageKey,
1493
- type: "image",
1494
- accountId
1495
- });
1496
- let contentType = result.contentType;
1497
- if (!contentType) contentType = await core.media.detectMime({ buffer: result.buffer });
1498
- const saved = await core.channel.media.saveMediaBuffer(result.buffer, contentType, "inbound", maxBytes);
1499
- out.push({
1500
- path: saved.path,
1501
- contentType: saved.contentType,
1502
- placeholder: "<media:image>"
1503
- });
1504
- log?.(`feishu: downloaded embedded image ${imageKey}, saved to ${saved.path}`);
1505
- } catch (err) {
1506
- log?.(`feishu: failed to download embedded image ${imageKey}: ${String(err)}`);
1507
- }
1508
- for (const media of postMediaKeys) try {
1509
- const result = await downloadMessageResourceFeishu({
1510
- cfg,
1511
- messageId,
1512
- fileKey: media.fileKey,
1513
- type: "file",
1514
- accountId
1515
- });
1516
- let contentType = result.contentType;
1517
- if (!contentType) contentType = await core.media.detectMime({ buffer: result.buffer });
1518
- const saved = await core.channel.media.saveMediaBuffer(result.buffer, contentType, "inbound", maxBytes);
1519
- out.push({
1520
- path: saved.path,
1521
- contentType: saved.contentType,
1522
- placeholder: "<media:video>"
1523
- });
1524
- log?.(`feishu: downloaded embedded media ${media.fileKey}, saved to ${saved.path}`);
1525
- } catch (err) {
1526
- log?.(`feishu: failed to download embedded media ${media.fileKey}: ${String(err)}`);
1527
- }
1528
- return out;
1529
- }
1530
- const mediaKeys = parseMediaKeys(content, messageType);
1531
- if (!mediaKeys.imageKey && !mediaKeys.fileKey) return [];
1532
- try {
1533
- let buffer;
1534
- let contentType;
1535
- let fileName;
1536
- const fileKey = mediaKeys.fileKey || mediaKeys.imageKey;
1537
- if (!fileKey) return [];
1538
- const result = await downloadMessageResourceFeishu({
1539
- cfg,
1540
- messageId,
1541
- fileKey,
1542
- type: toMessageResourceType(messageType),
1543
- accountId
1544
- });
1545
- buffer = result.buffer;
1546
- contentType = result.contentType;
1547
- fileName = result.fileName || mediaKeys.fileName;
1548
- if (!contentType) contentType = await core.media.detectMime({ buffer });
1549
- const saved = await core.channel.media.saveMediaBuffer(buffer, contentType, "inbound", maxBytes, fileName);
1550
- out.push({
1551
- path: saved.path,
1552
- contentType: saved.contentType,
1553
- placeholder: inferPlaceholder(messageType)
1554
- });
1555
- log?.(`feishu: downloaded ${messageType} media, saved to ${saved.path}`);
1556
- } catch (err) {
1557
- log?.(`feishu: failed to download ${messageType} media: ${String(err)}`);
1558
- }
1559
- return out;
1560
- }
1561
- function resolveBroadcastAgents(cfg, peerId) {
1562
- const broadcast = cfg.broadcast;
1563
- if (!broadcast || typeof broadcast !== "object") return null;
1564
- const agents = broadcast[peerId];
1565
- if (!Array.isArray(agents) || agents.length === 0) return null;
1566
- return agents;
1567
- }
1568
- function buildBroadcastSessionKey(baseSessionKey, originalAgentId, targetAgentId) {
1569
- const prefix = `agent:${originalAgentId}:`;
1570
- if (baseSessionKey.startsWith(prefix)) return `agent:${targetAgentId}:${baseSessionKey.slice(prefix.length)}`;
1571
- return baseSessionKey;
1572
- }
1573
- /**
1574
- * Build media payload for inbound context.
1575
- * Similar to Discord's buildDiscordMediaPayload().
1576
- */
1577
- function parseFeishuMessageEvent(event, botOpenId, _botName) {
1578
- const rawContent = parseMessageContent(event.message.content, event.message.message_type);
1579
- const mentionedBot = checkBotMentioned(event, botOpenId);
1580
- const hasAnyMention = (event.message.mentions?.length ?? 0) > 0;
1581
- const content = normalizeMentions(rawContent, event.message.mentions, botOpenId);
1582
- const senderOpenId = event.sender.sender_id.open_id?.trim();
1583
- const senderUserId = event.sender.sender_id.user_id?.trim();
1584
- const senderFallbackId = senderOpenId || senderUserId || "";
1585
- const ctx = {
1586
- chatId: event.message.chat_id,
1587
- messageId: event.message.message_id,
1588
- senderId: senderUserId || senderOpenId || "",
1589
- senderOpenId: senderFallbackId,
1590
- chatType: event.message.chat_type,
1591
- mentionedBot,
1592
- hasAnyMention,
1593
- rootId: event.message.root_id || void 0,
1594
- parentId: event.message.parent_id || void 0,
1595
- threadId: event.message.thread_id || void 0,
1596
- content,
1597
- contentType: event.message.message_type
1598
- };
1599
- if (isMentionForwardRequest(event, botOpenId)) {
1600
- const mentionTargets = extractMentionTargets(event, botOpenId);
1601
- if (mentionTargets.length > 0) ctx.mentionTargets = mentionTargets;
1602
- }
1603
- return ctx;
1604
- }
1605
- function buildFeishuAgentBody(params) {
1606
- const { ctx, quotedContent, permissionErrorForAgent, botOpenId } = params;
1607
- let messageBody = ctx.content;
1608
- if (quotedContent) messageBody = `[Replying to: "${quotedContent}"]\n\n${ctx.content}`;
1609
- messageBody = `${ctx.senderName ?? ctx.senderOpenId}: ${messageBody}`;
1610
- if (ctx.hasAnyMention) {
1611
- const botIdHint = botOpenId?.trim();
1612
- messageBody += "\n\n[System: The content may include mention tags in the form <at user_id=\"...\">name</at>. Treat these as real mentions of Feishu entities (users or bots).]";
1613
- if (botIdHint) messageBody += `\n[System: If user_id is "${botIdHint}", that mention refers to you.]`;
1614
- }
1615
- if (ctx.mentionTargets && ctx.mentionTargets.length > 0) {
1616
- const targetNames = ctx.mentionTargets.map((t) => t.name).join(", ");
1617
- messageBody += `\n\n[System: Your reply will automatically @mention: ${targetNames}. Do not write @xxx yourself.]`;
1618
- }
1619
- messageBody = `[message_id: ${ctx.messageId}]\n${messageBody}`;
1620
- if (permissionErrorForAgent) {
1621
- const grantUrl = permissionErrorForAgent.grantUrl ?? "";
1622
- messageBody += `\n\n[System: The bot encountered a Feishu API permission error. Please inform the user about this issue and provide the permission grant URL for the admin to authorize. Permission grant URL: ${grantUrl}]`;
1623
- }
1624
- return messageBody;
1625
- }
1626
- async function handleFeishuMessage(params) {
1627
- const { cfg, event, botOpenId, botName, runtime, chatHistories, accountId, processingClaimHeld = false } = params;
1628
- const account = resolveFeishuAccount({
1629
- cfg,
1630
- accountId
1631
- });
1632
- const feishuCfg = account.config;
1633
- const log = runtime?.log ?? console.log;
1634
- const error = runtime?.error ?? console.error;
1635
- const messageId = event.message.message_id;
1636
- if (!await finalizeFeishuMessageProcessing({
1637
- messageId,
1638
- namespace: account.accountId,
1639
- log,
1640
- claimHeld: processingClaimHeld
1641
- })) {
1642
- log(`feishu: skipping duplicate message ${messageId}`);
1643
- return;
1644
- }
1645
- let ctx = parseFeishuMessageEvent(event, botOpenId, botName);
1646
- const isGroup = ctx.chatType === "group";
1647
- const isDirect = !isGroup;
1648
- const senderUserId = event.sender.sender_id.user_id?.trim() || void 0;
1649
- if (event.message.message_type === "merge_forward") {
1650
- log(`feishu[${account.accountId}]: processing merge_forward message, fetching full content via API`);
1651
- try {
1652
- const response = await createFeishuClient(account).im.message.get({ path: { message_id: event.message.message_id } });
1653
- if (response.code === 0 && response.data?.items && response.data.items.length > 0) {
1654
- log(`feishu[${account.accountId}]: merge_forward API returned ${response.data.items.length} items`);
1655
- const expandedContent = parseMergeForwardContent({
1656
- content: JSON.stringify(response.data.items),
1657
- log
1658
- });
1659
- ctx = {
1660
- ...ctx,
1661
- content: expandedContent
1662
- };
1663
- } else {
1664
- log(`feishu[${account.accountId}]: merge_forward API returned no items`);
1665
- ctx = {
1666
- ...ctx,
1667
- content: "[Merged and Forwarded Message - could not fetch]"
1668
- };
1669
- }
1670
- } catch (err) {
1671
- log(`feishu[${account.accountId}]: merge_forward fetch failed: ${String(err)}`);
1672
- ctx = {
1673
- ...ctx,
1674
- content: "[Merged and Forwarded Message - fetch error]"
1675
- };
1676
- }
1677
- }
1678
- let permissionErrorForAgent;
1679
- if (feishuCfg?.resolveSenderNames ?? true) {
1680
- const senderResult = await resolveFeishuSenderName({
1681
- account,
1682
- senderId: ctx.senderOpenId,
1683
- log
1684
- });
1685
- if (senderResult.name) ctx = {
1686
- ...ctx,
1687
- senderName: senderResult.name
1688
- };
1689
- if (senderResult.permissionError) {
1690
- const appKey = account.appId ?? "default";
1691
- const now = Date.now();
1692
- if (now - (permissionErrorNotifiedAt.get(appKey) ?? 0) > PERMISSION_ERROR_COOLDOWN_MS) {
1693
- permissionErrorNotifiedAt.set(appKey, now);
1694
- permissionErrorForAgent = senderResult.permissionError;
1695
- }
1696
- }
1697
- }
1698
- log(`feishu[${account.accountId}]: received message from ${ctx.senderOpenId} in ${ctx.chatId} (${ctx.chatType})`);
1699
- if (ctx.mentionTargets && ctx.mentionTargets.length > 0) {
1700
- const names = ctx.mentionTargets.map((t) => t.name).join(", ");
1701
- log(`feishu[${account.accountId}]: detected @ forward request, targets: [${names}]`);
1702
- }
1703
- const historyLimit = Math.max(0, feishuCfg?.historyLimit ?? cfg.messages?.groupChat?.historyLimit ?? 50);
1704
- const groupConfig = isGroup ? resolveFeishuGroupConfig({
1705
- cfg: feishuCfg,
1706
- groupId: ctx.chatId
1707
- }) : void 0;
1708
- const groupSession = isGroup ? resolveFeishuGroupSession({
1709
- chatId: ctx.chatId,
1710
- senderOpenId: ctx.senderOpenId,
1711
- messageId: ctx.messageId,
1712
- rootId: ctx.rootId,
1713
- threadId: ctx.threadId,
1714
- groupConfig,
1715
- feishuCfg
1716
- }) : null;
1717
- const groupHistoryKey = isGroup ? groupSession?.peerId ?? ctx.chatId : void 0;
1718
- const dmPolicy = feishuCfg?.dmPolicy ?? "pairing";
1719
- const configAllowFrom = feishuCfg?.allowFrom ?? [];
1720
- const useAccessGroups = cfg.commands?.useAccessGroups !== false;
1721
- const rawBroadcastAgents = isGroup ? resolveBroadcastAgents(cfg, ctx.chatId) : null;
1722
- const broadcastAgents = rawBroadcastAgents ? [...new Set(rawBroadcastAgents.map((id) => normalizeAgentId(id)))] : null;
1723
- let requireMention = false;
1724
- if (isGroup) {
1725
- if (groupConfig?.enabled === false) {
1726
- log(`feishu[${account.accountId}]: group ${ctx.chatId} is disabled`);
1727
- return;
1728
- }
1729
- const defaultGroupPolicy = resolveDefaultGroupPolicy(cfg);
1730
- const { groupPolicy, providerMissingFallbackApplied } = resolveOpenProviderRuntimeGroupPolicy({
1731
- providerConfigPresent: cfg.channels?.feishu !== void 0,
1732
- groupPolicy: feishuCfg?.groupPolicy,
1733
- defaultGroupPolicy
1734
- });
1735
- warnMissingProviderGroupPolicyFallbackOnce({
1736
- providerMissingFallbackApplied,
1737
- providerKey: "feishu",
1738
- accountId: account.accountId,
1739
- log
1740
- });
1741
- if (!isFeishuGroupAllowed({
1742
- groupPolicy,
1743
- allowFrom: feishuCfg?.groupAllowFrom ?? [],
1744
- senderId: ctx.chatId,
1745
- senderName: void 0
1746
- })) {
1747
- log(`feishu[${account.accountId}]: group ${ctx.chatId} not in groupAllowFrom (groupPolicy=${groupPolicy})`);
1748
- return;
1749
- }
1750
- const perGroupSenderAllowFrom = groupConfig?.allowFrom ?? [];
1751
- const globalSenderAllowFrom = feishuCfg?.groupSenderAllowFrom ?? [];
1752
- const effectiveSenderAllowFrom = perGroupSenderAllowFrom.length > 0 ? perGroupSenderAllowFrom : globalSenderAllowFrom;
1753
- if (effectiveSenderAllowFrom.length > 0) {
1754
- if (!isFeishuGroupAllowed({
1755
- groupPolicy: "allowlist",
1756
- allowFrom: effectiveSenderAllowFrom,
1757
- senderId: ctx.senderOpenId,
1758
- senderIds: [senderUserId],
1759
- senderName: ctx.senderName
1760
- })) {
1761
- log(`feishu: sender ${ctx.senderOpenId} not in group ${ctx.chatId} sender allowlist`);
1762
- return;
1763
- }
1764
- }
1765
- ({requireMention} = resolveFeishuReplyPolicy({
1766
- isDirectMessage: false,
1767
- globalConfig: feishuCfg,
1768
- groupConfig
1769
- }));
1770
- if (requireMention && !ctx.mentionedBot) {
1771
- log(`feishu[${account.accountId}]: message in group ${ctx.chatId} did not mention bot`);
1772
- if (!broadcastAgents && chatHistories && groupHistoryKey) recordPendingHistoryEntryIfEnabled({
1773
- historyMap: chatHistories,
1774
- historyKey: groupHistoryKey,
1775
- limit: historyLimit,
1776
- entry: {
1777
- sender: ctx.senderOpenId,
1778
- body: `${ctx.senderName ?? ctx.senderOpenId}: ${ctx.content}`,
1779
- timestamp: Date.now(),
1780
- messageId: ctx.messageId
1781
- }
1782
- });
1783
- return;
1784
- }
1785
- }
1786
- try {
1787
- const core = getFeishuRuntime();
1788
- const pairing = createScopedPairingAccess({
1789
- core,
1790
- channel: "feishu",
1791
- accountId: account.accountId
1792
- });
1793
- const commandProbeBody = isGroup ? normalizeFeishuCommandProbeBody(ctx.content) : ctx.content;
1794
- const shouldComputeCommandAuthorized = core.channel.commands.shouldComputeCommandAuthorized(commandProbeBody, cfg);
1795
- const storeAllowFrom = !isGroup && dmPolicy !== "allowlist" && (dmPolicy !== "open" || shouldComputeCommandAuthorized) ? await pairing.readAllowFromStore().catch(() => []) : [];
1796
- const effectiveDmAllowFrom = [...configAllowFrom, ...storeAllowFrom];
1797
- const dmAllowed = resolveFeishuAllowlistMatch({
1798
- allowFrom: effectiveDmAllowFrom,
1799
- senderId: ctx.senderOpenId,
1800
- senderIds: [senderUserId],
1801
- senderName: ctx.senderName
1802
- }).allowed;
1803
- if (isDirect && dmPolicy !== "open" && !dmAllowed) {
1804
- if (dmPolicy === "pairing") await issuePairingChallenge({
1805
- channel: "feishu",
1806
- senderId: ctx.senderOpenId,
1807
- senderIdLine: `Your Feishu user id: ${ctx.senderOpenId}`,
1808
- meta: { name: ctx.senderName },
1809
- upsertPairingRequest: pairing.upsertPairingRequest,
1810
- onCreated: () => {
1811
- log(`feishu[${account.accountId}]: pairing request sender=${ctx.senderOpenId}`);
1812
- },
1813
- sendPairingReply: async (text) => {
1814
- await sendMessageFeishu({
1815
- cfg,
1816
- to: `chat:${ctx.chatId}`,
1817
- text,
1818
- accountId: account.accountId
1819
- });
1820
- },
1821
- onReplyError: (err) => {
1822
- log(`feishu[${account.accountId}]: pairing reply failed for ${ctx.senderOpenId}: ${String(err)}`);
1823
- }
1824
- });
1825
- else log(`feishu[${account.accountId}]: blocked unauthorized sender ${ctx.senderOpenId} (dmPolicy=${dmPolicy})`);
1826
- return;
1827
- }
1828
- const commandAllowFrom = isGroup ? groupConfig?.allowFrom ?? configAllowFrom : effectiveDmAllowFrom;
1829
- const senderAllowedForCommands = resolveFeishuAllowlistMatch({
1830
- allowFrom: commandAllowFrom,
1831
- senderId: ctx.senderOpenId,
1832
- senderIds: [senderUserId],
1833
- senderName: ctx.senderName
1834
- }).allowed;
1835
- const commandAuthorized = shouldComputeCommandAuthorized ? core.channel.commands.resolveCommandAuthorizedFromAuthorizers({
1836
- useAccessGroups,
1837
- authorizers: [{
1838
- configured: commandAllowFrom.length > 0,
1839
- allowed: senderAllowedForCommands
1840
- }]
1841
- }) : void 0;
1842
- const feishuFrom = `feishu:${ctx.senderOpenId}`;
1843
- const feishuTo = isGroup ? `chat:${ctx.chatId}` : `user:${ctx.senderOpenId}`;
1844
- const peerId = isGroup ? groupSession?.peerId ?? ctx.chatId : ctx.senderOpenId;
1845
- const parentPeer = isGroup ? groupSession?.parentPeer ?? null : null;
1846
- const replyInThread = isGroup ? groupSession?.replyInThread ?? false : false;
1847
- const feishuAcpConversationSupported = !isGroup || groupSession?.groupSessionScope === "group_topic" || groupSession?.groupSessionScope === "group_topic_sender";
1848
- if (isGroup && groupSession) log(`feishu[${account.accountId}]: group session scope=${groupSession.groupSessionScope}, peer=${peerId}`);
1849
- let route = core.channel.routing.resolveAgentRoute({
1850
- cfg,
1851
- channel: "feishu",
1852
- accountId: account.accountId,
1853
- peer: {
1854
- kind: isGroup ? "group" : "direct",
1855
- id: peerId
1856
- },
1857
- parentPeer
1858
- });
1859
- let effectiveCfg = cfg;
1860
- if (!isGroup && route.matchedBy === "default") {
1861
- const dynamicCfg = feishuCfg?.dynamicAgentCreation;
1862
- if (dynamicCfg?.enabled) {
1863
- const result = await maybeCreateDynamicAgent({
1864
- cfg,
1865
- runtime: getFeishuRuntime(),
1866
- senderOpenId: ctx.senderOpenId,
1867
- dynamicCfg,
1868
- log: (msg) => log(msg)
1869
- });
1870
- if (result.created) {
1871
- effectiveCfg = result.updatedCfg;
1872
- route = core.channel.routing.resolveAgentRoute({
1873
- cfg: result.updatedCfg,
1874
- channel: "feishu",
1875
- accountId: account.accountId,
1876
- peer: {
1877
- kind: "direct",
1878
- id: ctx.senderOpenId
1879
- }
1880
- });
1881
- log(`feishu[${account.accountId}]: dynamic agent created, new route: ${route.sessionKey}`);
1882
- }
1883
- }
1884
- }
1885
- const currentConversationId = peerId;
1886
- const parentConversationId = isGroup ? parentPeer?.id ?? ctx.chatId : void 0;
1887
- let configuredBinding = null;
1888
- if (feishuAcpConversationSupported) {
1889
- const configuredRoute = resolveConfiguredAcpRoute({
1890
- cfg: effectiveCfg,
1891
- route,
1892
- channel: "feishu",
1893
- accountId: account.accountId,
1894
- conversationId: currentConversationId,
1895
- parentConversationId
1896
- });
1897
- configuredBinding = configuredRoute.configuredBinding;
1898
- route = configuredRoute.route;
1899
- const threadBinding = getSessionBindingService().resolveByConversation({
1900
- channel: "feishu",
1901
- accountId: account.accountId,
1902
- conversationId: currentConversationId,
1903
- ...parentConversationId ? { parentConversationId } : {}
1904
- });
1905
- const boundSessionKey = threadBinding?.targetSessionKey?.trim();
1906
- if (threadBinding && boundSessionKey) {
1907
- route = {
1908
- ...route,
1909
- sessionKey: boundSessionKey,
1910
- agentId: resolveAgentIdFromSessionKey(boundSessionKey) || route.agentId,
1911
- lastRoutePolicy: deriveLastRoutePolicy({
1912
- sessionKey: boundSessionKey,
1913
- mainSessionKey: route.mainSessionKey
1914
- }),
1915
- matchedBy: "binding.channel"
1916
- };
1917
- configuredBinding = null;
1918
- getSessionBindingService().touch(threadBinding.bindingId);
1919
- log(`feishu[${account.accountId}]: routed via bound conversation ${currentConversationId} -> ${boundSessionKey}`);
1920
- }
1921
- }
1922
- if (configuredBinding) {
1923
- const ensured = await ensureConfiguredAcpRouteReady({
1924
- cfg: effectiveCfg,
1925
- configuredBinding
1926
- });
1927
- if (!ensured.ok) {
1928
- const replyTargetMessageId = isGroup && (groupSession?.groupSessionScope === "group_topic" || groupSession?.groupSessionScope === "group_topic_sender") ? ctx.rootId ?? ctx.messageId : ctx.messageId;
1929
- await sendMessageFeishu({
1930
- cfg: effectiveCfg,
1931
- to: `chat:${ctx.chatId}`,
1932
- text: `⚠️ Failed to initialize the configured ACP session for this Feishu conversation: ${ensured.error}`,
1933
- replyToMessageId: replyTargetMessageId,
1934
- replyInThread: isGroup ? groupSession?.replyInThread ?? false : false,
1935
- accountId: account.accountId
1936
- }).catch((err) => {
1937
- log(`feishu[${account.accountId}]: failed to send ACP init error reply: ${String(err)}`);
1938
- });
1939
- return;
1940
- }
1941
- }
1942
- const preview = ctx.content.replace(/\s+/g, " ").slice(0, 160);
1943
- const inboundLabel = isGroup ? `Feishu[${account.accountId}] message in group ${ctx.chatId}` : `Feishu[${account.accountId}] DM from ${ctx.senderOpenId}`;
1944
- log(`feishu[${account.accountId}]: ${inboundLabel}: ${preview}`);
1945
- const mediaMaxBytes = (feishuCfg?.mediaMaxMb ?? 30) * 1024 * 1024;
1946
- const mediaPayload = buildAgentMediaPayload(await resolveFeishuMediaList({
1947
- cfg,
1948
- messageId: ctx.messageId,
1949
- messageType: event.message.message_type,
1950
- content: event.message.content,
1951
- maxBytes: mediaMaxBytes,
1952
- log,
1953
- accountId: account.accountId
1954
- }));
1955
- let quotedMessageInfo = null;
1956
- let quotedContent;
1957
- if (ctx.parentId) try {
1958
- quotedMessageInfo = await getMessageFeishu({
1959
- cfg,
1960
- messageId: ctx.parentId,
1961
- accountId: account.accountId
1962
- });
1963
- if (quotedMessageInfo) {
1964
- quotedContent = quotedMessageInfo.content;
1965
- log(`feishu[${account.accountId}]: fetched quoted message: ${quotedContent?.slice(0, 100)}`);
1966
- }
1967
- } catch (err) {
1968
- log(`feishu[${account.accountId}]: failed to fetch quoted message: ${String(err)}`);
1969
- }
1970
- const isTopicSessionForThread = isGroup && (groupSession?.groupSessionScope === "group_topic" || groupSession?.groupSessionScope === "group_topic_sender");
1971
- const envelopeOptions = core.channel.reply.resolveEnvelopeFormatOptions(cfg);
1972
- const messageBody = buildFeishuAgentBody({
1973
- ctx,
1974
- quotedContent,
1975
- permissionErrorForAgent,
1976
- botOpenId
1977
- });
1978
- const envelopeFrom = isGroup ? `${ctx.chatId}:${ctx.senderOpenId}` : ctx.senderOpenId;
1979
- if (permissionErrorForAgent) log(`feishu[${account.accountId}]: appending permission error notice to message body`);
1980
- let combinedBody = core.channel.reply.formatAgentEnvelope({
1981
- channel: "Feishu",
1982
- from: envelopeFrom,
1983
- timestamp: /* @__PURE__ */ new Date(),
1984
- envelope: envelopeOptions,
1985
- body: messageBody
1986
- });
1987
- const historyKey = groupHistoryKey;
1988
- if (isGroup && historyKey && chatHistories) combinedBody = buildPendingHistoryContextFromMap({
1989
- historyMap: chatHistories,
1990
- historyKey,
1991
- limit: historyLimit,
1992
- currentMessage: combinedBody,
1993
- formatEntry: (entry) => core.channel.reply.formatAgentEnvelope({
1994
- channel: "Feishu",
1995
- from: `${ctx.chatId}:${entry.sender}`,
1996
- timestamp: entry.timestamp,
1997
- body: entry.body,
1998
- envelope: envelopeOptions
1999
- })
2000
- });
2001
- const inboundHistory = isGroup && historyKey && historyLimit > 0 && chatHistories ? (chatHistories.get(historyKey) ?? []).map((entry) => ({
2002
- sender: entry.sender,
2003
- body: entry.body,
2004
- timestamp: entry.timestamp
2005
- })) : void 0;
2006
- const threadContextBySessionKey = /* @__PURE__ */ new Map();
2007
- let rootMessageInfo;
2008
- let rootMessageFetched = false;
2009
- const getRootMessageInfo = async () => {
2010
- if (!ctx.rootId) return null;
2011
- if (!rootMessageFetched) {
2012
- rootMessageFetched = true;
2013
- if (ctx.rootId === ctx.parentId && quotedMessageInfo) rootMessageInfo = quotedMessageInfo;
2014
- else try {
2015
- rootMessageInfo = await getMessageFeishu({
2016
- cfg,
2017
- messageId: ctx.rootId,
2018
- accountId: account.accountId
2019
- });
2020
- } catch (err) {
2021
- log(`feishu[${account.accountId}]: failed to fetch root message: ${String(err)}`);
2022
- rootMessageInfo = null;
2023
- }
2024
- }
2025
- return rootMessageInfo ?? null;
2026
- };
2027
- const resolveThreadContextForAgent = async (agentId, agentSessionKey) => {
2028
- const cached = threadContextBySessionKey.get(agentSessionKey);
2029
- if (cached) return cached;
2030
- const threadContext = { threadLabel: (ctx.rootId || ctx.threadId) && isTopicSessionForThread ? `Feishu thread in ${ctx.chatId}` : void 0 };
2031
- if (!(ctx.rootId || ctx.threadId) || !isTopicSessionForThread) {
2032
- threadContextBySessionKey.set(agentSessionKey, threadContext);
2033
- return threadContext;
2034
- }
2035
- const storePath = core.channel.session.resolveStorePath(cfg.session?.store, { agentId });
2036
- if (core.channel.session.readSessionUpdatedAt({
2037
- storePath,
2038
- sessionKey: agentSessionKey
2039
- })) {
2040
- log(`feishu[${account.accountId}]: skipping thread bootstrap for existing session ${agentSessionKey}`);
2041
- threadContextBySessionKey.set(agentSessionKey, threadContext);
2042
- return threadContext;
2043
- }
2044
- const rootMsg = await getRootMessageInfo();
2045
- let feishuThreadId = ctx.threadId ?? rootMsg?.threadId;
2046
- if (feishuThreadId) log(`feishu[${account.accountId}]: resolved thread ID: ${feishuThreadId}`);
2047
- if (!feishuThreadId) {
2048
- log(`feishu[${account.accountId}]: no threadId found for root message ${ctx.rootId ?? "none"}, skipping thread history`);
2049
- threadContextBySessionKey.set(agentSessionKey, threadContext);
2050
- return threadContext;
2051
- }
2052
- try {
2053
- const threadMessages = await listFeishuThreadMessages({
2054
- cfg,
2055
- threadId: feishuThreadId,
2056
- currentMessageId: ctx.messageId,
2057
- rootMessageId: ctx.rootId,
2058
- limit: 20,
2059
- accountId: account.accountId
2060
- });
2061
- const senderScoped = groupSession?.groupSessionScope === "group_topic_sender";
2062
- const senderIds = new Set([ctx.senderOpenId, senderUserId].map((id) => id?.trim()).filter((id) => id !== void 0 && id.length > 0));
2063
- const relevantMessages = (senderScoped ? threadMessages.filter((msg) => msg.senderType === "app" || msg.senderId !== void 0 && senderIds.has(msg.senderId.trim())) : threadMessages) ?? [];
2064
- const threadStarterBody = rootMsg?.content ?? relevantMessages[0]?.content;
2065
- const historyMessages = Boolean(rootMsg?.content || ctx.rootId) ? relevantMessages : relevantMessages.slice(1);
2066
- const historyParts = historyMessages.map((msg) => {
2067
- const role = msg.senderType === "app" ? "assistant" : "user";
2068
- return core.channel.reply.formatAgentEnvelope({
2069
- channel: "Feishu",
2070
- from: `${msg.senderId ?? "Unknown"} (${role})`,
2071
- timestamp: msg.createTime,
2072
- body: msg.content,
2073
- envelope: envelopeOptions
2074
- });
2075
- });
2076
- threadContext.threadStarterBody = threadStarterBody;
2077
- threadContext.threadHistoryBody = historyParts.length > 0 ? historyParts.join("\n\n") : void 0;
2078
- log(`feishu[${account.accountId}]: populated thread bootstrap with starter=${threadStarterBody ? "yes" : "no"} history=${historyMessages.length}`);
2079
- } catch (err) {
2080
- log(`feishu[${account.accountId}]: failed to fetch thread history: ${String(err)}`);
2081
- }
2082
- threadContextBySessionKey.set(agentSessionKey, threadContext);
2083
- return threadContext;
2084
- };
2085
- const buildCtxPayloadForAgent = async (agentId, agentSessionKey, agentAccountId, wasMentioned) => {
2086
- const threadContext = await resolveThreadContextForAgent(agentId, agentSessionKey);
2087
- return core.channel.reply.finalizeInboundContext({
2088
- Body: combinedBody,
2089
- BodyForAgent: messageBody,
2090
- InboundHistory: inboundHistory,
2091
- ReplyToId: ctx.parentId,
2092
- RootMessageId: ctx.rootId,
2093
- RawBody: ctx.content,
2094
- CommandBody: ctx.content,
2095
- From: feishuFrom,
2096
- To: feishuTo,
2097
- SessionKey: agentSessionKey,
2098
- AccountId: agentAccountId,
2099
- ChatType: isGroup ? "group" : "direct",
2100
- GroupSubject: isGroup ? ctx.chatId : void 0,
2101
- SenderName: ctx.senderName ?? ctx.senderOpenId,
2102
- SenderId: ctx.senderOpenId,
2103
- Provider: "feishu",
2104
- Surface: "feishu",
2105
- MessageSid: ctx.messageId,
2106
- ReplyToBody: quotedContent ?? void 0,
2107
- ThreadStarterBody: threadContext.threadStarterBody,
2108
- ThreadHistoryBody: threadContext.threadHistoryBody,
2109
- ThreadLabel: threadContext.threadLabel,
2110
- MessageThreadId: ctx.rootId && isTopicSessionForThread ? ctx.rootId : void 0,
2111
- Timestamp: Date.now(),
2112
- WasMentioned: wasMentioned,
2113
- CommandAuthorized: commandAuthorized,
2114
- OriginatingChannel: "feishu",
2115
- OriginatingTo: feishuTo,
2116
- GroupSystemPrompt: isGroup ? groupConfig?.systemPrompt?.trim() || void 0 : void 0,
2117
- ...mediaPayload
2118
- });
2119
- };
2120
- const messageCreateTimeMs = event.message.create_time ? parseInt(event.message.create_time, 10) : void 0;
2121
- const isTopicSession = isGroup && (groupSession?.groupSessionScope === "group_topic" || groupSession?.groupSessionScope === "group_topic_sender");
2122
- const configReplyInThread = isGroup && (groupConfig?.replyInThread ?? feishuCfg?.replyInThread ?? "disabled") === "enabled";
2123
- const replyTargetMessageId = isTopicSession || configReplyInThread ? ctx.rootId ?? ctx.messageId : ctx.messageId;
2124
- const threadReply = isGroup ? groupSession?.threadReply ?? false : false;
2125
- if (broadcastAgents) {
2126
- if (!await tryRecordMessagePersistent(ctx.messageId, "broadcast", log)) {
2127
- log(`feishu[${account.accountId}]: broadcast already claimed by another account for message ${ctx.messageId}; skipping`);
2128
- return;
2129
- }
2130
- const strategy = cfg.broadcast?.strategy || "parallel";
2131
- const activeAgentId = ctx.mentionedBot || !requireMention ? normalizeAgentId(route.agentId) : null;
2132
- const agentIds = (cfg.agents?.list ?? []).map((a) => normalizeAgentId(a.id));
2133
- const hasKnownAgents = agentIds.length > 0;
2134
- log(`feishu[${account.accountId}]: broadcasting to ${broadcastAgents.length} agents (strategy=${strategy}, active=${activeAgentId ?? "none"})`);
2135
- const dispatchForAgent = async (agentId) => {
2136
- if (hasKnownAgents && !agentIds.includes(normalizeAgentId(agentId))) {
2137
- log(`feishu[${account.accountId}]: broadcast agent ${agentId} not found in agents.list; skipping`);
2138
- return;
2139
- }
2140
- const agentSessionKey = buildBroadcastSessionKey(route.sessionKey, route.agentId, agentId);
2141
- const agentCtx = await buildCtxPayloadForAgent(agentId, agentSessionKey, route.accountId, ctx.mentionedBot && agentId === activeAgentId);
2142
- if (agentId === activeAgentId) {
2143
- const identity = resolveAgentOutboundIdentity(cfg, agentId);
2144
- const { dispatcher, replyOptions, markDispatchIdle } = createFeishuReplyDispatcher({
2145
- cfg,
2146
- agentId,
2147
- runtime,
2148
- chatId: ctx.chatId,
2149
- replyToMessageId: replyTargetMessageId,
2150
- skipReplyToInMessages: !isGroup,
2151
- replyInThread,
2152
- rootId: ctx.rootId,
2153
- threadReply,
2154
- mentionTargets: ctx.mentionTargets,
2155
- accountId: account.accountId,
2156
- identity,
2157
- messageCreateTimeMs
2158
- });
2159
- log(`feishu[${account.accountId}]: broadcast active dispatch agent=${agentId} (session=${agentSessionKey})`);
2160
- await core.channel.reply.withReplyDispatcher({
2161
- dispatcher,
2162
- onSettled: () => markDispatchIdle(),
2163
- run: () => core.channel.reply.dispatchReplyFromConfig({
2164
- ctx: agentCtx,
2165
- cfg,
2166
- dispatcher,
2167
- replyOptions
2168
- })
2169
- });
2170
- } else {
2171
- delete agentCtx.CommandAuthorized;
2172
- const noopDispatcher = {
2173
- sendToolResult: () => false,
2174
- sendBlockReply: () => false,
2175
- sendFinalReply: () => false,
2176
- waitForIdle: async () => {},
2177
- getQueuedCounts: () => ({
2178
- tool: 0,
2179
- block: 0,
2180
- final: 0
2181
- }),
2182
- markComplete: () => {}
2183
- };
2184
- log(`feishu[${account.accountId}]: broadcast observer dispatch agent=${agentId} (session=${agentSessionKey})`);
2185
- await core.channel.reply.withReplyDispatcher({
2186
- dispatcher: noopDispatcher,
2187
- run: () => core.channel.reply.dispatchReplyFromConfig({
2188
- ctx: agentCtx,
2189
- cfg,
2190
- dispatcher: noopDispatcher
2191
- })
2192
- });
2193
- }
2194
- };
2195
- if (strategy === "sequential") for (const agentId of broadcastAgents) try {
2196
- await dispatchForAgent(agentId);
2197
- } catch (err) {
2198
- log(`feishu[${account.accountId}]: broadcast dispatch failed for agent=${agentId}: ${String(err)}`);
2199
- }
2200
- else {
2201
- const results = await Promise.allSettled(broadcastAgents.map(dispatchForAgent));
2202
- for (let i = 0; i < results.length; i++) if (results[i].status === "rejected") log(`feishu[${account.accountId}]: broadcast dispatch failed for agent=${broadcastAgents[i]}: ${String(results[i].reason)}`);
2203
- }
2204
- if (isGroup && historyKey && chatHistories) clearHistoryEntriesIfEnabled({
2205
- historyMap: chatHistories,
2206
- historyKey,
2207
- limit: historyLimit
2208
- });
2209
- log(`feishu[${account.accountId}]: broadcast dispatch complete for ${broadcastAgents.length} agents`);
2210
- } else {
2211
- const ctxPayload = await buildCtxPayloadForAgent(route.agentId, route.sessionKey, route.accountId, ctx.mentionedBot);
2212
- const identity = resolveAgentOutboundIdentity(cfg, route.agentId);
2213
- const { dispatcher, replyOptions, markDispatchIdle } = createFeishuReplyDispatcher({
2214
- cfg,
2215
- agentId: route.agentId,
2216
- runtime,
2217
- chatId: ctx.chatId,
2218
- replyToMessageId: replyTargetMessageId,
2219
- skipReplyToInMessages: !isGroup,
2220
- replyInThread,
2221
- rootId: ctx.rootId,
2222
- threadReply,
2223
- mentionTargets: ctx.mentionTargets,
2224
- accountId: account.accountId,
2225
- identity,
2226
- messageCreateTimeMs
2227
- });
2228
- log(`feishu[${account.accountId}]: dispatching to agent (session=${route.sessionKey})`);
2229
- const { queuedFinal, counts } = await core.channel.reply.withReplyDispatcher({
2230
- dispatcher,
2231
- onSettled: () => {
2232
- markDispatchIdle();
2233
- },
2234
- run: () => core.channel.reply.dispatchReplyFromConfig({
2235
- ctx: ctxPayload,
2236
- cfg,
2237
- dispatcher,
2238
- replyOptions
2239
- })
2240
- });
2241
- if (isGroup && historyKey && chatHistories) clearHistoryEntriesIfEnabled({
2242
- historyMap: chatHistories,
2243
- historyKey,
2244
- limit: historyLimit
2245
- });
2246
- log(`feishu[${account.accountId}]: dispatch complete (queuedFinal=${queuedFinal}, replies=${counts.final})`);
2247
- }
2248
- } catch (err) {
2249
- error(`feishu[${account.accountId}]: failed to dispatch message: ${String(err)}`);
2250
- }
2251
- }
2252
- //#endregion
2253
- //#region extensions/feishu/src/card-interaction.ts
2254
- const FEISHU_CARD_INTERACTION_VERSION = "ocf1";
2255
- function isRecord(value) {
2256
- return typeof value === "object" && value !== null;
2257
- }
2258
- function isInteractionKind(value) {
2259
- return value === "button" || value === "quick" || value === "meta";
2260
- }
2261
- function isMetadataValue(value) {
2262
- return value === null || value === void 0 || typeof value === "string" || typeof value === "number" || typeof value === "boolean";
2263
- }
2264
- function createFeishuCardInteractionEnvelope(envelope) {
2265
- return {
2266
- oc: FEISHU_CARD_INTERACTION_VERSION,
2267
- ...envelope
2268
- };
2269
- }
2270
- function buildFeishuCardActionTextFallback(event) {
2271
- const actionValue = event.action.value;
2272
- if (isRecord(actionValue)) {
2273
- if (typeof actionValue.text === "string") return actionValue.text;
2274
- if (typeof actionValue.command === "string") return actionValue.command;
2275
- return JSON.stringify(actionValue);
2276
- }
2277
- return String(actionValue);
2278
- }
2279
- function decodeFeishuCardAction(params) {
2280
- const { event, now = Date.now() } = params;
2281
- const actionValue = event.action.value;
2282
- if (!isRecord(actionValue) || actionValue.oc !== "ocf1") return {
2283
- kind: "legacy",
2284
- text: buildFeishuCardActionTextFallback(event)
2285
- };
2286
- if (!isInteractionKind(actionValue.k) || typeof actionValue.a !== "string" || !actionValue.a) return {
2287
- kind: "invalid",
2288
- reason: "malformed"
2289
- };
2290
- if (actionValue.q !== void 0 && typeof actionValue.q !== "string") return {
2291
- kind: "invalid",
2292
- reason: "malformed"
2293
- };
2294
- if (actionValue.m !== void 0) {
2295
- if (!isRecord(actionValue.m)) return {
2296
- kind: "invalid",
2297
- reason: "malformed"
2298
- };
2299
- for (const value of Object.values(actionValue.m)) if (!isMetadataValue(value)) return {
2300
- kind: "invalid",
2301
- reason: "malformed"
2302
- };
2303
- }
2304
- if (actionValue.c !== void 0) {
2305
- if (!isRecord(actionValue.c)) return {
2306
- kind: "invalid",
2307
- reason: "malformed"
2308
- };
2309
- if (actionValue.c.u !== void 0 && typeof actionValue.c.u !== "string") return {
2310
- kind: "invalid",
2311
- reason: "malformed"
2312
- };
2313
- if (actionValue.c.h !== void 0 && typeof actionValue.c.h !== "string") return {
2314
- kind: "invalid",
2315
- reason: "malformed"
2316
- };
2317
- if (actionValue.c.s !== void 0 && typeof actionValue.c.s !== "string") return {
2318
- kind: "invalid",
2319
- reason: "malformed"
2320
- };
2321
- if (actionValue.c.e !== void 0 && !Number.isFinite(actionValue.c.e)) return {
2322
- kind: "invalid",
2323
- reason: "malformed"
2324
- };
2325
- if (actionValue.c.t !== void 0 && actionValue.c.t !== "p2p" && actionValue.c.t !== "group") return {
2326
- kind: "invalid",
2327
- reason: "malformed"
2328
- };
2329
- if (typeof actionValue.c.e === "number" && actionValue.c.e < now) return {
2330
- kind: "invalid",
2331
- reason: "stale"
2332
- };
2333
- const expectedUser = actionValue.c.u?.trim();
2334
- if (expectedUser && expectedUser !== (event.operator.open_id ?? "").trim()) return {
2335
- kind: "invalid",
2336
- reason: "wrong_user"
2337
- };
2338
- const expectedChat = actionValue.c.h?.trim();
2339
- if (expectedChat && expectedChat !== (event.context.chat_id ?? "").trim()) return {
2340
- kind: "invalid",
2341
- reason: "wrong_conversation"
2342
- };
2343
- }
2344
- return {
2345
- kind: "structured",
2346
- envelope: actionValue
2347
- };
2348
- }
2349
- //#endregion
2350
- //#region extensions/feishu/src/card-ux-shared.ts
2351
- function buildFeishuCardButton(params) {
2352
- return {
2353
- tag: "button",
2354
- text: {
2355
- tag: "plain_text",
2356
- content: params.label
2357
- },
2358
- type: params.type ?? "default",
2359
- value: params.value
2360
- };
2361
- }
2362
- function buildFeishuCardInteractionContext(params) {
2363
- return {
2364
- u: params.operatorOpenId,
2365
- ...params.chatId ? { h: params.chatId } : {},
2366
- ...params.sessionKey ? { s: params.sessionKey } : {},
2367
- e: params.expiresAt,
2368
- ...params.chatType ? { t: params.chatType } : {}
2369
- };
2370
- }
2371
- //#endregion
2372
- //#region extensions/feishu/src/card-ux-approval.ts
2373
- const FEISHU_APPROVAL_REQUEST_ACTION = "feishu.quick_actions.request_approval";
2374
- const FEISHU_APPROVAL_CONFIRM_ACTION = "feishu.approval.confirm";
2375
- const FEISHU_APPROVAL_CANCEL_ACTION = "feishu.approval.cancel";
2376
- function createApprovalCard(params) {
2377
- const context = buildFeishuCardInteractionContext(params);
2378
- return {
2379
- schema: "2.0",
2380
- config: { wide_screen_mode: true },
2381
- header: {
2382
- title: {
2383
- tag: "plain_text",
2384
- content: "Confirm action"
2385
- },
2386
- template: "orange"
2387
- },
2388
- body: { elements: [{
2389
- tag: "markdown",
2390
- content: params.prompt
2391
- }, {
2392
- tag: "action",
2393
- actions: [buildFeishuCardButton({
2394
- label: params.confirmLabel ?? "Confirm",
2395
- type: "primary",
2396
- value: createFeishuCardInteractionEnvelope({
2397
- k: "quick",
2398
- a: FEISHU_APPROVAL_CONFIRM_ACTION,
2399
- q: params.command,
2400
- c: context
2401
- })
2402
- }), buildFeishuCardButton({
2403
- label: params.cancelLabel ?? "Cancel",
2404
- value: createFeishuCardInteractionEnvelope({
2405
- k: "button",
2406
- a: FEISHU_APPROVAL_CANCEL_ACTION,
2407
- c: context
2408
- })
2409
- })]
2410
- }] }
2411
- };
2412
- }
2413
- //#endregion
2414
- //#region extensions/feishu/src/card-action.ts
2415
- const FEISHU_APPROVAL_CARD_TTL_MS = 5 * 6e4;
2416
- const FEISHU_CARD_ACTION_TOKEN_TTL_MS = 15 * 6e4;
2417
- const processedCardActionTokens = /* @__PURE__ */ new Map();
2418
- function pruneProcessedCardActionTokens(now) {
2419
- for (const [key, entry] of processedCardActionTokens.entries()) if (entry.expiresAt <= now) processedCardActionTokens.delete(key);
2420
- }
2421
- function beginFeishuCardActionToken(params) {
2422
- const now = params.now ?? Date.now();
2423
- pruneProcessedCardActionTokens(now);
2424
- const normalizedToken = params.token.trim();
2425
- if (!normalizedToken) return true;
2426
- const key = `${params.accountId}:${normalizedToken}`;
2427
- const existing = processedCardActionTokens.get(key);
2428
- if (existing && existing.expiresAt > now) return false;
2429
- processedCardActionTokens.set(key, {
2430
- status: "inflight",
2431
- expiresAt: now + FEISHU_CARD_ACTION_TOKEN_TTL_MS
2432
- });
2433
- return true;
2434
- }
2435
- function completeFeishuCardActionToken(params) {
2436
- const now = params.now ?? Date.now();
2437
- const normalizedToken = params.token.trim();
2438
- if (!normalizedToken) return;
2439
- processedCardActionTokens.set(`${params.accountId}:${normalizedToken}`, {
2440
- status: "completed",
2441
- expiresAt: now + FEISHU_CARD_ACTION_TOKEN_TTL_MS
2442
- });
2443
- }
2444
- function releaseFeishuCardActionToken(params) {
2445
- const normalizedToken = params.token.trim();
2446
- if (!normalizedToken) return;
2447
- processedCardActionTokens.delete(`${params.accountId}:${normalizedToken}`);
2448
- }
2449
- function buildSyntheticMessageEvent(event, content, chatType) {
2450
- return {
2451
- sender: { sender_id: {
2452
- open_id: event.operator.open_id,
2453
- user_id: event.operator.user_id,
2454
- union_id: event.operator.union_id
2455
- } },
2456
- message: {
2457
- message_id: `card-action-${event.token}`,
2458
- chat_id: event.context.chat_id || event.operator.open_id,
2459
- chat_type: chatType ?? (event.context.chat_id ? "group" : "p2p"),
2460
- message_type: "text",
2461
- content: JSON.stringify({ text: content })
2462
- }
2463
- };
2464
- }
2465
- function resolveCallbackTarget(event) {
2466
- const chatId = event.context.chat_id?.trim();
2467
- if (chatId) return `chat:${chatId}`;
2468
- return `user:${event.operator.open_id}`;
2469
- }
2470
- async function dispatchSyntheticCommand(params) {
2471
- await handleFeishuMessage({
2472
- cfg: params.cfg,
2473
- event: buildSyntheticMessageEvent(params.event, params.command, params.chatType),
2474
- botOpenId: params.botOpenId,
2475
- runtime: params.runtime,
2476
- accountId: params.accountId
2477
- });
2478
- }
2479
- async function sendInvalidInteractionNotice(params) {
2480
- const reasonText = params.reason === "stale" ? "This card action has expired. Open a fresh launcher card and try again." : params.reason === "wrong_user" ? "This card action belongs to a different user." : params.reason === "wrong_conversation" ? "This card action belongs to a different conversation." : "This card action payload is invalid.";
2481
- await sendMessageFeishu({
2482
- cfg: params.cfg,
2483
- to: resolveCallbackTarget(params.event),
2484
- text: `⚠️ ${reasonText}`,
2485
- accountId: params.accountId
2486
- });
2487
- }
2488
- async function handleFeishuCardAction(params) {
2489
- const { cfg, event, runtime, accountId } = params;
2490
- const account = resolveFeishuAccount({
2491
- cfg,
2492
- accountId
2493
- });
2494
- const log = runtime?.log ?? console.log;
2495
- const decoded = decodeFeishuCardAction({ event });
2496
- if (!beginFeishuCardActionToken({
2497
- token: event.token,
2498
- accountId: account.accountId
2499
- })) {
2500
- log(`feishu[${account.accountId}]: skipping duplicate card action token ${event.token}`);
2501
- return;
2502
- }
2503
- try {
2504
- if (decoded.kind === "invalid") {
2505
- log(`feishu[${account.accountId}]: rejected card action from ${event.operator.open_id}: ${decoded.reason}`);
2506
- await sendInvalidInteractionNotice({
2507
- cfg,
2508
- event,
2509
- reason: decoded.reason,
2510
- accountId
2511
- });
2512
- completeFeishuCardActionToken({
2513
- token: event.token,
2514
- accountId: account.accountId
2515
- });
2516
- return;
2517
- }
2518
- if (decoded.kind === "structured") {
2519
- const { envelope } = decoded;
2520
- log(`feishu[${account.accountId}]: handling structured card action ${envelope.a} from ${event.operator.open_id}`);
2521
- if (envelope.a === "feishu.quick_actions.request_approval") {
2522
- const command = typeof envelope.m?.command === "string" ? envelope.m.command.trim() : "";
2523
- if (!command) {
2524
- await sendInvalidInteractionNotice({
2525
- cfg,
2526
- event,
2527
- reason: "malformed",
2528
- accountId
2529
- });
2530
- completeFeishuCardActionToken({
2531
- token: event.token,
2532
- accountId: account.accountId
2533
- });
2534
- return;
2535
- }
2536
- const prompt = typeof envelope.m?.prompt === "string" && envelope.m.prompt.trim() ? envelope.m.prompt : `Run \`${command}\` in this Feishu conversation?`;
2537
- await sendCardFeishu({
2538
- cfg,
2539
- to: resolveCallbackTarget(event),
2540
- card: createApprovalCard({
2541
- operatorOpenId: event.operator.open_id,
2542
- chatId: event.context.chat_id || void 0,
2543
- command,
2544
- prompt,
2545
- sessionKey: envelope.c?.s,
2546
- expiresAt: Date.now() + FEISHU_APPROVAL_CARD_TTL_MS,
2547
- chatType: envelope.c?.t ?? (event.context.chat_id ? "group" : "p2p"),
2548
- confirmLabel: command === "/reset" ? "Reset" : "Confirm"
2549
- }),
2550
- accountId
2551
- });
2552
- completeFeishuCardActionToken({
2553
- token: event.token,
2554
- accountId: account.accountId
2555
- });
2556
- return;
2557
- }
2558
- if (envelope.a === "feishu.approval.cancel") {
2559
- await sendMessageFeishu({
2560
- cfg,
2561
- to: resolveCallbackTarget(event),
2562
- text: "Cancelled.",
2563
- accountId
2564
- });
2565
- completeFeishuCardActionToken({
2566
- token: event.token,
2567
- accountId: account.accountId
2568
- });
2569
- return;
2570
- }
2571
- if (envelope.a === "feishu.approval.confirm" || envelope.k === "quick") {
2572
- const command = envelope.q?.trim();
2573
- if (!command) {
2574
- await sendInvalidInteractionNotice({
2575
- cfg,
2576
- event,
2577
- reason: "malformed",
2578
- accountId
2579
- });
2580
- completeFeishuCardActionToken({
2581
- token: event.token,
2582
- accountId: account.accountId
2583
- });
2584
- return;
2585
- }
2586
- await dispatchSyntheticCommand({
2587
- cfg,
2588
- event,
2589
- command,
2590
- botOpenId: params.botOpenId,
2591
- runtime,
2592
- accountId,
2593
- chatType: envelope.c?.t ?? (event.context.chat_id ? "group" : "p2p")
2594
- });
2595
- completeFeishuCardActionToken({
2596
- token: event.token,
2597
- accountId: account.accountId
2598
- });
2599
- return;
2600
- }
2601
- await sendInvalidInteractionNotice({
2602
- cfg,
2603
- event,
2604
- reason: "malformed",
2605
- accountId
2606
- });
2607
- completeFeishuCardActionToken({
2608
- token: event.token,
2609
- accountId: account.accountId
2610
- });
2611
- return;
2612
- }
2613
- const content = buildFeishuCardActionTextFallback(event);
2614
- log(`feishu[${account.accountId}]: handling card action from ${event.operator.open_id}: ${content}`);
2615
- await dispatchSyntheticCommand({
2616
- cfg,
2617
- event,
2618
- command: content,
2619
- botOpenId: params.botOpenId,
2620
- runtime,
2621
- accountId
2622
- });
2623
- completeFeishuCardActionToken({
2624
- token: event.token,
2625
- accountId: account.accountId
2626
- });
2627
- } catch (err) {
2628
- releaseFeishuCardActionToken({
2629
- token: event.token,
2630
- accountId: account.accountId
2631
- });
2632
- throw err;
2633
- }
2634
- }
2635
- //#endregion
2636
- //#region extensions/feishu/src/card-ux-launcher.ts
2637
- const FEISHU_QUICK_ACTION_CARD_TTL_MS = 10 * 6e4;
2638
- const QUICK_ACTION_MENU_KEYS = new Set([
2639
- "quick-actions",
2640
- "quick_actions",
2641
- "launcher"
2642
- ]);
2643
- function isFeishuQuickActionMenuEventKey(eventKey) {
2644
- return QUICK_ACTION_MENU_KEYS.has(eventKey.trim().toLowerCase());
2645
- }
2646
- function createQuickActionLauncherCard(params) {
2647
- const context = buildFeishuCardInteractionContext(params);
2648
- return {
2649
- schema: "2.0",
2650
- config: { wide_screen_mode: true },
2651
- header: {
2652
- title: {
2653
- tag: "plain_text",
2654
- content: "Quick actions"
2655
- },
2656
- template: "indigo"
2657
- },
2658
- body: { elements: [{
2659
- tag: "markdown",
2660
- content: "Run common actions without typing raw commands."
2661
- }, {
2662
- tag: "action",
2663
- actions: [
2664
- buildFeishuCardButton({
2665
- label: "Help",
2666
- value: createFeishuCardInteractionEnvelope({
2667
- k: "quick",
2668
- a: "feishu.quick_actions.help",
2669
- q: "/help",
2670
- c: context
2671
- })
2672
- }),
2673
- buildFeishuCardButton({
2674
- label: "New session",
2675
- type: "primary",
2676
- value: createFeishuCardInteractionEnvelope({
2677
- k: "meta",
2678
- a: FEISHU_APPROVAL_REQUEST_ACTION,
2679
- m: {
2680
- command: "/new",
2681
- prompt: "Start a fresh session? This will reset the current chat context."
2682
- },
2683
- c: context
2684
- })
2685
- }),
2686
- buildFeishuCardButton({
2687
- label: "Reset",
2688
- type: "danger",
2689
- value: createFeishuCardInteractionEnvelope({
2690
- k: "meta",
2691
- a: FEISHU_APPROVAL_REQUEST_ACTION,
2692
- m: {
2693
- command: "/reset",
2694
- prompt: "Reset this session now? Any active conversation state will be cleared."
2695
- },
2696
- c: context
2697
- })
2698
- })
2699
- ]
2700
- }] }
2701
- };
2702
- }
2703
- async function maybeHandleFeishuQuickActionMenu(params) {
2704
- if (!isFeishuQuickActionMenuEventKey(params.eventKey)) return false;
2705
- const expiresAt = (params.now ?? Date.now()) + FEISHU_QUICK_ACTION_CARD_TTL_MS;
2706
- try {
2707
- await sendCardFeishu({
2708
- cfg: params.cfg,
2709
- to: `user:${params.operatorOpenId}`,
2710
- card: createQuickActionLauncherCard({
2711
- operatorOpenId: params.operatorOpenId,
2712
- expiresAt,
2713
- chatType: "p2p"
2714
- }),
2715
- accountId: params.accountId
2716
- });
2717
- } catch (err) {
2718
- params.runtime?.log?.(`feishu[${params.accountId ?? "default"}]: failed to open quick-action launcher for ${params.operatorOpenId}: ${String(err)}`);
2719
- return false;
2720
- }
2721
- params.runtime?.log?.(`feishu[${params.accountId ?? "default"}]: opened quick-action launcher for ${params.operatorOpenId}`);
2722
- return true;
2723
- }
2724
- function isTimeoutErrorMessage(message) {
2725
- return message?.toLowerCase().includes("timeout") || message?.toLowerCase().includes("timed out") ? true : false;
2726
- }
2727
- function isAbortErrorMessage(message) {
2728
- return message?.toLowerCase().includes("aborted") ?? false;
2729
- }
2730
- async function fetchBotIdentityForMonitor(account, options = {}) {
2731
- if (options.abortSignal?.aborted) return {};
2732
- const timeoutMs = options.timeoutMs ?? 1e4;
2733
- const result = await probeFeishu(account, {
2734
- timeoutMs,
2735
- abortSignal: options.abortSignal
2736
- });
2737
- if (result.ok) return {
2738
- botOpenId: result.botOpenId,
2739
- botName: result.botName
2740
- };
2741
- if (options.abortSignal?.aborted || isAbortErrorMessage(result.error)) return {};
2742
- if (isTimeoutErrorMessage(result.error)) (options.runtime?.error ?? console.error)(`feishu[${account.accountId}]: bot info probe timed out after ${timeoutMs}ms; continuing startup`);
2743
- return {};
2744
- }
2745
- //#endregion
2746
- //#region extensions/feishu/src/monitor.state.ts
2747
- const wsClients = /* @__PURE__ */ new Map();
2748
- const httpServers = /* @__PURE__ */ new Map();
2749
- const botOpenIds = /* @__PURE__ */ new Map();
2750
- const botNames = /* @__PURE__ */ new Map();
2751
- const FEISHU_WEBHOOK_MAX_BODY_BYTES = 1024 * 1024;
2752
- const FEISHU_WEBHOOK_BODY_TIMEOUT_MS = 3e4;
2753
- const FEISHU_WEBHOOK_RATE_LIMIT_FALLBACK_DEFAULTS = {
2754
- windowMs: 6e4,
2755
- maxRequests: 120,
2756
- maxTrackedKeys: 4096
2757
- };
2758
- const FEISHU_WEBHOOK_ANOMALY_FALLBACK_DEFAULTS = {
2759
- maxTrackedKeys: 4096,
2760
- ttlMs: 360 * 6e4,
2761
- logEvery: 25
2762
- };
2763
- function coercePositiveInt(value, fallback) {
2764
- if (typeof value !== "number" || !Number.isFinite(value)) return fallback;
2765
- const normalized = Math.floor(value);
2766
- return normalized > 0 ? normalized : fallback;
2767
- }
2768
- function resolveFeishuWebhookRateLimitDefaultsForTest(defaults) {
2769
- const resolved = defaults;
2770
- return {
2771
- windowMs: coercePositiveInt(resolved?.windowMs, FEISHU_WEBHOOK_RATE_LIMIT_FALLBACK_DEFAULTS.windowMs),
2772
- maxRequests: coercePositiveInt(resolved?.maxRequests, FEISHU_WEBHOOK_RATE_LIMIT_FALLBACK_DEFAULTS.maxRequests),
2773
- maxTrackedKeys: coercePositiveInt(resolved?.maxTrackedKeys, FEISHU_WEBHOOK_RATE_LIMIT_FALLBACK_DEFAULTS.maxTrackedKeys)
2774
- };
2775
- }
2776
- function resolveFeishuWebhookAnomalyDefaultsForTest(defaults) {
2777
- const resolved = defaults;
2778
- return {
2779
- maxTrackedKeys: coercePositiveInt(resolved?.maxTrackedKeys, FEISHU_WEBHOOK_ANOMALY_FALLBACK_DEFAULTS.maxTrackedKeys),
2780
- ttlMs: coercePositiveInt(resolved?.ttlMs, FEISHU_WEBHOOK_ANOMALY_FALLBACK_DEFAULTS.ttlMs),
2781
- logEvery: coercePositiveInt(resolved?.logEvery, FEISHU_WEBHOOK_ANOMALY_FALLBACK_DEFAULTS.logEvery)
2782
- };
2783
- }
2784
- const feishuWebhookRateLimitDefaults = resolveFeishuWebhookRateLimitDefaultsForTest(WEBHOOK_RATE_LIMIT_DEFAULTS);
2785
- const feishuWebhookAnomalyDefaults = resolveFeishuWebhookAnomalyDefaultsForTest(WEBHOOK_ANOMALY_COUNTER_DEFAULTS);
2786
- const feishuWebhookRateLimiter = createFixedWindowRateLimiter({
2787
- windowMs: feishuWebhookRateLimitDefaults.windowMs,
2788
- maxRequests: feishuWebhookRateLimitDefaults.maxRequests,
2789
- maxTrackedKeys: feishuWebhookRateLimitDefaults.maxTrackedKeys
2790
- });
2791
- const feishuWebhookAnomalyTracker = createWebhookAnomalyTracker({
2792
- maxTrackedKeys: feishuWebhookAnomalyDefaults.maxTrackedKeys,
2793
- ttlMs: feishuWebhookAnomalyDefaults.ttlMs,
2794
- logEvery: feishuWebhookAnomalyDefaults.logEvery
2795
- });
2796
- function recordWebhookStatus(runtime, accountId, path, statusCode) {
2797
- feishuWebhookAnomalyTracker.record({
2798
- key: `${accountId}:${path}:${statusCode}`,
2799
- statusCode,
2800
- log: runtime?.log ?? console.log,
2801
- message: (count) => `feishu[${accountId}]: webhook anomaly path=${path} status=${statusCode} count=${count}`
2802
- });
2803
- }
2804
- //#endregion
2805
- //#region extensions/feishu/src/monitor.transport.ts
2806
- function isFeishuWebhookPayload(value) {
2807
- return !!value && typeof value === "object" && !Array.isArray(value);
2808
- }
2809
- function timingSafeEqualString(left, right) {
2810
- const leftBuffer = Buffer.from(left, "utf8");
2811
- const rightBuffer = Buffer.from(right, "utf8");
2812
- if (leftBuffer.length !== rightBuffer.length) return false;
2813
- return crypto.timingSafeEqual(leftBuffer, rightBuffer);
2814
- }
2815
- function buildFeishuWebhookEnvelope(req, payload) {
2816
- return Object.assign(Object.create({ headers: req.headers }), payload);
2817
- }
2818
- function isFeishuWebhookSignatureValid(params) {
2819
- const encryptKey = params.encryptKey?.trim();
2820
- if (!encryptKey) return true;
2821
- const timestampHeader = params.headers["x-lark-request-timestamp"];
2822
- const nonceHeader = params.headers["x-lark-request-nonce"];
2823
- const signatureHeader = params.headers["x-lark-signature"];
2824
- const timestamp = Array.isArray(timestampHeader) ? timestampHeader[0] : timestampHeader;
2825
- const nonce = Array.isArray(nonceHeader) ? nonceHeader[0] : nonceHeader;
2826
- const signature = Array.isArray(signatureHeader) ? signatureHeader[0] : signatureHeader;
2827
- if (!timestamp || !nonce || !signature) return false;
2828
- return timingSafeEqualString(crypto.createHash("sha256").update(timestamp + nonce + encryptKey + JSON.stringify(params.payload)).digest("hex"), signature);
2829
- }
2830
- function respondText(res, statusCode, body) {
2831
- res.statusCode = statusCode;
2832
- res.setHeader("Content-Type", "text/plain; charset=utf-8");
2833
- res.end(body);
2834
- }
2835
- async function monitorWebSocket({ account, accountId, runtime, abortSignal, eventDispatcher }) {
2836
- const log = runtime?.log ?? console.log;
2837
- log(`feishu[${accountId}]: starting WebSocket connection...`);
2838
- const wsClient = createFeishuWSClient(account);
2839
- wsClients.set(accountId, wsClient);
2840
- return new Promise((resolve, reject) => {
2841
- const cleanup = () => {
2842
- wsClients.delete(accountId);
2843
- botOpenIds.delete(accountId);
2844
- botNames.delete(accountId);
2845
- };
2846
- const handleAbort = () => {
2847
- log(`feishu[${accountId}]: abort signal received, stopping`);
2848
- cleanup();
2849
- resolve();
2850
- };
2851
- if (abortSignal?.aborted) {
2852
- cleanup();
2853
- resolve();
2854
- return;
2855
- }
2856
- abortSignal?.addEventListener("abort", handleAbort, { once: true });
2857
- try {
2858
- wsClient.start({ eventDispatcher });
2859
- log(`feishu[${accountId}]: WebSocket client started`);
2860
- } catch (err) {
2861
- cleanup();
2862
- abortSignal?.removeEventListener("abort", handleAbort);
2863
- reject(err);
2864
- }
2865
- });
2866
- }
2867
- async function monitorWebhook({ account, accountId, runtime, abortSignal, eventDispatcher }) {
2868
- const log = runtime?.log ?? console.log;
2869
- const error = runtime?.error ?? console.error;
2870
- const port = account.config.webhookPort ?? 3e3;
2871
- const path = account.config.webhookPath ?? "/feishu/events";
2872
- const host = account.config.webhookHost ?? "127.0.0.1";
2873
- log(`feishu[${accountId}]: starting Webhook server on ${host}:${port}, path ${path}...`);
2874
- const server = http.createServer();
2875
- server.on("request", (req, res) => {
2876
- res.on("finish", () => {
2877
- recordWebhookStatus(runtime, accountId, path, res.statusCode);
2878
- });
2879
- if (!applyBasicWebhookRequestGuards({
2880
- req,
2881
- res,
2882
- rateLimiter: feishuWebhookRateLimiter,
2883
- rateLimitKey: `${accountId}:${path}:${req.socket.remoteAddress ?? "unknown"}`,
2884
- nowMs: Date.now(),
2885
- requireJsonContentType: true
2886
- })) return;
2887
- const guard = installRequestBodyLimitGuard(req, res, {
2888
- maxBytes: FEISHU_WEBHOOK_MAX_BODY_BYTES,
2889
- timeoutMs: FEISHU_WEBHOOK_BODY_TIMEOUT_MS,
2890
- responseFormat: "text"
2891
- });
2892
- if (guard.isTripped()) return;
2893
- (async () => {
2894
- try {
2895
- const bodyResult = await readJsonBodyWithLimit(req, {
2896
- maxBytes: FEISHU_WEBHOOK_MAX_BODY_BYTES,
2897
- timeoutMs: FEISHU_WEBHOOK_BODY_TIMEOUT_MS
2898
- });
2899
- if (guard.isTripped() || res.writableEnded) return;
2900
- if (!bodyResult.ok) {
2901
- if (bodyResult.code === "INVALID_JSON") respondText(res, 400, "Invalid JSON");
2902
- return;
2903
- }
2904
- if (!isFeishuWebhookPayload(bodyResult.value)) {
2905
- respondText(res, 400, "Invalid JSON");
2906
- return;
2907
- }
2908
- if (!isFeishuWebhookSignatureValid({
2909
- headers: req.headers,
2910
- payload: bodyResult.value,
2911
- encryptKey: account.encryptKey
2912
- })) {
2913
- respondText(res, 401, "Invalid signature");
2914
- return;
2915
- }
2916
- const { isChallenge, challenge } = Lark.generateChallenge(bodyResult.value, { encryptKey: account.encryptKey ?? "" });
2917
- if (isChallenge) {
2918
- res.statusCode = 200;
2919
- res.setHeader("Content-Type", "application/json; charset=utf-8");
2920
- res.end(JSON.stringify(challenge));
2921
- return;
2922
- }
2923
- const value = await eventDispatcher.invoke(buildFeishuWebhookEnvelope(req, bodyResult.value), { needCheck: false });
2924
- if (!res.headersSent) {
2925
- res.statusCode = 200;
2926
- res.setHeader("Content-Type", "application/json; charset=utf-8");
2927
- res.end(JSON.stringify(value));
2928
- }
2929
- } catch (err) {
2930
- if (!guard.isTripped()) {
2931
- error(`feishu[${accountId}]: webhook handler error: ${String(err)}`);
2932
- if (!res.headersSent) respondText(res, 500, "Internal Server Error");
2933
- }
2934
- } finally {
2935
- guard.dispose();
2936
- }
2937
- })();
2938
- });
2939
- httpServers.set(accountId, server);
2940
- return new Promise((resolve, reject) => {
2941
- const cleanup = () => {
2942
- server.close();
2943
- httpServers.delete(accountId);
2944
- botOpenIds.delete(accountId);
2945
- botNames.delete(accountId);
2946
- };
2947
- const handleAbort = () => {
2948
- log(`feishu[${accountId}]: abort signal received, stopping Webhook server`);
2949
- cleanup();
2950
- resolve();
2951
- };
2952
- if (abortSignal?.aborted) {
2953
- cleanup();
2954
- resolve();
2955
- return;
2956
- }
2957
- abortSignal?.addEventListener("abort", handleAbort, { once: true });
2958
- server.listen(port, host, () => {
2959
- log(`feishu[${accountId}]: Webhook server listening on ${host}:${port}`);
2960
- });
2961
- server.on("error", (err) => {
2962
- error(`feishu[${accountId}]: Webhook server error: ${err}`);
2963
- abortSignal?.removeEventListener("abort", handleAbort);
2964
- reject(err);
2965
- });
2966
- });
2967
- }
2968
- //#endregion
2969
- //#region extensions/feishu/src/monitor.account.ts
2970
- const FEISHU_REACTION_VERIFY_TIMEOUT_MS = 1500;
2971
- async function resolveReactionSyntheticEvent(params) {
2972
- const { cfg, accountId, event, botOpenId, fetchMessage = getMessageFeishu, verificationTimeoutMs = FEISHU_REACTION_VERIFY_TIMEOUT_MS, logger, uuid = () => crypto$2.randomUUID(), action = "created" } = params;
2973
- const emoji = event.reaction_type?.emoji_type;
2974
- const messageId = event.message_id;
2975
- const senderId = event.user_id?.open_id;
2976
- if (!emoji || !messageId || !senderId) return null;
2977
- const reactionNotifications = resolveFeishuAccount({
2978
- cfg,
2979
- accountId
2980
- }).config.reactionNotifications ?? "own";
2981
- if (reactionNotifications === "off") return null;
2982
- if (event.operator_type === "app" || senderId === botOpenId) return null;
2983
- if (emoji === "Typing") return null;
2984
- if (reactionNotifications === "own" && !botOpenId) {
2985
- logger?.(`feishu[${accountId}]: bot open_id unavailable, skipping reaction ${emoji} on ${messageId}`);
2986
- return null;
2987
- }
2988
- const reactedMsg = await raceWithTimeoutAndAbort(fetchMessage({
2989
- cfg,
2990
- messageId,
2991
- accountId
2992
- }), { timeoutMs: verificationTimeoutMs }).then((result) => result.status === "resolved" ? result.value : null).catch(() => null);
2993
- const isBotMessage = reactedMsg?.senderType === "app" || reactedMsg?.senderOpenId === botOpenId;
2994
- if (!reactedMsg || reactionNotifications === "own" && !isBotMessage) {
2995
- logger?.(`feishu[${accountId}]: ignoring reaction on non-bot/unverified message ${messageId} (sender: ${reactedMsg?.senderOpenId ?? "unknown"})`);
2996
- return null;
2997
- }
2998
- const fallbackChatType = reactedMsg.chatType;
2999
- const resolvedChatType = normalizeFeishuChatType(event.chat_type) ?? fallbackChatType;
3000
- if (!resolvedChatType) {
3001
- logger?.(`feishu[${accountId}]: skipping reaction ${emoji} on ${messageId} without chat type context`);
3002
- return null;
3003
- }
3004
- const syntheticChatIdRaw = event.chat_id ?? reactedMsg.chatId;
3005
- const syntheticChatId = syntheticChatIdRaw?.trim() ? syntheticChatIdRaw : `p2p:${senderId}`;
3006
- const syntheticChatType = resolvedChatType;
3007
- return {
3008
- sender: {
3009
- sender_id: { open_id: senderId },
3010
- sender_type: "user"
3011
- },
3012
- message: {
3013
- message_id: `${messageId}:reaction:${emoji}:${uuid()}`,
3014
- chat_id: syntheticChatId,
3015
- chat_type: syntheticChatType,
3016
- message_type: "text",
3017
- content: JSON.stringify({ text: action === "deleted" ? `[removed reaction ${emoji} from message ${messageId}]` : `[reacted with ${emoji} to message ${messageId}]` })
3018
- }
3019
- };
3020
- }
3021
- function normalizeFeishuChatType(value) {
3022
- return value === "group" || value === "private" || value === "p2p" ? value : void 0;
3023
- }
3024
- /**
3025
- * Per-chat serial queue that ensures messages from the same chat are processed
3026
- * in arrival order while allowing different chats to run concurrently.
3027
- */
3028
- function createChatQueue() {
3029
- const queues = /* @__PURE__ */ new Map();
3030
- return (chatId, task) => {
3031
- const next = (queues.get(chatId) ?? Promise.resolve()).then(task, task);
3032
- queues.set(chatId, next);
3033
- next.finally(() => {
3034
- if (queues.get(chatId) === next) queues.delete(chatId);
3035
- });
3036
- return next;
3037
- };
3038
- }
3039
- function mergeFeishuDebounceMentions(entries) {
3040
- const merged = /* @__PURE__ */ new Map();
3041
- for (const entry of entries) for (const mention of entry.message.mentions ?? []) {
3042
- const stableId = mention.id.open_id?.trim() || mention.id.user_id?.trim() || mention.id.union_id?.trim();
3043
- const mentionName = mention.name?.trim();
3044
- const mentionKey = mention.key?.trim();
3045
- const fallback = mentionName && mentionKey ? `${mentionName}|${mentionKey}` : mentionName || mentionKey;
3046
- const key = stableId || fallback;
3047
- if (!key || merged.has(key)) continue;
3048
- merged.set(key, mention);
3049
- }
3050
- if (merged.size === 0) return;
3051
- return Array.from(merged.values());
3052
- }
3053
- function dedupeFeishuDebounceEntriesByMessageId(entries) {
3054
- const seen = /* @__PURE__ */ new Set();
3055
- const deduped = [];
3056
- for (const entry of entries) {
3057
- const messageId = entry.message.message_id?.trim();
3058
- if (!messageId) {
3059
- deduped.push(entry);
3060
- continue;
3061
- }
3062
- if (seen.has(messageId)) continue;
3063
- seen.add(messageId);
3064
- deduped.push(entry);
3065
- }
3066
- return deduped;
3067
- }
3068
- function resolveFeishuDebounceMentions(params) {
3069
- const { entries, botOpenId } = params;
3070
- if (entries.length === 0) return;
3071
- for (let index = entries.length - 1; index >= 0; index -= 1) {
3072
- const entry = entries[index];
3073
- if (isMentionForwardRequest(entry, botOpenId)) return mergeFeishuDebounceMentions([entry]);
3074
- }
3075
- const merged = mergeFeishuDebounceMentions(entries);
3076
- if (!merged) return;
3077
- const normalizedBotOpenId = botOpenId?.trim();
3078
- if (!normalizedBotOpenId) return;
3079
- const botMentions = merged.filter((mention) => mention.id.open_id?.trim() === normalizedBotOpenId);
3080
- return botMentions.length > 0 ? botMentions : void 0;
3081
- }
3082
- function registerEventHandlers(eventDispatcher, context) {
3083
- const { cfg, accountId, runtime, chatHistories, fireAndForget } = context;
3084
- const core = getFeishuRuntime();
3085
- const inboundDebounceMs = core.channel.debounce.resolveInboundDebounceMs({
3086
- cfg,
3087
- channel: "feishu"
3088
- });
3089
- const log = runtime?.log ?? console.log;
3090
- const error = runtime?.error ?? console.error;
3091
- const enqueue = createChatQueue();
3092
- const runFeishuHandler = async (params) => {
3093
- if (fireAndForget) {
3094
- params.task().catch((err) => {
3095
- error(`${params.errorMessage}: ${String(err)}`);
3096
- });
3097
- return;
3098
- }
3099
- try {
3100
- await params.task();
3101
- } catch (err) {
3102
- error(`${params.errorMessage}: ${String(err)}`);
3103
- }
3104
- };
3105
- const dispatchFeishuMessage = async (event) => {
3106
- const chatId = event.message.chat_id?.trim() || "unknown";
3107
- const task = () => handleFeishuMessage({
3108
- cfg,
3109
- event,
3110
- botOpenId: botOpenIds.get(accountId),
3111
- botName: botNames.get(accountId),
3112
- runtime,
3113
- chatHistories,
3114
- accountId,
3115
- processingClaimHeld: true
3116
- });
3117
- await enqueue(chatId, task);
3118
- };
3119
- const resolveSenderDebounceId = (event) => {
3120
- return event.sender.sender_id.open_id?.trim() || event.sender.sender_id.user_id?.trim() || void 0;
3121
- };
3122
- const resolveDebounceText = (event) => {
3123
- return parseFeishuMessageEvent(event, botOpenIds.get(accountId), botNames.get(accountId)).content.trim();
3124
- };
3125
- const recordSuppressedMessageIds = async (entries, dispatchMessageId) => {
3126
- const keepMessageId = dispatchMessageId?.trim();
3127
- const suppressedIds = new Set(entries.map((entry) => entry.message.message_id?.trim()).filter((id) => Boolean(id) && (!keepMessageId || id !== keepMessageId)));
3128
- if (suppressedIds.size === 0) return;
3129
- for (const messageId of suppressedIds) try {
3130
- await recordProcessedFeishuMessage(messageId, accountId, log);
3131
- } catch (err) {
3132
- error(`feishu[${accountId}]: failed to record merged dedupe id ${messageId}: ${String(err)}`);
3133
- }
3134
- };
3135
- const isMessageAlreadyProcessed = async (entry) => {
3136
- return await hasProcessedFeishuMessage(entry.message.message_id, accountId, log);
3137
- };
3138
- const inboundDebouncer = core.channel.debounce.createInboundDebouncer({
3139
- debounceMs: inboundDebounceMs,
3140
- buildKey: (event) => {
3141
- const chatId = event.message.chat_id?.trim();
3142
- const senderId = resolveSenderDebounceId(event);
3143
- if (!chatId || !senderId) return null;
3144
- const rootId = event.message.root_id?.trim();
3145
- return `feishu:${accountId}:${chatId}:${rootId ? `thread:${rootId}` : "chat"}:${senderId}`;
3146
- },
3147
- shouldDebounce: (event) => {
3148
- if (event.message.message_type !== "text") return false;
3149
- const text = resolveDebounceText(event);
3150
- if (!text) return false;
3151
- return !core.channel.text.hasControlCommand(text, cfg);
3152
- },
3153
- onFlush: async (entries) => {
3154
- const last = entries.at(-1);
3155
- if (!last) return;
3156
- if (entries.length === 1) {
3157
- await dispatchFeishuMessage(last);
3158
- return;
3159
- }
3160
- const dedupedEntries = dedupeFeishuDebounceEntriesByMessageId(entries);
3161
- const freshEntries = [];
3162
- for (const entry of dedupedEntries) if (!await isMessageAlreadyProcessed(entry)) freshEntries.push(entry);
3163
- const dispatchEntry = freshEntries.at(-1);
3164
- if (!dispatchEntry) return;
3165
- await recordSuppressedMessageIds(dedupedEntries, dispatchEntry.message.message_id);
3166
- const combinedText = freshEntries.map((entry) => resolveDebounceText(entry)).filter(Boolean).join("\n");
3167
- const mergedMentions = resolveFeishuDebounceMentions({
3168
- entries: freshEntries,
3169
- botOpenId: botOpenIds.get(accountId)
3170
- });
3171
- if (!combinedText.trim()) {
3172
- await dispatchFeishuMessage({
3173
- ...dispatchEntry,
3174
- message: {
3175
- ...dispatchEntry.message,
3176
- mentions: mergedMentions ?? dispatchEntry.message.mentions
3177
- }
3178
- });
3179
- return;
3180
- }
3181
- await dispatchFeishuMessage({
3182
- ...dispatchEntry,
3183
- message: {
3184
- ...dispatchEntry.message,
3185
- message_type: "text",
3186
- content: JSON.stringify({ text: combinedText }),
3187
- mentions: mergedMentions ?? dispatchEntry.message.mentions
3188
- }
3189
- });
3190
- },
3191
- onError: (err, entries) => {
3192
- for (const entry of entries) releaseFeishuMessageProcessing(entry.message.message_id, accountId);
3193
- error(`feishu[${accountId}]: inbound debounce flush failed: ${String(err)}`);
3194
- }
3195
- });
3196
- eventDispatcher.register({
3197
- "im.message.receive_v1": async (data) => {
3198
- const event = data;
3199
- const messageId = event.message?.message_id?.trim();
3200
- if (!tryBeginFeishuMessageProcessing(messageId, accountId)) {
3201
- log(`feishu[${accountId}]: dropping duplicate event for message ${messageId}`);
3202
- return;
3203
- }
3204
- const processMessage = async () => {
3205
- await inboundDebouncer.enqueue(event);
3206
- };
3207
- if (fireAndForget) {
3208
- processMessage().catch((err) => {
3209
- releaseFeishuMessageProcessing(messageId, accountId);
3210
- error(`feishu[${accountId}]: error handling message: ${String(err)}`);
3211
- });
3212
- return;
3213
- }
3214
- try {
3215
- await processMessage();
3216
- } catch (err) {
3217
- releaseFeishuMessageProcessing(messageId, accountId);
3218
- error(`feishu[${accountId}]: error handling message: ${String(err)}`);
3219
- }
3220
- },
3221
- "im.message.message_read_v1": async () => {},
3222
- "im.chat.member.bot.added_v1": async (data) => {
3223
- try {
3224
- log(`feishu[${accountId}]: bot added to chat ${data.chat_id}`);
3225
- } catch (err) {
3226
- error(`feishu[${accountId}]: error handling bot added event: ${String(err)}`);
3227
- }
3228
- },
3229
- "im.chat.member.bot.deleted_v1": async (data) => {
3230
- try {
3231
- log(`feishu[${accountId}]: bot removed from chat ${data.chat_id}`);
3232
- } catch (err) {
3233
- error(`feishu[${accountId}]: error handling bot removed event: ${String(err)}`);
3234
- }
3235
- },
3236
- "im.message.reaction.created_v1": async (data) => {
3237
- await runFeishuHandler({
3238
- errorMessage: `feishu[${accountId}]: error handling reaction event`,
3239
- task: async () => {
3240
- const event = data;
3241
- const myBotId = botOpenIds.get(accountId);
3242
- const syntheticEvent = await resolveReactionSyntheticEvent({
3243
- cfg,
3244
- accountId,
3245
- event,
3246
- botOpenId: myBotId,
3247
- logger: log
3248
- });
3249
- if (!syntheticEvent) return;
3250
- await handleFeishuMessage({
3251
- cfg,
3252
- event: syntheticEvent,
3253
- botOpenId: myBotId,
3254
- botName: botNames.get(accountId),
3255
- runtime,
3256
- chatHistories,
3257
- accountId
3258
- });
3259
- }
3260
- });
3261
- },
3262
- "im.message.reaction.deleted_v1": async (data) => {
3263
- await runFeishuHandler({
3264
- errorMessage: `feishu[${accountId}]: error handling reaction removal event`,
3265
- task: async () => {
3266
- const event = data;
3267
- const myBotId = botOpenIds.get(accountId);
3268
- const syntheticEvent = await resolveReactionSyntheticEvent({
3269
- cfg,
3270
- accountId,
3271
- event,
3272
- botOpenId: myBotId,
3273
- logger: log,
3274
- action: "deleted"
3275
- });
3276
- if (!syntheticEvent) return;
3277
- await handleFeishuMessage({
3278
- cfg,
3279
- event: syntheticEvent,
3280
- botOpenId: myBotId,
3281
- botName: botNames.get(accountId),
3282
- runtime,
3283
- chatHistories,
3284
- accountId
3285
- });
3286
- }
3287
- });
3288
- },
3289
- "application.bot.menu_v6": async (data) => {
3290
- try {
3291
- const event = data;
3292
- const operatorOpenId = event.operator?.operator_id?.open_id?.trim();
3293
- const eventKey = event.event_key?.trim();
3294
- if (!operatorOpenId || !eventKey) return;
3295
- const syntheticEvent = {
3296
- sender: {
3297
- sender_id: {
3298
- open_id: operatorOpenId,
3299
- user_id: event.operator?.operator_id?.user_id,
3300
- union_id: event.operator?.operator_id?.union_id
3301
- },
3302
- sender_type: "user"
3303
- },
3304
- message: {
3305
- message_id: `bot-menu:${eventKey}:${event.timestamp ?? Date.now()}`,
3306
- chat_id: `p2p:${operatorOpenId}`,
3307
- chat_type: "p2p",
3308
- message_type: "text",
3309
- content: JSON.stringify({ text: `/menu ${eventKey}` })
3310
- }
3311
- };
3312
- const handleLegacyMenu = () => handleFeishuMessage({
3313
- cfg,
3314
- event: syntheticEvent,
3315
- botOpenId: botOpenIds.get(accountId),
3316
- botName: botNames.get(accountId),
3317
- runtime,
3318
- chatHistories,
3319
- accountId
3320
- });
3321
- const promise = maybeHandleFeishuQuickActionMenu({
3322
- cfg,
3323
- eventKey,
3324
- operatorOpenId,
3325
- runtime,
3326
- accountId
3327
- }).then((handledMenu) => {
3328
- if (handledMenu) return;
3329
- return handleLegacyMenu();
3330
- });
3331
- if (fireAndForget) {
3332
- promise.catch((err) => {
3333
- error(`feishu[${accountId}]: error handling bot menu event: ${String(err)}`);
3334
- });
3335
- return;
3336
- }
3337
- await promise;
3338
- } catch (err) {
3339
- error(`feishu[${accountId}]: error handling bot menu event: ${String(err)}`);
3340
- }
3341
- },
3342
- "card.action.trigger": async (data) => {
3343
- try {
3344
- const promise = handleFeishuCardAction({
3345
- cfg,
3346
- event: data,
3347
- botOpenId: botOpenIds.get(accountId),
3348
- runtime,
3349
- accountId
3350
- });
3351
- if (fireAndForget) promise.catch((err) => {
3352
- error(`feishu[${accountId}]: error handling card action: ${String(err)}`);
3353
- });
3354
- else await promise;
3355
- } catch (err) {
3356
- error(`feishu[${accountId}]: error handling card action: ${String(err)}`);
3357
- }
3358
- }
3359
- });
3360
- }
3361
- async function monitorSingleAccount(params) {
3362
- const { cfg, account, runtime, abortSignal } = params;
3363
- const { accountId } = account;
3364
- const log = runtime?.log ?? console.log;
3365
- const botOpenIdSource = params.botOpenIdSource ?? { kind: "fetch" };
3366
- const botIdentity = botOpenIdSource.kind === "prefetched" ? {
3367
- botOpenId: botOpenIdSource.botOpenId,
3368
- botName: botOpenIdSource.botName
3369
- } : await fetchBotIdentityForMonitor(account, {
3370
- runtime,
3371
- abortSignal
3372
- });
3373
- const botOpenId = botIdentity.botOpenId;
3374
- const botName = botIdentity.botName?.trim();
3375
- botOpenIds.set(accountId, botOpenId ?? "");
3376
- if (botName) botNames.set(accountId, botName);
3377
- else botNames.delete(accountId);
3378
- log(`feishu[${accountId}]: bot open_id resolved: ${botOpenId ?? "unknown"}`);
3379
- const connectionMode = account.config.connectionMode ?? "websocket";
3380
- if (connectionMode === "webhook" && !account.verificationToken?.trim()) throw new Error(`Feishu account "${accountId}" webhook mode requires verificationToken`);
3381
- if (connectionMode === "webhook" && !account.encryptKey?.trim()) throw new Error(`Feishu account "${accountId}" webhook mode requires encryptKey`);
3382
- const warmupCount = await warmupDedupFromDisk(accountId, log);
3383
- if (warmupCount > 0) log(`feishu[${accountId}]: dedup warmup loaded ${warmupCount} entries from disk`);
3384
- let threadBindingManager = null;
3385
- try {
3386
- const eventDispatcher = createEventDispatcher(account);
3387
- const chatHistories = /* @__PURE__ */ new Map();
3388
- threadBindingManager = createFeishuThreadBindingManager({
3389
- accountId,
3390
- cfg
3391
- });
3392
- registerEventHandlers(eventDispatcher, {
3393
- cfg,
3394
- accountId,
3395
- runtime,
3396
- chatHistories,
3397
- fireAndForget: true
3398
- });
3399
- if (connectionMode === "webhook") return await monitorWebhook({
3400
- account,
3401
- accountId,
3402
- runtime,
3403
- abortSignal,
3404
- eventDispatcher
3405
- });
3406
- return await monitorWebSocket({
3407
- account,
3408
- accountId,
3409
- runtime,
3410
- abortSignal,
3411
- eventDispatcher
3412
- });
3413
- } finally {
3414
- threadBindingManager?.stop();
3415
- }
3416
- }
3417
- //#endregion
3418
- //#region extensions/feishu/src/monitor.ts
3419
- async function monitorFeishuProvider(opts = {}) {
3420
- const cfg = opts.config;
3421
- if (!cfg) throw new Error("Config is required for Feishu monitor");
3422
- const log = opts.runtime?.log ?? console.log;
3423
- if (opts.accountId) {
3424
- const account = resolveFeishuAccount({
3425
- cfg,
3426
- accountId: opts.accountId
3427
- });
3428
- if (!account.enabled || !account.configured) throw new Error(`Feishu account "${opts.accountId}" not configured or disabled`);
3429
- return monitorSingleAccount({
3430
- cfg,
3431
- account,
3432
- runtime: opts.runtime,
3433
- abortSignal: opts.abortSignal
3434
- });
3435
- }
3436
- const accounts = listEnabledFeishuAccounts(cfg);
3437
- if (accounts.length === 0) throw new Error("No enabled Feishu accounts configured");
3438
- log(`feishu: starting ${accounts.length} account(s): ${accounts.map((a) => a.accountId).join(", ")}`);
3439
- const monitorPromises = [];
3440
- for (const account of accounts) {
3441
- if (opts.abortSignal?.aborted) {
3442
- log("feishu: abort signal received during startup preflight; stopping startup");
3443
- break;
3444
- }
3445
- const { botOpenId, botName } = await fetchBotIdentityForMonitor(account, {
3446
- runtime: opts.runtime,
3447
- abortSignal: opts.abortSignal
3448
- });
3449
- if (opts.abortSignal?.aborted) {
3450
- log("feishu: abort signal received during startup preflight; stopping startup");
3451
- break;
3452
- }
3453
- monitorPromises.push(monitorSingleAccount({
3454
- cfg,
3455
- account,
3456
- runtime: opts.runtime,
3457
- abortSignal: opts.abortSignal,
3458
- botOpenIdSource: {
3459
- kind: "prefetched",
3460
- botOpenId,
3461
- botName
3462
- }
3463
- }));
3464
- }
3465
- await Promise.all(monitorPromises);
3466
- }
3467
- //#endregion
3468
- export { resolveReactionSyntheticEvent as n, getFeishuThreadBindingManager as r, monitorFeishuProvider as t };