@duckmind/dm-darwin-x64 0.43.2 → 0.43.4

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,18 +1,18 @@
1
- import{createRequire as Hr}from"node:module";var Ar=Object.defineProperty;var Jr=(t)=>t;function Rr(t,e){this[t]=Jr.bind(null,e)}var et=(t,e)=>{for(var r in e)Ar(t,r,{get:e[r],enumerable:!0,configurable:!0,set:Rr.bind(e,r)})};var gt=(t,e)=>()=>(t&&(e=t(t=0)),e);var Fr=Hr(import.meta.url);var er={};et(er,{fetchTopSource:()=>pa,fetchSourceContent:()=>tr,fetchMultipleSources:()=>ga});import{createRequire as fn}from"node:module";import{Readability as gn}from"@mozilla/readability";import{JSDOM as pn}from"jsdom";import on from"turndown";import{platform as ri,tmpdir as On}from"node:os";import{spawn as Tn}from"node:child_process";import{dirname as Pn,join as Ln}from"node:path";import{fileURLToPath as Wn}from"node:url";import{basename as Mn}from"node:path";import{existsSync as $t,mkdirSync as An,readFileSync as Ge,writeFileSync as Jn}from"node:fs";import{homedir as Rn}from"node:os";import{join as Ke}from"node:path";import{tmpdir as bt}from"node:os";import{platform as wi,tmpdir as Hn}from"node:os";import{existsSync as ct,mkdirSync as Fn,readFileSync as Ze,writeFileSync as Yn}from"node:fs";import{homedir as Vn}from"node:os";import{join as de}from"node:path";import{tmpdir as kt}from"node:os";import{existsSync as mt,mkdirSync as zn,readFileSync as Qe,writeFileSync as Sn}from"node:fs";import{homedir as un}from"node:os";import{join as ze}from"node:path";import{tmpdir as yt}from"node:os";function wn(t={}){return{...Ae,...t}}function Je(t){try{let e=new URL(t),r=e.hostname.toLowerCase();for(let n of hn)if(n.test(r))return{blocked:!0,reason:`Private/internal address: ${r}`};if(e.protocol==="file:")return{blocked:!0,reason:"File protocol not allowed"};return{blocked:!1}}catch(e){return{blocked:!0,reason:`Invalid URL: ${e.message}`}}}function $n(t){try{let e=new URL(t);if(!(e.hostname==="github.com"||e.hostname.endsWith(".github.com")))return t;let r=e.pathname.split("/").filter(Boolean);if(r.length<5)return t;let[n,a,l,i,...f]=r;if(l!=="blob")return t;let p=f.join("/");return`https://raw.githubusercontent.com/${n}/${a}/${i}/${p}`}catch{return t}}async function cn(t,e={}){let r=Je(t);if(r.blocked)return{ok:!1,url:t,finalUrl:t,status:403,error:`Blocked: ${r.reason}`,needsBrowser:!1};let n=t;if(t=$n(t),t!==n)console.error(`[fetcher] Rewrote GitHub URL: ${n.slice(0,60)}... → raw.githubusercontent.com`);let{timeoutMs:a=15000,userAgent:l,signal:i}=e,f=new AbortController,p=setTimeout(()=>f.abort(),a);if(i)i.addEventListener("abort",()=>f.abort(),{once:!0});try{let g=await fetch(t,{method:"GET",headers:{...Ae,"user-agent":l||Me},redirect:"follow",signal:f.signal});clearTimeout(p);let o=g.headers.get("content-type")||"",h=g.url,m=g.headers.get("last-modified")||"",c=!1;try{c=new URL(h).hostname.toLowerCase()==="raw.githubusercontent.com"}catch{}if(o.includes("text/plain")&&c){let N=await g.text();return{ok:!0,url:n,finalUrl:h,status:g.status,title:h.split("/").pop()||"GitHub File",byline:"",siteName:"GitHub",lang:"",publishedTime:m,lastModified:m,markdown:N,contentLength:N.length,excerpt:N.slice(0,300).replaceAll(/\n/g," "),needsBrowser:!1}}if(!o.includes("text/html")&&!o.includes("application/xhtml"))return{ok:!1,url:t,finalUrl:h,status:g.status,error:`Unsupported content type: ${o}`,needsBrowser:!1};let y=await g.text(),b=Re(g.status,y,h,t);if(b.blocked)return{ok:!1,url:t,finalUrl:h,status:g.status,error:`Blocked: ${b.reason}`,needsBrowser:!0};let w=He(y,h),_=Fe(w);if(!_.ok)return{ok:!1,url:t,finalUrl:h,status:g.status,error:`Low quality content: ${_.reason}`,needsBrowser:!0};return{ok:!0,url:t,finalUrl:h,status:g.status,title:w.title,byline:w.byline,siteName:w.siteName,lang:w.lang,publishedTime:w.publishedTime||m,lastModified:m,markdown:w.markdown,excerpt:w.excerpt,contentLength:w.markdown.length,needsBrowser:!1}}catch(g){clearTimeout(p);let o=_n(g);return{ok:!1,url:t,finalUrl:t,status:0,error:g.message,needsBrowser:o}}}function Re(t,e,r,n){let a=e.match(/<title[^>]*>([^<]*)<\/title>/i)?.[1]?.toLowerCase()||"",l=e.slice(0,30000).toLowerCase(),i=`${a} ${l}`;if(t===403||t===429||t===503)return{blocked:!0,reason:`HTTP ${t}`};let f=[{pattern:/class=["'][^"']*captcha["']|<div[^>]*id=["']captcha/i,reason:"captcha"},{pattern:/g-recaptcha|data-sitekey|i['"]m not a robot/i,reason:"captcha"},{pattern:/checking your browser.{0,100}please wait|cf-browser-verification/i,reason:"cloudflare challenge"},{pattern:/just a moment.{0,50}security check|ddos protection by cloudflare/i,reason:"cloudflare challenge"},{pattern:/unusual traffic.{0,50}from your computer network/i,reason:"unusual traffic"},{pattern:/bot detected|automated.{0,20}request/i,reason:"bot detection"},{pattern:/enable\s+javascript\s+to\s+view|javascript\s+is\s+required.{0,50}enabled/i,reason:"requires javascript"},{pattern:/access denied|accessdenied/i,reason:"access denied"},{pattern:/protected by anubis|anubis uses a proof-of-work/i,reason:"anubis challenge"}];for(let g of f)if(g.pattern.test(i))return{blocked:!0,reason:g.reason};let p=yn(n,r,e);if(p)return{blocked:!0,reason:p};return{blocked:!1}}function yn(t,e,r){try{let n=new URL(t),a=new URL(e);if(n.hostname.toLowerCase()===a.hostname.toLowerCase())return;let l=a.hostname.toLowerCase();if(mn.some((f)=>l===f||l.endsWith(`.${f}`)))return`redirected to login (${a.hostname})`;if(bn.some((f)=>l.startsWith(f)))return`redirected to login (${a.hostname})`;let i=r.slice(0,20000).toLowerCase();if(kn.some((f)=>i.includes(f)))return`redirected to login page (${a.hostname})`}catch{}return}function _n(t){let e=t.message.toLowerCase();return e.includes("fetch failed")||e.includes("unable to verify")||e.includes("certificate")||e.includes("timeout")}function Ce(t){let e=['meta[property="article:published_time"]','meta[name="article:published_time"]','meta[property="og:published_time"]','meta[name="publication_date"]','meta[name="date"]','meta[itemprop="datePublished"]','time[itemprop="datePublished"]','meta[name="DC.date"]'];for(let r of e){let n=t.querySelector(r),a=n?.getAttribute("content")||n?.getAttribute("datetime")||"";if(a)return a}return""}function He(t,e){let r=new pn(t,{url:e}).window.document,n=new gn(r).parse();if(n&&n.content){let l=We.turndown(n.content).replaceAll(/\n{3,}/g,`
1
+ import{createRequire as Vr}from"node:module";var Fr=Object.defineProperty;var Yr=(t)=>t;function Gr(t,e){this[t]=Yr.bind(null,e)}var et=(t,e)=>{for(var r in e)Fr(t,r,{get:e[r],enumerable:!0,configurable:!0,set:Gr.bind(e,r)})};var gt=(t,e)=>()=>(t&&(e=t(t=0)),e);var dr=Vr(import.meta.url);var lr={};et(lr,{fetchTopSource:()=>ma,fetchSourceContent:()=>ar,fetchMultipleSources:()=>ca});import{createRequire as $n}from"node:module";import{Readability as cn}from"@mozilla/readability";import{JSDOM as mn}from"jsdom";import bn from"turndown";import{platform as wi,tmpdir as An}from"node:os";import{spawn as Rn}from"node:child_process";import{dirname as Jn,join as Hn}from"node:path";import{fileURLToPath as Fn}from"node:url";import{basename as Yn}from"node:path";import{existsSync as ct,mkdirSync as Gn,readFileSync as Ke,writeFileSync as Vn}from"node:fs";import{homedir as dn}from"node:os";import{join as Ue}from"node:path";import{tmpdir as yt}from"node:os";import{platform as ji,tmpdir as Zn}from"node:os";import{existsSync as mt,mkdirSync as Bn,readFileSync as Ie,writeFileSync as Kn}from"node:fs";import{homedir as Un}from"node:os";import{join as Xe}from"node:path";import{tmpdir as _t}from"node:os";import{existsSync as bt,mkdirSync as ra,readFileSync as qe,writeFileSync as na}from"node:fs";import{homedir as aa}from"node:os";import{join as tr}from"node:path";import{tmpdir as kt}from"node:os";function _n(t={}){return{...Fe,...t}}function Ye(t){try{let e=new URL(t),r=e.hostname.toLowerCase();for(let n of yn)if(n.test(r))return{blocked:!0,reason:`Private/internal address: ${r}`};if(e.protocol==="file:")return{blocked:!0,reason:"File protocol not allowed"};return{blocked:!1}}catch(e){return{blocked:!0,reason:`Invalid URL: ${e.message}`}}}function kn(t){try{let e=new URL(t);if(!(e.hostname==="github.com"||e.hostname.endsWith(".github.com")))return t;let r=e.pathname.split("/").filter(Boolean);if(r.length<5)return t;let[n,a,l,i,...f]=r;if(l!=="blob")return t;let o=f.join("/");return`https://raw.githubusercontent.com/${n}/${a}/${i}/${o}`}catch{return t}}async function sn(t,e={}){let r=Ye(t);if(r.blocked)return{ok:!1,url:t,finalUrl:t,status:403,error:`Blocked: ${r.reason}`,needsBrowser:!1};let n=t;if(t=kn(t),t!==n)console.error(`[fetcher] Rewrote GitHub URL: ${n.slice(0,60)}... → raw.githubusercontent.com`);let{timeoutMs:a=15000,userAgent:l,signal:i}=e,f=new AbortController,o=setTimeout(()=>f.abort(),a);if(i)i.addEventListener("abort",()=>f.abort(),{once:!0});try{let g=await fetch(t,{method:"GET",headers:{...Fe,"user-agent":l||He},redirect:"follow",signal:f.signal});clearTimeout(o);let p=g.headers.get("content-type")||"",h=g.url,m=g.headers.get("last-modified")||"",c=!1;try{c=new URL(h).hostname.toLowerCase()==="raw.githubusercontent.com"}catch{}if(p.includes("text/plain")&&c){let C=await g.text();return{ok:!0,url:n,finalUrl:h,status:g.status,title:h.split("/").pop()||"GitHub File",byline:"",siteName:"GitHub",lang:"",publishedTime:m,lastModified:m,markdown:C,contentLength:C.length,excerpt:C.slice(0,300).replaceAll(/\n/g," "),needsBrowser:!1}}if(!p.includes("text/html")&&!p.includes("application/xhtml"))return{ok:!1,url:t,finalUrl:h,status:g.status,error:`Unsupported content type: ${p}`,needsBrowser:!1};let y=await g.text(),b=Ge(g.status,y,h,t);if(b.blocked)return{ok:!1,url:t,finalUrl:h,status:g.status,error:`Blocked: ${b.reason}`,needsBrowser:!0};let w=Ve(y,h),k=de(w);if(!k.ok)return{ok:!1,url:t,finalUrl:h,status:g.status,error:`Low quality content: ${k.reason}`,needsBrowser:!0};return{ok:!0,url:t,finalUrl:h,status:g.status,title:w.title,byline:w.byline,siteName:w.siteName,lang:w.lang,publishedTime:w.publishedTime||m,lastModified:m,markdown:w.markdown,excerpt:w.excerpt,contentLength:w.markdown.length,needsBrowser:!1}}catch(g){clearTimeout(o);let p=Cn(g);return{ok:!1,url:t,finalUrl:t,status:0,error:g.message,needsBrowser:p}}}function Ge(t,e,r,n){let a=e.match(/<title[^>]*>([^<]*)<\/title>/i)?.[1]?.toLowerCase()||"",l=e.slice(0,30000).toLowerCase(),i=`${a} ${l}`;if(t===403||t===429||t===503)return{blocked:!0,reason:`HTTP ${t}`};let f=[{pattern:/class=["'][^"']*captcha["']|<div[^>]*id=["']captcha/i,reason:"captcha"},{pattern:/g-recaptcha|data-sitekey|i['"]m not a robot/i,reason:"captcha"},{pattern:/checking your browser.{0,100}please wait|cf-browser-verification/i,reason:"cloudflare challenge"},{pattern:/just a moment.{0,50}security check|ddos protection by cloudflare/i,reason:"cloudflare challenge"},{pattern:/unusual traffic.{0,50}from your computer network/i,reason:"unusual traffic"},{pattern:/bot detected|automated.{0,20}request/i,reason:"bot detection"},{pattern:/enable\s+javascript\s+to\s+view|javascript\s+is\s+required.{0,50}enabled/i,reason:"requires javascript"},{pattern:/access denied|accessdenied/i,reason:"access denied"},{pattern:/protected by anubis|anubis uses a proof-of-work/i,reason:"anubis challenge"}];for(let g of f)if(g.pattern.test(i))return{blocked:!0,reason:g.reason};let o=En(n,r,e);if(o)return{blocked:!0,reason:o};return{blocked:!1}}function En(t,e,r){try{let n=new URL(t),a=new URL(e);if(n.hostname.toLowerCase()===a.hostname.toLowerCase())return;let l=a.hostname.toLowerCase();if(vn.some((f)=>l===f||l.endsWith(`.${f}`)))return`redirected to login (${a.hostname})`;if(jn.some((f)=>l.startsWith(f)))return`redirected to login (${a.hostname})`;let i=r.slice(0,20000).toLowerCase();if(Dn.some((f)=>i.includes(f)))return`redirected to login page (${a.hostname})`}catch{}return}function Cn(t){let e=t.message.toLowerCase();return e.includes("fetch failed")||e.includes("unable to verify")||e.includes("certificate")||e.includes("timeout")}function Te(t){let e=['meta[property="article:published_time"]','meta[name="article:published_time"]','meta[property="og:published_time"]','meta[name="publication_date"]','meta[name="date"]','meta[itemprop="datePublished"]','time[itemprop="datePublished"]','meta[name="DC.date"]'];for(let r of e){let n=t.querySelector(r),a=n?.getAttribute("content")||n?.getAttribute("datetime")||"";if(a)return a}return""}function Ve(t,e){let r=new mn(t,{url:e}).window.document,n=new cn(r).parse();if(n&&n.content){let l=Je.turndown(n.content).replaceAll(/\n{3,}/g,`
2
2
 
3
- `).trim(),i=n.publishedTime||Ce(r)||"";return{title:n.title||r.title||e,byline:n.byline||"",siteName:n.siteName||"",lang:n.lang||"",publishedTime:i,markdown:l,excerpt:l.slice(0,300).replaceAll(/\n/g," ")}}let a=r.body;if(a){let l=a.cloneNode(!0);l.querySelectorAll("script, style, nav, footer, header, aside").forEach((f)=>f.remove());let i=(l.textContent||"").replaceAll(/\s+/g," ").trim();return{title:r.title||e,byline:"",siteName:"",lang:"",publishedTime:Ce(r),markdown:i,excerpt:i.slice(0,300)}}return{title:e,byline:"",siteName:"",lang:"",publishedTime:"",markdown:"",excerpt:""}}function Fe(t){let e=t.markdown.trim().toLowerCase(),r=(t.title||"").toLowerCase();if(t.markdown.trim().length<100)return{ok:!1,reason:"content too short (< 100 chars)"};let n=e.toLowerCase(),a=[{check:()=>n.includes("loading")&&n.includes("please wait"),desc:"loading page"},{check:()=>n.includes("please ensure javascript is enabled"),desc:"requires javascript"},{check:()=>n.includes("enable javascript to view"),desc:"requires javascript"},{check:()=>n.includes("just a moment"),desc:"cloudflare challenge detected in content"},{check:()=>n.includes("verify you are human"),desc:"human verification"},{check:()=>n.includes("captcha required"),desc:"captcha in extracted content"},{check:()=>n.includes("access denied"),desc:"access denied in content"},{check:()=>/^\s{0,10}sign\s{1,5}in\s{0,10}$|^\s{0,10}log\s{1,5}in\s{0,10}$/im.test(e),desc:"login form only"}];for(let{check:l,desc:i}of a)if(l())return{ok:!1,reason:i};if(r.includes("just a moment")||r.includes("checking your browser"))return{ok:!1,reason:"cloudflare challenge page detected in title"};return{ok:!0}}function Ft(t){try{let e=new URL(t);if(!(e.hostname==="github.com"||e.hostname.endsWith(".github.com")))return null;let r=e.pathname.split("/").filter(Boolean);if(r.length<2)return null;let[n,a]=r;if(r.length===2)return{owner:n,repo:a,type:"root"};if(r.length>=4&&(r[2]==="blob"||r[2]==="tree")){let l=r[2],i=r[3],f=r.slice(4).join("/");return{owner:n,repo:a,type:l,ref:i,path:f}}return null}catch{return null}}async function B(t,e=1e4){let r=new AbortController,n=setTimeout(()=>r.abort(),e);try{let a=await fetch(`https://api.github.com${t}`,{headers:Ye,signal:r.signal});if(clearTimeout(n),!a.ok)throw Error(`GitHub API ${a.status}: ${t}`);return await a.json()}catch(a){throw clearTimeout(n),a}}async function vn(t,e){try{let r=await B(`/repos/${t}/${e}/readme`);if(r.content&&r.encoding==="base64")return Buffer.from(r.content,"base64").toString("utf8");return""}catch{return""}}async function Ee(t,e,r="HEAD",n=""){try{let a=await B(`/repos/${t}/${e}/git/ref/heads/${r==="HEAD"?"main":r}`).catch(()=>B(`/repos/${t}/${e}/git/ref/heads/master`).catch(()=>null)),l;if(a?.object?.sha)l=(await B(`/repos/${t}/${e}/git/commits/${a.object.sha}`)).tree.sha;else{let f=await B(`/repos/${t}/${e}`);l=(await B(`/repos/${t}/${e}/branches/${f.default_branch}`)).commit.commit.tree.sha}let i=(await B(`/repos/${t}/${e}/git/trees/${l}`)).tree||[];if(n)i=i.filter((f)=>f.path.startsWith(n));return i.slice(0,50).map((f)=>({path:f.path,type:f.type==="tree"?"dir":"file",size:f.size}))}catch{return[]}}async function jn(t,e,r,n,a=1e4){let l=[`https://raw.githubusercontent.com/${t}/${e}/${r&&r!=="HEAD"?r:"main"}/${n}`,`https://raw.githubusercontent.com/${t}/${e}/master/${n}`];for(let i of l){let f=new AbortController,p=setTimeout(()=>f.abort(),a);try{let g=await fetch(i,{headers:{"user-agent":Ye["user-agent"]},signal:f.signal});if(clearTimeout(p),g.ok)return await g.text()}catch{clearTimeout(p)}}return null}async function Dn(t){let e=Ft(t);if(!e)return{ok:!1,error:"Not a valid GitHub URL"};let{owner:r,repo:n,type:a,ref:l,path:i}=e;try{if(a==="root"||a==="tree"&&!i){let[f,p,g]=await Promise.allSettled([B(`/repos/${r}/${n}`),vn(r,n),Ee(r,n,l||"HEAD")]);if(f.status==="rejected")return{ok:!1,error:f.reason?.message||"Repo not found"};let o=f.value,h=p.status==="fulfilled"?p.value:"",m=g.status==="fulfilled"?g.value:[],c=o?.description?`
3
+ `).trim(),i=n.publishedTime||Te(r)||"";return{title:n.title||r.title||e,byline:n.byline||"",siteName:n.siteName||"",lang:n.lang||"",publishedTime:i,markdown:l,excerpt:l.slice(0,300).replaceAll(/\n/g," ")}}let a=r.body;if(a){let l=a.cloneNode(!0);l.querySelectorAll("script, style, nav, footer, header, aside").forEach((f)=>f.remove());let i=(l.textContent||"").replaceAll(/\s+/g," ").trim();return{title:r.title||e,byline:"",siteName:"",lang:"",publishedTime:Te(r),markdown:i,excerpt:i.slice(0,300)}}return{title:e,byline:"",siteName:"",lang:"",publishedTime:"",markdown:"",excerpt:""}}function de(t){let e=t.markdown.trim().toLowerCase(),r=(t.title||"").toLowerCase();if(t.markdown.trim().length<100)return{ok:!1,reason:"content too short (< 100 chars)"};let n=e.toLowerCase(),a=[{check:()=>n.includes("loading")&&n.includes("please wait"),desc:"loading page"},{check:()=>n.includes("please ensure javascript is enabled"),desc:"requires javascript"},{check:()=>n.includes("enable javascript to view"),desc:"requires javascript"},{check:()=>n.includes("just a moment"),desc:"cloudflare challenge detected in content"},{check:()=>n.includes("verify you are human"),desc:"human verification"},{check:()=>n.includes("captcha required"),desc:"captcha in extracted content"},{check:()=>n.includes("access denied"),desc:"access denied in content"},{check:()=>/^\s{0,10}sign\s{1,5}in\s{0,10}$|^\s{0,10}log\s{1,5}in\s{0,10}$/im.test(e),desc:"login form only"}];for(let{check:l,desc:i}of a)if(l())return{ok:!1,reason:i};if(r.includes("just a moment")||r.includes("checking your browser"))return{ok:!1,reason:"cloudflare challenge page detected in title"};return{ok:!0}}function Yt(t){try{let e=new URL(t);if(!(e.hostname==="github.com"||e.hostname.endsWith(".github.com")))return null;let r=e.pathname.split("/").filter(Boolean);if(r.length<2)return null;let[n,a]=r;if(r.length===2)return{owner:n,repo:a,type:"root"};if(r.length>=4&&(r[2]==="blob"||r[2]==="tree")){let l=r[2],i=r[3],f=r.slice(4).join("/");return{owner:n,repo:a,type:l,ref:i,path:f}}return null}catch{return null}}async function K(t,e=1e4){let r=new AbortController,n=setTimeout(()=>r.abort(),e);try{let a=await fetch(`https://api.github.com${t}`,{headers:Ze,signal:r.signal});if(clearTimeout(n),!a.ok)throw Error(`GitHub API ${a.status}: ${t}`);return await a.json()}catch(a){throw clearTimeout(n),a}}async function Nn(t,e){try{let r=await K(`/repos/${t}/${e}/readme`);if(r.content&&r.encoding==="base64")return Buffer.from(r.content,"base64").toString("utf8");return""}catch{return""}}async function Oe(t,e,r="HEAD",n=""){try{let a=await K(`/repos/${t}/${e}/git/ref/heads/${r==="HEAD"?"main":r}`).catch(()=>K(`/repos/${t}/${e}/git/ref/heads/master`).catch(()=>null)),l;if(a?.object?.sha)l=(await K(`/repos/${t}/${e}/git/commits/${a.object.sha}`)).tree.sha;else{let f=await K(`/repos/${t}/${e}`);l=(await K(`/repos/${t}/${e}/branches/${f.default_branch}`)).commit.commit.tree.sha}let i=(await K(`/repos/${t}/${e}/git/trees/${l}`)).tree||[];if(n)i=i.filter((f)=>f.path.startsWith(n));return i.slice(0,50).map((f)=>({path:f.path,type:f.type==="tree"?"dir":"file",size:f.size}))}catch{return[]}}async function Tn(t,e,r,n,a=1e4){let l=[`https://raw.githubusercontent.com/${t}/${e}/${r&&r!=="HEAD"?r:"main"}/${n}`,`https://raw.githubusercontent.com/${t}/${e}/master/${n}`];for(let i of l){let f=new AbortController,o=setTimeout(()=>f.abort(),a);try{let g=await fetch(i,{headers:{"user-agent":Ze["user-agent"]},signal:f.signal});if(clearTimeout(o),g.ok)return await g.text()}catch{clearTimeout(o)}}return null}async function On(t){let e=Yt(t);if(!e)return{ok:!1,error:"Not a valid GitHub URL"};let{owner:r,repo:n,type:a,ref:l,path:i}=e;try{if(a==="root"||a==="tree"&&!i){let[f,o,g]=await Promise.allSettled([K(`/repos/${r}/${n}`),Nn(r,n),Oe(r,n,l||"HEAD")]);if(f.status==="rejected")return{ok:!1,error:f.reason?.message||"Repo not found"};let p=f.value,h=o.status==="fulfilled"?o.value:"",m=g.status==="fulfilled"?g.value:[],c=p?.description?`
4
4
 
5
- > ${o.description}`:"",y=o?.stargazers_count==null?"":` ⭐ ${o.stargazers_count}`,b=o?.language?` · ${o.language}`:"",w=`# ${r}/${n}${y}${b}${c}
5
+ > ${p.description}`:"",y=p?.stargazers_count==null?"":` ⭐ ${p.stargazers_count}`,b=p?.language?` · ${p.language}`:"",w=`# ${r}/${n}${y}${b}${c}
6
6
 
7
7
  `;if(h)w+=h.slice(0,6000);else w+=`[No README found]
8
8
 
9
9
  Files:
10
- ${m.map((_)=>` ${_.type==="dir"?"\uD83D\uDCC1":"\uD83D\uDCC4"} ${_.path}`).join(`
11
- `)}`;return{ok:!0,title:`${r}/${n}`,content:w,tree:m.slice(0,30)}}if(a==="blob"&&i){let f=await jn(r,n,l,i);if(f===null)return{ok:!1,error:`File not found: ${i}`};return{ok:!0,title:`${r}/${n}: ${i}`,content:f}}if(a==="tree"&&i){let f=await Ee(r,n,l||"HEAD",i),p=f.map((g)=>` ${g.type==="dir"?"\uD83D\uDCC1":"\uD83D\uDCC4"} ${g.path}`).join(`
10
+ ${m.map((k)=>` ${k.type==="dir"?"\uD83D\uDCC1":"\uD83D\uDCC4"} ${k.path}`).join(`
11
+ `)}`;return{ok:!0,title:`${r}/${n}`,content:w,tree:m.slice(0,30)}}if(a==="blob"&&i){let f=await Tn(r,n,l,i);if(f===null)return{ok:!1,error:`File not found: ${i}`};return{ok:!0,title:`${r}/${n}: ${i}`,content:f}}if(a==="tree"&&i){let f=await Oe(r,n,l||"HEAD",i),o=f.map((g)=>` ${g.type==="dir"?"\uD83D\uDCC1":"\uD83D\uDCC4"} ${g.path}`).join(`
12
12
  `);return{ok:!0,title:`${r}/${n}/${i}`,content:`[Directory: ${i}]
13
13
 
14
14
  Files:
15
- ${p}`,tree:f}}return{ok:!1,error:"Unsupported GitHub URL type"}}catch(f){return{ok:!1,error:f.message}}}function En(t){try{let e=new URL(t),r=e.hostname.toLowerCase();if(!(r==="reddit.com"||r.endsWith(".reddit.com")))return null;let n=e.pathname;if(n.match(/^\/(u|user)\/[^/]+\/?$/i))return{type:"user",cleanUrl:Ne(t)};if(n.match(/^\/r\/[^/]+\/comments\/[^/]+/i))return{type:"post",cleanUrl:Ne(t)};return null}catch{return null}}function Ne(t){try{let e=new URL(t);return`${e.protocol}//${e.hostname}${e.pathname}`}catch{return t}}async function Nn(t,e=8000){let r=Date.now();try{let n=t.replaceAll(/\/?$/g,".json"),a=new AbortController,l=setTimeout(()=>a.abort(),15000),i=await fetch(n,{headers:Cn,signal:a.signal});if(clearTimeout(l),!i.ok)throw Error(`Reddit API ${i.status}`);let f=await i.json();if(!Array.isArray(f)||f.length<1)throw Error("Invalid Reddit API response structure");let p=f[0],g=f[1],o=p?.data?.children?.[0]?.data;if(!o)throw Error("No post data in Reddit response");let h=sn(o,g,e);return{ok:!0,url:t,finalUrl:t,status:200,contentType:"text/markdown",lastModified:"",title:o.title||"Reddit Post",byline:`u/${o.author}`,siteName:`r/${o.subreddit}`,lang:"en",publishedTime:new Date(o.created_utc*1000).toISOString(),excerpt:o.selftext?.slice(0,300).replace(/\n/g," ")||"",markdown:h,contentLength:h.length,needsBrowser:!1,duration:Date.now()-r}}catch(n){return{ok:!1,url:t,finalUrl:t,status:0,error:`Reddit fetch failed: ${n.message}`,needsBrowser:!1,duration:Date.now()-r}}}function sn(t,e,r){let n="";if(n+=`# ${t.title}
15
+ ${o}`,tree:f}}return{ok:!1,error:"Unsupported GitHub URL type"}}catch(f){return{ok:!1,error:f.message}}}function Ln(t){try{let e=new URL(t),r=e.hostname.toLowerCase();if(!(r==="reddit.com"||r.endsWith(".reddit.com")))return null;let n=e.pathname;if(n.match(/^\/(u|user)\/[^/]+\/?$/i))return{type:"user",cleanUrl:Pe(t)};if(n.match(/^\/r\/[^/]+\/comments\/[^/]+/i))return{type:"post",cleanUrl:Pe(t)};return null}catch{return null}}function Pe(t){try{let e=new URL(t);return`${e.protocol}//${e.hostname}${e.pathname}`}catch{return t}}async function Wn(t,e=8000){let r=Date.now();try{let n=t.replaceAll(/\/?$/g,".json"),a=new AbortController,l=setTimeout(()=>a.abort(),15000),i=await fetch(n,{headers:Pn,signal:a.signal});if(clearTimeout(l),!i.ok)throw Error(`Reddit API ${i.status}`);let f=await i.json();if(!Array.isArray(f)||f.length<1)throw Error("Invalid Reddit API response structure");let o=f[0],g=f[1],p=o?.data?.children?.[0]?.data;if(!p)throw Error("No post data in Reddit response");let h=Mn(p,g,e);return{ok:!0,url:t,finalUrl:t,status:200,contentType:"text/markdown",lastModified:"",title:p.title||"Reddit Post",byline:`u/${p.author}`,siteName:`r/${p.subreddit}`,lang:"en",publishedTime:new Date(p.created_utc*1000).toISOString(),excerpt:p.selftext?.slice(0,300).replace(/\n/g," ")||"",markdown:h,contentLength:h.length,needsBrowser:!1,duration:Date.now()-r}}catch(n){return{ok:!1,url:t,finalUrl:t,status:0,error:`Reddit fetch failed: ${n.message}`,needsBrowser:!1,duration:Date.now()-r}}}function Mn(t,e,r){let n="";if(n+=`# ${t.title}
16
16
 
17
17
  `,n+=`**Subreddit:** r/${t.subreddit} | **Author:** u/${t.author} | **Score:** ${t.score}
18
18
 
@@ -26,21 +26,21 @@ ${p}`,tree:f}}return{ok:!1,error:"Unsupported GitHub URL type"}}catch(f){return{
26
26
 
27
27
  ## Comments
28
28
 
29
- `;let a=e.data.children.filter((l)=>l.kind==="t1").slice(0,10);for(let l of a)n+=Ve(l.data,0),n+=`
29
+ `;let a=e.data.children.filter((l)=>l.kind==="t1").slice(0,10);for(let l of a)n+=Be(l.data,0),n+=`
30
30
  `}if(n.length>r)n=n.slice(0,r).trim()+`
31
31
 
32
- ... (truncated)`;return n}function Ve(t,e){if(!t||t.body==="[deleted]"||t.body==="[removed]")return"";let r="> ".repeat(e),n="";if(n+=`${r}**u/${t.author}** (${t.score} pts)
32
+ ... (truncated)`;return n}function Be(t,e){if(!t||t.body==="[deleted]"||t.body==="[removed]")return"";let r="> ".repeat(e),n="";if(n+=`${r}**u/${t.author}** (${t.score} pts)
33
33
  `,n+=`${r}${t.body.replaceAll(`
34
34
  `,`
35
35
  `+r)}
36
36
  `,e<3&&t.replies?.data?.children){let a=t.replies.data.children.filter((l)=>l.kind==="t1");for(let l of a.slice(0,5))n+=`
37
- `+Ve(l.data,e+1)}return n}function q(t,e=8000){if(!t||t.length<=e)return t;let r=`
37
+ `+Be(l.data,e+1)}return n}function q(t,e=8000){if(!t||t.length<=e)return t;let r=`
38
38
 
39
39
  [...content trimmed...]
40
40
 
41
41
  `,n=e-r.length,a=Math.floor(n*0.75),l=n-a,i=a;while(i>a-100&&t[i]!==`
42
42
  `)i--;if(i<=a-100)i=a;let f=t.length-l;while(f<t.length-l+100&&t[f]!==`
43
- `)f++;if(f>=t.length-l+100)f=t.length-l;let p=t.slice(0,i).trimEnd(),g=t.slice(f).trimStart();return`${p}${r}${g}`}function Gn(t=process.env,e=process.execPath){let r=t.GREEDY_SEARCH_NODE||t.NODE_BINARY||t.NODE;if(r?.trim())return r.trim();let n=Mn(e||"").toLowerCase();if(n==="node"||n==="node.exe")return e;return"node"}function Be(t,e=30000){return dn(t,null,e)}function dn(t,e=null,r=30000){return new Promise((n,a)=>{let l=Tn(Gn(),[Zn,...t],{stdio:[e==null?"ignore":"pipe","pipe","pipe"]});if(e!=null)l.stdin.write(e),l.stdin.end();let i="",f="";l.stdout.on("data",(g)=>i+=g),l.stderr.on("data",(g)=>f+=g);let p=setTimeout(()=>{l.kill(),a(Error(`cdp timeout: ${t[0]}`))},r);l.on("close",(g)=>{if(clearTimeout(p),g===0)n(i.trim());else a(Error(f.trim()||`cdp exit ${g}`))})})}async function se(t){await Be(["evalraw",t,"Page.addScriptToEvaluateOnNewDocument",JSON.stringify({source:`
43
+ `)f++;if(f>=t.length-l+100)f=t.length-l;let o=t.slice(0,i).trimEnd(),g=t.slice(f).trimStart();return`${o}${r}${g}`}function In(t=process.env,e=process.execPath){let r=t.GREEDY_SEARCH_NODE||t.NODE_BINARY||t.NODE;if(r?.trim())return r.trim();let n=Yn(e||"").toLowerCase();if(n==="node"||n==="node.exe")return e;return"node"}function xe(t,e=30000){return Qn(t,null,e)}function Qn(t,e=null,r=30000){return new Promise((n,a)=>{let l=Rn(In(),[xn,...t],{stdio:[e==null?"ignore":"pipe","pipe","pipe"]});if(e!=null)l.stdin.write(e),l.stdin.end();let i="",f="";l.stdout.on("data",(g)=>i+=g),l.stderr.on("data",(g)=>f+=g);let o=setTimeout(()=>{l.kill(),a(Error(`cdp timeout: ${t[0]}`))},r);l.on("close",(g)=>{if(clearTimeout(o),g===0)n(i.trim());else a(Error(f.trim()||`cdp exit ${g}`))})})}async function Le(t){await xe(["evalraw",t,"Page.addScriptToEvaluateOnNewDocument",JSON.stringify({source:`
44
44
  (function() {
45
45
  // ── Runtime.enable / CDP detection masking ──────────────
46
46
  try { delete window.__REBROWSER_RUNTIME_ENABLE; } catch(_) {}
@@ -248,35 +248,35 @@ ${p}`,tree:f}}return{ok:!1,error:"Unsupported GitHub URL type"}}catch(f){return{
248
248
  };
249
249
  } catch(_) {}
250
250
  })();
251
- `})])}function Bn(){try{if($t(G)){let t=Ge(G,"utf8"),e=JSON.parse(t);if(Array.isArray(e.engines)&&e.engines.length>0&&e.engines.every((r)=>typeof r==="string")){let r=e.engines.filter((a)=>Jt[a]),n=e.engines.filter((a)=>!Jt[a]);if(n.length>0)process.stderr.write(`[greedysearch] Warning: ignoring unknown engine(s) in ${G}: ${n.join(", ")}
251
+ `})])}function zn(){try{if(ct(V)){let t=Ke(V,"utf8"),e=JSON.parse(t);if(Array.isArray(e.engines)&&e.engines.length>0&&e.engines.every((r)=>typeof r==="string")){let r=e.engines.filter((a)=>Jt[a]),n=e.engines.filter((a)=>!Jt[a]);if(n.length>0)process.stderr.write(`[greedysearch] Warning: ignoring unknown engine(s) in ${V}: ${n.join(", ")}
252
252
  [greedysearch] Available engines: ${Object.keys(Jt).join(", ")}
253
- `);if(r.length>0)return r;process.stderr.write(`[greedysearch] Warning: no valid engines in ${G}, falling back to defaults: ${Vt.join(", ")}
254
- `)}}}catch{}return Vt}function Un(){try{if(!$t(Yt))An(Yt,{recursive:!0});if(!$t(G))Jn(G,JSON.stringify({engines:Vt,synthesizer:Gt},null,2)+`
255
- `,"utf8")}catch{}}function In(){try{if($t(G)){let t=Ge(G,"utf8"),e=JSON.parse(t);if(typeof e.synthesizer==="string"){let r=e.synthesizer.toLowerCase();if(Oe.includes(r))return r;process.stderr.write(`[greedysearch] Warning: unknown synthesizer "${e.synthesizer}" in ${G}
256
- [greedysearch] Available synthesizers: ${Oe.join(", ")}
257
- [greedysearch] Falling back to default: ${Gt}
258
- `)}}}catch{}return Gt}function Xn(){try{if(ct(K)){let t=Ze(K,"utf8"),e=JSON.parse(t);if(Array.isArray(e.engines)&&e.engines.length>0&&e.engines.every((r)=>typeof r==="string")){let r=e.engines.filter((a)=>Rt[a]),n=e.engines.filter((a)=>!Rt[a]);if(n.length>0)process.stderr.write(`[greedysearch] Warning: ignoring unknown engine(s) in ${K}: ${n.join(", ")}
259
- [greedysearch] Available engines: ${Object.keys(Rt).join(", ")}
260
- `);if(r.length>0)return r;process.stderr.write(`[greedysearch] Warning: no valid engines in ${K}, falling back to defaults: ${Zt.join(", ")}
261
- `)}}}catch{}return Zt}function xn(){try{if(!ct(Kt))Fn(Kt,{recursive:!0});if(!ct(K))Yn(K,JSON.stringify({engines:Zt,synthesizer:dt},null,2)+`
262
- `,"utf8")}catch{}}function Qn(){try{if(ct(K)){let t=Ze(K,"utf8"),e=JSON.parse(t);if(typeof e.synthesizer==="string"){let r=e.synthesizer.toLowerCase();if(Te.includes(r))return r;process.stderr.write(`[greedysearch] Warning: unknown synthesizer "${e.synthesizer}" in ${K}
263
- [greedysearch] Available synthesizers: ${Te.join(", ")}
253
+ `);if(r.length>0)return r;process.stderr.write(`[greedysearch] Warning: no valid engines in ${V}, falling back to defaults: ${Vt.join(", ")}
254
+ `)}}}catch{}return Vt}function Sn(){try{if(!ct(Gt))Gn(Gt,{recursive:!0});if(!ct(V))Vn(V,JSON.stringify({engines:Vt,synthesizer:dt},null,2)+`
255
+ `,"utf8")}catch{}}function un(){try{if(ct(V)){let t=Ke(V,"utf8"),e=JSON.parse(t);if(typeof e.synthesizer==="string"){let r=e.synthesizer.toLowerCase();if(We.includes(r))return r;process.stderr.write(`[greedysearch] Warning: unknown synthesizer "${e.synthesizer}" in ${V}
256
+ [greedysearch] Available synthesizers: ${We.join(", ")}
264
257
  [greedysearch] Falling back to default: ${dt}
265
- `)}}}catch{}return dt}async function xe(){let t=(await P(["list"])).split(`
266
- `)[0];if(!t)throw Error("No Chrome tabs found");return t.slice(0,8)}async function Xt(t="about:blank"){let e=await xe(),r=new URL(t).hostname;if(r==="copilot.microsoft.com"||r==="www.perplexity.ai"||r==="perplexity.ai"||r.endsWith(".perplexity.ai")){let l=await P(["evalraw",e,"Target.createTarget",JSON.stringify({url:"about:blank"})]),{targetId:i}=JSON.parse(l),f=i.slice(0,8);if(await P(["list"]).catch(()=>null),r==="copilot.microsoft.com")await se(f);else se(f).catch(()=>{});return await P(["list"]).catch(()=>null),i}let n=await P(["evalraw",e,"Target.createTarget",JSON.stringify({url:t})]),{targetId:a}=JSON.parse(n);return await P(["list"]).catch(()=>null),a}async function xt(t){try{let e=await xe();await P(["evalraw",e,"Target.closeTarget",JSON.stringify({targetId:t})])}catch{}}function qn(){try{if(mt(Z)){let t=Qe(Z,"utf8"),e=JSON.parse(t);if(Array.isArray(e.engines)&&e.engines.length>0&&e.engines.every((r)=>typeof r==="string")){let r=e.engines.filter((a)=>Ht[a]),n=e.engines.filter((a)=>!Ht[a]);if(n.length>0)process.stderr.write(`[greedysearch] Warning: ignoring unknown engine(s) in ${Z}: ${n.join(", ")}
258
+ `)}}}catch{}return dt}function qn(){try{if(mt(d)){let t=Ie(d,"utf8"),e=JSON.parse(t);if(Array.isArray(e.engines)&&e.engines.length>0&&e.engines.every((r)=>typeof r==="string")){let r=e.engines.filter((a)=>Ht[a]),n=e.engines.filter((a)=>!Ht[a]);if(n.length>0)process.stderr.write(`[greedysearch] Warning: ignoring unknown engine(s) in ${d}: ${n.join(", ")}
267
259
  [greedysearch] Available engines: ${Object.keys(Ht).join(", ")}
268
- `);if(r.length>0)return r;process.stderr.write(`[greedysearch] Warning: no valid engines in ${Z}, falling back to defaults: ${Ut.join(", ")}
269
- `)}}}catch{}return Ut}function ta(){try{if(!mt(Bt))zn(Bt,{recursive:!0});if(!mt(Z))Sn(Z,JSON.stringify({engines:Ut,synthesizer:It},null,2)+`
270
- `,"utf8")}catch{}}function ea(){try{if(mt(Z)){let t=Qe(Z,"utf8"),e=JSON.parse(t);if(typeof e.synthesizer==="string"){let r=e.synthesizer.toLowerCase();if(Pe.includes(r))return r;process.stderr.write(`[greedysearch] Warning: unknown synthesizer "${e.synthesizer}" in ${Z}
271
- [greedysearch] Available synthesizers: ${Pe.join(", ")}
272
- [greedysearch] Falling back to default: ${It}
273
- `)}}}catch{}return It}function ra(){if(typeof globalThis.DOMMatrix>"u")globalThis.DOMMatrix=class{constructor(t=void 0){}multiplySelf(){return this}preMultiplySelf(){return this}translateSelf(){return this}scaleSelf(){return this}rotateSelf(){return this}};if(typeof globalThis.ImageData>"u")globalThis.ImageData=class{constructor(t=void 0,e=0,r=0){this.data=t,this.width=e,this.height=r}};if(typeof globalThis.Path2D>"u")globalThis.Path2D=class{constructor(t=void 0){}}}async function na(){ra();let t=await import("pdf-parse"),e=t.PDFParse??t.default;if(!e)throw Error("pdf-parse did not export PDFParse");return e}async function aa(t,e){try{let r=new(await na())({data:new Uint8Array(t)});await r.load();let n=await r.getText(),a=n.text?.trim();if(!a)return null;return{title:new URL(e).pathname.split("/").pop()||"Document.pdf",content:`## PDF Content (${n.total} pages)
274
-
275
- ${a}`,pages:n.total}}catch(r){return{error:r.message||String(r)}}}function ue(t="",e=240){let r=String(t).replaceAll(/\s+/g," ").trim();if(r.length<=e)return r;let n=r.slice(0,e),a=n.lastIndexOf(" ");return a>0?`${n.slice(0,a)}...`:`${n}...`}async function la(t,e,r=8000){let n=Date.now();try{let a=(await P(["evalraw",t,"Page.getFrameTree","{}"]).then((m)=>JSON.parse(m)).catch(()=>null))?.frameTree?.frame?.id||void 0,l=await P(["evalraw",t,"Network.loadNetworkResource",JSON.stringify({frameId:a,url:e,options:{disableCache:!0,includeCredentials:!1}})],20000),i=JSON.parse(l).resource;if(!i?.success||!i.httpStatusCode)return{url:e,error:i?.netErrorName||i?.netError||"loadNetworkResource failed",source:"chrome",duration:Date.now()-n,needsFallback:!0};let f="";if(i.stream)try{let m=await P(["evalraw",t,"IO.read",JSON.stringify({handle:i.stream})],1e4);f=JSON.parse(m).data||"",await P(["evalraw",t,"IO.close",JSON.stringify({handle:i.stream})]).catch(()=>{})}catch{}if(!f||f.length<100)return{url:e,error:"Empty response body from Network.loadNetworkResource",source:"chrome",duration:Date.now()-n,needsFallback:!0};let p=Re(i.httpStatusCode,f,e,e);if(p.blocked)return{url:e,status:i.httpStatusCode,error:`Blocked: ${p.reason}`,source:"chrome",duration:Date.now()-n,needsBrowser:!0};let g=He(f,e),o=Fe(g);if(!o.ok)return{url:e,status:i.httpStatusCode,error:`Low quality: ${o.reason}`,source:"chrome",duration:Date.now()-n,needsBrowser:!0};let h=q(g.markdown,r);return{url:e,finalUrl:e,status:i.httpStatusCode,contentType:"text/markdown",lastModified:"",publishedTime:g.publishedTime||"",byline:g.byline||"",siteName:g.siteName||"",lang:g.lang||"",title:g.title||e,snippet:g.excerpt,content:h,contentChars:h.length,source:"chrome",duration:Date.now()-n}}catch(a){return{url:e,error:a.message,source:"chrome",duration:Date.now()-n,needsFallback:!0}}}function qe(t){try{return new URL(t).pathname.toLowerCase().endsWith(".pdf")}catch{return!1}}async function ia(t,e=8000){let r=Je(t);if(r.blocked)return{url:t,finalUrl:t,status:403,error:`Blocked: ${r.reason}`,source:"pdf-http"};let n=new AbortController,a=setTimeout(()=>n.abort(),20000),l=Date.now();try{let i=await fetch(t,{method:"GET",redirect:"follow",signal:n.signal,headers:wn({accept:"application/pdf,application/octet-stream;q=0.9,*/*;q=0.5"})});clearTimeout(a);let f=i.headers.get("content-type")||"",p=i.url||t,g=Number.parseInt(i.headers.get("content-length")||"0",10);if(i.status>=400)return{url:t,finalUrl:p,status:i.status,error:`HTTP ${i.status}`,source:"pdf-http",duration:Date.now()-l};if(!f.toLowerCase().includes("application/pdf")&&!qe(p))return null;if(g>31457280)return{url:t,finalUrl:p,status:i.status,error:`PDF too large: ${g} bytes`,source:"pdf-http",duration:Date.now()-l};let o=Buffer.from(await i.arrayBuffer()),h=await aa(o,p);if(!h||h.error)return{url:t,finalUrl:p,status:i.status,error:h?.error||"PDF text extraction failed",source:"pdf-http",duration:Date.now()-l};let m=q(h.content,e);return{url:t,finalUrl:p,status:i.status,contentType:"application/pdf",lastModified:i.headers.get("last-modified")||"",title:h.title,snippet:ue(m,320),content:m,contentChars:m.length,pages:h.pages,source:"pdf-http",duration:Date.now()-l}}catch(i){return clearTimeout(a),{url:t,finalUrl:t,error:i.message||String(i),source:"pdf-http",duration:Date.now()-l}}}async function tr(t,e=8000){let r=Date.now();if(qe(t)){let a=await ia(t,e);if(a?.content||a?.status===403)return a}if(Ft(t)){let a=Ft(t);if(a&&(a.type==="root"||a.type==="tree"||a.type==="blob"&&!a.path?.includes("."))){let l=await Dn(t);if(l.ok){let i=q(l.content,e);return{url:t,finalUrl:t,status:200,contentType:"text/markdown",lastModified:"",title:l.title,snippet:i.slice(0,320),content:i,contentChars:i.length,source:"github-api",...l.tree&&{tree:l.tree},duration:Date.now()-r}}process.stderr.write(`[greedysearch] GitHub API fetch failed, trying HTTP: ${l.error}
276
- `)}}if(En(t)?.type==="post"){process.stderr.write(`[greedysearch] Using Reddit JSON API for: ${t.slice(0,60)}...
277
- `);let a=await Nn(t,e);if(a.ok){let l=q(a.markdown,e);return{url:t,finalUrl:a.finalUrl,status:a.status,contentType:"text/markdown",lastModified:a.lastModified||"",publishedTime:a.publishedTime||"",byline:a.byline||"",siteName:a.siteName||"",lang:a.lang||"",title:a.title,snippet:a.excerpt,content:l,contentChars:l.length,source:"reddit-api",duration:Date.now()-r}}process.stderr.write(`[greedysearch] Reddit API fetch failed, falling back to HTTP: ${a.error}
278
- `)}let n=await cn(t,{timeoutMs:1e4});if(n.ok){let a=q(n.markdown,e);return{url:t,finalUrl:n.finalUrl,status:n.status,contentType:"text/markdown",lastModified:n.lastModified||"",publishedTime:n.publishedTime||"",byline:n.byline||"",siteName:n.siteName||"",lang:n.lang||"",title:n.title,snippet:n.excerpt,content:a,contentChars:a.length,source:"http",duration:Date.now()-r}}if(n.needsBrowser)try{let a=await Xt();try{let l=await la(a,t,e);if(l.content&&l.content.length>100)return l}finally{await xt(a)}}catch{}return process.stderr.write(`[greedysearch] HTTP failed for ${t.slice(0,60)}, trying browser...
279
- `),await fa(t,e)}async function fa(t,e=8000){let r=Date.now(),n;try{n=await Xt()}catch(a){return{url:t,title:"",content:null,snippet:"",contentChars:0,error:`openNewTab failed: ${a.message}`,source:"browser",duration:Date.now()-r}}try{await P(["nav",n,t],30000),await new Promise((f)=>setTimeout(f,800));let a=await P(["eval",n,String.raw`
260
+ `);if(r.length>0)return r;process.stderr.write(`[greedysearch] Warning: no valid engines in ${d}, falling back to defaults: ${Bt.join(", ")}
261
+ `)}}}catch{}return Bt}function ta(){try{if(!mt(Zt))Bn(Zt,{recursive:!0});if(!mt(d))Kn(d,JSON.stringify({engines:Bt,synthesizer:Kt},null,2)+`
262
+ `,"utf8")}catch{}}function ea(){try{if(mt(d)){let t=Ie(d,"utf8"),e=JSON.parse(t);if(typeof e.synthesizer==="string"){let r=e.synthesizer.toLowerCase();if(Me.includes(r))return r;process.stderr.write(`[greedysearch] Warning: unknown synthesizer "${e.synthesizer}" in ${d}
263
+ [greedysearch] Available synthesizers: ${Me.join(", ")}
264
+ [greedysearch] Falling back to default: ${Kt}
265
+ `)}}}catch{}return Kt}async function ue(){let t=(await P(["list"])).split(`
266
+ `)[0];if(!t)throw Error("No Chrome tabs found");return t.slice(0,8)}async function xt(t="about:blank"){let e=await ue(),r=new URL(t).hostname;if(r==="copilot.microsoft.com"||r==="www.perplexity.ai"||r==="perplexity.ai"||r.endsWith(".perplexity.ai")){let l=await P(["evalraw",e,"Target.createTarget",JSON.stringify({url:"about:blank"})]),{targetId:i}=JSON.parse(l),f=i.slice(0,8);if(await P(["list"]).catch(()=>null),r==="copilot.microsoft.com")await Le(f);else Le(f).catch(()=>{});return await P(["list"]).catch(()=>null),i}let n=await P(["evalraw",e,"Target.createTarget",JSON.stringify({url:t})]),{targetId:a}=JSON.parse(n);return await P(["list"]).catch(()=>null),a}async function Qt(t){try{let e=await ue();await P(["evalraw",e,"Target.closeTarget",JSON.stringify({targetId:t})])}catch{}}function la(){try{if(bt(Z)){let t=qe(Z,"utf8"),e=JSON.parse(t);if(Array.isArray(e.engines)&&e.engines.length>0&&e.engines.every((r)=>typeof r==="string")){let r=e.engines.filter((a)=>Ft[a]),n=e.engines.filter((a)=>!Ft[a]);if(n.length>0)process.stderr.write(`[greedysearch] Warning: ignoring unknown engine(s) in ${Z}: ${n.join(", ")}
267
+ [greedysearch] Available engines: ${Object.keys(Ft).join(", ")}
268
+ `);if(r.length>0)return r;process.stderr.write(`[greedysearch] Warning: no valid engines in ${Z}, falling back to defaults: ${It.join(", ")}
269
+ `)}}}catch{}return It}function ia(){try{if(!bt(Ut))ra(Ut,{recursive:!0});if(!bt(Z))na(Z,JSON.stringify({engines:It,synthesizer:Xt},null,2)+`
270
+ `,"utf8")}catch{}}function fa(){try{if(bt(Z)){let t=qe(Z,"utf8"),e=JSON.parse(t);if(typeof e.synthesizer==="string"){let r=e.synthesizer.toLowerCase();if(Ae.includes(r))return r;process.stderr.write(`[greedysearch] Warning: unknown synthesizer "${e.synthesizer}" in ${Z}
271
+ [greedysearch] Available synthesizers: ${Ae.join(", ")}
272
+ [greedysearch] Falling back to default: ${Xt}
273
+ `)}}}catch{}return Xt}function ga(){if(typeof globalThis.DOMMatrix>"u")globalThis.DOMMatrix=class{constructor(t=void 0){}multiplySelf(){return this}preMultiplySelf(){return this}translateSelf(){return this}scaleSelf(){return this}rotateSelf(){return this}};if(typeof globalThis.ImageData>"u")globalThis.ImageData=class{constructor(t=void 0,e=0,r=0){this.data=t,this.width=e,this.height=r}};if(typeof globalThis.Path2D>"u")globalThis.Path2D=class{constructor(t=void 0){}}}async function oa(){ga();let t=await import("pdf-parse"),e=t.PDFParse??t.default;if(!e)throw Error("pdf-parse did not export PDFParse");return e}async function pa(t,e){try{let r=new(await oa())({data:new Uint8Array(t)});await r.load();let n=await r.getText(),a=n.text?.trim();if(!a)return null;return{title:new URL(e).pathname.split("/").pop()||"Document.pdf",content:`## PDF Content (${n.total} pages)
274
+
275
+ ${a}`,pages:n.total}}catch(r){return{error:r.message||String(r)}}}function rr(t="",e=240){let r=String(t).replaceAll(/\s+/g," ").trim();if(r.length<=e)return r;let n=r.slice(0,e),a=n.lastIndexOf(" ");return a>0?`${n.slice(0,a)}...`:`${n}...`}async function ha(t,e,r=8000){let n=Date.now();try{let a=(await P(["evalraw",t,"Page.getFrameTree","{}"]).then((m)=>JSON.parse(m)).catch(()=>null))?.frameTree?.frame?.id||void 0,l=await P(["evalraw",t,"Network.loadNetworkResource",JSON.stringify({frameId:a,url:e,options:{disableCache:!0,includeCredentials:!1}})],20000),i=JSON.parse(l).resource;if(!i?.success||!i.httpStatusCode)return{url:e,error:i?.netErrorName||i?.netError||"loadNetworkResource failed",source:"chrome",duration:Date.now()-n,needsFallback:!0};let f="";if(i.stream)try{let m=await P(["evalraw",t,"IO.read",JSON.stringify({handle:i.stream})],1e4);f=JSON.parse(m).data||"",await P(["evalraw",t,"IO.close",JSON.stringify({handle:i.stream})]).catch(()=>{})}catch{}if(!f||f.length<100)return{url:e,error:"Empty response body from Network.loadNetworkResource",source:"chrome",duration:Date.now()-n,needsFallback:!0};let o=Ge(i.httpStatusCode,f,e,e);if(o.blocked)return{url:e,status:i.httpStatusCode,error:`Blocked: ${o.reason}`,source:"chrome",duration:Date.now()-n,needsBrowser:!0};let g=Ve(f,e),p=de(g);if(!p.ok)return{url:e,status:i.httpStatusCode,error:`Low quality: ${p.reason}`,source:"chrome",duration:Date.now()-n,needsBrowser:!0};let h=q(g.markdown,r);return{url:e,finalUrl:e,status:i.httpStatusCode,contentType:"text/markdown",lastModified:"",publishedTime:g.publishedTime||"",byline:g.byline||"",siteName:g.siteName||"",lang:g.lang||"",title:g.title||e,snippet:g.excerpt,content:h,contentChars:h.length,source:"chrome",duration:Date.now()-n}}catch(a){return{url:e,error:a.message,source:"chrome",duration:Date.now()-n,needsFallback:!0}}}function nr(t){try{return new URL(t).pathname.toLowerCase().endsWith(".pdf")}catch{return!1}}async function wa(t,e=8000){let r=Ye(t);if(r.blocked)return{url:t,finalUrl:t,status:403,error:`Blocked: ${r.reason}`,source:"pdf-http"};let n=new AbortController,a=setTimeout(()=>n.abort(),20000),l=Date.now();try{let i=await fetch(t,{method:"GET",redirect:"follow",signal:n.signal,headers:_n({accept:"application/pdf,application/octet-stream;q=0.9,*/*;q=0.5"})});clearTimeout(a);let f=i.headers.get("content-type")||"",o=i.url||t,g=Number.parseInt(i.headers.get("content-length")||"0",10);if(i.status>=400)return{url:t,finalUrl:o,status:i.status,error:`HTTP ${i.status}`,source:"pdf-http",duration:Date.now()-l};if(!f.toLowerCase().includes("application/pdf")&&!nr(o))return null;if(g>31457280)return{url:t,finalUrl:o,status:i.status,error:`PDF too large: ${g} bytes`,source:"pdf-http",duration:Date.now()-l};let p=Buffer.from(await i.arrayBuffer()),h=await pa(p,o);if(!h||h.error)return{url:t,finalUrl:o,status:i.status,error:h?.error||"PDF text extraction failed",source:"pdf-http",duration:Date.now()-l};let m=q(h.content,e);return{url:t,finalUrl:o,status:i.status,contentType:"application/pdf",lastModified:i.headers.get("last-modified")||"",title:h.title,snippet:rr(m,320),content:m,contentChars:m.length,pages:h.pages,source:"pdf-http",duration:Date.now()-l}}catch(i){return clearTimeout(a),{url:t,finalUrl:t,error:i.message||String(i),source:"pdf-http",duration:Date.now()-l}}}async function ar(t,e=8000){let r=Date.now();if(nr(t)){let a=await wa(t,e);if(a?.content||a?.status===403)return a}if(Yt(t)){let a=Yt(t);if(a&&(a.type==="root"||a.type==="tree"||a.type==="blob"&&!a.path?.includes("."))){let l=await On(t);if(l.ok){let i=q(l.content,e);return{url:t,finalUrl:t,status:200,contentType:"text/markdown",lastModified:"",title:l.title,snippet:i.slice(0,320),content:i,contentChars:i.length,source:"github-api",...l.tree&&{tree:l.tree},duration:Date.now()-r}}process.stderr.write(`[greedysearch] GitHub API fetch failed, trying HTTP: ${l.error}
276
+ `)}}if(Ln(t)?.type==="post"){process.stderr.write(`[greedysearch] Using Reddit JSON API for: ${t.slice(0,60)}...
277
+ `);let a=await Wn(t,e);if(a.ok){let l=q(a.markdown,e);return{url:t,finalUrl:a.finalUrl,status:a.status,contentType:"text/markdown",lastModified:a.lastModified||"",publishedTime:a.publishedTime||"",byline:a.byline||"",siteName:a.siteName||"",lang:a.lang||"",title:a.title,snippet:a.excerpt,content:l,contentChars:l.length,source:"reddit-api",duration:Date.now()-r}}process.stderr.write(`[greedysearch] Reddit API fetch failed, falling back to HTTP: ${a.error}
278
+ `)}let n=await sn(t,{timeoutMs:1e4});if(n.ok){let a=q(n.markdown,e);return{url:t,finalUrl:n.finalUrl,status:n.status,contentType:"text/markdown",lastModified:n.lastModified||"",publishedTime:n.publishedTime||"",byline:n.byline||"",siteName:n.siteName||"",lang:n.lang||"",title:n.title,snippet:n.excerpt,content:a,contentChars:a.length,source:"http",duration:Date.now()-r}}if(n.needsBrowser)try{let a=await xt();try{let l=await ha(a,t,e);if(l.content&&l.content.length>100)return l}finally{await Qt(a)}}catch{}return process.stderr.write(`[greedysearch] HTTP failed for ${t.slice(0,60)}, trying browser...
279
+ `),await $a(t,e)}async function $a(t,e=8000){let r=Date.now(),n;try{n=await xt()}catch(a){return{url:t,title:"",content:null,snippet:"",contentChars:0,error:`openNewTab failed: ${a.message}`,source:"browser",duration:Date.now()-r}}try{await P(["nav",n,t],30000),await new Promise((f)=>setTimeout(f,800));let a=await P(["eval",n,String.raw`
280
280
  (function(){
281
281
  var el = document.querySelector('article, [role="main"], main, .post-content, .article-body, #content, .content');
282
282
  var text = (el || document.body).innerText;
@@ -286,19 +286,19 @@ ${a}`,pages:n.total}}catch(r){return{error:r.message||String(r)}}}function ue(t=
286
286
  url: location.href
287
287
  });
288
288
  })()
289
- `]),l=JSON.parse(a),i=q(l.content,e);return{url:t,finalUrl:l.url||t,status:200,contentType:"text/plain",lastModified:"",title:l.title,snippet:ue(i,320),content:i,contentChars:i.length,source:"browser",duration:Date.now()-r}}catch(a){return{url:t,title:"",content:null,snippet:"",contentChars:0,error:a.message,source:"browser",duration:Date.now()-r}}finally{await xt(n)}}async function ga(t,e=5,r=8000,n=Le){let a=t.slice(0,e);if(a.length===0)return[];let l=Math.min(a.length,Math.max(1,Number.parseInt(String(n),10)||Le));process.stderr.write(`[greedysearch] Fetching content from ${a.length} sources via HTTP (concurrency ${l})...
290
- `);let i=Array(a.length),f=0,p=0;async function g(){while(!0){let c=f++;if(c>=a.length)return;let y=a[c],b=y.canonicalUrl||y.url;process.stderr.write(`[greedysearch] [${c+1}/${a.length}] Fetching: ${b.slice(0,60)}...
291
- `);let w=await tr(b,r).catch((_)=>({url:b,title:"",content:null,snippet:"",contentChars:0,error:_.message,source:"error",duration:0}));if(i[c]={id:y.id,...w},w.content&&w.content.length>100)process.stderr.write(`[greedysearch] ✓ ${w.source}: ${w.content.length} chars
289
+ `]),l=JSON.parse(a),i=q(l.content,e);return{url:t,finalUrl:l.url||t,status:200,contentType:"text/plain",lastModified:"",title:l.title,snippet:rr(i,320),content:i,contentChars:i.length,source:"browser",duration:Date.now()-r}}catch(a){return{url:t,title:"",content:null,snippet:"",contentChars:0,error:a.message,source:"browser",duration:Date.now()-r}}finally{await Qt(n)}}async function ca(t,e=5,r=8000,n=Re){let a=t.slice(0,e);if(a.length===0)return[];let l=Math.min(a.length,Math.max(1,Number.parseInt(String(n),10)||Re));process.stderr.write(`[greedysearch] Fetching content from ${a.length} sources via HTTP (concurrency ${l})...
290
+ `);let i=Array(a.length),f=0,o=0;async function g(){while(!0){let c=f++;if(c>=a.length)return;let y=a[c],b=y.canonicalUrl||y.url;process.stderr.write(`[greedysearch] [${c+1}/${a.length}] Fetching: ${b.slice(0,60)}...
291
+ `);let w=await ar(b,r).catch((k)=>({url:b,title:"",content:null,snippet:"",contentChars:0,error:k.message,source:"error",duration:0}));if(i[c]={id:y.id,...w},w.content&&w.content.length>100)process.stderr.write(`[greedysearch] ✓ ${w.source}: ${w.content.length} chars
292
292
  `);else if(w.error)process.stderr.write(`[greedysearch] ✗ ${w.error.slice(0,80)}
293
- `);p+=1,process.stderr.write(`PROGRESS:fetch:${p}/${a.length}
294
- `)}}await Promise.all(Array.from({length:l},()=>g()));let o=i.filter((c)=>c.content&&c.content.length>100),h=i.filter((c)=>c.source==="http").length,m=i.filter((c)=>c.source==="browser").length;return process.stderr.write(`[greedysearch] Fetched ${o.length}/${i.length} sources (HTTP: ${h}, Browser: ${m})
295
- `),i}async function pa(t){let e=await Xt();await P(["list"]);try{await P(["nav",e,t],30000),await new Promise((n)=>setTimeout(n,800));let r=await P(["eval",e,String.raw`
293
+ `);o+=1,process.stderr.write(`PROGRESS:fetch:${o}/${a.length}
294
+ `)}}await Promise.all(Array.from({length:l},()=>g()));let p=i.filter((c)=>c.content&&c.content.length>100),h=i.filter((c)=>c.source==="http").length,m=i.filter((c)=>c.source==="browser").length;return process.stderr.write(`[greedysearch] Fetched ${p.length}/${i.length} sources (HTTP: ${h}, Browser: ${m})
295
+ `),i}async function ma(t){let e=await xt();await P(["list"]);try{await P(["nav",e,t],30000),await new Promise((n)=>setTimeout(n,800));let r=await P(["eval",e,String.raw`
296
296
  (function(){
297
297
  var el = document.querySelector('article, [role="main"], main, .post-content, .article-body, #content, .content');
298
298
  var text = (el || document.body).innerText;
299
299
  return text.replace(/\s+/g, ' ').trim();
300
300
  })()
301
- `]);return{url:t,content:r}}catch(r){return{url:t,content:null,error:r.message}}finally{await xt(e)}}var Sl,We,Me="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36",Ae,hn,mn,bn,kn,Ye,Cn,Kn,Zn,Ue,ki,yi,_i,vi,Yt,G,Vt,Gt="gemini",Oe,Jt,ji,Di,Ci,Ie,Ei,Ni,si,Oi,Kt,K,Zt,dt="gemini",Te,Rt,Ti,Pi,Li,at,Wi,Mi,Ai,Ji,Ri,Hi,Fi,Xe,Yi,Vi,Gi,Ki,P,Se,Ii,Xi,xi,Qi,Bt,Z,Ut,It="gemini",Pe,Ht,zi,Si,Le;var rr=gt(()=>{Sl=fn(import.meta.url),We=new on({headingStyle:"atx",bulletListMarker:"-",codeBlockStyle:"fenced"});We.addRule("removeDataUrls",{filter:(t)=>t.tagName==="IMG"&&t.getAttribute("src")?.startsWith("data:"),replacement:()=>""});Ae={"user-agent":Me,accept:"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8","accept-language":"en-US,en;q=0.9","accept-encoding":"gzip, deflate, br","cache-control":"no-cache",pragma:"no-cache","sec-ch-ua":'"Chromium";v="122", "Not(A:Brand";v="24", "Google Chrome";v="122"',"sec-ch-ua-mobile":"?0","sec-ch-ua-platform":'"Windows"',"sec-fetch-dest":"document","sec-fetch-mode":"navigate","sec-fetch-site":"none","sec-fetch-user":"?1","upgrade-insecure-requests":"1"},hn=[/^localhost$/i,/^127\.\d+\.\d+\.\d+$/,/^0\.0\.0\.0$/,/^\[::1\]$/,/^10\./,/^172\.(1[6-9]|2\d|3[01])\./,/^192\.168\./,/^169\.254\./,/^fc00:/i,/^fe80:/i,/\.local$/i,/\.internal$/i,/\.localhost$/i];mn=["accounts.google.com","login.microsoftonline.com","login.live.com","auth0.com","okta.com","auth.mozilla.auth0.com","id.atlassian.com"],bn=["login.","signin.","auth.","sso.","accounts.","idp."],kn=["sign in to continue","log in to continue","authentication required","create an account to continue","subscribe to continue reading","members only"];Ye={"user-agent":"GreedySearch/1.0",accept:"application/vnd.github+json","x-github-api-version":"2022-11-28"};Cn={"user-agent":"GreedySearch/1.0 (Research Bot)",accept:"application/json"};Kn=Pn(Wn(import.meta.url)),Zn=Ln(Kn,"..","bin","cdp.mjs");Ue=`${bt().replaceAll("\\","/")}/greedysearch-chrome-profile`,ki=`${Ue}/DevToolsActivePort`,yi=`${bt().replaceAll("\\","/")}/cdp-pages.json`,_i=`${bt().replaceAll("\\","/")}/greedysearch-chrome-mode`,vi=`${bt().replaceAll("\\","/")}/greedysearch-visible-recovery.jsonl`,Yt=Ke(Rn(),".dm"),G=Ke(Yt,"greedyconfig"),Vt=["perplexity","google","chatgpt"];Un();Oe=["gemini","chatgpt"];Jt={perplexity:"perplexity.mjs",p:"perplexity.mjs",bing:"bing-copilot.mjs",b:"bing-copilot.mjs",google:"google-ai.mjs",g:"google-ai.mjs",gemini:"gemini.mjs",gem:"gemini.mjs",chatgpt:"chatgpt.mjs",gpt:"chatgpt.mjs","semantic-scholar":"semantic-scholar.mjs",semanticscholar:"semantic-scholar.mjs",s2:"semantic-scholar.mjs",logically:"logically.mjs",log:"logically.mjs"},ji=Bn(),Di=In(),Ci=Math.max(1,Number.parseInt(process.env.GREEDY_FETCH_CONCURRENCY||"5",10)||5);process.env.CDP_PROFILE_DIR=Ue;Ie=`${kt().replaceAll("\\","/")}/greedysearch-chrome-profile`,Ei=`${Ie}/DevToolsActivePort`,Ni=`${kt().replaceAll("\\","/")}/cdp-pages.json`,si=`${kt().replaceAll("\\","/")}/greedysearch-chrome-mode`,Oi=`${kt().replaceAll("\\","/")}/greedysearch-visible-recovery.jsonl`,Kt=de(Vn(),".dm"),K=de(Kt,"greedyconfig"),Zt=["perplexity","google","chatgpt"];xn();Te=["gemini","chatgpt"];Rt={perplexity:"perplexity.mjs",p:"perplexity.mjs",bing:"bing-copilot.mjs",b:"bing-copilot.mjs",google:"google-ai.mjs",g:"google-ai.mjs",gemini:"gemini.mjs",gem:"gemini.mjs",chatgpt:"chatgpt.mjs",gpt:"chatgpt.mjs","semantic-scholar":"semantic-scholar.mjs",semanticscholar:"semantic-scholar.mjs",s2:"semantic-scholar.mjs",logically:"logically.mjs",log:"logically.mjs"},Ti=Xn(),Pi=Qn(),Li=Math.max(1,Number.parseInt(process.env.GREEDY_FETCH_CONCURRENCY||"5",10)||5);process.env.CDP_PROFILE_DIR=Ie;at=Hn().replaceAll("\\","/"),Wi=`${at}/greedysearch-chrome-metadata.json`,Mi=`${at}/greedysearch-chrome-launch.lock`,Ai=`${at}/greedysearch-chrome.pid`,Ji=`${at}/greedysearch-chrome-mode`,Ri=`${at}/greedysearch-chrome-last-activity`,Hi=Number.parseInt(process.env.GREEDY_SEARCH_IDLE_TIMEOUT_MINUTES||"5",10)||5,Fi=import.meta.dirname||new URL(".",import.meta.url).pathname.replace(/^\/([A-Z]:)/,"$1"),Xe=On().replaceAll("\\","/"),Yi=`${Xe}/greedysearch-chrome.pid`,Vi=`${Xe}/greedysearch-chrome-last-activity`,Gi=Number.parseInt(process.env.GREEDY_SEARCH_IDLE_TIMEOUT_MINUTES||"5",10)||5,Ki=Number.parseInt(process.env.GREEDY_SEARCH_VISIBLE_IDLE_TIMEOUT_MINUTES||"60",10)||60,P=Be;Se=`${yt().replaceAll("\\","/")}/greedysearch-chrome-profile`,Ii=`${Se}/DevToolsActivePort`,Xi=`${yt().replaceAll("\\","/")}/cdp-pages.json`,xi=`${yt().replaceAll("\\","/")}/greedysearch-chrome-mode`,Qi=`${yt().replaceAll("\\","/")}/greedysearch-visible-recovery.jsonl`,Bt=ze(un(),".dm"),Z=ze(Bt,"greedyconfig"),Ut=["perplexity","google","chatgpt"];ta();Pe=["gemini","chatgpt"];Ht={perplexity:"perplexity.mjs",p:"perplexity.mjs",bing:"bing-copilot.mjs",b:"bing-copilot.mjs",google:"google-ai.mjs",g:"google-ai.mjs",gemini:"gemini.mjs",gem:"gemini.mjs",chatgpt:"chatgpt.mjs",gpt:"chatgpt.mjs","semantic-scholar":"semantic-scholar.mjs",semanticscholar:"semantic-scholar.mjs",s2:"semantic-scholar.mjs",logically:"logically.mjs",log:"logically.mjs"},zi=qn(),Si=ea(),Le=Math.max(1,Number.parseInt(process.env.GREEDY_FETCH_CONCURRENCY||"5",10)||5);process.env.CDP_PROFILE_DIR=Se});var ar={};et(ar,{writeSourcesToFiles:()=>$a});import{mkdirSync as oa,writeFileSync as ha}from"node:fs";import{join as nr}from"node:path";function $a(t,e=wa){return oa(e,{recursive:!0}),t.map((r)=>{if(!r.content||r.content.length<10)return r;let n=String(r.id||"unknown").replace(/[^a-zA-Z0-9_-]/g,""),a=(r.canonicalUrl||r.url||"").replace(/^https?:\/\//,"").replace(/[^a-zA-Z0-9]/g,"-").slice(0,40),l=`${n}-${a}.md`,i=nr(e,l),f=`---
301
+ `]);return{url:t,content:r}}catch(r){return{url:t,content:null,error:r.message}}finally{await Qt(e)}}var fi,Je,He="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36",Fe,yn,vn,jn,Dn,Ze,Pn,Xn,xn,Qe,Ti,Oi,Pi,Li,Gt,V,Vt,dt="gemini",We,Jt,Wi,Mi,Ai,ze,Ri,Ji,Hi,Fi,Zt,d,Bt,Kt="gemini",Me,Ht,Yi,Gi,Vi,at,di,Zi,Bi,Ki,Ui,Ii,Xi,Se,xi,Qi,zi,Si,P,er,rf,nf,af,lf,Ut,Z,It,Xt="gemini",Ae,Ft,ff,gf,Re;var ir=gt(()=>{fi=$n(import.meta.url),Je=new bn({headingStyle:"atx",bulletListMarker:"-",codeBlockStyle:"fenced"});Je.addRule("removeDataUrls",{filter:(t)=>t.tagName==="IMG"&&t.getAttribute("src")?.startsWith("data:"),replacement:()=>""});Fe={"user-agent":He,accept:"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8","accept-language":"en-US,en;q=0.9","accept-encoding":"gzip, deflate, br","cache-control":"no-cache",pragma:"no-cache","sec-ch-ua":'"Chromium";v="122", "Not(A:Brand";v="24", "Google Chrome";v="122"',"sec-ch-ua-mobile":"?0","sec-ch-ua-platform":'"Windows"',"sec-fetch-dest":"document","sec-fetch-mode":"navigate","sec-fetch-site":"none","sec-fetch-user":"?1","upgrade-insecure-requests":"1"},yn=[/^localhost$/i,/^127\.\d+\.\d+\.\d+$/,/^0\.0\.0\.0$/,/^\[::1\]$/,/^10\./,/^172\.(1[6-9]|2\d|3[01])\./,/^192\.168\./,/^169\.254\./,/^fc00:/i,/^fe80:/i,/\.local$/i,/\.internal$/i,/\.localhost$/i];vn=["accounts.google.com","login.microsoftonline.com","login.live.com","auth0.com","okta.com","auth.mozilla.auth0.com","id.atlassian.com"],jn=["login.","signin.","auth.","sso.","accounts.","idp."],Dn=["sign in to continue","log in to continue","authentication required","create an account to continue","subscribe to continue reading","members only"];Ze={"user-agent":"GreedySearch/1.0",accept:"application/vnd.github+json","x-github-api-version":"2022-11-28"};Pn={"user-agent":"GreedySearch/1.0 (Research Bot)",accept:"application/json"};Xn=Jn(Fn(import.meta.url)),xn=Hn(Xn,"..","bin","cdp.mjs");Qe=`${yt().replaceAll("\\","/")}/greedysearch-chrome-profile`,Ti=`${Qe}/DevToolsActivePort`,Oi=`${yt().replaceAll("\\","/")}/cdp-pages.json`,Pi=`${yt().replaceAll("\\","/")}/greedysearch-chrome-mode`,Li=`${yt().replaceAll("\\","/")}/greedysearch-visible-recovery.jsonl`,Gt=Ue(dn(),".dm"),V=Ue(Gt,"greedyconfig"),Vt=["perplexity","google","chatgpt"];Sn();We=["gemini","chatgpt"];Jt={perplexity:"perplexity.mjs",p:"perplexity.mjs",bing:"bing-copilot.mjs",b:"bing-copilot.mjs",google:"google-ai.mjs",g:"google-ai.mjs",gemini:"gemini.mjs",gem:"gemini.mjs",chatgpt:"chatgpt.mjs",gpt:"chatgpt.mjs","semantic-scholar":"semantic-scholar.mjs",semanticscholar:"semantic-scholar.mjs",s2:"semantic-scholar.mjs",logically:"logically.mjs",log:"logically.mjs"},Wi=zn(),Mi=un(),Ai=Math.max(1,Number.parseInt(process.env.GREEDY_FETCH_CONCURRENCY||"5",10)||5);process.env.CDP_PROFILE_DIR=Qe;ze=`${_t().replaceAll("\\","/")}/greedysearch-chrome-profile`,Ri=`${ze}/DevToolsActivePort`,Ji=`${_t().replaceAll("\\","/")}/cdp-pages.json`,Hi=`${_t().replaceAll("\\","/")}/greedysearch-chrome-mode`,Fi=`${_t().replaceAll("\\","/")}/greedysearch-visible-recovery.jsonl`,Zt=Xe(Un(),".dm"),d=Xe(Zt,"greedyconfig"),Bt=["perplexity","google","chatgpt"];ta();Me=["gemini","chatgpt"];Ht={perplexity:"perplexity.mjs",p:"perplexity.mjs",bing:"bing-copilot.mjs",b:"bing-copilot.mjs",google:"google-ai.mjs",g:"google-ai.mjs",gemini:"gemini.mjs",gem:"gemini.mjs",chatgpt:"chatgpt.mjs",gpt:"chatgpt.mjs","semantic-scholar":"semantic-scholar.mjs",semanticscholar:"semantic-scholar.mjs",s2:"semantic-scholar.mjs",logically:"logically.mjs",log:"logically.mjs"},Yi=qn(),Gi=ea(),Vi=Math.max(1,Number.parseInt(process.env.GREEDY_FETCH_CONCURRENCY||"5",10)||5);process.env.CDP_PROFILE_DIR=ze;at=Zn().replaceAll("\\","/"),di=`${at}/greedysearch-chrome-metadata.json`,Zi=`${at}/greedysearch-chrome-launch.lock`,Bi=`${at}/greedysearch-chrome.pid`,Ki=`${at}/greedysearch-chrome-mode`,Ui=`${at}/greedysearch-chrome-last-activity`,Ii=Number.parseInt(process.env.GREEDY_SEARCH_IDLE_TIMEOUT_MINUTES||"5",10)||5,Xi=import.meta.dirname||new URL(".",import.meta.url).pathname.replace(/^\/([A-Z]:)/,"$1"),Se=An().replaceAll("\\","/"),xi=`${Se}/greedysearch-chrome.pid`,Qi=`${Se}/greedysearch-chrome-last-activity`,zi=Number.parseInt(process.env.GREEDY_SEARCH_IDLE_TIMEOUT_MINUTES||"5",10)||5,Si=Number.parseInt(process.env.GREEDY_SEARCH_VISIBLE_IDLE_TIMEOUT_MINUTES||"60",10)||60,P=xe;er=`${kt().replaceAll("\\","/")}/greedysearch-chrome-profile`,rf=`${er}/DevToolsActivePort`,nf=`${kt().replaceAll("\\","/")}/cdp-pages.json`,af=`${kt().replaceAll("\\","/")}/greedysearch-chrome-mode`,lf=`${kt().replaceAll("\\","/")}/greedysearch-visible-recovery.jsonl`,Ut=tr(aa(),".dm"),Z=tr(Ut,"greedyconfig"),It=["perplexity","google","chatgpt"];ia();Ae=["gemini","chatgpt"];Ft={perplexity:"perplexity.mjs",p:"perplexity.mjs",bing:"bing-copilot.mjs",b:"bing-copilot.mjs",google:"google-ai.mjs",g:"google-ai.mjs",gemini:"gemini.mjs",gem:"gemini.mjs",chatgpt:"chatgpt.mjs",gpt:"chatgpt.mjs","semantic-scholar":"semantic-scholar.mjs",semanticscholar:"semantic-scholar.mjs",s2:"semantic-scholar.mjs",logically:"logically.mjs",log:"logically.mjs"},ff=la(),gf=fa(),Re=Math.max(1,Number.parseInt(process.env.GREEDY_FETCH_CONCURRENCY||"5",10)||5);process.env.CDP_PROFILE_DIR=er});var gr={};et(gr,{writeSourcesToFiles:()=>ka});import{mkdirSync as ba,writeFileSync as ya}from"node:fs";import{join as fr}from"node:path";function ka(t,e=_a){return ba(e,{recursive:!0}),t.map((r)=>{if(!r.content||r.content.length<10)return r;let n=String(r.id||"unknown").replace(/[^a-zA-Z0-9_-]/g,""),a=(r.canonicalUrl||r.url||"").replace(/^https?:\/\//,"").replace(/[^a-zA-Z0-9]/g,"-").slice(0,40),l=`${n}-${a}.md`,i=fr(e,l),f=`---
302
302
  url: ${r.finalUrl||r.url}
303
303
  title: ${r.title||""}
304
304
  source: ${r.source||"unknown"}
@@ -306,38 +306,38 @@ status: ${r.status||""}
306
306
  chars: ${r.contentChars||r.content.length}
307
307
  ---
308
308
 
309
- `;ha(i,f+r.content,"utf8");let{content:p,...g}=r;return{...g,contentPath:i,contentChars:r.contentChars||p.length}})}var wa;var lr=gt(()=>{wa=nr(process.cwd(),".dm","greedysearch-sources")});var Qt={};et(Qt,{parseGitHubUrl:()=>gr,fetchGitHubContent:()=>ba});function gr(t){try{let e=new URL(t);if(!(e.hostname==="github.com"||e.hostname.endsWith(".github.com")))return null;let r=e.pathname.split("/").filter(Boolean);if(r.length<2)return null;let[n,a]=r;if(r.length===2)return{owner:n,repo:a,type:"root"};if(r.length>=4&&(r[2]==="blob"||r[2]==="tree")){let l=r[2],i=r[3],f=r.slice(4).join("/");return{owner:n,repo:a,type:l,ref:i,path:f}}return null}catch{return null}}async function U(t,e=1e4){let r=new AbortController,n=setTimeout(()=>r.abort(),e);try{let a=await fetch(`https://api.github.com${t}`,{headers:fr,signal:r.signal});if(clearTimeout(n),!a.ok)throw Error(`GitHub API ${a.status}: ${t}`);return await a.json()}catch(a){throw clearTimeout(n),a}}async function ca(t,e){try{let r=await U(`/repos/${t}/${e}/readme`);if(r.content&&r.encoding==="base64")return Buffer.from(r.content,"base64").toString("utf8");return""}catch{return""}}async function ir(t,e,r="HEAD",n=""){try{let a=await U(`/repos/${t}/${e}/git/ref/heads/${r==="HEAD"?"main":r}`).catch(()=>U(`/repos/${t}/${e}/git/ref/heads/master`).catch(()=>null)),l;if(a?.object?.sha)l=(await U(`/repos/${t}/${e}/git/commits/${a.object.sha}`)).tree.sha;else{let f=await U(`/repos/${t}/${e}`);l=(await U(`/repos/${t}/${e}/branches/${f.default_branch}`)).commit.commit.tree.sha}let i=(await U(`/repos/${t}/${e}/git/trees/${l}`)).tree||[];if(n)i=i.filter((f)=>f.path.startsWith(n));return i.slice(0,50).map((f)=>({path:f.path,type:f.type==="tree"?"dir":"file",size:f.size}))}catch{return[]}}async function ma(t,e,r,n,a=1e4){let l=[`https://raw.githubusercontent.com/${t}/${e}/${r&&r!=="HEAD"?r:"main"}/${n}`,`https://raw.githubusercontent.com/${t}/${e}/master/${n}`];for(let i of l){let f=new AbortController,p=setTimeout(()=>f.abort(),a);try{let g=await fetch(i,{headers:{"user-agent":fr["user-agent"]},signal:f.signal});if(clearTimeout(p),g.ok)return await g.text()}catch{clearTimeout(p)}}return null}async function ba(t){let e=gr(t);if(!e)return{ok:!1,error:"Not a valid GitHub URL"};let{owner:r,repo:n,type:a,ref:l,path:i}=e;try{if(a==="root"||a==="tree"&&!i){let[f,p,g]=await Promise.allSettled([U(`/repos/${r}/${n}`),ca(r,n),ir(r,n,l||"HEAD")]);if(f.status==="rejected")return{ok:!1,error:f.reason?.message||"Repo not found"};let o=f.value,h=p.status==="fulfilled"?p.value:"",m=g.status==="fulfilled"?g.value:[],c=o?.description?`
309
+ `;ya(i,f+r.content,"utf8");let{content:o,...g}=r;return{...g,contentPath:i,contentChars:r.contentChars||o.length}})}var _a;var or=gt(()=>{_a=fr(process.cwd(),".dm","greedysearch-sources")});var zt={};et(zt,{parseGitHubUrl:()=>wr,fetchGitHubContent:()=>ja});function wr(t){try{let e=new URL(t);if(!(e.hostname==="github.com"||e.hostname.endsWith(".github.com")))return null;let r=e.pathname.split("/").filter(Boolean);if(r.length<2)return null;let[n,a]=r;if(r.length===2)return{owner:n,repo:a,type:"root"};if(r.length>=4&&(r[2]==="blob"||r[2]==="tree")){let l=r[2],i=r[3],f=r.slice(4).join("/");return{owner:n,repo:a,type:l,ref:i,path:f}}return null}catch{return null}}async function U(t,e=1e4){let r=new AbortController,n=setTimeout(()=>r.abort(),e);try{let a=await fetch(`https://api.github.com${t}`,{headers:hr,signal:r.signal});if(clearTimeout(n),!a.ok)throw Error(`GitHub API ${a.status}: ${t}`);return await a.json()}catch(a){throw clearTimeout(n),a}}async function sa(t,e){try{let r=await U(`/repos/${t}/${e}/readme`);if(r.content&&r.encoding==="base64")return Buffer.from(r.content,"base64").toString("utf8");return""}catch{return""}}async function pr(t,e,r="HEAD",n=""){try{let a=await U(`/repos/${t}/${e}/git/ref/heads/${r==="HEAD"?"main":r}`).catch(()=>U(`/repos/${t}/${e}/git/ref/heads/master`).catch(()=>null)),l;if(a?.object?.sha)l=(await U(`/repos/${t}/${e}/git/commits/${a.object.sha}`)).tree.sha;else{let f=await U(`/repos/${t}/${e}`);l=(await U(`/repos/${t}/${e}/branches/${f.default_branch}`)).commit.commit.tree.sha}let i=(await U(`/repos/${t}/${e}/git/trees/${l}`)).tree||[];if(n)i=i.filter((f)=>f.path.startsWith(n));return i.slice(0,50).map((f)=>({path:f.path,type:f.type==="tree"?"dir":"file",size:f.size}))}catch{return[]}}async function va(t,e,r,n,a=1e4){let l=[`https://raw.githubusercontent.com/${t}/${e}/${r&&r!=="HEAD"?r:"main"}/${n}`,`https://raw.githubusercontent.com/${t}/${e}/master/${n}`];for(let i of l){let f=new AbortController,o=setTimeout(()=>f.abort(),a);try{let g=await fetch(i,{headers:{"user-agent":hr["user-agent"]},signal:f.signal});if(clearTimeout(o),g.ok)return await g.text()}catch{clearTimeout(o)}}return null}async function ja(t){let e=wr(t);if(!e)return{ok:!1,error:"Not a valid GitHub URL"};let{owner:r,repo:n,type:a,ref:l,path:i}=e;try{if(a==="root"||a==="tree"&&!i){let[f,o,g]=await Promise.allSettled([U(`/repos/${r}/${n}`),sa(r,n),pr(r,n,l||"HEAD")]);if(f.status==="rejected")return{ok:!1,error:f.reason?.message||"Repo not found"};let p=f.value,h=o.status==="fulfilled"?o.value:"",m=g.status==="fulfilled"?g.value:[],c=p?.description?`
310
310
 
311
- > ${o.description}`:"",y=o?.stargazers_count==null?"":` ⭐ ${o.stargazers_count}`,b=o?.language?` · ${o.language}`:"",w=`# ${r}/${n}${y}${b}${c}
311
+ > ${p.description}`:"",y=p?.stargazers_count==null?"":` ⭐ ${p.stargazers_count}`,b=p?.language?` · ${p.language}`:"",w=`# ${r}/${n}${y}${b}${c}
312
312
 
313
313
  `;if(h)w+=h.slice(0,6000);else w+=`[No README found]
314
314
 
315
315
  Files:
316
- ${m.map((_)=>` ${_.type==="dir"?"\uD83D\uDCC1":"\uD83D\uDCC4"} ${_.path}`).join(`
317
- `)}`;return{ok:!0,title:`${r}/${n}`,content:w,tree:m.slice(0,30)}}if(a==="blob"&&i){let f=await ma(r,n,l,i);if(f===null)return{ok:!1,error:`File not found: ${i}`};return{ok:!0,title:`${r}/${n}: ${i}`,content:f}}if(a==="tree"&&i){let f=await ir(r,n,l||"HEAD",i),p=f.map((g)=>` ${g.type==="dir"?"\uD83D\uDCC1":"\uD83D\uDCC4"} ${g.path}`).join(`
316
+ ${m.map((k)=>` ${k.type==="dir"?"\uD83D\uDCC1":"\uD83D\uDCC4"} ${k.path}`).join(`
317
+ `)}`;return{ok:!0,title:`${r}/${n}`,content:w,tree:m.slice(0,30)}}if(a==="blob"&&i){let f=await va(r,n,l,i);if(f===null)return{ok:!1,error:`File not found: ${i}`};return{ok:!0,title:`${r}/${n}: ${i}`,content:f}}if(a==="tree"&&i){let f=await pr(r,n,l||"HEAD",i),o=f.map((g)=>` ${g.type==="dir"?"\uD83D\uDCC1":"\uD83D\uDCC4"} ${g.path}`).join(`
318
318
  `);return{ok:!0,title:`${r}/${n}/${i}`,content:`[Directory: ${i}]
319
319
 
320
320
  Files:
321
- ${p}`,tree:f}}return{ok:!1,error:"Unsupported GitHub URL type"}}catch(f){return{ok:!1,error:f.message}}}var fr;var zt=gt(()=>{fr={"user-agent":"GreedySearch/1.0",accept:"application/vnd.github+json","x-github-api-version":"2022-11-28"}});var St={};et(St,{truncateContent:()=>_a,trimContentHeadTail:()=>ya});function ya(t,e=8000){if(!t||t.length<=e)return t;let r=`
321
+ ${o}`,tree:f}}return{ok:!1,error:"Unsupported GitHub URL type"}}catch(f){return{ok:!1,error:f.message}}}var hr;var St=gt(()=>{hr={"user-agent":"GreedySearch/1.0",accept:"application/vnd.github+json","x-github-api-version":"2022-11-28"}});var ut={};et(ut,{truncateContent:()=>Ca,trimContentHeadTail:()=>Ea});function Ea(t,e=8000){if(!t||t.length<=e)return t;let r=`
322
322
 
323
323
  [...content trimmed...]
324
324
 
325
325
  `,n=e-r.length,a=Math.floor(n*0.75),l=n-a,i=a;while(i>a-100&&t[i]!==`
326
326
  `)i--;if(i<=a-100)i=a;let f=t.length-l;while(f<t.length-l+100&&t[f]!==`
327
- `)f++;if(f>=t.length-l+100)f=t.length-l;let p=t.slice(0,i).trimEnd(),g=t.slice(f).trimStart();return`${p}${r}${g}`}function _a(t,e=8000){if(!t||t.length<=e)return t;let r=t.slice(0,e),n=r.lastIndexOf(" ");return n>0?`${r.slice(0,n)}...`:`${r}...`}var yr={};et(yr,{shouldUseBrowser:()=>Wa,rewriteGitHubUrl:()=>cr,isPrivateUrl:()=>$r,fetchSourceHttp:()=>Na,extractContent:()=>br,detectBotBlock:()=>mr,defaultFetchHeaders:()=>Ea,checkContentQuality:()=>kr});import{Readability as va}from"@mozilla/readability";import{JSDOM as ja}from"jsdom";import Da from"turndown";function Ea(t={}){return{...wr,...t}}function $r(t){try{let e=new URL(t),r=e.hostname.toLowerCase();for(let n of Ca)if(n.test(r))return{blocked:!0,reason:`Private/internal address: ${r}`};if(e.protocol==="file:")return{blocked:!0,reason:"File protocol not allowed"};return{blocked:!1}}catch(e){return{blocked:!0,reason:`Invalid URL: ${e.message}`}}}function cr(t){try{let e=new URL(t);if(!(e.hostname==="github.com"||e.hostname.endsWith(".github.com")))return t;let r=e.pathname.split("/").filter(Boolean);if(r.length<5)return t;let[n,a,l,i,...f]=r;if(l!=="blob")return t;let p=f.join("/");return`https://raw.githubusercontent.com/${n}/${a}/${i}/${p}`}catch{return t}}async function Na(t,e={}){let r=$r(t);if(r.blocked)return{ok:!1,url:t,finalUrl:t,status:403,error:`Blocked: ${r.reason}`,needsBrowser:!1};let n=t;if(t=cr(t),t!==n)console.error(`[fetcher] Rewrote GitHub URL: ${n.slice(0,60)}... → raw.githubusercontent.com`);let{timeoutMs:a=15000,userAgent:l,signal:i}=e,f=new AbortController,p=setTimeout(()=>f.abort(),a);if(i)i.addEventListener("abort",()=>f.abort(),{once:!0});try{let g=await fetch(t,{method:"GET",headers:{...wr,"user-agent":l||hr},redirect:"follow",signal:f.signal});clearTimeout(p);let o=g.headers.get("content-type")||"",h=g.url,m=g.headers.get("last-modified")||"",c=!1;try{c=new URL(h).hostname.toLowerCase()==="raw.githubusercontent.com"}catch{}if(o.includes("text/plain")&&c){let N=await g.text();return{ok:!0,url:n,finalUrl:h,status:g.status,title:h.split("/").pop()||"GitHub File",byline:"",siteName:"GitHub",lang:"",publishedTime:m,lastModified:m,markdown:N,contentLength:N.length,excerpt:N.slice(0,300).replaceAll(/\n/g," "),needsBrowser:!1}}if(!o.includes("text/html")&&!o.includes("application/xhtml"))return{ok:!1,url:t,finalUrl:h,status:g.status,error:`Unsupported content type: ${o}`,needsBrowser:!1};let y=await g.text(),b=mr(g.status,y,h,t);if(b.blocked)return{ok:!1,url:t,finalUrl:h,status:g.status,error:`Blocked: ${b.reason}`,needsBrowser:!0};let w=br(y,h),_=kr(w);if(!_.ok)return{ok:!1,url:t,finalUrl:h,status:g.status,error:`Low quality content: ${_.reason}`,needsBrowser:!0};return{ok:!0,url:t,finalUrl:h,status:g.status,title:w.title,byline:w.byline,siteName:w.siteName,lang:w.lang,publishedTime:w.publishedTime||m,lastModified:m,markdown:w.markdown,excerpt:w.excerpt,contentLength:w.markdown.length,needsBrowser:!1}}catch(g){clearTimeout(p);let o=La(g);return{ok:!1,url:t,finalUrl:t,status:0,error:g.message,needsBrowser:o}}}function mr(t,e,r,n){let a=e.match(/<title[^>]*>([^<]*)<\/title>/i)?.[1]?.toLowerCase()||"",l=e.slice(0,30000).toLowerCase(),i=`${a} ${l}`;if(t===403||t===429||t===503)return{blocked:!0,reason:`HTTP ${t}`};let f=[{pattern:/class=["'][^"']*captcha["']|<div[^>]*id=["']captcha/i,reason:"captcha"},{pattern:/g-recaptcha|data-sitekey|i['"]m not a robot/i,reason:"captcha"},{pattern:/checking your browser.{0,100}please wait|cf-browser-verification/i,reason:"cloudflare challenge"},{pattern:/just a moment.{0,50}security check|ddos protection by cloudflare/i,reason:"cloudflare challenge"},{pattern:/unusual traffic.{0,50}from your computer network/i,reason:"unusual traffic"},{pattern:/bot detected|automated.{0,20}request/i,reason:"bot detection"},{pattern:/enable\s+javascript\s+to\s+view|javascript\s+is\s+required.{0,50}enabled/i,reason:"requires javascript"},{pattern:/access denied|accessdenied/i,reason:"access denied"},{pattern:/protected by anubis|anubis uses a proof-of-work/i,reason:"anubis challenge"}];for(let g of f)if(g.pattern.test(i))return{blocked:!0,reason:g.reason};let p=Pa(n,r,e);if(p)return{blocked:!0,reason:p};return{blocked:!1}}function Pa(t,e,r){try{let n=new URL(t),a=new URL(e);if(n.hostname.toLowerCase()===a.hostname.toLowerCase())return;let l=a.hostname.toLowerCase();if(sa.some((f)=>l===f||l.endsWith(`.${f}`)))return`redirected to login (${a.hostname})`;if(Oa.some((f)=>l.startsWith(f)))return`redirected to login (${a.hostname})`;let i=r.slice(0,20000).toLowerCase();if(Ta.some((f)=>i.includes(f)))return`redirected to login page (${a.hostname})`}catch{}return}function La(t){let e=t.message.toLowerCase();return e.includes("fetch failed")||e.includes("unable to verify")||e.includes("certificate")||e.includes("timeout")}function pr(t){let e=['meta[property="article:published_time"]','meta[name="article:published_time"]','meta[property="og:published_time"]','meta[name="publication_date"]','meta[name="date"]','meta[itemprop="datePublished"]','time[itemprop="datePublished"]','meta[name="DC.date"]'];for(let r of e){let n=t.querySelector(r),a=n?.getAttribute("content")||n?.getAttribute("datetime")||"";if(a)return a}return""}function br(t,e){let r=new ja(t,{url:e}).window.document,n=new va(r).parse();if(n&&n.content){let l=or.turndown(n.content).replaceAll(/\n{3,}/g,`
327
+ `)f++;if(f>=t.length-l+100)f=t.length-l;let o=t.slice(0,i).trimEnd(),g=t.slice(f).trimStart();return`${o}${r}${g}`}function Ca(t,e=8000){if(!t||t.length<=e)return t;let r=t.slice(0,e),n=r.lastIndexOf(" ");return n>0?`${r.slice(0,n)}...`:`${r}...`}var jr={};et(jr,{shouldUseBrowser:()=>Fa,rewriteGitHubUrl:()=>_r,isPrivateUrl:()=>yr,fetchSourceHttp:()=>Wa,extractContent:()=>sr,detectBotBlock:()=>kr,defaultFetchHeaders:()=>La,checkContentQuality:()=>vr});import{Readability as Na}from"@mozilla/readability";import{JSDOM as Ta}from"jsdom";import Oa from"turndown";function La(t={}){return{...br,...t}}function yr(t){try{let e=new URL(t),r=e.hostname.toLowerCase();for(let n of Pa)if(n.test(r))return{blocked:!0,reason:`Private/internal address: ${r}`};if(e.protocol==="file:")return{blocked:!0,reason:"File protocol not allowed"};return{blocked:!1}}catch(e){return{blocked:!0,reason:`Invalid URL: ${e.message}`}}}function _r(t){try{let e=new URL(t);if(!(e.hostname==="github.com"||e.hostname.endsWith(".github.com")))return t;let r=e.pathname.split("/").filter(Boolean);if(r.length<5)return t;let[n,a,l,i,...f]=r;if(l!=="blob")return t;let o=f.join("/");return`https://raw.githubusercontent.com/${n}/${a}/${i}/${o}`}catch{return t}}async function Wa(t,e={}){let r=yr(t);if(r.blocked)return{ok:!1,url:t,finalUrl:t,status:403,error:`Blocked: ${r.reason}`,needsBrowser:!1};let n=t;if(t=_r(t),t!==n)console.error(`[fetcher] Rewrote GitHub URL: ${n.slice(0,60)}... → raw.githubusercontent.com`);let{timeoutMs:a=15000,userAgent:l,signal:i}=e,f=new AbortController,o=setTimeout(()=>f.abort(),a);if(i)i.addEventListener("abort",()=>f.abort(),{once:!0});try{let g=await fetch(t,{method:"GET",headers:{...br,"user-agent":l||mr},redirect:"follow",signal:f.signal});clearTimeout(o);let p=g.headers.get("content-type")||"",h=g.url,m=g.headers.get("last-modified")||"",c=!1;try{c=new URL(h).hostname.toLowerCase()==="raw.githubusercontent.com"}catch{}if(p.includes("text/plain")&&c){let C=await g.text();return{ok:!0,url:n,finalUrl:h,status:g.status,title:h.split("/").pop()||"GitHub File",byline:"",siteName:"GitHub",lang:"",publishedTime:m,lastModified:m,markdown:C,contentLength:C.length,excerpt:C.slice(0,300).replaceAll(/\n/g," "),needsBrowser:!1}}if(!p.includes("text/html")&&!p.includes("application/xhtml"))return{ok:!1,url:t,finalUrl:h,status:g.status,error:`Unsupported content type: ${p}`,needsBrowser:!1};let y=await g.text(),b=kr(g.status,y,h,t);if(b.blocked)return{ok:!1,url:t,finalUrl:h,status:g.status,error:`Blocked: ${b.reason}`,needsBrowser:!0};let w=sr(y,h),k=vr(w);if(!k.ok)return{ok:!1,url:t,finalUrl:h,status:g.status,error:`Low quality content: ${k.reason}`,needsBrowser:!0};return{ok:!0,url:t,finalUrl:h,status:g.status,title:w.title,byline:w.byline,siteName:w.siteName,lang:w.lang,publishedTime:w.publishedTime||m,lastModified:m,markdown:w.markdown,excerpt:w.excerpt,contentLength:w.markdown.length,needsBrowser:!1}}catch(g){clearTimeout(o);let p=Ha(g);return{ok:!1,url:t,finalUrl:t,status:0,error:g.message,needsBrowser:p}}}function kr(t,e,r,n){let a=e.match(/<title[^>]*>([^<]*)<\/title>/i)?.[1]?.toLowerCase()||"",l=e.slice(0,30000).toLowerCase(),i=`${a} ${l}`;if(t===403||t===429||t===503)return{blocked:!0,reason:`HTTP ${t}`};let f=[{pattern:/class=["'][^"']*captcha["']|<div[^>]*id=["']captcha/i,reason:"captcha"},{pattern:/g-recaptcha|data-sitekey|i['"]m not a robot/i,reason:"captcha"},{pattern:/checking your browser.{0,100}please wait|cf-browser-verification/i,reason:"cloudflare challenge"},{pattern:/just a moment.{0,50}security check|ddos protection by cloudflare/i,reason:"cloudflare challenge"},{pattern:/unusual traffic.{0,50}from your computer network/i,reason:"unusual traffic"},{pattern:/bot detected|automated.{0,20}request/i,reason:"bot detection"},{pattern:/enable\s+javascript\s+to\s+view|javascript\s+is\s+required.{0,50}enabled/i,reason:"requires javascript"},{pattern:/access denied|accessdenied/i,reason:"access denied"},{pattern:/protected by anubis|anubis uses a proof-of-work/i,reason:"anubis challenge"}];for(let g of f)if(g.pattern.test(i))return{blocked:!0,reason:g.reason};let o=Ja(n,r,e);if(o)return{blocked:!0,reason:o};return{blocked:!1}}function Ja(t,e,r){try{let n=new URL(t),a=new URL(e);if(n.hostname.toLowerCase()===a.hostname.toLowerCase())return;let l=a.hostname.toLowerCase();if(Ma.some((f)=>l===f||l.endsWith(`.${f}`)))return`redirected to login (${a.hostname})`;if(Aa.some((f)=>l.startsWith(f)))return`redirected to login (${a.hostname})`;let i=r.slice(0,20000).toLowerCase();if(Ra.some((f)=>i.includes(f)))return`redirected to login page (${a.hostname})`}catch{}return}function Ha(t){let e=t.message.toLowerCase();return e.includes("fetch failed")||e.includes("unable to verify")||e.includes("certificate")||e.includes("timeout")}function $r(t){let e=['meta[property="article:published_time"]','meta[name="article:published_time"]','meta[property="og:published_time"]','meta[name="publication_date"]','meta[name="date"]','meta[itemprop="datePublished"]','time[itemprop="datePublished"]','meta[name="DC.date"]'];for(let r of e){let n=t.querySelector(r),a=n?.getAttribute("content")||n?.getAttribute("datetime")||"";if(a)return a}return""}function sr(t,e){let r=new Ta(t,{url:e}).window.document,n=new Na(r).parse();if(n&&n.content){let l=cr.turndown(n.content).replaceAll(/\n{3,}/g,`
328
328
 
329
- `).trim(),i=n.publishedTime||pr(r)||"";return{title:n.title||r.title||e,byline:n.byline||"",siteName:n.siteName||"",lang:n.lang||"",publishedTime:i,markdown:l,excerpt:l.slice(0,300).replaceAll(/\n/g," ")}}let a=r.body;if(a){let l=a.cloneNode(!0);l.querySelectorAll("script, style, nav, footer, header, aside").forEach((f)=>f.remove());let i=(l.textContent||"").replaceAll(/\s+/g," ").trim();return{title:r.title||e,byline:"",siteName:"",lang:"",publishedTime:pr(r),markdown:i,excerpt:i.slice(0,300)}}return{title:e,byline:"",siteName:"",lang:"",publishedTime:"",markdown:"",excerpt:""}}function kr(t){let e=t.markdown.trim().toLowerCase(),r=(t.title||"").toLowerCase();if(t.markdown.trim().length<100)return{ok:!1,reason:"content too short (< 100 chars)"};let n=e.toLowerCase(),a=[{check:()=>n.includes("loading")&&n.includes("please wait"),desc:"loading page"},{check:()=>n.includes("please ensure javascript is enabled"),desc:"requires javascript"},{check:()=>n.includes("enable javascript to view"),desc:"requires javascript"},{check:()=>n.includes("just a moment"),desc:"cloudflare challenge detected in content"},{check:()=>n.includes("verify you are human"),desc:"human verification"},{check:()=>n.includes("captcha required"),desc:"captcha in extracted content"},{check:()=>n.includes("access denied"),desc:"access denied in content"},{check:()=>/^\s{0,10}sign\s{1,5}in\s{0,10}$|^\s{0,10}log\s{1,5}in\s{0,10}$/im.test(e),desc:"login form only"}];for(let{check:l,desc:i}of a)if(l())return{ok:!1,reason:i};if(r.includes("just a moment")||r.includes("checking your browser"))return{ok:!1,reason:"cloudflare challenge page detected in title"};return{ok:!0}}function Wa(t){try{let e=new URL(t),r=e.hostname.toLowerCase(),n=e.pathname.toLowerCase();if(["react.dev","nextjs.org","vuejs.org","angular.io","svelte.dev","docs.expo.dev","tailwindcss.com","storybook.js.org"].some((a)=>r===a||r.endsWith(`.${a}`)))return!0;if(n.includes("/playground")||n.includes("/demo")||n.includes("/app"))return!0;if(e.hash&&e.hash.length>1)return!0;return!1}catch{return!1}}var or,hr="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36",wr,Ca,sa,Oa,Ta;var _r=gt(()=>{or=new Da({headingStyle:"atx",bulletListMarker:"-",codeBlockStyle:"fenced"});or.addRule("removeDataUrls",{filter:(t)=>t.tagName==="IMG"&&t.getAttribute("src")?.startsWith("data:"),replacement:()=>""});wr={"user-agent":hr,accept:"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8","accept-language":"en-US,en;q=0.9","accept-encoding":"gzip, deflate, br","cache-control":"no-cache",pragma:"no-cache","sec-ch-ua":'"Chromium";v="122", "Not(A:Brand";v="24", "Google Chrome";v="122"',"sec-ch-ua-mobile":"?0","sec-ch-ua-platform":'"Windows"',"sec-fetch-dest":"document","sec-fetch-mode":"navigate","sec-fetch-site":"none","sec-fetch-user":"?1","upgrade-insecure-requests":"1"},Ca=[/^localhost$/i,/^127\.\d+\.\d+\.\d+$/,/^0\.0\.0\.0$/,/^\[::1\]$/,/^10\./,/^172\.(1[6-9]|2\d|3[01])\./,/^192\.168\./,/^169\.254\./,/^fc00:/i,/^fe80:/i,/\.local$/i,/\.internal$/i,/\.localhost$/i];sa=["accounts.google.com","login.microsoftonline.com","login.live.com","auth0.com","okta.com","auth.mozilla.auth0.com","id.atlassian.com"],Oa=["login.","signin.","auth.","sso.","accounts.","idp."],Ta=["sign in to continue","log in to continue","authentication required","create an account to continue","subscribe to continue reading","members only"]});import{spawn as Ma}from"node:child_process";import{existsSync as Aa,mkdirSync as ut,writeFileSync as W}from"node:fs";import{join as C}from"node:path";import{fileURLToPath as Ja}from"node:url";var Yr=["fbclid","gclid","ref","ref_src","ref_url","source","utm_campaign","utm_content","utm_medium","utm_source","utm_term"],Vr=["dev.to","hashnode.com","medium.com","reddit.com","stackoverflow.com","stackexchange.com","substack.com"],Gr=["arstechnica.com","techcrunch.com","theverge.com","venturebeat.com","wired.com","zdnet.com"],ke=["facebook.com","instagram.com","linkedin.com","pinterest.com","tiktok.com","twitter.com","x.com"];function O(t="",e=240){let r=String(t).replaceAll(/\s+/g," ").trim();if(r.length<=e)return r;let n=r.slice(0,e),a=n.lastIndexOf(" ");return a>0?`${n.slice(0,a)}...`:`${n}...`}function Nt(t=""){let e=O(t,180);if(!e)return"";if(/^https?:\/\//i.test(e))return"";let r=e.split(/\s+/).filter(Boolean).length,n=/[A-Z]/.test(e),a=/\d/.test(e);return e===e.toLowerCase()&&r<=4&&!n&&!a?"":e}function st(t="",e=""){let r=Nt(t),n=Nt(e);if(!n)return r;if(!r)return n;let a=/^https?:\/\//i.test(r),l=/^https?:\/\//i.test(n);if(a&&!l)return n;if(!a&&l)return r;return n.length>r.length?n:r}function M(t){if(!t)return null;try{let e=new URL(t);if(!["http:","https:"].includes(e.protocol))return null;if(e.hash="",e.hostname=e.hostname.toLowerCase(),e.protocol==="https:"&&e.port==="443"||e.protocol==="http:"&&e.port==="80")e.port="";for(let a of[...e.searchParams.keys()]){let l=a.toLowerCase();if(Yr.includes(l)||l.startsWith("utm_"))e.searchParams.delete(a)}e.searchParams.sort();let r=e.pathname.replace(/\/{1,10}$/,"")||"/";e.pathname=r;let n=e.toString();return r==="/"?n.replace(/\/$/,""):n}catch{return null}}function Kr(t){try{return new URL(t).hostname.toLowerCase().replace(/^www\./,"")}catch{return""}}function rt(t,e){return e.some((r)=>t===r||t.endsWith(`.${r}`))}function Ot(t,e="",r=""){let n=e.toLowerCase(),a=r.toLowerCase();if(t==="github.com"||t==="gitlab.com")return"repo";if(t==="arxiv.org"||t==="doi.org"||t==="semanticscholar.org"||t.endsWith(".semanticscholar.org")||a.includes("/paper/")||a.includes("/pdf/"))return"academic";if(rt(t,ke))return"social";if(rt(t,Vr))return"community";if(rt(t,Gr))return"news";if(t.startsWith("docs.")||t.startsWith("developer.")||t.startsWith("developers.")||t.startsWith("api.")||n.includes("documentation")||n.includes("docs")||n.includes("reference")||a.includes("/docs/")||a.includes("/reference/")||a.includes("/api/"))return"official-docs";if(t.startsWith("blog.")||a.includes("/blog/"))return"maintainer-blog";return"website"}function Zr(t){switch(t){case"official-docs":return 5;case"repo":return 4;case"academic":return 4;case"maintainer-blog":return 3;case"website":return 2;case"community":return 1;case"news":return 0;case"social":return-6;default:return 0}}function dr(t){let e=Object.values(t.perEngine||{}).map((r)=>r?.rank||99);return e.length?Math.min(...e):99}var Br=["reddit.com","news.ycombinator.com","lobste.rs"];function X(t){return t.smartScore*3+t.engineCount*5+Zr(t.sourceType)*2+Math.max(0,7-dr(t))}function Ur(t){let e=t.toLowerCase(),r=[];if(e.includes("openai")||e.includes("gpt")||e.includes("chatgpt"))r.push("openai.com","platform.openai.com","help.openai.com");if(e.includes("anthropic")||e.includes("claude"))r.push("anthropic.com","docs.anthropic.com");if(e.includes("bun"))r.push("bun.sh","bun.com");if(e.includes("next.js")||e.includes("nextjs"))r.push("nextjs.org","vercel.com");if(e.includes("playwright"))r.push("playwright.dev");if(e.includes("supabase"))r.push("supabase.com","supabase.io");if(e.includes("prisma"))r.push("prisma.io");if(e.includes("tailwind"))r.push("tailwindcss.com");if(e.includes("vite"))r.push("vitejs.dev","vite.dev");if(e.includes("astro"))r.push("astro.build");if(e.includes("svelte"))r.push("svelte.dev");if(e.includes("solid"))r.push("solidjs.com");if(e.includes("vue")||e.includes("nuxt"))r.push("vuejs.org","nuxt.com");if(e.includes("react")||e.includes("react native"))r.push("react.dev","reactnative.dev");if(e.includes("angular"))r.push("angular.io","angular.dev");if(e.includes("node.js")||e.includes("nodejs"))r.push("nodejs.org","nodejs.dev","npmjs.com");if(/\bgo\b/.test(e)||e.includes("golang"))r.push("go.dev","golang.org","pkg.go.dev");if(e.includes("deno"))r.push("deno.land","deno.com");if(e.includes("fresh"))r.push("fresh.deno.dev");if(e.includes("typescript")||e.includes("ts"))r.push("typescriptlang.org");if(e.includes("python"))r.push("python.org","docs.python.org");if(e.includes("rust"))r.push("rust-lang.org","docs.rs","crates.io");if(e.includes("zig"))r.push("ziglang.org");if(e.includes("docker"))r.push("docker.com","docs.docker.com","hub.docker.com");if(e.includes("kubernetes")||e.includes("k8s"))r.push("kubernetes.io","k8s.io");if(e.includes("postgres")||e.includes("postgresql"))r.push("postgresql.org","neon.tech","supabase.com");if(e.includes("redis"))r.push("redis.io");if(e.includes("sqlite"))r.push("sqlite.org");if(e.includes("cloudflare"))r.push("developers.cloudflare.com","cloudflare.com");if(e.includes("vercel"))r.push("vercel.com","nextjs.org");if(e.includes("netlify"))r.push("netlify.com","docs.netlify.com");if(e.includes("stripe"))r.push("stripe.com","docs.stripe.com");if(e.includes("github"))r.push("github.com","docs.github.com");if(e.includes("gitlab"))r.push("gitlab.com","docs.gitlab.com");if(e.includes("aws"))r.push("aws.amazon.com","docs.aws.amazon.com");if(e.includes("azure"))r.push("azure.microsoft.com","learn.microsoft.com");if(e.includes("gcp")||e.includes("google cloud"))r.push("cloud.google.com","developers.google.com");if(e.includes("gemini")||e.includes("google ai"))r.push("ai.google.dev","developers.google.com");for(let n of ke){let a=n.replace(/\.com$/,"");if(e.includes(a))r.push(n)}return[...new Set(r)]}function be(t,e){return t===e||t.endsWith(`.${e}`)}function Tt(t,e=""){let r=new Map,n=Object.keys(t||{}).filter((g)=>!g.startsWith("_")),a=Ur(e);for(let g of n){let o=t[g];if(!o?.sources)continue;for(let h=0;h<o.sources.length;h++){let m=o.sources[h],c=M(m.url);if(!c||c.length<10)continue;let y=Nt(m.title||""),b=Kr(c),w=Ot(b,y,c),_=0;if(a.some((s)=>be(b,s)))_+=10;if(w==="official-docs")_+=3;let N=c.toLowerCase();if(/\/docs\/|\/documentation\/|\.dev\/|\/api\/|\/reference\//.test(N))_+=2;let k=a.some((s)=>be(b,s));if(w==="social"&&!k)_-=20;if(a.length>0){if(rt(b,Br))_-=3;else if(w==="community"&&!rt(b,["stackoverflow.com","stackexchange.com"]))_-=1}let v=r.get(c)||{id:"",canonicalUrl:c,displayUrl:m.url||c,domain:b,title:"",engines:[],engineCount:0,perEngine:{},sourceType:w,isOfficial:w==="official-docs",smartScore:0};if(v.title=st(v.title,y),v.displayUrl=v.displayUrl||m.url||c,v.sourceType=v.sourceType||w,v.isOfficial=v.isOfficial||w==="official-docs",v.smartScore=Math.max(v.smartScore,_),!v.engines.includes(g))v.engines.push(g);v.perEngine[g]={rank:h+1,title:st(v.perEngine[g]?.title||"",y)},r.set(c,v)}}let l=Array.from(r.values()).map((g)=>({...g,engineCount:g.engines.length})),i=l.filter((g)=>g.sourceType!=="social"),f=l.filter((g)=>g.sourceType==="social");return i.sort((g,o)=>{let h=X(o)-X(g);if(h!==0)return h;return g.domain.localeCompare(o.domain)}),f.sort((g,o)=>{let h=X(o)-X(g);if(h!==0)return h;return g.domain.localeCompare(o.domain)}),[...i,...f].slice(0,12).map((g,o)=>({...g,id:`S${o+1}`,title:g.title||g.domain||g.canonicalUrl}))}function ye(t,e){let r=new Map(e.map((n)=>[n.id,n]));return t.map((n)=>{let a=r.get(n.id);if(!a)return n;let l=st(n.title,a.title||"");return{...n,title:l||n.title,fetch:{attempted:!0,ok:!a.error&&a.contentChars>100,status:a.status||null,finalUrl:a.finalUrl||a.url||n.canonicalUrl,contentType:a.contentType||"",lastModified:a.lastModified||"",publishedTime:a.publishedTime||"",byline:a.byline||"",siteName:a.siteName||"",lang:a.lang||"",title:a.title||"",snippet:a.snippet||"",contentChars:a.contentChars||0,source:a.source||"unknown",duration:a.duration||0,error:a.error||""}}})}import{existsSync as pt,mkdirSync as Ir,readFileSync as _e,writeFileSync as Xr}from"node:fs";import{homedir as xr}from"node:os";import{join as ve}from"node:path";import{tmpdir as ot}from"node:os";var ht=`${ot().replaceAll("\\","/")}/greedysearch-chrome-profile`,Ll=`${ht}/DevToolsActivePort`,Wl=`${ot().replaceAll("\\","/")}/cdp-pages.json`,Ml=`${ot().replaceAll("\\","/")}/greedysearch-chrome-mode`,Al=`${ot().replaceAll("\\","/")}/greedysearch-visible-recovery.jsonl`,Lt=ve(xr(),".dm"),V=ve(Lt,"greedyconfig"),Wt=["perplexity","google","chatgpt"],Mt="gemini";function Qr(){try{if(pt(V)){let t=_e(V,"utf8"),e=JSON.parse(t);if(Array.isArray(e.engines)&&e.engines.length>0&&e.engines.every((r)=>typeof r==="string")){let r=e.engines.filter((a)=>Pt[a]),n=e.engines.filter((a)=>!Pt[a]);if(n.length>0)process.stderr.write(`[greedysearch] Warning: ignoring unknown engine(s) in ${V}: ${n.join(", ")}
330
- [greedysearch] Available engines: ${Object.keys(Pt).join(", ")}
331
- `);if(r.length>0)return r;process.stderr.write(`[greedysearch] Warning: no valid engines in ${V}, falling back to defaults: ${Wt.join(", ")}
332
- `)}}}catch{}return Wt}function zr(){try{if(!pt(Lt))Ir(Lt,{recursive:!0});if(!pt(V))Xr(V,JSON.stringify({engines:Wt,synthesizer:Mt},null,2)+`
333
- `,"utf8")}catch{}}zr();var nt=["gemini","chatgpt"];function Sr(){try{if(pt(V)){let t=_e(V,"utf8"),e=JSON.parse(t);if(typeof e.synthesizer==="string"){let r=e.synthesizer.toLowerCase();if(nt.includes(r))return r;process.stderr.write(`[greedysearch] Warning: unknown synthesizer "${e.synthesizer}" in ${V}
329
+ `).trim(),i=n.publishedTime||$r(r)||"";return{title:n.title||r.title||e,byline:n.byline||"",siteName:n.siteName||"",lang:n.lang||"",publishedTime:i,markdown:l,excerpt:l.slice(0,300).replaceAll(/\n/g," ")}}let a=r.body;if(a){let l=a.cloneNode(!0);l.querySelectorAll("script, style, nav, footer, header, aside").forEach((f)=>f.remove());let i=(l.textContent||"").replaceAll(/\s+/g," ").trim();return{title:r.title||e,byline:"",siteName:"",lang:"",publishedTime:$r(r),markdown:i,excerpt:i.slice(0,300)}}return{title:e,byline:"",siteName:"",lang:"",publishedTime:"",markdown:"",excerpt:""}}function vr(t){let e=t.markdown.trim().toLowerCase(),r=(t.title||"").toLowerCase();if(t.markdown.trim().length<100)return{ok:!1,reason:"content too short (< 100 chars)"};let n=e.toLowerCase(),a=[{check:()=>n.includes("loading")&&n.includes("please wait"),desc:"loading page"},{check:()=>n.includes("please ensure javascript is enabled"),desc:"requires javascript"},{check:()=>n.includes("enable javascript to view"),desc:"requires javascript"},{check:()=>n.includes("just a moment"),desc:"cloudflare challenge detected in content"},{check:()=>n.includes("verify you are human"),desc:"human verification"},{check:()=>n.includes("captcha required"),desc:"captcha in extracted content"},{check:()=>n.includes("access denied"),desc:"access denied in content"},{check:()=>/^\s{0,10}sign\s{1,5}in\s{0,10}$|^\s{0,10}log\s{1,5}in\s{0,10}$/im.test(e),desc:"login form only"}];for(let{check:l,desc:i}of a)if(l())return{ok:!1,reason:i};if(r.includes("just a moment")||r.includes("checking your browser"))return{ok:!1,reason:"cloudflare challenge page detected in title"};return{ok:!0}}function Fa(t){try{let e=new URL(t),r=e.hostname.toLowerCase(),n=e.pathname.toLowerCase();if(["react.dev","nextjs.org","vuejs.org","angular.io","svelte.dev","docs.expo.dev","tailwindcss.com","storybook.js.org"].some((a)=>r===a||r.endsWith(`.${a}`)))return!0;if(n.includes("/playground")||n.includes("/demo")||n.includes("/app"))return!0;if(e.hash&&e.hash.length>1)return!0;return!1}catch{return!1}}var cr,mr="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36",br,Pa,Ma,Aa,Ra;var Dr=gt(()=>{cr=new Oa({headingStyle:"atx",bulletListMarker:"-",codeBlockStyle:"fenced"});cr.addRule("removeDataUrls",{filter:(t)=>t.tagName==="IMG"&&t.getAttribute("src")?.startsWith("data:"),replacement:()=>""});br={"user-agent":mr,accept:"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8","accept-language":"en-US,en;q=0.9","accept-encoding":"gzip, deflate, br","cache-control":"no-cache",pragma:"no-cache","sec-ch-ua":'"Chromium";v="122", "Not(A:Brand";v="24", "Google Chrome";v="122"',"sec-ch-ua-mobile":"?0","sec-ch-ua-platform":'"Windows"',"sec-fetch-dest":"document","sec-fetch-mode":"navigate","sec-fetch-site":"none","sec-fetch-user":"?1","upgrade-insecure-requests":"1"},Pa=[/^localhost$/i,/^127\.\d+\.\d+\.\d+$/,/^0\.0\.0\.0$/,/^\[::1\]$/,/^10\./,/^172\.(1[6-9]|2\d|3[01])\./,/^192\.168\./,/^169\.254\./,/^fc00:/i,/^fe80:/i,/\.local$/i,/\.internal$/i,/\.localhost$/i];Ma=["accounts.google.com","login.microsoftonline.com","login.live.com","auth0.com","okta.com","auth.mozilla.auth0.com","id.atlassian.com"],Aa=["login.","signin.","auth.","sso.","accounts.","idp."],Ra=["sign in to continue","log in to continue","authentication required","create an account to continue","subscribe to continue reading","members only"]});import{spawn as Ya}from"node:child_process";import{existsSync as Ga,mkdirSync as qt,writeFileSync as W}from"node:fs";import{join as D}from"node:path";import{fileURLToPath as Va}from"node:url";var Zr=["fbclid","gclid","ref","ref_src","ref_url","source","utm_campaign","utm_content","utm_medium","utm_source","utm_term"],Br=["dev.to","hashnode.com","medium.com","reddit.com","stackoverflow.com","stackexchange.com","substack.com"],Kr=["arstechnica.com","techcrunch.com","theverge.com","venturebeat.com","wired.com","zdnet.com"],_e=["facebook.com","instagram.com","linkedin.com","pinterest.com","tiktok.com","twitter.com","x.com"];function T(t="",e=240){let r=String(t).replaceAll(/\s+/g," ").trim();if(r.length<=e)return r;let n=r.slice(0,e),a=n.lastIndexOf(" ");return a>0?`${n.slice(0,a)}...`:`${n}...`}function Nt(t=""){let e=T(t,180);if(!e)return"";if(/^https?:\/\//i.test(e))return"";let r=e.split(/\s+/).filter(Boolean).length,n=/[A-Z]/.test(e),a=/\d/.test(e);return e===e.toLowerCase()&&r<=4&&!n&&!a?"":e}function Tt(t="",e=""){let r=Nt(t),n=Nt(e);if(!n)return r;if(!r)return n;let a=/^https?:\/\//i.test(r),l=/^https?:\/\//i.test(n);if(a&&!l)return n;if(!a&&l)return r;return n.length>r.length?n:r}function M(t){if(!t)return null;try{let e=new URL(t);if(!["http:","https:"].includes(e.protocol))return null;if(e.hash="",e.hostname=e.hostname.toLowerCase(),e.protocol==="https:"&&e.port==="443"||e.protocol==="http:"&&e.port==="80")e.port="";for(let a of[...e.searchParams.keys()]){let l=a.toLowerCase();if(Zr.includes(l)||l.startsWith("utm_"))e.searchParams.delete(a)}e.searchParams.sort();let r=e.pathname.replace(/\/{1,10}$/,"")||"/";e.pathname=r;let n=e.toString();return r==="/"?n.replace(/\/$/,""):n}catch{return null}}function Ur(t){try{return new URL(t).hostname.toLowerCase().replace(/^www\./,"")}catch{return""}}function rt(t,e){return e.some((r)=>t===r||t.endsWith(`.${r}`))}function Ot(t,e="",r=""){let n=e.toLowerCase(),a=r.toLowerCase();if(t==="github.com"||t==="gitlab.com")return"repo";if(t==="arxiv.org"||t==="doi.org"||t==="semanticscholar.org"||t.endsWith(".semanticscholar.org")||a.includes("/paper/")||a.includes("/pdf/"))return"academic";if(rt(t,_e))return"social";if(rt(t,Br))return"community";if(rt(t,Kr))return"news";if(t.startsWith("docs.")||t.startsWith("developer.")||t.startsWith("developers.")||t.startsWith("api.")||n.includes("documentation")||n.includes("docs")||n.includes("reference")||a.includes("/docs/")||a.includes("/reference/")||a.includes("/api/"))return"official-docs";if(t.startsWith("blog.")||a.includes("/blog/"))return"maintainer-blog";return"website"}function Ir(t){switch(t){case"official-docs":return 5;case"repo":return 4;case"academic":return 4;case"maintainer-blog":return 3;case"website":return 2;case"community":return 1;case"news":return 0;case"social":return-6;default:return 0}}function Xr(t){let e=Object.values(t.perEngine||{}).map((r)=>r?.rank||99);return e.length?Math.min(...e):99}var xr=["reddit.com","news.ycombinator.com","lobste.rs"];function X(t){return t.smartScore*3+t.engineCount*5+Ir(t.sourceType)*2+Math.max(0,7-Xr(t))}function Qr(t){let e=t.toLowerCase(),r=[];if(e.includes("openai")||e.includes("gpt")||e.includes("chatgpt"))r.push("openai.com","platform.openai.com","help.openai.com");if(e.includes("anthropic")||e.includes("claude"))r.push("anthropic.com","docs.anthropic.com");if(e.includes("bun"))r.push("bun.sh","bun.com");if(e.includes("next.js")||e.includes("nextjs"))r.push("nextjs.org","vercel.com");if(e.includes("playwright"))r.push("playwright.dev");if(e.includes("supabase"))r.push("supabase.com","supabase.io");if(e.includes("prisma"))r.push("prisma.io");if(e.includes("tailwind"))r.push("tailwindcss.com");if(e.includes("vite"))r.push("vitejs.dev","vite.dev");if(e.includes("astro"))r.push("astro.build");if(e.includes("svelte"))r.push("svelte.dev");if(e.includes("solid"))r.push("solidjs.com");if(e.includes("vue")||e.includes("nuxt"))r.push("vuejs.org","nuxt.com");if(e.includes("react")||e.includes("react native"))r.push("react.dev","reactnative.dev");if(e.includes("angular"))r.push("angular.io","angular.dev");if(e.includes("node.js")||e.includes("nodejs"))r.push("nodejs.org","nodejs.dev","npmjs.com");if(/\bgo\b/.test(e)||e.includes("golang"))r.push("go.dev","golang.org","pkg.go.dev");if(e.includes("deno"))r.push("deno.land","deno.com");if(e.includes("fresh"))r.push("fresh.deno.dev");if(e.includes("typescript")||e.includes("ts"))r.push("typescriptlang.org");if(e.includes("python"))r.push("python.org","docs.python.org");if(e.includes("rust"))r.push("rust-lang.org","docs.rs","crates.io");if(e.includes("zig"))r.push("ziglang.org");if(e.includes("docker"))r.push("docker.com","docs.docker.com","hub.docker.com");if(e.includes("kubernetes")||e.includes("k8s"))r.push("kubernetes.io","k8s.io");if(e.includes("postgres")||e.includes("postgresql"))r.push("postgresql.org","neon.tech","supabase.com");if(e.includes("redis"))r.push("redis.io");if(e.includes("sqlite"))r.push("sqlite.org");if(e.includes("cloudflare"))r.push("developers.cloudflare.com","cloudflare.com");if(e.includes("vercel"))r.push("vercel.com","nextjs.org");if(e.includes("netlify"))r.push("netlify.com","docs.netlify.com");if(e.includes("stripe"))r.push("stripe.com","docs.stripe.com");if(e.includes("github"))r.push("github.com","docs.github.com");if(e.includes("gitlab"))r.push("gitlab.com","docs.gitlab.com");if(e.includes("aws"))r.push("aws.amazon.com","docs.aws.amazon.com");if(e.includes("azure"))r.push("azure.microsoft.com","learn.microsoft.com");if(e.includes("gcp")||e.includes("google cloud"))r.push("cloud.google.com","developers.google.com");if(e.includes("gemini")||e.includes("google ai"))r.push("ai.google.dev","developers.google.com");for(let n of _e){let a=n.replace(/\.com$/,"");if(e.includes(a))r.push(n)}return[...new Set(r)]}function ye(t,e){return t===e||t.endsWith(`.${e}`)}function Pt(t,e=""){let r=new Map,n=Object.keys(t||{}).filter((g)=>!g.startsWith("_")),a=Qr(e);for(let g of n){let p=t[g];if(!p?.sources)continue;for(let h=0;h<p.sources.length;h++){let m=p.sources[h],c=M(m.url);if(!c||c.length<10)continue;let y=Nt(m.title||""),b=Ur(c),w=Ot(b,y,c),k=0;if(a.some((N)=>ye(b,N)))k+=10;if(w==="official-docs")k+=3;let C=c.toLowerCase();if(/\/docs\/|\/documentation\/|\.dev\/|\/api\/|\/reference\//.test(C))k+=2;let _=a.some((N)=>ye(b,N));if(w==="social"&&!_)k-=20;if(a.length>0){if(rt(b,xr))k-=3;else if(w==="community"&&!rt(b,["stackoverflow.com","stackexchange.com"]))k-=1}let s=r.get(c)||{id:"",canonicalUrl:c,displayUrl:m.url||c,domain:b,title:"",engines:[],engineCount:0,perEngine:{},sourceType:w,isOfficial:w==="official-docs",smartScore:0};if(s.title=Tt(s.title,y),s.displayUrl=s.displayUrl||m.url||c,s.sourceType=s.sourceType||w,s.isOfficial=s.isOfficial||w==="official-docs",s.smartScore=Math.max(s.smartScore,k),!s.engines.includes(g))s.engines.push(g);s.perEngine[g]={rank:h+1,title:Tt(s.perEngine[g]?.title||"",y)},r.set(c,s)}}let l=Array.from(r.values()).map((g)=>({...g,engineCount:g.engines.length})),i=l.filter((g)=>g.sourceType!=="social"),f=l.filter((g)=>g.sourceType==="social");return i.sort((g,p)=>{let h=X(p)-X(g);if(h!==0)return h;return g.domain.localeCompare(p.domain)}),f.sort((g,p)=>{let h=X(p)-X(g);if(h!==0)return h;return g.domain.localeCompare(p.domain)}),[...i,...f].slice(0,12).map((g,p)=>({...g,id:`S${p+1}`,title:g.title||g.domain||g.canonicalUrl}))}function ke(t,e){let r=new Map(e.map((n)=>[n.id,n]));return t.map((n)=>{let a=r.get(n.id);if(!a)return n;let l=Tt(n.title,a.title||"");return{...n,title:l||n.title,fetch:{attempted:!0,ok:!a.error&&a.contentChars>100,status:a.status||null,finalUrl:a.finalUrl||a.url||n.canonicalUrl,contentType:a.contentType||"",lastModified:a.lastModified||"",publishedTime:a.publishedTime||"",byline:a.byline||"",siteName:a.siteName||"",lang:a.lang||"",title:a.title||"",snippet:a.snippet||"",contentChars:a.contentChars||0,source:a.source||"unknown",duration:a.duration||0,error:a.error||""}}})}import{existsSync as ot,mkdirSync as zr,readFileSync as se,writeFileSync as Sr}from"node:fs";import{homedir as ur}from"node:os";import{join as ve}from"node:path";import{tmpdir as pt}from"node:os";var ht=`${pt().replaceAll("\\","/")}/greedysearch-chrome-profile`,Hl=`${ht}/DevToolsActivePort`,Fl=`${pt().replaceAll("\\","/")}/cdp-pages.json`,Yl=`${pt().replaceAll("\\","/")}/greedysearch-chrome-mode`,Gl=`${pt().replaceAll("\\","/")}/greedysearch-visible-recovery.jsonl`,Wt=ve(ur(),".dm"),G=ve(Wt,"greedyconfig"),Mt=["perplexity","google","chatgpt"],At="gemini";function qr(){try{if(ot(G)){let t=se(G,"utf8"),e=JSON.parse(t);if(Array.isArray(e.engines)&&e.engines.length>0&&e.engines.every((r)=>typeof r==="string")){let r=e.engines.filter((a)=>Lt[a]),n=e.engines.filter((a)=>!Lt[a]);if(n.length>0)process.stderr.write(`[greedysearch] Warning: ignoring unknown engine(s) in ${G}: ${n.join(", ")}
330
+ [greedysearch] Available engines: ${Object.keys(Lt).join(", ")}
331
+ `);if(r.length>0)return r;process.stderr.write(`[greedysearch] Warning: no valid engines in ${G}, falling back to defaults: ${Mt.join(", ")}
332
+ `)}}}catch{}return Mt}function tn(){try{if(!ot(Wt))zr(Wt,{recursive:!0});if(!ot(G))Sr(G,JSON.stringify({engines:Mt,synthesizer:At},null,2)+`
333
+ `,"utf8")}catch{}}tn();var nt=["gemini","chatgpt"];function en(){try{if(ot(G)){let t=se(G,"utf8"),e=JSON.parse(t);if(typeof e.synthesizer==="string"){let r=e.synthesizer.toLowerCase();if(nt.includes(r))return r;process.stderr.write(`[greedysearch] Warning: unknown synthesizer "${e.synthesizer}" in ${G}
334
334
  [greedysearch] Available synthesizers: ${nt.join(", ")}
335
- [greedysearch] Falling back to default: ${Mt}
336
- `)}}}catch{}return Mt}var Pt={perplexity:"perplexity.mjs",p:"perplexity.mjs",bing:"bing-copilot.mjs",b:"bing-copilot.mjs",google:"google-ai.mjs",g:"google-ai.mjs",gemini:"gemini.mjs",gem:"gemini.mjs",chatgpt:"chatgpt.mjs",gpt:"chatgpt.mjs","semantic-scholar":"semantic-scholar.mjs",semanticscholar:"semantic-scholar.mjs",s2:"semantic-scholar.mjs",logically:"logically.mjs",log:"logically.mjs"},je=Qr(),At=je,Jl=Sr(),Rl=Math.max(1,Number.parseInt(process.env.GREEDY_FETCH_CONCURRENCY||"5",10)||5);process.env.CDP_PROFILE_DIR=ht;function ur(t){let e="",r=!1,n=!1;for(let a of String(t)){if(n){e+=a,n=!1;continue}if(a==="\\"){e+=a,n=!0;continue}if(a==='"'){r=!r,e+=a;continue}if(r&&a===`
337
- `)e+="\\n";else if(r&&a==="\r")e+="\\r";else if(r&&a==="\t")e+="\\t";else e+=a}return e}function wt(t){if(!t)return null;let e=String(t).trim(),r=e.indexOf("BEGIN_JSON"),n=e.indexOf("END_JSON");if(r!==-1&&n!==-1&&r<n)e=e.slice(r+10,n).trim();else{let f=e.indexOf("{");if(f>0)e=e.slice(f)}let a=[e,e.replace(/^```json\s*/i,"").replace(/^```\s*/i,"").replace(/```$/i,"").trim()],l=e.indexOf("{"),i=e.lastIndexOf("}");if(l!==-1&&i!==-1&&l<i)a.push(e.slice(l,i+1));for(let f of[...a]){let p=ur(f);if(p!==f)a.push(p)}for(let f of a)try{return JSON.parse(f)}catch{}return null}import{spawn as qr}from"node:child_process";import{join as tn}from"node:path";var en=import.meta.dirname||new URL(".",import.meta.url).pathname.replace(/^\/([A-Z]:)/,"$1"),rn={gemini:"gemini.mjs",chatgpt:"chatgpt.mjs"};function nn(t="gemini"){let e=String(t||"gemini").toLowerCase();if(e==="gem")return"gemini";if(e==="gpt")return"chatgpt";return e}async function an(t,e,{tabPrefix:r=null,timeoutMs:n=180000,visible:a=null}={}){let l=nn(t),i=rn[l];if(!i||!nt.includes(l))throw Error(`Unsupported synthesizer "${t}". Supported: ${nt.join(", ")}`);return new Promise((f,p)=>{let g=r?["--tab",String(r)]:[],o={...process.env,CDP_PROFILE_DIR:ht};if(a!==!0)delete o.GREEDY_SEARCH_VISIBLE,delete o.GREEDY_SEARCH_ALWAYS_VISIBLE;else o.GREEDY_SEARCH_VISIBLE="1",o.GREEDY_SEARCH_ALWAYS_VISIBLE="1";let h=qr(nodeRuntimeCommand(),[tn(en,"..","..","extractors",i),"--stdin",...g],{stdio:["pipe","pipe","pipe"],env:o});h.stdin.write(e),h.stdin.end();let m="",c="";h.stdout.on("data",(b)=>m+=b),h.stderr.on("data",(b)=>c+=b);let y=setTimeout(()=>{h.kill(),p(Error(`${l} prompt timed out after ${n/1000}s`))},n);h.on("close",(b)=>{if(clearTimeout(y),b!==0){p(Error(c.trim()||`${l} extractor failed`));return}try{f(JSON.parse(m.trim()))}catch{p(Error(`bad JSON from ${l}: ${m.slice(0,100)}`))}})})}async function x(t,e={}){return an("gemini",t,e)}import{basename as ln}from"node:path";function De(t=process.env,e=process.execPath){let r=t.GREEDY_SEARCH_NODE||t.NODE_BINARY||t.NODE;if(r?.trim())return r.trim();let n=ln(e||"").toLowerCase();if(n==="node"||n==="node.exe")return e;return"node"}var Ra=Ja(new URL(".",import.meta.url)).replace(/^\/([A-Z]:)/,"$1");function Ha(t=Ra){let e=[C(t,"search.mjs"),C(t,"..","bin","search.mjs"),C(t,"..","..","bin","search.mjs")];for(let r of e)if(Aa(r))return r;return e[e.length-1]}var Fa=Ha(),Ya=C(process.cwd(),".dm","greedysearch-research");function Va(t){return String(t||"research").toLowerCase().replaceAll(/[^a-z0-9]+/g,"-").replaceAll(/^-|-$/g,"").slice(0,60)||"research"}function d(t,e=1/0){let r=new Set,n=[];for(let a of t||[]){let l=O(String(a||""),1000);if(!l||r.has(l))continue;if(r.add(l),n.push(l),n.length>=e)break}return n}async function Ga(...t){let{fetchMultipleSources:e}=await Promise.resolve().then(() => (rr(),er));return e(...t)}async function ne(...t){let{writeSourcesToFiles:e}=await Promise.resolve().then(() => (lr(),ar));return e(...t)}function Ka({breadth:t=3,iterations:e=2,maxSources:r}){let n=qt(t,1,5,3),a=qt(e,1,3,2),l=qt(r??Math.max(5,n*a*2),3,12,8);return{breadth:n,iterations:a,maxSources:l}}function qt(t,e,r,n){let a=Number.parseInt(String(t??""),10);if(!Number.isFinite(a))return n;return Math.min(r,Math.max(e,a))}function Za(t,e,r,{expand:n=!0,includeOriginal:a=!0,exclude:l=[]}={}){let i=Array.isArray(t?.queries)?t.queries:[],f=[],p=new Set([...l].map((g)=>A(g).toLowerCase()));for(let g of i){let o=typeof g==="string"?g:g?.query,h=typeof g==="string"?"":g?.researchGoal||"";te(f,o,h,{exclude:p})}if(a)te(f,e,"Original user query",{prepend:!0,exclude:p});if(n){let g=[{query:`${e} official docs GitHub`,researchGoal:"Find primary project docs, repository details, and maintainer claims."},{query:`${e} benchmarks limitations compatibility`,researchGoal:"Validate performance claims and uncover unsupported APIs or caveats."},{query:`${e} alternatives comparison production use cases`,researchGoal:"Compare against conventional headless browsers and identify when to choose it."},{query:`${e} anti bot detection Cloudflare screenshots visual rendering`,researchGoal:"Check automation risks, rendering gaps, screenshots, and bot-detection behavior."}];for(let o of g){if(f.length>=r)break;te(f,o.query,o.researchGoal,{exclude:p})}}return f.slice(0,r)}function te(t,e,r="",{prepend:n=!1,exclude:a=new Set}={}){if(!e||typeof e!=="string")return;let l=A(e);if(!l||a.has(l.toLowerCase())||t.some((f)=>f.query.toLowerCase()===l.toLowerCase()))return;let i={query:l,researchGoal:O(r,320)};if(n)t.unshift(i);else t.push(i)}function A(t){return Ba(da(String(t)))}function da(t){let e="",r=0;while(r<t.length){let n=t.indexOf("[",r);if(n===-1){e+=t.slice(r);break}let a=t.indexOf("]",n+1);if(a===-1||t[a+1]!=="("||a===n+1){e+=t.slice(r,n+1),r=n+1;continue}let l=t.indexOf(")",a+2);if(l===-1){e+=t.slice(r,n+1),r=n+1;continue}let i=t.slice(a+2,l).trimStart();if(!i.startsWith("http://")&&!i.startsWith("https://")){e+=t.slice(r,n+1),r=n+1;continue}e+=t.slice(r,n),e+=t.slice(n+1,a),r=l+1}return e}function Ba(t){let e="",r=!1;for(let n of t)if(n===" "||n==="\t"||n===`
338
- `||n==="\r"){if(!r)e+=" ";r=!0}else e+=n,r=!1;return e.trim()}function vr(t){return new Set(String(t).toLowerCase().normalize("NFD").replaceAll(/[\u0300-\u036f]/g,"").split(/[^\w]+/).filter((e)=>e.length>1))}function Dt(t,e){let r=vr(t),n=vr(e),a=new Set([...r,...n]).size;if(a===0)return 1;let l=0;for(let i of r)if(n.has(i))l++;return l/a}function Er(t,e,{threshold:r=0.75,roundIndex:n=0,originalQuery:a=null}={}){let l=A(t).toLowerCase();if(e.has(l))return!0;if(a&&n>0&&l===A(a).toLowerCase())return!0;for(let i of e)if(Dt(l,i)>=r)return!0;return!1}function Ua(t,e,r,n){let a=e.map((l)=>({queries:l.queries?.map((i)=>i.query||"")||[],learnings:l.learnings||[],gaps:l.gaps||[]}));return["You are evaluating the quality of an iterative research run.","Assess coverage across: official sources, limitations/risks, benchmarks/performance, production usage, and counter-evidence.","Score each dimension 0-10. Overall score 0-10.","Identify remaining knowledge gaps.","Propose targeted next actions (search queries or direct URL fetches) that would most improve the research.","Decide whether to continue or stop.","terminationReason must be one of: quality_threshold | max_rounds | no_novel_actions | insufficient_evidence.","",`Original research question: ${t}`,`Rounds completed: ${JSON.stringify(a,null,2)}`,`Accumulated learnings: ${JSON.stringify(r.slice(0,12),null,2)}`,`Known gaps: ${JSON.stringify(n.slice(0,8),null,2)}`,"","Respond ONLY with JSON wrapped in BEGIN_JSON / END_JSON markers:","BEGIN_JSON",JSON.stringify({score:7.5,coverage:{officialSources:8,limitations:5,benchmarks:7,productionUseCases:6,counterEvidence:4},knowledgeGaps:["specific gap or missing evidence"],shouldContinue:!0,terminationReason:"quality_threshold",nextActions:[{type:"search",query:"targeted search query"},{type:"fetchUrl",url:"https://example.com/primary-doc"}]},null,2),"END_JSON"].join(`
339
- `)}function Ia(t,e,r,n,a){let l=[],i=[{template:(f)=>`${f} official documentation`,label:"official docs"},{template:(f)=>`${f} GitHub issues discussions`,label:"community signals"},{template:(f)=>`${f} benchmarks performance comparison`,label:"benchmarks"},{template:(f)=>`${f} limitations risks caveats`,label:"limitations"},{template:(f)=>`${f} production deployment experience`,label:"production usage"},{template:(f)=>`${e} ${f} counter evidence`,label:"counter-evidence"}];for(let f=0;f<t.length&&l.length<n;f++){let p=t[f],g=i[f%i.length],o=g.template(p);if(!Er(o,r,{roundIndex:a}))l.push({query:o,researchGoal:`Gap-driven: ${p} (${g.label})`})}return l}async function Xa(t,e,r,n,a){try{let l=await x(Ua(t,e,r,n),{timeoutMs:120000}),i=lt(l,{}),f=typeof i.score==="number"?Math.min(10,Math.max(0,i.score)):a.length>0?a[a.length-1]:5,p=Array.isArray(i.knowledgeGaps)?i.knowledgeGaps.map((m)=>String(m)).filter(Boolean).slice(0,6):[],g=Array.isArray(i.nextActions)?i.nextActions.slice(0,5):[],o=typeof i.shouldContinue==="boolean"?i.shouldContinue:f<8,h=i.terminationReason||null;return{score:f,coverage:i.coverage||{},knowledgeGaps:p,shouldContinue:o,nextActions:g,terminationReason:h||(f>=8.5?"quality_threshold":null),evaluationError:""}}catch(l){return process.stderr.write(`[greedysearch] Quality evaluation failed: ${l.message}
340
- `),{score:a.length>0?a[a.length-1]:5,coverage:{},knowledgeGaps:[],shouldContinue:!0,nextActions:[],terminationReason:null,evaluationError:l.message}}}function xa(t){let e={};for(let r of Object.keys(t||{}).filter((n)=>!n.startsWith("_"))){let n=t?.[r];if(!n)continue;e[r]=n.error?{status:"error",error:String(n.error)}:{status:"ok",answer:O(n.answer||"",1400),sources:Array.isArray(n.sources)?n.sources.slice(0,5).map((a)=>({title:O(a.title||"",160),url:a.url||""})):[]}}return e}function Qa(t,e,r=[],n=[],a=[]){let l=n.length>0?`
335
+ [greedysearch] Falling back to default: ${At}
336
+ `)}}}catch{}return At}var Lt={perplexity:"perplexity.mjs",p:"perplexity.mjs",bing:"bing-copilot.mjs",b:"bing-copilot.mjs",google:"google-ai.mjs",g:"google-ai.mjs",gemini:"gemini.mjs",gem:"gemini.mjs",chatgpt:"chatgpt.mjs",gpt:"chatgpt.mjs","semantic-scholar":"semantic-scholar.mjs",semanticscholar:"semantic-scholar.mjs",s2:"semantic-scholar.mjs",logically:"logically.mjs",log:"logically.mjs"},je=qr(),Rt=je,Vl=en(),dl=Math.max(1,Number.parseInt(process.env.GREEDY_FETCH_CONCURRENCY||"5",10)||5);process.env.CDP_PROFILE_DIR=ht;function rn(t){let e="",r=!1,n=!1;for(let a of String(t)){if(n){e+=a,n=!1;continue}if(a==="\\"){e+=a,n=!0;continue}if(a==='"'){r=!r,e+=a;continue}if(r&&a===`
337
+ `)e+="\\n";else if(r&&a==="\r")e+="\\r";else if(r&&a==="\t")e+="\\t";else e+=a}return e}function wt(t){if(!t)return null;let e=String(t).trim(),r=e.indexOf("BEGIN_JSON"),n=e.indexOf("END_JSON");if(r!==-1&&n!==-1&&r<n)e=e.slice(r+10,n).trim();else{let f=e.indexOf("{");if(f>0)e=e.slice(f)}let a=[e,e.replace(/^```json\s*/i,"").replace(/^```\s*/i,"").replace(/```$/i,"").trim()],l=e.indexOf("{"),i=e.lastIndexOf("}");if(l!==-1&&i!==-1&&l<i)a.push(e.slice(l,i+1));for(let f of[...a]){let o=rn(f);if(o!==f)a.push(o)}for(let f of a)try{return JSON.parse(f)}catch{}return null}import{spawn as fn}from"node:child_process";import{existsSync as De}from"node:fs";import{dirname as nn,join as $t}from"node:path";import{fileURLToPath as an}from"node:url";function Ee(t){return an(new URL(".",t)).replace(/^\/([A-Z]:)/,"$1")}function ln(t,e=De){let r=new Set;for(let n of t.filter(Boolean)){if(r.has(n))continue;if(r.add(n),e(n))return n}return t.filter(Boolean).at(-1)}function Ce(t,{moduleDir:e,entrypoint:r=process.argv[1],env:n=process.env,exists:a=De}={}){let l=r?nn(r):null,i=n.GREEDY_SEARCH_EXTENSION_DIR?.trim()||null,f=[e?$t(e,"..","..","extractors",t):null,e?$t(e,"..","extractors",t):null,i?$t(i,"extractors",t):null,l?$t(l,"..","extractors",t):null];return ln(f,a)}var gn=Ee(import.meta.url),on={gemini:"gemini.mjs",chatgpt:"chatgpt.mjs"};function pn(t="gemini"){let e=String(t||"gemini").toLowerCase();if(e==="gem")return"gemini";if(e==="gpt")return"chatgpt";return e}async function hn(t,e,{tabPrefix:r=null,timeoutMs:n=180000,visible:a=null}={}){let l=pn(t),i=on[l];if(!i||!nt.includes(l))throw Error(`Unsupported synthesizer "${t}". Supported: ${nt.join(", ")}`);return new Promise((f,o)=>{let g=r?["--tab",String(r)]:[],p=Ce(i,{moduleDir:gn}),h={...process.env,CDP_PROFILE_DIR:ht};if(a!==!0)delete h.GREEDY_SEARCH_VISIBLE,delete h.GREEDY_SEARCH_ALWAYS_VISIBLE;else h.GREEDY_SEARCH_VISIBLE="1",h.GREEDY_SEARCH_ALWAYS_VISIBLE="1";let m=fn(nodeRuntimeCommand(),[p,"--stdin",...g],{stdio:["pipe","pipe","pipe"],env:h});m.stdin.write(e),m.stdin.end();let c="",y="";m.stdout.on("data",(w)=>c+=w),m.stderr.on("data",(w)=>y+=w);let b=setTimeout(()=>{m.kill(),o(Error(`${l} prompt timed out after ${n/1000}s`))},n);m.on("close",(w)=>{if(clearTimeout(b),w!==0){o(Error(y.trim()||`${l} extractor failed`));return}try{f(JSON.parse(c.trim()))}catch{o(Error(`bad JSON from ${l}: ${c.slice(0,100)}`))}})})}async function x(t,e={}){return hn("gemini",t,e)}import{basename as wn}from"node:path";function Ne(t=process.env,e=process.execPath){let r=t.GREEDY_SEARCH_NODE||t.NODE_BINARY||t.NODE;if(r?.trim())return r.trim();let n=wn(e||"").toLowerCase();if(n==="node"||n==="node.exe")return e;return"node"}var da=Va(new URL(".",import.meta.url)).replace(/^\/([A-Z]:)/,"$1");function Za(t=da){let e=[D(t,"search.mjs"),D(t,"..","bin","search.mjs"),D(t,"..","..","bin","search.mjs")];for(let r of e)if(Ga(r))return r;return e[e.length-1]}var Ba=Za(),Ka=D(process.cwd(),".dm","greedysearch-research");function Ua(t){return String(t||"research").toLowerCase().replaceAll(/[^a-z0-9]+/g,"-").replaceAll(/^-|-$/g,"").slice(0,60)||"research"}function B(t,e=1/0){let r=new Set,n=[];for(let a of t||[]){let l=T(String(a||""),1000);if(!l||r.has(l))continue;if(r.add(l),n.push(l),n.length>=e)break}return n}async function Ia(...t){let{fetchMultipleSources:e}=await Promise.resolve().then(() => (ir(),lr));return e(...t)}async function ae(...t){let{writeSourcesToFiles:e}=await Promise.resolve().then(() => (or(),gr));return e(...t)}function Xa({breadth:t=3,iterations:e=2,maxSources:r}){let n=te(t,1,5,3),a=te(e,1,3,2),l=te(r??Math.max(5,n*a*2),3,12,8);return{breadth:n,iterations:a,maxSources:l}}function te(t,e,r,n){let a=Number.parseInt(String(t??""),10);if(!Number.isFinite(a))return n;return Math.min(r,Math.max(e,a))}function xa(t,e,r,{expand:n=!0,includeOriginal:a=!0,exclude:l=[]}={}){let i=Array.isArray(t?.queries)?t.queries:[],f=[],o=new Set([...l].map((g)=>A(g).toLowerCase()));for(let g of i){let p=typeof g==="string"?g:g?.query,h=typeof g==="string"?"":g?.researchGoal||"";ee(f,p,h,{exclude:o})}if(a)ee(f,e,"Original user query",{prepend:!0,exclude:o});if(n){let g=[{query:`${e} official docs GitHub`,researchGoal:"Find primary project docs, repository details, and maintainer claims."},{query:`${e} benchmarks limitations compatibility`,researchGoal:"Validate performance claims and uncover unsupported APIs or caveats."},{query:`${e} alternatives comparison production use cases`,researchGoal:"Compare against conventional headless browsers and identify when to choose it."},{query:`${e} anti bot detection Cloudflare screenshots visual rendering`,researchGoal:"Check automation risks, rendering gaps, screenshots, and bot-detection behavior."}];for(let p of g){if(f.length>=r)break;ee(f,p.query,p.researchGoal,{exclude:o})}}return f.slice(0,r)}function ee(t,e,r="",{prepend:n=!1,exclude:a=new Set}={}){if(!e||typeof e!=="string")return;let l=A(e);if(!l||a.has(l.toLowerCase())||t.some((f)=>f.query.toLowerCase()===l.toLowerCase()))return;let i={query:l,researchGoal:T(r,320)};if(n)t.unshift(i);else t.push(i)}function A(t){return za(Qa(String(t)))}function Qa(t){let e="",r=0;while(r<t.length){let n=t.indexOf("[",r);if(n===-1){e+=t.slice(r);break}let a=t.indexOf("]",n+1);if(a===-1||t[a+1]!=="("||a===n+1){e+=t.slice(r,n+1),r=n+1;continue}let l=t.indexOf(")",a+2);if(l===-1){e+=t.slice(r,n+1),r=n+1;continue}let i=t.slice(a+2,l).trimStart();if(!i.startsWith("http://")&&!i.startsWith("https://")){e+=t.slice(r,n+1),r=n+1;continue}e+=t.slice(r,n),e+=t.slice(n+1,a),r=l+1}return e}function za(t){let e="",r=!1;for(let n of t)if(n===" "||n==="\t"||n===`
338
+ `||n==="\r"){if(!r)e+=" ";r=!0}else e+=n,r=!1;return e.trim()}function Er(t){return new Set(String(t).toLowerCase().normalize("NFD").replaceAll(/[\u0300-\u036f]/g,"").split(/[^\w]+/).filter((e)=>e.length>1))}function Dt(t,e){let r=Er(t),n=Er(e),a=new Set([...r,...n]).size;if(a===0)return 1;let l=0;for(let i of r)if(n.has(i))l++;return l/a}function Or(t,e,{threshold:r=0.75,roundIndex:n=0,originalQuery:a=null}={}){let l=A(t).toLowerCase();if(e.has(l))return!0;if(a&&n>0&&l===A(a).toLowerCase())return!0;for(let i of e)if(Dt(l,i)>=r)return!0;return!1}function Sa(t,e,r,n){let a=e.map((l)=>({queries:l.queries?.map((i)=>i.query||"")||[],learnings:l.learnings||[],gaps:l.gaps||[]}));return["You are evaluating the quality of an iterative research run.","Assess coverage across: official sources, limitations/risks, benchmarks/performance, production usage, and counter-evidence.","Score each dimension 0-10. Overall score 0-10.","Identify remaining knowledge gaps.","Propose targeted next actions (search queries or direct URL fetches) that would most improve the research.","Decide whether to continue or stop.","terminationReason must be one of: quality_threshold | max_rounds | no_novel_actions | insufficient_evidence.","",`Original research question: ${t}`,`Rounds completed: ${JSON.stringify(a,null,2)}`,`Accumulated learnings: ${JSON.stringify(r.slice(0,12),null,2)}`,`Known gaps: ${JSON.stringify(n.slice(0,8),null,2)}`,"","Respond ONLY with JSON wrapped in BEGIN_JSON / END_JSON markers:","BEGIN_JSON",JSON.stringify({score:7.5,coverage:{officialSources:8,limitations:5,benchmarks:7,productionUseCases:6,counterEvidence:4},knowledgeGaps:["specific gap or missing evidence"],shouldContinue:!0,terminationReason:"quality_threshold",nextActions:[{type:"search",query:"targeted search query"},{type:"fetchUrl",url:"https://example.com/primary-doc"}]},null,2),"END_JSON"].join(`
339
+ `)}function ua(t,e,r,n,a){let l=[],i=[{template:(f)=>`${f} official documentation`,label:"official docs"},{template:(f)=>`${f} GitHub issues discussions`,label:"community signals"},{template:(f)=>`${f} benchmarks performance comparison`,label:"benchmarks"},{template:(f)=>`${f} limitations risks caveats`,label:"limitations"},{template:(f)=>`${f} production deployment experience`,label:"production usage"},{template:(f)=>`${e} ${f} counter evidence`,label:"counter-evidence"}];for(let f=0;f<t.length&&l.length<n;f++){let o=t[f],g=i[f%i.length],p=g.template(o);if(!Or(p,r,{roundIndex:a}))l.push({query:p,researchGoal:`Gap-driven: ${o} (${g.label})`})}return l}async function qa(t,e,r,n,a){try{let l=await x(Sa(t,e,r,n),{timeoutMs:120000}),i=lt(l,{}),f=typeof i.score==="number"?Math.min(10,Math.max(0,i.score)):a.length>0?a[a.length-1]:5,o=Array.isArray(i.knowledgeGaps)?i.knowledgeGaps.map((m)=>String(m)).filter(Boolean).slice(0,6):[],g=Array.isArray(i.nextActions)?i.nextActions.slice(0,5):[],p=typeof i.shouldContinue==="boolean"?i.shouldContinue:f<8,h=i.terminationReason||null;return{score:f,coverage:i.coverage||{},knowledgeGaps:o,shouldContinue:p,nextActions:g,terminationReason:h||(f>=8.5?"quality_threshold":null),evaluationError:""}}catch(l){return process.stderr.write(`[greedysearch] Quality evaluation failed: ${l.message}
340
+ `),{score:a.length>0?a[a.length-1]:5,coverage:{},knowledgeGaps:[],shouldContinue:!0,nextActions:[],terminationReason:null,evaluationError:l.message}}}function tl(t){let e={};for(let r of Object.keys(t||{}).filter((n)=>!n.startsWith("_"))){let n=t?.[r];if(!n)continue;e[r]=n.error?{status:"error",error:String(n.error)}:{status:"ok",answer:T(n.answer||"",1400),sources:Array.isArray(n.sources)?n.sources.slice(0,5).map((a)=>({title:T(a.title||"",160),url:a.url||""})):[]}}return e}function el(t,e,r=[],n=[],a=[]){let l=n.length>0?`
341
341
  Known knowledge gaps to target:
342
342
  ${n.map((f)=>`- ${f}`).join(`
343
343
  `)}`:"",i=a.length>0?`
@@ -347,45 +347,45 @@ ${a.map((f)=>`- ${f}`).join(`
347
347
  Prior learnings to build on:
348
348
  ${r.map((f)=>`- ${f}`).join(`
349
349
  `)}`:"",l,i,"","Respond ONLY with JSON wrapped in BEGIN_JSON / END_JSON markers:","BEGIN_JSON",JSON.stringify({actions:[{type:"search",query:"specific search query",researchGoal:"what this action should clarify"},{type:"fetchUrl",url:"https://example.com/docs/relevant-page",researchGoal:"extract specific information from this page"}]},null,2),"END_JSON"].join(`
350
- `)}function Nr(t){if(!t||typeof t!=="object")return null;let e=t.type,r=O(t.researchGoal||"",320);if(e==="search"){if(t.query==null)return null;let n=A(t.query);return n?{type:"search",query:n,researchGoal:r}:null}if(e==="fetchUrl"){if(t.url==null)return null;let n=M(t.url);return n?{type:"fetchUrl",url:n,researchGoal:r}:null}return null}async function za(t,{locale:e=null,short:r=!0,usedQueries:n,usedUrls:a,maxChars:l=8000}={}){if(t.type==="search"){let i=A(t.query).toLowerCase();n.add(i);try{let f=await pl(t.query,{locale:e,short:r}),p=Tt(f,t.query);return{ok:!0,action:t,result:f,sources:p}}catch(f){return{ok:!1,action:t,error:f.message,sources:[]}}}if(t.type==="fetchUrl"){let i=M(t.url);if(a.has(i))return{ok:!1,action:t,error:`URL already fetched: ${i}`,sources:[]};try{let f=await Sa(i,l);a.add(i);let p=qa(i),g={id:"",canonicalUrl:f.finalUrl||i,displayUrl:f.url||i,domain:p,title:f.title||i,engines:["fetch"],engineCount:1,perEngine:{},sourceType:Ot(p,f.title||"",f.finalUrl||i),isOfficial:!1,smartScore:0,fetch:{attempted:!0,ok:!f.error&&(f.contentChars||0)>100,status:f.status||null,finalUrl:f.finalUrl||i,content:f.content||"",contentChars:f.contentChars||0,snippet:f.snippet||"",error:f.error||""}};return{ok:!0,action:t,result:null,sources:[g],fetchResult:{id:g.id,url:i,finalUrl:f.finalUrl||i,title:f.title||"",content:f.content||"",contentChars:f.contentChars||0,snippet:f.snippet||"",status:f.status||null,error:f.error||"",source:f.source||"http",duration:f.duration||0}}}catch(f){return{ok:!1,action:t,error:f.message,sources:[]}}}return{ok:!1,action:t,error:`Unknown action type: ${t.type}`,sources:[]}}async function Sa(t,e){return await ua(t,e)}async function ua(t,e=8000){let r=Date.now();try{let{parseGitHubUrl:n,fetchGitHubContent:a}=await Promise.resolve().then(() => (zt(),Qt)),l=n(t);if(l&&(l.type==="root"||l.type==="tree"||l.type==="blob"&&!l.path?.includes("."))){let i=await a(t);if(i.ok){let{trimContentHeadTail:f}=await Promise.resolve().then(() => St),p=f(i.content,e);return{url:t,finalUrl:t,status:200,title:i.title,snippet:p.slice(0,320),content:p,contentChars:p.length,source:"github-api",duration:Date.now()-r}}}}catch{}try{let{fetchSourceHttp:n}=await Promise.resolve().then(() => (_r(),yr)),{trimContentHeadTail:a}=await Promise.resolve().then(() => St),l=await n(t,{timeoutMs:1e4});if(l.ok){let i=a(l.markdown,e);return{url:t,finalUrl:l.finalUrl,status:l.status,title:l.title,snippet:l.excerpt,content:i,contentChars:i.length,source:"http",duration:Date.now()-r}}}catch{}return{url:t,title:"",content:"",contentChars:0,snippet:"",error:"HTTP fetch failed",source:"error",duration:Date.now()-r}}function qa(t){try{return new URL(t).hostname.toLowerCase().replace(/^www\./,"")}catch{return""}}async function tl(t,e){let r=[],{parseGitHubUrl:n}=await Promise.resolve().then(() => (zt(),Qt));for(let a of t){if(a.type!=="fetchUrl"){r.push(a);continue}let l=n(a.url);if(!l||l.type!=="root"){r.push(a);continue}let{owner:i,repo:f}=l,p=`https://github.com/${i}/${f}`;if(e.has(p))continue;let g=[p],o=[`${p}/blob/main/CONTRIBUTING.md`,`${p}/blob/master/CONTRIBUTING.md`,`${p}/blob/main/CHANGELOG.md`,`${p}/blob/master/CHANGELOG.md`,`${p}/blob/main/docs/README.md`];for(let h of o){if(g.length>=3)break;if(!e.has(h))g.push(h)}for(let h of g)r.push({type:"fetchUrl",url:h,researchGoal:a.researchGoal||`Fetch GitHub content for ${i}/${f}`})}return r}function el(t,e){let r=wt(t?.answer||"")||{},n=Array.isArray(r?.actions)?r.actions:[],a=[];for(let l of n){let i=Nr(l);if(i&&a.length<e)a.push(i)}return a}function rl(t){return(t||[]).map((e)=>({type:"search",query:typeof e==="string"?e:e.query,researchGoal:typeof e==="string"?"":e.researchGoal||""})).filter((e)=>e.query)}function jt(t){return M(t?.finalUrl||t?.canonicalUrl||t?.url||"")||t?.id||""}function nl(t,e,r,n=new Set){let a=(e||[]).filter((i)=>i.status!=="closed").slice(0,12).map((i)=>({id:i.id,question:i.question})),l=(r||[]).filter((i)=>i?.content||i?.snippet).filter((i)=>!n.has(jt(i))).slice(0,6).map((i,f)=>({id:i.id||`F${f+1}`,title:i.title||"",url:i.finalUrl||i.url||i.canonicalUrl||"",content:O(i.content||i.snippet||"",5000)}));return["You are doing goal-based evidence extraction for an iterative research run.","For each source, extract only information that helps answer the open questions.","Use original wording/details where useful. Do not invent answers; leave questions open if evidence is insufficient.","If a source answers one or more tracked questions, identify those question IDs explicitly.","Also propose genuinely new sub-questions discovered from the evidence.","",`Original research question: ${t}`,`Open question ledger: ${JSON.stringify(a,null,2)}`,`Fetched sources: ${JSON.stringify(l,null,2)}`,"","Respond ONLY with JSON wrapped in BEGIN_JSON / END_JSON markers:","BEGIN_JSON",JSON.stringify({extractions:[{sourceId:"S1",url:"https://example.com/source",rational:"why this source matters for the goal",evidence:"specific quoted/paraphrased evidence with numbers, dates, caveats",summary:"concise contribution to the research question",answers:[{id:"Q1",evidence:"brief evidence that closes the question"}],newQuestions:["new sub-question raised by this source"]}]},null,2),"END_JSON"].join(`
351
- `)}function al(t,e){let r=Array.isArray(t?.extractions)?t.extractions:[],n=new Map,a=new Map;for(let l of e||[]){if(l?.id)a.set(String(l.id),l);let i=jt(l);if(i)n.set(i,l)}return r.map((l)=>{let i=a.get(String(l?.sourceId||""))||n.get(M(l?.url||"")||""),f=String(l?.sourceId||i?.id||""),p=M(l?.url||i?.finalUrl||i?.url||""),g=Array.isArray(l?.answers)?l.answers.map((o)=>({id:String(o?.id||""),evidence:O(o?.evidence||"",500),sourceIds:[f].filter(Boolean)})).filter((o)=>o.id):[];return{sourceId:f,url:p,title:i?.title||l?.title||"",rational:O(l?.rational||"",700),evidence:O(l?.evidence||"",1600),summary:O(l?.summary||"",700),answers:g,newQuestions:d(l?.newQuestions||[],6)}}).filter((l)=>l.sourceId||l.url||l.summary||l.evidence)}async function ll({query:t,questions:e,fetchedSources:r,extractedSourceKeys:n}){let a=(r||[]).filter((l)=>(l?.content||l?.snippet)&&!n.has(jt(l)));if(a.length===0)return{evidence:[],error:""};try{let l=await x(nl(t,e,a,n),{timeoutMs:120000}),i=lt(l,{extractions:[]}),f=al(i,a);for(let p of a){let g=jt(p);if(g)n.add(g)}return{evidence:f,error:""}}catch(l){return{evidence:[],error:l.message||String(l)}}}function il(t,e,r,n,a=[],l=[]){let i=n.filter((f)=>f?.content||f?.snippet).slice(0,10).map((f,p)=>({id:`F${p+1}`,title:f.title||"",url:f.finalUrl||f.url||"",snippet:O(f.content||f.snippet||"",3000)}));return["You are extracting compact research state from live multi-engine search results.","Create dense, non-overlapping learnings with exact names, numbers, dates, limitations, and caveats where available.","Also propose follow-up search queries that would most improve confidence or fill gaps.","",`Original research question: ${t}`,`Round queries: ${JSON.stringify(e,null,2)}`,`Question ledger: ${JSON.stringify(a,null,2)}`,`Extracted source evidence: ${JSON.stringify(l.slice(-12),null,2)}`,`Engine summaries: ${JSON.stringify(r,null,2)}`,`Fetched source snippets: ${JSON.stringify(i,null,2)}`,"","Respond ONLY with JSON wrapped in BEGIN_JSON / END_JSON markers:","BEGIN_JSON",JSON.stringify({learnings:["concise, information-dense learning"],answeredQuestions:[{id:"Q1",evidence:"brief evidence that closes this question",sourceIds:["S1"]}],newQuestions:["new sub-question discovered from the evidence"],followUpQueries:["specific next search query"],gaps:["important uncertainty or missing evidence"]},null,2),"END_JSON"].join(`
352
- `)}function fl(t,e,r,n=[],a=[]){let l=e.flatMap((p)=>p.learnings||[]),i=e.flatMap((p)=>p.gaps||[]),f=r.slice(0,12).map((p)=>({id:p.id,title:p.title,domain:p.domain,url:p.canonicalUrl,type:p.sourceType,engines:p.engines,fetch:p.fetch?.attempted?{ok:p.fetch.ok,snippet:O(p.fetch.snippet||"",1200),publishedTime:p.fetch.publishedTime||""}:void 0}));return["You are writing the final research report for an iterative deep-research run.","Produce a thorough markdown report organized into clear sections.","","Use the learnings and source registry below. Every substantive claim MUST be backed by an [S1] citation.",'Where engines disagree, surface the conflicting claims explicitly in the "differences" array.','Include a "Key Claims" structure that maps each distinct claim to its supporting source IDs.',"","Report structure:","1. ## Summary — A 2-4 sentence executive summary of findings","2. ## Key Findings — The main findings, organized by theme or question, each with inline citations","3. ## Areas of Disagreement — Where engines or sources conflict (if any)","4. ## Limitations & Caveats — Important qualifiers, gaps, or uncertainties","",`Original research question: ${t}`,`Learnings: ${JSON.stringify(l,null,2)}`,`Known gaps/caveats: ${JSON.stringify(i,null,2)}`,`Question ledger: ${JSON.stringify(n,null,2)}`,`Goal-based extracted evidence: ${JSON.stringify(a.slice(-20),null,2)}`,`Source registry: ${JSON.stringify(f,null,2)}`,"","Respond ONLY with JSON wrapped in BEGIN_JSON / END_JSON markers:","BEGIN_JSON",JSON.stringify({answer:"markdown report with sections and inline [S1] citations",agreement:{level:"high|medium|low|mixed|conflicting",summary:"one-sentence confidence summary"},differences:["notable disagreement or conflict between sources"],caveats:["important caveat or qualification"],claims:[{claim:"specific factual statement from the research",support:"strong|moderate|weak|conflicting",sourceIds:["S1","S2"]}],recommendedSources:["S1","S2"]},null,2),"END_JSON"].join(`
353
- `)}function gl(t,e=[],r=[],n=[]){let a=e.slice(0,12).map((p)=>({id:p.id,title:p.title,domain:p.domain,url:p.canonicalUrl,type:p.sourceType,engines:p.engines})),l=n.slice(-20),i=new Set;for(let p of l)for(let g of p.answers||[])if(g?.id)i.add(g.id);let f=(r||[]).filter((p)=>p.status!=="closed").map((p)=>({id:p.id,question:p.question}));return["You are writing the final research report from goal-based extracted evidence.","Per-round learnings were not produced, but the per-source evidence extraction step succeeded.","Synthesize a thorough markdown report using ONLY the evidence below. Every substantive claim MUST be backed by an [S1] citation.","","Report structure:","1. ## Summary — A 2-4 sentence executive summary of findings","2. ## Key Findings — The main findings, organized by theme or question, each with inline citations","3. ## Limitations & Caveats — Important qualifiers, gaps, or uncertainties","",`Original research question: ${t}`,`Per-source extracted evidence: ${JSON.stringify(l,null,2)}`,`Source registry: ${JSON.stringify(a,null,2)}`,`Questions already answered by the evidence: ${JSON.stringify(Array.from(i))}`,`Questions still open after this evidence: ${JSON.stringify(f)}`,"","Respond ONLY with JSON wrapped in BEGIN_JSON / END_JSON markers:","BEGIN_JSON",JSON.stringify({answer:"markdown report with sections and inline [S1] citations",agreement:{level:"high|medium|low|mixed|conflicting",summary:"one-sentence confidence summary"},differences:["notable disagreement or conflict between sources"],caveats:["important caveat or qualification"],claims:[{claim:"specific factual statement supported by the evidence",support:"strong|moderate|weak|conflicting",sourceIds:["S1","S2"]}],recommendedSources:["S1","S2"]},null,2),"END_JSON"].join(`
354
- `)}async function pl(t,{locale:e=null,short:r=!0}={}){let n=[Fa,"all","--inline","--stdin","--fast"];if(!r)n.push("--full");if(e)n.push("--locale",e);return new Promise((a,l)=>{let i=Ma(De(),n,{stdio:["pipe","pipe","pipe"],env:{...process.env,GREEDY_SEARCH_RESEARCH_CHILD:"1"}});i.stdin.write(t),i.stdin.end();let f="",p="",g="";i.stdout.on("data",(h)=>f+=h),i.stderr.on("data",(h)=>{p+=h,g+=h.toString();let m=g.split(`
355
- `);g=m.pop()||"";for(let c of m)if(hl(c))process.stderr.write(`${c}
356
- `)});let o=setTimeout(()=>{i.kill(),l(Error(`research child search timed out for: ${t}`))},140000);i.on("close",(h)=>{if(clearTimeout(o),h!==0){l(Error(p.trim()||`search child exited with code ${h}`));return}try{a(JSON.parse(f.trim()))}catch{l(Error(`Invalid JSON from research child: ${f.slice(0,200)}`))}})})}function ol(t){let e=new Map;for(let r of t.flat()){let n=M(r.canonicalUrl||r.url);if(!n)continue;let a=e.get(n);if(!a){e.set(n,{...r,canonicalUrl:n});continue}a.engines=[...new Set([...a.engines||[],...r.engines||[]])],a.engineCount=a.engines.length,a.smartScore=Math.max(a.smartScore||0,r.smartScore||0)}return Array.from(e.values()).sort((r,n)=>{let a=X(n)-X(r);if(a!==0)return a;return(r.domain||"").localeCompare(n.domain||"")}).slice(0,12).map((r,n)=>({...r,id:`S${n+1}`}))}function hl(t){return/^PROGRESS:/.test(t)||/^\[greedysearch\]/.test(t)||/^\[(bing|perplexity|google|gemini|chatgpt|logically|semantic-scholar)\]/.test(t)||/^GreedySearch Chrome/.test(t)||/^Launching GreedySearch Chrome/.test(t)||/^Headless mode/.test(t)||/^Ready\.?$/.test(t)}function lt(t,e={}){return wt(t?.answer||"")||e}function wl(t,e){if(!t||!Array.isArray(e))return{cited:[],missing:[],unfetched:[],ok:!0};let r=/\b[SF](\d+)\b/g,n=new Set,a;while((a=r.exec(t))!==null)n.add(`S${a[1]}`),n.add(`F${a[1]}`);let l=new Map;for(let g of e){let o=g?.id;if(o)l.set(o,g)}let i=Array.from(n),f=[],p=[];for(let g of i){let o=l.get(g);if(!o){let h=g.match(/^(S|F)(\d+)$/);if(h){let m=parseInt(h[2],10)-1;if(m>=0&&m<e.length){let c=e[m];if(c){if(!(c.fetch?.ok||c.content&&c.content.length>100||c.contentChars&&c.contentChars>100))p.push(g);continue}}}f.push(g)}else if(!(o.fetch?.ok||o.content&&o.content.length>100||o.contentChars&&o.contentChars>100))p.push(g)}return{cited:i,missing:f,unfetched:p,ok:f.length===0}}function jr({sources:t=[],fetchedSources:e=[],synthesis:r={},citationAudit:n=null,gaps:a=[],questions:l=[],rounds:i=[],qualityScore:f=0,qualityThreshold:p=8.5,maxSources:g=8,requireCitations:o=!0,requireQuestions:h=!0}={}){let m=e.filter((s)=>s?.fetch?.ok||(s?.contentChars||0)>100||String(s?.content||"").length>100),c=t.filter((s)=>["official-docs","repo","maintainer-blog","academic"].includes(String(s?.sourceType||""))),y=Array.isArray(r?.claims)?r.claims:[],b=n?n.cited?.length||0:0,w=ae(l),_=(l||[]).filter((s)=>!s.createdRound||s.reason==="Original research question"),N=ae(_),k=Math.min(4,Math.max(2,Number(g)||8)),v={roundsRun:i.length>=1,fetchedSources:m.length>=k,primarySources:c.length>=1,qualityScore:f>=Math.min(p,8),claimsExtracted:!o||y.length>0,citationsPresent:!o||b>0,citationsValid:!o||n?.ok===!0,unfetchedCitations:!o||(n?.unfetched||[]).length===0,requiredQuestionsClosed:!h||N.open===0};return{floorMet:Object.values(v).every(Boolean),checks:v,metrics:{fetchedOk:m.length,primarySources:c.length,claims:y.length,cited:b,gaps:a.length,openQuestions:w.open,closedQuestions:w.closed,totalQuestions:w.total,openRequiredQuestions:N.open,closedRequiredQuestions:N.closed,totalRequiredQuestions:N.total,qualityScore:f,minFetched:k}}}function Dr(t,e){let r=new Map;for(let n of e||[]){let a=M(n?.canonicalUrl||n?.finalUrl||n?.url);if(a&&n?.id)r.set(a,n.id)}return(t||[]).map((n,a)=>{let l=M(n?.finalUrl||n?.canonicalUrl||n?.url);return{...n,id:n?.id||r.get(l)||`F${a+1}`}})}function $l(t){return[{id:"Q1",question:O(A(t),320),status:"open",reason:"Original research question",evidence:[],sourceIds:[]}]}function cl(t){let e=0;for(let r of t||[]){let n=Number.parseInt(String(r.id||"").replace(/^Q/i,""),10);if(Number.isFinite(n))e=Math.max(e,n)}return`Q${e+1}`}function sr(t,e){let r=A(e).toLowerCase();return(t||[]).find((n)=>n.question?.toLowerCase()===r||Dt(n.question||"",r)>=0.82)}function ee(t,e,{reason:r="",round:n=null}={}){let a=O(A(e),320);if(!a)return null;let l=sr(t,a);if(l)return l;let i={id:cl(t),question:a,status:"open",reason:O(r,240),createdRound:n,evidence:[],sourceIds:[]};return t.push(i),i}function vt(t,e,{evidence:r="",sourceIds:n=[],round:a=null}={}){let l=t.find((i)=>i.id===e)||sr(t,e);if(!l)return null;if(l.status="closed",l.closedRound=l.closedRound||a,r)l.evidence=d([...l.evidence||[],r],4);if(Array.isArray(n))l.sourceIds=d([...l.sourceIds||[],...n],8);return l}function ae(t){let e=t.length,r=t.filter((n)=>n.status==="closed").length;return{total:e,closed:r,open:Math.max(0,e-r)}}function _t(t,{roundNumber:e,actions:r=[],learningPayload:n={}}={}){for(let p of r){let g=p?.action||p,o=g?.researchGoal&&g.researchGoal!=="Original user query"?g.researchGoal:g?.query||g?.url||"";if(o)ee(t,o,{reason:"Planned research action",round:e})}let a=5,l=t.filter((p)=>p.status==="open"&&p.reason==="Discovered gap/follow-up");if(l.length>a){let p=l.sort((g,o)=>(g.createdRound||0)-(o.createdRound||0)).slice(0,l.length-a);for(let g of p)g.status="resolved",g.closedRound=e,g.evidence=d([...g.evidence||[],"Auto-resolved to cap open-question ledger"],4)}let i=Array.isArray(n.answeredQuestions)?n.answeredQuestions:[];for(let p of i){if(typeof p==="string"){vt(t,p,{round:e});continue}let g=p?.id||p?.question;if(!g&&p?.question){let o=ee(t,p.question,{reason:"Answered during learning extraction",round:e});if(o)vt(t,o.id,{round:e});continue}vt(t,g,{evidence:p?.evidence||p?.answer||"",sourceIds:Array.isArray(p?.sourceIds)?p.sourceIds:[],round:e})}let f=Array.isArray(n.newQuestions)?n.newQuestions:[];for(let p of f)ee(t,p,{reason:"Discovered gap/follow-up",round:e});return t}function ml(t,e){if(!Array.isArray(t)||t.length===0)return[];let r=["arxiv.org","semanticscholar.org","doi.org"],n=new Set,a=[];for(let l of t){let i=l?.canonicalUrl||l?.finalUrl||l?.url||"";if(!i)continue;let f="";try{f=new URL(i).hostname.toLowerCase().replace(/^www\./,"")}catch{continue}if(!r.some((g)=>f===g||f.endsWith(`.${g}`)))continue;if(e.has(i)||n.has(i))continue;n.add(i);let p=i.includes("/pdf/")?i.replace(/\/pdf\//,"/html/").replace(/\.pdf$/i,""):i;a.push({url:p,label:l?.title||l?.id||f})}return a.slice(0,2)}function bl(t,e,r){if(!e?.answer||r?.ok!==!0)return t;let n=Array.isArray(e.claims)?e.claims:[],a=Array.isArray(r.cited)?r.cited:[];if(n.length===0||a.length===0)return t;for(let l of t){if(l.status==="closed")continue;let i=null,f=0;for(let p of n){let g=Dt(l.question||"",p.claim||"");if(g>f)f=g,i=p}if(l.id==="Q1"||f>=0.18)vt(t,l.id,{evidence:i?.claim||"Answered in final cited synthesis",sourceIds:Array.isArray(i?.sourceIds)?i.sourceIds:a.slice(0,4)})}return t}function kl(t){if(!t.length)return"No tracked questions.";return t.map((e)=>{let r=e.sourceIds?.length?` (${e.sourceIds.join(", ")})`:"";return`- [${e.status==="closed"?"x":" "}] ${e.id}: ${e.question}${r}`}).join(`
357
- `)}function re(t,e="None recorded."){let r=d(t);return r.length?r.map((n)=>`- ${n}`).join(`
358
- `):e}async function yl({query:t,rounds:e,sources:r,fetchedSources:n,evidenceItems:a=[],synthesis:l,citationAudit:i,floor:f,manifest:p,allGaps:g=[],questions:o=[],outDir:h=null}){let m=new Date().toISOString().replaceAll(/[:.]/g,"-").slice(0,19),c=h||C(Ya,`${m}_${Va(t)}`),y=C(c,"reports"),b=C(c,"sources"),w=C(c,"data");ut(y,{recursive:!0}),ut(b,{recursive:!0}),ut(w,{recursive:!0});let _=await ne(n,b),N=d([...g,...e.flatMap((k)=>k.gaps||[])]);return W(C(c,"STATUS.md"),[f.floorMet?"STATUS: DONE":"STATUS: PARTIAL","",`Query: ${t}`,`Stop reason: ${p.terminationReason||"max_rounds"}`,"","## Deterministic floor checks",...Object.entries(f.checks).map(([k,v])=>`- [${v?"x":" "}] ${k}`),"","## Questions",kl(o),"","## Open gaps",re(N),""].join(`
359
- `),"utf8"),W(C(c,"OUTLINE.md"),["# Research bundle outline","","- `reports/SUMMARY.md` — final cited report","- `reports/CLAIMS.md` — extracted claims with support/source IDs","- `reports/EVIDENCE.md` — goal-based source evidence","- `reports/GAPS.md` — remaining caveats and uncertainties","- `sources/` — fetched source markdown files","- `data/manifest.json` — machine-readable run metadata","- `data/rounds.json` — per-round actions/learnings/gaps","- `data/sources.json` — ranked source registry","- `data/questions.json` — open/closed question ledger",""].join(`
360
- `),"utf8"),W(C(y,"SUMMARY.md"),String(l.answer||""),"utf8"),W(C(y,"CLAIMS.md"),["# Key claims","",...Array.isArray(l.claims)&&l.claims.length?l.claims.map((k)=>{let v=Array.isArray(k.sourceIds)?k.sourceIds.join(", "):"";return`- ${k.claim||""} (${k.support||"support unknown"}${v?`; ${v}`:""})`}):["No structured claims were extracted."],""].join(`
361
- `),"utf8"),W(C(y,"EVIDENCE.md"),["# Extracted evidence","",...a.length?a.map((k)=>[`## ${k.sourceId||k.url||"Source"}`,k.url?`<${k.url}>`:"",k.rational?`**Rational:** ${k.rational}`:"",k.evidence?`**Evidence:** ${k.evidence}`:"",k.summary?`**Summary:** ${k.summary}`:"",""].filter(Boolean).join(`
350
+ `)}function Pr(t){if(!t||typeof t!=="object")return null;let e=t.type,r=T(t.researchGoal||"",320);if(e==="search"){if(t.query==null)return null;let n=A(t.query);return n?{type:"search",query:n,researchGoal:r}:null}if(e==="fetchUrl"){if(t.url==null)return null;let n=M(t.url);return n?{type:"fetchUrl",url:n,researchGoal:r}:null}return null}async function rl(t,{locale:e=null,short:r=!0,usedQueries:n,usedUrls:a,maxChars:l=8000}={}){if(t.type==="search"){let i=A(t.query).toLowerCase();n.add(i);try{let f=await ml(t.query,{locale:e,short:r}),o=Pt(f,t.query);return{ok:!0,action:t,result:f,sources:o}}catch(f){return{ok:!1,action:t,error:f.message,sources:[]}}}if(t.type==="fetchUrl"){let i=M(t.url);if(a.has(i))return{ok:!1,action:t,error:`URL already fetched: ${i}`,sources:[]};try{let f=await nl(i,l);a.add(i);let o=ll(i),g={id:"",canonicalUrl:f.finalUrl||i,displayUrl:f.url||i,domain:o,title:f.title||i,engines:["fetch"],engineCount:1,perEngine:{},sourceType:Ot(o,f.title||"",f.finalUrl||i),isOfficial:!1,smartScore:0,fetch:{attempted:!0,ok:!f.error&&(f.contentChars||0)>100,status:f.status||null,finalUrl:f.finalUrl||i,content:f.content||"",contentChars:f.contentChars||0,snippet:f.snippet||"",error:f.error||""}};return{ok:!0,action:t,result:null,sources:[g],fetchResult:{id:g.id,url:i,finalUrl:f.finalUrl||i,title:f.title||"",content:f.content||"",contentChars:f.contentChars||0,snippet:f.snippet||"",status:f.status||null,error:f.error||"",source:f.source||"http",duration:f.duration||0}}}catch(f){return{ok:!1,action:t,error:f.message,sources:[]}}}return{ok:!1,action:t,error:`Unknown action type: ${t.type}`,sources:[]}}async function nl(t,e){return await al(t,e)}async function al(t,e=8000){let r=Date.now();try{let{parseGitHubUrl:n,fetchGitHubContent:a}=await Promise.resolve().then(() => (St(),zt)),l=n(t);if(l&&(l.type==="root"||l.type==="tree"||l.type==="blob"&&!l.path?.includes("."))){let i=await a(t);if(i.ok){let{trimContentHeadTail:f}=await Promise.resolve().then(() => ut),o=f(i.content,e);return{url:t,finalUrl:t,status:200,title:i.title,snippet:o.slice(0,320),content:o,contentChars:o.length,source:"github-api",duration:Date.now()-r}}}}catch{}try{let{fetchSourceHttp:n}=await Promise.resolve().then(() => (Dr(),jr)),{trimContentHeadTail:a}=await Promise.resolve().then(() => ut),l=await n(t,{timeoutMs:1e4});if(l.ok){let i=a(l.markdown,e);return{url:t,finalUrl:l.finalUrl,status:l.status,title:l.title,snippet:l.excerpt,content:i,contentChars:i.length,source:"http",duration:Date.now()-r}}}catch{}return{url:t,title:"",content:"",contentChars:0,snippet:"",error:"HTTP fetch failed",source:"error",duration:Date.now()-r}}function ll(t){try{return new URL(t).hostname.toLowerCase().replace(/^www\./,"")}catch{return""}}async function il(t,e){let r=[],{parseGitHubUrl:n}=await Promise.resolve().then(() => (St(),zt));for(let a of t){if(a.type!=="fetchUrl"){r.push(a);continue}let l=n(a.url);if(!l||l.type!=="root"){r.push(a);continue}let{owner:i,repo:f}=l,o=`https://github.com/${i}/${f}`;if(e.has(o))continue;let g=[o],p=[`${o}/blob/main/CONTRIBUTING.md`,`${o}/blob/master/CONTRIBUTING.md`,`${o}/blob/main/CHANGELOG.md`,`${o}/blob/master/CHANGELOG.md`,`${o}/blob/main/docs/README.md`];for(let h of p){if(g.length>=3)break;if(!e.has(h))g.push(h)}for(let h of g)r.push({type:"fetchUrl",url:h,researchGoal:a.researchGoal||`Fetch GitHub content for ${i}/${f}`})}return r}function fl(t,e){let r=wt(t?.answer||"")||{},n=Array.isArray(r?.actions)?r.actions:[],a=[];for(let l of n){let i=Pr(l);if(i&&a.length<e)a.push(i)}return a}function gl(t){return(t||[]).map((e)=>({type:"search",query:typeof e==="string"?e:e.query,researchGoal:typeof e==="string"?"":e.researchGoal||""})).filter((e)=>e.query)}function jt(t){return M(t?.finalUrl||t?.canonicalUrl||t?.url||"")||t?.id||""}function ol(t,e,r,n=new Set){let a=(e||[]).filter((i)=>i.status!=="closed").slice(0,12).map((i)=>({id:i.id,question:i.question})),l=(r||[]).filter((i)=>i?.content||i?.snippet).filter((i)=>!n.has(jt(i))).slice(0,6).map((i,f)=>({id:i.id||`F${f+1}`,title:i.title||"",url:i.finalUrl||i.url||i.canonicalUrl||"",content:T(i.content||i.snippet||"",5000)}));return["You are doing goal-based evidence extraction for an iterative research run.","For each source, extract only information that helps answer the open questions.","Use original wording/details where useful. Do not invent answers; leave questions open if evidence is insufficient.","If a source answers one or more tracked questions, identify those question IDs explicitly.","Also propose genuinely new sub-questions discovered from the evidence.","",`Original research question: ${t}`,`Open question ledger: ${JSON.stringify(a,null,2)}`,`Fetched sources: ${JSON.stringify(l,null,2)}`,"","Respond ONLY with JSON wrapped in BEGIN_JSON / END_JSON markers:","BEGIN_JSON",JSON.stringify({extractions:[{sourceId:"S1",url:"https://example.com/source",rational:"why this source matters for the goal",evidence:"specific quoted/paraphrased evidence with numbers, dates, caveats",summary:"concise contribution to the research question",answers:[{id:"Q1",evidence:"brief evidence that closes the question"}],newQuestions:["new sub-question raised by this source"]}]},null,2),"END_JSON"].join(`
351
+ `)}function pl(t,e){let r=Array.isArray(t?.extractions)?t.extractions:[],n=new Map,a=new Map;for(let l of e||[]){if(l?.id)a.set(String(l.id),l);let i=jt(l);if(i)n.set(i,l)}return r.map((l)=>{let i=a.get(String(l?.sourceId||""))||n.get(M(l?.url||"")||""),f=String(l?.sourceId||i?.id||""),o=M(l?.url||i?.finalUrl||i?.url||""),g=Array.isArray(l?.answers)?l.answers.map((p)=>({id:String(p?.id||""),evidence:T(p?.evidence||"",500),sourceIds:[f].filter(Boolean)})).filter((p)=>p.id):[];return{sourceId:f,url:o,title:i?.title||l?.title||"",rational:T(l?.rational||"",700),evidence:T(l?.evidence||"",1600),summary:T(l?.summary||"",700),answers:g,newQuestions:B(l?.newQuestions||[],6)}}).filter((l)=>l.sourceId||l.url||l.summary||l.evidence)}async function hl({query:t,questions:e,fetchedSources:r,extractedSourceKeys:n}){let a=(r||[]).filter((l)=>(l?.content||l?.snippet)&&!n.has(jt(l)));if(a.length===0)return{evidence:[],error:""};try{let l=await x(ol(t,e,a,n),{timeoutMs:120000}),i=lt(l,{extractions:[]}),f=pl(i,a);for(let o of a){let g=jt(o);if(g)n.add(g)}return{evidence:f,error:""}}catch(l){return{evidence:[],error:l.message||String(l)}}}function wl(t,e,r,n,a=[],l=[]){let i=n.filter((f)=>f?.content||f?.snippet).slice(0,10).map((f,o)=>({id:`F${o+1}`,title:f.title||"",url:f.finalUrl||f.url||"",snippet:T(f.content||f.snippet||"",3000)}));return["You are extracting compact research state from live multi-engine search results.","Create dense, non-overlapping learnings with exact names, numbers, dates, limitations, and caveats where available.","Also propose follow-up search queries that would most improve confidence or fill gaps.","",`Original research question: ${t}`,`Round queries: ${JSON.stringify(e,null,2)}`,`Question ledger: ${JSON.stringify(a,null,2)}`,`Extracted source evidence: ${JSON.stringify(l.slice(-12),null,2)}`,`Engine summaries: ${JSON.stringify(r,null,2)}`,`Fetched source snippets: ${JSON.stringify(i,null,2)}`,"","Respond ONLY with JSON wrapped in BEGIN_JSON / END_JSON markers:","BEGIN_JSON",JSON.stringify({learnings:["concise, information-dense learning"],answeredQuestions:[{id:"Q1",evidence:"brief evidence that closes this question",sourceIds:["S1"]}],newQuestions:["new sub-question discovered from the evidence"],followUpQueries:["specific next search query"],gaps:["important uncertainty or missing evidence"]},null,2),"END_JSON"].join(`
352
+ `)}function $l(t,e,r,n=[],a=[]){let l=e.flatMap((o)=>o.learnings||[]),i=e.flatMap((o)=>o.gaps||[]),f=r.slice(0,12).map((o)=>({id:o.id,title:o.title,domain:o.domain,url:o.canonicalUrl,type:o.sourceType,engines:o.engines,fetch:o.fetch?.attempted?{ok:o.fetch.ok,snippet:T(o.fetch.snippet||"",1200),publishedTime:o.fetch.publishedTime||""}:void 0}));return["You are writing the final research report for an iterative deep-research run.","Produce a thorough markdown report organized into clear sections.","","Use the learnings and source registry below. Every substantive claim MUST be backed by an [S1] citation.",'Where engines disagree, surface the conflicting claims explicitly in the "differences" array.','Include a "Key Claims" structure that maps each distinct claim to its supporting source IDs.',"","Report structure:","1. ## Summary — A 2-4 sentence executive summary of findings","2. ## Key Findings — The main findings, organized by theme or question, each with inline citations","3. ## Areas of Disagreement — Where engines or sources conflict (if any)","4. ## Limitations & Caveats — Important qualifiers, gaps, or uncertainties","",`Original research question: ${t}`,`Learnings: ${JSON.stringify(l,null,2)}`,`Known gaps/caveats: ${JSON.stringify(i,null,2)}`,`Question ledger: ${JSON.stringify(n,null,2)}`,`Goal-based extracted evidence: ${JSON.stringify(a.slice(-20),null,2)}`,`Source registry: ${JSON.stringify(f,null,2)}`,"","Respond ONLY with JSON wrapped in BEGIN_JSON / END_JSON markers:","BEGIN_JSON",JSON.stringify({answer:"markdown report with sections and inline [S1] citations",agreement:{level:"high|medium|low|mixed|conflicting",summary:"one-sentence confidence summary"},differences:["notable disagreement or conflict between sources"],caveats:["important caveat or qualification"],claims:[{claim:"specific factual statement from the research",support:"strong|moderate|weak|conflicting",sourceIds:["S1","S2"]}],recommendedSources:["S1","S2"]},null,2),"END_JSON"].join(`
353
+ `)}function cl(t,e=[],r=[],n=[]){let a=e.slice(0,12).map((o)=>({id:o.id,title:o.title,domain:o.domain,url:o.canonicalUrl,type:o.sourceType,engines:o.engines})),l=n.slice(-20),i=new Set;for(let o of l)for(let g of o.answers||[])if(g?.id)i.add(g.id);let f=(r||[]).filter((o)=>o.status!=="closed").map((o)=>({id:o.id,question:o.question}));return["You are writing the final research report from goal-based extracted evidence.","Per-round learnings were not produced, but the per-source evidence extraction step succeeded.","Synthesize a thorough markdown report using ONLY the evidence below. Every substantive claim MUST be backed by an [S1] citation.","","Report structure:","1. ## Summary — A 2-4 sentence executive summary of findings","2. ## Key Findings — The main findings, organized by theme or question, each with inline citations","3. ## Limitations & Caveats — Important qualifiers, gaps, or uncertainties","",`Original research question: ${t}`,`Per-source extracted evidence: ${JSON.stringify(l,null,2)}`,`Source registry: ${JSON.stringify(a,null,2)}`,`Questions already answered by the evidence: ${JSON.stringify(Array.from(i))}`,`Questions still open after this evidence: ${JSON.stringify(f)}`,"","Respond ONLY with JSON wrapped in BEGIN_JSON / END_JSON markers:","BEGIN_JSON",JSON.stringify({answer:"markdown report with sections and inline [S1] citations",agreement:{level:"high|medium|low|mixed|conflicting",summary:"one-sentence confidence summary"},differences:["notable disagreement or conflict between sources"],caveats:["important caveat or qualification"],claims:[{claim:"specific factual statement supported by the evidence",support:"strong|moderate|weak|conflicting",sourceIds:["S1","S2"]}],recommendedSources:["S1","S2"]},null,2),"END_JSON"].join(`
354
+ `)}async function ml(t,{locale:e=null,short:r=!0}={}){let n=[Ba,"all","--inline","--stdin","--fast"];if(!r)n.push("--full");if(e)n.push("--locale",e);return new Promise((a,l)=>{let i=Ya(Ne(),n,{stdio:["pipe","pipe","pipe"],env:{...process.env,GREEDY_SEARCH_RESEARCH_CHILD:"1"}});i.stdin.write(t),i.stdin.end();let f="",o="",g="";i.stdout.on("data",(h)=>f+=h),i.stderr.on("data",(h)=>{o+=h,g+=h.toString();let m=g.split(`
355
+ `);g=m.pop()||"";for(let c of m)if(yl(c))process.stderr.write(`${c}
356
+ `)});let p=setTimeout(()=>{i.kill(),l(Error(`research child search timed out for: ${t}`))},140000);i.on("close",(h)=>{if(clearTimeout(p),h!==0){l(Error(o.trim()||`search child exited with code ${h}`));return}try{a(JSON.parse(f.trim()))}catch{l(Error(`Invalid JSON from research child: ${f.slice(0,200)}`))}})})}function bl(t){let e=new Map;for(let r of t.flat()){let n=M(r.canonicalUrl||r.url);if(!n)continue;let a=e.get(n);if(!a){e.set(n,{...r,canonicalUrl:n});continue}a.engines=[...new Set([...a.engines||[],...r.engines||[]])],a.engineCount=a.engines.length,a.smartScore=Math.max(a.smartScore||0,r.smartScore||0)}return Array.from(e.values()).sort((r,n)=>{let a=X(n)-X(r);if(a!==0)return a;return(r.domain||"").localeCompare(n.domain||"")}).slice(0,12).map((r,n)=>({...r,id:`S${n+1}`}))}function yl(t){return/^PROGRESS:/.test(t)||/^\[greedysearch\]/.test(t)||/^\[(bing|perplexity|google|gemini|chatgpt|logically|semantic-scholar)\]/.test(t)||/^GreedySearch Chrome/.test(t)||/^Launching GreedySearch Chrome/.test(t)||/^Headless mode/.test(t)||/^Ready\.?$/.test(t)}function lt(t,e={}){return wt(t?.answer||"")||e}function _l(t,e){if(!t||!Array.isArray(e))return{cited:[],missing:[],unfetched:[],ok:!0};let r=/\b[SF](\d+)\b/g,n=new Set,a;while((a=r.exec(t))!==null)n.add(`S${a[1]}`),n.add(`F${a[1]}`);let l=new Map;for(let g of e){let p=g?.id;if(p)l.set(p,g)}let i=Array.from(n),f=[],o=[];for(let g of i){let p=l.get(g);if(!p){let h=g.match(/^(S|F)(\d+)$/);if(h){let m=parseInt(h[2],10)-1;if(m>=0&&m<e.length){let c=e[m];if(c){if(!(c.fetch?.ok||c.content&&c.content.length>100||c.contentChars&&c.contentChars>100))o.push(g);continue}}}f.push(g)}else if(!(p.fetch?.ok||p.content&&p.content.length>100||p.contentChars&&p.contentChars>100))o.push(g)}return{cited:i,missing:f,unfetched:o,ok:f.length===0}}function Cr({sources:t=[],fetchedSources:e=[],synthesis:r={},citationAudit:n=null,gaps:a=[],questions:l=[],rounds:i=[],qualityScore:f=0,qualityThreshold:o=8.5,maxSources:g=8,requireCitations:p=!0,requireQuestions:h=!0}={}){let m=e.filter((N)=>N?.fetch?.ok||(N?.contentChars||0)>100||String(N?.content||"").length>100),c=t.filter((N)=>["official-docs","repo","maintainer-blog","academic"].includes(String(N?.sourceType||""))),y=Array.isArray(r?.claims)?r.claims:[],b=n?n.cited?.length||0:0,w=le(l),k=(l||[]).filter((N)=>!N.createdRound||N.reason==="Original research question"),C=le(k),_=Math.min(4,Math.max(2,Number(g)||8)),s={roundsRun:i.length>=1,fetchedSources:m.length>=_,primarySources:c.length>=1,qualityScore:f>=Math.min(o,8),claimsExtracted:!p||y.length>0,citationsPresent:!p||b>0,citationsValid:!p||n?.ok===!0,unfetchedCitations:!p||(n?.unfetched||[]).length===0,requiredQuestionsClosed:!h||C.open===0};return{floorMet:Object.values(s).every(Boolean),checks:s,metrics:{fetchedOk:m.length,primarySources:c.length,claims:y.length,cited:b,gaps:a.length,openQuestions:w.open,closedQuestions:w.closed,totalQuestions:w.total,openRequiredQuestions:C.open,closedRequiredQuestions:C.closed,totalRequiredQuestions:C.total,qualityScore:f,minFetched:_}}}function Nr(t,e){let r=new Map;for(let n of e||[]){let a=M(n?.canonicalUrl||n?.finalUrl||n?.url);if(a&&n?.id)r.set(a,n.id)}return(t||[]).map((n,a)=>{let l=M(n?.finalUrl||n?.canonicalUrl||n?.url);return{...n,id:n?.id||r.get(l)||`F${a+1}`}})}function kl(t){return[{id:"Q1",question:T(A(t),320),status:"open",reason:"Original research question",evidence:[],sourceIds:[]}]}function sl(t){let e=0;for(let r of t||[]){let n=Number.parseInt(String(r.id||"").replace(/^Q/i,""),10);if(Number.isFinite(n))e=Math.max(e,n)}return`Q${e+1}`}function Lr(t,e){let r=A(e).toLowerCase();return(t||[]).find((n)=>n.question?.toLowerCase()===r||Dt(n.question||"",r)>=0.82)}function re(t,e,{reason:r="",round:n=null}={}){let a=T(A(e),320);if(!a)return null;let l=Lr(t,a);if(l)return l;let i={id:sl(t),question:a,status:"open",reason:T(r,240),createdRound:n,evidence:[],sourceIds:[]};return t.push(i),i}function vt(t,e,{evidence:r="",sourceIds:n=[],round:a=null}={}){let l=t.find((i)=>i.id===e)||Lr(t,e);if(!l)return null;if(l.status="closed",l.closedRound=l.closedRound||a,r)l.evidence=B([...l.evidence||[],r],4);if(Array.isArray(n))l.sourceIds=B([...l.sourceIds||[],...n],8);return l}function le(t){let e=t.length,r=t.filter((n)=>n.status==="closed").length;return{total:e,closed:r,open:Math.max(0,e-r)}}function st(t,{roundNumber:e,actions:r=[],learningPayload:n={}}={}){for(let o of r){let g=o?.action||o,p=g?.researchGoal&&g.researchGoal!=="Original user query"?g.researchGoal:g?.query||g?.url||"";if(p)re(t,p,{reason:"Planned research action",round:e})}let a=5,l=t.filter((o)=>o.status==="open"&&o.reason==="Discovered gap/follow-up");if(l.length>a){let o=l.sort((g,p)=>(g.createdRound||0)-(p.createdRound||0)).slice(0,l.length-a);for(let g of o)g.status="resolved",g.closedRound=e,g.evidence=B([...g.evidence||[],"Auto-resolved to cap open-question ledger"],4)}let i=Array.isArray(n.answeredQuestions)?n.answeredQuestions:[];for(let o of i){if(typeof o==="string"){vt(t,o,{round:e});continue}let g=o?.id||o?.question;if(!g&&o?.question){let p=re(t,o.question,{reason:"Answered during learning extraction",round:e});if(p)vt(t,p.id,{round:e});continue}vt(t,g,{evidence:o?.evidence||o?.answer||"",sourceIds:Array.isArray(o?.sourceIds)?o.sourceIds:[],round:e})}let f=Array.isArray(n.newQuestions)?n.newQuestions:[];for(let o of f)re(t,o,{reason:"Discovered gap/follow-up",round:e});return t}function vl(t,e){if(!Array.isArray(t)||t.length===0)return[];let r=["arxiv.org","semanticscholar.org","doi.org"],n=new Set,a=[];for(let l of t){let i=l?.canonicalUrl||l?.finalUrl||l?.url||"";if(!i)continue;let f="";try{f=new URL(i).hostname.toLowerCase().replace(/^www\./,"")}catch{continue}if(!r.some((g)=>f===g||f.endsWith(`.${g}`)))continue;if(e.has(i)||n.has(i))continue;n.add(i);let o=i.includes("/pdf/")?i.replace(/\/pdf\//,"/html/").replace(/\.pdf$/i,""):i;a.push({url:o,label:l?.title||l?.id||f})}return a.slice(0,2)}function jl(t,e,r){if(!e?.answer||r?.ok!==!0)return t;let n=Array.isArray(e.claims)?e.claims:[],a=Array.isArray(r.cited)?r.cited:[];if(n.length===0||a.length===0)return t;for(let l of t){if(l.status==="closed")continue;let i=null,f=0;for(let o of n){let g=Dt(l.question||"",o.claim||"");if(g>f)f=g,i=o}if(l.id==="Q1"||f>=0.18)vt(t,l.id,{evidence:i?.claim||"Answered in final cited synthesis",sourceIds:Array.isArray(i?.sourceIds)?i.sourceIds:a.slice(0,4)})}return t}function Dl(t){if(!t.length)return"No tracked questions.";return t.map((e)=>{let r=e.sourceIds?.length?` (${e.sourceIds.join(", ")})`:"";return`- [${e.status==="closed"?"x":" "}] ${e.id}: ${e.question}${r}`}).join(`
357
+ `)}function ne(t,e="None recorded."){let r=B(t);return r.length?r.map((n)=>`- ${n}`).join(`
358
+ `):e}async function El({query:t,rounds:e,sources:r,fetchedSources:n,evidenceItems:a=[],synthesis:l,citationAudit:i,floor:f,manifest:o,allGaps:g=[],questions:p=[],outDir:h=null}){let m=new Date().toISOString().replaceAll(/[:.]/g,"-").slice(0,19),c=h||D(Ka,`${m}_${Ua(t)}`),y=D(c,"reports"),b=D(c,"sources"),w=D(c,"data");qt(y,{recursive:!0}),qt(b,{recursive:!0}),qt(w,{recursive:!0});let k=await ae(n,b),C=B([...g,...e.flatMap((_)=>_.gaps||[])]);return W(D(c,"STATUS.md"),[f.floorMet?"STATUS: DONE":"STATUS: PARTIAL","",`Query: ${t}`,`Stop reason: ${o.terminationReason||"max_rounds"}`,"","## Deterministic floor checks",...Object.entries(f.checks).map(([_,s])=>`- [${s?"x":" "}] ${_}`),"","## Questions",Dl(p),"","## Open gaps",ne(C),""].join(`
359
+ `),"utf8"),W(D(c,"OUTLINE.md"),["# Research bundle outline","","- `reports/SUMMARY.md` — final cited report","- `reports/CLAIMS.md` — extracted claims with support/source IDs","- `reports/EVIDENCE.md` — goal-based source evidence","- `reports/GAPS.md` — remaining caveats and uncertainties","- `sources/` — fetched source markdown files","- `data/manifest.json` — machine-readable run metadata","- `data/rounds.json` — per-round actions/learnings/gaps","- `data/sources.json` — ranked source registry","- `data/questions.json` — open/closed question ledger",""].join(`
360
+ `),"utf8"),W(D(y,"SUMMARY.md"),String(l.answer||""),"utf8"),W(D(y,"CLAIMS.md"),["# Key claims","",...Array.isArray(l.claims)&&l.claims.length?l.claims.map((_)=>{let s=Array.isArray(_.sourceIds)?_.sourceIds.join(", "):"";return`- ${_.claim||""} (${_.support||"support unknown"}${s?`; ${s}`:""})`}):["No structured claims were extracted."],""].join(`
361
+ `),"utf8"),W(D(y,"EVIDENCE.md"),["# Extracted evidence","",...a.length?a.map((_)=>[`## ${_.sourceId||_.url||"Source"}`,_.url?`<${_.url}>`:"",_.rational?`**Rational:** ${_.rational}`:"",_.evidence?`**Evidence:** ${_.evidence}`:"",_.summary?`**Summary:** ${_.summary}`:"",""].filter(Boolean).join(`
362
362
  `)):["No goal-based evidence was extracted."],""].join(`
363
- `),"utf8"),W(C(y,"GAPS.md"),["# Gaps and caveats","","## Caveats",re(l.caveats||[]),"","## Research gaps",re(N),""].join(`
364
- `),"utf8"),W(C(w,"manifest.json"),JSON.stringify({...p,floor:f,citationAudit:i},null,2),"utf8"),W(C(w,"rounds.json"),JSON.stringify(e,null,2),"utf8"),W(C(w,"sources.json"),JSON.stringify(r,null,2),"utf8"),W(C(w,"questions.json"),JSON.stringify(o,null,2),"utf8"),W(C(w,"evidence.json"),JSON.stringify(a,null,2),"utf8"),W(C(b,"index.md"),["# Source index","",..._.map((k)=>{let v=k.title||k.url,s=k.finalUrl||k.url,J=k.contentPath?` — ${k.contentPath}`:"";return`- ${k.id||"?"}: [${v}](${s})${J}`}),""].join(`
365
- `),"utf8"),{dir:c,statusPath:C(c,"STATUS.md"),summaryPath:C(y,"SUMMARY.md"),manifestPath:C(w,"manifest.json"),sourceCount:_.length,sourceFiles:_}}async function bf({query:t,breadth:e=3,iterations:r=2,maxSources:n,locale:a=null,short:l=!1,qualityThreshold:i=8.5,writeBundle:f=process.env.GREEDY_RESEARCH_BUNDLE!=="0",researchOutDir:p=null}={}){let g=Ka({breadth:e,iterations:r,maxSources:n}),o=[],h=[],m=[],c=$l(t),y=null,b=[],w=[],_=[],N=new Set,k=new Set,v=new Set,s=[],J="max_rounds",Or=new Date().toISOString(),Tr=Date.now(),le=0,ie=0,fe=0,ge=[];process.stderr.write(`[greedysearch] Research mode: breadth ${g.breadth}, iterations ${g.iterations}, qualityThreshold ${i}, engines ${At.join(",")}, synthesizer gemini
366
- `);for(let j=0;j<g.iterations;j++){let E=j+1,R=Math.max(1,Math.ceil(g.breadth/2**j));if(process.stderr.write(`PROGRESS:research:round-${E}:planning
367
- `),!y)try{let $=await x(Qa(t,R,h,m,[...v]),{timeoutMs:120000}),D=el($,R);if(j===0)D.unshift({type:"search",query:t,researchGoal:"Original user query"});D=await tl(D,v),y=D}catch($){process.stderr.write(`[greedysearch] Action planning failed, using fallback queries: ${$.message}
368
- `);let D=Za(null,t,R,{includeOriginal:j===0,exclude:k});y=rl(D)}let z=(y||[]).filter(($)=>{if($.type==="search"){let D=!Er($.query,k,{roundIndex:j,originalQuery:t});if(!D)process.stderr.write(`[greedysearch] Novelty gate rejected search: ${$.query}
369
- `);return D}if($.type==="fetchUrl"){let D=!v.has($.url);if(!D)process.stderr.write(`[greedysearch] Novelty gate rejected fetch: ${$.url}
370
- `);return D}return!1}).slice(0,R),oe=ml(b,v);if(!z.some(($)=>$.type==="fetchUrl")&&oe.length>0){let $=oe[0];z.push({type:"fetchUrl",url:$.url,researchGoal:`Direct fetch of known academic source: ${$.label||$.url}`}),process.stderr.write(`[greedysearch] Forced fetchUrl for academic source: ${$.url}
371
- `)}let S=[];for(let $=0;$<z.length;$++){let D=z[$];process.stderr.write(`PROGRESS:research:round-${E}:action-${$+1}/${z.length}
372
- `),process.stderr.write(`[greedysearch] Action ${$+1}/${z.length} [${D.type}]: ${(D.query||D.url).slice(0,80)}
373
- `);let Y=await za(D,{locale:a,short:l,usedQueries:k,usedUrls:v,maxChars:8000});if(S.push(Y),le++,D.type==="search")ie++;if(D.type==="fetchUrl")fe++;if(!Y.ok)ge.push({round:E,type:D.type,target:D.query||D.url,error:Y.error}),process.stderr.write(`[greedysearch] Action failed: ${Y.error}
374
- `)}let he=S.filter(($)=>$.action.type==="search"),we=S.filter(($)=>$.action.type==="fetchUrl");_t(c,{roundNumber:E,actions:S}),b=ol([b,he.flatMap(($)=>$.sources||[]),we.flatMap(($)=>$.sources||[])]);for(let $ of we)if($.fetchResult)w.push($.fetchResult);w=Cr(w);let Ct=Math.max(0,g.maxSources-w.filter(($)=>$?.content||$?.contentChars>100).length);if(Ct>0&&b.length>0){process.stderr.write(`PROGRESS:research:round-${E}:fetching
375
- `);let $=await Ga(b,Math.min(Ct,b.length),8000,Math.min(3,Ct||1));w=Cr([...w,...$]),b=ye(b,w)}w=Dr(w,b),process.stderr.write(`PROGRESS:research:round-${E}:evidence
376
- `);let u=await ll({query:t,questions:c,fetchedSources:w,extractedSourceKeys:N});if(u.error)process.stderr.write(`[greedysearch] Evidence extraction failed: ${u.error}
377
- `);_=[..._,...u.evidence];for(let $ of u.evidence)_t(c,{roundNumber:E,learningPayload:{answeredQuestions:$.answers||[],newQuestions:$.newQuestions||[]}});let Mr=S.map(($)=>({query:$.action.query||$.action.url||"",researchGoal:$.action.researchGoal||""}));process.stderr.write(`PROGRESS:research:round-${E}:learning
378
- `);let H={learnings:[],followUpQueries:[],gaps:[]},$e="";try{let $=await x(il(t,Mr,he.map((D)=>({query:D.action.query,researchGoal:D.action.researchGoal,error:D.error||"",engines:xa(D.result)})),w,c,_),{timeoutMs:120000});H={...H,...lt($,H)}}catch($){$e=$.message,process.stderr.write(`[greedysearch] Learning extraction failed: ${$.message}
379
- `)}let ce=Array.isArray(H.learnings)?H.learnings.map(($)=>String($)).filter(Boolean).slice(0,8):[],Et=Array.isArray(H.gaps)?H.gaps.map(($)=>String($)).filter(Boolean).slice(0,6):[];h=d([...h,...ce]),m=d([...m,...Et]),_t(c,{roundNumber:E,actions:[],learningPayload:H,gaps:Et}),o.push({round:E,actions:S.map(($)=>({type:$.action.type,query:$.action.query||"",url:$.action.url||"",researchGoal:$.action.researchGoal||"",error:$.error||"",sourceCount:$.sources?.length||0})),learnings:ce,gaps:Et,evidence:u.evidence,evidenceError:u.error,learningError:$e}),process.stderr.write(`PROGRESS:research:round-${E}:evaluating
380
- `);let L=await Xa(t,o,h,m,s);s.push(L.score),m=d([...m,...L.knowledgeGaps||[]]),_t(c,{roundNumber:E,gaps:L.knowledgeGaps||[]});let me=jr({sources:b,fetchedSources:w,gaps:m,questions:c,rounds:o,qualityScore:L.score,qualityThreshold:i,maxSources:g.maxSources,requireCitations:!1,requireQuestions:!1});if(process.stderr.write(`[greedysearch] Quality score round ${E}: ${L.score.toFixed(1)} (shouldContinue: ${L.shouldContinue}, floor: ${me.floorMet})
381
- `),L.score>=i&&me.floorMet&&(!L.shouldContinue||L.terminationReason==="quality_threshold")){J=L.terminationReason||"quality_threshold",process.stderr.write(`[greedysearch] Research floor reached (score: ${L.score.toFixed(1)}). Terminating early.
382
- `);break}let I=Math.max(1,Math.ceil(R/2)),F=(H.followUpQueries||[]).map(($)=>({type:"search",query:A(String($)),researchGoal:"Follow-up from learning extraction"})).filter(($)=>$.query&&$.query.toLowerCase()!==t.toLowerCase()).slice(0,I);if(F.length<I&&L.nextActions.length>0){let $=L.nextActions.map((Y)=>Nr(Y)).filter(Boolean);F=[...F,...$].slice(0,I)}if(F.length<I&&m.length>0){let $=Ia(m,t,k,I-F.length,j+1),D=$.map((Y)=>({type:"search",query:Y.query,researchGoal:Y.researchGoal}));if(F=[...F,...D].slice(0,I),$.length>0)process.stderr.write(`[greedysearch] Generated ${$.length} gap-driven fallback actions.
363
+ `),"utf8"),W(D(y,"GAPS.md"),["# Gaps and caveats","","## Caveats",ne(l.caveats||[]),"","## Research gaps",ne(C),""].join(`
364
+ `),"utf8"),W(D(w,"manifest.json"),JSON.stringify({...o,floor:f,citationAudit:i},null,2),"utf8"),W(D(w,"rounds.json"),JSON.stringify(e,null,2),"utf8"),W(D(w,"sources.json"),JSON.stringify(r,null,2),"utf8"),W(D(w,"questions.json"),JSON.stringify(p,null,2),"utf8"),W(D(w,"evidence.json"),JSON.stringify(a,null,2),"utf8"),W(D(b,"index.md"),["# Source index","",...k.map((_)=>{let s=_.title||_.url,N=_.finalUrl||_.url,R=_.contentPath?` — ${_.contentPath}`:"";return`- ${_.id||"?"}: [${s}](${N})${R}`}),""].join(`
365
+ `),"utf8"),{dir:c,statusPath:D(c,"STATUS.md"),summaryPath:D(y,"SUMMARY.md"),manifestPath:D(w,"manifest.json"),sourceCount:k.length,sourceFiles:k}}async function Nf({query:t,breadth:e=3,iterations:r=2,maxSources:n,locale:a=null,short:l=!1,qualityThreshold:i=8.5,writeBundle:f=process.env.GREEDY_RESEARCH_BUNDLE!=="0",researchOutDir:o=null}={}){let g=Xa({breadth:e,iterations:r,maxSources:n}),p=[],h=[],m=[],c=kl(t),y=null,b=[],w=[],k=[],C=new Set,_=new Set,s=new Set,N=[],R="max_rounds",Wr=new Date().toISOString(),Mr=Date.now(),ie=0,fe=0,ge=0,oe=[];process.stderr.write(`[greedysearch] Research mode: breadth ${g.breadth}, iterations ${g.iterations}, qualityThreshold ${i}, engines ${Rt.join(",")}, synthesizer gemini
366
+ `);for(let v=0;v<g.iterations;v++){let E=v+1,J=Math.max(1,Math.ceil(g.breadth/2**v));if(process.stderr.write(`PROGRESS:research:round-${E}:planning
367
+ `),!y)try{let $=await x(el(t,J,h,m,[...s]),{timeoutMs:120000}),j=fl($,J);if(v===0)j.unshift({type:"search",query:t,researchGoal:"Original user query"});j=await il(j,s),y=j}catch($){process.stderr.write(`[greedysearch] Action planning failed, using fallback queries: ${$.message}
368
+ `);let j=xa(null,t,J,{includeOriginal:v===0,exclude:_});y=gl(j)}let z=(y||[]).filter(($)=>{if($.type==="search"){let j=!Or($.query,_,{roundIndex:v,originalQuery:t});if(!j)process.stderr.write(`[greedysearch] Novelty gate rejected search: ${$.query}
369
+ `);return j}if($.type==="fetchUrl"){let j=!s.has($.url);if(!j)process.stderr.write(`[greedysearch] Novelty gate rejected fetch: ${$.url}
370
+ `);return j}return!1}).slice(0,J),he=vl(b,s);if(!z.some(($)=>$.type==="fetchUrl")&&he.length>0){let $=he[0];z.push({type:"fetchUrl",url:$.url,researchGoal:`Direct fetch of known academic source: ${$.label||$.url}`}),process.stderr.write(`[greedysearch] Forced fetchUrl for academic source: ${$.url}
371
+ `)}let S=[];for(let $=0;$<z.length;$++){let j=z[$];process.stderr.write(`PROGRESS:research:round-${E}:action-${$+1}/${z.length}
372
+ `),process.stderr.write(`[greedysearch] Action ${$+1}/${z.length} [${j.type}]: ${(j.query||j.url).slice(0,80)}
373
+ `);let Y=await rl(j,{locale:a,short:l,usedQueries:_,usedUrls:s,maxChars:8000});if(S.push(Y),ie++,j.type==="search")fe++;if(j.type==="fetchUrl")ge++;if(!Y.ok)oe.push({round:E,type:j.type,target:j.query||j.url,error:Y.error}),process.stderr.write(`[greedysearch] Action failed: ${Y.error}
374
+ `)}let we=S.filter(($)=>$.action.type==="search"),$e=S.filter(($)=>$.action.type==="fetchUrl");st(c,{roundNumber:E,actions:S}),b=bl([b,we.flatMap(($)=>$.sources||[]),$e.flatMap(($)=>$.sources||[])]);for(let $ of $e)if($.fetchResult)w.push($.fetchResult);w=Tr(w);let Et=Math.max(0,g.maxSources-w.filter(($)=>$?.content||$?.contentChars>100).length);if(Et>0&&b.length>0){process.stderr.write(`PROGRESS:research:round-${E}:fetching
375
+ `);let $=await Ia(b,Math.min(Et,b.length),8000,Math.min(3,Et||1));w=Tr([...w,...$]),b=ke(b,w)}w=Nr(w,b),process.stderr.write(`PROGRESS:research:round-${E}:evidence
376
+ `);let u=await hl({query:t,questions:c,fetchedSources:w,extractedSourceKeys:C});if(u.error)process.stderr.write(`[greedysearch] Evidence extraction failed: ${u.error}
377
+ `);k=[...k,...u.evidence];for(let $ of u.evidence)st(c,{roundNumber:E,learningPayload:{answeredQuestions:$.answers||[],newQuestions:$.newQuestions||[]}});let Hr=S.map(($)=>({query:$.action.query||$.action.url||"",researchGoal:$.action.researchGoal||""}));process.stderr.write(`PROGRESS:research:round-${E}:learning
378
+ `);let H={learnings:[],followUpQueries:[],gaps:[]},ce="";try{let $=await x(wl(t,Hr,we.map((j)=>({query:j.action.query,researchGoal:j.action.researchGoal,error:j.error||"",engines:tl(j.result)})),w,c,k),{timeoutMs:120000});H={...H,...lt($,H)}}catch($){ce=$.message,process.stderr.write(`[greedysearch] Learning extraction failed: ${$.message}
379
+ `)}let me=Array.isArray(H.learnings)?H.learnings.map(($)=>String($)).filter(Boolean).slice(0,8):[],Ct=Array.isArray(H.gaps)?H.gaps.map(($)=>String($)).filter(Boolean).slice(0,6):[];h=B([...h,...me]),m=B([...m,...Ct]),st(c,{roundNumber:E,actions:[],learningPayload:H,gaps:Ct}),p.push({round:E,actions:S.map(($)=>({type:$.action.type,query:$.action.query||"",url:$.action.url||"",researchGoal:$.action.researchGoal||"",error:$.error||"",sourceCount:$.sources?.length||0})),learnings:me,gaps:Ct,evidence:u.evidence,evidenceError:u.error,learningError:ce}),process.stderr.write(`PROGRESS:research:round-${E}:evaluating
380
+ `);let L=await qa(t,p,h,m,N);N.push(L.score),m=B([...m,...L.knowledgeGaps||[]]),st(c,{roundNumber:E,gaps:L.knowledgeGaps||[]});let be=Cr({sources:b,fetchedSources:w,gaps:m,questions:c,rounds:p,qualityScore:L.score,qualityThreshold:i,maxSources:g.maxSources,requireCitations:!1,requireQuestions:!1});if(process.stderr.write(`[greedysearch] Quality score round ${E}: ${L.score.toFixed(1)} (shouldContinue: ${L.shouldContinue}, floor: ${be.floorMet})
381
+ `),L.score>=i&&be.floorMet&&(!L.shouldContinue||L.terminationReason==="quality_threshold")){R=L.terminationReason||"quality_threshold",process.stderr.write(`[greedysearch] Research floor reached (score: ${L.score.toFixed(1)}). Terminating early.
382
+ `);break}let I=Math.max(1,Math.ceil(J/2)),F=(H.followUpQueries||[]).map(($)=>({type:"search",query:A(String($)),researchGoal:"Follow-up from learning extraction"})).filter(($)=>$.query&&$.query.toLowerCase()!==t.toLowerCase()).slice(0,I);if(F.length<I&&L.nextActions.length>0){let $=L.nextActions.map((Y)=>Pr(Y)).filter(Boolean);F=[...F,...$].slice(0,I)}if(F.length<I&&m.length>0){let $=ua(m,t,_,I-F.length,v+1),j=$.map((Y)=>({type:"search",query:Y.query,researchGoal:Y.researchGoal}));if(F=[...F,...j].slice(0,I),$.length>0)process.stderr.write(`[greedysearch] Generated ${$.length} gap-driven fallback actions.
383
383
  `)}y=F.length>=I?F:null}process.stderr.write(`PROGRESS:research:final-report
384
- `);let T={answer:h.length?h.map((j)=>`- ${j}`).join(`
385
- `):"Research completed, but no structured learnings were extracted.",agreement:{level:"mixed",summary:"Research synthesis fallback."},differences:[],caveats:[],claims:[],recommendedSources:b.slice(0,4).map((j)=>j.id),synthesized:!1};try{let j=await x(fl(t,o,b,c,_),{timeoutMs:180000}),E=lt(j,{}),R=Array.isArray(E?.claims)&&E.claims.length>0;T={...T,...E,rawAnswer:j.answer||"",geminiSources:j.sources||[],synthesized:R}}catch(j){process.stderr.write(`[greedysearch] Final report failed: ${j.message}
386
- `),T.error=j.message}if(!(T.synthesized===!0&&Array.isArray(T.claims)&&T.claims.length>0)&&_.length>0){process.stderr.write(`[greedysearch] Falling back to evidence-based synthesis (no per-round learnings).
387
- `);try{let j=gl(t,b,c,_),E=await x(j,{timeoutMs:180000}),R=lt(E,{});T={...T,...R,rawAnswer:E.answer||T.answer||"",geminiSources:E.sources||T.geminiSources||[],synthesized:!0,synthesisMode:"evidence_fallback"}}catch(j){process.stderr.write(`[greedysearch] Evidence-based synthesis failed: ${j.message}
388
- `),T.evidenceFallbackError=j.message}}let Pr=new Date().toISOString(),Lr=Date.now()-Tr,Wr=s.at(-1)||0;w=Dr(w,b),process.stderr.write(`PROGRESS:research:audit-citations
389
- `);let it=wl(T.answer||"",b);bl(c,T,it);let Q=jr({sources:b,fetchedSources:w,synthesis:T,citationAudit:it,gaps:m,questions:c,rounds:o,qualityScore:Wr,qualityThreshold:i,maxSources:g.maxSources});if(Q.floorMet&&J==="max_rounds")J="done_floor_met";else if(!Q.floorMet&&J==="quality_threshold")J="max_rounds_floor_unmet";let pe={startedAt:Or,finishedAt:Pr,durationMs:Lr,engines:At,synthesizer:"gemini",rounds:o.length,actionsRun:le,searches:ie,fetches:fe,sourcesFetched:w.filter((j)=>j?.contentChars>100).length,engineFailures:ge,terminationReason:J,floorMet:Q.floorMet},tt=null,ft;if(f){process.stderr.write(`PROGRESS:research:bundle
390
- `);try{tt=await yl({query:t,rounds:o,sources:b,fetchedSources:w,evidenceItems:_,synthesis:T,citationAudit:it,floor:Q,manifest:pe,allGaps:m,questions:c,outDir:p}),ft=tt.sourceFiles,delete tt.sourceFiles}catch(j){tt={error:j.message||String(j)},ft=await ne(w)}}else ft=await ne(w);return process.stderr.write(`PROGRESS:research:done
391
- `),{query:t,_research:{mode:"iterative",breadth:g.breadth,iterations:g.iterations,maxSources:g.maxSources,rounds:o,learnings:h,gaps:m,evidence:_,questions:c,questionProgress:ae(c),qualityHistory:s,terminationReason:J,qualityThreshold:i,floor:Q,bundle:tt,manifest:pe},_citationAudit:it,_sources:b,_fetchedSources:ft,_synthesis:T,_confidence:{sourcesCount:b.length,fetchedSourceSuccessRate:w.length>0?Number((w.filter((j)=>j.contentChars>100).length/w.length).toFixed(2)):0,agreementLevel:T.agreement?.level||"mixed",floorMet:Q.floorMet}}}function Cr(t){let e=new Map;for(let n of t){let a=n?.id||M(n?.finalUrl||n?.url||"");if(!a)continue;let l=e.get(a);if(!l||(n.contentChars||0)>(l.contentChars||0))e.set(a,n)}let r=[];for(let n of e.values()){let a=String(n.content||n.snippet||""),l=r.findIndex((i)=>{let f=String(i.content||i.snippet||"");if(a.length<400||f.length<400)return!1;return Dt(a.slice(0,4000),f.slice(0,4000))>=0.9});if(l===-1){r.push(n);continue}if((n.contentChars||0)>(r[l].contentChars||0))r[l]=n}return r}export{Nr as validateAction,_t as updateQuestionLedger,vr as tokenSet,bf as runResearchMode,Ha as resolveGreedySearchSearchScript,rl as queriesToActions,el as parseActionPlan,Za as normalizeResearchQueries,Dt as jaccardSimilarity,Er as isDuplicateQuery,$l as createQuestionLedger,jr as computeResearchFloor,Ka as clampResearchOptions,Ia as buildFallbackQueriesFromGaps,wl as auditCitations};
384
+ `);let O={answer:h.length?h.map((v)=>`- ${v}`).join(`
385
+ `):"Research completed, but no structured learnings were extracted.",agreement:{level:"mixed",summary:"Research synthesis fallback."},differences:[],caveats:[],claims:[],recommendedSources:b.slice(0,4).map((v)=>v.id),synthesized:!1};try{let v=await x($l(t,p,b,c,k),{timeoutMs:180000}),E=lt(v,{}),J=Array.isArray(E?.claims)&&E.claims.length>0;O={...O,...E,rawAnswer:v.answer||"",geminiSources:v.sources||[],synthesized:J}}catch(v){process.stderr.write(`[greedysearch] Final report failed: ${v.message}
386
+ `),O.error=v.message}if(!(O.synthesized===!0&&Array.isArray(O.claims)&&O.claims.length>0)&&k.length>0){process.stderr.write(`[greedysearch] Falling back to evidence-based synthesis (no per-round learnings).
387
+ `);try{let v=cl(t,b,c,k),E=await x(v,{timeoutMs:180000}),J=lt(E,{});O={...O,...J,rawAnswer:E.answer||O.answer||"",geminiSources:E.sources||O.geminiSources||[],synthesized:!0,synthesisMode:"evidence_fallback"}}catch(v){process.stderr.write(`[greedysearch] Evidence-based synthesis failed: ${v.message}
388
+ `),O.evidenceFallbackError=v.message}}let Ar=new Date().toISOString(),Rr=Date.now()-Mr,Jr=N.at(-1)||0;w=Nr(w,b),process.stderr.write(`PROGRESS:research:audit-citations
389
+ `);let it=_l(O.answer||"",b);jl(c,O,it);let Q=Cr({sources:b,fetchedSources:w,synthesis:O,citationAudit:it,gaps:m,questions:c,rounds:p,qualityScore:Jr,qualityThreshold:i,maxSources:g.maxSources});if(Q.floorMet&&R==="max_rounds")R="done_floor_met";else if(!Q.floorMet&&R==="quality_threshold")R="max_rounds_floor_unmet";let pe={startedAt:Wr,finishedAt:Ar,durationMs:Rr,engines:Rt,synthesizer:"gemini",rounds:p.length,actionsRun:ie,searches:fe,fetches:ge,sourcesFetched:w.filter((v)=>v?.contentChars>100).length,engineFailures:oe,terminationReason:R,floorMet:Q.floorMet},tt=null,ft;if(f){process.stderr.write(`PROGRESS:research:bundle
390
+ `);try{tt=await El({query:t,rounds:p,sources:b,fetchedSources:w,evidenceItems:k,synthesis:O,citationAudit:it,floor:Q,manifest:pe,allGaps:m,questions:c,outDir:o}),ft=tt.sourceFiles,delete tt.sourceFiles}catch(v){tt={error:v.message||String(v)},ft=await ae(w)}}else ft=await ae(w);return process.stderr.write(`PROGRESS:research:done
391
+ `),{query:t,_research:{mode:"iterative",breadth:g.breadth,iterations:g.iterations,maxSources:g.maxSources,rounds:p,learnings:h,gaps:m,evidence:k,questions:c,questionProgress:le(c),qualityHistory:N,terminationReason:R,qualityThreshold:i,floor:Q,bundle:tt,manifest:pe},_citationAudit:it,_sources:b,_fetchedSources:ft,_synthesis:O,_confidence:{sourcesCount:b.length,fetchedSourceSuccessRate:w.length>0?Number((w.filter((v)=>v.contentChars>100).length/w.length).toFixed(2)):0,agreementLevel:O.agreement?.level||"mixed",floorMet:Q.floorMet}}}function Tr(t){let e=new Map;for(let n of t){let a=n?.id||M(n?.finalUrl||n?.url||"");if(!a)continue;let l=e.get(a);if(!l||(n.contentChars||0)>(l.contentChars||0))e.set(a,n)}let r=[];for(let n of e.values()){let a=String(n.content||n.snippet||""),l=r.findIndex((i)=>{let f=String(i.content||i.snippet||"");if(a.length<400||f.length<400)return!1;return Dt(a.slice(0,4000),f.slice(0,4000))>=0.9});if(l===-1){r.push(n);continue}if((n.contentChars||0)>(r[l].contentChars||0))r[l]=n}return r}export{Pr as validateAction,st as updateQuestionLedger,Er as tokenSet,Nf as runResearchMode,Za as resolveGreedySearchSearchScript,gl as queriesToActions,fl as parseActionPlan,xa as normalizeResearchQueries,Dt as jaccardSimilarity,Or as isDuplicateQuery,kl as createQuestionLedger,Cr as computeResearchFloor,Xa as clampResearchOptions,ua as buildFallbackQueriesFromGaps,_l as auditCitations};