summaryrefslogtreecommitdiffhomepage
path: root/packages/tool-web-search/src/client.ts
blob: 071ba97e957a02ca12e4a223e6e2101a086cb2f4 (plain)
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
/**
 * FirecrawlClient — the injected outermost edge for the web_search tool.
 *
 * All effects (fetch, sleep, clock) are injected so the pure decision logic
 * remains testable without real I/O. The factory builds four methods
 * (`search`, `scrape`, `crawl`, `map`) over a self-hosted Firecrawl instance
 * (no API key). `crawl` polls a status URL until the crawl completes or fails.
 */

import type { CrawlPage, ScrapeResult, SearchHit } from "./format.js";

export type FetchLike = typeof globalThis.fetch;

export const DEFAULT_BASE_URL = "http://100.102.55.49:31329/v1";
export const DEFAULT_TIMEOUT_MS = 30_000;
export const CRAWL_POLL_MS = 2_000;
export const CRAWL_MAX_WAIT_MS = 5 * 60 * 1_000;

export interface SearchParams {
	readonly query: string;
	readonly limit: number;
	readonly lang?: string;
	readonly country?: string;
	readonly scrapeOptions?: {
		readonly formats: readonly string[];
		readonly onlyMainContent: boolean;
	};
}

export interface ScrapeParams {
	readonly url: string;
	readonly formats: readonly string[];
}

export interface CrawlParams {
	readonly url: string;
	readonly limit: number;
	readonly formats: readonly string[];
}

export interface FirecrawlClient {
	readonly search: (params: SearchParams, signal: AbortSignal) => Promise<readonly SearchHit[]>;
	readonly scrape: (params: ScrapeParams, signal: AbortSignal) => Promise<ScrapeResult>;
	readonly crawl: (params: CrawlParams, signal: AbortSignal) => Promise<readonly CrawlPage[]>;
	readonly map: (url: string, signal: AbortSignal) => Promise<readonly string[]>;
}

export interface FirecrawlClientDeps {
	readonly baseUrl: string;
	readonly fetchFn: FetchLike;
	readonly timeoutMs?: number;
	readonly pollMs?: number;
	readonly maxWaitMs?: number;
	readonly now?: () => number;
	readonly sleep?: (ms: number, signal: AbortSignal) => Promise<void>;
}

interface SearchResponse {
	readonly success: boolean;
	readonly data?: readonly SearchHit[];
	readonly error?: string;
}

interface ScrapeResponse {
	readonly success: boolean;
	readonly data?: {
		readonly markdown?: string;
		readonly metadata?: { readonly title?: string };
	};
	readonly error?: string;
}

interface CrawlStartResponse {
	readonly success: boolean;
	readonly url?: string;
	readonly error?: string;
}

interface CrawlStatusResponse {
	readonly status: string;
	readonly data?: readonly CrawlPage[];
	readonly error?: string;
}

interface MapResponse {
	readonly success: boolean;
	readonly links?: readonly string[];
	readonly error?: string;
}

/** Default sleep: resolve after `ms`, reject on abort. */
async function defaultSleep(ms: number, signal: AbortSignal): Promise<void> {
	return new Promise<void>((resolve, reject) => {
		if (signal.aborted) {
			reject(new Error("Request aborted."));
			return;
		}
		let timer: ReturnType<typeof setTimeout> | undefined;
		const onAbort = (): void => {
			if (timer !== undefined) {
				clearTimeout(timer);
			}
			reject(new Error("Request aborted."));
		};
		timer = setTimeout(() => {
			signal.removeEventListener("abort", onAbort);
			resolve();
		}, ms);
		signal.addEventListener("abort", onAbort, { once: true });
	});
}

/**
 * Create a FirecrawlClient. Each method builds a fetch request, calls the
 * injected `fetchFn`, and handles HTTP + JSON errors. The per-request timeout
 * is combined with the caller's cancellation signal via `AbortSignal.any`.
 */
export function createFirecrawlClient(deps: FirecrawlClientDeps): FirecrawlClient {
	const baseUrl = deps.baseUrl;
	const fetchFn = deps.fetchFn;
	const timeoutMs = deps.timeoutMs ?? DEFAULT_TIMEOUT_MS;
	const pollMs = deps.pollMs ?? CRAWL_POLL_MS;
	const maxWaitMs = deps.maxWaitMs ?? CRAWL_MAX_WAIT_MS;
	const now = deps.now ?? Date.now;
	const sleep = deps.sleep ?? defaultSleep;

	async function request(
		method: "POST" | "GET",
		url: string,
		body: unknown,
		signal: AbortSignal,
	): Promise<unknown> {
		const controller = new AbortController();
		const timeout = setTimeout(() => controller.abort(), timeoutMs);
		const combined = AbortSignal.any([signal, controller.signal]);
		try {
			let response: Response;
			try {
				response = await fetchFn(url, {
					method,
					headers:
						body !== undefined
							? { "Content-Type": "application/json", Accept: "application/json" }
							: { Accept: "application/json" },
					body: body !== undefined ? JSON.stringify(body) : undefined,
					signal: combined,
				});
			} catch (err) {
				if (signal.aborted) {
					throw new Error("Request aborted.");
				}
				if (controller.signal.aborted) {
					throw new Error(`Firecrawl request timed out after ${timeoutMs / 1000} seconds.`);
				}
				throw err;
			}
			if (!response.ok) {
				const text = await response.text().catch(() => "");
				throw new Error(`HTTP ${response.status} ${response.statusText}${text ? `: ${text}` : ""}`);
			}
			try {
				return await response.json();
			} catch {
				throw new Error("Failed to parse Firecrawl response as JSON");
			}
		} finally {
			clearTimeout(timeout);
		}
	}

	async function post(endpoint: string, body: unknown, signal: AbortSignal): Promise<unknown> {
		return request("POST", `${baseUrl}/${endpoint}`, body, signal);
	}

	return {
		async search(params: SearchParams, signal: AbortSignal): Promise<readonly SearchHit[]> {
			const body: Record<string, unknown> = { query: params.query, limit: params.limit };
			if (params.lang !== undefined) {
				body.lang = params.lang;
			}
			if (params.country !== undefined) {
				body.country = params.country;
			}
			if (params.scrapeOptions !== undefined) {
				body.scrapeOptions = params.scrapeOptions;
			}
			const json = (await post("search", body, signal)) as SearchResponse;
			if (!json.success) {
				throw new Error(json.error ?? "Unknown error");
			}
			return json.data ?? [];
		},

		async scrape(params: ScrapeParams, signal: AbortSignal): Promise<ScrapeResult> {
			const body = {
				url: params.url,
				formats: params.formats,
				onlyMainContent: true,
			};
			const json = (await post("scrape", body, signal)) as ScrapeResponse;
			if (!json.success) {
				throw new Error(json.error ?? "Unknown error");
			}
			return json;
		},

		async crawl(params: CrawlParams, signal: AbortSignal): Promise<readonly CrawlPage[]> {
			const body = {
				url: params.url,
				limit: params.limit,
				scrapeOptions: { formats: params.formats, onlyMainContent: true },
			};
			const startJson = (await post("crawl", body, signal)) as CrawlStartResponse;
			if (!startJson.success) {
				throw new Error(startJson.error ?? "Unknown error");
			}
			const statusUrl = startJson.url;
			if (statusUrl === undefined) {
				throw new Error("crawl response missing status URL.");
			}
			const started = now();
			while (now() - started < maxWaitMs) {
				await sleep(pollMs, signal);
				const status = (await request("GET", statusUrl, undefined, signal)) as CrawlStatusResponse;
				if (status.status === "completed") {
					return status.data ?? [];
				}
				if (status.status === "failed") {
					throw new Error(`crawl failed: ${status.error ?? "unknown"}`);
				}
			}
			throw new Error("crawl timed out waiting for completion.");
		},

		async map(url: string, signal: AbortSignal): Promise<readonly string[]> {
			const json = (await post("map", { url }, signal)) as MapResponse;
			if (!json.success) {
				throw new Error(json.error ?? "Unknown error");
			}
			return json.links ?? [];
		},
	};
}