@duckmind/dm-darwin-x64 0.52.6 → 0.53.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dm +0 -0
- package/extensions/.dm-extensions.json +14 -10
- package/extensions/dm-cua/bin/browser-cua.mjs +6 -6
- package/extensions/dm-cua/index.js +5 -5
- package/extensions/dm-cua/src/browser-cua-lib.mjs +2 -2
- package/extensions/greedysearch-dm/bin/cdp-greedy.mjs +8 -1
- package/extensions/greedysearch-dm/bin/cdp-headless.mjs +1 -1
- package/extensions/greedysearch-dm/bin/cdp-visible.mjs +1 -1
- package/extensions/greedysearch-dm/bin/cdp.mjs +29 -20
- package/extensions/greedysearch-dm/bin/gschrome.mjs +1 -8
- package/extensions/greedysearch-dm/bin/kill-visible.mjs +1 -1
- package/extensions/greedysearch-dm/bin/launch-visible.mjs +1 -2
- package/extensions/greedysearch-dm/bin/launch.mjs +9 -11
- package/extensions/greedysearch-dm/bin/mcp.mjs +375 -0
- package/extensions/greedysearch-dm/bin/search.mjs +309 -157
- package/extensions/greedysearch-dm/bin/visible.mjs +1 -1
- package/extensions/greedysearch-dm/extractors/bing-copilot.mjs +205 -68
- package/extensions/greedysearch-dm/extractors/chatgpt.mjs +229 -140
- package/extensions/greedysearch-dm/extractors/common.mjs +160 -50
- package/extensions/greedysearch-dm/extractors/consensus.mjs +226 -93
- package/extensions/greedysearch-dm/extractors/consent.mjs +44 -17
- package/extensions/greedysearch-dm/extractors/gemini.mjs +277 -138
- package/extensions/greedysearch-dm/extractors/google-ai.mjs +172 -64
- package/extensions/greedysearch-dm/extractors/logically.mjs +183 -69
- package/extensions/greedysearch-dm/extractors/perplexity.mjs +331 -65
- package/extensions/greedysearch-dm/extractors/semantic-scholar.mjs +118 -41
- package/extensions/greedysearch-dm/index.js +23 -20
- package/extensions/greedysearch-dm/package.json +8 -3
- package/extensions/greedysearch-dm/skills/greedy-search/skill.md +8 -8
- package/extensions/greedysearch-dm/src/fetcher.mjs +2 -2
- package/extensions/greedysearch-dm/src/formatters/results.js +8 -6
- package/extensions/greedysearch-dm/src/github.mjs +7 -7
- package/extensions/greedysearch-dm/src/reddit.mjs +1 -1
- package/extensions/greedysearch-dm/src/search/browser-lifecycle.mjs +10 -8
- package/extensions/greedysearch-dm/src/search/challenge-detect.mjs +48 -0
- package/extensions/greedysearch-dm/src/search/chrome.mjs +130 -54
- package/extensions/greedysearch-dm/src/search/constants.mjs +6 -6
- package/extensions/greedysearch-dm/src/search/engines.mjs +9 -9
- package/extensions/greedysearch-dm/src/search/fetch-source.mjs +160 -83
- package/extensions/greedysearch-dm/src/search/minimize.mjs +1 -0
- package/extensions/greedysearch-dm/src/search/paths.mjs +1 -1
- package/extensions/greedysearch-dm/src/search/pdf.mjs +1 -1
- package/extensions/greedysearch-dm/src/search/port-pid.mjs +1 -1
- package/extensions/greedysearch-dm/src/search/progress.mjs +2 -0
- package/extensions/greedysearch-dm/src/search/recovery.mjs +1 -1
- package/extensions/greedysearch-dm/src/search/research.mjs +262 -167
- package/extensions/greedysearch-dm/src/search/scale-aware.mjs +11 -0
- package/extensions/greedysearch-dm/src/search/simple-research.mjs +827 -0
- package/extensions/greedysearch-dm/src/search/synthesis-runner.mjs +9 -9
- package/extensions/greedysearch-dm/src/search/synthesis.mjs +10 -10
- package/extensions/greedysearch-dm/src/tools/greedy-search-handler.js +23 -20
- package/extensions/greedysearch-dm/src/tools/shared.js +10 -9
- package/extensions/greedysearch-dm/src/utils/node-runtime.mjs +1 -1
- package/package.json +1 -1
- package/extensions/greedysearch-dm/src/search/cdp-endpoint.mjs +0 -3
- package/extensions/greedysearch-dm/src/search/launcher-paths.mjs +0 -3
- package/extensions/greedysearch-dm/src/search/partial-output.mjs +0 -1
- package/extensions/greedysearch-dm/src/utils/browser-executable.mjs +0 -1
|
@@ -1,46 +1,54 @@
|
|
|
1
|
-
import{createRequire as
|
|
1
|
+
import{createRequire as rp}from"node:module";var pp=Object.defineProperty;var gp=(t)=>t;function ap(t,n){this[t]=gp.bind(null,n)}var dt=(t,n)=>{for(var f in n)pp(t,f,{get:n[f],enumerable:!0,configurable:!0,set:ap.bind(n,f)})};var qt=(t,n)=>()=>(t&&(n=t(t=0)),n);var Mt=rp(import.meta.url);var af={};dt(af,{writeSourcesToFiles:()=>Vt});import{mkdirSync as Wp,writeFileSync as Vp}from"node:fs";import{join as gf}from"node:path";function Vt(t,n=mp){return Wp(n,{recursive:!0}),t.map((f)=>{if(!f.content||f.content.length<10)return f;let p=String(f.id||"unknown").replace(/[^a-zA-Z0-9_-]/g,""),g=(f.canonicalUrl||f.url||"").replace(/^https?:\/\//,"").replace(/[^a-zA-Z0-9]/g,"-").slice(0,40),r=`${p}-${g}.md`,a=gf(n,r),l=`---
|
|
2
|
+
url: ${f.finalUrl||f.url}
|
|
3
|
+
title: ${f.title||""}
|
|
4
|
+
source: ${f.source||"unknown"}
|
|
5
|
+
status: ${f.status||""}
|
|
6
|
+
chars: ${f.contentChars||f.content.length}
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
`;Vp(a,l+f.content,"utf8");let{content:w,...$}=f;return{...$,contentPath:a,contentChars:f.contentChars||w.length}})}var mp;var an=qt(()=>{mp=gf(process.cwd(),".dm","greedysearch-sources")});var Nn={};dt(Nn,{fetchTopSource:()=>qg,fetchSourceContent:()=>Wf,fetchMultipleSources:()=>On});import{createRequire as Jp}from"node:module";import{createRequire as Zp}from"node:module";import{spawn as lg}from"node:child_process";import{basename as wg}from"node:path";import{dirname as $g,join as _g}from"node:path";import{fileURLToPath as hg}from"node:url";import{existsSync as Jt,mkdirSync as eg,readFileSync as Af,writeFileSync as Eg}from"node:fs";import{homedir as Dg}from"node:os";import{join as Tf}from"node:path";import{tmpdir as bg}from"node:os";import{existsSync as Zt,mkdirSync as Cg,readFileSync as kf,writeFileSync as vg}from"node:fs";import{homedir as jg}from"node:os";import{join as Hf}from"node:path";import{tmpdir as Pg}from"node:os";import{existsSync as Kt,mkdirSync as Vg,readFileSync as If,writeFileSync as mg}from"node:fs";import{homedir as Jg}from"node:os";import{join as Mf}from"node:path";import{tmpdir as Zg}from"node:os";async function Kp(){if(mt)return mt;let[{Readability:t},{JSDOM:n},{default:f}]=await Promise.all([import("@mozilla/readability"),import("jsdom"),import("turndown")]),p=new f({headingStyle:"atx",bulletListMarker:"-",codeBlockStyle:"fenced"});return p.addRule("removeDataUrls",{filter:(g)=>g.tagName==="IMG"&&g.getAttribute("src")?.startsWith("data:"),replacement:()=>""}),mt={Readability:t,JSDOM:n,turndown:p},mt}function Up(t){if(!t||t.includes(":"))return null;let n=t.split(".");if(n.length<1||n.length>4)return null;if(!n.every((g)=>Bp.test(g)))return null;let f=n.map((g)=>{if(/^0x/i.test(g))return parseInt(g,16);if(/^0[0-7]+$/.test(g))return parseInt(g,8);return parseInt(g,10)});if(f.some((g)=>!Number.isFinite(g)||g<0))return null;let p;if(f.length===1){if(f[0]>4294967295)return null;p=[f[0]>>>24&255,f[0]>>>16&255,f[0]>>>8&255,f[0]&255]}else if(f.length===2){if(f[0]>255||f[1]>16777215)return null;p=[f[0],f[1]>>>16&255,f[1]>>>8&255,f[1]&255]}else if(f.length===3){if(f[0]>255||f[1]>255||f[2]>65535)return null;p=[f[0],f[1],f[2]>>>8&255,f[2]&255]}else{if(f.some((g)=>g>255))return null;p=f}return p.join(".")}function Qp(t){let n=t.match(Xp);if(n)return`${n[1]}.${n[2]}.${n[3]}.${n[4]}`;let f=t.match(op);if(f){let p=parseInt(f[1],16),g=parseInt(f[2],16);if(p>65535||g>65535)return null;return[p>>>8&255,p&255,g>>>8&255,g&255].join(".")}return null}function rf(t){return jf.some((n)=>n.test(t))}function xp(t={}){return{...vf,...t}}function $n(t){try{if(typeof t!=="string"||!t.trim())return{blocked:!0,reason:"URL must be a non-empty string"};let n=new URL(t);if(n.protocol!=="http:"&&n.protocol!=="https:")return{blocked:!0,reason:`Protocol not allowed: ${n.protocol}`};let f=n.hostname.toLowerCase();for(let g of jf)if(g.test(f))return{blocked:!0,reason:`Private/internal address: ${f}`};if(f.startsWith("[")&&f.endsWith("]")){let g=f.slice(1,-1),r=Qp(g);if(r&&rf(r))return{blocked:!0,reason:`Private/internal address: ${f} (maps to ${r})`}}let p=Up(f);if(p&&rf(p))return{blocked:!0,reason:`Private/internal address: ${f} (normalizes to ${p})`};return{blocked:!1}}catch(n){return{blocked:!0,reason:`Invalid URL: ${n.message}`}}}function zp(t){try{let n=new URL(t);if(!(n.hostname==="github.com"||n.hostname.endsWith(".github.com")))return t;let f=n.pathname.split("/").filter(Boolean);if(f.length<5)return t;let[p,g,r,a,...l]=f;if(r!=="blob")return t;let w=l.join("/");return`https://raw.githubusercontent.com/${p}/${g}/${a}/${w}`}catch{return t}}async function Sp(t,n={}){let f=$n(t);if(f.blocked)return{ok:!1,url:t,finalUrl:t,status:403,error:`Blocked: ${f.reason}`,needsBrowser:!1};let p=t;if(t=zp(t),t!==p)console.error(`[fetcher] Rewrote GitHub URL: ${p.slice(0,60)}... → raw.githubusercontent.com`);let{timeoutMs:g=15000,userAgent:r,signal:a}=n,l=new AbortController,w=setTimeout(()=>l.abort(),g);if(a)a.addEventListener("abort",()=>l.abort(),{once:!0});try{let $=await fetch(t,{method:"GET",headers:{...vf,"user-agent":r||Cf},redirect:"follow",signal:l.signal});clearTimeout(w);let _=$.headers.get("content-type")||"",e=$.url,E=$.headers.get("last-modified")||"",h=$n(e);if(h.blocked)return{ok:!1,url:t,finalUrl:e,status:$.status,error:`Blocked: ${h.reason}`,needsBrowser:!1};let b=!1;try{b=new URL(e).hostname.toLowerCase()==="raw.githubusercontent.com"}catch{}if(_.includes("text/plain")&&b){let y=await $.text();return{ok:!0,url:p,finalUrl:e,status:$.status,title:e.split("/").pop()||"GitHub File",byline:"",siteName:"GitHub",lang:"",publishedTime:E,lastModified:E,markdown:y,contentLength:y.length,excerpt:y.slice(0,300).replaceAll(/\n/g," "),needsBrowser:!1}}if(!_.includes("text/html")&&!_.includes("application/xhtml"))return{ok:!1,url:t,finalUrl:e,status:$.status,error:`Unsupported content type: ${_}`,needsBrowser:!1};let D=await $.text(),v=Pf($.status,D,e,t);if(v.blocked)return{ok:!1,url:t,finalUrl:e,status:$.status,error:`Blocked: ${v.reason}`,needsBrowser:!0};let P=await Rf(D,e),R=yf(P);if(!R.ok)return{ok:!1,url:t,finalUrl:e,status:$.status,error:`Low quality content: ${R.reason}`,needsBrowser:!0};return{ok:!0,url:t,finalUrl:e,status:$.status,title:P.title,byline:P.byline,siteName:P.siteName,lang:P.lang,publishedTime:P.publishedTime||E,lastModified:E,markdown:P.markdown,excerpt:P.excerpt,contentLength:P.markdown.length,needsBrowser:!1}}catch($){clearTimeout(w);let _=up($);return{ok:!1,url:t,finalUrl:t,status:0,error:$.message,needsBrowser:_}}}function Pf(t,n,f,p){let g=n.match(/<title[^>]*>([^<]*)<\/title>/i)?.[1]?.toLowerCase()||"",r=n.slice(0,30000).toLowerCase(),a=`${g} ${r}`;if(t===403||t===429||t===503)return{blocked:!0,reason:`HTTP ${t}`};let l=[{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 $ of l)if($.pattern.test(a))return{blocked:!0,reason:$.reason};let w=sp(p,f,n);if(w)return{blocked:!0,reason:w};return{blocked:!1}}function sp(t,n,f){try{let p=new URL(t),g=new URL(n);if(p.hostname.toLowerCase()===g.hostname.toLowerCase())return;let r=g.hostname.toLowerCase();if(cp.some((l)=>r===l||r.endsWith(`.${l}`)))return`redirected to login (${g.hostname})`;if(dp.some((l)=>r.startsWith(l)))return`redirected to login (${g.hostname})`;let a=f.slice(0,20000).toLowerCase();if(qp.some((l)=>a.includes(l)))return`redirected to login page (${g.hostname})`}catch{}return}function up(t){let n=t.message.toLowerCase();return n.includes("fetch failed")||n.includes("unable to verify")||n.includes("certificate")||n.includes("timeout")}function lf(t){let n=['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 f of n){let p=t.querySelector(f),g=p?.getAttribute("content")||p?.getAttribute("datetime")||"";if(g)return g}return""}async function Rf(t,n){let{Readability:f,JSDOM:p,turndown:g}=await Kp(),r=new p(t,{url:n});try{let a=r.window.document,l=new f(a).parse();if(l&&l.content){let $=g.turndown(l.content).replaceAll(/\n{3,}/g,`
|
|
2
10
|
|
|
3
|
-
`).trim(),
|
|
11
|
+
`).trim(),_=l.publishedTime||lf(a)||"";return{title:l.title||a.title||n,byline:l.byline||"",siteName:l.siteName||"",lang:l.lang||"",publishedTime:_,markdown:$,excerpt:$.slice(0,300).replaceAll(/\n/g," ")}}let w=a.body;if(w){let $=w.cloneNode(!0);$.querySelectorAll("script, style, nav, footer, header, aside").forEach((e)=>e.remove());let _=($.textContent||"").replaceAll(/\s+/g," ").trim();return{title:a.title||n,byline:"",siteName:"",lang:"",publishedTime:lf(a),markdown:_,excerpt:_.slice(0,300)}}return{title:n,byline:"",siteName:"",lang:"",publishedTime:"",markdown:"",excerpt:""}}finally{r.window.close()}}function yf(t){let n=t.markdown.trim().toLowerCase(),f=(t.title||"").toLowerCase();if(t.markdown.trim().length<100)return{ok:!1,reason:"content too short (< 100 chars)"};let p=n.toLowerCase(),g=[{check:()=>p.includes("loading")&&p.includes("please wait"),desc:"loading page"},{check:()=>p.includes("please ensure javascript is enabled"),desc:"requires javascript"},{check:()=>p.includes("enable javascript to view"),desc:"requires javascript"},{check:()=>p.includes("just a moment"),desc:"cloudflare challenge detected in content"},{check:()=>p.includes("verify you are human"),desc:"human verification"},{check:()=>p.includes("captcha required"),desc:"captcha in extracted content"},{check:()=>p.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(n),desc:"login form only"}];for(let{check:r,desc:a}of g)if(r())return{ok:!1,reason:a};if(f.includes("just a moment")||f.includes("checking your browser"))return{ok:!1,reason:"cloudflare challenge page detected in title"};return{ok:!0}}function _n(t){try{let n=new URL(t);if(!(n.hostname==="github.com"||n.hostname.endsWith(".github.com")))return null;let f=n.pathname.split("/").filter(Boolean);if(f.length<2)return null;let[p,g]=f;if(f.length===2)return{owner:p,repo:g,type:"root"};if(f.length>=4&&(f[2]==="blob"||f[2]==="tree")){let r=f[2],a=f[3],l=f.slice(4).join("/");return{owner:p,repo:g,type:r,ref:a,path:l}}return null}catch{return null}}async function B(t,n=1e4){let f=new AbortController,p=setTimeout(()=>f.abort(),n);try{let g=await fetch(`https://api.github.com${t}`,{headers:Of,signal:f.signal});if(clearTimeout(p),!g.ok)throw Error(`GitHub API ${g.status}: ${t}`);return await g.json()}catch(g){throw clearTimeout(p),g}}async function tg(t,n){try{let f=await B(`/repos/${t}/${n}/readme`);if(f.content&&f.encoding==="base64")return Buffer.from(f.content,"base64").toString("utf8");return""}catch{return""}}async function wf(t,n,f="HEAD",p="",g){try{let r;if(f==="HEAD")if(g)r=await B(`/repos/${t}/${n}/git/ref/heads/${g}`).catch(()=>null);else r=await Promise.any([B(`/repos/${t}/${n}/git/ref/heads/main`),B(`/repos/${t}/${n}/git/ref/heads/master`)]).catch(()=>null);else r=await B(`/repos/${t}/${n}/git/ref/heads/${f}`).catch(()=>B(`/repos/${t}/${n}/git/ref/heads/master`).catch(()=>null));if(!r?.object?.sha)return[];let a=(await B(`/repos/${t}/${n}/git/commits/${r.object.sha}`)).tree.sha,l=(await B(`/repos/${t}/${n}/git/trees/${a}`)).tree||[];if(p)l=l.filter((w)=>w.path.startsWith(p));return l.slice(0,50).map((w)=>({path:w.path,type:w.type==="tree"?"dir":"file",size:w.size}))}catch{return[]}}async function ng(t,n,f,p,g=1e4,r){let a=async(w)=>{let $=new AbortController,_=setTimeout(()=>$.abort(),g);try{let e=await fetch(w,{headers:{"user-agent":Of["user-agent"]},signal:$.signal});if(clearTimeout(_),e.ok)return await e.text();throw Error("not ok")}catch{throw clearTimeout(_),Error("failed")}};if(!f||f==="HEAD"){if(r)try{return await a(`https://raw.githubusercontent.com/${t}/${n}/${r}/${p}`)}catch{return null}try{return await Promise.any([a(`https://raw.githubusercontent.com/${t}/${n}/main/${p}`),a(`https://raw.githubusercontent.com/${t}/${n}/master/${p}`)])}catch{return null}}let l=[`https://raw.githubusercontent.com/${t}/${n}/${f}/${p}`,`https://raw.githubusercontent.com/${t}/${n}/master/${p}`];for(let w of l)try{return await a(w)}catch{}return null}async function fg(t){let n=_n(t);if(!n)return{ok:!1,error:"Not a valid GitHub URL"};let{owner:f,repo:p,type:g,ref:r,path:a}=n;try{if(g==="root"||g==="tree"&&!a){let l=await B(`/repos/${f}/${p}`),[w,$]=await Promise.allSettled([tg(f,p),wf(f,p,r||"HEAD","",l.default_branch)]),_=w.status==="fulfilled"?w.value:"",e=$.status==="fulfilled"?$.value:[],E=l?.description?`
|
|
4
12
|
|
|
5
|
-
> ${
|
|
13
|
+
> ${l.description}`:"",h=l?.stargazers_count==null?"":` ⭐ ${l.stargazers_count}`,b=l?.language?` · ${l.language}`:"",D=`# ${f}/${p}${h}${b}${E}
|
|
6
14
|
|
|
7
|
-
`;if(
|
|
15
|
+
`;if(_)D+=_.slice(0,6000);else D+=`[No README found]
|
|
8
16
|
|
|
9
17
|
Files:
|
|
10
|
-
${
|
|
11
|
-
`)}`;return{ok:!0,title:`${
|
|
12
|
-
`);return{ok:!0,title:`${
|
|
18
|
+
${e.map((v)=>` ${v.type==="dir"?"\uD83D\uDCC1":"\uD83D\uDCC4"} ${v.path}`).join(`
|
|
19
|
+
`)}`;return{ok:!0,title:`${f}/${p}`,content:D,tree:e.slice(0,30)}}if(g==="blob"&&a){let l;if(!r||r==="HEAD")try{l=(await B(`/repos/${f}/${p}`)).default_branch}catch{l=void 0}let w=await ng(f,p,r,a,1e4,l);if(w===null)return{ok:!1,error:`File not found: ${a}`};return{ok:!0,title:`${f}/${p}: ${a}`,content:w}}if(g==="tree"&&a){let l;if(!r||r==="HEAD")try{l=(await B(`/repos/${f}/${p}`)).default_branch}catch{l=void 0}let w=await wf(f,p,r||"HEAD",a,l),$=w.map((_)=>` ${_.type==="dir"?"\uD83D\uDCC1":"\uD83D\uDCC4"} ${_.path}`).join(`
|
|
20
|
+
`);return{ok:!0,title:`${f}/${p}/${a}`,content:`[Directory: ${a}]
|
|
13
21
|
|
|
14
22
|
Files:
|
|
15
|
-
${
|
|
23
|
+
${$}`,tree:w}}return{ok:!1,error:"Unsupported GitHub URL type"}}catch(l){return{ok:!1,error:l.message}}}function gg(t){try{let n=new URL(t),f=n.hostname.toLowerCase();if(!(f==="reddit.com"||f.endsWith(".reddit.com")))return null;let p=n.pathname;if(p.match(/^\/(u|user)\/[^/]+\/?$/i))return{type:"user",cleanUrl:$f(t)};if(p.match(/^\/r\/[^/]+\/comments\/[^/]+/i))return{type:"post",cleanUrl:$f(t)};return null}catch{return null}}function $f(t){try{let n=new URL(t);return`${n.protocol}//${n.hostname}${n.pathname}`}catch{return t}}async function ag(t,n=8000){let f=Date.now();try{let p=t.replace(/\/+$/,"")+".json",g=new AbortController,r=setTimeout(()=>g.abort(),15000),a=await fetch(p,{headers:pg,signal:g.signal});if(clearTimeout(r),!a.ok)throw Error(`Reddit API ${a.status}`);let l=await a.json();if(!Array.isArray(l)||l.length<1)throw Error("Invalid Reddit API response structure");let w=l[0],$=l[1],_=w?.data?.children?.[0]?.data;if(!_)throw Error("No post data in Reddit response");let e=rg(_,$,n);return{ok:!0,url:t,finalUrl:t,status:200,contentType:"text/markdown",lastModified:"",title:_.title||"Reddit Post",byline:`u/${_.author}`,siteName:`r/${_.subreddit}`,lang:"en",publishedTime:new Date(_.created_utc*1000).toISOString(),excerpt:_.selftext?.slice(0,300).replace(/\n/g," ")||"",markdown:e,contentLength:e.length,needsBrowser:!1,duration:Date.now()-f}}catch(p){return{ok:!1,url:t,finalUrl:t,status:0,error:`Reddit fetch failed: ${p.message}`,needsBrowser:!1,duration:Date.now()-f}}}function rg(t,n,f){let p="";if(p+=`# ${t.title}
|
|
16
24
|
|
|
17
|
-
`,
|
|
25
|
+
`,p+=`**Subreddit:** r/${t.subreddit} | **Author:** u/${t.author} | **Score:** ${t.score}
|
|
18
26
|
|
|
19
|
-
`,t.selftext)
|
|
27
|
+
`,t.selftext)p+=t.selftext,p+=`
|
|
20
28
|
|
|
21
|
-
`;else if(t.url)try{let
|
|
29
|
+
`;else if(t.url)try{let g=new URL(t.url).hostname.toLowerCase();if(g!=="reddit.com"&&!g.endsWith(".reddit.com"))p+=`**Link:** ${t.url}
|
|
22
30
|
|
|
23
|
-
`}catch{
|
|
31
|
+
`}catch{p+=`**Link:** ${t.url}
|
|
24
32
|
|
|
25
|
-
`}if(
|
|
33
|
+
`}if(n?.data?.children?.length>0){p+=`---
|
|
26
34
|
|
|
27
35
|
## Comments
|
|
28
36
|
|
|
29
|
-
`;let
|
|
30
|
-
`}if(
|
|
37
|
+
`;let g=n.data.children.filter((r)=>r.kind==="t1").slice(0,10);for(let r of g)p+=Nf(r.data,0),p+=`
|
|
38
|
+
`}if(p.length>f)p=p.slice(0,f).trim()+`
|
|
31
39
|
|
|
32
|
-
... (truncated)`;return
|
|
33
|
-
`,
|
|
40
|
+
... (truncated)`;return p}function Nf(t,n){if(!t||t.body==="[deleted]"||t.body==="[removed]")return"";let f="> ".repeat(n),p="";if(p+=`${f}**u/${t.author}** (${t.score} pts)
|
|
41
|
+
`,p+=`${f}${t.body.replaceAll(`
|
|
34
42
|
`,`
|
|
35
|
-
`+
|
|
36
|
-
`,
|
|
37
|
-
`+
|
|
43
|
+
`+f)}
|
|
44
|
+
`,n<3&&t.replies?.data?.children){let g=t.replies.data.children.filter((r)=>r.kind==="t1");for(let r of g.slice(0,5))p+=`
|
|
45
|
+
`+Nf(r.data,n+1)}return p}function Rt(t,n=8000){if(!t||t.length<=n)return t;let f=`
|
|
38
46
|
|
|
39
47
|
[...content trimmed...]
|
|
40
48
|
|
|
41
|
-
`,n
|
|
42
|
-
`)
|
|
43
|
-
`)
|
|
49
|
+
`,p=n-f.length,g=Math.floor(p*0.75),r=p-g,a=g;while(a>g-100&&t[a]!==`
|
|
50
|
+
`)a--;if(a<=g-100)a=g;let l=t.length-r;while(l<t.length-r+100&&t[l]!==`
|
|
51
|
+
`)l++;if(l>=t.length-r+100)l=t.length-r;let w=t.slice(0,a).trimEnd(),$=t.slice(l).trimStart();return`${w}${f}${$}`}function Rg(t=process.env,n=process.execPath){let f=t.GREEDY_SEARCH_NODE||t.NODE_BINARY||t.NODE;if(f?.trim())return f.trim();let p=wg(n||"").toLowerCase();if(p==="node"||p==="node.exe")return n;return"node"}function ig(t){if(!Array.isArray(t)||t.length===0)throw Error("cdp: args must be a non-empty array");if(t[0]==="test")return t.map((n,f)=>_f(n,f));if(!Ng.has(t[0]))throw Error(`cdp: unknown subcommand '${t[0]}'`);return t.map((n,f)=>_f(n,f))}function _f(t,n){if(typeof t!=="string")throw Error(`cdp: argv[${n}] must be a string (got ${typeof t})`);if(t.includes("\x00"))throw Error(`cdp: argv[${n}] contains a null byte`);return t}function Lf(t,n=30000){return Ag(t,null,n)}function Ag(t,n=null,f=30000){let p=ig(t);return new Promise((g,r)=>{let a=lg(Rg(),[Og,...p],{stdio:[n==null?"ignore":"pipe","pipe","pipe"]});if(n!=null)a.stdin.write(n),a.stdin.end();let l="",w="";a.stdout.on("data",(_)=>l+=_),a.stderr.on("data",(_)=>w+=_);let $=setTimeout(()=>{a.kill(),r(Error(`cdp timeout: ${t[0]}`))},f);a.on("close",(_)=>{if(clearTimeout($),_===0)g(l.trim());else r(Error(w.trim()||`cdp exit ${_}`))})})}async function hf(t){await Lf(["evalraw",t,"Page.addScriptToEvaluateOnNewDocument",JSON.stringify({source:`
|
|
44
52
|
(function() {
|
|
45
53
|
// ── Runtime.enable / CDP detection masking ──────────────
|
|
46
54
|
try { delete window.__REBROWSER_RUNTIME_ENABLE; } catch(_) {}
|
|
@@ -51,40 +59,62 @@ ${g}`,tree:f}}return{ok:!1,error:"Unsupported GitHub URL type"}}catch(f){return{
|
|
|
51
59
|
try { delete window._phantom; } catch(_) {}
|
|
52
60
|
try { delete window.Buffer; } catch(_) {}
|
|
53
61
|
|
|
54
|
-
// Real Chrome without automation
|
|
55
|
-
// A literal false
|
|
56
|
-
//
|
|
57
|
-
|
|
62
|
+
// Real Chrome without automation should not expose navigator.webdriver at all.
|
|
63
|
+
// A literal false or an own-property getter returning undefined is itself a
|
|
64
|
+
// common stealth tell; remove both instance and prototype properties when the
|
|
65
|
+
// descriptor is configurable (as it is with --disable-blink-features).
|
|
66
|
+
try { delete navigator.webdriver; } catch(_) {}
|
|
67
|
+
try { delete Navigator.prototype.webdriver; } catch(_) {}
|
|
58
68
|
Object.defineProperty(navigator, 'vendor', { get: () => 'Google Inc.', configurable: true });
|
|
59
69
|
Object.defineProperty(navigator, 'platform', { get: () => 'Win32', configurable: true });
|
|
60
70
|
Object.defineProperty(navigator, 'maxTouchPoints', { get: () => 0, configurable: true });
|
|
61
71
|
Object.defineProperty(navigator, 'pdfViewerEnabled', { get: () => true, configurable: true });
|
|
72
|
+
Object.defineProperty(navigator, 'productSub', { get: () => '20030107', configurable: true });
|
|
73
|
+
Object.defineProperty(navigator, 'product', { get: () => 'Gecko', configurable: true });
|
|
74
|
+
var __greedyMimeTypes = null;
|
|
75
|
+
function __makeMimeTypes() {
|
|
76
|
+
var pdf = { type: 'application/pdf', suffixes: 'pdf', description: 'Portable Document Format', enabledPlugin: null };
|
|
77
|
+
var textPdf = { type: 'text/pdf', suffixes: 'pdf', description: 'Portable Document Format', enabledPlugin: null };
|
|
78
|
+
try { Object.setPrototypeOf(pdf, MimeType.prototype); } catch(_) {}
|
|
79
|
+
try { Object.setPrototypeOf(textPdf, MimeType.prototype); } catch(_) {}
|
|
80
|
+
var m = [pdf, textPdf];
|
|
81
|
+
try { Object.setPrototypeOf(m, MimeTypeArray.prototype); } catch(_) {}
|
|
82
|
+
m.item = function item(i) { return this[i] || null; };
|
|
83
|
+
m.namedItem = function namedItem(name) { return Array.prototype.find.call(this, function(x) { return x && x.type === name; }) || null; };
|
|
84
|
+
return m;
|
|
85
|
+
}
|
|
62
86
|
Object.defineProperty(navigator, 'plugins', {
|
|
63
87
|
get: () => {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
88
|
+
__greedyMimeTypes = __greedyMimeTypes || __makeMimeTypes();
|
|
89
|
+
var plugin0 = { name: 'Chrome PDF Plugin', filename: 'internal-pdf-viewer', description: 'Portable Document Format' };
|
|
90
|
+
var plugin1 = { name: 'Chrome PDF Viewer', filename: 'mhjfbmdgcfjbbpaeojofohoefgiehjai', description: '' };
|
|
91
|
+
var plugin2 = { name: 'Native Client', filename: 'internal-nacl-plugin', description: '' };
|
|
92
|
+
try { Object.setPrototypeOf(plugin0, Plugin.prototype); } catch(_) {}
|
|
93
|
+
try { Object.setPrototypeOf(plugin1, Plugin.prototype); } catch(_) {}
|
|
94
|
+
try { Object.setPrototypeOf(plugin2, Plugin.prototype); } catch(_) {}
|
|
95
|
+
var p = [plugin0, plugin1, plugin2];
|
|
96
|
+
p.item = function item(i) { return this[i] || null; };
|
|
97
|
+
p.namedItem = function namedItem(name) { return Array.prototype.find.call(this, function(x) { return x && x.name === name; }) || null; };
|
|
98
|
+
p.refresh = function refresh() {};
|
|
99
|
+
try { Object.setPrototypeOf(p, PluginArray.prototype); } catch(_) {}
|
|
100
|
+
try {
|
|
101
|
+
__greedyMimeTypes[0].enabledPlugin = p[0];
|
|
102
|
+
__greedyMimeTypes[1].enabledPlugin = p[0];
|
|
103
|
+
} catch(_) {}
|
|
70
104
|
return p;
|
|
71
105
|
},
|
|
106
|
+
configurable: true,
|
|
72
107
|
});
|
|
73
108
|
Object.defineProperty(navigator, 'mimeTypes', {
|
|
74
109
|
get: () => {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
{ type: 'text/pdf', suffixes: 'pdf', description: 'Portable Document Format', enabledPlugin: null },
|
|
78
|
-
];
|
|
79
|
-
m.item = function(i) { return m[i] || null; };
|
|
80
|
-
m.namedItem = function(name) { return m.find(function(x) { return x.type === name; }) || null; };
|
|
81
|
-
return m;
|
|
110
|
+
__greedyMimeTypes = __greedyMimeTypes || __makeMimeTypes();
|
|
111
|
+
return __greedyMimeTypes;
|
|
82
112
|
},
|
|
83
113
|
configurable: true,
|
|
84
114
|
});
|
|
85
115
|
Object.defineProperty(navigator, 'languages', { get: () => ['en-US', 'en'], configurable: true });
|
|
86
116
|
try {
|
|
87
|
-
Object.defineProperty(navigator, 'connection', { get: () => ({ effectiveType: '4g', rtt: 50, downlink: 10, saveData: false }), configurable: true });
|
|
117
|
+
Object.defineProperty(navigator, 'connection', { get: () => ({ effectiveType: '4g', rtt: 50, downlink: 10, downlinkMax: Infinity, saveData: false }), configurable: true });
|
|
88
118
|
} catch(_) {}
|
|
89
119
|
if (!navigator.mediaDevices) {
|
|
90
120
|
Object.defineProperty(navigator, 'mediaDevices', {
|
|
@@ -100,6 +130,18 @@ ${g}`,tree:f}}return{ok:!1,error:"Unsupported GitHub URL type"}}catch(f){return{
|
|
|
100
130
|
configurable: true,
|
|
101
131
|
});
|
|
102
132
|
}
|
|
133
|
+
// ── Missing platform APIs (headless often lacks these) ─
|
|
134
|
+
try {
|
|
135
|
+
if (!navigator.share) {
|
|
136
|
+
navigator.share = function() { return Promise.reject(new Error('NotAllowedError')); };
|
|
137
|
+
}
|
|
138
|
+
} catch(_) {}
|
|
139
|
+
try {
|
|
140
|
+
if (!navigator.contentIndex) {
|
|
141
|
+
Object.defineProperty(navigator, 'contentIndex', { get: () => ({ add: function() {}, delete: function() {}, getAll: function() { return Promise.resolve([]); } }), configurable: true });
|
|
142
|
+
}
|
|
143
|
+
} catch(_) {}
|
|
144
|
+
|
|
103
145
|
if (!window.chrome) {
|
|
104
146
|
window.chrome = {
|
|
105
147
|
app: { isInstalled: false, InstallState: {}, RunningState: {} },
|
|
@@ -107,8 +149,8 @@ ${g}`,tree:f}}return{ok:!1,error:"Unsupported GitHub URL type"}}catch(f){return{
|
|
|
107
149
|
OnInstalledReason: {}, OnRestartRequiredReason: {}, PlatformArch: {}, PlatformNaclArch: {}, PlatformOs: {}, RequestUpdateCheckStatus: {},
|
|
108
150
|
connect: () => ({}), sendMessage: () => {}, onMessage: { addListener: () => {} }
|
|
109
151
|
},
|
|
110
|
-
loadTimes: ()
|
|
111
|
-
csi: ()
|
|
152
|
+
loadTimes: function() { return { requestTime: 0, startLoadTime: Date.now() - 5000, commitLoadTime: Date.now() - 3000, finishDocumentLoadTime: Date.now() - 2000, finishLoadTime: Date.now() - 1000, firstPaintTime: Date.now() - 800, navigationType: 'Other', wasFetchedViaSpdy: true, wasNpnNegotiated: true, npnNegotiatedProtocol: 'h2', wasAlternateProtocolAvailable: false, connectionInfo: 'http/2' }; },
|
|
153
|
+
csi: function() { var t = Date.now(); return { onloadT: t - 2000, startE: t - 5000, pageT: 'back', tran: 2 }; },
|
|
112
154
|
};
|
|
113
155
|
}
|
|
114
156
|
var __greedyNativeFns = [];
|
|
@@ -129,6 +171,19 @@ ${g}`,tree:f}}return{ok:!1,error:"Unsupported GitHub URL type"}}catch(f){return{
|
|
|
129
171
|
return getParam.call(this, p);
|
|
130
172
|
});
|
|
131
173
|
} catch(_) {}
|
|
174
|
+
// ── WebGL readPixels noise ──────────────────────────
|
|
175
|
+
// CreepJS and other fingerprinters draw content with WebGL and read back the
|
|
176
|
+
// rendered pixels. Adding subtle noise breaks rendering-based fingerprinting.
|
|
177
|
+
try {
|
|
178
|
+
var origReadPixels = WebGLRenderingContext.prototype.readPixels;
|
|
179
|
+
WebGLRenderingContext.prototype.readPixels = __markNative(function readPixels(x, y, width, height, format, type, pixels) {
|
|
180
|
+
var result = origReadPixels.call(this, x, y, width, height, format, type, pixels);
|
|
181
|
+
if (pixels && pixels.length > 0) {
|
|
182
|
+
pixels[0] ^= 1;
|
|
183
|
+
}
|
|
184
|
+
return result;
|
|
185
|
+
});
|
|
186
|
+
} catch(_) {}
|
|
132
187
|
Object.defineProperty(navigator, 'hardwareConcurrency', { get: () => 8, configurable: true });
|
|
133
188
|
Object.defineProperty(navigator, 'deviceMemory', { get: () => 8, configurable: true });
|
|
134
189
|
|
|
@@ -136,7 +191,7 @@ ${g}`,tree:f}}return{ok:!1,error:"Unsupported GitHub URL type"}}catch(f){return{
|
|
|
136
191
|
// Headless rendering engines produce slightly different canvas output
|
|
137
192
|
// than headed Chrome. Subtle noise breaks hash-based fingerprinting.
|
|
138
193
|
try {
|
|
139
|
-
var __canvasNoise = ((Date.now()
|
|
194
|
+
var __canvasNoise = ((Date.now() & 0xFF) | 1);
|
|
140
195
|
var origFill = CanvasRenderingContext2D.prototype.fillText;
|
|
141
196
|
CanvasRenderingContext2D.prototype.fillText = __markNative(function fillText() {
|
|
142
197
|
this.globalAlpha = 0.9995;
|
|
@@ -155,15 +210,39 @@ ${g}`,tree:f}}return{ok:!1,error:"Unsupported GitHub URL type"}}catch(f){return{
|
|
|
155
210
|
HTMLCanvasElement.prototype.toDataURL = __markNative(function toDataURL() {
|
|
156
211
|
var ctx = this.getContext('2d');
|
|
157
212
|
if (ctx) {
|
|
158
|
-
//
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
213
|
+
// Spread noise across canvas to break hash-based fingerprinting.
|
|
214
|
+
// Uses a deterministic pattern so it's consistent per page load
|
|
215
|
+
// but varies between sessions.
|
|
216
|
+
var w = this.width, h = this.height;
|
|
217
|
+
if (w > 0 && h > 0) {
|
|
218
|
+
var imgData = ctx.getImageData(0, 0, Math.min(w, 4), Math.min(h, 4));
|
|
219
|
+
if (imgData && imgData.data) {
|
|
220
|
+
for (var __i = 0; __i < imgData.data.length; __i += 4) {
|
|
221
|
+
imgData.data[__i] ^= (__canvasNoise + __i) & 0xFF;
|
|
222
|
+
}
|
|
223
|
+
ctx.putImageData(imgData, 0, 0);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
162
226
|
}
|
|
163
227
|
return origToDataURL.apply(this, arguments);
|
|
164
228
|
});
|
|
165
229
|
} catch(_) {}
|
|
166
230
|
|
|
231
|
+
// ── AudioContext fingerprint noise ────────────────────
|
|
232
|
+
// Headless Chrome's AudioContext produces slightly different output.
|
|
233
|
+
// Subtle noise breaks audio-based fingerprinting.
|
|
234
|
+
try {
|
|
235
|
+
var __audioSeed = ((Date.now() & 0x1F) | 1);
|
|
236
|
+
var origGetChannelData = AudioBuffer.prototype.getChannelData;
|
|
237
|
+
AudioBuffer.prototype.getChannelData = __markNative(function getChannelData(channel) {
|
|
238
|
+
var data = origGetChannelData.call(this, channel);
|
|
239
|
+
for (var __i = 0; __i < data.length; __i += 64) {
|
|
240
|
+
data[__i] *= 0.99999;
|
|
241
|
+
}
|
|
242
|
+
return data;
|
|
243
|
+
});
|
|
244
|
+
} catch(_) {}
|
|
245
|
+
|
|
167
246
|
// ── window outer dimensions ──────────────────────────
|
|
168
247
|
// outerWidth/Height = 0 in headless — a well-known bot signal.
|
|
169
248
|
// Mirror innerWidth/Height (set by --window-size flag) so the ratio is sane.
|
|
@@ -173,9 +252,15 @@ ${g}`,tree:f}}return{ok:!1,error:"Unsupported GitHub URL type"}}catch(f){return{
|
|
|
173
252
|
} catch(_) {}
|
|
174
253
|
|
|
175
254
|
// ── screen properties ─────────────────────────────────
|
|
255
|
+
// Headless Chrome often reports an 800x600 screen even when the viewport is
|
|
256
|
+
// 1920x1080. Keep screen metrics internally consistent with our launch flags.
|
|
176
257
|
try {
|
|
177
|
-
|
|
178
|
-
|
|
258
|
+
Object.defineProperty(screen, 'width', { get: () => 1920, configurable: true });
|
|
259
|
+
Object.defineProperty(screen, 'height', { get: () => 1080, configurable: true });
|
|
260
|
+
Object.defineProperty(screen, 'availWidth', { get: () => 1920, configurable: true });
|
|
261
|
+
Object.defineProperty(screen, 'availHeight', { get: () => 1040, configurable: true });
|
|
262
|
+
Object.defineProperty(screen, 'colorDepth', { get: () => 24, configurable: true });
|
|
263
|
+
Object.defineProperty(screen, 'pixelDepth', { get: () => 24, configurable: true });
|
|
179
264
|
} catch(_) {}
|
|
180
265
|
|
|
181
266
|
// ── navigator.userAgentData (UA Client Hints) ─────────
|
|
@@ -248,35 +333,35 @@ ${g}`,tree:f}}return{ok:!1,error:"Unsupported GitHub URL type"}}catch(f){return{
|
|
|
248
333
|
};
|
|
249
334
|
} catch(_) {}
|
|
250
335
|
})();
|
|
251
|
-
`})])}function
|
|
252
|
-
[greedysearch] Available engines: ${Object.keys(
|
|
253
|
-
`);if(
|
|
254
|
-
`)}}}catch{}return
|
|
255
|
-
`,"utf8")}catch{}}function
|
|
256
|
-
[greedysearch] Available synthesizers: ${
|
|
257
|
-
[greedysearch] Falling back to default: ${
|
|
258
|
-
`)}}}catch{}return
|
|
259
|
-
[greedysearch] Available engines: ${Object.keys(
|
|
260
|
-
`);if(
|
|
261
|
-
`)}}}catch{}return
|
|
262
|
-
`,"utf8")}catch{}}function
|
|
263
|
-
[greedysearch] Available synthesizers: ${
|
|
264
|
-
[greedysearch] Falling back to default: ${
|
|
265
|
-
`)}}}catch{}return
|
|
266
|
-
`)[0];if(!t)throw Error("No Chrome tabs found");return t.slice(0,8)}async function
|
|
267
|
-
[greedysearch] Available engines: ${Object.keys(
|
|
268
|
-
`);if(
|
|
269
|
-
`)}}}catch{}return
|
|
270
|
-
`,"utf8")}catch{}}function
|
|
271
|
-
[greedysearch] Available synthesizers: ${
|
|
272
|
-
[greedysearch] Falling back to default: ${
|
|
273
|
-
`)}}}catch{}return
|
|
274
|
-
|
|
275
|
-
${
|
|
276
|
-
`)}}if(
|
|
277
|
-
`);let
|
|
278
|
-
`)}let
|
|
279
|
-
`),await
|
|
336
|
+
`})])}function Tg(t,n=9222){let f=Number.parseInt(String(t??""),10);return Number.isInteger(f)&&f>1024&&f<65535?f:n}function Hg(){try{if(Jt(pt)){let t=Af(pt,"utf8"),n=JSON.parse(t);if(Array.isArray(n.engines)&&n.engines.length>0&&n.engines.every((f)=>typeof f==="string")){let f=n.engines.filter((g)=>rn[g]),p=n.engines.filter((g)=>!rn[g]);if(p.length>0)process.stderr.write(`[greedysearch] Warning: ignoring unknown engine(s) in ${pt}: ${p.join(", ")}
|
|
337
|
+
[greedysearch] Available engines: ${Object.keys(rn).join(", ")}
|
|
338
|
+
`);if(f.length>0)return f;process.stderr.write(`[greedysearch] Warning: no valid engines in ${pt}, falling back to defaults: ${en.join(", ")}
|
|
339
|
+
`)}}}catch{}return en}function Lg(){try{if(!Jt(hn))eg(hn,{recursive:!0});if(!Jt(pt))Eg(pt,JSON.stringify({engines:en,synthesizer:En},null,2)+`
|
|
340
|
+
`,"utf8")}catch{}}function Yg(){try{if(Jt(pt)){let t=Af(pt,"utf8"),n=JSON.parse(t);if(typeof n.synthesizer==="string"){let f=n.synthesizer.toLowerCase();if(ef.includes(f))return f;process.stderr.write(`[greedysearch] Warning: unknown synthesizer "${n.synthesizer}" in ${pt}
|
|
341
|
+
[greedysearch] Available synthesizers: ${ef.join(", ")}
|
|
342
|
+
[greedysearch] Falling back to default: ${En}
|
|
343
|
+
`)}}}catch{}return En}function Ig(t,n=9222){let f=Number.parseInt(String(t??""),10);return Number.isInteger(f)&&f>1024&&f<65535?f:n}function Fg(){try{if(Zt(gt)){let t=kf(gt,"utf8"),n=JSON.parse(t);if(Array.isArray(n.engines)&&n.engines.length>0&&n.engines.every((f)=>typeof f==="string")){let f=n.engines.filter((g)=>ln[g]),p=n.engines.filter((g)=>!ln[g]);if(p.length>0)process.stderr.write(`[greedysearch] Warning: ignoring unknown engine(s) in ${gt}: ${p.join(", ")}
|
|
344
|
+
[greedysearch] Available engines: ${Object.keys(ln).join(", ")}
|
|
345
|
+
`);if(f.length>0)return f;process.stderr.write(`[greedysearch] Warning: no valid engines in ${gt}, falling back to defaults: ${bn.join(", ")}
|
|
346
|
+
`)}}}catch{}return bn}function Gg(){try{if(!Zt(Dn))Cg(Dn,{recursive:!0});if(!Zt(gt))vg(gt,JSON.stringify({engines:bn,synthesizer:Cn},null,2)+`
|
|
347
|
+
`,"utf8")}catch{}}function Wg(){try{if(Zt(gt)){let t=kf(gt,"utf8"),n=JSON.parse(t);if(typeof n.synthesizer==="string"){let f=n.synthesizer.toLowerCase();if(Ef.includes(f))return f;process.stderr.write(`[greedysearch] Warning: unknown synthesizer "${n.synthesizer}" in ${gt}
|
|
348
|
+
[greedysearch] Available synthesizers: ${Ef.join(", ")}
|
|
349
|
+
[greedysearch] Falling back to default: ${Cn}
|
|
350
|
+
`)}}}catch{}return Cn}async function Yf(){let t=(await G(["list"])).split(`
|
|
351
|
+
`)[0];if(!t)throw Error("No Chrome tabs found");return t.slice(0,8)}async function Rn(t="about:blank"){let n=await Yf(),f=new URL(t).hostname;if(f==="copilot.microsoft.com"||f==="www.perplexity.ai"||f==="perplexity.ai"||f.endsWith(".perplexity.ai")){let r=await G(["evalraw",n,"Target.createTarget",JSON.stringify({url:"about:blank"})]),{targetId:a}=JSON.parse(r),l=a.slice(0,8);if(await G(["list"]).catch(()=>null),f==="copilot.microsoft.com")await hf(l);else hf(l).catch(()=>{});return await G(["list"]).catch(()=>null),a}let p=await G(["evalraw",n,"Target.createTarget",JSON.stringify({url:t})]),{targetId:g}=JSON.parse(p);return await G(["list"]).catch(()=>null),g}async function yn(t){try{let n=await Yf();await G(["evalraw",n,"Target.closeTarget",JSON.stringify({targetId:t})])}catch{}}function Kg(t,n=9222){let f=Number.parseInt(String(t??""),10);return Number.isInteger(f)&&f>1024&&f<65535?f:n}function og(){try{if(Kt(at)){let t=If(at,"utf8"),n=JSON.parse(t);if(Array.isArray(n.engines)&&n.engines.length>0&&n.engines.every((f)=>typeof f==="string")){let f=n.engines.filter((g)=>wn[g]),p=n.engines.filter((g)=>!wn[g]);if(p.length>0)process.stderr.write(`[greedysearch] Warning: ignoring unknown engine(s) in ${at}: ${p.join(", ")}
|
|
352
|
+
[greedysearch] Available engines: ${Object.keys(wn).join(", ")}
|
|
353
|
+
`);if(f.length>0)return f;process.stderr.write(`[greedysearch] Warning: no valid engines in ${at}, falling back to defaults: ${jn.join(", ")}
|
|
354
|
+
`)}}}catch{}return jn}function Bg(){try{if(!Kt(vn))Vg(vn,{recursive:!0});if(!Kt(at))mg(at,JSON.stringify({engines:jn,synthesizer:Pn},null,2)+`
|
|
355
|
+
`,"utf8")}catch{}}function Ug(){try{if(Kt(at)){let t=If(at,"utf8"),n=JSON.parse(t);if(typeof n.synthesizer==="string"){let f=n.synthesizer.toLowerCase();if(Df.includes(f))return f;process.stderr.write(`[greedysearch] Warning: unknown synthesizer "${n.synthesizer}" in ${at}
|
|
356
|
+
[greedysearch] Available synthesizers: ${Df.join(", ")}
|
|
357
|
+
[greedysearch] Falling back to default: ${Pn}
|
|
358
|
+
`)}}}catch{}return Pn}function Qg(){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,n=0,f=0){this.data=t,this.width=n,this.height=f}};if(typeof globalThis.Path2D>"u")globalThis.Path2D=class{constructor(t=void 0){}}}async function xg(){Qg();let t=await import("pdf-parse"),n=t.PDFParse??t.default;if(!n)throw Error("pdf-parse did not export PDFParse");return n}async function zg(t,n){try{let f=new(await xg())({data:new Uint8Array(t)});await f.load();let p=await f.getText(),g=p.text?.trim();if(!g)return null;return{title:new URL(n).pathname.split("/").pop()||"Document.pdf",content:`## PDF Content (${p.total} pages)
|
|
359
|
+
|
|
360
|
+
${g}`,pages:p.total}}catch(f){return{error:f.message||String(f)}}}function Ff(t="",n=240){let f=String(t).replaceAll(/\s+/g," ").trim();if(f.length<=n)return f;let p=f.slice(0,n),g=p.lastIndexOf(" ");return g>0?`${p.slice(0,g)}...`:`${p}...`}async function Sg(t,n,f=8000){let p=Date.now();try{let g=(await G(["evalraw",t,"Page.getFrameTree","{}"]).then((E)=>JSON.parse(E)).catch(()=>null))?.frameTree?.frame?.id||void 0,r=await G(["evalraw",t,"Network.loadNetworkResource",JSON.stringify({frameId:g,url:n,options:{disableCache:!0,includeCredentials:!1}})],20000),a=JSON.parse(r).resource;if(!a?.success||!a.httpStatusCode)return{url:n,error:a?.netErrorName||a?.netError||"loadNetworkResource failed",source:"chrome",duration:Date.now()-p,needsFallback:!0};let l="";if(a.stream)try{let E=await G(["evalraw",t,"IO.read",JSON.stringify({handle:a.stream})],1e4);l=JSON.parse(E).data||"",await G(["evalraw",t,"IO.close",JSON.stringify({handle:a.stream})]).catch(()=>{})}catch{}if(!l||l.length<100)return{url:n,error:"Empty response body from Network.loadNetworkResource",source:"chrome",duration:Date.now()-p,needsFallback:!0};let w=Pf(a.httpStatusCode,l,n,n);if(w.blocked)return{url:n,status:a.httpStatusCode,error:`Blocked: ${w.reason}`,source:"chrome",duration:Date.now()-p,needsBrowser:!0};let $=await Rf(l,n),_=yf($);if(!_.ok)return{url:n,status:a.httpStatusCode,error:`Low quality: ${_.reason}`,source:"chrome",duration:Date.now()-p,needsBrowser:!0};let e=Rt($.markdown,f);return{url:n,finalUrl:n,status:a.httpStatusCode,contentType:"text/markdown",lastModified:"",publishedTime:$.publishedTime||"",byline:$.byline||"",siteName:$.siteName||"",lang:$.lang||"",title:$.title||n,snippet:$.excerpt,content:e,contentChars:e.length,source:"chrome",duration:Date.now()-p}}catch(g){return{url:n,error:g.message,source:"chrome",duration:Date.now()-p,needsFallback:!0}}}function Gf(t){try{return new URL(t).pathname.toLowerCase().endsWith(".pdf")}catch{return!1}}async function cg(t,n=8000){let f=$n(t);if(f.blocked)return{url:t,finalUrl:t,status:403,error:`Blocked: ${f.reason}`,source:"pdf-http"};let p=new AbortController,g=setTimeout(()=>p.abort(),20000),r=Date.now();try{let a=await fetch(t,{method:"GET",redirect:"follow",signal:p.signal,headers:xp({accept:"application/pdf,application/octet-stream;q=0.9,*/*;q=0.5"})});clearTimeout(g);let l=a.headers.get("content-type")||"",w=a.url||t,$=Number.parseInt(a.headers.get("content-length")||"0",10);if(a.status>=400)return{url:t,finalUrl:w,status:a.status,error:`HTTP ${a.status}`,source:"pdf-http",duration:Date.now()-r};if(!l.toLowerCase().includes("application/pdf")&&!Gf(w))return null;if($>31457280)return{url:t,finalUrl:w,status:a.status,error:`PDF too large: ${$} bytes`,source:"pdf-http",duration:Date.now()-r};let _=Buffer.from(await a.arrayBuffer()),e=await zg(_,w);if(!e||e.error)return{url:t,finalUrl:w,status:a.status,error:e?.error||"PDF text extraction failed",source:"pdf-http",duration:Date.now()-r};let E=Rt(e.content,n);return{url:t,finalUrl:w,status:a.status,contentType:"application/pdf",lastModified:a.headers.get("last-modified")||"",title:e.title,snippet:Ff(E,320),content:E,contentChars:E.length,pages:e.pages,source:"pdf-http",duration:Date.now()-r}}catch(a){return clearTimeout(g),{url:t,finalUrl:t,error:a.message||String(a),source:"pdf-http",duration:Date.now()-r}}}async function Wf(t,n=8000){let f=Date.now();if(Gf(t)){let g=await cg(t,n);if(g?.content||g?.status===403)return g}if(_n(t)){let g=_n(t);if(g&&(g.type==="root"||g.type==="tree"||g.type==="blob"&&!g.path?.includes("."))){let r=await fg(t);if(r.ok){let a=Rt(r.content,n);return{url:t,finalUrl:t,status:200,contentType:"text/markdown",lastModified:"",title:r.title,snippet:a.slice(0,320),content:a,contentChars:a.length,source:"github-api",...r.tree&&{tree:r.tree},duration:Date.now()-f}}process.stderr.write(`[greedysearch] GitHub API fetch failed, trying HTTP: ${r.error}
|
|
361
|
+
`)}}if(gg(t)?.type==="post"){process.stderr.write(`[greedysearch] Using Reddit JSON API for: ${t.slice(0,60)}...
|
|
362
|
+
`);let g=await ag(t,n);if(g.ok){let r=Rt(g.markdown,n);return{url:t,finalUrl:g.finalUrl,status:g.status,contentType:"text/markdown",lastModified:g.lastModified||"",publishedTime:g.publishedTime||"",byline:g.byline||"",siteName:g.siteName||"",lang:g.lang||"",title:g.title,snippet:g.excerpt,content:r,contentChars:r.length,source:"reddit-api",duration:Date.now()-f}}process.stderr.write(`[greedysearch] Reddit API fetch failed, falling back to HTTP: ${g.error}
|
|
363
|
+
`)}let p=await Sp(t,{timeoutMs:1e4});if(p.ok){let g=Rt(p.markdown,n);return{url:t,finalUrl:p.finalUrl,status:p.status,contentType:"text/markdown",lastModified:p.lastModified||"",publishedTime:p.publishedTime||"",byline:p.byline||"",siteName:p.siteName||"",lang:p.lang||"",title:p.title,snippet:p.excerpt,content:g,contentChars:g.length,source:"http",duration:Date.now()-f}}if(p.needsBrowser)try{let g=await Rn();try{let r=await Sg(g,t,n);if(r.content&&r.content.length>100)return r}finally{await yn(g)}}catch{}return process.stderr.write(`[greedysearch] HTTP failed for ${t.slice(0,60)}, trying browser...
|
|
364
|
+
`),await dg(t,n)}async function Vf(t,n=4000,f=200){let p=Date.now()+n;while(Date.now()<p){try{if((await G(["eval",t,'document.readyState === "complete" && !!document.body && document.body.innerText.length > 500'])).trim()==="true")return}catch{}await new Promise((g)=>setTimeout(g,f))}}async function dg(t,n=8000){let f=Date.now(),p;try{p=await Rn()}catch(g){return{url:t,title:"",content:null,snippet:"",contentChars:0,error:`openNewTab failed: ${g.message}`,source:"browser",duration:Date.now()-f}}try{await G(["nav",p,t],30000),await Vf(p);let g=await G(["eval",p,String.raw`
|
|
280
365
|
(function(){
|
|
281
366
|
var el = document.querySelector('article, [role="main"], main, .post-content, .article-body, #content, .content');
|
|
282
367
|
var text = (el || document.body).innerText;
|
|
@@ -286,106 +371,116 @@ ${a}`,pages:n.total}}catch(r){return{error:r.message||String(r)}}}function ar(t=
|
|
|
286
371
|
url: location.href
|
|
287
372
|
});
|
|
288
373
|
})()
|
|
289
|
-
`]),
|
|
290
|
-
`);let
|
|
291
|
-
`);let
|
|
292
|
-
`);else if(
|
|
293
|
-
`);
|
|
294
|
-
`)}}await Promise.all(Array.from({length:
|
|
295
|
-
`),
|
|
374
|
+
`]),r=JSON.parse(g),a=Rt(r.content,n);return{url:t,finalUrl:r.url||t,status:200,contentType:"text/plain",lastModified:"",title:r.title,snippet:Ff(a,320),content:a,contentChars:a.length,source:"browser",duration:Date.now()-f}}catch(g){return{url:t,title:"",content:null,snippet:"",contentChars:0,error:g.message,source:"browser",duration:Date.now()-f}}finally{await yn(p)}}async function On(t,n=5,f=8000,p=bf){let g=t.slice(0,n);if(g.length===0)return[];let r=Math.min(g.length,Math.max(1,Number.parseInt(String(p),10)||bf));process.stderr.write(`[greedysearch] Fetching content from ${g.length} sources via HTTP (concurrency ${r})...
|
|
375
|
+
`);let a=Array(g.length),l=0,w=0;async function $(){while(!0){let h=l++;if(h>=g.length)return;let b=g[h],D=b.canonicalUrl||b.url;process.stderr.write(`[greedysearch] [${h+1}/${g.length}] Fetching: ${D.slice(0,60)}...
|
|
376
|
+
`);let v=await Wf(D,f).catch((P)=>({url:D,title:"",content:null,snippet:"",contentChars:0,error:P.message,source:"error",duration:0}));if(a[h]={id:b.id,...v},v.content&&v.content.length>100)process.stderr.write(`[greedysearch] ✓ ${v.source}: ${v.content.length} chars
|
|
377
|
+
`);else if(v.error)process.stderr.write(`[greedysearch] ✗ ${v.error.slice(0,80)}
|
|
378
|
+
`);w+=1,process.stderr.write(`PROGRESS:fetch:${w}/${g.length}
|
|
379
|
+
`)}}await Promise.all(Array.from({length:r},()=>$()));let _=a.filter((h)=>h.content&&h.content.length>100),e=a.filter((h)=>h.source==="http").length,E=a.filter((h)=>h.source==="browser").length;return process.stderr.write(`[greedysearch] Fetched ${_.length}/${a.length} sources (HTTP: ${e}, Browser: ${E})
|
|
380
|
+
`),a}async function qg(t){let n=await Rn();try{await G(["nav",n,t],30000),await Vf(n);let f=await G(["eval",n,String.raw`
|
|
296
381
|
(function(){
|
|
297
382
|
var el = document.querySelector('article, [role="main"], main, .post-content, .article-body, #content, .content');
|
|
298
383
|
var text = (el || document.body).innerText;
|
|
299
384
|
return text.replace(/\s+/g, ' ').trim();
|
|
300
385
|
})()
|
|
301
|
-
`]);return{url:t,content:
|
|
302
|
-
url: ${r.finalUrl||r.url}
|
|
303
|
-
title: ${r.title||""}
|
|
304
|
-
source: ${r.source||"unknown"}
|
|
305
|
-
status: ${r.status||""}
|
|
306
|
-
chars: ${r.contentChars||r.content.length}
|
|
307
|
-
---
|
|
308
|
-
|
|
309
|
-
`;ka(i,f+r.content,"utf8");let{content:g,...o}=r;return{...o,contentPath:i,contentChars:r.contentChars||g.length}})}var ja;var hr=ot(()=>{ja=gr(process.cwd(),".dm","greedysearch-sources")});var ut={};et(ut,{parseGitHubUrl:()=>$r,fetchGitHubContent:()=>Pa});function $r(t){try{let e=new URL(t);if(!(e.hostname==="github.com"||e.hostname.endsWith(".github.com")))return null;let r=e.pathname.split("/").filter(Boolean);if(r.length<2)return null;let[n,a]=r;if(r.length===2)return{owner:n,repo:a,type:"root"};if(r.length>=4&&(r[2]==="blob"||r[2]==="tree")){let l=r[2],i=r[3],f=r.slice(4).join("/");return{owner:n,repo:a,type:l,ref:i,path:f}}return null}catch{return null}}async function B(t,e=1e4){let r=new AbortController,n=setTimeout(()=>r.abort(),e);try{let a=await fetch(`https://api.github.com${t}`,{headers:wr,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 Na(t,e){try{let r=await B(`/repos/${t}/${e}/readme`);if(r.content&&r.encoding==="base64")return Buffer.from(r.content,"base64").toString("utf8");return""}catch{return""}}async function cr(t,e,r="HEAD",n=""){try{let a=await B(`/repos/${t}/${e}/git/ref/heads/${r==="HEAD"?"main":r}`).catch(()=>B(`/repos/${t}/${e}/git/ref/heads/master`).catch(()=>null)),l;if(a?.object?.sha)l=(await B(`/repos/${t}/${e}/git/commits/${a.object.sha}`)).tree.sha;else{let f=await B(`/repos/${t}/${e}`);l=(await B(`/repos/${t}/${e}/branches/${f.default_branch}`)).commit.commit.tree.sha}let i=(await B(`/repos/${t}/${e}/git/trees/${l}`)).tree||[];if(n)i=i.filter((f)=>f.path.startsWith(n));return i.slice(0,50).map((f)=>({path:f.path,type:f.type==="tree"?"dir":"file",size:f.size}))}catch{return[]}}async function Oa(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,g=setTimeout(()=>f.abort(),a);try{let o=await fetch(i,{headers:{"user-agent":wr["user-agent"]},signal:f.signal});if(clearTimeout(g),o.ok)return await o.text()}catch{clearTimeout(g)}}return null}async function Pa(t){let e=$r(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,g,o]=await Promise.allSettled([B(`/repos/${r}/${n}`),Na(r,n),cr(r,n,l||"HEAD")]);if(f.status==="rejected")return{ok:!1,error:f.reason?.message||"Repo not found"};let p=f.value,h=g.status==="fulfilled"?g.value:"",m=o.status==="fulfilled"?o.value:[],w=p?.description?`
|
|
386
|
+
`]);return{url:t,content:f}}catch(f){return{url:t,content:null,error:f.message}}finally{await yn(n)}}var zr,cr,mt=null,Cf="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36",vf,jf,Xp,op,Bp,cp,dp,qp,Of,pg,yg,Og,Ng,kg,wl,x,$l,_l,hl,el,El,Dl,bl,Cl,vl,hn,pt,en,En="gemini",ef,rn,jl,Pl,Rl,Mg,yl,z,Ol,Nl,il,Al,Tl,kl,Hl,Ll,Yl,Dn,gt,bn,Cn="gemini",Ef,ln,Il,Ml,Fl,Gl,Wl,Vl,ml,G,Xg,ol,S,Bl,Ul,Ql,xl,zl,Sl,cl,dl,ql,vn,at,jn,Pn="gemini",Df,wn,sl,ul,bf;var Xt=qt(()=>{zr=Jp(import.meta.url),cr=Zp(import.meta.url);vf={"user-agent":Cf,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"},jf=[/^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],Xp=/^::ffff:(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/i,op=/^::ffff:([0-9a-f]{1,4}):([0-9a-f]{1,4})$/i,Bp=/^(?:0x[0-9a-f]+|0[0-7]*|[1-9][0-9]*)$/i;cp=["accounts.google.com","login.microsoftonline.com","login.live.com","auth0.com","okta.com","auth.mozilla.auth0.com","id.atlassian.com"],dp=["login.","signin.","auth.","sso.","accounts.","idp."],qp=["sign in to continue","log in to continue","authentication required","create an account to continue","subscribe to continue reading","members only"];Of={"user-agent":"GreedySearch/1.0",accept:"application/vnd.github+json","x-github-api-version":"2022-11-28"};pg={"user-agent":"GreedySearch/1.0 (Research Bot)",accept:"application/json"};yg=$g(hg(import.meta.url)),Og=_g(yg,"..","bin","cdp.mjs"),Ng=new Set(["list","snap","eval","shot","html","nav","net","click","clickxy","type","loadall","evalraw","browse","stop","--tab"]);kg=bg().replaceAll("\\","/"),wl=Tg(process.env.GREEDY_SEARCH_PORT),x=(process.env.GREEDY_SEARCH_PROFILE_DIR||process.env.CDP_PROFILE_DIR||`${kg}/greedysearch-chrome-profile`).replaceAll("\\","/"),$l=`${x}/DevToolsActivePort`,_l=process.env.GREEDY_SEARCH_PID_FILE||`${x}/browser.pid`,hl=process.env.CDP_PAGES_CACHE||`${x}/cdp-pages.json`,el=process.env.GREEDY_SEARCH_MODE_FILE||`${x}/browser-mode`,El=process.env.GREEDY_SEARCH_METADATA_FILE||`${x}/browser-metadata.json`,Dl=process.env.GREEDY_SEARCH_LAUNCH_LOCK_FILE||`${x}/browser-launch.lock`,bl=process.env.GREEDY_SEARCH_ACTIVITY_FILE||`${x}/browser-last-activity`,Cl=(process.env.CDP_SOCKET_DIR||`${x}/cdp-sockets`).replaceAll("\\","/"),vl=`${x}/visible-recovery.jsonl`,hn=Tf(Dg(),".dm"),pt=Tf(hn,"greedyconfig"),en=["perplexity","google","chatgpt","gemini"];Lg();ef=["gemini","chatgpt"];rn={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"},jl=Hg(),Pl=Yg(),Rl=Math.max(1,Number.parseInt(process.env.GREEDY_FETCH_CONCURRENCY||"5",10)||5);process.env.CDP_PROFILE_DIR=x;Mg=Pg().replaceAll("\\","/"),yl=Ig(process.env.GREEDY_SEARCH_PORT),z=(process.env.GREEDY_SEARCH_PROFILE_DIR||process.env.CDP_PROFILE_DIR||`${Mg}/greedysearch-chrome-profile`).replaceAll("\\","/"),Ol=`${z}/DevToolsActivePort`,Nl=process.env.GREEDY_SEARCH_PID_FILE||`${z}/browser.pid`,il=process.env.CDP_PAGES_CACHE||`${z}/cdp-pages.json`,Al=process.env.GREEDY_SEARCH_MODE_FILE||`${z}/browser-mode`,Tl=process.env.GREEDY_SEARCH_METADATA_FILE||`${z}/browser-metadata.json`,kl=process.env.GREEDY_SEARCH_LAUNCH_LOCK_FILE||`${z}/browser-launch.lock`,Hl=process.env.GREEDY_SEARCH_ACTIVITY_FILE||`${z}/browser-last-activity`,Ll=(process.env.CDP_SOCKET_DIR||`${z}/cdp-sockets`).replaceAll("\\","/"),Yl=`${z}/visible-recovery.jsonl`,Dn=Hf(jg(),".dm"),gt=Hf(Dn,"greedyconfig"),bn=["perplexity","google","chatgpt","gemini"];Gg();Ef=["gemini","chatgpt"];ln={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"},Il=Fg(),Ml=Wg(),Fl=Math.max(1,Number.parseInt(process.env.GREEDY_FETCH_CONCURRENCY||"5",10)||5);process.env.CDP_PROFILE_DIR=z;Gl=Number.parseInt(process.env.GREEDY_SEARCH_IDLE_TIMEOUT_MINUTES||"5",10)||5,Wl=import.meta.dirname||new URL(".",import.meta.url).pathname.replace(/^\/([A-Z]:)/,"$1"),Vl=Number.parseInt(process.env.GREEDY_SEARCH_IDLE_TIMEOUT_MINUTES||"5",10)||5,ml=Number.parseInt(process.env.GREEDY_SEARCH_VISIBLE_IDLE_TIMEOUT_MINUTES||"60",10)||60,G=Lf;Xg=Zg().replaceAll("\\","/"),ol=Kg(process.env.GREEDY_SEARCH_PORT),S=(process.env.GREEDY_SEARCH_PROFILE_DIR||process.env.CDP_PROFILE_DIR||`${Xg}/greedysearch-chrome-profile`).replaceAll("\\","/"),Bl=`${S}/DevToolsActivePort`,Ul=process.env.GREEDY_SEARCH_PID_FILE||`${S}/browser.pid`,Ql=process.env.CDP_PAGES_CACHE||`${S}/cdp-pages.json`,xl=process.env.GREEDY_SEARCH_MODE_FILE||`${S}/browser-mode`,zl=process.env.GREEDY_SEARCH_METADATA_FILE||`${S}/browser-metadata.json`,Sl=process.env.GREEDY_SEARCH_LAUNCH_LOCK_FILE||`${S}/browser-launch.lock`,cl=process.env.GREEDY_SEARCH_ACTIVITY_FILE||`${S}/browser-last-activity`,dl=(process.env.CDP_SOCKET_DIR||`${S}/cdp-sockets`).replaceAll("\\","/"),ql=`${S}/visible-recovery.jsonl`,vn=Mf(Jg(),".dm"),at=Mf(vn,"greedyconfig"),jn=["perplexity","google","chatgpt","gemini"];Bg();Df=["gemini","chatgpt"];wn={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"},sl=og(),ul=Ug(),bf=Math.max(1,Number.parseInt(process.env.GREEDY_FETCH_CONCURRENCY||"5",10)||5);process.env.CDP_PROFILE_DIR=S});var Bf={};dt(Bf,{parseGitHubUrl:()=>of,fetchGitHubContent:()=>ba});function of(t){try{let n=new URL(t);if(!(n.hostname==="github.com"||n.hostname.endsWith(".github.com")))return null;let f=n.pathname.split("/").filter(Boolean);if(f.length<2)return null;let[p,g]=f;if(f.length===2)return{owner:p,repo:g,type:"root"};if(f.length>=4&&(f[2]==="blob"||f[2]==="tree")){let r=f[2],a=f[3],l=f.slice(4).join("/");return{owner:p,repo:g,type:r,ref:a,path:l}}return null}catch{return null}}async function U(t,n=1e4){let f=new AbortController,p=setTimeout(()=>f.abort(),n);try{let g=await fetch(`https://api.github.com${t}`,{headers:Xf,signal:f.signal});if(clearTimeout(p),!g.ok)throw Error(`GitHub API ${g.status}: ${t}`);return await g.json()}catch(g){throw clearTimeout(p),g}}async function Ea(t,n){try{let f=await U(`/repos/${t}/${n}/readme`);if(f.content&&f.encoding==="base64")return Buffer.from(f.content,"base64").toString("utf8");return""}catch{return""}}async function Kf(t,n,f="HEAD",p="",g){try{let r;if(f==="HEAD")if(g)r=await U(`/repos/${t}/${n}/git/ref/heads/${g}`).catch(()=>null);else r=await Promise.any([U(`/repos/${t}/${n}/git/ref/heads/main`),U(`/repos/${t}/${n}/git/ref/heads/master`)]).catch(()=>null);else r=await U(`/repos/${t}/${n}/git/ref/heads/${f}`).catch(()=>U(`/repos/${t}/${n}/git/ref/heads/master`).catch(()=>null));if(!r?.object?.sha)return[];let a=(await U(`/repos/${t}/${n}/git/commits/${r.object.sha}`)).tree.sha,l=(await U(`/repos/${t}/${n}/git/trees/${a}`)).tree||[];if(p)l=l.filter((w)=>w.path.startsWith(p));return l.slice(0,50).map((w)=>({path:w.path,type:w.type==="tree"?"dir":"file",size:w.size}))}catch{return[]}}async function Da(t,n,f,p,g=1e4,r){let a=async(w)=>{let $=new AbortController,_=setTimeout(()=>$.abort(),g);try{let e=await fetch(w,{headers:{"user-agent":Xf["user-agent"]},signal:$.signal});if(clearTimeout(_),e.ok)return await e.text();throw Error("not ok")}catch{throw clearTimeout(_),Error("failed")}};if(!f||f==="HEAD"){if(r)try{return await a(`https://raw.githubusercontent.com/${t}/${n}/${r}/${p}`)}catch{return null}try{return await Promise.any([a(`https://raw.githubusercontent.com/${t}/${n}/main/${p}`),a(`https://raw.githubusercontent.com/${t}/${n}/master/${p}`)])}catch{return null}}let l=[`https://raw.githubusercontent.com/${t}/${n}/${f}/${p}`,`https://raw.githubusercontent.com/${t}/${n}/master/${p}`];for(let w of l)try{return await a(w)}catch{}return null}async function ba(t){let n=of(t);if(!n)return{ok:!1,error:"Not a valid GitHub URL"};let{owner:f,repo:p,type:g,ref:r,path:a}=n;try{if(g==="root"||g==="tree"&&!a){let l=await U(`/repos/${f}/${p}`),[w,$]=await Promise.allSettled([Ea(f,p),Kf(f,p,r||"HEAD","",l.default_branch)]),_=w.status==="fulfilled"?w.value:"",e=$.status==="fulfilled"?$.value:[],E=l?.description?`
|
|
310
387
|
|
|
311
|
-
> ${
|
|
388
|
+
> ${l.description}`:"",h=l?.stargazers_count==null?"":` ⭐ ${l.stargazers_count}`,b=l?.language?` · ${l.language}`:"",D=`# ${f}/${p}${h}${b}${E}
|
|
312
389
|
|
|
313
|
-
`;if(
|
|
390
|
+
`;if(_)D+=_.slice(0,6000);else D+=`[No README found]
|
|
314
391
|
|
|
315
392
|
Files:
|
|
316
|
-
${
|
|
317
|
-
`)}`;return{ok:!0,title:`${
|
|
318
|
-
`);return{ok:!0,title:`${
|
|
393
|
+
${e.map((v)=>` ${v.type==="dir"?"\uD83D\uDCC1":"\uD83D\uDCC4"} ${v.path}`).join(`
|
|
394
|
+
`)}`;return{ok:!0,title:`${f}/${p}`,content:D,tree:e.slice(0,30)}}if(g==="blob"&&a){let l;if(!r||r==="HEAD")try{l=(await U(`/repos/${f}/${p}`)).default_branch}catch{l=void 0}let w=await Da(f,p,r,a,1e4,l);if(w===null)return{ok:!1,error:`File not found: ${a}`};return{ok:!0,title:`${f}/${p}: ${a}`,content:w}}if(g==="tree"&&a){let l;if(!r||r==="HEAD")try{l=(await U(`/repos/${f}/${p}`)).default_branch}catch{l=void 0}let w=await Kf(f,p,r||"HEAD",a,l),$=w.map((_)=>` ${_.type==="dir"?"\uD83D\uDCC1":"\uD83D\uDCC4"} ${_.path}`).join(`
|
|
395
|
+
`);return{ok:!0,title:`${f}/${p}/${a}`,content:`[Directory: ${a}]
|
|
319
396
|
|
|
320
397
|
Files:
|
|
321
|
-
${
|
|
322
|
-
|
|
323
|
-
[
|
|
324
|
-
|
|
325
|
-
`,
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
`).trim(),i=n.publishedTime||mr(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:mr(r),markdown:i,excerpt:i.slice(0,300)}}return{title:e,byline:"",siteName:"",lang:"",publishedTime:"",markdown:"",excerpt:""}}function kr(t){let e=t.markdown.trim().toLowerCase(),r=(t.title||"").toLowerCase();if(t.markdown.trim().length<100)return{ok:!1,reason:"content too short (< 100 chars)"};let n=e.toLowerCase(),a=[{check:()=>n.includes("loading")&&n.includes("please wait"),desc:"loading page"},{check:()=>n.includes("please ensure javascript is enabled"),desc:"requires javascript"},{check:()=>n.includes("enable javascript to view"),desc:"requires javascript"},{check:()=>n.includes("just a moment"),desc:"cloudflare challenge detected in content"},{check:()=>n.includes("verify you are human"),desc:"human verification"},{check:()=>n.includes("captcha required"),desc:"captcha in extracted content"},{check:()=>n.includes("access denied"),desc:"access denied in content"},{check:()=>/^\s{0,10}sign\s{1,5}in\s{0,10}$|^\s{0,10}log\s{1,5}in\s{0,10}$/im.test(e),desc:"login form only"}];for(let{check:l,desc:i}of a)if(l())return{ok:!1,reason:i};if(r.includes("just a moment")||r.includes("checking your browser"))return{ok:!1,reason:"cloudflare challenge page detected in title"};return{ok:!0}}function Za(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 br,sr="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36",_r,Ha,Ia,Ja,Ya;var Cr=ot(()=>{br=new Wa({headingStyle:"atx",bulletListMarker:"-",codeBlockStyle:"fenced"});br.addRule("removeDataUrls",{filter:(t)=>t.tagName==="IMG"&&t.getAttribute("src")?.startsWith("data:"),replacement:()=>""});_r={"user-agent":sr,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"},Ha=[/^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];Ia=["accounts.google.com","login.microsoftonline.com","login.live.com","auth0.com","okta.com","auth.mozilla.auth0.com","id.atlassian.com"],Ja=["login.","signin.","auth.","sso.","accounts.","idp."],Ya=["sign in to continue","log in to continue","authentication required","create an account to continue","subscribe to continue reading","members only"]});import{spawn as Ka}from"node:child_process";import{mkdirSync as ee,writeFileSync as L}from"node:fs";import{join as j}from"node:path";import{fileURLToPath as Ba}from"node:url";var Kr=["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"],Ur=["arstechnica.com","techcrunch.com","theverge.com","venturebeat.com","wired.com","zdnet.com"],ye=["facebook.com","instagram.com","linkedin.com","pinterest.com","tiktok.com","twitter.com","x.com"];function O(t="",e=240){let r=String(t).replaceAll(/\s+/g," ").trim();if(r.length<=e)return r;let n=r.slice(0,e),a=n.lastIndexOf(" ");return a>0?`${n.slice(0,a)}...`:`${n}...`}function Ot(t=""){let e=O(t,180);if(!e)return"";if(/^https?:\/\//i.test(e))return"";let r=e.split(/\s+/).filter(Boolean).length,n=/[A-Z]/.test(e),a=/\d/.test(e);return e===e.toLowerCase()&&r<=4&&!n&&!a?"":e}function Pt(t="",e=""){let r=Ot(t),n=Ot(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 A(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(Kr.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 Xr(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 Rt(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,ye))return"social";if(rt(t,Br))return"community";if(rt(t,Ur))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 xr(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 Qr(t){let e=Object.values(t.perEngine||{}).map((r)=>r?.rank||99);return e.length?Math.min(...e):99}var Sr=["reddit.com","news.ycombinator.com","lobste.rs"];function X(t){return t.smartScore*3+t.engineCount*5+xr(t.sourceType)*2+Math.max(0,7-Qr(t))}function zr(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 ye){let a=n.replace(/\.com$/,"");if(e.includes(a))r.push(n)}return[...new Set(r)]}function Ee(t,e){return t===e||t.endsWith(`.${e}`)}function Tt(t,e=""){let r=new Map,n=Object.keys(t||{}).filter((o)=>!o.startsWith("_")),a=zr(e);for(let o of n){let p=t[o];if(!p?.sources)continue;for(let h=0;h<p.sources.length;h++){let m=p.sources[h],w=A(m.url);if(!w||w.length<10)continue;let s=Ot(m.title||""),b=Xr(w),c=Rt(b,s,w),E=0;if(a.some((N)=>Ee(b,N)))E+=10;if(c==="official-docs")E+=3;let C=w.toLowerCase();if(/\/docs\/|\/documentation\/|\.dev\/|\/api\/|\/reference\//.test(C))E+=2;let _=a.some((N)=>Ee(b,N));if(c==="social"&&!_)E-=20;if(a.length>0){if(rt(b,Sr))E-=3;else if(c==="community"&&!rt(b,["stackoverflow.com","stackexchange.com"]))E-=1}let y=r.get(w)||{id:"",canonicalUrl:w,displayUrl:m.url||w,domain:b,title:"",engines:[],engineCount:0,perEngine:{},sourceType:c,isOfficial:c==="official-docs",smartScore:0};if(y.title=Pt(y.title,s),y.displayUrl=y.displayUrl||m.url||w,y.sourceType=y.sourceType||c,y.isOfficial=y.isOfficial||c==="official-docs",y.smartScore=Math.max(y.smartScore,E),!y.engines.includes(o))y.engines.push(o);y.perEngine[o]={rank:h+1,title:Pt(y.perEngine[o]?.title||"",s)},r.set(w,y)}}let l=Array.from(r.values()).map((o)=>({...o,engineCount:o.engines.length})),i=l.filter((o)=>o.sourceType!=="social"),f=l.filter((o)=>o.sourceType==="social");return i.sort((o,p)=>{let h=X(p)-X(o);if(h!==0)return h;return o.domain.localeCompare(p.domain)}),f.sort((o,p)=>{let h=X(p)-X(o);if(h!==0)return h;return o.domain.localeCompare(p.domain)}),[...i,...f].slice(0,12).map((o,p)=>({...o,id:`S${p+1}`,title:o.title||o.domain||o.canonicalUrl}))}function De(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=Pt(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 gt,mkdirSync as ur,readFileSync as ve,writeFileSync as qr}from"node:fs";import{homedir as tn}from"node:os";import{join as ke}from"node:path";import{tmpdir as pt}from"node:os";function en(t,e=9222){let r=Number.parseInt(String(t??""),10);return Number.isInteger(r)&&r>1024&&r<65535?r:e}var Yl=en(process.env.GREEDY_SEARCH_PORT,9222),ht=(process.env.GREEDY_SEARCH_PROFILE_DIR||process.env.CDP_PROFILE_DIR||`${pt().replaceAll("\\","/")}/greedysearch-chrome-profile`).replaceAll("\\","/"),dl=`${ht}/DevToolsActivePort`,Vl=`${pt().replaceAll("\\","/")}/cdp-pages.json`,Zl=process.env.GREEDY_SEARCH_MODE_FILE||`${pt().replaceAll("\\","/")}/greedysearch-chrome-mode`,Kl=`${pt().replaceAll("\\","/")}/greedysearch-visible-recovery.jsonl`,At=ke(tn(),".dm"),J=ke(At,"greedyconfig"),Mt=["perplexity","google","chatgpt"],Wt="gemini";function rn(){try{if(gt(J)){let t=ve(J,"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 ${J}: ${n.join(", ")}
|
|
330
|
-
|
|
331
|
-
`);if(r.length>0)return
|
|
332
|
-
`)}}}
|
|
333
|
-
|
|
334
|
-
[
|
|
335
|
-
[greedysearch]
|
|
336
|
-
`)}
|
|
337
|
-
`)
|
|
338
|
-
|
|
339
|
-
`)
|
|
340
|
-
`)
|
|
398
|
+
${$}`,tree:w}}return{ok:!1,error:"Unsupported GitHub URL type"}}catch(l){return{ok:!1,error:l.message}}}var Xf;var Uf=qt(()=>{Xf={"user-agent":"GreedySearch/1.0",accept:"application/vnd.github+json","x-github-api-version":"2022-11-28"}});import{spawn as Ca}from"node:child_process";import{basename as lp}from"node:path";function jt(t=process.env,n=process.execPath){let f=t.GREEDY_SEARCH_NODE||t.NODE_BINARY||t.NODE;if(f?.trim())return f.trim();let p=lp(n||"").toLowerCase();if(p==="node"||p==="node.exe")return n;return"node"}import{mkdirSync as Mn,writeFileSync as J}from"node:fs";import{join as Y}from"node:path";import{fileURLToPath as va}from"node:url";var wp=["fbclid","gclid","ref","ref_src","ref_url","source","utm_campaign","utm_content","utm_medium","utm_source","utm_term"],$p=["dev.to","hashnode.com","medium.com","reddit.com","stackoverflow.com","stackexchange.com","substack.com"],_p=["arstechnica.com","techcrunch.com","theverge.com","venturebeat.com","wired.com","zdnet.com"],dn=["facebook.com","instagram.com","linkedin.com","pinterest.com","tiktok.com","twitter.com","x.com"];function H(t="",n=240){let f=String(t).replaceAll(/\s+/g," ").trim();if(f.length<=n)return f;let p=f.slice(0,n),g=p.lastIndexOf(" ");return g>0?`${p.slice(0,g)}...`:`${p}...`}function st(t=""){let n=H(t,180);if(!n)return"";if(/^https?:\/\//i.test(n))return"";let f=n.split(/\s+/).filter(Boolean).length,p=/[A-Z]/.test(n),g=/\d/.test(n);return n===n.toLowerCase()&&f<=4&&!p&&!g?"":n}function ut(t="",n=""){let f=st(t),p=st(n);if(!p)return f;if(!f)return p;let g=/^https?:\/\//i.test(f),r=/^https?:\/\//i.test(p);if(g&&!r)return p;if(!g&&r)return f;return p.length>f.length?p:f}function V(t){if(!t)return null;try{let n=new URL(t);if(!["http:","https:"].includes(n.protocol))return null;if(n.hash="",n.hostname=n.hostname.toLowerCase(),n.protocol==="https:"&&n.port==="443"||n.protocol==="http:"&&n.port==="80")n.port="";for(let g of[...n.searchParams.keys()]){let r=g.toLowerCase();if(wp.includes(r)||r.startsWith("utm_"))n.searchParams.delete(g)}n.searchParams.sort();let f=n.pathname.replace(/\/{1,10}$/,"")||"/";n.pathname=f;let p=n.toString();return f==="/"?p.replace(/\/$/,""):p}catch{return null}}function hp(t){try{return new URL(t).hostname.toLowerCase().replace(/^www\./,"")}catch{return""}}function it(t,n){return n.some((f)=>t===f||t.endsWith(`.${f}`))}function tn(t,n="",f=""){let p=n.toLowerCase(),g=f.toLowerCase();if(t==="github.com"||t==="gitlab.com")return"repo";if(t==="arxiv.org"||t==="doi.org"||t==="semanticscholar.org"||t.endsWith(".semanticscholar.org")||g.includes("/paper/")||g.includes("/pdf/"))return"academic";if(it(t,dn))return"social";if(it(t,$p))return"community";if(it(t,_p))return"news";if(t.startsWith("docs.")||t.startsWith("developer.")||t.startsWith("developers.")||t.startsWith("api.")||p.includes("documentation")||p.includes("docs")||p.includes("reference")||g.includes("/docs/")||g.includes("/reference/")||g.includes("/api/"))return"official-docs";if(t.startsWith("blog.")||g.includes("/blog/"))return"maintainer-blog";return"website"}function ep(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 Ep(t){let n=Object.values(t.perEngine||{}).map((f)=>f?.rank||99);return n.length?Math.min(...n):99}var Dp=["reddit.com","news.ycombinator.com","lobste.rs"];function ht(t){return t.smartScore*3+t.engineCount*5+ep(t.sourceType)*2+Math.max(0,7-Ep(t))}function bp(t){let n=t.toLowerCase(),f=[];if(n.includes("openai")||n.includes("gpt")||n.includes("chatgpt"))f.push("openai.com","platform.openai.com","help.openai.com");if(n.includes("anthropic")||n.includes("claude"))f.push("anthropic.com","docs.anthropic.com");if(n.includes("bun"))f.push("bun.sh","bun.com");if(n.includes("next.js")||n.includes("nextjs"))f.push("nextjs.org","vercel.com");if(n.includes("playwright"))f.push("playwright.dev");if(n.includes("supabase"))f.push("supabase.com","supabase.io");if(n.includes("prisma"))f.push("prisma.io");if(n.includes("tailwind"))f.push("tailwindcss.com");if(n.includes("vite"))f.push("vitejs.dev","vite.dev");if(n.includes("astro"))f.push("astro.build");if(n.includes("svelte"))f.push("svelte.dev");if(n.includes("solid"))f.push("solidjs.com");if(n.includes("vue")||n.includes("nuxt"))f.push("vuejs.org","nuxt.com");if(n.includes("react")||n.includes("react native"))f.push("react.dev","reactnative.dev");if(n.includes("angular"))f.push("angular.io","angular.dev");if(n.includes("node.js")||n.includes("nodejs"))f.push("nodejs.org","nodejs.dev","npmjs.com");if(/\bgo\b/.test(n)||n.includes("golang"))f.push("go.dev","golang.org","pkg.go.dev");if(n.includes("deno"))f.push("deno.land","deno.com");if(n.includes("fresh"))f.push("fresh.deno.dev");if(n.includes("typescript")||n.includes("ts"))f.push("typescriptlang.org");if(n.includes("python"))f.push("python.org","docs.python.org");if(n.includes("rust"))f.push("rust-lang.org","docs.rs","crates.io");if(n.includes("zig"))f.push("ziglang.org");if(n.includes("docker"))f.push("docker.com","docs.docker.com","hub.docker.com");if(n.includes("kubernetes")||n.includes("k8s"))f.push("kubernetes.io","k8s.io");if(n.includes("postgres")||n.includes("postgresql"))f.push("postgresql.org","neon.tech","supabase.com");if(n.includes("redis"))f.push("redis.io");if(n.includes("sqlite"))f.push("sqlite.org");if(n.includes("cloudflare"))f.push("developers.cloudflare.com","cloudflare.com");if(n.includes("vercel"))f.push("vercel.com","nextjs.org");if(n.includes("netlify"))f.push("netlify.com","docs.netlify.com");if(n.includes("stripe"))f.push("stripe.com","docs.stripe.com");if(n.includes("github"))f.push("github.com","docs.github.com");if(n.includes("gitlab"))f.push("gitlab.com","docs.gitlab.com");if(n.includes("aws"))f.push("aws.amazon.com","docs.aws.amazon.com");if(n.includes("azure"))f.push("azure.microsoft.com","learn.microsoft.com");if(n.includes("gcp")||n.includes("google cloud"))f.push("cloud.google.com","developers.google.com");if(n.includes("gemini")||n.includes("google ai"))f.push("ai.google.dev","developers.google.com");for(let p of dn){let g=p.replace(/\.com$/,"");if(n.includes(g))f.push(p)}return[...new Set(f)]}function cn(t,n){return t===n||t.endsWith(`.${n}`)}function At(t,n=""){let f=new Map,p=Object.keys(t||{}).filter(($)=>!$.startsWith("_")),g=bp(n);for(let $ of p){let _=t[$];if(!_?.sources)continue;for(let e=0;e<_.sources.length;e++){let E=_.sources[e],h=V(E.url);if(!h||h.length<10)continue;let b=st(E.title||""),D=hp(h),v=tn(D,b,h),P=0;if(g.some((I)=>cn(D,I)))P+=10;if(v==="official-docs")P+=3;let R=h.toLowerCase();if(/\/docs\/|\/documentation\/|\.dev\/|\/api\/|\/reference\//.test(R))P+=2;let y=g.some((I)=>cn(D,I));if(v==="social"&&!y)P-=20;if(g.length>0){if(it(D,Dp))P-=3;else if(v==="community"&&!it(D,["stackoverflow.com","stackexchange.com"]))P-=1}let j=f.get(h)||{id:"",canonicalUrl:h,displayUrl:E.url||h,domain:D,title:"",engines:[],engineCount:0,perEngine:{},sourceType:v,isOfficial:v==="official-docs",smartScore:0};if(j.title=ut(j.title,b),j.displayUrl=j.displayUrl||E.url||h,j.sourceType=j.sourceType||v,j.isOfficial=j.isOfficial||v==="official-docs",j.smartScore=Math.max(j.smartScore,P),!j.engines.includes($))j.engines.push($);j.perEngine[$]={rank:e+1,title:ut(j.perEngine[$]?.title||"",b)},f.set(h,j)}}let r=Array.from(f.values()).map(($)=>({...$,engineCount:$.engines.length})),a=r.filter(($)=>$.sourceType!=="social"),l=r.filter(($)=>$.sourceType==="social");return a.sort(($,_)=>{let e=ht(_)-ht($);if(e!==0)return e;return $.domain.localeCompare(_.domain)}),l.sort(($,_)=>{let e=ht(_)-ht($);if(e!==0)return e;return $.domain.localeCompare(_.domain)}),[...a,...l].slice(0,12).map(($,_)=>({...$,id:`S${_+1}`,title:$.title||$.domain||$.canonicalUrl}))}function Ft(t,n){let f=new Map(n.map((p)=>[p.id,p]));return t.map((p)=>{let g=f.get(p.id);if(!g)return p;let r=ut(p.title,g.title||"");return{...p,title:r||p.title,fetch:{attempted:!0,ok:!g.error&&g.contentChars>100,status:g.status||null,finalUrl:g.finalUrl||g.url||p.canonicalUrl,contentType:g.contentType||"",lastModified:g.lastModified||"",publishedTime:g.publishedTime||"",byline:g.byline||"",siteName:g.siteName||"",lang:g.lang||"",title:g.title||"",snippet:g.snippet||"",contentChars:g.contentChars||0,source:g.source||"unknown",duration:g.duration||0,error:g.error||""}}})}import{existsSync as Gt,mkdirSync as Cp,readFileSync as qn,writeFileSync as vp}from"node:fs";import{homedir as jp}from"node:os";import{join as sn}from"node:path";import{tmpdir as Pp}from"node:os";function Rp(t,n=9222){let f=Number.parseInt(String(t??""),10);return Number.isInteger(f)&&f>1024&&f<65535?f:n}var yp=Pp().replaceAll("\\","/"),hr=Rp(process.env.GREEDY_SEARCH_PORT),K=(process.env.GREEDY_SEARCH_PROFILE_DIR||process.env.CDP_PROFILE_DIR||`${yp}/greedysearch-chrome-profile`).replaceAll("\\","/"),er=`${K}/DevToolsActivePort`,Er=process.env.GREEDY_SEARCH_PID_FILE||`${K}/browser.pid`,Dr=process.env.CDP_PAGES_CACHE||`${K}/cdp-pages.json`,br=process.env.GREEDY_SEARCH_MODE_FILE||`${K}/browser-mode`,Cr=process.env.GREEDY_SEARCH_METADATA_FILE||`${K}/browser-metadata.json`,vr=process.env.GREEDY_SEARCH_LAUNCH_LOCK_FILE||`${K}/browser-launch.lock`,jr=process.env.GREEDY_SEARCH_ACTIVITY_FILE||`${K}/browser-last-activity`,Pr=(process.env.CDP_SOCKET_DIR||`${K}/cdp-sockets`).replaceAll("\\","/"),Rr=`${K}/visible-recovery.jsonl`,fn=sn(jp(),".dm"),nt=sn(fn,"greedyconfig"),pn=["perplexity","google","chatgpt","gemini"],gn="gemini";function Op(){try{if(Gt(nt)){let t=qn(nt,"utf8"),n=JSON.parse(t);if(Array.isArray(n.engines)&&n.engines.length>0&&n.engines.every((f)=>typeof f==="string")){let f=n.engines.filter((g)=>nn[g]),p=n.engines.filter((g)=>!nn[g]);if(p.length>0)process.stderr.write(`[greedysearch] Warning: ignoring unknown engine(s) in ${nt}: ${p.join(", ")}
|
|
399
|
+
[greedysearch] Available engines: ${Object.keys(nn).join(", ")}
|
|
400
|
+
`);if(f.length>0)return f;process.stderr.write(`[greedysearch] Warning: no valid engines in ${nt}, falling back to defaults: ${pn.join(", ")}
|
|
401
|
+
`)}}}catch{}return pn}function Np(){try{if(!Gt(fn))Cp(fn,{recursive:!0});if(!Gt(nt))vp(nt,JSON.stringify({engines:pn,synthesizer:gn},null,2)+`
|
|
402
|
+
`,"utf8")}catch{}}Np();var Tt=["gemini","chatgpt"];function ip(){try{if(Gt(nt)){let t=qn(nt,"utf8"),n=JSON.parse(t);if(typeof n.synthesizer==="string"){let f=n.synthesizer.toLowerCase();if(Tt.includes(f))return f;process.stderr.write(`[greedysearch] Warning: unknown synthesizer "${n.synthesizer}" in ${nt}
|
|
403
|
+
[greedysearch] Available synthesizers: ${Tt.join(", ")}
|
|
404
|
+
[greedysearch] Falling back to default: ${gn}
|
|
405
|
+
`)}}}catch{}return gn}var nn={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"},Pt=Op(),kt=Pt,yr=ip(),Or=Math.max(1,Number.parseInt(process.env.GREEDY_FETCH_CONCURRENCY||"5",10)||5);process.env.CDP_PROFILE_DIR=K;function Ap(t){let n="",f=!1,p=!1;for(let g of String(t)){if(p){n+=g,p=!1;continue}if(g==="\\"){n+=g,p=!0;continue}if(g==='"'){f=!f,n+=g;continue}if(f&&g===`
|
|
406
|
+
`)n+="\\n";else if(f&&g==="\r")n+="\\r";else if(f&&g==="\t")n+="\\t";else n+=g}return n}function ft(t){if(!t)return null;let n=String(t).trim(),f=n.indexOf("BEGIN_JSON"),p=n.indexOf("END_JSON");if(f!==-1&&p!==-1&&f<p)n=n.slice(f+10,p).trim();else{let l=n.indexOf("{");if(l>0)n=n.slice(l)}let g=[n,n.replace(/^```json\s*/i,"").replace(/^```\s*/i,"").replace(/```$/i,"").trim()],r=n.indexOf("{"),a=n.lastIndexOf("}");if(r!==-1&&a!==-1&&r<a)g.push(n.slice(r,a+1));for(let l of[...g]){let w=Ap(l);if(w!==l)g.push(w)}for(let l of g)try{return JSON.parse(l)}catch{}return null}import{spawn as Lp}from"node:child_process";import{existsSync as un}from"node:fs";import{dirname as Tp,join as Wt}from"node:path";import{fileURLToPath as kp}from"node:url";function tf(t){return kp(new URL(".",t)).replace(/^\/([A-Z]:)/,"$1")}function Hp(t,n=un){let f=new Set;for(let p of t.filter(Boolean)){if(f.has(p))continue;if(f.add(p),n(p))return p}return t.filter(Boolean).at(-1)}function nf(t,{moduleDir:n,entrypoint:f=process.argv[1],env:p=process.env,exists:g=un}={}){let r=f?Tp(f):null,a=p.GREEDY_SEARCH_EXTENSION_DIR?.trim()||null;return Hp([n?Wt(n,"..","..","extractors",t):null,n?Wt(n,"..","extractors",t):null,a?Wt(a,"extractors",t):null,r?Wt(r,"..","extractors",t):null],g)}var Yp=tf(import.meta.url),Ip={gemini:"gemini.mjs",chatgpt:"chatgpt.mjs"};function Mp(t="gemini"){let n=String(t||"gemini").toLowerCase();if(n==="gem")return"gemini";if(n==="gpt")return"chatgpt";return n}async function Fp(t,n,{tabPrefix:f=null,timeoutMs:p=180000,visible:g=null}={}){let r=Mp(t),a=Ip[r];if(!a||!Tt.includes(r))throw Error(`Unsupported synthesizer "${t}". Supported: ${Tt.join(", ")}`);return new Promise((l,w)=>{let $=nf(a,{moduleDir:Yp}),_=f?["--tab",String(f)]:[],e={...process.env,CDP_PROFILE_DIR:K};if(g!==!0)delete e.GREEDY_SEARCH_VISIBLE,delete e.GREEDY_SEARCH_ALWAYS_VISIBLE;else e.GREEDY_SEARCH_VISIBLE="1",e.GREEDY_SEARCH_ALWAYS_VISIBLE="1";let E=Lp(jt(),[$,"--stdin",..._],{stdio:["pipe","pipe","pipe"],env:e});E.stdin.write(n),E.stdin.end();let h="",b="";E.stdout.on("data",(v)=>h+=v),E.stderr.on("data",(v)=>b+=v);let D=setTimeout(()=>{E.kill(),w(Error(`${r} prompt timed out after ${p/1000}s`))},p);E.on("close",(v)=>{if(clearTimeout(D),v!==0){w(Error(b.trim()||`${r} extractor failed`));return}try{l(JSON.parse(h.trim()))}catch{w(Error(`bad JSON from ${r}: ${h.slice(0,100)}`))}})})}async function X(t,n={}){return Fp("gemini",t,n)}var Gp=30000;function ff(t,n,f,p){let g=Number.parseInt(String(t??""),10);if(!Number.isFinite(g))return p;return Math.min(f,Math.max(n,g))}async function pf(t){let n=["You are a research complexity classifier.","Classify the following query by research complexity.","","- simple: A narrow factual question (what is X, define X, how does X work)."," Answerable with 1-3 search queries and a short synthesis. No sub-questions.","- moderate: A focused comparison, recent change, or best-practice lookup."," Needs 2-4 angles but stays within one domain.","- complex: Multi-faceted survey, landscape analysis, or cross-domain investigation."," Benefits from parallel research directions and iterative deepening.","","Respond ONLY with JSON wrapped in BEGIN_JSON / END_JSON markers:","BEGIN_JSON",JSON.stringify({complexity:"simple",reasoning:"narrow factual question",suggestedBreadth:1,suggestedIterations:1,needsAcademicSources:!1},null,2),"END_JSON","","Query: "+t].join(`
|
|
407
|
+
`);try{let f=await X(n,{timeoutMs:Gp}),p=ft(f?.answer||"")||{},g=["simple","moderate","complex"].includes(p.complexity)?p.complexity:"moderate";return{complexity:g,reasoning:H(p.reasoning||"",200),suggestedBreadth:ff(p.suggestedBreadth,1,5,g==="simple"?1:3),suggestedIterations:ff(p.suggestedIterations,1,3,g==="simple"?1:2),needsAcademicSources:p.needsAcademicSources===!0}}catch(f){return process.stderr.write(`[greedysearch] Complexity classification failed, defaulting to moderate: ${f.message}
|
|
408
|
+
`),{complexity:"moderate",reasoning:"classification failed",suggestedBreadth:3,suggestedIterations:2,needsAcademicSources:!1}}}an();Xt();function sg(t){if(t<1000)return"0s";let n=Math.round(t/1000);if(n<60)return`${n}s`;let f=Math.floor(n/60),p=n%60;return`${f}m ${p}s`}function ug(t,n=20){let f=Math.round(t*n),p=n-f;return"["+"█".repeat(f)+"░".repeat(p)+"]"}function ot({totalActions:t=0,totalRounds:n=0,totalFetches:f=0,silent:p=!1}={}){let g=Date.now(),r=0,a=0,l=0,w=[],$=null,_=null,e=0,E=500;function h(P){if(w.push(P),w.length>5)w.shift()}function b(){if(w.length===0)return null;return w.reduce((P,R)=>P+R,0)/w.length}function D(P){let R=Date.now()-g,y=t+f+n,j=r+l+a,I=y>0?Math.min(1,j/y):0,M=ug(I),A=b(),k=Math.max(0,y-j),F=A?A*k:null,et=F?sg(F):"—",d=_?` ${_}`:"";return`${M} ${j}/${y} (${P}${d}, ETA ${et})`}function v(P){if(p)return;let R=Date.now();if(R-e<E&&P!=="done")return;e=R,process.stderr.write(`[greedysearch] ${D(P)}
|
|
409
|
+
`)}return{startRound(P){a=P-1},endRound(){a++,v("round")},startAction(P,R){$=Date.now(),_=`${P}:${(R||"").slice(0,40)}`,v(P)},endAction(){if($)h(Date.now()-$),$=null;r++,v("action")},startFetch(P){$=Date.now(),_=`fetch:${(P||"").slice(0,40)}`,v("fetch")},endFetch(P=!0){if($)h(Date.now()-$),$=null;l++,v(P?"fetch":"fetch-failed")},print(){v("progress")},finish(){v("done")},getElapsedMs(){return Date.now()-g}}}import{spawn as ta}from"node:child_process";import{join as na}from"node:path";import{fileURLToPath as fa}from"node:url";var pa=fa(new URL(".",import.meta.url)).replace(/^\/([A-Z]:)/,"$1"),ga=na(pa,"..","..","bin","search.mjs");function mf(t,n=1/0){let f=new Set,p=[];for(let g of t||[]){let r=H(String(g||""),1000);if(!r||f.has(r))continue;if(f.add(r),p.push(r),p.length>=n)break}return p}function aa(t){let n=String(t||"").trim();if(!n)return[];return[`${n} — definition and overview`,`${n} — how it works, mechanism, or key details`,`${n} — current usage, comparison, or best practices`]}function ra(t,n){let f=new Map;for(let p of t||[]){let g=p?.canonicalUrl||p?.finalUrl||p?.url;if(g)f.set(g,p)}for(let p of n||[]){let g=p?.canonicalUrl||p?.finalUrl||p?.url;if(!g)continue;if(f.has(g)){let r={...f.get(g),angles:[...f.get(g).angles||[f.get(g).query||""],p.query||""]};f.set(g,r)}else f.set(g,p)}return Array.from(f.values())}var la=Pt.join("|"),wa=new RegExp(`^\\[(${la})\\]`);function $a(t){return/^PROGRESS:/.test(t)||/^\[greedysearch\]/.test(t)||wa.test(t)||/^GreedySearch Chrome/.test(t)||/^Launching GreedySearch Chrome/.test(t)||/^Headless mode/.test(t)||/^Ready\.?$/.test(t)}async function _a(t,{locale:n=null,short:f=!0}={}){let p=[ga,"all","--inline","--stdin","--fast"];if(!f)p.push("--full");if(n)p.push("--locale",n);return new Promise((g,r)=>{let a=ta(jt(),p,{stdio:["pipe","pipe","pipe"],env:{...process.env,GREEDY_SEARCH_RESEARCH_CHILD:"1"}});a.stdin.write(t),a.stdin.end();let l="",w="",$="";a.stdout.on("data",(e)=>l+=e),a.stderr.on("data",(e)=>{w+=e,$+=e.toString();let E=$.split(`
|
|
410
|
+
`);$=E.pop()||"";for(let h of E)if($a(h))process.stderr.write(`${h}
|
|
411
|
+
`)});let _=setTimeout(()=>{a.kill(),r(Error(`research child search timed out for: ${t}`))},140000);a.on("close",(e)=>{if(clearTimeout(_),e!==0){r(Error(w.trim()||`search child exited with code ${e}`));return}try{g(JSON.parse(l.trim()))}catch{r(Error(`Invalid JSON from research child: ${l.slice(0,200)}`))}})})}function ha(t,n){let f=new Map;for(let p of n||[]){let g=V(p?.canonicalUrl||p?.finalUrl||p?.url);if(g&&p?.id)f.set(g,p.id)}return(t||[]).map((p,g)=>{let r=V(p?.finalUrl||p?.canonicalUrl||p?.url);return{...p,id:p?.id||f.get(r)||`F${g+1}`}})}function ea(t){let n=t.length,f=t.filter((p)=>p.status==="closed").length;return{total:n,closed:f,open:Math.max(0,n-f)}}async function Jf({query:t,locale:n=null,maxSources:f=5,qualityThreshold:p=8.5,writeBundle:g=process.env.GREEDY_RESEARCH_BUNDLE!=="0",researchOutDir:r=null}={}){let a=new Date().toISOString(),l=Date.now(),w=Ln(t),$=new Set;process.stderr.write(`[greedysearch] Simple research mode: single-pass for "${H(t,80)}"
|
|
412
|
+
`);let E=ot({totalActions:6,totalRounds:1,totalFetches:1,silent:process.env.GREEDY_RESEARCH_QUIET==="1"});E.startRound(1);let h=[],b=[],D=aa(t),v=[];E.startAction("search",`${D.length} angles in parallel`);let P=await Promise.allSettled(D.map((N)=>_a(N,{locale:n,short:!0})));for(let N=0;N<D.length;N++){let o=D[N],lt=P[N];if(E.endAction(),lt.status==="fulfilled"){let Z=lt.value;v.push({angle:o,result:Z});let T=At(Z,o);h=ra(h,T)}else process.stderr.write(`[greedysearch] Simple search angle "${o}" failed: ${lt.reason.message}
|
|
413
|
+
`)}if(process.stderr.write(`PROGRESS:research:simple:fetching
|
|
414
|
+
`),h.length>0)try{E.startFetch(`top ${Math.min(f,h.length)} sources`),b=await On(h,Math.min(f,h.length),8000,Math.min(3,f)),E.endFetch(!0),h=Ft(h,b)}catch(N){E.endFetch(!1),process.stderr.write(`[greedysearch] Source fetching failed: ${N.message}
|
|
415
|
+
`)}b=ha(b,h),process.stderr.write(`PROGRESS:research:simple:evidence
|
|
416
|
+
`);let R=[];try{let N=await Zf({query:t,questions:w,fetchedSources:b,extractedSourceKeys:$});R=N.evidence||[];for(let o of N.evidence){let lt=Array.isArray(o.answers)?o.answers:[];for(let T of lt){let $t=T?.id||T?.question;if($t){let q=w.find((xt)=>xt.id===$t);if(q){if(q.status="closed",q.closedRound=1,T.evidence)q.evidence=mf([...q.evidence||[],T.evidence],4)}}}let Z=Array.isArray(o.newQuestions)?o.newQuestions:[];for(let T of Z){let $t=H(String(T),320);if($t&&!w.some((q)=>q.question===$t))w.push({id:`Q${w.length+1}`,question:$t,status:"open",reason:"Discovered gap/follow-up",createdRound:1,evidence:[],sourceIds:[]})}}}catch(N){process.stderr.write(`[greedysearch] Evidence extraction failed: ${N.message}
|
|
417
|
+
`)}process.stderr.write(`PROGRESS:research:simple:synthesizing
|
|
418
|
+
`);let y={answer:"",agreement:{level:"mixed",summary:"Single-pass synthesis."},differences:[],caveats:[],claims:[],recommendedSources:h.slice(0,4).map((N)=>N.id),synthesized:!1};if(R.length>0)try{E.startAction("synth-evidence","from evidence");let N=await X(Tn(t,h,w,R),{timeoutMs:120000});E.endAction(),y={...y,...ft(N?.answer||"")||{}},y.synthesized=Array.isArray(y.claims)&&y.claims.length>0}catch(N){process.stderr.write(`[greedysearch] Evidence synthesis failed: ${N.message}
|
|
419
|
+
`)}if(!y.synthesized&&h.length>0)try{E.startAction("synth-final","fallback report");let N=await X(An(t,[{round:1,learnings:[],gaps:[],actions:[]}],h,w,R),{timeoutMs:120000});E.endAction(),y={...y,...ft(N?.answer||"")||{}},y.synthesized=Array.isArray(y.claims)&&y.claims.length>0}catch(N){process.stderr.write(`[greedysearch] Final synthesis failed: ${N.message}
|
|
420
|
+
`)}process.stderr.write(`PROGRESS:research:simple:audit
|
|
421
|
+
`);let j=kn(y.answer||"",h),I=await Hn(h,j);Yn(w,y,j);let M=mf(y.caveats||[]),A=Bt({sources:h,fetchedSources:b,synthesis:y,citationAudit:j,gaps:M,questions:w,rounds:[{round:1,actions:[],learnings:[],gaps:M}],qualityScore:y.synthesized?8:5,qualityThreshold:p,maxSources:f}),k=new Date().toISOString(),F=Date.now()-l,et={startedAt:a,finishedAt:k,durationMs:F,rounds:1,terminationReason:"simple_single_pass"},d=null,wt;if(g){process.stderr.write(`PROGRESS:research:simple:bundle
|
|
422
|
+
`);try{d=await In({query:t,rounds:[{round:1,actions:[],learnings:[],gaps:M,evidence:R}],sources:h,fetchedSources:b,evidenceItems:R,synthesis:y,citationAudit:j,citationUrls:I,floor:A,manifest:{...et,engines:kt,synthesizer:"gemini",actionsRun:1,searches:1,fetches:b.length,sourcesFetched:b.filter((N)=>N?.contentChars>100).length,engineFailures:[],floorMet:A.floorMet},allGaps:M,questions:w,outDir:r}),wt=d.sourceFiles,delete d.sourceFiles}catch(N){process.stderr.write(`[greedysearch] Research bundle write failed: ${N.message}
|
|
423
|
+
`),d={error:N.message||String(N)},wt=await Vt(b)}}else wt=await Vt(b);return process.stderr.write(`PROGRESS:research:done
|
|
424
|
+
`),E.endRound(),E.finish(),{query:t,_research:{mode:"simple",breadth:1,iterations:1,maxSources:f,rounds:[{round:1,actions:[],learnings:[],gaps:M,evidence:R}],learnings:[],gaps:M,evidence:R,questions:w,questionProgress:ea(w),qualityHistory:[y.synthesized?8:5],terminationReason:"simple_single_pass",qualityThreshold:p,floor:A,bundle:d,manifest:et},_citationAudit:j,_citationUrls:I,_sources:h,_fetchedSources:wt,_synthesis:y,_confidence:{sourcesCount:h.length,fetchedSourceSuccessRate:b.length>0?b.filter((N)=>N.contentChars>100).length/b.length:0,agreementLevel:y.agreement?.level||"mixed",floorMet:A.floorMet}}}var ja=va(new URL(".",import.meta.url)).replace(/^\/([A-Z]:)/,"$1"),Pa=Y(ja,"..","..","bin","search.mjs"),Ra=Y(process.cwd(),".pi","greedysearch-research"),ya=28000;function Oa(t){return String(t||"research").toLowerCase().replaceAll(/[^a-z0-9]+/g,"-").replaceAll(/^-|-$/g,"").slice(0,60)||"research"}function rt(t,n=1/0){let f=new Set,p=[];for(let g of t||[]){let r=H(String(g||""),1000);if(!r||f.has(r))continue;if(f.add(r),p.push(r),p.length>=n)break}return p}async function Na(...t){let{fetchMultipleSources:n}=await Promise.resolve().then(() => (Xt(),Nn));return n(...t)}async function mn(...t){let{writeSourcesToFiles:n}=await Promise.resolve().then(() => (an(),af));return n(...t)}function ia({breadth:t=3,iterations:n=2,maxSources:f}){let p=Fn(t,1,5,3),g=Fn(n,1,3,2),r=Fn(f??Math.max(5,p*g*2),3,12,8);return{breadth:p,iterations:g,maxSources:r}}function Fn(t,n,f,p){let g=Number.parseInt(String(t??""),10);if(!Number.isFinite(g))return p;return Math.min(f,Math.max(n,g))}function Aa(t,n,f,{expand:p=!0,includeOriginal:g=!0,exclude:r=[]}={}){let a=Array.isArray(t?.queries)?t.queries:[],l=[],w=new Set([...r].map(($)=>c($).toLowerCase()));for(let $ of a){let _=typeof $==="string"?$:$?.query,e=typeof $==="string"?"":$?.researchGoal||"";Gn(l,_,e,{exclude:w})}if(g)Gn(l,n,"Original user query",{prepend:!0,exclude:w});if(p){let $=[{query:`${n} official docs GitHub`,researchGoal:"Find primary project docs, repository details, and maintainer claims."},{query:`${n} benchmarks limitations compatibility`,researchGoal:"Validate performance claims and uncover unsupported APIs or caveats."},{query:`${n} alternatives comparison production use cases`,researchGoal:"Compare against conventional headless browsers and identify when to choose it."},{query:`${n} anti bot detection Cloudflare screenshots visual rendering`,researchGoal:"Check automation risks, rendering gaps, screenshots, and bot-detection behavior."}];for(let _ of $){if(l.length>=f)break;Gn(l,_.query,_.researchGoal,{exclude:w})}}return l.slice(0,f)}function Gn(t,n,f="",{prepend:p=!1,exclude:g=new Set}={}){if(!n||typeof n!=="string")return;let r=c(n);if(!r||g.has(r.toLowerCase())||t.some((l)=>l.query.toLowerCase()===r.toLowerCase()))return;let a={query:r,researchGoal:H(f,320)};if(p)t.unshift(a);else t.push(a)}function c(t){return ka(Ta(String(t)))}function Ta(t){let n="",f=0;while(f<t.length){let p=t.indexOf("[",f);if(p===-1){n+=t.slice(f);break}let g=t.indexOf("]",p+1);if(g===-1||t[g+1]!=="("||g===p+1){n+=t.slice(f,p+1),f=p+1;continue}let r=t.indexOf(")",g+2);if(r===-1){n+=t.slice(f,p+1),f=p+1;continue}let a=t.slice(g+2,r).trimStart();if(!a.startsWith("http://")&&!a.startsWith("https://")){n+=t.slice(f,p+1),f=p+1;continue}n+=t.slice(f,p),n+=t.slice(p+1,g),f=r+1}return n}function ka(t){let n="",f=!1;for(let p of t)if(p===" "||p==="\t"||p===`
|
|
425
|
+
`||p==="\r"){if(!f)n+=" ";f=!0}else n+=p,f=!1;return n.trim()}function Jn(t){return new Set(String(t).toLowerCase().normalize("NFD").replaceAll(/[\u0300-\u036f]/g,"").split(/[^\w]+/).filter((n)=>n.length>1))}function Kn(t,n){let f=Jn(t),p=Jn(n),g=new Set([...f,...p]).size;if(g===0)return 1;let r=0;for(let a of f)if(p.has(a))r++;return r/g}function zf(t,n,{threshold:f=0.75,roundIndex:p=0,originalQuery:g=null}={}){let r=c(t).toLowerCase();if(n.has(r))return!0;if(g&&p>0&&r===c(g).toLowerCase())return!0;for(let a of n)if(Kn(r,a)>=f)return!0;return!1}function Ha(t,n,f,p){let g=n.map((r)=>({queries:r.queries?.map((a)=>a.query||"")||[],learnings:r.learnings||[],gaps:r.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(g,null,2)}`,`Accumulated learnings: ${JSON.stringify(f.slice(0,12),null,2)}`,`Known gaps: ${JSON.stringify(p.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(`
|
|
426
|
+
`)}function La(t,n,f,p,g){let r=[],a=[{template:(l)=>`${l} official documentation`,label:"official docs"},{template:(l)=>`${l} GitHub issues discussions`,label:"community signals"},{template:(l)=>`${l} benchmarks performance comparison`,label:"benchmarks"},{template:(l)=>`${l} limitations risks caveats`,label:"limitations"},{template:(l)=>`${l} production deployment experience`,label:"production usage"},{template:(l)=>`${n} ${l} counter evidence`,label:"counter-evidence"}];for(let l=0;l<t.length&&r.length<p;l++){let w=t[l],$=a[l%a.length],_=$.template(w);if(!zf(_,f,{roundIndex:g}))r.push({query:_,researchGoal:`Gap-driven: ${w} (${$.label})`})}return r}async function Ya(t,n,f,p,g){try{let r=await X(Ha(t,n,f,p),{timeoutMs:120000}),a=Ht(r,{}),l=typeof a.score==="number"?Math.min(10,Math.max(0,a.score)):g.length>0?g[g.length-1]:5,w=Array.isArray(a.knowledgeGaps)?a.knowledgeGaps.map((E)=>String(E)).filter(Boolean).slice(0,6):[],$=Array.isArray(a.nextActions)?a.nextActions.slice(0,5):[],_=typeof a.shouldContinue==="boolean"?a.shouldContinue:l<8,e=a.terminationReason||null;return{score:l,coverage:a.coverage||{},knowledgeGaps:w,shouldContinue:_,nextActions:$,terminationReason:e||(l>=8.5?"quality_threshold":null),evaluationError:""}}catch(r){return process.stderr.write(`[greedysearch] Quality evaluation failed: ${r.message}
|
|
427
|
+
`),{score:g.length>0?g[g.length-1]:5,coverage:{},knowledgeGaps:[],shouldContinue:!0,nextActions:[],terminationReason:null,evaluationError:r.message}}}function Ia(t){let n={};for(let f of Object.keys(t||{}).filter((p)=>!p.startsWith("_"))){let p=t?.[f];if(!p)continue;n[f]=p.error?{status:"error",error:String(p.error)}:{status:"ok",answer:H(p.answer||"",1400),sources:Array.isArray(p.sources)?p.sources.slice(0,5).map((g)=>({title:H(g.title||"",160),url:g.url||""})):[]}}return n}function Ma(t,n,f=[],p=[],g=[]){let r=p.length>0?`
|
|
341
428
|
Known knowledge gaps to target:
|
|
342
|
-
${
|
|
343
|
-
`)}`:"",
|
|
429
|
+
${p.map((l)=>`- ${l}`).join(`
|
|
430
|
+
`)}`:"",a=g.length>0?`
|
|
344
431
|
Already fetched URLs (do not re-fetch):
|
|
345
|
-
${
|
|
346
|
-
`)}`:"";return["You are planning web research actions for a multi-engine search agent.","You can plan two types of actions:",' - "search": run a multi-engine SERP search query',' - "fetchUrl": directly fetch a specific URL (docs page, GitHub repo, specification, etc.)','Prefer "fetchUrl" when a specific primary source URL is known or obvious.','Use "search" for broad discovery or when specific URLs are unknown.',`Return at most ${
|
|
432
|
+
${g.map((l)=>`- ${l}`).join(`
|
|
433
|
+
`)}`:"";return["You are planning web research actions for a multi-engine search agent.","You can plan two types of actions:",' - "search": run a multi-engine SERP search query',' - "fetchUrl": directly fetch a specific URL (docs page, GitHub repo, specification, etc.)','Prefer "fetchUrl" when a specific primary source URL is known or obvious.','Use "search" for broad discovery or when specific URLs are unknown.',`Return at most ${n} actions.`,"Avoid near-duplicate search queries and already-fetched URLs.","",`User topic: ${t}`,f.length?`
|
|
347
434
|
Prior learnings to build on:
|
|
348
|
-
${
|
|
349
|
-
`)}`:"",
|
|
350
|
-
`)}function
|
|
351
|
-
`)}
|
|
352
|
-
`)}function
|
|
353
|
-
`)}function
|
|
354
|
-
`)}async function
|
|
355
|
-
`)
|
|
356
|
-
`)});let
|
|
357
|
-
`)
|
|
358
|
-
`)
|
|
359
|
-
`),"
|
|
360
|
-
`)
|
|
361
|
-
`),
|
|
435
|
+
${f.map((l)=>`- ${l}`).join(`
|
|
436
|
+
`)}`:"",r,a,"","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(`
|
|
437
|
+
`)}function Sf(t){if(!t||typeof t!=="object")return null;let n=t.type,f=H(t.researchGoal||"",320);if(n==="search"){if(t.query==null)return null;let p=c(t.query);return p?{type:"search",query:p,researchGoal:f}:null}if(n==="fetchUrl"){if(t.url==null)return null;let p=V(t.url);return p?{type:"fetchUrl",url:p,researchGoal:f}:null}return null}async function Fa(t,{locale:n=null,short:f=!0,usedQueries:p,usedUrls:g,maxChars:r=8000}={}){if(t.type==="search"){let a=c(t.query).toLowerCase();p.add(a);try{let l=await oa(t.query,{locale:n,short:f}),w=At(l,t.query);return{ok:!0,action:t,result:l,sources:w}}catch(l){return{ok:!1,action:t,error:l.message,sources:[]}}}if(t.type==="fetchUrl"){let a=V(t.url);if(g.has(a))return{ok:!1,action:t,error:`URL already fetched: ${a}`,sources:[]};try{let l=await Ga(a,r);g.add(a);let w=Wa(a),$={id:"",canonicalUrl:l.finalUrl||a,displayUrl:l.url||a,domain:w,title:l.title||a,engines:["fetch"],engineCount:1,perEngine:{},sourceType:tn(w,l.title||"",l.finalUrl||a),isOfficial:!1,smartScore:0,fetch:{attempted:!0,ok:!l.error&&(l.contentChars||0)>100,status:l.status||null,finalUrl:l.finalUrl||a,content:l.content||"",contentChars:l.contentChars||0,snippet:l.snippet||"",error:l.error||""}};return{ok:!0,action:t,result:null,sources:[$],fetchResult:{id:$.id,url:a,finalUrl:l.finalUrl||a,title:l.title||"",content:l.content||"",contentChars:l.contentChars||0,snippet:l.snippet||"",status:l.status||null,error:l.error||"",source:l.source||"http",duration:l.duration||0}}}catch(l){return{ok:!1,action:t,error:l.message,sources:[]}}}return{ok:!1,action:t,error:`Unknown action type: ${t.type}`,sources:[]}}async function Ga(t,n){let{fetchSourceContent:f}=await Promise.resolve().then(() => (Xt(),Nn));return await f(t,n)}function Wa(t){try{return new URL(t).hostname.toLowerCase().replace(/^www\./,"")}catch{return""}}async function Va(t,n){let f=[],{parseGitHubUrl:p}=await Promise.resolve().then(() => (Uf(),Bf));for(let g of t){if(g.type!=="fetchUrl"){f.push(g);continue}let r=p(g.url);if(!r||r.type!=="root"){f.push(g);continue}let{owner:a,repo:l}=r,w=`https://github.com/${a}/${l}`;if(n.has(w))continue;let $=[w],_=[`${w}/blob/main/CONTRIBUTING.md`,`${w}/blob/master/CONTRIBUTING.md`,`${w}/blob/main/CHANGELOG.md`,`${w}/blob/master/CHANGELOG.md`,`${w}/blob/main/docs/README.md`];for(let e of _){if($.length>=3)break;if(!n.has(e))$.push(e)}for(let e of $)f.push({type:"fetchUrl",url:e,researchGoal:g.researchGoal||`Fetch GitHub content for ${a}/${l}`})}return f}function ma(t,n){let f=ft(t?.answer||"")||{},p=Array.isArray(f?.actions)?f.actions:[],g=[];for(let r of p){let a=Sf(r);if(a&&g.length<n)g.push(a)}return g}function Ja(t){return(t||[]).map((n)=>({type:"search",query:typeof n==="string"?n:n.query,researchGoal:typeof n==="string"?"":n.researchGoal||""})).filter((n)=>n.query)}function yt(t){return V(t?.finalUrl||t?.canonicalUrl||t?.url||"")||t?.id||""}function cf(t,n){let f=Array.isArray(t?.extractions)?t.extractions:[],p=new Map,g=new Map;for(let r of n||[]){if(r?.id)g.set(String(r.id),r);let a=yt(r);if(a)p.set(a,r)}return f.map((r)=>{let a=g.get(String(r?.sourceId||""))||p.get(V(r?.url||"")||""),l=String(r?.sourceId||a?.id||""),w=V(r?.url||a?.finalUrl||a?.url||""),$=Array.isArray(r?.answers)?r.answers.map((_)=>({id:String(_?.id||""),evidence:H(_?.evidence||"",500),sourceIds:[l].filter(Boolean)})).filter((_)=>_.id):[];return{sourceId:l,url:w,title:a?.title||r?.title||"",rational:H(r?.rational||"",700),evidence:H(r?.evidence||"",1600),summary:H(r?.summary||"",700),answers:$,newQuestions:rt(r?.newQuestions||[],6)}}).filter((r)=>r.sourceId||r.url||r.summary||r.evidence)}function Za(t,n,f,p=new Set){let g=(n||[]).filter((a)=>a.status!=="closed").slice(0,12).map((a)=>({id:a.id,question:a.question})),r=(f||[]).filter((a)=>a?.content||a?.snippet).filter((a)=>!p.has(yt(a))).slice(0,6).map((a,l)=>({id:a.id||`F${l+1}`,title:a.title||"",url:a.finalUrl||a.url||a.canonicalUrl||"",content:H(a.content||a.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(g,null,2)}`,`Fetched sources: ${JSON.stringify(r,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(`
|
|
438
|
+
`)}async function Zf({query:t,questions:n,fetchedSources:f,extractedSourceKeys:p}){let g=(f||[]).filter((r)=>(r?.content||r?.snippet)&&!p.has(yt(r)));if(g.length===0)return{evidence:[],error:""};try{let r=await X(Za(t,n,g,p),{timeoutMs:120000}),a=Ht(r,{extractions:[]}),l=cf(a,g);for(let w of g){let $=yt(w);if($)p.add($)}return{evidence:l,error:""}}catch(r){return{evidence:[],error:r.message||String(r)}}}function Ka(t,n,f,p,g,r,a=[]){let l=(n||[]).filter((R)=>R.status!=="closed").slice(0,12).map((R)=>({id:R.id,question:R.question})),w=(g||[]).filter((R)=>R?.content||R?.snippet),$=(r||[]).filter((R)=>R?.content||R?.snippet),_=({extractionCount:R,extractionLimit:y,learningCount:j,learningLimit:I})=>{let M=w.slice(0,R).map((k,F)=>({id:k.id||`F${F+1}`,title:k.title||"",url:k.finalUrl||k.url||k.canonicalUrl||"",content:H(k.content||k.snippet||"",y)})),A=$.slice(0,j).map((k,F)=>({id:`F${F+1}`,title:k.title||"",url:k.finalUrl||k.url||"",snippet:H(k.content||k.snippet||"",I)}));return["You are doing two combined research tasks for one round of an iterative research run. Perform BOTH tasks and return a single combined JSON object.","","TASK A — Goal-based evidence extraction:","For each source under 'Sources for evidence extraction', 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.","","TASK B — Compact research-state learning extraction:","Using the round queries, question ledger, extracted source evidence, engine summaries, and fetched source snippets below, 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}`,`Open question ledger: ${JSON.stringify(l)}`,`Round queries: ${JSON.stringify(f)}`,`Question ledger: ${JSON.stringify(n)}`,`Extracted source evidence so far: ${JSON.stringify(a.slice(-12))}`,`Engine summaries: ${JSON.stringify(p)}`,`Sources for evidence extraction (Task A): ${JSON.stringify(M)}`,`Fetched source snippets (Task B context): ${JSON.stringify(A)}`,"","Respond ONLY with JSON wrapped in BEGIN_JSON / END_JSON markers, combining both tasks into one object:","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"]}],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(`
|
|
439
|
+
`)},e=Math.min(4,w.length),E=3000,h=Math.min(6,$.length),b=2000,D=_({extractionCount:e,extractionLimit:E,learningCount:h,learningLimit:b}),v=!1,P=600;while(D.length>ya){if(E>P||b>P)E=Math.max(P,Math.floor(E/2)),b=Math.max(P,Math.floor(b/2));else if(h>1)h-=1;else if(e>1)e-=1;else if(E>1||b>1)E=Math.max(1,Math.floor(E/2)),b=Math.max(1,Math.floor(b/2));else throw Error(`[greedysearch] evidence/learning prompt exceeds Gemini input cap after source trimming: ${D.length} chars`);v=!0,D=_({extractionCount:e,extractionLimit:E,learningCount:h,learningLimit:b})}if(v)console.error(`[greedysearch] evidence/learning prompt trimmed to fit Gemini input cap: ${D.length} chars`);return D}async function Xa({query:t,questions:n,fetchedSources:f,extractedSourceKeys:p,roundQueries:g,searchSummaries:r,evidenceItems:a=[]}){let l=(f||[]).filter((E)=>(E?.content||E?.snippet)&&!p.has(yt(E))),w=[],$="",_={learnings:[],followUpQueries:[],gaps:[]},e="";try{let E=await X(Ka(t,n,g,r,l,f,a),{timeoutMs:180000}),h=Ht(E,{});w=cf(h,l);for(let b of l){let D=yt(b);if(D)p.add(D)}_={..._,...h}}catch(E){let h=E.message||String(E);$=h,e=h}return{evidence:w,evidenceError:$,learningPayload:_,learningError:e}}function An(t,n,f,p=[],g=[]){let r=n.flatMap((w)=>w.learnings||[]),a=n.flatMap((w)=>w.gaps||[]),l=f.slice(0,12).map((w)=>({id:w.id,title:w.title,domain:w.domain,url:w.canonicalUrl,type:w.sourceType,engines:w.engines,fetch:w.fetch?.attempted?{ok:w.fetch.ok,snippet:H(w.fetch.snippet||"",1200),publishedTime:w.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(r,null,2)}`,`Known gaps/caveats: ${JSON.stringify(a,null,2)}`,`Question ledger: ${JSON.stringify(p,null,2)}`,`Goal-based extracted evidence: ${JSON.stringify(g.slice(-20),null,2)}`,`Source registry: ${JSON.stringify(l,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(`
|
|
440
|
+
`)}function Tn(t,n=[],f=[],p=[]){let g=n.slice(0,12).map((w)=>({id:w.id,title:w.title,domain:w.domain,url:w.canonicalUrl,type:w.sourceType,engines:w.engines})),r=p.slice(-20),a=new Set;for(let w of r)for(let $ of w.answers||[])if($?.id)a.add($.id);let l=(f||[]).filter((w)=>w.status!=="closed").map((w)=>({id:w.id,question:w.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(r,null,2)}`,`Source registry: ${JSON.stringify(g,null,2)}`,`Questions already answered by the evidence: ${JSON.stringify(Array.from(a))}`,`Questions still open after this evidence: ${JSON.stringify(l)}`,"","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(`
|
|
441
|
+
`)}async function oa(t,{locale:n=null,short:f=!0}={}){let p=[Pa,"all","--inline","--stdin","--fast"];if(!f)p.push("--full");if(n)p.push("--locale",n);return new Promise((g,r)=>{let a=Ca(jt(),p,{stdio:["pipe","pipe","pipe"],env:{...process.env,GREEDY_SEARCH_RESEARCH_CHILD:"1"}});a.stdin.write(t),a.stdin.end();let l="",w="",$="";a.stdout.on("data",(e)=>l+=e),a.stderr.on("data",(e)=>{w+=e,$+=e.toString();let E=$.split(`
|
|
442
|
+
`);$=E.pop()||"";for(let h of E)if(xa(h))process.stderr.write(`${h}
|
|
443
|
+
`)});let _=setTimeout(()=>{a.kill(),r(Error(`research child search timed out for: ${t}`))},140000);a.on("close",(e)=>{if(clearTimeout(_),e!==0){r(Error(w.trim()||`search child exited with code ${e}`));return}try{g(JSON.parse(l.trim()))}catch{r(Error(`Invalid JSON from research child: ${l.slice(0,200)}`))}})})}function Ba(t){let n=new Map;for(let f of t.flat()){let p=V(f.canonicalUrl||f.url);if(!p)continue;let g=n.get(p);if(!g){n.set(p,{...f,canonicalUrl:p});continue}g.engines=[...new Set([...g.engines||[],...f.engines||[]])],g.engineCount=g.engines.length,g.smartScore=Math.max(g.smartScore||0,f.smartScore||0)}return Array.from(n.values()).sort((f,p)=>{let g=ht(p)-ht(f);if(g!==0)return g;return(f.domain||"").localeCompare(p.domain||"")}).slice(0,12).map((f,p)=>({...f,id:`S${p+1}`}))}var Ua=Pt.join("|"),Qa=new RegExp(`^\\[(${Ua})\\]`);function xa(t){return/^PROGRESS:/.test(t)||/^\[greedysearch\]/.test(t)||Qa.test(t)||/^GreedySearch Chrome/.test(t)||/^Launching GreedySearch Chrome/.test(t)||/^Headless mode/.test(t)||/^Ready\.?$/.test(t)}function Ht(t,n={}){return ft(t?.answer||"")||n}function kn(t,n){if(!t||!Array.isArray(n))return{cited:[],missing:[],unfetched:[],ok:!0};let f=/\b[SF](\d+)\b/g,p=new Set,g;while((g=f.exec(t))!==null)p.add(`S${g[1]}`),p.add(`F${g[1]}`);let r=new Map;for(let $ of n){let _=$?.id;if(_)r.set(_,$)}let a=Array.from(p),l=[],w=[];for(let $ of a){let _=r.get($);if(!_){let e=$.match(/^(S|F)(\d+)$/);if(e){let E=parseInt(e[2],10)-1;if(E>=0&&E<n.length){let h=n[E];if(h){if(!(h.fetch?.ok||h.content&&h.content.length>100||h.contentChars&&h.contentChars>100))w.push($);continue}}}l.push($)}else if(!(_.fetch?.ok||_.content&&_.content.length>100||_.contentChars&&_.contentChars>100))w.push($)}return{cited:a,missing:l,unfetched:w,ok:l.length===0}}async function za(t,{timeoutMs:n=6000,concurrency:f=4}={}){let p=Math.max(1,Math.floor(f||1)),g=(t||[]).filter((E)=>E?.id&&(E?.canonicalUrl||E?.finalUrl||E?.url));if(g.length===0)return{reachable:[],dead:[],skipped:[],ok:!0};let r=[],a=[],l=[],w=Array(g.length),$=0;async function _(){while(!0){let E=$++;if(E>=g.length)return;let h=g[E];try{let b=h.fetch?.finalUrl||h.canonicalUrl||h.finalUrl||h.url;if(!b){w[E]={id:h.id,url:"",status:"skipped"};continue}try{let D=new URL(b);if(D.protocol!=="http:"&&D.protocol!=="https:"){w[E]={id:h.id,url:b,status:"skipped"};continue}}catch{w[E]={id:h.id,url:b,status:"skipped"};continue}try{let D=new AbortController,v=setTimeout(()=>D.abort(),n);try{let P=await fetch(b,{method:"HEAD",redirect:"follow",signal:D.signal,headers:{"User-Agent":"Mozilla/5.0 (compatible; GreedySearch/2.0; +https://github.com/apmantza/greedysearch-dm)"}});clearTimeout(v);let R=P.status>=200&&P.status<400,y=[401,403,405,429].includes(P.status),j="dead";if(R)j="reachable";else if(y)j="skipped";w[E]={id:h.id,url:b,status:j,httpStatus:P.status,reason:y?"bot-protected-or-head-disallowed":void 0}}catch(P){clearTimeout(v),w[E]={id:h.id,url:b,status:"dead",error:P.name==="AbortError"?"timeout":P.message}}}catch(D){w[E]={id:h.id,url:b,status:"dead",error:D.message}}}catch(b){w[E]={id:"?",url:"",status:"dead",error:b?.message||"unknown"}}}}let e=Math.min(g.length,p);await Promise.all(Array.from({length:e},()=>_()));for(let E of w)if(E.status==="reachable")r.push(E);else if(E.status==="dead")a.push(E);else l.push(E);return{reachable:r,dead:a,skipped:l,ok:a.length===0}}async function Hn(t,n=null){process.stderr.write(`PROGRESS:research:check-urls
|
|
444
|
+
`);try{let f=new Set(n?.cited||[]),p=f.size?(t||[]).filter((r)=>f.has(r?.id)):t,g=await za(p,{timeoutMs:6000,concurrency:4});if(!g.ok)process.stderr.write(`[greedysearch] ${g.dead.length} dead citation URL(s) detected
|
|
445
|
+
`);return g}catch(f){return process.stderr.write(`[greedysearch] URL reachability check failed: ${f.message}
|
|
446
|
+
`),null}}function Bt({sources:t=[],fetchedSources:n=[],synthesis:f={},citationAudit:p=null,gaps:g=[],questions:r=[],rounds:a=[],qualityScore:l=0,qualityThreshold:w=8.5,maxSources:$=8,requireCitations:_=!0,requireQuestions:e=!0}={}){let E=n.filter((A)=>A?.fetch?.ok||(A?.contentChars||0)>100||String(A?.content||"").length>100),h=t.filter((A)=>["official-docs","repo","maintainer-blog","academic"].includes(String(A?.sourceType||""))),b=Array.isArray(f?.claims)?f.claims:[],D=p?p.cited?.length||0:0,v=Zn(r),P=(r||[]).filter((A)=>!A.createdRound||A.reason==="Original research question"),R=Zn(P),y=(a||[]).length,j=Math.min(4,Math.max(2,Number($)||8)),I=y<=1?Math.min(2,j):j,M={roundsRun:a.length>=1,fetchedSources:E.length>=I,primarySources:h.length>=1,qualityScore:l>=Math.min(w,8)||_&&b.length>0&&D>0,claimsExtracted:!_||b.length>0,citationsPresent:!_||D>0,citationsValid:!_||p?.ok===!0,unfetchedCitations:!_||(p?.unfetched||[]).length===0,requiredQuestionsClosed:!e||R.open===0};return{floorMet:Object.values(M).every(Boolean),checks:M,metrics:{fetchedOk:E.length,primarySources:h.length,claims:b.length,cited:D,gaps:g.length,openQuestions:v.open,closedQuestions:v.closed,totalQuestions:v.total,openRequiredQuestions:R.open,closedRequiredQuestions:R.closed,totalRequiredQuestions:R.total,qualityScore:l,minFetched:I}}}function Qf(t,n){let f=new Map;for(let p of n||[]){let g=V(p?.canonicalUrl||p?.finalUrl||p?.url);if(g&&p?.id)f.set(g,p.id)}return(t||[]).map((p,g)=>{let r=V(p?.finalUrl||p?.canonicalUrl||p?.url);return{...p,id:p?.id||f.get(r)||`F${g+1}`}})}function Ln(t){return[{id:"Q1",question:H(c(t),320),status:"open",reason:"Original research question",evidence:[],sourceIds:[]}]}function Sa(t){let n=0;for(let f of t||[]){let p=Number.parseInt(String(f.id||"").replace(/^Q/i,""),10);if(Number.isFinite(p))n=Math.max(n,p)}return`Q${n+1}`}function df(t,n){let f=c(n).toLowerCase();return(t||[]).find((p)=>p.question?.toLowerCase()===f||Kn(p.question||"",f)>=0.82)}function Wn(t,n,{reason:f="",round:p=null}={}){let g=H(c(n),320);if(!g)return null;let r=df(t,g);if(r)return r;let a={id:Sa(t),question:g,status:"open",reason:H(f,240),createdRound:p,evidence:[],sourceIds:[]};return t.push(a),a}function Qt(t,n,{evidence:f="",sourceIds:p=[],round:g=null}={}){let r=t.find((a)=>a.id===n)||df(t,n);if(!r)return null;if(r.status="closed",r.closedRound=r.closedRound||g,f)r.evidence=rt([...r.evidence||[],f],4);if(Array.isArray(p))r.sourceIds=rt([...r.sourceIds||[],...p],8);return r}function Zn(t){let n=t.length,f=t.filter((p)=>p.status==="closed").length;return{total:n,closed:f,open:Math.max(0,n-f)}}function Ut(t,{roundNumber:n,actions:f=[],learningPayload:p={}}={}){for(let w of f){let $=w?.action||w,_=$?.researchGoal&&$.researchGoal!=="Original user query"?$.researchGoal:$?.query||$?.url||"";if(_)Wn(t,_,{reason:"Planned research action",round:n})}let g=5,r=t.filter((w)=>w.status==="open"&&w.reason==="Discovered gap/follow-up");if(r.length>g){let w=r.sort(($,_)=>($.createdRound||0)-(_.createdRound||0)).slice(0,r.length-g);for(let $ of w)$.status="resolved",$.closedRound=n,$.evidence=rt([...$.evidence||[],"Auto-resolved to cap open-question ledger"],4)}let a=Array.isArray(p.answeredQuestions)?p.answeredQuestions:[];for(let w of a){if(typeof w==="string"){Qt(t,w,{round:n});continue}let $=w?.id||w?.question;if(!$&&w?.question){let _=Wn(t,w.question,{reason:"Answered during learning extraction",round:n});if(_)Qt(t,_.id,{round:n});continue}Qt(t,$,{evidence:w?.evidence||w?.answer||"",sourceIds:Array.isArray(w?.sourceIds)?w.sourceIds:[],round:n})}let l=Array.isArray(p.newQuestions)?p.newQuestions:[];for(let w of l)Wn(t,w,{reason:"Discovered gap/follow-up",round:n});return t}function ca(t,n){if(!Array.isArray(t)||t.length===0)return[];let f=["arxiv.org","semanticscholar.org","doi.org"],p=new Set,g=[];for(let r of t){let a=r?.canonicalUrl||r?.finalUrl||r?.url||"";if(!a)continue;let l="";try{l=new URL(a).hostname.toLowerCase().replace(/^www\./,"")}catch{continue}if(!f.some(($)=>l===$||l.endsWith(`.${$}`)))continue;if(n.has(a)||p.has(a))continue;p.add(a);let w=a.includes("/pdf/")?a.replace(/\/pdf\//,"/html/").replace(/\.pdf$/i,""):a;g.push({url:w,label:r?.title||r?.id||l})}return g.slice(0,2)}function Yn(t,n,f){if(!n?.answer||f?.ok!==!0)return t;let p=Array.isArray(n.claims)?n.claims:[],g=Array.isArray(f.cited)?f.cited:[];if(p.length===0||g.length===0)return t;for(let r of t){if(r.status==="closed")continue;let a=null,l=0;for(let w of p){let $=Kn(r.question||"",w.claim||"");if($>l)l=$,a=w}if(r.id==="Q1"||l>=0.18)Qt(t,r.id,{evidence:a?.claim||"Answered in final cited synthesis",sourceIds:Array.isArray(a?.sourceIds)?a.sourceIds:g.slice(0,4)})}return t}function da(t){if(!t.length)return"No tracked questions.";return t.map((n)=>{let f=n.sourceIds?.length?` (${n.sourceIds.join(", ")})`:"";return`- [${n.status==="closed"?"x":" "}] ${n.id}: ${n.question}${f}`}).join(`
|
|
447
|
+
`)}function Vn(t,n="None recorded."){let f=rt(t);return f.length?f.map((p)=>`- ${p}`).join(`
|
|
448
|
+
`):n}function qa(t,{query:n,rounds:f,sources:p,fetchedSources:g,citationAudit:r,citationUrls:a,floor:l,manifest:w}){let $=(g||[]).filter((D)=>D?.contentChars>100||D?.fetch?.ok),_=(p||[]).filter((D)=>["official-docs","repo","maintainer-blog","academic"].includes(String(D?.sourceType||""))),e=new Set(r?.cited||[]),E=(p||[]).filter((D)=>e.has(D?.id)),h=[`# Provenance: ${n}`,"",`- **Date:** ${w?.startedAt||new Date().toISOString()}`,`- **Duration:** ${w?.durationMs?`${(w.durationMs/1000).toFixed(1)}s`:"unknown"}`,`- **Mode:** ${w?.terminationReason==="simple_single_pass"?"simple (single-pass)":"iterative"}`,`- **Rounds:** ${w?.rounds||f?.length||1}`,"","## Sources","",`- **Consulted:** ${p?.length||0}`,`- **Fetched successfully:** ${$.length}`,`- **Primary sources:** ${_.length}`,`- **Cited in report:** ${E.length}`,""];if(E.length>0){h.push("### Cited sources","");for(let D of E){let v=D.canonicalUrl||D.finalUrl||D.url||"",P=D.fetch?.ok?"✓":"✗";h.push(`- **${D.id}:** [${D.title||v}](${v}) (${D.sourceType||"unknown"}, fetched: ${P})`)}h.push("")}if(a&&(a.reachable.length>0||a.dead.length>0)){if(h.push("## URL reachability",""),a.dead.length>0){h.push(""),h.push("**Dead links:**");for(let D of a.dead)h.push(`- ${D.id}: ${D.url} (${D.httpStatus||D.error||"unknown"})`)}if(a.reachable.length>0)h.push(""),h.push(`**Reachable:** ${a.reachable.length}/${a.reachable.length+a.dead.length}`);h.push("")}let b=!r?"NOT CHECKED":r.ok&&(a?.ok??!0)?"PASS":r.ok===!1?"FAIL (missing citations)":"FAIL (dead links)";if(h.push("## Verification","",`- **Citations:** ${r?.ok?"PASS":`FAIL — missing: ${(r?.missing||[]).join(", ")}`}`,`- **URL reachability:** ${a?a.ok?"PASS":`FAIL — ${a.dead.length} dead`:"SKIPPED"}`,`- **Floor:** ${l?.floorMet?"PASS":"PARTIAL"}`,`- **Overall:** ${b}`,""),l?.checks){h.push("## Floor checks","");for(let[D,v]of Object.entries(l.checks))h.push(`- [${v?"x":" "}] ${D}`);h.push("")}J(Y(t,"provenance.md"),h.join(`
|
|
449
|
+
`),"utf8")}async function In({query:t,rounds:n,sources:f,fetchedSources:p,evidenceItems:g=[],synthesis:r,citationAudit:a,floor:l,manifest:w,allGaps:$=[],questions:_=[],citationUrls:e=null,outDir:E=null}){let h=new Date().toISOString().replaceAll(/[:.]/g,"-").slice(0,19),b=E||Y(Ra,`${h}_${Oa(t)}`),D=Y(b,"reports"),v=Y(b,"sources"),P=Y(b,"data");Mn(D,{recursive:!0}),Mn(v,{recursive:!0}),Mn(P,{recursive:!0});let R=await mn(p,v),y=rt([...$,...n.flatMap((j)=>j.gaps||[])]);J(Y(b,"STATUS.md"),[l.floorMet?"STATUS: DONE":"STATUS: PARTIAL","",`Query: ${t}`,`Stop reason: ${w.terminationReason||"max_rounds"}`,"","## Deterministic floor checks",...Object.entries(l.checks).map(([j,I])=>`- [${I?"x":" "}] ${j}`),"","## Questions",da(_),"","## Open gaps",Vn(y),""].join(`
|
|
450
|
+
`),"utf8"),J(Y(b,"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","- `provenance.md` — human-readable run metadata and verification","- `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(`
|
|
451
|
+
`),"utf8"),J(Y(D,"SUMMARY.md"),String(r.answer||""),"utf8"),J(Y(D,"CLAIMS.md"),["# Key claims","",...Array.isArray(r.claims)&&r.claims.length?r.claims.map((j)=>{let I=Array.isArray(j.sourceIds)?j.sourceIds.join(", "):"";return`- ${j.claim||""} (${j.support||"support unknown"}${I?`; ${I}`:""})`}):["No structured claims were extracted."],""].join(`
|
|
452
|
+
`),"utf8"),J(Y(D,"EVIDENCE.md"),["# Extracted evidence","",...g.length?g.map((j)=>[`## ${j.sourceId||j.url||"Source"}`,j.url?`<${j.url}>`:"",j.rational?`**Rational:** ${j.rational}`:"",j.evidence?`**Evidence:** ${j.evidence}`:"",j.summary?`**Summary:** ${j.summary}`:"",""].filter(Boolean).join(`
|
|
362
453
|
`)):["No goal-based evidence was extracted."],""].join(`
|
|
363
|
-
`),"utf8"),
|
|
364
|
-
`),"utf8"),
|
|
365
|
-
`),"utf8")
|
|
366
|
-
`)
|
|
367
|
-
`)
|
|
368
|
-
`)
|
|
369
|
-
`)
|
|
370
|
-
`);
|
|
371
|
-
`)
|
|
372
|
-
`),process.stderr.write(`[greedysearch]
|
|
373
|
-
`);
|
|
374
|
-
`)}
|
|
375
|
-
`)
|
|
376
|
-
`)
|
|
377
|
-
`)
|
|
378
|
-
`)
|
|
379
|
-
`)}let
|
|
380
|
-
`)
|
|
381
|
-
`),
|
|
382
|
-
`);
|
|
383
|
-
`)
|
|
384
|
-
`);let
|
|
385
|
-
`)
|
|
386
|
-
`),
|
|
387
|
-
`)
|
|
388
|
-
`)
|
|
389
|
-
`)
|
|
390
|
-
`)
|
|
391
|
-
`)
|
|
454
|
+
`),"utf8"),J(Y(D,"GAPS.md"),["# Gaps and caveats","","## Caveats",Vn(r.caveats||[]),"","## Research gaps",Vn(y),""].join(`
|
|
455
|
+
`),"utf8"),J(Y(P,"manifest.json"),JSON.stringify({...w,floor:l,citationAudit:a},null,2),"utf8"),J(Y(P,"rounds.json"),JSON.stringify(n,null,2),"utf8"),J(Y(P,"sources.json"),JSON.stringify(f,null,2),"utf8"),J(Y(P,"questions.json"),JSON.stringify(_,null,2),"utf8"),J(Y(P,"evidence.json"),JSON.stringify(g,null,2),"utf8"),J(Y(v,"index.md"),["# Source index","",...R.map((j)=>{let I=j.title||j.url,M=j.finalUrl||j.url,A=j.contentPath?` — ${j.contentPath}`:"";return`- ${j.id||"?"}: [${I}](${M})${A}`}),""].join(`
|
|
456
|
+
`),"utf8");try{qa(b,{query:t,rounds:n,sources:f,fetchedSources:p,citationAudit:a,citationUrls:e,floor:l,manifest:w})}catch(j){process.stderr.write(`[greedysearch] Provenance sidecar write failed (non-critical): ${j.message}
|
|
457
|
+
`)}return{dir:b,statusPath:Y(b,"STATUS.md"),summaryPath:Y(D,"SUMMARY.md"),manifestPath:Y(P,"manifest.json"),provenancePath:Y(b,"provenance.md"),sourceCount:R.length,sourceFiles:R}}async function L0({query:t,breadth:n,iterations:f,maxSources:p,locale:g=null,short:r=!1,qualityThreshold:a=8.5,writeBundle:l=process.env.GREEDY_RESEARCH_BUNDLE!=="0",researchOutDir:w=null}={}){let $=ia({breadth:n,iterations:f,maxSources:p}),_=n!==void 0&&n!==null,e=f!==void 0&&f!==null;if(!_&&!e)try{let O=await pf(t);if(process.stderr.write(`[greedysearch] Complexity: ${O.complexity} (${O.reasoning})
|
|
458
|
+
`),O.complexity==="simple")return process.stderr.write(`[greedysearch] Simple query detected — using fast single-pass path
|
|
459
|
+
`),Jf({query:t,locale:g,maxSources:Math.min(p??5,5),qualityThreshold:a,writeBundle:l,researchOutDir:w});if(!_)$.breadth=O.suggestedBreadth;if(!e)$.iterations=O.suggestedIterations}catch(O){process.stderr.write(`[greedysearch] Scale classification failed, using defaults: ${O.message}
|
|
460
|
+
`)}let h=[],b=[],D=[],v=Ln(t),P=null,R=[],y=[],j=[],I=new Set,M=new Set,A=new Set,k=[],F="max_rounds",et=new Date().toISOString(),d=Date.now(),wt=0,N=0,o=0,lt=[],Z=ot({totalActions:$.iterations*$.breadth,totalRounds:$.iterations,totalFetches:$.iterations,silent:process.env.GREEDY_RESEARCH_QUIET==="1"});Z.startRound(1),process.stderr.write(`[greedysearch] Research mode: breadth ${$.breadth}, iterations ${$.iterations}, qualityThreshold ${a}, engines ${kt.join(",")}, synthesizer gemini
|
|
461
|
+
`);for(let O=0;O<$.iterations;O++){let L=O+1,s=Math.max(1,Math.ceil($.breadth/2**O));if(process.stderr.write(`PROGRESS:research:round-${L}:planning
|
|
462
|
+
`),!P)try{let C=await X(Ma(t,s,b,D,[...A]),{timeoutMs:120000}),i=ma(C,s);if(O===0)i.unshift({type:"search",query:t,researchGoal:"Original user query"});i=await Va(i,A),P=i}catch(C){process.stderr.write(`[greedysearch] Action planning failed, using fallback queries: ${C.message}
|
|
463
|
+
`);let i=Aa(null,t,s,{includeOriginal:O===0,exclude:M});P=Ja(i)}let Q=(P||[]).filter((C)=>{if(C.type==="search"){let i=!zf(C.query,M,{roundIndex:O,originalQuery:t});if(!i)process.stderr.write(`[greedysearch] Novelty gate rejected search: ${C.query}
|
|
464
|
+
`);return i}if(C.type==="fetchUrl"){let i=!A.has(C.url);if(!i)process.stderr.write(`[greedysearch] Novelty gate rejected fetch: ${C.url}
|
|
465
|
+
`);return i}return!1}).slice(0,s),Bn=ca(R,A);if(!Q.some((C)=>C.type==="fetchUrl")&&Bn.length>0){let C=Bn[0];Q.push({type:"fetchUrl",url:C.url,researchGoal:`Direct fetch of known academic source: ${C.label||C.url}`}),process.stderr.write(`[greedysearch] Forced fetchUrl for academic source: ${C.url}
|
|
466
|
+
`)}let Un=Array(Q.length),sf=Math.min(3,Q.length),uf=0;async function tp(){while(!0){let C=uf++;if(C>=Q.length)return;let i=Q[C];process.stderr.write(`PROGRESS:research:round-${L}:action-${C+1}/${Q.length}
|
|
467
|
+
`),process.stderr.write(`[greedysearch] Action ${C+1}/${Q.length} [${i.type}]: ${(i.query||i.url).slice(0,80)}
|
|
468
|
+
`),Z.startAction(i.type,(i.query||i.url||"").slice(0,60));let W=await Fa(i,{locale:g,short:r,usedQueries:M,usedUrls:A,maxChars:8000});Z.endAction(),Un[C]=W}}await Promise.all(Array.from({length:sf},()=>tp()));let Dt=[];for(let C=0;C<Q.length;C++){let i=Q[C],W=Un[C];if(Dt.push(W),wt++,i.type==="search")N++;if(i.type==="fetchUrl")o++,Z.endFetch(W.ok);if(!W.ok)lt.push({round:L,type:i.type,target:i.query||i.url,error:W.error}),process.stderr.write(`[greedysearch] Action failed: ${W.error}
|
|
469
|
+
`)}let Qn=Dt.filter((C)=>C.action.type==="search"),xn=Dt.filter((C)=>C.action.type==="fetchUrl");Ut(v,{roundNumber:L,actions:Dt}),R=Ba([R,Qn.flatMap((C)=>C.sources||[]),xn.flatMap((C)=>C.sources||[])]);for(let C of xn)if(C.fetchResult)y.push(C.fetchResult);y=xf(y);let zt=Math.max(0,$.maxSources-y.filter((C)=>C?.content||C?.contentChars>100).length);if(zt>0&&R.length>0){process.stderr.write(`PROGRESS:research:round-${L}:fetching
|
|
470
|
+
`);let C=new Set,i=(tt)=>{try{return V(tt||"")}catch{return""}};for(let tt of y)for(let It of[tt?.url,tt?.finalUrl,tt?.canonicalUrl]){let vt=i(It);if(vt)C.add(vt)}let W=R.filter((tt)=>{let It=[tt?.canonicalUrl,tt?.finalUrl,tt?.url].map((vt)=>i(vt)).filter(Boolean);return It.length>0&&It.every((vt)=>!C.has(vt))}),fp=await Na(W,Math.min(zt,W.length),8000,Math.min(3,zt||1));y=xf([...y,...fp]),R=Ft(R,y)}y=Qf(y,R);let np=Dt.map((C)=>({query:C.action.query||C.action.url||"",researchGoal:C.action.researchGoal||""}));process.stderr.write(`PROGRESS:research:round-${L}:evidence
|
|
471
|
+
`),process.stderr.write(`PROGRESS:research:round-${L}:learning
|
|
472
|
+
`);let Yt=await Xa({query:t,questions:v,fetchedSources:y,extractedSourceKeys:I,roundQueries:np,searchSummaries:Qn.map((C)=>({query:C.action.query,researchGoal:C.action.researchGoal,error:C.error||"",engines:Ia(C.result)})),evidenceItems:j}),bt={evidence:Yt.evidence,error:Yt.evidenceError};if(bt.error)process.stderr.write(`[greedysearch] Evidence extraction failed: ${bt.error}
|
|
473
|
+
`);j=[...j,...bt.evidence];for(let C of bt.evidence)Ut(v,{roundNumber:L,learningPayload:{answeredQuestions:C.answers||[],newQuestions:C.newQuestions||[]}});let{learningPayload:Ct,learningError:St}=Yt;if(St)process.stderr.write(`[greedysearch] Learning extraction failed: ${St}
|
|
474
|
+
`);let zn=Array.isArray(Ct.learnings)?Ct.learnings.map((C)=>String(C)).filter(Boolean).slice(0,8):[],ct=Array.isArray(Ct.gaps)?Ct.gaps.map((C)=>String(C)).filter(Boolean).slice(0,6):[];if(b=rt([...b,...zn]),D=rt([...D,...ct]),Ut(v,{roundNumber:L,actions:[],learningPayload:Ct,gaps:ct}),h.push({round:L,actions:Dt.map((C)=>({type:C.action.type,query:C.action.query||"",url:C.action.url||"",researchGoal:C.action.researchGoal||"",error:C.error||"",sourceCount:C.sources?.length||0})),learnings:zn,gaps:ct,evidence:bt.evidence,evidenceError:bt.error,learningError:St}),process.stderr.write(`PROGRESS:research:round-${L}:evaluating
|
|
475
|
+
`),Z.endRound(),L<$.iterations)Z.startRound(L+1);let m=L===$.iterations?{score:k.length>0?k[k.length-1]:5,coverage:{},knowledgeGaps:[],shouldContinue:!1,nextActions:[],terminationReason:null,evaluationError:""}:await Ya(t,h,b,D,k);k.push(m.score),D=rt([...D,...m.knowledgeGaps||[]]),Ut(v,{roundNumber:L,gaps:m.knowledgeGaps||[]});let Sn=Bt({sources:R,fetchedSources:y,gaps:D,questions:v,rounds:h,qualityScore:m.score,qualityThreshold:a,maxSources:$.maxSources,requireCitations:!1,requireQuestions:!1});if(process.stderr.write(`[greedysearch] Quality score round ${L}: ${m.score.toFixed(1)} (shouldContinue: ${m.shouldContinue}, floor: ${Sn.floorMet})
|
|
476
|
+
`),m.score>=a&&Sn.floorMet&&(!m.shouldContinue||m.terminationReason==="quality_threshold")){F=m.terminationReason||"quality_threshold",process.stderr.write(`[greedysearch] Research floor reached (score: ${m.score.toFixed(1)}). Terminating early.
|
|
477
|
+
`);break}let _t=Math.max(1,Math.ceil(s/2)),u=(Ct.followUpQueries||[]).map((C)=>({type:"search",query:c(String(C)),researchGoal:"Follow-up from learning extraction"})).filter((C)=>C.query&&C.query.toLowerCase()!==t.toLowerCase()).slice(0,_t);if(u.length<_t&&m.nextActions.length>0){let C=m.nextActions.map((W)=>Sf(W)).filter(Boolean);u=[...u,...C].slice(0,_t)}if(u.length<_t&&D.length>0){let C=La(D,t,M,_t-u.length,O+1),i=C.map((W)=>({type:"search",query:W.query,researchGoal:W.researchGoal}));if(u=[...u,...i].slice(0,_t),C.length>0)process.stderr.write(`[greedysearch] Generated ${C.length} gap-driven fallback actions.
|
|
478
|
+
`)}P=u.length>=_t?u:null}process.stderr.write(`PROGRESS:research:final-report
|
|
479
|
+
`);let T={answer:b.length?b.map((O)=>`- ${O}`).join(`
|
|
480
|
+
`):"Research completed, but no structured learnings were extracted.",agreement:{level:"mixed",summary:"Research synthesis fallback."},differences:[],caveats:[],claims:[],recommendedSources:R.slice(0,4).map((O)=>O.id),synthesized:!1};try{let O=await X(An(t,h,R,v,j),{timeoutMs:180000}),L=Ht(O,{}),s=Array.isArray(L?.claims)&&L.claims.length>0;T={...T,...L,rawAnswer:O.answer||"",geminiSources:O.sources||[],synthesized:s}}catch(O){process.stderr.write(`[greedysearch] Final report failed: ${O.message}
|
|
481
|
+
`),T.error=O.message}if(!(T.synthesized===!0&&Array.isArray(T.claims)&&T.claims.length>0)&&j.length>0){process.stderr.write(`[greedysearch] Falling back to evidence-based synthesis (no per-round learnings).
|
|
482
|
+
`);try{let O=Tn(t,R,v,j),L=await X(O,{timeoutMs:180000}),s=Ht(L,{});T={...T,...s,rawAnswer:L.answer||T.answer||"",geminiSources:L.sources||T.geminiSources||[],synthesized:!0,synthesisMode:"evidence_fallback"}}catch(O){process.stderr.write(`[greedysearch] Evidence-based synthesis failed: ${O.message}
|
|
483
|
+
`),T.evidenceFallbackError=O.message}}let q=new Date().toISOString(),xt=Date.now()-d,qf=k.at(-1)||0;y=Qf(y,R),process.stderr.write(`PROGRESS:research:audit-citations
|
|
484
|
+
`);let Ot=kn(T.answer||"",R),Xn=await Hn(R,Ot);Yn(v,T,Ot);let Et=Bt({sources:R,fetchedSources:y,synthesis:T,citationAudit:Ot,gaps:D,questions:v,rounds:h,qualityScore:qf,qualityThreshold:a,maxSources:$.maxSources});if(Et.floorMet&&F==="max_rounds")F="done_floor_met";else if(!Et.floorMet&&F==="quality_threshold")F="max_rounds_floor_unmet";let on={startedAt:et,finishedAt:q,durationMs:xt,engines:kt,synthesizer:"gemini",rounds:h.length,actionsRun:wt,searches:N,fetches:o,sourcesFetched:y.filter((O)=>O?.contentChars>100).length,engineFailures:lt,terminationReason:F,floorMet:Et.floorMet},Nt=null,Lt;if(l){process.stderr.write(`PROGRESS:research:bundle
|
|
485
|
+
`);try{Nt=await In({query:t,rounds:h,sources:R,fetchedSources:y,evidenceItems:j,synthesis:T,citationAudit:Ot,citationUrls:Xn,floor:Et,manifest:on,allGaps:D,questions:v,outDir:w}),Lt=Nt.sourceFiles,delete Nt.sourceFiles}catch(O){Nt={error:O.message||String(O)},Lt=await mn(y)}}else Lt=await mn(y);return process.stderr.write(`PROGRESS:research:done
|
|
486
|
+
`),Z.finish(),{query:t,_research:{mode:"iterative",breadth:$.breadth,iterations:$.iterations,maxSources:$.maxSources,rounds:h,learnings:b,gaps:D,evidence:j,questions:v,questionProgress:Zn(v),qualityHistory:k,terminationReason:F,qualityThreshold:a,floor:Et,bundle:Nt,manifest:on},_citationAudit:Ot,_citationUrls:Xn,_sources:R,_fetchedSources:Lt,_synthesis:T,_confidence:{sourcesCount:R.length,fetchedSourceSuccessRate:y.length>0?Number((y.filter((O)=>O.contentChars>100).length/y.length).toFixed(2)):0,agreementLevel:T.agreement?.level||"mixed",floorMet:Et.floorMet}}}function xf(t){let n=new Map;for(let a of t){let l=a?.id||V(a?.finalUrl||a?.url||"");if(!l)continue;let w=n.get(l);if(!w||(a.contentChars||0)>(w.contentChars||0))n.set(l,a)}let f=new Map;function p(a){let l=f.get(a);if(!l){let w=String(a.content||a.snippet||"");l={length:w.length,tokens:Jn(w.slice(0,4000))},f.set(a,l)}return l}function g(a,l){let w=0,$=a.size<=l.size?a:l,_=a.size<=l.size?l:a;for(let E of $)if(_.has(E))w++;let e=a.size+l.size-w;if(e===0)return 1;return w/e}let r=[];for(let a of n.values()){let l=p(a),w=r.findIndex(($)=>{let _=f.get($);if(l.length<400||_.length<400)return!1;return g(l.tokens,_.tokens)>=0.9});if(w===-1){r.push(a);continue}if((a.contentChars||0)>(r[w].contentChars||0))r[w]=a}return r}export{In as writeResearchBundle,qa as writeProvenanceSidecar,Sf as validateAction,Ut as updateQuestionLedger,Jn as tokenSet,L0 as runResearchMode,Hn as runCitationUrlCheck,Yn as reconcileQuestionsFromSynthesis,Ja as queriesToActions,ma as parseActionPlan,Aa as normalizeResearchQueries,Kn as jaccardSimilarity,zf as isDuplicateQuery,Zf as extractEvidenceFromSources,Xa as extractEvidenceAndLearnings,Ln as createQuestionLedger,Bt as computeResearchFloor,ia as clampResearchOptions,za as checkCitationUrls,Tn as buildSynthesisFromEvidencePrompt,An as buildFinalReportPrompt,La as buildFallbackQueriesFromGaps,Ka as buildEvidenceAndLearningPrompt,kn as auditCitations};
|