@duckmind/dm-darwin-x64 0.42.5 → 0.42.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dm +0 -0
- package/extensions/.dm-extensions.json +4 -4
- package/extensions/dm-cua/bin/browser-cua.mjs +3 -3
- package/extensions/dm-cua/index.js +2 -2
- package/extensions/dm-cua/src/browser-cua-lib.mjs +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,4 +1,4 @@
|
|
|
1
|
-
import{spawn as
|
|
1
|
+
import{spawn as Qt,execFileSync as Ve,execSync as ge}from"node:child_process";import{existsSync as C,readFileSync as X,renameSync as Ht,unlinkSync as d,writeFileSync as G}from"node:fs";import de from"node:http";import{platform as me,tmpdir as qt}from"node:os";import{join as fe}from"node:path";import{spawn as Ie}from"node:child_process";import{dirname as Ee,join as Fe}from"node:path";import{fileURLToPath as ze}from"node:url";import{basename as et}from"node:path";function tt(e=process.env,t=process.execPath){let r=e.GREEDY_SEARCH_NODE||e.NODE_BINARY||e.NODE;if(r?.trim())return r.trim();let n=et(t||"").toLowerCase();if(n==="node"||n==="node.exe")return t;return"node"}var rt=Ee(ze(import.meta.url)),nt=Fe(rt,"..","bin","cdp.mjs");function I(e,t=30000){return it(e,null,t)}function it(e,t=null,r=30000){return new Promise((n,i)=>{let a=Ie(tt(),[nt,...e],{stdio:[t==null?"ignore":"pipe","pipe","pipe"]});if(t!=null)a.stdin.write(t),a.stdin.end();let u="",s="";a.stdout.on("data",(l)=>u+=l),a.stderr.on("data",(l)=>s+=l);let o=setTimeout(()=>{a.kill(),i(Error(`cdp timeout: ${e[0]}`))},r);a.on("close",(l)=>{if(clearTimeout(o),l===0)n(u.trim());else i(Error(s.trim()||`cdp exit ${l}`))})})}async function E(e){await I(["evalraw",e,"Page.addScriptToEvaluateOnNewDocument",JSON.stringify({source:`
|
|
2
2
|
(function() {
|
|
3
3
|
// ── Runtime.enable / CDP detection masking ──────────────
|
|
4
4
|
try { delete window.__REBROWSER_RUNTIME_ENABLE; } catch(_) {}
|
|
@@ -206,29 +206,29 @@ import{spawn as Bt,execFileSync as qe,execSync as pe}from"node:child_process";im
|
|
|
206
206
|
};
|
|
207
207
|
} catch(_) {}
|
|
208
208
|
})();
|
|
209
|
-
`})])}import{basename as
|
|
209
|
+
`})])}import{basename as at}from"node:path";function ye(e=process.env,t=process.execPath){let r=e.GREEDY_SEARCH_NODE||e.NODE_BINARY||e.NODE;if(r?.trim())return r.trim();let n=at(t||"").toLowerCase();if(n==="node"||n==="node.exe")return t;return"node"}import{existsSync as ve}from"node:fs";import{platform as ot}from"node:os";import{join as y}from"node:path";function f(e){let t=ot()==="win32",r=process.env.SystemRoot||"C:\\Windows",n={win32:{powershell:y(r,"System32","WindowsPowerShell","v1.0","powershell.exe"),powershell_ise:y(r,"System32","WindowsPowerShell","v1.0","powershell_ise.exe"),netstat:y(r,"System32","netstat.exe"),taskkill:y(r,"System32","taskkill.exe"),tasklist:y(r,"System32","tasklist.exe"),cmd:y(r,"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=t?n.win32:n.unix,a=e.toLowerCase();if(i[a]&&ve(i[a]))return i[a];if(t&&a==="netstat"){let u=y(r,"Sysnative","netstat.exe");if(ve(u))return u}return e}import{existsSync as B,mkdirSync as st,readFileSync as $e,writeFileSync as lt}from"node:fs";import{homedir as ut}from"node:os";import{join as Pe}from"node:path";import{tmpdir as Q}from"node:os";var c=9222,k=`${Q().replaceAll("\\","/")}/greedysearch-chrome-profile`,A=`${k}/DevToolsActivePort`,re=`${Q().replaceAll("\\","/")}/cdp-pages.json`,v=`${Q().replaceAll("\\","/")}/greedysearch-chrome-mode`,hr=`${Q().replaceAll("\\","/")}/greedysearch-visible-recovery.jsonl`,z=Pe(ut(),".dm"),p=Pe(z,"greedyconfig"),ee=["perplexity","google","chatgpt"],te="gemini";function ct(){try{if(B(p)){let e=$e(p,"utf8"),t=JSON.parse(e);if(Array.isArray(t.engines)&&t.engines.length>0&&t.engines.every((r)=>typeof r==="string")){let r=t.engines.filter((i)=>F[i]),n=t.engines.filter((i)=>!F[i]);if(n.length>0)process.stderr.write(`[greedysearch] Warning: ignoring unknown engine(s) in ${p}: ${n.join(", ")}
|
|
210
210
|
[greedysearch] Available engines: ${Object.keys(F).join(", ")}
|
|
211
211
|
`);if(r.length>0)return r;process.stderr.write(`[greedysearch] Warning: no valid engines in ${p}, falling back to defaults: ${ee.join(", ")}
|
|
212
|
-
`)}}}catch{}return ee}function
|
|
213
|
-
`,"utf8")}catch{}}
|
|
214
|
-
[greedysearch] Available synthesizers: ${
|
|
212
|
+
`)}}}catch{}return ee}function ft(){try{if(!B(z))st(z,{recursive:!0});if(!B(p))lt(p,JSON.stringify({engines:ee,synthesizer:te},null,2)+`
|
|
213
|
+
`,"utf8")}catch{}}ft();var _e=["gemini","chatgpt"];function pt(){try{if(B(p)){let e=$e(p,"utf8"),t=JSON.parse(e);if(typeof t.synthesizer==="string"){let r=t.synthesizer.toLowerCase();if(_e.includes(r))return r;process.stderr.write(`[greedysearch] Warning: unknown synthesizer "${t.synthesizer}" in ${p}
|
|
214
|
+
[greedysearch] Available synthesizers: ${_e.join(", ")}
|
|
215
215
|
[greedysearch] Falling back to default: ${te}
|
|
216
|
-
`)}}}catch{}return te}var F={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"},
|
|
216
|
+
`)}}}catch{}return te}var F={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"},wr=ct();var br=pt(),yr=Math.max(1,Number.parseInt(process.env.GREEDY_FETCH_CONCURRENCY||"5",10)||5);process.env.CDP_PROFILE_DIR=k;import{existsSync as $r,mkdirSync as gt,readFileSync as Ae,renameSync as dt,unlinkSync as mt,writeFileSync as De}from"node:fs";import ht from"node:http";import{platform as Se}from"node:os";import{dirname as wt}from"node:path";var Ne=[9222,9223],bt=/^(0|false|no|off)$/i;function yt(e){return[...new Set(e.filter(Boolean))]}function xe(e,t="explicit"){let r=new URL(e),n=r.protocol==="wss:"?"wss:":"ws:",i=r.port?Number.parseInt(r.port,10):n==="wss:"?443:80;return{source:t,wsUrl:`${n}//${r.host}${r.pathname}${r.search}`,host:r.hostname,port:i,path:`${r.pathname}${r.search}`,httpUrl:`http://${r.hostname}:${i}`}}function Ce(e=process.env){return![e.GREEDY_SEARCH_REUSE_EXISTING_BROWSER,e.DM_CUA_REUSE_EXISTING_BROWSER].some((t)=>t!==void 0&&bt.test(String(t)))}function ne(e){let t=Number.parseInt(String(e??""),10);return Number.isInteger(t)&&t>0&&t<65536?t:null}function vt(e){if(!e)return null;let t=String(e).trim();if(!t)return null;let r=new URL(t.includes("://")?t:`http://${t}`),n=ne(r.port)||(r.protocol==="https:"?443:80);return{host:r.hostname,port:n,source:`env:${t}`}}function _t(e){if(!e)return[];return String(e).split(/[,\s]+/).map(ne).filter(Boolean)}function $t({env:e=process.env,platformName:t=Se(),procVersionText:r=null}={}){if(t!=="linux")return!1;if(e.WSL_DISTRO_NAME||e.WSL_INTEROP)return!0;let n=r??(()=>{try{return Ae("/proc/version","utf8")}catch{return""}})();return/microsoft|wsl/i.test(n)}function Pt({env:e=process.env,platformName:t=Se(),procVersionText:r=null,resolvConfText:n=null}={}){let i=["localhost","127.0.0.1"];if($t({env:e,platformName:t,procVersionText:r})){let a=(n??(()=>{try{return Ae("/etc/resolv.conf","utf8")}catch{return""}})()).match(/^\s*nameserver\s+([^\s#]+)/m);if(a)i.push(a[1]);i.push("host.docker.internal")}return yt(i)}function xt({env:e=process.env,hosts:t=Pt({env:e}),autoPorts:r=Ne,excludePorts:n=[]}={}){let i=[];for(let o of["GREEDY_SEARCH_CDP_WS_URL","DM_CUA_CDP_WS_URL","CDP_WS_URL"])if(e[o])i.push({wsUrl:e[o],source:`env:${o}`});for(let o of["GREEDY_SEARCH_CDP_URL","DM_CUA_CDP_URL","CDP_URL"])try{let l=vt(e[o]);if(l)i.push(l)}catch{}for(let o of["GREEDY_SEARCH_EXISTING_CDP_PORT","DM_CUA_EXISTING_CDP_PORT","GREEDY_SEARCH_CDP_PORT","DM_CUA_CDP_PORT"]){let l=ne(e[o]);if(!l)continue;for(let h of t)i.push({host:h,port:l,source:`env:${o}`})}let a=_t(e.GREEDY_SEARCH_EXISTING_CDP_PORTS);for(let o of a)for(let l of t)i.push({host:l,port:o,source:"env:GREEDY_SEARCH_EXISTING_CDP_PORTS"});let u=new Set(n.filter(Boolean));if(Ce(e))for(let o of r){if(u.has(o))continue;for(let l of t)i.push({host:l,port:o,source:"auto"})}let s=new Set;return i.filter((o)=>{let l=o.wsUrl||`${o.host}:${o.port}`;if(s.has(l))return!1;return s.add(l),!0})}async function At(e,{timeoutMs:t=700}={}){if(e.wsUrl)return xe(e.wsUrl,e.source);let r=e.host||"localhost",n=e.port;if(!n)return null;let i=await new Promise((a)=>{let u=ht.get({host:r,port:n,path:"/json/version",timeout:t},(s)=>{let o="";s.setEncoding("utf8"),s.on("data",(l)=>o+=l),s.on("end",()=>a(s.statusCode===200?o:null))});u.on("error",()=>a(null)),u.setTimeout(t,()=>{u.destroy(),a(null)})});if(!i)return null;try{let a=JSON.parse(i);if(!a.webSocketDebuggerUrl)return null;let u=xe(a.webSocketDebuggerUrl,e.source),s=u.path||new URL(a.webSocketDebuggerUrl).pathname;return{...u,host:r,port:n,httpUrl:`http://${r}:${n}`,wsUrl:`ws://${r}:${n}${s}`,path:s}}catch{return null}}async function Dt(e={}){for(let t of xt(e)){let r=await At(t,e);if(r)return r}return null}function St(e){if(!e)throw Error("missing CDP endpoint");let t=new URL(e.wsUrl);return`${t.host}
|
|
217
217
|
${t.pathname}${t.search}
|
|
218
|
-
`}function
|
|
218
|
+
`}function Nt(e,t){gt(wt(e),{recursive:!0});let r=`${e}.tmp-${process.pid}`;De(r,St(t),"utf8");try{mt(e)}catch{}dt(r,e)}async function ke({activePortFile:e,modeFile:t,env:r=process.env,excludePorts:n=[],autoPorts:i=Ne}={}){if(!Ce(r))return null;let a=await Dt({env:r,excludePorts:n,autoPorts:i});if(!a)return null;if(Nt(e,a),t)De(t,"existing","utf8");return a}import{execFileSync as We,execSync as ae}from"node:child_process";import{existsSync as H,mkdirSync as Ct,readFileSync as D,unlinkSync as P,writeFileSync as x}from"node:fs";import{platform as ue,tmpdir as Xe}from"node:os";import{existsSync as q,mkdirSync as kt,readFileSync as je,writeFileSync as Wt}from"node:fs";import{homedir as Jt}from"node:os";import{join as Be}from"node:path";import{tmpdir as L}from"node:os";import{existsSync as Me}from"node:fs";import{platform as Ot}from"node:os";import{join as _}from"node:path";var S=9222,W=`${L().replaceAll("\\","/")}/greedysearch-chrome-profile`,Rr=`${W}/DevToolsActivePort`,Ur=`${L().replaceAll("\\","/")}/cdp-pages.json`,Or=`${L().replaceAll("\\","/")}/greedysearch-chrome-mode`,Tr=`${L().replaceAll("\\","/")}/greedysearch-visible-recovery.jsonl`,oe=Be(Jt(),".dm"),g=Be(oe,"greedyconfig"),se=["perplexity","google","chatgpt"],le="gemini";function Mt(){try{if(q(g)){let e=je(g,"utf8"),t=JSON.parse(e);if(Array.isArray(t.engines)&&t.engines.length>0&&t.engines.every((r)=>typeof r==="string")){let r=t.engines.filter((i)=>ie[i]),n=t.engines.filter((i)=>!ie[i]);if(n.length>0)process.stderr.write(`[greedysearch] Warning: ignoring unknown engine(s) in ${g}: ${n.join(", ")}
|
|
219
219
|
[greedysearch] Available engines: ${Object.keys(ie).join(", ")}
|
|
220
220
|
`);if(r.length>0)return r;process.stderr.write(`[greedysearch] Warning: no valid engines in ${g}, falling back to defaults: ${se.join(", ")}
|
|
221
|
-
`)}}}catch{}return se}function
|
|
222
|
-
`,"utf8")}catch{}}
|
|
223
|
-
[greedysearch] Available synthesizers: ${
|
|
221
|
+
`)}}}catch{}return se}function Rt(){try{if(!q(oe))kt(oe,{recursive:!0});if(!q(g))Wt(g,JSON.stringify({engines:se,synthesizer:le},null,2)+`
|
|
222
|
+
`,"utf8")}catch{}}Rt();var Je=["gemini","chatgpt"];function Ut(){try{if(q(g)){let e=je(g,"utf8"),t=JSON.parse(e);if(typeof t.synthesizer==="string"){let r=t.synthesizer.toLowerCase();if(Je.includes(r))return r;process.stderr.write(`[greedysearch] Warning: unknown synthesizer "${t.synthesizer}" in ${g}
|
|
223
|
+
[greedysearch] Available synthesizers: ${Je.join(", ")}
|
|
224
224
|
[greedysearch] Falling back to default: ${le}
|
|
225
|
-
`)}}}catch{}return le}var ie={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"},
|
|
226
|
-
`)[0],10):null}catch{return null}}function
|
|
227
|
-
`)[0],10):null}catch{return null}}async function
|
|
228
|
-
`);return t}var
|
|
229
|
-
`)[0];if(!t)throw Error("No Chrome tabs found");return t.slice(0,8)}async function
|
|
230
|
-
${o}`,"utf8");try{d(A)}catch{}
|
|
231
|
-
`);return}
|
|
232
|
-
`),await
|
|
233
|
-
`),await
|
|
234
|
-
`);let o=[
|
|
225
|
+
`)}}}catch{}return le}var ie={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"},Xr=Mt(),jr=Ut(),Br=Math.max(1,Number.parseInt(process.env.GREEDY_FETCH_CONCURRENCY||"5",10)||5);process.env.CDP_PROFILE_DIR=W;function w(e){let t=Ot()==="win32",r=process.env.SystemRoot||"C:\\Windows",n={win32:{powershell:_(r,"System32","WindowsPowerShell","v1.0","powershell.exe"),powershell_ise:_(r,"System32","WindowsPowerShell","v1.0","powershell_ise.exe"),netstat:_(r,"System32","netstat.exe"),taskkill:_(r,"System32","taskkill.exe"),tasklist:_(r,"System32","tasklist.exe"),cmd:_(r,"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=t?n.win32:n.unix,a=e.toLowerCase();if(i[a]&&Me(i[a]))return i[a];if(t&&a==="netstat"){let u=_(r,"Sysnative","netstat.exe");if(Me(u))return u}return e}var R=Xe().replaceAll("\\","/"),V=`${R}/greedysearch-chrome-metadata.json`,$=`${R}/greedysearch-chrome-launch.lock`,Tt=15000,Z=`${R}/greedysearch-chrome.pid`,Y=`${R}/greedysearch-chrome-mode`,J=`${R}/greedysearch-chrome-last-activity`;function M(e){if(!Number.isInteger(e)||e<=0)return!1;try{return process.kill(e,0),!0}catch{return!1}}function Xt(e){if(!M(e))return null;try{if(ue()==="win32")return We(w("powershell"),["-NoProfile","-NonInteractive","-Command",`(Get-CimInstance Win32_Process -Filter "ProcessId = ${e}").CommandLine`],{encoding:"utf8",windowsHide:!0,timeout:5000}).trim()||null;return We(w("ps"),["-p",String(e),"-o","command="],{encoding:"utf8",timeout:5000}).trim()||null}catch{return null}}function jt(e,t,r=S){if(!e)return!1;let n=(u)=>String(u||"").replaceAll("\\","/").toLowerCase(),i=n(e),a=n(t);return i.includes(a)&&i.includes(`--remote-debugging-port=${r}`)&&!i.includes("--type=")}function Re(e,t,r=S){return jt(Xt(e),t,r)}function Bt(e=S){try{if(ue()==="win32"){let r=ae(`${w("netstat")} -ano -p TCP 2>nul`,{encoding:"utf8"}),n=new RegExp(String.raw`TCP\s+\S+:${e}\s+\S+:0\s+LISTENING\s+(\d+)`,"i"),i=r.match(n);return i?Number.parseInt(i[1],10):null}let t=ae(`${w("lsof")} -i :${e} -t 2>/dev/null || ${w("ss")} -tlnp 2>/dev/null | ${w("grep")} :${e} | ${w("grep")} -oP 'pid=\\K\\d+'`,{encoding:"utf8"}).trim();return t?Number.parseInt(t.split(`
|
|
226
|
+
`)[0],10):null}catch{return null}}function Ue(e){try{if(ue()==="win32")ae(`${w("taskkill")} /F /T /PID ${e}`,{stdio:"ignore"});else{try{process.kill(-e,"SIGKILL")}catch{}try{process.kill(e,"SIGKILL")}catch{}}return!0}catch{return!1}}function U(){try{if(H(V)){let e=D(V,"utf8"),t=JSON.parse(e);if(t&&typeof t.tempDir==="string"&&typeof t.debugPort==="number")return{browserPid:Number.isInteger(t.browserPid)?t.browserPid:void 0,debugPort:t.debugPort,tempDir:t.tempDir,clientPids:Array.isArray(t.clientPids)?t.clientPids.filter((r)=>Number.isInteger(r)&&r>0):[],sessionMode:t.sessionMode==="visible"?"visible":"headless",lastActivity:Number.isFinite(t.lastActivity)?t.lastActivity:0,launchedAt:Number.isFinite(t.launchedAt)?t.launchedAt:0}}}catch{}try{let e=H(Z)?Number.parseInt(D(Z,"utf8").trim(),10)||void 0:void 0,t=H(Y)?D(Y,"utf8").trim()==="visible"?"visible":"headless":"headless",r=H(J)?Number.parseInt(D(J,"utf8").trim(),10)||0:0;return{browserPid:e,debugPort:S,tempDir:W,clientPids:e?[e]:[],sessionMode:t,lastActivity:r,launchedAt:0}}catch{return null}}function K(e){try{x(V,JSON.stringify({browserPid:e.browserPid,debugPort:e.debugPort,tempDir:e.tempDir,clientPids:[...new Set(e.clientPids.filter((t)=>t>0))],sessionMode:e.sessionMode,lastActivity:e.lastActivity,launchedAt:e.launchedAt},null,2),"utf8")}catch{}try{if(e.browserPid)x(Z,String(e.browserPid),"utf8")}catch{}try{x(Y,e.sessionMode,"utf8")}catch{}try{x(J,String(e.lastActivity),"utf8")}catch{}}function Oe(){try{P(V)}catch{}try{P(Z)}catch{}try{P(Y)}catch{}try{P(J)}catch{}}function Qe(e){if(!e)return e;let t={...e,clientPids:[...new Set([...e.clientPids,process.pid].filter((r)=>M(r)||r===process.pid))]};return K(t),t}function ce(e){let t=Date.now();try{if(e)K({...e,lastActivity:t});else x(J,String(t),"utf8")}catch{}}var Vr=Number.parseInt(process.env.GREEDY_SEARCH_IDLE_TIMEOUT_MINUTES||"5",10)||5;var Te=!1;function He(){if(Te)return;Te=!0;let e=U();if(!e)return;if(e.browserPid){if(!M(e.browserPid)){Ue(e.browserPid),Oe();return}if(!Re(e.browserPid,e.tempDir,e.debugPort))Oe();else K({...e,clientPids:e.clientPids.filter((r)=>M(r))})}let t=Bt();if(t&&t!==e.browserPid)if(Re(t,W,S))K({browserPid:t,debugPort:S,tempDir:W,clientPids:[t],sessionMode:e.sessionMode,lastActivity:Date.now(),launchedAt:Date.now()});else Ue(t)}function qe(){Ct(Xe(),{recursive:!0});try{let e=JSON.stringify({pid:process.pid,ts:Date.now()});return x($,e,{encoding:"utf8",flag:"wx"}),{acquired:!0,release:()=>{try{let t=D($,"utf8");if(JSON.parse(t).pid===process.pid)P($)}catch{}}}}catch(e){if(e?.code!=="EEXIST")return{acquired:!1,release:()=>{}}}try{let e=D($,"utf8"),t=JSON.parse(e),r=Date.now()-(t.ts||0);if(!M(t.pid)||r>Tt){try{P($)}catch{}try{let n=JSON.stringify({pid:process.pid,ts:Date.now()});return x($,n,{encoding:"utf8",flag:"wx"}),{acquired:!0,release:()=>{try{P($)}catch{}}}}catch{return{acquired:!1,release:()=>{}}}}}catch{}return{acquired:!1,release:()=>{}}}var Vt=import.meta.dirname||new URL(".",import.meta.url).pathname.replace(/^\/([A-Z]:)/,"$1");function Zt(e=Vt){let t=[fe(e,"launch.mjs"),fe(e,"..","bin","launch.mjs"),fe(e,"..","..","bin","launch.mjs")];for(let r of t)if(C(r))return r;return t[t.length-1]}var Ye=qt().replaceAll("\\","/"),O=`${Ye}/greedysearch-chrome.pid`,T=`${Ye}/greedysearch-chrome-last-activity`,Yt=Number.parseInt(process.env.GREEDY_SEARCH_IDLE_TIMEOUT_MINUTES||"5",10)||5,Kt=Number.parseInt(process.env.GREEDY_SEARCH_VISIBLE_IDLE_TIMEOUT_MINUTES||"60",10)||60;function Lt(e,t=c){let r=String(e||"").toLowerCase();if(!r.includes(`--remote-debugging-port=${t}`)||r.includes("--type="))return null;return r.includes("--headless")}function Ke(){try{let e=he(),t=e?It(e):null,r=Lt(t);if(r!==null){try{G(v,r?"headless":"visible","utf8")}catch{}return r}}catch{}try{if(!C(v))return!0;return X(v,"utf8").trim()==="headless"}catch{return!0}}function Gt(){try{G(T,String(Date.now()),"utf8")}catch{}try{let e=U();if(e)ce(e)}catch{}}function It(e){try{if(me()==="win32")return Ve(f("powershell"),["-NoProfile","-NonInteractive","-Command",`(Get-CimInstance Win32_Process -Filter "ProcessId = ${e}").CommandLine`],{encoding:"utf8",windowsHide:!0,timeout:5000}).trim()||null;return Ve(f("ps"),["-p",String(e),"-o","command="],{encoding:"utf8",timeout:5000}).trim()||null}catch{return null}}function he(){try{if(me()==="win32"){let t=ge(`${f("netstat")} -ano -p TCP 2>nul`,{encoding:"utf8"}),r=new RegExp(String.raw`TCP\s+\S+:${c}\s+\S+:0\s+LISTENING\s+(\d+)`,"i"),n=t.match(r);return n?Number.parseInt(n[1],10):null}let e=ge(`${f("lsof")} -i :${c} -t 2>/dev/null || ${f("ss")} -tlnp 2>/dev/null | ${f("grep")} :${c} | ${f("grep")} -oP 'pid=\\K\\d+'`,{encoding:"utf8"}).trim();return e?Number.parseInt(e.split(`
|
|
227
|
+
`)[0],10):null}catch{return null}}async function Et(e=1500){try{let r=await new Promise((i,a)=>{let u=de.get(`http://localhost:${c}/json/version`,(s)=>{let o="";s.on("data",(l)=>o+=l),s.on("end",()=>{try{i(JSON.parse(o))}catch{a(Error("bad JSON"))}})});u.on("error",a),u.setTimeout(1000,()=>{u.destroy(),a(Error("timeout"))})}),n=new globalThis.WebSocket(r.webSocketDebuggerUrl);await new Promise((i)=>{n.onopen=()=>{n.send(JSON.stringify({id:1,method:"Browser.close"})),setTimeout(()=>{n.close(),i()},200)},n.onerror=()=>i(),setTimeout(i,1000)})}catch{}let t=Date.now()+e;while(Date.now()<t){if(!he())return!0;await new Promise((n)=>setTimeout(n,150))}return Ft()}function Ft(){try{let e=he();if(!e&&C(O))e=Number.parseInt(X(O,"utf8").trim(),10)||null;if(!e)return!1;if(me()==="win32")ge(`${f("taskkill")} /F /PID ${e}`,{stdio:"ignore"});else process.kill(e,"SIGKILL");return!0}catch{return!1}}async function Le(){if(!await N(500)){try{d(O)}catch{}try{d(T)}catch{}try{d(v)}catch{}return!1}let t=await Et(1500);try{d(O)}catch{}try{d(T)}catch{}try{d(v)}catch{}if(t)process.stderr.write(`[greedysearch] Killed Chrome on port ${c}.
|
|
228
|
+
`);return t}var Ze=Le;async function zt(){let t=Ke()?Yt:Kt;if(t<=0)return!1;if(!C(T))return Gt(),!1;try{let r=Number.parseInt(X(T,"utf8").trim(),10);if(!r)return!1;if((Date.now()-r)/60000>=t)return Le()}catch{}return!1}var m=I;async function we(){let t=(await m(["list"])).split(`
|
|
229
|
+
`)[0];if(!t)throw Error("No Chrome tabs found");return t.slice(0,8)}async function nn(e="about:blank"){let t=await we(),r=new URL(e).hostname;if(r==="copilot.microsoft.com"||r==="www.perplexity.ai"||r==="perplexity.ai"||r.endsWith(".perplexity.ai")){let u=await m(["evalraw",t,"Target.createTarget",JSON.stringify({url:"about:blank"})]),{targetId:s}=JSON.parse(u),o=s.slice(0,8);if(await m(["list"]).catch(()=>null),r==="copilot.microsoft.com")await E(o);else E(o).catch(()=>{});return await m(["list"]).catch(()=>null),s}let i=await m(["evalraw",t,"Target.createTarget",JSON.stringify({url:e})]),{targetId:a}=JSON.parse(i);return await m(["list"]).catch(()=>null),a}async function an(e){try{let t=await we();await m(["evalraw",t,"Target.activateTarget",JSON.stringify({targetId:e})])}catch{}}async function er(e){try{let t=await we();await m(["evalraw",t,"Target.closeTarget",JSON.stringify({targetId:e})])}catch{}}async function on(e=[]){if(await Promise.all(e.filter(Boolean).map((t)=>er(t).catch(()=>{}))),e.length>0)await m(["list"]).catch(()=>null)}function sn(e,t){try{if(!C(re))return null;let n=JSON.parse(X(re,"utf8")).find((i)=>i.url.includes(t[e]));return n?n.targetId:null}catch{return null}}function N(e=3000){return new Promise((t)=>{let r=de.get(`http://localhost:${c}/json/version`,(n)=>{n.resume(),t(n.statusCode===200)});r.on("error",()=>t(!1)),r.setTimeout(e,()=>{r.destroy(),t(!1)})})}async function pe(){let e=`${A}.lock`,t=`${A}.tmp`,r=5000,n=1000,i=await new Promise((a)=>{let u=Date.now(),s=()=>{try{let o=JSON.stringify({pid:process.pid,ts:Date.now()});G(e,o,{encoding:"utf8",flag:"wx"}),a(!0)}catch(o){if(o?.code!=="EEXIST"){if(Date.now()-u<1000)setTimeout(s,50);else a(!1);return}try{let l=X(e,"utf8").trim(),h=l.startsWith("{")?JSON.parse(l):{ts:Number(l)},b=Number(h?.ts)||0;if(b>0&&Date.now()-b>5000)try{d(e)}catch{}if(Date.now()-u<1000)setTimeout(s,50);else a(!1)}catch{if(Date.now()-u<1000)setTimeout(s,50);else a(!1)}}};s()});try{let a=await new Promise((l,h)=>{let b=de.get(`http://localhost:${c}/json/version`,(j)=>{let be="";j.on("data",(Ge)=>be+=Ge),j.on("end",()=>l(be))});b.on("error",h),b.setTimeout(3000,()=>{b.destroy(),h(Error("timeout"))})}),{webSocketDebuggerUrl:u}=JSON.parse(a),s=new URL(u),o=`${s.pathname}${s.search}`;if(i){G(t,`localhost:${c}
|
|
230
|
+
${o}`,"utf8");try{d(A)}catch{}Ht(t,A)}}catch{}finally{if(i)try{d(e)}catch{}}}async function ln(){let e=C(O)||!!U(),t=await ke({activePortFile:A,modeFile:v,excludePorts:e?[c]:[]});if(t){process.stderr.write(`[greedysearch] Reusing existing browser CDP endpoint ${t.httpUrl} (${t.source}).
|
|
231
|
+
`);return}He();let r=await zt(),n=r?!1:await N();if(!n&&!r)await new Promise((s)=>setTimeout(s,500)),n=await N();let i=!1;if(n){let s=Ke(),o=process.env.GREEDY_SEARCH_VISIBLE==="1";if(!o&&!s)process.stderr.write(`[greedysearch] Visible Chrome detected — switching to headless mode...
|
|
232
|
+
`),await Ze(),await new Promise((l)=>setTimeout(l,1000)),i=!0;else if(o&&s)process.stderr.write(`[greedysearch] Headless Chrome detected — switching to visible mode...
|
|
233
|
+
`),await Ze(),await new Promise((l)=>setTimeout(l,1000)),i=!0}if(i?!1:await N()){await pe();try{let s=U();if(s)ce(s),Qe(s)}catch{}return}let u=qe();if(!u.acquired){if(await new Promise((o)=>setTimeout(o,3000)),await N(5000)){await pe();return}}try{if(await N(1000)){await pe();return}process.stderr.write(`GreedySearch Chrome not running on port ${c} — auto-launching...
|
|
234
|
+
`);let o=[Zt()];if(process.env.GREEDY_SEARCH_VISIBLE!=="1")o.push("--headless");await new Promise((l,h)=>{Qt(ye(),o,{stdio:["ignore",process.stderr,process.stderr],env:{...process.env,CDP_PROFILE_DIR:k,GREEDY_SEARCH_PROFILE_DIR:k}}).on("close",(j)=>j===0?l():h(Error("launch.mjs failed")))})}finally{u.release()}}export{Gt as touchActivity,Zt as resolveGreedySearchLaunchScript,pe as refreshPortFile,N as probeGreedyChrome,nn as openNewTab,Ze as killHeadlessChrome,Le as killChrome,Ke as isChromeHeadless,sn as getFullTabFromCache,we as getAnyTab,ln as ensureChrome,Lt as detectHeadlessFromChromeCommandLine,on as closeTabs,er as closeTab,zt as checkAndKillIdle,m as cdp,an as activateTab};
|
|
@@ -1,46 +1,46 @@
|
|
|
1
|
-
import{createRequire as Ie}from"node:module";var Re=Ie(import.meta.url);import{Readability as je}from"@mozilla/readability";import{JSDOM as Ce}from"jsdom";import Le from"turndown";var ee=new Le({headingStyle:"atx",bulletListMarker:"-",codeBlockStyle:"fenced"});ee.addRule("removeDataUrls",{filter:(e)=>e.tagName==="IMG"&&e.getAttribute("src")?.startsWith("data:"),replacement:()=>""});var te="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36",re={"user-agent":te,accept:"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8","accept-language":"en-US,en;q=0.9","accept-encoding":"gzip, deflate, br","cache-control":"no-cache",pragma:"no-cache","sec-ch-ua":'"Chromium";v="122", "Not(A:Brand";v="24", "Google Chrome";v="122"',"sec-ch-ua-mobile":"?0","sec-ch-ua-platform":'"Windows"',"sec-fetch-dest":"document","sec-fetch-mode":"navigate","sec-fetch-site":"none","sec-fetch-user":"?1","upgrade-insecure-requests":"1"},Oe=[/^localhost$/i,/^127\.\d+\.\d+\.\d+$/,/^0\.0\.0\.0$/,/^\[::1\]$/,/^10\./,/^172\.(1[6-9]|2\d|3[01])\./,/^192\.168\./,/^169\.254\./,/^fc00:/i,/^fe80:/i,/\.local$/i,/\.internal$/i,/\.localhost$/i];function
|
|
1
|
+
import{createRequire as Ie}from"node:module";var Re=Ie(import.meta.url);import{Readability as je}from"@mozilla/readability";import{JSDOM as Ce}from"jsdom";import Le from"turndown";var ee=new Le({headingStyle:"atx",bulletListMarker:"-",codeBlockStyle:"fenced"});ee.addRule("removeDataUrls",{filter:(e)=>e.tagName==="IMG"&&e.getAttribute("src")?.startsWith("data:"),replacement:()=>""});var te="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36",re={"user-agent":te,accept:"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8","accept-language":"en-US,en;q=0.9","accept-encoding":"gzip, deflate, br","cache-control":"no-cache",pragma:"no-cache","sec-ch-ua":'"Chromium";v="122", "Not(A:Brand";v="24", "Google Chrome";v="122"',"sec-ch-ua-mobile":"?0","sec-ch-ua-platform":'"Windows"',"sec-fetch-dest":"document","sec-fetch-mode":"navigate","sec-fetch-site":"none","sec-fetch-user":"?1","upgrade-insecure-requests":"1"},Oe=[/^localhost$/i,/^127\.\d+\.\d+\.\d+$/,/^0\.0\.0\.0$/,/^\[::1\]$/,/^10\./,/^172\.(1[6-9]|2\d|3[01])\./,/^192\.168\./,/^169\.254\./,/^fc00:/i,/^fe80:/i,/\.local$/i,/\.internal$/i,/\.localhost$/i];function ne(e={}){return{...re,...e}}function O(e){try{let t=new URL(e),r=t.hostname.toLowerCase();for(let n of Oe)if(n.test(r))return{blocked:!0,reason:`Private/internal address: ${r}`};if(t.protocol==="file:")return{blocked:!0,reason:"File protocol not allowed"};return{blocked:!1}}catch(t){return{blocked:!0,reason:`Invalid URL: ${t.message}`}}}function Te(e){try{let t=new URL(e);if(!(t.hostname==="github.com"||t.hostname.endsWith(".github.com")))return e;let r=t.pathname.split("/").filter(Boolean);if(r.length<5)return e;let[n,i,o,a,...s]=r;if(o!=="blob")return e;let c=s.join("/");return`https://raw.githubusercontent.com/${n}/${i}/${a}/${c}`}catch{return e}}async function ie(e,t={}){let r=O(e);if(r.blocked)return{ok:!1,url:e,finalUrl:e,status:403,error:`Blocked: ${r.reason}`,needsBrowser:!1};let n=e;if(e=Te(e),e!==n)console.error(`[fetcher] Rewrote GitHub URL: ${n.slice(0,60)}... → raw.githubusercontent.com`);let{timeoutMs:i=15000,userAgent:o,signal:a}=t,s=new AbortController,c=setTimeout(()=>s.abort(),i);if(a)a.addEventListener("abort",()=>s.abort(),{once:!0});try{let l=await fetch(e,{method:"GET",headers:{...re,"user-agent":o||te},redirect:"follow",signal:s.signal});clearTimeout(c);let f=l.headers.get("content-type")||"",u=l.url,m=l.headers.get("last-modified")||"",g=!1;try{g=new URL(u).hostname.toLowerCase()==="raw.githubusercontent.com"}catch{}if(f.includes("text/plain")&&g){let L=await l.text();return{ok:!0,url:n,finalUrl:u,status:l.status,title:u.split("/").pop()||"GitHub File",byline:"",siteName:"GitHub",lang:"",publishedTime:m,lastModified:m,markdown:L,contentLength:L.length,excerpt:L.slice(0,300).replaceAll(/\n/g," "),needsBrowser:!1}}if(!f.includes("text/html")&&!f.includes("application/xhtml"))return{ok:!1,url:e,finalUrl:u,status:l.status,error:`Unsupported content type: ${f}`,needsBrowser:!1};let h=await l.text(),y=T(l.status,h,u,e);if(y.blocked)return{ok:!1,url:e,finalUrl:u,status:l.status,error:`Blocked: ${y.reason}`,needsBrowser:!0};let p=F(h,u),_=x(p);if(!_.ok)return{ok:!1,url:e,finalUrl:u,status:l.status,error:`Low quality content: ${_.reason}`,needsBrowser:!0};return{ok:!0,url:e,finalUrl:u,status:l.status,title:p.title,byline:p.byline,siteName:p.siteName,lang:p.lang,publishedTime:p.publishedTime||m,lastModified:m,markdown:p.markdown,excerpt:p.excerpt,contentLength:p.markdown.length,needsBrowser:!1}}catch(l){clearTimeout(c);let f=Me(l);return{ok:!1,url:e,finalUrl:e,status:0,error:l.message,needsBrowser:f}}}function T(e,t,r,n){let i=t.match(/<title[^>]*>([^<]*)<\/title>/i)?.[1]?.toLowerCase()||"",o=t.slice(0,30000).toLowerCase(),a=`${i} ${o}`;if(e===403||e===429||e===503)return{blocked:!0,reason:`HTTP ${e}`};let s=[{pattern:/class=["'][^"']*captcha["']|<div[^>]*id=["']captcha/i,reason:"captcha"},{pattern:/g-recaptcha|data-sitekey|i['"]m not a robot/i,reason:"captcha"},{pattern:/checking your browser.{0,100}please wait|cf-browser-verification/i,reason:"cloudflare challenge"},{pattern:/just a moment.{0,50}security check|ddos protection by cloudflare/i,reason:"cloudflare challenge"},{pattern:/unusual traffic.{0,50}from your computer network/i,reason:"unusual traffic"},{pattern:/bot detected|automated.{0,20}request/i,reason:"bot detection"},{pattern:/enable\s+javascript\s+to\s+view|javascript\s+is\s+required.{0,50}enabled/i,reason:"requires javascript"},{pattern:/access denied|accessdenied/i,reason:"access denied"},{pattern:/protected by anubis|anubis uses a proof-of-work/i,reason:"anubis challenge"}];for(let l of s)if(l.pattern.test(a))return{blocked:!0,reason:l.reason};let c=We(n,r,t);if(c)return{blocked:!0,reason:c};return{blocked:!1}}var Fe=["accounts.google.com","login.microsoftonline.com","login.live.com","auth0.com","okta.com","auth.mozilla.auth0.com","id.atlassian.com"],xe=["login.","signin.","auth.","sso.","accounts.","idp."],Ge=["sign in to continue","log in to continue","authentication required","create an account to continue","subscribe to continue reading","members only"];function We(e,t,r){try{let n=new URL(e),i=new URL(t);if(n.hostname.toLowerCase()===i.hostname.toLowerCase())return;let o=i.hostname.toLowerCase();if(Fe.some((s)=>o===s||o.endsWith(`.${s}`)))return`redirected to login (${i.hostname})`;if(xe.some((s)=>o.startsWith(s)))return`redirected to login (${i.hostname})`;let a=r.slice(0,20000).toLowerCase();if(Ge.some((s)=>a.includes(s)))return`redirected to login page (${i.hostname})`}catch{}return}function Me(e){let t=e.message.toLowerCase();return t.includes("fetch failed")||t.includes("unable to verify")||t.includes("certificate")||t.includes("timeout")}function z(e){let t=['meta[property="article:published_time"]','meta[name="article:published_time"]','meta[property="og:published_time"]','meta[name="publication_date"]','meta[name="date"]','meta[itemprop="datePublished"]','time[itemprop="datePublished"]','meta[name="DC.date"]'];for(let r of t){let n=e.querySelector(r),i=n?.getAttribute("content")||n?.getAttribute("datetime")||"";if(i)return i}return""}function F(e,t){let r=new Ce(e,{url:t}).window.document,n=new je(r).parse();if(n&&n.content){let o=ee.turndown(n.content).replaceAll(/\n{3,}/g,`
|
|
2
2
|
|
|
3
|
-
`).trim(),a=
|
|
3
|
+
`).trim(),a=n.publishedTime||z(r)||"";return{title:n.title||r.title||t,byline:n.byline||"",siteName:n.siteName||"",lang:n.lang||"",publishedTime:a,markdown:o,excerpt:o.slice(0,300).replaceAll(/\n/g," ")}}let i=r.body;if(i){let o=i.cloneNode(!0);o.querySelectorAll("script, style, nav, footer, header, aside").forEach((s)=>s.remove());let a=(o.textContent||"").replaceAll(/\s+/g," ").trim();return{title:r.title||t,byline:"",siteName:"",lang:"",publishedTime:z(r),markdown:a,excerpt:a.slice(0,300)}}return{title:t,byline:"",siteName:"",lang:"",publishedTime:"",markdown:"",excerpt:""}}function x(e){let t=e.markdown.trim().toLowerCase(),r=(e.title||"").toLowerCase();if(e.markdown.trim().length<100)return{ok:!1,reason:"content too short (< 100 chars)"};let n=t.toLowerCase(),i=[{check:()=>n.includes("loading")&&n.includes("please wait"),desc:"loading page"},{check:()=>n.includes("please ensure javascript is enabled"),desc:"requires javascript"},{check:()=>n.includes("enable javascript to view"),desc:"requires javascript"},{check:()=>n.includes("just a moment"),desc:"cloudflare challenge detected in content"},{check:()=>n.includes("verify you are human"),desc:"human verification"},{check:()=>n.includes("captcha required"),desc:"captcha in extracted content"},{check:()=>n.includes("access denied"),desc:"access denied in content"},{check:()=>/^\s{0,10}sign\s{1,5}in\s{0,10}$|^\s{0,10}log\s{1,5}in\s{0,10}$/im.test(t),desc:"login form only"}];for(let{check:o,desc:a}of i)if(o())return{ok:!1,reason:a};if(r.includes("just a moment")||r.includes("checking your browser"))return{ok:!1,reason:"cloudflare challenge page detected in title"};return{ok:!0}}var ae={"user-agent":"GreedySearch/1.0",accept:"application/vnd.github+json","x-github-api-version":"2022-11-28"};function D(e){try{let t=new URL(e);if(!(t.hostname==="github.com"||t.hostname.endsWith(".github.com")))return null;let r=t.pathname.split("/").filter(Boolean);if(r.length<2)return null;let[n,i]=r;if(r.length===2)return{owner:n,repo:i,type:"root"};if(r.length>=4&&(r[2]==="blob"||r[2]==="tree")){let o=r[2],a=r[3],s=r.slice(4).join("/");return{owner:n,repo:i,type:o,ref:a,path:s}}return null}catch{return null}}async function $(e,t=1e4){let r=new AbortController,n=setTimeout(()=>r.abort(),t);try{let i=await fetch(`https://api.github.com${e}`,{headers:ae,signal:r.signal});if(clearTimeout(n),!i.ok)throw Error(`GitHub API ${i.status}: ${e}`);return await i.json()}catch(i){throw clearTimeout(n),i}}async function Ue(e,t){try{let r=await $(`/repos/${e}/${t}/readme`);if(r.content&&r.encoding==="base64")return Buffer.from(r.content,"base64").toString("utf8");return""}catch{return""}}async function oe(e,t,r="HEAD",n=""){try{let i=await $(`/repos/${e}/${t}/git/ref/heads/${r==="HEAD"?"main":r}`).catch(()=>$(`/repos/${e}/${t}/git/ref/heads/master`).catch(()=>null)),o;if(i?.object?.sha)o=(await $(`/repos/${e}/${t}/git/commits/${i.object.sha}`)).tree.sha;else{let s=await $(`/repos/${e}/${t}`);o=(await $(`/repos/${e}/${t}/branches/${s.default_branch}`)).commit.commit.tree.sha}let a=(await $(`/repos/${e}/${t}/git/trees/${o}`)).tree||[];if(n)a=a.filter((s)=>s.path.startsWith(n));return a.slice(0,50).map((s)=>({path:s.path,type:s.type==="tree"?"dir":"file",size:s.size}))}catch{return[]}}async function He(e,t,r,n,i=1e4){let o=[`https://raw.githubusercontent.com/${e}/${t}/${r&&r!=="HEAD"?r:"main"}/${n}`,`https://raw.githubusercontent.com/${e}/${t}/master/${n}`];for(let a of o){let s=new AbortController,c=setTimeout(()=>s.abort(),i);try{let l=await fetch(a,{headers:{"user-agent":ae["user-agent"]},signal:s.signal});if(clearTimeout(c),l.ok)return await l.text()}catch{clearTimeout(c)}}return null}async function se(e){let t=D(e);if(!t)return{ok:!1,error:"Not a valid GitHub URL"};let{owner:r,repo:n,type:i,ref:o,path:a}=t;try{if(i==="root"||i==="tree"&&!a){let[s,c,l]=await Promise.allSettled([$(`/repos/${r}/${n}`),Ue(r,n),oe(r,n,o||"HEAD")]);if(s.status==="rejected")return{ok:!1,error:s.reason?.message||"Repo not found"};let f=s.value,u=c.status==="fulfilled"?c.value:"",m=l.status==="fulfilled"?l.value:[],g=f?.description?`
|
|
4
4
|
|
|
5
|
-
> ${f.description}`:"",h=f?.stargazers_count==null?"":` ⭐ ${f.stargazers_count}`,y=f?.language?` · ${f.language}`:"",p=`# ${r}/${
|
|
5
|
+
> ${f.description}`:"",h=f?.stargazers_count==null?"":` ⭐ ${f.stargazers_count}`,y=f?.language?` · ${f.language}`:"",p=`# ${r}/${n}${h}${y}${g}
|
|
6
6
|
|
|
7
7
|
`;if(u)p+=u.slice(0,6000);else p+=`[No README found]
|
|
8
8
|
|
|
9
9
|
Files:
|
|
10
10
|
${m.map((_)=>` ${_.type==="dir"?"\uD83D\uDCC1":"\uD83D\uDCC4"} ${_.path}`).join(`
|
|
11
|
-
`)}`;return{ok:!0,title:`${r}/${
|
|
12
|
-
`);return{ok:!0,title:`${r}/${
|
|
11
|
+
`)}`;return{ok:!0,title:`${r}/${n}`,content:p,tree:m.slice(0,30)}}if(i==="blob"&&a){let s=await He(r,n,o,a);if(s===null)return{ok:!1,error:`File not found: ${a}`};return{ok:!0,title:`${r}/${n}: ${a}`,content:s}}if(i==="tree"&&a){let s=await oe(r,n,o||"HEAD",a),c=s.map((l)=>` ${l.type==="dir"?"\uD83D\uDCC1":"\uD83D\uDCC4"} ${l.path}`).join(`
|
|
12
|
+
`);return{ok:!0,title:`${r}/${n}/${a}`,content:`[Directory: ${a}]
|
|
13
13
|
|
|
14
14
|
Files:
|
|
15
|
-
${c}`,tree:s}}return{ok:!1,error:"Unsupported GitHub URL type"}}catch(s){return{ok:!1,error:s.message}}}var Je={"user-agent":"GreedySearch/1.0 (Research Bot)",accept:"application/json"};function ce(e){try{let t=new URL(e),r=t.hostname.toLowerCase();if(!(r==="reddit.com"||r.endsWith(".reddit.com")))return null;let
|
|
15
|
+
${c}`,tree:s}}return{ok:!1,error:"Unsupported GitHub URL type"}}catch(s){return{ok:!1,error:s.message}}}var Je={"user-agent":"GreedySearch/1.0 (Research Bot)",accept:"application/json"};function ce(e){try{let t=new URL(e),r=t.hostname.toLowerCase();if(!(r==="reddit.com"||r.endsWith(".reddit.com")))return null;let n=t.pathname;if(n.match(/^\/(u|user)\/[^/]+\/?$/i))return{type:"user",cleanUrl:le(e)};if(n.match(/^\/r\/[^/]+\/comments\/[^/]+/i))return{type:"post",cleanUrl:le(e)};return null}catch{return null}}function le(e){try{let t=new URL(e);return`${t.protocol}//${t.hostname}${t.pathname}`}catch{return e}}async function ue(e,t=8000){let r=Date.now();try{let n=e.replaceAll(/\/?$/g,".json"),i=new AbortController,o=setTimeout(()=>i.abort(),15000),a=await fetch(n,{headers:Je,signal:i.signal});if(clearTimeout(o),!a.ok)throw Error(`Reddit API ${a.status}`);let s=await a.json();if(!Array.isArray(s)||s.length<1)throw Error("Invalid Reddit API response structure");let c=s[0],l=s[1],f=c?.data?.children?.[0]?.data;if(!f)throw Error("No post data in Reddit response");let u=Be(f,l,t);return{ok:!0,url:e,finalUrl:e,status:200,contentType:"text/markdown",lastModified:"",title:f.title||"Reddit Post",byline:`u/${f.author}`,siteName:`r/${f.subreddit}`,lang:"en",publishedTime:new Date(f.created_utc*1000).toISOString(),excerpt:f.selftext?.slice(0,300).replace(/\n/g," ")||"",markdown:u,contentLength:u.length,needsBrowser:!1,duration:Date.now()-r}}catch(n){return{ok:!1,url:e,finalUrl:e,status:0,error:`Reddit fetch failed: ${n.message}`,needsBrowser:!1,duration:Date.now()-r}}}function Be(e,t,r){let n="";if(n+=`# ${e.title}
|
|
16
16
|
|
|
17
|
-
`,
|
|
17
|
+
`,n+=`**Subreddit:** r/${e.subreddit} | **Author:** u/${e.author} | **Score:** ${e.score}
|
|
18
18
|
|
|
19
|
-
`,e.selftext)
|
|
19
|
+
`,e.selftext)n+=e.selftext,n+=`
|
|
20
20
|
|
|
21
|
-
`;else if(e.url)try{let
|
|
21
|
+
`;else if(e.url)try{let i=new URL(e.url).hostname.toLowerCase();if(i!=="reddit.com"&&!i.endsWith(".reddit.com"))n+=`**Link:** ${e.url}
|
|
22
22
|
|
|
23
|
-
`}catch{
|
|
23
|
+
`}catch{n+=`**Link:** ${e.url}
|
|
24
24
|
|
|
25
|
-
`}if(t?.data?.children?.length>0){
|
|
25
|
+
`}if(t?.data?.children?.length>0){n+=`---
|
|
26
26
|
|
|
27
27
|
## Comments
|
|
28
28
|
|
|
29
|
-
`;let
|
|
30
|
-
`}if(
|
|
29
|
+
`;let i=t.data.children.filter((o)=>o.kind==="t1").slice(0,10);for(let o of i)n+=fe(o.data,0),n+=`
|
|
30
|
+
`}if(n.length>r)n=n.slice(0,r).trim()+`
|
|
31
31
|
|
|
32
|
-
... (truncated)`;return
|
|
33
|
-
`,
|
|
32
|
+
... (truncated)`;return n}function fe(e,t){if(!e||e.body==="[deleted]"||e.body==="[removed]")return"";let r="> ".repeat(t),n="";if(n+=`${r}**u/${e.author}** (${e.score} pts)
|
|
33
|
+
`,n+=`${r}${e.body.replaceAll(`
|
|
34
34
|
`,`
|
|
35
35
|
`+r)}
|
|
36
|
-
`,t<3&&e.replies?.data?.children){let
|
|
37
|
-
`+fe(o.data,t+1)}return
|
|
36
|
+
`,t<3&&e.replies?.data?.children){let i=e.replies.data.children.filter((o)=>o.kind==="t1");for(let o of i.slice(0,5))n+=`
|
|
37
|
+
`+fe(o.data,t+1)}return n}function P(e,t=8000){if(!e||e.length<=t)return e;let r=`
|
|
38
38
|
|
|
39
39
|
[...content trimmed...]
|
|
40
40
|
|
|
41
|
-
`,
|
|
42
|
-
`)a--;if(a<=
|
|
43
|
-
`)s++;if(s>=e.length-o+100)s=e.length-o;let c=e.slice(0,a).trimEnd(),l=e.slice(s).trimStart();return`${c}${r}${l}`}import{platform as Ft,tmpdir as Ye}from"node:os";import{spawn as Ve}from"node:child_process";import{dirname as qe,join as Ke}from"node:path";import{fileURLToPath as Xe}from"node:url";import{basename as Ze}from"node:path";import{existsSync as S,mkdirSync as rt,readFileSync as he,writeFileSync as
|
|
41
|
+
`,n=t-r.length,i=Math.floor(n*0.75),o=n-i,a=i;while(a>i-100&&e[a]!==`
|
|
42
|
+
`)a--;if(a<=i-100)a=i;let s=e.length-o;while(s<e.length-o+100&&e[s]!==`
|
|
43
|
+
`)s++;if(s>=e.length-o+100)s=e.length-o;let c=e.slice(0,a).trimEnd(),l=e.slice(s).trimStart();return`${c}${r}${l}`}import{platform as Ft,tmpdir as Ye}from"node:os";import{spawn as Ve}from"node:child_process";import{dirname as qe,join as Ke}from"node:path";import{fileURLToPath as Xe}from"node:url";import{basename as Ze}from"node:path";import{existsSync as S,mkdirSync as rt,readFileSync as he,writeFileSync as nt}from"node:fs";import{homedir as it}from"node:os";import{join as ye}from"node:path";import{tmpdir as k}from"node:os";import{platform as er,tmpdir as lt}from"node:os";import{existsSync as N,mkdirSync as ct,readFileSync as ve,writeFileSync as ut}from"node:fs";import{homedir as ft}from"node:os";import{join as be}from"node:path";import{tmpdir as A}from"node:os";function Qe(e=process.env,t=process.execPath){let r=e.GREEDY_SEARCH_NODE||e.NODE_BINARY||e.NODE;if(r?.trim())return r.trim();let n=Ze(t||"").toLowerCase();if(n==="node"||n==="node.exe")return t;return"node"}var ze=qe(Xe(import.meta.url)),et=Ke(ze,"..","bin","cdp.mjs");function me(e,t=30000){return tt(e,null,t)}function tt(e,t=null,r=30000){return new Promise((n,i)=>{let o=Ve(Qe(),[et,...e],{stdio:[t==null?"ignore":"pipe","pipe","pipe"]});if(t!=null)o.stdin.write(t),o.stdin.end();let a="",s="";o.stdout.on("data",(l)=>a+=l),o.stderr.on("data",(l)=>s+=l);let c=setTimeout(()=>{o.kill(),i(Error(`cdp timeout: ${e[0]}`))},r);o.on("close",(l)=>{if(clearTimeout(c),l===0)n(a.trim());else i(Error(s.trim()||`cdp exit ${l}`))})})}async function pe(e){await me(["evalraw",e,"Page.addScriptToEvaluateOnNewDocument",JSON.stringify({source:`
|
|
44
44
|
(function() {
|
|
45
45
|
// ── Runtime.enable / CDP detection masking ──────────────
|
|
46
46
|
try { delete window.__REBROWSER_RUNTIME_ENABLE; } catch(_) {}
|
|
@@ -248,35 +248,35 @@ ${c}`,tree:s}}return{ok:!1,error:"Unsupported GitHub URL type"}}catch(s){return{
|
|
|
248
248
|
};
|
|
249
249
|
} catch(_) {}
|
|
250
250
|
})();
|
|
251
|
-
`})])}var we=`${k().replaceAll("\\","/")}/greedysearch-chrome-profile`,Yt=`${we}/DevToolsActivePort`,Vt=`${k().replaceAll("\\","/")}/cdp-pages.json`,qt=`${k().replaceAll("\\","/")}/greedysearch-chrome-mode`,Kt=`${k().replaceAll("\\","/")}/greedysearch-visible-recovery.jsonl`,M=ye(
|
|
251
|
+
`})])}var we=`${k().replaceAll("\\","/")}/greedysearch-chrome-profile`,Yt=`${we}/DevToolsActivePort`,Vt=`${k().replaceAll("\\","/")}/cdp-pages.json`,qt=`${k().replaceAll("\\","/")}/greedysearch-chrome-mode`,Kt=`${k().replaceAll("\\","/")}/greedysearch-visible-recovery.jsonl`,M=ye(it(),".dm"),w=ye(M,"greedyconfig"),U=["perplexity","google","chatgpt"],H="gemini";function ot(){try{if(S(w)){let e=he(w,"utf8"),t=JSON.parse(e);if(Array.isArray(t.engines)&&t.engines.length>0&&t.engines.every((r)=>typeof r==="string")){let r=t.engines.filter((i)=>G[i]),n=t.engines.filter((i)=>!G[i]);if(n.length>0)process.stderr.write(`[greedysearch] Warning: ignoring unknown engine(s) in ${w}: ${n.join(", ")}
|
|
252
252
|
[greedysearch] Available engines: ${Object.keys(G).join(", ")}
|
|
253
253
|
`);if(r.length>0)return r;process.stderr.write(`[greedysearch] Warning: no valid engines in ${w}, falling back to defaults: ${U.join(", ")}
|
|
254
|
-
`)}}}catch{}return U}function at(){try{if(!S(M))rt(M,{recursive:!0});if(!S(w))
|
|
254
|
+
`)}}}catch{}return U}function at(){try{if(!S(M))rt(M,{recursive:!0});if(!S(w))nt(w,JSON.stringify({engines:U,synthesizer:H},null,2)+`
|
|
255
255
|
`,"utf8")}catch{}}at();var ge=["gemini","chatgpt"];function st(){try{if(S(w)){let e=he(w,"utf8"),t=JSON.parse(e);if(typeof t.synthesizer==="string"){let r=t.synthesizer.toLowerCase();if(ge.includes(r))return r;process.stderr.write(`[greedysearch] Warning: unknown synthesizer "${t.synthesizer}" in ${w}
|
|
256
256
|
[greedysearch] Available synthesizers: ${ge.join(", ")}
|
|
257
257
|
[greedysearch] Falling back to default: ${H}
|
|
258
|
-
`)}}}catch{}return H}var G={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"},Xt=ot(),Zt=st(),Qt=Math.max(1,Number.parseInt(process.env.GREEDY_FETCH_CONCURRENCY||"5",10)||5);process.env.CDP_PROFILE_DIR=we;var $e=`${A().replaceAll("\\","/")}/greedysearch-chrome-profile`,or=`${$e}/DevToolsActivePort`,ar=`${A().replaceAll("\\","/")}/cdp-pages.json`,sr=`${A().replaceAll("\\","/")}/greedysearch-chrome-mode`,lr=`${A().replaceAll("\\","/")}/greedysearch-visible-recovery.jsonl`,J=be(ft(),".dm"),v=be(J,"greedyconfig"),B=["perplexity","google","chatgpt"],Y="gemini";function pt(){try{if(N(v)){let e=ve(v,"utf8"),t=JSON.parse(e);if(Array.isArray(t.engines)&&t.engines.length>0&&t.engines.every((r)=>typeof r==="string")){let r=t.engines.filter((
|
|
258
|
+
`)}}}catch{}return H}var G={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"},Xt=ot(),Zt=st(),Qt=Math.max(1,Number.parseInt(process.env.GREEDY_FETCH_CONCURRENCY||"5",10)||5);process.env.CDP_PROFILE_DIR=we;var $e=`${A().replaceAll("\\","/")}/greedysearch-chrome-profile`,or=`${$e}/DevToolsActivePort`,ar=`${A().replaceAll("\\","/")}/cdp-pages.json`,sr=`${A().replaceAll("\\","/")}/greedysearch-chrome-mode`,lr=`${A().replaceAll("\\","/")}/greedysearch-visible-recovery.jsonl`,J=be(ft(),".dm"),v=be(J,"greedyconfig"),B=["perplexity","google","chatgpt"],Y="gemini";function pt(){try{if(N(v)){let e=ve(v,"utf8"),t=JSON.parse(e);if(Array.isArray(t.engines)&&t.engines.length>0&&t.engines.every((r)=>typeof r==="string")){let r=t.engines.filter((i)=>W[i]),n=t.engines.filter((i)=>!W[i]);if(n.length>0)process.stderr.write(`[greedysearch] Warning: ignoring unknown engine(s) in ${v}: ${n.join(", ")}
|
|
259
259
|
[greedysearch] Available engines: ${Object.keys(W).join(", ")}
|
|
260
260
|
`);if(r.length>0)return r;process.stderr.write(`[greedysearch] Warning: no valid engines in ${v}, falling back to defaults: ${B.join(", ")}
|
|
261
261
|
`)}}}catch{}return B}function gt(){try{if(!N(J))ct(J,{recursive:!0});if(!N(v))ut(v,JSON.stringify({engines:B,synthesizer:Y},null,2)+`
|
|
262
262
|
`,"utf8")}catch{}}gt();var de=["gemini","chatgpt"];function dt(){try{if(N(v)){let e=ve(v,"utf8"),t=JSON.parse(e);if(typeof t.synthesizer==="string"){let r=t.synthesizer.toLowerCase();if(de.includes(r))return r;process.stderr.write(`[greedysearch] Warning: unknown synthesizer "${t.synthesizer}" in ${v}
|
|
263
263
|
[greedysearch] Available synthesizers: ${de.join(", ")}
|
|
264
264
|
[greedysearch] Falling back to default: ${Y}
|
|
265
|
-
`)}}}catch{}return Y}var W={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"},cr=pt(),ur=dt(),fr=Math.max(1,Number.parseInt(process.env.GREEDY_FETCH_CONCURRENCY||"5",10)||5);process.env.CDP_PROFILE_DIR=$e;var E=lt().replaceAll("\\","/"),pr=`${E}/greedysearch-chrome-metadata.json`,gr=`${E}/greedysearch-chrome-launch.lock`;var dr=`${E}/greedysearch-chrome.pid`,mr=`${E}/greedysearch-chrome-mode`,hr=`${E}/greedysearch-chrome-last-activity`;var yr=Number.parseInt(process.env.GREEDY_SEARCH_IDLE_TIMEOUT_MINUTES||"5",10)||5;var wr=import.meta.dirname||new URL(".",import.meta.url).pathname.replace(/^\/([A-Z]:)/,"$1")
|
|
266
|
-
`)[0];if(!e)throw Error("No Chrome tabs found");return e.slice(0,8)}async function I(e="about:blank"){let t=await Pe(),r=new URL(e).hostname;if(r==="copilot.microsoft.com"||r==="www.perplexity.ai"||r==="perplexity.ai"||r.endsWith(".perplexity.ai")){let o=await d(["evalraw",t,"Target.createTarget",JSON.stringify({url:"about:blank"})]),{targetId:a}=JSON.parse(o),s=a.slice(0,8);if(await d(["list"]).catch(()=>null),r==="copilot.microsoft.com")await pe(s);else pe(s).catch(()=>{});return await d(["list"]).catch(()=>null),a}let
|
|
265
|
+
`)}}}catch{}return Y}var W={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"},cr=pt(),ur=dt(),fr=Math.max(1,Number.parseInt(process.env.GREEDY_FETCH_CONCURRENCY||"5",10)||5);process.env.CDP_PROFILE_DIR=$e;var E=lt().replaceAll("\\","/"),pr=`${E}/greedysearch-chrome-metadata.json`,gr=`${E}/greedysearch-chrome-launch.lock`;var dr=`${E}/greedysearch-chrome.pid`,mr=`${E}/greedysearch-chrome-mode`,hr=`${E}/greedysearch-chrome-last-activity`;var yr=Number.parseInt(process.env.GREEDY_SEARCH_IDLE_TIMEOUT_MINUTES||"5",10)||5;var wr=import.meta.dirname||new URL(".",import.meta.url).pathname.replace(/^\/([A-Z]:)/,"$1");var _e=Ye().replaceAll("\\","/"),vr=`${_e}/greedysearch-chrome.pid`,br=`${_e}/greedysearch-chrome-last-activity`,$r=Number.parseInt(process.env.GREEDY_SEARCH_IDLE_TIMEOUT_MINUTES||"5",10)||5,_r=Number.parseInt(process.env.GREEDY_SEARCH_VISIBLE_IDLE_TIMEOUT_MINUTES||"60",10)||60;var d=me;async function Pe(){let e=(await d(["list"])).split(`
|
|
266
|
+
`)[0];if(!e)throw Error("No Chrome tabs found");return e.slice(0,8)}async function I(e="about:blank"){let t=await Pe(),r=new URL(e).hostname;if(r==="copilot.microsoft.com"||r==="www.perplexity.ai"||r==="perplexity.ai"||r.endsWith(".perplexity.ai")){let o=await d(["evalraw",t,"Target.createTarget",JSON.stringify({url:"about:blank"})]),{targetId:a}=JSON.parse(o),s=a.slice(0,8);if(await d(["list"]).catch(()=>null),r==="copilot.microsoft.com")await pe(s);else pe(s).catch(()=>{});return await d(["list"]).catch(()=>null),a}let n=await d(["evalraw",t,"Target.createTarget",JSON.stringify({url:e})]),{targetId:i}=JSON.parse(n);return await d(["list"]).catch(()=>null),i}async function R(e){try{let t=await Pe();await d(["evalraw",t,"Target.closeTarget",JSON.stringify({targetId:e})])}catch{}}import{existsSync as j,mkdirSync as mt,readFileSync as De,writeFileSync as ht}from"node:fs";import{homedir as yt}from"node:os";import{join as Se}from"node:path";import{tmpdir as C}from"node:os";var Ne=`${C().replaceAll("\\","/")}/greedysearch-chrome-profile`,kr=`${Ne}/DevToolsActivePort`,Ar=`${C().replaceAll("\\","/")}/cdp-pages.json`,Ir=`${C().replaceAll("\\","/")}/greedysearch-chrome-mode`,Rr=`${C().replaceAll("\\","/")}/greedysearch-visible-recovery.jsonl`,q=Se(yt(),".dm"),b=Se(q,"greedyconfig"),K=["perplexity","google","chatgpt"],X="gemini";function wt(){try{if(j(b)){let e=De(b,"utf8"),t=JSON.parse(e);if(Array.isArray(t.engines)&&t.engines.length>0&&t.engines.every((r)=>typeof r==="string")){let r=t.engines.filter((i)=>V[i]),n=t.engines.filter((i)=>!V[i]);if(n.length>0)process.stderr.write(`[greedysearch] Warning: ignoring unknown engine(s) in ${b}: ${n.join(", ")}
|
|
267
267
|
[greedysearch] Available engines: ${Object.keys(V).join(", ")}
|
|
268
268
|
`);if(r.length>0)return r;process.stderr.write(`[greedysearch] Warning: no valid engines in ${b}, falling back to defaults: ${K.join(", ")}
|
|
269
269
|
`)}}}catch{}return K}function vt(){try{if(!j(q))mt(q,{recursive:!0});if(!j(b))ht(b,JSON.stringify({engines:K,synthesizer:X},null,2)+`
|
|
270
270
|
`,"utf8")}catch{}}vt();var Ee=["gemini","chatgpt"];function bt(){try{if(j(b)){let e=De(b,"utf8"),t=JSON.parse(e);if(typeof t.synthesizer==="string"){let r=t.synthesizer.toLowerCase();if(Ee.includes(r))return r;process.stderr.write(`[greedysearch] Warning: unknown synthesizer "${t.synthesizer}" in ${b}
|
|
271
271
|
[greedysearch] Available synthesizers: ${Ee.join(", ")}
|
|
272
272
|
[greedysearch] Falling back to default: ${X}
|
|
273
|
-
`)}}}catch{}return X}var V={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"},jr=wt();var Cr=bt(),Z=Math.max(1,Number.parseInt(process.env.GREEDY_FETCH_CONCURRENCY||"5",10)||5);process.env.CDP_PROFILE_DIR=Ne;function $t(){if(typeof globalThis.DOMMatrix>"u")globalThis.DOMMatrix=class{constructor(t=void 0){}multiplySelf(){return this}preMultiplySelf(){return this}translateSelf(){return this}scaleSelf(){return this}rotateSelf(){return this}};if(typeof globalThis.ImageData>"u")globalThis.ImageData=class{constructor(t=void 0,r=0,
|
|
273
|
+
`)}}}catch{}return X}var V={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"},jr=wt();var Cr=bt(),Z=Math.max(1,Number.parseInt(process.env.GREEDY_FETCH_CONCURRENCY||"5",10)||5);process.env.CDP_PROFILE_DIR=Ne;function $t(){if(typeof globalThis.DOMMatrix>"u")globalThis.DOMMatrix=class{constructor(t=void 0){}multiplySelf(){return this}preMultiplySelf(){return this}translateSelf(){return this}scaleSelf(){return this}rotateSelf(){return this}};if(typeof globalThis.ImageData>"u")globalThis.ImageData=class{constructor(t=void 0,r=0,n=0){this.data=t,this.width=r,this.height=n}};if(typeof globalThis.Path2D>"u")globalThis.Path2D=class{constructor(t=void 0){}}}async function _t(){$t();let e=await import("pdf-parse"),t=e.PDFParse??e.default;if(!t)throw Error("pdf-parse did not export PDFParse");return t}async function ke(e,t){try{let n=new(await _t())({data:new Uint8Array(e)});await n.load();let i=await n.getText(),o=i.text?.trim();if(!o)return null;return{title:new URL(t).pathname.split("/").pop()||"Document.pdf",content:`## PDF Content (${i.total} pages)
|
|
274
274
|
|
|
275
|
-
${o}`,pages:
|
|
275
|
+
${o}`,pages:i.total}}catch(r){return{error:r.message||String(r)}}}function Q(e="",t=240){let r=String(e).replaceAll(/\s+/g," ").trim();if(r.length<=t)return r;let n=r.slice(0,t),i=n.lastIndexOf(" ");return i>0?`${n.slice(0,i)}...`:`${n}...`}async function Pt(e,t,r=8000){let n=Date.now();try{let o=(await d(["evalraw",e,"Page.getFrameTree","{}"]).then((h)=>JSON.parse(h)).catch(()=>null))?.frameTree?.frame?.id||void 0,a=await d(["evalraw",e,"Network.loadNetworkResource",JSON.stringify({frameId:o,url:t,options:{disableCache:!0,includeCredentials:!1}})],20000),c=JSON.parse(a).resource;if(!c?.success||!c.httpStatusCode)return{url:t,error:c?.netErrorName||c?.netError||"loadNetworkResource failed",source:"chrome",duration:Date.now()-n,needsFallback:!0};let l="";if(c.stream)try{let h=await d(["evalraw",e,"IO.read",JSON.stringify({handle:c.stream})],1e4);l=JSON.parse(h).data||"",await d(["evalraw",e,"IO.close",JSON.stringify({handle:c.stream})]).catch(()=>{})}catch{}if(!l||l.length<100)return{url:t,error:"Empty response body from Network.loadNetworkResource",source:"chrome",duration:Date.now()-n,needsFallback:!0};let f=T(c.httpStatusCode,l,t,t);if(f.blocked)return{url:t,status:c.httpStatusCode,error:`Blocked: ${f.reason}`,source:"chrome",duration:Date.now()-n,needsBrowser:!0};let u=F(l,t),m=x(u);if(!m.ok)return{url:t,status:c.httpStatusCode,error:`Low quality: ${m.reason}`,source:"chrome",duration:Date.now()-n,needsBrowser:!0};let g=P(u.markdown,r);return{url:t,finalUrl:t,status:c.httpStatusCode,contentType:"text/markdown",lastModified:"",publishedTime:u.publishedTime||"",byline:u.byline||"",siteName:u.siteName||"",lang:u.lang||"",title:u.title||t,snippet:u.excerpt,content:g,contentChars:g.length,source:"chrome",duration:Date.now()-n}}catch(i){return{url:t,error:i.message,source:"chrome",duration:Date.now()-n,needsFallback:!0}}}function Ae(e){try{return new URL(e).pathname.toLowerCase().endsWith(".pdf")}catch{return!1}}async function Et(e,t=8000){let r=O(e);if(r.blocked)return{url:e,finalUrl:e,status:403,error:`Blocked: ${r.reason}`,source:"pdf-http"};let n=new AbortController,i=setTimeout(()=>n.abort(),20000),o=Date.now();try{let a=await fetch(e,{method:"GET",redirect:"follow",signal:n.signal,headers:ne({accept:"application/pdf,application/octet-stream;q=0.9,*/*;q=0.5"})});clearTimeout(i);let s=a.headers.get("content-type")||"",c=a.url||e,l=Number.parseInt(a.headers.get("content-length")||"0",10);if(a.status>=400)return{url:e,finalUrl:c,status:a.status,error:`HTTP ${a.status}`,source:"pdf-http",duration:Date.now()-o};if(!s.toLowerCase().includes("application/pdf")&&!Ae(c))return null;if(l>31457280)return{url:e,finalUrl:c,status:a.status,error:`PDF too large: ${l} bytes`,source:"pdf-http",duration:Date.now()-o};let f=Buffer.from(await a.arrayBuffer()),u=await ke(f,c);if(!u||u.error)return{url:e,finalUrl:c,status:a.status,error:u?.error||"PDF text extraction failed",source:"pdf-http",duration:Date.now()-o};let m=P(u.content,t);return{url:e,finalUrl:c,status:a.status,contentType:"application/pdf",lastModified:a.headers.get("last-modified")||"",title:u.title,snippet:Q(m,320),content:m,contentChars:m.length,pages:u.pages,source:"pdf-http",duration:Date.now()-o}}catch(a){return clearTimeout(i),{url:e,finalUrl:e,error:a.message||String(a),source:"pdf-http",duration:Date.now()-o}}}async function Dt(e,t=8000){let r=Date.now();if(Ae(e)){let o=await Et(e,t);if(o?.content||o?.status===403)return o}if(D(e)){let o=D(e);if(o&&(o.type==="root"||o.type==="tree"||o.type==="blob"&&!o.path?.includes("."))){let a=await se(e);if(a.ok){let s=P(a.content,t);return{url:e,finalUrl:e,status:200,contentType:"text/markdown",lastModified:"",title:a.title,snippet:s.slice(0,320),content:s,contentChars:s.length,source:"github-api",...a.tree&&{tree:a.tree},duration:Date.now()-r}}process.stderr.write(`[greedysearch] GitHub API fetch failed, trying HTTP: ${a.error}
|
|
276
276
|
`)}}if(ce(e)?.type==="post"){process.stderr.write(`[greedysearch] Using Reddit JSON API for: ${e.slice(0,60)}...
|
|
277
277
|
`);let o=await ue(e,t);if(o.ok){let a=P(o.markdown,t);return{url:e,finalUrl:o.finalUrl,status:o.status,contentType:"text/markdown",lastModified:o.lastModified||"",publishedTime:o.publishedTime||"",byline:o.byline||"",siteName:o.siteName||"",lang:o.lang||"",title:o.title,snippet:o.excerpt,content:a,contentChars:a.length,source:"reddit-api",duration:Date.now()-r}}process.stderr.write(`[greedysearch] Reddit API fetch failed, falling back to HTTP: ${o.error}
|
|
278
|
-
`)}let
|
|
279
|
-
`),await St(e,t)}async function St(e,t=8000){let r=Date.now(),
|
|
278
|
+
`)}let i=await ie(e,{timeoutMs:1e4});if(i.ok){let o=P(i.markdown,t);return{url:e,finalUrl:i.finalUrl,status:i.status,contentType:"text/markdown",lastModified:i.lastModified||"",publishedTime:i.publishedTime||"",byline:i.byline||"",siteName:i.siteName||"",lang:i.lang||"",title:i.title,snippet:i.excerpt,content:o,contentChars:o.length,source:"http",duration:Date.now()-r}}if(i.needsBrowser)try{let o=await I();try{let a=await Pt(o,e,t);if(a.content&&a.content.length>100)return a}finally{await R(o)}}catch{}return process.stderr.write(`[greedysearch] HTTP failed for ${e.slice(0,60)}, trying browser...
|
|
279
|
+
`),await St(e,t)}async function St(e,t=8000){let r=Date.now(),n;try{n=await I()}catch(i){return{url:e,title:"",content:null,snippet:"",contentChars:0,error:`openNewTab failed: ${i.message}`,source:"browser",duration:Date.now()-r}}try{await d(["nav",n,e],30000),await new Promise((s)=>setTimeout(s,800));let i=await d(["eval",n,String.raw`
|
|
280
280
|
(function(){
|
|
281
281
|
var el = document.querySelector('article, [role="main"], main, .post-content, .article-body, #content, .content');
|
|
282
282
|
var text = (el || document.body).innerText;
|
|
@@ -286,13 +286,13 @@ ${o}`,pages:n.total}}catch(r){return{error:r.message||String(r)}}}function Q(e="
|
|
|
286
286
|
url: location.href
|
|
287
287
|
});
|
|
288
288
|
})()
|
|
289
|
-
`]),o=JSON.parse(
|
|
290
|
-
`);let a=Array(
|
|
289
|
+
`]),o=JSON.parse(i),a=P(o.content,t);return{url:e,finalUrl:o.url||e,status:200,contentType:"text/plain",lastModified:"",title:o.title,snippet:Q(a,320),content:a,contentChars:a.length,source:"browser",duration:Date.now()-r}}catch(i){return{url:e,title:"",content:null,snippet:"",contentChars:0,error:i.message,source:"browser",duration:Date.now()-r}}finally{await R(n)}}async function Br(e,t=5,r=8000,n=Z){let i=e.slice(0,t);if(i.length===0)return[];let o=Math.min(i.length,Math.max(1,Number.parseInt(String(n),10)||Z));process.stderr.write(`[greedysearch] Fetching content from ${i.length} sources via HTTP (concurrency ${o})...
|
|
290
|
+
`);let a=Array(i.length),s=0,c=0;async function l(){while(!0){let g=s++;if(g>=i.length)return;let h=i[g],y=h.canonicalUrl||h.url;process.stderr.write(`[greedysearch] [${g+1}/${i.length}] Fetching: ${y.slice(0,60)}...
|
|
291
291
|
`);let p=await Dt(y,r).catch((_)=>({url:y,title:"",content:null,snippet:"",contentChars:0,error:_.message,source:"error",duration:0}));if(a[g]={id:h.id,...p},p.content&&p.content.length>100)process.stderr.write(`[greedysearch] ✓ ${p.source}: ${p.content.length} chars
|
|
292
292
|
`);else if(p.error)process.stderr.write(`[greedysearch] ✗ ${p.error.slice(0,80)}
|
|
293
|
-
`);c+=1,process.stderr.write(`PROGRESS:fetch:${c}/${
|
|
293
|
+
`);c+=1,process.stderr.write(`PROGRESS:fetch:${c}/${i.length}
|
|
294
294
|
`)}}await Promise.all(Array.from({length:o},()=>l()));let f=a.filter((g)=>g.content&&g.content.length>100),u=a.filter((g)=>g.source==="http").length,m=a.filter((g)=>g.source==="browser").length;return process.stderr.write(`[greedysearch] Fetched ${f.length}/${a.length} sources (HTTP: ${u}, Browser: ${m})
|
|
295
|
-
`),a}async function Yr(e){let t=await I();await d(["list"]);try{await d(["nav",t,e],30000),await new Promise((
|
|
295
|
+
`),a}async function Yr(e){let t=await I();await d(["list"]);try{await d(["nav",t,e],30000),await new Promise((n)=>setTimeout(n,800));let r=await d(["eval",t,String.raw`
|
|
296
296
|
(function(){
|
|
297
297
|
var el = document.querySelector('article, [role="main"], main, .post-content, .article-body, #content, .content');
|
|
298
298
|
var text = (el || document.body).innerText;
|