import{H as e}from"./subsystem-kTFXkQM5.js";import{g as t}from"./paths-BjD3T_xq.js";import{d as n}from"./agent-scope-BKE4M04k.js";import{i as r,r as i}from"./windows-spawn-Dw4dpTt2.js";import{n as a,t as o}from"./fetch-guard-CEuTlYG7.js";import s from"node:path";let c;function l(){return c||=e(),c}function u(e,t={}){let n=s.resolve(e);return[t.preferredTmpDir??l(),s.join(n,`media`),s.join(n,`agents`),s.join(n,`workspace`),s.join(n,`sandboxes`)]}function d(){return u(t())}function f(e,r){let i=u(t());if(!r?.trim())return i;let a=n(e,r);if(!a)return i;let o=s.resolve(a);return i.includes(o)||i.push(o),i}async function p(e,t){let n,r=!1;return await new Promise((i,a)=>{let o=()=>{n!==void 0&&(clearTimeout(n),n=void 0)};n=setTimeout(()=>{r=!0,o(),e.cancel().catch(()=>void 0),a(Error(`Media download stalled: no data received for ${t}ms`))},t),e.read().then(e=>{o(),r||i(e)},e=>{o(),r||a(e)})})}async function m(e,t,n){let r=n?.onOverflow??(e=>Error(`Content too large: ${e.size} bytes (limit: ${e.maxBytes} bytes)`)),i=n?.chunkTimeoutMs,a=e.body;if(!a||typeof a.getReader!=`function`){let n=Buffer.from(await e.arrayBuffer());if(n.length>t)throw r({size:n.length,maxBytes:t,res:e});return n}let o=a.getReader(),s=[],c=0;try{for(;;){let{done:n,value:a}=i?await p(o,i):await o.read();if(n)break;if(a?.length){if(c+=a.length,c>t){try{await o.cancel()}catch{}throw r({size:c,maxBytes:t,res:e})}s.push(a)}}}finally{try{o.releaseLock()}catch{}}return Buffer.concat(s.map(e=>Buffer.from(e)),c)}var h=class extends Error{constructor(e,t){super(t),this.code=e,this.name=`MediaFetchError`}};function g(e){return e.replace(/^["']|["']$/g,``)}function _(e){if(!e)return;let t=/filename\*\s*=\s*([^;]+)/i.exec(e);if(t?.[1]){let e=g(t[1].trim()),n=e.split(`''`).slice(1).join(`''`)||e;try{return s.basename(decodeURIComponent(n))}catch{return s.basename(n)}}let n=/filename\s*=\s*([^;]+)/i.exec(e);if(n?.[1])return s.basename(g(n[1].trim()))}async function v(e,t=200){try{let n=await e.text();if(!n)return;let r=n.replace(/\s+/g,` `).trim();return r?r.length<=t?r:`${r.slice(0,t)}…`:void 0}catch{return}}async function y(e){let{url:t,fetchImpl:n,requestInit:c,filePathHint:l,maxBytes:u,maxRedirects:d,readIdleTimeoutMs:f,ssrfPolicy:p,lookupFn:g}=e,y,b=t,x=null;try{let e=await o(a({url:t,fetchImpl:n,init:c,maxRedirects:d,policy:p,lookupFn:g}));y=e.response,b=e.finalUrl,x=e.release}catch(e){throw new h(`fetch_failed`,`Failed to fetch media from ${t}: ${String(e)}`)}try{if(!y.ok){let e=y.statusText?` ${y.statusText}`:``,n=b===t?``:` (redirected to ${b})`,r=`HTTP ${y.status}${e}`;if(!y.body)r=`HTTP ${y.status}${e}; empty response body`;else{let e=await v(y);e&&(r+=`; body: ${e}`)}throw new h(`http_error`,`Failed to fetch media from ${t}${n}: ${r}`)}let e=y.headers.get(`content-length`);if(u&&e){let n=Number(e);if(Number.isFinite(n)&&n>u)throw new h(`max_bytes`,`Failed to fetch media from ${t}: content length ${n} exceeds maxBytes ${u}`)}let n;try{n=u?await m(y,u,{onOverflow:({maxBytes:e,res:n})=>new h(`max_bytes`,`Failed to fetch media from ${n.url||t}: payload exceeds maxBytes ${e}`),chunkTimeoutMs:f}):Buffer.from(await y.arrayBuffer())}catch(e){throw e instanceof h?e:new h(`fetch_failed`,`Failed to fetch media from ${y.url||t}: ${String(e)}`)}let a;try{let e=new URL(b);a=s.basename(e.pathname)||void 0}catch{}let o=_(y.headers.get(`content-disposition`)),c=o||a||(l?s.basename(l):void 0),d=o&&s.extname(o)?o:l??b,p=await i({buffer:n,headerMime:y.headers.get(`content-type`),filePath:d});if(c&&!s.extname(c)&&p){let e=r(p);e&&(c=`${c}${e}`)}return{buffer:n,contentType:p??void 0,fileName:c}}finally{x&&await x()}}export{d as a,f as i,y as n,m as r,h as t};
