@bacnh85/pi-web 0.1.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +37 -2
- package/index.ts +388 -313
- package/lib/brave.ts +40 -0
- package/lib/config.ts +155 -0
- package/lib/content.ts +78 -0
- package/lib/firecrawl.ts +86 -0
- package/lib/format.ts +121 -0
- package/lib/retry.ts +142 -0
- package/lib/searxng.ts +63 -0
- package/package.json +14 -3
package/index.ts
CHANGED
|
@@ -2,328 +2,403 @@
|
|
|
2
2
|
|
|
3
3
|
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
4
4
|
import { Type } from "typebox";
|
|
5
|
-
import fs from "node:fs";
|
|
6
|
-
import os from "node:os";
|
|
7
|
-
import path from "node:path";
|
|
8
|
-
import { createRequire } from "node:module";
|
|
9
5
|
|
|
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
|
-
let text: string;
|
|
50
|
-
try { text = fs.readFileSync(file, "utf8"); } catch (e: any) { if (e?.code === "ENOENT") return null; throw e; }
|
|
51
|
-
const values: Record<string, string> = {};
|
|
52
|
-
for (const rawLine of text.split(/\r?\n/)) {
|
|
53
|
-
const line = rawLine.trim();
|
|
54
|
-
if (!line || line.startsWith("#")) continue;
|
|
55
|
-
const match = line.match(/^(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*)$/);
|
|
56
|
-
if (match) values[match[1]] = parseDotenvValue(match[2]);
|
|
57
|
-
}
|
|
58
|
-
return values;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
function findEnvValue(name: string, cwd = process.cwd()): { value?: string; source: string; checkedFiles: string[] } {
|
|
62
|
-
if (process.env[name]) return { value: process.env[name], source: "process.env", checkedFiles: [] };
|
|
63
|
-
const checkedFiles = envFileCandidates(cwd);
|
|
64
|
-
for (const file of checkedFiles) {
|
|
65
|
-
const parsed = parseDotenvFile(file);
|
|
66
|
-
if (parsed?.[name]) return { value: parsed[name], source: file, checkedFiles };
|
|
67
|
-
}
|
|
68
|
-
return { value: undefined, source: "", checkedFiles };
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
function truncateText(text: string): string {
|
|
72
|
-
const lines = text.split("\n");
|
|
73
|
-
let output = lines.slice(0, OUTPUT_MAX_LINES).join("\n");
|
|
74
|
-
while (Buffer.byteLength(output, "utf8") > OUTPUT_MAX_BYTES) output = output.slice(0, Math.max(0, output.length - 1024));
|
|
75
|
-
if (lines.length > OUTPUT_MAX_LINES || output.length < text.length) output += `\n\n[Web output truncated to ${OUTPUT_MAX_LINES} lines / ${OUTPUT_MAX_BYTES} bytes.]`;
|
|
76
|
-
return output;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
function cwdFromContext(ctx: any): string {
|
|
80
|
-
return typeof ctx?.cwd === "string" && ctx.cwd ? ctx.cwd : process.cwd();
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
function signalWithTimeout(timeoutMs: number, signal?: AbortSignal): AbortSignal {
|
|
84
|
-
const timeoutSignal = AbortSignal.timeout(timeoutMs);
|
|
85
|
-
if (!signal) return timeoutSignal;
|
|
86
|
-
return AbortSignal.any([signal, timeoutSignal]);
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
function abortableSleep(ms: number, signal?: AbortSignal): Promise<void> {
|
|
90
|
-
if (signal?.aborted) return Promise.reject(signal.reason ?? new Error("Operation aborted."));
|
|
91
|
-
return new Promise((resolve, reject) => {
|
|
92
|
-
const timer = setTimeout(cleanupResolve, ms);
|
|
93
|
-
function cleanupResolve() {
|
|
94
|
-
signal?.removeEventListener("abort", cleanupReject);
|
|
95
|
-
resolve();
|
|
96
|
-
}
|
|
97
|
-
function cleanupReject() {
|
|
98
|
-
clearTimeout(timer);
|
|
99
|
-
reject(signal?.reason ?? new Error("Operation aborted."));
|
|
100
|
-
}
|
|
101
|
-
signal?.addEventListener("abort", cleanupReject, { once: true });
|
|
102
|
-
});
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
function sanitizeSnippet(text = ""): string {
|
|
106
|
-
return text.replace(/<[^>]*>/g, "").replace(/ /gi, " ").replace(/&/gi, "&").replace(/</gi, "<").replace(/>/gi, ">").replace(/"/gi, '"').replace(/'|'/gi, "'").replace(/&#(\d+);/g, (_, code) => String.fromCodePoint(Number(code))).replace(/&#x([0-9a-f]+);/gi, (_, code) => String.fromCodePoint(Number.parseInt(code, 16))).replace(/\s+/g, " ").trim();
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
function loadReadableContentDependencies(): { Readability: any; JSDOM: any; TurndownService: any; gfm: any } {
|
|
110
|
-
try {
|
|
111
|
-
return {
|
|
112
|
-
Readability: require("@mozilla/readability").Readability,
|
|
113
|
-
JSDOM: require("jsdom").JSDOM,
|
|
114
|
-
TurndownService: require("turndown"),
|
|
115
|
-
gfm: require("turndown-plugin-gfm").gfm,
|
|
116
|
-
};
|
|
117
|
-
} catch (error: any) {
|
|
118
|
-
throw new Error(`Readable content extraction dependencies are missing. Install pi-web dependencies with \`npm install\` in the pi-web extension directory, or install the package with Pi so runtime dependencies are installed. Original error: ${error?.message || error}`);
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
function htmlToMarkdown(html: string, deps = loadReadableContentDependencies()): string {
|
|
123
|
-
const turndown = new deps.TurndownService({ headingStyle: "atx", codeBlockStyle: "fenced" });
|
|
124
|
-
turndown.use(deps.gfm as any);
|
|
125
|
-
turndown.addRule("removeEmptyLinks", { filter: (node: any) => node.nodeName === "A" && !node.textContent?.trim(), replacement: () => "" });
|
|
126
|
-
return turndown.turndown(html).replace(/\[\\?\[\s*\\?\]\]\([^)]*\)/g, "").replace(/ +/g, " ").replace(/\s+,/g, ",").replace(/\s+\./g, ".").replace(/\n{3,}/g, "\n\n").trim();
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
async function fetchReadableContent(url: string, timeoutMs = 15000, signal?: AbortSignal): Promise<{ title: string; markdown: string }> {
|
|
130
|
-
const response = await fetch(url, { headers: { "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 Chrome/120 Safari/537.36", Accept: "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "Accept-Language": "en-US,en;q=0.9" }, signal: signalWithTimeout(timeoutMs, signal) });
|
|
131
|
-
if (!response.ok) throw new Error(`HTTP ${response.status}: ${response.statusText}`);
|
|
132
|
-
const html = await response.text();
|
|
133
|
-
const deps = loadReadableContentDependencies();
|
|
134
|
-
const dom = new deps.JSDOM(html, { url });
|
|
135
|
-
const article = new deps.Readability(dom.window.document).parse();
|
|
136
|
-
if (article?.content) return { title: article.title || "", markdown: htmlToMarkdown(article.content, deps) };
|
|
137
|
-
const fallbackDoc = new deps.JSDOM(html, { url });
|
|
138
|
-
const doc = fallbackDoc.window.document;
|
|
139
|
-
doc.querySelectorAll("script, style, noscript, nav, header, footer, aside").forEach((el: Element) => el.remove());
|
|
140
|
-
const title = doc.querySelector("title")?.textContent?.trim() || "";
|
|
141
|
-
const main = doc.querySelector("main, article, [role='main'], .content, #content") || doc.body;
|
|
142
|
-
const fallbackHtml = main?.innerHTML || "";
|
|
143
|
-
if (fallbackHtml.trim().length <= 100) throw new Error("Could not extract readable content from this page.");
|
|
144
|
-
return { title, markdown: htmlToMarkdown(fallbackHtml, deps) };
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
async function fetchBraveResults(query: string, count: number, country: string, freshness: string, apiKey: string, signal?: AbortSignal) {
|
|
148
|
-
const params = new URLSearchParams({ q: query, count: String(count), country });
|
|
149
|
-
if (freshness) params.append("freshness", freshness);
|
|
150
|
-
const response = await fetch(`https://api.search.brave.com/res/v1/web/search?${params}`, { headers: { Accept: "application/json", "Accept-Encoding": "gzip", "X-Subscription-Token": apiKey }, signal });
|
|
151
|
-
if (!response.ok) throw new Error(`HTTP ${response.status}: ${response.statusText}\n${await response.text()}`);
|
|
152
|
-
const data: any = await response.json();
|
|
153
|
-
return (data.web?.results || []).slice(0, count).map((r: any) => ({ title: sanitizeSnippet(r.title || ""), url: r.url || "", snippet: sanitizeSnippet(r.description || ""), age: sanitizeSnippet(r.age || r.page_age || "") }));
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
function normalizeSearxngBaseUrl(raw?: string): string {
|
|
157
|
-
let value = (raw || DEFAULT_SEARXNG_BASE_URL).trim();
|
|
158
|
-
if (!/^https?:\/\//i.test(value)) value = /^(localhost|127\.|10\.|172\.(1[6-9]|2\d|3[01])\.|192\.168\.)/i.test(value) ? `http://${value}` : `https://${value}`;
|
|
159
|
-
return value.replace(/\/+$/, "");
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
function loadSearxngConfig(params: any = {}, cwd = process.cwd()) {
|
|
163
|
-
const baseUrlEnv = findEnvValue("SEARXNG_BASE_URL", cwd);
|
|
164
|
-
return { baseUrl: normalizeSearxngBaseUrl(params.searxng_base_url || params.base_url || baseUrlEnv.value), source: params.searxng_base_url || params.base_url ? "tool parameter" : baseUrlEnv.source || "default local" };
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
async function fetchSearxngResults(params: any, baseUrl: string, signal?: AbortSignal) {
|
|
168
|
-
const limit = Math.min(50, Math.max(1, params.count ?? 5));
|
|
169
|
-
const url = new URL(`${baseUrl}/search`);
|
|
170
|
-
url.searchParams.set("q", params.query);
|
|
171
|
-
url.searchParams.set("format", "json");
|
|
172
|
-
url.searchParams.set("pageno", String(Math.max(1, params.pageno ?? 1)));
|
|
173
|
-
for (const key of ["categories", "engines", "language", "time_range", "safesearch"] as const) if (params[key] !== undefined && params[key] !== "") url.searchParams.set(key, String(params[key]));
|
|
174
|
-
const response = await fetch(url, { headers: { Accept: "application/json", "User-Agent": "pi-web/0.1 SearXNG" }, signal: signalWithTimeout(params.timeout_ms || 15000, signal) });
|
|
175
|
-
const text = await response.text();
|
|
176
|
-
if (!response.ok) {
|
|
177
|
-
const hint = response.status === 403 ? "\nHint: enable JSON output in SearXNG settings.yml with search.formats including json." : "";
|
|
178
|
-
throw new Error(`HTTP ${response.status}: ${response.statusText}${hint}${text ? `\n${text}` : ""}`);
|
|
179
|
-
}
|
|
180
|
-
const data: any = text ? JSON.parse(text) : {};
|
|
181
|
-
const results = (data.results || []).slice(0, limit).map((r: any) => ({ title: sanitizeSnippet(r.title || ""), url: r.url || "", snippet: sanitizeSnippet(r.content || r.snippet || ""), engine: r.engine || "", category: r.category || "", score: r.score, publishedDate: r.publishedDate || r.published_date || "" }));
|
|
182
|
-
return { ...data, results };
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
function formatSearchResults(results: any[]): string {
|
|
186
|
-
if (!results.length) return "No results found.";
|
|
187
|
-
return results.map((r, i) => [`--- Result ${i + 1} ---`, `Title: ${r.title || ""}`, `Link: ${r.url || r.link || ""}`, r.age ? `Age: ${r.age}` : "", r.snippet || r.description ? `Snippet: ${r.snippet || r.description}` : "", r.content || r.markdown ? `Content:\n${r.content || r.markdown}` : ""].filter(Boolean).join("\n")).join("\n\n");
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
function normalizeFirecrawlBaseUrl(raw?: string): string {
|
|
191
|
-
let value = (raw || HOSTED_FIRECRAWL_BASE_URL).trim();
|
|
192
|
-
if (!/^https?:\/\//i.test(value)) value = /^(localhost|127\.|10\.|172\.(1[6-9]|2\d|3[01])\.|192\.168\.)/i.test(value) ? `http://${value}` : `https://${value}`;
|
|
193
|
-
value = value.replace(/\/+$/, "");
|
|
194
|
-
if (!/\/v\d+$/i.test(value)) value += "/v2";
|
|
195
|
-
return value;
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
function loadFirecrawlConfig(params: any = {}, cwd = process.cwd()) {
|
|
199
|
-
const apiUrl = params.firecrawl_api_url || params.api_url || findEnvValue("FIRECRAWL_API_URL", cwd).value;
|
|
200
|
-
const apiKey = params.firecrawl_api_key || params.api_key || findEnvValue("FIRECRAWL_API_KEY", cwd).value;
|
|
201
|
-
const timeoutValue = params.timeout_ms || findEnvValue("FIRECRAWL_TIMEOUT_MS", cwd).value;
|
|
202
|
-
const baseUrl = normalizeFirecrawlBaseUrl(apiUrl);
|
|
203
|
-
const isHosted = !apiUrl || baseUrl.startsWith(HOSTED_FIRECRAWL_BASE_URL);
|
|
204
|
-
const timeoutMs = timeoutValue ? Number.parseInt(String(timeoutValue), 10) : 60000;
|
|
205
|
-
if (!Number.isInteger(timeoutMs) || timeoutMs < 1000) throw new Error("FIRECRAWL_TIMEOUT_MS/timeout_ms must be an integer >= 1000.");
|
|
206
|
-
if (isHosted && !apiKey) throw new Error("FIRECRAWL_API_KEY is required for hosted Firecrawl.");
|
|
207
|
-
return { baseUrl, apiKey, isHosted, timeoutMs };
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
class FirecrawlHttpError extends Error { status: number; constructor(status: number, statusText: string, text: string) { super(`HTTP ${status}: ${statusText}${text ? `\n${text}` : ""}`); this.status = status; } }
|
|
211
|
-
|
|
212
|
-
async function firecrawlRequest(config: any, method: string, endpoint: string, body?: unknown, allowFallback = true, signal?: AbortSignal): Promise<any> {
|
|
213
|
-
try { return await firecrawlRequestJson(config, method, endpoint, body, signal); }
|
|
214
|
-
catch (e) {
|
|
215
|
-
if (!allowFallback || config.isHosted || !config.baseUrl.endsWith("/v2") || !(e instanceof FirecrawlHttpError) || e.status !== 404) throw e;
|
|
216
|
-
return firecrawlRequestJson({ ...config, baseUrl: config.baseUrl.replace(/\/v2$/, "/v1") }, method, endpoint, body, signal);
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
async function firecrawlRequestJson(config: any, method: string, endpoint: string, body?: unknown, signal?: AbortSignal): Promise<any> {
|
|
221
|
-
const headers: Record<string, string> = { Accept: "application/json" };
|
|
222
|
-
if (body !== undefined) headers["Content-Type"] = "application/json";
|
|
223
|
-
if (config.apiKey) headers.Authorization = `Bearer ${config.apiKey}`;
|
|
224
|
-
const response = await fetch(`${config.baseUrl}${endpoint}`, { method, headers, body: body === undefined ? undefined : JSON.stringify(body), signal: signalWithTimeout(config.timeoutMs, signal) });
|
|
225
|
-
const text = await response.text();
|
|
226
|
-
if (!response.ok) throw new FirecrawlHttpError(response.status, response.statusText, text);
|
|
227
|
-
return text ? JSON.parse(text) : { success: true };
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
function formatFirecrawlScrape(data: any, maxChars = 20000): string {
|
|
231
|
-
const page = data.data || data;
|
|
232
|
-
const meta = page.metadata || {};
|
|
233
|
-
const parts: string[] = [];
|
|
234
|
-
if (meta.title) parts.push(`# ${meta.title}`);
|
|
235
|
-
if (meta.sourceURL || meta.url) parts.push(`Source: ${meta.sourceURL || meta.url}`);
|
|
236
|
-
if (meta.statusCode) parts.push(`Status: ${meta.statusCode}`);
|
|
237
|
-
if (data.warning || page.warning) parts.push(`Warning: ${data.warning || page.warning}`);
|
|
238
|
-
const body = page.markdown || page.summary || page.answer || page.html || page.rawHtml || "";
|
|
239
|
-
if (body) parts.push(String(body).slice(0, maxChars));
|
|
240
|
-
if (Array.isArray(page.links)) parts.push(["## Links", ...page.links.slice(0, 100).map((l: string) => `- ${l}`)].join("\n"));
|
|
241
|
-
return parts.join("\n\n") || JSON.stringify(data, null, 2);
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
function formatFirecrawlSearch(data: any, maxChars = 5000): string {
|
|
245
|
-
const web = data.data?.web || [];
|
|
246
|
-
const news = data.data?.news || [];
|
|
247
|
-
const images = data.data?.images || [];
|
|
248
|
-
const results = [...web, ...news].map((r: any) => ({ title: r.title || r.metadata?.title || "", url: r.url || r.metadata?.sourceURL || "", snippet: r.description || r.snippet || "", markdown: r.markdown ? String(r.markdown).slice(0, maxChars) : "" }));
|
|
249
|
-
let text = formatSearchResults(results);
|
|
250
|
-
if (images.length) text += "\n\n" + images.map((img: any, i: number) => [`--- Image ${i + 1} ---`, `Title: ${img.title || ""}`, `Image: ${img.imageUrl || ""}`, `Page: ${img.url || ""}`].join("\n")).join("\n\n");
|
|
251
|
-
if (data.warning) text += `\n\nWarning: ${data.warning}`;
|
|
252
|
-
return text;
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
const controlSchema = { api_key: Type.Optional(Type.String({ description: "Provider API key override. Prefer env vars." })), timeout_ms: Type.Optional(Type.Number({ description: "Request timeout in milliseconds." })) };
|
|
256
|
-
const firecrawlControlSchema = { firecrawl_api_key: Type.Optional(Type.String({ description: "Firecrawl API key override. Prefer FIRECRAWL_API_KEY." })), firecrawl_api_url: Type.Optional(Type.String({ description: "Firecrawl API URL override. Defaults to FIRECRAWL_API_URL or hosted." })), timeout_ms: Type.Optional(Type.Number({ description: "Request timeout in milliseconds." })) };
|
|
257
|
-
const searxngControlSchema = { searxng_base_url: Type.Optional(Type.String({ description: "SearXNG base URL override. Defaults to SEARXNG_BASE_URL or http://172.30.55.22:8888." })), timeout_ms: Type.Optional(Type.Number({ description: "Request timeout in milliseconds." })) };
|
|
6
|
+
import {
|
|
7
|
+
findEnvValue,
|
|
8
|
+
cwdFromContext,
|
|
9
|
+
includeProjectEnv,
|
|
10
|
+
normalizeSearxngBaseUrl,
|
|
11
|
+
normalizeFirecrawlBaseUrl,
|
|
12
|
+
loadSearxngConfig,
|
|
13
|
+
loadFirecrawlConfig,
|
|
14
|
+
HOSTED_FIRECRAWL_BASE_URL,
|
|
15
|
+
} from "./lib/config";
|
|
16
|
+
import { truncateText, formatSearchResults, formatFirecrawlScrape, formatFirecrawlSearch } from "./lib/format";
|
|
17
|
+
import { fetchReadableContent } from "./lib/content";
|
|
18
|
+
import { fetchBraveResults } from "./lib/brave";
|
|
19
|
+
import { fetchSearxngResults } from "./lib/searxng";
|
|
20
|
+
import { firecrawlRequest, type FirecrawlResult } from "./lib/firecrawl";
|
|
21
|
+
|
|
22
|
+
// ---------------------------------------------------------------------------
|
|
23
|
+
// Schema fragments shared across tool registrations
|
|
24
|
+
// ---------------------------------------------------------------------------
|
|
25
|
+
|
|
26
|
+
const controlSchema = {
|
|
27
|
+
api_key: Type.Optional(Type.String({ description: "Provider API key override. Prefer env vars." })),
|
|
28
|
+
timeout_ms: Type.Optional(Type.Number({ description: "Request timeout in milliseconds." })),
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const firecrawlControlSchema = {
|
|
32
|
+
firecrawl_api_key: Type.Optional(Type.String({ description: "Firecrawl API key override. Prefer FIRECRAWL_API_KEY." })),
|
|
33
|
+
firecrawl_api_url: Type.Optional(Type.String({ description: "Firecrawl API URL override. Defaults to FIRECRAWL_API_URL or hosted." })),
|
|
34
|
+
timeout_ms: Type.Optional(Type.Number({ description: "Request timeout in milliseconds." })),
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
const searxngControlSchema = {
|
|
38
|
+
searxng_base_url: Type.Optional(Type.String({ description: "SearXNG base URL override. Defaults to SEARXNG_BASE_URL or http://172.30.55.22:8888." })),
|
|
39
|
+
timeout_ms: Type.Optional(Type.Number({ description: "Request timeout in milliseconds." })),
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
// ---------------------------------------------------------------------------
|
|
43
|
+
// Extension entry point
|
|
44
|
+
// ---------------------------------------------------------------------------
|
|
258
45
|
|
|
259
46
|
export default function piWebExtension(pi: ExtensionAPI) {
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
47
|
+
// ── Brave Search ──────────────────────────────────────────────────────
|
|
48
|
+
pi.registerTool({
|
|
49
|
+
name: "brave_search",
|
|
50
|
+
label: "Brave Search",
|
|
51
|
+
description:
|
|
52
|
+
"Search the web with Brave Search API, optionally fetching readable page content. Best as a fast hosted fallback or independent index for source discovery, current facts, docs lookup, and general web search.",
|
|
53
|
+
promptSnippet: "Search current web results with Brave",
|
|
54
|
+
promptGuidelines: [
|
|
55
|
+
"Use SearXNG first for general web search when the self-hosted instance is available; use Brave as a fast hosted fallback or independent second source.",
|
|
56
|
+
"Use include_content only for a small number of results to avoid rate limits and extra page fetches.",
|
|
57
|
+
"Cite result URLs when web results materially support the answer; if Brave is rate-limited, try SearXNG or Firecrawl search.",
|
|
58
|
+
],
|
|
59
|
+
parameters: Type.Object({
|
|
60
|
+
...controlSchema,
|
|
61
|
+
query: Type.String(),
|
|
62
|
+
count: Type.Optional(Type.Number({ default: 5 })),
|
|
63
|
+
country: Type.Optional(Type.String({ default: "US" })),
|
|
64
|
+
freshness: Type.Optional(Type.String()),
|
|
65
|
+
include_content: Type.Optional(Type.Boolean({ default: false })),
|
|
66
|
+
content_chars: Type.Optional(Type.Number({ default: 5000 })),
|
|
67
|
+
}),
|
|
68
|
+
async execute(_id: string, params: Record<string, unknown>, signal: AbortSignal, _onUpdate: unknown, ctx: any) {
|
|
69
|
+
const cwd = cwdFromContext(ctx);
|
|
70
|
+
const apiKey = (params.api_key as string) || findEnvValue("BRAVE_API_KEY", cwd, includeProjectEnv(ctx)).value;
|
|
71
|
+
if (!apiKey) throw new Error("BRAVE_API_KEY is required.");
|
|
72
|
+
const count = Math.min(20, Math.max(1, (params.count as number) ?? 5));
|
|
73
|
+
const results = await fetchBraveResults(
|
|
74
|
+
params.query as string,
|
|
75
|
+
count,
|
|
76
|
+
((params.country as string) || "US").toUpperCase(),
|
|
77
|
+
(params.freshness as string) || "",
|
|
78
|
+
apiKey,
|
|
79
|
+
signal,
|
|
80
|
+
);
|
|
81
|
+
if (params.include_content) {
|
|
82
|
+
await Promise.all(
|
|
83
|
+
results.map(async (r) => {
|
|
84
|
+
try {
|
|
85
|
+
const content = await fetchReadableContent(r.url, (params.timeout_ms as number) || 10000, signal);
|
|
86
|
+
r.content = content.markdown.slice(0, (params.content_chars as number) ?? 5000);
|
|
87
|
+
} catch (e: any) {
|
|
88
|
+
r.content = `(Fetch error for ${r.url}: ${e?.message || String(e)})`;
|
|
89
|
+
}
|
|
90
|
+
}),
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
return { content: [{ type: "text" as const, text: truncateText(formatSearchResults(results)) }], details: results };
|
|
94
|
+
},
|
|
95
|
+
});
|
|
269
96
|
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
97
|
+
// ── Web Content (readable extraction) ─────────────────────────────────
|
|
98
|
+
pi.registerTool({
|
|
99
|
+
name: "web_content",
|
|
100
|
+
label: "Web Content",
|
|
101
|
+
description:
|
|
102
|
+
"Fetch a URL and extract readable markdown content using JSDOM + Readability + Turndown (does not use the Brave API). Best for fast known-URL article/docs extraction.",
|
|
103
|
+
promptSnippet: "Extract readable webpage content as markdown",
|
|
104
|
+
promptGuidelines: [
|
|
105
|
+
"Use for known URLs when simple readable markdown extraction is enough.",
|
|
106
|
+
"Prefer Firecrawl scrape for dynamic pages, pages that block direct fetching, or when links/structured extraction are needed.",
|
|
107
|
+
"Cite the source URL when using extracted content in an answer.",
|
|
108
|
+
],
|
|
109
|
+
parameters: Type.Object({
|
|
110
|
+
url: Type.String(),
|
|
111
|
+
timeout_ms: Type.Optional(Type.Number({ default: 15000 })),
|
|
112
|
+
content_chars: Type.Optional(Type.Number({ default: 20000 })),
|
|
113
|
+
}),
|
|
114
|
+
async execute(_id: string, params: Record<string, unknown>, signal: AbortSignal) {
|
|
115
|
+
const parsed = new URL(params.url as string);
|
|
116
|
+
if (!["http:", "https:"].includes(parsed.protocol)) throw new Error("URL must use http or https.");
|
|
117
|
+
const article = await fetchReadableContent(parsed.href, (params.timeout_ms as number) || 15000, signal);
|
|
118
|
+
const text = `${article.title ? `# ${article.title}\n\n` : ""}${article.markdown.slice(0, (params.content_chars as number) ?? 20000)}`;
|
|
119
|
+
return { content: [{ type: "text" as const, text: truncateText(text) }], details: { url: parsed.href, ...article } };
|
|
120
|
+
},
|
|
121
|
+
});
|
|
276
122
|
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
123
|
+
// ── SearXNG Search ───────────────────────────────────────────────────
|
|
124
|
+
pi.registerTool({
|
|
125
|
+
name: "searxng_search",
|
|
126
|
+
label: "SearXNG Search",
|
|
127
|
+
description:
|
|
128
|
+
"Search the web through the configured self-hosted SearXNG metasearch instance. Best for free, self-hosted source discovery without hosted API keys.",
|
|
129
|
+
promptSnippet: "Search web results with self-hosted SearXNG",
|
|
130
|
+
promptGuidelines: [
|
|
131
|
+
"Use SearXNG for self-hosted metasearch and source discovery, especially when Brave is unavailable or avoiding hosted search APIs.",
|
|
132
|
+
"Use SearXNG before browser/scrape tools when the task starts with finding URLs or sources.",
|
|
133
|
+
"Cite result URLs when SearXNG results materially support the answer.",
|
|
134
|
+
],
|
|
135
|
+
parameters: Type.Object({
|
|
136
|
+
...searxngControlSchema,
|
|
137
|
+
query: Type.String(),
|
|
138
|
+
count: Type.Optional(Type.Number({ default: 5 })),
|
|
139
|
+
pageno: Type.Optional(Type.Number({ default: 1 })),
|
|
140
|
+
categories: Type.Optional(Type.String({ description: "Comma-separated SearXNG categories, e.g. general,science." })),
|
|
141
|
+
engines: Type.Optional(Type.String({ description: "Comma-separated SearXNG engines, e.g. brave,wikipedia." })),
|
|
142
|
+
language: Type.Optional(Type.String()),
|
|
143
|
+
time_range: Type.Optional(Type.Union([Type.Literal("day"), Type.Literal("month"), Type.Literal("year")])),
|
|
144
|
+
safesearch: Type.Optional(Type.Number({ description: "SearXNG safesearch level: 0, 1, or 2." })),
|
|
145
|
+
}),
|
|
146
|
+
async execute(_id: string, params: Record<string, unknown>, signal: AbortSignal, _onUpdate: unknown, ctx: any) {
|
|
147
|
+
const config = loadSearxngConfig(params, cwdFromContext(ctx), includeProjectEnv(ctx));
|
|
148
|
+
const result = await fetchSearxngResults(params, config.baseUrl, signal);
|
|
149
|
+
return { content: [{ type: "text" as const, text: truncateText(formatSearchResults(result.results || [])) }], details: { ...result, baseUrl: config.baseUrl } };
|
|
150
|
+
},
|
|
151
|
+
});
|
|
282
152
|
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
153
|
+
// ── Firecrawl Search ─────────────────────────────────────────────────
|
|
154
|
+
pi.registerTool({
|
|
155
|
+
name: "firecrawl_search",
|
|
156
|
+
label: "Firecrawl Search",
|
|
157
|
+
description:
|
|
158
|
+
"Search web/news/images through Firecrawl, optionally scraping result markdown. Best when SearXNG/Brave are unavailable or when Firecrawl-backed search/scrape is preferred.",
|
|
159
|
+
promptSnippet: "Search with Firecrawl",
|
|
160
|
+
promptGuidelines: [
|
|
161
|
+
"Use when SearXNG and Brave are missing/rate-limited/insufficient, when Firecrawl search is preferred, or when you need scraped markdown from results.",
|
|
162
|
+
"Keep limits conservative; scraped search is slower and heavier than plain search.",
|
|
163
|
+
"Cite result URLs and verify scraped content before relying on it.",
|
|
164
|
+
],
|
|
165
|
+
parameters: Type.Object({
|
|
166
|
+
...firecrawlControlSchema,
|
|
167
|
+
query: Type.String(),
|
|
168
|
+
limit: Type.Optional(Type.Number({ default: 5 })),
|
|
169
|
+
sources: Type.Optional(Type.String({ description: "Comma-separated sources: web,news,images", default: "web" })),
|
|
170
|
+
country: Type.Optional(Type.String()),
|
|
171
|
+
location: Type.Optional(Type.String()),
|
|
172
|
+
tbs: Type.Optional(Type.String()),
|
|
173
|
+
scrape: Type.Optional(Type.Boolean({ default: false })),
|
|
174
|
+
content_chars: Type.Optional(Type.Number({ default: 5000 })),
|
|
175
|
+
}),
|
|
176
|
+
async execute(_id: string, params: Record<string, unknown>, signal: AbortSignal, _onUpdate: unknown, ctx: any) {
|
|
177
|
+
const config = loadFirecrawlConfig(params, cwdFromContext(ctx), includeProjectEnv(ctx));
|
|
178
|
+
const sources = String(params.sources || "web")
|
|
179
|
+
.split(",")
|
|
180
|
+
.map((type: string) => ({ type: type.trim() }))
|
|
181
|
+
.filter((s: { type: string }) => s.type);
|
|
182
|
+
const body: Record<string, unknown> = {
|
|
183
|
+
query: params.query,
|
|
184
|
+
limit: Math.min(100, Math.max(1, (params.limit as number) ?? 5)),
|
|
185
|
+
sources,
|
|
186
|
+
...(params.country ? { country: String(params.country).toUpperCase() } : {}),
|
|
187
|
+
...(params.location ? { location: params.location } : {}),
|
|
188
|
+
...(params.tbs ? { tbs: params.tbs } : {}),
|
|
189
|
+
...(params.scrape ? { scrapeOptions: { formats: ["markdown"], onlyMainContent: true } } : {}),
|
|
190
|
+
};
|
|
191
|
+
const result = await firecrawlRequest(config, "POST", "/search", body, true, signal);
|
|
192
|
+
return { content: [{ type: "text" as const, text: truncateText(formatFirecrawlSearch((result as FirecrawlResult) as Record<string, unknown>, (params.content_chars as number) ?? 5000)) }], details: result };
|
|
193
|
+
},
|
|
194
|
+
});
|
|
290
195
|
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
196
|
+
// ── Firecrawl Scrape ─────────────────────────────────────────────────
|
|
197
|
+
pi.registerTool({
|
|
198
|
+
name: "firecrawl_scrape",
|
|
199
|
+
label: "Firecrawl Scrape",
|
|
200
|
+
description:
|
|
201
|
+
"Scrape a URL through Firecrawl as markdown/html/links/summary/json. Best for dynamic pages, higher-quality extraction, links, or structured JSON extraction.",
|
|
202
|
+
promptSnippet: "Scrape a URL with Firecrawl",
|
|
203
|
+
promptGuidelines: [
|
|
204
|
+
"Use for known URLs when extraction quality matters, pages are dynamic, or markdown/links/JSON extraction is needed.",
|
|
205
|
+
"Use prompt/schema only for explicit structured extraction tasks.",
|
|
206
|
+
"Prefer web_content for simple fast known-URL extraction when Firecrawl is unnecessary.",
|
|
207
|
+
],
|
|
208
|
+
parameters: Type.Object({
|
|
209
|
+
...firecrawlControlSchema,
|
|
210
|
+
url: Type.String(),
|
|
211
|
+
formats: Type.Optional(Type.String({ default: "markdown" })),
|
|
212
|
+
prompt: Type.Optional(Type.String()),
|
|
213
|
+
schema: Type.Optional(Type.Any()),
|
|
214
|
+
wait_for: Type.Optional(Type.Number()),
|
|
215
|
+
timeout: Type.Optional(Type.Number()),
|
|
216
|
+
mobile: Type.Optional(Type.Boolean()),
|
|
217
|
+
country: Type.Optional(Type.String()),
|
|
218
|
+
actions: Type.Optional(Type.Any()),
|
|
219
|
+
only_main_content: Type.Optional(Type.Boolean({ default: true })),
|
|
220
|
+
content_chars: Type.Optional(Type.Number({ default: 20000 })),
|
|
221
|
+
}),
|
|
222
|
+
async execute(_id: string, params: Record<string, unknown>, signal: AbortSignal, _onUpdate: unknown, ctx: any) {
|
|
223
|
+
const formats = String(params.formats || "markdown")
|
|
224
|
+
.split(",")
|
|
225
|
+
.map((f: string) => f.trim())
|
|
226
|
+
.filter(Boolean)
|
|
227
|
+
.map((f: string) =>
|
|
228
|
+
f === "json" && (params.prompt || params.schema)
|
|
229
|
+
? { type: "json", ...(params.prompt ? { prompt: params.prompt } : {}), ...(params.schema ? { schema: params.schema } : {}) }
|
|
230
|
+
: f,
|
|
231
|
+
);
|
|
232
|
+
const body: Record<string, unknown> = {
|
|
233
|
+
url: params.url,
|
|
234
|
+
formats,
|
|
235
|
+
onlyMainContent: params.only_main_content !== false,
|
|
236
|
+
...(params.wait_for ? { waitFor: params.wait_for } : {}),
|
|
237
|
+
...(params.timeout ? { timeout: params.timeout } : {}),
|
|
238
|
+
...(params.mobile ? { mobile: true } : {}),
|
|
239
|
+
...(params.country ? { location: { country: String(params.country).toUpperCase() } } : {}),
|
|
240
|
+
...(params.actions ? { actions: params.actions } : {}),
|
|
241
|
+
};
|
|
242
|
+
const result = await firecrawlRequest(loadFirecrawlConfig(params, cwdFromContext(ctx), includeProjectEnv(ctx)), "POST", "/scrape", body, true, signal);
|
|
243
|
+
return { content: [{ type: "text" as const, text: truncateText(formatFirecrawlScrape((result as FirecrawlResult) as Record<string, unknown>, (params.content_chars as number) ?? 20000)) }], details: result };
|
|
244
|
+
},
|
|
245
|
+
});
|
|
297
246
|
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
247
|
+
// ── Firecrawl Map ────────────────────────────────────────────────────
|
|
248
|
+
pi.registerTool({
|
|
249
|
+
name: "firecrawl_map",
|
|
250
|
+
label: "Firecrawl Map",
|
|
251
|
+
description:
|
|
252
|
+
"Discover URLs from a site with Firecrawl map. Best before crawling or when you need candidate pages from a site/docs section.",
|
|
253
|
+
promptSnippet: "Map site URLs",
|
|
254
|
+
promptGuidelines: [
|
|
255
|
+
"Use before crawling to identify relevant URLs and avoid unnecessary broad crawls.",
|
|
256
|
+
"Keep limits small unless the user explicitly asks for broad site discovery.",
|
|
257
|
+
"Use mapped URLs with firecrawl_scrape for targeted extraction.",
|
|
258
|
+
],
|
|
259
|
+
parameters: Type.Object({
|
|
260
|
+
...firecrawlControlSchema,
|
|
261
|
+
url: Type.String(),
|
|
262
|
+
limit: Type.Optional(Type.Number({ default: 100 })),
|
|
263
|
+
include_subdomains: Type.Optional(Type.Boolean({ default: false })),
|
|
264
|
+
}),
|
|
265
|
+
async execute(_id: string, params: Record<string, unknown>, signal: AbortSignal, _onUpdate: unknown, ctx: any) {
|
|
266
|
+
const result = await firecrawlRequest(
|
|
267
|
+
loadFirecrawlConfig(params, cwdFromContext(ctx), includeProjectEnv(ctx)),
|
|
268
|
+
"POST",
|
|
269
|
+
"/map",
|
|
270
|
+
{ url: params.url, limit: (params.limit as number) ?? 100, includeSubdomains: Boolean(params.include_subdomains) },
|
|
271
|
+
true,
|
|
272
|
+
signal,
|
|
273
|
+
);
|
|
274
|
+
const urls = result.data || result.links || result.urls || [];
|
|
275
|
+
const text = Array.isArray(urls)
|
|
276
|
+
? (urls as Array<Record<string, unknown> | string>).map((u: any) => u.url || u).join("\n")
|
|
277
|
+
: JSON.stringify(result, null, 2);
|
|
278
|
+
return { content: [{ type: "text" as const, text: truncateText(text) }], details: result };
|
|
279
|
+
},
|
|
280
|
+
});
|
|
304
281
|
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
282
|
+
// ── Firecrawl Crawl ──────────────────────────────────────────────────
|
|
283
|
+
pi.registerTool({
|
|
284
|
+
name: "firecrawl_crawl",
|
|
285
|
+
label: "Firecrawl Crawl",
|
|
286
|
+
description:
|
|
287
|
+
"Start a conservative Firecrawl site crawl and optionally poll for results. Best for small docs/site sections after mapping or when multiple pages are required.",
|
|
288
|
+
promptSnippet: "Crawl a small site section",
|
|
289
|
+
promptGuidelines: [
|
|
290
|
+
"Use only when multiple pages from a site are needed; prefer firecrawl_map plus targeted scrape when possible.",
|
|
291
|
+
"Keep limits conservative and use include/exclude paths to narrow scope.",
|
|
292
|
+
"Avoid large crawls unless the user explicitly requests them and accepts runtime/load.",
|
|
293
|
+
],
|
|
294
|
+
parameters: Type.Object({
|
|
295
|
+
...firecrawlControlSchema,
|
|
296
|
+
url: Type.String(),
|
|
297
|
+
limit: Type.Optional(Type.Number({ default: 10 })),
|
|
298
|
+
include_paths: Type.Optional(Type.String()),
|
|
299
|
+
exclude_paths: Type.Optional(Type.String()),
|
|
300
|
+
poll: Type.Optional(Type.Boolean({ default: false })),
|
|
301
|
+
}),
|
|
302
|
+
async execute(_id: string, params: Record<string, unknown>, signal: AbortSignal, _onUpdate: unknown, ctx: any) {
|
|
303
|
+
const config = loadFirecrawlConfig(params, cwdFromContext(ctx), includeProjectEnv(ctx));
|
|
304
|
+
let result = await firecrawlRequest(
|
|
305
|
+
config,
|
|
306
|
+
"POST",
|
|
307
|
+
"/crawl",
|
|
308
|
+
{
|
|
309
|
+
url: params.url,
|
|
310
|
+
limit: Math.min(10000, Math.max(1, (params.limit as number) ?? 10)),
|
|
311
|
+
includePaths: params.include_paths
|
|
312
|
+
? String(params.include_paths)
|
|
313
|
+
.split(",")
|
|
314
|
+
.map((s: string) => s.trim())
|
|
315
|
+
.filter(Boolean)
|
|
316
|
+
: [],
|
|
317
|
+
excludePaths: params.exclude_paths
|
|
318
|
+
? String(params.exclude_paths)
|
|
319
|
+
.split(",")
|
|
320
|
+
.map((s: string) => s.trim())
|
|
321
|
+
.filter(Boolean)
|
|
322
|
+
: [],
|
|
323
|
+
scrapeOptions: { formats: ["markdown"], onlyMainContent: true },
|
|
324
|
+
},
|
|
325
|
+
true,
|
|
326
|
+
signal,
|
|
327
|
+
);
|
|
328
|
+
const id = result.id || (result.data as Record<string, unknown> | undefined)?.id;
|
|
329
|
+
if (params.poll && id && !Array.isArray(result.data)) {
|
|
330
|
+
const { abortableSleep } = await import("./lib/retry");
|
|
331
|
+
for (let i = 0; i < 60; i++) {
|
|
332
|
+
result = await firecrawlRequest(config, "GET", `/crawl/${id}`, undefined, true, signal);
|
|
333
|
+
if (["completed", "failed", "cancelled"].includes(String(result.status || (result.data as Record<string, unknown> | undefined)?.status || ""))) break;
|
|
334
|
+
await abortableSleep(2000, signal);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
const pages = Array.isArray(result.data)
|
|
338
|
+
? (result.data as Record<string, unknown>[])
|
|
339
|
+
: ((result.data as Record<string, unknown>)?.data as Record<string, unknown>[]) || [];
|
|
340
|
+
const text = pages.length
|
|
341
|
+
? pages
|
|
342
|
+
.map((p: Record<string, unknown>) => formatFirecrawlScrape({ data: p } as Record<string, unknown>, 12000))
|
|
343
|
+
.join("\n\n---\n\n")
|
|
344
|
+
: id
|
|
345
|
+
? `Crawl started: ${id}\nUse poll=true or check Firecrawl status/dashboard.`
|
|
346
|
+
: JSON.stringify(result, null, 2);
|
|
347
|
+
return { content: [{ type: "text" as const, text: truncateText(text) }], details: result };
|
|
348
|
+
},
|
|
349
|
+
});
|
|
316
350
|
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
351
|
+
// ── Web Status ───────────────────────────────────────────────────────
|
|
352
|
+
pi.registerTool({
|
|
353
|
+
name: "web_status",
|
|
354
|
+
label: "Web Provider Status",
|
|
355
|
+
description:
|
|
356
|
+
"Show Brave, SearXNG, and Firecrawl configuration status without printing secrets.",
|
|
357
|
+
promptSnippet: "Check web provider configuration",
|
|
358
|
+
promptGuidelines: [
|
|
359
|
+
"Use when web tools fail due to missing credentials/config or before comparing Brave, SearXNG, and Firecrawl availability.",
|
|
360
|
+
"Never print API key values; this tool reports only presence and source.",
|
|
361
|
+
],
|
|
362
|
+
parameters: Type.Object({}),
|
|
363
|
+
async execute(_id: string, _params: Record<string, unknown>, _signal: AbortSignal, _onUpdate: unknown, ctx: any) {
|
|
364
|
+
const cwd = cwdFromContext(ctx);
|
|
365
|
+
const trusted = includeProjectEnv(ctx);
|
|
366
|
+
const braveKey = findEnvValue("BRAVE_API_KEY", cwd, trusted);
|
|
367
|
+
const searxngUrl = findEnvValue("SEARXNG_BASE_URL", cwd, trusted);
|
|
368
|
+
const fireKey = findEnvValue("FIRECRAWL_API_KEY", cwd, trusted);
|
|
369
|
+
const fireUrl = findEnvValue("FIRECRAWL_API_URL", cwd, trusted);
|
|
370
|
+
const status = {
|
|
371
|
+
brave: { apiKeyFound: Boolean(braveKey.value), apiKeySource: braveKey.value ? braveKey.source : "not set" },
|
|
372
|
+
searxng: {
|
|
373
|
+
baseUrl: normalizeSearxngBaseUrl(searxngUrl.value),
|
|
374
|
+
baseUrlSource: searxngUrl.source || "default local",
|
|
375
|
+
},
|
|
376
|
+
firecrawl: {
|
|
377
|
+
baseUrl: normalizeFirecrawlBaseUrl(fireUrl.value),
|
|
378
|
+
apiUrlSource: fireUrl.source || "default hosted",
|
|
379
|
+
apiKeyFound: Boolean(fireKey.value),
|
|
380
|
+
apiKeySource: fireKey.value ? fireKey.source : "not set",
|
|
381
|
+
hostedMode: !fireUrl.value || normalizeFirecrawlBaseUrl(fireUrl.value).startsWith(HOSTED_FIRECRAWL_BASE_URL),
|
|
382
|
+
},
|
|
383
|
+
};
|
|
384
|
+
return { content: [{ type: "text" as const, text: JSON.stringify(status, null, 2) }], details: status };
|
|
385
|
+
},
|
|
386
|
+
});
|
|
323
387
|
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
388
|
+
// ── /web-status command ──────────────────────────────────────────────
|
|
389
|
+
pi.registerCommand("web-status", {
|
|
390
|
+
description: "Show web provider configuration status",
|
|
391
|
+
handler: async (_args: string, ctx: any) => {
|
|
392
|
+
const cwd = cwdFromContext(ctx);
|
|
393
|
+
const trusted = includeProjectEnv(ctx);
|
|
394
|
+
const braveKey = findEnvValue("BRAVE_API_KEY", cwd, trusted);
|
|
395
|
+
const searxngUrl = findEnvValue("SEARXNG_BASE_URL", cwd, trusted);
|
|
396
|
+
const fireKey = findEnvValue("FIRECRAWL_API_KEY", cwd, trusted);
|
|
397
|
+
const fireUrl = findEnvValue("FIRECRAWL_API_URL", cwd, trusted);
|
|
398
|
+
ctx.ui.notify(
|
|
399
|
+
`Brave API key: ${braveKey.value ? `found (${braveKey.source})` : "not set"}\nSearXNG base URL: ${normalizeSearxngBaseUrl(searxngUrl.value)} (${searxngUrl.source || "default local"})\nFirecrawl base URL: ${normalizeFirecrawlBaseUrl(fireUrl.value)}\nFirecrawl API key: ${fireKey.value ? `found (${fireKey.source})` : "not set"}`,
|
|
400
|
+
"info",
|
|
401
|
+
);
|
|
402
|
+
},
|
|
403
|
+
});
|
|
329
404
|
}
|