UNPKG

eve

Version:

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

1 lines 4.17 kB
import{invokeResultJsonSchema,parseInvokeResumeInput}from"./result.js";import{resolveInvokeOperation}from"./invoke.js";import{InvalidArgumentError}from"#compiled/commander/index.js";import{agentCommand}from"#cli/agent-command.js";import{parseDevelopmentHeaderOption,resolveDevelopmentUrlTarget}from"#cli/dev/url-target.js";import{parseDevelopmentServerUrl}from"#cli/dev/url.js";function registerRuntimeInvokeCommand(e){registerInvokeCommand({...e,deps:{loadEnvironment:async e=>await(await import(`#cli/dev/environment.js`)).loadDevelopmentEnvironmentFiles(e),runInvoke:async t=>await(e.runtime.runInvoke??(await import(`./invoke.js`)).runInvoke)(t),startHost:async t=>(e.runtime.startHost??(await import(`#internal/nitro/host.js`)).createDevelopmentServer)(t,{existing:`reject`})}})}function registerInvokeCommand(e){agentCommand(e.program.command(`invoke`),e.applicationContext,e=>{let t=e.opts();return t.url===void 0&&t.jsonSchema!==!0}).description(`Invoke an eve agent without a terminal UI.`).argument(`[prompt]`,`Prompt, follow-up message, or answer to a pending input`).option(`-u, --url <url>`,`Invoke an existing server URL`,parseDevelopmentServerUrl).option(`-H, --header <header>`,`Request header for a URL target, in "Name: value" form (repeatable)`,parseDevelopmentHeaderOption).option(`--resume`,`Read a previous resumable result from stdin`).option(`--scope <team>`,`Vercel team that owns the URL target`).option(`--json-schema`,`Print the invoke result JSON Schema and exit`).action((t,n)=>runInvokeCommand({...e,options:n,prompt:t}))}async function runInvokeCommand(e){let{options:t}=e;if(t.jsonSchema===!0){assertSchemaOnly(e.prompt,t),e.logger.log(JSON.stringify(invokeResultJsonSchema,null,2));return}let n=t.resume===!0?parseInvokeResumeInput(await readJsonFromStdin()):void 0,r=n?.resume.target;if(r?.kind===`local`&&t.url!==void 0)throw new InvalidArgumentError(`A local invocation cannot be resumed against --url.`);let i=t.url??(r?.kind===`remote`?r.serverUrl:void 0),a=resolveDevelopmentUrlTarget({header:t.header,url:i},void 0);if(t.scope!==void 0&&a===void 0)throw new InvalidArgumentError(`The --scope option requires a URL target.`);if(r?.kind===`remote`&&t.url!==void 0&&a!==void 0&&a.serverUrl!==r.serverUrl)throw new InvalidArgumentError(`Session target ${r.serverUrl} does not match ${a.serverUrl}.`);let o=resolveInvokeOperation({previous:n,prompt:e.prompt});if(await e.deps.loadEnvironment(e.applicationContext.root),a!==void 0){await executeWithSignals(e,{kind:`remote`,serverUrl:a.serverUrl,workspaceRoot:e.applicationContext.root},a.headers,o,t.scope);return}let s=await e.deps.startHost(e.applicationContext.root);try{let t=await s.start();await executeWithSignals(e,{kind:`local`,serverUrl:t.url,workspaceRoot:t.appRoot},void 0,o)}finally{await s.close()}}async function executeWithSignals(e,t,n,r,i){let a=new AbortController,o,handleSigint=()=>{o=130,a.abort()},handleSigterm=()=>{o=143,a.abort()};process.once(`SIGINT`,handleSigint),process.once(`SIGTERM`,handleSigterm);try{let s=n===void 0?{operation:r,signal:a.signal,target:t}:{headers:n,operation:r,signal:a.signal,target:t},c=i===void 0?s:{...s,vercelScope:i},l=await e.deps.runInvoke(c);e.logger.log(JSON.stringify(l,null,2)),process.exitCode=o??invokeExitCode(l)}finally{process.off(`SIGINT`,handleSigint),process.off(`SIGTERM`,handleSigterm)}}function assertSchemaOnly(e,t){if(e!==void 0||t.resume===!0||t.scope!==void 0||t.url!==void 0||t.header!==void 0)throw new InvalidArgumentError(`--json-schema cannot be combined with invoke options or a prompt.`)}async function readJsonFromStdin(){let e=``;process.stdin.setEncoding(`utf8`);for await(let t of process.stdin)e+=t;if(e.trim().length===0)throw new InvalidArgumentError(`--resume expected a resumable eve invoke result on stdin.`);try{return JSON.parse(e)}catch{throw new InvalidArgumentError(`--resume received invalid JSON on stdin.`)}}function invokeExitCode(e){return e.status===`failed`||e.status===`authentication-required`||e.status===`ready`&&e.outcome.status===`failed`?1:e.status===`input-required`||e.status===`authorization-required`?3:0}export{registerInvokeCommand,registerRuntimeInvokeCommand};