@duckmind/dm-darwin-arm64 0.52.3 → 0.53.1

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.
Files changed (56) hide show
  1. package/dm +0 -0
  2. package/extensions/.dm-extensions.json +15 -8
  3. package/extensions/dm-cua/bin/browser-cua.mjs +6 -6
  4. package/extensions/dm-cua/index.js +5 -5
  5. package/extensions/dm-cua/src/browser-cua-lib.mjs +2 -2
  6. package/extensions/greedysearch-dm/bin/cdp-greedy.mjs +8 -1
  7. package/extensions/greedysearch-dm/bin/cdp-headless.mjs +1 -1
  8. package/extensions/greedysearch-dm/bin/cdp-visible.mjs +1 -1
  9. package/extensions/greedysearch-dm/bin/cdp.mjs +29 -20
  10. package/extensions/greedysearch-dm/bin/gschrome.mjs +1 -10
  11. package/extensions/greedysearch-dm/bin/kill-visible.mjs +1 -1
  12. package/extensions/greedysearch-dm/bin/launch-visible.mjs +1 -4
  13. package/extensions/greedysearch-dm/bin/launch.mjs +9 -5
  14. package/extensions/greedysearch-dm/bin/mcp.mjs +375 -0
  15. package/extensions/greedysearch-dm/bin/search.mjs +310 -156
  16. package/extensions/greedysearch-dm/bin/visible.mjs +1 -1
  17. package/extensions/greedysearch-dm/extractors/bing-copilot.mjs +205 -68
  18. package/extensions/greedysearch-dm/extractors/chatgpt.mjs +229 -140
  19. package/extensions/greedysearch-dm/extractors/common.mjs +160 -50
  20. package/extensions/greedysearch-dm/extractors/consensus.mjs +225 -92
  21. package/extensions/greedysearch-dm/extractors/consent.mjs +44 -17
  22. package/extensions/greedysearch-dm/extractors/gemini.mjs +275 -136
  23. package/extensions/greedysearch-dm/extractors/google-ai.mjs +172 -64
  24. package/extensions/greedysearch-dm/extractors/logically.mjs +183 -69
  25. package/extensions/greedysearch-dm/extractors/perplexity.mjs +331 -65
  26. package/extensions/greedysearch-dm/extractors/semantic-scholar.mjs +118 -41
  27. package/extensions/greedysearch-dm/index.js +20 -17
  28. package/extensions/greedysearch-dm/package.json +8 -3
  29. package/extensions/greedysearch-dm/skills/greedy-search/skill.md +8 -8
  30. package/extensions/greedysearch-dm/src/fetcher.mjs +2 -2
  31. package/extensions/greedysearch-dm/src/formatters/results.js +8 -6
  32. package/extensions/greedysearch-dm/src/github.mjs +7 -7
  33. package/extensions/greedysearch-dm/src/reddit.mjs +1 -1
  34. package/extensions/greedysearch-dm/src/search/browser-lifecycle.mjs +10 -9
  35. package/extensions/greedysearch-dm/src/search/challenge-detect.mjs +48 -0
  36. package/extensions/greedysearch-dm/src/search/chrome.mjs +129 -53
  37. package/extensions/greedysearch-dm/src/search/constants.mjs +8 -8
  38. package/extensions/greedysearch-dm/src/search/engines.mjs +9 -9
  39. package/extensions/greedysearch-dm/src/search/fetch-source.mjs +154 -77
  40. package/extensions/greedysearch-dm/src/search/minimize.mjs +1 -0
  41. package/extensions/greedysearch-dm/src/search/paths.mjs +1 -1
  42. package/extensions/greedysearch-dm/src/search/pdf.mjs +1 -1
  43. package/extensions/greedysearch-dm/src/search/port-pid.mjs +1 -0
  44. package/extensions/greedysearch-dm/src/search/progress.mjs +2 -0
  45. package/extensions/greedysearch-dm/src/search/recovery.mjs +1 -1
  46. package/extensions/greedysearch-dm/src/search/research.mjs +262 -167
  47. package/extensions/greedysearch-dm/src/search/scale-aware.mjs +11 -0
  48. package/extensions/greedysearch-dm/src/search/simple-research.mjs +827 -0
  49. package/extensions/greedysearch-dm/src/search/synthesis-runner.mjs +11 -11
  50. package/extensions/greedysearch-dm/src/search/synthesis.mjs +11 -11
  51. package/extensions/greedysearch-dm/src/tools/greedy-search-handler.js +23 -20
  52. package/extensions/greedysearch-dm/src/tools/shared.js +10 -9
  53. package/extensions/greedysearch-dm/src/utils/node-runtime.mjs +1 -1
  54. package/package.json +1 -1
  55. package/extensions/greedysearch-dm/src/search/cdp-endpoint.mjs +0 -3
  56. package/extensions/greedysearch-dm/src/utils/browser-executable.mjs +0 -1
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
- import{spawn as k}from"node:child_process";import{dirname as C,join as j}from"node:path";import{fileURLToPath as A}from"node:url";import{basename as N}from"node:path";function R(e=process.env,t=process.execPath){let n=e.GREEDY_SEARCH_NODE||e.NODE_BINARY||e.NODE;if(n?.trim())return n.trim();let r=N(t||"").toLowerCase();if(r==="node"||r==="node.exe")return t;return"node"}var E=C(A(import.meta.url)),I=j(E,"..","bin","cdp.mjs");function d(e,t=30000){return $(e,null,t)}function $(e,t=null,n=30000){return new Promise((r,i)=>{let o=k(R(),[I,...e],{stdio:[t==null?"ignore":"pipe","pipe","pipe"]});if(t!=null)o.stdin.write(t),o.stdin.end();let a="",l="";o.stdout.on("data",(c)=>a+=c),o.stderr.on("data",(c)=>l+=c);let u=setTimeout(()=>{o.kill(),i(Error(`cdp timeout: ${e[0]}`))},n);o.on("close",(c)=>{if(clearTimeout(u),c===0)r(a.trim());else i(Error(l.trim()||`cdp exit ${c}`))})})}async function m(e){if(e)return e;let t=(await d(["list"])).split(`
3
- `)[0]?.slice(0,8);if(!t)throw Error("No Chrome tabs found. Is Chrome running with --remote-debugging-port=9222?");let n=await d(["evalraw",t,"Target.createTarget",'{"url":"about:blank"}']),{targetId:r}=JSON.parse(n);await d(["list"]);let i=r.slice(0,8);try{await T(i)}catch(o){process.stderr.write(`[getOrOpenTab] stealth injection failed: ${o.message}
4
- `)}return i}async function T(e){await d(["evalraw",e,"Page.addScriptToEvaluateOnNewDocument",JSON.stringify({source:`
2
+ import{spawn as C}from"node:child_process";import{basename as T}from"node:path";import{dirname as A,join as M}from"node:path";import{fileURLToPath as $}from"node:url";function j(e=process.env,t=process.execPath){let r=e.GREEDY_SEARCH_NODE||e.NODE_BINARY||e.NODE;if(r?.trim())return r.trim();let n=T(t||"").toLowerCase();if(n==="node"||n==="node.exe")return t;return"node"}var N=A($(import.meta.url)),E=M(N,"..","bin","cdp.mjs"),I=new Set(["list","snap","eval","shot","html","nav","net","click","clickxy","type","loadall","evalraw","browse","stop","--tab"]);function R(e){if(!Array.isArray(e)||e.length===0)throw Error("cdp: args must be a non-empty array");if(e[0]==="test")return e.map((t,r)=>p(t,r));if(!I.has(e[0]))throw Error(`cdp: unknown subcommand '${e[0]}'`);return e.map((t,r)=>p(t,r))}function p(e,t){if(typeof e!=="string")throw Error(`cdp: argv[${t}] must be a string (got ${typeof e})`);if(e.includes("\x00"))throw Error(`cdp: argv[${t}] contains a null byte`);return e}function u(e,t=30000){return S(e,null,t)}function S(e,t=null,r=30000){let n=R(e);return new Promise((i,a)=>{let o=C(j(),[E,...n],{stdio:[t==null?"ignore":"pipe","pipe","pipe"]});if(t!=null)o.stdin.write(t),o.stdin.end();let l="",c="";o.stdout.on("data",(d)=>l+=d),o.stderr.on("data",(d)=>c+=d);let f=setTimeout(()=>{o.kill(),a(Error(`cdp timeout: ${e[0]}`))},r);o.on("close",(d)=>{if(clearTimeout(f),d===0)i(l.trim());else a(Error(c.trim()||`cdp exit ${d}`))})})}async function h(e){if(e)return e;let t=(await u(["list"])).split(`
3
+ `)[0]?.slice(0,8);if(!t)throw Error("No Chrome tabs found. Is Chrome running with --remote-debugging-port=9222?");let r=await u(["evalraw",t,"Target.createTarget",'{"url":"about:blank"}']),n;try{n=JSON.parse(r)}catch(o){throw Error(`Target.createTarget returned invalid JSON: ${o.message}`)}let{targetId:i}=n;if(!i)throw Error("Target.createTarget did not return a targetId");await u(["list"]);let a=i.slice(0,8);try{await W(a)}catch(o){process.stderr.write(`[getOrOpenTab] stealth injection failed: ${o.message}
4
+ `)}return a}async function W(e){await u(["evalraw",e,"Page.addScriptToEvaluateOnNewDocument",JSON.stringify({source:`
5
5
  (function() {
6
6
  // ── Runtime.enable / CDP detection masking ──────────────
7
7
  try { delete window.__REBROWSER_RUNTIME_ENABLE; } catch(_) {}
@@ -12,40 +12,62 @@ import{spawn as k}from"node:child_process";import{dirname as C,join as j}from"no
12
12
  try { delete window._phantom; } catch(_) {}
13
13
  try { delete window.Buffer; } catch(_) {}
14
14
 
15
- // Real Chrome without automation does not expose a useful webdriver value.
16
- // A literal false value is itself a common stealth tell; prefer undefined and
17
- // make the descriptor configurable like native browser properties.
18
- Object.defineProperty(navigator, 'webdriver', { get: () => undefined, configurable: true });
15
+ // Real Chrome without automation should not expose navigator.webdriver at all.
16
+ // A literal false or an own-property getter returning undefined is itself a
17
+ // common stealth tell; remove both instance and prototype properties when the
18
+ // descriptor is configurable (as it is with --disable-blink-features).
19
+ try { delete navigator.webdriver; } catch(_) {}
20
+ try { delete Navigator.prototype.webdriver; } catch(_) {}
19
21
  Object.defineProperty(navigator, 'vendor', { get: () => 'Google Inc.', configurable: true });
20
22
  Object.defineProperty(navigator, 'platform', { get: () => 'Win32', configurable: true });
21
23
  Object.defineProperty(navigator, 'maxTouchPoints', { get: () => 0, configurable: true });
22
24
  Object.defineProperty(navigator, 'pdfViewerEnabled', { get: () => true, configurable: true });
25
+ Object.defineProperty(navigator, 'productSub', { get: () => '20030107', configurable: true });
26
+ Object.defineProperty(navigator, 'product', { get: () => 'Gecko', configurable: true });
27
+ var __greedyMimeTypes = null;
28
+ function __makeMimeTypes() {
29
+ var pdf = { type: 'application/pdf', suffixes: 'pdf', description: 'Portable Document Format', enabledPlugin: null };
30
+ var textPdf = { type: 'text/pdf', suffixes: 'pdf', description: 'Portable Document Format', enabledPlugin: null };
31
+ try { Object.setPrototypeOf(pdf, MimeType.prototype); } catch(_) {}
32
+ try { Object.setPrototypeOf(textPdf, MimeType.prototype); } catch(_) {}
33
+ var m = [pdf, textPdf];
34
+ try { Object.setPrototypeOf(m, MimeTypeArray.prototype); } catch(_) {}
35
+ m.item = function item(i) { return this[i] || null; };
36
+ m.namedItem = function namedItem(name) { return Array.prototype.find.call(this, function(x) { return x && x.type === name; }) || null; };
37
+ return m;
38
+ }
23
39
  Object.defineProperty(navigator, 'plugins', {
24
40
  get: () => {
25
- var p = [
26
- { name: 'Chrome PDF Plugin', filename: 'internal-pdf-viewer', description: 'Portable Document Format' },
27
- { name: 'Chrome PDF Viewer', filename: 'mhjfbmdgcfjbbpaeojofohoefgiehjai', description: '' },
28
- { name: 'Native Client', filename: 'internal-nacl-plugin', description: '' },
29
- ];
30
- p.length = 3;
41
+ __greedyMimeTypes = __greedyMimeTypes || __makeMimeTypes();
42
+ var plugin0 = { name: 'Chrome PDF Plugin', filename: 'internal-pdf-viewer', description: 'Portable Document Format' };
43
+ var plugin1 = { name: 'Chrome PDF Viewer', filename: 'mhjfbmdgcfjbbpaeojofohoefgiehjai', description: '' };
44
+ var plugin2 = { name: 'Native Client', filename: 'internal-nacl-plugin', description: '' };
45
+ try { Object.setPrototypeOf(plugin0, Plugin.prototype); } catch(_) {}
46
+ try { Object.setPrototypeOf(plugin1, Plugin.prototype); } catch(_) {}
47
+ try { Object.setPrototypeOf(plugin2, Plugin.prototype); } catch(_) {}
48
+ var p = [plugin0, plugin1, plugin2];
49
+ p.item = function item(i) { return this[i] || null; };
50
+ p.namedItem = function namedItem(name) { return Array.prototype.find.call(this, function(x) { return x && x.name === name; }) || null; };
51
+ p.refresh = function refresh() {};
52
+ try { Object.setPrototypeOf(p, PluginArray.prototype); } catch(_) {}
53
+ try {
54
+ __greedyMimeTypes[0].enabledPlugin = p[0];
55
+ __greedyMimeTypes[1].enabledPlugin = p[0];
56
+ } catch(_) {}
31
57
  return p;
32
58
  },
59
+ configurable: true,
33
60
  });
34
61
  Object.defineProperty(navigator, 'mimeTypes', {
35
62
  get: () => {
36
- var m = [
37
- { type: 'application/pdf', suffixes: 'pdf', description: 'Portable Document Format', enabledPlugin: null },
38
- { type: 'text/pdf', suffixes: 'pdf', description: 'Portable Document Format', enabledPlugin: null },
39
- ];
40
- m.item = function(i) { return m[i] || null; };
41
- m.namedItem = function(name) { return m.find(function(x) { return x.type === name; }) || null; };
42
- return m;
63
+ __greedyMimeTypes = __greedyMimeTypes || __makeMimeTypes();
64
+ return __greedyMimeTypes;
43
65
  },
44
66
  configurable: true,
45
67
  });
46
68
  Object.defineProperty(navigator, 'languages', { get: () => ['en-US', 'en'], configurable: true });
47
69
  try {
48
- Object.defineProperty(navigator, 'connection', { get: () => ({ effectiveType: '4g', rtt: 50, downlink: 10, saveData: false }), configurable: true });
70
+ Object.defineProperty(navigator, 'connection', { get: () => ({ effectiveType: '4g', rtt: 50, downlink: 10, downlinkMax: Infinity, saveData: false }), configurable: true });
49
71
  } catch(_) {}
50
72
  if (!navigator.mediaDevices) {
51
73
  Object.defineProperty(navigator, 'mediaDevices', {
@@ -61,6 +83,18 @@ import{spawn as k}from"node:child_process";import{dirname as C,join as j}from"no
61
83
  configurable: true,
62
84
  });
63
85
  }
86
+ // ── Missing platform APIs (headless often lacks these) ─
87
+ try {
88
+ if (!navigator.share) {
89
+ navigator.share = function() { return Promise.reject(new Error('NotAllowedError')); };
90
+ }
91
+ } catch(_) {}
92
+ try {
93
+ if (!navigator.contentIndex) {
94
+ Object.defineProperty(navigator, 'contentIndex', { get: () => ({ add: function() {}, delete: function() {}, getAll: function() { return Promise.resolve([]); } }), configurable: true });
95
+ }
96
+ } catch(_) {}
97
+
64
98
  if (!window.chrome) {
65
99
  window.chrome = {
66
100
  app: { isInstalled: false, InstallState: {}, RunningState: {} },
@@ -68,8 +102,8 @@ import{spawn as k}from"node:child_process";import{dirname as C,join as j}from"no
68
102
  OnInstalledReason: {}, OnRestartRequiredReason: {}, PlatformArch: {}, PlatformNaclArch: {}, PlatformOs: {}, RequestUpdateCheckStatus: {},
69
103
  connect: () => ({}), sendMessage: () => {}, onMessage: { addListener: () => {} }
70
104
  },
71
- loadTimes: () => ({}),
72
- csi: () => ({}),
105
+ loadTimes: function() { return { requestTime: 0, startLoadTime: Date.now() - 5000, commitLoadTime: Date.now() - 3000, finishDocumentLoadTime: Date.now() - 2000, finishLoadTime: Date.now() - 1000, firstPaintTime: Date.now() - 800, navigationType: 'Other', wasFetchedViaSpdy: true, wasNpnNegotiated: true, npnNegotiatedProtocol: 'h2', wasAlternateProtocolAvailable: false, connectionInfo: 'http/2' }; },
106
+ csi: function() { var t = Date.now(); return { onloadT: t - 2000, startE: t - 5000, pageT: 'back', tran: 2 }; },
73
107
  };
74
108
  }
75
109
  var __greedyNativeFns = [];
@@ -90,6 +124,19 @@ import{spawn as k}from"node:child_process";import{dirname as C,join as j}from"no
90
124
  return getParam.call(this, p);
91
125
  });
92
126
  } catch(_) {}
127
+ // ── WebGL readPixels noise ──────────────────────────
128
+ // CreepJS and other fingerprinters draw content with WebGL and read back the
129
+ // rendered pixels. Adding subtle noise breaks rendering-based fingerprinting.
130
+ try {
131
+ var origReadPixels = WebGLRenderingContext.prototype.readPixels;
132
+ WebGLRenderingContext.prototype.readPixels = __markNative(function readPixels(x, y, width, height, format, type, pixels) {
133
+ var result = origReadPixels.call(this, x, y, width, height, format, type, pixels);
134
+ if (pixels && pixels.length > 0) {
135
+ pixels[0] ^= 1;
136
+ }
137
+ return result;
138
+ });
139
+ } catch(_) {}
93
140
  Object.defineProperty(navigator, 'hardwareConcurrency', { get: () => 8, configurable: true });
94
141
  Object.defineProperty(navigator, 'deviceMemory', { get: () => 8, configurable: true });
95
142
 
@@ -97,7 +144,7 @@ import{spawn as k}from"node:child_process";import{dirname as C,join as j}from"no
97
144
  // Headless rendering engines produce slightly different canvas output
98
145
  // than headed Chrome. Subtle noise breaks hash-based fingerprinting.
99
146
  try {
100
- var __canvasNoise = ((Date.now() % 997) + Math.floor(Math.random() * 997)) & 1;
147
+ var __canvasNoise = ((Date.now() & 0xFF) | 1);
101
148
  var origFill = CanvasRenderingContext2D.prototype.fillText;
102
149
  CanvasRenderingContext2D.prototype.fillText = __markNative(function fillText() {
103
150
  this.globalAlpha = 0.9995;
@@ -116,15 +163,39 @@ import{spawn as k}from"node:child_process";import{dirname as C,join as j}from"no
116
163
  HTMLCanvasElement.prototype.toDataURL = __markNative(function toDataURL() {
117
164
  var ctx = this.getContext('2d');
118
165
  if (ctx) {
119
- // Add 1px noise pixel in corner (invisible but changes hash)
120
- var imgData = ctx.getImageData(0, 0, 1, 1);
121
- if (imgData) imgData.data[0] ^= __canvasNoise;
122
- ctx.putImageData(imgData, 0, 0);
166
+ // Spread noise across canvas to break hash-based fingerprinting.
167
+ // Uses a deterministic pattern so it's consistent per page load
168
+ // but varies between sessions.
169
+ var w = this.width, h = this.height;
170
+ if (w > 0 && h > 0) {
171
+ var imgData = ctx.getImageData(0, 0, Math.min(w, 4), Math.min(h, 4));
172
+ if (imgData && imgData.data) {
173
+ for (var __i = 0; __i < imgData.data.length; __i += 4) {
174
+ imgData.data[__i] ^= (__canvasNoise + __i) & 0xFF;
175
+ }
176
+ ctx.putImageData(imgData, 0, 0);
177
+ }
178
+ }
123
179
  }
124
180
  return origToDataURL.apply(this, arguments);
125
181
  });
126
182
  } catch(_) {}
127
183
 
184
+ // ── AudioContext fingerprint noise ────────────────────
185
+ // Headless Chrome's AudioContext produces slightly different output.
186
+ // Subtle noise breaks audio-based fingerprinting.
187
+ try {
188
+ var __audioSeed = ((Date.now() & 0x1F) | 1);
189
+ var origGetChannelData = AudioBuffer.prototype.getChannelData;
190
+ AudioBuffer.prototype.getChannelData = __markNative(function getChannelData(channel) {
191
+ var data = origGetChannelData.call(this, channel);
192
+ for (var __i = 0; __i < data.length; __i += 64) {
193
+ data[__i] *= 0.99999;
194
+ }
195
+ return data;
196
+ });
197
+ } catch(_) {}
198
+
128
199
  // ── window outer dimensions ──────────────────────────
129
200
  // outerWidth/Height = 0 in headless — a well-known bot signal.
130
201
  // Mirror innerWidth/Height (set by --window-size flag) so the ratio is sane.
@@ -134,9 +205,15 @@ import{spawn as k}from"node:child_process";import{dirname as C,join as j}from"no
134
205
  } catch(_) {}
135
206
 
136
207
  // ── screen properties ─────────────────────────────────
208
+ // Headless Chrome often reports an 800x600 screen even when the viewport is
209
+ // 1920x1080. Keep screen metrics internally consistent with our launch flags.
137
210
  try {
138
- if (!screen.colorDepth) Object.defineProperty(screen, 'colorDepth', { get: () => 24, configurable: true });
139
- if (!screen.pixelDepth) Object.defineProperty(screen, 'pixelDepth', { get: () => 24, configurable: true });
211
+ Object.defineProperty(screen, 'width', { get: () => 1920, configurable: true });
212
+ Object.defineProperty(screen, 'height', { get: () => 1080, configurable: true });
213
+ Object.defineProperty(screen, 'availWidth', { get: () => 1920, configurable: true });
214
+ Object.defineProperty(screen, 'availHeight', { get: () => 1040, configurable: true });
215
+ Object.defineProperty(screen, 'colorDepth', { get: () => 24, configurable: true });
216
+ Object.defineProperty(screen, 'pixelDepth', { get: () => 24, configurable: true });
140
217
  } catch(_) {}
141
218
 
142
219
  // ── navigator.userAgentData (UA Client Hints) ─────────
@@ -209,10 +286,10 @@ import{spawn as k}from"node:child_process";import{dirname as C,join as j}from"no
209
286
  };
210
287
  } catch(_) {}
211
288
  })();
212
- `})])}async function h(e,t,n=15000,r=500){let i=String.raw`
289
+ `})])}async function y(e,t,r=15000,n=500){let i=String.raw`
213
290
  new Promise((resolve) => {
214
- const _deadline = Date.now() + ${n};
215
- const _baseInterval = ${r};
291
+ const _deadline = Date.now() + ${r};
292
+ const _baseInterval = ${n};
216
293
 
217
294
  function _jitter(ms) {
218
295
  return Math.max(50, ms + (Math.random() * ms * 0.4 - ms * 0.2));
@@ -228,11 +305,11 @@ import{spawn as k}from"node:child_process";import{dirname as C,join as j}from"no
228
305
 
229
306
  _poll();
230
307
  })
231
- `;return await d(["eval",e,i],n+5000)==="true"}async function _(e){let t=e.indexOf("--stdin");if(t===-1)return e;let n=await new Promise((i)=>{let o="";process.stdin.setEncoding("utf8"),process.stdin.on("data",(a)=>o+=a),process.stdin.on("end",()=>i(o.trim()))}),r=[...e];return r[t]=n,r}function b(e){let t=e.includes("--short"),n=e.filter((l)=>l!=="--short"),r=n.indexOf("--tab"),i=r===-1?null:n[r+1];if(r!==-1)n=n.filter((l,u)=>u!==r&&u!==r+1);let o=n.indexOf("--locale"),a=o===-1?null:n[o+1];if(o!==-1)n=n.filter((l,u)=>u!==o&&u!==o+1);return{query:n.join(" "),tabPrefix:i,short:t,locale:a}}function w(e,t){if(!e.length||e[0]==="--help")process.stderr.write(t),process.exit(1)}function p(e,t,n=300){if(!t||e.length<=n)return e;let r=e.slice(0,n),i=r.lastIndexOf(" ");return i>0?`${r.slice(0,i)}…`:`${r}…`}function y(e){process.stdout.write(`${JSON.stringify(e,null,2)}
232
- `)}function f(e,t,n=null){if(!e||typeof e!=="object")return;let r=n?` (+${Date.now()-n}ms)`:"";if(e.lastStage=t,!Array.isArray(e.stages))e.stages=[];e.stages.push({stage:t,at:Date.now()});let i=e.engine||"extractor";console.error(`[${i}] stage: ${t}${r}`)}function g({engine:e,mode:t="headless",clipboardEmpty:n=null,fallbackUsed:r=null,blockedBy:i=null,verificationResult:o=null,inputReady:a=null,durationMs:l=null,lastStage:u=null,stages:c=null}={}){return{engine:e,mode:t,clipboardEmpty:n,fallbackUsed:r,blockedBy:i,verificationResult:o,inputReady:a,durationMs:l,lastStage:u,stages:c}}function v(e,t=null){if(t){let n=JSON.stringify({_envelope:t,error:e.message});process.stdout.write(`${n}
308
+ `;return await u(["eval",e,i],r+5000)==="true"}async function _(e){let t=e.indexOf("--stdin");if(t===-1)return e;let r=await new Promise((i)=>{let a="";process.stdin.setEncoding("utf8"),process.stdin.on("data",(o)=>a+=o),process.stdin.on("end",()=>i(a.trim()))}),n=[...e];return n[t]=r,n}function w(e){let t=e.includes("--short"),r=e.filter((l)=>l!=="--short"),n=r.indexOf("--tab"),i=n===-1?null:r[n+1];if(n!==-1)r=r.filter((l,c)=>c!==n&&c!==n+1);let a=r.indexOf("--locale"),o=a===-1?null:r[a+1];if(a!==-1)r=r.filter((l,c)=>c!==a&&c!==a+1);return{query:r.join(" "),tabPrefix:i,short:t,locale:o}}function b(e,t){if(!e.length||e[0]==="--help")process.stderr.write(t),process.exit(1)}function v(e,t,r=300){if(!t||e.length<=r)return e;let n=e.slice(0,r),i=n.lastIndexOf(" ");return i>0?`${n.slice(0,i)}…`:`${n}…`}function x(e){process.stdout.write(`${JSON.stringify(e,null,2)}
309
+ `)}function g(e,t,r=null){if(!e||typeof e!=="object")return;let n=r?` (+${Date.now()-r}ms)`:"";if(e.lastStage=t,!Array.isArray(e.stages))e.stages=[];e.stages.push({stage:t,at:Date.now()});let i=e.engine||"extractor";console.error(`[${i}] stage: ${t}${n}`)}function m({engine:e,mode:t="headless",clipboardEmpty:r=null,fallbackUsed:n=null,blockedBy:i=null,verificationResult:a=null,inputReady:o=null,durationMs:l=null,lastStage:c=null,stages:f=null}={}){return{engine:e,mode:t,clipboardEmpty:r,fallbackUsed:n,blockedBy:i,verificationResult:a,inputReady:o,durationMs:l,lastStage:c,stages:f}}function P(e,t=null){if(t){let r=JSON.stringify({_envelope:t,error:e.message});process.stdout.write(`${r}
233
310
  `)}process.stderr.write(`Error: ${e.message}
234
- `),process.exit(1)}var M=`Usage: node extractors/semantic-scholar.mjs "<query>" [--tab <prefix>]
235
- `,L=".cl-paper-row[data-paper-id]";function x(e){let t=String(e||"").replaceAll("-"," ");return`https://www.semanticscholar.org/search?q=${encodeURIComponent(t)}&sort=relevance`}async function S(e){await d(["eval",e,String.raw`
311
+ `),process.exit(1)}var L=`Usage: node extractors/semantic-scholar.mjs "<query>" [--tab <prefix>]
312
+ `,F=".cl-paper-row[data-paper-id]";function D(e){let t=String(e||"").replaceAll("-"," ");return`https://www.semanticscholar.org/search?q=${encodeURIComponent(t)}&sort=relevance`}async function G(e){await u(["eval",e,String.raw`
236
313
  (() => {
237
314
  const selectors = [
238
315
  '.osano-cm-dialog__close',
@@ -246,7 +323,7 @@ import{spawn as k}from"node:child_process";import{dirname as C,join as j}from"no
246
323
  }
247
324
  return null;
248
325
  })()
249
- `]).catch(()=>null)}async function W(e,{limit:t=8}={}){let n=await d(["eval",e,String.raw`
326
+ `]).catch(()=>null)}async function H(e,{limit:t=8}={}){let r=await u(["eval",e,String.raw`
250
327
  ((limit) => {
251
328
  function clean(value) {
252
329
  return String(value || '').replace(/\s+/g, ' ').trim();
@@ -303,7 +380,7 @@ import{spawn as k}from"node:child_process";import{dirname as C,join as j}from"no
303
380
  };
304
381
  }));
