UNPKG

eve

Version:

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

1 lines 1.81 kB
import{truncateHead}from"#execution/sandbox/truncate-output.js";import{EVE_PACKAGE_NAME}from"#internal/package-name.js";import{convertHtmlToMarkdown,extractTextFromHtml}from"#execution/web-fetch/html.js";const MAX_RESPONSE_SIZE=5*1024*1024;async function executeWebFetchTool(n){let{url:r,format:i=`markdown`,timeout:a}=n;if(!r.startsWith(`http://`)&&!r.startsWith(`https://`))throw Error(`URL must start with http:// or https://`);let o=Math.min(a===void 0?3e4:a*1e3,12e4),s=AbortSignal.timeout(o),c=buildHeaders(i),l=await fetch(r,{headers:c,signal:s}),u=l.status===403&&l.headers.get(`cf-mitigated`)===`challenge`?await fetch(r,{headers:{...c,"User-Agent":EVE_PACKAGE_NAME},signal:s}):l;if(!u.ok)throw Error(`Request failed with status code: ${u.status}`);let d=u.headers.get(`content-length`);if(d!==null&&parseInt(d,10)>MAX_RESPONSE_SIZE)throw Error(`Response too large (exceeds 5 MB limit).`);let f=await u.arrayBuffer();if(f.byteLength>MAX_RESPONSE_SIZE)throw Error(`Response too large (exceeds 5 MB limit).`);let p=u.headers.get(`content-type`)??``,m=p.includes(`text/html`),h=new TextDecoder().decode(f),g;g=i===`markdown`&&m?convertHtmlToMarkdown(h):i===`text`&&m?extractTextFromHtml(h):h;let{output:_,truncated:v}=truncateHead(g);return{content:_,contentType:p,truncated:v,url:r}}function buildHeaders(e){let t;return t=e===`markdown`?`text/markdown;q=1.0, text/x-markdown;q=0.9, text/plain;q=0.8, text/html;q=0.7, */*;q=0.1`:e===`text`?`text/plain;q=1.0, text/markdown;q=0.9, text/html;q=0.8, */*;q=0.1`:`text/html;q=1.0, application/xhtml+xml;q=0.9, text/plain;q=0.8, text/markdown;q=0.7, */*;q=0.1`,{Accept:t,"Accept-Language":`en-US,en;q=0.9`,"User-Agent":`Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36`}}export{executeWebFetchTool};