@howells/fieldwork-sdk 0.0.0-stage → 0.3.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/LICENSE +21 -0
- package/README.md +239 -2
- package/dist/adapter-CEAQuT7F.js +3 -0
- package/dist/adapter-DGehHCMo.d.ts +470 -0
- package/dist/brave.d.ts +164 -0
- package/dist/brave.js +399 -0
- package/dist/browserbase.d.ts +128 -0
- package/dist/browserbase.js +180 -0
- package/dist/errors-BiIBG9zN.js +65 -0
- package/dist/exa.d.ts +191 -0
- package/dist/exa.js +471 -0
- package/dist/fieldwork-B3x3cxDJ.d.ts +78 -0
- package/dist/firecrawl.d.ts +322 -0
- package/dist/firecrawl.js +932 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.js +91 -0
- package/dist/parallel.d.ts +230 -0
- package/dist/parallel.js +696 -0
- package/dist/perplexity.d.ts +101 -0
- package/dist/perplexity.js +333 -0
- package/dist/serpapi.d.ts +172 -0
- package/dist/serpapi.js +517 -0
- package/dist/tavily.d.ts +157 -0
- package/dist/tavily.js +417 -0
- package/dist/tools.d.ts +104 -0
- package/dist/tools.js +1367 -0
- package/dist/types-CnomnF-6.js +27 -0
- package/dist/util-CYenIQQT.js +265 -0
- package/package.json +98 -3
package/dist/serpapi.js
ADDED
|
@@ -0,0 +1,517 @@
|
|
|
1
|
+
import { t as defineAdapter } from "./adapter-CEAQuT7F.js";
|
|
2
|
+
import { b as requireKey, c as num, d as record, f as refuseTogether, g as text, h as strings, i as compact, m as scalar, o as list, s as meta, t as callSettings, v as createRawClient } from "./util-CYenIQQT.js";
|
|
3
|
+
import { t as FieldworkError } from "./errors-BiIBG9zN.js";
|
|
4
|
+
// src/adapters/serpapi/parse.ts
|
|
5
|
+
const provider = "serpapi";
|
|
6
|
+
const invalid = (message) => new FieldworkError("InvalidRequest", `${provider} ${message}`, { provider });
|
|
7
|
+
const priceOf = (item) => {
|
|
8
|
+
const price = compact({
|
|
9
|
+
amount: scalar(item.extracted_price),
|
|
10
|
+
formatted: text(item.price)
|
|
11
|
+
});
|
|
12
|
+
return Object.keys(price).length > 0 ? price : void 0;
|
|
13
|
+
};
|
|
14
|
+
const webResults = (response) => list(response.organic_results).flatMap((entry) => {
|
|
15
|
+
const item = record(entry);
|
|
16
|
+
const url = text(item.link);
|
|
17
|
+
return url ? [compact({
|
|
18
|
+
author: text(item.author),
|
|
19
|
+
favicon: text(item.favicon),
|
|
20
|
+
image: text(item.thumbnail),
|
|
21
|
+
kind: "web",
|
|
22
|
+
publishedAt: text(item.date),
|
|
23
|
+
snippet: text(item.snippet),
|
|
24
|
+
title: text(item.title),
|
|
25
|
+
url
|
|
26
|
+
})] : [];
|
|
27
|
+
});
|
|
28
|
+
const newsItem = (entry) => {
|
|
29
|
+
const item = record(entry);
|
|
30
|
+
const url = text(item.link);
|
|
31
|
+
if (!url) return [];
|
|
32
|
+
const source = record(item.source);
|
|
33
|
+
const authors = strings(source.authors);
|
|
34
|
+
return [compact({
|
|
35
|
+
author: authors?.join(", "),
|
|
36
|
+
favicon: text(source.icon),
|
|
37
|
+
image: text(item.thumbnail),
|
|
38
|
+
kind: "news",
|
|
39
|
+
publishedAt: text(item.iso_date) ?? text(item.date),
|
|
40
|
+
title: text(item.title),
|
|
41
|
+
url
|
|
42
|
+
})];
|
|
43
|
+
};
|
|
44
|
+
const newsResults = (response) => list(response.news_results).flatMap((entry) => {
|
|
45
|
+
const item = record(entry);
|
|
46
|
+
return [
|
|
47
|
+
...newsItem(item),
|
|
48
|
+
...newsItem(item.highlight),
|
|
49
|
+
...list(item.stories).flatMap((story) => newsItem(story))
|
|
50
|
+
];
|
|
51
|
+
});
|
|
52
|
+
const shoppingResults = (response) => {
|
|
53
|
+
const categorized = list(response.categorized_shopping_results).flatMap((group) => list(record(group).shopping_results));
|
|
54
|
+
return ([
|
|
55
|
+
list(response.shopping_results),
|
|
56
|
+
categorized,
|
|
57
|
+
list(response.inline_shopping_results)
|
|
58
|
+
].find((group) => group.length > 0) ?? []).flatMap((entry) => {
|
|
59
|
+
const item = record(entry);
|
|
60
|
+
const url = text(item.link) ?? text(item.product_link);
|
|
61
|
+
return url ? [compact({
|
|
62
|
+
image: text(item.thumbnail),
|
|
63
|
+
kind: "shopping",
|
|
64
|
+
price: priceOf(item),
|
|
65
|
+
rating: num(item.rating),
|
|
66
|
+
reviewCount: num(item.reviews),
|
|
67
|
+
snippet: text(item.snippet),
|
|
68
|
+
title: text(item.title),
|
|
69
|
+
url
|
|
70
|
+
})] : [];
|
|
71
|
+
});
|
|
72
|
+
};
|
|
73
|
+
const mapsUrl = (item) => {
|
|
74
|
+
const website = text(item.website);
|
|
75
|
+
if (website) return website;
|
|
76
|
+
const id = text(item.place_id);
|
|
77
|
+
const title = text(item.title);
|
|
78
|
+
return id && title ? `https://www.google.com/maps/search/?api=1&query=${encodeURIComponent(title)}&query_place_id=${encodeURIComponent(id)}` : void 0;
|
|
79
|
+
};
|
|
80
|
+
const placeResults = (response) => {
|
|
81
|
+
const single = record(response.place_results);
|
|
82
|
+
const places = list(response.local_results);
|
|
83
|
+
return (places.length === 0 && Object.keys(single).length > 0 ? [single] : places).flatMap((entry) => {
|
|
84
|
+
const item = record(entry);
|
|
85
|
+
const url = mapsUrl(item);
|
|
86
|
+
if (!url) return [];
|
|
87
|
+
const coordinates = record(item.gps_coordinates);
|
|
88
|
+
const place = compact({
|
|
89
|
+
address: text(item.address),
|
|
90
|
+
latitude: num(coordinates.latitude),
|
|
91
|
+
longitude: num(coordinates.longitude)
|
|
92
|
+
});
|
|
93
|
+
return [compact({
|
|
94
|
+
image: text(item.thumbnail),
|
|
95
|
+
kind: "place",
|
|
96
|
+
place: Object.keys(place).length > 0 ? place : void 0,
|
|
97
|
+
rating: num(item.rating),
|
|
98
|
+
reviewCount: num(item.reviews),
|
|
99
|
+
snippet: text(item.description),
|
|
100
|
+
title: text(item.title),
|
|
101
|
+
url
|
|
102
|
+
})];
|
|
103
|
+
});
|
|
104
|
+
};
|
|
105
|
+
const scholarResults = (response) => list(response.organic_results).flatMap((entry) => {
|
|
106
|
+
const item = record(entry);
|
|
107
|
+
const url = text(item.link) ?? text(record(list(item.resources)[0]).link);
|
|
108
|
+
return url ? [compact({
|
|
109
|
+
kind: "scholar",
|
|
110
|
+
snippet: text(item.snippet),
|
|
111
|
+
title: text(item.title),
|
|
112
|
+
url
|
|
113
|
+
})] : [];
|
|
114
|
+
});
|
|
115
|
+
const patentResults = (response) => list(response.organic_results).flatMap((entry) => {
|
|
116
|
+
const item = record(entry);
|
|
117
|
+
const url = text(item.patent_link);
|
|
118
|
+
return url ? [compact({
|
|
119
|
+
author: text(item.inventor),
|
|
120
|
+
image: text(item.thumbnail),
|
|
121
|
+
kind: "patent",
|
|
122
|
+
publishedAt: text(item.publication_date),
|
|
123
|
+
snippet: text(item.snippet),
|
|
124
|
+
title: text(item.title),
|
|
125
|
+
url
|
|
126
|
+
})] : [];
|
|
127
|
+
});
|
|
128
|
+
const answerText = (response) => {
|
|
129
|
+
const box = record(response.answer_box ?? list(response.answer_box_list)[0]);
|
|
130
|
+
return text(box.answer) ?? text(scalar(box.result)) ?? text(box.snippet);
|
|
131
|
+
};
|
|
132
|
+
const searchMeta = (response) => meta({
|
|
133
|
+
requestId: record(response.search_metadata).id,
|
|
134
|
+
warnings: response.error
|
|
135
|
+
});
|
|
136
|
+
const failIfError = (response) => {
|
|
137
|
+
if (record(response.search_metadata).status === "Error") throw new FieldworkError("ProviderError", `serpapi search failed: ${text(response.error) ?? "no reason given"}`, {
|
|
138
|
+
cause: response,
|
|
139
|
+
provider,
|
|
140
|
+
retryable: true
|
|
141
|
+
});
|
|
142
|
+
};
|
|
143
|
+
// src/adapters/serpapi/options.ts
|
|
144
|
+
const RECENCY_CODE = {
|
|
145
|
+
day: "d",
|
|
146
|
+
hour: "h",
|
|
147
|
+
month: "m",
|
|
148
|
+
week: "w",
|
|
149
|
+
year: "y"
|
|
150
|
+
};
|
|
151
|
+
const str = (value) => typeof value === "string" ? value : void 0;
|
|
152
|
+
const count = (value) => typeof value === "number" ? value : void 0;
|
|
153
|
+
const googleCountry = (value) => {
|
|
154
|
+
const code = str(value)?.toLowerCase();
|
|
155
|
+
return code === "gb" ? "uk" : code;
|
|
156
|
+
};
|
|
157
|
+
const isoDate = (name, value) => {
|
|
158
|
+
if (value === void 0) return;
|
|
159
|
+
if (typeof value !== "string" || !/^\d{4}-\d{2}-\d{2}$/u.test(value)) throw invalid(`${name} must be YYYY-MM-DD`);
|
|
160
|
+
return value;
|
|
161
|
+
};
|
|
162
|
+
const compactDate = (name, value) => isoDate(name, value)?.replaceAll("-", "");
|
|
163
|
+
const usDate = (name, value) => {
|
|
164
|
+
const [year, month, day] = isoDate(name, value)?.split("-") ?? [];
|
|
165
|
+
return year === void 0 ? void 0 : `${Number(month)}/${Number(day)}/${year}`;
|
|
166
|
+
};
|
|
167
|
+
const bounded = (engine, value, min, max) => {
|
|
168
|
+
if (value === void 0) return;
|
|
169
|
+
if (typeof value !== "number" || !Number.isInteger(value) || value < min || value > max) throw invalid(`${engine} limit must be a whole number from ${min} to ${max}`);
|
|
170
|
+
return value;
|
|
171
|
+
};
|
|
172
|
+
const googleTbs = (o) => {
|
|
173
|
+
refuseTogether(provider, o, "recency", "publishedAfter");
|
|
174
|
+
refuseTogether(provider, o, "recency", "publishedBefore");
|
|
175
|
+
if (o.recency !== void 0) return `qdr:${RECENCY_CODE[o.recency]}`;
|
|
176
|
+
const after = usDate("publishedAfter", o.publishedAfter);
|
|
177
|
+
const before = usDate("publishedBefore", o.publishedBefore);
|
|
178
|
+
if (after === void 0 && before === void 0) return;
|
|
179
|
+
return [
|
|
180
|
+
"cdr:1",
|
|
181
|
+
after === void 0 ? void 0 : `cd_min:${after}`,
|
|
182
|
+
before === void 0 ? void 0 : `cd_max:${before}`
|
|
183
|
+
].filter((part) => part !== void 0).join(",");
|
|
184
|
+
};
|
|
185
|
+
const duckDuckGoFilter = (o) => {
|
|
186
|
+
refuseTogether(provider, o, "recency", "publishedAfter");
|
|
187
|
+
refuseTogether(provider, o, "recency", "publishedBefore");
|
|
188
|
+
if (o.recency === "hour") throw invalid("duckduckgo has no hour window; use day, week, month or year");
|
|
189
|
+
if (o.recency !== void 0) return RECENCY_CODE[o.recency];
|
|
190
|
+
if (o.publishedAfter === void 0 && o.publishedBefore === void 0) return;
|
|
191
|
+
if (o.publishedAfter === void 0 || o.publishedBefore === void 0) throw invalid("duckduckgo filters a date range by both ends: pass publishedAfter and publishedBefore together");
|
|
192
|
+
return `${isoDate("publishedAfter", o.publishedAfter)}..${isoDate("publishedBefore", o.publishedBefore)}`;
|
|
193
|
+
};
|
|
194
|
+
const patentDate = (name, value) => {
|
|
195
|
+
const date = compactDate(name, value);
|
|
196
|
+
return date === void 0 ? void 0 : `publication:${date}`;
|
|
197
|
+
};
|
|
198
|
+
const mapsOrigin = (o) => {
|
|
199
|
+
refuseTogether(provider, o, "location", "latitude");
|
|
200
|
+
refuseTogether(provider, o, "location", "longitude");
|
|
201
|
+
const coordinates = o.latitude !== void 0 || o.longitude !== void 0;
|
|
202
|
+
if (coordinates && (o.latitude === void 0 || o.longitude === void 0)) throw invalid("google_maps needs latitude and longitude together");
|
|
203
|
+
const origin = coordinates || o.location !== void 0;
|
|
204
|
+
if (origin && o.zoom === void 0) throw invalid("google_maps needs zoom with location or latitude and longitude");
|
|
205
|
+
if (!origin && o.zoom !== void 0) throw invalid("google_maps zoom needs location or latitude and longitude");
|
|
206
|
+
return {
|
|
207
|
+
lat: count(o.latitude),
|
|
208
|
+
location: str(o.location),
|
|
209
|
+
lon: count(o.longitude),
|
|
210
|
+
z: count(o.zoom)
|
|
211
|
+
};
|
|
212
|
+
};
|
|
213
|
+
// src/adapters/serpapi/engines.ts
|
|
214
|
+
const ENGINES = {
|
|
215
|
+
bing: {
|
|
216
|
+
allowed: [
|
|
217
|
+
"country",
|
|
218
|
+
"market",
|
|
219
|
+
"location",
|
|
220
|
+
"safeSearch",
|
|
221
|
+
"device",
|
|
222
|
+
"first",
|
|
223
|
+
"noCache"
|
|
224
|
+
],
|
|
225
|
+
params: (o) => {
|
|
226
|
+
refuseTogether(provider, o, "country", "market");
|
|
227
|
+
return {
|
|
228
|
+
cc: str(o.country)?.toLowerCase(),
|
|
229
|
+
device: str(o.device),
|
|
230
|
+
first: count(o.first),
|
|
231
|
+
location: str(o.location),
|
|
232
|
+
mkt: str(o.market),
|
|
233
|
+
safeSearch: str(o.safeSearch)
|
|
234
|
+
};
|
|
235
|
+
},
|
|
236
|
+
results: webResults
|
|
237
|
+
},
|
|
238
|
+
duckduckgo: {
|
|
239
|
+
allowed: [
|
|
240
|
+
"limit",
|
|
241
|
+
"region",
|
|
242
|
+
"safe",
|
|
243
|
+
"recency",
|
|
244
|
+
"publishedAfter",
|
|
245
|
+
"publishedBefore",
|
|
246
|
+
"start",
|
|
247
|
+
"noCache"
|
|
248
|
+
],
|
|
249
|
+
params: (o) => ({
|
|
250
|
+
df: duckDuckGoFilter(o),
|
|
251
|
+
kl: str(o.region),
|
|
252
|
+
m: bounded("duckduckgo", o.limit, 1, 50),
|
|
253
|
+
safe: count(o.safe),
|
|
254
|
+
start: count(o.start)
|
|
255
|
+
}),
|
|
256
|
+
results: webResults
|
|
257
|
+
},
|
|
258
|
+
google: {
|
|
259
|
+
allowed: [
|
|
260
|
+
"country",
|
|
261
|
+
"language",
|
|
262
|
+
"location",
|
|
263
|
+
"recency",
|
|
264
|
+
"publishedAfter",
|
|
265
|
+
"publishedBefore",
|
|
266
|
+
"safe",
|
|
267
|
+
"device",
|
|
268
|
+
"start",
|
|
269
|
+
"noCache"
|
|
270
|
+
],
|
|
271
|
+
params: (o) => ({
|
|
272
|
+
device: str(o.device),
|
|
273
|
+
gl: googleCountry(o.country),
|
|
274
|
+
hl: str(o.language),
|
|
275
|
+
location: str(o.location),
|
|
276
|
+
safe: str(o.safe),
|
|
277
|
+
start: count(o.start),
|
|
278
|
+
tbs: googleTbs(o)
|
|
279
|
+
}),
|
|
280
|
+
results: webResults
|
|
281
|
+
},
|
|
282
|
+
google_maps: {
|
|
283
|
+
allowed: [
|
|
284
|
+
"language",
|
|
285
|
+
"latitude",
|
|
286
|
+
"longitude",
|
|
287
|
+
"location",
|
|
288
|
+
"zoom",
|
|
289
|
+
"start",
|
|
290
|
+
"noCache"
|
|
291
|
+
],
|
|
292
|
+
params: (o) => ({
|
|
293
|
+
type: "search",
|
|
294
|
+
hl: str(o.language),
|
|
295
|
+
...mapsOrigin(o),
|
|
296
|
+
start: count(o.start)
|
|
297
|
+
}),
|
|
298
|
+
results: placeResults
|
|
299
|
+
},
|
|
300
|
+
google_news: {
|
|
301
|
+
allowed: [
|
|
302
|
+
"country",
|
|
303
|
+
"language",
|
|
304
|
+
"noCache"
|
|
305
|
+
],
|
|
306
|
+
params: (o) => ({
|
|
307
|
+
gl: googleCountry(o.country),
|
|
308
|
+
hl: str(o.language)
|
|
309
|
+
}),
|
|
310
|
+
results: newsResults
|
|
311
|
+
},
|
|
312
|
+
google_patents: {
|
|
313
|
+
allowed: [
|
|
314
|
+
"limit",
|
|
315
|
+
"page",
|
|
316
|
+
"sort",
|
|
317
|
+
"publishedAfter",
|
|
318
|
+
"publishedBefore",
|
|
319
|
+
"inventor",
|
|
320
|
+
"assignee",
|
|
321
|
+
"status",
|
|
322
|
+
"noCache"
|
|
323
|
+
],
|
|
324
|
+
params: (o) => ({
|
|
325
|
+
after: patentDate("publishedAfter", o.publishedAfter),
|
|
326
|
+
assignee: str(o.assignee),
|
|
327
|
+
before: patentDate("publishedBefore", o.publishedBefore),
|
|
328
|
+
inventor: str(o.inventor),
|
|
329
|
+
num: bounded("google_patents", o.limit, 10, 100),
|
|
330
|
+
page: count(o.page),
|
|
331
|
+
sort: str(o.sort),
|
|
332
|
+
status: str(o.status)
|
|
333
|
+
}),
|
|
334
|
+
results: patentResults
|
|
335
|
+
},
|
|
336
|
+
google_scholar: {
|
|
337
|
+
allowed: [
|
|
338
|
+
"limit",
|
|
339
|
+
"language",
|
|
340
|
+
"safe",
|
|
341
|
+
"yearFrom",
|
|
342
|
+
"yearTo",
|
|
343
|
+
"start",
|
|
344
|
+
"noCache"
|
|
345
|
+
],
|
|
346
|
+
params: (o) => ({
|
|
347
|
+
as_yhi: count(o.yearTo),
|
|
348
|
+
as_ylo: count(o.yearFrom),
|
|
349
|
+
hl: str(o.language),
|
|
350
|
+
num: bounded("google_scholar", o.limit, 1, 20),
|
|
351
|
+
safe: str(o.safe),
|
|
352
|
+
start: count(o.start)
|
|
353
|
+
}),
|
|
354
|
+
results: scholarResults
|
|
355
|
+
},
|
|
356
|
+
google_shopping: {
|
|
357
|
+
allowed: [
|
|
358
|
+
"country",
|
|
359
|
+
"language",
|
|
360
|
+
"location",
|
|
361
|
+
"safe",
|
|
362
|
+
"device",
|
|
363
|
+
"minPrice",
|
|
364
|
+
"maxPrice",
|
|
365
|
+
"sortBy",
|
|
366
|
+
"freeShipping",
|
|
367
|
+
"onSale",
|
|
368
|
+
"smallBusiness",
|
|
369
|
+
"noCache"
|
|
370
|
+
],
|
|
371
|
+
params: (o) => ({
|
|
372
|
+
device: str(o.device),
|
|
373
|
+
free_shipping: o.freeShipping === true ? true : void 0,
|
|
374
|
+
gl: googleCountry(o.country),
|
|
375
|
+
hl: str(o.language),
|
|
376
|
+
location: str(o.location),
|
|
377
|
+
max_price: count(o.maxPrice),
|
|
378
|
+
min_price: count(o.minPrice),
|
|
379
|
+
on_sale: o.onSale === true ? true : void 0,
|
|
380
|
+
safe: str(o.safe),
|
|
381
|
+
small_business: o.smallBusiness === true ? true : void 0,
|
|
382
|
+
sort_by: count(o.sortBy)
|
|
383
|
+
}),
|
|
384
|
+
results: shoppingResults
|
|
385
|
+
}
|
|
386
|
+
};
|
|
387
|
+
const IMAGE_OPTIONS = [
|
|
388
|
+
"country",
|
|
389
|
+
"language",
|
|
390
|
+
"location",
|
|
391
|
+
"recency",
|
|
392
|
+
"publishedAfter",
|
|
393
|
+
"publishedBefore",
|
|
394
|
+
"safe",
|
|
395
|
+
"device",
|
|
396
|
+
"page",
|
|
397
|
+
"aspectRatio",
|
|
398
|
+
"size",
|
|
399
|
+
"color",
|
|
400
|
+
"imageType",
|
|
401
|
+
"license",
|
|
402
|
+
"noCache"
|
|
403
|
+
];
|
|
404
|
+
const onlyAllowed = (engine, options, allowed) => {
|
|
405
|
+
const refused = Object.keys(options).filter((name) => options[name] !== void 0 && !allowed.includes(name));
|
|
406
|
+
if (refused.length > 0) throw invalid(`${engine} does not accept ${refused.join(", ")}`);
|
|
407
|
+
};
|
|
408
|
+
// src/adapters/serpapi.ts
|
|
409
|
+
const serpapi = (config = {}) => {
|
|
410
|
+
const key = requireKey(provider, config.apiKey, "SERPAPI_API_KEY");
|
|
411
|
+
const authenticated = (async (input, init) => {
|
|
412
|
+
const url = new URL(input);
|
|
413
|
+
url.searchParams.set("api_key", key);
|
|
414
|
+
return await (config.fetch ?? globalThis.fetch)(url, init);
|
|
415
|
+
});
|
|
416
|
+
const raw = createRawClient({
|
|
417
|
+
baseUrl: config.baseUrl ?? "https://serpapi.com",
|
|
418
|
+
fetch: authenticated,
|
|
419
|
+
headers: {},
|
|
420
|
+
provider
|
|
421
|
+
});
|
|
422
|
+
return defineAdapter({
|
|
423
|
+
name: provider,
|
|
424
|
+
operations: {
|
|
425
|
+
async images(query, options) {
|
|
426
|
+
const { call, rest: o } = callSettings(options);
|
|
427
|
+
onlyAllowed("google_images", o, IMAGE_OPTIONS);
|
|
428
|
+
refuseTogether(provider, o, "recency", "publishedAfter");
|
|
429
|
+
refuseTogether(provider, o, "recency", "publishedBefore");
|
|
430
|
+
const response = record(await raw.request("GET", "/search", {
|
|
431
|
+
...call,
|
|
432
|
+
query: compact({
|
|
433
|
+
device: o.device,
|
|
434
|
+
end_date: compactDate("publishedBefore", o.publishedBefore),
|
|
435
|
+
engine: "google_images",
|
|
436
|
+
gl: googleCountry(o.country),
|
|
437
|
+
hl: o.language,
|
|
438
|
+
ijn: o.page,
|
|
439
|
+
image_color: o.color,
|
|
440
|
+
image_type: o.imageType,
|
|
441
|
+
imgar: o.aspectRatio,
|
|
442
|
+
imgsz: o.size,
|
|
443
|
+
licenses: o.license,
|
|
444
|
+
location: o.location,
|
|
445
|
+
no_cache: o.noCache === true ? true : void 0,
|
|
446
|
+
period_unit: o.recency === void 0 ? void 0 : RECENCY_CODE[o.recency],
|
|
447
|
+
q: query,
|
|
448
|
+
safe: o.safe,
|
|
449
|
+
start_date: compactDate("publishedAfter", o.publishedAfter)
|
|
450
|
+
})
|
|
451
|
+
}));
|
|
452
|
+
failIfError(response);
|
|
453
|
+
const results = list(response.images_results).flatMap((entry) => {
|
|
454
|
+
const item = record(entry);
|
|
455
|
+
const url = text(item.original);
|
|
456
|
+
return url ? [compact({
|
|
457
|
+
height: num(item.original_height),
|
|
458
|
+
sourceUrl: text(item.link),
|
|
459
|
+
thumbnailUrl: text(item.thumbnail),
|
|
460
|
+
title: text(item.title),
|
|
461
|
+
url,
|
|
462
|
+
width: num(item.original_width)
|
|
463
|
+
})] : [];
|
|
464
|
+
});
|
|
465
|
+
return compact({
|
|
466
|
+
meta: searchMeta(response),
|
|
467
|
+
raw: response,
|
|
468
|
+
results
|
|
469
|
+
});
|
|
470
|
+
},
|
|
471
|
+
async search(query, options) {
|
|
472
|
+
const { call, rest } = callSettings(options);
|
|
473
|
+
const { engine = "google", ...chosen } = rest;
|
|
474
|
+
const o = { ...chosen };
|
|
475
|
+
if (!Object.hasOwn(ENGINES, engine)) throw invalid(`does not offer the engine "${engine}"`);
|
|
476
|
+
const spec = ENGINES[engine];
|
|
477
|
+
onlyAllowed(engine, o, spec.allowed);
|
|
478
|
+
const response = record(await raw.request("GET", "/search", {
|
|
479
|
+
...call,
|
|
480
|
+
query: compact({
|
|
481
|
+
engine,
|
|
482
|
+
q: query,
|
|
483
|
+
...spec.params(o),
|
|
484
|
+
no_cache: o.noCache === true ? true : void 0
|
|
485
|
+
})
|
|
486
|
+
}));
|
|
487
|
+
failIfError(response);
|
|
488
|
+
return compact({
|
|
489
|
+
answer: engine === "google" ? answerText(response) : void 0,
|
|
490
|
+
meta: searchMeta(response),
|
|
491
|
+
raw: response,
|
|
492
|
+
results: spec.results(response)
|
|
493
|
+
});
|
|
494
|
+
},
|
|
495
|
+
async usage(options) {
|
|
496
|
+
const { call } = callSettings(options);
|
|
497
|
+
const response = record(await raw.request("GET", "/account", call));
|
|
498
|
+
const safe = Object.fromEntries(Object.entries(response).filter(([name]) => name !== "api_key"));
|
|
499
|
+
const breakdown = {};
|
|
500
|
+
for (const name of ["this_hour_searches", "last_hour_searches"]) {
|
|
501
|
+
const value = num(response[name]);
|
|
502
|
+
if (value !== void 0) breakdown[name] = value;
|
|
503
|
+
}
|
|
504
|
+
return compact({
|
|
505
|
+
breakdown: Object.keys(breakdown).length > 0 ? breakdown : void 0,
|
|
506
|
+
limit: num(response.searches_per_month),
|
|
507
|
+
periodEnd: text(response.plan_renewal_date),
|
|
508
|
+
raw: safe,
|
|
509
|
+
remaining: num(response.plan_searches_left),
|
|
510
|
+
used: num(response.this_month_usage)
|
|
511
|
+
});
|
|
512
|
+
}
|
|
513
|
+
},
|
|
514
|
+
raw
|
|
515
|
+
});
|
|
516
|
+
};
|
|
517
|
+
export { serpapi };
|
package/dist/tavily.d.ts
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { A as JsonSchema, G as Research, I as Page, L as PageBatch, W as Recency, X as SiteMap, a as RawClient, f as CallOptions, g as Crawl, k as ImageSearch, l as Answer, q as SearchResponse, s as AccountUsage, t as Adapter } from "./adapter-DGehHCMo.js";
|
|
2
|
+
interface TavilyConfig {
|
|
3
|
+
/** Defaults to the TAVILY_API_KEY environment variable. */
|
|
4
|
+
apiKey?: string;
|
|
5
|
+
/** Sent as the X-Project-ID header, so Tavily groups usage and logs by project. */
|
|
6
|
+
projectId?: string;
|
|
7
|
+
baseUrl?: string;
|
|
8
|
+
fetch?: typeof fetch;
|
|
9
|
+
}
|
|
10
|
+
type TavilySearchDepth = "ultra-fast" | "fast" | "basic" | "advanced";
|
|
11
|
+
interface TavilySearchOptions extends CallOptions {
|
|
12
|
+
/** Results to return; Tavily allows 0 to 20. */
|
|
13
|
+
limit?: number;
|
|
14
|
+
/**
|
|
15
|
+
* `advanced` costs 2 credits and is the most relevant. `ultra-fast` returns
|
|
16
|
+
* one summary per page instead of several passages.
|
|
17
|
+
*/
|
|
18
|
+
searchDepth?: TavilySearchDepth;
|
|
19
|
+
topic?: "general" | "news" | "finance";
|
|
20
|
+
/** Only results from this far back. Tavily has no `hour` window. */
|
|
21
|
+
recency?: Exclude<Recency, "hour">;
|
|
22
|
+
/** YYYY-MM-DD. */
|
|
23
|
+
publishedAfter?: string;
|
|
24
|
+
/** YYYY-MM-DD. */
|
|
25
|
+
publishedBefore?: string;
|
|
26
|
+
/**
|
|
27
|
+
* Drop results outside the date window, and results with no detectable date.
|
|
28
|
+
* Without it Tavily keeps undated results when a window is set.
|
|
29
|
+
*/
|
|
30
|
+
filterByPublishedDate?: boolean;
|
|
31
|
+
/** Passages to return per page, 1 to 3. Not available with `ultra-fast`. */
|
|
32
|
+
chunksPerSource?: number;
|
|
33
|
+
includeDomains?: string[];
|
|
34
|
+
excludeDomains?: string[];
|
|
35
|
+
/** "restrict" keeps to includeDomains; "prefer" only ranks them higher. */
|
|
36
|
+
includeDomainsMode?: "restrict" | "prefer";
|
|
37
|
+
/** ISO 3166-1 alpha-2 code such as "GB". Ranks that country higher; general topic only. */
|
|
38
|
+
country?: string;
|
|
39
|
+
/** ISO 639-1 code. Ranks pages in that language higher. */
|
|
40
|
+
language?: string;
|
|
41
|
+
/** Remove pages that are not in `language`, instead of only ranking them lower. */
|
|
42
|
+
filterByLanguage?: boolean;
|
|
43
|
+
/** Remove adult and unsafe pages. Not available with `fast` or `ultra-fast`. */
|
|
44
|
+
safeSearch?: boolean;
|
|
45
|
+
/** Let Tavily choose parameters from the query; it may switch to `advanced` (2 credits). */
|
|
46
|
+
autoParameters?: boolean;
|
|
47
|
+
/** Only pages containing the quoted phrases in the query. */
|
|
48
|
+
exactMatch?: boolean;
|
|
49
|
+
/** Seconds a cached copy may be old; 0 always fetches live, a negative number never does. Tavily counts hours, up to 720. */
|
|
50
|
+
maxAge?: number;
|
|
51
|
+
/** Include each result's page text; `true` means Markdown. */
|
|
52
|
+
content?: boolean | "markdown" | "text";
|
|
53
|
+
/** Return images found for the query in `images`. */
|
|
54
|
+
includeImages?: boolean;
|
|
55
|
+
/** Add a description to each image. Needs `includeImages`. */
|
|
56
|
+
imageDescriptions?: boolean;
|
|
57
|
+
/** Return each result's favicon. */
|
|
58
|
+
favicon?: boolean;
|
|
59
|
+
}
|
|
60
|
+
interface TavilyImagesOptions extends CallOptions {
|
|
61
|
+
searchDepth?: TavilySearchDepth;
|
|
62
|
+
topic?: "general" | "news" | "finance";
|
|
63
|
+
includeDomains?: string[];
|
|
64
|
+
excludeDomains?: string[];
|
|
65
|
+
safeSearch?: boolean;
|
|
66
|
+
/** Describe each image. Defaults to true. */
|
|
67
|
+
descriptions?: boolean;
|
|
68
|
+
}
|
|
69
|
+
interface TavilyAnswerOptions extends Omit<TavilySearchOptions, "content" | "includeImages" | "imageDescriptions" | "favicon"> {
|
|
70
|
+
/** `advanced` writes a more detailed answer. Defaults to `basic`. */
|
|
71
|
+
answerDepth?: "basic" | "advanced";
|
|
72
|
+
}
|
|
73
|
+
interface TavilyExtractOptions extends CallOptions {
|
|
74
|
+
/** `advanced` also reads tables and embedded content, and costs 2 credits per 5 pages. */
|
|
75
|
+
extractDepth?: "basic" | "advanced";
|
|
76
|
+
/** Return only the passages that answer this, as `excerpts`, instead of the whole page. */
|
|
77
|
+
query?: string;
|
|
78
|
+
/** Passages to return per page, 1 to 5. Needs `query`. */
|
|
79
|
+
chunksPerSource?: number;
|
|
80
|
+
/** `text` returns plain text, in `markdown`. */
|
|
81
|
+
format?: "markdown" | "text";
|
|
82
|
+
includeImages?: boolean;
|
|
83
|
+
favicon?: boolean;
|
|
84
|
+
/** Seconds Tavily waits for the page, 1 to 60. Not the same as `timeout`, which is milliseconds on our side. */
|
|
85
|
+
serverTimeout?: number;
|
|
86
|
+
}
|
|
87
|
+
type TavilyReadOptions = TavilyExtractOptions;
|
|
88
|
+
type TavilyReadManyOptions = TavilyExtractOptions;
|
|
89
|
+
interface TavilyTraversal {
|
|
90
|
+
/** Plain-words instructions that focus the walk. Raises the cost. */
|
|
91
|
+
instructions?: string;
|
|
92
|
+
maxDepth?: number;
|
|
93
|
+
maxBreadth?: number;
|
|
94
|
+
/** Links to process before stopping. */
|
|
95
|
+
limit?: number;
|
|
96
|
+
/** Regular expressions; a URL's path must match one. */
|
|
97
|
+
selectPathPatterns?: string[];
|
|
98
|
+
/** Regular expressions; a URL whose path matches one is skipped. */
|
|
99
|
+
excludePathPatterns?: string[];
|
|
100
|
+
/** Regular expressions such as `^docs\\.example\\.com$`; a URL's host must match one. */
|
|
101
|
+
selectDomainPatterns?: string[];
|
|
102
|
+
/** Regular expressions; a URL whose host matches one is skipped. */
|
|
103
|
+
excludeDomainPatterns?: string[];
|
|
104
|
+
/** Include links to other domains. Tavily defaults to true. */
|
|
105
|
+
allowExternal?: boolean;
|
|
106
|
+
/** Seconds Tavily works before stopping, 10 to 150. Not the same as `timeout`, which is milliseconds on our side. */
|
|
107
|
+
serverTimeout?: number;
|
|
108
|
+
}
|
|
109
|
+
interface TavilyMapOptions extends CallOptions, TavilyTraversal {}
|
|
110
|
+
interface TavilyCrawlOptions extends CallOptions, TavilyTraversal {
|
|
111
|
+
extractDepth?: "basic" | "advanced";
|
|
112
|
+
format?: "markdown" | "text";
|
|
113
|
+
/** Passages to return per page, 1 to 5. Needs `instructions`. */
|
|
114
|
+
chunksPerSource?: number;
|
|
115
|
+
includeImages?: boolean;
|
|
116
|
+
favicon?: boolean;
|
|
117
|
+
}
|
|
118
|
+
/** A file the research agent reads as a source and cites. */
|
|
119
|
+
interface TavilyResearchFile {
|
|
120
|
+
/** With an extension of `.txt`, `.md` or `.json`. */
|
|
121
|
+
name: string;
|
|
122
|
+
/** The file's text, or its bytes. Sent base64-encoded. */
|
|
123
|
+
data: string | Uint8Array;
|
|
124
|
+
}
|
|
125
|
+
interface TavilyResearchOptions extends CallOptions {
|
|
126
|
+
/** Tavily accepts object, string, integer, number and array types only, and each property needs a description. */
|
|
127
|
+
schema?: JsonSchema;
|
|
128
|
+
model?: "mini" | "pro" | "auto";
|
|
129
|
+
citationFormat?: "numbered" | "mla" | "apa" | "chicago";
|
|
130
|
+
outputLength?: "short" | "standard" | "long";
|
|
131
|
+
/** A preference, not a restriction. Up to 20. */
|
|
132
|
+
includeDomains?: string[];
|
|
133
|
+
/** Never cited, subdomains included. Up to 20. */
|
|
134
|
+
excludeDomains?: string[];
|
|
135
|
+
/** Up to 5 files, 80,000 words in all. */
|
|
136
|
+
files?: TavilyResearchFile[];
|
|
137
|
+
/** Milliseconds between checks. */
|
|
138
|
+
interval?: number;
|
|
139
|
+
/**
|
|
140
|
+
* The id of a job that timed out or was cancelled, from the error's
|
|
141
|
+
* `jobId`. Waits for that job instead of starting a new one.
|
|
142
|
+
*/
|
|
143
|
+
resume?: string;
|
|
144
|
+
}
|
|
145
|
+
interface TavilyOperations {
|
|
146
|
+
search(query: string, options?: TavilySearchOptions): Promise<SearchResponse>;
|
|
147
|
+
images(query: string, options?: TavilyImagesOptions): Promise<ImageSearch>;
|
|
148
|
+
read(url: string, options?: TavilyReadOptions): Promise<Page>;
|
|
149
|
+
readMany(urls: string[], options?: TavilyReadManyOptions): Promise<PageBatch>;
|
|
150
|
+
map(url: string, options?: TavilyMapOptions): Promise<SiteMap>;
|
|
151
|
+
crawl(url: string, options?: TavilyCrawlOptions): Promise<Crawl>;
|
|
152
|
+
answer(question: string, options?: TavilyAnswerOptions): Promise<Answer<unknown>>;
|
|
153
|
+
research<T = unknown>(question: string, options?: TavilyResearchOptions): Promise<Research<T>>;
|
|
154
|
+
usage(options?: CallOptions): Promise<AccountUsage>;
|
|
155
|
+
}
|
|
156
|
+
export declare const tavily: (config?: TavilyConfig) => Adapter<"tavily", TavilyOperations, RawClient>;
|
|
157
|
+
export type { TavilyAnswerOptions, TavilyConfig, TavilyCrawlOptions, TavilyImagesOptions, TavilyMapOptions, TavilyOperations, TavilyReadManyOptions, TavilyReadOptions, TavilyResearchFile, TavilyResearchOptions, TavilySearchDepth, TavilySearchOptions };
|