forked from nicobailon/pi-web-access
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtinyfish.ts
More file actions
397 lines (361 loc) · 13 KB
/
Copy pathtinyfish.ts
File metadata and controls
397 lines (361 loc) · 13 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
import { existsSync, readFileSync } from "node:fs";
import { activityMonitor } from "./activity.ts";
import type { ExtractedContent, ExtractOptions } from "./extract.ts";
import type { SearchOptions, SearchResponse } from "./perplexity.ts";
import { hasCredentialSource, redactCredential, resolveCredential } from "./credential-source.ts";
import { getWebSearchConfigPath } from "./utils.ts";
const TINYFISH_SEARCH_URL = "https://api.search.tinyfish.ai";
const TINYFISH_FETCH_URL = "https://api.fetch.tinyfish.ai";
const CONFIG_PATH = getWebSearchConfigPath();
const SEARCH_TIMEOUT_MS = 60_000;
const FETCH_TIMEOUT_MS = 150_000;
const MAX_FETCH_URLS = 10;
const MAX_FETCH_PER_URL_TIMEOUT_MS = 110_000;
interface WebSearchConfig {
tinyfishApiKey?: unknown;
}
interface TinyFishSearchResult {
position?: number;
site_name?: string | null;
title?: string | null;
snippet?: string | null;
url?: string | null;
date?: string | null;
publisher?: string | null;
}
interface TinyFishSearchResponse {
query?: string;
results?: TinyFishSearchResult[];
total_results?: number;
page?: number;
}
interface TinyFishFetchResult {
url?: string;
final_url?: string;
title?: string | null;
text?: string | Record<string, unknown> | null;
format?: string;
}
interface TinyFishFetchError {
url?: string;
error?: string;
status?: number;
}
interface TinyFishFetchResponse {
results?: TinyFishFetchResult[];
errors?: TinyFishFetchError[];
}
interface TinyFishSearchOptions extends SearchOptions {
includeContent?: boolean;
}
let cachedConfig: WebSearchConfig | null = null;
function loadConfig(): WebSearchConfig {
if (cachedConfig) return cachedConfig;
if (!existsSync(CONFIG_PATH)) {
cachedConfig = {};
return cachedConfig;
}
const raw = readFileSync(CONFIG_PATH, "utf-8");
try {
cachedConfig = JSON.parse(raw) as WebSearchConfig;
return cachedConfig;
} catch (err) {
const message = err instanceof Error ? err.message : String(err);
throw new Error(`Failed to parse ${CONFIG_PATH}: ${message}`);
}
}
export function clearTinyFishConfigCache(): void {
cachedConfig = null;
}
async function getApiKey(signal?: AbortSignal): Promise<string> {
const key = await resolveCredential({
provider: "TinyFish",
configuredValue: loadConfig().tinyfishApiKey,
environmentValue: process.env.TINYFISH_API_KEY,
signal,
});
if (!key) {
throw new Error(
"TinyFish API key not found. Either:\n" +
` 1. Create ${CONFIG_PATH} with { "tinyfishApiKey": "your-key" }\n` +
" 2. Set TINYFISH_API_KEY environment variable\n" +
"Get a key at https://agent.tinyfish.ai/api-keys",
);
}
return key;
}
export function isTinyFishAvailable(): boolean {
return hasCredentialSource({
provider: "TinyFish",
configuredValue: loadConfig().tinyfishApiKey,
environmentValue: process.env.TINYFISH_API_KEY,
});
}
function errorMessage(err: unknown): string {
return err instanceof Error ? err.message : String(err);
}
function requestSignal(signal: AbortSignal | undefined, timeoutMs: number): AbortSignal {
const timeout = AbortSignal.timeout(timeoutMs);
return signal ? AbortSignal.any([signal, timeout]) : timeout;
}
function normalizeDomain(value: string): string | null {
let input = value.trim().toLowerCase();
if (!input) return null;
if (input.startsWith("-")) input = input.slice(1).trim();
if (!input) return null;
try {
const parsed = input.includes("://") ? new URL(input) : new URL(`https://${input}`);
input = parsed.hostname;
} catch {
input = input.split("/")[0]?.split(":")[0] ?? "";
}
input = input.replace(/^\.+|\.+$/g, "");
return /^[a-z0-9][a-z0-9.-]*\.[a-z]{2,}$/i.test(input) ? input : null;
}
function mapDomainFilter(domainFilter: string[] | undefined): { includeDomains: string[]; excludeDomains: string[] } {
const includeDomains: string[] = [];
const excludeDomains: string[] = [];
for (const raw of domainFilter ?? []) {
const domain = normalizeDomain(raw);
if (!domain) continue;
const target = raw.trim().startsWith("-") ? excludeDomains : includeDomains;
if (!target.includes(domain)) target.push(domain);
}
return { includeDomains, excludeDomains };
}
function recencyMinutes(filter: SearchOptions["recencyFilter"]): number | undefined {
if (!filter) return undefined;
const minutes: Record<NonNullable<SearchOptions["recencyFilter"]>, number> = {
day: 1_440,
week: 10_080,
month: 43_200,
year: 525_600,
};
return minutes[filter];
}
function normalizeNumResults(value: number | undefined): number {
if (typeof value !== "number" || !Number.isFinite(value)) return 5;
return Math.max(1, Math.min(Math.floor(value), 20));
}
function buildSearchUrl(query: string, options: SearchOptions, page: number): string {
const params = new URLSearchParams({ query });
const { includeDomains, excludeDomains } = mapDomainFilter(options.domainFilter);
if (includeDomains.length > 0) params.set("include_domains", includeDomains.join(","));
if (excludeDomains.length > 0) params.set("exclude_domains", excludeDomains.join(","));
const recency = recencyMinutes(options.recencyFilter);
if (recency !== undefined) params.set("recency_minutes", String(recency));
if (page > 0) params.set("page", String(page));
return `${TINYFISH_SEARCH_URL}?${params.toString()}`;
}
async function tinyFishJsonRequest<T>(
label: "Search" | "Fetch",
url: string,
apiKey: string,
init: RequestInit,
timeoutMs: number,
signal?: AbortSignal,
): Promise<T> {
let response: Response;
try {
response = await fetch(url, {
...init,
headers: {
"X-API-Key": apiKey,
...(init.body ? { "Content-Type": "application/json" } : {}),
...init.headers,
},
signal: requestSignal(signal, timeoutMs),
});
} catch (err) {
const message = errorMessage(err);
const redactedMessage = redactCredential(message, apiKey);
if (redactedMessage === message) throw err;
const redactedError = new Error(redactedMessage);
if (err instanceof Error) redactedError.name = err.name;
throw redactedError;
}
const raw = await response.text();
if (!response.ok) {
throw new Error(`TinyFish ${label} API error ${response.status}: ${redactCredential(raw, apiKey).slice(0, 300)}`);
}
try {
return JSON.parse(raw) as T;
} catch (err) {
throw new Error(`TinyFish ${label} API returned invalid JSON: ${errorMessage(err)}`);
}
}
function mapSearchResults(results: TinyFishSearchResult[] | undefined): SearchResponse["results"] {
if (!Array.isArray(results)) return [];
return results.flatMap((item) => {
if (!item || typeof item.url !== "string" || item.url.trim().length === 0) return [];
const url = item.url.trim();
return [{
title: typeof item.title === "string" && item.title.trim() ? item.title.trim() : url,
url,
snippet: typeof item.snippet === "string" ? item.snippet.replace(/\s+/g, " ").trim() : "",
}];
});
}
function deduplicateResults(results: SearchResponse["results"], limit: number): SearchResponse["results"] {
const seen = new Set<string>();
const unique: SearchResponse["results"] = [];
for (const result of results) {
if (seen.has(result.url)) continue;
seen.add(result.url);
unique.push(result);
if (unique.length >= limit) break;
}
return unique;
}
function buildAnswer(results: SearchResponse["results"]): string {
return results.map((result) => {
if (result.snippet) return `${result.snippet}\nSource: ${result.title} (${result.url})`;
return `Source: ${result.title} (${result.url})`;
}).join("\n\n");
}
function fetchPerUrlTimeout(value: number | undefined): number {
if (typeof value !== "number" || !Number.isFinite(value)) return MAX_FETCH_PER_URL_TIMEOUT_MS;
return Math.max(1, Math.min(Math.floor(value), MAX_FETCH_PER_URL_TIMEOUT_MS));
}
function fetchBody(urls: string[], options: ExtractOptions = {}): Record<string, unknown> {
const body: Record<string, unknown> = {
urls,
format: "markdown",
per_url_timeout_ms: fetchPerUrlTimeout(options.timeoutMs),
};
const purpose = options.prompt?.trim();
if (purpose) body.purpose = purpose.slice(0, 2_000);
return body;
}
function findFetchError(errors: TinyFishFetchError[] | undefined, url: string): TinyFishFetchError | undefined {
if (!Array.isArray(errors)) return undefined;
return errors.find(item => item?.url === url) ?? errors[0];
}
function fetchResultContent(result: TinyFishFetchResult): string {
if (typeof result.text === "string") return result.text.trim();
if (result.text && typeof result.text === "object") return JSON.stringify(result.text, null, 2);
return "";
}
function mapFetchResult(result: TinyFishFetchResult | undefined, requestedUrl: string): ExtractedContent | null {
if (!result) return null;
const content = fetchResultContent(result);
if (!content) return null;
return {
url: requestedUrl,
title: typeof result.title === "string" ? result.title.trim() : "",
content,
error: null,
};
}
async function fetchBatch(
urls: string[],
apiKey: string,
signal?: AbortSignal,
options: ExtractOptions = {},
): Promise<TinyFishFetchResponse> {
return tinyFishJsonRequest<TinyFishFetchResponse>(
"Fetch",
TINYFISH_FETCH_URL,
apiKey,
{ method: "POST", body: JSON.stringify(fetchBody(urls, options)) },
FETCH_TIMEOUT_MS,
signal,
);
}
async function fetchInlineContent(
urls: string[],
apiKey: string,
signal?: AbortSignal,
): Promise<ExtractedContent[]> {
const content: ExtractedContent[] = [];
for (let offset = 0; offset < urls.length; offset += MAX_FETCH_URLS) {
const batch = urls.slice(offset, offset + MAX_FETCH_URLS);
const data = await fetchBatch(batch, apiKey, signal);
if (!Array.isArray(data.results) || !Array.isArray(data.errors)) {
throw new Error("TinyFish Fetch API returned an unexpected response shape");
}
for (const url of batch) {
const result = Array.isArray(data.results)
? data.results.find(item => item?.url === url || item?.final_url === url)
: undefined;
const mapped = mapFetchResult(result, url);
if (mapped) content.push(mapped);
}
}
return content;
}
export async function searchWithTinyFish(query: string, options: TinyFishSearchOptions = {}): Promise<SearchResponse> {
const apiKey = await getApiKey(options.signal);
const numResults = normalizeNumResults(options.numResults);
const activityId = activityMonitor.logStart({ type: "api", query });
try {
const combined: SearchResponse["results"] = [];
const pages = numResults > 10 ? 2 : 1;
for (let page = 0; page < pages; page++) {
const data = await tinyFishJsonRequest<TinyFishSearchResponse>(
"Search",
buildSearchUrl(query, options, page),
apiKey,
{ method: "GET" },
SEARCH_TIMEOUT_MS,
options.signal,
);
if (!Array.isArray(data.results)) throw new Error("TinyFish Search API returned an unexpected response shape");
combined.push(...mapSearchResults(data.results));
if (data.results.length < 10) break;
}
const results = deduplicateResults(combined, numResults);
const response: SearchResponse = { answer: buildAnswer(results), results };
if (options.includeContent && results.length > 0) {
const inlineContent = await fetchInlineContent(results.map(result => result.url), apiKey, options.signal);
if (inlineContent.length > 0) response.inlineContent = inlineContent;
}
activityMonitor.logComplete(activityId, 200);
return response;
} catch (err) {
const message = errorMessage(err);
const redactedMessage = redactCredential(message, apiKey);
if (redactedMessage.toLowerCase().includes("abort")) activityMonitor.logComplete(activityId, 0);
else activityMonitor.logError(activityId, redactedMessage);
if (redactedMessage === message) throw err;
const redactedError = new Error(redactedMessage);
if (err instanceof Error) redactedError.name = err.name;
throw redactedError;
}
}
export async function extractWithTinyFish(
url: string,
signal?: AbortSignal,
options: ExtractOptions = {},
): Promise<ExtractedContent | null> {
const apiKey = await getApiKey(signal);
const activityId = activityMonitor.logStart({ type: "fetch", url });
try {
const data = await fetchBatch([url], apiKey, signal, options);
if (!Array.isArray(data.results) || !Array.isArray(data.errors)) {
throw new Error("TinyFish Fetch API returned an unexpected response shape");
}
const result = data.results.find(item => item?.url === url || item?.final_url === url) ?? data.results[0];
const mapped = mapFetchResult(result, url);
if (mapped) {
activityMonitor.logComplete(activityId, 200);
return mapped;
}
const fetchError = findFetchError(data.errors, url);
if (fetchError) {
const status = typeof fetchError.status === "number" ? ` (HTTP ${fetchError.status})` : "";
throw new Error(`TinyFish Fetch failed for ${url}: ${fetchError.error || "unknown error"}${status}`);
}
activityMonitor.logComplete(activityId, 200);
return null;
} catch (err) {
const message = errorMessage(err);
const redactedMessage = redactCredential(message, apiKey);
if (redactedMessage.toLowerCase().includes("abort")) activityMonitor.logComplete(activityId, 0);
else activityMonitor.logError(activityId, redactedMessage);
if (redactedMessage === message) throw err;
const redactedError = new Error(redactedMessage);
if (err instanceof Error) redactedError.name = err.name;
throw redactedError;
}
}