mcp-scraper 0.96.1 → 0.96.2

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.
@@ -1,4 +1,4 @@
1
- import{a as An,e as Je,s as il}from"./chunk-JHT6A4U6.js";import{b as ol}from"./chunk-7POHNTVW.js";import{j as Gs,k as Ws,l as Bs,m as Ks}from"./chunk-MVMWPKRC.js";import{c as Vs,e as _o,f as Js,h as Io,j as ko,k as Ro,l as To,m as Ys,n as Zr,p as Co,q as Xs,r as Ao,s as Po,u as Zs}from"./chunk-2IATUJO6.js";import{$ as As,aa as Ps,c as ho,d as Kr,e as fo,f as bo,g as Cs}from"./chunk-CORWWDHL.js";import{e as Br,h as Dt,j as er}from"./chunk-5LYINW7Z.js";import{a as Ls,c as rr,d as Us,e as ir,f as $s,g as qs}from"./chunk-CNSVZTTT.js";import{a as Ms,b as wo}from"./chunk-5BKSKLHX.js";import{f as js}from"./chunk-7SR7MEU5.js";import{e as nr,g as Xr,i as Es}from"./chunk-SEZETSB3.js";import{b as Os,c as Yr,i as tr}from"./chunk-SDQW7IJN.js";import{a as xs,c as go,e as Cn,f as Gr,i as Ts}from"./chunk-B4ZKPLSP.js";import{d as ks,g as Rs}from"./chunk-VVSHZV5S.js";import{c as _s,f as Wr,g as qt,h as Xe,i as Qn}from"./chunk-C7MGJUTU.js";import{c as Is}from"./chunk-4ZVO6GX4.js";import{a as Ds,b as Hs,c as Ns,d as So,e as zs,f as Fs,h as Qs,i as el,k as tl,m as nl,n as rl}from"./chunk-24YDQ3EO.js";import{b as Vr,c as Ve,d as vo,e as Jr}from"./chunk-OILOHQNL.js";import{a as xo}from"./chunk-CVQUHVHX.js";import{z as Ae}from"zod";var Qr=class extends Error{constructor(e){super("lead_provider_admission_denied",{cause:e}),this.name="LeadProviderAdmissionError"}};var sl=["president\\s*[/&-]\\s*owner","owner\\s*[/&-]\\s*president","chief executive officer","operations manager","general manager","managing member","managing partner","business owner","co[- ]founder","team member","president","principal","director","manager","partner","founder","owner","ceo"].join("|"),Oo=String.raw`(?:Mr\.?|Ms\.?|Mrs\.?|Dr\.?)?\s*([\p{Lu}][\p{L}'’.-]+(?:\s+[\p{Lu}][\p{L}'’.-]+){1,3})`,Wv=`${Oo}\\s*(?:,|[-\u2013\u2014]|\\bis\\b|\\bas\\b|\\bserves as\\b)?\\s*(${sl})\\b`,Bv=`\\b(${sl})\\b\\s*(?:[,:\u2013\u2014-]|\\bis\\b|\\bof\\b)?\\s*${Oo}`,Kv=`\\b((?:co[- ]?)?owned|founded)\\s+by\\s+${Oo}`,Vv=new Set(["about","additional","business","company","contact","customer","details","information","management","member","owner","president","profile","roofing","team","website"]),Jv=new Set(["co","company","corp","corporation","inc","incorporated","llc","ltd","limited","pllc"]);function or(t,e=400){return t.replace(/\s+/g," ").trim().slice(0,e)}function Ht(t){return t.toLowerCase().replace(/[^a-z0-9]+/g," ").trim().split(/\s+/).filter(Boolean)}function ll(t){let e=Ht(t);for(;e.length&&Jv.has(e[e.length-1]);)e.pop();return e}function Yv(t){return t.replace(/^(?:Mr\.?|Ms\.?|Mrs\.?|Dr\.?)\s+/i,"").replace(/[.!?]\s+.*$/,"").replace(/[.,;:!?]+$/,"").replace(/\s+/g," ").trim()}function Xv(t,e){let n=Ht(t);if(n.length<2||n.length>4||n.some(o=>Vv.has(o)))return!1;let r=new Set(ll(e));return n.some(o=>!r.has(o))}function Zv(t){let e=t.toLowerCase().replace(/\s+/g," ");return/founder/.test(e)?"founder":/owner|managing member|partner/.test(e)?"owner":/manager/.test(e)?"manager":/president|chief executive officer|\bceo\b|principal|director/.test(e)?"executive":"team_member"}function Qv(t){return/founded/i.test(t)?"Founder":/owned/i.test(t)?"Owner":or(t,80).replace(/\bceo\b/i,"CEO").replace(/\b\w/g,e=>e.toUpperCase())}function cl(t){return or(`${t.title} | ${t.snippet??""}`)}function dl(t){return t.map((e,n)=>({result:e,index:n})).sort((e,n)=>{let r=Number.isFinite(e.result.position)?e.result.position:Number.MAX_SAFE_INTEGER,o=Number.isFinite(n.result.position)?n.result.position:Number.MAX_SAFE_INTEGER;return r-o||e.index-n.index}).map(e=>e.result)}function ew(t){if(!t)return null;try{let e=new URL(t);return e.protocol==="http:"||e.protocol==="https:"?e.href:null}catch{return null}}function al(t){return`"${t.replace(/["\\]/g," ").replace(/\s+/g," ").trim()}"`}function ei(t){return or(t??"",120).replace(/["\\]/g," ")}function tw(t){return t==="owners"?'"{business}" owner of company {location}':'"{business}" leadership team {location}'}function ul(t){let e={business:ei(t.businessName),city:ei(t.city),region:ei(t.region),location:ei(t.location)},n=(t.queryTemplates?.length?t.queryTemplates:[tw(t.mode)]).map(r=>or(r.replace(/\{(business|city|region|location)\}/g,(o,s)=>e[s]),240)).filter(Boolean);return[...new Set(n)].slice(0,3)}function pl(t){return`site:linkedin.com/in ${al(t.personName)} ${al(t.businessName)}`}function ml(t){let e=new Map,n=[],r=t.aiOverview?.detected?or(t.aiOverview.text??""):"";if(r){let o=ew(t.aiOverview?.shareUrl)??(t.query?`https://www.google.com/search?q=${encodeURIComponent(t.query)}`:"https://www.google.com/"),s=(t.aiOverview?.citations??[]).flatMap(l=>{try{let m=new URL(l.href);return["http:","https:"].includes(m.protocol)?[m.href]:[]}catch{return[]}}).slice(0,5);n.push({sourceType:"ai_overview",sourceUrl:o,sourceTitle:"Google AI Overview",sourcePosition:0,context:r,...s.length?{citationUrls:s}:{}})}for(let o of dl(t.results))n.push({sourceType:"serp_snippet",sourceUrl:o.url,sourceTitle:o.title,sourcePosition:o.position,context:cl(o)});for(let o of n){let s=o.context;if(!yl(s,t.businessName))continue;let l=t.city||(t.location??"").split(",")[0]||"",m=Ht(l).filter(v=>v.length>2),c=m.some(v=>Ht(s).includes(v));if(o.sourceType==="ai_overview"&&m.length&&!c)continue;let f=[];for(let v of s.matchAll(new RegExp(Wv,"giu")))f.push({name:v[1]??"",role:v[2]??""});for(let v of s.matchAll(new RegExp(Bv,"giu")))f.push({name:v[2]??"",role:v[1]??""});for(let v of s.matchAll(new RegExp(Kv,"giu")))f.push({name:v[2]??"",role:v[1]??""});for(let v of f){let d=Yv(v.name),p=Qv(v.role);if(!Xv(d,t.businessName))continue;let h=Zv(p);if(t.mode==="owners"&&h!=="owner"&&h!=="founder")continue;let w=Ht(d).join(" ");if(!w)continue;let R={sourceType:o.sourceType,purpose:"role",sourceUrl:o.sourceUrl,sourceTitle:o.sourceTitle,sourcePosition:o.sourcePosition,context:s,...o.citationUrls?{citationUrls:o.citationUrls}:{}},A=e.get(w);if(A){A.sources.length<2&&!A.sources.some(P=>P.sourceUrl===R.sourceUrl)&&A.sources.push(R),A.reasons=[...new Set([...A.reasons,"multiple_search_sources"])],A.confidence=Math.max(A.confidence,o.sourceType==="ai_overview"?c?.93:.9:.88);continue}let C=o.sourceType==="ai_overview";if(e.set(w,{name:d,role:p,relationship:h,linkedinUrl:null,confidence:C?c?.93:.9:.88,reasons:[C?"name_role_in_ai_overview":"name_role_in_search_result","exact_business_query_context",...C&&c?["ai_overview_location_match"]:[]],sources:[R]}),e.size>=5)return[...e.values()]}}return[...e.values()]}function nw(t){try{let e=new URL(t),n=e.hostname.toLowerCase().replace(/^www\./,"");return e.protocol!=="https:"||n!=="linkedin.com"||!/^\/in\/[^/]+\/?$/i.test(e.pathname)?null:(e.search="",e.hash="",e.href)}catch{return null}}function rw(t,e){let n=new Set(Ht(t)),r=Ht(e);return r.length>=2&&r.every(o=>n.has(o))}function yl(t,e){let n=new Set(Ht(t)),r=ll(e);if(r.length<2)return!1;let o=r.filter(s=>n.has(s)).length;return o>=2&&o/r.length>=.6}function gl(t){for(let e of dl(t.results)){let n=nw(e.url);if(!n)continue;let r=cl(e);if(!rw(r,t.person.name)||!yl(r,t.businessName))continue;let s=Ht((t.location??"").split(",")[0]??"").filter(l=>l.length>2).some(l=>Ht(r).includes(l));return{...t.person,linkedinUrl:n,confidence:s?.97:.94,reasons:[...new Set([...t.person.reasons,"linkedin_name_match","linkedin_company_match",...s?["linkedin_location_match"]:[]])],sources:[...t.person.sources,{sourceType:"serp_snippet",purpose:"linkedin",sourceUrl:e.url,sourceTitle:e.title,sourcePosition:e.position,context:r}]}}return t.person}var iw=Ae.union([Ae.string(),Ae.number().finite(),Ae.boolean(),Ae.null()]),Il="enriched_",ow=250,hl=100,aw=32*1024,fl=500*1024,sw=5*1024*1024,lw=Ae.array(Ae.record(Ae.string(),iw)).min(1).max(ow).superRefine((t,e)=>{t.forEach((n,r)=>{Object.keys(n).forEach(o=>{o.toLowerCase().startsWith(Il)&&e.addIssue({code:"custom",message:`Input header ${o} uses the reserved enriched_ prefix`,path:[r,o]})})})}),cw=Ae.object({name:Ae.string().min(1),location:Ae.string().min(1).optional(),city:Ae.string().min(1).optional(),region:Ae.string().min(1).optional(),organization:Ae.string().min(1).optional(),websiteUrl:Ae.string().min(1).optional(),profileUrl:Ae.string().min(1).optional(),phone:Ae.string().min(1).optional(),email:Ae.string().min(1).optional(),entityType:Ae.string().min(1).optional()}).strict(),dw=/\{(?:business|city|region|location)\}/g,uw=Ae.string().trim().min(1).max(240).refine(t=>!/[\r\n]/.test(t),"People query templates must be one line").refine(t=>t.includes("{business}"),"People query templates must include {business}").refine(t=>!/[{}]/.test(t.replace(dw,"")),"People query templates may use only {business}, {city}, {region}, and {location}"),jo=Ae.array(uw).min(1).max(3),kl=Ae.object({resolveMissingUrls:Ae.boolean().default(!0),defaultEntityType:Ae.enum(["business","person"]).default("business"),gl:Ae.string().regex(/^[a-z]{2}$/i).transform(t=>t.toLowerCase()).default("us"),hl:Ae.string().regex(/^[a-z]{2}(?:-[a-z]{2})?$/i).transform(t=>t.toLowerCase()).default("en"),maxPagesPerLead:Ae.number().int().min(1).max(4).default(3),concurrency:Ae.number().int().min(1).max(5).default(3),emailSearchFallback:Ae.enum(["off","serp_snippets"]).default("off"),peopleDiscovery:Ae.enum(["off","owners","leadership"]).default("off"),peopleQueryTemplates:jo.optional(),maxPeoplePerLead:Ae.number().int().min(1).max(3).default(2),outputFormats:Ae.array(Ae.enum(["csv","xlsx"])).min(1).max(2).transform(t=>[...new Set(t)]).default(["csv","xlsx"])}).strict(),pw={name:["name","business_name","business name","company","company_name","full_name","full name"],location:["location","market","address"],city:["city","town"],region:["region","state","province"],organization:["organization","organisation","brokerage","agency","company"],websiteUrl:["websiteUrl","website_url","website url","website","url"],profileUrl:["profileUrl","profile_url","profile url","profile"],phone:["phone","phone_number","phone number","telephone"],email:["email","email_address","email address"],entityType:["entityType","entity_type","entity type","type"]};function mw(t){return Buffer.byteLength(String(t??""),"utf8")}function yw(t){return t==null?null:String(t).trim()||null}function gw(t){let e=t.split(".").map(Number);return e.length!==4||e.some(n=>!Number.isInteger(n)||n<0||n>255)?!1:e[0]===10||e[0]===127||e[0]===169&&e[1]===254||e[0]===172&&e[1]>=16&&e[1]<=31||e[0]===192&&e[1]===168||e[0]===0}function bl(t){if(!t)return null;try{let e=new URL(t),n=e.hostname.toLowerCase();return!["http:","https:"].includes(e.protocol)||e.username||e.password||n==="localhost"||n.endsWith(".localhost")||n==="::1"||n.endsWith(".local")||gw(n)?null:e.href}catch{return null}}function hw(t,e){let n=new Map(t.map(o=>[o.toLowerCase(),o])),r={...e};for(let[o,s]of Object.entries(pw)){if(r[o])continue;let l=s.map(m=>n.get(m.toLowerCase())).find(Boolean);l&&(r[o]=l)}if(!r.name)throw new Error(`invalid_column_map: name must identify one exact source header; available headers: ${t.join(", ")}`);return cw.parse(r)}function fw(t,e){for(let[n,r]of Object.entries(e))if(!(n==="entityType"&&(r==="person"||r==="business")&&!t.includes(r))&&!t.includes(r))throw new Error(`invalid_column_map: ${n} references missing header ${r}; available headers: ${t.join(", ")}`)}function Nt(t,e){return e?yw(t[e]):null}function ri(t,e,n){return t||[e,n].filter(Boolean).join(", ")||null}function bw(t){let e=lw.parse(t.rows),n=kl.parse(t.options??{}),r=Object.keys(e[0]);if(!r.length||r.length>hl)throw new Error(`lead_list_bounds_exceeded: input must contain 1-${hl} columns`);if(r.some(c=>!c.trim()))throw new Error("Input headers must not be blank");if(new Set(r).size!==r.length)throw new Error("Input headers must be unique");for(let[c,f]of e.entries()){let v=Object.keys(f);if(v.length!==r.length||v.some((d,p)=>d!==r[p]))throw new Error(`All rows must have the same columns in the same order; row ${c+1} differs`);for(let[d,p]of Object.entries(f))if(mw(p)>aw)throw new Error(`lead_list_bounds_exceeded: cell in column ${d} exceeds 32 KiB`)}let o=Buffer.byteLength(JSON.stringify(e),"utf8"),s=t.maxSerializedBytes??fl;if(!Number.isInteger(s)||s<1||s>sw)throw new Error("maxSerializedBytes must be an integer no greater than 5 MiB");if(o>s){let c=s===fl?"inline rows exceed 500 KiB":"normalized rows exceed the configured byte limit";throw new Error(`lead_list_bounds_exceeded: ${c}`)}let l=hw(r,t.columnMap);fw(r,l);let m=e.map((c,f)=>{let v=Nt(c,l.name);if(!v)throw new Error(`Row ${f+1} requires a non-empty name`);let d=Nt(c,l.websiteUrl),p=Nt(c,l.profileUrl),h=d?bl(d):null,w=p?bl(p):null;if(d&&!h||p&&!w)throw new Error(`Row ${f+1} URL must be a public HTTP(S) URL`);let R=Nt(c,l.location),A=Nt(c,l.city),C=Nt(c,l.region);if(!h&&!w&&(!n.resolveMissingUrls||!ri(R,A,C)))throw new Error(`Row ${f+1} requires a public website/profile URL or a location with resolveMissingUrls enabled`);let P=l.entityType==="person"||l.entityType==="business"?l.entityType:Nt(c,l.entityType);if(P&&P!=="person"&&P!=="business")throw new Error(`Row ${f+1} entityType must be business or person`);let M=P==="person"||P==="business"?P:n.defaultEntityType;return{ordinal:f,source:Object.fromEntries(r.map(H=>[H,c[H]])),mapped:{name:v,location:R,city:A,region:C,organization:Nt(c,l.organization),websiteUrl:h,profileUrl:w,phone:Nt(c,l.phone),email:Nt(c,l.email)?.toLowerCase()??null,entityType:M}}});return{headers:r,rows:m,serializedBytes:o,columnMap:l,options:n}}function vw(t){return{query:[t.name,t.organization].filter(Boolean).join(" "),location:ri(t.location,t.city,t.region)}}var ww=new Set(["co","company","corp","corporation","inc","incorporated","llc","llp","lp","ltd","limited","pllc","pc"]);function ni(t){let e=t.toLowerCase().replace(/&/g," and ").replace(/[^a-z0-9]+/g," ").trim().split(/\s+/).filter(Boolean);for(;e.length&&ww.has(e[e.length-1]);)e.pop();return e}function Sw(t,e){let n=ni(t),r=ni(e);if(!n.length||!r.length)return 0;if(n.join(" ")===r.join(" "))return 1;let o=new Set(n),s=r.filter(c=>o.has(c)).length,l=2*s/(n.length+r.length),m=s/Math.min(n.length,r.length);return Math.max(l,m>=1?.9:m*.85)}function vl(t){let e=(t??"").replace(/\D/g,"");return e.length>10?e.slice(-10):e}function wl(t){try{return new URL(t??"").hostname.toLowerCase().replace(/^www\./,"")||null}catch{return null}}function ti(t,e){let n=ni(e??"").filter(o=>o.length>2),r=ni(t??"");return!!(n.length&&n.every(o=>r.includes(o)))}function xw(t,e){let n=Sw(t.name,e.name);if(n<.72)return null;let r=[n===1?"exact_name_match":"near_name_match"],o=[],s=vl(t.phone),l=vl(e.phone);s.length>=7&&l.endsWith(s.slice(-7))&&o.push("phone_match");let m=ri(t.location,t.city,t.region);m&&ti(e.address,m)&&o.push("location_match"),t.city&&ti(e.address,t.city)&&o.push("location_match"),t.organization&&(ti(e.organization,t.organization)||ti(e.name,t.organization))&&o.push("organization_match");let c=wl(t.websiteUrl??t.profileUrl);c&&c===wl(e.websiteUrl)&&o.push("domain_match");let f=[...new Set(o)];if(!f.length)return null;r.push(...f);let v=Math.min(1,Number((n*.7+.2+Math.min(.1,(f.length-1)*.05)).toFixed(3)));return{candidate:e,confidence:v,reasons:r,corroborators:f}}function Sl(t,e){if(t.profileUrl??t.websiteUrl)return{status:"provided_url",confidence:1,reasons:["caller_provided_url"],selected:null,candidates:[]};if(!t.name)return{status:"invalid",confidence:0,reasons:["missing_name"],selected:null,candidates:[]};let r=e.map(s=>xw(t,s)).filter(s=>!!s).sort((s,l)=>l.confidence-s.confidence||s.candidate.name.localeCompare(l.candidate.name));if(!r.length)return{status:"not_found",confidence:0,reasons:["no_corroborated_candidate"],selected:null,candidates:[]};let o=r.filter(s=>s.confidence>=r[0].confidence-.08);return o.length>1?{status:"ambiguous",confidence:r[0].confidence,reasons:["multiple_plausible_candidates"],selected:null,candidates:o.slice(0,3)}:{status:"matched",confidence:r[0].confidence,reasons:r[0].reasons,selected:r[0].candidate,candidates:r.slice(0,3)}}function _w(t){return Array.isArray(t)?t:t.organicResults}function Iw(t){return Array.isArray(t)?{organicResults:t}:t}async function xl(t){if(t.options.peopleDiscovery==="off")return[];if(t.mapped.entityType!=="business")return t.warnings.push("people_discovery_skipped_for_person"),[];if(!t.adapters.searchPeopleSerp)return t.warnings.push("people_search_failed"),[];let e=t.adapters.searchPeopleSerp,n=t.options.peopleDiscovery,r=ri(t.mapped.location,t.mapped.city,t.mapped.region),o=ul({businessName:t.mapped.name,location:r,city:t.mapped.city,region:t.mapped.region,mode:n,queryTemplates:t.options.peopleQueryTemplates}),s=new Map,l=0;for(let f of o)try{let v=await e({query:f,gl:t.options.gl,hl:t.options.hl,includeAiOverview:!0});t.usage.serpSearches+=1,l+=1;let d=Iw(v);for(let p of ml({businessName:t.mapped.name,location:r,city:t.mapped.city,query:f,mode:n,results:d.organicResults,aiOverview:d.aiOverview})){let h=p.name.toLocaleLowerCase("en-US");if(s.has(h)||s.set(h,p),s.size>=t.options.maxPeoplePerLead)break}if(s.size>=t.options.maxPeoplePerLead)break}catch{t.warnings.push("people_search_failed")}let m=[...s.values()].slice(0,t.options.maxPeoplePerLead);if(!l)return[];if(!m.length)return t.warnings.push("people_not_found"),[];let c=[];for(let f of m)try{let v=await e({query:pl({personName:f.name,businessName:t.mapped.name}),gl:t.options.gl,hl:t.options.hl,includeAiOverview:!1});t.usage.serpSearches+=1,c.push(gl({person:f,businessName:t.mapped.name,location:r,results:_w(v)}))}catch{t.warnings.push("people_linkedin_search_failed"),c.push(f)}return c}function ar(){return{website:null,phone:null,primaryEmail:null,emails:[],socialProfiles:[],contactFormUrl:null,sourceUrls:[]}}function Eo(t,e){let n=new Set(t.emails.map(l=>l.value));for(let l of e.emails){if(t.emails.length>=20)break;n.has(l.value)||(t.emails.push(l),n.add(l.value))}let r=new Set(t.socialProfiles.map(l=>l.url));for(let l of e.socialProfiles){if(t.socialProfiles.length>=20)break;r.has(l.url)||(t.socialProfiles.push(l),r.add(l.url))}let o=new Set(t.contactForms.map(l=>l.url));for(let l of e.contactForms){if(t.contactForms.length>=10)break;o.has(l.url)||(t.contactForms.push(l),o.add(l.url))}let s=new Set(t.candidatePageUrls.map(l=>l.url));for(let l of e.candidatePageUrls){if(t.candidatePageUrls.length>=20)break;s.has(l.url)||(t.candidatePageUrls.push(l),s.add(l.url))}}function kw(t,e){let n={emails:[],socialProfiles:[],contactForms:[],candidatePageUrls:[]},r=t.kpo?.email?.trim().toLowerCase();r&&n.emails.push({value:r,sourceUrl:e,context:`Structured entity data for ${t.kpo?.entityName??"the page entity"}`,sourceType:"structured_data"});for(let o of[...t.kpo?.sameAs??[],...t.kpo?.socialLinks??[]])try{let s=new URL(o);if(!["http:","https:"].includes(s.protocol))continue;let l=s.hostname.replace(/^www\./,"").split(".")[0];n.socialProfiles.some(m=>m.url===s.href)||n.socialProfiles.push({url:s.href,network:l,sourceUrl:e,context:"Structured entity social profile"})}catch{}return n}function Rw(t){return{name:t.name,websiteUrl:t.websiteUrl??null,address:t.address??null,phone:t.phone??null,placeUrl:t.placeUrl??null,cid:t.cid==null?null:String(t.cid),organization:t.organization??null,category:t.category??null}}function _l(t){return typeof t=="object"&&t!==null&&"source"in t&&typeof t.source=="object"&&t.source!==null&&"mapped"in t&&typeof t.mapped=="object"&&t.mapped!==null}async function qT(t){let e,n;try{if(_l(t.row))e=t.row,n=kl.parse(t.options??{});else{let B=bw({rows:[t.row],columnMap:t.columnMap,options:t.options});e={...B.rows[0],ordinal:t.ordinal},n=B.options}}catch{return{ordinal:t.ordinal,source:_l(t.row)?t.row.source:t.row,rowStatus:"failed",identity:{status:"invalid",confidence:0,reasons:["invalid_input"],selected:null,candidates:[]},contacts:ar(),people:[],usage:{mapsAttempts:0,pageAttempts:0,pageSuccesses:0,serpSearches:0},warnings:[],error:{code:"invalid_row",message:"The row could not be normalized with the supplied column mapping."}}}let r={mapsAttempts:0,pageAttempts:0,pageSuccesses:0,serpSearches:0},o;if(e.mapped.profileUrl||e.mapped.websiteUrl)o=Sl(e.mapped,[]);else{let B=vw(e.mapped);try{r.mapsAttempts=1;let de=await t.adapters.mapsSearch({query:B.query,location:B.location,maxResults:5,gl:n.gl,hl:n.hl}),Ie=(Array.isArray(de)?de:de.results).map(Rw);o=Sl(e.mapped,Ie)}catch(de){return de instanceof Qr&&(r.mapsAttempts=0),{ordinal:e.ordinal,source:e.source,rowStatus:"failed",identity:{status:"not_found",confidence:0,reasons:["maps_provider_failure"],selected:null,candidates:[]},contacts:ar(),people:[],usage:r,warnings:[],error:{code:"resolution_failed",message:"The business lookup could not be completed for this row."}}}}if(o.status==="ambiguous"||o.status==="not_found"||o.status==="invalid")return{ordinal:e.ordinal,source:e.source,rowStatus:o.status==="invalid"?"failed":"complete",identity:o,contacts:ar(),people:[],usage:r,warnings:[],error:o.status==="invalid"?{code:"invalid_row",message:"The row could not be resolved."}:null};let s=e.mapped.profileUrl??e.mapped.websiteUrl??o.selected?.websiteUrl??null,l=s?{value:s,sourceUrl:o.status==="provided_url"?"provided":o.selected?.placeUrl??"maps_search"}:null,m=e.mapped.phone?{value:e.mapped.phone,sourceUrl:"provided"}:o.selected?.phone?{value:o.selected.phone,sourceUrl:o.selected.placeUrl??"maps_search"}:null,c=[];if(!s){let B=await xl({mapped:e.mapped,options:n,adapters:t.adapters,usage:r,warnings:c}),de=[...c.includes("people_search_failed")?["The optional associated-people search could not be completed."]:[],...c.includes("people_linkedin_search_failed")?["One or more optional LinkedIn candidate searches could not be completed."]:[],...c.includes("people_not_found")?["No role-bearing associated people were found in the bounded search."]:[],...c.includes("people_discovery_skipped_for_person")?["Associated-people discovery applies to business rows and was skipped for this person row."]:[]];return{ordinal:e.ordinal,source:e.source,rowStatus:"complete",identity:o,contacts:{...ar(),website:l,phone:m,sourceUrls:[l?.sourceUrl,m?.sourceUrl].filter(Ie=>!!Ie)},people:B,usage:r,warnings:["The matched listing did not expose a public website.",...de],error:null}}let f={emails:[],socialProfiles:[],contactForms:[],candidatePageUrls:[]};e.mapped.email&&f.emails.push({value:e.mapped.email,sourceUrl:"provided",context:`Provided for ${e.mapped.name}`,sourceType:"provided"});let v=new Set,d=[s],p=null;for(;d.length&&v.size<n.maxPagesPerLead;){let B=d.find(de=>!v.has(de));if(!B)break;v.add(B),r.pageAttempts+=1;try{let de=await t.adapters.extractPage({url:B});r.pageSuccesses+=1;let Ie=de.url??B,ze=de.contactEvidence??Ds(de.bodyHtml??de.bodyMarkdown??"",Ie);Eo(ze,kw(de,Ie)),Eo(f,ze),!p&&de.kpo?.phone&&(p={value:de.kpo.phone,sourceUrl:Ie}),d=So(s,f.candidatePageUrls,n.maxPagesPerLead)}catch{c.push("page_failed"),d=So(s,f.candidatePageUrls,n.maxPagesPerLead)}}if(!f.emails.length&&n.emailSearchFallback==="serp_snippets"){let B=zs(s);if(B)try{let de=await t.adapters.searchEmailSerp({query:B,gl:n.gl,hl:n.hl});r.serpSearches+=1;let Ie=Array.isArray(de)?de:de.organicResults;Eo(f,{emails:Fs({websiteUrl:s,results:Ie}),socialProfiles:[],contactForms:[],candidatePageUrls:[]})}catch{c.push("serp_failed")}}let h=await xl({mapped:e.mapped,options:n,adapters:t.adapters,usage:r,warnings:c}),w=[...c.includes("page_failed")?["One or more selected website pages could not be read."]:[],...c.includes("serp_failed")?["The optional email search could not be completed."]:[],...c.includes("people_search_failed")?["The optional associated-people search could not be completed."]:[],...c.includes("people_linkedin_search_failed")?["One or more optional LinkedIn candidate searches could not be completed."]:[],...c.includes("people_not_found")?["No role-bearing associated people were found in the bounded search."]:[],...c.includes("people_discovery_skipped_for_person")?["Associated-people discovery applies to business rows and was skipped for this person row."]:[]];if(!r.pageSuccesses&&!f.emails.length&&!h.length)return{ordinal:e.ordinal,source:e.source,rowStatus:"failed",identity:o,contacts:{...ar(),website:l,phone:m},people:h,usage:r,warnings:w,error:{code:"page_extraction_failed",message:"No selected website page could be read for this row."}};let R={entityType:e.mapped.entityType,name:e.mapped.name,organization:e.mapped.organization},A=["provided","first_party_page","structured_data","serp_snippet"],C=["person","team","office","unknown"],P=f.emails.map(B=>({...B,...Hs(B,R)})).sort((B,de)=>A.indexOf(B.sourceType)-A.indexOf(de.sourceType)||C.indexOf(B.scope)-C.indexOf(de.scope)),M=Ns(f.emails,R),H=f.contactForms[0]?{value:f.contactForms[0].url,sourceUrl:f.contactForms[0].sourceUrl}:null,K=m??p,Z=[...new Set([l?.sourceUrl,K?.sourceUrl,...P.map(B=>B.sourceUrl),...f.socialProfiles.map(B=>B.sourceUrl),H?.sourceUrl].filter(B=>!!B))];return{ordinal:e.ordinal,source:e.source,rowStatus:"complete",identity:o,contacts:{website:l,phone:K,primaryEmail:M,emails:P,socialProfiles:f.socialProfiles.slice(0,20),contactFormUrl:H,sourceUrls:Z},people:h,usage:r,warnings:w,error:null}}var Tw=["enriched_row_status","enriched_identity_status","enriched_match_confidence","enriched_matched_name","enriched_organization","enriched_website","enriched_phone","enriched_primary_email","enriched_primary_email_scope","enriched_secondary_email","enriched_all_emails","enriched_social_profiles","enriched_contact_form_url","enriched_primary_person_name","enriched_primary_person_role","enriched_primary_person_linkedin","enriched_all_people","enriched_maps_url","enriched_cid","enriched_source_urls","enriched_warnings"];function sr(t){return JSON.stringify(t)}function DT(t,e){let n=e??Object.keys(t[0]?.source??{});for(let o of n)if(o.toLowerCase().startsWith(Il))throw new Error(`Source header ${o} collides with reserved enriched_ output columns`);let r=[...t].sort((o,s)=>o.ordinal-s.ordinal);return{headers:[...n,...Tw],rows:r.map(o=>{let s=o.contacts.primaryEmail?o.contacts.emails.find(l=>l.value!==o.contacts.primaryEmail?.value)??null:null;return[...n.map(l=>o.source[l]??null),o.rowStatus,o.identity.status,o.identity.confidence,o.identity.selected?.name??"",o.identity.selected?.organization??"",o.contacts.website?.value??"",o.contacts.phone?.value??"",o.contacts.primaryEmail?.value??"",o.contacts.primaryEmail?.scope??"",s?.value??"",sr(o.contacts.emails.map(l=>({value:l.value,scope:l.scope,sourceType:l.sourceType,sourceUrl:l.sourceUrl,...l.sourceTitle?{sourceTitle:l.sourceTitle}:{},...l.sourcePosition!=null?{sourcePosition:l.sourcePosition}:{},context:l.context,reasons:l.reasons}))),sr(o.contacts.socialProfiles.map(l=>({url:l.url,network:l.network,sourceUrl:l.sourceUrl}))),o.contacts.contactFormUrl?.value??"",o.people[0]?.name??"",o.people[0]?.role??"",o.people[0]?.linkedinUrl??"",sr(o.people),o.identity.selected?.placeUrl??"",o.identity.selected?.cid??"",sr(o.contacts.sourceUrls),sr([...o.warnings,...o.error?[o.error.message]:[]])]})}}import{z as Cw}from"zod";var lr=["LocalBusiness","AnimalShelter","AutomotiveBusiness","AutoBodyShop","AutoDealer","AutoPartsStore","AutoRental","AutoRepair","AutoWash","GasStation","MotorcycleDealer","MotorcycleRepair","ChildCare","Dentist","DryCleaningOrLaundry","EmergencyService","EmploymentAgency","EntertainmentBusiness","FinancialService","AccountingService","AutomatedTeller","BankOrCreditUnion","InsuranceAgency","FoodEstablishment","Bakery","BarOrPub","Brewery","CafeOrCoffeeShop","Distillery","FastFoodRestaurant","IceCreamShop","Restaurant","Winery","HealthAndBeautyBusiness","BeautySalon","DaySpa","HairSalon","HealthClub","NailSalon","TattooParlor","HomeAndConstructionBusiness","Electrician","GeneralContractor","HVACBusiness","HousePainter","Locksmith","MovingCompany","Plumber","RoofingContractor","LegalService","Library","LodgingBusiness","MedicalBusiness","Pharmacy","Physician","RealEstateAgent","RecyclingCenter","SelfStorage","ShoppingCenter","SportsActivityLocation","Store","TouristInformationCenter","TravelAgency"],Aw=Cw.enum(lr),Pw={home:"HomeAndConstructionBusiness",professional:"LocalBusiness",restaurants:"FoodEstablishment",financial:"FinancialService",realestate:"RealEstateAgent",auto:"AutomotiveBusiness",wellness:"HealthAndBeautyBusiness"};function Rl(t){return Pw[t]}function zT(t,e){let n=Aw.safeParse(e);return n.success?n.data:Rl(t)}function FT(t){return{rootType:"LocalBusiness",selectedDefault:t?Rl(t):"LocalBusiness",acceptedSubtypes:lr,callerSuppliedJsonLdAccepted:!1,compilation:"MCP Scraper generates JSON-LD from source-backed listing evidence after acquisition."}}var Ow=new Intl.Segmenter(void 0,{granularity:"word"});function ft(t){let e=0;for(let n of Ow.segment(t))n.isWordLike&&(e+=1);return e}function Rt(t,e,n){let r=ft(t),o=e.reduce((v,d)=>{let p=Number(d.timestamp?.[1]);return Number.isFinite(p)&&p>v?p:v},0),s=typeof n=="number"&&Number.isFinite(n)&&n>0?n:o>0?o:null,l=s&&s>0?Math.round(r/s*600)/10:null,m=r===0?"empty":r<5?"low_speech_signal":"speech_detected",c=r<=2,f=[];return m==="empty"?f.push("No speech was transcribed. This does not distinguish silent media from an inaccessible or expired playback URL."):m==="low_speech_signal"&&f.push("Very little speech was transcribed. Review the media before treating this as a complete transcript."),c&&f.push("If speech was expected, refresh the media source and retry using the tool for its original platform."),{status:m,speechDetected:m==="speech_detected",confidence:m==="speech_detected"?"medium":"low",basis:"transcript_word_count_and_timing",mediaDurationSec:s,wordsPerMinute:l,retryRecommended:c,warnings:f}}import{fal as Ew}from"@fal-ai/client";var Mo=9e4,Pn=Mo+15e3,ii=["af","am","ar","as","az","ba","be","bg","bn","bo","br","bs","ca","cs","cy","da","de","el","en","es","et","eu","fa","fi","fo","fr","gl","gu","ha","haw","he","hi","hr","ht","hu","hy","id","is","it","ja","jw","ka","kk","km","kn","ko","la","lb","ln","lo","lt","lv","mg","mi","mk","ml","mn","mr","ms","mt","my","ne","nl","nn","no","oc","pa","pl","ps","pt","ro","ru","sa","sd","si","sk","sl","sn","so","sq","sr","su","sv","sw","ta","te","tg","th","tk","tl","tr","tt","uk","ur","uz","vi","yi","yo","zh"];function jw(t,e,n,r){let o=l=>`${Math.floor(l/60)}:${String(Math.floor(l%60)).padStart(2,"0")}`,s=[t,"",`*Transcribed in ${(r/1e3).toFixed(1)}s*`,"","## Full Text","",e,""];if(n.length){s.push("## Timestamped Segments","");for(let l of n)s.push(`**[${o(l.timestamp[0])} -> ${o(l.timestamp[1])}]** ${l.text.trim()}`,"")}return s.join(`
1
+ import{a as An,e as Je,s as il}from"./chunk-JHT6A4U6.js";import{b as ol}from"./chunk-7POHNTVW.js";import{j as Gs,k as Ws,l as Bs,m as Ks}from"./chunk-MVMWPKRC.js";import{c as Vs,e as _o,f as Js,h as Io,j as ko,k as Ro,l as To,m as Ys,n as Zr,p as Co,q as Xs,r as Ao,s as Po,u as Zs}from"./chunk-2IATUJO6.js";import{$ as As,aa as Ps,c as ho,d as Kr,e as fo,f as bo,g as Cs}from"./chunk-CORWWDHL.js";import{e as Br,h as Dt,j as er}from"./chunk-5LYINW7Z.js";import{a as Ls,c as rr,d as Us,e as ir,f as $s,g as qs}from"./chunk-CNSVZTTT.js";import{a as Ms,b as wo}from"./chunk-5BKSKLHX.js";import{f as js}from"./chunk-7SR7MEU5.js";import{e as nr,g as Xr,i as Es}from"./chunk-SEZETSB3.js";import{b as Os,c as Yr,i as tr}from"./chunk-SDQW7IJN.js";import{a as xs,c as go,e as Cn,f as Gr,i as Ts}from"./chunk-B4ZKPLSP.js";import{d as ks,g as Rs}from"./chunk-VVSHZV5S.js";import{c as _s,f as Wr,g as qt,h as Xe,i as Qn}from"./chunk-C7MGJUTU.js";import{c as Is}from"./chunk-4ZVO6GX4.js";import{a as Ds,b as Hs,c as Ns,d as So,e as zs,f as Fs,h as Qs,i as el,k as tl,m as nl,n as rl}from"./chunk-24YDQ3EO.js";import{b as Vr,c as Ve,d as vo,e as Jr}from"./chunk-OILOHQNL.js";import{a as xo}from"./chunk-7H6YWD2L.js";import{z as Ae}from"zod";var Qr=class extends Error{constructor(e){super("lead_provider_admission_denied",{cause:e}),this.name="LeadProviderAdmissionError"}};var sl=["president\\s*[/&-]\\s*owner","owner\\s*[/&-]\\s*president","chief executive officer","operations manager","general manager","managing member","managing partner","business owner","co[- ]founder","team member","president","principal","director","manager","partner","founder","owner","ceo"].join("|"),Oo=String.raw`(?:Mr\.?|Ms\.?|Mrs\.?|Dr\.?)?\s*([\p{Lu}][\p{L}'’.-]+(?:\s+[\p{Lu}][\p{L}'’.-]+){1,3})`,Wv=`${Oo}\\s*(?:,|[-\u2013\u2014]|\\bis\\b|\\bas\\b|\\bserves as\\b)?\\s*(${sl})\\b`,Bv=`\\b(${sl})\\b\\s*(?:[,:\u2013\u2014-]|\\bis\\b|\\bof\\b)?\\s*${Oo}`,Kv=`\\b((?:co[- ]?)?owned|founded)\\s+by\\s+${Oo}`,Vv=new Set(["about","additional","business","company","contact","customer","details","information","management","member","owner","president","profile","roofing","team","website"]),Jv=new Set(["co","company","corp","corporation","inc","incorporated","llc","ltd","limited","pllc"]);function or(t,e=400){return t.replace(/\s+/g," ").trim().slice(0,e)}function Ht(t){return t.toLowerCase().replace(/[^a-z0-9]+/g," ").trim().split(/\s+/).filter(Boolean)}function ll(t){let e=Ht(t);for(;e.length&&Jv.has(e[e.length-1]);)e.pop();return e}function Yv(t){return t.replace(/^(?:Mr\.?|Ms\.?|Mrs\.?|Dr\.?)\s+/i,"").replace(/[.!?]\s+.*$/,"").replace(/[.,;:!?]+$/,"").replace(/\s+/g," ").trim()}function Xv(t,e){let n=Ht(t);if(n.length<2||n.length>4||n.some(o=>Vv.has(o)))return!1;let r=new Set(ll(e));return n.some(o=>!r.has(o))}function Zv(t){let e=t.toLowerCase().replace(/\s+/g," ");return/founder/.test(e)?"founder":/owner|managing member|partner/.test(e)?"owner":/manager/.test(e)?"manager":/president|chief executive officer|\bceo\b|principal|director/.test(e)?"executive":"team_member"}function Qv(t){return/founded/i.test(t)?"Founder":/owned/i.test(t)?"Owner":or(t,80).replace(/\bceo\b/i,"CEO").replace(/\b\w/g,e=>e.toUpperCase())}function cl(t){return or(`${t.title} | ${t.snippet??""}`)}function dl(t){return t.map((e,n)=>({result:e,index:n})).sort((e,n)=>{let r=Number.isFinite(e.result.position)?e.result.position:Number.MAX_SAFE_INTEGER,o=Number.isFinite(n.result.position)?n.result.position:Number.MAX_SAFE_INTEGER;return r-o||e.index-n.index}).map(e=>e.result)}function ew(t){if(!t)return null;try{let e=new URL(t);return e.protocol==="http:"||e.protocol==="https:"?e.href:null}catch{return null}}function al(t){return`"${t.replace(/["\\]/g," ").replace(/\s+/g," ").trim()}"`}function ei(t){return or(t??"",120).replace(/["\\]/g," ")}function tw(t){return t==="owners"?'"{business}" owner of company {location}':'"{business}" leadership team {location}'}function ul(t){let e={business:ei(t.businessName),city:ei(t.city),region:ei(t.region),location:ei(t.location)},n=(t.queryTemplates?.length?t.queryTemplates:[tw(t.mode)]).map(r=>or(r.replace(/\{(business|city|region|location)\}/g,(o,s)=>e[s]),240)).filter(Boolean);return[...new Set(n)].slice(0,3)}function pl(t){return`site:linkedin.com/in ${al(t.personName)} ${al(t.businessName)}`}function ml(t){let e=new Map,n=[],r=t.aiOverview?.detected?or(t.aiOverview.text??""):"";if(r){let o=ew(t.aiOverview?.shareUrl)??(t.query?`https://www.google.com/search?q=${encodeURIComponent(t.query)}`:"https://www.google.com/"),s=(t.aiOverview?.citations??[]).flatMap(l=>{try{let m=new URL(l.href);return["http:","https:"].includes(m.protocol)?[m.href]:[]}catch{return[]}}).slice(0,5);n.push({sourceType:"ai_overview",sourceUrl:o,sourceTitle:"Google AI Overview",sourcePosition:0,context:r,...s.length?{citationUrls:s}:{}})}for(let o of dl(t.results))n.push({sourceType:"serp_snippet",sourceUrl:o.url,sourceTitle:o.title,sourcePosition:o.position,context:cl(o)});for(let o of n){let s=o.context;if(!yl(s,t.businessName))continue;let l=t.city||(t.location??"").split(",")[0]||"",m=Ht(l).filter(v=>v.length>2),c=m.some(v=>Ht(s).includes(v));if(o.sourceType==="ai_overview"&&m.length&&!c)continue;let f=[];for(let v of s.matchAll(new RegExp(Wv,"giu")))f.push({name:v[1]??"",role:v[2]??""});for(let v of s.matchAll(new RegExp(Bv,"giu")))f.push({name:v[2]??"",role:v[1]??""});for(let v of s.matchAll(new RegExp(Kv,"giu")))f.push({name:v[2]??"",role:v[1]??""});for(let v of f){let d=Yv(v.name),p=Qv(v.role);if(!Xv(d,t.businessName))continue;let h=Zv(p);if(t.mode==="owners"&&h!=="owner"&&h!=="founder")continue;let w=Ht(d).join(" ");if(!w)continue;let R={sourceType:o.sourceType,purpose:"role",sourceUrl:o.sourceUrl,sourceTitle:o.sourceTitle,sourcePosition:o.sourcePosition,context:s,...o.citationUrls?{citationUrls:o.citationUrls}:{}},A=e.get(w);if(A){A.sources.length<2&&!A.sources.some(P=>P.sourceUrl===R.sourceUrl)&&A.sources.push(R),A.reasons=[...new Set([...A.reasons,"multiple_search_sources"])],A.confidence=Math.max(A.confidence,o.sourceType==="ai_overview"?c?.93:.9:.88);continue}let C=o.sourceType==="ai_overview";if(e.set(w,{name:d,role:p,relationship:h,linkedinUrl:null,confidence:C?c?.93:.9:.88,reasons:[C?"name_role_in_ai_overview":"name_role_in_search_result","exact_business_query_context",...C&&c?["ai_overview_location_match"]:[]],sources:[R]}),e.size>=5)return[...e.values()]}}return[...e.values()]}function nw(t){try{let e=new URL(t),n=e.hostname.toLowerCase().replace(/^www\./,"");return e.protocol!=="https:"||n!=="linkedin.com"||!/^\/in\/[^/]+\/?$/i.test(e.pathname)?null:(e.search="",e.hash="",e.href)}catch{return null}}function rw(t,e){let n=new Set(Ht(t)),r=Ht(e);return r.length>=2&&r.every(o=>n.has(o))}function yl(t,e){let n=new Set(Ht(t)),r=ll(e);if(r.length<2)return!1;let o=r.filter(s=>n.has(s)).length;return o>=2&&o/r.length>=.6}function gl(t){for(let e of dl(t.results)){let n=nw(e.url);if(!n)continue;let r=cl(e);if(!rw(r,t.person.name)||!yl(r,t.businessName))continue;let s=Ht((t.location??"").split(",")[0]??"").filter(l=>l.length>2).some(l=>Ht(r).includes(l));return{...t.person,linkedinUrl:n,confidence:s?.97:.94,reasons:[...new Set([...t.person.reasons,"linkedin_name_match","linkedin_company_match",...s?["linkedin_location_match"]:[]])],sources:[...t.person.sources,{sourceType:"serp_snippet",purpose:"linkedin",sourceUrl:e.url,sourceTitle:e.title,sourcePosition:e.position,context:r}]}}return t.person}var iw=Ae.union([Ae.string(),Ae.number().finite(),Ae.boolean(),Ae.null()]),Il="enriched_",ow=250,hl=100,aw=32*1024,fl=500*1024,sw=5*1024*1024,lw=Ae.array(Ae.record(Ae.string(),iw)).min(1).max(ow).superRefine((t,e)=>{t.forEach((n,r)=>{Object.keys(n).forEach(o=>{o.toLowerCase().startsWith(Il)&&e.addIssue({code:"custom",message:`Input header ${o} uses the reserved enriched_ prefix`,path:[r,o]})})})}),cw=Ae.object({name:Ae.string().min(1),location:Ae.string().min(1).optional(),city:Ae.string().min(1).optional(),region:Ae.string().min(1).optional(),organization:Ae.string().min(1).optional(),websiteUrl:Ae.string().min(1).optional(),profileUrl:Ae.string().min(1).optional(),phone:Ae.string().min(1).optional(),email:Ae.string().min(1).optional(),entityType:Ae.string().min(1).optional()}).strict(),dw=/\{(?:business|city|region|location)\}/g,uw=Ae.string().trim().min(1).max(240).refine(t=>!/[\r\n]/.test(t),"People query templates must be one line").refine(t=>t.includes("{business}"),"People query templates must include {business}").refine(t=>!/[{}]/.test(t.replace(dw,"")),"People query templates may use only {business}, {city}, {region}, and {location}"),jo=Ae.array(uw).min(1).max(3),kl=Ae.object({resolveMissingUrls:Ae.boolean().default(!0),defaultEntityType:Ae.enum(["business","person"]).default("business"),gl:Ae.string().regex(/^[a-z]{2}$/i).transform(t=>t.toLowerCase()).default("us"),hl:Ae.string().regex(/^[a-z]{2}(?:-[a-z]{2})?$/i).transform(t=>t.toLowerCase()).default("en"),maxPagesPerLead:Ae.number().int().min(1).max(4).default(3),concurrency:Ae.number().int().min(1).max(5).default(3),emailSearchFallback:Ae.enum(["off","serp_snippets"]).default("off"),peopleDiscovery:Ae.enum(["off","owners","leadership"]).default("off"),peopleQueryTemplates:jo.optional(),maxPeoplePerLead:Ae.number().int().min(1).max(3).default(2),outputFormats:Ae.array(Ae.enum(["csv","xlsx"])).min(1).max(2).transform(t=>[...new Set(t)]).default(["csv","xlsx"])}).strict(),pw={name:["name","business_name","business name","company","company_name","full_name","full name"],location:["location","market","address"],city:["city","town"],region:["region","state","province"],organization:["organization","organisation","brokerage","agency","company"],websiteUrl:["websiteUrl","website_url","website url","website","url"],profileUrl:["profileUrl","profile_url","profile url","profile"],phone:["phone","phone_number","phone number","telephone"],email:["email","email_address","email address"],entityType:["entityType","entity_type","entity type","type"]};function mw(t){return Buffer.byteLength(String(t??""),"utf8")}function yw(t){return t==null?null:String(t).trim()||null}function gw(t){let e=t.split(".").map(Number);return e.length!==4||e.some(n=>!Number.isInteger(n)||n<0||n>255)?!1:e[0]===10||e[0]===127||e[0]===169&&e[1]===254||e[0]===172&&e[1]>=16&&e[1]<=31||e[0]===192&&e[1]===168||e[0]===0}function bl(t){if(!t)return null;try{let e=new URL(t),n=e.hostname.toLowerCase();return!["http:","https:"].includes(e.protocol)||e.username||e.password||n==="localhost"||n.endsWith(".localhost")||n==="::1"||n.endsWith(".local")||gw(n)?null:e.href}catch{return null}}function hw(t,e){let n=new Map(t.map(o=>[o.toLowerCase(),o])),r={...e};for(let[o,s]of Object.entries(pw)){if(r[o])continue;let l=s.map(m=>n.get(m.toLowerCase())).find(Boolean);l&&(r[o]=l)}if(!r.name)throw new Error(`invalid_column_map: name must identify one exact source header; available headers: ${t.join(", ")}`);return cw.parse(r)}function fw(t,e){for(let[n,r]of Object.entries(e))if(!(n==="entityType"&&(r==="person"||r==="business")&&!t.includes(r))&&!t.includes(r))throw new Error(`invalid_column_map: ${n} references missing header ${r}; available headers: ${t.join(", ")}`)}function Nt(t,e){return e?yw(t[e]):null}function ri(t,e,n){return t||[e,n].filter(Boolean).join(", ")||null}function bw(t){let e=lw.parse(t.rows),n=kl.parse(t.options??{}),r=Object.keys(e[0]);if(!r.length||r.length>hl)throw new Error(`lead_list_bounds_exceeded: input must contain 1-${hl} columns`);if(r.some(c=>!c.trim()))throw new Error("Input headers must not be blank");if(new Set(r).size!==r.length)throw new Error("Input headers must be unique");for(let[c,f]of e.entries()){let v=Object.keys(f);if(v.length!==r.length||v.some((d,p)=>d!==r[p]))throw new Error(`All rows must have the same columns in the same order; row ${c+1} differs`);for(let[d,p]of Object.entries(f))if(mw(p)>aw)throw new Error(`lead_list_bounds_exceeded: cell in column ${d} exceeds 32 KiB`)}let o=Buffer.byteLength(JSON.stringify(e),"utf8"),s=t.maxSerializedBytes??fl;if(!Number.isInteger(s)||s<1||s>sw)throw new Error("maxSerializedBytes must be an integer no greater than 5 MiB");if(o>s){let c=s===fl?"inline rows exceed 500 KiB":"normalized rows exceed the configured byte limit";throw new Error(`lead_list_bounds_exceeded: ${c}`)}let l=hw(r,t.columnMap);fw(r,l);let m=e.map((c,f)=>{let v=Nt(c,l.name);if(!v)throw new Error(`Row ${f+1} requires a non-empty name`);let d=Nt(c,l.websiteUrl),p=Nt(c,l.profileUrl),h=d?bl(d):null,w=p?bl(p):null;if(d&&!h||p&&!w)throw new Error(`Row ${f+1} URL must be a public HTTP(S) URL`);let R=Nt(c,l.location),A=Nt(c,l.city),C=Nt(c,l.region);if(!h&&!w&&(!n.resolveMissingUrls||!ri(R,A,C)))throw new Error(`Row ${f+1} requires a public website/profile URL or a location with resolveMissingUrls enabled`);let P=l.entityType==="person"||l.entityType==="business"?l.entityType:Nt(c,l.entityType);if(P&&P!=="person"&&P!=="business")throw new Error(`Row ${f+1} entityType must be business or person`);let M=P==="person"||P==="business"?P:n.defaultEntityType;return{ordinal:f,source:Object.fromEntries(r.map(H=>[H,c[H]])),mapped:{name:v,location:R,city:A,region:C,organization:Nt(c,l.organization),websiteUrl:h,profileUrl:w,phone:Nt(c,l.phone),email:Nt(c,l.email)?.toLowerCase()??null,entityType:M}}});return{headers:r,rows:m,serializedBytes:o,columnMap:l,options:n}}function vw(t){return{query:[t.name,t.organization].filter(Boolean).join(" "),location:ri(t.location,t.city,t.region)}}var ww=new Set(["co","company","corp","corporation","inc","incorporated","llc","llp","lp","ltd","limited","pllc","pc"]);function ni(t){let e=t.toLowerCase().replace(/&/g," and ").replace(/[^a-z0-9]+/g," ").trim().split(/\s+/).filter(Boolean);for(;e.length&&ww.has(e[e.length-1]);)e.pop();return e}function Sw(t,e){let n=ni(t),r=ni(e);if(!n.length||!r.length)return 0;if(n.join(" ")===r.join(" "))return 1;let o=new Set(n),s=r.filter(c=>o.has(c)).length,l=2*s/(n.length+r.length),m=s/Math.min(n.length,r.length);return Math.max(l,m>=1?.9:m*.85)}function vl(t){let e=(t??"").replace(/\D/g,"");return e.length>10?e.slice(-10):e}function wl(t){try{return new URL(t??"").hostname.toLowerCase().replace(/^www\./,"")||null}catch{return null}}function ti(t,e){let n=ni(e??"").filter(o=>o.length>2),r=ni(t??"");return!!(n.length&&n.every(o=>r.includes(o)))}function xw(t,e){let n=Sw(t.name,e.name);if(n<.72)return null;let r=[n===1?"exact_name_match":"near_name_match"],o=[],s=vl(t.phone),l=vl(e.phone);s.length>=7&&l.endsWith(s.slice(-7))&&o.push("phone_match");let m=ri(t.location,t.city,t.region);m&&ti(e.address,m)&&o.push("location_match"),t.city&&ti(e.address,t.city)&&o.push("location_match"),t.organization&&(ti(e.organization,t.organization)||ti(e.name,t.organization))&&o.push("organization_match");let c=wl(t.websiteUrl??t.profileUrl);c&&c===wl(e.websiteUrl)&&o.push("domain_match");let f=[...new Set(o)];if(!f.length)return null;r.push(...f);let v=Math.min(1,Number((n*.7+.2+Math.min(.1,(f.length-1)*.05)).toFixed(3)));return{candidate:e,confidence:v,reasons:r,corroborators:f}}function Sl(t,e){if(t.profileUrl??t.websiteUrl)return{status:"provided_url",confidence:1,reasons:["caller_provided_url"],selected:null,candidates:[]};if(!t.name)return{status:"invalid",confidence:0,reasons:["missing_name"],selected:null,candidates:[]};let r=e.map(s=>xw(t,s)).filter(s=>!!s).sort((s,l)=>l.confidence-s.confidence||s.candidate.name.localeCompare(l.candidate.name));if(!r.length)return{status:"not_found",confidence:0,reasons:["no_corroborated_candidate"],selected:null,candidates:[]};let o=r.filter(s=>s.confidence>=r[0].confidence-.08);return o.length>1?{status:"ambiguous",confidence:r[0].confidence,reasons:["multiple_plausible_candidates"],selected:null,candidates:o.slice(0,3)}:{status:"matched",confidence:r[0].confidence,reasons:r[0].reasons,selected:r[0].candidate,candidates:r.slice(0,3)}}function _w(t){return Array.isArray(t)?t:t.organicResults}function Iw(t){return Array.isArray(t)?{organicResults:t}:t}async function xl(t){if(t.options.peopleDiscovery==="off")return[];if(t.mapped.entityType!=="business")return t.warnings.push("people_discovery_skipped_for_person"),[];if(!t.adapters.searchPeopleSerp)return t.warnings.push("people_search_failed"),[];let e=t.adapters.searchPeopleSerp,n=t.options.peopleDiscovery,r=ri(t.mapped.location,t.mapped.city,t.mapped.region),o=ul({businessName:t.mapped.name,location:r,city:t.mapped.city,region:t.mapped.region,mode:n,queryTemplates:t.options.peopleQueryTemplates}),s=new Map,l=0;for(let f of o)try{let v=await e({query:f,gl:t.options.gl,hl:t.options.hl,includeAiOverview:!0});t.usage.serpSearches+=1,l+=1;let d=Iw(v);for(let p of ml({businessName:t.mapped.name,location:r,city:t.mapped.city,query:f,mode:n,results:d.organicResults,aiOverview:d.aiOverview})){let h=p.name.toLocaleLowerCase("en-US");if(s.has(h)||s.set(h,p),s.size>=t.options.maxPeoplePerLead)break}if(s.size>=t.options.maxPeoplePerLead)break}catch{t.warnings.push("people_search_failed")}let m=[...s.values()].slice(0,t.options.maxPeoplePerLead);if(!l)return[];if(!m.length)return t.warnings.push("people_not_found"),[];let c=[];for(let f of m)try{let v=await e({query:pl({personName:f.name,businessName:t.mapped.name}),gl:t.options.gl,hl:t.options.hl,includeAiOverview:!1});t.usage.serpSearches+=1,c.push(gl({person:f,businessName:t.mapped.name,location:r,results:_w(v)}))}catch{t.warnings.push("people_linkedin_search_failed"),c.push(f)}return c}function ar(){return{website:null,phone:null,primaryEmail:null,emails:[],socialProfiles:[],contactFormUrl:null,sourceUrls:[]}}function Eo(t,e){let n=new Set(t.emails.map(l=>l.value));for(let l of e.emails){if(t.emails.length>=20)break;n.has(l.value)||(t.emails.push(l),n.add(l.value))}let r=new Set(t.socialProfiles.map(l=>l.url));for(let l of e.socialProfiles){if(t.socialProfiles.length>=20)break;r.has(l.url)||(t.socialProfiles.push(l),r.add(l.url))}let o=new Set(t.contactForms.map(l=>l.url));for(let l of e.contactForms){if(t.contactForms.length>=10)break;o.has(l.url)||(t.contactForms.push(l),o.add(l.url))}let s=new Set(t.candidatePageUrls.map(l=>l.url));for(let l of e.candidatePageUrls){if(t.candidatePageUrls.length>=20)break;s.has(l.url)||(t.candidatePageUrls.push(l),s.add(l.url))}}function kw(t,e){let n={emails:[],socialProfiles:[],contactForms:[],candidatePageUrls:[]},r=t.kpo?.email?.trim().toLowerCase();r&&n.emails.push({value:r,sourceUrl:e,context:`Structured entity data for ${t.kpo?.entityName??"the page entity"}`,sourceType:"structured_data"});for(let o of[...t.kpo?.sameAs??[],...t.kpo?.socialLinks??[]])try{let s=new URL(o);if(!["http:","https:"].includes(s.protocol))continue;let l=s.hostname.replace(/^www\./,"").split(".")[0];n.socialProfiles.some(m=>m.url===s.href)||n.socialProfiles.push({url:s.href,network:l,sourceUrl:e,context:"Structured entity social profile"})}catch{}return n}function Rw(t){return{name:t.name,websiteUrl:t.websiteUrl??null,address:t.address??null,phone:t.phone??null,placeUrl:t.placeUrl??null,cid:t.cid==null?null:String(t.cid),organization:t.organization??null,category:t.category??null}}function _l(t){return typeof t=="object"&&t!==null&&"source"in t&&typeof t.source=="object"&&t.source!==null&&"mapped"in t&&typeof t.mapped=="object"&&t.mapped!==null}async function qT(t){let e,n;try{if(_l(t.row))e=t.row,n=kl.parse(t.options??{});else{let B=bw({rows:[t.row],columnMap:t.columnMap,options:t.options});e={...B.rows[0],ordinal:t.ordinal},n=B.options}}catch{return{ordinal:t.ordinal,source:_l(t.row)?t.row.source:t.row,rowStatus:"failed",identity:{status:"invalid",confidence:0,reasons:["invalid_input"],selected:null,candidates:[]},contacts:ar(),people:[],usage:{mapsAttempts:0,pageAttempts:0,pageSuccesses:0,serpSearches:0},warnings:[],error:{code:"invalid_row",message:"The row could not be normalized with the supplied column mapping."}}}let r={mapsAttempts:0,pageAttempts:0,pageSuccesses:0,serpSearches:0},o;if(e.mapped.profileUrl||e.mapped.websiteUrl)o=Sl(e.mapped,[]);else{let B=vw(e.mapped);try{r.mapsAttempts=1;let de=await t.adapters.mapsSearch({query:B.query,location:B.location,maxResults:5,gl:n.gl,hl:n.hl}),Ie=(Array.isArray(de)?de:de.results).map(Rw);o=Sl(e.mapped,Ie)}catch(de){return de instanceof Qr&&(r.mapsAttempts=0),{ordinal:e.ordinal,source:e.source,rowStatus:"failed",identity:{status:"not_found",confidence:0,reasons:["maps_provider_failure"],selected:null,candidates:[]},contacts:ar(),people:[],usage:r,warnings:[],error:{code:"resolution_failed",message:"The business lookup could not be completed for this row."}}}}if(o.status==="ambiguous"||o.status==="not_found"||o.status==="invalid")return{ordinal:e.ordinal,source:e.source,rowStatus:o.status==="invalid"?"failed":"complete",identity:o,contacts:ar(),people:[],usage:r,warnings:[],error:o.status==="invalid"?{code:"invalid_row",message:"The row could not be resolved."}:null};let s=e.mapped.profileUrl??e.mapped.websiteUrl??o.selected?.websiteUrl??null,l=s?{value:s,sourceUrl:o.status==="provided_url"?"provided":o.selected?.placeUrl??"maps_search"}:null,m=e.mapped.phone?{value:e.mapped.phone,sourceUrl:"provided"}:o.selected?.phone?{value:o.selected.phone,sourceUrl:o.selected.placeUrl??"maps_search"}:null,c=[];if(!s){let B=await xl({mapped:e.mapped,options:n,adapters:t.adapters,usage:r,warnings:c}),de=[...c.includes("people_search_failed")?["The optional associated-people search could not be completed."]:[],...c.includes("people_linkedin_search_failed")?["One or more optional LinkedIn candidate searches could not be completed."]:[],...c.includes("people_not_found")?["No role-bearing associated people were found in the bounded search."]:[],...c.includes("people_discovery_skipped_for_person")?["Associated-people discovery applies to business rows and was skipped for this person row."]:[]];return{ordinal:e.ordinal,source:e.source,rowStatus:"complete",identity:o,contacts:{...ar(),website:l,phone:m,sourceUrls:[l?.sourceUrl,m?.sourceUrl].filter(Ie=>!!Ie)},people:B,usage:r,warnings:["The matched listing did not expose a public website.",...de],error:null}}let f={emails:[],socialProfiles:[],contactForms:[],candidatePageUrls:[]};e.mapped.email&&f.emails.push({value:e.mapped.email,sourceUrl:"provided",context:`Provided for ${e.mapped.name}`,sourceType:"provided"});let v=new Set,d=[s],p=null;for(;d.length&&v.size<n.maxPagesPerLead;){let B=d.find(de=>!v.has(de));if(!B)break;v.add(B),r.pageAttempts+=1;try{let de=await t.adapters.extractPage({url:B});r.pageSuccesses+=1;let Ie=de.url??B,ze=de.contactEvidence??Ds(de.bodyHtml??de.bodyMarkdown??"",Ie);Eo(ze,kw(de,Ie)),Eo(f,ze),!p&&de.kpo?.phone&&(p={value:de.kpo.phone,sourceUrl:Ie}),d=So(s,f.candidatePageUrls,n.maxPagesPerLead)}catch{c.push("page_failed"),d=So(s,f.candidatePageUrls,n.maxPagesPerLead)}}if(!f.emails.length&&n.emailSearchFallback==="serp_snippets"){let B=zs(s);if(B)try{let de=await t.adapters.searchEmailSerp({query:B,gl:n.gl,hl:n.hl});r.serpSearches+=1;let Ie=Array.isArray(de)?de:de.organicResults;Eo(f,{emails:Fs({websiteUrl:s,results:Ie}),socialProfiles:[],contactForms:[],candidatePageUrls:[]})}catch{c.push("serp_failed")}}let h=await xl({mapped:e.mapped,options:n,adapters:t.adapters,usage:r,warnings:c}),w=[...c.includes("page_failed")?["One or more selected website pages could not be read."]:[],...c.includes("serp_failed")?["The optional email search could not be completed."]:[],...c.includes("people_search_failed")?["The optional associated-people search could not be completed."]:[],...c.includes("people_linkedin_search_failed")?["One or more optional LinkedIn candidate searches could not be completed."]:[],...c.includes("people_not_found")?["No role-bearing associated people were found in the bounded search."]:[],...c.includes("people_discovery_skipped_for_person")?["Associated-people discovery applies to business rows and was skipped for this person row."]:[]];if(!r.pageSuccesses&&!f.emails.length&&!h.length)return{ordinal:e.ordinal,source:e.source,rowStatus:"failed",identity:o,contacts:{...ar(),website:l,phone:m},people:h,usage:r,warnings:w,error:{code:"page_extraction_failed",message:"No selected website page could be read for this row."}};let R={entityType:e.mapped.entityType,name:e.mapped.name,organization:e.mapped.organization},A=["provided","first_party_page","structured_data","serp_snippet"],C=["person","team","office","unknown"],P=f.emails.map(B=>({...B,...Hs(B,R)})).sort((B,de)=>A.indexOf(B.sourceType)-A.indexOf(de.sourceType)||C.indexOf(B.scope)-C.indexOf(de.scope)),M=Ns(f.emails,R),H=f.contactForms[0]?{value:f.contactForms[0].url,sourceUrl:f.contactForms[0].sourceUrl}:null,K=m??p,Z=[...new Set([l?.sourceUrl,K?.sourceUrl,...P.map(B=>B.sourceUrl),...f.socialProfiles.map(B=>B.sourceUrl),H?.sourceUrl].filter(B=>!!B))];return{ordinal:e.ordinal,source:e.source,rowStatus:"complete",identity:o,contacts:{website:l,phone:K,primaryEmail:M,emails:P,socialProfiles:f.socialProfiles.slice(0,20),contactFormUrl:H,sourceUrls:Z},people:h,usage:r,warnings:w,error:null}}var Tw=["enriched_row_status","enriched_identity_status","enriched_match_confidence","enriched_matched_name","enriched_organization","enriched_website","enriched_phone","enriched_primary_email","enriched_primary_email_scope","enriched_secondary_email","enriched_all_emails","enriched_social_profiles","enriched_contact_form_url","enriched_primary_person_name","enriched_primary_person_role","enriched_primary_person_linkedin","enriched_all_people","enriched_maps_url","enriched_cid","enriched_source_urls","enriched_warnings"];function sr(t){return JSON.stringify(t)}function DT(t,e){let n=e??Object.keys(t[0]?.source??{});for(let o of n)if(o.toLowerCase().startsWith(Il))throw new Error(`Source header ${o} collides with reserved enriched_ output columns`);let r=[...t].sort((o,s)=>o.ordinal-s.ordinal);return{headers:[...n,...Tw],rows:r.map(o=>{let s=o.contacts.primaryEmail?o.contacts.emails.find(l=>l.value!==o.contacts.primaryEmail?.value)??null:null;return[...n.map(l=>o.source[l]??null),o.rowStatus,o.identity.status,o.identity.confidence,o.identity.selected?.name??"",o.identity.selected?.organization??"",o.contacts.website?.value??"",o.contacts.phone?.value??"",o.contacts.primaryEmail?.value??"",o.contacts.primaryEmail?.scope??"",s?.value??"",sr(o.contacts.emails.map(l=>({value:l.value,scope:l.scope,sourceType:l.sourceType,sourceUrl:l.sourceUrl,...l.sourceTitle?{sourceTitle:l.sourceTitle}:{},...l.sourcePosition!=null?{sourcePosition:l.sourcePosition}:{},context:l.context,reasons:l.reasons}))),sr(o.contacts.socialProfiles.map(l=>({url:l.url,network:l.network,sourceUrl:l.sourceUrl}))),o.contacts.contactFormUrl?.value??"",o.people[0]?.name??"",o.people[0]?.role??"",o.people[0]?.linkedinUrl??"",sr(o.people),o.identity.selected?.placeUrl??"",o.identity.selected?.cid??"",sr(o.contacts.sourceUrls),sr([...o.warnings,...o.error?[o.error.message]:[]])]})}}import{z as Cw}from"zod";var lr=["LocalBusiness","AnimalShelter","AutomotiveBusiness","AutoBodyShop","AutoDealer","AutoPartsStore","AutoRental","AutoRepair","AutoWash","GasStation","MotorcycleDealer","MotorcycleRepair","ChildCare","Dentist","DryCleaningOrLaundry","EmergencyService","EmploymentAgency","EntertainmentBusiness","FinancialService","AccountingService","AutomatedTeller","BankOrCreditUnion","InsuranceAgency","FoodEstablishment","Bakery","BarOrPub","Brewery","CafeOrCoffeeShop","Distillery","FastFoodRestaurant","IceCreamShop","Restaurant","Winery","HealthAndBeautyBusiness","BeautySalon","DaySpa","HairSalon","HealthClub","NailSalon","TattooParlor","HomeAndConstructionBusiness","Electrician","GeneralContractor","HVACBusiness","HousePainter","Locksmith","MovingCompany","Plumber","RoofingContractor","LegalService","Library","LodgingBusiness","MedicalBusiness","Pharmacy","Physician","RealEstateAgent","RecyclingCenter","SelfStorage","ShoppingCenter","SportsActivityLocation","Store","TouristInformationCenter","TravelAgency"],Aw=Cw.enum(lr),Pw={home:"HomeAndConstructionBusiness",professional:"LocalBusiness",restaurants:"FoodEstablishment",financial:"FinancialService",realestate:"RealEstateAgent",auto:"AutomotiveBusiness",wellness:"HealthAndBeautyBusiness"};function Rl(t){return Pw[t]}function zT(t,e){let n=Aw.safeParse(e);return n.success?n.data:Rl(t)}function FT(t){return{rootType:"LocalBusiness",selectedDefault:t?Rl(t):"LocalBusiness",acceptedSubtypes:lr,callerSuppliedJsonLdAccepted:!1,compilation:"MCP Scraper generates JSON-LD from source-backed listing evidence after acquisition."}}var Ow=new Intl.Segmenter(void 0,{granularity:"word"});function ft(t){let e=0;for(let n of Ow.segment(t))n.isWordLike&&(e+=1);return e}function Rt(t,e,n){let r=ft(t),o=e.reduce((v,d)=>{let p=Number(d.timestamp?.[1]);return Number.isFinite(p)&&p>v?p:v},0),s=typeof n=="number"&&Number.isFinite(n)&&n>0?n:o>0?o:null,l=s&&s>0?Math.round(r/s*600)/10:null,m=r===0?"empty":r<5?"low_speech_signal":"speech_detected",c=r<=2,f=[];return m==="empty"?f.push("No speech was transcribed. This does not distinguish silent media from an inaccessible or expired playback URL."):m==="low_speech_signal"&&f.push("Very little speech was transcribed. Review the media before treating this as a complete transcript."),c&&f.push("If speech was expected, refresh the media source and retry using the tool for its original platform."),{status:m,speechDetected:m==="speech_detected",confidence:m==="speech_detected"?"medium":"low",basis:"transcript_word_count_and_timing",mediaDurationSec:s,wordsPerMinute:l,retryRecommended:c,warnings:f}}import{fal as Ew}from"@fal-ai/client";var Mo=9e4,Pn=Mo+15e3,ii=["af","am","ar","as","az","ba","be","bg","bn","bo","br","bs","ca","cs","cy","da","de","el","en","es","et","eu","fa","fi","fo","fr","gl","gu","ha","haw","he","hi","hr","ht","hu","hy","id","is","it","ja","jw","ka","kk","km","kn","ko","la","lb","ln","lo","lt","lv","mg","mi","mk","ml","mn","mr","ms","mt","my","ne","nl","nn","no","oc","pa","pl","ps","pt","ro","ru","sa","sd","si","sk","sl","sn","so","sq","sr","su","sv","sw","ta","te","tg","th","tk","tl","tr","tt","uk","ur","uz","vi","yi","yo","zh"];function jw(t,e,n,r){let o=l=>`${Math.floor(l/60)}:${String(Math.floor(l%60)).padStart(2,"0")}`,s=[t,"",`*Transcribed in ${(r/1e3).toFixed(1)}s*`,"","## Full Text","",e,""];if(n.length){s.push("## Timestamped Segments","");for(let l of n)s.push(`**[${o(l.timestamp[0])} -> ${o(l.timestamp[1])}]** ${l.text.trim()}`,"")}return s.join(`
2
2
  `)}async function eC(t,e="# Media Transcript",n="en",r){let o=Date.now(),s=Ts(Mo,r),l=Is(),m=null;try{m=await Ms({tool:l?.op==="yt_transcription"?"youtube_transcribe":l?.op==="google_ads_transcribe"?"google_ads_transcribe":l?.op==="tiktok_video_transcribe"||l?.op==="tiktok_transcribe"?"tiktok_video_transcribe":l?.op==="instagram_transcribe"?"instagram_media_download":l?.op==="facebook_video_transcribe"?"facebook_video_transcribe":"facebook_ad_transcribe",method:l?.op==="instagram_transcribe"?"fal_wizper_optional":l?.op==="google_ads_transcribe"||l?.op==="facebook_ad_transcribe"?"fal_wizper_direct_media":"fal_wizper",provider:"fal",providerProduct:"fal_wizper"})}catch(P){console.warn(JSON.stringify({event:"fal_cost_telemetry_start_failed",message:P instanceof Error?P.message:String(P)}))}let c=Promise.resolve(),f=null,v=P=>{let M=m;!M||!P||(c=c.then(async()=>{f!==P&&(await js(M.receiptId,P),f=P)}).catch(()=>console.warn(JSON.stringify({event:"fal_cost_source_attach_failed",errorCode:"provider_source_attach_failed"}))))},d;try{d=await Ew.subscribe("fal-ai/wizper",{input:{audio_url:t,task:"transcribe",language:n},logs:!1,pollInterval:3e3,abortSignal:s,startTimeout:Mo/1e3,onEnqueue:v})}catch(P){throw await c,m&&await wo(m,s.aborted?"timed_out":"failed","fal_submit_or_result_failed").catch(()=>{}),P}d.requestId&&v(d.requestId),await c;let p=d.data&&typeof d.data=="object"?d.data:{},h=typeof p.text=="string"?p.text:"",w=Array.isArray(p.chunks)?p.chunks:[],R=Rt(h,w);m&&await wo(m,"succeeded").catch(P=>console.warn(JSON.stringify({event:"fal_cost_telemetry_finish_failed",message:P instanceof Error?P.message:String(P)})));let A=Date.now()-o;Rs({vendor:"fal_wizper",model:"fal-ai/wizper",units:A/1e3,unitType:"wall_sec"});let C=ks("fal_wizper",A/1e3);return{text:h,chunks:w,durationMs:A,costUsd:C,markdown:jw(e,h,w,A),transcriptSignal:R}}import{z as k}from"zod";var Mw=["desktop","mobile"],Lw=["location","configured","none"],Cl=["paa_found","paa_partial","no_paa","serp_only","degraded_result","captcha","request_aborted","timeout","location_mismatch","proxy_tunnel_failed","proxy_unavailable","browser_session_interrupted","mcp_unavailable","error"],Uw=["not_requested","matched","mismatch","unknown"],Al=["organic","ai_citation","local_pack_website","configured_target","site_subject"],$w=["captured","skipped","failed"],qw=["fetch","headless","browser","mcp"],Dw=/(^|\.)localhost$/i,Hw=/^\d{1,3}(?:\.\d{1,3}){3}$/;function Nw(t){if(!Hw.test(t))return!1;let e=t.split(".").map(o=>Number(o));if(e.some(o=>!Number.isInteger(o)||o<0||o>255))return!0;let[n,r]=e;return n===10||n===127||n===0||n===169&&r===254||n===172&&r>=16&&r<=31||n===192&&r===168||n===100&&r>=64&&r<=127}function zw(t){let e=t.replace(/^\[/,"").replace(/\]$/,"").toLowerCase();return e==="::1"||e==="0:0:0:0:0:0:0:1"||e.startsWith("fc")||e.startsWith("fd")||e.startsWith("fe80:")}function Fw(t){try{let e=new URL(t);if(e.protocol!=="http:"&&e.protocol!=="https:")return!1;let n=e.hostname.toLowerCase();return!(!n||Dw.test(n)||Nw(n)||zw(n))}catch{return!1}}var On=k.string().url().refine(Fw,"url must be a public HTTP or HTTPS URL"),oC=k.object({query:k.string().trim().min(1,"query is required"),location:k.string().trim().min(1).optional(),gl:k.string().trim().length(2).default("us"),hl:k.string().trim().length(2).default("en"),device:k.enum(Mw).default("desktop"),proxyMode:k.enum(Lw).default(xs),proxyZip:k.string().regex(/^\d{5}$/).optional(),pages:k.number().int().min(1).max(2).default(1),debug:k.boolean().default(!1),includePageSnapshots:k.boolean().default(!1),pageSnapshotLimit:k.number().int().min(0).max(10).default(0)}).strict(),Gw=k.object({url:On,sourceKind:k.enum(Al).default("configured_target"),sourcePosition:k.number().int().min(1).optional()}).strict(),aC=k.object({urls:k.array(On).min(1).max(25).optional(),targets:k.array(Gw).min(1).max(25).optional(),maxConcurrency:k.number().int().min(1).max(5).default(2),timeoutMs:k.number().int().min(1e3).max(6e4).default(15e3),debug:k.boolean().default(!1)}).strict().superRefine((t,e)=>{let n=!!t.urls?.length,r=!!t.targets?.length;n===r&&e.addIssue({code:k.ZodIssueCode.custom,message:"provide exactly one of urls or targets",path:n?["targets"]:["urls"]})}),ve=k.string().nullable(),oi={url:k.string(),rawUrl:k.string().optional(),resolvedUrl:ve.optional(),linkType:k.enum(["plain","google_url_redirect","google_goto_redirect"]).optional(),resolutionStatus:k.enum(["not_needed","resolved","unresolved","rejected"]).optional()},Tl=k.object({text:k.string(),href:k.string(),rawUrl:k.string().optional(),resolvedUrl:ve.optional(),linkType:oi.linkType,resolutionStatus:oi.resolutionStatus}),Ww=k.object({position:k.number().int().min(1),title:k.string(),...oi,sourcePage:k.union([k.literal(1),k.literal(2)]).optional(),domain:k.string(),cite:ve,snippet:ve,isRedditStyle:k.boolean(),inlineRating:k.object({value:k.string(),count:k.string()}).nullable()}),Pl=k.object({...oi,title:ve,site:ve,description:ve.optional()}),Bw=k.object({questionId:k.string(),googleLinkId:ve,googleEvidenceId:ve,question:k.string(),answer:ve,answerExpansionIncomplete:k.boolean().optional(),sourceTitle:ve,sourceSite:ve,sourceCite:ve,sources:k.array(Pl),depth:k.number().int().min(0),parentQuestion:ve}),Ol=k.lazy(()=>Bw.extend({children:k.array(Ol)})),Kw=k.object({seed_query:k.string(),question_id:k.string(),google_link_id:k.string(),google_evidence_id:k.string(),question:k.string(),answer:k.string(),answerExpansionIncomplete:k.boolean().optional(),source_title:k.string(),source_site:k.string(),source_cite:k.string(),sources:k.array(Pl),depth:k.number().int().min(0),parent_question:k.string(),extracted_at:k.string()}),Vw=k.object({position:k.number().int().min(1),name:k.string(),cid:ve.optional(),rating:ve.optional(),reviewCount:ve.optional(),metadata:k.array(k.string()).optional(),websiteUrl:ve.optional(),directionsUrl:ve.optional()}),Jw=k.object({type:k.enum(["reddit","facebook","instagram","tiktok","youtube","news","unknown"]),title:k.string(),url:k.string(),source:k.string(),platform:k.string(),popularComment:ve,engagement:k.string(),date:k.string(),duration:ve,authorNote:ve}),cr=k.object({status:k.enum(Uw),expected:k.object({city:k.string(),regionCode:ve,canonicalLocation:k.string()}).nullable(),candidates:k.array(k.object({city:k.string(),regionCode:k.string(),count:k.number().int().min(0),examples:k.array(k.string())}))}),Tt=k.number().min(0),se=k.number().int().min(0),Lo=k.object({completionStatus:k.enum(["paa_found","no_paa","serp_only"]),problem:k.null().optional(),resultQuality:k.enum(["complete","partial","degraded"]).optional(),degradedResult:k.boolean().optional(),degradationReasons:k.array(k.enum(["empty_primary_serp","page_two_missing"])).optional(),retryRecommended:k.boolean().optional(),noPaaObserved:k.boolean().optional(),phaseTimings:k.object({providerConnectMs:Tt.optional(),navigationMs:Tt.optional(),serpParseMs:Tt.optional(),paaExpansionMs:Tt.optional(),linkResolutionMs:Tt.optional(),totalServerMs:Tt}).optional(),pagination:k.object({requestedPages:k.union([k.literal(1),k.literal(2)]),capturedPages:k.union([k.literal(1),k.literal(2)]),page2Status:k.enum(["not_requested","not_attempted","captured","unavailable","failed"]),page1OrganicCount:se,page2OrganicCount:se,failureCode:k.enum(["missing_next","invalid_next","empty_page","captcha","timeout","navigation_error","unsupported_driver"]).optional()}).optional(),completeness:k.object({paaWithoutAnswer:se,paaWithoutSource:se,paaIncompleteAnswers:se.optional(),paaAnswersRecovered:se,aioShareCaptured:k.boolean().nullable(),paaRequested:se.optional(),paaReturned:se.optional(),paaUnique:se.optional()}).optional(),interruption:k.object({code:k.enum(["browser_session_interrupted","request_aborted","timeout"])}).optional(),warnings:k.array(k.object({code:k.enum(["short_videos_captcha_skipped","paa_partial"]),surface:k.enum(["short_videos","paa"]),retryable:k.boolean()})).optional(),links:k.object({linkCandidatesObserved:se,plainLinksObserved:se,googleUrlRedirectsObserved:se,googleGotoRedirectsObserved:se,redirectsResolved:se,redirectsUnresolved:se,redirectsRejected:se,resolverNonGoogleRequestsObserved:se,paaSourcesPreserved:se,aiOverviewLabelValidated:k.boolean()}).optional(),paaLifecycle:k.object({requestedQuestions:se,questionsFound:se,targetStatus:k.enum(["met","not_met"]),discoveryStatus:k.enum(["in_progress","target_reached","frontier_exhausted","no_paa_observed","interrupted","recovery_exhausted"]),materialStatus:k.enum(["complete","partial"]),automaticRetries:se,retryBreakdown:k.object({controlRetries:se,browserRetries:se,providerFallbacks:se,runtimeRecoveries:se,idempotentReplays:se}),knownQuestions:se,processedQuestions:se,failedInteractions:se,noGrowthConfirmations:se,visibleControls:se.optional(),eligibleControls:se.optional(),dispatchedInteractions:se.optional(),confirmedInteractions:se.optional(),unconfirmedInteractions:se.optional()}).optional()}),dr=k.object({seed:k.string(),location:ve,extractedAt:k.string(),totalQuestions:se,surface:k.enum(["web","aim","unknown"]),aiOverview:k.object({detected:k.boolean(),text:ve,citations:k.array(Tl),expanded:k.boolean().optional(),fullyExpanded:k.boolean().optional(),sections:k.array(k.string()).optional(),shareUrl:ve.optional()}),aiMode:k.object({detected:k.boolean(),text:ve,citations:k.array(Tl)}),tree:k.array(Ol),flat:k.array(Kw),videos:k.array(k.object({type:k.enum(["video","short_video"]),title:k.string(),channel:k.string(),platform:k.string(),duration:k.string(),url:k.string()})),forums:k.array(k.object({title:k.string(),source:k.string(),url:k.string()})),organicResults:k.array(Ww),localPack:k.array(Vw),entityIds:k.object({entities:k.array(k.object({name:k.string(),kgId:ve,cid:ve,gcid:ve})),kgIds:k.array(k.string()),cids:k.array(k.string()),gcids:k.array(k.string())}),stats:k.object({seed:k.string(),totalQuestions:se,maxDepthReached:se,durationMs:Tt,errorCount:se}),diagnostics:Lo,whatPeopleSaying:k.array(Jw)}),Uo=[...Cl,"request_aborted","captcha_exhausted","harvest_timeout","service_unavailable","vendor_unavailable","extraction_failed","invalid_url","capture_failed"],yn=k.object({attemptNumber:k.number().int().min(1),outcome:k.enum([...Cl,"page_captured","page_failed"]),startedAt:k.string().optional(),completedAt:k.string().optional(),durationMs:Tt.optional(),problemCode:k.enum(Uo).optional(),cleanupSucceeded:k.boolean().nullable().optional()}),ai=k.object({url:On,requestedUrl:On,finalUrl:On.nullable(),sourceKind:k.enum(Al),sourcePosition:k.number().int().min(1).nullable(),status:k.enum($w),fetchedVia:k.enum(qw).nullable(),httpStatus:k.number().int().min(100).max(599).nullable(),contentType:ve,title:ve,canonicalUrl:On.nullable(),metaDescription:ve,headings:k.array(k.object({level:k.number().int().min(1).max(6),text:k.string()})).default([]),artifact:k.object({htmlBlobUrl:k.string().url().nullable(),textBlobUrl:k.string().url().nullable(),markdownBlobUrl:k.string().url().nullable(),screenshotBlobUrl:k.string().url().nullable(),contentSha256:ve,capturedAt:ve}),error:k.object({code:k.enum(["invalid_url","timeout","capture_failed"]),message:k.string()}).nullable(),schemaTypes:k.array(k.string()).optional(),wordCount:se.optional(),linkCounts:k.object({wordCount:se,internalLinkCount:se,externalLinkCount:se,totalLinkCount:se}).optional(),hashes:k.object({htmlSha256:ve,markdownSha256:ve,textSha256:ve}).optional(),content:k.object({html:ve,markdown:ve,text:ve}).optional(),diagnostics:k.object({requestedAt:k.string(),completedAt:k.string(),durationMs:Tt,urlValidation:k.object({passed:k.boolean()}),browserFallback:k.object({configured:k.boolean(),used:k.boolean(),boundedByTimeoutMs:Tt}),content:k.object({htmlIncluded:k.boolean(),markdownIncluded:k.boolean(),textIncluded:k.boolean(),maxContentChars:se,truncated:k.boolean()})}).optional()}),ur=ai.partial().extend({artifact:ai.shape.artifact.partial().optional()}),pr=k.object({requestedCount:se.optional(),capturedCount:se.optional(),failedCount:se.optional(),maxConcurrency:k.number().int().min(1).max(5).optional(),timeoutMs:Tt.optional()}),El=k.object({harvestResult:dr,attempts:k.array(yn),locationEvidence:cr.nullable(),pageSnapshotArtifacts:k.array(ai),billing:k.object({creditsUsed:Tt.optional(),requestId:k.string().optional(),jobId:k.string().optional()}).optional()}),jl=k.object({pageSnapshotArtifacts:k.array(ai),attempts:k.array(yn).default([]),diagnostics:pr.optional()});function mr(t){let e=new Set(Uo);return(Array.isArray(t)?t:[]).map(n=>{let r=n;return yn.parse({...r,...r.problemCode!==void 0&&!e.has(String(r.problemCode))?{problemCode:"extraction_failed"}:{}})})}function $o(t){return dr.partial().parse(t)}function yr(t){return(Array.isArray(t)?t:[]).map(e=>{let n=e,r=n.error&&typeof n.error=="object"?n.error:null,o=r?.code==="timeout"||r?.code==="invalid_url"?r.code:"capture_failed";return ur.parse({...n,error:r?{code:o,message:o==="timeout"?"Page capture timed out. Retry this URL when ready.":o==="invalid_url"?"Provide a public HTTP or HTTPS page URL.":"Page capture failed. Retry this URL when ready."}:null})})}function cC(t){let e=t;return El.parse({...e,harvestResult:$o(e.harvestResult),attempts:mr(e.attempts),pageSnapshotArtifacts:yr(e.pageSnapshotArtifacts)})}function dC(t){let e=t;return jl.parse({pageSnapshotArtifacts:yr(e.pageSnapshotArtifacts),attempts:mr(e.attempts),...e.diagnostics?{diagnostics:qo(e.diagnostics)}:{}})}function qo(t){return pr.parse(t)}function ee(t){return{title:t,readOnlyHint:!0,destructiveHint:!1,idempotentHint:!1,openWorldHint:!0}}function Ge(t){return{title:t,readOnlyHint:!0,destructiveHint:!1,idempotentHint:!0,openWorldHint:!1}}function Bt(t,e){return e.length===0?t:Object.fromEntries(Object.entries(t).filter(([n])=>!e.includes(n)))}function Yw(t=process.env){let e=t.MCP_SCRAPER_OUTPUT_SCHEMA_MODE?.trim().toLowerCase();if(e){if(e==="contract"||e==="all")return"contract";if(e==="runtime"||e==="none"||e==="essential")return"runtime";throw new Error(`Unsupported MCP_SCRAPER_OUTPUT_SCHEMA_MODE: ${e}`)}let n=t.MCP_SCRAPER_ADVERTISE_OUTPUT_SCHEMAS?.trim().toLowerCase();if(n==="true")return"contract";if(n===void 0||n===""||n==="false")return"runtime";throw new Error(`Unsupported MCP_SCRAPER_ADVERTISE_OUTPUT_SCHEMAS value: ${n}`)}var Xw=Yw(),Zw=Xw==="contract",si={};function T(t,e){return si[t]=e,Zw?e:void 0}import{z as O}from"zod";import{z as zt}from"zod";var li="Expected a public http(s) URL or bare domain (for example example.com)",gn=zt.string().trim().min(1).transform((t,e)=>{if(/^[/?#]/.test(t)||/[\\\u0000-\u001f\u007f]/.test(t))return e.addIssue({code:zt.ZodIssueCode.custom,message:li}),zt.NEVER;let n=/^[a-z][a-z0-9+.-]*:\/\//i.test(t),r=n?t:`https://${t}`;try{let o=new URL(r);if(!["http:","https:"].includes(o.protocol)||o.username||o.password)return e.addIssue({code:zt.ZodIssueCode.custom,message:li}),zt.NEVER;if(!n){let s=o.hostname.replace(/^\[|\]$/g,""),l=/^\d{1,3}(?:\.\d{1,3}){3}$/.test(s),m=s.includes(":"),c=s.includes(".");if(!l&&!m&&!c&&!(s==="localhost"))return e.addIssue({code:zt.ZodIssueCode.custom,message:li}),zt.NEVER}return o.href}catch{return e.addIssue({code:zt.ZodIssueCode.custom,message:li}),zt.NEVER}}),b=zt.string().nullable();import{z as N}from"zod";var ci=N.object({requestedQuestions:N.number().int().min(0),questionsFound:N.number().int().min(0),targetStatus:N.enum(["met","not_met"]),discoveryStatus:N.enum(["in_progress","target_reached","frontier_exhausted","no_paa_observed","interrupted","recovery_exhausted"]),materialStatus:N.enum(["complete","partial"]),completionReason:N.string(),automaticRetries:N.number().int().min(0),retryBreakdown:N.object({controlRetries:N.number().int().min(0),browserRetries:N.number().int().min(0),providerFallbacks:N.number().int().min(0),runtimeRecoveries:N.number().int().min(0),idempotentReplays:N.number().int().min(0)}),knownQuestions:N.number().int().min(0),processedQuestions:N.number().int().min(0),failedInteractions:N.number().int().min(0),noGrowthConfirmations:N.number().int().min(0),visibleControls:N.number().int().min(0),eligibleControls:N.number().int().min(0),dispatchedInteractions:N.number().int().min(0),confirmedInteractions:N.number().int().min(0),unconfirmedInteractions:N.number().int().min(0)}).nullable(),gr=N.object({providerConnectMs:N.number().int().min(0).optional(),navigationMs:N.number().int().min(0).optional(),serpParseMs:N.number().int().min(0).optional(),paaExpansionMs:N.number().int().min(0).optional(),linkResolutionMs:N.number().int().min(0).optional(),totalServerMs:N.number().int().min(0)}).nullable(),Ml=N.object({observations:N.number().int().min(0),dispatched:N.number().int().min(0),confirmed:N.number().int().min(0),unconfirmed:N.number().int().min(0),dispatchFailures:N.number().int().min(0),confirmedWithoutDispatchAck:N.number().int().min(0),expandedSignals:N.number().int().min(0),answerSignals:N.number().int().min(0),sourceSignals:N.number().int().min(0),questionGrowthBatches:N.number().int().min(0),checkpoint700ms:N.number().int().min(0),checkpoint1000ms:N.number().int().min(0),checkpoint1400ms:N.number().int().min(0)}),Ll=N.object({id:N.string(),questionId:N.string(),question:N.string(),batchNumber:N.number().int().min(0),controlAttempt:N.number().int().min(0),observedAt:b,dispatched:N.boolean(),outcome:N.enum(["confirmed","unconfirmed","dispatch_failed"]),confirmationSignals:N.array(N.enum(["expanded","answer","source","question_growth"])),confirmationCheckpointMs:N.union([N.literal(700),N.literal(1e3),N.literal(1400)]).nullable(),confirmedAtMs:N.number().int().min(0).nullable(),dispatchDurationMs:N.number().int().min(0),totalDurationMs:N.number().int().min(0),beforeQuestionCount:N.number().int().min(0),afterQuestionCount:N.number().int().min(0),newQuestionCount:N.number().int().min(0),expandedBefore:N.boolean(),expandedAfter:N.boolean(),answerObserved:N.boolean(),sourceCount:N.number().int().min(0),errorCode:N.enum(["click_failed","click_ack_timeout","control_missing","confirmation_timeout"]).nullable()}),En=N.object({requestedPages:N.union([N.literal(1),N.literal(2)]),capturedPages:N.union([N.literal(1),N.literal(2)]),page2Status:N.enum(["not_requested","not_attempted","captured","unavailable","failed"]),page1OrganicCount:N.number().int().min(0),page2OrganicCount:N.number().int().min(0),failureCode:N.enum(["missing_next","invalid_next","empty_page","captcha","timeout","navigation_error","unsupported_driver"]).optional()}).nullable();import{z as we}from"zod";var Ul=we.enum(["plain","google_url_redirect","google_goto_redirect"]),$l=we.enum(["not_needed","resolved","unresolved","rejected"]),ql={url:we.string(),rawUrl:we.string(),resolvedUrl:b,linkType:Ul,resolutionStatus:$l},Qw=we.object({...ql,title:b,site:b,description:b.optional().describe("Captured source-card description from Google, when present.")}),Do=we.object({questionId:we.string(),googleLinkId:b,googleEvidenceId:b,question:we.string(),answer:b,answerExpansionIncomplete:we.boolean().optional().describe("True when a captured answer still has unexpanded content."),sources:we.array(Qw),sourceTitle:b,sourceSite:b,sourceUrl:b}),hr=we.object({position:we.number().int(),sourcePage:we.union([we.literal(1),we.literal(2)]).optional(),title:we.string(),...ql,domain:we.string(),snippet:b}),fr=we.object({detected:we.boolean(),text:b,citations:we.array(we.object({text:we.string(),href:we.string(),rawUrl:we.string(),resolvedUrl:b,linkType:Ul,resolutionStatus:$l})),expanded:we.boolean(),fullyExpanded:we.boolean(),sections:we.array(we.string()),shareUrl:b.optional()}).nullable(),br=we.object({entities:we.array(we.object({name:we.string(),kgId:we.string().nullable(),cid:we.string().nullable(),gcid:we.string().nullable()})).describe("Entities named on the page with their kgId/cid/gcid. Flat lists below are the same IDs deduplicated, kept for backward compatibility."),kgIds:we.array(we.string()),cids:we.array(we.string()),gcids:we.array(we.string())}).nullable();var Dl={jobId:O.string().trim().min(1).max(200).describe("The jobId returned by search_serp.")},jn=O.enum(["observed_present","observed_absent","incomplete","not_requested","unknown","unsupported"]),Ho={mode:O.enum(["light","full"]),jobId:b,status:O.enum(["pending","running","done"]),statusTool:b,replayed:O.boolean(),query:O.string(),location:b,resultQuality:b,degradedResult:O.boolean().nullable(),degradationReasons:O.array(O.string()),retryRecommended:O.boolean().nullable(),pagination:En,serpCompleteness:O.object({pagination:O.object({status:O.enum(["complete","incomplete"]),requestedPages:O.array(O.union([O.literal(1),O.literal(2)])).min(1).max(2),attemptedPages:O.array(O.union([O.literal(1),O.literal(2)])).max(2),capturedPages:O.array(O.union([O.literal(1),O.literal(2)])).max(2),pageResultCounts:O.array(O.object({page:O.union([O.literal(1),O.literal(2)]),resultCount:O.number().int().min(0)})).max(2),providerRequestCount:O.number().int().min(0),failureCode:b}),features:O.object({localPack:jn,forums:jn,videos:jn,aiOverview:jn,whatPeopleSaying:jn}),queryIntegrity:O.object({status:O.enum(["matched","unresolved_location","query_mismatch"]),queryHash:O.string(),normalizedQueryTokens:O.array(O.string()),canonicalLocation:b,locationResolutionSource:O.string(),locationAmbiguousCandidates:O.number().int().min(0),uulePresent:O.boolean(),uuleHash:b,outboundProviderMethod:O.string(),finalQueryHash:b}),failureStage:O.enum(["provider_transport","invalid_payload","serp_readiness","query_integrity","pagination","optional_feature_parsing","unknown"]).nullable()}).nullable(),organicResults:O.array(hr),localPack:O.array(O.object({position:O.number().int(),name:O.string(),rating:b,reviewCount:b,websiteUrl:b})),aiOverview:fr,aiMode:O.object({detected:O.boolean(),text:b,citations:O.array(O.object({text:O.string(),href:O.string()}))}).nullable(),forums:O.array(O.object({title:O.string(),source:O.string(),url:O.string()})),videos:O.array(O.object({type:O.string(),title:O.string(),channel:O.string(),platform:O.string(),duration:O.string(),url:O.string()})),whatPeopleSaying:O.array(O.record(O.string(),O.unknown())),paaPreview:O.array(O.object({question:O.string(),answer:b,url:b,sourceTitle:b.optional(),sourceName:b.optional(),displayLink:b.optional()})),additionalSerpFeatures:O.array(O.object({name:O.string(),attributes:O.record(O.string(),O.union([O.string(),O.number(),O.boolean(),O.null()])),items:O.array(O.object({title:b,url:b,description:b,position:O.number().nullable()}))})),featureStatus:O.record(O.string(),jn),featurePages:O.array(O.record(O.string(),O.unknown())).max(2),entityIds:br,phaseTimings:gr,durationMs:O.number().int().min(0).nullable(),attemptCount:O.number().int().min(1)},Hl={schemaVersion:O.literal("serp-intelligence.capture.v1"),status:O.string(),query:b,location:b,capturedAt:b,resultCount:O.number().int().min(0).nullable(),organicResultCount:O.number().int().min(0),localPackResultCount:O.number().int().min(0),pageSnapshotCount:O.number().int().min(0),pageSnapshotFailedCount:O.number().int().min(0),snapshotId:b,resolvedInputs:O.record(O.string(),O.unknown()),artifacts:O.array(ur),attempts:O.array(yn),billing:O.object({creditsUsed:O.number().min(0).nullable(),requestId:b,jobId:b}).nullable(),diagnostics:Lo.nullable(),harvestResult:dr.partial(),locationEvidence:cr.nullable()},Nl={schemaVersion:O.literal("serp-intelligence.page-snapshots.v1"),status:O.string(),count:O.number().int().min(0),capturedCount:O.number().int().min(0),failedCount:O.number().int().min(0),skippedCount:O.number().int().min(0),captures:O.array(ur),attempts:O.array(yn),resolvedInputs:O.record(O.string(),O.unknown()),diagnostics:pr.nullable()},zl={mode:O.enum(["light","full"]).default("light").describe("Light returns organic results. Full adds available same-page features for an unfiltered one-page search. Date-filtered, two-page, and browser-backup results contain organic listings only and mark rich features unsupported."),query:O.string().min(1).describe("The search topic, exactly as it should be searched. Include the place here when you want it in the search terms \u2014 the server sends your query to Google unchanged and never adds or removes a location."),location:O.string().optional().describe("Compatibility field. Ordinary search ignores this. Include a place in query when needed."),gl:O.string().length(2).default("us").describe("Compatibility field. Ordinary search uses its fixed default market."),hl:O.string().default("en").describe("Compatibility field. Ordinary search uses its fixed default language."),device:O.enum(["desktop","mobile"]).default("desktop").describe("Compatibility field. Ordinary search uses desktop results."),idempotencyKey:O.string().trim().min(8).max(200).optional().describe("Retry key: reuse after a timeout to avoid re-billing. New key per search."),serpIdentity:O.string().regex(/^[a-z0-9][a-z0-9_-]{0,63}$/).optional().describe("Optional persistent SERP identity created with serp_identity_create. Reuses the same saved browser state and fixed network address across calls."),includeAllSerpFeatures:O.boolean().default(!1).describe("Compatibility field. Ordinary search returns organic results only."),includeLocalPack:O.boolean().default(!1).describe("Compatibility field. Ordinary search does not include the local pack."),includeForums:O.boolean().default(!1).describe("Compatibility field. Ordinary search does not include forum modules."),includeVideos:O.boolean().default(!1).describe("Compatibility field. Ordinary search does not include video modules."),includeAiOverview:O.boolean().default(!1).describe("Compatibility field. Ordinary search does not include AI modules."),includeWhatPeopleSaying:O.boolean().default(!1).describe("Compatibility field. Ordinary search does not include social modules."),debug:O.boolean().default(!1).describe("Include sanitized diagnostics for debugging."),pages:O.number().int().min(1).max(2).default(1).describe("One page by default. Set 2 only when the user explicitly requests two pages; two-page results include organic listings only; if page 2 is unavailable, page 1 is returned and billed when available."),recency:O.enum(["week","month"]).optional().describe("Limit Google results to the past week or past month. A date-filtered search uses the managed browser and returns organic listings only. It costs 35 Credits for one page or 70 Credits when two requested pages are delivered. Omit for unfiltered results.")},Fl={query:O.string().min(1).describe("Search topic to capture, exactly as it should be searched. Include the place here when you want it in the search terms \u2014 the server sends your query to Google unchanged and never adds or removes a location."),location:O.string().optional().describe("Where Google should think the searcher is. Sets the Google UULE parameter only \u2014 it never changes your query text and never selects a proxy. To put the place in the search terms too, write it into query."),gl:O.string().length(2).default("us").describe("Google country code inferred from the requested market."),hl:O.string().default("en").describe("Google interface/content language inferred from the user request."),device:O.enum(["desktop","mobile"]).default("desktop").describe("SERP device context. Use mobile only for mobile rankings/evidence."),debug:O.boolean().default(!1).describe("Collect additional location evidence; private browser diagnostics stay server-side."),pages:O.number().int().min(1).max(2).default(1).describe("Google result pages to capture. Use 2 only for deeper ranking evidence."),includePageSnapshots:O.boolean().default(!1).describe("Also capture ranking-page snapshots for selected SERP URLs. Each attempted snapshot adds 1 Credit."),pageSnapshotLimit:O.number().int().min(0).max(10).default(0).describe("Maximum ranking-page snapshots when includePageSnapshots is true. This capacity is held up front and unused capacity is refunded.")},Gl={urls:O.array(O.string().url()).min(1).max(25).optional().describe("Public HTTP/HTTPS URLs to capture. Provide exactly one of urls or targets. Do not pass localhost, private IPs, file URLs, or internal admin URLs."),targets:O.array(O.object({url:O.string().url().describe("Public HTTP/HTTPS URL to capture."),sourceKind:O.enum(["organic","ai_citation","local_pack_website","configured_target","site_subject"]).default("configured_target").describe("Why this page is being captured."),sourcePosition:O.number().int().min(1).optional().describe("Ranking or citation position when the page came from SERP evidence.")}).strict()).min(1).max(25).optional().describe("Structured targets. Provide exactly one of targets or urls; use targets when source kind or position should be preserved."),maxConcurrency:O.number().int().min(1).max(5).default(2).describe("Parallel page captures."),timeoutMs:O.number().int().min(1e3).max(6e4).default(15e3).describe("Per-page capture timeout in milliseconds; timeouts return as structured capture failures."),debug:O.boolean().default(!1).describe("Compatibility field. Results include public page-capture diagnostics only.")};function hn(t){return Qn(t.replace((new RegExp("\u006b\u0065\u0072\u006e\u0065\u006c_session_id", "gi")),"browser_session_id").replace((new RegExp("\u006b\u0065\u0072\u006e\u0065\u006c_delete_succeeded", "gi")),"session_cleanup_succeeded").replace((new RegExp("\u006b\u0065\u0072\u006e\u0065\u006c_delete_started", "gi")),"session_cleanup_started").replace((new RegExp("\u006b\u0065\u0072\u006e\u0065\u006c_delete_error", "gi")),"session_cleanup_error").replace((new RegExp("\u006b\u0065\u0072\u006e\u0065\u006cSessionId", "g")),"browserSessionId").replace((new RegExp("\u006b\u0065\u0072\u006e\u0065\u006cProxyId", "g")),"proxyId").replace((new RegExp("\u004b\u0045\u0052\u004e\u0045\u004c_API_KEY", "g")),"BROWSER_SERVICE_API_KEY").replace((new RegExp("\"\u006b\u0065\u0072\u006e\u0065\u006c\"\\s*:", "gi")),'"browserRuntime":'))}function X(t){return t&&typeof t=="object"&&!Array.isArray(t)?t:{}}function Wl(t){let e=X(t.details),n=X(t.upgrade);return{...e,active:t.active,limit:t.limit,operation:t.operation,upgrade_url:t.upgrade_url,topup_url:t.topup_url,upgrade_command:t.upgrade_command,slots_per_pack:t.slots_per_pack??n.slots_per_pack,pack_price_usd:t.pack_price_usd??n.pack_price_usd,balance_credits:t.balance_credits,required_credits:t.required_credits}}function Mn(t){let e=t.details??{},n=[`${t.error_code}: ${t.message}`];return typeof e.active=="number"&&typeof e.limit=="number"&&n.push(`Active operations: ${e.active}. Account limit: ${e.limit}.`),typeof e.operation=="string"&&n.push(`Operation: ${e.operation}.`),typeof e.balance_credits=="number"&&typeof e.required_credits=="number"&&n.push(`Balance: ${e.balance_credits} Credits. Required: ${e.required_credits} Credits.`),t.retry_after_seconds&&n.push(`Retry after: ${t.retry_after_seconds} seconds.`),t.charge_status&&n.push(`Charge status: ${t.charge_status.replaceAll("_"," ")}.`),typeof e.upgrade_url=="string"&&n.push(`Add capacity: ${e.upgrade_url}`),typeof e.topup_url=="string"&&n.push(`Billing: ${e.topup_url}`),typeof e.upgrade_command=="string"&&n.push(`Terminal: ${e.upgrade_command}`),t.retryable&&n.push("Retryable: yes."),n.join(" ")}function eS(t,e){if(t.error==="idempotency_in_progress"||t.error_code==="idempotency_in_progress"){let r=typeof t.job_id=="string"&&t.job_id.length<=200?t.job_id:null,o=typeof t.path=="string"?t.path:"",s=o==="/harvest"||o==="/harvest/sync",l=typeof t.retry_after_seconds=="number"&&Number.isFinite(t.retry_after_seconds)?Math.max(1,Math.floor(t.retry_after_seconds)):10,m=e?Math.max(Je,l):l,c={error_code:"idempotency_in_progress",error_type:"connection",message:s?"This PAA harvest is already running. Poll the existing job instead of starting a second run.":"This request is already running. Retry with the same idempotencyKey after the indicated delay; a completed result will replay instead of starting or billing another run.",retryable:!0,retry_after_seconds:m,details:{...r?{job_id:r}:{},retry_guidance:s?`Keep the same idempotencyKey and wait at least ${m} seconds before each harvest_paa_status check of the returned job_id until terminal.`:"Keep the same idempotencyKey and retry after the indicated delay. This operation does not expose a resumable status tool."}};return{text:Mn(c),envelope:c}}if(t.error==="insufficient_balance"){let r=Xe({errorCode:"insufficient_balance",errorType:"billing",retryable:!1,details:Wl(t)});return{text:Mn(r),envelope:r}}if(t.error==="mcp_request_timeout"){let r=typeof t.idempotency_fingerprint=="string"&&/^[a-f0-9]{16}$/.test(t.idempotency_fingerprint)?t.idempotency_fingerprint:null,o=typeof t.retry_guidance=="string"?t.retry_guidance.slice(0,500):"Retry with the same idempotencyKey so a completed operation replays instead of running and billing again.",s={...Xe({errorCode:"mcp_request_timeout",retryable:!0,details:{retry_guidance:o}}),...r?{idempotencyFingerprint:r}:{}};return{text:Mn(s),envelope:s}}if(typeof t.error_code=="string"){let r=Wr(t.error_code),o=Xe({errorCode:r,errorType:t.error_type,retryable:t.retryable,retryAfterSeconds:t.retry_after_seconds??t.retryAfterSeconds,chargeStatus:t.charge_status??t.chargeStatus,details:Wl(t)});return{text:`${Mn(o)}${Bl(t)}`,envelope:o}}let n=Xe({errorCode:"service_unavailable",retryable:!0});return{text:Mn(n),envelope:n}}function Y(t,e={}){let n=t.content.find(o=>o.type==="text"),r=n?.type==="text"?n.text:"";try{let o=JSON.parse(r||"{}");if(t.isError||o.error||o.error_code){let l=fn(o,e);return{error:{...l,text:hn(l.text)}}}return{data:o.result??o}}catch{let o=Xe({errorCode:"service_unavailable",retryable:!0});return t.isError?{error:{text:qt("service_unavailable"),envelope:o}}:{error:{text:"Failed to parse tool response",envelope:o}}}}function z(t){return{content:[{type:"text",text:t.text}],structuredContent:t.envelope,isError:!0}}function Bl(t){let e=Array.isArray(t.attempts)?t.attempts:[];return e.length===0?"":`
3
3
 
4
4
  Attempts: