@duckmind/dm-darwin-x64 0.42.5 → 0.42.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env node
2
- import{createRequire as h2}from"node:module";var S2=Object.defineProperty;var g2=($)=>$;function y2($,X){this[$]=g2.bind(null,X)}var g0=($,X)=>{for(var Z in X)S2($,Z,{get:X[Z],enumerable:!0,configurable:!0,set:y2.bind(X,Z)})};var c=($,X)=>()=>($&&(X=$($=0)),X);var P1=h2(import.meta.url);import{basename as p2}from"node:path";function H0($=process.env,X=process.execPath){let Z=$.GREEDY_SEARCH_NODE||$.NODE_BINARY||$.NODE;if(Z?.trim())return Z.trim();let J=p2(X||"").toLowerCase();if(J==="node"||J==="node.exe")return X;return"node"}var y0=()=>{};import{spawn as m2}from"node:child_process";import{dirname as d2,join as l2}from"node:path";import{fileURLToPath as u2}from"node:url";function q$($,X=30000){return i2($,null,X)}function i2($,X=null,Z=30000){return new Promise((J,W)=>{let K=m2(H0(),[n2,...$],{stdio:[X==null?"ignore":"pipe","pipe","pipe"]});if(X!=null)K.stdin.write(X),K.stdin.end();let V="",Y="";K.stdout.on("data",(Q)=>V+=Q),K.stderr.on("data",(Q)=>Y+=Q);let j=setTimeout(()=>{K.kill(),W(Error(`cdp timeout: ${$[0]}`))},Z);K.on("close",(Q)=>{if(clearTimeout(j),Q===0)J(V.trim());else W(Error(Y.trim()||`cdp exit ${Q}`))})})}async function b$($){await q$(["evalraw",$,"Page.addScriptToEvaluateOnNewDocument",JSON.stringify({source:`
2
+ import{createRequire as l2}from"node:module";var p2=Object.defineProperty;var m2=($)=>$;function d2($,X){this[$]=m2.bind(null,X)}var g0=($,X)=>{for(var Z in X)p2($,Z,{get:X[Z],enumerable:!0,configurable:!0,set:d2.bind(X,Z)})};var c=($,X)=>()=>($&&(X=$($=0)),X);var L1=l2(import.meta.url);import{basename as u2}from"node:path";function H0($=process.env,X=process.execPath){let Z=$.GREEDY_SEARCH_NODE||$.NODE_BINARY||$.NODE;if(Z?.trim())return Z.trim();let J=u2(X||"").toLowerCase();if(J==="node"||J==="node.exe")return X;return"node"}var y0=()=>{};import{spawn as c2}from"node:child_process";import{dirname as n2,join as i2}from"node:path";import{fileURLToPath as o2}from"node:url";function q$($,X=30000){return t2($,null,X)}function t2($,X=null,Z=30000){return new Promise((J,W)=>{let K=c2(H0(),[s2,...$],{stdio:[X==null?"ignore":"pipe","pipe","pipe"]});if(X!=null)K.stdin.write(X),K.stdin.end();let V="",Y="";K.stdout.on("data",(j)=>V+=j),K.stderr.on("data",(j)=>Y+=j);let Q=setTimeout(()=>{K.kill(),W(Error(`cdp timeout: ${$[0]}`))},Z);K.on("close",(j)=>{if(clearTimeout(Q),j===0)J(V.trim());else W(Error(Y.trim()||`cdp exit ${j}`))})})}async function b$($){await q$(["evalraw",$,"Page.addScriptToEvaluateOnNewDocument",JSON.stringify({source:`
3
3
  (function() {
4
4
  // ── Runtime.enable / CDP detection masking ──────────────
5
5
  try { delete window.__REBROWSER_RUNTIME_ENABLE; } catch(_) {}
@@ -207,39 +207,39 @@ import{createRequire as h2}from"node:module";var S2=Object.defineProperty;var g2
207
207
  };
208
208
  } catch(_) {}
209
209
  })();
210
- `})])}var c2,n2;var _1=c(()=>{y0();c2=d2(u2(import.meta.url)),n2=l2(c2,"..","bin","cdp.mjs")});import{existsSync as k1}from"node:fs";import{platform as o2}from"node:os";import{join as A0}from"node:path";function p($){let X=o2()==="win32",Z=process.env.SystemRoot||"C:\\Windows",J={win32:{powershell:A0(Z,"System32","WindowsPowerShell","v1.0","powershell.exe"),powershell_ise:A0(Z,"System32","WindowsPowerShell","v1.0","powershell_ise.exe"),netstat:A0(Z,"System32","netstat.exe"),taskkill:A0(Z,"System32","taskkill.exe"),tasklist:A0(Z,"System32","tasklist.exe"),cmd:A0(Z,"System32","cmd.exe")},unix:{ps:"/usr/bin/ps",lsof:"/usr/bin/lsof",ss:"/usr/sbin/ss",grep:"/usr/bin/grep",kill:"/usr/bin/kill"}},W=X?J.win32:J.unix,K=$.toLowerCase();if(W[K]&&k1(W[K]))return W[K];if(X&&K==="netstat"){let V=A0(Z,"Sysnative","netstat.exe");if(k1(V))return V}return $}var v$=()=>{};import{existsSync as J$,mkdirSync as a2,readFileSync as F1,writeFileSync as s2}from"node:fs";import{homedir as t2}from"node:os";import{join as L1}from"node:path";import{tmpdir as W$}from"node:os";function e2(){try{if(J$(K0)){let $=F1(K0,"utf8"),X=JSON.parse($);if(Array.isArray(X.engines)&&X.engines.length>0&&X.engines.every((Z)=>typeof Z==="string")){let Z=X.engines.filter((W)=>X0[W]),J=X.engines.filter((W)=>!X0[W]);if(J.length>0)process.stderr.write(`[greedysearch] Warning: ignoring unknown engine(s) in ${K0}: ${J.join(", ")}
210
+ `})])}var a2,s2;var w1=c(()=>{y0();a2=n2(o2(import.meta.url)),s2=i2(a2,"..","bin","cdp.mjs")});import{existsSync as T1}from"node:fs";import{platform as r2}from"node:os";import{join as A0}from"node:path";function p($){let X=r2()==="win32",Z=process.env.SystemRoot||"C:\\Windows",J={win32:{powershell:A0(Z,"System32","WindowsPowerShell","v1.0","powershell.exe"),powershell_ise:A0(Z,"System32","WindowsPowerShell","v1.0","powershell_ise.exe"),netstat:A0(Z,"System32","netstat.exe"),taskkill:A0(Z,"System32","taskkill.exe"),tasklist:A0(Z,"System32","tasklist.exe"),cmd:A0(Z,"System32","cmd.exe")},unix:{ps:"/usr/bin/ps",lsof:"/usr/bin/lsof",ss:"/usr/sbin/ss",grep:"/usr/bin/grep",kill:"/usr/bin/kill"}},W=X?J.win32:J.unix,K=$.toLowerCase();if(W[K]&&T1(W[K]))return W[K];if(X&&K==="netstat"){let V=A0(Z,"Sysnative","netstat.exe");if(T1(V))return V}return $}var v$=()=>{};import{existsSync as W$,mkdirSync as e2,readFileSync as C1,writeFileSync as $8}from"node:fs";import{homedir as X8}from"node:os";import{join as R1}from"node:path";import{tmpdir as K$}from"node:os";function J8(){try{if(W$(K0)){let $=C1(K0,"utf8"),X=JSON.parse($);if(Array.isArray(X.engines)&&X.engines.length>0&&X.engines.every((Z)=>typeof Z==="string")){let Z=X.engines.filter((W)=>X0[W]),J=X.engines.filter((W)=>!X0[W]);if(J.length>0)process.stderr.write(`[greedysearch] Warning: ignoring unknown engine(s) in ${K0}: ${J.join(", ")}
211
211
  [greedysearch] Available engines: ${Object.keys(X0).join(", ")}
212
212
  `);if(Z.length>0)return Z;process.stderr.write(`[greedysearch] Warning: no valid engines in ${K0}, falling back to defaults: ${f$.join(", ")}
213
- `)}}}catch{}return f$}function $8(){try{if(!J$(E$))a2(E$,{recursive:!0});if(!J$(K0))s2(K0,JSON.stringify({engines:f$,synthesizer:x$},null,2)+`
214
- `,"utf8")}catch{}}function X8(){try{if(J$(K0)){let $=F1(K0,"utf8"),X=JSON.parse($);if(typeof X.synthesizer==="string"){let Z=X.synthesizer.toLowerCase();if(h0.includes(Z))return Z;process.stderr.write(`[greedysearch] Warning: unknown synthesizer "${X.synthesizer}" in ${K0}
213
+ `)}}}catch{}return f$}function W8(){try{if(!W$(E$))e2(E$,{recursive:!0});if(!W$(K0))$8(K0,JSON.stringify({engines:f$,synthesizer:x$},null,2)+`
214
+ `,"utf8")}catch{}}function K8(){try{if(W$(K0)){let $=C1(K0,"utf8"),X=JSON.parse($);if(typeof X.synthesizer==="string"){let Z=X.synthesizer.toLowerCase();if(h0.includes(Z))return Z;process.stderr.write(`[greedysearch] Warning: unknown synthesizer "${X.synthesizer}" in ${K0}
215
215
  [greedysearch] Available synthesizers: ${h0.join(", ")}
216
216
  [greedysearch] Falling back to default: ${x$}
217
- `)}}}catch{}return x$}var y=9222,n,q0,r2,P0,w1,E$,K0,f$,x$="gemini",h0,X0,e,S$,T1,g$;var V0=c(()=>{n=`${W$().replaceAll("\\","/")}/greedysearch-chrome-profile`,q0=`${n}/DevToolsActivePort`,r2=`${W$().replaceAll("\\","/")}/cdp-pages.json`,P0=`${W$().replaceAll("\\","/")}/greedysearch-chrome-mode`,w1=`${W$().replaceAll("\\","/")}/greedysearch-visible-recovery.jsonl`,E$=L1(t2(),".dm"),K0=L1(E$,"greedyconfig"),f$=["perplexity","google","chatgpt"];$8();h0=["gemini","chatgpt"];X0={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"},e=e2(),S$=e,T1=X8(),g$=Math.max(1,Number.parseInt(process.env.GREEDY_FETCH_CONCURRENCY||"5",10)||5);process.env.CDP_PROFILE_DIR=n});import{existsSync as e9,mkdirSync as Z8,readFileSync as R1,renameSync as J8,unlinkSync as W8,writeFileSync as I1}from"node:fs";import K8 from"node:http";import{platform as q1}from"node:os";import{dirname as V8}from"node:path";function j8($){return[...new Set($.filter(Boolean))]}function C1($,X="explicit"){let Z=new URL($),J=Z.protocol==="wss:"?"wss:":"ws:",W=Z.port?Number.parseInt(Z.port,10):J==="wss:"?443:80;return{source:X,wsUrl:`${J}//${Z.host}${Z.pathname}${Z.search}`,host:Z.hostname,port:W,path:`${Z.pathname}${Z.search}`,httpUrl:`http://${Z.hostname}:${W}`}}function v1($=process.env){return![$.GREEDY_SEARCH_REUSE_EXISTING_BROWSER,$.DM_CUA_REUSE_EXISTING_BROWSER].some((X)=>X!==void 0&&Y8.test(String(X)))}function y$($){let X=Number.parseInt(String($??""),10);return Number.isInteger(X)&&X>0&&X<65536?X:null}function Q8($){if(!$)return null;let X=String($).trim();if(!X)return null;let Z=new URL(X.includes("://")?X:`http://${X}`),J=y$(Z.port)||(Z.protocol==="https:"?443:80);return{host:Z.hostname,port:J,source:`env:${X}`}}function B8($){if(!$)return[];return String($).split(/[,\s]+/).map(y$).filter(Boolean)}function M8({env:$=process.env,platformName:X=q1(),procVersionText:Z=null}={}){if(X!=="linux")return!1;if($.WSL_DISTRO_NAME||$.WSL_INTEROP)return!0;let J=Z??(()=>{try{return R1("/proc/version","utf8")}catch{return""}})();return/microsoft|wsl/i.test(J)}function H8({env:$=process.env,platformName:X=q1(),procVersionText:Z=null,resolvConfText:J=null}={}){let W=["localhost","127.0.0.1"];if(M8({env:$,platformName:X,procVersionText:Z})){let V=(J??(()=>{try{return R1("/etc/resolv.conf","utf8")}catch{return""}})()).match(/^\s*nameserver\s+([^\s#]+)/m);if(V)W.push(V[1]);W.push("host.docker.internal")}return j8(W)}function G8({env:$=process.env,hosts:X=H8({env:$}),autoPorts:Z=b1,excludePorts:J=[]}={}){let W=[];for(let j of["GREEDY_SEARCH_CDP_WS_URL","DM_CUA_CDP_WS_URL","CDP_WS_URL"])if($[j])W.push({wsUrl:$[j],source:`env:${j}`});for(let j of["GREEDY_SEARCH_CDP_URL","DM_CUA_CDP_URL","CDP_URL"])try{let Q=Q8($[j]);if(Q)W.push(Q)}catch{}for(let j of["GREEDY_SEARCH_EXISTING_CDP_PORT","DM_CUA_EXISTING_CDP_PORT","GREEDY_SEARCH_CDP_PORT","DM_CUA_CDP_PORT"]){let Q=y$($[j]);if(!Q)continue;for(let B of X)W.push({host:B,port:Q,source:`env:${j}`})}let K=B8($.GREEDY_SEARCH_EXISTING_CDP_PORTS);for(let j of K)for(let Q of X)W.push({host:Q,port:j,source:"env:GREEDY_SEARCH_EXISTING_CDP_PORTS"});let V=new Set(J.filter(Boolean));if(v1($))for(let j of Z){if(V.has(j))continue;for(let Q of X)W.push({host:Q,port:j,source:"auto"})}let Y=new Set;return W.filter((j)=>{let Q=j.wsUrl||`${j.host}:${j.port}`;if(Y.has(Q))return!1;return Y.add(Q),!0})}async function O8($,{timeoutMs:X=700}={}){if($.wsUrl)return C1($.wsUrl,$.source);let Z=$.host||"localhost",J=$.port;if(!J)return null;let W=await new Promise((K)=>{let V=K8.get({host:Z,port:J,path:"/json/version",timeout:X},(Y)=>{let j="";Y.setEncoding("utf8"),Y.on("data",(Q)=>j+=Q),Y.on("end",()=>K(Y.statusCode===200?j:null))});V.on("error",()=>K(null)),V.setTimeout(X,()=>{V.destroy(),K(null)})});if(!W)return null;try{let K=JSON.parse(W);if(!K.webSocketDebuggerUrl)return null;let V=C1(K.webSocketDebuggerUrl,$.source),Y=V.path||new URL(K.webSocketDebuggerUrl).pathname;return{...V,host:Z,port:J,httpUrl:`http://${Z}:${J}`,wsUrl:`ws://${Z}:${J}${Y}`,path:Y}}catch{return null}}async function D8($={}){for(let X of G8($)){let Z=await O8(X,$);if(Z)return Z}return null}function N8($){if(!$)throw Error("missing CDP endpoint");let X=new URL($.wsUrl);return`${X.host}
217
+ `)}}}catch{}return x$}var y=9222,n,q0,Z8,P0,I1,E$,K0,f$,x$="gemini",h0,X0,e,S$,q1,g$;var V0=c(()=>{n=`${K$().replaceAll("\\","/")}/greedysearch-chrome-profile`,q0=`${n}/DevToolsActivePort`,Z8=`${K$().replaceAll("\\","/")}/cdp-pages.json`,P0=`${K$().replaceAll("\\","/")}/greedysearch-chrome-mode`,I1=`${K$().replaceAll("\\","/")}/greedysearch-visible-recovery.jsonl`,E$=R1(X8(),".dm"),K0=R1(E$,"greedyconfig"),f$=["perplexity","google","chatgpt"];W8();h0=["gemini","chatgpt"];X0={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"},e=J8(),S$=e,q1=K8(),g$=Math.max(1,Number.parseInt(process.env.GREEDY_FETCH_CONCURRENCY||"5",10)||5);process.env.CDP_PROFILE_DIR=n});import{existsSync as Z4,mkdirSync as V8,readFileSync as v1,renameSync as Y8,unlinkSync as Q8,writeFileSync as E1}from"node:fs";import j8 from"node:http";import{platform as f1}from"node:os";import{dirname as B8}from"node:path";function H8($){return[...new Set($.filter(Boolean))]}function b1($,X="explicit"){let Z=new URL($),J=Z.protocol==="wss:"?"wss:":"ws:",W=Z.port?Number.parseInt(Z.port,10):J==="wss:"?443:80;return{source:X,wsUrl:`${J}//${Z.host}${Z.pathname}${Z.search}`,host:Z.hostname,port:W,path:`${Z.pathname}${Z.search}`,httpUrl:`http://${Z.hostname}:${W}`}}function S1($=process.env){return![$.GREEDY_SEARCH_REUSE_EXISTING_BROWSER,$.DM_CUA_REUSE_EXISTING_BROWSER].some((X)=>X!==void 0&&M8.test(String(X)))}function y$($){let X=Number.parseInt(String($??""),10);return Number.isInteger(X)&&X>0&&X<65536?X:null}function O8($){if(!$)return null;let X=String($).trim();if(!X)return null;let Z=new URL(X.includes("://")?X:`http://${X}`),J=y$(Z.port)||(Z.protocol==="https:"?443:80);return{host:Z.hostname,port:J,source:`env:${X}`}}function G8($){if(!$)return[];return String($).split(/[,\s]+/).map(y$).filter(Boolean)}function D8({env:$=process.env,platformName:X=f1(),procVersionText:Z=null}={}){if(X!=="linux")return!1;if($.WSL_DISTRO_NAME||$.WSL_INTEROP)return!0;let J=Z??(()=>{try{return v1("/proc/version","utf8")}catch{return""}})();return/microsoft|wsl/i.test(J)}function N8({env:$=process.env,platformName:X=f1(),procVersionText:Z=null,resolvConfText:J=null}={}){let W=["localhost","127.0.0.1"];if(D8({env:$,platformName:X,procVersionText:Z})){let V=(J??(()=>{try{return v1("/etc/resolv.conf","utf8")}catch{return""}})()).match(/^\s*nameserver\s+([^\s#]+)/m);if(V)W.push(V[1]);W.push("host.docker.internal")}return H8(W)}function z8({env:$=process.env,hosts:X=N8({env:$}),autoPorts:Z=x1,excludePorts:J=[]}={}){let W=[];for(let Q of["GREEDY_SEARCH_CDP_WS_URL","DM_CUA_CDP_WS_URL","CDP_WS_URL"])if($[Q])W.push({wsUrl:$[Q],source:`env:${Q}`});for(let Q of["GREEDY_SEARCH_CDP_URL","DM_CUA_CDP_URL","CDP_URL"])try{let j=O8($[Q]);if(j)W.push(j)}catch{}for(let Q of["GREEDY_SEARCH_EXISTING_CDP_PORT","DM_CUA_EXISTING_CDP_PORT","GREEDY_SEARCH_CDP_PORT","DM_CUA_CDP_PORT"]){let j=y$($[Q]);if(!j)continue;for(let B of X)W.push({host:B,port:j,source:`env:${Q}`})}let K=G8($.GREEDY_SEARCH_EXISTING_CDP_PORTS);for(let Q of K)for(let j of X)W.push({host:j,port:Q,source:"env:GREEDY_SEARCH_EXISTING_CDP_PORTS"});let V=new Set(J.filter(Boolean));if(S1($))for(let Q of Z){if(V.has(Q))continue;for(let j of X)W.push({host:j,port:Q,source:"auto"})}let Y=new Set;return W.filter((Q)=>{let j=Q.wsUrl||`${Q.host}:${Q.port}`;if(Y.has(j))return!1;return Y.add(j),!0})}async function U8($,{timeoutMs:X=700}={}){if($.wsUrl)return b1($.wsUrl,$.source);let Z=$.host||"localhost",J=$.port;if(!J)return null;let W=await new Promise((K)=>{let V=j8.get({host:Z,port:J,path:"/json/version",timeout:X},(Y)=>{let Q="";Y.setEncoding("utf8"),Y.on("data",(j)=>Q+=j),Y.on("end",()=>K(Y.statusCode===200?Q:null))});V.on("error",()=>K(null)),V.setTimeout(X,()=>{V.destroy(),K(null)})});if(!W)return null;try{let K=JSON.parse(W);if(!K.webSocketDebuggerUrl)return null;let V=b1(K.webSocketDebuggerUrl,$.source),Y=V.path||new URL(K.webSocketDebuggerUrl).pathname;return{...V,host:Z,port:J,httpUrl:`http://${Z}:${J}`,wsUrl:`ws://${Z}:${J}${Y}`,path:Y}}catch{return null}}async function A8($={}){for(let X of z8($)){let Z=await U8(X,$);if(Z)return Z}return null}function P8($){if(!$)throw Error("missing CDP endpoint");let X=new URL($.wsUrl);return`${X.host}
218
218
  ${X.pathname}${X.search}
219
- `}function z8($,X){Z8(V8($),{recursive:!0});let Z=`${$}.tmp-${process.pid}`;I1(Z,N8(X),"utf8");try{W8($)}catch{}J8(Z,$)}async function E1({activePortFile:$,modeFile:X,env:Z=process.env,excludePorts:J=[],autoPorts:W=b1}={}){if(!v1(Z))return null;let K=await D8({env:Z,excludePorts:J,autoPorts:W});if(!K)return null;if(z8($,K),X)I1(X,"existing","utf8");return K}var b1,Y8;var f1=c(()=>{b1=[9222,9223],Y8=/^(0|false|no|off)$/i});import{execFileSync as x1,execSync as h$}from"node:child_process";import{existsSync as K$,mkdirSync as U8,readFileSync as b0,unlinkSync as k0,writeFileSync as F0}from"node:fs";import{platform as p$,tmpdir as p1}from"node:os";function m0($){if(!Number.isInteger($)||$<=0)return!1;try{return process.kill($,0),!0}catch{return!1}}function P8($){if(!m0($))return null;try{if(p$()==="win32")return x1(p("powershell"),["-NoProfile","-NonInteractive","-Command",`(Get-CimInstance Win32_Process -Filter "ProcessId = ${$}").CommandLine`],{encoding:"utf8",windowsHide:!0,timeout:5000}).trim()||null;return x1(p("ps"),["-p",String($),"-o","command="],{encoding:"utf8",timeout:5000}).trim()||null}catch{return null}}function _8($,X,Z=y){if(!$)return!1;let J=(V)=>String(V||"").replaceAll("\\","/").toLowerCase(),W=J($),K=J(X);return W.includes(K)&&W.includes(`--remote-debugging-port=${Z}`)&&!W.includes("--type=")}function S1($,X,Z=y){return _8(P8($),X,Z)}function k8($=y){try{if(p$()==="win32"){let Z=h$(`${p("netstat")} -ano -p TCP 2>nul`,{encoding:"utf8"}),J=new RegExp(String.raw`TCP\s+\S+:${$}\s+\S+:0\s+LISTENING\s+(\d+)`,"i"),W=Z.match(J);return W?Number.parseInt(W[1],10):null}let X=h$(`${p("lsof")} -i :${$} -t 2>/dev/null || ${p("ss")} -tlnp 2>/dev/null | ${p("grep")} :${$} | ${p("grep")} -oP 'pid=\\K\\d+'`,{encoding:"utf8"}).trim();return X?Number.parseInt(X.split(`
220
- `)[0],10):null}catch{return null}}function g1($){try{if(p$()==="win32")h$(`${p("taskkill")} /F /T /PID ${$}`,{stdio:"ignore"});else{try{process.kill(-$,"SIGKILL")}catch{}try{process.kill($,"SIGKILL")}catch{}}return!0}catch{return!1}}function l0(){try{if(K$(V$)){let $=b0(V$,"utf8"),X=JSON.parse($);if(X&&typeof X.tempDir==="string"&&typeof X.debugPort==="number")return{browserPid:Number.isInteger(X.browserPid)?X.browserPid:void 0,debugPort:X.debugPort,tempDir:X.tempDir,clientPids:Array.isArray(X.clientPids)?X.clientPids.filter((Z)=>Number.isInteger(Z)&&Z>0):[],sessionMode:X.sessionMode==="visible"?"visible":"headless",lastActivity:Number.isFinite(X.lastActivity)?X.lastActivity:0,launchedAt:Number.isFinite(X.launchedAt)?X.launchedAt:0}}}catch{}try{let $=K$(Y$)?Number.parseInt(b0(Y$,"utf8").trim(),10)||void 0:void 0,X=K$(j$)?b0(j$,"utf8").trim()==="visible"?"visible":"headless":"headless",Z=K$(p0)?Number.parseInt(b0(p0,"utf8").trim(),10)||0:0;return{browserPid:$,debugPort:y,tempDir:n,clientPids:$?[$]:[],sessionMode:X,lastActivity:Z,launchedAt:0}}catch{return null}}function Q$($){try{F0(V$,JSON.stringify({browserPid:$.browserPid,debugPort:$.debugPort,tempDir:$.tempDir,clientPids:[...new Set($.clientPids.filter((X)=>X>0))],sessionMode:$.sessionMode,lastActivity:$.lastActivity,launchedAt:$.launchedAt},null,2),"utf8")}catch{}try{if($.browserPid)F0(Y$,String($.browserPid),"utf8")}catch{}try{F0(j$,$.sessionMode,"utf8")}catch{}try{F0(p0,String($.lastActivity),"utf8")}catch{}}function y1(){try{k0(V$)}catch{}try{k0(Y$)}catch{}try{k0(j$)}catch{}try{k0(p0)}catch{}}function m1($){if(!$)return $;let X={...$,clientPids:[...new Set([...$.clientPids,process.pid].filter((Z)=>m0(Z)||Z===process.pid))]};return Q$(X),X}function m$($){let X=Date.now();try{if($)Q$({...$,lastActivity:X});else F0(p0,String(X),"utf8")}catch{}}function d1(){if(h1)return;h1=!0;let $=l0();if(!$)return;if($.browserPid){if(!m0($.browserPid)){g1($.browserPid),y1();return}if(!S1($.browserPid,$.tempDir,$.debugPort))y1();else Q$({...$,clientPids:$.clientPids.filter((W)=>m0(W))})}let X=k8();if(X&&X!==$.browserPid)if(S1(X,n,y))Q$({browserPid:X,debugPort:y,tempDir:n,clientPids:[X],sessionMode:$.sessionMode,lastActivity:Date.now(),launchedAt:Date.now()});else g1(X)}function l1(){U8(p1(),{recursive:!0});try{let $=JSON.stringify({pid:process.pid,ts:Date.now()});return F0(_0,$,{encoding:"utf8",flag:"wx"}),{acquired:!0,release:()=>{try{let X=b0(_0,"utf8");if(JSON.parse(X).pid===process.pid)k0(_0)}catch{}}}}catch($){if($?.code!=="EEXIST")return{acquired:!1,release:()=>{}}}try{let $=b0(_0,"utf8"),X=JSON.parse($),Z=Date.now()-(X.ts||0);if(!m0(X.pid)||Z>A8){try{k0(_0)}catch{}try{let W=JSON.stringify({pid:process.pid,ts:Date.now()});return F0(_0,W,{encoding:"utf8",flag:"wx"}),{acquired:!0,release:()=>{try{k0(_0)}catch{}}}}catch{return{acquired:!1,release:()=>{}}}}}catch{}return{acquired:!1,release:()=>{}}}var d0,V$,_0,A8=15000,Y$,j$,p0,Q4,h1=!1;var u1=c(()=>{V0();v$();d0=p1().replaceAll("\\","/"),V$=`${d0}/greedysearch-chrome-metadata.json`,_0=`${d0}/greedysearch-chrome-launch.lock`,Y$=`${d0}/greedysearch-chrome.pid`,j$=`${d0}/greedysearch-chrome-mode`,p0=`${d0}/greedysearch-chrome-last-activity`;Q4=Number.parseInt(process.env.GREEDY_SEARCH_IDLE_TIMEOUT_MINUTES||"5",10)||5});import{spawn as F8,execFileSync as c1,execSync as l$}from"node:child_process";import{existsSync as M$,readFileSync as H$,renameSync as L8,unlinkSync as Y0,writeFileSync as B$}from"node:fs";import u$ from"node:http";import{platform as c$,tmpdir as w8}from"node:os";import{join as T8}from"node:path";function q8($,X=y){let Z=String($||"").toLowerCase();if(!Z.includes(`--remote-debugging-port=${X}`)||Z.includes("--type="))return null;return Z.includes("--headless")}function i1(){try{let $=n$(),X=$?b8($):null,Z=q8(X);if(Z!==null){try{B$(P0,Z?"headless":"visible","utf8")}catch{}return Z}}catch{}try{if(!M$(P0))return!0;return H$(P0,"utf8").trim()==="headless"}catch{return!0}}function G$(){try{B$(c0,String(Date.now()),"utf8")}catch{}try{let $=l0();if($)m$($)}catch{}}function b8($){try{if(c$()==="win32")return c1(p("powershell"),["-NoProfile","-NonInteractive","-Command",`(Get-CimInstance Win32_Process -Filter "ProcessId = ${$}").CommandLine`],{encoding:"utf8",windowsHide:!0,timeout:5000}).trim()||null;return c1(p("ps"),["-p",String($),"-o","command="],{encoding:"utf8",timeout:5000}).trim()||null}catch{return null}}function n$(){try{if(c$()==="win32"){let X=l$(`${p("netstat")} -ano -p TCP 2>nul`,{encoding:"utf8"}),Z=new RegExp(String.raw`TCP\s+\S+:${y}\s+\S+:0\s+LISTENING\s+(\d+)`,"i"),J=X.match(Z);return J?Number.parseInt(J[1],10):null}let $=l$(`${p("lsof")} -i :${y} -t 2>/dev/null || ${p("ss")} -tlnp 2>/dev/null | ${p("grep")} :${y} | ${p("grep")} -oP 'pid=\\K\\d+'`,{encoding:"utf8"}).trim();return $?Number.parseInt($.split(`
221
- `)[0],10):null}catch{return null}}async function v8($=1500){try{let Z=await new Promise((W,K)=>{let V=u$.get(`http://localhost:${y}/json/version`,(Y)=>{let j="";Y.on("data",(Q)=>j+=Q),Y.on("end",()=>{try{W(JSON.parse(j))}catch{K(Error("bad JSON"))}})});V.on("error",K),V.setTimeout(1000,()=>{V.destroy(),K(Error("timeout"))})}),J=new globalThis.WebSocket(Z.webSocketDebuggerUrl);await new Promise((W)=>{J.onopen=()=>{J.send(JSON.stringify({id:1,method:"Browser.close"})),setTimeout(()=>{J.close(),W()},200)},J.onerror=()=>W(),setTimeout(W,1000)})}catch{}let X=Date.now()+$;while(Date.now()<X){if(!n$())return!0;await new Promise((J)=>setTimeout(J,150))}return E8()}function E8(){try{let $=n$();if(!$&&M$(u0))$=Number.parseInt(H$(u0,"utf8").trim(),10)||null;if(!$)return!1;if(c$()==="win32")l$(`${p("taskkill")} /F /PID ${$}`,{stdio:"ignore"});else process.kill($,"SIGKILL");return!0}catch{return!1}}async function o1(){if(!await v0(500)){try{Y0(u0)}catch{}try{Y0(c0)}catch{}try{Y0(P0)}catch{}return!1}let X=await v8(1500);try{Y0(u0)}catch{}try{Y0(c0)}catch{}try{Y0(P0)}catch{}if(X)process.stderr.write(`[greedysearch] Killed Chrome on port ${y}.
222
- `);return X}async function f8(){let X=i1()?R8:I8;if(X<=0)return!1;if(!M$(c0))return G$(),!1;try{let Z=Number.parseInt(H$(c0,"utf8").trim(),10);if(!Z)return!1;if((Date.now()-Z)/60000>=X)return o1()}catch{}return!1}async function a1(){let X=(await b(["list"])).split(`
223
- `)[0];if(!X)throw Error("No Chrome tabs found");return X.slice(0,8)}async function j0($="about:blank"){let X=await a1(),Z=new URL($).hostname;if(Z==="copilot.microsoft.com"||Z==="www.perplexity.ai"||Z==="perplexity.ai"||Z.endsWith(".perplexity.ai")){let V=await b(["evalraw",X,"Target.createTarget",JSON.stringify({url:"about:blank"})]),{targetId:Y}=JSON.parse(V),j=Y.slice(0,8);if(await b(["list"]).catch(()=>null),Z==="copilot.microsoft.com")await b$(j);else b$(j).catch(()=>{});return await b(["list"]).catch(()=>null),Y}let W=await b(["evalraw",X,"Target.createTarget",JSON.stringify({url:$})]),{targetId:K}=JSON.parse(W);return await b(["list"]).catch(()=>null),K}async function G0($){try{let X=await a1();await b(["evalraw",X,"Target.closeTarget",JSON.stringify({targetId:$})])}catch{}}async function O$($=[]){if(await Promise.all($.filter(Boolean).map((X)=>G0(X).catch(()=>{}))),$.length>0)await b(["list"]).catch(()=>null)}function v0($=3000){return new Promise((X)=>{let Z=u$.get(`http://localhost:${y}/json/version`,(J)=>{J.resume(),X(J.statusCode===200)});Z.on("error",()=>X(!1)),Z.setTimeout($,()=>{Z.destroy(),X(!1)})})}async function d$(){let $=`${q0}.lock`,X=`${q0}.tmp`,Z=5000,J=1000,W=await new Promise((K)=>{let V=Date.now(),Y=()=>{try{let j=JSON.stringify({pid:process.pid,ts:Date.now()});B$($,j,{encoding:"utf8",flag:"wx"}),K(!0)}catch(j){if(j?.code!=="EEXIST"){if(Date.now()-V<1000)setTimeout(Y,50);else K(!1);return}try{let Q=H$($,"utf8").trim(),B=Q.startsWith("{")?JSON.parse(Q):{ts:Number(Q)},M=Number(B?.ts)||0;if(M>0&&Date.now()-M>5000)try{Y0($)}catch{}if(Date.now()-V<1000)setTimeout(Y,50);else K(!1)}catch{if(Date.now()-V<1000)setTimeout(Y,50);else K(!1)}}};Y()});try{let K=await new Promise((Q,B)=>{let M=u$.get(`http://localhost:${y}/json/version`,(D)=>{let H="";D.on("data",(z)=>H+=z),D.on("end",()=>Q(H))});M.on("error",B),M.setTimeout(3000,()=>{M.destroy(),B(Error("timeout"))})}),{webSocketDebuggerUrl:V}=JSON.parse(K),Y=new URL(V),j=`${Y.pathname}${Y.search}`;if(W){B$(X,`localhost:${y}
224
- ${j}`,"utf8");try{Y0(q0)}catch{}L8(X,q0)}}catch{}finally{if(W)try{Y0($)}catch{}}}async function n0(){let $=M$(u0)||!!l0(),X=await E1({activePortFile:q0,modeFile:P0,excludePorts:$?[y]:[]});if(X){process.stderr.write(`[greedysearch] Reusing existing browser CDP endpoint ${X.httpUrl} (${X.source}).
225
- `);return}d1();let Z=await f8(),J=Z?!1:await v0();if(!J&&!Z)await new Promise((Y)=>setTimeout(Y,500)),J=await v0();let W=!1;if(J){let Y=i1(),j=process.env.GREEDY_SEARCH_VISIBLE==="1";if(!j&&!Y)process.stderr.write(`[greedysearch] Visible Chrome detected — switching to headless mode...
226
- `),await L0(),await new Promise((Q)=>setTimeout(Q,1000)),W=!0;else if(j&&Y)process.stderr.write(`[greedysearch] Headless Chrome detected — switching to visible mode...
227
- `),await L0(),await new Promise((Q)=>setTimeout(Q,1000)),W=!0}if(W?!1:await v0()){await d$();try{let Y=l0();if(Y)m$(Y),m1(Y)}catch{}return}let V=l1();if(!V.acquired){if(await new Promise((j)=>setTimeout(j,3000)),await v0(5000)){await d$();return}}try{if(await v0(1000)){await d$();return}process.stderr.write(`GreedySearch Chrome not running on port ${y} — auto-launching...
228
- `);let j=[T8(C8,"..","..","bin","launch.mjs")];if(process.env.GREEDY_SEARCH_VISIBLE!=="1")j.push("--headless");await new Promise((Q,B)=>{F8(H0(),j,{stdio:["ignore",process.stderr,process.stderr],env:{...process.env,CDP_PROFILE_DIR:n,GREEDY_SEARCH_PROFILE_DIR:n}}).on("close",(D)=>D===0?Q():B(Error("launch.mjs failed")))})}finally{V.release()}}var C8,n1,u0,c0,R8,I8,L0,b;var i$=c(()=>{_1();y0();v$();V0();f1();u1();C8=import.meta.dirname||new URL(".",import.meta.url).pathname.replace(/^\/([A-Z]:)/,"$1"),n1=w8().replaceAll("\\","/"),u0=`${n1}/greedysearch-chrome.pid`,c0=`${n1}/greedysearch-chrome-last-activity`,R8=Number.parseInt(process.env.GREEDY_SEARCH_IDLE_TIMEOUT_MINUTES||"5",10)||5,I8=Number.parseInt(process.env.GREEDY_SEARCH_VISIBLE_IDLE_TIMEOUT_MINUTES||"60",10)||60;L0=o1;b=q$});var X2={};g0(X2,{shouldUseBrowser:()=>i8,rewriteGitHubUrl:()=>$2,isPrivateUrl:()=>D$,fetchSourceHttp:()=>a$,extractContent:()=>z$,detectBotBlock:()=>N$,defaultFetchHeaders:()=>o$,checkContentQuality:()=>U$});import{Readability as y8}from"@mozilla/readability";import{JSDOM as h8}from"jsdom";import p8 from"turndown";function o$($={}){return{...e1,...$}}function D$($){try{let X=new URL($),Z=X.hostname.toLowerCase();for(let J of m8)if(J.test(Z))return{blocked:!0,reason:`Private/internal address: ${Z}`};if(X.protocol==="file:")return{blocked:!0,reason:"File protocol not allowed"};return{blocked:!1}}catch(X){return{blocked:!0,reason:`Invalid URL: ${X.message}`}}}function $2($){try{let X=new URL($);if(!(X.hostname==="github.com"||X.hostname.endsWith(".github.com")))return $;let Z=X.pathname.split("/").filter(Boolean);if(Z.length<5)return $;let[J,W,K,V,...Y]=Z;if(K!=="blob")return $;let j=Y.join("/");return`https://raw.githubusercontent.com/${J}/${W}/${V}/${j}`}catch{return $}}async function a$($,X={}){let Z=D$($);if(Z.blocked)return{ok:!1,url:$,finalUrl:$,status:403,error:`Blocked: ${Z.reason}`,needsBrowser:!1};let J=$;if($=$2($),$!==J)console.error(`[fetcher] Rewrote GitHub URL: ${J.slice(0,60)}... → raw.githubusercontent.com`);let{timeoutMs:W=15000,userAgent:K,signal:V}=X,Y=new AbortController,j=setTimeout(()=>Y.abort(),W);if(V)V.addEventListener("abort",()=>Y.abort(),{once:!0});try{let Q=await fetch($,{method:"GET",headers:{...e1,"user-agent":K||r1},redirect:"follow",signal:Y.signal});clearTimeout(j);let B=Q.headers.get("content-type")||"",M=Q.url,D=Q.headers.get("last-modified")||"",H=!1;try{H=new URL(M).hostname.toLowerCase()==="raw.githubusercontent.com"}catch{}if(B.includes("text/plain")&&H){let E=await Q.text();return{ok:!0,url:J,finalUrl:M,status:Q.status,title:M.split("/").pop()||"GitHub File",byline:"",siteName:"GitHub",lang:"",publishedTime:D,lastModified:D,markdown:E,contentLength:E.length,excerpt:E.slice(0,300).replaceAll(/\n/g," "),needsBrowser:!1}}if(!B.includes("text/html")&&!B.includes("application/xhtml"))return{ok:!1,url:$,finalUrl:M,status:Q.status,error:`Unsupported content type: ${B}`,needsBrowser:!1};let z=await Q.text(),N=N$(Q.status,z,M,$);if(N.blocked)return{ok:!1,url:$,finalUrl:M,status:Q.status,error:`Blocked: ${N.reason}`,needsBrowser:!0};let G=z$(z,M),k=U$(G);if(!k.ok)return{ok:!1,url:$,finalUrl:M,status:Q.status,error:`Low quality content: ${k.reason}`,needsBrowser:!0};return{ok:!0,url:$,finalUrl:M,status:Q.status,title:G.title,byline:G.byline,siteName:G.siteName,lang:G.lang,publishedTime:G.publishedTime||D,lastModified:D,markdown:G.markdown,excerpt:G.excerpt,contentLength:G.markdown.length,needsBrowser:!1}}catch(Q){clearTimeout(j);let B=n8(Q);return{ok:!1,url:$,finalUrl:$,status:0,error:Q.message,needsBrowser:B}}}function N$($,X,Z,J){let W=X.match(/<title[^>]*>([^<]*)<\/title>/i)?.[1]?.toLowerCase()||"",K=X.slice(0,30000).toLowerCase(),V=`${W} ${K}`;if($===403||$===429||$===503)return{blocked:!0,reason:`HTTP ${$}`};let Y=[{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 Q of Y)if(Q.pattern.test(V))return{blocked:!0,reason:Q.reason};let j=c8(J,Z,X);if(j)return{blocked:!0,reason:j};return{blocked:!1}}function c8($,X,Z){try{let J=new URL($),W=new URL(X);if(J.hostname.toLowerCase()===W.hostname.toLowerCase())return;let K=W.hostname.toLowerCase();if(d8.some((Y)=>K===Y||K.endsWith(`.${Y}`)))return`redirected to login (${W.hostname})`;if(l8.some((Y)=>K.startsWith(Y)))return`redirected to login (${W.hostname})`;let V=Z.slice(0,20000).toLowerCase();if(u8.some((Y)=>V.includes(Y)))return`redirected to login page (${W.hostname})`}catch{}return}function n8($){let X=$.message.toLowerCase();return X.includes("fetch failed")||X.includes("unable to verify")||X.includes("certificate")||X.includes("timeout")}function s1($){let X=['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 Z of X){let J=$.querySelector(Z),W=J?.getAttribute("content")||J?.getAttribute("datetime")||"";if(W)return W}return""}function z$($,X){let J=new h8($,{url:X}).window.document,K=new y8(J).parse();if(K&&K.content){let j=t1.turndown(K.content).replaceAll(/\n{3,}/g,`
219
+ `}function _8($,X){V8(B8($),{recursive:!0});let Z=`${$}.tmp-${process.pid}`;E1(Z,P8(X),"utf8");try{Q8($)}catch{}Y8(Z,$)}async function g1({activePortFile:$,modeFile:X,env:Z=process.env,excludePorts:J=[],autoPorts:W=x1}={}){if(!S1(Z))return null;let K=await A8({env:Z,excludePorts:J,autoPorts:W});if(!K)return null;if(_8($,K),X)E1(X,"existing","utf8");return K}var x1,M8;var y1=c(()=>{x1=[9222,9223],M8=/^(0|false|no|off)$/i});import{execFileSync as h1,execSync as h$}from"node:child_process";import{existsSync as V$,mkdirSync as k8,readFileSync as b0,unlinkSync as k0,writeFileSync as F0}from"node:fs";import{platform as p$,tmpdir as u1}from"node:os";function m0($){if(!Number.isInteger($)||$<=0)return!1;try{return process.kill($,0),!0}catch{return!1}}function L8($){if(!m0($))return null;try{if(p$()==="win32")return h1(p("powershell"),["-NoProfile","-NonInteractive","-Command",`(Get-CimInstance Win32_Process -Filter "ProcessId = ${$}").CommandLine`],{encoding:"utf8",windowsHide:!0,timeout:5000}).trim()||null;return h1(p("ps"),["-p",String($),"-o","command="],{encoding:"utf8",timeout:5000}).trim()||null}catch{return null}}function w8($,X,Z=y){if(!$)return!1;let J=(V)=>String(V||"").replaceAll("\\","/").toLowerCase(),W=J($),K=J(X);return W.includes(K)&&W.includes(`--remote-debugging-port=${Z}`)&&!W.includes("--type=")}function p1($,X,Z=y){return w8(L8($),X,Z)}function T8($=y){try{if(p$()==="win32"){let Z=h$(`${p("netstat")} -ano -p TCP 2>nul`,{encoding:"utf8"}),J=new RegExp(String.raw`TCP\s+\S+:${$}\s+\S+:0\s+LISTENING\s+(\d+)`,"i"),W=Z.match(J);return W?Number.parseInt(W[1],10):null}let X=h$(`${p("lsof")} -i :${$} -t 2>/dev/null || ${p("ss")} -tlnp 2>/dev/null | ${p("grep")} :${$} | ${p("grep")} -oP 'pid=\\K\\d+'`,{encoding:"utf8"}).trim();return X?Number.parseInt(X.split(`
220
+ `)[0],10):null}catch{return null}}function m1($){try{if(p$()==="win32")h$(`${p("taskkill")} /F /T /PID ${$}`,{stdio:"ignore"});else{try{process.kill(-$,"SIGKILL")}catch{}try{process.kill($,"SIGKILL")}catch{}}return!0}catch{return!1}}function l0(){try{if(V$(Y$)){let $=b0(Y$,"utf8"),X=JSON.parse($);if(X&&typeof X.tempDir==="string"&&typeof X.debugPort==="number")return{browserPid:Number.isInteger(X.browserPid)?X.browserPid:void 0,debugPort:X.debugPort,tempDir:X.tempDir,clientPids:Array.isArray(X.clientPids)?X.clientPids.filter((Z)=>Number.isInteger(Z)&&Z>0):[],sessionMode:X.sessionMode==="visible"?"visible":"headless",lastActivity:Number.isFinite(X.lastActivity)?X.lastActivity:0,launchedAt:Number.isFinite(X.launchedAt)?X.launchedAt:0}}}catch{}try{let $=V$(Q$)?Number.parseInt(b0(Q$,"utf8").trim(),10)||void 0:void 0,X=V$(j$)?b0(j$,"utf8").trim()==="visible"?"visible":"headless":"headless",Z=V$(p0)?Number.parseInt(b0(p0,"utf8").trim(),10)||0:0;return{browserPid:$,debugPort:y,tempDir:n,clientPids:$?[$]:[],sessionMode:X,lastActivity:Z,launchedAt:0}}catch{return null}}function B$($){try{F0(Y$,JSON.stringify({browserPid:$.browserPid,debugPort:$.debugPort,tempDir:$.tempDir,clientPids:[...new Set($.clientPids.filter((X)=>X>0))],sessionMode:$.sessionMode,lastActivity:$.lastActivity,launchedAt:$.launchedAt},null,2),"utf8")}catch{}try{if($.browserPid)F0(Q$,String($.browserPid),"utf8")}catch{}try{F0(j$,$.sessionMode,"utf8")}catch{}try{F0(p0,String($.lastActivity),"utf8")}catch{}}function d1(){try{k0(Y$)}catch{}try{k0(Q$)}catch{}try{k0(j$)}catch{}try{k0(p0)}catch{}}function c1($){if(!$)return $;let X={...$,clientPids:[...new Set([...$.clientPids,process.pid].filter((Z)=>m0(Z)||Z===process.pid))]};return B$(X),X}function m$($){let X=Date.now();try{if($)B$({...$,lastActivity:X});else F0(p0,String(X),"utf8")}catch{}}function n1(){if(l1)return;l1=!0;let $=l0();if(!$)return;if($.browserPid){if(!m0($.browserPid)){m1($.browserPid),d1();return}if(!p1($.browserPid,$.tempDir,$.debugPort))d1();else B$({...$,clientPids:$.clientPids.filter((W)=>m0(W))})}let X=T8();if(X&&X!==$.browserPid)if(p1(X,n,y))B$({browserPid:X,debugPort:y,tempDir:n,clientPids:[X],sessionMode:$.sessionMode,lastActivity:Date.now(),launchedAt:Date.now()});else m1(X)}function i1(){k8(u1(),{recursive:!0});try{let $=JSON.stringify({pid:process.pid,ts:Date.now()});return F0(_0,$,{encoding:"utf8",flag:"wx"}),{acquired:!0,release:()=>{try{let X=b0(_0,"utf8");if(JSON.parse(X).pid===process.pid)k0(_0)}catch{}}}}catch($){if($?.code!=="EEXIST")return{acquired:!1,release:()=>{}}}try{let $=b0(_0,"utf8"),X=JSON.parse($),Z=Date.now()-(X.ts||0);if(!m0(X.pid)||Z>F8){try{k0(_0)}catch{}try{let W=JSON.stringify({pid:process.pid,ts:Date.now()});return F0(_0,W,{encoding:"utf8",flag:"wx"}),{acquired:!0,release:()=>{try{k0(_0)}catch{}}}}catch{return{acquired:!1,release:()=>{}}}}}catch{}return{acquired:!1,release:()=>{}}}var d0,Y$,_0,F8=15000,Q$,j$,p0,H4,l1=!1;var o1=c(()=>{V0();v$();d0=u1().replaceAll("\\","/"),Y$=`${d0}/greedysearch-chrome-metadata.json`,_0=`${d0}/greedysearch-chrome-launch.lock`,Q$=`${d0}/greedysearch-chrome.pid`,j$=`${d0}/greedysearch-chrome-mode`,p0=`${d0}/greedysearch-chrome-last-activity`;H4=Number.parseInt(process.env.GREEDY_SEARCH_IDLE_TIMEOUT_MINUTES||"5",10)||5});import{spawn as C8,execFileSync as a1,execSync as u$}from"node:child_process";import{existsSync as n0,readFileSync as H$,renameSync as R8,unlinkSync as Y0,writeFileSync as M$}from"node:fs";import c$ from"node:http";import{platform as n$,tmpdir as I8}from"node:os";import{join as d$}from"node:path";function i$($=q8){let X=[d$($,"launch.mjs"),d$($,"..","bin","launch.mjs"),d$($,"..","..","bin","launch.mjs")];for(let Z of X)if(n0(Z))return Z;return X[X.length-1]}function E8($,X=y){let Z=String($||"").toLowerCase();if(!Z.includes(`--remote-debugging-port=${X}`)||Z.includes("--type="))return null;return Z.includes("--headless")}function t1(){try{let $=o$(),X=$?f8($):null,Z=E8(X);if(Z!==null){try{M$(P0,Z?"headless":"visible","utf8")}catch{}return Z}}catch{}try{if(!n0(P0))return!0;return H$(P0,"utf8").trim()==="headless"}catch{return!0}}function O$(){try{M$(c0,String(Date.now()),"utf8")}catch{}try{let $=l0();if($)m$($)}catch{}}function f8($){try{if(n$()==="win32")return a1(p("powershell"),["-NoProfile","-NonInteractive","-Command",`(Get-CimInstance Win32_Process -Filter "ProcessId = ${$}").CommandLine`],{encoding:"utf8",windowsHide:!0,timeout:5000}).trim()||null;return a1(p("ps"),["-p",String($),"-o","command="],{encoding:"utf8",timeout:5000}).trim()||null}catch{return null}}function o$(){try{if(n$()==="win32"){let X=u$(`${p("netstat")} -ano -p TCP 2>nul`,{encoding:"utf8"}),Z=new RegExp(String.raw`TCP\s+\S+:${y}\s+\S+:0\s+LISTENING\s+(\d+)`,"i"),J=X.match(Z);return J?Number.parseInt(J[1],10):null}let $=u$(`${p("lsof")} -i :${y} -t 2>/dev/null || ${p("ss")} -tlnp 2>/dev/null | ${p("grep")} :${y} | ${p("grep")} -oP 'pid=\\K\\d+'`,{encoding:"utf8"}).trim();return $?Number.parseInt($.split(`
221
+ `)[0],10):null}catch{return null}}async function x8($=1500){try{let Z=await new Promise((W,K)=>{let V=c$.get(`http://localhost:${y}/json/version`,(Y)=>{let Q="";Y.on("data",(j)=>Q+=j),Y.on("end",()=>{try{W(JSON.parse(Q))}catch{K(Error("bad JSON"))}})});V.on("error",K),V.setTimeout(1000,()=>{V.destroy(),K(Error("timeout"))})}),J=new globalThis.WebSocket(Z.webSocketDebuggerUrl);await new Promise((W)=>{J.onopen=()=>{J.send(JSON.stringify({id:1,method:"Browser.close"})),setTimeout(()=>{J.close(),W()},200)},J.onerror=()=>W(),setTimeout(W,1000)})}catch{}let X=Date.now()+$;while(Date.now()<X){if(!o$())return!0;await new Promise((J)=>setTimeout(J,150))}return S8()}function S8(){try{let $=o$();if(!$&&n0(u0))$=Number.parseInt(H$(u0,"utf8").trim(),10)||null;if(!$)return!1;if(n$()==="win32")u$(`${p("taskkill")} /F /PID ${$}`,{stdio:"ignore"});else process.kill($,"SIGKILL");return!0}catch{return!1}}async function r1(){if(!await v0(500)){try{Y0(u0)}catch{}try{Y0(c0)}catch{}try{Y0(P0)}catch{}return!1}let X=await x8(1500);try{Y0(u0)}catch{}try{Y0(c0)}catch{}try{Y0(P0)}catch{}if(X)process.stderr.write(`[greedysearch] Killed Chrome on port ${y}.
222
+ `);return X}async function g8(){let X=t1()?b8:v8;if(X<=0)return!1;if(!n0(c0))return O$(),!1;try{let Z=Number.parseInt(H$(c0,"utf8").trim(),10);if(!Z)return!1;if((Date.now()-Z)/60000>=X)return r1()}catch{}return!1}async function e1(){let X=(await b(["list"])).split(`
223
+ `)[0];if(!X)throw Error("No Chrome tabs found");return X.slice(0,8)}async function Q0($="about:blank"){let X=await e1(),Z=new URL($).hostname;if(Z==="copilot.microsoft.com"||Z==="www.perplexity.ai"||Z==="perplexity.ai"||Z.endsWith(".perplexity.ai")){let V=await b(["evalraw",X,"Target.createTarget",JSON.stringify({url:"about:blank"})]),{targetId:Y}=JSON.parse(V),Q=Y.slice(0,8);if(await b(["list"]).catch(()=>null),Z==="copilot.microsoft.com")await b$(Q);else b$(Q).catch(()=>{});return await b(["list"]).catch(()=>null),Y}let W=await b(["evalraw",X,"Target.createTarget",JSON.stringify({url:$})]),{targetId:K}=JSON.parse(W);return await b(["list"]).catch(()=>null),K}async function O0($){try{let X=await e1();await b(["evalraw",X,"Target.closeTarget",JSON.stringify({targetId:$})])}catch{}}async function G$($=[]){if(await Promise.all($.filter(Boolean).map((X)=>O0(X).catch(()=>{}))),$.length>0)await b(["list"]).catch(()=>null)}function v0($=3000){return new Promise((X)=>{let Z=c$.get(`http://localhost:${y}/json/version`,(J)=>{J.resume(),X(J.statusCode===200)});Z.on("error",()=>X(!1)),Z.setTimeout($,()=>{Z.destroy(),X(!1)})})}async function l$(){let $=`${q0}.lock`,X=`${q0}.tmp`,Z=5000,J=1000,W=await new Promise((K)=>{let V=Date.now(),Y=()=>{try{let Q=JSON.stringify({pid:process.pid,ts:Date.now()});M$($,Q,{encoding:"utf8",flag:"wx"}),K(!0)}catch(Q){if(Q?.code!=="EEXIST"){if(Date.now()-V<1000)setTimeout(Y,50);else K(!1);return}try{let j=H$($,"utf8").trim(),B=j.startsWith("{")?JSON.parse(j):{ts:Number(j)},M=Number(B?.ts)||0;if(M>0&&Date.now()-M>5000)try{Y0($)}catch{}if(Date.now()-V<1000)setTimeout(Y,50);else K(!1)}catch{if(Date.now()-V<1000)setTimeout(Y,50);else K(!1)}}};Y()});try{let K=await new Promise((j,B)=>{let M=c$.get(`http://localhost:${y}/json/version`,(D)=>{let H="";D.on("data",(z)=>H+=z),D.on("end",()=>j(H))});M.on("error",B),M.setTimeout(3000,()=>{M.destroy(),B(Error("timeout"))})}),{webSocketDebuggerUrl:V}=JSON.parse(K),Y=new URL(V),Q=`${Y.pathname}${Y.search}`;if(W){M$(X,`localhost:${y}
224
+ ${Q}`,"utf8");try{Y0(q0)}catch{}R8(X,q0)}}catch{}finally{if(W)try{Y0($)}catch{}}}async function i0(){let $=n0(u0)||!!l0(),X=await g1({activePortFile:q0,modeFile:P0,excludePorts:$?[y]:[]});if(X){process.stderr.write(`[greedysearch] Reusing existing browser CDP endpoint ${X.httpUrl} (${X.source}).
225
+ `);return}n1();let Z=await g8(),J=Z?!1:await v0();if(!J&&!Z)await new Promise((Y)=>setTimeout(Y,500)),J=await v0();let W=!1;if(J){let Y=t1(),Q=process.env.GREEDY_SEARCH_VISIBLE==="1";if(!Q&&!Y)process.stderr.write(`[greedysearch] Visible Chrome detected — switching to headless mode...
226
+ `),await L0(),await new Promise((j)=>setTimeout(j,1000)),W=!0;else if(Q&&Y)process.stderr.write(`[greedysearch] Headless Chrome detected — switching to visible mode...
227
+ `),await L0(),await new Promise((j)=>setTimeout(j,1000)),W=!0}if(W?!1:await v0()){await l$();try{let Y=l0();if(Y)m$(Y),c1(Y)}catch{}return}let V=i1();if(!V.acquired){if(await new Promise((Q)=>setTimeout(Q,3000)),await v0(5000)){await l$();return}}try{if(await v0(1000)){await l$();return}process.stderr.write(`GreedySearch Chrome not running on port ${y} — auto-launching...
228
+ `);let Q=[i$()];if(process.env.GREEDY_SEARCH_VISIBLE!=="1")Q.push("--headless");await new Promise((j,B)=>{C8(H0(),Q,{stdio:["ignore",process.stderr,process.stderr],env:{...process.env,CDP_PROFILE_DIR:n,GREEDY_SEARCH_PROFILE_DIR:n}}).on("close",(D)=>D===0?j():B(Error("launch.mjs failed")))})}finally{V.release()}}var q8,s1,u0,c0,b8,v8,L0,b;var a$=c(()=>{w1();y0();v$();V0();y1();o1();q8=import.meta.dirname||new URL(".",import.meta.url).pathname.replace(/^\/([A-Z]:)/,"$1");s1=I8().replaceAll("\\","/"),u0=`${s1}/greedysearch-chrome.pid`,c0=`${s1}/greedysearch-chrome-last-activity`,b8=Number.parseInt(process.env.GREEDY_SEARCH_IDLE_TIMEOUT_MINUTES||"5",10)||5,v8=Number.parseInt(process.env.GREEDY_SEARCH_VISIBLE_IDLE_TIMEOUT_MINUTES||"60",10)||60;L0=r1;b=q$});var K2={};g0(K2,{shouldUseBrowser:()=>s8,rewriteGitHubUrl:()=>W2,isPrivateUrl:()=>D$,fetchSourceHttp:()=>t$,extractContent:()=>z$,detectBotBlock:()=>N$,defaultFetchHeaders:()=>s$,checkContentQuality:()=>U$});import{Readability as m8}from"@mozilla/readability";import{JSDOM as d8}from"jsdom";import l8 from"turndown";function s$($={}){return{...J2,...$}}function D$($){try{let X=new URL($),Z=X.hostname.toLowerCase();for(let J of u8)if(J.test(Z))return{blocked:!0,reason:`Private/internal address: ${Z}`};if(X.protocol==="file:")return{blocked:!0,reason:"File protocol not allowed"};return{blocked:!1}}catch(X){return{blocked:!0,reason:`Invalid URL: ${X.message}`}}}function W2($){try{let X=new URL($);if(!(X.hostname==="github.com"||X.hostname.endsWith(".github.com")))return $;let Z=X.pathname.split("/").filter(Boolean);if(Z.length<5)return $;let[J,W,K,V,...Y]=Z;if(K!=="blob")return $;let Q=Y.join("/");return`https://raw.githubusercontent.com/${J}/${W}/${V}/${Q}`}catch{return $}}async function t$($,X={}){let Z=D$($);if(Z.blocked)return{ok:!1,url:$,finalUrl:$,status:403,error:`Blocked: ${Z.reason}`,needsBrowser:!1};let J=$;if($=W2($),$!==J)console.error(`[fetcher] Rewrote GitHub URL: ${J.slice(0,60)}... → raw.githubusercontent.com`);let{timeoutMs:W=15000,userAgent:K,signal:V}=X,Y=new AbortController,Q=setTimeout(()=>Y.abort(),W);if(V)V.addEventListener("abort",()=>Y.abort(),{once:!0});try{let j=await fetch($,{method:"GET",headers:{...J2,"user-agent":K||Z2},redirect:"follow",signal:Y.signal});clearTimeout(Q);let B=j.headers.get("content-type")||"",M=j.url,D=j.headers.get("last-modified")||"",H=!1;try{H=new URL(M).hostname.toLowerCase()==="raw.githubusercontent.com"}catch{}if(B.includes("text/plain")&&H){let f=await j.text();return{ok:!0,url:J,finalUrl:M,status:j.status,title:M.split("/").pop()||"GitHub File",byline:"",siteName:"GitHub",lang:"",publishedTime:D,lastModified:D,markdown:f,contentLength:f.length,excerpt:f.slice(0,300).replaceAll(/\n/g," "),needsBrowser:!1}}if(!B.includes("text/html")&&!B.includes("application/xhtml"))return{ok:!1,url:$,finalUrl:M,status:j.status,error:`Unsupported content type: ${B}`,needsBrowser:!1};let z=await j.text(),N=N$(j.status,z,M,$);if(N.blocked)return{ok:!1,url:$,finalUrl:M,status:j.status,error:`Blocked: ${N.reason}`,needsBrowser:!0};let O=z$(z,M),k=U$(O);if(!k.ok)return{ok:!1,url:$,finalUrl:M,status:j.status,error:`Low quality content: ${k.reason}`,needsBrowser:!0};return{ok:!0,url:$,finalUrl:M,status:j.status,title:O.title,byline:O.byline,siteName:O.siteName,lang:O.lang,publishedTime:O.publishedTime||D,lastModified:D,markdown:O.markdown,excerpt:O.excerpt,contentLength:O.markdown.length,needsBrowser:!1}}catch(j){clearTimeout(Q);let B=a8(j);return{ok:!1,url:$,finalUrl:$,status:0,error:j.message,needsBrowser:B}}}function N$($,X,Z,J){let W=X.match(/<title[^>]*>([^<]*)<\/title>/i)?.[1]?.toLowerCase()||"",K=X.slice(0,30000).toLowerCase(),V=`${W} ${K}`;if($===403||$===429||$===503)return{blocked:!0,reason:`HTTP ${$}`};let Y=[{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 j of Y)if(j.pattern.test(V))return{blocked:!0,reason:j.reason};let Q=o8(J,Z,X);if(Q)return{blocked:!0,reason:Q};return{blocked:!1}}function o8($,X,Z){try{let J=new URL($),W=new URL(X);if(J.hostname.toLowerCase()===W.hostname.toLowerCase())return;let K=W.hostname.toLowerCase();if(c8.some((Y)=>K===Y||K.endsWith(`.${Y}`)))return`redirected to login (${W.hostname})`;if(n8.some((Y)=>K.startsWith(Y)))return`redirected to login (${W.hostname})`;let V=Z.slice(0,20000).toLowerCase();if(i8.some((Y)=>V.includes(Y)))return`redirected to login page (${W.hostname})`}catch{}return}function a8($){let X=$.message.toLowerCase();return X.includes("fetch failed")||X.includes("unable to verify")||X.includes("certificate")||X.includes("timeout")}function $2($){let X=['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 Z of X){let J=$.querySelector(Z),W=J?.getAttribute("content")||J?.getAttribute("datetime")||"";if(W)return W}return""}function z$($,X){let J=new d8($,{url:X}).window.document,K=new m8(J).parse();if(K&&K.content){let Q=X2.turndown(K.content).replaceAll(/\n{3,}/g,`
229
229
 
230
- `).trim(),Q=K.publishedTime||s1(J)||"";return{title:K.title||J.title||X,byline:K.byline||"",siteName:K.siteName||"",lang:K.lang||"",publishedTime:Q,markdown:j,excerpt:j.slice(0,300).replaceAll(/\n/g," ")}}let V=J.body;if(V){let Y=V.cloneNode(!0);Y.querySelectorAll("script, style, nav, footer, header, aside").forEach((B)=>B.remove());let Q=(Y.textContent||"").replaceAll(/\s+/g," ").trim();return{title:J.title||X,byline:"",siteName:"",lang:"",publishedTime:s1(J),markdown:Q,excerpt:Q.slice(0,300)}}return{title:X,byline:"",siteName:"",lang:"",publishedTime:"",markdown:"",excerpt:""}}function U$($){let X=$.markdown.trim().toLowerCase(),Z=($.title||"").toLowerCase();if($.markdown.trim().length<100)return{ok:!1,reason:"content too short (< 100 chars)"};let J=X.toLowerCase(),W=[{check:()=>J.includes("loading")&&J.includes("please wait"),desc:"loading page"},{check:()=>J.includes("please ensure javascript is enabled"),desc:"requires javascript"},{check:()=>J.includes("enable javascript to view"),desc:"requires javascript"},{check:()=>J.includes("just a moment"),desc:"cloudflare challenge detected in content"},{check:()=>J.includes("verify you are human"),desc:"human verification"},{check:()=>J.includes("captcha required"),desc:"captcha in extracted content"},{check:()=>J.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(X),desc:"login form only"}];for(let{check:K,desc:V}of W)if(K())return{ok:!1,reason:V};if(Z.includes("just a moment")||Z.includes("checking your browser"))return{ok:!1,reason:"cloudflare challenge page detected in title"};return{ok:!0}}function i8($){try{let X=new URL($),Z=X.hostname.toLowerCase(),J=X.pathname.toLowerCase();if(["react.dev","nextjs.org","vuejs.org","angular.io","svelte.dev","docs.expo.dev","tailwindcss.com","storybook.js.org"].some((K)=>Z===K||Z.endsWith(`.${K}`)))return!0;if(J.includes("/playground")||J.includes("/demo")||J.includes("/app"))return!0;if(X.hash&&X.hash.length>1)return!0;return!1}catch{return!1}}var t1,r1="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36",e1,m8,d8,l8,u8;var s$=c(()=>{t1=new p8({headingStyle:"atx",bulletListMarker:"-",codeBlockStyle:"fenced"});t1.addRule("removeDataUrls",{filter:($)=>$.tagName==="IMG"&&$.getAttribute("src")?.startsWith("data:"),replacement:()=>""});e1={"user-agent":r1,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"},m8=[/^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];d8=["accounts.google.com","login.microsoftonline.com","login.live.com","auth0.com","okta.com","auth.mozilla.auth0.com","id.atlassian.com"],l8=["login.","signin.","auth.","sso.","accounts.","idp."],u8=["sign in to continue","log in to continue","authentication required","create an account to continue","subscribe to continue reading","members only"]});var r$={};g0(r$,{parseGitHubUrl:()=>i0,fetchGitHubContent:()=>t$});function i0($){try{let X=new URL($);if(!(X.hostname==="github.com"||X.hostname.endsWith(".github.com")))return null;let Z=X.pathname.split("/").filter(Boolean);if(Z.length<2)return null;let[J,W]=Z;if(Z.length===2)return{owner:J,repo:W,type:"root"};if(Z.length>=4&&(Z[2]==="blob"||Z[2]==="tree")){let K=Z[2],V=Z[3],Y=Z.slice(4).join("/");return{owner:J,repo:W,type:K,ref:V,path:Y}}return null}catch{return null}}async function O0($,X=1e4){let Z=new AbortController,J=setTimeout(()=>Z.abort(),X);try{let W=await fetch(`https://api.github.com${$}`,{headers:J2,signal:Z.signal});if(clearTimeout(J),!W.ok)throw Error(`GitHub API ${W.status}: ${$}`);return await W.json()}catch(W){throw clearTimeout(J),W}}async function o8($,X){try{let Z=await O0(`/repos/${$}/${X}/readme`);if(Z.content&&Z.encoding==="base64")return Buffer.from(Z.content,"base64").toString("utf8");return""}catch{return""}}async function Z2($,X,Z="HEAD",J=""){try{let W=await O0(`/repos/${$}/${X}/git/ref/heads/${Z==="HEAD"?"main":Z}`).catch(()=>O0(`/repos/${$}/${X}/git/ref/heads/master`).catch(()=>null)),K;if(W?.object?.sha)K=(await O0(`/repos/${$}/${X}/git/commits/${W.object.sha}`)).tree.sha;else{let j=await O0(`/repos/${$}/${X}`);K=(await O0(`/repos/${$}/${X}/branches/${j.default_branch}`)).commit.commit.tree.sha}let Y=(await O0(`/repos/${$}/${X}/git/trees/${K}`)).tree||[];if(J)Y=Y.filter((j)=>j.path.startsWith(J));return Y.slice(0,50).map((j)=>({path:j.path,type:j.type==="tree"?"dir":"file",size:j.size}))}catch{return[]}}async function a8($,X,Z,J,W=1e4){let V=[`https://raw.githubusercontent.com/${$}/${X}/${Z&&Z!=="HEAD"?Z:"main"}/${J}`,`https://raw.githubusercontent.com/${$}/${X}/master/${J}`];for(let Y of V){let j=new AbortController,Q=setTimeout(()=>j.abort(),W);try{let B=await fetch(Y,{headers:{"user-agent":J2["user-agent"]},signal:j.signal});if(clearTimeout(Q),B.ok)return await B.text()}catch{clearTimeout(Q)}}return null}async function t$($){let X=i0($);if(!X)return{ok:!1,error:"Not a valid GitHub URL"};let{owner:Z,repo:J,type:W,ref:K,path:V}=X;try{if(W==="root"||W==="tree"&&!V){let[Y,j,Q]=await Promise.allSettled([O0(`/repos/${Z}/${J}`),o8(Z,J),Z2(Z,J,K||"HEAD")]);if(Y.status==="rejected")return{ok:!1,error:Y.reason?.message||"Repo not found"};let B=Y.value,M=j.status==="fulfilled"?j.value:"",D=Q.status==="fulfilled"?Q.value:[],H=B?.description?`
230
+ `).trim(),j=K.publishedTime||$2(J)||"";return{title:K.title||J.title||X,byline:K.byline||"",siteName:K.siteName||"",lang:K.lang||"",publishedTime:j,markdown:Q,excerpt:Q.slice(0,300).replaceAll(/\n/g," ")}}let V=J.body;if(V){let Y=V.cloneNode(!0);Y.querySelectorAll("script, style, nav, footer, header, aside").forEach((B)=>B.remove());let j=(Y.textContent||"").replaceAll(/\s+/g," ").trim();return{title:J.title||X,byline:"",siteName:"",lang:"",publishedTime:$2(J),markdown:j,excerpt:j.slice(0,300)}}return{title:X,byline:"",siteName:"",lang:"",publishedTime:"",markdown:"",excerpt:""}}function U$($){let X=$.markdown.trim().toLowerCase(),Z=($.title||"").toLowerCase();if($.markdown.trim().length<100)return{ok:!1,reason:"content too short (< 100 chars)"};let J=X.toLowerCase(),W=[{check:()=>J.includes("loading")&&J.includes("please wait"),desc:"loading page"},{check:()=>J.includes("please ensure javascript is enabled"),desc:"requires javascript"},{check:()=>J.includes("enable javascript to view"),desc:"requires javascript"},{check:()=>J.includes("just a moment"),desc:"cloudflare challenge detected in content"},{check:()=>J.includes("verify you are human"),desc:"human verification"},{check:()=>J.includes("captcha required"),desc:"captcha in extracted content"},{check:()=>J.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(X),desc:"login form only"}];for(let{check:K,desc:V}of W)if(K())return{ok:!1,reason:V};if(Z.includes("just a moment")||Z.includes("checking your browser"))return{ok:!1,reason:"cloudflare challenge page detected in title"};return{ok:!0}}function s8($){try{let X=new URL($),Z=X.hostname.toLowerCase(),J=X.pathname.toLowerCase();if(["react.dev","nextjs.org","vuejs.org","angular.io","svelte.dev","docs.expo.dev","tailwindcss.com","storybook.js.org"].some((K)=>Z===K||Z.endsWith(`.${K}`)))return!0;if(J.includes("/playground")||J.includes("/demo")||J.includes("/app"))return!0;if(X.hash&&X.hash.length>1)return!0;return!1}catch{return!1}}var X2,Z2="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36",J2,u8,c8,n8,i8;var r$=c(()=>{X2=new l8({headingStyle:"atx",bulletListMarker:"-",codeBlockStyle:"fenced"});X2.addRule("removeDataUrls",{filter:($)=>$.tagName==="IMG"&&$.getAttribute("src")?.startsWith("data:"),replacement:()=>""});J2={"user-agent":Z2,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"},u8=[/^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];c8=["accounts.google.com","login.microsoftonline.com","login.live.com","auth0.com","okta.com","auth.mozilla.auth0.com","id.atlassian.com"],n8=["login.","signin.","auth.","sso.","accounts.","idp."],i8=["sign in to continue","log in to continue","authentication required","create an account to continue","subscribe to continue reading","members only"]});var $1={};g0($1,{parseGitHubUrl:()=>o0,fetchGitHubContent:()=>e$});function o0($){try{let X=new URL($);if(!(X.hostname==="github.com"||X.hostname.endsWith(".github.com")))return null;let Z=X.pathname.split("/").filter(Boolean);if(Z.length<2)return null;let[J,W]=Z;if(Z.length===2)return{owner:J,repo:W,type:"root"};if(Z.length>=4&&(Z[2]==="blob"||Z[2]==="tree")){let K=Z[2],V=Z[3],Y=Z.slice(4).join("/");return{owner:J,repo:W,type:K,ref:V,path:Y}}return null}catch{return null}}async function G0($,X=1e4){let Z=new AbortController,J=setTimeout(()=>Z.abort(),X);try{let W=await fetch(`https://api.github.com${$}`,{headers:Y2,signal:Z.signal});if(clearTimeout(J),!W.ok)throw Error(`GitHub API ${W.status}: ${$}`);return await W.json()}catch(W){throw clearTimeout(J),W}}async function t8($,X){try{let Z=await G0(`/repos/${$}/${X}/readme`);if(Z.content&&Z.encoding==="base64")return Buffer.from(Z.content,"base64").toString("utf8");return""}catch{return""}}async function V2($,X,Z="HEAD",J=""){try{let W=await G0(`/repos/${$}/${X}/git/ref/heads/${Z==="HEAD"?"main":Z}`).catch(()=>G0(`/repos/${$}/${X}/git/ref/heads/master`).catch(()=>null)),K;if(W?.object?.sha)K=(await G0(`/repos/${$}/${X}/git/commits/${W.object.sha}`)).tree.sha;else{let Q=await G0(`/repos/${$}/${X}`);K=(await G0(`/repos/${$}/${X}/branches/${Q.default_branch}`)).commit.commit.tree.sha}let Y=(await G0(`/repos/${$}/${X}/git/trees/${K}`)).tree||[];if(J)Y=Y.filter((Q)=>Q.path.startsWith(J));return Y.slice(0,50).map((Q)=>({path:Q.path,type:Q.type==="tree"?"dir":"file",size:Q.size}))}catch{return[]}}async function r8($,X,Z,J,W=1e4){let V=[`https://raw.githubusercontent.com/${$}/${X}/${Z&&Z!=="HEAD"?Z:"main"}/${J}`,`https://raw.githubusercontent.com/${$}/${X}/master/${J}`];for(let Y of V){let Q=new AbortController,j=setTimeout(()=>Q.abort(),W);try{let B=await fetch(Y,{headers:{"user-agent":Y2["user-agent"]},signal:Q.signal});if(clearTimeout(j),B.ok)return await B.text()}catch{clearTimeout(j)}}return null}async function e$($){let X=o0($);if(!X)return{ok:!1,error:"Not a valid GitHub URL"};let{owner:Z,repo:J,type:W,ref:K,path:V}=X;try{if(W==="root"||W==="tree"&&!V){let[Y,Q,j]=await Promise.allSettled([G0(`/repos/${Z}/${J}`),t8(Z,J),V2(Z,J,K||"HEAD")]);if(Y.status==="rejected")return{ok:!1,error:Y.reason?.message||"Repo not found"};let B=Y.value,M=Q.status==="fulfilled"?Q.value:"",D=j.status==="fulfilled"?j.value:[],H=B?.description?`
231
231
 
232
- > ${B.description}`:"",z=B?.stargazers_count==null?"":` ⭐ ${B.stargazers_count}`,N=B?.language?` · ${B.language}`:"",G=`# ${Z}/${J}${z}${N}${H}
232
+ > ${B.description}`:"",z=B?.stargazers_count==null?"":` ⭐ ${B.stargazers_count}`,N=B?.language?` · ${B.language}`:"",O=`# ${Z}/${J}${z}${N}${H}
233
233
 
234
- `;if(M)G+=M.slice(0,6000);else G+=`[No README found]
234
+ `;if(M)O+=M.slice(0,6000);else O+=`[No README found]
235
235
 
236
236
  Files:
237
237
  ${D.map((k)=>` ${k.type==="dir"?"\uD83D\uDCC1":"\uD83D\uDCC4"} ${k.path}`).join(`
238
- `)}`;return{ok:!0,title:`${Z}/${J}`,content:G,tree:D.slice(0,30)}}if(W==="blob"&&V){let Y=await a8(Z,J,K,V);if(Y===null)return{ok:!1,error:`File not found: ${V}`};return{ok:!0,title:`${Z}/${J}: ${V}`,content:Y}}if(W==="tree"&&V){let Y=await Z2(Z,J,K||"HEAD",V),j=Y.map((Q)=>` ${Q.type==="dir"?"\uD83D\uDCC1":"\uD83D\uDCC4"} ${Q.path}`).join(`
238
+ `)}`;return{ok:!0,title:`${Z}/${J}`,content:O,tree:D.slice(0,30)}}if(W==="blob"&&V){let Y=await r8(Z,J,K,V);if(Y===null)return{ok:!1,error:`File not found: ${V}`};return{ok:!0,title:`${Z}/${J}: ${V}`,content:Y}}if(W==="tree"&&V){let Y=await V2(Z,J,K||"HEAD",V),Q=Y.map((j)=>` ${j.type==="dir"?"\uD83D\uDCC1":"\uD83D\uDCC4"} ${j.path}`).join(`
239
239
  `);return{ok:!0,title:`${Z}/${J}/${V}`,content:`[Directory: ${V}]
240
240
 
241
241
  Files:
242
- ${j}`,tree:Y}}return{ok:!1,error:"Unsupported GitHub URL type"}}catch(Y){return{ok:!1,error:Y.message}}}var J2;var A$=c(()=>{J2={"user-agent":"GreedySearch/1.0",accept:"application/vnd.github+json","x-github-api-version":"2022-11-28"}});function K2($){try{let X=new URL($),Z=X.hostname.toLowerCase();if(!(Z==="reddit.com"||Z.endsWith(".reddit.com")))return null;let J=X.pathname;if(J.match(/^\/(u|user)\/[^/]+\/?$/i))return{type:"user",cleanUrl:W2($)};if(J.match(/^\/r\/[^/]+\/comments\/[^/]+/i))return{type:"post",cleanUrl:W2($)};return null}catch{return null}}function W2($){try{let X=new URL($);return`${X.protocol}//${X.hostname}${X.pathname}`}catch{return $}}async function V2($,X=8000){let Z=Date.now();try{let J=$.replaceAll(/\/?$/g,".json"),W=new AbortController,K=setTimeout(()=>W.abort(),15000),V=await fetch(J,{headers:s8,signal:W.signal});if(clearTimeout(K),!V.ok)throw Error(`Reddit API ${V.status}`);let Y=await V.json();if(!Array.isArray(Y)||Y.length<1)throw Error("Invalid Reddit API response structure");let j=Y[0],Q=Y[1],B=j?.data?.children?.[0]?.data;if(!B)throw Error("No post data in Reddit response");let M=t8(B,Q,X);return{ok:!0,url:$,finalUrl:$,status:200,contentType:"text/markdown",lastModified:"",title:B.title||"Reddit Post",byline:`u/${B.author}`,siteName:`r/${B.subreddit}`,lang:"en",publishedTime:new Date(B.created_utc*1000).toISOString(),excerpt:B.selftext?.slice(0,300).replace(/\n/g," ")||"",markdown:M,contentLength:M.length,needsBrowser:!1,duration:Date.now()-Z}}catch(J){return{ok:!1,url:$,finalUrl:$,status:0,error:`Reddit fetch failed: ${J.message}`,needsBrowser:!1,duration:Date.now()-Z}}}function t8($,X,Z){let J="";if(J+=`# ${$.title}
242
+ ${Q}`,tree:Y}}return{ok:!1,error:"Unsupported GitHub URL type"}}catch(Y){return{ok:!1,error:Y.message}}}var Y2;var A$=c(()=>{Y2={"user-agent":"GreedySearch/1.0",accept:"application/vnd.github+json","x-github-api-version":"2022-11-28"}});function j2($){try{let X=new URL($),Z=X.hostname.toLowerCase();if(!(Z==="reddit.com"||Z.endsWith(".reddit.com")))return null;let J=X.pathname;if(J.match(/^\/(u|user)\/[^/]+\/?$/i))return{type:"user",cleanUrl:Q2($)};if(J.match(/^\/r\/[^/]+\/comments\/[^/]+/i))return{type:"post",cleanUrl:Q2($)};return null}catch{return null}}function Q2($){try{let X=new URL($);return`${X.protocol}//${X.hostname}${X.pathname}`}catch{return $}}async function B2($,X=8000){let Z=Date.now();try{let J=$.replaceAll(/\/?$/g,".json"),W=new AbortController,K=setTimeout(()=>W.abort(),15000),V=await fetch(J,{headers:e8,signal:W.signal});if(clearTimeout(K),!V.ok)throw Error(`Reddit API ${V.status}`);let Y=await V.json();if(!Array.isArray(Y)||Y.length<1)throw Error("Invalid Reddit API response structure");let Q=Y[0],j=Y[1],B=Q?.data?.children?.[0]?.data;if(!B)throw Error("No post data in Reddit response");let M=$6(B,j,X);return{ok:!0,url:$,finalUrl:$,status:200,contentType:"text/markdown",lastModified:"",title:B.title||"Reddit Post",byline:`u/${B.author}`,siteName:`r/${B.subreddit}`,lang:"en",publishedTime:new Date(B.created_utc*1000).toISOString(),excerpt:B.selftext?.slice(0,300).replace(/\n/g," ")||"",markdown:M,contentLength:M.length,needsBrowser:!1,duration:Date.now()-Z}}catch(J){return{ok:!1,url:$,finalUrl:$,status:0,error:`Reddit fetch failed: ${J.message}`,needsBrowser:!1,duration:Date.now()-Z}}}function $6($,X,Z){let J="";if(J+=`# ${$.title}
243
243
 
244
244
  `,J+=`**Subreddit:** r/${$.subreddit} | **Author:** u/${$.author} | **Score:** ${$.score}
245
245
 
@@ -253,27 +253,27 @@ ${j}`,tree:Y}}return{ok:!1,error:"Unsupported GitHub URL type"}}catch(Y){return{
253
253
 
254
254
  ## Comments
255
255
 
256
- `;let W=X.data.children.filter((K)=>K.kind==="t1").slice(0,10);for(let K of W)J+=Y2(K.data,0),J+=`
256
+ `;let W=X.data.children.filter((K)=>K.kind==="t1").slice(0,10);for(let K of W)J+=M2(K.data,0),J+=`
257
257
  `}if(J.length>Z)J=J.slice(0,Z).trim()+`
258
258
 
259
- ... (truncated)`;return J}function Y2($,X){if(!$||$.body==="[deleted]"||$.body==="[removed]")return"";let Z="> ".repeat(X),J="";if(J+=`${Z}**u/${$.author}** (${$.score} pts)
259
+ ... (truncated)`;return J}function M2($,X){if(!$||$.body==="[deleted]"||$.body==="[removed]")return"";let Z="> ".repeat(X),J="";if(J+=`${Z}**u/${$.author}** (${$.score} pts)
260
260
  `,J+=`${Z}${$.body.replaceAll(`
261
261
  `,`
262
262
  `+Z)}
263
263
  `,X<3&&$.replies?.data?.children){let W=$.replies.data.children.filter((K)=>K.kind==="t1");for(let K of W.slice(0,5))J+=`
264
- `+Y2(K.data,X+1)}return J}var s8;var j2=c(()=>{s8={"user-agent":"GreedySearch/1.0 (Research Bot)",accept:"application/json"}});var $1={};g0($1,{truncateContent:()=>r8,trimContentHeadTail:()=>D0});function D0($,X=8000){if(!$||$.length<=X)return $;let Z=`
264
+ `+M2(K.data,X+1)}return J}var e8;var H2=c(()=>{e8={"user-agent":"GreedySearch/1.0 (Research Bot)",accept:"application/json"}});var Z1={};g0(Z1,{truncateContent:()=>X6,trimContentHeadTail:()=>D0});function D0($,X=8000){if(!$||$.length<=X)return $;let Z=`
265
265
 
266
266
  [...content trimmed...]
267
267
 
268
268
  `,J=X-Z.length,W=Math.floor(J*0.75),K=J-W,V=W;while(V>W-100&&$[V]!==`
269
269
  `)V--;if(V<=W-100)V=W;let Y=$.length-K;while(Y<$.length-K+100&&$[Y]!==`
270
- `)Y++;if(Y>=$.length-K+100)Y=$.length-K;let j=$.slice(0,V).trimEnd(),Q=$.slice(Y).trimStart();return`${j}${Z}${Q}`}function r8($,X=8000){if(!$||$.length<=X)return $;let Z=$.slice(0,X),J=Z.lastIndexOf(" ");return J>0?`${Z.slice(0,J)}...`:`${Z}...`}function e8(){if(typeof globalThis.DOMMatrix>"u")globalThis.DOMMatrix=class{constructor(X=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(X=void 0,Z=0,J=0){this.data=X,this.width=Z,this.height=J}};if(typeof globalThis.Path2D>"u")globalThis.Path2D=class{constructor(X=void 0){}}}async function $6(){e8();let $=await import("pdf-parse"),X=$.PDFParse??$.default;if(!X)throw Error("pdf-parse did not export PDFParse");return X}async function Q2($,X){try{let J=new(await $6())({data:new Uint8Array($)});await J.load();let W=await J.getText(),K=W.text?.trim();if(!K)return null;return{title:new URL(X).pathname.split("/").pop()||"Document.pdf",content:`## PDF Content (${W.total} pages)
270
+ `)Y++;if(Y>=$.length-K+100)Y=$.length-K;let Q=$.slice(0,V).trimEnd(),j=$.slice(Y).trimStart();return`${Q}${Z}${j}`}function X6($,X=8000){if(!$||$.length<=X)return $;let Z=$.slice(0,X),J=Z.lastIndexOf(" ");return J>0?`${Z.slice(0,J)}...`:`${Z}...`}function Z6(){if(typeof globalThis.DOMMatrix>"u")globalThis.DOMMatrix=class{constructor(X=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(X=void 0,Z=0,J=0){this.data=X,this.width=Z,this.height=J}};if(typeof globalThis.Path2D>"u")globalThis.Path2D=class{constructor(X=void 0){}}}async function J6(){Z6();let $=await import("pdf-parse"),X=$.PDFParse??$.default;if(!X)throw Error("pdf-parse did not export PDFParse");return X}async function O2($,X){try{let J=new(await J6())({data:new Uint8Array($)});await J.load();let W=await J.getText(),K=W.text?.trim();if(!K)return null;return{title:new URL(X).pathname.split("/").pop()||"Document.pdf",content:`## PDF Content (${W.total} pages)
271
271
 
272
- ${K}`,pages:W.total}}catch(Z){return{error:Z.message||String(Z)}}}function I($="",X=240){let Z=String($).replaceAll(/\s+/g," ").trim();if(Z.length<=X)return Z;let J=Z.slice(0,X),W=J.lastIndexOf(" ");return W>0?`${J.slice(0,W)}...`:`${J}...`}function X1($=""){let X=I($,180);if(!X)return"";if(/^https?:\/\//i.test(X))return"";let Z=X.split(/\s+/).filter(Boolean).length,J=/[A-Z]/.test(X),W=/\d/.test(X);return X===X.toLowerCase()&&Z<=4&&!J&&!W?"":X}function Z1($="",X=""){let Z=X1($),J=X1(X);if(!J)return Z;if(!Z)return J;let W=/^https?:\/\//i.test(Z),K=/^https?:\/\//i.test(J);if(W&&!K)return J;if(!W&&K)return Z;return J.length>Z.length?J:Z}function $0($){if(!$)return null;try{let X=new URL($);if(!["http:","https:"].includes(X.protocol))return null;if(X.hash="",X.hostname=X.hostname.toLowerCase(),X.protocol==="https:"&&X.port==="443"||X.protocol==="http:"&&X.port==="80")X.port="";for(let W of[...X.searchParams.keys()]){let K=W.toLowerCase();if(X6.includes(K)||K.startsWith("utm_"))X.searchParams.delete(W)}X.searchParams.sort();let Z=X.pathname.replace(/\/{1,10}$/,"")||"/";X.pathname=Z;let J=X.toString();return Z==="/"?J.replace(/\/$/,""):J}catch{return null}}function W6($){try{return new URL($).hostname.toLowerCase().replace(/^www\./,"")}catch{return""}}function o0($,X){return X.some((Z)=>$===Z||$.endsWith(`.${Z}`))}function J1($,X="",Z=""){let J=X.toLowerCase(),W=Z.toLowerCase();if($==="github.com"||$==="gitlab.com")return"repo";if($==="arxiv.org"||$==="doi.org"||$==="semanticscholar.org"||$.endsWith(".semanticscholar.org")||W.includes("/paper/")||W.includes("/pdf/"))return"academic";if(o0($,M2))return"social";if(o0($,Z6))return"community";if(o0($,J6))return"news";if($.startsWith("docs.")||$.startsWith("developer.")||$.startsWith("developers.")||$.startsWith("api.")||J.includes("documentation")||J.includes("docs")||J.includes("reference")||W.includes("/docs/")||W.includes("/reference/")||W.includes("/api/"))return"official-docs";if($.startsWith("blog.")||W.includes("/blog/"))return"maintainer-blog";return"website"}function K6($){switch($){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 V6($){let X=Object.values($.perEngine||{}).map((Z)=>Z?.rank||99);return X.length?Math.min(...X):99}function w0($){return $.smartScore*3+$.engineCount*5+K6($.sourceType)*2+Math.max(0,7-V6($))}function j6($){let X=$.toLowerCase(),Z=[];if(X.includes("openai")||X.includes("gpt")||X.includes("chatgpt"))Z.push("openai.com","platform.openai.com","help.openai.com");if(X.includes("anthropic")||X.includes("claude"))Z.push("anthropic.com","docs.anthropic.com");if(X.includes("bun"))Z.push("bun.sh","bun.com");if(X.includes("next.js")||X.includes("nextjs"))Z.push("nextjs.org","vercel.com");if(X.includes("playwright"))Z.push("playwright.dev");if(X.includes("supabase"))Z.push("supabase.com","supabase.io");if(X.includes("prisma"))Z.push("prisma.io");if(X.includes("tailwind"))Z.push("tailwindcss.com");if(X.includes("vite"))Z.push("vitejs.dev","vite.dev");if(X.includes("astro"))Z.push("astro.build");if(X.includes("svelte"))Z.push("svelte.dev");if(X.includes("solid"))Z.push("solidjs.com");if(X.includes("vue")||X.includes("nuxt"))Z.push("vuejs.org","nuxt.com");if(X.includes("react")||X.includes("react native"))Z.push("react.dev","reactnative.dev");if(X.includes("angular"))Z.push("angular.io","angular.dev");if(X.includes("node.js")||X.includes("nodejs"))Z.push("nodejs.org","nodejs.dev","npmjs.com");if(/\bgo\b/.test(X)||X.includes("golang"))Z.push("go.dev","golang.org","pkg.go.dev");if(X.includes("deno"))Z.push("deno.land","deno.com");if(X.includes("fresh"))Z.push("fresh.deno.dev");if(X.includes("typescript")||X.includes("ts"))Z.push("typescriptlang.org");if(X.includes("python"))Z.push("python.org","docs.python.org");if(X.includes("rust"))Z.push("rust-lang.org","docs.rs","crates.io");if(X.includes("zig"))Z.push("ziglang.org");if(X.includes("docker"))Z.push("docker.com","docs.docker.com","hub.docker.com");if(X.includes("kubernetes")||X.includes("k8s"))Z.push("kubernetes.io","k8s.io");if(X.includes("postgres")||X.includes("postgresql"))Z.push("postgresql.org","neon.tech","supabase.com");if(X.includes("redis"))Z.push("redis.io");if(X.includes("sqlite"))Z.push("sqlite.org");if(X.includes("cloudflare"))Z.push("developers.cloudflare.com","cloudflare.com");if(X.includes("vercel"))Z.push("vercel.com","nextjs.org");if(X.includes("netlify"))Z.push("netlify.com","docs.netlify.com");if(X.includes("stripe"))Z.push("stripe.com","docs.stripe.com");if(X.includes("github"))Z.push("github.com","docs.github.com");if(X.includes("gitlab"))Z.push("gitlab.com","docs.gitlab.com");if(X.includes("aws"))Z.push("aws.amazon.com","docs.aws.amazon.com");if(X.includes("azure"))Z.push("azure.microsoft.com","learn.microsoft.com");if(X.includes("gcp")||X.includes("google cloud"))Z.push("cloud.google.com","developers.google.com");if(X.includes("gemini")||X.includes("google ai"))Z.push("ai.google.dev","developers.google.com");for(let J of M2){let W=J.replace(/\.com$/,"");if(X.includes(W))Z.push(J)}return[...new Set(Z)]}function B2($,X){return $===X||$.endsWith(`.${X}`)}function f0($,X=""){let Z=new Map,J=Object.keys($||{}).filter((Q)=>!Q.startsWith("_")),W=j6(X);for(let Q of J){let B=$[Q];if(!B?.sources)continue;for(let M=0;M<B.sources.length;M++){let D=B.sources[M],H=$0(D.url);if(!H||H.length<10)continue;let z=X1(D.title||""),N=W6(H),G=J1(N,z,H),k=0;if(W.some((f)=>B2(N,f)))k+=10;if(G==="official-docs")k+=3;let E=H.toLowerCase();if(/\/docs\/|\/documentation\/|\.dev\/|\/api\/|\/reference\//.test(E))k+=2;let P=W.some((f)=>B2(N,f));if(G==="social"&&!P)k-=20;if(W.length>0){if(o0(N,Y6))k-=3;else if(G==="community"&&!o0(N,["stackoverflow.com","stackexchange.com"]))k-=1}let L=Z.get(H)||{id:"",canonicalUrl:H,displayUrl:D.url||H,domain:N,title:"",engines:[],engineCount:0,perEngine:{},sourceType:G,isOfficial:G==="official-docs",smartScore:0};if(L.title=Z1(L.title,z),L.displayUrl=L.displayUrl||D.url||H,L.sourceType=L.sourceType||G,L.isOfficial=L.isOfficial||G==="official-docs",L.smartScore=Math.max(L.smartScore,k),!L.engines.includes(Q))L.engines.push(Q);L.perEngine[Q]={rank:M+1,title:Z1(L.perEngine[Q]?.title||"",z)},Z.set(H,L)}}let K=Array.from(Z.values()).map((Q)=>({...Q,engineCount:Q.engines.length})),V=K.filter((Q)=>Q.sourceType!=="social"),Y=K.filter((Q)=>Q.sourceType==="social");return V.sort((Q,B)=>{let M=w0(B)-w0(Q);if(M!==0)return M;return Q.domain.localeCompare(B.domain)}),Y.sort((Q,B)=>{let M=w0(B)-w0(Q);if(M!==0)return M;return Q.domain.localeCompare(B.domain)}),[...V,...Y].slice(0,12).map((Q,B)=>({...Q,id:`S${B+1}`,title:Q.title||Q.domain||Q.canonicalUrl}))}function P$($,X){let Z=new Map(X.map((J)=>[J.id,J]));return $.map((J)=>{let W=Z.get(J.id);if(!W)return J;let K=Z1(J.title,W.title||"");return{...J,title:K||J.title,fetch:{attempted:!0,ok:!W.error&&W.contentChars>100,status:W.status||null,finalUrl:W.finalUrl||W.url||J.canonicalUrl,contentType:W.contentType||"",lastModified:W.lastModified||"",publishedTime:W.publishedTime||"",byline:W.byline||"",siteName:W.siteName||"",lang:W.lang||"",title:W.title||"",snippet:W.snippet||"",contentChars:W.contentChars||0,source:W.source||"unknown",duration:W.duration||0,error:W.error||""}}})}var X6,Z6,J6,M2,Y6;var x0=c(()=>{X6=["fbclid","gclid","ref","ref_src","ref_url","source","utm_campaign","utm_content","utm_medium","utm_source","utm_term"],Z6=["dev.to","hashnode.com","medium.com","reddit.com","stackoverflow.com","stackexchange.com","substack.com"],J6=["arstechnica.com","techcrunch.com","theverge.com","venturebeat.com","wired.com","zdnet.com"],M2=["facebook.com","instagram.com","linkedin.com","pinterest.com","tiktok.com","twitter.com","x.com"];Y6=["reddit.com","news.ycombinator.com","lobste.rs"]});var O2={};g0(O2,{fetchTopSource:()=>a0,fetchSourceContent:()=>G2,fetchMultipleSources:()=>W1});async function Q6($,X,Z=8000){let J=Date.now();try{let K=(await b(["evalraw",$,"Page.getFrameTree","{}"]).then((z)=>JSON.parse(z)).catch(()=>null))?.frameTree?.frame?.id||void 0,V=await b(["evalraw",$,"Network.loadNetworkResource",JSON.stringify({frameId:K,url:X,options:{disableCache:!0,includeCredentials:!1}})],20000),j=JSON.parse(V).resource;if(!j?.success||!j.httpStatusCode)return{url:X,error:j?.netErrorName||j?.netError||"loadNetworkResource failed",source:"chrome",duration:Date.now()-J,needsFallback:!0};let Q="";if(j.stream)try{let z=await b(["evalraw",$,"IO.read",JSON.stringify({handle:j.stream})],1e4);Q=JSON.parse(z).data||"",await b(["evalraw",$,"IO.close",JSON.stringify({handle:j.stream})]).catch(()=>{})}catch{}if(!Q||Q.length<100)return{url:X,error:"Empty response body from Network.loadNetworkResource",source:"chrome",duration:Date.now()-J,needsFallback:!0};let B=N$(j.httpStatusCode,Q,X,X);if(B.blocked)return{url:X,status:j.httpStatusCode,error:`Blocked: ${B.reason}`,source:"chrome",duration:Date.now()-J,needsBrowser:!0};let M=z$(Q,X),D=U$(M);if(!D.ok)return{url:X,status:j.httpStatusCode,error:`Low quality: ${D.reason}`,source:"chrome",duration:Date.now()-J,needsBrowser:!0};let H=D0(M.markdown,Z);return{url:X,finalUrl:X,status:j.httpStatusCode,contentType:"text/markdown",lastModified:"",publishedTime:M.publishedTime||"",byline:M.byline||"",siteName:M.siteName||"",lang:M.lang||"",title:M.title||X,snippet:M.excerpt,content:H,contentChars:H.length,source:"chrome",duration:Date.now()-J}}catch(W){return{url:X,error:W.message,source:"chrome",duration:Date.now()-J,needsFallback:!0}}}function H2($){try{return new URL($).pathname.toLowerCase().endsWith(".pdf")}catch{return!1}}async function B6($,X=8000){let Z=D$($);if(Z.blocked)return{url:$,finalUrl:$,status:403,error:`Blocked: ${Z.reason}`,source:"pdf-http"};let J=new AbortController,W=setTimeout(()=>J.abort(),20000),K=Date.now();try{let V=await fetch($,{method:"GET",redirect:"follow",signal:J.signal,headers:o$({accept:"application/pdf,application/octet-stream;q=0.9,*/*;q=0.5"})});clearTimeout(W);let Y=V.headers.get("content-type")||"",j=V.url||$,Q=Number.parseInt(V.headers.get("content-length")||"0",10);if(V.status>=400)return{url:$,finalUrl:j,status:V.status,error:`HTTP ${V.status}`,source:"pdf-http",duration:Date.now()-K};if(!Y.toLowerCase().includes("application/pdf")&&!H2(j))return null;if(Q>31457280)return{url:$,finalUrl:j,status:V.status,error:`PDF too large: ${Q} bytes`,source:"pdf-http",duration:Date.now()-K};let B=Buffer.from(await V.arrayBuffer()),M=await Q2(B,j);if(!M||M.error)return{url:$,finalUrl:j,status:V.status,error:M?.error||"PDF text extraction failed",source:"pdf-http",duration:Date.now()-K};let D=D0(M.content,X);return{url:$,finalUrl:j,status:V.status,contentType:"application/pdf",lastModified:V.headers.get("last-modified")||"",title:M.title,snippet:I(D,320),content:D,contentChars:D.length,pages:M.pages,source:"pdf-http",duration:Date.now()-K}}catch(V){return clearTimeout(W),{url:$,finalUrl:$,error:V.message||String(V),source:"pdf-http",duration:Date.now()-K}}}async function G2($,X=8000){let Z=Date.now();if(H2($)){let K=await B6($,X);if(K?.content||K?.status===403)return K}if(i0($)){let K=i0($);if(K&&(K.type==="root"||K.type==="tree"||K.type==="blob"&&!K.path?.includes("."))){let V=await t$($);if(V.ok){let Y=D0(V.content,X);return{url:$,finalUrl:$,status:200,contentType:"text/markdown",lastModified:"",title:V.title,snippet:Y.slice(0,320),content:Y,contentChars:Y.length,source:"github-api",...V.tree&&{tree:V.tree},duration:Date.now()-Z}}process.stderr.write(`[greedysearch] GitHub API fetch failed, trying HTTP: ${V.error}
273
- `)}}if(K2($)?.type==="post"){process.stderr.write(`[greedysearch] Using Reddit JSON API for: ${$.slice(0,60)}...
274
- `);let K=await V2($,X);if(K.ok){let V=D0(K.markdown,X);return{url:$,finalUrl:K.finalUrl,status:K.status,contentType:"text/markdown",lastModified:K.lastModified||"",publishedTime:K.publishedTime||"",byline:K.byline||"",siteName:K.siteName||"",lang:K.lang||"",title:K.title,snippet:K.excerpt,content:V,contentChars:V.length,source:"reddit-api",duration:Date.now()-Z}}process.stderr.write(`[greedysearch] Reddit API fetch failed, falling back to HTTP: ${K.error}
275
- `)}let W=await a$($,{timeoutMs:1e4});if(W.ok){let K=D0(W.markdown,X);return{url:$,finalUrl:W.finalUrl,status:W.status,contentType:"text/markdown",lastModified:W.lastModified||"",publishedTime:W.publishedTime||"",byline:W.byline||"",siteName:W.siteName||"",lang:W.lang||"",title:W.title,snippet:W.excerpt,content:K,contentChars:K.length,source:"http",duration:Date.now()-Z}}if(W.needsBrowser)try{let K=await j0();try{let V=await Q6(K,$,X);if(V.content&&V.content.length>100)return V}finally{await G0(K)}}catch{}return process.stderr.write(`[greedysearch] HTTP failed for ${$.slice(0,60)}, trying browser...
276
- `),await M6($,X)}async function M6($,X=8000){let Z=Date.now(),J;try{J=await j0()}catch(W){return{url:$,title:"",content:null,snippet:"",contentChars:0,error:`openNewTab failed: ${W.message}`,source:"browser",duration:Date.now()-Z}}try{await b(["nav",J,$],30000),await new Promise((Y)=>setTimeout(Y,800));let W=await b(["eval",J,String.raw`
272
+ ${K}`,pages:W.total}}catch(Z){return{error:Z.message||String(Z)}}}function I($="",X=240){let Z=String($).replaceAll(/\s+/g," ").trim();if(Z.length<=X)return Z;let J=Z.slice(0,X),W=J.lastIndexOf(" ");return W>0?`${J.slice(0,W)}...`:`${J}...`}function J1($=""){let X=I($,180);if(!X)return"";if(/^https?:\/\//i.test(X))return"";let Z=X.split(/\s+/).filter(Boolean).length,J=/[A-Z]/.test(X),W=/\d/.test(X);return X===X.toLowerCase()&&Z<=4&&!J&&!W?"":X}function W1($="",X=""){let Z=J1($),J=J1(X);if(!J)return Z;if(!Z)return J;let W=/^https?:\/\//i.test(Z),K=/^https?:\/\//i.test(J);if(W&&!K)return J;if(!W&&K)return Z;return J.length>Z.length?J:Z}function $0($){if(!$)return null;try{let X=new URL($);if(!["http:","https:"].includes(X.protocol))return null;if(X.hash="",X.hostname=X.hostname.toLowerCase(),X.protocol==="https:"&&X.port==="443"||X.protocol==="http:"&&X.port==="80")X.port="";for(let W of[...X.searchParams.keys()]){let K=W.toLowerCase();if(W6.includes(K)||K.startsWith("utm_"))X.searchParams.delete(W)}X.searchParams.sort();let Z=X.pathname.replace(/\/{1,10}$/,"")||"/";X.pathname=Z;let J=X.toString();return Z==="/"?J.replace(/\/$/,""):J}catch{return null}}function Y6($){try{return new URL($).hostname.toLowerCase().replace(/^www\./,"")}catch{return""}}function a0($,X){return X.some((Z)=>$===Z||$.endsWith(`.${Z}`))}function K1($,X="",Z=""){let J=X.toLowerCase(),W=Z.toLowerCase();if($==="github.com"||$==="gitlab.com")return"repo";if($==="arxiv.org"||$==="doi.org"||$==="semanticscholar.org"||$.endsWith(".semanticscholar.org")||W.includes("/paper/")||W.includes("/pdf/"))return"academic";if(a0($,D2))return"social";if(a0($,K6))return"community";if(a0($,V6))return"news";if($.startsWith("docs.")||$.startsWith("developer.")||$.startsWith("developers.")||$.startsWith("api.")||J.includes("documentation")||J.includes("docs")||J.includes("reference")||W.includes("/docs/")||W.includes("/reference/")||W.includes("/api/"))return"official-docs";if($.startsWith("blog.")||W.includes("/blog/"))return"maintainer-blog";return"website"}function Q6($){switch($){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 j6($){let X=Object.values($.perEngine||{}).map((Z)=>Z?.rank||99);return X.length?Math.min(...X):99}function w0($){return $.smartScore*3+$.engineCount*5+Q6($.sourceType)*2+Math.max(0,7-j6($))}function M6($){let X=$.toLowerCase(),Z=[];if(X.includes("openai")||X.includes("gpt")||X.includes("chatgpt"))Z.push("openai.com","platform.openai.com","help.openai.com");if(X.includes("anthropic")||X.includes("claude"))Z.push("anthropic.com","docs.anthropic.com");if(X.includes("bun"))Z.push("bun.sh","bun.com");if(X.includes("next.js")||X.includes("nextjs"))Z.push("nextjs.org","vercel.com");if(X.includes("playwright"))Z.push("playwright.dev");if(X.includes("supabase"))Z.push("supabase.com","supabase.io");if(X.includes("prisma"))Z.push("prisma.io");if(X.includes("tailwind"))Z.push("tailwindcss.com");if(X.includes("vite"))Z.push("vitejs.dev","vite.dev");if(X.includes("astro"))Z.push("astro.build");if(X.includes("svelte"))Z.push("svelte.dev");if(X.includes("solid"))Z.push("solidjs.com");if(X.includes("vue")||X.includes("nuxt"))Z.push("vuejs.org","nuxt.com");if(X.includes("react")||X.includes("react native"))Z.push("react.dev","reactnative.dev");if(X.includes("angular"))Z.push("angular.io","angular.dev");if(X.includes("node.js")||X.includes("nodejs"))Z.push("nodejs.org","nodejs.dev","npmjs.com");if(/\bgo\b/.test(X)||X.includes("golang"))Z.push("go.dev","golang.org","pkg.go.dev");if(X.includes("deno"))Z.push("deno.land","deno.com");if(X.includes("fresh"))Z.push("fresh.deno.dev");if(X.includes("typescript")||X.includes("ts"))Z.push("typescriptlang.org");if(X.includes("python"))Z.push("python.org","docs.python.org");if(X.includes("rust"))Z.push("rust-lang.org","docs.rs","crates.io");if(X.includes("zig"))Z.push("ziglang.org");if(X.includes("docker"))Z.push("docker.com","docs.docker.com","hub.docker.com");if(X.includes("kubernetes")||X.includes("k8s"))Z.push("kubernetes.io","k8s.io");if(X.includes("postgres")||X.includes("postgresql"))Z.push("postgresql.org","neon.tech","supabase.com");if(X.includes("redis"))Z.push("redis.io");if(X.includes("sqlite"))Z.push("sqlite.org");if(X.includes("cloudflare"))Z.push("developers.cloudflare.com","cloudflare.com");if(X.includes("vercel"))Z.push("vercel.com","nextjs.org");if(X.includes("netlify"))Z.push("netlify.com","docs.netlify.com");if(X.includes("stripe"))Z.push("stripe.com","docs.stripe.com");if(X.includes("github"))Z.push("github.com","docs.github.com");if(X.includes("gitlab"))Z.push("gitlab.com","docs.gitlab.com");if(X.includes("aws"))Z.push("aws.amazon.com","docs.aws.amazon.com");if(X.includes("azure"))Z.push("azure.microsoft.com","learn.microsoft.com");if(X.includes("gcp")||X.includes("google cloud"))Z.push("cloud.google.com","developers.google.com");if(X.includes("gemini")||X.includes("google ai"))Z.push("ai.google.dev","developers.google.com");for(let J of D2){let W=J.replace(/\.com$/,"");if(X.includes(W))Z.push(J)}return[...new Set(Z)]}function G2($,X){return $===X||$.endsWith(`.${X}`)}function f0($,X=""){let Z=new Map,J=Object.keys($||{}).filter((j)=>!j.startsWith("_")),W=M6(X);for(let j of J){let B=$[j];if(!B?.sources)continue;for(let M=0;M<B.sources.length;M++){let D=B.sources[M],H=$0(D.url);if(!H||H.length<10)continue;let z=J1(D.title||""),N=Y6(H),O=K1(N,z,H),k=0;if(W.some((x)=>G2(N,x)))k+=10;if(O==="official-docs")k+=3;let f=H.toLowerCase();if(/\/docs\/|\/documentation\/|\.dev\/|\/api\/|\/reference\//.test(f))k+=2;let P=W.some((x)=>G2(N,x));if(O==="social"&&!P)k-=20;if(W.length>0){if(a0(N,B6))k-=3;else if(O==="community"&&!a0(N,["stackoverflow.com","stackexchange.com"]))k-=1}let L=Z.get(H)||{id:"",canonicalUrl:H,displayUrl:D.url||H,domain:N,title:"",engines:[],engineCount:0,perEngine:{},sourceType:O,isOfficial:O==="official-docs",smartScore:0};if(L.title=W1(L.title,z),L.displayUrl=L.displayUrl||D.url||H,L.sourceType=L.sourceType||O,L.isOfficial=L.isOfficial||O==="official-docs",L.smartScore=Math.max(L.smartScore,k),!L.engines.includes(j))L.engines.push(j);L.perEngine[j]={rank:M+1,title:W1(L.perEngine[j]?.title||"",z)},Z.set(H,L)}}let K=Array.from(Z.values()).map((j)=>({...j,engineCount:j.engines.length})),V=K.filter((j)=>j.sourceType!=="social"),Y=K.filter((j)=>j.sourceType==="social");return V.sort((j,B)=>{let M=w0(B)-w0(j);if(M!==0)return M;return j.domain.localeCompare(B.domain)}),Y.sort((j,B)=>{let M=w0(B)-w0(j);if(M!==0)return M;return j.domain.localeCompare(B.domain)}),[...V,...Y].slice(0,12).map((j,B)=>({...j,id:`S${B+1}`,title:j.title||j.domain||j.canonicalUrl}))}function P$($,X){let Z=new Map(X.map((J)=>[J.id,J]));return $.map((J)=>{let W=Z.get(J.id);if(!W)return J;let K=W1(J.title,W.title||"");return{...J,title:K||J.title,fetch:{attempted:!0,ok:!W.error&&W.contentChars>100,status:W.status||null,finalUrl:W.finalUrl||W.url||J.canonicalUrl,contentType:W.contentType||"",lastModified:W.lastModified||"",publishedTime:W.publishedTime||"",byline:W.byline||"",siteName:W.siteName||"",lang:W.lang||"",title:W.title||"",snippet:W.snippet||"",contentChars:W.contentChars||0,source:W.source||"unknown",duration:W.duration||0,error:W.error||""}}})}var W6,K6,V6,D2,B6;var x0=c(()=>{W6=["fbclid","gclid","ref","ref_src","ref_url","source","utm_campaign","utm_content","utm_medium","utm_source","utm_term"],K6=["dev.to","hashnode.com","medium.com","reddit.com","stackoverflow.com","stackexchange.com","substack.com"],V6=["arstechnica.com","techcrunch.com","theverge.com","venturebeat.com","wired.com","zdnet.com"],D2=["facebook.com","instagram.com","linkedin.com","pinterest.com","tiktok.com","twitter.com","x.com"];B6=["reddit.com","news.ycombinator.com","lobste.rs"]});var U2={};g0(U2,{fetchTopSource:()=>s0,fetchSourceContent:()=>z2,fetchMultipleSources:()=>V1});async function H6($,X,Z=8000){let J=Date.now();try{let K=(await b(["evalraw",$,"Page.getFrameTree","{}"]).then((z)=>JSON.parse(z)).catch(()=>null))?.frameTree?.frame?.id||void 0,V=await b(["evalraw",$,"Network.loadNetworkResource",JSON.stringify({frameId:K,url:X,options:{disableCache:!0,includeCredentials:!1}})],20000),Q=JSON.parse(V).resource;if(!Q?.success||!Q.httpStatusCode)return{url:X,error:Q?.netErrorName||Q?.netError||"loadNetworkResource failed",source:"chrome",duration:Date.now()-J,needsFallback:!0};let j="";if(Q.stream)try{let z=await b(["evalraw",$,"IO.read",JSON.stringify({handle:Q.stream})],1e4);j=JSON.parse(z).data||"",await b(["evalraw",$,"IO.close",JSON.stringify({handle:Q.stream})]).catch(()=>{})}catch{}if(!j||j.length<100)return{url:X,error:"Empty response body from Network.loadNetworkResource",source:"chrome",duration:Date.now()-J,needsFallback:!0};let B=N$(Q.httpStatusCode,j,X,X);if(B.blocked)return{url:X,status:Q.httpStatusCode,error:`Blocked: ${B.reason}`,source:"chrome",duration:Date.now()-J,needsBrowser:!0};let M=z$(j,X),D=U$(M);if(!D.ok)return{url:X,status:Q.httpStatusCode,error:`Low quality: ${D.reason}`,source:"chrome",duration:Date.now()-J,needsBrowser:!0};let H=D0(M.markdown,Z);return{url:X,finalUrl:X,status:Q.httpStatusCode,contentType:"text/markdown",lastModified:"",publishedTime:M.publishedTime||"",byline:M.byline||"",siteName:M.siteName||"",lang:M.lang||"",title:M.title||X,snippet:M.excerpt,content:H,contentChars:H.length,source:"chrome",duration:Date.now()-J}}catch(W){return{url:X,error:W.message,source:"chrome",duration:Date.now()-J,needsFallback:!0}}}function N2($){try{return new URL($).pathname.toLowerCase().endsWith(".pdf")}catch{return!1}}async function O6($,X=8000){let Z=D$($);if(Z.blocked)return{url:$,finalUrl:$,status:403,error:`Blocked: ${Z.reason}`,source:"pdf-http"};let J=new AbortController,W=setTimeout(()=>J.abort(),20000),K=Date.now();try{let V=await fetch($,{method:"GET",redirect:"follow",signal:J.signal,headers:s$({accept:"application/pdf,application/octet-stream;q=0.9,*/*;q=0.5"})});clearTimeout(W);let Y=V.headers.get("content-type")||"",Q=V.url||$,j=Number.parseInt(V.headers.get("content-length")||"0",10);if(V.status>=400)return{url:$,finalUrl:Q,status:V.status,error:`HTTP ${V.status}`,source:"pdf-http",duration:Date.now()-K};if(!Y.toLowerCase().includes("application/pdf")&&!N2(Q))return null;if(j>31457280)return{url:$,finalUrl:Q,status:V.status,error:`PDF too large: ${j} bytes`,source:"pdf-http",duration:Date.now()-K};let B=Buffer.from(await V.arrayBuffer()),M=await O2(B,Q);if(!M||M.error)return{url:$,finalUrl:Q,status:V.status,error:M?.error||"PDF text extraction failed",source:"pdf-http",duration:Date.now()-K};let D=D0(M.content,X);return{url:$,finalUrl:Q,status:V.status,contentType:"application/pdf",lastModified:V.headers.get("last-modified")||"",title:M.title,snippet:I(D,320),content:D,contentChars:D.length,pages:M.pages,source:"pdf-http",duration:Date.now()-K}}catch(V){return clearTimeout(W),{url:$,finalUrl:$,error:V.message||String(V),source:"pdf-http",duration:Date.now()-K}}}async function z2($,X=8000){let Z=Date.now();if(N2($)){let K=await O6($,X);if(K?.content||K?.status===403)return K}if(o0($)){let K=o0($);if(K&&(K.type==="root"||K.type==="tree"||K.type==="blob"&&!K.path?.includes("."))){let V=await e$($);if(V.ok){let Y=D0(V.content,X);return{url:$,finalUrl:$,status:200,contentType:"text/markdown",lastModified:"",title:V.title,snippet:Y.slice(0,320),content:Y,contentChars:Y.length,source:"github-api",...V.tree&&{tree:V.tree},duration:Date.now()-Z}}process.stderr.write(`[greedysearch] GitHub API fetch failed, trying HTTP: ${V.error}
273
+ `)}}if(j2($)?.type==="post"){process.stderr.write(`[greedysearch] Using Reddit JSON API for: ${$.slice(0,60)}...
274
+ `);let K=await B2($,X);if(K.ok){let V=D0(K.markdown,X);return{url:$,finalUrl:K.finalUrl,status:K.status,contentType:"text/markdown",lastModified:K.lastModified||"",publishedTime:K.publishedTime||"",byline:K.byline||"",siteName:K.siteName||"",lang:K.lang||"",title:K.title,snippet:K.excerpt,content:V,contentChars:V.length,source:"reddit-api",duration:Date.now()-Z}}process.stderr.write(`[greedysearch] Reddit API fetch failed, falling back to HTTP: ${K.error}
275
+ `)}let W=await t$($,{timeoutMs:1e4});if(W.ok){let K=D0(W.markdown,X);return{url:$,finalUrl:W.finalUrl,status:W.status,contentType:"text/markdown",lastModified:W.lastModified||"",publishedTime:W.publishedTime||"",byline:W.byline||"",siteName:W.siteName||"",lang:W.lang||"",title:W.title,snippet:W.excerpt,content:K,contentChars:K.length,source:"http",duration:Date.now()-Z}}if(W.needsBrowser)try{let K=await Q0();try{let V=await H6(K,$,X);if(V.content&&V.content.length>100)return V}finally{await O0(K)}}catch{}return process.stderr.write(`[greedysearch] HTTP failed for ${$.slice(0,60)}, trying browser...
276
+ `),await G6($,X)}async function G6($,X=8000){let Z=Date.now(),J;try{J=await Q0()}catch(W){return{url:$,title:"",content:null,snippet:"",contentChars:0,error:`openNewTab failed: ${W.message}`,source:"browser",duration:Date.now()-Z}}try{await b(["nav",J,$],30000),await new Promise((Y)=>setTimeout(Y,800));let W=await b(["eval",J,String.raw`
277
277
  (function(){
278
278
  var el = document.querySelector('article, [role="main"], main, .post-content, .article-body, #content, .content');
279
279
  var text = (el || document.body).innerText;
@@ -283,19 +283,19 @@ ${K}`,pages:W.total}}catch(Z){return{error:Z.message||String(Z)}}}function I($="
283
283
  url: location.href
284
284
  });
285
285
  })()
286
- `]),K=JSON.parse(W),V=D0(K.content,X);return{url:$,finalUrl:K.url||$,status:200,contentType:"text/plain",lastModified:"",title:K.title,snippet:I(V,320),content:V,contentChars:V.length,source:"browser",duration:Date.now()-Z}}catch(W){return{url:$,title:"",content:null,snippet:"",contentChars:0,error:W.message,source:"browser",duration:Date.now()-Z}}finally{await G0(J)}}async function W1($,X=5,Z=8000,J=g$){let W=$.slice(0,X);if(W.length===0)return[];let K=Math.min(W.length,Math.max(1,Number.parseInt(String(J),10)||g$));process.stderr.write(`[greedysearch] Fetching content from ${W.length} sources via HTTP (concurrency ${K})...
287
- `);let V=Array(W.length),Y=0,j=0;async function Q(){while(!0){let H=Y++;if(H>=W.length)return;let z=W[H],N=z.canonicalUrl||z.url;process.stderr.write(`[greedysearch] [${H+1}/${W.length}] Fetching: ${N.slice(0,60)}...
288
- `);let G=await G2(N,Z).catch((k)=>({url:N,title:"",content:null,snippet:"",contentChars:0,error:k.message,source:"error",duration:0}));if(V[H]={id:z.id,...G},G.content&&G.content.length>100)process.stderr.write(`[greedysearch] ✓ ${G.source}: ${G.content.length} chars
289
- `);else if(G.error)process.stderr.write(`[greedysearch] ✗ ${G.error.slice(0,80)}
290
- `);j+=1,process.stderr.write(`PROGRESS:fetch:${j}/${W.length}
291
- `)}}await Promise.all(Array.from({length:K},()=>Q()));let B=V.filter((H)=>H.content&&H.content.length>100),M=V.filter((H)=>H.source==="http").length,D=V.filter((H)=>H.source==="browser").length;return process.stderr.write(`[greedysearch] Fetched ${B.length}/${V.length} sources (HTTP: ${M}, Browser: ${D})
292
- `),V}async function a0($){let X=await j0();await b(["list"]);try{await b(["nav",X,$],30000),await new Promise((J)=>setTimeout(J,800));let Z=await b(["eval",X,String.raw`
286
+ `]),K=JSON.parse(W),V=D0(K.content,X);return{url:$,finalUrl:K.url||$,status:200,contentType:"text/plain",lastModified:"",title:K.title,snippet:I(V,320),content:V,contentChars:V.length,source:"browser",duration:Date.now()-Z}}catch(W){return{url:$,title:"",content:null,snippet:"",contentChars:0,error:W.message,source:"browser",duration:Date.now()-Z}}finally{await O0(J)}}async function V1($,X=5,Z=8000,J=g$){let W=$.slice(0,X);if(W.length===0)return[];let K=Math.min(W.length,Math.max(1,Number.parseInt(String(J),10)||g$));process.stderr.write(`[greedysearch] Fetching content from ${W.length} sources via HTTP (concurrency ${K})...
287
+ `);let V=Array(W.length),Y=0,Q=0;async function j(){while(!0){let H=Y++;if(H>=W.length)return;let z=W[H],N=z.canonicalUrl||z.url;process.stderr.write(`[greedysearch] [${H+1}/${W.length}] Fetching: ${N.slice(0,60)}...
288
+ `);let O=await z2(N,Z).catch((k)=>({url:N,title:"",content:null,snippet:"",contentChars:0,error:k.message,source:"error",duration:0}));if(V[H]={id:z.id,...O},O.content&&O.content.length>100)process.stderr.write(`[greedysearch] ✓ ${O.source}: ${O.content.length} chars
289
+ `);else if(O.error)process.stderr.write(`[greedysearch] ✗ ${O.error.slice(0,80)}
290
+ `);Q+=1,process.stderr.write(`PROGRESS:fetch:${Q}/${W.length}
291
+ `)}}await Promise.all(Array.from({length:K},()=>j()));let B=V.filter((H)=>H.content&&H.content.length>100),M=V.filter((H)=>H.source==="http").length,D=V.filter((H)=>H.source==="browser").length;return process.stderr.write(`[greedysearch] Fetched ${B.length}/${V.length} sources (HTTP: ${M}, Browser: ${D})
292
+ `),V}async function s0($){let X=await Q0();await b(["list"]);try{await b(["nav",X,$],30000),await new Promise((J)=>setTimeout(J,800));let Z=await b(["eval",X,String.raw`
293
293
  (function(){
294
294
  var el = document.querySelector('article, [role="main"], main, .post-content, .article-body, #content, .content');
295
295
  var text = (el || document.body).innerText;
296
296
  return text.replace(/\s+/g, ' ').trim();
297
297
  })()
298
- `]);return{url:$,content:Z}}catch(Z){return{url:$,content:null,error:Z.message}}finally{await G0(X)}}var K1=c(()=>{s$();A$();j2();i$();V0();x0()});var N2={};g0(N2,{writeSourcesToFiles:()=>V1});import{mkdirSync as H6,writeFileSync as G6}from"node:fs";import{join as D2}from"node:path";function V1($,X=O6){return H6(X,{recursive:!0}),$.map((Z)=>{if(!Z.content||Z.content.length<10)return Z;let J=String(Z.id||"unknown").replace(/[^a-zA-Z0-9_-]/g,""),W=(Z.canonicalUrl||Z.url||"").replace(/^https?:\/\//,"").replace(/[^a-zA-Z0-9]/g,"-").slice(0,40),K=`${J}-${W}.md`,V=D2(X,K),Y=`---
298
+ `]);return{url:$,content:Z}}catch(Z){return{url:$,content:null,error:Z.message}}finally{await O0(X)}}var Y1=c(()=>{r$();A$();H2();a$();V0();x0()});var P2={};g0(P2,{writeSourcesToFiles:()=>Q1});import{mkdirSync as D6,writeFileSync as N6}from"node:fs";import{join as A2}from"node:path";function Q1($,X=z6){return D6(X,{recursive:!0}),$.map((Z)=>{if(!Z.content||Z.content.length<10)return Z;let J=String(Z.id||"unknown").replace(/[^a-zA-Z0-9_-]/g,""),W=(Z.canonicalUrl||Z.url||"").replace(/^https?:\/\//,"").replace(/[^a-zA-Z0-9]/g,"-").slice(0,40),K=`${J}-${W}.md`,V=A2(X,K),Y=`---
299
299
  url: ${Z.finalUrl||Z.url}
300
300
  title: ${Z.title||""}
301
301
  source: ${Z.source||"unknown"}
@@ -303,19 +303,19 @@ status: ${Z.status||""}
303
303
  chars: ${Z.contentChars||Z.content.length}
304
304
  ---
305
305
 
306
- `;G6(V,Y+Z.content,"utf8");let{content:j,...Q}=Z;return{...Q,contentPath:V,contentChars:Z.contentChars||j.length}})}var O6;var Y1=c(()=>{O6=D2(process.cwd(),".dm","greedysearch-sources")});i$();V0();import{appendFileSync as L9,existsSync as w9,readFileSync as T9}from"node:fs";import{homedir as C9}from"node:os";import{join as x2}from"node:path";y0();V0();import{spawn as x8}from"node:child_process";import{join as S8}from"node:path";var g8=import.meta.dirname||new URL(".",import.meta.url).pathname.replace(/^\/([A-Z]:)/,"$1");function E0($,X,Z=null,J=!1,W=null,K=null){if(W===null)W=$.includes("logically")?120000:$.includes("chatgpt")?80000:$.includes("gemini")?70000:60000;let V=[...Z?["--tab",Z]:[],...J?["--short"]:[],...K?["--locale",K]:[]];return new Promise((Y,j)=>{let Q=x8(H0(),[S8(g8,"..","..","extractors",$),"--stdin",...V],{stdio:["pipe","pipe","pipe"],env:{...process.env,CDP_PROFILE_DIR:n}});Q.stdin.write(X),Q.stdin.end();let B="",M="";Q.stdout.on("data",(H)=>B+=H),Q.stderr.on("data",(H)=>{if(M+=H,process.env.GREEDY_SEARCH_CHILD_STDERR!=="0")process.stderr.write(H)});let D=setTimeout(()=>{Q.kill();let H=(G,k=20)=>String(G??"").split(/\r?\n/).filter(Boolean).slice(-k).join(`
307
- `),z=null;try{let G=JSON.parse(B.trim());if(G._envelope)z=G._envelope}catch{}let N=Error(`${$} timed out after ${W/1000}s`+(z?.lastStage?` (last stage: ${z.lastStage})`:""));N.engineScript=$,N.lastStage=z?.lastStage||null,N.partialErr=H(M),N.partialOut=H(B),j(N)},W);Q.on("close",(H)=>{if(clearTimeout(D),H===0)try{Y(JSON.parse(B.trim()))}catch{j(Error(`bad JSON from ${$}: ${B.slice(0,100)}`))}else{let z=null;try{let k=JSON.parse(B.trim());if(k._envelope)z=k._envelope}catch{}let N=M.trim()||`extractor exit ${H}`,G=Error(N);if(z)G.envelope=z;j(G)}})})}K1();Y1();import{mkdirSync as D6,readdirSync as N6,rmSync as z6,statSync as U6,writeFileSync as j1}from"node:fs";import{join as _$}from"node:path";var A6=import.meta.dirname||new URL(".",import.meta.url).pathname.replace(/^\/([A-Z]:)/,"$1");function P6($){return $.toLowerCase().replaceAll(/[^a-z0-9]+/g,"-").replaceAll(/^-|-$/g,"").slice(0,60)}var _6=604800000,k6=10;function F6($){try{let X=N6($).filter((J)=>J.endsWith(".json")||J.endsWith(".md")).map((J)=>({f:J,mtime:U6(_$($,J)).mtimeMs})).sort((J,W)=>W.mtime-J.mtime),Z=Date.now()-_6;for(let J=k6;J<X.length;J++)if(X[J].mtime<Z)z6(_$($,X[J].f),{force:!0})}catch{}}function L6(){let $=_$(A6,"..","..","results");return D6($,{recursive:!0}),F6($),$}function S0($,X,{inline:Z=!1,synthesize:J=!1,query:W=""}={}){let K=`${JSON.stringify($,null,2)}
308
- `;if(X){j1(X,K,"utf8"),process.stderr.write(`Results written to ${X}
309
- `);return}if(Z){process.stdout.write(K);return}let V=new Date().toISOString().replaceAll("T","_").replaceAll(/[:.]/g,"-").slice(0,19),Y=P6(W),j=_$(L6(),`${V}_${Y}`);if(j1(`${j}.json`,K,"utf8"),J&&$._synthesis?.answer)j1(`${j}-synthesis.md`,$._synthesis.answer,"utf8"),process.stdout.write(`${j}-synthesis.md
310
- `);else process.stdout.write(`${j}.json
311
- `)}var w6=["perplexity","bing","chatgpt","semantic-scholar","logically"],T6=/timed out|timeout|verification|captcha|cloudflare|turnstile|input not found|ask-input|clipboard|copy button hidden|sign.in|login required/i,C6=/needs-human|verification required|please solve|captcha|cloudflare|turnstile|could not be completed automatically|manual intervention|sign.in|login required/i;function z2($){return T6.test(String($||""))}function Q1($){return C6.test(String($||""))}function U2($){return w6.filter((X)=>{let Z=$?.[X];if(!Z)return!1;if(Z._envelope?.blockedBy)return!0;if(Z._envelope?.verificationResult==="needs-human")return!0;let J=Z.error;return J&&z2(J)})}function A2($){if(!$)return!1;let X=$.envelope;if(X?.blockedBy)return!0;if(X?.verificationResult==="needs-human")return!0;return z2($.message)}x0();V0();x0();function R6($){let X="",Z=!1,J=!1;for(let W of String($)){if(J){X+=W,J=!1;continue}if(W==="\\"){X+=W,J=!0;continue}if(W==='"'){Z=!Z,X+=W;continue}if(Z&&W===`
312
- `)X+="\\n";else if(Z&&W==="\r")X+="\\r";else if(Z&&W==="\t")X+="\\t";else X+=W}return X}function s0($){if(!$)return null;let X=String($).trim(),Z=X.indexOf("BEGIN_JSON"),J=X.indexOf("END_JSON");if(Z!==-1&&J!==-1&&Z<J)X=X.slice(Z+10,J).trim();else{let Y=X.indexOf("{");if(Y>0)X=X.slice(Y)}let W=[X,X.replace(/^```json\s*/i,"").replace(/^```\s*/i,"").replace(/```$/i,"").trim()],K=X.indexOf("{"),V=X.lastIndexOf("}");if(K!==-1&&V!==-1&&K<V)W.push(X.slice(K,V+1));for(let Y of[...W]){let j=R6(Y);if(j!==Y)W.push(j)}for(let Y of W)try{return JSON.parse(Y)}catch{}return null}function P2($,X,Z=""){let J=new Set(X.map((j)=>j.id)),W=["high","medium","low","mixed","conflicting"].includes($?.agreement?.level)?$.agreement.level:"mixed",K=Array.isArray($?.claims)?$.claims.map((j)=>({claim:I(j?.claim||"",260),support:["strong","moderate","weak","conflicting"].includes(j?.support)?j.support:"moderate",sourceIds:Array.isArray(j?.sourceIds)?j.sourceIds.filter((Q)=>J.has(Q)):[]})).filter((j)=>j.claim):[],V=Array.isArray($?.recommendedSources)?$.recommendedSources.filter((j)=>J.has(j)).slice(0,6):[],Y="";if(Z){let j=Z.indexOf("{"),Q=Z.lastIndexOf("}");if(j!==-1&&Q!==-1&&j<Q)Y=Z.slice(j,Q+1);else Y=Z}return{answer:I($?.answer||Y||Z,4000),agreement:{level:W,summary:I($?.agreement?.summary||"",280)},differences:Array.isArray($?.differences)?$.differences.map((j)=>I(j,220)).filter(Boolean).slice(0,5):[],caveats:Array.isArray($?.caveats)?$.caveats.map((j)=>I(j,220)).filter(Boolean).slice(0,5):[],claims:K,recommendedSources:V}}function _2($,X,Z,{grounded:J=!1}={}){let W={};for(let Y of["perplexity","bing","google"]){let j=X[Y];if(!j)continue;if(j.error){W[Y]={status:"error",error:String(j.error)};continue}W[Y]={status:"ok",answer:I(j.answer||"",J?4500:2200),sourceIds:Z.filter((Q)=>Q.engines.includes(Y)).sort((Q,B)=>(Q.perEngine[Y]?.rank||99)-(B.perEngine[Y]?.rank||99)).map((Q)=>Q.id).slice(0,6)}}let K=J?700:300,V=Z.slice(0,J?10:8).map((Y)=>({id:Y.id,title:Y.title,domain:Y.domain,canonicalUrl:Y.canonicalUrl,sourceType:Y.sourceType,isOfficial:Y.isOfficial,engines:Y.engines,engineCount:Y.engineCount,fetch:Y.fetch?.attempted?{ok:Y.fetch.ok,publishedTime:Y.fetch.publishedTime||"",byline:Y.fetch.byline||"",snippet:I(Y.fetch.snippet||"",K)}:void 0}));return["You are a research synthesizer. Combine these search engine results into a single authoritative answer.","",`Query: ${$}`,"",`Engine summaries:
306
+ `;N6(V,Y+Z.content,"utf8");let{content:Q,...j}=Z;return{...j,contentPath:V,contentChars:Z.contentChars||Q.length}})}var z6;var j1=c(()=>{z6=A2(process.cwd(),".dm","greedysearch-sources")});a$();V0();import{appendFileSync as R9,existsSync as P1,readFileSync as I9}from"node:fs";import{homedir as q9}from"node:os";import{join as h2}from"node:path";y0();V0();import{spawn as y8}from"node:child_process";import{join as h8}from"node:path";var p8=import.meta.dirname||new URL(".",import.meta.url).pathname.replace(/^\/([A-Z]:)/,"$1");function E0($,X,Z=null,J=!1,W=null,K=null){if(W===null)W=$.includes("logically")?120000:$.includes("chatgpt")?80000:$.includes("gemini")?70000:60000;let V=[...Z?["--tab",Z]:[],...J?["--short"]:[],...K?["--locale",K]:[]];return new Promise((Y,Q)=>{let j=y8(H0(),[h8(p8,"..","..","extractors",$),"--stdin",...V],{stdio:["pipe","pipe","pipe"],env:{...process.env,CDP_PROFILE_DIR:n}});j.stdin.write(X),j.stdin.end();let B="",M="";j.stdout.on("data",(H)=>B+=H),j.stderr.on("data",(H)=>{if(M+=H,process.env.GREEDY_SEARCH_CHILD_STDERR!=="0")process.stderr.write(H)});let D=setTimeout(()=>{j.kill();let H=(O,k=20)=>String(O??"").split(/\r?\n/).filter(Boolean).slice(-k).join(`
307
+ `),z=null;try{let O=JSON.parse(B.trim());if(O._envelope)z=O._envelope}catch{}let N=Error(`${$} timed out after ${W/1000}s`+(z?.lastStage?` (last stage: ${z.lastStage})`:""));N.engineScript=$,N.lastStage=z?.lastStage||null,N.partialErr=H(M),N.partialOut=H(B),Q(N)},W);j.on("close",(H)=>{if(clearTimeout(D),H===0)try{Y(JSON.parse(B.trim()))}catch{Q(Error(`bad JSON from ${$}: ${B.slice(0,100)}`))}else{let z=null;try{let k=JSON.parse(B.trim());if(k._envelope)z=k._envelope}catch{}let N=M.trim()||`extractor exit ${H}`,O=Error(N);if(z)O.envelope=z;Q(O)}})})}Y1();j1();import{mkdirSync as U6,readdirSync as A6,rmSync as P6,statSync as _6,writeFileSync as B1}from"node:fs";import{join as _$}from"node:path";var k6=import.meta.dirname||new URL(".",import.meta.url).pathname.replace(/^\/([A-Z]:)/,"$1");function F6($){return $.toLowerCase().replaceAll(/[^a-z0-9]+/g,"-").replaceAll(/^-|-$/g,"").slice(0,60)}var L6=604800000,w6=10;function T6($){try{let X=A6($).filter((J)=>J.endsWith(".json")||J.endsWith(".md")).map((J)=>({f:J,mtime:_6(_$($,J)).mtimeMs})).sort((J,W)=>W.mtime-J.mtime),Z=Date.now()-L6;for(let J=w6;J<X.length;J++)if(X[J].mtime<Z)P6(_$($,X[J].f),{force:!0})}catch{}}function C6(){let $=_$(k6,"..","..","results");return U6($,{recursive:!0}),T6($),$}function S0($,X,{inline:Z=!1,synthesize:J=!1,query:W=""}={}){let K=`${JSON.stringify($,null,2)}
308
+ `;if(X){B1(X,K,"utf8"),process.stderr.write(`Results written to ${X}
309
+ `);return}if(Z){process.stdout.write(K);return}let V=new Date().toISOString().replaceAll("T","_").replaceAll(/[:.]/g,"-").slice(0,19),Y=F6(W),Q=_$(C6(),`${V}_${Y}`);if(B1(`${Q}.json`,K,"utf8"),J&&$._synthesis?.answer)B1(`${Q}-synthesis.md`,$._synthesis.answer,"utf8"),process.stdout.write(`${Q}-synthesis.md
310
+ `);else process.stdout.write(`${Q}.json
311
+ `)}var R6=["perplexity","bing","chatgpt","semantic-scholar","logically"],I6=/timed out|timeout|verification|captcha|cloudflare|turnstile|input not found|ask-input|clipboard|copy button hidden|sign.in|login required/i,q6=/needs-human|verification required|please solve|captcha|cloudflare|turnstile|could not be completed automatically|manual intervention|sign.in|login required/i;function _2($){return I6.test(String($||""))}function M1($){return q6.test(String($||""))}function k2($){return R6.filter((X)=>{let Z=$?.[X];if(!Z)return!1;if(Z._envelope?.blockedBy)return!0;if(Z._envelope?.verificationResult==="needs-human")return!0;let J=Z.error;return J&&_2(J)})}function F2($){if(!$)return!1;let X=$.envelope;if(X?.blockedBy)return!0;if(X?.verificationResult==="needs-human")return!0;return _2($.message)}x0();V0();x0();function b6($){let X="",Z=!1,J=!1;for(let W of String($)){if(J){X+=W,J=!1;continue}if(W==="\\"){X+=W,J=!0;continue}if(W==='"'){Z=!Z,X+=W;continue}if(Z&&W===`
312
+ `)X+="\\n";else if(Z&&W==="\r")X+="\\r";else if(Z&&W==="\t")X+="\\t";else X+=W}return X}function t0($){if(!$)return null;let X=String($).trim(),Z=X.indexOf("BEGIN_JSON"),J=X.indexOf("END_JSON");if(Z!==-1&&J!==-1&&Z<J)X=X.slice(Z+10,J).trim();else{let Y=X.indexOf("{");if(Y>0)X=X.slice(Y)}let W=[X,X.replace(/^```json\s*/i,"").replace(/^```\s*/i,"").replace(/```$/i,"").trim()],K=X.indexOf("{"),V=X.lastIndexOf("}");if(K!==-1&&V!==-1&&K<V)W.push(X.slice(K,V+1));for(let Y of[...W]){let Q=b6(Y);if(Q!==Y)W.push(Q)}for(let Y of W)try{return JSON.parse(Y)}catch{}return null}function L2($,X,Z=""){let J=new Set(X.map((Q)=>Q.id)),W=["high","medium","low","mixed","conflicting"].includes($?.agreement?.level)?$.agreement.level:"mixed",K=Array.isArray($?.claims)?$.claims.map((Q)=>({claim:I(Q?.claim||"",260),support:["strong","moderate","weak","conflicting"].includes(Q?.support)?Q.support:"moderate",sourceIds:Array.isArray(Q?.sourceIds)?Q.sourceIds.filter((j)=>J.has(j)):[]})).filter((Q)=>Q.claim):[],V=Array.isArray($?.recommendedSources)?$.recommendedSources.filter((Q)=>J.has(Q)).slice(0,6):[],Y="";if(Z){let Q=Z.indexOf("{"),j=Z.lastIndexOf("}");if(Q!==-1&&j!==-1&&Q<j)Y=Z.slice(Q,j+1);else Y=Z}return{answer:I($?.answer||Y||Z,4000),agreement:{level:W,summary:I($?.agreement?.summary||"",280)},differences:Array.isArray($?.differences)?$.differences.map((Q)=>I(Q,220)).filter(Boolean).slice(0,5):[],caveats:Array.isArray($?.caveats)?$.caveats.map((Q)=>I(Q,220)).filter(Boolean).slice(0,5):[],claims:K,recommendedSources:V}}function w2($,X,Z,{grounded:J=!1}={}){let W={};for(let Y of["perplexity","bing","google"]){let Q=X[Y];if(!Q)continue;if(Q.error){W[Y]={status:"error",error:String(Q.error)};continue}W[Y]={status:"ok",answer:I(Q.answer||"",J?4500:2200),sourceIds:Z.filter((j)=>j.engines.includes(Y)).sort((j,B)=>(j.perEngine[Y]?.rank||99)-(B.perEngine[Y]?.rank||99)).map((j)=>j.id).slice(0,6)}}let K=J?700:300,V=Z.slice(0,J?10:8).map((Y)=>({id:Y.id,title:Y.title,domain:Y.domain,canonicalUrl:Y.canonicalUrl,sourceType:Y.sourceType,isOfficial:Y.isOfficial,engines:Y.engines,engineCount:Y.engineCount,fetch:Y.fetch?.attempted?{ok:Y.fetch.ok,publishedTime:Y.fetch.publishedTime||"",byline:Y.fetch.byline||"",snippet:I(Y.fetch.snippet||"",K)}:void 0}));return["You are a research synthesizer. Combine these search engine results into a single authoritative answer.","",`Query: ${$}`,"",`Engine summaries:
313
313
  ${JSON.stringify(W,null,2)}`,"",`Source registry:
314
314
  ${JSON.stringify(V,null,2)}`,"","Instructions:","- Write a clear, direct answer in markdown (use headers/bullets where they help readability)","- Cite sources inline as [S1], [S2] etc. when making specific claims","- Prefer sources with content (fetch.ok=true and non-empty snippet) for citations","- Note where the engines agree or meaningfully disagree","- List any important caveats or limitations","- recommendedSources: the 2-4 source IDs most worth reading for this query","","Respond ONLY with a JSON object wrapped in BEGIN_JSON / END_JSON markers:","","BEGIN_JSON",JSON.stringify({answer:"<your markdown answer here>",agreement:{level:"high|medium|mixed|conflicting",summary:"<one sentence>"},differences:["<notable difference between engines, if any>"],caveats:["<important caveat or limitation>"],recommendedSources:["S1","S2"]},null,2),"END_JSON"].join(`
315
- `)}function k2($){let X=Array.isArray($._sources)?$._sources:[],Z=X.length>0?X[0]?.engineCount||0:0,J=X.filter((Q)=>Q.isOfficial).length,W=X.filter((Q)=>Q.isOfficial||Q.sourceType==="maintainer-blog").length,K=X.filter((Q)=>Q.fetch?.attempted).length,V=X.filter((Q)=>Q.fetch?.ok).length,Y=X.reduce((Q,B)=>{return Q[B.sourceType]=(Q[B.sourceType]||0)+1,Q},{}),j=$._synthesis?.agreement?.level;return{sourcesCount:X.length,topSourceConsensus:Z,agreementLevel:j||(Z>=3?"high":Z>=2?"medium":"low"),enginesResponded:e.filter((Q)=>$[Q]?.answer&&!$[Q]?.error),enginesFailed:e.filter((Q)=>$[Q]?.error),officialSourceCount:J,firstPartySourceCount:W,fetchedSourceSuccessRate:K>0?Number((V/K).toFixed(2)):0,sourceTypeBreakdown:Y}}V0();import{spawn as I6}from"node:child_process";import{join as q6}from"node:path";x0();var b6=import.meta.dirname||new URL(".",import.meta.url).pathname.replace(/^\/([A-Z]:)/,"$1"),v6={gemini:"gemini.mjs",chatgpt:"chatgpt.mjs"},E6={gemini:"https://gemini.google.com/app",chatgpt:"https://chatgpt.com/"};function t0($="gemini"){let X=String($||"gemini").toLowerCase();if(X==="gem")return"gemini";if(X==="gpt")return"chatgpt";return X}function F2($="gemini"){return E6[t0($)]||"about:blank"}async function L2($,X,{tabPrefix:Z=null,timeoutMs:J=180000,visible:W=null}={}){let K=t0($),V=v6[K];if(!V||!h0.includes(K))throw Error(`Unsupported synthesizer "${$}". Supported: ${h0.join(", ")}`);return new Promise((Y,j)=>{let Q=Z?["--tab",String(Z)]:[],B={...process.env,CDP_PROFILE_DIR:n};if(W!==!0)delete B.GREEDY_SEARCH_VISIBLE,delete B.GREEDY_SEARCH_ALWAYS_VISIBLE;else B.GREEDY_SEARCH_VISIBLE="1",B.GREEDY_SEARCH_ALWAYS_VISIBLE="1";let M=I6(nodeRuntimeCommand(),[q6(b6,"..","..","extractors",V),"--stdin",...Q],{stdio:["pipe","pipe","pipe"],env:B});M.stdin.write(X),M.stdin.end();let D="",H="";M.stdout.on("data",(N)=>D+=N),M.stderr.on("data",(N)=>H+=N);let z=setTimeout(()=>{M.kill(),j(Error(`${K} prompt timed out after ${J/1000}s`))},J);M.on("close",(N)=>{if(clearTimeout(z),N!==0){j(Error(H.trim()||`${K} extractor failed`));return}try{Y(JSON.parse(D.trim()))}catch{j(Error(`bad JSON from ${K}: ${D.slice(0,100)}`))}})})}async function T0($,X={}){return L2("gemini",$,X)}async function w2($,X,{grounded:Z=!1,tabPrefix:J=null,visible:W=null,synthesizer:K="gemini"}={}){let V=t0(K),Y=Array.isArray(X._sources)?X._sources:f0(X),j=_2($,X,Y,{grounded:Z}),Q=await L2(V,j,{tabPrefix:J,timeoutMs:180000,visible:W}),B=s0(Q.answer||""),D=B&&["answer","agreement","claims","differences","caveats"].some((z)=>(z in B));if(B&&["perplexity","bing","google","chatgpt","gemini"].some((z)=>(z in B))&&!D)B=null;return{...P2(B,Y,Q.answer||""),rawAnswer:Q.answer||"",synthesizedBy:V,synthesizerSources:Q.sources||[],geminiSources:V==="gemini"?Q.sources||[]:[]}}var f6=/^(can you |could you |please |would you mind |i need to (know|understand) |i want to (know|understand) |i('m| am) (looking for|wondering about|curious about) |i need (information|info) (about|on) |tell me )?(about |explain |describe |give me |help me understand |search for |look up |find |research )?(about |regarding |on |for )?(it|this|the following)?\s*/i,x6=/\b(latest|newest|current|recent|up-to-date|up to date)\b/i,S6=/\b\d+\.\d+|\bv\d+\b|\b20(2[0-9]|[3-9]\d)\b/i;function g6($){let X=$.trim().replace(f6,"").trim();return X.length>4?X:$.trim()}function y6($,X=new Date().getFullYear()){if(!x6.test($))return $;if(S6.test($))return $;return`${$.trimEnd()} ${X}`}function k$($){if(!$?.trim())return $;let X=g6($);return X=y6(X),X||$}x0();import{spawn as h6}from"node:child_process";import{mkdirSync as B1,writeFileSync as i}from"node:fs";import{join as x}from"node:path";import{fileURLToPath as p6}from"node:url";V0();y0();var m6=p6(new URL(".",import.meta.url)).replace(/^\/([A-Z]:)/,"$1"),d6=x(m6,"..","..","bin","search.mjs"),l6=x(process.cwd(),".dm","greedysearch-research");function u6($){return String($||"research").toLowerCase().replaceAll(/[^a-z0-9]+/g,"-").replaceAll(/^-|-$/g,"").slice(0,60)||"research"}function Q0($,X=1/0){let Z=new Set,J=[];for(let W of $||[]){let K=I(String(W||""),1000);if(!K||Z.has(K))continue;if(Z.add(K),J.push(K),J.length>=X)break}return J}async function c6(...$){let{fetchMultipleSources:X}=await Promise.resolve().then(() => (K1(),O2));return X(...$)}async function D1(...$){let{writeSourcesToFiles:X}=await Promise.resolve().then(() => (Y1(),N2));return X(...$)}function n6({breadth:$=3,iterations:X=2,maxSources:Z}){let J=M1($,1,5,3),W=M1(X,1,3,2),K=M1(Z??Math.max(5,J*W*2),3,12,8);return{breadth:J,iterations:W,maxSources:K}}function M1($,X,Z,J){let W=Number.parseInt(String($??""),10);if(!Number.isFinite(W))return J;return Math.min(Z,Math.max(X,W))}function i6($,X,Z,{expand:J=!0,includeOriginal:W=!0,exclude:K=[]}={}){let V=Array.isArray($?.queries)?$.queries:[],Y=[],j=new Set([...K].map((Q)=>Z0(Q).toLowerCase()));for(let Q of V){let B=typeof Q==="string"?Q:Q?.query,M=typeof Q==="string"?"":Q?.researchGoal||"";H1(Y,B,M,{exclude:j})}if(W)H1(Y,X,"Original user query",{prepend:!0,exclude:j});if(J){let Q=[{query:`${X} official docs GitHub`,researchGoal:"Find primary project docs, repository details, and maintainer claims."},{query:`${X} benchmarks limitations compatibility`,researchGoal:"Validate performance claims and uncover unsupported APIs or caveats."},{query:`${X} alternatives comparison production use cases`,researchGoal:"Compare against conventional headless browsers and identify when to choose it."},{query:`${X} anti bot detection Cloudflare screenshots visual rendering`,researchGoal:"Check automation risks, rendering gaps, screenshots, and bot-detection behavior."}];for(let B of Q){if(Y.length>=Z)break;H1(Y,B.query,B.researchGoal,{exclude:j})}}return Y.slice(0,Z)}function H1($,X,Z="",{prepend:J=!1,exclude:W=new Set}={}){if(!X||typeof X!=="string")return;let K=Z0(X);if(!K||W.has(K.toLowerCase())||$.some((Y)=>Y.query.toLowerCase()===K.toLowerCase()))return;let V={query:K,researchGoal:I(Z,320)};if(J)$.unshift(V);else $.push(V)}function Z0($){return a6(o6(String($)))}function o6($){let X="",Z=0;while(Z<$.length){let J=$.indexOf("[",Z);if(J===-1){X+=$.slice(Z);break}let W=$.indexOf("]",J+1);if(W===-1||$[W+1]!=="("||W===J+1){X+=$.slice(Z,J+1),Z=J+1;continue}let K=$.indexOf(")",W+2);if(K===-1){X+=$.slice(Z,J+1),Z=J+1;continue}let V=$.slice(W+2,K).trimStart();if(!V.startsWith("http://")&&!V.startsWith("https://")){X+=$.slice(Z,J+1),Z=J+1;continue}X+=$.slice(Z,J),X+=$.slice(J+1,W),Z=K+1}return X}function a6($){let X="",Z=!1;for(let J of $)if(J===" "||J==="\t"||J===`
316
- `||J==="\r"){if(!Z)X+=" ";Z=!0}else X+=J,Z=!1;return X.trim()}function T2($){return new Set(String($).toLowerCase().normalize("NFD").replaceAll(/[\u0300-\u036f]/g,"").split(/[^\w]+/).filter((X)=>X.length>1))}function T$($,X){let Z=T2($),J=T2(X),W=new Set([...Z,...J]).size;if(W===0)return 1;let K=0;for(let V of Z)if(J.has(V))K++;return K/W}function q2($,X,{threshold:Z=0.75,roundIndex:J=0,originalQuery:W=null}={}){let K=Z0($).toLowerCase();if(X.has(K))return!0;if(W&&J>0&&K===Z0(W).toLowerCase())return!0;for(let V of X)if(T$(K,V)>=Z)return!0;return!1}function s6($,X,Z,J){let W=X.map((K)=>({queries:K.queries?.map((V)=>V.query||"")||[],learnings:K.learnings||[],gaps:K.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: ${$}`,`Rounds completed: ${JSON.stringify(W,null,2)}`,`Accumulated learnings: ${JSON.stringify(Z.slice(0,12),null,2)}`,`Known gaps: ${JSON.stringify(J.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(`
317
- `)}function t6($,X,Z,J,W){let K=[],V=[{template:(Y)=>`${Y} official documentation`,label:"official docs"},{template:(Y)=>`${Y} GitHub issues discussions`,label:"community signals"},{template:(Y)=>`${Y} benchmarks performance comparison`,label:"benchmarks"},{template:(Y)=>`${Y} limitations risks caveats`,label:"limitations"},{template:(Y)=>`${Y} production deployment experience`,label:"production usage"},{template:(Y)=>`${X} ${Y} counter evidence`,label:"counter-evidence"}];for(let Y=0;Y<$.length&&K.length<J;Y++){let j=$[Y],Q=V[Y%V.length],B=Q.template(j);if(!q2(B,Z,{roundIndex:W}))K.push({query:B,researchGoal:`Gap-driven: ${j} (${Q.label})`})}return K}async function r6($,X,Z,J,W){try{let K=await T0(s6($,X,Z,J),{timeoutMs:120000}),V=r0(K,{}),Y=typeof V.score==="number"?Math.min(10,Math.max(0,V.score)):W.length>0?W[W.length-1]:5,j=Array.isArray(V.knowledgeGaps)?V.knowledgeGaps.map((D)=>String(D)).filter(Boolean).slice(0,6):[],Q=Array.isArray(V.nextActions)?V.nextActions.slice(0,5):[],B=typeof V.shouldContinue==="boolean"?V.shouldContinue:Y<8,M=V.terminationReason||null;return{score:Y,coverage:V.coverage||{},knowledgeGaps:j,shouldContinue:B,nextActions:Q,terminationReason:M||(Y>=8.5?"quality_threshold":null),evaluationError:""}}catch(K){return process.stderr.write(`[greedysearch] Quality evaluation failed: ${K.message}
318
- `),{score:W.length>0?W[W.length-1]:5,coverage:{},knowledgeGaps:[],shouldContinue:!0,nextActions:[],terminationReason:null,evaluationError:K.message}}}function e6($){let X={};for(let Z of Object.keys($||{}).filter((J)=>!J.startsWith("_"))){let J=$?.[Z];if(!J)continue;X[Z]=J.error?{status:"error",error:String(J.error)}:{status:"ok",answer:I(J.answer||"",1400),sources:Array.isArray(J.sources)?J.sources.slice(0,5).map((W)=>({title:I(W.title||"",160),url:W.url||""})):[]}}return X}function $9($,X,Z=[],J=[],W=[]){let K=J.length>0?`
315
+ `)}function T2($){let X=Array.isArray($._sources)?$._sources:[],Z=X.length>0?X[0]?.engineCount||0:0,J=X.filter((j)=>j.isOfficial).length,W=X.filter((j)=>j.isOfficial||j.sourceType==="maintainer-blog").length,K=X.filter((j)=>j.fetch?.attempted).length,V=X.filter((j)=>j.fetch?.ok).length,Y=X.reduce((j,B)=>{return j[B.sourceType]=(j[B.sourceType]||0)+1,j},{}),Q=$._synthesis?.agreement?.level;return{sourcesCount:X.length,topSourceConsensus:Z,agreementLevel:Q||(Z>=3?"high":Z>=2?"medium":"low"),enginesResponded:e.filter((j)=>$[j]?.answer&&!$[j]?.error),enginesFailed:e.filter((j)=>$[j]?.error),officialSourceCount:J,firstPartySourceCount:W,fetchedSourceSuccessRate:K>0?Number((V/K).toFixed(2)):0,sourceTypeBreakdown:Y}}V0();import{spawn as v6}from"node:child_process";import{join as E6}from"node:path";x0();var f6=import.meta.dirname||new URL(".",import.meta.url).pathname.replace(/^\/([A-Z]:)/,"$1"),x6={gemini:"gemini.mjs",chatgpt:"chatgpt.mjs"},S6={gemini:"https://gemini.google.com/app",chatgpt:"https://chatgpt.com/"};function r0($="gemini"){let X=String($||"gemini").toLowerCase();if(X==="gem")return"gemini";if(X==="gpt")return"chatgpt";return X}function C2($="gemini"){return S6[r0($)]||"about:blank"}async function R2($,X,{tabPrefix:Z=null,timeoutMs:J=180000,visible:W=null}={}){let K=r0($),V=x6[K];if(!V||!h0.includes(K))throw Error(`Unsupported synthesizer "${$}". Supported: ${h0.join(", ")}`);return new Promise((Y,Q)=>{let j=Z?["--tab",String(Z)]:[],B={...process.env,CDP_PROFILE_DIR:n};if(W!==!0)delete B.GREEDY_SEARCH_VISIBLE,delete B.GREEDY_SEARCH_ALWAYS_VISIBLE;else B.GREEDY_SEARCH_VISIBLE="1",B.GREEDY_SEARCH_ALWAYS_VISIBLE="1";let M=v6(nodeRuntimeCommand(),[E6(f6,"..","..","extractors",V),"--stdin",...j],{stdio:["pipe","pipe","pipe"],env:B});M.stdin.write(X),M.stdin.end();let D="",H="";M.stdout.on("data",(N)=>D+=N),M.stderr.on("data",(N)=>H+=N);let z=setTimeout(()=>{M.kill(),Q(Error(`${K} prompt timed out after ${J/1000}s`))},J);M.on("close",(N)=>{if(clearTimeout(z),N!==0){Q(Error(H.trim()||`${K} extractor failed`));return}try{Y(JSON.parse(D.trim()))}catch{Q(Error(`bad JSON from ${K}: ${D.slice(0,100)}`))}})})}async function T0($,X={}){return R2("gemini",$,X)}async function I2($,X,{grounded:Z=!1,tabPrefix:J=null,visible:W=null,synthesizer:K="gemini"}={}){let V=r0(K),Y=Array.isArray(X._sources)?X._sources:f0(X),Q=w2($,X,Y,{grounded:Z}),j=await R2(V,Q,{tabPrefix:J,timeoutMs:180000,visible:W}),B=t0(j.answer||""),D=B&&["answer","agreement","claims","differences","caveats"].some((z)=>(z in B));if(B&&["perplexity","bing","google","chatgpt","gemini"].some((z)=>(z in B))&&!D)B=null;return{...L2(B,Y,j.answer||""),rawAnswer:j.answer||"",synthesizedBy:V,synthesizerSources:j.sources||[],geminiSources:V==="gemini"?j.sources||[]:[]}}var g6=/^(can you |could you |please |would you mind |i need to (know|understand) |i want to (know|understand) |i('m| am) (looking for|wondering about|curious about) |i need (information|info) (about|on) |tell me )?(about |explain |describe |give me |help me understand |search for |look up |find |research )?(about |regarding |on |for )?(it|this|the following)?\s*/i,y6=/\b(latest|newest|current|recent|up-to-date|up to date)\b/i,h6=/\b\d+\.\d+|\bv\d+\b|\b20(2[0-9]|[3-9]\d)\b/i;function p6($){let X=$.trim().replace(g6,"").trim();return X.length>4?X:$.trim()}function m6($,X=new Date().getFullYear()){if(!y6.test($))return $;if(h6.test($))return $;return`${$.trimEnd()} ${X}`}function k$($){if(!$?.trim())return $;let X=p6($);return X=m6(X),X||$}x0();import{spawn as d6}from"node:child_process";import{existsSync as l6,mkdirSync as H1,writeFileSync as i}from"node:fs";import{join as E}from"node:path";import{fileURLToPath as u6}from"node:url";V0();y0();var c6=u6(new URL(".",import.meta.url)).replace(/^\/([A-Z]:)/,"$1");function A1($=c6){let X=[E($,"search.mjs"),E($,"..","bin","search.mjs"),E($,"..","..","bin","search.mjs")];for(let Z of X)if(l6(Z))return Z;return X[X.length-1]}var n6=A1(),i6=E(process.cwd(),".dm","greedysearch-research");function o6($){return String($||"research").toLowerCase().replaceAll(/[^a-z0-9]+/g,"-").replaceAll(/^-|-$/g,"").slice(0,60)||"research"}function j0($,X=1/0){let Z=new Set,J=[];for(let W of $||[]){let K=I(String(W||""),1000);if(!K||Z.has(K))continue;if(Z.add(K),J.push(K),J.length>=X)break}return J}async function a6(...$){let{fetchMultipleSources:X}=await Promise.resolve().then(() => (Y1(),U2));return X(...$)}async function z1(...$){let{writeSourcesToFiles:X}=await Promise.resolve().then(() => (j1(),P2));return X(...$)}function s6({breadth:$=3,iterations:X=2,maxSources:Z}){let J=O1($,1,5,3),W=O1(X,1,3,2),K=O1(Z??Math.max(5,J*W*2),3,12,8);return{breadth:J,iterations:W,maxSources:K}}function O1($,X,Z,J){let W=Number.parseInt(String($??""),10);if(!Number.isFinite(W))return J;return Math.min(Z,Math.max(X,W))}function t6($,X,Z,{expand:J=!0,includeOriginal:W=!0,exclude:K=[]}={}){let V=Array.isArray($?.queries)?$.queries:[],Y=[],Q=new Set([...K].map((j)=>Z0(j).toLowerCase()));for(let j of V){let B=typeof j==="string"?j:j?.query,M=typeof j==="string"?"":j?.researchGoal||"";G1(Y,B,M,{exclude:Q})}if(W)G1(Y,X,"Original user query",{prepend:!0,exclude:Q});if(J){let j=[{query:`${X} official docs GitHub`,researchGoal:"Find primary project docs, repository details, and maintainer claims."},{query:`${X} benchmarks limitations compatibility`,researchGoal:"Validate performance claims and uncover unsupported APIs or caveats."},{query:`${X} alternatives comparison production use cases`,researchGoal:"Compare against conventional headless browsers and identify when to choose it."},{query:`${X} anti bot detection Cloudflare screenshots visual rendering`,researchGoal:"Check automation risks, rendering gaps, screenshots, and bot-detection behavior."}];for(let B of j){if(Y.length>=Z)break;G1(Y,B.query,B.researchGoal,{exclude:Q})}}return Y.slice(0,Z)}function G1($,X,Z="",{prepend:J=!1,exclude:W=new Set}={}){if(!X||typeof X!=="string")return;let K=Z0(X);if(!K||W.has(K.toLowerCase())||$.some((Y)=>Y.query.toLowerCase()===K.toLowerCase()))return;let V={query:K,researchGoal:I(Z,320)};if(J)$.unshift(V);else $.push(V)}function Z0($){return e6(r6(String($)))}function r6($){let X="",Z=0;while(Z<$.length){let J=$.indexOf("[",Z);if(J===-1){X+=$.slice(Z);break}let W=$.indexOf("]",J+1);if(W===-1||$[W+1]!=="("||W===J+1){X+=$.slice(Z,J+1),Z=J+1;continue}let K=$.indexOf(")",W+2);if(K===-1){X+=$.slice(Z,J+1),Z=J+1;continue}let V=$.slice(W+2,K).trimStart();if(!V.startsWith("http://")&&!V.startsWith("https://")){X+=$.slice(Z,J+1),Z=J+1;continue}X+=$.slice(Z,J),X+=$.slice(J+1,W),Z=K+1}return X}function e6($){let X="",Z=!1;for(let J of $)if(J===" "||J==="\t"||J===`
316
+ `||J==="\r"){if(!Z)X+=" ";Z=!0}else X+=J,Z=!1;return X.trim()}function q2($){return new Set(String($).toLowerCase().normalize("NFD").replaceAll(/[\u0300-\u036f]/g,"").split(/[^\w]+/).filter((X)=>X.length>1))}function T$($,X){let Z=q2($),J=q2(X),W=new Set([...Z,...J]).size;if(W===0)return 1;let K=0;for(let V of Z)if(J.has(V))K++;return K/W}function f2($,X,{threshold:Z=0.75,roundIndex:J=0,originalQuery:W=null}={}){let K=Z0($).toLowerCase();if(X.has(K))return!0;if(W&&J>0&&K===Z0(W).toLowerCase())return!0;for(let V of X)if(T$(K,V)>=Z)return!0;return!1}function $9($,X,Z,J){let W=X.map((K)=>({queries:K.queries?.map((V)=>V.query||"")||[],learnings:K.learnings||[],gaps:K.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: ${$}`,`Rounds completed: ${JSON.stringify(W,null,2)}`,`Accumulated learnings: ${JSON.stringify(Z.slice(0,12),null,2)}`,`Known gaps: ${JSON.stringify(J.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(`
317
+ `)}function X9($,X,Z,J,W){let K=[],V=[{template:(Y)=>`${Y} official documentation`,label:"official docs"},{template:(Y)=>`${Y} GitHub issues discussions`,label:"community signals"},{template:(Y)=>`${Y} benchmarks performance comparison`,label:"benchmarks"},{template:(Y)=>`${Y} limitations risks caveats`,label:"limitations"},{template:(Y)=>`${Y} production deployment experience`,label:"production usage"},{template:(Y)=>`${X} ${Y} counter evidence`,label:"counter-evidence"}];for(let Y=0;Y<$.length&&K.length<J;Y++){let Q=$[Y],j=V[Y%V.length],B=j.template(Q);if(!f2(B,Z,{roundIndex:W}))K.push({query:B,researchGoal:`Gap-driven: ${Q} (${j.label})`})}return K}async function Z9($,X,Z,J,W){try{let K=await T0($9($,X,Z,J),{timeoutMs:120000}),V=e0(K,{}),Y=typeof V.score==="number"?Math.min(10,Math.max(0,V.score)):W.length>0?W[W.length-1]:5,Q=Array.isArray(V.knowledgeGaps)?V.knowledgeGaps.map((D)=>String(D)).filter(Boolean).slice(0,6):[],j=Array.isArray(V.nextActions)?V.nextActions.slice(0,5):[],B=typeof V.shouldContinue==="boolean"?V.shouldContinue:Y<8,M=V.terminationReason||null;return{score:Y,coverage:V.coverage||{},knowledgeGaps:Q,shouldContinue:B,nextActions:j,terminationReason:M||(Y>=8.5?"quality_threshold":null),evaluationError:""}}catch(K){return process.stderr.write(`[greedysearch] Quality evaluation failed: ${K.message}
318
+ `),{score:W.length>0?W[W.length-1]:5,coverage:{},knowledgeGaps:[],shouldContinue:!0,nextActions:[],terminationReason:null,evaluationError:K.message}}}function J9($){let X={};for(let Z of Object.keys($||{}).filter((J)=>!J.startsWith("_"))){let J=$?.[Z];if(!J)continue;X[Z]=J.error?{status:"error",error:String(J.error)}:{status:"ok",answer:I(J.answer||"",1400),sources:Array.isArray(J.sources)?J.sources.slice(0,5).map((W)=>({title:I(W.title||"",160),url:W.url||""})):[]}}return X}function W9($,X,Z=[],J=[],W=[]){let K=J.length>0?`
319
319
  Known knowledge gaps to target:
320
320
  ${J.map((Y)=>`- ${Y}`).join(`
321
321
  `)}`:"",V=W.length>0?`
@@ -325,78 +325,78 @@ ${W.map((Y)=>`- ${Y}`).join(`
325
325
  Prior learnings to build on:
326
326
  ${Z.map((Y)=>`- ${Y}`).join(`
327
327
  `)}`:"",K,V,"","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(`
328
- `)}function b2($){if(!$||typeof $!=="object")return null;let X=$.type,Z=I($.researchGoal||"",320);if(X==="search"){if($.query==null)return null;let J=Z0($.query);return J?{type:"search",query:J,researchGoal:Z}:null}if(X==="fetchUrl"){if($.url==null)return null;let J=$0($.url);return J?{type:"fetchUrl",url:J,researchGoal:Z}:null}return null}async function X9($,{locale:X=null,short:Z=!0,usedQueries:J,usedUrls:W,maxChars:K=8000}={}){if($.type==="search"){let V=Z0($.query).toLowerCase();J.add(V);try{let Y=await O9($.query,{locale:X,short:Z}),j=f0(Y,$.query);return{ok:!0,action:$,result:Y,sources:j}}catch(Y){return{ok:!1,action:$,error:Y.message,sources:[]}}}if($.type==="fetchUrl"){let V=$0($.url);if(W.has(V))return{ok:!1,action:$,error:`URL already fetched: ${V}`,sources:[]};try{let Y=await Z9(V,K);W.add(V);let j=W9(V),Q={id:"",canonicalUrl:Y.finalUrl||V,displayUrl:Y.url||V,domain:j,title:Y.title||V,engines:["fetch"],engineCount:1,perEngine:{},sourceType:J1(j,Y.title||"",Y.finalUrl||V),isOfficial:!1,smartScore:0,fetch:{attempted:!0,ok:!Y.error&&(Y.contentChars||0)>100,status:Y.status||null,finalUrl:Y.finalUrl||V,content:Y.content||"",contentChars:Y.contentChars||0,snippet:Y.snippet||"",error:Y.error||""}};return{ok:!0,action:$,result:null,sources:[Q],fetchResult:{id:Q.id,url:V,finalUrl:Y.finalUrl||V,title:Y.title||"",content:Y.content||"",contentChars:Y.contentChars||0,snippet:Y.snippet||"",status:Y.status||null,error:Y.error||"",source:Y.source||"http",duration:Y.duration||0}}}catch(Y){return{ok:!1,action:$,error:Y.message,sources:[]}}}return{ok:!1,action:$,error:`Unknown action type: ${$.type}`,sources:[]}}async function Z9($,X){return await J9($,X)}async function J9($,X=8000){let Z=Date.now();try{let{parseGitHubUrl:J,fetchGitHubContent:W}=await Promise.resolve().then(() => (A$(),r$)),K=J($);if(K&&(K.type==="root"||K.type==="tree"||K.type==="blob"&&!K.path?.includes("."))){let V=await W($);if(V.ok){let{trimContentHeadTail:Y}=await Promise.resolve().then(() => $1),j=Y(V.content,X);return{url:$,finalUrl:$,status:200,title:V.title,snippet:j.slice(0,320),content:j,contentChars:j.length,source:"github-api",duration:Date.now()-Z}}}}catch{}try{let{fetchSourceHttp:J}=await Promise.resolve().then(() => (s$(),X2)),{trimContentHeadTail:W}=await Promise.resolve().then(() => $1),K=await J($,{timeoutMs:1e4});if(K.ok){let V=W(K.markdown,X);return{url:$,finalUrl:K.finalUrl,status:K.status,title:K.title,snippet:K.excerpt,content:V,contentChars:V.length,source:"http",duration:Date.now()-Z}}}catch{}return{url:$,title:"",content:"",contentChars:0,snippet:"",error:"HTTP fetch failed",source:"error",duration:Date.now()-Z}}function W9($){try{return new URL($).hostname.toLowerCase().replace(/^www\./,"")}catch{return""}}async function K9($,X){let Z=[],{parseGitHubUrl:J}=await Promise.resolve().then(() => (A$(),r$));for(let W of $){if(W.type!=="fetchUrl"){Z.push(W);continue}let K=J(W.url);if(!K||K.type!=="root"){Z.push(W);continue}let{owner:V,repo:Y}=K,j=`https://github.com/${V}/${Y}`;if(X.has(j))continue;let Q=[j],B=[`${j}/blob/main/CONTRIBUTING.md`,`${j}/blob/master/CONTRIBUTING.md`,`${j}/blob/main/CHANGELOG.md`,`${j}/blob/master/CHANGELOG.md`,`${j}/blob/main/docs/README.md`];for(let M of B){if(Q.length>=3)break;if(!X.has(M))Q.push(M)}for(let M of Q)Z.push({type:"fetchUrl",url:M,researchGoal:W.researchGoal||`Fetch GitHub content for ${V}/${Y}`})}return Z}function V9($,X){let Z=s0($?.answer||"")||{},J=Array.isArray(Z?.actions)?Z.actions:[],W=[];for(let K of J){let V=b2(K);if(V&&W.length<X)W.push(V)}return W}function Y9($){return($||[]).map((X)=>({type:"search",query:typeof X==="string"?X:X.query,researchGoal:typeof X==="string"?"":X.researchGoal||""})).filter((X)=>X.query)}function w$($){return $0($?.finalUrl||$?.canonicalUrl||$?.url||"")||$?.id||""}function j9($,X,Z,J=new Set){let W=(X||[]).filter((V)=>V.status!=="closed").slice(0,12).map((V)=>({id:V.id,question:V.question})),K=(Z||[]).filter((V)=>V?.content||V?.snippet).filter((V)=>!J.has(w$(V))).slice(0,6).map((V,Y)=>({id:V.id||`F${Y+1}`,title:V.title||"",url:V.finalUrl||V.url||V.canonicalUrl||"",content:I(V.content||V.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: ${$}`,`Open question ledger: ${JSON.stringify(W,null,2)}`,`Fetched sources: ${JSON.stringify(K,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(`
329
- `)}function Q9($,X){let Z=Array.isArray($?.extractions)?$.extractions:[],J=new Map,W=new Map;for(let K of X||[]){if(K?.id)W.set(String(K.id),K);let V=w$(K);if(V)J.set(V,K)}return Z.map((K)=>{let V=W.get(String(K?.sourceId||""))||J.get($0(K?.url||"")||""),Y=String(K?.sourceId||V?.id||""),j=$0(K?.url||V?.finalUrl||V?.url||""),Q=Array.isArray(K?.answers)?K.answers.map((B)=>({id:String(B?.id||""),evidence:I(B?.evidence||"",500),sourceIds:[Y].filter(Boolean)})).filter((B)=>B.id):[];return{sourceId:Y,url:j,title:V?.title||K?.title||"",rational:I(K?.rational||"",700),evidence:I(K?.evidence||"",1600),summary:I(K?.summary||"",700),answers:Q,newQuestions:Q0(K?.newQuestions||[],6)}}).filter((K)=>K.sourceId||K.url||K.summary||K.evidence)}async function B9({query:$,questions:X,fetchedSources:Z,extractedSourceKeys:J}){let W=(Z||[]).filter((K)=>(K?.content||K?.snippet)&&!J.has(w$(K)));if(W.length===0)return{evidence:[],error:""};try{let K=await T0(j9($,X,W,J),{timeoutMs:120000}),V=r0(K,{extractions:[]}),Y=Q9(V,W);for(let j of W){let Q=w$(j);if(Q)J.add(Q)}return{evidence:Y,error:""}}catch(K){return{evidence:[],error:K.message||String(K)}}}function M9($,X,Z,J,W=[],K=[]){let V=J.filter((Y)=>Y?.content||Y?.snippet).slice(0,10).map((Y,j)=>({id:`F${j+1}`,title:Y.title||"",url:Y.finalUrl||Y.url||"",snippet:I(Y.content||Y.snippet||"",3000)}));return["You are extracting compact research state from live multi-engine search results.","Create dense, non-overlapping learnings with exact names, numbers, dates, limitations, and caveats where available.","Also propose follow-up search queries that would most improve confidence or fill gaps.","",`Original research question: ${$}`,`Round queries: ${JSON.stringify(X,null,2)}`,`Question ledger: ${JSON.stringify(W,null,2)}`,`Extracted source evidence: ${JSON.stringify(K.slice(-12),null,2)}`,`Engine summaries: ${JSON.stringify(Z,null,2)}`,`Fetched source snippets: ${JSON.stringify(V,null,2)}`,"","Respond ONLY with JSON wrapped in BEGIN_JSON / END_JSON markers:","BEGIN_JSON",JSON.stringify({learnings:["concise, information-dense learning"],answeredQuestions:[{id:"Q1",evidence:"brief evidence that closes this question",sourceIds:["S1"]}],newQuestions:["new sub-question discovered from the evidence"],followUpQueries:["specific next search query"],gaps:["important uncertainty or missing evidence"]},null,2),"END_JSON"].join(`
330
- `)}function H9($,X,Z,J=[],W=[]){let K=X.flatMap((j)=>j.learnings||[]),V=X.flatMap((j)=>j.gaps||[]),Y=Z.slice(0,12).map((j)=>({id:j.id,title:j.title,domain:j.domain,url:j.canonicalUrl,type:j.sourceType,engines:j.engines,fetch:j.fetch?.attempted?{ok:j.fetch.ok,snippet:I(j.fetch.snippet||"",1200),publishedTime:j.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: ${$}`,`Learnings: ${JSON.stringify(K,null,2)}`,`Known gaps/caveats: ${JSON.stringify(V,null,2)}`,`Question ledger: ${JSON.stringify(J,null,2)}`,`Goal-based extracted evidence: ${JSON.stringify(W.slice(-20),null,2)}`,`Source registry: ${JSON.stringify(Y,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(`
331
- `)}function G9($,X=[],Z=[],J=[]){let W=X.slice(0,12).map((j)=>({id:j.id,title:j.title,domain:j.domain,url:j.canonicalUrl,type:j.sourceType,engines:j.engines})),K=J.slice(-20),V=new Set;for(let j of K)for(let Q of j.answers||[])if(Q?.id)V.add(Q.id);let Y=(Z||[]).filter((j)=>j.status!=="closed").map((j)=>({id:j.id,question:j.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: ${$}`,`Per-source extracted evidence: ${JSON.stringify(K,null,2)}`,`Source registry: ${JSON.stringify(W,null,2)}`,`Questions already answered by the evidence: ${JSON.stringify(Array.from(V))}`,`Questions still open after this evidence: ${JSON.stringify(Y)}`,"","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(`
332
- `)}async function O9($,{locale:X=null,short:Z=!0}={}){let J=[d6,"all","--inline","--stdin","--fast"];if(!Z)J.push("--full");if(X)J.push("--locale",X);return new Promise((W,K)=>{let V=h6(H0(),J,{stdio:["pipe","pipe","pipe"],env:{...process.env,GREEDY_SEARCH_RESEARCH_CHILD:"1"}});V.stdin.write($),V.stdin.end();let Y="",j="",Q="";V.stdout.on("data",(M)=>Y+=M),V.stderr.on("data",(M)=>{j+=M,Q+=M.toString();let D=Q.split(`
333
- `);Q=D.pop()||"";for(let H of D)if(N9(H))process.stderr.write(`${H}
334
- `)});let B=setTimeout(()=>{V.kill(),K(Error(`research child search timed out for: ${$}`))},140000);V.on("close",(M)=>{if(clearTimeout(B),M!==0){K(Error(j.trim()||`search child exited with code ${M}`));return}try{W(JSON.parse(Y.trim()))}catch{K(Error(`Invalid JSON from research child: ${Y.slice(0,200)}`))}})})}function D9($){let X=new Map;for(let Z of $.flat()){let J=$0(Z.canonicalUrl||Z.url);if(!J)continue;let W=X.get(J);if(!W){X.set(J,{...Z,canonicalUrl:J});continue}W.engines=[...new Set([...W.engines||[],...Z.engines||[]])],W.engineCount=W.engines.length,W.smartScore=Math.max(W.smartScore||0,Z.smartScore||0)}return Array.from(X.values()).sort((Z,J)=>{let W=w0(J)-w0(Z);if(W!==0)return W;return(Z.domain||"").localeCompare(J.domain||"")}).slice(0,12).map((Z,J)=>({...Z,id:`S${J+1}`}))}function N9($){return/^PROGRESS:/.test($)||/^\[greedysearch\]/.test($)||/^\[(bing|perplexity|google|gemini|chatgpt|logically|semantic-scholar)\]/.test($)||/^GreedySearch Chrome/.test($)||/^Launching GreedySearch Chrome/.test($)||/^Headless mode/.test($)||/^Ready\.?$/.test($)}function r0($,X={}){return s0($?.answer||"")||X}function z9($,X){if(!$||!Array.isArray(X))return{cited:[],missing:[],unfetched:[],ok:!0};let Z=/\b[SF](\d+)\b/g,J=new Set,W;while((W=Z.exec($))!==null)J.add(`S${W[1]}`),J.add(`F${W[1]}`);let K=new Map;for(let Q of X){let B=Q?.id;if(B)K.set(B,Q)}let V=Array.from(J),Y=[],j=[];for(let Q of V){let B=K.get(Q);if(!B){let M=Q.match(/^(S|F)(\d+)$/);if(M){let D=parseInt(M[2],10)-1;if(D>=0&&D<X.length){let H=X[D];if(H){if(!(H.fetch?.ok||H.content&&H.content.length>100||H.contentChars&&H.contentChars>100))j.push(Q);continue}}}Y.push(Q)}else if(!(B.fetch?.ok||B.content&&B.content.length>100||B.contentChars&&B.contentChars>100))j.push(Q)}return{cited:V,missing:Y,unfetched:j,ok:Y.length===0}}function C2({sources:$=[],fetchedSources:X=[],synthesis:Z={},citationAudit:J=null,gaps:W=[],questions:K=[],rounds:V=[],qualityScore:Y=0,qualityThreshold:j=8.5,maxSources:Q=8,requireCitations:B=!0,requireQuestions:M=!0}={}){let D=X.filter((f)=>f?.fetch?.ok||(f?.contentChars||0)>100||String(f?.content||"").length>100),H=$.filter((f)=>["official-docs","repo","maintainer-blog","academic"].includes(String(f?.sourceType||""))),z=Array.isArray(Z?.claims)?Z.claims:[],N=J?J.cited?.length||0:0,G=N1(K),k=(K||[]).filter((f)=>!f.createdRound||f.reason==="Original research question"),E=N1(k),P=Math.min(4,Math.max(2,Number(Q)||8)),L={roundsRun:V.length>=1,fetchedSources:D.length>=P,primarySources:H.length>=1,qualityScore:Y>=Math.min(j,8),claimsExtracted:!B||z.length>0,citationsPresent:!B||N>0,citationsValid:!B||J?.ok===!0,unfetchedCitations:!B||(J?.unfetched||[]).length===0,requiredQuestionsClosed:!M||E.open===0};return{floorMet:Object.values(L).every(Boolean),checks:L,metrics:{fetchedOk:D.length,primarySources:H.length,claims:z.length,cited:N,gaps:W.length,openQuestions:G.open,closedQuestions:G.closed,totalQuestions:G.total,openRequiredQuestions:E.open,closedRequiredQuestions:E.closed,totalRequiredQuestions:E.total,qualityScore:Y,minFetched:P}}}function R2($,X){let Z=new Map;for(let J of X||[]){let W=$0(J?.canonicalUrl||J?.finalUrl||J?.url);if(W&&J?.id)Z.set(W,J.id)}return($||[]).map((J,W)=>{let K=$0(J?.finalUrl||J?.canonicalUrl||J?.url);return{...J,id:J?.id||Z.get(K)||`F${W+1}`}})}function U9($){return[{id:"Q1",question:I(Z0($),320),status:"open",reason:"Original research question",evidence:[],sourceIds:[]}]}function A9($){let X=0;for(let Z of $||[]){let J=Number.parseInt(String(Z.id||"").replace(/^Q/i,""),10);if(Number.isFinite(J))X=Math.max(X,J)}return`Q${X+1}`}function v2($,X){let Z=Z0(X).toLowerCase();return($||[]).find((J)=>J.question?.toLowerCase()===Z||T$(J.question||"",Z)>=0.82)}function G1($,X,{reason:Z="",round:J=null}={}){let W=I(Z0(X),320);if(!W)return null;let K=v2($,W);if(K)return K;let V={id:A9($),question:W,status:"open",reason:I(Z,240),createdRound:J,evidence:[],sourceIds:[]};return $.push(V),V}function L$($,X,{evidence:Z="",sourceIds:J=[],round:W=null}={}){let K=$.find((V)=>V.id===X)||v2($,X);if(!K)return null;if(K.status="closed",K.closedRound=K.closedRound||W,Z)K.evidence=Q0([...K.evidence||[],Z],4);if(Array.isArray(J))K.sourceIds=Q0([...K.sourceIds||[],...J],8);return K}function N1($){let X=$.length,Z=$.filter((J)=>J.status==="closed").length;return{total:X,closed:Z,open:Math.max(0,X-Z)}}function F$($,{roundNumber:X,actions:Z=[],learningPayload:J={}}={}){for(let j of Z){let Q=j?.action||j,B=Q?.researchGoal&&Q.researchGoal!=="Original user query"?Q.researchGoal:Q?.query||Q?.url||"";if(B)G1($,B,{reason:"Planned research action",round:X})}let W=5,K=$.filter((j)=>j.status==="open"&&j.reason==="Discovered gap/follow-up");if(K.length>W){let j=K.sort((Q,B)=>(Q.createdRound||0)-(B.createdRound||0)).slice(0,K.length-W);for(let Q of j)Q.status="resolved",Q.closedRound=X,Q.evidence=Q0([...Q.evidence||[],"Auto-resolved to cap open-question ledger"],4)}let V=Array.isArray(J.answeredQuestions)?J.answeredQuestions:[];for(let j of V){if(typeof j==="string"){L$($,j,{round:X});continue}let Q=j?.id||j?.question;if(!Q&&j?.question){let B=G1($,j.question,{reason:"Answered during learning extraction",round:X});if(B)L$($,B.id,{round:X});continue}L$($,Q,{evidence:j?.evidence||j?.answer||"",sourceIds:Array.isArray(j?.sourceIds)?j.sourceIds:[],round:X})}let Y=Array.isArray(J.newQuestions)?J.newQuestions:[];for(let j of Y)G1($,j,{reason:"Discovered gap/follow-up",round:X});return $}function P9($,X){if(!Array.isArray($)||$.length===0)return[];let Z=["arxiv.org","semanticscholar.org","doi.org"],J=new Set,W=[];for(let K of $){let V=K?.canonicalUrl||K?.finalUrl||K?.url||"";if(!V)continue;let Y="";try{Y=new URL(V).hostname.toLowerCase().replace(/^www\./,"")}catch{continue}if(!Z.some((Q)=>Y===Q||Y.endsWith(`.${Q}`)))continue;if(X.has(V)||J.has(V))continue;J.add(V);let j=V.includes("/pdf/")?V.replace(/\/pdf\//,"/html/").replace(/\.pdf$/i,""):V;W.push({url:j,label:K?.title||K?.id||Y})}return W.slice(0,2)}function _9($,X,Z){if(!X?.answer||Z?.ok!==!0)return $;let J=Array.isArray(X.claims)?X.claims:[],W=Array.isArray(Z.cited)?Z.cited:[];if(J.length===0||W.length===0)return $;for(let K of $){if(K.status==="closed")continue;let V=null,Y=0;for(let j of J){let Q=T$(K.question||"",j.claim||"");if(Q>Y)Y=Q,V=j}if(K.id==="Q1"||Y>=0.18)L$($,K.id,{evidence:V?.claim||"Answered in final cited synthesis",sourceIds:Array.isArray(V?.sourceIds)?V.sourceIds:W.slice(0,4)})}return $}function k9($){if(!$.length)return"No tracked questions.";return $.map((X)=>{let Z=X.sourceIds?.length?` (${X.sourceIds.join(", ")})`:"";return`- [${X.status==="closed"?"x":" "}] ${X.id}: ${X.question}${Z}`}).join(`
335
- `)}function O1($,X="None recorded."){let Z=Q0($);return Z.length?Z.map((J)=>`- ${J}`).join(`
336
- `):X}async function F9({query:$,rounds:X,sources:Z,fetchedSources:J,evidenceItems:W=[],synthesis:K,citationAudit:V,floor:Y,manifest:j,allGaps:Q=[],questions:B=[],outDir:M=null}){let D=new Date().toISOString().replaceAll(/[:.]/g,"-").slice(0,19),H=M||x(l6,`${D}_${u6($)}`),z=x(H,"reports"),N=x(H,"sources"),G=x(H,"data");B1(z,{recursive:!0}),B1(N,{recursive:!0}),B1(G,{recursive:!0});let k=await D1(J,N),E=Q0([...Q,...X.flatMap((P)=>P.gaps||[])]);return i(x(H,"STATUS.md"),[Y.floorMet?"STATUS: DONE":"STATUS: PARTIAL","",`Query: ${$}`,`Stop reason: ${j.terminationReason||"max_rounds"}`,"","## Deterministic floor checks",...Object.entries(Y.checks).map(([P,L])=>`- [${L?"x":" "}] ${P}`),"","## Questions",k9(B),"","## Open gaps",O1(E),""].join(`
337
- `),"utf8"),i(x(H,"OUTLINE.md"),["# Research bundle outline","","- `reports/SUMMARY.md` — final cited report","- `reports/CLAIMS.md` — extracted claims with support/source IDs","- `reports/EVIDENCE.md` — goal-based source evidence","- `reports/GAPS.md` — remaining caveats and uncertainties","- `sources/` — fetched source markdown files","- `data/manifest.json` — machine-readable run metadata","- `data/rounds.json` — per-round actions/learnings/gaps","- `data/sources.json` — ranked source registry","- `data/questions.json` — open/closed question ledger",""].join(`
338
- `),"utf8"),i(x(z,"SUMMARY.md"),String(K.answer||""),"utf8"),i(x(z,"CLAIMS.md"),["# Key claims","",...Array.isArray(K.claims)&&K.claims.length?K.claims.map((P)=>{let L=Array.isArray(P.sourceIds)?P.sourceIds.join(", "):"";return`- ${P.claim||""} (${P.support||"support unknown"}${L?`; ${L}`:""})`}):["No structured claims were extracted."],""].join(`
339
- `),"utf8"),i(x(z,"EVIDENCE.md"),["# Extracted evidence","",...W.length?W.map((P)=>[`## ${P.sourceId||P.url||"Source"}`,P.url?`<${P.url}>`:"",P.rational?`**Rational:** ${P.rational}`:"",P.evidence?`**Evidence:** ${P.evidence}`:"",P.summary?`**Summary:** ${P.summary}`:"",""].filter(Boolean).join(`
328
+ `)}function x2($){if(!$||typeof $!=="object")return null;let X=$.type,Z=I($.researchGoal||"",320);if(X==="search"){if($.query==null)return null;let J=Z0($.query);return J?{type:"search",query:J,researchGoal:Z}:null}if(X==="fetchUrl"){if($.url==null)return null;let J=$0($.url);return J?{type:"fetchUrl",url:J,researchGoal:Z}:null}return null}async function K9($,{locale:X=null,short:Z=!0,usedQueries:J,usedUrls:W,maxChars:K=8000}={}){if($.type==="search"){let V=Z0($.query).toLowerCase();J.add(V);try{let Y=await U9($.query,{locale:X,short:Z}),Q=f0(Y,$.query);return{ok:!0,action:$,result:Y,sources:Q}}catch(Y){return{ok:!1,action:$,error:Y.message,sources:[]}}}if($.type==="fetchUrl"){let V=$0($.url);if(W.has(V))return{ok:!1,action:$,error:`URL already fetched: ${V}`,sources:[]};try{let Y=await V9(V,K);W.add(V);let Q=Q9(V),j={id:"",canonicalUrl:Y.finalUrl||V,displayUrl:Y.url||V,domain:Q,title:Y.title||V,engines:["fetch"],engineCount:1,perEngine:{},sourceType:K1(Q,Y.title||"",Y.finalUrl||V),isOfficial:!1,smartScore:0,fetch:{attempted:!0,ok:!Y.error&&(Y.contentChars||0)>100,status:Y.status||null,finalUrl:Y.finalUrl||V,content:Y.content||"",contentChars:Y.contentChars||0,snippet:Y.snippet||"",error:Y.error||""}};return{ok:!0,action:$,result:null,sources:[j],fetchResult:{id:j.id,url:V,finalUrl:Y.finalUrl||V,title:Y.title||"",content:Y.content||"",contentChars:Y.contentChars||0,snippet:Y.snippet||"",status:Y.status||null,error:Y.error||"",source:Y.source||"http",duration:Y.duration||0}}}catch(Y){return{ok:!1,action:$,error:Y.message,sources:[]}}}return{ok:!1,action:$,error:`Unknown action type: ${$.type}`,sources:[]}}async function V9($,X){return await Y9($,X)}async function Y9($,X=8000){let Z=Date.now();try{let{parseGitHubUrl:J,fetchGitHubContent:W}=await Promise.resolve().then(() => (A$(),$1)),K=J($);if(K&&(K.type==="root"||K.type==="tree"||K.type==="blob"&&!K.path?.includes("."))){let V=await W($);if(V.ok){let{trimContentHeadTail:Y}=await Promise.resolve().then(() => Z1),Q=Y(V.content,X);return{url:$,finalUrl:$,status:200,title:V.title,snippet:Q.slice(0,320),content:Q,contentChars:Q.length,source:"github-api",duration:Date.now()-Z}}}}catch{}try{let{fetchSourceHttp:J}=await Promise.resolve().then(() => (r$(),K2)),{trimContentHeadTail:W}=await Promise.resolve().then(() => Z1),K=await J($,{timeoutMs:1e4});if(K.ok){let V=W(K.markdown,X);return{url:$,finalUrl:K.finalUrl,status:K.status,title:K.title,snippet:K.excerpt,content:V,contentChars:V.length,source:"http",duration:Date.now()-Z}}}catch{}return{url:$,title:"",content:"",contentChars:0,snippet:"",error:"HTTP fetch failed",source:"error",duration:Date.now()-Z}}function Q9($){try{return new URL($).hostname.toLowerCase().replace(/^www\./,"")}catch{return""}}async function j9($,X){let Z=[],{parseGitHubUrl:J}=await Promise.resolve().then(() => (A$(),$1));for(let W of $){if(W.type!=="fetchUrl"){Z.push(W);continue}let K=J(W.url);if(!K||K.type!=="root"){Z.push(W);continue}let{owner:V,repo:Y}=K,Q=`https://github.com/${V}/${Y}`;if(X.has(Q))continue;let j=[Q],B=[`${Q}/blob/main/CONTRIBUTING.md`,`${Q}/blob/master/CONTRIBUTING.md`,`${Q}/blob/main/CHANGELOG.md`,`${Q}/blob/master/CHANGELOG.md`,`${Q}/blob/main/docs/README.md`];for(let M of B){if(j.length>=3)break;if(!X.has(M))j.push(M)}for(let M of j)Z.push({type:"fetchUrl",url:M,researchGoal:W.researchGoal||`Fetch GitHub content for ${V}/${Y}`})}return Z}function B9($,X){let Z=t0($?.answer||"")||{},J=Array.isArray(Z?.actions)?Z.actions:[],W=[];for(let K of J){let V=x2(K);if(V&&W.length<X)W.push(V)}return W}function M9($){return($||[]).map((X)=>({type:"search",query:typeof X==="string"?X:X.query,researchGoal:typeof X==="string"?"":X.researchGoal||""})).filter((X)=>X.query)}function w$($){return $0($?.finalUrl||$?.canonicalUrl||$?.url||"")||$?.id||""}function H9($,X,Z,J=new Set){let W=(X||[]).filter((V)=>V.status!=="closed").slice(0,12).map((V)=>({id:V.id,question:V.question})),K=(Z||[]).filter((V)=>V?.content||V?.snippet).filter((V)=>!J.has(w$(V))).slice(0,6).map((V,Y)=>({id:V.id||`F${Y+1}`,title:V.title||"",url:V.finalUrl||V.url||V.canonicalUrl||"",content:I(V.content||V.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: ${$}`,`Open question ledger: ${JSON.stringify(W,null,2)}`,`Fetched sources: ${JSON.stringify(K,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(`
329
+ `)}function O9($,X){let Z=Array.isArray($?.extractions)?$.extractions:[],J=new Map,W=new Map;for(let K of X||[]){if(K?.id)W.set(String(K.id),K);let V=w$(K);if(V)J.set(V,K)}return Z.map((K)=>{let V=W.get(String(K?.sourceId||""))||J.get($0(K?.url||"")||""),Y=String(K?.sourceId||V?.id||""),Q=$0(K?.url||V?.finalUrl||V?.url||""),j=Array.isArray(K?.answers)?K.answers.map((B)=>({id:String(B?.id||""),evidence:I(B?.evidence||"",500),sourceIds:[Y].filter(Boolean)})).filter((B)=>B.id):[];return{sourceId:Y,url:Q,title:V?.title||K?.title||"",rational:I(K?.rational||"",700),evidence:I(K?.evidence||"",1600),summary:I(K?.summary||"",700),answers:j,newQuestions:j0(K?.newQuestions||[],6)}}).filter((K)=>K.sourceId||K.url||K.summary||K.evidence)}async function G9({query:$,questions:X,fetchedSources:Z,extractedSourceKeys:J}){let W=(Z||[]).filter((K)=>(K?.content||K?.snippet)&&!J.has(w$(K)));if(W.length===0)return{evidence:[],error:""};try{let K=await T0(H9($,X,W,J),{timeoutMs:120000}),V=e0(K,{extractions:[]}),Y=O9(V,W);for(let Q of W){let j=w$(Q);if(j)J.add(j)}return{evidence:Y,error:""}}catch(K){return{evidence:[],error:K.message||String(K)}}}function D9($,X,Z,J,W=[],K=[]){let V=J.filter((Y)=>Y?.content||Y?.snippet).slice(0,10).map((Y,Q)=>({id:`F${Q+1}`,title:Y.title||"",url:Y.finalUrl||Y.url||"",snippet:I(Y.content||Y.snippet||"",3000)}));return["You are extracting compact research state from live multi-engine search results.","Create dense, non-overlapping learnings with exact names, numbers, dates, limitations, and caveats where available.","Also propose follow-up search queries that would most improve confidence or fill gaps.","",`Original research question: ${$}`,`Round queries: ${JSON.stringify(X,null,2)}`,`Question ledger: ${JSON.stringify(W,null,2)}`,`Extracted source evidence: ${JSON.stringify(K.slice(-12),null,2)}`,`Engine summaries: ${JSON.stringify(Z,null,2)}`,`Fetched source snippets: ${JSON.stringify(V,null,2)}`,"","Respond ONLY with JSON wrapped in BEGIN_JSON / END_JSON markers:","BEGIN_JSON",JSON.stringify({learnings:["concise, information-dense learning"],answeredQuestions:[{id:"Q1",evidence:"brief evidence that closes this question",sourceIds:["S1"]}],newQuestions:["new sub-question discovered from the evidence"],followUpQueries:["specific next search query"],gaps:["important uncertainty or missing evidence"]},null,2),"END_JSON"].join(`
330
+ `)}function N9($,X,Z,J=[],W=[]){let K=X.flatMap((Q)=>Q.learnings||[]),V=X.flatMap((Q)=>Q.gaps||[]),Y=Z.slice(0,12).map((Q)=>({id:Q.id,title:Q.title,domain:Q.domain,url:Q.canonicalUrl,type:Q.sourceType,engines:Q.engines,fetch:Q.fetch?.attempted?{ok:Q.fetch.ok,snippet:I(Q.fetch.snippet||"",1200),publishedTime:Q.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: ${$}`,`Learnings: ${JSON.stringify(K,null,2)}`,`Known gaps/caveats: ${JSON.stringify(V,null,2)}`,`Question ledger: ${JSON.stringify(J,null,2)}`,`Goal-based extracted evidence: ${JSON.stringify(W.slice(-20),null,2)}`,`Source registry: ${JSON.stringify(Y,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(`
331
+ `)}function z9($,X=[],Z=[],J=[]){let W=X.slice(0,12).map((Q)=>({id:Q.id,title:Q.title,domain:Q.domain,url:Q.canonicalUrl,type:Q.sourceType,engines:Q.engines})),K=J.slice(-20),V=new Set;for(let Q of K)for(let j of Q.answers||[])if(j?.id)V.add(j.id);let Y=(Z||[]).filter((Q)=>Q.status!=="closed").map((Q)=>({id:Q.id,question:Q.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: ${$}`,`Per-source extracted evidence: ${JSON.stringify(K,null,2)}`,`Source registry: ${JSON.stringify(W,null,2)}`,`Questions already answered by the evidence: ${JSON.stringify(Array.from(V))}`,`Questions still open after this evidence: ${JSON.stringify(Y)}`,"","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(`
332
+ `)}async function U9($,{locale:X=null,short:Z=!0}={}){let J=[n6,"all","--inline","--stdin","--fast"];if(!Z)J.push("--full");if(X)J.push("--locale",X);return new Promise((W,K)=>{let V=d6(H0(),J,{stdio:["pipe","pipe","pipe"],env:{...process.env,GREEDY_SEARCH_RESEARCH_CHILD:"1"}});V.stdin.write($),V.stdin.end();let Y="",Q="",j="";V.stdout.on("data",(M)=>Y+=M),V.stderr.on("data",(M)=>{Q+=M,j+=M.toString();let D=j.split(`
333
+ `);j=D.pop()||"";for(let H of D)if(P9(H))process.stderr.write(`${H}
334
+ `)});let B=setTimeout(()=>{V.kill(),K(Error(`research child search timed out for: ${$}`))},140000);V.on("close",(M)=>{if(clearTimeout(B),M!==0){K(Error(Q.trim()||`search child exited with code ${M}`));return}try{W(JSON.parse(Y.trim()))}catch{K(Error(`Invalid JSON from research child: ${Y.slice(0,200)}`))}})})}function A9($){let X=new Map;for(let Z of $.flat()){let J=$0(Z.canonicalUrl||Z.url);if(!J)continue;let W=X.get(J);if(!W){X.set(J,{...Z,canonicalUrl:J});continue}W.engines=[...new Set([...W.engines||[],...Z.engines||[]])],W.engineCount=W.engines.length,W.smartScore=Math.max(W.smartScore||0,Z.smartScore||0)}return Array.from(X.values()).sort((Z,J)=>{let W=w0(J)-w0(Z);if(W!==0)return W;return(Z.domain||"").localeCompare(J.domain||"")}).slice(0,12).map((Z,J)=>({...Z,id:`S${J+1}`}))}function P9($){return/^PROGRESS:/.test($)||/^\[greedysearch\]/.test($)||/^\[(bing|perplexity|google|gemini|chatgpt|logically|semantic-scholar)\]/.test($)||/^GreedySearch Chrome/.test($)||/^Launching GreedySearch Chrome/.test($)||/^Headless mode/.test($)||/^Ready\.?$/.test($)}function e0($,X={}){return t0($?.answer||"")||X}function _9($,X){if(!$||!Array.isArray(X))return{cited:[],missing:[],unfetched:[],ok:!0};let Z=/\b[SF](\d+)\b/g,J=new Set,W;while((W=Z.exec($))!==null)J.add(`S${W[1]}`),J.add(`F${W[1]}`);let K=new Map;for(let j of X){let B=j?.id;if(B)K.set(B,j)}let V=Array.from(J),Y=[],Q=[];for(let j of V){let B=K.get(j);if(!B){let M=j.match(/^(S|F)(\d+)$/);if(M){let D=parseInt(M[2],10)-1;if(D>=0&&D<X.length){let H=X[D];if(H){if(!(H.fetch?.ok||H.content&&H.content.length>100||H.contentChars&&H.contentChars>100))Q.push(j);continue}}}Y.push(j)}else if(!(B.fetch?.ok||B.content&&B.content.length>100||B.contentChars&&B.contentChars>100))Q.push(j)}return{cited:V,missing:Y,unfetched:Q,ok:Y.length===0}}function b2({sources:$=[],fetchedSources:X=[],synthesis:Z={},citationAudit:J=null,gaps:W=[],questions:K=[],rounds:V=[],qualityScore:Y=0,qualityThreshold:Q=8.5,maxSources:j=8,requireCitations:B=!0,requireQuestions:M=!0}={}){let D=X.filter((x)=>x?.fetch?.ok||(x?.contentChars||0)>100||String(x?.content||"").length>100),H=$.filter((x)=>["official-docs","repo","maintainer-blog","academic"].includes(String(x?.sourceType||""))),z=Array.isArray(Z?.claims)?Z.claims:[],N=J?J.cited?.length||0:0,O=U1(K),k=(K||[]).filter((x)=>!x.createdRound||x.reason==="Original research question"),f=U1(k),P=Math.min(4,Math.max(2,Number(j)||8)),L={roundsRun:V.length>=1,fetchedSources:D.length>=P,primarySources:H.length>=1,qualityScore:Y>=Math.min(Q,8),claimsExtracted:!B||z.length>0,citationsPresent:!B||N>0,citationsValid:!B||J?.ok===!0,unfetchedCitations:!B||(J?.unfetched||[]).length===0,requiredQuestionsClosed:!M||f.open===0};return{floorMet:Object.values(L).every(Boolean),checks:L,metrics:{fetchedOk:D.length,primarySources:H.length,claims:z.length,cited:N,gaps:W.length,openQuestions:O.open,closedQuestions:O.closed,totalQuestions:O.total,openRequiredQuestions:f.open,closedRequiredQuestions:f.closed,totalRequiredQuestions:f.total,qualityScore:Y,minFetched:P}}}function v2($,X){let Z=new Map;for(let J of X||[]){let W=$0(J?.canonicalUrl||J?.finalUrl||J?.url);if(W&&J?.id)Z.set(W,J.id)}return($||[]).map((J,W)=>{let K=$0(J?.finalUrl||J?.canonicalUrl||J?.url);return{...J,id:J?.id||Z.get(K)||`F${W+1}`}})}function k9($){return[{id:"Q1",question:I(Z0($),320),status:"open",reason:"Original research question",evidence:[],sourceIds:[]}]}function F9($){let X=0;for(let Z of $||[]){let J=Number.parseInt(String(Z.id||"").replace(/^Q/i,""),10);if(Number.isFinite(J))X=Math.max(X,J)}return`Q${X+1}`}function S2($,X){let Z=Z0(X).toLowerCase();return($||[]).find((J)=>J.question?.toLowerCase()===Z||T$(J.question||"",Z)>=0.82)}function D1($,X,{reason:Z="",round:J=null}={}){let W=I(Z0(X),320);if(!W)return null;let K=S2($,W);if(K)return K;let V={id:F9($),question:W,status:"open",reason:I(Z,240),createdRound:J,evidence:[],sourceIds:[]};return $.push(V),V}function L$($,X,{evidence:Z="",sourceIds:J=[],round:W=null}={}){let K=$.find((V)=>V.id===X)||S2($,X);if(!K)return null;if(K.status="closed",K.closedRound=K.closedRound||W,Z)K.evidence=j0([...K.evidence||[],Z],4);if(Array.isArray(J))K.sourceIds=j0([...K.sourceIds||[],...J],8);return K}function U1($){let X=$.length,Z=$.filter((J)=>J.status==="closed").length;return{total:X,closed:Z,open:Math.max(0,X-Z)}}function F$($,{roundNumber:X,actions:Z=[],learningPayload:J={}}={}){for(let Q of Z){let j=Q?.action||Q,B=j?.researchGoal&&j.researchGoal!=="Original user query"?j.researchGoal:j?.query||j?.url||"";if(B)D1($,B,{reason:"Planned research action",round:X})}let W=5,K=$.filter((Q)=>Q.status==="open"&&Q.reason==="Discovered gap/follow-up");if(K.length>W){let Q=K.sort((j,B)=>(j.createdRound||0)-(B.createdRound||0)).slice(0,K.length-W);for(let j of Q)j.status="resolved",j.closedRound=X,j.evidence=j0([...j.evidence||[],"Auto-resolved to cap open-question ledger"],4)}let V=Array.isArray(J.answeredQuestions)?J.answeredQuestions:[];for(let Q of V){if(typeof Q==="string"){L$($,Q,{round:X});continue}let j=Q?.id||Q?.question;if(!j&&Q?.question){let B=D1($,Q.question,{reason:"Answered during learning extraction",round:X});if(B)L$($,B.id,{round:X});continue}L$($,j,{evidence:Q?.evidence||Q?.answer||"",sourceIds:Array.isArray(Q?.sourceIds)?Q.sourceIds:[],round:X})}let Y=Array.isArray(J.newQuestions)?J.newQuestions:[];for(let Q of Y)D1($,Q,{reason:"Discovered gap/follow-up",round:X});return $}function L9($,X){if(!Array.isArray($)||$.length===0)return[];let Z=["arxiv.org","semanticscholar.org","doi.org"],J=new Set,W=[];for(let K of $){let V=K?.canonicalUrl||K?.finalUrl||K?.url||"";if(!V)continue;let Y="";try{Y=new URL(V).hostname.toLowerCase().replace(/^www\./,"")}catch{continue}if(!Z.some((j)=>Y===j||Y.endsWith(`.${j}`)))continue;if(X.has(V)||J.has(V))continue;J.add(V);let Q=V.includes("/pdf/")?V.replace(/\/pdf\//,"/html/").replace(/\.pdf$/i,""):V;W.push({url:Q,label:K?.title||K?.id||Y})}return W.slice(0,2)}function w9($,X,Z){if(!X?.answer||Z?.ok!==!0)return $;let J=Array.isArray(X.claims)?X.claims:[],W=Array.isArray(Z.cited)?Z.cited:[];if(J.length===0||W.length===0)return $;for(let K of $){if(K.status==="closed")continue;let V=null,Y=0;for(let Q of J){let j=T$(K.question||"",Q.claim||"");if(j>Y)Y=j,V=Q}if(K.id==="Q1"||Y>=0.18)L$($,K.id,{evidence:V?.claim||"Answered in final cited synthesis",sourceIds:Array.isArray(V?.sourceIds)?V.sourceIds:W.slice(0,4)})}return $}function T9($){if(!$.length)return"No tracked questions.";return $.map((X)=>{let Z=X.sourceIds?.length?` (${X.sourceIds.join(", ")})`:"";return`- [${X.status==="closed"?"x":" "}] ${X.id}: ${X.question}${Z}`}).join(`
335
+ `)}function N1($,X="None recorded."){let Z=j0($);return Z.length?Z.map((J)=>`- ${J}`).join(`
336
+ `):X}async function C9({query:$,rounds:X,sources:Z,fetchedSources:J,evidenceItems:W=[],synthesis:K,citationAudit:V,floor:Y,manifest:Q,allGaps:j=[],questions:B=[],outDir:M=null}){let D=new Date().toISOString().replaceAll(/[:.]/g,"-").slice(0,19),H=M||E(i6,`${D}_${o6($)}`),z=E(H,"reports"),N=E(H,"sources"),O=E(H,"data");H1(z,{recursive:!0}),H1(N,{recursive:!0}),H1(O,{recursive:!0});let k=await z1(J,N),f=j0([...j,...X.flatMap((P)=>P.gaps||[])]);return i(E(H,"STATUS.md"),[Y.floorMet?"STATUS: DONE":"STATUS: PARTIAL","",`Query: ${$}`,`Stop reason: ${Q.terminationReason||"max_rounds"}`,"","## Deterministic floor checks",...Object.entries(Y.checks).map(([P,L])=>`- [${L?"x":" "}] ${P}`),"","## Questions",T9(B),"","## Open gaps",N1(f),""].join(`
337
+ `),"utf8"),i(E(H,"OUTLINE.md"),["# Research bundle outline","","- `reports/SUMMARY.md` — final cited report","- `reports/CLAIMS.md` — extracted claims with support/source IDs","- `reports/EVIDENCE.md` — goal-based source evidence","- `reports/GAPS.md` — remaining caveats and uncertainties","- `sources/` — fetched source markdown files","- `data/manifest.json` — machine-readable run metadata","- `data/rounds.json` — per-round actions/learnings/gaps","- `data/sources.json` — ranked source registry","- `data/questions.json` — open/closed question ledger",""].join(`
338
+ `),"utf8"),i(E(z,"SUMMARY.md"),String(K.answer||""),"utf8"),i(E(z,"CLAIMS.md"),["# Key claims","",...Array.isArray(K.claims)&&K.claims.length?K.claims.map((P)=>{let L=Array.isArray(P.sourceIds)?P.sourceIds.join(", "):"";return`- ${P.claim||""} (${P.support||"support unknown"}${L?`; ${L}`:""})`}):["No structured claims were extracted."],""].join(`
339
+ `),"utf8"),i(E(z,"EVIDENCE.md"),["# Extracted evidence","",...W.length?W.map((P)=>[`## ${P.sourceId||P.url||"Source"}`,P.url?`<${P.url}>`:"",P.rational?`**Rational:** ${P.rational}`:"",P.evidence?`**Evidence:** ${P.evidence}`:"",P.summary?`**Summary:** ${P.summary}`:"",""].filter(Boolean).join(`
340
340
  `)):["No goal-based evidence was extracted."],""].join(`
341
- `),"utf8"),i(x(z,"GAPS.md"),["# Gaps and caveats","","## Caveats",O1(K.caveats||[]),"","## Research gaps",O1(E),""].join(`
342
- `),"utf8"),i(x(G,"manifest.json"),JSON.stringify({...j,floor:Y,citationAudit:V},null,2),"utf8"),i(x(G,"rounds.json"),JSON.stringify(X,null,2),"utf8"),i(x(G,"sources.json"),JSON.stringify(Z,null,2),"utf8"),i(x(G,"questions.json"),JSON.stringify(B,null,2),"utf8"),i(x(G,"evidence.json"),JSON.stringify(W,null,2),"utf8"),i(x(N,"index.md"),["# Source index","",...k.map((P)=>{let L=P.title||P.url,f=P.finalUrl||P.url,o=P.contentPath?` — ${P.contentPath}`:"";return`- ${P.id||"?"}: [${L}](${f})${o}`}),""].join(`
343
- `),"utf8"),{dir:H,statusPath:x(H,"STATUS.md"),summaryPath:x(z,"SUMMARY.md"),manifestPath:x(G,"manifest.json"),sourceCount:k.length,sourceFiles:k}}async function E2({query:$,breadth:X=3,iterations:Z=2,maxSources:J,locale:W=null,short:K=!1,qualityThreshold:V=8.5,writeBundle:Y=process.env.GREEDY_RESEARCH_BUNDLE!=="0",researchOutDir:j=null}={}){let Q=n6({breadth:X,iterations:Z,maxSources:J}),B=[],M=[],D=[],H=U9($),z=null,N=[],G=[],k=[],E=new Set,P=new Set,L=new Set,f=[],o="max_rounds",C0=new Date().toISOString(),C$=Date.now(),$$=0,N0=0,B0=0,M0=[];process.stderr.write(`[greedysearch] Research mode: breadth ${Q.breadth}, iterations ${Q.iterations}, qualityThreshold ${V}, engines ${S$.join(",")}, synthesizer gemini
344
- `);for(let F=0;F<Q.iterations;F++){let w=F+1,A=Math.max(1,Math.ceil(Q.breadth/2**F));if(process.stderr.write(`PROGRESS:research:round-${w}:planning
345
- `),!z)try{let O=await T0($9($,A,M,D,[...L]),{timeoutMs:120000}),T=V9(O,A);if(F===0)T.unshift({type:"search",query:$,researchGoal:"Original user query"});T=await K9(T,L),z=T}catch(O){process.stderr.write(`[greedysearch] Action planning failed, using fallback queries: ${O.message}
346
- `);let T=i6(null,$,A,{includeOriginal:F===0,exclude:P});z=Y9(T)}let _=(z||[]).filter((O)=>{if(O.type==="search"){let T=!q2(O.query,P,{roundIndex:F,originalQuery:$});if(!T)process.stderr.write(`[greedysearch] Novelty gate rejected search: ${O.query}
347
- `);return T}if(O.type==="fetchUrl"){let T=!L.has(O.url);if(!T)process.stderr.write(`[greedysearch] Novelty gate rejected fetch: ${O.url}
348
- `);return T}return!1}).slice(0,A),m=P9(N,L);if(!_.some((O)=>O.type==="fetchUrl")&&m.length>0){let O=m[0];_.push({type:"fetchUrl",url:O.url,researchGoal:`Direct fetch of known academic source: ${O.label||O.url}`}),process.stderr.write(`[greedysearch] Forced fetchUrl for academic source: ${O.url}
349
- `)}let g=[];for(let O=0;O<_.length;O++){let T=_[O];process.stderr.write(`PROGRESS:research:round-${w}:action-${O+1}/${_.length}
350
- `),process.stderr.write(`[greedysearch] Action ${O+1}/${_.length} [${T.type}]: ${(T.query||T.url).slice(0,80)}
351
- `);let W0=await X9(T,{locale:W,short:K,usedQueries:P,usedUrls:L,maxChars:8000});if(g.push(W0),$$++,T.type==="search")N0++;if(T.type==="fetchUrl")B0++;if(!W0.ok)M0.push({round:w,type:T.type,target:T.query||T.url,error:W0.error}),process.stderr.write(`[greedysearch] Action failed: ${W0.error}
352
- `)}let d=g.filter((O)=>O.action.type==="search"),Z$=g.filter((O)=>O.action.type==="fetchUrl");F$(H,{roundNumber:w,actions:g}),N=D9([N,d.flatMap((O)=>O.sources||[]),Z$.flatMap((O)=>O.sources||[])]);for(let O of Z$)if(O.fetchResult)G.push(O.fetchResult);G=I2(G);let C=Math.max(0,Q.maxSources-G.filter((O)=>O?.content||O?.contentChars>100).length);if(C>0&&N.length>0){process.stderr.write(`PROGRESS:research:round-${w}:fetching
353
- `);let O=await c6(N,Math.min(C,N.length),8000,Math.min(3,C||1));G=I2([...G,...O]),N=P$(N,G)}G=R2(G,N),process.stderr.write(`PROGRESS:research:round-${w}:evidence
354
- `);let t=await B9({query:$,questions:H,fetchedSources:G,extractedSourceKeys:E});if(t.error)process.stderr.write(`[greedysearch] Evidence extraction failed: ${t.error}
355
- `);k=[...k,...t.evidence];for(let O of t.evidence)F$(H,{roundNumber:w,learningPayload:{answeredQuestions:O.answers||[],newQuestions:O.newQuestions||[]}});let h=g.map((O)=>({query:O.action.query||O.action.url||"",researchGoal:O.action.researchGoal||""}));process.stderr.write(`PROGRESS:research:round-${w}:learning
356
- `);let r={learnings:[],followUpQueries:[],gaps:[]},I0="";try{let O=await T0(M9($,h,d.map((T)=>({query:T.action.query,researchGoal:T.action.researchGoal,error:T.error||"",engines:e6(T.result)})),G,H,k),{timeoutMs:120000});r={...r,...r0(O,r)}}catch(O){I0=O.message,process.stderr.write(`[greedysearch] Learning extraction failed: ${O.message}
357
- `)}let U1=Array.isArray(r.learnings)?r.learnings.map((O)=>String(O)).filter(Boolean).slice(0,8):[],I$=Array.isArray(r.gaps)?r.gaps.map((O)=>String(O)).filter(Boolean).slice(0,6):[];M=Q0([...M,...U1]),D=Q0([...D,...I$]),F$(H,{roundNumber:w,actions:[],learningPayload:r,gaps:I$}),B.push({round:w,actions:g.map((O)=>({type:O.action.type,query:O.action.query||"",url:O.action.url||"",researchGoal:O.action.researchGoal||"",error:O.error||"",sourceCount:O.sources?.length||0})),learnings:U1,gaps:I$,evidence:t.evidence,evidenceError:t.error,learningError:I0}),process.stderr.write(`PROGRESS:research:round-${w}:evaluating
358
- `);let u=await r6($,B,M,D,f);f.push(u.score),D=Q0([...D,...u.knowledgeGaps||[]]),F$(H,{roundNumber:w,gaps:u.knowledgeGaps||[]});let A1=C2({sources:N,fetchedSources:G,gaps:D,questions:H,rounds:B,qualityScore:u.score,qualityThreshold:V,maxSources:Q.maxSources,requireCitations:!1,requireQuestions:!1});if(process.stderr.write(`[greedysearch] Quality score round ${w}: ${u.score.toFixed(1)} (shouldContinue: ${u.shouldContinue}, floor: ${A1.floorMet})
359
- `),u.score>=V&&A1.floorMet&&(!u.shouldContinue||u.terminationReason==="quality_threshold")){o=u.terminationReason||"quality_threshold",process.stderr.write(`[greedysearch] Research floor reached (score: ${u.score.toFixed(1)}). Terminating early.
360
- `);break}let U0=Math.max(1,Math.ceil(A/2)),J0=(r.followUpQueries||[]).map((O)=>({type:"search",query:Z0(String(O)),researchGoal:"Follow-up from learning extraction"})).filter((O)=>O.query&&O.query.toLowerCase()!==$.toLowerCase()).slice(0,U0);if(J0.length<U0&&u.nextActions.length>0){let O=u.nextActions.map((W0)=>b2(W0)).filter(Boolean);J0=[...J0,...O].slice(0,U0)}if(J0.length<U0&&D.length>0){let O=t6(D,$,P,U0-J0.length,F+1),T=O.map((W0)=>({type:"search",query:W0.query,researchGoal:W0.researchGoal}));if(J0=[...J0,...T].slice(0,U0),O.length>0)process.stderr.write(`[greedysearch] Generated ${O.length} gap-driven fallback actions.
341
+ `),"utf8"),i(E(z,"GAPS.md"),["# Gaps and caveats","","## Caveats",N1(K.caveats||[]),"","## Research gaps",N1(f),""].join(`
342
+ `),"utf8"),i(E(O,"manifest.json"),JSON.stringify({...Q,floor:Y,citationAudit:V},null,2),"utf8"),i(E(O,"rounds.json"),JSON.stringify(X,null,2),"utf8"),i(E(O,"sources.json"),JSON.stringify(Z,null,2),"utf8"),i(E(O,"questions.json"),JSON.stringify(B,null,2),"utf8"),i(E(O,"evidence.json"),JSON.stringify(W,null,2),"utf8"),i(E(N,"index.md"),["# Source index","",...k.map((P)=>{let L=P.title||P.url,x=P.finalUrl||P.url,o=P.contentPath?` — ${P.contentPath}`:"";return`- ${P.id||"?"}: [${L}](${x})${o}`}),""].join(`
343
+ `),"utf8"),{dir:H,statusPath:E(H,"STATUS.md"),summaryPath:E(z,"SUMMARY.md"),manifestPath:E(O,"manifest.json"),sourceCount:k.length,sourceFiles:k}}async function g2({query:$,breadth:X=3,iterations:Z=2,maxSources:J,locale:W=null,short:K=!1,qualityThreshold:V=8.5,writeBundle:Y=process.env.GREEDY_RESEARCH_BUNDLE!=="0",researchOutDir:Q=null}={}){let j=s6({breadth:X,iterations:Z,maxSources:J}),B=[],M=[],D=[],H=k9($),z=null,N=[],O=[],k=[],f=new Set,P=new Set,L=new Set,x=[],o="max_rounds",C0=new Date().toISOString(),C$=Date.now(),X$=0,N0=0,B0=0,M0=[];process.stderr.write(`[greedysearch] Research mode: breadth ${j.breadth}, iterations ${j.iterations}, qualityThreshold ${V}, engines ${S$.join(",")}, synthesizer gemini
344
+ `);for(let F=0;F<j.iterations;F++){let T=F+1,A=Math.max(1,Math.ceil(j.breadth/2**F));if(process.stderr.write(`PROGRESS:research:round-${T}:planning
345
+ `),!z)try{let G=await T0(W9($,A,M,D,[...L]),{timeoutMs:120000}),C=B9(G,A);if(F===0)C.unshift({type:"search",query:$,researchGoal:"Original user query"});C=await j9(C,L),z=C}catch(G){process.stderr.write(`[greedysearch] Action planning failed, using fallback queries: ${G.message}
346
+ `);let C=t6(null,$,A,{includeOriginal:F===0,exclude:P});z=M9(C)}let _=(z||[]).filter((G)=>{if(G.type==="search"){let C=!f2(G.query,P,{roundIndex:F,originalQuery:$});if(!C)process.stderr.write(`[greedysearch] Novelty gate rejected search: ${G.query}
347
+ `);return C}if(G.type==="fetchUrl"){let C=!L.has(G.url);if(!C)process.stderr.write(`[greedysearch] Novelty gate rejected fetch: ${G.url}
348
+ `);return C}return!1}).slice(0,A),m=L9(N,L);if(!_.some((G)=>G.type==="fetchUrl")&&m.length>0){let G=m[0];_.push({type:"fetchUrl",url:G.url,researchGoal:`Direct fetch of known academic source: ${G.label||G.url}`}),process.stderr.write(`[greedysearch] Forced fetchUrl for academic source: ${G.url}
349
+ `)}let g=[];for(let G=0;G<_.length;G++){let C=_[G];process.stderr.write(`PROGRESS:research:round-${T}:action-${G+1}/${_.length}
350
+ `),process.stderr.write(`[greedysearch] Action ${G+1}/${_.length} [${C.type}]: ${(C.query||C.url).slice(0,80)}
351
+ `);let W0=await K9(C,{locale:W,short:K,usedQueries:P,usedUrls:L,maxChars:8000});if(g.push(W0),X$++,C.type==="search")N0++;if(C.type==="fetchUrl")B0++;if(!W0.ok)M0.push({round:T,type:C.type,target:C.query||C.url,error:W0.error}),process.stderr.write(`[greedysearch] Action failed: ${W0.error}
352
+ `)}let d=g.filter((G)=>G.action.type==="search"),J$=g.filter((G)=>G.action.type==="fetchUrl");F$(H,{roundNumber:T,actions:g}),N=A9([N,d.flatMap((G)=>G.sources||[]),J$.flatMap((G)=>G.sources||[])]);for(let G of J$)if(G.fetchResult)O.push(G.fetchResult);O=E2(O);let R=Math.max(0,j.maxSources-O.filter((G)=>G?.content||G?.contentChars>100).length);if(R>0&&N.length>0){process.stderr.write(`PROGRESS:research:round-${T}:fetching
353
+ `);let G=await a6(N,Math.min(R,N.length),8000,Math.min(3,R||1));O=E2([...O,...G]),N=P$(N,O)}O=v2(O,N),process.stderr.write(`PROGRESS:research:round-${T}:evidence
354
+ `);let t=await G9({query:$,questions:H,fetchedSources:O,extractedSourceKeys:f});if(t.error)process.stderr.write(`[greedysearch] Evidence extraction failed: ${t.error}
355
+ `);k=[...k,...t.evidence];for(let G of t.evidence)F$(H,{roundNumber:T,learningPayload:{answeredQuestions:G.answers||[],newQuestions:G.newQuestions||[]}});let h=g.map((G)=>({query:G.action.query||G.action.url||"",researchGoal:G.action.researchGoal||""}));process.stderr.write(`PROGRESS:research:round-${T}:learning
356
+ `);let r={learnings:[],followUpQueries:[],gaps:[]},I0="";try{let G=await T0(D9($,h,d.map((C)=>({query:C.action.query,researchGoal:C.action.researchGoal,error:C.error||"",engines:J9(C.result)})),O,H,k),{timeoutMs:120000});r={...r,...e0(G,r)}}catch(G){I0=G.message,process.stderr.write(`[greedysearch] Learning extraction failed: ${G.message}
357
+ `)}let k1=Array.isArray(r.learnings)?r.learnings.map((G)=>String(G)).filter(Boolean).slice(0,8):[],I$=Array.isArray(r.gaps)?r.gaps.map((G)=>String(G)).filter(Boolean).slice(0,6):[];M=j0([...M,...k1]),D=j0([...D,...I$]),F$(H,{roundNumber:T,actions:[],learningPayload:r,gaps:I$}),B.push({round:T,actions:g.map((G)=>({type:G.action.type,query:G.action.query||"",url:G.action.url||"",researchGoal:G.action.researchGoal||"",error:G.error||"",sourceCount:G.sources?.length||0})),learnings:k1,gaps:I$,evidence:t.evidence,evidenceError:t.error,learningError:I0}),process.stderr.write(`PROGRESS:research:round-${T}:evaluating
358
+ `);let u=await Z9($,B,M,D,x);x.push(u.score),D=j0([...D,...u.knowledgeGaps||[]]),F$(H,{roundNumber:T,gaps:u.knowledgeGaps||[]});let F1=b2({sources:N,fetchedSources:O,gaps:D,questions:H,rounds:B,qualityScore:u.score,qualityThreshold:V,maxSources:j.maxSources,requireCitations:!1,requireQuestions:!1});if(process.stderr.write(`[greedysearch] Quality score round ${T}: ${u.score.toFixed(1)} (shouldContinue: ${u.shouldContinue}, floor: ${F1.floorMet})
359
+ `),u.score>=V&&F1.floorMet&&(!u.shouldContinue||u.terminationReason==="quality_threshold")){o=u.terminationReason||"quality_threshold",process.stderr.write(`[greedysearch] Research floor reached (score: ${u.score.toFixed(1)}). Terminating early.
360
+ `);break}let U0=Math.max(1,Math.ceil(A/2)),J0=(r.followUpQueries||[]).map((G)=>({type:"search",query:Z0(String(G)),researchGoal:"Follow-up from learning extraction"})).filter((G)=>G.query&&G.query.toLowerCase()!==$.toLowerCase()).slice(0,U0);if(J0.length<U0&&u.nextActions.length>0){let G=u.nextActions.map((W0)=>x2(W0)).filter(Boolean);J0=[...J0,...G].slice(0,U0)}if(J0.length<U0&&D.length>0){let G=X9(D,$,P,U0-J0.length,F+1),C=G.map((W0)=>({type:"search",query:W0.query,researchGoal:W0.researchGoal}));if(J0=[...J0,...C].slice(0,U0),G.length>0)process.stderr.write(`[greedysearch] Generated ${G.length} gap-driven fallback actions.
361
361
  `)}z=J0.length>=U0?J0:null}process.stderr.write(`PROGRESS:research:final-report
362
362
  `);let S={answer:M.length?M.map((F)=>`- ${F}`).join(`
363
- `):"Research completed, but no structured learnings were extracted.",agreement:{level:"mixed",summary:"Research synthesis fallback."},differences:[],caveats:[],claims:[],recommendedSources:N.slice(0,4).map((F)=>F.id),synthesized:!1};try{let F=await T0(H9($,B,N,H,k),{timeoutMs:180000}),w=r0(F,{}),A=Array.isArray(w?.claims)&&w.claims.length>0;S={...S,...w,rawAnswer:F.answer||"",geminiSources:F.sources||[],synthesized:A}}catch(F){process.stderr.write(`[greedysearch] Final report failed: ${F.message}
363
+ `):"Research completed, but no structured learnings were extracted.",agreement:{level:"mixed",summary:"Research synthesis fallback."},differences:[],caveats:[],claims:[],recommendedSources:N.slice(0,4).map((F)=>F.id),synthesized:!1};try{let F=await T0(N9($,B,N,H,k),{timeoutMs:180000}),T=e0(F,{}),A=Array.isArray(T?.claims)&&T.claims.length>0;S={...S,...T,rawAnswer:F.answer||"",geminiSources:F.sources||[],synthesized:A}}catch(F){process.stderr.write(`[greedysearch] Final report failed: ${F.message}
364
364
  `),S.error=F.message}if(!(S.synthesized===!0&&Array.isArray(S.claims)&&S.claims.length>0)&&k.length>0){process.stderr.write(`[greedysearch] Falling back to evidence-based synthesis (no per-round learnings).
365
- `);try{let F=G9($,N,H,k),w=await T0(F,{timeoutMs:180000}),A=r0(w,{});S={...S,...A,rawAnswer:w.answer||S.answer||"",geminiSources:w.sources||S.geminiSources||[],synthesized:!0,synthesisMode:"evidence_fallback"}}catch(F){process.stderr.write(`[greedysearch] Evidence-based synthesis failed: ${F.message}
366
- `),S.evidenceFallbackError=F.message}}let a=new Date().toISOString(),X$=Date.now()-C$,z0=f.at(-1)||0;G=R2(G,N),process.stderr.write(`PROGRESS:research:audit-citations
367
- `);let R0=z9(S.answer||"",N);_9(H,S,R0);let v=C2({sources:N,fetchedSources:G,synthesis:S,citationAudit:R0,gaps:D,questions:H,rounds:B,qualityScore:z0,qualityThreshold:V,maxSources:Q.maxSources});if(v.floorMet&&o==="max_rounds")o="done_floor_met";else if(!v.floorMet&&o==="quality_threshold")o="max_rounds_floor_unmet";let s={startedAt:C0,finishedAt:a,durationMs:X$,engines:S$,synthesizer:"gemini",rounds:B.length,actionsRun:$$,searches:N0,fetches:B0,sourcesFetched:G.filter((F)=>F?.contentChars>100).length,engineFailures:M0,terminationReason:o,floorMet:v.floorMet},U=null,R;if(Y){process.stderr.write(`PROGRESS:research:bundle
368
- `);try{U=await F9({query:$,rounds:B,sources:N,fetchedSources:G,evidenceItems:k,synthesis:S,citationAudit:R0,floor:v,manifest:s,allGaps:D,questions:H,outDir:j}),R=U.sourceFiles,delete U.sourceFiles}catch(F){U={error:F.message||String(F)},R=await D1(G)}}else R=await D1(G);return process.stderr.write(`PROGRESS:research:done
369
- `),{query:$,_research:{mode:"iterative",breadth:Q.breadth,iterations:Q.iterations,maxSources:Q.maxSources,rounds:B,learnings:M,gaps:D,evidence:k,questions:H,questionProgress:N1(H),qualityHistory:f,terminationReason:o,qualityThreshold:V,floor:v,bundle:U,manifest:s},_citationAudit:R0,_sources:N,_fetchedSources:R,_synthesis:S,_confidence:{sourcesCount:N.length,fetchedSourceSuccessRate:G.length>0?Number((G.filter((F)=>F.contentChars>100).length/G.length).toFixed(2)):0,agreementLevel:S.agreement?.level||"mixed",floorMet:v.floorMet}}}function I2($){let X=new Map;for(let J of $){let W=J?.id||$0(J?.finalUrl||J?.url||"");if(!W)continue;let K=X.get(W);if(!K||(J.contentChars||0)>(K.contentChars||0))X.set(W,J)}let Z=[];for(let J of X.values()){let W=String(J.content||J.snippet||""),K=Z.findIndex((V)=>{let Y=String(V.content||V.snippet||"");if(W.length<400||Y.length<400)return!1;return T$(W.slice(0,4000),Y.slice(0,4000))>=0.9});if(K===-1){Z.push(J);continue}if((J.contentChars||0)>(Z[K].contentChars||0))Z[K]=J}return Z}var R9=x2(C9(),".config","greedysearch"),f2=x2(R9,"config.json");function I9(){try{if(w9(f2))return JSON.parse(T9(f2,"utf8"))}catch{}return{}}function e0($){try{L9(w1,`${JSON.stringify({at:new Date().toISOString(),...$})}
370
- `,"utf8")}catch{}}async function q9(){return new Promise(($)=>{let X="";if(process.stdin.setEncoding("utf8"),process.stdin.on("data",(Z)=>X+=Z),process.stdin.on("end",()=>$(X.trim())),process.stdin.isTTY)$("")})}async function b9(){let $=process.argv.slice(2);if($.length<2||$[0]==="--help")process.stderr.write(`${['Usage: node search.mjs <engine> "<query>"',"","Engines: all, perplexity (p), google (g), chatgpt (gpt), gemini (gem), semantic-scholar (s2), logically (log), bing (b)","","Flags:"," --synthesize For engine=all: synthesize fetched sources"," --synthesizer <engine> Synthesis engine (default from ~/.dm/greedyconfig)"," --fast Legacy quick mode: no source fetching or synthesis"," --depth <mode> Legacy: fast|standard|deep aliases, or research"," --deep-research Deprecated alias for --research"," --research Iterative query/learnings loop (alias: --depth research)"," --breadth <n> Research mode query breadth, 1-5 (default: 3)"," --iterations <n> Research mode rounds, 1-3 (default: 2)"," --max-sources <n> Research mode fetched source cap, 3-12"," --research-out-dir <dir> Write research bundle to a specific directory"," --no-research-bundle Disable the default .dm/greedysearch-research bundle"," --fetch-top-source Fetch content from top source"," --inline Output JSON to stdout (for piping)"," --locale <lang> Force results language (en, de, fr, etc.)"," --visible Always use visible Chrome for this search"," --always-visible Alias for --visible"," --stdin Read query from stdin (avoids command-line leakage)","","Environment:"," GREEDY_SEARCH_VISIBLE Set to 1 to show Chrome window (disables headless)"," GREEDY_SEARCH_ALWAYS_VISIBLE Set to 1 to force visible mode for all runs"," GREEDY_SEARCH_LOCALE Default locale (default: en)","","Examples:",' node search.mjs all "Node.js streams" # Grounded: engines + fetched sources',' node search.mjs all "Node.js streams" --synthesize # Add Gemini synthesis',' node search.mjs all "quick check" --fast # Legacy fast: no sources/synthesis',' node search.mjs all "browser automation" --research --breadth 3 --iterations 2',' node search.mjs p "what is memoization" # Single engine search'].join(`
365
+ `);try{let F=z9($,N,H,k),T=await T0(F,{timeoutMs:180000}),A=e0(T,{});S={...S,...A,rawAnswer:T.answer||S.answer||"",geminiSources:T.sources||S.geminiSources||[],synthesized:!0,synthesisMode:"evidence_fallback"}}catch(F){process.stderr.write(`[greedysearch] Evidence-based synthesis failed: ${F.message}
366
+ `),S.evidenceFallbackError=F.message}}let a=new Date().toISOString(),Z$=Date.now()-C$,z0=x.at(-1)||0;O=v2(O,N),process.stderr.write(`PROGRESS:research:audit-citations
367
+ `);let R0=_9(S.answer||"",N);w9(H,S,R0);let v=b2({sources:N,fetchedSources:O,synthesis:S,citationAudit:R0,gaps:D,questions:H,rounds:B,qualityScore:z0,qualityThreshold:V,maxSources:j.maxSources});if(v.floorMet&&o==="max_rounds")o="done_floor_met";else if(!v.floorMet&&o==="quality_threshold")o="max_rounds_floor_unmet";let s={startedAt:C0,finishedAt:a,durationMs:Z$,engines:S$,synthesizer:"gemini",rounds:B.length,actionsRun:X$,searches:N0,fetches:B0,sourcesFetched:O.filter((F)=>F?.contentChars>100).length,engineFailures:M0,terminationReason:o,floorMet:v.floorMet},U=null,w;if(Y){process.stderr.write(`PROGRESS:research:bundle
368
+ `);try{U=await C9({query:$,rounds:B,sources:N,fetchedSources:O,evidenceItems:k,synthesis:S,citationAudit:R0,floor:v,manifest:s,allGaps:D,questions:H,outDir:Q}),w=U.sourceFiles,delete U.sourceFiles}catch(F){U={error:F.message||String(F)},w=await z1(O)}}else w=await z1(O);return process.stderr.write(`PROGRESS:research:done
369
+ `),{query:$,_research:{mode:"iterative",breadth:j.breadth,iterations:j.iterations,maxSources:j.maxSources,rounds:B,learnings:M,gaps:D,evidence:k,questions:H,questionProgress:U1(H),qualityHistory:x,terminationReason:o,qualityThreshold:V,floor:v,bundle:U,manifest:s},_citationAudit:R0,_sources:N,_fetchedSources:w,_synthesis:S,_confidence:{sourcesCount:N.length,fetchedSourceSuccessRate:O.length>0?Number((O.filter((F)=>F.contentChars>100).length/O.length).toFixed(2)):0,agreementLevel:S.agreement?.level||"mixed",floorMet:v.floorMet}}}function E2($){let X=new Map;for(let J of $){let W=J?.id||$0(J?.finalUrl||J?.url||"");if(!W)continue;let K=X.get(W);if(!K||(J.contentChars||0)>(K.contentChars||0))X.set(W,J)}let Z=[];for(let J of X.values()){let W=String(J.content||J.snippet||""),K=Z.findIndex((V)=>{let Y=String(V.content||V.snippet||"");if(W.length<400||Y.length<400)return!1;return T$(W.slice(0,4000),Y.slice(0,4000))>=0.9});if(K===-1){Z.push(J);continue}if((J.contentChars||0)>(Z[K].contentChars||0))Z[K]=J}return Z}var b9=h2(q9(),".config","greedysearch"),y2=h2(b9,"config.json");function v9(){try{if(P1(y2))return JSON.parse(I9(y2,"utf8"))}catch{}return{}}function $$($){try{R9(I1,`${JSON.stringify({at:new Date().toISOString(),...$})}
370
+ `,"utf8")}catch{}}async function E9(){return new Promise(($)=>{let X="";if(process.stdin.setEncoding("utf8"),process.stdin.on("data",(Z)=>X+=Z),process.stdin.on("end",()=>$(X.trim())),process.stdin.isTTY)$("")})}async function f9(){let $=process.argv.slice(2);if($[0]==="--dm-print-helper-paths"){let U=i$(),w=A1();console.log(JSON.stringify({launchScript:U,launchScriptExists:P1(U),searchScript:w,searchScriptExists:P1(w)}));return}if($.length<2||$[0]==="--help")process.stderr.write(`${['Usage: node search.mjs <engine> "<query>"',"","Engines: all, perplexity (p), google (g), chatgpt (gpt), gemini (gem), semantic-scholar (s2), logically (log), bing (b)","","Flags:"," --synthesize For engine=all: synthesize fetched sources"," --synthesizer <engine> Synthesis engine (default from ~/.dm/greedyconfig)"," --fast Legacy quick mode: no source fetching or synthesis"," --depth <mode> Legacy: fast|standard|deep aliases, or research"," --deep-research Deprecated alias for --research"," --research Iterative query/learnings loop (alias: --depth research)"," --breadth <n> Research mode query breadth, 1-5 (default: 3)"," --iterations <n> Research mode rounds, 1-3 (default: 2)"," --max-sources <n> Research mode fetched source cap, 3-12"," --research-out-dir <dir> Write research bundle to a specific directory"," --no-research-bundle Disable the default .dm/greedysearch-research bundle"," --fetch-top-source Fetch content from top source"," --inline Output JSON to stdout (for piping)"," --locale <lang> Force results language (en, de, fr, etc.)"," --visible Always use visible Chrome for this search"," --always-visible Alias for --visible"," --stdin Read query from stdin (avoids command-line leakage)","","Environment:"," GREEDY_SEARCH_VISIBLE Set to 1 to show Chrome window (disables headless)"," GREEDY_SEARCH_ALWAYS_VISIBLE Set to 1 to force visible mode for all runs"," GREEDY_SEARCH_LOCALE Default locale (default: en)","","Examples:",' node search.mjs all "Node.js streams" # Grounded: engines + fetched sources',' node search.mjs all "Node.js streams" --synthesize # Add Gemini synthesis',' node search.mjs all "quick check" --fast # Legacy fast: no sources/synthesis',' node search.mjs all "browser automation" --research --breadth 3 --iterations 2',' node search.mjs p "what is memoization" # Single engine search'].join(`
371
371
  `)}
372
- `),process.exit(1);if($.includes("--visible")||$.includes("--always-visible")||process.env.GREEDY_SEARCH_ALWAYS_VISIBLE==="1")process.env.GREEDY_SEARCH_VISIBLE="1",process.env.GREEDY_SEARCH_ALWAYS_VISIBLE="1",delete process.env.GREEDY_SEARCH_HEADLESS;else if(process.env.GREEDY_SEARCH_VISIBLE!=="1")process.env.GREEDY_SEARCH_HEADLESS="1";await n0(),G$();let Z=$.indexOf("--depth"),J=Z!==-1&&$[Z+1]?$[Z+1].toLowerCase():null,W=$.find((U)=>!U.startsWith("--"))?.toLowerCase(),K=$.includes("--research")||$.includes("--deep-research")||J==="research",V=$.includes("--fast")||J==="fast",Y=J==="standard"||J==="deep"||$.includes("--deep"),j=W==="all"&&!V,Q=W==="all"&&!V&&($.includes("--synthesize")||Y),B=J==="deep"||$.includes("--deep");if($.includes("--deep-research"))process.stderr.write(`[greedysearch] --deep-research is deprecated; use --research or --depth research
372
+ `),process.exit(1);if($.includes("--visible")||$.includes("--always-visible")||process.env.GREEDY_SEARCH_ALWAYS_VISIBLE==="1")process.env.GREEDY_SEARCH_VISIBLE="1",process.env.GREEDY_SEARCH_ALWAYS_VISIBLE="1",delete process.env.GREEDY_SEARCH_HEADLESS;else if(process.env.GREEDY_SEARCH_VISIBLE!=="1")process.env.GREEDY_SEARCH_HEADLESS="1";await i0(),O$();let Z=$.indexOf("--depth"),J=Z!==-1&&$[Z+1]?$[Z+1].toLowerCase():null,W=$.find((U)=>!U.startsWith("--"))?.toLowerCase(),K=$.includes("--research")||$.includes("--deep-research")||J==="research",V=$.includes("--fast")||J==="fast",Y=J==="standard"||J==="deep"||$.includes("--deep"),Q=W==="all"&&!V,j=W==="all"&&!V&&($.includes("--synthesize")||Y),B=J==="deep"||$.includes("--deep");if($.includes("--deep-research"))process.stderr.write(`[greedysearch] --deep-research is deprecated; use --research or --depth research
373
373
  `);if(Y)process.stderr.write(`[greedysearch] depth fast|standard|deep is deprecated; use default grounded search plus --synthesize when needed
374
- `);let M=$.indexOf("--synthesizer"),D=t0(M===-1?T1:$[M+1]),z=!$.includes("--full"),N=$.includes("--fetch-top-source"),G=$.includes("--inline"),k=$.indexOf("--breadth"),E=$.indexOf("--iterations"),P=$.indexOf("--max-sources"),L=k===-1?void 0:$[k+1],f=E===-1?void 0:$[E+1],o=P===-1?void 0:$[P+1],C0=$.indexOf("--research-out-dir"),C$=C0===-1?void 0:$[C0+1],$$=!$.includes("--no-research-bundle"),N0=$.indexOf("--out"),B0=N0===-1?null:$[N0+1],M0=$.indexOf("--locale"),S=process.env.GREEDY_SEARCH_LOCALE,R$=I9(),a="en";if(M0!==-1&&$[M0+1])a=$[M0+1];else if(S)a=S;else if(R$.locale)a=R$.locale;let X$=$.filter((U,R)=>U!=="--full"&&U!=="--short"&&U!=="--fast"&&U!=="--fetch-top-source"&&U!=="--synthesize"&&U!=="--deep-research"&&U!=="--deep"&&U!=="--research"&&U!=="--inline"&&U!=="--stdin"&&U!=="--headless"&&U!=="--visible"&&U!=="--always-visible"&&U!=="--depth"&&U!=="--synthesizer"&&U!=="--out"&&U!=="--locale"&&U!=="--breadth"&&U!=="--iterations"&&U!=="--max-sources"&&U!=="--research-out-dir"&&U!=="--no-research-bundle"&&U!=="--help"&&(Z===-1||R!==Z+1)&&(M===-1||R!==M+1)&&(N0===-1||R!==N0+1)&&(M0===-1||R!==M0+1)&&(k===-1||R!==k+1)&&(E===-1||R!==E+1)&&(P===-1||R!==P+1)&&(C0===-1||R!==C0+1)),z0=X$[0]?.toLowerCase(),R0=$.includes("--stdin"),v;if(R0)v=await q9();else v=X$.slice(1).join(" ");if(K){if(z0!=="all")process.stderr.write(`[greedysearch] Research mode uses all engines; ignoring engine "${z0}".
375
- `);let U=await E2({query:k$(v),breadth:L,iterations:f,maxSources:o,locale:a,short:z,writeBundle:$$,researchOutDir:C$});S0(U,B0,{inline:G,synthesize:!0,query:v});return}if(z0==="all"){await b(["list"]);let U={perplexity:"https://www.perplexity.ai/",google:"https://www.google.com/","semantic-scholar":"https://www.semanticscholar.org/",semanticscholar:"https://www.semanticscholar.org/",s2:"https://www.semanticscholar.org/",logically:"https://logically.app/research-assistant/"},R=await Promise.all(e.map((w)=>j0(U[w])));await b(["list"]);let F=(w)=>{if(!V)return 70000;return w==="chatgpt"?60000:35000};try{let w=await Promise.allSettled(e.map((_,m)=>E0(X0[_],k$(v),R[m],z,F(_),a).then((l)=>{return process.stderr.write(`PROGRESS:${_}:done
376
- `),{engine:_,...l}}).catch((l)=>{throw l}))),A={};for(let _=0;_<w.length;_++){let m=w[_];if(m.status==="fulfilled")A[m.value.engine]=m.value;else{let l=m.reason,g=l?.message||"unknown error";if(A[e[_]]={error:g},l?.lastStage)process.stderr.write(`[greedysearch] ${e[_]} failed at stage '${l.lastStage}': ${g}
374
+ `);let M=$.indexOf("--synthesizer"),D=r0(M===-1?q1:$[M+1]),z=!$.includes("--full"),N=$.includes("--fetch-top-source"),O=$.includes("--inline"),k=$.indexOf("--breadth"),f=$.indexOf("--iterations"),P=$.indexOf("--max-sources"),L=k===-1?void 0:$[k+1],x=f===-1?void 0:$[f+1],o=P===-1?void 0:$[P+1],C0=$.indexOf("--research-out-dir"),C$=C0===-1?void 0:$[C0+1],X$=!$.includes("--no-research-bundle"),N0=$.indexOf("--out"),B0=N0===-1?null:$[N0+1],M0=$.indexOf("--locale"),S=process.env.GREEDY_SEARCH_LOCALE,R$=v9(),a="en";if(M0!==-1&&$[M0+1])a=$[M0+1];else if(S)a=S;else if(R$.locale)a=R$.locale;let Z$=$.filter((U,w)=>U!=="--full"&&U!=="--short"&&U!=="--fast"&&U!=="--fetch-top-source"&&U!=="--synthesize"&&U!=="--deep-research"&&U!=="--deep"&&U!=="--research"&&U!=="--inline"&&U!=="--stdin"&&U!=="--headless"&&U!=="--visible"&&U!=="--always-visible"&&U!=="--depth"&&U!=="--synthesizer"&&U!=="--out"&&U!=="--locale"&&U!=="--breadth"&&U!=="--iterations"&&U!=="--max-sources"&&U!=="--research-out-dir"&&U!=="--no-research-bundle"&&U!=="--help"&&(Z===-1||w!==Z+1)&&(M===-1||w!==M+1)&&(N0===-1||w!==N0+1)&&(M0===-1||w!==M0+1)&&(k===-1||w!==k+1)&&(f===-1||w!==f+1)&&(P===-1||w!==P+1)&&(C0===-1||w!==C0+1)),z0=Z$[0]?.toLowerCase(),R0=$.includes("--stdin"),v;if(R0)v=await E9();else v=Z$.slice(1).join(" ");if(K){if(z0!=="all")process.stderr.write(`[greedysearch] Research mode uses all engines; ignoring engine "${z0}".
375
+ `);let U=await g2({query:k$(v),breadth:L,iterations:x,maxSources:o,locale:a,short:z,writeBundle:X$,researchOutDir:C$});S0(U,B0,{inline:O,synthesize:!0,query:v});return}if(z0==="all"){await b(["list"]);let U={perplexity:"https://www.perplexity.ai/",google:"https://www.google.com/","semantic-scholar":"https://www.semanticscholar.org/",semanticscholar:"https://www.semanticscholar.org/",s2:"https://www.semanticscholar.org/",logically:"https://logically.app/research-assistant/"},w=await Promise.all(e.map((T)=>Q0(U[T])));await b(["list"]);let F=(T)=>{if(!V)return 70000;return T==="chatgpt"?60000:35000};try{let T=await Promise.allSettled(e.map((_,m)=>E0(X0[_],k$(v),w[m],z,F(_),a).then((l)=>{return process.stderr.write(`PROGRESS:${_}:done
376
+ `),{engine:_,...l}}).catch((l)=>{throw l}))),A={};for(let _=0;_<T.length;_++){let m=T[_];if(m.status==="fulfilled")A[m.value.engine]=m.value;else{let l=m.reason,g=l?.message||"unknown error";if(A[e[_]]={error:g},l?.lastStage)process.stderr.write(`[greedysearch] ${e[_]} failed at stage '${l.lastStage}': ${g}
377
377
  `);if(l?.partialErr)process.stderr.write(`[greedysearch] ${e[_]} tail stderr:
378
378
  ${l.partialErr}
379
- `)}}let q=U2(A);if(q.length>0&&process.env.GREEDY_SEARCH_VISIBLE!=="1"){e0({scope:"all",phase:"start",engines:q,reasons:Object.fromEntries(q.map((g)=>[g,{error:A[g]?.error||null,envelope:A[g]?._envelope||null}]))}),process.stderr.write(`[greedysearch] \uD83D\uDD13 Headless ${q.join(", ")} search hit timeout/verification/antibot signals — retrying visible to establish cookies...
379
+ `)}}let q=k2(A);if(q.length>0&&process.env.GREEDY_SEARCH_VISIBLE!=="1"){$$({scope:"all",phase:"start",engines:q,reasons:Object.fromEntries(q.map((g)=>[g,{error:A[g]?.error||null,envelope:A[g]?._envelope||null}]))}),process.stderr.write(`[greedysearch] \uD83D\uDD13 Headless ${q.join(", ")} search hit timeout/verification/antibot signals — retrying visible to establish cookies...
380
380
  `);for(let g of q)process.stderr.write(`[greedysearch] ${g} recovery starting in visible mode...
381
- `);await O$(R),await L0(),process.env.GREEDY_SEARCH_VISIBLE="1",delete process.env.GREEDY_SEARCH_HEADLESS,await n0(),await b(["list"]);let _=[],m=!1,l=0;for(let g=0;g<q.length;g++){let d=await j0();_.push(d)}try{let g=await Promise.allSettled(q.map((C,t)=>E0(X0[C],v,_[t],z,null,a).then((h)=>({engine:C,...h})).catch((h)=>({engine:C,error:h.message})))),d=[],Z$=[];for(let C of g)if(C.status==="fulfilled"&&!C.value.error)A[C.value.engine]=C.value,l++,process.stderr.write(`PROGRESS:${C.value.engine}:done
382
- `);else if(C.status==="fulfilled"){if(A[C.value.engine]=C.value,d.push(C.value.engine),Q1(C.value.error))Z$.push(C.value.engine)}if(l>0)process.stderr.write(`[greedysearch] ✅ ${l}/${q.length} engine(s) recovered — cookies cached for future headless runs.
381
+ `);await G$(w),await L0(),process.env.GREEDY_SEARCH_VISIBLE="1",delete process.env.GREEDY_SEARCH_HEADLESS,await i0(),await b(["list"]);let _=[],m=!1,l=0;for(let g=0;g<q.length;g++){let d=await Q0();_.push(d)}try{let g=await Promise.allSettled(q.map((R,t)=>E0(X0[R],v,_[t],z,null,a).then((h)=>({engine:R,...h})).catch((h)=>({engine:R,error:h.message})))),d=[],J$=[];for(let R of g)if(R.status==="fulfilled"&&!R.value.error)A[R.value.engine]=R.value,l++,process.stderr.write(`PROGRESS:${R.value.engine}:done
382
+ `);else if(R.status==="fulfilled"){if(A[R.value.engine]=R.value,d.push(R.value.engine),M1(R.value.error))J$.push(R.value.engine)}if(l>0)process.stderr.write(`[greedysearch] ✅ ${l}/${q.length} engine(s) recovered — cookies cached for future headless runs.
383
383
  `);else process.stderr.write(`[greedysearch] ⚠️ Recovery attempt did not extract an answer — ${q.join(", ")} may still need manual verification or a DOM fallback.
384
384
  `);if(d.length>0){process.stderr.write(`[greedysearch] Second visible retry for ${d.join(", ")} — Turnstile may have resolved on first attempt...
385
- `);let C=await Promise.allSettled(d.map((h)=>{let r=q.indexOf(h);return E0(X0[h],v,_[r],z,null,a).then((I0)=>({engine:h,...I0})).catch((I0)=>({engine:h,error:I0.message}))})),t=[];for(let h of C)if(h.status==="fulfilled"&&!h.value.error)A[h.value.engine]=h.value,l++,process.stderr.write(`PROGRESS:${h.value.engine}:done
385
+ `);let R=await Promise.allSettled(d.map((h)=>{let r=q.indexOf(h);return E0(X0[h],v,_[r],z,null,a).then((I0)=>({engine:h,...I0})).catch((I0)=>({engine:h,error:I0.message}))})),t=[];for(let h of R)if(h.status==="fulfilled"&&!h.value.error)A[h.value.engine]=h.value,l++,process.stderr.write(`PROGRESS:${h.value.engine}:done
386
386
  `),process.stderr.write(`[greedysearch] ✅ ${h.value.engine} recovered on second visible retry.
387
- `);else t.push(h.value?.engine||"unknown");d.length=0,d.push(...t)}if(e0({scope:"all",phase:d.length>0?"needs-human":"success",engines:q,results:Object.fromEntries(q.map((C)=>[C,{mode:A[C]?._envelope?.mode||null,durationMs:A[C]?._envelope?.durationMs||null,lastStage:A[C]?._envelope?.lastStage||null,error:A[C]?.error||null}]))}),d.length>0){for(let C of d)process.stderr.write(`PROGRESS:${C}:needs-human
387
+ `);else t.push(h.value?.engine||"unknown");d.length=0,d.push(...t)}if($$({scope:"all",phase:d.length>0?"needs-human":"success",engines:q,results:Object.fromEntries(q.map((R)=>[R,{mode:A[R]?._envelope?.mode||null,durationMs:A[R]?._envelope?.durationMs||null,lastStage:A[R]?._envelope?.lastStage||null,error:A[R]?.error||null}]))}),d.length>0){for(let R of d)process.stderr.write(`PROGRESS:${R}:needs-human
388
388
  `);m=!0,A._needsHumanVerification={engines:d,message:"Visible Chrome is open with the engine page loaded. Solve the Turnstile checkbox or other challenge in the visible window to store cookies. Cookies persist for future runs."},process.stderr.write(`[greedysearch] \uD83D\uDD13 ${d.join(", ")} still blocked — keeping visible Chrome open. Solve the challenge in the window to store cookies, then rerun.
389
- `)}}finally{if(m)z1().catch(()=>{});else await O$(_),process.stderr.write(`[greedysearch] Switching back to headless Chrome...
390
- `),await L0(),delete process.env.GREEDY_SEARCH_VISIBLE,process.env.GREEDY_SEARCH_HEADLESS="1",await n0(),await b(["list"])}R.length=0}for(let _ of e){if(!A[_]?.error)continue;if(q.includes(_)){if(process.env.GREEDY_SEARCH_VISIBLE==="1")process.stderr.write(`PROGRESS:${_}:${Q1(A[_].error)?"needs-human":"error"}
389
+ `)}}finally{if(m)_1().catch(()=>{});else await G$(_),process.stderr.write(`[greedysearch] Switching back to headless Chrome...
390
+ `),await L0(),delete process.env.GREEDY_SEARCH_VISIBLE,process.env.GREEDY_SEARCH_HEADLESS="1",await i0(),await b(["list"])}w.length=0}for(let _ of e){if(!A[_]?.error)continue;if(q.includes(_)){if(process.env.GREEDY_SEARCH_VISIBLE==="1")process.stderr.write(`PROGRESS:${_}:${M1(A[_].error)?"needs-human":"error"}
391
391
  `);continue}process.stderr.write(`PROGRESS:${_}:error
392
- `)}if(A._sources=f0(A,v),j&&A._sources.length>0){process.stderr.write(`PROGRESS:source-fetch:start
393
- `);let _=await W1(A._sources,5,8000);A._sources=P$(A._sources,_),A._fetchedSources=V1(_),process.stderr.write(`PROGRESS:source-fetch:done
394
- `)}if(Q){process.stderr.write(`PROGRESS:synthesis:start
392
+ `)}if(A._sources=f0(A,v),Q&&A._sources.length>0){process.stderr.write(`PROGRESS:source-fetch:start
393
+ `);let _=await V1(A._sources,5,8000);A._sources=P$(A._sources,_),A._fetchedSources=Q1(_),process.stderr.write(`PROGRESS:source-fetch:done
394
+ `)}if(j){process.stderr.write(`PROGRESS:synthesis:start
395
395
  `),process.stderr.write(`[greedysearch] Synthesizing results with ${D}...
396
- `);let _=null;try{_=await j0(F2(D));let m=await w2(v,A,{grounded:B,tabPrefix:_,visible:process.env.GREEDY_SEARCH_VISIBLE==="1",synthesizer:D});A._synthesis={...m,synthesized:!0},process.stderr.write(`PROGRESS:synthesis:done
396
+ `);let _=null;try{_=await Q0(C2(D));let m=await I2(v,A,{grounded:B,tabPrefix:_,visible:process.env.GREEDY_SEARCH_VISIBLE==="1",synthesizer:D});A._synthesis={...m,synthesized:!0},process.stderr.write(`PROGRESS:synthesis:done
397
397
  `)}catch(m){process.stderr.write(`[greedysearch] Synthesis failed: ${m.message}
398
- `),A._synthesis={error:m.message,synthesized:!1,synthesizedBy:D}}finally{if(_)await G0(_)}}if(N){let _=v9(A);if(_)A._topSource=await a0(_.canonicalUrl||_.url)}if(!V)A._confidence=k2(A);S0(A,B0,{inline:G,synthesize:Q,query:v});return}finally{await O$(R)}}let s=X0[z0];if(!s)process.stderr.write(`Unknown engine: "${z0}"
398
+ `),A._synthesis={error:m.message,synthesized:!1,synthesizedBy:D}}finally{if(_)await O0(_)}}if(N){let _=x9(A);if(_)A._topSource=await s0(_.canonicalUrl||_.url)}if(!V)A._confidence=T2(A);S0(A,B0,{inline:O,synthesize:j,query:v});return}finally{await G$(w)}}let s=X0[z0];if(!s)process.stderr.write(`Unknown engine: "${z0}"
399
399
  Available: ${Object.keys(X0).join(", ")}
400
- `),process.exit(1);try{let U=await E0(s,k$(v),null,z,null,a);if(N&&U.sources?.length>0)U.topSource=await a0(U.sources[0].url);S0(U,B0,{inline:G,synthesize:!1,query:v})}catch(U){let R=s.includes("bing")?"bing":s.includes("perplexity")?"perplexity":s.includes("chatgpt")?"chatgpt":s.includes("semantic-scholar")?"semantic-scholar":s.includes("logically")?"logically":null;if(R&&process.env.GREEDY_SEARCH_VISIBLE!=="1"&&A2(U)){e0({scope:"single",phase:"start",engines:[R],reasons:{[R]:{error:U.message||null,envelope:U.envelope||null,lastStage:U.lastStage||null}}}),process.stderr.write(`[greedysearch] \uD83D\uDD13 ${R} blocked in headless — retrying visible to establish cookies...
401
- `),await L0(),process.env.GREEDY_SEARCH_VISIBLE="1",delete process.env.GREEDY_SEARCH_HEADLESS,await n0(),await b(["list"]);let w=await j0(),A=!1;try{let q=await E0(s,v,w,z,null,a);if(e0({scope:"single",phase:"success",engines:[R],result:{engine:R,mode:q._envelope?.mode||null,durationMs:q._envelope?.durationMs||null,lastStage:q._envelope?.lastStage||null}}),N&&q.sources?.length>0)q.topSource=await a0(q.sources[0].url);S0(q,B0,{inline:G,synthesize:!1,query:v});return}catch(q){e0({scope:"single",phase:"needs-human",engines:[R],result:{engine:R,error:q.message||String(q),envelope:q.envelope||null}}),A=!0,S0({query:v,error:q.message,_needsHumanVerification:{engines:[R],message:"Visible Chrome is open with the engine page loaded. Solve the Turnstile checkbox or other challenge to store cookies. Cookies persist for future runs."}},B0,{inline:G,synthesize:!1,query:v});return}finally{if(!A)await G0(w),await L0(),delete process.env.GREEDY_SEARCH_VISIBLE,process.env.GREEDY_SEARCH_HEADLESS="1";else z1().catch(()=>{})}}process.stderr.write(`Error: ${U.message}
402
- `),process.exit(1)}}function v9($){if(Array.isArray($._sources)&&$._sources.length>0)return $._sources[0];for(let X of["perplexity","google","bing"]){let Z=$[X];if(Z?.sources?.length>0)return Z.sources[0]}return null}async function z1(){if(process.env.GREEDY_SEARCH_HEADLESS==="1")return;try{let $=await import("node:http"),Z=(await new Promise((Y,j)=>{$.get("http://localhost:9222/json/version",(Q)=>{let B="";Q.on("data",(M)=>B+=M),Q.on("end",()=>Y(JSON.parse(B)))}).on("error",j)})).webSocketDebuggerUrl,J=globalThis.WebSocket;if(!J)return;let W=new J(Z),K=0,V=new Map;W.onopen=()=>{let Y=++K;V.set(Y,{resolve:(j)=>{let B=(j.targetInfos||[]).find((D)=>D.type==="page");if(!B){W.close();return}let M=++K;V.set(M,{resolve:(D)=>{let H=D.windowId,z=++K;V.set(z,{resolve:()=>{},reject:()=>{}}),W.send(JSON.stringify({id:z,method:"Browser.setWindowBounds",params:{windowId:H,bounds:{windowState:"minimized"}}})),setTimeout(()=>W.close(),500)},reject:()=>W.close()}),W.send(JSON.stringify({id:M,method:"Browser.getWindowForTarget",params:{targetId:B.targetId}}))},reject:()=>W.close()}),W.send(JSON.stringify({id:Y,method:"Target.getTargets",params:{}}))},W.onmessage=(Y)=>{let j=JSON.parse(Y.data);if(j.id&&V.has(j.id)){let{resolve:Q,reject:B}=V.get(j.id);if(V.delete(j.id),j.error)B?.(j.error);else Q?.(j.result)}},setTimeout(()=>W.close(),3000)}catch{}}b9().finally(async()=>{G$(),z1().catch(()=>{})});
400
+ `),process.exit(1);try{let U=await E0(s,k$(v),null,z,null,a);if(N&&U.sources?.length>0)U.topSource=await s0(U.sources[0].url);S0(U,B0,{inline:O,synthesize:!1,query:v})}catch(U){let w=s.includes("bing")?"bing":s.includes("perplexity")?"perplexity":s.includes("chatgpt")?"chatgpt":s.includes("semantic-scholar")?"semantic-scholar":s.includes("logically")?"logically":null;if(w&&process.env.GREEDY_SEARCH_VISIBLE!=="1"&&F2(U)){$$({scope:"single",phase:"start",engines:[w],reasons:{[w]:{error:U.message||null,envelope:U.envelope||null,lastStage:U.lastStage||null}}}),process.stderr.write(`[greedysearch] \uD83D\uDD13 ${w} blocked in headless — retrying visible to establish cookies...
401
+ `),await L0(),process.env.GREEDY_SEARCH_VISIBLE="1",delete process.env.GREEDY_SEARCH_HEADLESS,await i0(),await b(["list"]);let T=await Q0(),A=!1;try{let q=await E0(s,v,T,z,null,a);if($$({scope:"single",phase:"success",engines:[w],result:{engine:w,mode:q._envelope?.mode||null,durationMs:q._envelope?.durationMs||null,lastStage:q._envelope?.lastStage||null}}),N&&q.sources?.length>0)q.topSource=await s0(q.sources[0].url);S0(q,B0,{inline:O,synthesize:!1,query:v});return}catch(q){$$({scope:"single",phase:"needs-human",engines:[w],result:{engine:w,error:q.message||String(q),envelope:q.envelope||null}}),A=!0,S0({query:v,error:q.message,_needsHumanVerification:{engines:[w],message:"Visible Chrome is open with the engine page loaded. Solve the Turnstile checkbox or other challenge to store cookies. Cookies persist for future runs."}},B0,{inline:O,synthesize:!1,query:v});return}finally{if(!A)await O0(T),await L0(),delete process.env.GREEDY_SEARCH_VISIBLE,process.env.GREEDY_SEARCH_HEADLESS="1";else _1().catch(()=>{})}}process.stderr.write(`Error: ${U.message}
402
+ `),process.exit(1)}}function x9($){if(Array.isArray($._sources)&&$._sources.length>0)return $._sources[0];for(let X of["perplexity","google","bing"]){let Z=$[X];if(Z?.sources?.length>0)return Z.sources[0]}return null}async function _1(){if(process.env.GREEDY_SEARCH_HEADLESS==="1")return;try{let $=await import("node:http"),Z=(await new Promise((Y,Q)=>{$.get("http://localhost:9222/json/version",(j)=>{let B="";j.on("data",(M)=>B+=M),j.on("end",()=>Y(JSON.parse(B)))}).on("error",Q)})).webSocketDebuggerUrl,J=globalThis.WebSocket;if(!J)return;let W=new J(Z),K=0,V=new Map;W.onopen=()=>{let Y=++K;V.set(Y,{resolve:(Q)=>{let B=(Q.targetInfos||[]).find((D)=>D.type==="page");if(!B){W.close();return}let M=++K;V.set(M,{resolve:(D)=>{let H=D.windowId,z=++K;V.set(z,{resolve:()=>{},reject:()=>{}}),W.send(JSON.stringify({id:z,method:"Browser.setWindowBounds",params:{windowId:H,bounds:{windowState:"minimized"}}})),setTimeout(()=>W.close(),500)},reject:()=>W.close()}),W.send(JSON.stringify({id:M,method:"Browser.getWindowForTarget",params:{targetId:B.targetId}}))},reject:()=>W.close()}),W.send(JSON.stringify({id:Y,method:"Target.getTargets",params:{}}))},W.onmessage=(Y)=>{let Q=JSON.parse(Y.data);if(Q.id&&V.has(Q.id)){let{resolve:j,reject:B}=V.get(Q.id);if(V.delete(Q.id),Q.error)B?.(Q.error);else j?.(Q.result)}},setTimeout(()=>W.close(),3000)}catch{}}f9().finally(async()=>{O$(),_1().catch(()=>{})});