305
382
  })(${t})
306
- `]);try{return JSON.parse(n)}catch{return[]}}function F(e){if(!e.length)return"Semantic Scholar returned no paper results.";return e.map((t)=>{let n=[];if(t.authors?.length)n.push(t.authors.join(", "));if(t.venue)n.push(t.venue);if(t.date)n.push(t.date);if(Number.isFinite(t.citationCount))n.push(`${t.citationCount.toLocaleString()} citations`);let r=n.length?` — ${n.join(" · ")}`:"",i=t.tldr?`
307
- TLDR: ${t.tldr}`:"";return`${t.rank}. ${t.title}${r}${i}`}).join(`
383
+ `]);try{return JSON.parse(r)}catch{return[]}}function J(e){if(!e.length)return"Semantic Scholar returned no paper results.";return e.map((t)=>{let r=[];if(t.authors?.length)r.push(t.authors.join(", "));if(t.venue)r.push(t.venue);if(t.date)r.push(t.date);if(Number.isFinite(t.citationCount))r.push(`${t.citationCount.toLocaleString()} citations`);let n=r.length?` — ${r.join(" · ")}`:"",i=t.tldr?`
384
+ TLDR: ${t.tldr}`:"";return`${t.rank}. ${t.title}${n}${i}`}).join(`
308
385
 
