mcp-scraper 0.97.0 → 0.97.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. package/CHANGELOG.md +18 -1
  2. package/README.md +1 -1
  3. package/dist/actions-5X5CQAKO.js +1 -0
  4. package/dist/bin/api-server.js +1 -1
  5. package/dist/bin/mcp-scraper-cli.js +1 -1
  6. package/dist/bin/mcp-scraper-core.js +1 -1
  7. package/dist/bin/mcp-scraper-install.js +1 -1
  8. package/dist/bin/mcp-stdio-server.js +1 -1
  9. package/dist/bin/paa-harvest.js +1 -1
  10. package/dist/bundle-GS657HXG.js +1 -0
  11. package/dist/chunk-2L7CHN5N.js +4 -0
  12. package/dist/{chunk-4P3LZNVA.js → chunk-4AEW53RU.js} +145 -145
  13. package/dist/{chunk-U3NV72AL.js → chunk-4YZ74SJ6.js} +1 -8
  14. package/dist/{chunk-AMYPXA2F.js → chunk-5RQWFYXY.js} +74 -74
  15. package/dist/{chunk-PD7BI7TR.js → chunk-5TQINRZN.js} +1 -1
  16. package/dist/{chunk-Z4EHBBL4.js → chunk-5UQAL7HV.js} +1 -1
  17. package/dist/{chunk-GLCR4KJI.js → chunk-6D25Q5BR.js} +20 -14
  18. package/dist/chunk-C3JWX6KM.js +10 -0
  19. package/dist/{chunk-NULRGHJ4.js → chunk-F3M7INSN.js} +7 -7
  20. package/dist/chunk-FLAWC5PF.js +5 -0
  21. package/dist/{chunk-PQRT4T2G.js → chunk-FM7BX2LU.js} +1 -1
  22. package/dist/chunk-IPQOBOFY.js +1 -0
  23. package/dist/chunk-J7XDXIB6.js +163 -0
  24. package/dist/chunk-JHDD6R3O.js +8 -0
  25. package/dist/chunk-KBZY2OGP.js +11 -0
  26. package/dist/{chunk-JNPM3U3L.js → chunk-NQFTETVC.js} +1 -1
  27. package/dist/{chunk-A4UASMEQ.js → chunk-P6CDR3RM.js} +1 -1
  28. package/dist/chunk-QCBCOEH5.js +1 -0
  29. package/dist/chunk-SJNSTO3C.js +42 -0
  30. package/dist/{chunk-MVMWPKRC.js → chunk-TZYITYL4.js} +1 -1
  31. package/dist/{chunk-YJPFEL2E.js → chunk-UUJYICAJ.js} +1 -1
  32. package/dist/{chunk-OSKCHEVM.js → chunk-XNTAHMPA.js} +1 -1
  33. package/dist/{chunk-ND4WIBXT.js → chunk-ZDECILF6.js} +1 -1
  34. package/dist/{chunk-OOB3RKWD.js → chunk-ZNPGM3XE.js} +1 -1
  35. package/dist/compatibility-UPPKZ4KY.js +1 -0
  36. package/dist/direct-completion-ZVPPEBDU.js +1 -0
  37. package/dist/dispatch-GRMMPKQD.js +1 -0
  38. package/dist/index.cjs +34 -34
  39. package/dist/index.js +1 -1
  40. package/dist/jobs-NRZHGQ7I.js +1 -0
  41. package/dist/queue-2VKW7SGH.js +1 -0
  42. package/dist/reconciliation-QC6SILLR.js +1 -0
  43. package/dist/{report-artifact-offload-6OOY2PTM.js → report-artifact-offload-PQNFDNPL.js} +1 -1
  44. package/dist/{server-6HUBN3OR.js → server-EYMJROPX.js} +530 -533
  45. package/dist/settlement-47EUTSGX.js +1 -0
  46. package/dist/vendor-errors-647LSH6H.js +1 -0
  47. package/dist/worker-AWJF7UYR.js +1 -0
  48. package/package.json +1 -1
  49. package/dist/actions-NCZNPYEY.js +0 -1
  50. package/dist/bundle-XR6XKYJM.js +0 -1
  51. package/dist/chunk-63VWBMYR.js +0 -3
  52. package/dist/chunk-6FAMBNHY.js +0 -1
  53. package/dist/chunk-M5QHXNFZ.js +0 -10
  54. package/dist/chunk-U4WIJZCX.js +0 -1
  55. package/dist/chunk-WHVK6FUB.js +0 -11
  56. package/dist/chunk-XYUJTIPE.js +0 -203
  57. package/dist/dispatch-ILBBKOSW.js +0 -1
  58. package/dist/queue-VOGXQDPR.js +0 -1
  59. package/dist/reconciliation-OXBG7JA2.js +0 -1
  60. package/dist/settlement-3PS32LQO.js +0 -1
  61. package/dist/worker-NXRU3TT3.js +0 -1
@@ -0,0 +1,10 @@
1
+ function S(r,e,t,n,o){if(n==="m")throw new TypeError("Private method is not writable");if(n==="a"&&!o)throw new TypeError("Private accessor was defined without a setter");if(typeof e=="function"?r!==e||!o:!e.has(r))throw new TypeError("Cannot write private member to an object whose class did not declare it");return n==="a"?o.call(r,t):o?o.value=t:e.set(r,t),t}function _(r,e,t,n){if(t==="a"&&!n)throw new TypeError("Private accessor was defined without a getter");if(typeof e=="function"?r!==e||!n:!e.has(r))throw new TypeError("Cannot read private member from an object whose class did not declare it");return t==="m"?n:t==="a"?n.call(r):n?n.value:e.get(r)}var ct=function(){let{crypto:r}=globalThis;if(r?.randomUUID)return ct=r.randomUUID.bind(r),r.randomUUID();let e=new Uint8Array(1),t=r?()=>r.getRandomValues(e)[0]:()=>Math.random()*255&255;return"10000000-1000-4000-8000-100000000000".replace(/[018]/g,n=>(+n^t()&15>>+n/4).toString(16))};function Ue(r){return typeof r=="object"&&r!==null&&("name"in r&&r.name==="AbortError"||"message"in r&&String(r.message).includes("FetchRequestCanceledException"))}var ve=r=>{if(r instanceof Error)return r;if(typeof r=="object"&&r!==null){try{if(Object.prototype.toString.call(r)==="[object Error]"){let e=new Error(r.message,r.cause?{cause:r.cause}:{});return r.stack&&(e.stack=r.stack),r.cause&&!e.cause&&(e.cause=r.cause),r.name&&(e.name=r.name),e}}catch{}try{return new Error(JSON.stringify(r))}catch{}}return new Error(r)};var x=class extends Error{},I=class r extends x{constructor(e,t,n,o){super(`${r.makeMessage(e,t,n)}`),this.status=e,this.headers=o,this.error=t}static makeMessage(e,t,n){let o=t?.message?typeof t.message=="string"?t.message:JSON.stringify(t.message):t?JSON.stringify(t):n;return e&&o?`${e} ${o}`:e?`${e} status code (no body)`:o||"(no status code or body)"}static generate(e,t,n,o){if(!e||!o)return new D({message:n,cause:ve(t)});let s=t;return e===400?new he(e,s,n,o):e===401?new W(e,s,n,o):e===403?new X(e,s,n,o):e===404?new me(e,s,n,o):e===409?new ye(e,s,n,o):e===422?new ge(e,s,n,o):e===429?new we(e,s,n,o):e>=500?new V(e,s,n,o):new r(e,s,n,o)}},T=class extends I{constructor({message:e}={}){super(void 0,void 0,e||"Request was aborted.",void 0)}},D=class extends I{constructor({message:e,cause:t}){super(void 0,void 0,e||"Connection error.",void 0),t&&(this.cause=t)}},U=class extends D{constructor({message:e}={}){super({message:e??"Request timed out."})}},he=class extends I{},W=class extends I{},X=class extends I{},me=class extends I{},ye=class extends I{},ge=class extends I{},we=class extends I{},V=class extends I{};var Er=/^[a-z][a-z0-9+.-]*:/i,vt=r=>Er.test(r),$=r=>($=Array.isArray,$(r)),lt=$;function ut(r){return typeof r!="object"?{}:r??{}}function ft(r){if(!r)return!0;for(let e in r)return!1;return!0}function Dt(r,e){return Object.prototype.hasOwnProperty.call(r,e)}var Nt=(r,e)=>{if(typeof e!="number"||!Number.isInteger(e))throw new x(`${r} must be an integer`);if(e<0)throw new x(`${r} must be a positive integer`);return e},Sr=r=>{if(typeof r=="number")return Math.round(r);if(typeof r=="string")return parseInt(r,10);throw new x(`Could not coerce ${r} (type: ${typeof r}) into a number`)};var Ir=r=>typeof r=="boolean"?r:typeof r=="string"?r==="true":!!r,Ht=r=>{if(r!=null)return Sr(r)};var dt=r=>{if(r!=null)return Ir(r)},Bt=r=>{try{return JSON.parse(r)}catch{return}};var Mt=r=>new Promise(e=>setTimeout(e,r));var z="0.93.0";function $r(){return typeof Deno<"u"&&Deno.build!=null?"deno":typeof EdgeRuntime<"u"?"edge":Object.prototype.toString.call(typeof globalThis.process<"u"?globalThis.process:0)==="[object process]"?"node":"unknown"}var Lr=()=>{let r=$r();if(r==="deno")return{"X-Stainless-Lang":"js","X-Stainless-Package-Version":z,"X-Stainless-OS":qt(Deno.build.os),"X-Stainless-Arch":Kt(Deno.build.arch),"X-Stainless-Runtime":"deno","X-Stainless-Runtime-Version":typeof Deno.version=="string"?Deno.version:Deno.version?.deno??"unknown"};if(typeof EdgeRuntime<"u")return{"X-Stainless-Lang":"js","X-Stainless-Package-Version":z,"X-Stainless-OS":"Unknown","X-Stainless-Arch":`other:${EdgeRuntime}`,"X-Stainless-Runtime":"edge","X-Stainless-Runtime-Version":globalThis.process.version};if(r==="node")return{"X-Stainless-Lang":"js","X-Stainless-Package-Version":z,"X-Stainless-OS":qt(globalThis.process.platform??"unknown"),"X-Stainless-Arch":Kt(globalThis.process.arch??"unknown"),"X-Stainless-Runtime":"node","X-Stainless-Runtime-Version":globalThis.process.version??"unknown"};let e=Tr();return e?{"X-Stainless-Lang":"js","X-Stainless-Package-Version":z,"X-Stainless-OS":"Unknown","X-Stainless-Arch":"unknown","X-Stainless-Runtime":`browser:${e.browser}`,"X-Stainless-Runtime-Version":e.version}:{"X-Stainless-Lang":"js","X-Stainless-Package-Version":z,"X-Stainless-OS":"Unknown","X-Stainless-Arch":"unknown","X-Stainless-Runtime":"unknown","X-Stainless-Runtime-Version":"unknown"}};function Tr(){if(typeof navigator>"u"||!navigator)return null;let r=[{key:"edge",pattern:/Edge(?:\W+(\d+)\.(\d+)(?:\.(\d+))?)?/},{key:"ie",pattern:/MSIE(?:\W+(\d+)\.(\d+)(?:\.(\d+))?)?/},{key:"ie",pattern:/Trident(?:.*rv\:(\d+)\.(\d+)(?:\.(\d+))?)?/},{key:"chrome",pattern:/Chrome(?:\W+(\d+)\.(\d+)(?:\.(\d+))?)?/},{key:"firefox",pattern:/Firefox(?:\W+(\d+)\.(\d+)(?:\.(\d+))?)?/},{key:"safari",pattern:/(?:Version\W+(\d+)\.(\d+)(?:\.(\d+))?)?(?:\W+Mobile\S*)?\W+Safari/}];for(let{key:e,pattern:t}of r){let n=t.exec(navigator.userAgent);if(n){let o=n[1]||0,s=n[2]||0,a=n[3]||0;return{browser:e,version:`${o}.${s}.${a}`}}}return null}var Kt=r=>r==="x32"?"x32":r==="x86_64"||r==="x64"?"x64":r==="arm"?"arm":r==="aarch64"||r==="arm64"?"arm64":r?`other:${r}`:"unknown",qt=r=>(r=r.toLowerCase(),r.includes("ios")?"iOS":r==="android"?"Android":r==="darwin"?"MacOS":r==="win32"?"Windows":r==="freebsd"?"FreeBSD":r==="openbsd"?"OpenBSD":r==="linux"?"Linux":r?`Other:${r}`:"Unknown"),Wt,Xt=()=>Wt??(Wt=Lr());function Vt(){if(typeof fetch<"u")return fetch;throw new Error("`fetch` is not defined as a global; Either pass `fetch` to the client, `new \u004b\u0065\u0072\u006e\u0065\u006c({ fetch })` or polyfill the global, `globalThis.fetch = fetch`")}function pt(...r){let e=globalThis.ReadableStream;if(typeof e>"u")throw new Error("`ReadableStream` is not defined as a global; You will need to polyfill it, `globalThis.ReadableStream = ReadableStream`");return new e(...r)}function Ve(r){let e=Symbol.asyncIterator in r?r[Symbol.asyncIterator]():r[Symbol.iterator]();return pt({start(){},async pull(t){let{done:n,value:o}=await e.next();n?t.close():t.enqueue(o)},async cancel(){await e.return?.()}})}function ht(r){if(r[Symbol.asyncIterator])return r;let e=r.getReader();return{async next(){try{let t=await e.read();return t?.done&&e.releaseLock(),t}catch(t){throw e.releaseLock(),t}},async return(){let t=e.cancel();return e.releaseLock(),await t,{done:!0,value:void 0}},[Symbol.asyncIterator](){return this}}}async function zt(r){if(r===null||typeof r!="object")return;if(r[Symbol.asyncIterator]){await r[Symbol.asyncIterator]().return?.();return}let e=r.getReader(),t=e.cancel();e.releaseLock(),await t}var Jt=({headers:r,body:e})=>({bodyHeaders:{"content-type":"application/json"},body:JSON.stringify(e)});var mt="RFC3986",yt=r=>String(r),gt={RFC1738:r=>String(r).replace(/%20/g,"+"),RFC3986:yt},Gt="RFC1738";var ze=(r,e)=>(ze=Object.hasOwn??Function.prototype.call.bind(Object.prototype.hasOwnProperty),ze(r,e)),N=(()=>{let r=[];for(let e=0;e<256;++e)r.push("%"+((e<16?"0":"")+e.toString(16)).toUpperCase());return r})();var wt=1024,Qt=(r,e,t,n,o)=>{if(r.length===0)return r;let s=r;if(typeof r=="symbol"?s=Symbol.prototype.toString.call(r):typeof r!="string"&&(s=String(r)),t==="iso-8859-1")return escape(s).replace(/%u[0-9a-f]{4}/gi,function(c){return"%26%23"+parseInt(c.slice(2),16)+"%3B"});let a="";for(let c=0;c<s.length;c+=wt){let f=s.length>=wt?s.slice(c,c+wt):s,d=[];for(let m=0;m<f.length;++m){let l=f.charCodeAt(m);if(l===45||l===46||l===95||l===126||l>=48&&l<=57||l>=65&&l<=90||l>=97&&l<=122||o===Gt&&(l===40||l===41)){d[d.length]=f.charAt(m);continue}if(l<128){d[d.length]=N[l];continue}if(l<2048){d[d.length]=N[192|l>>6]+N[128|l&63];continue}if(l<55296||l>=57344){d[d.length]=N[224|l>>12]+N[128|l>>6&63]+N[128|l&63];continue}m+=1,l=65536+((l&1023)<<10|f.charCodeAt(m)&1023),d[d.length]=N[240|l>>18]+N[128|l>>12&63]+N[128|l>>6&63]+N[128|l&63]}a+=d.join("")}return a};function Yt(r){return!r||typeof r!="object"?!1:!!(r.constructor&&r.constructor.isBuffer&&r.constructor.isBuffer(r))}function bt(r,e){if($(r)){let t=[];for(let n=0;n<r.length;n+=1)t.push(e(r[n]));return t}return e(r)}var er={brackets(r){return String(r)+"[]"},comma:"comma",indices(r,e){return String(r)+"["+e+"]"},repeat(r){return String(r)}},tr=function(r,e){Array.prototype.push.apply(r,$(e)?e:[e])},Zt,R={addQueryPrefix:!1,allowDots:!1,allowEmptyArrays:!1,arrayFormat:"indices",charset:"utf-8",charsetSentinel:!1,delimiter:"&",encode:!0,encodeDotInKeys:!1,encoder:Qt,encodeValuesOnly:!1,format:mt,formatter:yt,indices:!1,serializeDate(r){return(Zt??(Zt=Function.prototype.call.bind(Date.prototype.toISOString)))(r)},skipNulls:!1,strictNullHandling:!1};function Cr(r){return typeof r=="string"||typeof r=="number"||typeof r=="boolean"||typeof r=="symbol"||typeof r=="bigint"}var xt={};function rr(r,e,t,n,o,s,a,c,f,d,m,l,g,h,A,E,v,k){let b=r,Y=k,Z=0,it=!1;for(;(Y=Y.get(xt))!==void 0&&!it;){let L=Y.get(r);if(Z+=1,typeof L<"u"){if(L===Z)throw new RangeError("Cyclic object value");it=!0}typeof Y.get(xt)>"u"&&(Z=0)}if(typeof d=="function"?b=d(e,b):b instanceof Date?b=g?.(b):t==="comma"&&$(b)&&(b=bt(b,function(L){return L instanceof Date?g?.(L):L})),b===null){if(s)return f&&!E?f(e,R.encoder,v,"key",h):e;b=""}if(Cr(b)||Yt(b)){if(f){let L=E?e:f(e,R.encoder,v,"key",h);return[A?.(L)+"="+A?.(f(b,R.encoder,v,"value",h))]}return[A?.(e)+"="+A?.(String(b))]}let q=[];if(typeof b>"u")return q;let Fe;if(t==="comma"&&$(b))E&&f&&(b=bt(b,f)),Fe=[{value:b.length>0?b.join(",")||null:void 0}];else if($(d))Fe=d;else{let L=Object.keys(b);Fe=m?L.sort(m):L}let kt=c?String(e).replace(/\./g,"%2E"):String(e),Xe=n&&$(b)&&b.length===1?kt+"[]":kt;if(o&&$(b)&&b.length===0)return Xe+"[]";for(let L=0;L<Fe.length;++L){let pe=Fe[L],Ft=typeof pe=="object"&&typeof pe.value<"u"?pe.value:b[pe];if(a&&Ft===null)continue;let at=l&&c?pe.replace(/\./g,"%2E"):pe,Rr=$(b)?typeof t=="function"?t(Xe,at):Xe:Xe+(l?"."+at:"["+at+"]");k.set(r,Z);let Ut=new WeakMap;Ut.set(xt,k),tr(q,rr(Ft,Rr,t,n,o,s,a,c,t==="comma"&&E&&$(b)?null:f,d,m,l,g,h,A,E,v,Ut))}return q}function kr(r=R){if(typeof r.allowEmptyArrays<"u"&&typeof r.allowEmptyArrays!="boolean")throw new TypeError("`allowEmptyArrays` option can only be `true` or `false`, when provided");if(typeof r.encodeDotInKeys<"u"&&typeof r.encodeDotInKeys!="boolean")throw new TypeError("`encodeDotInKeys` option can only be `true` or `false`, when provided");if(r.encoder!==null&&typeof r.encoder<"u"&&typeof r.encoder!="function")throw new TypeError("Encoder has to be a function.");let e=r.charset||R.charset;if(typeof r.charset<"u"&&r.charset!=="utf-8"&&r.charset!=="iso-8859-1")throw new TypeError("The charset option must be either utf-8, iso-8859-1, or undefined");let t=mt;if(typeof r.format<"u"){if(!ze(gt,r.format))throw new TypeError("Unknown format option provided.");t=r.format}let n=gt[t],o=R.filter;(typeof r.filter=="function"||$(r.filter))&&(o=r.filter);let s;if(r.arrayFormat&&r.arrayFormat in er?s=r.arrayFormat:"indices"in r?s=r.indices?"indices":"repeat":s=R.arrayFormat,"commaRoundTrip"in r&&typeof r.commaRoundTrip!="boolean")throw new TypeError("`commaRoundTrip` must be a boolean, or absent");let a=typeof r.allowDots>"u"?r.encodeDotInKeys?!0:R.allowDots:!!r.allowDots;return{addQueryPrefix:typeof r.addQueryPrefix=="boolean"?r.addQueryPrefix:R.addQueryPrefix,allowDots:a,allowEmptyArrays:typeof r.allowEmptyArrays=="boolean"?!!r.allowEmptyArrays:R.allowEmptyArrays,arrayFormat:s,charset:e,charsetSentinel:typeof r.charsetSentinel=="boolean"?r.charsetSentinel:R.charsetSentinel,commaRoundTrip:!!r.commaRoundTrip,delimiter:typeof r.delimiter>"u"?R.delimiter:r.delimiter,encode:typeof r.encode=="boolean"?r.encode:R.encode,encodeDotInKeys:typeof r.encodeDotInKeys=="boolean"?r.encodeDotInKeys:R.encodeDotInKeys,encoder:typeof r.encoder=="function"?r.encoder:R.encoder,encodeValuesOnly:typeof r.encodeValuesOnly=="boolean"?r.encodeValuesOnly:R.encodeValuesOnly,filter:o,format:t,formatter:n,serializeDate:typeof r.serializeDate=="function"?r.serializeDate:R.serializeDate,skipNulls:typeof r.skipNulls=="boolean"?r.skipNulls:R.skipNulls,sort:typeof r.sort=="function"?r.sort:null,strictNullHandling:typeof r.strictNullHandling=="boolean"?r.strictNullHandling:R.strictNullHandling}}function nr(r,e={}){let t=r,n=kr(e),o,s;typeof n.filter=="function"?(s=n.filter,t=s("",t)):$(n.filter)&&(s=n.filter,o=s);let a=[];if(typeof t!="object"||t===null)return"";let c=er[n.arrayFormat],f=c==="comma"&&n.commaRoundTrip;o||(o=Object.keys(t)),n.sort&&o.sort(n.sort);let d=new WeakMap;for(let g=0;g<o.length;++g){let h=o[g];n.skipNulls&&t[h]===null||tr(a,rr(t[h],h,c,f,n.allowEmptyArrays,n.strictNullHandling,n.skipNulls,n.encodeDotInKeys,n.encode?n.encoder:null,n.filter,n.sort,n.allowDots,n.serializeDate,n.format,n.formatter,n.encodeValuesOnly,n.charset,d))}let m=a.join(n.delimiter),l=n.addQueryPrefix===!0?"?":"";return n.charsetSentinel&&(n.charset==="iso-8859-1"?l+="utf8=%26%2310003%3B&":l+="utf8=%E2%9C%93&"),m.length>0?l+m:""}function or(r){return nr(r,{arrayFormat:"comma"})}function ar(r){let e=0;for(let o of r)e+=o.length;let t=new Uint8Array(e),n=0;for(let o of r)t.set(o,n),n+=o.length;return t}var sr;function De(r){let e;return(sr??(e=new globalThis.TextEncoder,sr=e.encode.bind(e)))(r)}var ir;function _t(r){let e;return(ir??(e=new globalThis.TextDecoder,ir=e.decode.bind(e)))(r)}var j,C,ee=class{constructor(){j.set(this,void 0),C.set(this,void 0),S(this,j,new Uint8Array,"f"),S(this,C,null,"f")}decode(e){if(e==null)return[];let t=e instanceof ArrayBuffer?new Uint8Array(e):typeof e=="string"?De(e):e;S(this,j,ar([_(this,j,"f"),t]),"f");let n=[],o;for(;(o=Ur(_(this,j,"f"),_(this,C,"f")))!=null;){if(o.carriage&&_(this,C,"f")==null){S(this,C,o.index,"f");continue}if(_(this,C,"f")!=null&&(o.index!==_(this,C,"f")+1||o.carriage)){n.push(_t(_(this,j,"f").subarray(0,_(this,C,"f")-1))),S(this,j,_(this,j,"f").subarray(_(this,C,"f")),"f"),S(this,C,null,"f");continue}let s=_(this,C,"f")!==null?o.preceding-1:o.preceding,a=_t(_(this,j,"f").subarray(0,s));n.push(a),S(this,j,_(this,j,"f").subarray(o.index),"f"),S(this,C,null,"f")}return n}flush(){return _(this,j,"f").length?this.decode(`
2
+ `):[]}};j=new WeakMap,C=new WeakMap;ee.NEWLINE_CHARS=new Set([`
3
+ `,"\r"]);ee.NEWLINE_REGEXP=/\r\n|[\n\r]/g;function Ur(r,e){for(let o=e??0;o<r.length;o++){if(r[o]===10)return{preceding:o,index:o+1,carriage:!1};if(r[o]===13)return{preceding:o,index:o+1,carriage:!0}}return null}function cr(r){for(let n=0;n<r.length-1;n++){if(r[n]===10&&r[n+1]===10||r[n]===13&&r[n+1]===13)return n+2;if(r[n]===13&&r[n+1]===10&&n+3<r.length&&r[n+2]===13&&r[n+3]===10)return n+4}return-1}var Ge={off:0,error:200,warn:300,info:400,debug:500},At=(r,e,t)=>{if(r){if(Dt(Ge,r))return r;P(t).warn(`${e} was set to ${JSON.stringify(r)}, expected one of ${JSON.stringify(Object.keys(Ge))}`)}};function Ne(){}function Je(r,e,t){return!e||Ge[r]>Ge[t]?Ne:e[r].bind(e)}var vr={error:Ne,warn:Ne,info:Ne,debug:Ne},lr=new WeakMap;function P(r){let e=r.logger,t=r.logLevel??"off";if(!e)return vr;let n=lr.get(e);if(n&&n[0]===t)return n[1];let o={error:Je("error",e,t),warn:Je("warn",e,t),info:Je("info",e,t),debug:Je("debug",e,t)};return lr.set(e,[t,o]),o}var H=r=>(r.options&&(r.options={...r.options},delete r.options.headers),r.headers&&(r.headers=Object.fromEntries((r.headers instanceof Headers?[...r.headers]:Object.entries(r.headers)).map(([e,t])=>[e,e.toLowerCase()==="authorization"||e.toLowerCase()==="api-key"||e.toLowerCase()==="x-api-key"||e.toLowerCase()==="cookie"||e.toLowerCase()==="set-cookie"?"***":t]))),"retryOfRequestLogID"in r&&(r.retryOfRequestLogID&&(r.retryOf=r.retryOfRequestLogID),delete r.retryOfRequestLogID),r);var He,Qe=class r{constructor(e,t,n){this.iterator=e,He.set(this,void 0),this.controller=t,S(this,He,n,"f")}static fromSSEResponse(e,t,n){let o=!1,s=n?P(n):console;async function*a(){if(o)throw new x("Cannot iterate over a consumed stream, use `.tee()` to split the stream.");o=!0;let c=!1;try{for await(let f of Dr(e,t))try{yield JSON.parse(f.data)}catch(d){throw s.error("Could not parse message into JSON:",f.data),s.error("From chunk:",f.raw),d}c=!0}catch(f){if(Ue(f))return;throw f}finally{c||t.abort()}}return new r(a,t,n)}static fromReadableStream(e,t,n){let o=!1;async function*s(){let c=new ee,f=ht(e);for await(let d of f)for(let m of c.decode(d))yield m;for(let d of c.flush())yield d}async function*a(){if(o)throw new x("Cannot iterate over a consumed stream, use `.tee()` to split the stream.");o=!0;let c=!1;try{for await(let f of s())c||f&&(yield JSON.parse(f));c=!0}catch(f){if(Ue(f))return;throw f}finally{c||t.abort()}}return new r(a,t,n)}[(He=new WeakMap,Symbol.asyncIterator)](){return this.iterator()}tee(){let e=[],t=[],n=this.iterator(),o=s=>({next:()=>{if(s.length===0){let a=n.next();e.push(a),t.push(a)}return s.shift()}});return[new r(()=>o(e),this.controller,_(this,He,"f")),new r(()=>o(t),this.controller,_(this,He,"f"))]}toReadableStream(){let e=this,t;return pt({async start(){t=e[Symbol.asyncIterator]()},async pull(n){try{let{value:o,done:s}=await t.next();if(s)return n.close();let a=De(JSON.stringify(o)+`
4
+ `);n.enqueue(a)}catch(o){n.error(o)}},async cancel(){await t.return?.()}})}};async function*Dr(r,e){if(!r.body)throw e.abort(),typeof globalThis.navigator<"u"&&globalThis.navigator.product==="ReactNative"?new x("The default react-native fetch implementation does not support streaming. Please use expo/fetch: https://docs.expo.dev/versions/latest/sdk/expo/#expofetch-api"):new x("Attempted to iterate over a response with no body");let t=new Rt,n=new ee,o=ht(r.body);for await(let s of Nr(o))for(let a of n.decode(s)){let c=t.decode(a);c&&(yield c)}for(let s of n.flush()){let a=t.decode(s);a&&(yield a)}}async function*Nr(r){let e=new Uint8Array;for await(let t of r){if(t==null)continue;let n=t instanceof ArrayBuffer?new Uint8Array(t):typeof t=="string"?De(t):t,o=new Uint8Array(e.length+n.length);o.set(e),o.set(n,e.length),e=o;let s;for(;(s=cr(e))!==-1;)yield e.slice(0,s),e=e.slice(s)}e.length>0&&(yield e)}var Rt=class{constructor(){this.event=null,this.data=[],this.chunks=[]}decode(e){if(e.endsWith("\r")&&(e=e.substring(0,e.length-1)),!e){if(!this.event&&!this.data.length)return null;let s={event:this.event,data:this.data.join(`
5
+ `),raw:this.chunks};return this.event=null,this.data=[],this.chunks=[],s}if(this.chunks.push(e),e.startsWith(":"))return null;let[t,n,o]=Hr(e,":");return o.startsWith(" ")&&(o=o.substring(1)),t==="event"?this.event=o:t==="data"&&this.data.push(o),null}};function Hr(r,e){let t=r.indexOf(e);return t!==-1?[r.substring(0,t),e,r.substring(t+e.length)]:[r,"",""]}async function Ye(r,e){let{response:t,requestLogID:n,retryOfRequestLogID:o,startTime:s}=e,a=await(async()=>{if(e.options.stream)return P(r).debug("response",t.status,t.url,t.headers,t.body),e.options.__streamClass?e.options.__streamClass.fromSSEResponse(t,e.controller,r):Qe.fromSSEResponse(t,e.controller,r);if(t.status===204)return null;if(e.options.__binaryResponse)return t;let f=t.headers.get("content-type")?.split(";")[0]?.trim();return f?.includes("application/json")||f?.endsWith("+json")?t.headers.get("content-length")==="0"?void 0:await t.json():await t.text()})();return P(r).debug(`[${n}] response parsed`,H({retryOfRequestLogID:o,url:t.url,status:t.status,body:a,durationMs:Date.now()-s})),a}var Be,te=class r extends Promise{constructor(e,t,n=Ye){super(o=>{o(null)}),this.responsePromise=t,this.parseResponse=n,Be.set(this,void 0),S(this,Be,e,"f")}_thenUnwrap(e){return new r(_(this,Be,"f"),this.responsePromise,async(t,n)=>e(await this.parseResponse(t,n),n))}asResponse(){return this.responsePromise.then(e=>e.response)}async withResponse(){let[e,t]=await Promise.all([this.parse(),this.asResponse()]);return{data:e,response:t}}parse(){return this.parsedPromise||(this.parsedPromise=this.responsePromise.then(e=>this.parseResponse(_(this,Be,"f"),e))),this.parsedPromise}then(e,t){return this.parse().then(e,t)}catch(e){return this.parse().catch(e)}finally(e){return this.parse().finally(e)}};Be=new WeakMap;var Ze,et=class{constructor(e,t,n,o){Ze.set(this,void 0),S(this,Ze,e,"f"),this.options=o,this.response=t,this.body=n}hasNextPage(){return this.getPaginatedItems().length?this.nextPageRequestOptions()!=null:!1}async getNextPage(){let e=this.nextPageRequestOptions();if(!e)throw new x("No next page expected; please check `.hasNextPage()` before calling `.getNextPage()`.");return await _(this,Ze,"f").requestAPIList(this.constructor,e)}async*iterPages(){let e=this;for(yield e;e.hasNextPage();)e=await e.getNextPage(),yield e}async*[(Ze=new WeakMap,Symbol.asyncIterator)](){for await(let e of this.iterPages())for(let t of e.getPaginatedItems())yield t}},Me=class extends te{constructor(e,t,n){super(e,t,async(o,s)=>new n(o,s.response,await Ye(o,s),s.options))}async*[Symbol.asyncIterator](){let e=await this;for await(let t of e)yield t}},tt=class extends et{constructor(e,t,n,o){super(e,t,n,o),this.items=n||[],this.next_page_token=this.response.headers.get("x-next-page-token")??null,this.has_more=dt(this.response.headers.get("x-has-more"))??null}getPaginatedItems(){return this.items??[]}hasNextPage(){return this.has_more===!1?!1:super.hasNextPage()}nextPageRequestOptions(){let e=this.next_page_token;return e?{...this.options,query:{...ut(this.options.query),page_token:e}}:null}},w=class extends et{constructor(e,t,n,o){super(e,t,n,o),this.items=n||[],this.has_more=dt(this.response.headers.get("x-has-more"))??null,this.next_offset=Ht(this.response.headers.get("x-next-offset"))??null}getPaginatedItems(){return this.items??[]}hasNextPage(){return this.has_more===!1?!1:super.hasNextPage()}nextPageRequestOptions(){let e=this.next_offset;if(e==null){if(this.has_more)throw new x("Server reported X-Has-More: true without an X-Next-Offset header; refusing to silently truncate pagination");return null}return e===0?null:{...this.options,query:{...ut(this.options.query),offset:e}}}};var St=()=>{if(typeof File>"u"){let{process:r}=globalThis,e=typeof r?.versions?.node=="string"&&parseInt(r.versions.node.split("."))<20;throw new Error("`File` is not defined as a global, which is required for file uploads."+(e?" Update to Node 20 LTS or newer, or set `globalThis.File` to `import('node:buffer').File`.":""))}};function be(r,e,t){return St(),new File(r,e??"unknown_file",t)}function Ke(r){return(typeof r=="object"&&r!==null&&("name"in r&&r.name&&String(r.name)||"url"in r&&r.url&&String(r.url)||"filename"in r&&r.filename&&String(r.filename)||"path"in r&&r.path&&String(r.path))||"").split(/[\\/]/).pop()||void 0}var rt=r=>r!=null&&typeof r=="object"&&typeof r[Symbol.asyncIterator]=="function";var B=async(r,e)=>({...r,body:await Kr(r.body,e)}),ur=new WeakMap;function Mr(r){let e=typeof r=="function"?r:r.fetch,t=ur.get(e);if(t)return t;let n=(async()=>{try{let o="Response"in e?e.Response:(await e("data:,")).constructor,s=new FormData;return s.toString()!==await new o(s).text()}catch{return!0}})();return ur.set(e,n),n}var Kr=async(r,e)=>{if(!await Mr(e))throw new TypeError("The provided fetch function does not support file uploads with the current global FormData class.");let t=new FormData;return await Promise.all(Object.entries(r||{}).map(([n,o])=>Et(t,n,o))),t},fr=r=>r instanceof Blob&&"name"in r,qr=r=>typeof r=="object"&&r!==null&&(r instanceof Response||rt(r)||fr(r)),Pt=r=>{if(qr(r))return!0;if(Array.isArray(r))return r.some(Pt);if(r&&typeof r=="object"){for(let e in r)if(Pt(r[e]))return!0}return!1},Et=async(r,e,t)=>{if(t!==void 0){if(t==null)throw new TypeError(`Received null for "${e}"; to pass null in FormData, you must use the string 'null'`);if(typeof t=="string"||typeof t=="number"||typeof t=="boolean")r.append(e,String(t));else if(t instanceof Response)r.append(e,be([await t.blob()],Ke(t)));else if(rt(t))r.append(e,be([await new Response(Ve(t)).blob()],Ke(t)));else if(fr(t))r.append(e,t,Ke(t));else if(Array.isArray(t))await Promise.all(t.map((n,o)=>Et(r,`${e}[${o}]`,n)));else if(typeof t=="object")!(e==="env_vars")&&!Pt(t)?Object.entries(t).filter(([s,a])=>a!==void 0).length>0&&r.append(e,JSON.stringify(t)):await Promise.all(Object.entries(t).map(([o,s])=>Et(r,`${e}[${o}]`,s)));else throw new TypeError(`Invalid value given to form, expected a string, number, boolean, object, Array, File or Blob but got ${t} instead`)}};var dr=r=>r!=null&&typeof r=="object"&&typeof r.size=="number"&&typeof r.type=="string"&&typeof r.text=="function"&&typeof r.slice=="function"&&typeof r.arrayBuffer=="function",Wr=r=>r!=null&&typeof r=="object"&&typeof r.name=="string"&&typeof r.lastModified=="number"&&dr(r),Xr=r=>r!=null&&typeof r=="object"&&typeof r.url=="string"&&typeof r.blob=="function";async function nt(r,e,t){if(St(),r=await r,Wr(r))return r instanceof File?r:be([await r.arrayBuffer()],r.name);if(Xr(r)){let o=await r.blob();return e||(e=new URL(r.url).pathname.split(/[\\/]/).pop()),be(await It(o),e,t)}let n=await It(r);if(e||(e=Ke(r)),!t?.type){let o=n.find(s=>typeof s=="object"&&"type"in s&&s.type);typeof o=="string"&&(t={...t,type:o})}return be(n,e,t)}async function It(r){let e=[];if(typeof r=="string"||ArrayBuffer.isView(r)||r instanceof ArrayBuffer)e.push(r);else if(dr(r))e.push(r instanceof Blob?r:await r.arrayBuffer());else if(rt(r))for await(let t of r)e.push(...await It(t));else{let t=r?.constructor?.name;throw new Error(`Unexpected data type: ${typeof r}${t?`; constructor: ${t}`:""}${Vr(r)}`)}return e}function Vr(r){return typeof r!="object"||r===null?"":`; props: [${Object.getOwnPropertyNames(r).map(t=>`"${t}"`).join(", ")}]`}var p=class{constructor(e){this._client=e}};var pr=Symbol("brand.privateNullableHeaders");function*Jr(r){if(!r)return;if(pr in r){let{values:n,nulls:o}=r;yield*n.entries();for(let s of o)yield[s,null];return}let e=!1,t;r instanceof Headers?t=r.entries():lt(r)?t=r:(e=!0,t=Object.entries(r??{}));for(let n of t){let o=n[0];if(typeof o!="string")throw new TypeError("expected header name to be a string");let s=lt(n[1])?n[1]:[n[1]],a=!1;for(let c of s)c!==void 0&&(e&&!a&&(a=!0,yield[o,null]),yield[o,c])}}var u=r=>{let e=new Headers,t=new Set;for(let n of r){let o=new Set;for(let[s,a]of Jr(n)){let c=s.toLowerCase();o.has(c)||(e.delete(s),o.add(c)),a===null?(e.delete(s),t.add(c)):(e.append(s,a),t.delete(c))}}return{[pr]:!0,values:e,nulls:t}};function mr(r){return r.replace(/[^A-Za-z0-9\-._~!$&'()*+,;=:@]+/g,encodeURIComponent)}var hr=Object.freeze(Object.create(null)),Gr=(r=mr)=>function(t,...n){if(t.length===1)return t[0];let o=!1,s=[],a=t.reduce((m,l,g)=>{/[?#]/.test(l)&&(o=!0);let h=n[g],A=(o?encodeURIComponent:r)(""+h);return g!==n.length&&(h==null||typeof h=="object"&&h.toString===Object.getPrototypeOf(Object.getPrototypeOf(h.hasOwnProperty??hr)??hr)?.toString)&&(A=h+"",s.push({start:m.length+l.length,length:A.length,error:`Value of type ${Object.prototype.toString.call(h).slice(8,-1)} is not a valid path parameter`})),m+l+(g===n.length?"":A)},""),c=a.split(/[?#]/,1)[0],f=/(?<=^|\/)(?:\.|%2e){1,2}(?=\/|$)/gi,d;for(;(d=f.exec(c))!==null;)s.push({start:d.index,length:d[0].length,error:`Value "${d[0]}" can't be safely passed as a path parameter`});if(s.sort((m,l)=>m.start-l.start),s.length>0){let m=0,l=s.reduce((g,h)=>{let A=" ".repeat(h.start-m),E="^".repeat(h.length);return m=h.start+h.length,g+A+E},"");throw new x(`Path parameters result in path with invalid segments:
6
+ ${s.map(g=>g.error).join(`
7
+ `)}
8
+ ${a}
9
+ ${l}`)}return a},i=Gr(mr);var re=class extends p{create(e,t){return this._client.post("/org/api_keys",{body:e,...t})}retrieve(e,t={},n){return this._client.get(i`/org/api_keys/${e}`,{query:t,...n})}update(e,t,n){return this._client.patch(i`/org/api_keys/${e}`,{body:t,...n})}list(e={},t){return this._client.getAPIList("/org/api_keys",w,{query:e,...t})}delete(e,t){return this._client.delete(i`/org/api_keys/${e}`,{...t,headers:u([{Accept:"*/*"},t?.headers])})}rotate(e,t={},n){return this._client.post(i`/org/api_keys/${e}/rotate`,{body:t,...n})}};var ne=class extends p{list(e={},t){return this._client.getAPIList("/apps",w,{query:e,...t})}};var xe=class extends p{create(e,t){return this._client.post("/audit-logs/export/destinations",{body:e,...t})}retrieve(e,t){return this._client.get(i`/audit-logs/export/destinations/${e}`,t)}update(e,t,n){return this._client.patch(i`/audit-logs/export/destinations/${e}`,{body:t,...n})}list(e={},t){return this._client.getAPIList("/audit-logs/export/destinations",w,{query:e,...t})}delete(e,t){return this._client.delete(i`/audit-logs/export/destinations/${e}`,{...t,headers:u([{Accept:"*/*"},t?.headers])})}test(e,t){return this._client.post(i`/audit-logs/export/destinations/${e}/test`,t)}};var Yr=6,Zr=5e4,en=8e3,F=class extends x{};async function gr(r,e,t,n,o={}){if(!t||typeof t.write!="function")throw new TypeError("audit log download destination must provide write()");let{onProgress:s,maxTransferRetries:a=Yr,...c}=o;if(!Number.isInteger(a)||a<0)throw new TypeError("maxTransferRetries must be a non-negative integer");let f=c.timeout??n,d,m={bytesWritten:0,chunks:0,rows:0},l=new Set;for(;;){let g=await tn(r,d?{...e,cursor:d}:e,c,a,f),{nextCursor:h,hasMore:A,rows:E}=rn(g.headers,d);if(A&&h){if(l.has(h))throw new F("response repeated X-Next-Cursor header");l.add(h)}if(await sn(t,g.body),d=h,m.bytesWritten+=g.body.byteLength,m.chunks+=1,m.rows+=E,s&&await s({...m,chunkRows:E}),!A)return m}}async function tn(r,e,t,n,o){for(let s=0;;s+=1){let a=new AbortController,c=()=>a.abort();t.signal?.aborted?a.abort():t.signal?.addEventListener("abort",c,{once:!0});let f;try{f=await r(e,{...t,signal:a.signal})}catch(l){throw t.signal?.removeEventListener("abort",c),l}let d=!1,m=setTimeout(()=>{d=!0,a.abort()},o);try{let l=new Uint8Array(await f.arrayBuffer());if(clearTimeout(m),t.signal?.aborted)throw new T;if(d)throw new U;let g=f.headers.get("x-content-sha256");if(!g)throw new F("response missing X-Content-Sha256 header");let h=await nn(l);if(t.signal?.aborted)throw new T;if(h!==g)throw new F(`audit log chunk checksum mismatch (got ${h}, want ${g})`);return{body:l,headers:f.headers}}catch(l){if(t.signal?.aborted&&!(l instanceof T)?l=new T:d&&(l=new U),s===n||l instanceof T)throw l;await on(s+1,t.signal)}finally{clearTimeout(m),t.signal?.removeEventListener("abort",c)}}}function rn(r,e){let t=r.get("x-has-more");if(t!=="true"&&t!=="false")throw new F("response missing or invalid X-Has-More header");let n=t==="true",o=r.get("x-row-count");if(o===null||!/^[0-9]+$/.test(o))throw new F("response missing or invalid X-Row-Count header");let s=Number(o);if(!Number.isSafeInteger(s)||s>Zr)throw new F("response missing or invalid X-Row-Count header");let a=r.get("x-next-cursor")||void 0;if(n&&(!a||a===e))throw new F("response has invalid X-Next-Cursor header");if(!n&&a)throw new F("response returned a cursor after the final chunk");return{rows:s,nextCursor:a,hasMore:n}}async function nn(r){let e=await globalThis.crypto.subtle.digest("SHA-256",r);return Array.from(new Uint8Array(e),t=>t.toString(16).padStart(2,"0")).join("")}async function on(r,e){let t=Math.min(1e3*2**(r-1),en);await new Promise((n,o)=>{if(e?.aborted){o(new T);return}let s=()=>{clearTimeout(a),o(new T)},a=setTimeout(()=>{e?.removeEventListener("abort",s),n()},t);e?.addEventListener("abort",s,{once:!0})})}async function sn(r,e){let t=0;for(;t<e.byteLength;){let n=await r.write(t===0?e:e.subarray(t));if(typeof n=="number"){t+=yr(n,e.byteLength-t);continue}if(n&&typeof n=="object"&&"bytesWritten"in n){let o=n.bytesWritten;t+=yr(o,e.byteLength-t);continue}return}}function yr(r,e){if(!Number.isSafeInteger(r)||r<=0||r>e)throw new F("audit log download destination performed a short write");return r}var J=class extends p{constructor(){super(...arguments),this.exportDestinations=new xe(this._client)}list(e,t){return this._client.getAPIList("/audit-logs",tt,{query:e,...t})}exportChunk(e,t){return this._client.get("/audit-logs/export/chunk",{query:e,...t,headers:u([{Accept:"application/octet-stream"},t?.headers]),__binaryResponse:!0})}download(e,t,n){return gr((o,s)=>this.exportChunk(o,s),e,t,this._client.timeout,n)}};J.ExportDestinations=xe;var _e=class extends p{create(e,t){return this._client.post("/auth/connections",{body:e,...t})}retrieve(e,t){return this._client.get(i`/auth/connections/${e}`,t)}update(e,t,n){return this._client.patch(i`/auth/connections/${e}`,{body:t,...n})}list(e={},t){return this._client.getAPIList("/auth/connections",w,{query:e,...t})}delete(e,t){return this._client.delete(i`/auth/connections/${e}`,{...t,headers:u([{Accept:"*/*"},t?.headers])})}follow(e,t){return this._client.get(i`/auth/connections/${e}/events`,{...t,headers:u([{Accept:"text/event-stream"},t?.headers]),stream:!0})}login(e,t={},n){return this._client.post(i`/auth/connections/${e}/login`,{body:t,...n})}submit(e,t,n){return this._client.post(i`/auth/connections/${e}/submit`,{body:t,...n})}timeline(e,t={},n){return this._client.getAPIList(i`/auth/connections/${e}/timeline`,w,{query:t,...n})}};var Ae=class extends p{retrieve(e){return this._client.get("/auth/context",e)}};var M=class extends p{constructor(){super(...arguments),this.context=new Ae(this._client),this.connections=new _e(this._client)}};M.Context=Ae;M.Connections=_e;var oe=class extends p{create(e,t){return this._client.post("/browser_pools",{body:e,...t})}retrieve(e,t){return this._client.get(i`/browser_pools/${e}`,t)}update(e,t,n){return this._client.patch(i`/browser_pools/${e}`,{body:t,...n})}list(e={},t){return this._client.getAPIList("/browser_pools",w,{query:e,...t})}delete(e,t={},n){return this._client.delete(i`/browser_pools/${e}`,{body:t,...n,headers:u([{Accept:"*/*"},n?.headers])})}acquire(e,t,n){return this._client.post(i`/browser_pools/${e}/acquire`,{body:t,...n})}flush(e,t){return this._client.post(i`/browser_pools/${e}/flush`,{...t,headers:u([{Accept:"*/*"},t?.headers])})}release(e,t,n){return this._client.post(i`/browser_pools/${e}/release`,{body:t,...n,headers:u([{Accept:"*/*"},n?.headers])})}};var Re=class extends p{batch(e,t,n){return this._client.post(i`/browsers/${e}/computer/batch`,{body:t,...n,headers:u([{Accept:"*/*"},n?.headers])})}captureScreenshot(e,t={},n){return this._client.post(i`/browsers/${e}/computer/screenshot`,{body:t,...n,headers:u([{Accept:"image/png"},n?.headers]),__binaryResponse:!0})}clickMouse(e,t,n){return this._client.post(i`/browsers/${e}/computer/click_mouse`,{body:t,...n,headers:u([{Accept:"*/*"},n?.headers])})}dragMouse(e,t,n){return this._client.post(i`/browsers/${e}/computer/drag_mouse`,{body:t,...n,headers:u([{Accept:"*/*"},n?.headers])})}getMousePosition(e,t){return this._client.post(i`/browsers/${e}/computer/get_mouse_position`,t)}moveMouse(e,t,n){return this._client.post(i`/browsers/${e}/computer/move_mouse`,{body:t,...n,headers:u([{Accept:"*/*"},n?.headers])})}pressKey(e,t,n){return this._client.post(i`/browsers/${e}/computer/press_key`,{body:t,...n,headers:u([{Accept:"*/*"},n?.headers])})}readClipboard(e,t){return this._client.post(i`/browsers/${e}/computer/clipboard/read`,t)}scroll(e,t,n){return this._client.post(i`/browsers/${e}/computer/scroll`,{body:t,...n,headers:u([{Accept:"*/*"},n?.headers])})}setCursorVisibility(e,t,n){return this._client.post(i`/browsers/${e}/computer/cursor`,{body:t,...n})}typeText(e,t,n){return this._client.post(i`/browsers/${e}/computer/type`,{body:t,...n,headers:u([{Accept:"*/*"},n?.headers])})}writeClipboard(e,t,n){return this._client.post(i`/browsers/${e}/computer/clipboard/write`,{body:t,...n,headers:u([{Accept:"*/*"},n?.headers])})}};var Pe=class extends p{stream(e,t,n){return this._client.get(i`/browsers/${e}/logs/stream`,{query:t,...n,headers:u([{Accept:"text/event-stream"},n?.headers]),stream:!0})}};var Ee=class extends p{execute(e,t,n){return this._client.post(i`/browsers/${e}/playwright/execute`,{body:t,...n})}};var Se=class extends p{exec(e,t,n){return this._client.post(i`/browsers/${e}/process/exec`,{body:t,...n})}kill(e,t,n){let{id:o,...s}=t;return this._client.post(i`/browsers/${o}/process/${e}/kill`,{body:s,...n})}resize(e,t,n){let{id:o,...s}=t;return this._client.post(i`/browsers/${o}/process/${e}/resize`,{body:s,...n})}spawn(e,t,n){return this._client.post(i`/browsers/${e}/process/spawn`,{body:t,...n})}status(e,t,n){let{id:o}=t;return this._client.get(i`/browsers/${o}/process/${e}/status`,n)}stdin(e,t,n){let{id:o,...s}=t;return this._client.post(i`/browsers/${o}/process/${e}/stdin`,{body:s,...n})}stdoutStream(e,t,n){let{id:o}=t;return this._client.get(i`/browsers/${o}/process/${e}/stdout/stream`,{...n,headers:u([{Accept:"text/event-stream"},n?.headers]),stream:!0})}};var Ie=class extends p{list(e,t){return this._client.get(i`/browsers/${e}/replays`,t)}download(e,t,n){let{id:o}=t;return this._client.get(i`/browsers/${o}/replays/${e}`,{...n,headers:u([{Accept:"video/mp4"},n?.headers]),__binaryResponse:!0})}start(e,t={},n){return this._client.post(i`/browsers/${e}/replays`,{body:t,...n})}stop(e,t,n){let{id:o}=t;return this._client.post(i`/browsers/${o}/replays/${e}/stop`,{...n,headers:u([{Accept:"*/*"},n?.headers])})}};var $e=class extends p{events(e,t={},n){return this._client.getAPIList(i`/browsers/${e}/telemetry/events`,w,{query:t,...n})}stream(e,t={},n){let{"Last-Event-ID":o,...s}=t??{};return this._client.get(i`/browsers/${e}/telemetry/stream`,{query:s,...n,headers:u([{Accept:"text/event-stream",...o!=null?{"Last-Event-ID":o}:void 0},n?.headers]),stream:!0})}};var Le=class extends p{events(e,t,n){let{id:o}=t;return this._client.get(i`/browsers/${o}/fs/watch/${e}/events`,{...n,headers:u([{Accept:"text/event-stream"},n?.headers]),stream:!0})}start(e,t,n){return this._client.post(i`/browsers/${e}/fs/watch`,{body:t,...n})}stop(e,t,n){let{id:o}=t;return this._client.delete(i`/browsers/${o}/fs/watch/${e}`,{...n,headers:u([{Accept:"*/*"},n?.headers])})}};var se=class extends p{constructor(){super(...arguments),this.watch=new Le(this._client)}createDirectory(e,t,n){return this._client.put(i`/browsers/${e}/fs/create_directory`,{body:t,...n,headers:u([{Accept:"*/*"},n?.headers])})}deleteDirectory(e,t,n){return this._client.put(i`/browsers/${e}/fs/delete_directory`,{body:t,...n,headers:u([{Accept:"*/*"},n?.headers])})}deleteFile(e,t,n){return this._client.put(i`/browsers/${e}/fs/delete_file`,{body:t,...n,headers:u([{Accept:"*/*"},n?.headers])})}downloadDirZip(e,t,n){return this._client.get(i`/browsers/${e}/fs/download_dir_zip`,{query:t,...n,headers:u([{Accept:"application/zip"},n?.headers]),__binaryResponse:!0})}fileInfo(e,t,n){return this._client.get(i`/browsers/${e}/fs/file_info`,{query:t,...n})}listFiles(e,t,n){return this._client.get(i`/browsers/${e}/fs/list_files`,{query:t,...n})}move(e,t,n){return this._client.put(i`/browsers/${e}/fs/move`,{body:t,...n,headers:u([{Accept:"*/*"},n?.headers])})}readFile(e,t,n){return this._client.get(i`/browsers/${e}/fs/read_file`,{query:t,...n,headers:u([{Accept:"application/octet-stream"},n?.headers]),__binaryResponse:!0})}setFilePermissions(e,t,n){return this._client.put(i`/browsers/${e}/fs/set_file_permissions`,{body:t,...n,headers:u([{Accept:"*/*"},n?.headers])})}upload(e,t,n){return this._client.post(i`/browsers/${e}/fs/upload`,B({body:t,...n,headers:u([{Accept:"*/*"},n?.headers])},this._client))}uploadZip(e,t,n){return this._client.post(i`/browsers/${e}/fs/upload_zip`,B({body:t,...n,headers:u([{Accept:"*/*"},n?.headers])},this._client))}writeFile(e,t,n,o){let{path:s,mode:a}=n;return this._client.put(i`/browsers/${e}/fs/write_file`,{body:t,query:{path:s,mode:a},...o,headers:u([{"Content-Type":"application/octet-stream",Accept:"*/*"},o?.headers])})}};se.Watch=Le;function ot(r,e){return`${r.replace(/\/+$/,"")}${e.startsWith("/")?e:`/${e}`}`}async function wr(r,e,t,n){let o=r.browserRouteCache.get(e);if(!o)throw new x(`browser route cache does not contain session ${e}; create, retrieve, or list the browser before calling browser.fetch`);let{url:s,method:a,headers:c,body:f,signal:d,duplex:m,timeout_ms:l}=wn(t,n);bn(s);let g={url:s,jwt:o.jwt};l!==void 0&&(g.timeout_ms=l);let h=c.get("accept"),A={method:gn(a),path:ot(o.baseURL,"/curl/raw"),query:g,body:f,headers:u([{Authorization:null},h?{Accept:h}:{Accept:"*/*"},xn(c)]),signal:d??null,__binaryResponse:!0};return m&&(A.fetchOptions={duplex:m}),r.request(A).asResponse()}function gn(r){let e=r.toLowerCase();if(!new Set(["get","post","put","patch","delete"]).has(e))throw new x(`browser.fetch unsupported HTTP method: ${r}`);return e}function wn(r,e){let t=e&&"timeout_ms"in e?e.timeout_ms:void 0;if(r instanceof Request){let o=new Headers(r.headers);e?.headers&&new Headers(e.headers).forEach((d,m)=>{o.set(m,d)});let s={url:r.url,method:(e?.method??r.method)?.toUpperCase()||"GET",headers:o},a=e?.body??r.body;a!=null&&(s.body=a);let c=e?.signal??r.signal;return c!==void 0&&(s.signal=c),e?.duplex!==void 0&&(s.duplex=e.duplex),t!==void 0&&(s.timeout_ms=t),s}let n={url:r instanceof URL?r.href:String(r),method:(e?.method??"GET").toUpperCase(),headers:new Headers(e?.headers)};return e?.body!==void 0&&(n.body=e.body),e?.signal!==void 0&&(n.signal=e.signal),e?.duplex!==void 0&&(n.duplex=e.duplex),t!==void 0&&(n.timeout_ms=t),n}function bn(r){let e;try{e=new URL(r)}catch{throw new x(`browser.fetch target must be an absolute URL; received: ${r}`)}if(e.protocol!=="http:"&&e.protocol!=="https:")throw new x(`browser.fetch only supports http(s) URLs; received: ${e.protocol}`)}function xn(r){let e={};return r.forEach((t,n)=>{switch(n.toLowerCase()){case"accept":case"content-length":case"connection":case"keep-alive":case"proxy-authenticate":case"proxy-authorization":case"te":case"trailers":case"transfer-encoding":case"upgrade":return;default:e[n]=t}}),e}var O=class extends p{constructor(){super(...arguments),this.telemetry=new $e(this._client),this.replays=new Ie(this._client),this.fs=new se(this._client),this.process=new Se(this._client),this.logs=new Pe(this._client),this.computer=new Re(this._client),this.playwright=new Ee(this._client)}create(e={},t){return this._client.post("/browsers",{body:e,...t})}retrieve(e,t={},n){return this._client.get(i`/browsers/${e}`,{query:t,...n})}update(e,t,n){return this._client.patch(i`/browsers/${e}`,{body:t,...n})}list(e={},t){return this._client.getAPIList("/browsers",w,{query:e,...t})}curl(e,t,n){return this._client.post(i`/browsers/${e}/curl`,{body:t,...n})}fetch(e,t,n){return wr(this._client,e,t,n)}deleteByID(e,t){return this._client.delete(i`/browsers/${e}`,{...t,headers:u([{Accept:"*/*"},t?.headers])})}loadExtensions(e,t,n){return this._client.post(i`/browsers/${e}/extensions`,B({body:t,...n,headers:u([{Accept:"*/*"},n?.headers])},this._client))}};O.Telemetry=$e;O.Replays=Ie;O.Fs=se;O.Process=Se;O.Logs=Pe;O.Computer=Re;O.Playwright=Ee;var ie=class extends p{create(e,t){return this._client.post("/org/credential_providers",{body:e,...t})}retrieve(e,t){return this._client.get(i`/org/credential_providers/${e}`,t)}update(e,t,n){return this._client.patch(i`/org/credential_providers/${e}`,{body:t,...n})}list(e={},t){return this._client.getAPIList("/org/credential_providers",w,{query:e,...t})}delete(e,t){return this._client.delete(i`/org/credential_providers/${e}`,{...t,headers:u([{Accept:"*/*"},t?.headers])})}listItems(e,t){return this._client.get(i`/org/credential_providers/${e}/items`,t)}test(e,t){return this._client.post(i`/org/credential_providers/${e}/test`,t)}};var ae=class extends p{create(e,t){return this._client.post("/credentials",{body:e,...t})}retrieve(e,t){return this._client.get(i`/credentials/${e}`,t)}update(e,t,n){return this._client.patch(i`/credentials/${e}`,{body:t,...n})}list(e={},t){return this._client.getAPIList("/credentials",w,{query:e,...t})}delete(e,t){return this._client.delete(i`/credentials/${e}`,{...t,headers:u([{Accept:"*/*"},t?.headers])})}totpCode(e,t){return this._client.get(i`/credentials/${e}/totp-code`,t)}};var ce=class extends p{create(e,t){return this._client.post("/deployments",B({body:e,...t},this._client))}retrieve(e,t){return this._client.get(i`/deployments/${e}`,t)}list(e={},t){return this._client.getAPIList("/deployments",w,{query:e,...t})}delete(e,t){return this._client.delete(i`/deployments/${e}`,{...t,headers:u([{Accept:"*/*"},t?.headers])})}follow(e,t={},n){return this._client.get(i`/deployments/${e}/events`,{query:t,...n,headers:u([{Accept:"text/event-stream"},n?.headers]),stream:!0})}};var le=class extends p{list(e={},t){return this._client.getAPIList("/extensions",w,{query:e,...t})}delete(e,t){return this._client.delete(i`/extensions/${e}`,{...t,headers:u([{Accept:"*/*"},t?.headers])})}download(e,t){return this._client.get(i`/extensions/${e}`,{...t,headers:u([{Accept:"application/octet-stream"},t?.headers]),__binaryResponse:!0})}downloadFromChromeStore(e,t){return this._client.get("/extensions/from_chrome_store",{query:e,...t,headers:u([{Accept:"application/octet-stream"},t?.headers]),__binaryResponse:!0})}get(e,t){return this._client.get(i`/extensions/${e}/metadata`,t)}upload(e,t){return this._client.post("/extensions",B({body:e,...t},this._client))}};var ue=class extends p{create(e,t){return this._client.post("/invocations",{body:e,...t})}retrieve(e,t){return this._client.get(i`/invocations/${e}`,t)}update(e,t,n){return this._client.patch(i`/invocations/${e}`,{body:t,...n})}list(e={},t){return this._client.getAPIList("/invocations",w,{query:e,...t})}deleteBrowsers(e,t){return this._client.delete(i`/invocations/${e}/browsers`,{...t,headers:u([{Accept:"*/*"},t?.headers])})}follow(e,t={},n){return this._client.get(i`/invocations/${e}/events`,{query:t,...n,headers:u([{Accept:"text/event-stream"},n?.headers]),stream:!0})}listBrowsers(e,t){return this._client.get(i`/invocations/${e}/browsers`,t)}};var Te=class extends p{retrieve(e){return this._client.get("/org/entitlements",e)}};var Oe=class extends p{retrieve(e){return this._client.get("/org/limits",e)}update(e,t){return this._client.patch("/org/limits",{body:e,...t})}};var K=class extends p{constructor(){super(...arguments),this.entitlements=new Te(this._client),this.limits=new Oe(this._client)}};K.Entitlements=Te;K.Limits=Oe;var fe=class extends p{create(e,t){return this._client.post("/profiles",{body:e,...t})}retrieve(e,t){return this._client.get(i`/profiles/${e}`,t)}update(e,t,n){return this._client.patch(i`/profiles/${e}`,{body:t,...n})}list(e={},t){return this._client.getAPIList("/profiles",w,{query:e,...t})}delete(e,t){return this._client.delete(i`/profiles/${e}`,{...t,headers:u([{Accept:"*/*"},t?.headers])})}download(e,t={},n){return this._client.get(i`/profiles/${e}/download`,{query:t,...n,headers:u([{Accept:"application/octet-stream"},n?.headers]),__binaryResponse:!0})}};var je=class extends p{retrieve(e,t){return this._client.get(i`/org/projects/${e}/limits`,t)}update(e,t,n){return this._client.patch(i`/org/projects/${e}/limits`,{body:t,...n})}};var G=class extends p{constructor(){super(...arguments),this.limits=new je(this._client)}create(e,t){return this._client.post("/org/projects",{body:e,...t})}retrieve(e,t){return this._client.get(i`/org/projects/${e}`,t)}update(e,t,n){return this._client.patch(i`/org/projects/${e}`,{body:t,...n})}list(e={},t){return this._client.getAPIList("/org/projects",w,{query:e,...t})}delete(e,t){return this._client.delete(i`/org/projects/${e}`,{...t,headers:u([{Accept:"*/*"},t?.headers])})}};G.Limits=je;var de=class extends p{create(e,t){return this._client.post("/proxies",{body:e,...t})}retrieve(e,t){return this._client.get(i`/proxies/${e}`,t)}update(e,t,n){return this._client.patch(i`/proxies/${e}`,{body:t,...n})}list(e={},t){return this._client.getAPIList("/proxies",w,{query:e,...t})}delete(e,t){return this._client.delete(i`/proxies/${e}`,{...t,headers:u([{Accept:"*/*"},t?.headers])})}check(e,t={},n){return this._client.post(i`/proxies/${e}/check`,{body:t,...n})}};var Ce=class extends p{create(e,t){return this._client.post("/telemetry/destinations",{body:e,...t})}retrieve(e,t){return this._client.get(i`/telemetry/destinations/${e}`,t)}update(e,t,n){return this._client.patch(i`/telemetry/destinations/${e}`,{body:t,...n})}list(e={},t){return this._client.getAPIList("/telemetry/destinations",w,{query:e,...t})}delete(e,t){return this._client.delete(i`/telemetry/destinations/${e}`,{...t,headers:u([{Accept:"*/*"},t?.headers])})}};var Q=class extends p{constructor(){super(...arguments),this.destinations=new Ce(this._client)}};Q.Destinations=Ce;var qe=class{constructor(){this.entries=new Map}get(e){return this.entries.get(e)}set(e){this.entries.set(e.sessionId,e)}delete(e){this.entries.delete(e)}clear(){this.entries.clear()}},br="\u004b\u0045\u0052\u004e\u0045\u004c_BROWSER_ROUTING_SUBRESOURCES",Sn=["curl","telemetry/stream"],In=/^\/(?:v\d+\/)?browsers(?:\/[^/]+)?\/?$/,$n=/^\/(?:v\d+\/)?browser_pools\/[^/]+\/acquire\/?$/,Ln=/^\/(?:v\d+\/)?browsers\/([^/]+)\/?$/,Tn=/^\/(?:v\d+\/)?browser_pools\/[^/]+\/release\/?$/;function Lt(){let r=Kn();return r===void 0?[...Sn]:r.trim()===""?[]:r.split(",").map(e=>e.trim()).filter(Boolean)}function Tt(r,{apiBaseURL:e,subresources:t,cache:n}){let o=[...t].map(a=>a.trim().replace(/^\/+|\/+$/g,"")).filter(Boolean),s=new URL(e).origin;return async(a,c)=>{let f=new Request(a,c),d=On(f,s),m=await Dn(r,{input:a,init:c,request:f},s,o,n);return d&&await Un(m,n),await jn(f,m,s,n),m}}function On(r,e){let t=new URL(r.url);return t.origin===e&&(In.test(t.pathname)||$n.test(t.pathname))}async function jn(r,e,t,n){if(!e.ok)return;let o=new URL(r.url);if(o.origin!==t)return;let s=Cn(r,o.pathname)??await kn(r,o.pathname);s&&n.delete(s)}function Cn(r,e){if(r.method.toUpperCase()!=="DELETE")return;let t=e.match(Ln);return t&&decodeURIComponent(t[1]??"")||void 0}async function kn(r,e){if(!(r.method.toUpperCase()!=="POST"||!Tn.test(e)))try{let t=await r.clone().json();if(!t||typeof t!="object")return;let n=t.session_id;return typeof n=="string"&&n.trim()?n.trim():void 0}catch{return}}function Fn(r){if(!r||typeof r!="object")return;let e=r,t=typeof e.session_id=="string"?e.session_id.trim():"",n=typeof e.base_url=="string"?e.base_url.trim():"";if(!t||!n)return;let o=typeof e.jwt=="string"?e.jwt.trim():"",s=typeof e.cdp_ws_url=="string"?e.cdp_ws_url:void 0,a=o||Mn(s)||"";if(a)return{sessionId:t,baseURL:n,jwt:a}}async function Un(r,e){if((r.headers.get("content-type")?.toLowerCase()??"").includes("application/json"))try{$t(await r.clone().json(),e)}catch{}}function $t(r,e){let t=Fn(r);if(t&&e.set(t),Array.isArray(r)){for(let n of r)$t(n,e);return}if(!(!r||typeof r!="object"))for(let n of Object.values(r))typeof n=="object"&&n!==null&&$t(n,e)}function vn(r,e){let t=r.replace(/^\/+|\/+$/g,"");return e.some(n=>t===n||t.startsWith(n+"/"))}async function Dn(r,{input:e,init:t,request:n},o,s,a){let c=new URL(n.url);if(c.origin!==o)return r(e,t);let f=c.pathname.match(/^\/(?:v\d+\/)?browsers\/([^/]+)\/([^/]+)(\/.*)?$/);if(!f)return r(e,t);let d=decodeURIComponent(f[1]??""),m=f[2]??"";if(!d||!vn(m+(f[3]??""),s))return r(e,t);let l=a.get(d);if(l===void 0)return r(e,t);let g=new URL(ot(l.baseURL,`/${m}${f[3]??""}`));c.searchParams.forEach((A,E)=>{E!=="jwt"&&g.searchParams.append(E,A)}),g.searchParams.get("jwt")||g.searchParams.set("jwt",l.jwt);let h=new Headers(n.headers);return h.delete("authorization"),r(g.toString(),Nn(e,n,t,h))}function Nn(r,e,t,n){let o=e.method.toUpperCase(),s={...t??{},method:o,headers:n,redirect:e.redirect,signal:t?.signal??(r instanceof Request?r.signal:void 0)};if(delete s.body,delete s.duplex,o!=="GET"&&o!=="HEAD"){let a=Hn(e,t);a!==void 0&&(s.body=a),t?.duplex!==void 0?s.duplex=t.duplex:Bn(a)&&(s.duplex="half")}return s}function Hn(r,e){return e?.body!==void 0&&e.body!==null?e.body:r.body??void 0}function Bn(r){return globalThis.ReadableStream&&r instanceof globalThis.ReadableStream||typeof r=="object"&&r!==null&&Symbol.asyncIterator in r}function Mn(r){if(r)try{return new URL(r).searchParams.get("jwt")??void 0}catch{return}}function Kn(){if(typeof globalThis.process<"u"){let r=globalThis.process.env?.[br];return typeof r=="string"?r:void 0}if(typeof globalThis.Deno<"u"){let r=globalThis.Deno.env?.get?.(br);return typeof r=="string"?r:void 0}}var st=class{constructor(e){this.actions=new Map,this.name=e,xr.registerApp(this)}action(e,t){let n;if(t.length===0)throw new Error("Action handlers must accept at least the context parameter");if(t.length===1){let o=t;n=async(s,a)=>o(s)}else n=t;return this.actions.set(e,{name:e,handler:n}),n}getActions(){return Array.from(this.actions.values())}getAction(e){return this.actions.get(e)}toJSON(){return{name:this.name,actions:this.getActions().map(e=>({name:e.name}))}}},Ot=class{constructor(){this.apps=new Map}registerApp(e){this.apps.set(e.name,e)}getApps(){return Array.from(this.apps.values())}getAppByName(e){return this.apps.get(e)}export(){return{apps:this.getApps().map(e=>e.toJSON())}}exportJSON(){return JSON.stringify(this.export(),null,2)}},xr=new Ot;var ke=r=>{if(typeof globalThis.process<"u")return globalThis.process.env?.[r]?.trim()||void 0;if(typeof globalThis.Deno<"u")return globalThis.Deno.env?.get?.(r)?.trim()||void 0};var jt,Ct,We,_r,Ar={production:"https://api.on\u006b\u0065\u0072\u006e\u0065\u006c.com/",development:"https://localhost:3001/"},y=class{constructor({baseURL:e=ke("\u004b\u0045\u0052\u004e\u0045\u004c_BASE_URL"),apiKey:t=ke("\u004b\u0045\u0052\u004e\u0045\u004c_API_KEY"),projectID:n=null,project:o=null,...s}={}){if(jt.add(this),We.set(this,void 0),this.deployments=new ce(this),this.apps=new ne(this),this.invocations=new ue(this),this.browsers=new O(this),this.profiles=new fe(this),this.auth=new M(this),this.telemetry=new Q(this),this.proxies=new de(this),this.extensions=new le(this),this.browserPools=new oe(this),this.credentials=new ae(this),this.projects=new G(this),this.organization=new K(this),this.auditLogs=new J(this),this.apiKeys=new re(this),this.credentialProviders=new ie(this),typeof globalThis<"u"&&typeof globalThis.Bun<"u"&&globalThis.Bun.version&&!ke("\u004b\u0045\u0052\u004e\u0045\u004c_SUPPRESS_BUN_WARNING")&&P(this).warn("The Bun runtime was detected. Playwright may have CDP connection issues, proceed with caution. Suppress this warning by setting the \u004b\u0045\u0052\u004e\u0045\u004c_SUPPRESS_BUN_WARNING environment variable to true"),t===void 0)throw new x("The \u004b\u0045\u0052\u004e\u0045\u004c_API_KEY environment variable is missing or empty; either provide it, or instantiate the \u004b\u0065\u0072\u006e\u0065\u006c client with an apiKey option, like new \u004b\u0065\u0072\u006e\u0065\u006c({ apiKey: 'My API Key' }).");let a={apiKey:t,projectID:n,project:o,...s,baseURL:e,environment:s.environment??"production"};if(e&&s.environment)throw new x("Ambiguous URL; The `baseURL` option (or \u004b\u0045\u0052\u004e\u0045\u004c_BASE_URL env var) and the `environment` option are given. If you want to use the environment you must pass baseURL: null");this.baseURL=a.baseURL||Ar[a.environment||"production"],this.timeout=a.timeout??Ct.DEFAULT_TIMEOUT,this.logger=a.logger??console;let c="warn";this.logLevel=c,this.logLevel=At(a.logLevel,"ClientOptions.logLevel",this)??At(ke("\u004b\u0045\u0052\u004e\u0045\u004c_LOG"),"process.env['\u004b\u0045\u0052\u004e\u0045\u004c_LOG']",this)??c,this.fetchOptions=a.fetchOptions,this.maxRetries=a.maxRetries??2,this.rawFetch=a.fetch??Vt(),this.browserRouteCache=new qe,this.fetch=Tt(this.rawFetch,{apiBaseURL:this.baseURL,subresources:Lt(),cache:this.browserRouteCache}),S(this,We,Jt,"f");let f=ke("\u004b\u0045\u0052\u004e\u0045\u004c_CUSTOM_HEADERS");if(f){let d={};for(let m of f.split(`
10
+ `)){let l=m.indexOf(":");l>=0&&(d[m.substring(0,l).trim()]=m.substring(l+1).trim())}a.defaultHeaders={...d,...a.defaultHeaders}}this._options=a,this.apiKey=t,this.projectID=n,this.project=o}withOptions(e){let t=new this.constructor({...this._options,environment:e.environment?e.environment:void 0,baseURL:e.environment?void 0:this.baseURL,maxRetries:this.maxRetries,timeout:this.timeout,logger:this.logger,logLevel:this.logLevel,fetch:this.rawFetch,fetchOptions:this.fetchOptions,apiKey:this.apiKey,projectID:this.projectID,project:this.project,...e});return t.browserRouteCache=this.browserRouteCache,t.fetch=Tt(t.rawFetch,{apiBaseURL:t.baseURL,subresources:Lt(),cache:t.browserRouteCache}),t}defaultQuery(){return this._options.defaultQuery}validateHeaders({values:e,nulls:t}){}async authHeaders(e){return u([{Authorization:`Bearer ${this.apiKey}`}])}stringifyQuery(e){return or(e)}getUserAgent(){return`${this.constructor.name}/JS ${z}`}defaultIdempotencyKey(){return`stainless-node-retry-${ct()}`}makeStatusError(e,t,n,o){return I.generate(e,t,n,o)}buildURL(e,t,n){let o=!_(this,jt,"m",_r).call(this)&&n||this.baseURL,s=vt(e)?new URL(e):new URL(o+(o.endsWith("/")&&e.startsWith("/")?e.slice(1):e)),a=this.defaultQuery(),c=Object.fromEntries(s.searchParams);return(!ft(a)||!ft(c))&&(t={...c,...a,...t}),typeof t=="object"&&t&&!Array.isArray(t)&&(s.search=this.stringifyQuery(t)),s.toString()}async prepareOptions(e){}async prepareRequest(e,{url:t,options:n}){}get(e,t){return this.methodRequest("get",e,t)}post(e,t){return this.methodRequest("post",e,t)}patch(e,t){return this.methodRequest("patch",e,t)}put(e,t){return this.methodRequest("put",e,t)}delete(e,t){return this.methodRequest("delete",e,t)}methodRequest(e,t,n){return this.request(Promise.resolve(n).then(o=>({method:e,path:t,...o})))}request(e,t=null){return new te(this,this.makeRequest(e,t,void 0))}async makeRequest(e,t,n){let o=await e,s=o.maxRetries??this.maxRetries;t==null&&(t=s),await this.prepareOptions(o);let{req:a,url:c,timeout:f}=await this.buildRequest(o,{retryCount:s-t});await this.prepareRequest(a,{url:c,options:o});let d="log_"+(Math.random()*(1<<24)|0).toString(16).padStart(6,"0"),m=n===void 0?"":`, retryOf: ${n}`,l=Date.now();if(P(this).debug(`[${d}] sending request`,H({retryOfRequestLogID:n,method:o.method,url:c,options:o,headers:a.headers})),o.signal?.aborted)throw new T;let g=new AbortController,h=await this.fetchWithTimeout(c,a,f,g).catch(ve),A=Date.now();if(h instanceof globalThis.Error){let v=`retrying, ${t} attempts remaining`;if(o.signal?.aborted)throw new T;let k=Ue(h)||/timed? ?out/i.test(String(h)+("cause"in h?String(h.cause):""));if(t)return P(this).info(`[${d}] connection ${k?"timed out":"failed"} - ${v}`),P(this).debug(`[${d}] connection ${k?"timed out":"failed"} (${v})`,H({retryOfRequestLogID:n,url:c,durationMs:A-l,message:h.message})),this.retryRequest(o,t,n??d);throw P(this).info(`[${d}] connection ${k?"timed out":"failed"} - error; no more retries left`),P(this).debug(`[${d}] connection ${k?"timed out":"failed"} (error; no more retries left)`,H({retryOfRequestLogID:n,url:c,durationMs:A-l,message:h.message})),k?new U:new D({cause:h})}let E=`[${d}${m}] ${a.method} ${c} ${h.ok?"succeeded":"failed"} with status ${h.status} in ${A-l}ms`;if(!h.ok){let v=await this.shouldRetry(h);if(t&&v){let q=`retrying, ${t} attempts remaining`;return await zt(h.body),P(this).info(`${E} - ${q}`),P(this).debug(`[${d}] response error (${q})`,H({retryOfRequestLogID:n,url:h.url,status:h.status,headers:h.headers,durationMs:A-l})),this.retryRequest(o,t,n??d,h.headers)}let k=v?"error; no more retries left":"error; not retryable";P(this).info(`${E} - ${k}`);let b=await h.text().catch(q=>ve(q).message),Y=Bt(b),Z=Y?void 0:b;throw P(this).debug(`[${d}] response error (${k})`,H({retryOfRequestLogID:n,url:h.url,status:h.status,headers:h.headers,message:Z,durationMs:Date.now()-l})),this.makeStatusError(h.status,Y,Z,h.headers)}return P(this).info(E),P(this).debug(`[${d}] response start`,H({retryOfRequestLogID:n,url:h.url,status:h.status,headers:h.headers,durationMs:A-l})),{response:h,options:o,controller:g,requestLogID:d,retryOfRequestLogID:n,startTime:l}}getAPIList(e,t,n){return this.requestAPIList(t,n&&"then"in n?n.then(o=>({method:"get",path:e,...o})):{method:"get",path:e,...n})}requestAPIList(e,t){let n=this.makeRequest(t,null,void 0);return new Me(this,n,e)}async fetchWithTimeout(e,t,n,o){let{signal:s,method:a,...c}=t||{},f=this._makeAbort(o);s&&s.addEventListener("abort",f,{once:!0});let d=setTimeout(f,n),m=globalThis.ReadableStream&&c.body instanceof globalThis.ReadableStream||typeof c.body=="object"&&c.body!==null&&Symbol.asyncIterator in c.body,l={signal:o.signal,...m?{duplex:"half"}:{},method:"GET",...c};a&&(l.method=a.toUpperCase());try{return await this.fetch.call(void 0,e,l)}finally{clearTimeout(d)}}async shouldRetry(e){let t=e.headers.get("x-should-retry");return t==="true"?!0:t==="false"?!1:e.status===408||e.status===409||e.status===429||e.status>=500}async retryRequest(e,t,n,o){let s,a=o?.get("retry-after-ms");if(a){let f=parseFloat(a);Number.isNaN(f)||(s=f)}let c=o?.get("retry-after");if(c&&!s){let f=parseFloat(c);Number.isNaN(f)?s=Date.parse(c)-Date.now():s=f*1e3}if(s===void 0){let f=e.maxRetries??this.maxRetries;s=this.calculateDefaultRetryTimeoutMillis(t,f)}return await Mt(s),this.makeRequest(e,t-1,n)}calculateDefaultRetryTimeoutMillis(e,t){let s=t-e,a=Math.min(.5*Math.pow(2,s),8),c=1-Math.random()*.25;return a*c*1e3}async buildRequest(e,{retryCount:t=0}={}){let n={...e},{method:o,path:s,query:a,defaultBaseURL:c}=n,f=this.buildURL(s,a,c);"timeout"in n&&Nt("timeout",n.timeout),n.timeout=n.timeout??this.timeout;let{bodyHeaders:d,body:m}=this.buildBody({options:n}),l=await this.buildHeaders({options:e,method:o,bodyHeaders:d,retryCount:t});return{req:{method:o,headers:l,...n.signal&&{signal:n.signal},...globalThis.ReadableStream&&m instanceof globalThis.ReadableStream&&{duplex:"half"},...m&&{body:m},...this.fetchOptions??{},...n.fetchOptions??{}},url:f,timeout:n.timeout}}async buildHeaders({options:e,method:t,bodyHeaders:n,retryCount:o}){let s={};this.idempotencyHeader&&t!=="get"&&(e.idempotencyKey||(e.idempotencyKey=this.defaultIdempotencyKey()),s[this.idempotencyHeader]=e.idempotencyKey);let a=u([s,{Accept:"application/json","User-Agent":this.getUserAgent(),"X-Stainless-Retry-Count":String(o),...e.timeout?{"X-Stainless-Timeout":String(Math.trunc(e.timeout/1e3))}:{},...Xt(),"X-\u004b\u0065\u0072\u006e\u0065\u006c-Project-Id":this.projectID,"X-\u004b\u0065\u0072\u006e\u0065\u006c-Project":this.project},await this.authHeaders(e),this._options.defaultHeaders,n,e.headers]);return this.validateHeaders(a),a.values}_makeAbort(e){return()=>e.abort()}buildBody({options:e}){let{body:t,headers:n}=e;if(!t)return t==null&&"body"in e?_(this,We,"f").call(this,{body:t,headers:u([n])}):{bodyHeaders:void 0,body:void 0};let o=u([n]);return ArrayBuffer.isView(t)||t instanceof ArrayBuffer||t instanceof DataView||typeof t=="string"&&o.values.has("content-type")||globalThis.Blob&&t instanceof globalThis.Blob||t instanceof FormData||t instanceof URLSearchParams||globalThis.ReadableStream&&t instanceof globalThis.ReadableStream?{bodyHeaders:void 0,body:t}:typeof t=="object"&&(Symbol.asyncIterator in t||Symbol.iterator in t&&"next"in t&&typeof t.next=="function")?{bodyHeaders:void 0,body:Ve(t)}:typeof t=="object"&&o.values.get("content-type")==="application/x-www-form-urlencoded"?{bodyHeaders:{"content-type":"application/x-www-form-urlencoded"},body:this.stringifyQuery(t)}:_(this,We,"f").call(this,{body:t,headers:o})}app(e){return new st(e)}};Ct=y,We=new WeakMap,jt=new WeakSet,_r=function(){return this.baseURL!==Ar[this._options.environment||"production"]};y.\u004b\u0065\u0072\u006e\u0065\u006c=Ct;y.DEFAULT_TIMEOUT=6e4;y.\u004b\u0065\u0072\u006e\u0065\u006cError=x;y.APIError=I;y.APIConnectionError=D;y.APIConnectionTimeoutError=U;y.APIUserAbortError=T;y.NotFoundError=me;y.ConflictError=ye;y.RateLimitError=we;y.BadRequestError=he;y.AuthenticationError=W;y.InternalServerError=V;y.PermissionDeniedError=X;y.UnprocessableEntityError=ge;y.toFile=nt;y.Deployments=ce;y.Apps=ne;y.Invocations=ue;y.Browsers=O;y.Profiles=fe;y.Auth=M;y.Telemetry=Q;y.Proxies=de;y.Extensions=le;y.BrowserPools=oe;y.Credentials=ae;y.Projects=G;y.Organization=K;y.AuditLogs=J;y.APIKeys=re;y.CredentialProviders=ie;var qn="SERP browser service is not configured";function Cc(r,e){return Wn(e)||r instanceof W||r instanceof X||r instanceof D||r instanceof U||r instanceof V||/^401\s+Invalid or disabled API key\s*$/i.test(e.trim())||e===qn}function Wn(r){return/(?:401|403)\s+Auth Failed\s*\((?:customer_suspended|customer_inactive|invalid_auth|auth_failed)\)/i.test(r)}export{y as a,Cc as b,Wn as c};
@@ -1,4 +1,4 @@
1
- import{a as ot}from"./chunk-U4WIJZCX.js";import{G as fe,a as Qe,c as Ve}from"./chunk-JNPM3U3L.js";import{g as Ke,h as Ze}from"./chunk-KXJCGV3H.js";import{c as st}from"./chunk-JMZIDHYQ.js";import{c as tt,e as nt,f as rt,g as it}from"./chunk-CNSVZTTT.js";import{c as et}from"./chunk-K3VDGJNK.js";import{h as Ge}from"./chunk-C7MGJUTU.js";import{a as se}from"./chunk-WNUXMPVX.js";import{createWriteStream as ae,mkdtempSync as Yt,mkdirSync as le,readFileSync as Wt,readdirSync as Gt,rmSync as Kt,statSync as Zt,writeFileSync as y}from"fs";import{createHash as W}from"crypto";import{join as l}from"path";import{tmpdir as Qt}from"os";import{createGzip as Vt}from"zlib";import{once as G}from"events";import{finished as ue}from"stream/promises";import{ZipFile as en}from"yazl";import tn from"p-limit";var vt=new Set(["browser_navigation_blocked","browser_navigation_failed","browser_timeout","browser_session_failed","browser_result_missing"]);function Mt(e){return e instanceof Error?e.code:void 0}function At(e){return e instanceof Error?e.message:String(e)}function at(e){let r=e?.match(/^browser_http_(\d+)$/);return r?Number(r[1]):null}function It(e){return e===404?"page_not_found":e===403?"page_forbidden":e===429?"page_rate_limited":e>=500&&e<=599?"page_server_error":"page_http_error"}function Rt(e){let r=At(e),i=Mt(e);if(ot(e,r))return{errorCode:"vendor_unavailable",httpStatus:null,retryable:!0};let a=at(i)??at(r.match(/^(browser_http_\d+)/)?.[1]);return a!=null?{errorCode:It(a),httpStatus:a,retryable:a===429||a>=500}:i==="browser_challenge_unresolved"||/^browser_challenge_unresolved/.test(r)?{errorCode:"bot_check_unresolved",httpStatus:null,retryable:!0}:i==="browser_response_too_large"||/exceeds \d+ byte limit/i.test(r)?{errorCode:"page_too_large",httpStatus:null,retryable:!1}:/browser (?:has been )?closed|context closed|target page, context or browser has been closed|session closed/i.test(r)?{errorCode:"browser_session_interrupted",httpStatus:null,retryable:!0}:vt.has(i??"")||/timeout|navigation_failed|ERR_/i.test(r)?{errorCode:"page_unreachable",httpStatus:null,retryable:!0}:{errorCode:"extraction_failed",httpStatus:null,retryable:!1}}function lt(e,r){switch(e){case"page_not_found":return`The page could not be found (HTTP ${r??404}). It may have been moved or deleted.`;case"page_forbidden":return`The site refused the request (HTTP ${r??403}).`;case"page_rate_limited":return"The site is rate-limiting requests right now (HTTP 429). Retrying after a short wait usually works.";case"page_server_error":return`The page's own server returned an error (HTTP ${r}). This is on the target site's side, not something a retry here can fix.`;case"page_http_error":return`The page returned an unexpected HTTP ${r} response.`;case"bot_check_unresolved":return"This site has automated-traffic protection (a bot/CAPTCHA check) that could not be resolved in time. Some sites are simply not extractable this way.";case"page_too_large":return"The page is larger than we can safely process.";case"page_unreachable":return"The page did not respond in time, or the connection could not be completed.";case"browser_session_interrupted":return"The browser session closed before extraction completed. A retry uses a fresh browser session.";case"vendor_unavailable":return"MCP Scraper's internal browser services require MCP Scraper team attention. Servers or IPs are down until this is fixed \u2014 this is not caused by your request. Please retry in a few minutes.";case"extraction_failed":return"The page could not be extracted. Check retryable before attempting it again; contact support if this persists for the same URL."}}function on(e,r={}){let i=lt(e.errorCode,e.httpStatus);return{...Ge({errorCode:e.errorCode,retryable:e.retryable,chargeStatus:r.chargeStatus,details:{...r.details??{},...e.httpStatus!=null?{http_status:e.httpStatus}:{}}}),message:i,error:i}}function an(e){return e.httpStatus===404?404:e.httpStatus===403?403:e.httpStatus===429?429:e.httpStatus!=null&&e.httpStatus>=500&&e.httpStatus<=599?500:e.httpStatus!=null?502:e.errorCode==="vendor_unavailable"?503:502}var Dt="wayback_capture_missing";function Me(e,r){if(e==null)return{failureCode:null,failureReason:null};if(e===Dt)return{failureCode:e,failureReason:r??null};let i=new Error(r??e);i.code=e;let a=Rt(i);return{failureCode:a.errorCode,failureReason:lt(a.errorCode,a.httpStatus)}}import{createHash as Ie}from"crypto";import{neon as $t}from"@neondatabase/serverless";import{waitUntil as Ut}from"@vercel/functions";var jt="jina",ye=null,ut=!1;function be(){return(process.env.JINA_EMBED_MODEL??"jina-embeddings-v5-omni-small").trim()}function _e(){return Number((process.env.JINA_EMBED_DIM??"1024").trim())}function Ae(){return!!(process.env.JINA_API_KEY?.trim()&&process.env.MEMORY_DATABASE_URL?.trim())}function oe(){if(ye)return ye;let e=process.env.MEMORY_DATABASE_URL?.trim();if(!e)throw new Error("MEMORY_DATABASE_URL is not set; Commons semantic search needs the shared Postgres.");return ye=$t(e),ye}async function mt(){if(ut)return;let e=_e();await oe().query("CREATE EXTENSION IF NOT EXISTS vector"),await oe().query(`
1
+ import{c as ot}from"./chunk-JMZIDHYQ.js";import{G as fe,a as Ve,c as et}from"./chunk-NQFTETVC.js";import{g as Ze,h as Qe}from"./chunk-KXJCGV3H.js";import{b as Ge}from"./chunk-C3JWX6KM.js";import{c as nt,e as rt,f as it,g as st}from"./chunk-CNSVZTTT.js";import{c as tt}from"./chunk-K3VDGJNK.js";import{h as Ke}from"./chunk-C7MGJUTU.js";import{a as se}from"./chunk-WNUXMPVX.js";import{createWriteStream as ae,mkdtempSync as Yt,mkdirSync as le,readFileSync as Wt,readdirSync as Gt,rmSync as Kt,statSync as Zt,writeFileSync as y}from"fs";import{createHash as W}from"crypto";import{join as l}from"path";import{tmpdir as Qt}from"os";import{createGzip as Vt}from"zlib";import{once as G}from"events";import{finished as ue}from"stream/promises";import{ZipFile as en}from"yazl";import tn from"p-limit";var vt=new Set(["browser_navigation_blocked","browser_navigation_failed","browser_timeout","browser_session_failed","browser_result_missing"]);function Mt(e){return e instanceof Error?e.code:void 0}function At(e){return e instanceof Error?e.message:String(e)}function at(e){let r=e?.match(/^browser_http_(\d+)$/);return r?Number(r[1]):null}function It(e){return e===404?"page_not_found":e===403?"page_forbidden":e===429?"page_rate_limited":e>=500&&e<=599?"page_server_error":"page_http_error"}function Rt(e){let r=At(e),i=Mt(e);if(Ge(e,r))return{errorCode:"vendor_unavailable",httpStatus:null,retryable:!0};let a=at(i)??at(r.match(/^(browser_http_\d+)/)?.[1]);return a!=null?{errorCode:It(a),httpStatus:a,retryable:a===429||a>=500}:i==="browser_challenge_unresolved"||/^browser_challenge_unresolved/.test(r)?{errorCode:"bot_check_unresolved",httpStatus:null,retryable:!0}:i==="browser_response_too_large"||/exceeds \d+ byte limit/i.test(r)?{errorCode:"page_too_large",httpStatus:null,retryable:!1}:/browser (?:has been )?closed|context closed|target page, context or browser has been closed|session closed/i.test(r)?{errorCode:"browser_session_interrupted",httpStatus:null,retryable:!0}:vt.has(i??"")||/timeout|navigation_failed|ERR_/i.test(r)?{errorCode:"page_unreachable",httpStatus:null,retryable:!0}:{errorCode:"extraction_failed",httpStatus:null,retryable:!1}}function lt(e,r){switch(e){case"page_not_found":return`The page could not be found (HTTP ${r??404}). It may have been moved or deleted.`;case"page_forbidden":return`The site refused the request (HTTP ${r??403}).`;case"page_rate_limited":return"The site is rate-limiting requests right now (HTTP 429). Retrying after a short wait usually works.";case"page_server_error":return`The page's own server returned an error (HTTP ${r}). This is on the target site's side, not something a retry here can fix.`;case"page_http_error":return`The page returned an unexpected HTTP ${r} response.`;case"bot_check_unresolved":return"This site has automated-traffic protection (a bot/CAPTCHA check) that could not be resolved in time. Some sites are simply not extractable this way.";case"page_too_large":return"The page is larger than we can safely process.";case"page_unreachable":return"The page did not respond in time, or the connection could not be completed.";case"browser_session_interrupted":return"The browser session closed before extraction completed. A retry uses a fresh browser session.";case"vendor_unavailable":return"MCP Scraper's internal browser services require MCP Scraper team attention. Servers or IPs are down until this is fixed \u2014 this is not caused by your request. Please retry in a few minutes.";case"extraction_failed":return"The page could not be extracted. Check retryable before attempting it again; contact support if this persists for the same URL."}}function on(e,r={}){let i=lt(e.errorCode,e.httpStatus);return{...Ke({errorCode:e.errorCode,retryable:e.retryable,chargeStatus:r.chargeStatus,details:{...r.details??{},...e.httpStatus!=null?{http_status:e.httpStatus}:{}}}),message:i,error:i}}function an(e){return e.httpStatus===404?404:e.httpStatus===403?403:e.httpStatus===429?429:e.httpStatus!=null&&e.httpStatus>=500&&e.httpStatus<=599?500:e.httpStatus!=null?502:e.errorCode==="vendor_unavailable"?503:502}var Dt="wayback_capture_missing";function Me(e,r){if(e==null)return{failureCode:null,failureReason:null};if(e===Dt)return{failureCode:e,failureReason:r??null};let i=new Error(r??e);i.code=e;let a=Rt(i);return{failureCode:a.errorCode,failureReason:lt(a.errorCode,a.httpStatus)}}import{createHash as Ie}from"crypto";import{neon as $t}from"@neondatabase/serverless";import{waitUntil as Ut}from"@vercel/functions";var jt="jina",ye=null,ut=!1;function be(){return(process.env.JINA_EMBED_MODEL??"jina-embeddings-v5-omni-small").trim()}function _e(){return Number((process.env.JINA_EMBED_DIM??"1024").trim())}function Ae(){return!!(process.env.JINA_API_KEY?.trim()&&process.env.MEMORY_DATABASE_URL?.trim())}function oe(){if(ye)return ye;let e=process.env.MEMORY_DATABASE_URL?.trim();if(!e)throw new Error("MEMORY_DATABASE_URL is not set; Commons semantic search needs the shared Postgres.");return ye=$t(e),ye}async function mt(){if(ut)return;let e=_e();await oe().query("CREATE EXTENSION IF NOT EXISTS vector"),await oe().query(`
2
2
  CREATE TABLE IF NOT EXISTS commons_index_vectors (
3
3
  document_id TEXT PRIMARY KEY,
4
4
  entity_id TEXT NOT NULL,
@@ -21,13 +21,13 @@ ${S.text}`));for(let[S,_]of a.entries()){let C=`[${h[S].join(",")}]`;await oe().
21
21
 
22
22
  `).trim(),v=C.length>=60?C:h.bodyMarkdown;return`${h.title?.trim()||"Untitled page"}
23
23
 
24
- ${v}`}),minimumPageCount:i,removedBlockSignatures:s.size,removedCharacters:p}}var Re=class{parent;constructor(r){this.parent=Array.from({length:r},(i,a)=>a)}find(r){let i=this.parent[r];return i!==r&&(this.parent[r]=this.find(i)),this.parent[r]}union(r,i){let a=this.find(r),s=this.find(i);a!==s&&(this.parent[s]=a)}};async function pt(e,r={}){let i=e.filter(o=>o.bodyMarkdown.trim().length>0).slice(0,Ot),a=Number.isFinite(r.threshold)?r.threshold:Bt,s=Number.isFinite(r.maxPairs)?r.maxPairs:qt,p=Math.max(0,Math.min(1,a)),M=Math.max(1,Math.min(Ft,Math.floor(s))),h=r.model??be(),S=r.dimensions??_e(),_=r.embedTexts??we,C=zt(i),v=[],P=new Map,I=[];for(let[o,f]of i.entries()){let w=f.contentHash||Ie("sha256").update(f.bodyMarkdown).digest("hex"),k=P.get(w);k==null&&(k=v.length,P.set(w,k),v.push(C.texts[o])),I.push(k)}let D=[];for(let o=0;o<v.length;o+=64)D.push(...await _(v.slice(o,o+64)));if(D.length!==v.length)throw new Error(`Embedding provider returned ${D.length} vectors for ${v.length} unique page bodies.`);let K=I.map(o=>D[o]),ce=new Re(i.length),L=[],O=[];for(let o=0;o<i.length;o++)for(let f=o+1;f<i.length;f++){let w=Jt(K[o],K[f]);L.push(w),!(w<p)&&(O.push({source:o,target:f,score:w}),ce.union(o,f))}O.sort((o,f)=>f.score-o.score||i[o.source].url.localeCompare(i[f.source].url)||i[o.target].url.localeCompare(i[f.target].url));let R=[...L].sort((o,f)=>o-f),U=o=>R.length?Se(R[Math.floor((R.length-1)*o)],6):null,j=new Map;for(let o=0;o<i.length;o++){let f=ce.find(o),w=j.get(f)??[];w.push(o),j.set(f,w)}let B=new Map,J=[...j.values()].filter(o=>o.length>1).sort((o,f)=>f.length-o.length||i[o[0]].url.localeCompare(i[f[0]].url)).map((o,f)=>{let w=`cluster-${String(f+1).padStart(3,"0")}`;for(let k of o)B.set(k,w);return{clusterId:w,pageCount:o.length,urls:o.map(k=>i[k].url).sort()}}),H=O.slice(0,M).map((o,f)=>{let w=i[o.source],k=i[o.target],q=Se(o.score,6);return{sourceUrl:w.url,sourceTitle:w.title,targetUrl:k.url,targetTitle:k.title,similarity:q,similarityPercent:Se(q*100,2),corpusPercentile:R.length<=1?100:Se((1-f/(R.length-1))*100,2),exactContentDuplicate:!!(w.contentHash&&w.contentHash===k.contentHash),sourceWordCount:w.wordCount,targetWordCount:k.wordCount,clusterId:B.get(o.source)??null}}),z=Ht(i),me=Ie("sha256").update(JSON.stringify({corpusSha256:z,model:h,dimensions:S,threshold:p,maxPairs:M,boilerplateRemoval:{method:"corpus_repeated_markdown_blocks",minimumPageCount:C.minimumPageCount,removedBlockSignatures:C.removedBlockSignatures}})).digest("hex");return{generatedAt:new Date().toISOString(),provider:"jina",model:h,dimensions:S,threshold:p,requestedMaxPairs:M,comparedPages:i.length,possiblePairs:i.length*Math.max(0,i.length-1)/2,qualifyingPairs:O.length,returnedPairs:H.length,pairsTruncated:O.length>H.length,corpusSha256:z,analysisSha256:me,scoreDistribution:{min:U(0),p25:U(.25),median:U(.5),p75:U(.75),p90:U(.9),max:U(1)},boilerplateRemoval:{method:"corpus_repeated_markdown_blocks",minimumPageCount:C.minimumPageCount,removedBlockSignatures:C.removedBlockSignatures,removedCharacters:C.removedCharacters},rows:H,clusters:J}}import{basename as Xt}from"path";function Y(e){return e.split("#")[0].replace(/\/$/,"")}function De(e){let r=e.replace(/^www\./,"").toLowerCase(),i=r.split(".");return i.length<=2?r:i.slice(-2).join(".")}function gt(e,r){try{let i=new URL(e);return Xt(i.pathname).replace(/[^a-zA-Z0-9._-]/g,"_").slice(0,80)||`image-${r}`}catch{return`image-${r}`}}function ht(){let e=new Map;return r=>{let i=r.replace(/^https?:\/\//,"").replace(/[^a-zA-Z0-9]+/g,"-").replace(/^-+|-+$/g,"").slice(0,80)||"page",a=e.get(i)??0;return e.set(i,a+1),a?`${i}-${a}`:i}}function ft(e){let r=e==null?"":String(e);return/[",\r\n]/.test(r)?`"${r.replace(/"/g,'""')}"`:r}function yt(e){return{source_url:e.sourceUrl,source_title:e.sourceTitle,target_url:e.targetUrl,target_title:e.targetTitle,similarity:e.similarity,similarity_percent:e.similarityPercent,corpus_percentile:e.corpusPercentile,exact_content_duplicate:e.exactContentDuplicate,source_word_count:e.sourceWordCount,target_word_count:e.targetWordCount,cluster_id:e.clusterId}}var $e=20,Ue=500,nn=8,bt=10*1024*1024,_t=100*1024*1024,je=25e4;async function Fn(e,r={}){let i=e.options.downloadImages===!0;if(e.userId==null)throw new Error("site extract artifact owner is missing");let a=Qe(String(e.userId)),s=Yt(l(Qt(),"extract-bundle-"));le(s,{recursive:!0}),i&&le(l(s,"images"),{recursive:!0});let p=e.options.captureRenderedDom===!0,M=e.options.semanticSimilarity===!0;p&&le(l(s,"rendered-dom"),{recursive:!0});try{let h=[],S=[],_=[],C=ht(),v=[],P=e.options.waybackTimeline,I=new Map;for await(let n of fe(e.id))for(let t of n){I.set(Y(t.url),t.status),h.push({url:t.url,status:t.status,title:t.title,titleLength:t.titleLength,titlePixels:t.titlePixels,metaDescription:t.metaDescription,metaDescLength:t.metaDescLength,h1:t.h1,h1_2:t.h1_2,h2Count:t.h2Count,extractionStatus:t.extractionStatus,indexable:t.indexable,indexabilityReason:t.indexabilityReason,canonicalUrl:t.canonicalUrl?t.url:null,wordCount:t.wordCount,contentHash:t.contentHash,imagesMissingAlt:t.imagesMissingAlt,schemaTypes:t.schemaTypes.length>0?["present"]:[],outlinks:[]});let u=t.pageId??W("sha256").update(t.archivedUrl??t.url).digest("hex"),E=l(s,"pages",u);le(E,{recursive:!0});let d=t.contentRef?await a(t.contentRef):null,g=d?.html??null,c=d?.markdown??t.bodyMarkdown??null;if(M&&t.extractionStatus==="successful"&&c?.trim()&&S.push({url:t.url,title:t.title,bodyMarkdown:c,contentHash:t.contentHash,wordCount:t.wordCount}),p&&g!=null){let We=`rendered-dom/${C(t.url)}.html.txt`;y(l(s,We),g),_.push({url:t.url,path:We,bytes:Buffer.byteLength(g),truncated:t.renderedDomTruncated===!0,sanitized:t.renderedDomSanitized===!0})}let m=g==null?null:`pages/${u}/page.html`,b=c==null?null:`pages/${u}/page.md`,x=c==null||!t.archiveRequestedMonth?null:`pages/${t.archiveRequestedMonth}/${u}.md`;g!=null&&m&&y(l(s,m),g),c!=null&&b&&y(l(s,b),c),c!=null&&x&&(le(l(s,"pages",t.archiveRequestedMonth),{recursive:!0}),y(l(s,x),c));let{bodyMarkdown:A,contentRef:ne,discoveryLinks:re,...Ct}=t,ie={version:"site-export-page.v1",...Ct,pageId:u,content:{htmlPath:m,markdownPath:b,htmlBytes:g==null?0:Buffer.byteLength(g),markdownBytes:c==null?0:Buffer.byteLength(c),htmlSha256:g==null?null:W("sha256").update(g).digest("hex"),markdownSha256:c==null?null:W("sha256").update(c).digest("hex")}},Xe=JSON.stringify(ie,null,2),Ye=`pages/${u}/page.json`;y(l(s,Ye),Xe),v.push({pageId:u,url:t.archivedUrl??t.url,jsonPath:Ye,htmlPath:m,markdownPath:b,legacyMarkdownPath:x,jsonSha256:W("sha256").update(Xe).digest("hex"),htmlSha256:ie.content.htmlSha256,markdownSha256:ie.content.markdownSha256,htmlBytes:ie.content.htmlBytes,markdownBytes:ie.content.markdownBytes})}let D=Vt(),K=ae(l(s,"links.jsonl.gz"));D.pipe(K);let ce=async n=>{D.write(n)||await G(D,"drain")},L=ae(l(s,"schema.jsonl")),O=async n=>{L.write(n)||await G(L,"drain")},R=ae(l(s,"failures.jsonl")),U=async n=>{R.write(n)||await G(R,"drain")},j=new Map,B=new Map,J=new Map,H=new Map,z=new Map,me=new Set,o=0,f=0,w=0,k=0,q=0,xe=!1,Ee="";try{Ee=De(new URL(e.startUrl).hostname)}catch{Ee=""}let wt=tn(nn),Ne=[],Le=[],Oe=[],Z=0,de=0,ke=0,St=n=>ke+n>_t?!1:(ke+=n,!0);for await(let n of fe(e.id))for(let t of n){if(t.schema?.length&&await O(JSON.stringify({url:t.url,schema:t.schema})+`
24
+ ${v}`}),minimumPageCount:i,removedBlockSignatures:s.size,removedCharacters:p}}var Re=class{parent;constructor(r){this.parent=Array.from({length:r},(i,a)=>a)}find(r){let i=this.parent[r];return i!==r&&(this.parent[r]=this.find(i)),this.parent[r]}union(r,i){let a=this.find(r),s=this.find(i);a!==s&&(this.parent[s]=a)}};async function pt(e,r={}){let i=e.filter(o=>o.bodyMarkdown.trim().length>0).slice(0,Ot),a=Number.isFinite(r.threshold)?r.threshold:Bt,s=Number.isFinite(r.maxPairs)?r.maxPairs:qt,p=Math.max(0,Math.min(1,a)),M=Math.max(1,Math.min(Ft,Math.floor(s))),h=r.model??be(),S=r.dimensions??_e(),_=r.embedTexts??we,C=zt(i),v=[],P=new Map,I=[];for(let[o,f]of i.entries()){let w=f.contentHash||Ie("sha256").update(f.bodyMarkdown).digest("hex"),k=P.get(w);k==null&&(k=v.length,P.set(w,k),v.push(C.texts[o])),I.push(k)}let D=[];for(let o=0;o<v.length;o+=64)D.push(...await _(v.slice(o,o+64)));if(D.length!==v.length)throw new Error(`Embedding provider returned ${D.length} vectors for ${v.length} unique page bodies.`);let K=I.map(o=>D[o]),ce=new Re(i.length),L=[],O=[];for(let o=0;o<i.length;o++)for(let f=o+1;f<i.length;f++){let w=Jt(K[o],K[f]);L.push(w),!(w<p)&&(O.push({source:o,target:f,score:w}),ce.union(o,f))}O.sort((o,f)=>f.score-o.score||i[o.source].url.localeCompare(i[f.source].url)||i[o.target].url.localeCompare(i[f.target].url));let R=[...L].sort((o,f)=>o-f),U=o=>R.length?Se(R[Math.floor((R.length-1)*o)],6):null,j=new Map;for(let o=0;o<i.length;o++){let f=ce.find(o),w=j.get(f)??[];w.push(o),j.set(f,w)}let B=new Map,J=[...j.values()].filter(o=>o.length>1).sort((o,f)=>f.length-o.length||i[o[0]].url.localeCompare(i[f[0]].url)).map((o,f)=>{let w=`cluster-${String(f+1).padStart(3,"0")}`;for(let k of o)B.set(k,w);return{clusterId:w,pageCount:o.length,urls:o.map(k=>i[k].url).sort()}}),H=O.slice(0,M).map((o,f)=>{let w=i[o.source],k=i[o.target],q=Se(o.score,6);return{sourceUrl:w.url,sourceTitle:w.title,targetUrl:k.url,targetTitle:k.title,similarity:q,similarityPercent:Se(q*100,2),corpusPercentile:R.length<=1?100:Se((1-f/(R.length-1))*100,2),exactContentDuplicate:!!(w.contentHash&&w.contentHash===k.contentHash),sourceWordCount:w.wordCount,targetWordCount:k.wordCount,clusterId:B.get(o.source)??null}}),z=Ht(i),me=Ie("sha256").update(JSON.stringify({corpusSha256:z,model:h,dimensions:S,threshold:p,maxPairs:M,boilerplateRemoval:{method:"corpus_repeated_markdown_blocks",minimumPageCount:C.minimumPageCount,removedBlockSignatures:C.removedBlockSignatures}})).digest("hex");return{generatedAt:new Date().toISOString(),provider:"jina",model:h,dimensions:S,threshold:p,requestedMaxPairs:M,comparedPages:i.length,possiblePairs:i.length*Math.max(0,i.length-1)/2,qualifyingPairs:O.length,returnedPairs:H.length,pairsTruncated:O.length>H.length,corpusSha256:z,analysisSha256:me,scoreDistribution:{min:U(0),p25:U(.25),median:U(.5),p75:U(.75),p90:U(.9),max:U(1)},boilerplateRemoval:{method:"corpus_repeated_markdown_blocks",minimumPageCount:C.minimumPageCount,removedBlockSignatures:C.removedBlockSignatures,removedCharacters:C.removedCharacters},rows:H,clusters:J}}import{basename as Xt}from"path";function Y(e){return e.split("#")[0].replace(/\/$/,"")}function De(e){let r=e.replace(/^www\./,"").toLowerCase(),i=r.split(".");return i.length<=2?r:i.slice(-2).join(".")}function gt(e,r){try{let i=new URL(e);return Xt(i.pathname).replace(/[^a-zA-Z0-9._-]/g,"_").slice(0,80)||`image-${r}`}catch{return`image-${r}`}}function ht(){let e=new Map;return r=>{let i=r.replace(/^https?:\/\//,"").replace(/[^a-zA-Z0-9]+/g,"-").replace(/^-+|-+$/g,"").slice(0,80)||"page",a=e.get(i)??0;return e.set(i,a+1),a?`${i}-${a}`:i}}function ft(e){let r=e==null?"":String(e);return/[",\r\n]/.test(r)?`"${r.replace(/"/g,'""')}"`:r}function yt(e){return{source_url:e.sourceUrl,source_title:e.sourceTitle,target_url:e.targetUrl,target_title:e.targetTitle,similarity:e.similarity,similarity_percent:e.similarityPercent,corpus_percentile:e.corpusPercentile,exact_content_duplicate:e.exactContentDuplicate,source_word_count:e.sourceWordCount,target_word_count:e.targetWordCount,cluster_id:e.clusterId}}var $e=20,Ue=500,nn=8,bt=10*1024*1024,_t=100*1024*1024,je=25e4;async function Fn(e,r={}){let i=e.options.downloadImages===!0;if(e.userId==null)throw new Error("site extract artifact owner is missing");let a=Ve(String(e.userId)),s=Yt(l(Qt(),"extract-bundle-"));le(s,{recursive:!0}),i&&le(l(s,"images"),{recursive:!0});let p=e.options.captureRenderedDom===!0,M=e.options.semanticSimilarity===!0;p&&le(l(s,"rendered-dom"),{recursive:!0});try{let h=[],S=[],_=[],C=ht(),v=[],P=e.options.waybackTimeline,I=new Map;for await(let n of fe(e.id))for(let t of n){I.set(Y(t.url),t.status),h.push({url:t.url,status:t.status,title:t.title,titleLength:t.titleLength,titlePixels:t.titlePixels,metaDescription:t.metaDescription,metaDescLength:t.metaDescLength,h1:t.h1,h1_2:t.h1_2,h2Count:t.h2Count,extractionStatus:t.extractionStatus,indexable:t.indexable,indexabilityReason:t.indexabilityReason,canonicalUrl:t.canonicalUrl?t.url:null,wordCount:t.wordCount,contentHash:t.contentHash,imagesMissingAlt:t.imagesMissingAlt,schemaTypes:t.schemaTypes.length>0?["present"]:[],outlinks:[]});let u=t.pageId??W("sha256").update(t.archivedUrl??t.url).digest("hex"),E=l(s,"pages",u);le(E,{recursive:!0});let d=t.contentRef?await a(t.contentRef):null,g=d?.html??null,c=d?.markdown??t.bodyMarkdown??null;if(M&&t.extractionStatus==="successful"&&c?.trim()&&S.push({url:t.url,title:t.title,bodyMarkdown:c,contentHash:t.contentHash,wordCount:t.wordCount}),p&&g!=null){let We=`rendered-dom/${C(t.url)}.html.txt`;y(l(s,We),g),_.push({url:t.url,path:We,bytes:Buffer.byteLength(g),truncated:t.renderedDomTruncated===!0,sanitized:t.renderedDomSanitized===!0})}let m=g==null?null:`pages/${u}/page.html`,b=c==null?null:`pages/${u}/page.md`,x=c==null||!t.archiveRequestedMonth?null:`pages/${t.archiveRequestedMonth}/${u}.md`;g!=null&&m&&y(l(s,m),g),c!=null&&b&&y(l(s,b),c),c!=null&&x&&(le(l(s,"pages",t.archiveRequestedMonth),{recursive:!0}),y(l(s,x),c));let{bodyMarkdown:A,contentRef:ne,discoveryLinks:re,...Ct}=t,ie={version:"site-export-page.v1",...Ct,pageId:u,content:{htmlPath:m,markdownPath:b,htmlBytes:g==null?0:Buffer.byteLength(g),markdownBytes:c==null?0:Buffer.byteLength(c),htmlSha256:g==null?null:W("sha256").update(g).digest("hex"),markdownSha256:c==null?null:W("sha256").update(c).digest("hex")}},Xe=JSON.stringify(ie,null,2),Ye=`pages/${u}/page.json`;y(l(s,Ye),Xe),v.push({pageId:u,url:t.archivedUrl??t.url,jsonPath:Ye,htmlPath:m,markdownPath:b,legacyMarkdownPath:x,jsonSha256:W("sha256").update(Xe).digest("hex"),htmlSha256:ie.content.htmlSha256,markdownSha256:ie.content.markdownSha256,htmlBytes:ie.content.htmlBytes,markdownBytes:ie.content.markdownBytes})}let D=Vt(),K=ae(l(s,"links.jsonl.gz"));D.pipe(K);let ce=async n=>{D.write(n)||await G(D,"drain")},L=ae(l(s,"schema.jsonl")),O=async n=>{L.write(n)||await G(L,"drain")},R=ae(l(s,"failures.jsonl")),U=async n=>{R.write(n)||await G(R,"drain")},j=new Map,B=new Map,J=new Map,H=new Map,z=new Map,me=new Set,o=0,f=0,w=0,k=0,q=0,xe=!1,Ee="";try{Ee=De(new URL(e.startUrl).hostname)}catch{Ee=""}let wt=tn(nn),Ne=[],Le=[],Oe=[],Z=0,de=0,ke=0,St=n=>ke+n>_t?!1:(ke+=n,!0);for await(let n of fe(e.id))for(let t of n){if(t.schema?.length&&await O(JSON.stringify({url:t.url,schema:t.schema})+`
25
25
  `),t.extractionStatus==="failed"){let{failureCode:d,failureReason:g}=Me(t.failureCode,t.failureReason);await U(JSON.stringify({url:t.url,status:t.status,via:t.via,failureCode:d,failureReason:g,fetchAttempts:t.fetchAttempts??[]})+`
