@ashx-j/lunr 0.2.25 → 0.2.26
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.
- package/CHANGELOG.md +16 -1
- package/dist/builtin-extensions/index.d.ts.map +1 -1
- package/dist/builtin-extensions/index.js +1 -0
- package/dist/builtin-extensions/index.js.map +1 -1
- package/dist/builtin-extensions/lunr-computer-use.d.ts +3 -0
- package/dist/builtin-extensions/lunr-computer-use.d.ts.map +1 -0
- package/dist/builtin-extensions/lunr-computer-use.js +179 -0
- package/dist/builtin-extensions/lunr-computer-use.js.map +1 -0
- package/dist/builtin-extensions/pi-subagents/src/extension/schemas.d.ts.map +1 -1
- package/dist/builtin-extensions/pi-subagents/src/extension/schemas.js +1 -1
- package/dist/builtin-extensions/pi-subagents/src/extension/schemas.js.map +1 -1
- package/dist/builtin-extensions/pi-subagents/src/extension/tool-description.d.ts +4 -4
- package/dist/builtin-extensions/pi-subagents/src/extension/tool-description.d.ts.map +1 -1
- package/dist/builtin-extensions/pi-subagents/src/extension/tool-description.js +8 -5
- package/dist/builtin-extensions/pi-subagents/src/extension/tool-description.js.map +1 -1
- package/dist/builtin-extensions/pi-subagents/src/runs/background/subagent-runner.d.ts +1 -0
- package/dist/builtin-extensions/pi-subagents/src/runs/background/subagent-runner.d.ts.map +1 -1
- package/dist/builtin-extensions/pi-subagents/src/runs/background/subagent-runner.js +11 -2
- package/dist/builtin-extensions/pi-subagents/src/runs/background/subagent-runner.js.map +1 -1
- package/dist/builtin-extensions/pi-subagents/src/runs/foreground/chain-execution.d.ts.map +1 -1
- package/dist/builtin-extensions/pi-subagents/src/runs/foreground/chain-execution.js +3 -3
- package/dist/builtin-extensions/pi-subagents/src/runs/foreground/chain-execution.js.map +1 -1
- package/dist/builtin-extensions/pi-subagents/src/runs/foreground/execution.d.ts.map +1 -1
- package/dist/builtin-extensions/pi-subagents/src/runs/foreground/execution.js +15 -1
- package/dist/builtin-extensions/pi-subagents/src/runs/foreground/execution.js.map +1 -1
- package/dist/builtin-extensions/pi-subagents/src/runs/shared/chain-outputs.d.ts +1 -0
- package/dist/builtin-extensions/pi-subagents/src/runs/shared/chain-outputs.d.ts.map +1 -1
- package/dist/builtin-extensions/pi-subagents/src/runs/shared/chain-outputs.js +5 -0
- package/dist/builtin-extensions/pi-subagents/src/runs/shared/chain-outputs.js.map +1 -1
- package/dist/builtin-extensions/pi-subagents/src/runs/shared/child-tools.d.ts +1 -1
- package/dist/builtin-extensions/pi-subagents/src/runs/shared/child-tools.d.ts.map +1 -1
- package/dist/builtin-extensions/pi-subagents/src/runs/shared/child-tools.js +2 -0
- package/dist/builtin-extensions/pi-subagents/src/runs/shared/child-tools.js.map +1 -1
- package/dist/builtin-extensions/pi-subagents/src/tui/communication.d.ts.map +1 -1
- package/dist/builtin-extensions/pi-subagents/src/tui/communication.js +1 -1
- package/dist/builtin-extensions/pi-subagents/src/tui/communication.js.map +1 -1
- package/dist/builtin-extensions/pi-subagents/src/tui/render.d.ts +1 -0
- package/dist/builtin-extensions/pi-subagents/src/tui/render.d.ts.map +1 -1
- package/dist/builtin-extensions/pi-subagents/src/tui/render.js +5 -3
- package/dist/builtin-extensions/pi-subagents/src/tui/render.js.map +1 -1
- package/dist/core/computer-use/adapter.d.ts +28 -0
- package/dist/core/computer-use/adapter.d.ts.map +1 -0
- package/dist/core/computer-use/adapter.js +133 -0
- package/dist/core/computer-use/adapter.js.map +1 -0
- package/dist/core/computer-use/image.d.ts +27 -0
- package/dist/core/computer-use/image.d.ts.map +1 -0
- package/dist/core/computer-use/image.js +72 -0
- package/dist/core/computer-use/image.js.map +1 -0
- package/dist/core/computer-use/lease.d.ts +18 -0
- package/dist/core/computer-use/lease.d.ts.map +1 -0
- package/dist/core/computer-use/lease.js +129 -0
- package/dist/core/computer-use/lease.js.map +1 -0
- package/dist/core/computer-use/macos-runtime.d.ts +14 -0
- package/dist/core/computer-use/macos-runtime.d.ts.map +1 -0
- package/dist/core/computer-use/macos-runtime.js +148 -0
- package/dist/core/computer-use/macos-runtime.js.map +1 -0
- package/dist/core/computer-use/policy.d.ts +19 -0
- package/dist/core/computer-use/policy.d.ts.map +1 -0
- package/dist/core/computer-use/policy.js +67 -0
- package/dist/core/computer-use/policy.js.map +1 -0
- package/dist/core/computer-use/release.generated.d.ts +31 -0
- package/dist/core/computer-use/release.generated.d.ts.map +1 -0
- package/dist/core/computer-use/release.generated.js +36 -0
- package/dist/core/computer-use/release.generated.js.map +1 -0
- package/dist/core/computer-use/runtime.d.ts +13 -0
- package/dist/core/computer-use/runtime.d.ts.map +1 -0
- package/dist/core/computer-use/runtime.js +158 -0
- package/dist/core/computer-use/runtime.js.map +1 -0
- package/dist/core/computer-use/schemas.d.ts +100 -0
- package/dist/core/computer-use/schemas.d.ts.map +1 -0
- package/dist/core/computer-use/schemas.js +79 -0
- package/dist/core/computer-use/schemas.js.map +1 -0
- package/dist/core/computer-use/windows-desktop.d.ts +3 -0
- package/dist/core/computer-use/windows-desktop.d.ts.map +1 -0
- package/dist/core/computer-use/windows-desktop.js +48 -0
- package/dist/core/computer-use/windows-desktop.js.map +1 -0
- package/dist/core/computer-use/workflow.d.ts +38 -0
- package/dist/core/computer-use/workflow.d.ts.map +1 -0
- package/dist/core/computer-use/workflow.js +560 -0
- package/dist/core/computer-use/workflow.js.map +1 -0
- package/dist/core/extensions/loader.d.ts.map +1 -1
- package/dist/core/extensions/loader.js +4 -0
- package/dist/core/extensions/loader.js.map +1 -1
- package/dist/core/permissions.d.ts +1 -1
- package/dist/core/permissions.d.ts.map +1 -1
- package/dist/core/permissions.js +14 -3
- package/dist/core/permissions.js.map +1 -1
- package/dist/core/plan-mode.d.ts.map +1 -1
- package/dist/core/plan-mode.js +9 -0
- package/dist/core/plan-mode.js.map +1 -1
- package/dist/core/settings-manager.d.ts +6 -0
- package/dist/core/settings-manager.d.ts.map +1 -1
- package/dist/core/settings-manager.js +19 -0
- package/dist/core/settings-manager.js.map +1 -1
- package/dist/core/subagent-event-transport.d.ts +3 -0
- package/dist/core/subagent-event-transport.d.ts.map +1 -0
- package/dist/core/subagent-event-transport.js +61 -0
- package/dist/core/subagent-event-transport.js.map +1 -0
- package/dist/gateway/adapters/discord.d.ts.map +1 -1
- package/dist/gateway/adapters/discord.js +2 -2
- package/dist/gateway/adapters/discord.js.map +1 -1
- package/dist/gateway/approval.d.ts.map +1 -1
- package/dist/gateway/approval.js +3 -3
- package/dist/gateway/approval.js.map +1 -1
- package/dist/gateway/authz.d.ts +4 -4
- package/dist/gateway/authz.d.ts.map +1 -1
- package/dist/gateway/authz.js +8 -12
- package/dist/gateway/authz.js.map +1 -1
- package/dist/gateway/commands.d.ts.map +1 -1
- package/dist/gateway/commands.js +2 -6
- package/dist/gateway/commands.js.map +1 -1
- package/dist/gateway/cron.d.ts.map +1 -1
- package/dist/gateway/cron.js +20 -6
- package/dist/gateway/cron.js.map +1 -1
- package/dist/gateway/index.d.ts.map +1 -1
- package/dist/gateway/index.js +3 -3
- package/dist/gateway/index.js.map +1 -1
- package/dist/gateway/mobile-commands.d.ts.map +1 -1
- package/dist/gateway/mobile-commands.js +8 -8
- package/dist/gateway/mobile-commands.js.map +1 -1
- package/dist/gateway/presenter.d.ts.map +1 -1
- package/dist/gateway/presenter.js +3 -3
- package/dist/gateway/presenter.js.map +1 -1
- package/dist/gateway/router.d.ts.map +1 -1
- package/dist/gateway/router.js +5 -7
- package/dist/gateway/router.js.map +1 -1
- package/dist/gateway/setup.d.ts.map +1 -1
- package/dist/gateway/setup.js +6 -4
- package/dist/gateway/setup.js.map +1 -1
- package/dist/modes/interactive/components/settings-selector.d.ts +4 -0
- package/dist/modes/interactive/components/settings-selector.d.ts.map +1 -1
- package/dist/modes/interactive/components/settings-selector.js +20 -0
- package/dist/modes/interactive/components/settings-selector.js.map +1 -1
- package/dist/modes/interactive/interactive-mode.d.ts.map +1 -1
- package/dist/modes/interactive/interactive-mode.js +8 -0
- package/dist/modes/interactive/interactive-mode.js.map +1 -1
- package/dist/modes/print-mode.d.ts.map +1 -1
- package/dist/modes/print-mode.js +9 -1
- package/dist/modes/print-mode.js.map +1 -1
- package/dist/node-runtime/adapter-VFG7WNPX.js +17 -0
- package/dist/node-runtime/{agent-session-services-YXXFCP5J.js → agent-session-services-5YXS2PLF.js} +10 -8
- package/dist/node-runtime/{builtin-extensions-ZLAX7TUH.js → builtin-extensions-I2HIFEMY.js} +3 -2
- package/dist/node-runtime/{chunk-3XPLBUEM.js → chunk-2CFDTH5B.js} +7 -3
- package/dist/node-runtime/chunk-2IDTZAG5.js +1415 -0
- package/dist/node-runtime/{chunk-QTU6AKNJ.js → chunk-3MISGG4S.js} +11 -15
- package/dist/node-runtime/chunk-4SBQAI4T.js +2185 -0
- package/dist/node-runtime/{chunk-TORR5XBV.js → chunk-5B5QN3ZQ.js} +12 -12
- package/dist/node-runtime/chunk-5B6VKFEV.js +180 -0
- package/dist/node-runtime/{chunk-AD6VXAO7.js → chunk-5Q4A37RB.js} +15 -104
- package/dist/node-runtime/{chunk-4TLO4PO2.js → chunk-7IQGBKBJ.js} +7 -1401
- package/dist/node-runtime/{chunk-OCVIXVTZ.js → chunk-D5UCKPBQ.js} +12 -1
- package/dist/node-runtime/{chunk-7S3T6YEA.js → chunk-DVQSHCVP.js} +1 -1
- package/dist/node-runtime/{chunk-HFVZUPFJ.js → chunk-E75WFZ33.js} +2 -2
- package/dist/node-runtime/chunk-FY4A63RK.js +73 -0
- package/dist/node-runtime/{chunk-6XL67VVY.js → chunk-I74LN5JG.js} +15 -0
- package/dist/node-runtime/chunk-IKU6Y7DL.js +287 -0
- package/dist/node-runtime/{chunk-WF7PGWRZ.js → chunk-L24TPBS4.js} +5 -4
- package/dist/node-runtime/{chunk-VQFSUJJG.js → chunk-LJTIVM7I.js} +7 -7
- package/dist/node-runtime/{chunk-DSIKTUPW.js → chunk-LWJXTA6Z.js} +274 -2433
- package/dist/node-runtime/{chunk-E4D3IGP2.js → chunk-P7XBVXGT.js} +6 -7
- package/dist/node-runtime/{chunk-OVX6X377.js → chunk-PTFBPO3C.js} +1 -1
- package/dist/node-runtime/{chunk-RC7DMV45.js → chunk-RCR3XAQY.js} +34 -6
- package/dist/node-runtime/chunk-RGQAEOM5.js +81 -0
- package/dist/node-runtime/{chunk-YCUZEGJG.js → chunk-SN2KSMPQ.js} +6 -3
- package/dist/node-runtime/{chunk-NRKGMJYU.js → chunk-SPVDBUOS.js} +1 -1
- package/dist/node-runtime/{chunk-IJIFHRTB.js → chunk-T4DA4XE7.js} +19 -19
- package/dist/node-runtime/chunk-U5F4IC7R.js +53 -0
- package/dist/node-runtime/{chunk-KWIYUUZK.js → chunk-UVWCDQYF.js} +27 -18
- package/dist/node-runtime/{chunk-ZXSGTGJ6.js → chunk-WIF24I7G.js} +21 -5
- package/dist/node-runtime/{chunk-2HHLJWGD.js → chunk-X3PGBLQ7.js} +2 -2
- package/dist/node-runtime/chunk-XHIRAZ26.js +108 -0
- package/dist/node-runtime/{chunk-YDO2F2IA.js → chunk-YWOJIGFY.js} +1 -1
- package/dist/node-runtime/{command-6WDOYKS6.js → command-FSBU2EVZ.js} +3 -3
- package/dist/node-runtime/{commands-NZLSOHPE.js → commands-EMBQQVVT.js} +4 -2
- package/dist/node-runtime/{commands-QC6VUZJQ.js → commands-NAAL7KNT.js} +5 -4
- package/dist/node-runtime/{direct-tool-executor-77GW4F5P.js → direct-tool-executor-4D7UXYU4.js} +7 -4
- package/dist/node-runtime/{gateway-INAYBF4Q.js → gateway-DQM333YV.js} +18 -19
- package/dist/node-runtime/index.js +23 -19
- package/dist/node-runtime/{init-HS6NQXSK.js → init-P5AUV77I.js} +4 -2
- package/dist/node-runtime/{install-cli-KL4UDDAD.js → install-cli-NN4YAU44.js} +1 -1
- package/dist/node-runtime/{interactive-mode-Z5M7WYYZ.js → interactive-mode-YOHZVMKX.js} +11 -9
- package/dist/node-runtime/lunr-computer-use-Y23CCDIJ.js +158 -0
- package/dist/node-runtime/{lunr-cron-LWIZJM2N.js → lunr-cron-USHC4LXH.js} +6 -4
- package/dist/node-runtime/main.js +16 -14
- package/dist/node-runtime/{mcp-auth-flow-QIXHW6QN.js → mcp-auth-flow-QVLFVLEO.js} +2 -1
- package/dist/node-runtime/{mobile-commands-J7OJEKFO.js → mobile-commands-O5LCHL32.js} +4 -3
- package/dist/node-runtime/{package-manager-cli-VYZM5WHN.js → package-manager-cli-Q7BL6KZO.js} +7 -6
- package/dist/node-runtime/{pi-mcp-adapter-5UKBZP44.js → pi-mcp-adapter-3PHF4KZ4.js} +5 -5
- package/dist/node-runtime/{pi-subagents-DNY7EAIG.js → pi-subagents-7IBSSGEW.js} +24 -18
- package/dist/node-runtime/{print-mode-XSFM26EL.js → print-mode-XQ76CYMT.js} +2 -1
- package/dist/node-runtime/{proxy-modes-LGEUYD7R.js → proxy-modes-5FAPSZEZ.js} +7 -4
- package/dist/node-runtime/rpc-entry.js +16 -14
- package/dist/node-runtime/runtime-EFPPD2UW.js +21 -0
- package/dist/node-runtime/{runtime-bridges-VRDDR73P.js → runtime-bridges-7WVCLI76.js} +3 -2
- package/dist/node-runtime/{settings-manager-J4SPQPDJ.js → settings-manager-DJDM5AHM.js} +2 -1
- package/dist/node-runtime/{setup-CQ2NLVK5.js → setup-PFBWRNUI.js} +10 -9
- package/dist/node-runtime/{subagent-executor-KZZQGAKV.js → subagent-executor-LPZ4BHWE.js} +29 -11
- package/dist/node-runtime/{update-cli-GTFJC7XK.js → update-cli-HNMOTNGX.js} +2 -1
- package/dist/node-runtime/workflow-OHPBEFO4.js +637 -0
- package/dist/utils/image-resize-core.d.ts +2 -0
- package/dist/utils/image-resize-core.d.ts.map +1 -1
- package/dist/utils/image-resize-core.js +10 -0
- package/dist/utils/image-resize-core.js.map +1 -1
- package/docs/computer-use.md +362 -0
- package/docs/features.md +8 -6
- package/examples/extensions/custom-provider-anthropic/package-lock.json +2 -2
- package/examples/extensions/custom-provider-anthropic/package.json +1 -1
- package/examples/extensions/custom-provider-gitlab-duo/package.json +1 -1
- package/examples/extensions/gondolin/package-lock.json +2 -2
- package/examples/extensions/gondolin/package.json +1 -1
- package/examples/extensions/sandbox/package-lock.json +2 -2
- package/examples/extensions/sandbox/package.json +1 -1
- package/examples/extensions/with-deps/package-lock.json +2 -2
- package/examples/extensions/with-deps/package.json +1 -1
- package/install-lock/package-lock.json +3841 -0
- package/install-lock/package.json +18 -0
- package/native/computer-use/LICENSE.md +21 -0
- package/npm-shrinkwrap.json +3822 -0
- package/package.json +15 -9
- package/dist/node-runtime/{chunk-LQP3RB67.js → chunk-RAK2QA2B.js} +3 -3
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"discord.d.ts","sourceRoot":"","sources":["../../../src/gateway/adapters/discord.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiEG;AAEH,OAAO,EAMN,MAAM,EAIN,MAAM,YAAY,CAAC;AAEpB,OAAO,EAAE,KAAK,aAAa,EAAqB,MAAM,cAAc,CAAC;AAGrE,OAAO,KAAK,EACX,UAAU,EACV,aAAa,EAGb,YAAY,EACZ,eAAe,EACf,WAAW,EACX,UAAU,EACV,MAAM,aAAa,CAAC;AACrB,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AAyB/C,MAAM,WAAW,sBAAsB;IACtC,EAAE,EAAE,MAAM,CAAC;CACX;AAED,MAAM,WAAW,yBAAyB;IACzC,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,IAAI,CAAC,CAAC,OAAO,EAAE,MAAM,GAAG;QAAE,OAAO,EAAE,MAAM,CAAC;QAAC,UAAU,CAAC,EAAE,OAAO,EAAE,CAAA;KAAE,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;CACvF;AAED,MAAM,WAAW,kBAAkB;IAClC,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC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CAAC,IAAI,CAAC,CAa7D;IAEK,WAAW,CAAC,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,UAAU,EAAE,EAAE,GAAG,OAAO,CAAC,UAAU,CAAC,CAqBhH;IAEK,UAAU,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAsBjE;YAEa,QAAQ;IAStB,OAAO,CAAC,UAAU;CAMlB","sourcesContent":["/**\n * lunR: gateway Discord adapter (Phase 3).\n *\n * Built on discord.js v14 (gateway websocket; the library owns reconnects and\n * REST rate-limit queuing internally, so neither is handled here). No voice,\n * no native slash-command registration — commands arrive as plain message\n * text, same as Telegram.\n *\n * Intents: Guilds, GuildMessages, DirectMessages, MessageContent. GuildMembers\n * is deliberately NOT requested: it is a privileged intent, and asking for it\n * without the dev-portal opt-in makes Discord refuse the identify — the bot\n * connects and immediately goes offline. v1 has no role-based config\n * (SessionSource.roleAuthorized stays undefined), so there is nothing to gain.\n *\n * Inbound: messageToEvent() is the pure message → MessageEvent mapping\n * (dm/group/thread chat types, <@id>/<@!id> mention detection with the mention\n * stripped, thread-participation counts as a mention, own + all-bot + system +\n * attachment-only messages dropped, other-mention-without-us dropped outside\n * DMs — hermes DISCORD_IGNORE_NO_MENTION). A module-level seen-id Set\n * (cap 500, FIFO evict) drops gateway-resume replays. cfg.ignoredChannels is\n * enforced adapter-side (channel id or parent id; blacklist wins over\n * mentions). Consecutive texts from the same chat/thread/user are debounced\n * 600ms and merged (\"\\n\"-joined, latest messageId kept); texts starting with\n * \"/\" flush immediately so commands never merge.\n * Button interactions are routed via interactionCreate → buttonInteractionToEvent().\n *\n * chatId/threadId mapping (pairs with session-keys.ts buildSessionKey):\n * dm → chatId = channel.id → agent:main:discord:dm:<channelId>\n * thread → chatId = PARENT channel id, threadId = → agent:main:discord:thread:<parentId>:<threadId>\n * thread channel id (shared session, no userId)\n * guild text/announcement → chatId = channel.id → agent:main:discord:group:<channelId>[:<userId>]\n * (chatType \"group\"; userId appended when groupSessionsPerUser is on)\n *\n * autoThread (cfg.discord.autoThread, default true): a bot-mentioning message\n * in a guild TEXT channel gets a thread spun off it (startThread, 60min\n * archive); the event is re-targeted to the thread (chatType \"thread\", chatId\n * parent id, threadId thread id). On failure the event stays in-channel.\n *\n * Thread participation: an in-memory Set of thread ids the bot has sent to\n * (recorded on every successful send() into a thread and on autoThread\n * creation); messages in those threads count as mentionedBot so follow-ups\n * don't need a re-mention. NOT persisted in v1 (hermes persists it).\n *\n * Outbound: channel.send({ content, reply: { messageReference } }) with a\n * one-shot plain-send fallback when the reply reference fails. DiscordAPIError\n * 50035 (invalid form body, e.g. overlong content) is reported non-retryable;\n * 429s are queued internally by discord.js; everything else is retryable.\n * editMessage resolves the channel via a messageId → channel map recorded at\n * send() time (router passes only chatId, but thread messages live in their\n * own channel); Unknown Message (10008) is a terminal { success: false }.\n * Message splitting is NOT done here — the router owns that (text.ts).\n *\n * Buttons: ActionRowBuilder rows of ButtonBuilder components (max 5×5).\n * answerCallback acknowledges the interaction; a text argument produces an\n * ephemeral reply (used for unauthorized/expired taps), otherwise deferUpdate().\n *\n * Typing: sendTyping() is a self-contained refresher — Discord typing bubbles\n * last ~10s, so the action is re-sent on an unref'd 8s interval per target.\n * The interval stops when send() completes for that target or after 2 minutes\n * without a refresh (safety net). All errors swallowed.\n *\n * Testability: the discord.js Client goes through an injectable clientFactory\n * (tests inject a minimal fake), timers through an injectable Scheduler, and\n * messageToEvent() operates on the structural DiscordMessageLike — plain mock\n * objects, no real Client construction.\n */\n\nimport {\n\tActionRowBuilder,\n\tApplicationCommandOptionType,\n\tButtonBuilder,\n\tButtonStyle,\n\tChannelType,\n\tClient,\n\tEvents,\n\tGatewayIntentBits,\n\ttype Interaction,\n} from \"discord.js\";\nimport { isAuthorized, isGatewayOwner } from \"../authz.ts\";\nimport { type DiscordConfig, loadGatewayConfig } from \"../config.ts\";\nimport { downloadAttachment } from \"../download.ts\";\nimport { createPairingStore } from \"../pairing.ts\";\nimport type {\n\tButtonSpec,\n\tCallbackEvent,\n\tChatType,\n\tInboundAttachment,\n\tMessageEvent,\n\tPlatformAdapter,\n\tSendOptions,\n\tSendResult,\n} from \"../types.ts\";\nimport type { Scheduler } from \"./telegram.ts\";\n\nconst DEFAULT_DEBOUNCE_MS = 600;\nconst TYPING_INTERVAL_MS = 8000; // Discord typing lasts ~10s\nconst TYPING_SAFETY_MS = 120_000; // auto-stop after 2min without a refresh\nconst REPLY_TO_TEXT_MAX = 500;\nconst THREAD_NAME_MAX = 40;\nconst SEEN_CAP = 500;\nconst SENT_CHANNEL_CAP = 500;\nconst CALLBACK_INTERACTION_CAP = 500;\nconst CHANNEL_CACHE_CAP = 500;\nconst PARTICIPATING_THREAD_CAP = 500;\nconst ERR_INVALID_FORM_BODY = 50035; // e.g. content over 2000 chars — non-retryable\nconst ERR_UNKNOWN_MESSAGE = 10008; // deleted/unknown message — terminal\n\nconst THREAD_TYPES: readonly number[] = [\n\tChannelType.PublicThread,\n\tChannelType.PrivateThread,\n\tChannelType.AnnouncementThread,\n];\n\n// ---------------------------------------------------------------------------\n// Structural discord.js shapes (only the fields the adapter reads)\n// ---------------------------------------------------------------------------\n\nexport interface DiscordSentMessageLike {\n\tid: string;\n}\n\nexport interface DiscordFetchedMessageLike {\n\tcontent?: string;\n\tedit?(options: string | { content: string; components?: unknown[] }): Promise<unknown>;\n}\n\nexport interface DiscordChannelLike {\n\tid: string;\n\ttype: number;\n\tparentId?: string | null;\n\tsend?(payload: {\n\t\tcontent: string;\n\t\tfiles?: string[];\n\t\treply?: { messageReference: string };\n\t\tcomponents?: unknown[];\n\t}): Promise<DiscordSentMessageLike>;\n\tsendTyping?(): Promise<unknown>;\n\tmessages?: {\n\t\tcache?: { get(id: string): { content?: string } | undefined };\n\t\tfetch(id: string): Promise<DiscordFetchedMessageLike | null>;\n\t};\n}\n\nexport interface DiscordButtonInteractionLike {\n\tid: string;\n\tcustomId: string;\n\tuser: { id: string; username?: string };\n\tmessage: { id: string; channel: DiscordChannelLike };\n\tisButton(): boolean;\n\tdeferUpdate(): Promise<unknown>;\n\treply?(options: { content: string; ephemeral: boolean }): Promise<unknown>;\n\tupdate?(options: { content: string; components?: unknown[] }): Promise<unknown>;\n}\n\nexport interface DiscordAttachmentLike {\n\tid: string;\n\t/** Direct CDN URL for the attachment content. */\n\turl: string;\n\tcontentType?: string;\n\tname?: string;\n\tsize?: number;\n}\n\nexport interface DiscordMessageLike {\n\tid: string;\n\tcontent?: string;\n\tsystem?: boolean;\n\tauthor: { id: string; bot?: boolean; username?: string };\n\tchannel: DiscordChannelLike;\n\treference?: { messageId?: string | null } | null;\n\tmentions?: {\n\t\thas(user: { id: string }): boolean;\n\t\trepliedUser?: { id: string } | null;\n\t};\n\tstartThread?(opts: { name: string; autoArchiveDuration: number }): Promise<DiscordChannelLike>;\n\tattachments?: DiscordAttachmentLike[];\n}\n\n/** Minimal Client surface; the real discord.js Client satisfies this. */\nexport interface DiscordClientLike {\n\tapplication?: Pick<NonNullable<Client[\"application\"]>, \"commands\"> | null;\n\tuser: { id: string } | null;\n\tlogin(token: string): Promise<unknown>;\n\ton(event: string, listener: (arg: unknown) => void): unknown;\n\tonce(event: string, listener: () => void): unknown;\n\tdestroy(): void;\n\tchannels: { fetch(id: string): Promise<DiscordChannelLike | null> };\n}\n\nexport type DiscordClientFactory = () => DiscordClientLike;\n\n// ---------------------------------------------------------------------------\n// Pure mapping + small helpers\n// ---------------------------------------------------------------------------\n\nfunction errMessage(err: unknown): string {\n\treturn err instanceof Error ? err.message : String(err);\n}\n\n/** lunr: download an inbound Discord attachment from its CDN URL. */\n/** Discord REST error code (DiscordAPIError.code) when present. */\nfunction discordErrorCode(err: unknown): number | undefined {\n\tconst code = (err as { code?: unknown } | null)?.code;\n\treturn typeof code === \"number\" ? code : undefined;\n}\n\nexport interface MessageToEventOptions {\n\t/** Channel ids (or parent ids for threads) to drop outright — blacklist wins over mentions. */\n\tignoredChannels?: readonly string[];\n\t/** Threads the bot has spoken in; messages there count as mentionedBot. */\n\tparticipatingThreads?: ReadonlySet<string>;\n}\n\nfunction isImageContentType(contentType: string | undefined): boolean {\n\treturn typeof contentType === \"string\" && contentType.split(\";\")[0]?.trim().toLowerCase().startsWith(\"image/\");\n}\n\n/** lunr: collect inbound image attachments (contentType image/*) from a Discord message. */\nexport function collectDiscordImageAttachments(message: DiscordMessageLike): DiscordAttachmentLike[] {\n\treturn Array.from(message.attachments?.values() ?? []).filter((attachment) =>\n\t\tisImageContentType(attachment.contentType),\n\t);\n}\n\n/**\n * Map a Discord message to a MessageEvent; null = drop (own message, any bot,\n * system message, ignored channel, unsupported channel type, or a non-DM\n * message mentioning others but not us). Messages with no text but image\n * attachments are kept (v1: images inbound).\n */\nexport function messageToEvent(\n\tmessage: DiscordMessageLike,\n\tbotUser: { id: string },\n\toptions: MessageToEventOptions = {},\n): MessageEvent | null {\n\tconst author = message.author;\n\tif (!author) return null;\n\tif (author.id === botUser.id) return null; // own messages\n\tif (author.bot) return null; // all bots (same simplification as telegram)\n\tif (message.system) return null;\n\tconst content = (message.content ?? \"\").trim();\n\tconst imageAttachments = collectDiscordImageAttachments(message);\n\tif (content === \"\" && imageAttachments.length === 0 && !Array.from(message.attachments?.values() ?? []).length)\n\t\treturn null;\n\n\tconst channel = message.channel;\n\tconst ignored = options.ignoredChannels ?? [];\n\tif (ignored.includes(channel.id) || (channel.parentId != null && ignored.includes(channel.parentId))) {\n\t\treturn null; // blacklist wins over mentions\n\t}\n\n\t// chatId/threadId mapping — see the file header for the session-key pairing.\n\tlet chatType: ChatType;\n\tlet chatId: string;\n\tlet threadId: string | undefined;\n\tif (channel.type === ChannelType.DM) {\n\t\tchatType = \"dm\";\n\t\tchatId = channel.id;\n\t} else if (THREAD_TYPES.includes(channel.type)) {\n\t\tchatType = \"thread\";\n\t\tchatId = channel.parentId ?? channel.id;\n\t\tthreadId = channel.id;\n\t} else if (channel.type === ChannelType.GuildText || channel.type === ChannelType.GuildAnnouncement) {\n\t\tchatType = \"group\";\n\t\tchatId = channel.id;\n\t} else {\n\t\treturn null; // voice/category/forum containers etc. — unsupported in v1\n\t}\n\n\tconst explicitMention = message.mentions?.has(botUser) === true;\n\tconst participating = threadId !== undefined && options.participatingThreads?.has(threadId) === true;\n\tconst mentionedBot = explicitMention || participating;\n\n\t// Multi-agent filter (hermes DISCORD_IGNORE_NO_MENTION): outside DMs, a\n\t// message that mentions other users/bots but not us is not for us.\n\tif (!mentionedBot && chatType !== \"dm\") {\n\t\tconst mentionedIds = [...content.matchAll(/<@!?(\\d+)>/g)].map((m) => m[1]);\n\t\tif (mentionedIds.some((id) => id !== botUser.id)) return null;\n\t}\n\n\tlet text = content;\n\tif (explicitMention) {\n\t\ttext = text.replace(new RegExp(`<@!?${botUser.id}>`, \"g\"), \"\").trim();\n\t}\n\n\tconst refId = message.reference?.messageId;\n\tconst cachedReply = refId != null ? channel.messages?.cache?.get(refId)?.content : undefined;\n\n\treturn {\n\t\ttext,\n\t\tmessageId: message.id,\n\t\treplyToText: cachedReply !== undefined ? cachedReply.slice(0, REPLY_TO_TEXT_MAX) : undefined,\n\t\tsource: {\n\t\t\tplatform: \"discord\",\n\t\t\tchatId,\n\t\t\tchatType,\n\t\t\tuserId: author.id,\n\t\t\tuserName: author.username,\n\t\t\tthreadId,\n\t\t},\n\t\tmetadata: mentionedBot ? { mentionedBot: true } : undefined,\n\t};\n}\n\nfunction mapDiscordChannel(\n\tchannel: DiscordChannelLike,\n): { chatType: ChatType; chatId: string; threadId?: string } | null {\n\tif (channel.type === ChannelType.DM) {\n\t\treturn { chatType: \"dm\", chatId: channel.id };\n\t}\n\tif (THREAD_TYPES.includes(channel.type)) {\n\t\treturn { chatType: \"thread\", chatId: channel.parentId ?? channel.id, threadId: channel.id };\n\t}\n\tif (channel.type === ChannelType.GuildText || channel.type === ChannelType.GuildAnnouncement) {\n\t\treturn { chatType: \"group\", chatId: channel.id };\n\t}\n\treturn null;\n}\n\nexport function buttonInteractionToEvent(interaction: DiscordButtonInteractionLike): CallbackEvent | null {\n\tconst channel = interaction.message.channel;\n\tconst mapped = mapDiscordChannel(channel);\n\tif (!mapped) return null;\n\treturn {\n\t\tid: interaction.id,\n\t\tchatId: mapped.chatId,\n\t\tmessageId: interaction.message.id,\n\t\tuserId: interaction.user.id,\n\t\tuserName: interaction.user.username,\n\t\tdata: interaction.customId,\n\t\tthreadId: mapped.threadId,\n\t};\n}\n\nfunction buildDiscordComponents(buttons: ButtonSpec[][]): unknown[] {\n\tconst rows: ActionRowBuilder<ButtonBuilder>[] = [];\n\tfor (const row of buttons) {\n\t\tconst actionRow = new ActionRowBuilder<ButtonBuilder>();\n\t\tfor (const button of row) {\n\t\t\tconst style = button.data === \"\" ? ButtonStyle.Secondary : ButtonStyle.Secondary;\n\t\t\tconst builder = new ButtonBuilder().setCustomId(button.data).setLabel(button.label).setStyle(style);\n\t\t\tif (button.data === \"\") builder.setDisabled(true);\n\t\t\tactionRow.addComponents(builder);\n\t\t}\n\t\trows.push(actionRow);\n\t}\n\treturn rows;\n}\n\n// ---------------------------------------------------------------------------\n// Gateway-resume replay dedup (module-level: one process = one bot connection)\n// ---------------------------------------------------------------------------\n\nconst seenMessageIds = new Set<string>();\n\n/** Returns true when this id was already seen (drop the replay). */\nfunction markMessageSeen(id: string): boolean {\n\tif (seenMessageIds.has(id)) return true;\n\tseenMessageIds.add(id);\n\tif (seenMessageIds.size > SEEN_CAP) {\n\t\tconst oldest = seenMessageIds.values().next().value; // Sets iterate in insertion order\n\t\tif (oldest !== undefined) seenMessageIds.delete(oldest);\n\t}\n\treturn false;\n}\n\n/** Test hook: reset RESUME dedup state between tests. */\nexport function resetSeenMessageIds(): void {\n\tseenMessageIds.clear();\n}\n\n// ---------------------------------------------------------------------------\n// Adapter\n// ---------------------------------------------------------------------------\n\nconst defaultScheduler: Scheduler = {\n\tsetTimeout: (fn, ms) => {\n\t\tconst handle = setTimeout(fn, ms);\n\t\thandle.unref?.();\n\t\treturn handle;\n\t},\n\tclearTimeout: (handle) => clearTimeout(handle),\n\tsetInterval: (fn, ms) => {\n\t\tconst handle = setInterval(fn, ms);\n\t\thandle.unref?.();\n\t\treturn handle;\n\t},\n\tclearInterval: (handle) => clearInterval(handle),\n};\n\nfunction defaultClientFactory(): DiscordClientLike {\n\t// No GatewayIntentBits.GuildMembers — see the file header for the\n\t// privileged-intent trap (bot stays offline without the portal opt-in).\n\treturn new Client({\n\t\tintents: [\n\t\t\tGatewayIntentBits.Guilds,\n\t\t\tGatewayIntentBits.GuildMessages,\n\t\t\tGatewayIntentBits.DirectMessages,\n\t\t\tGatewayIntentBits.MessageContent,\n\t\t],\n\t}) as unknown as DiscordClientLike;\n}\n\nexport interface DiscordAdapterOptions {\n\t/** Injected Client factory (tests). When omitted, a real discord.js Client is created. */\n\tclientFactory?: DiscordClientFactory;\n\t/** lunr: injected image downloader for inbound attachments (tests). Defaults to fetch(url). */\n\tdownloadAttachment?: (url: string) => Promise<Uint8Array>;\n\t/** lunr: skip inbound media larger than this many bytes. Defaults to 10MB. */\n\tmaxMediaBytes?: number;\n\tscheduler?: Scheduler;\n\tdebounceMs?: number;\n\tnow?: () => number;\n}\n\ninterface PendingEntry {\n\tevent: MessageEvent;\n\ttimer: ReturnType<typeof setTimeout>;\n}\n\ninterface TypingEntry {\n\tinterval: ReturnType<typeof setInterval>;\n\tlastRefresh: number;\n}\n\nfunction typingKey(chatId: string, threadId?: string): string {\n\treturn `${chatId}:${threadId ?? \"\"}`;\n}\n\nexport class DiscordAdapter implements PlatformAdapter {\n\treadonly platform = \"discord\";\n\treadonly maxMessageLength = 2000;\n\n\tprivate readonly cfg: DiscordConfig;\n\tprivate readonly clientFactory: DiscordClientFactory;\n\tprivate readonly downloadAttachment: (url: string) => Promise<Uint8Array>;\n\tprivate readonly maxMediaBytes: number;\n\tprivate readonly scheduler: Scheduler;\n\tprivate readonly debounceMs: number;\n\tprivate readonly now: () => number;\n\tprivate client?: DiscordClientLike;\n\tprivate botUserId?: string;\n\tprivate handler?: (event: MessageEvent) => void;\n\tprivate commandSuggestions?: Parameters<NonNullable<PlatformAdapter[\"setCommandSuggestions\"]>>[0];\n\tprivate callbackHandler?: (event: CallbackEvent) => void;\n\tprivate readonly pending = new Map<string, PendingEntry>();\n\tprivate readonly typing = new Map<string, TypingEntry>();\n\tprivate readonly channelCache = new Map<string, DiscordChannelLike>();\n\t/** Threads the bot has spoken in (session memory only; hermes persists this). */\n\tprivate readonly participatingThreads = new Set<string>();\n\t/** messageId → channel it was sent to, so editMessage finds thread messages. */\n\tprivate readonly sentMessageChannels = new Map<string, string>();\n\t/** callbackId → interaction, so answerCallback can acknowledge the click. */\n\tprivate readonly callbackInteractions = new Map<string, DiscordButtonInteractionLike>();\n\n\tconstructor(cfg: DiscordConfig, options: DiscordAdapterOptions = {}) {\n\t\tthis.cfg = cfg;\n\t\tthis.scheduler = options.scheduler ?? defaultScheduler;\n\t\tthis.debounceMs = options.debounceMs ?? DEFAULT_DEBOUNCE_MS;\n\t\tthis.now = options.now ?? Date.now;\n\t\tif (options.clientFactory) {\n\t\t\tthis.clientFactory = options.clientFactory;\n\t\t} else {\n\t\t\tif (!cfg.token) {\n\t\t\t\tthrow new Error(\"DiscordAdapter: no bot token (set discord.token or LUNR_DISCORD_BOT_TOKEN)\");\n\t\t\t}\n\t\t\tthis.clientFactory = defaultClientFactory;\n\t\t}\n\t\tthis.downloadAttachment = options.downloadAttachment ?? downloadAttachment;\n\t\tthis.maxMediaBytes = options.maxMediaBytes ?? 10 * 1024 * 1024;\n\t}\n\n\tsetCommandSuggestions(\n\t\thandler: (source: MessageEvent[\"source\"], command: string) => Promise<readonly string[]>,\n\t): void {\n\t\tthis.commandSuggestions = handler;\n\t}\n\n\tonMessage(handler: (event: MessageEvent) => void): void {\n\t\tthis.handler = handler;\n\t}\n\n\tonCallback(handler: (event: CallbackEvent) => void): void {\n\t\tthis.callbackHandler = handler;\n\t}\n\n\tasync connect(): Promise<boolean> {\n\t\tif (!this.cfg.token) {\n\t\t\tthrow new Error(\"DiscordAdapter: no bot token (set discord.token or LUNR_DISCORD_BOT_TOKEN)\");\n\t\t}\n\t\tconst client = this.clientFactory();\n\t\tthis.client = client;\n\t\tclient.on(Events.MessageCreate, (message) => {\n\t\t\tvoid this.handleMessage(message as DiscordMessageLike).catch((err) => {\n\t\t\t\tconsole.error(`[gateway] discord message handling error: ${errMessage(err)}`);\n\t\t\t});\n\t\t});\n\t\tclient.on(Events.InteractionCreate, (interaction) => {\n\t\t\tconst event = interaction as Interaction;\n\t\t\tconst handling = event.isButton()\n\t\t\t\t? this.handleInteraction(interaction as DiscordButtonInteractionLike)\n\t\t\t\t: this.handleNativeCommand(event);\n\t\t\tvoid handling.catch((err) => {\n\t\t\t\tconsole.error(`[gateway] discord interaction handling error: ${errMessage(err)}`);\n\t\t\t});\n\t\t});\n\t\tconst ready = new Promise<void>((resolve) => client.once(Events.ClientReady, () => resolve()));\n\t\ttry {\n\t\t\tawait client.login(this.cfg.token);\n\t\t} catch (err) {\n\t\t\tconsole.error(`[gateway] discord login failed: ${errMessage(err)}`);\n\t\t\treturn false;\n\t\t}\n\t\tawait ready;\n\t\tthis.botUserId = client.user?.id;\n\t\tif (this.botUserId === undefined) {\n\t\t\tconsole.error(\"[gateway] discord: client ready without a user — cannot map messages\");\n\t\t\treturn false;\n\t\t}\n\t\treturn true;\n\t}\n\n\tasync disconnect(): Promise<void> {\n\t\tfor (const key of [...this.pending.keys()]) this.cancelPending(key);\n\t\tfor (const key of [...this.typing.keys()]) this.stopTyping(key);\n\t\tthis.channelCache.clear();\n\t\tthis.callbackInteractions.clear();\n\t\tthis.client?.destroy();\n\t\tthis.client = undefined;\n\t}\n\n\t// -----------------------------------------------------------------------\n\t// Inbound dispatch + debounce\n\t// -----------------------------------------------------------------------\n\n\tprivate async handleMessage(message: DiscordMessageLike): Promise<void> {\n\t\tif (!this.botUserId || !this.handler) return;\n\t\tif (markMessageSeen(message.id)) return; // gateway-resume replay\n\t\tconst event = messageToEvent(\n\t\t\tmessage,\n\t\t\t{ id: this.botUserId },\n\t\t\t{\n\t\t\t\tignoredChannels: this.cfg.ignoredChannels,\n\t\t\t\tparticipatingThreads: this.participatingThreads,\n\t\t\t},\n\t\t);\n\t\tif (!event) return;\n\t\tif (!isAuthorized(event.source, { ...loadGatewayConfig(), discord: this.cfg }, createPairingStore())) {\n\t\t\tthis.dispatchEvent(event);\n\t\t\treturn;\n\t\t}\n\t\tawait this.resolveReplyToText(message, event);\n\t\tawait this.maybeAutoThread(message, event);\n\t\tawait this.downloadAttachments(message, event);\n\t\tthis.dispatchEvent(event);\n\t}\n\n\t/** Download and encode attachments after authorization. */\n\tprivate async downloadAttachments(message: DiscordMessageLike, event: MessageEvent): Promise<void> {\n\t\tconst imageAttachments = Array.from(message.attachments?.values() ?? []);\n\t\tif (imageAttachments.length === 0) return;\n\t\tconst attachments: InboundAttachment[] = [];\n\t\tfor (const attachment of imageAttachments) {\n\t\t\tif (attachment.size !== undefined && attachment.size > this.maxMediaBytes) continue;\n\t\t\ttry {\n\t\t\t\tconst bytes = await this.downloadAttachment(attachment.url);\n\t\t\t\tif (bytes.byteLength > this.maxMediaBytes) continue;\n\t\t\t\tattachments.push({\n\t\t\t\t\tdata: Buffer.from(bytes).toString(\"base64\"),\n\t\t\t\t\tmimeType: attachment.contentType ?? \"application/octet-stream\",\n\t\t\t\t\t...(attachment.name ? { filename: attachment.name } : {}),\n\t\t\t\t});\n\t\t\t} catch {\n\t\t\t\t// Media download failed — drop this attachment, keep the text turn.\n\t\t\t}\n\t\t}\n\t\tif (attachments.length > 0) event.attachments = attachments;\n\t}\n\n\tasync registerCommands(commands: { name: string; description: string }[]): Promise<void> {\n\t\tif (!this.client?.application) return;\n\t\tawait this.client.application.commands.set(\n\t\t\tcommands.slice(0, 95).map((command) => ({\n\t\t\t\tname: command.name,\n\t\t\t\tdescription: command.description.slice(0, 100),\n\t\t\t\toptions: [\n\t\t\t\t\t{\n\t\t\t\t\t\tname: \"args\",\n\t\t\t\t\t\tdescription: \"Command arguments or search\",\n\t\t\t\t\t\ttype: ApplicationCommandOptionType.String,\n\t\t\t\t\t\trequired: false,\n\t\t\t\t\t\tautocomplete: [\"mode\", \"thinking\", \"project\", \"sessions\"].includes(command.name),\n\t\t\t\t\t},\n\t\t\t\t],\n\t\t\t})),\n\t\t);\n\t}\n\n\tprivate async handleNativeCommand(interaction: Interaction): Promise<boolean> {\n\t\tif (!interaction.isChatInputCommand?.() && !interaction.isAutocomplete?.()) return false;\n\t\tif (!interaction.isChatInputCommand() && !interaction.isAutocomplete()) return false;\n\t\tconst thread = interaction.channel?.isThread() ? interaction.channel : undefined;\n\t\tconst source = {\n\t\t\tplatform: \"discord\",\n\t\t\tchatId: thread?.parentId ?? interaction.channelId ?? interaction.user.id,\n\t\t\tthreadId: thread?.id,\n\t\t\tchatType: thread ? (\"thread\" as const) : interaction.inGuild() ? (\"channel\" as const) : (\"dm\" as const),\n\t\t\tuserId: interaction.user.id,\n\t\t\tuserName: interaction.user.username,\n\t\t};\n\t\tconst cfg = loadGatewayConfig();\n\t\tconst authorized = isAuthorized(source, cfg, createPairingStore());\n\t\tif (interaction.isAutocomplete()) {\n\t\t\tif (!authorized || !isGatewayOwner(source, cfg)) {\n\t\t\t\tawait interaction.respond([]);\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\tconst query = String(interaction.options.getFocused()).toLowerCase();\n\t\t\tlet values: string[] = [];\n\t\t\tif (interaction.commandName === \"mode\") values = [\"yolo\", \"auto\", \"read-only\"];\n\t\t\tif (this.commandSuggestions && [\"thinking\", \"model\"].includes(interaction.commandName)) {\n\t\t\t\tvalues = [...(await this.commandSuggestions(source, interaction.commandName))];\n\t\t\t}\n\t\t\tif (interaction.commandName === \"project\") values = cfg.projectRoots ?? [];\n\t\t\tif (interaction.commandName === \"sessions\") {\n\t\t\t\tconst { SessionManager } = await import(\"../../core/session-manager.ts\");\n\t\t\t\tvalues = (\n\t\t\t\t\tawait Promise.race([\n\t\t\t\t\t\tSessionManager.listAll(),\n\t\t\t\t\t\tnew Promise<[]>((resolve) => setTimeout(() => resolve([]), 1500)),\n\t\t\t\t\t])\n\t\t\t\t).map((s) => s.name || s.id);\n\t\t\t}\n\t\t\tawait interaction.respond(\n\t\t\t\tvalues\n\t\t\t\t\t.filter((s) => s.toLowerCase().includes(query))\n\t\t\t\t\t.slice(0, 25)\n\t\t\t\t\t.map((value) => ({ name: value.slice(0, 100), value: value.slice(0, 100) })),\n\t\t\t);\n\t\t\treturn true;\n\t\t}\n\t\tawait interaction.reply({\n\t\t\tcontent: authorized\n\t\t\t\t? \"Command received.\"\n\t\t\t\t: \"Pair your account before using this command. Send the bot a private message for a pairing code.\",\n\t\t\tephemeral: true,\n\t\t});\n\t\tif (authorized)\n\t\t\tthis.handler?.({\n\t\t\t\ttext: `/${interaction.commandName}${interaction.options.getString(\"args\") ? ` ${interaction.options.getString(\"args\")}` : \"\"}`,\n\t\t\t\tsource,\n\t\t\t\tmessageId: \"\",\n\t\t\t\tmetadata: { mentionedBot: true, nativeCommand: true },\n\t\t\t});\n\t\treturn true;\n\t}\n\n\tasync sendFile(chatId: string, file: string, opts?: SendOptions): Promise<SendResult> {\n\t\tconst channel = await this.fetchChannel(opts?.threadId ?? chatId);\n\t\tif (!channel?.send) return { success: false, error: \"Channel is unavailable.\" };\n\t\ttry {\n\t\t\tconst message = await channel.send({ content: \"\", files: [file] });\n\t\t\treturn { success: true, messageId: message.id };\n\t\t} catch (error) {\n\t\t\treturn { success: false, error: errMessage(error) };\n\t\t}\n\t}\n\n\tprivate async handleInteraction(interaction: DiscordButtonInteractionLike): Promise<void> {\n\t\tif (!this.callbackHandler) return;\n\t\tif (!interaction.isButton()) return;\n\t\tconst event = buttonInteractionToEvent(interaction);\n\t\tif (!event) return;\n\t\tthis.callbackInteractions.set(interaction.id, interaction);\n\t\tif (this.callbackInteractions.size > CALLBACK_INTERACTION_CAP) {\n\t\t\tconst oldest = this.callbackInteractions.keys().next().value;\n\t\t\tif (oldest !== undefined) this.callbackInteractions.delete(oldest);\n\t\t}\n\t\tthis.callbackHandler(event);\n\t}\n\n\t/** Best-effort: fill replyToText from the referenced message when it wasn't cached. */\n\tprivate async resolveReplyToText(message: DiscordMessageLike, event: MessageEvent): Promise<void> {\n\t\tif (event.replyToText !== undefined) return;\n\t\tconst refId = message.reference?.messageId;\n\t\tif (refId == null) return;\n\t\tconst messages = message.channel.messages;\n\t\tif (!messages) return;\n\t\ttry {\n\t\t\tconst referenced = await messages.fetch(refId);\n\t\t\tif (referenced?.content) event.replyToText = referenced.content.slice(0, REPLY_TO_TEXT_MAX);\n\t\t} catch {\n\t\t\t// unknown/deleted reference — no reply context\n\t\t}\n\t}\n\n\t/** autoThread: spin a thread off a bot-mentioning guild-text message and re-target the event. */\n\tprivate async maybeAutoThread(message: DiscordMessageLike, event: MessageEvent): Promise<void> {\n\t\tif (!this.cfg.autoThread) return;\n\t\tif (event.metadata?.mentionedBot !== true) return;\n\t\t// Only guild TEXT channels: DMs can't thread and messages already inside\n\t\t// a thread don't need another one.\n\t\tif (message.channel.type !== ChannelType.GuildText) return;\n\t\tif (typeof message.startThread !== \"function\") return;\n\t\ttry {\n\t\t\tconst thread = await message.startThread({\n\t\t\t\tname: (event.text || \"conversation\").slice(0, THREAD_NAME_MAX),\n\t\t\t\tautoArchiveDuration: 60,\n\t\t\t});\n\t\t\tthis.participatingThreads.add(thread.id);\n\t\t\tif (this.participatingThreads.size > PARTICIPATING_THREAD_CAP) {\n\t\t\t\tconst oldest = this.participatingThreads.values().next().value;\n\t\t\t\tif (oldest !== undefined) this.participatingThreads.delete(oldest);\n\t\t\t}\n\t\t\tevent.source = { ...event.source, chatType: \"thread\", chatId: message.channel.id, threadId: thread.id };\n\t\t} catch {\n\t\t\t// e.g. missing CREATE_THREADS permission — respond in-channel instead.\n\t\t}\n\t}\n\n\tprivate debounceKey(event: MessageEvent): string {\n\t\treturn `${event.source.chatId}:${event.source.threadId ?? \"\"}:${event.source.userId}`;\n\t}\n\n\tprivate dispatchEvent(event: MessageEvent): void {\n\t\tconst key = this.debounceKey(event);\n\t\tif (event.text.trimStart().startsWith(\"/\")) {\n\t\t\t// Commands must not merge: flush any pending text, emit immediately.\n\t\t\tthis.flushPending(key);\n\t\t\tthis.handler?.(event);\n\t\t\treturn;\n\t\t}\n\t\tconst existing = this.pending.get(key);\n\t\tif (existing) {\n\t\t\tthis.scheduler.clearTimeout(existing.timer);\n\t\t\t// Merge: earlier text first, latest messageId (and other fields) win.\n\t\t\tconst mergedAttachments = [...(existing.event.attachments ?? []), ...(event.attachments ?? [])];\n\t\t\texisting.event = {\n\t\t\t\t...event,\n\t\t\t\ttext: `${existing.event.text}\\n${event.text}`,\n\t\t\t\t...(mergedAttachments.length > 0 ? { attachments: mergedAttachments } : {}),\n\t\t\t};\n\t\t\texisting.timer = this.scheduler.setTimeout(() => this.flushPending(key), this.debounceMs);\n\t\t} else {\n\t\t\tconst timer = this.scheduler.setTimeout(() => this.flushPending(key), this.debounceMs);\n\t\t\tthis.pending.set(key, { event, timer });\n\t\t}\n\t}\n\n\tprivate flushPending(key: string): void {\n\t\tconst entry = this.pending.get(key);\n\t\tif (!entry) return;\n\t\tthis.scheduler.clearTimeout(entry.timer);\n\t\tthis.pending.delete(key);\n\t\tthis.handler?.(entry.event);\n\t}\n\n\tprivate cancelPending(key: string): void {\n\t\tconst entry = this.pending.get(key);\n\t\tif (!entry) return;\n\t\tthis.scheduler.clearTimeout(entry.timer);\n\t\tthis.pending.delete(key);\n\t}\n\n\t// -----------------------------------------------------------------------\n\t// Outbound\n\t// -----------------------------------------------------------------------\n\n\tprivate async fetchChannel(id: string): Promise<DiscordChannelLike | null> {\n\t\tif (this.channelCache.has(id)) return this.channelCache.get(id) ?? null;\n\t\tif (!this.client) return null;\n\t\ttry {\n\t\t\tconst channel = await this.client.channels.fetch(id);\n\t\t\tif (channel) this.channelCache.set(id, channel);\n\t\t\tif (this.channelCache.size > CHANNEL_CACHE_CAP) {\n\t\t\t\tconst oldest = this.channelCache.keys().next().value;\n\t\t\t\tif (oldest !== undefined) this.channelCache.delete(oldest);\n\t\t\t}\n\t\t\treturn channel;\n\t\t} catch {\n\t\t\treturn null;\n\t\t}\n\t}\n\n\tprivate recordSent(messageId: string, channel: DiscordChannelLike): void {\n\t\tif (THREAD_TYPES.includes(channel.type)) this.participatingThreads.add(channel.id);\n\t\tif (this.participatingThreads.size > PARTICIPATING_THREAD_CAP) {\n\t\t\tconst oldest = this.participatingThreads.values().next().value;\n\t\t\tif (oldest !== undefined) this.participatingThreads.delete(oldest);\n\t\t}\n\t\tthis.sentMessageChannels.set(messageId, channel.id);\n\t\tif (this.sentMessageChannels.size > SENT_CHANNEL_CAP) {\n\t\t\tconst oldest = this.sentMessageChannels.keys().next().value;\n\t\t\tif (oldest !== undefined) this.sentMessageChannels.delete(oldest);\n\t\t}\n\t}\n\n\tprivate async trySend(\n\t\tchannel: DiscordChannelLike,\n\t\tpayload: { content: string; reply?: { messageReference: string }; components?: unknown[] },\n\t): Promise<SendResult> {\n\t\tif (typeof channel.send !== \"function\") {\n\t\t\treturn { success: false, error: `channel ${channel.id} is not sendable` };\n\t\t}\n\t\ttry {\n\t\t\tconst sent = await channel.send(payload);\n\t\t\tthis.recordSent(sent.id, channel);\n\t\t\treturn { success: true, messageId: sent.id };\n\t\t} catch (err) {\n\t\t\t// 429s are queued internally by discord.js and don't surface here.\n\t\t\treturn { success: false, error: errMessage(err), retryable: discordErrorCode(err) !== ERR_INVALID_FORM_BODY };\n\t\t}\n\t}\n\n\tasync send(chatId: string, text: string, opts?: SendOptions): Promise<SendResult> {\n\t\t// A completed send ends the typing bubble — stop the refresher (see sendTyping).\n\t\tthis.stopTyping(typingKey(chatId, opts?.threadId));\n\t\tconst channel = await this.fetchChannel(opts?.threadId ?? chatId);\n\t\tif (!channel) return { success: false, error: `channel ${opts?.threadId ?? chatId} not found` };\n\t\tconst result = await this.trySend(channel, {\n\t\t\tcontent: text,\n\t\t\treply: opts?.replyTo !== undefined ? { messageReference: opts.replyTo } : undefined,\n\t\t});\n\t\tif (!result.success && opts?.replyTo !== undefined) {\n\t\t\t// The reply target may be deleted/unknown — fall back to a plain send.\n\t\t\treturn this.trySend(channel, { content: text });\n\t\t}\n\t\treturn result;\n\t}\n\n\tasync sendButtons(chatId: string, text: string, buttons: ButtonSpec[][], opts?: SendOptions): Promise<SendResult> {\n\t\tthis.stopTyping(typingKey(chatId, opts?.threadId));\n\t\tconst channel = await this.fetchChannel(opts?.threadId ?? chatId);\n\t\tif (!channel) return { success: false, error: `channel ${opts?.threadId ?? chatId} not found` };\n\t\tconst result = await this.trySend(channel, {\n\t\t\tcontent: text,\n\t\t\treply: opts?.replyTo !== undefined ? { messageReference: opts.replyTo } : undefined,\n\t\t\tcomponents: buildDiscordComponents(buttons),\n\t\t});\n\t\tif (!result.success && opts?.replyTo !== undefined) {\n\t\t\treturn this.trySend(channel, { content: text, components: buildDiscordComponents(buttons) });\n\t\t}\n\t\treturn result;\n\t}\n\n\tasync answerCallback(id: string, text?: string): Promise<void> {\n\t\tconst interaction = this.callbackInteractions.get(id);\n\t\tif (!interaction) return;\n\t\tthis.callbackInteractions.delete(id);\n\t\ttry {\n\t\t\tif (text !== undefined && interaction.reply) {\n\t\t\t\tawait interaction.reply({ content: text, ephemeral: true });\n\t\t\t} else {\n\t\t\t\tawait interaction.deferUpdate();\n\t\t\t}\n\t\t} catch {\n\t\t\t// best-effort acknowledgment\n\t\t}\n\t}\n\n\tasync editMessage(chatId: string, messageId: string, text: string, buttons?: ButtonSpec[][]): Promise<SendResult> {\n\t\t// The router passes only chatId; thread messages live in their own\n\t\t// channel, so prefer the channel recorded at send() time.\n\t\tconst channel = await this.fetchChannel(this.sentMessageChannels.get(messageId) ?? chatId);\n\t\tif (!channel?.messages) {\n\t\t\treturn { success: false, error: `channel ${this.sentMessageChannels.get(messageId) ?? chatId} not found` };\n\t\t}\n\t\ttry {\n\t\t\tconst message = await channel.messages.fetch(messageId);\n\t\t\tif (!message || typeof message.edit !== \"function\") {\n\t\t\t\treturn { success: false, error: `message ${messageId} not found` };\n\t\t\t}\n\t\t\tconst editOptions: { content: string; components?: unknown[] } = { content: text };\n\t\t\tif (buttons !== undefined) editOptions.components = buildDiscordComponents(buttons);\n\t\t\tawait message.edit(editOptions);\n\t\t\treturn { success: true, messageId };\n\t\t} catch (err) {\n\t\t\tconst code = discordErrorCode(err);\n\t\t\tif (code === ERR_UNKNOWN_MESSAGE) return { success: false, error: errMessage(err) }; // terminal\n\t\t\treturn { success: false, error: errMessage(err), retryable: true };\n\t\t}\n\t}\n\n\tasync sendTyping(chatId: string, threadId?: string): Promise<void> {\n\t\t// Self-contained typing refresher (see file header): re-send the action\n\t\t// every 8s until send() completes for this target or the 2min safety net\n\t\t// fires — all errors swallowed.\n\t\tconst key = typingKey(chatId, threadId);\n\t\tconst targetId = threadId ?? chatId;\n\t\tawait this.doTyping(targetId);\n\t\tconst existing = this.typing.get(key);\n\t\tif (existing) {\n\t\t\texisting.lastRefresh = this.now();\n\t\t\treturn;\n\t\t}\n\t\tconst interval = this.scheduler.setInterval(() => {\n\t\t\tconst entry = this.typing.get(key);\n\t\t\tif (!entry) return;\n\t\t\tif (this.now() - entry.lastRefresh > TYPING_SAFETY_MS) {\n\t\t\t\tthis.stopTyping(key);\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tvoid this.doTyping(targetId);\n\t\t}, TYPING_INTERVAL_MS);\n\t\tthis.typing.set(key, { interval, lastRefresh: this.now() });\n\t}\n\n\tprivate async doTyping(channelId: string): Promise<void> {\n\t\ttry {\n\t\t\tconst channel = await this.fetchChannel(channelId);\n\t\t\tawait channel?.sendTyping?.();\n\t\t} catch {\n\t\t\t// typing is cosmetic — never surface\n\t\t}\n\t}\n\n\tprivate stopTyping(key: string): void {\n\t\tconst entry = this.typing.get(key);\n\t\tif (!entry) return;\n\t\tthis.scheduler.clearInterval(entry.interval);\n\t\tthis.typing.delete(key);\n\t}\n}\n"]}
|
|
1
|
+
{"version":3,"file":"discord.d.ts","sourceRoot":"","sources":["../../../src/gateway/adapters/discord.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiEG;AAEH,OAAO,EAMN,MAAM,EAIN,MAAM,YAAY,CAAC;AAEpB,OAAO,EAAE,KAAK,aAAa,EAAqB,MAAM,cAAc,CAAC;AAGrE,OAAO,KAAK,EACX,UAAU,EACV,aAAa,EAGb,YAAY,EACZ,eAAe,EACf,WAAW,EACX,UAAU,EACV,MAAM,aAAa,CAAC;AACrB,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AAyB/C,MAAM,WAAW,sBAAsB;IACtC,EAAE,EAAE,MAAM,CAAC;CACX;AAED,MAAM,WAAW,yBAAyB;IACzC,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,IAAI,CAAC,CAAC,OAAO,EAAE,MAAM,GAAG;QAAE,OAAO,EAAE,MAAM,CAAC;QAAC,UAAU,CAAC,EAAE,OAAO,EAAE,CAAA;KAAE,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;CACvF;AAED,MAAM,WAAW,kBAAkB;IAClC,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACzB,IAAI,CAAC,CAAC,OAAO,EAAE;QACd,OAAO,EAAE,MAAM,CAAC;QAChB,KAAK,CAAC,EAAE,MAAM,EAAE,CAAC;QACjB,KAAK,CAAC,EAAE;YAAE,gBAAgB,EAAE,MAAM,CAAA;SAAE,CAAC;QACrC,UAAU,CAAC,EAAE,OAAO,EAAE,CAAC;KACvB,GAAG,OAAO,CAAC,sBAAsB,CAAC,CAAC;IACpC,UAAU,CAAC,IAAI,OAAO,CAAC,OAAO,CAAC,CAAC;IAChC,QAAQ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C,UAAU;CAMlB","sourcesContent":["/**\n * lunR: gateway Discord adapter (Phase 3).\n *\n * Built on discord.js v14 (gateway websocket; the library owns reconnects and\n * REST rate-limit queuing internally, so neither is handled here). No voice,\n * no native slash-command registration — commands arrive as plain message\n * text, same as Telegram.\n *\n * Intents: Guilds, GuildMessages, DirectMessages, MessageContent. GuildMembers\n * is deliberately NOT requested: it is a privileged intent, and asking for it\n * without the dev-portal opt-in makes Discord refuse the identify — the bot\n * connects and immediately goes offline. v1 has no role-based config\n * (SessionSource.roleAuthorized stays undefined), so there is nothing to gain.\n *\n * Inbound: messageToEvent() is the pure message → MessageEvent mapping\n * (dm/group/thread chat types, <@id>/<@!id> mention detection with the mention\n * stripped, thread-participation counts as a mention, own + all-bot + system +\n * attachment-only messages dropped, other-mention-without-us dropped outside\n * DMs — hermes DISCORD_IGNORE_NO_MENTION). A module-level seen-id Set\n * (cap 500, FIFO evict) drops gateway-resume replays. cfg.ignoredChannels is\n * enforced adapter-side (channel id or parent id; blacklist wins over\n * mentions). Consecutive texts from the same chat/thread/user are debounced\n * 600ms and merged (\"\\n\"-joined, latest messageId kept); texts starting with\n * \"/\" flush immediately so commands never merge.\n * Button interactions are routed via interactionCreate → buttonInteractionToEvent().\n *\n * chatId/threadId mapping (pairs with session-keys.ts buildSessionKey):\n * dm → chatId = channel.id → agent:main:discord:dm:<channelId>\n * thread → chatId = PARENT channel id, threadId = → agent:main:discord:thread:<parentId>:<threadId>\n * thread channel id (shared session, no userId)\n * guild text/announcement → chatId = channel.id → agent:main:discord:group:<channelId>[:<userId>]\n * (chatType \"group\"; userId appended when groupSessionsPerUser is on)\n *\n * autoThread (cfg.discord.autoThread, default true): a bot-mentioning message\n * in a guild TEXT channel gets a thread spun off it (startThread, 60min\n * archive); the event is re-targeted to the thread (chatType \"thread\", chatId\n * parent id, threadId thread id). On failure the event stays in-channel.\n *\n * Thread participation: an in-memory Set of thread ids the bot has sent to\n * (recorded on every successful send() into a thread and on autoThread\n * creation); messages in those threads count as mentionedBot so follow-ups\n * don't need a re-mention. NOT persisted in v1 (hermes persists it).\n *\n * Outbound: channel.send({ content, reply: { messageReference } }) with a\n * one-shot plain-send fallback when the reply reference fails. DiscordAPIError\n * 50035 (invalid form body, e.g. overlong content) is reported non-retryable;\n * 429s are queued internally by discord.js; everything else is retryable.\n * editMessage resolves the channel via a messageId → channel map recorded at\n * send() time (router passes only chatId, but thread messages live in their\n * own channel); Unknown Message (10008) is a terminal { success: false }.\n * Message splitting is NOT done here — the router owns that (text.ts).\n *\n * Buttons: ActionRowBuilder rows of ButtonBuilder components (max 5×5).\n * answerCallback acknowledges the interaction; a text argument produces an\n * ephemeral reply (used for unauthorized/expired taps), otherwise deferUpdate().\n *\n * Typing: sendTyping() is a self-contained refresher — Discord typing bubbles\n * last ~10s, so the action is re-sent on an unref'd 8s interval per target.\n * The interval stops when send() completes for that target or after 2 minutes\n * without a refresh (safety net). All errors swallowed.\n *\n * Testability: the discord.js Client goes through an injectable clientFactory\n * (tests inject a minimal fake), timers through an injectable Scheduler, and\n * messageToEvent() operates on the structural DiscordMessageLike — plain mock\n * objects, no real Client construction.\n */\n\nimport {\n\tActionRowBuilder,\n\tApplicationCommandOptionType,\n\tButtonBuilder,\n\tButtonStyle,\n\tChannelType,\n\tClient,\n\tEvents,\n\tGatewayIntentBits,\n\ttype Interaction,\n} from \"discord.js\";\nimport { isAuthorized } from \"../authz.ts\";\nimport { type DiscordConfig, loadGatewayConfig } from \"../config.ts\";\nimport { downloadAttachment } from \"../download.ts\";\nimport { createPairingStore } from \"../pairing.ts\";\nimport type {\n\tButtonSpec,\n\tCallbackEvent,\n\tChatType,\n\tInboundAttachment,\n\tMessageEvent,\n\tPlatformAdapter,\n\tSendOptions,\n\tSendResult,\n} from \"../types.ts\";\nimport type { Scheduler } from \"./telegram.ts\";\n\nconst DEFAULT_DEBOUNCE_MS = 600;\nconst TYPING_INTERVAL_MS = 8000; // Discord typing lasts ~10s\nconst TYPING_SAFETY_MS = 120_000; // auto-stop after 2min without a refresh\nconst REPLY_TO_TEXT_MAX = 500;\nconst THREAD_NAME_MAX = 40;\nconst SEEN_CAP = 500;\nconst SENT_CHANNEL_CAP = 500;\nconst CALLBACK_INTERACTION_CAP = 500;\nconst CHANNEL_CACHE_CAP = 500;\nconst PARTICIPATING_THREAD_CAP = 500;\nconst ERR_INVALID_FORM_BODY = 50035; // e.g. content over 2000 chars — non-retryable\nconst ERR_UNKNOWN_MESSAGE = 10008; // deleted/unknown message — terminal\n\nconst THREAD_TYPES: readonly number[] = [\n\tChannelType.PublicThread,\n\tChannelType.PrivateThread,\n\tChannelType.AnnouncementThread,\n];\n\n// ---------------------------------------------------------------------------\n// Structural discord.js shapes (only the fields the adapter reads)\n// ---------------------------------------------------------------------------\n\nexport interface DiscordSentMessageLike {\n\tid: string;\n}\n\nexport interface DiscordFetchedMessageLike {\n\tcontent?: string;\n\tedit?(options: string | { content: string; components?: unknown[] }): Promise<unknown>;\n}\n\nexport interface DiscordChannelLike {\n\tid: string;\n\ttype: number;\n\tparentId?: string | null;\n\tsend?(payload: {\n\t\tcontent: string;\n\t\tfiles?: string[];\n\t\treply?: { messageReference: string };\n\t\tcomponents?: unknown[];\n\t}): Promise<DiscordSentMessageLike>;\n\tsendTyping?(): Promise<unknown>;\n\tmessages?: {\n\t\tcache?: { get(id: string): { content?: string } | undefined };\n\t\tfetch(id: string): Promise<DiscordFetchedMessageLike | null>;\n\t};\n}\n\nexport interface DiscordButtonInteractionLike {\n\tid: string;\n\tcustomId: string;\n\tuser: { id: string; username?: string };\n\tmessage: { id: string; channel: DiscordChannelLike };\n\tisButton(): boolean;\n\tdeferUpdate(): Promise<unknown>;\n\treply?(options: { content: string; ephemeral: boolean }): Promise<unknown>;\n\tupdate?(options: { content: string; components?: unknown[] }): Promise<unknown>;\n}\n\nexport interface DiscordAttachmentLike {\n\tid: string;\n\t/** Direct CDN URL for the attachment content. */\n\turl: string;\n\tcontentType?: string;\n\tname?: string;\n\tsize?: number;\n}\n\nexport interface DiscordMessageLike {\n\tid: string;\n\tcontent?: string;\n\tsystem?: boolean;\n\tauthor: { id: string; bot?: boolean; username?: string };\n\tchannel: DiscordChannelLike;\n\treference?: { messageId?: string | null } | null;\n\tmentions?: {\n\t\thas(user: { id: string }): boolean;\n\t\trepliedUser?: { id: string } | null;\n\t};\n\tstartThread?(opts: { name: string; autoArchiveDuration: number }): Promise<DiscordChannelLike>;\n\tattachments?: DiscordAttachmentLike[];\n}\n\n/** Minimal Client surface; the real discord.js Client satisfies this. */\nexport interface DiscordClientLike {\n\tapplication?: Pick<NonNullable<Client[\"application\"]>, \"commands\"> | null;\n\tuser: { id: string } | null;\n\tlogin(token: string): Promise<unknown>;\n\ton(event: string, listener: (arg: unknown) => void): unknown;\n\tonce(event: string, listener: () => void): unknown;\n\tdestroy(): void;\n\tchannels: { fetch(id: string): Promise<DiscordChannelLike | null> };\n}\n\nexport type DiscordClientFactory = () => DiscordClientLike;\n\n// ---------------------------------------------------------------------------\n// Pure mapping + small helpers\n// ---------------------------------------------------------------------------\n\nfunction errMessage(err: unknown): string {\n\treturn err instanceof Error ? err.message : String(err);\n}\n\n/** lunr: download an inbound Discord attachment from its CDN URL. */\n/** Discord REST error code (DiscordAPIError.code) when present. */\nfunction discordErrorCode(err: unknown): number | undefined {\n\tconst code = (err as { code?: unknown } | null)?.code;\n\treturn typeof code === \"number\" ? code : undefined;\n}\n\nexport interface MessageToEventOptions {\n\t/** Channel ids (or parent ids for threads) to drop outright — blacklist wins over mentions. */\n\tignoredChannels?: readonly string[];\n\t/** Threads the bot has spoken in; messages there count as mentionedBot. */\n\tparticipatingThreads?: ReadonlySet<string>;\n}\n\nfunction isImageContentType(contentType: string | undefined): boolean {\n\treturn typeof contentType === \"string\" && contentType.split(\";\")[0]?.trim().toLowerCase().startsWith(\"image/\");\n}\n\n/** lunr: collect inbound image attachments (contentType image/*) from a Discord message. */\nexport function collectDiscordImageAttachments(message: DiscordMessageLike): DiscordAttachmentLike[] {\n\treturn Array.from(message.attachments?.values() ?? []).filter((attachment) =>\n\t\tisImageContentType(attachment.contentType),\n\t);\n}\n\n/**\n * Map a Discord message to a MessageEvent; null = drop (own message, any bot,\n * system message, ignored channel, unsupported channel type, or a non-DM\n * message mentioning others but not us). Messages with no text but image\n * attachments are kept (v1: images inbound).\n */\nexport function messageToEvent(\n\tmessage: DiscordMessageLike,\n\tbotUser: { id: string },\n\toptions: MessageToEventOptions = {},\n): MessageEvent | null {\n\tconst author = message.author;\n\tif (!author) return null;\n\tif (author.id === botUser.id) return null; // own messages\n\tif (author.bot) return null; // all bots (same simplification as telegram)\n\tif (message.system) return null;\n\tconst content = (message.content ?? \"\").trim();\n\tconst imageAttachments = collectDiscordImageAttachments(message);\n\tif (content === \"\" && imageAttachments.length === 0 && !Array.from(message.attachments?.values() ?? []).length)\n\t\treturn null;\n\n\tconst channel = message.channel;\n\tconst ignored = options.ignoredChannels ?? [];\n\tif (ignored.includes(channel.id) || (channel.parentId != null && ignored.includes(channel.parentId))) {\n\t\treturn null; // blacklist wins over mentions\n\t}\n\n\t// chatId/threadId mapping — see the file header for the session-key pairing.\n\tlet chatType: ChatType;\n\tlet chatId: string;\n\tlet threadId: string | undefined;\n\tif (channel.type === ChannelType.DM) {\n\t\tchatType = \"dm\";\n\t\tchatId = channel.id;\n\t} else if (THREAD_TYPES.includes(channel.type)) {\n\t\tchatType = \"thread\";\n\t\tchatId = channel.parentId ?? channel.id;\n\t\tthreadId = channel.id;\n\t} else if (channel.type === ChannelType.GuildText || channel.type === ChannelType.GuildAnnouncement) {\n\t\tchatType = \"group\";\n\t\tchatId = channel.id;\n\t} else {\n\t\treturn null; // voice/category/forum containers etc. — unsupported in v1\n\t}\n\n\tconst explicitMention = message.mentions?.has(botUser) === true;\n\tconst participating = threadId !== undefined && options.participatingThreads?.has(threadId) === true;\n\tconst mentionedBot = explicitMention || participating;\n\n\t// Multi-agent filter (hermes DISCORD_IGNORE_NO_MENTION): outside DMs, a\n\t// message that mentions other users/bots but not us is not for us.\n\tif (!mentionedBot && chatType !== \"dm\") {\n\t\tconst mentionedIds = [...content.matchAll(/<@!?(\\d+)>/g)].map((m) => m[1]);\n\t\tif (mentionedIds.some((id) => id !== botUser.id)) return null;\n\t}\n\n\tlet text = content;\n\tif (explicitMention) {\n\t\ttext = text.replace(new RegExp(`<@!?${botUser.id}>`, \"g\"), \"\").trim();\n\t}\n\n\tconst refId = message.reference?.messageId;\n\tconst cachedReply = refId != null ? channel.messages?.cache?.get(refId)?.content : undefined;\n\n\treturn {\n\t\ttext,\n\t\tmessageId: message.id,\n\t\treplyToText: cachedReply !== undefined ? cachedReply.slice(0, REPLY_TO_TEXT_MAX) : undefined,\n\t\tsource: {\n\t\t\tplatform: \"discord\",\n\t\t\tchatId,\n\t\t\tchatType,\n\t\t\tuserId: author.id,\n\t\t\tuserName: author.username,\n\t\t\tthreadId,\n\t\t},\n\t\tmetadata: mentionedBot ? { mentionedBot: true } : undefined,\n\t};\n}\n\nfunction mapDiscordChannel(\n\tchannel: DiscordChannelLike,\n): { chatType: ChatType; chatId: string; threadId?: string } | null {\n\tif (channel.type === ChannelType.DM) {\n\t\treturn { chatType: \"dm\", chatId: channel.id };\n\t}\n\tif (THREAD_TYPES.includes(channel.type)) {\n\t\treturn { chatType: \"thread\", chatId: channel.parentId ?? channel.id, threadId: channel.id };\n\t}\n\tif (channel.type === ChannelType.GuildText || channel.type === ChannelType.GuildAnnouncement) {\n\t\treturn { chatType: \"group\", chatId: channel.id };\n\t}\n\treturn null;\n}\n\nexport function buttonInteractionToEvent(interaction: DiscordButtonInteractionLike): CallbackEvent | null {\n\tconst channel = interaction.message.channel;\n\tconst mapped = mapDiscordChannel(channel);\n\tif (!mapped) return null;\n\treturn {\n\t\tid: interaction.id,\n\t\tchatId: mapped.chatId,\n\t\tmessageId: interaction.message.id,\n\t\tuserId: interaction.user.id,\n\t\tuserName: interaction.user.username,\n\t\tdata: interaction.customId,\n\t\tthreadId: mapped.threadId,\n\t};\n}\n\nfunction buildDiscordComponents(buttons: ButtonSpec[][]): unknown[] {\n\tconst rows: ActionRowBuilder<ButtonBuilder>[] = [];\n\tfor (const row of buttons) {\n\t\tconst actionRow = new ActionRowBuilder<ButtonBuilder>();\n\t\tfor (const button of row) {\n\t\t\tconst style = button.data === \"\" ? ButtonStyle.Secondary : ButtonStyle.Secondary;\n\t\t\tconst builder = new ButtonBuilder().setCustomId(button.data).setLabel(button.label).setStyle(style);\n\t\t\tif (button.data === \"\") builder.setDisabled(true);\n\t\t\tactionRow.addComponents(builder);\n\t\t}\n\t\trows.push(actionRow);\n\t}\n\treturn rows;\n}\n\n// ---------------------------------------------------------------------------\n// Gateway-resume replay dedup (module-level: one process = one bot connection)\n// ---------------------------------------------------------------------------\n\nconst seenMessageIds = new Set<string>();\n\n/** Returns true when this id was already seen (drop the replay). */\nfunction markMessageSeen(id: string): boolean {\n\tif (seenMessageIds.has(id)) return true;\n\tseenMessageIds.add(id);\n\tif (seenMessageIds.size > SEEN_CAP) {\n\t\tconst oldest = seenMessageIds.values().next().value; // Sets iterate in insertion order\n\t\tif (oldest !== undefined) seenMessageIds.delete(oldest);\n\t}\n\treturn false;\n}\n\n/** Test hook: reset RESUME dedup state between tests. */\nexport function resetSeenMessageIds(): void {\n\tseenMessageIds.clear();\n}\n\n// ---------------------------------------------------------------------------\n// Adapter\n// ---------------------------------------------------------------------------\n\nconst defaultScheduler: Scheduler = {\n\tsetTimeout: (fn, ms) => {\n\t\tconst handle = setTimeout(fn, ms);\n\t\thandle.unref?.();\n\t\treturn handle;\n\t},\n\tclearTimeout: (handle) => clearTimeout(handle),\n\tsetInterval: (fn, ms) => {\n\t\tconst handle = setInterval(fn, ms);\n\t\thandle.unref?.();\n\t\treturn handle;\n\t},\n\tclearInterval: (handle) => clearInterval(handle),\n};\n\nfunction defaultClientFactory(): DiscordClientLike {\n\t// No GatewayIntentBits.GuildMembers — see the file header for the\n\t// privileged-intent trap (bot stays offline without the portal opt-in).\n\treturn new Client({\n\t\tintents: [\n\t\t\tGatewayIntentBits.Guilds,\n\t\t\tGatewayIntentBits.GuildMessages,\n\t\t\tGatewayIntentBits.DirectMessages,\n\t\t\tGatewayIntentBits.MessageContent,\n\t\t],\n\t}) as unknown as DiscordClientLike;\n}\n\nexport interface DiscordAdapterOptions {\n\t/** Injected Client factory (tests). When omitted, a real discord.js Client is created. */\n\tclientFactory?: DiscordClientFactory;\n\t/** lunr: injected image downloader for inbound attachments (tests). Defaults to fetch(url). */\n\tdownloadAttachment?: (url: string) => Promise<Uint8Array>;\n\t/** lunr: skip inbound media larger than this many bytes. Defaults to 10MB. */\n\tmaxMediaBytes?: number;\n\tscheduler?: Scheduler;\n\tdebounceMs?: number;\n\tnow?: () => number;\n}\n\ninterface PendingEntry {\n\tevent: MessageEvent;\n\ttimer: ReturnType<typeof setTimeout>;\n}\n\ninterface TypingEntry {\n\tinterval: ReturnType<typeof setInterval>;\n\tlastRefresh: number;\n}\n\nfunction typingKey(chatId: string, threadId?: string): string {\n\treturn `${chatId}:${threadId ?? \"\"}`;\n}\n\nexport class DiscordAdapter implements PlatformAdapter {\n\treadonly platform = \"discord\";\n\treadonly maxMessageLength = 2000;\n\n\tprivate readonly cfg: DiscordConfig;\n\tprivate readonly clientFactory: DiscordClientFactory;\n\tprivate readonly downloadAttachment: (url: string) => Promise<Uint8Array>;\n\tprivate readonly maxMediaBytes: number;\n\tprivate readonly scheduler: Scheduler;\n\tprivate readonly debounceMs: number;\n\tprivate readonly now: () => number;\n\tprivate client?: DiscordClientLike;\n\tprivate botUserId?: string;\n\tprivate handler?: (event: MessageEvent) => void;\n\tprivate commandSuggestions?: Parameters<NonNullable<PlatformAdapter[\"setCommandSuggestions\"]>>[0];\n\tprivate callbackHandler?: (event: CallbackEvent) => void;\n\tprivate readonly pending = new Map<string, PendingEntry>();\n\tprivate readonly typing = new Map<string, TypingEntry>();\n\tprivate readonly channelCache = new Map<string, DiscordChannelLike>();\n\t/** Threads the bot has spoken in (session memory only; hermes persists this). */\n\tprivate readonly participatingThreads = new Set<string>();\n\t/** messageId → channel it was sent to, so editMessage finds thread messages. */\n\tprivate readonly sentMessageChannels = new Map<string, string>();\n\t/** callbackId → interaction, so answerCallback can acknowledge the click. */\n\tprivate readonly callbackInteractions = new Map<string, DiscordButtonInteractionLike>();\n\n\tconstructor(cfg: DiscordConfig, options: DiscordAdapterOptions = {}) {\n\t\tthis.cfg = cfg;\n\t\tthis.scheduler = options.scheduler ?? defaultScheduler;\n\t\tthis.debounceMs = options.debounceMs ?? DEFAULT_DEBOUNCE_MS;\n\t\tthis.now = options.now ?? Date.now;\n\t\tif (options.clientFactory) {\n\t\t\tthis.clientFactory = options.clientFactory;\n\t\t} else {\n\t\t\tif (!cfg.token) {\n\t\t\t\tthrow new Error(\"DiscordAdapter: no bot token (set discord.token or LUNR_DISCORD_BOT_TOKEN)\");\n\t\t\t}\n\t\t\tthis.clientFactory = defaultClientFactory;\n\t\t}\n\t\tthis.downloadAttachment = options.downloadAttachment ?? downloadAttachment;\n\t\tthis.maxMediaBytes = options.maxMediaBytes ?? 10 * 1024 * 1024;\n\t}\n\n\tsetCommandSuggestions(\n\t\thandler: (source: MessageEvent[\"source\"], command: string) => Promise<readonly string[]>,\n\t): void {\n\t\tthis.commandSuggestions = handler;\n\t}\n\n\tonMessage(handler: (event: MessageEvent) => void): void {\n\t\tthis.handler = handler;\n\t}\n\n\tonCallback(handler: (event: CallbackEvent) => void): void {\n\t\tthis.callbackHandler = handler;\n\t}\n\n\tasync connect(): Promise<boolean> {\n\t\tif (!this.cfg.token) {\n\t\t\tthrow new Error(\"DiscordAdapter: no bot token (set discord.token or LUNR_DISCORD_BOT_TOKEN)\");\n\t\t}\n\t\tconst client = this.clientFactory();\n\t\tthis.client = client;\n\t\tclient.on(Events.MessageCreate, (message) => {\n\t\t\tvoid this.handleMessage(message as DiscordMessageLike).catch((err) => {\n\t\t\t\tconsole.error(`[gateway] discord message handling error: ${errMessage(err)}`);\n\t\t\t});\n\t\t});\n\t\tclient.on(Events.InteractionCreate, (interaction) => {\n\t\t\tconst event = interaction as Interaction;\n\t\t\tconst handling = event.isButton()\n\t\t\t\t? this.handleInteraction(interaction as DiscordButtonInteractionLike)\n\t\t\t\t: this.handleNativeCommand(event);\n\t\t\tvoid handling.catch((err) => {\n\t\t\t\tconsole.error(`[gateway] discord interaction handling error: ${errMessage(err)}`);\n\t\t\t});\n\t\t});\n\t\tconst ready = new Promise<void>((resolve) => client.once(Events.ClientReady, () => resolve()));\n\t\ttry {\n\t\t\tawait client.login(this.cfg.token);\n\t\t} catch (err) {\n\t\t\tconsole.error(`[gateway] discord login failed: ${errMessage(err)}`);\n\t\t\treturn false;\n\t\t}\n\t\tawait ready;\n\t\tthis.botUserId = client.user?.id;\n\t\tif (this.botUserId === undefined) {\n\t\t\tconsole.error(\"[gateway] discord: client ready without a user — cannot map messages\");\n\t\t\treturn false;\n\t\t}\n\t\treturn true;\n\t}\n\n\tasync disconnect(): Promise<void> {\n\t\tfor (const key of [...this.pending.keys()]) this.cancelPending(key);\n\t\tfor (const key of [...this.typing.keys()]) this.stopTyping(key);\n\t\tthis.channelCache.clear();\n\t\tthis.callbackInteractions.clear();\n\t\tthis.client?.destroy();\n\t\tthis.client = undefined;\n\t}\n\n\t// -----------------------------------------------------------------------\n\t// Inbound dispatch + debounce\n\t// -----------------------------------------------------------------------\n\n\tprivate async handleMessage(message: DiscordMessageLike): Promise<void> {\n\t\tif (!this.botUserId || !this.handler) return;\n\t\tif (markMessageSeen(message.id)) return; // gateway-resume replay\n\t\tconst event = messageToEvent(\n\t\t\tmessage,\n\t\t\t{ id: this.botUserId },\n\t\t\t{\n\t\t\t\tignoredChannels: this.cfg.ignoredChannels,\n\t\t\t\tparticipatingThreads: this.participatingThreads,\n\t\t\t},\n\t\t);\n\t\tif (!event) return;\n\t\tif (!isAuthorized(event.source, { ...loadGatewayConfig(), discord: this.cfg }, createPairingStore())) {\n\t\t\tthis.dispatchEvent(event);\n\t\t\treturn;\n\t\t}\n\t\tawait this.resolveReplyToText(message, event);\n\t\tawait this.maybeAutoThread(message, event);\n\t\tawait this.downloadAttachments(message, event);\n\t\tthis.dispatchEvent(event);\n\t}\n\n\t/** Download and encode attachments after authorization. */\n\tprivate async downloadAttachments(message: DiscordMessageLike, event: MessageEvent): Promise<void> {\n\t\tconst imageAttachments = Array.from(message.attachments?.values() ?? []);\n\t\tif (imageAttachments.length === 0) return;\n\t\tconst attachments: InboundAttachment[] = [];\n\t\tfor (const attachment of imageAttachments) {\n\t\t\tif (attachment.size !== undefined && attachment.size > this.maxMediaBytes) continue;\n\t\t\ttry {\n\t\t\t\tconst bytes = await this.downloadAttachment(attachment.url);\n\t\t\t\tif (bytes.byteLength > this.maxMediaBytes) continue;\n\t\t\t\tattachments.push({\n\t\t\t\t\tdata: Buffer.from(bytes).toString(\"base64\"),\n\t\t\t\t\tmimeType: attachment.contentType ?? \"application/octet-stream\",\n\t\t\t\t\t...(attachment.name ? { filename: attachment.name } : {}),\n\t\t\t\t});\n\t\t\t} catch {\n\t\t\t\t// Media download failed — drop this attachment, keep the text turn.\n\t\t\t}\n\t\t}\n\t\tif (attachments.length > 0) event.attachments = attachments;\n\t}\n\n\tasync registerCommands(commands: { name: string; description: string }[]): Promise<void> {\n\t\tif (!this.client?.application) return;\n\t\tawait this.client.application.commands.set(\n\t\t\tcommands.slice(0, 95).map((command) => ({\n\t\t\t\tname: command.name,\n\t\t\t\tdescription: command.description.slice(0, 100),\n\t\t\t\toptions: [\n\t\t\t\t\t{\n\t\t\t\t\t\tname: \"args\",\n\t\t\t\t\t\tdescription: \"Command arguments or search\",\n\t\t\t\t\t\ttype: ApplicationCommandOptionType.String,\n\t\t\t\t\t\trequired: false,\n\t\t\t\t\t\tautocomplete: [\"mode\", \"thinking\", \"project\", \"sessions\"].includes(command.name),\n\t\t\t\t\t},\n\t\t\t\t],\n\t\t\t})),\n\t\t);\n\t}\n\n\tprivate async handleNativeCommand(interaction: Interaction): Promise<boolean> {\n\t\tif (!interaction.isChatInputCommand?.() && !interaction.isAutocomplete?.()) return false;\n\t\tif (!interaction.isChatInputCommand() && !interaction.isAutocomplete()) return false;\n\t\tconst thread = interaction.channel?.isThread() ? interaction.channel : undefined;\n\t\tconst source = {\n\t\t\tplatform: \"discord\",\n\t\t\tchatId: thread?.parentId ?? interaction.channelId ?? interaction.user.id,\n\t\t\tthreadId: thread?.id,\n\t\t\tchatType: thread ? (\"thread\" as const) : interaction.inGuild() ? (\"channel\" as const) : (\"dm\" as const),\n\t\t\tuserId: interaction.user.id,\n\t\t\tuserName: interaction.user.username,\n\t\t};\n\t\tconst cfg = loadGatewayConfig();\n\t\tconst authorized = isAuthorized(source, cfg, createPairingStore());\n\t\tif (interaction.isAutocomplete()) {\n\t\t\tif (!authorized) {\n\t\t\t\tawait interaction.respond([]);\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\tconst query = String(interaction.options.getFocused()).toLowerCase();\n\t\t\tlet values: string[] = [];\n\t\t\tif (interaction.commandName === \"mode\") values = [\"yolo\", \"auto\", \"read-only\"];\n\t\t\tif (this.commandSuggestions && [\"thinking\", \"model\"].includes(interaction.commandName)) {\n\t\t\t\tvalues = [...(await this.commandSuggestions(source, interaction.commandName))];\n\t\t\t}\n\t\t\tif (interaction.commandName === \"project\") values = cfg.projectRoots ?? [];\n\t\t\tif (interaction.commandName === \"sessions\") {\n\t\t\t\tconst { SessionManager } = await import(\"../../core/session-manager.ts\");\n\t\t\t\tvalues = (\n\t\t\t\t\tawait Promise.race([\n\t\t\t\t\t\tSessionManager.listAll(),\n\t\t\t\t\t\tnew Promise<[]>((resolve) => setTimeout(() => resolve([]), 1500)),\n\t\t\t\t\t])\n\t\t\t\t).map((s) => s.name || s.id);\n\t\t\t}\n\t\t\tawait interaction.respond(\n\t\t\t\tvalues\n\t\t\t\t\t.filter((s) => s.toLowerCase().includes(query))\n\t\t\t\t\t.slice(0, 25)\n\t\t\t\t\t.map((value) => ({ name: value.slice(0, 100), value: value.slice(0, 100) })),\n\t\t\t);\n\t\t\treturn true;\n\t\t}\n\t\tawait interaction.reply({\n\t\t\tcontent: authorized\n\t\t\t\t? \"Command received.\"\n\t\t\t\t: \"Pair your account before using this command. Send the bot a private message for a pairing code.\",\n\t\t\tephemeral: true,\n\t\t});\n\t\tif (authorized)\n\t\t\tthis.handler?.({\n\t\t\t\ttext: `/${interaction.commandName}${interaction.options.getString(\"args\") ? ` ${interaction.options.getString(\"args\")}` : \"\"}`,\n\t\t\t\tsource,\n\t\t\t\tmessageId: \"\",\n\t\t\t\tmetadata: { mentionedBot: true, nativeCommand: true },\n\t\t\t});\n\t\treturn true;\n\t}\n\n\tasync sendFile(chatId: string, file: string, opts?: SendOptions): Promise<SendResult> {\n\t\tconst channel = await this.fetchChannel(opts?.threadId ?? chatId);\n\t\tif (!channel?.send) return { success: false, error: \"Channel is unavailable.\" };\n\t\ttry {\n\t\t\tconst message = await channel.send({ content: \"\", files: [file] });\n\t\t\treturn { success: true, messageId: message.id };\n\t\t} catch (error) {\n\t\t\treturn { success: false, error: errMessage(error) };\n\t\t}\n\t}\n\n\tprivate async handleInteraction(interaction: DiscordButtonInteractionLike): Promise<void> {\n\t\tif (!this.callbackHandler) return;\n\t\tif (!interaction.isButton()) return;\n\t\tconst event = buttonInteractionToEvent(interaction);\n\t\tif (!event) return;\n\t\tthis.callbackInteractions.set(interaction.id, interaction);\n\t\tif (this.callbackInteractions.size > CALLBACK_INTERACTION_CAP) {\n\t\t\tconst oldest = this.callbackInteractions.keys().next().value;\n\t\t\tif (oldest !== undefined) this.callbackInteractions.delete(oldest);\n\t\t}\n\t\tthis.callbackHandler(event);\n\t}\n\n\t/** Best-effort: fill replyToText from the referenced message when it wasn't cached. */\n\tprivate async resolveReplyToText(message: DiscordMessageLike, event: MessageEvent): Promise<void> {\n\t\tif (event.replyToText !== undefined) return;\n\t\tconst refId = message.reference?.messageId;\n\t\tif (refId == null) return;\n\t\tconst messages = message.channel.messages;\n\t\tif (!messages) return;\n\t\ttry {\n\t\t\tconst referenced = await messages.fetch(refId);\n\t\t\tif (referenced?.content) event.replyToText = referenced.content.slice(0, REPLY_TO_TEXT_MAX);\n\t\t} catch {\n\t\t\t// unknown/deleted reference — no reply context\n\t\t}\n\t}\n\n\t/** autoThread: spin a thread off a bot-mentioning guild-text message and re-target the event. */\n\tprivate async maybeAutoThread(message: DiscordMessageLike, event: MessageEvent): Promise<void> {\n\t\tif (!this.cfg.autoThread) return;\n\t\tif (event.metadata?.mentionedBot !== true) return;\n\t\t// Only guild TEXT channels: DMs can't thread and messages already inside\n\t\t// a thread don't need another one.\n\t\tif (message.channel.type !== ChannelType.GuildText) return;\n\t\tif (typeof message.startThread !== \"function\") return;\n\t\ttry {\n\t\t\tconst thread = await message.startThread({\n\t\t\t\tname: (event.text || \"conversation\").slice(0, THREAD_NAME_MAX),\n\t\t\t\tautoArchiveDuration: 60,\n\t\t\t});\n\t\t\tthis.participatingThreads.add(thread.id);\n\t\t\tif (this.participatingThreads.size > PARTICIPATING_THREAD_CAP) {\n\t\t\t\tconst oldest = this.participatingThreads.values().next().value;\n\t\t\t\tif (oldest !== undefined) this.participatingThreads.delete(oldest);\n\t\t\t}\n\t\t\tevent.source = { ...event.source, chatType: \"thread\", chatId: message.channel.id, threadId: thread.id };\n\t\t} catch {\n\t\t\t// e.g. missing CREATE_THREADS permission — respond in-channel instead.\n\t\t}\n\t}\n\n\tprivate debounceKey(event: MessageEvent): string {\n\t\treturn `${event.source.chatId}:${event.source.threadId ?? \"\"}:${event.source.userId}`;\n\t}\n\n\tprivate dispatchEvent(event: MessageEvent): void {\n\t\tconst key = this.debounceKey(event);\n\t\tif (event.text.trimStart().startsWith(\"/\")) {\n\t\t\t// Commands must not merge: flush any pending text, emit immediately.\n\t\t\tthis.flushPending(key);\n\t\t\tthis.handler?.(event);\n\t\t\treturn;\n\t\t}\n\t\tconst existing = this.pending.get(key);\n\t\tif (existing) {\n\t\t\tthis.scheduler.clearTimeout(existing.timer);\n\t\t\t// Merge: earlier text first, latest messageId (and other fields) win.\n\t\t\tconst mergedAttachments = [...(existing.event.attachments ?? []), ...(event.attachments ?? [])];\n\t\t\texisting.event = {\n\t\t\t\t...event,\n\t\t\t\ttext: `${existing.event.text}\\n${event.text}`,\n\t\t\t\t...(mergedAttachments.length > 0 ? { attachments: mergedAttachments } : {}),\n\t\t\t};\n\t\t\texisting.timer = this.scheduler.setTimeout(() => this.flushPending(key), this.debounceMs);\n\t\t} else {\n\t\t\tconst timer = this.scheduler.setTimeout(() => this.flushPending(key), this.debounceMs);\n\t\t\tthis.pending.set(key, { event, timer });\n\t\t}\n\t}\n\n\tprivate flushPending(key: string): void {\n\t\tconst entry = this.pending.get(key);\n\t\tif (!entry) return;\n\t\tthis.scheduler.clearTimeout(entry.timer);\n\t\tthis.pending.delete(key);\n\t\tthis.handler?.(entry.event);\n\t}\n\n\tprivate cancelPending(key: string): void {\n\t\tconst entry = this.pending.get(key);\n\t\tif (!entry) return;\n\t\tthis.scheduler.clearTimeout(entry.timer);\n\t\tthis.pending.delete(key);\n\t}\n\n\t// -----------------------------------------------------------------------\n\t// Outbound\n\t// -----------------------------------------------------------------------\n\n\tprivate async fetchChannel(id: string): Promise<DiscordChannelLike | null> {\n\t\tif (this.channelCache.has(id)) return this.channelCache.get(id) ?? null;\n\t\tif (!this.client) return null;\n\t\ttry {\n\t\t\tconst channel = await this.client.channels.fetch(id);\n\t\t\tif (channel) this.channelCache.set(id, channel);\n\t\t\tif (this.channelCache.size > CHANNEL_CACHE_CAP) {\n\t\t\t\tconst oldest = this.channelCache.keys().next().value;\n\t\t\t\tif (oldest !== undefined) this.channelCache.delete(oldest);\n\t\t\t}\n\t\t\treturn channel;\n\t\t} catch {\n\t\t\treturn null;\n\t\t}\n\t}\n\n\tprivate recordSent(messageId: string, channel: DiscordChannelLike): void {\n\t\tif (THREAD_TYPES.includes(channel.type)) this.participatingThreads.add(channel.id);\n\t\tif (this.participatingThreads.size > PARTICIPATING_THREAD_CAP) {\n\t\t\tconst oldest = this.participatingThreads.values().next().value;\n\t\t\tif (oldest !== undefined) this.participatingThreads.delete(oldest);\n\t\t}\n\t\tthis.sentMessageChannels.set(messageId, channel.id);\n\t\tif (this.sentMessageChannels.size > SENT_CHANNEL_CAP) {\n\t\t\tconst oldest = this.sentMessageChannels.keys().next().value;\n\t\t\tif (oldest !== undefined) this.sentMessageChannels.delete(oldest);\n\t\t}\n\t}\n\n\tprivate async trySend(\n\t\tchannel: DiscordChannelLike,\n\t\tpayload: { content: string; reply?: { messageReference: string }; components?: unknown[] },\n\t): Promise<SendResult> {\n\t\tif (typeof channel.send !== \"function\") {\n\t\t\treturn { success: false, error: `channel ${channel.id} is not sendable` };\n\t\t}\n\t\ttry {\n\t\t\tconst sent = await channel.send(payload);\n\t\t\tthis.recordSent(sent.id, channel);\n\t\t\treturn { success: true, messageId: sent.id };\n\t\t} catch (err) {\n\t\t\t// 429s are queued internally by discord.js and don't surface here.\n\t\t\treturn { success: false, error: errMessage(err), retryable: discordErrorCode(err) !== ERR_INVALID_FORM_BODY };\n\t\t}\n\t}\n\n\tasync send(chatId: string, text: string, opts?: SendOptions): Promise<SendResult> {\n\t\t// A completed send ends the typing bubble — stop the refresher (see sendTyping).\n\t\tthis.stopTyping(typingKey(chatId, opts?.threadId));\n\t\tconst channel = await this.fetchChannel(opts?.threadId ?? chatId);\n\t\tif (!channel) return { success: false, error: `channel ${opts?.threadId ?? chatId} not found` };\n\t\tconst result = await this.trySend(channel, {\n\t\t\tcontent: text,\n\t\t\treply: opts?.replyTo !== undefined ? { messageReference: opts.replyTo } : undefined,\n\t\t});\n\t\tif (!result.success && opts?.replyTo !== undefined) {\n\t\t\t// The reply target may be deleted/unknown — fall back to a plain send.\n\t\t\treturn this.trySend(channel, { content: text });\n\t\t}\n\t\treturn result;\n\t}\n\n\tasync sendButtons(chatId: string, text: string, buttons: ButtonSpec[][], opts?: SendOptions): Promise<SendResult> {\n\t\tthis.stopTyping(typingKey(chatId, opts?.threadId));\n\t\tconst channel = await this.fetchChannel(opts?.threadId ?? chatId);\n\t\tif (!channel) return { success: false, error: `channel ${opts?.threadId ?? chatId} not found` };\n\t\tconst result = await this.trySend(channel, {\n\t\t\tcontent: text,\n\t\t\treply: opts?.replyTo !== undefined ? { messageReference: opts.replyTo } : undefined,\n\t\t\tcomponents: buildDiscordComponents(buttons),\n\t\t});\n\t\tif (!result.success && opts?.replyTo !== undefined) {\n\t\t\treturn this.trySend(channel, { content: text, components: buildDiscordComponents(buttons) });\n\t\t}\n\t\treturn result;\n\t}\n\n\tasync answerCallback(id: string, text?: string): Promise<void> {\n\t\tconst interaction = this.callbackInteractions.get(id);\n\t\tif (!interaction) return;\n\t\tthis.callbackInteractions.delete(id);\n\t\ttry {\n\t\t\tif (text !== undefined && interaction.reply) {\n\t\t\t\tawait interaction.reply({ content: text, ephemeral: true });\n\t\t\t} else {\n\t\t\t\tawait interaction.deferUpdate();\n\t\t\t}\n\t\t} catch {\n\t\t\t// best-effort acknowledgment\n\t\t}\n\t}\n\n\tasync editMessage(chatId: string, messageId: string, text: string, buttons?: ButtonSpec[][]): Promise<SendResult> {\n\t\t// The router passes only chatId; thread messages live in their own\n\t\t// channel, so prefer the channel recorded at send() time.\n\t\tconst channel = await this.fetchChannel(this.sentMessageChannels.get(messageId) ?? chatId);\n\t\tif (!channel?.messages) {\n\t\t\treturn { success: false, error: `channel ${this.sentMessageChannels.get(messageId) ?? chatId} not found` };\n\t\t}\n\t\ttry {\n\t\t\tconst message = await channel.messages.fetch(messageId);\n\t\t\tif (!message || typeof message.edit !== \"function\") {\n\t\t\t\treturn { success: false, error: `message ${messageId} not found` };\n\t\t\t}\n\t\t\tconst editOptions: { content: string; components?: unknown[] } = { content: text };\n\t\t\tif (buttons !== undefined) editOptions.components = buildDiscordComponents(buttons);\n\t\t\tawait message.edit(editOptions);\n\t\t\treturn { success: true, messageId };\n\t\t} catch (err) {\n\t\t\tconst code = discordErrorCode(err);\n\t\t\tif (code === ERR_UNKNOWN_MESSAGE) return { success: false, error: errMessage(err) }; // terminal\n\t\t\treturn { success: false, error: errMessage(err), retryable: true };\n\t\t}\n\t}\n\n\tasync sendTyping(chatId: string, threadId?: string): Promise<void> {\n\t\t// Self-contained typing refresher (see file header): re-send the action\n\t\t// every 8s until send() completes for this target or the 2min safety net\n\t\t// fires — all errors swallowed.\n\t\tconst key = typingKey(chatId, threadId);\n\t\tconst targetId = threadId ?? chatId;\n\t\tawait this.doTyping(targetId);\n\t\tconst existing = this.typing.get(key);\n\t\tif (existing) {\n\t\t\texisting.lastRefresh = this.now();\n\t\t\treturn;\n\t\t}\n\t\tconst interval = this.scheduler.setInterval(() => {\n\t\t\tconst entry = this.typing.get(key);\n\t\t\tif (!entry) return;\n\t\t\tif (this.now() - entry.lastRefresh > TYPING_SAFETY_MS) {\n\t\t\t\tthis.stopTyping(key);\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tvoid this.doTyping(targetId);\n\t\t}, TYPING_INTERVAL_MS);\n\t\tthis.typing.set(key, { interval, lastRefresh: this.now() });\n\t}\n\n\tprivate async doTyping(channelId: string): Promise<void> {\n\t\ttry {\n\t\t\tconst channel = await this.fetchChannel(channelId);\n\t\t\tawait channel?.sendTyping?.();\n\t\t} catch {\n\t\t\t// typing is cosmetic — never surface\n\t\t}\n\t}\n\n\tprivate stopTyping(key: string): void {\n\t\tconst entry = this.typing.get(key);\n\t\tif (!entry) return;\n\t\tthis.scheduler.clearInterval(entry.interval);\n\t\tthis.typing.delete(key);\n\t}\n}\n"]}
|
|
@@ -65,7 +65,7 @@
|
|
|
65
65
|
* objects, no real Client construction.
|
|
66
66
|
*/
|
|
67
67
|
import { ActionRowBuilder, ApplicationCommandOptionType, ButtonBuilder, ButtonStyle, ChannelType, Client, Events, GatewayIntentBits, } from "discord.js";
|
|
68
|
-
import { isAuthorized
|
|
68
|
+
import { isAuthorized } from "../authz.js";
|
|
69
69
|
import { loadGatewayConfig } from "../config.js";
|
|
70
70
|
import { downloadAttachment } from "../download.js";
|
|
71
71
|
import { createPairingStore } from "../pairing.js";
|
|
@@ -454,7 +454,7 @@ export class DiscordAdapter {
|
|
|
454
454
|
const cfg = loadGatewayConfig();
|
|
455
455
|
const authorized = isAuthorized(source, cfg, createPairingStore());
|
|
456
456
|
if (interaction.isAutocomplete()) {
|
|
457
|
-
if (!authorized
|
|
457
|
+
if (!authorized) {
|
|
458
458
|
await interaction.respond([]);
|
|
459
459
|
return true;
|
|
460
460
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"discord.js","sourceRoot":"","sources":["../../../src/gateway/adapters/discord.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiEG;AAEH,OAAO,EACN,gBAAgB,EAChB,4BAA4B,EAC5B,aAAa,EACb,WAAW,EACX,WAAW,EACX,MAAM,EACN,MAAM,EACN,iBAAiB,GAEjB,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAC3D,OAAO,EAAsB,iBAAiB,EAAE,MAAM,cAAc,CAAC;AACrE,OAAO,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AACpD,OAAO,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAC;AAanD,MAAM,mBAAmB,GAAG,GAAG,CAAC;AAChC,MAAM,kBAAkB,GAAG,IAAI,CAAC,CAAC,4BAA4B;AAC7D,MAAM,gBAAgB,GAAG,OAAO,CAAC,CAAC,yCAAyC;AAC3E,MAAM,iBAAiB,GAAG,GAAG,CAAC;AAC9B,MAAM,eAAe,GAAG,EAAE,CAAC;AAC3B,MAAM,QAAQ,GAAG,GAAG,CAAC;AACrB,MAAM,gBAAgB,GAAG,GAAG,CAAC;AAC7B,MAAM,wBAAwB,GAAG,GAAG,CAAC;AACrC,MAAM,iBAAiB,GAAG,GAAG,CAAC;AAC9B,MAAM,wBAAwB,GAAG,GAAG,CAAC;AACrC,MAAM,q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AEO,KAAK,CAAC,QAAQ,CAAC,SAAiB,EAAiB;QACxD,IAAI,CAAC;YACJ,MAAM,OAAO,GAAG,MAAM,IAAI,CAAC,YAAY,CAAC,SAAS,CAAC,CAAC;YACnD,MAAM,OAAO,EAAE,UAAU,EAAE,EAAE,CAAC;QAC/B,CAAC;QAAC,MAAM,CAAC;YACR,uCAAqC;QACtC,CAAC;IAAA,CACD;IAEO,UAAU,CAAC,GAAW,EAAQ;QACrC,MAAM,KAAK,GAAG,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QACnC,IAAI,CAAC,KAAK;YAAE,OAAO;QACnB,IAAI,CAAC,SAAS,CAAC,aAAa,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;QAC7C,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;IAAA,CACxB;CACD","sourcesContent":["/**\n * lunR: gateway Discord adapter (Phase 3).\n *\n * Built on discord.js v14 (gateway websocket; the library owns reconnects and\n * REST rate-limit queuing internally, so neither is handled here). No voice,\n * no native slash-command registration — commands arrive as plain message\n * text, same as Telegram.\n *\n * Intents: Guilds, GuildMessages, DirectMessages, MessageContent. GuildMembers\n * is deliberately NOT requested: it is a privileged intent, and asking for it\n * without the dev-portal opt-in makes Discord refuse the identify — the bot\n * connects and immediately goes offline. v1 has no role-based config\n * (SessionSource.roleAuthorized stays undefined), so there is nothing to gain.\n *\n * Inbound: messageToEvent() is the pure message → MessageEvent mapping\n * (dm/group/thread chat types, <@id>/<@!id> mention detection with the mention\n * stripped, thread-participation counts as a mention, own + all-bot + system +\n * attachment-only messages dropped, other-mention-without-us dropped outside\n * DMs — hermes DISCORD_IGNORE_NO_MENTION). A module-level seen-id Set\n * (cap 500, FIFO evict) drops gateway-resume replays. cfg.ignoredChannels is\n * enforced adapter-side (channel id or parent id; blacklist wins over\n * mentions). Consecutive texts from the same chat/thread/user are debounced\n * 600ms and merged (\"\\n\"-joined, latest messageId kept); texts starting with\n * \"/\" flush immediately so commands never merge.\n * Button interactions are routed via interactionCreate → buttonInteractionToEvent().\n *\n * chatId/threadId mapping (pairs with session-keys.ts buildSessionKey):\n * dm → chatId = channel.id → agent:main:discord:dm:<channelId>\n * thread → chatId = PARENT channel id, threadId = → agent:main:discord:thread:<parentId>:<threadId>\n * thread channel id (shared session, no userId)\n * guild text/announcement → chatId = channel.id → agent:main:discord:group:<channelId>[:<userId>]\n * (chatType \"group\"; userId appended when groupSessionsPerUser is on)\n *\n * autoThread (cfg.discord.autoThread, default true): a bot-mentioning message\n * in a guild TEXT channel gets a thread spun off it (startThread, 60min\n * archive); the event is re-targeted to the thread (chatType \"thread\", chatId\n * parent id, threadId thread id). On failure the event stays in-channel.\n *\n * Thread participation: an in-memory Set of thread ids the bot has sent to\n * (recorded on every successful send() into a thread and on autoThread\n * creation); messages in those threads count as mentionedBot so follow-ups\n * don't need a re-mention. NOT persisted in v1 (hermes persists it).\n *\n * Outbound: channel.send({ content, reply: { messageReference } }) with a\n * one-shot plain-send fallback when the reply reference fails. DiscordAPIError\n * 50035 (invalid form body, e.g. overlong content) is reported non-retryable;\n * 429s are queued internally by discord.js; everything else is retryable.\n * editMessage resolves the channel via a messageId → channel map recorded at\n * send() time (router passes only chatId, but thread messages live in their\n * own channel); Unknown Message (10008) is a terminal { success: false }.\n * Message splitting is NOT done here — the router owns that (text.ts).\n *\n * Buttons: ActionRowBuilder rows of ButtonBuilder components (max 5×5).\n * answerCallback acknowledges the interaction; a text argument produces an\n * ephemeral reply (used for unauthorized/expired taps), otherwise deferUpdate().\n *\n * Typing: sendTyping() is a self-contained refresher — Discord typing bubbles\n * last ~10s, so the action is re-sent on an unref'd 8s interval per target.\n * The interval stops when send() completes for that target or after 2 minutes\n * without a refresh (safety net). All errors swallowed.\n *\n * Testability: the discord.js Client goes through an injectable clientFactory\n * (tests inject a minimal fake), timers through an injectable Scheduler, and\n * messageToEvent() operates on the structural DiscordMessageLike — plain mock\n * objects, no real Client construction.\n */\n\nimport {\n\tActionRowBuilder,\n\tApplicationCommandOptionType,\n\tButtonBuilder,\n\tButtonStyle,\n\tChannelType,\n\tClient,\n\tEvents,\n\tGatewayIntentBits,\n\ttype Interaction,\n} from \"discord.js\";\nimport { isAuthorized, isGatewayOwner } from \"../authz.ts\";\nimport { type DiscordConfig, loadGatewayConfig } from \"../config.ts\";\nimport { downloadAttachment } from \"../download.ts\";\nimport { createPairingStore } from \"../pairing.ts\";\nimport type {\n\tButtonSpec,\n\tCallbackEvent,\n\tChatType,\n\tInboundAttachment,\n\tMessageEvent,\n\tPlatformAdapter,\n\tSendOptions,\n\tSendResult,\n} from \"../types.ts\";\nimport type { Scheduler } from \"./telegram.ts\";\n\nconst DEFAULT_DEBOUNCE_MS = 600;\nconst TYPING_INTERVAL_MS = 8000; // Discord typing lasts ~10s\nconst TYPING_SAFETY_MS = 120_000; // auto-stop after 2min without a refresh\nconst REPLY_TO_TEXT_MAX = 500;\nconst THREAD_NAME_MAX = 40;\nconst SEEN_CAP = 500;\nconst SENT_CHANNEL_CAP = 500;\nconst CALLBACK_INTERACTION_CAP = 500;\nconst CHANNEL_CACHE_CAP = 500;\nconst PARTICIPATING_THREAD_CAP = 500;\nconst ERR_INVALID_FORM_BODY = 50035; // e.g. content over 2000 chars — non-retryable\nconst ERR_UNKNOWN_MESSAGE = 10008; // deleted/unknown message — terminal\n\nconst THREAD_TYPES: readonly number[] = [\n\tChannelType.PublicThread,\n\tChannelType.PrivateThread,\n\tChannelType.AnnouncementThread,\n];\n\n// ---------------------------------------------------------------------------\n// Structural discord.js shapes (only the fields the adapter reads)\n// ---------------------------------------------------------------------------\n\nexport interface DiscordSentMessageLike {\n\tid: string;\n}\n\nexport interface DiscordFetchedMessageLike {\n\tcontent?: string;\n\tedit?(options: string | { content: string; components?: unknown[] }): Promise<unknown>;\n}\n\nexport interface DiscordChannelLike {\n\tid: string;\n\ttype: number;\n\tparentId?: string | null;\n\tsend?(payload: {\n\t\tcontent: string;\n\t\tfiles?: string[];\n\t\treply?: { messageReference: string };\n\t\tcomponents?: unknown[];\n\t}): Promise<DiscordSentMessageLike>;\n\tsendTyping?(): Promise<unknown>;\n\tmessages?: {\n\t\tcache?: { get(id: string): { content?: string } | undefined };\n\t\tfetch(id: string): Promise<DiscordFetchedMessageLike | null>;\n\t};\n}\n\nexport interface DiscordButtonInteractionLike {\n\tid: string;\n\tcustomId: string;\n\tuser: { id: string; username?: string };\n\tmessage: { id: string; channel: DiscordChannelLike };\n\tisButton(): boolean;\n\tdeferUpdate(): Promise<unknown>;\n\treply?(options: { content: string; ephemeral: boolean }): Promise<unknown>;\n\tupdate?(options: { content: string; components?: unknown[] }): Promise<unknown>;\n}\n\nexport interface DiscordAttachmentLike {\n\tid: string;\n\t/** Direct CDN URL for the attachment content. */\n\turl: string;\n\tcontentType?: string;\n\tname?: string;\n\tsize?: number;\n}\n\nexport interface DiscordMessageLike {\n\tid: string;\n\tcontent?: string;\n\tsystem?: boolean;\n\tauthor: { id: string; bot?: boolean; username?: string };\n\tchannel: DiscordChannelLike;\n\treference?: { messageId?: string | null } | null;\n\tmentions?: {\n\t\thas(user: { id: string }): boolean;\n\t\trepliedUser?: { id: string } | null;\n\t};\n\tstartThread?(opts: { name: string; autoArchiveDuration: number }): Promise<DiscordChannelLike>;\n\tattachments?: DiscordAttachmentLike[];\n}\n\n/** Minimal Client surface; the real discord.js Client satisfies this. */\nexport interface DiscordClientLike {\n\tapplication?: Pick<NonNullable<Client[\"application\"]>, \"commands\"> | null;\n\tuser: { id: string } | null;\n\tlogin(token: string): Promise<unknown>;\n\ton(event: string, listener: (arg: unknown) => void): unknown;\n\tonce(event: string, listener: () => void): unknown;\n\tdestroy(): void;\n\tchannels: { fetch(id: string): Promise<DiscordChannelLike | null> };\n}\n\nexport type DiscordClientFactory = () => DiscordClientLike;\n\n// ---------------------------------------------------------------------------\n// Pure mapping + small helpers\n// ---------------------------------------------------------------------------\n\nfunction errMessage(err: unknown): string {\n\treturn err instanceof Error ? err.message : String(err);\n}\n\n/** lunr: download an inbound Discord attachment from its CDN URL. */\n/** Discord REST error code (DiscordAPIError.code) when present. */\nfunction discordErrorCode(err: unknown): number | undefined {\n\tconst code = (err as { code?: unknown } | null)?.code;\n\treturn typeof code === \"number\" ? code : undefined;\n}\n\nexport interface MessageToEventOptions {\n\t/** Channel ids (or parent ids for threads) to drop outright — blacklist wins over mentions. */\n\tignoredChannels?: readonly string[];\n\t/** Threads the bot has spoken in; messages there count as mentionedBot. */\n\tparticipatingThreads?: ReadonlySet<string>;\n}\n\nfunction isImageContentType(contentType: string | undefined): boolean {\n\treturn typeof contentType === \"string\" && contentType.split(\";\")[0]?.trim().toLowerCase().startsWith(\"image/\");\n}\n\n/** lunr: collect inbound image attachments (contentType image/*) from a Discord message. */\nexport function collectDiscordImageAttachments(message: DiscordMessageLike): DiscordAttachmentLike[] {\n\treturn Array.from(message.attachments?.values() ?? []).filter((attachment) =>\n\t\tisImageContentType(attachment.contentType),\n\t);\n}\n\n/**\n * Map a Discord message to a MessageEvent; null = drop (own message, any bot,\n * system message, ignored channel, unsupported channel type, or a non-DM\n * message mentioning others but not us). Messages with no text but image\n * attachments are kept (v1: images inbound).\n */\nexport function messageToEvent(\n\tmessage: DiscordMessageLike,\n\tbotUser: { id: string },\n\toptions: MessageToEventOptions = {},\n): MessageEvent | null {\n\tconst author = message.author;\n\tif (!author) return null;\n\tif (author.id === botUser.id) return null; // own messages\n\tif (author.bot) return null; // all bots (same simplification as telegram)\n\tif (message.system) return null;\n\tconst content = (message.content ?? \"\").trim();\n\tconst imageAttachments = collectDiscordImageAttachments(message);\n\tif (content === \"\" && imageAttachments.length === 0 && !Array.from(message.attachments?.values() ?? []).length)\n\t\treturn null;\n\n\tconst channel = message.channel;\n\tconst ignored = options.ignoredChannels ?? [];\n\tif (ignored.includes(channel.id) || (channel.parentId != null && ignored.includes(channel.parentId))) {\n\t\treturn null; // blacklist wins over mentions\n\t}\n\n\t// chatId/threadId mapping — see the file header for the session-key pairing.\n\tlet chatType: ChatType;\n\tlet chatId: string;\n\tlet threadId: string | undefined;\n\tif (channel.type === ChannelType.DM) {\n\t\tchatType = \"dm\";\n\t\tchatId = channel.id;\n\t} else if (THREAD_TYPES.includes(channel.type)) {\n\t\tchatType = \"thread\";\n\t\tchatId = channel.parentId ?? channel.id;\n\t\tthreadId = channel.id;\n\t} else if (channel.type === ChannelType.GuildText || channel.type === ChannelType.GuildAnnouncement) {\n\t\tchatType = \"group\";\n\t\tchatId = channel.id;\n\t} else {\n\t\treturn null; // voice/category/forum containers etc. — unsupported in v1\n\t}\n\n\tconst explicitMention = message.mentions?.has(botUser) === true;\n\tconst participating = threadId !== undefined && options.participatingThreads?.has(threadId) === true;\n\tconst mentionedBot = explicitMention || participating;\n\n\t// Multi-agent filter (hermes DISCORD_IGNORE_NO_MENTION): outside DMs, a\n\t// message that mentions other users/bots but not us is not for us.\n\tif (!mentionedBot && chatType !== \"dm\") {\n\t\tconst mentionedIds = [...content.matchAll(/<@!?(\\d+)>/g)].map((m) => m[1]);\n\t\tif (mentionedIds.some((id) => id !== botUser.id)) return null;\n\t}\n\n\tlet text = content;\n\tif (explicitMention) {\n\t\ttext = text.replace(new RegExp(`<@!?${botUser.id}>`, \"g\"), \"\").trim();\n\t}\n\n\tconst refId = message.reference?.messageId;\n\tconst cachedReply = refId != null ? channel.messages?.cache?.get(refId)?.content : undefined;\n\n\treturn {\n\t\ttext,\n\t\tmessageId: message.id,\n\t\treplyToText: cachedReply !== undefined ? cachedReply.slice(0, REPLY_TO_TEXT_MAX) : undefined,\n\t\tsource: {\n\t\t\tplatform: \"discord\",\n\t\t\tchatId,\n\t\t\tchatType,\n\t\t\tuserId: author.id,\n\t\t\tuserName: author.username,\n\t\t\tthreadId,\n\t\t},\n\t\tmetadata: mentionedBot ? { mentionedBot: true } : undefined,\n\t};\n}\n\nfunction mapDiscordChannel(\n\tchannel: DiscordChannelLike,\n): { chatType: ChatType; chatId: string; threadId?: string } | null {\n\tif (channel.type === ChannelType.DM) {\n\t\treturn { chatType: \"dm\", chatId: channel.id };\n\t}\n\tif (THREAD_TYPES.includes(channel.type)) {\n\t\treturn { chatType: \"thread\", chatId: channel.parentId ?? channel.id, threadId: channel.id };\n\t}\n\tif (channel.type === ChannelType.GuildText || channel.type === ChannelType.GuildAnnouncement) {\n\t\treturn { chatType: \"group\", chatId: channel.id };\n\t}\n\treturn null;\n}\n\nexport function buttonInteractionToEvent(interaction: DiscordButtonInteractionLike): CallbackEvent | null {\n\tconst channel = interaction.message.channel;\n\tconst mapped = mapDiscordChannel(channel);\n\tif (!mapped) return null;\n\treturn {\n\t\tid: interaction.id,\n\t\tchatId: mapped.chatId,\n\t\tmessageId: interaction.message.id,\n\t\tuserId: interaction.user.id,\n\t\tuserName: interaction.user.username,\n\t\tdata: interaction.customId,\n\t\tthreadId: mapped.threadId,\n\t};\n}\n\nfunction buildDiscordComponents(buttons: ButtonSpec[][]): unknown[] {\n\tconst rows: ActionRowBuilder<ButtonBuilder>[] = [];\n\tfor (const row of buttons) {\n\t\tconst actionRow = new ActionRowBuilder<ButtonBuilder>();\n\t\tfor (const button of row) {\n\t\t\tconst style = button.data === \"\" ? ButtonStyle.Secondary : ButtonStyle.Secondary;\n\t\t\tconst builder = new ButtonBuilder().setCustomId(button.data).setLabel(button.label).setStyle(style);\n\t\t\tif (button.data === \"\") builder.setDisabled(true);\n\t\t\tactionRow.addComponents(builder);\n\t\t}\n\t\trows.push(actionRow);\n\t}\n\treturn rows;\n}\n\n// ---------------------------------------------------------------------------\n// Gateway-resume replay dedup (module-level: one process = one bot connection)\n// ---------------------------------------------------------------------------\n\nconst seenMessageIds = new Set<string>();\n\n/** Returns true when this id was already seen (drop the replay). */\nfunction markMessageSeen(id: string): boolean {\n\tif (seenMessageIds.has(id)) return true;\n\tseenMessageIds.add(id);\n\tif (seenMessageIds.size > SEEN_CAP) {\n\t\tconst oldest = seenMessageIds.values().next().value; // Sets iterate in insertion order\n\t\tif (oldest !== undefined) seenMessageIds.delete(oldest);\n\t}\n\treturn false;\n}\n\n/** Test hook: reset RESUME dedup state between tests. */\nexport function resetSeenMessageIds(): void {\n\tseenMessageIds.clear();\n}\n\n// ---------------------------------------------------------------------------\n// Adapter\n// ---------------------------------------------------------------------------\n\nconst defaultScheduler: Scheduler = {\n\tsetTimeout: (fn, ms) => {\n\t\tconst handle = setTimeout(fn, ms);\n\t\thandle.unref?.();\n\t\treturn handle;\n\t},\n\tclearTimeout: (handle) => clearTimeout(handle),\n\tsetInterval: (fn, ms) => {\n\t\tconst handle = setInterval(fn, ms);\n\t\thandle.unref?.();\n\t\treturn handle;\n\t},\n\tclearInterval: (handle) => clearInterval(handle),\n};\n\nfunction defaultClientFactory(): DiscordClientLike {\n\t// No GatewayIntentBits.GuildMembers — see the file header for the\n\t// privileged-intent trap (bot stays offline without the portal opt-in).\n\treturn new Client({\n\t\tintents: [\n\t\t\tGatewayIntentBits.Guilds,\n\t\t\tGatewayIntentBits.GuildMessages,\n\t\t\tGatewayIntentBits.DirectMessages,\n\t\t\tGatewayIntentBits.MessageContent,\n\t\t],\n\t}) as unknown as DiscordClientLike;\n}\n\nexport interface DiscordAdapterOptions {\n\t/** Injected Client factory (tests). When omitted, a real discord.js Client is created. */\n\tclientFactory?: DiscordClientFactory;\n\t/** lunr: injected image downloader for inbound attachments (tests). Defaults to fetch(url). */\n\tdownloadAttachment?: (url: string) => Promise<Uint8Array>;\n\t/** lunr: skip inbound media larger than this many bytes. Defaults to 10MB. */\n\tmaxMediaBytes?: number;\n\tscheduler?: Scheduler;\n\tdebounceMs?: number;\n\tnow?: () => number;\n}\n\ninterface PendingEntry {\n\tevent: MessageEvent;\n\ttimer: ReturnType<typeof setTimeout>;\n}\n\ninterface TypingEntry {\n\tinterval: ReturnType<typeof setInterval>;\n\tlastRefresh: number;\n}\n\nfunction typingKey(chatId: string, threadId?: string): string {\n\treturn `${chatId}:${threadId ?? \"\"}`;\n}\n\nexport class DiscordAdapter implements PlatformAdapter {\n\treadonly platform = \"discord\";\n\treadonly maxMessageLength = 2000;\n\n\tprivate readonly cfg: DiscordConfig;\n\tprivate readonly clientFactory: DiscordClientFactory;\n\tprivate readonly downloadAttachment: (url: string) => Promise<Uint8Array>;\n\tprivate readonly maxMediaBytes: number;\n\tprivate readonly scheduler: Scheduler;\n\tprivate readonly debounceMs: number;\n\tprivate readonly now: () => number;\n\tprivate client?: DiscordClientLike;\n\tprivate botUserId?: string;\n\tprivate handler?: (event: MessageEvent) => void;\n\tprivate commandSuggestions?: Parameters<NonNullable<PlatformAdapter[\"setCommandSuggestions\"]>>[0];\n\tprivate callbackHandler?: (event: CallbackEvent) => void;\n\tprivate readonly pending = new Map<string, PendingEntry>();\n\tprivate readonly typing = new Map<string, TypingEntry>();\n\tprivate readonly channelCache = new Map<string, DiscordChannelLike>();\n\t/** Threads the bot has spoken in (session memory only; hermes persists this). */\n\tprivate readonly participatingThreads = new Set<string>();\n\t/** messageId → channel it was sent to, so editMessage finds thread messages. */\n\tprivate readonly sentMessageChannels = new Map<string, string>();\n\t/** callbackId → interaction, so answerCallback can acknowledge the click. */\n\tprivate readonly callbackInteractions = new Map<string, DiscordButtonInteractionLike>();\n\n\tconstructor(cfg: DiscordConfig, options: DiscordAdapterOptions = {}) {\n\t\tthis.cfg = cfg;\n\t\tthis.scheduler = options.scheduler ?? defaultScheduler;\n\t\tthis.debounceMs = options.debounceMs ?? DEFAULT_DEBOUNCE_MS;\n\t\tthis.now = options.now ?? Date.now;\n\t\tif (options.clientFactory) {\n\t\t\tthis.clientFactory = options.clientFactory;\n\t\t} else {\n\t\t\tif (!cfg.token) {\n\t\t\t\tthrow new Error(\"DiscordAdapter: no bot token (set discord.token or LUNR_DISCORD_BOT_TOKEN)\");\n\t\t\t}\n\t\t\tthis.clientFactory = defaultClientFactory;\n\t\t}\n\t\tthis.downloadAttachment = options.downloadAttachment ?? downloadAttachment;\n\t\tthis.maxMediaBytes = options.maxMediaBytes ?? 10 * 1024 * 1024;\n\t}\n\n\tsetCommandSuggestions(\n\t\thandler: (source: MessageEvent[\"source\"], command: string) => Promise<readonly string[]>,\n\t): void {\n\t\tthis.commandSuggestions = handler;\n\t}\n\n\tonMessage(handler: (event: MessageEvent) => void): void {\n\t\tthis.handler = handler;\n\t}\n\n\tonCallback(handler: (event: CallbackEvent) => void): void {\n\t\tthis.callbackHandler = handler;\n\t}\n\n\tasync connect(): Promise<boolean> {\n\t\tif (!this.cfg.token) {\n\t\t\tthrow new Error(\"DiscordAdapter: no bot token (set discord.token or LUNR_DISCORD_BOT_TOKEN)\");\n\t\t}\n\t\tconst client = this.clientFactory();\n\t\tthis.client = client;\n\t\tclient.on(Events.MessageCreate, (message) => {\n\t\t\tvoid this.handleMessage(message as DiscordMessageLike).catch((err) => {\n\t\t\t\tconsole.error(`[gateway] discord message handling error: ${errMessage(err)}`);\n\t\t\t});\n\t\t});\n\t\tclient.on(Events.InteractionCreate, (interaction) => {\n\t\t\tconst event = interaction as Interaction;\n\t\t\tconst handling = event.isButton()\n\t\t\t\t? this.handleInteraction(interaction as DiscordButtonInteractionLike)\n\t\t\t\t: this.handleNativeCommand(event);\n\t\t\tvoid handling.catch((err) => {\n\t\t\t\tconsole.error(`[gateway] discord interaction handling error: ${errMessage(err)}`);\n\t\t\t});\n\t\t});\n\t\tconst ready = new Promise<void>((resolve) => client.once(Events.ClientReady, () => resolve()));\n\t\ttry {\n\t\t\tawait client.login(this.cfg.token);\n\t\t} catch (err) {\n\t\t\tconsole.error(`[gateway] discord login failed: ${errMessage(err)}`);\n\t\t\treturn false;\n\t\t}\n\t\tawait ready;\n\t\tthis.botUserId = client.user?.id;\n\t\tif (this.botUserId === undefined) {\n\t\t\tconsole.error(\"[gateway] discord: client ready without a user — cannot map messages\");\n\t\t\treturn false;\n\t\t}\n\t\treturn true;\n\t}\n\n\tasync disconnect(): Promise<void> {\n\t\tfor (const key of [...this.pending.keys()]) this.cancelPending(key);\n\t\tfor (const key of [...this.typing.keys()]) this.stopTyping(key);\n\t\tthis.channelCache.clear();\n\t\tthis.callbackInteractions.clear();\n\t\tthis.client?.destroy();\n\t\tthis.client = undefined;\n\t}\n\n\t// -----------------------------------------------------------------------\n\t// Inbound dispatch + debounce\n\t// -----------------------------------------------------------------------\n\n\tprivate async handleMessage(message: DiscordMessageLike): Promise<void> {\n\t\tif (!this.botUserId || !this.handler) return;\n\t\tif (markMessageSeen(message.id)) return; // gateway-resume replay\n\t\tconst event = messageToEvent(\n\t\t\tmessage,\n\t\t\t{ id: this.botUserId },\n\t\t\t{\n\t\t\t\tignoredChannels: this.cfg.ignoredChannels,\n\t\t\t\tparticipatingThreads: this.participatingThreads,\n\t\t\t},\n\t\t);\n\t\tif (!event) return;\n\t\tif (!isAuthorized(event.source, { ...loadGatewayConfig(), discord: this.cfg }, createPairingStore())) {\n\t\t\tthis.dispatchEvent(event);\n\t\t\treturn;\n\t\t}\n\t\tawait this.resolveReplyToText(message, event);\n\t\tawait this.maybeAutoThread(message, event);\n\t\tawait this.downloadAttachments(message, event);\n\t\tthis.dispatchEvent(event);\n\t}\n\n\t/** Download and encode attachments after authorization. */\n\tprivate async downloadAttachments(message: DiscordMessageLike, event: MessageEvent): Promise<void> {\n\t\tconst imageAttachments = Array.from(message.attachments?.values() ?? []);\n\t\tif (imageAttachments.length === 0) return;\n\t\tconst attachments: InboundAttachment[] = [];\n\t\tfor (const attachment of imageAttachments) {\n\t\t\tif (attachment.size !== undefined && attachment.size > this.maxMediaBytes) continue;\n\t\t\ttry {\n\t\t\t\tconst bytes = await this.downloadAttachment(attachment.url);\n\t\t\t\tif (bytes.byteLength > this.maxMediaBytes) continue;\n\t\t\t\tattachments.push({\n\t\t\t\t\tdata: Buffer.from(bytes).toString(\"base64\"),\n\t\t\t\t\tmimeType: attachment.contentType ?? \"application/octet-stream\",\n\t\t\t\t\t...(attachment.name ? { filename: attachment.name } : {}),\n\t\t\t\t});\n\t\t\t} catch {\n\t\t\t\t// Media download failed — drop this attachment, keep the text turn.\n\t\t\t}\n\t\t}\n\t\tif (attachments.length > 0) event.attachments = attachments;\n\t}\n\n\tasync registerCommands(commands: { name: string; description: string }[]): Promise<void> {\n\t\tif (!this.client?.application) return;\n\t\tawait this.client.application.commands.set(\n\t\t\tcommands.slice(0, 95).map((command) => ({\n\t\t\t\tname: command.name,\n\t\t\t\tdescription: command.description.slice(0, 100),\n\t\t\t\toptions: [\n\t\t\t\t\t{\n\t\t\t\t\t\tname: \"args\",\n\t\t\t\t\t\tdescription: \"Command arguments or search\",\n\t\t\t\t\t\ttype: ApplicationCommandOptionType.String,\n\t\t\t\t\t\trequired: false,\n\t\t\t\t\t\tautocomplete: [\"mode\", \"thinking\", \"project\", \"sessions\"].includes(command.name),\n\t\t\t\t\t},\n\t\t\t\t],\n\t\t\t})),\n\t\t);\n\t}\n\n\tprivate async handleNativeCommand(interaction: Interaction): Promise<boolean> {\n\t\tif (!interaction.isChatInputCommand?.() && !interaction.isAutocomplete?.()) return false;\n\t\tif (!interaction.isChatInputCommand() && !interaction.isAutocomplete()) return false;\n\t\tconst thread = interaction.channel?.isThread() ? interaction.channel : undefined;\n\t\tconst source = {\n\t\t\tplatform: \"discord\",\n\t\t\tchatId: thread?.parentId ?? interaction.channelId ?? interaction.user.id,\n\t\t\tthreadId: thread?.id,\n\t\t\tchatType: thread ? (\"thread\" as const) : interaction.inGuild() ? (\"channel\" as const) : (\"dm\" as const),\n\t\t\tuserId: interaction.user.id,\n\t\t\tuserName: interaction.user.username,\n\t\t};\n\t\tconst cfg = loadGatewayConfig();\n\t\tconst authorized = isAuthorized(source, cfg, createPairingStore());\n\t\tif (interaction.isAutocomplete()) {\n\t\t\tif (!authorized || !isGatewayOwner(source, cfg)) {\n\t\t\t\tawait interaction.respond([]);\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\tconst query = String(interaction.options.getFocused()).toLowerCase();\n\t\t\tlet values: string[] = [];\n\t\t\tif (interaction.commandName === \"mode\") values = [\"yolo\", \"auto\", \"read-only\"];\n\t\t\tif (this.commandSuggestions && [\"thinking\", \"model\"].includes(interaction.commandName)) {\n\t\t\t\tvalues = [...(await this.commandSuggestions(source, interaction.commandName))];\n\t\t\t}\n\t\t\tif (interaction.commandName === \"project\") values = cfg.projectRoots ?? [];\n\t\t\tif (interaction.commandName === \"sessions\") {\n\t\t\t\tconst { SessionManager } = await import(\"../../core/session-manager.ts\");\n\t\t\t\tvalues = (\n\t\t\t\t\tawait Promise.race([\n\t\t\t\t\t\tSessionManager.listAll(),\n\t\t\t\t\t\tnew Promise<[]>((resolve) => setTimeout(() => resolve([]), 1500)),\n\t\t\t\t\t])\n\t\t\t\t).map((s) => s.name || s.id);\n\t\t\t}\n\t\t\tawait interaction.respond(\n\t\t\t\tvalues\n\t\t\t\t\t.filter((s) => s.toLowerCase().includes(query))\n\t\t\t\t\t.slice(0, 25)\n\t\t\t\t\t.map((value) => ({ name: value.slice(0, 100), value: value.slice(0, 100) })),\n\t\t\t);\n\t\t\treturn true;\n\t\t}\n\t\tawait interaction.reply({\n\t\t\tcontent: authorized\n\t\t\t\t? \"Command received.\"\n\t\t\t\t: \"Pair your account before using this command. Send the bot a private message for a pairing code.\",\n\t\t\tephemeral: true,\n\t\t});\n\t\tif (authorized)\n\t\t\tthis.handler?.({\n\t\t\t\ttext: `/${interaction.commandName}${interaction.options.getString(\"args\") ? ` ${interaction.options.getString(\"args\")}` : \"\"}`,\n\t\t\t\tsource,\n\t\t\t\tmessageId: \"\",\n\t\t\t\tmetadata: { mentionedBot: true, nativeCommand: true },\n\t\t\t});\n\t\treturn true;\n\t}\n\n\tasync sendFile(chatId: string, file: string, opts?: SendOptions): Promise<SendResult> {\n\t\tconst channel = await this.fetchChannel(opts?.threadId ?? chatId);\n\t\tif (!channel?.send) return { success: false, error: \"Channel is unavailable.\" };\n\t\ttry {\n\t\t\tconst message = await channel.send({ content: \"\", files: [file] });\n\t\t\treturn { success: true, messageId: message.id };\n\t\t} catch (error) {\n\t\t\treturn { success: false, error: errMessage(error) };\n\t\t}\n\t}\n\n\tprivate async handleInteraction(interaction: DiscordButtonInteractionLike): Promise<void> {\n\t\tif (!this.callbackHandler) return;\n\t\tif (!interaction.isButton()) return;\n\t\tconst event = buttonInteractionToEvent(interaction);\n\t\tif (!event) return;\n\t\tthis.callbackInteractions.set(interaction.id, interaction);\n\t\tif (this.callbackInteractions.size > CALLBACK_INTERACTION_CAP) {\n\t\t\tconst oldest = this.callbackInteractions.keys().next().value;\n\t\t\tif (oldest !== undefined) this.callbackInteractions.delete(oldest);\n\t\t}\n\t\tthis.callbackHandler(event);\n\t}\n\n\t/** Best-effort: fill replyToText from the referenced message when it wasn't cached. */\n\tprivate async resolveReplyToText(message: DiscordMessageLike, event: MessageEvent): Promise<void> {\n\t\tif (event.replyToText !== undefined) return;\n\t\tconst refId = message.reference?.messageId;\n\t\tif (refId == null) return;\n\t\tconst messages = message.channel.messages;\n\t\tif (!messages) return;\n\t\ttry {\n\t\t\tconst referenced = await messages.fetch(refId);\n\t\t\tif (referenced?.content) event.replyToText = referenced.content.slice(0, REPLY_TO_TEXT_MAX);\n\t\t} catch {\n\t\t\t// unknown/deleted reference — no reply context\n\t\t}\n\t}\n\n\t/** autoThread: spin a thread off a bot-mentioning guild-text message and re-target the event. */\n\tprivate async maybeAutoThread(message: DiscordMessageLike, event: MessageEvent): Promise<void> {\n\t\tif (!this.cfg.autoThread) return;\n\t\tif (event.metadata?.mentionedBot !== true) return;\n\t\t// Only guild TEXT channels: DMs can't thread and messages already inside\n\t\t// a thread don't need another one.\n\t\tif (message.channel.type !== ChannelType.GuildText) return;\n\t\tif (typeof message.startThread !== \"function\") return;\n\t\ttry {\n\t\t\tconst thread = await message.startThread({\n\t\t\t\tname: (event.text || \"conversation\").slice(0, THREAD_NAME_MAX),\n\t\t\t\tautoArchiveDuration: 60,\n\t\t\t});\n\t\t\tthis.participatingThreads.add(thread.id);\n\t\t\tif (this.participatingThreads.size > PARTICIPATING_THREAD_CAP) {\n\t\t\t\tconst oldest = this.participatingThreads.values().next().value;\n\t\t\t\tif (oldest !== undefined) this.participatingThreads.delete(oldest);\n\t\t\t}\n\t\t\tevent.source = { ...event.source, chatType: \"thread\", chatId: message.channel.id, threadId: thread.id };\n\t\t} catch {\n\t\t\t// e.g. missing CREATE_THREADS permission — respond in-channel instead.\n\t\t}\n\t}\n\n\tprivate debounceKey(event: MessageEvent): string {\n\t\treturn `${event.source.chatId}:${event.source.threadId ?? \"\"}:${event.source.userId}`;\n\t}\n\n\tprivate dispatchEvent(event: MessageEvent): void {\n\t\tconst key = this.debounceKey(event);\n\t\tif (event.text.trimStart().startsWith(\"/\")) {\n\t\t\t// Commands must not merge: flush any pending text, emit immediately.\n\t\t\tthis.flushPending(key);\n\t\t\tthis.handler?.(event);\n\t\t\treturn;\n\t\t}\n\t\tconst existing = this.pending.get(key);\n\t\tif (existing) {\n\t\t\tthis.scheduler.clearTimeout(existing.timer);\n\t\t\t// Merge: earlier text first, latest messageId (and other fields) win.\n\t\t\tconst mergedAttachments = [...(existing.event.attachments ?? []), ...(event.attachments ?? [])];\n\t\t\texisting.event = {\n\t\t\t\t...event,\n\t\t\t\ttext: `${existing.event.text}\\n${event.text}`,\n\t\t\t\t...(mergedAttachments.length > 0 ? { attachments: mergedAttachments } : {}),\n\t\t\t};\n\t\t\texisting.timer = this.scheduler.setTimeout(() => this.flushPending(key), this.debounceMs);\n\t\t} else {\n\t\t\tconst timer = this.scheduler.setTimeout(() => this.flushPending(key), this.debounceMs);\n\t\t\tthis.pending.set(key, { event, timer });\n\t\t}\n\t}\n\n\tprivate flushPending(key: string): void {\n\t\tconst entry = this.pending.get(key);\n\t\tif (!entry) return;\n\t\tthis.scheduler.clearTimeout(entry.timer);\n\t\tthis.pending.delete(key);\n\t\tthis.handler?.(entry.event);\n\t}\n\n\tprivate cancelPending(key: string): void {\n\t\tconst entry = this.pending.get(key);\n\t\tif (!entry) return;\n\t\tthis.scheduler.clearTimeout(entry.timer);\n\t\tthis.pending.delete(key);\n\t}\n\n\t// -----------------------------------------------------------------------\n\t// Outbound\n\t// -----------------------------------------------------------------------\n\n\tprivate async fetchChannel(id: string): Promise<DiscordChannelLike | null> {\n\t\tif (this.channelCache.has(id)) return this.channelCache.get(id) ?? null;\n\t\tif (!this.client) return null;\n\t\ttry {\n\t\t\tconst channel = await this.client.channels.fetch(id);\n\t\t\tif (channel) this.channelCache.set(id, channel);\n\t\t\tif (this.channelCache.size > CHANNEL_CACHE_CAP) {\n\t\t\t\tconst oldest = this.channelCache.keys().next().value;\n\t\t\t\tif (oldest !== undefined) this.channelCache.delete(oldest);\n\t\t\t}\n\t\t\treturn channel;\n\t\t} catch {\n\t\t\treturn null;\n\t\t}\n\t}\n\n\tprivate recordSent(messageId: string, channel: DiscordChannelLike): void {\n\t\tif (THREAD_TYPES.includes(channel.type)) this.participatingThreads.add(channel.id);\n\t\tif (this.participatingThreads.size > PARTICIPATING_THREAD_CAP) {\n\t\t\tconst oldest = this.participatingThreads.values().next().value;\n\t\t\tif (oldest !== undefined) this.participatingThreads.delete(oldest);\n\t\t}\n\t\tthis.sentMessageChannels.set(messageId, channel.id);\n\t\tif (this.sentMessageChannels.size > SENT_CHANNEL_CAP) {\n\t\t\tconst oldest = this.sentMessageChannels.keys().next().value;\n\t\t\tif (oldest !== undefined) this.sentMessageChannels.delete(oldest);\n\t\t}\n\t}\n\n\tprivate async trySend(\n\t\tchannel: DiscordChannelLike,\n\t\tpayload: { content: string; reply?: { messageReference: string }; components?: unknown[] },\n\t): Promise<SendResult> {\n\t\tif (typeof channel.send !== \"function\") {\n\t\t\treturn { success: false, error: `channel ${channel.id} is not sendable` };\n\t\t}\n\t\ttry {\n\t\t\tconst sent = await channel.send(payload);\n\t\t\tthis.recordSent(sent.id, channel);\n\t\t\treturn { success: true, messageId: sent.id };\n\t\t} catch (err) {\n\t\t\t// 429s are queued internally by discord.js and don't surface here.\n\t\t\treturn { success: false, error: errMessage(err), retryable: discordErrorCode(err) !== ERR_INVALID_FORM_BODY };\n\t\t}\n\t}\n\n\tasync send(chatId: string, text: string, opts?: SendOptions): Promise<SendResult> {\n\t\t// A completed send ends the typing bubble — stop the refresher (see sendTyping).\n\t\tthis.stopTyping(typingKey(chatId, opts?.threadId));\n\t\tconst channel = await this.fetchChannel(opts?.threadId ?? chatId);\n\t\tif (!channel) return { success: false, error: `channel ${opts?.threadId ?? chatId} not found` };\n\t\tconst result = await this.trySend(channel, {\n\t\t\tcontent: text,\n\t\t\treply: opts?.replyTo !== undefined ? { messageReference: opts.replyTo } : undefined,\n\t\t});\n\t\tif (!result.success && opts?.replyTo !== undefined) {\n\t\t\t// The reply target may be deleted/unknown — fall back to a plain send.\n\t\t\treturn this.trySend(channel, { content: text });\n\t\t}\n\t\treturn result;\n\t}\n\n\tasync sendButtons(chatId: string, text: string, buttons: ButtonSpec[][], opts?: SendOptions): Promise<SendResult> {\n\t\tthis.stopTyping(typingKey(chatId, opts?.threadId));\n\t\tconst channel = await this.fetchChannel(opts?.threadId ?? chatId);\n\t\tif (!channel) return { success: false, error: `channel ${opts?.threadId ?? chatId} not found` };\n\t\tconst result = await this.trySend(channel, {\n\t\t\tcontent: text,\n\t\t\treply: opts?.replyTo !== undefined ? { messageReference: opts.replyTo } : undefined,\n\t\t\tcomponents: buildDiscordComponents(buttons),\n\t\t});\n\t\tif (!result.success && opts?.replyTo !== undefined) {\n\t\t\treturn this.trySend(channel, { content: text, components: buildDiscordComponents(buttons) });\n\t\t}\n\t\treturn result;\n\t}\n\n\tasync answerCallback(id: string, text?: string): Promise<void> {\n\t\tconst interaction = this.callbackInteractions.get(id);\n\t\tif (!interaction) return;\n\t\tthis.callbackInteractions.delete(id);\n\t\ttry {\n\t\t\tif (text !== undefined && interaction.reply) {\n\t\t\t\tawait interaction.reply({ content: text, ephemeral: true });\n\t\t\t} else {\n\t\t\t\tawait interaction.deferUpdate();\n\t\t\t}\n\t\t} catch {\n\t\t\t// best-effort acknowledgment\n\t\t}\n\t}\n\n\tasync editMessage(chatId: string, messageId: string, text: string, buttons?: ButtonSpec[][]): Promise<SendResult> {\n\t\t// The router passes only chatId; thread messages live in their own\n\t\t// channel, so prefer the channel recorded at send() time.\n\t\tconst channel = await this.fetchChannel(this.sentMessageChannels.get(messageId) ?? chatId);\n\t\tif (!channel?.messages) {\n\t\t\treturn { success: false, error: `channel ${this.sentMessageChannels.get(messageId) ?? chatId} not found` };\n\t\t}\n\t\ttry {\n\t\t\tconst message = await channel.messages.fetch(messageId);\n\t\t\tif (!message || typeof message.edit !== \"function\") {\n\t\t\t\treturn { success: false, error: `message ${messageId} not found` };\n\t\t\t}\n\t\t\tconst editOptions: { content: string; components?: unknown[] } = { content: text };\n\t\t\tif (buttons !== undefined) editOptions.components = buildDiscordComponents(buttons);\n\t\t\tawait message.edit(editOptions);\n\t\t\treturn { success: true, messageId };\n\t\t} catch (err) {\n\t\t\tconst code = discordErrorCode(err);\n\t\t\tif (code === ERR_UNKNOWN_MESSAGE) return { success: false, error: errMessage(err) }; // terminal\n\t\t\treturn { success: false, error: errMessage(err), retryable: true };\n\t\t}\n\t}\n\n\tasync sendTyping(chatId: string, threadId?: string): Promise<void> {\n\t\t// Self-contained typing refresher (see file header): re-send the action\n\t\t// every 8s until send() completes for this target or the 2min safety net\n\t\t// fires — all errors swallowed.\n\t\tconst key = typingKey(chatId, threadId);\n\t\tconst targetId = threadId ?? chatId;\n\t\tawait this.doTyping(targetId);\n\t\tconst existing = this.typing.get(key);\n\t\tif (existing) {\n\t\t\texisting.lastRefresh = this.now();\n\t\t\treturn;\n\t\t}\n\t\tconst interval = this.scheduler.setInterval(() => {\n\t\t\tconst entry = this.typing.get(key);\n\t\t\tif (!entry) return;\n\t\t\tif (this.now() - entry.lastRefresh > TYPING_SAFETY_MS) {\n\t\t\t\tthis.stopTyping(key);\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tvoid this.doTyping(targetId);\n\t\t}, TYPING_INTERVAL_MS);\n\t\tthis.typing.set(key, { interval, lastRefresh: this.now() });\n\t}\n\n\tprivate async doTyping(channelId: string): Promise<void> {\n\t\ttry {\n\t\t\tconst channel = await this.fetchChannel(channelId);\n\t\t\tawait channel?.sendTyping?.();\n\t\t} catch {\n\t\t\t// typing is cosmetic — never surface\n\t\t}\n\t}\n\n\tprivate stopTyping(key: string): void {\n\t\tconst entry = this.typing.get(key);\n\t\tif (!entry) return;\n\t\tthis.scheduler.clearInterval(entry.interval);\n\t\tthis.typing.delete(key);\n\t}\n}\n"]}
|
|
1
|
+
{"version":3,"file":"discord.js","sourceRoot":"","sources":["../../../src/gateway/adapters/discord.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiEG;AAEH,OAAO,EACN,gBAAgB,EAChB,4BAA4B,EAC5B,aAAa,EACb,WAAW,EACX,WAAW,EACX,MAAM,EACN,MAAM,EACN,iBAAiB,GAEjB,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAC3C,OAAO,EAAsB,iBAAiB,EAAE,MAAM,cAAc,CAAC;AACrE,OAAO,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AACpD,OAAO,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAC;AAanD,MAAM,mBAAmB,GAAG,GAAG,CAAC;AAChC,MAAM,kBAAkB,GAAG,IAAI,CAAC,CAAC,4BAA4B;AAC7D,MAAM,gBAAgB,GAAG,OAAO,CAAC,CAAC,yCAAyC;AAC3E,MAAM,iBAAiB,GAAG,GAAG,CAAC;AAC9B,MAAM,eAAe,GAAG,EAAE,CAAC;AAC3B,MAAM,QAAQ,GAAG,GAAG,CAAC;AACrB,MAAM,gBAAgB,GAAG,GAAG,CAAC;AAC7B,MAAM,wBAAwB,GAAG,GAAG,CAAC;AACrC,MAAM,iBAAiB,GAAG,GAAG,CAAC;AAC9B,MAAM,wBAAwB,GAAG,GAAG,CAAC;AACrC,MAAM,qBAAqB,GAAG,KAAK,CAAC,CAAC,iDAA+C;AACpF,MA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I,CAAC,YAAY,CAAC,SAAS,CAAC,CAAC;YACnD,MAAM,OAAO,EAAE,UAAU,EAAE,EAAE,CAAC;QAC/B,CAAC;QAAC,MAAM,CAAC;YACR,uCAAqC;QACtC,CAAC;IAAA,CACD;IAEO,UAAU,CAAC,GAAW,EAAQ;QACrC,MAAM,KAAK,GAAG,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QACnC,IAAI,CAAC,KAAK;YAAE,OAAO;QACnB,IAAI,CAAC,SAAS,CAAC,aAAa,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;QAC7C,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;IAAA,CACxB;CACD","sourcesContent":["/**\n * lunR: gateway Discord adapter (Phase 3).\n *\n * Built on discord.js v14 (gateway websocket; the library owns reconnects and\n * REST rate-limit queuing internally, so neither is handled here). No voice,\n * no native slash-command registration — commands arrive as plain message\n * text, same as Telegram.\n *\n * Intents: Guilds, GuildMessages, DirectMessages, MessageContent. GuildMembers\n * is deliberately NOT requested: it is a privileged intent, and asking for it\n * without the dev-portal opt-in makes Discord refuse the identify — the bot\n * connects and immediately goes offline. v1 has no role-based config\n * (SessionSource.roleAuthorized stays undefined), so there is nothing to gain.\n *\n * Inbound: messageToEvent() is the pure message → MessageEvent mapping\n * (dm/group/thread chat types, <@id>/<@!id> mention detection with the mention\n * stripped, thread-participation counts as a mention, own + all-bot + system +\n * attachment-only messages dropped, other-mention-without-us dropped outside\n * DMs — hermes DISCORD_IGNORE_NO_MENTION). A module-level seen-id Set\n * (cap 500, FIFO evict) drops gateway-resume replays. cfg.ignoredChannels is\n * enforced adapter-side (channel id or parent id; blacklist wins over\n * mentions). Consecutive texts from the same chat/thread/user are debounced\n * 600ms and merged (\"\\n\"-joined, latest messageId kept); texts starting with\n * \"/\" flush immediately so commands never merge.\n * Button interactions are routed via interactionCreate → buttonInteractionToEvent().\n *\n * chatId/threadId mapping (pairs with session-keys.ts buildSessionKey):\n * dm → chatId = channel.id → agent:main:discord:dm:<channelId>\n * thread → chatId = PARENT channel id, threadId = → agent:main:discord:thread:<parentId>:<threadId>\n * thread channel id (shared session, no userId)\n * guild text/announcement → chatId = channel.id → agent:main:discord:group:<channelId>[:<userId>]\n * (chatType \"group\"; userId appended when groupSessionsPerUser is on)\n *\n * autoThread (cfg.discord.autoThread, default true): a bot-mentioning message\n * in a guild TEXT channel gets a thread spun off it (startThread, 60min\n * archive); the event is re-targeted to the thread (chatType \"thread\", chatId\n * parent id, threadId thread id). On failure the event stays in-channel.\n *\n * Thread participation: an in-memory Set of thread ids the bot has sent to\n * (recorded on every successful send() into a thread and on autoThread\n * creation); messages in those threads count as mentionedBot so follow-ups\n * don't need a re-mention. NOT persisted in v1 (hermes persists it).\n *\n * Outbound: channel.send({ content, reply: { messageReference } }) with a\n * one-shot plain-send fallback when the reply reference fails. DiscordAPIError\n * 50035 (invalid form body, e.g. overlong content) is reported non-retryable;\n * 429s are queued internally by discord.js; everything else is retryable.\n * editMessage resolves the channel via a messageId → channel map recorded at\n * send() time (router passes only chatId, but thread messages live in their\n * own channel); Unknown Message (10008) is a terminal { success: false }.\n * Message splitting is NOT done here — the router owns that (text.ts).\n *\n * Buttons: ActionRowBuilder rows of ButtonBuilder components (max 5×5).\n * answerCallback acknowledges the interaction; a text argument produces an\n * ephemeral reply (used for unauthorized/expired taps), otherwise deferUpdate().\n *\n * Typing: sendTyping() is a self-contained refresher — Discord typing bubbles\n * last ~10s, so the action is re-sent on an unref'd 8s interval per target.\n * The interval stops when send() completes for that target or after 2 minutes\n * without a refresh (safety net). All errors swallowed.\n *\n * Testability: the discord.js Client goes through an injectable clientFactory\n * (tests inject a minimal fake), timers through an injectable Scheduler, and\n * messageToEvent() operates on the structural DiscordMessageLike — plain mock\n * objects, no real Client construction.\n */\n\nimport {\n\tActionRowBuilder,\n\tApplicationCommandOptionType,\n\tButtonBuilder,\n\tButtonStyle,\n\tChannelType,\n\tClient,\n\tEvents,\n\tGatewayIntentBits,\n\ttype Interaction,\n} from \"discord.js\";\nimport { isAuthorized } from \"../authz.ts\";\nimport { type DiscordConfig, loadGatewayConfig } from \"../config.ts\";\nimport { downloadAttachment } from \"../download.ts\";\nimport { createPairingStore } from \"../pairing.ts\";\nimport type {\n\tButtonSpec,\n\tCallbackEvent,\n\tChatType,\n\tInboundAttachment,\n\tMessageEvent,\n\tPlatformAdapter,\n\tSendOptions,\n\tSendResult,\n} from \"../types.ts\";\nimport type { Scheduler } from \"./telegram.ts\";\n\nconst DEFAULT_DEBOUNCE_MS = 600;\nconst TYPING_INTERVAL_MS = 8000; // Discord typing lasts ~10s\nconst TYPING_SAFETY_MS = 120_000; // auto-stop after 2min without a refresh\nconst REPLY_TO_TEXT_MAX = 500;\nconst THREAD_NAME_MAX = 40;\nconst SEEN_CAP = 500;\nconst SENT_CHANNEL_CAP = 500;\nconst CALLBACK_INTERACTION_CAP = 500;\nconst CHANNEL_CACHE_CAP = 500;\nconst PARTICIPATING_THREAD_CAP = 500;\nconst ERR_INVALID_FORM_BODY = 50035; // e.g. content over 2000 chars — non-retryable\nconst ERR_UNKNOWN_MESSAGE = 10008; // deleted/unknown message — terminal\n\nconst THREAD_TYPES: readonly number[] = [\n\tChannelType.PublicThread,\n\tChannelType.PrivateThread,\n\tChannelType.AnnouncementThread,\n];\n\n// ---------------------------------------------------------------------------\n// Structural discord.js shapes (only the fields the adapter reads)\n// ---------------------------------------------------------------------------\n\nexport interface DiscordSentMessageLike {\n\tid: string;\n}\n\nexport interface DiscordFetchedMessageLike {\n\tcontent?: string;\n\tedit?(options: string | { content: string; components?: unknown[] }): Promise<unknown>;\n}\n\nexport interface DiscordChannelLike {\n\tid: string;\n\ttype: number;\n\tparentId?: string | null;\n\tsend?(payload: {\n\t\tcontent: string;\n\t\tfiles?: string[];\n\t\treply?: { messageReference: string };\n\t\tcomponents?: unknown[];\n\t}): Promise<DiscordSentMessageLike>;\n\tsendTyping?(): Promise<unknown>;\n\tmessages?: {\n\t\tcache?: { get(id: string): { content?: string } | undefined };\n\t\tfetch(id: string): Promise<DiscordFetchedMessageLike | null>;\n\t};\n}\n\nexport interface DiscordButtonInteractionLike {\n\tid: string;\n\tcustomId: string;\n\tuser: { id: string; username?: string };\n\tmessage: { id: string; channel: DiscordChannelLike };\n\tisButton(): boolean;\n\tdeferUpdate(): Promise<unknown>;\n\treply?(options: { content: string; ephemeral: boolean }): Promise<unknown>;\n\tupdate?(options: { content: string; components?: unknown[] }): Promise<unknown>;\n}\n\nexport interface DiscordAttachmentLike {\n\tid: string;\n\t/** Direct CDN URL for the attachment content. */\n\turl: string;\n\tcontentType?: string;\n\tname?: string;\n\tsize?: number;\n}\n\nexport interface DiscordMessageLike {\n\tid: string;\n\tcontent?: string;\n\tsystem?: boolean;\n\tauthor: { id: string; bot?: boolean; username?: string };\n\tchannel: DiscordChannelLike;\n\treference?: { messageId?: string | null } | null;\n\tmentions?: {\n\t\thas(user: { id: string }): boolean;\n\t\trepliedUser?: { id: string } | null;\n\t};\n\tstartThread?(opts: { name: string; autoArchiveDuration: number }): Promise<DiscordChannelLike>;\n\tattachments?: DiscordAttachmentLike[];\n}\n\n/** Minimal Client surface; the real discord.js Client satisfies this. */\nexport interface DiscordClientLike {\n\tapplication?: Pick<NonNullable<Client[\"application\"]>, \"commands\"> | null;\n\tuser: { id: string } | null;\n\tlogin(token: string): Promise<unknown>;\n\ton(event: string, listener: (arg: unknown) => void): unknown;\n\tonce(event: string, listener: () => void): unknown;\n\tdestroy(): void;\n\tchannels: { fetch(id: string): Promise<DiscordChannelLike | null> };\n}\n\nexport type DiscordClientFactory = () => DiscordClientLike;\n\n// ---------------------------------------------------------------------------\n// Pure mapping + small helpers\n// ---------------------------------------------------------------------------\n\nfunction errMessage(err: unknown): string {\n\treturn err instanceof Error ? err.message : String(err);\n}\n\n/** lunr: download an inbound Discord attachment from its CDN URL. */\n/** Discord REST error code (DiscordAPIError.code) when present. */\nfunction discordErrorCode(err: unknown): number | undefined {\n\tconst code = (err as { code?: unknown } | null)?.code;\n\treturn typeof code === \"number\" ? code : undefined;\n}\n\nexport interface MessageToEventOptions {\n\t/** Channel ids (or parent ids for threads) to drop outright — blacklist wins over mentions. */\n\tignoredChannels?: readonly string[];\n\t/** Threads the bot has spoken in; messages there count as mentionedBot. */\n\tparticipatingThreads?: ReadonlySet<string>;\n}\n\nfunction isImageContentType(contentType: string | undefined): boolean {\n\treturn typeof contentType === \"string\" && contentType.split(\";\")[0]?.trim().toLowerCase().startsWith(\"image/\");\n}\n\n/** lunr: collect inbound image attachments (contentType image/*) from a Discord message. */\nexport function collectDiscordImageAttachments(message: DiscordMessageLike): DiscordAttachmentLike[] {\n\treturn Array.from(message.attachments?.values() ?? []).filter((attachment) =>\n\t\tisImageContentType(attachment.contentType),\n\t);\n}\n\n/**\n * Map a Discord message to a MessageEvent; null = drop (own message, any bot,\n * system message, ignored channel, unsupported channel type, or a non-DM\n * message mentioning others but not us). Messages with no text but image\n * attachments are kept (v1: images inbound).\n */\nexport function messageToEvent(\n\tmessage: DiscordMessageLike,\n\tbotUser: { id: string },\n\toptions: MessageToEventOptions = {},\n): MessageEvent | null {\n\tconst author = message.author;\n\tif (!author) return null;\n\tif (author.id === botUser.id) return null; // own messages\n\tif (author.bot) return null; // all bots (same simplification as telegram)\n\tif (message.system) return null;\n\tconst content = (message.content ?? \"\").trim();\n\tconst imageAttachments = collectDiscordImageAttachments(message);\n\tif (content === \"\" && imageAttachments.length === 0 && !Array.from(message.attachments?.values() ?? []).length)\n\t\treturn null;\n\n\tconst channel = message.channel;\n\tconst ignored = options.ignoredChannels ?? [];\n\tif (ignored.includes(channel.id) || (channel.parentId != null && ignored.includes(channel.parentId))) {\n\t\treturn null; // blacklist wins over mentions\n\t}\n\n\t// chatId/threadId mapping — see the file header for the session-key pairing.\n\tlet chatType: ChatType;\n\tlet chatId: string;\n\tlet threadId: string | undefined;\n\tif (channel.type === ChannelType.DM) {\n\t\tchatType = \"dm\";\n\t\tchatId = channel.id;\n\t} else if (THREAD_TYPES.includes(channel.type)) {\n\t\tchatType = \"thread\";\n\t\tchatId = channel.parentId ?? channel.id;\n\t\tthreadId = channel.id;\n\t} else if (channel.type === ChannelType.GuildText || channel.type === ChannelType.GuildAnnouncement) {\n\t\tchatType = \"group\";\n\t\tchatId = channel.id;\n\t} else {\n\t\treturn null; // voice/category/forum containers etc. — unsupported in v1\n\t}\n\n\tconst explicitMention = message.mentions?.has(botUser) === true;\n\tconst participating = threadId !== undefined && options.participatingThreads?.has(threadId) === true;\n\tconst mentionedBot = explicitMention || participating;\n\n\t// Multi-agent filter (hermes DISCORD_IGNORE_NO_MENTION): outside DMs, a\n\t// message that mentions other users/bots but not us is not for us.\n\tif (!mentionedBot && chatType !== \"dm\") {\n\t\tconst mentionedIds = [...content.matchAll(/<@!?(\\d+)>/g)].map((m) => m[1]);\n\t\tif (mentionedIds.some((id) => id !== botUser.id)) return null;\n\t}\n\n\tlet text = content;\n\tif (explicitMention) {\n\t\ttext = text.replace(new RegExp(`<@!?${botUser.id}>`, \"g\"), \"\").trim();\n\t}\n\n\tconst refId = message.reference?.messageId;\n\tconst cachedReply = refId != null ? channel.messages?.cache?.get(refId)?.content : undefined;\n\n\treturn {\n\t\ttext,\n\t\tmessageId: message.id,\n\t\treplyToText: cachedReply !== undefined ? cachedReply.slice(0, REPLY_TO_TEXT_MAX) : undefined,\n\t\tsource: {\n\t\t\tplatform: \"discord\",\n\t\t\tchatId,\n\t\t\tchatType,\n\t\t\tuserId: author.id,\n\t\t\tuserName: author.username,\n\t\t\tthreadId,\n\t\t},\n\t\tmetadata: mentionedBot ? { mentionedBot: true } : undefined,\n\t};\n}\n\nfunction mapDiscordChannel(\n\tchannel: DiscordChannelLike,\n): { chatType: ChatType; chatId: string; threadId?: string } | null {\n\tif (channel.type === ChannelType.DM) {\n\t\treturn { chatType: \"dm\", chatId: channel.id };\n\t}\n\tif (THREAD_TYPES.includes(channel.type)) {\n\t\treturn { chatType: \"thread\", chatId: channel.parentId ?? channel.id, threadId: channel.id };\n\t}\n\tif (channel.type === ChannelType.GuildText || channel.type === ChannelType.GuildAnnouncement) {\n\t\treturn { chatType: \"group\", chatId: channel.id };\n\t}\n\treturn null;\n}\n\nexport function buttonInteractionToEvent(interaction: DiscordButtonInteractionLike): CallbackEvent | null {\n\tconst channel = interaction.message.channel;\n\tconst mapped = mapDiscordChannel(channel);\n\tif (!mapped) return null;\n\treturn {\n\t\tid: interaction.id,\n\t\tchatId: mapped.chatId,\n\t\tmessageId: interaction.message.id,\n\t\tuserId: interaction.user.id,\n\t\tuserName: interaction.user.username,\n\t\tdata: interaction.customId,\n\t\tthreadId: mapped.threadId,\n\t};\n}\n\nfunction buildDiscordComponents(buttons: ButtonSpec[][]): unknown[] {\n\tconst rows: ActionRowBuilder<ButtonBuilder>[] = [];\n\tfor (const row of buttons) {\n\t\tconst actionRow = new ActionRowBuilder<ButtonBuilder>();\n\t\tfor (const button of row) {\n\t\t\tconst style = button.data === \"\" ? ButtonStyle.Secondary : ButtonStyle.Secondary;\n\t\t\tconst builder = new ButtonBuilder().setCustomId(button.data).setLabel(button.label).setStyle(style);\n\t\t\tif (button.data === \"\") builder.setDisabled(true);\n\t\t\tactionRow.addComponents(builder);\n\t\t}\n\t\trows.push(actionRow);\n\t}\n\treturn rows;\n}\n\n// ---------------------------------------------------------------------------\n// Gateway-resume replay dedup (module-level: one process = one bot connection)\n// ---------------------------------------------------------------------------\n\nconst seenMessageIds = new Set<string>();\n\n/** Returns true when this id was already seen (drop the replay). */\nfunction markMessageSeen(id: string): boolean {\n\tif (seenMessageIds.has(id)) return true;\n\tseenMessageIds.add(id);\n\tif (seenMessageIds.size > SEEN_CAP) {\n\t\tconst oldest = seenMessageIds.values().next().value; // Sets iterate in insertion order\n\t\tif (oldest !== undefined) seenMessageIds.delete(oldest);\n\t}\n\treturn false;\n}\n\n/** Test hook: reset RESUME dedup state between tests. */\nexport function resetSeenMessageIds(): void {\n\tseenMessageIds.clear();\n}\n\n// ---------------------------------------------------------------------------\n// Adapter\n// ---------------------------------------------------------------------------\n\nconst defaultScheduler: Scheduler = {\n\tsetTimeout: (fn, ms) => {\n\t\tconst handle = setTimeout(fn, ms);\n\t\thandle.unref?.();\n\t\treturn handle;\n\t},\n\tclearTimeout: (handle) => clearTimeout(handle),\n\tsetInterval: (fn, ms) => {\n\t\tconst handle = setInterval(fn, ms);\n\t\thandle.unref?.();\n\t\treturn handle;\n\t},\n\tclearInterval: (handle) => clearInterval(handle),\n};\n\nfunction defaultClientFactory(): DiscordClientLike {\n\t// No GatewayIntentBits.GuildMembers — see the file header for the\n\t// privileged-intent trap (bot stays offline without the portal opt-in).\n\treturn new Client({\n\t\tintents: [\n\t\t\tGatewayIntentBits.Guilds,\n\t\t\tGatewayIntentBits.GuildMessages,\n\t\t\tGatewayIntentBits.DirectMessages,\n\t\t\tGatewayIntentBits.MessageContent,\n\t\t],\n\t}) as unknown as DiscordClientLike;\n}\n\nexport interface DiscordAdapterOptions {\n\t/** Injected Client factory (tests). When omitted, a real discord.js Client is created. */\n\tclientFactory?: DiscordClientFactory;\n\t/** lunr: injected image downloader for inbound attachments (tests). Defaults to fetch(url). */\n\tdownloadAttachment?: (url: string) => Promise<Uint8Array>;\n\t/** lunr: skip inbound media larger than this many bytes. Defaults to 10MB. */\n\tmaxMediaBytes?: number;\n\tscheduler?: Scheduler;\n\tdebounceMs?: number;\n\tnow?: () => number;\n}\n\ninterface PendingEntry {\n\tevent: MessageEvent;\n\ttimer: ReturnType<typeof setTimeout>;\n}\n\ninterface TypingEntry {\n\tinterval: ReturnType<typeof setInterval>;\n\tlastRefresh: number;\n}\n\nfunction typingKey(chatId: string, threadId?: string): string {\n\treturn `${chatId}:${threadId ?? \"\"}`;\n}\n\nexport class DiscordAdapter implements PlatformAdapter {\n\treadonly platform = \"discord\";\n\treadonly maxMessageLength = 2000;\n\n\tprivate readonly cfg: DiscordConfig;\n\tprivate readonly clientFactory: DiscordClientFactory;\n\tprivate readonly downloadAttachment: (url: string) => Promise<Uint8Array>;\n\tprivate readonly maxMediaBytes: number;\n\tprivate readonly scheduler: Scheduler;\n\tprivate readonly debounceMs: number;\n\tprivate readonly now: () => number;\n\tprivate client?: DiscordClientLike;\n\tprivate botUserId?: string;\n\tprivate handler?: (event: MessageEvent) => void;\n\tprivate commandSuggestions?: Parameters<NonNullable<PlatformAdapter[\"setCommandSuggestions\"]>>[0];\n\tprivate callbackHandler?: (event: CallbackEvent) => void;\n\tprivate readonly pending = new Map<string, PendingEntry>();\n\tprivate readonly typing = new Map<string, TypingEntry>();\n\tprivate readonly channelCache = new Map<string, DiscordChannelLike>();\n\t/** Threads the bot has spoken in (session memory only; hermes persists this). */\n\tprivate readonly participatingThreads = new Set<string>();\n\t/** messageId → channel it was sent to, so editMessage finds thread messages. */\n\tprivate readonly sentMessageChannels = new Map<string, string>();\n\t/** callbackId → interaction, so answerCallback can acknowledge the click. */\n\tprivate readonly callbackInteractions = new Map<string, DiscordButtonInteractionLike>();\n\n\tconstructor(cfg: DiscordConfig, options: DiscordAdapterOptions = {}) {\n\t\tthis.cfg = cfg;\n\t\tthis.scheduler = options.scheduler ?? defaultScheduler;\n\t\tthis.debounceMs = options.debounceMs ?? DEFAULT_DEBOUNCE_MS;\n\t\tthis.now = options.now ?? Date.now;\n\t\tif (options.clientFactory) {\n\t\t\tthis.clientFactory = options.clientFactory;\n\t\t} else {\n\t\t\tif (!cfg.token) {\n\t\t\t\tthrow new Error(\"DiscordAdapter: no bot token (set discord.token or LUNR_DISCORD_BOT_TOKEN)\");\n\t\t\t}\n\t\t\tthis.clientFactory = defaultClientFactory;\n\t\t}\n\t\tthis.downloadAttachment = options.downloadAttachment ?? downloadAttachment;\n\t\tthis.maxMediaBytes = options.maxMediaBytes ?? 10 * 1024 * 1024;\n\t}\n\n\tsetCommandSuggestions(\n\t\thandler: (source: MessageEvent[\"source\"], command: string) => Promise<readonly string[]>,\n\t): void {\n\t\tthis.commandSuggestions = handler;\n\t}\n\n\tonMessage(handler: (event: MessageEvent) => void): void {\n\t\tthis.handler = handler;\n\t}\n\n\tonCallback(handler: (event: CallbackEvent) => void): void {\n\t\tthis.callbackHandler = handler;\n\t}\n\n\tasync connect(): Promise<boolean> {\n\t\tif (!this.cfg.token) {\n\t\t\tthrow new Error(\"DiscordAdapter: no bot token (set discord.token or LUNR_DISCORD_BOT_TOKEN)\");\n\t\t}\n\t\tconst client = this.clientFactory();\n\t\tthis.client = client;\n\t\tclient.on(Events.MessageCreate, (message) => {\n\t\t\tvoid this.handleMessage(message as DiscordMessageLike).catch((err) => {\n\t\t\t\tconsole.error(`[gateway] discord message handling error: ${errMessage(err)}`);\n\t\t\t});\n\t\t});\n\t\tclient.on(Events.InteractionCreate, (interaction) => {\n\t\t\tconst event = interaction as Interaction;\n\t\t\tconst handling = event.isButton()\n\t\t\t\t? this.handleInteraction(interaction as DiscordButtonInteractionLike)\n\t\t\t\t: this.handleNativeCommand(event);\n\t\t\tvoid handling.catch((err) => {\n\t\t\t\tconsole.error(`[gateway] discord interaction handling error: ${errMessage(err)}`);\n\t\t\t});\n\t\t});\n\t\tconst ready = new Promise<void>((resolve) => client.once(Events.ClientReady, () => resolve()));\n\t\ttry {\n\t\t\tawait client.login(this.cfg.token);\n\t\t} catch (err) {\n\t\t\tconsole.error(`[gateway] discord login failed: ${errMessage(err)}`);\n\t\t\treturn false;\n\t\t}\n\t\tawait ready;\n\t\tthis.botUserId = client.user?.id;\n\t\tif (this.botUserId === undefined) {\n\t\t\tconsole.error(\"[gateway] discord: client ready without a user — cannot map messages\");\n\t\t\treturn false;\n\t\t}\n\t\treturn true;\n\t}\n\n\tasync disconnect(): Promise<void> {\n\t\tfor (const key of [...this.pending.keys()]) this.cancelPending(key);\n\t\tfor (const key of [...this.typing.keys()]) this.stopTyping(key);\n\t\tthis.channelCache.clear();\n\t\tthis.callbackInteractions.clear();\n\t\tthis.client?.destroy();\n\t\tthis.client = undefined;\n\t}\n\n\t// -----------------------------------------------------------------------\n\t// Inbound dispatch + debounce\n\t// -----------------------------------------------------------------------\n\n\tprivate async handleMessage(message: DiscordMessageLike): Promise<void> {\n\t\tif (!this.botUserId || !this.handler) return;\n\t\tif (markMessageSeen(message.id)) return; // gateway-resume replay\n\t\tconst event = messageToEvent(\n\t\t\tmessage,\n\t\t\t{ id: this.botUserId },\n\t\t\t{\n\t\t\t\tignoredChannels: this.cfg.ignoredChannels,\n\t\t\t\tparticipatingThreads: this.participatingThreads,\n\t\t\t},\n\t\t);\n\t\tif (!event) return;\n\t\tif (!isAuthorized(event.source, { ...loadGatewayConfig(), discord: this.cfg }, createPairingStore())) {\n\t\t\tthis.dispatchEvent(event);\n\t\t\treturn;\n\t\t}\n\t\tawait this.resolveReplyToText(message, event);\n\t\tawait this.maybeAutoThread(message, event);\n\t\tawait this.downloadAttachments(message, event);\n\t\tthis.dispatchEvent(event);\n\t}\n\n\t/** Download and encode attachments after authorization. */\n\tprivate async downloadAttachments(message: DiscordMessageLike, event: MessageEvent): Promise<void> {\n\t\tconst imageAttachments = Array.from(message.attachments?.values() ?? []);\n\t\tif (imageAttachments.length === 0) return;\n\t\tconst attachments: InboundAttachment[] = [];\n\t\tfor (const attachment of imageAttachments) {\n\t\t\tif (attachment.size !== undefined && attachment.size > this.maxMediaBytes) continue;\n\t\t\ttry {\n\t\t\t\tconst bytes = await this.downloadAttachment(attachment.url);\n\t\t\t\tif (bytes.byteLength > this.maxMediaBytes) continue;\n\t\t\t\tattachments.push({\n\t\t\t\t\tdata: Buffer.from(bytes).toString(\"base64\"),\n\t\t\t\t\tmimeType: attachment.contentType ?? \"application/octet-stream\",\n\t\t\t\t\t...(attachment.name ? { filename: attachment.name } : {}),\n\t\t\t\t});\n\t\t\t} catch {\n\t\t\t\t// Media download failed — drop this attachment, keep the text turn.\n\t\t\t}\n\t\t}\n\t\tif (attachments.length > 0) event.attachments = attachments;\n\t}\n\n\tasync registerCommands(commands: { name: string; description: string }[]): Promise<void> {\n\t\tif (!this.client?.application) return;\n\t\tawait this.client.application.commands.set(\n\t\t\tcommands.slice(0, 95).map((command) => ({\n\t\t\t\tname: command.name,\n\t\t\t\tdescription: command.description.slice(0, 100),\n\t\t\t\toptions: [\n\t\t\t\t\t{\n\t\t\t\t\t\tname: \"args\",\n\t\t\t\t\t\tdescription: \"Command arguments or search\",\n\t\t\t\t\t\ttype: ApplicationCommandOptionType.String,\n\t\t\t\t\t\trequired: false,\n\t\t\t\t\t\tautocomplete: [\"mode\", \"thinking\", \"project\", \"sessions\"].includes(command.name),\n\t\t\t\t\t},\n\t\t\t\t],\n\t\t\t})),\n\t\t);\n\t}\n\n\tprivate async handleNativeCommand(interaction: Interaction): Promise<boolean> {\n\t\tif (!interaction.isChatInputCommand?.() && !interaction.isAutocomplete?.()) return false;\n\t\tif (!interaction.isChatInputCommand() && !interaction.isAutocomplete()) return false;\n\t\tconst thread = interaction.channel?.isThread() ? interaction.channel : undefined;\n\t\tconst source = {\n\t\t\tplatform: \"discord\",\n\t\t\tchatId: thread?.parentId ?? interaction.channelId ?? interaction.user.id,\n\t\t\tthreadId: thread?.id,\n\t\t\tchatType: thread ? (\"thread\" as const) : interaction.inGuild() ? (\"channel\" as const) : (\"dm\" as const),\n\t\t\tuserId: interaction.user.id,\n\t\t\tuserName: interaction.user.username,\n\t\t};\n\t\tconst cfg = loadGatewayConfig();\n\t\tconst authorized = isAuthorized(source, cfg, createPairingStore());\n\t\tif (interaction.isAutocomplete()) {\n\t\t\tif (!authorized) {\n\t\t\t\tawait interaction.respond([]);\n\t\t\t\treturn true;\n\t\t\t}\n\t\t\tconst query = String(interaction.options.getFocused()).toLowerCase();\n\t\t\tlet values: string[] = [];\n\t\t\tif (interaction.commandName === \"mode\") values = [\"yolo\", \"auto\", \"read-only\"];\n\t\t\tif (this.commandSuggestions && [\"thinking\", \"model\"].includes(interaction.commandName)) {\n\t\t\t\tvalues = [...(await this.commandSuggestions(source, interaction.commandName))];\n\t\t\t}\n\t\t\tif (interaction.commandName === \"project\") values = cfg.projectRoots ?? [];\n\t\t\tif (interaction.commandName === \"sessions\") {\n\t\t\t\tconst { SessionManager } = await import(\"../../core/session-manager.ts\");\n\t\t\t\tvalues = (\n\t\t\t\t\tawait Promise.race([\n\t\t\t\t\t\tSessionManager.listAll(),\n\t\t\t\t\t\tnew Promise<[]>((resolve) => setTimeout(() => resolve([]), 1500)),\n\t\t\t\t\t])\n\t\t\t\t).map((s) => s.name || s.id);\n\t\t\t}\n\t\t\tawait interaction.respond(\n\t\t\t\tvalues\n\t\t\t\t\t.filter((s) => s.toLowerCase().includes(query))\n\t\t\t\t\t.slice(0, 25)\n\t\t\t\t\t.map((value) => ({ name: value.slice(0, 100), value: value.slice(0, 100) })),\n\t\t\t);\n\t\t\treturn true;\n\t\t}\n\t\tawait interaction.reply({\n\t\t\tcontent: authorized\n\t\t\t\t? \"Command received.\"\n\t\t\t\t: \"Pair your account before using this command. Send the bot a private message for a pairing code.\",\n\t\t\tephemeral: true,\n\t\t});\n\t\tif (authorized)\n\t\t\tthis.handler?.({\n\t\t\t\ttext: `/${interaction.commandName}${interaction.options.getString(\"args\") ? ` ${interaction.options.getString(\"args\")}` : \"\"}`,\n\t\t\t\tsource,\n\t\t\t\tmessageId: \"\",\n\t\t\t\tmetadata: { mentionedBot: true, nativeCommand: true },\n\t\t\t});\n\t\treturn true;\n\t}\n\n\tasync sendFile(chatId: string, file: string, opts?: SendOptions): Promise<SendResult> {\n\t\tconst channel = await this.fetchChannel(opts?.threadId ?? chatId);\n\t\tif (!channel?.send) return { success: false, error: \"Channel is unavailable.\" };\n\t\ttry {\n\t\t\tconst message = await channel.send({ content: \"\", files: [file] });\n\t\t\treturn { success: true, messageId: message.id };\n\t\t} catch (error) {\n\t\t\treturn { success: false, error: errMessage(error) };\n\t\t}\n\t}\n\n\tprivate async handleInteraction(interaction: DiscordButtonInteractionLike): Promise<void> {\n\t\tif (!this.callbackHandler) return;\n\t\tif (!interaction.isButton()) return;\n\t\tconst event = buttonInteractionToEvent(interaction);\n\t\tif (!event) return;\n\t\tthis.callbackInteractions.set(interaction.id, interaction);\n\t\tif (this.callbackInteractions.size > CALLBACK_INTERACTION_CAP) {\n\t\t\tconst oldest = this.callbackInteractions.keys().next().value;\n\t\t\tif (oldest !== undefined) this.callbackInteractions.delete(oldest);\n\t\t}\n\t\tthis.callbackHandler(event);\n\t}\n\n\t/** Best-effort: fill replyToText from the referenced message when it wasn't cached. */\n\tprivate async resolveReplyToText(message: DiscordMessageLike, event: MessageEvent): Promise<void> {\n\t\tif (event.replyToText !== undefined) return;\n\t\tconst refId = message.reference?.messageId;\n\t\tif (refId == null) return;\n\t\tconst messages = message.channel.messages;\n\t\tif (!messages) return;\n\t\ttry {\n\t\t\tconst referenced = await messages.fetch(refId);\n\t\t\tif (referenced?.content) event.replyToText = referenced.content.slice(0, REPLY_TO_TEXT_MAX);\n\t\t} catch {\n\t\t\t// unknown/deleted reference — no reply context\n\t\t}\n\t}\n\n\t/** autoThread: spin a thread off a bot-mentioning guild-text message and re-target the event. */\n\tprivate async maybeAutoThread(message: DiscordMessageLike, event: MessageEvent): Promise<void> {\n\t\tif (!this.cfg.autoThread) return;\n\t\tif (event.metadata?.mentionedBot !== true) return;\n\t\t// Only guild TEXT channels: DMs can't thread and messages already inside\n\t\t// a thread don't need another one.\n\t\tif (message.channel.type !== ChannelType.GuildText) return;\n\t\tif (typeof message.startThread !== \"function\") return;\n\t\ttry {\n\t\t\tconst thread = await message.startThread({\n\t\t\t\tname: (event.text || \"conversation\").slice(0, THREAD_NAME_MAX),\n\t\t\t\tautoArchiveDuration: 60,\n\t\t\t});\n\t\t\tthis.participatingThreads.add(thread.id);\n\t\t\tif (this.participatingThreads.size > PARTICIPATING_THREAD_CAP) {\n\t\t\t\tconst oldest = this.participatingThreads.values().next().value;\n\t\t\t\tif (oldest !== undefined) this.participatingThreads.delete(oldest);\n\t\t\t}\n\t\t\tevent.source = { ...event.source, chatType: \"thread\", chatId: message.channel.id, threadId: thread.id };\n\t\t} catch {\n\t\t\t// e.g. missing CREATE_THREADS permission — respond in-channel instead.\n\t\t}\n\t}\n\n\tprivate debounceKey(event: MessageEvent): string {\n\t\treturn `${event.source.chatId}:${event.source.threadId ?? \"\"}:${event.source.userId}`;\n\t}\n\n\tprivate dispatchEvent(event: MessageEvent): void {\n\t\tconst key = this.debounceKey(event);\n\t\tif (event.text.trimStart().startsWith(\"/\")) {\n\t\t\t// Commands must not merge: flush any pending text, emit immediately.\n\t\t\tthis.flushPending(key);\n\t\t\tthis.handler?.(event);\n\t\t\treturn;\n\t\t}\n\t\tconst existing = this.pending.get(key);\n\t\tif (existing) {\n\t\t\tthis.scheduler.clearTimeout(existing.timer);\n\t\t\t// Merge: earlier text first, latest messageId (and other fields) win.\n\t\t\tconst mergedAttachments = [...(existing.event.attachments ?? []), ...(event.attachments ?? [])];\n\t\t\texisting.event = {\n\t\t\t\t...event,\n\t\t\t\ttext: `${existing.event.text}\\n${event.text}`,\n\t\t\t\t...(mergedAttachments.length > 0 ? { attachments: mergedAttachments } : {}),\n\t\t\t};\n\t\t\texisting.timer = this.scheduler.setTimeout(() => this.flushPending(key), this.debounceMs);\n\t\t} else {\n\t\t\tconst timer = this.scheduler.setTimeout(() => this.flushPending(key), this.debounceMs);\n\t\t\tthis.pending.set(key, { event, timer });\n\t\t}\n\t}\n\n\tprivate flushPending(key: string): void {\n\t\tconst entry = this.pending.get(key);\n\t\tif (!entry) return;\n\t\tthis.scheduler.clearTimeout(entry.timer);\n\t\tthis.pending.delete(key);\n\t\tthis.handler?.(entry.event);\n\t}\n\n\tprivate cancelPending(key: string): void {\n\t\tconst entry = this.pending.get(key);\n\t\tif (!entry) return;\n\t\tthis.scheduler.clearTimeout(entry.timer);\n\t\tthis.pending.delete(key);\n\t}\n\n\t// -----------------------------------------------------------------------\n\t// Outbound\n\t// -----------------------------------------------------------------------\n\n\tprivate async fetchChannel(id: string): Promise<DiscordChannelLike | null> {\n\t\tif (this.channelCache.has(id)) return this.channelCache.get(id) ?? null;\n\t\tif (!this.client) return null;\n\t\ttry {\n\t\t\tconst channel = await this.client.channels.fetch(id);\n\t\t\tif (channel) this.channelCache.set(id, channel);\n\t\t\tif (this.channelCache.size > CHANNEL_CACHE_CAP) {\n\t\t\t\tconst oldest = this.channelCache.keys().next().value;\n\t\t\t\tif (oldest !== undefined) this.channelCache.delete(oldest);\n\t\t\t}\n\t\t\treturn channel;\n\t\t} catch {\n\t\t\treturn null;\n\t\t}\n\t}\n\n\tprivate recordSent(messageId: string, channel: DiscordChannelLike): void {\n\t\tif (THREAD_TYPES.includes(channel.type)) this.participatingThreads.add(channel.id);\n\t\tif (this.participatingThreads.size > PARTICIPATING_THREAD_CAP) {\n\t\t\tconst oldest = this.participatingThreads.values().next().value;\n\t\t\tif (oldest !== undefined) this.participatingThreads.delete(oldest);\n\t\t}\n\t\tthis.sentMessageChannels.set(messageId, channel.id);\n\t\tif (this.sentMessageChannels.size > SENT_CHANNEL_CAP) {\n\t\t\tconst oldest = this.sentMessageChannels.keys().next().value;\n\t\t\tif (oldest !== undefined) this.sentMessageChannels.delete(oldest);\n\t\t}\n\t}\n\n\tprivate async trySend(\n\t\tchannel: DiscordChannelLike,\n\t\tpayload: { content: string; reply?: { messageReference: string }; components?: unknown[] },\n\t): Promise<SendResult> {\n\t\tif (typeof channel.send !== \"function\") {\n\t\t\treturn { success: false, error: `channel ${channel.id} is not sendable` };\n\t\t}\n\t\ttry {\n\t\t\tconst sent = await channel.send(payload);\n\t\t\tthis.recordSent(sent.id, channel);\n\t\t\treturn { success: true, messageId: sent.id };\n\t\t} catch (err) {\n\t\t\t// 429s are queued internally by discord.js and don't surface here.\n\t\t\treturn { success: false, error: errMessage(err), retryable: discordErrorCode(err) !== ERR_INVALID_FORM_BODY };\n\t\t}\n\t}\n\n\tasync send(chatId: string, text: string, opts?: SendOptions): Promise<SendResult> {\n\t\t// A completed send ends the typing bubble — stop the refresher (see sendTyping).\n\t\tthis.stopTyping(typingKey(chatId, opts?.threadId));\n\t\tconst channel = await this.fetchChannel(opts?.threadId ?? chatId);\n\t\tif (!channel) return { success: false, error: `channel ${opts?.threadId ?? chatId} not found` };\n\t\tconst result = await this.trySend(channel, {\n\t\t\tcontent: text,\n\t\t\treply: opts?.replyTo !== undefined ? { messageReference: opts.replyTo } : undefined,\n\t\t});\n\t\tif (!result.success && opts?.replyTo !== undefined) {\n\t\t\t// The reply target may be deleted/unknown — fall back to a plain send.\n\t\t\treturn this.trySend(channel, { content: text });\n\t\t}\n\t\treturn result;\n\t}\n\n\tasync sendButtons(chatId: string, text: string, buttons: ButtonSpec[][], opts?: SendOptions): Promise<SendResult> {\n\t\tthis.stopTyping(typingKey(chatId, opts?.threadId));\n\t\tconst channel = await this.fetchChannel(opts?.threadId ?? chatId);\n\t\tif (!channel) return { success: false, error: `channel ${opts?.threadId ?? chatId} not found` };\n\t\tconst result = await this.trySend(channel, {\n\t\t\tcontent: text,\n\t\t\treply: opts?.replyTo !== undefined ? { messageReference: opts.replyTo } : undefined,\n\t\t\tcomponents: buildDiscordComponents(buttons),\n\t\t});\n\t\tif (!result.success && opts?.replyTo !== undefined) {\n\t\t\treturn this.trySend(channel, { content: text, components: buildDiscordComponents(buttons) });\n\t\t}\n\t\treturn result;\n\t}\n\n\tasync answerCallback(id: string, text?: string): Promise<void> {\n\t\tconst interaction = this.callbackInteractions.get(id);\n\t\tif (!interaction) return;\n\t\tthis.callbackInteractions.delete(id);\n\t\ttry {\n\t\t\tif (text !== undefined && interaction.reply) {\n\t\t\t\tawait interaction.reply({ content: text, ephemeral: true });\n\t\t\t} else {\n\t\t\t\tawait interaction.deferUpdate();\n\t\t\t}\n\t\t} catch {\n\t\t\t// best-effort acknowledgment\n\t\t}\n\t}\n\n\tasync editMessage(chatId: string, messageId: string, text: string, buttons?: ButtonSpec[][]): Promise<SendResult> {\n\t\t// The router passes only chatId; thread messages live in their own\n\t\t// channel, so prefer the channel recorded at send() time.\n\t\tconst channel = await this.fetchChannel(this.sentMessageChannels.get(messageId) ?? chatId);\n\t\tif (!channel?.messages) {\n\t\t\treturn { success: false, error: `channel ${this.sentMessageChannels.get(messageId) ?? chatId} not found` };\n\t\t}\n\t\ttry {\n\t\t\tconst message = await channel.messages.fetch(messageId);\n\t\t\tif (!message || typeof message.edit !== \"function\") {\n\t\t\t\treturn { success: false, error: `message ${messageId} not found` };\n\t\t\t}\n\t\t\tconst editOptions: { content: string; components?: unknown[] } = { content: text };\n\t\t\tif (buttons !== undefined) editOptions.components = buildDiscordComponents(buttons);\n\t\t\tawait message.edit(editOptions);\n\t\t\treturn { success: true, messageId };\n\t\t} catch (err) {\n\t\t\tconst code = discordErrorCode(err);\n\t\t\tif (code === ERR_UNKNOWN_MESSAGE) return { success: false, error: errMessage(err) }; // terminal\n\t\t\treturn { success: false, error: errMessage(err), retryable: true };\n\t\t}\n\t}\n\n\tasync sendTyping(chatId: string, threadId?: string): Promise<void> {\n\t\t// Self-contained typing refresher (see file header): re-send the action\n\t\t// every 8s until send() completes for this target or the 2min safety net\n\t\t// fires — all errors swallowed.\n\t\tconst key = typingKey(chatId, threadId);\n\t\tconst targetId = threadId ?? chatId;\n\t\tawait this.doTyping(targetId);\n\t\tconst existing = this.typing.get(key);\n\t\tif (existing) {\n\t\t\texisting.lastRefresh = this.now();\n\t\t\treturn;\n\t\t}\n\t\tconst interval = this.scheduler.setInterval(() => {\n\t\t\tconst entry = this.typing.get(key);\n\t\t\tif (!entry) return;\n\t\t\tif (this.now() - entry.lastRefresh > TYPING_SAFETY_MS) {\n\t\t\t\tthis.stopTyping(key);\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tvoid this.doTyping(targetId);\n\t\t}, TYPING_INTERVAL_MS);\n\t\tthis.typing.set(key, { interval, lastRefresh: this.now() });\n\t}\n\n\tprivate async doTyping(channelId: string): Promise<void> {\n\t\ttry {\n\t\t\tconst channel = await this.fetchChannel(channelId);\n\t\t\tawait channel?.sendTyping?.();\n\t\t} catch {\n\t\t\t// typing is cosmetic — never surface\n\t\t}\n\t}\n\n\tprivate stopTyping(key: string): void {\n\t\tconst entry = this.typing.get(key);\n\t\tif (!entry) return;\n\t\tthis.scheduler.clearInterval(entry.interval);\n\t\tthis.typing.delete(key);\n\t}\n}\n"]}
|