UNPKG

eve

Version:

Filesystem-first framework for durable backend AI agents that run anywhere.

2 lines • 4.1 kB
import{isNonEmptyString,isObject}from"#shared/guards.js";function parseTelegramUpdate(e){if(!isObject(e))return null;let t=parseTelegramMessage(e.message);if(t!==null)return{kind:`message`,message:t};let n=parseTelegramCallbackQuery(e.callback_query);return n===null?null:{callbackQuery:n,kind:`callback_query`}}function formatTelegramContextBlock(e){return[`<telegram_context>`,`response_medium: telegram`,`response_instructions: Reply for Telegram in concise plain text. Avoid tables, long code fences, and formatting that depends on Markdown rendering.`,`chat_id: ${e.chatId}`,`chat_type: ${e.chatType}`,...e.chatTitle?[`chat_title: ${e.chatTitle}`]:[],`message_id: ${e.messageId}`,...e.messageThreadId===void 0?[]:[`message_thread_id: ${e.messageThreadId}`],...e.userId?[`user_id: ${e.userId}`]:[],...e.username?[`username: ${e.username}`]:[],...e.botUsername?[`bot_username: ${e.botUsername}`]:[],...e.isMentioned===void 0?[]:[`is_mentioned: ${e.isMentioned}`],`</telegram_context>`].join(` `)}function parseTelegramMessage(e){if(!isObject(e))return null;let t=parseTelegramChat(e.chat),n=numberLikeToString(e.message_id);return!t||!n?null:{attachments:parseAttachments(e),caption:typeof e.caption==`string`?e.caption:``,chat:t,from:parseTelegramUser(e.from),messageId:n,messageThreadId:typeof e.message_thread_id==`number`?e.message_thread_id:void 0,raw:e,replyToMessage:parseMessageReference(e.reply_to_message),text:typeof e.text==`string`?e.text:``}}function parseTelegramCallbackQuery(e){if(!isObject(e)||!isNonEmptyString(e.id))return null;let t=parseTelegramUser(e.from);return t?{data:typeof e.data==`string`?e.data:void 0,from:t,id:e.id,message:parseMessageReference(e.message),raw:e}:null}function parseMessageReference(e){if(!isObject(e))return;let t=parseTelegramChat(e.chat),n=numberLikeToString(e.message_id);if(t&&n)return{chat:t,from:parseTelegramUser(e.from),messageId:n,messageThreadId:typeof e.message_thread_id==`number`?e.message_thread_id:void 0}}function parseTelegramChat(e){if(!isObject(e))return null;let t=numberLikeToString(e.id),n=parseTelegramChatType(e.type);return!t||!n?null:{id:t,title:typeof e.title==`string`?e.title:void 0,type:n,username:typeof e.username==`string`?e.username:void 0}}function parseTelegramUser(e){if(!isObject(e))return;let t=numberLikeToString(e.id);if(t)return{firstName:typeof e.first_name==`string`?e.first_name:void 0,id:t,isBot:e.is_bot===!0,languageCode:typeof e.language_code==`string`?e.language_code:void 0,lastName:typeof e.last_name==`string`?e.last_name:void 0,username:typeof e.username==`string`?e.username:void 0}}function parseAttachments(e){let t=[],n=parseLargestPhoto(e.photo);n!==null&&t.push(n);let r=parseDocument(e.document);return r!==null&&t.push(r),t}function parseLargestPhoto(e){if(!Array.isArray(e)||e.length===0)return null;let t=e.filter(isObject).map(e=>({fileId:typeof e.file_id==`string`?e.file_id:``,fileUniqueId:typeof e.file_unique_id==`string`?e.file_unique_id:void 0,height:typeof e.height==`number`?e.height:void 0,size:typeof e.file_size==`number`?e.file_size:void 0,width:typeof e.width==`number`?e.width:void 0})).filter(e=>e.fileId.length>0).sort((e,t)=>scorePhoto(t)-scorePhoto(e))[0];return t?{fileId:t.fileId,fileName:`photo.jpg`,fileUniqueId:t.fileUniqueId,height:t.height,kind:`photo`,mediaType:`image/jpeg`,size:t.size,width:t.width}:null}function parseDocument(e){return!isObject(e)||typeof e.file_id!=`string`?null:{fileId:e.file_id,fileName:typeof e.file_name==`string`?e.file_name:void 0,fileUniqueId:typeof e.file_unique_id==`string`?e.file_unique_id:void 0,kind:`document`,mediaType:typeof e.mime_type==`string`?e.mime_type:void 0,size:typeof e.file_size==`number`?e.file_size:void 0}}function scorePhoto(e){return e.size===void 0?(e.width??0)*(e.height??0):e.size}function parseTelegramChatType(e){return e===`channel`||e===`group`||e===`private`||e===`supergroup`?e:null}function numberLikeToString(e){if(typeof e==`string`&&e.length>0)return e;if(typeof e==`number`&&Number.isFinite(e))return String(e)}export{formatTelegramContextBlock,parseTelegramChatType,parseTelegramUpdate};