@duckmind/dm-darwin-x64 0.42.7 → 0.43.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
- import{randomInt as gf}from"node:crypto";import{spawn as of}from"node:child_process";import{dirname as lf,join as wf}from"node:path";import{fileURLToPath as rf}from"node:url";import{basename as hf}from"node:path";function $f(n=process.env,f=process.execPath){let u=n.GREEDY_SEARCH_NODE||n.NODE_BINARY||n.NODE;if(u?.trim())return u.trim();let g=hf(f||"").toLowerCase();if(g==="node"||g==="node.exe")return f;return"node"}var Wf=lf(rf(import.meta.url)),Qf=wf(Wf,"..","bin","cdp.mjs");function $(n,f=30000){return E(n,null,f)}function E(n,f=null,u=30000){return new Promise((g,i)=>{let o=of($f(),[Qf,...n],{stdio:[f==null?"ignore":"pipe","pipe","pipe"]});if(f!=null)o.stdin.write(f),o.stdin.end();let w="",l="";o.stdout.on("data",(h)=>w+=h),o.stderr.on("data",(h)=>l+=h);let r=setTimeout(()=>{o.kill(),i(Error(`cdp timeout: ${n[0]}`))},u);o.on("close",(h)=>{if(clearTimeout(r),h===0)g(w.trim());else i(Error(l.trim()||`cdp exit ${h}`))})})}async function bn(n){if(n)return n;let f=(await $(["list"])).split(`
3
- `)[0]?.slice(0,8);if(!f)throw Error("No Chrome tabs found. Is Chrome running with --remote-debugging-port=9222?");let u=await $(["evalraw",f,"Target.createTarget",'{"url":"about:blank"}']),{targetId:g}=JSON.parse(u);await $(["list"]);let i=g.slice(0,8);try{await Bn(i)}catch(o){process.stderr.write(`[getOrOpenTab] stealth injection failed: ${o.message}
4
- `)}return i}async function Bn(n){await $(["evalraw",n,"Page.addScriptToEvaluateOnNewDocument",JSON.stringify({source:`
2
+ import{randomInt as of}from"node:crypto";import{spawn as wf}from"node:child_process";import{dirname as hf,join as lf}from"node:path";import{fileURLToPath as $f}from"node:url";import{basename as rf}from"node:path";function Wf(n=process.env,f=process.execPath){let u=n.GREEDY_SEARCH_NODE||n.NODE_BINARY||n.NODE;if(u?.trim())return u.trim();let g=rf(f||"").toLowerCase();if(g==="node"||g==="node.exe")return f;return"node"}var Qf=hf($f(import.meta.url)),Xf=lf(Qf,"..","bin","cdp.mjs");function r(n,f=30000){return E(n,null,f)}function E(n,f=null,u=30000){return new Promise((g,i)=>{let o=wf(Wf(),[Xf,...n],{stdio:[f==null?"ignore":"pipe","pipe","pipe"]});if(f!=null)o.stdin.write(f),o.stdin.end();let h="",w="";o.stdout.on("data",($)=>h+=$),o.stderr.on("data",($)=>w+=$);let l=setTimeout(()=>{o.kill(),i(Error(`cdp timeout: ${n[0]}`))},u);o.on("close",($)=>{if(clearTimeout(l),$===0)g(h.trim());else i(Error(w.trim()||`cdp exit ${$}`))})})}async function Bn(n){if(n)return n;let f=(await r(["list"])).split(`
3
+ `)[0]?.slice(0,8);if(!f)throw Error("No Chrome tabs found. Is Chrome running with --remote-debugging-port=9222?");let u=await r(["evalraw",f,"Target.createTarget",'{"url":"about:blank"}']),{targetId:g}=JSON.parse(u);await r(["list"]);let i=g.slice(0,8);try{await qn(i)}catch(o){process.stderr.write(`[getOrOpenTab] stealth injection failed: ${o.message}
4
+ `)}return i}async function qn(n){await r(["evalraw",n,"Page.addScriptToEvaluateOnNewDocument",JSON.stringify({source:`
5
5
  (function() {
6
6
  // ── Runtime.enable / CDP detection masking ──────────────
7
7
  try { delete window.__REBROWSER_RUNTIME_ENABLE; } catch(_) {}
@@ -209,7 +209,7 @@ import{randomInt as gf}from"node:crypto";import{spawn as of}from"node:child_proc
209
209
  };
210
210
  } catch(_) {}
211
211
  })();
212
- `})])}var d={postNav:800,postNavSlow:1200,postClick:300,postType:300,inputPoll:400,copyPoll:600,afterVerify:1500};function D(n){let f=n*0.4,u=gf(-Math.floor(f),Math.floor(f)+1);return Math.max(50,Math.round(n+u))}async function z(n,f,u=15000,g=500){let i=String.raw`
212
+ `})])}var d={postNav:800,postNavSlow:1200,postClick:300,postType:300,inputPoll:400,copyPoll:600,afterVerify:1500};function z(n){let f=n*0.4,u=of(-Math.floor(f),Math.floor(f)+1);return Math.max(50,Math.round(n+u))}async function y(n,f,u=15000,g=500){let i=String.raw`
213
213
  new Promise((resolve) => {
214
214
  const _deadline = Date.now() + ${u};
215
215
  const _baseInterval = ${g};
@@ -228,27 +228,27 @@ import{randomInt as gf}from"node:crypto";import{spawn as of}from"node:child_proc
228
228
 
229
229
  _poll();
230
230
  })
231
- `;return await $(["eval",n,i],u+5000)==="true"}async function qn(n){let f=n.indexOf("--stdin");if(f===-1)return n;let u=await new Promise((i)=>{let o="";process.stdin.setEncoding("utf8"),process.stdin.on("data",(w)=>o+=w),process.stdin.on("end",()=>i(o.trim()))}),g=[...n];return g[f]=u,g}function Nn(n){let f=n.includes("--short"),u=n.filter((l)=>l!=="--short"),g=u.indexOf("--tab"),i=g===-1?null:u[g+1];if(g!==-1)u=u.filter((l,r)=>r!==g&&r!==g+1);let o=u.indexOf("--locale"),w=o===-1?null:u[o+1];if(o!==-1)u=u.filter((l,r)=>r!==o&&r!==o+1);return{query:u.join(" "),tabPrefix:i,short:f,locale:w}}function Vn(n,f){if(!n.length||n[0]==="--help")process.stderr.write(f),process.exit(1)}function _n(n,f,u=300){if(!f||n.length<=u)return n;let g=n.slice(0,u),i=g.lastIndexOf(" ");return i>0?`${g.slice(0,i)}…`:`${g}…`}function Hn(n){process.stdout.write(`${JSON.stringify(n,null,2)}
232
- `)}function Z(n,f,u=null){if(!n||typeof n!=="object")return;let g=u?` (+${Date.now()-u}ms)`:"";if(n.lastStage=f,!Array.isArray(n.stages))n.stages=[];n.stages.push({stage:f,at:Date.now()});let i=n.engine||"extractor";console.error(`[${i}] stage: ${f}${g}`)}function nn({engine:n,mode:f="headless",clipboardEmpty:u=null,fallbackUsed:g=null,blockedBy:i=null,verificationResult:o=null,inputReady:w=null,durationMs:l=null,lastStage:r=null,stages:h=null}={}){return{engine:n,mode:f,clipboardEmpty:u,fallbackUsed:g,blockedBy:i,verificationResult:o,inputReady:w,durationMs:l,lastStage:r,stages:h}}function Kn(n,f=null){if(f){let u=JSON.stringify({_envelope:f,error:n.message});process.stdout.write(`${u}
231
+ `;return await r(["eval",n,i],u+5000)==="true"}async function Nn(n){let f=n.indexOf("--stdin");if(f===-1)return n;let u=await new Promise((i)=>{let o="";process.stdin.setEncoding("utf8"),process.stdin.on("data",(h)=>o+=h),process.stdin.on("end",()=>i(o.trim()))}),g=[...n];return g[f]=u,g}function Vn(n){let f=n.includes("--short"),u=n.filter((w)=>w!=="--short"),g=u.indexOf("--tab"),i=g===-1?null:u[g+1];if(g!==-1)u=u.filter((w,l)=>l!==g&&l!==g+1);let o=u.indexOf("--locale"),h=o===-1?null:u[o+1];if(o!==-1)u=u.filter((w,l)=>l!==o&&l!==o+1);return{query:u.join(" "),tabPrefix:i,short:f,locale:h}}function _n(n,f){if(!n.length||n[0]==="--help")process.stderr.write(f),process.exit(1)}function Hn(n,f,u=300){if(!f||n.length<=u)return n;let g=n.slice(0,u),i=g.lastIndexOf(" ");return i>0?`${g.slice(0,i)}…`:`${g}…`}function Kn(n){process.stdout.write(`${JSON.stringify(n,null,2)}
232
+ `)}function j(n,f,u=null){if(!n||typeof n!=="object")return;let g=u?` (+${Date.now()-u}ms)`:"";if(n.lastStage=f,!Array.isArray(n.stages))n.stages=[];n.stages.push({stage:f,at:Date.now()});let i=n.engine||"extractor";console.error(`[${i}] stage: ${f}${g}`)}function nn({engine:n,mode:f="headless",clipboardEmpty:u=null,fallbackUsed:g=null,blockedBy:i=null,verificationResult:o=null,inputReady:h=null,durationMs:w=null,lastStage:l=null,stages:$=null}={}){return{engine:n,mode:f,clipboardEmpty:u,fallbackUsed:g,blockedBy:i,verificationResult:o,inputReady:h,durationMs:w,lastStage:l,stages:$}}function Un(n,f=null){if(f){let u=JSON.stringify({_envelope:f,error:n.message});process.stdout.write(`${u}
233
233
  `)}process.stderr.write(`Error: ${n.message}
234
- `),process.exit(1)}import{spawn as Rf,execFileSync as In,execSync as Wn}from"node:child_process";import{existsSync as S,readFileSync as s,renameSync as af,unlinkSync as q,writeFileSync as F}from"node:fs";import Qn from"node:http";import{platform as Xn,tmpdir as pf}from"node:os";import{join as Lf}from"node:path";import{basename as Xf}from"node:path";function Un(n=process.env,f=process.execPath){let u=n.GREEDY_SEARCH_NODE||n.NODE_BINARY||n.NODE;if(u?.trim())return u.trim();let g=Xf(f||"").toLowerCase();if(g==="node"||g==="node.exe")return f;return"node"}import{existsSync as xn}from"node:fs";import{platform as tf}from"node:os";import{join as H}from"node:path";function X(n){let f=tf()==="win32",u=process.env.SystemRoot||"C:\\Windows",g={win32:{powershell:H(u,"System32","WindowsPowerShell","v1.0","powershell.exe"),powershell_ise:H(u,"System32","WindowsPowerShell","v1.0","powershell_ise.exe"),netstat:H(u,"System32","netstat.exe"),taskkill:H(u,"System32","taskkill.exe"),tasklist:H(u,"System32","tasklist.exe"),cmd:H(u,"System32","cmd.exe")},unix:{ps:"/usr/bin/ps",lsof:"/usr/bin/lsof",ss:"/usr/sbin/ss",grep:"/usr/bin/grep",kill:"/usr/bin/kill"}},i=f?g.win32:g.unix,o=n.toLowerCase();if(i[o]&&xn(i[o]))return i[o];if(f&&o==="netstat"){let w=H(u,"Sysnative","netstat.exe");if(xn(w))return w}return n}import{existsSync as C,mkdirSync as Zf,readFileSync as kn,writeFileSync as jf}from"node:fs";import{homedir as Jf}from"node:os";import{join as Mn}from"node:path";import{tmpdir as R}from"node:os";var Q=9222,B=`${R().replaceAll("\\","/")}/greedysearch-chrome-profile`,k=`${B}/DevToolsActivePort`,bf=`${R().replaceAll("\\","/")}/cdp-pages.json`,K=`${R().replaceAll("\\","/")}/greedysearch-chrome-mode`,Mu=`${R().replaceAll("\\","/")}/greedysearch-visible-recovery.jsonl`,un=Mn(Jf(),".dm"),b=Mn(un,"greedyconfig"),gn=["perplexity","google","chatgpt"],on="gemini";function Bf(){try{if(C(b)){let n=kn(b,"utf8"),f=JSON.parse(n);if(Array.isArray(f.engines)&&f.engines.length>0&&f.engines.every((u)=>typeof u==="string")){let u=f.engines.filter((i)=>fn[i]),g=f.engines.filter((i)=>!fn[i]);if(g.length>0)process.stderr.write(`[greedysearch] Warning: ignoring unknown engine(s) in ${b}: ${g.join(", ")}
234
+ `),process.exit(1)}import{spawn as pf,execFileSync as Fn,execSync as Qn}from"node:child_process";import{existsSync as C,readFileSync as s,renameSync as Lf,unlinkSync as q,writeFileSync as e}from"node:fs";import Xn from"node:http";import{platform as Zn,tmpdir as af}from"node:os";import{join as rn}from"node:path";import{basename as Zf}from"node:path";function xn(n=process.env,f=process.execPath){let u=n.GREEDY_SEARCH_NODE||n.NODE_BINARY||n.NODE;if(u?.trim())return u.trim();let g=Zf(f||"").toLowerCase();if(g==="node"||g==="node.exe")return f;return"node"}import{existsSync as An}from"node:fs";import{platform as jf}from"node:os";import{join as H}from"node:path";function X(n){let f=jf()==="win32",u=process.env.SystemRoot||"C:\\Windows",g={win32:{powershell:H(u,"System32","WindowsPowerShell","v1.0","powershell.exe"),powershell_ise:H(u,"System32","WindowsPowerShell","v1.0","powershell_ise.exe"),netstat:H(u,"System32","netstat.exe"),taskkill:H(u,"System32","taskkill.exe"),tasklist:H(u,"System32","tasklist.exe"),cmd:H(u,"System32","cmd.exe")},unix:{ps:"/usr/bin/ps",lsof:"/usr/bin/lsof",ss:"/usr/sbin/ss",grep:"/usr/bin/grep",kill:"/usr/bin/kill"}},i=f?g.win32:g.unix,o=n.toLowerCase();if(i[o]&&An(i[o]))return i[o];if(f&&o==="netstat"){let h=H(u,"Sysnative","netstat.exe");if(An(h))return h}return n}import{existsSync as R,mkdirSync as Jf,readFileSync as Mn,writeFileSync as tf}from"node:fs";import{homedir as bf}from"node:os";import{join as On}from"node:path";import{tmpdir as p}from"node:os";var Q=9222,B=`${p().replaceAll("\\","/")}/greedysearch-chrome-profile`,k=`${B}/DevToolsActivePort`,Bf=`${p().replaceAll("\\","/")}/cdp-pages.json`,K=`${p().replaceAll("\\","/")}/greedysearch-chrome-mode`,Ou=`${p().replaceAll("\\","/")}/greedysearch-visible-recovery.jsonl`,un=On(bf(),".dm"),b=On(un,"greedyconfig"),gn=["perplexity","google","chatgpt"],on="gemini";function qf(){try{if(R(b)){let n=Mn(b,"utf8"),f=JSON.parse(n);if(Array.isArray(f.engines)&&f.engines.length>0&&f.engines.every((u)=>typeof u==="string")){let u=f.engines.filter((i)=>fn[i]),g=f.engines.filter((i)=>!fn[i]);if(g.length>0)process.stderr.write(`[greedysearch] Warning: ignoring unknown engine(s) in ${b}: ${g.join(", ")}
235
235
  [greedysearch] Available engines: ${Object.keys(fn).join(", ")}
236
236
  `);if(u.length>0)return u;process.stderr.write(`[greedysearch] Warning: no valid engines in ${b}, falling back to defaults: ${gn.join(", ")}
237
- `)}}}catch{}return gn}function qf(){try{if(!C(un))Zf(un,{recursive:!0});if(!C(b))jf(b,JSON.stringify({engines:gn,synthesizer:on},null,2)+`
238
- `,"utf8")}catch{}}qf();var An=["gemini","chatgpt"];function Nf(){try{if(C(b)){let n=kn(b,"utf8"),f=JSON.parse(n);if(typeof f.synthesizer==="string"){let u=f.synthesizer.toLowerCase();if(An.includes(u))return u;process.stderr.write(`[greedysearch] Warning: unknown synthesizer "${f.synthesizer}" in ${b}
239
- [greedysearch] Available synthesizers: ${An.join(", ")}
237
+ `)}}}catch{}return gn}function Nf(){try{if(!R(un))Jf(un,{recursive:!0});if(!R(b))tf(b,JSON.stringify({engines:gn,synthesizer:on},null,2)+`
238
+ `,"utf8")}catch{}}Nf();var kn=["gemini","chatgpt"];function Vf(){try{if(R(b)){let n=Mn(b,"utf8"),f=JSON.parse(n);if(typeof f.synthesizer==="string"){let u=f.synthesizer.toLowerCase();if(kn.includes(u))return u;process.stderr.write(`[greedysearch] Warning: unknown synthesizer "${f.synthesizer}" in ${b}
239
+ [greedysearch] Available synthesizers: ${kn.join(", ")}
240
240
  [greedysearch] Falling back to default: ${on}
241
- `)}}}catch{}return on}var fn={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"},Ou=Bf();var Du=Nf(),zu=Math.max(1,Number.parseInt(process.env.GREEDY_FETCH_CONCURRENCY||"5",10)||5);process.env.CDP_PROFILE_DIR=B;import{existsSync as vu,mkdirSync as Vf,readFileSync as Dn,renameSync as _f,unlinkSync as Hf,writeFileSync as zn}from"node:fs";import Kf from"node:http";import{platform as yn}from"node:os";import{dirname as Uf}from"node:path";var Yn=[9222,9223],xf=/^(0|false|no|off)$/i;function Af(n){return[...new Set(n.filter(Boolean))]}function On(n,f="explicit"){let u=new URL(n),g=u.protocol==="wss:"?"wss:":"ws:",i=u.port?Number.parseInt(u.port,10):g==="wss:"?443:80;return{source:f,wsUrl:`${g}//${u.host}${u.pathname}${u.search}`,host:u.hostname,port:i,path:`${u.pathname}${u.search}`,httpUrl:`http://${u.hostname}:${i}`}}function vn(n=process.env){return![n.GREEDY_SEARCH_REUSE_EXISTING_BROWSER,n.DM_CUA_REUSE_EXISTING_BROWSER].some((f)=>f!==void 0&&xf.test(String(f)))}function ln(n){let f=Number.parseInt(String(n??""),10);return Number.isInteger(f)&&f>0&&f<65536?f:null}function kf(n){if(!n)return null;let f=String(n).trim();if(!f)return null;let u=new URL(f.includes("://")?f:`http://${f}`),g=ln(u.port)||(u.protocol==="https:"?443:80);return{host:u.hostname,port:g,source:`env:${f}`}}function Mf(n){if(!n)return[];return String(n).split(/[,\s]+/).map(ln).filter(Boolean)}function Of({env:n=process.env,platformName:f=yn(),procVersionText:u=null}={}){if(f!=="linux")return!1;if(n.WSL_DISTRO_NAME||n.WSL_INTEROP)return!0;let g=u??(()=>{try{return Dn("/proc/version","utf8")}catch{return""}})();return/microsoft|wsl/i.test(g)}function Df({env:n=process.env,platformName:f=yn(),procVersionText:u=null,resolvConfText:g=null}={}){let i=["localhost","127.0.0.1"];if(Of({env:n,platformName:f,procVersionText:u})){let w=(g??(()=>{try{return Dn("/etc/resolv.conf","utf8")}catch{return""}})()).match(/^\s*nameserver\s+([^\s#]+)/m);if(w)i.push(w[1]);i.push("host.docker.internal")}return Af(i)}function zf({env:n=process.env,hosts:f=Df({env:n}),autoPorts:u=Yn,excludePorts:g=[]}={}){let i=[];for(let r of["GREEDY_SEARCH_CDP_WS_URL","DM_CUA_CDP_WS_URL","CDP_WS_URL"])if(n[r])i.push({wsUrl:n[r],source:`env:${r}`});for(let r of["GREEDY_SEARCH_CDP_URL","DM_CUA_CDP_URL","CDP_URL"])try{let h=kf(n[r]);if(h)i.push(h)}catch{}for(let r of["GREEDY_SEARCH_EXISTING_CDP_PORT","DM_CUA_EXISTING_CDP_PORT","GREEDY_SEARCH_CDP_PORT","DM_CUA_CDP_PORT"]){let h=ln(n[r]);if(!h)continue;for(let W of f)i.push({host:W,port:h,source:`env:${r}`})}let o=Mf(n.GREEDY_SEARCH_EXISTING_CDP_PORTS);for(let r of o)for(let h of f)i.push({host:h,port:r,source:"env:GREEDY_SEARCH_EXISTING_CDP_PORTS"});let w=new Set(g.filter(Boolean));if(vn(n))for(let r of u){if(w.has(r))continue;for(let h of f)i.push({host:h,port:r,source:"auto"})}let l=new Set;return i.filter((r)=>{let h=r.wsUrl||`${r.host}:${r.port}`;if(l.has(h))return!1;return l.add(h),!0})}async function yf(n,{timeoutMs:f=700}={}){if(n.wsUrl)return On(n.wsUrl,n.source);let u=n.host||"localhost",g=n.port;if(!g)return null;let i=await new Promise((o)=>{let w=Kf.get({host:u,port:g,path:"/json/version",timeout:f},(l)=>{let r="";l.setEncoding("utf8"),l.on("data",(h)=>r+=h),l.on("end",()=>o(l.statusCode===200?r:null))});w.on("error",()=>o(null)),w.setTimeout(f,()=>{w.destroy(),o(null)})});if(!i)return null;try{let o=JSON.parse(i);if(!o.webSocketDebuggerUrl)return null;let w=On(o.webSocketDebuggerUrl,n.source),l=w.path||new URL(o.webSocketDebuggerUrl).pathname;return{...w,host:u,port:g,httpUrl:`http://${u}:${g}`,wsUrl:`ws://${u}:${g}${l}`,path:l}}catch{return null}}async function Yf(n={}){for(let f of zf(n)){let u=await yf(f,n);if(u)return u}return null}function vf(n){if(!n)throw Error("missing CDP endpoint");let f=new URL(n.wsUrl);return`${f.host}
241
+ `)}}}catch{}return on}var fn={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"},zu=qf();var yu=Vf(),Du=Math.max(1,Number.parseInt(process.env.GREEDY_FETCH_CONCURRENCY||"5",10)||5);process.env.CDP_PROFILE_DIR=B;import{existsSync as Gu,mkdirSync as _f,readFileSync as yn,renameSync as Hf,unlinkSync as Kf,writeFileSync as Dn}from"node:fs";import Uf from"node:http";import{platform as Yn}from"node:os";import{dirname as xf}from"node:path";var vn=[9222,9223],Af=/^(0|false|no|off)$/i;function kf(n){return[...new Set(n.filter(Boolean))]}function zn(n,f="explicit"){let u=new URL(n),g=u.protocol==="wss:"?"wss:":"ws:",i=u.port?Number.parseInt(u.port,10):g==="wss:"?443:80;return{source:f,wsUrl:`${g}//${u.host}${u.pathname}${u.search}`,host:u.hostname,port:i,path:`${u.pathname}${u.search}`,httpUrl:`http://${u.hostname}:${i}`}}function Gn(n=process.env){return![n.GREEDY_SEARCH_REUSE_EXISTING_BROWSER,n.DM_CUA_REUSE_EXISTING_BROWSER].some((f)=>f!==void 0&&Af.test(String(f)))}function wn(n){let f=Number.parseInt(String(n??""),10);return Number.isInteger(f)&&f>0&&f<65536?f:null}function Mf(n){if(!n)return null;let f=String(n).trim();if(!f)return null;let u=new URL(f.includes("://")?f:`http://${f}`),g=wn(u.port)||(u.protocol==="https:"?443:80);return{host:u.hostname,port:g,source:`env:${f}`}}function Of(n){if(!n)return[];return String(n).split(/[,\s]+/).map(wn).filter(Boolean)}function zf({env:n=process.env,platformName:f=Yn(),procVersionText:u=null}={}){if(f!=="linux")return!1;if(n.WSL_DISTRO_NAME||n.WSL_INTEROP)return!0;let g=u??(()=>{try{return yn("/proc/version","utf8")}catch{return""}})();return/microsoft|wsl/i.test(g)}function yf({env:n=process.env,platformName:f=Yn(),procVersionText:u=null,resolvConfText:g=null}={}){let i=["localhost","127.0.0.1"];if(zf({env:n,platformName:f,procVersionText:u})){let h=(g??(()=>{try{return yn("/etc/resolv.conf","utf8")}catch{return""}})()).match(/^\s*nameserver\s+([^\s#]+)/m);if(h)i.push(h[1]);i.push("host.docker.internal")}return kf(i)}function Df({env:n=process.env,hosts:f=yf({env:n}),autoPorts:u=vn,excludePorts:g=[]}={}){let i=[];for(let l of["GREEDY_SEARCH_CDP_WS_URL","DM_CUA_CDP_WS_URL","CDP_WS_URL"])if(n[l])i.push({wsUrl:n[l],source:`env:${l}`});for(let l of["GREEDY_SEARCH_CDP_URL","DM_CUA_CDP_URL","CDP_URL"])try{let $=Mf(n[l]);if($)i.push($)}catch{}for(let l of["GREEDY_SEARCH_EXISTING_CDP_PORT","DM_CUA_EXISTING_CDP_PORT","GREEDY_SEARCH_CDP_PORT","DM_CUA_CDP_PORT"]){let $=wn(n[l]);if(!$)continue;for(let W of f)i.push({host:W,port:$,source:`env:${l}`})}let o=Of(n.GREEDY_SEARCH_EXISTING_CDP_PORTS);for(let l of o)for(let $ of f)i.push({host:$,port:l,source:"env:GREEDY_SEARCH_EXISTING_CDP_PORTS"});let h=new Set(g.filter(Boolean));if(Gn(n))for(let l of u){if(h.has(l))continue;for(let $ of f)i.push({host:$,port:l,source:"auto"})}let w=new Set;return i.filter((l)=>{let $=l.wsUrl||`${l.host}:${l.port}`;if(w.has($))return!1;return w.add($),!0})}async function Yf(n,{timeoutMs:f=700}={}){if(n.wsUrl)return zn(n.wsUrl,n.source);let u=n.host||"localhost",g=n.port;if(!g)return null;let i=await new Promise((o)=>{let h=Uf.get({host:u,port:g,path:"/json/version",timeout:f},(w)=>{let l="";w.setEncoding("utf8"),w.on("data",($)=>l+=$),w.on("end",()=>o(w.statusCode===200?l:null))});h.on("error",()=>o(null)),h.setTimeout(f,()=>{h.destroy(),o(null)})});if(!i)return null;try{let o=JSON.parse(i);if(!o.webSocketDebuggerUrl)return null;let h=zn(o.webSocketDebuggerUrl,n.source),w=h.path||new URL(o.webSocketDebuggerUrl).pathname;return{...h,host:u,port:g,httpUrl:`http://${u}:${g}`,wsUrl:`ws://${u}:${g}${w}`,path:w}}catch{return null}}async function vf(n={}){for(let f of Df(n)){let u=await Yf(f,n);if(u)return u}return null}function Gf(n){if(!n)throw Error("missing CDP endpoint");let f=new URL(n.wsUrl);return`${f.host}
242
242
  ${f.pathname}${f.search}
243
- `}function Gf(n,f){Vf(Uf(n),{recursive:!0});let u=`${n}.tmp-${process.pid}`;zn(u,vf(f),"utf8");try{Hf(n)}catch{}_f(u,n)}async function Gn({activePortFile:n,modeFile:f,env:u=process.env,excludePorts:g=[],autoPorts:i=Yn}={}){if(!vn(u))return null;let o=await Yf({env:u,excludePorts:g,autoPorts:i});if(!o)return null;if(Gf(n,o),f)zn(f,"existing","utf8");return o}import{execFileSync as Tn,execSync as wn}from"node:child_process";import{existsSync as a,mkdirSync as Tf,readFileSync as M,unlinkSync as x,writeFileSync as A}from"node:fs";import{platform as rn,tmpdir as Rn}from"node:os";var v=Rn().replaceAll("\\","/"),p=`${v}/greedysearch-chrome-metadata.json`,U=`${v}/greedysearch-chrome-launch.lock`,Pf=15000,L=`${v}/greedysearch-chrome.pid`,I=`${v}/greedysearch-chrome-mode`,y=`${v}/greedysearch-chrome-last-activity`;function Y(n){if(!Number.isInteger(n)||n<=0)return!1;try{return process.kill(n,0),!0}catch{return!1}}function cf(n){if(!Y(n))return null;try{if(rn()==="win32")return Tn(X("powershell"),["-NoProfile","-NonInteractive","-Command",`(Get-CimInstance Win32_Process -Filter "ProcessId = ${n}").CommandLine`],{encoding:"utf8",windowsHide:!0,timeout:5000}).trim()||null;return Tn(X("ps"),["-p",String(n),"-o","command="],{encoding:"utf8",timeout:5000}).trim()||null}catch{return null}}function mf(n,f,u=Q){if(!n)return!1;let g=(w)=>String(w||"").replaceAll("\\","/").toLowerCase(),i=g(n),o=g(f);return i.includes(o)&&i.includes(`--remote-debugging-port=${u}`)&&!i.includes("--type=")}function Pn(n,f,u=Q){return mf(cf(n),f,u)}function Cf(n=Q){try{if(rn()==="win32"){let u=wn(`${X("netstat")} -ano -p TCP 2>nul`,{encoding:"utf8"}),g=new RegExp(String.raw`TCP\s+\S+:${n}\s+\S+:0\s+LISTENING\s+(\d+)`,"i"),i=u.match(g);return i?Number.parseInt(i[1],10):null}let f=wn(`${X("lsof")} -i :${n} -t 2>/dev/null || ${X("ss")} -tlnp 2>/dev/null | ${X("grep")} :${n} | ${X("grep")} -oP 'pid=\\K\\d+'`,{encoding:"utf8"}).trim();return f?Number.parseInt(f.split(`
244
- `)[0],10):null}catch{return null}}function cn(n){try{if(rn()==="win32")wn(`${X("taskkill")} /F /T /PID ${n}`,{stdio:"ignore"});else{try{process.kill(-n,"SIGKILL")}catch{}try{process.kill(n,"SIGKILL")}catch{}}return!0}catch{return!1}}function G(){try{if(a(p)){let n=M(p,"utf8"),f=JSON.parse(n);if(f&&typeof f.tempDir==="string"&&typeof f.debugPort==="number")return{browserPid:Number.isInteger(f.browserPid)?f.browserPid:void 0,debugPort:f.debugPort,tempDir:f.tempDir,clientPids:Array.isArray(f.clientPids)?f.clientPids.filter((u)=>Number.isInteger(u)&&u>0):[],sessionMode:f.sessionMode==="visible"?"visible":"headless",lastActivity:Number.isFinite(f.lastActivity)?f.lastActivity:0,launchedAt:Number.isFinite(f.launchedAt)?f.launchedAt:0}}}catch{}try{let n=a(L)?Number.parseInt(M(L,"utf8").trim(),10)||void 0:void 0,f=a(I)?M(I,"utf8").trim()==="visible"?"visible":"headless":"headless",u=a(y)?Number.parseInt(M(y,"utf8").trim(),10)||0:0;return{browserPid:n,debugPort:Q,tempDir:B,clientPids:n?[n]:[],sessionMode:f,lastActivity:u,launchedAt:0}}catch{return null}}function e(n){try{A(p,JSON.stringify({browserPid:n.browserPid,debugPort:n.debugPort,tempDir:n.tempDir,clientPids:[...new Set(n.clientPids.filter((f)=>f>0))],sessionMode:n.sessionMode,lastActivity:n.lastActivity,launchedAt:n.launchedAt},null,2),"utf8")}catch{}try{if(n.browserPid)A(L,String(n.browserPid),"utf8")}catch{}try{A(I,n.sessionMode,"utf8")}catch{}try{A(y,String(n.lastActivity),"utf8")}catch{}}function mn(){try{x(p)}catch{}try{x(L)}catch{}try{x(I)}catch{}try{x(y)}catch{}}function an(n){if(!n)return n;let f={...n,clientPids:[...new Set([...n.clientPids,process.pid].filter((u)=>Y(u)||u===process.pid))]};return e(f),f}function hn(n){let f=Date.now();try{if(n)e({...n,lastActivity:f});else A(y,String(f),"utf8")}catch{}}var Lu=Number.parseInt(process.env.GREEDY_SEARCH_IDLE_TIMEOUT_MINUTES||"5",10)||5;var Cn=!1;function pn(){if(Cn)return;Cn=!0;let n=G();if(!n)return;if(n.browserPid){if(!Y(n.browserPid)){cn(n.browserPid),mn();return}if(!Pn(n.browserPid,n.tempDir,n.debugPort))mn();else e({...n,clientPids:n.clientPids.filter((i)=>Y(i))})}let f=Cf();if(f&&f!==n.browserPid)if(Pn(f,B,Q))e({browserPid:f,debugPort:Q,tempDir:B,clientPids:[f],sessionMode:n.sessionMode,lastActivity:Date.now(),launchedAt:Date.now()});else cn(f)}function Ln(){Tf(Rn(),{recursive:!0});try{let n=JSON.stringify({pid:process.pid,ts:Date.now()});return A(U,n,{encoding:"utf8",flag:"wx"}),{acquired:!0,release:()=>{try{let f=M(U,"utf8");if(JSON.parse(f).pid===process.pid)x(U)}catch{}}}}catch(n){if(n?.code!=="EEXIST")return{acquired:!1,release:()=>{}}}try{let n=M(U,"utf8"),f=JSON.parse(n),u=Date.now()-(f.ts||0);if(!Y(f.pid)||u>Pf){try{x(U)}catch{}try{let i=JSON.stringify({pid:process.pid,ts:Date.now()});return A(U,i,{encoding:"utf8",flag:"wx"}),{acquired:!0,release:()=>{try{x(U)}catch{}}}}catch{return{acquired:!1,release:()=>{}}}}}catch{}return{acquired:!1,release:()=>{}}}var If=import.meta.dirname||new URL(".",import.meta.url).pathname.replace(/^\/([A-Z]:)/,"$1"),Fn=pf().replaceAll("\\","/"),T=`${Fn}/greedysearch-chrome.pid`,P=`${Fn}/greedysearch-chrome-last-activity`,ef=Number.parseInt(process.env.GREEDY_SEARCH_IDLE_TIMEOUT_MINUTES||"5",10)||5,Ff=Number.parseInt(process.env.GREEDY_SEARCH_VISIBLE_IDLE_TIMEOUT_MINUTES||"60",10)||60;function Sf(n,f=Q){let u=String(n||"").toLowerCase();if(!u.includes(`--remote-debugging-port=${f}`)||u.includes("--type="))return null;return u.includes("--headless")}function Sn(){try{let n=tn(),f=n?Ef(n):null,u=Sf(f);if(u!==null){try{F(K,u?"headless":"visible","utf8")}catch{}return u}}catch{}try{if(!S(K))return!0;return s(K,"utf8").trim()==="headless"}catch{return!0}}function sf(){try{F(P,String(Date.now()),"utf8")}catch{}try{let n=G();if(n)hn(n)}catch{}}function Ef(n){try{if(Xn()==="win32")return In(X("powershell"),["-NoProfile","-NonInteractive","-Command",`(Get-CimInstance Win32_Process -Filter "ProcessId = ${n}").CommandLine`],{encoding:"utf8",windowsHide:!0,timeout:5000}).trim()||null;return In(X("ps"),["-p",String(n),"-o","command="],{encoding:"utf8",timeout:5000}).trim()||null}catch{return null}}function tn(){try{if(Xn()==="win32"){let f=Wn(`${X("netstat")} -ano -p TCP 2>nul`,{encoding:"utf8"}),u=new RegExp(String.raw`TCP\s+\S+:${Q}\s+\S+:0\s+LISTENING\s+(\d+)`,"i"),g=f.match(u);return g?Number.parseInt(g[1],10):null}let n=Wn(`${X("lsof")} -i :${Q} -t 2>/dev/null || ${X("ss")} -tlnp 2>/dev/null | ${X("grep")} :${Q} | ${X("grep")} -oP 'pid=\\K\\d+'`,{encoding:"utf8"}).trim();return n?Number.parseInt(n.split(`
245
- `)[0],10):null}catch{return null}}async function df(n=1500){try{let u=await new Promise((i,o)=>{let w=Qn.get(`http://localhost:${Q}/json/version`,(l)=>{let r="";l.on("data",(h)=>r+=h),l.on("end",()=>{try{i(JSON.parse(r))}catch{o(Error("bad JSON"))}})});w.on("error",o),w.setTimeout(1000,()=>{w.destroy(),o(Error("timeout"))})}),g=new globalThis.WebSocket(u.webSocketDebuggerUrl);await new Promise((i)=>{g.onopen=()=>{g.send(JSON.stringify({id:1,method:"Browser.close"})),setTimeout(()=>{g.close(),i()},200)},g.onerror=()=>i(),setTimeout(i,1000)})}catch{}let f=Date.now()+n;while(Date.now()<f){if(!tn())return!0;await new Promise((g)=>setTimeout(g,150))}return nu()}function nu(){try{let n=tn();if(!n&&S(T))n=Number.parseInt(s(T,"utf8").trim(),10)||null;if(!n)return!1;if(Xn()==="win32")Wn(`${X("taskkill")} /F /PID ${n}`,{stdio:"ignore"});else process.kill(n,"SIGKILL");return!0}catch{return!1}}async function sn(){if(!await O(500)){try{q(T)}catch{}try{q(P)}catch{}try{q(K)}catch{}return!1}let f=await df(1500);try{q(T)}catch{}try{q(P)}catch{}try{q(K)}catch{}if(f)process.stderr.write(`[greedysearch] Killed Chrome on port ${Q}.
246
- `);return f}var en=sn;async function fu(){let f=Sn()?ef:Ff;if(f<=0)return!1;if(!S(P))return sf(),!1;try{let u=Number.parseInt(s(P,"utf8").trim(),10);if(!u)return!1;if((Date.now()-u)/60000>=f)return sn()}catch{}return!1}function O(n=3000){return new Promise((f)=>{let u=Qn.get(`http://localhost:${Q}/json/version`,(g)=>{g.resume(),f(g.statusCode===200)});u.on("error",()=>f(!1)),u.setTimeout(n,()=>{u.destroy(),f(!1)})})}async function $n(){let n=`${k}.lock`,f=`${k}.tmp`,u=5000,g=1000,i=await new Promise((o)=>{let w=Date.now(),l=()=>{try{let r=JSON.stringify({pid:process.pid,ts:Date.now()});F(n,r,{encoding:"utf8",flag:"wx"}),o(!0)}catch(r){if(r?.code!=="EEXIST"){if(Date.now()-w<1000)setTimeout(l,50);else o(!1);return}try{let h=s(n,"utf8").trim(),W=h.startsWith("{")?JSON.parse(h):{ts:Number(h)},j=Number(W?.ts)||0;if(j>0&&Date.now()-j>5000)try{q(n)}catch{}if(Date.now()-w<1000)setTimeout(l,50);else o(!1)}catch{if(Date.now()-w<1000)setTimeout(l,50);else o(!1)}}};l()});try{let o=await new Promise((h,W)=>{let j=Qn.get(`http://localhost:${Q}/json/version`,(V)=>{let _="";V.on("data",(N)=>_+=N),V.on("end",()=>h(_))});j.on("error",W),j.setTimeout(3000,()=>{j.destroy(),W(Error("timeout"))})}),{webSocketDebuggerUrl:w}=JSON.parse(o),l=new URL(w),r=`${l.pathname}${l.search}`;if(i){F(f,`localhost:${Q}
247
- ${r}`,"utf8");try{q(k)}catch{}af(f,k)}}catch{}finally{if(i)try{q(n)}catch{}}}async function En(){let n=S(T)||!!G(),f=await Gn({activePortFile:k,modeFile:K,excludePorts:n?[Q]:[]});if(f){process.stderr.write(`[greedysearch] Reusing existing browser CDP endpoint ${f.httpUrl} (${f.source}).
248
- `);return}pn();let u=await fu(),g=u?!1:await O();if(!g&&!u)await new Promise((l)=>setTimeout(l,500)),g=await O();let i=!1;if(g){let l=Sn(),r=process.env.GREEDY_SEARCH_VISIBLE==="1";if(!r&&!l)process.stderr.write(`[greedysearch] Visible Chrome detected — switching to headless mode...
249
- `),await en(),await new Promise((h)=>setTimeout(h,1000)),i=!0;else if(r&&l)process.stderr.write(`[greedysearch] Headless Chrome detected — switching to visible mode...
250
- `),await en(),await new Promise((h)=>setTimeout(h,1000)),i=!0}if(i?!1:await O()){await $n();try{let l=G();if(l)hn(l),an(l)}catch{}return}let w=Ln();if(!w.acquired){if(await new Promise((r)=>setTimeout(r,3000)),await O(5000)){await $n();return}}try{if(await O(1000)){await $n();return}process.stderr.write(`GreedySearch Chrome not running on port ${Q} — auto-launching...
251
- `);let r=[Lf(If,"..","..","bin","launch.mjs")];if(process.env.GREEDY_SEARCH_VISIBLE!=="1")r.push("--headless");await new Promise((h,W)=>{Rf(Un(),r,{stdio:["ignore",process.stderr,process.stderr],env:{...process.env,CDP_PROFILE_DIR:B,GREEDY_SEARCH_PROFILE_DIR:B}}).on("close",(V)=>V===0?h():W(Error("launch.mjs failed")))})}finally{w.release()}}var uu=`
243
+ `}function Tf(n,f){_f(xf(n),{recursive:!0});let u=`${n}.tmp-${process.pid}`;Dn(u,Gf(f),"utf8");try{Kf(n)}catch{}Hf(u,n)}async function Tn({activePortFile:n,modeFile:f,env:u=process.env,excludePorts:g=[],autoPorts:i=vn}={}){if(!Gn(u))return null;let o=await vf({env:u,excludePorts:g,autoPorts:i});if(!o)return null;if(Tf(n,o),f)Dn(f,"existing","utf8");return o}import{execFileSync as Pn,execSync as hn}from"node:child_process";import{existsSync as L,mkdirSync as Pf,readFileSync as M,unlinkSync as x,writeFileSync as A}from"node:fs";import{platform as ln,tmpdir as pn}from"node:os";var v=pn().replaceAll("\\","/"),a=`${v}/greedysearch-chrome-metadata.json`,U=`${v}/greedysearch-chrome-launch.lock`,Cf=15000,I=`${v}/greedysearch-chrome.pid`,F=`${v}/greedysearch-chrome-mode`,D=`${v}/greedysearch-chrome-last-activity`;function Y(n){if(!Number.isInteger(n)||n<=0)return!1;try{return process.kill(n,0),!0}catch{return!1}}function cf(n){if(!Y(n))return null;try{if(ln()==="win32")return Pn(X("powershell"),["-NoProfile","-NonInteractive","-Command",`(Get-CimInstance Win32_Process -Filter "ProcessId = ${n}").CommandLine`],{encoding:"utf8",windowsHide:!0,timeout:5000}).trim()||null;return Pn(X("ps"),["-p",String(n),"-o","command="],{encoding:"utf8",timeout:5000}).trim()||null}catch{return null}}function mf(n,f,u=Q){if(!n)return!1;let g=(h)=>String(h||"").replaceAll("\\","/").toLowerCase(),i=g(n),o=g(f);return i.includes(o)&&i.includes(`--remote-debugging-port=${u}`)&&!i.includes("--type=")}function Cn(n,f,u=Q){return mf(cf(n),f,u)}function Rf(n=Q){try{if(ln()==="win32"){let u=hn(`${X("netstat")} -ano -p TCP 2>nul`,{encoding:"utf8"}),g=new RegExp(String.raw`TCP\s+\S+:${n}\s+\S+:0\s+LISTENING\s+(\d+)`,"i"),i=u.match(g);return i?Number.parseInt(i[1],10):null}let f=hn(`${X("lsof")} -i :${n} -t 2>/dev/null || ${X("ss")} -tlnp 2>/dev/null | ${X("grep")} :${n} | ${X("grep")} -oP 'pid=\\K\\d+'`,{encoding:"utf8"}).trim();return f?Number.parseInt(f.split(`
244
+ `)[0],10):null}catch{return null}}function cn(n){try{if(ln()==="win32")hn(`${X("taskkill")} /F /T /PID ${n}`,{stdio:"ignore"});else{try{process.kill(-n,"SIGKILL")}catch{}try{process.kill(n,"SIGKILL")}catch{}}return!0}catch{return!1}}function G(){try{if(L(a)){let n=M(a,"utf8"),f=JSON.parse(n);if(f&&typeof f.tempDir==="string"&&typeof f.debugPort==="number")return{browserPid:Number.isInteger(f.browserPid)?f.browserPid:void 0,debugPort:f.debugPort,tempDir:f.tempDir,clientPids:Array.isArray(f.clientPids)?f.clientPids.filter((u)=>Number.isInteger(u)&&u>0):[],sessionMode:f.sessionMode==="visible"?"visible":"headless",lastActivity:Number.isFinite(f.lastActivity)?f.lastActivity:0,launchedAt:Number.isFinite(f.launchedAt)?f.launchedAt:0}}}catch{}try{let n=L(I)?Number.parseInt(M(I,"utf8").trim(),10)||void 0:void 0,f=L(F)?M(F,"utf8").trim()==="visible"?"visible":"headless":"headless",u=L(D)?Number.parseInt(M(D,"utf8").trim(),10)||0:0;return{browserPid:n,debugPort:Q,tempDir:B,clientPids:n?[n]:[],sessionMode:f,lastActivity:u,launchedAt:0}}catch{return null}}function S(n){try{A(a,JSON.stringify({browserPid:n.browserPid,debugPort:n.debugPort,tempDir:n.tempDir,clientPids:[...new Set(n.clientPids.filter((f)=>f>0))],sessionMode:n.sessionMode,lastActivity:n.lastActivity,launchedAt:n.launchedAt},null,2),"utf8")}catch{}try{if(n.browserPid)A(I,String(n.browserPid),"utf8")}catch{}try{A(F,n.sessionMode,"utf8")}catch{}try{A(D,String(n.lastActivity),"utf8")}catch{}}function mn(){try{x(a)}catch{}try{x(I)}catch{}try{x(F)}catch{}try{x(D)}catch{}}function Ln(n){if(!n)return n;let f={...n,clientPids:[...new Set([...n.clientPids,process.pid].filter((u)=>Y(u)||u===process.pid))]};return S(f),f}function $n(n){let f=Date.now();try{if(n)S({...n,lastActivity:f});else A(D,String(f),"utf8")}catch{}}var Iu=Number.parseInt(process.env.GREEDY_SEARCH_IDLE_TIMEOUT_MINUTES||"5",10)||5;var Rn=!1;function an(){if(Rn)return;Rn=!0;let n=G();if(!n)return;if(n.browserPid){if(!Y(n.browserPid)){cn(n.browserPid),mn();return}if(!Cn(n.browserPid,n.tempDir,n.debugPort))mn();else S({...n,clientPids:n.clientPids.filter((i)=>Y(i))})}let f=Rf();if(f&&f!==n.browserPid)if(Cn(f,B,Q))S({browserPid:f,debugPort:Q,tempDir:B,clientPids:[f],sessionMode:n.sessionMode,lastActivity:Date.now(),launchedAt:Date.now()});else cn(f)}function In(){Pf(pn(),{recursive:!0});try{let n=JSON.stringify({pid:process.pid,ts:Date.now()});return A(U,n,{encoding:"utf8",flag:"wx"}),{acquired:!0,release:()=>{try{let f=M(U,"utf8");if(JSON.parse(f).pid===process.pid)x(U)}catch{}}}}catch(n){if(n?.code!=="EEXIST")return{acquired:!1,release:()=>{}}}try{let n=M(U,"utf8"),f=JSON.parse(n),u=Date.now()-(f.ts||0);if(!Y(f.pid)||u>Cf){try{x(U)}catch{}try{let i=JSON.stringify({pid:process.pid,ts:Date.now()});return A(U,i,{encoding:"utf8",flag:"wx"}),{acquired:!0,release:()=>{try{x(U)}catch{}}}}catch{return{acquired:!1,release:()=>{}}}}}catch{}return{acquired:!1,release:()=>{}}}var If=import.meta.dirname||new URL(".",import.meta.url).pathname.replace(/^\/([A-Z]:)/,"$1");function Ff(n=If){let f=[rn(n,"launch.mjs"),rn(n,"..","bin","launch.mjs"),rn(n,"..","..","bin","launch.mjs")];for(let u of f)if(C(u))return u;return f[f.length-1]}var en=af().replaceAll("\\","/"),T=`${en}/greedysearch-chrome.pid`,P=`${en}/greedysearch-chrome-last-activity`,Sf=Number.parseInt(process.env.GREEDY_SEARCH_IDLE_TIMEOUT_MINUTES||"5",10)||5,ef=Number.parseInt(process.env.GREEDY_SEARCH_VISIBLE_IDLE_TIMEOUT_MINUTES||"60",10)||60;function sf(n,f=Q){let u=String(n||"").toLowerCase();if(!u.includes(`--remote-debugging-port=${f}`)||u.includes("--type="))return null;return u.includes("--headless")}function sn(){try{let n=jn(),f=n?df(n):null,u=sf(f);if(u!==null){try{e(K,u?"headless":"visible","utf8")}catch{}return u}}catch{}try{if(!C(K))return!0;return s(K,"utf8").trim()==="headless"}catch{return!0}}function Ef(){try{e(P,String(Date.now()),"utf8")}catch{}try{let n=G();if(n)$n(n)}catch{}}function df(n){try{if(Zn()==="win32")return Fn(X("powershell"),["-NoProfile","-NonInteractive","-Command",`(Get-CimInstance Win32_Process -Filter "ProcessId = ${n}").CommandLine`],{encoding:"utf8",windowsHide:!0,timeout:5000}).trim()||null;return Fn(X("ps"),["-p",String(n),"-o","command="],{encoding:"utf8",timeout:5000}).trim()||null}catch{return null}}function jn(){try{if(Zn()==="win32"){let f=Qn(`${X("netstat")} -ano -p TCP 2>nul`,{encoding:"utf8"}),u=new RegExp(String.raw`TCP\s+\S+:${Q}\s+\S+:0\s+LISTENING\s+(\d+)`,"i"),g=f.match(u);return g?Number.parseInt(g[1],10):null}let n=Qn(`${X("lsof")} -i :${Q} -t 2>/dev/null || ${X("ss")} -tlnp 2>/dev/null | ${X("grep")} :${Q} | ${X("grep")} -oP 'pid=\\K\\d+'`,{encoding:"utf8"}).trim();return n?Number.parseInt(n.split(`
245
+ `)[0],10):null}catch{return null}}async function nu(n=1500){try{let u=await new Promise((i,o)=>{let h=Xn.get(`http://localhost:${Q}/json/version`,(w)=>{let l="";w.on("data",($)=>l+=$),w.on("end",()=>{try{i(JSON.parse(l))}catch{o(Error("bad JSON"))}})});h.on("error",o),h.setTimeout(1000,()=>{h.destroy(),o(Error("timeout"))})}),g=new globalThis.WebSocket(u.webSocketDebuggerUrl);await new Promise((i)=>{g.onopen=()=>{g.send(JSON.stringify({id:1,method:"Browser.close"})),setTimeout(()=>{g.close(),i()},200)},g.onerror=()=>i(),setTimeout(i,1000)})}catch{}let f=Date.now()+n;while(Date.now()<f){if(!jn())return!0;await new Promise((g)=>setTimeout(g,150))}return fu()}function fu(){try{let n=jn();if(!n&&C(T))n=Number.parseInt(s(T,"utf8").trim(),10)||null;if(!n)return!1;if(Zn()==="win32")Qn(`${X("taskkill")} /F /PID ${n}`,{stdio:"ignore"});else process.kill(n,"SIGKILL");return!0}catch{return!1}}async function En(){if(!await O(500)){try{q(T)}catch{}try{q(P)}catch{}try{q(K)}catch{}return!1}let f=await nu(1500);try{q(T)}catch{}try{q(P)}catch{}try{q(K)}catch{}if(f)process.stderr.write(`[greedysearch] Killed Chrome on port ${Q}.
246
+ `);return f}var Sn=En;async function uu(){let f=sn()?Sf:ef;if(f<=0)return!1;if(!C(P))return Ef(),!1;try{let u=Number.parseInt(s(P,"utf8").trim(),10);if(!u)return!1;if((Date.now()-u)/60000>=f)return En()}catch{}return!1}function O(n=3000){return new Promise((f)=>{let u=Xn.get(`http://localhost:${Q}/json/version`,(g)=>{g.resume(),f(g.statusCode===200)});u.on("error",()=>f(!1)),u.setTimeout(n,()=>{u.destroy(),f(!1)})})}async function Wn(){let n=`${k}.lock`,f=`${k}.tmp`,u=5000,g=1000,i=await new Promise((o)=>{let h=Date.now(),w=()=>{try{let l=JSON.stringify({pid:process.pid,ts:Date.now()});e(n,l,{encoding:"utf8",flag:"wx"}),o(!0)}catch(l){if(l?.code!=="EEXIST"){if(Date.now()-h<1000)setTimeout(w,50);else o(!1);return}try{let $=s(n,"utf8").trim(),W=$.startsWith("{")?JSON.parse($):{ts:Number($)},J=Number(W?.ts)||0;if(J>0&&Date.now()-J>5000)try{q(n)}catch{}if(Date.now()-h<1000)setTimeout(w,50);else o(!1)}catch{if(Date.now()-h<1000)setTimeout(w,50);else o(!1)}}};w()});try{let o=await new Promise(($,W)=>{let J=Xn.get(`http://localhost:${Q}/json/version`,(V)=>{let _="";V.on("data",(N)=>_+=N),V.on("end",()=>$(_))});J.on("error",W),J.setTimeout(3000,()=>{J.destroy(),W(Error("timeout"))})}),{webSocketDebuggerUrl:h}=JSON.parse(o),w=new URL(h),l=`${w.pathname}${w.search}`;if(i){e(f,`localhost:${Q}
247
+ ${l}`,"utf8");try{q(k)}catch{}Lf(f,k)}}catch{}finally{if(i)try{q(n)}catch{}}}async function dn(){let n=C(T)||!!G(),f=await Tn({activePortFile:k,modeFile:K,excludePorts:n?[Q]:[]});if(f){process.stderr.write(`[greedysearch] Reusing existing browser CDP endpoint ${f.httpUrl} (${f.source}).
248
+ `);return}an();let u=await uu(),g=u?!1:await O();if(!g&&!u)await new Promise((w)=>setTimeout(w,500)),g=await O();let i=!1;if(g){let w=sn(),l=process.env.GREEDY_SEARCH_VISIBLE==="1";if(!l&&!w)process.stderr.write(`[greedysearch] Visible Chrome detected — switching to headless mode...
249
+ `),await Sn(),await new Promise(($)=>setTimeout($,1000)),i=!0;else if(l&&w)process.stderr.write(`[greedysearch] Headless Chrome detected — switching to visible mode...
250
+ `),await Sn(),await new Promise(($)=>setTimeout($,1000)),i=!0}if(i?!1:await O()){await Wn();try{let w=G();if(w)$n(w),Ln(w)}catch{}return}let h=In();if(!h.acquired){if(await new Promise((l)=>setTimeout(l,3000)),await O(5000)){await Wn();return}}try{if(await O(1000)){await Wn();return}process.stderr.write(`GreedySearch Chrome not running on port ${Q} — auto-launching...
251
+ `);let l=[Ff()];if(process.env.GREEDY_SEARCH_VISIBLE!=="1")l.push("--headless");await new Promise(($,W)=>{pf(xn(),l,{stdio:["ignore",process.stderr,process.stderr],env:{...process.env,CDP_PROFILE_DIR:B,GREEDY_SEARCH_PROFILE_DIR:B}}).on("close",(V)=>V===0?$():W(Error("launch.mjs failed")))})}finally{h.release()}}var gu=`
252
252
  (function() {
253
253
  // Google consent page (consent.google.com)
254
254
  var g = document.querySelector('#L2AGLb, button[jsname="b3VHJd"], .tHlp8d');
@@ -265,10 +265,10 @@ ${r}`,"utf8");try{q(k)}catch{}af(f,k)}}catch{}finally{if(i)try{q(n)}catch{}}}asy
265
265
 
266
266
  return null;
267
267
  })()
268
- `;async function Zn(n,f){let u=await f(["eval",n,uu]).catch(()=>null);if(u&&u!=="null")await new Promise((g)=>setTimeout(g,1500))}var J={input:'textarea[name="newMessage"]',submitButton:'button[aria-label="Submit search"]',answerContainer:'div[class*="prose"]',paperCard:'a[data-testid="search-result"]',exportButton:'button[aria-label="Export"]'};async function gu(n){return await $(["eval",n,`(() => {
268
+ `;async function Jn(n,f){let u=await f(["eval",n,gu]).catch(()=>null);if(u&&u!=="null")await new Promise((g)=>setTimeout(g,1500))}var t={input:'textarea[name="newMessage"]',submitButton:'button[aria-label="Submit search"]',answerContainer:'div[class*="prose"]',paperCard:'a[data-testid="search-result"]',exportButton:'button[aria-label="Export"]'};async function iu(n){return await r(["eval",n,`(() => {
269
269
  const url = document.location.href || '';
270
270
  return url.indexOf('/sign-up/') !== -1 || url.indexOf('redirect_url=') !== -1;
271
- })()`]).catch(()=>"false")==="true"}async function dn(n){try{return JSON.parse(await $(["eval",n,`(() => {
271
+ })()`]).catch(()=>"false")==="true"}async function nf(n){try{return JSON.parse(await r(["eval",n,`(() => {
272
272
  const url = document.location.href || '';
273
273
  const title = document.title || '';
274
274
  const text = document.body?.innerText || '';
@@ -279,7 +279,7 @@ ${r}`,"utf8");try{q(k)}catch{}af(f,k)}}catch{}finally{if(i)try{q(n)}catch{}}}asy
279
279
  text.includes('refresh_request_origin_azp_mismatch') ||
280
280
  (text.includes('HTTP ERROR 405') && text.includes('This page isn'));
281
281
  return JSON.stringify({ stale, url, title, text: text.slice(0, 500) });
282
- })()`],5000))}catch{return{stale:!1,url:"",title:"",text:""}}}async function iu(n){for(let f of["https://consensus.app","https://clerk.consensus.app"])await $(["evalraw",n,"Storage.clearDataForOrigin",JSON.stringify({origin:f,storageTypes:"all"})]).catch((u)=>{console.error(`[consensus] Warning: failed to clear stale auth storage for ${f}: ${u.message}`)})}async function nf(n,f,u){let g=await dn(n);if(!g.stale)return!1;if(Z(f,"auth-storage-reset",u),console.error(`[consensus] Detected stale Clerk/Consensus auth state (${g.title||g.url}) — clearing per-origin storage and retrying navigation`),f.fallbackUsed="clear-stale-consensus-auth",await iu(n),await $(["nav",n,"https://consensus.app/"],20000),await new Promise((o)=>setTimeout(o,900)),(await dn(n)).stale)throw f.blockedBy="signin",f.verificationResult="needs-human",Error("Consensus auth session is stale — visible Chrome is open. Please sign in again, then rerun the search.");return!0}async function ou(n,f){if(await $(["click",n,J.input]),await new Promise((g)=>setTimeout(g,D(200))),await E(["type",n,"--stdin"],f),await new Promise((g)=>setTimeout(g,D(300))),await $(["eval",n,`(document.querySelector('${J.input}')?.value || '').length >= ${Math.floor(f.length*0.8)}`])!=="true")throw Error("Consensus input did not accept text — input verification failed")}async function lu(n){let u=await $(["eval",n,`(() => {
282
+ })()`],5000))}catch{return{stale:!1,url:"",title:"",text:""}}}async function ou(n){for(let f of["https://consensus.app","https://clerk.consensus.app"])await r(["evalraw",n,"Storage.clearDataForOrigin",JSON.stringify({origin:f,storageTypes:"all"})]).catch((u)=>{console.error(`[consensus] Warning: failed to clear stale auth storage for ${f}: ${u.message}`)})}async function ff(n,f,u){let g=await nf(n);if(!g.stale)return!1;if(j(f,"auth-storage-reset",u),console.error(`[consensus] Detected stale Clerk/Consensus auth state (${g.title||g.url}) — clearing per-origin storage and retrying navigation`),f.fallbackUsed="clear-stale-consensus-auth",await ou(n),await r(["nav",n,"https://consensus.app/"],20000),await new Promise((o)=>setTimeout(o,900)),(await nf(n)).stale)throw f.blockedBy="signin",f.verificationResult="needs-human",Error("Consensus auth session is stale — visible Chrome is open. Please sign in again, then rerun the search.");return!0}async function wu(n,f){if(await r(["click",n,t.input]),await new Promise((g)=>setTimeout(g,z(200))),await E(["type",n,"--stdin"],f),await new Promise((g)=>setTimeout(g,z(300))),await r(["eval",n,`(document.querySelector('${t.input}')?.value || '').length >= ${Math.floor(f.length*0.8)}`])!=="true")throw Error("Consensus input did not accept text — input verification failed")}async function hu(n){let u=await r(["eval",n,`(() => {
283
283
  if (window.__pdiInstalled) return 'already';
284
284
  window.__pdiInstalled = true;
285
285
  const _origOpen = XMLHttpRequest.prototype.open;
@@ -312,7 +312,7 @@ ${r}`,"utf8");try{q(k)}catch{}af(f,k)}}catch{}finally{if(i)try{q(n)}catch{}}}asy
312
312
  return _origSend.apply(this, arguments);
313
313
  };
314
314
  return 'installed';
315
- })()`]);return u==="installed"||u==="already"}async function wu(n,f=1e4){let u=`new Promise((resolve) => {
315
+ })()`]);return u==="installed"||u==="already"}async function lu(n,f=1e4){let u=`new Promise((resolve) => {
316
316
  const _deadline = Date.now() + ${f};
317
317
  function _check() {
318
318
  const resps = window.__papersDetailsResps || [];
@@ -328,13 +328,13 @@ ${r}`,"utf8");try{q(k)}catch{}af(f,k)}}catch{}finally{if(i)try{q(n)}catch{}}}asy
328
328
  }
329
329
  }
330
330
  _check();
331
- })`,g=await $(["eval",n,u],f+5000);return JSON.parse(g)}async function ru(n,f=8){if(!await z(n,J.paperCard,20000,500).catch(()=>!1))return console.error("[consensus] Warning: no paper cards appeared within 20s — Load More will be skipped"),0;await new Promise((i)=>setTimeout(i,800));let g=0;for(let i=0;i<f;i++){if(await $(["eval",n,`(() => {
331
+ })`,g=await r(["eval",n,u],f+5000);return JSON.parse(g)}async function $u(n,f=8){if(!await y(n,t.paperCard,20000,500).catch(()=>!1))return console.error("[consensus] Warning: no paper cards appeared within 20s — Load More will be skipped"),0;await new Promise((i)=>setTimeout(i,800));let g=0;for(let i=0;i<f;i++){if(await r(["eval",n,`(() => {
332
332
  const btns = Array.from(document.querySelectorAll('button'));
333
333
  return btns.some(b => {
334
334
  const t = (b.innerText || '').trim();
335
335
  return /load more/i.test(t) || /more results/i.test(t) || /show more/i.test(t);
336
336
  });
337
- })()`])!=="true")break;await $(["eval",n,`(() => {
337
+ })()`])!=="true")break;await r(["eval",n,`(() => {
338
338
  const btns = Array.from(document.querySelectorAll('button'));
339
339
  const btn = btns.find(b => {
340
340
  const t = (b.innerText || '').trim();
@@ -342,20 +342,20 @@ ${r}`,"utf8");try{q(k)}catch{}af(f,k)}}catch{}finally{if(i)try{q(n)}catch{}}}asy
342
342
  });
343
343
  btn?.click();
344
344
  return 'clicked';
345
- })()`]),g++,await new Promise((w)=>setTimeout(w,1500))}return g}async function hu(n){return await $(["eval",n,`(() => {
346
- const btn = document.querySelector('${J.exportButton}');
345
+ })()`]),g++,await new Promise((h)=>setTimeout(h,1500))}return g}async function ru(n){return await r(["eval",n,`(() => {
346
+ const btn = document.querySelector('${t.exportButton}');
347
347
  if (!btn) return 'no-export';
348
348
  btn.scrollIntoView({ block: 'center' });
349
349
  btn.click();
350
350
  return 'opened';
351
- })()`]),await new Promise((u)=>setTimeout(u,600)),await $(["eval",n,`(() => {
351
+ })()`]),await new Promise((u)=>setTimeout(u,600)),await r(["eval",n,`(() => {
352
352
  const btn = Array.from(document.querySelectorAll('button'))
353
353
  .find(b => /\\.CSV/.test((b.innerText || '').trim()));
354
354
  if (!btn) return 'no-csv';
355
355
  btn.click();
356
356
  return 'clicked';
357
- })()`])}function $u(n){let f=n?.paperDetailsListByPaperId||{},u=Object.keys(f),g=[];for(let i=0;i<u.length;i++){let o=f[u[i]]||{},w=o.url_slug||"",l=o.paper_id||o.hash_paper_id||u[i],r;if(w)r=`https://consensus.app/papers/${w}/${l}/`;else if(o.provider_url)r=o.provider_url;else if(o.doi)r=`https://doi.org/${o.doi}`;else r=`https://consensus.app/paper/${l}`;let h=[],W=o.badges||{};if(W.study_type==="rct")h.push("RCT");else if(W.study_type==="meta_analysis")h.push("META-ANALYSIS");else if(W.study_type==="systematic_review")h.push("SYSTEMATIC REVIEW");else if(W.study_type)h.push(W.study_type.toUpperCase());if(W.rigorous_journal)h.push("RIGOROUS JOURNAL");if(W.very_rigorous_journal)h.push("VERY RIGOROUS JOURNAL");if(W.highly_cited_paper)h.push("HIGHLY CITED");if(W.large_human_trial)h.push("LARGE HUMAN TRIAL");if(o.is_retracted)h.push("RETRACTED");if(o.open_access_pdf_url)h.push("OPEN ACCESS");g.push({title:o.title||"",url:r,rank:i+1,authors:Array.isArray(o.authors)?o.authors:[],year:o.year||null,journal:o.journal||o.publisher_name||null,doi:o.doi||null,citation_count:o.citation_count||0,snippet:o.abstract_takeaway||"",tags:h})}return g}async function ff(n){let f=`(() => {
358
- const cards = Array.from(document.querySelectorAll('${J.paperCard}'));
357
+ })()`])}function Wu(n){let f=n?.paperDetailsListByPaperId||{},u=Object.keys(f),g=[];for(let i=0;i<u.length;i++){let o=f[u[i]]||{},h=o.url_slug||"",w=o.paper_id||o.hash_paper_id||u[i],l;if(h)l=`https://consensus.app/papers/${h}/${w}/`;else if(o.provider_url)l=o.provider_url;else if(o.doi)l=`https://doi.org/${o.doi}`;else l=`https://consensus.app/paper/${w}`;let $=[],W=o.badges||{};if(W.study_type==="rct")$.push("RCT");else if(W.study_type==="meta_analysis")$.push("META-ANALYSIS");else if(W.study_type==="systematic_review")$.push("SYSTEMATIC REVIEW");else if(W.study_type)$.push(W.study_type.toUpperCase());if(W.rigorous_journal)$.push("RIGOROUS JOURNAL");if(W.very_rigorous_journal)$.push("VERY RIGOROUS JOURNAL");if(W.highly_cited_paper)$.push("HIGHLY CITED");if(W.large_human_trial)$.push("LARGE HUMAN TRIAL");if(o.is_retracted)$.push("RETRACTED");if(o.open_access_pdf_url)$.push("OPEN ACCESS");g.push({title:o.title||"",url:l,rank:i+1,authors:Array.isArray(o.authors)?o.authors:[],year:o.year||null,journal:o.journal||o.publisher_name||null,doi:o.doi||null,citation_count:o.citation_count||0,snippet:o.abstract_takeaway||"",tags:$})}return g}async function uf(n){let f=`(() => {
358
+ const cards = Array.from(document.querySelectorAll('${t.paperCard}'));
359
359
  const sources = [];
360
360
  const seen = new Set();
361
361
  for (const card of cards) {
@@ -377,8 +377,8 @@ ${r}`,"utf8");try{q(k)}catch{}af(f,k)}}catch{}finally{if(i)try{q(n)}catch{}}}asy
377
377
  sources.push({ title, url, rank, snippet, tags });
