UNPKG

eve

Version:

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

1 lines 4.05 kB
import{findWorkflowToolRun,isInboxSubagentResultFromRecordedWorkflowToolRun,isInboxToolResultFromRecordedWorkflowToolRun}from"#harness/workflow-tool-runs.js";import{cancelAgentInvocationOwnerStep}from"#execution/tools/subagent/task-cancel.js";import{runProxySubagentEventStep}from"#subagents/event-proxy-step.js";import{applyTaskAgentRequest}from"#execution/tools/subagent/task-agent-requests.js";import{resumeHookStep}from"#execution/tools/workflow/resume-hook-step.js";import{workflowToolRunOutcomeToSubagentResult,workflowToolRunOutcomeToToolResult,workflowToolRunRequestToInputRequestPayload}from"#execution/tools/workflow/owner-inbox.js";import{resolveWorkflowCallbackBaseUrl}from"#execution/workflow-callback-url.js";import{releaseAgentInvocationOwnerStep}from"#execution/tools/subagent/invoke-step.js";import{emitWorkflowToolRunReportStep}from"#execution/tools/workflow/emit-workflow-tool-run-report-step.js";async function handleWorkflowToolRunMessage(e){let{message:t}=e;switch(t.kind){case`outcome`:return await handleWorkflowToolRunOutcome({...e,message:t});case`request`:await handleWorkflowToolRunRequest({...e,message:t});return;case`report`:await emitWorkflowToolRunReportStep({from:t.from,parentWritable:e.cursor.parentWritable,update:t.update});return}}async function handleWorkflowToolRunOutcome(e){let{cursor:t,message:n}=e,r=findWorkflowToolRun(t.sessionState.snapshot?.session.state,n.from.callId);if(r?.runId!==n.from.runId)return;let i=r.resultKind===`subagent`?await settleSubagentOutcome(e):workflowToolRunOutcomeToToolResult(n);await cancelAgentInvocationOwnerStep({ownerId:n.from.runId,serializedContext:t.serializedContext,sessionState:t.sessionState});let a=await releaseAgentInvocationOwnerStep({cancelled:n.result.status===`cancelled`,ownerId:n.from.runId,sessionState:t.sessionState});await t.adopt({serializedContext:t.serializedContext,sessionState:a.sessionState});let o=t.sessionState.snapshot?.session.state;return(i.kind===`subagent-result`?i.callId===n.from.callId&&isInboxSubagentResultFromRecordedWorkflowToolRun(o,i):isInboxToolResultFromRecordedWorkflowToolRun(o,i))?i:void 0}async function settleSubagentOutcome(e){let{cursor:t,message:n}=e,r=workflowToolRunOutcomeToSubagentResult(n);return r.origin===`child`&&await t.adopt(await applyTaskAgentRequest({accumulateUsage:!1,ownerId:n.from.runId,replyTo:n.from.runId,request:{kind:`agent-settled`,result:r}},requestContext(e))),r}async function handleWorkflowToolRunRequest(e){let{cursor:t,message:n}=e;if(n.request.kind===`agent-invoke`||n.request.kind===`agent-settled`){if(findWorkflowToolRun(t.sessionState.snapshot?.session.state,n.from.callId)?.runId!==n.from.runId){n.request.kind===`agent-invoke`&&await resumeHookStep(n.replyTo,{kind:`runtime-action-result`,results:[{callId:n.request.invocationId,isError:!0,kind:`subagent-result`,origin:`dispatch`,output:{code:`AGENT_INVOCATION_NOT_ADMITTED`,message:`The workflow tool run no longer owns this agent invocation.`},subagentName:n.request.input.target}]});return}await t.adopt(await applyTaskAgentRequest({accumulateUsage:n.from.resultKind!==`subagent`,ownerId:n.from.runId,replyTo:n.replyTo,request:n.request},requestContext(e)));return}if(n.request.kind===`authorization-request`){await t.adopt(await runProxySubagentEventStep({hookPayload:n.request.event,parentWritable:t.parentWritable,serializedContext:t.serializedContext,sessionState:t.sessionState})),n.request.event.childSessionId===n.from.runId&&await resumeHookStep(n.replyTo,null,{ifPresent:!0});return}await t.adopt(await runProxySubagentEventStep({...n.requestCoordinates===void 0?{answerHook:{runId:n.from.runId}}:{},hookPayload:workflowToolRunRequestToInputRequestPayload(n),parentWritable:t.parentWritable,serializedContext:t.serializedContext,sessionState:t.sessionState}))}function requestContext(e){return{callbackBaseUrl:resolveWorkflowCallbackBaseUrl(e.callbackMetadataUrl),parentWritable:e.cursor.parentWritable,serializedContext:e.cursor.serializedContext,sessionState:e.cursor.sessionState}}export{handleWorkflowToolRunMessage};