UNPKG

eve

Version:

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

3 lines • 1.69 kB
import{loadContext}from"#context/container.js";import{MAX_OUTPUT_BYTES,capLineLength}from"#execution/sandbox/truncate-output.js";import{buildReadFileTargetKey,createReadFileStamp,normalizeModelPath,setReadFileStamp}from"#execution/tools/file-state.js";import{resolveAbsoluteFilePath}from"#execution/sandbox/require-sandbox.js";async function executeReadFileOnSandbox(e,t){let{filePath:n,offset:r,limit:i}=t,a=await resolveAbsoluteFilePath(e,n),o=normalizeModelPath(a),s=r??1,c=i??2e3;if(s<1)throw Error(`offset must be >= 1. Received: ${s}.`);let l=await e.readTextFile({path:a});if(l===null)throw Error(`File not found: ${n}. Verify the path exists and is accessible in the sandbox.`);if(l.includes(`\0`))throw Error(`File "${n}" contains NUL bytes and appears to be a binary file. read_file only supports text files.`);let u=l.split(` `),d=u.length>0&&u[u.length-1]===``?u.length-1:u.length;if(d===0){if(s>1)throw Error(`offset ${s} is past the end of the file (0 lines). Use the default offset to read an empty file.`)}else if(s>d)throw Error(`offset ${s} is past the end of the file (${d} lines).`);let f=createReadFileStamp({content:l,filePath:o}),p=buildReadFileTargetKey(o);if(setReadFileStamp(loadContext(),p,f),d===0)return{content:``,path:o,totalLines:0,truncated:!1};let m=s-1,h=Math.min(m+c,d),g=u.slice(m,h),_=[],v=0,y=!1;for(let e=0;e<g.length;e++){let t=`${s+e}: ${capLineLength(g[e]??``)}`,n=Buffer.byteLength(t,`utf8`)+1;if(v+n>MAX_OUTPUT_BYTES&&_.length>0){y=!0;break}_.push(t),v+=n}let b=_.join(` `),x=s+_.length-1;return x<d||y?{content:b,nextOffset:x+1,path:o,totalLines:d,truncated:!0}:{content:b,path:o,totalLines:d,truncated:!1}}export{executeReadFileOnSandbox};