309
- `)}async function H(){let e=await _(process.argv.slice(2));w(e,M);let{query:t,tabPrefix:n,short:r}=b(e),i=Date.now(),a={engine:"semantic-scholar",mode:process.env.GREEDY_SEARCH_VISIBLE==="1"?"visible":"headless",blockedBy:null,verificationResult:null,inputReady:null};try{if(!n)await d(["list"]);let l=await m(n);f(a,"nav",i),await d(["nav",l,x(t)],25000),await new Promise((s)=>setTimeout(s,800)),f(a,"consent",i),await S(l),f(a,"results-wait",i);let u=await h(l,L,15000,500);if(a.inputReady=u,!u){let s=await d(["eval",l,"document.body?.innerText || ''"]).catch(()=>"");if(/captcha|cloudflare|verify|robot|blocked/i.test(s))throw a.blockedBy="verification",a.verificationResult="needs-human",Error("Semantic Scholar verification required — please solve it in the visible browser window");throw Error("Semantic Scholar results not found")}f(a,"extract",i);let c=await W(l,{limit:r?5:8}),P=c.filter((s)=>s.title&&s.url).map((s)=>({title:s.pdfUrl?`${s.title} (PDF)`:s.title,url:s.url,semanticScholarUrl:s.semanticScholarUrl,paperId:s.paperId,citationCount:s.citationCount,venue:s.venue,year:s.date})),D=F(c),O=Date.now()-i;y({answer:p(D,r),sources:P,query:t,url:x(t),papers:c,_envelope:g({...a,durationMs:O})})}catch(l){v(l,g({...a,durationMs:Date.now()-i}))}}H();
386
+ `)}async function q(){let e=await _(process.argv.slice(2));b(e,L);let{query:t,tabPrefix:r,short:n}=w(e),i=Date.now(),o={engine:"semantic-scholar",mode:process.env.GREEDY_SEARCH_VISIBLE==="1"?"visible":"headless",blockedBy:null,verificationResult:null,inputReady:null};try{if(!r)await u(["list"]);let l=await h(r);g(o,"nav",i),await u(["nav",l,D(t)],25000),await new Promise((s)=>setTimeout(s,800)),g(o,"consent",i),await G(l),g(o,"results-wait",i);let c=await y(l,F,15000,500);if(o.inputReady=c,!c){let s=await u(["eval",l,"document.body?.innerText || ''"]).catch(()=>"");if(/captcha|cloudflare|verify|robot|blocked/i.test(s))throw o.blockedBy="verification",o.verificationResult="needs-human",Error("Semantic Scholar verification required — please solve it in the visible browser window");throw Error("Semantic Scholar results not found")}g(o,"extract",i);let f=await H(l,{limit:n?5:8}),d=f.filter((s)=>s.title&&s.url).map((s)=>({title:s.pdfUrl?`${s.title} (PDF)`:s.title,url:s.url,semanticScholarUrl:s.semanticScholarUrl,paperId:s.paperId,citationCount:s.citationCount,venue:s.venue,year:s.date})),O=J(f),k=Date.now()-i;x({answer:v(O,n),sources:d,query:t,url:D(t),papers:f,_envelope:m({...o,durationMs:k})})}catch(l){P(l,m({...o,durationMs:Date.now()-i}))}}q();
@@ -1,20 +1,23 @@
1
- import{spawn as wX}from"node:child_process";import{existsSync as DX,mkdirSync as GX,readFileSync as LX,writeFileSync as RX}from"node:fs";import{homedir as vX}from"node:os";import{dirname as CX,join as h}from"node:path";import{fileURLToPath as NX}from"node:url";import{Type as Y}from"@sinclair/typebox";function G(X){return{bing:"Bing Copilot",google:"Google AI",gemini:"Gemini",copilot:"Copilot",perplexity:"Perplexity"}[X]??X.charAt(0).toUpperCase()+X.slice(1)}function p(X){if(!X)return"";if(X==="official-docs")return"official docs";return X.replace(/-/g," ")}function u(X){if(!X)return"Mixed";return X.charAt(0).toUpperCase()+X.slice(1)}function m(X){return String(X.displayUrl||X.canonicalUrl||X.url||"")}function VX(X){return String(X.title||X.domain||m(X)||"Untitled source")}function WX(X){if(typeof X.engineCount==="number")return X.engineCount;return(Array.isArray(X.engines)?X.engines:[]).length}function BX(X){return new Map(X.map(($)=>[String($.id||""),$]).filter(([$])=>$))}function d(X){let $=String(X.id||"?"),J=m(X),K=VX(X),Q=String(X.domain||""),V=Array.isArray(X.engines)?X.engines:[],B=WX(X),W=p(String(X.sourceType||"")),q=X.fetch,Z=q?.ok?`fetched ${q.status||200}`:q?.attempted?"fetch failed":"";return`- ${[`${$} - [${K}](${J})`,Q,W,V.length?`cited by ${V.map(G).join(", ")} (${B}/3)`:`${B}/3`,Z].filter(Boolean).join(" - ")}`}function l(X,$=[],J=6){if(!X.length)return[];let K=BX(X),Q=$.map((V)=>K.get(V)).filter((V)=>Boolean(V));if(Q.length>0)return Q.slice(0,J);return X.slice(0,J)}function c(X,$,J,K=6){if($.answer)X.push("## Answer"),X.push(String($.answer)),X.push("");let Q=$.agreement,V=String(Q?.summary||"").trim(),B=String(Q?.level||"").trim();if(V||B)X.push("## Consensus"),X.push(`- ${u(B)}${V?` - ${V}`:""}`),X.push("");let W=Array.isArray($.differences)?$.differences:[];if(W.length>0){X.push("## Where Engines Differ");for(let z of W)X.push(`- ${z}`);X.push("")}let q=Array.isArray($.caveats)?$.caveats:[];if(q.length>0){X.push("## Caveats");for(let z of q)X.push(`- ${z}`);X.push("")}let Z=Array.isArray($.claims)?$.claims:[];if(Z.length>0){X.push("## Key Claims");for(let z of Z){let M=Array.isArray(z.sourceIds)?z.sourceIds:[],O=String(z.support||"moderate");X.push(`- ${String(z.claim||"")} [${O}${M.length?`; ${M.join(", ")}`:""}]`)}X.push("")}let H=Array.isArray($.recommendedSources)?$.recommendedSources:[],b=l(J,H,K);if(b.length>0){X.push("## Top Sources");for(let z of b)X.push(d(z));X.push("")}}function o(X,$){let J=[];if(X==="all")return ZX($,J);return YX($,J)}function ZX(X,$){let{_synthesis:J,_sources:K,_needsHumanVerification:Q,_research:V}=X;if(Q){let B=Array.isArray(Q.engines)?Q.engines.join(", "):"one or more engines";$.push("## Manual verification required"),$.push(String(Q.message||"Visible Chrome is open. Solve the verification challenge, then rerun the same search.")),$.push(`Engines: ${B}`),$.push("")}if(J?.answer){if(V?.mode==="iterative")qX($,V);c($,J,K||[],6);let B=String(J.synthesizedBy||"configured synthesizer");return $.push(V?.mode==="iterative"?`*Research mode: iterative planning, source fetching, citation audit, and bundle output*
1
+ import{spawn as fJ}from"node:child_process";import{basename as UJ}from"node:path";function L(J=process.env,$=process.execPath){let K=J.GREEDY_SEARCH_NODE||J.NODE_BINARY||J.NODE;if(K?.trim())return K.trim();let Q=UJ($||"").toLowerCase();if(Q==="node"||Q==="node.exe")return $;return"node"}import{existsSync as yJ,mkdirSync as hJ,readFileSync as gJ,writeFileSync as pJ}from"node:fs";import{homedir as uJ}from"node:os";import{dirname as mJ,join as l}from"node:path";import{fileURLToPath as dJ}from"node:url";import{Type as k}from"@sinclair/typebox";function F(J){return{bing:"Bing Copilot",google:"Google AI",gemini:"Gemini",copilot:"Copilot",perplexity:"Perplexity"}[J]??J.charAt(0).toUpperCase()+J.slice(1)}function t(J){if(!J)return"";if(J==="official-docs")return"official docs";return J.replace(/-/g," ")}function a(J){if(!J)return"Mixed";return J.charAt(0).toUpperCase()+J.slice(1)}function r(J){return String(J.displayUrl||J.canonicalUrl||J.url||"")}function jJ(J){return String(J.title||J.domain||r(J)||"Untitled source")}function AJ(J){if(typeof J.engineCount==="number")return J.engineCount;return(Array.isArray(J.engines)?J.engines:[]).length}function wJ(J){return new Map(J.map(($)=>[String($.id||""),$]).filter(([$])=>$))}function n(J){let $=String(J.id||"?"),K=r(J),Q=jJ(J),X=String(J.domain||""),V=Array.isArray(J.engines)?J.engines:[],B=AJ(J),q=t(String(J.sourceType||"")),H=J.fetch,Z=H?.ok?`fetched ${H.status||200}`:H?.attempted?"fetch failed":"";return`- ${[`${$} - [${Q}](${K})`,X,q,V.length?`cited by ${V.map(F).join(", ")} (${B}/3)`:`${B}/3`,Z].filter(Boolean).join(" - ")}`}function s(J,$=[],K=6){if(!J.length)return[];let Q=wJ(J),X=$.map((V)=>Q.get(V)).filter((V)=>Boolean(V));if(X.length>0)return X.slice(0,K);return J.slice(0,K)}function e(J,$,K,Q=6){if($.answer)J.push("## Answer"),J.push(String($.answer)),J.push("");let X=$.agreement,V=String(X?.summary||"").trim(),B=String(X?.level||"").trim();if(V||B)J.push("## Consensus"),J.push(`- ${a(B)}${V?` - ${V}`:""}`),J.push("");let q=Array.isArray($.differences)?$.differences:[];if(q.length>0){J.push("## Where Engines Differ");for(let W of q)J.push(`- ${W}`);J.push("")}let H=Array.isArray($.caveats)?$.caveats:[];if(H.length>0){J.push("## Caveats");for(let W of H)J.push(`- ${W}`);J.push("")}let Z=Array.isArray($.claims)?$.claims:[];if(Z.length>0){J.push("## Key Claims");for(let W of Z){let b=Array.isArray(W.sourceIds)?W.sourceIds:[],U=String(W.support||"moderate");J.push(`- ${String(W.claim||"")} [${U}${b.length?`; ${b.join(", ")}`:""}]`)}J.push("")}let Y=Array.isArray($.recommendedSources)?$.recommendedSources:[],z=s(K,Y,Q);if(z.length>0){J.push("## Top Sources");for(let W of z)J.push(n(W));J.push("")}}var JJ=800;function $J(J){return J.split(`
2
+ `).map(($)=>$.length>JJ?$.slice(0,JJ-1)+"…":$).join(`
3
+ `)}function KJ(J,$){let K=[];if(J==="all")return $J(OJ($,K));return $J(GJ($,K))}function OJ(J,$){let{_synthesis:K,_sources:Q,_needsHumanVerification:X,_research:V}=J;if(X){let B=Array.isArray(X.engines)?X.engines.join(", "):"one or more engines";$.push("## Manual verification required"),$.push(String(X.message||"Visible Chrome is open. Solve the verification challenge, then rerun the same search.")),$.push(`Engines: ${B}`),$.push("")}if(K?.answer){if(V?.mode==="iterative")MJ($,V);e($,K,Q||[],6);let B=String(K.synthesizedBy||"configured synthesizer");return $.push(V?.mode==="iterative"?`*Research mode: iterative planning, source fetching, citation audit, and bundle output*
2
4
  `:`*Synthesized by ${B} from multi-engine results and fetched sources*
3
5
  `),$.join(`
4
- `).trim()}for(let[B,W]of Object.entries(X)){if(B.startsWith("_"))continue;$.push(`
5
- ## ${G(B)}`),i(W,$,3)}return $.join(`
6
- `).trim()}function qX(X,$){let J=$.floor,K=J?.metrics,Q=$.bundle,V=$.manifest;if(X.push("## Research Run"),X.push(`- Status: ${J?.floorMet?"floor met":"partial / floor unmet"}`),V?.terminationReason)X.push(`- Stop reason: ${String(V.terminationReason)}`);if(K)X.push(`- Evidence: ${K.fetchedOk||0} fetched sources, ${K.primarySources||0} primary/official, ${K.claims||0} claims, ${K.cited||0} citations`),X.push(`- Questions: ${K.closedQuestions||0}/${K.totalQuestions||0} closed${K.openQuestions?`, ${K.openQuestions} open`:""}`);if(Q?.dir)X.push(`- Bundle: ${String(Q.dir)}`);X.push("")}function YX(X,$){let J=X._needsHumanVerification;if(J){let K=Array.isArray(J.engines)?J.engines.join(", "):"this engine";$.push("## Manual verification required"),$.push(String(J.message||"Visible Chrome is open. Solve the verification challenge, then rerun the same search.")),$.push(`Engines: ${K}`),$.push("")}return i(X,$,5),$.join(`
7
- `).trim()}function i(X,$,J){if(X.error){$.push(`Error: ${X.error}`);return}if(X.answer)$.push(String(X.answer));let K=X.sources;if(Array.isArray(K)&&K.length>0){$.push(`
8
- Sources:`);for(let Q of K.slice(0,J))$.push(`- [${Q.title||Q.url}](${Q.url})`)}}import{spawn as jX}from"node:child_process";import{existsSync as PX}from"node:fs";import{join as UX}from"node:path";import{basename as zX}from"node:path";function L(X=process.env,$=process.execPath){let J=X.GREEDY_SEARCH_NODE||X.NODE_BINARY||X.NODE;if(J?.trim())return J.trim();let K=zX($||"").toLowerCase();if(K==="node"||K==="node.exe")return $;return"node"}import{existsSync as R,mkdirSync as kX,readFileSync as a,writeFileSync as HX}from"node:fs";import{homedir as MX}from"node:os";import{join as r}from"node:path";import{tmpdir as v}from"node:os";var n=`${v().replaceAll("\\","/")}/greedysearch-chrome-profile`,oX=`${n}/DevToolsActivePort`,iX=`${v().replaceAll("\\","/")}/cdp-pages.json`,tX=`${v().replaceAll("\\","/")}/greedysearch-chrome-mode`,aX=`${v().replaceAll("\\","/")}/greedysearch-visible-recovery.jsonl`,F=r(MX(),".dm"),U=r(F,"greedyconfig"),I=["perplexity","google","chatgpt"],T="gemini";function bX(){try{if(R(U)){let X=a(U,"utf8"),$=JSON.parse(X);if(Array.isArray($.engines)&&$.engines.length>0&&$.engines.every((J)=>typeof J==="string")){let J=$.engines.filter((Q)=>_[Q]),K=$.engines.filter((Q)=>!_[Q]);if(K.length>0)process.stderr.write(`[greedysearch] Warning: ignoring unknown engine(s) in ${U}: ${K.join(", ")}
9
- [greedysearch] Available engines: ${Object.keys(_).join(", ")}
10
- `);if(J.length>0)return J;process.stderr.write(`[greedysearch] Warning: no valid engines in ${U}, falling back to defaults: ${I.join(", ")}
11
- `)}}}catch{}return I}function AX(){try{if(!R(F))kX(F,{recursive:!0});if(!R(U))HX(U,JSON.stringify({engines:I,synthesizer:T},null,2)+`
12
- `,"utf8")}catch{}}AX();var t=["gemini","chatgpt"];function OX(){try{if(R(U)){let X=a(U,"utf8"),$=JSON.parse(X);if(typeof $.synthesizer==="string"){let J=$.synthesizer.toLowerCase();if(t.includes(J))return J;process.stderr.write(`[greedysearch] Warning: unknown synthesizer "${$.synthesizer}" in ${U}
13
- [greedysearch] Available synthesizers: ${t.join(", ")}
14
- [greedysearch] Falling back to default: ${T}
15
- `)}}}catch{}return T}var _={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"},x=bX();var rX=OX(),nX=Math.max(1,Number.parseInt(process.env.GREEDY_FETCH_CONCURRENCY||"5",10)||5);process.env.CDP_PROFILE_DIR=n;function S(X){return X.replace(/^"|"$/g,"")}function C(X){return PX(UX(X,"bin","cdp.mjs"))}function s(){return{content:[{type:"text",text:"cdp.mjs missing — try reinstalling: Rebuild or reinstall DM to refresh the bundled GreedySearch asset."}],details:{}}}function e(X,$){let J=$ instanceof Error?$.message:String($);return{content:[{type:"text",text:`${X}: ${J}`}],details:{}}}function XX(X,$,J,K,Q,V,B={}){return new Promise((W,q)=>{let{headless:Z=!0}=B,H=[...J];if(Z!==!1&&process.env.GREEDY_SEARCH_VISIBLE!=="1")H.push("--headless");if(Z===!1)H.push("--always-visible");let b={...process.env};if(Z===!1)b.GREEDY_SEARCH_VISIBLE="1",b.GREEDY_SEARCH_ALWAYS_VISIBLE="1";let z=jX(L(),[K,X,"--inline","--stdin",...H],{stdio:["pipe","pipe","pipe"],env:b});z.stdin.write($),z.stdin.end();let M="",O="",j=()=>{z.kill("SIGTERM"),q(Error("Aborted"))};Q?.addEventListener("abort",j,{once:!0}),z.stderr.on("data",(A)=>{O+=A;let k=/^PROGRESS:(perplexity|google|chatgpt|bing|gemini|semantic-scholar|semanticscholar|s2|logically):(done|error|needs-human)$/;for(let N of A.toString().split(`
16
- `)){let D=N.match(k);if(D&&V)V(D[1],D[2]);let w=N.match(/^PROGRESS:synthesis:(done|error|skipped)$/);if(w&&V)V("synthesis",w[1])}}),z.stdout.on("data",(A)=>M+=A),z.on("close",(A)=>{if(Q?.removeEventListener("abort",j),A!==0)q(Error(O.trim()||`search.mjs exited with code ${A}`));else try{W(JSON.parse(M.trim()))}catch{q(Error(`Invalid JSON from search.mjs: ${M.slice(0,200)}`))}})})}function $X(X,$,J,K){let Q=new Map;return(V,B)=>{Q.set(V,B);let W=[];for(let q of X){let Z=Q.get(q);if(Z==="done")W.push(`✅ ${q} done`);else if(Z==="error")W.push(`❌ ${q} failed`);else if(Z==="needs-human")W.push(`\uD83D\uDD13 ${q} needs manual verification`);else W.push(`⏳ ${q}`)}if(K&&Q.size>=X.length){let q=Q.get("synthesis");if(q==="done")W.push("✅ synthesized");else if(q==="error")W.push("❌ synthesis failed");else if(q==="needs-human")W.push("⏭️ synthesis skipped");else W.push("\uD83D\uDD04 synthesizing")}$?.({content:[{type:"text",text:`**${J}...** ${W.join(" · ")}`}],details:{_progress:!0}})}}class P{text;paddingX;paddingY;constructor(X,$=0,J=0){this.text=X;this.paddingX=$;this.paddingY=J}render(X){let $=" ".repeat(this.paddingX),J="",K=Math.max(1,X-this.paddingX*2),Q=this.text.split(`
17
- `).flatMap((V)=>{if(V.length<=K)return[`${$}${V}`];let B=[];for(let W=0;W<V.length;W+=K)B.push(`${$}${V.slice(W,W+K)}`);return B});return[...Array.from({length:this.paddingY},()=>""),...Q,...Array.from({length:this.paddingY},()=>"")]}invalidate(){}}function JX(X,$){X.registerTool({name:"greedy_search",label:"Greedy Search",description:"WEB/RESEARCH SEARCH ONLY — searches live web via Perplexity, Google AI, ChatGPT, and Gemini, plus opt-in research through Semantic Scholar and Logically. "+"Research mode reuses the configured ~/.dm/greedyconfig engines for child searches and Gemini for planning/final synthesis. Research mode is the centerpiece: it plans follow-up actions, fetches sources, audits citations, and writes a structured research bundle on disk. Use for: library docs, recent framework changes, error messages, best practices, current events. Reports streaming progress as each engine completes.",promptSnippet:"Multi-engine AI web search with streaming progress",parameters:Y.Object({query:Y.String({description:"The search query"}),engine:Y.String({description:'Engine to use: "all" (default), "perplexity", "google", "chatgpt", "gemini", "gem". Research engines: "semantic-scholar" (alias "s2") and "logically". "all" fans out to the configured engines and fetches top sources. Customize via ~/.dm/greedyconfig. Bing Copilot is still available as "bing" for signed-in users.',default:"all"}),synthesize:Y.Optional(Y.Boolean({description:'Only for engine="all": synthesize the multi-engine results and fetched sources. Default: false.',default:!1})),synthesizer:Y.Optional(Y.String({description:'Synthesis engine for synthesize=true. Defaults to ~/.dm/greedyconfig synthesizer (currently "gemini" by default). Supported: "gemini", "chatgpt".'})),depth:Y.Optional(Y.String({description:'Deprecated except "research". Use depth="research" for the iterative research workflow. Research child searches use ~/.dm/greedyconfig engines; Gemini handles research planning/final synthesis. Legacy values: "fast" skips source fetching; "standard"/"deep" alias synthesize=true.'})),breadth:Y.Optional(Y.Number({description:'Only for depth="research": number of parallel research directions per round, 1-5 (default: 3).',default:3})),iterations:Y.Optional(Y.Number({description:'Only for depth="research": number of iterative research rounds, 1-3 (default: 2).',default:2})),maxSources:Y.Optional(Y.Number({description:'Only for depth="research": maximum fetched sources for the final report, 3-12.'})),researchOutDir:Y.Optional(Y.String({description:'Only for depth="research": optional directory for the structured research bundle. Defaults to .dm/greedysearch-research/<timestamp>_<query>.'})),writeResearchBundle:Y.Optional(Y.Boolean({description:'Only for depth="research": write the structured research bundle to disk (default true).',default:!0})),fullAnswer:Y.Optional(Y.Boolean({description:"When true, returns the complete answer instead of a truncated preview (default: false, answers are shortened to ~300 chars to save tokens).",default:!1})),headless:Y.Optional(Y.Boolean({description:"Set to false to show Chrome window (headless is the default). Set GREEDY_SEARCH_VISIBLE=1 to disable headless globally.",default:!0})),visible:Y.Optional(Y.Boolean({description:"Set to true to always use visible Chrome for this search. Alias for headless: false.",default:!1})),alwaysVisible:Y.Optional(Y.Boolean({description:"Set to true to keep GreedySearch in visible Chrome mode for this search. Alias for visible: true.",default:!1}))}),execute:async(J,K,Q,V)=>{let{query:B,fullAnswer:W}=K,q=S(K.engine??"all")||"all",Z=S(K.depth??""),H=Z==="research",b=Z==="fast",z=Z==="standard"||Z==="deep",M=q==="all"&&!b&&(K.synthesize===!0||z),O=H?"all":q,A=!(K.visible===!0||K.alwaysVisible===!0||K.headless===!1||process.env.GREEDY_SEARCH_VISIBLE==="1"||process.env.GREEDY_SEARCH_ALWAYS_VISIBLE==="1");if(!C($))return s();let k=[];if(W??O!=="all")k.push("--full");if(H){if(k.push("--depth","research"),typeof K.breadth==="number")k.push("--breadth",String(K.breadth));if(typeof K.iterations==="number")k.push("--iterations",String(K.iterations));if(typeof K.maxSources==="number")k.push("--max-sources",String(K.maxSources));if(typeof K.researchOutDir==="string")k.push("--research-out-dir",K.researchOutDir);if(K.writeResearchBundle===!1)k.push("--no-research-bundle")}else if(b)k.push("--fast");else if(Z==="deep")k.push("--depth","deep");else if(M)k.push("--synthesize");if(M&&typeof K.synthesizer==="string")k.push("--synthesizer",K.synthesizer);let D=O==="all"?$X(x,V,H?"Researching":"Searching",M):void 0;try{let w=await XX(O,B,k,`${$}/bin/search.mjs`,Q,D,{headless:A});return{content:[{type:"text",text:o(O,w)||"No results returned."}],details:{raw:w}}}catch(w){return e("Search failed",w)}},renderCall(J,K){let Q=(J.query||"").slice(0,60),V=Q.length<(J.query||"").length?`${Q}...`:Q,B=J.engine&&J.engine!=="all"?K.fg("dim",` (${J.engine})`):"";return new P(`${K.fg("toolTitle",K.bold("greedy_search"))} "${K.fg("accent",V)}"${B}`,0,0)},renderResult(J,{expanded:K,isPartial:Q},V){if(Q){let Z=J.content.find((b)=>b.type==="text")?.text,H=Z?Z.replace(/\*\*/g,""):"Searching...";return new P(V.fg("warning",H),0,0)}let B=J.content.find((Z)=>Z.type==="text"),W=J.details?.raw;if(!K){if(W?._needsHumanVerification)return new P(V.fg("warning"," → Manual verification required"),0,0);let H=W?._synthesis,b=W?._sources;if(H){let j=Array.isArray(b)?b.length:0,A=H.agreement?.level,k=" → Synthesized";if(j>0)k+=` · ${j} source${j>1?"s":""}`;if(A)k+=` · ${A}`;return new P(V.fg("muted",k),0,0)}let z=Object.keys(W||{}).filter((j)=>!j.startsWith("_")),M=0;for(let j of z){let k=W?.[j]?.sources;if(Array.isArray(k))M+=k.length}if(M>0)return new P(V.fg("muted",` → ${M} source${M>1?"s":""}`),0,0);let O=B?.text;if(O)return new P(V.fg("warning",` → ${O.slice(0,80)}`),0,0);return new P(V.fg("muted"," → Done"),0,0)}if(!B||B.type!=="text")return new P("",0,0);let q=B.text.split(`
6
+ `).trim()}for(let[B,q]of Object.entries(J)){if(B.startsWith("_"))continue;$.push(`
7
+ ## ${F(B)}`),QJ(q,$,3)}return $.join(`
8
+ `).trim()}function MJ(J,$){let K=$.floor,Q=K?.metrics,X=$.bundle,V=$.manifest;if(J.push("## Research Run"),J.push(`- Status: ${K?.floorMet?"floor met":"partial / floor unmet"}`),V?.terminationReason)J.push(`- Stop reason: ${String(V.terminationReason)}`);if(Q)J.push(`- Evidence: ${Q.fetchedOk||0} fetched sources, ${Q.primarySources||0} primary/official, ${Q.claims||0} claims, ${Q.cited||0} citations`),J.push(`- Questions: ${Q.closedQuestions||0}/${Q.totalQuestions||0} closed${Q.openQuestions?`, ${Q.openQuestions} open`:""}`);if(X?.dir)J.push(`- Bundle: ${String(X.dir)}`);J.push("")}function GJ(J,$){let K=J._needsHumanVerification;if(K){let Q=Array.isArray(K.engines)?K.engines.join(", "):"this engine";$.push("## Manual verification required"),$.push(String(K.message||"Visible Chrome is open. Solve the verification challenge, then rerun the same search.")),$.push(`Engines: ${Q}`),$.push("")}return QJ(J,$,5),$.join(`
9
+ `).trim()}function QJ(J,$,K){if(J.error){$.push(`Error: ${J.error}`);return}if(J.answer)$.push(String(J.answer));let Q=J.sources;if(Array.isArray(Q)&&Q.length>0){$.push(`
10
+ Sources:`);for(let X of Q.slice(0,K))$.push(`- [${X.title||X.url}](${X.url})`)}}import{spawn as SJ}from"node:child_process";import{existsSync as xJ}from"node:fs";import{join as IJ}from"node:path";import{existsSync as R,mkdirSync as CJ,readFileSync as XJ,writeFileSync as NJ}from"node:fs";import{homedir as vJ}from"node:os";import{join as WJ}from"node:path";import{tmpdir as DJ}from"node:os";function PJ(J,$=9222){let K=Number.parseInt(String(J??""),10);return Number.isInteger(K)&&K>1024&&K<65535?K:$}var LJ=DJ().replaceAll("\\","/"),W0=PJ(process.env.GREEDY_SEARCH_PORT),O=(process.env.GREEDY_SEARCH_PROFILE_DIR||process.env.CDP_PROFILE_DIR||`${LJ}/greedysearch-chrome-profile`).replaceAll("\\","/"),Z0=`${O}/DevToolsActivePort`,B0=process.env.GREEDY_SEARCH_PID_FILE||`${O}/browser.pid`,q0=process.env.CDP_PAGES_CACHE||`${O}/cdp-pages.json`,z0=process.env.GREEDY_SEARCH_MODE_FILE||`${O}/browser-mode`,k0=process.env.GREEDY_SEARCH_METADATA_FILE||`${O}/browser-metadata.json`,H0=process.env.GREEDY_SEARCH_LAUNCH_LOCK_FILE||`${O}/browser-launch.lock`,Y0=process.env.GREEDY_SEARCH_ACTIVITY_FILE||`${O}/browser-last-activity`,b0=(process.env.CDP_SOCKET_DIR||`${O}/cdp-sockets`).replaceAll("\\","/"),U0=`${O}/visible-recovery.jsonl`,E=WJ(vJ(),".dm"),G=WJ(E,"greedyconfig"),f=["perplexity","google","chatgpt","gemini"],y="gemini";function FJ(){try{if(R(G)){let J=XJ(G,"utf8"),$=JSON.parse(J);if(Array.isArray($.engines)&&$.engines.length>0&&$.engines.every((K)=>typeof K==="string")){let K=$.engines.filter((X)=>T[X]),Q=$.engines.filter((X)=>!T[X]);if(Q.length>0)process.stderr.write(`[greedysearch] Warning: ignoring unknown engine(s) in ${G}: ${Q.join(", ")}
11
+ [greedysearch] Available engines: ${Object.keys(T).join(", ")}
12
+ `);if(K.length>0)return K;process.stderr.write(`[greedysearch] Warning: no valid engines in ${G}, falling back to defaults: ${f.join(", ")}
13
+ `)}}}catch{}return f}function RJ(){try{if(!R(E))CJ(E,{recursive:!0});if(!R(G))NJ(G,JSON.stringify({engines:f,synthesizer:y},null,2)+`
14
+ `,"utf8")}catch{}}RJ();var VJ=["gemini","chatgpt"];function _J(){try{if(R(G)){let J=XJ(G,"utf8"),$=JSON.parse(J);if(typeof $.synthesizer==="string"){let K=$.synthesizer.toLowerCase();if(VJ.includes(K))return K;process.stderr.write(`[greedysearch] Warning: unknown synthesizer "${$.synthesizer}" in ${G}
15
+ [greedysearch] Available synthesizers: ${VJ.join(", ")}
16
+ [greedysearch] Falling back to default: ${y}
17
+ `)}}}catch{}return y}var T={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"},h=FJ();var j0=_J(),A0=Math.max(1,Number.parseInt(process.env.GREEDY_FETCH_CONCURRENCY||"5",10)||5);process.env.CDP_PROFILE_DIR=O;function g(J){return J.replace(/^"|"$/g,"")}function _(J){return xJ(IJ(J,"bin","cdp.mjs"))}function ZJ(){return{content:[{type:"text",text:"cdp.mjs missing — try reinstalling: pi install git:github.com/apmantza/greedysearch-dm"}],details:{}}}function BJ(J,$){let K=$ instanceof Error?$.message:String($);return{content:[{type:"text",text:`${J}: ${K}`}],details:{}}}function qJ(J,$,K,Q,X,V,B={}){return new Promise((q,H)=>{let{headless:Z=!0}=B,Y=[...K];if(Z!==!1&&process.env.GREEDY_SEARCH_VISIBLE!=="1")Y.push("--headless");if(Z===!1)Y.push("--always-visible");let z={...process.env};if(Z===!1)z.GREEDY_SEARCH_VISIBLE="1",z.GREEDY_SEARCH_ALWAYS_VISIBLE="1";let W=SJ(L(),[Q,J,"--inline","--stdin",...Y],{stdio:["pipe","pipe","pipe"],env:z});W.stdin.write($),W.stdin.end();let b="",U="",w=51200,A=!1,j=900000,S=parseInt(process.env.GREEDY_SEARCH_TIMEOUT_MS||"",10),D=Number.isNaN(S)?j:S,C=setTimeout(()=>{if(A)return;if(A=!0,X?.removeEventListener("abort",P),process.platform==="win32")W.kill();else W.kill("SIGTERM");H(Error(`greedy-search child timed out after ${D}ms (watchdog)`))},D);C.unref();let P=()=>{if(A)return;A=!0,clearTimeout(C),W.kill("SIGTERM"),H(Error("Aborted"))};X?.addEventListener("abort",P,{once:!0}),W.stderr.on("data",(N)=>{if(U+=N,U.length>w)U=U.slice(-w);let bJ=/^PROGRESS:(perplexity|google|chatgpt|bing|gemini|semantic-scholar|semanticscholar|s2|logically):(done|error|needs-human)$/;for(let v of N.toString().split(`
18
+ `)){let x=v.match(bJ);if(x&&V)V({type:"engine",engine:x[1],status:x[2]});let c=v.match(/^PROGRESS:synthesis:(done|error|skipped)$/);if(c&&V)V({type:"engine",engine:"synthesis",status:c[1]});let o=v.match(/^PROGRESS:research:(.+)$/);if(o&&V)V({type:"text",text:o[1]});let i=v.match(/^\[greedysearch\] (\[.+?\] .+)$/);if(i&&V)V({type:"text",text:i[1]});let I=v.match(/^\[(perplexity|google|chatgpt|bing|gemini|semantic-scholar|logically)\] stage: (.+) \(\+\d+ms\)$/);if(I&&V)V({type:"text",text:`${I[1]}: ${I[2]}`})}}),W.stdout.on("data",(N)=>b+=N),W.on("close",(N)=>{if(A)return;if(A=!0,clearTimeout(C),X?.removeEventListener("abort",P),N!==0)H(Error(U.trim()||`search.mjs exited with code ${N}`));else{if(V&&J!=="all")V({type:"engine",engine:J,status:"done"});try{q(JSON.parse(b.trim()))}catch{H(Error(`Invalid JSON from search.mjs: ${b.slice(0,200)}`))}}})})}function TJ(J,$){if($<=0)return"";let K=16,Q=Math.round(J/$*K);return"["+"█".repeat(Math.min(Q,K))+"░".repeat(Math.max(0,K-Q))+"]"}function EJ(J){if(J<1000)return"—";let $=Math.round(J/1000);if($<60)return`${$}s`;return`${Math.floor($/60)}m ${$%60}s`}function zJ(J,$,K,Q,X){let V=Date.now(),B=new Map,q="";function H(){let Z=[];Z.push(`**${K}...** ${X||""}`.trim());let Y=B.size;if(J.length>1&&Y>0&&!q){let W=Date.now()-V,b=Math.min(1,Y/J.length),U=b>0.01?Math.round(W/b-W):null,w=TJ(Y,J.length),A=U!=null&&U>0?EJ(U):"—";Z.push(`${w} ${Y}/${J.length} engines (ETA ${A})`)}if(q)Z.push(q);let z=[];for(let W of J){let b=B.get(W);if(b==="done")z.push(`✅ ${W} done`);else if(b==="error")z.push(`❌ ${W} failed`);else if(b==="needs-human")z.push(`\uD83D\uDD13 ${W} needs manual verification`);else z.push(`⏳ ${W}`)}if(Q&&Y>=J.length){let W=B.get("synthesis");if(W==="done")z.push("✅ synthesized");else if(W==="error")z.push("❌ synthesis failed");else if(W==="needs-human")z.push("⏭️ synthesis skipped");else z.push("\uD83D\uDD04 synthesizing")}if(z.length>0){let W=z.join(" · ");Z.push(W.length>90?W.slice(0,88)+"":W)}$?.({content:[{type:"text",text:Z.join(`
19
+ `)}],details:{_progress:!0}})}return(Z)=>{if(Z.type==="text"){if(Z.text.startsWith("["))q=Z.text;H();return}let{engine:Y,status:z}=Z;B.set(Y,z),H()}}class M{text;paddingX;paddingY;constructor(J,$=0,K=0){this.text=J;this.paddingX=$;this.paddingY=K}render(J){let $=" ".repeat(this.paddingX),K="",Q=Math.max(1,J-this.paddingX*2),X=this.text.split(`
20
+ `).flatMap((V)=>{if(V.length<=Q)return[`${$}${V}`];let B=[];for(let q=0;q<V.length;q+=Q)B.push(`${$}${V.slice(q,q+Q)}`);return B});return[...Array.from({length:this.paddingY},()=>""),...X,...Array.from({length:this.paddingY},()=>"")]}invalidate(){}}function kJ(J,$){J.registerTool({name:"greedy_search",label:"Greedy Search",description:"WEB/RESEARCH SEARCH ONLY — searches live web via Perplexity, Google AI, ChatGPT, and Gemini, plus opt-in research through Semantic Scholar and Logically. "+"Research mode reuses the configured ~/.dm/greedyconfig engines for child searches and Gemini for planning/final synthesis. Research mode is the centerpiece: it plans follow-up actions, fetches sources, audits citations, and writes a structured research bundle on disk. Scale-aware: simple queries auto-classify and use a fast single-pass path. Use for: library docs, recent framework changes, error messages, best practices, current events. Reports streaming progress as each engine completes.",promptSnippet:"Multi-engine AI web search with streaming progress",parameters:k.Object({query:k.String({description:"The search query"}),engine:k.String({description:'Engine to use: "all" (default), "perplexity", "google", "chatgpt", "gemini", "gem". Research engines: "semantic-scholar" (alias "s2") and "logically". "all" fans out to the configured engines and fetches top sources. Customize via ~/.dm/greedyconfig. Bing Copilot is still available as "bing" for signed-in users.',default:"all"}),synthesize:k.Optional(k.Boolean({description:'Only for engine="all": synthesize the multi-engine results and fetched sources. Default: false.',default:!1})),synthesizer:k.Optional(k.String({description:'Synthesis engine for synthesize=true. Defaults to ~/.dm/greedyconfig synthesizer (currently "gemini" by default). Supported: "gemini", "chatgpt".'})),depth:k.Optional(k.String({description:'Deprecated except "research". Use depth="research" for the iterative research workflow. Research child searches use ~/.dm/greedyconfig engines; Gemini handles research planning/final synthesis. Legacy values: "fast" skips source fetching; "standard"/"deep" alias synthesize=true.'})),breadth:k.Optional(k.Number({description:'Only for depth="research": number of parallel research directions per round, 1-5 (default: 3).',default:3})),iterations:k.Optional(k.Number({description:'Only for depth="research": number of iterative research rounds, 1-3 (default: 2).',default:2})),maxSources:k.Optional(k.Number({description:'Only for depth="research": maximum fetched sources for the final report, 3-12.'})),researchOutDir:k.Optional(k.String({description:'Only for depth="research": optional directory for the structured research bundle. Defaults to .dm/greedysearch-research/<timestamp>_<query>.'})),writeResearchBundle:k.Optional(k.Boolean({description:'Only for depth="research": write the structured research bundle to disk (default true).',default:!0})),fullAnswer:k.Optional(k.Boolean({description:"When true, returns the complete answer instead of a truncated preview (default: false, answers are shortened to ~300 chars to save tokens).",default:!1})),headless:k.Optional(k.Boolean({description:"Set to false to show Chrome window (headless is the default). Set GREEDY_SEARCH_VISIBLE=1 to disable headless globally.",default:!0})),visible:k.Optional(k.Boolean({description:"Set to true to always use visible Chrome for this search. Alias for headless: false.",default:!1})),alwaysVisible:k.Optional(k.Boolean({description:"Set to true to keep GreedySearch in visible Chrome mode for this search. Alias for visible: true.",default:!1}))}),execute:async(K,Q,X,V)=>{let{query:B,fullAnswer:q}=Q,H=g(Q.engine??"all")||"all",Z=g(Q.depth??""),Y=Z==="research",z=Z==="fast",W=Z==="standard"||Z==="deep",b=H==="all"&&!z&&(Q.synthesize===!0||W),U=Y?"all":H,A=!(Q.visible===!0||Q.alwaysVisible===!0||Q.headless===!1||process.env.GREEDY_SEARCH_VISIBLE==="1"||process.env.GREEDY_SEARCH_ALWAYS_VISIBLE==="1");if(!_($))return ZJ();let j=[];if(q??U!=="all")j.push("--full");if(Y){if(j.push("--depth","research"),typeof Q.breadth==="number")j.push("--breadth",String(Q.breadth));if(typeof Q.iterations==="number")j.push("--iterations",String(Q.iterations));if(typeof Q.maxSources==="number")j.push("--max-sources",String(Q.maxSources));if(typeof Q.researchOutDir==="string")j.push("--research-out-dir",Q.researchOutDir);if(Q.writeResearchBundle===!1)j.push("--no-research-bundle")}else if(z)j.push("--fast");else if(Z==="deep")j.push("--depth","deep");else if(b)j.push("--synthesize");if(b&&typeof Q.synthesizer==="string")j.push("--synthesizer",Q.synthesizer);let D=zJ(U==="all"?h:[U],V,Y?"Researching":"Searching",b&&U==="all",B);try{let C=await qJ(U,B,j,`${$}/bin/search.mjs`,X,D,{headless:A});return{content:[{type:"text",text:KJ(U,C)||"No results returned."}],details:{raw:C}}}catch(C){return BJ("Search failed",C)}},renderCall(K,Q){let X=(K.query||"").slice(0,60),V=X.length<(K.query||"").length?`${X}...`:X,B=K.engine&&K.engine!=="all"?Q.fg("dim",` (${K.engine})`):"";return new M(`${Q.fg("toolTitle",Q.bold("greedy_search"))} "${Q.fg("accent",V)}"${B}`,0,0)},renderResult(K,{expanded:Q,isPartial:X},V){if(X){let Z=K.content.find((z)=>z.type==="text")?.text,Y=Z?Z.replace(/\*\*/g,""):"Searching...";return new M(V.fg("warning",Y),0,0)}let B=K.content.find((Z)=>Z.type==="text"),q=K.details?.raw;if(!Q){if(q?._needsHumanVerification)return new M(V.fg("warning"," → Manual verification required"),0,0);let Y=q?._synthesis,z=q?._sources;if(Y){let w=Array.isArray(z)?z.length:0,A=Y.agreement?.level,j=" → Synthesized";if(w>0)j+=` · ${w} source${w>1?"s":""}`;if(A)j+=` · ${A}`;return new M(V.fg("muted",j),0,0)}let W=Object.keys(q||{}).filter((w)=>!w.startsWith("_")),b=0;for(let w of W){let j=q?.[w]?.sources;if(Array.isArray(j))b+=j.length}if(b>0)return new M(V.fg("muted",` → ${b} source${b>1?"s":""}`),0,0);let U=B?.text;if(U)return new M(V.fg("warning",` → ${U.slice(0,80)}`),0,0);return new M(V.fg("muted"," → Done"),0,0)}if(!B||B.type!=="text")return new M("",0,0);let H=B.text.split(`
18
21
  `).map((Z)=>V.fg("toolOutput",Z)).join(`
19
- `);return new P(`
20
- ${q}`,0,0)}})}var y=CX(NX(import.meta.url));function _X(X){X.on("session_start",async($,J)=>{if(!C(y))J.ui.notify("GreedySearch: cdp.mjs missing from package directory — try reinstall DM or rebuild the bundled GreedySearch asset","warning")}),JX(X,y),X.registerCommand("greedy-visible",{description:"Launch GreedySearch Chrome in visible mode for captcha/login/cookie setup.",handler:async($,J)=>{await E([],J,"Visible GreedySearch Chrome launched.")}}),X.registerCommand("greedy-status",{description:"Show GreedySearch Chrome status.",handler:async($,J)=>{await E(["--status"],J)}}),X.registerCommand("greedy-kill",{description:"Stop GreedySearch Chrome.",handler:async($,J)=>{await E(["--kill"],J,"GreedySearch Chrome stopped.")}}),X.registerCommand("set-greedy-locale",{description:"Set default locale for GreedySearch results (e.g., /set-greedy-locale de, /set-greedy-locale --clear, /set-greedy-locale --show)",handler:async($,J)=>{let K=$.trim()||"--show";if(K==="--show"){let W=f();if(W.locale)J.ui.notify(`Default locale: ${W.locale}`,"info");else J.ui.notify("No default locale (uses: en)","info");return}if(K==="--clear"){let W=f();delete W.locale,KX(W),J.ui.notify("Default locale cleared (now uses: en).","info");return}let Q=K.toLowerCase(),V=["en","de","fr","es","it","pt","nl","pl","ru","ja","ko","zh","ar","hi","tr","sv","da","no","fi","cs","hu","ro","el"];if(!V.includes(Q)){J.ui.notify(`Invalid locale "${Q}". Valid: ${V.join(", ")}`,"error");return}let B=f();B.locale=Q,KX(B),J.ui.notify(`Default locale set to: ${Q}`,"info")}})}var QX=h(vX(),".config","greedysearch"),g=h(QX,"config.json");async function E(X,$,J){let K=h(y,"bin","visible.mjs"),{code:Q,output:V}=await new Promise((B)=>{let W=wX(L(),[K,...X],{stdio:["ignore","pipe","pipe"],env:{...process.env,GREEDY_SEARCH_VISIBLE:"1"}}),q="";W.stdout.on("data",(Z)=>q+=Z.toString()),W.stderr.on("data",(Z)=>q+=Z.toString()),W.on("close",(Z)=>B({code:Z,output:q}))});if(Q===0)$.ui.notify((J||V.trim()||"Done.").trim(),"info");else $.ui.notify(V.trim()||`GreedySearch Chrome command failed (${Q})`,"error")}function f(){try{if(DX(g))return JSON.parse(LX(g,"utf8"))}catch{}return{}}function KX(X){GX(QX,{recursive:!0}),RX(g,JSON.stringify(X,null,2),"utf8")}export{_X as default};
22
+ `);return new M(`
23
+ ${H}`,0,0)}})}var m=mJ(dJ(import.meta.url));function lJ(J){J.on("session_start",async($,K)=>{if(!_(m))K.ui.notify("GreedySearch: cdp.mjs missing from package directory — try reinstalling: pi install git:github.com/apmantza/greedysearch-dm","warning")}),kJ(J,m),J.registerCommand("greedy-visible",{description:"Launch GreedySearch Chrome in visible mode for captcha/login/cookie setup.",handler:async($,K)=>{await p([],K,"Visible GreedySearch Chrome launched.")}}),J.registerCommand("greedy-status",{description:"Show GreedySearch Chrome status.",handler:async($,K)=>{await p(["--status"],K)}}),J.registerCommand("greedy-kill",{description:"Stop GreedySearch Chrome.",handler:async($,K)=>{await p(["--kill"],K,"GreedySearch Chrome stopped.")}}),J.registerCommand("set-greedy-locale",{description:"Set default locale for GreedySearch results (e.g., /set-greedy-locale de, /set-greedy-locale --clear, /set-greedy-locale --show)",handler:async($,K)=>{let Q=$.trim()||"--show";if(Q==="--show"){let q=u();if(q.locale)K.ui.notify(`Default locale: ${q.locale}`,"info");else K.ui.notify("No default locale (uses: en)","info");return}if(Q==="--clear"){let q=u();delete q.locale,HJ(q),K.ui.notify("Default locale cleared (now uses: en).","info");return}let X=Q.toLowerCase(),V=["en","de","fr","es","it","pt","nl","pl","ru","ja","ko","zh","ar","hi","tr","sv","da","no","fi","cs","hu","ro","el"];if(!V.includes(X)){K.ui.notify(`Invalid locale "${X}". Valid: ${V.join(", ")}`,"error");return}let B=u();B.locale=X,HJ(B),K.ui.notify(`Default locale set to: ${X}`,"info")}})}var YJ=l(uJ(),".config","greedysearch"),d=l(YJ,"config.json");async function p(J,$,K){let Q=l(m,"bin","visible.mjs"),{code:X,output:V}=await new Promise((B)=>{let q=fJ(L(),[Q,...J],{stdio:["ignore","pipe","pipe"],env:{...process.env,GREEDY_SEARCH_VISIBLE:"1"}}),H="";q.stdout.on("data",(Z)=>H+=Z.toString()),q.stderr.on("data",(Z)=>H+=Z.toString()),q.on("close",(Z)=>B({code:Z,output:H}))});if(X===0)$.ui.notify((K||V.trim()||"Done.").trim(),"info");else $.ui.notify(V.trim()||`GreedySearch Chrome command failed (${X})`,"error")}function u(){try{if(yJ(d))return JSON.parse(gJ(d,"utf8"))}catch{}return{}}function HJ(J){hJ(YJ,{recursive:!0}),pJ(d,JSON.stringify(J,null,2),"utf8")}export{lJ as default};
@@ -1,16 +1,21 @@
1
1
  {
2
2
  "name": "greedysearch-dm",
3
- "version": "2.0.0",
4
- "description": "DM extension: multi-engine AI search via browser automation with optional Gemini synthesis",
3
+ "version": "2.1.6",
4
+ "description": "DM extension for headless multi-engine browser search, grounded source fetching, synthesis, and research",
5
5
  "type": "module",
6
6
  "keywords": [
7
7
  "duckmind",
8
8
  "dm",
9
+ "greedysearch",
9
10
  "web-search",
10
- "browser-automation"
11
+ "browser-automation",
12
+ "headless"
11
13
  ],
12
14
  "author": "Apostolos Mantzaris",
13
15
  "license": "MIT",
16
+ "engines": {
17
+ "node": ">=20.11.0"
18
+ },
14
19
  "dm": {
15
20
  "extensions": [
16
21
  "./index.js"
@@ -1,18 +1,18 @@
1
1
  ---
2
2
  name: greedy-search
3
- description: Web/search plus opt-in research via Perplexity, Google AI, ChatGPT, Gemini, Semantic Scholar, and Logically. Grounded all-engine search fetches sources by default; optional configurable synthesis; deep research as separate workflow. Configurable via ~/.dm/greedyconfig. Bing Copilot available for signed-in users. Current docs, recent changes, dependency choices. NOT codebase search.
3
+ description: Headless-first web and research search through DM using Perplexity, Google AI, ChatGPT, Gemini, Semantic Scholar, Logically, and signed-in Bing Copilot. Grounded all-engine search fetches sources by default; synthesis and deep research are opt-in. Engine and synthesizer settings use ~/.dm/greedyconfig. Not for local codebase search.
4
4
  ---
5
5
 
6
- `greedy_search({ query, engine: "all"|"perplexity"|"google"|"chatgpt"|"gemini"|"semantic-scholar"|"logically"|"bing", synthesize?: bool, synthesizer?: "gemini"|"chatgpt", depth?: "research", breadth: 1-5, iterations: 1-3, maxSources: 3-12, researchOutDir?: string, writeResearchBundle?: bool, visible: bool })`
6
+ `greedy_search({ query, engine?: "all"|"perplexity"|"google"|"chatgpt"|"gemini"|"semantic-scholar"|"logically"|"bing", synthesize?: bool, synthesizer?: "gemini"|"chatgpt", depth?: "research", breadth?: 1-5, iterations?: 1-3, maxSources?: 3-12, researchOutDir?: string, writeResearchBundle?: bool, fullAnswer?: bool, headless?: bool, visible?: bool, alwaysVisible?: bool })`
7
7
 
8
- **Modes:** individual engine search · grounded `engine:"all"` search with fetched sources · optional `synthesize:true` using the configured synthesizer over all-engine results · `depth:"research"` for the iterative deep-research workflow.
8
+ **Browser mode:** Headless is the default. Set `headless:false`, `visible:true`, or `alwaysVisible:true` only when a visible browser is required. `GREEDY_SEARCH_VISIBLE=1` is the explicit global visible-mode override.
9
9
 
10
- **Config:** `~/.dm/greedyconfig` supports `{ "engines": ["perplexity", "google", "chatgpt", "gemini", "semantic-scholar", "logically"], "synthesizer": "gemini" }`. Gemini is a normal search engine; Semantic Scholar and Logically are opt-in research engines. Any configured engine can participate in `engine:"all"`; deep research child searches reuse the same configured `engines` list and stdin-safe query passing. Normal all-search synthesis remains controlled separately by `synthesizer`; research planning/final synthesis uses Gemini.
10
+ **Search modes:** Use one engine for a direct search. Use `engine:"all"` for configured multi-engine fan-out with fetched sources. Add `synthesize:true` for a combined answer. Use `depth:"research"` for iterative planning, source fetching, citation checks, and a structured research report.
11
11
 
12
- **Compatibility:** legacy `depth:"fast"|"standard"|"deep"` is still accepted. `fast` skips source fetching; `standard`/`deep` alias `synthesize:true`. Prefer `synthesize:true`, optional `synthesizer`, and `depth:"research"` going forward.
12
+ **Configuration:** `~/.dm/greedyconfig` controls the `engine:"all"` list and default synthesizer, for example `{ "engines": ["perplexity", "google", "chatgpt", "gemini"], "synthesizer": "gemini" }`.
13
13
 
14
- **Research output:** `depth:"research"` writes a dataroom-style bundle by default under `.dm/greedysearch-research/<timestamp>_<query>/` with `STATUS.md`, `OUTLINE.md`, `reports/SUMMARY.md`, `reports/CLAIMS.md`, `reports/GAPS.md`, `sources/`, and `data/manifest.json`. Pass `researchOutDir` to choose the directory or `writeResearchBundle:false` to disable disk output.
14
+ **Research output:** `depth:"research"` writes a structured bundle under `.dm/greedysearch-research/<timestamp>_<query>/` by default. Set `researchOutDir` to choose another directory or `writeResearchBundle:false` to disable disk output.
15
15
 
16
- **Auto-recovery:** Headless default. Bing/Perplexity auto-retry visible on CF block. Manual CAPTCHA visible stays open; solve then rerun.
16
+ **Compatibility:** Legacy `depth:"fast"|"standard"|"deep"` remains accepted by the imported runtime. Prefer `synthesize:true` and `depth:"research"` in new calls.
17
17
 
18
- **CDP safety:** Use `bin/cdp-greedy.mjs` only. Never raw `bin/cdp.mjs`.
18
+ **CDP safety:** Use the bundled `bin/cdp-greedy.mjs` lane. Browser state is scoped through the DM-provided port, profile, PID, and mode environment variables. The launcher refuses to kill a listener unless its command line matches both the expected profile and debugging port.
@@ -1,3 +1,3 @@
1
- import{Readability as D}from"@mozilla/readability";import{JSDOM as H}from"jsdom";import S from"turndown";var C=new S({headingStyle:"atx",bulletListMarker:"-",codeBlockStyle:"fenced"});C.addRule("removeDataUrls",{filter:(j)=>j.tagName==="IMG"&&j.getAttribute("src")?.startsWith("data:"),replacement:()=>""});var v="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36",b={"user-agent":v,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"},_=[/^localhost$/i,/^127\.\d+\.\d+\.\d+$/,/^0\.0\.0\.0$/,/^\[::1\]$/,/^10\./,/^172\.(1[6-9]|2\d|3[01])\./,/^192\.168\./,/^169\.254\./,/^fc00:/i,/^fe80:/i,/\.local$/i,/\.internal$/i,/\.localhost$/i];function m(j={}){return{...b,...j}}function y(j){try{let z=new URL(j),$=z.hostname.toLowerCase();for(let K of _)if(K.test($))return{blocked:!0,reason:`Private/internal address: ${$}`};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 M(j){try{let z=new URL(j);if(!(z.hostname==="github.com"||z.hostname.endsWith(".github.com")))return j;let $=z.pathname.split("/").filter(Boolean);if($.length<5)return j;let[K,J,Y,V,...Q]=$;if(Y!=="blob")return j;let W=Q.join("/");return`https://raw.githubusercontent.com/${K}/${J}/${V}/${W}`}catch{return j}}async function p(j,z={}){let $=y(j);if($.blocked)return{ok:!1,url:j,finalUrl:j,status:403,error:`Blocked: ${$.reason}`,needsBrowser:!1};let K=j;if(j=M(j),j!==K)console.error(`[fetcher] Rewrote GitHub URL: ${K.slice(0,60)}... → raw.githubusercontent.com`);let{timeoutMs:J=15000,userAgent:Y,signal:V}=z,Q=new AbortController,W=setTimeout(()=>Q.abort(),J);if(V)V.addEventListener("abort",()=>Q.abort(),{once:!0});try{let Z=await fetch(j,{method:"GET",headers:{...b,"user-agent":Y||v},redirect:"follow",signal:Q.signal});clearTimeout(W);let B=Z.headers.get("content-type")||"",X=Z.url,F=Z.headers.get("last-modified")||"",I=!1;try{I=new URL(X).hostname.toLowerCase()==="raw.githubusercontent.com"}catch{}if(B.includes("text/plain")&&I){let G=await Z.text();return{ok:!0,url:K,finalUrl:X,status:Z.status,title:X.split("/").pop()||"GitHub File",byline:"",siteName:"GitHub",lang:"",publishedTime:F,lastModified:F,markdown:G,contentLength:G.length,excerpt:G.slice(0,300).replaceAll(/\n/g," "),needsBrowser:!1}}if(!B.includes("text/html")&&!B.includes("application/xhtml"))return{ok:!1,url:j,finalUrl:X,status:Z.status,error:`Unsupported content type: ${B}`,needsBrowser:!1};let L=await Z.text(),N=w(Z.status,L,X,j);if(N.blocked)return{ok:!1,url:j,finalUrl:X,status:Z.status,error:`Blocked: ${N.reason}`,needsBrowser:!0};let O=T(L,X),P=x(O);if(!P.ok)return{ok:!1,url:j,finalUrl:X,status:Z.status,error:`Low quality content: ${P.reason}`,needsBrowser:!0};return{ok:!0,url:j,finalUrl:X,status:Z.status,title:O.title,byline:O.byline,siteName:O.siteName,lang:O.lang,publishedTime:O.publishedTime||F,lastModified:F,markdown:O.markdown,excerpt:O.excerpt,contentLength:O.markdown.length,needsBrowser:!1}}catch(Z){clearTimeout(W);let B=E(Z);return{ok:!1,url:j,finalUrl:j,status:0,error:Z.message,needsBrowser:B}}}function w(j,z,$,K){let J=z.match(/<title[^>]*>([^<]*)<\/title>/i)?.[1]?.toLowerCase()||"",Y=z.slice(0,30000).toLowerCase(),V=`${J} ${Y}`;if(j===403||j===429||j===503)return{blocked:!0,reason:`HTTP ${j}`};let Q=[{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 Z of Q)if(Z.pattern.test(V))return{blocked:!0,reason:Z.reason};let W=g(K,$,z);if(W)return{blocked:!0,reason:W};return{blocked:!1}}var A=["accounts.google.com","login.microsoftonline.com","login.live.com","auth0.com","okta.com","auth.mozilla.auth0.com","id.atlassian.com"],R=["login.","signin.","auth.","sso.","accounts.","idp."],k=["sign in to continue","log in to continue","authentication required","create an account to continue","subscribe to continue reading","members only"];function g(j,z,$){try{let K=new URL(j),J=new URL(z);if(K.hostname.toLowerCase()===J.hostname.toLowerCase())return;let Y=J.hostname.toLowerCase();if(A.some((Q)=>Y===Q||Y.endsWith(`.${Q}`)))return`redirected to login (${J.hostname})`;if(R.some((Q)=>Y.startsWith(Q)))return`redirected to login (${J.hostname})`;let V=$.slice(0,20000).toLowerCase();if(k.some((Q)=>V.includes(Q)))return`redirected to login page (${J.hostname})`}catch{}return}function E(j){let z=j.message.toLowerCase();return z.includes("fetch failed")||z.includes("unable to verify")||z.includes("certificate")||z.includes("timeout")}function q(j){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 $ of z){let K=j.querySelector($),J=K?.getAttribute("content")||K?.getAttribute("datetime")||"";if(J)return J}return""}function T(j,z){let K=new H(j,{url:z}).window.document,Y=new D(K).parse();if(Y&&Y.content){let W=C.turndown(Y.content).replaceAll(/\n{3,}/g,`
1
+ import{createRequire as y}from"node:module";var C=y(import.meta.url);var L=null;async function A(){if(L)return L;let[{Readability:K},{JSDOM:Y},{default:j}]=await Promise.all([import("@mozilla/readability"),import("jsdom"),import("turndown")]),Z=new j({headingStyle:"atx",bulletListMarker:"-",codeBlockStyle:"fenced"});return Z.addRule("removeDataUrls",{filter:($)=>$.tagName==="IMG"&&$.getAttribute("src")?.startsWith("data:"),replacement:()=>""}),L={Readability:K,JSDOM:Y,turndown:Z},L}var w="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36",g={"user-agent":w,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"},k=[/^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],P=/^::ffff:(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/i,R=/^::ffff:([0-9a-f]{1,4}):([0-9a-f]{1,4})$/i,_=/^(?:0x[0-9a-f]+|0[0-7]*|[1-9][0-9]*)$/i;function M(K){if(!K||K.includes(":"))return null;let Y=K.split(".");if(Y.length<1||Y.length>4)return null;if(!Y.every(($)=>_.test($)))return null;let j=Y.map(($)=>{if(/^0x/i.test($))return parseInt($,16);if(/^0[0-7]+$/.test($))return parseInt($,8);return parseInt($,10)});if(j.some(($)=>!Number.isFinite($)||$<0))return null;let Z;if(j.length===1){if(j[0]>4294967295)return null;Z=[j[0]>>>24&255,j[0]>>>16&255,j[0]>>>8&255,j[0]&255]}else if(j.length===2){if(j[0]>255||j[1]>16777215)return null;Z=[j[0],j[1]>>>16&255,j[1]>>>8&255,j[1]&255]}else if(j.length===3){if(j[0]>255||j[1]>255||j[2]>65535)return null;Z=[j[0],j[1],j[2]>>>8&255,j[2]&255]}else{if(j.some(($)=>$>255))return null;Z=j}return Z.join(".")}function E(K){let Y=K.match(P);if(Y)return`${Y[1]}.${Y[2]}.${Y[3]}.${Y[4]}`;let j=K.match(R);if(j){let Z=parseInt(j[1],16),$=parseInt(j[2],16);if(Z>65535||$>65535)return null;return[Z>>>8&255,Z&255,$>>>8&255,$&255].join(".")}return null}function D(K){return k.some((Y)=>Y.test(K))}function o(K={}){return{...g,...K}}function b(K){try{if(typeof K!=="string"||!K.trim())return{blocked:!0,reason:"URL must be a non-empty string"};let Y=new URL(K);if(Y.protocol!=="http:"&&Y.protocol!=="https:")return{blocked:!0,reason:`Protocol not allowed: ${Y.protocol}`};let j=Y.hostname.toLowerCase();for(let $ of k)if($.test(j))return{blocked:!0,reason:`Private/internal address: ${j}`};if(j.startsWith("[")&&j.endsWith("]")){let $=j.slice(1,-1),W=E($);if(W&&D(W))return{blocked:!0,reason:`Private/internal address: ${j} (maps to ${W})`}}let Z=M(j);if(Z&&D(Z))return{blocked:!0,reason:`Private/internal address: ${j} (normalizes to ${Z})`};return{blocked:!1}}catch(Y){return{blocked:!0,reason:`Invalid URL: ${Y.message}`}}}function x(K){try{let Y=new URL(K);if(!(Y.hostname==="github.com"||Y.hostname.endsWith(".github.com")))return K;let j=Y.pathname.split("/").filter(Boolean);if(j.length<5)return K;let[Z,$,W,X,...z]=j;if(W!=="blob")return K;let J=z.join("/");return`https://raw.githubusercontent.com/${Z}/${$}/${X}/${J}`}catch{return K}}async function n(K,Y={}){let j=b(K);if(j.blocked)return{ok:!1,url:K,finalUrl:K,status:403,error:`Blocked: ${j.reason}`,needsBrowser:!1};let Z=K;if(K=x(K),K!==Z)console.error(`[fetcher] Rewrote GitHub URL: ${Z.slice(0,60)}... → raw.githubusercontent.com`);let{timeoutMs:$=15000,userAgent:W,signal:X}=Y,z=new AbortController,J=setTimeout(()=>z.abort(),$);if(X)X.addEventListener("abort",()=>z.abort(),{once:!0});try{let Q=await fetch(K,{method:"GET",headers:{...g,"user-agent":W||w},redirect:"follow",signal:z.signal});clearTimeout(J);let B=Q.headers.get("content-type")||"",V=Q.url,F=Q.headers.get("last-modified")||"",q=b(V);if(q.blocked)return{ok:!1,url:K,finalUrl:V,status:Q.status,error:`Blocked: ${q.reason}`,needsBrowser:!1};let H=!1;try{H=new URL(V).hostname.toLowerCase()==="raw.githubusercontent.com"}catch{}if(B.includes("text/plain")&&H){let O=await Q.text();return{ok:!0,url:Z,finalUrl:V,status:Q.status,title:V.split("/").pop()||"GitHub File",byline:"",siteName:"GitHub",lang:"",publishedTime:F,lastModified:F,markdown:O,contentLength:O.length,excerpt:O.slice(0,300).replaceAll(/\n/g," "),needsBrowser:!1}}if(!B.includes("text/html")&&!B.includes("application/xhtml"))return{ok:!1,url:K,finalUrl:V,status:Q.status,error:`Unsupported content type: ${B}`,needsBrowser:!1};let I=await Q.text(),N=T(Q.status,I,V,K);if(N.blocked)return{ok:!1,url:K,finalUrl:V,status:Q.status,error:`Blocked: ${N.reason}`,needsBrowser:!0};let G=await c(I,V),S=d(G);if(!S.ok)return{ok:!1,url:K,finalUrl:V,status:Q.status,error:`Low quality content: ${S.reason}`,needsBrowser:!0};return{ok:!0,url:K,finalUrl:V,status:Q.status,title:G.title,byline:G.byline,siteName:G.siteName,lang:G.lang,publishedTime:G.publishedTime||F,lastModified:F,markdown:G.markdown,excerpt:G.excerpt,contentLength:G.markdown.length,needsBrowser:!1}}catch(Q){clearTimeout(J);let B=m(Q);return{ok:!1,url:K,finalUrl:K,status:0,error:Q.message,needsBrowser:B}}}function T(K,Y,j,Z){let $=Y.match(/<title[^>]*>([^<]*)<\/title>/i)?.[1]?.toLowerCase()||"",W=Y.slice(0,30000).toLowerCase(),X=`${$} ${W}`;if(K===403||K===429||K===503)return{blocked:!0,reason:`HTTP ${K}`};let z=[{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 z)if(Q.pattern.test(X))return{blocked:!0,reason:Q.reason};let J=p(Z,j,Y);if(J)return{blocked:!0,reason:J};return{blocked:!1}}var f=["accounts.google.com","login.microsoftonline.com","login.live.com","auth0.com","okta.com","auth.mozilla.auth0.com","id.atlassian.com"],U=["login.","signin.","auth.","sso.","accounts.","idp."],h=["sign in to continue","log in to continue","authentication required","create an account to continue","subscribe to continue reading","members only"];function p(K,Y,j){try{let Z=new URL(K),$=new URL(Y);if(Z.hostname.toLowerCase()===$.hostname.toLowerCase())return;let W=$.hostname.toLowerCase();if(f.some((z)=>W===z||W.endsWith(`.${z}`)))return`redirected to login (${$.hostname})`;if(U.some((z)=>W.startsWith(z)))return`redirected to login (${$.hostname})`;let X=j.slice(0,20000).toLowerCase();if(h.some((z)=>X.includes(z)))return`redirected to login page (${$.hostname})`}catch{}return}function m(K){let Y=K.message.toLowerCase();return Y.includes("fetch failed")||Y.includes("unable to verify")||Y.includes("certificate")||Y.includes("timeout")}function v(K){let Y=['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 j of Y){let Z=K.querySelector(j),$=Z?.getAttribute("content")||Z?.getAttribute("datetime")||"";if($)return $}return""}async function c(K,Y){let{Readability:j,JSDOM:Z,turndown:$}=await A(),W=new Z(K,{url:Y});try{let X=W.window.document,J=new j(X).parse();if(J&&J.content){let V=$.turndown(J.content).replaceAll(/\n{3,}/g,`
2
2
 
3
- `).trim(),Z=Y.publishedTime||q(K)||"";return{title:Y.title||K.title||z,byline:Y.byline||"",siteName:Y.siteName||"",lang:Y.lang||"",publishedTime:Z,markdown:W,excerpt:W.slice(0,300).replaceAll(/\n/g," ")}}let V=K.body;if(V){let Q=V.cloneNode(!0);Q.querySelectorAll("script, style, nav, footer, header, aside").forEach((B)=>B.remove());let Z=(Q.textContent||"").replaceAll(/\s+/g," ").trim();return{title:K.title||z,byline:"",siteName:"",lang:"",publishedTime:q(K),markdown:Z,excerpt:Z.slice(0,300)}}return{title:z,byline:"",siteName:"",lang:"",publishedTime:"",markdown:"",excerpt:""}}function x(j){let z=j.markdown.trim().toLowerCase(),$=(j.title||"").toLowerCase();if(j.markdown.trim().length<100)return{ok:!1,reason:"content too short (< 100 chars)"};let K=z.toLowerCase(),J=[{check:()=>K.includes("loading")&&K.includes("please wait"),desc:"loading page"},{check:()=>K.includes("please ensure javascript is enabled"),desc:"requires javascript"},{check:()=>K.includes("enable javascript to view"),desc:"requires javascript"},{check:()=>K.includes("just a moment"),desc:"cloudflare challenge detected in content"},{check:()=>K.includes("verify you are human"),desc:"human verification"},{check:()=>K.includes("captcha required"),desc:"captcha in extracted content"},{check:()=>K.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:Y,desc:V}of J)if(Y())return{ok:!1,reason:V};if($.includes("just a moment")||$.includes("checking your browser"))return{ok:!1,reason:"cloudflare challenge page detected in title"};return{ok:!0}}function c(j){try{let z=new URL(j),$=z.hostname.toLowerCase(),K=z.pathname.toLowerCase();if(["react.dev","nextjs.org","vuejs.org","angular.io","svelte.dev","docs.expo.dev","tailwindcss.com","storybook.js.org"].some((Y)=>$===Y||$.endsWith(`.${Y}`)))return!0;if(K.includes("/playground")||K.includes("/demo")||K.includes("/app"))return!0;if(z.hash&&z.hash.length>1)return!0;return!1}catch{return!1}}export{c as shouldUseBrowser,M as rewriteGitHubUrl,y as isPrivateUrl,p as fetchSourceHttp,T as extractContent,w as detectBotBlock,m as defaultFetchHeaders,x as checkContentQuality};
3
+ `).trim(),F=J.publishedTime||v(X)||"";return{title:J.title||X.title||Y,byline:J.byline||"",siteName:J.siteName||"",lang:J.lang||"",publishedTime:F,markdown:V,excerpt:V.slice(0,300).replaceAll(/\n/g," ")}}let Q=X.body;if(Q){let B=Q.cloneNode(!0);B.querySelectorAll("script, style, nav, footer, header, aside").forEach((q)=>q.remove());let F=(B.textContent||"").replaceAll(/\s+/g," ").trim();return{title:X.title||Y,byline:"",siteName:"",lang:"",publishedTime:v(X),markdown:F,excerpt:F.slice(0,300)}}return{title:Y,byline:"",siteName:"",lang:"",publishedTime:"",markdown:"",excerpt:""}}finally{W.window.close()}}function d(K){let Y=K.markdown.trim().toLowerCase(),j=(K.title||"").toLowerCase();if(K.markdown.trim().length<100)return{ok:!1,reason:"content too short (< 100 chars)"};let Z=Y.toLowerCase(),$=[{check:()=>Z.includes("loading")&&Z.includes("please wait"),desc:"loading page"},{check:()=>Z.includes("please ensure javascript is enabled"),desc:"requires javascript"},{check:()=>Z.includes("enable javascript to view"),desc:"requires javascript"},{check:()=>Z.includes("just a moment"),desc:"cloudflare challenge detected in content"},{check:()=>Z.includes("verify you are human"),desc:"human verification"},{check:()=>Z.includes("captcha required"),desc:"captcha in extracted content"},{check:()=>Z.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(Y),desc:"login form only"}];for(let{check:W,desc:X}of $)if(W())return{ok:!1,reason:X};if(j.includes("just a moment")||j.includes("checking your browser"))return{ok:!1,reason:"cloudflare challenge page detected in title"};return{ok:!0}}function s(K){try{let Y=new URL(K),j=Y.hostname.toLowerCase(),Z=Y.pathname.toLowerCase();if(["react.dev","nextjs.org","vuejs.org","angular.io","svelte.dev","docs.expo.dev","tailwindcss.com","storybook.js.org"].some((W)=>j===W||j.endsWith(`.${W}`)))return!0;if(Z.includes("/playground")||Z.includes("/demo")||Z.includes("/app"))return!0;if(Y.hash&&Y.hash.length>1)return!0;return!1}catch{return!1}}export{s as shouldUseBrowser,x as rewriteGitHubUrl,b as isPrivateUrl,n as fetchSourceHttp,c as extractContent,T as detectBotBlock,o as defaultFetchHeaders,d as checkContentQuality};