@duckmind/dm-darwin-x64 0.43.8 → 0.49.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. package/dm +0 -0
  2. package/extensions/.dm-extensions.json +147 -218
  3. package/extensions/dm-9router-ext/package.json +0 -6
  4. package/extensions/dm-9router-ext/src/index.js +1 -1
  5. package/extensions/dm-ask-user/package.json +0 -4
  6. package/extensions/dm-caveman/LICENSE +21 -0
  7. package/extensions/dm-caveman/extensions/caveman.js +29 -0
  8. package/extensions/dm-caveman/package.json +20 -0
  9. package/extensions/dm-cli-anything/index.js +5 -5
  10. package/extensions/dm-cli-anything/package.json +0 -7
  11. package/extensions/dm-cli-anything/skills/cli-anything/SKILL.md +1 -3
  12. package/extensions/dm-context/package.json +0 -1
  13. package/extensions/dm-cua/package.json +0 -7
  14. package/extensions/dm-fff/package.json +0 -6
  15. package/extensions/dm-goal/package.json +0 -8
  16. package/extensions/dm-grill-me/index.js +2 -2
  17. package/extensions/dm-grill-me/package.json +0 -7
  18. package/extensions/dm-image2/package.json +0 -8
  19. package/extensions/dm-subagents/package.json +0 -11
  20. package/extensions/dm-tasks/package.json +0 -7
  21. package/extensions/dm-usage/package.json +0 -7
  22. package/extensions/greedysearch-dm/bin/cdp-greedy.mjs +1 -8
  23. package/extensions/greedysearch-dm/bin/gschrome.mjs +3 -3
  24. package/extensions/greedysearch-dm/bin/launch-visible.mjs +3 -3
  25. package/extensions/greedysearch-dm/bin/launch.mjs +4 -4
  26. package/extensions/greedysearch-dm/bin/search.mjs +106 -106
  27. package/extensions/greedysearch-dm/extractors/consensus.mjs +35 -35
  28. package/extensions/greedysearch-dm/extractors/gemini.mjs +38 -38
  29. package/extensions/greedysearch-dm/extractors/logically.mjs +35 -35
  30. package/extensions/greedysearch-dm/package.json +0 -10
  31. package/extensions/greedysearch-dm/src/search/chrome.mjs +23 -23
  32. package/extensions/greedysearch-dm/src/search/fetch-source.mjs +35 -35
  33. package/extensions/greedysearch-dm/src/search/research.mjs +77 -77
  34. package/extensions/greedysearch-dm/src/search/synthesis-runner.mjs +10 -10
  35. package/package.json +1 -1
  36. package/extensions/dm-9router-ext/README.md +0 -142
  37. package/extensions/dm-9router-ext/tsconfig.json +0 -17
  38. package/extensions/dm-ask-user/README.md +0 -226
  39. package/extensions/dm-cli-anything/README.md +0 -33
  40. package/extensions/dm-cli-anything/vendor/UPSTREAM.md +0 -14
  41. package/extensions/dm-context/AGENTS.md +0 -41
  42. package/extensions/dm-context/README.md +0 -18
  43. package/extensions/dm-context/tsconfig.json +0 -16
  44. package/extensions/dm-cua/README.md +0 -24
  45. package/extensions/dm-fff/README.md +0 -36
  46. package/extensions/dm-fff/tsconfig.json +0 -13
  47. package/extensions/dm-goal/CHANGELOG.md +0 -12
  48. package/extensions/dm-goal/README.md +0 -29
  49. package/extensions/dm-goal/tsconfig.json +0 -15
  50. package/extensions/dm-grill-me/DESIGN.md +0 -301
  51. package/extensions/dm-grill-me/README.md +0 -82
  52. package/extensions/dm-grill-me/tsconfig.json +0 -17
  53. package/extensions/dm-image2/README.md +0 -33
  54. package/extensions/dm-subagents/CHANGELOG.md +0 -1211
  55. package/extensions/dm-subagents/README.md +0 -29
  56. package/extensions/dm-subagents/biome.json +0 -26
  57. package/extensions/dm-subagents/tsconfig.json +0 -18
  58. package/extensions/dm-tasks/README.md +0 -30
  59. package/extensions/dm-tasks/biome.json +0 -26
  60. package/extensions/dm-tasks/tsconfig.json +0 -14
  61. package/extensions/dm-usage/README.md +0 -29
  62. package/extensions/greedysearch-dm/README.md +0 -222