378
378
  }
379
379
  return JSON.stringify(sources);
380
- })()`,u=await $(["eval",n,f],1e4);return JSON.parse(u)}async function Wu(n){let f=`(() => {
381
- const prose = document.querySelector('${J.answerContainer}');
380
+ })()`,u=await r(["eval",n,f],1e4);return JSON.parse(u)}async function Qu(n){let f=`(() => {
381
+ const prose = document.querySelector('${t.answerContainer}');
382
382
  return prose?.innerText?.trim() || '';
383
- })()`;return await $(["eval",n,f],1e4)||""}var Qu=`Usage: node extractors/consensus.mjs "<query>" [--tab <prefix>]
384
- `;async function Xu(){let n=await qn(process.argv.slice(2));Vn(n,Qu);let{query:f,tabPrefix:u,short:g}=Nn(n),i=Date.now(),w={engine:"consensus",mode:process.env.GREEDY_SEARCH_VISIBLE==="1"?"visible":"headless",clipboardEmpty:null,fallbackUsed:null,blockedBy:null,verificationResult:null,inputReady:null};try{if(process.env.GREEDY_SEARCH_VISIBLE!=="1"&&process.env.GREEDY_SEARCH_ALWAYS_VISIBLE!=="1")process.env.GREEDY_SEARCH_HEADLESS="1";if(await En(),!u)await $(["list"]);let l=await bn(u),r=await $(["eval",l,"document.location.href"]).catch(()=>""),h=!1;try{let t=new URL(r).hostname.toLowerCase();h=t==="consensus.app"||t.endsWith(".consensus.app")}catch{}if(!h)Z(w,"nav",i),await $(["nav",l,"https://consensus.app/"],20000),await new Promise((t)=>setTimeout(t,600));await nf(l,w,i),await Zn(l,$),Z(w,"input-wait",i);let W=await z(l,J.input,15000,400);if(w.inputReady=W,!W)if(await nf(l,w,i)){let c=await z(l,J.input,15000,400);if(w.inputReady=c,c)await Zn(l,$);else throw Error("Consensus input not found after stale auth recovery — page may not have loaded or is in unexpected state")}else throw Error("Consensus input not found — page may not have loaded or is in unexpected state");if(await new Promise((t)=>setTimeout(t,D(d.postClick))),Z(w,"type-and-submit",i),await ou(l,f),await new Promise((t)=>setTimeout(t,D(d.postType))),await $(["eval",l,`document.querySelector('${J.submitButton}')?.click()`]),await new Promise((t)=>setTimeout(t,2000)),await gu(l))throw w.blockedBy="signin",Error("Consensus requires sign-in — please sign in or create a free account in the visible browser window. Once signed in, cookies persist for future runs.");Z(w,"answer-wait",i),await z(l,J.answerContainer,30000,500),await lu(l),Z(w,"expand-refs",i);let j=await ru(l,8);if(j===0)console.error("[consensus] Note: 'Load more results' button not present (initial page has all references)");else console.error(`[consensus] Clicked 'Load more results' ${j} time(s) to expand references`);await new Promise((t)=>setTimeout(t,1500)),Z(w,"csv-click",i);let V=await hu(l);if(V!=="clicked")console.error(`[consensus] Export → .CSV click did not register (${V}) — falling back to DOM`);Z(w,"wait-csv-resp",i);let _=await wu(l,12000),N=[],jn="dom";if(_.ok)N=$u(_.data),jn="api-intercept",console.error(`[consensus] Captured /api/papers/details/ response with ${_.count} paper(s)`);else console.error(`[consensus] /api/papers/details/ response not captured (${_.reason}) — falling back to DOM cards`);Z(w,"extract",i);let Jn=await Wu(l);if(!Jn)throw Error("No answer extracted — Consensus may not have responded");if(N.length===0)N=await ff(l);else{let t=await ff(l),c=new Set(N.map((m)=>m.url));for(let m of t)if(!c.has(m.url))N.push(m)}let uf=await $(["eval",l,"document.location.href"]).catch(()=>"");w.durationMs=Date.now()-i,w.sourcePath=jn,Z(w,"done",i),Hn({query:f,url:uf,answer:_n(Jn,g),sources:N,_envelope:nn(w)})}catch(l){w.durationMs=Date.now()-i,Kn(l,nn(w))}}Xu();
383
+ })()`;return await r(["eval",n,f],1e4)||""}var Xu=`Usage: node extractors/consensus.mjs "<query>" [--tab <prefix>]
384
+ `;async function Zu(){let n=await Nn(process.argv.slice(2));_n(n,Xu);let{query:f,tabPrefix:u,short:g}=Vn(n),i=Date.now(),h={engine:"consensus",mode:process.env.GREEDY_SEARCH_VISIBLE==="1"?"visible":"headless",clipboardEmpty:null,fallbackUsed:null,blockedBy:null,verificationResult:null,inputReady:null};try{if(process.env.GREEDY_SEARCH_VISIBLE!=="1"&&process.env.GREEDY_SEARCH_ALWAYS_VISIBLE!=="1")process.env.GREEDY_SEARCH_HEADLESS="1";if(await dn(),!u)await r(["list"]);let w=await Bn(u),l=await r(["eval",w,"document.location.href"]).catch(()=>""),$=!1;try{let Z=new URL(l).hostname.toLowerCase();$=Z==="consensus.app"||Z.endsWith(".consensus.app")}catch{}if(!$)j(h,"nav",i),await r(["nav",w,"https://consensus.app/"],20000),await new Promise((Z)=>setTimeout(Z,600));await ff(w,h,i),await Jn(w,r),j(h,"input-wait",i);let W=await y(w,t.input,15000,400);if(h.inputReady=W,!W)if(await ff(w,h,i)){let c=await y(w,t.input,15000,400);if(h.inputReady=c,c)await Jn(w,r);else throw Error("Consensus input not found after stale auth recovery — page may not have loaded or is in unexpected state")}else throw Error("Consensus input not found — page may not have loaded or is in unexpected state");if(await new Promise((Z)=>setTimeout(Z,z(d.postClick))),j(h,"type-and-submit",i),await wu(w,f),await new Promise((Z)=>setTimeout(Z,z(d.postType))),await r(["eval",w,`document.querySelector('${t.submitButton}')?.click()`]),await new Promise((Z)=>setTimeout(Z,2000)),await iu(w))throw h.blockedBy="signin",Error("Consensus requires sign-in — please sign in or create a free account in the visible browser window. Once signed in, cookies persist for future runs.");j(h,"answer-wait",i),await y(w,t.answerContainer,30000,500),await hu(w),j(h,"expand-refs",i);let J=await $u(w,8);if(J===0)console.error("[consensus] Note: 'Load more results' button not present (initial page has all references)");else console.error(`[consensus] Clicked 'Load more results' ${J} time(s) to expand references`);await new Promise((Z)=>setTimeout(Z,1500)),j(h,"csv-click",i);let V=await ru(w);if(V!=="clicked")console.error(`[consensus] Export → .CSV click did not register (${V}) — falling back to DOM`);j(h,"wait-csv-resp",i);let _=await lu(w,12000),N=[],tn="dom";if(_.ok)N=Wu(_.data),tn="api-intercept",console.error(`[consensus] Captured /api/papers/details/ response with ${_.count} paper(s)`);else console.error(`[consensus] /api/papers/details/ response not captured (${_.reason}) — falling back to DOM cards`);j(h,"extract",i);let bn=await Qu(w);if(!bn)throw Error("No answer extracted — Consensus may not have responded");if(N.length===0)N=await uf(w);else{let Z=await uf(w),c=new Set(N.map((m)=>m.url));for(let m of Z)if(!c.has(m.url))N.push(m)}let gf=await r(["eval",w,"document.location.href"]).catch(()=>"");h.durationMs=Date.now()-i,h.sourcePath=tn,j(h,"done",i),Kn({query:f,url:gf,answer:Hn(bn,g),sources:N,_envelope:nn(h)})}catch(w){h.durationMs=Date.now()-i,Un(w,nn(h))}}Zu();