eve
Version:
Filesystem-first framework for durable backend AI agents that run anywhere.
1 lines • 3.25 kB
JavaScript
import{resolveSlackBotToken}from"#public/channels/slack/api.js";import{SLACK_CARD_SUBTEXT_MAX_LENGTH,truncateCardSubtext}from"#public/channels/slack/limits.js";import{buildAnsweredBlocks,isHitlAction}from"#public/channels/slack/hitl.js";function buildAnsweredHitlMessageBlocks(e){let t=e.messageBlocks.findIndex(t=>blockContainsActionId(t,e.actionId));if(t===-1)return buildAnsweredBlocks({promptBlocks:findPromptBlocks(e.messageBlocks),answerLabel:e.answerLabel,userId:e.userId});let n=e.messageBlocks[t],r=answeredBlocksFromActionBlock({answerLabel:e.answerLabel,block:n,userId:e.userId})??buildAnsweredBlocks({promptBlocks:promptBlocksFromActionBlock(n),answerLabel:e.answerLabel,userId:e.userId});return[...e.messageBlocks.slice(0,t),...r,...e.messageBlocks.slice(t+1)]}function findPromptBlocks(e){let t=[];for(let n of e){if(!isObjectRecord(n))continue;let e=n.type;if(e===`actions`)break;(e===`section`||e===`context`||e===`divider`||e===`image`)&&t.push(n)}return t}function blockContainsActionId(e,t){return isObjectRecord(e)?actionsContainActionId(e.elements,t)||actionsContainActionId(e.actions,t):!1}function actionsContainActionId(e,t){return Array.isArray(e)?e.some(e=>isObjectRecord(e)&&e.action_id===t):!1}function answeredBlocksFromActionBlock(e){if(!isObjectRecord(e.block)||e.block.type!==`card`)return;let{actions:t,subtext:n,...r}=e.block,i={...r,subtext:{type:`mrkdwn`,text:formatAnsweredCardSubtext(e),verbatim:!1}};return hasCardContent(i)?[i]:void 0}function formatAnsweredCardSubtext(e){let t=e.userId.length>0?` by <@${e.userId}>`:``,n=SLACK_CARD_SUBTEXT_MAX_LENGTH-20-1-t.length,r=truncateWithEllipsis(e.answerLabel,n);return truncateCardSubtext(`:white_check_mark: *${r}*${t}`)}function truncateWithEllipsis(e,t){if(t<=0)return``;if(e.length<=t)return e;let n=Math.max(0,t-3);return`${e.slice(0,n).trimEnd()}...`}function promptBlocksFromActionBlock(e){if(!isObjectRecord(e)||e.type!==`card`)return[];let{actions:t,...n}=e;return hasCardContent(n)?[n]:[]}function hasCardContent(e){return e.body!==void 0||e.title!==void 0||e.hero_image!==void 0}function isObjectRecord(e){return typeof e==`object`&&!!e}async function updateAnsweredHitlCard(e,t){let n=e.actions.find(e=>isHitlAction(e.actionId));if(!n?.messageTs)return;let r=n.label??n.selectedOptionValue??n.value;r&&await updateAnsweredCard({answerLabel:r,blocks:buildAnsweredHitlMessageBlocks({actionId:n.actionId,answerLabel:r,messageBlocks:e.messageBlocks,userId:n.user.id}),channelId:e.channelId,deps:t,installationTeamId:e.installationTeamId,messageTs:n.messageTs})}async function updateAnsweredFreeformCard(e){await updateAnsweredCard({...e,blocks:buildAnsweredBlocks({promptBlocks:[],answerLabel:e.answerLabel,userId:e.userId})})}async function updateAnsweredCard(e){let t=await resolveSlackBotToken(e.deps.config.credentials?.botToken,{teamId:e.installationTeamId}),n=await fetch(`https://slack.com/api/chat.update`,{method:`POST`,headers:{authorization:`Bearer ${t}`,"content-type":`application/json; charset=utf-8`},body:JSON.stringify({channel:e.channelId,ts:e.messageTs,blocks:e.blocks,text:`Answered: ${e.answerLabel}`})});if(!n.ok)throw Error(`Slack chat.update returned HTTP ${n.status}`)}export{updateAnsweredFreeformCard,updateAnsweredHitlCard};