UNPKG

eve

Version:

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

1 lines 7.65 kB
import{parseJsonObject}from"#shared/json.js";import{createActionResultEvent}from"#protocol/message.js";import{findBlockingWorkflowToolRun,removeBlockingWorkflowToolRuns}from"#harness/workflow-tool-runs.js";import{clearProxyInputRequestsForChild,clearProxyInputRequestsWhere}from"#harness/proxy-input-requests.js";import{findRunningAgentHandle,isResultBoundToRunningHandle}from"#subagents/handles/query.js";import{accumulateSessionUsage,getTurnUsageState,setTurnUsageState}from"#harness/turn-tag-state.js";import{resolveRuntimeActionResultsForCallIds}from"#runtime/actions/results.js";import{settleAgentTurn}from"#subagents/handles/transitions.js";import{markRuntimeWorkflowToolAction}from"#shared/action-types.js";import{normalizeToolModelOutput}from"#harness/tool-model-output.js";const PENDING_COORDINATION_BATCH_KEY=`eve.runtime.pendingCoordinationBatch`;function readSubagentResultOutcome(e){return e.origin===`child`?e.outcome:void 0}function getPendingCoordinationBatch(e){let t=e?.[PENDING_COORDINATION_BATCH_KEY];if(typeof t!=`object`||!t)return;let n=t;if(Array.isArray(n.runtimeActions)&&Array.isArray(n.tasks)&&Array.isArray(n.responseMessages)&&typeof n.event==`object`&&n.event!==null)return n}function hasPendingCoordinationBatch(e){return getPendingCoordinationBatch(e)!==void 0}function clearPendingCoordinationBatch(e){if(e.state?.[PENDING_COORDINATION_BATCH_KEY]===void 0)return e;let t={...e.state};return delete t[PENDING_COORDINATION_BATCH_KEY],{...e,state:Object.keys(t).length>0?t:void 0}}function setPendingCoordinationBatch(e){assertUniqueCoordinationCallIds([...e.runtimeActions,...e.tasks]);let t={...e.session.state};return t[PENDING_COORDINATION_BATCH_KEY]={runtimeActions:[...e.runtimeActions],tasks:[...e.tasks],event:e.event,localFanoutSize:e.localFanoutSize,responseMessages:[...e.responseMessages]},{...e.session,state:t}}function assertUniqueCoordinationCallIds(e){let t=new Set;for(let n of e){if(t.has(n.callId))throw Error(`Coordination batch contains duplicate callId "${n.callId}".`);t.add(n.callId)}}function resolveReadyCoordinationResults(e){let t=getPendingCoordinationBatch(e.session.state);if(t!==void 0)return resolveResultsForCoordinationBatch({batch:t,results:e.results,state:e.session.state})}function resolveResultsForCoordinationBatch(e){return resolveRuntimeActionResultsForCallIds({pendingCallIds:[...e.batch.runtimeActions,...e.batch.tasks].map(e=>e.callId),results:e.results.filter(t=>isResultBoundToRunningHandle(e.state,t))})}async function resolvePendingCoordination(e){let t=getPendingCoordinationBatch(e.session.state);if(t===void 0)return{messages:[...e.session.history],outcome:`continue`,session:e.session};let n=resolveReadyCoordinationResults({results:e.stepInput?.runtimeActionResults??[],session:e.session});if(n===void 0)return{messages:[...e.session.history],outcome:`unresolved`,session:e.session};let r=e.session;for(let e of n){if(e.kind!==`subagent-result`||e.origin!==`child`||readBackgroundTaskReceipt(e)!==void 0)continue;let t=findRunningAgentHandle(r.state,{callId:e.callId});if(t===void 0)continue;let n=readSubagentResultOutcome(e);if(n===void 0)continue;n.kind===`terminal`&&`continuationToken`in t.address&&(r=clearProxyInputRequestsForChild(r,t.address.continuationToken));let i=settleAgentTurn(r,{operationId:t.operation.id,outcome:n});i.kind===`settled`&&(r=i.session)}for(let e of n){if(e.kind!==`tool-result`)continue;let n=findBlockingWorkflowToolRun(r.state,e.callId,t.event.turnId);n!==void 0&&(r=removeBlockingWorkflowToolRuns(clearProxyInputRequestsWhere(r,e=>e.answerHook?.runId===n.address.runId),t.event.turnId,n.callId))}let i={...r.state};delete i[PENDING_COORDINATION_BATCH_KEY],r={...r,state:Object.keys(i).length>0?i:void 0};for(let e of n){if(e.kind!==`subagent-result`)continue;let t=readSubagentResultOutcome(e);t!==void 0&&(r=setTurnUsageState(r,accumulateSessionUsage({previous:getTurnUsageState(r.state),usage:t.usageDelta})))}if(e.emit!==void 0)for(let r of n){if(r.kind===`subagent-result`&&r.origin===`child`&&r.outcome.result.kind===`succeeded`&&readBackgroundTaskReceipt(r)===void 0){let t={callId:r.callId,output:typeof r.output==`string`?r.output:JSON.stringify(r.output),subagentName:r.subagentName};await e.emit({data:t,type:`subagent.completed`})}await e.emit(createActionResultEvent({result:r,sequence:t.event.sequence,stepIndex:t.event.stepIndex,turnId:t.event.turnId}))}let a=[];for(let t of n){switch(t.kind){case`load-skill-result`:a.push({output:toToolResultOutput(t),toolCallId:t.callId,toolName:`load_skill`,type:`tool-result`});continue;case`subagent-result`:a.push({output:toToolResultOutput(t),toolCallId:t.callId,toolName:t.subagentName,type:`tool-result`});continue;case`tool-result`:a.push({output:await projectToolResultOutput(t,e.tools?.get(t.toolName)),toolCallId:t.callId,toolName:t.toolName,type:`tool-result`});continue}throw Error(`Unsupported runtime action result kind "${String(t)}".`)}let o=[...r.history,...t.responseMessages];return a.length>0&&o.push({content:a,role:`tool`}),{messages:o,outcome:`resolved`,session:r}}function readBackgroundTaskReceipt(e){return`backgroundTask`in e?e.backgroundTask:void 0}function createRuntimeActionRequestFromToolCall(e){let t=e.tools.get(e.toolCall.toolName),n=resolveToolCallInputObject(e.toolCall.input,{callId:e.toolCall.toolCallId,toolName:e.toolCall.toolName});if(t?.frameworkAction===`load-skill`)return{callId:e.toolCall.toolCallId,input:n,kind:`load-skill`};let r={callId:e.toolCall.toolCallId,input:n,kind:`tool-call`,toolName:e.toolCall.toolName};return t?.workflowId===void 0?r:markRuntimeWorkflowToolAction(r)}function createCoordinationRequestFromToolCall(e){let t=e.tools.get(e.toolCall.toolName),n=resolveToolCallInputObject(e.toolCall.input,{callId:e.toolCall.toolCallId,toolName:e.toolCall.toolName});if(t?.runtimeAction?.kind===`task-control`)return{kind:`runtime-action`,request:{callId:e.toolCall.toolCallId,input:n,kind:`tool-call`,toolName:e.toolCall.toolName}};if(t?.workflowId!==void 0)return{kind:`task`,request:{callId:e.toolCall.toolCallId,executeInput:t.executeInput?.(n),input:n,kind:`workflow-task`,toolName:e.toolCall.toolName,workflowId:t.workflowId}};throw Error(`Deferred tool "${e.toolCall.toolName}" has no task or runtime action.`)}function resolveToolCallInputObject(e,t){if(e==null||typeof e==`string`&&e.trim()===``)return{};try{return parseJsonObject(typeof e==`string`?parseJsonStringInput(e):e)}catch(e){let n=e instanceof Error?e.message:String(e);throw TypeError(`Failed to parse tool-call arguments for "${t.toolName}" (${t.callId}): ${n}`,{cause:e})}}function parseJsonStringInput(e){return JSON.parse(e)}async function projectToolResultOutput(e,t){return e.isError===!0||t?.toModelOutput===void 0?toToolResultOutput(e):normalizeToolModelOutput({output:await t.toModelOutput(e.output),toolCallId:e.callId,toolName:e.toolName})}function toToolResultOutput(e){return typeof e.output==`string`?e.isError===!0?{type:`error-text`,value:e.output}:{type:`text`,value:e.output}:e.isError===!0?{type:`error-json`,value:toMutableJsonValue(e.output)}:{type:`json`,value:toMutableJsonValue(e.output)}}function toMutableJsonValue(e){if(e===null||typeof e==`string`||typeof e==`number`||typeof e==`boolean`)return e;if(Array.isArray(e))return e.map(e=>toMutableJsonValue(e));let t={};for(let[n,r]of Object.entries(e))t[n]=toMutableJsonValue(r);return t}export{assertUniqueCoordinationCallIds,clearPendingCoordinationBatch,createCoordinationRequestFromToolCall,createRuntimeActionRequestFromToolCall,getPendingCoordinationBatch,hasPendingCoordinationBatch,resolvePendingCoordination,resolveToolCallInputObject,setPendingCoordinationBatch};