import { TextChannel, Message, AttachmentBuilder } from "discord.js"; import { splitForDiscord } from "./split.js"; import { childLogger } from "../runtime/logger.js"; const log = childLogger("send-response"); const FILE_THRESHOLD = 4000; /** * Sends an agent response to a Discord channel. * - Text > 4000 chars is sent as a Markdown file attachment. * - Text <= 4000 chars is split with splitForDiscord and sent as plain messages. */ export async function sendResponse( channel: TextChannel, text: string, _triggerMessage: Message ): Promise { if (text.length === 0) { return; } if (text.length > FILE_THRESHOLD) { log.debug({ length: text.length }, "Response exceeds threshold — sending as file"); await channel.send({ files: [ new AttachmentBuilder(Buffer.from(text, "utf-8"), { name: "response.md", }), ], }); return; } const chunks = splitForDiscord(text); for (const chunk of chunks) { await channel.send(chunk); } }