-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsearch.mjs
More file actions
executable file
·554 lines (477 loc) · 15.4 KB
/
search.mjs
File metadata and controls
executable file
·554 lines (477 loc) · 15.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
#!/usr/bin/env node
import { existsSync, readFileSync, writeFileSync, realpathSync } from "fs";
import { spawnSync, execSync } from "child_process";
import { homedir } from "os";
import { dirname, isAbsolute, join, resolve } from "path";
import { fileURLToPath, pathToFileURL } from "url";
function parseArgs(argv) {
const out = {
provider: undefined,
model: undefined,
purpose: "general research support",
timeoutMs: 120000,
json: false,
help: false,
query: "",
};
const positional = [];
for (let i = 0; i < argv.length; i++) {
const arg = argv[i];
if (arg === "--help" || arg === "-h") {
out.help = true;
continue;
}
if (arg === "--json") {
out.json = true;
continue;
}
if (arg === "--provider") {
out.provider = argv[++i];
continue;
}
if (arg.startsWith("--provider=")) {
out.provider = arg.slice("--provider=".length);
continue;
}
if (arg === "--model") {
out.model = argv[++i];
continue;
}
if (arg.startsWith("--model=")) {
out.model = arg.slice("--model=".length);
continue;
}
if (arg === "--purpose") {
out.purpose = argv[++i] || out.purpose;
continue;
}
if (arg.startsWith("--purpose=")) {
out.purpose = arg.slice("--purpose=".length) || out.purpose;
continue;
}
if (arg === "--timeout") {
out.timeoutMs = Math.max(1000, Number(argv[++i] || out.timeoutMs));
continue;
}
if (arg.startsWith("--timeout=")) {
out.timeoutMs = Math.max(1000, Number(arg.slice("--timeout=".length) || out.timeoutMs));
continue;
}
positional.push(arg);
}
out.query = positional.join(" ").trim();
return out;
}
function usage() {
return `Usage:
node search.mjs "<query>" [--purpose "<why>"] [--provider openai-codex|anthropic] [--model <id>] [--json]
Examples:
node search.mjs "latest python release" --purpose "update dependency notes"
node search.mjs "HTTP/3 browser support 2026" --provider openai-codex
node search.mjs "vite 7 breaking changes" --json`;
}
function readJson(path, fallback = {}) {
if (!existsSync(path)) return fallback;
try {
return JSON.parse(readFileSync(path, "utf8"));
} catch {
return fallback;
}
}
function writeJson(path, value) {
writeFileSync(path, `${JSON.stringify(value, null, 2)}\n`, "utf8");
}
function resolveConfigValue(config) {
if (typeof config !== "string" || !config) return undefined;
if (config.startsWith("!")) {
try {
const out = execSync(config.slice(1), {
encoding: "utf8",
timeout: 10000,
stdio: ["ignore", "pipe", "ignore"],
}).trim();
return out || undefined;
} catch {
return undefined;
}
}
return process.env[config] || config;
}
function getAgentDir() {
const configured = process.env.PI_CODING_AGENT_DIR;
if (!configured) return join(homedir(), ".pi", "agent");
if (configured === "~") return homedir();
if (configured.startsWith("~/")) return join(homedir(), configured.slice(2));
return configured;
}
function normalizeProvider(provider) {
if (!provider) return undefined;
const p = String(provider).toLowerCase().trim();
if (p.includes("anthropic") || p.includes("claude")) return "anthropic";
if (p.includes("codex") || p === "openai" || p.startsWith("openai")) return "openai-codex";
return undefined;
}
function pickProvider(argProvider, settings, auth) {
const forced = normalizeProvider(argProvider);
if (forced) return forced;
const fromSettings = normalizeProvider(settings?.defaultProvider);
if (fromSettings) return fromSettings;
if (auth?.["openai-codex"]) return "openai-codex";
if (auth?.anthropic) return "anthropic";
throw new Error("Could not determine provider. Pass --provider openai-codex|anthropic");
}
function decodeJwtAccountId(jwt) {
if (!jwt || typeof jwt !== "string") return undefined;
try {
const parts = jwt.split(".");
if (parts.length !== 3) return undefined;
const payload = JSON.parse(Buffer.from(parts[1], "base64url").toString("utf8"));
return payload?.["https://api.openai.com/auth"]?.chatgpt_account_id;
} catch {
return undefined;
}
}
function findPiExecutable() {
const cmd = process.platform === "win32" ? "where" : "which";
const result = spawnSync(cmd, ["pi"], { encoding: "utf8" });
if (result.status !== 0) return undefined;
const first = result.stdout
.split(/\r?\n/)
.map((x) => x.trim())
.find(Boolean);
return first || undefined;
}
function collectModuleCandidates() {
const candidates = new Set();
const add = (p) => {
if (!p) return;
const abs = isAbsolute(p) ? p : resolve(p);
candidates.add(abs);
};
if (process.env.PI_AI_MODULE_PATH) add(process.env.PI_AI_MODULE_PATH);
const cwd = process.cwd();
const scriptDir = dirname(fileURLToPath(import.meta.url));
for (const start of [cwd, scriptDir]) {
let dir = start;
for (let i = 0; i < 8; i++) {
add(join(dir, "node_modules", "@mariozechner", "pi-ai", "dist", "index.js"));
add(join(dir, "packages", "ai", "dist", "index.js"));
add(join(dir, "ai", "dist", "index.js"));
const parent = dirname(dir);
if (parent === dir) break;
dir = parent;
}
}
const piExec = findPiExecutable();
if (piExec) {
try {
const piReal = realpathSync(piExec);
const piDir = dirname(piReal);
add(join(piDir, "..", "..", "ai", "dist", "index.js"));
add(join(piDir, "..", "..", "pi-ai", "dist", "index.js"));
add(join(piDir, "..", "node_modules", "@mariozechner", "pi-ai", "dist", "index.js"));
add(join(piDir, "..", "..", "node_modules", "@mariozechner", "pi-ai", "dist", "index.js"));
} catch {
// ignore
}
}
add(join(homedir(), "Development", "pi-mono", "packages", "ai", "dist", "index.js"));
return Array.from(candidates);
}
async function loadPiAi() {
const tried = [];
try {
return await import("@mariozechner/pi-ai");
} catch (err) {
tried.push(`@mariozechner/pi-ai (${err?.code || err?.message || "not found"})`);
}
for (const candidate of collectModuleCandidates()) {
if (!existsSync(candidate)) continue;
try {
return await import(pathToFileURL(candidate).href);
} catch (err) {
tried.push(`${candidate} (${err?.code || err?.message || "failed"})`);
}
}
throw new Error(
`Could not load @mariozechner/pi-ai. Set PI_AI_MODULE_PATH to its dist/index.js.\nTried:\n- ${tried.join("\n- ")}`,
);
}
function pickFastModel(provider, requestedModel, piAi) {
const models = typeof piAi.getModels === "function" ? piAi.getModels(provider) : [];
if (!Array.isArray(models) || models.length === 0) {
if (requestedModel) return { id: requestedModel, baseUrl: undefined };
if (provider === "openai-codex") return { id: "gpt-5.1-codex-mini", baseUrl: "https://chatgpt.com/backend-api" };
return { id: "claude-haiku-4-5", baseUrl: "https://api.anthropic.com" };
}
if (requestedModel) {
const exact = models.find((m) => m.id === requestedModel);
if (exact) return exact;
return { ...models[0], id: requestedModel };
}
const preferredIds =
provider === "openai-codex"
? ["gpt-5.1-codex-mini", "gpt-5.3-codex-spark", "gpt-5.1"]
: ["claude-haiku-4-5", "claude-3-5-haiku-latest", "claude-3-5-haiku-20241022"];
for (const id of preferredIds) {
const found = models.find((m) => m.id === id);
if (found) return found;
}
const heuristic = models.find((m) => /mini|haiku|spark|flash|fast/i.test(m.id));
return heuristic || models[0];
}
async function resolveApiKey(provider, auth, authPath, piAi) {
const entry = auth?.[provider];
if (!entry) {
throw new Error(`No credentials for provider '${provider}' in ${authPath}`);
}
const inferredType = entry.type || (entry.access && entry.refresh ? "oauth" : entry.key ? "api_key" : undefined);
if (inferredType === "api_key") {
const key = resolveConfigValue(entry.key);
if (!key) throw new Error(`API key for ${provider} is empty or unresolved.`);
return { apiKey: key, accountId: entry.accountId };
}
if (inferredType !== "oauth") {
throw new Error(`Unsupported credential type for ${provider}: ${String(entry.type || "unknown")}`);
}
if (typeof piAi.getOAuthApiKey !== "function") {
throw new Error("Loaded pi-ai module does not export getOAuthApiKey");
}
const oauthCreds = {};
for (const [k, v] of Object.entries(auth || {})) {
if (v && (v.type === "oauth" || (v.access && v.refresh && v.expires))) {
oauthCreds[k] = v;
}
}
const refreshed = await piAi.getOAuthApiKey(provider, oauthCreds);
if (!refreshed) {
throw new Error(`No OAuth credentials available for provider '${provider}'`);
}
const mergedCred = { type: "oauth", ...(entry || {}), ...(refreshed.newCredentials || {}) };
auth[provider] = mergedCred;
writeJson(authPath, auth);
return {
apiKey: refreshed.apiKey,
accountId: mergedCred.accountId,
};
}
function buildUserPrompt(query, purpose) {
return `Search the internet for: ${query}\n\nPurpose: ${purpose}\n\nReturn a concise research summary with:\n- 3 to 7 key findings\n- for every finding: title, why it matters for this purpose, and a full canonical URL (https://...)\n- if multiple sources disagree, call that out\n- finish with a short recommendation on which source(s) to trust first.`;
}
function buildSystemPrompt() {
return "You are a fast web research assistant. Always produce practical summaries and include full source URLs (no shortened links).";
}
function resolveCodexUrl(baseUrl = "https://chatgpt.com/backend-api") {
const normalized = String(baseUrl || "https://chatgpt.com/backend-api").replace(/\/+$/, "");
if (normalized.endsWith("/codex/responses")) return normalized;
if (normalized.endsWith("/codex")) return `${normalized}/responses`;
return `${normalized}/codex/responses`;
}
function extractEventData(chunk) {
const payload = chunk
.split(/\r?\n/)
.filter((line) => line.startsWith("data:"))
.map((line) => line.slice(5).trim())
.join("\n")
.trim();
if (!payload || payload === "[DONE]") return null;
return payload;
}
async function runCodexSearch({ model, apiKey, accountId, query, purpose, timeoutMs, baseUrl }) {
const tokenAccountId = accountId || decodeJwtAccountId(apiKey);
if (!tokenAccountId) {
throw new Error("Could not determine ChatGPT account ID for openai-codex token.");
}
const body = {
model,
store: false,
stream: true,
instructions: buildSystemPrompt(),
input: [{ role: "user", content: buildUserPrompt(query, purpose) }],
tools: [{ type: "web_search" }],
tool_choice: "auto",
};
const endpoint = resolveCodexUrl(baseUrl);
const signal = typeof AbortSignal !== "undefined" && AbortSignal.timeout ? AbortSignal.timeout(timeoutMs) : undefined;
const res = await fetch(endpoint, {
method: "POST",
headers: {
authorization: `Bearer ${apiKey}`,
"chatgpt-account-id": tokenAccountId,
"content-type": "application/json",
accept: "text/event-stream",
"OpenAI-Beta": "responses=experimental",
originator: "pi-native-web-search-skill",
},
body: JSON.stringify(body),
signal,
});
if (!res.ok) {
const detail = await res.text();
throw new Error(`Codex request failed (${res.status}): ${detail}`);
}
if (!res.body) {
throw new Error("Codex response had no body");
}
const reader = res.body.getReader();
const decoder = new TextDecoder();
let buffer = "";
let text = "";
let fallbackText = "";
while (true) {
const { done, value } = await reader.read();
if (done) break;
buffer += decoder.decode(value, { stream: true });
let idx = buffer.indexOf("\n\n");
while (idx !== -1) {
const chunk = buffer.slice(0, idx);
buffer = buffer.slice(idx + 2);
idx = buffer.indexOf("\n\n");
const data = extractEventData(chunk);
if (!data) continue;
let event;
try {
event = JSON.parse(data);
} catch {
continue;
}
if (event.type === "response.output_text.delta" && typeof event.delta === "string") {
text += event.delta;
}
if (event.type === "response.output_item.done" && event.item?.type === "message") {
const parts = Array.isArray(event.item?.content) ? event.item.content : [];
const full = parts
.filter((p) => p.type === "output_text" && typeof p.text === "string")
.map((p) => p.text)
.join("\n");
if (full) fallbackText = full;
}
if (event.type === "error") {
throw new Error(event.message || "Codex stream failed");
}
if (event.type === "response.failed") {
throw new Error(event.response?.error?.message || "Codex response failed");
}
}
}
const finalText = (text || fallbackText || "").trim();
if (!finalText) {
throw new Error("Codex returned an empty response");
}
return finalText;
}
function buildAnthropicHeaders(apiKey) {
const oauthToken = typeof apiKey === "string" && apiKey.includes("sk-ant-oat");
if (oauthToken) {
return {
authorization: `Bearer ${apiKey}`,
"anthropic-version": "2023-06-01",
"anthropic-beta": "claude-code-20250219,oauth-2025-04-20,web-search-2025-03-05",
"content-type": "application/json",
accept: "application/json",
"x-app": "cli",
"user-agent": "claude-cli/1.0.72 (external, cli)",
};
}
return {
"x-api-key": apiKey,
"anthropic-version": "2023-06-01",
"anthropic-beta": "web-search-2025-03-05",
"content-type": "application/json",
accept: "application/json",
};
}
async function runAnthropicSearch({ model, apiKey, query, purpose, timeoutMs }) {
const body = {
model,
max_tokens: 1800,
temperature: 0,
system: buildSystemPrompt(),
tools: [{ type: "web_search_20250305", name: "web_search", max_uses: 5 }],
messages: [{ role: "user", content: buildUserPrompt(query, purpose) }],
};
const signal = typeof AbortSignal !== "undefined" && AbortSignal.timeout ? AbortSignal.timeout(timeoutMs) : undefined;
const res = await fetch("https://api.anthropic.com/v1/messages", {
method: "POST",
headers: buildAnthropicHeaders(apiKey),
body: JSON.stringify(body),
signal,
});
const payload = await res.text();
if (!res.ok) {
throw new Error(`Anthropic request failed (${res.status}): ${payload}`);
}
let parsed;
try {
parsed = JSON.parse(payload);
} catch {
throw new Error("Anthropic returned non-JSON response");
}
const text = (parsed.content || [])
.filter((item) => item.type === "text" && typeof item.text === "string")
.map((item) => item.text)
.join("\n\n")
.trim();
if (!text) {
throw new Error("Anthropic returned no text content");
}
return text;
}
async function main() {
const args = parseArgs(process.argv.slice(2));
if (args.help || !args.query) {
console.error(usage());
process.exit(args.help ? 0 : 1);
}
const agentDir = getAgentDir();
const authPath = join(agentDir, "auth.json");
const settingsPath = join(agentDir, "settings.json");
const auth = readJson(authPath, {});
const settings = readJson(settingsPath, {});
const provider = pickProvider(args.provider, settings, auth);
const piAi = await loadPiAi();
const model = pickFastModel(provider, args.model, piAi);
const { apiKey, accountId } = await resolveApiKey(provider, auth, authPath, piAi);
const text =
provider === "openai-codex"
? await runCodexSearch({
model: model.id,
apiKey,
accountId,
query: args.query,
purpose: args.purpose,
timeoutMs: args.timeoutMs,
baseUrl: model.baseUrl,
})
: await runAnthropicSearch({
model: model.id,
apiKey,
query: args.query,
purpose: args.purpose,
timeoutMs: args.timeoutMs,
});
if (args.json) {
console.log(
JSON.stringify(
{
provider,
model: model.id,
query: args.query,
purpose: args.purpose,
result: text,
},
null,
2,
),
);
return;
}
console.log(`Provider: ${provider}`);
console.log(`Model: ${model.id}`);
console.log("");
console.log(text);
}
main().catch((err) => {
console.error(`Error: ${err?.message || err}`);
process.exit(1);
});