UNPKG

eve

Version:

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

2 lines 2.71 kB
import{randomUUID}from"node:crypto";import{WORKSPACE_ROOT}from"#runtime/workspace/types.js";import{expectDockerSuccess}from"#execution/sandbox/bindings/docker-utils.js";import{dirname}from"node:path/posix";import{resolveWorkspacePath}from"#execution/sandbox/bindings/local-backend-utils.js";import{shellQuote}from"#execution/sandbox/shell-quote.js";import{bufferToStream,streamToBuffer}from"#execution/sandbox/stream-utils.js";const DOCKER_KILL_TREE_SCRIPT=[`pid_file="$1"`,`target="$(cat "$pid_file" 2>/dev/null)" || exit 0`,`[ -n "$target" ] || exit 0`,`kill_tree() {`,` local parent="$1" dir child ppid line`,` for dir in /proc/[0-9]*; do`,' child="${dir#/proc/}"',` ppid=""`,` while IFS= read -r line; do`,` case "$line" in`,' PPid:*) ppid="${line#PPid:}"; ppid="${ppid//[^0-9]/}"; break ;;',` esac`,` done < "$dir/status" 2>/dev/null`,` [ "$ppid" = "$parent" ] && kill_tree "$child"`,` done`,` kill -9 "$parent" 2>/dev/null`,`}`,`kill_tree "$target"`,`rm -f "$pid_file"`,`exit 0`].join(` `);function createDockerInternalSession(e){let{cli:t,containerIdentity:n}=e;async function killSpawnTree(e){await t.run([`exec`,n,`bash`,`-c`,DOCKER_KILL_TREE_SCRIPT,`eve-kill-tree`,e]).catch(()=>{})}return{id:e.id,resolvePath:resolveWorkspacePath,async spawn(e){let r=[`exec`,`-w`,resolveWorkspacePath(e.workingDirectory??WORKSPACE_ROOT)];for(let[t,n]of Object.entries(e.env??{}))r.push(`-e`,`${t}=${n}`);let i=`/tmp/.eve-sbx-spawn-${randomUUID()}.pid`,a=`echo "$$" > ${shellQuote(i)}; bash -lc ${shellQuote(e.command)}; status=$?; rm -f ${shellQuote(i)}; exit $status`;r.push(n,`bash`,`-c`,a);let o=t.stream(r,{signal:e.abortSignal});return e.abortSignal?.addEventListener(`abort`,()=>void killSpawnTree(i),{once:!0}),{stdout:o.stdout,stderr:o.stderr,async wait(){return await o.wait()},async kill(){await killSpawnTree(i),await o.kill()}}},async readFile(e){let r=shellQuote(e.path),i=await t.run([`exec`,n,`bash`,`-lc`,`if [ -e ${r} ]; then exec cat ${r}; else exit 43; fi`],{signal:e.abortSignal});return i.exitCode===43?null:(expectDockerSuccess(i,`read "${e.path}" from sandbox container`),bufferToStream(i.stdoutBytes))},async removePath(e){let r=`${e.recursive===!0?`r`:``}${e.force===!0?`f`:``}`,i=[`exec`,n,`rm`,...r.length>0?[`-${r}`]:[],`--`,e.path],a=await t.run(i,{signal:e.abortSignal});expectDockerSuccess(a,`remove "${e.path}" from sandbox container`)},async writeFile(e){let r=await streamToBuffer(e.content),i=await t.run([`exec`,`-i`,n,`bash`,`-lc`,`mkdir -p ${shellQuote(dirname(e.path))} && cat > ${shellQuote(e.path)}`],{signal:e.abortSignal,stdin:r});expectDockerSuccess(i,`write "${e.path}" into sandbox container`)}}}export{createDockerInternalSession};