26
- `)}if(t.imageLinks?.length)for(let[d,g]of t.imageLinks.entries()){let c=W("sha256").update(`${t.url}\0${g}`).digest("hex"),m={imageId:c,sourcePage:t.url,sourceUrl:g,path:null,status:"not_requested",mimeType:null,bytes:null,sha256:null,failureCode:null,failureReason:null,artifactId:null};if(Le.push(m),!i)continue;if(d>=$e){m.status="skipped_page_limit",m.failureCode="page_image_limit",m.failureReason=`Only the first ${$e} image candidates are downloaded per page.`;continue}if(Z>=Ue){m.status="skipped_site_limit",m.failureCode="site_image_limit",m.failureReason=`Only the first ${Ue} image candidates are downloaded per site.`;continue}Z++;let b=`${c}-${gt(g,d)}`,x=l(s,"images");Ne.push(wt(()=>st(g,x,b,{expectedType:"image",maxBytes:bt,consumeBytes:St})).then(async A=>{let ne=Wt(A.savedPath),re=await Ze({ownerId:String(e.userId),jobId:e.id,imageId:c,filename:b,contentType:A.mimeType??"application/octet-stream",content:ne,sourceUrl:g,sourcePage:t.url});Oe.push(re),m.status="downloaded",m.path=`images/${b}`,m.artifactId=re.key,m.mimeType=A.mimeType,m.bytes=A.sizeBytes,m.sha256=W("sha256").update(ne).digest("hex")},A=>{de++,m.status="failed",m.failureCode="image_download_failed",m.failureReason=(A instanceof Error?A.message:String(A)).replace(/\s+/g," ").slice(0,500)}))}let u=Y(t.url),E={int:0,ext:0};for(let d of t.outlinks??[]){let g=(d.rel??"").split(/\s+/).includes("nofollow"),c=Y(d.href),m=I.get(c)??null;if(await ce(JSON.stringify({from:t.url,to:d.href,anchor:d.anchor,rel:d.rel,internal:d.internal,nofollow:g,targetStatus:m})+`
27
- `),k++,q>=je){xe=!0;continue}if(q++,d.internal){if(E.int++,o++,j.has(c)||j.set(c,new Set),j.get(c).add(u),d.anchor){B.has(c)||B.set(c,new Map);let b=B.get(c);b.set(d.anchor,(b.get(d.anchor)??0)+1)}!g&&(I.get(c)===200||!I.has(c))&&(J.has(u)||J.set(u,new Set),J.get(u).add(c)),m!=null&&m>=400&&(w++,me.add(t.url))}else{E.ext++;let b="";try{b=De(new URL(d.href).hostname)}catch{continue}if(!b||b===Ee)continue;f++;let x=z.get(b)??{links:0,nofollow:0,pages:new Set};x.links++,g&&x.nofollow++,x.pages.add(t.url),z.set(b,x)}}H.set(t.url,E)}let xt=Promise.all([ue(K),ue(L),ue(R)]);D.end(),L.end(),R.end(),await xt,await Promise.all(Ne);let X=new Map,Pe=Y(e.startUrl);X.set(Pe,0);let Te=[Pe];for(;Te.length>0;){let n=Te.shift(),t=X.get(n);for(let u of J.get(n)??[])X.has(u)||(X.set(u,t+1),Te.push(u))}let Q=new Map;for(let n of h){let t=Y(n.url),u=j.get(t)??new Set,E=B.get(t),d=E?[...E.entries()].sort((c,m)=>m[1]-c[1]).slice(0,3).map(c=>c[0]):[],g=H.get(n.url)??{int:0,ext:0};Q.set(n.url,{url:n.url,inlinks:u.size,uniqueInlinks:u.size,outlinksInternal:g.int,outlinksExternal:g.ext,crawlDepth:X.has(t)?X.get(t):null,orphan:u.size===0&&t!==Pe,topAnchors:d})}let Be=rt(h,Q,me),V=it(e.startUrl,h,Be,Q),ee=Ve(e),F={jobId:e.id,startUrl:e.startUrl,status:e.successfulUrls===0?"failed":e.failedUrls>0||e.remainingUrls>0||ee.creditTruncated?"partial":"complete",...ee,discovered:e.totalUrls,attempted:e.attemptedUrls,successful:e.successfulUrls,failed:e.failedUrls,remaining:e.remainingUrls,linkAnalysis:{truncated:xe,analyzedEdges:q,streamedEdges:k,edgeCap:je},generatedAt:new Date().toISOString()};V=["## Crawl delivery",`- Discovered: ${F.discovered}`,`- Attempted: ${F.attempted}`,`- Successful: ${F.successful}`,`- Failed: ${F.failed}`,`- Remaining: ${F.remaining}`,`- Requested page cap: ${ee.requestedMaxPages}`,`- Funded page cap: ${ee.effectiveMaxPages}`,`- Credit-truncated: ${ee.creditTruncated?"yes":"no"}`,`- Link analysis: ${q}/${k} edges${xe?` (capped at ${je})`:""}`,"",V].join(`
26
+ `)}if(t.imageLinks?.length)for(let[d,g]of t.imageLinks.entries()){let c=W("sha256").update(`${t.url}\0${g}`).digest("hex"),m={imageId:c,sourcePage:t.url,sourceUrl:g,path:null,status:"not_requested",mimeType:null,bytes:null,sha256:null,failureCode:null,failureReason:null,artifactId:null};if(Le.push(m),!i)continue;if(d>=$e){m.status="skipped_page_limit",m.failureCode="page_image_limit",m.failureReason=`Only the first ${$e} image candidates are downloaded per page.`;continue}if(Z>=Ue){m.status="skipped_site_limit",m.failureCode="site_image_limit",m.failureReason=`Only the first ${Ue} image candidates are downloaded per site.`;continue}Z++;let b=`${c}-${gt(g,d)}`,x=l(s,"images");Ne.push(wt(()=>ot(g,x,b,{expectedType:"image",maxBytes:bt,consumeBytes:St})).then(async A=>{let ne=Wt(A.savedPath),re=await Qe({ownerId:String(e.userId),jobId:e.id,imageId:c,filename:b,contentType:A.mimeType??"application/octet-stream",content:ne,sourceUrl:g,sourcePage:t.url});Oe.push(re),m.status="downloaded",m.path=`images/${b}`,m.artifactId=re.key,m.mimeType=A.mimeType,m.bytes=A.sizeBytes,m.sha256=W("sha256").update(ne).digest("hex")},A=>{de++,m.status="failed",m.failureCode="image_download_failed",m.failureReason=(A instanceof Error?A.message:String(A)).replace(/\s+/g," ").slice(0,500)}))}let u=Y(t.url),E={int:0,ext:0};for(let d of t.outlinks??[]){let g=(d.rel??"").split(/\s+/).includes("nofollow"),c=Y(d.href),m=I.get(c)??null;if(await ce(JSON.stringify({from:t.url,to:d.href,anchor:d.anchor,rel:d.rel,internal:d.internal,nofollow:g,targetStatus:m})+`
27
+ `),k++,q>=je){xe=!0;continue}if(q++,d.internal){if(E.int++,o++,j.has(c)||j.set(c,new Set),j.get(c).add(u),d.anchor){B.has(c)||B.set(c,new Map);let b=B.get(c);b.set(d.anchor,(b.get(d.anchor)??0)+1)}!g&&(I.get(c)===200||!I.has(c))&&(J.has(u)||J.set(u,new Set),J.get(u).add(c)),m!=null&&m>=400&&(w++,me.add(t.url))}else{E.ext++;let b="";try{b=De(new URL(d.href).hostname)}catch{continue}if(!b||b===Ee)continue;f++;let x=z.get(b)??{links:0,nofollow:0,pages:new Set};x.links++,g&&x.nofollow++,x.pages.add(t.url),z.set(b,x)}}H.set(t.url,E)}let xt=Promise.all([ue(K),ue(L),ue(R)]);D.end(),L.end(),R.end(),await xt,await Promise.all(Ne);let X=new Map,Pe=Y(e.startUrl);X.set(Pe,0);let Te=[Pe];for(;Te.length>0;){let n=Te.shift(),t=X.get(n);for(let u of J.get(n)??[])X.has(u)||(X.set(u,t+1),Te.push(u))}let Q=new Map;for(let n of h){let t=Y(n.url),u=j.get(t)??new Set,E=B.get(t),d=E?[...E.entries()].sort((c,m)=>m[1]-c[1]).slice(0,3).map(c=>c[0]):[],g=H.get(n.url)??{int:0,ext:0};Q.set(n.url,{url:n.url,inlinks:u.size,uniqueInlinks:u.size,outlinksInternal:g.int,outlinksExternal:g.ext,crawlDepth:X.has(t)?X.get(t):null,orphan:u.size===0&&t!==Pe,topAnchors:d})}let Be=it(h,Q,me),V=st(e.startUrl,h,Be,Q),ee=et(e),F={jobId:e.id,startUrl:e.startUrl,status:e.successfulUrls===0?"failed":e.failedUrls>0||e.remainingUrls>0||ee.creditTruncated?"partial":"complete",...ee,discovered:e.totalUrls,attempted:e.attemptedUrls,successful:e.successfulUrls,failed:e.failedUrls,remaining:e.remainingUrls,linkAnalysis:{truncated:xe,analyzedEdges:q,streamedEdges:k,edgeCap:je},generatedAt:new Date().toISOString()};V=["## Crawl delivery",`- Discovered: ${F.discovered}`,`- Attempted: ${F.attempted}`,`- Successful: ${F.successful}`,`- Failed: ${F.failed}`,`- Remaining: ${F.remaining}`,`- Requested page cap: ${ee.requestedMaxPages}`,`- Funded page cap: ${ee.effectiveMaxPages}`,`- Credit-truncated: ${ee.creditTruncated?"yes":"no"}`,`- Link analysis: ${q}/${k} edges${xe?` (capped at ${je})`:""}`,"",V].join(`
28
28
  `),r.imageAudit&&(V+=`
