openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
166 lines (165 loc) • 7.36 kB
JavaScript
import { C as resolveMessagePresentationButtonAction, c as isMessagePresentationInteractiveBlock, d as normalizeLegacyInteractiveReply, f as normalizeMessagePresentation, l as legacyInteractiveReplyToPresentation, v as renderMessagePresentationFallbackText } from "./payload-Cab4FCxl.mjs";
import { u as resolveAskUserQuestionOptionIndex } from "./reply-payload-CwASJYas.mjs";
import { p as parseExecApprovalCommandText } from "./exec-approval-reply-CfpNUvnC.mjs";
import "./approval-reply-runtime-B0T7gU8u.mjs";
import "./interactive-runtime-sv_-VQ8S.mjs";
import { c as buildTelegramApprovalCallbackData, f as rewriteTelegramApprovalDecisionAlias, n as buildTelegramOpaqueCallbackData, p as sanitizeTelegramCallbackData, t as buildTelegramNativeCommandCallbackData, u as hasTelegramApprovalCallbackPrefix } from "./native-command-callback-data-DMBSeb_1.mjs";
//#region extensions/telegram/src/question-callback-data.ts
const TELEGRAM_QUESTION_CALLBACK_PREFIXES = ["tgq1:", "tgqo1:"];
const QUESTION_RECORD_ID_PATTERN = /^ask_[a-f0-9]{32}$/u;
function hasTelegramQuestionCallbackPrefix(e) {
return TELEGRAM_QUESTION_CALLBACK_PREFIXES.some((t) => e?.startsWith(t) === true);
}
function buildTelegramQuestionCallbackData(e) {
if (!QUESTION_RECORD_ID_PATTERN.test(e.questionId) || !Number.isInteger(e.optionIndex) || e.optionIndex < 0 || e.optionIndex > 3) return;
return `tgq1:${e.questionId}:${e.optionIndex}`;
}
function buildTelegramQuestionCustomInputCallbackData(e) {
return QUESTION_RECORD_ID_PATTERN.test(e) ? `tgqo1:${e}` : void 0;
}
function parseTelegramQuestionCallbackData(e) {
if (!e) return null;
const t = /^tgq1:(ask_[a-f0-9]{32}):([0-3])$/u.exec(e);
if (t?.[1] && t[2]) return {
questionId: t[1],
intent: "select",
optionIndex: Number(t[2])
};
const n = /^tgqo1:(ask_[a-f0-9]{32})$/u.exec(e);
return n?.[1] ? {
questionId: n[1],
intent: "custom-input"
} : null;
}
//#endregion
//#region extensions/telegram/src/button-types.ts
function normalizeTelegramButtonStyle(e) {
return e === "danger" || e === "success" || e === "primary" ? e : void 0;
}
function appendTelegramDroppedControlFallback(e, t) {
const n = renderMessagePresentationFallbackText({ presentation: { blocks: [{
type: "buttons",
buttons: t.map((e) => ({
label: e.label,
value: "unavailable"
}))
}] } });
if (!n || e === n || e.endsWith(`\n\n${n}`)) return e;
return [e, n].filter(Boolean).join("\n\n");
}
const TELEGRAM_INTERACTIVE_ROW_SIZE = 3;
function recordDroppedControl(e, t, n, r) {
const i = r ? Buffer.byteLength(r, "utf8") : void 0;
t?.onDroppedControl?.({
label: e.label,
reason: i !== void 0 && i > 64 ? "callback_data_too_long" : n,
...i !== void 0 ? { callbackDataBytes: i } : {}
});
}
function toTelegramInlineButton(e, t) {
const n = normalizeTelegramButtonStyle(e.style);
const r = resolveMessagePresentationButtonAction(e);
if (!r) return recordDroppedControl(e, t, "invalid_action");
if (e.disabled === true) return t?.supportsDisabled === true ? {
text: e.label,
disabled: {},
style: n
} : recordDroppedControl(e, t, "disabled_unsupported");
if (r.type === "url") return {
text: e.label,
url: r.url,
style: n
};
if (r.type === "web-app") return t?.allowWebAppButtons === true && r.url ? {
text: e.label,
web_app: { url: r.url },
style: n
} : recordDroppedControl(e, t, "web_app_unavailable");
const callbackButton = (r, i = "invalid_action", a) => r ? {
text: e.label,
callback_data: r,
style: n
} : recordDroppedControl(e, t, i, a);
if (r.type === "approval") return callbackButton(buildTelegramApprovalCallbackData(r));
if (r.type === "question") {
const n = t?.questionOptionIndices?.has(r.questionId) === true;
if ("intent" in r) return callbackButton(n ? buildTelegramQuestionCustomInputCallbackData(r.questionId) : void 0, "question_context_unavailable");
const i = resolveAskUserQuestionOptionIndex({
questionOptionIndices: t?.questionOptionIndices,
questionId: r.questionId,
optionValue: r.optionValue
});
if (i === void 0) return recordDroppedControl(e, t, "question_context_unavailable");
return callbackButton(buildTelegramQuestionCallbackData({
questionId: r.questionId,
optionIndex: i
}));
}
if (r.type === "command") {
const e = rewriteTelegramApprovalDecisionAlias(r.command.trim());
const t = e ? buildTelegramNativeCommandCallbackData(e) : void 0;
return callbackButton((t ? sanitizeTelegramCallbackData(t) : void 0) ?? (parseExecApprovalCommandText(e) ? sanitizeTelegramCallbackData(e) : void 0), "invalid_action", t);
}
const i = r.value.trim();
const a = Boolean(e.action) || hasTelegramApprovalCallbackPrefix(i) || hasTelegramQuestionCallbackPrefix(i) ? buildTelegramOpaqueCallbackData(r.value, t?.typedCallbackOwner) : r.value;
return callbackButton(sanitizeTelegramCallbackData(a), "invalid_action", a);
}
function chunkInteractiveButtons(e, t, n) {
let r;
for (const i of e) {
const e = toTelegramInlineButton(i, n);
if (!e) continue;
const a = resolveMessagePresentationButtonAction(i)?.type === "question";
if (!r || r.length === TELEGRAM_INTERACTIVE_ROW_SIZE || a) {
r = [];
t.push(r);
}
r.push(e);
if (a) r = void 0;
}
}
function buildTelegramPresentationButtons(e, t) {
const n = [];
for (const r of e?.blocks ?? []) {
if (!isMessagePresentationInteractiveBlock(r)) continue;
chunkInteractiveButtons(r.type === "buttons" ? r.buttons : r.options.map((e) => ({
label: e.label,
action: e.action,
value: e.value
})), n, t);
}
return n.length > 0 ? n : void 0;
}
/** Resolve Telegram inline buttons, preserving explicit and legacy button precedence. */
function resolveTelegramInlineButtons(e, t) {
if (e.buttons) return e.buttons;
const n = normalizeLegacyInteractiveReply(e.interactive);
return buildTelegramPresentationButtons(n ? legacyInteractiveReplyToPresentation(n) : void 0, t) ?? buildTelegramPresentationButtons(normalizeMessagePresentation(e.presentation), t);
}
/** Message-action params: their typed callbacks belong to the agent. */
function resolveTelegramButtonsFromParams(e, t = normalizeMessagePresentation(e.presentation), n) {
return resolveTelegramInlineButtons({
presentation: t,
interactive: e.interactive
}, {
...n,
typedCallbackOwner: "agent"
});
}
function buildTelegramControlDegradation(e, t) {
if (e.length === 0) return;
const n = [...new Set(e.map((e) => e.reason))];
const r = n.includes("callback_data_too_long");
return {
warning: t ? `Telegram delivered ${e.length} unencodable control${e.length === 1 ? "" : "s"} as readable text.` : `Telegram could not deliver ${e.length} control${e.length === 1 ? "" : "s"}.`,
degradedDelivery: {
droppedControls: e.length,
fallback: t ? "text" : "not_delivered",
reasons: n,
...r ? { callbackDataLimitBytes: 64 } : {},
guidance: r ? `Shorten callback data to at most ${64} UTF-8 bytes and retry if clickable controls are required.` : "Retry with a supported control action if clickable controls are required."
}
};
}
//#endregion
export { resolveTelegramButtonsFromParams as a, parseTelegramQuestionCallbackData as c, normalizeTelegramButtonStyle as i, buildTelegramControlDegradation as n, resolveTelegramInlineButtons as o, buildTelegramPresentationButtons as r, hasTelegramQuestionCallbackPrefix as s, appendTelegramDroppedControlFallback as t };