mcp-scraper 0.89.0 → 0.89.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +32 -14
- package/README.md +5 -3
- package/dist/{analytics-repository-2BMT5JNE.js → analytics-repository-J25XR5E7.js} +1 -1
- package/dist/bin/api-server.js +1 -1
- package/dist/bin/mcp-scraper-cli.js +1 -1
- package/dist/bin/mcp-scraper-core.js +1 -1
- package/dist/bin/mcp-scraper-install.js +1 -1
- package/dist/bin/mcp-stdio-server.js +1 -1
- package/dist/bin/paa-harvest.js +1 -1
- package/dist/chunk-3GP5CYZX.js +1 -1
- package/dist/chunk-4FROKQJN.js +1 -1
- package/dist/{chunk-TXENITMS.js → chunk-57AHYL2O.js} +1 -1
- package/dist/chunk-5H3MCTTP.js +1 -0
- package/dist/{chunk-GMWKPIYX.js → chunk-63T4W2LX.js} +182 -83
- package/dist/{chunk-WSCGYRWA.js → chunk-645LQBE7.js} +1 -1
- package/dist/{chunk-7YGVI5J4.js → chunk-6QO4F2KC.js} +3 -3
- package/dist/{chunk-M22MM4N4.js → chunk-D6IQJP64.js} +1 -1
- package/dist/{chunk-CFI6CXIV.js → chunk-DGBLGFNJ.js} +1 -1
- package/dist/{chunk-4AI7DOS7.js → chunk-FFPD2DOF.js} +1 -1
- package/dist/chunk-FXEWHK54.js +1 -0
- package/dist/{chunk-HDPYG3XV.js → chunk-IJ2AO7AO.js} +1 -1
- package/dist/{chunk-PUHFVA7P.js → chunk-MEM4D2QN.js} +4 -4
- package/dist/{chunk-E2WRWV3A.js → chunk-PRWZNHQB.js} +1 -1
- package/dist/chunk-QPWPR5XG.js +3 -3
- package/dist/{chunk-MASR22K4.js → chunk-RYNHNEF3.js} +15 -15
- package/dist/{chunk-L4CGLFPU.js → chunk-YUPLOOTB.js} +1 -1
- package/dist/{chunk-XPZVJIZ2.js → chunk-ZHORX44A.js} +1 -1
- package/dist/db-JJWJLAVQ.js +1 -0
- package/dist/{extract-bundle-SNUIHM3J.js → extract-bundle-TTSEGWYF.js} +1 -1
- package/dist/{gmail-service-BZ3H75XC.js → gmail-service-34PIJRLK.js} +1 -1
- package/dist/index.cjs +143 -44
- package/dist/index.d.cts +14 -14
- package/dist/index.d.ts +14 -14
- package/dist/index.js +1 -1
- package/dist/{lead-list-enrichment-repository-S2H3U7T7.js → lead-list-enrichment-repository-VJBBMBCG.js} +1 -1
- package/dist/{location-data-repository-OTWHWMV6.js → location-data-repository-W3LPT6G3.js} +1 -1
- package/dist/{server-RFR2A5UJ.js → server-UEY5YQL6.js} +846 -820
- package/dist/{site-extract-repository-SE776XDC.js → site-extract-repository-7W5ZUNPR.js} +1 -1
- package/dist/worker-ZOTOKG56.js +1 -0
- package/package.json +131 -17
- package/THIRD_PARTY_NOTICES.html +0 -203
- package/dist/chunk-X54CQLK2.js +0 -1
- package/dist/chunk-Z2QGQJS2.js +0 -1
- package/dist/db-F2MX63GI.js +0 -1
- package/dist/worker-XUDSM3AL.js +0 -1
package/dist/index.cjs
CHANGED
|
@@ -1,24 +1,123 @@
|
|
|
1
|
-
"use strict";var Co=Object.create;var nn=Object.defineProperty;var ko=Object.getOwnPropertyDescriptor;var Mo=Object.getOwnPropertyNames;var fo=Object.getPrototypeOf,To=Object.prototype.hasOwnProperty;var No=(e,t)=>{for(var n in t)nn(e,n,{get:t[n],enumerable:!0})},ti=(e,t,n,a)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of Mo(t))!To.call(e,i)&&i!==n&&nn(e,i,{get:()=>t[i],enumerable:!(a=ko(t,i))||a.enumerable});return e};var an=(e,t,n)=>(n=e!=null?Co(fo(e)):{},ti(t||!e||!e.__esModule?nn(n,"default",{value:e,enumerable:!0}):n,e)),vo=e=>ti(nn({},"__esModule",{value:!0}),e);var El={};No(El,{VideoGenerator:()=>Dn,buildClipPrompts:()=>On,harvest:()=>so});module.exports=vo(El);var w=require("zod"),Yn="none",bo="none",ei=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(Yn),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(1e3).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 _e(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 Al=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)}),Ll=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(bo),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)}),ni=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([])}),xl=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()}),Ol=w.z.object({day:w.z.string(),hours:w.z.string()}),Wl=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()}))}),Rl=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()}),Bl=w.z.object({section:w.z.string(),attribute:w.z.string()});function ai(){return(process.env.BROWSER_SERVICE_API_KEY??process.env.\u004b\u0045\u0052\u004e\u0045\u004c_API_KEY)?.trim()||void 0}var Nn=require("playwright-extra"),ls=an(require("puppeteer-extra-plugin-stealth"),1),Ss=require("playwright");function Q(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 V(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 Kn=function(){let{crypto:e}=globalThis;if(e?.randomUUID)return Kn=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 Ie(e){return typeof e=="object"&&e!==null&&("name"in e&&e.name==="AbortError"||"message"in e&&String(e.message).includes("FetchRequestCanceledException"))}var Ae=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{},Z=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 Ct({message:a,cause:Ae(n)});let s=n;return t===400?new te(t,s,a,i):t===401?new ee(t,s,a,i):t===403?new ne(t,s,a,i):t===404?new ae(t,s,a,i):t===409?new ie(t,s,a,i):t===422?new se(t,s,a,i):t===429?new oe(t,s,a,i):t>=500?new re(t,s,a,i):new e(t,s,a,i)}},nt=class extends Z{constructor({message:t}={}){super(void 0,void 0,t||"Request was aborted.",void 0)}},Ct=class extends Z{constructor({message:t,cause:n}){super(void 0,void 0,t||"Connection error.",void 0),n&&(this.cause=n)}},ht=class extends Ct{constructor({message:t}={}){super({message:t??"Request timed out."})}},te=class extends Z{},ee=class extends Z{},ne=class extends Z{},ae=class extends Z{},ie=class extends Z{},se=class extends Z{},oe=class extends Z{},re=class extends Z{};var Eo=/^[a-z][a-z0-9+.-]*:/i,ii=e=>Eo.test(e),tt=e=>(tt=Array.isArray,tt(e)),Jn=tt;function qn(e){return typeof e!="object"?{}:e??{}}function Xn(e){if(!e)return!0;for(let t in e)return!1;return!0}function si(e,t){return Object.prototype.hasOwnProperty.call(e,t)}var oi=(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},_o=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 Io=e=>typeof e=="boolean"?e:typeof e=="string"?e==="true":!!e,ri=e=>{if(e!=null)return _o(e)};var jn=e=>{if(e!=null)return Io(e)},di=e=>{try{return JSON.parse(e)}catch{return}};var li=e=>new Promise(t=>setTimeout(t,e));var kt="0.93.0";function Ao(){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 Lo=()=>{let e=Ao();if(e==="deno")return{"X-Stainless-Lang":"js","X-Stainless-Package-Version":kt,"X-Stainless-OS":Ui(Deno.build.os),"X-Stainless-Arch":Si(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":kt,"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":kt,"X-Stainless-OS":Ui(globalThis.process.platform??"unknown"),"X-Stainless-Arch":Si(globalThis.process.arch??"unknown"),"X-Stainless-Runtime":"node","X-Stainless-Runtime-Version":globalThis.process.version??"unknown"};let t=xo();return t?{"X-Stainless-Lang":"js","X-Stainless-Package-Version":kt,"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":kt,"X-Stainless-OS":"Unknown","X-Stainless-Arch":"unknown","X-Stainless-Runtime":"unknown","X-Stainless-Runtime-Version":"unknown"}};function xo(){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 Si=e=>e==="x32"?"x32":e==="x86_64"||e==="x64"?"x64":e==="arm"?"arm":e==="aarch64"||e==="arm64"?"arm64":e?`other:${e}`:"unknown",Ui=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"),ui,ci=()=>ui??(ui=Lo());function hi(){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 zn(...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 sn(e){let t=Symbol.asyncIterator in e?e[Symbol.asyncIterator]():e[Symbol.iterator]();return zn({start(){},async pull(n){let{done:a,value:i}=await t.next();a?n.close():n.enqueue(i)},async cancel(){await t.return?.()}})}function $n(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 gi(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 wi=({headers:e,body:t})=>({bodyHeaders:{"content-type":"application/json"},body:JSON.stringify(t)});var Qn="RFC3986",Zn=e=>String(e),ta={RFC1738:e=>String(e).replace(/%20/g,"+"),RFC3986:Zn},pi="RFC1738";var on=(e,t)=>(on=Object.hasOwn??Function.prototype.call.bind(Object.prototype.hasOwnProperty),on(e,t)),ct=(()=>{let e=[];for(let t=0;t<256;++t)e.push("%"+((t<16?"0":"")+t.toString(16)).toUpperCase());return e})();var ea=1024,mi=(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+=ea){let l=s.length>=ea?s.slice(d,d+ea):s,c=[];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===pi&&(r===40||r===41)){c[c.length]=l.charAt(U);continue}if(r<128){c[c.length]=ct[r];continue}if(r<2048){c[c.length]=ct[192|r>>6]+ct[128|r&63];continue}if(r<55296||r>=57344){c[c.length]=ct[224|r>>12]+ct[128|r>>6&63]+ct[128|r&63];continue}U+=1,r=65536+((r&1023)<<10|l.charCodeAt(U)&1023),c[c.length]=ct[240|r>>18]+ct[128|r>>12&63]+ct[128|r>>6&63]+ct[128|r&63]}o+=c.join("")}return o};function yi(e){return!e||typeof e!="object"?!1:!!(e.constructor&&e.constructor.isBuffer&&e.constructor.isBuffer(e))}function na(e,t){if(tt(e)){let n=[];for(let a=0;a<e.length;a+=1)n.push(t(e[a]));return n}return t(e)}var ki={brackets(e){return String(e)+"[]"},comma:"comma",indices(e,t){return String(e)+"["+t+"]"},repeat(e){return String(e)}},Mi=function(e,t){Array.prototype.push.apply(e,tt(t)?t:[t])},Ci,X={addQueryPrefix:!1,allowDots:!1,allowEmptyArrays:!1,arrayFormat:"indices",charset:"utf-8",charsetSentinel:!1,delimiter:"&",encode:!0,encodeDotInKeys:!1,encoder:mi,encodeValuesOnly:!1,format:Qn,formatter:Zn,indices:!1,serializeDate(e){return(Ci??(Ci=Function.prototype.call.bind(Date.prototype.toISOString)))(e)},skipNulls:!1,strictNullHandling:!1};function Ro(e){return typeof e=="string"||typeof e=="number"||typeof e=="boolean"||typeof e=="symbol"||typeof e=="bigint"}var aa={};function fi(e,t,n,a,i,s,o,d,l,c,U,r,S,u,h,m,I,T){let k=e,P=T,W=0,E=!1;for(;(P=P.get(aa))!==void 0&&!E;){let p=P.get(e);if(W+=1,typeof p<"u"){if(p===W)throw new RangeError("Cyclic object value");E=!0}typeof P.get(aa)>"u"&&(W=0)}if(typeof c=="function"?k=c(t,k):k instanceof Date?k=S?.(k):n==="comma"&&tt(k)&&(k=na(k,function(p){return p instanceof Date?S?.(p):p})),k===null){if(s)return l&&!m?l(t,X.encoder,I,"key",u):t;k=""}if(Ro(k)||yi(k)){if(l){let p=m?t:l(t,X.encoder,I,"key",u);return[h?.(p)+"="+h?.(l(k,X.encoder,I,"value",u))]}return[h?.(t)+"="+h?.(String(k))]}let f=[];if(typeof k>"u")return f;let O;if(n==="comma"&&tt(k))m&&l&&(k=na(k,l)),O=[{value:k.length>0?k.join(",")||null:void 0}];else if(tt(c))O=c;else{let p=Object.keys(k);O=U?p.sort(U):p}let A=d?String(t).replace(/\./g,"%2E"):String(t),_=a&&tt(k)&&k.length===1?A+"[]":A;if(i&&tt(k)&&k.length===0)return _+"[]";for(let p=0;p<O.length;++p){let b=O[p],y=typeof b=="object"&&typeof b.value<"u"?b.value:k[b];if(o&&y===null)continue;let L=r&&d?b.replace(/\./g,"%2E"):b,C=tt(k)?typeof n=="function"?n(_,L):_:_+(r?"."+L:"["+L+"]");T.set(e,W);let N=new WeakMap;N.set(aa,T),Mi(f,fi(y,C,n,a,i,s,o,d,n==="comma"&&m&&tt(k)?null:l,c,U,r,S,u,h,m,I,N))}return f}function Bo(e=X){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||X.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=Qn;if(typeof e.format<"u"){if(!on(ta,e.format))throw new TypeError("Unknown format option provided.");n=e.format}let a=ta[n],i=X.filter;(typeof e.filter=="function"||tt(e.filter))&&(i=e.filter);let s;if(e.arrayFormat&&e.arrayFormat in ki?s=e.arrayFormat:"indices"in e?s=e.indices?"indices":"repeat":s=X.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:X.allowDots:!!e.allowDots;return{addQueryPrefix:typeof e.addQueryPrefix=="boolean"?e.addQueryPrefix:X.addQueryPrefix,allowDots:o,allowEmptyArrays:typeof e.allowEmptyArrays=="boolean"?!!e.allowEmptyArrays:X.allowEmptyArrays,arrayFormat:s,charset:t,charsetSentinel:typeof e.charsetSentinel=="boolean"?e.charsetSentinel:X.charsetSentinel,commaRoundTrip:!!e.commaRoundTrip,delimiter:typeof e.delimiter>"u"?X.delimiter:e.delimiter,encode:typeof e.encode=="boolean"?e.encode:X.encode,encodeDotInKeys:typeof e.encodeDotInKeys=="boolean"?e.encodeDotInKeys:X.encodeDotInKeys,encoder:typeof e.encoder=="function"?e.encoder:X.encoder,encodeValuesOnly:typeof e.encodeValuesOnly=="boolean"?e.encodeValuesOnly:X.encodeValuesOnly,filter:i,format:n,formatter:a,serializeDate:typeof e.serializeDate=="function"?e.serializeDate:X.serializeDate,skipNulls:typeof e.skipNulls=="boolean"?e.skipNulls:X.skipNulls,sort:typeof e.sort=="function"?e.sort:null,strictNullHandling:typeof e.strictNullHandling=="boolean"?e.strictNullHandling:X.strictNullHandling}}function Ti(e,t={}){let n=e,a=Bo(t),i,s;typeof a.filter=="function"?(s=a.filter,n=s("",n)):tt(a.filter)&&(s=a.filter,i=s);let o=[];if(typeof n!="object"||n===null)return"";let d=ki[a.arrayFormat],l=d==="comma"&&a.commaRoundTrip;i||(i=Object.keys(n)),a.sort&&i.sort(a.sort);let c=new WeakMap;for(let S=0;S<i.length;++S){let u=i[S];a.skipNulls&&n[u]===null||Mi(o,fi(n[u],u,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,c))}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 Ni(e){return Ti(e,{arrayFormat:"comma"})}function Pi(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 vi;function Le(e){let t;return(vi??(t=new globalThis.TextEncoder,vi=t.encode.bind(t)))(e)}var bi;function ia(e){let t;return(bi??(t=new globalThis.TextDecoder,bi=t.decode.bind(t)))(e)}var dt,lt,It=class{constructor(){dt.set(this,void 0),lt.set(this,void 0),Q(this,dt,new Uint8Array,"f"),Q(this,lt,null,"f")}decode(t){if(t==null)return[];let n=t instanceof ArrayBuffer?new Uint8Array(t):typeof t=="string"?Le(t):t;Q(this,dt,Pi([V(this,dt,"f"),n]),"f");let a=[],i;for(;(i=Do(V(this,dt,"f"),V(this,lt,"f")))!=null;){if(i.carriage&&V(this,lt,"f")==null){Q(this,lt,i.index,"f");continue}if(V(this,lt,"f")!=null&&(i.index!==V(this,lt,"f")+1||i.carriage)){a.push(ia(V(this,dt,"f").subarray(0,V(this,lt,"f")-1))),Q(this,dt,V(this,dt,"f").subarray(V(this,lt,"f")),"f"),Q(this,lt,null,"f");continue}let s=V(this,lt,"f")!==null?i.preceding-1:i.preceding,o=ia(V(this,dt,"f").subarray(0,s));a.push(o),Q(this,dt,V(this,dt,"f").subarray(i.index),"f"),Q(this,lt,null,"f")}return a}flush(){return V(this,dt,"f").length?this.decode(`
|
|
2
|
-
`):[]}};dt=new WeakMap,lt=new WeakMap;
|
|
3
|
-
`,"\r"]);
|
|
4
|
-
`);a.enqueue(o)}catch(i){a.error(i)}},async cancel(){await n.return?.()}})}};async function*
|
|
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]=
|
|
1
|
+
"use strict";var ko=Object.create;var an=Object.defineProperty;var To=Object.getOwnPropertyDescriptor;var Mo=Object.getOwnPropertyNames;var fo=Object.getPrototypeOf,No=Object.prototype.hasOwnProperty;var vo=(e,t)=>{for(var n in t)an(e,n,{get:t[n],enumerable:!0})},ei=(e,t,n,a)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of Mo(t))!No.call(e,i)&&i!==n&&an(e,i,{get:()=>t[i],enumerable:!(a=To(t,i))||a.enumerable});return e};var sn=(e,t,n)=>(n=e!=null?ko(fo(e)):{},ei(t||!e||!e.__esModule?an(n,"default",{value:e,enumerable:!0}):n,e)),bo=e=>ei(an({},"__esModule",{value:!0}),e);var Pl={};vo(Pl,{VideoGenerator:()=>Fn,buildClipPrompts:()=>Wn,harvest:()=>oo});module.exports=bo(Pl);var w=require("zod"),Kn="none",_o="none",ni=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(Kn),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(1e3).default(100),headless:w.z.boolean().default(!1),profileDir:w.z.string().optional(),proxy:w.z.string().url().optional(),kernelApiKey:w.z.string().optional(),kernelProxyId:w.z.string().optional(),kernelProfileName:w.z.string().optional(),kernelProfileSaveChanges:w.z.boolean().optional(),kernelStealth:w.z.boolean().optional(),serpIdentity:w.z.string().regex(/^[a-z0-9][a-z0-9_-]{0,63}$/).optional(),kernelProxyResolution: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 Ie(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 Ll=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),kernelApiKey:w.z.string().optional(),kernelProxyId: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(_o),proxyZip:w.z.string().regex(/^\d{5}$/).optional(),serpRedirect:w.z.boolean().optional(),forceDirectEgress:w.z.boolean().optional(),debug:w.z.boolean().default(!1),kernelApiKey:w.z.string().optional(),kernelProxyId:w.z.string().optional(),kernelProxyResolution:w.z.unknown().optional(),headless:w.z.boolean().default(!0)}),ai=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([])}),Ol=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()}),Wl=w.z.object({day:w.z.string(),hours:w.z.string()}),Rl=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()}))}),Bl=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()}),Hl=w.z.object({section:w.z.string(),attribute:w.z.string()});function ii(){return(process.env.BROWSER_SERVICE_API_KEY??process.env.KERNEL_API_KEY)?.trim()||void 0}var vn=require("playwright-extra"),Ss=sn(require("puppeteer-extra-plugin-stealth"),1),Us=require("playwright");function Q(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 V(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 Jn=function(){let{crypto:e}=globalThis;if(e?.randomUUID)return Jn=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 Ae(e){return typeof e=="object"&&e!==null&&("name"in e&&e.name==="AbortError"||"message"in e&&String(e.message).includes("FetchRequestCanceledException"))}var Le=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{},Z=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 Ct({message:a,cause:Le(n)});let s=n;return t===400?new ee(t,s,a,i):t===401?new ne(t,s,a,i):t===403?new ae(t,s,a,i):t===404?new ie(t,s,a,i):t===409?new se(t,s,a,i):t===422?new oe(t,s,a,i):t===429?new re(t,s,a,i):t>=500?new de(t,s,a,i):new e(t,s,a,i)}},nt=class extends Z{constructor({message:t}={}){super(void 0,void 0,t||"Request was aborted.",void 0)}},Ct=class extends Z{constructor({message:t,cause:n}){super(void 0,void 0,t||"Connection error.",void 0),n&&(this.cause=n)}},ht=class extends Ct{constructor({message:t}={}){super({message:t??"Request timed out."})}},ee=class extends Z{},ne=class extends Z{},ae=class extends Z{},ie=class extends Z{},se=class extends Z{},oe=class extends Z{},re=class extends Z{},de=class extends Z{};var Po=/^[a-z][a-z0-9+.-]*:/i,si=e=>Po.test(e),tt=e=>(tt=Array.isArray,tt(e)),qn=tt;function Xn(e){return typeof e!="object"?{}:e??{}}function jn(e){if(!e)return!0;for(let t in e)return!1;return!0}function oi(e,t){return Object.prototype.hasOwnProperty.call(e,t)}var ri=(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},Io=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 Ao=e=>typeof e=="boolean"?e:typeof e=="string"?e==="true":!!e,di=e=>{if(e!=null)return Io(e)};var zn=e=>{if(e!=null)return Ao(e)},li=e=>{try{return JSON.parse(e)}catch{return}};var Si=e=>new Promise(t=>setTimeout(t,e));var kt="0.93.0";function Lo(){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 xo=()=>{let e=Lo();if(e==="deno")return{"X-Stainless-Lang":"js","X-Stainless-Package-Version":kt,"X-Stainless-OS":ui(Deno.build.os),"X-Stainless-Arch":Ui(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":kt,"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":kt,"X-Stainless-OS":ui(globalThis.process.platform??"unknown"),"X-Stainless-Arch":Ui(globalThis.process.arch??"unknown"),"X-Stainless-Runtime":"node","X-Stainless-Runtime-Version":globalThis.process.version??"unknown"};let t=Oo();return t?{"X-Stainless-Lang":"js","X-Stainless-Package-Version":kt,"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":kt,"X-Stainless-OS":"Unknown","X-Stainless-Arch":"unknown","X-Stainless-Runtime":"unknown","X-Stainless-Runtime-Version":"unknown"}};function Oo(){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 Ui=e=>e==="x32"?"x32":e==="x86_64"||e==="x64"?"x64":e==="arm"?"arm":e==="aarch64"||e==="arm64"?"arm64":e?`other:${e}`:"unknown",ui=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"),ci,hi=()=>ci??(ci=xo());function gi(){if(typeof fetch<"u")return fetch;throw new Error("`fetch` is not defined as a global; Either pass `fetch` to the client, `new Kernel({ fetch })` or polyfill the global, `globalThis.fetch = fetch`")}function $n(...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 on(e){let t=Symbol.asyncIterator in e?e[Symbol.asyncIterator]():e[Symbol.iterator]();return $n({start(){},async pull(n){let{done:a,value:i}=await t.next();a?n.close():n.enqueue(i)},async cancel(){await t.return?.()}})}function Qn(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 wi(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 pi=({headers:e,body:t})=>({bodyHeaders:{"content-type":"application/json"},body:JSON.stringify(t)});var Zn="RFC3986",ta=e=>String(e),ea={RFC1738:e=>String(e).replace(/%20/g,"+"),RFC3986:ta},mi="RFC1738";var rn=(e,t)=>(rn=Object.hasOwn??Function.prototype.call.bind(Object.prototype.hasOwnProperty),rn(e,t)),ct=(()=>{let e=[];for(let t=0;t<256;++t)e.push("%"+((t<16?"0":"")+t.toString(16)).toUpperCase());return e})();var na=1024,yi=(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+=na){let l=s.length>=na?s.slice(d,d+na):s,c=[];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===mi&&(r===40||r===41)){c[c.length]=l.charAt(U);continue}if(r<128){c[c.length]=ct[r];continue}if(r<2048){c[c.length]=ct[192|r>>6]+ct[128|r&63];continue}if(r<55296||r>=57344){c[c.length]=ct[224|r>>12]+ct[128|r>>6&63]+ct[128|r&63];continue}U+=1,r=65536+((r&1023)<<10|l.charCodeAt(U)&1023),c[c.length]=ct[240|r>>18]+ct[128|r>>12&63]+ct[128|r>>6&63]+ct[128|r&63]}o+=c.join("")}return o};function Ci(e){return!e||typeof e!="object"?!1:!!(e.constructor&&e.constructor.isBuffer&&e.constructor.isBuffer(e))}function aa(e,t){if(tt(e)){let n=[];for(let a=0;a<e.length;a+=1)n.push(t(e[a]));return n}return t(e)}var Ti={brackets(e){return String(e)+"[]"},comma:"comma",indices(e,t){return String(e)+"["+t+"]"},repeat(e){return String(e)}},Mi=function(e,t){Array.prototype.push.apply(e,tt(t)?t:[t])},ki,X={addQueryPrefix:!1,allowDots:!1,allowEmptyArrays:!1,arrayFormat:"indices",charset:"utf-8",charsetSentinel:!1,delimiter:"&",encode:!0,encodeDotInKeys:!1,encoder:yi,encodeValuesOnly:!1,format:Zn,formatter:ta,indices:!1,serializeDate(e){return(ki??(ki=Function.prototype.call.bind(Date.prototype.toISOString)))(e)},skipNulls:!1,strictNullHandling:!1};function Bo(e){return typeof e=="string"||typeof e=="number"||typeof e=="boolean"||typeof e=="symbol"||typeof e=="bigint"}var ia={};function fi(e,t,n,a,i,s,o,d,l,c,U,r,S,u,h,m,I,f){let k=e,_=f,W=0,E=!1;for(;(_=_.get(ia))!==void 0&&!E;){let p=_.get(e);if(W+=1,typeof p<"u"){if(p===W)throw new RangeError("Cyclic object value");E=!0}typeof _.get(ia)>"u"&&(W=0)}if(typeof c=="function"?k=c(t,k):k instanceof Date?k=S?.(k):n==="comma"&&tt(k)&&(k=aa(k,function(p){return p instanceof Date?S?.(p):p})),k===null){if(s)return l&&!m?l(t,X.encoder,I,"key",u):t;k=""}if(Bo(k)||Ci(k)){if(l){let p=m?t:l(t,X.encoder,I,"key",u);return[h?.(p)+"="+h?.(l(k,X.encoder,I,"value",u))]}return[h?.(t)+"="+h?.(String(k))]}let M=[];if(typeof k>"u")return M;let O;if(n==="comma"&&tt(k))m&&l&&(k=aa(k,l)),O=[{value:k.length>0?k.join(",")||null:void 0}];else if(tt(c))O=c;else{let p=Object.keys(k);O=U?p.sort(U):p}let A=d?String(t).replace(/\./g,"%2E"):String(t),P=a&&tt(k)&&k.length===1?A+"[]":A;if(i&&tt(k)&&k.length===0)return P+"[]";for(let p=0;p<O.length;++p){let b=O[p],y=typeof b=="object"&&typeof b.value<"u"?b.value:k[b];if(o&&y===null)continue;let L=r&&d?b.replace(/\./g,"%2E"):b,C=tt(k)?typeof n=="function"?n(P,L):P:P+(r?"."+L:"["+L+"]");f.set(e,W);let N=new WeakMap;N.set(ia,f),Mi(M,fi(y,C,n,a,i,s,o,d,n==="comma"&&m&&tt(k)?null:l,c,U,r,S,u,h,m,I,N))}return M}function Ho(e=X){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||X.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=Zn;if(typeof e.format<"u"){if(!rn(ea,e.format))throw new TypeError("Unknown format option provided.");n=e.format}let a=ea[n],i=X.filter;(typeof e.filter=="function"||tt(e.filter))&&(i=e.filter);let s;if(e.arrayFormat&&e.arrayFormat in Ti?s=e.arrayFormat:"indices"in e?s=e.indices?"indices":"repeat":s=X.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:X.allowDots:!!e.allowDots;return{addQueryPrefix:typeof e.addQueryPrefix=="boolean"?e.addQueryPrefix:X.addQueryPrefix,allowDots:o,allowEmptyArrays:typeof e.allowEmptyArrays=="boolean"?!!e.allowEmptyArrays:X.allowEmptyArrays,arrayFormat:s,charset:t,charsetSentinel:typeof e.charsetSentinel=="boolean"?e.charsetSentinel:X.charsetSentinel,commaRoundTrip:!!e.commaRoundTrip,delimiter:typeof e.delimiter>"u"?X.delimiter:e.delimiter,encode:typeof e.encode=="boolean"?e.encode:X.encode,encodeDotInKeys:typeof e.encodeDotInKeys=="boolean"?e.encodeDotInKeys:X.encodeDotInKeys,encoder:typeof e.encoder=="function"?e.encoder:X.encoder,encodeValuesOnly:typeof e.encodeValuesOnly=="boolean"?e.encodeValuesOnly:X.encodeValuesOnly,filter:i,format:n,formatter:a,serializeDate:typeof e.serializeDate=="function"?e.serializeDate:X.serializeDate,skipNulls:typeof e.skipNulls=="boolean"?e.skipNulls:X.skipNulls,sort:typeof e.sort=="function"?e.sort:null,strictNullHandling:typeof e.strictNullHandling=="boolean"?e.strictNullHandling:X.strictNullHandling}}function Ni(e,t={}){let n=e,a=Ho(t),i,s;typeof a.filter=="function"?(s=a.filter,n=s("",n)):tt(a.filter)&&(s=a.filter,i=s);let o=[];if(typeof n!="object"||n===null)return"";let d=Ti[a.arrayFormat],l=d==="comma"&&a.commaRoundTrip;i||(i=Object.keys(n)),a.sort&&i.sort(a.sort);let c=new WeakMap;for(let S=0;S<i.length;++S){let u=i[S];a.skipNulls&&n[u]===null||Mi(o,fi(n[u],u,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,c))}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 vi(e){return Ni(e,{arrayFormat:"comma"})}function Ei(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 bi;function xe(e){let t;return(bi??(t=new globalThis.TextEncoder,bi=t.encode.bind(t)))(e)}var _i;function sa(e){let t;return(_i??(t=new globalThis.TextDecoder,_i=t.decode.bind(t)))(e)}var dt,lt,At=class{constructor(){dt.set(this,void 0),lt.set(this,void 0),Q(this,dt,new Uint8Array,"f"),Q(this,lt,null,"f")}decode(t){if(t==null)return[];let n=t instanceof ArrayBuffer?new Uint8Array(t):typeof t=="string"?xe(t):t;Q(this,dt,Ei([V(this,dt,"f"),n]),"f");let a=[],i;for(;(i=Fo(V(this,dt,"f"),V(this,lt,"f")))!=null;){if(i.carriage&&V(this,lt,"f")==null){Q(this,lt,i.index,"f");continue}if(V(this,lt,"f")!=null&&(i.index!==V(this,lt,"f")+1||i.carriage)){a.push(sa(V(this,dt,"f").subarray(0,V(this,lt,"f")-1))),Q(this,dt,V(this,dt,"f").subarray(V(this,lt,"f")),"f"),Q(this,lt,null,"f");continue}let s=V(this,lt,"f")!==null?i.preceding-1:i.preceding,o=sa(V(this,dt,"f").subarray(0,s));a.push(o),Q(this,dt,V(this,dt,"f").subarray(i.index),"f"),Q(this,lt,null,"f")}return a}flush(){return V(this,dt,"f").length?this.decode(`
|
|
2
|
+
`):[]}};dt=new WeakMap,lt=new WeakMap;At.NEWLINE_CHARS=new Set([`
|
|
3
|
+
`,"\r"]);At.NEWLINE_REGEXP=/\r\n|[\n\r]/g;function Fo(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 Pi(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 ln={off:0,error:200,warn:300,info:400,debug:500},oa=(e,t,n)=>{if(e){if(oi(ln,e))return e;j(n).warn(`${t} was set to ${JSON.stringify(e)}, expected one of ${JSON.stringify(Object.keys(ln))}`)}};function Oe(){}function dn(e,t,n){return!t||ln[e]>ln[n]?Oe:t[e].bind(t)}var Go={error:Oe,warn:Oe,info:Oe,debug:Oe},Ii=new WeakMap;function j(e){let t=e.logger,n=e.logLevel??"off";if(!t)return Go;let a=Ii.get(t);if(a&&a[0]===n)return a[1];let i={error:dn("error",t,n),warn:dn("warn",t,n),info:dn("info",t,n),debug:dn("debug",t,n)};return Ii.set(t,[n,i]),i}var gt=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 We,Sn=class e{constructor(t,n,a){this.iterator=t,We.set(this,void 0),this.controller=n,Q(this,We,a,"f")}static fromSSEResponse(t,n,a){let i=!1,s=a?j(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 Vo(t,n))try{yield JSON.parse(l.data)}catch(c){throw s.error("Could not parse message into JSON:",l.data),s.error("From chunk:",l.raw),c}d=!0}catch(l){if(Ae(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 At,l=Qn(t);for await(let c of l)for(let U of d.decode(c))yield U;for(let c of d.flush())yield c}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(Ae(l))return;throw l}finally{d||n.abort()}}return new e(o,n,a)}[(We=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,V(this,We,"f")),new e(()=>i(n),this.controller,V(this,We,"f"))]}toReadableStream(){let t=this,n;return $n({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=xe(JSON.stringify(i)+`
|
|
4
|
+
`);a.enqueue(o)}catch(i){a.error(i)}},async cancel(){await n.return?.()}})}};async function*Vo(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 ra,a=new At,i=Qn(e.body);for await(let s of Yo(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*Yo(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"?xe(n):n,i=new Uint8Array(t.length+a.length);i.set(t),i.set(a,t.length),t=i;let s;for(;(s=Pi(t))!==-1;)yield t.slice(0,s),t=t.slice(s)}t.length>0&&(yield t)}var ra=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]=Ko(t,":");return i.startsWith(" ")&&(i=i.substring(1)),n==="event"?this.event=i:n==="data"&&this.data.push(i),null}};function Ko(e,t){let n=e.indexOf(t);return n!==-1?[e.substring(0,n),t,e.substring(n+t.length)]:[e,"",""]}async function Un(e,t){let{response:n,requestLogID:a,retryOfRequestLogID:i,startTime:s}=t,o=await(async()=>{if(t.options.stream)return j(e).debug("response",n.status,n.url,n.headers,n.body),t.options.__streamClass?t.options.__streamClass.fromSSEResponse(n,t.controller,e):Sn.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 j(e).debug(`[${a}] response parsed`,gt({retryOfRequestLogID:i,url:n.url,status:n.status,body:o,durationMs:Date.now()-s})),o}var Re,Lt=class e extends Promise{constructor(t,n,a=Un){super(i=>{i(null)}),this.responsePromise=n,this.parseResponse=a,Re.set(this,void 0),Q(this,Re,t,"f")}_thenUnwrap(t){return new e(V(this,Re,"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(V(this,Re,"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)}};Re=new WeakMap;var un,cn=class{constructor(t,n,a,i){un.set(this,void 0),Q(this,un,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 V(this,un,"f").requestAPIList(this.constructor,t)}async*iterPages(){let t=this;for(yield t;t.hasNextPage();)t=await t.getNextPage(),yield t}async*[(un=new WeakMap,Symbol.asyncIterator)](){for await(let t of this.iterPages())for(let n of t.getPaginatedItems())yield n}},Be=class extends Lt{constructor(t,n,a){super(t,n,async(i,s)=>new a(i,s.response,await Un(i,s),s.options))}async*[Symbol.asyncIterator](){let t=await this;for await(let n of t)yield n}},hn=class extends cn{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=zn(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:{...Xn(this.options.query),page_token:t}}:null}},D=class extends cn{constructor(t,n,a,i){super(t,n,a,i),this.items=a||[],this.has_more=zn(this.response.headers.get("x-has-more"))??null,this.next_offset=di(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:{...Xn(this.options.query),offset:t}}}};var Sa=()=>{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 le(e,t,n){return Sa(),new File(e,t??"unknown_file",n)}function He(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 gn=e=>e!=null&&typeof e=="object"&&typeof e[Symbol.asyncIterator]=="function";var wt=async(e,t)=>({...e,body:await Xo(e.body,t)}),Ai=new WeakMap;function qo(e){let t=typeof e=="function"?e:e.fetch,n=Ai.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 Ai.set(t,a),a}var Xo=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])=>la(n,a,i))),n},Li=e=>e instanceof Blob&&"name"in e,jo=e=>typeof e=="object"&&e!==null&&(e instanceof Response||gn(e)||Li(e)),da=e=>{if(jo(e))return!0;if(Array.isArray(e))return e.some(da);if(e&&typeof e=="object"){for(let t in e)if(da(e[t]))return!0}return!1},la=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,le([await n.blob()],He(n)));else if(gn(n))e.append(t,le([await new Response(on(n)).blob()],He(n)));else if(Li(n))e.append(t,n,He(n));else if(Array.isArray(n))await Promise.all(n.map((a,i)=>la(e,`${t}[${i}]`,a)));else if(typeof n=="object")!(t==="env_vars")&&!da(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])=>la(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 xi=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",zo=e=>e!=null&&typeof e=="object"&&typeof e.name=="string"&&typeof e.lastModified=="number"&&xi(e),$o=e=>e!=null&&typeof e=="object"&&typeof e.url=="string"&&typeof e.blob=="function";async function wn(e,t,n){if(Sa(),e=await e,zo(e))return e instanceof File?e:le([await e.arrayBuffer()],e.name);if($o(e)){let i=await e.blob();return t||(t=new URL(e.url).pathname.split(/[\\/]/).pop()),le(await Ua(i),t,n)}let a=await Ua(e);if(t||(t=He(e)),!n?.type){let i=a.find(s=>typeof s=="object"&&"type"in s&&s.type);typeof i=="string"&&(n={...n,type:i})}return le(a,t,n)}async function Ua(e){let t=[];if(typeof e=="string"||ArrayBuffer.isView(e)||e instanceof ArrayBuffer)t.push(e);else if(xi(e))t.push(e instanceof Blob?e:await e.arrayBuffer());else if(gn(e))for await(let n of e)t.push(...await Ua(n));else{let n=e?.constructor?.name;throw new Error(`Unexpected data type: ${typeof e}${n?`; constructor: ${n}`:""}${Qo(e)}`)}return t}function Qo(e){return typeof e!="object"||e===null?"":`; props: [${Object.getOwnPropertyNames(e).map(n=>`"${n}"`).join(", ")}]`}var v=class{constructor(t){this._client=t}};var Oi=Symbol("brand.privateNullableHeaders");function*tr(e){if(!e)return;if(Oi 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 tr(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{[Oi]:!0,values:t,nulls:n}};function Ri(e){return e.replace(/[^A-Za-z0-9\-._~!$&'()*+,;=:@]+/g,encodeURIComponent)}var Wi=Object.freeze(Object.create(null)),er=(e=Ri)=>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 u=a[S],h=(i?encodeURIComponent:e)(""+u);return S!==a.length&&(u==null||typeof u=="object"&&u.toString===Object.getPrototypeOf(Object.getPrototypeOf(u.hasOwnProperty??Wi)??Wi)?.toString)&&(h=u+"",s.push({start:U.length+r.length,length:h.length,error:`Value of type ${Object.prototype.toString.call(u).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,c;for(;(c=l.exec(d))!==null;)s.push({start:c.index,length:c[0].length,error:`Value "${c[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,u)=>{let h=" ".repeat(u.start-U),m="^".repeat(u.length);return U=u.start+u.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=tr(Wi);var Lt=class extends v{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",D,{query:t,...n})}delete(t,n){return this._client.delete(g`/org/api_keys/${t}`,{...n,headers:M([{Accept:"*/*"},n?.headers])})}rotate(t,n={},a){return this._client.post(g`/org/api_keys/${t}/rotate`,{body:n,...a})}};var xt=class extends v{list(t={},n){return this._client.getAPIList("/apps",D,{query:t,...n})}};var le=class extends v{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",D,{query:t,...n})}delete(t,n){return this._client.delete(g`/audit-logs/export/destinations/${t}`,{...n,headers:M([{Accept:"*/*"},n?.headers])})}test(t,n){return this._client.post(g`/audit-logs/export/destinations/${t}/test`,n)}};var nr=6,ar=5e4,ir=8e3,St=class extends G{};async function Bi(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=nr,...d}=i;if(!Number.isInteger(o)||o<0)throw new TypeError("maxTransferRetries must be a non-negative integer");let l=d.timeout??a,c,U={bytesWritten:0,chunks:0,rows:0},r=new Set;for(;;){let S=await sr(e,c?{...t,cursor:c}:t,d,o,l),{nextCursor:u,hasMore:h,rows:m}=or(S.headers,c);if(h&&u){if(r.has(u))throw new St("response repeated X-Next-Cursor header");r.add(u)}if(await lr(n,S.body),c=u,U.bytesWritten+=S.body.byteLength,U.chunks+=1,U.rows+=m,s&&await s({...U,chunkRows:m}),!h)return U}}async function sr(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 c=!1,U=setTimeout(()=>{c=!0,o.abort()},i);try{let r=new Uint8Array(await l.arrayBuffer());if(clearTimeout(U),n.signal?.aborted)throw new nt;if(c)throw new ht;let S=l.headers.get("x-content-sha256");if(!S)throw new St("response missing X-Content-Sha256 header");let u=await rr(r);if(n.signal?.aborted)throw new nt;if(u!==S)throw new St(`audit log chunk checksum mismatch (got ${u}, want ${S})`);return{body:r,headers:l.headers}}catch(r){if(n.signal?.aborted&&!(r instanceof nt)?r=new nt:c&&(r=new ht),s===a||r instanceof nt)throw r;await dr(s+1,n.signal)}finally{clearTimeout(U),n.signal?.removeEventListener("abort",d)}}}function or(e,t){let n=e.get("x-has-more");if(n!=="true"&&n!=="false")throw new St("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 St("response missing or invalid X-Row-Count header");let s=Number(i);if(!Number.isSafeInteger(s)||s>ar)throw new St("response missing or invalid X-Row-Count header");let o=e.get("x-next-cursor")||void 0;if(a&&(!o||o===t))throw new St("response has invalid X-Next-Cursor header");if(!a&&o)throw new St("response returned a cursor after the final chunk");return{rows:s,nextCursor:o,hasMore:a}}async function rr(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 dr(e,t){let n=Math.min(1e3*2**(e-1),ir);await new Promise((a,i)=>{if(t?.aborted){i(new nt);return}let s=()=>{clearTimeout(o),i(new nt)},o=setTimeout(()=>{t?.removeEventListener("abort",s),a()},n);t?.addEventListener("abort",s,{once:!0})})}async function lr(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+=Ri(a,t.byteLength-n);continue}if(a&&typeof a=="object"&&"bytesWritten"in a){let i=a.bytesWritten;n+=Ri(i,t.byteLength-n);continue}return}}function Ri(e,t){if(!Number.isSafeInteger(e)||e<=0||e>t)throw new St("audit log download destination performed a short write");return e}var Mt=class extends v{constructor(){super(...arguments),this.exportDestinations=new le(this._client)}list(t,n){return this._client.getAPIList("/audit-logs",cn,{query:t,...n})}exportChunk(t,n){return this._client.get("/audit-logs/export/chunk",{query:t,...n,headers:M([{Accept:"application/octet-stream"},n?.headers]),__binaryResponse:!0})}download(t,n,a){return Bi((i,s)=>this.exportChunk(i,s),t,n,this._client.timeout,a)}};Mt.ExportDestinations=le;var Se=class extends v{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",D,{query:t,...n})}delete(t,n){return this._client.delete(g`/auth/connections/${t}`,{...n,headers:M([{Accept:"*/*"},n?.headers])})}follow(t,n){return this._client.get(g`/auth/connections/${t}/events`,{...n,headers:M([{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`,D,{query:n,...a})}};var Ue=class extends v{retrieve(t){return this._client.get("/auth/context",t)}};var pt=class extends v{constructor(){super(...arguments),this.context=new Ue(this._client),this.connections=new Se(this._client)}};pt.Context=Ue;pt.Connections=Se;var Ot=class extends v{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",D,{query:t,...n})}delete(t,n={},a){return this._client.delete(g`/browser_pools/${t}`,{body:n,...a,headers:M([{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:M([{Accept:"*/*"},n?.headers])})}release(t,n,a){return this._client.post(g`/browser_pools/${t}/release`,{body:n,...a,headers:M([{Accept:"*/*"},a?.headers])})}};var ue=class extends v{batch(t,n,a){return this._client.post(g`/browsers/${t}/computer/batch`,{body:n,...a,headers:M([{Accept:"*/*"},a?.headers])})}captureScreenshot(t,n={},a){return this._client.post(g`/browsers/${t}/computer/screenshot`,{body:n,...a,headers:M([{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:M([{Accept:"*/*"},a?.headers])})}dragMouse(t,n,a){return this._client.post(g`/browsers/${t}/computer/drag_mouse`,{body:n,...a,headers:M([{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:M([{Accept:"*/*"},a?.headers])})}pressKey(t,n,a){return this._client.post(g`/browsers/${t}/computer/press_key`,{body:n,...a,headers:M([{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:M([{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:M([{Accept:"*/*"},a?.headers])})}writeClipboard(t,n,a){return this._client.post(g`/browsers/${t}/computer/clipboard/write`,{body:n,...a,headers:M([{Accept:"*/*"},a?.headers])})}};var ce=class extends v{stream(t,n,a){return this._client.get(g`/browsers/${t}/logs/stream`,{query:n,...a,headers:M([{Accept:"text/event-stream"},a?.headers]),stream:!0})}};var he=class extends v{execute(t,n,a){return this._client.post(g`/browsers/${t}/playwright/execute`,{body:n,...a})}};var ge=class extends v{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:M([{Accept:"text/event-stream"},a?.headers]),stream:!0})}};var we=class extends v{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:M([{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:M([{Accept:"*/*"},a?.headers])})}};var pe=class extends v{events(t,n={},a){return this._client.getAPIList(g`/browsers/${t}/telemetry/events`,D,{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:M([{Accept:"text/event-stream",...i!=null?{"Last-Event-ID":i}:void 0},a?.headers]),stream:!0})}};var me=class extends v{events(t,n,a){let{id:i}=n;return this._client.get(g`/browsers/${i}/fs/watch/${t}/events`,{...a,headers:M([{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:M([{Accept:"*/*"},a?.headers])})}};var Wt=class extends v{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:M([{Accept:"*/*"},a?.headers])})}deleteDirectory(t,n,a){return this._client.put(g`/browsers/${t}/fs/delete_directory`,{body:n,...a,headers:M([{Accept:"*/*"},a?.headers])})}deleteFile(t,n,a){return this._client.put(g`/browsers/${t}/fs/delete_file`,{body:n,...a,headers:M([{Accept:"*/*"},a?.headers])})}downloadDirZip(t,n,a){return this._client.get(g`/browsers/${t}/fs/download_dir_zip`,{query:n,...a,headers:M([{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:M([{Accept:"*/*"},a?.headers])})}readFile(t,n,a){return this._client.get(g`/browsers/${t}/fs/read_file`,{query:n,...a,headers:M([{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:M([{Accept:"*/*"},a?.headers])})}upload(t,n,a){return this._client.post(g`/browsers/${t}/fs/upload`,wt({body:n,...a,headers:M([{Accept:"*/*"},a?.headers])},this._client))}uploadZip(t,n,a){return this._client.post(g`/browsers/${t}/fs/upload_zip`,wt({body:n,...a,headers:M([{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:M([{"Content-Type":"application/octet-stream",Accept:"*/*"},i?.headers])})}};Wt.Watch=me;function wn(e,t){return`${e.replace(/\/+$/,"")}${t.startsWith("/")?t:`/${t}`}`}async function Hi(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:c,duplex:U,timeout_ms:r}=kr(n,a);Mr(s);let S={url:s,jwt:i.jwt};r!==void 0&&(S.timeout_ms=r);let u=d.get("accept"),h={method:Cr(o),path:wn(i.baseURL,"/curl/raw"),query:S,body:l,headers:M([{Authorization:null},u?{Accept:u}:{Accept:"*/*"},fr(d)]),signal:c??null,__binaryResponse:!0};return U&&(h.fetchOptions={duplex:U}),e.request(h).asResponse()}function Cr(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 kr(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((c,U)=>{i.set(U,c)});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 Mr(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 fr(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 ot=class extends v{constructor(){super(...arguments),this.telemetry=new pe(this._client),this.replays=new we(this._client),this.fs=new Wt(this._client),this.process=new ge(this._client),this.logs=new ce(this._client),this.computer=new ue(this._client),this.playwright=new he(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",D,{query:t,...n})}curl(t,n,a){return this._client.post(g`/browsers/${t}/curl`,{body:n,...a})}fetch(t,n,a){return Hi(this._client,t,n,a)}deleteByID(t,n){return this._client.delete(g`/browsers/${t}`,{...n,headers:M([{Accept:"*/*"},n?.headers])})}loadExtensions(t,n,a){return this._client.post(g`/browsers/${t}/extensions`,wt({body:n,...a,headers:M([{Accept:"*/*"},a?.headers])},this._client))}};ot.Telemetry=pe;ot.Replays=we;ot.Fs=Wt;ot.Process=ge;ot.Logs=ce;ot.Computer=ue;ot.Playwright=he;var Rt=class extends v{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",D,{query:t,...n})}delete(t,n){return this._client.delete(g`/org/credential_providers/${t}`,{...n,headers:M([{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 Bt=class extends v{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",D,{query:t,...n})}delete(t,n){return this._client.delete(g`/credentials/${t}`,{...n,headers:M([{Accept:"*/*"},n?.headers])})}totpCode(t,n){return this._client.get(g`/credentials/${t}/totp-code`,n)}};var Ht=class extends v{create(t,n){return this._client.post("/deployments",wt({body:t,...n},this._client))}retrieve(t,n){return this._client.get(g`/deployments/${t}`,n)}list(t={},n){return this._client.getAPIList("/deployments",D,{query:t,...n})}delete(t,n){return this._client.delete(g`/deployments/${t}`,{...n,headers:M([{Accept:"*/*"},n?.headers])})}follow(t,n={},a){return this._client.get(g`/deployments/${t}/events`,{query:n,...a,headers:M([{Accept:"text/event-stream"},a?.headers]),stream:!0})}};var Dt=class extends v{list(t={},n){return this._client.getAPIList("/extensions",D,{query:t,...n})}delete(t,n){return this._client.delete(g`/extensions/${t}`,{...n,headers:M([{Accept:"*/*"},n?.headers])})}download(t,n){return this._client.get(g`/extensions/${t}`,{...n,headers:M([{Accept:"application/octet-stream"},n?.headers]),__binaryResponse:!0})}downloadFromChromeStore(t,n){return this._client.get("/extensions/from_chrome_store",{query:t,...n,headers:M([{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",wt({body:t,...n},this._client))}};var Ft=class extends v{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",D,{query:t,...n})}deleteBrowsers(t,n){return this._client.delete(g`/invocations/${t}/browsers`,{...n,headers:M([{Accept:"*/*"},n?.headers])})}follow(t,n={},a){return this._client.get(g`/invocations/${t}/events`,{query:n,...a,headers:M([{Accept:"text/event-stream"},a?.headers]),stream:!0})}listBrowsers(t,n){return this._client.get(g`/invocations/${t}/browsers`,n)}};var ye=class extends v{retrieve(t){return this._client.get("/org/entitlements",t)}};var Ce=class extends v{retrieve(t){return this._client.get("/org/limits",t)}update(t,n){return this._client.patch("/org/limits",{body:t,...n})}};var mt=class extends v{constructor(){super(...arguments),this.entitlements=new ye(this._client),this.limits=new Ce(this._client)}};mt.Entitlements=ye;mt.Limits=Ce;var Gt=class extends v{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",D,{query:t,...n})}delete(t,n){return this._client.delete(g`/profiles/${t}`,{...n,headers:M([{Accept:"*/*"},n?.headers])})}download(t,n={},a){return this._client.get(g`/profiles/${t}/download`,{query:n,...a,headers:M([{Accept:"application/octet-stream"},a?.headers]),__binaryResponse:!0})}};var ke=class extends v{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 ft=class extends v{constructor(){super(...arguments),this.limits=new ke(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",D,{query:t,...n})}delete(t,n){return this._client.delete(g`/org/projects/${t}`,{...n,headers:M([{Accept:"*/*"},n?.headers])})}};ft.Limits=ke;var Vt=class extends v{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",D,{query:t,...n})}delete(t,n){return this._client.delete(g`/proxies/${t}`,{...n,headers:M([{Accept:"*/*"},n?.headers])})}check(t,n={},a){return this._client.post(g`/proxies/${t}/check`,{body:n,...a})}};var Me=class extends v{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",D,{query:t,...n})}delete(t,n){return this._client.delete(g`/telemetry/destinations/${t}`,{...n,headers:M([{Accept:"*/*"},n?.headers])})}};var Tt=class extends v{constructor(){super(...arguments),this.destinations=new Me(this._client)}};Tt.Destinations=Me;var He=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()}},Di="\u004b\u0045\u0052\u004e\u0045\u004c_BROWSER_ROUTING_SUBRESOURCES",Er=["curl","telemetry/stream"],_r=/^\/(?:v\d+\/)?browsers(?:\/[^/]+)?\/?$/,Ir=/^\/(?:v\d+\/)?browser_pools\/[^/]+\/acquire\/?$/,Ar=/^\/(?:v\d+\/)?browsers\/([^/]+)\/?$/,Lr=/^\/(?:v\d+\/)?browser_pools\/[^/]+\/release\/?$/;function ua(){let e=Jr();return e===void 0?[...Er]:e.trim()===""?[]:e.split(",").map(t=>t.trim()).filter(Boolean)}function ca(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),c=xr(l,s),U=await Fr(e,{input:o,init:d,request:l},s,i,a);return c&&await Hr(U,a),await Or(l,U,s,a),U}}function xr(e,t){let n=new URL(e.url);return n.origin===t&&(_r.test(n.pathname)||Ir.test(n.pathname))}async function Or(e,t,n,a){if(!t.ok)return;let i=new URL(e.url);if(i.origin!==n)return;let s=Wr(e,i.pathname)??await Rr(e,i.pathname);s&&a.delete(s)}function Wr(e,t){if(e.method.toUpperCase()!=="DELETE")return;let n=t.match(Ar);return n&&decodeURIComponent(n[1]??"")||void 0}async function Rr(e,t){if(!(e.method.toUpperCase()!=="POST"||!Lr.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 Br(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||Kr(s)||"";if(o)return{sessionId:n,baseURL:a,jwt:o}}async function Hr(e,t){if((e.headers.get("content-type")?.toLowerCase()??"").includes("application/json"))try{Ua(await e.clone().json(),t)}catch{}}function Ua(e,t){let n=Br(e);if(n&&t.set(n),Array.isArray(e)){for(let a of e)Ua(a,t);return}if(!(!e||typeof e!="object"))for(let a of Object.values(e))typeof a=="object"&&a!==null&&Ua(a,t)}function Dr(e,t){let n=e.replace(/^\/+|\/+$/g,"");return t.some(a=>n===a||n.startsWith(a+"/"))}async function Fr(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 c=decodeURIComponent(l[1]??""),U=l[2]??"";if(!c||!Dr(U+(l[3]??""),s))return e(t,n);let r=o.get(c);if(r===void 0)return e(t,n);let S=new URL(wn(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 u=new Headers(a.headers);return u.delete("authorization"),e(S.toString(),Gr(t,a,n,u))}function Gr(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=Vr(t,n);o!==void 0&&(s.body=o),n?.duplex!==void 0?s.duplex=n.duplex:Yr(o)&&(s.duplex="half")}return s}function Vr(e,t){return t?.body!==void 0&&t.body!==null?t.body:e.body??void 0}function Yr(e){return globalThis.ReadableStream&&e instanceof globalThis.ReadableStream||typeof e=="object"&&e!==null&&Symbol.asyncIterator in e}function Kr(e){if(e)try{return new URL(e).searchParams.get("jwt")??void 0}catch{return}}function Jr(){if(typeof globalThis.process<"u"){let e=globalThis.process.env?.[Di];return typeof e=="string"?e:void 0}if(typeof globalThis.Deno<"u"){let e=globalThis.Deno.env?.get?.(Di);return typeof e=="string"?e:void 0}}var pn=class{constructor(t){this.actions=new Map,this.name=t,Fi.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}))}}},ha=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)}},Fi=new ha;var fe=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 ga,wa,De,Gi,Vi={production:"https://api.on\u006b\u0065\u0072\u006e\u0065\u006c.com/",development:"https://localhost:3001/"},B=class{constructor({baseURL:t=fe("\u004b\u0045\u0052\u004e\u0045\u004c_BASE_URL"),apiKey:n=fe("\u004b\u0045\u0052\u004e\u0045\u004c_API_KEY"),projectID:a=null,project:i=null,...s}={}){if(ga.add(this),De.set(this,void 0),this.deployments=new Ht(this),this.apps=new xt(this),this.invocations=new Ft(this),this.browsers=new ot(this),this.profiles=new Gt(this),this.auth=new pt(this),this.telemetry=new Tt(this),this.proxies=new Vt(this),this.extensions=new Dt(this),this.browserPools=new Ot(this),this.credentials=new Bt(this),this.projects=new ft(this),this.organization=new mt(this),this.auditLogs=new Mt(this),this.apiKeys=new Lt(this),this.credentialProviders=new Rt(this),typeof globalThis<"u"&&typeof globalThis.Bun<"u"&&globalThis.Bun.version&&!fe("\u004b\u0045\u0052\u004e\u0045\u004c_SUPPRESS_BUN_WARNING")&&j(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||Vi[o.environment||"production"],this.timeout=o.timeout??wa.DEFAULT_TIMEOUT,this.logger=o.logger??console;let d="warn";this.logLevel=d,this.logLevel=sa(o.logLevel,"ClientOptions.logLevel",this)??sa(fe("\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??hi(),this.browserRouteCache=new He,this.fetch=ca(this.rawFetch,{apiBaseURL:this.baseURL,subresources:ua(),cache:this.browserRouteCache}),Q(this,De,wi,"f");let l=fe("\u004b\u0045\u0052\u004e\u0045\u004c_CUSTOM_HEADERS");if(l){let c={};for(let U of l.split(`
|
|
10
|
-
`)){let r=U.indexOf(":");r>=0&&(c[U.substring(0,r).trim()]=U.substring(r+1).trim())}o.defaultHeaders={...c,...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=ca(n.rawFetch,{apiBaseURL:n.baseURL,subresources:ua(),cache:n.browserRouteCache}),n}defaultQuery(){return this._options.defaultQuery}validateHeaders({values:t,nulls:n}){}async authHeaders(t){return M([{Authorization:`Bearer ${this.apiKey}`}])}stringifyQuery(t){return Ni(t)}getUserAgent(){return`${this.constructor.name}/JS ${kt}`}defaultIdempotencyKey(){return`stainless-node-retry-${Kn()}`}makeStatusError(t,n,a,i){return Z.generate(t,n,a,i)}buildURL(t,n,a){let i=!V(this,ga,"m",Gi).call(this)&&a||this.baseURL,s=ii(t)?new URL(t):new URL(i+(i.endsWith("/")&&t.startsWith("/")?t.slice(1):t)),o=this.defaultQuery(),d=Object.fromEntries(s.searchParams);return(!Xn(o)||!Xn(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 At(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 c="log_"+(Math.random()*(1<<24)|0).toString(16).padStart(6,"0"),U=a===void 0?"":`, retryOf: ${a}`,r=Date.now();if(j(this).debug(`[${c}] sending request`,gt({retryOfRequestLogID:a,method:i.method,url:d,options:i,headers:o.headers})),i.signal?.aborted)throw new nt;let S=new AbortController,u=await this.fetchWithTimeout(d,o,l,S).catch(Ae),h=Date.now();if(u instanceof globalThis.Error){let I=`retrying, ${n} attempts remaining`;if(i.signal?.aborted)throw new nt;let T=Ie(u)||/timed? ?out/i.test(String(u)+("cause"in u?String(u.cause):""));if(n)return j(this).info(`[${c}] connection ${T?"timed out":"failed"} - ${I}`),j(this).debug(`[${c}] connection ${T?"timed out":"failed"} (${I})`,gt({retryOfRequestLogID:a,url:d,durationMs:h-r,message:u.message})),this.retryRequest(i,n,a??c);throw j(this).info(`[${c}] connection ${T?"timed out":"failed"} - error; no more retries left`),j(this).debug(`[${c}] connection ${T?"timed out":"failed"} (error; no more retries left)`,gt({retryOfRequestLogID:a,url:d,durationMs:h-r,message:u.message})),T?new ht:new Ct({cause:u})}let m=`[${c}${U}] ${o.method} ${d} ${u.ok?"succeeded":"failed"} with status ${u.status} in ${h-r}ms`;if(!u.ok){let I=await this.shouldRetry(u);if(n&&I){let f=`retrying, ${n} attempts remaining`;return await gi(u.body),j(this).info(`${m} - ${f}`),j(this).debug(`[${c}] response error (${f})`,gt({retryOfRequestLogID:a,url:u.url,status:u.status,headers:u.headers,durationMs:h-r})),this.retryRequest(i,n,a??c,u.headers)}let T=I?"error; no more retries left":"error; not retryable";j(this).info(`${m} - ${T}`);let k=await u.text().catch(f=>Ae(f).message),P=di(k),W=P?void 0:k;throw j(this).debug(`[${c}] response error (${T})`,gt({retryOfRequestLogID:a,url:u.url,status:u.status,headers:u.headers,message:W,durationMs:Date.now()-r})),this.makeStatusError(u.status,P,W,u.headers)}return j(this).info(m),j(this).debug(`[${c}] response start`,gt({retryOfRequestLogID:a,url:u.url,status:u.status,headers:u.headers,durationMs:h-r})),{response:u,options:i,controller:S,requestLogID:c,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 Re(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 c=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(c)}}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 li(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&&oi("timeout",a.timeout),a.timeout=a.timeout??this.timeout;let{bodyHeaders:c,body:U}=this.buildBody({options:a}),r=await this.buildHeaders({options:t,method:i,bodyHeaders:c,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=M([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?V(this,De,"f").call(this,{body:n,headers:M([a])}):{bodyHeaders:void 0,body:void 0};let i=M([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:sn(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)}:V(this,De,"f").call(this,{body:n,headers:i})}app(t){return new pn(t)}};wa=B,De=new WeakMap,ga=new WeakSet,Gi=function(){return this.baseURL!==Vi[this._options.environment||"production"]};B.\u004b\u0065\u0072\u006e\u0065\u006c=wa;B.DEFAULT_TIMEOUT=6e4;B.\u004b\u0065\u0072\u006e\u0065\u006cError=G;B.APIError=Z;B.APIConnectionError=Ct;B.APIConnectionTimeoutError=ht;B.APIUserAbortError=nt;B.NotFoundError=ae;B.ConflictError=ie;B.RateLimitError=oe;B.BadRequestError=te;B.AuthenticationError=ee;B.InternalServerError=re;B.PermissionDeniedError=ne;B.UnprocessableEntityError=se;B.toFile=gn;B.Deployments=Ht;B.Apps=xt;B.Invocations=Ft;B.Browsers=ot;B.Profiles=Gt;B.Auth=pt;B.Telemetry=Tt;B.Proxies=Vt;B.Extensions=Dt;B.BrowserPools=Ot;B.Credentials=Bt;B.Projects=ft;B.Organization=mt;B.AuditLogs=Mt;B.APIKeys=Lt;B.CredentialProviders=Rt;var Y={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]'},Yi={container:'div[jscontroller="HWk0Gf"]',sectionHeading:'.mgAbYb[role="heading"]',item:"a.rIRoqf"},mn={udm:"39",item:"a.rIRoqf",durationPattern:/^\d+:\d+$/,platforms:["YouTube","TikTok","Instagram","Facebook","X"]},Ki={section:".ULSxyf",item:"a.KYg7td.INpicf",title:".hyYc0c",source:".K4ETW"},Ji={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"},qi={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"},Xi={root:'[data-hveid="CAUQAA"]',wrapper:".Fgyi2e",citations:'.Fgyi2e [data-hveid] a[jsname="pxBnId"]'},ji={result:".wHYlTd.tF2Cxc",title:"h3.LC20lb",siteName:".VuuXrf",cite:"cite.tjvcx",snippet:".VwiC3b",redditCite:"cite.qLRx3b",ratingWrap:".Y0A0hc",ratingValue:".yi40Hd",reviewCount:".RDApEe"},zi={headingText:"Businesses",card:".w7Dbne",name:".OSrXXb",ratingValue:".yi40Hd",reviewCount:".RDApEe"};var $i="Google returned a CAPTCHA. Run with --headless=false to re-warm the browser profile, then retry.";var qr=["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"],Xr=["request_aborted","captcha","location_mismatch","connection","timeout","extraction","concurrency_limit","unauthorized","forbidden","invalid_request","rate_limit","billing","service_unavailable"];var bh=new Set(qr),Ph=new Set(Xr);function Qi(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 et=class extends Error{constructor(n){super(`CAPTCHA detected. ${n}`);this.instructions=n}instructions;name="CaptchaError"},Te=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)}},Yt=class extends Error{name="PaaInteractionInterruptedError"},Fe=class extends Error{name="LocationMismatchError";constructor(t="Google returned results for a different location than requested"){super(t)}};async function yn(e,t,n,a){let i,s=!1,o,d=()=>{},l=()=>a?.reason instanceof DOMException&&a.reason.name==="TimeoutError"?a.reason:new Ut,c=()=>d(l()),U=new Promise((S,u)=>{d=u}),r=async S=>{let u;try{await Promise.race([S.close().catch(()=>{}),new Promise(h=>{u=setTimeout(h,2e3)})])}finally{u&&clearTimeout(u)}};if(a?.aborted)throw l();a?.addEventListener("abort",c,{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",c),i&&await r(i)}}var Ca=require("crypto");var Zi=require("@libsql/client/http"),Ge=require("crypto"),z=require("zod");var zr=process.env.TURSO_DATABASE_URL??"file:./paa-api.db",$r=process.env.TURSO_AUTH_TOKEN,pa=null;function Cn(){return pa||(pa=(0,Zi.createClient)({url:zr,authToken:$r})),pa}var Rh=32*1024;var Bh=z.z.object({id:z.z.string(),user_id:z.z.number(),status:z.z.string().default("pending"),client_domain:z.z.string(),session_path:z.z.string(),request:z.z.string(),result:z.z.string().nullable(),error:z.z.string().nullable(),created_at:z.z.string(),updated_at:z.z.string()}),Hh=z.z.object({id:z.z.number(),job_id:z.z.string(),phase:z.z.string(),completed_at:z.z.string(),output_summary:z.z.string()});var ts=require("async_hooks"),es="MCP_SCRAPER_ALLOW_HEADLESS_HARVEST_TEST",ns=new ts.AsyncLocalStorage;function as(e,t){return ns.run(e,t)}function Kt(){return ns.getStore()}var Qr=166667e-10,Zr=.0001333336,is=new Set(["serp","fb_search","fb_ad","instagram"]),td=.00111,ed=4,nd=4e-4;function Ve(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 ad=Ve("NANGO_USD_PER_CONNECTION_MONTH",1),id=Ve("NANGO_USD_PER_FUNCTION_RUN",1e-4),sd=Ve("NANGO_USD_PER_PROXY_REQUEST",1e-4),od=Ve("NANGO_USD_PER_COMPUTE_SEC",2e-4),rd=Ve("\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?Zr:Qr)}function ss(e,t){return e==="fal_wizper"?Math.max(0,t)/ed*td:e==="deepinfra_qwen"?Math.max(0,t)/1e3*nd:e==="openrouter"||e==="mcp_memory_video"||e==="mcp_memory_ai"?Math.max(0,t):e==="nango_connection"?Math.max(0,t)*ad:e==="nango_function_run"?Math.max(0,t)*id:e==="nango_proxy_request"?Math.max(0,t)*sd:e==="nango_compute"?Math.max(0,t)*od:e==="\u0062\u0072\u0069\u0067\u0068\u0074\u0064\u0061\u0074\u0061_browser_api"?Math.max(0,t)/1e9*rd:0}var ya=!1,Ye=null;async function os(){if(!ya)return Ye||(Ye=dd().finally(()=>{Ye=null}),Ye)}async function dd(){let e=Cn(),t=await e.execute(`
|
|
9
|
+
${r}`)}return o},g=er(Ri);var xt=class extends v{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",D,{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 Ot=class extends v{list(t={},n){return this._client.getAPIList("/apps",D,{query:t,...n})}};var Se=class extends v{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",D,{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 ar=6,ir=5e4,sr=8e3,St=class extends G{};async function Hi(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=ar,...d}=i;if(!Number.isInteger(o)||o<0)throw new TypeError("maxTransferRetries must be a non-negative integer");let l=d.timeout??a,c,U={bytesWritten:0,chunks:0,rows:0},r=new Set;for(;;){let S=await or(e,c?{...t,cursor:c}:t,d,o,l),{nextCursor:u,hasMore:h,rows:m}=rr(S.headers,c);if(h&&u){if(r.has(u))throw new St("response repeated X-Next-Cursor header");r.add(u)}if(await Sr(n,S.body),c=u,U.bytesWritten+=S.body.byteLength,U.chunks+=1,U.rows+=m,s&&await s({...U,chunkRows:m}),!h)return U}}async function or(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 c=!1,U=setTimeout(()=>{c=!0,o.abort()},i);try{let r=new Uint8Array(await l.arrayBuffer());if(clearTimeout(U),n.signal?.aborted)throw new nt;if(c)throw new ht;let S=l.headers.get("x-content-sha256");if(!S)throw new St("response missing X-Content-Sha256 header");let u=await dr(r);if(n.signal?.aborted)throw new nt;if(u!==S)throw new St(`audit log chunk checksum mismatch (got ${u}, want ${S})`);return{body:r,headers:l.headers}}catch(r){if(n.signal?.aborted&&!(r instanceof nt)?r=new nt:c&&(r=new ht),s===a||r instanceof nt)throw r;await lr(s+1,n.signal)}finally{clearTimeout(U),n.signal?.removeEventListener("abort",d)}}}function rr(e,t){let n=e.get("x-has-more");if(n!=="true"&&n!=="false")throw new St("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 St("response missing or invalid X-Row-Count header");let s=Number(i);if(!Number.isSafeInteger(s)||s>ir)throw new St("response missing or invalid X-Row-Count header");let o=e.get("x-next-cursor")||void 0;if(a&&(!o||o===t))throw new St("response has invalid X-Next-Cursor header");if(!a&&o)throw new St("response returned a cursor after the final chunk");return{rows:s,nextCursor:o,hasMore:a}}async function dr(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 lr(e,t){let n=Math.min(1e3*2**(e-1),sr);await new Promise((a,i)=>{if(t?.aborted){i(new nt);return}let s=()=>{clearTimeout(o),i(new nt)},o=setTimeout(()=>{t?.removeEventListener("abort",s),a()},n);t?.addEventListener("abort",s,{once:!0})})}async function Sr(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+=Bi(a,t.byteLength-n);continue}if(a&&typeof a=="object"&&"bytesWritten"in a){let i=a.bytesWritten;n+=Bi(i,t.byteLength-n);continue}return}}function Bi(e,t){if(!Number.isSafeInteger(e)||e<=0||e>t)throw new St("audit log download destination performed a short write");return e}var Tt=class extends v{constructor(){super(...arguments),this.exportDestinations=new Se(this._client)}list(t,n){return this._client.getAPIList("/audit-logs",hn,{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 Hi((i,s)=>this.exportChunk(i,s),t,n,this._client.timeout,a)}};Tt.ExportDestinations=Se;var Ue=class extends v{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",D,{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`,D,{query:n,...a})}};var ue=class extends v{retrieve(t){return this._client.get("/auth/context",t)}};var pt=class extends v{constructor(){super(...arguments),this.context=new ue(this._client),this.connections=new Ue(this._client)}};pt.Context=ue;pt.Connections=Ue;var Wt=class extends v{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",D,{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 ce=class extends v{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 he=class extends v{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 ge=class extends v{execute(t,n,a){return this._client.post(g`/browsers/${t}/playwright/execute`,{body:n,...a})}};var we=class extends v{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 pe=class extends v{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 me=class extends v{events(t,n={},a){return this._client.getAPIList(g`/browsers/${t}/telemetry/events`,D,{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 ye=class extends v{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 Rt=class extends v{constructor(){super(...arguments),this.watch=new ye(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`,wt({body:n,...a,headers:T([{Accept:"*/*"},a?.headers])},this._client))}uploadZip(t,n,a){return this._client.post(g`/browsers/${t}/fs/upload_zip`,wt({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])})}};Rt.Watch=ye;function pn(e,t){return`${e.replace(/\/+$/,"")}${t.startsWith("/")?t:`/${t}`}`}async function Di(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:c,duplex:U,timeout_ms:r}=Tr(n,a);Mr(s);let S={url:s,jwt:i.jwt};r!==void 0&&(S.timeout_ms=r);let u=d.get("accept"),h={method:kr(o),path:pn(i.baseURL,"/curl/raw"),query:S,body:l,headers:T([{Authorization:null},u?{Accept:u}:{Accept:"*/*"},fr(d)]),signal:c??null,__binaryResponse:!0};return U&&(h.fetchOptions={duplex:U}),e.request(h).asResponse()}function kr(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 Tr(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((c,U)=>{i.set(U,c)});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 Mr(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 fr(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 ot=class extends v{constructor(){super(...arguments),this.telemetry=new me(this._client),this.replays=new pe(this._client),this.fs=new Rt(this._client),this.process=new we(this._client),this.logs=new he(this._client),this.computer=new ce(this._client),this.playwright=new ge(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",D,{query:t,...n})}curl(t,n,a){return this._client.post(g`/browsers/${t}/curl`,{body:n,...a})}fetch(t,n,a){return Di(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`,wt({body:n,...a,headers:T([{Accept:"*/*"},a?.headers])},this._client))}};ot.Telemetry=me;ot.Replays=pe;ot.Fs=Rt;ot.Process=we;ot.Logs=he;ot.Computer=ce;ot.Playwright=ge;var Bt=class extends v{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",D,{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 Ht=class extends v{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",D,{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 Dt=class extends v{create(t,n){return this._client.post("/deployments",wt({body:t,...n},this._client))}retrieve(t,n){return this._client.get(g`/deployments/${t}`,n)}list(t={},n){return this._client.getAPIList("/deployments",D,{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 Ft=class extends v{list(t={},n){return this._client.getAPIList("/extensions",D,{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",wt({body:t,...n},this._client))}};var Gt=class extends v{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",D,{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 Ce=class extends v{retrieve(t){return this._client.get("/org/entitlements",t)}};var ke=class extends v{retrieve(t){return this._client.get("/org/limits",t)}update(t,n){return this._client.patch("/org/limits",{body:t,...n})}};var mt=class extends v{constructor(){super(...arguments),this.entitlements=new Ce(this._client),this.limits=new ke(this._client)}};mt.Entitlements=Ce;mt.Limits=ke;var Vt=class extends v{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",D,{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 Te=class extends v{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 Mt=class extends v{constructor(){super(...arguments),this.limits=new Te(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",D,{query:t,...n})}delete(t,n){return this._client.delete(g`/org/projects/${t}`,{...n,headers:T([{Accept:"*/*"},n?.headers])})}};Mt.Limits=Te;var Yt=class extends v{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",D,{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 Me=class extends v{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",D,{query:t,...n})}delete(t,n){return this._client.delete(g`/telemetry/destinations/${t}`,{...n,headers:T([{Accept:"*/*"},n?.headers])})}};var ft=class extends v{constructor(){super(...arguments),this.destinations=new Me(this._client)}};ft.Destinations=Me;var De=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()}},Fi="KERNEL_BROWSER_ROUTING_SUBRESOURCES",Pr=["curl","telemetry/stream"],Ir=/^\/(?:v\d+\/)?browsers(?:\/[^/]+)?\/?$/,Ar=/^\/(?:v\d+\/)?browser_pools\/[^/]+\/acquire\/?$/,Lr=/^\/(?:v\d+\/)?browsers\/([^/]+)\/?$/,xr=/^\/(?:v\d+\/)?browser_pools\/[^/]+\/release\/?$/;function ca(){let e=qr();return e===void 0?[...Pr]:e.trim()===""?[]:e.split(",").map(t=>t.trim()).filter(Boolean)}function ha(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),c=Or(l,s),U=await Gr(e,{input:o,init:d,request:l},s,i,a);return c&&await Dr(U,a),await Wr(l,U,s,a),U}}function Or(e,t){let n=new URL(e.url);return n.origin===t&&(Ir.test(n.pathname)||Ar.test(n.pathname))}async function Wr(e,t,n,a){if(!t.ok)return;let i=new URL(e.url);if(i.origin!==n)return;let s=Rr(e,i.pathname)??await Br(e,i.pathname);s&&a.delete(s)}function Rr(e,t){if(e.method.toUpperCase()!=="DELETE")return;let n=t.match(Lr);return n&&decodeURIComponent(n[1]??"")||void 0}async function Br(e,t){if(!(e.method.toUpperCase()!=="POST"||!xr.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 Hr(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||Jr(s)||"";if(o)return{sessionId:n,baseURL:a,jwt:o}}async function Dr(e,t){if((e.headers.get("content-type")?.toLowerCase()??"").includes("application/json"))try{ua(await e.clone().json(),t)}catch{}}function ua(e,t){let n=Hr(e);if(n&&t.set(n),Array.isArray(e)){for(let a of e)ua(a,t);return}if(!(!e||typeof e!="object"))for(let a of Object.values(e))typeof a=="object"&&a!==null&&ua(a,t)}function Fr(e,t){let n=e.replace(/^\/+|\/+$/g,"");return t.some(a=>n===a||n.startsWith(a+"/"))}async function Gr(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 c=decodeURIComponent(l[1]??""),U=l[2]??"";if(!c||!Fr(U+(l[3]??""),s))return e(t,n);let r=o.get(c);if(r===void 0)return e(t,n);let S=new URL(pn(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 u=new Headers(a.headers);return u.delete("authorization"),e(S.toString(),Vr(t,a,n,u))}function Vr(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=Yr(t,n);o!==void 0&&(s.body=o),n?.duplex!==void 0?s.duplex=n.duplex:Kr(o)&&(s.duplex="half")}return s}function Yr(e,t){return t?.body!==void 0&&t.body!==null?t.body:e.body??void 0}function Kr(e){return globalThis.ReadableStream&&e instanceof globalThis.ReadableStream||typeof e=="object"&&e!==null&&Symbol.asyncIterator in e}function Jr(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?.[Fi];return typeof e=="string"?e:void 0}if(typeof globalThis.Deno<"u"){let e=globalThis.Deno.env?.get?.(Fi);return typeof e=="string"?e:void 0}}var mn=class{constructor(t){this.actions=new Map,this.name=t,Gi.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}))}}},ga=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)}},Gi=new ga;var fe=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 wa,pa,Fe,Vi,Yi={production:"https://api.onkernel.com/",development:"https://localhost:3001/"},B=class{constructor({baseURL:t=fe("KERNEL_BASE_URL"),apiKey:n=fe("KERNEL_API_KEY"),projectID:a=null,project:i=null,...s}={}){if(wa.add(this),Fe.set(this,void 0),this.deployments=new Dt(this),this.apps=new Ot(this),this.invocations=new Gt(this),this.browsers=new ot(this),this.profiles=new Vt(this),this.auth=new pt(this),this.telemetry=new ft(this),this.proxies=new Yt(this),this.extensions=new Ft(this),this.browserPools=new Wt(this),this.credentials=new Ht(this),this.projects=new Mt(this),this.organization=new mt(this),this.auditLogs=new Tt(this),this.apiKeys=new xt(this),this.credentialProviders=new Bt(this),typeof globalThis<"u"&&typeof globalThis.Bun<"u"&&globalThis.Bun.version&&!fe("KERNEL_SUPPRESS_BUN_WARNING")&&j(this).warn("The Bun runtime was detected. Playwright may have CDP connection issues, proceed with caution. Suppress this warning by setting the KERNEL_SUPPRESS_BUN_WARNING environment variable to true"),n===void 0)throw new G("The KERNEL_API_KEY environment variable is missing or empty; either provide it, or instantiate the Kernel client with an apiKey option, like new Kernel({ 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 KERNEL_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||Yi[o.environment||"production"],this.timeout=o.timeout??pa.DEFAULT_TIMEOUT,this.logger=o.logger??console;let d="warn";this.logLevel=d,this.logLevel=oa(o.logLevel,"ClientOptions.logLevel",this)??oa(fe("KERNEL_LOG"),"process.env['KERNEL_LOG']",this)??d,this.fetchOptions=o.fetchOptions,this.maxRetries=o.maxRetries??2,this.rawFetch=o.fetch??gi(),this.browserRouteCache=new De,this.fetch=ha(this.rawFetch,{apiBaseURL:this.baseURL,subresources:ca(),cache:this.browserRouteCache}),Q(this,Fe,pi,"f");let l=fe("KERNEL_CUSTOM_HEADERS");if(l){let c={};for(let U of l.split(`
|
|
10
|
+
`)){let r=U.indexOf(":");r>=0&&(c[U.substring(0,r).trim()]=U.substring(r+1).trim())}o.defaultHeaders={...c,...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=ha(n.rawFetch,{apiBaseURL:n.baseURL,subresources:ca(),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 vi(t)}getUserAgent(){return`${this.constructor.name}/JS ${kt}`}defaultIdempotencyKey(){return`stainless-node-retry-${Jn()}`}makeStatusError(t,n,a,i){return Z.generate(t,n,a,i)}buildURL(t,n,a){let i=!V(this,wa,"m",Vi).call(this)&&a||this.baseURL,s=si(t)?new URL(t):new URL(i+(i.endsWith("/")&&t.startsWith("/")?t.slice(1):t)),o=this.defaultQuery(),d=Object.fromEntries(s.searchParams);return(!jn(o)||!jn(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 Lt(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 c="log_"+(Math.random()*(1<<24)|0).toString(16).padStart(6,"0"),U=a===void 0?"":`, retryOf: ${a}`,r=Date.now();if(j(this).debug(`[${c}] sending request`,gt({retryOfRequestLogID:a,method:i.method,url:d,options:i,headers:o.headers})),i.signal?.aborted)throw new nt;let S=new AbortController,u=await this.fetchWithTimeout(d,o,l,S).catch(Le),h=Date.now();if(u instanceof globalThis.Error){let I=`retrying, ${n} attempts remaining`;if(i.signal?.aborted)throw new nt;let f=Ae(u)||/timed? ?out/i.test(String(u)+("cause"in u?String(u.cause):""));if(n)return j(this).info(`[${c}] connection ${f?"timed out":"failed"} - ${I}`),j(this).debug(`[${c}] connection ${f?"timed out":"failed"} (${I})`,gt({retryOfRequestLogID:a,url:d,durationMs:h-r,message:u.message})),this.retryRequest(i,n,a??c);throw j(this).info(`[${c}] connection ${f?"timed out":"failed"} - error; no more retries left`),j(this).debug(`[${c}] connection ${f?"timed out":"failed"} (error; no more retries left)`,gt({retryOfRequestLogID:a,url:d,durationMs:h-r,message:u.message})),f?new ht:new Ct({cause:u})}let m=`[${c}${U}] ${o.method} ${d} ${u.ok?"succeeded":"failed"} with status ${u.status} in ${h-r}ms`;if(!u.ok){let I=await this.shouldRetry(u);if(n&&I){let M=`retrying, ${n} attempts remaining`;return await wi(u.body),j(this).info(`${m} - ${M}`),j(this).debug(`[${c}] response error (${M})`,gt({retryOfRequestLogID:a,url:u.url,status:u.status,headers:u.headers,durationMs:h-r})),this.retryRequest(i,n,a??c,u.headers)}let f=I?"error; no more retries left":"error; not retryable";j(this).info(`${m} - ${f}`);let k=await u.text().catch(M=>Le(M).message),_=li(k),W=_?void 0:k;throw j(this).debug(`[${c}] response error (${f})`,gt({retryOfRequestLogID:a,url:u.url,status:u.status,headers:u.headers,message:W,durationMs:Date.now()-r})),this.makeStatusError(u.status,_,W,u.headers)}return j(this).info(m),j(this).debug(`[${c}] response start`,gt({retryOfRequestLogID:a,url:u.url,status:u.status,headers:u.headers,durationMs:h-r})),{response:u,options:i,controller:S,requestLogID:c,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 Be(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 c=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(c)}}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 Si(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&&ri("timeout",a.timeout),a.timeout=a.timeout??this.timeout;let{bodyHeaders:c,body:U}=this.buildBody({options:a}),r=await this.buildHeaders({options:t,method:i,bodyHeaders:c,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))}:{},...hi(),"X-Kernel-Project-Id":this.projectID,"X-Kernel-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?V(this,Fe,"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:on(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)}:V(this,Fe,"f").call(this,{body:n,headers:i})}app(t){return new mn(t)}};pa=B,Fe=new WeakMap,wa=new WeakSet,Vi=function(){return this.baseURL!==Yi[this._options.environment||"production"]};B.Kernel=pa;B.DEFAULT_TIMEOUT=6e4;B.KernelError=G;B.APIError=Z;B.APIConnectionError=Ct;B.APIConnectionTimeoutError=ht;B.APIUserAbortError=nt;B.NotFoundError=ie;B.ConflictError=se;B.RateLimitError=re;B.BadRequestError=ee;B.AuthenticationError=ne;B.InternalServerError=de;B.PermissionDeniedError=ae;B.UnprocessableEntityError=oe;B.toFile=wn;B.Deployments=Dt;B.Apps=Ot;B.Invocations=Gt;B.Browsers=ot;B.Profiles=Vt;B.Auth=pt;B.Telemetry=ft;B.Proxies=Yt;B.Extensions=Ft;B.BrowserPools=Wt;B.Credentials=Ht;B.Projects=Mt;B.Organization=mt;B.AuditLogs=Tt;B.APIKeys=xt;B.CredentialProviders=Bt;var Y={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]'},Ki={container:'div[jscontroller="HWk0Gf"]',sectionHeading:'.mgAbYb[role="heading"]',item:"a.rIRoqf"},yn={udm:"39",item:"a.rIRoqf",durationPattern:/^\d+:\d+$/,platforms:["YouTube","TikTok","Instagram","Facebook","X"]},Ji={section:".ULSxyf",item:"a.KYg7td.INpicf",title:".hyYc0c",source:".K4ETW"},qi={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"},Xi={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"},ji={root:'[data-hveid="CAUQAA"]',wrapper:".Fgyi2e",citations:'.Fgyi2e [data-hveid] a[jsname="pxBnId"]'},zi={result:".wHYlTd.tF2Cxc",title:"h3.LC20lb",siteName:".VuuXrf",cite:"cite.tjvcx",snippet:".VwiC3b",redditCite:"cite.qLRx3b",ratingWrap:".Y0A0hc",ratingValue:".yi40Hd",reviewCount:".RDApEe"},$i={headingText:"Businesses",card:".w7Dbne",name:".OSrXXb",ratingValue:".yi40Hd",reviewCount:".RDApEe"};var Qi="Google returned a CAPTCHA. Run with --headless=false to re-warm the browser profile, then retry.";var Xr=["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"],jr=["request_aborted","captcha","location_mismatch","connection","timeout","extraction","concurrency_limit","unauthorized","forbidden","invalid_request","rate_limit","billing","service_unavailable"];var _h=new Set(Xr),Eh=new Set(jr);function Zi(e){return e.replace(/https?:\/\/[^\s]*(?:brightdata|onkernel|kernel\.sh)[^\s]*/gi,"the service").replace(/bright\s*data/gi,"the service").replace(/brightdata/gi,"the service").replace(/kernel\.sh\s+sessions?/gi,"sessions").replace(/kernel\.sh\s+session/gi,"this session").replace(/kernel\.sh/gi,"the service").replace(/kernel\s+sessions?/gi,"sessions").replace(/kernel\s+session/gi,"this session").replace(/\b(?:sub|cus|sess|proxy)_[A-Za-z0-9_-]+\b/g,"[redacted]").replace(/\b(?:KERNEL|BRIGHTDATA|BRIGHT_DATA)_[A-Z0-9_]*API_KEY\b/g,"BROWSER_SERVICE_API_KEY").replace(/\bkernel\b/gi,"the service").replace(/ +/g," ").trim()}var et=class extends Error{constructor(n){super(`CAPTCHA detected. ${n}`);this.instructions=n}instructions;name="CaptchaError"},Ne=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)}},Kt=class extends Error{name="PaaInteractionInterruptedError"},Ge=class extends Error{name="LocationMismatchError";constructor(t="Google returned results for a different location than requested"){super(t)}};async function Cn(e,t,n,a){let i,s=!1,o,d=()=>{},l=()=>a?.reason instanceof DOMException&&a.reason.name==="TimeoutError"?a.reason:new Ut,c=()=>d(l()),U=new Promise((S,u)=>{d=u}),r=async S=>{let u;try{await Promise.race([S.close().catch(()=>{}),new Promise(h=>{u=setTimeout(h,2e3)})])}finally{u&&clearTimeout(u)}};if(a?.aborted)throw l();a?.addEventListener("abort",c,{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",c),i&&await r(i)}}var ka=require("crypto");var ts=require("@libsql/client/http"),Ve=require("crypto"),z=require("zod");var Nt="strftime('%Y-%m-%dT%H:%M:%fZ','now')";var xh=[`CREATE TABLE IF NOT EXISTS oauth_store_metadata (
|
|
11
|
+
id INTEGER PRIMARY KEY CHECK (id = 1),
|
|
12
|
+
store_id TEXT NOT NULL UNIQUE,
|
|
13
|
+
schema_version INTEGER NOT NULL CHECK (schema_version > 0),
|
|
14
|
+
authority_epoch INTEGER NOT NULL CHECK (authority_epoch > 0),
|
|
15
|
+
authority_state TEXT NOT NULL CHECK (authority_state IN ('ready','authoritative','retired')),
|
|
16
|
+
updated_at TEXT NOT NULL DEFAULT (${Nt})
|
|
17
|
+
) STRICT`,`CREATE TABLE IF NOT EXISTS oauth_clients (
|
|
18
|
+
client_id TEXT PRIMARY KEY,
|
|
19
|
+
client_name TEXT NOT NULL,
|
|
20
|
+
client_type TEXT NOT NULL CHECK (client_type = 'public'),
|
|
21
|
+
token_endpoint_auth_method TEXT NOT NULL CHECK (token_endpoint_auth_method = 'none'),
|
|
22
|
+
status TEXT NOT NULL CHECK (status IN ('active','disabled','expired')),
|
|
23
|
+
created_at TEXT NOT NULL DEFAULT (${Nt}),
|
|
24
|
+
last_used_at TEXT,
|
|
25
|
+
expires_at TEXT
|
|
26
|
+
) STRICT`,`CREATE TABLE IF NOT EXISTS oauth_client_redirect_uris (
|
|
27
|
+
client_id TEXT NOT NULL REFERENCES oauth_clients(client_id) ON DELETE CASCADE,
|
|
28
|
+
redirect_uri TEXT NOT NULL,
|
|
29
|
+
redirect_kind TEXT NOT NULL CHECK (redirect_kind IN ('https','loopback','private_scheme')),
|
|
30
|
+
PRIMARY KEY (client_id, redirect_uri)
|
|
31
|
+
) STRICT`,`CREATE TABLE IF NOT EXISTS oauth_codes (
|
|
32
|
+
code_digest BLOB PRIMARY KEY,
|
|
33
|
+
lookup_key_id TEXT NOT NULL,
|
|
34
|
+
client_id TEXT NOT NULL REFERENCES oauth_clients(client_id) ON DELETE RESTRICT,
|
|
35
|
+
redirect_uri TEXT NOT NULL,
|
|
36
|
+
code_challenge TEXT NOT NULL,
|
|
37
|
+
challenge_method TEXT NOT NULL CHECK (challenge_method = 'S256'),
|
|
38
|
+
user_id TEXT NOT NULL,
|
|
39
|
+
scope TEXT NOT NULL,
|
|
40
|
+
resource TEXT,
|
|
41
|
+
issued_at TEXT NOT NULL DEFAULT (${Nt}),
|
|
42
|
+
expires_at TEXT NOT NULL
|
|
43
|
+
) STRICT`,`CREATE TABLE IF NOT EXISTS oauth_code_exchanges (
|
|
44
|
+
code_digest BLOB PRIMARY KEY,
|
|
45
|
+
request_fingerprint BLOB NOT NULL UNIQUE,
|
|
46
|
+
client_id TEXT NOT NULL,
|
|
47
|
+
family_id TEXT NOT NULL UNIQUE,
|
|
48
|
+
grant_scope TEXT NOT NULL,
|
|
49
|
+
grant_resource TEXT,
|
|
50
|
+
scope TEXT NOT NULL,
|
|
51
|
+
resource TEXT,
|
|
52
|
+
sealed_refresh BLOB NOT NULL,
|
|
53
|
+
sealed_nonce BLOB NOT NULL,
|
|
54
|
+
sealed_tag BLOB NOT NULL,
|
|
55
|
+
replay_key_id TEXT NOT NULL,
|
|
56
|
+
completed_at TEXT NOT NULL DEFAULT (${Nt}),
|
|
57
|
+
replay_expires_at TEXT NOT NULL
|
|
58
|
+
) STRICT`,`CREATE TABLE IF NOT EXISTS oauth_refresh_families (
|
|
59
|
+
family_id TEXT PRIMARY KEY,
|
|
60
|
+
user_id TEXT NOT NULL,
|
|
61
|
+
client_id TEXT NOT NULL REFERENCES oauth_clients(client_id) ON DELETE RESTRICT,
|
|
62
|
+
issuer TEXT NOT NULL,
|
|
63
|
+
scope TEXT NOT NULL,
|
|
64
|
+
resource TEXT,
|
|
65
|
+
status TEXT NOT NULL CHECK (status IN ('active','revoked','compromised','expired')),
|
|
66
|
+
current_generation INTEGER NOT NULL CHECK (current_generation > 0),
|
|
67
|
+
created_at TEXT NOT NULL DEFAULT (${Nt}),
|
|
68
|
+
expires_at TEXT NOT NULL,
|
|
69
|
+
revoked_at TEXT,
|
|
70
|
+
compromised_at TEXT,
|
|
71
|
+
UNIQUE (family_id, client_id),
|
|
72
|
+
FOREIGN KEY (family_id, current_generation)
|
|
73
|
+
REFERENCES oauth_refresh_tokens(family_id, generation)
|
|
74
|
+
DEFERRABLE INITIALLY DEFERRED
|
|
75
|
+
) STRICT`,`CREATE TABLE IF NOT EXISTS oauth_refresh_tokens (
|
|
76
|
+
family_id TEXT NOT NULL REFERENCES oauth_refresh_families(family_id) ON DELETE CASCADE,
|
|
77
|
+
generation INTEGER NOT NULL CHECK (generation > 0),
|
|
78
|
+
token_digest BLOB NOT NULL UNIQUE,
|
|
79
|
+
lookup_key_id TEXT NOT NULL,
|
|
80
|
+
token_prefix TEXT NOT NULL,
|
|
81
|
+
predecessor_generation INTEGER,
|
|
82
|
+
issued_at TEXT NOT NULL DEFAULT (${Nt}),
|
|
83
|
+
expires_at TEXT NOT NULL,
|
|
84
|
+
used_at TEXT,
|
|
85
|
+
revoked_at TEXT,
|
|
86
|
+
PRIMARY KEY (family_id, generation),
|
|
87
|
+
CHECK (predecessor_generation IS NULL OR (predecessor_generation > 0 AND predecessor_generation < generation))
|
|
88
|
+
) STRICT`,`CREATE TABLE IF NOT EXISTS oauth_refresh_rotations (
|
|
89
|
+
family_id TEXT NOT NULL REFERENCES oauth_refresh_families(family_id) ON DELETE CASCADE,
|
|
90
|
+
predecessor_generation INTEGER NOT NULL,
|
|
91
|
+
request_fingerprint BLOB NOT NULL,
|
|
92
|
+
successor_generation INTEGER NOT NULL,
|
|
93
|
+
sealed_successor BLOB NOT NULL,
|
|
94
|
+
sealed_nonce BLOB NOT NULL,
|
|
95
|
+
sealed_tag BLOB NOT NULL,
|
|
96
|
+
replay_key_id TEXT NOT NULL,
|
|
97
|
+
rotated_at TEXT NOT NULL DEFAULT (${Nt}),
|
|
98
|
+
replay_expires_at TEXT NOT NULL,
|
|
99
|
+
PRIMARY KEY (family_id, predecessor_generation),
|
|
100
|
+
UNIQUE (family_id, successor_generation),
|
|
101
|
+
CHECK (successor_generation = predecessor_generation + 1)
|
|
102
|
+
) STRICT`,`CREATE TABLE IF NOT EXISTS oauth_security_events (
|
|
103
|
+
event_id TEXT PRIMARY KEY,
|
|
104
|
+
family_id TEXT NOT NULL,
|
|
105
|
+
event_type TEXT NOT NULL CHECK (event_type IN ('reuse','binding_mismatch','manual_revoke','expiry')),
|
|
106
|
+
reason_code TEXT NOT NULL,
|
|
107
|
+
occurred_at TEXT NOT NULL DEFAULT (${Nt}),
|
|
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)"];var $r=process.env.TURSO_DATABASE_URL??"file:./paa-api.db",Qr=process.env.TURSO_AUTH_TOKEN,ma=null;function kn(){return ma||(ma=(0,ts.createClient)({url:$r,authToken:Qr})),ma}var Vh=32*1024;var Yh=z.z.object({id:z.z.string(),user_id:z.z.number(),status:z.z.string().default("pending"),client_domain:z.z.string(),session_path:z.z.string(),request:z.z.string(),result:z.z.string().nullable(),error:z.z.string().nullable(),created_at:z.z.string(),updated_at:z.z.string()}),Kh=z.z.object({id:z.z.number(),job_id:z.z.string(),phase:z.z.string(),completed_at:z.z.string(),output_summary:z.z.string()});var es=require("async_hooks"),ns="MCP_SCRAPER_ALLOW_HEADLESS_HARVEST_TEST",as=new es.AsyncLocalStorage;function is(e,t){return as.run(e,t)}function Jt(){return as.getStore()}var Zr=166667e-10,td=.0001333336,ss=new Set(["serp","fb_search","fb_ad","instagram"]),ed=.00111,nd=4,ad=4e-4;function Ye(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 id=Ye("NANGO_USD_PER_CONNECTION_MONTH",1),sd=Ye("NANGO_USD_PER_FUNCTION_RUN",1e-4),od=Ye("NANGO_USD_PER_PROXY_REQUEST",1e-4),rd=Ye("NANGO_USD_PER_COMPUTE_SEC",2e-4),dd=Ye("BRIGHTDATA_BROWSER_USD_PER_GB",8);function ya(e,t){return Math.max(0,e)/1e3*(t?td:Zr)}function os(e,t){return e==="fal_wizper"?Math.max(0,t)/nd*ed:e==="deepinfra_qwen"?Math.max(0,t)/1e3*ad:e==="openrouter"||e==="mcp_memory_video"||e==="mcp_memory_ai"?Math.max(0,t):e==="nango_connection"?Math.max(0,t)*id:e==="nango_function_run"?Math.max(0,t)*sd:e==="nango_proxy_request"?Math.max(0,t)*od:e==="nango_compute"?Math.max(0,t)*rd:e==="brightdata_browser_api"?Math.max(0,t)/1e9*dd:0}var Ca=!1,Ke=null;async function rs(){if(!Ca)return Ke||(Ke=ld().finally(()=>{Ke=null}),Ke)}async function ld(){let e=kn(),t=await e.execute(`
|
|
11
110
|
SELECT
|
|
12
|
-
(SELECT COUNT(*) FROM sqlite_master WHERE type = 'table' AND name IN ('
|
|
13
|
-
AND (SELECT COUNT(*) FROM pragma_table_info('
|
|
111
|
+
(SELECT COUNT(*) FROM sqlite_master WHERE type = 'table' AND name IN ('kernel_session_log', 'vendor_usage_log', 'cost_probe_runs')) = 3
|
|
112
|
+
AND (SELECT COUNT(*) FROM pragma_table_info('kernel_session_log') WHERE name IN ('proxy_source', 'proxy_type', 'method')) = 3
|
|
14
113
|
AND (SELECT COUNT(*) FROM pragma_table_info('vendor_usage_log') WHERE name IN ('method', 'source_key', 'provider_duration_ms', 'provider_captcha', 'provider_status')) = 5
|
|
15
114
|
AND (SELECT COUNT(*) FROM sqlite_master WHERE type = 'index' AND name = 'vendor_usage_log_vendor_source_key') = 1
|
|
16
115
|
AND (SELECT COUNT(*) FROM pragma_table_info('cost_probe_runs') WHERE name IN ('units', 'unit_type', 'mode')) = 3
|
|
17
116
|
AS ready
|
|
18
|
-
`);if(Number(t.rows[0]?.ready??0)===1){
|
|
19
|
-
CREATE TABLE IF NOT EXISTS
|
|
117
|
+
`);if(Number(t.rows[0]?.ready??0)===1){Ca=!0;return}await e.execute(`
|
|
118
|
+
CREATE TABLE IF NOT EXISTS kernel_session_log (
|
|
20
119
|
id TEXT PRIMARY KEY,
|
|
21
|
-
|
|
120
|
+
kernel_session_id TEXT,
|
|
22
121
|
op TEXT,
|
|
23
122
|
source TEXT NOT NULL,
|
|
24
123
|
probe_run_id TEXT,
|
|
@@ -35,7 +134,7 @@ ${r}`)}return o},g=tr(Wi);var Lt=class extends v{create(t,n){return this._client
|
|
|
35
134
|
error TEXT,
|
|
36
135
|
created_at TEXT NOT NULL DEFAULT (datetime('now'))
|
|
37
136
|
)
|
|
38
|
-
`),await e.execute("CREATE INDEX IF NOT EXISTS
|
|
137
|
+
`),await e.execute("CREATE INDEX IF NOT EXISTS kernel_session_log_op ON kernel_session_log(op)"),await e.execute("CREATE INDEX IF NOT EXISTS kernel_session_log_probe ON kernel_session_log(probe_run_id)"),await e.execute("CREATE INDEX IF NOT EXISTS kernel_session_log_created ON kernel_session_log(created_at)");try{await e.execute("ALTER TABLE kernel_session_log ADD COLUMN proxy_source TEXT")}catch{}try{await e.execute("ALTER TABLE kernel_session_log ADD COLUMN proxy_type TEXT")}catch{}try{await e.execute("ALTER TABLE kernel_session_log ADD COLUMN method TEXT")}catch{}await e.execute(`
|
|
39
138
|
CREATE TABLE IF NOT EXISTS vendor_usage_log (
|
|
40
139
|
id TEXT PRIMARY KEY,
|
|
41
140
|
op TEXT,
|
|
@@ -63,23 +162,23 @@ ${r}`)}return o},g=tr(Wi);var Lt=class extends v{create(t,n){return this._client
|
|
|
63
162
|
success INTEGER NOT NULL DEFAULT 0,
|
|
64
163
|
error TEXT,
|
|
65
164
|
http_only INTEGER NOT NULL DEFAULT 0,
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
165
|
+
kernel_used INTEGER NOT NULL DEFAULT 0,
|
|
166
|
+
kernel_sessions INTEGER NOT NULL DEFAULT 0,
|
|
167
|
+
kernel_seconds_total REAL NOT NULL DEFAULT 0,
|
|
168
|
+
kernel_tier_observed TEXT,
|
|
169
|
+
est_kernel_cost_usd_headless REAL NOT NULL DEFAULT 0,
|
|
170
|
+
est_kernel_cost_usd_headful REAL NOT NULL DEFAULT 0,
|
|
72
171
|
vendor_cost_usd REAL NOT NULL DEFAULT 0,
|
|
73
172
|
charged_credits REAL NOT NULL DEFAULT 0,
|
|
74
173
|
margin_usd_headless_starter REAL NOT NULL DEFAULT 0,
|
|
75
174
|
margin_usd_headful_starter REAL NOT NULL DEFAULT 0,
|
|
76
175
|
notes TEXT
|
|
77
176
|
)
|
|
78
|
-
`),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{}
|
|
79
|
-
(id,
|
|
80
|
-
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`,args:[(0,
|
|
177
|
+
`),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{}Ca=!0}async function ds(e){try{await rs();let t=Jt(),n=Math.max(0,e.closedAtMs-e.openedAtMs);await kn().execute({sql:`INSERT INTO kernel_session_log
|
|
178
|
+
(id, kernel_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)
|
|
179
|
+
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`,args:[(0,ka.randomUUID)(),e.kernelSessionId??null,t?.op??null,e.source,t?.probeRunId??null,t?.userId??null,Tn(e.stealth),Tn(e.headlessSent),Tn(e.proxyUsed),e.proxySource??null,e.proxyType??null,e.fallback?1:0,new Date(e.openedAtMs).toISOString(),new Date(e.closedAtMs).toISOString(),n,ya(n,!1),ya(n,!0),e.error??null,t?.subOp??null]})}catch(t){console.warn("[cost-telemetry] recordKernelSession failed:",t instanceof Error?t.message:String(t))}}async function Ta(e){try{await rs();let t=Jt();return(await kn().execute({sql:`INSERT OR IGNORE INTO vendor_usage_log
|
|
81
180
|
(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)
|
|
82
|
-
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`,args:[(0,Ca.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,ss(e.vendor,e.units),e.error??null,e.method??t?.subOp??null,e.sourceKey??null,e.providerDurationMs??null,kn(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 kn(e){return e==null?null:e?1:0}var ld=/\b(?:HeadlessChrome|Chrome|Chromium|Firefox)\/\d+|\bVersion\/\d+.*\bSafari\//;async function Mn(e){try{let t=await e.evaluate(()=>navigator.userAgent);return typeof t=="string"&&ld.test(t)?t:null}catch{return null}}async function fn(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 Mn(n)}catch{return null}finally{t&&await t.close().catch(()=>{})}}Nn.chromium.use((0,ls.default)());var Sd=180,Ud=3e3,ud=5e3;function cd(e,t){let n=process.env[e];if(!n)return t;let a=Number(n);return Number.isInteger(a)&&a>0?a:t}function Ma(e){return e?e.slice(-6):null}function ds(e,t){let n=(t??process.env.SERP_REDIRECT_BASE)?.trim().replace(/\/+$/,"");return n?`${n}/g/${Buffer.from(e,"utf8").toString("base64url")}`:e}function hd(e){return e?/united states|u\.?s\.?a?\.?|^us$/i.test(e.trim()):!1}function Ke(e){return e instanceof Error?e.message:String(e)}function gd(e){return/\b409\b|conflict|already exists/i.test(Ke(e))}async function wd(e,t){try{await e.profiles.create({name:t})}catch(n){if(!gd(n))throw n}}function Tn(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 Ne(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 pd(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),c=l[0]??"",U=l[1]??"";if(c.startsWith("@"))return`https://www.youtube.com/${c}/videos`;if(c==="channel"&&U)return`https://www.youtube.com/channel/${U}/videos`;if((c==="c"||c==="user")&&U)return`https://www.youtube.com/${c}/${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 vn=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 B({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):cd("\u004b\u0045\u0052\u004e\u0045\u004c_BROWSER_TIMEOUT_SECONDS",Sd);t.\u006b\u0065\u0072\u006e\u0065\u006cProfileName&&t.\u006b\u0065\u0072\u006e\u0065\u006cProfileSaveChanges===!0&&await wd(this.\u006b\u0065\u0072\u006e\u0065\u006cClient,t.\u006b\u0065\u0072\u006e\u0065\u006cProfileName);let o=Kt(),d;t.headlessMode==="headful"||o?.forceHeadful===!0?d=!1:t.headlessMode==="headless"?d=!0:d=o?.forceHeadless===!0||process.env.\u004b\u0045\u0052\u004e\u0045\u004c_HEADLESS_DISABLED!=="true"&&!!o?.op&&is.has(o.op),this.\u006b\u0065\u0072\u006e\u0065\u006cHeadlessSent=d?!0:null,o?.headlessSentOut&&(o.headlessSentOut.value=this.\u006b\u0065\u0072\u006e\u0065\u006cHeadlessSent);let l=await this.\u006b\u0065\u0072\u006e\u0065\u006cClient.browsers.create({stealth:t.\u006b\u0065\u0072\u006e\u0065\u006cStealth===!0,timeout_seconds:s,...d?{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=l.session_id,this.\u006b\u0065\u0072\u006e\u0065\u006cOpenedAtMs=Date.now(),this.\u006b\u0065\u0072\u006e\u0065\u006cStealth=typeof l.stealth=="boolean"?l.stealth:null,this.\u006b\u0065\u0072\u006e\u0065\u006cProxyUsed=typeof l.proxy_id=="string"?!!l.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 c=null,U=null;if(!t.\u006b\u0065\u0072\u006e\u0065\u006cProxyId&&!t.keepDefaultProxy)try{await Ne(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`),c=!0}catch(S){c=!1,U=Ke(S)}let r={sessionId:this.\u006b\u0065\u0072\u006e\u0065\u006cSessionId,proxyMode:n,requestedProxyIdPresent:!!t.\u006b\u0065\u0072\u006e\u0065\u006cProxyId,requestedProxyIdSuffix:Ma(t.\u006b\u0065\u0072\u006e\u0065\u006cProxyId),createdProxyIdPresent:typeof l.proxy_id=="string"?!!l.proxy_id:null,createdProxyIdSuffix:Ma(l.proxy_id),retrievedProxyIdPresent:null,retrievedProxyIdSuffix:null,retrievedProxyIdMatchesRequested:null,defaultProxyDisabled:c,defaultProxyDisableError:U,proxyResolution:t.\u006b\u0065\u0072\u006e\u0065\u006cProxyResolution??null,timeoutSeconds:s,stealth:typeof l.stealth=="boolean"?l.stealth:null,profilePresent:null,poolPresent:null,retrieveError:null};if(this.debugSnapshot.\u006b\u0065\u0072\u006e\u0065\u006c=r,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:s,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(r,t.\u006b\u0065\u0072\u006e\u0065\u006cProxyId),this.browser=await Ss.chromium.connectOverCDP(l.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(Tn(t)),this.page=this.context.pages()[0]??await this.context.newPage(),this.debugSnapshot.context.userAgent=await Mn(this.page);else{let S=await fn(this.browser);this.debugSnapshot.context.userAgent=S,this.context=await this.browser.newContext(Tn(t,S)),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 S=this.debugSnapshot.networkLocation?.country??null;if(!hd(S))throw new et(`Non-US egress (${S??"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 Nn.chromium.launchPersistentContext(t.profileDir,{...i,...Tn(t)}),await this.installEsbuildHelperShims(this.context),this.page=await this.context.newPage(),this.debugSnapshot.context.userAgent=await Mn(this.page);else{this.browser=await Nn.chromium.launch(i);let s=await fn(this.browser);this.debugSnapshot.context.userAgent=s,this.context=await this.browser.newContext(Tn(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 Ne(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=Ma(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=Ke(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 Ne(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(Ke(a))}finally{await n?.close().catch(()=>{})}}async loadJsonInDebugPage(t,n){try{await Ne(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 yn(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=ds(d),c=s?.debug?{requestedUrl:d,finalUrl:null,title:null,bodySnippet:null,hasPaa:null,captchaDetected:null,googleSorryUrl:null,redirected:null}:null;c&&(this.debugSnapshot.serpNavigation=c);try{await this.page.goto(l,{waitUntil:"domcontentloaded",timeout:45e3})}catch(r){await this.updateSerpNavigationDebug(c,d,{hasPaa:null,captchaDetected:null});let S=await this.captureDiagnostics(d);throw new Te(`page.goto failed: ${r.message} | ${S}`)}if(await this.isSerpChallengeVisible())throw await this.updateSerpNavigationDebug(c,d,{hasPaa:!1,captchaDetected:!0}),new et(this.captchaMessage());if(await this.page.waitForSelector(Y.item,{timeout:4e3}).catch(()=>null))return await this.updateSerpNavigationDebug(c,d,{hasPaa:!0,captchaDetected:!1}),{hasPaa:!0};if(await this.isSerpChallengeVisible())throw await this.updateSerpNavigationDebug(c,d,{hasPaa:!1,captchaDetected:!0}),new et(this.captchaMessage());for(let r=1;r<=6;r++)if(await this.page.evaluate(u=>{window.scrollTo(0,document.body.scrollHeight*u)},r/6),await this.page.waitForTimeout(600),await this.page.locator(Y.item).count()>0)return await this.updateSerpNavigationDebug(c,d,{hasPaa:!0,captchaDetected:!1}),{hasPaa:!0};return await this.updateSerpNavigationDebug(c,d,{hasPaa:!1,captchaDetected:!1}),{hasPaa:!1}}async isSerpChallengeVisible(){if(!this.page)return!1;if(await this.page.locator(Y.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: ${Ke(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.":$i}async navigateTo(t){let n=process.env.ADS_ARRIVAL_REDIRECT_BASE?.trim(),a=n?ds(t,n):t;try{if(await this.page.goto(a,{waitUntil:"domcontentloaded",timeout:45e3}),/google\.[^/]+\/search\?/i.test(t)&&await this.isSerpChallengeVisible())throw new et(this.captchaMessage())}catch(i){if(i instanceof et)throw i;let s=await this.captureDiagnostics(t);throw new Te(`page.goto failed: ${i.message} | ${s}`)}}async navigateToChannel(t){let n=pd(t);try{await this.page.goto(n,{waitUntil:"networkidle",timeout:3e4})}catch(a){let i=await this.captureDiagnostics(n);throw new Te(`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,c=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&&rs({\u006b\u0065\u0072\u006e\u0065\u006cSessionId:n,source:"browser_driver",stealth:s,headlessSent:c,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=Ne(a.browsers.deleteByID(n),ud,`\u004b\u0065\u0072\u006e\u0065\u006c session ${n} delete`),S=Ne(t.close(),Ud,`\u004b\u0065\u0072\u006e\u0065\u006c browser ${n} close`),[u,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:u.status==="fulfilled",\u006b\u0065\u0072\u006e\u0065\u006cDeleteError:u.status==="rejected"?u.reason instanceof Error?u.reason.message:String(u.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 u.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}:`,u.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 gs=require("playwright");function md(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 Us(e){let t=md(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 us(e){return Us(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 fa(e,t,n=new Date().toISOString()){let a=Us(t);return{source:e,message:a??e.replaceAll("_"," "),observedAt:n}}function yd(e){return{provider:e.provider,providerDisconnectObserved:e.disconnectObservation!==null,providerDisconnectMessage:e.disconnectObservation?.message??null}}function hs(e){return{...yd(e),providerSessionId:e.providerSessionId}}var Cd=new Set(["image","media","font","stylesheet","ping"]),kd=["doubleclick.net","googleadservices.com","googlesyndication.com","google-analytics.com"];function ws(){return process.env.\u0042\u0052\u0049\u0047\u0048\u0054\u0044\u0041\u0054\u0041_BROWSER_WSS?.trim()||null}function ps(){return process.env.SERP_BROWSER_PROVIDER?.trim().toLowerCase()==="\u006b\u0065\u0072\u006e\u0065\u006c"?!1:!!ws()}async function Ta(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 bn=class{browser=null;context=null;page=null;cdpSession=null;providerSessionId=null;disconnectObservation=null;closing=!1;observeDisconnect(t,n){this.closing||this.disconnectObservation||(this.disconnectObservation=fa(t,n))}async launch(t){if(this.page)return;this.closing=!1,this.providerSessionId=null,this.disconnectObservation=null;let n=ws();if(!n)throw new Error("SERP browser service is not configured");this.browser=await gs.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 Ta(n,!0))&&!(s.pathname.includes("/shared_dict/")&&await Ta(n))&&!(i==="script"&&(o==="www.google.com"||o==="www.gstatic.com")&&await Ta(n))){if(Cd.has(i)){await n.abort("blockedbyclient");return}if(kd.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 yn(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:"load",timeout:45e3}),{hasPaa:!!await this.page.waitForSelector(Y.item,{timeout: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=us(a instanceof Error?a.message:String(a))??"\u0042\u0072\u0069\u0067\u0068\u0074 \u0044\u0061\u0074\u0061 browser close failed",this.disconnectObservation??=fa("browser_close_error",t)}let n=hs(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 ms(e,t){let n=!!(e?.serpIdentity||e?.\u006b\u0065\u0072\u006e\u0065\u006cProfileName);return t?.forceManaged===!0||ps()&&!n?new bn:new vn}var Jt={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 ys=`3, Nanjemoy,Maryland,United States
|
|
181
|
+
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`,args:[(0,ka.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,os(e.vendor,e.units),e.error??null,e.method??t?.subOp??null,e.sourceKey??null,e.providerDurationMs??null,Tn(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 Tn(e){return e==null?null:e?1:0}var Sd=/\b(?:HeadlessChrome|Chrome|Chromium|Firefox)\/\d+|\bVersion\/\d+.*\bSafari\//;async function Mn(e){try{let t=await e.evaluate(()=>navigator.userAgent);return typeof t=="string"&&Sd.test(t)?t:null}catch{return null}}async function fn(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 Mn(n)}catch{return null}finally{t&&await t.close().catch(()=>{})}}vn.chromium.use((0,Ss.default)());var Ud=180,ud=3e3,cd=5e3;function hd(e,t){let n=process.env[e];if(!n)return t;let a=Number(n);return Number.isInteger(a)&&a>0?a:t}function Ma(e){return e?e.slice(-6):null}function ls(e,t){let n=(t??process.env.SERP_REDIRECT_BASE)?.trim().replace(/\/+$/,"");return n?`${n}/g/${Buffer.from(e,"utf8").toString("base64url")}`:e}function gd(e){return e?/united states|u\.?s\.?a?\.?|^us$/i.test(e.trim()):!1}function Je(e){return e instanceof Error?e.message:String(e)}function wd(e){return/\b409\b|conflict|already exists/i.test(Je(e))}async function pd(e,t){try{await e.profiles.create({name:t})}catch(n){if(!wd(n))throw n}}function Nn(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 ve(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 md(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),c=l[0]??"",U=l[1]??"";if(c.startsWith("@"))return`https://www.youtube.com/${c}/videos`;if(c==="channel"&&U)return`https://www.youtube.com/channel/${U}/videos`;if((c==="c"||c==="user")&&U)return`https://www.youtube.com/${c}/${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 bn=class{browser=null;context=null;page=null;kernelClient=null;kernelSessionId=null;kernelOpenedAtMs=null;kernelStealth=null;kernelProxyUsed=null;kernelProxySource=null;kernelProxyType=null;kernelHeadlessSent=null;debugEnabled=!1;debugSnapshot={kernel:null,context:null,networkLocation:null,serpNavigation:null};async launch(t){this.debugEnabled=t.debug===!0;let n=t.proxyMode??(t.kernelProxyId?"configured":"none"),a=t.isMobile?"mobile":"desktop";if(this.debugSnapshot={kernel: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.kernelApiKey){this.kernelClient=new B({apiKey:t.kernelApiKey});let s=Number.isInteger(t.kernelTimeoutSeconds)&&Number(t.kernelTimeoutSeconds)>0?Number(t.kernelTimeoutSeconds):hd("KERNEL_BROWSER_TIMEOUT_SECONDS",Ud);t.kernelProfileName&&t.kernelProfileSaveChanges===!0&&await pd(this.kernelClient,t.kernelProfileName);let o=Jt(),d;t.headlessMode==="headful"||o?.forceHeadful===!0?d=!1:t.headlessMode==="headless"?d=!0:d=o?.forceHeadless===!0||process.env.KERNEL_HEADLESS_DISABLED!=="true"&&!!o?.op&&ss.has(o.op),this.kernelHeadlessSent=d?!0:null,o?.headlessSentOut&&(o.headlessSentOut.value=this.kernelHeadlessSent);let l=await this.kernelClient.browsers.create({stealth:t.kernelStealth===!0,timeout_seconds:s,...d?{headless:!0}:{},...t.kernelProxyId?{proxy_id:t.kernelProxyId}:{},...t.kernelProfileName?{profile:{name:t.kernelProfileName,...typeof t.kernelProfileSaveChanges=="boolean"?{save_changes:t.kernelProfileSaveChanges}:{}}}:{}});this.kernelSessionId=l.session_id,this.kernelOpenedAtMs=Date.now(),this.kernelStealth=typeof l.stealth=="boolean"?l.stealth:null,this.kernelProxyUsed=typeof l.proxy_id=="string"?!!l.proxy_id:!!t.kernelProxyId,this.kernelProxySource=t.kernelProxyResolution?.source??null,this.kernelProxyType=t.kernelProxyResolution?.proxyType??null;let c=null,U=null;if(!t.kernelProxyId&&!t.keepDefaultProxy)try{await ve(this.kernelClient.browsers.update(this.kernelSessionId,{disable_default_proxy:!0}),5e3,`Kernel session ${this.kernelSessionId} disable default proxy`),c=!0}catch(S){c=!1,U=Je(S)}let r={sessionId:this.kernelSessionId,proxyMode:n,requestedProxyIdPresent:!!t.kernelProxyId,requestedProxyIdSuffix:Ma(t.kernelProxyId),createdProxyIdPresent:typeof l.proxy_id=="string"?!!l.proxy_id:null,createdProxyIdSuffix:Ma(l.proxy_id),retrievedProxyIdPresent:null,retrievedProxyIdSuffix:null,retrievedProxyIdMatchesRequested:null,defaultProxyDisabled:c,defaultProxyDisableError:U,proxyResolution:t.kernelProxyResolution??null,timeoutSeconds:s,stealth:typeof l.stealth=="boolean"?l.stealth:null,profilePresent:null,poolPresent:null,retrieveError:null};if(this.debugSnapshot.kernel=r,console.info(JSON.stringify({event:"kernel_browser_created",kernel_session_id:this.kernelSessionId,timeout_seconds:s,proxy_mode:n,proxy_id_present:!!t.kernelProxyId,profile_name_present:!!t.kernelProfileName,proxy_resolution_source:t.kernelProxyResolution?.source})),this.debugEnabled&&await this.populateKernelRetrieveDebug(r,t.kernelProxyId),this.browser=await Us.chromium.connectOverCDP(l.cdp_ws_url),t.kernelProfileName||t.preserveKernelContext)this.context=this.browser.contexts()[0]??await this.browser.newContext(Nn(t)),this.page=this.context.pages()[0]??await this.context.newPage(),this.debugSnapshot.context.userAgent=await Mn(this.page);else{let S=await fn(this.browser);this.debugSnapshot.context.userAgent=S,this.context=await this.browser.newContext(Nn(t,S)),this.page=await this.context.newPage()}if(await this.installEsbuildHelperShims(this.context),t.preserveKernelContext||await this.page.setViewportSize(t.viewport),(this.debugEnabled||t.requireUsEgress)&&(this.debugSnapshot.networkLocation=await this.captureBrowserNetworkLocation()),t.requireUsEgress){let S=this.debugSnapshot.networkLocation?.country??null;if(!gd(S))throw new et(`Non-US egress (${S??"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 vn.chromium.launchPersistentContext(t.profileDir,{...i,...Nn(t)}),await this.installEsbuildHelperShims(this.context),this.page=await this.context.newPage(),this.debugSnapshot.context.userAgent=await Mn(this.page);else{this.browser=await vn.chromium.launch(i);let s=await fn(this.browser);this.debugSnapshot.context.userAgent=s,this.context=await this.browser.newContext(Nn(t,s)),await this.installEsbuildHelperShims(this.context),this.page=await this.context.newPage()}this.debugEnabled&&(this.debugSnapshot.networkLocation=await this.captureBrowserNetworkLocation())}async populateKernelRetrieveDebug(t,n){if(!(!this.kernelClient||!this.kernelSessionId))try{let a=await ve(this.kernelClient.browsers.retrieve(this.kernelSessionId),5e3,`Kernel session ${this.kernelSessionId} retrieve`);t.retrievedProxyIdPresent=typeof a.proxy_id=="string"?!!a.proxy_id:!1,t.retrievedProxyIdSuffix=Ma(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=Je(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 ve(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(Je(a))}finally{await n?.close().catch(()=>{})}}async loadJsonInDebugPage(t,n){try{await ve(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 Cn(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=ls(d),c=s?.debug?{requestedUrl:d,finalUrl:null,title:null,bodySnippet:null,hasPaa:null,captchaDetected:null,googleSorryUrl:null,redirected:null}:null;c&&(this.debugSnapshot.serpNavigation=c);try{await this.page.goto(l,{waitUntil:"domcontentloaded",timeout:45e3})}catch(r){await this.updateSerpNavigationDebug(c,d,{hasPaa:null,captchaDetected:null});let S=await this.captureDiagnostics(d);throw new Ne(`page.goto failed: ${r.message} | ${S}`)}if(await this.isSerpChallengeVisible())throw await this.updateSerpNavigationDebug(c,d,{hasPaa:!1,captchaDetected:!0}),new et(this.captchaMessage());if(await this.page.waitForSelector(Y.item,{timeout:4e3}).catch(()=>null))return await this.updateSerpNavigationDebug(c,d,{hasPaa:!0,captchaDetected:!1}),{hasPaa:!0};if(await this.isSerpChallengeVisible())throw await this.updateSerpNavigationDebug(c,d,{hasPaa:!1,captchaDetected:!0}),new et(this.captchaMessage());for(let r=1;r<=6;r++)if(await this.page.evaluate(u=>{window.scrollTo(0,document.body.scrollHeight*u)},r/6),await this.page.waitForTimeout(600),await this.page.locator(Y.item).count()>0)return await this.updateSerpNavigationDebug(c,d,{hasPaa:!0,captchaDetected:!1}),{hasPaa:!0};return await this.updateSerpNavigationDebug(c,d,{hasPaa:!1,captchaDetected:!1}),{hasPaa:!1}}async isSerpChallengeVisible(){if(!this.page)return!1;if(await this.page.locator(Y.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: ${Je(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.kernelClient?"Google returned a CAPTCHA on this session \u2014 retrying with a fresh session.":Qi}async navigateTo(t){let n=process.env.ADS_ARRIVAL_REDIRECT_BASE?.trim(),a=n?ls(t,n):t;try{if(await this.page.goto(a,{waitUntil:"domcontentloaded",timeout:45e3}),/google\.[^/]+\/search\?/i.test(t)&&await this.isSerpChallengeVisible())throw new et(this.captchaMessage())}catch(i){if(i instanceof et)throw i;let s=await this.captureDiagnostics(t);throw new Ne(`page.goto failed: ${i.message} | ${s}`)}}async navigateToChannel(t){let n=md(t);try{await this.page.goto(n,{waitUntil:"networkidle",timeout:3e4})}catch(a){let i=await this.captureDiagnostics(n);throw new Ne(`navigateToChannel failed: ${a.message} | ${i}`)}}async evaluate(t,n){return this.page.evaluate(t,n)}getPage(){return this.page}getKernelSessionId(){return this.kernelSessionId}getProviderSessionMetadata(){return{provider:this.kernelSessionId?"kernel":"local",providerSessionId:this.kernelSessionId,disconnectObservation:null}}getDebugSnapshot(){return this.debugSnapshot}async close(){if(this.browser){let t=this.browser,n=this.kernelSessionId,a=this.kernelClient,i=this.kernelOpenedAtMs,s=this.kernelStealth,o=this.kernelProxyUsed,d=this.kernelProxySource,l=this.kernelProxyType,c=this.kernelHeadlessSent,U=this.getProviderSessionMetadata();if(this.browser=null,this.context=null,this.page=null,this.kernelSessionId=null,this.kernelClient=null,this.kernelOpenedAtMs=null,this.kernelHeadlessSent=null,this.kernelProxySource=null,this.kernelProxyType=null,a&&n&&i!=null&&ds({kernelSessionId:n,source:"browser_driver",stealth:s,headlessSent:c,proxyUsed:o,proxySource:d,proxyType:l,openedAtMs:i,closedAtMs:Date.now()}),a&&n){console.info(JSON.stringify({event:"kernel_browser_delete_started",kernel_session_id:n}));let r=ve(a.browsers.deleteByID(n),cd,`Kernel session ${n} delete`),S=ve(t.close(),ud,`Kernel browser ${n} close`),[u,h]=await Promise.allSettled([r,S]),m={kernelSessionId:n,kernelDeleteStarted:!0,kernelDeleteSucceeded:u.status==="fulfilled",kernelDeleteError:u.status==="rejected"?u.reason instanceof Error?u.reason.message:String(u.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 u.status==="rejected"?(console.warn(JSON.stringify({event:"kernel_browser_delete_failed",kernel_session_id:n,message:m.kernelDeleteError})),console.warn(`Kernel session cleanup failed for ${n}:`,u.reason)):console.info(JSON.stringify({event:"kernel_browser_delete_succeeded",kernel_session_id:n})),h.status==="rejected"&&(console.warn(JSON.stringify({event:"kernel_browser_close_failed",kernel_session_id:n,message:m.browserCloseError})),console.warn(`Kernel browser close failed for ${n}:`,h.reason)),m}return await t.close(),{kernelSessionId:null,kernelDeleteStarted:!1,kernelDeleteSucceeded:null,kernelDeleteError: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(),{kernelSessionId:null,kernelDeleteStarted:!1,kernelDeleteSucceeded:null,kernelDeleteError:null,browserCloseSucceeded:!0,browserCloseError:null,provider:"local",providerSessionId:null,providerDisconnectObserved:!1,providerDisconnectMessage:null}}return{kernelSessionId:null,kernelDeleteStarted:!1,kernelDeleteSucceeded:null,kernelDeleteError:null,browserCloseSucceeded:null,browserCloseError:null,provider:"local",providerSessionId:null,providerDisconnectObserved:!1,providerDisconnectMessage:null}}};var ws=require("playwright");function yd(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 us(e){let t=yd(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 cs(e){return us(e)}function hs(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("Bright Data did not return a valid browser session ID");return e}function fa(e,t,n=new Date().toISOString()){let a=us(t);return{source:e,message:a??e.replaceAll("_"," "),observedAt:n}}function Cd(e){return{provider:e.provider,providerDisconnectObserved:e.disconnectObservation!==null,providerDisconnectMessage:e.disconnectObservation?.message??null}}function gs(e){return{...Cd(e),providerSessionId:e.providerSessionId}}var kd=new Set(["image","media","font","stylesheet","ping"]),Td=["doubleclick.net","googleadservices.com","googlesyndication.com","google-analytics.com"];function ps(){return process.env.BRIGHTDATA_BROWSER_WSS?.trim()||null}function ms(){return process.env.SERP_BROWSER_PROVIDER?.trim().toLowerCase()==="kernel"?!1:!!ps()}async function Na(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 _n=class{browser=null;context=null;page=null;cdpSession=null;providerSessionId=null;disconnectObservation=null;closing=!1;observeDisconnect(t,n){this.closing||this.disconnectObservation||(this.disconnectObservation=fa(t,n))}async launch(t){if(this.page)return;this.closing=!1,this.providerSessionId=null,this.disconnectObservation=null;let n=ps();if(!n)throw new Error("SERP browser service is not configured");this.browser=await ws.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=hs(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 Na(n,!0))&&!(s.pathname.includes("/shared_dict/")&&await Na(n))&&!(i==="script"&&(o==="www.google.com"||o==="www.gstatic.com")&&await Na(n))){if(kd.has(i)){await n.abort("blockedbyclient");return}if(Td.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 Cn(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:"load",timeout:45e3}),{hasPaa:!!await this.page.waitForSelector(Y.item,{timeout: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}getKernelSessionId(){return null}getProviderSessionMetadata(){return{provider:"bright_data_browser_api",providerSessionId:this.providerSessionId,disconnectObservation:this.disconnectObservation}}getDebugSnapshot(){return{kernel:null,context:null,networkLocation:null,serpNavigation:null}}async close(){this.closing=!0;let t=null;try{await this.browser?.close()}catch(a){t=cs(a instanceof Error?a.message:String(a))??"Bright Data browser close failed",this.disconnectObservation??=fa("browser_close_error",t)}let n=gs(this.getProviderSessionMetadata());return this.browser=null,this.context=null,this.page=null,this.cdpSession=null,{kernelSessionId:null,kernelDeleteStarted:!1,kernelDeleteSucceeded:null,kernelDeleteError:null,browserCloseSucceeded:t===null,browserCloseError:t,...n}}};function ys(e,t){let n=!!(e?.serpIdentity||e?.kernelProfileName);return t?.forceManaged===!0||ms()&&!n?new _n:new bn}var qt={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 Cs=`3, Nanjemoy,Maryland,United States
|
|
83
182
|
Aaronsburg,Pennsylvania,United States
|
|
84
183
|
Abbeville,Alabama,United States
|
|
85
184
|
Abbeville,Georgia,United States
|
|
@@ -21771,8 +21870,8 @@ Zortman,Montana,United States
|
|
|
21771
21870
|
Zuehl,Texas,United States
|
|
21772
21871
|
Zumbrota,Minnesota,United States
|
|
21773
21872
|
Zuni,New Mexico,United States
|
|
21774
|
-
Zwolle,Louisiana,United States`,
|
|
21775
|
-
`)){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(
|
|
21873
|
+
Zwolle,Louisiana,United States`,ks={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 va={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 ba=null,Ms=null;function Md(){if(ba)return;let e=new Map,t=new Map;for(let n of Cs.split(`
|
|
21874
|
+
`)){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(ks)){let a=String(n).toLowerCase();e.has(a)||e.set(a,String(n))}ba=e,Ms=t}function fd(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 fs(e){let t=Buffer.from(e,"utf8");return`w+${Buffer.concat([Buffer.from([8,2,16,32,34]),Buffer.from(fd(t.length)),t]).toString("base64")}`}function Ts(){return{canonical:null,matched:!1,source:"unresolved",ambiguousCandidates:0}}function vt(e,t){return{canonical:e,matched:!0,source:t,ambiguousCandidates:0}}function _a(e){let t=e.toLowerCase().trim();if(!t)return Ts();if(qt[t])return vt(qt[t],"alias");Md();let n=ba,a=Ms,i=n.get(t);if(i)return vt(i,"exact");let s=n.get(`${t},united states`);if(s)return vt(s,"state-or-country");let o=t.split(",");if(o.length>=2){let r=o[0].trim(),S=o.slice(1).join(",").trim(),u=va[S.toUpperCase()]??S,h=n.get(`${r},${u.toLowerCase()},united states`);if(h)return vt(h,"city-state")}let d=t.match(/^(.*?)[\s,]+([a-z]{2})$/);if(d){let r=va[d[2].toUpperCase()];if(r){let S=n.get(`${d[1].trim()},${r.toLowerCase()},united states`);if(S)return vt(S,"city-state")}}let l=a.get(t);if(l?.length===1)return vt(l[0],"city-unique");if(l&&l.length>1)return{canonical:null,matched:!1,source:"unresolved",ambiguousCandidates:l.length};let c=o[0].trim();if(c!==t&&qt[c])return vt(qt[c],"alias-partial");let U=t.replace(/\s+[a-z]{2}$/,"").trim();return U!==t&&qt[U]?vt(qt[U],"alias-partial"):Ts()}function Ns(e){return _a(e).canonical??e}var Ea={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"},Nd=[...Object.keys(Ea).map(e=>e.replace(/\s+/g,"\\s+")),...Object.values(Ea)].join("|"),vd=new RegExp(`\\b([A-Z][A-Za-z]+(?:[\\s.-][A-Z][A-Za-z]+){0,4}),?\\s+(${Nd})\\b`,"gi");function bs(e){if(!e)return null;let t=e.trim();return/^[A-Z]{2}$/i.test(t)?t.toUpperCase():Ea[t.toLowerCase()]??null}function _s(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:_s(t),regionCode:bs(n),canonicalLocation:e}}function _d(e,t,n,a){let i=_s(t),s=bs(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 Ed(e){try{return decodeURIComponent(e)}catch{}try{return decodeURIComponent(e.replace(/%(?![0-9a-fA-F]{2})/g,"%25"))}catch{return e}}function vs(e,t){let n=Ed(t).replace(/[+/|_-]+/g," ");for(let a of n.matchAll(vd))_d(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)vs(i,[d.title,d.snippet??"",d.cite??"",d.url].join(" "));for(let d of n)vs(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 Pd=[/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],Id=[/\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 En(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 Ad(e){let t=-1;for(let n of Id){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 Ld(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 xd(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),c=/^(?:Best|Top|What|How|Why|When|Where|Which|Can|Should|Is|Are|Do|Does)\b/i.test(d);if(d.length>20&&c&&/(?:\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 Od(e){let t=e.match(/https?:\/\/\S+/i);if(!t?.index)return e;let n=e.slice(0,t.index);return n.slice(0,xd(n))}function Wd(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 Es(e,t,n){if(!e)return;let a=En(e),i=t?En(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 Pd)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=En(a),a=Ad(a),a=Ld(a,n),a=Od(a),a=En(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=Wd(a),!!a&&!/^An error has occurred\.?\s*Please try again later\.?$/i.test(a))return a}}async function Ps(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(p){return p?(p.innerText??p.textContent??"").trim():""}function s(p){if(!p)return!1;let b=p.replace(/\s+/g," ").trim();return b.length>=16&&b.split(" ").filter(Boolean).length>=2}function o(p){let b=p.querySelector(t.aio.heading);if(i(b)==="AI Overview")return!0;let y=p.querySelector(t.aio.header);return!!i(y).split(/\n|\s{2,}/).some(L=>L.trim()==="AI Overview")}function d(){let b=Array.from(document.querySelectorAll(t.aio.root)).find(o);if(b)return b;if(t.aio.legacyRoot){let L=document.querySelector(t.aio.legacyRoot);if(L&&o(L))return L}let y=document.querySelectorAll(`${t.aio.heading}, h1, h2, h3, [role="heading"]`);for(let L of y){if(i(L)!=="AI Overview")continue;let C=L.parentElement;for(let N=0;N<8&&C;N++){if(C.matches(t.aio.root)||C.querySelector(t.aio.controller)||C.querySelector(t.aio.contentSubtree))return C;C=C.parentElement}return L.parentElement}return null}function l(p){if(!p)return null;let b=p.cloneNode(!0);b.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(x=>x.remove());let y=document.createElement("div");y.style.position="fixed",y.style.left="-10000px",y.style.top="0",y.style.width=`${Math.max(320,Math.round(p.getBoundingClientRect?.().width||960))}px`,y.style.opacity="0",y.style.pointerEvents="none",y.append(b),document.body.append(y);let L=b.innerText||b.textContent||"";y.remove();let C=L.replace(/\r/g,"").replace(/[ \t]+\n/g,`
|
|
21776
21875
|
`).replace(/\n[ \t]+/g,`
|
|
21777
21876
|
`).replace(/\n{3,}/g,`
|
|
21778
21877
|
|
|
@@ -21780,15 +21879,15 @@ Zwolle,Louisiana,United States`,Cs={ad:"Andorra",ae:"United Arab Emirates",af:"A
|
|
|
21780
21879
|
`).map(x=>x.replace(/\u00a0/g," ").trim()).filter(Boolean),N=[];for(let x=0;x<C.length;x++){let R=C[x],J=C[x+1]??"";if(R!=="AI Overview"&&R!=="Show more"&&!/^AI can make mistakes/i.test(R)&&!/^Thank you\b/i.test(R)&&!/^Your feedback helps Google improve/i.test(R)&&!/^\+?\d+$/.test(R)){if(/^\+\d+$/.test(J)&&R.length<=80){x++;continue}N.push(R)}}let H=N.join(`
|
|
21781
21880
|
`).replace(/\n{3,}/g,`
|
|
21782
21881
|
|
|
21783
|
-
`).trim();return!H||/not available|try again|can't generate/i.test(H)?null:H}function c(p){if(!p||p.startsWith("javascript:"))return null;try{let b=new URL(p,window.location.href);return!["http:","https:"].includes(b.protocol)||b.username||b.password?null:{href:b.href,rawHref:b.href}}catch{return null}}function U(p){if(!p)return[];let b=new Set,y=[];for(let L of Array.from(p.querySelectorAll("a[href]"))){let C=c(L.getAttribute("href")??"");if(!C||b.has(C.rawHref))continue;b.add(C.rawHref);let N="";try{N=new URL(C.href).hostname.replace(/^www\./,"")}catch{}y.push({text:i(L)||N||C.href,href:C.href,rawHref:C.rawHref})}return y}async function r(p){let b=p.querySelector(t.aio.showMoreButton);if(!b||b.getAttribute("aria-expanded")!=="false")return!1;b.click();let y=t.expandWaitMs??1500;return y>0&&await new Promise(L=>setTimeout(L,y)),!0}let S=d(),u=null,h=[],m=!1,I=!1,
|
|
21784
|
-
`).map(L=>L.trim()).filter(L=>/^\d+\.\s+.+/.test(L)),h=U(S)}let k=document.querySelector(t.aim.root),P=a==="aim"&&!!k,W=k?.closest(t.aim.wrapper)??k,E=P?l(W):null,f=P&&s(E),O=f?E:null,A=f?U(W):[],_=!!S&&s(u);return{surface:a,aiOverview:{detected:_,text:_?u:null,citations:_?h:[],expanded:_?m:!1,fullyExpanded:_?I:!1,sections:_?T:[]},aiMode:{detected:f&&O!==null,text:O,citations:A}}}var Is=require("crypto"),As=8e3,Ls=3e3,xs=1e4,Wd=8;function vt(e){return/(^|\.)google\.(?:com|[a-z]{2,3}(?:\.[a-z]{2})?)$/i.test(e)}function Xe(e){if(!e||e.length>As)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 Rd(e,t){if(!e||e.length>As||/^javascript:/i.test(e))return null;try{let n=new URL(e,t);return Xe(n.href)}catch{return null}}function qe(e){let t=Xe(e);return!t||t.protocol!=="https:"||!vt(t.hostname)||t.pathname!=="/goto"?null:t.searchParams.get("url")?t:null}function Ea(e){if(e.linkType!=="google_goto_redirect"||e.resolutionStatus!=="resolved"||!qe(e.rawUrl)||!e.resolvedUrl)return!1;let t=Xe(e.resolvedUrl);return!!(t&&!vt(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,Is.createHash)("sha256").update(t).digest("hex").slice(0,16)}`}function En(e,t){let n=Rd(e,t);if(!n)return null;let a=n.href;if(!vt(n.hostname))return{url:a,rawUrl:a,resolvedUrl:a,linkType:"plain",resolutionStatus:"not_needed"};if(n.pathname==="/url"){let i=Xe(n.searchParams.get("q")??n.searchParams.get("url")??"");return!i||vt(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 _a(e){return qe(e)?.searchParams.get("url")??null}function Os(e,t){try{let n=new URL(e),a=Xe(new URL(t,n).href);return!a||vt(a.hostname)?null:(!a.hash&&n.hash&&(a.hash=n.hash),a.href)}catch{return null}}async function Ws(e,t={}){let n=[...new Set([...e].flatMap(r=>qe(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??Ls),d=Date.now()+Math.max(1,t.totalBudgetMs??xs),l=Math.max(1,Math.min(16,Math.floor(t.concurrency??Wd))),c=0,U=async()=>{for(;c<n.length;){let r=n[c++],S=qe(r),u=S?.searchParams.get("url");if(!S||!u){a.set(r,{rawUrl:r,resolvedUrl:null,resolutionStatus:"rejected"});continue}S.hash="";let h=S.href;for(let m=0;m<2;m++){let I=d-Date.now();if(I<=0)return;let T=new AbortController,k=setTimeout(()=>T.abort(),Math.min(o,I)),P;try{P=await s(h,{method:"GET",redirect:"manual",headers:{accept:"*/*","user-agent":"Mozilla/5.0"},signal:T.signal})}catch{break}finally{clearTimeout(k)}if(P.status<300||P.status>=400)break;let W=P.headers.get("location");if(!W)break;let E=Os(r,W);if(E){a.set(r,{rawUrl:r,resolvedUrl:E,resolutionStatus:"resolved"});break}let f;try{f=new URL(W,h)}catch{a.set(r,{rawUrl:r,resolvedUrl:null,resolutionStatus:"rejected"});break}if(!vt(f.hostname)||_a(f.href)!==u){a.set(r,{rawUrl:r,resolvedUrl:null,resolutionStatus:"rejected"});break}h=f.href}}};return await Promise.all(Array.from({length:Math.min(l,n.length)},()=>U())),{resolutions:[...a.values()],nonGoogleRequestsObserved:0}}function _s(e,t){try{return new URL(e.resolvedUrl??e.url).hostname.replace(/^www\./,"")}catch{return t}}async function Rs(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,...u})=>({...u,url:S})),...(e.aiMode?.citations??[]).map(({text:r,href:S,...u})=>({...u,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 Ws(o,t);for(let S of r.resolutions){let u=s.get(S.rawUrl);u&&s.set(S.rawUrl,S.resolutionStatus==="resolved"&&S.resolvedUrl?{...u,url:S.resolvedUrl,resolvedUrl:S.resolvedUrl,resolutionStatus:"resolved"}:{...u,resolutionStatus:S.resolutionStatus})}}let d=r=>({...r,...s.get(r.rawUrl)??r}),l=r=>r.map(S=>{let u=d(S);return{...u,site:_s(u,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:_s(S,e.organicResults[r].domain)??""}}let c=r=>{let S=d({url:r.href,rawUrl:r.rawUrl,resolvedUrl:r.resolvedUrl,linkType:r.linkType,resolutionStatus:r.resolutionStatus}),{url:u,...h}=S;return{...r,...h,href:u}};e.aiOverview&&(e.aiOverview.citations=(e.aiOverview.citations??[]).map(c)),e.aiMode&&(e.aiMode.citations=(e.aiMode.citations??[]).map(c));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 Bs(e,t,n={}){let a=new Map;for(let S of t){let u=a.get(S.rawUrl);u&&Ea(u)||a.set(S.rawUrl,S.linkType==="google_goto_redirect"&&!Ea(S)?{...S,url:S.rawUrl,resolvedUrl:null,resolutionStatus:"unresolved"}:S)}let i=[...a.values()].filter(S=>S.linkType==="google_goto_redirect"&&!Ea(S));if(i.length===0)return a;let s=Date.now()+(n.totalBudgetMs??xs),o=await Ws(i.map(S=>S.rawUrl),n);for(let S of o.resolutions){let u=a.get(S.rawUrl);u&&a.set(S.rawUrl,S.resolutionStatus==="resolved"&&S.resolvedUrl?{...u,url:S.resolvedUrl,resolvedUrl:S.resolvedUrl,resolutionStatus:"resolved"}:{...u,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??Ls,c=await e.context().newPage().catch(()=>null);if(!c)return n.onNetworkAudit?.({nonGoogleRequestsObserved:0}),a;let U=null,r=0;try{await c.route("**/*",async S=>{let u=S.request().url();try{vt(new URL(u).hostname)||(r+=1)}catch{}let h=_a(u);if(!U||h!==U.token){await S.abort("blockedbyclient").catch(()=>{});return}let m=U;try{let I=await S.fetch({maxRedirects:0,timeout:m.timeoutMs});m.location=I.headers().location??null}finally{await S.abort("blockedbyclient").catch(()=>{}),m.finish()}});for(let S of d){let u=s-Date.now();if(u<=0)break;let h=qe(S.rawUrl),m=h?.searchParams.get("url");if(!h||!m){a.set(S.rawUrl,{...S,resolutionStatus:"rejected"});continue}let I=Math.min(l,u);h.hash="";let T=h.href,k=!1;for(let P=0;P<2;P++){let W,E=new Promise(p=>{W=p}),f=`mcp-goto-${Math.random().toString(36).slice(2)}`;U={token:m,timeoutMs:I,location:null,finish:W},await c.evaluate(({url:p,id:b})=>{let y=document.createElement("iframe");y.id=b,y.hidden=!0,y.src=p,document.body.append(y)},{url:T,id:f}).catch(()=>null),await Promise.race([E,new Promise(p=>setTimeout(p,I))]);let O=U.location;if(U=null,await c.evaluate(p=>document.getElementById(p)?.remove(),f).catch(()=>null),!O)break;let A=Os(S.rawUrl,O);if(A){a.set(S.rawUrl,{...S,url:A,resolvedUrl:A,resolutionStatus:"resolved"});break}let _;try{_=new URL(O,T)}catch{k=!0;break}if(!vt(_.hostname)||_a(_.href)!==m){k=!0;break}T=_.href}k&&a.get(S.rawUrl)?.resolutionStatus!=="resolved"&&a.set(S.rawUrl,{...S,resolutionStatus:"rejected"})}}finally{U=null,await c.close().catch(()=>{}),n.onNetworkAudit?.({nonGoogleRequestsObserved:r})}return a}var Hs=String.raw`^(?:an error has occurred(?:[.!…]\s*)?(?:please try again later[.!…]?)?|loading|please wait|try again|something went wrong)[.!…\s]*$`,Bd=700,Hd=1e3,Dd=1400,Ds=900,Fd=3,Fs=250,_n=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=[];emptyPaaLifecycle(t){return{requestedQuestions:t,questionsFound:0,targetStatus:"not_met",discoveryStatus:"in_progress",materialStatus:"partial",completionReason:"PAA expansion is still running.",automaticRetries: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=En(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=[...n.flatMap(u=>u.sources??[]),...a,...i.citations.map(({text:u,href:h,...m})=>({...m,url:h})),...s.citations.map(({text:u,href:h,...m})=>({...m,url:h}))],d=new Map(o.map(u=>[u.rawUrl,u])),l=0,c=await Bs(t,o,{onNetworkAudit:u=>{l=u.nonGoogleRequestsObserved}}),U=u=>({...u,...c.get(u.rawUrl)??u});for(let u of n){u.sources=(u.sources??[]).map(U);let h=u.sources[0];u.source_title=h?.title??u.source_title,u.source_site=h?.site??u.source_site,u.source_cite=h?.url??u.source_cite}for(let u=0;u<a.length;u++){let h=U(a[u]);if(h.resolvedUrl)try{h.domain=new URL(h.resolvedUrl).hostname.replace(/^www\./,"")}catch{}a[u]=h}let r=u=>{let h=U({url:u.href,rawUrl:u.rawUrl,resolvedUrl:u.resolvedUrl,linkType:u.linkType,resolutionStatus:u.resolutionStatus}),{url:m,...I}=h;return{...I,text:u.text,href:m}};i.citations=i.citations.map(r),s.citations=s.citations.map(r);let S=[...c.values()];this.linkDiagnostics={linkCandidatesObserved:d.size,plainLinksObserved:S.filter(u=>u.linkType==="plain").length,googleUrlRedirectsObserved:S.filter(u=>u.linkType==="google_url_redirect").length,googleGotoRedirectsObserved:S.filter(u=>u.linkType==="google_goto_redirect").length,redirectsResolved:S.filter(u=>u.linkType!=="plain"&&u.resolutionStatus==="resolved").length,redirectsUnresolved:S.filter(u=>u.resolutionStatus==="unresolved").length,redirectsRejected:S.filter(u=>u.resolutionStatus==="rejected").length,resolverNonGoogleRequestsObserved:l,paaSourcesPreserved:n.reduce((u,h)=>u+(h.sources??[]).length,0),aiOverviewLabelValidated:i.detected}}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(Y.captchaMarker).count().catch(()=>0)>0)throw new et(`${n} returned a CAPTCHA \u2014 retrying with a fresh session.`)}async extractVisibleItems(t,n){let a=Y,i=await t.evaluate(({selectors:o,onlyQuestion:d,answerPlaceholderPattern:l})=>{function c(r){if(!r)return"";let S=[];for(let u of r.childNodes)if(u.nodeType===Node.TEXT_NODE){let h=u.textContent?.trim();h&&S.push(h)}else{if(u.tagName==="STYLE"||u.tagName==="SCRIPT")continue;{let h=c(u);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),u=r.classList.contains(o.expandedClass)||S?.getAttribute("aria-expanded")==="true",h=r.querySelector(o.answerContainer),I=h?.querySelector(o.aiAnswerBody)??h,T=c(I),k=new RegExp(l,"i").test(T),P=[...Array.from((I??r).querySelectorAll("a[href]")),...Array.from(r.querySelectorAll(o.aiCitation)),...Array.from(r.querySelectorAll(o.aiSourceCard))],W=!k&&(u&&T.length>=10||T.length>30&&P.some(A=>/^https?:\/\//.test(A.href))),E=W&&r.querySelector(o.sourceTitle)?.innerText?.trim()||void 0,f=W&&r.querySelector(o.sourceSite)?.innerText?.trim()||void 0,O=[];if(W){let A=new Set;for(let p of P)!/^https?:\/\//.test(p.href)||A.has(p.href)||(A.add(p.href),O.push({title:U(p.getAttribute("aria-label"))||(p.textContent||"").trim(),rawHref:p.href}));let _=O.find(p=>p.title.length>2)??O[0];_&&(E=E||_.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:W&&T||void 0,sourceTitle:E,sourceSite:f,sources:O}})},{selectors:a,onlyQuestion:n,answerPlaceholderPattern:Hs}),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 u=En(S.rawHref,s);if(!u||l.has(u.rawUrl))continue;l.add(u.rawUrl);let h=o.sourceSite??null;try{h||=new URL(u.resolvedUrl??u.rawUrl).hostname.replace(/^www\./,"")}catch{}d.push({...u,title:S.title||o.sourceTitle||null,site:h})}let c=d[0],U={questionId:qt(o.question),googleLinkId:o.googleLinkId,googleEvidenceId:o.googleEvidenceId,question:o.question,answer:Ps(o.answer,o.question,o.sourceTitle),sourceTitle:c?.title??o.sourceTitle,sourceSite:c?.site??o.sourceSite,sourceCite:c?.url,sources:d},r=ni.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(`${Y.item}[data-q="${n}"], ${Y.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:{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=[];let d=new Set,l=new Map,c=new Set,U=new Set,r=new Map,S=2,u=3,h=0,m={sel:Y.item,dataQ:Y.itemDataQ,dataInitQ:Y.itemDataInitQ,questionEl:Y.itemQuestionEl,answerSel:Y.answerContainer,expandedClass:Y.expandedClass,clickTarget:Y.clickTarget,answerPlaceholderPattern:Hs,questionsOnly:n.questionsOnly,sourceSel:"a[href]"},I=()=>t.evaluate(({sel:f,dataQ:O,dataInitQ:A,questionEl:_,expandedClass:p,clickTarget:b,answerSel:y,answerPlaceholderPattern:L,questionsOnly:C})=>{let N=H=>{if(!H)return"";let x=[];for(let R of H.childNodes){if(R.nodeType===Node.TEXT_NODE){let K=R.textContent?.trim();K&&x.push(K);continue}if(R.nodeType!==Node.ELEMENT_NODE)continue;let J=R;if(J.tagName==="STYLE"||J.tagName==="SCRIPT"||J.getAttribute("aria-hidden")==="true")continue;let st=N(J);st&&x.push(st)}return x.join(" ").replace(/\s+/g," ").trim()};return Array.from(document.querySelectorAll(f)).map((H,x)=>{let R=N(H.querySelector(y)).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(),J=H.classList.contains(p)||H.querySelector(b)?.getAttribute("aria-expanded")==="true",st=R.length>=10&&!new RegExp(L,"i").test(R);return{q:H.getAttribute(O)||H.getAttribute(A)||H.querySelector(_)?.innerText?.trim()||"",done:J&&(C||st),expanded:J,index:x}}).filter(H=>H.q)},m),T=async(f,O,A,_,p,b)=>{let y=Date.now(),L={id:`${b}:${qt(f)}:${p}`,questionId:qt(f),question:f,batchNumber:b,controlAttempt:p,observedAt:new Date(y).toISOString(),beforeQuestionCount:O,expandedBefore:_},C=A>=0?t.locator(m.sel).nth(A).locator(m.clickTarget):null,N=null,H=C?await(async()=>(_&&(await C.click({timeout:Ds,force:!0}),await t.waitForTimeout(120)),await C.click({timeout:Ds,force:!0}),!0))().catch($=>(N=$ instanceof Error&&($.name==="TimeoutError"||/timeout/i.test($.message))?"click_ack_timeout":"click_failed",!1)):!1,x=Date.now()-y;if(!H&&N!=="click_ack_timeout")return{dispatched:!1,confirmed:!1,observation:{...L,dispatched:!1,outcome:"dispatch_failed",confirmationSignals:[],confirmationCheckpointMs:null,confirmedAtMs:null,dispatchDurationMs:x,totalDurationMs:x,afterQuestionCount:O,newQuestionCount:0,expandedAfter:_,answerObserved:!1,sourceCount:0,errorCode:A<0?"control_missing":N??"click_failed"}};let R={exists:!0,expanded:_,answerReady:!1,sourceCount:0,questionCount:O},J=[],st=null,K=null,q=!1;for(let $ of[Bd,Hd,Dd]){let $t=y+$-Date.now();if($t>0&&await t.waitForTimeout($t),R=await t.evaluate(({sel:Pe,dataQ:_t,dataInitQ:ze,questionEl:Qt,expandedClass:$e,clickTarget:Ee,answerSel:Fn,answerPlaceholderPattern:Qe,sourceSel:po,target:mo})=>{let Xa=Zt=>{if(!Zt)return"";let Vn=[];for(let tn of Zt.childNodes){if(tn.nodeType===Node.TEXT_NODE){let Za=tn.textContent?.trim();Za&&Vn.push(Za);continue}if(tn.nodeType!==Node.ELEMENT_NODE)continue;let en=tn;if(en.tagName==="STYLE"||en.tagName==="SCRIPT"||en.getAttribute("aria-hidden")==="true")continue;let Qa=Xa(en);Qa&&Vn.push(Qa)}return Vn.join(" ").replace(/\s+/g," ").trim()},Gn=Array.from(document.querySelectorAll(Pe)),Ze=Gn.find(Zt=>(Zt.getAttribute(_t)||Zt.getAttribute(ze)||Zt.querySelector(Qt)?.innerText?.trim())===mo);if(!Ze)return{exists:!1,expanded:!1,answerReady:!1,sourceCount:0,questionCount:Gn.length};let yo=Ze.classList.contains($e)||Ze.querySelector(Ee)?.getAttribute("aria-expanded")==="true",ja=Ze.querySelector(Fn),za=Xa(ja).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(),$a=za.length>=10&&!new RegExp(Qe,"i").test(za);return{exists:!0,expanded:yo,answerReady:$a,sourceCount:$a?ja?.querySelectorAll(po).length??0:0,questionCount:Gn.length}},{...m,target:f}).catch(()=>R),J=[...R.expanded?["expanded"]:[],...R.answerReady?["answer"]:[],...R.sourceCount>0?["source"]:[],...R.questionCount>O?["question_growth"]:[]],R.answerReady||R.sourceCount>0||R.questionCount>O){q=!0,st=$,K=Date.now()-y;break}}let ut=Date.now()-y,F={...L,dispatched:H,outcome:q?"confirmed":H?"unconfirmed":"dispatch_failed",confirmationSignals:J,confirmationCheckpointMs:st,confirmedAtMs:K,dispatchDurationMs:x,totalDurationMs:ut,afterQuestionCount:R.questionCount,newQuestionCount:Math.max(0,R.questionCount-O),expandedAfter:R.expanded,answerObserved:R.answerReady,sourceCount:R.sourceCount,errorCode:H?q?null:R.exists?"confirmation_timeout":"control_missing":N};return{dispatched:H,confirmed:q,observation:F}},k=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 Yt(this.paaLifecycle.completionReason);this.throwIfAborted(a);let f=await I(),O=!1;for(let y of f)s.has(y.q)||(s.add(y.q),o.push(y.q),o.length<=n.maxQuestions&&(O=this.upsertCollectedRow(this.toFlatRow({questionId:qt(y.q),googleLinkId:null,googleEvidenceId:null,question:y.q,sources:[]},1,null,n.query))||O)),y.done&&c.add(y.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:c.size,failedInteractions:U.size,visibleControls:f.length},O&&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 y=!1,L=new Set(o.slice(0,n.maxQuestions));for(let C of await this.extractVisibleItems(t)){if(!L.has(C.question))continue;let N=r.get(C.question),H={...C,answer:C.answer??N?.answer,sourceTitle:C.sourceTitle??N?.sourceTitle,sourceSite:C.sourceSite??N?.sourceSite,sourceCite:C.sourceCite??N?.sourceCite,sources:this.mergePaaSources(N?.sources,C.sources)};r.set(C.question,H),y=this.upsertCollectedRow(this.toFlatRow(H,1,null,n.query))||y}if(y&&await this.emitProgress("answer_captured"),[...L].every(C=>{let N=r.get(C);return!!(N?.answer?.trim()&&N.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 A=new Set(o.slice(0,n.maxQuestions)),_=f.filter(y=>A.has(y.q)&&!y.done&&(l.get(y.q)??0)<S&&!d.has(y.q)),p=_.slice(-Fd);if(this.paaLifecycle={...this.paaLifecycle,eligibleControls:_.length},p.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 y=new Set(f.map(C=>C.q)),L=o.slice(0,n.maxQuestions).filter(C=>!c.has(C)&&(U.has(C)||!y.has(C)));if(L.length>0)throw this.paaLifecycle={...this.paaLifecycle,discoveryStatus:"interrupted",failedInteractions:new Set([...U,...L]).size,completionReason:"At least one known PAA control failed or disappeared before it could be confirmed."},await this.emitProgress("expansion_finished"),new Yt(this.paaLifecycle.completionReason);for(let C=1;C<=u;C++){this.throwIfAborted(a),await this.throwIfCaptcha(t,"Google PAA exhaustion confirmation"),await t.mouse.wheel(0,320);let N=await t.waitForFunction(({sel:x,min:R})=>document.querySelectorAll(x).length>R,{sel:Y.item,min:f.length},{timeout:1500,polling:100}).catch(()=>null);if(this.paaLifecycle={...this.paaLifecycle,noGrowthConfirmations:C},N||(await I()).some(x=>!s.has(x.q)))continue t}this.paaLifecycle={...this.paaLifecycle,discoveryStatus:"frontier_exhausted",targetStatus:"not_met",failedInteractions:0,noGrowthConfirmations:u,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 y of p)this.reporter.onDepth(++k),(l.get(y.q)??0)>0&&(this.paaLifecycle={...this.paaLifecycle,automaticRetries:this.paaLifecycle.automaticRetries+1}),l.set(y.q,(l.get(y.q)??0)+1);let b=await Promise.all(p.map(async y=>({target:y,result:await T(y.q,f.length,y.index,y.expanded,l.get(y.q)??1,h)})));for(let{target:y,result:L}of b)(L.dispatched||L.confirmed)&&this.clickedQuestions.add(y.q),this.interactionObservations.push(L.observation);if(this.interactionObservations.length>Fs&&this.interactionObservations.splice(0,this.interactionObservations.length-Fs),!n.questionsOnly){let y=new Set(p.map(L=>L.q));for(let L of await this.extractVisibleItems(t))y.has(L.question)&&(r.set(L.question,L),this.upsertCollectedRow(this.toFlatRow(L,1,null,n.query))&&await this.emitProgress("answer_captured"))}for(let{target:y,result:L}of b){let C=r.get(y.q);L.confirmed||C?.answer?.trim()?(c.add(y.q),U.delete(y.q)):(l.get(y.q)??0)>=S&&(U.add(y.q),d.add(y.q))}this.paaLifecycle={...this.paaLifecycle,processedQuestions:c.size,failedInteractions:U.size,noGrowthConfirmations:0,dispatchedInteractions:this.interactionObservations.filter(y=>y.dispatched).length,confirmedInteractions:this.interactionObservations.filter(y=>y.outcome==="confirmed").length,unconfirmedInteractions:this.interactionObservations.filter(y=>y.outcome!=="confirmed").length},await this.emitProgress("click_observed")}let P=n.questionsOnly?new Map:new Map(r);n.questionsOnly||await this.fillIncompleteItems(t,o,P,n,d);let W=[];for(let f of o){if(W.length>=n.maxQuestions)break;let O=this.normalizeQuestion(f);if(i.has(O))continue;i.add(O);let A=P.get(f);if(A){let _=this.toFlatRow(A,1,null,n.query);this.upsertCollectedRow(_),W.push(_),this.reporter.onQuestion({questionId:A.questionId,googleLinkId:A.googleLinkId,googleEvidenceId:A.googleEvidenceId,question:A.question,answer:A.answer??null,sourceTitle:A.sourceTitle??null,sourceSite:A.sourceSite??null,sourceCite:A.sourceCite??null,sources:A.sources,depth:1,parentQuestion:null,children:[]})}else{let _=this.toFlatRow({questionId:qt(f),googleLinkId:null,googleEvidenceId:null,question:f,answer:void 0,sourceTitle:void 0,sourceSite:void 0,sourceCite:void 0,sources:[]},1,null,n.query);this.upsertCollectedRow(_),W.push(_)}}this.completeness.paaWithoutAnswer=n.questionsOnly?0:W.filter(f=>!f.answer).length,this.completeness.paaWithoutSource=n.questionsOnly?0:W.filter(f=>(f.sources??[]).length===0).length,this.completeness.paaRequested=n.maxQuestions,this.completeness.paaReturned=W.length,this.completeness.paaUnique=i.size;let E=!n.questionsOnly&&(this.completeness.paaWithoutAnswer>0||this.completeness.paaWithoutSource>0);return this.paaLifecycle={...this.paaLifecycle,questionsFound:W.length,targetStatus:W.length>=n.maxQuestions?"met":"not_met",materialStatus:E?"partial":"complete",processedQuestions:c.size,failedInteractions:U.size},await this.emitProgress("fill_incomplete"),await this.emitProgress("expansion_finished"),W}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)))),c=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 u=a.get(U);u&&this.upsertCollectedRow(this.toFlatRow(u,1,null,i.query))&&await this.emitProgress("fill_incomplete")}this.completeness.paaAnswersRecovered=[...c].filter(U=>a.get(U)?.answer).length}}async reExpandCollapsedItems(t){let n=`${Y.item}:not(.${Y.expandedClass}) ${Y.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=Yi;return t.evaluate(a=>{let i=[],s=Array.from(document.querySelectorAll(a.container));for(let o of s){let c=(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 u=r.textContent?.trim()??"",h=u.indexOf("YouTube");if(h===-1)continue;let m=u.slice(0,h).trim(),T=u.slice(h+7).replace(/^[·\s·]+/,"").match(/^([^·\n]+)/),k=T?T[1].trim():"";m&&i.push({type:c,title:m,channel:k,platform:"YouTube",duration:"",url:S})}}return i},n)}async extractForums(t){let n=Ki;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 c=l.href;if(!c)continue;let U=l.querySelector(a.title),r=l.querySelector(a.source),S=U?.textContent?.trim()??"",u=r?.textContent?.trim()??"";S&&i.push({title:S,source:u,url:c})}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:mn.item,platforms:[...mn.platforms]};return(await t.evaluate(s=>{let o=new Set,d=[],l=Array.from(document.querySelectorAll(s.item)),c=["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||!c.some(h=>S.includes(h)))continue;let u=r.textContent?.trim()??"";U.has(S)||U.set(S,[]),U.get(S).push(u)}for(let[r,S]of U.entries()){if(o.has(r))continue;o.add(r);let u=S.find(k=>/^\d+:\d+$/.test(k))??"",h=S.find(k=>!/^\d+:\d+$/.test(k)&&k.length>5)??"";if(!h)continue;let m=h,I="",T="";for(let k of s.platforms){let P=-1,W=0;for(;;){let _=h.indexOf(k,W);if(_===-1)break;P=_,W=_+1}if(P===-1)continue;let E=h.slice(P+k.length);if(!(/^[\s·]/.test(E)||E.trim()===""))continue;m=h.slice(0,P).trim(),I=k;let O=E.replace(/^[\s·]+/,""),A=O.indexOf("\xB7");T=(A===-1?O:O.slice(0,A)).trim();break}m&&d.push({title:m,channel:T,platform:I,duration:u,url:r})}return d},a)).map(s=>({type:"short_video",...s}))}async extractWhatPeopleSaying(t){let n=Ji;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(),c=s.querySelector(a.titleDiv)?.textContent?.trim(),U=l??c??"",r=s.querySelector(a.source)?.textContent?.trim()??"",u=s.querySelector(a.platformBadge)?.textContent?.trim()??"",h=s.querySelector(a.ytChannel)?.textContent?.trim()??"",m=s.querySelector(a.ytDate)?.textContent?.trim()??"",I=s.querySelector(a.authorNote)?.textContent?.trim()??null,T=s.querySelector(a.popularCommentLabel),k=null;if(T){let N=T.nextSibling;for(;N;){let H=N.textContent?.trim();if(H){k=H;break}N=N.nextSibling}}let P=Array.from(s.querySelectorAll("span")),W=P.find(N=>/^\d+:\d+$/.test(N.textContent?.trim()??""))?.textContent?.trim()??null,f=P.map(N=>N.textContent?.trim()??"").filter(N=>/\d/.test(N)&&(N.includes("comment")||N.includes("reaction")||N.includes("view")||N.includes("like")||N.includes("share")))[0]??"",O=P.map(N=>N.textContent?.trim()??"").filter(N=>/\d+ (day|week|month|year|hour)s? ago|Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec/.test(N)),A=m||(O[0]??""),_=u||(h?"YouTube":""),p=h||r,b="unknown",y=_.toLowerCase(),L=p.toLowerCase(),C=r.toLowerCase();return y.includes("reddit")||L.startsWith("r/")?b="reddit":y.includes("facebook")||C.includes("facebook")?b="facebook":y.includes("instagram")||C.includes("instagram")?b="instagram":y.includes("tiktok")||C.includes("tiktok")?b="tiktok":y.includes("youtube")||h?b="youtube":b="news",{type:b,title:U,url:d,source:p,platform:_,popularComment:k,engagement:f,date:A,duration:W,authorNote:I}}):[]},n)}async extractOrganicResults(t){let n=ji,a=await t.evaluate(s=>{let o=[],d=0;return document.querySelectorAll(s.result).forEach(l=>{let c=l.querySelector(s.title);if(!c)return;let U=c.textContent?.trim()??"",S=c.closest("a")?.href??"";if(!U||!S)return;d++;let u=l.querySelector(s.cite)?.textContent?.trim()??null,h=l.querySelector(s.snippet)?.textContent?.trim()??null,m=!!l.querySelector(s.redditCite),I=l.querySelector(s.ratingWrap),T=I?{value:I.querySelector(s.ratingValue)?.textContent?.trim()??"",count:I.querySelector(s.reviewCount)?.textContent?.trim()??""}:null,k="";try{k=new URL(S).hostname.replace(/^www\./,"")}catch{k=l.querySelector(s.siteName)?.textContent?.trim()??""}o.push({position:d,title:U,url:S,domain:k,cite:u,snippet:h,isRedditStyle:m,inlineRating:T})}),o},n),i=typeof t.url=="function"?t.url():"https://www.google.com/";return a.flatMap(s=>{let o=En(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=zi;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 c=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 T of Array.from(o.querySelectorAll("a[href]"))){let k=T.href.match(/[?&]cid=(\d+)/);if(k){S=k[1];break}let P=T.href.match(/!1s0x[0-9a-f]+:0x([0-9a-f]+)/i);if(P){try{S=BigInt("0x"+P[1]).toString()}catch{}if(S)break}}let u=[];o.querySelectorAll("div, span").forEach(T=>{let k=Array.from(T.childNodes).filter(P=>P.nodeType===3).map(P=>P.textContent?.trim()??"").filter(P=>P.length>1&&P.length<120).join(" ");k&&!u.includes(k)&&u.push(k)});let h=Array.from(o.querySelectorAll("a[href]")),m=h.find(T=>T.href.includes("google.com/maps"))?.href??null,I=h.find(T=>!T.href.includes("google.com")&&T.href.startsWith("http"))?.href??null;i.push({position:d+1,name:l,cid:S,rating:c,reviewCount:r,metadata:u,websiteUrl:I,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 c=[".OSrXXb",".dbg0pd",".tzt0oe",'[role="heading"]',"h3"];for(let U of c){let r=l.querySelector(U);if(r?.textContent?.trim())return r.textContent.trim()}return""}document.querySelectorAll('[id^="pv-/g/"]').forEach(l=>{let c=l.getAttribute("id");if(!c)return;let U=c.replace("pv-","");n.add(U);let r=o(l),u=l.querySelector("a[data-cid]")?.getAttribute("data-cid")??null;u&&a.add(u),r&&s.set(U,{name:r,kgId:U,cid:u,gcid:null})}),document.querySelectorAll("[data-mid]").forEach(l=>{let c=l.getAttribute("data-mid");if(c?.startsWith("/g/")&&(n.add(c),!s.has(c))){let U=o(l);U&&s.set(c,{name:U,kgId:c,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/"]'),u=S?S.getAttribute("id").replace("pv-",""):null,h=u??`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:u,cid:U,gcid:null})}),document.querySelectorAll("a[data-cid]").forEach(l=>{let c=l.getAttribute("data-cid");if(!c)return;if(a.add(c),![...s.values()].some(r=>r.cid===c)){let r=l.parentElement,S="";for(let u=0;u<8&&r;u++){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:${c}`,{name:S,kgId:null,cid:c,gcid:null})}});let d=Array.from(document.querySelectorAll("script:not([src])")).map(l=>l.textContent??"").filter(l=>l.length>1e4).join(`
|
|
21785
|
-
`);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(Es,{aio:qi,aim:Xi,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 c=d.querySelector('[aria-label="Share"]');if(c)return c.scrollIntoView({block:"center"}),c.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=c=>{try{let U=new URL(c,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 c=>{if(await c.route("**/*",async U=>{let r=U.request();r.isNavigationRequest()&&r.frame()===c.mainFrame()&&!o(r.url())?(i=/^https:\/\/www\.google\.com\/sorry(?:\/|\?)/.test(r.url())?"captcha":"invalid_next",await U.abort("blockedbyclient")):await U.fallback()}),await c.goto(new URL(d,s).href,{waitUntil:"domcontentloaded",timeout:2e4}),this.throwIfAborted(a),await this.throwIfCaptcha(c,"Google SERP page 2"),!o(c.url()))throw i="invalid_next",new Error("Invalid pagination landing");return this.extractOrganicResults(c)},a);return this.throwIfAborted(a),l.length>0?{organic:l.map(c=>({...c,position:c.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 et?"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),await this.driver.launch(s);let l=this.driver.getProviderSessionMetadata?.();if(l?.providerSessionId&&this.onProviderSession)try{await this.onProviderSession(l)}catch(F){console.warn(JSON.stringify({event:"paa_provider_session_sink_failed",provider:l.provider,message:F instanceof Error?F.message:String(F)}))}this.throwIfAborted(n);let c=t.location?ba(t.location):null,U=t.location?c?.canonical??t.location:null,r=U?fs(U):null,S=t,u=t.includeAllSerpFeatures,h=u||t.includeLocalPack,m=u||t.includeForums,I=u||t.includeVideos,T=u||t.includeAiOverview,k=u||t.includeWhatPeopleSaying,P={surface:"web",aiOverview:{detected:!1,text:null,citations:[]},aiMode:{detected:!1,text:null,citations:[]}},{hasPaa:W}=await this.driver.navigateToSERP(S.query,r,t.gl,t.hl,{..._e(t.recency)?{tbs:_e(t.recency)}:{},debug:t.debug});this.throwIfAborted(n);let E=this.driver.getPage();if(await this.throwIfCaptcha(E,"Google SERP"),W||(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 F=W?await this.runBFS(E,S,n):[];await this.resolveMaterialLinks(E,F,[],P.aiOverview,P.aiMode);let $={seed:S.query,totalQuestions:F.length,maxDepthReached:F.reduce(($t,Pe)=>Math.max($t,Pe.depth),0),durationMs:Date.now()-a,errorCount:o};return this.reporter.onComplete($),{seed:S.query,location:t.location??null,extractedAt:new Date().toISOString(),diagnostics:{completionStatus:W?"paa_found":"no_paa",...W?{}:{noPaaObserved:!0},problem:null,paaLifecycle:{...this.paaLifecycle},completeness:{...this.completeness},links:{...this.linkDiagnostics},...t.debug?{debug:this.buildHarvestDebugSnapshot(S,U,r,void 0,c)}:{}},totalQuestions:F.length,surface:"web",aiOverview:{detected:!1,text:null,citations:[]},aiMode:{detected:!1,text:null,citations:[]},whatPeopleSaying:[],tree:this.buildTree(F,S.query),flat:F,videos:[],forums:[],organicResults:[],localPack:[],entityIds:{entities:[],kgIds:[],cids:[],gcids:[]},stats:$}}if(T&&(this.aioShareUrlEarly=await this.captureAIOverviewShareUrl(E,t).catch(()=>null)),t.serpOnly){let[F,$,$t]=await Promise.all([this.extractOrganicResults(E),h?this.extractLocalPack(E):Promise.resolve([]),this.extractEntityIds(E)]),Pe=this.mergeLocalPackIntoEntities($t,$),_t=T?await this.extractAISurfaces(E,t):P,ze=t.debug?Je(U,F,$):void 0,Qt=F;if((t.pages??1)>=2){let Ee=new URLSearchParams({q:S.query,gl:t.gl,hl:t.hl,pws:"0",start:"10"});_e(t.recency)&&Ee.set("tbs",_e(t.recency)),r&&Ee.set("uule",r),await this.driver.navigateTo("https://www.google.com/search?"+Ee.toString()),await this.throwIfCaptcha(E,"Google SERP page 2");let Fn=await this.extractOrganicResults(E);Qt=[...F,...Fn.map(Qe=>({...Qe,position:Qe.position+10}))],t.debug&&(ze=Je(U,Qt,$))}await this.resolveMaterialLinks(E,[],Qt,_t.aiOverview,_t.aiMode);let $e={seed:S.query,totalQuestions:0,maxDepthReached:0,durationMs:Date.now()-a,errorCount:o};return this.reporter.onComplete($e),{seed:S.query,location:t.location??null,extractedAt:new Date().toISOString(),diagnostics:{completionStatus:"serp_only",problem:null,completeness:{...this.completeness},links:{...this.linkDiagnostics},...t.debug?{debug:this.buildHarvestDebugSnapshot(S,U,r,ze,c)}:{}},totalQuestions:0,surface:_t.surface,aiOverview:_t.aiOverview,aiMode:_t.aiMode,whatPeopleSaying:[],tree:[],flat:[],videos:[],forums:[],organicResults:Qt,localPack:$,entityIds:Pe,stats:$e}}let[f,O,A,_,p,b]=await Promise.all([I?this.extractVideos(E):Promise.resolve([]),m?this.extractForums(E):Promise.resolve([]),k?this.extractWhatPeopleSaying(E):Promise.resolve([]),this.extractEntityIds(E),this.extractOrganicResults(E),h?this.extractLocalPack(E):Promise.resolve([])]),y=this.mergeLocalPackIntoEntities(_,b),L=t.debug?Je(U,p,b):void 0;this.reporter.onVideos(f),this.reporter.onForums(O);let C=T?await this.extractAISurfaces(E,t):P,N={requestedPages:(t.pages??1)>=2?2:1,capturedPages:1,page2Status:(t.pages??1)>=2?"not_attempted":"not_requested",page1OrganicCount:p.length,page2OrganicCount:0},H=p;if(this.checkpointMaterial={surface:C.surface,aiOverview:C.aiOverview,aiMode:C.aiMode,whatPeopleSaying:A,videos:f,forums:O,organicResults:H,localPack:b,entityIds:y,pagination:N},await this.emitProgress("serp_captured"),N.requestedPages===2){let F=await this.captureSecondOrganicPage(E,S,n);H=[...p,...F.organic],N={...N,capturedPages:F.status==="captured"?2:1,page2Status:F.status,page2OrganicCount:F.organic.length,...F.failureCode?{failureCode:F.failureCode}:{}},this.checkpointMaterial={...this.checkpointMaterial,organicResults:H,pagination:N},await this.emitProgress("serp_captured"),await this.resolveMaterialLinks(E,[],H,C.aiOverview,C.aiMode),this.checkpointMaterial={...this.checkpointMaterial,organicResults:H},await this.emitProgress("serp_captured")}let x=t.debug?Je(U,H,b):L;if(!W){await this.resolveMaterialLinks(E,[],H,C.aiOverview,C.aiMode),this.checkpointMaterial={...this.checkpointMaterial,organicResults:H},await this.emitProgress("serp_captured");let F={seed:S.query,totalQuestions:0,maxDepthReached:0,durationMs:Date.now()-a,errorCount:o};return this.reporter.onComplete(F),{seed:S.query,location:t.location??null,extractedAt:new Date().toISOString(),diagnostics:{completionStatus:"no_paa",noPaaObserved:!0,problem:null,pagination:N,paaLifecycle:{...this.paaLifecycle},completeness:{...this.completeness},links:{...this.linkDiagnostics},...t.debug?{debug:this.buildHarvestDebugSnapshot(S,U,r,x,c)}:{}},totalQuestions:0,surface:C.surface,aiOverview:C.aiOverview,aiMode:C.aiMode,whatPeopleSaying:A,tree:[],flat:[],videos:f,forums:O,organicResults:H,localPack:b,entityIds:y,stats:F}}let R=await this.runBFS(E,S,n);this.throwIfAborted(n);let J=new URLSearchParams({q:S.query,gl:t.gl,hl:t.hl,pws:"0",udm:mn.udm});r&&J.set("uule",r);let st=[];if(t.includeShortVideos!==!1)try{st=await this.extractShortVideos(E,"https://www.google.com/search?"+J.toString())}catch(F){if(!(F instanceof et))throw F;o++,d.push({code:"short_videos_captcha_skipped",surface:"short_videos",message:F.message,retryable:!0})}this.reporter.onVideos(st),await this.resolveMaterialLinks(E,R,H,C.aiOverview,C.aiMode),this.checkpointMaterial={...this.checkpointMaterial,surface:C.surface,aiOverview:C.aiOverview,aiMode:C.aiMode,videos:[...f,...st],organicResults:H},await this.emitProgress("expansion_finished");let K=[...f,...st],q=this.buildTree(R,S.query),ut={seed:S.query,totalQuestions:R.length,maxDepthReached:R.reduce((F,$)=>Math.max(F,$.depth),0),durationMs:Date.now()-a,errorCount:o};return this.reporter.onComplete(ut),{seed:S.query,location:t.location??null,extractedAt:new Date().toISOString(),diagnostics:{completionStatus:"paa_found",problem:null,pagination:N,paaLifecycle:{...this.paaLifecycle},completeness:{...this.completeness},links:{...this.linkDiagnostics},...d.length>0?{warnings:d}:{},...t.debug?{debug:this.buildHarvestDebugSnapshot(S,U,r,x,c)}:{}},totalQuestions:R.length,surface:C.surface,aiOverview:C.aiOverview,aiMode:C.aiMode,whatPeopleSaying:A,tree:q,flat:R,videos:K,forums:O,organicResults:H,localPack:b,entityIds:y,stats:ut}}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 at=require("fs"),bt=an(require("path"),1),Xt=an(require("papaparse"),1);async function Gs(e){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 In=class{async writeJSON(t,n){await at.promises.mkdir(n,{recursive:!0});let i=`${t.seed.toLowerCase().replace(/\W+/g,"-").slice(0,40)}-${Date.now()}.json`,s=bt.default.join(n,i);return await at.promises.writeFile(s,JSON.stringify(t,null,2),"utf8"),await Gs({title:`${t.seed} scrape`,content:JSON.stringify(t),source:`mcp-scraper:${s}`}),s}async writeCSV(t,n){await at.promises.mkdir(n,{recursive:!0});let i=(t[0]?.seed_query??"paa").toLowerCase().replace(/\W+/g,"-").slice(0,40),s=Xt.default.unparse(t.map(({sources:l,...c})=>({...c,sources_json:JSON.stringify(l)})),{header:!0}),o=`${i}-${Date.now()}.csv`,d=bt.default.join(n,o);return await at.promises.writeFile(d,s,"utf8"),d}async writeVideoCSV(t,n,a){await at.promises.mkdir(a,{recursive:!0});let i=n.toLowerCase().replace(/\W+/g,"-").slice(0,40),s=Xt.default.unparse(t,{header:!0}),o=`${i}-videos-${Date.now()}.csv`,d=bt.default.join(a,o);return await at.promises.writeFile(d,s,"utf8"),d}async writeForumCSV(t,n,a){await at.promises.mkdir(a,{recursive:!0});let i=n.toLowerCase().replace(/\W+/g,"-").slice(0,40),s=Xt.default.unparse(t,{header:!0}),o=`${i}-forums-${Date.now()}.csv`,d=bt.default.join(a,o);return await at.promises.writeFile(d,s,"utf8"),d}async writeAIOverviewCSV(t,n,a,i){await at.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=Xt.default.unparse(o,{header:!0}),l=`${s}-ai-overview-${Date.now()}.csv`,c=bt.default.join(i,l);return await at.promises.writeFile(c,d,"utf8"),c}async writeAIModeCSV(t,n,a,i){await at.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=Xt.default.unparse(o,{header:!0}),l=`${s}-ai-mode-${Date.now()}.csv`,c=bt.default.join(i,l);return await at.promises.writeFile(c,d,"utf8"),c}async writeWhatPeopleSayingCSV(t,n,a){await at.promises.mkdir(a,{recursive:!0});let i=n.toLowerCase().replace(/\W+/g,"-").slice(0,40),s=t.map(c=>({seed_query:n,...c})),o=Xt.default.unparse(s,{header:!0}),d=`${i}-what-people-saying-${Date.now()}.csv`,l=bt.default.join(a,d);return await at.promises.writeFile(l,o,"utf8"),l}};var An=class{onQuestion(t){process.stdout.write(JSON.stringify({event:"question",depth:t.depth,question:t.question})+`
|
|
21882
|
+
`).trim();return!H||/not available|try again|can't generate/i.test(H)?null:H}function c(p){if(!p||p.startsWith("javascript:"))return null;try{let b=new URL(p,window.location.href);return!["http:","https:"].includes(b.protocol)||b.username||b.password?null:{href:b.href,rawHref:b.href}}catch{return null}}function U(p){if(!p)return[];let b=new Set,y=[];for(let L of Array.from(p.querySelectorAll("a[href]"))){let C=c(L.getAttribute("href")??"");if(!C||b.has(C.rawHref))continue;b.add(C.rawHref);let N="";try{N=new URL(C.href).hostname.replace(/^www\./,"")}catch{}y.push({text:i(L)||N||C.href,href:C.href,rawHref:C.rawHref})}return y}async function r(p){let b=p.querySelector(t.aio.showMoreButton);if(!b||b.getAttribute("aria-expanded")!=="false")return!1;b.click();let y=t.expandWaitMs??1500;return y>0&&await new Promise(L=>setTimeout(L,y)),!0}let S=d(),u=null,h=[],m=!1,I=!1,f=[];if(S){m=await r(S);let p=S.querySelector(t.aio.controller),b=S.querySelector(t.aio.contentSubtree),y=S.querySelector(t.aio.showMoreButton);I=p?.getAttribute("data-trnct")==="false"||y?.getAttribute("aria-expanded")==="true"||!y,u=l(b??p??S),f=(u??"").split(`
|
|
21883
|
+
`).map(L=>L.trim()).filter(L=>/^\d+\.\s+.+/.test(L)),h=U(S)}let k=document.querySelector(t.aim.root),_=a==="aim"&&!!k,W=k?.closest(t.aim.wrapper)??k,E=_?l(W):null,M=_&&s(E),O=M?E:null,A=M?U(W):[],P=!!S&&s(u);return{surface:a,aiOverview:{detected:P,text:P?u:null,citations:P?h:[],expanded:P?m:!1,fullyExpanded:P?I:!1,sections:P?f:[]},aiMode:{detected:M&&O!==null,text:O,citations:A}}}var As=require("crypto"),Ls=8e3,xs=3e3,Os=1e4,Rd=8;function bt(e){return/(^|\.)google\.(?:com|[a-z]{2,3}(?:\.[a-z]{2})?)$/i.test(e)}function je(e){if(!e||e.length>Ls)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 Bd(e,t){if(!e||e.length>Ls||/^javascript:/i.test(e))return null;try{let n=new URL(e,t);return je(n.href)}catch{return null}}function Xe(e){let t=je(e);return!t||t.protocol!=="https:"||!bt(t.hostname)||t.pathname!=="/goto"?null:t.searchParams.get("url")?t:null}function Pa(e){if(e.linkType!=="google_goto_redirect"||e.resolutionStatus!=="resolved"||!Xe(e.rawUrl)||!e.resolvedUrl)return!1;let t=je(e.resolvedUrl);return!!(t&&!bt(t.hostname)&&t.href===e.resolvedUrl&&e.url===t.href)}function Xt(e){let t=e.normalize("NFKC").toLocaleLowerCase("en-US").replace(/[^\p{L}\p{N}\s]/gu,"").replace(/\s+/g," ").trim();return`paa_${(0,As.createHash)("sha256").update(t).digest("hex").slice(0,16)}`}function Pn(e,t){let n=Bd(e,t);if(!n)return null;let a=n.href;if(!bt(n.hostname))return{url:a,rawUrl:a,resolvedUrl:a,linkType:"plain",resolutionStatus:"not_needed"};if(n.pathname==="/url"){let i=je(n.searchParams.get("q")??n.searchParams.get("url")??"");return!i||bt(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 Ia(e){return Xe(e)?.searchParams.get("url")??null}function Ws(e,t){try{let n=new URL(e),a=je(new URL(t,n).href);return!a||bt(a.hostname)?null:(!a.hash&&n.hash&&(a.hash=n.hash),a.href)}catch{return null}}async function Rs(e,t={}){let n=[...new Set([...e].flatMap(r=>Xe(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??xs),d=Date.now()+Math.max(1,t.totalBudgetMs??Os),l=Math.max(1,Math.min(16,Math.floor(t.concurrency??Rd))),c=0,U=async()=>{for(;c<n.length;){let r=n[c++],S=Xe(r),u=S?.searchParams.get("url");if(!S||!u){a.set(r,{rawUrl:r,resolvedUrl:null,resolutionStatus:"rejected"});continue}S.hash="";let h=S.href;for(let m=0;m<2;m++){let I=d-Date.now();if(I<=0)return;let f=new AbortController,k=setTimeout(()=>f.abort(),Math.min(o,I)),_;try{_=await s(h,{method:"GET",redirect:"manual",headers:{accept:"*/*","user-agent":"Mozilla/5.0"},signal:f.signal})}catch{break}finally{clearTimeout(k)}if(_.status<300||_.status>=400)break;let W=_.headers.get("location");if(!W)break;let E=Ws(r,W);if(E){a.set(r,{rawUrl:r,resolvedUrl:E,resolutionStatus:"resolved"});break}let M;try{M=new URL(W,h)}catch{a.set(r,{rawUrl:r,resolvedUrl:null,resolutionStatus:"rejected"});break}if(!bt(M.hostname)||Ia(M.href)!==u){a.set(r,{rawUrl:r,resolvedUrl:null,resolutionStatus:"rejected"});break}h=M.href}}};return await Promise.all(Array.from({length:Math.min(l,n.length)},()=>U())),{resolutions:[...a.values()],nonGoogleRequestsObserved:0}}function Is(e,t){try{return new URL(e.resolvedUrl??e.url).hostname.replace(/^www\./,"")}catch{return t}}async function Bs(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,...u})=>({...u,url:S})),...(e.aiMode?.citations??[]).map(({text:r,href:S,...u})=>({...u,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 Rs(o,t);for(let S of r.resolutions){let u=s.get(S.rawUrl);u&&s.set(S.rawUrl,S.resolutionStatus==="resolved"&&S.resolvedUrl?{...u,url:S.resolvedUrl,resolvedUrl:S.resolvedUrl,resolutionStatus:"resolved"}:{...u,resolutionStatus:S.resolutionStatus})}}let d=r=>({...r,...s.get(r.rawUrl)??r}),l=r=>r.map(S=>{let u=d(S);return{...u,site:Is(u,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:Is(S,e.organicResults[r].domain)??""}}let c=r=>{let S=d({url:r.href,rawUrl:r.rawUrl,resolvedUrl:r.resolvedUrl,linkType:r.linkType,resolutionStatus:r.resolutionStatus}),{url:u,...h}=S;return{...r,...h,href:u}};e.aiOverview&&(e.aiOverview.citations=(e.aiOverview.citations??[]).map(c)),e.aiMode&&(e.aiMode.citations=(e.aiMode.citations??[]).map(c));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 Hs(e,t,n={}){let a=new Map;for(let S of t){let u=a.get(S.rawUrl);u&&Pa(u)||a.set(S.rawUrl,S.linkType==="google_goto_redirect"&&!Pa(S)?{...S,url:S.rawUrl,resolvedUrl:null,resolutionStatus:"unresolved"}:S)}let i=[...a.values()].filter(S=>S.linkType==="google_goto_redirect"&&!Pa(S));if(i.length===0)return a;let s=Date.now()+(n.totalBudgetMs??Os),o=await Rs(i.map(S=>S.rawUrl),n);for(let S of o.resolutions){let u=a.get(S.rawUrl);u&&a.set(S.rawUrl,S.resolutionStatus==="resolved"&&S.resolvedUrl?{...u,url:S.resolvedUrl,resolvedUrl:S.resolvedUrl,resolutionStatus:"resolved"}:{...u,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??xs,c=await e.context().newPage().catch(()=>null);if(!c)return n.onNetworkAudit?.({nonGoogleRequestsObserved:0}),a;let U=null,r=0;try{await c.route("**/*",async S=>{let u=S.request().url();try{bt(new URL(u).hostname)||(r+=1)}catch{}let h=Ia(u);if(!U||h!==U.token){await S.abort("blockedbyclient").catch(()=>{});return}let m=U;try{let I=await S.fetch({maxRedirects:0,timeout:m.timeoutMs});m.location=I.headers().location??null}finally{await S.abort("blockedbyclient").catch(()=>{}),m.finish()}});for(let S of d){let u=s-Date.now();if(u<=0)break;let h=Xe(S.rawUrl),m=h?.searchParams.get("url");if(!h||!m){a.set(S.rawUrl,{...S,resolutionStatus:"rejected"});continue}let I=Math.min(l,u);h.hash="";let f=h.href,k=!1;for(let _=0;_<2;_++){let W,E=new Promise(p=>{W=p}),M=`mcp-goto-${Math.random().toString(36).slice(2)}`;U={token:m,timeoutMs:I,location:null,finish:W},await c.evaluate(({url:p,id:b})=>{let y=document.createElement("iframe");y.id=b,y.hidden=!0,y.src=p,document.body.append(y)},{url:f,id:M}).catch(()=>null),await Promise.race([E,new Promise(p=>setTimeout(p,I))]);let O=U.location;if(U=null,await c.evaluate(p=>document.getElementById(p)?.remove(),M).catch(()=>null),!O)break;let A=Ws(S.rawUrl,O);if(A){a.set(S.rawUrl,{...S,url:A,resolvedUrl:A,resolutionStatus:"resolved"});break}let P;try{P=new URL(O,f)}catch{k=!0;break}if(!bt(P.hostname)||Ia(P.href)!==m){k=!0;break}f=P.href}k&&a.get(S.rawUrl)?.resolutionStatus!=="resolved"&&a.set(S.rawUrl,{...S,resolutionStatus:"rejected"})}}finally{U=null,await c.close().catch(()=>{}),n.onNetworkAudit?.({nonGoogleRequestsObserved:r})}return a}var Ds=String.raw`^(?:an error has occurred(?:[.!…]\s*)?(?:please try again later[.!…]?)?|loading|please wait|try again|something went wrong)[.!…\s]*$`,Hd=700,Dd=1e3,Fd=1400,Fs=900,Gd=3,Gs=250,In=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=[];emptyPaaLifecycle(t){return{requestedQuestions:t,questionsFound:0,targetStatus:"not_met",discoveryStatus:"in_progress",materialStatus:"partial",completionReason:"PAA expansion is still running.",automaticRetries: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=Pn(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=[...n.flatMap(u=>u.sources??[]),...a,...i.citations.map(({text:u,href:h,...m})=>({...m,url:h})),...s.citations.map(({text:u,href:h,...m})=>({...m,url:h}))],d=new Map(o.map(u=>[u.rawUrl,u])),l=0,c=await Hs(t,o,{onNetworkAudit:u=>{l=u.nonGoogleRequestsObserved}}),U=u=>({...u,...c.get(u.rawUrl)??u});for(let u of n){u.sources=(u.sources??[]).map(U);let h=u.sources[0];u.source_title=h?.title??u.source_title,u.source_site=h?.site??u.source_site,u.source_cite=h?.url??u.source_cite}for(let u=0;u<a.length;u++){let h=U(a[u]);if(h.resolvedUrl)try{h.domain=new URL(h.resolvedUrl).hostname.replace(/^www\./,"")}catch{}a[u]=h}let r=u=>{let h=U({url:u.href,rawUrl:u.rawUrl,resolvedUrl:u.resolvedUrl,linkType:u.linkType,resolutionStatus:u.resolutionStatus}),{url:m,...I}=h;return{...I,text:u.text,href:m}};i.citations=i.citations.map(r),s.citations=s.citations.map(r);let S=[...c.values()];this.linkDiagnostics={linkCandidatesObserved:d.size,plainLinksObserved:S.filter(u=>u.linkType==="plain").length,googleUrlRedirectsObserved:S.filter(u=>u.linkType==="google_url_redirect").length,googleGotoRedirectsObserved:S.filter(u=>u.linkType==="google_goto_redirect").length,redirectsResolved:S.filter(u=>u.linkType!=="plain"&&u.resolutionStatus==="resolved").length,redirectsUnresolved:S.filter(u=>u.resolutionStatus==="unresolved").length,redirectsRejected:S.filter(u=>u.resolutionStatus==="rejected").length,resolverNonGoogleRequestsObserved:l,paaSourcesPreserved:n.reduce((u,h)=>u+(h.sources??[]).length,0),aiOverviewLabelValidated:i.detected}}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(Y.captchaMarker).count().catch(()=>0)>0)throw new et(`${n} returned a CAPTCHA \u2014 retrying with a fresh session.`)}async extractVisibleItems(t,n){let a=Y,i=await t.evaluate(({selectors:o,onlyQuestion:d,answerPlaceholderPattern:l})=>{function c(r){if(!r)return"";let S=[];for(let u of r.childNodes)if(u.nodeType===Node.TEXT_NODE){let h=u.textContent?.trim();h&&S.push(h)}else{if(u.tagName==="STYLE"||u.tagName==="SCRIPT")continue;{let h=c(u);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),u=r.classList.contains(o.expandedClass)||S?.getAttribute("aria-expanded")==="true",h=r.querySelector(o.answerContainer),I=h?.querySelector(o.aiAnswerBody)??h,f=c(I),k=new RegExp(l,"i").test(f),_=[...Array.from((I??r).querySelectorAll("a[href]")),...Array.from(r.querySelectorAll(o.aiCitation)),...Array.from(r.querySelectorAll(o.aiSourceCard))],W=!k&&(u&&f.length>=10||f.length>30&&_.some(A=>/^https?:\/\//.test(A.href))),E=W&&r.querySelector(o.sourceTitle)?.innerText?.trim()||void 0,M=W&&r.querySelector(o.sourceSite)?.innerText?.trim()||void 0,O=[];if(W){let A=new Set;for(let p of _)!/^https?:\/\//.test(p.href)||A.has(p.href)||(A.add(p.href),O.push({title:U(p.getAttribute("aria-label"))||(p.textContent||"").trim(),rawHref:p.href}));let P=O.find(p=>p.title.length>2)??O[0];P&&(E=E||P.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:W&&f||void 0,sourceTitle:E,sourceSite:M,sources:O}})},{selectors:a,onlyQuestion:n,answerPlaceholderPattern:Ds}),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 u=Pn(S.rawHref,s);if(!u||l.has(u.rawUrl))continue;l.add(u.rawUrl);let h=o.sourceSite??null;try{h||=new URL(u.resolvedUrl??u.rawUrl).hostname.replace(/^www\./,"")}catch{}d.push({...u,title:S.title||o.sourceTitle||null,site:h})}let c=d[0],U={questionId:Xt(o.question),googleLinkId:o.googleLinkId,googleEvidenceId:o.googleEvidenceId,question:o.question,answer:Es(o.answer,o.question,o.sourceTitle),sourceTitle:c?.title??o.sourceTitle,sourceSite:c?.site??o.sourceSite,sourceCite:c?.url,sources:d},r=ai.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(`${Y.item}[data-q="${n}"], ${Y.item}[data-initq="${n}"]`).first().click()}catch{}}toFlatRow(t,n,a,i){return{seed_query:i,question_id:t.questionId??Xt(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:{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=[];let d=new Set,l=new Map,c=new Set,U=new Set,r=new Map,S=2,u=3,h=0,m={sel:Y.item,dataQ:Y.itemDataQ,dataInitQ:Y.itemDataInitQ,questionEl:Y.itemQuestionEl,answerSel:Y.answerContainer,expandedClass:Y.expandedClass,clickTarget:Y.clickTarget,answerPlaceholderPattern:Ds,questionsOnly:n.questionsOnly,sourceSel:"a[href]"},I=()=>t.evaluate(({sel:M,dataQ:O,dataInitQ:A,questionEl:P,expandedClass:p,clickTarget:b,answerSel:y,answerPlaceholderPattern:L,questionsOnly:C})=>{let N=H=>{if(!H)return"";let x=[];for(let R of H.childNodes){if(R.nodeType===Node.TEXT_NODE){let K=R.textContent?.trim();K&&x.push(K);continue}if(R.nodeType!==Node.ELEMENT_NODE)continue;let J=R;if(J.tagName==="STYLE"||J.tagName==="SCRIPT"||J.getAttribute("aria-hidden")==="true")continue;let st=N(J);st&&x.push(st)}return x.join(" ").replace(/\s+/g," ").trim()};return Array.from(document.querySelectorAll(M)).map((H,x)=>{let R=N(H.querySelector(y)).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(),J=H.classList.contains(p)||H.querySelector(b)?.getAttribute("aria-expanded")==="true",st=R.length>=10&&!new RegExp(L,"i").test(R);return{q:H.getAttribute(O)||H.getAttribute(A)||H.querySelector(P)?.innerText?.trim()||"",done:J&&(C||st),expanded:J,index:x}}).filter(H=>H.q)},m),f=async(M,O,A,P,p,b)=>{let y=Date.now(),L={id:`${b}:${Xt(M)}:${p}`,questionId:Xt(M),question:M,batchNumber:b,controlAttempt:p,observedAt:new Date(y).toISOString(),beforeQuestionCount:O,expandedBefore:P},C=A>=0?t.locator(m.sel).nth(A).locator(m.clickTarget):null,N=null,H=C?await(async()=>(P&&(await C.click({timeout:Fs,force:!0}),await t.waitForTimeout(120)),await C.click({timeout:Fs,force:!0}),!0))().catch($=>(N=$ instanceof Error&&($.name==="TimeoutError"||/timeout/i.test($.message))?"click_ack_timeout":"click_failed",!1)):!1,x=Date.now()-y;if(!H&&N!=="click_ack_timeout")return{dispatched:!1,confirmed:!1,observation:{...L,dispatched:!1,outcome:"dispatch_failed",confirmationSignals:[],confirmationCheckpointMs:null,confirmedAtMs:null,dispatchDurationMs:x,totalDurationMs:x,afterQuestionCount:O,newQuestionCount:0,expandedAfter:P,answerObserved:!1,sourceCount:0,errorCode:A<0?"control_missing":N??"click_failed"}};let R={exists:!0,expanded:P,answerReady:!1,sourceCount:0,questionCount:O},J=[],st=null,K=null,q=!1;for(let $ of[Hd,Dd,Fd]){let Qt=y+$-Date.now();if(Qt>0&&await t.waitForTimeout(Qt),R=await t.evaluate(({sel:Ee,dataQ:It,dataInitQ:$e,questionEl:Zt,expandedClass:Qe,clickTarget:Pe,answerSel:Gn,answerPlaceholderPattern:Ze,sourceSel:mo,target:yo})=>{let ja=te=>{if(!te)return"";let Yn=[];for(let en of te.childNodes){if(en.nodeType===Node.TEXT_NODE){let ti=en.textContent?.trim();ti&&Yn.push(ti);continue}if(en.nodeType!==Node.ELEMENT_NODE)continue;let nn=en;if(nn.tagName==="STYLE"||nn.tagName==="SCRIPT"||nn.getAttribute("aria-hidden")==="true")continue;let Za=ja(nn);Za&&Yn.push(Za)}return Yn.join(" ").replace(/\s+/g," ").trim()},Vn=Array.from(document.querySelectorAll(Ee)),tn=Vn.find(te=>(te.getAttribute(It)||te.getAttribute($e)||te.querySelector(Zt)?.innerText?.trim())===yo);if(!tn)return{exists:!1,expanded:!1,answerReady:!1,sourceCount:0,questionCount:Vn.length};let Co=tn.classList.contains(Qe)||tn.querySelector(Pe)?.getAttribute("aria-expanded")==="true",za=tn.querySelector(Gn),$a=ja(za).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(),Qa=$a.length>=10&&!new RegExp(Ze,"i").test($a);return{exists:!0,expanded:Co,answerReady:Qa,sourceCount:Qa?za?.querySelectorAll(mo).length??0:0,questionCount:Vn.length}},{...m,target:M}).catch(()=>R),J=[...R.expanded?["expanded"]:[],...R.answerReady?["answer"]:[],...R.sourceCount>0?["source"]:[],...R.questionCount>O?["question_growth"]:[]],R.answerReady||R.sourceCount>0||R.questionCount>O){q=!0,st=$,K=Date.now()-y;break}}let ut=Date.now()-y,F={...L,dispatched:H,outcome:q?"confirmed":H?"unconfirmed":"dispatch_failed",confirmationSignals:J,confirmationCheckpointMs:st,confirmedAtMs:K,dispatchDurationMs:x,totalDurationMs:ut,afterQuestionCount:R.questionCount,newQuestionCount:Math.max(0,R.questionCount-O),expandedAfter:R.expanded,answerObserved:R.answerReady,sourceCount:R.sourceCount,errorCode:H?q?null:R.exists?"confirmation_timeout":"control_missing":N};return{dispatched:H,confirmed:q,observation:F}},k=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 Kt(this.paaLifecycle.completionReason);this.throwIfAborted(a);let M=await I(),O=!1;for(let y of M)s.has(y.q)||(s.add(y.q),o.push(y.q),o.length<=n.maxQuestions&&(O=this.upsertCollectedRow(this.toFlatRow({questionId:Xt(y.q),googleLinkId:null,googleEvidenceId:null,question:y.q,sources:[]},1,null,n.query))||O)),y.done&&c.add(y.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:c.size,failedInteractions:U.size,visibleControls:M.length},O&&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 y=!1,L=new Set(o.slice(0,n.maxQuestions));for(let C of await this.extractVisibleItems(t)){if(!L.has(C.question))continue;let N=r.get(C.question),H={...C,answer:C.answer??N?.answer,sourceTitle:C.sourceTitle??N?.sourceTitle,sourceSite:C.sourceSite??N?.sourceSite,sourceCite:C.sourceCite??N?.sourceCite,sources:this.mergePaaSources(N?.sources,C.sources)};r.set(C.question,H),y=this.upsertCollectedRow(this.toFlatRow(H,1,null,n.query))||y}if(y&&await this.emitProgress("answer_captured"),[...L].every(C=>{let N=r.get(C);return!!(N?.answer?.trim()&&N.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 A=new Set(o.slice(0,n.maxQuestions)),P=M.filter(y=>A.has(y.q)&&!y.done&&(l.get(y.q)??0)<S&&!d.has(y.q)),p=P.slice(-Gd);if(this.paaLifecycle={...this.paaLifecycle,eligibleControls:P.length},p.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 y=new Set(M.map(C=>C.q)),L=o.slice(0,n.maxQuestions).filter(C=>!c.has(C)&&(U.has(C)||!y.has(C)));if(L.length>0)throw this.paaLifecycle={...this.paaLifecycle,discoveryStatus:"interrupted",failedInteractions:new Set([...U,...L]).size,completionReason:"At least one known PAA control failed or disappeared before it could be confirmed."},await this.emitProgress("expansion_finished"),new Kt(this.paaLifecycle.completionReason);for(let C=1;C<=u;C++){this.throwIfAborted(a),await this.throwIfCaptcha(t,"Google PAA exhaustion confirmation"),await t.mouse.wheel(0,320);let N=await t.waitForFunction(({sel:x,min:R})=>document.querySelectorAll(x).length>R,{sel:Y.item,min:M.length},{timeout:1500,polling:100}).catch(()=>null);if(this.paaLifecycle={...this.paaLifecycle,noGrowthConfirmations:C},N||(await I()).some(x=>!s.has(x.q)))continue t}this.paaLifecycle={...this.paaLifecycle,discoveryStatus:"frontier_exhausted",targetStatus:"not_met",failedInteractions:0,noGrowthConfirmations:u,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 y of p)this.reporter.onDepth(++k),(l.get(y.q)??0)>0&&(this.paaLifecycle={...this.paaLifecycle,automaticRetries:this.paaLifecycle.automaticRetries+1}),l.set(y.q,(l.get(y.q)??0)+1);let b=await Promise.all(p.map(async y=>({target:y,result:await f(y.q,M.length,y.index,y.expanded,l.get(y.q)??1,h)})));for(let{target:y,result:L}of b)(L.dispatched||L.confirmed)&&this.clickedQuestions.add(y.q),this.interactionObservations.push(L.observation);if(this.interactionObservations.length>Gs&&this.interactionObservations.splice(0,this.interactionObservations.length-Gs),!n.questionsOnly){let y=new Set(p.map(L=>L.q));for(let L of await this.extractVisibleItems(t))y.has(L.question)&&(r.set(L.question,L),this.upsertCollectedRow(this.toFlatRow(L,1,null,n.query))&&await this.emitProgress("answer_captured"))}for(let{target:y,result:L}of b){let C=r.get(y.q);L.confirmed||C?.answer?.trim()?(c.add(y.q),U.delete(y.q)):(l.get(y.q)??0)>=S&&(U.add(y.q),d.add(y.q))}this.paaLifecycle={...this.paaLifecycle,processedQuestions:c.size,failedInteractions:U.size,noGrowthConfirmations:0,dispatchedInteractions:this.interactionObservations.filter(y=>y.dispatched).length,confirmedInteractions:this.interactionObservations.filter(y=>y.outcome==="confirmed").length,unconfirmedInteractions:this.interactionObservations.filter(y=>y.outcome!=="confirmed").length},await this.emitProgress("click_observed")}let _=n.questionsOnly?new Map:new Map(r);n.questionsOnly||await this.fillIncompleteItems(t,o,_,n,d);let W=[];for(let M of o){if(W.length>=n.maxQuestions)break;let O=this.normalizeQuestion(M);if(i.has(O))continue;i.add(O);let A=_.get(M);if(A){let P=this.toFlatRow(A,1,null,n.query);this.upsertCollectedRow(P),W.push(P),this.reporter.onQuestion({questionId:A.questionId,googleLinkId:A.googleLinkId,googleEvidenceId:A.googleEvidenceId,question:A.question,answer:A.answer??null,sourceTitle:A.sourceTitle??null,sourceSite:A.sourceSite??null,sourceCite:A.sourceCite??null,sources:A.sources,depth:1,parentQuestion:null,children:[]})}else{let P=this.toFlatRow({questionId:Xt(M),googleLinkId:null,googleEvidenceId:null,question:M,answer:void 0,sourceTitle:void 0,sourceSite:void 0,sourceCite:void 0,sources:[]},1,null,n.query);this.upsertCollectedRow(P),W.push(P)}}this.completeness.paaWithoutAnswer=n.questionsOnly?0:W.filter(M=>!M.answer).length,this.completeness.paaWithoutSource=n.questionsOnly?0:W.filter(M=>(M.sources??[]).length===0).length,this.completeness.paaRequested=n.maxQuestions,this.completeness.paaReturned=W.length,this.completeness.paaUnique=i.size;let E=!n.questionsOnly&&(this.completeness.paaWithoutAnswer>0||this.completeness.paaWithoutSource>0);return this.paaLifecycle={...this.paaLifecycle,questionsFound:W.length,targetStatus:W.length>=n.maxQuestions?"met":"not_met",materialStatus:E?"partial":"complete",processedQuestions:c.size,failedInteractions:U.size},await this.emitProgress("fill_incomplete"),await this.emitProgress("expansion_finished"),W}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)))),c=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 u=a.get(U);u&&this.upsertCollectedRow(this.toFlatRow(u,1,null,i.query))&&await this.emitProgress("fill_incomplete")}this.completeness.paaAnswersRecovered=[...c].filter(U=>a.get(U)?.answer).length}}async reExpandCollapsedItems(t){let n=`${Y.item}:not(.${Y.expandedClass}) ${Y.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=Ki;return t.evaluate(a=>{let i=[],s=Array.from(document.querySelectorAll(a.container));for(let o of s){let c=(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 u=r.textContent?.trim()??"",h=u.indexOf("YouTube");if(h===-1)continue;let m=u.slice(0,h).trim(),f=u.slice(h+7).replace(/^[·\s·]+/,"").match(/^([^·\n]+)/),k=f?f[1].trim():"";m&&i.push({type:c,title:m,channel:k,platform:"YouTube",duration:"",url:S})}}return i},n)}async extractForums(t){let n=Ji;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 c=l.href;if(!c)continue;let U=l.querySelector(a.title),r=l.querySelector(a.source),S=U?.textContent?.trim()??"",u=r?.textContent?.trim()??"";S&&i.push({title:S,source:u,url:c})}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:yn.item,platforms:[...yn.platforms]};return(await t.evaluate(s=>{let o=new Set,d=[],l=Array.from(document.querySelectorAll(s.item)),c=["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||!c.some(h=>S.includes(h)))continue;let u=r.textContent?.trim()??"";U.has(S)||U.set(S,[]),U.get(S).push(u)}for(let[r,S]of U.entries()){if(o.has(r))continue;o.add(r);let u=S.find(k=>/^\d+:\d+$/.test(k))??"",h=S.find(k=>!/^\d+:\d+$/.test(k)&&k.length>5)??"";if(!h)continue;let m=h,I="",f="";for(let k of s.platforms){let _=-1,W=0;for(;;){let P=h.indexOf(k,W);if(P===-1)break;_=P,W=P+1}if(_===-1)continue;let E=h.slice(_+k.length);if(!(/^[\s·]/.test(E)||E.trim()===""))continue;m=h.slice(0,_).trim(),I=k;let O=E.replace(/^[\s·]+/,""),A=O.indexOf("\xB7");f=(A===-1?O:O.slice(0,A)).trim();break}m&&d.push({title:m,channel:f,platform:I,duration:u,url:r})}return d},a)).map(s=>({type:"short_video",...s}))}async extractWhatPeopleSaying(t){let n=qi;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(),c=s.querySelector(a.titleDiv)?.textContent?.trim(),U=l??c??"",r=s.querySelector(a.source)?.textContent?.trim()??"",u=s.querySelector(a.platformBadge)?.textContent?.trim()??"",h=s.querySelector(a.ytChannel)?.textContent?.trim()??"",m=s.querySelector(a.ytDate)?.textContent?.trim()??"",I=s.querySelector(a.authorNote)?.textContent?.trim()??null,f=s.querySelector(a.popularCommentLabel),k=null;if(f){let N=f.nextSibling;for(;N;){let H=N.textContent?.trim();if(H){k=H;break}N=N.nextSibling}}let _=Array.from(s.querySelectorAll("span")),W=_.find(N=>/^\d+:\d+$/.test(N.textContent?.trim()??""))?.textContent?.trim()??null,M=_.map(N=>N.textContent?.trim()??"").filter(N=>/\d/.test(N)&&(N.includes("comment")||N.includes("reaction")||N.includes("view")||N.includes("like")||N.includes("share")))[0]??"",O=_.map(N=>N.textContent?.trim()??"").filter(N=>/\d+ (day|week|month|year|hour)s? ago|Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec/.test(N)),A=m||(O[0]??""),P=u||(h?"YouTube":""),p=h||r,b="unknown",y=P.toLowerCase(),L=p.toLowerCase(),C=r.toLowerCase();return y.includes("reddit")||L.startsWith("r/")?b="reddit":y.includes("facebook")||C.includes("facebook")?b="facebook":y.includes("instagram")||C.includes("instagram")?b="instagram":y.includes("tiktok")||C.includes("tiktok")?b="tiktok":y.includes("youtube")||h?b="youtube":b="news",{type:b,title:U,url:d,source:p,platform:P,popularComment:k,engagement:M,date:A,duration:W,authorNote:I}}):[]},n)}async extractOrganicResults(t){let n=zi,a=await t.evaluate(s=>{let o=[],d=0;return document.querySelectorAll(s.result).forEach(l=>{let c=l.querySelector(s.title);if(!c)return;let U=c.textContent?.trim()??"",S=c.closest("a")?.href??"";if(!U||!S)return;d++;let u=l.querySelector(s.cite)?.textContent?.trim()??null,h=l.querySelector(s.snippet)?.textContent?.trim()??null,m=!!l.querySelector(s.redditCite),I=l.querySelector(s.ratingWrap),f=I?{value:I.querySelector(s.ratingValue)?.textContent?.trim()??"",count:I.querySelector(s.reviewCount)?.textContent?.trim()??""}:null,k="";try{k=new URL(S).hostname.replace(/^www\./,"")}catch{k=l.querySelector(s.siteName)?.textContent?.trim()??""}o.push({position:d,title:U,url:S,domain:k,cite:u,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=Pn(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=$i;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 c=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 k=f.href.match(/[?&]cid=(\d+)/);if(k){S=k[1];break}let _=f.href.match(/!1s0x[0-9a-f]+:0x([0-9a-f]+)/i);if(_){try{S=BigInt("0x"+_[1]).toString()}catch{}if(S)break}}let u=[];o.querySelectorAll("div, span").forEach(f=>{let k=Array.from(f.childNodes).filter(_=>_.nodeType===3).map(_=>_.textContent?.trim()??"").filter(_=>_.length>1&&_.length<120).join(" ");k&&!u.includes(k)&&u.push(k)});let h=Array.from(o.querySelectorAll("a[href]")),m=h.find(f=>f.href.includes("google.com/maps"))?.href??null,I=h.find(f=>!f.href.includes("google.com")&&f.href.startsWith("http"))?.href??null;i.push({position:d+1,name:l,cid:S,rating:c,reviewCount:r,metadata:u,websiteUrl:I,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 c=[".OSrXXb",".dbg0pd",".tzt0oe",'[role="heading"]',"h3"];for(let U of c){let r=l.querySelector(U);if(r?.textContent?.trim())return r.textContent.trim()}return""}document.querySelectorAll('[id^="pv-/g/"]').forEach(l=>{let c=l.getAttribute("id");if(!c)return;let U=c.replace("pv-","");n.add(U);let r=o(l),u=l.querySelector("a[data-cid]")?.getAttribute("data-cid")??null;u&&a.add(u),r&&s.set(U,{name:r,kgId:U,cid:u,gcid:null})}),document.querySelectorAll("[data-mid]").forEach(l=>{let c=l.getAttribute("data-mid");if(c?.startsWith("/g/")&&(n.add(c),!s.has(c))){let U=o(l);U&&s.set(c,{name:U,kgId:c,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/"]'),u=S?S.getAttribute("id").replace("pv-",""):null,h=u??`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:u,cid:U,gcid:null})}),document.querySelectorAll("a[data-cid]").forEach(l=>{let c=l.getAttribute("data-cid");if(!c)return;if(a.add(c),![...s.values()].some(r=>r.cid===c)){let r=l.parentElement,S="";for(let u=0;u<8&&r;u++){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:${c}`,{name:S,kgId:null,cid:c,gcid:null})}});let d=Array.from(document.querySelectorAll("script:not([src])")).map(l=>l.textContent??"").filter(l=>l.length>1e4).join(`
|
|
21884
|
+
`);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(Ps,{aio:Xi,aim:ji,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 c=d.querySelector('[aria-label="Share"]');if(c)return c.scrollIntoView({block:"center"}),c.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=c=>{try{let U=new URL(c,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 c=>{if(await c.route("**/*",async U=>{let r=U.request();r.isNavigationRequest()&&r.frame()===c.mainFrame()&&!o(r.url())?(i=/^https:\/\/www\.google\.com\/sorry(?:\/|\?)/.test(r.url())?"captcha":"invalid_next",await U.abort("blockedbyclient")):await U.fallback()}),await c.goto(new URL(d,s).href,{waitUntil:"domcontentloaded",timeout:2e4}),this.throwIfAborted(a),await this.throwIfCaptcha(c,"Google SERP page 2"),!o(c.url()))throw i="invalid_next",new Error("Invalid pagination landing");return this.extractOrganicResults(c)},a);return this.throwIfAborted(a),l.length>0?{organic:l.map(c=>({...c,position:c.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 et?"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,kernelApiKey:t.kernelApiKey,kernelProxyId:t.kernelProxyId,kernelProfileName:t.kernelProfileName,kernelProfileSaveChanges:t.kernelProfileSaveChanges,kernelStealth:t.kernelStealth,kernelProxyResolution:t.kernelProxyResolution,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),await this.driver.launch(s);let l=this.driver.getProviderSessionMetadata?.();if(l?.providerSessionId&&this.onProviderSession)try{await this.onProviderSession(l)}catch(F){console.warn(JSON.stringify({event:"paa_provider_session_sink_failed",provider:l.provider,message:F instanceof Error?F.message:String(F)}))}this.throwIfAborted(n);let c=t.location?_a(t.location):null,U=t.location?c?.canonical??t.location:null,r=U?fs(U):null,S=t,u=t.includeAllSerpFeatures,h=u||t.includeLocalPack,m=u||t.includeForums,I=u||t.includeVideos,f=u||t.includeAiOverview,k=u||t.includeWhatPeopleSaying,_={surface:"web",aiOverview:{detected:!1,text:null,citations:[]},aiMode:{detected:!1,text:null,citations:[]}},{hasPaa:W}=await this.driver.navigateToSERP(S.query,r,t.gl,t.hl,{...Ie(t.recency)?{tbs:Ie(t.recency)}:{},debug:t.debug});this.throwIfAborted(n);let E=this.driver.getPage();if(await this.throwIfCaptcha(E,"Google SERP"),W||(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 F=W?await this.runBFS(E,S,n):[];await this.resolveMaterialLinks(E,F,[],_.aiOverview,_.aiMode);let $={seed:S.query,totalQuestions:F.length,maxDepthReached:F.reduce((Qt,Ee)=>Math.max(Qt,Ee.depth),0),durationMs:Date.now()-a,errorCount:o};return this.reporter.onComplete($),{seed:S.query,location:t.location??null,extractedAt:new Date().toISOString(),diagnostics:{completionStatus:W?"paa_found":"no_paa",...W?{}:{noPaaObserved:!0},problem:null,paaLifecycle:{...this.paaLifecycle},completeness:{...this.completeness},links:{...this.linkDiagnostics},...t.debug?{debug:this.buildHarvestDebugSnapshot(S,U,r,void 0,c)}:{}},totalQuestions:F.length,surface:"web",aiOverview:{detected:!1,text:null,citations:[]},aiMode:{detected:!1,text:null,citations:[]},whatPeopleSaying:[],tree:this.buildTree(F,S.query),flat:F,videos:[],forums:[],organicResults:[],localPack:[],entityIds:{entities:[],kgIds:[],cids:[],gcids:[]},stats:$}}if(f&&(this.aioShareUrlEarly=await this.captureAIOverviewShareUrl(E,t).catch(()=>null)),t.serpOnly){let[F,$,Qt]=await Promise.all([this.extractOrganicResults(E),h?this.extractLocalPack(E):Promise.resolve([]),this.extractEntityIds(E)]),Ee=this.mergeLocalPackIntoEntities(Qt,$),It=f?await this.extractAISurfaces(E,t):_,$e=t.debug?qe(U,F,$):void 0,Zt=F;if((t.pages??1)>=2){let Pe=new URLSearchParams({q:S.query,gl:t.gl,hl:t.hl,pws:"0",start:"10"});Ie(t.recency)&&Pe.set("tbs",Ie(t.recency)),r&&Pe.set("uule",r),await this.driver.navigateTo("https://www.google.com/search?"+Pe.toString()),await this.throwIfCaptcha(E,"Google SERP page 2");let Gn=await this.extractOrganicResults(E);Zt=[...F,...Gn.map(Ze=>({...Ze,position:Ze.position+10}))],t.debug&&($e=qe(U,Zt,$))}await this.resolveMaterialLinks(E,[],Zt,It.aiOverview,It.aiMode);let Qe={seed:S.query,totalQuestions:0,maxDepthReached:0,durationMs:Date.now()-a,errorCount:o};return this.reporter.onComplete(Qe),{seed:S.query,location:t.location??null,extractedAt:new Date().toISOString(),diagnostics:{completionStatus:"serp_only",problem:null,completeness:{...this.completeness},links:{...this.linkDiagnostics},...t.debug?{debug:this.buildHarvestDebugSnapshot(S,U,r,$e,c)}:{}},totalQuestions:0,surface:It.surface,aiOverview:It.aiOverview,aiMode:It.aiMode,whatPeopleSaying:[],tree:[],flat:[],videos:[],forums:[],organicResults:Zt,localPack:$,entityIds:Ee,stats:Qe}}let[M,O,A,P,p,b]=await Promise.all([I?this.extractVideos(E):Promise.resolve([]),m?this.extractForums(E):Promise.resolve([]),k?this.extractWhatPeopleSaying(E):Promise.resolve([]),this.extractEntityIds(E),this.extractOrganicResults(E),h?this.extractLocalPack(E):Promise.resolve([])]),y=this.mergeLocalPackIntoEntities(P,b),L=t.debug?qe(U,p,b):void 0;this.reporter.onVideos(M),this.reporter.onForums(O);let C=f?await this.extractAISurfaces(E,t):_,N={requestedPages:(t.pages??1)>=2?2:1,capturedPages:1,page2Status:(t.pages??1)>=2?"not_attempted":"not_requested",page1OrganicCount:p.length,page2OrganicCount:0},H=p;if(this.checkpointMaterial={surface:C.surface,aiOverview:C.aiOverview,aiMode:C.aiMode,whatPeopleSaying:A,videos:M,forums:O,organicResults:H,localPack:b,entityIds:y,pagination:N},await this.emitProgress("serp_captured"),N.requestedPages===2){let F=await this.captureSecondOrganicPage(E,S,n);H=[...p,...F.organic],N={...N,capturedPages:F.status==="captured"?2:1,page2Status:F.status,page2OrganicCount:F.organic.length,...F.failureCode?{failureCode:F.failureCode}:{}},this.checkpointMaterial={...this.checkpointMaterial,organicResults:H,pagination:N},await this.emitProgress("serp_captured"),await this.resolveMaterialLinks(E,[],H,C.aiOverview,C.aiMode),this.checkpointMaterial={...this.checkpointMaterial,organicResults:H},await this.emitProgress("serp_captured")}let x=t.debug?qe(U,H,b):L;if(!W){await this.resolveMaterialLinks(E,[],H,C.aiOverview,C.aiMode),this.checkpointMaterial={...this.checkpointMaterial,organicResults:H},await this.emitProgress("serp_captured");let F={seed:S.query,totalQuestions:0,maxDepthReached:0,durationMs:Date.now()-a,errorCount:o};return this.reporter.onComplete(F),{seed:S.query,location:t.location??null,extractedAt:new Date().toISOString(),diagnostics:{completionStatus:"no_paa",noPaaObserved:!0,problem:null,pagination:N,paaLifecycle:{...this.paaLifecycle},completeness:{...this.completeness},links:{...this.linkDiagnostics},...t.debug?{debug:this.buildHarvestDebugSnapshot(S,U,r,x,c)}:{}},totalQuestions:0,surface:C.surface,aiOverview:C.aiOverview,aiMode:C.aiMode,whatPeopleSaying:A,tree:[],flat:[],videos:M,forums:O,organicResults:H,localPack:b,entityIds:y,stats:F}}let R=await this.runBFS(E,S,n);this.throwIfAborted(n);let J=new URLSearchParams({q:S.query,gl:t.gl,hl:t.hl,pws:"0",udm:yn.udm});r&&J.set("uule",r);let st=[];if(t.includeShortVideos!==!1)try{st=await this.extractShortVideos(E,"https://www.google.com/search?"+J.toString())}catch(F){if(!(F instanceof et))throw F;o++,d.push({code:"short_videos_captcha_skipped",surface:"short_videos",message:F.message,retryable:!0})}this.reporter.onVideos(st),await this.resolveMaterialLinks(E,R,H,C.aiOverview,C.aiMode),this.checkpointMaterial={...this.checkpointMaterial,surface:C.surface,aiOverview:C.aiOverview,aiMode:C.aiMode,videos:[...M,...st],organicResults:H},await this.emitProgress("expansion_finished");let K=[...M,...st],q=this.buildTree(R,S.query),ut={seed:S.query,totalQuestions:R.length,maxDepthReached:R.reduce((F,$)=>Math.max(F,$.depth),0),durationMs:Date.now()-a,errorCount:o};return this.reporter.onComplete(ut),{seed:S.query,location:t.location??null,extractedAt:new Date().toISOString(),diagnostics:{completionStatus:"paa_found",problem:null,pagination:N,paaLifecycle:{...this.paaLifecycle},completeness:{...this.completeness},links:{...this.linkDiagnostics},...d.length>0?{warnings:d}:{},...t.debug?{debug:this.buildHarvestDebugSnapshot(S,U,r,x,c)}:{}},totalQuestions:R.length,surface:C.surface,aiOverview:C.aiOverview,aiMode:C.aiMode,whatPeopleSaying:A,tree:q,flat:R,videos:K,forums:O,organicResults:H,localPack:b,entityIds:y,stats:ut}}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 at=require("fs"),_t=sn(require("path"),1),jt=sn(require("papaparse"),1);async function Vs(e){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 An=class{async writeJSON(t,n){await at.promises.mkdir(n,{recursive:!0});let i=`${t.seed.toLowerCase().replace(/\W+/g,"-").slice(0,40)}-${Date.now()}.json`,s=_t.default.join(n,i);return await at.promises.writeFile(s,JSON.stringify(t,null,2),"utf8"),await Vs({title:`${t.seed} scrape`,content:JSON.stringify(t),source:`mcp-scraper:${s}`}),s}async writeCSV(t,n){await at.promises.mkdir(n,{recursive:!0});let i=(t[0]?.seed_query??"paa").toLowerCase().replace(/\W+/g,"-").slice(0,40),s=jt.default.unparse(t.map(({sources:l,...c})=>({...c,sources_json:JSON.stringify(l)})),{header:!0}),o=`${i}-${Date.now()}.csv`,d=_t.default.join(n,o);return await at.promises.writeFile(d,s,"utf8"),d}async writeVideoCSV(t,n,a){await at.promises.mkdir(a,{recursive:!0});let i=n.toLowerCase().replace(/\W+/g,"-").slice(0,40),s=jt.default.unparse(t,{header:!0}),o=`${i}-videos-${Date.now()}.csv`,d=_t.default.join(a,o);return await at.promises.writeFile(d,s,"utf8"),d}async writeForumCSV(t,n,a){await at.promises.mkdir(a,{recursive:!0});let i=n.toLowerCase().replace(/\W+/g,"-").slice(0,40),s=jt.default.unparse(t,{header:!0}),o=`${i}-forums-${Date.now()}.csv`,d=_t.default.join(a,o);return await at.promises.writeFile(d,s,"utf8"),d}async writeAIOverviewCSV(t,n,a,i){await at.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=jt.default.unparse(o,{header:!0}),l=`${s}-ai-overview-${Date.now()}.csv`,c=_t.default.join(i,l);return await at.promises.writeFile(c,d,"utf8"),c}async writeAIModeCSV(t,n,a,i){await at.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=jt.default.unparse(o,{header:!0}),l=`${s}-ai-mode-${Date.now()}.csv`,c=_t.default.join(i,l);return await at.promises.writeFile(c,d,"utf8"),c}async writeWhatPeopleSayingCSV(t,n,a){await at.promises.mkdir(a,{recursive:!0});let i=n.toLowerCase().replace(/\W+/g,"-").slice(0,40),s=t.map(c=>({seed_query:n,...c})),o=jt.default.unparse(s,{header:!0}),d=`${i}-what-people-saying-${Date.now()}.csv`,l=_t.default.join(a,d);return await at.promises.writeFile(l,o,"utf8"),l}};var Ln=class{onQuestion(t){process.stdout.write(JSON.stringify({event:"question",depth:t.depth,question:t.question})+`
|
|
21786
21885
|
`)}onDepth(t){process.stdout.write(JSON.stringify({event:"depth",depth:t})+`
|
|
21787
21886
|
`)}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})+`
|
|
21788
21887
|
`)}onForums(t){for(let n of t)process.stdout.write(JSON.stringify({event:"forum",title:n.title,source:n.source,url:n.url})+`
|
|
21789
21888
|
`)}onComplete(t){process.stdout.write(JSON.stringify({event:"complete",...t})+`
|
|
21790
21889
|
`)}onError(t){process.stderr.write(JSON.stringify({event:"error",type:t.constructor.name,message:t.message})+`
|
|
21791
|
-
`)}};var Vs=require("crypto");var Gd="pr.oxylabs.io",Vd=7777,Yd=10;function Ys(){return process.env.OXYLABS_USERNAME?.trim()||void 0}function Ks(){return process.env.OXYLABS_PASSWORD?.trim()||void 0}function Kd(){return(process.env.OXYLABS_COUNTRY?.trim()||"US").toUpperCase()}function Js(){return!!(Ys()&&Ks())}function Jd(){let e="";for(let t=0;t<10;t++)e+=String((0,Vs.randomInt)(0,10));return e}function qd(e={}){let t=Ys();if(!t)return;let n=t.startsWith("customer-")?t:`customer-${t}`,a=(e.country?.trim()||Kd()).toUpperCase(),i=e.sticky?`-sessid-${Jd()}-sesstime-${Yd}`:"";return`${n}-cc-${a}${i}`}function Ia(e){return e.replace(/pr\.oxylabs\.io(:\d+)?/gi,"backup proxy").replace(/oxylabs/gi,"backup proxy")}function Xd(e){return`mcp-backup-custom-${`${Date.now()}-${e??0}-${Math.random().toString(36).slice(2,8)}`}`}async function qs(e,t={}){let n=qd({sticky:t.sticky}),a=Ks();if(!n||!a)throw new Error("backup proxy pool is not configured");let i=new B({apiKey:e});try{let s=await i.proxies.create({type:"custom",name:Xd(t.attemptIndex),protocol:"http",config:{host:Gd,port:Vd,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(Ia(o))}}var jd={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"},zd={"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 $d(e){return e?e.slice(-6):null}function it(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:$d(n),target:a,error:i,disposable:s}}}function Qd(e){return e.trim().toLowerCase().replace(/\s+/g," ")}function Zd(e){return e.trim().toLowerCase().replace(/\./g,"").replace(/\s+/g," ")}function tl(e){if(!e)return!0;let t=Zd(e);return t==="united states"||t==="united states of america"||t==="usa"||t==="us"}function Xs(e){let t=e.trim();return/^[A-Za-z]{2}$/.test(t)?t.toUpperCase():jd[Qd(t)]??null}function el(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 Et(e,t,n){return n?`mcp-serp-residential-${e.toLowerCase()}-${t.toLowerCase()}-${n}`:`mcp-serp-residential-${e.toLowerCase()}-${t.toLowerCase()}`}function Ln(e,t){let n=`${Date.now()}-${t??0}-${Math.random().toString(36).slice(2,8)}`;return`${e}-fresh-${n}`}function nl(e){return`mcp-serp-residential-us-zip-${e}`}function al(e,t){if(!e||t.toLowerCase()!=="us")return null;let n=Ts(e),a=n.split(",").map(c=>c.trim()).filter(Boolean);if(a.length>1&&tl(a[a.length-1])&&(a=a.slice(0,-1)),a.length===1){let c=Xs(a[0]);return c?{canonicalLocation:n,level:"state",country:"US",state:c,city:"",cityCandidates:[],proxyName:Et("US",c),config:{country:"US",state:c}}:null}let[i="",s=""]=a;if(!i||!s)return null;let o=Xs(s);if(!o)return null;let d=el(i),l=d[0];return l?{canonicalLocation:n,level:"city",country:"US",state:o,city:l,cityCandidates:d,proxyName:Et("US",o,l),config:{country:"US",state:o,city:l}}:null}function il(e){return`${e.city}|${e.state}`}function js(e,t){return t&&/^\d{5}$/.test(t)?t:zd[il(e)]??null}function zs(e,t){return{...e,level:"zip",zip:t,proxyName:nl(t),config:{country:e.country,state:e.state,zip:t}}}function Aa(e,t){return{...e,proxyName:t}}function xa(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 sl(e,t){return e.find(n=>n.type==="residential"&&n.status!=="unavailable"&&!!n.id&&(n.name===t.proxyName||xa(n.config,t,t.level==="city"?t.city:void 0)))??null}function ol(e,t){for(let n of t.cityCandidates){let a=Et(t.country,t.state,n),i=e.find(s=>s.type==="residential"&&s.status!=="unavailable"&&!!s.id&&(s.name===a||xa(s.config,t,n)));if(i)return i}return null}function xn(e){return{...e,level:"state",proxyName:Et(e.country,e.state),config:{country:e.country,state:e.state}}}function rl(e,t){let n=Et(t.country,t.state);return e.find(a=>a.type==="residential"&&a.status!=="unavailable"&&!!a.id&&(a.name===n||xa(a.config,t)))??null}function dl(e,t){return xn(e)}function Pt(e){return e instanceof Error?e.message:String(e)}function ll(e,t,n){let a=[],i=js(e,t);if(i){let o=zs(e,i);a.push(Aa(o,Ln(o.proxyName,n)))}for(let o of e.cityCandidates){let d={...e,level:"city",city:o,proxyName:Et(e.country,e.state,o),config:{country:e.country,state:e.state,city:o}};a.push(Aa(d,Ln(d.proxyName,n)))}let s=xn(e);return a.push(Aa(s,Ln(s.proxyName,n))),a}async function La(e,t,n){if(e.backupOnExhaustion&&n.length>0&&e.\u006b\u0065\u0072\u006e\u0065\u006cApiKey&&Js())try{let a=await qs(e.\u006b\u0065\u0072\u006e\u0065\u006cApiKey,{attemptIndex:e.attemptIndex});return it("backup_custom",e.proxyMode,a,t,null,!0)}catch(a){n.push(`backup: ${Ia(Pt(a))}`)}return it("configured_fallback",e.proxyMode,e.configured\u004b\u0065\u0072\u006e\u0065\u006cProxyId,t,n.join(" | "))}async function Sl(e,t,n){let a=[];for(let i of ll(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 it("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}: ${Pt(s)}`)}if(process.env.\u004b\u0045\u0052\u004e\u0045\u004c_PROXY_TYPE!=="residential"){let i=xn(n);try{let s=await e.proxies.create({type:"isp",name:Ln(`mcp-serp-isp-${i.country.toLowerCase()}-${(i.state??"").toLowerCase()}`,t.attemptIndex),config:{country:i.country,...i.state?{state:i.state}:{}}});if(s.id)return it("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: ${Pt(s)}`)}}return La(t,n,a)}async function $s(e,t){if(!e||!t)return;await new B({apiKey:e}).proxies.delete(t)}async function Qs(e){if(e.proxyMode==="none")return it("disabled",e.proxyMode,void 0,null,null);if(e.proxyMode==="configured")return it("configured_fallback",e.proxyMode,e.configured\u004b\u0065\u0072\u006e\u0065\u006cProxyId,null,null);let t=al(e.location,e.gl);if(!t||!e.\u006b\u0065\u0072\u006e\u0065\u006cApiKey)return it("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 B({apiKey:e.\u006b\u0065\u0072\u006e\u0065\u006cApiKey});try{let a=e.attemptIndex??0;if(e.fresh)return await Sl(n,e,t);if(a>=1){let U=dl(t,a),r=[];try{let S=await n.proxies.create({type:"residential",name:U.proxyName,config:U.config});if(S.id)return it("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}: ${Pt(S)}`)}return La(e,U,r)}let i=[];for await(let U of await n.proxies.list())i.push(U);let s=js(t,e.proxyZip),o=[];if(s){let U=zs(t,s),r=sl(i,U);if(r?.id)return it("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 it("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}: ${Pt(S)}`)}}let d=ol(i,t);if(d?.id)return it("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:Et(t.country,t.state,U),config:{country:t.country,state:t.state,city:U}});if(r.id)return it("location_created",e.proxyMode,r.id,{...t,level:"city",city:U,proxyName:Et(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}: ${Pt(r)}`)}let l=xn(t),c=rl(i,l);if(c?.id)return it("location_reused",e.proxyMode,c.id,l,o.join(" | "));try{let U=await n.proxies.create({type:"residential",name:l.proxyName,config:l.config});if(U.id)return it("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}: ${Pt(U)}`)}return La(e,t,o)}catch(a){return it("configured_fallback",e.proxyMode,e.configured\u004b\u0065\u0072\u006e\u0065\u006cProxyId,t,Pt(a))}}var Ul=3,ul=5;function Wa(e){return e.reason instanceof DOMException&&e.reason.name==="TimeoutError"?e.reason:new Ut}function cl(e){if(!e||typeof e!="object")return;let t=e.signal;if(t instanceof AbortSignal)return t}function hl(e){if(!e||typeof e!="object")return;let t=e.onAttemptEvent;return typeof t=="function"?t:void 0}function gl(e){if(!e||typeof e!="object")return;let t=e.onPaaProgress;return typeof t=="function"?t:void 0}async function ve(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 Ra(e){if(e instanceof et)return"captcha";if(e instanceof Fe)return"location_mismatch";if(e instanceof Ut)return"request_aborted";if(e instanceof Yt)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":Cl(t)?"proxy_tunnel_failed":kl(t)?"proxy_unavailable":/timeout|timed out|Timeout \d+ms exceeded|deadline/i.test(t)?"timeout":"error"}function wl(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 Zs(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),c=e.diagnostics.completionStatus==="paa_found",U=e.diagnostics.paaLifecycle?.discoveryStatus,r=U==="interrupted"||U==="recovery_exhausted",S=c&&(s<t&&U!=="frontier_exhausted"||o>0||d>0||!!e.diagnostics.interruption||r),u=l?"complete":c&&s>0?S||i?"partial":"complete":a?i?"partial":"complete":"degraded",h=u==="degraded";return e.diagnostics={...e.diagnostics,resultQuality:u,degradedResult:h,degradationReasons:h?["empty_primary_serp"]:[],retryRecommended:h||u==="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 ml(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 yt(e){return e instanceof Error?e.message:String(e)}function yl(e){return e==="location"?ul:Ul}function Cl(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 kl(e){return/proxy unavailable|proxy_unavailable|connection_test_failed|did not return a proxy id|configured fallback/i.test(e)}function to(e){return e==="captcha"||e==="proxy_tunnel_failed"||e==="proxy_unavailable"}function eo(e){return e==="browser_session_interrupted"}function Ml(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 fl(e,t){return t==="location"&&e.diagnostics.debug?.locationEvidence?.status==="mismatch"}function no(e,t){if(t||!e.diagnostics.debug)return e;let n={...e.diagnostics};return delete n.debug,{...e,diagnostics:n}}async function ao(e){try{return await Rs(e,{totalBudgetMs:1e4})}catch(t){return console.warn(JSON.stringify({event:"harvest_goto_resolution_failed",message:yt(t)})),e}}async function Oa(e,t){if(!(!e||!t))try{await $s(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:yt(n)}))}}async function io(e,t,n=!1,a,i){let s=ms(e,{forceManaged:n}),o=new An,d=new _n(s,o,a,i);if(t?.aborted)return{result:null,error:Wa(t),cleanup:await s.close(),debug:null};let l,c=t?new Promise((h,m)=>{l=()=>m(Wa(t)),t.addEventListener("abort",l,{once:!0})}):null,U=null,r=null,S,u=null;try{let h=d.extract(e,t);c&&h.catch(()=>{}),U=await(c?Promise.race([h,c]):h)}catch(h){r=h}finally{t&&l&&t.removeEventListener("abort",l),u=U?.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:s.getDebugSnapshot()}:null),S=await s.close()}if(r){let h=Ra(r),m=h==="timeout"||h==="request_aborted"||h==="browser_session_interrupted"?h:null,I=null;if(m)try{I=typeof d.getPartialResult=="function"?d.getPartialResult():null}catch{I=null}return I&&m?(I.diagnostics={...I.diagnostics,interruption:{code:m,reasonSource:"client_runtime",message:yt(r)}},{result:I,error:r,cleanup:S,debug:u,salvagedFrom:r}):{result:null,error:r,cleanup:S,debug:u}}return{result:U,error:null,cleanup:S,debug:u}}async function so(e){let t=typeof e=="object"&&e!==null?e:{},n=cl(e),a=hl(e),i=gl(e),s=t.forceManagedSerp===!0,o=t.proxyMode,d=o==="location"||o==="none"||o==="configured"?o:Yn,l=typeof t.\u006b\u0065\u0072\u006e\u0065\u006cApiKey=="string"?t.\u006b\u0065\u0072\u006e\u0065\u006cApiKey.trim():ai(),c=typeof t.\u006b\u0065\u0072\u006e\u0065\u006cProxyId=="string"?t.\u006b\u0065\u0072\u006e\u0065\u006cProxyId.trim():void 0,U={\u006b\u0065\u0072\u006e\u0065\u006cApiKey:l,proxyMode:d,configured\u004b\u0065\u0072\u006e\u0065\u006cProxyId:c,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"},r=typeof t.debug=="boolean"?t.debug:!1,S=d==="location"&&!!U.location,u=t.maxAttempts,m=typeof u=="number"&&Number.isInteger(u)&&u>=1?u:yl(d)+1,I=typeof t.softDeadlineMs=="number"?t.softDeadlineMs:null,T=new In,k=null,P=d,W=0;for(let E=0;E<m;E++){let f=E+1,O=Date.now();try{if(n?.aborted)throw Wa(n);let A=await Qs({...U,proxyMode:P,attemptIndex:E,fresh:P==="location"});if(!A)break;let _={...t,\u006b\u0065\u0072\u006e\u0065\u006cApiKey:l,\u006b\u0065\u0072\u006e\u0065\u006cProxyId:A.\u006b\u0065\u0072\u006e\u0065\u006cProxyId,\u006b\u0065\u0072\u006e\u0065\u006cProxyResolution:A.resolution,proxyMode:P,keepDefaultProxy:E>0,debug:r||S};P==="none"&&(_.\u006b\u0065\u0072\u006e\u0065\u006cProxyId=void 0);let p=ei.parse(_);await ve(a,{type:"started",attemptNumber:f,maxAttempts:m,query:p.query,location:p.location??null,maxQuestions:p.maxQuestions,startedAt:new Date(O).toISOString()}),console.info(JSON.stringify({event:"harvest_attempt_started",attempt_number:f,max_attempts:m,query:p.query,location:p.location??null,max_questions:p.maxQuestions}));let b=Kt(),y=K=>ve(a,{type:"provider_session_started",attemptNumber:f,maxAttempts:m,provider:K.provider,providerSessionId:K.providerSessionId,observedAt:new Date().toISOString()}),L=b?.harvestBrowserMode==="headless-test"&&process.env[es]==="true",C=b?await as({...b,forceHeadless:L,forceHeadful:!L},()=>io(p,n,s,i,y)):await io(p,n,s,i,y);if(C.error){let K=C.error,q=Ra(K);(q==="captcha"||q==="proxy_tunnel_failed")&&P==="location"&&(W++,W>=2&&(P="none"));let ut=E<m-1&&(q==="captcha"||eo(q)||d==="location"&&to(q));if(q==="captcha"?console.warn(JSON.stringify({event:"harvest_attempt_captcha",attempt_number:f,max_attempts:m,message:yt(K),will_retry:ut})):ut&&console.warn(JSON.stringify({event:"harvest_attempt_proxy_retry",attempt_number:f,max_attempts:m,outcome:q,message:yt(K),will_retry:ut})),await ve(a,{type:"finished",attemptNumber:f,maxAttempts:m,outcome:q,\u006b\u0065\u0072\u006e\u0065\u006cSessionId:C.cleanup.\u006b\u0065\u0072\u006e\u0065\u006cSessionId,questionCount:C.result?.totalQuestions??0,durationMs:Date.now()-O,error:yt(K),willRetry:ut,cleanup:C.cleanup,debug:C.debug,completedAt:new Date().toISOString()}),await Oa(l,A.disposableProxyId),k=K,ut)continue;if(C.result){let F=await ao(C.result);return Zs(no(F,r),p.maxQuestions,p.questionsOnly)}break}let N=C.result;if(!N)throw new Error("Harvest attempt completed without a result");if(fl(N,d)){let K=new Fe(Ml(N)),q=E<m-1;if(console.warn(JSON.stringify({event:"harvest_attempt_location_mismatch",attempt_number:f,max_attempts:m,message:K.message,will_retry:q})),await ve(a,{type:"finished",attemptNumber:f,maxAttempts:m,outcome:"location_mismatch",\u006b\u0065\u0072\u006e\u0065\u006cSessionId:C.cleanup.\u006b\u0065\u0072\u006e\u0065\u006cSessionId,questionCount:N.totalQuestions,durationMs:Date.now()-O,error:K.message,willRetry:q,cleanup:C.cleanup,debug:C.debug,completedAt:new Date().toISOString()}),await Oa(l,A.disposableProxyId),k=K,q)continue;break}let H=await ao(N),x=Zs(no(H,r),p.maxQuestions,p.questionsOnly),R=wl(x),J=R==="degraded_result"&&E<m-1,st=R==="degraded_result"?ml(x):null;if(J&&console.warn(JSON.stringify({event:"harvest_attempt_degraded_result",attempt_number:f,max_attempts:m,degradation_reasons:x.diagnostics.degradationReasons,will_retry:!0})),await ve(a,{type:"finished",attemptNumber:f,maxAttempts:m,outcome:R,\u006b\u0065\u0072\u006e\u0065\u006cSessionId:C.cleanup.\u006b\u0065\u0072\u006e\u0065\u006cSessionId,questionCount:x.totalQuestions,durationMs:Date.now()-O,error:st,willRetry:J,cleanup:C.cleanup,debug:C.debug,completedAt:new Date().toISOString()}),await Oa(l,A.disposableProxyId),J)continue;return(p.format==="json"||p.format==="both")&&await T.writeJSON(x,p.outputDir),(p.format==="csv"||p.format==="both")&&await Promise.all([T.writeCSV(x.flat,p.outputDir),x.videos.length>0?T.writeVideoCSV(x.videos,x.seed,p.outputDir):Promise.resolve(""),x.forums.length>0?T.writeForumCSV(x.forums,x.seed,p.outputDir):Promise.resolve(""),x.aiOverview.detected?T.writeAIOverviewCSV(x.aiOverview.citations,x.aiOverview.text,x.seed,p.outputDir):Promise.resolve(""),x.aiMode.detected?T.writeAIModeCSV(x.aiMode.citations,x.aiMode.text,x.seed,p.outputDir):Promise.resolve(""),x.whatPeopleSaying.length>0?T.writeWhatPeopleSayingCSV(x.whatPeopleSaying,x.seed,p.outputDir):Promise.resolve("")]),x}catch(A){let _=Ra(A);(_==="captcha"||_==="proxy_tunnel_failed")&&P==="location"&&(W++,W>=2&&(P="none"));let p=E<m-1&&(_==="captcha"||eo(_)||d==="location"&&to(_));if(_==="captcha"?console.warn(JSON.stringify({event:"harvest_attempt_captcha",attempt_number:f,max_attempts:m,message:yt(A),will_retry:p})):p&&console.warn(JSON.stringify({event:"harvest_attempt_proxy_retry",attempt_number:f,max_attempts:m,outcome:_,message:yt(A),will_retry:p})),await ve(a,{type:"finished",attemptNumber:f,maxAttempts:m,outcome:_,\u006b\u0065\u0072\u006e\u0065\u006cSessionId:null,questionCount:0,durationMs:Date.now()-O,error:yt(A),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()}),k=A,p)continue;if(_==="captcha")break;throw A}}throw k&&!(k instanceof et)?k:(console.warn(JSON.stringify({event:"harvest_captcha_exhausted",max_attempts:m,session_kind:l?"\u006b\u0065\u0072\u006e\u0065\u006c":"local"})),new et(Qi(`CAPTCHA on all ${m} fresh sessions. Try again in a few minutes.`)))}var wo=require("child_process"),rt=require("fs"),qa=require("os"),jt=require("path"),zt=require("@fal-ai/client");var Tl="https://api.deepinfra.com/v1/openai/chat/completions",ro="https://openrouter.ai/api/v1/chat/completions",Ba="Qwen/Qwen3.6-35B-A3B",Nl=`You are a video prompt engineer for Seedance 2.0 text-to-video AI.
|
|
21890
|
+
`)}};var Ys=require("crypto");var Vd="pr.oxylabs.io",Yd=7777,Kd=10;function Ks(){return process.env.OXYLABS_USERNAME?.trim()||void 0}function Js(){return process.env.OXYLABS_PASSWORD?.trim()||void 0}function Jd(){return(process.env.OXYLABS_COUNTRY?.trim()||"US").toUpperCase()}function qs(){return!!(Ks()&&Js())}function qd(){let e="";for(let t=0;t<10;t++)e+=String((0,Ys.randomInt)(0,10));return e}function Xd(e={}){let t=Ks();if(!t)return;let n=t.startsWith("customer-")?t:`customer-${t}`,a=(e.country?.trim()||Jd()).toUpperCase(),i=e.sticky?`-sessid-${qd()}-sesstime-${Kd}`:"";return`${n}-cc-${a}${i}`}function Aa(e){return e.replace(/pr\.oxylabs\.io(:\d+)?/gi,"backup proxy").replace(/oxylabs/gi,"backup proxy")}function jd(e){return`mcp-backup-custom-${`${Date.now()}-${e??0}-${Math.random().toString(36).slice(2,8)}`}`}async function Xs(e,t={}){let n=Xd({sticky:t.sticky}),a=Js();if(!n||!a)throw new Error("backup proxy pool is not configured");let i=new B({apiKey:e});try{let s=await i.proxies.create({type:"custom",name:jd(t.attemptIndex),protocol:"http",config:{host:Vd,port:Yd,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(Aa(o))}}var zd={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"},$d={"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 Qd(e){return e?e.slice(-6):null}function it(e,t,n,a,i,s=!1){return{kernelProxyId: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:Qd(n),target:a,error:i,disposable:s}}}function Zd(e){return e.trim().toLowerCase().replace(/\s+/g," ")}function tl(e){return e.trim().toLowerCase().replace(/\./g,"").replace(/\s+/g," ")}function el(e){if(!e)return!0;let t=tl(e);return t==="united states"||t==="united states of america"||t==="usa"||t==="us"}function js(e){let t=e.trim();return/^[A-Za-z]{2}$/.test(t)?t.toUpperCase():zd[Zd(t)]??null}function nl(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 Pt(e,t,n){return n?`mcp-serp-residential-${e.toLowerCase()}-${t.toLowerCase()}-${n}`:`mcp-serp-residential-${e.toLowerCase()}-${t.toLowerCase()}`}function xn(e,t){let n=`${Date.now()}-${t??0}-${Math.random().toString(36).slice(2,8)}`;return`${e}-fresh-${n}`}function al(e){return`mcp-serp-residential-us-zip-${e}`}function il(e,t){if(!e||t.toLowerCase()!=="us")return null;let n=Ns(e),a=n.split(",").map(c=>c.trim()).filter(Boolean);if(a.length>1&&el(a[a.length-1])&&(a=a.slice(0,-1)),a.length===1){let c=js(a[0]);return c?{canonicalLocation:n,level:"state",country:"US",state:c,city:"",cityCandidates:[],proxyName:Pt("US",c),config:{country:"US",state:c}}:null}let[i="",s=""]=a;if(!i||!s)return null;let o=js(s);if(!o)return null;let d=nl(i),l=d[0];return l?{canonicalLocation:n,level:"city",country:"US",state:o,city:l,cityCandidates:d,proxyName:Pt("US",o,l),config:{country:"US",state:o,city:l}}:null}function sl(e){return`${e.city}|${e.state}`}function zs(e,t){return t&&/^\d{5}$/.test(t)?t:$d[sl(e)]??null}function $s(e,t){return{...e,level:"zip",zip:t,proxyName:al(t),config:{country:e.country,state:e.state,zip:t}}}function La(e,t){return{...e,proxyName:t}}function Oa(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 ol(e,t){return e.find(n=>n.type==="residential"&&n.status!=="unavailable"&&!!n.id&&(n.name===t.proxyName||Oa(n.config,t,t.level==="city"?t.city:void 0)))??null}function rl(e,t){for(let n of t.cityCandidates){let a=Pt(t.country,t.state,n),i=e.find(s=>s.type==="residential"&&s.status!=="unavailable"&&!!s.id&&(s.name===a||Oa(s.config,t,n)));if(i)return i}return null}function On(e){return{...e,level:"state",proxyName:Pt(e.country,e.state),config:{country:e.country,state:e.state}}}function dl(e,t){let n=Pt(t.country,t.state);return e.find(a=>a.type==="residential"&&a.status!=="unavailable"&&!!a.id&&(a.name===n||Oa(a.config,t)))??null}function ll(e,t){return On(e)}function Et(e){return e instanceof Error?e.message:String(e)}function Sl(e,t,n){let a=[],i=zs(e,t);if(i){let o=$s(e,i);a.push(La(o,xn(o.proxyName,n)))}for(let o of e.cityCandidates){let d={...e,level:"city",city:o,proxyName:Pt(e.country,e.state,o),config:{country:e.country,state:e.state,city:o}};a.push(La(d,xn(d.proxyName,n)))}let s=On(e);return a.push(La(s,xn(s.proxyName,n))),a}async function xa(e,t,n){if(e.backupOnExhaustion&&n.length>0&&e.kernelApiKey&&qs())try{let a=await Xs(e.kernelApiKey,{attemptIndex:e.attemptIndex});return it("backup_custom",e.proxyMode,a,t,null,!0)}catch(a){n.push(`backup: ${Aa(Et(a))}`)}return it("configured_fallback",e.proxyMode,e.configuredKernelProxyId,t,n.join(" | "))}async function Ul(e,t,n){let a=[];for(let i of Sl(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 it("location_created",t.proxyMode,s.id,i,null,!0);a.push(`${i.proxyName}: Kernel did not return a proxy id`)}catch(s){a.push(`${i.proxyName}: ${Et(s)}`)}if(process.env.KERNEL_PROXY_TYPE!=="residential"){let i=On(n);try{let s=await e.proxies.create({type:"isp",name:xn(`mcp-serp-isp-${i.country.toLowerCase()}-${(i.state??"").toLowerCase()}`,t.attemptIndex),config:{country:i.country,...i.state?{state:i.state}:{}}});if(s.id)return it("isp_created",t.proxyMode,s.id,i,null,!0);a.push("isp: Kernel did not return a proxy id")}catch(s){a.push(`isp: ${Et(s)}`)}}return xa(t,n,a)}async function Qs(e,t){if(!e||!t)return;await new B({apiKey:e}).proxies.delete(t)}async function Zs(e){if(e.proxyMode==="none")return it("disabled",e.proxyMode,void 0,null,null);if(e.proxyMode==="configured")return it("configured_fallback",e.proxyMode,e.configuredKernelProxyId,null,null);let t=il(e.location,e.gl);if(!t||!e.kernelApiKey)return it("configured_fallback",e.proxyMode,e.configuredKernelProxyId,t,t?null:"location could not be normalized to a US city/state proxy target");let n=new B({apiKey:e.kernelApiKey});try{let a=e.attemptIndex??0;if(e.fresh)return await Ul(n,e,t);if(a>=1){let U=ll(t,a),r=[];try{let S=await n.proxies.create({type:"residential",name:U.proxyName,config:U.config});if(S.id)return it("location_created",e.proxyMode,S.id,U,null);r.push(`${U.state}: Kernel did not return a proxy id`)}catch(S){r.push(`${U.state}: ${Et(S)}`)}return xa(e,U,r)}let i=[];for await(let U of await n.proxies.list())i.push(U);let s=zs(t,e.proxyZip),o=[];if(s){let U=$s(t,s),r=ol(i,U);if(r?.id)return it("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 it("location_created",e.proxyMode,S.id,U,null);o.push(`${s}: Kernel did not return a proxy id`)}catch(S){o.push(`${s}: ${Et(S)}`)}}let d=rl(i,t);if(d?.id)return it("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:Pt(t.country,t.state,U),config:{country:t.country,state:t.state,city:U}});if(r.id)return it("location_created",e.proxyMode,r.id,{...t,level:"city",city:U,proxyName:Pt(t.country,t.state,U),config:{country:t.country,state:t.state,city:U}},null);o.push(`${U}: Kernel did not return a proxy id`)}catch(r){o.push(`${U}: ${Et(r)}`)}let l=On(t),c=dl(i,l);if(c?.id)return it("location_reused",e.proxyMode,c.id,l,o.join(" | "));try{let U=await n.proxies.create({type:"residential",name:l.proxyName,config:l.config});if(U.id)return it("location_created",e.proxyMode,U.id,l,o.join(" | "));o.push(`${l.state}: Kernel did not return a proxy id`)}catch(U){o.push(`${l.state}: ${Et(U)}`)}return xa(e,t,o)}catch(a){return it("configured_fallback",e.proxyMode,e.configuredKernelProxyId,t,Et(a))}}var ul=3,cl=5;function Ra(e){return e.reason instanceof DOMException&&e.reason.name==="TimeoutError"?e.reason:new Ut}function hl(e){if(!e||typeof e!="object")return;let t=e.signal;if(t instanceof AbortSignal)return t}function gl(e){if(!e||typeof e!="object")return;let t=e.onAttemptEvent;return typeof t=="function"?t:void 0}function wl(e){if(!e||typeof e!="object")return;let t=e.onPaaProgress;return typeof t=="function"?t:void 0}async function be(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 Ba(e){if(e instanceof et)return"captcha";if(e instanceof Ge)return"location_mismatch";if(e instanceof Ut)return"request_aborted";if(e instanceof Kt)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":kl(t)?"proxy_tunnel_failed":Tl(t)?"proxy_unavailable":/timeout|timed out|Timeout \d+ms exceeded|deadline/i.test(t)?"timeout":"error"}function pl(e){return e.diagnostics.degradedResult?"degraded_result":e.diagnostics?.completionStatus??(e.totalQuestions>0?"paa_found":"no_paa")}function ml(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 to(e,t,n){let a=ml(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),c=e.diagnostics.completionStatus==="paa_found",U=e.diagnostics.paaLifecycle?.discoveryStatus,r=U==="interrupted"||U==="recovery_exhausted",S=c&&(s<t&&U!=="frontier_exhausted"||o>0||d>0||!!e.diagnostics.interruption||r),u=l?"complete":c&&s>0?S||i?"partial":"complete":a?i?"partial":"complete":"degraded",h=u==="degraded";return e.diagnostics={...e.diagnostics,resultQuality:u,degradedResult:h,degradationReasons:h?["empty_primary_serp"]:[],retryRecommended:h||u==="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 yl(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 yt(e){return e instanceof Error?e.message:String(e)}function Cl(e){return e==="location"?cl:ul}function kl(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 Tl(e){return/proxy unavailable|proxy_unavailable|connection_test_failed|did not return a proxy id|configured fallback/i.test(e)}function eo(e){return e==="captcha"||e==="proxy_tunnel_failed"||e==="proxy_unavailable"}function no(e){return e==="browser_session_interrupted"}function Ml(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 fl(e,t){return t==="location"&&e.diagnostics.debug?.locationEvidence?.status==="mismatch"}function ao(e,t){if(t||!e.diagnostics.debug)return e;let n={...e.diagnostics};return delete n.debug,{...e,diagnostics:n}}async function io(e){try{return await Bs(e,{totalBudgetMs:1e4})}catch(t){return console.warn(JSON.stringify({event:"harvest_goto_resolution_failed",message:yt(t)})),e}}async function Wa(e,t){if(!(!e||!t))try{await Qs(e,t),console.info(JSON.stringify({event:"kernel_proxy_deleted",proxy_id_suffix:t.slice(-6)}))}catch(n){console.warn(JSON.stringify({event:"kernel_proxy_delete_failed",proxy_id_suffix:t.slice(-6),message:yt(n)}))}}async function so(e,t,n=!1,a,i){let s=ys(e,{forceManaged:n}),o=new Ln,d=new In(s,o,a,i);if(t?.aborted)return{result:null,error:Ra(t),cleanup:await s.close(),debug:null};let l,c=t?new Promise((h,m)=>{l=()=>m(Ra(t)),t.addEventListener("abort",l,{once:!0})}):null,U=null,r=null,S,u=null;try{let h=d.extract(e,t);c&&h.catch(()=>{}),U=await(c?Promise.race([h,c]):h)}catch(h){r=h}finally{t&&l&&t.removeEventListener("abort",l),u=U?.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:s.getDebugSnapshot()}:null),S=await s.close()}if(r){let h=Ba(r),m=h==="timeout"||h==="request_aborted"||h==="browser_session_interrupted"?h:null,I=null;if(m)try{I=typeof d.getPartialResult=="function"?d.getPartialResult():null}catch{I=null}return I&&m?(I.diagnostics={...I.diagnostics,interruption:{code:m,reasonSource:"client_runtime",message:yt(r)}},{result:I,error:r,cleanup:S,debug:u,salvagedFrom:r}):{result:null,error:r,cleanup:S,debug:u}}return{result:U,error:null,cleanup:S,debug:u}}async function oo(e){let t=typeof e=="object"&&e!==null?e:{},n=hl(e),a=gl(e),i=wl(e),s=t.forceManagedSerp===!0,o=t.proxyMode,d=o==="location"||o==="none"||o==="configured"?o:Kn,l=typeof t.kernelApiKey=="string"?t.kernelApiKey.trim():ii(),c=typeof t.kernelProxyId=="string"?t.kernelProxyId.trim():void 0,U={kernelApiKey:l,proxyMode:d,configuredKernelProxyId:c,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"},r=typeof t.debug=="boolean"?t.debug:!1,S=d==="location"&&!!U.location,u=t.maxAttempts,m=typeof u=="number"&&Number.isInteger(u)&&u>=1?u:Cl(d)+1,I=typeof t.softDeadlineMs=="number"?t.softDeadlineMs:null,f=new An,k=null,_=d,W=0;for(let E=0;E<m;E++){let M=E+1,O=Date.now();try{if(n?.aborted)throw Ra(n);let A=await Zs({...U,proxyMode:_,attemptIndex:E,fresh:_==="location"});if(!A)break;let P={...t,kernelApiKey:l,kernelProxyId:A.kernelProxyId,kernelProxyResolution:A.resolution,proxyMode:_,keepDefaultProxy:E>0,debug:r||S};_==="none"&&(P.kernelProxyId=void 0);let p=ni.parse(P);await be(a,{type:"started",attemptNumber:M,maxAttempts:m,query:p.query,location:p.location??null,maxQuestions:p.maxQuestions,startedAt:new Date(O).toISOString()}),console.info(JSON.stringify({event:"harvest_attempt_started",attempt_number:M,max_attempts:m,query:p.query,location:p.location??null,max_questions:p.maxQuestions}));let b=Jt(),y=K=>be(a,{type:"provider_session_started",attemptNumber:M,maxAttempts:m,provider:K.provider,providerSessionId:K.providerSessionId,observedAt:new Date().toISOString()}),L=b?.harvestBrowserMode==="headless-test"&&process.env[ns]==="true",C=b?await is({...b,forceHeadless:L,forceHeadful:!L},()=>so(p,n,s,i,y)):await so(p,n,s,i,y);if(C.error){let K=C.error,q=Ba(K);(q==="captcha"||q==="proxy_tunnel_failed")&&_==="location"&&(W++,W>=2&&(_="none"));let ut=E<m-1&&(q==="captcha"||no(q)||d==="location"&&eo(q));if(q==="captcha"?console.warn(JSON.stringify({event:"harvest_attempt_captcha",attempt_number:M,max_attempts:m,message:yt(K),will_retry:ut})):ut&&console.warn(JSON.stringify({event:"harvest_attempt_proxy_retry",attempt_number:M,max_attempts:m,outcome:q,message:yt(K),will_retry:ut})),await be(a,{type:"finished",attemptNumber:M,maxAttempts:m,outcome:q,kernelSessionId:C.cleanup.kernelSessionId,questionCount:C.result?.totalQuestions??0,durationMs:Date.now()-O,error:yt(K),willRetry:ut,cleanup:C.cleanup,debug:C.debug,completedAt:new Date().toISOString()}),await Wa(l,A.disposableProxyId),k=K,ut)continue;if(C.result){let F=await io(C.result);return to(ao(F,r),p.maxQuestions,p.questionsOnly)}break}let N=C.result;if(!N)throw new Error("Harvest attempt completed without a result");if(fl(N,d)){let K=new Ge(Ml(N)),q=E<m-1;if(console.warn(JSON.stringify({event:"harvest_attempt_location_mismatch",attempt_number:M,max_attempts:m,message:K.message,will_retry:q})),await be(a,{type:"finished",attemptNumber:M,maxAttempts:m,outcome:"location_mismatch",kernelSessionId:C.cleanup.kernelSessionId,questionCount:N.totalQuestions,durationMs:Date.now()-O,error:K.message,willRetry:q,cleanup:C.cleanup,debug:C.debug,completedAt:new Date().toISOString()}),await Wa(l,A.disposableProxyId),k=K,q)continue;break}let H=await io(N),x=to(ao(H,r),p.maxQuestions,p.questionsOnly),R=pl(x),J=R==="degraded_result"&&E<m-1,st=R==="degraded_result"?yl(x):null;if(J&&console.warn(JSON.stringify({event:"harvest_attempt_degraded_result",attempt_number:M,max_attempts:m,degradation_reasons:x.diagnostics.degradationReasons,will_retry:!0})),await be(a,{type:"finished",attemptNumber:M,maxAttempts:m,outcome:R,kernelSessionId:C.cleanup.kernelSessionId,questionCount:x.totalQuestions,durationMs:Date.now()-O,error:st,willRetry:J,cleanup:C.cleanup,debug:C.debug,completedAt:new Date().toISOString()}),await Wa(l,A.disposableProxyId),J)continue;return(p.format==="json"||p.format==="both")&&await f.writeJSON(x,p.outputDir),(p.format==="csv"||p.format==="both")&&await Promise.all([f.writeCSV(x.flat,p.outputDir),x.videos.length>0?f.writeVideoCSV(x.videos,x.seed,p.outputDir):Promise.resolve(""),x.forums.length>0?f.writeForumCSV(x.forums,x.seed,p.outputDir):Promise.resolve(""),x.aiOverview.detected?f.writeAIOverviewCSV(x.aiOverview.citations,x.aiOverview.text,x.seed,p.outputDir):Promise.resolve(""),x.aiMode.detected?f.writeAIModeCSV(x.aiMode.citations,x.aiMode.text,x.seed,p.outputDir):Promise.resolve(""),x.whatPeopleSaying.length>0?f.writeWhatPeopleSayingCSV(x.whatPeopleSaying,x.seed,p.outputDir):Promise.resolve("")]),x}catch(A){let P=Ba(A);(P==="captcha"||P==="proxy_tunnel_failed")&&_==="location"&&(W++,W>=2&&(_="none"));let p=E<m-1&&(P==="captcha"||no(P)||d==="location"&&eo(P));if(P==="captcha"?console.warn(JSON.stringify({event:"harvest_attempt_captcha",attempt_number:M,max_attempts:m,message:yt(A),will_retry:p})):p&&console.warn(JSON.stringify({event:"harvest_attempt_proxy_retry",attempt_number:M,max_attempts:m,outcome:P,message:yt(A),will_retry:p})),await be(a,{type:"finished",attemptNumber:M,maxAttempts:m,outcome:P,kernelSessionId:null,questionCount:0,durationMs:Date.now()-O,error:yt(A),willRetry:p,cleanup:{kernelSessionId:null,kernelDeleteStarted:!1,kernelDeleteSucceeded:null,kernelDeleteError:null,browserCloseSucceeded:null,browserCloseError:null},debug:null,completedAt:new Date().toISOString()}),k=A,p)continue;if(P==="captcha")break;throw A}}throw k&&!(k instanceof et)?k:(console.warn(JSON.stringify({event:"harvest_captcha_exhausted",max_attempts:m,session_kind:l?"kernel":"local"})),new et(Zi(`CAPTCHA on all ${m} fresh sessions. Try again in a few minutes.`)))}var po=require("child_process"),rt=require("fs"),Xa=require("os"),zt=require("path"),$t=require("@fal-ai/client");var Nl="https://api.deepinfra.com/v1/openai/chat/completions",lo="https://openrouter.ai/api/v1/chat/completions",Ha="Qwen/Qwen3.6-35B-A3B",vl=`You are a video prompt engineer for Seedance 2.0 text-to-video AI.
|
|
21792
21891
|
|
|
21793
21892
|
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.
|
|
21794
21893
|
|
|
@@ -21810,20 +21909,20 @@ Rules for visuals:
|
|
|
21810
21909
|
- Describe exactly what the camera sees
|
|
21811
21910
|
|
|
21812
21911
|
Respond with JSON only:
|
|
21813
|
-
{"clip1": "...", "clip2": "...", "voiceover": "...", "audioMood": "..."}`;async function
|
|
21912
|
+
{"clip1": "...", "clip2": "...", "voiceover": "...", "audioMood": "..."}`;async function ro(e,t,n,a){let i=await fetch(t,{method:"POST",headers:{Authorization:`Bearer ${e}`,"Content-Type":"application/json"},body:JSON.stringify({model:Ha,temperature:.7,messages:[{role:"system",content:vl},{role:"user",content:`Question: ${n}
|
|
21814
21913
|
|
|
21815
|
-
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===
|
|
21816
|
-
[1/7] Generating prompts via QWEN 3.6...`);let s=await
|
|
21817
|
-
[2/7] Generating clip 1 (text-to-video)...`);let o=await
|
|
21818
|
-
[3/7] Extracting last frame \u2192 clip 2 start...`);let d=await
|
|
21819
|
-
[4/7] Generating clip 2 (image-to-video from last frame)...`);let U=a.seed!==void 0?a.seed+1:void 0,r=await
|
|
21820
|
-
[5/7] Concatenating clips + generating voiceover (parallel)...`);let[S,u]=await Promise.all([
|
|
21821
|
-
[6/7] Adding background audio via MMAudio V2...`);let h=await
|
|
21822
|
-
[7/7] Overlaying voiceover on final video...`);let
|
|
21914
|
+
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===lo?Ta({vendor:"openrouter",model:Ha,units:s.usage?.cost??0,unitType:"usd"}):Ta({vendor:"deepinfra_qwen",model:Ha,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 Wn(e,t){let n=process.env.DEEPINFRA_API_KEY,a=process.env.OPENROUTER_API_KEY;if(n)try{return await ro(n,Nl,e,t)}catch(i){console.warn("[promptBuilder] DeepInfra failed, trying OpenRouter:",i.message)}if(a)return await ro(a,lo,e,t);throw new Error("No LLM key \u2014 set DEEPINFRA_API_KEY or OPENROUTER_API_KEY")}function So(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 ze=require("fs"),Da=require("path"),uo=require("os"),Rn=require("@fal-ai/client"),bl="fal-ai/mmaudio-v2",_l="fal-ai/elevenlabs/tts",El="fal-ai/google/gemini-2.5-flash-preview-tts";async function Uo(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 Fa(e){console.log("[AudioGenerator] Generating voiceover...");let t=(0,Da.join)((0,uo.tmpdir)(),`tts-${Date.now()}`);(0,ze.mkdirSync)(t,{recursive:!0});let n=(0,Da.join)(t,"voiceover.mp3");try{let s=process.env.ELEVENLABS_VOICE_ID??"pNInz6obpgDQGcFmaJgB",o=await Rn.fal.run(_l,{input:{text:e,voice_id:s,model_id:"eleven_v3"}});return(0,ze.writeFileSync)(n,await Uo(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 Rn.fal.run(El,{input:{text:e,voice:a}});return(0,ze.writeFileSync)(n,await Uo(i.audio.url)),console.log("[AudioGenerator] TTS: Gemini via fal"),n}async function Ga(e,t,n){return console.log("[AudioGenerator] Adding background audio via MMAudio V2..."),(await Rn.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 Ya=require("child_process"),Bn=require("fs"),_e=require("path"),co=require("@fal-ai/client");async function Va(e,t){let n=await fetch(e);if(!n.ok)throw new Error(`Download failed (${n.status}): ${e}`);(0,Bn.writeFileSync)(t,Buffer.from(await n.arrayBuffer()))}async function Ka(e,t,n){(0,Bn.mkdirSync)(n,{recursive:!0});let a=Date.now(),i=(0,_e.join)(n,`clip1-${a}.mp4`),s=(0,_e.join)(n,`clip2-${a}.mp4`),o=(0,_e.join)(n,`combined-${a}.mp4`);return console.log("[VideoMixer] Downloading clips..."),await Promise.all([Va(e,i),Va(t,s)]),console.log("[VideoMixer] Concatenating..."),(0,Ya.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 Ja(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 qa(e,t,n){let a=Date.now(),i=(0,_e.join)(n,`voiceover-${a}.wav`),s=(0,_e.join)(n,`final-${a}.mp4`);return console.log("[VideoMixer] Downloading voiceover..."),await Va(t,i),console.log("[VideoMixer] Mixing voiceover over background audio..."),(0,Ya.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 ho="bytedance/seedance-2.0/text-to-video",go="bytedance/seedance-2.0/image-to-video";function Hn(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 Dn(e,t){let{request_id:n}=await $t.fal.queue.submit(e,{input:t});for(console.log(`[fal] submitted ${e} \u2192 ${n}`);;){await new Promise(s=>setTimeout(s,5e3));let a=await $t.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 $t.fal.queue.result(e,{requestId:n})).data}}async function wo(e,t){let n=Date.now(),a=(0,zt.join)(t,`clip1-raw-${n}.mp4`),i=(0,zt.join)(t,`last-frame-${n}.jpg`),s=await fetch(e);if(!s.ok)throw new Error(`Failed to download clip 1 (${s.status})`);(0,rt.writeFileSync)(a,Buffer.from(await s.arrayBuffer()));try{(0,po.execSync)(`ffmpeg -sseof -0.1 -i "${a}" -vframes 1 -y "${i}" -loglevel error`)}finally{try{(0,rt.unlinkSync)(a)}catch{}}return i}var Fn=class{constructor(t){let n=t??process.env.FAL_KEY;if(!n)throw new Error("FAL_KEY is required");$t.fal.config({credentials:n})}async generateClipPair(t,n,a={}){let i=a.outputDir??(0,zt.join)((0,Xa.tmpdir)(),`paa-video-${Date.now()}`);(0,rt.mkdirSync)(i,{recursive:!0}),console.log(`
|
|
21915
|
+
[1/7] Generating prompts via QWEN 3.6...`);let s=await Wn(t,n);console.log(" Voiceover:",s.voiceover),console.log(" Audio mood:",s.audioMood),console.log(`
|
|
21916
|
+
[2/7] Generating clip 1 (text-to-video)...`);let o=await Dn(ho,Hn(s.clip1,a,a.seed));console.log(`
|
|
21917
|
+
[3/7] Extracting last frame \u2192 clip 2 start...`);let d=await wo(o.video.url,i),l=new Blob([(0,rt.readFileSync)(d)],{type:"image/jpeg"}),c=await $t.fal.storage.upload(l);try{(0,rt.unlinkSync)(d)}catch{}console.log(`
|
|
21918
|
+
[4/7] Generating clip 2 (image-to-video from last frame)...`);let U=a.seed!==void 0?a.seed+1:void 0,r=await Dn(go,Hn(s.clip2,a,U,c));console.log(`
|
|
21919
|
+
[5/7] Concatenating clips + generating voiceover (parallel)...`);let[S,u]=await Promise.all([Ka(o.video.url,r.video.url,i),Fa(s.voiceover)]);console.log(`
|
|
21920
|
+
[6/7] Adding background audio via MMAudio V2...`);let h=await Ja(S),m=(a.clipDurationSeconds??8)*2,I=await Ga(h,s.audioMood,m);console.log(`
|
|
21921
|
+
[7/7] Overlaying voiceover on final video...`);let f=(0,zt.join)(i,`with-bg-audio-${Date.now()}.mp4`),k=await fetch(I);(0,rt.writeFileSync)(f,Buffer.from(await k.arrayBuffer()));let _=await qa(f,u,i);return{clip1Url:o.video.url,clip2Url:r.video.url,finalVideoPath:_,seed:o.seed,promptClip1:s.clip1,promptClip2:s.clip2,voiceover:s.voiceover,audioMood:s.audioMood}}async generateEpisode(t,n={}){let a=n.outputDir??(0,zt.join)((0,Xa.tmpdir)(),`episode-${t.episodeNumber}-${Date.now()}`);(0,rt.mkdirSync)(a,{recursive:!0});let i=So(t);console.log(`
|
|
21823
21922
|
[Episode ${t.episodeNumber}/${t.episodeCount}] ${t.sectionTitle}`),console.log(" Voiceover:",i.voiceover),console.log(`
|
|
21824
|
-
[2/7] Generating clip 1 (text-to-video)...`);let s=await
|
|
21825
|
-
[3/7] Extracting last frame \u2192 clip 2 start...`);let o=await
|
|
21826
|
-
[4/7] Generating clip 2 (image-to-video from last frame)...`);let c=n.seed!==void 0?n.seed+1:void 0,U=await
|
|
21827
|
-
[5/7] Concatenating clips + generating voiceover (parallel)...`);let[r,S]=await Promise.all([
|
|
21828
|
-
[6/7] Adding background audio via MMAudio V2...`);let u=await
|
|
21829
|
-
[7/7] Overlaying voiceover on final video...`);let I=(0,
|
|
21923
|
+
[2/7] Generating clip 1 (text-to-video)...`);let s=await Dn(ho,Hn(i.clip1,n,n.seed));console.log(`
|
|
21924
|
+
[3/7] Extracting last frame \u2192 clip 2 start...`);let o=await wo(s.video.url,a),d=new Blob([(0,rt.readFileSync)(o)],{type:"image/jpeg"}),l=await $t.fal.storage.upload(d);try{(0,rt.unlinkSync)(o)}catch{}console.log(`
|
|
21925
|
+
[4/7] Generating clip 2 (image-to-video from last frame)...`);let c=n.seed!==void 0?n.seed+1:void 0,U=await Dn(go,Hn(i.clip2,n,c,l));console.log(`
|
|
21926
|
+
[5/7] Concatenating clips + generating voiceover (parallel)...`);let[r,S]=await Promise.all([Ka(s.video.url,U.video.url,a),Fa(i.voiceover)]);console.log(`
|
|
21927
|
+
[6/7] Adding background audio via MMAudio V2...`);let u=await Ja(r),h=(n.clipDurationSeconds??8)*2,m=await Ga(u,i.audioMood,h);console.log(`
|
|
21928
|
+
[7/7] Overlaying voiceover on final video...`);let I=(0,zt.join)(a,`with-bg-audio-${Date.now()}.mp4`),f=await fetch(m);(0,rt.writeFileSync)(I,Buffer.from(await f.arrayBuffer()));let k=await qa(I,S,a);return{clip1Url:s.video.url,clip2Url:U.video.url,finalVideoPath:k,seed:s.seed,promptClip1:i.clip1,promptClip2:i.clip2,voiceover:i.voiceover,audioMood:i.audioMood}}};0&&(module.exports={VideoGenerator,buildClipPrompts,harvest});
|