UNPKG

eve

Version:

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

1 lines 1.77 kB
import{createLogger,logError}from"#internal/logging.js";import{renderInputRequestPostParts}from"#public/channels/slack/hitl.js";const log=createLogger(`slack.private-approval-delivery`);async function deliverPrivateInputRequest(e){let t=await e.slack.request(`conversations.open`,{users:e.reviewer}),n=t.ok===!0?t.channel?.id:void 0;if(typeof n!=`string`||n.length===0)throw Error(`Slack conversations.open failed: ${t.error??`unknown_error`}`);let r={channelId:e.slack.channelId,threadTs:e.slack.threadTs},i=renderInputRequestPostParts(e.request,r),a=[];try{let t=await resolveMessagePermalink(e.slack,e.previewMessageTs);t!==void 0&&a.push(await postMessage(e.slack,{channel:n,markdown_text:t,unfurl_links:!0,unfurl_media:!1})),i.details!==void 0&&a.push(await postMessage(e.slack,{blocks:i.details.blocks,channel:n,text:i.details.text,unfurl_links:!1,unfurl_media:!1}));let r=await postMessage(e.slack,{blocks:i.controls.blocks,channel:n,text:i.controls.text,unfurl_links:!1,unfurl_media:!1});return a.push(r),{messageBlocks:i.controls.blocks,messageChannelId:n,messageTs:r}}catch(t){throw await Promise.allSettled(a.map(async t=>{try{await e.slack.request(`chat.delete`,{channel:n,ts:t})}catch(e){logError(log,`failed to roll back partial private approval delivery`,e,{channelId:n,messageTs:t})}})),t}}async function resolveMessagePermalink(e,t){if(!e.channelId||!t)return;let n=await e.request(`chat.getPermalink`,{channel:e.channelId,message_ts:t});return n.ok===!0&&typeof n.permalink==`string`?n.permalink:void 0}async function postMessage(e,t){let n=await e.request(`chat.postMessage`,t);if(n.ok!==!0||typeof n.ts!=`string`||n.ts.length===0)throw Error(`Slack chat.postMessage failed: ${n.error??`unknown_error`}`);return n.ts}export{deliverPrivateInputRequest};