eve
Version:
Filesystem-first framework for durable backend AI agents that run anywhere.
1 lines • 3.77 kB
JavaScript
import{createEveCallbackRoutePath,createEveCancelTurnRoutePath}from"#protocol/routes.js";import{EVE_SESSION_ID_HEADER}from"#protocol/message.js";import{CancelTurnResponseSchema}from"#protocol/cancel-turn.js";import{createWorkflowCallbackUrl}from"#execution/workflow-callback-url.js";import{formatSubagentInput}from"#execution/subagent-invocation.js";var RemoteAgentCancelRequestError=class extends Error{retryable;constructor(e,t){super(e),this.name=`RemoteAgentCancelRequestError`,this.retryable=t.retryable}};async function startRemoteAgentSession(t){let r=t.callbackToken??t.session.continuationToken;if(!r)throw Error(`Cannot dispatch remote agent without a parent continuation token.`);if(!t.callbackBaseUrl)throw Error(`Cannot dispatch remote agent without a callback base URL.`);let a=await resolveRemoteAgentRequestHeaders(t.remote),o=await fetch(createRemoteAgentSessionUrl(t.remote),{body:JSON.stringify({callback:{callId:t.action.callId,subagentName:t.action.remoteAgentName,token:r,url:createWorkflowCallbackUrl(t.callbackBaseUrl,createEveCallbackRoutePath(r))},message:formatRemoteAgentCallInputMessage({action:t.action,remote:t.remote}),mode:`task`,outputSchema:t.action.input.outputSchema??t.remote.outputSchema}),headers:{"content-type":`application/json`,...a},method:`POST`});if(!o.ok)throw Error(`Remote agent "${t.action.remoteAgentName}" create-session request failed with HTTP ${o.status}.`);let s=o.headers.get(EVE_SESSION_ID_HEADER);if(s!==null&&s.length>0)return s;try{let e=await o.json();if(typeof e.sessionId==`string`&&e.sessionId.length>0)return e.sessionId}catch{}throw Error(`Remote agent "${t.action.remoteAgentName}" create-session response did not include a session id.`)}async function cancelRemoteAgentTurn(e){let t=await resolveRemoteAgentRequestHeaders(e.remote),n=await fetch(createRemoteAgentCancelTurnUrl(e.remote,e.sessionId),{headers:t,method:`POST`});if(!n.ok)throw new RemoteAgentCancelRequestError(`Remote agent "${e.remote.name}" cancel-turn request failed with HTTP ${n.status}.`,{retryable:isRetryableRemoteCancelStatus(n.status)});let i;try{i=await n.json()}catch{throw new RemoteAgentCancelRequestError(`Remote agent "${e.remote.name}" cancel-turn response was not valid JSON.`,{retryable:!1})}let a=CancelTurnResponseSchema.safeParse(i);if(!a.success||a.data.sessionId!==e.sessionId)throw new RemoteAgentCancelRequestError(`Remote agent "${e.remote.name}" cancel-turn response was invalid.`,{retryable:!1});return{status:a.data.status}}function isRetryableRemoteAgentCancelError(e){return!(e instanceof RemoteAgentCancelRequestError)||e.retryable}function resolveRemoteAgentForAction(e){let t=e.registry.get(e.nodeId)?.definition;if(t?.kind!==`remote`)throw Error(`Missing remote agent "${e.remoteAgentName}" in runtime registry.`);return t}function createRemoteAgentSessionUrl(e){return new URL(e.path,`${trimTrailingSlash(e.url)}/`).toString()}function createRemoteAgentCancelTurnUrl(e,n){return new URL(createEveCancelTurnRoutePath(n),`${trimTrailingSlash(e.url)}/`).toString()}function isRetryableRemoteCancelStatus(e){return e===408||e===425||e===429||e>=500}async function resolveRemoteAgentRequestHeaders(e){let t={};return e.headers!==void 0&&Object.assign(t,typeof e.headers==`function`?await e.headers():e.headers),e.auth!==void 0&&Object.assign(t,(await e.auth()).headers),t}function formatRemoteAgentCallInputMessage(e){let t=typeof e.action.input.message==`string`?e.action.input.message:``;return formatSubagentInput({description:e.remote.description,message:t,name:e.action.remoteAgentName,type:`remote`}).message}function trimTrailingSlash(e){return e.endsWith(`/`)?e.slice(0,-1):e}export{cancelRemoteAgentTurn,isRetryableRemoteAgentCancelError,resolveRemoteAgentForAction,startRemoteAgentSession};