import type { ChannelOutboundAdapter, ChannelPollResult } from "../channels/plugins/types.js"; import type { OutboundDeliveryResult } from "../infra/outbound/deliver.js"; export type { ChannelOutboundAdapter } from "../channels/plugins/types.js"; export type ChannelSendRawResult = { ok: boolean; messageId?: string | null; error?: string | null; }; export function attachChannelToResult(channel: string, result: T) { return { channel, ...result, }; } export function attachChannelToResults(channel: string, results: readonly T[]) { return results.map((result) => attachChannelToResult(channel, result)); } export function createEmptyChannelResult( channel: string, result: Partial> & { messageId?: string; } = {}, ): OutboundDeliveryResult { return attachChannelToResult(channel, { messageId: "", ...result, }); } type MaybePromise = T | Promise; type SendTextParams = Parameters>[0]; type SendMediaParams = Parameters>[0]; type SendPollParams = Parameters>[0]; export function createAttachedChannelResultAdapter(params: { channel: string; sendText?: (ctx: SendTextParams) => MaybePromise>; sendMedia?: (ctx: SendMediaParams) => MaybePromise>; sendPoll?: (ctx: SendPollParams) => MaybePromise>; }): Pick { return { sendText: params.sendText ? async (ctx) => attachChannelToResult(params.channel, await params.sendText!(ctx)) : undefined, sendMedia: params.sendMedia ? async (ctx) => attachChannelToResult(params.channel, await params.sendMedia!(ctx)) : undefined, sendPoll: params.sendPoll ? async (ctx) => attachChannelToResult(params.channel, await params.sendPoll!(ctx)) : undefined, }; } export function createRawChannelSendResultAdapter(params: { channel: string; sendText?: (ctx: SendTextParams) => MaybePromise; sendMedia?: (ctx: SendMediaParams) => MaybePromise; }): Pick { return { sendText: params.sendText ? async (ctx) => buildChannelSendResult(params.channel, await params.sendText!(ctx)) : undefined, sendMedia: params.sendMedia ? async (ctx) => buildChannelSendResult(params.channel, await params.sendMedia!(ctx)) : undefined, }; } /** Normalize raw channel send results into the shape shared outbound callers expect. */ export function buildChannelSendResult(channel: string, result: ChannelSendRawResult) { return { channel, ok: result.ok, messageId: result.messageId ?? "", error: result.error ? new Error(result.error) : undefined, }; }