eve
Version:
Filesystem-first framework for durable backend AI agents that run anywhere.
3 lines • 3.94 kB
JavaScript
import{cardChildToFallbackText}from"#compiled/chat/index.js";import{truncatePlainText}from"#public/channels/slack/limits.js";function cardToBlocks(e){let n=[],r={usedNativeTable:!1};e.title&&n.push({type:`header`,text:{type:`plain_text`,text:truncatePlainText(e.title)??e.title,emoji:!0}}),e.subtitle&&n.push({type:`context`,elements:[{type:`mrkdwn`,text:e.subtitle}]}),e.imageUrl&&n.push({type:`image`,image_url:e.imageUrl,alt_text:e.title??``});for(let t of e.children)appendChildBlocks(t,n,r);return n}function cardToFallbackText(t){let n=[];t.title&&n.push(t.title),t.subtitle&&n.push(t.subtitle);for(let r of t.children){let t=cardChildToFallbackText(r);t&&t.length>0&&n.push(t)}return n.join(`
`).trim()}function appendChildBlocks(t,n,r){switch(t.type){case`text`:n.push(textToBlock(t));return;case`image`:n.push(imageToBlock(t));return;case`divider`:n.push(dividerToBlock(t));return;case`actions`:n.push(actionsToBlock(t));return;case`section`:for(let e of t.children)appendChildBlocks(e,n,r);return;case`fields`:n.push(fieldsToBlock(t));return;case`link`:n.push(linkToBlock(t));return;case`table`:n.push(...tableToBlocks(t,r));return;default:{let r=cardChildToFallbackText(t);r&&n.push({type:`section`,text:{type:`mrkdwn`,text:r}})}}}function textToBlock(e){return e.style===`muted`?{type:`context`,elements:[{type:`mrkdwn`,text:e.content}]}:{type:`section`,text:{type:`mrkdwn`,text:e.style===`bold`?`*${e.content}*`:e.content}}}function imageToBlock(e){return{type:`image`,image_url:e.url,alt_text:e.alt??``}}function dividerToBlock(e){return{type:`divider`}}function actionsToBlock(e){let t=[];for(let n of e.children){let e=actionChildToElement(n);e&&t.push(e)}return{type:`actions`,elements:t}}function actionChildToElement(e){switch(e.type){case`button`:return buttonToElement(e);case`link-button`:return linkButtonToElement(e);case`select`:return selectToElement(e);case`radio_select`:return radioSelectToElement(e);default:return null}}function buttonToElement(e){let n={type:`button`,action_id:e.id,text:{type:`plain_text`,text:truncatePlainText(e.label)??e.label}};return e.value!==void 0&&(n.value=e.value),e.style&&e.style!=="default"&&(n.style=e.style),e.disabled===!0&&(n.disabled=!0),e.callbackUrl&&(n.url=e.callbackUrl),n}function linkButtonToElement(e){let n={type:`button`,action_id:`link:${e.url}`,text:{type:`plain_text`,text:truncatePlainText(e.label)??e.label},url:e.url};return e.style&&e.style!=="default"&&(n.style=e.style),n}function selectToElement(e){return{type:`static_select`,action_id:e.id,placeholder:{type:`plain_text`,text:e.placeholder??e.label},options:e.options.map(e=>({text:{type:`plain_text`,text:e.label},value:e.value,...e.description?{description:{type:`plain_text`,text:e.description}}:{}}))}}function radioSelectToElement(e){return{type:`radio_buttons`,action_id:e.id,options:e.options.map(e=>({text:{type:`plain_text`,text:e.label},value:e.value,...e.description?{description:{type:`plain_text`,text:e.description}}:{}}))}}function fieldsToBlock(e){return{type:`section`,fields:e.children.map(e=>({type:`mrkdwn`,text:`*${e.label}*\n${e.value}`}))}}function linkToBlock(e){return{type:`section`,text:{type:`mrkdwn`,text:`<${e.url}|${e.label}>`}}}function tableToBlocks(e,t){return!t.usedNativeTable&&e.rows.length<=100&&e.headers.length<=20?(t.usedNativeTable=!0,[{type:`table`,rows:[e.headers.map(tableCellToRawText),...e.rows.map(e=>e.map(tableCellToRawText))]}]):[tableToFallbackBlock(e)]}function tableCellToRawText(e){return{type:`raw_text`,text:e||` `}}function tableToFallbackBlock(e){let t=e.headers.map((t,n)=>{let r=t.length;for(let t of e.rows){let e=t[n]??``;e.length>r&&(r=e.length)}return r});return{type:`section`,text:{type:`mrkdwn`,text:["```",e.headers.map((e,n)=>e.padEnd(t[n]??0)).join(` | `),t.map(e=>`-`.repeat(e)).join(`-|-`),...e.rows.map(n=>e.headers.map((e,r)=>(n[r]??``).padEnd(t[r]??0)).join(` | `)),"```"].join(`
`)}}}export{cardToBlocks,cardToFallbackText};