@duckmind/dm-darwin-x64 0.42.7 → 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.
- package/dm +0 -0
- package/extensions/.dm-extensions.json +2 -2
- package/extensions/greedysearch-dm/bin/search.mjs +106 -106
- package/extensions/greedysearch-dm/extractors/consensus.mjs +35 -35
- package/extensions/greedysearch-dm/extractors/gemini.mjs +38 -38
- package/extensions/greedysearch-dm/extractors/logically.mjs +32 -32
- package/extensions/greedysearch-dm/src/search/chrome.mjs +20 -20
- package/extensions/greedysearch-dm/src/search/fetch-source.mjs +33 -33
- package/extensions/greedysearch-dm/src/search/research.mjs +68 -68
- package/package.json +1 -1
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import{randomInt as
|
|
3
|
-
`)[0]?.slice(0,8);if(!f)throw Error("No Chrome tabs found. Is Chrome running with --remote-debugging-port=9222?");let u=await
|
|
4
|
-
`)}return i}async function
|
|
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
|
|
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
|
|
232
|
-
`)}function
|
|
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
|
|
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
|
|
238
|
-
`,"utf8")}catch{}}
|
|
239
|
-
[greedysearch] Available synthesizers: ${
|
|
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"},
|
|
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
|
|
244
|
-
`)[0],10):null}catch{return null}}function cn(n){try{if(
|
|
245
|
-
`)[0],10):null}catch{return null}}async function
|
|
246
|
-
`);return f}var
|
|
247
|
-
${
|
|
248
|
-
`);return}
|
|
249
|
-
`),await
|
|
250
|
-
`),await
|
|
251
|
-
`);let
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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((
|
|
346
|
-
const btn = document.querySelector('${
|
|
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
|
|
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
|
|
358
|
-
const cards = Array.from(document.querySelectorAll('${
|
|
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
|
|
381
|
-
const prose = document.querySelector('${
|
|
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
|
|
384
|
-
`;async function
|
|
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();
|