29
29
 
30
- ${tt(r.imageAudit)}`);let pe=[...Q.values()],qe=n=>Math.round(n*10)/10,te={zero:0,oneToTwo:0,threeToTen:0,elevenPlus:0},Fe=0,Je=0;for(let n of pe)Fe+=n.inlinks,Je+=n.outlinksInternal+n.outlinksExternal,n.inlinks===0?te.zero++:n.inlinks<=2?te.oneToTwo++:n.inlinks<=10?te.threeToTen++:te.elevenPlus++;let Ce=[...z.entries()].map(([n,t])=>({domain:n,links:t.links,nofollow:t.nofollow,pages:t.pages.size})).sort((n,t)=>t.links-n.links),ve={summary:{internal:{totalLinks:o,pages:h.length,orphans:pe.filter(n=>n.orphan).length,brokenInternal:w,avgInlinks:h.length?qe(Fe/h.length):0,avgOutlinks:h.length?qe(Je/h.length):0,distribution:te,topByInlinks:[...pe].sort((n,t)=>t.inlinks-n.inlinks).slice(0,20).map(n=>({url:n.url,inlinks:n.inlinks,outlinksInternal:n.outlinksInternal,outlinksExternal:n.outlinksExternal}))},external:{totalLinks:f,uniqueDomains:Ce.length,topDomains:Ce.slice(0,20)}},externalDomains:Ce},ge=ae(l(s,"pages.jsonl")),Et=new Map(v.map(n=>[n.pageId,n])),$=P?ae(l(s,"capture-matrix.jsonl")):null,He=new Set,ze=(n,t)=>{let u=t;try{u=new URL(t).href}catch{}return`${n??""}\0${u}`};for await(let n of fe(e.id))for(let t of n){let u=t.extractionStatus==="failed"?Me(t.failureCode,t.failureReason):{failureCode:null,failureReason:null},{bodyMarkdown:E,schema:d,outlinks:g,contentRef:c,...m}=t,b=Q.get(t.url),x=t.pageId?Et.get(t.pageId):void 0,A={...m,...t.extractionStatus==="failed"?u:{},representations:x?{jsonPath:x.jsonPath,htmlPath:x.htmlPath,markdownPath:x.markdownPath,jsonSha256:x.jsonSha256,htmlSha256:x.htmlSha256,markdownSha256:x.markdownSha256,htmlBytes:x.htmlBytes,markdownBytes:x.markdownBytes}:null,inlinks:b?.inlinks??0,crawlDepth:b?.crawlDepth??null,orphan:b?.orphan??!1};if(ge.write(JSON.stringify(A)+`
30
+ ${nt(r.imageAudit)}`);let pe=[...Q.values()],qe=n=>Math.round(n*10)/10,te={zero:0,oneToTwo:0,threeToTen:0,elevenPlus:0},Fe=0,Je=0;for(let n of pe)Fe+=n.inlinks,Je+=n.outlinksInternal+n.outlinksExternal,n.inlinks===0?te.zero++:n.inlinks<=2?te.oneToTwo++:n.inlinks<=10?te.threeToTen++:te.elevenPlus++;let Ce=[...z.entries()].map(([n,t])=>({domain:n,links:t.links,nofollow:t.nofollow,pages:t.pages.size})).sort((n,t)=>t.links-n.links),ve={summary:{internal:{totalLinks:o,pages:h.length,orphans:pe.filter(n=>n.orphan).length,brokenInternal:w,avgInlinks:h.length?qe(Fe/h.length):0,avgOutlinks:h.length?qe(Je/h.length):0,distribution:te,topByInlinks:[...pe].sort((n,t)=>t.inlinks-n.inlinks).slice(0,20).map(n=>({url:n.url,inlinks:n.inlinks,outlinksInternal:n.outlinksInternal,outlinksExternal:n.outlinksExternal}))},external:{totalLinks:f,uniqueDomains:Ce.length,topDomains:Ce.slice(0,20)}},externalDomains:Ce},ge=ae(l(s,"pages.jsonl")),Et=new Map(v.map(n=>[n.pageId,n])),$=P?ae(l(s,"capture-matrix.jsonl")):null,He=new Set,ze=(n,t)=>{let u=t;try{u=new URL(t).href}catch{}return`${n??""}\0${u}`};for await(let n of fe(e.id))for(let t of n){let u=t.extractionStatus==="failed"?Me(t.failureCode,t.failureReason):{failureCode:null,failureReason:null},{bodyMarkdown:E,schema:d,outlinks:g,contentRef:c,...m}=t,b=Q.get(t.url),x=t.pageId?Et.get(t.pageId):void 0,A={...m,...t.extractionStatus==="failed"?u:{},representations:x?{jsonPath:x.jsonPath,htmlPath:x.htmlPath,markdownPath:x.markdownPath,jsonSha256:x.jsonSha256,htmlSha256:x.htmlSha256,markdownSha256:x.markdownSha256,htmlBytes:x.htmlBytes,markdownBytes:x.markdownBytes}:null,inlinks:b?.inlinks??0,crawlDepth:b?.crawlDepth??null,orphan:b?.orphan??!1};if(ge.write(JSON.stringify(A)+`
31
31
  `)||await G(ge,"drain"),$){let ne=ze(t.archiveRequestedMonth,t.originalUrl??t.url);He.add(ne);let re={requestedMonth:t.archiveRequestedMonth??null,captureTimestamp:t.archiveTimestamp??null,originalUrl:t.originalUrl??t.url,archivedUrl:t.archivedUrl??null,status:t.extractionStatus==="successful"?"captured":"failed",httpStatus:t.status,contentHash:t.contentHash||null,wordCount:t.wordCount,failureCode:u.failureCode,failureReason:u.failureReason};$.write(JSON.stringify(re)+`
