eve
Version:
Filesystem-first framework for durable backend AI agents that run anywhere.
3 lines • 2.86 kB
JavaScript
import{normalizeModelPath}from"#runtime/framework-tools/file-state.js";import{MAX_OUTPUT_BYTES,capLineLength}from"#execution/sandbox/truncate-output.js";import{shellQuote}from"#execution/sandbox/shell-quote.js";import{validateAbsoluteFilePath}from"#execution/sandbox/require-sandbox.js";import{ripgrepIsAvailable}from"#execution/sandbox/ripgrep-probe.js";async function executeGrepOnSandbox(t,n){let r=n.path??`/workspace`;validateAbsoluteFilePath(r);let o=normalizeModelPath(r),s=Math.min(Math.max(1,n.limit??100),1e3),c=n.context!==void 0&&n.context>0?n.context:0,l=await ripgrepIsAvailable(t)?buildRipgrepCommand({contextLines:c,effectiveLimit:s,glob:n.glob,ignoreCase:n.ignoreCase??!1,literal:n.literal??!1,normalizedPath:o,pattern:n.pattern}):buildPosixGrepCommand({contextLines:c,effectiveLimit:s,glob:n.glob,ignoreCase:n.ignoreCase??!1,literal:n.literal??!1,normalizedPath:o,pattern:n.pattern}),u=await t.run({command:l});if(u.exitCode!==0&&u.exitCode!==1)throw buildGrepExecutionError(l,u.exitCode,u.stderr);let d=u.stdout;return d.trim().length===0?{content:`No matches found`,matchCount:0,path:o,truncated:!1}:processOutput({effectiveLimit:s,normalizedPath:o,stdout:d})}function buildRipgrepCommand(e){let t=[`rg`,`--line-number`,`--color=never`,`--hidden`,`--glob '!.git/*'`];return e.ignoreCase&&t.push(`--ignore-case`),e.literal&&t.push(`--fixed-strings`),e.glob!==void 0&&t.push(`--glob ${shellQuote(e.glob)}`),e.contextLines>0&&t.push(`--context ${e.contextLines}`),t.push(`--max-count ${e.effectiveLimit}`),t.push(`--`),t.push(shellQuote(e.pattern)),t.push(shellQuote(e.normalizedPath)),t.join(` `)}function buildPosixGrepCommand(e){let t=[`grep`,`-r`,`-n`,`--color=never`,`--exclude-dir=.git`];return e.ignoreCase&&t.push(`-i`),e.literal?t.push(`-F`):t.push(`-E`),e.glob!==void 0&&t.push(`--include=${shellQuote(e.glob)}`),e.contextLines>0&&t.push(`-C ${e.contextLines}`),t.push(`-m ${e.effectiveLimit}`),t.push(`--`),t.push(shellQuote(e.pattern)),t.push(shellQuote(e.normalizedPath)),t.join(` `)}function processOutput(e){let r=e.stdout.split(`
`),i=[],a=0,o=0,s=!1;for(let e=0;e<r.length;e+=1){let c=r[e]??``;if(c.length===0&&e===r.length-1)continue;c!==`--`&&c.length>0&&/^.+:\d+:/.test(c)&&o++;let l=capLineLength(c),u=Buffer.byteLength(l,`utf8`)+1;if(a+u>MAX_OUTPUT_BYTES&&i.length>0){s=!0;break}i.push(l),a+=u}let c=s||o>=e.effectiveLimit,l=i.join(`
`);if(c){let t=[];o>=e.effectiveLimit&&t.push(`Match limit reached (${e.effectiveLimit}). Use a larger limit or more specific pattern.`),s&&t.push(`Output truncated due to size. Use a more specific path or pattern.`),l+=`\n\n[${t.join(` `)}]`}return{content:l,matchCount:o,path:e.normalizedPath,truncated:c}}function buildGrepExecutionError(e,t,n){let r=n.trim(),i=r.length>0?r:`no stderr output`;return Error(`grep failed (exit ${t}): ${i}\nCommand: ${e}`)}export{executeGrepOnSandbox};