UNPKG

eve

Version:

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

1 lines • 1.7 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";import{requestPublicUrl}from"#execution/web-fetch/request.js";const MAX_RESPONSE_SIZE=5242880;async function executeWebFetchTool(e,t){let{url:n,format:r=`markdown`,timeout:i}=e,a=Math.min(i===void 0?3e4:i*1e3,12e4),o=AbortSignal.timeout(a),s=t?.abortSignal===void 0?o:AbortSignal.any([t.abortSignal,o]),c=buildHeaders(r),l=await requestPublicUrl(n,{headers:c,maxResponseSize:MAX_RESPONSE_SIZE,signal:s}),u=l.status===403&&l.headers.get(`cf-mitigated`)===`challenge`?await requestPublicUrl(n,{headers:{...c,"User-Agent":EVE_PACKAGE_NAME},maxResponseSize:MAX_RESPONSE_SIZE,signal:s}):l,d=await u.arrayBuffer(),f=u.headers.get(`content-type`)??``,p=f.includes(`text/html`),m=new TextDecoder().decode(d),h;if(h=r===`markdown`&&p?convertHtmlToMarkdown(m):r===`text`&&p?extractTextFromHtml(m):m,!u.ok){let e=`Request failed with status code: ${u.status}`;h=h.length===0?e:`${e}\n\n${h}`}let{output:g,truncated:_}=truncateHead(h);return{content:g,contentType:f,truncated:_,url:n}}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};