@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,1416 +0,0 @@
1
- import { danger } from "klaw/plugin-sdk/runtime-env";
2
- import { typedCases } from "klaw/plugin-sdk/test-fixtures";
3
- import { beforeEach, describe, expect, it, vi } from "vitest";
4
- import { ChannelType, type Guild } from "./internal/discord.js";
5
- import {
6
- allowListMatches,
7
- type DiscordGuildEntryResolved,
8
- isDiscordGroupAllowedByPolicy,
9
- normalizeDiscordAllowList,
10
- normalizeDiscordSlug,
11
- resolveDiscordChannelConfig,
12
- resolveDiscordChannelConfigWithFallback,
13
- resolveDiscordGuildEntry,
14
- resolveDiscordOwnerAccess,
15
- resolveDiscordShouldRequireMention,
16
- resolveGroupDmAllow,
17
- shouldEmitDiscordReactionNotification,
18
- } from "./monitor/allow-list.js";
19
- import { buildDiscordMediaPayload } from "./monitor/message-utils.js";
20
- import { resolveDiscordReplyTarget, sanitizeDiscordThreadName } from "./monitor/threading.js";
21
- type DiscordReactionEvent = Parameters<
22
- import("./monitor/listeners.js").DiscordReactionListener["handle"]
23
- >[0];
24
- type DiscordReactionClient = Parameters<
25
- import("./monitor/listeners.js").DiscordReactionListener["handle"]
26
- >[1];
27
-
28
- const readAllowFromStoreMock = vi.hoisted(() => vi.fn());
29
-
30
- vi.mock("klaw/plugin-sdk/conversation-runtime", async () => {
31
- const actual = await vi.importActual<typeof import("klaw/plugin-sdk/conversation-runtime")>(
32
- "klaw/plugin-sdk/conversation-runtime",
33
- );
34
- return {
35
- ...actual,
36
- readChannelAllowFromStore: (...args: unknown[]) => readAllowFromStoreMock(...args),
37
- };
38
- });
39
-
40
- const fakeGuild = (id: string, name: string) => ({ id, name }) as Guild;
41
-
42
- function expectNormalizedAllowList(
43
- entries: string[],
44
- prefixes: string[],
45
- ): NonNullable<ReturnType<typeof normalizeDiscordAllowList>> {
46
- const allow = normalizeDiscordAllowList(entries, prefixes);
47
- if (allow === null) {
48
- throw new Error("Expected allow list to be normalized");
49
- }
50
- return allow;
51
- }
52
-
53
- const makeEntries = (
54
- entries: Record<string, Partial<DiscordGuildEntryResolved>>,
55
- ): Record<string, DiscordGuildEntryResolved> => {
56
- const out: Record<string, DiscordGuildEntryResolved> = {};
57
- for (const [key, value] of Object.entries(entries)) {
58
- out[key] = {
59
- slug: value.slug,
60
- requireMention: value.requireMention,
61
- reactionNotifications: value.reactionNotifications,
62
- users: value.users,
63
- roles: value.roles,
64
- channels: value.channels,
65
- };
66
- }
67
- return out;
68
- };
69
-
70
- function createAutoThreadMentionContext() {
71
- const guildInfo: DiscordGuildEntryResolved = {
72
- requireMention: true,
73
- channels: {
74
- general: { enabled: true, autoThread: true },
75
- },
76
- };
77
- const channelConfig = resolveDiscordChannelConfig({
78
- guildInfo,
79
- channelId: "1",
80
- channelName: "General",
81
- channelSlug: "general",
82
- });
83
- return { guildInfo, channelConfig };
84
- }
85
-
86
- beforeEach(() => {
87
- vi.useRealTimers();
88
- readAllowFromStoreMock.mockReset().mockResolvedValue([]);
89
- });
90
-
91
- describe("registerDiscordListener", () => {
92
- class FakeListener {
93
- readonly testListener = true;
94
- }
95
-
96
- it("dedupes listeners by constructor", () => {
97
- const listeners: object[] = [];
98
-
99
- expect(registerDiscordListener(listeners, new FakeListener())).toBe(true);
100
- expect(registerDiscordListener(listeners, new FakeListener())).toBe(false);
101
- expect(listeners).toHaveLength(1);
102
- });
103
- });
104
-
105
- describe("DiscordMessageListener", () => {
106
- function createDeferred() {
107
- let resolve: (() => void) | null = null;
108
- const promise = new Promise<void>((done) => {
109
- resolve = done;
110
- });
111
- return {
112
- promise,
113
- resolve: () => {
114
- if (typeof resolve === "function") {
115
- (resolve as () => void)();
116
- }
117
- },
118
- };
119
- }
120
-
121
- async function flushAsyncWork() {
122
- await Promise.resolve();
123
- await Promise.resolve();
124
- }
125
-
126
- it("returns immediately while handler continues in background", async () => {
127
- let handlerResolved = false;
128
- const deferred = createDeferred();
129
- const handler = vi.fn(async () => {
130
- await deferred.promise;
131
- handlerResolved = true;
132
- });
133
- const listener = new DiscordMessageListener(handler);
134
-
135
- const handlePromise = listener.handle(
136
- {} as unknown as import("./monitor/listeners.js").DiscordMessageEvent,
137
- {} as unknown as import("./internal/discord.js").Client,
138
- );
139
-
140
- // handle() returns immediately while the background queue starts on the next tick.
141
- await expect(handlePromise).resolves.toBeUndefined();
142
- await flushAsyncWork();
143
- expect(handler).toHaveBeenCalledOnce();
144
- expect(handlerResolved).toBe(false);
145
-
146
- // Release and let background handler finish.
147
- deferred.resolve();
148
- await Promise.resolve();
149
- expect(handlerResolved).toBe(true);
150
- });
151
-
152
- it("dispatches subsequent events concurrently without blocking on prior handler", async () => {
153
- const first = createDeferred();
154
- const second = createDeferred();
155
- let runCount = 0;
156
- const handler = vi.fn(async () => {
157
- runCount += 1;
158
- if (runCount === 1) {
159
- await first.promise;
160
- return;
161
- }
162
- await second.promise;
163
- });
164
- const listener = new DiscordMessageListener(handler);
165
-
166
- await expect(
167
- listener.handle(
168
- {} as unknown as import("./monitor/listeners.js").DiscordMessageEvent,
169
- {} as unknown as import("./internal/discord.js").Client,
170
- ),
171
- ).resolves.toBeUndefined();
172
- await expect(
173
- listener.handle(
174
- {} as unknown as import("./monitor/listeners.js").DiscordMessageEvent,
175
- {} as unknown as import("./internal/discord.js").Client,
176
- ),
177
- ).resolves.toBeUndefined();
178
-
179
- // Both handlers are dispatched concurrently (fire-and-forget).
180
- await flushAsyncWork();
181
- expect(handler).toHaveBeenCalledTimes(2);
182
-
183
- first.resolve();
184
- second.resolve();
185
- await Promise.resolve();
186
- });
187
-
188
- it("logs handler failures", async () => {
189
- const logger = {
190
- warn: vi.fn(),
191
- error: vi.fn(),
192
- } as unknown as ReturnType<typeof import("klaw/plugin-sdk/logging-core").createSubsystemLogger>;
193
- const handler = vi.fn(async () => {
194
- throw new Error("boom");
195
- });
196
- const listener = new DiscordMessageListener(handler, logger);
197
-
198
- await listener.handle(
199
- {} as unknown as import("./monitor/listeners.js").DiscordMessageEvent,
200
- {} as unknown as import("./internal/discord.js").Client,
201
- );
202
- await flushAsyncWork();
203
- expect(logger.error).toHaveBeenCalledWith(danger("discord handler failed: Error: boom"));
204
- });
205
-
206
- it("does not apply its own slow-listener logging", async () => {
207
- const deferred = createDeferred();
208
- const handler = vi.fn(() => deferred.promise);
209
- const logger = {
210
- warn: vi.fn(),
211
- error: vi.fn(),
212
- } as unknown as ReturnType<typeof import("klaw/plugin-sdk/logging-core").createSubsystemLogger>;
213
- const listener = new DiscordMessageListener(handler, logger);
214
-
215
- const handlePromise = listener.handle(
216
- {} as unknown as import("./monitor/listeners.js").DiscordMessageEvent,
217
- {} as unknown as import("./internal/discord.js").Client,
218
- );
219
- await expect(handlePromise).resolves.toBeUndefined();
220
-
221
- deferred.resolve();
222
- await flushAsyncWork();
223
- expect(handler).toHaveBeenCalledOnce();
224
- // The listener no longer wraps message handlers with slow-listener logging.
225
- expect(logger.warn).not.toHaveBeenCalled();
226
- });
227
- });
228
-
229
- describe("discord allowlist helpers", () => {
230
- it("normalizes slugs", () => {
231
- expect(normalizeDiscordSlug("Friends of Klaw")).toBe("friends-of-klaw");
232
- expect(normalizeDiscordSlug("#General")).toBe("general");
233
- expect(normalizeDiscordSlug("Dev__Chat")).toBe("dev-chat");
234
- });
235
-
236
- it("matches ids by default and names only when enabled", () => {
237
- const allow = expectNormalizedAllowList(
238
- ["123", "steipete", "Friends of Klaw"],
239
- ["discord:", "user:", "guild:", "channel:"],
240
- );
241
- expect(allowListMatches(allow, { id: "123" })).toBe(true);
242
- expect(allowListMatches(allow, { name: "steipete" })).toBe(false);
243
- expect(allowListMatches(allow, { name: "friends-of-klaw" })).toBe(false);
244
- expect(allowListMatches(allow, { name: "steipete" }, { allowNameMatching: true })).toBe(true);
245
- expect(allowListMatches(allow, { name: "friends-of-klaw" }, { allowNameMatching: true })).toBe(
246
- true,
247
- );
248
- expect(allowListMatches(allow, { name: "other" })).toBe(false);
249
- });
250
-
251
- it("matches pk-prefixed allowlist entries", () => {
252
- const allow = expectNormalizedAllowList(["pk:member-123"], ["discord:", "user:", "pk:"]);
253
- expect(allowListMatches(allow, { id: "member-123" })).toBe(true);
254
- expect(allowListMatches(allow, { id: "member-999" })).toBe(false);
255
- });
256
-
257
- it("does not treat DM wildcard access as owner access", () => {
258
- const wildcardOnly = resolveDiscordOwnerAccess({
259
- allowFrom: ["*"],
260
- sender: { id: "123" },
261
- });
262
- expect(wildcardOnly.ownerAllowList).toBeNull();
263
- expect(wildcardOnly.ownerAllowed).toBe(false);
264
-
265
- const explicitOwner = resolveDiscordOwnerAccess({
266
- allowFrom: ["*", "user:123"],
267
- sender: { id: "123" },
268
- });
269
- if (explicitOwner.ownerAllowList === null) {
270
- throw new Error("Expected explicit owner allowlist");
271
- }
272
- expect(explicitOwner.ownerAllowList.allowAll).toBe(false);
273
- expect(explicitOwner.ownerAllowList.ids).toEqual(new Set(["123"]));
274
- expect(explicitOwner.ownerAllowed).toBe(true);
275
- });
276
- });
277
-
278
- describe("discord guild/channel resolution", () => {
279
- it("resolves guild entry by id", () => {
280
- const guildEntries = makeEntries({
281
- "123": { slug: "friends-of-klaw" },
282
- });
283
- const resolved = resolveDiscordGuildEntry({
284
- guild: fakeGuild("123", "Friends of Klaw"),
285
- guildEntries,
286
- });
287
- expect(resolved?.id).toBe("123");
288
- expect(resolved?.slug).toBe("friends-of-klaw");
289
- });
290
-
291
- it("resolves guild entry by raw guild id when guild object is missing", () => {
292
- const guildEntries = makeEntries({
293
- "123": { slug: "friends-of-klaw" },
294
- });
295
- const resolved = resolveDiscordGuildEntry({
296
- guildId: "123",
297
- guildEntries,
298
- });
299
- expect(resolved?.id).toBe("123");
300
- expect(resolved?.slug).toBe("friends-of-klaw");
301
- });
302
-
303
- it("resolves guild entry by slug key", () => {
304
- const guildEntries = makeEntries({
305
- "friends-of-klaw": { slug: "friends-of-klaw" },
306
- });
307
- const resolved = resolveDiscordGuildEntry({
308
- guild: fakeGuild("123", "Friends of Klaw"),
309
- guildEntries,
310
- });
311
- expect(resolved?.id).toBe("123");
312
- expect(resolved?.slug).toBe("friends-of-klaw");
313
- });
314
-
315
- it("falls back to wildcard guild entry", () => {
316
- const guildEntries = makeEntries({
317
- "*": { requireMention: false },
318
- });
319
- const resolved = resolveDiscordGuildEntry({
320
- guild: fakeGuild("123", "Friends of Klaw"),
321
- guildEntries,
322
- });
323
- expect(resolved?.id).toBe("123");
324
- expect(resolved?.requireMention).toBe(false);
325
- });
326
-
327
- it("resolves channel config by slug", () => {
328
- const guildInfo: DiscordGuildEntryResolved = {
329
- channels: {
330
- general: { enabled: true },
331
- help: {
332
- enabled: true,
333
- requireMention: true,
334
- skills: ["search"],
335
- users: ["123"],
336
- systemPrompt: "Use short answers.",
337
- autoThread: true,
338
- },
339
- },
340
- };
341
- const channel = resolveDiscordChannelConfig({
342
- guildInfo,
343
- channelId: "456",
344
- channelName: "General",
345
- channelSlug: "general",
346
- });
347
- expect(channel?.allowed).toBe(true);
348
- expect(channel?.requireMention).toBeUndefined();
349
-
350
- const help = resolveDiscordChannelConfig({
351
- guildInfo,
352
- channelId: "789",
353
- channelName: "Help",
354
- channelSlug: "help",
355
- });
356
- expect(help?.allowed).toBe(true);
357
- expect(help?.requireMention).toBe(true);
358
- expect(help?.skills).toEqual(["search"]);
359
- expect(help?.enabled).toBe(true);
360
- expect(help?.users).toEqual(["123"]);
361
- expect(help?.systemPrompt).toBe("Use short answers.");
362
- expect(help?.autoThread).toBe(true);
363
- });
364
-
365
- it("denies channel when config present but no match", () => {
366
- const guildInfo: DiscordGuildEntryResolved = {
367
- channels: {
368
- general: { enabled: true },
369
- },
370
- };
371
- const channel = resolveDiscordChannelConfig({
372
- guildInfo,
373
- channelId: "999",
374
- channelName: "random",
375
- channelSlug: "random",
376
- });
377
- expect(channel?.allowed).toBe(false);
378
- });
379
-
380
- it("treats empty channel config map as no channel allowlist", () => {
381
- const guildInfo: DiscordGuildEntryResolved = {
382
- channels: {},
383
- };
384
- const channel = resolveDiscordChannelConfig({
385
- guildInfo,
386
- channelId: "999",
387
- channelName: "random",
388
- channelSlug: "random",
389
- });
390
- expect(channel).toBeNull();
391
- });
392
-
393
- it("inherits parent config for thread channels", () => {
394
- const guildInfo: DiscordGuildEntryResolved = {
395
- channels: {
396
- general: { enabled: true },
397
- random: { enabled: false },
398
- },
399
- };
400
- const thread = resolveDiscordChannelConfigWithFallback({
401
- guildInfo,
402
- channelId: "thread-123",
403
- channelName: "topic",
404
- channelSlug: "topic",
405
- parentId: "999",
406
- parentName: "random",
407
- parentSlug: "random",
408
- scope: "thread",
409
- });
410
- expect(thread?.allowed).toBe(false);
411
- });
412
-
413
- it("does not match thread name/slug when resolving allowlists", () => {
414
- const guildInfo: DiscordGuildEntryResolved = {
415
- channels: {
416
- general: { enabled: true },
417
- random: { enabled: false },
418
- },
419
- };
420
- const thread = resolveDiscordChannelConfigWithFallback({
421
- guildInfo,
422
- channelId: "thread-999",
423
- channelName: "general",
424
- channelSlug: "general",
425
- parentId: "999",
426
- parentName: "random",
427
- parentSlug: "random",
428
- scope: "thread",
429
- });
430
- expect(thread?.allowed).toBe(false);
431
- });
432
-
433
- it("applies wildcard channel config when no specific match", () => {
434
- const guildInfo: DiscordGuildEntryResolved = {
435
- channels: {
436
- general: { enabled: true, requireMention: false },
437
- "*": { enabled: true, autoThread: true, requireMention: true },
438
- },
439
- };
440
- // Specific channel should NOT use wildcard
441
- const general = resolveDiscordChannelConfig({
442
- guildInfo,
443
- channelId: "123",
444
- channelName: "general",
445
- channelSlug: "general",
446
- });
447
- expect(general?.allowed).toBe(true);
448
- expect(general?.requireMention).toBe(false);
449
- expect(general?.autoThread).toBeUndefined();
450
- expect(general?.matchSource).toBe("direct");
451
-
452
- // Unknown channel should use wildcard
453
- const random = resolveDiscordChannelConfig({
454
- guildInfo,
455
- channelId: "999",
456
- channelName: "random",
457
- channelSlug: "random",
458
- });
459
- expect(random?.allowed).toBe(true);
460
- expect(random?.autoThread).toBe(true);
461
- expect(random?.requireMention).toBe(true);
462
- expect(random?.matchSource).toBe("wildcard");
463
- });
464
-
465
- it("falls back to wildcard when thread channel and parent are missing", () => {
466
- const guildInfo: DiscordGuildEntryResolved = {
467
- channels: {
468
- "*": { enabled: true, requireMention: false },
469
- },
470
- };
471
- const thread = resolveDiscordChannelConfigWithFallback({
472
- guildInfo,
473
- channelId: "thread-123",
474
- channelName: "topic",
475
- channelSlug: "topic",
476
- parentId: "parent-999",
477
- parentName: "general",
478
- parentSlug: "general",
479
- scope: "thread",
480
- });
481
- expect(thread?.allowed).toBe(true);
482
- expect(thread?.matchKey).toBe("*");
483
- expect(thread?.matchSource).toBe("wildcard");
484
- });
485
-
486
- it("treats empty channel config map as no thread allowlist", () => {
487
- const guildInfo: DiscordGuildEntryResolved = {
488
- channels: {},
489
- };
490
- const thread = resolveDiscordChannelConfigWithFallback({
491
- guildInfo,
492
- channelId: "thread-123",
493
- channelName: "topic",
494
- channelSlug: "topic",
495
- parentId: "parent-999",
496
- parentName: "general",
497
- parentSlug: "general",
498
- scope: "thread",
499
- });
500
- expect(thread).toBeNull();
501
- });
502
- });
503
-
504
- describe("discord mention gating", () => {
505
- it("requires mention by default", () => {
506
- const guildInfo: DiscordGuildEntryResolved = {
507
- requireMention: true,
508
- channels: {
509
- general: { enabled: true },
510
- },
511
- };
512
- const channelConfig = resolveDiscordChannelConfig({
513
- guildInfo,
514
- channelId: "1",
515
- channelName: "General",
516
- channelSlug: "general",
517
- });
518
- expect(
519
- resolveDiscordShouldRequireMention({
520
- isGuildMessage: true,
521
- isThread: false,
522
- channelConfig,
523
- guildInfo,
524
- }),
525
- ).toBe(true);
526
- });
527
-
528
- it("applies autoThread mention rules based on thread ownership", () => {
529
- const cases = [
530
- { name: "bot-owned thread", threadOwnerId: "bot123", expected: false },
531
- { name: "user-owned thread", threadOwnerId: "user456", expected: true },
532
- { name: "unknown thread owner", threadOwnerId: undefined, expected: true },
533
- ] as const;
534
-
535
- for (const testCase of cases) {
536
- const { guildInfo, channelConfig } = createAutoThreadMentionContext();
537
- expect(
538
- resolveDiscordShouldRequireMention({
539
- isGuildMessage: true,
540
- isThread: true,
541
- botId: "bot123",
542
- threadOwnerId: testCase.threadOwnerId,
543
- channelConfig,
544
- guildInfo,
545
- }),
546
- testCase.name,
547
- ).toBe(testCase.expected);
548
- }
549
- });
550
-
551
- it("inherits parent channel mention rules for threads", () => {
552
- const guildInfo: DiscordGuildEntryResolved = {
553
- requireMention: true,
554
- channels: {
555
- "parent-1": { enabled: true, requireMention: false },
556
- },
557
- };
558
- const channelConfig = resolveDiscordChannelConfigWithFallback({
559
- guildInfo,
560
- channelId: "thread-1",
561
- channelName: "topic",
562
- channelSlug: "topic",
563
- parentId: "parent-1",
564
- parentName: "Parent",
565
- parentSlug: "parent",
566
- scope: "thread",
567
- });
568
- expect(channelConfig?.matchSource).toBe("parent");
569
- expect(channelConfig?.matchKey).toBe("parent-1");
570
- expect(
571
- resolveDiscordShouldRequireMention({
572
- isGuildMessage: true,
573
- isThread: true,
574
- channelConfig,
575
- guildInfo,
576
- }),
577
- ).toBe(false);
578
- });
579
- });
580
-
581
- describe("discord groupPolicy gating", () => {
582
- it("applies open/disabled/allowlist policy rules", () => {
583
- const cases = [
584
- {
585
- name: "open policy always allows",
586
- input: {
587
- groupPolicy: "open" as const,
588
- guildAllowlisted: false,
589
- channelAllowlistConfigured: false,
590
- channelAllowed: false,
591
- },
592
- expected: true,
593
- },
594
- {
595
- name: "disabled policy always blocks",
596
- input: {
597
- groupPolicy: "disabled" as const,
598
- guildAllowlisted: true,
599
- channelAllowlistConfigured: true,
600
- channelAllowed: true,
601
- },
602
- expected: false,
603
- },
604
- {
605
- name: "allowlist blocks when guild not allowlisted",
606
- input: {
607
- groupPolicy: "allowlist" as const,
608
- guildAllowlisted: false,
609
- channelAllowlistConfigured: false,
610
- channelAllowed: true,
611
- },
612
- expected: false,
613
- },
614
- {
615
- name: "allowlist allows when guild allowlisted and no channel allowlist",
616
- input: {
617
- groupPolicy: "allowlist" as const,
618
- guildAllowlisted: true,
619
- channelAllowlistConfigured: false,
620
- channelAllowed: true,
621
- },
622
- expected: true,
623
- },
624
- {
625
- name: "allowlist allows when channel is allowed",
626
- input: {
627
- groupPolicy: "allowlist" as const,
628
- guildAllowlisted: true,
629
- channelAllowlistConfigured: true,
630
- channelAllowed: true,
631
- },
632
- expected: true,
633
- },
634
- {
635
- name: "allowlist blocks when channel is not allowed",
636
- input: {
637
- groupPolicy: "allowlist" as const,
638
- guildAllowlisted: true,
639
- channelAllowlistConfigured: true,
640
- channelAllowed: false,
641
- },
642
- expected: false,
643
- },
644
- ] as const;
645
-
646
- for (const testCase of cases) {
647
- expect(isDiscordGroupAllowedByPolicy(testCase.input), testCase.name).toBe(testCase.expected);
648
- }
649
- });
650
- });
651
-
652
- describe("discord group DM gating", () => {
653
- it("allows all when no allowlist", () => {
654
- expect(
655
- resolveGroupDmAllow({
656
- channels: undefined,
657
- channelId: "1",
658
- channelName: "dm",
659
- channelSlug: "dm",
660
- }),
661
- ).toBe(true);
662
- });
663
-
664
- it("matches group DM allowlist", () => {
665
- expect(
666
- resolveGroupDmAllow({
667
- channels: ["klaw-dm"],
668
- channelId: "1",
669
- channelName: "Klaw DM",
670
- channelSlug: "klaw-dm",
671
- }),
672
- ).toBe(true);
673
- expect(
674
- resolveGroupDmAllow({
675
- channels: ["klaw-dm"],
676
- channelId: "1",
677
- channelName: "Other",
678
- channelSlug: "other",
679
- }),
680
- ).toBe(false);
681
- });
682
- });
683
-
684
- describe("discord reply target selection", () => {
685
- it("handles off/first/all reply modes", () => {
686
- const cases = [
687
- { name: "off mode", replyToMode: "off" as const, hasReplied: false, expected: undefined },
688
- {
689
- name: "first mode before reply",
690
- replyToMode: "first" as const,
691
- hasReplied: false,
692
- expected: "123",
693
- },
694
- {
695
- name: "first mode after reply",
696
- replyToMode: "first" as const,
697
- hasReplied: true,
698
- expected: undefined,
699
- },
700
- {
701
- name: "all mode before reply",
702
- replyToMode: "all" as const,
703
- hasReplied: false,
704
- expected: "123",
705
- },
706
- {
707
- name: "all mode after reply",
708
- replyToMode: "all" as const,
709
- hasReplied: true,
710
- expected: "123",
711
- },
712
- ] as const;
713
-
714
- for (const testCase of cases) {
715
- expect(
716
- resolveDiscordReplyTarget({
717
- replyToMode: testCase.replyToMode,
718
- replyToId: "123",
719
- hasReplied: testCase.hasReplied,
720
- }),
721
- testCase.name,
722
- ).toBe(testCase.expected);
723
- }
724
- });
725
- });
726
-
727
- describe("discord autoThread name sanitization", () => {
728
- it("strips mentions and collapses whitespace", () => {
729
- const name = sanitizeDiscordThreadName(" <@123> <@&456> <#789> Help here ", "msg-1");
730
- expect(name).toBe("Help here");
731
- });
732
-
733
- it("falls back to thread + id when empty after cleaning", () => {
734
- const name = sanitizeDiscordThreadName(" <@123>", "abc");
735
- expect(name).toBe("Thread abc");
736
- });
737
- });
738
-
739
- describe("discord reaction notification gating", () => {
740
- it("applies mode-specific reaction notification rules", () => {
741
- const cases = typedCases<{
742
- name: string;
743
- input: Parameters<typeof shouldEmitDiscordReactionNotification>[0];
744
- expected: boolean;
745
- }>([
746
- {
747
- name: "unset defaults to own (author is bot)",
748
- input: {
749
- mode: undefined,
750
- botId: "bot-1",
751
- messageAuthorId: "bot-1",
752
- userId: "user-1",
753
- },
754
- expected: true,
755
- },
756
- {
757
- name: "unset defaults to own (author is not bot)",
758
- input: {
759
- mode: undefined,
760
- botId: "bot-1",
761
- messageAuthorId: "user-1",
762
- userId: "user-2",
763
- },
764
- expected: false,
765
- },
766
- {
767
- name: "off mode",
768
- input: {
769
- mode: "off" as const,
770
- botId: "bot-1",
771
- messageAuthorId: "bot-1",
772
- userId: "user-1",
773
- },
774
- expected: false,
775
- },
776
- {
777
- name: "all mode",
778
- input: {
779
- mode: "all" as const,
780
- botId: "bot-1",
781
- messageAuthorId: "user-1",
782
- userId: "user-2",
783
- },
784
- expected: true,
785
- },
786
- {
787
- name: "all mode blocks non-allowlisted guild member",
788
- input: {
789
- mode: "all" as const,
790
- botId: "bot-1",
791
- messageAuthorId: "user-1",
792
- userId: "user-2",
793
- guildInfo: { users: ["trusted-user"] },
794
- },
795
- expected: false,
796
- },
797
- {
798
- name: "own mode with bot-authored message",
799
- input: {
800
- mode: "own" as const,
801
- botId: "bot-1",
802
- messageAuthorId: "bot-1",
803
- userId: "user-2",
804
- },
805
- expected: true,
806
- },
807
- {
808
- name: "own mode with non-bot-authored message",
809
- input: {
810
- mode: "own" as const,
811
- botId: "bot-1",
812
- messageAuthorId: "user-2",
813
- userId: "user-3",
814
- },
815
- expected: false,
816
- },
817
- {
818
- name: "own mode still blocks member outside users allowlist",
819
- input: {
820
- mode: "own" as const,
821
- botId: "bot-1",
822
- messageAuthorId: "bot-1",
823
- userId: "user-3",
824
- guildInfo: { users: ["trusted-user"] },
825
- },
826
- expected: false,
827
- },
828
- {
829
- name: "allowlist mode without match",
830
- input: {
831
- mode: "allowlist" as const,
832
- botId: "bot-1",
833
- messageAuthorId: "user-1",
834
- userId: "user-2",
835
- allowlist: [] as string[],
836
- },
837
- expected: false,
838
- },
839
- {
840
- name: "allowlist mode with id match",
841
- input: {
842
- mode: "allowlist" as const,
843
- botId: "bot-1",
844
- messageAuthorId: "user-1",
845
- userId: "123",
846
- userName: "steipete",
847
- guildInfo: { users: ["123", "other"] },
848
- },
849
- expected: true,
850
- },
851
- {
852
- name: "allowlist mode does not match usernames by default",
853
- input: {
854
- mode: "allowlist" as const,
855
- botId: "bot-1",
856
- messageAuthorId: "user-1",
857
- userId: "999",
858
- userName: "trusted-user",
859
- guildInfo: { users: ["trusted-user"] },
860
- },
861
- expected: false,
862
- },
863
- {
864
- name: "allowlist mode matches usernames when explicitly enabled",
865
- input: {
866
- mode: "allowlist" as const,
867
- botId: "bot-1",
868
- messageAuthorId: "user-1",
869
- userId: "999",
870
- userName: "trusted-user",
871
- guildInfo: { users: ["trusted-user"] },
872
- allowNameMatching: true,
873
- },
874
- expected: true,
875
- },
876
- {
877
- name: "allowlist mode matches allowed role",
878
- input: {
879
- mode: "allowlist" as const,
880
- botId: "bot-1",
881
- messageAuthorId: "user-1",
882
- userId: "999",
883
- guildInfo: { roles: ["role:trusted-role"] },
884
- memberRoleIds: ["trusted-role"],
885
- },
886
- expected: true,
887
- },
888
- ]);
889
-
890
- for (const testCase of cases) {
891
- expect(
892
- shouldEmitDiscordReactionNotification({
893
- ...testCase.input,
894
- }),
895
- testCase.name,
896
- ).toBe(testCase.expected);
897
- }
898
- });
899
- });
900
-
901
- describe("discord media payload", () => {
902
- it("preserves attachment order for MediaPaths/MediaUrls", () => {
903
- const payload = buildDiscordMediaPayload([
904
- { path: "/tmp/a.png", contentType: "image/png" },
905
- { path: "/tmp/b.png", contentType: "image/png" },
906
- { path: "/tmp/c.png", contentType: "image/png" },
907
- ]);
908
- expect(payload.MediaPath).toBe("/tmp/a.png");
909
- expect(payload.MediaUrl).toBe("/tmp/a.png");
910
- expect(payload.MediaType).toBe("image/png");
911
- expect(payload.MediaPaths).toEqual(["/tmp/a.png", "/tmp/b.png", "/tmp/c.png"]);
912
- expect(payload.MediaUrls).toEqual(["/tmp/a.png", "/tmp/b.png", "/tmp/c.png"]);
913
- });
914
- });
915
-
916
- // --- DM reaction integration tests ---
917
- // These test that handleDiscordReactionEvent (via DiscordReactionListener)
918
- // properly handles DM reactions instead of silently dropping them.
919
-
920
- const { enqueueSystemEventSpy, resolveAgentRouteMock } = vi.hoisted(() => ({
921
- enqueueSystemEventSpy: vi.fn(),
922
- resolveAgentRouteMock: vi.fn((params: unknown) => ({
923
- agentId: "default",
924
- channel: "discord",
925
- accountId: "acc-1",
926
- sessionKey: "discord:acc-1:dm:user-1",
927
- mainSessionKey: "discord:acc-1:dm:user-1",
928
- lastRoutePolicy: "session" as const,
929
- matchedBy: "default" as const,
930
- ...(typeof params === "object" && params !== null ? { _params: params } : {}),
931
- })),
932
- }));
933
-
934
- const channelRuntimeModule = await import("klaw/plugin-sdk/system-event-runtime");
935
- vi.spyOn(channelRuntimeModule, "enqueueSystemEvent").mockImplementation(enqueueSystemEventSpy);
936
-
937
- const routingModule = await import("klaw/plugin-sdk/routing");
938
- vi.spyOn(routingModule, "resolveAgentRoute").mockImplementation(resolveAgentRouteMock);
939
-
940
- const { DiscordMessageListener, DiscordReactionListener, registerDiscordListener } =
941
- await import("./monitor/listeners.js");
942
-
943
- type MockWithCalls = { mock: { calls: unknown[][] } };
944
-
945
- function firstMockCall(mock: MockWithCalls, label: string): unknown[] {
946
- const call = mock.mock.calls.at(0);
947
- if (!call) {
948
- throw new Error(`expected ${label} call`);
949
- }
950
- return call;
951
- }
952
-
953
- function firstMockArg(mock: MockWithCalls, label: string) {
954
- return firstMockCall(mock, label)[0];
955
- }
956
-
957
- function requireRecord(value: unknown, label: string): Record<string, unknown> {
958
- if (typeof value !== "object" || value === null) {
959
- throw new Error(`expected ${label} to be an object`);
960
- }
961
- return value as Record<string, unknown>;
962
- }
963
-
964
- function makeReactionEvent(overrides?: {
965
- guildId?: string;
966
- channelId?: string;
967
- userId?: string;
968
- messageId?: string;
969
- emojiName?: string;
970
- botAsAuthor?: boolean;
971
- messageAuthorId?: string;
972
- messageFetch?: ReturnType<typeof vi.fn>;
973
- guild?: { name?: string; id?: string };
974
- memberRoleIds?: string[];
975
- }) {
976
- const userId = overrides?.userId ?? "user-1";
977
- const messageId = overrides?.messageId ?? "msg-1";
978
- const channelId = overrides?.channelId ?? "channel-1";
979
- const messageFetch =
980
- overrides?.messageFetch ??
981
- vi.fn(async () => ({
982
- author: {
983
- id: overrides?.messageAuthorId ?? (overrides?.botAsAuthor ? "bot-1" : "other-user"),
984
- username: overrides?.botAsAuthor ? "bot" : "otheruser",
985
- discriminator: "0",
986
- },
987
- }));
988
- return {
989
- guild_id: overrides?.guildId,
990
- channel_id: channelId,
991
- message_id: messageId,
992
- emoji: { name: overrides?.emojiName ?? "👍", id: null },
993
- guild: overrides?.guild,
994
- rawMember: overrides?.memberRoleIds ? { roles: overrides.memberRoleIds } : undefined,
995
- user: {
996
- id: userId,
997
- bot: false,
998
- username: "testuser",
999
- discriminator: "0",
1000
- },
1001
- message: {
1002
- fetch: messageFetch,
1003
- },
1004
- } as unknown as DiscordReactionEvent;
1005
- }
1006
-
1007
- function makeReactionClient(options?: {
1008
- channelType?: ChannelType;
1009
- channelName?: string;
1010
- parentId?: string;
1011
- parentName?: string;
1012
- }) {
1013
- const channelType = options?.channelType ?? ChannelType.DM;
1014
- const channelName =
1015
- options?.channelName ?? (channelType === ChannelType.DM ? undefined : "test-channel");
1016
- const parentId = options?.parentId;
1017
- const parentName = options?.parentName ?? "parent-channel";
1018
-
1019
- return {
1020
- fetchChannel: vi.fn(async (channelId: string) => {
1021
- if (parentId && channelId === parentId) {
1022
- return { type: ChannelType.GuildText, name: parentName, parentId: undefined };
1023
- }
1024
- return { type: channelType, name: channelName, parentId };
1025
- }),
1026
- } as unknown as DiscordReactionClient;
1027
- }
1028
-
1029
- function getReactionClientFetchChannelMock(client: DiscordReactionClient) {
1030
- return (client as unknown as { fetchChannel: ReturnType<typeof vi.fn> }).fetchChannel;
1031
- }
1032
-
1033
- function makeReactionListenerParams(overrides?: {
1034
- botUserId?: string;
1035
- dmEnabled?: boolean;
1036
- groupDmEnabled?: boolean;
1037
- groupDmChannels?: string[];
1038
- dmPolicy?: "open" | "pairing" | "allowlist" | "disabled";
1039
- allowFrom?: string[];
1040
- groupPolicy?: "open" | "allowlist" | "disabled";
1041
- allowNameMatching?: boolean;
1042
- guildEntries?: Record<string, DiscordGuildEntryResolved>;
1043
- }) {
1044
- return {
1045
- cfg: {} as import("klaw/plugin-sdk/config-contracts").KlawConfig,
1046
- accountId: "acc-1",
1047
- runtime: {} as import("klaw/plugin-sdk/runtime-env").RuntimeEnv,
1048
- botUserId: overrides?.botUserId ?? "bot-1",
1049
- dmEnabled: overrides?.dmEnabled ?? true,
1050
- groupDmEnabled: overrides?.groupDmEnabled ?? true,
1051
- groupDmChannels: overrides?.groupDmChannels ?? [],
1052
- dmPolicy: overrides?.dmPolicy ?? "open",
1053
- allowFrom: overrides?.allowFrom ?? ["*"],
1054
- groupPolicy: overrides?.groupPolicy ?? "open",
1055
- allowNameMatching: overrides?.allowNameMatching ?? false,
1056
- guildEntries: overrides?.guildEntries,
1057
- logger: {
1058
- info: vi.fn(),
1059
- warn: vi.fn(),
1060
- error: vi.fn(),
1061
- debug: vi.fn(),
1062
- } as unknown as ReturnType<typeof import("klaw/plugin-sdk/logging-core").createSubsystemLogger>,
1063
- };
1064
- }
1065
-
1066
- describe("discord DM reaction handling", () => {
1067
- beforeEach(() => {
1068
- enqueueSystemEventSpy.mockClear();
1069
- resolveAgentRouteMock.mockClear();
1070
- readAllowFromStoreMock.mockReset().mockResolvedValue([]);
1071
- });
1072
-
1073
- it("processes DM reactions with or without guild allowlists", async () => {
1074
- const cases = [
1075
- { name: "no guild allowlist", guildEntries: undefined },
1076
- {
1077
- name: "guild allowlist configured",
1078
- guildEntries: makeEntries({
1079
- "guild-123": { slug: "guild-123" },
1080
- }),
1081
- },
1082
- ] as const;
1083
-
1084
- for (const testCase of cases) {
1085
- enqueueSystemEventSpy.mockClear();
1086
- resolveAgentRouteMock.mockClear();
1087
-
1088
- const data = makeReactionEvent({ botAsAuthor: true });
1089
- const client = makeReactionClient({ channelType: ChannelType.DM });
1090
- const listener = new DiscordReactionListener(
1091
- makeReactionListenerParams({ guildEntries: testCase.guildEntries }),
1092
- );
1093
-
1094
- await listener.handle(data, client);
1095
-
1096
- expect(enqueueSystemEventSpy, testCase.name).toHaveBeenCalledOnce();
1097
- const [text, opts] = firstMockCall(enqueueSystemEventSpy, "enqueueSystemEvent");
1098
- expect(text, testCase.name).toContain("Discord reaction added");
1099
- expect(text, testCase.name).toContain("👍");
1100
- expect(text, testCase.name).toContain("dm");
1101
- expect(text, testCase.name).not.toContain("undefined");
1102
- expect(requireRecord(opts, "system event options").sessionKey, testCase.name).toBe(
1103
- "discord:acc-1:dm:user-1",
1104
- );
1105
- }
1106
- });
1107
-
1108
- it("blocks DM reactions when dmPolicy is disabled", async () => {
1109
- const data = makeReactionEvent({ botAsAuthor: true });
1110
- const client = makeReactionClient({ channelType: ChannelType.DM });
1111
- const listener = new DiscordReactionListener(
1112
- makeReactionListenerParams({ dmPolicy: "disabled" }),
1113
- );
1114
-
1115
- await listener.handle(data, client);
1116
-
1117
- expect(enqueueSystemEventSpy).not.toHaveBeenCalled();
1118
- });
1119
-
1120
- it("blocks DM reactions for unauthorized sender in allowlist mode", async () => {
1121
- const data = makeReactionEvent({ botAsAuthor: true, userId: "user-1" });
1122
- const client = makeReactionClient({ channelType: ChannelType.DM });
1123
- const listener = new DiscordReactionListener(
1124
- makeReactionListenerParams({
1125
- dmPolicy: "allowlist",
1126
- allowFrom: ["user:user-2"],
1127
- }),
1128
- );
1129
-
1130
- await listener.handle(data, client);
1131
-
1132
- expect(enqueueSystemEventSpy).not.toHaveBeenCalled();
1133
- });
1134
-
1135
- it("allows DM reactions for authorized sender in allowlist mode", async () => {
1136
- const data = makeReactionEvent({ botAsAuthor: true, userId: "user-1" });
1137
- const client = makeReactionClient({ channelType: ChannelType.DM });
1138
- const listener = new DiscordReactionListener(
1139
- makeReactionListenerParams({
1140
- dmPolicy: "allowlist",
1141
- allowFrom: ["user:user-1"],
1142
- }),
1143
- );
1144
-
1145
- await listener.handle(data, client);
1146
-
1147
- expect(enqueueSystemEventSpy).toHaveBeenCalledOnce();
1148
- });
1149
-
1150
- it("blocks group DM reactions when group DMs are disabled", async () => {
1151
- const data = makeReactionEvent({ botAsAuthor: true });
1152
- const client = makeReactionClient({ channelType: ChannelType.GroupDM });
1153
- const listener = new DiscordReactionListener(
1154
- makeReactionListenerParams({ groupDmEnabled: false }),
1155
- );
1156
-
1157
- await listener.handle(data, client);
1158
-
1159
- expect(enqueueSystemEventSpy).not.toHaveBeenCalled();
1160
- });
1161
-
1162
- it("blocks guild reactions when groupPolicy is disabled", async () => {
1163
- const data = makeReactionEvent({
1164
- guildId: "guild-123",
1165
- botAsAuthor: true,
1166
- guild: { id: "guild-123", name: "Guild" },
1167
- });
1168
- const client = makeReactionClient({ channelType: ChannelType.GuildText });
1169
- const listener = new DiscordReactionListener(
1170
- makeReactionListenerParams({ groupPolicy: "disabled" }),
1171
- );
1172
-
1173
- await listener.handle(data, client);
1174
-
1175
- expect(getReactionClientFetchChannelMock(client)).not.toHaveBeenCalled();
1176
- expect(enqueueSystemEventSpy).not.toHaveBeenCalled();
1177
- });
1178
-
1179
- it("blocks guild reactions for sender outside users allowlist", async () => {
1180
- const data = makeReactionEvent({
1181
- guildId: "guild-123",
1182
- userId: "attacker-user",
1183
- botAsAuthor: true,
1184
- guild: { id: "guild-123", name: "Test Guild" },
1185
- });
1186
- const client = makeReactionClient({ channelType: ChannelType.GuildText });
1187
- const listener = new DiscordReactionListener(
1188
- makeReactionListenerParams({
1189
- guildEntries: makeEntries({
1190
- "guild-123": {
1191
- users: ["user:trusted-user"],
1192
- },
1193
- }),
1194
- }),
1195
- );
1196
-
1197
- await listener.handle(data, client);
1198
-
1199
- expect(enqueueSystemEventSpy).not.toHaveBeenCalled();
1200
- expect(resolveAgentRouteMock).not.toHaveBeenCalled();
1201
- });
1202
-
1203
- it("allows guild reactions for sender in channel role allowlist override", async () => {
1204
- resolveAgentRouteMock.mockReturnValueOnce({
1205
- agentId: "default",
1206
- channel: "discord",
1207
- accountId: "acc-1",
1208
- sessionKey: "discord:acc-1:guild-123:channel-1",
1209
- mainSessionKey: "discord:acc-1:guild-123:channel-1",
1210
- lastRoutePolicy: "session",
1211
- matchedBy: "default",
1212
- });
1213
-
1214
- const data = makeReactionEvent({
1215
- guildId: "guild-123",
1216
- userId: "member-user",
1217
- botAsAuthor: true,
1218
- guild: { id: "guild-123", name: "Test Guild" },
1219
- memberRoleIds: ["trusted-role"],
1220
- });
1221
- const client = makeReactionClient({ channelType: ChannelType.GuildText });
1222
- const listener = new DiscordReactionListener(
1223
- makeReactionListenerParams({
1224
- guildEntries: makeEntries({
1225
- "guild-123": {
1226
- roles: ["role:blocked-role"],
1227
- channels: {
1228
- "channel-1": {
1229
- enabled: true,
1230
- roles: ["role:trusted-role"],
1231
- },
1232
- },
1233
- },
1234
- }),
1235
- }),
1236
- );
1237
-
1238
- await listener.handle(data, client);
1239
-
1240
- expect(getReactionClientFetchChannelMock(client)).toHaveBeenCalled();
1241
- expect(enqueueSystemEventSpy).toHaveBeenCalledOnce();
1242
- const text = firstMockArg(enqueueSystemEventSpy, "enqueueSystemEvent");
1243
- expect(text).toContain("Discord reaction added");
1244
- });
1245
-
1246
- it("routes DM reactions with peer kind 'direct' and user id", async () => {
1247
- enqueueSystemEventSpy.mockClear();
1248
- resolveAgentRouteMock.mockClear();
1249
-
1250
- const data = makeReactionEvent({ userId: "user-42", botAsAuthor: true });
1251
- const client = makeReactionClient({ channelType: ChannelType.DM });
1252
- const listener = new DiscordReactionListener(makeReactionListenerParams());
1253
-
1254
- await listener.handle(data, client);
1255
-
1256
- expect(resolveAgentRouteMock).toHaveBeenCalledOnce();
1257
- const routeArgs = firstMockArg(resolveAgentRouteMock, "resolveAgentRoute") as {
1258
- peer?: unknown;
1259
- };
1260
- expect(routeArgs.peer).toEqual({ kind: "direct", id: "user-42" });
1261
- });
1262
-
1263
- it("routes group DM reactions with peer kind 'group'", async () => {
1264
- enqueueSystemEventSpy.mockClear();
1265
- resolveAgentRouteMock.mockClear();
1266
-
1267
- const data = makeReactionEvent({ botAsAuthor: true });
1268
- const client = makeReactionClient({ channelType: ChannelType.GroupDM });
1269
- const listener = new DiscordReactionListener(makeReactionListenerParams());
1270
-
1271
- await listener.handle(data, client);
1272
-
1273
- expect(resolveAgentRouteMock).toHaveBeenCalledOnce();
1274
- const routeArgs = firstMockArg(resolveAgentRouteMock, "resolveAgentRoute") as {
1275
- peer?: unknown;
1276
- };
1277
- expect(routeArgs.peer).toEqual({ kind: "group", id: "channel-1" });
1278
- });
1279
- });
1280
-
1281
- describe("discord reaction notification modes", () => {
1282
- const guildId = "guild-900";
1283
- const guild = fakeGuild(guildId, "Mode Guild");
1284
-
1285
- it("applies message-fetch behavior across notification modes and channel types", async () => {
1286
- const cases = typedCases<{
1287
- name: string;
1288
- reactionNotifications: "off" | "all" | "allowlist" | "own";
1289
- users: string[] | undefined;
1290
- userId: string | undefined;
1291
- channelType: ChannelType;
1292
- channelId: string | undefined;
1293
- parentId: string | undefined;
1294
- messageAuthorId: string;
1295
- expectedFetchChannelCalls: number;
1296
- expectedMessageFetchCalls: number;
1297
- expectedEnqueueCalls: number;
1298
- }>([
1299
- {
1300
- name: "off mode",
1301
- reactionNotifications: "off" as const,
1302
- users: undefined,
1303
- userId: undefined,
1304
- channelType: ChannelType.GuildText,
1305
- channelId: undefined,
1306
- parentId: undefined,
1307
- messageAuthorId: "other-user",
1308
- expectedFetchChannelCalls: 0,
1309
- expectedMessageFetchCalls: 0,
1310
- expectedEnqueueCalls: 0,
1311
- },
1312
- {
1313
- name: "all mode",
1314
- reactionNotifications: "all" as const,
1315
- users: undefined,
1316
- userId: undefined,
1317
- channelType: ChannelType.GuildText,
1318
- channelId: undefined,
1319
- parentId: undefined,
1320
- messageAuthorId: "other-user",
1321
- expectedFetchChannelCalls: 1,
1322
- expectedMessageFetchCalls: 0,
1323
- expectedEnqueueCalls: 1,
1324
- },
1325
- {
1326
- name: "allowlist mode",
1327
- reactionNotifications: "allowlist" as const,
1328
- users: ["123"] as string[],
1329
- userId: "123",
1330
- channelType: ChannelType.GuildText,
1331
- channelId: undefined,
1332
- parentId: undefined,
1333
- messageAuthorId: "other-user",
1334
- expectedFetchChannelCalls: 1,
1335
- expectedMessageFetchCalls: 0,
1336
- expectedEnqueueCalls: 1,
1337
- },
1338
- {
1339
- name: "allowlist mode denied without channel overrides",
1340
- reactionNotifications: "allowlist" as const,
1341
- users: ["trusted-user"] as string[],
1342
- userId: "untrusted-user",
1343
- channelType: ChannelType.GuildText,
1344
- channelId: undefined,
1345
- parentId: undefined,
1346
- messageAuthorId: "other-user",
1347
- expectedFetchChannelCalls: 0,
1348
- expectedMessageFetchCalls: 0,
1349
- expectedEnqueueCalls: 0,
1350
- },
1351
- {
1352
- name: "own mode",
1353
- reactionNotifications: "own" as const,
1354
- users: undefined,
1355
- userId: undefined,
1356
- channelType: ChannelType.GuildText,
1357
- channelId: undefined,
1358
- parentId: undefined,
1359
- messageAuthorId: "bot-1",
1360
- expectedFetchChannelCalls: 1,
1361
- expectedMessageFetchCalls: 1,
1362
- expectedEnqueueCalls: 1,
1363
- },
1364
- {
1365
- name: "all mode thread channel",
1366
- reactionNotifications: "all" as const,
1367
- users: undefined,
1368
- userId: undefined,
1369
- channelType: ChannelType.PublicThread,
1370
- channelId: "thread-1",
1371
- parentId: "parent-1",
1372
- messageAuthorId: "other-user",
1373
- expectedFetchChannelCalls: 2,
1374
- expectedMessageFetchCalls: 0,
1375
- expectedEnqueueCalls: 1,
1376
- },
1377
- ]);
1378
-
1379
- for (const testCase of cases) {
1380
- enqueueSystemEventSpy.mockClear();
1381
- resolveAgentRouteMock.mockClear();
1382
-
1383
- const messageFetch = vi.fn(async () => ({
1384
- author: { id: testCase.messageAuthorId, username: "author", discriminator: "0" },
1385
- }));
1386
- const data = makeReactionEvent({
1387
- guildId,
1388
- guild,
1389
- userId: testCase.userId,
1390
- channelId: testCase.channelId,
1391
- messageFetch,
1392
- });
1393
- const client = makeReactionClient({
1394
- channelType: testCase.channelType,
1395
- parentId: testCase.parentId,
1396
- });
1397
- const guildEntries = makeEntries({
1398
- [guildId]: {
1399
- reactionNotifications: testCase.reactionNotifications,
1400
- users: testCase.users ? [...testCase.users] : undefined,
1401
- },
1402
- });
1403
- const listener = new DiscordReactionListener(makeReactionListenerParams({ guildEntries }));
1404
-
1405
- await listener.handle(data, client);
1406
-
1407
- expect(getReactionClientFetchChannelMock(client), testCase.name).toHaveBeenCalledTimes(
1408
- testCase.expectedFetchChannelCalls,
1409
- );
1410
- expect(messageFetch, testCase.name).toHaveBeenCalledTimes(testCase.expectedMessageFetchCalls);
1411
- expect(enqueueSystemEventSpy, testCase.name).toHaveBeenCalledTimes(
1412
- testCase.expectedEnqueueCalls,
1413
- );
1414
- }
1415
- });
1416
- });