32
32
  `)||await G($,"drain")}}if($&&P?.timeline?.urls){let n=P.timeline,t=n.urls,u=(()=>{if(n.months?.length)return[...new Set(n.months)].sort();if(!n.from||!n.to)return[];let E=[],d=new Date(`${n.from}-01T00:00:00Z`),g=new Date(`${n.to}-01T00:00:00Z`);for(;d<=g&&E.length<60;)E.push(`${d.getUTCFullYear()}-${String(d.getUTCMonth()+1).padStart(2,"0")}`),d.setUTCMonth(d.getUTCMonth()+(n.intervalMonths??1));return E})();for(let E of u)for(let d of t)He.has(ze(E,d))||$.write(JSON.stringify({requestedMonth:E,captureTimestamp:null,originalUrl:d,archivedUrl:null,status:"missing",httpStatus:null,contentHash:null,wordCount:0,failureCode:"wayback_capture_missing",failureReason:"No successful HTML capture was available in the requested month."})+`
33
33
  `)||await G($,"drain")}let kt=ue(ge),Pt=$?ue($):Promise.resolve();if(ge.end(),$?.end(),await Promise.all([kt,Pt]),p&&y(l(s,"rendered-dom","manifest.jsonl"),_.map(n=>JSON.stringify(n)).join(`
