UNPKG

eve

Version:

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

1 lines • 2.38 kB
import{parseJsonValue}from"#shared/json.js";function workflowToolRunOutcomeToToolResult(e){let{from:t,result:n}=e;return n.status===`completed`?{callId:t.callId,kind:`tool-result`,output:n.output,toolName:t.toolName}:{callId:t.callId,isError:!0,kind:`tool-result`,output:n.status===`failed`?workflowToolRunFailureOutput(e):n.reason??`The workflow tool run was cancelled.`,toolName:t.toolName}}function workflowToolRunFailureOutput(e){if(e.result.status!==`failed`)throw TypeError(`Expected a failed workflow outcome.`);let t=parseJsonValueOrUndefined(e.result.error);return t!==void 0&&typeof t==`object`&&t&&!Array.isArray(t)&&typeof Reflect.get(t,`code`)==`string`?t:errorMessage(e.result.error)}function errorMessage(e){if(typeof e==`object`&&e&&`message`in e){let t=e.message;if(typeof t==`string`)return t}return String(e)}function parseJsonValueOrUndefined(e){try{return parseJsonValue(e)}catch{return}}function workflowToolRunRequestToInputRequestPayload(e){let{from:t,replyTo:n,requestCoordinates:r}=e;return{callId:t.callId,childContinuationToken:n,childSessionId:t.runId,event:{requests:workflowToolRunInputRequests(e),sequence:r?.sequence??t.sequence,stepIndex:r?.stepIndex??t.stepIndex,turnId:r?.turnId??t.turnId},kind:`subagent-input-request`,subagentName:t.toolName}}function workflowToolRunInputRequests(e){return e.request.kind===`input-batch`?e.request.requests:[normalizeInputRequest(e.request,e.from,e.replyTo)]}function normalizeInputRequest(e,t,n){switch(e.kind){case`agent-invoke`:case`agent-settled`:throw TypeError(`A workflow agent request cannot be normalized as human input.`);case`authorization-request`:throw TypeError(`A workflow authorization event cannot be normalized as human input.`);case`ask`:return normalizeAskRequest(e.request,t,n);default:return e}}function normalizeAskRequest(e,t,n){if(typeof e.prompt!=`string`||e.prompt.length===0)throw TypeError("A workflow tool run request needs a non-empty `prompt`.");let r={action:{callId:t.callId,input:t.input,kind:`tool-call`,toolName:t.toolName},kind:`question`,prompt:e.prompt,requestId:n};return e.allowFreeform!==void 0&&(r.allowFreeform=e.allowFreeform),e.display!==void 0&&(r.display=e.display),e.options!==void 0&&(r.options=[...e.options]),r}export{workflowToolRunFailureOutput,workflowToolRunInputRequests,workflowToolRunOutcomeToToolResult,workflowToolRunRequestToInputRequestPayload};