@kodelyth/discord 2026.5.42 → 2026.6.1

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 (514) hide show
  1. package/klaw.plugin.json +3822 -2
  2. package/package.json +16 -4
  3. package/account-inspect-api.ts +0 -6
  4. package/action-runtime-api.ts +0 -1
  5. package/api.ts +0 -130
  6. package/channel-config-api.ts +0 -1
  7. package/channel-plugin-api.ts +0 -3
  8. package/config-api.ts +0 -4
  9. package/configured-state.ts +0 -6
  10. package/contract-api.ts +0 -21
  11. package/directory-contract-api.ts +0 -4
  12. package/doctor-contract-api.ts +0 -1
  13. package/index.test.ts +0 -13
  14. package/index.ts +0 -24
  15. package/runtime-api.actions.ts +0 -15
  16. package/runtime-api.lookup.ts +0 -22
  17. package/runtime-api.monitor.ts +0 -50
  18. package/runtime-api.send.ts +0 -79
  19. package/runtime-api.threads.ts +0 -31
  20. package/runtime-api.ts +0 -181
  21. package/runtime-setter-api.ts +0 -3
  22. package/secret-contract-api.ts +0 -4
  23. package/security-audit-contract-api.ts +0 -1
  24. package/security-contract-api.ts +0 -4
  25. package/session-key-api.ts +0 -1
  26. package/setup-entry.ts +0 -9
  27. package/setup-plugin-api.ts +0 -3
  28. package/src/account-inspect.test.ts +0 -126
  29. package/src/account-inspect.ts +0 -128
  30. package/src/accounts.test.ts +0 -381
  31. package/src/accounts.ts +0 -205
  32. package/src/actions/handle-action.guild-admin.ts +0 -421
  33. package/src/actions/handle-action.test.ts +0 -480
  34. package/src/actions/handle-action.ts +0 -402
  35. package/src/actions/runtime.guild.ts +0 -446
  36. package/src/actions/runtime.messaging.messages.ts +0 -226
  37. package/src/actions/runtime.messaging.reactions.ts +0 -67
  38. package/src/actions/runtime.messaging.runtime.ts +0 -73
  39. package/src/actions/runtime.messaging.send.ts +0 -336
  40. package/src/actions/runtime.messaging.shared.ts +0 -97
  41. package/src/actions/runtime.messaging.ts +0 -37
  42. package/src/actions/runtime.moderation-shared.ts +0 -48
  43. package/src/actions/runtime.moderation.authz.test.ts +0 -151
  44. package/src/actions/runtime.moderation.ts +0 -116
  45. package/src/actions/runtime.presence.test.ts +0 -165
  46. package/src/actions/runtime.presence.ts +0 -117
  47. package/src/actions/runtime.shared.ts +0 -86
  48. package/src/actions/runtime.test.ts +0 -1337
  49. package/src/actions/runtime.ts +0 -87
  50. package/src/api-barrel.test.ts +0 -78
  51. package/src/api.test.ts +0 -152
  52. package/src/api.ts +0 -215
  53. package/src/approval-handler.runtime.test.ts +0 -41
  54. package/src/approval-handler.runtime.ts +0 -633
  55. package/src/approval-native.test.ts +0 -330
  56. package/src/approval-native.ts +0 -219
  57. package/src/approval-runtime.ts +0 -14
  58. package/src/approval-shared.ts +0 -50
  59. package/src/audit-core.ts +0 -178
  60. package/src/audit.test.ts +0 -204
  61. package/src/audit.ts +0 -32
  62. package/src/channel-actions.contract.test.ts +0 -45
  63. package/src/channel-actions.runtime.ts +0 -1
  64. package/src/channel-actions.test.ts +0 -504
  65. package/src/channel-actions.ts +0 -254
  66. package/src/channel-api.ts +0 -29
  67. package/src/channel.conversation.ts +0 -159
  68. package/src/channel.loaders.ts +0 -50
  69. package/src/channel.message-adapter.test.ts +0 -230
  70. package/src/channel.runtime.ts +0 -1
  71. package/src/channel.setup.ts +0 -12
  72. package/src/channel.test.ts +0 -828
  73. package/src/channel.ts +0 -728
  74. package/src/chunk.test.ts +0 -170
  75. package/src/chunk.ts +0 -321
  76. package/src/client.proxy.test.ts +0 -177
  77. package/src/client.test.ts +0 -83
  78. package/src/client.ts +0 -143
  79. package/src/component-custom-id.ts +0 -72
  80. package/src/components-registry.ts +0 -356
  81. package/src/components.builders.ts +0 -409
  82. package/src/components.modal.ts +0 -124
  83. package/src/components.parse.ts +0 -407
  84. package/src/components.test.ts +0 -345
  85. package/src/components.ts +0 -54
  86. package/src/components.types.ts +0 -187
  87. package/src/config-schema.test.ts +0 -439
  88. package/src/config-schema.ts +0 -6
  89. package/src/config-ui-hints.ts +0 -354
  90. package/src/conversation-identity.ts +0 -58
  91. package/src/delivery-retry.ts +0 -52
  92. package/src/directory-cache.ts +0 -116
  93. package/src/directory-config.ts +0 -58
  94. package/src/directory-contract.test.ts +0 -129
  95. package/src/directory-live.test.ts +0 -141
  96. package/src/directory-live.ts +0 -135
  97. package/src/doctor-contract.ts +0 -477
  98. package/src/doctor-shared.ts +0 -5
  99. package/src/doctor.test.ts +0 -393
  100. package/src/doctor.ts +0 -340
  101. package/src/draft-chunking.test.ts +0 -64
  102. package/src/draft-chunking.ts +0 -43
  103. package/src/draft-stream.test.ts +0 -193
  104. package/src/draft-stream.ts +0 -162
  105. package/src/durable-delivery.test.ts +0 -103
  106. package/src/error-body.ts +0 -38
  107. package/src/exec-approvals.test.ts +0 -88
  108. package/src/exec-approvals.ts +0 -110
  109. package/src/gateway-logging.test.ts +0 -98
  110. package/src/gateway-logging.ts +0 -67
  111. package/src/group-policy.ts +0 -113
  112. package/src/guilds.ts +0 -29
  113. package/src/inbound-context.contract.test.ts +0 -11
  114. package/src/inbound-event-delivery.ts +0 -135
  115. package/src/interactive-dispatch.ts +0 -104
  116. package/src/internal/api.commands.ts +0 -51
  117. package/src/internal/api.guild.ts +0 -164
  118. package/src/internal/api.interactions.ts +0 -53
  119. package/src/internal/api.messages.ts +0 -113
  120. package/src/internal/api.reactions.ts +0 -38
  121. package/src/internal/api.test.ts +0 -260
  122. package/src/internal/api.ts +0 -61
  123. package/src/internal/api.users.ts +0 -19
  124. package/src/internal/api.webhooks.ts +0 -13
  125. package/src/internal/client.test.ts +0 -472
  126. package/src/internal/client.ts +0 -310
  127. package/src/internal/command-deploy.test.ts +0 -197
  128. package/src/internal/command-deploy.ts +0 -352
  129. package/src/internal/commands.ts +0 -188
  130. package/src/internal/components.base.ts +0 -65
  131. package/src/internal/components.message.ts +0 -279
  132. package/src/internal/components.modal.ts +0 -95
  133. package/src/internal/components.ts +0 -31
  134. package/src/internal/discord.ts +0 -11
  135. package/src/internal/embeds.ts +0 -35
  136. package/src/internal/entity-cache.ts +0 -98
  137. package/src/internal/event-queue.ts +0 -185
  138. package/src/internal/gateway-close-codes.ts +0 -25
  139. package/src/internal/gateway-dispatch.ts +0 -96
  140. package/src/internal/gateway-identify-limiter.ts +0 -26
  141. package/src/internal/gateway-lifecycle.test.ts +0 -114
  142. package/src/internal/gateway-lifecycle.ts +0 -75
  143. package/src/internal/gateway-rate-limit.ts +0 -104
  144. package/src/internal/gateway.test.ts +0 -676
  145. package/src/internal/gateway.ts +0 -479
  146. package/src/internal/interaction-dispatch.test.ts +0 -148
  147. package/src/internal/interaction-dispatch.ts +0 -162
  148. package/src/internal/interaction-options.ts +0 -98
  149. package/src/internal/interaction-response.ts +0 -53
  150. package/src/internal/interactions.test.ts +0 -329
  151. package/src/internal/interactions.ts +0 -378
  152. package/src/internal/listeners.ts +0 -91
  153. package/src/internal/live-smoke.live.test.ts +0 -26
  154. package/src/internal/modal-fields.ts +0 -95
  155. package/src/internal/payload.ts +0 -69
  156. package/src/internal/rest-body.ts +0 -115
  157. package/src/internal/rest-errors.ts +0 -88
  158. package/src/internal/rest-routes.ts +0 -50
  159. package/src/internal/rest-scheduler.ts +0 -557
  160. package/src/internal/rest.test.ts +0 -681
  161. package/src/internal/rest.ts +0 -322
  162. package/src/internal/schemas.ts +0 -36
  163. package/src/internal/structures.test.ts +0 -43
  164. package/src/internal/structures.ts +0 -280
  165. package/src/internal/test-builders.test-support.ts +0 -167
  166. package/src/internal/voice.ts +0 -49
  167. package/src/media-detection.ts +0 -28
  168. package/src/mentions.test.ts +0 -111
  169. package/src/mentions.ts +0 -147
  170. package/src/monitor/ack-reactions.ts +0 -70
  171. package/src/monitor/acp-bind-here.integration.test.ts +0 -219
  172. package/src/monitor/agent-components-auth.ts +0 -7
  173. package/src/monitor/agent-components-context.ts +0 -154
  174. package/src/monitor/agent-components-data.ts +0 -224
  175. package/src/monitor/agent-components-dm-auth.ts +0 -177
  176. package/src/monitor/agent-components-guild-auth.ts +0 -322
  177. package/src/monitor/agent-components-helpers.runtime.ts +0 -3
  178. package/src/monitor/agent-components-helpers.ts +0 -34
  179. package/src/monitor/agent-components-reply.ts +0 -10
  180. package/src/monitor/agent-components.deps.runtime.ts +0 -2
  181. package/src/monitor/agent-components.dispatch.ts +0 -359
  182. package/src/monitor/agent-components.handlers.ts +0 -303
  183. package/src/monitor/agent-components.modal.ts +0 -160
  184. package/src/monitor/agent-components.plugin-interactive.ts +0 -187
  185. package/src/monitor/agent-components.runtime.ts +0 -14
  186. package/src/monitor/agent-components.system-controls.ts +0 -215
  187. package/src/monitor/agent-components.ts +0 -70
  188. package/src/monitor/agent-components.types.ts +0 -58
  189. package/src/monitor/agent-components.wildcard-controls.ts +0 -171
  190. package/src/monitor/agent-components.wildcard.test.ts +0 -71
  191. package/src/monitor/allow-list.test.ts +0 -14
  192. package/src/monitor/allow-list.ts +0 -631
  193. package/src/monitor/auto-presence.test.ts +0 -184
  194. package/src/monitor/auto-presence.ts +0 -356
  195. package/src/monitor/channel-access.test.ts +0 -113
  196. package/src/monitor/channel-access.ts +0 -102
  197. package/src/monitor/commands.test.ts +0 -24
  198. package/src/monitor/commands.ts +0 -9
  199. package/src/monitor/dm-command-auth.test.ts +0 -274
  200. package/src/monitor/dm-command-auth.ts +0 -259
  201. package/src/monitor/dm-command-decision.test.ts +0 -108
  202. package/src/monitor/dm-command-decision.ts +0 -49
  203. package/src/monitor/exec-approvals.test.ts +0 -225
  204. package/src/monitor/exec-approvals.ts +0 -158
  205. package/src/monitor/format.ts +0 -45
  206. package/src/monitor/gateway-handle.ts +0 -33
  207. package/src/monitor/gateway-metadata.test.ts +0 -29
  208. package/src/monitor/gateway-metadata.ts +0 -298
  209. package/src/monitor/gateway-plugin.test.ts +0 -320
  210. package/src/monitor/gateway-plugin.ts +0 -302
  211. package/src/monitor/gateway-registry.ts +0 -37
  212. package/src/monitor/gateway-supervisor.test.ts +0 -157
  213. package/src/monitor/gateway-supervisor.ts +0 -206
  214. package/src/monitor/inbound-context.test-helpers.ts +0 -37
  215. package/src/monitor/inbound-context.test.ts +0 -112
  216. package/src/monitor/inbound-context.ts +0 -95
  217. package/src/monitor/inbound-dedupe.ts +0 -79
  218. package/src/monitor/inbound-job.test.ts +0 -216
  219. package/src/monitor/inbound-job.ts +0 -118
  220. package/src/monitor/listeners.queue.ts +0 -91
  221. package/src/monitor/listeners.reactions.ts +0 -594
  222. package/src/monitor/listeners.test.ts +0 -209
  223. package/src/monitor/listeners.ts +0 -150
  224. package/src/monitor/message-channel-info.ts +0 -96
  225. package/src/monitor/message-forwarded.ts +0 -114
  226. package/src/monitor/message-handler.batch-gate.test.ts +0 -22
  227. package/src/monitor/message-handler.batch-gate.ts +0 -19
  228. package/src/monitor/message-handler.bot-self-filter.test.ts +0 -68
  229. package/src/monitor/message-handler.context.ts +0 -492
  230. package/src/monitor/message-handler.dm-preflight.ts +0 -119
  231. package/src/monitor/message-handler.draft-preview.ts +0 -426
  232. package/src/monitor/message-handler.hydration.test.ts +0 -80
  233. package/src/monitor/message-handler.hydration.ts +0 -198
  234. package/src/monitor/message-handler.inbound-context.test.ts +0 -61
  235. package/src/monitor/message-handler.module-test-helpers.ts +0 -31
  236. package/src/monitor/message-handler.preflight-channel-access.ts +0 -86
  237. package/src/monitor/message-handler.preflight-channel-context.test.ts +0 -18
  238. package/src/monitor/message-handler.preflight-channel-context.ts +0 -58
  239. package/src/monitor/message-handler.preflight-context.ts +0 -54
  240. package/src/monitor/message-handler.preflight-helpers.ts +0 -164
  241. package/src/monitor/message-handler.preflight-history.ts +0 -23
  242. package/src/monitor/message-handler.preflight-logging.ts +0 -36
  243. package/src/monitor/message-handler.preflight-pluralkit.ts +0 -26
  244. package/src/monitor/message-handler.preflight-runtime.ts +0 -28
  245. package/src/monitor/message-handler.preflight-thread.ts +0 -49
  246. package/src/monitor/message-handler.preflight.acp-bindings.test.ts +0 -371
  247. package/src/monitor/message-handler.preflight.test-helpers.ts +0 -114
  248. package/src/monitor/message-handler.preflight.test.ts +0 -2255
  249. package/src/monitor/message-handler.preflight.ts +0 -822
  250. package/src/monitor/message-handler.preflight.types.ts +0 -115
  251. package/src/monitor/message-handler.process.test.ts +0 -2520
  252. package/src/monitor/message-handler.process.ts +0 -1027
  253. package/src/monitor/message-handler.queue.test.ts +0 -680
  254. package/src/monitor/message-handler.routing-preflight.ts +0 -112
  255. package/src/monitor/message-handler.test-harness.ts +0 -99
  256. package/src/monitor/message-handler.test-helpers.ts +0 -75
  257. package/src/monitor/message-handler.ts +0 -309
  258. package/src/monitor/message-media.ts +0 -536
  259. package/src/monitor/message-run-queue.ts +0 -101
  260. package/src/monitor/message-text.ts +0 -171
  261. package/src/monitor/message-utils.test.ts +0 -1234
  262. package/src/monitor/message-utils.ts +0 -34
  263. package/src/monitor/model-picker-preferences.test.ts +0 -67
  264. package/src/monitor/model-picker-preferences.ts +0 -184
  265. package/src/monitor/model-picker.state.ts +0 -364
  266. package/src/monitor/model-picker.test-utils.ts +0 -26
  267. package/src/monitor/model-picker.test.ts +0 -869
  268. package/src/monitor/model-picker.ts +0 -38
  269. package/src/monitor/model-picker.view.ts +0 -722
  270. package/src/monitor/monitor.agent-components.test.ts +0 -410
  271. package/src/monitor/monitor.test.ts +0 -919
  272. package/src/monitor/monitor.threading-utils.test.ts +0 -614
  273. package/src/monitor/native-command-agent-reply.ts +0 -125
  274. package/src/monitor/native-command-arg-ui.ts +0 -233
  275. package/src/monitor/native-command-auth.ts +0 -309
  276. package/src/monitor/native-command-bypass.ts +0 -13
  277. package/src/monitor/native-command-context.test.ts +0 -105
  278. package/src/monitor/native-command-context.ts +0 -109
  279. package/src/monitor/native-command-dispatch.ts +0 -35
  280. package/src/monitor/native-command-model-picker-apply.ts +0 -209
  281. package/src/monitor/native-command-model-picker-interaction.ts +0 -516
  282. package/src/monitor/native-command-model-picker-ui.ts +0 -357
  283. package/src/monitor/native-command-reply.test.ts +0 -68
  284. package/src/monitor/native-command-reply.ts +0 -185
  285. package/src/monitor/native-command-route.ts +0 -91
  286. package/src/monitor/native-command-status.ts +0 -76
  287. package/src/monitor/native-command-ui.ts +0 -26
  288. package/src/monitor/native-command-ui.types.ts +0 -20
  289. package/src/monitor/native-command.args.ts +0 -45
  290. package/src/monitor/native-command.command-arg.test.ts +0 -108
  291. package/src/monitor/native-command.commands-allowfrom.test.ts +0 -504
  292. package/src/monitor/native-command.model-picker.test.ts +0 -930
  293. package/src/monitor/native-command.options.test.ts +0 -379
  294. package/src/monitor/native-command.options.ts +0 -153
  295. package/src/monitor/native-command.plugin-dispatch.test.ts +0 -1212
  296. package/src/monitor/native-command.runtime.ts +0 -51
  297. package/src/monitor/native-command.status-direct.test.ts +0 -278
  298. package/src/monitor/native-command.test-helpers.ts +0 -64
  299. package/src/monitor/native-command.think-autocomplete.test.ts +0 -411
  300. package/src/monitor/native-command.ts +0 -747
  301. package/src/monitor/native-command.types.ts +0 -9
  302. package/src/monitor/native-interaction-channel-context.ts +0 -50
  303. package/src/monitor/preflight-audio.runtime.ts +0 -9
  304. package/src/monitor/preflight-audio.test.ts +0 -157
  305. package/src/monitor/preflight-audio.ts +0 -130
  306. package/src/monitor/presence-cache.ts +0 -61
  307. package/src/monitor/presence.test.ts +0 -61
  308. package/src/monitor/presence.ts +0 -50
  309. package/src/monitor/provider-session.runtime.ts +0 -12
  310. package/src/monitor/provider.acp.ts +0 -89
  311. package/src/monitor/provider.allowlist.test.ts +0 -217
  312. package/src/monitor/provider.allowlist.ts +0 -398
  313. package/src/monitor/provider.cleanup.ts +0 -41
  314. package/src/monitor/provider.commands.ts +0 -129
  315. package/src/monitor/provider.config-log.ts +0 -45
  316. package/src/monitor/provider.deploy-errors.ts +0 -362
  317. package/src/monitor/provider.deploy.ts +0 -221
  318. package/src/monitor/provider.interactions.ts +0 -160
  319. package/src/monitor/provider.lifecycle.test.ts +0 -734
  320. package/src/monitor/provider.lifecycle.ts +0 -562
  321. package/src/monitor/provider.proxy.test.ts +0 -804
  322. package/src/monitor/provider.rest-proxy.test.ts +0 -389
  323. package/src/monitor/provider.runtime.ts +0 -1
  324. package/src/monitor/provider.skill-dedupe.test.ts +0 -42
  325. package/src/monitor/provider.startup-log.ts +0 -32
  326. package/src/monitor/provider.startup.test.ts +0 -440
  327. package/src/monitor/provider.startup.ts +0 -323
  328. package/src/monitor/provider.test.ts +0 -1173
  329. package/src/monitor/provider.ts +0 -688
  330. package/src/monitor/reply-context.ts +0 -64
  331. package/src/monitor/reply-delivery.test.ts +0 -474
  332. package/src/monitor/reply-delivery.ts +0 -212
  333. package/src/monitor/reply-safety.ts +0 -96
  334. package/src/monitor/rest-fetch.ts +0 -94
  335. package/src/monitor/route-resolution.test.ts +0 -209
  336. package/src/monitor/route-resolution.ts +0 -140
  337. package/src/monitor/sender-identity.ts +0 -81
  338. package/src/monitor/startup-status.test.ts +0 -30
  339. package/src/monitor/startup-status.ts +0 -10
  340. package/src/monitor/status.ts +0 -22
  341. package/src/monitor/system-events.ts +0 -55
  342. package/src/monitor/thread-bindings.config.ts +0 -35
  343. package/src/monitor/thread-bindings.discord-api.test.ts +0 -250
  344. package/src/monitor/thread-bindings.discord-api.ts +0 -310
  345. package/src/monitor/thread-bindings.lifecycle.test.ts +0 -1994
  346. package/src/monitor/thread-bindings.lifecycle.ts +0 -354
  347. package/src/monitor/thread-bindings.manager.ts +0 -551
  348. package/src/monitor/thread-bindings.messages.ts +0 -6
  349. package/src/monitor/thread-bindings.persona.test.ts +0 -34
  350. package/src/monitor/thread-bindings.persona.ts +0 -25
  351. package/src/monitor/thread-bindings.session-adapter.ts +0 -229
  352. package/src/monitor/thread-bindings.session-shared.ts +0 -59
  353. package/src/monitor/thread-bindings.session-updates.ts +0 -35
  354. package/src/monitor/thread-bindings.shared-state.test.ts +0 -39
  355. package/src/monitor/thread-bindings.state.ts +0 -540
  356. package/src/monitor/thread-bindings.ts +0 -48
  357. package/src/monitor/thread-bindings.types.ts +0 -83
  358. package/src/monitor/thread-channel-context.ts +0 -112
  359. package/src/monitor/thread-session-close.test.ts +0 -180
  360. package/src/monitor/thread-session-close.ts +0 -63
  361. package/src/monitor/thread-title.generate.test.ts +0 -209
  362. package/src/monitor/thread-title.test.ts +0 -31
  363. package/src/monitor/thread-title.ts +0 -181
  364. package/src/monitor/threading.auto-thread.test.ts +0 -330
  365. package/src/monitor/threading.auto-thread.ts +0 -287
  366. package/src/monitor/threading.cache.ts +0 -45
  367. package/src/monitor/threading.parent-info.test.ts +0 -156
  368. package/src/monitor/threading.starter.test.ts +0 -279
  369. package/src/monitor/threading.starter.ts +0 -288
  370. package/src/monitor/threading.ts +0 -20
  371. package/src/monitor/threading.types.ts +0 -102
  372. package/src/monitor/timeouts.ts +0 -84
  373. package/src/monitor/typing.test.ts +0 -42
  374. package/src/monitor/typing.ts +0 -17
  375. package/src/monitor.gateway.test.ts +0 -187
  376. package/src/monitor.gateway.ts +0 -75
  377. package/src/monitor.test.ts +0 -1416
  378. package/src/monitor.ts +0 -28
  379. package/src/network-config.test.ts +0 -92
  380. package/src/network-config.ts +0 -79
  381. package/src/normalize.test.ts +0 -56
  382. package/src/normalize.ts +0 -86
  383. package/src/outbound-adapter.interactive-order.test.ts +0 -82
  384. package/src/outbound-adapter.test-harness.ts +0 -207
  385. package/src/outbound-adapter.test.ts +0 -804
  386. package/src/outbound-adapter.ts +0 -326
  387. package/src/outbound-approval.ts +0 -29
  388. package/src/outbound-components.ts +0 -86
  389. package/src/outbound-payload.contract.test.ts +0 -49
  390. package/src/outbound-payload.ts +0 -208
  391. package/src/outbound-send-context.ts +0 -89
  392. package/src/outbound-session-route.test.ts +0 -42
  393. package/src/outbound-session-route.ts +0 -72
  394. package/src/pluralkit.test.ts +0 -67
  395. package/src/pluralkit.ts +0 -58
  396. package/src/preview-streaming.ts +0 -18
  397. package/src/probe.intents.test.ts +0 -94
  398. package/src/probe.parse-token.test.ts +0 -43
  399. package/src/probe.runtime.ts +0 -1
  400. package/src/probe.ts +0 -237
  401. package/src/proxy-fetch.ts +0 -92
  402. package/src/proxy-request-client.test.ts +0 -100
  403. package/src/proxy-request-client.ts +0 -21
  404. package/src/recipient-resolution.ts +0 -39
  405. package/src/resolve-allowlist-common.test.ts +0 -40
  406. package/src/resolve-allowlist-common.ts +0 -39
  407. package/src/resolve-channels.test.ts +0 -341
  408. package/src/resolve-channels.ts +0 -369
  409. package/src/resolve-users.test.ts +0 -243
  410. package/src/resolve-users.ts +0 -184
  411. package/src/retry.test.ts +0 -83
  412. package/src/retry.ts +0 -98
  413. package/src/runtime-api.ts +0 -61
  414. package/src/runtime-config.ts +0 -16
  415. package/src/runtime.ts +0 -23
  416. package/src/secret-config-contract.ts +0 -140
  417. package/src/security-audit.runtime.ts +0 -1
  418. package/src/security-audit.test.ts +0 -245
  419. package/src/security-audit.ts +0 -208
  420. package/src/security-contract.ts +0 -47
  421. package/src/security-doctor.test.ts +0 -25
  422. package/src/security-doctor.ts +0 -20
  423. package/src/security.ts +0 -60
  424. package/src/send-target-parsing.ts +0 -14
  425. package/src/send.channels.ts +0 -139
  426. package/src/send.components.test.ts +0 -330
  427. package/src/send.components.ts +0 -391
  428. package/src/send.creates-thread.test.ts +0 -681
  429. package/src/send.emojis-stickers.ts +0 -57
  430. package/src/send.guild.ts +0 -170
  431. package/src/send.message-request.ts +0 -112
  432. package/src/send.messages.test.ts +0 -59
  433. package/src/send.messages.ts +0 -229
  434. package/src/send.outbound.ts +0 -459
  435. package/src/send.permissions.authz.test.ts +0 -190
  436. package/src/send.permissions.ts +0 -283
  437. package/src/send.reactions.ts +0 -155
  438. package/src/send.receipt.ts +0 -69
  439. package/src/send.sends-basic-channel-messages.test.ts +0 -1068
  440. package/src/send.shared.ts +0 -469
  441. package/src/send.test-harness.ts +0 -56
  442. package/src/send.ts +0 -82
  443. package/src/send.types.ts +0 -191
  444. package/src/send.typing.test.ts +0 -41
  445. package/src/send.typing.ts +0 -9
  446. package/src/send.voice.ts +0 -136
  447. package/src/send.webhook-activity.test.ts +0 -152
  448. package/src/send.webhook.proxy.test.ts +0 -210
  449. package/src/send.webhook.ts +0 -137
  450. package/src/session-contract.ts +0 -3
  451. package/src/session-key-normalization.test.ts +0 -44
  452. package/src/session-key-normalization.ts +0 -47
  453. package/src/setup-account-state.test.ts +0 -113
  454. package/src/setup-account-state.ts +0 -141
  455. package/src/setup-adapter.ts +0 -14
  456. package/src/setup-core.ts +0 -215
  457. package/src/setup-runtime-helpers.ts +0 -10
  458. package/src/setup-surface.test.ts +0 -137
  459. package/src/setup-surface.ts +0 -132
  460. package/src/shared-interactive.test.ts +0 -153
  461. package/src/shared-interactive.ts +0 -161
  462. package/src/shared.test.ts +0 -186
  463. package/src/shared.ts +0 -197
  464. package/src/status-issues.test.ts +0 -97
  465. package/src/status-issues.ts +0 -198
  466. package/src/subagent-hooks.test.ts +0 -465
  467. package/src/subagent-hooks.ts +0 -232
  468. package/src/target-parsing.ts +0 -70
  469. package/src/target-resolver.ts +0 -129
  470. package/src/targets.test.ts +0 -393
  471. package/src/targets.ts +0 -12
  472. package/src/test-http-helpers.ts +0 -10
  473. package/src/test-support/component-runtime.ts +0 -194
  474. package/src/test-support/config.ts +0 -7
  475. package/src/test-support/configured-binding-runtime.ts +0 -29
  476. package/src/test-support/partial-channel.ts +0 -26
  477. package/src/test-support/provider.test-support.ts +0 -547
  478. package/src/token.test.ts +0 -174
  479. package/src/token.ts +0 -107
  480. package/src/ui-colors.ts +0 -27
  481. package/src/ui.ts +0 -20
  482. package/src/voice/access.test.ts +0 -288
  483. package/src/voice/access.ts +0 -126
  484. package/src/voice/audio.test.ts +0 -47
  485. package/src/voice/audio.ts +0 -249
  486. package/src/voice/capture-state.test.ts +0 -48
  487. package/src/voice/capture-state.ts +0 -120
  488. package/src/voice/command.test.ts +0 -170
  489. package/src/voice/command.ts +0 -284
  490. package/src/voice/config.ts +0 -8
  491. package/src/voice/ingress.ts +0 -164
  492. package/src/voice/manager.e2e.test.ts +0 -3286
  493. package/src/voice/manager.ready-listener.test.ts +0 -54
  494. package/src/voice/manager.runtime.ts +0 -14
  495. package/src/voice/manager.ts +0 -1155
  496. package/src/voice/prompt.test.ts +0 -30
  497. package/src/voice/prompt.ts +0 -22
  498. package/src/voice/realtime.ts +0 -1370
  499. package/src/voice/receive-recovery.test.ts +0 -81
  500. package/src/voice/receive-recovery.ts +0 -159
  501. package/src/voice/sanitize.test.ts +0 -34
  502. package/src/voice/sanitize.ts +0 -29
  503. package/src/voice/sdk-runtime.ts +0 -14
  504. package/src/voice/segment.ts +0 -160
  505. package/src/voice/session.ts +0 -81
  506. package/src/voice/speaker-context.ts +0 -127
  507. package/src/voice/tts.ts +0 -151
  508. package/src/voice-message.test.ts +0 -376
  509. package/src/voice-message.ts +0 -474
  510. package/subagent-hooks-api.ts +0 -27
  511. package/test-api.ts +0 -4
  512. package/thread-binding-api.ts +0 -1
  513. package/timeouts.ts +0 -6
  514. package/tsconfig.json +0 -16