@@ -37,5 +37,5 @@ ${tt(r.imageAudit)}`);let pe=[...Q.values()],qe=n=>Math.round(n*10)/10,te={zero:
37
37
  `)),y(l(s,"similarity-table-schema.json"),JSON.stringify({tableNameSuggestion:`site_similarity_${new URL(e.startUrl).hostname.replace(/[^a-z0-9]+/gi,"_").replace(/^_+|_+$/g,"").toLowerCase()}`,defaultSort:[{column:"similarity",direction:"desc"}],columns:{source_url:"text",source_title:"text",target_url:"text",target_title:"text",similarity:"number",similarity_percent:"number",corpus_percentile:"number",exact_content_duplicate:"boolean",source_word_count:"integer",target_word_count:"integer",cluster_id:"text"}},null,2)),y(l(s,"similarity-summary.json"),JSON.stringify(c,null,2)),y(l(s,"content-clusters.json"),JSON.stringify(n.clusters,null,2)),V+=["","## Rendered content similarity",`- Compared pages: ${n.comparedPages}`,`- Raw cosine threshold: ${n.threshold}`,`- Corpus score distribution: min ${n.scoreDistribution.min??"n/a"} / median ${n.scoreDistribution.median??"n/a"} / p90 ${n.scoreDistribution.p90??"n/a"} / max ${n.scoreDistribution.max??"n/a"}`,`- Qualifying pairs: ${n.qualifyingPairs}`,`- Returned table rows: ${n.returnedPairs}${n.pairsTruncated?" (capped)":""}`,`- Multi-page clusters: ${n.clusters.length}`,`- Embedding model: ${n.model} (${n.dimensions} dimensions)`,`- Corpus boilerplate removed: ${n.boilerplateRemoval.removedBlockSignatures} repeated block signatures / ${n.boilerplateRemoval.removedCharacters} characters`,`- Corpus SHA-256: ${n.corpusSha256}`].join(`
38
38
  `)}y(l(s,"report.md"),V),y(l(s,"crawl-summary.json"),JSON.stringify(F,null,2)),y(l(s,"images-manifest.jsonl"),Le.map(n=>JSON.stringify(n)).join(`
39
39
  `)),y(l(s,"manifest.json"),JSON.stringify({version:"site-export.v1",jobId:e.id,startUrl:e.startUrl,generatedAt:F.generatedAt,outputProfile:{json:!0,html:!0,markdown:!0,imageManifest:!0,imageBytes:i},pages:[...v].sort((n,t)=>n.pageId.localeCompare(t.pageId)),imagesManifestPath:"images-manifest.jsonl",download:"This ZIP is the complete human export. AI clients should use site_export_read and site_export_image."},null,2)),P&&y(l(s,"wayback-manifest.json"),JSON.stringify({rootUrl:P.rootUrl??e.startUrl,requested:P.timeline??null,maxPagesPerSnapshot:P.maxPagesPerSnapshot??null,captureMatrix:"capture-matrix.jsonl"},null,2)),y(l(s,"issues.json"),JSON.stringify(Be,null,2)),y(l(s,"link-metrics.jsonl"),pe.map(n=>JSON.stringify(n)).join(`
40
- `)),y(l(s,"link-report.md"),nt(ve)),y(l(s,"links-summary.json"),JSON.stringify(ve.summary,null,2)),y(l(s,"external-domains.json"),JSON.stringify(ve.externalDomains,null,2)),r.imageAudit&&(y(l(s,"images.jsonl"),r.imageAudit.rows.map(n=>JSON.stringify(n)).join(`
41
- `)),y(l(s,"images-summary.json"),JSON.stringify(r.imageAudit.summary,null,2))),r.seoAudit&&y(l(s,"seo-audit.json"),JSON.stringify(r.seoAudit,null,2)),r.branding&&y(l(s,"branding.json"),JSON.stringify(r.branding,null,2)),i&&y(l(s,"images-download-summary.json"),JSON.stringify({queued:Z,downloaded:Z-de,failed:de,downloadedBytes:ke,perPageCap:$e,perSiteCap:Ue,perFileByteCap:bt,totalByteCap:_t},null,2));let T=[{name:"manifest.json",type:"application/json"},{name:"images-manifest.jsonl",type:"application/x-ndjson"},{name:"report.md",type:"text/markdown"},{name:"crawl-summary.json",type:"application/json"},{name:"failures.jsonl",type:"application/x-ndjson"},{name:"schema.jsonl",type:"application/x-ndjson"},{name:"issues.json",type:"application/json"},{name:"pages.jsonl",type:"application/x-ndjson"},{name:"links.jsonl.gz",type:"application/gzip"},{name:"link-metrics.jsonl",type:"application/x-ndjson"},{name:"link-report.md",type:"text/markdown"},{name:"links-summary.json",type:"application/json"},{name:"external-domains.json",type:"application/json"}];if(p){T.push({name:"rendered-dom/manifest.jsonl",type:"application/x-ndjson"});for(let n of _)T.push({name:n.path,type:"text/plain"})}M&&(T.push({name:"similarity-table.csv",type:"text/csv"}),T.push({name:"similarity.jsonl",type:"application/x-ndjson"}),T.push({name:"similarity-table-schema.json",type:"application/json"}),T.push({name:"similarity-summary.json",type:"application/json"}),T.push({name:"content-clusters.json",type:"application/json"})),P&&(T.push({name:"wayback-manifest.json",type:"application/json"}),T.push({name:"capture-matrix.jsonl",type:"application/x-ndjson"})),r.imageAudit&&(T.push({name:"images.jsonl",type:"application/x-ndjson"}),T.push({name:"images-summary.json",type:"application/json"})),r.seoAudit&&T.push({name:"seo-audit.json",type:"application/json"}),r.branding&&T.push({name:"branding.json",type:"application/json"}),i&&T.push({name:"images-download-summary.json",type:"application/json"});let N=new en,Tt=Ke({ownerId:String(e.userId),jobId:e.id,createdAt:new Date,content:N.outputStream});for(let n of T)N.addFile(l(s,n.name),n.name);let he=[...v].sort((n,t)=>n.pageId.localeCompare(t.pageId));for(let n of he)n.markdownPath&&N.addFile(l(s,n.markdownPath),n.markdownPath);for(let n of he)n.legacyMarkdownPath&&N.addFile(l(s,n.legacyMarkdownPath),n.legacyMarkdownPath);for(let n of he)n.htmlPath&&N.addFile(l(s,n.htmlPath),n.htmlPath);for(let n of he)N.addFile(l(s,n.jsonPath),n.jsonPath);if(i&&Z>de){let n=l(s,"images");for(let t of Gt(n,{recursive:!0})){let u=l(n,t);Zt(u).isFile()&&N.addFile(u,`images/${t.split("\\").join("/")}`)}}return N.end(),[await et("export_packaging",()=>Tt),...Oe]}finally{Kt(s,{recursive:!0,force:!0})}}export{Rt as a,on as b,an as c,Me as d,Ae as e,Lt as f,dn as g,ct as h,pn as i,je as j,Fn as k};
40
+ `)),y(l(s,"link-report.md"),rt(ve)),y(l(s,"links-summary.json"),JSON.stringify(ve.summary,null,2)),y(l(s,"external-domains.json"),JSON.stringify(ve.externalDomains,null,2)),r.imageAudit&&(y(l(s,"images.jsonl"),r.imageAudit.rows.map(n=>JSON.stringify(n)).join(`
41
+ `)),y(l(s,"images-summary.json"),JSON.stringify(r.imageAudit.summary,null,2))),r.seoAudit&&y(l(s,"seo-audit.json"),JSON.stringify(r.seoAudit,null,2)),r.branding&&y(l(s,"branding.json"),JSON.stringify(r.branding,null,2)),i&&y(l(s,"images-download-summary.json"),JSON.stringify({queued:Z,downloaded:Z-de,failed:de,downloadedBytes:ke,perPageCap:$e,perSiteCap:Ue,perFileByteCap:bt,totalByteCap:_t},null,2));let T=[{name:"manifest.json",type:"application/json"},{name:"images-manifest.jsonl",type:"application/x-ndjson"},{name:"report.md",type:"text/markdown"},{name:"crawl-summary.json",type:"application/json"},{name:"failures.jsonl",type:"application/x-ndjson"},{name:"schema.jsonl",type:"application/x-ndjson"},{name:"issues.json",type:"application/json"},{name:"pages.jsonl",type:"application/x-ndjson"},{name:"links.jsonl.gz",type:"application/gzip"},{name:"link-metrics.jsonl",type:"application/x-ndjson"},{name:"link-report.md",type:"text/markdown"},{name:"links-summary.json",type:"application/json"},{name:"external-domains.json",type:"application/json"}];if(p){T.push({name:"rendered-dom/manifest.jsonl",type:"application/x-ndjson"});for(let n of _)T.push({name:n.path,type:"text/plain"})}M&&(T.push({name:"similarity-table.csv",type:"text/csv"}),T.push({name:"similarity.jsonl",type:"application/x-ndjson"}),T.push({name:"similarity-table-schema.json",type:"application/json"}),T.push({name:"similarity-summary.json",type:"application/json"}),T.push({name:"content-clusters.json",type:"application/json"})),P&&(T.push({name:"wayback-manifest.json",type:"application/json"}),T.push({name:"capture-matrix.jsonl",type:"application/x-ndjson"})),r.imageAudit&&(T.push({name:"images.jsonl",type:"application/x-ndjson"}),T.push({name:"images-summary.json",type:"application/json"})),r.seoAudit&&T.push({name:"seo-audit.json",type:"application/json"}),r.branding&&T.push({name:"branding.json",type:"application/json"}),i&&T.push({name:"images-download-summary.json",type:"application/json"});let N=new en,Tt=Ze({ownerId:String(e.userId),jobId:e.id,createdAt:new Date,content:N.outputStream});for(let n of T)N.addFile(l(s,n.name),n.name);let he=[...v].sort((n,t)=>n.pageId.localeCompare(t.pageId));for(let n of he)n.markdownPath&&N.addFile(l(s,n.markdownPath),n.markdownPath);for(let n of he)n.legacyMarkdownPath&&N.addFile(l(s,n.legacyMarkdownPath),n.legacyMarkdownPath);for(let n of he)n.htmlPath&&N.addFile(l(s,n.htmlPath),n.htmlPath);for(let n of he)N.addFile(l(s,n.jsonPath),n.jsonPath);if(i&&Z>de){let n=l(s,"images");for(let t of Gt(n,{recursive:!0})){let u=l(n,t);Zt(u).isFile()&&N.addFile(u,`images/${t.split("\\").join("/")}`)}}return N.end(),[await tt("export_packaging",()=>Tt),...Oe]}finally{Kt(s,{recursive:!0,force:!0})}}export{Rt as a,on as b,an as c,Me as d,Ae as e,Lt as f,dn as g,ct as h,pn as i,je as j,Fn as k};
@@ -0,0 +1,5 @@
1
+ import{e as l}from"./chunk-C7MGJUTU.js";import{a as o}from"./chunk-WNUXMPVX.js";function c(t){return{id:String(t.id),user_id:Number(t.user_id),status:String(t.status),query:String(t.query),options:String(t.options),callback_url:t.callback_url!=null?String(t.callback_url):null,result:t.result!=null?String(t.result):null,error:t.error!=null?String(t.error):null,public_error_json:t.public_error_json!=null?String(t.public_error_json):null,created_at:String(t.created_at),started_at:t.started_at!=null?String(t.started_at):null,completed_at:t.completed_at!=null?String(t.completed_at):null}}function g(t){let{public_error_json:r,...e}=t;return{...e,options:JSON.parse(t.options),result:t.result?JSON.parse(t.result):null,publicError:l(r)}}async function a(t,r){let e=o(),n=r?await e.execute({sql:"SELECT * FROM jobs WHERE id = ? AND user_id = ?",args:[t,r]}):await e.execute({sql:"SELECT * FROM jobs WHERE id = ?",args:[t]});return n.rows[0]?g(c(n.rows[0])):void 0}async function b(t,r,e,n,s){if(!/^[a-zA-Z0-9-]{1,100}$/.test(t))throw new Error("job id is invalid");await o().execute({sql:"INSERT INTO jobs (id, user_id, query, options, callback_url) VALUES (?, ?, ?, ?, ?)",args:[t,r,e,JSON.stringify(n),s??null]})}async function _(t,r,e,n,s){let u=Number(r);if(!Number.isSafeInteger(u)||u<=0||typeof n.requestFingerprint!="string"||!/^[a-f0-9]{64}$/.test(n.requestFingerprint))throw new Error("Job receipt requires a principal and request fingerprint");try{return await b(t,r,e,n,s),{kind:"created",jobId:t}}catch{let i;try{i=await a(t,r)}catch{return{kind:"unknown",jobId:t}}return!i||i.query!==e||i.callback_url!==(s??null)||i.options.requestFingerprint!==n.requestFingerprint||i.options.billingDebitKey!==n.billingDebitKey||i.options.billingHoldMc!==n.billingHoldMc?{kind:"unknown",jobId:t}:{kind:"recovered",jobId:t,job:i}}}async function f(t,r,e){let n=Number(r);if(!Number.isSafeInteger(n)||n<=0||!/^[a-f0-9]{64}$/.test(e))throw new Error("Job dispatch receipt requires a principal and request fingerprint");try{if((await o().execute({sql:`UPDATE jobs SET options=json_set(options,'$.dispatchRequestedAt',strftime('%Y-%m-%dT%H:%M:%fZ','now'))
2
+ WHERE id=? AND user_id=? AND json_extract(options,'$.requestFingerprint')=?
3
+ AND json_extract(options,'$.dispatchRequestedAt') IS NULL`,args:[t,r,e]})).rowsAffected===1)return!0}catch{}let s=await a(t,r).catch(()=>{});return s?.options.requestFingerprint===e&&typeof s.options.dispatchRequestedAt=="string"}async function m(t,r,e=new Date().toISOString()){if(!Number.isSafeInteger(r)||r<0)throw new Error("job billed amount must be a non-negative integer");return(await o().execute({sql:`UPDATE jobs
4
+ SET options = json_set(options, '$.billingSettledAt', ?, '$.billedMc', ?)
5
+ WHERE id = ?`,args:[e,r,t]})).rowsAffected===1}export{c as a,g as b,a as c,b as d,_ as e,f,m as g};
@@ -1,4 +1,4 @@
1
- import{a as M,b as q,f as L}from"./chunk-OSKCHEVM.js";import{A as ge,f as I,h as F,s as b,v as J,w as pe,x as _e,y as H}from"./chunk-U3NV72AL.js";import{g as j}from"./chunk-VVSHZV5S.js";import{b as $}from"./chunk-4ZVO6GX4.js";import{a as l}from"./chunk-WNUXMPVX.js";import{createHash as O,randomUUID as Oe}from"crypto";import{Client as tt,StreamableHTTPClientTransport as nt}from"@modelcontextprotocol/client";import{randomUUID as Q}from"crypto";import{createHash as Y,randomBytes as W,scryptSync as fe,timingSafeEqual as We}from"crypto";function Ee(){return"sk_"+W(24).toString("hex")}function ye(e){let t=W(16).toString("hex"),n=fe(e,t,64);return`${t}:${n.toString("hex")}`}function Ct(e,t){let[n,i]=t.split(":"),r=Buffer.from(i,"hex"),o=fe(e,n,64);return We(r,o)}function K(e){return{id:Number(e.id),email:String(e.email),name:e.name!=null?String(e.name):null,api_key:String(e.api_key),key_active:Number(e.key_active??1),password_hash:e.password_hash!=null?String(e.password_hash):null,created_at:String(e.created_at),active:Number(e.active),stripe_customer_id:e.stripe_customer_id!=null?String(e.stripe_customer_id):null,balance_mc:Number(e.balance_mc??0),extra_concurrency_slots:Number(e.extra_concurrency_slots??0),concurrency_stripe_sub_id:e.concurrency_stripe_sub_id!=null?String(e.concurrency_stripe_sub_id):null,concurrency_pack_quantity:Number(e.concurrency_pack_quantity??0),subscription_concurrency:Number(e.subscription_concurrency??0),subscription_tier:e.subscription_tier!=null?String(e.subscription_tier):null,subscription_id:e.subscription_id!=null?String(e.subscription_id):null,memory_plan:e.memory_plan!=null?String(e.memory_plan):"free",memory_subscription_id:e.memory_subscription_id!=null?String(e.memory_subscription_id):null,memory_provisioned:Number(e.memory_provisioned??0)}}async function X(e){let t=await l().execute({sql:"SELECT * FROM users WHERE email = ? AND active = 1",args:[e]});return t.rows[0]?K(t.rows[0]):void 0}async function St(e){let t=await l().execute({sql:"SELECT * FROM users WHERE api_key = ? AND active = 1 AND key_active = 1",args:[e]});return t.rows[0]?K(t.rows[0]):void 0}async function Nt(e){let t=await l().execute({sql:"SELECT * FROM users WHERE id = ? AND active = 1",args:[e]});return t.rows[0]?K(t.rows[0]):void 0}async function ht(e,t){await l().execute({sql:"UPDATE users SET password_hash = ? WHERE id = ?",args:[ye(t),e]})}async function Rt(e){let t=l(),[n,i,r,o,s]=await Promise.all([t.execute({sql:"SELECT COUNT(*) as n FROM jobs WHERE user_id = ?",args:[e]}),t.execute({sql:"SELECT COUNT(*) as n FROM jobs WHERE user_id = ? AND status = 'done'",args:[e]}),t.execute({sql:"SELECT COUNT(*) as n FROM jobs WHERE user_id = ? AND status = 'failed'",args:[e]}),t.execute({sql:"SELECT COALESCE(SUM(json_extract(result, '$.totalQuestions')), 0) as n FROM jobs WHERE user_id = ? AND status = 'done'",args:[e]}),t.execute({sql:"SELECT MAX(created_at) as t FROM jobs WHERE user_id = ?",args:[e]})]);return{total:Number(n.rows[0]?.n??0),done:Number(i.rows[0]?.n??0),failed:Number(r.rows[0]?.n??0),totalQuestions:Number(o.rows[0]?.n??0),lastUsed:s.rows[0]?.t!=null?String(s.rows[0].t):null}}async function At(e,t,n,i){let r=l(),o=Ee(),s=n??W(6).toString("hex"),a=ye(s);return{id:(await r.execute({sql:"INSERT INTO users (email, name, api_key, password_hash, stripe_customer_id) VALUES (?, ?, ?, ?, ?)",args:[e,t??null,o,a,i??null]})).lastInsertRowid,email:e,name:t,api_key:o,password:s}}async function xt(e,t,n,i){let r=Math.floor(Date.now()/1e3),o=Math.floor(r/i)*i,s=Y("sha256").update(t).digest("hex"),d=await l().execute({sql:`
1
+ import{a as M,b as q,f as L}from"./chunk-XNTAHMPA.js";import{b as I,d as F,o as b,r as J,s as pe,t as _e,u as H,w as ge}from"./chunk-4YZ74SJ6.js";import{g as j}from"./chunk-VVSHZV5S.js";import{b as $}from"./chunk-4ZVO6GX4.js";import{a as l}from"./chunk-WNUXMPVX.js";import{createHash as O,randomUUID as Oe}from"crypto";import{Client as tt,StreamableHTTPClientTransport as nt}from"@modelcontextprotocol/client";import{randomUUID as Q}from"crypto";import{createHash as Y,randomBytes as W,scryptSync as fe,timingSafeEqual as We}from"crypto";function Ee(){return"sk_"+W(24).toString("hex")}function ye(e){let t=W(16).toString("hex"),n=fe(e,t,64);return`${t}:${n.toString("hex")}`}function Ct(e,t){let[n,i]=t.split(":"),r=Buffer.from(i,"hex"),o=fe(e,n,64);return We(r,o)}function K(e){return{id:Number(e.id),email:String(e.email),name:e.name!=null?String(e.name):null,api_key:String(e.api_key),key_active:Number(e.key_active??1),password_hash:e.password_hash!=null?String(e.password_hash):null,created_at:String(e.created_at),active:Number(e.active),stripe_customer_id:e.stripe_customer_id!=null?String(e.stripe_customer_id):null,balance_mc:Number(e.balance_mc??0),extra_concurrency_slots:Number(e.extra_concurrency_slots??0),concurrency_stripe_sub_id:e.concurrency_stripe_sub_id!=null?String(e.concurrency_stripe_sub_id):null,concurrency_pack_quantity:Number(e.concurrency_pack_quantity??0),subscription_concurrency:Number(e.subscription_concurrency??0),subscription_tier:e.subscription_tier!=null?String(e.subscription_tier):null,subscription_id:e.subscription_id!=null?String(e.subscription_id):null,memory_plan:e.memory_plan!=null?String(e.memory_plan):"free",memory_subscription_id:e.memory_subscription_id!=null?String(e.memory_subscription_id):null,memory_provisioned:Number(e.memory_provisioned??0)}}async function X(e){let t=await l().execute({sql:"SELECT * FROM users WHERE email = ? AND active = 1",args:[e]});return t.rows[0]?K(t.rows[0]):void 0}async function St(e){let t=await l().execute({sql:"SELECT * FROM users WHERE api_key = ? AND active = 1 AND key_active = 1",args:[e]});return t.rows[0]?K(t.rows[0]):void 0}async function Nt(e){let t=await l().execute({sql:"SELECT * FROM users WHERE id = ? AND active = 1",args:[e]});return t.rows[0]?K(t.rows[0]):void 0}async function ht(e,t){await l().execute({sql:"UPDATE users SET password_hash = ? WHERE id = ?",args:[ye(t),e]})}async function Rt(e){let t=l(),[n,i,r,o,s]=await Promise.all([t.execute({sql:"SELECT COUNT(*) as n FROM jobs WHERE user_id = ?",args:[e]}),t.execute({sql:"SELECT COUNT(*) as n FROM jobs WHERE user_id = ? AND status = 'done'",args:[e]}),t.execute({sql:"SELECT COUNT(*) as n FROM jobs WHERE user_id = ? AND status = 'failed'",args:[e]}),t.execute({sql:"SELECT COALESCE(SUM(json_extract(result, '$.totalQuestions')), 0) as n FROM jobs WHERE user_id = ? AND status = 'done'",args:[e]}),t.execute({sql:"SELECT MAX(created_at) as t FROM jobs WHERE user_id = ?",args:[e]})]);return{total:Number(n.rows[0]?.n??0),done:Number(i.rows[0]?.n??0),failed:Number(r.rows[0]?.n??0),totalQuestions:Number(o.rows[0]?.n??0),lastUsed:s.rows[0]?.t!=null?String(s.rows[0].t):null}}async function At(e,t,n,i){let r=l(),o=Ee(),s=n??W(6).toString("hex"),a=ye(s);return{id:(await r.execute({sql:"INSERT INTO users (email, name, api_key, password_hash, stripe_customer_id) VALUES (?, ?, ?, ?, ?)",args:[e,t??null,o,a,i??null]})).lastInsertRowid,email:e,name:t,api_key:o,password:s}}async function xt(e,t,n,i){let r=Math.floor(Date.now()/1e3),o=Math.floor(r/i)*i,s=Y("sha256").update(t).digest("hex"),d=await l().execute({sql:`
2
2
  INSERT INTO rate_limits (scope, key_hash, window_start, count)
3
3
  VALUES (?, ?, ?, 1)
4
4
  ON CONFLICT(scope, key_hash, window_start)
@@ -0,0 +1 @@
1
+ var o="0.97.1";export{o as a};
@@ -0,0 +1,163 @@
1
+ import{D as Oe,F as Re,G as Ce,H as j,c as R,h as Ee,i as C,j as y,k as ye,l as Se,m as P,n as he,o as Ae,p as we,q as T,r as xe,s as w,t as p,u as Ne,x as Ie,y as De,z as ke}from"./chunk-SJNSTO3C.js";import{L as ee}from"./chunk-6D25Q5BR.js";import{b as F}from"./chunk-C3JWX6KM.js";import{b as re,c as te}from"./chunk-QCBCOEH5.js";import{d as O}from"./chunk-KBZY2OGP.js";import{a as ue,b as ce,c as pe,f as me}from"./chunk-4YZ74SJ6.js";import{d as de}from"./chunk-JHDD6R3O.js";import{b as be,d as ge,e as D,f as ve,j as k,p as fe}from"./chunk-7SR7MEU5.js";import{d as oe,e as ne,f as ie,g as se,i as I,l as ae,s as le,t as b}from"./chunk-ANQ4JJVI.js";import{a as g,b as E,c}from"./chunk-FLAWC5PF.js";import{c as W,d as B}from"./chunk-B4ZKPLSP.js";import{a as V,b as X,c as Z}from"./chunk-VVSHZV5S.js";import{b as Y}from"./chunk-4ZVO6GX4.js";import{b as N,d as z,g as m,h as A,j as K,m as G,o as Q}from"./chunk-C7MGJUTU.js";import{a}from"./chunk-WNUXMPVX.js";import{b as _e}from"./chunk-B3JRL45T.js";import{c as h,d as f}from"./chunk-HNNYXMSI.js";import{randomUUID as tr}from"crypto";async function Ar(e,r,t){let n=tr();return await a().execute({sql:"INSERT INTO jobs (id, user_id, query, options, status, started_at) VALUES (?, ?, ?, ?, 'running', datetime('now'))",args:[n,e,r,JSON.stringify(t)]}),n}async function wr(e,r,t,n){let s=await a().execute({sql:`INSERT INTO jobs (id, user_id, query, options, status, started_at)
2
+ VALUES (?, ?, ?, ?, 'running', datetime('now'))
3
+ ON CONFLICT(id) DO NOTHING`,args:[e,r,t,JSON.stringify(n)]}),i=await c(e,r);if(!i)throw new Error(`getOrCreateRunningJob: row missing immediately after insert/lookup for ${e}`);return{job:i,created:s.rowsAffected===1}}async function xr(e,r,t){await a().execute({sql:`UPDATE jobs
4
+ SET status = 'running', started_at = COALESCE(started_at, datetime('now')),
5
+ options = CASE WHEN ? = 'runtime' THEN json_set(options, '$.executionOwner', 'runtime') ELSE options END
6
+ WHERE id = ?
7
+ AND (status = 'pending' OR (
8
+ status = 'running' AND ? = 'runtime'
9
+ AND json_extract(options, '$.executionOwner') = 'inngest'
10
+ AND EXISTS (SELECT 1 FROM operation_executions e JOIN operation_runs r ON r.id = e.run_id
11
+ WHERE e.run_id = 'extract-url-execution:' || jobs.id AND r.user_id = jobs.user_id
12
+ AND r.status = 'running' AND e.phase = 'claimed'
13
+ AND e.expires_at_ms <= CAST(strftime('%s', 'now') AS INTEGER) * 1000)
14
+ ))
15
+ AND (json_extract(options, '$.executionOwner') = ?
16
+ OR (? = 'runtime' AND json_extract(options, '$.executionOwner') = 'inngest'))
17
+ AND json_extract(options, '$.jobKind') = ?`,args:[r,e,r,r,r,t]});let n=await c(e);if(!(!n||n.options.executionOwner!==r||n.options.jobKind!==t))return n}async function Nr(e=25){let r=Math.max(1,Math.min(100,Math.floor(e)));return(await a().execute({sql:`SELECT * FROM jobs
18
+ WHERE status IN ('pending', 'running')
19
+ AND json_extract(options, '$.executionOwner') IN ('runtime', 'inngest')
20
+ AND json_extract(options, '$.jobKind') = 'extract_url'
21
+ AND json_extract(options, '$.billingDebitKey') IS NOT NULL
22
+ AND EXISTS (SELECT 1 FROM billing_debits d
23
+ WHERE d.idempotency_key = json_extract(jobs.options, '$.billingDebitKey')
24
+ AND d.user_id = jobs.user_id AND d.status = 'applied'
25
+ AND d.operation = ? AND d.amount_mc = ?)
26
+ AND (
27
+ NOT EXISTS (SELECT 1 FROM operation_executions e
28
+ WHERE e.run_id = 'extract-url-execution:' || jobs.id)
29
+ AND (status = 'pending' OR json_extract(options, '$.executionOwner') = 'runtime')
30
+ OR EXISTS (SELECT 1 FROM operation_executions e JOIN operation_runs r ON r.id = e.run_id
31
+ WHERE e.run_id = 'extract-url-execution:' || jobs.id AND r.user_id = jobs.user_id
32
+ AND r.status = 'running' AND e.phase = 'claimed'
33
+ AND e.expires_at_ms <= CAST(strftime('%s', 'now') AS INTEGER) * 1000)
34
+ )
35
+ AND created_at <= datetime('now', '-30 seconds')
36
+ AND (status = 'pending' OR datetime(COALESCE(started_at, created_at)) <= datetime('now', '-30 seconds'))
37
+ AND datetime(COALESCE(json_extract(options, '$.extractUrlLastDispatchAt'), '1970-01-01')) <= datetime('now', '-30 seconds')
38
+ ORDER BY created_at
39
+ LIMIT ?`,args:[ce.EXTRACT_URL,ue.page_scrape,r]})).rows.map(n=>E(g(n)))}async function Ir(e,r){return(await a().execute({sql:`UPDATE jobs
40
+ SET options = json_set(options,
41
+ '$.extractUrlDispatchAttempts', COALESCE(json_extract(options, '$.extractUrlDispatchAttempts'), 0) + 1,
42
+ '$.extractUrlLastDispatchAt', ?,
43
+ '$.extractUrlLastDispatchError', ?)
44
+ WHERE id = ? AND (status = 'pending' OR (status = 'running' AND (
45
+ json_extract(options, '$.executionOwner') = 'runtime'
46
+ OR EXISTS (SELECT 1 FROM operation_executions e JOIN operation_runs r ON r.id = e.run_id
47
+ WHERE e.run_id = 'extract-url-execution:' || jobs.id AND r.user_id = jobs.user_id
48
+ AND r.status = 'running' AND e.phase = 'claimed'
49
+ AND e.expires_at_ms <= CAST(strftime('%s', 'now') AS INTEGER) * 1000)
50
+ )))
51
+ AND json_extract(options, '$.executionOwner') IN ('runtime', 'inngest')
52
+ AND json_extract(options, '$.jobKind') = 'extract_url'`,args:[new Date().toISOString(),r??null,e]})).rowsAffected===1}async function Dr(e){return(await a().execute({sql:"SELECT * FROM jobs WHERE user_id = ? ORDER BY created_at DESC LIMIT 50",args:[e]})).rows.map(t=>E(g(t)))}async function Pe(){let e=a(),r=await e.execute(`
53
+ SELECT * FROM jobs
54
+ WHERE status = 'pending'
55
+ AND COALESCE(json_extract(options, '$.executionOwner'), 'cron') NOT IN ('direct', 'runtime', 'inngest')
56
+ AND json_extract(options,'$.queueExecutionProtocol') IS NOT 0
57
+ ORDER BY created_at
58
+ LIMIT 1
59
+ `),t=r.rows[0]?g(r.rows[0]):void 0;return t?(await e.execute({sql:"UPDATE jobs SET status = 'running', started_at = datetime('now') WHERE id = ? AND status = 'pending'",args:[t.id]})).rowsAffected===0?void 0:{...t,status:"running"}:void 0}async function kr(e,r){await a().execute({sql:`UPDATE jobs SET status = 'done', result = ?, error = NULL, public_error_json = NULL, completed_at = datetime('now') WHERE id = ? AND ${O}`,args:[JSON.stringify(r),e]})}async function Or(e,r,t){await a().execute({sql:`UPDATE jobs SET status = 'failed', error = ?, public_error_json = ?, completed_at = datetime('now') WHERE id = ? AND ${O}`,args:[r,z(t),e]})}async function Tr(e=25){let r=Math.max(1,Math.min(100,Math.floor(e)));return(await a().execute({sql:`SELECT * FROM jobs
60
+ WHERE status IN ('done', 'failed', 'cancelled')
61
+ AND json_extract(options, '$.executionOwner') IN ('direct', 'inngest')
62
+ AND json_extract(options, '$.billingDebitKey') IS NOT NULL
63
+ AND CAST(json_extract(options, '$.billingHoldMc') AS INTEGER) > 0
64
+ AND json_extract(options, '$.billingSettledAt') IS NULL
65
+ ORDER BY completed_at DESC
66
+ LIMIT ?`,args:[r]})).rows.map(n=>E(g(n)))}async function Te(e,r=25,t="1"){if(!/^[a-z_]+$/.test(e)||!t.trim()||t.length>8192)throw Error("Invalid intent selection");return(await a().execute({sql:`SELECT r.id,r.user_id,r.job_id FROM operation_runs r
67
+ JOIN operation_executions e ON e.run_id=r.id
68
+ WHERE r.tool=? AND r.status='running' AND e.phase='claimed'
69
+ AND e.expires_at_ms <= CAST(strftime('%s','now') AS INTEGER)*1000
70
+ AND json_extract(r.normalized_flags_json,'$._storeGateRequired') IS 1 AND (${t})
71
+ ORDER BY r.started_at LIMIT ?`,args:[e,Math.max(1,Math.min(100,Math.floor(r)))]})).rows.map(s=>({runId:String(s.id),userId:Number(s.user_id),jobId:String(s.job_id)}))}function je(e){let r=JSON.parse(f(e.options)),t=r.serpOnly===!0&&!r.serpIdentity;return{profile:"queue",jobId:e.jobId,userId:e.userId,tool:"search_serp",requestFingerprint:e.requestFingerprint,options:r,callbackUrl:e.callbackUrl??null,deadlineAt:0,budgetMs:W(Number(r.maxQuestions??30),!0,r.mode,r.pages,!!r.recency,t).serverMs,hold:{userId:e.userId,key:`serp-search:${e.jobId}:hold`,amountMc:e.amountMc,operation:"serp",description:`SERP search: ${r.query}`.slice(0,500)},refundOperation:"serp_refund",sourceSurface:"serp_search"}}var Gr=e=>j(je(e));async function or(e,r){let t;try{t=await b(`harvest:${r}`,e,"search_serp",r)}catch{return{kind:"unknown",reply:{status:503,body:{job_id:r,status:"pending",executionState:"unknown",hold_state:"unconfirmed",status_tool:"harvest_paa_status",...A({errorCode:"response_lost",retryable:!0})}}}}return t?.flags.syncExecutionProtocol!==1||t.flags.intent?.profile!=="queue"?null:j(t.flags.intent,!1)}async function Me(){for(let r of await Te("search_serp",1,`json_extract(r.normalized_flags_json,'$.intent.profile')='queue' AND (EXISTS(SELECT 1 FROM (SELECT r.user_id AS user_id,json_extract(r.normalized_flags_json,'$.intent') AS normalized_flags_json) saved WHERE ${de("saved.user_id","json_extract(saved.normalized_flags_json,'$.hold.key')")}))`))await c(r.jobId,r.userId)||await or(r.userId,r.jobId);let e=(await a().execute(`SELECT j.id,j.user_id FROM jobs j JOIN operation_runs r ON r.id='harvest:'||j.id AND r.user_id=j.user_id JOIN operation_executions x ON x.run_id=r.id
72
+ WHERE json_extract(j.options,'$.queueExecutionProtocol')=1 AND x.phase IN ('effects_started','unknown') AND x.expires_at_ms<=CAST(strftime('%s','now') AS INTEGER)*1000
73
+ ORDER BY j.created_at LIMIT 1`)).rows[0];return e?c(String(e.id),Number(e.user_id)):void 0}async function Le(e,r){if(e.options.queueExecutionProtocol!==1)return await a().execute({sql:"UPDATE jobs SET options=json_set(options,'$.queueExecutionProtocol',0,'$.executionState','unknown','$.holdState','unconfirmed') WHERE id=? AND user_id=? AND json_extract(options,'$.queueExecutionProtocol') IS NULL",args:[e.id,e.user_id]}),{completed:!1,replayed:!1,reply:null};let t=await Re(e);if(t)return{completed:(await c(e.id,e.user_id))?.options.executionState==="finalized"&&t.status===200,replayed:!0,reply:t};let n=e.options,s=Object.fromEntries(Object.entries(n).filter(([d])=>!["executionOwner","queueExecutionProtocol","billingHoldMc","billingDebitKey","requestFingerprint","compatDeadlineAt","executionState","holdState"].includes(d))),i=je({jobId:e.id,userId:e.user_id,options:s,requestFingerprint:String(n.requestFingerprint),amountMc:Number(n.billingHoldMc),callbackUrl:e.callback_url}),o=await Ce(i,async d=>{let u={value:null},_=n.serpOnly===!0&&!n.serpIdentity,v=`harvest:${e.id}`,x=null,J=AbortSignal.timeout(Math.max(0,d.deadlineAt-Date.now()));if(_&&!De({pages:n.pages===2?2:1,recency:n.recency==="week"||n.recency==="month"?n.recency:void 0,mode:n.mode,token:r.apifyToken}))throw Error("Search providers are not configured");let er=await r.withCostContext({op:"serp",userId:e.user_id,headlessSentOut:u,operationRunId:v,operationAccountScope:"production-default"},async()=>_?ke({query:String(n.query??e.query),pages:n.pages===2?2:1,mode:n.mode,recency:n.recency==="week"||n.recency==="month"?n.recency:void 0,token:r.apifyToken,operationRunId:v,accountScope:"production-default",signal:J,onAttemptEvent:r.onAttemptEvent,physicalFactory:d.physicalFactory}).then($=>(x=$,$.result)):ee({...n,physicalExecution:await d.physicalFactory(),\u006b\u0065\u0072\u006e\u0065\u006cApiKey:r.\u006b\u0065\u0072\u006e\u0065\u006cApiKey,headless:!0,format:"json",outputDir:"/tmp/paa-output-api",signal:J,onAttemptEvent:r.onAttemptEvent}));if(_&&!x)throw Error("Search provider billing receipt is missing");let U=x,rr=_?me(U.deliveredPages,U.deliveryProvider):pe(u.value);return{result:er,amountMc:Math.min(Number(n.billingHoldMc),rr)}},r.classifyFailure);return{completed:(await c(e.id,e.user_id))?.options.executionState==="finalized"&&!!(o.body.status&&["done","failed","cancelled"].includes(String(o.body.status))),replayed:!1,reply:o}}import{randomUUID as nr}from"crypto";function S(e){return e==null?null:Number(e)===1}function He(e){if(e==null)return null;try{return JSON.parse(String(e))}catch{return null}}function qe(e){return{id:String(e.id),job_id:String(e.job_id),user_id:Number(e.user_id),attempt_number:Number(e.attempt_number),max_attempts:Number(e.max_attempts),query:String(e.query),location:e.location!=null?String(e.location):null,max_questions:Number(e.max_questions),status:String(e.status),outcome:e.outcome!=null?String(e.outcome):null,\u006b\u0065\u0072\u006e\u0065\u006c_session_id:e.\u006b\u0065\u0072\u006e\u0065\u006c_session_id!=null?String(e.\u006b\u0065\u0072\u006e\u0065\u006c_session_id):null,question_count:e.question_count!=null?Number(e.question_count):null,duration_ms:e.duration_ms!=null?Number(e.duration_ms):null,error:e.error!=null?String(e.error):null,will_retry:S(e.will_retry),\u006b\u0065\u0072\u006e\u0065\u006c_delete_started:Number(e.\u006b\u0065\u0072\u006e\u0065\u006c_delete_started??0)===1,\u006b\u0065\u0072\u006e\u0065\u006c_delete_succeeded:S(e.\u006b\u0065\u0072\u006e\u0065\u006c_delete_succeeded),\u006b\u0065\u0072\u006e\u0065\u006c_delete_error:e.\u006b\u0065\u0072\u006e\u0065\u006c_delete_error!=null?String(e.\u006b\u0065\u0072\u006e\u0065\u006c_delete_error):null,browser_close_succeeded:S(e.browser_close_succeeded),browser_close_error:e.browser_close_error!=null?String(e.browser_close_error):null,browser_provider:e.browser_provider!=null?String(e.browser_provider):null,provider_session_id:e.provider_session_id!=null?String(e.provider_session_id):null,provider_session_observed_at:e.provider_session_observed_at!=null?String(e.provider_session_observed_at):null,provider_lookup_status:e.provider_lookup_status!=null?String(e.provider_lookup_status):null,provider_status:e.provider_status!=null?String(e.provider_status):null,provider_error_code:e.provider_error_code!=null?String(e.provider_error_code):null,provider_error_message:e.provider_error_message!=null?String(e.provider_error_message):null,provider_reason_source:e.provider_reason_source!=null?String(e.provider_reason_source):null,provider_duration_ms:e.provider_duration_ms!=null?Number(e.provider_duration_ms):null,provider_bandwidth_bytes:e.provider_bandwidth_bytes!=null?Number(e.provider_bandwidth_bytes):null,provider_captcha:S(e.provider_captcha),provider_checked_at:e.provider_checked_at!=null?String(e.provider_checked_at):null,provider_detail:He(e.provider_detail_json),provider_disconnect_observed:S(e.provider_disconnect_observed),provider_disconnect_message:e.provider_disconnect_message!=null?String(e.provider_disconnect_message):null,debug:He(e.debug_json),created_at:String(e.created_at),completed_at:e.completed_at!=null?String(e.completed_at):null}}async function Je(e){await a().execute({sql:`
74
+ INSERT INTO harvest_attempts (
75
+ id, job_id, user_id, attempt_number, max_attempts, query, location, max_questions, status, created_at
76
+ )
77
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, 'running', ?)
78
+ ON CONFLICT(job_id, attempt_number) DO NOTHING
79
+ `,args:[nr(),e.jobId,e.userId,e.attemptNumber,e.maxAttempts,e.query,e.location,e.maxQuestions,e.startedAt]})}async function Ue(e){return(await a().execute({sql:`
80
+ UPDATE harvest_attempts SET
81
+ browser_provider = ?,
82
+ provider_session_id = ?,
83
+ provider_session_observed_at = COALESCE(provider_session_observed_at, ?),
84
+ provider_lookup_status = CASE
85
+ WHEN provider_lookup_status = 'complete' THEN provider_lookup_status
86
+ ELSE 'pending'
87
+ END
88
+ WHERE job_id = ? AND attempt_number = ? AND status = 'running'
89
+ `,args:[e.browserProvider,e.providerSessionId,e.observedAt,e.jobId,e.attemptNumber]})).rowsAffected===1}async function $e(e){await a().execute({sql:`
90
+ UPDATE harvest_attempts SET
91
+ status = 'finished',
92
+ outcome = ?,
93
+ \u006b\u0065\u0072\u006e\u0065\u006c_session_id = ?,
94
+ question_count = ?,
95
+ duration_ms = ?,
96
+ error = ?,
97
+ will_retry = ?,
98
+ \u006b\u0065\u0072\u006e\u0065\u006c_delete_started = ?,
99
+ \u006b\u0065\u0072\u006e\u0065\u006c_delete_succeeded = ?,
100
+ \u006b\u0065\u0072\u006e\u0065\u006c_delete_error = ?,
101
+ browser_close_succeeded = ?,
102
+ browser_close_error = ?,
103
+ browser_provider = COALESCE(browser_provider, ?),
104
+ provider_session_id = COALESCE(provider_session_id, ?),
105
+ provider_lookup_status = CASE
106
+ WHEN provider_lookup_status = 'complete' THEN provider_lookup_status
107
+ ELSE COALESCE(?, provider_lookup_status)
108
+ END,
109
+ provider_status = COALESCE(provider_status, ?),
110
+ provider_error_code = CASE
111
+ WHEN provider_reason_source = 'provider_session_api' THEN provider_error_code
112
+ ELSE COALESCE(?, provider_error_code)
113
+ END,
114
+ provider_error_message = CASE
115
+ WHEN provider_reason_source = 'provider_session_api' THEN provider_error_message
116
+ ELSE COALESCE(?, provider_error_message)
117
+ END,
118
+ provider_reason_source = CASE
119
+ WHEN provider_reason_source = 'provider_session_api' THEN provider_reason_source
120
+ ELSE COALESCE(?, provider_reason_source)
121
+ END,
122
+ provider_duration_ms = COALESCE(provider_duration_ms, ?),
123
+ provider_bandwidth_bytes = COALESCE(provider_bandwidth_bytes, ?),
124
+ provider_captcha = COALESCE(provider_captcha, ?),
125
+ provider_checked_at = COALESCE(provider_checked_at, ?),
126
+ provider_detail_json = COALESCE(provider_detail_json, ?),
127
+ provider_disconnect_observed = COALESCE(provider_disconnect_observed, ?),
128
+ provider_disconnect_message = COALESCE(provider_disconnect_message, ?),
129
+ debug_json = ?,
130
+ completed_at = ?
131
+ WHERE job_id = ? AND attempt_number = ?
132
+ `,args:[e.outcome,e.\u006b\u0065\u0072\u006e\u0065\u006cSessionId,e.questionCount,e.durationMs,e.error,e.willRetry?1:0,e.\u006b\u0065\u0072\u006e\u0065\u006cDeleteStarted?1:0,e.\u006b\u0065\u0072\u006e\u0065\u006cDeleteSucceeded==null?null:e.\u006b\u0065\u0072\u006e\u0065\u006cDeleteSucceeded?1:0,e.\u006b\u0065\u0072\u006e\u0065\u006cDeleteError,e.browserCloseSucceeded==null?null:e.browserCloseSucceeded?1:0,e.browserCloseError,e.browserProvider??null,e.providerSessionId??null,e.providerLookupStatus??null,e.providerStatus??null,e.providerErrorCode??null,e.providerErrorMessage??null,e.providerReasonSource??null,e.providerDurationMs??null,e.providerBandwidthBytes??null,e.providerCaptcha==null?null:e.providerCaptcha?1:0,e.providerCheckedAt??null,e.providerDetail==null?null:Fe(e.providerDetail),e.providerDisconnectObserved==null?null:e.providerDisconnectObserved?1:0,e.providerDisconnectMessage??null,e.debug==null?null:JSON.stringify(e.debug),e.completedAt,e.jobId,e.attemptNumber]})}function Fe(e,r=4096){let t=JSON.stringify(e);return t.length<=r?t:JSON.stringify({truncated:!0})}async function Xr(e){return(await a().execute({sql:`
133
+ UPDATE harvest_attempts SET
134
+ provider_lookup_status = ?,
135
+ provider_status = ?,
136
+ provider_error_code = ?,
137
+ provider_error_message = ?,
138
+ provider_reason_source = ?,
139
+ provider_duration_ms = ?,
140
+ provider_bandwidth_bytes = ?,
141
+ provider_captcha = ?,
142
+ provider_checked_at = ?,
143
+ provider_detail_json = ?
144
+ WHERE id = ?
145
+ AND job_id = ?
146
+ AND user_id = ?
147
+ AND browser_provider = '\u0062\u0072\u0069\u0067\u0068\u0074_\u0064\u0061\u0074\u0061_browser_api'
148
+ AND provider_session_id = ?
149
+ AND COALESCE(provider_lookup_status, '') <> 'complete'
150
+ `,args:[e.lookupStatus,e.providerStatus??null,e.errorCode??null,e.errorMessage??null,e.reasonSource??null,e.durationMs??null,e.bandwidthBytes??null,e.captcha==null?null:e.captcha?1:0,e.checkedAt,e.detail==null?null:Fe(e.detail),e.attemptId,e.jobId,e.userId,e.providerSessionId]})).rowsAffected===1}async function Zr(e=25){let r=Math.max(1,Math.min(100,Math.trunc(e)));return(await a().execute({sql:`
151
+ SELECT * FROM harvest_attempts
152
+ WHERE browser_provider = '\u0062\u0072\u0069\u0067\u0068\u0074_\u0064\u0061\u0074\u0061_browser_api'
153
+ AND provider_session_id IS NOT NULL
154
+ AND provider_lookup_status IN ('pending', 'temporary_error')
155
+ ORDER BY COALESCE(provider_checked_at, created_at) ASC
156
+ LIMIT ?
157
+ `,args:[r]})).rows.map(n=>qe(n))}async function We(e,r){return(r==null?await a().execute({sql:"SELECT * FROM harvest_attempts WHERE job_id = ? ORDER BY attempt_number ASC",args:[e]}):await a().execute({sql:`
158
+ SELECT ha.*
159
+ FROM harvest_attempts ha
160
+ INNER JOIN jobs j ON j.id = ha.job_id
161
+ WHERE ha.job_id = ? AND j.user_id = ?
162
+ ORDER BY ha.attempt_number ASC
163
+ `,args:[e,r]})).rows.map(n=>qe(n))}import{createHash as it}from"crypto";var ir=1e9;function M(e){return e==="timeout"?"timed_out":e==="request_aborted"?"cancelled":e==="browser_session_interrupted"?"interrupted":e==="captcha"||e==="proxy_tunnel_failed"||e==="proxy_unavailable"||e==="location_mismatch"||e==="error"?"failed":"succeeded"}function sr(e){return e==="\u0062\u0072\u0069\u0067\u0068\u0074_\u0064\u0061\u0074\u0061_browser_api"?"\u0062\u0072\u0069\u0067\u0068\u0074\u0064\u0061\u0074\u0061":"\u006b\u0065\u0072\u006e\u0065\u006c"}function Be(e,r,t=0){let n=new Map,s=null;return async i=>{if(e)try{if(i.type==="started"){let l=t+i.attemptNumber;if(l>1&&!s){let v=(await fe(e))?.attempts.at(-1);s=v?.id==null?null:String(v.id)}let d=await be({runId:e,attemptNumber:l,method:i.method,provider:i.provider,parentAttemptId:l===1?null:s,edgeKind:l===1?null:t>0&&i.attemptNumber===1?"durable_resume":i.retryKind==="provider_fallback"?"provider_fallback":"same_method_retry",retryKind:l===1?null:t>0&&i.attemptNumber===1?"durable_resume":i.retryKind,retryReason:l===1?null:t>0&&i.attemptNumber===1?"durable_worker_resume":i.retryReason,retryOwner:l===1?null:t>0&&i.attemptNumber===1?"durable_reconciler":"harvest",normalizedFlags:{maxAttempts:i.maxAttempts,maxQuestions:i.maxQuestions,hasLocation:!!i.location,plannedProvider:i.provider,planReason:i.planReason??null,planTimeoutMs:i.planTimeoutMs??null},startedAt:i.startedAt}),u=await D({attemptId:d,provider:i.provider,providerProduct:i.provider==="\u0062\u0072\u0069\u0067\u0068\u0074\u0064\u0061\u0074\u0061"?"\u0062\u0072\u0069\u0067\u0068\u0074\u0064\u0061\u0074\u0061_browser_api":"\u006b\u0065\u0072\u006e\u0065\u006c_browser",billingTier:i.provider==="\u0062\u0072\u0069\u0067\u0068\u0074\u0064\u0061\u0074\u0061"?R("https://www.google.com/search"):null,accountScope:r,method:i.method,providerStatus:"started"});n.set(i.attemptNumber,{attemptId:d,receiptId:u.id,plannedProvider:i.provider,observedProvider:null,providerMismatch:!1,method:i.method,startedAtMs:Date.parse(i.startedAt),billingMode:null}),s=d;return}let o=n.get(i.attemptNumber);if(!o)throw new Error(`Missing operation attempt state for attempt ${i.attemptNumber}`);if(i.type==="provider_session_started"){let l=sr(i.provider);if(o.observedProvider=l,o.billingMode=i.billingMode??null,l!==o.plannedProvider){o.providerMismatch=!0,await k({receiptId:o.receiptId,costState:"unavailable",providerStatus:"provider_mismatch",errorCode:"provider_mismatch",errorMessage:`Planned ${o.plannedProvider}; observed ${l}`,finalizedAt:i.observedAt});let d=l==="\u0062\u0072\u0069\u0067\u0068\u0074\u0064\u0061\u0074\u0061"?"\u0062\u0072\u0069\u0067\u0068\u0074\u0064\u0061\u0074\u0061_browser":"\u006b\u0065\u0072\u006e\u0065\u006c_browser",u=await D({attemptId:o.attemptId,provider:l,providerProduct:l==="\u0062\u0072\u0069\u0067\u0068\u0074\u0064\u0061\u0074\u0061"?"\u0062\u0072\u0069\u0067\u0068\u0074\u0064\u0061\u0074\u0061_browser_api":"\u006b\u0065\u0072\u006e\u0065\u006c_browser",billingTier:l==="\u0062\u0072\u0069\u0067\u0068\u0074\u0064\u0061\u0074\u0061"?R("https://www.google.com/search"):null,accountScope:r,method:d,providerStatus:"observed_provider_mismatch"});o.receiptId=u.id,o.method=d}await ve(o.receiptId,i.providerSessionId);return}if(await ge({attemptId:o.attemptId,status:o.providerMismatch?"failed":M(i.outcome),errorCode:o.providerMismatch?"provider_mismatch":M(i.outcome)==="succeeded"?null:i.outcome,errorMessage:i.error,durationMs:i.durationMs,endedAt:i.completedAt}),(o.observedProvider??o.plannedProvider)==="\u006b\u0065\u0072\u006e\u0065\u006c"){let l=o.billingMode==="headful",d=o.billingMode==="headless"||o.billingMode==="headful"?Z(i.durationMs,l):null;await k({receiptId:o.receiptId,costState:"estimated",units:i.durationMs/1e3,unitType:"wall_open_second",amountUsdNanos:d==null?null:Math.round(d*ir),providerProduct:"\u006b\u0065\u0072\u006e\u0065\u006c_browser",billingTier:o.billingMode,billableEvent:"provider_active_gb_second",measurementSource:"wall_open_time_proxy",rateSource:d!=null?`${X};\u006b\u0065\u0072\u006e\u0065\u006c_usd_per_second:${V}`:null,providerStatus:o.providerMismatch?"provider_mismatch":i.outcome,providerDurationMs:i.durationMs,errorCode:o.providerMismatch?"provider_mismatch":M(i.outcome)==="succeeded"?null:i.outcome,errorMessage:i.error,finalizedAt:i.completedAt})}}catch(o){console.warn(JSON.stringify({event:"operation_attempt_telemetry_failed",operation_run_id:e,attempt_number:i.attemptNumber,message:o instanceof Error?o.message:String(o)}))}}}function ze(e,r,t=null,n="production-default",s=0){let i=Be(t,n,s);return async o=>{if(await i(o),o.type==="started"){await Je({jobId:e,userId:r,attemptNumber:o.attemptNumber,maxAttempts:o.maxAttempts,query:o.query,location:o.location,maxQuestions:o.maxQuestions,startedAt:o.startedAt});return}if(o.type==="provider_session_started"){await Ue({jobId:e,attemptNumber:o.attemptNumber,browserProvider:o.provider,providerSessionId:o.providerSessionId,observedAt:o.observedAt});return}await $e({jobId:e,attemptNumber:o.attemptNumber,outcome:o.outcome,\u006b\u0065\u0072\u006e\u0065\u006cSessionId:o.\u006b\u0065\u0072\u006e\u0065\u006cSessionId,questionCount:o.questionCount,durationMs:o.durationMs,error:o.error,willRetry:o.willRetry,\u006b\u0065\u0072\u006e\u0065\u006cDeleteStarted:o.cleanup.\u006b\u0065\u0072\u006e\u0065\u006cDeleteStarted,\u006b\u0065\u0072\u006e\u0065\u006cDeleteSucceeded:o.cleanup.\u006b\u0065\u0072\u006e\u0065\u006cDeleteSucceeded,\u006b\u0065\u0072\u006e\u0065\u006cDeleteError:o.cleanup.\u006b\u0065\u0072\u006e\u0065\u006cDeleteError,browserCloseSucceeded:o.cleanup.browserCloseSucceeded,browserCloseError:o.cleanup.browserCloseError,browserProvider:o.cleanup.provider??null,providerSessionId:o.cleanup.providerSessionId??null,providerLookupStatus:o.cleanup.providerSessionId?"pending":"not_applicable",providerErrorCode:o.cleanup.providerDisconnectObserved?"browser_session_interrupted":null,providerErrorMessage:o.cleanup.providerDisconnectMessage??null,providerReasonSource:o.cleanup.providerDisconnectObserved?"client_runtime":null,providerDisconnectObserved:o.cleanup.providerDisconnectObserved??!1,providerDisconnectMessage:o.cleanup.providerDisconnectMessage??null,debug:o.debug,completedAt:o.completedAt})}}function ar(e){return e instanceof Error?e.message:String(e)}function lr(e,r){return e instanceof DOMException&&(e.name==="TimeoutError"||e.name==="AbortError")?!0:/timeout|timed out|Timeout \d+ms exceeded|deadline/i.test(r)}function dr(e){return/captcha|recaptcha|unusual traffic|google\.com\/sorry|blocked/i.test(e)}function ur(e){return/ERR_TUNNEL_CONNECTION_FAILED|ERR_PROXY_CONNECTION_FAILED|ERR_SOCKS_CONNECTION_FAILED|tunnel connection failed|proxy connection failed|transport error: proxy/i.test(e)}function cr(e){return/proxy unavailable|proxy_unavailable|connection_test_failed|did not return a proxy id|configured fallback/i.test(e)}function pr(e){return/(?:spending|billing|organization|account).{0,80}(?:cap|limit|blocked|disabled)|(?:quota|capacity).{0,40}(?:reached|exceeded)|https?:\/\/\S*dashboard/i.test(e)}function mr(e){return/browser (?:has been )?closed|context (?:has been )?closed|target page, context or browser has been closed|session closed|page has been closed/i.test(e)}function L(e){let r=ar(e);return e instanceof G?{error_code:"request_aborted",error_type:"request_aborted",message:m("request_aborted"),retryable:!0,httpStatus:408,terminalStatus:"cancelled"}:e instanceof K||dr(r)?{error_code:"captcha_exhausted",error_type:"captcha",message:m("captcha_exhausted"),retryable:!0,httpStatus:503,terminalStatus:"failed"}:F(e,r)?{error_code:"vendor_unavailable",error_type:"service_unavailable",message:m("vendor_unavailable"),retryable:!0,httpStatus:503,terminalStatus:"failed"}:e instanceof Q?{error_code:"location_mismatch",error_type:"location_mismatch",message:m("location_mismatch"),retryable:!0,httpStatus:503,terminalStatus:"failed"}:ur(r)?{error_code:"proxy_tunnel_failed",error_type:"connection",message:m("proxy_tunnel_failed"),retryable:!0,httpStatus:503,terminalStatus:"failed"}:cr(r)?{error_code:"proxy_unavailable",error_type:"connection",message:m("proxy_unavailable"),retryable:!0,httpStatus:503,terminalStatus:"failed"}:mr(r)?{error_code:"browser_session_interrupted",error_type:"extraction",message:m("browser_session_interrupted"),retryable:!0,httpStatus:503,terminalStatus:"failed"}:lr(e,r)?{error_code:"harvest_timeout",error_type:"timeout",message:m("harvest_timeout"),retryable:!0,httpStatus:504,terminalStatus:"failed"}:pr(r)?{error_code:"service_unavailable",error_type:"service_unavailable",message:N,retryable:!0,httpStatus:503,terminalStatus:"failed"}:{error_code:"extraction_failed",error_type:"extraction",message:N,retryable:!1,httpStatus:500,terminalStatus:"failed"}}function bt(e){return JSON.stringify({error_code:e.error_code,error_type:e.error_type,message:e.message,retryable:e.retryable})}function Ke(e,r={}){let t=A({errorCode:e.error_code,errorType:e.error_type,retryable:e.retryable,retryAfterSeconds:r.retryAfterSeconds,chargeStatus:r.chargeStatus,details:r.details});return{error:t.message,...t}}function Ge(e,r={}){let{error:t,...n}=Ke(e,r);return n}function gt(e,r={}){let t=L(e);return{body:Ke(t,r),status:t.httpStatus}}import{randomUUID as Qe}from"crypto";var _r=3e4;async function Ye(e,r,t){if(!e.receiptJson)return;let n=C(e.id);if((await b(n,e.userId,"paa_callback",e.jobId))?.phase==="published")return;if(t){if(await ne(t,{status:"succeeded",json:e.receiptJson}))return;await I(t)}let i=await oe(n,e.userId);i&&await ie(i,{status:"unknown",json:e.receiptJson},P(e,r,"accepted"))}function br(e){return!!(e.payloadJson&&e.payloadSha256===h(e.payloadJson)&&e.sendNonce&&e.sendToken&&e.httpStatus&&e.httpStatus>=200&&e.httpStatus<300&&e.receiptJson===f({deliveryId:e.id,idempotencyKey:e.id,payloadSha256:e.payloadSha256,httpStatus:e.httpStatus}))}async function Ve(e){let r=e.record,t,n=!1;try{if(r=await y(r.id,r.userId)??r,r.state==="accepted")return br(r)?(await Ye(r,e.domain).catch(()=>{}),p(r)):{state:"unavailable",deliveryId:r.id};if(["sending","unknown","expired","unavailable"].includes(r.state))return p(r);if(r.expiresAtMs<=r.nowMs)return await w(r,"expired"),p(await y(r.id,r.userId));if(r.payloadJson||(r=await Se(r,await e.compose(),e.domain)),!r.payloadJson||r.payloadSha256!==h(r.payloadJson))throw Error("Delivery payload conflicts");let s=P(r,e.domain,"frozen"),i=C(r.id),o={version:1,purpose:"outbound-callback",request:ye(r),payloadSha256:r.payloadSha256},l=await ae({id:i,userId:r.userId,ownerScope:`user:${r.userId}`,tool:"paa_callback",jobId:r.jobId,idempotencyKey:r.id,requestFingerprint:h(f(o)),normalizedFlags:o},Qe(),_r,s);if(l.kind!=="claimed"){let u=await b(i,r.userId,"paa_callback",r.jobId);if(u&&["effects_started","unknown","published"].includes(u.phase)){await T(r),u.phase==="published"&&await w(r,"unavailable",s);let _=await y(r.id,r.userId);return _?p(_):{state:"unknown",deliveryId:r.id}}return{state:"pending",deliveryId:r.id,expiresAtMs:r.expiresAtMs}}if(t=l.claim,!await e.validate(r.url))return p(r);if(!await le(t,s))throw Error("Delivery effects start is unconfirmed");if(n=!0,r=await he(r,t,Qe(),e.domain),!await Ae(r,t,e.domain))throw Error("Delivery authority was lost before send");let d=await fetch(r.url,{method:"POST",headers:{"content-type":"application/json","idempotency-key":r.id},body:r.payloadJson,redirect:"error",signal:AbortSignal.timeout(1e4)});if(!d.ok||d.status<200||d.status>=300)throw Error("Recipient acceptance is unconfirmed");return r=await we(r,d.status),await Ye(r,e.domain,t).catch(()=>{}),p(r)}catch(s){if(s instanceof Ee&&await w(r,"unavailable",e.domain).catch(()=>{}),t){let o=await b(t.runId,t.userId,"paa_callback",r.jobId).catch(()=>null);(n||o?.phase==="effects_started"||o?.phase==="unknown")&&await I(t).catch(()=>{})}r.sendNonce?await xe(r).catch(()=>{}):await T(r).catch(()=>{});let i=await y(r.id,r.userId).catch(()=>null);return i?p(i):{state:"unknown",deliveryId:r.id}}finally{t&&!n&&await se(t).catch(()=>{})}}async function H(e,r){let t=await Ie(e,r);return t.kind==="none"?{state:"none"}:t.kind==="unavailable"?{state:"unavailable"}:t.kind==="pending"?(await p(t.record),{state:"pending",deliveryId:t.record.id,expiresAtMs:t.record.expiresAtMs}):Ve({record:t.record,domain:t.domain,validate:async n=>{let s=await _e(n,{field:"callback_url",requireHttps:!0});return!s.error&&s.parsed?.href===n&&!s.parsed.username&&!s.parsed.password},compose:async()=>{let n=(await Oe(t.job,!1)).body;return{...n,...n.result?{result:te(n.result)}:{},...t.record.profile==="paa-queue"?{attempts:re(await We(t.job.id,t.job.user_id))}:{}}}})}async function Rt(e=25){let r=0,t=0,n=await Ne(e);for(let s of n)try{(await H(s.jobId,s.userId)).state==="accepted"?r++:t++}catch{t++}return{checked:n.length,accepted:r,pending:t}}var Xe=2,q=0;async function Ze(){let e=await Me(),r=e?void 0:await Pe(),t=e??(r?await c(r.id,Number(r.user_id)):void 0);if(!t)return{claimed:!1};let n=Date.now();q++;try{let s=await Le(t,{\u006b\u0065\u0072\u006e\u0065\u006cApiKey:B(),apifyToken:process.env.APIFY_API_TOKEN?.trim(),onAttemptEvent:ze(t.id,t.user_id,`harvest:${t.id}`),withCostContext:Y,classifyFailure:i=>{let o=L(i);return{publicError:Ge(o),httpStatus:o.httpStatus,terminalStatus:o.terminalStatus}}});return s.completed&&t.callback_url&&await H(t.id,t.user_id).catch(()=>{}),{claimed:!0,jobId:t.id,completed:s.completed,durationMs:Date.now()-n}}finally{q--}}async function Ut(e){let r=[];for(let t=0;t<e.maxJobs&&!(Date.now()>=e.deadlineMs);t++){let n=await Ze();if(r.push(n),!n.claimed)break}return r}function $t(){setInterval(async()=>{q>=Xe||Ze()},2e3),console.log(`[worker] started \u2014 polling every 2s, max ${Xe} concurrent`)}export{Ar as a,wr as b,xr as c,Nr as d,Ir as e,Dr as f,kr as g,Or as h,Xr as i,Zr as j,We as k,L as l,bt as m,Ge as n,gt as o,H as p,Rt as q,Tr as r,Te as s,Gr as t,or as u,ze as v,Ze as w,Ut as x,$t as y};