eve
Version:
Filesystem-first framework for durable backend AI agents that run anywhere.
1 lines • 3.44 kB
JavaScript
import{truncateModalTitle,truncatePlainText,truncateSectionText}from"#public/channels/slack/limits.js";const HITL_ACTION_PREFIX=`eve_input:`,HITL_FREEFORM_ACTION_PREFIX=`eve_input_freeform:`,HITL_FREEFORM_MODAL_CALLBACK_ID=`eve_input_freeform_submit`,HITL_FREEFORM_MODAL_BLOCK_ID=`eve_freeform_block`,HITL_FREEFORM_MODAL_ACTION_ID=`eve_freeform_text`,BUTTON_ACTION_ID_RE=/^(?<requestId>.+):button:\d+$/u;function deriveHitlResponse(e){if(!e.actionId.startsWith(`eve_input:`))return null;let t=e.actionId.slice(10);if(e.selectedOptionValue!==void 0)return t?{optionId:e.selectedOptionValue,requestId:t}:null;if(e.value!==void 0){let n=BUTTON_ACTION_ID_RE.exec(t)?.groups?.requestId;return n?{optionId:e.value,requestId:n}:null}return null}function isHitlAction(e){return e.startsWith(HITL_ACTION_PREFIX)}function renderInputRequestBlocks(e){let t={text:{text:truncateSectionText(e.prompt),type:`mrkdwn`},type:`section`},a=`${HITL_ACTION_PREFIX}${e.requestId}`,o=e.options,s=e.allowFreeform===!0||!o||o.length===0;return o&&o.length>0&&e.display===`select`?[t,{type:`actions`,elements:[o.length<=6?{type:`radio_buttons`,action_id:a,options:o.map(buildOption)}:{type:`static_select`,action_id:a,options:o.map(buildOption),placeholder:{type:`plain_text`,text:`Choose an option`}}]}]:o&&o.length>0?[t,{type:`actions`,elements:o.map((e,t)=>buildButton(e,a,t))}]:s?[t,{type:`actions`,elements:[{type:`button`,action_id:`${HITL_FREEFORM_ACTION_PREFIX}${e.requestId}`,text:{type:`plain_text`,text:`Type your answer`},style:`primary`,value:e.requestId}]}]:[t]}function buildFreeformModalView(t){let r=t.prompt?truncateModalTitle(t.prompt):`Your answer`,i=t.prompt?[{type:`section`,text:{type:`mrkdwn`,text:truncateSectionText(t.prompt)}}]:[];return{type:`modal`,callback_id:HITL_FREEFORM_MODAL_CALLBACK_ID,private_metadata:JSON.stringify(t.metadata),title:{type:`plain_text`,text:r},submit:{type:`plain_text`,text:`Submit`},close:{type:`plain_text`,text:`Cancel`},blocks:[...i,{type:`input`,block_id:HITL_FREEFORM_MODAL_BLOCK_ID,element:{type:`plain_text_input`,action_id:HITL_FREEFORM_MODAL_ACTION_ID,multiline:!0,placeholder:{type:`plain_text`,text:`Type your answer here...`}},label:{type:`plain_text`,text:`Answer`}}]}}function isFreeformAction(e){return e.startsWith(HITL_FREEFORM_ACTION_PREFIX)}function freeformRequestIdFromActionId(e){if(!isFreeformAction(e))return;let t=e.slice(19);return t.length>0?t:void 0}function buildButton(e,n,r){let i={action_id:`${n}:button:${r}`,text:{text:truncatePlainText(e.label),type:`plain_text`},type:`button`,value:e.id};return(e.style===`primary`||e.style===`danger`)&&(i.style=e.style),i}function buildOption(e){let n={text:{text:truncatePlainText(e.label),type:`plain_text`},value:e.id},r=truncatePlainText(e.description);return r&&r.length>0&&(n.description={text:r,type:`plain_text`}),n}function buildAnsweredBlocks(e){let t=[];return e.promptBlock!==void 0&&e.promptBlock!==null&&t.push(e.promptBlock),t.push({type:`section`,text:{type:`mrkdwn`,text:`:white_check_mark: *${e.answerLabel}*`}}),e.userId&&e.userId.length>0&&t.push({type:`context`,elements:[{type:`mrkdwn`,text:`Answered by <@${e.userId}>`}]}),t}export{HITL_ACTION_PREFIX,HITL_FREEFORM_ACTION_PREFIX,HITL_FREEFORM_MODAL_ACTION_ID,HITL_FREEFORM_MODAL_BLOCK_ID,HITL_FREEFORM_MODAL_CALLBACK_ID,buildAnsweredBlocks,buildFreeformModalView,deriveHitlResponse,freeformRequestIdFromActionId,isFreeformAction,isHitlAction,renderInputRequestBlocks};