UNPKG

eve

Version:

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

1 lines 6.45 kB
import{assertFullSha}from"../identifiers.js";import{readTerminalHeadlessEvent}from"./eve-add.js";import{gitOutput,runGitCommand}from"../git.js";import{shellQuote}from"#shared/shell-quote.js";const OFFICIAL_ADDRESS=/^(?:(?:channel|connection|extension|instrumentation)\/)?[a-z0-9][a-z0-9._-]*$/u;function assertOfficialRegistryAddress(e){if(!OFFICIAL_ADDRESS.test(e))throw Error(`Production registry installation requires an exact official eve registry address.`)}async function installProductionRegistryItem(e){assertOfficialRegistryAddress(e.address);let t=e.workspace.directory===`.`?e.workspace.repositoryPath:`${e.workspace.repositoryPath}/${e.workspace.directory}`,n=await locatePackageManagerRoot(e.sandbox,t,e.workspace.repositoryPath,e.signal);if(n===void 0)return{kind:`failed`,message:`The proposal checkout has no supported lockfile.`};if(!await runChecked(e.sandbox,`mkdir -p ${quote(`${e.workspace.repositoryPath}/.git/info`)} && printf '\\nnode_modules/\\n**/node_modules/\\n' >> ${quote(`${e.workspace.repositoryPath}/.git/info/exclude`)}`,e.signal)||!await runChecked(e.sandbox,bootstrapCommand(n),e.signal))return{kind:`failed`,message:`Could not prepare the proposal checkout's locked dependencies.`};let r=await locateEveExecutable(e.sandbox,t,n.root,e.signal);if(r===void 0)return{kind:`failed`,message:`The proposal checkout's locked dependencies do not provide the eve CLI.`};let i=await captureTree(e.sandbox,e.workspace.repositoryPath);if(e.answers!==void 0){if(e.installed!==!0)return{kind:`failed`,message:`Setup answers require a prior installed input-required result.`};let n;try{n=await e.sandbox.run({abortSignal:e.signal,command:isolatedCommand(t,`${quote(r)} add ${quote(e.address)} --non-interactive --skip-install`)})}finally{await restoreTree(e.sandbox,e.workspace.repositoryPath,i)}let a=readTerminalHeadlessEvent(`${String(n.stdout??``)}\n${String(n.stderr??``)}`);if(a?.type!==`blocked`||a.item!==e.address)return{kind:`failed`,message:`Setup continuation is no longer waiting for a validated question.`};let o=setupQuestion(a.question);if(o===void 0)return{kind:`failed`,message:`Registry setup returned an invalid continuation question.`};if(o.kind===`environment`)return{kind:`input-required`,installed:!0,question:a.question};if(!acceptsNonSecretSetupAnswers(o,e.answers))return{kind:`failed`,message:`Setup answers must contain only the current non-secret question.`}}let a=Object.entries(e.answers??{}).flatMap(([e,t])=>[`--answer`,`${e}=${JSON.stringify(t)}`]);try{let n=`${quote(r)} add ${quote(e.address)} --non-interactive ${e.installed===!0?`--skip-install `:``}${a.map(quote).join(` `)}`,o=await e.sandbox.run({abortSignal:e.signal,command:isolatedCommand(t,n)}),s=readTerminalHeadlessEvent(`${String(o.stdout??``)}\n${String(o.stderr??``)}`);if(s?.type===`completed`&&s.item===e.address&&o.exitCode===0){let t=await captureTree(e.sandbox,e.workspace.repositoryPath);return{kind:`completed`,changedPaths:await changedPaths(e.sandbox,e.workspace.repositoryPath,i,t),completedItems:s.completedItems?.filter(e=>OFFICIAL_ADDRESS.test(e))??[],deploymentRequired:s.deploymentRequired===!0}}if(s?.type===`blocked`&&s.item===e.address)return{kind:`input-required`,installed:s.installed===!0,question:s.question??s};if(s?.type===`external_action`&&typeof s.url==`string`&&typeof s.message==`string`){let t={kind:`external-action-required`,installed:e.installed===!0,message:s.message,url:s.url};return typeof s.userCode==`string`&&(t.userCode=s.userCode),t}return await restoreTree(e.sandbox,e.workspace.repositoryPath,i),s?.type===`cancelled`||e.signal?.aborted===!0?{kind:`cancelled`,message:`Installing ${e.address} was cancelled.`}:{kind:`failed`,message:`Installing ${e.address} failed.`}}catch(t){throw await restoreTree(e.sandbox,e.workspace.repositoryPath,i),t}}function acceptsNonSecretSetupAnswers(e,t){return e.kind!==`environment`&&Object.keys(t).length===1&&t[e.key]!==void 0}function setupQuestion(e){if(typeof e!=`object`||!e||Array.isArray(e))return;let t=e;if(typeof t.key==`string`&&t.key.length!==0&&typeof t.kind==`string`)return{key:t.key,kind:t.sensitive===!0?`environment`:t.kind}}async function locatePackageManagerRoot(e,t,n,r){let i=await e.run({abortSignal:r,command:`root=${quote(t)}; repository=${quote(n)}; while :; do if test -f "$root/pnpm-lock.yaml"; then printf 'pnpm\\t%s' "$root"; exit 0; fi; if test -f "$root/package-lock.json"; then printf 'npm\\t%s' "$root"; exit 0; fi; test "$root" = "$repository" && exit 0; root=\${root%/*}; done`});if(i.exitCode!==0)throw Error(`Could not inspect the proposal package workspace.`);let a=/^(pnpm|npm)\t([^\n]+)$/u.exec(String(i.stdout??``));if(a===null)return;let o=a[2];if(o!==n&&!o.startsWith(`${n}/`))throw Error(`Package manager root escaped the proposal repository.`);return{kind:a[1],root:o}}function bootstrapCommand(e){return isolatedCommand(e.root,e.kind===`pnpm`?`corepack pnpm --dir ${quote(e.root)} install --frozen-lockfile --ignore-scripts`:`npm ci --prefix ${quote(e.root)} --ignore-scripts`)}async function locateEveExecutable(e,t,n,r){let i=[`${t}/node_modules/.bin/eve`,`${n}/node_modules/.bin/eve`],a=await e.run({abortSignal:r,command:`for executable in ${i.map(quote).join(` `)}; do if test -x "$executable"; then printf '%s' "$executable"; exit 0; fi; done`});if(a.exitCode!==0)throw Error(`Could not inspect the checkout's eve executable.`);return i.includes(String(a.stdout??``))?String(a.stdout):void 0}async function captureTree(e,t){await runGitCommand(e,`git -C ${quote(t)} add -A -- .`);let n=await gitOutput(e,`git -C ${quote(t)} write-tree`);return assertFullSha(n,`registry tree`),n}async function restoreTree(e,t,n){await runGitCommand(e,`git -C ${quote(t)} read-tree --reset -u ${quote(n)}`),await runGitCommand(e,`git -C ${quote(t)} clean -fd -- .`)}async function changedPaths(e,t,n,r){return(await gitOutput(e,`git -C ${quote(t)} diff-tree -r --no-commit-id --name-only -z ${quote(n)} ${quote(r)}`,!1)).split(`\0`).filter(e=>e.length>0)}async function runChecked(e,t,n){return(await e.run({abortSignal:n,command:t})).exitCode===0}function isolatedCommand(e,t){return`env -i HOME=/tmp/eve-selfmod-home PATH=/usr/local/bin:/usr/bin:/bin CI=1 npm_config_ignore_scripts=true sh -c ${quote(`cd ${quote(e)} && ${t}`)}`}function quote(e){return shellQuote(e)}export{acceptsNonSecretSetupAnswers,assertOfficialRegistryAddress,bootstrapCommand,installProductionRegistryItem,locatePackageManagerRoot};