mcp-scraper 0.89.9 → 0.90.0

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 (29) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/README.md +1 -1
  3. package/dist/bin/api-server.js +1 -1
  4. package/dist/bin/mcp-scraper-cli.js +1 -1
  5. package/dist/bin/mcp-scraper-core.js +1 -1
  6. package/dist/bin/mcp-scraper-install.js +1 -1
  7. package/dist/bin/mcp-stdio-server.js +1 -1
  8. package/dist/bin/paa-harvest.js +1 -1
  9. package/dist/{chunk-Q4A2KIPZ.js → chunk-3R5QLQT3.js} +1 -1
  10. package/dist/chunk-45Z77CM7.js +1 -0
  11. package/dist/{chunk-52XUWR3X.js → chunk-HJ7OGU2J.js} +80 -80
  12. package/dist/{chunk-F4LIB772.js → chunk-M7EXIKT7.js} +6 -6
  13. package/dist/{chunk-XATD7HDY.js → chunk-RAG2WBBL.js} +2 -2
  14. package/dist/{chunk-2WWKTVCO.js → chunk-SWKUC7YD.js} +1 -1
  15. package/dist/{chunk-5B42QVDC.js → chunk-WTOKMWDS.js} +1 -1
  16. package/dist/{chunk-S3N55WHK.js → chunk-XGZJGKEG.js} +4 -4
  17. package/dist/chunk-YEFXVTMJ.js +1 -0
  18. package/dist/{chunk-VT6SLBOU.js → chunk-ZMLCK45S.js} +1 -1
  19. package/dist/{extract-bundle-DI7OVLT5.js → extract-bundle-7BI2K26D.js} +1 -1
  20. package/dist/{gmail-service-ASX7YBC3.js → gmail-service-Y2UZX3YW.js} +1 -1
  21. package/dist/index.cjs +28 -28
  22. package/dist/index.js +1 -1
  23. package/dist/{server-EYSV2RRD.js → server-TI7YYGK5.js} +532 -532
  24. package/dist/{site-extract-repository-WADCAIYB.js → site-extract-repository-T23S2T7L.js} +1 -1
  25. package/dist/{stripe-event-worker-ZPNWXX2T.js → stripe-event-worker-HVCB6SF3.js} +1 -1
  26. package/dist/{worker-577GJULQ.js → worker-IUJVVXIG.js} +1 -1
  27. package/package.json +1 -1
  28. package/dist/chunk-E4ME7J6G.js +0 -1
  29. package/dist/chunk-PVTKGAZL.js +0 -1
package/dist/index.cjs CHANGED
@@ -1,13 +1,13 @@
1
- "use strict";var No=Object.create;var ln=Object.defineProperty;var vo=Object.getOwnPropertyDescriptor;var bo=Object.getOwnPropertyNames;var _o=Object.getPrototypeOf,Eo=Object.prototype.hasOwnProperty;var Po=(e,t)=>{for(var n in t)ln(e,n,{get:t[n],enumerable:!0})},ri=(e,t,n,a)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of bo(t))!Eo.call(e,i)&&i!==n&&ln(e,i,{get:()=>t[i],enumerable:!(a=vo(t,i))||a.enumerable});return e};var Sn=(e,t,n)=>(n=e!=null?No(_o(e)):{},ri(t||!e||!e.__esModule?ln(n,"default",{value:e,enumerable:!0}):n,e)),Io=e=>ri(ln({},"__esModule",{value:!0}),e);var Dl={};Po(Dl,{VideoGenerator:()=>qn,buildClipPrompts:()=>Gn,harvest:()=>co});module.exports=Io(Dl);var w=require("zod");var jn="none",Lo="none",di=w.z.object({query:w.z.string().min(1),location:w.z.string().optional(),gl:w.z.string().length(2).default("us"),hl:w.z.string().length(2).default("en"),device:w.z.enum(["desktop","mobile"]).default("desktop"),proxyMode:w.z.enum(["location","configured","none"]).default(jn),proxyZip:w.z.string().regex(/^\d{5}$/).optional(),keepDefaultProxy:w.z.boolean().optional(),requireUsEgress:w.z.boolean().optional(),maxAttempts:w.z.number().int().min(1).max(12).optional(),debug:w.z.boolean().default(!1),depth:w.z.number().int().min(1).max(30).default(3),maxQuestions:w.z.number().int().min(1).max(100).default(100),headless:w.z.boolean().default(!1),profileDir:w.z.string().optional(),proxy:w.z.string().url().optional(),\u006b\u0065\u0072\u006e\u0065\u006cApiKey:w.z.string().optional(),\u006b\u0065\u0072\u006e\u0065\u006cProxyId:w.z.string().optional(),\u006b\u0065\u0072\u006e\u0065\u006cProfileName:w.z.string().optional(),\u006b\u0065\u0072\u006e\u0065\u006cProfileSaveChanges:w.z.boolean().optional(),\u006b\u0065\u0072\u006e\u0065\u006cStealth:w.z.boolean().optional(),serpIdentity:w.z.string().regex(/^[a-z0-9][a-z0-9_-]{0,63}$/).optional(),\u006b\u0065\u0072\u006e\u0065\u006cProxyResolution:w.z.unknown().optional(),outputDir:w.z.string().default("./paa-output"),format:w.z.enum(["json","csv","both"]).default("both"),serpOnly:w.z.boolean().default(!1),questionsOnly:w.z.boolean().default(!1),questionGrowthRecoveryRounds:w.z.number().int().min(0).max(4).default(2),pages:w.z.number().int().min(1).max(2).default(1),includeAllSerpFeatures:w.z.boolean().default(!1),includeLocalPack:w.z.boolean().default(!1),includeForums:w.z.boolean().default(!1),includeVideos:w.z.boolean().default(!1),includeAiOverview:w.z.boolean().default(!1),includeWhatPeopleSaying:w.z.boolean().default(!1),includeShortVideos:w.z.boolean().default(!1),recency:w.z.enum(["day","week","month","year"]).optional(),softDeadlineMs:w.z.number().optional()});function Re(e){switch(e){case"day":return"qdr:d";case"week":return"qdr:w";case"month":return"qdr:m";case"year":return"qdr:y";default:return}}var Kl=w.z.object({businessName:w.z.string().min(1),location:w.z.string().min(1),gl:w.z.string().length(2).default("us"),hl:w.z.string().length(2).default("en"),includeReviews:w.z.boolean().default(!1),maxReviews:w.z.number().int().min(1).max(500).default(50),includeServices:w.z.boolean().default(!1),includeImages:w.z.boolean().default(!1),imageScope:w.z.enum(["owner","all"]).default("all"),maxImages:w.z.number().int().min(1).max(250).default(100),maxInlineImages:w.z.number().int().min(0).max(5).default(3),\u006b\u0065\u0072\u006e\u0065\u006cApiKey:w.z.string().optional(),\u006b\u0065\u0072\u006e\u0065\u006cProxyId:w.z.string().optional(),headless:w.z.boolean().default(!0)}),Jl=w.z.object({query:w.z.string().min(1),location:w.z.string().optional(),gl:w.z.string().length(2).default("us"),hl:w.z.string().length(2).default("en"),maxResults:w.z.number().int().min(1).max(50).default(10),includeServices:w.z.boolean().default(!1),proxyMode:w.z.enum(["location","configured","none"]).default(Lo),proxyZip:w.z.string().regex(/^\d{5}$/).optional(),serpRedirect:w.z.boolean().optional(),forceDirectEgress:w.z.boolean().optional(),debug:w.z.boolean().default(!1),\u006b\u0065\u0072\u006e\u0065\u006cApiKey:w.z.string().optional(),\u006b\u0065\u0072\u006e\u0065\u006cProxyId:w.z.string().optional(),\u006b\u0065\u0072\u006e\u0065\u006cProxyResolution:w.z.unknown().optional(),headless:w.z.boolean().default(!0)}),li=w.z.object({questionId:w.z.string().min(1),googleLinkId:w.z.string().nullable(),googleEvidenceId:w.z.string().nullable(),question:w.z.string().min(1),answer:w.z.string().optional(),sourceTitle:w.z.string().optional(),sourceSite:w.z.string().optional(),sourceCite:w.z.string().optional(),sources:w.z.array(w.z.object({title:w.z.string().nullable(),site:w.z.string().nullable(),url:w.z.string().min(1),rawUrl:w.z.string().min(1),resolvedUrl:w.z.string().nullable(),linkType:w.z.enum(["plain","google_url_redirect","google_goto_redirect"]),resolutionStatus:w.z.enum(["not_needed","resolved","unresolved","rejected"])})).default([])}),ql=w.z.object({name:w.z.string().nullable(),rating:w.z.string().nullable(),reviewCount:w.z.string().nullable(),category:w.z.string().nullable(),address:w.z.string().nullable(),hoursSummary:w.z.string().nullable(),phone:w.z.string().nullable(),phoneDisplay:w.z.string().nullable(),website:w.z.string().nullable(),plusCode:w.z.string().nullable(),bookingUrl:w.z.string().nullable()}),Xl=w.z.object({day:w.z.string(),hours:w.z.string()}),jl=w.z.object({reviewHistogram:w.z.array(w.z.object({stars:w.z.number(),count:w.z.string()})),reviewTopics:w.z.array(w.z.object({label:w.z.string(),count:w.z.string()}))}),zl=w.z.object({reviewId:w.z.string(),author:w.z.string().nullable(),stars:w.z.string().nullable(),date:w.z.string().nullable(),text:w.z.string().nullable(),ownerResponse:w.z.string().nullable()}),$l=w.z.object({section:w.z.string(),attribute:w.z.string()});function Si(){return(process.env.BROWSER_SERVICE_API_KEY??process.env.\u004b\u0045\u0052\u004e\u0045\u004c_API_KEY)?.trim()||void 0}var In=require("playwright-extra"),ps=Sn(require("puppeteer-extra-plugin-stealth"),1),ws=require("playwright");function $(e,t,n,a,i){if(a==="m")throw new TypeError("Private method is not writable");if(a==="a"&&!i)throw new TypeError("Private accessor was defined without a setter");if(typeof t=="function"?e!==t||!i:!t.has(e))throw new TypeError("Cannot write private member to an object whose class did not declare it");return a==="a"?i.call(e,n):i?i.value=n:t.set(e,n),n}function Y(e,t,n,a){if(n==="a"&&!a)throw new TypeError("Private accessor was defined without a getter");if(typeof t=="function"?e!==t||!a:!t.has(e))throw new TypeError("Cannot read private member from an object whose class did not declare it");return n==="m"?a:n==="a"?a.call(e):a?a.value:t.get(e)}var zn=function(){let{crypto:e}=globalThis;if(e?.randomUUID)return zn=e.randomUUID.bind(e),e.randomUUID();let t=new Uint8Array(1),n=e?()=>e.getRandomValues(t)[0]:()=>Math.random()*255&255;return"10000000-1000-4000-8000-100000000000".replace(/[018]/g,a=>(+a^n()&15>>+a/4).toString(16))};function We(e){return typeof e=="object"&&e!==null&&("name"in e&&e.name==="AbortError"||"message"in e&&String(e.message).includes("FetchRequestCanceledException"))}var He=e=>{if(e instanceof Error)return e;if(typeof e=="object"&&e!==null){try{if(Object.prototype.toString.call(e)==="[object Error]"){let t=new Error(e.message,e.cause?{cause:e.cause}:{});return e.stack&&(t.stack=e.stack),e.cause&&!t.cause&&(t.cause=e.cause),e.name&&(t.name=e.name),t}}catch{}try{return new Error(JSON.stringify(e))}catch{}}return new Error(e)};var G=class extends Error{},tt=class e extends G{constructor(t,n,a,i){super(`${e.makeMessage(t,n,a)}`),this.status=t,this.headers=i,this.error=n}static makeMessage(t,n,a){let i=n?.message?typeof n.message=="string"?n.message:JSON.stringify(n.message):n?JSON.stringify(n):a;return t&&i?`${t} ${i}`:t?`${t} status code (no body)`:i||"(no status code or body)"}static generate(t,n,a,i){if(!t||!i)return new Nt({message:a,cause:He(n)});let s=n;return t===400?new se(t,s,a,i):t===401?new oe(t,s,a,i):t===403?new re(t,s,a,i):t===404?new de(t,s,a,i):t===409?new le(t,s,a,i):t===422?new Se(t,s,a,i):t===429?new Ue(t,s,a,i):t>=500?new ue(t,s,a,i):new e(t,s,a,i)}},at=class extends tt{constructor({message:t}={}){super(void 0,void 0,t||"Request was aborted.",void 0)}},Nt=class extends tt{constructor({message:t,cause:n}){super(void 0,void 0,t||"Connection error.",void 0),n&&(this.cause=n)}},wt=class extends Nt{constructor({message:t}={}){super({message:t??"Request timed out."})}},se=class extends tt{},oe=class extends tt{},re=class extends tt{},de=class extends tt{},le=class extends tt{},Se=class extends tt{},Ue=class extends tt{},ue=class extends tt{};var Oo=/^[a-z][a-z0-9+.-]*:/i,Ui=e=>Oo.test(e),et=e=>(et=Array.isArray,et(e)),$n=et;function Qn(e){return typeof e!="object"?{}:e??{}}function Zn(e){if(!e)return!0;for(let t in e)return!1;return!0}function ui(e,t){return Object.prototype.hasOwnProperty.call(e,t)}var ci=(e,t)=>{if(typeof t!="number"||!Number.isInteger(t))throw new G(`${e} must be an integer`);if(t<0)throw new G(`${e} must be a positive integer`);return t},Ro=e=>{if(typeof e=="number")return Math.round(e);if(typeof e=="string")return parseInt(e,10);throw new G(`Could not coerce ${e} (type: ${typeof e}) into a number`)};var Wo=e=>typeof e=="boolean"?e:typeof e=="string"?e==="true":!!e,hi=e=>{if(e!=null)return Ro(e)};var ta=e=>{if(e!=null)return Wo(e)},gi=e=>{try{return JSON.parse(e)}catch{return}};var pi=e=>new Promise(t=>setTimeout(t,e));var vt="0.93.0";function Ho(){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 Bo=()=>{let e=Ho();if(e==="deno")return{"X-Stainless-Lang":"js","X-Stainless-Package-Version":vt,"X-Stainless-OS":mi(Deno.build.os),"X-Stainless-Arch":wi(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":vt,"X-Stainless-OS":"Unknown","X-Stainless-Arch":`other:${EdgeRuntime}`,"X-Stainless-Runtime":"edge","X-Stainless-Runtime-Version":globalThis.process.version};if(e==="node")return{"X-Stainless-Lang":"js","X-Stainless-Package-Version":vt,"X-Stainless-OS":mi(globalThis.process.platform??"unknown"),"X-Stainless-Arch":wi(globalThis.process.arch??"unknown"),"X-Stainless-Runtime":"node","X-Stainless-Runtime-Version":globalThis.process.version??"unknown"};let t=Do();return t?{"X-Stainless-Lang":"js","X-Stainless-Package-Version":vt,"X-Stainless-OS":"Unknown","X-Stainless-Arch":"unknown","X-Stainless-Runtime":`browser:${t.browser}`,"X-Stainless-Runtime-Version":t.version}:{"X-Stainless-Lang":"js","X-Stainless-Package-Version":vt,"X-Stainless-OS":"Unknown","X-Stainless-Arch":"unknown","X-Stainless-Runtime":"unknown","X-Stainless-Runtime-Version":"unknown"}};function Do(){if(typeof navigator>"u"||!navigator)return null;let e=[{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:t,pattern:n}of e){let a=n.exec(navigator.userAgent);if(a){let i=a[1]||0,s=a[2]||0,o=a[3]||0;return{browser:t,version:`${i}.${s}.${o}`}}}return null}var wi=e=>e==="x32"?"x32":e==="x86_64"||e==="x64"?"x64":e==="arm"?"arm":e==="aarch64"||e==="arm64"?"arm64":e?`other:${e}`:"unknown",mi=e=>(e=e.toLowerCase(),e.includes("ios")?"iOS":e==="android"?"Android":e==="darwin"?"MacOS":e==="win32"?"Windows":e==="freebsd"?"FreeBSD":e==="openbsd"?"OpenBSD":e==="linux"?"Linux":e?`Other:${e}`:"Unknown"),yi,Ci=()=>yi??(yi=Bo());function ki(){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 ea(...e){let t=globalThis.ReadableStream;if(typeof t>"u")throw new Error("`ReadableStream` is not defined as a global; You will need to polyfill it, `globalThis.ReadableStream = ReadableStream`");return new t(...e)}function Un(e){let t=Symbol.asyncIterator in e?e[Symbol.asyncIterator]():e[Symbol.iterator]();return ea({start(){},async pull(n){let{done:a,value:i}=await t.next();a?n.close():n.enqueue(i)},async cancel(){await t.return?.()}})}function na(e){if(e[Symbol.asyncIterator])return e;let t=e.getReader();return{async next(){try{let n=await t.read();return n?.done&&t.releaseLock(),n}catch(n){throw t.releaseLock(),n}},async return(){let n=t.cancel();return t.releaseLock(),await n,{done:!0,value:void 0}},[Symbol.asyncIterator](){return this}}}async function Ti(e){if(e===null||typeof e!="object")return;if(e[Symbol.asyncIterator]){await e[Symbol.asyncIterator]().return?.();return}let t=e.getReader(),n=t.cancel();t.releaseLock(),await n}var Mi=({headers:e,body:t})=>({bodyHeaders:{"content-type":"application/json"},body:JSON.stringify(t)});var aa="RFC3986",ia=e=>String(e),sa={RFC1738:e=>String(e).replace(/%20/g,"+"),RFC3986:ia},fi="RFC1738";var un=(e,t)=>(un=Object.hasOwn??Function.prototype.call.bind(Object.prototype.hasOwnProperty),un(e,t)),gt=(()=>{let e=[];for(let t=0;t<256;++t)e.push("%"+((t<16?"0":"")+t.toString(16)).toUpperCase());return e})();var oa=1024,Ni=(e,t,n,a,i)=>{if(e.length===0)return e;let s=e;if(typeof e=="symbol"?s=Symbol.prototype.toString.call(e):typeof e!="string"&&(s=String(e)),n==="iso-8859-1")return escape(s).replace(/%u[0-9a-f]{4}/gi,function(d){return"%26%23"+parseInt(d.slice(2),16)+"%3B"});let o="";for(let d=0;d<s.length;d+=oa){let l=s.length>=oa?s.slice(d,d+oa):s,U=[];for(let u=0;u<l.length;++u){let r=l.charCodeAt(u);if(r===45||r===46||r===95||r===126||r>=48&&r<=57||r>=65&&r<=90||r>=97&&r<=122||i===fi&&(r===40||r===41)){U[U.length]=l.charAt(u);continue}if(r<128){U[U.length]=gt[r];continue}if(r<2048){U[U.length]=gt[192|r>>6]+gt[128|r&63];continue}if(r<55296||r>=57344){U[U.length]=gt[224|r>>12]+gt[128|r>>6&63]+gt[128|r&63];continue}u+=1,r=65536+((r&1023)<<10|l.charCodeAt(u)&1023),U[U.length]=gt[240|r>>18]+gt[128|r>>12&63]+gt[128|r>>6&63]+gt[128|r&63]}o+=U.join("")}return o};function vi(e){return!e||typeof e!="object"?!1:!!(e.constructor&&e.constructor.isBuffer&&e.constructor.isBuffer(e))}function ra(e,t){if(et(e)){let n=[];for(let a=0;a<e.length;a+=1)n.push(t(e[a]));return n}return t(e)}var _i={brackets(e){return String(e)+"[]"},comma:"comma",indices(e,t){return String(e)+"["+t+"]"},repeat(e){return String(e)}},Ei=function(e,t){Array.prototype.push.apply(e,et(t)?t:[t])},bi,q={addQueryPrefix:!1,allowDots:!1,allowEmptyArrays:!1,arrayFormat:"indices",charset:"utf-8",charsetSentinel:!1,delimiter:"&",encode:!0,encodeDotInKeys:!1,encoder:Ni,encodeValuesOnly:!1,format:aa,formatter:ia,indices:!1,serializeDate(e){return(bi??(bi=Function.prototype.call.bind(Date.prototype.toISOString)))(e)},skipNulls:!1,strictNullHandling:!1};function Vo(e){return typeof e=="string"||typeof e=="number"||typeof e=="boolean"||typeof e=="symbol"||typeof e=="bigint"}var da={};function Pi(e,t,n,a,i,s,o,d,l,U,u,r,S,c,h,m,k,f){let C=e,A=f,R=0,F=!1;for(;(A=A.get(da))!==void 0&&!F;){let y=A.get(e);if(R+=1,typeof y<"u"){if(y===R)throw new RangeError("Cyclic object value");F=!0}typeof A.get(da)>"u"&&(R=0)}if(typeof U=="function"?C=U(t,C):C instanceof Date?C=S?.(C):n==="comma"&&et(C)&&(C=ra(C,function(y){return y instanceof Date?S?.(y):y})),C===null){if(s)return l&&!m?l(t,q.encoder,k,"key",c):t;C=""}if(Vo(C)||vi(C)){if(l){let y=m?t:l(t,q.encoder,k,"key",c);return[h?.(y)+"="+h?.(l(C,q.encoder,k,"value",c))]}return[h?.(t)+"="+h?.(String(C))]}let E=[];if(typeof C>"u")return E;let M;if(n==="comma"&&et(C))m&&l&&(C=ra(C,l)),M=[{value:C.length>0?C.join(",")||null:void 0}];else if(et(U))M=U;else{let y=Object.keys(C);M=u?y.sort(u):y}let P=d?String(t).replace(/\./g,"%2E"):String(t),x=a&&et(C)&&C.length===1?P+"[]":P;if(i&&et(C)&&C.length===0)return x+"[]";for(let y=0;y<M.length;++y){let N=M[y],p=typeof N=="object"&&typeof N.value<"u"?N.value:C[N];if(o&&p===null)continue;let v=r&&d?N.replace(/\./g,"%2E"):N,_=et(C)?typeof n=="function"?n(x,v):x:x+(r?"."+v:"["+v+"]");f.set(e,R);let I=new WeakMap;I.set(da,f),Ei(E,Pi(p,_,n,a,i,s,o,d,n==="comma"&&m&&et(C)?null:l,U,u,r,S,c,h,m,k,I))}return E}function Yo(e=q){if(typeof e.allowEmptyArrays<"u"&&typeof e.allowEmptyArrays!="boolean")throw new TypeError("`allowEmptyArrays` option can only be `true` or `false`, when provided");if(typeof e.encodeDotInKeys<"u"&&typeof e.encodeDotInKeys!="boolean")throw new TypeError("`encodeDotInKeys` option can only be `true` or `false`, when provided");if(e.encoder!==null&&typeof e.encoder<"u"&&typeof e.encoder!="function")throw new TypeError("Encoder has to be a function.");let t=e.charset||q.charset;if(typeof e.charset<"u"&&e.charset!=="utf-8"&&e.charset!=="iso-8859-1")throw new TypeError("The charset option must be either utf-8, iso-8859-1, or undefined");let n=aa;if(typeof e.format<"u"){if(!un(sa,e.format))throw new TypeError("Unknown format option provided.");n=e.format}let a=sa[n],i=q.filter;(typeof e.filter=="function"||et(e.filter))&&(i=e.filter);let s;if(e.arrayFormat&&e.arrayFormat in _i?s=e.arrayFormat:"indices"in e?s=e.indices?"indices":"repeat":s=q.arrayFormat,"commaRoundTrip"in e&&typeof e.commaRoundTrip!="boolean")throw new TypeError("`commaRoundTrip` must be a boolean, or absent");let o=typeof e.allowDots>"u"?e.encodeDotInKeys?!0:q.allowDots:!!e.allowDots;return{addQueryPrefix:typeof e.addQueryPrefix=="boolean"?e.addQueryPrefix:q.addQueryPrefix,allowDots:o,allowEmptyArrays:typeof e.allowEmptyArrays=="boolean"?!!e.allowEmptyArrays:q.allowEmptyArrays,arrayFormat:s,charset:t,charsetSentinel:typeof e.charsetSentinel=="boolean"?e.charsetSentinel:q.charsetSentinel,commaRoundTrip:!!e.commaRoundTrip,delimiter:typeof e.delimiter>"u"?q.delimiter:e.delimiter,encode:typeof e.encode=="boolean"?e.encode:q.encode,encodeDotInKeys:typeof e.encodeDotInKeys=="boolean"?e.encodeDotInKeys:q.encodeDotInKeys,encoder:typeof e.encoder=="function"?e.encoder:q.encoder,encodeValuesOnly:typeof e.encodeValuesOnly=="boolean"?e.encodeValuesOnly:q.encodeValuesOnly,filter:i,format:n,formatter:a,serializeDate:typeof e.serializeDate=="function"?e.serializeDate:q.serializeDate,skipNulls:typeof e.skipNulls=="boolean"?e.skipNulls:q.skipNulls,sort:typeof e.sort=="function"?e.sort:null,strictNullHandling:typeof e.strictNullHandling=="boolean"?e.strictNullHandling:q.strictNullHandling}}function Ii(e,t={}){let n=e,a=Yo(t),i,s;typeof a.filter=="function"?(s=a.filter,n=s("",n)):et(a.filter)&&(s=a.filter,i=s);let o=[];if(typeof n!="object"||n===null)return"";let d=_i[a.arrayFormat],l=d==="comma"&&a.commaRoundTrip;i||(i=Object.keys(n)),a.sort&&i.sort(a.sort);let U=new WeakMap;for(let S=0;S<i.length;++S){let c=i[S];a.skipNulls&&n[c]===null||Ei(o,Pi(n[c],c,d,l,a.allowEmptyArrays,a.strictNullHandling,a.skipNulls,a.encodeDotInKeys,a.encode?a.encoder:null,a.filter,a.sort,a.allowDots,a.serializeDate,a.format,a.formatter,a.encodeValuesOnly,a.charset,U))}let u=o.join(a.delimiter),r=a.addQueryPrefix===!0?"?":"";return a.charsetSentinel&&(a.charset==="iso-8859-1"?r+="utf8=%26%2310003%3B&":r+="utf8=%E2%9C%93&"),u.length>0?r+u:""}function Ai(e){return Ii(e,{arrayFormat:"comma"})}function Oi(e){let t=0;for(let i of e)t+=i.length;let n=new Uint8Array(t),a=0;for(let i of e)n.set(i,a),a+=i.length;return n}var Li;function Be(e){let t;return(Li??(t=new globalThis.TextEncoder,Li=t.encode.bind(t)))(e)}var xi;function la(e){let t;return(xi??(t=new globalThis.TextDecoder,xi=t.decode.bind(t)))(e)}var lt,St,Rt=class{constructor(){lt.set(this,void 0),St.set(this,void 0),$(this,lt,new Uint8Array,"f"),$(this,St,null,"f")}decode(t){if(t==null)return[];let n=t instanceof ArrayBuffer?new Uint8Array(t):typeof t=="string"?Be(t):t;$(this,lt,Oi([Y(this,lt,"f"),n]),"f");let a=[],i;for(;(i=Jo(Y(this,lt,"f"),Y(this,St,"f")))!=null;){if(i.carriage&&Y(this,St,"f")==null){$(this,St,i.index,"f");continue}if(Y(this,St,"f")!=null&&(i.index!==Y(this,St,"f")+1||i.carriage)){a.push(la(Y(this,lt,"f").subarray(0,Y(this,St,"f")-1))),$(this,lt,Y(this,lt,"f").subarray(Y(this,St,"f")),"f"),$(this,St,null,"f");continue}let s=Y(this,St,"f")!==null?i.preceding-1:i.preceding,o=la(Y(this,lt,"f").subarray(0,s));a.push(o),$(this,lt,Y(this,lt,"f").subarray(i.index),"f"),$(this,St,null,"f")}return a}flush(){return Y(this,lt,"f").length?this.decode(`
1
+ "use strict";var vo=Object.create;var ln=Object.defineProperty;var bo=Object.getOwnPropertyDescriptor;var _o=Object.getOwnPropertyNames;var Eo=Object.getPrototypeOf,Po=Object.prototype.hasOwnProperty;var Io=(e,t)=>{for(var n in t)ln(e,n,{get:t[n],enumerable:!0})},ri=(e,t,n,a)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of _o(t))!Po.call(e,i)&&i!==n&&ln(e,i,{get:()=>t[i],enumerable:!(a=bo(t,i))||a.enumerable});return e};var Sn=(e,t,n)=>(n=e!=null?vo(Eo(e)):{},ri(t||!e||!e.__esModule?ln(n,"default",{value:e,enumerable:!0}):n,e)),Ao=e=>ri(ln({},"__esModule",{value:!0}),e);var Gl={};Io(Gl,{VideoGenerator:()=>Xn,buildClipPrompts:()=>Vn,harvest:()=>ho});module.exports=Ao(Gl);var w=require("zod");var zn="none",xo="none",di=w.z.object({query:w.z.string().min(1),location:w.z.string().optional(),gl:w.z.string().length(2).default("us"),hl:w.z.string().length(2).default("en"),device:w.z.enum(["desktop","mobile"]).default("desktop"),proxyMode:w.z.enum(["location","configured","none"]).default(zn),proxyZip:w.z.string().regex(/^\d{5}$/).optional(),keepDefaultProxy:w.z.boolean().optional(),requireUsEgress:w.z.boolean().optional(),maxAttempts:w.z.number().int().min(1).max(12).optional(),debug:w.z.boolean().default(!1),depth:w.z.number().int().min(1).max(30).default(3),maxQuestions:w.z.number().int().min(1).max(100).default(100),headless:w.z.boolean().default(!1),profileDir:w.z.string().optional(),proxy:w.z.string().url().optional(),\u006b\u0065\u0072\u006e\u0065\u006cApiKey:w.z.string().optional(),\u006b\u0065\u0072\u006e\u0065\u006cProxyId:w.z.string().optional(),\u006b\u0065\u0072\u006e\u0065\u006cProfileName:w.z.string().optional(),\u006b\u0065\u0072\u006e\u0065\u006cProfileSaveChanges:w.z.boolean().optional(),\u006b\u0065\u0072\u006e\u0065\u006cStealth:w.z.boolean().optional(),serpIdentity:w.z.string().regex(/^[a-z0-9][a-z0-9_-]{0,63}$/).optional(),\u006b\u0065\u0072\u006e\u0065\u006cProxyResolution:w.z.unknown().optional(),outputDir:w.z.string().default("./paa-output"),format:w.z.enum(["json","csv","both"]).default("both"),serpOnly:w.z.boolean().default(!1),questionsOnly:w.z.boolean().default(!1),questionGrowthRecoveryRounds:w.z.number().int().min(0).max(4).default(2),pages:w.z.number().int().min(1).max(2).default(1),includeAllSerpFeatures:w.z.boolean().default(!1),includeLocalPack:w.z.boolean().default(!1),includeForums:w.z.boolean().default(!1),includeVideos:w.z.boolean().default(!1),includeAiOverview:w.z.boolean().default(!1),includeWhatPeopleSaying:w.z.boolean().default(!1),includeShortVideos:w.z.boolean().default(!1),recency:w.z.enum(["day","week","month","year"]).optional(),softDeadlineMs:w.z.number().optional()});function Re(e){switch(e){case"day":return"qdr:d";case"week":return"qdr:w";case"month":return"qdr:m";case"year":return"qdr:y";default:return}}var ql=w.z.object({businessName:w.z.string().min(1),location:w.z.string().min(1),gl:w.z.string().length(2).default("us"),hl:w.z.string().length(2).default("en"),includeReviews:w.z.boolean().default(!1),maxReviews:w.z.number().int().min(1).max(500).default(50),includeServices:w.z.boolean().default(!1),includeImages:w.z.boolean().default(!1),imageScope:w.z.enum(["owner","all"]).default("all"),maxImages:w.z.number().int().min(1).max(250).default(100),maxInlineImages:w.z.number().int().min(0).max(5).default(3),\u006b\u0065\u0072\u006e\u0065\u006cApiKey:w.z.string().optional(),\u006b\u0065\u0072\u006e\u0065\u006cProxyId:w.z.string().optional(),headless:w.z.boolean().default(!0)}),Xl=w.z.object({query:w.z.string().min(1),location:w.z.string().optional(),gl:w.z.string().length(2).default("us"),hl:w.z.string().length(2).default("en"),maxResults:w.z.number().int().min(1).max(50).default(10),includeServices:w.z.boolean().default(!1),proxyMode:w.z.enum(["location","configured","none"]).default(xo),proxyZip:w.z.string().regex(/^\d{5}$/).optional(),serpRedirect:w.z.boolean().optional(),forceDirectEgress:w.z.boolean().optional(),debug:w.z.boolean().default(!1),\u006b\u0065\u0072\u006e\u0065\u006cApiKey:w.z.string().optional(),\u006b\u0065\u0072\u006e\u0065\u006cProxyId:w.z.string().optional(),\u006b\u0065\u0072\u006e\u0065\u006cProxyResolution:w.z.unknown().optional(),headless:w.z.boolean().default(!0)}),li=w.z.object({questionId:w.z.string().min(1),googleLinkId:w.z.string().nullable(),googleEvidenceId:w.z.string().nullable(),question:w.z.string().min(1),answer:w.z.string().optional(),sourceTitle:w.z.string().optional(),sourceSite:w.z.string().optional(),sourceCite:w.z.string().optional(),sources:w.z.array(w.z.object({title:w.z.string().nullable(),site:w.z.string().nullable(),url:w.z.string().min(1),rawUrl:w.z.string().min(1),resolvedUrl:w.z.string().nullable(),linkType:w.z.enum(["plain","google_url_redirect","google_goto_redirect"]),resolutionStatus:w.z.enum(["not_needed","resolved","unresolved","rejected"])})).default([])}),jl=w.z.object({name:w.z.string().nullable(),rating:w.z.string().nullable(),reviewCount:w.z.string().nullable(),category:w.z.string().nullable(),address:w.z.string().nullable(),hoursSummary:w.z.string().nullable(),phone:w.z.string().nullable(),phoneDisplay:w.z.string().nullable(),website:w.z.string().nullable(),plusCode:w.z.string().nullable(),bookingUrl:w.z.string().nullable()}),zl=w.z.object({day:w.z.string(),hours:w.z.string()}),$l=w.z.object({reviewHistogram:w.z.array(w.z.object({stars:w.z.number(),count:w.z.string()})),reviewTopics:w.z.array(w.z.object({label:w.z.string(),count:w.z.string()}))}),Ql=w.z.object({reviewId:w.z.string(),author:w.z.string().nullable(),stars:w.z.string().nullable(),date:w.z.string().nullable(),text:w.z.string().nullable(),ownerResponse:w.z.string().nullable()}),Zl=w.z.object({section:w.z.string(),attribute:w.z.string()});function Si(){return(process.env.BROWSER_SERVICE_API_KEY??process.env.\u004b\u0045\u0052\u004e\u0045\u004c_API_KEY)?.trim()||void 0}var In=require("playwright-extra"),ps=Sn(require("puppeteer-extra-plugin-stealth"),1),ws=require("playwright");function $(e,t,n,a,i){if(a==="m")throw new TypeError("Private method is not writable");if(a==="a"&&!i)throw new TypeError("Private accessor was defined without a setter");if(typeof t=="function"?e!==t||!i:!t.has(e))throw new TypeError("Cannot write private member to an object whose class did not declare it");return a==="a"?i.call(e,n):i?i.value=n:t.set(e,n),n}function Y(e,t,n,a){if(n==="a"&&!a)throw new TypeError("Private accessor was defined without a getter");if(typeof t=="function"?e!==t||!a:!t.has(e))throw new TypeError("Cannot read private member from an object whose class did not declare it");return n==="m"?a:n==="a"?a.call(e):a?a.value:t.get(e)}var $n=function(){let{crypto:e}=globalThis;if(e?.randomUUID)return $n=e.randomUUID.bind(e),e.randomUUID();let t=new Uint8Array(1),n=e?()=>e.getRandomValues(t)[0]:()=>Math.random()*255&255;return"10000000-1000-4000-8000-100000000000".replace(/[018]/g,a=>(+a^n()&15>>+a/4).toString(16))};function We(e){return typeof e=="object"&&e!==null&&("name"in e&&e.name==="AbortError"||"message"in e&&String(e.message).includes("FetchRequestCanceledException"))}var He=e=>{if(e instanceof Error)return e;if(typeof e=="object"&&e!==null){try{if(Object.prototype.toString.call(e)==="[object Error]"){let t=new Error(e.message,e.cause?{cause:e.cause}:{});return e.stack&&(t.stack=e.stack),e.cause&&!t.cause&&(t.cause=e.cause),e.name&&(t.name=e.name),t}}catch{}try{return new Error(JSON.stringify(e))}catch{}}return new Error(e)};var G=class extends Error{},tt=class e extends G{constructor(t,n,a,i){super(`${e.makeMessage(t,n,a)}`),this.status=t,this.headers=i,this.error=n}static makeMessage(t,n,a){let i=n?.message?typeof n.message=="string"?n.message:JSON.stringify(n.message):n?JSON.stringify(n):a;return t&&i?`${t} ${i}`:t?`${t} status code (no body)`:i||"(no status code or body)"}static generate(t,n,a,i){if(!t||!i)return new Nt({message:a,cause:He(n)});let s=n;return t===400?new se(t,s,a,i):t===401?new oe(t,s,a,i):t===403?new re(t,s,a,i):t===404?new de(t,s,a,i):t===409?new le(t,s,a,i):t===422?new Se(t,s,a,i):t===429?new Ue(t,s,a,i):t>=500?new ue(t,s,a,i):new e(t,s,a,i)}},at=class extends tt{constructor({message:t}={}){super(void 0,void 0,t||"Request was aborted.",void 0)}},Nt=class extends tt{constructor({message:t,cause:n}){super(void 0,void 0,t||"Connection error.",void 0),n&&(this.cause=n)}},wt=class extends Nt{constructor({message:t}={}){super({message:t??"Request timed out."})}},se=class extends tt{},oe=class extends tt{},re=class extends tt{},de=class extends tt{},le=class extends tt{},Se=class extends tt{},Ue=class extends tt{},ue=class extends tt{};var Ro=/^[a-z][a-z0-9+.-]*:/i,Ui=e=>Ro.test(e),et=e=>(et=Array.isArray,et(e)),Qn=et;function Zn(e){return typeof e!="object"?{}:e??{}}function ta(e){if(!e)return!0;for(let t in e)return!1;return!0}function ui(e,t){return Object.prototype.hasOwnProperty.call(e,t)}var ci=(e,t)=>{if(typeof t!="number"||!Number.isInteger(t))throw new G(`${e} must be an integer`);if(t<0)throw new G(`${e} must be a positive integer`);return t},Wo=e=>{if(typeof e=="number")return Math.round(e);if(typeof e=="string")return parseInt(e,10);throw new G(`Could not coerce ${e} (type: ${typeof e}) into a number`)};var Ho=e=>typeof e=="boolean"?e:typeof e=="string"?e==="true":!!e,hi=e=>{if(e!=null)return Wo(e)};var ea=e=>{if(e!=null)return Ho(e)},gi=e=>{try{return JSON.parse(e)}catch{return}};var pi=e=>new Promise(t=>setTimeout(t,e));var vt="0.93.0";function Bo(){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 Do=()=>{let e=Bo();if(e==="deno")return{"X-Stainless-Lang":"js","X-Stainless-Package-Version":vt,"X-Stainless-OS":mi(Deno.build.os),"X-Stainless-Arch":wi(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":vt,"X-Stainless-OS":"Unknown","X-Stainless-Arch":`other:${EdgeRuntime}`,"X-Stainless-Runtime":"edge","X-Stainless-Runtime-Version":globalThis.process.version};if(e==="node")return{"X-Stainless-Lang":"js","X-Stainless-Package-Version":vt,"X-Stainless-OS":mi(globalThis.process.platform??"unknown"),"X-Stainless-Arch":wi(globalThis.process.arch??"unknown"),"X-Stainless-Runtime":"node","X-Stainless-Runtime-Version":globalThis.process.version??"unknown"};let t=Fo();return t?{"X-Stainless-Lang":"js","X-Stainless-Package-Version":vt,"X-Stainless-OS":"Unknown","X-Stainless-Arch":"unknown","X-Stainless-Runtime":`browser:${t.browser}`,"X-Stainless-Runtime-Version":t.version}:{"X-Stainless-Lang":"js","X-Stainless-Package-Version":vt,"X-Stainless-OS":"Unknown","X-Stainless-Arch":"unknown","X-Stainless-Runtime":"unknown","X-Stainless-Runtime-Version":"unknown"}};function Fo(){if(typeof navigator>"u"||!navigator)return null;let e=[{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:t,pattern:n}of e){let a=n.exec(navigator.userAgent);if(a){let i=a[1]||0,s=a[2]||0,o=a[3]||0;return{browser:t,version:`${i}.${s}.${o}`}}}return null}var wi=e=>e==="x32"?"x32":e==="x86_64"||e==="x64"?"x64":e==="arm"?"arm":e==="aarch64"||e==="arm64"?"arm64":e?`other:${e}`:"unknown",mi=e=>(e=e.toLowerCase(),e.includes("ios")?"iOS":e==="android"?"Android":e==="darwin"?"MacOS":e==="win32"?"Windows":e==="freebsd"?"FreeBSD":e==="openbsd"?"OpenBSD":e==="linux"?"Linux":e?`Other:${e}`:"Unknown"),yi,Ci=()=>yi??(yi=Do());function ki(){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 na(...e){let t=globalThis.ReadableStream;if(typeof t>"u")throw new Error("`ReadableStream` is not defined as a global; You will need to polyfill it, `globalThis.ReadableStream = ReadableStream`");return new t(...e)}function Un(e){let t=Symbol.asyncIterator in e?e[Symbol.asyncIterator]():e[Symbol.iterator]();return na({start(){},async pull(n){let{done:a,value:i}=await t.next();a?n.close():n.enqueue(i)},async cancel(){await t.return?.()}})}function aa(e){if(e[Symbol.asyncIterator])return e;let t=e.getReader();return{async next(){try{let n=await t.read();return n?.done&&t.releaseLock(),n}catch(n){throw t.releaseLock(),n}},async return(){let n=t.cancel();return t.releaseLock(),await n,{done:!0,value:void 0}},[Symbol.asyncIterator](){return this}}}async function Ti(e){if(e===null||typeof e!="object")return;if(e[Symbol.asyncIterator]){await e[Symbol.asyncIterator]().return?.();return}let t=e.getReader(),n=t.cancel();t.releaseLock(),await n}var Mi=({headers:e,body:t})=>({bodyHeaders:{"content-type":"application/json"},body:JSON.stringify(t)});var ia="RFC3986",sa=e=>String(e),oa={RFC1738:e=>String(e).replace(/%20/g,"+"),RFC3986:sa},fi="RFC1738";var un=(e,t)=>(un=Object.hasOwn??Function.prototype.call.bind(Object.prototype.hasOwnProperty),un(e,t)),gt=(()=>{let e=[];for(let t=0;t<256;++t)e.push("%"+((t<16?"0":"")+t.toString(16)).toUpperCase());return e})();var ra=1024,Ni=(e,t,n,a,i)=>{if(e.length===0)return e;let s=e;if(typeof e=="symbol"?s=Symbol.prototype.toString.call(e):typeof e!="string"&&(s=String(e)),n==="iso-8859-1")return escape(s).replace(/%u[0-9a-f]{4}/gi,function(d){return"%26%23"+parseInt(d.slice(2),16)+"%3B"});let o="";for(let d=0;d<s.length;d+=ra){let l=s.length>=ra?s.slice(d,d+ra):s,U=[];for(let u=0;u<l.length;++u){let r=l.charCodeAt(u);if(r===45||r===46||r===95||r===126||r>=48&&r<=57||r>=65&&r<=90||r>=97&&r<=122||i===fi&&(r===40||r===41)){U[U.length]=l.charAt(u);continue}if(r<128){U[U.length]=gt[r];continue}if(r<2048){U[U.length]=gt[192|r>>6]+gt[128|r&63];continue}if(r<55296||r>=57344){U[U.length]=gt[224|r>>12]+gt[128|r>>6&63]+gt[128|r&63];continue}u+=1,r=65536+((r&1023)<<10|l.charCodeAt(u)&1023),U[U.length]=gt[240|r>>18]+gt[128|r>>12&63]+gt[128|r>>6&63]+gt[128|r&63]}o+=U.join("")}return o};function vi(e){return!e||typeof e!="object"?!1:!!(e.constructor&&e.constructor.isBuffer&&e.constructor.isBuffer(e))}function da(e,t){if(et(e)){let n=[];for(let a=0;a<e.length;a+=1)n.push(t(e[a]));return n}return t(e)}var _i={brackets(e){return String(e)+"[]"},comma:"comma",indices(e,t){return String(e)+"["+t+"]"},repeat(e){return String(e)}},Ei=function(e,t){Array.prototype.push.apply(e,et(t)?t:[t])},bi,q={addQueryPrefix:!1,allowDots:!1,allowEmptyArrays:!1,arrayFormat:"indices",charset:"utf-8",charsetSentinel:!1,delimiter:"&",encode:!0,encodeDotInKeys:!1,encoder:Ni,encodeValuesOnly:!1,format:ia,formatter:sa,indices:!1,serializeDate(e){return(bi??(bi=Function.prototype.call.bind(Date.prototype.toISOString)))(e)},skipNulls:!1,strictNullHandling:!1};function Yo(e){return typeof e=="string"||typeof e=="number"||typeof e=="boolean"||typeof e=="symbol"||typeof e=="bigint"}var la={};function Pi(e,t,n,a,i,s,o,d,l,U,u,r,S,c,h,m,k,f){let C=e,A=f,R=0,F=!1;for(;(A=A.get(la))!==void 0&&!F;){let y=A.get(e);if(R+=1,typeof y<"u"){if(y===R)throw new RangeError("Cyclic object value");F=!0}typeof A.get(la)>"u"&&(R=0)}if(typeof U=="function"?C=U(t,C):C instanceof Date?C=S?.(C):n==="comma"&&et(C)&&(C=da(C,function(y){return y instanceof Date?S?.(y):y})),C===null){if(s)return l&&!m?l(t,q.encoder,k,"key",c):t;C=""}if(Yo(C)||vi(C)){if(l){let y=m?t:l(t,q.encoder,k,"key",c);return[h?.(y)+"="+h?.(l(C,q.encoder,k,"value",c))]}return[h?.(t)+"="+h?.(String(C))]}let E=[];if(typeof C>"u")return E;let M;if(n==="comma"&&et(C))m&&l&&(C=da(C,l)),M=[{value:C.length>0?C.join(",")||null:void 0}];else if(et(U))M=U;else{let y=Object.keys(C);M=u?y.sort(u):y}let P=d?String(t).replace(/\./g,"%2E"):String(t),x=a&&et(C)&&C.length===1?P+"[]":P;if(i&&et(C)&&C.length===0)return x+"[]";for(let y=0;y<M.length;++y){let N=M[y],p=typeof N=="object"&&typeof N.value<"u"?N.value:C[N];if(o&&p===null)continue;let v=r&&d?N.replace(/\./g,"%2E"):N,_=et(C)?typeof n=="function"?n(x,v):x:x+(r?"."+v:"["+v+"]");f.set(e,R);let I=new WeakMap;I.set(la,f),Ei(E,Pi(p,_,n,a,i,s,o,d,n==="comma"&&m&&et(C)?null:l,U,u,r,S,c,h,m,k,I))}return E}function Ko(e=q){if(typeof e.allowEmptyArrays<"u"&&typeof e.allowEmptyArrays!="boolean")throw new TypeError("`allowEmptyArrays` option can only be `true` or `false`, when provided");if(typeof e.encodeDotInKeys<"u"&&typeof e.encodeDotInKeys!="boolean")throw new TypeError("`encodeDotInKeys` option can only be `true` or `false`, when provided");if(e.encoder!==null&&typeof e.encoder<"u"&&typeof e.encoder!="function")throw new TypeError("Encoder has to be a function.");let t=e.charset||q.charset;if(typeof e.charset<"u"&&e.charset!=="utf-8"&&e.charset!=="iso-8859-1")throw new TypeError("The charset option must be either utf-8, iso-8859-1, or undefined");let n=ia;if(typeof e.format<"u"){if(!un(oa,e.format))throw new TypeError("Unknown format option provided.");n=e.format}let a=oa[n],i=q.filter;(typeof e.filter=="function"||et(e.filter))&&(i=e.filter);let s;if(e.arrayFormat&&e.arrayFormat in _i?s=e.arrayFormat:"indices"in e?s=e.indices?"indices":"repeat":s=q.arrayFormat,"commaRoundTrip"in e&&typeof e.commaRoundTrip!="boolean")throw new TypeError("`commaRoundTrip` must be a boolean, or absent");let o=typeof e.allowDots>"u"?e.encodeDotInKeys?!0:q.allowDots:!!e.allowDots;return{addQueryPrefix:typeof e.addQueryPrefix=="boolean"?e.addQueryPrefix:q.addQueryPrefix,allowDots:o,allowEmptyArrays:typeof e.allowEmptyArrays=="boolean"?!!e.allowEmptyArrays:q.allowEmptyArrays,arrayFormat:s,charset:t,charsetSentinel:typeof e.charsetSentinel=="boolean"?e.charsetSentinel:q.charsetSentinel,commaRoundTrip:!!e.commaRoundTrip,delimiter:typeof e.delimiter>"u"?q.delimiter:e.delimiter,encode:typeof e.encode=="boolean"?e.encode:q.encode,encodeDotInKeys:typeof e.encodeDotInKeys=="boolean"?e.encodeDotInKeys:q.encodeDotInKeys,encoder:typeof e.encoder=="function"?e.encoder:q.encoder,encodeValuesOnly:typeof e.encodeValuesOnly=="boolean"?e.encodeValuesOnly:q.encodeValuesOnly,filter:i,format:n,formatter:a,serializeDate:typeof e.serializeDate=="function"?e.serializeDate:q.serializeDate,skipNulls:typeof e.skipNulls=="boolean"?e.skipNulls:q.skipNulls,sort:typeof e.sort=="function"?e.sort:null,strictNullHandling:typeof e.strictNullHandling=="boolean"?e.strictNullHandling:q.strictNullHandling}}function Ii(e,t={}){let n=e,a=Ko(t),i,s;typeof a.filter=="function"?(s=a.filter,n=s("",n)):et(a.filter)&&(s=a.filter,i=s);let o=[];if(typeof n!="object"||n===null)return"";let d=_i[a.arrayFormat],l=d==="comma"&&a.commaRoundTrip;i||(i=Object.keys(n)),a.sort&&i.sort(a.sort);let U=new WeakMap;for(let S=0;S<i.length;++S){let c=i[S];a.skipNulls&&n[c]===null||Ei(o,Pi(n[c],c,d,l,a.allowEmptyArrays,a.strictNullHandling,a.skipNulls,a.encodeDotInKeys,a.encode?a.encoder:null,a.filter,a.sort,a.allowDots,a.serializeDate,a.format,a.formatter,a.encodeValuesOnly,a.charset,U))}let u=o.join(a.delimiter),r=a.addQueryPrefix===!0?"?":"";return a.charsetSentinel&&(a.charset==="iso-8859-1"?r+="utf8=%26%2310003%3B&":r+="utf8=%E2%9C%93&"),u.length>0?r+u:""}function Ai(e){return Ii(e,{arrayFormat:"comma"})}function Oi(e){let t=0;for(let i of e)t+=i.length;let n=new Uint8Array(t),a=0;for(let i of e)n.set(i,a),a+=i.length;return n}var Li;function Be(e){let t;return(Li??(t=new globalThis.TextEncoder,Li=t.encode.bind(t)))(e)}var xi;function Sa(e){let t;return(xi??(t=new globalThis.TextDecoder,xi=t.decode.bind(t)))(e)}var lt,St,Rt=class{constructor(){lt.set(this,void 0),St.set(this,void 0),$(this,lt,new Uint8Array,"f"),$(this,St,null,"f")}decode(t){if(t==null)return[];let n=t instanceof ArrayBuffer?new Uint8Array(t):typeof t=="string"?Be(t):t;$(this,lt,Oi([Y(this,lt,"f"),n]),"f");let a=[],i;for(;(i=qo(Y(this,lt,"f"),Y(this,St,"f")))!=null;){if(i.carriage&&Y(this,St,"f")==null){$(this,St,i.index,"f");continue}if(Y(this,St,"f")!=null&&(i.index!==Y(this,St,"f")+1||i.carriage)){a.push(Sa(Y(this,lt,"f").subarray(0,Y(this,St,"f")-1))),$(this,lt,Y(this,lt,"f").subarray(Y(this,St,"f")),"f"),$(this,St,null,"f");continue}let s=Y(this,St,"f")!==null?i.preceding-1:i.preceding,o=Sa(Y(this,lt,"f").subarray(0,s));a.push(o),$(this,lt,Y(this,lt,"f").subarray(i.index),"f"),$(this,St,null,"f")}return a}flush(){return Y(this,lt,"f").length?this.decode(`
2
2
  `):[]}};lt=new WeakMap,St=new WeakMap;Rt.NEWLINE_CHARS=new Set([`
3
- `,"\r"]);Rt.NEWLINE_REGEXP=/\r\n|[\n\r]/g;function Jo(e,t){for(let i=t??0;i<e.length;i++){if(e[i]===10)return{preceding:i,index:i+1,carriage:!1};if(e[i]===13)return{preceding:i,index:i+1,carriage:!0}}return null}function Ri(e){for(let a=0;a<e.length-1;a++){if(e[a]===10&&e[a+1]===10||e[a]===13&&e[a+1]===13)return a+2;if(e[a]===13&&e[a+1]===10&&a+3<e.length&&e[a+2]===13&&e[a+3]===10)return a+4}return-1}var hn={off:0,error:200,warn:300,info:400,debug:500},Sa=(e,t,n)=>{if(e){if(ui(hn,e))return e;X(n).warn(`${t} was set to ${JSON.stringify(e)}, expected one of ${JSON.stringify(Object.keys(hn))}`)}};function De(){}function cn(e,t,n){return!t||hn[e]>hn[n]?De:t[e].bind(t)}var qo={error:De,warn:De,info:De,debug:De},Wi=new WeakMap;function X(e){let t=e.logger,n=e.logLevel??"off";if(!t)return qo;let a=Wi.get(t);if(a&&a[0]===n)return a[1];let i={error:cn("error",t,n),warn:cn("warn",t,n),info:cn("info",t,n),debug:cn("debug",t,n)};return Wi.set(t,[n,i]),i}var mt=e=>(e.options&&(e.options={...e.options},delete e.options.headers),e.headers&&(e.headers=Object.fromEntries((e.headers instanceof Headers?[...e.headers]:Object.entries(e.headers)).map(([t,n])=>[t,t.toLowerCase()==="authorization"||t.toLowerCase()==="api-key"||t.toLowerCase()==="x-api-key"||t.toLowerCase()==="cookie"||t.toLowerCase()==="set-cookie"?"***":n]))),"retryOfRequestLogID"in e&&(e.retryOfRequestLogID&&(e.retryOf=e.retryOfRequestLogID),delete e.retryOfRequestLogID),e);var Fe,gn=class e{constructor(t,n,a){this.iterator=t,Fe.set(this,void 0),this.controller=n,$(this,Fe,a,"f")}static fromSSEResponse(t,n,a){let i=!1,s=a?X(a):console;async function*o(){if(i)throw new G("Cannot iterate over a consumed stream, use `.tee()` to split the stream.");i=!0;let d=!1;try{for await(let l of Xo(t,n))try{yield JSON.parse(l.data)}catch(U){throw s.error("Could not parse message into JSON:",l.data),s.error("From chunk:",l.raw),U}d=!0}catch(l){if(We(l))return;throw l}finally{d||n.abort()}}return new e(o,n,a)}static fromReadableStream(t,n,a){let i=!1;async function*s(){let d=new Rt,l=na(t);for await(let U of l)for(let u of d.decode(U))yield u;for(let U of d.flush())yield U}async function*o(){if(i)throw new G("Cannot iterate over a consumed stream, use `.tee()` to split the stream.");i=!0;let d=!1;try{for await(let l of s())d||l&&(yield JSON.parse(l));d=!0}catch(l){if(We(l))return;throw l}finally{d||n.abort()}}return new e(o,n,a)}[(Fe=new WeakMap,Symbol.asyncIterator)](){return this.iterator()}tee(){let t=[],n=[],a=this.iterator(),i=s=>({next:()=>{if(s.length===0){let o=a.next();t.push(o),n.push(o)}return s.shift()}});return[new e(()=>i(t),this.controller,Y(this,Fe,"f")),new e(()=>i(n),this.controller,Y(this,Fe,"f"))]}toReadableStream(){let t=this,n;return ea({async start(){n=t[Symbol.asyncIterator]()},async pull(a){try{let{value:i,done:s}=await n.next();if(s)return a.close();let o=Be(JSON.stringify(i)+`
4
- `);a.enqueue(o)}catch(i){a.error(i)}},async cancel(){await n.return?.()}})}};async function*Xo(e,t){if(!e.body)throw t.abort(),typeof globalThis.navigator<"u"&&globalThis.navigator.product==="ReactNative"?new G("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 G("Attempted to iterate over a response with no body");let n=new Ua,a=new Rt,i=na(e.body);for await(let s of jo(i))for(let o of a.decode(s)){let d=n.decode(o);d&&(yield d)}for(let s of a.flush()){let o=n.decode(s);o&&(yield o)}}async function*jo(e){let t=new Uint8Array;for await(let n of e){if(n==null)continue;let a=n instanceof ArrayBuffer?new Uint8Array(n):typeof n=="string"?Be(n):n,i=new Uint8Array(t.length+a.length);i.set(t),i.set(a,t.length),t=i;let s;for(;(s=Ri(t))!==-1;)yield t.slice(0,s),t=t.slice(s)}t.length>0&&(yield t)}var Ua=class{constructor(){this.event=null,this.data=[],this.chunks=[]}decode(t){if(t.endsWith("\r")&&(t=t.substring(0,t.length-1)),!t){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(t),t.startsWith(":"))return null;let[n,a,i]=zo(t,":");return i.startsWith(" ")&&(i=i.substring(1)),n==="event"?this.event=i:n==="data"&&this.data.push(i),null}};function zo(e,t){let n=e.indexOf(t);return n!==-1?[e.substring(0,n),t,e.substring(n+t.length)]:[e,"",""]}async function pn(e,t){let{response:n,requestLogID:a,retryOfRequestLogID:i,startTime:s}=t,o=await(async()=>{if(t.options.stream)return X(e).debug("response",n.status,n.url,n.headers,n.body),t.options.__streamClass?t.options.__streamClass.fromSSEResponse(n,t.controller,e):gn.fromSSEResponse(n,t.controller,e);if(n.status===204)return null;if(t.options.__binaryResponse)return n;let l=n.headers.get("content-type")?.split(";")[0]?.trim();return l?.includes("application/json")||l?.endsWith("+json")?n.headers.get("content-length")==="0"?void 0:await n.json():await n.text()})();return X(e).debug(`[${a}] response parsed`,mt({retryOfRequestLogID:i,url:n.url,status:n.status,body:o,durationMs:Date.now()-s})),o}var Ge,Wt=class e extends Promise{constructor(t,n,a=pn){super(i=>{i(null)}),this.responsePromise=n,this.parseResponse=a,Ge.set(this,void 0),$(this,Ge,t,"f")}_thenUnwrap(t){return new e(Y(this,Ge,"f"),this.responsePromise,async(n,a)=>t(await this.parseResponse(n,a),a))}asResponse(){return this.responsePromise.then(t=>t.response)}async withResponse(){let[t,n]=await Promise.all([this.parse(),this.asResponse()]);return{data:t,response:n}}parse(){return this.parsedPromise||(this.parsedPromise=this.responsePromise.then(t=>this.parseResponse(Y(this,Ge,"f"),t))),this.parsedPromise}then(t,n){return this.parse().then(t,n)}catch(t){return this.parse().catch(t)}finally(t){return this.parse().finally(t)}};Ge=new WeakMap;var wn,mn=class{constructor(t,n,a,i){wn.set(this,void 0),$(this,wn,t,"f"),this.options=i,this.response=n,this.body=a}hasNextPage(){return this.getPaginatedItems().length?this.nextPageRequestOptions()!=null:!1}async getNextPage(){let t=this.nextPageRequestOptions();if(!t)throw new G("No next page expected; please check `.hasNextPage()` before calling `.getNextPage()`.");return await Y(this,wn,"f").requestAPIList(this.constructor,t)}async*iterPages(){let t=this;for(yield t;t.hasNextPage();)t=await t.getNextPage(),yield t}async*[(wn=new WeakMap,Symbol.asyncIterator)](){for await(let t of this.iterPages())for(let n of t.getPaginatedItems())yield n}},Ve=class extends Wt{constructor(t,n,a){super(t,n,async(i,s)=>new a(i,s.response,await pn(i,s),s.options))}async*[Symbol.asyncIterator](){let t=await this;for await(let n of t)yield n}},yn=class extends mn{constructor(t,n,a,i){super(t,n,a,i),this.items=a||[],this.next_page_token=this.response.headers.get("x-next-page-token")??null,this.has_more=ta(this.response.headers.get("x-has-more"))??null}getPaginatedItems(){return this.items??[]}hasNextPage(){return this.has_more===!1?!1:super.hasNextPage()}nextPageRequestOptions(){let t=this.next_page_token;return t?{...this.options,query:{...Qn(this.options.query),page_token:t}}:null}},B=class extends mn{constructor(t,n,a,i){super(t,n,a,i),this.items=a||[],this.has_more=ta(this.response.headers.get("x-has-more"))??null,this.next_offset=hi(this.response.headers.get("x-next-offset"))??null}getPaginatedItems(){return this.items??[]}hasNextPage(){return this.has_more===!1?!1:super.hasNextPage()}nextPageRequestOptions(){let t=this.next_offset;if(t==null){if(this.has_more)throw new G("Server reported X-Has-More: true without an X-Next-Offset header; refusing to silently truncate pagination");return null}return t===0?null:{...this.options,query:{...Qn(this.options.query),offset:t}}}};var ha=()=>{if(typeof File>"u"){let{process:e}=globalThis,t=typeof e?.versions?.node=="string"&&parseInt(e.versions.node.split("."))<20;throw new Error("`File` is not defined as a global, which is required for file uploads."+(t?" Update to Node 20 LTS or newer, or set `globalThis.File` to `import('node:buffer').File`.":""))}};function ce(e,t,n){return ha(),new File(e,t??"unknown_file",n)}function Ye(e){return(typeof e=="object"&&e!==null&&("name"in e&&e.name&&String(e.name)||"url"in e&&e.url&&String(e.url)||"filename"in e&&e.filename&&String(e.filename)||"path"in e&&e.path&&String(e.path))||"").split(/[\\/]/).pop()||void 0}var Cn=e=>e!=null&&typeof e=="object"&&typeof e[Symbol.asyncIterator]=="function";var yt=async(e,t)=>({...e,body:await Zo(e.body,t)}),Hi=new WeakMap;function Qo(e){let t=typeof e=="function"?e:e.fetch,n=Hi.get(t);if(n)return n;let a=(async()=>{try{let i="Response"in t?t.Response:(await t("data:,")).constructor,s=new FormData;return s.toString()!==await new i(s).text()}catch{return!0}})();return Hi.set(t,a),a}var Zo=async(e,t)=>{if(!await Qo(t))throw new TypeError("The provided fetch function does not support file uploads with the current global FormData class.");let n=new FormData;return await Promise.all(Object.entries(e||{}).map(([a,i])=>ca(n,a,i))),n},Bi=e=>e instanceof Blob&&"name"in e,tr=e=>typeof e=="object"&&e!==null&&(e instanceof Response||Cn(e)||Bi(e)),ua=e=>{if(tr(e))return!0;if(Array.isArray(e))return e.some(ua);if(e&&typeof e=="object"){for(let t in e)if(ua(e[t]))return!0}return!1},ca=async(e,t,n)=>{if(n!==void 0){if(n==null)throw new TypeError(`Received null for "${t}"; to pass null in FormData, you must use the string 'null'`);if(typeof n=="string"||typeof n=="number"||typeof n=="boolean")e.append(t,String(n));else if(n instanceof Response)e.append(t,ce([await n.blob()],Ye(n)));else if(Cn(n))e.append(t,ce([await new Response(Un(n)).blob()],Ye(n)));else if(Bi(n))e.append(t,n,Ye(n));else if(Array.isArray(n))await Promise.all(n.map((a,i)=>ca(e,`${t}[${i}]`,a)));else if(typeof n=="object")!(t==="env_vars")&&!ua(n)?Object.entries(n).filter(([s,o])=>o!==void 0).length>0&&e.append(t,JSON.stringify(n)):await Promise.all(Object.entries(n).map(([i,s])=>ca(e,`${t}[${i}]`,s)));else throw new TypeError(`Invalid value given to form, expected a string, number, boolean, object, Array, File or Blob but got ${n} instead`)}};var Di=e=>e!=null&&typeof e=="object"&&typeof e.size=="number"&&typeof e.type=="string"&&typeof e.text=="function"&&typeof e.slice=="function"&&typeof e.arrayBuffer=="function",er=e=>e!=null&&typeof e=="object"&&typeof e.name=="string"&&typeof e.lastModified=="number"&&Di(e),nr=e=>e!=null&&typeof e=="object"&&typeof e.url=="string"&&typeof e.blob=="function";async function kn(e,t,n){if(ha(),e=await e,er(e))return e instanceof File?e:ce([await e.arrayBuffer()],e.name);if(nr(e)){let i=await e.blob();return t||(t=new URL(e.url).pathname.split(/[\\/]/).pop()),ce(await ga(i),t,n)}let a=await ga(e);if(t||(t=Ye(e)),!n?.type){let i=a.find(s=>typeof s=="object"&&"type"in s&&s.type);typeof i=="string"&&(n={...n,type:i})}return ce(a,t,n)}async function ga(e){let t=[];if(typeof e=="string"||ArrayBuffer.isView(e)||e instanceof ArrayBuffer)t.push(e);else if(Di(e))t.push(e instanceof Blob?e:await e.arrayBuffer());else if(Cn(e))for await(let n of e)t.push(...await ga(n));else{let n=e?.constructor?.name;throw new Error(`Unexpected data type: ${typeof e}${n?`; constructor: ${n}`:""}${ar(e)}`)}return t}function ar(e){return typeof e!="object"||e===null?"":`; props: [${Object.getOwnPropertyNames(e).map(n=>`"${n}"`).join(", ")}]`}var b=class{constructor(t){this._client=t}};var Fi=Symbol("brand.privateNullableHeaders");function*sr(e){if(!e)return;if(Fi in e){let{values:a,nulls:i}=e;yield*a.entries();for(let s of i)yield[s,null];return}let t=!1,n;e instanceof Headers?n=e.entries():$n(e)?n=e:(t=!0,n=Object.entries(e??{}));for(let a of n){let i=a[0];if(typeof i!="string")throw new TypeError("expected header name to be a string");let s=$n(a[1])?a[1]:[a[1]],o=!1;for(let d of s)d!==void 0&&(t&&!o&&(o=!0,yield[i,null]),yield[i,d])}}var T=e=>{let t=new Headers,n=new Set;for(let a of e){let i=new Set;for(let[s,o]of sr(a)){let d=s.toLowerCase();i.has(d)||(t.delete(s),i.add(d)),o===null?(t.delete(s),n.add(d)):(t.append(s,o),n.delete(d))}}return{[Fi]:!0,values:t,nulls:n}};function Vi(e){return e.replace(/[^A-Za-z0-9\-._~!$&'()*+,;=:@]+/g,encodeURIComponent)}var Gi=Object.freeze(Object.create(null)),or=(e=Vi)=>function(n,...a){if(n.length===1)return n[0];let i=!1,s=[],o=n.reduce((u,r,S)=>{/[?#]/.test(r)&&(i=!0);let c=a[S],h=(i?encodeURIComponent:e)(""+c);return S!==a.length&&(c==null||typeof c=="object"&&c.toString===Object.getPrototypeOf(Object.getPrototypeOf(c.hasOwnProperty??Gi)??Gi)?.toString)&&(h=c+"",s.push({start:u.length+r.length,length:h.length,error:`Value of type ${Object.prototype.toString.call(c).slice(8,-1)} is not a valid path parameter`})),u+r+(S===a.length?"":h)},""),d=o.split(/[?#]/,1)[0],l=/(?<=^|\/)(?:\.|%2e){1,2}(?=\/|$)/gi,U;for(;(U=l.exec(d))!==null;)s.push({start:U.index,length:U[0].length,error:`Value "${U[0]}" can't be safely passed as a path parameter`});if(s.sort((u,r)=>u.start-r.start),s.length>0){let u=0,r=s.reduce((S,c)=>{let h=" ".repeat(c.start-u),m="^".repeat(c.length);return u=c.start+c.length,S+h+m},"");throw new G(`Path parameters result in path with invalid segments:
3
+ `,"\r"]);Rt.NEWLINE_REGEXP=/\r\n|[\n\r]/g;function qo(e,t){for(let i=t??0;i<e.length;i++){if(e[i]===10)return{preceding:i,index:i+1,carriage:!1};if(e[i]===13)return{preceding:i,index:i+1,carriage:!0}}return null}function Ri(e){for(let a=0;a<e.length-1;a++){if(e[a]===10&&e[a+1]===10||e[a]===13&&e[a+1]===13)return a+2;if(e[a]===13&&e[a+1]===10&&a+3<e.length&&e[a+2]===13&&e[a+3]===10)return a+4}return-1}var hn={off:0,error:200,warn:300,info:400,debug:500},Ua=(e,t,n)=>{if(e){if(ui(hn,e))return e;X(n).warn(`${t} was set to ${JSON.stringify(e)}, expected one of ${JSON.stringify(Object.keys(hn))}`)}};function De(){}function cn(e,t,n){return!t||hn[e]>hn[n]?De:t[e].bind(t)}var Xo={error:De,warn:De,info:De,debug:De},Wi=new WeakMap;function X(e){let t=e.logger,n=e.logLevel??"off";if(!t)return Xo;let a=Wi.get(t);if(a&&a[0]===n)return a[1];let i={error:cn("error",t,n),warn:cn("warn",t,n),info:cn("info",t,n),debug:cn("debug",t,n)};return Wi.set(t,[n,i]),i}var mt=e=>(e.options&&(e.options={...e.options},delete e.options.headers),e.headers&&(e.headers=Object.fromEntries((e.headers instanceof Headers?[...e.headers]:Object.entries(e.headers)).map(([t,n])=>[t,t.toLowerCase()==="authorization"||t.toLowerCase()==="api-key"||t.toLowerCase()==="x-api-key"||t.toLowerCase()==="cookie"||t.toLowerCase()==="set-cookie"?"***":n]))),"retryOfRequestLogID"in e&&(e.retryOfRequestLogID&&(e.retryOf=e.retryOfRequestLogID),delete e.retryOfRequestLogID),e);var Fe,gn=class e{constructor(t,n,a){this.iterator=t,Fe.set(this,void 0),this.controller=n,$(this,Fe,a,"f")}static fromSSEResponse(t,n,a){let i=!1,s=a?X(a):console;async function*o(){if(i)throw new G("Cannot iterate over a consumed stream, use `.tee()` to split the stream.");i=!0;let d=!1;try{for await(let l of jo(t,n))try{yield JSON.parse(l.data)}catch(U){throw s.error("Could not parse message into JSON:",l.data),s.error("From chunk:",l.raw),U}d=!0}catch(l){if(We(l))return;throw l}finally{d||n.abort()}}return new e(o,n,a)}static fromReadableStream(t,n,a){let i=!1;async function*s(){let d=new Rt,l=aa(t);for await(let U of l)for(let u of d.decode(U))yield u;for(let U of d.flush())yield U}async function*o(){if(i)throw new G("Cannot iterate over a consumed stream, use `.tee()` to split the stream.");i=!0;let d=!1;try{for await(let l of s())d||l&&(yield JSON.parse(l));d=!0}catch(l){if(We(l))return;throw l}finally{d||n.abort()}}return new e(o,n,a)}[(Fe=new WeakMap,Symbol.asyncIterator)](){return this.iterator()}tee(){let t=[],n=[],a=this.iterator(),i=s=>({next:()=>{if(s.length===0){let o=a.next();t.push(o),n.push(o)}return s.shift()}});return[new e(()=>i(t),this.controller,Y(this,Fe,"f")),new e(()=>i(n),this.controller,Y(this,Fe,"f"))]}toReadableStream(){let t=this,n;return na({async start(){n=t[Symbol.asyncIterator]()},async pull(a){try{let{value:i,done:s}=await n.next();if(s)return a.close();let o=Be(JSON.stringify(i)+`
4
+ `);a.enqueue(o)}catch(i){a.error(i)}},async cancel(){await n.return?.()}})}};async function*jo(e,t){if(!e.body)throw t.abort(),typeof globalThis.navigator<"u"&&globalThis.navigator.product==="ReactNative"?new G("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 G("Attempted to iterate over a response with no body");let n=new ua,a=new Rt,i=aa(e.body);for await(let s of zo(i))for(let o of a.decode(s)){let d=n.decode(o);d&&(yield d)}for(let s of a.flush()){let o=n.decode(s);o&&(yield o)}}async function*zo(e){let t=new Uint8Array;for await(let n of e){if(n==null)continue;let a=n instanceof ArrayBuffer?new Uint8Array(n):typeof n=="string"?Be(n):n,i=new Uint8Array(t.length+a.length);i.set(t),i.set(a,t.length),t=i;let s;for(;(s=Ri(t))!==-1;)yield t.slice(0,s),t=t.slice(s)}t.length>0&&(yield t)}var ua=class{constructor(){this.event=null,this.data=[],this.chunks=[]}decode(t){if(t.endsWith("\r")&&(t=t.substring(0,t.length-1)),!t){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(t),t.startsWith(":"))return null;let[n,a,i]=$o(t,":");return i.startsWith(" ")&&(i=i.substring(1)),n==="event"?this.event=i:n==="data"&&this.data.push(i),null}};function $o(e,t){let n=e.indexOf(t);return n!==-1?[e.substring(0,n),t,e.substring(n+t.length)]:[e,"",""]}async function pn(e,t){let{response:n,requestLogID:a,retryOfRequestLogID:i,startTime:s}=t,o=await(async()=>{if(t.options.stream)return X(e).debug("response",n.status,n.url,n.headers,n.body),t.options.__streamClass?t.options.__streamClass.fromSSEResponse(n,t.controller,e):gn.fromSSEResponse(n,t.controller,e);if(n.status===204)return null;if(t.options.__binaryResponse)return n;let l=n.headers.get("content-type")?.split(";")[0]?.trim();return l?.includes("application/json")||l?.endsWith("+json")?n.headers.get("content-length")==="0"?void 0:await n.json():await n.text()})();return X(e).debug(`[${a}] response parsed`,mt({retryOfRequestLogID:i,url:n.url,status:n.status,body:o,durationMs:Date.now()-s})),o}var Ge,Wt=class e extends Promise{constructor(t,n,a=pn){super(i=>{i(null)}),this.responsePromise=n,this.parseResponse=a,Ge.set(this,void 0),$(this,Ge,t,"f")}_thenUnwrap(t){return new e(Y(this,Ge,"f"),this.responsePromise,async(n,a)=>t(await this.parseResponse(n,a),a))}asResponse(){return this.responsePromise.then(t=>t.response)}async withResponse(){let[t,n]=await Promise.all([this.parse(),this.asResponse()]);return{data:t,response:n}}parse(){return this.parsedPromise||(this.parsedPromise=this.responsePromise.then(t=>this.parseResponse(Y(this,Ge,"f"),t))),this.parsedPromise}then(t,n){return this.parse().then(t,n)}catch(t){return this.parse().catch(t)}finally(t){return this.parse().finally(t)}};Ge=new WeakMap;var wn,mn=class{constructor(t,n,a,i){wn.set(this,void 0),$(this,wn,t,"f"),this.options=i,this.response=n,this.body=a}hasNextPage(){return this.getPaginatedItems().length?this.nextPageRequestOptions()!=null:!1}async getNextPage(){let t=this.nextPageRequestOptions();if(!t)throw new G("No next page expected; please check `.hasNextPage()` before calling `.getNextPage()`.");return await Y(this,wn,"f").requestAPIList(this.constructor,t)}async*iterPages(){let t=this;for(yield t;t.hasNextPage();)t=await t.getNextPage(),yield t}async*[(wn=new WeakMap,Symbol.asyncIterator)](){for await(let t of this.iterPages())for(let n of t.getPaginatedItems())yield n}},Ve=class extends Wt{constructor(t,n,a){super(t,n,async(i,s)=>new a(i,s.response,await pn(i,s),s.options))}async*[Symbol.asyncIterator](){let t=await this;for await(let n of t)yield n}},yn=class extends mn{constructor(t,n,a,i){super(t,n,a,i),this.items=a||[],this.next_page_token=this.response.headers.get("x-next-page-token")??null,this.has_more=ea(this.response.headers.get("x-has-more"))??null}getPaginatedItems(){return this.items??[]}hasNextPage(){return this.has_more===!1?!1:super.hasNextPage()}nextPageRequestOptions(){let t=this.next_page_token;return t?{...this.options,query:{...Zn(this.options.query),page_token:t}}:null}},B=class extends mn{constructor(t,n,a,i){super(t,n,a,i),this.items=a||[],this.has_more=ea(this.response.headers.get("x-has-more"))??null,this.next_offset=hi(this.response.headers.get("x-next-offset"))??null}getPaginatedItems(){return this.items??[]}hasNextPage(){return this.has_more===!1?!1:super.hasNextPage()}nextPageRequestOptions(){let t=this.next_offset;if(t==null){if(this.has_more)throw new G("Server reported X-Has-More: true without an X-Next-Offset header; refusing to silently truncate pagination");return null}return t===0?null:{...this.options,query:{...Zn(this.options.query),offset:t}}}};var ga=()=>{if(typeof File>"u"){let{process:e}=globalThis,t=typeof e?.versions?.node=="string"&&parseInt(e.versions.node.split("."))<20;throw new Error("`File` is not defined as a global, which is required for file uploads."+(t?" Update to Node 20 LTS or newer, or set `globalThis.File` to `import('node:buffer').File`.":""))}};function ce(e,t,n){return ga(),new File(e,t??"unknown_file",n)}function Ye(e){return(typeof e=="object"&&e!==null&&("name"in e&&e.name&&String(e.name)||"url"in e&&e.url&&String(e.url)||"filename"in e&&e.filename&&String(e.filename)||"path"in e&&e.path&&String(e.path))||"").split(/[\\/]/).pop()||void 0}var Cn=e=>e!=null&&typeof e=="object"&&typeof e[Symbol.asyncIterator]=="function";var yt=async(e,t)=>({...e,body:await tr(e.body,t)}),Hi=new WeakMap;function Zo(e){let t=typeof e=="function"?e:e.fetch,n=Hi.get(t);if(n)return n;let a=(async()=>{try{let i="Response"in t?t.Response:(await t("data:,")).constructor,s=new FormData;return s.toString()!==await new i(s).text()}catch{return!0}})();return Hi.set(t,a),a}var tr=async(e,t)=>{if(!await Zo(t))throw new TypeError("The provided fetch function does not support file uploads with the current global FormData class.");let n=new FormData;return await Promise.all(Object.entries(e||{}).map(([a,i])=>ha(n,a,i))),n},Bi=e=>e instanceof Blob&&"name"in e,er=e=>typeof e=="object"&&e!==null&&(e instanceof Response||Cn(e)||Bi(e)),ca=e=>{if(er(e))return!0;if(Array.isArray(e))return e.some(ca);if(e&&typeof e=="object"){for(let t in e)if(ca(e[t]))return!0}return!1},ha=async(e,t,n)=>{if(n!==void 0){if(n==null)throw new TypeError(`Received null for "${t}"; to pass null in FormData, you must use the string 'null'`);if(typeof n=="string"||typeof n=="number"||typeof n=="boolean")e.append(t,String(n));else if(n instanceof Response)e.append(t,ce([await n.blob()],Ye(n)));else if(Cn(n))e.append(t,ce([await new Response(Un(n)).blob()],Ye(n)));else if(Bi(n))e.append(t,n,Ye(n));else if(Array.isArray(n))await Promise.all(n.map((a,i)=>ha(e,`${t}[${i}]`,a)));else if(typeof n=="object")!(t==="env_vars")&&!ca(n)?Object.entries(n).filter(([s,o])=>o!==void 0).length>0&&e.append(t,JSON.stringify(n)):await Promise.all(Object.entries(n).map(([i,s])=>ha(e,`${t}[${i}]`,s)));else throw new TypeError(`Invalid value given to form, expected a string, number, boolean, object, Array, File or Blob but got ${n} instead`)}};var Di=e=>e!=null&&typeof e=="object"&&typeof e.size=="number"&&typeof e.type=="string"&&typeof e.text=="function"&&typeof e.slice=="function"&&typeof e.arrayBuffer=="function",nr=e=>e!=null&&typeof e=="object"&&typeof e.name=="string"&&typeof e.lastModified=="number"&&Di(e),ar=e=>e!=null&&typeof e=="object"&&typeof e.url=="string"&&typeof e.blob=="function";async function kn(e,t,n){if(ga(),e=await e,nr(e))return e instanceof File?e:ce([await e.arrayBuffer()],e.name);if(ar(e)){let i=await e.blob();return t||(t=new URL(e.url).pathname.split(/[\\/]/).pop()),ce(await pa(i),t,n)}let a=await pa(e);if(t||(t=Ye(e)),!n?.type){let i=a.find(s=>typeof s=="object"&&"type"in s&&s.type);typeof i=="string"&&(n={...n,type:i})}return ce(a,t,n)}async function pa(e){let t=[];if(typeof e=="string"||ArrayBuffer.isView(e)||e instanceof ArrayBuffer)t.push(e);else if(Di(e))t.push(e instanceof Blob?e:await e.arrayBuffer());else if(Cn(e))for await(let n of e)t.push(...await pa(n));else{let n=e?.constructor?.name;throw new Error(`Unexpected data type: ${typeof e}${n?`; constructor: ${n}`:""}${ir(e)}`)}return t}function ir(e){return typeof e!="object"||e===null?"":`; props: [${Object.getOwnPropertyNames(e).map(n=>`"${n}"`).join(", ")}]`}var b=class{constructor(t){this._client=t}};var Fi=Symbol("brand.privateNullableHeaders");function*or(e){if(!e)return;if(Fi in e){let{values:a,nulls:i}=e;yield*a.entries();for(let s of i)yield[s,null];return}let t=!1,n;e instanceof Headers?n=e.entries():Qn(e)?n=e:(t=!0,n=Object.entries(e??{}));for(let a of n){let i=a[0];if(typeof i!="string")throw new TypeError("expected header name to be a string");let s=Qn(a[1])?a[1]:[a[1]],o=!1;for(let d of s)d!==void 0&&(t&&!o&&(o=!0,yield[i,null]),yield[i,d])}}var T=e=>{let t=new Headers,n=new Set;for(let a of e){let i=new Set;for(let[s,o]of or(a)){let d=s.toLowerCase();i.has(d)||(t.delete(s),i.add(d)),o===null?(t.delete(s),n.add(d)):(t.append(s,o),n.delete(d))}}return{[Fi]:!0,values:t,nulls:n}};function Vi(e){return e.replace(/[^A-Za-z0-9\-._~!$&'()*+,;=:@]+/g,encodeURIComponent)}var Gi=Object.freeze(Object.create(null)),rr=(e=Vi)=>function(n,...a){if(n.length===1)return n[0];let i=!1,s=[],o=n.reduce((u,r,S)=>{/[?#]/.test(r)&&(i=!0);let c=a[S],h=(i?encodeURIComponent:e)(""+c);return S!==a.length&&(c==null||typeof c=="object"&&c.toString===Object.getPrototypeOf(Object.getPrototypeOf(c.hasOwnProperty??Gi)??Gi)?.toString)&&(h=c+"",s.push({start:u.length+r.length,length:h.length,error:`Value of type ${Object.prototype.toString.call(c).slice(8,-1)} is not a valid path parameter`})),u+r+(S===a.length?"":h)},""),d=o.split(/[?#]/,1)[0],l=/(?<=^|\/)(?:\.|%2e){1,2}(?=\/|$)/gi,U;for(;(U=l.exec(d))!==null;)s.push({start:U.index,length:U[0].length,error:`Value "${U[0]}" can't be safely passed as a path parameter`});if(s.sort((u,r)=>u.start-r.start),s.length>0){let u=0,r=s.reduce((S,c)=>{let h=" ".repeat(c.start-u),m="^".repeat(c.length);return u=c.start+c.length,S+h+m},"");throw new G(`Path parameters result in path with invalid segments:
6
6
  ${s.map(S=>S.error).join(`
7
7
  `)}
8
8
  ${o}
9
- ${r}`)}return o},g=or(Vi);var Ht=class extends b{create(t,n){return this._client.post("/org/api_keys",{body:t,...n})}retrieve(t,n={},a){return this._client.get(g`/org/api_keys/${t}`,{query:n,...a})}update(t,n,a){return this._client.patch(g`/org/api_keys/${t}`,{body:n,...a})}list(t={},n){return this._client.getAPIList("/org/api_keys",B,{query:t,...n})}delete(t,n){return this._client.delete(g`/org/api_keys/${t}`,{...n,headers:T([{Accept:"*/*"},n?.headers])})}rotate(t,n={},a){return this._client.post(g`/org/api_keys/${t}/rotate`,{body:n,...a})}};var Bt=class extends b{list(t={},n){return this._client.getAPIList("/apps",B,{query:t,...n})}};var he=class extends b{create(t,n){return this._client.post("/audit-logs/export/destinations",{body:t,...n})}retrieve(t,n){return this._client.get(g`/audit-logs/export/destinations/${t}`,n)}update(t,n,a){return this._client.patch(g`/audit-logs/export/destinations/${t}`,{body:n,...a})}list(t={},n){return this._client.getAPIList("/audit-logs/export/destinations",B,{query:t,...n})}delete(t,n){return this._client.delete(g`/audit-logs/export/destinations/${t}`,{...n,headers:T([{Accept:"*/*"},n?.headers])})}test(t,n){return this._client.post(g`/audit-logs/export/destinations/${t}/test`,n)}};var dr=6,lr=5e4,Sr=8e3,Ut=class extends G{};async function Ki(e,t,n,a,i={}){if(!n||typeof n.write!="function")throw new TypeError("audit log download destination must provide write()");let{onProgress:s,maxTransferRetries:o=dr,...d}=i;if(!Number.isInteger(o)||o<0)throw new TypeError("maxTransferRetries must be a non-negative integer");let l=d.timeout??a,U,u={bytesWritten:0,chunks:0,rows:0},r=new Set;for(;;){let S=await Ur(e,U?{...t,cursor:U}:t,d,o,l),{nextCursor:c,hasMore:h,rows:m}=ur(S.headers,U);if(h&&c){if(r.has(c))throw new Ut("response repeated X-Next-Cursor header");r.add(c)}if(await gr(n,S.body),U=c,u.bytesWritten+=S.body.byteLength,u.chunks+=1,u.rows+=m,s&&await s({...u,chunkRows:m}),!h)return u}}async function Ur(e,t,n,a,i){for(let s=0;;s+=1){let o=new AbortController,d=()=>o.abort();n.signal?.aborted?o.abort():n.signal?.addEventListener("abort",d,{once:!0});let l;try{l=await e(t,{...n,signal:o.signal})}catch(r){throw n.signal?.removeEventListener("abort",d),r}let U=!1,u=setTimeout(()=>{U=!0,o.abort()},i);try{let r=new Uint8Array(await l.arrayBuffer());if(clearTimeout(u),n.signal?.aborted)throw new at;if(U)throw new wt;let S=l.headers.get("x-content-sha256");if(!S)throw new Ut("response missing X-Content-Sha256 header");let c=await cr(r);if(n.signal?.aborted)throw new at;if(c!==S)throw new Ut(`audit log chunk checksum mismatch (got ${c}, want ${S})`);return{body:r,headers:l.headers}}catch(r){if(n.signal?.aborted&&!(r instanceof at)?r=new at:U&&(r=new wt),s===a||r instanceof at)throw r;await hr(s+1,n.signal)}finally{clearTimeout(u),n.signal?.removeEventListener("abort",d)}}}function ur(e,t){let n=e.get("x-has-more");if(n!=="true"&&n!=="false")throw new Ut("response missing or invalid X-Has-More header");let a=n==="true",i=e.get("x-row-count");if(i===null||!/^[0-9]+$/.test(i))throw new Ut("response missing or invalid X-Row-Count header");let s=Number(i);if(!Number.isSafeInteger(s)||s>lr)throw new Ut("response missing or invalid X-Row-Count header");let o=e.get("x-next-cursor")||void 0;if(a&&(!o||o===t))throw new Ut("response has invalid X-Next-Cursor header");if(!a&&o)throw new Ut("response returned a cursor after the final chunk");return{rows:s,nextCursor:o,hasMore:a}}async function cr(e){let t=await globalThis.crypto.subtle.digest("SHA-256",e);return Array.from(new Uint8Array(t),n=>n.toString(16).padStart(2,"0")).join("")}async function hr(e,t){let n=Math.min(1e3*2**(e-1),Sr);await new Promise((a,i)=>{if(t?.aborted){i(new at);return}let s=()=>{clearTimeout(o),i(new at)},o=setTimeout(()=>{t?.removeEventListener("abort",s),a()},n);t?.addEventListener("abort",s,{once:!0})})}async function gr(e,t){let n=0;for(;n<t.byteLength;){let a=await e.write(n===0?t:t.subarray(n));if(typeof a=="number"){n+=Yi(a,t.byteLength-n);continue}if(a&&typeof a=="object"&&"bytesWritten"in a){let i=a.bytesWritten;n+=Yi(i,t.byteLength-n);continue}return}}function Yi(e,t){if(!Number.isSafeInteger(e)||e<=0||e>t)throw new Ut("audit log download destination performed a short write");return e}var bt=class extends b{constructor(){super(...arguments),this.exportDestinations=new he(this._client)}list(t,n){return this._client.getAPIList("/audit-logs",yn,{query:t,...n})}exportChunk(t,n){return this._client.get("/audit-logs/export/chunk",{query:t,...n,headers:T([{Accept:"application/octet-stream"},n?.headers]),__binaryResponse:!0})}download(t,n,a){return Ki((i,s)=>this.exportChunk(i,s),t,n,this._client.timeout,a)}};bt.ExportDestinations=he;var ge=class extends b{create(t,n){return this._client.post("/auth/connections",{body:t,...n})}retrieve(t,n){return this._client.get(g`/auth/connections/${t}`,n)}update(t,n,a){return this._client.patch(g`/auth/connections/${t}`,{body:n,...a})}list(t={},n){return this._client.getAPIList("/auth/connections",B,{query:t,...n})}delete(t,n){return this._client.delete(g`/auth/connections/${t}`,{...n,headers:T([{Accept:"*/*"},n?.headers])})}follow(t,n){return this._client.get(g`/auth/connections/${t}/events`,{...n,headers:T([{Accept:"text/event-stream"},n?.headers]),stream:!0})}login(t,n={},a){return this._client.post(g`/auth/connections/${t}/login`,{body:n,...a})}submit(t,n,a){return this._client.post(g`/auth/connections/${t}/submit`,{body:n,...a})}timeline(t,n={},a){return this._client.getAPIList(g`/auth/connections/${t}/timeline`,B,{query:n,...a})}};var pe=class extends b{retrieve(t){return this._client.get("/auth/context",t)}};var Ct=class extends b{constructor(){super(...arguments),this.context=new pe(this._client),this.connections=new ge(this._client)}};Ct.Context=pe;Ct.Connections=ge;var Dt=class extends b{create(t,n){return this._client.post("/browser_pools",{body:t,...n})}retrieve(t,n){return this._client.get(g`/browser_pools/${t}`,n)}update(t,n,a){return this._client.patch(g`/browser_pools/${t}`,{body:n,...a})}list(t={},n){return this._client.getAPIList("/browser_pools",B,{query:t,...n})}delete(t,n={},a){return this._client.delete(g`/browser_pools/${t}`,{body:n,...a,headers:T([{Accept:"*/*"},a?.headers])})}acquire(t,n,a){return this._client.post(g`/browser_pools/${t}/acquire`,{body:n,...a})}flush(t,n){return this._client.post(g`/browser_pools/${t}/flush`,{...n,headers:T([{Accept:"*/*"},n?.headers])})}release(t,n,a){return this._client.post(g`/browser_pools/${t}/release`,{body:n,...a,headers:T([{Accept:"*/*"},a?.headers])})}};var we=class extends b{batch(t,n,a){return this._client.post(g`/browsers/${t}/computer/batch`,{body:n,...a,headers:T([{Accept:"*/*"},a?.headers])})}captureScreenshot(t,n={},a){return this._client.post(g`/browsers/${t}/computer/screenshot`,{body:n,...a,headers:T([{Accept:"image/png"},a?.headers]),__binaryResponse:!0})}clickMouse(t,n,a){return this._client.post(g`/browsers/${t}/computer/click_mouse`,{body:n,...a,headers:T([{Accept:"*/*"},a?.headers])})}dragMouse(t,n,a){return this._client.post(g`/browsers/${t}/computer/drag_mouse`,{body:n,...a,headers:T([{Accept:"*/*"},a?.headers])})}getMousePosition(t,n){return this._client.post(g`/browsers/${t}/computer/get_mouse_position`,n)}moveMouse(t,n,a){return this._client.post(g`/browsers/${t}/computer/move_mouse`,{body:n,...a,headers:T([{Accept:"*/*"},a?.headers])})}pressKey(t,n,a){return this._client.post(g`/browsers/${t}/computer/press_key`,{body:n,...a,headers:T([{Accept:"*/*"},a?.headers])})}readClipboard(t,n){return this._client.post(g`/browsers/${t}/computer/clipboard/read`,n)}scroll(t,n,a){return this._client.post(g`/browsers/${t}/computer/scroll`,{body:n,...a,headers:T([{Accept:"*/*"},a?.headers])})}setCursorVisibility(t,n,a){return this._client.post(g`/browsers/${t}/computer/cursor`,{body:n,...a})}typeText(t,n,a){return this._client.post(g`/browsers/${t}/computer/type`,{body:n,...a,headers:T([{Accept:"*/*"},a?.headers])})}writeClipboard(t,n,a){return this._client.post(g`/browsers/${t}/computer/clipboard/write`,{body:n,...a,headers:T([{Accept:"*/*"},a?.headers])})}};var me=class extends b{stream(t,n,a){return this._client.get(g`/browsers/${t}/logs/stream`,{query:n,...a,headers:T([{Accept:"text/event-stream"},a?.headers]),stream:!0})}};var ye=class extends b{execute(t,n,a){return this._client.post(g`/browsers/${t}/playwright/execute`,{body:n,...a})}};var Ce=class extends b{exec(t,n,a){return this._client.post(g`/browsers/${t}/process/exec`,{body:n,...a})}kill(t,n,a){let{id:i,...s}=n;return this._client.post(g`/browsers/${i}/process/${t}/kill`,{body:s,...a})}resize(t,n,a){let{id:i,...s}=n;return this._client.post(g`/browsers/${i}/process/${t}/resize`,{body:s,...a})}spawn(t,n,a){return this._client.post(g`/browsers/${t}/process/spawn`,{body:n,...a})}status(t,n,a){let{id:i}=n;return this._client.get(g`/browsers/${i}/process/${t}/status`,a)}stdin(t,n,a){let{id:i,...s}=n;return this._client.post(g`/browsers/${i}/process/${t}/stdin`,{body:s,...a})}stdoutStream(t,n,a){let{id:i}=n;return this._client.get(g`/browsers/${i}/process/${t}/stdout/stream`,{...a,headers:T([{Accept:"text/event-stream"},a?.headers]),stream:!0})}};var ke=class extends b{list(t,n){return this._client.get(g`/browsers/${t}/replays`,n)}download(t,n,a){let{id:i}=n;return this._client.get(g`/browsers/${i}/replays/${t}`,{...a,headers:T([{Accept:"video/mp4"},a?.headers]),__binaryResponse:!0})}start(t,n={},a){return this._client.post(g`/browsers/${t}/replays`,{body:n,...a})}stop(t,n,a){let{id:i}=n;return this._client.post(g`/browsers/${i}/replays/${t}/stop`,{...a,headers:T([{Accept:"*/*"},a?.headers])})}};var Te=class extends b{events(t,n={},a){return this._client.getAPIList(g`/browsers/${t}/telemetry/events`,B,{query:n,...a})}stream(t,n={},a){let{"Last-Event-ID":i,...s}=n??{};return this._client.get(g`/browsers/${t}/telemetry/stream`,{query:s,...a,headers:T([{Accept:"text/event-stream",...i!=null?{"Last-Event-ID":i}:void 0},a?.headers]),stream:!0})}};var Me=class extends b{events(t,n,a){let{id:i}=n;return this._client.get(g`/browsers/${i}/fs/watch/${t}/events`,{...a,headers:T([{Accept:"text/event-stream"},a?.headers]),stream:!0})}start(t,n,a){return this._client.post(g`/browsers/${t}/fs/watch`,{body:n,...a})}stop(t,n,a){let{id:i}=n;return this._client.delete(g`/browsers/${i}/fs/watch/${t}`,{...a,headers:T([{Accept:"*/*"},a?.headers])})}};var Ft=class extends b{constructor(){super(...arguments),this.watch=new Me(this._client)}createDirectory(t,n,a){return this._client.put(g`/browsers/${t}/fs/create_directory`,{body:n,...a,headers:T([{Accept:"*/*"},a?.headers])})}deleteDirectory(t,n,a){return this._client.put(g`/browsers/${t}/fs/delete_directory`,{body:n,...a,headers:T([{Accept:"*/*"},a?.headers])})}deleteFile(t,n,a){return this._client.put(g`/browsers/${t}/fs/delete_file`,{body:n,...a,headers:T([{Accept:"*/*"},a?.headers])})}downloadDirZip(t,n,a){return this._client.get(g`/browsers/${t}/fs/download_dir_zip`,{query:n,...a,headers:T([{Accept:"application/zip"},a?.headers]),__binaryResponse:!0})}fileInfo(t,n,a){return this._client.get(g`/browsers/${t}/fs/file_info`,{query:n,...a})}listFiles(t,n,a){return this._client.get(g`/browsers/${t}/fs/list_files`,{query:n,...a})}move(t,n,a){return this._client.put(g`/browsers/${t}/fs/move`,{body:n,...a,headers:T([{Accept:"*/*"},a?.headers])})}readFile(t,n,a){return this._client.get(g`/browsers/${t}/fs/read_file`,{query:n,...a,headers:T([{Accept:"application/octet-stream"},a?.headers]),__binaryResponse:!0})}setFilePermissions(t,n,a){return this._client.put(g`/browsers/${t}/fs/set_file_permissions`,{body:n,...a,headers:T([{Accept:"*/*"},a?.headers])})}upload(t,n,a){return this._client.post(g`/browsers/${t}/fs/upload`,yt({body:n,...a,headers:T([{Accept:"*/*"},a?.headers])},this._client))}uploadZip(t,n,a){return this._client.post(g`/browsers/${t}/fs/upload_zip`,yt({body:n,...a,headers:T([{Accept:"*/*"},a?.headers])},this._client))}writeFile(t,n,a,i){let{path:s,mode:o}=a;return this._client.put(g`/browsers/${t}/fs/write_file`,{body:n,query:{path:s,mode:o},...i,headers:T([{"Content-Type":"application/octet-stream",Accept:"*/*"},i?.headers])})}};Ft.Watch=Me;function Tn(e,t){return`${e.replace(/\/+$/,"")}${t.startsWith("/")?t:`/${t}`}`}async function Ji(e,t,n,a){let i=e.browserRouteCache.get(t);if(!i)throw new G(`browser route cache does not contain session ${t}; create, retrieve, or list the browser before calling browser.fetch`);let{url:s,method:o,headers:d,body:l,signal:U,duplex:u,timeout_ms:r}=br(n,a);_r(s);let S={url:s,jwt:i.jwt};r!==void 0&&(S.timeout_ms=r);let c=d.get("accept"),h={method:vr(o),path:Tn(i.baseURL,"/curl/raw"),query:S,body:l,headers:T([{Authorization:null},c?{Accept:c}:{Accept:"*/*"},Er(d)]),signal:U??null,__binaryResponse:!0};return u&&(h.fetchOptions={duplex:u}),e.request(h).asResponse()}function vr(e){let t=e.toLowerCase();if(!new Set(["get","post","put","patch","delete"]).has(t))throw new G(`browser.fetch unsupported HTTP method: ${e}`);return t}function br(e,t){let n=t&&"timeout_ms"in t?t.timeout_ms:void 0;if(e instanceof Request){let i=new Headers(e.headers);t?.headers&&new Headers(t.headers).forEach((U,u)=>{i.set(u,U)});let s={url:e.url,method:(t?.method??e.method)?.toUpperCase()||"GET",headers:i},o=t?.body??e.body;o!=null&&(s.body=o);let d=t?.signal??e.signal;return d!==void 0&&(s.signal=d),t?.duplex!==void 0&&(s.duplex=t.duplex),n!==void 0&&(s.timeout_ms=n),s}let a={url:e instanceof URL?e.href:String(e),method:(t?.method??"GET").toUpperCase(),headers:new Headers(t?.headers)};return t?.body!==void 0&&(a.body=t.body),t?.signal!==void 0&&(a.signal=t.signal),t?.duplex!==void 0&&(a.duplex=t.duplex),n!==void 0&&(a.timeout_ms=n),a}function _r(e){let t;try{t=new URL(e)}catch{throw new G(`browser.fetch target must be an absolute URL; received: ${e}`)}if(t.protocol!=="http:"&&t.protocol!=="https:")throw new G(`browser.fetch only supports http(s) URLs; received: ${t.protocol}`)}function Er(e){let t={};return e.forEach((n,a)=>{switch(a.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:t[a]=n}}),t}var rt=class extends b{constructor(){super(...arguments),this.telemetry=new Te(this._client),this.replays=new ke(this._client),this.fs=new Ft(this._client),this.process=new Ce(this._client),this.logs=new me(this._client),this.computer=new we(this._client),this.playwright=new ye(this._client)}create(t={},n){return this._client.post("/browsers",{body:t,...n})}retrieve(t,n={},a){return this._client.get(g`/browsers/${t}`,{query:n,...a})}update(t,n,a){return this._client.patch(g`/browsers/${t}`,{body:n,...a})}list(t={},n){return this._client.getAPIList("/browsers",B,{query:t,...n})}curl(t,n,a){return this._client.post(g`/browsers/${t}/curl`,{body:n,...a})}fetch(t,n,a){return Ji(this._client,t,n,a)}deleteByID(t,n){return this._client.delete(g`/browsers/${t}`,{...n,headers:T([{Accept:"*/*"},n?.headers])})}loadExtensions(t,n,a){return this._client.post(g`/browsers/${t}/extensions`,yt({body:n,...a,headers:T([{Accept:"*/*"},a?.headers])},this._client))}};rt.Telemetry=Te;rt.Replays=ke;rt.Fs=Ft;rt.Process=Ce;rt.Logs=me;rt.Computer=we;rt.Playwright=ye;var Gt=class extends b{create(t,n){return this._client.post("/org/credential_providers",{body:t,...n})}retrieve(t,n){return this._client.get(g`/org/credential_providers/${t}`,n)}update(t,n,a){return this._client.patch(g`/org/credential_providers/${t}`,{body:n,...a})}list(t={},n){return this._client.getAPIList("/org/credential_providers",B,{query:t,...n})}delete(t,n){return this._client.delete(g`/org/credential_providers/${t}`,{...n,headers:T([{Accept:"*/*"},n?.headers])})}listItems(t,n){return this._client.get(g`/org/credential_providers/${t}/items`,n)}test(t,n){return this._client.post(g`/org/credential_providers/${t}/test`,n)}};var Vt=class extends b{create(t,n){return this._client.post("/credentials",{body:t,...n})}retrieve(t,n){return this._client.get(g`/credentials/${t}`,n)}update(t,n,a){return this._client.patch(g`/credentials/${t}`,{body:n,...a})}list(t={},n){return this._client.getAPIList("/credentials",B,{query:t,...n})}delete(t,n){return this._client.delete(g`/credentials/${t}`,{...n,headers:T([{Accept:"*/*"},n?.headers])})}totpCode(t,n){return this._client.get(g`/credentials/${t}/totp-code`,n)}};var Yt=class extends b{create(t,n){return this._client.post("/deployments",yt({body:t,...n},this._client))}retrieve(t,n){return this._client.get(g`/deployments/${t}`,n)}list(t={},n){return this._client.getAPIList("/deployments",B,{query:t,...n})}delete(t,n){return this._client.delete(g`/deployments/${t}`,{...n,headers:T([{Accept:"*/*"},n?.headers])})}follow(t,n={},a){return this._client.get(g`/deployments/${t}/events`,{query:n,...a,headers:T([{Accept:"text/event-stream"},a?.headers]),stream:!0})}};var Kt=class extends b{list(t={},n){return this._client.getAPIList("/extensions",B,{query:t,...n})}delete(t,n){return this._client.delete(g`/extensions/${t}`,{...n,headers:T([{Accept:"*/*"},n?.headers])})}download(t,n){return this._client.get(g`/extensions/${t}`,{...n,headers:T([{Accept:"application/octet-stream"},n?.headers]),__binaryResponse:!0})}downloadFromChromeStore(t,n){return this._client.get("/extensions/from_chrome_store",{query:t,...n,headers:T([{Accept:"application/octet-stream"},n?.headers]),__binaryResponse:!0})}get(t,n){return this._client.get(g`/extensions/${t}/metadata`,n)}upload(t,n){return this._client.post("/extensions",yt({body:t,...n},this._client))}};var Jt=class extends b{create(t,n){return this._client.post("/invocations",{body:t,...n})}retrieve(t,n){return this._client.get(g`/invocations/${t}`,n)}update(t,n,a){return this._client.patch(g`/invocations/${t}`,{body:n,...a})}list(t={},n){return this._client.getAPIList("/invocations",B,{query:t,...n})}deleteBrowsers(t,n){return this._client.delete(g`/invocations/${t}/browsers`,{...n,headers:T([{Accept:"*/*"},n?.headers])})}follow(t,n={},a){return this._client.get(g`/invocations/${t}/events`,{query:n,...a,headers:T([{Accept:"text/event-stream"},a?.headers]),stream:!0})}listBrowsers(t,n){return this._client.get(g`/invocations/${t}/browsers`,n)}};var fe=class extends b{retrieve(t){return this._client.get("/org/entitlements",t)}};var Ne=class extends b{retrieve(t){return this._client.get("/org/limits",t)}update(t,n){return this._client.patch("/org/limits",{body:t,...n})}};var kt=class extends b{constructor(){super(...arguments),this.entitlements=new fe(this._client),this.limits=new Ne(this._client)}};kt.Entitlements=fe;kt.Limits=Ne;var qt=class extends b{create(t,n){return this._client.post("/profiles",{body:t,...n})}retrieve(t,n){return this._client.get(g`/profiles/${t}`,n)}update(t,n,a){return this._client.patch(g`/profiles/${t}`,{body:n,...a})}list(t={},n){return this._client.getAPIList("/profiles",B,{query:t,...n})}delete(t,n){return this._client.delete(g`/profiles/${t}`,{...n,headers:T([{Accept:"*/*"},n?.headers])})}download(t,n={},a){return this._client.get(g`/profiles/${t}/download`,{query:n,...a,headers:T([{Accept:"application/octet-stream"},a?.headers]),__binaryResponse:!0})}};var ve=class extends b{retrieve(t,n){return this._client.get(g`/org/projects/${t}/limits`,n)}update(t,n,a){return this._client.patch(g`/org/projects/${t}/limits`,{body:n,...a})}};var _t=class extends b{constructor(){super(...arguments),this.limits=new ve(this._client)}create(t,n){return this._client.post("/org/projects",{body:t,...n})}retrieve(t,n){return this._client.get(g`/org/projects/${t}`,n)}update(t,n,a){return this._client.patch(g`/org/projects/${t}`,{body:n,...a})}list(t={},n){return this._client.getAPIList("/org/projects",B,{query:t,...n})}delete(t,n){return this._client.delete(g`/org/projects/${t}`,{...n,headers:T([{Accept:"*/*"},n?.headers])})}};_t.Limits=ve;var Xt=class extends b{create(t,n){return this._client.post("/proxies",{body:t,...n})}retrieve(t,n){return this._client.get(g`/proxies/${t}`,n)}update(t,n,a){return this._client.patch(g`/proxies/${t}`,{body:n,...a})}list(t={},n){return this._client.getAPIList("/proxies",B,{query:t,...n})}delete(t,n){return this._client.delete(g`/proxies/${t}`,{...n,headers:T([{Accept:"*/*"},n?.headers])})}check(t,n={},a){return this._client.post(g`/proxies/${t}/check`,{body:n,...a})}};var be=class extends b{create(t,n){return this._client.post("/telemetry/destinations",{body:t,...n})}retrieve(t,n){return this._client.get(g`/telemetry/destinations/${t}`,n)}update(t,n,a){return this._client.patch(g`/telemetry/destinations/${t}`,{body:n,...a})}list(t={},n){return this._client.getAPIList("/telemetry/destinations",B,{query:t,...n})}delete(t,n){return this._client.delete(g`/telemetry/destinations/${t}`,{...n,headers:T([{Accept:"*/*"},n?.headers])})}};var Et=class extends b{constructor(){super(...arguments),this.destinations=new be(this._client)}};Et.Destinations=be;var Ke=class{constructor(){this.entries=new Map}get(t){return this.entries.get(t)}set(t){this.entries.set(t.sessionId,t)}delete(t){this.entries.delete(t)}clear(){this.entries.clear()}},qi="\u004b\u0045\u0052\u004e\u0045\u004c_BROWSER_ROUTING_SUBRESOURCES",Or=["curl","telemetry/stream"],Rr=/^\/(?:v\d+\/)?browsers(?:\/[^/]+)?\/?$/,Wr=/^\/(?:v\d+\/)?browser_pools\/[^/]+\/acquire\/?$/,Hr=/^\/(?:v\d+\/)?browsers\/([^/]+)\/?$/,Br=/^\/(?:v\d+\/)?browser_pools\/[^/]+\/release\/?$/;function wa(){let e=Qr();return e===void 0?[...Or]:e.trim()===""?[]:e.split(",").map(t=>t.trim()).filter(Boolean)}function ma(e,{apiBaseURL:t,subresources:n,cache:a}){let i=[...n].map(o=>o.trim().replace(/^\/+|\/+$/g,"")).filter(Boolean),s=new URL(t).origin;return async(o,d)=>{let l=new Request(o,d),U=Dr(l,s),u=await qr(e,{input:o,init:d,request:l},s,i,a);return U&&await Kr(u,a),await Fr(l,u,s,a),u}}function Dr(e,t){let n=new URL(e.url);return n.origin===t&&(Rr.test(n.pathname)||Wr.test(n.pathname))}async function Fr(e,t,n,a){if(!t.ok)return;let i=new URL(e.url);if(i.origin!==n)return;let s=Gr(e,i.pathname)??await Vr(e,i.pathname);s&&a.delete(s)}function Gr(e,t){if(e.method.toUpperCase()!=="DELETE")return;let n=t.match(Hr);return n&&decodeURIComponent(n[1]??"")||void 0}async function Vr(e,t){if(!(e.method.toUpperCase()!=="POST"||!Br.test(t)))try{let n=await e.clone().json();if(!n||typeof n!="object")return;let a=n.session_id;return typeof a=="string"&&a.trim()?a.trim():void 0}catch{return}}function Yr(e){if(!e||typeof e!="object")return;let t=e,n=typeof t.session_id=="string"?t.session_id.trim():"",a=typeof t.base_url=="string"?t.base_url.trim():"";if(!n||!a)return;let i=typeof t.jwt=="string"?t.jwt.trim():"",s=typeof t.cdp_ws_url=="string"?t.cdp_ws_url:void 0,o=i||$r(s)||"";if(o)return{sessionId:n,baseURL:a,jwt:o}}async function Kr(e,t){if((e.headers.get("content-type")?.toLowerCase()??"").includes("application/json"))try{pa(await e.clone().json(),t)}catch{}}function pa(e,t){let n=Yr(e);if(n&&t.set(n),Array.isArray(e)){for(let a of e)pa(a,t);return}if(!(!e||typeof e!="object"))for(let a of Object.values(e))typeof a=="object"&&a!==null&&pa(a,t)}function Jr(e,t){let n=e.replace(/^\/+|\/+$/g,"");return t.some(a=>n===a||n.startsWith(a+"/"))}async function qr(e,{input:t,init:n,request:a},i,s,o){let d=new URL(a.url);if(d.origin!==i)return e(t,n);let l=d.pathname.match(/^\/(?:v\d+\/)?browsers\/([^/]+)\/([^/]+)(\/.*)?$/);if(!l)return e(t,n);let U=decodeURIComponent(l[1]??""),u=l[2]??"";if(!U||!Jr(u+(l[3]??""),s))return e(t,n);let r=o.get(U);if(r===void 0)return e(t,n);let S=new URL(Tn(r.baseURL,`/${u}${l[3]??""}`));d.searchParams.forEach((h,m)=>{m!=="jwt"&&S.searchParams.append(m,h)}),S.searchParams.get("jwt")||S.searchParams.set("jwt",r.jwt);let c=new Headers(a.headers);return c.delete("authorization"),e(S.toString(),Xr(t,a,n,c))}function Xr(e,t,n,a){let i=t.method.toUpperCase(),s={...n??{},method:i,headers:a,redirect:t.redirect,signal:n?.signal??(e instanceof Request?e.signal:void 0)};if(delete s.body,delete s.duplex,i!=="GET"&&i!=="HEAD"){let o=jr(t,n);o!==void 0&&(s.body=o),n?.duplex!==void 0?s.duplex=n.duplex:zr(o)&&(s.duplex="half")}return s}function jr(e,t){return t?.body!==void 0&&t.body!==null?t.body:e.body??void 0}function zr(e){return globalThis.ReadableStream&&e instanceof globalThis.ReadableStream||typeof e=="object"&&e!==null&&Symbol.asyncIterator in e}function $r(e){if(e)try{return new URL(e).searchParams.get("jwt")??void 0}catch{return}}function Qr(){if(typeof globalThis.process<"u"){let e=globalThis.process.env?.[qi];return typeof e=="string"?e:void 0}if(typeof globalThis.Deno<"u"){let e=globalThis.Deno.env?.get?.(qi);return typeof e=="string"?e:void 0}}var Mn=class{constructor(t){this.actions=new Map,this.name=t,Xi.registerApp(this)}action(t,n){let a;if(n.length===0)throw new Error("Action handlers must accept at least the context parameter");if(n.length===1){let i=n;a=async(s,o)=>i(s)}else a=n;return this.actions.set(t,{name:t,handler:a}),a}getActions(){return Array.from(this.actions.values())}getAction(t){return this.actions.get(t)}toJSON(){return{name:this.name,actions:this.getActions().map(t=>({name:t.name}))}}},ya=class{constructor(){this.apps=new Map}registerApp(t){this.apps.set(t.name,t)}getApps(){return Array.from(this.apps.values())}getAppByName(t){return this.apps.get(t)}export(){return{apps:this.getApps().map(t=>t.toJSON())}}exportJSON(){return JSON.stringify(this.export(),null,2)}},Xi=new ya;var _e=e=>{if(typeof globalThis.process<"u")return globalThis.process.env?.[e]?.trim()||void 0;if(typeof globalThis.Deno<"u")return globalThis.Deno.env?.get?.(e)?.trim()||void 0};var Ca,ka,Je,ji,zi={production:"https://api.on\u006b\u0065\u0072\u006e\u0065\u006c.com/",development:"https://localhost:3001/"},H=class{constructor({baseURL:t=_e("\u004b\u0045\u0052\u004e\u0045\u004c_BASE_URL"),apiKey:n=_e("\u004b\u0045\u0052\u004e\u0045\u004c_API_KEY"),projectID:a=null,project:i=null,...s}={}){if(Ca.add(this),Je.set(this,void 0),this.deployments=new Yt(this),this.apps=new Bt(this),this.invocations=new Jt(this),this.browsers=new rt(this),this.profiles=new qt(this),this.auth=new Ct(this),this.telemetry=new Et(this),this.proxies=new Xt(this),this.extensions=new Kt(this),this.browserPools=new Dt(this),this.credentials=new Vt(this),this.projects=new _t(this),this.organization=new kt(this),this.auditLogs=new bt(this),this.apiKeys=new Ht(this),this.credentialProviders=new Gt(this),typeof globalThis<"u"&&typeof globalThis.Bun<"u"&&globalThis.Bun.version&&!_e("\u004b\u0045\u0052\u004e\u0045\u004c_SUPPRESS_BUN_WARNING")&&X(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"),n===void 0)throw new G("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 o={apiKey:n,projectID:a,project:i,...s,baseURL:t,environment:s.environment??"production"};if(t&&s.environment)throw new G("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=o.baseURL||zi[o.environment||"production"],this.timeout=o.timeout??ka.DEFAULT_TIMEOUT,this.logger=o.logger??console;let d="warn";this.logLevel=d,this.logLevel=Sa(o.logLevel,"ClientOptions.logLevel",this)??Sa(_e("\u004b\u0045\u0052\u004e\u0045\u004c_LOG"),"process.env['\u004b\u0045\u0052\u004e\u0045\u004c_LOG']",this)??d,this.fetchOptions=o.fetchOptions,this.maxRetries=o.maxRetries??2,this.rawFetch=o.fetch??ki(),this.browserRouteCache=new Ke,this.fetch=ma(this.rawFetch,{apiBaseURL:this.baseURL,subresources:wa(),cache:this.browserRouteCache}),$(this,Je,Mi,"f");let l=_e("\u004b\u0045\u0052\u004e\u0045\u004c_CUSTOM_HEADERS");if(l){let U={};for(let u of l.split(`
10
- `)){let r=u.indexOf(":");r>=0&&(U[u.substring(0,r).trim()]=u.substring(r+1).trim())}o.defaultHeaders={...U,...o.defaultHeaders}}this._options=o,this.apiKey=n,this.projectID=a,this.project=i}withOptions(t){let n=new this.constructor({...this._options,environment:t.environment?t.environment:void 0,baseURL:t.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,...t});return n.browserRouteCache=this.browserRouteCache,n.fetch=ma(n.rawFetch,{apiBaseURL:n.baseURL,subresources:wa(),cache:n.browserRouteCache}),n}defaultQuery(){return this._options.defaultQuery}validateHeaders({values:t,nulls:n}){}async authHeaders(t){return T([{Authorization:`Bearer ${this.apiKey}`}])}stringifyQuery(t){return Ai(t)}getUserAgent(){return`${this.constructor.name}/JS ${vt}`}defaultIdempotencyKey(){return`stainless-node-retry-${zn()}`}makeStatusError(t,n,a,i){return tt.generate(t,n,a,i)}buildURL(t,n,a){let i=!Y(this,Ca,"m",ji).call(this)&&a||this.baseURL,s=Ui(t)?new URL(t):new URL(i+(i.endsWith("/")&&t.startsWith("/")?t.slice(1):t)),o=this.defaultQuery(),d=Object.fromEntries(s.searchParams);return(!Zn(o)||!Zn(d))&&(n={...d,...o,...n}),typeof n=="object"&&n&&!Array.isArray(n)&&(s.search=this.stringifyQuery(n)),s.toString()}async prepareOptions(t){}async prepareRequest(t,{url:n,options:a}){}get(t,n){return this.methodRequest("get",t,n)}post(t,n){return this.methodRequest("post",t,n)}patch(t,n){return this.methodRequest("patch",t,n)}put(t,n){return this.methodRequest("put",t,n)}delete(t,n){return this.methodRequest("delete",t,n)}methodRequest(t,n,a){return this.request(Promise.resolve(a).then(i=>({method:t,path:n,...i})))}request(t,n=null){return new Wt(this,this.makeRequest(t,n,void 0))}async makeRequest(t,n,a){let i=await t,s=i.maxRetries??this.maxRetries;n==null&&(n=s),await this.prepareOptions(i);let{req:o,url:d,timeout:l}=await this.buildRequest(i,{retryCount:s-n});await this.prepareRequest(o,{url:d,options:i});let U="log_"+(Math.random()*(1<<24)|0).toString(16).padStart(6,"0"),u=a===void 0?"":`, retryOf: ${a}`,r=Date.now();if(X(this).debug(`[${U}] sending request`,mt({retryOfRequestLogID:a,method:i.method,url:d,options:i,headers:o.headers})),i.signal?.aborted)throw new at;let S=new AbortController,c=await this.fetchWithTimeout(d,o,l,S).catch(He),h=Date.now();if(c instanceof globalThis.Error){let k=`retrying, ${n} attempts remaining`;if(i.signal?.aborted)throw new at;let f=We(c)||/timed? ?out/i.test(String(c)+("cause"in c?String(c.cause):""));if(n)return X(this).info(`[${U}] connection ${f?"timed out":"failed"} - ${k}`),X(this).debug(`[${U}] connection ${f?"timed out":"failed"} (${k})`,mt({retryOfRequestLogID:a,url:d,durationMs:h-r,message:c.message})),this.retryRequest(i,n,a??U);throw X(this).info(`[${U}] connection ${f?"timed out":"failed"} - error; no more retries left`),X(this).debug(`[${U}] connection ${f?"timed out":"failed"} (error; no more retries left)`,mt({retryOfRequestLogID:a,url:d,durationMs:h-r,message:c.message})),f?new wt:new Nt({cause:c})}let m=`[${U}${u}] ${o.method} ${d} ${c.ok?"succeeded":"failed"} with status ${c.status} in ${h-r}ms`;if(!c.ok){let k=await this.shouldRetry(c);if(n&&k){let E=`retrying, ${n} attempts remaining`;return await Ti(c.body),X(this).info(`${m} - ${E}`),X(this).debug(`[${U}] response error (${E})`,mt({retryOfRequestLogID:a,url:c.url,status:c.status,headers:c.headers,durationMs:h-r})),this.retryRequest(i,n,a??U,c.headers)}let f=k?"error; no more retries left":"error; not retryable";X(this).info(`${m} - ${f}`);let C=await c.text().catch(E=>He(E).message),A=gi(C),R=A?void 0:C;throw X(this).debug(`[${U}] response error (${f})`,mt({retryOfRequestLogID:a,url:c.url,status:c.status,headers:c.headers,message:R,durationMs:Date.now()-r})),this.makeStatusError(c.status,A,R,c.headers)}return X(this).info(m),X(this).debug(`[${U}] response start`,mt({retryOfRequestLogID:a,url:c.url,status:c.status,headers:c.headers,durationMs:h-r})),{response:c,options:i,controller:S,requestLogID:U,retryOfRequestLogID:a,startTime:r}}getAPIList(t,n,a){return this.requestAPIList(n,a&&"then"in a?a.then(i=>({method:"get",path:t,...i})):{method:"get",path:t,...a})}requestAPIList(t,n){let a=this.makeRequest(n,null,void 0);return new Ve(this,a,t)}async fetchWithTimeout(t,n,a,i){let{signal:s,method:o,...d}=n||{},l=this._makeAbort(i);s&&s.addEventListener("abort",l,{once:!0});let U=setTimeout(l,a),u=globalThis.ReadableStream&&d.body instanceof globalThis.ReadableStream||typeof d.body=="object"&&d.body!==null&&Symbol.asyncIterator in d.body,r={signal:i.signal,...u?{duplex:"half"}:{},method:"GET",...d};o&&(r.method=o.toUpperCase());try{return await this.fetch.call(void 0,t,r)}finally{clearTimeout(U)}}async shouldRetry(t){let n=t.headers.get("x-should-retry");return n==="true"?!0:n==="false"?!1:t.status===408||t.status===409||t.status===429||t.status>=500}async retryRequest(t,n,a,i){let s,o=i?.get("retry-after-ms");if(o){let l=parseFloat(o);Number.isNaN(l)||(s=l)}let d=i?.get("retry-after");if(d&&!s){let l=parseFloat(d);Number.isNaN(l)?s=Date.parse(d)-Date.now():s=l*1e3}if(s===void 0){let l=t.maxRetries??this.maxRetries;s=this.calculateDefaultRetryTimeoutMillis(n,l)}return await pi(s),this.makeRequest(t,n-1,a)}calculateDefaultRetryTimeoutMillis(t,n){let s=n-t,o=Math.min(.5*Math.pow(2,s),8),d=1-Math.random()*.25;return o*d*1e3}async buildRequest(t,{retryCount:n=0}={}){let a={...t},{method:i,path:s,query:o,defaultBaseURL:d}=a,l=this.buildURL(s,o,d);"timeout"in a&&ci("timeout",a.timeout),a.timeout=a.timeout??this.timeout;let{bodyHeaders:U,body:u}=this.buildBody({options:a}),r=await this.buildHeaders({options:t,method:i,bodyHeaders:U,retryCount:n});return{req:{method:i,headers:r,...a.signal&&{signal:a.signal},...globalThis.ReadableStream&&u instanceof globalThis.ReadableStream&&{duplex:"half"},...u&&{body:u},...this.fetchOptions??{},...a.fetchOptions??{}},url:l,timeout:a.timeout}}async buildHeaders({options:t,method:n,bodyHeaders:a,retryCount:i}){let s={};this.idempotencyHeader&&n!=="get"&&(t.idempotencyKey||(t.idempotencyKey=this.defaultIdempotencyKey()),s[this.idempotencyHeader]=t.idempotencyKey);let o=T([s,{Accept:"application/json","User-Agent":this.getUserAgent(),"X-Stainless-Retry-Count":String(i),...t.timeout?{"X-Stainless-Timeout":String(Math.trunc(t.timeout/1e3))}:{},...Ci(),"X-\u004b\u0065\u0072\u006e\u0065\u006c-Project-Id":this.projectID,"X-\u004b\u0065\u0072\u006e\u0065\u006c-Project":this.project},await this.authHeaders(t),this._options.defaultHeaders,a,t.headers]);return this.validateHeaders(o),o.values}_makeAbort(t){return()=>t.abort()}buildBody({options:t}){let{body:n,headers:a}=t;if(!n)return n==null&&"body"in t?Y(this,Je,"f").call(this,{body:n,headers:T([a])}):{bodyHeaders:void 0,body:void 0};let i=T([a]);return ArrayBuffer.isView(n)||n instanceof ArrayBuffer||n instanceof DataView||typeof n=="string"&&i.values.has("content-type")||globalThis.Blob&&n instanceof globalThis.Blob||n instanceof FormData||n instanceof URLSearchParams||globalThis.ReadableStream&&n instanceof globalThis.ReadableStream?{bodyHeaders:void 0,body:n}:typeof n=="object"&&(Symbol.asyncIterator in n||Symbol.iterator in n&&"next"in n&&typeof n.next=="function")?{bodyHeaders:void 0,body:Un(n)}:typeof n=="object"&&i.values.get("content-type")==="application/x-www-form-urlencoded"?{bodyHeaders:{"content-type":"application/x-www-form-urlencoded"},body:this.stringifyQuery(n)}:Y(this,Je,"f").call(this,{body:n,headers:i})}app(t){return new Mn(t)}};ka=H,Je=new WeakMap,Ca=new WeakSet,ji=function(){return this.baseURL!==zi[this._options.environment||"production"]};H.\u004b\u0065\u0072\u006e\u0065\u006c=ka;H.DEFAULT_TIMEOUT=6e4;H.\u004b\u0065\u0072\u006e\u0065\u006cError=G;H.APIError=tt;H.APIConnectionError=Nt;H.APIConnectionTimeoutError=wt;H.APIUserAbortError=at;H.NotFoundError=de;H.ConflictError=le;H.RateLimitError=Ue;H.BadRequestError=se;H.AuthenticationError=oe;H.InternalServerError=ue;H.PermissionDeniedError=re;H.UnprocessableEntityError=Se;H.toFile=kn;H.Deployments=Yt;H.Apps=Bt;H.Invocations=Jt;H.Browsers=rt;H.Profiles=qt;H.Auth=Ct;H.Telemetry=Et;H.Proxies=Xt;H.Extensions=Kt;H.BrowserPools=Dt;H.Credentials=Vt;H.Projects=_t;H.Organization=kt;H.AuditLogs=bt;H.APIKeys=Ht;H.CredentialProviders=Gt;var K={container:".eJH8qe.adDDi",dataInitq:"[data-initq]",item:".related-question-pair",itemDataQ:"data-q",itemDataInitQ:"data-initq",itemQuestionEl:".JlqpRe",answerContainer:".bCOlv, .hgKElc, .wDYxhc, .LGOjhe, .fo7IQd, .fmW3u",aiAnswerBody:".pWvJNd",aiCitation:"a.PMDqCb[href]",aiSourceCard:"a.vIWmYe[href], a.Zbfntb[href]",sourceTitle:"h3",sourceSite:".VuuXrf",sourceCite:"cite",clickTarget:".JlqpRe",expandedClass:"aoRk1c",captchaMarker:'#captcha-form, #recaptcha, form[action*="/sorry/"], .g-recaptcha, [data-sitekey]'},$i={container:'div[jscontroller="HWk0Gf"]',sectionHeading:'.mgAbYb[role="heading"]',item:"a.rIRoqf"},fn={udm:"39",item:"a.rIRoqf",durationPattern:/^\d+:\d+$/,platforms:["YouTube","TikTok","Instagram","Facebook","X"]},Qi={section:".ULSxyf",item:"a.KYg7td.INpicf",title:".hyYc0c",source:".K4ETW"},Zi={sectionTag:"g-section-with-header",sectionHeadingText:"What people are saying",card:'.dRzkFf[role="listitem"]',cardLink:'a.WlydOe[jsname="YKoRaf"]',titleH1:"h1.WQWxe",titleDiv:".eAaXgc",popularCommentLabel:".qgdis",source:".sTl1Td",platformBadge:".appd0, .KrMNbf",ytChannel:".sjVJQd",ytDate:".PLq9Je",authorNote:".nDgy9d"},ts={root:"[data-lhcontainer][data-streaming-container][eid]",legacyRoot:'[data-hveid="CBMQAA"]',wrapper:".Fgyi2e",controller:'[jscontroller="AkrxPe"]',contentSubtree:'[data-subtree="mfc"]',header:".heWuVc",heading:".Fzsovc.cwYVJe.RJPOee",showMoreButton:'[aria-label="Show more AI Overview"]',sourcesPanel:".OZ9ddf.WAUd4",disclaimer:".DuQANe.MSJHRb"},es={root:'[data-hveid="CAUQAA"]',wrapper:".Fgyi2e",citations:'.Fgyi2e [data-hveid] a[jsname="pxBnId"]'},ns={result:".wHYlTd.tF2Cxc",title:"h3.LC20lb",siteName:".VuuXrf",cite:"cite.tjvcx",snippet:".VwiC3b",redditCite:"cite.qLRx3b",ratingWrap:".Y0A0hc",ratingValue:".yi40Hd",reviewCount:".RDApEe"},as={headingText:"Businesses",card:".w7Dbne",name:".OSrXXb",ratingValue:".yi40Hd",reviewCount:".RDApEe"};var is="Google returned a CAPTCHA. Run with --headless=false to re-warm the browser profile, then retry.";var Zr=["request_aborted","captcha_exhausted","captcha_or_blocked","location_mismatch","proxy_tunnel_failed","proxy_unavailable","harvest_timeout","mcp_request_timeout","mcp_http_error","extraction_failed","concurrency_limit_exceeded","unauthorized","forbidden","invalid_request","rate_limited","upstream_rate_limited","insufficient_balance","service_unavailable","response_lost","vendor_unavailable","page_not_found","page_forbidden","page_rate_limited","page_server_error","page_http_error","bot_check_unresolved","page_too_large","page_unreachable","browser_session_interrupted","site_export_not_found","site_export_format_unavailable","site_export_read_failed","site_export_image_not_found","image_url_invalid","image_url_not_https","image_host_unresolvable","image_host_blocked","image_redirect_invalid","image_source_unavailable","image_too_large","image_redirect_limit","image_base64_invalid","image_source_missing","image_source_ambiguous","image_source_type_invalid","image_artifact_not_found","image_page_too_large","page_image_not_found","page_image_candidates_unusable","image_format_unsupported","image_storage_unconfigured","featured_image_unhostable"],td=["request_aborted","captcha","location_mismatch","connection","timeout","extraction","concurrency_limit","unauthorized","forbidden","invalid_request","rate_limit","billing","service_unavailable"];var Dh=new Set(Zr),Fh=new Set(td);function ss(e){return e.replace((new RegExp("https?:\\/\\/[^\\s]*(?:\u0062\u0072\u0069\u0067\u0068\u0074\u0064\u0061\u0074\u0061|on\u006b\u0065\u0072\u006e\u0065\u006c|\u006b\u0065\u0072\u006e\u0065\u006c\\.sh)[^\\s]*", "gi")),"the service").replace(/bright\s*data/gi,"the service").replace((new RegExp("\u0062\u0072\u0069\u0067\u0068\u0074\u0064\u0061\u0074\u0061", "gi")),"the service").replace((new RegExp("\u006b\u0065\u0072\u006e\u0065\u006c\\.sh\\s+sessions?", "gi")),"sessions").replace((new RegExp("\u006b\u0065\u0072\u006e\u0065\u006c\\.sh\\s+session", "gi")),"this session").replace((new RegExp("\u006b\u0065\u0072\u006e\u0065\u006c\\.sh", "gi")),"the service").replace((new RegExp("\u006b\u0065\u0072\u006e\u0065\u006c\\s+sessions?", "gi")),"sessions").replace((new RegExp("\u006b\u0065\u0072\u006e\u0065\u006c\\s+session", "gi")),"this session").replace(/\b(?:sub|cus|sess|proxy)_[A-Za-z0-9_-]+\b/g,"[redacted]").replace((new RegExp("\\b(?:\u004b\u0045\u0052\u004e\u0045\u004c|\u0042\u0052\u0049\u0047\u0048\u0054\u0044\u0041\u0054\u0041|\u0042\u0052\u0049\u0047\u0048\u0054_\u0044\u0041\u0054\u0041)_[A-Z0-9_]*API_KEY\\b", "g")),"BROWSER_SERVICE_API_KEY").replace((new RegExp("\\b\u006b\u0065\u0072\u006e\u0065\u006c\\b", "gi")),"the service").replace(/ +/g," ").trim()}var nt=class extends Error{constructor(n){super(`CAPTCHA detected. ${n}`);this.instructions=n}instructions;name="CaptchaError"},Ee=class extends Error{constructor(n,a){super(n);this.cause=a}cause;name="ExtractionError"};var ut=class extends Error{name="RequestAbortedError";constructor(t="Request aborted before harvest completed"){super(t)}},jt=class extends Error{name="PaaInteractionInterruptedError"},qe=class extends Error{name="LocationMismatchError";constructor(t="Google returned results for a different location than requested"){super(t)}};async function Nn(e,t,n,a){let i,s=!1,o,d=()=>{},l=()=>a?.reason instanceof DOMException&&a.reason.name==="TimeoutError"?a.reason:new ut,U=()=>d(l()),u=new Promise((S,c)=>{d=c}),r=async S=>{let c;try{await Promise.race([S.close().catch(()=>{}),new Promise(h=>{c=setTimeout(h,2e3)})])}finally{c&&clearTimeout(c)}};if(a?.aborted)throw l();a?.addEventListener("abort",U,{once:!0}),o=setTimeout(()=>d(new DOMException("Optional pagination timed out","TimeoutError")),3e4);try{return await Promise.race([(async()=>{let S=await e.newPage();if(s)throw await r(S),new ut;if(i=S,await t(S),s)throw new ut;return n(S)})(),u])}finally{s=!0,o&&clearTimeout(o),a?.removeEventListener("abort",U),i&&await r(i)}}var Na=require("crypto");var os=require("@libsql/client/http"),Xe=require("crypto"),j=require("zod");var Tt="strftime('%Y-%m-%dT%H:%M:%fZ','now')";var ed=[`CREATE TABLE IF NOT EXISTS oauth_store_metadata (
9
+ ${r}`)}return o},g=rr(Vi);var Ht=class extends b{create(t,n){return this._client.post("/org/api_keys",{body:t,...n})}retrieve(t,n={},a){return this._client.get(g`/org/api_keys/${t}`,{query:n,...a})}update(t,n,a){return this._client.patch(g`/org/api_keys/${t}`,{body:n,...a})}list(t={},n){return this._client.getAPIList("/org/api_keys",B,{query:t,...n})}delete(t,n){return this._client.delete(g`/org/api_keys/${t}`,{...n,headers:T([{Accept:"*/*"},n?.headers])})}rotate(t,n={},a){return this._client.post(g`/org/api_keys/${t}/rotate`,{body:n,...a})}};var Bt=class extends b{list(t={},n){return this._client.getAPIList("/apps",B,{query:t,...n})}};var he=class extends b{create(t,n){return this._client.post("/audit-logs/export/destinations",{body:t,...n})}retrieve(t,n){return this._client.get(g`/audit-logs/export/destinations/${t}`,n)}update(t,n,a){return this._client.patch(g`/audit-logs/export/destinations/${t}`,{body:n,...a})}list(t={},n){return this._client.getAPIList("/audit-logs/export/destinations",B,{query:t,...n})}delete(t,n){return this._client.delete(g`/audit-logs/export/destinations/${t}`,{...n,headers:T([{Accept:"*/*"},n?.headers])})}test(t,n){return this._client.post(g`/audit-logs/export/destinations/${t}/test`,n)}};var lr=6,Sr=5e4,Ur=8e3,Ut=class extends G{};async function Ki(e,t,n,a,i={}){if(!n||typeof n.write!="function")throw new TypeError("audit log download destination must provide write()");let{onProgress:s,maxTransferRetries:o=lr,...d}=i;if(!Number.isInteger(o)||o<0)throw new TypeError("maxTransferRetries must be a non-negative integer");let l=d.timeout??a,U,u={bytesWritten:0,chunks:0,rows:0},r=new Set;for(;;){let S=await ur(e,U?{...t,cursor:U}:t,d,o,l),{nextCursor:c,hasMore:h,rows:m}=cr(S.headers,U);if(h&&c){if(r.has(c))throw new Ut("response repeated X-Next-Cursor header");r.add(c)}if(await pr(n,S.body),U=c,u.bytesWritten+=S.body.byteLength,u.chunks+=1,u.rows+=m,s&&await s({...u,chunkRows:m}),!h)return u}}async function ur(e,t,n,a,i){for(let s=0;;s+=1){let o=new AbortController,d=()=>o.abort();n.signal?.aborted?o.abort():n.signal?.addEventListener("abort",d,{once:!0});let l;try{l=await e(t,{...n,signal:o.signal})}catch(r){throw n.signal?.removeEventListener("abort",d),r}let U=!1,u=setTimeout(()=>{U=!0,o.abort()},i);try{let r=new Uint8Array(await l.arrayBuffer());if(clearTimeout(u),n.signal?.aborted)throw new at;if(U)throw new wt;let S=l.headers.get("x-content-sha256");if(!S)throw new Ut("response missing X-Content-Sha256 header");let c=await hr(r);if(n.signal?.aborted)throw new at;if(c!==S)throw new Ut(`audit log chunk checksum mismatch (got ${c}, want ${S})`);return{body:r,headers:l.headers}}catch(r){if(n.signal?.aborted&&!(r instanceof at)?r=new at:U&&(r=new wt),s===a||r instanceof at)throw r;await gr(s+1,n.signal)}finally{clearTimeout(u),n.signal?.removeEventListener("abort",d)}}}function cr(e,t){let n=e.get("x-has-more");if(n!=="true"&&n!=="false")throw new Ut("response missing or invalid X-Has-More header");let a=n==="true",i=e.get("x-row-count");if(i===null||!/^[0-9]+$/.test(i))throw new Ut("response missing or invalid X-Row-Count header");let s=Number(i);if(!Number.isSafeInteger(s)||s>Sr)throw new Ut("response missing or invalid X-Row-Count header");let o=e.get("x-next-cursor")||void 0;if(a&&(!o||o===t))throw new Ut("response has invalid X-Next-Cursor header");if(!a&&o)throw new Ut("response returned a cursor after the final chunk");return{rows:s,nextCursor:o,hasMore:a}}async function hr(e){let t=await globalThis.crypto.subtle.digest("SHA-256",e);return Array.from(new Uint8Array(t),n=>n.toString(16).padStart(2,"0")).join("")}async function gr(e,t){let n=Math.min(1e3*2**(e-1),Ur);await new Promise((a,i)=>{if(t?.aborted){i(new at);return}let s=()=>{clearTimeout(o),i(new at)},o=setTimeout(()=>{t?.removeEventListener("abort",s),a()},n);t?.addEventListener("abort",s,{once:!0})})}async function pr(e,t){let n=0;for(;n<t.byteLength;){let a=await e.write(n===0?t:t.subarray(n));if(typeof a=="number"){n+=Yi(a,t.byteLength-n);continue}if(a&&typeof a=="object"&&"bytesWritten"in a){let i=a.bytesWritten;n+=Yi(i,t.byteLength-n);continue}return}}function Yi(e,t){if(!Number.isSafeInteger(e)||e<=0||e>t)throw new Ut("audit log download destination performed a short write");return e}var bt=class extends b{constructor(){super(...arguments),this.exportDestinations=new he(this._client)}list(t,n){return this._client.getAPIList("/audit-logs",yn,{query:t,...n})}exportChunk(t,n){return this._client.get("/audit-logs/export/chunk",{query:t,...n,headers:T([{Accept:"application/octet-stream"},n?.headers]),__binaryResponse:!0})}download(t,n,a){return Ki((i,s)=>this.exportChunk(i,s),t,n,this._client.timeout,a)}};bt.ExportDestinations=he;var ge=class extends b{create(t,n){return this._client.post("/auth/connections",{body:t,...n})}retrieve(t,n){return this._client.get(g`/auth/connections/${t}`,n)}update(t,n,a){return this._client.patch(g`/auth/connections/${t}`,{body:n,...a})}list(t={},n){return this._client.getAPIList("/auth/connections",B,{query:t,...n})}delete(t,n){return this._client.delete(g`/auth/connections/${t}`,{...n,headers:T([{Accept:"*/*"},n?.headers])})}follow(t,n){return this._client.get(g`/auth/connections/${t}/events`,{...n,headers:T([{Accept:"text/event-stream"},n?.headers]),stream:!0})}login(t,n={},a){return this._client.post(g`/auth/connections/${t}/login`,{body:n,...a})}submit(t,n,a){return this._client.post(g`/auth/connections/${t}/submit`,{body:n,...a})}timeline(t,n={},a){return this._client.getAPIList(g`/auth/connections/${t}/timeline`,B,{query:n,...a})}};var pe=class extends b{retrieve(t){return this._client.get("/auth/context",t)}};var Ct=class extends b{constructor(){super(...arguments),this.context=new pe(this._client),this.connections=new ge(this._client)}};Ct.Context=pe;Ct.Connections=ge;var Dt=class extends b{create(t,n){return this._client.post("/browser_pools",{body:t,...n})}retrieve(t,n){return this._client.get(g`/browser_pools/${t}`,n)}update(t,n,a){return this._client.patch(g`/browser_pools/${t}`,{body:n,...a})}list(t={},n){return this._client.getAPIList("/browser_pools",B,{query:t,...n})}delete(t,n={},a){return this._client.delete(g`/browser_pools/${t}`,{body:n,...a,headers:T([{Accept:"*/*"},a?.headers])})}acquire(t,n,a){return this._client.post(g`/browser_pools/${t}/acquire`,{body:n,...a})}flush(t,n){return this._client.post(g`/browser_pools/${t}/flush`,{...n,headers:T([{Accept:"*/*"},n?.headers])})}release(t,n,a){return this._client.post(g`/browser_pools/${t}/release`,{body:n,...a,headers:T([{Accept:"*/*"},a?.headers])})}};var we=class extends b{batch(t,n,a){return this._client.post(g`/browsers/${t}/computer/batch`,{body:n,...a,headers:T([{Accept:"*/*"},a?.headers])})}captureScreenshot(t,n={},a){return this._client.post(g`/browsers/${t}/computer/screenshot`,{body:n,...a,headers:T([{Accept:"image/png"},a?.headers]),__binaryResponse:!0})}clickMouse(t,n,a){return this._client.post(g`/browsers/${t}/computer/click_mouse`,{body:n,...a,headers:T([{Accept:"*/*"},a?.headers])})}dragMouse(t,n,a){return this._client.post(g`/browsers/${t}/computer/drag_mouse`,{body:n,...a,headers:T([{Accept:"*/*"},a?.headers])})}getMousePosition(t,n){return this._client.post(g`/browsers/${t}/computer/get_mouse_position`,n)}moveMouse(t,n,a){return this._client.post(g`/browsers/${t}/computer/move_mouse`,{body:n,...a,headers:T([{Accept:"*/*"},a?.headers])})}pressKey(t,n,a){return this._client.post(g`/browsers/${t}/computer/press_key`,{body:n,...a,headers:T([{Accept:"*/*"},a?.headers])})}readClipboard(t,n){return this._client.post(g`/browsers/${t}/computer/clipboard/read`,n)}scroll(t,n,a){return this._client.post(g`/browsers/${t}/computer/scroll`,{body:n,...a,headers:T([{Accept:"*/*"},a?.headers])})}setCursorVisibility(t,n,a){return this._client.post(g`/browsers/${t}/computer/cursor`,{body:n,...a})}typeText(t,n,a){return this._client.post(g`/browsers/${t}/computer/type`,{body:n,...a,headers:T([{Accept:"*/*"},a?.headers])})}writeClipboard(t,n,a){return this._client.post(g`/browsers/${t}/computer/clipboard/write`,{body:n,...a,headers:T([{Accept:"*/*"},a?.headers])})}};var me=class extends b{stream(t,n,a){return this._client.get(g`/browsers/${t}/logs/stream`,{query:n,...a,headers:T([{Accept:"text/event-stream"},a?.headers]),stream:!0})}};var ye=class extends b{execute(t,n,a){return this._client.post(g`/browsers/${t}/playwright/execute`,{body:n,...a})}};var Ce=class extends b{exec(t,n,a){return this._client.post(g`/browsers/${t}/process/exec`,{body:n,...a})}kill(t,n,a){let{id:i,...s}=n;return this._client.post(g`/browsers/${i}/process/${t}/kill`,{body:s,...a})}resize(t,n,a){let{id:i,...s}=n;return this._client.post(g`/browsers/${i}/process/${t}/resize`,{body:s,...a})}spawn(t,n,a){return this._client.post(g`/browsers/${t}/process/spawn`,{body:n,...a})}status(t,n,a){let{id:i}=n;return this._client.get(g`/browsers/${i}/process/${t}/status`,a)}stdin(t,n,a){let{id:i,...s}=n;return this._client.post(g`/browsers/${i}/process/${t}/stdin`,{body:s,...a})}stdoutStream(t,n,a){let{id:i}=n;return this._client.get(g`/browsers/${i}/process/${t}/stdout/stream`,{...a,headers:T([{Accept:"text/event-stream"},a?.headers]),stream:!0})}};var ke=class extends b{list(t,n){return this._client.get(g`/browsers/${t}/replays`,n)}download(t,n,a){let{id:i}=n;return this._client.get(g`/browsers/${i}/replays/${t}`,{...a,headers:T([{Accept:"video/mp4"},a?.headers]),__binaryResponse:!0})}start(t,n={},a){return this._client.post(g`/browsers/${t}/replays`,{body:n,...a})}stop(t,n,a){let{id:i}=n;return this._client.post(g`/browsers/${i}/replays/${t}/stop`,{...a,headers:T([{Accept:"*/*"},a?.headers])})}};var Te=class extends b{events(t,n={},a){return this._client.getAPIList(g`/browsers/${t}/telemetry/events`,B,{query:n,...a})}stream(t,n={},a){let{"Last-Event-ID":i,...s}=n??{};return this._client.get(g`/browsers/${t}/telemetry/stream`,{query:s,...a,headers:T([{Accept:"text/event-stream",...i!=null?{"Last-Event-ID":i}:void 0},a?.headers]),stream:!0})}};var Me=class extends b{events(t,n,a){let{id:i}=n;return this._client.get(g`/browsers/${i}/fs/watch/${t}/events`,{...a,headers:T([{Accept:"text/event-stream"},a?.headers]),stream:!0})}start(t,n,a){return this._client.post(g`/browsers/${t}/fs/watch`,{body:n,...a})}stop(t,n,a){let{id:i}=n;return this._client.delete(g`/browsers/${i}/fs/watch/${t}`,{...a,headers:T([{Accept:"*/*"},a?.headers])})}};var Ft=class extends b{constructor(){super(...arguments),this.watch=new Me(this._client)}createDirectory(t,n,a){return this._client.put(g`/browsers/${t}/fs/create_directory`,{body:n,...a,headers:T([{Accept:"*/*"},a?.headers])})}deleteDirectory(t,n,a){return this._client.put(g`/browsers/${t}/fs/delete_directory`,{body:n,...a,headers:T([{Accept:"*/*"},a?.headers])})}deleteFile(t,n,a){return this._client.put(g`/browsers/${t}/fs/delete_file`,{body:n,...a,headers:T([{Accept:"*/*"},a?.headers])})}downloadDirZip(t,n,a){return this._client.get(g`/browsers/${t}/fs/download_dir_zip`,{query:n,...a,headers:T([{Accept:"application/zip"},a?.headers]),__binaryResponse:!0})}fileInfo(t,n,a){return this._client.get(g`/browsers/${t}/fs/file_info`,{query:n,...a})}listFiles(t,n,a){return this._client.get(g`/browsers/${t}/fs/list_files`,{query:n,...a})}move(t,n,a){return this._client.put(g`/browsers/${t}/fs/move`,{body:n,...a,headers:T([{Accept:"*/*"},a?.headers])})}readFile(t,n,a){return this._client.get(g`/browsers/${t}/fs/read_file`,{query:n,...a,headers:T([{Accept:"application/octet-stream"},a?.headers]),__binaryResponse:!0})}setFilePermissions(t,n,a){return this._client.put(g`/browsers/${t}/fs/set_file_permissions`,{body:n,...a,headers:T([{Accept:"*/*"},a?.headers])})}upload(t,n,a){return this._client.post(g`/browsers/${t}/fs/upload`,yt({body:n,...a,headers:T([{Accept:"*/*"},a?.headers])},this._client))}uploadZip(t,n,a){return this._client.post(g`/browsers/${t}/fs/upload_zip`,yt({body:n,...a,headers:T([{Accept:"*/*"},a?.headers])},this._client))}writeFile(t,n,a,i){let{path:s,mode:o}=a;return this._client.put(g`/browsers/${t}/fs/write_file`,{body:n,query:{path:s,mode:o},...i,headers:T([{"Content-Type":"application/octet-stream",Accept:"*/*"},i?.headers])})}};Ft.Watch=Me;function Tn(e,t){return`${e.replace(/\/+$/,"")}${t.startsWith("/")?t:`/${t}`}`}async function Ji(e,t,n,a){let i=e.browserRouteCache.get(t);if(!i)throw new G(`browser route cache does not contain session ${t}; create, retrieve, or list the browser before calling browser.fetch`);let{url:s,method:o,headers:d,body:l,signal:U,duplex:u,timeout_ms:r}=_r(n,a);Er(s);let S={url:s,jwt:i.jwt};r!==void 0&&(S.timeout_ms=r);let c=d.get("accept"),h={method:br(o),path:Tn(i.baseURL,"/curl/raw"),query:S,body:l,headers:T([{Authorization:null},c?{Accept:c}:{Accept:"*/*"},Pr(d)]),signal:U??null,__binaryResponse:!0};return u&&(h.fetchOptions={duplex:u}),e.request(h).asResponse()}function br(e){let t=e.toLowerCase();if(!new Set(["get","post","put","patch","delete"]).has(t))throw new G(`browser.fetch unsupported HTTP method: ${e}`);return t}function _r(e,t){let n=t&&"timeout_ms"in t?t.timeout_ms:void 0;if(e instanceof Request){let i=new Headers(e.headers);t?.headers&&new Headers(t.headers).forEach((U,u)=>{i.set(u,U)});let s={url:e.url,method:(t?.method??e.method)?.toUpperCase()||"GET",headers:i},o=t?.body??e.body;o!=null&&(s.body=o);let d=t?.signal??e.signal;return d!==void 0&&(s.signal=d),t?.duplex!==void 0&&(s.duplex=t.duplex),n!==void 0&&(s.timeout_ms=n),s}let a={url:e instanceof URL?e.href:String(e),method:(t?.method??"GET").toUpperCase(),headers:new Headers(t?.headers)};return t?.body!==void 0&&(a.body=t.body),t?.signal!==void 0&&(a.signal=t.signal),t?.duplex!==void 0&&(a.duplex=t.duplex),n!==void 0&&(a.timeout_ms=n),a}function Er(e){let t;try{t=new URL(e)}catch{throw new G(`browser.fetch target must be an absolute URL; received: ${e}`)}if(t.protocol!=="http:"&&t.protocol!=="https:")throw new G(`browser.fetch only supports http(s) URLs; received: ${t.protocol}`)}function Pr(e){let t={};return e.forEach((n,a)=>{switch(a.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:t[a]=n}}),t}var rt=class extends b{constructor(){super(...arguments),this.telemetry=new Te(this._client),this.replays=new ke(this._client),this.fs=new Ft(this._client),this.process=new Ce(this._client),this.logs=new me(this._client),this.computer=new we(this._client),this.playwright=new ye(this._client)}create(t={},n){return this._client.post("/browsers",{body:t,...n})}retrieve(t,n={},a){return this._client.get(g`/browsers/${t}`,{query:n,...a})}update(t,n,a){return this._client.patch(g`/browsers/${t}`,{body:n,...a})}list(t={},n){return this._client.getAPIList("/browsers",B,{query:t,...n})}curl(t,n,a){return this._client.post(g`/browsers/${t}/curl`,{body:n,...a})}fetch(t,n,a){return Ji(this._client,t,n,a)}deleteByID(t,n){return this._client.delete(g`/browsers/${t}`,{...n,headers:T([{Accept:"*/*"},n?.headers])})}loadExtensions(t,n,a){return this._client.post(g`/browsers/${t}/extensions`,yt({body:n,...a,headers:T([{Accept:"*/*"},a?.headers])},this._client))}};rt.Telemetry=Te;rt.Replays=ke;rt.Fs=Ft;rt.Process=Ce;rt.Logs=me;rt.Computer=we;rt.Playwright=ye;var Gt=class extends b{create(t,n){return this._client.post("/org/credential_providers",{body:t,...n})}retrieve(t,n){return this._client.get(g`/org/credential_providers/${t}`,n)}update(t,n,a){return this._client.patch(g`/org/credential_providers/${t}`,{body:n,...a})}list(t={},n){return this._client.getAPIList("/org/credential_providers",B,{query:t,...n})}delete(t,n){return this._client.delete(g`/org/credential_providers/${t}`,{...n,headers:T([{Accept:"*/*"},n?.headers])})}listItems(t,n){return this._client.get(g`/org/credential_providers/${t}/items`,n)}test(t,n){return this._client.post(g`/org/credential_providers/${t}/test`,n)}};var Vt=class extends b{create(t,n){return this._client.post("/credentials",{body:t,...n})}retrieve(t,n){return this._client.get(g`/credentials/${t}`,n)}update(t,n,a){return this._client.patch(g`/credentials/${t}`,{body:n,...a})}list(t={},n){return this._client.getAPIList("/credentials",B,{query:t,...n})}delete(t,n){return this._client.delete(g`/credentials/${t}`,{...n,headers:T([{Accept:"*/*"},n?.headers])})}totpCode(t,n){return this._client.get(g`/credentials/${t}/totp-code`,n)}};var Yt=class extends b{create(t,n){return this._client.post("/deployments",yt({body:t,...n},this._client))}retrieve(t,n){return this._client.get(g`/deployments/${t}`,n)}list(t={},n){return this._client.getAPIList("/deployments",B,{query:t,...n})}delete(t,n){return this._client.delete(g`/deployments/${t}`,{...n,headers:T([{Accept:"*/*"},n?.headers])})}follow(t,n={},a){return this._client.get(g`/deployments/${t}/events`,{query:n,...a,headers:T([{Accept:"text/event-stream"},a?.headers]),stream:!0})}};var Kt=class extends b{list(t={},n){return this._client.getAPIList("/extensions",B,{query:t,...n})}delete(t,n){return this._client.delete(g`/extensions/${t}`,{...n,headers:T([{Accept:"*/*"},n?.headers])})}download(t,n){return this._client.get(g`/extensions/${t}`,{...n,headers:T([{Accept:"application/octet-stream"},n?.headers]),__binaryResponse:!0})}downloadFromChromeStore(t,n){return this._client.get("/extensions/from_chrome_store",{query:t,...n,headers:T([{Accept:"application/octet-stream"},n?.headers]),__binaryResponse:!0})}get(t,n){return this._client.get(g`/extensions/${t}/metadata`,n)}upload(t,n){return this._client.post("/extensions",yt({body:t,...n},this._client))}};var Jt=class extends b{create(t,n){return this._client.post("/invocations",{body:t,...n})}retrieve(t,n){return this._client.get(g`/invocations/${t}`,n)}update(t,n,a){return this._client.patch(g`/invocations/${t}`,{body:n,...a})}list(t={},n){return this._client.getAPIList("/invocations",B,{query:t,...n})}deleteBrowsers(t,n){return this._client.delete(g`/invocations/${t}/browsers`,{...n,headers:T([{Accept:"*/*"},n?.headers])})}follow(t,n={},a){return this._client.get(g`/invocations/${t}/events`,{query:n,...a,headers:T([{Accept:"text/event-stream"},a?.headers]),stream:!0})}listBrowsers(t,n){return this._client.get(g`/invocations/${t}/browsers`,n)}};var fe=class extends b{retrieve(t){return this._client.get("/org/entitlements",t)}};var Ne=class extends b{retrieve(t){return this._client.get("/org/limits",t)}update(t,n){return this._client.patch("/org/limits",{body:t,...n})}};var kt=class extends b{constructor(){super(...arguments),this.entitlements=new fe(this._client),this.limits=new Ne(this._client)}};kt.Entitlements=fe;kt.Limits=Ne;var qt=class extends b{create(t,n){return this._client.post("/profiles",{body:t,...n})}retrieve(t,n){return this._client.get(g`/profiles/${t}`,n)}update(t,n,a){return this._client.patch(g`/profiles/${t}`,{body:n,...a})}list(t={},n){return this._client.getAPIList("/profiles",B,{query:t,...n})}delete(t,n){return this._client.delete(g`/profiles/${t}`,{...n,headers:T([{Accept:"*/*"},n?.headers])})}download(t,n={},a){return this._client.get(g`/profiles/${t}/download`,{query:n,...a,headers:T([{Accept:"application/octet-stream"},a?.headers]),__binaryResponse:!0})}};var ve=class extends b{retrieve(t,n){return this._client.get(g`/org/projects/${t}/limits`,n)}update(t,n,a){return this._client.patch(g`/org/projects/${t}/limits`,{body:n,...a})}};var _t=class extends b{constructor(){super(...arguments),this.limits=new ve(this._client)}create(t,n){return this._client.post("/org/projects",{body:t,...n})}retrieve(t,n){return this._client.get(g`/org/projects/${t}`,n)}update(t,n,a){return this._client.patch(g`/org/projects/${t}`,{body:n,...a})}list(t={},n){return this._client.getAPIList("/org/projects",B,{query:t,...n})}delete(t,n){return this._client.delete(g`/org/projects/${t}`,{...n,headers:T([{Accept:"*/*"},n?.headers])})}};_t.Limits=ve;var Xt=class extends b{create(t,n){return this._client.post("/proxies",{body:t,...n})}retrieve(t,n){return this._client.get(g`/proxies/${t}`,n)}update(t,n,a){return this._client.patch(g`/proxies/${t}`,{body:n,...a})}list(t={},n){return this._client.getAPIList("/proxies",B,{query:t,...n})}delete(t,n){return this._client.delete(g`/proxies/${t}`,{...n,headers:T([{Accept:"*/*"},n?.headers])})}check(t,n={},a){return this._client.post(g`/proxies/${t}/check`,{body:n,...a})}};var be=class extends b{create(t,n){return this._client.post("/telemetry/destinations",{body:t,...n})}retrieve(t,n){return this._client.get(g`/telemetry/destinations/${t}`,n)}update(t,n,a){return this._client.patch(g`/telemetry/destinations/${t}`,{body:n,...a})}list(t={},n){return this._client.getAPIList("/telemetry/destinations",B,{query:t,...n})}delete(t,n){return this._client.delete(g`/telemetry/destinations/${t}`,{...n,headers:T([{Accept:"*/*"},n?.headers])})}};var Et=class extends b{constructor(){super(...arguments),this.destinations=new be(this._client)}};Et.Destinations=be;var Ke=class{constructor(){this.entries=new Map}get(t){return this.entries.get(t)}set(t){this.entries.set(t.sessionId,t)}delete(t){this.entries.delete(t)}clear(){this.entries.clear()}},qi="\u004b\u0045\u0052\u004e\u0045\u004c_BROWSER_ROUTING_SUBRESOURCES",Rr=["curl","telemetry/stream"],Wr=/^\/(?:v\d+\/)?browsers(?:\/[^/]+)?\/?$/,Hr=/^\/(?:v\d+\/)?browser_pools\/[^/]+\/acquire\/?$/,Br=/^\/(?:v\d+\/)?browsers\/([^/]+)\/?$/,Dr=/^\/(?:v\d+\/)?browser_pools\/[^/]+\/release\/?$/;function ma(){let e=Zr();return e===void 0?[...Rr]:e.trim()===""?[]:e.split(",").map(t=>t.trim()).filter(Boolean)}function ya(e,{apiBaseURL:t,subresources:n,cache:a}){let i=[...n].map(o=>o.trim().replace(/^\/+|\/+$/g,"")).filter(Boolean),s=new URL(t).origin;return async(o,d)=>{let l=new Request(o,d),U=Fr(l,s),u=await Xr(e,{input:o,init:d,request:l},s,i,a);return U&&await Jr(u,a),await Gr(l,u,s,a),u}}function Fr(e,t){let n=new URL(e.url);return n.origin===t&&(Wr.test(n.pathname)||Hr.test(n.pathname))}async function Gr(e,t,n,a){if(!t.ok)return;let i=new URL(e.url);if(i.origin!==n)return;let s=Vr(e,i.pathname)??await Yr(e,i.pathname);s&&a.delete(s)}function Vr(e,t){if(e.method.toUpperCase()!=="DELETE")return;let n=t.match(Br);return n&&decodeURIComponent(n[1]??"")||void 0}async function Yr(e,t){if(!(e.method.toUpperCase()!=="POST"||!Dr.test(t)))try{let n=await e.clone().json();if(!n||typeof n!="object")return;let a=n.session_id;return typeof a=="string"&&a.trim()?a.trim():void 0}catch{return}}function Kr(e){if(!e||typeof e!="object")return;let t=e,n=typeof t.session_id=="string"?t.session_id.trim():"",a=typeof t.base_url=="string"?t.base_url.trim():"";if(!n||!a)return;let i=typeof t.jwt=="string"?t.jwt.trim():"",s=typeof t.cdp_ws_url=="string"?t.cdp_ws_url:void 0,o=i||Qr(s)||"";if(o)return{sessionId:n,baseURL:a,jwt:o}}async function Jr(e,t){if((e.headers.get("content-type")?.toLowerCase()??"").includes("application/json"))try{wa(await e.clone().json(),t)}catch{}}function wa(e,t){let n=Kr(e);if(n&&t.set(n),Array.isArray(e)){for(let a of e)wa(a,t);return}if(!(!e||typeof e!="object"))for(let a of Object.values(e))typeof a=="object"&&a!==null&&wa(a,t)}function qr(e,t){let n=e.replace(/^\/+|\/+$/g,"");return t.some(a=>n===a||n.startsWith(a+"/"))}async function Xr(e,{input:t,init:n,request:a},i,s,o){let d=new URL(a.url);if(d.origin!==i)return e(t,n);let l=d.pathname.match(/^\/(?:v\d+\/)?browsers\/([^/]+)\/([^/]+)(\/.*)?$/);if(!l)return e(t,n);let U=decodeURIComponent(l[1]??""),u=l[2]??"";if(!U||!qr(u+(l[3]??""),s))return e(t,n);let r=o.get(U);if(r===void 0)return e(t,n);let S=new URL(Tn(r.baseURL,`/${u}${l[3]??""}`));d.searchParams.forEach((h,m)=>{m!=="jwt"&&S.searchParams.append(m,h)}),S.searchParams.get("jwt")||S.searchParams.set("jwt",r.jwt);let c=new Headers(a.headers);return c.delete("authorization"),e(S.toString(),jr(t,a,n,c))}function jr(e,t,n,a){let i=t.method.toUpperCase(),s={...n??{},method:i,headers:a,redirect:t.redirect,signal:n?.signal??(e instanceof Request?e.signal:void 0)};if(delete s.body,delete s.duplex,i!=="GET"&&i!=="HEAD"){let o=zr(t,n);o!==void 0&&(s.body=o),n?.duplex!==void 0?s.duplex=n.duplex:$r(o)&&(s.duplex="half")}return s}function zr(e,t){return t?.body!==void 0&&t.body!==null?t.body:e.body??void 0}function $r(e){return globalThis.ReadableStream&&e instanceof globalThis.ReadableStream||typeof e=="object"&&e!==null&&Symbol.asyncIterator in e}function Qr(e){if(e)try{return new URL(e).searchParams.get("jwt")??void 0}catch{return}}function Zr(){if(typeof globalThis.process<"u"){let e=globalThis.process.env?.[qi];return typeof e=="string"?e:void 0}if(typeof globalThis.Deno<"u"){let e=globalThis.Deno.env?.get?.(qi);return typeof e=="string"?e:void 0}}var Mn=class{constructor(t){this.actions=new Map,this.name=t,Xi.registerApp(this)}action(t,n){let a;if(n.length===0)throw new Error("Action handlers must accept at least the context parameter");if(n.length===1){let i=n;a=async(s,o)=>i(s)}else a=n;return this.actions.set(t,{name:t,handler:a}),a}getActions(){return Array.from(this.actions.values())}getAction(t){return this.actions.get(t)}toJSON(){return{name:this.name,actions:this.getActions().map(t=>({name:t.name}))}}},Ca=class{constructor(){this.apps=new Map}registerApp(t){this.apps.set(t.name,t)}getApps(){return Array.from(this.apps.values())}getAppByName(t){return this.apps.get(t)}export(){return{apps:this.getApps().map(t=>t.toJSON())}}exportJSON(){return JSON.stringify(this.export(),null,2)}},Xi=new Ca;var _e=e=>{if(typeof globalThis.process<"u")return globalThis.process.env?.[e]?.trim()||void 0;if(typeof globalThis.Deno<"u")return globalThis.Deno.env?.get?.(e)?.trim()||void 0};var ka,Ta,Je,ji,zi={production:"https://api.on\u006b\u0065\u0072\u006e\u0065\u006c.com/",development:"https://localhost:3001/"},H=class{constructor({baseURL:t=_e("\u004b\u0045\u0052\u004e\u0045\u004c_BASE_URL"),apiKey:n=_e("\u004b\u0045\u0052\u004e\u0045\u004c_API_KEY"),projectID:a=null,project:i=null,...s}={}){if(ka.add(this),Je.set(this,void 0),this.deployments=new Yt(this),this.apps=new Bt(this),this.invocations=new Jt(this),this.browsers=new rt(this),this.profiles=new qt(this),this.auth=new Ct(this),this.telemetry=new Et(this),this.proxies=new Xt(this),this.extensions=new Kt(this),this.browserPools=new Dt(this),this.credentials=new Vt(this),this.projects=new _t(this),this.organization=new kt(this),this.auditLogs=new bt(this),this.apiKeys=new Ht(this),this.credentialProviders=new Gt(this),typeof globalThis<"u"&&typeof globalThis.Bun<"u"&&globalThis.Bun.version&&!_e("\u004b\u0045\u0052\u004e\u0045\u004c_SUPPRESS_BUN_WARNING")&&X(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"),n===void 0)throw new G("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 o={apiKey:n,projectID:a,project:i,...s,baseURL:t,environment:s.environment??"production"};if(t&&s.environment)throw new G("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=o.baseURL||zi[o.environment||"production"],this.timeout=o.timeout??Ta.DEFAULT_TIMEOUT,this.logger=o.logger??console;let d="warn";this.logLevel=d,this.logLevel=Ua(o.logLevel,"ClientOptions.logLevel",this)??Ua(_e("\u004b\u0045\u0052\u004e\u0045\u004c_LOG"),"process.env['\u004b\u0045\u0052\u004e\u0045\u004c_LOG']",this)??d,this.fetchOptions=o.fetchOptions,this.maxRetries=o.maxRetries??2,this.rawFetch=o.fetch??ki(),this.browserRouteCache=new Ke,this.fetch=ya(this.rawFetch,{apiBaseURL:this.baseURL,subresources:ma(),cache:this.browserRouteCache}),$(this,Je,Mi,"f");let l=_e("\u004b\u0045\u0052\u004e\u0045\u004c_CUSTOM_HEADERS");if(l){let U={};for(let u of l.split(`
10
+ `)){let r=u.indexOf(":");r>=0&&(U[u.substring(0,r).trim()]=u.substring(r+1).trim())}o.defaultHeaders={...U,...o.defaultHeaders}}this._options=o,this.apiKey=n,this.projectID=a,this.project=i}withOptions(t){let n=new this.constructor({...this._options,environment:t.environment?t.environment:void 0,baseURL:t.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,...t});return n.browserRouteCache=this.browserRouteCache,n.fetch=ya(n.rawFetch,{apiBaseURL:n.baseURL,subresources:ma(),cache:n.browserRouteCache}),n}defaultQuery(){return this._options.defaultQuery}validateHeaders({values:t,nulls:n}){}async authHeaders(t){return T([{Authorization:`Bearer ${this.apiKey}`}])}stringifyQuery(t){return Ai(t)}getUserAgent(){return`${this.constructor.name}/JS ${vt}`}defaultIdempotencyKey(){return`stainless-node-retry-${$n()}`}makeStatusError(t,n,a,i){return tt.generate(t,n,a,i)}buildURL(t,n,a){let i=!Y(this,ka,"m",ji).call(this)&&a||this.baseURL,s=Ui(t)?new URL(t):new URL(i+(i.endsWith("/")&&t.startsWith("/")?t.slice(1):t)),o=this.defaultQuery(),d=Object.fromEntries(s.searchParams);return(!ta(o)||!ta(d))&&(n={...d,...o,...n}),typeof n=="object"&&n&&!Array.isArray(n)&&(s.search=this.stringifyQuery(n)),s.toString()}async prepareOptions(t){}async prepareRequest(t,{url:n,options:a}){}get(t,n){return this.methodRequest("get",t,n)}post(t,n){return this.methodRequest("post",t,n)}patch(t,n){return this.methodRequest("patch",t,n)}put(t,n){return this.methodRequest("put",t,n)}delete(t,n){return this.methodRequest("delete",t,n)}methodRequest(t,n,a){return this.request(Promise.resolve(a).then(i=>({method:t,path:n,...i})))}request(t,n=null){return new Wt(this,this.makeRequest(t,n,void 0))}async makeRequest(t,n,a){let i=await t,s=i.maxRetries??this.maxRetries;n==null&&(n=s),await this.prepareOptions(i);let{req:o,url:d,timeout:l}=await this.buildRequest(i,{retryCount:s-n});await this.prepareRequest(o,{url:d,options:i});let U="log_"+(Math.random()*(1<<24)|0).toString(16).padStart(6,"0"),u=a===void 0?"":`, retryOf: ${a}`,r=Date.now();if(X(this).debug(`[${U}] sending request`,mt({retryOfRequestLogID:a,method:i.method,url:d,options:i,headers:o.headers})),i.signal?.aborted)throw new at;let S=new AbortController,c=await this.fetchWithTimeout(d,o,l,S).catch(He),h=Date.now();if(c instanceof globalThis.Error){let k=`retrying, ${n} attempts remaining`;if(i.signal?.aborted)throw new at;let f=We(c)||/timed? ?out/i.test(String(c)+("cause"in c?String(c.cause):""));if(n)return X(this).info(`[${U}] connection ${f?"timed out":"failed"} - ${k}`),X(this).debug(`[${U}] connection ${f?"timed out":"failed"} (${k})`,mt({retryOfRequestLogID:a,url:d,durationMs:h-r,message:c.message})),this.retryRequest(i,n,a??U);throw X(this).info(`[${U}] connection ${f?"timed out":"failed"} - error; no more retries left`),X(this).debug(`[${U}] connection ${f?"timed out":"failed"} (error; no more retries left)`,mt({retryOfRequestLogID:a,url:d,durationMs:h-r,message:c.message})),f?new wt:new Nt({cause:c})}let m=`[${U}${u}] ${o.method} ${d} ${c.ok?"succeeded":"failed"} with status ${c.status} in ${h-r}ms`;if(!c.ok){let k=await this.shouldRetry(c);if(n&&k){let E=`retrying, ${n} attempts remaining`;return await Ti(c.body),X(this).info(`${m} - ${E}`),X(this).debug(`[${U}] response error (${E})`,mt({retryOfRequestLogID:a,url:c.url,status:c.status,headers:c.headers,durationMs:h-r})),this.retryRequest(i,n,a??U,c.headers)}let f=k?"error; no more retries left":"error; not retryable";X(this).info(`${m} - ${f}`);let C=await c.text().catch(E=>He(E).message),A=gi(C),R=A?void 0:C;throw X(this).debug(`[${U}] response error (${f})`,mt({retryOfRequestLogID:a,url:c.url,status:c.status,headers:c.headers,message:R,durationMs:Date.now()-r})),this.makeStatusError(c.status,A,R,c.headers)}return X(this).info(m),X(this).debug(`[${U}] response start`,mt({retryOfRequestLogID:a,url:c.url,status:c.status,headers:c.headers,durationMs:h-r})),{response:c,options:i,controller:S,requestLogID:U,retryOfRequestLogID:a,startTime:r}}getAPIList(t,n,a){return this.requestAPIList(n,a&&"then"in a?a.then(i=>({method:"get",path:t,...i})):{method:"get",path:t,...a})}requestAPIList(t,n){let a=this.makeRequest(n,null,void 0);return new Ve(this,a,t)}async fetchWithTimeout(t,n,a,i){let{signal:s,method:o,...d}=n||{},l=this._makeAbort(i);s&&s.addEventListener("abort",l,{once:!0});let U=setTimeout(l,a),u=globalThis.ReadableStream&&d.body instanceof globalThis.ReadableStream||typeof d.body=="object"&&d.body!==null&&Symbol.asyncIterator in d.body,r={signal:i.signal,...u?{duplex:"half"}:{},method:"GET",...d};o&&(r.method=o.toUpperCase());try{return await this.fetch.call(void 0,t,r)}finally{clearTimeout(U)}}async shouldRetry(t){let n=t.headers.get("x-should-retry");return n==="true"?!0:n==="false"?!1:t.status===408||t.status===409||t.status===429||t.status>=500}async retryRequest(t,n,a,i){let s,o=i?.get("retry-after-ms");if(o){let l=parseFloat(o);Number.isNaN(l)||(s=l)}let d=i?.get("retry-after");if(d&&!s){let l=parseFloat(d);Number.isNaN(l)?s=Date.parse(d)-Date.now():s=l*1e3}if(s===void 0){let l=t.maxRetries??this.maxRetries;s=this.calculateDefaultRetryTimeoutMillis(n,l)}return await pi(s),this.makeRequest(t,n-1,a)}calculateDefaultRetryTimeoutMillis(t,n){let s=n-t,o=Math.min(.5*Math.pow(2,s),8),d=1-Math.random()*.25;return o*d*1e3}async buildRequest(t,{retryCount:n=0}={}){let a={...t},{method:i,path:s,query:o,defaultBaseURL:d}=a,l=this.buildURL(s,o,d);"timeout"in a&&ci("timeout",a.timeout),a.timeout=a.timeout??this.timeout;let{bodyHeaders:U,body:u}=this.buildBody({options:a}),r=await this.buildHeaders({options:t,method:i,bodyHeaders:U,retryCount:n});return{req:{method:i,headers:r,...a.signal&&{signal:a.signal},...globalThis.ReadableStream&&u instanceof globalThis.ReadableStream&&{duplex:"half"},...u&&{body:u},...this.fetchOptions??{},...a.fetchOptions??{}},url:l,timeout:a.timeout}}async buildHeaders({options:t,method:n,bodyHeaders:a,retryCount:i}){let s={};this.idempotencyHeader&&n!=="get"&&(t.idempotencyKey||(t.idempotencyKey=this.defaultIdempotencyKey()),s[this.idempotencyHeader]=t.idempotencyKey);let o=T([s,{Accept:"application/json","User-Agent":this.getUserAgent(),"X-Stainless-Retry-Count":String(i),...t.timeout?{"X-Stainless-Timeout":String(Math.trunc(t.timeout/1e3))}:{},...Ci(),"X-\u004b\u0065\u0072\u006e\u0065\u006c-Project-Id":this.projectID,"X-\u004b\u0065\u0072\u006e\u0065\u006c-Project":this.project},await this.authHeaders(t),this._options.defaultHeaders,a,t.headers]);return this.validateHeaders(o),o.values}_makeAbort(t){return()=>t.abort()}buildBody({options:t}){let{body:n,headers:a}=t;if(!n)return n==null&&"body"in t?Y(this,Je,"f").call(this,{body:n,headers:T([a])}):{bodyHeaders:void 0,body:void 0};let i=T([a]);return ArrayBuffer.isView(n)||n instanceof ArrayBuffer||n instanceof DataView||typeof n=="string"&&i.values.has("content-type")||globalThis.Blob&&n instanceof globalThis.Blob||n instanceof FormData||n instanceof URLSearchParams||globalThis.ReadableStream&&n instanceof globalThis.ReadableStream?{bodyHeaders:void 0,body:n}:typeof n=="object"&&(Symbol.asyncIterator in n||Symbol.iterator in n&&"next"in n&&typeof n.next=="function")?{bodyHeaders:void 0,body:Un(n)}:typeof n=="object"&&i.values.get("content-type")==="application/x-www-form-urlencoded"?{bodyHeaders:{"content-type":"application/x-www-form-urlencoded"},body:this.stringifyQuery(n)}:Y(this,Je,"f").call(this,{body:n,headers:i})}app(t){return new Mn(t)}};Ta=H,Je=new WeakMap,ka=new WeakSet,ji=function(){return this.baseURL!==zi[this._options.environment||"production"]};H.\u004b\u0065\u0072\u006e\u0065\u006c=Ta;H.DEFAULT_TIMEOUT=6e4;H.\u004b\u0065\u0072\u006e\u0065\u006cError=G;H.APIError=tt;H.APIConnectionError=Nt;H.APIConnectionTimeoutError=wt;H.APIUserAbortError=at;H.NotFoundError=de;H.ConflictError=le;H.RateLimitError=Ue;H.BadRequestError=se;H.AuthenticationError=oe;H.InternalServerError=ue;H.PermissionDeniedError=re;H.UnprocessableEntityError=Se;H.toFile=kn;H.Deployments=Yt;H.Apps=Bt;H.Invocations=Jt;H.Browsers=rt;H.Profiles=qt;H.Auth=Ct;H.Telemetry=Et;H.Proxies=Xt;H.Extensions=Kt;H.BrowserPools=Dt;H.Credentials=Vt;H.Projects=_t;H.Organization=kt;H.AuditLogs=bt;H.APIKeys=Ht;H.CredentialProviders=Gt;var K={container:".eJH8qe.adDDi",dataInitq:"[data-initq]",item:".related-question-pair",itemDataQ:"data-q",itemDataInitQ:"data-initq",itemQuestionEl:".JlqpRe",answerContainer:".bCOlv, .hgKElc, .wDYxhc, .LGOjhe, .fo7IQd, .fmW3u",aiAnswerBody:".pWvJNd",aiCitation:"a.PMDqCb[href]",aiSourceCard:"a.vIWmYe[href], a.Zbfntb[href]",sourceTitle:"h3",sourceSite:".VuuXrf",sourceCite:"cite",clickTarget:".JlqpRe",expandedClass:"aoRk1c",captchaMarker:'#captcha-form, #recaptcha, form[action*="/sorry/"], .g-recaptcha, [data-sitekey]'},$i={container:'div[jscontroller="HWk0Gf"]',sectionHeading:'.mgAbYb[role="heading"]',item:"a.rIRoqf"},fn={udm:"39",item:"a.rIRoqf",durationPattern:/^\d+:\d+$/,platforms:["YouTube","TikTok","Instagram","Facebook","X"]},Qi={section:".ULSxyf",item:"a.KYg7td.INpicf",title:".hyYc0c",source:".K4ETW"},Zi={sectionTag:"g-section-with-header",sectionHeadingText:"What people are saying",card:'.dRzkFf[role="listitem"]',cardLink:'a.WlydOe[jsname="YKoRaf"]',titleH1:"h1.WQWxe",titleDiv:".eAaXgc",popularCommentLabel:".qgdis",source:".sTl1Td",platformBadge:".appd0, .KrMNbf",ytChannel:".sjVJQd",ytDate:".PLq9Je",authorNote:".nDgy9d"},ts={root:"[data-lhcontainer][data-streaming-container][eid]",legacyRoot:'[data-hveid="CBMQAA"]',wrapper:".Fgyi2e",controller:'[jscontroller="AkrxPe"]',contentSubtree:'[data-subtree="mfc"]',header:".heWuVc",heading:".Fzsovc.cwYVJe.RJPOee",showMoreButton:'[aria-label="Show more AI Overview"]',sourcesPanel:".OZ9ddf.WAUd4",disclaimer:".DuQANe.MSJHRb"},es={root:'[data-hveid="CAUQAA"]',wrapper:".Fgyi2e",citations:'.Fgyi2e [data-hveid] a[jsname="pxBnId"]'},ns={result:".wHYlTd.tF2Cxc",title:"h3.LC20lb",siteName:".VuuXrf",cite:"cite.tjvcx",snippet:".VwiC3b",redditCite:"cite.qLRx3b",ratingWrap:".Y0A0hc",ratingValue:".yi40Hd",reviewCount:".RDApEe"},as={headingText:"Businesses",card:".w7Dbne",name:".OSrXXb",ratingValue:".yi40Hd",reviewCount:".RDApEe"};var is="Google returned a CAPTCHA. Run with --headless=false to re-warm the browser profile, then retry.";var td=["request_aborted","captcha_exhausted","captcha_or_blocked","location_mismatch","proxy_tunnel_failed","proxy_unavailable","harvest_timeout","mcp_request_timeout","mcp_http_error","extraction_failed","concurrency_limit_exceeded","unauthorized","forbidden","invalid_request","rate_limited","upstream_rate_limited","insufficient_balance","service_unavailable","response_lost","vendor_unavailable","page_not_found","page_forbidden","page_rate_limited","page_server_error","page_http_error","bot_check_unresolved","page_too_large","page_unreachable","browser_session_interrupted","site_export_not_found","site_export_format_unavailable","site_export_read_failed","site_export_image_not_found","image_url_invalid","image_url_not_https","image_host_unresolvable","image_host_blocked","image_redirect_invalid","image_source_unavailable","image_too_large","image_redirect_limit","image_base64_invalid","image_source_missing","image_source_ambiguous","image_source_type_invalid","image_artifact_not_found","image_page_too_large","page_image_not_found","page_image_candidates_unusable","image_format_unsupported","image_storage_unconfigured","featured_image_unhostable"],ed=["request_aborted","captcha","location_mismatch","connection","timeout","extraction","concurrency_limit","unauthorized","forbidden","invalid_request","rate_limit","billing","service_unavailable"];var Gh=new Set(td),Vh=new Set(ed);function ss(e){return e.replace((new RegExp("https?:\\/\\/[^\\s]*(?:\u0062\u0072\u0069\u0067\u0068\u0074\u0064\u0061\u0074\u0061|on\u006b\u0065\u0072\u006e\u0065\u006c|\u006b\u0065\u0072\u006e\u0065\u006c\\.sh)[^\\s]*", "gi")),"the service").replace(/bright\s*data/gi,"the service").replace((new RegExp("\u0062\u0072\u0069\u0067\u0068\u0074\u0064\u0061\u0074\u0061", "gi")),"the service").replace((new RegExp("\u006b\u0065\u0072\u006e\u0065\u006c\\.sh\\s+sessions?", "gi")),"sessions").replace((new RegExp("\u006b\u0065\u0072\u006e\u0065\u006c\\.sh\\s+session", "gi")),"this session").replace((new RegExp("\u006b\u0065\u0072\u006e\u0065\u006c\\.sh", "gi")),"the service").replace((new RegExp("\u006b\u0065\u0072\u006e\u0065\u006c\\s+sessions?", "gi")),"sessions").replace((new RegExp("\u006b\u0065\u0072\u006e\u0065\u006c\\s+session", "gi")),"this session").replace(/\b(?:sub|cus|sess|proxy)_[A-Za-z0-9_-]+\b/g,"[redacted]").replace((new RegExp("\\b(?:\u004b\u0045\u0052\u004e\u0045\u004c|\u0042\u0052\u0049\u0047\u0048\u0054\u0044\u0041\u0054\u0041|\u0042\u0052\u0049\u0047\u0048\u0054_\u0044\u0041\u0054\u0041)_[A-Z0-9_]*API_KEY\\b", "g")),"BROWSER_SERVICE_API_KEY").replace((new RegExp("\\b\u006b\u0065\u0072\u006e\u0065\u006c\\b", "gi")),"the service").replace(/ +/g," ").trim()}var nt=class extends Error{constructor(n){super(`CAPTCHA detected. ${n}`);this.instructions=n}instructions;name="CaptchaError"},Ee=class extends Error{constructor(n,a){super(n);this.cause=a}cause;name="ExtractionError"};var ut=class extends Error{name="RequestAbortedError";constructor(t="Request aborted before harvest completed"){super(t)}},jt=class extends Error{name="PaaInteractionInterruptedError"},qe=class extends Error{name="LocationMismatchError";constructor(t="Google returned results for a different location than requested"){super(t)}};async function Nn(e,t,n,a){let i,s=!1,o,d=()=>{},l=()=>a?.reason instanceof DOMException&&a.reason.name==="TimeoutError"?a.reason:new ut,U=()=>d(l()),u=new Promise((S,c)=>{d=c}),r=async S=>{let c;try{await Promise.race([S.close().catch(()=>{}),new Promise(h=>{c=setTimeout(h,2e3)})])}finally{c&&clearTimeout(c)}};if(a?.aborted)throw l();a?.addEventListener("abort",U,{once:!0}),o=setTimeout(()=>d(new DOMException("Optional pagination timed out","TimeoutError")),3e4);try{return await Promise.race([(async()=>{let S=await e.newPage();if(s)throw await r(S),new ut;if(i=S,await t(S),s)throw new ut;return n(S)})(),u])}finally{s=!0,o&&clearTimeout(o),a?.removeEventListener("abort",U),i&&await r(i)}}var va=require("crypto");var os=require("@libsql/client/http"),Xe=require("crypto"),j=require("zod");var Tt="strftime('%Y-%m-%dT%H:%M:%fZ','now')";var nd=[`CREATE TABLE IF NOT EXISTS oauth_store_metadata (
11
11
  id INTEGER PRIMARY KEY CHECK (id = 1),
12
12
  store_id TEXT NOT NULL UNIQUE,
13
13
  schema_version INTEGER NOT NULL CHECK (schema_version > 0),
@@ -106,9 +106,9 @@ ${r}`)}return o},g=or(Vi);var Ht=class extends b{create(t,n){return this._client
106
106
  reason_code TEXT NOT NULL,
107
107
  occurred_at TEXT NOT NULL DEFAULT (${Tt}),
108
108
  retain_until TEXT NOT NULL
109
- ) STRICT`,"CREATE INDEX IF NOT EXISTS oauth_clients_inactivity_idx ON oauth_clients(status, last_used_at, client_id)","CREATE INDEX IF NOT EXISTS oauth_codes_expiry_idx ON oauth_codes(expires_at, code_digest)","CREATE INDEX IF NOT EXISTS oauth_code_exchanges_replay_expiry_idx ON oauth_code_exchanges(replay_expires_at, code_digest)","CREATE INDEX IF NOT EXISTS oauth_refresh_families_lifecycle_idx ON oauth_refresh_families(status, expires_at, family_id)","CREATE INDEX IF NOT EXISTS oauth_refresh_families_user_status_idx ON oauth_refresh_families(user_id, status)","CREATE INDEX IF NOT EXISTS oauth_refresh_tokens_expiry_idx ON oauth_refresh_tokens(expires_at, family_id, generation)","CREATE INDEX IF NOT EXISTS oauth_refresh_rotations_replay_expiry_idx ON oauth_refresh_rotations(replay_expires_at, family_id, predecessor_generation)","CREATE INDEX IF NOT EXISTS oauth_security_events_retention_idx ON oauth_security_events(retain_until, event_id)"],nd=["CREATE INDEX IF NOT EXISTS oauth_clients_cleanup_idx ON oauth_clients(COALESCE(last_used_at,created_at), client_id)","CREATE INDEX IF NOT EXISTS oauth_codes_client_idx ON oauth_codes(client_id, code_digest)","CREATE INDEX IF NOT EXISTS oauth_refresh_families_client_idx ON oauth_refresh_families(client_id, family_id)",`CREATE INDEX IF NOT EXISTS oauth_refresh_families_cleanup_idx
109
+ ) STRICT`,"CREATE INDEX IF NOT EXISTS oauth_clients_inactivity_idx ON oauth_clients(status, last_used_at, client_id)","CREATE INDEX IF NOT EXISTS oauth_codes_expiry_idx ON oauth_codes(expires_at, code_digest)","CREATE INDEX IF NOT EXISTS oauth_code_exchanges_replay_expiry_idx ON oauth_code_exchanges(replay_expires_at, code_digest)","CREATE INDEX IF NOT EXISTS oauth_refresh_families_lifecycle_idx ON oauth_refresh_families(status, expires_at, family_id)","CREATE INDEX IF NOT EXISTS oauth_refresh_families_user_status_idx ON oauth_refresh_families(user_id, status)","CREATE INDEX IF NOT EXISTS oauth_refresh_tokens_expiry_idx ON oauth_refresh_tokens(expires_at, family_id, generation)","CREATE INDEX IF NOT EXISTS oauth_refresh_rotations_replay_expiry_idx ON oauth_refresh_rotations(replay_expires_at, family_id, predecessor_generation)","CREATE INDEX IF NOT EXISTS oauth_security_events_retention_idx ON oauth_security_events(retain_until, event_id)"],ad=["CREATE INDEX IF NOT EXISTS oauth_clients_cleanup_idx ON oauth_clients(COALESCE(last_used_at,created_at), client_id)","CREATE INDEX IF NOT EXISTS oauth_codes_client_idx ON oauth_codes(client_id, code_digest)","CREATE INDEX IF NOT EXISTS oauth_refresh_families_client_idx ON oauth_refresh_families(client_id, family_id)",`CREATE INDEX IF NOT EXISTS oauth_refresh_families_cleanup_idx
110
110
  ON oauth_refresh_families(COALESCE(compromised_at,revoked_at,expires_at), family_id)
111
- WHERE status!='active'`],ad=[`CREATE TABLE IF NOT EXISTS oauth_rate_limit_buckets (
111
+ WHERE status!='active'`],id=[`CREATE TABLE IF NOT EXISTS oauth_rate_limit_buckets (
112
112
  scope TEXT NOT NULL CHECK (length(scope) BETWEEN 1 AND 80),
113
113
  key_digest BLOB NOT NULL CHECK (typeof(key_digest) = 'blob' AND length(key_digest) = 32),
114
114
  window_start INTEGER NOT NULL CHECK (window_start >= 0),
@@ -118,7 +118,7 @@ ${r}`)}return o},g=or(Vi);var Ht=class extends b{create(t,n){return this._client
118
118
  updated_at TEXT NOT NULL DEFAULT (${Tt}),
119
119
  PRIMARY KEY (scope, key_digest, window_start, window_seconds)
120
120
  ) STRICT`,`CREATE INDEX IF NOT EXISTS oauth_rate_limit_buckets_expiry_idx
121
- ON oauth_rate_limit_buckets(expires_at, scope, key_digest, window_start, window_seconds)`],Jh=[...ed,...nd,...ad];var sd=process.env.TURSO_DATABASE_URL??"file:./paa-api.db",od=process.env.TURSO_AUTH_TOKEN,Ta=null;function vn(){return Ta||(Ta=(0,os.createClient)({url:sd,authToken:od})),Ta}var eg=32*1024;var ng=j.z.object({id:j.z.string(),user_id:j.z.number(),status:j.z.string().default("pending"),client_domain:j.z.string(),session_path:j.z.string(),request:j.z.string(),result:j.z.string().nullable(),error:j.z.string().nullable(),created_at:j.z.string(),updated_at:j.z.string()}),ag=j.z.object({id:j.z.number(),job_id:j.z.string(),phase:j.z.string(),completed_at:j.z.string(),output_summary:j.z.string()});var rs=require("async_hooks"),ds="MCP_SCRAPER_ALLOW_HEADLESS_HARVEST_TEST",ls=new rs.AsyncLocalStorage;function Ss(e,t){return ls.run(e,t)}function zt(){return ls.getStore()}var rd=166667e-10,dd=.0001333336,Us=new Set(["serp","fb_search","fb_ad","instagram"]),ld=.00111,Sd=4,Ud=4e-4;function je(e,t){let n=process.env[e]?.trim();if(!n)return t;let a=Number(n);return Number.isFinite(a)&&a>=0?a:t}var ud=je("NANGO_USD_PER_CONNECTION_MONTH",1),cd=je("NANGO_USD_PER_FUNCTION_RUN",1e-4),hd=je("NANGO_USD_PER_PROXY_REQUEST",1e-4),gd=je("NANGO_USD_PER_COMPUTE_SEC",2e-4),pd=je("\u0042\u0052\u0049\u0047\u0048\u0054\u0044\u0041\u0054\u0041_BROWSER_USD_PER_GB",8);function Ma(e,t){return Math.max(0,e)/1e3*(t?dd:rd)}function us(e,t){return e==="fal_wizper"?Math.max(0,t)/Sd*ld:e==="deepinfra_qwen"?Math.max(0,t)/1e3*Ud:e==="openrouter"||e==="mcp_memory_video"||e==="mcp_memory_ai"?Math.max(0,t):e==="nango_connection"?Math.max(0,t)*ud:e==="nango_function_run"?Math.max(0,t)*cd:e==="nango_proxy_request"?Math.max(0,t)*hd:e==="nango_compute"?Math.max(0,t)*gd:e==="\u0062\u0072\u0069\u0067\u0068\u0074\u0064\u0061\u0074\u0061_browser_api"?Math.max(0,t)/1e9*pd:0}var fa=!1,ze=null;async function cs(){if(!fa)return ze||(ze=wd().finally(()=>{ze=null}),ze)}async function wd(){let e=vn(),t=await e.execute(`
121
+ ON oauth_rate_limit_buckets(expires_at, scope, key_digest, window_start, window_seconds)`],Xh=[...nd,...ad,...id];var od=process.env.TURSO_DATABASE_URL??"file:./paa-api.db",rd=process.env.TURSO_AUTH_TOKEN,Ma=null;function vn(){return Ma||(Ma=(0,os.createClient)({url:od,authToken:rd})),Ma}var ag=32*1024;var ig=j.z.object({id:j.z.string(),user_id:j.z.number(),status:j.z.string().default("pending"),client_domain:j.z.string(),session_path:j.z.string(),request:j.z.string(),result:j.z.string().nullable(),error:j.z.string().nullable(),created_at:j.z.string(),updated_at:j.z.string()}),sg=j.z.object({id:j.z.number(),job_id:j.z.string(),phase:j.z.string(),completed_at:j.z.string(),output_summary:j.z.string()});var rs=require("async_hooks"),ds="MCP_SCRAPER_ALLOW_HEADLESS_HARVEST_TEST",ls=new rs.AsyncLocalStorage;function Ss(e,t){return ls.run(e,t)}function zt(){return ls.getStore()}var dd=166667e-10,ld=.0001333336,Us=new Set(["serp","fb_search","fb_ad","instagram"]),Sd=.00111,Ud=4,ud=4e-4;function je(e,t){let n=process.env[e]?.trim();if(!n)return t;let a=Number(n);return Number.isFinite(a)&&a>=0?a:t}var cd=je("NANGO_USD_PER_CONNECTION_MONTH",1),hd=je("NANGO_USD_PER_FUNCTION_RUN",1e-4),gd=je("NANGO_USD_PER_PROXY_REQUEST",1e-4),pd=je("NANGO_USD_PER_COMPUTE_SEC",2e-4),wd=je("\u0042\u0052\u0049\u0047\u0048\u0054\u0044\u0041\u0054\u0041_BROWSER_USD_PER_GB",8);function fa(e,t){return Math.max(0,e)/1e3*(t?ld:dd)}function us(e,t){return e==="fal_wizper"?Math.max(0,t)/Ud*Sd:e==="deepinfra_qwen"?Math.max(0,t)/1e3*ud:e==="openrouter"||e==="mcp_memory_video"||e==="mcp_memory_ai"?Math.max(0,t):e==="nango_connection"?Math.max(0,t)*cd:e==="nango_function_run"?Math.max(0,t)*hd:e==="nango_proxy_request"?Math.max(0,t)*gd:e==="nango_compute"?Math.max(0,t)*pd:e==="\u0062\u0072\u0069\u0067\u0068\u0074\u0064\u0061\u0074\u0061_browser_api"?Math.max(0,t)/1e9*wd:0}var Na=!1,ze=null;async function cs(){if(!Na)return ze||(ze=md().finally(()=>{ze=null}),ze)}async function md(){let e=vn(),t=await e.execute(`
122
122
  SELECT
123
123
  (SELECT COUNT(*) FROM sqlite_master WHERE type = 'table' AND name IN ('\u006b\u0065\u0072\u006e\u0065\u006c_session_log', 'vendor_usage_log', 'cost_probe_runs')) = 3
124
124
  AND (SELECT COUNT(*) FROM pragma_table_info('\u006b\u0065\u0072\u006e\u0065\u006c_session_log') WHERE name IN ('proxy_source', 'proxy_type', 'method')) = 3
@@ -126,7 +126,7 @@ ${r}`)}return o},g=or(Vi);var Ht=class extends b{create(t,n){return this._client
126
126
  AND (SELECT COUNT(*) FROM sqlite_master WHERE type = 'index' AND name = 'vendor_usage_log_vendor_source_key') = 1
127
127
  AND (SELECT COUNT(*) FROM pragma_table_info('cost_probe_runs') WHERE name IN ('units', 'unit_type', 'mode')) = 3
128
128
  AS ready
129
- `);if(Number(t.rows[0]?.ready??0)===1){fa=!0;return}await e.execute(`
129
+ `);if(Number(t.rows[0]?.ready??0)===1){Na=!0;return}await e.execute(`
130
130
  CREATE TABLE IF NOT EXISTS \u006b\u0065\u0072\u006e\u0065\u006c_session_log (
131
131
  id TEXT PRIMARY KEY,
132
132
  \u006b\u0065\u0072\u006e\u0065\u006c_session_id TEXT,
@@ -186,11 +186,11 @@ ${r}`)}return o},g=or(Vi);var Ht=class extends b{create(t,n){return this._client
186
186
  margin_usd_headful_starter REAL NOT NULL DEFAULT 0,
187
187
  notes TEXT
188
188
  )
189
- `),await e.execute("CREATE INDEX IF NOT EXISTS cost_probe_runs_tool ON cost_probe_runs(tool)");try{await e.execute("ALTER TABLE cost_probe_runs ADD COLUMN units REAL")}catch{}try{await e.execute("ALTER TABLE cost_probe_runs ADD COLUMN unit_type TEXT")}catch{}try{await e.execute("ALTER TABLE cost_probe_runs ADD COLUMN mode TEXT")}catch{}fa=!0}async function hs(e){try{await cs();let t=zt(),n=Math.max(0,e.closedAtMs-e.openedAtMs);await vn().execute({sql:`INSERT INTO \u006b\u0065\u0072\u006e\u0065\u006c_session_log
189
+ `),await e.execute("CREATE INDEX IF NOT EXISTS cost_probe_runs_tool ON cost_probe_runs(tool)");try{await e.execute("ALTER TABLE cost_probe_runs ADD COLUMN units REAL")}catch{}try{await e.execute("ALTER TABLE cost_probe_runs ADD COLUMN unit_type TEXT")}catch{}try{await e.execute("ALTER TABLE cost_probe_runs ADD COLUMN mode TEXT")}catch{}Na=!0}async function hs(e){try{await cs();let t=zt(),n=Math.max(0,e.closedAtMs-e.openedAtMs);await vn().execute({sql:`INSERT INTO \u006b\u0065\u0072\u006e\u0065\u006c_session_log
190
190
  (id, \u006b\u0065\u0072\u006e\u0065\u006c_session_id, op, source, probe_run_id, user_id, stealth, headless_sent, proxy_used, proxy_source, proxy_type, fallback, opened_at, closed_at, duration_ms, est_cost_usd_headless, est_cost_usd_headful, error, method)
191
- VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`,args:[(0,Na.randomUUID)(),e.\u006b\u0065\u0072\u006e\u0065\u006cSessionId??null,t?.op??null,e.source,t?.probeRunId??null,t?.userId??null,bn(e.stealth),bn(e.headlessSent),bn(e.proxyUsed),e.proxySource??null,e.proxyType??null,e.fallback?1:0,new Date(e.openedAtMs).toISOString(),new Date(e.closedAtMs).toISOString(),n,Ma(n,!1),Ma(n,!0),e.error??null,t?.subOp??null]})}catch(t){console.warn("[cost-telemetry] record\u004b\u0065\u0072\u006e\u0065\u006cSession failed:",t instanceof Error?t.message:String(t))}}async function va(e){try{await cs();let t=zt();return(await vn().execute({sql:`INSERT OR IGNORE INTO vendor_usage_log
191
+ VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`,args:[(0,va.randomUUID)(),e.\u006b\u0065\u0072\u006e\u0065\u006cSessionId??null,t?.op??null,e.source,t?.probeRunId??null,t?.userId??null,bn(e.stealth),bn(e.headlessSent),bn(e.proxyUsed),e.proxySource??null,e.proxyType??null,e.fallback?1:0,new Date(e.openedAtMs).toISOString(),new Date(e.closedAtMs).toISOString(),n,fa(n,!1),fa(n,!0),e.error??null,t?.subOp??null]})}catch(t){console.warn("[cost-telemetry] record\u004b\u0065\u0072\u006e\u0065\u006cSession failed:",t instanceof Error?t.message:String(t))}}async function ba(e){try{await cs();let t=zt();return(await vn().execute({sql:`INSERT OR IGNORE INTO vendor_usage_log
192
192
  (id, op, probe_run_id, user_id, vendor, model, units, unit_type, est_cost_usd, error, method, source_key, provider_duration_ms, provider_captcha, provider_status)
193
- VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`,args:[(0,Na.randomUUID)(),e.op??t?.op??null,e.probeRunId??t?.probeRunId??null,e.userId??t?.userId??null,e.vendor,e.model??null,e.units,e.unitType,us(e.vendor,e.units),e.error??null,e.method??t?.subOp??null,e.sourceKey??null,e.providerDurationMs??null,bn(e.providerCaptcha),e.providerStatus??null]})).rowsAffected===1}catch(t){return console.warn("[cost-telemetry] recordVendorUsage failed:",t instanceof Error?t.message:String(t)),!1}}function bn(e){return e==null?null:e?1:0}var md=/\b(?:HeadlessChrome|Chrome|Chromium|Firefox)\/\d+|\bVersion\/\d+.*\bSafari\//;async function _n(e){try{let t=await e.evaluate(()=>navigator.userAgent);return typeof t=="string"&&md.test(t)?t:null}catch{return null}}async function En(e){let t=null;try{let n=e.contexts().flatMap(a=>a.pages())[0];return n||(t=await e.newContext(),n=t.pages()[0]??await t.newPage()),await _n(n)}catch{return null}finally{t&&await t.close().catch(()=>{})}}In.chromium.use((0,ps.default)());var yd=180,Cd=3e3,kd=5e3,Td='#search, #rso, a[href] h3, [data-snhf], [role="main"]';function ba(e,t){return e?Math.max(1,Math.min(t,e-Date.now())):t}function Md(e,t){let n=process.env[e];if(!n)return t;let a=Number(n);return Number.isInteger(a)&&a>0?a:t}function _a(e){return e?e.slice(-6):null}function gs(e,t){let n=(t??process.env.SERP_REDIRECT_BASE)?.trim().replace(/\/+$/,"");return n?`${n}/g/${Buffer.from(e,"utf8").toString("base64url")}`:e}function fd(e){return e?/united states|u\.?s\.?a?\.?|^us$/i.test(e.trim()):!1}function $e(e){return e instanceof Error?e.message:String(e)}function Nd(e){return/\b409\b|conflict|already exists/i.test($e(e))}async function vd(e,t){try{await e.profiles.create({name:t})}catch(n){if(!Nd(n))throw n}}function Pn(e,t){return{viewport:e.viewport,locale:e.locale,...t?{userAgent:t}:{},...e.deviceScaleFactor?{deviceScaleFactor:e.deviceScaleFactor}:{},...e.isMobile!==void 0?{isMobile:e.isMobile}:{},...e.hasTouch!==void 0?{hasTouch:e.hasTouch}:{}}}async function Pe(e,t,n){let a;try{return await Promise.race([e,new Promise((i,s)=>{a=setTimeout(()=>s(new Error(`${n} timed out after ${t}ms`)),t)})])}finally{a&&clearTimeout(a)}}function bd(e){let t=e.trim();if(!t)throw new Error("channelHandle is required");if(/^https?:\/\//i.test(t)||/^(www\.|m\.)?youtube\.com\//i.test(t)){let o=new URL(/^https?:\/\//i.test(t)?t:`https://${t}`);if(o.hostname.replace(/^www\./,"").replace(/^m\./,"").toLowerCase()!=="youtube.com")throw new Error("channel URL must be on youtube.com");let l=o.pathname.split("/").filter(Boolean),U=l[0]??"",u=l[1]??"";if(U.startsWith("@"))return`https://www.youtube.com/${U}/videos`;if(U==="channel"&&u)return`https://www.youtube.com/channel/${u}/videos`;if((U==="c"||U==="user")&&u)return`https://www.youtube.com/${U}/${u}/videos`;throw new Error("channel URL must be a YouTube handle, /channel/UC..., /c/..., or /user/... URL")}let i=t.replace(/^\/+/,"").replace(/\/+$/,"").replace(/\/videos$/i,"");if(/^UC[\w-]{20,}$/.test(i))return`https://www.youtube.com/channel/${i}/videos`;let s=i.startsWith("@")?i:`@${i}`;if(!/^@[\w.-]+$/.test(s))throw new Error("channelHandle must be an @handle, UC channel ID, or YouTube channel URL");return`https://www.youtube.com/${s}/videos`}var An=class{browser=null;context=null;page=null;\u006b\u0065\u0072\u006e\u0065\u006cClient=null;\u006b\u0065\u0072\u006e\u0065\u006cSessionId=null;\u006b\u0065\u0072\u006e\u0065\u006cOpenedAtMs=null;\u006b\u0065\u0072\u006e\u0065\u006cStealth=null;\u006b\u0065\u0072\u006e\u0065\u006cProxyUsed=null;\u006b\u0065\u0072\u006e\u0065\u006cProxySource=null;\u006b\u0065\u0072\u006e\u0065\u006cProxyType=null;\u006b\u0065\u0072\u006e\u0065\u006cHeadlessSent=null;debugEnabled=!1;debugSnapshot={\u006b\u0065\u0072\u006e\u0065\u006c:null,context:null,networkLocation:null,serpNavigation:null};async launch(t){this.debugEnabled=t.debug===!0;let n=t.proxyMode??(t.\u006b\u0065\u0072\u006e\u0065\u006cProxyId?"configured":"none"),a=t.isMobile?"mobile":"desktop";if(this.debugSnapshot={\u006b\u0065\u0072\u006e\u0065\u006c:null,context:{viewport:t.viewport,locale:t.locale,device:a,userAgent:null,deviceScaleFactor:t.deviceScaleFactor??null,isMobile:t.isMobile===!0,hasTouch:t.hasTouch===!0},networkLocation:null,serpNavigation:null},t.\u006b\u0065\u0072\u006e\u0065\u006cApiKey){this.\u006b\u0065\u0072\u006e\u0065\u006cClient=new H({apiKey:t.\u006b\u0065\u0072\u006e\u0065\u006cApiKey});let s=Number.isInteger(t.\u006b\u0065\u0072\u006e\u0065\u006cTimeoutSeconds)&&Number(t.\u006b\u0065\u0072\u006e\u0065\u006cTimeoutSeconds)>0?Number(t.\u006b\u0065\u0072\u006e\u0065\u006cTimeoutSeconds):Md("\u004b\u0045\u0052\u004e\u0045\u004c_BROWSER_TIMEOUT_SECONDS",yd),o=Math.min(s,600);t.\u006b\u0065\u0072\u006e\u0065\u006cProfileName&&t.\u006b\u0065\u0072\u006e\u0065\u006cProfileSaveChanges===!0&&await vd(this.\u006b\u0065\u0072\u006e\u0065\u006cClient,t.\u006b\u0065\u0072\u006e\u0065\u006cProfileName);let d=zt(),l;t.headlessMode==="headful"||d?.forceHeadful===!0?l=!1:t.headlessMode==="headless"?l=!0:l=d?.forceHeadless===!0||process.env.\u004b\u0045\u0052\u004e\u0045\u004c_HEADLESS_DISABLED!=="true"&&!!d?.op&&Us.has(d.op),this.\u006b\u0065\u0072\u006e\u0065\u006cHeadlessSent=l?!0:null,d?.headlessSentOut&&(d.headlessSentOut.value=this.\u006b\u0065\u0072\u006e\u0065\u006cHeadlessSent);let U=await this.\u006b\u0065\u0072\u006e\u0065\u006cClient.browsers.create({stealth:t.\u006b\u0065\u0072\u006e\u0065\u006cStealth===!0,timeout_seconds:o,...l?{headless:!0}:{},...t.\u006b\u0065\u0072\u006e\u0065\u006cProxyId?{proxy_id:t.\u006b\u0065\u0072\u006e\u0065\u006cProxyId}:{},...t.\u006b\u0065\u0072\u006e\u0065\u006cProfileName?{profile:{name:t.\u006b\u0065\u0072\u006e\u0065\u006cProfileName,...typeof t.\u006b\u0065\u0072\u006e\u0065\u006cProfileSaveChanges=="boolean"?{save_changes:t.\u006b\u0065\u0072\u006e\u0065\u006cProfileSaveChanges}:{}}}:{}});this.\u006b\u0065\u0072\u006e\u0065\u006cSessionId=U.session_id,this.\u006b\u0065\u0072\u006e\u0065\u006cOpenedAtMs=Date.now(),this.\u006b\u0065\u0072\u006e\u0065\u006cStealth=typeof U.stealth=="boolean"?U.stealth:null,this.\u006b\u0065\u0072\u006e\u0065\u006cProxyUsed=typeof U.proxy_id=="string"?!!U.proxy_id:!!t.\u006b\u0065\u0072\u006e\u0065\u006cProxyId,this.\u006b\u0065\u0072\u006e\u0065\u006cProxySource=t.\u006b\u0065\u0072\u006e\u0065\u006cProxyResolution?.source??null,this.\u006b\u0065\u0072\u006e\u0065\u006cProxyType=t.\u006b\u0065\u0072\u006e\u0065\u006cProxyResolution?.proxyType??null;let u=null,r=null;if(!t.\u006b\u0065\u0072\u006e\u0065\u006cProxyId&&!t.keepDefaultProxy)try{await Pe(this.\u006b\u0065\u0072\u006e\u0065\u006cClient.browsers.update(this.\u006b\u0065\u0072\u006e\u0065\u006cSessionId,{disable_default_proxy:!0}),5e3,`\u004b\u0065\u0072\u006e\u0065\u006c session ${this.\u006b\u0065\u0072\u006e\u0065\u006cSessionId} disable default proxy`),u=!0}catch(c){u=!1,r=$e(c)}let S={sessionId:this.\u006b\u0065\u0072\u006e\u0065\u006cSessionId,proxyMode:n,requestedProxyIdPresent:!!t.\u006b\u0065\u0072\u006e\u0065\u006cProxyId,requestedProxyIdSuffix:_a(t.\u006b\u0065\u0072\u006e\u0065\u006cProxyId),createdProxyIdPresent:typeof U.proxy_id=="string"?!!U.proxy_id:null,createdProxyIdSuffix:_a(U.proxy_id),retrievedProxyIdPresent:null,retrievedProxyIdSuffix:null,retrievedProxyIdMatchesRequested:null,defaultProxyDisabled:u,defaultProxyDisableError:r,proxyResolution:t.\u006b\u0065\u0072\u006e\u0065\u006cProxyResolution??null,timeoutSeconds:o,stealth:typeof U.stealth=="boolean"?U.stealth:null,profilePresent:null,poolPresent:null,retrieveError:null};if(this.debugSnapshot.\u006b\u0065\u0072\u006e\u0065\u006c=S,console.info(JSON.stringify({event:"\u006b\u0065\u0072\u006e\u0065\u006c_browser_created",\u006b\u0065\u0072\u006e\u0065\u006c_session_id:this.\u006b\u0065\u0072\u006e\u0065\u006cSessionId,timeout_seconds:o,proxy_mode:n,proxy_id_present:!!t.\u006b\u0065\u0072\u006e\u0065\u006cProxyId,profile_name_present:!!t.\u006b\u0065\u0072\u006e\u0065\u006cProfileName,proxy_resolution_source:t.\u006b\u0065\u0072\u006e\u0065\u006cProxyResolution?.source})),this.debugEnabled&&await this.populate\u004b\u0065\u0072\u006e\u0065\u006cRetrieveDebug(S,t.\u006b\u0065\u0072\u006e\u0065\u006cProxyId),this.browser=await ws.chromium.connectOverCDP(U.cdp_ws_url),t.\u006b\u0065\u0072\u006e\u0065\u006cProfileName||t.preserve\u004b\u0065\u0072\u006e\u0065\u006cContext)this.context=this.browser.contexts()[0]??await this.browser.newContext(Pn(t)),this.page=this.context.pages()[0]??await this.context.newPage(),this.debugSnapshot.context.userAgent=await _n(this.page);else{let c=await En(this.browser);this.debugSnapshot.context.userAgent=c,this.context=await this.browser.newContext(Pn(t,c)),this.page=await this.context.newPage()}if(await this.installEsbuildHelperShims(this.context),t.preserve\u004b\u0065\u0072\u006e\u0065\u006cContext||await this.page.setViewportSize(t.viewport),(this.debugEnabled||t.requireUsEgress)&&(this.debugSnapshot.networkLocation=await this.captureBrowserNetworkLocation()),t.requireUsEgress){let c=this.debugSnapshot.networkLocation?.country??null;if(!fd(c))throw new nt(`Non-US egress (${c??"unknown"}) \u2014 retrying with a fresh session`)}return}let i={headless:t.headless,proxy:t.proxy?{server:t.proxy}:void 0,...t.browserExecutablePath?{executablePath:t.browserExecutablePath}:{},...t.ignoreDefaultArgs?{ignoreDefaultArgs:t.ignoreDefaultArgs}:{}};if(t.profileDir)this.context=await In.chromium.launchPersistentContext(t.profileDir,{...i,...Pn(t)}),await this.installEsbuildHelperShims(this.context),this.page=await this.context.newPage(),this.debugSnapshot.context.userAgent=await _n(this.page);else{this.browser=await In.chromium.launch(i);let s=await En(this.browser);this.debugSnapshot.context.userAgent=s,this.context=await this.browser.newContext(Pn(t,s)),await this.installEsbuildHelperShims(this.context),this.page=await this.context.newPage()}this.debugEnabled&&(this.debugSnapshot.networkLocation=await this.captureBrowserNetworkLocation())}async populate\u004b\u0065\u0072\u006e\u0065\u006cRetrieveDebug(t,n){if(!(!this.\u006b\u0065\u0072\u006e\u0065\u006cClient||!this.\u006b\u0065\u0072\u006e\u0065\u006cSessionId))try{let a=await Pe(this.\u006b\u0065\u0072\u006e\u0065\u006cClient.browsers.retrieve(this.\u006b\u0065\u0072\u006e\u0065\u006cSessionId),5e3,`\u004b\u0065\u0072\u006e\u0065\u006c session ${this.\u006b\u0065\u0072\u006e\u0065\u006cSessionId} retrieve`);t.retrievedProxyIdPresent=typeof a.proxy_id=="string"?!!a.proxy_id:!1,t.retrievedProxyIdSuffix=_a(a.proxy_id),t.retrievedProxyIdMatchesRequested=n?a.proxy_id===n:!a.proxy_id,t.timeoutSeconds=typeof a.timeout_seconds=="number"?a.timeout_seconds:t.timeoutSeconds,t.stealth=typeof a.stealth=="boolean"?a.stealth:t.stealth,t.profilePresent=!!a.profile,t.poolPresent=!!a.pool}catch(a){t.retrieveError=$e(a)}}async captureBrowserNetworkLocation(){let t=(a,i="ipapi.co")=>({source:i,ip:null,city:null,region:null,country:null,org:null,timezone:null,error:a});if(!this.context)return t("browser context is not available");let n=null;try{n=await this.context.newPage();let a=await this.loadJsonInDebugPage(n,"https://ipwho.is/");if(a){let d=typeof a.connection=="object"&&a.connection!==null?a.connection:{};return{source:"ipwho.is",ip:typeof a.ip=="string"?a.ip:null,city:typeof a.city=="string"?a.city:null,region:typeof a.region=="string"?a.region:null,country:typeof a.country=="string"?a.country:null,org:typeof d.org=="string"?d.org:null,timezone:typeof a.timezone=="object"&&a.timezone!==null&&typeof a.timezone.id=="string"?a.timezone.id:null,error:null}}let i=await this.loadJsonInDebugPage(n,"https://api64.ipify.org?format=json");if(i)return{source:"api64.ipify.org",ip:typeof i.ip=="string"?i.ip:null,city:null,region:null,country:null,org:null,timezone:null,error:null};await Pe(n.goto("https://ipapi.co/json/",{waitUntil:"domcontentloaded",timeout:7e3}),8e3,"browser network location navigation");let s=await n.locator("body").innerText({timeout:2e3}),o=JSON.parse(s);return{source:"ipapi.co",ip:typeof o.ip=="string"?o.ip:null,city:typeof o.city=="string"?o.city:null,region:typeof o.region=="string"?o.region:null,country:typeof o.country_name=="string"?o.country_name:typeof o.country=="string"?o.country:null,org:typeof o.org=="string"?o.org:null,timezone:typeof o.timezone=="string"?o.timezone:null,error:null}}catch(a){return t($e(a))}finally{await n?.close().catch(()=>{})}}async loadJsonInDebugPage(t,n){try{await Pe(t.goto(n,{waitUntil:"domcontentloaded",timeout:7e3}),8e3,`browser network location navigation ${n}`);let a=await t.locator("body").innerText({timeout:2e3});return JSON.parse(a)}catch{return null}}async installEsbuildHelperShims(t){await t.addInitScript(()=>{let n=globalThis;typeof n.__name!="function"&&(n.__name=a=>a),typeof n.__publicField!="function"&&(n.__publicField=(a,i,s)=>(a[i]=s,s))})}async withTemporaryPage(t,n){if(!this.context)throw new Error("Browser context is not available");return Nn(this.context,async()=>{},t,n)}async navigateToSERP(t,n,a,i,s){let o=new URLSearchParams({q:t,gl:a,hl:i,pws:"0"});s?.tbs&&o.set("tbs",s.tbs),n&&o.set("uule",n);let d="https://www.google.com/search?"+o.toString(),l=gs(d),U=s?.debug?{requestedUrl:d,finalUrl:null,title:null,bodySnippet:null,hasPaa:null,captchaDetected:null,googleSorryUrl:null,redirected:null}:null;U&&(this.debugSnapshot.serpNavigation=U);try{await this.page.goto(l,{waitUntil:"domcontentloaded",timeout:ba(s?.deadlineMs,s?.purpose==="serp"?8e3:45e3)})}catch(r){await this.updateSerpNavigationDebug(U,d,{hasPaa:null,captchaDetected:null});let S=await this.captureDiagnostics(d);throw new Ee(`page.goto failed: ${r.message} | ${S}`)}if(await this.isSerpChallengeVisible())throw await this.updateSerpNavigationDebug(U,d,{hasPaa:!1,captchaDetected:!0}),new nt(this.captchaMessage());if(s?.purpose==="serp"){await this.page.waitForSelector(Td,{timeout:ba(s.deadlineMs,1500)}).catch(()=>null);let r=await this.page.locator(K.item).count().catch(()=>0)>0;return await this.updateSerpNavigationDebug(U,d,{hasPaa:r,captchaDetected:!1}),{hasPaa:r}}if(await this.page.waitForSelector(K.item,{timeout:ba(s?.deadlineMs,4e3)}).catch(()=>null))return await this.updateSerpNavigationDebug(U,d,{hasPaa:!0,captchaDetected:!1}),{hasPaa:!0};if(await this.isSerpChallengeVisible())throw await this.updateSerpNavigationDebug(U,d,{hasPaa:!1,captchaDetected:!0}),new nt(this.captchaMessage());for(let r=1;r<=6;r++)if(await this.page.evaluate(c=>{window.scrollTo(0,document.body.scrollHeight*c)},r/6),await this.page.waitForTimeout(600),await this.page.locator(K.item).count()>0)return await this.updateSerpNavigationDebug(U,d,{hasPaa:!0,captchaDetected:!1}),{hasPaa:!0};return await this.updateSerpNavigationDebug(U,d,{hasPaa:!1,captchaDetected:!1}),{hasPaa:!1}}async isSerpChallengeVisible(){if(!this.page)return!1;if(await this.page.locator(K.captchaMarker).count().catch(()=>0)>0)return!0;let n=this.page.url();if(/google\.[^/]+\/sorry\//i.test(n))return!0;let a=await this.page.evaluate(()=>(document.body?.innerText??"").replace(/\s+/g," ").trim().slice(0,1e3)).catch(()=>"");return/recaptcha|unusual traffic|are you a robot|about this page|detected unusual traffic/i.test(a)}async updateSerpNavigationDebug(t,n,a){if(!(!t||!this.page))try{let i=this.page.url(),s=await this.page.title().catch(()=>""),o=await this.page.evaluate(()=>(document.body?.innerText??"").replace(/\s+/g," ").trim().slice(0,500)).catch(()=>""),d=/recaptcha|unusual traffic|are you a robot/i.test(o);t.finalUrl=i,t.title=s,t.bodySnippet=o,t.hasPaa=a.hasPaa,t.captchaDetected=a.captchaDetected??d,t.googleSorryUrl=/google\.[^/]+\/sorry\//i.test(i),t.redirected=i!==n}catch(i){t.bodySnippet=`debug capture failed: ${$e(i)}`}}async captureDiagnostics(t){try{let n=this.page.url(),a=await this.page.title().catch(()=>""),i=await this.page.evaluate(()=>(document.body?.innerText??"").replace(/\s+/g," ").trim().slice(0,400)).catch(()=>""),s=/consent\.google\./.test(n)||/before you continue/i.test(i),o=/recaptcha|unusual traffic|are you a robot/i.test(i),d=[s?"CONSENT_WALL":"",o?"BOT_CHALLENGE":"",n!==t?"REDIRECTED":""].filter(Boolean).join(",");return`intended=${t} | final=${n} | title="${a}" | flags=[${d}] | body="${i}"`}catch(n){return`diagnostics-failed: ${n.message}`}}captchaMessage(){return this.\u006b\u0065\u0072\u006e\u0065\u006cClient?"Google returned a CAPTCHA on this session \u2014 retrying with a fresh session.":is}async navigateTo(t){let n=process.env.ADS_ARRIVAL_REDIRECT_BASE?.trim(),a=n?gs(t,n):t;try{if(await this.page.goto(a,{waitUntil:"domcontentloaded",timeout:45e3}),/google\.[^/]+\/search\?/i.test(t)&&await this.isSerpChallengeVisible())throw new nt(this.captchaMessage())}catch(i){if(i instanceof nt)throw i;let s=await this.captureDiagnostics(t);throw new Ee(`page.goto failed: ${i.message} | ${s}`)}}async navigateToChannel(t){let n=bd(t);try{await this.page.goto(n,{waitUntil:"networkidle",timeout:3e4})}catch(a){let i=await this.captureDiagnostics(n);throw new Ee(`navigateToChannel failed: ${a.message} | ${i}`)}}async evaluate(t,n){return this.page.evaluate(t,n)}getPage(){return this.page}get\u004b\u0065\u0072\u006e\u0065\u006cSessionId(){return this.\u006b\u0065\u0072\u006e\u0065\u006cSessionId}getProviderSessionMetadata(){return{provider:this.\u006b\u0065\u0072\u006e\u0065\u006cSessionId?"\u006b\u0065\u0072\u006e\u0065\u006c":"local",providerSessionId:this.\u006b\u0065\u0072\u006e\u0065\u006cSessionId,disconnectObservation:null}}getDebugSnapshot(){return this.debugSnapshot}async close(){if(this.browser){let t=this.browser,n=this.\u006b\u0065\u0072\u006e\u0065\u006cSessionId,a=this.\u006b\u0065\u0072\u006e\u0065\u006cClient,i=this.\u006b\u0065\u0072\u006e\u0065\u006cOpenedAtMs,s=this.\u006b\u0065\u0072\u006e\u0065\u006cStealth,o=this.\u006b\u0065\u0072\u006e\u0065\u006cProxyUsed,d=this.\u006b\u0065\u0072\u006e\u0065\u006cProxySource,l=this.\u006b\u0065\u0072\u006e\u0065\u006cProxyType,U=this.\u006b\u0065\u0072\u006e\u0065\u006cHeadlessSent,u=this.getProviderSessionMetadata();if(this.browser=null,this.context=null,this.page=null,this.\u006b\u0065\u0072\u006e\u0065\u006cSessionId=null,this.\u006b\u0065\u0072\u006e\u0065\u006cClient=null,this.\u006b\u0065\u0072\u006e\u0065\u006cOpenedAtMs=null,this.\u006b\u0065\u0072\u006e\u0065\u006cHeadlessSent=null,this.\u006b\u0065\u0072\u006e\u0065\u006cProxySource=null,this.\u006b\u0065\u0072\u006e\u0065\u006cProxyType=null,a&&n&&i!=null&&hs({\u006b\u0065\u0072\u006e\u0065\u006cSessionId:n,source:"browser_driver",stealth:s,headlessSent:U,proxyUsed:o,proxySource:d,proxyType:l,openedAtMs:i,closedAtMs:Date.now()}),a&&n){console.info(JSON.stringify({event:"\u006b\u0065\u0072\u006e\u0065\u006c_browser_delete_started",\u006b\u0065\u0072\u006e\u0065\u006c_session_id:n}));let r=Pe(a.browsers.deleteByID(n),kd,`\u004b\u0065\u0072\u006e\u0065\u006c session ${n} delete`),S=Pe(t.close(),Cd,`\u004b\u0065\u0072\u006e\u0065\u006c browser ${n} close`),[c,h]=await Promise.allSettled([r,S]),m={\u006b\u0065\u0072\u006e\u0065\u006cSessionId:n,\u006b\u0065\u0072\u006e\u0065\u006cDeleteStarted:!0,\u006b\u0065\u0072\u006e\u0065\u006cDeleteSucceeded:c.status==="fulfilled",\u006b\u0065\u0072\u006e\u0065\u006cDeleteError:c.status==="rejected"?c.reason instanceof Error?c.reason.message:String(c.reason):null,browserCloseSucceeded:h.status==="fulfilled",browserCloseError:h.status==="rejected"?h.reason instanceof Error?h.reason.message:String(h.reason):null,provider:u.provider,providerSessionId:u.providerSessionId,providerDisconnectObserved:!1,providerDisconnectMessage:null};return c.status==="rejected"?(console.warn(JSON.stringify({event:"\u006b\u0065\u0072\u006e\u0065\u006c_browser_delete_failed",\u006b\u0065\u0072\u006e\u0065\u006c_session_id:n,message:m.\u006b\u0065\u0072\u006e\u0065\u006cDeleteError})),console.warn(`\u004b\u0065\u0072\u006e\u0065\u006c session cleanup failed for ${n}:`,c.reason)):console.info(JSON.stringify({event:"\u006b\u0065\u0072\u006e\u0065\u006c_browser_delete_succeeded",\u006b\u0065\u0072\u006e\u0065\u006c_session_id:n})),h.status==="rejected"&&(console.warn(JSON.stringify({event:"\u006b\u0065\u0072\u006e\u0065\u006c_browser_close_failed",\u006b\u0065\u0072\u006e\u0065\u006c_session_id:n,message:m.browserCloseError})),console.warn(`\u004b\u0065\u0072\u006e\u0065\u006c browser close failed for ${n}:`,h.reason)),m}return await t.close(),{\u006b\u0065\u0072\u006e\u0065\u006cSessionId:null,\u006b\u0065\u0072\u006e\u0065\u006cDeleteStarted:!1,\u006b\u0065\u0072\u006e\u0065\u006cDeleteSucceeded:null,\u006b\u0065\u0072\u006e\u0065\u006cDeleteError:null,browserCloseSucceeded:!0,browserCloseError:null,provider:u.provider,providerSessionId:u.providerSessionId,providerDisconnectObserved:!1,providerDisconnectMessage:null}}else if(this.context){let t=this.context;return this.context=null,this.page=null,await t.close(),{\u006b\u0065\u0072\u006e\u0065\u006cSessionId:null,\u006b\u0065\u0072\u006e\u0065\u006cDeleteStarted:!1,\u006b\u0065\u0072\u006e\u0065\u006cDeleteSucceeded:null,\u006b\u0065\u0072\u006e\u0065\u006cDeleteError:null,browserCloseSucceeded:!0,browserCloseError:null,provider:"local",providerSessionId:null,providerDisconnectObserved:!1,providerDisconnectMessage:null}}return{\u006b\u0065\u0072\u006e\u0065\u006cSessionId:null,\u006b\u0065\u0072\u006e\u0065\u006cDeleteStarted:!1,\u006b\u0065\u0072\u006e\u0065\u006cDeleteSucceeded:null,\u006b\u0065\u0072\u006e\u0065\u006cDeleteError:null,browserCloseSucceeded:null,browserCloseError:null,provider:"local",providerSessionId:null,providerDisconnectObserved:!1,providerDisconnectMessage:null}}};var Ts=require("playwright");function _d(e,t){if(typeof e!="string")return null;let n=e.replace(/[\u0000-\u001f\u007f]+/g," ").replace(/\s+/g," ").trim();return n?n.slice(0,t):null}function ms(e){let t=_d(e,320);return t?t.replace(/\b(?:wss?|https?):\/\/\S+/gi,"[redacted endpoint]").replace(/\b(?:authorization|proxy-authorization):\s*\S+/gi,"$1: [redacted]").replace(/\bBearer\s+\S+/gi,"Bearer [redacted]").slice(0,320):null}function ys(e){return ms(e)}function Cs(e){if(typeof e!="string"||e.length===0||e.length>512||e.trim()!==e||/[\s\u0000-\u001f\u007f]/.test(e)||/:\/\//.test(e))throw new Error("\u0042\u0072\u0069\u0067\u0068\u0074 \u0044\u0061\u0074\u0061 did not return a valid browser session ID");return e}function Ea(e,t,n=new Date().toISOString()){let a=ms(t);return{source:e,message:a??e.replaceAll("_"," "),observedAt:n}}function Ed(e){return{provider:e.provider,providerDisconnectObserved:e.disconnectObservation!==null,providerDisconnectMessage:e.disconnectObservation?.message??null}}function ks(e){return{...Ed(e),providerSessionId:e.providerSessionId}}var Pd=new Set(["image","media","font","stylesheet","ping"]),Id=["doubleclick.net","googleadservices.com","googlesyndication.com","google-analytics.com"],Ad='#search, #rso, a[href] h3, [data-snhf], [role="main"]';function Pa(e,t){return e?Math.max(1,Math.min(t,e-Date.now())):t}function Ms(){return process.env.\u0042\u0052\u0049\u0047\u0048\u0054\u0044\u0041\u0054\u0041_BROWSER_WSS?.trim()||null}function xn(){return process.env.SERP_BROWSER_PROVIDER?.trim().toLowerCase()==="\u006b\u0065\u0072\u006e\u0065\u006c"?!1:!!Ms()}async function Ia(e,t=!1){let n=e.request();try{let a=await n.allHeaders(),i=await fetch(n.url(),{method:n.method(),headers:{accept:a.accept??"*/*","accept-language":a["accept-language"]??"en-US,en;q=0.9","user-agent":a["user-agent"]??"Mozilla/5.0",...t?{cookie:a.cookie??"",referer:a.referer??"https://www.google.com/",...a["x-client-data"]?{"x-client-data":a["x-client-data"]}:{}}:{}},body:n.method()==="GET"||n.method()==="HEAD"?void 0:n.postData()});if(!i.ok)return!1;let s=Buffer.from(await i.arrayBuffer());return await e.fulfill({status:i.status,contentType:i.headers.get("content-type")??"application/octet-stream",headers:{"cache-control":"public, max-age=3600"},body:s}),!0}catch{return!1}}var Ln=class{browser=null;context=null;page=null;cdpSession=null;providerSessionId=null;disconnectObservation=null;closing=!1;observeDisconnect(t,n){this.closing||this.disconnectObservation||(this.disconnectObservation=Ea(t,n))}async launch(t){if(this.page)return;this.closing=!1,this.providerSessionId=null,this.disconnectObservation=null;let n=Ms();if(!n)throw new Error("SERP browser service is not configured");this.browser=await Ts.chromium.connectOverCDP(n),this.context=this.browser.contexts()[0]??await this.browser.newContext(),await this.context.addInitScript(()=>{let s=globalThis;typeof s.__name!="function"&&(s.__name=o=>o),typeof s.__publicField!="function"&&(s.__publicField=(o,d,l)=>(o[d]=l,l))}),this.page=this.context.pages()[0]??await this.context.newPage(),this.browser.on("disconnected",()=>this.observeDisconnect("browser_disconnected")),this.context.on("close",()=>this.observeDisconnect("context_closed")),this.page.on("close",()=>this.observeDisconnect("page_closed")),this.page.on("crash",()=>this.observeDisconnect("page_crashed")),this.cdpSession=await this.context.newCDPSession(this.page);let i=await this.cdpSession.send("Browser.getSessionId");this.providerSessionId=Cs(i?.sessionId),await this.setupPage(this.page)}async setupPage(t){await t.route("**/*",async n=>{let a=n.request(),i=a.resourceType(),s;try{s=new URL(a.url())}catch{await n.continue();return}let o=s.hostname.toLowerCase();if(!(s.pathname==="/sgasync"&&await Ia(n,!0))&&!(s.pathname.includes("/shared_dict/")&&await Ia(n))&&!(i==="script"&&(o==="www.google.com"||o==="www.gstatic.com")&&await Ia(n))){if(Pd.has(i)){await n.abort("blockedbyclient");return}if(Id.some(d=>o===d||o.endsWith(`.${d}`))){await n.abort("blockedbyclient");return}await n.continue()}})}async withTemporaryPage(t,n){if(!this.context||this.closing)throw new Error("Browser context is not available");return Nn(this.context,a=>this.setupPage(a),t,n)}async navigateToSERP(t,n,a,i,s){if(!this.page)throw new Error("Browser page is not available");let o=new URLSearchParams({q:t,gl:a,hl:i,pws:"0"});return s?.tbs&&o.set("tbs",s.tbs),n&&o.set("uule",n),await this.page.goto(`https://www.google.com/search?${o}`,{waitUntil:"domcontentloaded",timeout:Pa(s?.deadlineMs,s?.purpose==="serp"?8e3:45e3)}),s?.purpose==="serp"?(await this.page.waitForSelector(Ad,{timeout:Pa(s.deadlineMs,1500)}).catch(()=>null),{hasPaa:await this.page.locator(K.item).count().catch(()=>0)>0}):{hasPaa:!!await this.page.waitForSelector(K.item,{timeout:Pa(s?.deadlineMs,8e3)}).catch(()=>null)}}async navigateTo(t){if(!this.page)throw new Error("Browser page is not available");await this.page.goto(t,{waitUntil:"load",timeout:45e3})}async navigateToChannel(){throw new Error("YouTube navigation is not supported by the SERP browser driver")}async evaluate(t,n){if(!this.page)throw new Error("Browser page is not available");return this.page.evaluate(t,n)}getPage(){if(!this.page)throw new Error("Browser page is not available");return this.page}get\u004b\u0065\u0072\u006e\u0065\u006cSessionId(){return null}getProviderSessionMetadata(){return{provider:"\u0062\u0072\u0069\u0067\u0068\u0074_\u0064\u0061\u0074\u0061_browser_api",providerSessionId:this.providerSessionId,disconnectObservation:this.disconnectObservation}}getDebugSnapshot(){return{\u006b\u0065\u0072\u006e\u0065\u006c:null,context:null,networkLocation:null,serpNavigation:null}}async close(){this.closing=!0;let t=null;try{await this.browser?.close()}catch(a){t=ys(a instanceof Error?a.message:String(a))??"\u0042\u0072\u0069\u0067\u0068\u0074 \u0044\u0061\u0074\u0061 browser close failed",this.disconnectObservation??=Ea("browser_close_error",t)}let n=ks(this.getProviderSessionMetadata());return this.browser=null,this.context=null,this.page=null,this.cdpSession=null,{\u006b\u0065\u0072\u006e\u0065\u006cSessionId:null,\u006b\u0065\u0072\u006e\u0065\u006cDeleteStarted:!1,\u006b\u0065\u0072\u006e\u0065\u006cDeleteSucceeded:null,\u006b\u0065\u0072\u006e\u0065\u006cDeleteError:null,browserCloseSucceeded:t===null,browserCloseError:t,...n}}};function fs(e,t){let n=!!(e?.serpIdentity||e?.\u006b\u0065\u0072\u006e\u0065\u006cProfileName);return!t?.force\u004b\u0065\u0072\u006e\u0065\u006c&&(t?.forceManaged===!0||xn()&&!n)?new Ln:new An}var $t={austin:"Austin,Texas,United States","new york":"New York,New York,United States","new york city":"New York,New York,United States",nyc:"New York,New York,United States","los angeles":"Los Angeles,California,United States",la:"Los Angeles,California,United States",chicago:"Chicago,Illinois,United States",houston:"Houston,Texas,United States",phoenix:"Phoenix,Arizona,United States",philadelphia:"Philadelphia,Pennsylvania,United States",philly:"Philadelphia,Pennsylvania,United States","san antonio":"San Antonio,Texas,United States",dallas:"Dallas,Texas,United States",miami:"Miami,Florida,United States",seattle:"Seattle,Washington,United States",denver:"Denver,Colorado,United States",loveland:"Loveland,Colorado,United States","loveland co":"Loveland,Colorado,United States","fort collins":"Fort Collins,Colorado,United States",boulder:"Boulder,Colorado,United States","colorado springs":"Colorado Springs,Colorado,United States",boston:"Boston,Massachusetts,United States",atlanta:"Atlanta,Georgia,United States","san francisco":"San Francisco,California,United States",sf:"San Francisco,California,United States",portland:"Portland,Oregon,United States","las vegas":"Las Vegas,Nevada,United States",minneapolis:"Minneapolis,Minnesota,United States",detroit:"Detroit,Michigan,United States",nashville:"Nashville,Tennessee,United States",charlotte:"Charlotte,North Carolina,United States",orlando:"Orlando,Florida,United States","san diego":"San Diego,California,United States",baltimore:"Baltimore,Maryland,United States",sacramento:"Sacramento,California,United States",columbus:"Columbus,Ohio,United States",indianapolis:"Indianapolis,Indiana,United States","san jose":"San Jose,California,United States","fort worth":"Fort Worth,Texas,United States",jacksonville:"Jacksonville,Florida,United States",memphis:"Memphis,Tennessee,United States",louisville:"Louisville,Kentucky,United States",raleigh:"Raleigh,North Carolina,United States",richmond:"Richmond,Virginia,United States","salt lake city":"Salt Lake City,Utah,United States",toronto:"Toronto,Ontario,Canada",vancouver:"Vancouver,British Columbia,Canada",montreal:"Montreal,Quebec,Canada",calgary:"Calgary,Alberta,Canada",ottawa:"Ottawa,Ontario,Canada",london:"London,England,United Kingdom",manchester:"Manchester,England,United Kingdom",birmingham:"Birmingham,England,United Kingdom",edinburgh:"Edinburgh,Scotland,United Kingdom",glasgow:"Glasgow,Scotland,United Kingdom",leeds:"Leeds,England,United Kingdom",sydney:"Sydney,New South Wales,Australia",melbourne:"Melbourne,Victoria,Australia",brisbane:"Brisbane,Queensland,Australia",perth:"Perth,Western Australia,Australia",adelaide:"Adelaide,South Australia,Australia",dublin:"Dublin,Leinster,Ireland"};var Ns=`3, Nanjemoy,Maryland,United States
193
+ VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`,args:[(0,va.randomUUID)(),e.op??t?.op??null,e.probeRunId??t?.probeRunId??null,e.userId??t?.userId??null,e.vendor,e.model??null,e.units,e.unitType,us(e.vendor,e.units),e.error??null,e.method??t?.subOp??null,e.sourceKey??null,e.providerDurationMs??null,bn(e.providerCaptcha),e.providerStatus??null]})).rowsAffected===1}catch(t){return console.warn("[cost-telemetry] recordVendorUsage failed:",t instanceof Error?t.message:String(t)),!1}}function bn(e){return e==null?null:e?1:0}var yd=/\b(?:HeadlessChrome|Chrome|Chromium|Firefox)\/\d+|\bVersion\/\d+.*\bSafari\//;async function _n(e){try{let t=await e.evaluate(()=>navigator.userAgent);return typeof t=="string"&&yd.test(t)?t:null}catch{return null}}async function En(e){let t=null;try{let n=e.contexts().flatMap(a=>a.pages())[0];return n||(t=await e.newContext(),n=t.pages()[0]??await t.newPage()),await _n(n)}catch{return null}finally{t&&await t.close().catch(()=>{})}}In.chromium.use((0,ps.default)());var Cd=180,kd=3e3,Td=5e3,Md='#search, #rso, a[href] h3, [data-snhf], [role="main"]';function _a(e,t){return e?Math.max(1,Math.min(t,e-Date.now())):t}function fd(e,t){let n=process.env[e];if(!n)return t;let a=Number(n);return Number.isInteger(a)&&a>0?a:t}function Ea(e){return e?e.slice(-6):null}function gs(e,t){let n=(t??process.env.SERP_REDIRECT_BASE)?.trim().replace(/\/+$/,"");return n?`${n}/g/${Buffer.from(e,"utf8").toString("base64url")}`:e}function Nd(e){return e?/united states|u\.?s\.?a?\.?|^us$/i.test(e.trim()):!1}function $e(e){return e instanceof Error?e.message:String(e)}function vd(e){return/\b409\b|conflict|already exists/i.test($e(e))}async function bd(e,t){try{await e.profiles.create({name:t})}catch(n){if(!vd(n))throw n}}function Pn(e,t){return{viewport:e.viewport,locale:e.locale,...t?{userAgent:t}:{},...e.deviceScaleFactor?{deviceScaleFactor:e.deviceScaleFactor}:{},...e.isMobile!==void 0?{isMobile:e.isMobile}:{},...e.hasTouch!==void 0?{hasTouch:e.hasTouch}:{}}}async function Pe(e,t,n){let a;try{return await Promise.race([e,new Promise((i,s)=>{a=setTimeout(()=>s(new Error(`${n} timed out after ${t}ms`)),t)})])}finally{a&&clearTimeout(a)}}function _d(e){let t=e.trim();if(!t)throw new Error("channelHandle is required");if(/^https?:\/\//i.test(t)||/^(www\.|m\.)?youtube\.com\//i.test(t)){let o=new URL(/^https?:\/\//i.test(t)?t:`https://${t}`);if(o.hostname.replace(/^www\./,"").replace(/^m\./,"").toLowerCase()!=="youtube.com")throw new Error("channel URL must be on youtube.com");let l=o.pathname.split("/").filter(Boolean),U=l[0]??"",u=l[1]??"";if(U.startsWith("@"))return`https://www.youtube.com/${U}/videos`;if(U==="channel"&&u)return`https://www.youtube.com/channel/${u}/videos`;if((U==="c"||U==="user")&&u)return`https://www.youtube.com/${U}/${u}/videos`;throw new Error("channel URL must be a YouTube handle, /channel/UC..., /c/..., or /user/... URL")}let i=t.replace(/^\/+/,"").replace(/\/+$/,"").replace(/\/videos$/i,"");if(/^UC[\w-]{20,}$/.test(i))return`https://www.youtube.com/channel/${i}/videos`;let s=i.startsWith("@")?i:`@${i}`;if(!/^@[\w.-]+$/.test(s))throw new Error("channelHandle must be an @handle, UC channel ID, or YouTube channel URL");return`https://www.youtube.com/${s}/videos`}var An=class{browser=null;context=null;page=null;\u006b\u0065\u0072\u006e\u0065\u006cClient=null;\u006b\u0065\u0072\u006e\u0065\u006cSessionId=null;\u006b\u0065\u0072\u006e\u0065\u006cOpenedAtMs=null;\u006b\u0065\u0072\u006e\u0065\u006cStealth=null;\u006b\u0065\u0072\u006e\u0065\u006cProxyUsed=null;\u006b\u0065\u0072\u006e\u0065\u006cProxySource=null;\u006b\u0065\u0072\u006e\u0065\u006cProxyType=null;\u006b\u0065\u0072\u006e\u0065\u006cHeadlessSent=null;debugEnabled=!1;debugSnapshot={\u006b\u0065\u0072\u006e\u0065\u006c:null,context:null,networkLocation:null,serpNavigation:null};async launch(t){this.debugEnabled=t.debug===!0;let n=t.proxyMode??(t.\u006b\u0065\u0072\u006e\u0065\u006cProxyId?"configured":"none"),a=t.isMobile?"mobile":"desktop";if(this.debugSnapshot={\u006b\u0065\u0072\u006e\u0065\u006c:null,context:{viewport:t.viewport,locale:t.locale,device:a,userAgent:null,deviceScaleFactor:t.deviceScaleFactor??null,isMobile:t.isMobile===!0,hasTouch:t.hasTouch===!0},networkLocation:null,serpNavigation:null},t.\u006b\u0065\u0072\u006e\u0065\u006cApiKey){this.\u006b\u0065\u0072\u006e\u0065\u006cClient=new H({apiKey:t.\u006b\u0065\u0072\u006e\u0065\u006cApiKey});let s=Number.isInteger(t.\u006b\u0065\u0072\u006e\u0065\u006cTimeoutSeconds)&&Number(t.\u006b\u0065\u0072\u006e\u0065\u006cTimeoutSeconds)>0?Number(t.\u006b\u0065\u0072\u006e\u0065\u006cTimeoutSeconds):fd("\u004b\u0045\u0052\u004e\u0045\u004c_BROWSER_TIMEOUT_SECONDS",Cd),o=Math.min(s,600);t.\u006b\u0065\u0072\u006e\u0065\u006cProfileName&&t.\u006b\u0065\u0072\u006e\u0065\u006cProfileSaveChanges===!0&&await bd(this.\u006b\u0065\u0072\u006e\u0065\u006cClient,t.\u006b\u0065\u0072\u006e\u0065\u006cProfileName);let d=zt(),l;t.headlessMode==="headful"||d?.forceHeadful===!0?l=!1:t.headlessMode==="headless"?l=!0:l=d?.forceHeadless===!0||process.env.\u004b\u0045\u0052\u004e\u0045\u004c_HEADLESS_DISABLED!=="true"&&!!d?.op&&Us.has(d.op),this.\u006b\u0065\u0072\u006e\u0065\u006cHeadlessSent=l?!0:null,d?.headlessSentOut&&(d.headlessSentOut.value=this.\u006b\u0065\u0072\u006e\u0065\u006cHeadlessSent);let U=await this.\u006b\u0065\u0072\u006e\u0065\u006cClient.browsers.create({stealth:t.\u006b\u0065\u0072\u006e\u0065\u006cStealth===!0,timeout_seconds:o,...l?{headless:!0}:{},...t.\u006b\u0065\u0072\u006e\u0065\u006cProxyId?{proxy_id:t.\u006b\u0065\u0072\u006e\u0065\u006cProxyId}:{},...t.\u006b\u0065\u0072\u006e\u0065\u006cProfileName?{profile:{name:t.\u006b\u0065\u0072\u006e\u0065\u006cProfileName,...typeof t.\u006b\u0065\u0072\u006e\u0065\u006cProfileSaveChanges=="boolean"?{save_changes:t.\u006b\u0065\u0072\u006e\u0065\u006cProfileSaveChanges}:{}}}:{}});this.\u006b\u0065\u0072\u006e\u0065\u006cSessionId=U.session_id,this.\u006b\u0065\u0072\u006e\u0065\u006cOpenedAtMs=Date.now(),this.\u006b\u0065\u0072\u006e\u0065\u006cStealth=typeof U.stealth=="boolean"?U.stealth:null,this.\u006b\u0065\u0072\u006e\u0065\u006cProxyUsed=typeof U.proxy_id=="string"?!!U.proxy_id:!!t.\u006b\u0065\u0072\u006e\u0065\u006cProxyId,this.\u006b\u0065\u0072\u006e\u0065\u006cProxySource=t.\u006b\u0065\u0072\u006e\u0065\u006cProxyResolution?.source??null,this.\u006b\u0065\u0072\u006e\u0065\u006cProxyType=t.\u006b\u0065\u0072\u006e\u0065\u006cProxyResolution?.proxyType??null;let u=null,r=null;if(!t.\u006b\u0065\u0072\u006e\u0065\u006cProxyId&&!t.keepDefaultProxy)try{await Pe(this.\u006b\u0065\u0072\u006e\u0065\u006cClient.browsers.update(this.\u006b\u0065\u0072\u006e\u0065\u006cSessionId,{disable_default_proxy:!0}),5e3,`\u004b\u0065\u0072\u006e\u0065\u006c session ${this.\u006b\u0065\u0072\u006e\u0065\u006cSessionId} disable default proxy`),u=!0}catch(c){u=!1,r=$e(c)}let S={sessionId:this.\u006b\u0065\u0072\u006e\u0065\u006cSessionId,proxyMode:n,requestedProxyIdPresent:!!t.\u006b\u0065\u0072\u006e\u0065\u006cProxyId,requestedProxyIdSuffix:Ea(t.\u006b\u0065\u0072\u006e\u0065\u006cProxyId),createdProxyIdPresent:typeof U.proxy_id=="string"?!!U.proxy_id:null,createdProxyIdSuffix:Ea(U.proxy_id),retrievedProxyIdPresent:null,retrievedProxyIdSuffix:null,retrievedProxyIdMatchesRequested:null,defaultProxyDisabled:u,defaultProxyDisableError:r,proxyResolution:t.\u006b\u0065\u0072\u006e\u0065\u006cProxyResolution??null,timeoutSeconds:o,stealth:typeof U.stealth=="boolean"?U.stealth:null,profilePresent:null,poolPresent:null,retrieveError:null};if(this.debugSnapshot.\u006b\u0065\u0072\u006e\u0065\u006c=S,console.info(JSON.stringify({event:"\u006b\u0065\u0072\u006e\u0065\u006c_browser_created",\u006b\u0065\u0072\u006e\u0065\u006c_session_id:this.\u006b\u0065\u0072\u006e\u0065\u006cSessionId,timeout_seconds:o,proxy_mode:n,proxy_id_present:!!t.\u006b\u0065\u0072\u006e\u0065\u006cProxyId,profile_name_present:!!t.\u006b\u0065\u0072\u006e\u0065\u006cProfileName,proxy_resolution_source:t.\u006b\u0065\u0072\u006e\u0065\u006cProxyResolution?.source})),this.debugEnabled&&await this.populate\u004b\u0065\u0072\u006e\u0065\u006cRetrieveDebug(S,t.\u006b\u0065\u0072\u006e\u0065\u006cProxyId),this.browser=await ws.chromium.connectOverCDP(U.cdp_ws_url),t.\u006b\u0065\u0072\u006e\u0065\u006cProfileName||t.preserve\u004b\u0065\u0072\u006e\u0065\u006cContext)this.context=this.browser.contexts()[0]??await this.browser.newContext(Pn(t)),this.page=this.context.pages()[0]??await this.context.newPage(),this.debugSnapshot.context.userAgent=await _n(this.page);else{let c=await En(this.browser);this.debugSnapshot.context.userAgent=c,this.context=await this.browser.newContext(Pn(t,c)),this.page=await this.context.newPage()}if(await this.installEsbuildHelperShims(this.context),t.preserve\u004b\u0065\u0072\u006e\u0065\u006cContext||await this.page.setViewportSize(t.viewport),(this.debugEnabled||t.requireUsEgress)&&(this.debugSnapshot.networkLocation=await this.captureBrowserNetworkLocation()),t.requireUsEgress){let c=this.debugSnapshot.networkLocation?.country??null;if(!Nd(c))throw new nt(`Non-US egress (${c??"unknown"}) \u2014 retrying with a fresh session`)}return}let i={headless:t.headless,proxy:t.proxy?{server:t.proxy}:void 0,...t.browserExecutablePath?{executablePath:t.browserExecutablePath}:{},...t.ignoreDefaultArgs?{ignoreDefaultArgs:t.ignoreDefaultArgs}:{}};if(t.profileDir)this.context=await In.chromium.launchPersistentContext(t.profileDir,{...i,...Pn(t)}),await this.installEsbuildHelperShims(this.context),this.page=await this.context.newPage(),this.debugSnapshot.context.userAgent=await _n(this.page);else{this.browser=await In.chromium.launch(i);let s=await En(this.browser);this.debugSnapshot.context.userAgent=s,this.context=await this.browser.newContext(Pn(t,s)),await this.installEsbuildHelperShims(this.context),this.page=await this.context.newPage()}this.debugEnabled&&(this.debugSnapshot.networkLocation=await this.captureBrowserNetworkLocation())}async populate\u004b\u0065\u0072\u006e\u0065\u006cRetrieveDebug(t,n){if(!(!this.\u006b\u0065\u0072\u006e\u0065\u006cClient||!this.\u006b\u0065\u0072\u006e\u0065\u006cSessionId))try{let a=await Pe(this.\u006b\u0065\u0072\u006e\u0065\u006cClient.browsers.retrieve(this.\u006b\u0065\u0072\u006e\u0065\u006cSessionId),5e3,`\u004b\u0065\u0072\u006e\u0065\u006c session ${this.\u006b\u0065\u0072\u006e\u0065\u006cSessionId} retrieve`);t.retrievedProxyIdPresent=typeof a.proxy_id=="string"?!!a.proxy_id:!1,t.retrievedProxyIdSuffix=Ea(a.proxy_id),t.retrievedProxyIdMatchesRequested=n?a.proxy_id===n:!a.proxy_id,t.timeoutSeconds=typeof a.timeout_seconds=="number"?a.timeout_seconds:t.timeoutSeconds,t.stealth=typeof a.stealth=="boolean"?a.stealth:t.stealth,t.profilePresent=!!a.profile,t.poolPresent=!!a.pool}catch(a){t.retrieveError=$e(a)}}async captureBrowserNetworkLocation(){let t=(a,i="ipapi.co")=>({source:i,ip:null,city:null,region:null,country:null,org:null,timezone:null,error:a});if(!this.context)return t("browser context is not available");let n=null;try{n=await this.context.newPage();let a=await this.loadJsonInDebugPage(n,"https://ipwho.is/");if(a){let d=typeof a.connection=="object"&&a.connection!==null?a.connection:{};return{source:"ipwho.is",ip:typeof a.ip=="string"?a.ip:null,city:typeof a.city=="string"?a.city:null,region:typeof a.region=="string"?a.region:null,country:typeof a.country=="string"?a.country:null,org:typeof d.org=="string"?d.org:null,timezone:typeof a.timezone=="object"&&a.timezone!==null&&typeof a.timezone.id=="string"?a.timezone.id:null,error:null}}let i=await this.loadJsonInDebugPage(n,"https://api64.ipify.org?format=json");if(i)return{source:"api64.ipify.org",ip:typeof i.ip=="string"?i.ip:null,city:null,region:null,country:null,org:null,timezone:null,error:null};await Pe(n.goto("https://ipapi.co/json/",{waitUntil:"domcontentloaded",timeout:7e3}),8e3,"browser network location navigation");let s=await n.locator("body").innerText({timeout:2e3}),o=JSON.parse(s);return{source:"ipapi.co",ip:typeof o.ip=="string"?o.ip:null,city:typeof o.city=="string"?o.city:null,region:typeof o.region=="string"?o.region:null,country:typeof o.country_name=="string"?o.country_name:typeof o.country=="string"?o.country:null,org:typeof o.org=="string"?o.org:null,timezone:typeof o.timezone=="string"?o.timezone:null,error:null}}catch(a){return t($e(a))}finally{await n?.close().catch(()=>{})}}async loadJsonInDebugPage(t,n){try{await Pe(t.goto(n,{waitUntil:"domcontentloaded",timeout:7e3}),8e3,`browser network location navigation ${n}`);let a=await t.locator("body").innerText({timeout:2e3});return JSON.parse(a)}catch{return null}}async installEsbuildHelperShims(t){await t.addInitScript(()=>{let n=globalThis;typeof n.__name!="function"&&(n.__name=a=>a),typeof n.__publicField!="function"&&(n.__publicField=(a,i,s)=>(a[i]=s,s))})}async withTemporaryPage(t,n){if(!this.context)throw new Error("Browser context is not available");return Nn(this.context,async()=>{},t,n)}async navigateToSERP(t,n,a,i,s){let o=new URLSearchParams({q:t,gl:a,hl:i,pws:"0"});s?.tbs&&o.set("tbs",s.tbs),n&&o.set("uule",n);let d="https://www.google.com/search?"+o.toString(),l=gs(d),U=s?.debug?{requestedUrl:d,finalUrl:null,title:null,bodySnippet:null,hasPaa:null,captchaDetected:null,googleSorryUrl:null,redirected:null}:null;U&&(this.debugSnapshot.serpNavigation=U);try{await this.page.goto(l,{waitUntil:"domcontentloaded",timeout:_a(s?.deadlineMs,s?.purpose==="serp"?8e3:45e3)})}catch(r){await this.updateSerpNavigationDebug(U,d,{hasPaa:null,captchaDetected:null});let S=await this.captureDiagnostics(d);throw new Ee(`page.goto failed: ${r.message} | ${S}`)}if(await this.isSerpChallengeVisible())throw await this.updateSerpNavigationDebug(U,d,{hasPaa:!1,captchaDetected:!0}),new nt(this.captchaMessage());if(s?.purpose==="serp"){await this.page.waitForSelector(Md,{timeout:_a(s.deadlineMs,1500)}).catch(()=>null);let r=await this.page.locator(K.item).count().catch(()=>0)>0;return await this.updateSerpNavigationDebug(U,d,{hasPaa:r,captchaDetected:!1}),{hasPaa:r}}if(await this.page.waitForSelector(K.item,{timeout:_a(s?.deadlineMs,4e3)}).catch(()=>null))return await this.updateSerpNavigationDebug(U,d,{hasPaa:!0,captchaDetected:!1}),{hasPaa:!0};if(await this.isSerpChallengeVisible())throw await this.updateSerpNavigationDebug(U,d,{hasPaa:!1,captchaDetected:!0}),new nt(this.captchaMessage());for(let r=1;r<=6;r++)if(await this.page.evaluate(c=>{window.scrollTo(0,document.body.scrollHeight*c)},r/6),await this.page.waitForTimeout(600),await this.page.locator(K.item).count()>0)return await this.updateSerpNavigationDebug(U,d,{hasPaa:!0,captchaDetected:!1}),{hasPaa:!0};return await this.updateSerpNavigationDebug(U,d,{hasPaa:!1,captchaDetected:!1}),{hasPaa:!1}}async isSerpChallengeVisible(){if(!this.page)return!1;if(await this.page.locator(K.captchaMarker).count().catch(()=>0)>0)return!0;let n=this.page.url();if(/google\.[^/]+\/sorry\//i.test(n))return!0;let a=await this.page.evaluate(()=>(document.body?.innerText??"").replace(/\s+/g," ").trim().slice(0,1e3)).catch(()=>"");return/recaptcha|unusual traffic|are you a robot|about this page|detected unusual traffic/i.test(a)}async updateSerpNavigationDebug(t,n,a){if(!(!t||!this.page))try{let i=this.page.url(),s=await this.page.title().catch(()=>""),o=await this.page.evaluate(()=>(document.body?.innerText??"").replace(/\s+/g," ").trim().slice(0,500)).catch(()=>""),d=/recaptcha|unusual traffic|are you a robot/i.test(o);t.finalUrl=i,t.title=s,t.bodySnippet=o,t.hasPaa=a.hasPaa,t.captchaDetected=a.captchaDetected??d,t.googleSorryUrl=/google\.[^/]+\/sorry\//i.test(i),t.redirected=i!==n}catch(i){t.bodySnippet=`debug capture failed: ${$e(i)}`}}async captureDiagnostics(t){try{let n=this.page.url(),a=await this.page.title().catch(()=>""),i=await this.page.evaluate(()=>(document.body?.innerText??"").replace(/\s+/g," ").trim().slice(0,400)).catch(()=>""),s=/consent\.google\./.test(n)||/before you continue/i.test(i),o=/recaptcha|unusual traffic|are you a robot/i.test(i),d=[s?"CONSENT_WALL":"",o?"BOT_CHALLENGE":"",n!==t?"REDIRECTED":""].filter(Boolean).join(",");return`intended=${t} | final=${n} | title="${a}" | flags=[${d}] | body="${i}"`}catch(n){return`diagnostics-failed: ${n.message}`}}captchaMessage(){return this.\u006b\u0065\u0072\u006e\u0065\u006cClient?"Google returned a CAPTCHA on this session \u2014 retrying with a fresh session.":is}async navigateTo(t){let n=process.env.ADS_ARRIVAL_REDIRECT_BASE?.trim(),a=n?gs(t,n):t;try{if(await this.page.goto(a,{waitUntil:"domcontentloaded",timeout:45e3}),/google\.[^/]+\/search\?/i.test(t)&&await this.isSerpChallengeVisible())throw new nt(this.captchaMessage())}catch(i){if(i instanceof nt)throw i;let s=await this.captureDiagnostics(t);throw new Ee(`page.goto failed: ${i.message} | ${s}`)}}async navigateToChannel(t){let n=_d(t);try{await this.page.goto(n,{waitUntil:"networkidle",timeout:3e4})}catch(a){let i=await this.captureDiagnostics(n);throw new Ee(`navigateToChannel failed: ${a.message} | ${i}`)}}async evaluate(t,n){return this.page.evaluate(t,n)}getPage(){return this.page}get\u004b\u0065\u0072\u006e\u0065\u006cSessionId(){return this.\u006b\u0065\u0072\u006e\u0065\u006cSessionId}getProviderSessionMetadata(){return{provider:this.\u006b\u0065\u0072\u006e\u0065\u006cSessionId?"\u006b\u0065\u0072\u006e\u0065\u006c":"local",providerSessionId:this.\u006b\u0065\u0072\u006e\u0065\u006cSessionId,disconnectObservation:null}}getDebugSnapshot(){return this.debugSnapshot}async close(){if(this.browser){let t=this.browser,n=this.\u006b\u0065\u0072\u006e\u0065\u006cSessionId,a=this.\u006b\u0065\u0072\u006e\u0065\u006cClient,i=this.\u006b\u0065\u0072\u006e\u0065\u006cOpenedAtMs,s=this.\u006b\u0065\u0072\u006e\u0065\u006cStealth,o=this.\u006b\u0065\u0072\u006e\u0065\u006cProxyUsed,d=this.\u006b\u0065\u0072\u006e\u0065\u006cProxySource,l=this.\u006b\u0065\u0072\u006e\u0065\u006cProxyType,U=this.\u006b\u0065\u0072\u006e\u0065\u006cHeadlessSent,u=this.getProviderSessionMetadata();if(this.browser=null,this.context=null,this.page=null,this.\u006b\u0065\u0072\u006e\u0065\u006cSessionId=null,this.\u006b\u0065\u0072\u006e\u0065\u006cClient=null,this.\u006b\u0065\u0072\u006e\u0065\u006cOpenedAtMs=null,this.\u006b\u0065\u0072\u006e\u0065\u006cHeadlessSent=null,this.\u006b\u0065\u0072\u006e\u0065\u006cProxySource=null,this.\u006b\u0065\u0072\u006e\u0065\u006cProxyType=null,a&&n&&i!=null&&hs({\u006b\u0065\u0072\u006e\u0065\u006cSessionId:n,source:"browser_driver",stealth:s,headlessSent:U,proxyUsed:o,proxySource:d,proxyType:l,openedAtMs:i,closedAtMs:Date.now()}),a&&n){console.info(JSON.stringify({event:"\u006b\u0065\u0072\u006e\u0065\u006c_browser_delete_started",\u006b\u0065\u0072\u006e\u0065\u006c_session_id:n}));let r=Pe(a.browsers.deleteByID(n),Td,`\u004b\u0065\u0072\u006e\u0065\u006c session ${n} delete`),S=Pe(t.close(),kd,`\u004b\u0065\u0072\u006e\u0065\u006c browser ${n} close`),[c,h]=await Promise.allSettled([r,S]),m={\u006b\u0065\u0072\u006e\u0065\u006cSessionId:n,\u006b\u0065\u0072\u006e\u0065\u006cDeleteStarted:!0,\u006b\u0065\u0072\u006e\u0065\u006cDeleteSucceeded:c.status==="fulfilled",\u006b\u0065\u0072\u006e\u0065\u006cDeleteError:c.status==="rejected"?c.reason instanceof Error?c.reason.message:String(c.reason):null,browserCloseSucceeded:h.status==="fulfilled",browserCloseError:h.status==="rejected"?h.reason instanceof Error?h.reason.message:String(h.reason):null,provider:u.provider,providerSessionId:u.providerSessionId,providerDisconnectObserved:!1,providerDisconnectMessage:null};return c.status==="rejected"?(console.warn(JSON.stringify({event:"\u006b\u0065\u0072\u006e\u0065\u006c_browser_delete_failed",\u006b\u0065\u0072\u006e\u0065\u006c_session_id:n,message:m.\u006b\u0065\u0072\u006e\u0065\u006cDeleteError})),console.warn(`\u004b\u0065\u0072\u006e\u0065\u006c session cleanup failed for ${n}:`,c.reason)):console.info(JSON.stringify({event:"\u006b\u0065\u0072\u006e\u0065\u006c_browser_delete_succeeded",\u006b\u0065\u0072\u006e\u0065\u006c_session_id:n})),h.status==="rejected"&&(console.warn(JSON.stringify({event:"\u006b\u0065\u0072\u006e\u0065\u006c_browser_close_failed",\u006b\u0065\u0072\u006e\u0065\u006c_session_id:n,message:m.browserCloseError})),console.warn(`\u004b\u0065\u0072\u006e\u0065\u006c browser close failed for ${n}:`,h.reason)),m}return await t.close(),{\u006b\u0065\u0072\u006e\u0065\u006cSessionId:null,\u006b\u0065\u0072\u006e\u0065\u006cDeleteStarted:!1,\u006b\u0065\u0072\u006e\u0065\u006cDeleteSucceeded:null,\u006b\u0065\u0072\u006e\u0065\u006cDeleteError:null,browserCloseSucceeded:!0,browserCloseError:null,provider:u.provider,providerSessionId:u.providerSessionId,providerDisconnectObserved:!1,providerDisconnectMessage:null}}else if(this.context){let t=this.context;return this.context=null,this.page=null,await t.close(),{\u006b\u0065\u0072\u006e\u0065\u006cSessionId:null,\u006b\u0065\u0072\u006e\u0065\u006cDeleteStarted:!1,\u006b\u0065\u0072\u006e\u0065\u006cDeleteSucceeded:null,\u006b\u0065\u0072\u006e\u0065\u006cDeleteError:null,browserCloseSucceeded:!0,browserCloseError:null,provider:"local",providerSessionId:null,providerDisconnectObserved:!1,providerDisconnectMessage:null}}return{\u006b\u0065\u0072\u006e\u0065\u006cSessionId:null,\u006b\u0065\u0072\u006e\u0065\u006cDeleteStarted:!1,\u006b\u0065\u0072\u006e\u0065\u006cDeleteSucceeded:null,\u006b\u0065\u0072\u006e\u0065\u006cDeleteError:null,browserCloseSucceeded:null,browserCloseError:null,provider:"local",providerSessionId:null,providerDisconnectObserved:!1,providerDisconnectMessage:null}}};var Ms=require("playwright");function Ed(e,t){if(typeof e!="string")return null;let n=e.replace(/[\u0000-\u001f\u007f]+/g," ").replace(/\s+/g," ").trim();return n?n.slice(0,t):null}function ms(e){let t=Ed(e,320);return t?t.replace(/\b(?:wss?|https?):\/\/\S+/gi,"[redacted endpoint]").replace(/\b(?:authorization|proxy-authorization):\s*\S+/gi,"$1: [redacted]").replace(/\bBearer\s+\S+/gi,"Bearer [redacted]").slice(0,320):null}function ys(e){return ms(e)}function Cs(e){if(typeof e!="string"||e.length===0||e.length>512||e.trim()!==e||/[\s\u0000-\u001f\u007f]/.test(e)||/:\/\//.test(e))throw new Error("\u0042\u0072\u0069\u0067\u0068\u0074 \u0044\u0061\u0074\u0061 did not return a valid browser session ID");return e}function Pa(e,t,n=new Date().toISOString()){let a=ms(t);return{source:e,message:a??e.replaceAll("_"," "),observedAt:n}}function Pd(e){return{provider:e.provider,providerDisconnectObserved:e.disconnectObservation!==null,providerDisconnectMessage:e.disconnectObservation?.message??null}}function ks(e){return{...Pd(e),providerSessionId:e.providerSessionId}}var Id=new Set(["image","media","font","stylesheet","ping"]),Ad=["doubleclick.net","googleadservices.com","googlesyndication.com","google-analytics.com"],Ld='#search, #rso, a[href] h3, [data-snhf], [role="main"]',xd=3e4,Ts=1500;function Ln(e,t){return e?Math.max(1,Math.min(t,e-Date.now())):t}function fs(){return process.env.\u0042\u0052\u0049\u0047\u0048\u0054\u0044\u0041\u0054\u0041_BROWSER_WSS?.trim()||null}function On(){return process.env.SERP_BROWSER_PROVIDER?.trim().toLowerCase()==="\u006b\u0065\u0072\u006e\u0065\u006c"?!1:!!fs()}async function Ia(e,t=!1){let n=e.request();try{let a=await n.allHeaders(),i=await fetch(n.url(),{method:n.method(),headers:{accept:a.accept??"*/*","accept-language":a["accept-language"]??"en-US,en;q=0.9","user-agent":a["user-agent"]??"Mozilla/5.0",...t?{cookie:a.cookie??"",referer:a.referer??"https://www.google.com/",...a["x-client-data"]?{"x-client-data":a["x-client-data"]}:{}}:{}},body:n.method()==="GET"||n.method()==="HEAD"?void 0:n.postData()});if(!i.ok)return!1;let s=Buffer.from(await i.arrayBuffer());return await e.fulfill({status:i.status,contentType:i.headers.get("content-type")??"application/octet-stream",headers:{"cache-control":"public, max-age=3600"},body:s}),!0}catch{return!1}}var xn=class{browser=null;context=null;page=null;cdpSession=null;providerSessionId=null;disconnectObservation=null;closing=!1;observeDisconnect(t,n){this.closing||this.disconnectObservation||(this.disconnectObservation=Pa(t,n))}async launch(t){if(this.page)return;this.closing=!1,this.providerSessionId=null,this.disconnectObservation=null;let n=fs();if(!n)throw new Error("SERP browser service is not configured");this.browser=await Ms.chromium.connectOverCDP(n,{timeout:Ln(t.operationDeadlineMs,xd)}),this.context=this.browser.contexts()[0]??await this.browser.newContext(),await this.context.addInitScript(()=>{let s=globalThis;typeof s.__name!="function"&&(s.__name=o=>o),typeof s.__publicField!="function"&&(s.__publicField=(o,d,l)=>(o[d]=l,l))}),this.page=this.context.pages()[0]??await this.context.newPage(),this.browser.on("disconnected",()=>this.observeDisconnect("browser_disconnected")),this.context.on("close",()=>this.observeDisconnect("context_closed")),this.page.on("close",()=>this.observeDisconnect("page_closed")),this.page.on("crash",()=>this.observeDisconnect("page_crashed")),this.cdpSession=await this.context.newCDPSession(this.page);let i=await this.cdpSession.send("Browser.getSessionId");this.providerSessionId=Cs(i?.sessionId),await this.setupPage(this.page)}async setupPage(t){await t.route("**/*",async n=>{let a=n.request(),i=a.resourceType(),s;try{s=new URL(a.url())}catch{await n.continue();return}let o=s.hostname.toLowerCase();if(!(s.pathname==="/sgasync"&&await Ia(n,!0))&&!(s.pathname.includes("/shared_dict/")&&await Ia(n))&&!(i==="script"&&(o==="www.google.com"||o==="www.gstatic.com")&&await Ia(n))){if(Id.has(i)){await n.abort("blockedbyclient");return}if(Ad.some(d=>o===d||o.endsWith(`.${d}`))){await n.abort("blockedbyclient");return}await n.continue()}})}async withTemporaryPage(t,n){if(!this.context||this.closing)throw new Error("Browser context is not available");return Nn(this.context,a=>this.setupPage(a),t,n)}async navigateToSERP(t,n,a,i,s){if(!this.page)throw new Error("Browser page is not available");let o=new URLSearchParams({q:t,gl:a,hl:i,pws:"0"});return s?.tbs&&o.set("tbs",s.tbs),n&&o.set("uule",n),await this.page.goto(`https://www.google.com/search?${o}`,{waitUntil:"domcontentloaded",timeout:Ln(s?.deadlineMs,s?.purpose==="serp"?8e3:45e3)}),s?.purpose==="serp"?(await this.page.waitForSelector(Ld,{timeout:Ln(s.deadlineMs,1500)}).catch(()=>null),{hasPaa:await this.page.locator(K.item).count().catch(()=>0)>0}):{hasPaa:!!await this.page.waitForSelector(K.item,{timeout:Ln(s?.deadlineMs,8e3)}).catch(()=>null)}}async navigateTo(t){if(!this.page)throw new Error("Browser page is not available");await this.page.goto(t,{waitUntil:"load",timeout:45e3})}async navigateToChannel(){throw new Error("YouTube navigation is not supported by the SERP browser driver")}async evaluate(t,n){if(!this.page)throw new Error("Browser page is not available");return this.page.evaluate(t,n)}getPage(){if(!this.page)throw new Error("Browser page is not available");return this.page}get\u004b\u0065\u0072\u006e\u0065\u006cSessionId(){return null}getProviderSessionMetadata(){return{provider:"\u0062\u0072\u0069\u0067\u0068\u0074_\u0064\u0061\u0074\u0061_browser_api",providerSessionId:this.providerSessionId,disconnectObservation:this.disconnectObservation}}getDebugSnapshot(){return{\u006b\u0065\u0072\u006e\u0065\u006c:null,context:null,networkLocation:null,serpNavigation:null}}async close(){this.closing=!0;let t=null,n;try{let i=this.browser?.close();i&&await Promise.race([i,new Promise((s,o)=>{n=setTimeout(()=>o(new Error(`Browser close acknowledgement exceeded ${Ts}ms`)),Ts)})])}catch(i){t=ys(i instanceof Error?i.message:String(i))??"\u0042\u0072\u0069\u0067\u0068\u0074 \u0044\u0061\u0074\u0061 browser close failed",this.disconnectObservation??=Pa("browser_close_error",t)}finally{n&&clearTimeout(n)}let a=ks(this.getProviderSessionMetadata());return this.browser=null,this.context=null,this.page=null,this.cdpSession=null,{\u006b\u0065\u0072\u006e\u0065\u006cSessionId:null,\u006b\u0065\u0072\u006e\u0065\u006cDeleteStarted:!1,\u006b\u0065\u0072\u006e\u0065\u006cDeleteSucceeded:null,\u006b\u0065\u0072\u006e\u0065\u006cDeleteError:null,browserCloseSucceeded:t===null,browserCloseError:t,...a}}};function Ns(e,t){let n=!!(e?.serpIdentity||e?.\u006b\u0065\u0072\u006e\u0065\u006cProfileName);return!t?.force\u004b\u0065\u0072\u006e\u0065\u006c&&(t?.forceManaged===!0||On()&&!n)?new xn:new An}var $t={austin:"Austin,Texas,United States","new york":"New York,New York,United States","new york city":"New York,New York,United States",nyc:"New York,New York,United States","los angeles":"Los Angeles,California,United States",la:"Los Angeles,California,United States",chicago:"Chicago,Illinois,United States",houston:"Houston,Texas,United States",phoenix:"Phoenix,Arizona,United States",philadelphia:"Philadelphia,Pennsylvania,United States",philly:"Philadelphia,Pennsylvania,United States","san antonio":"San Antonio,Texas,United States",dallas:"Dallas,Texas,United States",miami:"Miami,Florida,United States",seattle:"Seattle,Washington,United States",denver:"Denver,Colorado,United States",loveland:"Loveland,Colorado,United States","loveland co":"Loveland,Colorado,United States","fort collins":"Fort Collins,Colorado,United States",boulder:"Boulder,Colorado,United States","colorado springs":"Colorado Springs,Colorado,United States",boston:"Boston,Massachusetts,United States",atlanta:"Atlanta,Georgia,United States","san francisco":"San Francisco,California,United States",sf:"San Francisco,California,United States",portland:"Portland,Oregon,United States","las vegas":"Las Vegas,Nevada,United States",minneapolis:"Minneapolis,Minnesota,United States",detroit:"Detroit,Michigan,United States",nashville:"Nashville,Tennessee,United States",charlotte:"Charlotte,North Carolina,United States",orlando:"Orlando,Florida,United States","san diego":"San Diego,California,United States",baltimore:"Baltimore,Maryland,United States",sacramento:"Sacramento,California,United States",columbus:"Columbus,Ohio,United States",indianapolis:"Indianapolis,Indiana,United States","san jose":"San Jose,California,United States","fort worth":"Fort Worth,Texas,United States",jacksonville:"Jacksonville,Florida,United States",memphis:"Memphis,Tennessee,United States",louisville:"Louisville,Kentucky,United States",raleigh:"Raleigh,North Carolina,United States",richmond:"Richmond,Virginia,United States","salt lake city":"Salt Lake City,Utah,United States",toronto:"Toronto,Ontario,Canada",vancouver:"Vancouver,British Columbia,Canada",montreal:"Montreal,Quebec,Canada",calgary:"Calgary,Alberta,Canada",ottawa:"Ottawa,Ontario,Canada",london:"London,England,United Kingdom",manchester:"Manchester,England,United Kingdom",birmingham:"Birmingham,England,United Kingdom",edinburgh:"Edinburgh,Scotland,United Kingdom",glasgow:"Glasgow,Scotland,United Kingdom",leeds:"Leeds,England,United Kingdom",sydney:"Sydney,New South Wales,Australia",melbourne:"Melbourne,Victoria,Australia",brisbane:"Brisbane,Queensland,Australia",perth:"Perth,Western Australia,Australia",adelaide:"Adelaide,South Australia,Australia",dublin:"Dublin,Leinster,Ireland"};var vs=`3, Nanjemoy,Maryland,United States
194
194
  Aaronsburg,Pennsylvania,United States
195
195
  Abbeville,Alabama,United States
196
196
  Abbeville,Georgia,United States
@@ -21882,8 +21882,8 @@ Zortman,Montana,United States
21882
21882
  Zuehl,Texas,United States
21883
21883
  Zumbrota,Minnesota,United States
21884
21884
  Zuni,New Mexico,United States
21885
- Zwolle,Louisiana,United States`,vs={ad:"Andorra",ae:"United Arab Emirates",af:"Afghanistan",ag:"Antigua and Barbuda",al:"Albania",am:"Armenia",ao:"Angola",aq:"Antarctica",ar:"Argentina",as:"American Samoa",at:"Austria",au:"Australia",az:"Azerbaijan",ba:"Bosnia and Herzegovina",bb:"Barbados",bd:"Bangladesh",be:"Belgium",bf:"Burkina Faso",bg:"Bulgaria",bh:"Bahrain",bi:"Burundi",bj:"Benin",bl:"Saint Barthelemy",bn:"Brunei",bo:"Bolivia",bq:"Caribbean Netherlands",br:"Brazil",bs:"The Bahamas",bt:"Bhutan",bw:"Botswana",by:"Belarus",bz:"Belize",ca:"Canada",cc:"Cocos (Keeling) Islands",cd:"Democratic Republic of the Congo",cf:"Central African Republic",cg:"Republic of the Congo",ch:"Switzerland",ci:"Cote d'Ivoire",ck:"Cook Islands",cl:"Chile",cm:"Cameroon",cn:"China",co:"Colombia",cr:"Costa Rica",cv:"Cabo Verde",cw:"Curacao",cx:"Christmas Island",cy:"Cyprus",cz:"Czechia",de:"Germany",dj:"Djibouti",dk:"Denmark",dm:"Dominica",do:"Dominican Republic",dz:"Algeria",ec:"Ecuador",ee:"Estonia",eg:"Egypt",er:"Eritrea",es:"Spain",et:"Ethiopia",fi:"Finland",fj:"Fiji",fm:"Micronesia",fr:"France",ga:"Gabon",gb:"United Kingdom",gd:"Grenada",ge:"Georgia",gg:"Guernsey",gh:"Ghana",gm:"The Gambia",gn:"Guinea",gq:"Equatorial Guinea",gr:"Greece",gs:"South Georgia and the South Sandwich Islands",gt:"Guatemala",gu:"Guam",gw:"Guinea-Bissau",gy:"Guyana",hm:"Heard Island and McDonald Islands",hn:"Honduras",hr:"Croatia",ht:"Haiti",hu:"Hungary",id:"Indonesia",ie:"Ireland",il:"Israel",im:"Isle of Man",in:"India",iq:"Iraq",is:"Iceland",it:"Italy",je:"Jersey",jm:"Jamaica",jo:"Jordan",jp:"Japan",ke:"Kenya",kg:"Kyrgyzstan",kh:"Cambodia",ki:"Kiribati",km:"Comoros",kn:"Saint Kitts and Nevis",kr:"South Korea",kw:"Kuwait",kz:"Kazakhstan",la:"Laos",lb:"Lebanon",lc:"Saint Lucia",li:"Liechtenstein",lk:"Sri Lanka",lr:"Liberia",ls:"Lesotho",lt:"Lithuania",lu:"Luxembourg",lv:"Latvia",ly:"Libya",ma:"Morocco",mc:"Monaco",md:"Moldova",me:"Montenegro",mf:"Saint Martin",mg:"Madagascar",mh:"Marshall Islands",mk:"North Macedonia",ml:"Mali",mm:"Myanmar (Burma)",mn:"Mongolia",mp:"Northern Mariana Islands",mr:"Mauritania",mt:"Malta",mu:"Mauritius",mv:"Maldives",mw:"Malawi",mx:"Mexico",my:"Malaysia",mz:"Mozambique",na:"Namibia",nc:"New Caledonia",ne:"Niger",nf:"Norfolk Island",ng:"Nigeria",ni:"Nicaragua",nl:"Netherlands",no:"Norway",np:"Nepal",nr:"Nauru",nu:"Niue",nz:"New Zealand",om:"Oman",pa:"Panama",pe:"Peru",pf:"French Polynesia",pg:"Papua New Guinea",ph:"Philippines",pk:"Pakistan",pl:"Poland",pm:"Saint Pierre and Miquelon",pn:"Pitcairn Islands",pt:"Portugal",pw:"Palau",py:"Paraguay",qa:"Qatar",ro:"Romania",rs:"Serbia",ru:"Russia",rw:"Rwanda",sa:"Saudi Arabia",sb:"Solomon Islands",sc:"Seychelles",sd:"Sudan",se:"Sweden",sg:"Singapore",sh:"Saint Helena, Ascension and Tristan da Cunha",si:"Slovenia",sk:"Slovakia",sl:"Sierra Leone",sm:"San Marino",sn:"Senegal",so:"Somalia",sr:"Suriname",ss:"South Sudan",st:"Sao Tome and Principe",sv:"El Salvador",sx:"Sint Maarten",sy:"Syria",sz:"Eswatini",td:"Chad",tf:"French Southern and Antarctic Lands",tg:"Togo",th:"Thailand",tj:"Tajikistan",tk:"Tokelau",tl:"Timor-Leste",tm:"Turkmenistan",tn:"Tunisia",to:"Tonga",tr:"Turkiye",tt:"Trinidad and Tobago",tv:"Tuvalu",tz:"Tanzania",ua:"Ukraine",ug:"Uganda",um:"United States Minor Outlying Islands",us:"United States",uy:"Uruguay",uz:"Uzbekistan",va:"Vatican City",vc:"Saint Vincent and the Grenadines",ve:"Venezuela",vn:"Vietnam",vu:"Vanuatu",wf:"Wallis and Futuna",ws:"Samoa",ye:"Yemen",za:"South Africa",zm:"Zambia",zw:"Zimbabwe"};var Aa={AL:"Alabama",AK:"Alaska",AZ:"Arizona",AR:"Arkansas",CA:"California",CO:"Colorado",CT:"Connecticut",DE:"Delaware",DC:"District of Columbia",FL:"Florida",GA:"Georgia",HI:"Hawaii",ID:"Idaho",IL:"Illinois",IN:"Indiana",IA:"Iowa",KS:"Kansas",KY:"Kentucky",LA:"Louisiana",ME:"Maine",MD:"Maryland",MA:"Massachusetts",MI:"Michigan",MN:"Minnesota",MS:"Mississippi",MO:"Missouri",MT:"Montana",NE:"Nebraska",NV:"Nevada",NH:"New Hampshire",NJ:"New Jersey",NM:"New Mexico",NY:"New York",NC:"North Carolina",ND:"North Dakota",OH:"Ohio",OK:"Oklahoma",OR:"Oregon",PA:"Pennsylvania",PR:"Puerto Rico",RI:"Rhode Island",SC:"South Carolina",SD:"South Dakota",TN:"Tennessee",TX:"Texas",UT:"Utah",VT:"Vermont",VA:"Virginia",WA:"Washington",WV:"West Virginia",WI:"Wisconsin",WY:"Wyoming"};var La=null,_s=null;function Ld(){if(La)return;let e=new Map,t=new Map;for(let n of Ns.split(`
21886
- `)){let a=n.trim();if(!a)continue;let i=a.toLowerCase();e.has(i)||e.set(i,a);let s=i.split(",",1)[0],o=t.get(s);o?o.push(a):t.set(s,[a])}for(let n of Object.values(vs)){let a=String(n).toLowerCase();e.has(a)||e.set(a,String(n))}La=e,_s=t}function xd(e){let t=[],n=e;do{let a=n&127;n>>>=7,n>0&&(a|=128),t.push(a)}while(n>0);return t}function Es(e){let t=Buffer.from(e,"utf8");return`w+${Buffer.concat([Buffer.from([8,2,16,32,34]),Buffer.from(xd(t.length)),t]).toString("base64")}`}function bs(){return{canonical:null,matched:!1,source:"unresolved",ambiguousCandidates:0}}function Pt(e,t){return{canonical:e,matched:!0,source:t,ambiguousCandidates:0}}function xa(e){let t=e.toLowerCase().trim();if(!t)return bs();if($t[t])return Pt($t[t],"alias");Ld();let n=La,a=_s,i=n.get(t);if(i)return Pt(i,"exact");let s=n.get(`${t},united states`);if(s)return Pt(s,"state-or-country");let o=t.split(",");if(o.length>=2){let r=o[0].trim(),S=o.slice(1).join(",").trim(),c=Aa[S.toUpperCase()]??S,h=n.get(`${r},${c.toLowerCase()},united states`);if(h)return Pt(h,"city-state")}let d=t.match(/^(.*?)[\s,]+([a-z]{2})$/);if(d){let r=Aa[d[2].toUpperCase()];if(r){let S=n.get(`${d[1].trim()},${r.toLowerCase()},united states`);if(S)return Pt(S,"city-state")}}let l=a.get(t);if(l?.length===1)return Pt(l[0],"city-unique");if(l&&l.length>1)return{canonical:null,matched:!1,source:"unresolved",ambiguousCandidates:l.length};let U=o[0].trim();if(U!==t&&$t[U])return Pt($t[U],"alias-partial");let u=t.replace(/\s+[a-z]{2}$/,"").trim();return u!==t&&$t[u]?Pt($t[u],"alias-partial"):bs()}function Ps(e){return xa(e).canonical??e}var Oa={alabama:"AL",alaska:"AK",arizona:"AZ",arkansas:"AR",california:"CA",colorado:"CO",connecticut:"CT",delaware:"DE",florida:"FL",georgia:"GA",hawaii:"HI",idaho:"ID",illinois:"IL",indiana:"IN",iowa:"IA",kansas:"KS",kentucky:"KY",louisiana:"LA",maine:"ME",maryland:"MD",massachusetts:"MA",michigan:"MI",minnesota:"MN",mississippi:"MS",missouri:"MO",montana:"MT",nebraska:"NE",nevada:"NV","new hampshire":"NH","new jersey":"NJ","new mexico":"NM","new york":"NY","north carolina":"NC","north dakota":"ND",ohio:"OH",oklahoma:"OK",oregon:"OR",pennsylvania:"PA","rhode island":"RI","south carolina":"SC","south dakota":"SD",tennessee:"TN",texas:"TX",utah:"UT",vermont:"VT",virginia:"VA",washington:"WA","west virginia":"WV",wisconsin:"WI",wyoming:"WY","district of columbia":"DC"},Od=[...Object.keys(Oa).map(e=>e.replace(/\s+/g,"\\s+")),...Object.values(Oa)].join("|"),Rd=new RegExp(`\\b([A-Z][A-Za-z]+(?:[\\s.-][A-Z][A-Za-z]+){0,4}),?\\s+(${Od})\\b`,"gi");function As(e){if(!e)return null;let t=e.trim();return/^[A-Z]{2}$/i.test(t)?t.toUpperCase():Oa[t.toLowerCase()]??null}function Ls(e){return e.replace(/\s+/g," ").trim().replace(/^.*\b(?:in|near|around|serving)\s+/i,"").toLowerCase().replace(/\b[a-z]/g,n=>n.toUpperCase())}function Wd(e){if(!e)return null;let[t="",n=""]=e.split(",").map(a=>a.trim());return{city:Ls(t),regionCode:As(n),canonicalLocation:e}}function Hd(e,t,n,a){let i=Ls(t),s=As(n);if(!i||!s)return;let o=`${i.toLowerCase()}|${s}`,d=e.get(o);if(d){d.count++,d.examples.length<3&&!d.examples.includes(a)&&d.examples.push(a);return}e.set(o,{city:i,regionCode:s,count:1,examples:[a]})}function Bd(e){try{return decodeURIComponent(e)}catch{}try{return decodeURIComponent(e.replace(/%(?![0-9a-fA-F]{2})/g,"%25"))}catch{return e}}function Is(e,t){let n=Bd(t).replace(/[+/|_-]+/g," ");for(let a of n.matchAll(Rd))Hd(e,a[1]??"",a[2]??"",n.slice(0,180))}function Qe(e,t,n){let a=Wd(e),i=new Map;for(let d of t)Is(i,[d.title,d.snippet??"",d.cite??"",d.url].join(" "));for(let d of n)Is(i,[d.name,...d.metadata,d.websiteUrl??"",d.directionsUrl??""].join(" "));let s=Array.from(i.values()).sort((d,l)=>l.count-d.count||d.city.localeCompare(l.city)).slice(0,8);return a?s.length===0?{status:"unknown",expected:a,candidates:[]}:{status:s.some(d=>d.city.toLowerCase()===a.city.toLowerCase()&&(a.regionCode==null||d.regionCode===a.regionCode))?"matched":"mismatch",expected:a,candidates:s}:{status:"not_requested",expected:null,candidates:s}}var Dd=[/An AI Overview is not available for this search/gi,/Can't generate an AI overview right now\.?\s*Try again later\.?/gi,/\bAI Overview\b/gi,/\bView all\b/gi],Fd=[/\bRelated Links\b/i,/\bAsk anything in\s*AI Mode\b/i,/\bAI can make mistakes\b/i,/\bThis is for informational purposes only\b/i,/\bShow more\b/i,/\b\d+\s+sites\b/i,/\b\d{1,2}\s*[msh]\s*[A-Z][A-Za-z]/,/\b(?:YouTube|Reddit|Facebook|Instagram|TikTok)·/];function On(e){return e.replace(/\u00a0/g," ").replace(/([.!?])([A-Z])/g,"$1 $2").replace(/([:;])([A-Z])/g,"$1 $2").replace(/([a-z])([A-Z][a-z])/g,"$1 $2").replace(/(\d)([A-Z][a-z])/g,"$1 $2").replace(/([a-z])(\d)/g,"$1 $2").replace(/\s+/g," ").trim()}function Gd(e){let t=-1;for(let n of Fd){let a=n.exec(e);n.lastIndex=0,a&&(t===-1||a.index<t)&&(t=a.index)}return t===-1?e:e.slice(0,t)}function Vd(e,t){let n=t?.trim();if(!n||n.length<8)return e;let a=e.toLowerCase().indexOf(n.toLowerCase());return a>40?e.slice(0,a):e}function Yd(e){let t=e.match(/[•·]\s*(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[a-z]*\.?\s+\d{1,2},\s+\d{4}/i);if(t?.index&&t.index>40)return t.index;let n=Math.max(0,e.length-260),a=e.slice(n),i=[...a.matchAll(/[.!?]\s*(?=[A-Z][A-Za-z0-9"'$])/g)];for(let o of i){let d=a.slice(o.index+1).trim(),l=d.slice(0,160),U=/^(?:Best|Top|What|How|Why|When|Where|Which|Can|Should|Is|Are|Do|Does)\b/i.test(d);if(d.length>20&&U&&/(?:\s[-|]\s|Heating|Cooling|Company|Services|Blog|Guide|Review)/i.test(l))return n+o.index+1}let s=i.at(-1);return s?.index!==void 0?n+s.index+1:e.length}function Kd(e){let t=e.match(/https?:\/\/\S+/i);if(!t?.index)return e;let n=e.slice(0,t.index);return n.slice(0,Yd(n))}function Jd(e){if(e.length<=1200)return e;let t=e.slice(0,1200),n=Math.max(t.lastIndexOf("."),t.lastIndexOf("!"),t.lastIndexOf("?"));return(n>240?t.slice(0,n+1):t).trim()}function xs(e,t,n){if(!e)return;let a=On(e),i=t?On(t):"";if(i&&a.toLowerCase().startsWith(i.toLowerCase())&&(a=a.slice(i.length).trim()),!/^An error has occurred\.?\s*Please try again later\.?/i.test(a)){for(let s of Dd)a=a.replace(s," ");if(a=a.replace(/\b[A-Z][A-Za-z&'\u2019 -]{2,60}\+\d+\b/g," ").replace(/\b(?:[a-z0-9-]+\.)+[a-z]{2,}\+\d+\b/gi," "),a=On(a),a=Gd(a),a=Vd(a,n),a=Kd(a),a=On(a),a=a.replace(/\s+(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[a-z]*\.?\s+\d{1,2},\s+\d{4}$/i,"").trim(),a=Jd(a),!!a&&!/^An error has occurred\.?\s*Please try again later\.?$/i.test(a))return a}}async function Os(e){let t=e??{aio:{root:"[data-lhcontainer][data-streaming-container][eid]",legacyRoot:'[data-hveid="CBMQAA"]',wrapper:".Fgyi2e",controller:'[jscontroller="AkrxPe"]',contentSubtree:'[data-subtree="mfc"]',heading:".Fzsovc.cwYVJe.RJPOee",header:".heWuVc",showMoreButton:'[aria-label="Show more AI Overview"]',sourcesPanel:".OZ9ddf.WAUd4",disclaimer:".DuQANe.MSJHRb"},aim:{root:'[data-hveid="CAUQAA"]',wrapper:".Fgyi2e"},expandWaitMs:1500},n=window.google?.sn??"unknown",a=n==="aim"?"aim":n==="web"?"web":"unknown";function i(y){return y?(y.innerText??y.textContent??"").trim():""}function s(y){if(!y)return!1;let N=y.replace(/\s+/g," ").trim();return N.length>=16&&N.split(" ").filter(Boolean).length>=2}function o(y){let N=y.querySelector(t.aio.heading);if(i(N)==="AI Overview")return!0;let p=y.querySelector(t.aio.header);return!!i(p).split(/\n|\s{2,}/).some(v=>v.trim()==="AI Overview")}function d(){let N=Array.from(document.querySelectorAll(t.aio.root)).find(o);if(N)return N;if(t.aio.legacyRoot){let v=document.querySelector(t.aio.legacyRoot);if(v&&o(v))return v}let p=document.querySelectorAll(`${t.aio.heading}, h1, h2, h3, [role="heading"]`);for(let v of p){if(i(v)!=="AI Overview")continue;let _=v.parentElement;for(let I=0;I<8&&_;I++){if(_.matches(t.aio.root)||_.querySelector(t.aio.controller)||_.querySelector(t.aio.contentSubtree))return _;_=_.parentElement}return v.parentElement}return null}function l(y){if(!y)return null;let N=y.cloneNode(!0);N.querySelectorAll(["script","style","noscript","img","picture","video",t.aio.header,t.aio.showMoreButton,t.aio.sourcesPanel,t.aio.disclaimer,'[data-subtree="dfa"]',"[data-src-id]",'[role="dialog"]',".HWMcu",".bTFeG",".CyMdWb",".MFrAxb",".F0OfWd.hfWAgb",".x2qcTc.fZavHb",".SvjEff",".sR2MY",".lKuDef",".GSPQcc","a[href]","button",'[role="button"]'].join(",")).forEach(W=>W.remove());let p=document.createElement("div");p.style.position="fixed",p.style.left="-10000px",p.style.top="0",p.style.width=`${Math.max(320,Math.round(y.getBoundingClientRect?.().width||960))}px`,p.style.opacity="0",p.style.pointerEvents="none",p.append(N),document.body.append(p);let v=N.innerText||N.textContent||"";p.remove();let _=v.replace(/\r/g,"").replace(/[ \t]+\n/g,`
21885
+ Zwolle,Louisiana,United States`,bs={ad:"Andorra",ae:"United Arab Emirates",af:"Afghanistan",ag:"Antigua and Barbuda",al:"Albania",am:"Armenia",ao:"Angola",aq:"Antarctica",ar:"Argentina",as:"American Samoa",at:"Austria",au:"Australia",az:"Azerbaijan",ba:"Bosnia and Herzegovina",bb:"Barbados",bd:"Bangladesh",be:"Belgium",bf:"Burkina Faso",bg:"Bulgaria",bh:"Bahrain",bi:"Burundi",bj:"Benin",bl:"Saint Barthelemy",bn:"Brunei",bo:"Bolivia",bq:"Caribbean Netherlands",br:"Brazil",bs:"The Bahamas",bt:"Bhutan",bw:"Botswana",by:"Belarus",bz:"Belize",ca:"Canada",cc:"Cocos (Keeling) Islands",cd:"Democratic Republic of the Congo",cf:"Central African Republic",cg:"Republic of the Congo",ch:"Switzerland",ci:"Cote d'Ivoire",ck:"Cook Islands",cl:"Chile",cm:"Cameroon",cn:"China",co:"Colombia",cr:"Costa Rica",cv:"Cabo Verde",cw:"Curacao",cx:"Christmas Island",cy:"Cyprus",cz:"Czechia",de:"Germany",dj:"Djibouti",dk:"Denmark",dm:"Dominica",do:"Dominican Republic",dz:"Algeria",ec:"Ecuador",ee:"Estonia",eg:"Egypt",er:"Eritrea",es:"Spain",et:"Ethiopia",fi:"Finland",fj:"Fiji",fm:"Micronesia",fr:"France",ga:"Gabon",gb:"United Kingdom",gd:"Grenada",ge:"Georgia",gg:"Guernsey",gh:"Ghana",gm:"The Gambia",gn:"Guinea",gq:"Equatorial Guinea",gr:"Greece",gs:"South Georgia and the South Sandwich Islands",gt:"Guatemala",gu:"Guam",gw:"Guinea-Bissau",gy:"Guyana",hm:"Heard Island and McDonald Islands",hn:"Honduras",hr:"Croatia",ht:"Haiti",hu:"Hungary",id:"Indonesia",ie:"Ireland",il:"Israel",im:"Isle of Man",in:"India",iq:"Iraq",is:"Iceland",it:"Italy",je:"Jersey",jm:"Jamaica",jo:"Jordan",jp:"Japan",ke:"Kenya",kg:"Kyrgyzstan",kh:"Cambodia",ki:"Kiribati",km:"Comoros",kn:"Saint Kitts and Nevis",kr:"South Korea",kw:"Kuwait",kz:"Kazakhstan",la:"Laos",lb:"Lebanon",lc:"Saint Lucia",li:"Liechtenstein",lk:"Sri Lanka",lr:"Liberia",ls:"Lesotho",lt:"Lithuania",lu:"Luxembourg",lv:"Latvia",ly:"Libya",ma:"Morocco",mc:"Monaco",md:"Moldova",me:"Montenegro",mf:"Saint Martin",mg:"Madagascar",mh:"Marshall Islands",mk:"North Macedonia",ml:"Mali",mm:"Myanmar (Burma)",mn:"Mongolia",mp:"Northern Mariana Islands",mr:"Mauritania",mt:"Malta",mu:"Mauritius",mv:"Maldives",mw:"Malawi",mx:"Mexico",my:"Malaysia",mz:"Mozambique",na:"Namibia",nc:"New Caledonia",ne:"Niger",nf:"Norfolk Island",ng:"Nigeria",ni:"Nicaragua",nl:"Netherlands",no:"Norway",np:"Nepal",nr:"Nauru",nu:"Niue",nz:"New Zealand",om:"Oman",pa:"Panama",pe:"Peru",pf:"French Polynesia",pg:"Papua New Guinea",ph:"Philippines",pk:"Pakistan",pl:"Poland",pm:"Saint Pierre and Miquelon",pn:"Pitcairn Islands",pt:"Portugal",pw:"Palau",py:"Paraguay",qa:"Qatar",ro:"Romania",rs:"Serbia",ru:"Russia",rw:"Rwanda",sa:"Saudi Arabia",sb:"Solomon Islands",sc:"Seychelles",sd:"Sudan",se:"Sweden",sg:"Singapore",sh:"Saint Helena, Ascension and Tristan da Cunha",si:"Slovenia",sk:"Slovakia",sl:"Sierra Leone",sm:"San Marino",sn:"Senegal",so:"Somalia",sr:"Suriname",ss:"South Sudan",st:"Sao Tome and Principe",sv:"El Salvador",sx:"Sint Maarten",sy:"Syria",sz:"Eswatini",td:"Chad",tf:"French Southern and Antarctic Lands",tg:"Togo",th:"Thailand",tj:"Tajikistan",tk:"Tokelau",tl:"Timor-Leste",tm:"Turkmenistan",tn:"Tunisia",to:"Tonga",tr:"Turkiye",tt:"Trinidad and Tobago",tv:"Tuvalu",tz:"Tanzania",ua:"Ukraine",ug:"Uganda",um:"United States Minor Outlying Islands",us:"United States",uy:"Uruguay",uz:"Uzbekistan",va:"Vatican City",vc:"Saint Vincent and the Grenadines",ve:"Venezuela",vn:"Vietnam",vu:"Vanuatu",wf:"Wallis and Futuna",ws:"Samoa",ye:"Yemen",za:"South Africa",zm:"Zambia",zw:"Zimbabwe"};var Aa={AL:"Alabama",AK:"Alaska",AZ:"Arizona",AR:"Arkansas",CA:"California",CO:"Colorado",CT:"Connecticut",DE:"Delaware",DC:"District of Columbia",FL:"Florida",GA:"Georgia",HI:"Hawaii",ID:"Idaho",IL:"Illinois",IN:"Indiana",IA:"Iowa",KS:"Kansas",KY:"Kentucky",LA:"Louisiana",ME:"Maine",MD:"Maryland",MA:"Massachusetts",MI:"Michigan",MN:"Minnesota",MS:"Mississippi",MO:"Missouri",MT:"Montana",NE:"Nebraska",NV:"Nevada",NH:"New Hampshire",NJ:"New Jersey",NM:"New Mexico",NY:"New York",NC:"North Carolina",ND:"North Dakota",OH:"Ohio",OK:"Oklahoma",OR:"Oregon",PA:"Pennsylvania",PR:"Puerto Rico",RI:"Rhode Island",SC:"South Carolina",SD:"South Dakota",TN:"Tennessee",TX:"Texas",UT:"Utah",VT:"Vermont",VA:"Virginia",WA:"Washington",WV:"West Virginia",WI:"Wisconsin",WY:"Wyoming"};var La=null,Es=null;function Od(){if(La)return;let e=new Map,t=new Map;for(let n of vs.split(`
21886
+ `)){let a=n.trim();if(!a)continue;let i=a.toLowerCase();e.has(i)||e.set(i,a);let s=i.split(",",1)[0],o=t.get(s);o?o.push(a):t.set(s,[a])}for(let n of Object.values(bs)){let a=String(n).toLowerCase();e.has(a)||e.set(a,String(n))}La=e,Es=t}function Rd(e){let t=[],n=e;do{let a=n&127;n>>>=7,n>0&&(a|=128),t.push(a)}while(n>0);return t}function Ps(e){let t=Buffer.from(e,"utf8");return`w+${Buffer.concat([Buffer.from([8,2,16,32,34]),Buffer.from(Rd(t.length)),t]).toString("base64")}`}function _s(){return{canonical:null,matched:!1,source:"unresolved",ambiguousCandidates:0}}function Pt(e,t){return{canonical:e,matched:!0,source:t,ambiguousCandidates:0}}function xa(e){let t=e.toLowerCase().trim();if(!t)return _s();if($t[t])return Pt($t[t],"alias");Od();let n=La,a=Es,i=n.get(t);if(i)return Pt(i,"exact");let s=n.get(`${t},united states`);if(s)return Pt(s,"state-or-country");let o=t.split(",");if(o.length>=2){let r=o[0].trim(),S=o.slice(1).join(",").trim(),c=Aa[S.toUpperCase()]??S,h=n.get(`${r},${c.toLowerCase()},united states`);if(h)return Pt(h,"city-state")}let d=t.match(/^(.*?)[\s,]+([a-z]{2})$/);if(d){let r=Aa[d[2].toUpperCase()];if(r){let S=n.get(`${d[1].trim()},${r.toLowerCase()},united states`);if(S)return Pt(S,"city-state")}}let l=a.get(t);if(l?.length===1)return Pt(l[0],"city-unique");if(l&&l.length>1)return{canonical:null,matched:!1,source:"unresolved",ambiguousCandidates:l.length};let U=o[0].trim();if(U!==t&&$t[U])return Pt($t[U],"alias-partial");let u=t.replace(/\s+[a-z]{2}$/,"").trim();return u!==t&&$t[u]?Pt($t[u],"alias-partial"):_s()}function Is(e){return xa(e).canonical??e}var Oa={alabama:"AL",alaska:"AK",arizona:"AZ",arkansas:"AR",california:"CA",colorado:"CO",connecticut:"CT",delaware:"DE",florida:"FL",georgia:"GA",hawaii:"HI",idaho:"ID",illinois:"IL",indiana:"IN",iowa:"IA",kansas:"KS",kentucky:"KY",louisiana:"LA",maine:"ME",maryland:"MD",massachusetts:"MA",michigan:"MI",minnesota:"MN",mississippi:"MS",missouri:"MO",montana:"MT",nebraska:"NE",nevada:"NV","new hampshire":"NH","new jersey":"NJ","new mexico":"NM","new york":"NY","north carolina":"NC","north dakota":"ND",ohio:"OH",oklahoma:"OK",oregon:"OR",pennsylvania:"PA","rhode island":"RI","south carolina":"SC","south dakota":"SD",tennessee:"TN",texas:"TX",utah:"UT",vermont:"VT",virginia:"VA",washington:"WA","west virginia":"WV",wisconsin:"WI",wyoming:"WY","district of columbia":"DC"},Wd=[...Object.keys(Oa).map(e=>e.replace(/\s+/g,"\\s+")),...Object.values(Oa)].join("|"),Hd=new RegExp(`\\b([A-Z][A-Za-z]+(?:[\\s.-][A-Z][A-Za-z]+){0,4}),?\\s+(${Wd})\\b`,"gi");function Ls(e){if(!e)return null;let t=e.trim();return/^[A-Z]{2}$/i.test(t)?t.toUpperCase():Oa[t.toLowerCase()]??null}function xs(e){return e.replace(/\s+/g," ").trim().replace(/^.*\b(?:in|near|around|serving)\s+/i,"").toLowerCase().replace(/\b[a-z]/g,n=>n.toUpperCase())}function Bd(e){if(!e)return null;let[t="",n=""]=e.split(",").map(a=>a.trim());return{city:xs(t),regionCode:Ls(n),canonicalLocation:e}}function Dd(e,t,n,a){let i=xs(t),s=Ls(n);if(!i||!s)return;let o=`${i.toLowerCase()}|${s}`,d=e.get(o);if(d){d.count++,d.examples.length<3&&!d.examples.includes(a)&&d.examples.push(a);return}e.set(o,{city:i,regionCode:s,count:1,examples:[a]})}function Fd(e){try{return decodeURIComponent(e)}catch{}try{return decodeURIComponent(e.replace(/%(?![0-9a-fA-F]{2})/g,"%25"))}catch{return e}}function As(e,t){let n=Fd(t).replace(/[+/|_-]+/g," ");for(let a of n.matchAll(Hd))Dd(e,a[1]??"",a[2]??"",n.slice(0,180))}function Qe(e,t,n){let a=Bd(e),i=new Map;for(let d of t)As(i,[d.title,d.snippet??"",d.cite??"",d.url].join(" "));for(let d of n)As(i,[d.name,...d.metadata,d.websiteUrl??"",d.directionsUrl??""].join(" "));let s=Array.from(i.values()).sort((d,l)=>l.count-d.count||d.city.localeCompare(l.city)).slice(0,8);return a?s.length===0?{status:"unknown",expected:a,candidates:[]}:{status:s.some(d=>d.city.toLowerCase()===a.city.toLowerCase()&&(a.regionCode==null||d.regionCode===a.regionCode))?"matched":"mismatch",expected:a,candidates:s}:{status:"not_requested",expected:null,candidates:s}}var Gd=[/An AI Overview is not available for this search/gi,/Can't generate an AI overview right now\.?\s*Try again later\.?/gi,/\bAI Overview\b/gi,/\bView all\b/gi],Vd=[/\bRelated Links\b/i,/\bAsk anything in\s*AI Mode\b/i,/\bAI can make mistakes\b/i,/\bThis is for informational purposes only\b/i,/\bShow more\b/i,/\b\d+\s+sites\b/i,/\b\d{1,2}\s*[msh]\s*[A-Z][A-Za-z]/,/\b(?:YouTube|Reddit|Facebook|Instagram|TikTok)·/];function Rn(e){return e.replace(/\u00a0/g," ").replace(/([.!?])([A-Z])/g,"$1 $2").replace(/([:;])([A-Z])/g,"$1 $2").replace(/([a-z])([A-Z][a-z])/g,"$1 $2").replace(/(\d)([A-Z][a-z])/g,"$1 $2").replace(/([a-z])(\d)/g,"$1 $2").replace(/\s+/g," ").trim()}function Yd(e){let t=-1;for(let n of Vd){let a=n.exec(e);n.lastIndex=0,a&&(t===-1||a.index<t)&&(t=a.index)}return t===-1?e:e.slice(0,t)}function Kd(e,t){let n=t?.trim();if(!n||n.length<8)return e;let a=e.toLowerCase().indexOf(n.toLowerCase());return a>40?e.slice(0,a):e}function Jd(e){let t=e.match(/[•·]\s*(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[a-z]*\.?\s+\d{1,2},\s+\d{4}/i);if(t?.index&&t.index>40)return t.index;let n=Math.max(0,e.length-260),a=e.slice(n),i=[...a.matchAll(/[.!?]\s*(?=[A-Z][A-Za-z0-9"'$])/g)];for(let o of i){let d=a.slice(o.index+1).trim(),l=d.slice(0,160),U=/^(?:Best|Top|What|How|Why|When|Where|Which|Can|Should|Is|Are|Do|Does)\b/i.test(d);if(d.length>20&&U&&/(?:\s[-|]\s|Heating|Cooling|Company|Services|Blog|Guide|Review)/i.test(l))return n+o.index+1}let s=i.at(-1);return s?.index!==void 0?n+s.index+1:e.length}function qd(e){let t=e.match(/https?:\/\/\S+/i);if(!t?.index)return e;let n=e.slice(0,t.index);return n.slice(0,Jd(n))}function Xd(e){if(e.length<=1200)return e;let t=e.slice(0,1200),n=Math.max(t.lastIndexOf("."),t.lastIndexOf("!"),t.lastIndexOf("?"));return(n>240?t.slice(0,n+1):t).trim()}function Os(e,t,n){if(!e)return;let a=Rn(e),i=t?Rn(t):"";if(i&&a.toLowerCase().startsWith(i.toLowerCase())&&(a=a.slice(i.length).trim()),!/^An error has occurred\.?\s*Please try again later\.?/i.test(a)){for(let s of Gd)a=a.replace(s," ");if(a=a.replace(/\b[A-Z][A-Za-z&'\u2019 -]{2,60}\+\d+\b/g," ").replace(/\b(?:[a-z0-9-]+\.)+[a-z]{2,}\+\d+\b/gi," "),a=Rn(a),a=Yd(a),a=Kd(a,n),a=qd(a),a=Rn(a),a=a.replace(/\s+(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)[a-z]*\.?\s+\d{1,2},\s+\d{4}$/i,"").trim(),a=Xd(a),!!a&&!/^An error has occurred\.?\s*Please try again later\.?$/i.test(a))return a}}async function Rs(e){let t=e??{aio:{root:"[data-lhcontainer][data-streaming-container][eid]",legacyRoot:'[data-hveid="CBMQAA"]',wrapper:".Fgyi2e",controller:'[jscontroller="AkrxPe"]',contentSubtree:'[data-subtree="mfc"]',heading:".Fzsovc.cwYVJe.RJPOee",header:".heWuVc",showMoreButton:'[aria-label="Show more AI Overview"]',sourcesPanel:".OZ9ddf.WAUd4",disclaimer:".DuQANe.MSJHRb"},aim:{root:'[data-hveid="CAUQAA"]',wrapper:".Fgyi2e"},expandWaitMs:1500},n=window.google?.sn??"unknown",a=n==="aim"?"aim":n==="web"?"web":"unknown";function i(y){return y?(y.innerText??y.textContent??"").trim():""}function s(y){if(!y)return!1;let N=y.replace(/\s+/g," ").trim();return N.length>=16&&N.split(" ").filter(Boolean).length>=2}function o(y){let N=y.querySelector(t.aio.heading);if(i(N)==="AI Overview")return!0;let p=y.querySelector(t.aio.header);return!!i(p).split(/\n|\s{2,}/).some(v=>v.trim()==="AI Overview")}function d(){let N=Array.from(document.querySelectorAll(t.aio.root)).find(o);if(N)return N;if(t.aio.legacyRoot){let v=document.querySelector(t.aio.legacyRoot);if(v&&o(v))return v}let p=document.querySelectorAll(`${t.aio.heading}, h1, h2, h3, [role="heading"]`);for(let v of p){if(i(v)!=="AI Overview")continue;let _=v.parentElement;for(let I=0;I<8&&_;I++){if(_.matches(t.aio.root)||_.querySelector(t.aio.controller)||_.querySelector(t.aio.contentSubtree))return _;_=_.parentElement}return v.parentElement}return null}function l(y){if(!y)return null;let N=y.cloneNode(!0);N.querySelectorAll(["script","style","noscript","img","picture","video",t.aio.header,t.aio.showMoreButton,t.aio.sourcesPanel,t.aio.disclaimer,'[data-subtree="dfa"]',"[data-src-id]",'[role="dialog"]',".HWMcu",".bTFeG",".CyMdWb",".MFrAxb",".F0OfWd.hfWAgb",".x2qcTc.fZavHb",".SvjEff",".sR2MY",".lKuDef",".GSPQcc","a[href]","button",'[role="button"]'].join(",")).forEach(W=>W.remove());let p=document.createElement("div");p.style.position="fixed",p.style.left="-10000px",p.style.top="0",p.style.width=`${Math.max(320,Math.round(y.getBoundingClientRect?.().width||960))}px`,p.style.opacity="0",p.style.pointerEvents="none",p.append(N),document.body.append(p);let v=N.innerText||N.textContent||"";p.remove();let _=v.replace(/\r/g,"").replace(/[ \t]+\n/g,`
21887
21887
  `).replace(/\n[ \t]+/g,`
21888
21888
  `).replace(/\n{3,}/g,`
21889
21889
 
@@ -21892,14 +21892,14 @@ Zwolle,Louisiana,United States`,vs={ad:"Andorra",ae:"United Arab Emirates",af:"A
21892
21892
  `).replace(/\n{3,}/g,`
21893
21893
 
21894
21894
  `).trim();return!L||/not available|try again|can't generate/i.test(L)?null:L}function U(y){if(!y||y.startsWith("javascript:"))return null;try{let N=new URL(y,window.location.href);return!["http:","https:"].includes(N.protocol)||N.username||N.password?null:{href:N.href,rawHref:N.href}}catch{return null}}function u(y){if(!y)return[];let N=new Set,p=[];for(let v of Array.from(y.querySelectorAll("a[href]"))){let _=U(v.getAttribute("href")??"");if(!_||N.has(_.rawHref))continue;N.add(_.rawHref);let I="";try{I=new URL(_.href).hostname.replace(/^www\./,"")}catch{}p.push({text:i(v)||I||_.href,href:_.href,rawHref:_.rawHref})}return p}async function r(y){let N=y.querySelector(t.aio.showMoreButton);if(!N||N.getAttribute("aria-expanded")!=="false")return!1;N.click();let p=t.expandWaitMs??1500;return p>0&&await new Promise(v=>setTimeout(v,p)),!0}let S=d(),c=null,h=[],m=!1,k=!1,f=[];if(S){m=await r(S);let y=S.querySelector(t.aio.controller),N=S.querySelector(t.aio.contentSubtree),p=S.querySelector(t.aio.showMoreButton);k=y?.getAttribute("data-trnct")==="false"||p?.getAttribute("aria-expanded")==="true"||!p,c=l(N??y??S),f=(c??"").split(`
21895
- `).map(v=>v.trim()).filter(v=>/^\d+\.\s+.+/.test(v)),h=u(S)}let C=document.querySelector(t.aim.root),A=a==="aim"&&!!C,R=C?.closest(t.aim.wrapper)??C,F=A?l(R):null,E=A&&s(F),M=E?F:null,P=E?u(R):[],x=!!S&&s(c);return{surface:a,aiOverview:{detected:x,text:x?c:null,citations:x?h:[],expanded:x?m:!1,fullyExpanded:x?k:!1,sections:x?f:[]},aiMode:{detected:E&&M!==null,text:M,citations:P}}}var Ws=require("crypto"),Hs=8e3,Bs=3e3,Ds=1e4,qd=8;function It(e){return/(^|\.)google\.(?:com|[a-z]{2,3}(?:\.[a-z]{2})?)$/i.test(e)}function tn(e){if(!e||e.length>Hs)return null;try{let t=new URL(e);return t.protocol!=="http:"&&t.protocol!=="https:"||t.username||t.password?null:t}catch{return null}}function Xd(e,t){if(!e||e.length>Hs||/^javascript:/i.test(e))return null;try{let n=new URL(e,t);return tn(n.href)}catch{return null}}function Ze(e){let t=tn(e);return!t||t.protocol!=="https:"||!It(t.hostname)||t.pathname!=="/goto"?null:t.searchParams.get("url")?t:null}function Ra(e){if(e.linkType!=="google_goto_redirect"||e.resolutionStatus!=="resolved"||!Ze(e.rawUrl)||!e.resolvedUrl)return!1;let t=tn(e.resolvedUrl);return!!(t&&!It(t.hostname)&&t.href===e.resolvedUrl&&e.url===t.href)}function Qt(e){let t=e.normalize("NFKC").toLocaleLowerCase("en-US").replace(/[^\p{L}\p{N}\s]/gu,"").replace(/\s+/g," ").trim();return`paa_${(0,Ws.createHash)("sha256").update(t).digest("hex").slice(0,16)}`}function Rn(e,t){let n=Xd(e,t);if(!n)return null;let a=n.href;if(!It(n.hostname))return{url:a,rawUrl:a,resolvedUrl:a,linkType:"plain",resolutionStatus:"not_needed"};if(n.pathname==="/url"){let i=tn(n.searchParams.get("q")??n.searchParams.get("url")??"");return!i||It(i.hostname)?null:{url:i.href,rawUrl:a,resolvedUrl:i.href,linkType:"google_url_redirect",resolutionStatus:"resolved"}}return n.pathname==="/goto"&&n.searchParams.get("url")?{url:a,rawUrl:a,resolvedUrl:null,linkType:"google_goto_redirect",resolutionStatus:"unresolved"}:null}function Wa(e){return Ze(e)?.searchParams.get("url")??null}function Fs(e,t){try{let n=new URL(e),a=tn(new URL(t,n).href);return!a||It(a.hostname)?null:(!a.hash&&n.hash&&(a.hash=n.hash),a.href)}catch{return null}}async function Gs(e,t={}){let n=[...new Set([...e].flatMap(r=>Ze(r)?[r]:[]))],a=new Map(n.map(r=>[r,{rawUrl:r,resolvedUrl:null,resolutionStatus:"unresolved"}])),i=typeof globalThis.fetch=="function"?globalThis.fetch.bind(globalThis):null,s=t.fetchImpl??i;if(n.length===0||!s)return{resolutions:[...a.values()],nonGoogleRequestsObserved:0};let o=Math.max(1,t.timeoutMs??Bs),d=Date.now()+Math.max(1,t.totalBudgetMs??Ds),l=Math.max(1,Math.min(16,Math.floor(t.concurrency??qd))),U=0,u=async()=>{for(;U<n.length;){let r=n[U++],S=Ze(r),c=S?.searchParams.get("url");if(!S||!c){a.set(r,{rawUrl:r,resolvedUrl:null,resolutionStatus:"rejected"});continue}S.hash="";let h=S.href;for(let m=0;m<2;m++){let k=d-Date.now();if(k<=0)return;let f=new AbortController,C=setTimeout(()=>f.abort(),Math.min(o,k)),A;try{A=await s(h,{method:"GET",redirect:"manual",headers:{accept:"*/*","user-agent":"Mozilla/5.0"},signal:f.signal})}catch{break}finally{clearTimeout(C)}if(A.status<300||A.status>=400)break;let R=A.headers.get("location");if(!R)break;let F=Fs(r,R);if(F){a.set(r,{rawUrl:r,resolvedUrl:F,resolutionStatus:"resolved"});break}let E;try{E=new URL(R,h)}catch{a.set(r,{rawUrl:r,resolvedUrl:null,resolutionStatus:"rejected"});break}if(!It(E.hostname)||Wa(E.href)!==c){a.set(r,{rawUrl:r,resolvedUrl:null,resolutionStatus:"rejected"});break}h=E.href}}};return await Promise.all(Array.from({length:Math.min(l,n.length)},()=>u())),{resolutions:[...a.values()],nonGoogleRequestsObserved:0}}function Rs(e,t){try{return new URL(e.resolvedUrl??e.url).hostname.replace(/^www\./,"")}catch{return t}}async function Vs(e,t={}){let n=[],a=r=>{for(let S of r)n.push(S),a(S.children??[])};a(e.tree??[]);let i=[...e.flat.flatMap(r=>r.sources??[]),...n.flatMap(r=>r.sources),...e.organicResults??[],...(e.aiOverview?.citations??[]).map(({text:r,href:S,...c})=>({...c,url:S})),...(e.aiMode?.citations??[]).map(({text:r,href:S,...c})=>({...c,url:S}))],s=new Map(i.filter(r=>!!r?.rawUrl).map(r=>[r.rawUrl,r])),o=[...s.values()].filter(r=>r.linkType==="google_goto_redirect"&&r.resolutionStatus!=="resolved").map(r=>r.rawUrl);if(o.length>0){let r=await Gs(o,t);for(let S of r.resolutions){let c=s.get(S.rawUrl);c&&s.set(S.rawUrl,S.resolutionStatus==="resolved"&&S.resolvedUrl?{...c,url:S.resolvedUrl,resolvedUrl:S.resolvedUrl,resolutionStatus:"resolved"}:{...c,resolutionStatus:S.resolutionStatus})}}let d=r=>({...r,...s.get(r.rawUrl)??r}),l=r=>r.map(S=>{let c=d(S);return{...c,site:Rs(c,S.site)}});for(let r of e.flat){r.sources=l(r.sources??[]);let S=r.sources[0];r.source_title=S?.title??r.source_title,r.source_site=S?.site??r.source_site,r.source_cite=S?.url??r.source_cite}for(let r of n){r.sources=l(r.sources??[]);let S=r.sources[0];r.sourceTitle=S?.title??r.sourceTitle,r.sourceSite=S?.site??r.sourceSite,r.sourceCite=S?.url??r.sourceCite}for(let r=0;r<(e.organicResults?.length??0);r++){let S=d(e.organicResults[r]);e.organicResults[r]={...S,domain:Rs(S,e.organicResults[r].domain)??""}}let U=r=>{let S=d({url:r.href,rawUrl:r.rawUrl,resolvedUrl:r.resolvedUrl,linkType:r.linkType,resolutionStatus:r.resolutionStatus}),{url:c,...h}=S;return{...r,...h,href:c}};e.aiOverview&&(e.aiOverview.citations=(e.aiOverview.citations??[]).map(U)),e.aiMode&&(e.aiMode.citations=(e.aiMode.citations??[]).map(U));let u=[...s.values()];return e.diagnostics.links={linkCandidatesObserved:s.size,plainLinksObserved:u.filter(r=>r.linkType==="plain").length,googleUrlRedirectsObserved:u.filter(r=>r.linkType==="google_url_redirect").length,googleGotoRedirectsObserved:u.filter(r=>r.linkType==="google_goto_redirect").length,redirectsResolved:u.filter(r=>r.linkType!=="plain"&&r.resolutionStatus==="resolved").length,redirectsUnresolved:u.filter(r=>r.resolutionStatus==="unresolved").length,redirectsRejected:u.filter(r=>r.resolutionStatus==="rejected").length,resolverNonGoogleRequestsObserved:0,paaSourcesPreserved:e.flat.reduce((r,S)=>r+S.sources.length,0),aiOverviewLabelValidated:e.aiOverview?.detected??!1},e}async function Ys(e,t,n={}){let a=new Map;for(let S of t){let c=a.get(S.rawUrl);c&&Ra(c)||a.set(S.rawUrl,S.linkType==="google_goto_redirect"&&!Ra(S)?{...S,url:S.rawUrl,resolvedUrl:null,resolutionStatus:"unresolved"}:S)}let i=[...a.values()].filter(S=>S.linkType==="google_goto_redirect"&&!Ra(S));if(i.length===0)return a;let s=Date.now()+(n.totalBudgetMs??Ds),o=await Gs(i.map(S=>S.rawUrl),n);for(let S of o.resolutions){let c=a.get(S.rawUrl);c&&a.set(S.rawUrl,S.resolutionStatus==="resolved"&&S.resolvedUrl?{...c,url:S.resolvedUrl,resolvedUrl:S.resolvedUrl,resolutionStatus:"resolved"}:{...c,resolutionStatus:S.resolutionStatus})}let d=i.filter(S=>a.get(S.rawUrl)?.resolutionStatus==="unresolved");if(d.length===0||n.browserFallback===!1||Date.now()>=s)return n.onNetworkAudit?.({nonGoogleRequestsObserved:0}),a;let l=n.timeoutMs??Bs,U=await e.context().newPage().catch(()=>null);if(!U)return n.onNetworkAudit?.({nonGoogleRequestsObserved:0}),a;let u=null,r=0;try{await U.route("**/*",async S=>{let c=S.request().url();try{It(new URL(c).hostname)||(r+=1)}catch{}let h=Wa(c);if(!u||h!==u.token){await S.abort("blockedbyclient").catch(()=>{});return}let m=u;try{let k=await S.fetch({maxRedirects:0,timeout:m.timeoutMs});m.location=k.headers().location??null}finally{await S.abort("blockedbyclient").catch(()=>{}),m.finish()}});for(let S of d){let c=s-Date.now();if(c<=0)break;let h=Ze(S.rawUrl),m=h?.searchParams.get("url");if(!h||!m){a.set(S.rawUrl,{...S,resolutionStatus:"rejected"});continue}let k=Math.min(l,c);h.hash="";let f=h.href,C=!1;for(let A=0;A<2;A++){let R,F=new Promise(y=>{R=y}),E=`mcp-goto-${Math.random().toString(36).slice(2)}`;u={token:m,timeoutMs:k,location:null,finish:R},await U.evaluate(({url:y,id:N})=>{let p=document.createElement("iframe");p.id=N,p.hidden=!0,p.src=y,document.body.append(p)},{url:f,id:E}).catch(()=>null),await Promise.race([F,new Promise(y=>setTimeout(y,k))]);let M=u.location;if(u=null,await U.evaluate(y=>document.getElementById(y)?.remove(),E).catch(()=>null),!M)break;let P=Fs(S.rawUrl,M);if(P){a.set(S.rawUrl,{...S,url:P,resolvedUrl:P,resolutionStatus:"resolved"});break}let x;try{x=new URL(M,f)}catch{C=!0;break}if(!It(x.hostname)||Wa(x.href)!==m){C=!0;break}f=x.href}C&&a.get(S.rawUrl)?.resolutionStatus!=="resolved"&&a.set(S.rawUrl,{...S,resolutionStatus:"rejected"})}}finally{u=null,await U.close().catch(()=>{}),n.onNetworkAudit?.({nonGoogleRequestsObserved:r})}return a}var Ks=String.raw`^(?:an error has occurred(?:[.!…]\s*)?(?:please try again later[.!…]?)?|loading|please wait|try again|something went wrong)[.!…\s]*$`,jd=700,zd=1e3,$d=1400,Js=900,Qd=3,qs=250,Wn=class{constructor(t,n,a,i){this.driver=t;this.reporter=n;this.onPaaProgress=a;this.onProviderSession=i}driver;reporter;onPaaProgress;onProviderSession;completeness={paaWithoutAnswer:0,paaWithoutSource:0,paaAnswersRecovered:0,aioShareCaptured:null};aioShareUrlEarly=null;linkDiagnostics=this.emptyLinkDiagnostics();collectedRows=[];partialContext=null;progressSequence=0;clickedQuestions=new Set;paaLifecycle=this.emptyPaaLifecycle(0);checkpointMaterial=null;interactionObservations=[];phaseTimings={};currentPhaseTimings(t){return{...this.phaseTimings,totalServerMs:Math.max(0,Date.now()-t)}}emptyPaaLifecycle(t){return{requestedQuestions:t,questionsFound:0,targetStatus:"not_met",discoveryStatus:"in_progress",materialStatus:"partial",completionReason:"PAA expansion is still running.",automaticRetries:0,retryBreakdown:{controlRetries:0,browserRetries:0,providerFallbacks:0,runtimeRecoveries:0,idempotentReplays:0},knownQuestions:0,processedQuestions:0,failedInteractions:0,noGrowthConfirmations:0,visibleControls:0,eligibleControls:0,dispatchedInteractions:0,confirmedInteractions:0,unconfirmedInteractions:0}}emptyLinkDiagnostics(){return{linkCandidatesObserved:0,plainLinksObserved:0,googleUrlRedirectsObserved:0,googleGotoRedirectsObserved:0,redirectsResolved:0,redirectsUnresolved:0,redirectsRejected:0,resolverNonGoogleRequestsObserved:0,paaSourcesPreserved:0,aiOverviewLabelValidated:!1}}mergePaaSources(t=[],n=[]){let a=new Map;for(let i of[...t,...n]){let s=a.get(i.rawUrl);if(!s){a.set(i.rawUrl,i);continue}let o=i.resolutionStatus==="resolved"&&s.resolutionStatus!=="resolved"?i:s;a.set(i.rawUrl,{...o,title:o.title??s.title??i.title,site:o.site??s.site??i.site})}return[...a.values()]}normalizeAICitations(t,n){let a=new Set;return n.flatMap(i=>{let s=Rn(i.rawHref??i.href,t);if(!s||a.has(s.rawUrl))return[];a.add(s.rawUrl);let{url:o,...d}=s;return[{...d,text:i.text,href:o}]})}async resolveMaterialLinks(t,n,a,i,s){let o=Date.now(),d=[...n.flatMap(h=>h.sources??[]),...a,...i.citations.map(({text:h,href:m,...k})=>({...k,url:m})),...s.citations.map(({text:h,href:m,...k})=>({...k,url:m}))],l=new Map(d.map(h=>[h.rawUrl,h])),U=0,u=await Ys(t,d,{onNetworkAudit:h=>{U=h.nonGoogleRequestsObserved}}),r=h=>({...h,...u.get(h.rawUrl)??h});for(let h of n){h.sources=(h.sources??[]).map(r);let m=h.sources[0];h.source_title=m?.title??h.source_title,h.source_site=m?.site??h.source_site,h.source_cite=m?.url??h.source_cite}for(let h=0;h<a.length;h++){let m=r(a[h]);if(m.resolvedUrl)try{m.domain=new URL(m.resolvedUrl).hostname.replace(/^www\./,"")}catch{}a[h]=m}let S=h=>{let m=r({url:h.href,rawUrl:h.rawUrl,resolvedUrl:h.resolvedUrl,linkType:h.linkType,resolutionStatus:h.resolutionStatus}),{url:k,...f}=m;return{...f,text:h.text,href:k}};i.citations=i.citations.map(S),s.citations=s.citations.map(S);let c=[...u.values()];this.linkDiagnostics={linkCandidatesObserved:l.size,plainLinksObserved:c.filter(h=>h.linkType==="plain").length,googleUrlRedirectsObserved:c.filter(h=>h.linkType==="google_url_redirect").length,googleGotoRedirectsObserved:c.filter(h=>h.linkType==="google_goto_redirect").length,redirectsResolved:c.filter(h=>h.linkType!=="plain"&&h.resolutionStatus==="resolved").length,redirectsUnresolved:c.filter(h=>h.resolutionStatus==="unresolved").length,redirectsRejected:c.filter(h=>h.resolutionStatus==="rejected").length,resolverNonGoogleRequestsObserved:U,paaSourcesPreserved:n.reduce((h,m)=>h+(m.sources??[]).length,0),aiOverviewLabelValidated:i.detected},this.phaseTimings.linkResolutionMs=(this.phaseTimings.linkResolutionMs??0)+(Date.now()-o)}normalizeQuestion(t){return t.toLowerCase().replace(/[^\w\s]/g,"").replace(/\s+/g," ").trim()}throwIfAborted(t){if(t?.aborted)throw t.reason instanceof DOMException&&t.reason.name==="TimeoutError"?t.reason:new ut}async throwIfCaptcha(t,n){if(await t.locator(K.captchaMarker).count().catch(()=>0)>0)throw new nt(`${n} returned a CAPTCHA \u2014 retrying with a fresh session.`)}async extractVisibleItems(t,n){let a=K,i=await t.evaluate(({selectors:o,onlyQuestion:d,answerPlaceholderPattern:l})=>{function U(r){if(!r)return"";let S=[];for(let c of r.childNodes)if(c.nodeType===Node.TEXT_NODE){let h=c.textContent?.trim();h&&S.push(h)}else{if(c.tagName==="STYLE"||c.tagName==="SCRIPT")continue;{let h=U(c);h&&S.push(h)}}return S.join(" ").replace(/\s+/g," ").trim()}function u(r){return(r??"").replace(/\s*\(\+\d+\)\s*-\s*View related links.*$/i,"").replace(/\s*-\s*Opens in new tab.*$/i,"").trim()}return Array.from(document.querySelectorAll(o.item)).filter(r=>!d||(r.getAttribute(o.itemDataQ)||r.getAttribute(o.itemDataInitQ)||r.querySelector(o.itemQuestionEl)?.innerText?.trim()||"")===d).map(r=>{let S=r.querySelector(o.clickTarget),c=r.classList.contains(o.expandedClass)||S?.getAttribute("aria-expanded")==="true",h=r.querySelector(o.answerContainer),k=h?.querySelector(o.aiAnswerBody)??h,f=U(k),C=new RegExp(l,"i").test(f),A=[...Array.from((k??r).querySelectorAll("a[href]")),...Array.from(r.querySelectorAll(o.aiCitation)),...Array.from(r.querySelectorAll(o.aiSourceCard))],R=!C&&(c&&f.length>=10||f.length>30&&A.some(P=>/^https?:\/\//.test(P.href))),F=R&&r.querySelector(o.sourceTitle)?.innerText?.trim()||void 0,E=R&&r.querySelector(o.sourceSite)?.innerText?.trim()||void 0,M=[];if(R){let P=new Set;for(let y of A)!/^https?:\/\//.test(y.href)||P.has(y.href)||(P.add(y.href),M.push({title:u(y.getAttribute("aria-label"))||(y.textContent||"").trim(),rawHref:y.href}));let x=M.find(y=>y.title.length>2)??M[0];x&&(F=F||x.title||void 0)}return{googleLinkId:r.getAttribute("data-lk")||r.querySelector("[data-lk]")?.getAttribute("data-lk")||null,googleEvidenceId:r.getAttribute("data-ved")||r.querySelector("[data-ved]")?.getAttribute("data-ved")||null,question:r.getAttribute(o.itemDataQ)||r.getAttribute(o.itemDataInitQ)||r.querySelector(o.itemQuestionEl)?.innerText?.trim()||"",answer:R&&f||void 0,sourceTitle:F,sourceSite:E,sources:M}})},{selectors:a,onlyQuestion:n,answerPlaceholderPattern:Ks}),s=typeof t.url=="function"?t.url():"https://www.google.com/";return i.flatMap(o=>{let d=[],l=new Set;for(let S of o.sources){let c=Rn(S.rawHref,s);if(!c||l.has(c.rawUrl))continue;l.add(c.rawUrl);let h=o.sourceSite??null;try{h||=new URL(c.resolvedUrl??c.rawUrl).hostname.replace(/^www\./,"")}catch{}d.push({...c,title:S.title||o.sourceTitle||null,site:h})}let U=d[0],u={questionId:Qt(o.question),googleLinkId:o.googleLinkId,googleEvidenceId:o.googleEvidenceId,question:o.question,answer:xs(o.answer,o.question,o.sourceTitle),sourceTitle:U?.title??o.sourceTitle,sourceSite:U?.site??o.sourceSite,sourceCite:U?.url,sources:d},r=li.safeParse(u);return r.success?[r.data]:(console.warn("[PAAExtractor] item parse failed:",o.question,r.error.issues[0]?.message),[])})}jitteredDelayMs(t,n){return t+Math.floor(Math.random()*(n-t+1))}async clickItem(t,n){try{await t.locator(`${K.item}[data-q="${n}"], ${K.item}[data-initq="${n}"]`).first().click()}catch{}}toFlatRow(t,n,a,i){return{seed_query:i,question_id:t.questionId??Qt(t.question),google_link_id:t.googleLinkId??"",google_evidence_id:t.googleEvidenceId??"",question:t.question,answer:t.answer??"",source_title:t.sourceTitle??"",source_site:t.sourceSite??"",source_cite:t.sourceCite??"",sources:t.sources??[],depth:n,parent_question:a??"",extracted_at:new Date().toISOString()}}upsertCollectedRow(t){let n=this.normalizeQuestion(t.question),a=this.collectedRows.findIndex(d=>this.normalizeQuestion(d.question)===n);if(a<0)return this.collectedRows.push({...t}),!0;let i=this.collectedRows[a],s={...i,...t,answer:t.answer||i.answer,source_title:t.source_title||i.source_title,source_site:t.source_site||i.source_site,source_cite:t.source_cite||i.source_cite,sources:this.mergePaaSources(i.sources,t.sources),extracted_at:t.extracted_at||i.extracted_at},o=s.answer!==i.answer||s.source_title!==i.source_title||s.source_site!==i.source_site||s.source_cite!==i.source_cite||JSON.stringify(s.sources)!==JSON.stringify(i.sources);return o&&(this.collectedRows[a]=s),o}async emitProgress(t){if(!this.onPaaProgress||this.collectedRows.length===0&&!this.checkpointMaterial)return;let n={sequence:++this.progressSequence,observedAt:new Date().toISOString(),clickedQuestions:[...this.clickedQuestions],phase:t,records:this.collectedRows.map(a=>({...a})),lifecycle:{...this.paaLifecycle},...this.checkpointMaterial?{material:structuredClone(this.checkpointMaterial)}:{},interactions:this.interactionObservations.map(a=>({...a,confirmationSignals:[...a.confirmationSignals]}))};try{await this.onPaaProgress(n)}catch(a){console.warn(JSON.stringify({event:"paa_progress_sink_failed",sequence:n.sequence,phase:n.phase,message:a instanceof Error?a.message:String(a)}))}}getPartialResult(){let t=this.partialContext;if(!t||this.collectedRows.length===0&&!this.checkpointMaterial)return null;let n=this.collectedRows.map(d=>({...d})),a=this.checkpointMaterial,i=t.questionsOnly?0:n.filter(d=>!d.answer?.trim()).length,s=t.questionsOnly?0:n.filter(d=>(d.sources??[]).length===0).length,o={seed:t.seed,totalQuestions:n.length,maxDepthReached:n.reduce((d,l)=>Math.max(d,l.depth),0),durationMs:Date.now()-t.startMs,errorCount:0};return{seed:t.seed,location:t.location,extractedAt:new Date().toISOString(),diagnostics:{phaseTimings:this.currentPhaseTimings(t.startMs),completionStatus:"paa_found",problem:null,...a?.pagination?{pagination:{...a.pagination}}:{},resultQuality:"partial",degradedResult:!1,retryRecommended:!0,paaLifecycle:{...this.paaLifecycle,questionsFound:n.length,targetStatus:n.length>=(t.maxQuestions??0)?"met":"not_met",discoveryStatus:"interrupted",materialStatus:i>0||s>0?"partial":"complete",completionReason:"The browser attempt ended before frontier exhaustion could be proven."},completeness:{...this.completeness,paaWithoutAnswer:i,paaWithoutSource:s,paaRequested:t.maxQuestions,paaReturned:n.length,paaUnique:n.length},warnings:[{code:"paa_partial",surface:"paa",message:`Harvest stopped before finishing; returning the ${n.length} question(s) collected up to that point.`,retryable:!0}]},totalQuestions:n.length,surface:a?.surface??"web",aiOverview:a?.aiOverview??{detected:!1,text:null,citations:[]},aiMode:a?.aiMode??{detected:!1,text:null,citations:[]},whatPeopleSaying:a?.whatPeopleSaying??[],tree:this.buildTree(n,t.seed),flat:n,videos:a?.videos??[],forums:a?.forums??[],organicResults:a?.organicResults??[],localPack:a?.localPack??[],entityIds:a?.entityIds??{entities:[],kgIds:[],cids:[],gcids:[]},stats:o}}async runBFS(t,n,a){await t.waitForTimeout(700);let i=new Set,s=new Set,o=[];this.clickedQuestions=new Set,this.interactionObservations=[],this.phaseTimings={};let d=new Set,l=new Map,U=new Set,u=new Set,r=new Map,S=2,c=3,h=0,m={sel:K.item,dataQ:K.itemDataQ,dataInitQ:K.itemDataInitQ,questionEl:K.itemQuestionEl,answerSel:K.answerContainer,expandedClass:K.expandedClass,clickTarget:K.clickTarget,answerPlaceholderPattern:Ks,questionsOnly:n.questionsOnly,sourceSel:"a[href]"},k=()=>t.evaluate(({sel:E,dataQ:M,dataInitQ:P,questionEl:x,expandedClass:y,clickTarget:N,answerSel:p,answerPlaceholderPattern:v,questionsOnly:_})=>{let I=L=>{if(!L)return"";let W=[];for(let O of L.childNodes){if(O.nodeType===Node.TEXT_NODE){let Q=O.textContent?.trim();Q&&W.push(Q);continue}if(O.nodeType!==Node.ELEMENT_NODE)continue;let z=O;if(z.tagName==="STYLE"||z.tagName==="SCRIPT"||z.getAttribute("aria-hidden")==="true")continue;let D=I(z);D&&W.push(D)}return W.join(" ").replace(/\s+/g," ").trim()};return Array.from(document.querySelectorAll(E)).map((L,W)=>{let O=I(L.querySelector(p)).replace(/An AI Overview is not available for this search/gi," ").replace(/Can't generate an AI overview right now\.?\s*Try again later\.?/gi," ").replace(/\bAI Overview\b/gi," ").replace(/\s+/g," ").trim(),z=L.classList.contains(y)||L.querySelector(N)?.getAttribute("aria-expanded")==="true",D=O.length>=10&&!new RegExp(v,"i").test(O);return{q:L.getAttribute(M)||L.getAttribute(P)||L.querySelector(x)?.innerText?.trim()||"",done:z&&(_||D),expanded:z,index:W}}).filter(L=>L.q)},m),f=async(E,M,P,x,y,N)=>{let p=Date.now(),v={id:`${N}:${Qt(E)}:${y}`,questionId:Qt(E),question:E,batchNumber:N,controlAttempt:y,observedAt:new Date(p).toISOString(),beforeQuestionCount:M,expandedBefore:x},_=P>=0?t.locator(m.sel).nth(P).locator(m.clickTarget):null,I=null,L=_?await(async()=>(x&&(await _.click({timeout:Js,force:!0}),await t.waitForTimeout(120)),await _.click({timeout:Js,force:!0}),!0))().catch(J=>(I=J instanceof Error&&(J.name==="TimeoutError"||/timeout/i.test(J.message))?"click_ack_timeout":"click_failed",!1)):!1,W=Date.now()-p;if(!L&&I!=="click_ack_timeout")return{dispatched:!1,confirmed:!1,observation:{...v,dispatched:!1,outcome:"dispatch_failed",confirmationSignals:[],confirmationCheckpointMs:null,confirmedAtMs:null,dispatchDurationMs:W,totalDurationMs:W,afterQuestionCount:M,newQuestionCount:0,expandedAfter:x,answerObserved:!1,sourceCount:0,errorCode:P<0?"control_missing":I??"click_failed"}};let O={exists:!0,expanded:x,answerReady:!1,sourceCount:0,questionCount:M},z=[],D=null,Q=null,ct=!1;for(let J of[jd,zd,$d]){let ht=p+J-Date.now();if(ht>0&&await t.waitForTimeout(ht),O=await t.evaluate(({sel:V,dataQ:ot,dataInitQ:pt,questionEl:Le,expandedClass:xe,clickTarget:Ot,answerSel:nn,answerPlaceholderPattern:ne,sourceSel:an,target:Oe})=>{let sn=ie=>{if(!ie)return"";let Xn=[];for(let rn of ie.childNodes){if(rn.nodeType===Node.TEXT_NODE){let oi=rn.textContent?.trim();oi&&Xn.push(oi);continue}if(rn.nodeType!==Node.ELEMENT_NODE)continue;let dn=rn;if(dn.tagName==="STYLE"||dn.tagName==="SCRIPT"||dn.getAttribute("aria-hidden")==="true")continue;let si=sn(dn);si&&Xn.push(si)}return Xn.join(" ").replace(/\s+/g," ").trim()},ae=Array.from(document.querySelectorAll(V)),on=ae.find(ie=>(ie.getAttribute(ot)||ie.getAttribute(pt)||ie.querySelector(Le)?.innerText?.trim())===Oe);if(!on)return{exists:!1,expanded:!1,answerReady:!1,sourceCount:0,questionCount:ae.length};let fo=on.classList.contains(xe)||on.querySelector(Ot)?.getAttribute("aria-expanded")==="true",ni=on.querySelector(nn),ai=sn(ni).replace(/An AI Overview is not available for this search/gi," ").replace(/Can't generate an AI overview right now\.?\s*Try again later\.?/gi," ").replace(/\bAI Overview\b/gi," ").replace(/\s+/g," ").trim(),ii=ai.length>=10&&!new RegExp(ne,"i").test(ai);return{exists:!0,expanded:fo,answerReady:ii,sourceCount:ii?ni?.querySelectorAll(an).length??0:0,questionCount:ae.length}},{...m,target:E}).catch(()=>O),z=[...O.expanded?["expanded"]:[],...O.answerReady?["answer"]:[],...O.sourceCount>0?["source"]:[],...O.questionCount>M?["question_growth"]:[]],O.answerReady||O.sourceCount>0||O.questionCount>M){ct=!0,D=J,Q=Date.now()-p;break}}let ft=Date.now()-p,Z={...v,dispatched:L,outcome:ct?"confirmed":L?"unconfirmed":"dispatch_failed",confirmationSignals:z,confirmationCheckpointMs:D,confirmedAtMs:Q,dispatchDurationMs:W,totalDurationMs:ft,afterQuestionCount:O.questionCount,newQuestionCount:Math.max(0,O.questionCount-M),expandedAfter:O.expanded,answerObserved:O.answerReady,sourceCount:O.sourceCount,errorCode:L?ct?null:O.exists?"confirmation_timeout":"control_missing":I};return{dispatched:L,confirmed:ct,observation:Z}},C=0;t:for(;;){if(n.softDeadlineMs&&Date.now()>=n.softDeadlineMs)throw this.paaLifecycle={...this.paaLifecycle,discoveryStatus:"interrupted",completionReason:"The execution deadline arrived before frontier exhaustion could be proven."},new jt(this.paaLifecycle.completionReason);this.throwIfAborted(a);let E=await k(),M=!1;for(let p of E)s.has(p.q)||(s.add(p.q),o.push(p.q),o.length<=n.maxQuestions&&(M=this.upsertCollectedRow(this.toFlatRow({questionId:Qt(p.q),googleLinkId:null,googleEvidenceId:null,question:p.q,sources:[]},1,null,n.query))||M)),p.done&&U.add(p.q);if(this.paaLifecycle={...this.paaLifecycle,questionsFound:Math.min(o.length,n.maxQuestions),targetStatus:o.length>=n.maxQuestions?"met":"not_met",knownQuestions:o.length,processedQuestions:U.size,failedInteractions:u.size,visibleControls:E.length},M&&await this.emitProgress("discovered"),n.questionsOnly&&s.size>=n.maxQuestions){this.paaLifecycle={...this.paaLifecycle,discoveryStatus:"target_reached",targetStatus:"met",completionReason:"The requested unique-question count was observed."};break}if(!n.questionsOnly&&s.size>=n.maxQuestions){let p=!1,v=new Set(o.slice(0,n.maxQuestions));for(let _ of await this.extractVisibleItems(t)){if(!v.has(_.question))continue;let I=r.get(_.question),L={..._,answer:_.answer??I?.answer,sourceTitle:_.sourceTitle??I?.sourceTitle,sourceSite:_.sourceSite??I?.sourceSite,sourceCite:_.sourceCite??I?.sourceCite,sources:this.mergePaaSources(I?.sources,_.sources)};r.set(_.question,L),p=this.upsertCollectedRow(this.toFlatRow(L,1,null,n.query))||p}if(p&&await this.emitProgress("answer_captured"),[...v].every(_=>{let I=r.get(_);return!!(I?.answer?.trim()&&I.sources.length>0)})){u.clear(),this.paaLifecycle={...this.paaLifecycle,discoveryStatus:"target_reached",targetStatus:"met",failedInteractions:0,completionReason:"The requested unique-question count and its answer/source material were observed."};break}}let P=new Set(o.slice(0,n.maxQuestions)),x=E.filter(p=>P.has(p.q)&&!p.done&&(l.get(p.q)??0)<S&&!d.has(p.q)),y=x.slice(-Qd);if(this.paaLifecycle={...this.paaLifecycle,eligibleControls:x.length},y.length===0){if(s.size>=n.maxQuestions){this.paaLifecycle={...this.paaLifecycle,discoveryStatus:"target_reached",targetStatus:"met",completionReason:"The requested unique-question count was observed."};break}let p=new Set(E.map(_=>_.q)),v=o.slice(0,n.maxQuestions).filter(_=>!U.has(_)&&(u.has(_)||!p.has(_)));if(v.length>0)throw this.paaLifecycle={...this.paaLifecycle,discoveryStatus:"interrupted",failedInteractions:new Set([...u,...v]).size,completionReason:"At least one known PAA control failed or disappeared before it could be confirmed."},await this.emitProgress("expansion_finished"),new jt(this.paaLifecycle.completionReason);for(let _=1;_<=c;_++){this.throwIfAborted(a),await this.throwIfCaptcha(t,"Google PAA exhaustion confirmation"),await t.mouse.wheel(0,320);let I=await t.waitForFunction(({sel:W,min:O})=>document.querySelectorAll(W).length>O,{sel:K.item,min:E.length},{timeout:1500,polling:100}).catch(()=>null);if(this.paaLifecycle={...this.paaLifecycle,noGrowthConfirmations:_},I||(await k()).some(W=>!s.has(W.q)))continue t}this.paaLifecycle={...this.paaLifecycle,discoveryStatus:"frontier_exhausted",targetStatus:"not_met",failedInteractions:0,noGrowthConfirmations:c,completionReason:"Every known eligible PAA control was processed and three healthy no-growth confirmations found no additional questions."};break}await this.throwIfCaptcha(t,"Google PAA expansion"),h+=1;for(let p of y){if(this.reporter.onDepth(++C),(l.get(p.q)??0)>0){let v=this.paaLifecycle.retryBreakdown.controlRetries+1;this.paaLifecycle={...this.paaLifecycle,automaticRetries:v+this.paaLifecycle.retryBreakdown.browserRetries+this.paaLifecycle.retryBreakdown.runtimeRecoveries,retryBreakdown:{...this.paaLifecycle.retryBreakdown,controlRetries:v}}}l.set(p.q,(l.get(p.q)??0)+1)}let N=await Promise.all(y.map(async p=>({target:p,result:await f(p.q,E.length,p.index,p.expanded,l.get(p.q)??1,h)})));for(let{target:p,result:v}of N)(v.dispatched||v.confirmed)&&this.clickedQuestions.add(p.q),this.interactionObservations.push(v.observation);if(this.interactionObservations.length>qs&&this.interactionObservations.splice(0,this.interactionObservations.length-qs),!n.questionsOnly){let p=new Set(y.map(v=>v.q));for(let v of await this.extractVisibleItems(t))p.has(v.question)&&(r.set(v.question,v),this.upsertCollectedRow(this.toFlatRow(v,1,null,n.query))&&await this.emitProgress("answer_captured"))}for(let{target:p,result:v}of N){let _=r.get(p.q);v.confirmed||_?.answer?.trim()?(U.add(p.q),u.delete(p.q)):(l.get(p.q)??0)>=S&&(u.add(p.q),d.add(p.q))}this.paaLifecycle={...this.paaLifecycle,processedQuestions:U.size,failedInteractions:u.size,noGrowthConfirmations:0,dispatchedInteractions:this.interactionObservations.filter(p=>p.dispatched).length,confirmedInteractions:this.interactionObservations.filter(p=>p.outcome==="confirmed").length,unconfirmedInteractions:this.interactionObservations.filter(p=>p.outcome!=="confirmed").length},await this.emitProgress("click_observed")}let A=n.questionsOnly?new Map:new Map(r);n.questionsOnly||await this.fillIncompleteItems(t,o,A,n,d);let R=[];for(let E of o){if(R.length>=n.maxQuestions)break;let M=this.normalizeQuestion(E);if(i.has(M))continue;i.add(M);let P=A.get(E);if(P){let x=this.toFlatRow(P,1,null,n.query);this.upsertCollectedRow(x),R.push(x),this.reporter.onQuestion({questionId:P.questionId,googleLinkId:P.googleLinkId,googleEvidenceId:P.googleEvidenceId,question:P.question,answer:P.answer??null,sourceTitle:P.sourceTitle??null,sourceSite:P.sourceSite??null,sourceCite:P.sourceCite??null,sources:P.sources,depth:1,parentQuestion:null,children:[]})}else{let x=this.toFlatRow({questionId:Qt(E),googleLinkId:null,googleEvidenceId:null,question:E,answer:void 0,sourceTitle:void 0,sourceSite:void 0,sourceCite:void 0,sources:[]},1,null,n.query);this.upsertCollectedRow(x),R.push(x)}}this.completeness.paaWithoutAnswer=n.questionsOnly?0:R.filter(E=>!E.answer).length,this.completeness.paaWithoutSource=n.questionsOnly?0:R.filter(E=>(E.sources??[]).length===0).length,this.completeness.paaRequested=n.maxQuestions,this.completeness.paaReturned=R.length,this.completeness.paaUnique=i.size;let F=!n.questionsOnly&&(this.completeness.paaWithoutAnswer>0||this.completeness.paaWithoutSource>0);return this.paaLifecycle={...this.paaLifecycle,questionsFound:R.length,targetStatus:R.length>=n.maxQuestions?"met":"not_met",materialStatus:F?"partial":"complete",processedQuestions:U.size,failedInteractions:u.size},await this.emitProgress("fill_incomplete"),await this.emitProgress("expansion_finished"),R}async fillIncompleteItems(t,n,a,i,s){let o=new Set(n.slice(0,i.maxQuestions)),d=u=>!u?.answer||(u.sources??[]).length===0,l=new Set([...o].filter(u=>d(a.get(u)))),U=new Set([...l].filter(u=>!a.get(u)?.answer));if(l.size!==0){for(let u of l){if(i.softDeadlineMs&&Date.now()>=i.softDeadlineMs)break;let r=(await this.extractVisibleItems(t,u))[0];if(!r)continue;let S=a.get(u);a.set(u,{...r,answer:r.answer??S?.answer,sourceTitle:r.sourceTitle??S?.sourceTitle,sourceSite:r.sourceSite??S?.sourceSite,sourceCite:r.sourceCite??S?.sourceCite,sources:this.mergePaaSources(S?.sources,r.sources)});let c=a.get(u);c&&this.upsertCollectedRow(this.toFlatRow(c,1,null,i.query))&&await this.emitProgress("fill_incomplete")}this.completeness.paaAnswersRecovered=[...U].filter(u=>a.get(u)?.answer).length}}async reExpandCollapsedItems(t){let n=`${K.item}:not(.${K.expandedClass}) ${K.clickTarget}`,a=await t.locator(n).count().catch(()=>0);if(a===0)return!1;for(let i=0;i<a;i++)try{let s=t.locator(n).first();await s.scrollIntoViewIfNeeded(),await s.click({force:!0}),await t.waitForTimeout(this.jitteredDelayMs(300,500))}catch{}return!0}async extractVideos(t){let n=$i;return t.evaluate(a=>{let i=[],s=Array.from(document.querySelectorAll(a.container));for(let o of s){let U=(o.querySelector(a.sectionHeading)?.textContent?.trim()??"").toLowerCase().includes("short")?"short_video":"video",u=Array.from(o.querySelectorAll(a.item));for(let r of u){let S=r.href;if(!S||!S.includes("youtube")&&!S.includes("youtu.be"))continue;let c=r.textContent?.trim()??"",h=c.indexOf("YouTube");if(h===-1)continue;let m=c.slice(0,h).trim(),f=c.slice(h+7).replace(/^[·\s·]+/,"").match(/^([^·\n]+)/),C=f?f[1].trim():"";m&&i.push({type:U,title:m,channel:C,platform:"YouTube",duration:"",url:S})}}return i},n)}async extractForums(t){let n=Qi;return t.evaluate(a=>{let i=[],o=Array.from(document.querySelectorAll(a.section)).find(l=>l.textContent?.includes("Discussions"));if(!o)return i;let d=Array.from(o.querySelectorAll(a.item));for(let l of d){let U=l.href;if(!U)continue;let u=l.querySelector(a.title),r=l.querySelector(a.source),S=u?.textContent?.trim()??"",c=r?.textContent?.trim()??"";S&&i.push({title:S,source:c,url:U})}return i},n)}async extractShortVideos(t,n){try{await t.goto(n,{waitUntil:"domcontentloaded"}),await t.waitForTimeout(1500)}catch{return[]}await this.throwIfCaptcha(t,"Google short video search");let a={item:fn.item,platforms:[...fn.platforms]};return(await t.evaluate(s=>{let o=new Set,d=[],l=Array.from(document.querySelectorAll(s.item)),U=["youtube.com","youtu.be","tiktok.com","instagram.com","facebook.com","fb.watch"],u=new Map;for(let r of l){let S=r.href;if(!S||!U.some(h=>S.includes(h)))continue;let c=r.textContent?.trim()??"";u.has(S)||u.set(S,[]),u.get(S).push(c)}for(let[r,S]of u.entries()){if(o.has(r))continue;o.add(r);let c=S.find(C=>/^\d+:\d+$/.test(C))??"",h=S.find(C=>!/^\d+:\d+$/.test(C)&&C.length>5)??"";if(!h)continue;let m=h,k="",f="";for(let C of s.platforms){let A=-1,R=0;for(;;){let x=h.indexOf(C,R);if(x===-1)break;A=x,R=x+1}if(A===-1)continue;let F=h.slice(A+C.length);if(!(/^[\s·]/.test(F)||F.trim()===""))continue;m=h.slice(0,A).trim(),k=C;let M=F.replace(/^[\s·]+/,""),P=M.indexOf("\xB7");f=(P===-1?M:M.slice(0,P)).trim();break}m&&d.push({title:m,channel:f,platform:k,duration:c,url:r})}return d},a)).map(s=>({type:"short_video",...s}))}async extractWhatPeopleSaying(t){let n=Zi;return t.evaluate(a=>{let i=Array.from(document.querySelectorAll(a.sectionTag)).find(s=>s.textContent?.includes(a.sectionHeadingText))??document.querySelector(".yG4QQe.TBC9ub.NbhJ1c");return i?Array.from(i.querySelectorAll(a.card)).map(s=>{let d=s.querySelector(a.cardLink)?.href??"",l=s.querySelector(a.titleH1)?.textContent?.trim(),U=s.querySelector(a.titleDiv)?.textContent?.trim(),u=l??U??"",r=s.querySelector(a.source)?.textContent?.trim()??"",c=s.querySelector(a.platformBadge)?.textContent?.trim()??"",h=s.querySelector(a.ytChannel)?.textContent?.trim()??"",m=s.querySelector(a.ytDate)?.textContent?.trim()??"",k=s.querySelector(a.authorNote)?.textContent?.trim()??null,f=s.querySelector(a.popularCommentLabel),C=null;if(f){let I=f.nextSibling;for(;I;){let L=I.textContent?.trim();if(L){C=L;break}I=I.nextSibling}}let A=Array.from(s.querySelectorAll("span")),R=A.find(I=>/^\d+:\d+$/.test(I.textContent?.trim()??""))?.textContent?.trim()??null,E=A.map(I=>I.textContent?.trim()??"").filter(I=>/\d/.test(I)&&(I.includes("comment")||I.includes("reaction")||I.includes("view")||I.includes("like")||I.includes("share")))[0]??"",M=A.map(I=>I.textContent?.trim()??"").filter(I=>/\d+ (day|week|month|year|hour)s? ago|Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec/.test(I)),P=m||(M[0]??""),x=c||(h?"YouTube":""),y=h||r,N="unknown",p=x.toLowerCase(),v=y.toLowerCase(),_=r.toLowerCase();return p.includes("reddit")||v.startsWith("r/")?N="reddit":p.includes("facebook")||_.includes("facebook")?N="facebook":p.includes("instagram")||_.includes("instagram")?N="instagram":p.includes("tiktok")||_.includes("tiktok")?N="tiktok":p.includes("youtube")||h?N="youtube":N="news",{type:N,title:u,url:d,source:y,platform:x,popularComment:C,engagement:E,date:P,duration:R,authorNote:k}}):[]},n)}async extractOrganicResults(t){let n=ns,a=await t.evaluate(s=>{let o=[],d=0;return document.querySelectorAll(s.result).forEach(l=>{let U=l.querySelector(s.title);if(!U)return;let u=U.textContent?.trim()??"",S=U.closest("a")?.href??"";if(!u||!S)return;d++;let c=l.querySelector(s.cite)?.textContent?.trim()??null,h=l.querySelector(s.snippet)?.textContent?.trim()??null,m=!!l.querySelector(s.redditCite),k=l.querySelector(s.ratingWrap),f=k?{value:k.querySelector(s.ratingValue)?.textContent?.trim()??"",count:k.querySelector(s.reviewCount)?.textContent?.trim()??""}:null,C="";try{C=new URL(S).hostname.replace(/^www\./,"")}catch{C=l.querySelector(s.siteName)?.textContent?.trim()??""}o.push({position:d,title:u,url:S,domain:C,cite:c,snippet:h,isRedditStyle:m,inlineRating:f})}),o},n),i=typeof t.url=="function"?t.url():"https://www.google.com/";return a.flatMap(s=>{let o=Rn(s.url,i);if(!o)return[];let d=s.domain;try{d=new URL(o.resolvedUrl??o.rawUrl).hostname.replace(/^www\./,"")}catch{}return[{...s,...o,domain:d}]})}async extractLocalPack(t){let n=as;return t.evaluate(a=>{let i=[],s=null;return document.querySelectorAll('[role="heading"]').forEach(o=>{!s&&o.textContent?.includes(a.headingText)&&(s=o.closest("[data-hveid]"))}),s&&s.querySelectorAll(a.card).forEach((o,d)=>{let l=o.querySelector(a.name)?.textContent?.trim()??"";if(!l)return;let U=o.querySelector(a.ratingValue)?.textContent?.trim()??null,u=o.querySelector(a.reviewCount)?.textContent?.trim()??null,r=u?u.replace(/[()]/g,"").trim():null,S=o.querySelector("a[data-cid]")?.getAttribute("data-cid")??null;if(!S)for(let f of Array.from(o.querySelectorAll("a[href]"))){let C=f.href.match(/[?&]cid=(\d+)/);if(C){S=C[1];break}let A=f.href.match(/!1s0x[0-9a-f]+:0x([0-9a-f]+)/i);if(A){try{S=BigInt("0x"+A[1]).toString()}catch{}if(S)break}}let c=[];o.querySelectorAll("div, span").forEach(f=>{let C=Array.from(f.childNodes).filter(A=>A.nodeType===3).map(A=>A.textContent?.trim()??"").filter(A=>A.length>1&&A.length<120).join(" ");C&&!c.includes(C)&&c.push(C)});let h=Array.from(o.querySelectorAll("a[href]")),m=h.find(f=>f.href.includes("google.com/maps"))?.href??null,k=h.find(f=>!f.href.includes("google.com")&&f.href.startsWith("http"))?.href??null;i.push({position:d+1,name:l,cid:S,rating:U,reviewCount:r,metadata:c,websiteUrl:k,directionsUrl:m})}),i},n)}async extractEntityIds(t){return t.evaluate(()=>{let n=new Set,a=new Set,i=new Set,s=new Map;function o(l){let U=[".OSrXXb",".dbg0pd",".tzt0oe",'[role="heading"]',"h3"];for(let u of U){let r=l.querySelector(u);if(r?.textContent?.trim())return r.textContent.trim()}return""}document.querySelectorAll('[id^="pv-/g/"]').forEach(l=>{let U=l.getAttribute("id");if(!U)return;let u=U.replace("pv-","");n.add(u);let r=o(l),c=l.querySelector("a[data-cid]")?.getAttribute("data-cid")??null;c&&a.add(c),r&&s.set(u,{name:r,kgId:u,cid:c,gcid:null})}),document.querySelectorAll("[data-mid]").forEach(l=>{let U=l.getAttribute("data-mid");if(U?.startsWith("/g/")&&(n.add(U),!s.has(U))){let u=o(l);u&&s.set(U,{name:u,kgId:U,cid:null,gcid:null})}}),document.querySelectorAll(".w7Dbne").forEach(l=>{let u=l.querySelector("a[data-cid]")?.getAttribute("data-cid")??null;if(!u)return;a.add(u);let r=l.querySelector(".OSrXXb")?.textContent?.trim()??"";if(!r)return;let S=l.querySelector('[id^="pv-/g/"]'),c=S?S.getAttribute("id").replace("pv-",""):null,h=c??`cid:${u}`;if(s.has(h)){let m=s.get(h);m.cid||s.set(h,{...m,cid:u})}else s.set(h,{name:r,kgId:c,cid:u,gcid:null})}),document.querySelectorAll("a[data-cid]").forEach(l=>{let U=l.getAttribute("data-cid");if(!U)return;if(a.add(U),![...s.values()].some(r=>r.cid===U)){let r=l.parentElement,S="";for(let c=0;c<8&&r;c++){let h=r.querySelector('.OSrXXb, .dbg0pd, [role="heading"], h3');if(h?.textContent?.trim()){S=h.textContent.trim();break}r=r.parentElement}S&&s.set(`cid:${U}`,{name:S,kgId:null,cid:U,gcid:null})}});let d=Array.from(document.querySelectorAll("script:not([src])")).map(l=>l.textContent??"").filter(l=>l.length>1e4).join(`
21896
- `);for(let l of d.matchAll(/\/g\/[a-zA-Z0-9_-]{5,20}/g))n.add(l[0]);for(let l of d.matchAll(/gcid:[a-zA-Z0-9_]+/g))i.add(l[0]);for(let l of d.matchAll(/0x[0-9a-f]+:0x([0-9a-f]+)/gi))try{a.add(BigInt("0x"+l[1]).toString())}catch{}return{entities:[...s.values()],kgIds:[...n],cids:[...a],gcids:[...i]}})}mergeLocalPackIntoEntities(t,n){let a=new Set(t.cids),i=t.entities.map(s=>({...s}));for(let s of n){if(!s.cid)continue;a.add(s.cid);let o=s.name.toLowerCase().trim(),d=i.find(l=>l.name.toLowerCase().trim()===o);d?d.cid||(d.cid=s.cid):i.find(l=>l.cid===s.cid)||i.push({name:s.name,kgId:null,cid:s.cid,gcid:null})}return{...t,entities:i,cids:[...a]}}async extractAISurfaces(t,n){let a=await t.evaluate(Os,{aio:ts,aim:es,expandWaitMs:1500}),i=typeof t.url=="function"?t.url():"https://www.google.com/",s={surface:a.surface,aiOverview:{...a.aiOverview,citations:this.normalizeAICitations(i,a.aiOverview.citations)},aiMode:{...a.aiMode,citations:this.normalizeAICitations(i,a.aiMode.citations)}};if(!s.aiOverview.detected)return this.completeness.aioShareCaptured=null,s;let o=this.aioShareUrlEarly??await this.captureAIOverviewShareUrl(t,n).catch(()=>null);return this.completeness.aioShareCaptured=o!==null,{...s,aiOverview:{...s.aiOverview,shareUrl:o}}}async captureAIOverviewShareUrl(t,n){if(n?.softDeadlineMs&&Date.now()>=n.softDeadlineMs||!await t.evaluate(()=>{let s=Array.from(document.querySelectorAll('h1,h2,h3,[role="heading"],div')).filter(o=>(o.textContent||"").trim()==="AI Overview");for(let o of s){let d=o;for(let l=0;l<8&&d;l++){let U=d.querySelector('[aria-label="Share"]');if(U)return U.scrollIntoView({block:"center"}),U.click(),!0;d=d.parentElement}}return!1}).catch(()=>!1))return null;let i=null;try{i=await(await t.waitForFunction(()=>{let o=document.createTreeWalker(document.body,NodeFilter.SHOW_TEXT);for(;o.nextNode();){let d=(o.currentNode.textContent||"").match(/https:\/\/share\.google\/aimode\/\S+/);if(d)return d[0]}return!1},void 0,{timeout:4500,polling:250})).jsonValue()}catch{i=null}return await t.keyboard.press("Escape").catch(()=>{}),i}buildTree(t,n){let a=[],i=new Map;for(let s of t){let o={questionId:s.question_id,googleLinkId:s.google_link_id||null,googleEvidenceId:s.google_evidence_id||null,question:s.question,answer:s.answer||null,sourceTitle:s.source_title||null,sourceSite:s.source_site||null,sourceCite:s.source_cite||null,sources:s.sources??[],depth:s.depth,parentQuestion:s.parent_question||null,children:[]};i.set(s.question,o)}for(let s of i.values())s.parentQuestion&&i.has(s.parentQuestion)?i.get(s.parentQuestion).children.push(s):a.push(s);return a}getBrowserDebugSnapshot(){return this.driver.getDebugSnapshot()}buildHarvestDebugSnapshot(t,n,a,i,s){if(t.debug)return{enabled:!0,request:{query:t.query,locationInput:t.location??null,canonicalLocation:n,locationResolved:s?s.matched:null,locationMatchSource:s?s.source:null,locationAmbiguousCandidates:s?s.ambiguousCandidates:null,uule:a,gl:t.gl,hl:t.hl,device:t.device,proxyMode:t.proxyMode,proxyZip:t.proxyZip??null,serpIdentity:t.serpIdentity??null,serpOnly:t.serpOnly,pages:t.pages??1},browser:this.getBrowserDebugSnapshot(),...i?{locationEvidence:i}:{}}}async captureSecondOrganicPage(t,n,a){if(this.throwIfAborted(a),!this.driver.withTemporaryPage)return{organic:[],status:"failed",failureCode:"unsupported_driver"};let i;try{let s=new URL(t.url()),o=U=>{try{let u=new URL(U,s);return s.origin==="https://www.google.com"&&u.origin===s.origin&&!u.username&&!u.password&&u.pathname==="/search"&&u.searchParams.getAll("q").length===1&&u.searchParams.get("q")===n.query&&u.searchParams.getAll("start").length===1&&u.searchParams.get("start")==="10"&&(!u.searchParams.has("num")||u.searchParams.get("num")==="10")&&["gl","hl"].every(r=>!u.searchParams.has(r)||u.searchParams.getAll(r).length===1&&u.searchParams.get(r)===n[r])&&["uule","tbs","udm","tbm","pws"].every(r=>u.searchParams.getAll(r).length<=1&&u.searchParams.get(r)===s.searchParams.get(r))}catch{return!1}},d=await t.evaluate(()=>document.querySelector('a#pnnext, a[rel="next"]')?.getAttribute("href")??null);if(!d)return{organic:[],status:"unavailable",failureCode:"missing_next"};if(!o(d))return{organic:[],status:"unavailable",failureCode:"invalid_next"};let l=await this.driver.withTemporaryPage(async U=>{if(await U.route("**/*",async u=>{let r=u.request();r.isNavigationRequest()&&r.frame()===U.mainFrame()&&!o(r.url())?(i=/^https:\/\/www\.google\.com\/sorry(?:\/|\?)/.test(r.url())?"captcha":"invalid_next",await u.abort("blockedbyclient")):await u.fallback()}),await U.goto(new URL(d,s).href,{waitUntil:"domcontentloaded",timeout:2e4}),this.throwIfAborted(a),await this.throwIfCaptcha(U,"Google SERP page 2"),!o(U.url()))throw i="invalid_next",new Error("Invalid pagination landing");return this.extractOrganicResults(U)},a);return this.throwIfAborted(a),l.length>0?{organic:l.map(U=>({...U,position:U.position+10})),status:"captured"}:{organic:[],status:"unavailable",failureCode:"empty_page"}}catch(s){if(this.throwIfAborted(a),s instanceof ut)throw s;return{organic:[],status:"failed",failureCode:i??(s instanceof nt?"captcha":s instanceof Error&&(s.name==="TimeoutError"||/timed? ?out/i.test(s.message))?"timeout":"navigation_error")}}}async extract(t,n){let a=Date.now();this.completeness={paaWithoutAnswer:0,paaWithoutSource:0,paaAnswersRecovered:0,aioShareCaptured:null},this.linkDiagnostics=this.emptyLinkDiagnostics(),this.aioShareUrlEarly=null,this.collectedRows=[],this.progressSequence=0,this.checkpointMaterial=null,this.clickedQuestions=new Set,this.paaLifecycle=this.emptyPaaLifecycle(t.maxQuestions),this.partialContext={seed:t.query,location:t.location??null,startMs:a,maxQuestions:t.maxQuestions,questionsOnly:t.questionsOnly};let i=t.device==="mobile",s={headless:t.headless,profileDir:t.profileDir,proxy:t.proxy,\u006b\u0065\u0072\u006e\u0065\u006cApiKey:t.\u006b\u0065\u0072\u006e\u0065\u006cApiKey,\u006b\u0065\u0072\u006e\u0065\u006cProxyId:t.\u006b\u0065\u0072\u006e\u0065\u006cProxyId,\u006b\u0065\u0072\u006e\u0065\u006cProfileName:t.\u006b\u0065\u0072\u006e\u0065\u006cProfileName,\u006b\u0065\u0072\u006e\u0065\u006cProfileSaveChanges:t.\u006b\u0065\u0072\u006e\u0065\u006cProfileSaveChanges,\u006b\u0065\u0072\u006e\u0065\u006cStealth:t.\u006b\u0065\u0072\u006e\u0065\u006cStealth,\u006b\u0065\u0072\u006e\u0065\u006cProxyResolution:t.\u006b\u0065\u0072\u006e\u0065\u006cProxyResolution,proxyMode:t.proxyMode,keepDefaultProxy:t.keepDefaultProxy,requireUsEgress:t.requireUsEgress,viewport:i?{width:390,height:844}:{width:1280,height:800},locale:`${t.hl}-${t.gl.toUpperCase()}`,deviceScaleFactor:i?3:1,isMobile:i,hasTouch:i,debug:t.debug},o=0,d=[];try{this.throwIfAborted(n);let l=Date.now();await this.driver.launch(s),this.phaseTimings.providerConnectMs=Date.now()-l;let U=this.driver.getProviderSessionMetadata?.();if(U?.providerSessionId&&this.onProviderSession)try{await this.onProviderSession(U)}catch(V){console.warn(JSON.stringify({event:"paa_provider_session_sink_failed",provider:U.provider,message:V instanceof Error?V.message:String(V)}))}this.throwIfAborted(n);let u=t.location?xa(t.location):null,r=t.location?u?.canonical??t.location:null,S=r?Es(r):null,c=t,h=t.includeAllSerpFeatures,m=h||t.includeLocalPack,k=h||t.includeForums,f=h||t.includeVideos,C=h||t.includeAiOverview,A=h||t.includeWhatPeopleSaying,R={surface:"web",aiOverview:{detected:!1,text:null,citations:[]},aiMode:{detected:!1,text:null,citations:[]}},F=Date.now(),{hasPaa:E}=await this.driver.navigateToSERP(c.query,S,t.gl,t.hl,{...Re(t.recency)?{tbs:Re(t.recency)}:{},debug:t.debug,purpose:t.serpOnly?"serp":"paa",...t.softDeadlineMs?{deadlineMs:t.softDeadlineMs}:{}});this.phaseTimings.navigationMs=Date.now()-F,this.throwIfAborted(n);let M=this.driver.getPage();if(await this.throwIfCaptcha(M,"Google SERP"),E||(this.paaLifecycle={...this.paaLifecycle,discoveryStatus:"no_paa_observed",materialStatus:"complete",completionReason:"A readable Google SERP was observed without a People Also Ask block."}),t.questionsOnly){let V=Date.now(),ot=E?await this.runBFS(M,c,n):[];this.phaseTimings.paaExpansionMs=Date.now()-V,await this.resolveMaterialLinks(M,ot,[],R.aiOverview,R.aiMode);let pt={seed:c.query,totalQuestions:ot.length,maxDepthReached:ot.reduce((Le,xe)=>Math.max(Le,xe.depth),0),durationMs:Date.now()-a,errorCount:o};return this.reporter.onComplete(pt),{seed:c.query,location:t.location??null,extractedAt:new Date().toISOString(),diagnostics:{phaseTimings:this.currentPhaseTimings(a),completionStatus:E?"paa_found":"no_paa",...E?{}:{noPaaObserved:!0},problem:null,paaLifecycle:{...this.paaLifecycle},completeness:{...this.completeness},links:{...this.linkDiagnostics},...t.debug?{debug:this.buildHarvestDebugSnapshot(c,r,S,void 0,u)}:{}},totalQuestions:ot.length,surface:"web",aiOverview:{detected:!1,text:null,citations:[]},aiMode:{detected:!1,text:null,citations:[]},whatPeopleSaying:[],tree:this.buildTree(ot,c.query),flat:ot,videos:[],forums:[],organicResults:[],localPack:[],entityIds:{entities:[],kgIds:[],cids:[],gcids:[]},stats:pt}}if(C&&(this.aioShareUrlEarly=await this.captureAIOverviewShareUrl(M,t).catch(()=>null)),t.serpOnly){let V=Date.now(),[ot,pt,Le]=await Promise.all([this.extractOrganicResults(M),m?this.extractLocalPack(M):Promise.resolve([]),this.extractEntityIds(M)]),xe=this.mergeLocalPackIntoEntities(Le,pt),Ot=C?await this.extractAISurfaces(M,t):R;this.phaseTimings.serpParseMs=Date.now()-V;let nn=t.debug?Qe(r,ot,pt):void 0,ne=ot;if((t.pages??1)>=2){let Oe=new URLSearchParams({q:c.query,gl:t.gl,hl:t.hl,pws:"0",start:"10"});Re(t.recency)&&Oe.set("tbs",Re(t.recency)),S&&Oe.set("uule",S),await this.driver.navigateTo("https://www.google.com/search?"+Oe.toString()),await this.throwIfCaptcha(M,"Google SERP page 2");let sn=await this.extractOrganicResults(M);ne=[...ot,...sn.map(ae=>({...ae,position:ae.position+10}))],t.debug&&(nn=Qe(r,ne,pt))}await this.resolveMaterialLinks(M,[],ne,Ot.aiOverview,Ot.aiMode);let an={seed:c.query,totalQuestions:0,maxDepthReached:0,durationMs:Date.now()-a,errorCount:o};return this.reporter.onComplete(an),{seed:c.query,location:t.location??null,extractedAt:new Date().toISOString(),diagnostics:{phaseTimings:this.currentPhaseTimings(a),completionStatus:"serp_only",problem:null,completeness:{...this.completeness},links:{...this.linkDiagnostics},...t.debug?{debug:this.buildHarvestDebugSnapshot(c,r,S,nn,u)}:{}},totalQuestions:0,surface:Ot.surface,aiOverview:Ot.aiOverview,aiMode:Ot.aiMode,whatPeopleSaying:[],tree:[],flat:[],videos:[],forums:[],organicResults:ne,localPack:pt,entityIds:xe,stats:an}}let[P,x,y,N,p,v]=await Promise.all([f?this.extractVideos(M):Promise.resolve([]),k?this.extractForums(M):Promise.resolve([]),A?this.extractWhatPeopleSaying(M):Promise.resolve([]),this.extractEntityIds(M),this.extractOrganicResults(M),m?this.extractLocalPack(M):Promise.resolve([])]),_=this.mergeLocalPackIntoEntities(N,v),I=t.debug?Qe(r,p,v):void 0;this.reporter.onVideos(P),this.reporter.onForums(x);let L=C?await this.extractAISurfaces(M,t):R,W={requestedPages:(t.pages??1)>=2?2:1,capturedPages:1,page2Status:(t.pages??1)>=2?"not_attempted":"not_requested",page1OrganicCount:p.length,page2OrganicCount:0},O=p;if(this.checkpointMaterial={surface:L.surface,aiOverview:L.aiOverview,aiMode:L.aiMode,whatPeopleSaying:y,videos:P,forums:x,organicResults:O,localPack:v,entityIds:_,pagination:W},await this.emitProgress("serp_captured"),W.requestedPages===2){let V=await this.captureSecondOrganicPage(M,c,n);O=[...p,...V.organic],W={...W,capturedPages:V.status==="captured"?2:1,page2Status:V.status,page2OrganicCount:V.organic.length,...V.failureCode?{failureCode:V.failureCode}:{}},this.checkpointMaterial={...this.checkpointMaterial,organicResults:O,pagination:W},await this.emitProgress("serp_captured"),await this.resolveMaterialLinks(M,[],O,L.aiOverview,L.aiMode),this.checkpointMaterial={...this.checkpointMaterial,organicResults:O},await this.emitProgress("serp_captured")}let z=t.debug?Qe(r,O,v):I;if(!E){await this.resolveMaterialLinks(M,[],O,L.aiOverview,L.aiMode),this.checkpointMaterial={...this.checkpointMaterial,organicResults:O},await this.emitProgress("serp_captured");let V={seed:c.query,totalQuestions:0,maxDepthReached:0,durationMs:Date.now()-a,errorCount:o};return this.reporter.onComplete(V),{seed:c.query,location:t.location??null,extractedAt:new Date().toISOString(),diagnostics:{phaseTimings:this.currentPhaseTimings(a),completionStatus:"no_paa",noPaaObserved:!0,problem:null,pagination:W,paaLifecycle:{...this.paaLifecycle},completeness:{...this.completeness},links:{...this.linkDiagnostics},...t.debug?{debug:this.buildHarvestDebugSnapshot(c,r,S,z,u)}:{}},totalQuestions:0,surface:L.surface,aiOverview:L.aiOverview,aiMode:L.aiMode,whatPeopleSaying:y,tree:[],flat:[],videos:P,forums:x,organicResults:O,localPack:v,entityIds:_,stats:V}}let D=Date.now(),Q=await this.runBFS(M,c,n);this.phaseTimings.paaExpansionMs=Date.now()-D,this.throwIfAborted(n);let ct=new URLSearchParams({q:c.query,gl:t.gl,hl:t.hl,pws:"0",udm:fn.udm});S&&ct.set("uule",S);let ft=[];if(t.includeShortVideos!==!1)try{ft=await this.extractShortVideos(M,"https://www.google.com/search?"+ct.toString())}catch(V){if(!(V instanceof nt))throw V;o++,d.push({code:"short_videos_captcha_skipped",surface:"short_videos",message:V.message,retryable:!0})}this.reporter.onVideos(ft),await this.resolveMaterialLinks(M,Q,O,L.aiOverview,L.aiMode),this.checkpointMaterial={...this.checkpointMaterial,surface:L.surface,aiOverview:L.aiOverview,aiMode:L.aiMode,videos:[...P,...ft],organicResults:O},await this.emitProgress("expansion_finished");let Z=[...P,...ft],J=this.buildTree(Q,c.query),ht={seed:c.query,totalQuestions:Q.length,maxDepthReached:Q.reduce((V,ot)=>Math.max(V,ot.depth),0),durationMs:Date.now()-a,errorCount:o};return this.reporter.onComplete(ht),{seed:c.query,location:t.location??null,extractedAt:new Date().toISOString(),diagnostics:{phaseTimings:this.currentPhaseTimings(a),completionStatus:"paa_found",problem:null,pagination:W,paaLifecycle:{...this.paaLifecycle},completeness:{...this.completeness},links:{...this.linkDiagnostics},...d.length>0?{warnings:d}:{},...t.debug?{debug:this.buildHarvestDebugSnapshot(c,r,S,z,u)}:{}},totalQuestions:Q.length,surface:L.surface,aiOverview:L.aiOverview,aiMode:L.aiMode,whatPeopleSaying:y,tree:J,flat:Q,videos:Z,forums:x,organicResults:O,localPack:v,entityIds:_,stats:ht}}catch(l){throw o++,this.paaLifecycle.discoveryStatus==="in_progress"&&(this.paaLifecycle={...this.paaLifecycle,discoveryStatus:"interrupted",completionReason:"The browser attempt ended before frontier exhaustion could be proven."}),await this.emitProgress("attempt_ending").catch(()=>{}),this.reporter.onError(l instanceof Error?l:new Error(String(l))),l}}};var it=require("fs"),At=Sn(require("path"),1),Zt=Sn(require("papaparse"),1);async function Xs(e){if((process.env.NODE_ENV==="test"||process.env.VITEST)&&process.env.MCP_SCRAPER_ENABLE_MEMORY_LIBRARY_SINK_IN_TESTS!=="1")return;let t=process.env.MCP_SCRAPER_API_KEY?.trim(),n=(process.env.MCP_SCRAPER_BASE_URL??"https://mcpscraper.dev").replace(/\/$/,"");if(t)try{let a=await fetch(`${n}/memory/mcp-call`,{method:"POST",headers:{"content-type":"application/json","x-api-key":t},body:JSON.stringify({toolName:"libraryIngestTool",args:e})}),i=await a.json().catch(()=>null);(!a.ok||i?.ok===!1)&&console.warn("[memory-library-sink] ingest not accepted:",a.status,i?.error??"")}catch(a){console.warn("[memory-library-sink] ingest failed:",a?.message)}}var Hn=class{async writeJSON(t,n){await it.promises.mkdir(n,{recursive:!0});let i=`${t.seed.toLowerCase().replace(/\W+/g,"-").slice(0,40)}-${Date.now()}.json`,s=At.default.join(n,i);return await it.promises.writeFile(s,JSON.stringify(t,null,2),"utf8"),await Xs({title:`${t.seed} scrape`,content:JSON.stringify(t),source:`mcp-scraper:${s}`}),s}async writeCSV(t,n){await it.promises.mkdir(n,{recursive:!0});let i=(t[0]?.seed_query??"paa").toLowerCase().replace(/\W+/g,"-").slice(0,40),s=Zt.default.unparse(t.map(({sources:l,...U})=>({...U,sources_json:JSON.stringify(l)})),{header:!0}),o=`${i}-${Date.now()}.csv`,d=At.default.join(n,o);return await it.promises.writeFile(d,s,"utf8"),d}async writeVideoCSV(t,n,a){await it.promises.mkdir(a,{recursive:!0});let i=n.toLowerCase().replace(/\W+/g,"-").slice(0,40),s=Zt.default.unparse(t,{header:!0}),o=`${i}-videos-${Date.now()}.csv`,d=At.default.join(a,o);return await it.promises.writeFile(d,s,"utf8"),d}async writeForumCSV(t,n,a){await it.promises.mkdir(a,{recursive:!0});let i=n.toLowerCase().replace(/\W+/g,"-").slice(0,40),s=Zt.default.unparse(t,{header:!0}),o=`${i}-forums-${Date.now()}.csv`,d=At.default.join(a,o);return await it.promises.writeFile(d,s,"utf8"),d}async writeAIOverviewCSV(t,n,a,i){await it.promises.mkdir(i,{recursive:!0});let s=a.toLowerCase().replace(/\W+/g,"-").slice(0,40),o=t.map((u,r)=>({seed_query:a,response_text:r===0?n??"":"",citation_text:u.text,citation_href:u.href})),d=Zt.default.unparse(o,{header:!0}),l=`${s}-ai-overview-${Date.now()}.csv`,U=At.default.join(i,l);return await it.promises.writeFile(U,d,"utf8"),U}async writeAIModeCSV(t,n,a,i){await it.promises.mkdir(i,{recursive:!0});let s=a.toLowerCase().replace(/\W+/g,"-").slice(0,40),o=t.map((u,r)=>({seed_query:a,response_text:r===0?n??"":"",citation_text:u.text,citation_href:u.href})),d=Zt.default.unparse(o,{header:!0}),l=`${s}-ai-mode-${Date.now()}.csv`,U=At.default.join(i,l);return await it.promises.writeFile(U,d,"utf8"),U}async writeWhatPeopleSayingCSV(t,n,a){await it.promises.mkdir(a,{recursive:!0});let i=n.toLowerCase().replace(/\W+/g,"-").slice(0,40),s=t.map(U=>({seed_query:n,...U})),o=Zt.default.unparse(s,{header:!0}),d=`${i}-what-people-saying-${Date.now()}.csv`,l=At.default.join(a,d);return await it.promises.writeFile(l,o,"utf8"),l}};var Bn=class{onQuestion(t){process.stdout.write(JSON.stringify({event:"question",depth:t.depth,question:t.question})+`
21895
+ `).map(v=>v.trim()).filter(v=>/^\d+\.\s+.+/.test(v)),h=u(S)}let C=document.querySelector(t.aim.root),A=a==="aim"&&!!C,R=C?.closest(t.aim.wrapper)??C,F=A?l(R):null,E=A&&s(F),M=E?F:null,P=E?u(R):[],x=!!S&&s(c);return{surface:a,aiOverview:{detected:x,text:x?c:null,citations:x?h:[],expanded:x?m:!1,fullyExpanded:x?k:!1,sections:x?f:[]},aiMode:{detected:E&&M!==null,text:M,citations:P}}}var Hs=require("crypto"),Bs=8e3,Ds=3e3,Fs=1e4,jd=8;function It(e){return/(^|\.)google\.(?:com|[a-z]{2,3}(?:\.[a-z]{2})?)$/i.test(e)}function tn(e){if(!e||e.length>Bs)return null;try{let t=new URL(e);return t.protocol!=="http:"&&t.protocol!=="https:"||t.username||t.password?null:t}catch{return null}}function zd(e,t){if(!e||e.length>Bs||/^javascript:/i.test(e))return null;try{let n=new URL(e,t);return tn(n.href)}catch{return null}}function Ze(e){let t=tn(e);return!t||t.protocol!=="https:"||!It(t.hostname)||t.pathname!=="/goto"?null:t.searchParams.get("url")?t:null}function Ra(e){if(e.linkType!=="google_goto_redirect"||e.resolutionStatus!=="resolved"||!Ze(e.rawUrl)||!e.resolvedUrl)return!1;let t=tn(e.resolvedUrl);return!!(t&&!It(t.hostname)&&t.href===e.resolvedUrl&&e.url===t.href)}function Qt(e){let t=e.normalize("NFKC").toLocaleLowerCase("en-US").replace(/[^\p{L}\p{N}\s]/gu,"").replace(/\s+/g," ").trim();return`paa_${(0,Hs.createHash)("sha256").update(t).digest("hex").slice(0,16)}`}function Wn(e,t){let n=zd(e,t);if(!n)return null;let a=n.href;if(!It(n.hostname))return{url:a,rawUrl:a,resolvedUrl:a,linkType:"plain",resolutionStatus:"not_needed"};if(n.pathname==="/url"){let i=tn(n.searchParams.get("q")??n.searchParams.get("url")??"");return!i||It(i.hostname)?null:{url:i.href,rawUrl:a,resolvedUrl:i.href,linkType:"google_url_redirect",resolutionStatus:"resolved"}}return n.pathname==="/goto"&&n.searchParams.get("url")?{url:a,rawUrl:a,resolvedUrl:null,linkType:"google_goto_redirect",resolutionStatus:"unresolved"}:null}function Wa(e){return Ze(e)?.searchParams.get("url")??null}function Gs(e,t){try{let n=new URL(e),a=tn(new URL(t,n).href);return!a||It(a.hostname)?null:(!a.hash&&n.hash&&(a.hash=n.hash),a.href)}catch{return null}}async function Vs(e,t={}){let n=[...new Set([...e].flatMap(r=>Ze(r)?[r]:[]))],a=new Map(n.map(r=>[r,{rawUrl:r,resolvedUrl:null,resolutionStatus:"unresolved"}])),i=typeof globalThis.fetch=="function"?globalThis.fetch.bind(globalThis):null,s=t.fetchImpl??i;if(n.length===0||!s)return{resolutions:[...a.values()],nonGoogleRequestsObserved:0};let o=Math.max(1,t.timeoutMs??Ds),d=Date.now()+Math.max(1,t.totalBudgetMs??Fs),l=Math.max(1,Math.min(16,Math.floor(t.concurrency??jd))),U=0,u=async()=>{for(;U<n.length;){let r=n[U++],S=Ze(r),c=S?.searchParams.get("url");if(!S||!c){a.set(r,{rawUrl:r,resolvedUrl:null,resolutionStatus:"rejected"});continue}S.hash="";let h=S.href;for(let m=0;m<2;m++){let k=d-Date.now();if(k<=0)return;let f=new AbortController,C=setTimeout(()=>f.abort(),Math.min(o,k)),A;try{A=await s(h,{method:"GET",redirect:"manual",headers:{accept:"*/*","user-agent":"Mozilla/5.0"},signal:f.signal})}catch{break}finally{clearTimeout(C)}if(A.status<300||A.status>=400)break;let R=A.headers.get("location");if(!R)break;let F=Gs(r,R);if(F){a.set(r,{rawUrl:r,resolvedUrl:F,resolutionStatus:"resolved"});break}let E;try{E=new URL(R,h)}catch{a.set(r,{rawUrl:r,resolvedUrl:null,resolutionStatus:"rejected"});break}if(!It(E.hostname)||Wa(E.href)!==c){a.set(r,{rawUrl:r,resolvedUrl:null,resolutionStatus:"rejected"});break}h=E.href}}};return await Promise.all(Array.from({length:Math.min(l,n.length)},()=>u())),{resolutions:[...a.values()],nonGoogleRequestsObserved:0}}function Ws(e,t){try{return new URL(e.resolvedUrl??e.url).hostname.replace(/^www\./,"")}catch{return t}}async function Ys(e,t={}){let n=[],a=r=>{for(let S of r)n.push(S),a(S.children??[])};a(e.tree??[]);let i=[...e.flat.flatMap(r=>r.sources??[]),...n.flatMap(r=>r.sources),...e.organicResults??[],...(e.aiOverview?.citations??[]).map(({text:r,href:S,...c})=>({...c,url:S})),...(e.aiMode?.citations??[]).map(({text:r,href:S,...c})=>({...c,url:S}))],s=new Map(i.filter(r=>!!r?.rawUrl).map(r=>[r.rawUrl,r])),o=[...s.values()].filter(r=>r.linkType==="google_goto_redirect"&&r.resolutionStatus!=="resolved").map(r=>r.rawUrl);if(o.length>0){let r=await Vs(o,t);for(let S of r.resolutions){let c=s.get(S.rawUrl);c&&s.set(S.rawUrl,S.resolutionStatus==="resolved"&&S.resolvedUrl?{...c,url:S.resolvedUrl,resolvedUrl:S.resolvedUrl,resolutionStatus:"resolved"}:{...c,resolutionStatus:S.resolutionStatus})}}let d=r=>({...r,...s.get(r.rawUrl)??r}),l=r=>r.map(S=>{let c=d(S);return{...c,site:Ws(c,S.site)}});for(let r of e.flat){r.sources=l(r.sources??[]);let S=r.sources[0];r.source_title=S?.title??r.source_title,r.source_site=S?.site??r.source_site,r.source_cite=S?.url??r.source_cite}for(let r of n){r.sources=l(r.sources??[]);let S=r.sources[0];r.sourceTitle=S?.title??r.sourceTitle,r.sourceSite=S?.site??r.sourceSite,r.sourceCite=S?.url??r.sourceCite}for(let r=0;r<(e.organicResults?.length??0);r++){let S=d(e.organicResults[r]);e.organicResults[r]={...S,domain:Ws(S,e.organicResults[r].domain)??""}}let U=r=>{let S=d({url:r.href,rawUrl:r.rawUrl,resolvedUrl:r.resolvedUrl,linkType:r.linkType,resolutionStatus:r.resolutionStatus}),{url:c,...h}=S;return{...r,...h,href:c}};e.aiOverview&&(e.aiOverview.citations=(e.aiOverview.citations??[]).map(U)),e.aiMode&&(e.aiMode.citations=(e.aiMode.citations??[]).map(U));let u=[...s.values()];return e.diagnostics.links={linkCandidatesObserved:s.size,plainLinksObserved:u.filter(r=>r.linkType==="plain").length,googleUrlRedirectsObserved:u.filter(r=>r.linkType==="google_url_redirect").length,googleGotoRedirectsObserved:u.filter(r=>r.linkType==="google_goto_redirect").length,redirectsResolved:u.filter(r=>r.linkType!=="plain"&&r.resolutionStatus==="resolved").length,redirectsUnresolved:u.filter(r=>r.resolutionStatus==="unresolved").length,redirectsRejected:u.filter(r=>r.resolutionStatus==="rejected").length,resolverNonGoogleRequestsObserved:0,paaSourcesPreserved:e.flat.reduce((r,S)=>r+S.sources.length,0),aiOverviewLabelValidated:e.aiOverview?.detected??!1},e}async function Ks(e,t,n={}){let a=new Map;for(let S of t){let c=a.get(S.rawUrl);c&&Ra(c)||a.set(S.rawUrl,S.linkType==="google_goto_redirect"&&!Ra(S)?{...S,url:S.rawUrl,resolvedUrl:null,resolutionStatus:"unresolved"}:S)}let i=[...a.values()].filter(S=>S.linkType==="google_goto_redirect"&&!Ra(S));if(i.length===0)return a;let s=Date.now()+(n.totalBudgetMs??Fs),o=await Vs(i.map(S=>S.rawUrl),n);for(let S of o.resolutions){let c=a.get(S.rawUrl);c&&a.set(S.rawUrl,S.resolutionStatus==="resolved"&&S.resolvedUrl?{...c,url:S.resolvedUrl,resolvedUrl:S.resolvedUrl,resolutionStatus:"resolved"}:{...c,resolutionStatus:S.resolutionStatus})}let d=i.filter(S=>a.get(S.rawUrl)?.resolutionStatus==="unresolved");if(d.length===0||n.browserFallback===!1||Date.now()>=s)return n.onNetworkAudit?.({nonGoogleRequestsObserved:0}),a;let l=n.timeoutMs??Ds,U=await e.context().newPage().catch(()=>null);if(!U)return n.onNetworkAudit?.({nonGoogleRequestsObserved:0}),a;let u=null,r=0;try{await U.route("**/*",async S=>{let c=S.request().url();try{It(new URL(c).hostname)||(r+=1)}catch{}let h=Wa(c);if(!u||h!==u.token){await S.abort("blockedbyclient").catch(()=>{});return}let m=u;try{let k=await S.fetch({maxRedirects:0,timeout:m.timeoutMs});m.location=k.headers().location??null}finally{await S.abort("blockedbyclient").catch(()=>{}),m.finish()}});for(let S of d){let c=s-Date.now();if(c<=0)break;let h=Ze(S.rawUrl),m=h?.searchParams.get("url");if(!h||!m){a.set(S.rawUrl,{...S,resolutionStatus:"rejected"});continue}let k=Math.min(l,c);h.hash="";let f=h.href,C=!1;for(let A=0;A<2;A++){let R,F=new Promise(y=>{R=y}),E=`mcp-goto-${Math.random().toString(36).slice(2)}`;u={token:m,timeoutMs:k,location:null,finish:R},await U.evaluate(({url:y,id:N})=>{let p=document.createElement("iframe");p.id=N,p.hidden=!0,p.src=y,document.body.append(p)},{url:f,id:E}).catch(()=>null),await Promise.race([F,new Promise(y=>setTimeout(y,k))]);let M=u.location;if(u=null,await U.evaluate(y=>document.getElementById(y)?.remove(),E).catch(()=>null),!M)break;let P=Gs(S.rawUrl,M);if(P){a.set(S.rawUrl,{...S,url:P,resolvedUrl:P,resolutionStatus:"resolved"});break}let x;try{x=new URL(M,f)}catch{C=!0;break}if(!It(x.hostname)||Wa(x.href)!==m){C=!0;break}f=x.href}C&&a.get(S.rawUrl)?.resolutionStatus!=="resolved"&&a.set(S.rawUrl,{...S,resolutionStatus:"rejected"})}}finally{u=null,await U.close().catch(()=>{}),n.onNetworkAudit?.({nonGoogleRequestsObserved:r})}return a}var Js=String.raw`^(?:an error has occurred(?:[.!…]\s*)?(?:please try again later[.!…]?)?|loading|please wait|try again|something went wrong)[.!…\s]*$`,$d=700,Qd=1e3,Zd=1400,qs=900,tl=3,Xs=250,Hn=class{constructor(t,n,a,i){this.driver=t;this.reporter=n;this.onPaaProgress=a;this.onProviderSession=i}driver;reporter;onPaaProgress;onProviderSession;completeness={paaWithoutAnswer:0,paaWithoutSource:0,paaAnswersRecovered:0,aioShareCaptured:null};aioShareUrlEarly=null;linkDiagnostics=this.emptyLinkDiagnostics();collectedRows=[];partialContext=null;progressSequence=0;clickedQuestions=new Set;paaLifecycle=this.emptyPaaLifecycle(0);checkpointMaterial=null;interactionObservations=[];phaseTimings={};currentPhaseTimings(t){return{...this.phaseTimings,totalServerMs:Math.max(0,Date.now()-t)}}emptyPaaLifecycle(t){return{requestedQuestions:t,questionsFound:0,targetStatus:"not_met",discoveryStatus:"in_progress",materialStatus:"partial",completionReason:"PAA expansion is still running.",automaticRetries:0,retryBreakdown:{controlRetries:0,browserRetries:0,providerFallbacks:0,runtimeRecoveries:0,idempotentReplays:0},knownQuestions:0,processedQuestions:0,failedInteractions:0,noGrowthConfirmations:0,visibleControls:0,eligibleControls:0,dispatchedInteractions:0,confirmedInteractions:0,unconfirmedInteractions:0}}emptyLinkDiagnostics(){return{linkCandidatesObserved:0,plainLinksObserved:0,googleUrlRedirectsObserved:0,googleGotoRedirectsObserved:0,redirectsResolved:0,redirectsUnresolved:0,redirectsRejected:0,resolverNonGoogleRequestsObserved:0,paaSourcesPreserved:0,aiOverviewLabelValidated:!1}}mergePaaSources(t=[],n=[]){let a=new Map;for(let i of[...t,...n]){let s=a.get(i.rawUrl);if(!s){a.set(i.rawUrl,i);continue}let o=i.resolutionStatus==="resolved"&&s.resolutionStatus!=="resolved"?i:s;a.set(i.rawUrl,{...o,title:o.title??s.title??i.title,site:o.site??s.site??i.site})}return[...a.values()]}normalizeAICitations(t,n){let a=new Set;return n.flatMap(i=>{let s=Wn(i.rawHref??i.href,t);if(!s||a.has(s.rawUrl))return[];a.add(s.rawUrl);let{url:o,...d}=s;return[{...d,text:i.text,href:o}]})}async resolveMaterialLinks(t,n,a,i,s){let o=Date.now(),d=[...n.flatMap(h=>h.sources??[]),...a,...i.citations.map(({text:h,href:m,...k})=>({...k,url:m})),...s.citations.map(({text:h,href:m,...k})=>({...k,url:m}))],l=new Map(d.map(h=>[h.rawUrl,h])),U=0,u=await Ks(t,d,{onNetworkAudit:h=>{U=h.nonGoogleRequestsObserved}}),r=h=>({...h,...u.get(h.rawUrl)??h});for(let h of n){h.sources=(h.sources??[]).map(r);let m=h.sources[0];h.source_title=m?.title??h.source_title,h.source_site=m?.site??h.source_site,h.source_cite=m?.url??h.source_cite}for(let h=0;h<a.length;h++){let m=r(a[h]);if(m.resolvedUrl)try{m.domain=new URL(m.resolvedUrl).hostname.replace(/^www\./,"")}catch{}a[h]=m}let S=h=>{let m=r({url:h.href,rawUrl:h.rawUrl,resolvedUrl:h.resolvedUrl,linkType:h.linkType,resolutionStatus:h.resolutionStatus}),{url:k,...f}=m;return{...f,text:h.text,href:k}};i.citations=i.citations.map(S),s.citations=s.citations.map(S);let c=[...u.values()];this.linkDiagnostics={linkCandidatesObserved:l.size,plainLinksObserved:c.filter(h=>h.linkType==="plain").length,googleUrlRedirectsObserved:c.filter(h=>h.linkType==="google_url_redirect").length,googleGotoRedirectsObserved:c.filter(h=>h.linkType==="google_goto_redirect").length,redirectsResolved:c.filter(h=>h.linkType!=="plain"&&h.resolutionStatus==="resolved").length,redirectsUnresolved:c.filter(h=>h.resolutionStatus==="unresolved").length,redirectsRejected:c.filter(h=>h.resolutionStatus==="rejected").length,resolverNonGoogleRequestsObserved:U,paaSourcesPreserved:n.reduce((h,m)=>h+(m.sources??[]).length,0),aiOverviewLabelValidated:i.detected},this.phaseTimings.linkResolutionMs=(this.phaseTimings.linkResolutionMs??0)+(Date.now()-o)}normalizeQuestion(t){return t.toLowerCase().replace(/[^\w\s]/g,"").replace(/\s+/g," ").trim()}throwIfAborted(t){if(t?.aborted)throw t.reason instanceof DOMException&&t.reason.name==="TimeoutError"?t.reason:new ut}async throwIfCaptcha(t,n){if(await t.locator(K.captchaMarker).count().catch(()=>0)>0)throw new nt(`${n} returned a CAPTCHA \u2014 retrying with a fresh session.`)}async extractVisibleItems(t,n){let a=K,i=await t.evaluate(({selectors:o,onlyQuestion:d,answerPlaceholderPattern:l})=>{function U(r){if(!r)return"";let S=[];for(let c of r.childNodes)if(c.nodeType===Node.TEXT_NODE){let h=c.textContent?.trim();h&&S.push(h)}else{if(c.tagName==="STYLE"||c.tagName==="SCRIPT")continue;{let h=U(c);h&&S.push(h)}}return S.join(" ").replace(/\s+/g," ").trim()}function u(r){return(r??"").replace(/\s*\(\+\d+\)\s*-\s*View related links.*$/i,"").replace(/\s*-\s*Opens in new tab.*$/i,"").trim()}return Array.from(document.querySelectorAll(o.item)).filter(r=>!d||(r.getAttribute(o.itemDataQ)||r.getAttribute(o.itemDataInitQ)||r.querySelector(o.itemQuestionEl)?.innerText?.trim()||"")===d).map(r=>{let S=r.querySelector(o.clickTarget),c=r.classList.contains(o.expandedClass)||S?.getAttribute("aria-expanded")==="true",h=r.querySelector(o.answerContainer),k=h?.querySelector(o.aiAnswerBody)??h,f=U(k),C=new RegExp(l,"i").test(f),A=[...Array.from((k??r).querySelectorAll("a[href]")),...Array.from(r.querySelectorAll(o.aiCitation)),...Array.from(r.querySelectorAll(o.aiSourceCard))],R=!C&&(c&&f.length>=10||f.length>30&&A.some(P=>/^https?:\/\//.test(P.href))),F=R&&r.querySelector(o.sourceTitle)?.innerText?.trim()||void 0,E=R&&r.querySelector(o.sourceSite)?.innerText?.trim()||void 0,M=[];if(R){let P=new Set;for(let y of A)!/^https?:\/\//.test(y.href)||P.has(y.href)||(P.add(y.href),M.push({title:u(y.getAttribute("aria-label"))||(y.textContent||"").trim(),rawHref:y.href}));let x=M.find(y=>y.title.length>2)??M[0];x&&(F=F||x.title||void 0)}return{googleLinkId:r.getAttribute("data-lk")||r.querySelector("[data-lk]")?.getAttribute("data-lk")||null,googleEvidenceId:r.getAttribute("data-ved")||r.querySelector("[data-ved]")?.getAttribute("data-ved")||null,question:r.getAttribute(o.itemDataQ)||r.getAttribute(o.itemDataInitQ)||r.querySelector(o.itemQuestionEl)?.innerText?.trim()||"",answer:R&&f||void 0,sourceTitle:F,sourceSite:E,sources:M}})},{selectors:a,onlyQuestion:n,answerPlaceholderPattern:Js}),s=typeof t.url=="function"?t.url():"https://www.google.com/";return i.flatMap(o=>{let d=[],l=new Set;for(let S of o.sources){let c=Wn(S.rawHref,s);if(!c||l.has(c.rawUrl))continue;l.add(c.rawUrl);let h=o.sourceSite??null;try{h||=new URL(c.resolvedUrl??c.rawUrl).hostname.replace(/^www\./,"")}catch{}d.push({...c,title:S.title||o.sourceTitle||null,site:h})}let U=d[0],u={questionId:Qt(o.question),googleLinkId:o.googleLinkId,googleEvidenceId:o.googleEvidenceId,question:o.question,answer:Os(o.answer,o.question,o.sourceTitle),sourceTitle:U?.title??o.sourceTitle,sourceSite:U?.site??o.sourceSite,sourceCite:U?.url,sources:d},r=li.safeParse(u);return r.success?[r.data]:(console.warn("[PAAExtractor] item parse failed:",o.question,r.error.issues[0]?.message),[])})}jitteredDelayMs(t,n){return t+Math.floor(Math.random()*(n-t+1))}async clickItem(t,n){try{await t.locator(`${K.item}[data-q="${n}"], ${K.item}[data-initq="${n}"]`).first().click()}catch{}}toFlatRow(t,n,a,i){return{seed_query:i,question_id:t.questionId??Qt(t.question),google_link_id:t.googleLinkId??"",google_evidence_id:t.googleEvidenceId??"",question:t.question,answer:t.answer??"",source_title:t.sourceTitle??"",source_site:t.sourceSite??"",source_cite:t.sourceCite??"",sources:t.sources??[],depth:n,parent_question:a??"",extracted_at:new Date().toISOString()}}upsertCollectedRow(t){let n=this.normalizeQuestion(t.question),a=this.collectedRows.findIndex(d=>this.normalizeQuestion(d.question)===n);if(a<0)return this.collectedRows.push({...t}),!0;let i=this.collectedRows[a],s={...i,...t,answer:t.answer||i.answer,source_title:t.source_title||i.source_title,source_site:t.source_site||i.source_site,source_cite:t.source_cite||i.source_cite,sources:this.mergePaaSources(i.sources,t.sources),extracted_at:t.extracted_at||i.extracted_at},o=s.answer!==i.answer||s.source_title!==i.source_title||s.source_site!==i.source_site||s.source_cite!==i.source_cite||JSON.stringify(s.sources)!==JSON.stringify(i.sources);return o&&(this.collectedRows[a]=s),o}async emitProgress(t){if(!this.onPaaProgress||this.collectedRows.length===0&&!this.checkpointMaterial)return;let n={sequence:++this.progressSequence,observedAt:new Date().toISOString(),clickedQuestions:[...this.clickedQuestions],phase:t,records:this.collectedRows.map(a=>({...a})),lifecycle:{...this.paaLifecycle},...this.checkpointMaterial?{material:structuredClone(this.checkpointMaterial)}:{},interactions:this.interactionObservations.map(a=>({...a,confirmationSignals:[...a.confirmationSignals]}))};try{await this.onPaaProgress(n)}catch(a){console.warn(JSON.stringify({event:"paa_progress_sink_failed",sequence:n.sequence,phase:n.phase,message:a instanceof Error?a.message:String(a)}))}}getPartialResult(){let t=this.partialContext;if(!t||this.collectedRows.length===0&&!this.checkpointMaterial)return null;let n=this.collectedRows.map(d=>({...d})),a=this.checkpointMaterial,i=t.questionsOnly?0:n.filter(d=>!d.answer?.trim()).length,s=t.questionsOnly?0:n.filter(d=>(d.sources??[]).length===0).length,o={seed:t.seed,totalQuestions:n.length,maxDepthReached:n.reduce((d,l)=>Math.max(d,l.depth),0),durationMs:Date.now()-t.startMs,errorCount:0};return{seed:t.seed,location:t.location,extractedAt:new Date().toISOString(),diagnostics:{phaseTimings:this.currentPhaseTimings(t.startMs),completionStatus:"paa_found",problem:null,...a?.pagination?{pagination:{...a.pagination}}:{},resultQuality:"partial",degradedResult:!1,retryRecommended:!0,paaLifecycle:{...this.paaLifecycle,questionsFound:n.length,targetStatus:n.length>=(t.maxQuestions??0)?"met":"not_met",discoveryStatus:"interrupted",materialStatus:i>0||s>0?"partial":"complete",completionReason:"The browser attempt ended before frontier exhaustion could be proven."},completeness:{...this.completeness,paaWithoutAnswer:i,paaWithoutSource:s,paaRequested:t.maxQuestions,paaReturned:n.length,paaUnique:n.length},warnings:[{code:"paa_partial",surface:"paa",message:`Harvest stopped before finishing; returning the ${n.length} question(s) collected up to that point.`,retryable:!0}]},totalQuestions:n.length,surface:a?.surface??"web",aiOverview:a?.aiOverview??{detected:!1,text:null,citations:[]},aiMode:a?.aiMode??{detected:!1,text:null,citations:[]},whatPeopleSaying:a?.whatPeopleSaying??[],tree:this.buildTree(n,t.seed),flat:n,videos:a?.videos??[],forums:a?.forums??[],organicResults:a?.organicResults??[],localPack:a?.localPack??[],entityIds:a?.entityIds??{entities:[],kgIds:[],cids:[],gcids:[]},stats:o}}async runBFS(t,n,a){await t.waitForTimeout(700);let i=new Set,s=new Set,o=[];this.clickedQuestions=new Set,this.interactionObservations=[],this.phaseTimings={};let d=new Set,l=new Map,U=new Set,u=new Set,r=new Map,S=2,c=3,h=0,m={sel:K.item,dataQ:K.itemDataQ,dataInitQ:K.itemDataInitQ,questionEl:K.itemQuestionEl,answerSel:K.answerContainer,expandedClass:K.expandedClass,clickTarget:K.clickTarget,answerPlaceholderPattern:Js,questionsOnly:n.questionsOnly,sourceSel:"a[href]"},k=()=>t.evaluate(({sel:E,dataQ:M,dataInitQ:P,questionEl:x,expandedClass:y,clickTarget:N,answerSel:p,answerPlaceholderPattern:v,questionsOnly:_})=>{let I=L=>{if(!L)return"";let W=[];for(let O of L.childNodes){if(O.nodeType===Node.TEXT_NODE){let Q=O.textContent?.trim();Q&&W.push(Q);continue}if(O.nodeType!==Node.ELEMENT_NODE)continue;let z=O;if(z.tagName==="STYLE"||z.tagName==="SCRIPT"||z.getAttribute("aria-hidden")==="true")continue;let D=I(z);D&&W.push(D)}return W.join(" ").replace(/\s+/g," ").trim()};return Array.from(document.querySelectorAll(E)).map((L,W)=>{let O=I(L.querySelector(p)).replace(/An AI Overview is not available for this search/gi," ").replace(/Can't generate an AI overview right now\.?\s*Try again later\.?/gi," ").replace(/\bAI Overview\b/gi," ").replace(/\s+/g," ").trim(),z=L.classList.contains(y)||L.querySelector(N)?.getAttribute("aria-expanded")==="true",D=O.length>=10&&!new RegExp(v,"i").test(O);return{q:L.getAttribute(M)||L.getAttribute(P)||L.querySelector(x)?.innerText?.trim()||"",done:z&&(_||D),expanded:z,index:W}}).filter(L=>L.q)},m),f=async(E,M,P,x,y,N)=>{let p=Date.now(),v={id:`${N}:${Qt(E)}:${y}`,questionId:Qt(E),question:E,batchNumber:N,controlAttempt:y,observedAt:new Date(p).toISOString(),beforeQuestionCount:M,expandedBefore:x},_=P>=0?t.locator(m.sel).nth(P).locator(m.clickTarget):null,I=null,L=_?await(async()=>(x&&(await _.click({timeout:qs,force:!0}),await t.waitForTimeout(120)),await _.click({timeout:qs,force:!0}),!0))().catch(J=>(I=J instanceof Error&&(J.name==="TimeoutError"||/timeout/i.test(J.message))?"click_ack_timeout":"click_failed",!1)):!1,W=Date.now()-p;if(!L&&I!=="click_ack_timeout")return{dispatched:!1,confirmed:!1,observation:{...v,dispatched:!1,outcome:"dispatch_failed",confirmationSignals:[],confirmationCheckpointMs:null,confirmedAtMs:null,dispatchDurationMs:W,totalDurationMs:W,afterQuestionCount:M,newQuestionCount:0,expandedAfter:x,answerObserved:!1,sourceCount:0,errorCode:P<0?"control_missing":I??"click_failed"}};let O={exists:!0,expanded:x,answerReady:!1,sourceCount:0,questionCount:M},z=[],D=null,Q=null,ct=!1;for(let J of[$d,Qd,Zd]){let ht=p+J-Date.now();if(ht>0&&await t.waitForTimeout(ht),O=await t.evaluate(({sel:V,dataQ:ot,dataInitQ:pt,questionEl:Le,expandedClass:xe,clickTarget:Ot,answerSel:nn,answerPlaceholderPattern:ne,sourceSel:an,target:Oe})=>{let sn=ie=>{if(!ie)return"";let jn=[];for(let rn of ie.childNodes){if(rn.nodeType===Node.TEXT_NODE){let oi=rn.textContent?.trim();oi&&jn.push(oi);continue}if(rn.nodeType!==Node.ELEMENT_NODE)continue;let dn=rn;if(dn.tagName==="STYLE"||dn.tagName==="SCRIPT"||dn.getAttribute("aria-hidden")==="true")continue;let si=sn(dn);si&&jn.push(si)}return jn.join(" ").replace(/\s+/g," ").trim()},ae=Array.from(document.querySelectorAll(V)),on=ae.find(ie=>(ie.getAttribute(ot)||ie.getAttribute(pt)||ie.querySelector(Le)?.innerText?.trim())===Oe);if(!on)return{exists:!1,expanded:!1,answerReady:!1,sourceCount:0,questionCount:ae.length};let No=on.classList.contains(xe)||on.querySelector(Ot)?.getAttribute("aria-expanded")==="true",ni=on.querySelector(nn),ai=sn(ni).replace(/An AI Overview is not available for this search/gi," ").replace(/Can't generate an AI overview right now\.?\s*Try again later\.?/gi," ").replace(/\bAI Overview\b/gi," ").replace(/\s+/g," ").trim(),ii=ai.length>=10&&!new RegExp(ne,"i").test(ai);return{exists:!0,expanded:No,answerReady:ii,sourceCount:ii?ni?.querySelectorAll(an).length??0:0,questionCount:ae.length}},{...m,target:E}).catch(()=>O),z=[...O.expanded?["expanded"]:[],...O.answerReady?["answer"]:[],...O.sourceCount>0?["source"]:[],...O.questionCount>M?["question_growth"]:[]],O.answerReady||O.sourceCount>0||O.questionCount>M){ct=!0,D=J,Q=Date.now()-p;break}}let ft=Date.now()-p,Z={...v,dispatched:L,outcome:ct?"confirmed":L?"unconfirmed":"dispatch_failed",confirmationSignals:z,confirmationCheckpointMs:D,confirmedAtMs:Q,dispatchDurationMs:W,totalDurationMs:ft,afterQuestionCount:O.questionCount,newQuestionCount:Math.max(0,O.questionCount-M),expandedAfter:O.expanded,answerObserved:O.answerReady,sourceCount:O.sourceCount,errorCode:L?ct?null:O.exists?"confirmation_timeout":"control_missing":I};return{dispatched:L,confirmed:ct,observation:Z}},C=0;t:for(;;){if(n.softDeadlineMs&&Date.now()>=n.softDeadlineMs)throw this.paaLifecycle={...this.paaLifecycle,discoveryStatus:"interrupted",completionReason:"The execution deadline arrived before frontier exhaustion could be proven."},new jt(this.paaLifecycle.completionReason);this.throwIfAborted(a);let E=await k(),M=!1;for(let p of E)s.has(p.q)||(s.add(p.q),o.push(p.q),o.length<=n.maxQuestions&&(M=this.upsertCollectedRow(this.toFlatRow({questionId:Qt(p.q),googleLinkId:null,googleEvidenceId:null,question:p.q,sources:[]},1,null,n.query))||M)),p.done&&U.add(p.q);if(this.paaLifecycle={...this.paaLifecycle,questionsFound:Math.min(o.length,n.maxQuestions),targetStatus:o.length>=n.maxQuestions?"met":"not_met",knownQuestions:o.length,processedQuestions:U.size,failedInteractions:u.size,visibleControls:E.length},M&&await this.emitProgress("discovered"),n.questionsOnly&&s.size>=n.maxQuestions){this.paaLifecycle={...this.paaLifecycle,discoveryStatus:"target_reached",targetStatus:"met",completionReason:"The requested unique-question count was observed."};break}if(!n.questionsOnly&&s.size>=n.maxQuestions){let p=!1,v=new Set(o.slice(0,n.maxQuestions));for(let _ of await this.extractVisibleItems(t)){if(!v.has(_.question))continue;let I=r.get(_.question),L={..._,answer:_.answer??I?.answer,sourceTitle:_.sourceTitle??I?.sourceTitle,sourceSite:_.sourceSite??I?.sourceSite,sourceCite:_.sourceCite??I?.sourceCite,sources:this.mergePaaSources(I?.sources,_.sources)};r.set(_.question,L),p=this.upsertCollectedRow(this.toFlatRow(L,1,null,n.query))||p}if(p&&await this.emitProgress("answer_captured"),[...v].every(_=>{let I=r.get(_);return!!(I?.answer?.trim()&&I.sources.length>0)})){u.clear(),this.paaLifecycle={...this.paaLifecycle,discoveryStatus:"target_reached",targetStatus:"met",failedInteractions:0,completionReason:"The requested unique-question count and its answer/source material were observed."};break}}let P=new Set(o.slice(0,n.maxQuestions)),x=E.filter(p=>P.has(p.q)&&!p.done&&(l.get(p.q)??0)<S&&!d.has(p.q)),y=x.slice(-tl);if(this.paaLifecycle={...this.paaLifecycle,eligibleControls:x.length},y.length===0){if(s.size>=n.maxQuestions){this.paaLifecycle={...this.paaLifecycle,discoveryStatus:"target_reached",targetStatus:"met",completionReason:"The requested unique-question count was observed."};break}let p=new Set(E.map(_=>_.q)),v=o.slice(0,n.maxQuestions).filter(_=>!U.has(_)&&(u.has(_)||!p.has(_)));if(v.length>0)throw this.paaLifecycle={...this.paaLifecycle,discoveryStatus:"interrupted",failedInteractions:new Set([...u,...v]).size,completionReason:"At least one known PAA control failed or disappeared before it could be confirmed."},await this.emitProgress("expansion_finished"),new jt(this.paaLifecycle.completionReason);for(let _=1;_<=c;_++){this.throwIfAborted(a),await this.throwIfCaptcha(t,"Google PAA exhaustion confirmation"),await t.mouse.wheel(0,320);let I=await t.waitForFunction(({sel:W,min:O})=>document.querySelectorAll(W).length>O,{sel:K.item,min:E.length},{timeout:1500,polling:100}).catch(()=>null);if(this.paaLifecycle={...this.paaLifecycle,noGrowthConfirmations:_},I||(await k()).some(W=>!s.has(W.q)))continue t}this.paaLifecycle={...this.paaLifecycle,discoveryStatus:"frontier_exhausted",targetStatus:"not_met",failedInteractions:0,noGrowthConfirmations:c,completionReason:"Every known eligible PAA control was processed and three healthy no-growth confirmations found no additional questions."};break}await this.throwIfCaptcha(t,"Google PAA expansion"),h+=1;for(let p of y){if(this.reporter.onDepth(++C),(l.get(p.q)??0)>0){let v=this.paaLifecycle.retryBreakdown.controlRetries+1;this.paaLifecycle={...this.paaLifecycle,automaticRetries:v+this.paaLifecycle.retryBreakdown.browserRetries+this.paaLifecycle.retryBreakdown.runtimeRecoveries,retryBreakdown:{...this.paaLifecycle.retryBreakdown,controlRetries:v}}}l.set(p.q,(l.get(p.q)??0)+1)}let N=await Promise.all(y.map(async p=>({target:p,result:await f(p.q,E.length,p.index,p.expanded,l.get(p.q)??1,h)})));for(let{target:p,result:v}of N)(v.dispatched||v.confirmed)&&this.clickedQuestions.add(p.q),this.interactionObservations.push(v.observation);if(this.interactionObservations.length>Xs&&this.interactionObservations.splice(0,this.interactionObservations.length-Xs),!n.questionsOnly){let p=new Set(y.map(v=>v.q));for(let v of await this.extractVisibleItems(t))p.has(v.question)&&(r.set(v.question,v),this.upsertCollectedRow(this.toFlatRow(v,1,null,n.query))&&await this.emitProgress("answer_captured"))}for(let{target:p,result:v}of N){let _=r.get(p.q);v.confirmed||_?.answer?.trim()?(U.add(p.q),u.delete(p.q)):(l.get(p.q)??0)>=S&&(u.add(p.q),d.add(p.q))}this.paaLifecycle={...this.paaLifecycle,processedQuestions:U.size,failedInteractions:u.size,noGrowthConfirmations:0,dispatchedInteractions:this.interactionObservations.filter(p=>p.dispatched).length,confirmedInteractions:this.interactionObservations.filter(p=>p.outcome==="confirmed").length,unconfirmedInteractions:this.interactionObservations.filter(p=>p.outcome!=="confirmed").length},await this.emitProgress("click_observed")}let A=n.questionsOnly?new Map:new Map(r);n.questionsOnly||await this.fillIncompleteItems(t,o,A,n,d);let R=[];for(let E of o){if(R.length>=n.maxQuestions)break;let M=this.normalizeQuestion(E);if(i.has(M))continue;i.add(M);let P=A.get(E);if(P){let x=this.toFlatRow(P,1,null,n.query);this.upsertCollectedRow(x),R.push(x),this.reporter.onQuestion({questionId:P.questionId,googleLinkId:P.googleLinkId,googleEvidenceId:P.googleEvidenceId,question:P.question,answer:P.answer??null,sourceTitle:P.sourceTitle??null,sourceSite:P.sourceSite??null,sourceCite:P.sourceCite??null,sources:P.sources,depth:1,parentQuestion:null,children:[]})}else{let x=this.toFlatRow({questionId:Qt(E),googleLinkId:null,googleEvidenceId:null,question:E,answer:void 0,sourceTitle:void 0,sourceSite:void 0,sourceCite:void 0,sources:[]},1,null,n.query);this.upsertCollectedRow(x),R.push(x)}}this.completeness.paaWithoutAnswer=n.questionsOnly?0:R.filter(E=>!E.answer).length,this.completeness.paaWithoutSource=n.questionsOnly?0:R.filter(E=>(E.sources??[]).length===0).length,this.completeness.paaRequested=n.maxQuestions,this.completeness.paaReturned=R.length,this.completeness.paaUnique=i.size;let F=!n.questionsOnly&&(this.completeness.paaWithoutAnswer>0||this.completeness.paaWithoutSource>0);return this.paaLifecycle={...this.paaLifecycle,questionsFound:R.length,targetStatus:R.length>=n.maxQuestions?"met":"not_met",materialStatus:F?"partial":"complete",processedQuestions:U.size,failedInteractions:u.size},await this.emitProgress("fill_incomplete"),await this.emitProgress("expansion_finished"),R}async fillIncompleteItems(t,n,a,i,s){let o=new Set(n.slice(0,i.maxQuestions)),d=u=>!u?.answer||(u.sources??[]).length===0,l=new Set([...o].filter(u=>d(a.get(u)))),U=new Set([...l].filter(u=>!a.get(u)?.answer));if(l.size!==0){for(let u of l){if(i.softDeadlineMs&&Date.now()>=i.softDeadlineMs)break;let r=(await this.extractVisibleItems(t,u))[0];if(!r)continue;let S=a.get(u);a.set(u,{...r,answer:r.answer??S?.answer,sourceTitle:r.sourceTitle??S?.sourceTitle,sourceSite:r.sourceSite??S?.sourceSite,sourceCite:r.sourceCite??S?.sourceCite,sources:this.mergePaaSources(S?.sources,r.sources)});let c=a.get(u);c&&this.upsertCollectedRow(this.toFlatRow(c,1,null,i.query))&&await this.emitProgress("fill_incomplete")}this.completeness.paaAnswersRecovered=[...U].filter(u=>a.get(u)?.answer).length}}async reExpandCollapsedItems(t){let n=`${K.item}:not(.${K.expandedClass}) ${K.clickTarget}`,a=await t.locator(n).count().catch(()=>0);if(a===0)return!1;for(let i=0;i<a;i++)try{let s=t.locator(n).first();await s.scrollIntoViewIfNeeded(),await s.click({force:!0}),await t.waitForTimeout(this.jitteredDelayMs(300,500))}catch{}return!0}async extractVideos(t){let n=$i;return t.evaluate(a=>{let i=[],s=Array.from(document.querySelectorAll(a.container));for(let o of s){let U=(o.querySelector(a.sectionHeading)?.textContent?.trim()??"").toLowerCase().includes("short")?"short_video":"video",u=Array.from(o.querySelectorAll(a.item));for(let r of u){let S=r.href;if(!S||!S.includes("youtube")&&!S.includes("youtu.be"))continue;let c=r.textContent?.trim()??"",h=c.indexOf("YouTube");if(h===-1)continue;let m=c.slice(0,h).trim(),f=c.slice(h+7).replace(/^[·\s·]+/,"").match(/^([^·\n]+)/),C=f?f[1].trim():"";m&&i.push({type:U,title:m,channel:C,platform:"YouTube",duration:"",url:S})}}return i},n)}async extractForums(t){let n=Qi;return t.evaluate(a=>{let i=[],o=Array.from(document.querySelectorAll(a.section)).find(l=>l.textContent?.includes("Discussions"));if(!o)return i;let d=Array.from(o.querySelectorAll(a.item));for(let l of d){let U=l.href;if(!U)continue;let u=l.querySelector(a.title),r=l.querySelector(a.source),S=u?.textContent?.trim()??"",c=r?.textContent?.trim()??"";S&&i.push({title:S,source:c,url:U})}return i},n)}async extractShortVideos(t,n){try{await t.goto(n,{waitUntil:"domcontentloaded"}),await t.waitForTimeout(1500)}catch{return[]}await this.throwIfCaptcha(t,"Google short video search");let a={item:fn.item,platforms:[...fn.platforms]};return(await t.evaluate(s=>{let o=new Set,d=[],l=Array.from(document.querySelectorAll(s.item)),U=["youtube.com","youtu.be","tiktok.com","instagram.com","facebook.com","fb.watch"],u=new Map;for(let r of l){let S=r.href;if(!S||!U.some(h=>S.includes(h)))continue;let c=r.textContent?.trim()??"";u.has(S)||u.set(S,[]),u.get(S).push(c)}for(let[r,S]of u.entries()){if(o.has(r))continue;o.add(r);let c=S.find(C=>/^\d+:\d+$/.test(C))??"",h=S.find(C=>!/^\d+:\d+$/.test(C)&&C.length>5)??"";if(!h)continue;let m=h,k="",f="";for(let C of s.platforms){let A=-1,R=0;for(;;){let x=h.indexOf(C,R);if(x===-1)break;A=x,R=x+1}if(A===-1)continue;let F=h.slice(A+C.length);if(!(/^[\s·]/.test(F)||F.trim()===""))continue;m=h.slice(0,A).trim(),k=C;let M=F.replace(/^[\s·]+/,""),P=M.indexOf("\xB7");f=(P===-1?M:M.slice(0,P)).trim();break}m&&d.push({title:m,channel:f,platform:k,duration:c,url:r})}return d},a)).map(s=>({type:"short_video",...s}))}async extractWhatPeopleSaying(t){let n=Zi;return t.evaluate(a=>{let i=Array.from(document.querySelectorAll(a.sectionTag)).find(s=>s.textContent?.includes(a.sectionHeadingText))??document.querySelector(".yG4QQe.TBC9ub.NbhJ1c");return i?Array.from(i.querySelectorAll(a.card)).map(s=>{let d=s.querySelector(a.cardLink)?.href??"",l=s.querySelector(a.titleH1)?.textContent?.trim(),U=s.querySelector(a.titleDiv)?.textContent?.trim(),u=l??U??"",r=s.querySelector(a.source)?.textContent?.trim()??"",c=s.querySelector(a.platformBadge)?.textContent?.trim()??"",h=s.querySelector(a.ytChannel)?.textContent?.trim()??"",m=s.querySelector(a.ytDate)?.textContent?.trim()??"",k=s.querySelector(a.authorNote)?.textContent?.trim()??null,f=s.querySelector(a.popularCommentLabel),C=null;if(f){let I=f.nextSibling;for(;I;){let L=I.textContent?.trim();if(L){C=L;break}I=I.nextSibling}}let A=Array.from(s.querySelectorAll("span")),R=A.find(I=>/^\d+:\d+$/.test(I.textContent?.trim()??""))?.textContent?.trim()??null,E=A.map(I=>I.textContent?.trim()??"").filter(I=>/\d/.test(I)&&(I.includes("comment")||I.includes("reaction")||I.includes("view")||I.includes("like")||I.includes("share")))[0]??"",M=A.map(I=>I.textContent?.trim()??"").filter(I=>/\d+ (day|week|month|year|hour)s? ago|Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec/.test(I)),P=m||(M[0]??""),x=c||(h?"YouTube":""),y=h||r,N="unknown",p=x.toLowerCase(),v=y.toLowerCase(),_=r.toLowerCase();return p.includes("reddit")||v.startsWith("r/")?N="reddit":p.includes("facebook")||_.includes("facebook")?N="facebook":p.includes("instagram")||_.includes("instagram")?N="instagram":p.includes("tiktok")||_.includes("tiktok")?N="tiktok":p.includes("youtube")||h?N="youtube":N="news",{type:N,title:u,url:d,source:y,platform:x,popularComment:C,engagement:E,date:P,duration:R,authorNote:k}}):[]},n)}async extractOrganicResults(t){let n=ns,a=await t.evaluate(s=>{let o=[],d=0;return document.querySelectorAll(s.result).forEach(l=>{let U=l.querySelector(s.title);if(!U)return;let u=U.textContent?.trim()??"",S=U.closest("a")?.href??"";if(!u||!S)return;d++;let c=l.querySelector(s.cite)?.textContent?.trim()??null,h=l.querySelector(s.snippet)?.textContent?.trim()??null,m=!!l.querySelector(s.redditCite),k=l.querySelector(s.ratingWrap),f=k?{value:k.querySelector(s.ratingValue)?.textContent?.trim()??"",count:k.querySelector(s.reviewCount)?.textContent?.trim()??""}:null,C="";try{C=new URL(S).hostname.replace(/^www\./,"")}catch{C=l.querySelector(s.siteName)?.textContent?.trim()??""}o.push({position:d,title:u,url:S,domain:C,cite:c,snippet:h,isRedditStyle:m,inlineRating:f})}),o},n),i=typeof t.url=="function"?t.url():"https://www.google.com/";return a.flatMap(s=>{let o=Wn(s.url,i);if(!o)return[];let d=s.domain;try{d=new URL(o.resolvedUrl??o.rawUrl).hostname.replace(/^www\./,"")}catch{}return[{...s,...o,domain:d}]})}async extractLocalPack(t){let n=as;return t.evaluate(a=>{let i=[],s=null;return document.querySelectorAll('[role="heading"]').forEach(o=>{!s&&o.textContent?.includes(a.headingText)&&(s=o.closest("[data-hveid]"))}),s&&s.querySelectorAll(a.card).forEach((o,d)=>{let l=o.querySelector(a.name)?.textContent?.trim()??"";if(!l)return;let U=o.querySelector(a.ratingValue)?.textContent?.trim()??null,u=o.querySelector(a.reviewCount)?.textContent?.trim()??null,r=u?u.replace(/[()]/g,"").trim():null,S=o.querySelector("a[data-cid]")?.getAttribute("data-cid")??null;if(!S)for(let f of Array.from(o.querySelectorAll("a[href]"))){let C=f.href.match(/[?&]cid=(\d+)/);if(C){S=C[1];break}let A=f.href.match(/!1s0x[0-9a-f]+:0x([0-9a-f]+)/i);if(A){try{S=BigInt("0x"+A[1]).toString()}catch{}if(S)break}}let c=[];o.querySelectorAll("div, span").forEach(f=>{let C=Array.from(f.childNodes).filter(A=>A.nodeType===3).map(A=>A.textContent?.trim()??"").filter(A=>A.length>1&&A.length<120).join(" ");C&&!c.includes(C)&&c.push(C)});let h=Array.from(o.querySelectorAll("a[href]")),m=h.find(f=>f.href.includes("google.com/maps"))?.href??null,k=h.find(f=>!f.href.includes("google.com")&&f.href.startsWith("http"))?.href??null;i.push({position:d+1,name:l,cid:S,rating:U,reviewCount:r,metadata:c,websiteUrl:k,directionsUrl:m})}),i},n)}async extractEntityIds(t){return t.evaluate(()=>{let n=new Set,a=new Set,i=new Set,s=new Map;function o(l){let U=[".OSrXXb",".dbg0pd",".tzt0oe",'[role="heading"]',"h3"];for(let u of U){let r=l.querySelector(u);if(r?.textContent?.trim())return r.textContent.trim()}return""}document.querySelectorAll('[id^="pv-/g/"]').forEach(l=>{let U=l.getAttribute("id");if(!U)return;let u=U.replace("pv-","");n.add(u);let r=o(l),c=l.querySelector("a[data-cid]")?.getAttribute("data-cid")??null;c&&a.add(c),r&&s.set(u,{name:r,kgId:u,cid:c,gcid:null})}),document.querySelectorAll("[data-mid]").forEach(l=>{let U=l.getAttribute("data-mid");if(U?.startsWith("/g/")&&(n.add(U),!s.has(U))){let u=o(l);u&&s.set(U,{name:u,kgId:U,cid:null,gcid:null})}}),document.querySelectorAll(".w7Dbne").forEach(l=>{let u=l.querySelector("a[data-cid]")?.getAttribute("data-cid")??null;if(!u)return;a.add(u);let r=l.querySelector(".OSrXXb")?.textContent?.trim()??"";if(!r)return;let S=l.querySelector('[id^="pv-/g/"]'),c=S?S.getAttribute("id").replace("pv-",""):null,h=c??`cid:${u}`;if(s.has(h)){let m=s.get(h);m.cid||s.set(h,{...m,cid:u})}else s.set(h,{name:r,kgId:c,cid:u,gcid:null})}),document.querySelectorAll("a[data-cid]").forEach(l=>{let U=l.getAttribute("data-cid");if(!U)return;if(a.add(U),![...s.values()].some(r=>r.cid===U)){let r=l.parentElement,S="";for(let c=0;c<8&&r;c++){let h=r.querySelector('.OSrXXb, .dbg0pd, [role="heading"], h3');if(h?.textContent?.trim()){S=h.textContent.trim();break}r=r.parentElement}S&&s.set(`cid:${U}`,{name:S,kgId:null,cid:U,gcid:null})}});let d=Array.from(document.querySelectorAll("script:not([src])")).map(l=>l.textContent??"").filter(l=>l.length>1e4).join(`
21896
+ `);for(let l of d.matchAll(/\/g\/[a-zA-Z0-9_-]{5,20}/g))n.add(l[0]);for(let l of d.matchAll(/gcid:[a-zA-Z0-9_]+/g))i.add(l[0]);for(let l of d.matchAll(/0x[0-9a-f]+:0x([0-9a-f]+)/gi))try{a.add(BigInt("0x"+l[1]).toString())}catch{}return{entities:[...s.values()],kgIds:[...n],cids:[...a],gcids:[...i]}})}mergeLocalPackIntoEntities(t,n){let a=new Set(t.cids),i=t.entities.map(s=>({...s}));for(let s of n){if(!s.cid)continue;a.add(s.cid);let o=s.name.toLowerCase().trim(),d=i.find(l=>l.name.toLowerCase().trim()===o);d?d.cid||(d.cid=s.cid):i.find(l=>l.cid===s.cid)||i.push({name:s.name,kgId:null,cid:s.cid,gcid:null})}return{...t,entities:i,cids:[...a]}}async extractAISurfaces(t,n){let a=await t.evaluate(Rs,{aio:ts,aim:es,expandWaitMs:1500}),i=typeof t.url=="function"?t.url():"https://www.google.com/",s={surface:a.surface,aiOverview:{...a.aiOverview,citations:this.normalizeAICitations(i,a.aiOverview.citations)},aiMode:{...a.aiMode,citations:this.normalizeAICitations(i,a.aiMode.citations)}};if(!s.aiOverview.detected)return this.completeness.aioShareCaptured=null,s;let o=this.aioShareUrlEarly??await this.captureAIOverviewShareUrl(t,n).catch(()=>null);return this.completeness.aioShareCaptured=o!==null,{...s,aiOverview:{...s.aiOverview,shareUrl:o}}}async captureAIOverviewShareUrl(t,n){if(n?.softDeadlineMs&&Date.now()>=n.softDeadlineMs||!await t.evaluate(()=>{let s=Array.from(document.querySelectorAll('h1,h2,h3,[role="heading"],div')).filter(o=>(o.textContent||"").trim()==="AI Overview");for(let o of s){let d=o;for(let l=0;l<8&&d;l++){let U=d.querySelector('[aria-label="Share"]');if(U)return U.scrollIntoView({block:"center"}),U.click(),!0;d=d.parentElement}}return!1}).catch(()=>!1))return null;let i=null;try{i=await(await t.waitForFunction(()=>{let o=document.createTreeWalker(document.body,NodeFilter.SHOW_TEXT);for(;o.nextNode();){let d=(o.currentNode.textContent||"").match(/https:\/\/share\.google\/aimode\/\S+/);if(d)return d[0]}return!1},void 0,{timeout:4500,polling:250})).jsonValue()}catch{i=null}return await t.keyboard.press("Escape").catch(()=>{}),i}buildTree(t,n){let a=[],i=new Map;for(let s of t){let o={questionId:s.question_id,googleLinkId:s.google_link_id||null,googleEvidenceId:s.google_evidence_id||null,question:s.question,answer:s.answer||null,sourceTitle:s.source_title||null,sourceSite:s.source_site||null,sourceCite:s.source_cite||null,sources:s.sources??[],depth:s.depth,parentQuestion:s.parent_question||null,children:[]};i.set(s.question,o)}for(let s of i.values())s.parentQuestion&&i.has(s.parentQuestion)?i.get(s.parentQuestion).children.push(s):a.push(s);return a}getBrowserDebugSnapshot(){return this.driver.getDebugSnapshot()}buildHarvestDebugSnapshot(t,n,a,i,s){if(t.debug)return{enabled:!0,request:{query:t.query,locationInput:t.location??null,canonicalLocation:n,locationResolved:s?s.matched:null,locationMatchSource:s?s.source:null,locationAmbiguousCandidates:s?s.ambiguousCandidates:null,uule:a,gl:t.gl,hl:t.hl,device:t.device,proxyMode:t.proxyMode,proxyZip:t.proxyZip??null,serpIdentity:t.serpIdentity??null,serpOnly:t.serpOnly,pages:t.pages??1},browser:this.getBrowserDebugSnapshot(),...i?{locationEvidence:i}:{}}}async captureSecondOrganicPage(t,n,a){if(this.throwIfAborted(a),!this.driver.withTemporaryPage)return{organic:[],status:"failed",failureCode:"unsupported_driver"};let i;try{let s=new URL(t.url()),o=U=>{try{let u=new URL(U,s);return s.origin==="https://www.google.com"&&u.origin===s.origin&&!u.username&&!u.password&&u.pathname==="/search"&&u.searchParams.getAll("q").length===1&&u.searchParams.get("q")===n.query&&u.searchParams.getAll("start").length===1&&u.searchParams.get("start")==="10"&&(!u.searchParams.has("num")||u.searchParams.get("num")==="10")&&["gl","hl"].every(r=>!u.searchParams.has(r)||u.searchParams.getAll(r).length===1&&u.searchParams.get(r)===n[r])&&["uule","tbs","udm","tbm","pws"].every(r=>u.searchParams.getAll(r).length<=1&&u.searchParams.get(r)===s.searchParams.get(r))}catch{return!1}},d=await t.evaluate(()=>document.querySelector('a#pnnext, a[rel="next"]')?.getAttribute("href")??null);if(!d)return{organic:[],status:"unavailable",failureCode:"missing_next"};if(!o(d))return{organic:[],status:"unavailable",failureCode:"invalid_next"};let l=await this.driver.withTemporaryPage(async U=>{if(await U.route("**/*",async u=>{let r=u.request();r.isNavigationRequest()&&r.frame()===U.mainFrame()&&!o(r.url())?(i=/^https:\/\/www\.google\.com\/sorry(?:\/|\?)/.test(r.url())?"captcha":"invalid_next",await u.abort("blockedbyclient")):await u.fallback()}),await U.goto(new URL(d,s).href,{waitUntil:"domcontentloaded",timeout:2e4}),this.throwIfAborted(a),await this.throwIfCaptcha(U,"Google SERP page 2"),!o(U.url()))throw i="invalid_next",new Error("Invalid pagination landing");return this.extractOrganicResults(U)},a);return this.throwIfAborted(a),l.length>0?{organic:l.map(U=>({...U,position:U.position+10})),status:"captured"}:{organic:[],status:"unavailable",failureCode:"empty_page"}}catch(s){if(this.throwIfAborted(a),s instanceof ut)throw s;return{organic:[],status:"failed",failureCode:i??(s instanceof nt?"captcha":s instanceof Error&&(s.name==="TimeoutError"||/timed? ?out/i.test(s.message))?"timeout":"navigation_error")}}}async extract(t,n){let a=Date.now();this.completeness={paaWithoutAnswer:0,paaWithoutSource:0,paaAnswersRecovered:0,aioShareCaptured:null},this.linkDiagnostics=this.emptyLinkDiagnostics(),this.aioShareUrlEarly=null,this.collectedRows=[],this.progressSequence=0,this.checkpointMaterial=null,this.clickedQuestions=new Set,this.paaLifecycle=this.emptyPaaLifecycle(t.maxQuestions),this.partialContext={seed:t.query,location:t.location??null,startMs:a,maxQuestions:t.maxQuestions,questionsOnly:t.questionsOnly};let i=t.device==="mobile",s={headless:t.headless,profileDir:t.profileDir,proxy:t.proxy,\u006b\u0065\u0072\u006e\u0065\u006cApiKey:t.\u006b\u0065\u0072\u006e\u0065\u006cApiKey,\u006b\u0065\u0072\u006e\u0065\u006cProxyId:t.\u006b\u0065\u0072\u006e\u0065\u006cProxyId,\u006b\u0065\u0072\u006e\u0065\u006cProfileName:t.\u006b\u0065\u0072\u006e\u0065\u006cProfileName,\u006b\u0065\u0072\u006e\u0065\u006cProfileSaveChanges:t.\u006b\u0065\u0072\u006e\u0065\u006cProfileSaveChanges,\u006b\u0065\u0072\u006e\u0065\u006cStealth:t.\u006b\u0065\u0072\u006e\u0065\u006cStealth,\u006b\u0065\u0072\u006e\u0065\u006cProxyResolution:t.\u006b\u0065\u0072\u006e\u0065\u006cProxyResolution,proxyMode:t.proxyMode,keepDefaultProxy:t.keepDefaultProxy,requireUsEgress:t.requireUsEgress,viewport:i?{width:390,height:844}:{width:1280,height:800},locale:`${t.hl}-${t.gl.toUpperCase()}`,deviceScaleFactor:i?3:1,isMobile:i,hasTouch:i,debug:t.debug,operationDeadlineMs:t.softDeadlineMs},o=0,d=[];try{this.throwIfAborted(n);let l=Date.now();await this.driver.launch(s),this.phaseTimings.providerConnectMs=Date.now()-l;let U=this.driver.getProviderSessionMetadata?.();if(U?.providerSessionId&&this.onProviderSession)try{await this.onProviderSession(U)}catch(V){console.warn(JSON.stringify({event:"paa_provider_session_sink_failed",provider:U.provider,message:V instanceof Error?V.message:String(V)}))}this.throwIfAborted(n);let u=t.location?xa(t.location):null,r=t.location?u?.canonical??t.location:null,S=r?Ps(r):null,c=t,h=t.includeAllSerpFeatures,m=h||t.includeLocalPack,k=h||t.includeForums,f=h||t.includeVideos,C=h||t.includeAiOverview,A=h||t.includeWhatPeopleSaying,R={surface:"web",aiOverview:{detected:!1,text:null,citations:[]},aiMode:{detected:!1,text:null,citations:[]}},F=Date.now(),{hasPaa:E}=await this.driver.navigateToSERP(c.query,S,t.gl,t.hl,{...Re(t.recency)?{tbs:Re(t.recency)}:{},debug:t.debug,purpose:t.serpOnly?"serp":"paa",...t.softDeadlineMs?{deadlineMs:t.softDeadlineMs}:{}});this.phaseTimings.navigationMs=Date.now()-F,this.throwIfAborted(n);let M=this.driver.getPage();if(await this.throwIfCaptcha(M,"Google SERP"),E||(this.paaLifecycle={...this.paaLifecycle,discoveryStatus:"no_paa_observed",materialStatus:"complete",completionReason:"A readable Google SERP was observed without a People Also Ask block."}),t.questionsOnly){let V=Date.now(),ot=E?await this.runBFS(M,c,n):[];this.phaseTimings.paaExpansionMs=Date.now()-V,await this.resolveMaterialLinks(M,ot,[],R.aiOverview,R.aiMode);let pt={seed:c.query,totalQuestions:ot.length,maxDepthReached:ot.reduce((Le,xe)=>Math.max(Le,xe.depth),0),durationMs:Date.now()-a,errorCount:o};return this.reporter.onComplete(pt),{seed:c.query,location:t.location??null,extractedAt:new Date().toISOString(),diagnostics:{phaseTimings:this.currentPhaseTimings(a),completionStatus:E?"paa_found":"no_paa",...E?{}:{noPaaObserved:!0},problem:null,paaLifecycle:{...this.paaLifecycle},completeness:{...this.completeness},links:{...this.linkDiagnostics},...t.debug?{debug:this.buildHarvestDebugSnapshot(c,r,S,void 0,u)}:{}},totalQuestions:ot.length,surface:"web",aiOverview:{detected:!1,text:null,citations:[]},aiMode:{detected:!1,text:null,citations:[]},whatPeopleSaying:[],tree:this.buildTree(ot,c.query),flat:ot,videos:[],forums:[],organicResults:[],localPack:[],entityIds:{entities:[],kgIds:[],cids:[],gcids:[]},stats:pt}}if(C&&(this.aioShareUrlEarly=await this.captureAIOverviewShareUrl(M,t).catch(()=>null)),t.serpOnly){let V=Date.now(),[ot,pt,Le]=await Promise.all([this.extractOrganicResults(M),m?this.extractLocalPack(M):Promise.resolve([]),this.extractEntityIds(M)]),xe=this.mergeLocalPackIntoEntities(Le,pt),Ot=C?await this.extractAISurfaces(M,t):R;this.phaseTimings.serpParseMs=Date.now()-V;let nn=t.debug?Qe(r,ot,pt):void 0,ne=ot;if((t.pages??1)>=2){let Oe=new URLSearchParams({q:c.query,gl:t.gl,hl:t.hl,pws:"0",start:"10"});Re(t.recency)&&Oe.set("tbs",Re(t.recency)),S&&Oe.set("uule",S),await this.driver.navigateTo("https://www.google.com/search?"+Oe.toString()),await this.throwIfCaptcha(M,"Google SERP page 2");let sn=await this.extractOrganicResults(M);ne=[...ot,...sn.map(ae=>({...ae,position:ae.position+10}))],t.debug&&(nn=Qe(r,ne,pt))}await this.resolveMaterialLinks(M,[],ne,Ot.aiOverview,Ot.aiMode);let an={seed:c.query,totalQuestions:0,maxDepthReached:0,durationMs:Date.now()-a,errorCount:o};return this.reporter.onComplete(an),{seed:c.query,location:t.location??null,extractedAt:new Date().toISOString(),diagnostics:{phaseTimings:this.currentPhaseTimings(a),completionStatus:"serp_only",problem:null,completeness:{...this.completeness},links:{...this.linkDiagnostics},...t.debug?{debug:this.buildHarvestDebugSnapshot(c,r,S,nn,u)}:{}},totalQuestions:0,surface:Ot.surface,aiOverview:Ot.aiOverview,aiMode:Ot.aiMode,whatPeopleSaying:[],tree:[],flat:[],videos:[],forums:[],organicResults:ne,localPack:pt,entityIds:xe,stats:an}}let[P,x,y,N,p,v]=await Promise.all([f?this.extractVideos(M):Promise.resolve([]),k?this.extractForums(M):Promise.resolve([]),A?this.extractWhatPeopleSaying(M):Promise.resolve([]),this.extractEntityIds(M),this.extractOrganicResults(M),m?this.extractLocalPack(M):Promise.resolve([])]),_=this.mergeLocalPackIntoEntities(N,v),I=t.debug?Qe(r,p,v):void 0;this.reporter.onVideos(P),this.reporter.onForums(x);let L=C?await this.extractAISurfaces(M,t):R,W={requestedPages:(t.pages??1)>=2?2:1,capturedPages:1,page2Status:(t.pages??1)>=2?"not_attempted":"not_requested",page1OrganicCount:p.length,page2OrganicCount:0},O=p;if(this.checkpointMaterial={surface:L.surface,aiOverview:L.aiOverview,aiMode:L.aiMode,whatPeopleSaying:y,videos:P,forums:x,organicResults:O,localPack:v,entityIds:_,pagination:W},await this.emitProgress("serp_captured"),W.requestedPages===2){let V=await this.captureSecondOrganicPage(M,c,n);O=[...p,...V.organic],W={...W,capturedPages:V.status==="captured"?2:1,page2Status:V.status,page2OrganicCount:V.organic.length,...V.failureCode?{failureCode:V.failureCode}:{}},this.checkpointMaterial={...this.checkpointMaterial,organicResults:O,pagination:W},await this.emitProgress("serp_captured"),await this.resolveMaterialLinks(M,[],O,L.aiOverview,L.aiMode),this.checkpointMaterial={...this.checkpointMaterial,organicResults:O},await this.emitProgress("serp_captured")}let z=t.debug?Qe(r,O,v):I;if(!E){await this.resolveMaterialLinks(M,[],O,L.aiOverview,L.aiMode),this.checkpointMaterial={...this.checkpointMaterial,organicResults:O},await this.emitProgress("serp_captured");let V={seed:c.query,totalQuestions:0,maxDepthReached:0,durationMs:Date.now()-a,errorCount:o};return this.reporter.onComplete(V),{seed:c.query,location:t.location??null,extractedAt:new Date().toISOString(),diagnostics:{phaseTimings:this.currentPhaseTimings(a),completionStatus:"no_paa",noPaaObserved:!0,problem:null,pagination:W,paaLifecycle:{...this.paaLifecycle},completeness:{...this.completeness},links:{...this.linkDiagnostics},...t.debug?{debug:this.buildHarvestDebugSnapshot(c,r,S,z,u)}:{}},totalQuestions:0,surface:L.surface,aiOverview:L.aiOverview,aiMode:L.aiMode,whatPeopleSaying:y,tree:[],flat:[],videos:P,forums:x,organicResults:O,localPack:v,entityIds:_,stats:V}}let D=Date.now(),Q=await this.runBFS(M,c,n);this.phaseTimings.paaExpansionMs=Date.now()-D,this.throwIfAborted(n);let ct=new URLSearchParams({q:c.query,gl:t.gl,hl:t.hl,pws:"0",udm:fn.udm});S&&ct.set("uule",S);let ft=[];if(t.includeShortVideos!==!1)try{ft=await this.extractShortVideos(M,"https://www.google.com/search?"+ct.toString())}catch(V){if(!(V instanceof nt))throw V;o++,d.push({code:"short_videos_captcha_skipped",surface:"short_videos",message:V.message,retryable:!0})}this.reporter.onVideos(ft),await this.resolveMaterialLinks(M,Q,O,L.aiOverview,L.aiMode),this.checkpointMaterial={...this.checkpointMaterial,surface:L.surface,aiOverview:L.aiOverview,aiMode:L.aiMode,videos:[...P,...ft],organicResults:O},await this.emitProgress("expansion_finished");let Z=[...P,...ft],J=this.buildTree(Q,c.query),ht={seed:c.query,totalQuestions:Q.length,maxDepthReached:Q.reduce((V,ot)=>Math.max(V,ot.depth),0),durationMs:Date.now()-a,errorCount:o};return this.reporter.onComplete(ht),{seed:c.query,location:t.location??null,extractedAt:new Date().toISOString(),diagnostics:{phaseTimings:this.currentPhaseTimings(a),completionStatus:"paa_found",problem:null,pagination:W,paaLifecycle:{...this.paaLifecycle},completeness:{...this.completeness},links:{...this.linkDiagnostics},...d.length>0?{warnings:d}:{},...t.debug?{debug:this.buildHarvestDebugSnapshot(c,r,S,z,u)}:{}},totalQuestions:Q.length,surface:L.surface,aiOverview:L.aiOverview,aiMode:L.aiMode,whatPeopleSaying:y,tree:J,flat:Q,videos:Z,forums:x,organicResults:O,localPack:v,entityIds:_,stats:ht}}catch(l){throw o++,this.paaLifecycle.discoveryStatus==="in_progress"&&(this.paaLifecycle={...this.paaLifecycle,discoveryStatus:"interrupted",completionReason:"The browser attempt ended before frontier exhaustion could be proven."}),await this.emitProgress("attempt_ending").catch(()=>{}),this.reporter.onError(l instanceof Error?l:new Error(String(l))),l}}};var it=require("fs"),At=Sn(require("path"),1),Zt=Sn(require("papaparse"),1);async function js(e){if((process.env.NODE_ENV==="test"||process.env.VITEST)&&process.env.MCP_SCRAPER_ENABLE_MEMORY_LIBRARY_SINK_IN_TESTS!=="1")return;let t=process.env.MCP_SCRAPER_API_KEY?.trim(),n=(process.env.MCP_SCRAPER_BASE_URL??"https://mcpscraper.dev").replace(/\/$/,"");if(t)try{let a=await fetch(`${n}/memory/mcp-call`,{method:"POST",headers:{"content-type":"application/json","x-api-key":t},body:JSON.stringify({toolName:"libraryIngestTool",args:e})}),i=await a.json().catch(()=>null);(!a.ok||i?.ok===!1)&&console.warn("[memory-library-sink] ingest not accepted:",a.status,i?.error??"")}catch(a){console.warn("[memory-library-sink] ingest failed:",a?.message)}}var Bn=class{async writeJSON(t,n){await it.promises.mkdir(n,{recursive:!0});let i=`${t.seed.toLowerCase().replace(/\W+/g,"-").slice(0,40)}-${Date.now()}.json`,s=At.default.join(n,i);return await it.promises.writeFile(s,JSON.stringify(t,null,2),"utf8"),await js({title:`${t.seed} scrape`,content:JSON.stringify(t),source:`mcp-scraper:${s}`}),s}async writeCSV(t,n){await it.promises.mkdir(n,{recursive:!0});let i=(t[0]?.seed_query??"paa").toLowerCase().replace(/\W+/g,"-").slice(0,40),s=Zt.default.unparse(t.map(({sources:l,...U})=>({...U,sources_json:JSON.stringify(l)})),{header:!0}),o=`${i}-${Date.now()}.csv`,d=At.default.join(n,o);return await it.promises.writeFile(d,s,"utf8"),d}async writeVideoCSV(t,n,a){await it.promises.mkdir(a,{recursive:!0});let i=n.toLowerCase().replace(/\W+/g,"-").slice(0,40),s=Zt.default.unparse(t,{header:!0}),o=`${i}-videos-${Date.now()}.csv`,d=At.default.join(a,o);return await it.promises.writeFile(d,s,"utf8"),d}async writeForumCSV(t,n,a){await it.promises.mkdir(a,{recursive:!0});let i=n.toLowerCase().replace(/\W+/g,"-").slice(0,40),s=Zt.default.unparse(t,{header:!0}),o=`${i}-forums-${Date.now()}.csv`,d=At.default.join(a,o);return await it.promises.writeFile(d,s,"utf8"),d}async writeAIOverviewCSV(t,n,a,i){await it.promises.mkdir(i,{recursive:!0});let s=a.toLowerCase().replace(/\W+/g,"-").slice(0,40),o=t.map((u,r)=>({seed_query:a,response_text:r===0?n??"":"",citation_text:u.text,citation_href:u.href})),d=Zt.default.unparse(o,{header:!0}),l=`${s}-ai-overview-${Date.now()}.csv`,U=At.default.join(i,l);return await it.promises.writeFile(U,d,"utf8"),U}async writeAIModeCSV(t,n,a,i){await it.promises.mkdir(i,{recursive:!0});let s=a.toLowerCase().replace(/\W+/g,"-").slice(0,40),o=t.map((u,r)=>({seed_query:a,response_text:r===0?n??"":"",citation_text:u.text,citation_href:u.href})),d=Zt.default.unparse(o,{header:!0}),l=`${s}-ai-mode-${Date.now()}.csv`,U=At.default.join(i,l);return await it.promises.writeFile(U,d,"utf8"),U}async writeWhatPeopleSayingCSV(t,n,a){await it.promises.mkdir(a,{recursive:!0});let i=n.toLowerCase().replace(/\W+/g,"-").slice(0,40),s=t.map(U=>({seed_query:n,...U})),o=Zt.default.unparse(s,{header:!0}),d=`${i}-what-people-saying-${Date.now()}.csv`,l=At.default.join(a,d);return await it.promises.writeFile(l,o,"utf8"),l}};var Dn=class{onQuestion(t){process.stdout.write(JSON.stringify({event:"question",depth:t.depth,question:t.question})+`
21897
21897
  `)}onDepth(t){process.stdout.write(JSON.stringify({event:"depth",depth:t})+`
21898
21898
  `)}onVideos(t){for(let n of t)process.stdout.write(JSON.stringify({event:"video",type:n.type,platform:n.platform,duration:n.duration,title:n.title,channel:n.channel,url:n.url})+`
21899
21899
  `)}onForums(t){for(let n of t)process.stdout.write(JSON.stringify({event:"forum",title:n.title,source:n.source,url:n.url})+`
21900
21900
  `)}onComplete(t){process.stdout.write(JSON.stringify({event:"complete",...t})+`
21901
21901
  `)}onError(t){process.stderr.write(JSON.stringify({event:"error",type:t.constructor.name,message:t.message})+`
21902
- `)}};var js=require("crypto");var Zd="pr.oxylabs.io",tl=7777,el=10;function zs(){return process.env.OXYLABS_USERNAME?.trim()||void 0}function $s(){return process.env.OXYLABS_PASSWORD?.trim()||void 0}function nl(){return(process.env.OXYLABS_COUNTRY?.trim()||"US").toUpperCase()}function Qs(){return!!(zs()&&$s())}function al(){let e="";for(let t=0;t<10;t++)e+=String((0,js.randomInt)(0,10));return e}function il(e={}){let t=zs();if(!t)return;let n=t.startsWith("customer-")?t:`customer-${t}`,a=(e.country?.trim()||nl()).toUpperCase(),i=e.sticky?`-sessid-${al()}-sesstime-${el}`:"";return`${n}-cc-${a}${i}`}function Ha(e){return e.replace(/pr\.oxylabs\.io(:\d+)?/gi,"backup proxy").replace(/oxylabs/gi,"backup proxy")}function sl(e){return`mcp-backup-custom-${`${Date.now()}-${e??0}-${Math.random().toString(36).slice(2,8)}`}`}async function Zs(e,t={}){let n=il({sticky:t.sticky}),a=$s();if(!n||!a)throw new Error("backup proxy pool is not configured");let i=new H({apiKey:e});try{let s=await i.proxies.create({type:"custom",name:sl(t.attemptIndex),protocol:"http",config:{host:Zd,port:tl,username:n,password:a}});if(!s.id)throw new Error("backup proxy creation did not return a proxy id");return s.id}catch(s){let o=s instanceof Error?s.message:String(s);throw new Error(Ha(o))}}var ol={alabama:"AL",alaska:"AK",arizona:"AZ",arkansas:"AR",california:"CA",colorado:"CO",connecticut:"CT",delaware:"DE",florida:"FL",georgia:"GA",hawaii:"HI",idaho:"ID",illinois:"IL",indiana:"IN",iowa:"IA",kansas:"KS",kentucky:"KY",louisiana:"LA",maine:"ME",maryland:"MD",massachusetts:"MA",michigan:"MI",minnesota:"MN",mississippi:"MS",missouri:"MO",montana:"MT",nebraska:"NE",nevada:"NV","new hampshire":"NH","new jersey":"NJ","new mexico":"NM","new york":"NY","north carolina":"NC","north dakota":"ND",ohio:"OH",oklahoma:"OK",oregon:"OR",pennsylvania:"PA","rhode island":"RI","south carolina":"SC","south dakota":"SD",tennessee:"TN",texas:"TX",utah:"UT",vermont:"VT",virginia:"VA",washington:"WA","west virginia":"WV",wisconsin:"WI",wyoming:"WY"},rl={"atlanta|GA":"30303","austin|TX":"78701","baltimore|MD":"21201","boston|MA":"02108","boulder|CO":"80302","charlotte|NC":"28202","chicago|IL":"60601","colorado_springs|CO":"80903","columbus|OH":"43215","dallas|TX":"75201","denver|CO":"80202","detroit|MI":"48226","fort_collins|CO":"80524","fort_worth|TX":"76102","houston|TX":"77002","indianapolis|IN":"46204","jacksonville|FL":"32202","las_vegas|NV":"89101","los_angeles|CA":"90012","louisville|KY":"40202","loveland|CO":"80537","memphis|TN":"38103","miami|FL":"33131","minneapolis|MN":"55401","nashville|TN":"37203","new_york|NY":"10001","orlando|FL":"32801","philadelphia|PA":"19103","phoenix|AZ":"85004","portland|OR":"97205","raleigh|NC":"27601","richmond|VA":"23219","sacramento|CA":"95814","salt_lake_city|UT":"84101","san_antonio|TX":"78205","san_diego|CA":"92101","san_francisco|CA":"94103","san_jose|CA":"95113","seattle|WA":"98101"};function dl(e){return e?e.slice(-6):null}function st(e,t,n,a,i,s=!1){return{\u006b\u0065\u0072\u006e\u0065\u006cProxyId:n,...s&&n?{disposableProxyId:n}:{},resolution:{source:e,proxyType:e==="disabled"?"none":e==="configured_fallback"?"configured":e==="isp_created"?"isp":e==="backup_custom"?"custom":e==="location_created"||e==="location_reused"?"residential":"unknown",proxyMode:t,proxyIdPresent:!!n,proxyIdSuffix:dl(n),target:a,error:i,disposable:s}}}function ll(e){return e.trim().toLowerCase().replace(/\s+/g," ")}function Sl(e){return e.trim().toLowerCase().replace(/\./g,"").replace(/\s+/g," ")}function Ul(e){if(!e)return!0;let t=Sl(e);return t==="united states"||t==="united states of america"||t==="usa"||t==="us"}function to(e){let t=e.trim();return/^[A-Za-z]{2}$/.test(t)?t.toUpperCase():ol[ll(t)]??null}function ul(e){let n=e.normalize("NFKD").replace(/[^\x00-\x7F]/g,"").toLowerCase().split(/[^a-z0-9]+/).filter(Boolean),a=n.join("_"),i=n.join("");return Array.from(new Set([a,i].filter(Boolean)))}function xt(e,t,n){return n?`mcp-serp-residential-${e.toLowerCase()}-${t.toLowerCase()}-${n}`:`mcp-serp-residential-${e.toLowerCase()}-${t.toLowerCase()}`}function Dn(e,t){let n=`${Date.now()}-${t??0}-${Math.random().toString(36).slice(2,8)}`;return`${e}-fresh-${n}`}function cl(e){return`mcp-serp-residential-us-zip-${e}`}function hl(e,t){if(!e||t.toLowerCase()!=="us")return null;let n=Ps(e),a=n.split(",").map(U=>U.trim()).filter(Boolean);if(a.length>1&&Ul(a[a.length-1])&&(a=a.slice(0,-1)),a.length===1){let U=to(a[0]);return U?{canonicalLocation:n,level:"state",country:"US",state:U,city:"",cityCandidates:[],proxyName:xt("US",U),config:{country:"US",state:U}}:null}let[i="",s=""]=a;if(!i||!s)return null;let o=to(s);if(!o)return null;let d=ul(i),l=d[0];return l?{canonicalLocation:n,level:"city",country:"US",state:o,city:l,cityCandidates:d,proxyName:xt("US",o,l),config:{country:"US",state:o,city:l}}:null}function gl(e){return`${e.city}|${e.state}`}function eo(e,t){return t&&/^\d{5}$/.test(t)?t:rl[gl(e)]??null}function no(e,t){return{...e,level:"zip",zip:t,proxyName:cl(t),config:{country:e.country,state:e.state,zip:t}}}function Ba(e,t){return{...e,proxyName:t}}function Fa(e,t,n){return t.level==="zip"?e?.country?.toUpperCase()===t.country&&e?.zip===t.zip:e?.country?.toUpperCase()===t.country&&e?.state?.toUpperCase()===t.state&&(n?e?.city===n:!e?.city)}function pl(e,t){return e.find(n=>n.type==="residential"&&n.status!=="unavailable"&&!!n.id&&(n.name===t.proxyName||Fa(n.config,t,t.level==="city"?t.city:void 0)))??null}function wl(e,t){for(let n of t.cityCandidates){let a=xt(t.country,t.state,n),i=e.find(s=>s.type==="residential"&&s.status!=="unavailable"&&!!s.id&&(s.name===a||Fa(s.config,t,n)));if(i)return i}return null}function Fn(e){return{...e,level:"state",proxyName:xt(e.country,e.state),config:{country:e.country,state:e.state}}}function ml(e,t){let n=xt(t.country,t.state);return e.find(a=>a.type==="residential"&&a.status!=="unavailable"&&!!a.id&&(a.name===n||Fa(a.config,t)))??null}function yl(e,t){return Fn(e)}function Lt(e){return e instanceof Error?e.message:String(e)}function Cl(e,t,n){let a=[],i=eo(e,t);if(i){let o=no(e,i);a.push(Ba(o,Dn(o.proxyName,n)))}for(let o of e.cityCandidates){let d={...e,level:"city",city:o,proxyName:xt(e.country,e.state,o),config:{country:e.country,state:e.state,city:o}};a.push(Ba(d,Dn(d.proxyName,n)))}let s=Fn(e);return a.push(Ba(s,Dn(s.proxyName,n))),a}async function Da(e,t,n){if(e.backupOnExhaustion&&n.length>0&&e.\u006b\u0065\u0072\u006e\u0065\u006cApiKey&&Qs())try{let a=await Zs(e.\u006b\u0065\u0072\u006e\u0065\u006cApiKey,{attemptIndex:e.attemptIndex});return st("backup_custom",e.proxyMode,a,t,null,!0)}catch(a){n.push(`backup: ${Ha(Lt(a))}`)}return st("configured_fallback",e.proxyMode,e.configured\u004b\u0065\u0072\u006e\u0065\u006cProxyId,t,n.join(" | "))}async function kl(e,t,n){let a=[];for(let i of Cl(n,t.proxyZip,t.attemptIndex))try{let s=await e.proxies.create({type:"residential",name:i.proxyName,config:i.level==="zip"?{country:i.country,zip:i.zip}:i.config});if(s.id)return st("location_created",t.proxyMode,s.id,i,null,!0);a.push(`${i.proxyName}: \u004b\u0065\u0072\u006e\u0065\u006c did not return a proxy id`)}catch(s){a.push(`${i.proxyName}: ${Lt(s)}`)}if(process.env.\u004b\u0045\u0052\u004e\u0045\u004c_PROXY_TYPE!=="residential"){let i=Fn(n);try{let s=await e.proxies.create({type:"isp",name:Dn(`mcp-serp-isp-${i.country.toLowerCase()}-${(i.state??"").toLowerCase()}`,t.attemptIndex),config:{country:i.country,...i.state?{state:i.state}:{}}});if(s.id)return st("isp_created",t.proxyMode,s.id,i,null,!0);a.push("isp: \u004b\u0065\u0072\u006e\u0065\u006c did not return a proxy id")}catch(s){a.push(`isp: ${Lt(s)}`)}}return Da(t,n,a)}async function ao(e,t){if(!e||!t)return;await new H({apiKey:e}).proxies.delete(t)}async function io(e){if(e.proxyMode==="none")return st("disabled",e.proxyMode,void 0,null,null);if(e.proxyMode==="configured")return st("configured_fallback",e.proxyMode,e.configured\u004b\u0065\u0072\u006e\u0065\u006cProxyId,null,null);let t=hl(e.location,e.gl);if(!t||!e.\u006b\u0065\u0072\u006e\u0065\u006cApiKey)return st("configured_fallback",e.proxyMode,e.configured\u004b\u0065\u0072\u006e\u0065\u006cProxyId,t,t?null:"location could not be normalized to a US city/state proxy target");let n=new H({apiKey:e.\u006b\u0065\u0072\u006e\u0065\u006cApiKey});try{let a=e.attemptIndex??0;if(e.fresh)return await kl(n,e,t);if(a>=1){let u=yl(t,a),r=[];try{let S=await n.proxies.create({type:"residential",name:u.proxyName,config:u.config});if(S.id)return st("location_created",e.proxyMode,S.id,u,null);r.push(`${u.state}: \u004b\u0065\u0072\u006e\u0065\u006c did not return a proxy id`)}catch(S){r.push(`${u.state}: ${Lt(S)}`)}return Da(e,u,r)}let i=[];for await(let u of await n.proxies.list())i.push(u);let s=eo(t,e.proxyZip),o=[];if(s){let u=no(t,s),r=pl(i,u);if(r?.id)return st("location_reused",e.proxyMode,r.id,u,null);try{let S=await n.proxies.create({type:"residential",name:u.proxyName,config:{country:u.country,zip:s}});if(S.id)return st("location_created",e.proxyMode,S.id,u,null);o.push(`${s}: \u004b\u0065\u0072\u006e\u0065\u006c did not return a proxy id`)}catch(S){o.push(`${s}: ${Lt(S)}`)}}let d=wl(i,t);if(d?.id)return st("location_reused",e.proxyMode,d.id,t,o.join(" | ")||null);for(let u of t.cityCandidates)try{let r=await n.proxies.create({type:"residential",name:xt(t.country,t.state,u),config:{country:t.country,state:t.state,city:u}});if(r.id)return st("location_created",e.proxyMode,r.id,{...t,level:"city",city:u,proxyName:xt(t.country,t.state,u),config:{country:t.country,state:t.state,city:u}},null);o.push(`${u}: \u004b\u0065\u0072\u006e\u0065\u006c did not return a proxy id`)}catch(r){o.push(`${u}: ${Lt(r)}`)}let l=Fn(t),U=ml(i,l);if(U?.id)return st("location_reused",e.proxyMode,U.id,l,o.join(" | "));try{let u=await n.proxies.create({type:"residential",name:l.proxyName,config:l.config});if(u.id)return st("location_created",e.proxyMode,u.id,l,o.join(" | "));o.push(`${l.state}: \u004b\u0065\u0072\u006e\u0065\u006c did not return a proxy id`)}catch(u){o.push(`${l.state}: ${Lt(u)}`)}return Da(e,t,o)}catch(a){return st("configured_fallback",e.proxyMode,e.configured\u004b\u0065\u0072\u006e\u0065\u006cProxyId,t,Lt(a))}}var Va=2,Tl=2,Ml=3e3;function Ya(e){return e.reason instanceof DOMException&&e.reason.name==="TimeoutError"?e.reason:new ut}function fl(e){if(!e||typeof e!="object")return;let t=e.signal;if(t instanceof AbortSignal)return t}function Nl(e){if(!e||typeof e!="object")return;let t=e.onAttemptEvent;return typeof t=="function"?t:void 0}function vl(e){if(!e||typeof e!="object")return;let t=e.onPaaProgress;return typeof t=="function"?t:void 0}async function Ie(e,t){if(e)try{await e(t)}catch(n){console.warn(JSON.stringify({event:"harvest_attempt_log_failed",attempt_number:t.attemptNumber,message:n instanceof Error?n.message:String(n)}))}}function Ka(e){if(e instanceof nt)return"captcha";if(e instanceof qe)return"location_mismatch";if(e instanceof ut)return"request_aborted";if(e instanceof jt)return"browser_session_interrupted";if(e instanceof DOMException&&(e.name==="TimeoutError"||e.name==="AbortError"))return"timeout";let t=e instanceof Error?e.message:String(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(t)?"browser_session_interrupted":Il(t)?"proxy_tunnel_failed":Al(t)?"proxy_unavailable":/timeout|timed out|Timeout \d+ms exceeded|deadline/i.test(t)?"timeout":"error"}function bl(e){return e.diagnostics.degradedResult?"degraded_result":e.diagnostics?.completionStatus??(e.totalQuestions>0?"paa_found":"no_paa")}function _l(e){return e.flat.length>0||e.organicResults.length>0||e.localPack.length>0||e.videos.length>0||e.forums.length>0||e.whatPeopleSaying.length>0||e.aiOverview.detected||e.aiMode.detected}function so(e,t,n){let a=_l(e),i=(e.diagnostics.warnings?.length??0)>0,s=new Set(e.flat.map(m=>m.question.toLowerCase().replace(/[^\w\s]/g,"").replace(/\s+/g," ").trim())).size,o=n?0:e.flat.filter(m=>!m.answer?.trim()).length,d=n?0:e.flat.filter(m=>!m.source_cite?.trim()).length,l=e.diagnostics.completionStatus==="no_paa"&&(e.diagnostics.noPaaObserved===!0||a),U=e.diagnostics.completionStatus==="paa_found",u=e.diagnostics.paaLifecycle?.discoveryStatus,r=u==="interrupted"||u==="recovery_exhausted",S=U&&(s<t&&u!=="frontier_exhausted"||o>0||d>0||!!e.diagnostics.interruption||r),c=l?"complete":U&&s>0?S||i?"partial":"complete":a?i?"partial":"complete":"degraded",h=c==="degraded";return e.diagnostics={...e.diagnostics,resultQuality:c,degradedResult:h,degradationReasons:h?["empty_primary_serp"]:[],retryRecommended:h||c==="partial"&&(S||e.diagnostics.warnings?.some(m=>m.retryable)===!0),completeness:{paaWithoutAnswer:o,paaWithoutSource:d,paaAnswersRecovered:e.diagnostics.completeness?.paaAnswersRecovered??0,aioShareCaptured:e.diagnostics.completeness?.aioShareCaptured??null,paaRequested:t,paaReturned:e.flat.length,paaUnique:s},...e.diagnostics.paaLifecycle?{paaLifecycle:{...e.diagnostics.paaLifecycle,questionsFound:s,targetStatus:s>=t?"met":"not_met",materialStatus:o>0||d>0?"partial":"complete"}}:{}},e}function oo(e,t,n){let a=e.diagnostics.paaLifecycle;if(!a)return e;let i={...a.retryBreakdown,browserRetries:t,providerFallbacks:n};return{...e,diagnostics:{...e.diagnostics,paaLifecycle:{...a,retryBreakdown:i,automaticRetries:i.controlRetries+i.browserRetries+i.runtimeRecoveries}}}}function El(e){return(e.diagnostics.degradationReasons??[]).includes("empty_primary_serp")?"No usable primary SERP surface was captured; the result may be incomplete.":"The harvest result was marked degraded."}function Mt(e){return e instanceof Error?e.message:String(e)}function Pl(e){return e==="location"?Tl:Va}function Il(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 Al(e){return/proxy unavailable|proxy_unavailable|connection_test_failed|did not return a proxy id|configured fallback/i.test(e)}function ro(e){return e==="captcha"||e==="proxy_tunnel_failed"||e==="proxy_unavailable"}function lo(e){return e==="browser_session_interrupted"}function Ll(e){let t=e.diagnostics.debug?.locationEvidence,n=t?.expected?.canonicalLocation??e.location??"requested location",a=t?.candidates.slice(0,3).map(i=>`${i.city}, ${i.regionCode}`).join("; ");return a?`Google returned results for ${a}, not ${n}`:`Google returned results for a different location than ${n}`}function xl(e,t){return t==="location"&&e.diagnostics.debug?.locationEvidence?.status==="mismatch"}function So(e,t){if(t||!e.diagnostics.debug)return e;let n={...e.diagnostics};return delete n.debug,{...e,diagnostics:n}}async function Uo(e,t){let n=t==null?3e3:t-Date.now();if(n<=0)return e;try{return await Vs(e,{totalBudgetMs:Math.min(3e3,n)})}catch(a){return console.warn(JSON.stringify({event:"harvest_goto_resolution_failed",message:Mt(a)})),e}}async function Ga(e,t){if(!(!e||!t))try{await ao(e,t),console.info(JSON.stringify({event:"\u006b\u0065\u0072\u006e\u0065\u006c_proxy_deleted",proxy_id_suffix:t.slice(-6)}))}catch(n){console.warn(JSON.stringify({event:"\u006b\u0065\u0072\u006e\u0065\u006c_proxy_delete_failed",proxy_id_suffix:t.slice(-6),message:Mt(n)}))}}async function uo(e,t,n=!1,a=!1,i,s){let o=fs(e,{forceManaged:n,force\u004b\u0065\u0072\u006e\u0065\u006c:a}),d=new Bn,l=new Wn(o,d,i,s);if(t?.aborted)return{result:null,error:Ya(t),cleanup:await o.close(),debug:null};let U,u=t?new Promise((m,k)=>{U=()=>k(Ya(t)),t.addEventListener("abort",U,{once:!0})}):null,r=null,S=null,c,h=null;try{let m=l.extract(e,t);u&&m.catch(()=>{}),r=await(u?Promise.race([m,u]):m)}catch(m){S=m}finally{t&&U&&t.removeEventListener("abort",U),h=r?.diagnostics.debug??(e.debug?{enabled:!0,request:{query:e.query,locationInput:e.location??null,canonicalLocation:null,locationResolved:null,locationMatchSource:null,locationAmbiguousCandidates:null,uule:null,gl:e.gl,hl:e.hl,device:e.device,proxyMode:e.proxyMode,proxyZip:e.proxyZip??null,serpIdentity:e.serpIdentity??null,serpOnly:e.serpOnly,pages:e.pages??1},browser:o.getDebugSnapshot()}:null),c=await o.close()}if(S){let m=Ka(S),k=m==="timeout"||m==="request_aborted"||m==="browser_session_interrupted"?m:null,f=null;if(k)try{f=typeof l.getPartialResult=="function"?l.getPartialResult():null}catch{f=null}return f&&k?(f.diagnostics={...f.diagnostics,interruption:{code:k,reasonSource:"client_runtime",message:Mt(S)}},{result:f,error:S,cleanup:c,debug:h,salvagedFrom:S}):{result:null,error:S,cleanup:c,debug:h}}return{result:r,error:null,cleanup:c,debug:h}}async function co(e){let t=typeof e=="object"&&e!==null?e:{},n=fl(e),a=Nl(e),i=vl(e),o=t.forceManagedSerp===!0||xn()&&!t.serpIdentity,d=t.proxyMode,l=d==="location"||d==="none"||d==="configured"?d:jn,U=typeof t.\u006b\u0065\u0072\u006e\u0065\u006cApiKey=="string"?t.\u006b\u0065\u0072\u006e\u0065\u006cApiKey.trim():Si(),u=typeof t.\u006b\u0065\u0072\u006e\u0065\u006cProxyId=="string"?t.\u006b\u0065\u0072\u006e\u0065\u006cProxyId.trim():void 0,r={\u006b\u0065\u0072\u006e\u0065\u006cApiKey:U,proxyMode:l,configured\u004b\u0065\u0072\u006e\u0065\u006cProxyId:u,location:typeof t.location=="string"?t.location:void 0,proxyZip:typeof t.proxyZip=="string"?t.proxyZip:void 0,gl:typeof t.gl=="string"?t.gl:"us"},S=typeof t.debug=="boolean"?t.debug:!1,c=l==="location"&&!!r.location,h=t.maxAttempts,m=typeof h=="number"&&Number.isInteger(h)&&h>=1?Math.min(Va,h):Pl(l),k=Math.min(Va,m),f=typeof t.softDeadlineMs=="number"?t.softDeadlineMs:null,C=new Hn,A=null,R=l,F=0,E=()=>f==null||f-Date.now()>=Ml;for(let M=0;M<k;M++){let P=M+1,x=Date.now();try{if(n?.aborted)throw Ya(n);let y=await io({...r,proxyMode:R,attemptIndex:M,fresh:R==="location"});if(!y)break;let N={...t,\u006b\u0065\u0072\u006e\u0065\u006cApiKey:U,\u006b\u0065\u0072\u006e\u0065\u006cProxyId:y.\u006b\u0065\u0072\u006e\u0065\u006cProxyId,\u006b\u0065\u0072\u006e\u0065\u006cProxyResolution:y.resolution,proxyMode:R,keepDefaultProxy:M>0,debug:S||c};R==="none"&&(N.\u006b\u0065\u0072\u006e\u0065\u006cProxyId=void 0);let p=di.parse(N);await Ie(a,{type:"started",attemptNumber:P,maxAttempts:k,query:p.query,location:p.location??null,maxQuestions:p.maxQuestions,startedAt:new Date(x).toISOString()}),console.info(JSON.stringify({event:"harvest_attempt_started",attempt_number:P,max_attempts:k,query:p.query,location:p.location??null,max_questions:p.maxQuestions}));let v=zt(),_=Z=>Ie(a,{type:"provider_session_started",attemptNumber:P,maxAttempts:k,provider:Z.provider,providerSessionId:Z.providerSessionId,observedAt:new Date().toISOString()}),I=v?.harvestBrowserMode==="headless-test"&&process.env[ds]==="true",L=o&&M>0&&!!U,W=v?await Ss({...v,forceHeadless:I,forceHeadful:!I},()=>uo(p,n,o&&!L,L,i,_)):await uo(p,n,o&&!L,L,i,_);if(W.error){let Z=W.error,J=Ka(Z);(J==="captcha"||J==="proxy_tunnel_failed")&&R==="location"&&(F++,F>=2&&(R="none"));let ht=!W.result&&M<k-1&&E()&&(J==="captcha"||lo(J)||l==="location"&&ro(J));if(J==="captcha"?console.warn(JSON.stringify({event:"harvest_attempt_captcha",attempt_number:P,max_attempts:k,message:Mt(Z),will_retry:ht})):ht&&console.warn(JSON.stringify({event:"harvest_attempt_proxy_retry",attempt_number:P,max_attempts:k,outcome:J,message:Mt(Z),will_retry:ht})),await Ie(a,{type:"finished",attemptNumber:P,maxAttempts:k,outcome:J,\u006b\u0065\u0072\u006e\u0065\u006cSessionId:W.cleanup.\u006b\u0065\u0072\u006e\u0065\u006cSessionId,questionCount:W.result?.totalQuestions??0,durationMs:Date.now()-x,error:Mt(Z),willRetry:ht,cleanup:W.cleanup,debug:W.debug,completedAt:new Date().toISOString()}),await Ga(U,y.disposableProxyId),A=Z,ht)continue;if(W.result){let V=await Uo(W.result,f);return oo(so(So(V,S),p.maxQuestions,p.questionsOnly),M,L?1:0)}break}let O=W.result;if(!O)throw new Error("Harvest attempt completed without a result");if(xl(O,l)){let Z=new qe(Ll(O)),J=M<k-1&&E();if(console.warn(JSON.stringify({event:"harvest_attempt_location_mismatch",attempt_number:P,max_attempts:k,message:Z.message,will_retry:J})),await Ie(a,{type:"finished",attemptNumber:P,maxAttempts:k,outcome:"location_mismatch",\u006b\u0065\u0072\u006e\u0065\u006cSessionId:W.cleanup.\u006b\u0065\u0072\u006e\u0065\u006cSessionId,questionCount:O.totalQuestions,durationMs:Date.now()-x,error:Z.message,willRetry:J,cleanup:W.cleanup,debug:W.debug,completedAt:new Date().toISOString()}),await Ga(U,y.disposableProxyId),A=Z,J)continue;break}let z=await Uo(O,f),D=oo(so(So(z,S),p.maxQuestions,p.questionsOnly),M,L?1:0),Q=bl(D),ct=Q==="degraded_result"&&M<k-1&&E(),ft=Q==="degraded_result"?El(D):null;if(ct&&console.warn(JSON.stringify({event:"harvest_attempt_degraded_result",attempt_number:P,max_attempts:k,degradation_reasons:D.diagnostics.degradationReasons,will_retry:!0})),await Ie(a,{type:"finished",attemptNumber:P,maxAttempts:k,outcome:Q,\u006b\u0065\u0072\u006e\u0065\u006cSessionId:W.cleanup.\u006b\u0065\u0072\u006e\u0065\u006cSessionId,questionCount:D.totalQuestions,durationMs:Date.now()-x,error:ft,willRetry:ct,cleanup:W.cleanup,debug:W.debug,completedAt:new Date().toISOString()}),await Ga(U,y.disposableProxyId),ct)continue;return(p.format==="json"||p.format==="both")&&await C.writeJSON(D,p.outputDir),(p.format==="csv"||p.format==="both")&&await Promise.all([C.writeCSV(D.flat,p.outputDir),D.videos.length>0?C.writeVideoCSV(D.videos,D.seed,p.outputDir):Promise.resolve(""),D.forums.length>0?C.writeForumCSV(D.forums,D.seed,p.outputDir):Promise.resolve(""),D.aiOverview.detected?C.writeAIOverviewCSV(D.aiOverview.citations,D.aiOverview.text,D.seed,p.outputDir):Promise.resolve(""),D.aiMode.detected?C.writeAIModeCSV(D.aiMode.citations,D.aiMode.text,D.seed,p.outputDir):Promise.resolve(""),D.whatPeopleSaying.length>0?C.writeWhatPeopleSayingCSV(D.whatPeopleSaying,D.seed,p.outputDir):Promise.resolve("")]),D}catch(y){let N=Ka(y);(N==="captcha"||N==="proxy_tunnel_failed")&&R==="location"&&(F++,F>=2&&(R="none"));let p=M<k-1&&E()&&(N==="captcha"||lo(N)||l==="location"&&ro(N));if(N==="captcha"?console.warn(JSON.stringify({event:"harvest_attempt_captcha",attempt_number:P,max_attempts:k,message:Mt(y),will_retry:p})):p&&console.warn(JSON.stringify({event:"harvest_attempt_proxy_retry",attempt_number:P,max_attempts:k,outcome:N,message:Mt(y),will_retry:p})),await Ie(a,{type:"finished",attemptNumber:P,maxAttempts:k,outcome:N,\u006b\u0065\u0072\u006e\u0065\u006cSessionId:null,questionCount:0,durationMs:Date.now()-x,error:Mt(y),willRetry:p,cleanup:{\u006b\u0065\u0072\u006e\u0065\u006cSessionId:null,\u006b\u0065\u0072\u006e\u0065\u006cDeleteStarted:!1,\u006b\u0065\u0072\u006e\u0065\u006cDeleteSucceeded:null,\u006b\u0065\u0072\u006e\u0065\u006cDeleteError:null,browserCloseSucceeded:null,browserCloseError:null},debug:null,completedAt:new Date().toISOString()}),A=y,p)continue;if(N==="captcha")break;throw y}}throw A&&!(A instanceof nt)?A:(console.warn(JSON.stringify({event:"harvest_captcha_exhausted",max_attempts:k,session_kind:U?"\u006b\u0065\u0072\u006e\u0065\u006c":"local"})),new nt(ss(`CAPTCHA on all ${k} fresh sessions. Try again in a few minutes.`)))}var Mo=require("child_process"),dt=require("fs"),ei=require("os"),te=require("path"),ee=require("@fal-ai/client");var Ol="https://api.deepinfra.com/v1/openai/chat/completions",go="https://openrouter.ai/api/v1/chat/completions",Ja="Qwen/Qwen3.6-35B-A3B",Rl=`You are a video prompt engineer for Seedance 2.0 text-to-video AI.
21902
+ `)}};var zs=require("crypto");var el="pr.oxylabs.io",nl=7777,al=10;function $s(){return process.env.OXYLABS_USERNAME?.trim()||void 0}function Qs(){return process.env.OXYLABS_PASSWORD?.trim()||void 0}function il(){return(process.env.OXYLABS_COUNTRY?.trim()||"US").toUpperCase()}function Zs(){return!!($s()&&Qs())}function sl(){let e="";for(let t=0;t<10;t++)e+=String((0,zs.randomInt)(0,10));return e}function ol(e={}){let t=$s();if(!t)return;let n=t.startsWith("customer-")?t:`customer-${t}`,a=(e.country?.trim()||il()).toUpperCase(),i=e.sticky?`-sessid-${sl()}-sesstime-${al}`:"";return`${n}-cc-${a}${i}`}function Ha(e){return e.replace(/pr\.oxylabs\.io(:\d+)?/gi,"backup proxy").replace(/oxylabs/gi,"backup proxy")}function rl(e){return`mcp-backup-custom-${`${Date.now()}-${e??0}-${Math.random().toString(36).slice(2,8)}`}`}async function to(e,t={}){let n=ol({sticky:t.sticky}),a=Qs();if(!n||!a)throw new Error("backup proxy pool is not configured");let i=new H({apiKey:e});try{let s=await i.proxies.create({type:"custom",name:rl(t.attemptIndex),protocol:"http",config:{host:el,port:nl,username:n,password:a}});if(!s.id)throw new Error("backup proxy creation did not return a proxy id");return s.id}catch(s){let o=s instanceof Error?s.message:String(s);throw new Error(Ha(o))}}var dl={alabama:"AL",alaska:"AK",arizona:"AZ",arkansas:"AR",california:"CA",colorado:"CO",connecticut:"CT",delaware:"DE",florida:"FL",georgia:"GA",hawaii:"HI",idaho:"ID",illinois:"IL",indiana:"IN",iowa:"IA",kansas:"KS",kentucky:"KY",louisiana:"LA",maine:"ME",maryland:"MD",massachusetts:"MA",michigan:"MI",minnesota:"MN",mississippi:"MS",missouri:"MO",montana:"MT",nebraska:"NE",nevada:"NV","new hampshire":"NH","new jersey":"NJ","new mexico":"NM","new york":"NY","north carolina":"NC","north dakota":"ND",ohio:"OH",oklahoma:"OK",oregon:"OR",pennsylvania:"PA","rhode island":"RI","south carolina":"SC","south dakota":"SD",tennessee:"TN",texas:"TX",utah:"UT",vermont:"VT",virginia:"VA",washington:"WA","west virginia":"WV",wisconsin:"WI",wyoming:"WY"},ll={"atlanta|GA":"30303","austin|TX":"78701","baltimore|MD":"21201","boston|MA":"02108","boulder|CO":"80302","charlotte|NC":"28202","chicago|IL":"60601","colorado_springs|CO":"80903","columbus|OH":"43215","dallas|TX":"75201","denver|CO":"80202","detroit|MI":"48226","fort_collins|CO":"80524","fort_worth|TX":"76102","houston|TX":"77002","indianapolis|IN":"46204","jacksonville|FL":"32202","las_vegas|NV":"89101","los_angeles|CA":"90012","louisville|KY":"40202","loveland|CO":"80537","memphis|TN":"38103","miami|FL":"33131","minneapolis|MN":"55401","nashville|TN":"37203","new_york|NY":"10001","orlando|FL":"32801","philadelphia|PA":"19103","phoenix|AZ":"85004","portland|OR":"97205","raleigh|NC":"27601","richmond|VA":"23219","sacramento|CA":"95814","salt_lake_city|UT":"84101","san_antonio|TX":"78205","san_diego|CA":"92101","san_francisco|CA":"94103","san_jose|CA":"95113","seattle|WA":"98101"};function Sl(e){return e?e.slice(-6):null}function st(e,t,n,a,i,s=!1){return{\u006b\u0065\u0072\u006e\u0065\u006cProxyId:n,...s&&n?{disposableProxyId:n}:{},resolution:{source:e,proxyType:e==="disabled"?"none":e==="configured_fallback"?"configured":e==="isp_created"?"isp":e==="backup_custom"?"custom":e==="location_created"||e==="location_reused"?"residential":"unknown",proxyMode:t,proxyIdPresent:!!n,proxyIdSuffix:Sl(n),target:a,error:i,disposable:s}}}function Ul(e){return e.trim().toLowerCase().replace(/\s+/g," ")}function ul(e){return e.trim().toLowerCase().replace(/\./g,"").replace(/\s+/g," ")}function cl(e){if(!e)return!0;let t=ul(e);return t==="united states"||t==="united states of america"||t==="usa"||t==="us"}function eo(e){let t=e.trim();return/^[A-Za-z]{2}$/.test(t)?t.toUpperCase():dl[Ul(t)]??null}function hl(e){let n=e.normalize("NFKD").replace(/[^\x00-\x7F]/g,"").toLowerCase().split(/[^a-z0-9]+/).filter(Boolean),a=n.join("_"),i=n.join("");return Array.from(new Set([a,i].filter(Boolean)))}function xt(e,t,n){return n?`mcp-serp-residential-${e.toLowerCase()}-${t.toLowerCase()}-${n}`:`mcp-serp-residential-${e.toLowerCase()}-${t.toLowerCase()}`}function Fn(e,t){let n=`${Date.now()}-${t??0}-${Math.random().toString(36).slice(2,8)}`;return`${e}-fresh-${n}`}function gl(e){return`mcp-serp-residential-us-zip-${e}`}function pl(e,t){if(!e||t.toLowerCase()!=="us")return null;let n=Is(e),a=n.split(",").map(U=>U.trim()).filter(Boolean);if(a.length>1&&cl(a[a.length-1])&&(a=a.slice(0,-1)),a.length===1){let U=eo(a[0]);return U?{canonicalLocation:n,level:"state",country:"US",state:U,city:"",cityCandidates:[],proxyName:xt("US",U),config:{country:"US",state:U}}:null}let[i="",s=""]=a;if(!i||!s)return null;let o=eo(s);if(!o)return null;let d=hl(i),l=d[0];return l?{canonicalLocation:n,level:"city",country:"US",state:o,city:l,cityCandidates:d,proxyName:xt("US",o,l),config:{country:"US",state:o,city:l}}:null}function wl(e){return`${e.city}|${e.state}`}function no(e,t){return t&&/^\d{5}$/.test(t)?t:ll[wl(e)]??null}function ao(e,t){return{...e,level:"zip",zip:t,proxyName:gl(t),config:{country:e.country,state:e.state,zip:t}}}function Ba(e,t){return{...e,proxyName:t}}function Fa(e,t,n){return t.level==="zip"?e?.country?.toUpperCase()===t.country&&e?.zip===t.zip:e?.country?.toUpperCase()===t.country&&e?.state?.toUpperCase()===t.state&&(n?e?.city===n:!e?.city)}function ml(e,t){return e.find(n=>n.type==="residential"&&n.status!=="unavailable"&&!!n.id&&(n.name===t.proxyName||Fa(n.config,t,t.level==="city"?t.city:void 0)))??null}function yl(e,t){for(let n of t.cityCandidates){let a=xt(t.country,t.state,n),i=e.find(s=>s.type==="residential"&&s.status!=="unavailable"&&!!s.id&&(s.name===a||Fa(s.config,t,n)));if(i)return i}return null}function Gn(e){return{...e,level:"state",proxyName:xt(e.country,e.state),config:{country:e.country,state:e.state}}}function Cl(e,t){let n=xt(t.country,t.state);return e.find(a=>a.type==="residential"&&a.status!=="unavailable"&&!!a.id&&(a.name===n||Fa(a.config,t)))??null}function kl(e,t){return Gn(e)}function Lt(e){return e instanceof Error?e.message:String(e)}function Tl(e,t,n){let a=[],i=no(e,t);if(i){let o=ao(e,i);a.push(Ba(o,Fn(o.proxyName,n)))}for(let o of e.cityCandidates){let d={...e,level:"city",city:o,proxyName:xt(e.country,e.state,o),config:{country:e.country,state:e.state,city:o}};a.push(Ba(d,Fn(d.proxyName,n)))}let s=Gn(e);return a.push(Ba(s,Fn(s.proxyName,n))),a}async function Da(e,t,n){if(e.backupOnExhaustion&&n.length>0&&e.\u006b\u0065\u0072\u006e\u0065\u006cApiKey&&Zs())try{let a=await to(e.\u006b\u0065\u0072\u006e\u0065\u006cApiKey,{attemptIndex:e.attemptIndex});return st("backup_custom",e.proxyMode,a,t,null,!0)}catch(a){n.push(`backup: ${Ha(Lt(a))}`)}return st("configured_fallback",e.proxyMode,e.configured\u004b\u0065\u0072\u006e\u0065\u006cProxyId,t,n.join(" | "))}async function Ml(e,t,n){let a=[];for(let i of Tl(n,t.proxyZip,t.attemptIndex))try{let s=await e.proxies.create({type:"residential",name:i.proxyName,config:i.level==="zip"?{country:i.country,zip:i.zip}:i.config});if(s.id)return st("location_created",t.proxyMode,s.id,i,null,!0);a.push(`${i.proxyName}: \u004b\u0065\u0072\u006e\u0065\u006c did not return a proxy id`)}catch(s){a.push(`${i.proxyName}: ${Lt(s)}`)}if(process.env.\u004b\u0045\u0052\u004e\u0045\u004c_PROXY_TYPE!=="residential"){let i=Gn(n);try{let s=await e.proxies.create({type:"isp",name:Fn(`mcp-serp-isp-${i.country.toLowerCase()}-${(i.state??"").toLowerCase()}`,t.attemptIndex),config:{country:i.country,...i.state?{state:i.state}:{}}});if(s.id)return st("isp_created",t.proxyMode,s.id,i,null,!0);a.push("isp: \u004b\u0065\u0072\u006e\u0065\u006c did not return a proxy id")}catch(s){a.push(`isp: ${Lt(s)}`)}}return Da(t,n,a)}async function io(e,t){if(!e||!t)return;await new H({apiKey:e}).proxies.delete(t)}async function so(e){if(e.proxyMode==="none")return st("disabled",e.proxyMode,void 0,null,null);if(e.proxyMode==="configured")return st("configured_fallback",e.proxyMode,e.configured\u004b\u0065\u0072\u006e\u0065\u006cProxyId,null,null);let t=pl(e.location,e.gl);if(!t||!e.\u006b\u0065\u0072\u006e\u0065\u006cApiKey)return st("configured_fallback",e.proxyMode,e.configured\u004b\u0065\u0072\u006e\u0065\u006cProxyId,t,t?null:"location could not be normalized to a US city/state proxy target");let n=new H({apiKey:e.\u006b\u0065\u0072\u006e\u0065\u006cApiKey});try{let a=e.attemptIndex??0;if(e.fresh)return await Ml(n,e,t);if(a>=1){let u=kl(t,a),r=[];try{let S=await n.proxies.create({type:"residential",name:u.proxyName,config:u.config});if(S.id)return st("location_created",e.proxyMode,S.id,u,null);r.push(`${u.state}: \u004b\u0065\u0072\u006e\u0065\u006c did not return a proxy id`)}catch(S){r.push(`${u.state}: ${Lt(S)}`)}return Da(e,u,r)}let i=[];for await(let u of await n.proxies.list())i.push(u);let s=no(t,e.proxyZip),o=[];if(s){let u=ao(t,s),r=ml(i,u);if(r?.id)return st("location_reused",e.proxyMode,r.id,u,null);try{let S=await n.proxies.create({type:"residential",name:u.proxyName,config:{country:u.country,zip:s}});if(S.id)return st("location_created",e.proxyMode,S.id,u,null);o.push(`${s}: \u004b\u0065\u0072\u006e\u0065\u006c did not return a proxy id`)}catch(S){o.push(`${s}: ${Lt(S)}`)}}let d=yl(i,t);if(d?.id)return st("location_reused",e.proxyMode,d.id,t,o.join(" | ")||null);for(let u of t.cityCandidates)try{let r=await n.proxies.create({type:"residential",name:xt(t.country,t.state,u),config:{country:t.country,state:t.state,city:u}});if(r.id)return st("location_created",e.proxyMode,r.id,{...t,level:"city",city:u,proxyName:xt(t.country,t.state,u),config:{country:t.country,state:t.state,city:u}},null);o.push(`${u}: \u004b\u0065\u0072\u006e\u0065\u006c did not return a proxy id`)}catch(r){o.push(`${u}: ${Lt(r)}`)}let l=Gn(t),U=Cl(i,l);if(U?.id)return st("location_reused",e.proxyMode,U.id,l,o.join(" | "));try{let u=await n.proxies.create({type:"residential",name:l.proxyName,config:l.config});if(u.id)return st("location_created",e.proxyMode,u.id,l,o.join(" | "));o.push(`${l.state}: \u004b\u0065\u0072\u006e\u0065\u006c did not return a proxy id`)}catch(u){o.push(`${l.state}: ${Lt(u)}`)}return Da(e,t,o)}catch(a){return st("configured_fallback",e.proxyMode,e.configured\u004b\u0065\u0072\u006e\u0065\u006cProxyId,t,Lt(a))}}var Va=2,fl=2,Nl=3e3;function Ya(e){return e.reason instanceof DOMException&&e.reason.name==="TimeoutError"?e.reason:new ut}function vl(e){if(!e||typeof e!="object")return;let t=e.signal;if(t instanceof AbortSignal)return t}function bl(e){if(!e||typeof e!="object")return;let t=e.onAttemptEvent;return typeof t=="function"?t:void 0}function _l(e){if(!e||typeof e!="object")return;let t=e.onPaaProgress;return typeof t=="function"?t:void 0}async function Ie(e,t){if(e)try{await e(t)}catch(n){console.warn(JSON.stringify({event:"harvest_attempt_log_failed",attempt_number:t.attemptNumber,message:n instanceof Error?n.message:String(n)}))}}function Ka(e){if(e instanceof nt)return"captcha";if(e instanceof qe)return"location_mismatch";if(e instanceof ut)return"request_aborted";if(e instanceof jt)return"browser_session_interrupted";if(e instanceof DOMException&&(e.name==="TimeoutError"||e.name==="AbortError"))return"timeout";let t=e instanceof Error?e.message:String(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(t)?"browser_session_interrupted":Ll(t)?"proxy_tunnel_failed":xl(t)?"proxy_unavailable":/timeout|timed out|Timeout \d+ms exceeded|deadline/i.test(t)?"timeout":"error"}function El(e){return e.diagnostics.degradedResult?"degraded_result":e.diagnostics?.completionStatus??(e.totalQuestions>0?"paa_found":"no_paa")}function Pl(e){return e.flat.length>0||e.organicResults.length>0||e.localPack.length>0||e.videos.length>0||e.forums.length>0||e.whatPeopleSaying.length>0||e.aiOverview.detected||e.aiMode.detected}function oo(e,t,n){let a=Pl(e),i=(e.diagnostics.warnings?.length??0)>0,s=new Set(e.flat.map(m=>m.question.toLowerCase().replace(/[^\w\s]/g,"").replace(/\s+/g," ").trim())).size,o=n?0:e.flat.filter(m=>!m.answer?.trim()).length,d=n?0:e.flat.filter(m=>!m.source_cite?.trim()).length,l=e.diagnostics.completionStatus==="no_paa"&&(e.diagnostics.noPaaObserved===!0||a),U=e.diagnostics.completionStatus==="paa_found",u=e.diagnostics.paaLifecycle?.discoveryStatus,r=u==="interrupted"||u==="recovery_exhausted",S=U&&(s<t&&u!=="frontier_exhausted"||o>0||d>0||!!e.diagnostics.interruption||r),c=l?"complete":U&&s>0?S||i?"partial":"complete":a?i?"partial":"complete":"degraded",h=c==="degraded";return e.diagnostics={...e.diagnostics,resultQuality:c,degradedResult:h,degradationReasons:h?["empty_primary_serp"]:[],retryRecommended:h||c==="partial"&&(S||e.diagnostics.warnings?.some(m=>m.retryable)===!0),completeness:{paaWithoutAnswer:o,paaWithoutSource:d,paaAnswersRecovered:e.diagnostics.completeness?.paaAnswersRecovered??0,aioShareCaptured:e.diagnostics.completeness?.aioShareCaptured??null,paaRequested:t,paaReturned:e.flat.length,paaUnique:s},...e.diagnostics.paaLifecycle?{paaLifecycle:{...e.diagnostics.paaLifecycle,questionsFound:s,targetStatus:s>=t?"met":"not_met",materialStatus:o>0||d>0?"partial":"complete"}}:{}},e}function ro(e,t,n){let a=e.diagnostics.paaLifecycle;if(!a)return e;let i={...a.retryBreakdown,browserRetries:t,providerFallbacks:n};return{...e,diagnostics:{...e.diagnostics,paaLifecycle:{...a,retryBreakdown:i,automaticRetries:i.controlRetries+i.browserRetries+i.runtimeRecoveries}}}}function Il(e){return(e.diagnostics.degradationReasons??[]).includes("empty_primary_serp")?"No usable primary SERP surface was captured; the result may be incomplete.":"The harvest result was marked degraded."}function Mt(e){return e instanceof Error?e.message:String(e)}function Al(e){return e==="location"?fl:Va}function Ll(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 xl(e){return/proxy unavailable|proxy_unavailable|connection_test_failed|did not return a proxy id|configured fallback/i.test(e)}function lo(e){return e==="captcha"||e==="proxy_tunnel_failed"||e==="proxy_unavailable"}function So(e){return e==="browser_session_interrupted"}function Ol(e){let t=e.diagnostics.debug?.locationEvidence,n=t?.expected?.canonicalLocation??e.location??"requested location",a=t?.candidates.slice(0,3).map(i=>`${i.city}, ${i.regionCode}`).join("; ");return a?`Google returned results for ${a}, not ${n}`:`Google returned results for a different location than ${n}`}function Rl(e,t){return t==="location"&&e.diagnostics.debug?.locationEvidence?.status==="mismatch"}function Uo(e,t){if(t||!e.diagnostics.debug)return e;let n={...e.diagnostics};return delete n.debug,{...e,diagnostics:n}}async function uo(e,t){let n=t==null?3e3:t-Date.now();if(n<=0)return e;try{return await Ys(e,{totalBudgetMs:Math.min(3e3,n)})}catch(a){return console.warn(JSON.stringify({event:"harvest_goto_resolution_failed",message:Mt(a)})),e}}async function Ga(e,t){if(!(!e||!t))try{await io(e,t),console.info(JSON.stringify({event:"\u006b\u0065\u0072\u006e\u0065\u006c_proxy_deleted",proxy_id_suffix:t.slice(-6)}))}catch(n){console.warn(JSON.stringify({event:"\u006b\u0065\u0072\u006e\u0065\u006c_proxy_delete_failed",proxy_id_suffix:t.slice(-6),message:Mt(n)}))}}async function co(e,t,n=!1,a=!1,i,s){let o=Ns(e,{forceManaged:n,force\u004b\u0065\u0072\u006e\u0065\u006c:a}),d=new Dn,l=new Hn(o,d,i,s);if(t?.aborted)return{result:null,error:Ya(t),cleanup:await o.close(),debug:null};let U,u=t?new Promise((m,k)=>{U=()=>k(Ya(t)),t.addEventListener("abort",U,{once:!0})}):null,r=null,S=null,c,h=null;try{let m=l.extract(e,t);u&&m.catch(()=>{}),r=await(u?Promise.race([m,u]):m)}catch(m){S=m}finally{t&&U&&t.removeEventListener("abort",U),h=r?.diagnostics.debug??(e.debug?{enabled:!0,request:{query:e.query,locationInput:e.location??null,canonicalLocation:null,locationResolved:null,locationMatchSource:null,locationAmbiguousCandidates:null,uule:null,gl:e.gl,hl:e.hl,device:e.device,proxyMode:e.proxyMode,proxyZip:e.proxyZip??null,serpIdentity:e.serpIdentity??null,serpOnly:e.serpOnly,pages:e.pages??1},browser:o.getDebugSnapshot()}:null),c=await o.close()}if(S){let m=Ka(S),k=m==="timeout"||m==="request_aborted"||m==="browser_session_interrupted"?m:null,f=null;if(k)try{f=typeof l.getPartialResult=="function"?l.getPartialResult():null}catch{f=null}return f&&k?(f.diagnostics={...f.diagnostics,interruption:{code:k,reasonSource:"client_runtime",message:Mt(S)}},{result:f,error:S,cleanup:c,debug:h,salvagedFrom:S}):{result:null,error:S,cleanup:c,debug:h}}return{result:r,error:null,cleanup:c,debug:h}}async function ho(e){let t=typeof e=="object"&&e!==null?e:{},n=vl(e),a=bl(e),i=_l(e),o=t.forceManagedSerp===!0||On()&&!t.serpIdentity,d=t.proxyMode,l=d==="location"||d==="none"||d==="configured"?d:zn,U=typeof t.\u006b\u0065\u0072\u006e\u0065\u006cApiKey=="string"?t.\u006b\u0065\u0072\u006e\u0065\u006cApiKey.trim():Si(),u=typeof t.\u006b\u0065\u0072\u006e\u0065\u006cProxyId=="string"?t.\u006b\u0065\u0072\u006e\u0065\u006cProxyId.trim():void 0,r={\u006b\u0065\u0072\u006e\u0065\u006cApiKey:U,proxyMode:l,configured\u004b\u0065\u0072\u006e\u0065\u006cProxyId:u,location:typeof t.location=="string"?t.location:void 0,proxyZip:typeof t.proxyZip=="string"?t.proxyZip:void 0,gl:typeof t.gl=="string"?t.gl:"us"},S=typeof t.debug=="boolean"?t.debug:!1,c=l==="location"&&!!r.location,h=t.maxAttempts,m=typeof h=="number"&&Number.isInteger(h)&&h>=1?Math.min(Va,h):Al(l),k=Math.min(Va,m),f=typeof t.softDeadlineMs=="number"?t.softDeadlineMs:null,C=new Bn,A=null,R=l,F=0,E=()=>f==null||f-Date.now()>=Nl;for(let M=0;M<k;M++){let P=M+1,x=Date.now();try{if(n?.aborted)throw Ya(n);let y=await so({...r,proxyMode:R,attemptIndex:M,fresh:R==="location"});if(!y)break;let N={...t,\u006b\u0065\u0072\u006e\u0065\u006cApiKey:U,\u006b\u0065\u0072\u006e\u0065\u006cProxyId:y.\u006b\u0065\u0072\u006e\u0065\u006cProxyId,\u006b\u0065\u0072\u006e\u0065\u006cProxyResolution:y.resolution,proxyMode:R,keepDefaultProxy:M>0,debug:S||c};R==="none"&&(N.\u006b\u0065\u0072\u006e\u0065\u006cProxyId=void 0);let p=di.parse(N);await Ie(a,{type:"started",attemptNumber:P,maxAttempts:k,query:p.query,location:p.location??null,maxQuestions:p.maxQuestions,startedAt:new Date(x).toISOString()}),console.info(JSON.stringify({event:"harvest_attempt_started",attempt_number:P,max_attempts:k,query:p.query,location:p.location??null,max_questions:p.maxQuestions}));let v=zt(),_=Z=>Ie(a,{type:"provider_session_started",attemptNumber:P,maxAttempts:k,provider:Z.provider,providerSessionId:Z.providerSessionId,observedAt:new Date().toISOString()}),I=v?.harvestBrowserMode==="headless-test"&&process.env[ds]==="true",L=o&&M>0&&!!U,W=v?await Ss({...v,forceHeadless:I,forceHeadful:!I},()=>co(p,n,o&&!L,L,i,_)):await co(p,n,o&&!L,L,i,_);if(W.error){let Z=W.error,J=Ka(Z);(J==="captcha"||J==="proxy_tunnel_failed")&&R==="location"&&(F++,F>=2&&(R="none"));let ht=!W.result&&M<k-1&&E()&&(J==="captcha"||So(J)||l==="location"&&lo(J));if(J==="captcha"?console.warn(JSON.stringify({event:"harvest_attempt_captcha",attempt_number:P,max_attempts:k,message:Mt(Z),will_retry:ht})):ht&&console.warn(JSON.stringify({event:"harvest_attempt_proxy_retry",attempt_number:P,max_attempts:k,outcome:J,message:Mt(Z),will_retry:ht})),await Ie(a,{type:"finished",attemptNumber:P,maxAttempts:k,outcome:J,\u006b\u0065\u0072\u006e\u0065\u006cSessionId:W.cleanup.\u006b\u0065\u0072\u006e\u0065\u006cSessionId,questionCount:W.result?.totalQuestions??0,durationMs:Date.now()-x,error:Mt(Z),willRetry:ht,cleanup:W.cleanup,debug:W.debug,completedAt:new Date().toISOString()}),await Ga(U,y.disposableProxyId),A=Z,ht)continue;if(W.result){let V=await uo(W.result,f);return ro(oo(Uo(V,S),p.maxQuestions,p.questionsOnly),M,L?1:0)}break}let O=W.result;if(!O)throw new Error("Harvest attempt completed without a result");if(Rl(O,l)){let Z=new qe(Ol(O)),J=M<k-1&&E();if(console.warn(JSON.stringify({event:"harvest_attempt_location_mismatch",attempt_number:P,max_attempts:k,message:Z.message,will_retry:J})),await Ie(a,{type:"finished",attemptNumber:P,maxAttempts:k,outcome:"location_mismatch",\u006b\u0065\u0072\u006e\u0065\u006cSessionId:W.cleanup.\u006b\u0065\u0072\u006e\u0065\u006cSessionId,questionCount:O.totalQuestions,durationMs:Date.now()-x,error:Z.message,willRetry:J,cleanup:W.cleanup,debug:W.debug,completedAt:new Date().toISOString()}),await Ga(U,y.disposableProxyId),A=Z,J)continue;break}let z=await uo(O,f),D=ro(oo(Uo(z,S),p.maxQuestions,p.questionsOnly),M,L?1:0),Q=El(D),ct=Q==="degraded_result"&&M<k-1&&E(),ft=Q==="degraded_result"?Il(D):null;if(ct&&console.warn(JSON.stringify({event:"harvest_attempt_degraded_result",attempt_number:P,max_attempts:k,degradation_reasons:D.diagnostics.degradationReasons,will_retry:!0})),await Ie(a,{type:"finished",attemptNumber:P,maxAttempts:k,outcome:Q,\u006b\u0065\u0072\u006e\u0065\u006cSessionId:W.cleanup.\u006b\u0065\u0072\u006e\u0065\u006cSessionId,questionCount:D.totalQuestions,durationMs:Date.now()-x,error:ft,willRetry:ct,cleanup:W.cleanup,debug:W.debug,completedAt:new Date().toISOString()}),await Ga(U,y.disposableProxyId),ct)continue;return(p.format==="json"||p.format==="both")&&await C.writeJSON(D,p.outputDir),(p.format==="csv"||p.format==="both")&&await Promise.all([C.writeCSV(D.flat,p.outputDir),D.videos.length>0?C.writeVideoCSV(D.videos,D.seed,p.outputDir):Promise.resolve(""),D.forums.length>0?C.writeForumCSV(D.forums,D.seed,p.outputDir):Promise.resolve(""),D.aiOverview.detected?C.writeAIOverviewCSV(D.aiOverview.citations,D.aiOverview.text,D.seed,p.outputDir):Promise.resolve(""),D.aiMode.detected?C.writeAIModeCSV(D.aiMode.citations,D.aiMode.text,D.seed,p.outputDir):Promise.resolve(""),D.whatPeopleSaying.length>0?C.writeWhatPeopleSayingCSV(D.whatPeopleSaying,D.seed,p.outputDir):Promise.resolve("")]),D}catch(y){let N=Ka(y);(N==="captcha"||N==="proxy_tunnel_failed")&&R==="location"&&(F++,F>=2&&(R="none"));let p=M<k-1&&E()&&(N==="captcha"||So(N)||l==="location"&&lo(N));if(N==="captcha"?console.warn(JSON.stringify({event:"harvest_attempt_captcha",attempt_number:P,max_attempts:k,message:Mt(y),will_retry:p})):p&&console.warn(JSON.stringify({event:"harvest_attempt_proxy_retry",attempt_number:P,max_attempts:k,outcome:N,message:Mt(y),will_retry:p})),await Ie(a,{type:"finished",attemptNumber:P,maxAttempts:k,outcome:N,\u006b\u0065\u0072\u006e\u0065\u006cSessionId:null,questionCount:0,durationMs:Date.now()-x,error:Mt(y),willRetry:p,cleanup:{\u006b\u0065\u0072\u006e\u0065\u006cSessionId:null,\u006b\u0065\u0072\u006e\u0065\u006cDeleteStarted:!1,\u006b\u0065\u0072\u006e\u0065\u006cDeleteSucceeded:null,\u006b\u0065\u0072\u006e\u0065\u006cDeleteError:null,browserCloseSucceeded:null,browserCloseError:null},debug:null,completedAt:new Date().toISOString()}),A=y,p)continue;if(N==="captcha")break;throw y}}throw A&&!(A instanceof nt)?A:(console.warn(JSON.stringify({event:"harvest_captcha_exhausted",max_attempts:k,session_kind:U?"\u006b\u0065\u0072\u006e\u0065\u006c":"local"})),new nt(ss(`CAPTCHA on all ${k} fresh sessions. Try again in a few minutes.`)))}var fo=require("child_process"),dt=require("fs"),ei=require("os"),te=require("path"),ee=require("@fal-ai/client");var Wl="https://api.deepinfra.com/v1/openai/chat/completions",po="https://openrouter.ai/api/v1/chat/completions",Ja="Qwen/Qwen3.6-35B-A3B",Hl=`You are a video prompt engineer for Seedance 2.0 text-to-video AI.
21903
21903
 
21904
21904
  IMPORTANT TECHNICAL CONTEXT: Clip 2 will be generated using image-to-video, meaning it will start from the exact last frame of Clip 1. The two clips will be visually seamless \u2014 same location, same characters, continuous motion. You must write prompts that make this feel like one uninterrupted 16-second video.
21905
21905
 
@@ -21921,20 +21921,20 @@ Rules for visuals:
21921
21921
  - Describe exactly what the camera sees
21922
21922
 
21923
21923
  Respond with JSON only:
21924
- {"clip1": "...", "clip2": "...", "voiceover": "...", "audioMood": "..."}`;async function ho(e,t,n,a){let i=await fetch(t,{method:"POST",headers:{Authorization:`Bearer ${e}`,"Content-Type":"application/json"},body:JSON.stringify({model:Ja,temperature:.7,messages:[{role:"system",content:Rl},{role:"user",content:`Question: ${n}
21924
+ {"clip1": "...", "clip2": "...", "voiceover": "...", "audioMood": "..."}`;async function go(e,t,n,a){let i=await fetch(t,{method:"POST",headers:{Authorization:`Bearer ${e}`,"Content-Type":"application/json"},body:JSON.stringify({model:Ja,temperature:.7,messages:[{role:"system",content:Hl},{role:"user",content:`Question: ${n}
21925
21925
 
21926
- Answer: ${a.slice(0,500)}`}]})});if(!i.ok)throw new Error(`LLM call failed (${i.status}): ${await i.text()}`);let s=await i.json();t===go?va({vendor:"openrouter",model:Ja,units:s.usage?.cost??0,unitType:"usd"}):va({vendor:"deepinfra_qwen",model:Ja,units:s.usage?.completion_tokens??0,unitType:"tokens"});let o=s.choices[0]?.message?.content?.trim()??"",d=o.match(/\{[\s\S]*\}/);if(!d)throw new Error(`No JSON in QWEN response: ${o.slice(0,200)}`);let l=JSON.parse(d[0]);if(!l.clip1||!l.clip2||!l.voiceover||!l.audioMood)throw new Error(`QWEN response missing fields: ${o.slice(0,200)}`);return l}async function Gn(e,t){let n=process.env.DEEPINFRA_API_KEY,a=process.env.OPENROUTER_API_KEY;if(n)try{return await ho(n,Ol,e,t)}catch(i){console.warn("[promptBuilder] DeepInfra failed, trying OpenRouter:",i.message)}if(a)return await ho(a,go,e,t);throw new Error("No LLM key \u2014 set DEEPINFRA_API_KEY or OPENROUTER_API_KEY")}function po(e){if(!e.clip1||!e.clip2||!e.voiceover||!e.audioMood)throw new Error("Episode brief is missing prompt fields \u2014 run blog-to-video skill to regenerate");return{clip1:e.clip1,clip2:e.clip2,voiceover:e.voiceover,audioMood:e.audioMood}}var en=require("fs"),qa=require("path"),mo=require("os"),Vn=require("@fal-ai/client"),Wl="fal-ai/mmaudio-v2",Hl="fal-ai/elevenlabs/tts",Bl="fal-ai/google/gemini-2.5-flash-preview-tts";async function wo(e){let t=await fetch(e);if(!t.ok)throw new Error(`Failed to download TTS audio (${t.status})`);return Buffer.from(await t.arrayBuffer())}async function Xa(e){console.log("[AudioGenerator] Generating voiceover...");let t=(0,qa.join)((0,mo.tmpdir)(),`tts-${Date.now()}`);(0,en.mkdirSync)(t,{recursive:!0});let n=(0,qa.join)(t,"voiceover.mp3");try{let s=process.env.ELEVENLABS_VOICE_ID??"pNInz6obpgDQGcFmaJgB",o=await Vn.fal.run(Hl,{input:{text:e,voice_id:s,model_id:"eleven_v3"}});return(0,en.writeFileSync)(n,await wo(o.audio.url)),console.log("[AudioGenerator] TTS: ElevenLabs via fal"),n}catch(s){console.warn("[AudioGenerator] ElevenLabs via fal failed, trying Gemini:",s.message)}let a=process.env.GEMINI_TTS_VOICE??"Kore",i=await Vn.fal.run(Bl,{input:{text:e,voice:a}});return(0,en.writeFileSync)(n,await wo(i.audio.url)),console.log("[AudioGenerator] TTS: Gemini via fal"),n}async function ja(e,t,n){return console.log("[AudioGenerator] Adding background audio via MMAudio V2..."),(await Vn.fal.run(Wl,{input:{video_url:e,prompt:t,negative_prompt:"speech, voice, talking, dialogue, narration, vocals, singing, human voice, conversation, words, lyrics, announcer, commentary",duration:n,cfg_strength:4.5}})).video.url}var $a=require("child_process"),Yn=require("fs"),Ae=require("path"),yo=require("@fal-ai/client");async function za(e,t){let n=await fetch(e);if(!n.ok)throw new Error(`Download failed (${n.status}): ${e}`);(0,Yn.writeFileSync)(t,Buffer.from(await n.arrayBuffer()))}async function Qa(e,t,n){(0,Yn.mkdirSync)(n,{recursive:!0});let a=Date.now(),i=(0,Ae.join)(n,`clip1-${a}.mp4`),s=(0,Ae.join)(n,`clip2-${a}.mp4`),o=(0,Ae.join)(n,`combined-${a}.mp4`);return console.log("[VideoMixer] Downloading clips..."),await Promise.all([za(e,i),za(t,s)]),console.log("[VideoMixer] Concatenating..."),(0,$a.execSync)(`ffmpeg -i "${i}" -i "${s}" -filter_complex "[0:v][1:v]concat=n=2:v=1:a=0[v]" -map "[v]" -y "${o}" -loglevel error`),o}async function Za(e){let{readFileSync:t}=await import("fs"),n=new Blob([t(e)],{type:"video/mp4"}),a=await yo.fal.storage.upload(n);return console.log("[VideoMixer] Uploaded to fal:",a),a}async function ti(e,t,n){let a=Date.now(),i=(0,Ae.join)(n,`voiceover-${a}.wav`),s=(0,Ae.join)(n,`final-${a}.mp4`);return console.log("[VideoMixer] Downloading voiceover..."),await za(t,i),console.log("[VideoMixer] Mixing voiceover over background audio..."),(0,$a.execSync)(`ffmpeg -i "${e}" -i "${i}" -filter_complex "[0:a]volume=0.2[bg];[1:a]volume=1.0[vo];[bg][vo]amix=inputs=2:duration=first[a]" -map 0:v -map "[a]" -c:v copy -y "${s}" -loglevel error`),s}var Co="bytedance/seedance-2.0/text-to-video",ko="bytedance/seedance-2.0/image-to-video";function Kn(e,t,n,a){return{prompt:e,resolution:t.resolution??"720p",duration:t.clipDurationSeconds??8,aspect_ratio:t.aspectRatio??"16:9",generate_audio:!1,...n!==void 0?{seed:n}:{},...a!==void 0?{image_url:a}:{}}}async function Jn(e,t){let{request_id:n}=await ee.fal.queue.submit(e,{input:t});for(console.log(`[fal] submitted ${e} \u2192 ${n}`);;){await new Promise(s=>setTimeout(s,5e3));let a=await ee.fal.queue.status(e,{requestId:n,logs:!1});if(console.log(`[fal] ${n} \u2192 ${a.status}`),a.status==="FAILED")throw new Error(`Request ${n} failed`);if(a.status!=="COMPLETED")continue;return(await ee.fal.queue.result(e,{requestId:n})).data}}async function To(e,t){let n=Date.now(),a=(0,te.join)(t,`clip1-raw-${n}.mp4`),i=(0,te.join)(t,`last-frame-${n}.jpg`),s=await fetch(e);if(!s.ok)throw new Error(`Failed to download clip 1 (${s.status})`);(0,dt.writeFileSync)(a,Buffer.from(await s.arrayBuffer()));try{(0,Mo.execSync)(`ffmpeg -sseof -0.1 -i "${a}" -vframes 1 -y "${i}" -loglevel error`)}finally{try{(0,dt.unlinkSync)(a)}catch{}}return i}var qn=class{constructor(t){let n=t??process.env.FAL_KEY;if(!n)throw new Error("FAL_KEY is required");ee.fal.config({credentials:n})}async generateClipPair(t,n,a={}){let i=a.outputDir??(0,te.join)((0,ei.tmpdir)(),`paa-video-${Date.now()}`);(0,dt.mkdirSync)(i,{recursive:!0}),console.log(`
21927
- [1/7] Generating prompts via QWEN 3.6...`);let s=await Gn(t,n);console.log(" Voiceover:",s.voiceover),console.log(" Audio mood:",s.audioMood),console.log(`
21928
- [2/7] Generating clip 1 (text-to-video)...`);let o=await Jn(Co,Kn(s.clip1,a,a.seed));console.log(`
21929
- [3/7] Extracting last frame \u2192 clip 2 start...`);let d=await To(o.video.url,i),l=new Blob([(0,dt.readFileSync)(d)],{type:"image/jpeg"}),U=await ee.fal.storage.upload(l);try{(0,dt.unlinkSync)(d)}catch{}console.log(`
21930
- [4/7] Generating clip 2 (image-to-video from last frame)...`);let u=a.seed!==void 0?a.seed+1:void 0,r=await Jn(ko,Kn(s.clip2,a,u,U));console.log(`
21926
+ Answer: ${a.slice(0,500)}`}]})});if(!i.ok)throw new Error(`LLM call failed (${i.status}): ${await i.text()}`);let s=await i.json();t===po?ba({vendor:"openrouter",model:Ja,units:s.usage?.cost??0,unitType:"usd"}):ba({vendor:"deepinfra_qwen",model:Ja,units:s.usage?.completion_tokens??0,unitType:"tokens"});let o=s.choices[0]?.message?.content?.trim()??"",d=o.match(/\{[\s\S]*\}/);if(!d)throw new Error(`No JSON in QWEN response: ${o.slice(0,200)}`);let l=JSON.parse(d[0]);if(!l.clip1||!l.clip2||!l.voiceover||!l.audioMood)throw new Error(`QWEN response missing fields: ${o.slice(0,200)}`);return l}async function Vn(e,t){let n=process.env.DEEPINFRA_API_KEY,a=process.env.OPENROUTER_API_KEY;if(n)try{return await go(n,Wl,e,t)}catch(i){console.warn("[promptBuilder] DeepInfra failed, trying OpenRouter:",i.message)}if(a)return await go(a,po,e,t);throw new Error("No LLM key \u2014 set DEEPINFRA_API_KEY or OPENROUTER_API_KEY")}function wo(e){if(!e.clip1||!e.clip2||!e.voiceover||!e.audioMood)throw new Error("Episode brief is missing prompt fields \u2014 run blog-to-video skill to regenerate");return{clip1:e.clip1,clip2:e.clip2,voiceover:e.voiceover,audioMood:e.audioMood}}var en=require("fs"),qa=require("path"),yo=require("os"),Yn=require("@fal-ai/client"),Bl="fal-ai/mmaudio-v2",Dl="fal-ai/elevenlabs/tts",Fl="fal-ai/google/gemini-2.5-flash-preview-tts";async function mo(e){let t=await fetch(e);if(!t.ok)throw new Error(`Failed to download TTS audio (${t.status})`);return Buffer.from(await t.arrayBuffer())}async function Xa(e){console.log("[AudioGenerator] Generating voiceover...");let t=(0,qa.join)((0,yo.tmpdir)(),`tts-${Date.now()}`);(0,en.mkdirSync)(t,{recursive:!0});let n=(0,qa.join)(t,"voiceover.mp3");try{let s=process.env.ELEVENLABS_VOICE_ID??"pNInz6obpgDQGcFmaJgB",o=await Yn.fal.run(Dl,{input:{text:e,voice_id:s,model_id:"eleven_v3"}});return(0,en.writeFileSync)(n,await mo(o.audio.url)),console.log("[AudioGenerator] TTS: ElevenLabs via fal"),n}catch(s){console.warn("[AudioGenerator] ElevenLabs via fal failed, trying Gemini:",s.message)}let a=process.env.GEMINI_TTS_VOICE??"Kore",i=await Yn.fal.run(Fl,{input:{text:e,voice:a}});return(0,en.writeFileSync)(n,await mo(i.audio.url)),console.log("[AudioGenerator] TTS: Gemini via fal"),n}async function ja(e,t,n){return console.log("[AudioGenerator] Adding background audio via MMAudio V2..."),(await Yn.fal.run(Bl,{input:{video_url:e,prompt:t,negative_prompt:"speech, voice, talking, dialogue, narration, vocals, singing, human voice, conversation, words, lyrics, announcer, commentary",duration:n,cfg_strength:4.5}})).video.url}var $a=require("child_process"),Kn=require("fs"),Ae=require("path"),Co=require("@fal-ai/client");async function za(e,t){let n=await fetch(e);if(!n.ok)throw new Error(`Download failed (${n.status}): ${e}`);(0,Kn.writeFileSync)(t,Buffer.from(await n.arrayBuffer()))}async function Qa(e,t,n){(0,Kn.mkdirSync)(n,{recursive:!0});let a=Date.now(),i=(0,Ae.join)(n,`clip1-${a}.mp4`),s=(0,Ae.join)(n,`clip2-${a}.mp4`),o=(0,Ae.join)(n,`combined-${a}.mp4`);return console.log("[VideoMixer] Downloading clips..."),await Promise.all([za(e,i),za(t,s)]),console.log("[VideoMixer] Concatenating..."),(0,$a.execSync)(`ffmpeg -i "${i}" -i "${s}" -filter_complex "[0:v][1:v]concat=n=2:v=1:a=0[v]" -map "[v]" -y "${o}" -loglevel error`),o}async function Za(e){let{readFileSync:t}=await import("fs"),n=new Blob([t(e)],{type:"video/mp4"}),a=await Co.fal.storage.upload(n);return console.log("[VideoMixer] Uploaded to fal:",a),a}async function ti(e,t,n){let a=Date.now(),i=(0,Ae.join)(n,`voiceover-${a}.wav`),s=(0,Ae.join)(n,`final-${a}.mp4`);return console.log("[VideoMixer] Downloading voiceover..."),await za(t,i),console.log("[VideoMixer] Mixing voiceover over background audio..."),(0,$a.execSync)(`ffmpeg -i "${e}" -i "${i}" -filter_complex "[0:a]volume=0.2[bg];[1:a]volume=1.0[vo];[bg][vo]amix=inputs=2:duration=first[a]" -map 0:v -map "[a]" -c:v copy -y "${s}" -loglevel error`),s}var ko="bytedance/seedance-2.0/text-to-video",To="bytedance/seedance-2.0/image-to-video";function Jn(e,t,n,a){return{prompt:e,resolution:t.resolution??"720p",duration:t.clipDurationSeconds??8,aspect_ratio:t.aspectRatio??"16:9",generate_audio:!1,...n!==void 0?{seed:n}:{},...a!==void 0?{image_url:a}:{}}}async function qn(e,t){let{request_id:n}=await ee.fal.queue.submit(e,{input:t});for(console.log(`[fal] submitted ${e} \u2192 ${n}`);;){await new Promise(s=>setTimeout(s,5e3));let a=await ee.fal.queue.status(e,{requestId:n,logs:!1});if(console.log(`[fal] ${n} \u2192 ${a.status}`),a.status==="FAILED")throw new Error(`Request ${n} failed`);if(a.status!=="COMPLETED")continue;return(await ee.fal.queue.result(e,{requestId:n})).data}}async function Mo(e,t){let n=Date.now(),a=(0,te.join)(t,`clip1-raw-${n}.mp4`),i=(0,te.join)(t,`last-frame-${n}.jpg`),s=await fetch(e);if(!s.ok)throw new Error(`Failed to download clip 1 (${s.status})`);(0,dt.writeFileSync)(a,Buffer.from(await s.arrayBuffer()));try{(0,fo.execSync)(`ffmpeg -sseof -0.1 -i "${a}" -vframes 1 -y "${i}" -loglevel error`)}finally{try{(0,dt.unlinkSync)(a)}catch{}}return i}var Xn=class{constructor(t){let n=t??process.env.FAL_KEY;if(!n)throw new Error("FAL_KEY is required");ee.fal.config({credentials:n})}async generateClipPair(t,n,a={}){let i=a.outputDir??(0,te.join)((0,ei.tmpdir)(),`paa-video-${Date.now()}`);(0,dt.mkdirSync)(i,{recursive:!0}),console.log(`
21927
+ [1/7] Generating prompts via QWEN 3.6...`);let s=await Vn(t,n);console.log(" Voiceover:",s.voiceover),console.log(" Audio mood:",s.audioMood),console.log(`
21928
+ [2/7] Generating clip 1 (text-to-video)...`);let o=await qn(ko,Jn(s.clip1,a,a.seed));console.log(`
21929
+ [3/7] Extracting last frame \u2192 clip 2 start...`);let d=await Mo(o.video.url,i),l=new Blob([(0,dt.readFileSync)(d)],{type:"image/jpeg"}),U=await ee.fal.storage.upload(l);try{(0,dt.unlinkSync)(d)}catch{}console.log(`
21930
+ [4/7] Generating clip 2 (image-to-video from last frame)...`);let u=a.seed!==void 0?a.seed+1:void 0,r=await qn(To,Jn(s.clip2,a,u,U));console.log(`
21931
21931
  [5/7] Concatenating clips + generating voiceover (parallel)...`);let[S,c]=await Promise.all([Qa(o.video.url,r.video.url,i),Xa(s.voiceover)]);console.log(`
21932
21932
  [6/7] Adding background audio via MMAudio V2...`);let h=await Za(S),m=(a.clipDurationSeconds??8)*2,k=await ja(h,s.audioMood,m);console.log(`
21933
- [7/7] Overlaying voiceover on final video...`);let f=(0,te.join)(i,`with-bg-audio-${Date.now()}.mp4`),C=await fetch(k);(0,dt.writeFileSync)(f,Buffer.from(await C.arrayBuffer()));let A=await ti(f,c,i);return{clip1Url:o.video.url,clip2Url:r.video.url,finalVideoPath:A,seed:o.seed,promptClip1:s.clip1,promptClip2:s.clip2,voiceover:s.voiceover,audioMood:s.audioMood}}async generateEpisode(t,n={}){let a=n.outputDir??(0,te.join)((0,ei.tmpdir)(),`episode-${t.episodeNumber}-${Date.now()}`);(0,dt.mkdirSync)(a,{recursive:!0});let i=po(t);console.log(`
21933
+ [7/7] Overlaying voiceover on final video...`);let f=(0,te.join)(i,`with-bg-audio-${Date.now()}.mp4`),C=await fetch(k);(0,dt.writeFileSync)(f,Buffer.from(await C.arrayBuffer()));let A=await ti(f,c,i);return{clip1Url:o.video.url,clip2Url:r.video.url,finalVideoPath:A,seed:o.seed,promptClip1:s.clip1,promptClip2:s.clip2,voiceover:s.voiceover,audioMood:s.audioMood}}async generateEpisode(t,n={}){let a=n.outputDir??(0,te.join)((0,ei.tmpdir)(),`episode-${t.episodeNumber}-${Date.now()}`);(0,dt.mkdirSync)(a,{recursive:!0});let i=wo(t);console.log(`
21934
21934
  [Episode ${t.episodeNumber}/${t.episodeCount}] ${t.sectionTitle}`),console.log(" Voiceover:",i.voiceover),console.log(`
21935
- [2/7] Generating clip 1 (text-to-video)...`);let s=await Jn(Co,Kn(i.clip1,n,n.seed));console.log(`
21936
- [3/7] Extracting last frame \u2192 clip 2 start...`);let o=await To(s.video.url,a),d=new Blob([(0,dt.readFileSync)(o)],{type:"image/jpeg"}),l=await ee.fal.storage.upload(d);try{(0,dt.unlinkSync)(o)}catch{}console.log(`
21937
- [4/7] Generating clip 2 (image-to-video from last frame)...`);let U=n.seed!==void 0?n.seed+1:void 0,u=await Jn(ko,Kn(i.clip2,n,U,l));console.log(`
21935
+ [2/7] Generating clip 1 (text-to-video)...`);let s=await qn(ko,Jn(i.clip1,n,n.seed));console.log(`
21936
+ [3/7] Extracting last frame \u2192 clip 2 start...`);let o=await Mo(s.video.url,a),d=new Blob([(0,dt.readFileSync)(o)],{type:"image/jpeg"}),l=await ee.fal.storage.upload(d);try{(0,dt.unlinkSync)(o)}catch{}console.log(`
21937
+ [4/7] Generating clip 2 (image-to-video from last frame)...`);let U=n.seed!==void 0?n.seed+1:void 0,u=await qn(To,Jn(i.clip2,n,U,l));console.log(`
21938
21938
  [5/7] Concatenating clips + generating voiceover (parallel)...`);let[r,S]=await Promise.all([Qa(s.video.url,u.video.url,a),Xa(i.voiceover)]);console.log(`
21939
21939
  [6/7] Adding background audio via MMAudio V2...`);let c=await Za(r),h=(n.clipDurationSeconds??8)*2,m=await ja(c,i.audioMood,h);console.log(`
21940
21940
  [7/7] Overlaying voiceover on final video...`);let k=(0,te.join)(a,`with-bg-audio-${Date.now()}.mp4`),f=await fetch(m);(0,dt.writeFileSync)(k,Buffer.from(await f.arrayBuffer()));let C=await ti(k,S,a);return{clip1Url:s.video.url,clip2Url:u.video.url,finalVideoPath:C,seed:s.seed,promptClip1:i.clip1,promptClip2:i.clip2,voiceover:i.voiceover,audioMood:i.audioMood}}};0&&(module.exports={VideoGenerator,buildClipPrompts,harvest});