eve
Version:
Filesystem-first framework for durable backend AI agents that run anywhere.
2 lines • 2.7 kB
JavaScript
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(s){let{cli:c,containerName:l}=s;async function killSpawnTree(e){await c.run([`exec`,l,`bash`,`-c`,DOCKER_KILL_TREE_SCRIPT,`eve-kill-tree`,e]).catch(()=>{})}return{id:s.id,resolvePath:resolveWorkspacePath,async spawn(n){let r=[`exec`,`-w`,resolveWorkspacePath(n.workingDirectory??WORKSPACE_ROOT)];for(let[e,t]of Object.entries(n.env??{}))r.push(`-e`,`${e}=${t}`);let i=`/tmp/.eve-sbx-spawn-${randomUUID()}.pid`,a=`echo "$$" > ${shellQuote(i)}; bash -lc ${shellQuote(n.command)}; status=$?; rm -f ${shellQuote(i)}; exit $status`;r.push(l,`bash`,`-c`,a);let o=c.stream(r,{signal:n.abortSignal});return n.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 t=shellQuote(e.path),r=await c.run([`exec`,l,`bash`,`-lc`,`if [ -e ${t} ]; then exec cat ${t}; else exit 43; fi`],{signal:e.abortSignal});return r.exitCode===43?null:(expectDockerSuccess(r,`read "${e.path}" from sandbox container`),bufferToStream(r.stdoutBytes))},async removePath(e){let t=`${e.recursive===!0?`r`:``}${e.force===!0?`f`:``}`,r=[`exec`,l,`rm`,...t.length>0?[`-${t}`]:[],`--`,e.path];expectDockerSuccess(await c.run(r,{signal:e.abortSignal}),`remove "${e.path}" from sandbox container`)},async writeFile(e){let t=await streamToBuffer(e.content);expectDockerSuccess(await c.run([`exec`,`-i`,l,`bash`,`-lc`,`mkdir -p ${shellQuote(dirname(e.path))} && cat > ${shellQuote(e.path)}`],{signal:e.abortSignal,stdin:t}),`write "${e.path}" into sandbox container`)}}}export{createDockerInternalSession};