UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

50 lines (49 loc) 2.27 kB
//#region src/plugin-sdk/channel-send-result.ts /** Attaches the channel id to a single outbound send result. */ function attachChannelToResult(channel, result) { return { ...result, channel }; } /** Attaches the channel id to each outbound send result in order. */ function attachChannelToResults(channel, results) { return results.map((result) => attachChannelToResult(channel, result)); } /** Creates an empty outbound delivery result for send paths that produced no platform id. */ function createEmptyChannelResult(channel, result = {}) { return attachChannelToResult(channel, { messageId: "", ...result }); } /** Wraps outbound send methods that already return delivery-shaped results without channel ids. */ function createAttachedChannelResultAdapter(params) { return { sendText: params.sendText ? async (ctx) => attachChannelToResult(params.channel, await params.sendText(ctx)) : void 0, sendMedia: params.sendMedia ? async (ctx) => attachChannelToResult(params.channel, await params.sendMedia(ctx)) : void 0, sendPoll: params.sendPoll ? async (ctx) => attachChannelToResult(params.channel, await params.sendPoll(ctx)) : void 0 }; } function buildRawChannelAdapterResult(channel, result) { if (!result.ok) throw new Error(result.error?.trim() || `Channel send failed for ${channel}`); return buildChannelSendResult(channel, result); } /** Wraps legacy raw text/media send methods and normalizes their results. */ function createRawChannelSendResultAdapter(params) { return { sendText: params.sendText ? async (ctx) => buildRawChannelAdapterResult(params.channel, await params.sendText(ctx)) : void 0, sendMedia: params.sendMedia ? async (ctx) => buildRawChannelAdapterResult(params.channel, await params.sendMedia(ctx)) : void 0 }; } /** Normalize raw channel send results into the shape shared outbound callers expect. */ function buildChannelSendResult(channel, result) { return { channel, ok: result.ok, messageId: result.messageId ?? "", error: result.error ? new Error(result.error) : void 0 }; } //#endregion export { createEmptyChannelResult as a, createAttachedChannelResultAdapter as i, attachChannelToResults as n, createRawChannelSendResultAdapter as o, buildChannelSendResult as r, attachChannelToResult as t };