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