openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
5 lines • 6.05 kB
JavaScript
import{D as parseStrictPositiveInteger}from"./number-coercion-B3CfI447.mjs";import{n as sliceUtf16Safe}from"./utf16-slice-DA3Mllbx.mjs";import"./text-utility-runtime-CDtGqkrl.mjs";import"./number-runtime-G0nhUVZu.mjs";import{l as fitsTelegramCallbackData,t as buildTelegramNativeCommandCallbackData}from"./native-command-callback-data-IMqzGQhB.mjs";import{createHash}from"node:crypto";
//!extensions/telegram/src/model-buttons.ts
const MODELS_PAGE_SIZE=8;const MODEL_BUTTON_LABEL_MAX_LENGTH=38;const LEGACY_PROVIDER_PATTERN=/^[a-z0-9_.-]+$/i;const CALLBACK_PREFIX={providers:`mdl_prov`,back:`mdl_back`,list:`mdl_list_`,selectStandard:`mdl_sel_`,opaqueModel:`mdl1~m:`,opaqueProvider:`mdl1~p:`};function hashOpaqueCallback(e,...t){return createHash(`sha256`).update(JSON.stringify([`openclaw.telegram.${e}-callback.v1`,...t])).digest(`base64url`)}function parseModelCallbackData(e){const t=e.trim();const n=t.match(/^mdl1~m:([A-Za-z0-9_-]{43})$/);if(n?.[1])return{type:`select-ref`,digest:n[1]};const r=t.match(/^mdl1~p:([A-Za-z0-9_-]{43}):(\d+)$/);if(r?.[1]){const e=parseStrictPositiveInteger(r[2]);if(e!==void 0&&fitsTelegramCallbackData(t))return{type:`list-ref`,digest:r[1],page:e}}if(t===CALLBACK_PREFIX.providers||t===CALLBACK_PREFIX.back)return{type:t===CALLBACK_PREFIX.providers?`providers`:`back`};const i=t.match(/^mdl_list_([a-z0-9_.-]+)_(\d+)$/i);if(i){const[,e,t]=i;const n=parseStrictPositiveInteger(t);if(e&&n!==void 0)return{type:`list`,provider:e,page:n}}const a=t.match(/^mdl_sel\/(.+)$/)?.[1];if(a)return{type:`select`,model:a};const[,o,s]=t.match(/^mdl_sel_([^/]+)\/(.+)$/)??[];return o&&s?{type:`select`,provider:o,model:s}:null}function buildModelSelectionCallbackData(e){const t=`${CALLBACK_PREFIX.selectStandard}${e.provider}/${e.model}`;if(LEGACY_PROVIDER_PATTERN.test(e.provider)&&fitsTelegramCallbackData(t))return t;return`${CALLBACK_PREFIX.opaqueModel}${hashOpaqueCallback(`model`,e.provider,e.model)}`}function buildProviderListCallbackData(e,t){const n=`${CALLBACK_PREFIX.list}${e}_${t}`;return LEGACY_PROVIDER_PATTERN.test(e)&&fitsTelegramCallbackData(n)?n:`${CALLBACK_PREFIX.opaqueProvider}${hashOpaqueCallback(`provider`,e)}:${t}`}function resolveModelSelection(e){const t=e.callback;if(t.type===`select`&&t.provider)return{kind:`resolved`,provider:t.provider,model:t.model};const n=e.providers.flatMap(n=>{const r=e.byProvider.get(n);if(t.type===`select`)return r?.has(t.model)?[{provider:n,model:t.model}]:[];return[...r??[]].filter(e=>hashOpaqueCallback(`model`,n,e)===t.digest).map(e=>({provider:n,model:e}))});const[r]=n;return n.length===1&&r?{kind:`resolved`,...r}:{kind:`ambiguous`,model:t.type===`select`?t.model:t.digest,matchingProviders:n.map(({provider:e})=>e)}}function resolveModelListCallback(e){const{callback:t}=e;if(t.type===`list`)return{provider:t.provider,page:t.page};const n=e.providers.filter(e=>hashOpaqueCallback(`provider`,e)===t.digest);const[r]=n;return n.length===1&&r!==void 0?{provider:r,page:t.page}:void 0}function buildProviderKeyboard(e){const t=[];for(const[n,r]of e.entries())(t[Math.floor(n/2)]??=[]).push({text:`${r.id} (${r.count})`,callback_data:buildProviderListCallbackData(r.id,1)});return t}function buildPaginationRow(e,t,n){return[{page:e-1,text:`◀ Prev`,show:e>1},{page:null,text:`${e}/${t}`,show:true},{page:e+1,text:`Next ▶`,show:e<t}].filter(({show:e})=>e).map(({page:e,text:t})=>({text:t,callback_data:n(e)}))}function buildModelsKeyboard(e){const{provider:t,models:n,currentModel:r,currentPage:i,totalPages:a,modelNames:o}=e;const s=r?.trim()??``;const c=e.pageSize??MODELS_PAGE_SIZE;if(n.length===0)return[[{text:`<< Back`,callback_data:CALLBACK_PREFIX.back}]];const l=[];const u=(i-1)*c;const d=Math.min(u+c,n.length);const f=n.slice(u,d);for(const e of f){const n=buildModelSelectionCallbackData({provider:t,model:e});const r=s.length>0&&s===(s.includes(`/`)?`${t}/${e}`:e);const i=e.includes(`/`)?`${t}/${e}`:e;const a=o?.get(`${t}/${e}`)??i;const c=a.length<=MODEL_BUTTON_LABEL_MAX_LENGTH?a:`…${sliceUtf16Safe(a,-37)}`;const u=r?`${c} ✓`:c;l.push([{text:u,callback_data:n}])}if(a>1)l.push(buildPaginationRow(i,a,e=>buildProviderListCallbackData(t,e??i)));l.push([{text:`<< Back`,callback_data:CALLBACK_PREFIX.back}]);return l}function buildBrowseProvidersButton(){return[[{text:`Browse providers`,callback_data:CALLBACK_PREFIX.providers}]]}function getModelsPageSize(){return MODELS_PAGE_SIZE}function calculateTotalPages(e,t){const n=t??MODELS_PAGE_SIZE;return n>0?Math.ceil(e/n):1}
//!extensions/telegram/src/command-ui.ts
function withTelegramButtons(e){return{telegram:{buttons:e}}}function buildCommandsPaginationKeyboard(e,t,n){return[buildPaginationRow(e,t,e=>{const t=`commands_page_${e??`noop`}`;const r=`${t}:${n}`;return n&&fitsTelegramCallbackData(r)?r:t})]}function buildTelegramCommandsListChannelData(e){if(e.totalPages<=1)return null;return withTelegramButtons(buildCommandsPaginationKeyboard(e.currentPage,e.totalPages,e.agentId))}function buildTelegramModelsProviderChannelData(e){if(e.providers.length===0)return null;return withTelegramButtons(buildProviderKeyboard(e.providers))}function buildTelegramModelsAddProviderChannelData(e){if(e.providers.length===0)return null;return withTelegramButtons(e.providers.map(e=>[{text:e.id,callback_data:buildTelegramNativeCommandCallbackData(`/models add ${e.id}`)}]))}function buildTelegramModelsListChannelData(e){return withTelegramButtons(buildModelsKeyboard(e))}function buildTelegramModelBrowseChannelData(){return withTelegramButtons(buildBrowseProvidersButton())}export{buildTelegramModelsListChannelData as a,buildModelSelectionCallbackData as c,calculateTotalPages as d,getModelsPageSize as f,resolveModelSelection as h,buildTelegramModelsAddProviderChannelData as i,buildModelsKeyboard as l,resolveModelListCallback as m,buildTelegramCommandsListChannelData as n,buildTelegramModelsProviderChannelData as o,parseModelCallbackData as p,buildTelegramModelBrowseChannelData as r,buildBrowseProvidersButton as s,buildCommandsPaginationKeyboard as t,buildProviderKeyboard as u};