openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
9 lines (7 loc) • 7.09 kB
JavaScript
import{c as isMessagePresentationInteractiveBlock,d as normalizeLegacyInteractiveReply,f as normalizeMessagePresentation,l as legacyInteractiveReplyToPresentation,v as renderMessagePresentationFallbackText,x as resolveLegacyInteractiveTextFallback}from"./payload-B5c5LLYp.mjs";import{o as copyReplyPayloadMetadata}from"./reply-payload-C7eQHHoS.mjs";import{d as resolveAskUserQuestionOptionIndices}from"./reply-payload-C6sYX5Rf.mjs";import{n as adaptMessagePresentationForChannel}from"./presentation-delivery-LAf1XxpQ.mjs";import"./interactive-runtime-Do8Y23C_.mjs";import{i as stampAgentControls}from"./agent-control-BfmwiAcx.mjs";import{E as escapeTelegramHtml,h as buildInlineKeyboard}from"./text-chunk-limit-YsOTt7TW.mjs";import{o as resolveTelegramInlineButtons,r as buildTelegramPresentationButtons,t as appendTelegramDroppedControlFallback}from"./button-types-CVT0c2ex.mjs";
//!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(`
`).map(e=>{const t=e.trim();return t&&!/[_*]/.test(t)?`_${t}_`:e}).join(`
`)}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(`
`)}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}));function canonicalizeTelegramPresentationPayload(e,t){const n=e.channelData?.telegram;const r=n?.typedCallbackOwner===`agent`?stampAgentControls({presentation:e.presentation,interactive:e.interactive}):e;const i=normalizeMessagePresentation(r.presentation);if(!i){const t=resolveTelegramInlineButtons({buttons:n?.buttons});if(!buildInlineKeyboard(t)||e.text?.trim())return e;return copyReplyPayloadMetadata(e,{...e,text:TELEGRAM_CONTROL_ONLY_FALLBACK})}const a=t?.richTables===true;const o=adaptMessagePresentationForChannel({presentation:i,capabilities:resolveTelegramPresentationCapabilities({richMessages:a})});const s=normalizeLegacyInteractiveReply(r.interactive);const c={allowWebAppButtons:t?.allowWebAppButtons===true,supportsDisabled:a,questionOptionIndices:resolveAskUserQuestionOptionIndices(e)};const l=resolveTelegramInlineButtons({buttons:n?.buttons,interactive:s},c);const u=l===void 0;const d=[];const f=[];const partitionControls=(e,t)=>{const n=[];const r=[];for(const i of e)(buildTelegramPresentationButtons({blocks:[t([i])]},c)?.length?n:r).push(i);if(n.length>0)f.push(t(n));if(r.length>0)d.push(t(r));return r.length>0};for(const e of o.blocks){if(!u||!isMessagePresentationInteractiveBlock(e)){d.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)d.push({type:`text`,text:e.placeholder})}const p=buildTelegramPresentationButtons(adaptMessagePresentationForChannel({presentation:{blocks:f},capabilities:{limits:{actions:{maxLabelLength:64,supportsDisabled:a},selects:{maxLabelLength:64}}}}),c);const m=l??p;const h={...o,blocks:d};const g=a?renderTelegramRichFallbackText(h):renderMessagePresentationFallbackText({presentation:h});const _=resolveLegacyInteractiveTextFallback({text:e.text,interactive:s})?.trim()??``;const v=e.presentationTextMode===`fallback`;const y=g.length>0&&(_===g||_.endsWith(`\n\n${g}`));const b=v?f.length>0?g:a?g||_:_||g:y?_:[_,g].filter(Boolean).join(`
`);const{presentation:x,presentationTextMode:S,...C}=e;const w={...C,text:b||(m?TELEGRAM_CONTROL_ONLY_FALLBACK:``)};if(m)w.channelData={...e.channelData,telegram:{...n,buttons:m}};return copyReplyPayloadMetadata(e,w)}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}export{resolveFinalTelegramPresentationText as a,markTelegramDroppedControlFallback as i,canonicalizeTelegramPresentationPayload as n,resolveTelegramInteractiveTextFallback as o,copyTelegramDroppedControlFallback as r,resolveTelegramPresentationCapabilities as s,applyTextToPayload as t};