@apmantza/greedysearch-pi 1.8.0 → 1.8.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,223 +1,223 @@
1
- // src/search/chrome.mjs — Chrome launch, probe, port file management, and CDP wrapper
2
- //
3
- // Extracted from search.mjs to reduce file complexity.
4
- // Also used by coding-task.mjs (via import).
5
- //
6
- // cdp() is re-exported from extractors/common.mjs to avoid duplication.
7
-
8
- import { spawn } from "node:child_process";
9
- import {
10
- existsSync,
11
- readFileSync,
12
- renameSync,
13
- unlinkSync,
14
- writeFileSync,
15
- } from "node:fs";
16
- import http from "node:http";
17
- import { join } from "node:path";
18
-
19
- import { GREEDY_PORT, ACTIVE_PORT_FILE, PAGES_CACHE } from "./constants.mjs";
20
- import { cdp as _cdp } from "../../extractors/common.mjs";
21
-
22
- const __dir = import.meta.dirname || new URL(".", import.meta.url).pathname.replace(/^\/([A-Z]:)/, "$1");
23
-
24
- /** Re-export cdp() from the canonical location in extractors/common.mjs */
25
- export const cdp = _cdp;
26
-
27
- export async function getAnyTab() {
28
- const list = await cdp(["list"]);
29
- const first = list.split("\n")[0];
30
- if (!first) throw new Error("No Chrome tabs found");
31
- return first.slice(0, 8);
32
- }
33
-
34
- export async function openNewTab() {
35
- const anchor = await getAnyTab();
36
- const raw = await cdp([
37
- "evalraw",
38
- anchor,
39
- "Target.createTarget",
40
- '{"url":"about:blank"}',
41
- ]);
42
- const { targetId } = JSON.parse(raw);
43
- return targetId;
44
- }
45
-
46
- export async function activateTab(targetId) {
47
- try {
48
- const anchor = await getAnyTab();
49
- await cdp([
50
- "evalraw",
51
- anchor,
52
- "Target.activateTarget",
53
- JSON.stringify({ targetId }),
54
- ]);
55
- } catch {
56
- // best-effort
57
- }
58
- }
59
-
60
- export async function closeTab(targetId) {
61
- try {
62
- const anchor = await getAnyTab();
63
- await cdp([
64
- "evalraw",
65
- anchor,
66
- "Target.closeTarget",
67
- JSON.stringify({ targetId }),
68
- ]);
69
- } catch {
70
- /* best-effort */
71
- }
72
- }
73
-
74
- export async function closeTabs(targetIds = []) {
75
- for (const tid of targetIds) {
76
- if (!tid) continue;
77
- await closeTab(tid);
78
- }
79
- if (targetIds.length > 0) {
80
- await new Promise((r) => setTimeout(r, 300));
81
- await cdp(["list"]).catch(() => null);
82
- }
83
- }
84
-
85
- export function getFullTabFromCache(engine, engineDomains) {
86
- try {
87
- if (!existsSync(PAGES_CACHE)) return null;
88
- const pages = JSON.parse(readFileSync(PAGES_CACHE, "utf8"));
89
- const found = pages.find((p) => p.url.includes(engineDomains[engine]));
90
- return found ? found.targetId : null;
91
- } catch {
92
- return null;
93
- }
94
- }
95
-
96
- export function probeGreedyChrome(timeoutMs = 3000) {
97
- return new Promise((resolve) => {
98
- const req = http.get(
99
- `http://localhost:${GREEDY_PORT}/json/version`,
100
- (res) => {
101
- res.resume();
102
- resolve(res.statusCode === 200);
103
- },
104
- );
105
- req.on("error", () => resolve(false));
106
- req.setTimeout(timeoutMs, () => {
107
- req.destroy();
108
- resolve(false);
109
- });
110
- });
111
- }
112
-
113
- export async function refreshPortFile() {
114
- const LOCK_FILE = `${ACTIVE_PORT_FILE}.lock`;
115
- const TEMP_FILE = `${ACTIVE_PORT_FILE}.tmp`;
116
- const LOCK_STALE_MS = 5000;
117
- const LOCK_WAIT_MS = 1000;
118
-
119
- // File-based lock with exclusive create + stale lock recovery
120
- const lockAcquired = await new Promise((resolve) => {
121
- const start = Date.now();
122
- const tryLock = () => {
123
- try {
124
- const payload = JSON.stringify({ pid: process.pid, ts: Date.now() });
125
- writeFileSync(LOCK_FILE, payload, { encoding: "utf8", flag: "wx" });
126
- resolve(true);
127
- } catch (e) {
128
- if (e?.code !== "EEXIST") {
129
- if (Date.now() - start < LOCK_WAIT_MS) {
130
- setTimeout(tryLock, 50);
131
- } else {
132
- resolve(false);
133
- }
134
- return;
135
- }
136
-
137
- try {
138
- const lockRaw = readFileSync(LOCK_FILE, "utf8").trim();
139
- const parsed = lockRaw.startsWith("{")
140
- ? JSON.parse(lockRaw)
141
- : { ts: Number(lockRaw) };
142
- const lockTime = Number(parsed?.ts) || 0;
143
-
144
- if (lockTime > 0 && Date.now() - lockTime > LOCK_STALE_MS) {
145
- try {
146
- unlinkSync(LOCK_FILE);
147
- } catch {}
148
- }
149
-
150
- if (Date.now() - start < LOCK_WAIT_MS) {
151
- setTimeout(tryLock, 50);
152
- } else {
153
- resolve(false);
154
- }
155
- } catch {
156
- if (Date.now() - start < LOCK_WAIT_MS) {
157
- setTimeout(tryLock, 50);
158
- } else {
159
- resolve(false);
160
- }
161
- }
162
- }
163
- };
164
- tryLock();
165
- });
166
-
167
- try {
168
- const body = await new Promise((res, rej) => {
169
- const req = http.get(
170
- `http://localhost:${GREEDY_PORT}/json/version`,
171
- (r) => {
172
- let b = "";
173
- r.on("data", (d) => (b += d));
174
- r.on("end", () => res(b));
175
- },
176
- );
177
- req.on("error", rej);
178
- req.setTimeout(3000, () => {
179
- req.destroy();
180
- rej(new Error("timeout"));
181
- });
182
- });
183
- const { webSocketDebuggerUrl } = JSON.parse(body);
184
- const wsPath = new URL(webSocketDebuggerUrl).pathname;
185
-
186
- // Atomic write: write to temp file, then rename
187
- if (lockAcquired) {
188
- writeFileSync(TEMP_FILE, `${GREEDY_PORT}\n${wsPath}`, "utf8");
189
- try {
190
- unlinkSync(ACTIVE_PORT_FILE);
191
- } catch {}
192
- renameSync(TEMP_FILE, ACTIVE_PORT_FILE);
193
- }
194
- } catch {
195
- /* best-effort — launch.mjs already wrote the file on first start */
196
- } finally {
197
- if (lockAcquired) {
198
- try {
199
- unlinkSync(LOCK_FILE);
200
- } catch {}
201
- }
202
- }
203
- }
204
-
205
- export async function ensureChrome() {
206
- const ready = await probeGreedyChrome();
207
- if (!ready) {
208
- process.stderr.write(
209
- `GreedySearch Chrome not running on port ${GREEDY_PORT} — auto-launching...\n`,
210
- );
211
- await new Promise((resolve, reject) => {
212
- const proc = spawn("node", [join(__dir, "..", "..", "bin", "launch.mjs")], {
213
- stdio: ["ignore", process.stderr, process.stderr],
214
- });
215
- proc.on("close", (code) =>
216
- code === 0 ? resolve() : reject(new Error("launch.mjs failed")),
217
- );
218
- });
219
- } else {
220
- // Chrome already running — refresh the port file
221
- await refreshPortFile();
222
- }
1
+ // src/search/chrome.mjs — Chrome launch, probe, port file management, and CDP wrapper
2
+ //
3
+ // Extracted from search.mjs to reduce file complexity.
4
+ // Also used by coding-task.mjs (via import).
5
+ //
6
+ // cdp() is re-exported from extractors/common.mjs to avoid duplication.
7
+
8
+ import { spawn } from "node:child_process";
9
+ import {
10
+ existsSync,
11
+ readFileSync,
12
+ renameSync,
13
+ unlinkSync,
14
+ writeFileSync,
15
+ } from "node:fs";
16
+ import http from "node:http";
17
+ import { join } from "node:path";
18
+
19
+ import { GREEDY_PORT, ACTIVE_PORT_FILE, PAGES_CACHE } from "./constants.mjs";
20
+ import { cdp as _cdp } from "../../extractors/common.mjs";
21
+
22
+ const __dir = import.meta.dirname || new URL(".", import.meta.url).pathname.replace(/^\/([A-Z]:)/, "$1");
23
+
24
+ /** Re-export cdp() from the canonical location in extractors/common.mjs */
25
+ export const cdp = _cdp;
26
+
27
+ export async function getAnyTab() {
28
+ const list = await cdp(["list"]);
29
+ const first = list.split("\n")[0];
30
+ if (!first) throw new Error("No Chrome tabs found");
31
+ return first.slice(0, 8);
32
+ }
33
+
34
+ export async function openNewTab() {
35
+ const anchor = await getAnyTab();
36
+ const raw = await cdp([
37
+ "evalraw",
38
+ anchor,
39
+ "Target.createTarget",
40
+ '{"url":"about:blank"}',
41
+ ]);
42
+ const { targetId } = JSON.parse(raw);
43
+ return targetId;
44
+ }
45
+
46
+ export async function activateTab(targetId) {
47
+ try {
48
+ const anchor = await getAnyTab();
49
+ await cdp([
50
+ "evalraw",
51
+ anchor,
52
+ "Target.activateTarget",
53
+ JSON.stringify({ targetId }),
54
+ ]);
55
+ } catch {
56
+ // best-effort
57
+ }
58
+ }
59
+
60
+ export async function closeTab(targetId) {
61
+ try {
62
+ const anchor = await getAnyTab();
63
+ await cdp([
64
+ "evalraw",
65
+ anchor,
66
+ "Target.closeTarget",
67
+ JSON.stringify({ targetId }),
68
+ ]);
69
+ } catch {
70
+ /* best-effort */
71
+ }
72
+ }
73
+
74
+ export async function closeTabs(targetIds = []) {
75
+ for (const tid of targetIds) {
76
+ if (!tid) continue;
77
+ await closeTab(tid);
78
+ }
79
+ if (targetIds.length > 0) {
80
+ await new Promise((r) => setTimeout(r, 300));
81
+ await cdp(["list"]).catch(() => null);
82
+ }
83
+ }
84
+
85
+ export function getFullTabFromCache(engine, engineDomains) {
86
+ try {
87
+ if (!existsSync(PAGES_CACHE)) return null;
88
+ const pages = JSON.parse(readFileSync(PAGES_CACHE, "utf8"));
89
+ const found = pages.find((p) => p.url.includes(engineDomains[engine]));
90
+ return found ? found.targetId : null;
91
+ } catch {
92
+ return null;
93
+ }
94
+ }
95
+
96
+ export function probeGreedyChrome(timeoutMs = 3000) {
97
+ return new Promise((resolve) => {
98
+ const req = http.get(
99
+ `http://localhost:${GREEDY_PORT}/json/version`,
100
+ (res) => {
101
+ res.resume();
102
+ resolve(res.statusCode === 200);
103
+ },
104
+ );
105
+ req.on("error", () => resolve(false));
106
+ req.setTimeout(timeoutMs, () => {
107
+ req.destroy();
108
+ resolve(false);
109
+ });
110
+ });
111
+ }
112
+
113
+ export async function refreshPortFile() {
114
+ const LOCK_FILE = `${ACTIVE_PORT_FILE}.lock`;
115
+ const TEMP_FILE = `${ACTIVE_PORT_FILE}.tmp`;
116
+ const LOCK_STALE_MS = 5000;
117
+ const LOCK_WAIT_MS = 1000;
118
+
119
+ // File-based lock with exclusive create + stale lock recovery
120
+ const lockAcquired = await new Promise((resolve) => {
121
+ const start = Date.now();
122
+ const tryLock = () => {
123
+ try {
124
+ const payload = JSON.stringify({ pid: process.pid, ts: Date.now() });
125
+ writeFileSync(LOCK_FILE, payload, { encoding: "utf8", flag: "wx" });
126
+ resolve(true);
127
+ } catch (e) {
128
+ if (e?.code !== "EEXIST") {
129
+ if (Date.now() - start < LOCK_WAIT_MS) {
130
+ setTimeout(tryLock, 50);
131
+ } else {
132
+ resolve(false);
133
+ }
134
+ return;
135
+ }
136
+
137
+ try {
138
+ const lockRaw = readFileSync(LOCK_FILE, "utf8").trim();
139
+ const parsed = lockRaw.startsWith("{")
140
+ ? JSON.parse(lockRaw)
141
+ : { ts: Number(lockRaw) };
142
+ const lockTime = Number(parsed?.ts) || 0;
143
+
144
+ if (lockTime > 0 && Date.now() - lockTime > LOCK_STALE_MS) {
145
+ try {
146
+ unlinkSync(LOCK_FILE);
147
+ } catch {}
148
+ }
149
+
150
+ if (Date.now() - start < LOCK_WAIT_MS) {
151
+ setTimeout(tryLock, 50);
152
+ } else {
153
+ resolve(false);
154
+ }
155
+ } catch {
156
+ if (Date.now() - start < LOCK_WAIT_MS) {
157
+ setTimeout(tryLock, 50);
158
+ } else {
159
+ resolve(false);
160
+ }
161
+ }
162
+ }
163
+ };
164
+ tryLock();
165
+ });
166
+
167
+ try {
168
+ const body = await new Promise((res, rej) => {
169
+ const req = http.get(
170
+ `http://localhost:${GREEDY_PORT}/json/version`,
171
+ (r) => {
172
+ let b = "";
173
+ r.on("data", (d) => (b += d));
174
+ r.on("end", () => res(b));
175
+ },
176
+ );
177
+ req.on("error", rej);
178
+ req.setTimeout(3000, () => {
179
+ req.destroy();
180
+ rej(new Error("timeout"));
181
+ });
182
+ });
183
+ const { webSocketDebuggerUrl } = JSON.parse(body);
184
+ const wsPath = new URL(webSocketDebuggerUrl).pathname;
185
+
186
+ // Atomic write: write to temp file, then rename
187
+ if (lockAcquired) {
188
+ writeFileSync(TEMP_FILE, `${GREEDY_PORT}\n${wsPath}`, "utf8");
189
+ try {
190
+ unlinkSync(ACTIVE_PORT_FILE);
191
+ } catch {}
192
+ renameSync(TEMP_FILE, ACTIVE_PORT_FILE);
193
+ }
194
+ } catch {
195
+ /* best-effort — launch.mjs already wrote the file on first start */
196
+ } finally {
197
+ if (lockAcquired) {
198
+ try {
199
+ unlinkSync(LOCK_FILE);
200
+ } catch {}
201
+ }
202
+ }
203
+ }
204
+
205
+ export async function ensureChrome() {
206
+ const ready = await probeGreedyChrome();
207
+ if (!ready) {
208
+ process.stderr.write(
209
+ `GreedySearch Chrome not running on port ${GREEDY_PORT} — auto-launching...\n`,
210
+ );
211
+ await new Promise((resolve, reject) => {
212
+ const proc = spawn("node", [join(__dir, "..", "..", "bin", "launch.mjs")], {
213
+ stdio: ["ignore", process.stderr, process.stderr],
214
+ });
215
+ proc.on("close", (code) =>
216
+ code === 0 ? resolve() : reject(new Error("launch.mjs failed")),
217
+ );
218
+ });
219
+ } else {
220
+ // Chrome already running — refresh the port file
221
+ await refreshPortFile();
222
+ }
223
223
  }
@@ -1,38 +1,38 @@
1
- // src/search/constants.mjs — Shared constants for GreedySearch search pipeline
2
-
3
- import { tmpdir } from "node:os";
4
-
5
- export const GREEDY_PORT = 9222;
6
- export const GREEDY_PROFILE_DIR = `${tmpdir().replace(/\\/g, "/")}/greedysearch-chrome-profile`;
7
- export const ACTIVE_PORT_FILE = `${GREEDY_PROFILE_DIR}/DevToolsActivePort`;
8
- export const PAGES_CACHE = `${tmpdir().replace(/\\/g, "/")}/cdp-pages.json`;
9
-
10
- export const ALL_ENGINES = ["perplexity", "bing", "google"];
11
-
12
- export const ENGINE_DOMAINS = {
13
- perplexity: "perplexity.ai",
14
- bing: "copilot.microsoft.com",
15
- google: "google.com",
16
- gemini: "gemini.google.com",
17
- };
18
-
19
- export const ENGINES = {
20
- perplexity: "perplexity.mjs",
21
- pplx: "perplexity.mjs",
22
- p: "perplexity.mjs",
23
- bing: "bing-copilot.mjs",
24
- copilot: "bing-copilot.mjs",
25
- b: "bing-copilot.mjs",
26
- google: "google-ai.mjs",
27
- g: "google-ai.mjs",
28
- gemini: "gemini.mjs",
29
- gem: "gemini.mjs",
30
- };
31
-
32
- export const SOURCE_FETCH_CONCURRENCY = Math.max(
33
- 1,
34
- parseInt(process.env.GREEDY_FETCH_CONCURRENCY || "2", 10) || 2,
35
- );
36
-
37
- // Tell cdp.mjs to prefer the GreedySearch Chrome profile's DevToolsActivePort
1
+ // src/search/constants.mjs — Shared constants for GreedySearch search pipeline
2
+
3
+ import { tmpdir } from "node:os";
4
+
5
+ export const GREEDY_PORT = 9222;
6
+ export const GREEDY_PROFILE_DIR = `${tmpdir().replace(/\\/g, "/")}/greedysearch-chrome-profile`;
7
+ export const ACTIVE_PORT_FILE = `${GREEDY_PROFILE_DIR}/DevToolsActivePort`;
8
+ export const PAGES_CACHE = `${tmpdir().replace(/\\/g, "/")}/cdp-pages.json`;
9
+
10
+ export const ALL_ENGINES = ["perplexity", "bing", "google"];
11
+
12
+ export const ENGINE_DOMAINS = {
13
+ perplexity: "perplexity.ai",
14
+ bing: "copilot.microsoft.com",
15
+ google: "google.com",
16
+ gemini: "gemini.google.com",
17
+ };
18
+
19
+ export const ENGINES = {
20
+ perplexity: "perplexity.mjs",
21
+ pplx: "perplexity.mjs",
22
+ p: "perplexity.mjs",
23
+ bing: "bing-copilot.mjs",
24
+ copilot: "bing-copilot.mjs",
25
+ b: "bing-copilot.mjs",
26
+ google: "google-ai.mjs",
27
+ g: "google-ai.mjs",
28
+ gemini: "gemini.mjs",
29
+ gem: "gemini.mjs",
30
+ };
31
+
32
+ export const SOURCE_FETCH_CONCURRENCY = Math.max(
33
+ 1,
34
+ parseInt(process.env.GREEDY_FETCH_CONCURRENCY || "2", 10) || 2,
35
+ );
36
+
37
+ // Tell cdp.mjs to prefer the GreedySearch Chrome profile's DevToolsActivePort
38
38
  process.env.CDP_PROFILE_DIR = GREEDY_PROFILE_DIR;
@@ -1,15 +1,15 @@
1
- // src/search/defaults.mjs — Shared default values and timeouts
2
- //
3
- // Centralizes magic numbers used across the codebase.
4
- // Import from here instead of hardcoding values.
5
-
6
- export const DEFAULTS = {
7
- CDP_TIMEOUT: 30000, // Default CDP command timeout (ms)
8
- CDP_TIMEOUT_SHORT: 15000, // Short CDP timeout for search operations (ms)
9
- NAV_TIMEOUT: 35000, // Navigation timeout (ms)
10
- STREAM_TIMEOUT: 30000, // Stream completion timeout (ms)
11
- COPY_TIMEOUT: 60000, // Copy button appearance timeout (ms)
12
- CODING_TASK_TIMEOUT: 180000, // Coding task max duration (ms)
13
- MAX_SOURCE_FETCH: 10, // Max concurrent source fetches
14
- DESCRIPTION_MAX_LENGTH: 300, // Max answer length in truncated mode
1
+ // src/search/defaults.mjs — Shared default values and timeouts
2
+ //
3
+ // Centralizes magic numbers used across the codebase.
4
+ // Import from here instead of hardcoding values.
5
+
6
+ export const DEFAULTS = {
7
+ CDP_TIMEOUT: 30000, // Default CDP command timeout (ms)
8
+ CDP_TIMEOUT_SHORT: 15000, // Short CDP timeout for search operations (ms)
9
+ NAV_TIMEOUT: 35000, // Navigation timeout (ms)
10
+ STREAM_TIMEOUT: 30000, // Stream completion timeout (ms)
11
+ COPY_TIMEOUT: 60000, // Copy button appearance timeout (ms)
12
+ CODING_TASK_TIMEOUT: 180000, // Coding task max duration (ms)
13
+ MAX_SOURCE_FETCH: 10, // Max concurrent source fetches
14
+ DESCRIPTION_MAX_LENGTH: 300, // Max answer length in truncated mode
15
15
  };
@@ -9,7 +9,9 @@ import { ENGINES, GREEDY_PROFILE_DIR } from "./constants.mjs";
9
9
 
10
10
  export { ENGINES };
11
11
 
12
- const __dir = import.meta.dirname || new URL(".", import.meta.url).pathname.replace(/^\/([A-Z]:)/, "$1");
12
+ const __dir =
13
+ import.meta.dirname ||
14
+ new URL(".", import.meta.url).pathname.replace(/^\/([A-Z]:)/, "$1");
13
15
 
14
16
  export function runExtractor(
15
17
  script,
@@ -17,6 +19,7 @@ export function runExtractor(
17
19
  tabPrefix = null,
18
20
  short = false,
19
21
  timeoutMs = null,
22
+ locale = null,
20
23
  ) {
21
24
  // Gemini is slower - use longer timeout
22
25
  if (timeoutMs === null) {
@@ -25,6 +28,7 @@ export function runExtractor(
25
28
  const extraArgs = [
26
29
  ...(tabPrefix ? ["--tab", tabPrefix] : []),
27
30
  ...(short ? ["--short"] : []),
31
+ ...(locale ? ["--locale", locale] : []),
28
32
  ];
29
33
  return new Promise((resolve, reject) => {
30
34
  const proc = spawn(
@@ -55,4 +59,4 @@ export function runExtractor(
55
59
  }
56
60
  });
57
61
  });
58
- }
62
+ }