@@ -1,2255 +0,0 @@
1
- import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
2
- import { ChannelType, MessageType } from "../internal/discord.js";
3
- import { createPartialDiscordChannelWithThrowingGetters } from "../test-support/partial-channel.js";
4
-
5
- const transcribeFirstAudioMock = vi.hoisted(() => vi.fn());
6
- const fetchPluralKitMessageInfoMock = vi.hoisted(() => vi.fn());
7
- const resolveDiscordDmCommandAccessMock = vi.hoisted(() => vi.fn());
8
- const handleDiscordDmCommandDecisionMock = vi.hoisted(() => vi.fn(async () => {}));
9
- const saveRemoteMediaMock = vi.hoisted(() => vi.fn());
10
-
11
- vi.mock("../pluralkit.js", () => ({
12
- fetchPluralKitMessageInfo: (...args: unknown[]) => fetchPluralKitMessageInfoMock(...args),
13
- }));
14
- vi.mock("./preflight-audio.runtime.js", () => ({
15
- transcribeFirstAudio: transcribeFirstAudioMock,
16
- }));
17
- vi.mock("./dm-command-auth.js", async (importOriginal) => ({
18
- ...(await importOriginal<typeof import("./dm-command-auth.js")>()),
19
- resolveDiscordDmCommandAccess: resolveDiscordDmCommandAccessMock,
20
- }));
21
- vi.mock("./dm-command-decision.js", () => ({
22
- handleDiscordDmCommandDecision: handleDiscordDmCommandDecisionMock,
23
- }));
24
- vi.mock("klaw/plugin-sdk/media-runtime", async () => {
25
- const actual = await vi.importActual<typeof import("klaw/plugin-sdk/media-runtime")>(
26
- "klaw/plugin-sdk/media-runtime",
27
- );
28
- return {
29
- ...actual,
30
- saveRemoteMedia: (...args: unknown[]) => saveRemoteMediaMock(...args),
31
- };
32
- });
33
- import {
34
- testing as sessionBindingTesting,
35
- registerSessionBindingAdapter,
36
- } from "klaw/plugin-sdk/conversation-runtime";
37
- import {
38
- createDiscordMessage,
39
- createDiscordPreflightArgs,
40
- createGuildEvent,
41
- createGuildTextClient,
42
- DEFAULT_PREFLIGHT_CFG,
43
- type DiscordClient,
44
- type DiscordConfig,
45
- type DiscordMessageEvent,
46
- } from "./message-handler.preflight.test-helpers.js";
47
- let preflightDiscordMessage: typeof import("./message-handler.preflight.js").preflightDiscordMessage;
48
- let resolvePreflightMentionRequirement: typeof import("./message-handler.preflight.js").resolvePreflightMentionRequirement;
49
- let shouldIgnoreBoundThreadWebhookMessage: typeof import("./message-handler.preflight.js").shouldIgnoreBoundThreadWebhookMessage;
50
- let threadBindingTesting: typeof import("./thread-bindings.js").testing;
51
- let createThreadBindingManager: typeof import("./thread-bindings.js").createThreadBindingManager;
52
-
53
- beforeAll(async () => {
54
- ({
55
- preflightDiscordMessage,
56
- resolvePreflightMentionRequirement,
57
- shouldIgnoreBoundThreadWebhookMessage,
58
- } = await import("./message-handler.preflight.js"));
59
- ({ testing: threadBindingTesting, createThreadBindingManager } =
60
- await import("./thread-bindings.js"));
61
- });
62
-
63
- beforeEach(() => {
64
- fetchPluralKitMessageInfoMock.mockReset();
65
- saveRemoteMediaMock.mockReset();
66
- saveRemoteMediaMock.mockImplementation(
67
- async (options: { fallbackContentType?: string; filePathHint?: string }) => ({
68
- id: "test-media",
69
- path: `/tmp/klaw-discord-test/${options.filePathHint ?? "media"}`,
70
- size: 5,
71
- contentType: options.fallbackContentType,
72
- }),
73
- );
74
- });
75
-
76
- function createThreadBinding(
77
- overrides?: Partial<import("klaw/plugin-sdk/conversation-runtime").SessionBindingRecord>,
78
- ) {
79
- return {
80
- bindingId: "default:thread-1",
81
- targetSessionKey: "agent:main:subagent:child-1",
82
- targetKind: "subagent",
83
- conversation: {
84
- channel: "discord",
85
- accountId: "default",
86
- conversationId: "thread-1",
87
- parentConversationId: "parent-1",
88
- },
89
- status: "active",
90
- boundAt: 1,
91
- metadata: {
92
- agentId: "main",
93
- boundBy: "test",
94
- webhookId: "wh-1",
95
- webhookToken: "tok-1",
96
- },
97
- ...overrides,
98
- } satisfies import("klaw/plugin-sdk/conversation-runtime").SessionBindingRecord;
99
- }
100
-
101
- function createPreflightArgs(params: {
102
- cfg: import("klaw/plugin-sdk/config-contracts").KlawConfig;
103
- discordConfig: DiscordConfig;
104
- data: DiscordMessageEvent;
105
- client: DiscordClient;
106
- }): Parameters<typeof preflightDiscordMessage>[0] {
107
- return createDiscordPreflightArgs(params);
108
- }
109
-
110
- type DiscordPreflightResult = NonNullable<Awaited<ReturnType<typeof preflightDiscordMessage>>>;
111
-
112
- function expectPreflightResult(
113
- result: Awaited<ReturnType<typeof preflightDiscordMessage>>,
114
- ): DiscordPreflightResult {
115
- if (result === null) {
116
- throw new Error("Expected Discord preflight result");
117
- }
118
- return result;
119
- }
120
-
121
- type MockWithCalls = { mock: { calls: unknown[][] } };
122
-
123
- function firstMockArg(mock: MockWithCalls, label: string) {
124
- const call = mock.mock.calls.at(0);
125
- if (!call) {
126
- throw new Error(`expected ${label} call`);
127
- }
128
- return call[0];
129
- }
130
-
131
- function createThreadClient(params: { threadId: string; parentId: string }): DiscordClient {
132
- return {
133
- fetchChannel: async (channelId: string) => {
134
- if (channelId === params.threadId) {
135
- return {
136
- id: params.threadId,
137
- type: ChannelType.PublicThread,
138
- name: "focus",
139
- parentId: params.parentId,
140
- ownerId: "owner-1",
141
- };
142
- }
143
- if (channelId === params.parentId) {
144
- return {
145
- id: params.parentId,
146
- type: ChannelType.GuildText,
147
- name: "general",
148
- };
149
- }
150
- return null;
151
- },
152
- } as unknown as DiscordClient;
153
- }
154
-
155
- function createDmClient(channelId: string): DiscordClient {
156
- return {
157
- fetchChannel: async (id: string) => {
158
- if (id === channelId) {
159
- return {
160
- id: channelId,
161
- type: ChannelType.DM,
162
- };
163
- }
164
- return null;
165
- },
166
- } as unknown as DiscordClient;
167
- }
168
-
169
- function createMissingChannelClient(): DiscordClient {
170
- return {
171
- fetchChannel: async () => null,
172
- } as unknown as DiscordClient;
173
- }
174
-
175
- async function runThreadBoundPreflight(params: {
176
- threadId: string;
177
- parentId: string;
178
- message: import("../internal/discord.js").Message;
179
- threadBinding: import("klaw/plugin-sdk/conversation-runtime").SessionBindingRecord;
180
- discordConfig: DiscordConfig;
181
- registerBindingAdapter?: boolean;
182
- }) {
183
- if (params.registerBindingAdapter) {
184
- registerSessionBindingAdapter({
185
- channel: "discord",
186
- accountId: "default",
187
- listBySession: () => [],
188
- resolveByConversation: (ref) =>
189
- ref.conversationId === params.threadId ? params.threadBinding : null,
190
- });
191
- }
192
-
193
- const client = createThreadClient({
194
- threadId: params.threadId,
195
- parentId: params.parentId,
196
- });
197
-
198
- return preflightDiscordMessage({
199
- ...createPreflightArgs({
200
- cfg: DEFAULT_PREFLIGHT_CFG,
201
- discordConfig: params.discordConfig,
202
- data: createGuildEvent({
203
- channelId: params.threadId,
204
- guildId: "guild-1",
205
- author: params.message.author,
206
- message: params.message,
207
- }),
208
- client,
209
- }),
210
- threadBindings: {
211
- getByThreadId: (id: string) => (id === params.threadId ? params.threadBinding : undefined),
212
- } as import("./thread-bindings.js").ThreadBindingManager,
213
- });
214
- }
215
-
216
- async function runGuildPreflight(params: {
217
- channelId: string;
218
- guildId: string;
219
- message: import("../internal/discord.js").Message;
220
- discordConfig: DiscordConfig;
221
- cfg?: import("klaw/plugin-sdk/config-contracts").KlawConfig;
222
- guildEntries?: Parameters<typeof preflightDiscordMessage>[0]["guildEntries"];
223
- includeGuildObject?: boolean;
224
- }) {
225
- return preflightDiscordMessage({
226
- ...createPreflightArgs({
227
- cfg: params.cfg ?? DEFAULT_PREFLIGHT_CFG,
228
- discordConfig: params.discordConfig,
229
- data: createGuildEvent({
230
- channelId: params.channelId,
231
- guildId: params.guildId,
232
- author: params.message.author,
233
- message: params.message,
234
- includeGuildObject: params.includeGuildObject,
235
- }),
236
- client: createGuildTextClient(params.channelId),
237
- }),
238
- guildEntries: params.guildEntries,
239
- });
240
- }
241
-
242
- async function runDmPreflight(params: {
243
- channelId: string;
244
- message: import("../internal/discord.js").Message;
245
- discordConfig: DiscordConfig;
246
- }) {
247
- return preflightDiscordMessage({
248
- ...createPreflightArgs({
249
- cfg: DEFAULT_PREFLIGHT_CFG,
250
- discordConfig: params.discordConfig,
251
- data: {
252
- channel_id: params.channelId,
253
- author: params.message.author,
254
- message: params.message,
255
- } as DiscordMessageEvent,
256
- client: createDmClient(params.channelId),
257
- }),
258
- });
259
- }
260
-
261
- async function runUnresolvedDmPreflight(params: {
262
- cfg?: import("klaw/plugin-sdk/config-contracts").KlawConfig;
263
- channelId: string;
264
- message: import("../internal/discord.js").Message;
265
- discordConfig: DiscordConfig;
266
- }) {
267
- return preflightDiscordMessage({
268
- ...createPreflightArgs({
269
- cfg: params.cfg ?? DEFAULT_PREFLIGHT_CFG,
270
- discordConfig: params.discordConfig,
271
- data: {
272
- channel_id: params.channelId,
273
- author: params.message.author,
274
- message: params.message,
275
- } as DiscordMessageEvent,
276
- client: createMissingChannelClient(),
277
- }),
278
- });
279
- }
280
-
281
- async function runMentionOnlyBotPreflight(params: {
282
- channelId: string;
283
- guildId: string;
284
- message: import("../internal/discord.js").Message;
285
- }) {
286
- return runGuildPreflight({
287
- channelId: params.channelId,
288
- guildId: params.guildId,
289
- message: params.message,
290
- discordConfig: {
291
- allowBots: "mentions",
292
- } as DiscordConfig,
293
- });
294
- }
295
-
296
- async function runIgnoreOtherMentionsPreflight(params: {
297
- channelId: string;
298
- guildId: string;
299
- message: import("../internal/discord.js").Message;
300
- }) {
301
- return runGuildPreflight({
302
- channelId: params.channelId,
303
- guildId: params.guildId,
304
- message: params.message,
305
- discordConfig: {} as DiscordConfig,
306
- guildEntries: {
307
- [params.guildId]: {
308
- requireMention: false,
309
- ignoreOtherMentions: true,
310
- },
311
- },
312
- });
313
- }
314
-
315
- describe("resolvePreflightMentionRequirement", () => {
316
- it("requires mention when config requires mention and thread is not bound", () => {
317
- expect(
318
- resolvePreflightMentionRequirement({
319
- shouldRequireMention: true,
320
- bypassMentionRequirement: false,
321
- }),
322
- ).toBe(true);
323
- });
324
-
325
- it("disables mention requirement when the route explicitly bypasses mentions", () => {
326
- expect(
327
- resolvePreflightMentionRequirement({
328
- shouldRequireMention: true,
329
- bypassMentionRequirement: true,
330
- }),
331
- ).toBe(false);
332
- });
333
-
334
- it("keeps mention requirement disabled when config already disables it", () => {
335
- expect(
336
- resolvePreflightMentionRequirement({
337
- shouldRequireMention: false,
338
- bypassMentionRequirement: false,
339
- }),
340
- ).toBe(false);
341
- });
342
- });
343
-
344
- describe("preflightDiscordMessage", () => {
345
- beforeEach(() => {
346
- sessionBindingTesting.resetSessionBindingAdaptersForTests();
347
- transcribeFirstAudioMock.mockReset();
348
- resolveDiscordDmCommandAccessMock.mockReset();
349
- resolveDiscordDmCommandAccessMock.mockResolvedValue({
350
- senderAccess: {
351
- allowed: true,
352
- decision: "allow",
353
- reasonCode: "dm_policy_allowlisted",
354
- },
355
- commandAccess: {
356
- authorized: true,
357
- },
358
- });
359
- handleDiscordDmCommandDecisionMock.mockReset();
360
- handleDiscordDmCommandDecisionMock.mockResolvedValue(undefined);
361
- });
362
-
363
- it("drops bound-thread bot system messages to prevent ACP self-loop", async () => {
364
- const threadBinding = createThreadBinding({
365
- targetKind: "session",
366
- targetSessionKey: "agent:main:acp:discord-thread-1",
367
- });
368
- const threadId = "thread-system-1";
369
- const parentId = "channel-parent-1";
370
- const message = createDiscordMessage({
371
- id: "m-system-1",
372
- channelId: threadId,
373
- content:
374
- "⚙️ codex-acp session active (auto-unfocus in 24h). Messages here go directly to this session.",
375
- author: {
376
- id: "relay-bot-1",
377
- bot: true,
378
- username: "Klaw",
379
- },
380
- });
381
-
382
- const result = await runThreadBoundPreflight({
383
- threadId,
384
- parentId,
385
- message,
386
- threadBinding,
387
- discordConfig: {
388
- allowBots: true,
389
- } as DiscordConfig,
390
- });
391
-
392
- expect(result).toBeNull();
393
- });
394
-
395
- it("restores direct-message bindings by user target instead of DM channel id", async () => {
396
- registerSessionBindingAdapter({
397
- channel: "discord",
398
- accountId: "default",
399
- listBySession: () => [],
400
- resolveByConversation: (ref) =>
401
- ref.conversationId === "user:user-1"
402
- ? createThreadBinding({
403
- conversation: {
404
- channel: "discord",
405
- accountId: "default",
406
- conversationId: "user:user-1",
407
- },
408
- metadata: {
409
- pluginBindingOwner: "plugin",
410
- pluginId: "klaw-codex-app-server",
411
- pluginRoot: "/Users/huntharo/github/klaw-app-server",
412
- },
413
- })
414
- : null,
415
- });
416
-
417
- const result = await runDmPreflight({
418
- channelId: "dm-channel-1",
419
- message: createDiscordMessage({
420
- id: "m-dm-1",
421
- channelId: "dm-channel-1",
422
- content: "who are you",
423
- author: {
424
- id: "user-1",
425
- bot: false,
426
- username: "alice",
427
- },
428
- }),
429
- discordConfig: {
430
- allowBots: true,
431
- dmPolicy: "open",
432
- } as DiscordConfig,
433
- });
434
-
435
- const preflight = expectPreflightResult(result);
436
- expect(preflight.threadBinding).toEqual({
437
- bindingId: "default:thread-1",
438
- targetSessionKey: "agent:main:subagent:child-1",
439
- targetKind: "subagent",
440
- conversation: {
441
- channel: "discord",
442
- accountId: "default",
443
- conversationId: "user:user-1",
444
- },
445
- status: "active",
446
- boundAt: 1,
447
- metadata: {
448
- pluginBindingOwner: "plugin",
449
- pluginId: "klaw-codex-app-server",
450
- pluginRoot: "/Users/huntharo/github/klaw-app-server",
451
- },
452
- });
453
- });
454
-
455
- it("ignores stale route-shaped channel bindings when config now routes to another agent", async () => {
456
- const channelId = "channel-stale-route";
457
- registerSessionBindingAdapter({
458
- channel: "discord",
459
- accountId: "default",
460
- listBySession: () => [],
461
- resolveByConversation: (ref) =>
462
- ref.conversationId === channelId
463
- ? createThreadBinding({
464
- bindingId: "default:channel-stale-route",
465
- targetKind: "session",
466
- targetSessionKey: `agent:oldagent:discord:channel:${channelId}`,
467
- conversation: {
468
- channel: "discord",
469
- accountId: "default",
470
- conversationId: channelId,
471
- },
472
- metadata: undefined,
473
- })
474
- : null,
475
- });
476
-
477
- const result = await runGuildPreflight({
478
- channelId,
479
- guildId: "guild-stale-route",
480
- message: createDiscordMessage({
481
- id: "m-stale-route",
482
- channelId,
483
- content: "which agent is this?",
484
- author: {
485
- id: "user-1",
486
- bot: false,
487
- username: "alice",
488
- },
489
- }),
490
- cfg: {
491
- agents: {
492
- list: [{ id: "newagent" }],
493
- },
494
- bindings: [
495
- {
496
- agentId: "newagent",
497
- match: {
498
- channel: "discord",
499
- accountId: "default",
500
- peer: { kind: "channel", id: channelId },
501
- },
502
- },
503
- ],
504
- channels: {
505
- discord: {},
506
- },
507
- },
508
- discordConfig: {
509
- allowBots: true,
510
- } as DiscordConfig,
511
- guildEntries: {
512
- "guild-stale-route": {
513
- channels: {
514
- [channelId]: {
515
- enabled: true,
516
- requireMention: false,
517
- },
518
- },
519
- },
520
- },
521
- });
522
-
523
- const preflight = expectPreflightResult(result);
524
- expect(preflight.route.agentId).toBe("newagent");
525
- expect(preflight.route.sessionKey).toBe(`agent:newagent:discord:channel:${channelId}`);
526
- expect(preflight.boundSessionKey).toBeUndefined();
527
- expect(preflight.threadBinding).toBeUndefined();
528
- });
529
-
530
- it("preflights direct-message voice notes without mention gating", async () => {
531
- transcribeFirstAudioMock.mockResolvedValue("hello klaw from dm audio");
532
-
533
- const result = await runDmPreflight({
534
- channelId: "dm-channel-audio-1",
535
- message: createDiscordMessage({
536
- id: "m-dm-audio-1",
537
- channelId: "dm-channel-audio-1",
538
- content: "",
539
- attachments: [
540
- {
541
- id: "att-dm-audio-1",
542
- url: "https://cdn.discordapp.com/attachments/voice.ogg",
543
- content_type: "audio/ogg",
544
- filename: "voice.ogg",
545
- },
546
- ],
547
- author: {
548
- id: "user-1",
549
- bot: false,
550
- username: "alice",
551
- },
552
- }),
553
- discordConfig: {
554
- dmPolicy: "open",
555
- } as DiscordConfig,
556
- });
557
-
558
- expect(transcribeFirstAudioMock).toHaveBeenCalledTimes(1);
559
- const dmAudioCall = firstMockArg(transcribeFirstAudioMock, "transcribeFirstAudio") as
560
- | { ctx?: { MediaUrls?: unknown; MediaTypes?: unknown } }
561
- | undefined;
562
- expect(dmAudioCall?.ctx?.MediaUrls).toEqual([
563
- "https://cdn.discordapp.com/attachments/voice.ogg",
564
- ]);
565
- expect(dmAudioCall?.ctx?.MediaTypes).toEqual(["audio/ogg"]);
566
- const preflight = expectPreflightResult(result);
567
- expect(preflight.isDirectMessage).toBe(true);
568
- expect(preflight.preflightAudioTranscript).toBe("hello klaw from dm audio");
569
- });
570
-
571
- it("keeps no-guild messages direct when channel lookup is unavailable", async () => {
572
- const result = await runUnresolvedDmPreflight({
573
- cfg: {
574
- ...DEFAULT_PREFLIGHT_CFG,
575
- session: {
576
- ...DEFAULT_PREFLIGHT_CFG.session,
577
- dmScope: "per-channel-peer",
578
- },
579
- },
580
- channelId: "dm-channel-unresolved-1",
581
- message: createDiscordMessage({
582
- id: "m-dm-unresolved-1",
583
- channelId: "dm-channel-unresolved-1",
584
- content: "hello from a degraded dm",
585
- author: {
586
- id: "user-1",
587
- bot: false,
588
- username: "alice",
589
- },
590
- }),
591
- discordConfig: {
592
- dmPolicy: "open",
593
- } as DiscordConfig,
594
- });
595
-
596
- const preflight = expectPreflightResult(result);
597
- expect(preflight.channelInfo).toBeNull();
598
- expect(preflight.isDirectMessage).toBe(true);
599
- expect(preflight.isGroupDm).toBe(false);
600
- expect(preflight.route.sessionKey).toBe("agent:main:discord:direct:user-1");
601
- });
602
-
603
- it("falls back to the default discord account for omitted-account dm authorization", async () => {
604
- const message = createDiscordMessage({
605
- id: "m-dm-default-account",
606
- channelId: "dm-channel-default-account",
607
- content: "who are you",
608
- author: {
609
- id: "user-1",
610
- bot: false,
611
- username: "alice",
612
- },
613
- });
614
-
615
- await preflightDiscordMessage({
616
- ...createPreflightArgs({
617
- cfg: {
618
- ...DEFAULT_PREFLIGHT_CFG,
619
- channels: {
620
- discord: {
621
- defaultAccount: "work",
622
- accounts: {
623
- default: {
624
- token: "token-default",
625
- },
626
- work: {
627
- token: "token-work",
628
- },
629
- },
630
- },
631
- },
632
- },
633
- discordConfig: {
634
- defaultAccount: "work",
635
- dmPolicy: "allowlist",
636
- } as DiscordConfig,
637
- data: {
638
- channel_id: "dm-channel-default-account",
639
- author: message.author,
640
- message,
641
- } as DiscordMessageEvent,
642
- client: createDmClient("dm-channel-default-account"),
643
- }),
644
- });
645
-
646
- expect(resolveDiscordDmCommandAccessMock).toHaveBeenCalledTimes(1);
647
- expect(
648
- (
649
- firstMockArg(resolveDiscordDmCommandAccessMock, "resolveDiscordDmCommandAccess") as
650
- | { accountId?: unknown }
651
- | undefined
652
- )?.accountId,
653
- ).toBe("default");
654
- });
655
-
656
- it("passes bot-loop protection facts for accepted bot-authored Discord messages (#58789)", async () => {
657
- const channelId = "channel-bot-loop";
658
- const guildId = "guild-bot-loop";
659
- const senderBotId = "relay-bot-1";
660
- const messageTimestamp = "2026-05-13T05:00:00.000Z";
661
-
662
- const message = createDiscordMessage({
663
- id: "m-loop-1",
664
- channelId,
665
- content: "chatter <@klaw-bot>",
666
- mentionedUsers: [{ id: "klaw-bot" }],
667
- author: { id: senderBotId, bot: true, username: "Relay" },
668
- timestamp: messageTimestamp,
669
- });
670
- const result = await preflightDiscordMessage(
671
- createPreflightArgs({
672
- cfg: DEFAULT_PREFLIGHT_CFG,
673
- discordConfig: {
674
- allowBots: true,
675
- botLoopProtection: {
676
- enabled: true,
677
- maxEventsPerWindow: 3,
678
- cooldownSeconds: 60,
679
- },
680
- } as DiscordConfig,
681
- data: createGuildEvent({
682
- channelId,
683
- guildId,
684
- author: message.author,
685
- message,
686
- }),
687
- client: createGuildTextClient(channelId),
688
- }),
689
- );
690
-
691
- expect(expectPreflightResult(result).botLoopProtection).toEqual({
692
- scopeId: "default",
693
- conversationId: channelId,
694
- senderId: senderBotId,
695
- receiverId: "klaw-bot",
696
- config: {
697
- enabled: true,
698
- maxEventsPerWindow: 3,
699
- cooldownSeconds: 60,
700
- },
701
- defaultsConfig: undefined,
702
- defaultEnabled: true,
703
- nowMs: Date.parse(messageTimestamp),
704
- });
705
- });
706
-
707
- it("passes generic channel defaults for Discord bot loop budgets", async () => {
708
- const channelId = "channel-bot-loop-defaults";
709
- const guildId = "guild-bot-loop-defaults";
710
- const discordConfig = { allowBots: true } as DiscordConfig;
711
- const message = createDiscordMessage({
712
- id: "m-loop-default-1",
713
- channelId,
714
- content: "relay <@klaw-bot>",
715
- mentionedUsers: [{ id: "klaw-bot" }],
716
- author: { id: "relay-bot-defaults", bot: true, username: "Relay" },
717
- });
718
- const result = await runGuildPreflight({
719
- channelId,
720
- guildId,
721
- message,
722
- discordConfig,
723
- cfg: {
724
- ...DEFAULT_PREFLIGHT_CFG,
725
- channels: {
726
- defaults: {
727
- botLoopProtection: {
728
- maxEventsPerWindow: 1,
729
- cooldownSeconds: 60,
730
- },
731
- },
732
- },
733
- },
734
- });
735
-
736
- expect(expectPreflightResult(result).botLoopProtection?.defaultsConfig).toEqual({
737
- maxEventsPerWindow: 1,
738
- cooldownSeconds: 60,
739
- });
740
- });
741
-
742
- it("does not prepare loop-guard facts for bot messages that later preflight gates drop (#58789)", async () => {
743
- const channelId = "channel-bot-loop-dropped";
744
- const guildId = "guild-bot-loop-dropped";
745
- const senderBotId = "relay-bot-dropped";
746
- const discordConfig = {
747
- allowBots: true,
748
- botLoopProtection: {
749
- enabled: true,
750
- maxEventsPerWindow: 1,
751
- cooldownSeconds: 60,
752
- },
753
- } as DiscordConfig;
754
- const guildEntries = {
755
- [guildId]: {
756
- requireMention: false,
757
- ignoreOtherMentions: true,
758
- },
759
- };
760
-
761
- for (const messageId of ["m-dropped-1", "m-dropped-2"]) {
762
- const message = createDiscordMessage({
763
- id: messageId,
764
- channelId,
765
- content: `cc <@999> ${messageId}`,
766
- mentionedUsers: [{ id: "999" }],
767
- author: { id: senderBotId, bot: true, username: "Relay" },
768
- });
769
-
770
- expect(
771
- await runGuildPreflight({
772
- channelId,
773
- guildId,
774
- message,
775
- discordConfig,
776
- guildEntries,
777
- }),
778
- ).toBeNull();
779
- }
780
-
781
- const validMessage = createDiscordMessage({
782
- id: "m-valid-after-dropped",
783
- channelId,
784
- content: "legitimate bot relay",
785
- author: { id: senderBotId, bot: true, username: "Relay" },
786
- });
787
-
788
- expect(
789
- await runGuildPreflight({
790
- channelId,
791
- guildId,
792
- message: validMessage,
793
- discordConfig,
794
- guildEntries,
795
- }),
796
- ).not.toBeNull();
797
- });
798
-
799
- it("keeps bound-thread regular bot messages flowing when allowBots=true", async () => {
800
- const threadBinding = createThreadBinding({
801
- targetKind: "session",
802
- targetSessionKey: "agent:main:acp:discord-thread-1",
803
- });
804
- const threadId = "thread-bot-regular-1";
805
- const parentId = "channel-parent-regular-1";
806
- const message = createDiscordMessage({
807
- id: "m-bot-regular-1",
808
- channelId: threadId,
809
- content: "here is tool output chunk",
810
- author: {
811
- id: "relay-bot-1",
812
- bot: true,
813
- username: "Relay",
814
- },
815
- });
816
-
817
- const result = await runThreadBoundPreflight({
818
- threadId,
819
- parentId,
820
- message,
821
- threadBinding,
822
- discordConfig: {
823
- allowBots: true,
824
- } as DiscordConfig,
825
- registerBindingAdapter: true,
826
- });
827
-
828
- expect(expectPreflightResult(result).boundSessionKey).toBe(threadBinding.targetSessionKey);
829
- });
830
-
831
- it("drops hydrated bound-thread webhook copies after fetching an empty payload", async () => {
832
- const threadBinding = createThreadBinding({
833
- targetKind: "session",
834
- targetSessionKey: "agent:main:acp:discord-thread-1",
835
- });
836
- const threadId = "thread-webhook-hydrated-1";
837
- const parentId = "channel-parent-webhook-hydrated-1";
838
- const message = createDiscordMessage({
839
- id: "m-webhook-hydrated-1",
840
- channelId: threadId,
841
- content: "",
842
- author: {
843
- id: "relay-bot-1",
844
- bot: true,
845
- username: "Relay",
846
- },
847
- });
848
- const restGet = vi.fn(async () => ({
849
- id: message.id,
850
- content: "webhook relay",
851
- webhook_id: "wh-1",
852
- attachments: [],
853
- embeds: [],
854
- mentions: [],
855
- mention_roles: [],
856
- mention_everyone: false,
857
- author: {
858
- id: "relay-bot-1",
859
- username: "Relay",
860
- bot: true,
861
- },
862
- }));
863
- const client = Object.assign(createThreadClient({ threadId, parentId }), {
864
- rest: {
865
- get: restGet,
866
- },
867
- }) as unknown as DiscordClient;
868
-
869
- const result = await preflightDiscordMessage({
870
- ...createPreflightArgs({
871
- cfg: DEFAULT_PREFLIGHT_CFG,
872
- discordConfig: {
873
- allowBots: true,
874
- } as DiscordConfig,
875
- data: createGuildEvent({
876
- channelId: threadId,
877
- guildId: "guild-1",
878
- author: message.author,
879
- message,
880
- }),
881
- client,
882
- }),
883
- threadBindings: {
884
- getByThreadId: (id: string) => (id === threadId ? threadBinding : undefined),
885
- } as import("./thread-bindings.js").ThreadBindingManager,
886
- });
887
-
888
- expect(restGet).toHaveBeenCalledTimes(1);
889
- expect(result).toBeNull();
890
- });
891
-
892
- it("drops bound-thread webhook copies from other webhook ids", async () => {
893
- const threadBinding = createThreadBinding({
894
- targetKind: "session",
895
- targetSessionKey: "agent:main:acp:discord-thread-1",
896
- });
897
- const threadId = "thread-webhook-proxy-1";
898
- const parentId = "channel-parent-webhook-proxy-1";
899
- const message = createDiscordMessage({
900
- id: "m-webhook-proxy-1",
901
- channelId: threadId,
902
- content: "proxied user message",
903
- webhookId: "pluralkit-webhook-1",
904
- author: {
905
- id: "relay-bot-1",
906
- bot: true,
907
- username: "Proxy",
908
- },
909
- });
910
-
911
- const result = await runThreadBoundPreflight({
912
- threadId,
913
- parentId,
914
- message,
915
- threadBinding,
916
- discordConfig: {
917
- allowBots: true,
918
- } as DiscordConfig,
919
- });
920
-
921
- expect(result).toBeNull();
922
- });
923
-
924
- it("canonicalizes PluralKit webhook messages to the original Discord message id", async () => {
925
- fetchPluralKitMessageInfoMock.mockResolvedValue({
926
- id: "proxy-456",
927
- original: "orig-123",
928
- member: { id: "member-1", name: "Echo" },
929
- system: { id: "system-1", name: "System" },
930
- });
931
-
932
- const result = await runGuildPreflight({
933
- channelId: "c1",
934
- guildId: "g1",
935
- message: createDiscordMessage({
936
- id: "proxy-456",
937
- channelId: "c1",
938
- content: "<@klaw-bot> hello",
939
- webhookId: "pluralkit-webhook-1",
940
- author: {
941
- id: "webhook-author",
942
- bot: true,
943
- username: "PluralKit",
944
- },
945
- mentionedUsers: [{ id: "klaw-bot" }],
946
- }),
947
- discordConfig: {
948
- pluralkit: { enabled: true },
949
- } as DiscordConfig,
950
- });
951
-
952
- expect(fetchPluralKitMessageInfoMock).toHaveBeenCalledTimes(1);
953
- const pluralKitCall = firstMockArg(
954
- fetchPluralKitMessageInfoMock,
955
- "fetchPluralKitMessageInfo",
956
- ) as { messageId?: unknown; config?: { enabled?: unknown } } | undefined;
957
- expect(pluralKitCall?.messageId).toBe("proxy-456");
958
- expect(pluralKitCall?.config?.enabled).toBe(true);
959
- const preflight = expectPreflightResult(result);
960
- expect(preflight.sender.isPluralKit).toBe(true);
961
- expect(preflight.canonicalMessageId).toBe("orig-123");
962
- });
963
-
964
- it("skips PluralKit lookup for bound-thread webhook echoes", async () => {
965
- const threadBinding = createThreadBinding({
966
- targetKind: "session",
967
- targetSessionKey: "agent:main:acp:discord-thread-1",
968
- });
969
- const threadId = "thread-webhook-pk-echo-1";
970
- const parentId = "channel-parent-webhook-pk-echo-1";
971
-
972
- const result = await runThreadBoundPreflight({
973
- threadId,
974
- parentId,
975
- threadBinding,
976
- message: createDiscordMessage({
977
- id: "m-webhook-pk-echo-1",
978
- channelId: threadId,
979
- content: "proxied user message",
980
- webhookId: "pluralkit-webhook-1",
981
- author: {
982
- id: "relay-bot-1",
983
- bot: true,
984
- username: "Proxy",
985
- },
986
- }),
987
- discordConfig: {
988
- pluralkit: { enabled: true },
989
- } as DiscordConfig,
990
- });
991
-
992
- expect(result).toBeNull();
993
- expect(fetchPluralKitMessageInfoMock).not.toHaveBeenCalled();
994
- });
995
-
996
- it("bypasses mention gating in bound threads for allowed bot senders", async () => {
997
- const threadBinding = createThreadBinding();
998
- const threadId = "thread-bot-focus";
999
- const parentId = "channel-parent-focus";
1000
- const client = createThreadClient({ threadId, parentId });
1001
- const message = createDiscordMessage({
1002
- id: "m-bot-1",
1003
- channelId: threadId,
1004
- content: "relay message without mention",
1005
- author: {
1006
- id: "relay-bot-1",
1007
- bot: true,
1008
- username: "Relay",
1009
- },
1010
- });
1011
-
1012
- registerSessionBindingAdapter({
1013
- channel: "discord",
1014
- accountId: "default",
1015
- listBySession: () => [],
1016
- resolveByConversation: (ref) => (ref.conversationId === threadId ? threadBinding : null),
1017
- });
1018
-
1019
- const result = await preflightDiscordMessage(
1020
- createPreflightArgs({
1021
- cfg: {
1022
- ...DEFAULT_PREFLIGHT_CFG,
1023
- } as import("klaw/plugin-sdk/config-contracts").KlawConfig,
1024
- discordConfig: {
1025
- allowBots: true,
1026
- } as DiscordConfig,
1027
- data: createGuildEvent({
1028
- channelId: threadId,
1029
- guildId: "guild-1",
1030
- author: message.author,
1031
- message,
1032
- }),
1033
- client,
1034
- }),
1035
- );
1036
-
1037
- const preflight = expectPreflightResult(result);
1038
- expect(preflight.boundSessionKey).toBe(threadBinding.targetSessionKey);
1039
- expect(preflight.shouldRequireMention).toBe(false);
1040
- });
1041
-
1042
- it("drops bot messages without mention when allowBots=mentions", async () => {
1043
- const channelId = "channel-bot-mentions-off";
1044
- const guildId = "guild-bot-mentions-off";
1045
- const message = createDiscordMessage({
1046
- id: "m-bot-mentions-off",
1047
- channelId,
1048
- content: "relay chatter",
1049
- author: {
1050
- id: "relay-bot-1",
1051
- bot: true,
1052
- username: "Relay",
1053
- },
1054
- });
1055
-
1056
- const result = await runMentionOnlyBotPreflight({ channelId, guildId, message });
1057
-
1058
- expect(result).toBeNull();
1059
- });
1060
-
1061
- it("allows bot messages with explicit mention when allowBots=mentions", async () => {
1062
- const channelId = "channel-bot-mentions-on";
1063
- const guildId = "guild-bot-mentions-on";
1064
- const message = createDiscordMessage({
1065
- id: "m-bot-mentions-on",
1066
- channelId,
1067
- content: "hi <@klaw-bot>",
1068
- mentionedUsers: [{ id: "klaw-bot" }],
1069
- author: {
1070
- id: "relay-bot-1",
1071
- bot: true,
1072
- username: "Relay",
1073
- },
1074
- });
1075
-
1076
- const result = await runMentionOnlyBotPreflight({ channelId, guildId, message });
1077
-
1078
- expect(expectPreflightResult(result).message.id).toBe("m-bot-mentions-on");
1079
- });
1080
-
1081
- it("hydrates mention metadata from REST when bot mention syntax is present but mentions are missing", async () => {
1082
- const channelId = "channel-bot-mentions-hydrated";
1083
- const guildId = "guild-bot-mentions-hydrated";
1084
- const botId = "123456789012345678";
1085
- const message = createDiscordMessage({
1086
- id: "m-bot-mentions-hydrated",
1087
- channelId,
1088
- content: `hi <@${botId}>`,
1089
- author: {
1090
- id: "relay-bot-1",
1091
- bot: true,
1092
- username: "Relay",
1093
- },
1094
- mentionedUsers: [],
1095
- });
1096
- const client = createGuildTextClient(channelId);
1097
- client.rest = {
1098
- get: vi.fn(async () => ({
1099
- id: message.id,
1100
- content: message.content,
1101
- mentions: [{ id: botId, username: "Klaw", bot: true }],
1102
- mention_roles: [],
1103
- mention_everyone: false,
1104
- })),
1105
- } as unknown as DiscordClient["rest"];
1106
-
1107
- const result = await preflightDiscordMessage({
1108
- ...createPreflightArgs({
1109
- cfg: DEFAULT_PREFLIGHT_CFG,
1110
- discordConfig: {
1111
- allowBots: "mentions",
1112
- } as DiscordConfig,
1113
- data: createGuildEvent({
1114
- channelId,
1115
- guildId,
1116
- author: message.author,
1117
- message,
1118
- }),
1119
- client,
1120
- }),
1121
- botUserId: botId,
1122
- });
1123
-
1124
- expect(expectPreflightResult(result).message.id).toBe("m-bot-mentions-hydrated");
1125
- });
1126
-
1127
- it("still drops bot control commands without a real mention when allowBots=mentions", async () => {
1128
- const channelId = "channel-bot-command-no-mention";
1129
- const guildId = "guild-bot-command-no-mention";
1130
- const message = createDiscordMessage({
1131
- id: "m-bot-command-no-mention",
1132
- channelId,
1133
- content: "/new incident room",
1134
- author: {
1135
- id: "relay-bot-1",
1136
- bot: true,
1137
- username: "Relay",
1138
- },
1139
- });
1140
-
1141
- const result = await runMentionOnlyBotPreflight({ channelId, guildId, message });
1142
-
1143
- expect(result).toBeNull();
1144
- });
1145
-
1146
- it("still allows bot control commands with an explicit mention when allowBots=mentions", async () => {
1147
- const channelId = "channel-bot-command-with-mention";
1148
- const guildId = "guild-bot-command-with-mention";
1149
- const message = createDiscordMessage({
1150
- id: "m-bot-command-with-mention",
1151
- channelId,
1152
- content: "<@klaw-bot> /new incident room",
1153
- mentionedUsers: [{ id: "klaw-bot" }],
1154
- author: {
1155
- id: "relay-bot-1",
1156
- bot: true,
1157
- username: "Relay",
1158
- },
1159
- });
1160
-
1161
- const result = await runMentionOnlyBotPreflight({ channelId, guildId, message });
1162
-
1163
- expect(expectPreflightResult(result).message.id).toBe("m-bot-command-with-mention");
1164
- });
1165
-
1166
- it("routes ordinary guild text control commands through authorization instead of dropping them", async () => {
1167
- const channelId = "channel-text-control-command";
1168
- const guildId = "guild-text-control-command";
1169
- const message = createDiscordMessage({
1170
- id: "m-text-control-command",
1171
- channelId,
1172
- content: "/steer keep digging",
1173
- author: {
1174
- id: "user-1",
1175
- bot: false,
1176
- username: "Alice",
1177
- },
1178
- });
1179
-
1180
- const result = await preflightDiscordMessage({
1181
- ...createPreflightArgs({
1182
- cfg: DEFAULT_PREFLIGHT_CFG,
1183
- discordConfig: {} as DiscordConfig,
1184
- data: createGuildEvent({
1185
- channelId,
1186
- guildId,
1187
- author: message.author,
1188
- message,
1189
- }),
1190
- client: createGuildTextClient(channelId),
1191
- }),
1192
- allowFrom: ["discord:user-1"],
1193
- guildEntries: {
1194
- [guildId]: {
1195
- channels: {
1196
- [channelId]: {
1197
- enabled: true,
1198
- requireMention: true,
1199
- },
1200
- },
1201
- },
1202
- },
1203
- });
1204
-
1205
- const preflight = expectPreflightResult(result);
1206
- expect(preflight.baseText).toBe("/steer keep digging");
1207
- expect(preflight.commandAuthorized).toBe(true);
1208
- expect(preflight.shouldRequireMention).toBe(true);
1209
- expect(preflight.shouldBypassMention).toBe(true);
1210
- });
1211
-
1212
- it("keeps unmentioned abort requests as user requests when room events are enabled", async () => {
1213
- const channelId = "channel-room-event-abort";
1214
- const guildId = "guild-room-event-abort";
1215
- const message = createDiscordMessage({
1216
- id: "m-room-event-abort",
1217
- channelId,
1218
- content: "please stop",
1219
- author: {
1220
- id: "user-1",
1221
- bot: false,
1222
- username: "Alice",
1223
- },
1224
- });
1225
-
1226
- const result = await runGuildPreflight({
1227
- channelId,
1228
- guildId,
1229
- message,
1230
- discordConfig: {} as DiscordConfig,
1231
- cfg: {
1232
- ...DEFAULT_PREFLIGHT_CFG,
1233
- messages: {
1234
- groupChat: {
1235
- unmentionedInbound: "room_event",
1236
- },
1237
- },
1238
- } as import("klaw/plugin-sdk/config-contracts").KlawConfig,
1239
- guildEntries: {
1240
- [guildId]: {
1241
- channels: {
1242
- [channelId]: {
1243
- enabled: true,
1244
- requireMention: false,
1245
- },
1246
- },
1247
- },
1248
- },
1249
- });
1250
-
1251
- const preflight = expectPreflightResult(result);
1252
- expect(preflight.baseText).toBe("please stop");
1253
- expect(preflight.inboundEventKind).toBe("user_request");
1254
- });
1255
-
1256
- it("still drops Discord native command echo messages", async () => {
1257
- const channelId = "channel-native-command-echo";
1258
- const guildId = "guild-native-command-echo";
1259
- const message = createDiscordMessage({
1260
- id: "m-native-command-echo",
1261
- channelId,
1262
- content: "/steer keep digging",
1263
- type: MessageType.ChatInputCommand,
1264
- author: {
1265
- id: "user-1",
1266
- bot: false,
1267
- username: "Alice",
1268
- },
1269
- });
1270
-
1271
- const result = await preflightDiscordMessage({
1272
- ...createPreflightArgs({
1273
- cfg: DEFAULT_PREFLIGHT_CFG,
1274
- discordConfig: {} as DiscordConfig,
1275
- data: createGuildEvent({
1276
- channelId,
1277
- guildId,
1278
- author: message.author,
1279
- message,
1280
- }),
1281
- client: createGuildTextClient(channelId),
1282
- }),
1283
- allowFrom: ["discord:user-1"],
1284
- guildEntries: {
1285
- [guildId]: {
1286
- channels: {
1287
- [channelId]: {
1288
- enabled: true,
1289
- requireMention: true,
1290
- },
1291
- },
1292
- },
1293
- },
1294
- });
1295
-
1296
- expect(result).toBeNull();
1297
- });
1298
-
1299
- it("does not mask mention gating when bot id is missing but mention patterns can detect", async () => {
1300
- const channelId = "channel-missing-bot-id-mention-gate";
1301
- const guildId = "guild-missing-bot-id-mention-gate";
1302
- const message = createDiscordMessage({
1303
- id: "m-missing-bot-id-mention-gate",
1304
- channelId,
1305
- content: "general update without the configured mention",
1306
- author: {
1307
- id: "user-1",
1308
- bot: false,
1309
- username: "Alice",
1310
- },
1311
- });
1312
-
1313
- const result = await preflightDiscordMessage({
1314
- ...createPreflightArgs({
1315
- cfg: {
1316
- ...DEFAULT_PREFLIGHT_CFG,
1317
- messages: {
1318
- groupChat: {
1319
- mentionPatterns: ["klaw"],
1320
- },
1321
- },
1322
- } as import("klaw/plugin-sdk/config-contracts").KlawConfig,
1323
- discordConfig: {} as DiscordConfig,
1324
- data: createGuildEvent({
1325
- channelId,
1326
- guildId,
1327
- author: message.author,
1328
- message,
1329
- }),
1330
- client: createGuildTextClient(channelId),
1331
- }),
1332
- botUserId: undefined,
1333
- guildEntries: {
1334
- [guildId]: {
1335
- channels: {
1336
- [channelId]: {
1337
- enabled: true,
1338
- requireMention: true,
1339
- },
1340
- },
1341
- },
1342
- },
1343
- });
1344
-
1345
- expect(result).toBeNull();
1346
- });
1347
-
1348
- it("treats @everyone as a mention when requireMention is true", async () => {
1349
- const channelId = "channel-everyone-mention";
1350
- const guildId = "guild-everyone-mention";
1351
- const message = createDiscordMessage({
1352
- id: "m-everyone-mention",
1353
- channelId,
1354
- content: "@everyone standup time!",
1355
- mentionedEveryone: true,
1356
- author: {
1357
- id: "user-1",
1358
- bot: false,
1359
- username: "Peter",
1360
- },
1361
- });
1362
-
1363
- const result = await runGuildPreflight({
1364
- channelId,
1365
- guildId,
1366
- message,
1367
- discordConfig: {
1368
- botId: "klaw-bot",
1369
- } as DiscordConfig,
1370
- guildEntries: {
1371
- [guildId]: {
1372
- channels: {
1373
- [channelId]: {
1374
- enabled: true,
1375
- requireMention: true,
1376
- },
1377
- },
1378
- },
1379
- },
1380
- });
1381
-
1382
- const preflight = expectPreflightResult(result);
1383
- expect(preflight.shouldRequireMention).toBe(true);
1384
- expect(preflight.wasMentioned).toBe(true);
1385
- });
1386
-
1387
- it("accepts allowlisted guild messages when guild object is missing", async () => {
1388
- const message = createDiscordMessage({
1389
- id: "m-guild-id-only",
1390
- channelId: "ch-1",
1391
- content: "hello from maintainers",
1392
- author: {
1393
- id: "user-1",
1394
- bot: false,
1395
- username: "Peter",
1396
- },
1397
- });
1398
-
1399
- const result = await runGuildPreflight({
1400
- channelId: "ch-1",
1401
- guildId: "guild-1",
1402
- message,
1403
- discordConfig: {} as DiscordConfig,
1404
- guildEntries: {
1405
- "guild-1": {
1406
- channels: {
1407
- "ch-1": {
1408
- enabled: true,
1409
- requireMention: false,
1410
- },
1411
- },
1412
- },
1413
- },
1414
- includeGuildObject: false,
1415
- });
1416
-
1417
- const preflight = expectPreflightResult(result);
1418
- expect(preflight.guildInfo?.id).toBe("guild-1");
1419
- expect(preflight.channelConfig?.allowed).toBe(true);
1420
- expect(preflight.shouldRequireMention).toBe(false);
1421
- });
1422
-
1423
- it("inherits parent thread allowlist when guild object is missing", async () => {
1424
- const threadId = "thread-1";
1425
- const parentId = "parent-1";
1426
- const message = createDiscordMessage({
1427
- id: "m-thread-id-only",
1428
- channelId: threadId,
1429
- content: "thread hello",
1430
- author: {
1431
- id: "user-1",
1432
- bot: false,
1433
- username: "Peter",
1434
- },
1435
- });
1436
-
1437
- const result = await preflightDiscordMessage({
1438
- ...createPreflightArgs({
1439
- cfg: DEFAULT_PREFLIGHT_CFG,
1440
- discordConfig: {} as DiscordConfig,
1441
- data: createGuildEvent({
1442
- channelId: threadId,
1443
- guildId: "guild-1",
1444
- author: message.author,
1445
- message,
1446
- includeGuildObject: false,
1447
- }),
1448
- client: createThreadClient({
1449
- threadId,
1450
- parentId,
1451
- }),
1452
- }),
1453
- guildEntries: {
1454
- "guild-1": {
1455
- channels: {
1456
- [parentId]: {
1457
- enabled: true,
1458
- requireMention: false,
1459
- },
1460
- },
1461
- },
1462
- },
1463
- });
1464
-
1465
- const preflight = expectPreflightResult(result);
1466
- expect(preflight.guildInfo?.id).toBe("guild-1");
1467
- expect(preflight.threadParentId).toBe(parentId);
1468
- expect(preflight.channelConfig?.allowed).toBe(true);
1469
- expect(preflight.shouldRequireMention).toBe(false);
1470
- });
1471
-
1472
- it("handles partial thread channel owner getters during mention preflight", async () => {
1473
- const threadId = "thread-partial-owner";
1474
- const parentId = "parent-partial-owner";
1475
- const message = createDiscordMessage({
1476
- id: "m-thread-partial-owner",
1477
- channelId: threadId,
1478
- content: "thread hello",
1479
- author: {
1480
- id: "user-1",
1481
- bot: false,
1482
- username: "Peter",
1483
- },
1484
- });
1485
- Object.defineProperty(message, "channel", {
1486
- value: createPartialDiscordChannelWithThrowingGetters(
1487
- {
1488
- id: threadId,
1489
- isThread: () => true,
1490
- ownerId: "owner-1",
1491
- parentId,
1492
- parent: { id: parentId, name: "general" },
1493
- },
1494
- ["ownerId", "parentId", "parent"],
1495
- ),
1496
- configurable: true,
1497
- enumerable: true,
1498
- });
1499
-
1500
- const result = await preflightDiscordMessage({
1501
- ...createPreflightArgs({
1502
- cfg: DEFAULT_PREFLIGHT_CFG,
1503
- discordConfig: {} as DiscordConfig,
1504
- data: createGuildEvent({
1505
- channelId: threadId,
1506
- guildId: "guild-1",
1507
- author: message.author,
1508
- message,
1509
- includeGuildObject: false,
1510
- }),
1511
- client: createThreadClient({
1512
- threadId,
1513
- parentId,
1514
- }),
1515
- }),
1516
- guildEntries: {
1517
- "guild-1": {
1518
- channels: {
1519
- [parentId]: {
1520
- enabled: true,
1521
- requireMention: false,
1522
- },
1523
- },
1524
- },
1525
- },
1526
- });
1527
-
1528
- const preflight = expectPreflightResult(result);
1529
- expect(preflight.threadParentId).toBe(parentId);
1530
- expect(preflight.shouldRequireMention).toBe(false);
1531
- });
1532
-
1533
- it("drops guild messages that mention another user when ignoreOtherMentions=true", async () => {
1534
- const channelId = "channel-other-mention-1";
1535
- const guildId = "guild-other-mention-1";
1536
- const message = createDiscordMessage({
1537
- id: "m-other-mention-1",
1538
- channelId,
1539
- content: "hello <@999>",
1540
- mentionedUsers: [{ id: "999" }],
1541
- author: {
1542
- id: "user-1",
1543
- bot: false,
1544
- username: "Alice",
1545
- },
1546
- });
1547
-
1548
- const result = await runIgnoreOtherMentionsPreflight({ channelId, guildId, message });
1549
-
1550
- expect(result).toBeNull();
1551
- });
1552
-
1553
- it("records local image media for skipped mention-gated guild history", async () => {
1554
- const channelId = "channel-history-image";
1555
- const guildId = "guild-history-image";
1556
- const guildHistories = new Map();
1557
- saveRemoteMediaMock.mockResolvedValueOnce({
1558
- id: "test-media",
1559
- path: "C:\\klaw\\media\\history.png",
1560
- size: 5,
1561
- contentType: "image/png",
1562
- });
1563
- const fetchImpl = vi.fn(
1564
- async () =>
1565
- new Response(Buffer.from("image"), {
1566
- headers: {
1567
- "content-type": "image/png",
1568
- },
1569
- }),
1570
- ) as unknown as typeof fetch;
1571
- const message = createDiscordMessage({
1572
- id: "m-history-image",
1573
- channelId,
1574
- content: "",
1575
- attachments: [
1576
- {
1577
- id: "att-history-image",
1578
- url: "https://cdn.discordapp.com/attachments/1/history.png",
1579
- filename: "history.png",
1580
- content_type: "image/png",
1581
- },
1582
- ],
1583
- author: {
1584
- id: "user-1",
1585
- bot: false,
1586
- username: "Alice",
1587
- },
1588
- });
1589
-
1590
- const result = await preflightDiscordMessage({
1591
- ...createPreflightArgs({
1592
- cfg: DEFAULT_PREFLIGHT_CFG,
1593
- discordConfig: {} as DiscordConfig,
1594
- data: createGuildEvent({
1595
- channelId,
1596
- guildId,
1597
- author: message.author,
1598
- message,
1599
- }),
1600
- client: createGuildTextClient(channelId),
1601
- }),
1602
- guildHistories,
1603
- historyLimit: 4,
1604
- discordRestFetch: fetchImpl,
1605
- guildEntries: {
1606
- [guildId]: {
1607
- channels: {
1608
- [channelId]: {
1609
- enabled: true,
1610
- requireMention: true,
1611
- },
1612
- },
1613
- },
1614
- },
1615
- });
1616
-
1617
- expect(result).toBeNull();
1618
- const entries = guildHistories.get(channelId);
1619
- expect(entries).toHaveLength(1);
1620
- expect(entries?.[0]).toMatchObject({
1621
- sender: "Alice",
1622
- body: "<media:image> (1 image)",
1623
- messageId: "m-history-image",
1624
- media: [
1625
- {
1626
- contentType: "image/png",
1627
- kind: "image",
1628
- messageId: "m-history-image",
1629
- },
1630
- ],
1631
- });
1632
- expect(entries?.[0]?.media?.[0]?.path).toContain("history");
1633
- expect(entries?.[0]?.media?.[0]?.path).not.toMatch(/^https?:/);
1634
- expect(entries?.[0]?.media?.[0]?.path).toBe("C:\\klaw\\media\\history.png");
1635
- expect(saveRemoteMediaMock).toHaveBeenCalledTimes(1);
1636
- });
1637
-
1638
- it("does not download non-image media for skipped mention-gated guild history", async () => {
1639
- const channelId = "channel-history-doc";
1640
- const guildId = "guild-history-doc";
1641
- const guildHistories = new Map();
1642
- const message = createDiscordMessage({
1643
- id: "m-history-doc",
1644
- channelId,
1645
- content: "",
1646
- attachments: [
1647
- {
1648
- id: "att-history-doc",
1649
- url: "https://cdn.discordapp.com/attachments/1/history.pdf",
1650
- filename: "history.pdf",
1651
- content_type: "application/pdf",
1652
- },
1653
- ],
1654
- author: {
1655
- id: "user-1",
1656
- bot: false,
1657
- username: "Alice",
1658
- },
1659
- });
1660
-
1661
- const result = await preflightDiscordMessage({
1662
- ...createPreflightArgs({
1663
- cfg: DEFAULT_PREFLIGHT_CFG,
1664
- discordConfig: {} as DiscordConfig,
1665
- data: createGuildEvent({
1666
- channelId,
1667
- guildId,
1668
- author: message.author,
1669
- message,
1670
- }),
1671
- client: createGuildTextClient(channelId),
1672
- }),
1673
- guildHistories,
1674
- historyLimit: 4,
1675
- guildEntries: {
1676
- [guildId]: {
1677
- channels: {
1678
- [channelId]: {
1679
- enabled: true,
1680
- requireMention: true,
1681
- },
1682
- },
1683
- },
1684
- },
1685
- });
1686
-
1687
- expect(result).toBeNull();
1688
- expect(saveRemoteMediaMock).not.toHaveBeenCalled();
1689
- expect(guildHistories.get(channelId)).toEqual([
1690
- expect.objectContaining({
1691
- sender: "Alice",
1692
- body: "<media:document> (1 file)",
1693
- messageId: "m-history-doc",
1694
- }),
1695
- ]);
1696
- expect(guildHistories.get(channelId)?.[0]?.media).toBeUndefined();
1697
- });
1698
-
1699
- it("records sticker image media for skipped mention-gated guild history", async () => {
1700
- const channelId = "channel-history-sticker";
1701
- const guildId = "guild-history-sticker";
1702
- const guildHistories = new Map();
1703
- saveRemoteMediaMock.mockResolvedValueOnce({
1704
- id: "test-sticker",
1705
- path: "/tmp/klaw-discord-test/sticker.png",
1706
- size: 5,
1707
- contentType: "image/png",
1708
- });
1709
- const message = Object.assign(
1710
- createDiscordMessage({
1711
- id: "m-history-sticker",
1712
- channelId,
1713
- content: "",
1714
- author: {
1715
- id: "user-1",
1716
- bot: false,
1717
- username: "Alice",
1718
- },
1719
- }),
1720
- {
1721
- stickers: [
1722
- {
1723
- id: "sticker-history",
1724
- name: "history-sticker",
1725
- format_type: 1,
1726
- },
1727
- ],
1728
- },
1729
- );
1730
-
1731
- const result = await preflightDiscordMessage({
1732
- ...createPreflightArgs({
1733
- cfg: DEFAULT_PREFLIGHT_CFG,
1734
- discordConfig: {} as DiscordConfig,
1735
- data: createGuildEvent({
1736
- channelId,
1737
- guildId,
1738
- author: message.author,
1739
- message,
1740
- }),
1741
- client: createGuildTextClient(channelId),
1742
- }),
1743
- guildHistories,
1744
- historyLimit: 4,
1745
- guildEntries: {
1746
- [guildId]: {
1747
- channels: {
1748
- [channelId]: {
1749
- enabled: true,
1750
- requireMention: true,
1751
- },
1752
- },
1753
- },
1754
- },
1755
- });
1756
-
1757
- expect(result).toBeNull();
1758
- expect(guildHistories.get(channelId)).toEqual([
1759
- expect.objectContaining({
1760
- sender: "Alice",
1761
- body: "<media:sticker> (1 sticker)",
1762
- messageId: "m-history-sticker",
1763
- media: [
1764
- {
1765
- path: "/tmp/klaw-discord-test/sticker.png",
1766
- contentType: "image/png",
1767
- kind: "image",
1768
- messageId: "m-history-sticker",
1769
- },
1770
- ],
1771
- }),
1772
- ]);
1773
- expect(saveRemoteMediaMock).toHaveBeenCalledTimes(1);
1774
- });
1775
-
1776
- it("caps skipped history media before falling back to raw Discord stickers", async () => {
1777
- const channelId = "channel-history-cap";
1778
- const guildId = "guild-history-cap";
1779
- const guildHistories = new Map();
1780
- const sticker = {
1781
- id: "sticker-history-cap",
1782
- name: "history-cap-sticker",
1783
- format_type: 1,
1784
- };
1785
- const message = Object.assign(
1786
- createDiscordMessage({
1787
- id: "m-history-cap",
1788
- channelId,
1789
- content: "",
1790
- attachments: Array.from({ length: 4 }, (_, index) => ({
1791
- id: `att-history-cap-${index}`,
1792
- url: `https://cdn.discordapp.com/attachments/1/history-${index}.png`,
1793
- filename: `history-${index}.png`,
1794
- content_type: "image/png",
1795
- })),
1796
- author: {
1797
- id: "user-1",
1798
- bot: false,
1799
- username: "Alice",
1800
- },
1801
- }),
1802
- {
1803
- rawData: {
1804
- sticker_items: [sticker],
1805
- },
1806
- stickers: [sticker],
1807
- },
1808
- );
1809
-
1810
- const result = await preflightDiscordMessage({
1811
- ...createPreflightArgs({
1812
- cfg: DEFAULT_PREFLIGHT_CFG,
1813
- discordConfig: {} as DiscordConfig,
1814
- data: createGuildEvent({
1815
- channelId,
1816
- guildId,
1817
- author: message.author,
1818
- message,
1819
- }),
1820
- client: createGuildTextClient(channelId),
1821
- }),
1822
- guildHistories,
1823
- historyLimit: 4,
1824
- guildEntries: {
1825
- [guildId]: {
1826
- channels: {
1827
- [channelId]: {
1828
- enabled: true,
1829
- requireMention: true,
1830
- },
1831
- },
1832
- },
1833
- },
1834
- });
1835
-
1836
- expect(result).toBeNull();
1837
- expect(guildHistories.get(channelId)?.[0]?.media).toHaveLength(4);
1838
- expect(saveRemoteMediaMock).toHaveBeenCalledTimes(4);
1839
- });
1840
-
1841
- it("does not drop @everyone messages when ignoreOtherMentions=true", async () => {
1842
- const channelId = "channel-other-mention-everyone";
1843
- const guildId = "guild-other-mention-everyone";
1844
- const message = createDiscordMessage({
1845
- id: "m-other-mention-everyone",
1846
- channelId,
1847
- content: "@everyone heads up",
1848
- mentionedEveryone: true,
1849
- author: {
1850
- id: "user-1",
1851
- bot: false,
1852
- username: "Alice",
1853
- },
1854
- });
1855
-
1856
- const result = await runIgnoreOtherMentionsPreflight({ channelId, guildId, message });
1857
-
1858
- expect(expectPreflightResult(result).hasAnyMention).toBe(true);
1859
- });
1860
-
1861
- it("ignores bot-sent @everyone mentions for detection", async () => {
1862
- const channelId = "channel-everyone-1";
1863
- const guildId = "guild-everyone-1";
1864
- const client = createGuildTextClient(channelId);
1865
- const message = createDiscordMessage({
1866
- id: "m-everyone-1",
1867
- channelId,
1868
- content: "@everyone heads up",
1869
- mentionedEveryone: true,
1870
- author: {
1871
- id: "relay-bot-1",
1872
- bot: true,
1873
- username: "Relay",
1874
- },
1875
- });
1876
-
1877
- const result = await preflightDiscordMessage({
1878
- ...createPreflightArgs({
1879
- cfg: DEFAULT_PREFLIGHT_CFG,
1880
- discordConfig: {
1881
- allowBots: true,
1882
- } as DiscordConfig,
1883
- data: createGuildEvent({
1884
- channelId,
1885
- guildId,
1886
- author: message.author,
1887
- message,
1888
- }),
1889
- client,
1890
- }),
1891
- guildEntries: {
1892
- [guildId]: {
1893
- requireMention: false,
1894
- },
1895
- },
1896
- });
1897
-
1898
- expect(expectPreflightResult(result).hasAnyMention).toBe(false);
1899
- });
1900
-
1901
- it("does not treat bot-sent @everyone as wasMentioned", async () => {
1902
- const channelId = "channel-everyone-2";
1903
- const guildId = "guild-everyone-2";
1904
- const client = createGuildTextClient(channelId);
1905
- const message = createDiscordMessage({
1906
- id: "m-everyone-2",
1907
- channelId,
1908
- content: "@everyone relay message",
1909
- mentionedEveryone: true,
1910
- author: {
1911
- id: "relay-bot-2",
1912
- bot: true,
1913
- username: "RelayBot",
1914
- },
1915
- });
1916
-
1917
- const result = await preflightDiscordMessage({
1918
- ...createPreflightArgs({
1919
- cfg: DEFAULT_PREFLIGHT_CFG,
1920
- discordConfig: {
1921
- allowBots: true,
1922
- } as DiscordConfig,
1923
- data: createGuildEvent({
1924
- channelId,
1925
- guildId,
1926
- author: message.author,
1927
- message,
1928
- }),
1929
- client,
1930
- }),
1931
- guildEntries: {
1932
- [guildId]: {
1933
- requireMention: false,
1934
- },
1935
- },
1936
- });
1937
-
1938
- expect(expectPreflightResult(result).wasMentioned).toBe(false);
1939
- });
1940
-
1941
- it("uses attachment content_type for guild audio preflight mention detection", async () => {
1942
- transcribeFirstAudioMock.mockResolvedValue("hey klaw");
1943
-
1944
- const channelId = "channel-audio-1";
1945
- const client = createGuildTextClient(channelId);
1946
-
1947
- const message = createDiscordMessage({
1948
- id: "m-audio-1",
1949
- channelId,
1950
- content: "",
1951
- attachments: [
1952
- {
1953
- id: "att-1",
1954
- url: "https://cdn.discordapp.com/attachments/voice.ogg",
1955
- content_type: "audio/ogg",
1956
- filename: "voice.ogg",
1957
- },
1958
- ],
1959
- author: {
1960
- id: "user-1",
1961
- bot: false,
1962
- username: "Alice",
1963
- },
1964
- });
1965
-
1966
- const result = await preflightDiscordMessage({
1967
- ...createPreflightArgs({
1968
- cfg: {
1969
- ...DEFAULT_PREFLIGHT_CFG,
1970
- messages: {
1971
- groupChat: {
1972
- mentionPatterns: ["klaw"],
1973
- },
1974
- },
1975
- } as import("klaw/plugin-sdk/config-contracts").KlawConfig,
1976
- discordConfig: {} as DiscordConfig,
1977
- data: createGuildEvent({
1978
- channelId,
1979
- guildId: "guild-1",
1980
- author: message.author,
1981
- message,
1982
- }),
1983
- client,
1984
- }),
1985
- guildEntries: {
1986
- "guild-1": {
1987
- channels: {
1988
- [channelId]: {
1989
- enabled: true,
1990
- requireMention: true,
1991
- },
1992
- },
1993
- },
1994
- },
1995
- });
1996
-
1997
- expect(transcribeFirstAudioMock).toHaveBeenCalledTimes(1);
1998
- const guildAudioCall = firstMockArg(transcribeFirstAudioMock, "transcribeFirstAudio") as
1999
- | { ctx?: { MediaUrls?: unknown; MediaTypes?: unknown } }
2000
- | undefined;
2001
- expect(guildAudioCall?.ctx?.MediaUrls).toEqual([
2002
- "https://cdn.discordapp.com/attachments/voice.ogg",
2003
- ]);
2004
- expect(guildAudioCall?.ctx?.MediaTypes).toEqual(["audio/ogg"]);
2005
- const preflight = expectPreflightResult(result);
2006
- expect(preflight.wasMentioned).toBe(true);
2007
- expect(preflight.preflightAudioTranscript).toBe("hey klaw");
2008
- });
2009
-
2010
- it("does not transcribe guild audio from unauthorized members", async () => {
2011
- const channelId = "channel-audio-unauthorized-1";
2012
- const guildId = "guild-audio-unauthorized-1";
2013
- const client = createGuildTextClient(channelId);
2014
-
2015
- const message = createDiscordMessage({
2016
- id: "m-audio-unauthorized-1",
2017
- channelId,
2018
- content: "",
2019
- attachments: [
2020
- {
2021
- id: "att-1",
2022
- url: "https://cdn.discordapp.com/attachments/voice.ogg",
2023
- content_type: "audio/ogg",
2024
- filename: "voice.ogg",
2025
- },
2026
- ],
2027
- author: {
2028
- id: "user-2",
2029
- bot: false,
2030
- username: "Mallory",
2031
- },
2032
- });
2033
-
2034
- const result = await preflightDiscordMessage({
2035
- ...createPreflightArgs({
2036
- cfg: {
2037
- ...DEFAULT_PREFLIGHT_CFG,
2038
- messages: {
2039
- groupChat: {
2040
- mentionPatterns: ["klaw"],
2041
- },
2042
- },
2043
- } as import("klaw/plugin-sdk/config-contracts").KlawConfig,
2044
- discordConfig: {} as DiscordConfig,
2045
- data: createGuildEvent({
2046
- channelId,
2047
- guildId,
2048
- author: message.author,
2049
- message,
2050
- }),
2051
- client,
2052
- }),
2053
- guildEntries: {
2054
- [guildId]: {
2055
- channels: {
2056
- [channelId]: {
2057
- enabled: true,
2058
- requireMention: true,
2059
- users: ["user-1"],
2060
- },
2061
- },
2062
- },
2063
- },
2064
- });
2065
-
2066
- expect(transcribeFirstAudioMock).not.toHaveBeenCalled();
2067
- expect(result).toBeNull();
2068
- });
2069
-
2070
- it("drops guild message without mention when channel has configuredBinding and requireMention: true", async () => {
2071
- const conversationRuntime = await import("klaw/plugin-sdk/conversation-runtime");
2072
- const channelId = "ch-binding-1";
2073
- const bindingRoute = {
2074
- bindingResolution: {
2075
- record: {
2076
- targetSessionKey: "agent:main:acp:binding:discord:default:abc",
2077
- targetKind: "session",
2078
- },
2079
- } as never,
2080
- route: { agentId: "main", matchedBy: "binding.channel" } as never,
2081
- boundSessionKey: "agent:main:acp:binding:discord:default:abc",
2082
- boundAgentId: "main",
2083
- };
2084
- const routeSpy = vi
2085
- .spyOn(conversationRuntime, "resolveConfiguredBindingRoute")
2086
- .mockReturnValue(bindingRoute);
2087
- const ensureSpy = vi
2088
- .spyOn(conversationRuntime, "ensureConfiguredBindingRouteReady")
2089
- .mockResolvedValue({ ok: true });
2090
-
2091
- try {
2092
- const result = await runGuildPreflight({
2093
- channelId,
2094
- guildId: "guild-1",
2095
- message: createDiscordMessage({
2096
- id: "m-binding-1",
2097
- channelId,
2098
- content: "hello without mention",
2099
- author: { id: "user-1", bot: false, username: "alice" },
2100
- }),
2101
- discordConfig: {} as DiscordConfig,
2102
- guildEntries: {
2103
- "guild-1": { channels: { [channelId]: { enabled: true, requireMention: true } } },
2104
- },
2105
- });
2106
- expect(result).toBeNull();
2107
- } finally {
2108
- routeSpy.mockRestore();
2109
- ensureSpy.mockRestore();
2110
- }
2111
- });
2112
-
2113
- it("allows guild message with mention when channel has configuredBinding and requireMention: true", async () => {
2114
- const conversationRuntime = await import("klaw/plugin-sdk/conversation-runtime");
2115
- const channelId = "ch-binding-2";
2116
- const bindingRoute = {
2117
- bindingResolution: {
2118
- record: {
2119
- targetSessionKey: "agent:main:acp:binding:discord:default:def",
2120
- targetKind: "session",
2121
- },
2122
- } as never,
2123
- route: { agentId: "main", matchedBy: "binding.channel" } as never,
2124
- boundSessionKey: "agent:main:acp:binding:discord:default:def",
2125
- boundAgentId: "main",
2126
- };
2127
- const routeSpy = vi
2128
- .spyOn(conversationRuntime, "resolveConfiguredBindingRoute")
2129
- .mockReturnValue(bindingRoute);
2130
- const ensureSpy = vi
2131
- .spyOn(conversationRuntime, "ensureConfiguredBindingRouteReady")
2132
- .mockResolvedValue({ ok: true });
2133
-
2134
- try {
2135
- const result = await runGuildPreflight({
2136
- channelId,
2137
- guildId: "guild-1",
2138
- message: createDiscordMessage({
2139
- id: "m-binding-2",
2140
- channelId,
2141
- content: "hello <@klaw-bot>",
2142
- author: { id: "user-1", bot: false, username: "alice" },
2143
- mentionedUsers: [{ id: "klaw-bot" }],
2144
- }),
2145
- discordConfig: {} as DiscordConfig,
2146
- guildEntries: {
2147
- "guild-1": { channels: { [channelId]: { enabled: true, requireMention: true } } },
2148
- },
2149
- });
2150
- expect(expectPreflightResult(result).message.id).toBe("m-binding-2");
2151
- } finally {
2152
- routeSpy.mockRestore();
2153
- ensureSpy.mockRestore();
2154
- }
2155
- });
2156
- });
2157
-
2158
- describe("shouldIgnoreBoundThreadWebhookMessage", () => {
2159
- beforeEach(() => {
2160
- sessionBindingTesting.resetSessionBindingAdaptersForTests();
2161
- threadBindingTesting.resetThreadBindingsForTests();
2162
- });
2163
-
2164
- it("returns true when inbound webhook id matches the bound thread webhook", () => {
2165
- expect(
2166
- shouldIgnoreBoundThreadWebhookMessage({
2167
- webhookId: "wh-1",
2168
- threadBinding: createThreadBinding(),
2169
- }),
2170
- ).toBe(true);
2171
- });
2172
-
2173
- it("returns true when a bound thread receives a different webhook id", () => {
2174
- expect(
2175
- shouldIgnoreBoundThreadWebhookMessage({
2176
- threadId: "thread-1",
2177
- webhookId: "wh-other",
2178
- threadBinding: createThreadBinding(),
2179
- }),
2180
- ).toBe(true);
2181
- });
2182
-
2183
- it("returns true when a bound thread receives a webhook without a recorded bound webhook id", () => {
2184
- expect(
2185
- shouldIgnoreBoundThreadWebhookMessage({
2186
- threadId: "thread-1",
2187
- webhookId: "wh-1",
2188
- threadBinding: createThreadBinding({
2189
- metadata: {
2190
- webhookId: undefined,
2191
- },
2192
- }),
2193
- }),
2194
- ).toBe(true);
2195
- });
2196
-
2197
- it("returns false for differing webhook ids without a known thread id", () => {
2198
- expect(
2199
- shouldIgnoreBoundThreadWebhookMessage({
2200
- webhookId: "wh-other",
2201
- threadBinding: createThreadBinding(),
2202
- }),
2203
- ).toBe(false);
2204
- });
2205
-
2206
- it("returns true for recently unbound thread webhook echoes", async () => {
2207
- const manager = createThreadBindingManager({
2208
- cfg: DEFAULT_PREFLIGHT_CFG,
2209
- accountId: "default",
2210
- persist: false,
2211
- enableSweeper: false,
2212
- });
2213
- const binding = await manager.bindTarget({
2214
- threadId: "thread-1",
2215
- channelId: "parent-1",
2216
- targetKind: "subagent",
2217
- targetSessionKey: "agent:main:subagent:child-1",
2218
- agentId: "main",
2219
- webhookId: "wh-1",
2220
- webhookToken: "tok-1",
2221
- });
2222
- if (!binding) {
2223
- throw new Error("Expected Discord thread binding");
2224
- }
2225
- expect(binding.accountId).toBe("default");
2226
- expect(binding.channelId).toBe("parent-1");
2227
- expect(binding.threadId).toBe("thread-1");
2228
- expect(binding.targetKind).toBe("subagent");
2229
- expect(binding.targetSessionKey).toBe("agent:main:subagent:child-1");
2230
- expect(binding.agentId).toBe("main");
2231
- expect(binding.webhookId).toBe("wh-1");
2232
- expect(binding.webhookToken).toBe("tok-1");
2233
- expect(binding.boundBy).toBe("system");
2234
- expect(binding.idleTimeoutMs).toBe(24 * 60 * 60 * 1000);
2235
- expect(binding.maxAgeMs).toBe(0);
2236
- expect(typeof binding.boundAt).toBe("number");
2237
- expect(binding.boundAt).toBeGreaterThan(0);
2238
- expect(binding.lastActivityAt).toBe(binding.boundAt);
2239
- expect(binding.label).toBeUndefined();
2240
- expect(binding.metadata).toBeUndefined();
2241
-
2242
- manager.unbindThread({
2243
- threadId: "thread-1",
2244
- sendFarewell: false,
2245
- });
2246
-
2247
- expect(
2248
- shouldIgnoreBoundThreadWebhookMessage({
2249
- accountId: "default",
2250
- threadId: "thread-1",
2251
- webhookId: "wh-1",
2252
- }),
2253
- ).toBe(true);
2254
- });
2255
- });