openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
229 lines (228 loc) • 8.19 kB
JavaScript
import { c as isMessagePresentationInteractiveBlock, d as normalizeLegacyInteractiveReply, f as normalizeMessagePresentation, l as legacyInteractiveReplyToPresentation, v as renderMessagePresentationFallbackText, x as resolveLegacyInteractiveTextFallback } from "./payload-Cab4FCxl.mjs";
import { o as copyReplyPayloadMetadata } from "./reply-payload-k5WsQWxi.mjs";
import { d as resolveAskUserQuestionOptionIndices } from "./reply-payload-CwASJYas.mjs";
import { n as adaptMessagePresentationForChannel } from "./presentation-delivery-5KftDvIO.mjs";
import "./interactive-runtime-sv_-VQ8S.mjs";
import { E as escapeTelegramHtml, h as buildInlineKeyboard } from "./text-chunk-limit-DTiKmKhG.mjs";
import { o as resolveTelegramInlineButtons, r as buildTelegramPresentationButtons, t as appendTelegramDroppedControlFallback } from "./button-types-D-RCz2yJ.mjs";
//#region extensions/telegram/src/interactive-fallback.ts
const TELEGRAM_CONTROL_ONLY_FALLBACK = "Choose an option.";
const TELEGRAM_PRESENTATION_CAPABILITIES = {
supported: true,
buttons: true,
selects: true,
context: true,
divider: false,
tables: false,
limits: {
actions: {
maxActions: 100,
maxActionsPerRow: 3,
supportsStyles: false,
supportsDisabled: false
},
selects: { maxOptions: 100 },
text: { markdownDialect: "markdown" }
}
};
function resolveTelegramPresentationCapabilities(e) {
const { limits: t } = TELEGRAM_PRESENTATION_CAPABILITIES;
return e.richMessages ? {
...TELEGRAM_PRESENTATION_CAPABILITIES,
tables: true,
limits: {
...t,
actions: {
...t.actions,
supportsDisabled: true
}
}
} : TELEGRAM_PRESENTATION_CAPABILITIES;
}
function escapeTelegramTableCellText(e) {
return escapeTelegramHtml(String(e)).replace(/\s+/g, " ").trim();
}
function renderTelegramTableIsland(e) {
const t = e.caption.trim() ? `<caption>${escapeTelegramTableCellText(e.caption)}</caption>` : "";
const renderRow = (e, t) => `<tr>${e.map((e, n) => {
const r = t === "all" || n === t ? "th" : "td";
return `<${r}>${escapeTelegramTableCellText(e)}</${r}>`;
}).join("")}</tr>`;
return `<table>${t}<thead>${renderRow(e.headers, "all")}</thead><tbody>${e.rows.map((t) => renderRow(t, e.rowHeaderColumnIndex)).join("")}</tbody></table>`;
}
function renderTelegramContextText(e) {
return e.split("\n").map((e) => {
const t = e.trim();
return t && !/[_*]/.test(t) ? `_${t}_` : e;
}).join("\n");
}
function renderTelegramRichFallbackText(e) {
const t = [];
if (e.title?.trim()) t.push(`**${e.title.trim()}**`);
for (const n of e.blocks) {
const e = n.type === "table" ? renderTelegramTableIsland(n) : n.type === "context" ? renderTelegramContextText(n.text) : renderMessagePresentationFallbackText({ presentation: { blocks: [n] } });
if (e.trim()) t.push(e);
}
return t.join("\n\n");
}
const telegramDroppedControlFallbacks = /* @__PURE__ */ new WeakMap();
function markTelegramDroppedControlFallback(e, t, n) {
if (t !== n) telegramDroppedControlFallbacks.set(e, n.slice(t.length));
return e;
}
function copyTelegramDroppedControlFallback(e, t) {
const n = telegramDroppedControlFallbacks.get(e);
if (t && n) telegramDroppedControlFallbacks.set(t, n);
return t;
}
const applyTextToPayload = (e, t) => e.text === t ? e : copyTelegramDroppedControlFallback(e, copyReplyPayloadMetadata(e, {
...e,
text: t
}));
/** Convert portable presentation into the one Telegram payload shape used by every send funnel. */
function canonicalizeTelegramPresentationPayload(e, t) {
const n = normalizeMessagePresentation(e.presentation);
const r = e.channelData?.telegram;
if (!n) {
const t = resolveTelegramInlineButtons({ buttons: r?.buttons });
if (!buildInlineKeyboard(t) || e.text?.trim()) return e;
return copyReplyPayloadMetadata(e, {
...e,
text: TELEGRAM_CONTROL_ONLY_FALLBACK
});
}
const i = t?.richTables === true;
const a = adaptMessagePresentationForChannel({
presentation: n,
capabilities: resolveTelegramPresentationCapabilities({ richMessages: i })
});
const o = normalizeLegacyInteractiveReply(e.interactive);
const s = {
allowWebAppButtons: t?.allowWebAppButtons === true,
supportsDisabled: i,
questionOptionIndices: resolveAskUserQuestionOptionIndices(e),
...r?.typedCallbackOwner === "agent" ? { typedCallbackOwner: "agent" } : {}
};
const c = resolveTelegramInlineButtons({
buttons: r?.buttons,
interactive: o
}, s);
const l = c === void 0;
const u = [];
const d = [];
const partitionControls = (e, t) => {
const n = [];
const r = [];
for (const i of e) (buildTelegramPresentationButtons({ blocks: [t([i])] }, s)?.length ? n : r).push(i);
if (n.length > 0) d.push(t(n));
if (r.length > 0) u.push(t(r));
return r.length > 0;
};
for (const e of a.blocks) {
if (!l || !isMessagePresentationInteractiveBlock(e)) {
u.push(e);
continue;
}
if (e.type === "buttons") partitionControls(e.buttons, (e) => ({
type: "buttons",
buttons: e
}));
else if (!partitionControls(e.options, (t) => ({
...e,
options: t
})) && e.placeholder) u.push({
type: "text",
text: e.placeholder
});
}
const f = buildTelegramPresentationButtons(adaptMessagePresentationForChannel({
presentation: { blocks: d },
capabilities: { limits: {
actions: {
maxLabelLength: 64,
supportsDisabled: i
},
selects: { maxLabelLength: 64 }
} }
}), s);
const p = c ?? f;
const m = {
...a,
blocks: u
};
const h = i ? renderTelegramRichFallbackText(m) : renderMessagePresentationFallbackText({ presentation: m });
const g = resolveLegacyInteractiveTextFallback({
text: e.text,
interactive: o
})?.trim() ?? "";
const _ = e.presentationTextMode === "fallback";
const v = h.length > 0 && (g === h || g.endsWith(`\n\n${h}`));
const y = _ ? d.length > 0 ? h : i ? h || g : g || h : v ? g : [g, h].filter(Boolean).join("\n\n");
const { presentation: b, presentationTextMode: x, ...S } = e;
const C = {
...S,
text: y || (p ? TELEGRAM_CONTROL_ONLY_FALLBACK : "")
};
if (p) C.channelData = {
...e.channelData,
telegram: {
...r,
buttons: p
}
};
return copyReplyPayloadMetadata(e, C);
}
function resolveTelegramInteractiveTextFallback(e) {
const t = normalizeLegacyInteractiveReply(e.interactive);
const n = resolveLegacyInteractiveTextFallback({
text: e.text ?? void 0,
interactive: t
});
if (n?.trim()) return n;
const r = normalizeMessagePresentation(e.presentation);
if (r) {
const t = renderMessagePresentationFallbackText({
text: e.text ?? void 0,
presentation: r
});
if (t.trim()) return t;
}
if (!t) return n;
const i = legacyInteractiveReplyToPresentation(t);
if (!i) return n;
const a = renderMessagePresentationFallbackText({ presentation: i });
return a.trim() ? a : n;
}
function resolveFinalTelegramPresentationText(e) {
if (!e.richMessages) return;
if (!normalizeMessagePresentation(e.payload.presentation)) return;
const t = [];
const n = {
allowWebAppButtons: e.allowWebAppButtons === true,
supportsDisabled: true,
questionOptionIndices: resolveAskUserQuestionOptionIndices(e.payload)
};
const r = e.payload.channelData?.telegram;
resolveTelegramInlineButtons({
buttons: r?.buttons,
interactive: normalizeLegacyInteractiveReply(e.payload.interactive)
}, {
...n,
onDroppedControl: (e) => t.push(e)
});
const i = telegramDroppedControlFallbacks.get(e.payload);
const a = i && e.text.endsWith(i) ? e.text.slice(0, -i.length) : e.text;
const o = i && e.payload.presentationTextMode !== "fallback" ? appendTelegramDroppedControlFallback(a, t) : a;
const s = canonicalizeTelegramPresentationPayload({
...e.payload,
text: o
}, {
richTables: true,
allowWebAppButtons: e.allowWebAppButtons
}).text?.trimEnd();
if (!s) return;
const c = e.payload.presentationTextMode === "fallback" ? appendTelegramDroppedControlFallback(s, t) : s;
return c !== e.text.trimEnd() ? c : void 0;
}
//#endregion
export { resolveFinalTelegramPresentationText as a, markTelegramDroppedControlFallback as i, canonicalizeTelegramPresentationPayload as n, resolveTelegramInteractiveTextFallback as o, copyTelegramDroppedControlFallback as r, resolveTelegramPresentationCapabilities as s, applyTextToPayload as t };