@@ -1,6 +1,6 @@
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,`
1
+ import{createRequire as Gr}from"node:module";var Hr=Object.defineProperty;var Fr=(t)=>t;function Yr(t,e){this[t]=Fr.bind(null,e)}var et=(t,e)=>{for(var r in e)Hr(t,r,{get:e[r],enumerable:!0,configurable:!0,set:Yr.bind(e,r)})};var gt=(t,e)=>()=>(t&&(e=t(t=0)),e);var Vr=Gr(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 hi,tmpdir as An}from"node:os";import{spawn as Rn}from"node:child_process";import{dirname as Jn,join as dn}from"node:path";import{fileURLToPath as Hn}from"node:url";import{basename as Fn}from"node:path";import{existsSync as mt,mkdirSync as Yn,readFileSync as Ke,writeFileSync as Gn}from"node:fs";import{homedir as Vn}from"node:os";import{join as Ue}from"node:path";import{tmpdir as _t}from"node:os";import{platform as vi,tmpdir as Zn}from"node:os";import{existsSync as bt,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 kt}from"node:os";import{existsSync as yt,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 st}from"node:os";function _n(t={}){return{...He,...t}}function Fe(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=Fe(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:{...He,"user-agent":l||de},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=Ye(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=Ge(y,h),k=Ve(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 Ye(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 Ge(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||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?`
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 Ve(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
5
  > ${p.description}`:"",y=p?.stargazers_count==null?"":` ⭐ ${p.stargazers_count}`,b=p?.language?` · ${p.language}`:"",w=`# ${r}/${n}${y}${b}${c}
6
6
 
@@ -40,7 +40,7 @@ ${o}`,tree:f}}return{ok:!1,error:"Unsupported GitHub URL type"}}catch(f){return{
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 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:`
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=Fn(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 @@ ${o}`,tree:f}}return{ok:!1,error:"Unsupported GitHub URL type"}}catch(f){return{
248
248
  };
249
249
  } catch(_) {}
250
250
  })();
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
- [greedysearch] Available engines: ${Object.keys(Jt).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}
251
+ `})])}function zn(){try{if(mt(G)){let t=Ke(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)=>dt[a]),n=e.engines.filter((a)=>!dt[a]);if(n.length>0)process.stderr.write(`[greedysearch] Warning: ignoring unknown engine(s) in ${G}: ${n.join(", ")}
252
+ [greedysearch] Available engines: ${Object.keys(dt).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 Sn(){try{if(!mt(Gt))Yn(Gt,{recursive:!0});if(!mt(G))Gn(G,JSON.stringify({engines:Vt,synthesizer:Zt},null,2)+`
255
+ `,"utf8")}catch{}}function un(){try{if(mt(G)){let t=Ke(G,"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 ${G}
256
256
  [greedysearch] Available synthesizers: ${We.join(", ")}
257
- [greedysearch] Falling back to default: ${dt}
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(", ")}
257
+ [greedysearch] Falling back to default: ${Zt}
258
+ `)}}}catch{}return Zt}function qn(){try{if(bt(V)){let t=Ie(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)=>Ht[a]),n=e.engines.filter((a)=>!Ht[a]);if(n.length>0)process.stderr.write(`[greedysearch] Warning: ignoring unknown engine(s) in ${V}: ${n.join(", ")}
259
259
  [greedysearch] Available engines: ${Object.keys(Ht).join(", ")}
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}
260
+ `);if(r.length>0)return r;process.stderr.write(`[greedysearch] Warning: no valid engines in ${V}, falling back to defaults: ${Kt.join(", ")}
261
+ `)}}}catch{}return Kt}function ta(){try{if(!bt(Bt))Bn(Bt,{recursive:!0});if(!bt(V))Kn(V,JSON.stringify({engines:Kt,synthesizer:Ut},null,2)+`
262
+ `,"utf8")}catch{}}function ea(){try{if(bt(V)){let t=Ie(V,"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 ${V}
263
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(", ")}
264
+ [greedysearch] Falling back to default: ${Ut}
265
+ `)}}}catch{}return Ut}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 Qt(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 zt(t){try{let e=await ue();await P(["evalraw",e,"Target.closeTarget",JSON.stringify({targetId:t})])}catch{}}function la(){try{if(yt(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
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}
268
+ `);if(r.length>0)return r;process.stderr.write(`[greedysearch] Warning: no valid engines in ${Z}, falling back to defaults: ${Xt.join(", ")}
269
+ `)}}}catch{}return Xt}function ia(){try{if(!yt(It))ra(It,{recursive:!0});if(!yt(Z))na(Z,JSON.stringify({engines:Xt,synthesizer:xt},null,2)+`
270
+ `,"utf8")}catch{}}function fa(){try{if(yt(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
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)
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
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}
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=Ye(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=Ge(f,e),p=Ve(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=Fe(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
276
  `)}}if(Ln(t)?.type==="post"){process.stderr.write(`[greedysearch] Using Reddit JSON API for: ${t.slice(0,60)}...
277
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`
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 Qt();try{let l=await ha(a,t,e);if(l.content&&l.content.length>100)return l}finally{await zt(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 Qt()}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 rr(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: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})...
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 zt(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
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
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
293
  `);o+=1,process.stderr.write(`PROGRESS:fetch:${o}/${a.length}
294
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`
295
+ `),i}async function ma(t){let e=await Qt();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 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=`---
301
+ `]);return{url:t,content:r}}catch(r){return{url:t,content:null,error:r.message}}finally{await zt(e)}}var ii,Je,de="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36",He,yn,vn,jn,Dn,Ze,Pn,Xn,xn,Qe,Ni,Ti,Oi,Pi,Gt,G,Vt,Zt="gemini",We,dt,Li,Wi,Mi,ze,Ai,Ri,Ji,di,Bt,V,Kt,Ut="gemini",Me,Ht,Hi,Fi,Yi,at,Gi,Vi,Zi,Bi,Ki,Ui,Ii,Se,Xi,xi,Qi,zi,P,er,ef,rf,nf,af,It,Z,Xt,xt="gemini",Ae,Ft,lf,ff,Re;var ir=gt(()=>{ii=$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:()=>""});He={"user-agent":de,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(Hn(import.meta.url)),xn=dn(Xn,"..","bin","cdp.mjs");Qe=`${_t().replaceAll("\\","/")}/greedysearch-chrome-profile`,Ni=`${Qe}/DevToolsActivePort`,Ti=`${_t().replaceAll("\\","/")}/cdp-pages.json`,Oi=`${_t().replaceAll("\\","/")}/greedysearch-chrome-mode`,Pi=`${_t().replaceAll("\\","/")}/greedysearch-visible-recovery.jsonl`,Gt=Ue(Vn(),".dm"),G=Ue(Gt,"greedyconfig"),Vt=["perplexity","google","chatgpt"];Sn();We=["gemini","chatgpt"];dt={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"},Li=zn(),Wi=un(),Mi=Math.max(1,Number.parseInt(process.env.GREEDY_FETCH_CONCURRENCY||"5",10)||5);process.env.CDP_PROFILE_DIR=Qe;ze=`${kt().replaceAll("\\","/")}/greedysearch-chrome-profile`,Ai=`${ze}/DevToolsActivePort`,Ri=`${kt().replaceAll("\\","/")}/cdp-pages.json`,Ji=`${kt().replaceAll("\\","/")}/greedysearch-chrome-mode`,di=`${kt().replaceAll("\\","/")}/greedysearch-visible-recovery.jsonl`,Bt=Xe(Un(),".dm"),V=Xe(Bt,"greedyconfig"),Kt=["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"},Hi=qn(),Fi=ea(),Yi=Math.max(1,Number.parseInt(process.env.GREEDY_FETCH_CONCURRENCY||"5",10)||5);process.env.CDP_PROFILE_DIR=ze;at=Zn().replaceAll("\\","/"),Gi=`${at}/greedysearch-chrome-metadata.json`,Vi=`${at}/greedysearch-chrome-launch.lock`,Zi=`${at}/greedysearch-chrome.pid`,Bi=`${at}/greedysearch-chrome-mode`,Ki=`${at}/greedysearch-chrome-last-activity`,Ui=Number.parseInt(process.env.GREEDY_SEARCH_IDLE_TIMEOUT_MINUTES||"5",10)||5,Ii=import.meta.dirname||new URL(".",import.meta.url).pathname.replace(/^\/([A-Z]:)/,"$1"),Se=An().replaceAll("\\","/"),Xi=`${Se}/greedysearch-chrome.pid`,xi=`${Se}/greedysearch-chrome-last-activity`,Qi=Number.parseInt(process.env.GREEDY_SEARCH_IDLE_TIMEOUT_MINUTES||"5",10)||5,zi=Number.parseInt(process.env.GREEDY_SEARCH_VISIBLE_IDLE_TIMEOUT_MINUTES||"60",10)||60,P=xe;er=`${st().replaceAll("\\","/")}/greedysearch-chrome-profile`,ef=`${er}/DevToolsActivePort`,rf=`${st().replaceAll("\\","/")}/cdp-pages.json`,nf=`${st().replaceAll("\\","/")}/greedysearch-chrome-mode`,af=`${st().replaceAll("\\","/")}/greedysearch-visible-recovery.jsonl`,It=tr(aa(),".dm"),Z=tr(It,"greedyconfig"),Xt=["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"},lf=la(),ff=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,7 +306,7 @@ status: ${r.status||""}
306
306
  chars: ${r.contentChars||r.content.length}
307
307
  ---
308
308
 
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?`
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 St={};et(St,{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
311
  > ${p.description}`:"",y=p?.stargazers_count==null?"":` ⭐ ${p.stargazers_count}`,b=p?.language?` · ${p.language}`:"",w=`# ${r}/${n}${y}${b}${c}
312
312
 
@@ -318,26 +318,26 @@ ${m.map((k)=>` ${k.type==="dir"?"\uD83D\uDCC1":"\uD83D\uDCC4"} ${k.path}`).join
318
318
  `);return{ok:!0,title:`${r}/${n}/${i}`,content:`[Directory: ${i}]
319
319
 
320
320
  Files:
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=`
321
+ ${o}`,tree:f}}return{ok:!1,error:"Unsupported GitHub URL type"}}catch(f){return{ok:!1,error:f.message}}}var hr;var ut=gt(()=>{hr={"user-agent":"GreedySearch/1.0",accept:"application/vnd.github+json","x-github-api-version":"2022-11-28"}});var qt={};et(qt,{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 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,`
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:()=>Ha,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=da(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 da(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||$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}
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 Ha(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 Fa}from"node:child_process";import{mkdirSync as te,writeFileSync as W}from"node:fs";import{join as E}from"node:path";import{fileURLToPath as Ya}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"],ke=["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 Tt(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 Ot(t="",e=""){let r=Tt(t),n=Tt(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 Pt(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,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 ke){let a=n.replace(/\.com$/,"");if(e.includes(a))r.push(n)}return[...new Set(r)]}function _e(t,e){return t===e||t.endsWith(`.${e}`)}function Lt(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=Tt(m.title||""),b=Ur(c),w=Pt(b,y,c),k=0;if(a.some((N)=>_e(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)=>_e(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=Ot(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:Ot(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 se(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=Ot(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 ve,writeFileSync as Sr}from"node:fs";import{homedir as ur}from"node:os";import{join as je}from"node:path";import{tmpdir as pt}from"node:os";var ht=`${pt().replaceAll("\\","/")}/greedysearch-chrome-profile`,Rl=`${ht}/DevToolsActivePort`,Jl=`${pt().replaceAll("\\","/")}/cdp-pages.json`,dl=`${pt().replaceAll("\\","/")}/greedysearch-chrome-mode`,Hl=`${pt().replaceAll("\\","/")}/greedysearch-visible-recovery.jsonl`,Mt=je(ur(),".dm"),Y=je(Mt,"greedyconfig"),At=["perplexity","google","chatgpt"],Rt="gemini";function qr(){try{if(ot(Y)){let t=ve(Y,"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)=>Wt[a]),n=e.engines.filter((a)=>!Wt[a]);if(n.length>0)process.stderr.write(`[greedysearch] Warning: ignoring unknown engine(s) in ${Y}: ${n.join(", ")}
330
+ [greedysearch] Available engines: ${Object.keys(Wt).join(", ")}
331
+ `);if(r.length>0)return r;process.stderr.write(`[greedysearch] Warning: no valid engines in ${Y}, falling back to defaults: ${At.join(", ")}
332
+ `)}}}catch{}return At}function tn(){try{if(!ot(Mt))zr(Mt,{recursive:!0});if(!ot(Y))Sr(Y,JSON.stringify({engines:At,synthesizer:Rt},null,2)+`
333
+ `,"utf8")}catch{}}tn();var nt=["gemini","chatgpt"];function en(){try{if(ot(Y)){let t=ve(Y,"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 ${Y}
334
334
  [greedysearch] Available synthesizers: ${nt.join(", ")}
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?`
335
+ [greedysearch] Falling back to default: ${Rt}
336
+ `)}}}catch{}return Rt}var Wt={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"},De=qr(),Jt=De,Fl=en(),Yl=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 gn}from"node:child_process";import{basename as nn}from"node:path";function $t(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=nn(e||"").toLowerCase();if(n==="node"||n==="node.exe")return e;return"node"}import{existsSync as Ee}from"node:fs";import{dirname as an,join as ct}from"node:path";import{fileURLToPath as ln}from"node:url";function Ce(t){return ln(new URL(".",t)).replace(/^\/([A-Z]:)/,"$1")}function fn(t,e=Ee){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 Ne(t,{moduleDir:e,entrypoint:r=process.argv[1],env:n=process.env,exists:a=Ee}={}){let l=r?an(r):null,i=n.GREEDY_SEARCH_EXTENSION_DIR?.trim()||null,f=[e?ct(e,"..","..","extractors",t):null,e?ct(e,"..","extractors",t):null,i?ct(i,"extractors",t):null,l?ct(l,"..","extractors",t):null];return fn(f,a)}var on=Ce(import.meta.url),pn={gemini:"gemini.mjs",chatgpt:"chatgpt.mjs"};function hn(t="gemini"){let e=String(t||"gemini").toLowerCase();if(e==="gem")return"gemini";if(e==="gpt")return"chatgpt";return e}async function wn(t,e,{tabPrefix:r=null,timeoutMs:n=180000,visible:a=null}={}){let l=hn(t),i=pn[l];if(!i||!nt.includes(l))throw Error(`Unsupported synthesizer "${t}". Supported: ${nt.join(", ")}`);let f=Ne(i,{moduleDir:on});return new Promise((o,g)=>{let p=r?["--tab",String(r)]:[],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=gn($t(),[f,"--stdin",...p],{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(),g(Error(`${l} prompt timed out after ${n/1000}s`))},n);m.on("close",(w)=>{if(clearTimeout(b),w!==0){g(Error(y.trim()||`${l} extractor failed`));return}try{o(JSON.parse(c.trim()))}catch{g(Error(`bad JSON from ${l}: ${c.slice(0,100)}`))}})})}async function x(t,e={}){return wn("gemini",t,e)}var Ga=Ya(new URL(".",import.meta.url)).replace(/^\/([A-Z]:)/,"$1"),Va=E(Ga,"..","..","bin","search.mjs"),Za=E(process.cwd(),".dm","greedysearch-research");function Ba(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 Ka(...t){let{fetchMultipleSources:e}=await Promise.resolve().then(() => (ir(),lr));return e(...t)}async function le(...t){let{writeSourcesToFiles:e}=await Promise.resolve().then(() => (or(),gr));return e(...t)}function Ua({breadth:t=3,iterations:e=2,maxSources:r}){let n=ee(t,1,5,3),a=ee(e,1,3,2),l=ee(r??Math.max(5,n*a*2),3,12,8);return{breadth:n,iterations:a,maxSources:l}}function ee(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 Ia(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||"";re(f,p,h,{exclude:o})}if(a)re(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;re(f,p.query,p.researchGoal,{exclude:o})}}return f.slice(0,r)}function re(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 xa(Xa(String(t)))}function Xa(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 xa(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 Et(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(Et(l,i)>=r)return!0;return!1}function Qa(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 za(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 Sa(t,e,r,n,a){try{let l=await x(Qa(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 ua(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 qa(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 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(`
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 tl(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 $l(t.query,{locale:e,short:r}),o=Lt(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 el(i,l);a.add(i);let o=nl(i),g={id:"",canonicalUrl:f.finalUrl||i,displayUrl:f.url||i,domain:o,title:f.title||i,engines:["fetch"],engineCount:1,perEngine:{},sourceType:Pt(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 el(t,e){return await rl(t,e)}async function rl(t,e=8000){let r=Date.now();try{let{parseGitHubUrl:n,fetchGitHubContent:a}=await Promise.resolve().then(() => (ut(),St)),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(() => qt),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(() => qt),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 nl(t){try{return new URL(t).hostname.toLowerCase().replace(/^www\./,"")}catch{return""}}async function al(t,e){let r=[],{parseGitHubUrl:n}=await Promise.resolve().then(() => (ut(),St));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 ll(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 il(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 Dt(t){return M(t?.finalUrl||t?.canonicalUrl||t?.url||"")||t?.id||""}function fl(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(Dt(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 gl(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=Dt(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 ol({query:t,questions:e,fetchedSources:r,extractedSourceKeys:n}){let a=(r||[]).filter((l)=>(l?.content||l?.snippet)&&!n.has(Dt(l)));if(a.length===0)return{evidence:[],error:""};try{let l=await x(fl(t,e,a,n),{timeoutMs:120000}),i=lt(l,{extractions:[]}),f=gl(i,a);for(let o of a){let g=Dt(o);if(g)n.add(g)}return{evidence:f,error:""}}catch(l){return{evidence:[],error:l.message||String(l)}}}function pl(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 hl(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 wl(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 $l(t,{locale:e=null,short:r=!0}={}){let n=[Va,"all","--inline","--stdin","--fast"];if(!r)n.push("--full");if(e)n.push("--locale",e);return new Promise((a,l)=>{let i=Fa($t(),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(ml(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 cl(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 ml(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 bl(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=ie(l),k=(l||[]).filter((N)=>!N.createdRound||N.reason==="Original research question"),C=ie(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 yl(t){return[{id:"Q1",question:T(A(t),320),status:"open",reason:"Original research question",evidence:[],sourceIds:[]}]}function _l(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||Et(n.question||"",r)>=0.82)}function ne(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:_l(t),question:a,status:"open",reason:T(r,240),createdRound:n,evidence:[],sourceIds:[]};return t.push(i),i}function jt(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 ie(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 vt(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)ne(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"){jt(t,o,{round:e});continue}let g=o?.id||o?.question;if(!g&&o?.question){let p=ne(t,o.question,{reason:"Answered during learning extraction",round:e});if(p)jt(t,p.id,{round:e});continue}jt(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)ne(t,o,{reason:"Discovered gap/follow-up",round:e});return t}function kl(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 sl(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=Et(l.question||"",o.claim||"");if(g>f)f=g,i=o}if(l.id==="Q1"||f>=0.18)jt(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 vl(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 ae(t,e="None recorded."){let r=B(t);return r.length?r.map((n)=>`- ${n}`).join(`
358
+ `):e}async function jl({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||E(Za,`${m}_${Ba(t)}`),y=E(c,"reports"),b=E(c,"sources"),w=E(c,"data");te(y,{recursive:!0}),te(b,{recursive:!0}),te(w,{recursive:!0});let k=await le(n,b),C=B([...g,...e.flatMap((_)=>_.gaps||[])]);return W(E(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",vl(p),"","## Open gaps",ae(C),""].join(`
359
+ `),"utf8"),W(E(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(E(y,"SUMMARY.md"),String(l.answer||""),"utf8"),W(E(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(E(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(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}
363
+ `),"utf8"),W(E(y,"GAPS.md"),["# Gaps and caveats","","## Caveats",ae(l.caveats||[]),"","## Research gaps",ae(C),""].join(`
364
+ `),"utf8"),W(E(w,"manifest.json"),JSON.stringify({...o,floor:f,citationAudit:i},null,2),"utf8"),W(E(w,"rounds.json"),JSON.stringify(e,null,2),"utf8"),W(E(w,"sources.json"),JSON.stringify(r,null,2),"utf8"),W(E(w,"questions.json"),JSON.stringify(p,null,2),"utf8"),W(E(w,"evidence.json"),JSON.stringify(a,null,2),"utf8"),W(E(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:E(c,"STATUS.md"),summaryPath:E(y,"SUMMARY.md"),manifestPath:E(w,"manifest.json"),sourceCount:k.length,sourceFiles:k}}async function Cf({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=Ua({breadth:e,iterations:r,maxSources:n}),p=[],h=[],m=[],c=yl(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(),fe=0,ge=0,oe=0,pe=[];process.stderr.write(`[greedysearch] Research mode: breadth ${g.breadth}, iterations ${g.iterations}, qualityThreshold ${i}, engines ${Jt.join(",")}, synthesizer gemini
366
+ `);for(let v=0;v<g.iterations;v++){let D=v+1,J=Math.max(1,Math.ceil(g.breadth/2**v));if(process.stderr.write(`PROGRESS:research:round-${D}:planning
367
+ `),!y)try{let $=await x(qa(t,J,h,m,[...s]),{timeoutMs:120000}),j=ll($,J);if(v===0)j.unshift({type:"search",query:t,researchGoal:"Original user query"});j=await al(j,s),y=j}catch($){process.stderr.write(`[greedysearch] Action planning failed, using fallback queries: ${$.message}
368
+ `);let j=Ia(null,t,J,{includeOriginal:v===0,exclude:_});y=il(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
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}
370
+ `);return j}return!1}).slice(0,J),we=kl(b,s);if(!z.some(($)=>$.type==="fetchUrl")&&we.length>0){let $=we[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-${D}:action-${$+1}/${z.length}
372
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
- `)}y=F.length>=I?F:null}process.stderr.write(`PROGRESS:research:final-report
373
+ `);let F=await tl(j,{locale:a,short:l,usedQueries:_,usedUrls:s,maxChars:8000});if(S.push(F),fe++,j.type==="search")ge++;if(j.type==="fetchUrl")oe++;if(!F.ok)pe.push({round:D,type:j.type,target:j.query||j.url,error:F.error}),process.stderr.write(`[greedysearch] Action failed: ${F.error}
374
+ `)}let $e=S.filter(($)=>$.action.type==="search"),ce=S.filter(($)=>$.action.type==="fetchUrl");vt(c,{roundNumber:D,actions:S}),b=cl([b,$e.flatMap(($)=>$.sources||[]),ce.flatMap(($)=>$.sources||[])]);for(let $ of ce)if($.fetchResult)w.push($.fetchResult);w=Tr(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-${D}:fetching
375
+ `);let $=await Ka(b,Math.min(Ct,b.length),8000,Math.min(3,Ct||1));w=Tr([...w,...$]),b=se(b,w)}w=Nr(w,b),process.stderr.write(`PROGRESS:research:round-${D}:evidence
376
+ `);let u=await ol({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)vt(c,{roundNumber:D,learningPayload:{answeredQuestions:$.answers||[],newQuestions:$.newQuestions||[]}});let dr=S.map(($)=>({query:$.action.query||$.action.url||"",researchGoal:$.action.researchGoal||""}));process.stderr.write(`PROGRESS:research:round-${D}:learning
378
+ `);let d={learnings:[],followUpQueries:[],gaps:[]},me="";try{let $=await x(pl(t,dr,$e.map((j)=>({query:j.action.query,researchGoal:j.action.researchGoal,error:j.error||"",engines:ua(j.result)})),w,c,k),{timeoutMs:120000});d={...d,...lt($,d)}}catch($){me=$.message,process.stderr.write(`[greedysearch] Learning extraction failed: ${$.message}
379
+ `)}let be=Array.isArray(d.learnings)?d.learnings.map(($)=>String($)).filter(Boolean).slice(0,8):[],Nt=Array.isArray(d.gaps)?d.gaps.map(($)=>String($)).filter(Boolean).slice(0,6):[];h=B([...h,...be]),m=B([...m,...Nt]),vt(c,{roundNumber:D,actions:[],learningPayload:d,gaps:Nt}),p.push({round:D,actions:S.map(($)=>({type:$.action.type,query:$.action.query||"",url:$.action.url||"",researchGoal:$.action.researchGoal||"",error:$.error||"",sourceCount:$.sources?.length||0})),learnings:be,gaps:Nt,evidence:u.evidence,evidenceError:u.error,learningError:me}),process.stderr.write(`PROGRESS:research:round-${D}:evaluating
380
+ `);let L=await Sa(t,p,h,m,N);N.push(L.score),m=B([...m,...L.knowledgeGaps||[]]),vt(c,{roundNumber:D,gaps:L.knowledgeGaps||[]});let ye=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 ${D}: ${L.score.toFixed(1)} (shouldContinue: ${L.shouldContinue}, floor: ${ye.floorMet})
381
+ `),L.score>=i&&ye.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)),H=(d.followUpQueries||[]).map(($)=>({type:"search",query:A(String($)),researchGoal:"Follow-up from learning extraction"})).filter(($)=>$.query&&$.query.toLowerCase()!==t.toLowerCase()).slice(0,I);if(H.length<I&&L.nextActions.length>0){let $=L.nextActions.map((F)=>Pr(F)).filter(Boolean);H=[...H,...$].slice(0,I)}if(H.length<I&&m.length>0){let $=za(m,t,_,I-H.length,v+1),j=$.map((F)=>({type:"search",query:F.query,researchGoal:F.researchGoal}));if(H=[...H,...j].slice(0,I),$.length>0)process.stderr.write(`[greedysearch] Generated ${$.length} gap-driven fallback actions.
383
+ `)}y=H.length>=I?H:null}process.stderr.write(`PROGRESS:research:final-report
384
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}
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(hl(t,p,b,c,k),{timeoutMs:180000}),D=lt(v,{}),J=Array.isArray(D?.claims)&&D.claims.length>0;O={...O,...D,rawAnswer:v.answer||"",geminiSources:v.sources||[],synthesized:J}}catch(v){process.stderr.write(`[greedysearch] Final report failed: ${v.message}
386
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}
387
+ `);try{let v=wl(t,b,c,k),D=await x(v,{timeoutMs:180000}),J=lt(D,{});O={...O,...J,rawAnswer:D.answer||O.answer||"",geminiSources:D.sources||O.geminiSources||[],synthesized:!0,synthesisMode:"evidence_fallback"}}catch(v){process.stderr.write(`[greedysearch] Evidence-based synthesis failed: ${v.message}
388
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};
389
+ `);let it=bl(O.answer||"",b);sl(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 he={startedAt:Wr,finishedAt:Ar,durationMs:Rr,engines:Jt,synthesizer:"gemini",rounds:p.length,actionsRun:fe,searches:ge,fetches:oe,sourcesFetched:w.filter((v)=>v?.contentChars>100).length,engineFailures:pe,terminationReason:R,floorMet:Q.floorMet},tt=null,ft;if(f){process.stderr.write(`PROGRESS:research:bundle
390
+ `);try{tt=await jl({query:t,rounds:p,sources:b,fetchedSources:w,evidenceItems:k,synthesis:O,citationAudit:it,floor:Q,manifest:he,allGaps:m,questions:c,outDir:o}),ft=tt.sourceFiles,delete tt.sourceFiles}catch(v){tt={error:v.message||String(v)},ft=await le(w)}}else ft=await le(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:ie(c),qualityHistory:N,terminationReason:R,qualityThreshold:i,floor:Q,bundle:tt,manifest:he},_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 Et(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,vt as updateQuestionLedger,Er as tokenSet,Cf as runResearchMode,il as queriesToActions,ll as parseActionPlan,Ia as normalizeResearchQueries,Et as jaccardSimilarity,Or as isDuplicateQuery,yl as createQuestionLedger,Cr as computeResearchFloor,Ua as clampResearchOptions,za as buildFallbackQueriesFromGaps,bl as auditCitations};