@howells/fieldwork-sdk 0.3.1 → 0.5.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 +66 -33
- package/dist/{adapter-DGehHCMo.d.ts → adapter-BgdfLBs-.d.ts} +101 -5
- package/dist/adapter-DWYJys20.js +8 -0
- package/dist/brave.d.ts +11 -4
- package/dist/brave.js +230 -127
- package/dist/browserbase.d.ts +15 -9
- package/dist/browserbase.js +97 -49
- package/dist/document-Dpj01mGx.js +51 -0
- package/dist/{errors-BiIBG9zN.js → errors-Yvk42oZH.js} +3 -1
- package/dist/exa.d.ts +28 -19
- package/dist/exa.js +324 -177
- package/dist/{fieldwork-B3x3cxDJ.d.ts → fieldwork-CXNIcqSN.d.ts} +9 -3
- package/dist/firecrawl.d.ts +33 -25
- package/dist/firecrawl.js +688 -364
- package/dist/index.d.ts +5 -3
- package/dist/index.js +33 -13
- package/dist/llamaparse.d.ts +58 -0
- package/dist/llamaparse.js +286 -0
- package/dist/parallel.d.ts +36 -26
- package/dist/parallel.js +440 -230
- package/dist/parse-D4SMt-Ip.js +510 -0
- package/dist/perplexity.d.ts +25 -16
- package/dist/perplexity.js +210 -139
- package/dist/reducto.d.ts +31 -0
- package/dist/reducto.js +220 -0
- package/dist/schema-Cupy7-7q.d.ts +90 -0
- package/dist/schema-DbSIABP0.js +45 -0
- package/dist/serpapi.d.ts +12 -4
- package/dist/serpapi.js +280 -174
- package/dist/tavily.d.ts +20 -12
- package/dist/tavily.js +209 -131
- package/dist/tools.d.ts +5 -2
- package/dist/tools.js +62 -12
- package/dist/util-DhYr1OS3.d.ts +1 -0
- package/package.json +17 -2
- package/dist/adapter-CEAQuT7F.js +0 -3
- package/dist/util-YaZYZDSk.js +0 -287
package/dist/serpapi.js
CHANGED
|
@@ -1,145 +1,138 @@
|
|
|
1
|
-
import { t as defineAdapter } from "./adapter-
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import { t as defineAdapter } from "./adapter-DWYJys20.js";
|
|
2
|
+
import { r as invalidRequest, t as FieldworkError } from "./errors-Yvk42oZH.js";
|
|
3
|
+
import { C as createRawClient, E as requireKey, N as meta, O as callSettings, R as refuseTogether, a as looseObject, f as optionalNumber, h as optionalString, j as compact, m as optionalScalar, o as nonEmptyString, r as arrayOf, x as warnings } from "./parse-D4SMt-Ip.js";
|
|
4
|
+
import * as z from "zod/mini";
|
|
4
5
|
// src/adapters/serpapi/parse.ts
|
|
5
6
|
const provider = "serpapi";
|
|
6
|
-
const invalid = (message) => new FieldworkError("InvalidRequest", `${provider} ${message}`, { provider });
|
|
7
7
|
const priceOf = (item) => {
|
|
8
8
|
const price = compact({
|
|
9
|
-
amount:
|
|
10
|
-
formatted:
|
|
9
|
+
amount: item.extracted_price,
|
|
10
|
+
formatted: item.price
|
|
11
11
|
});
|
|
12
12
|
return Object.keys(price).length > 0 ? price : void 0;
|
|
13
13
|
};
|
|
14
|
-
const webResults = (
|
|
15
|
-
const
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
image: text(item.thumbnail),
|
|
14
|
+
const webResults = (body) => body.organic_results.flatMap((item) => {
|
|
15
|
+
const url = item.link;
|
|
16
|
+
return url === void 0 ? [] : [compact({
|
|
17
|
+
author: item.author,
|
|
18
|
+
favicon: item.favicon,
|
|
19
|
+
image: item.thumbnail,
|
|
21
20
|
kind: "web",
|
|
22
|
-
publishedAt:
|
|
23
|
-
snippet:
|
|
24
|
-
title:
|
|
21
|
+
publishedAt: item.date,
|
|
22
|
+
snippet: item.snippet,
|
|
23
|
+
title: item.title,
|
|
25
24
|
url
|
|
26
|
-
})]
|
|
25
|
+
})];
|
|
26
|
+
});
|
|
27
|
+
const imageResults = (body) => body.images_results.flatMap((item) => {
|
|
28
|
+
const url = item.original;
|
|
29
|
+
return url === void 0 ? [] : [compact({
|
|
30
|
+
height: item.original_height,
|
|
31
|
+
sourceUrl: item.link,
|
|
32
|
+
thumbnailUrl: item.thumbnail,
|
|
33
|
+
title: item.title,
|
|
34
|
+
url,
|
|
35
|
+
width: item.original_width
|
|
36
|
+
})];
|
|
27
37
|
});
|
|
28
|
-
const newsItem = (
|
|
29
|
-
const
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
const source = record(item.source);
|
|
33
|
-
const authors = strings(source.authors);
|
|
38
|
+
const newsItem = (item) => {
|
|
39
|
+
const url = item.link;
|
|
40
|
+
if (url === void 0) return [];
|
|
41
|
+
const { authors } = item.source;
|
|
34
42
|
return [compact({
|
|
35
|
-
author: authors
|
|
36
|
-
favicon:
|
|
37
|
-
image:
|
|
43
|
+
author: authors.length > 0 ? authors.join(", ") : void 0,
|
|
44
|
+
favicon: item.source.icon,
|
|
45
|
+
image: item.thumbnail,
|
|
38
46
|
kind: "news",
|
|
39
|
-
publishedAt:
|
|
40
|
-
title:
|
|
47
|
+
publishedAt: item.iso_date ?? item.date,
|
|
48
|
+
title: item.title,
|
|
41
49
|
url
|
|
42
50
|
})];
|
|
43
51
|
};
|
|
44
|
-
const newsResults = (
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
});
|
|
52
|
-
const shoppingResults = (response) => {
|
|
53
|
-
const categorized = list(response.categorized_shopping_results).flatMap((group) => list(record(group).shopping_results));
|
|
52
|
+
const newsResults = (body) => body.news_results.flatMap((item) => [
|
|
53
|
+
...newsItem(item),
|
|
54
|
+
...newsItem(item.highlight),
|
|
55
|
+
...item.stories.flatMap((story) => newsItem(story))
|
|
56
|
+
]);
|
|
57
|
+
const shoppingResults = (body) => {
|
|
58
|
+
const categorized = body.categorized_shopping_results.flatMap((group) => group.shopping_results);
|
|
54
59
|
return ([
|
|
55
|
-
|
|
60
|
+
body.shopping_results,
|
|
56
61
|
categorized,
|
|
57
|
-
|
|
58
|
-
].find((group) => group.length > 0) ?? []).flatMap((
|
|
59
|
-
const
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
image: text(item.thumbnail),
|
|
62
|
+
body.inline_shopping_results
|
|
63
|
+
].find((group) => group.length > 0) ?? []).flatMap((item) => {
|
|
64
|
+
const url = item.link ?? item.product_link;
|
|
65
|
+
return url === void 0 ? [] : [compact({
|
|
66
|
+
image: item.thumbnail,
|
|
63
67
|
kind: "shopping",
|
|
64
68
|
price: priceOf(item),
|
|
65
|
-
rating:
|
|
66
|
-
reviewCount:
|
|
67
|
-
snippet:
|
|
68
|
-
title:
|
|
69
|
+
rating: item.rating,
|
|
70
|
+
reviewCount: item.reviews,
|
|
71
|
+
snippet: item.snippet,
|
|
72
|
+
title: item.title,
|
|
69
73
|
url
|
|
70
|
-
})]
|
|
74
|
+
})];
|
|
71
75
|
});
|
|
72
76
|
};
|
|
73
77
|
const mapsUrl = (item) => {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
const
|
|
77
|
-
|
|
78
|
-
return id && title ? `https://www.google.com/maps/search/?api=1&query=${encodeURIComponent(title)}&query_place_id=${encodeURIComponent(id)}` : void 0;
|
|
78
|
+
if (item.website !== void 0) return item.website;
|
|
79
|
+
const id = item.place_id;
|
|
80
|
+
const { title } = item;
|
|
81
|
+
return id === void 0 || title === void 0 ? void 0 : `https://www.google.com/maps/search/?api=1&query=${encodeURIComponent(title)}&query_place_id=${encodeURIComponent(id)}`;
|
|
79
82
|
};
|
|
80
|
-
const placeResults = (
|
|
81
|
-
const single =
|
|
82
|
-
const places =
|
|
83
|
-
return (places.length === 0 && Object.keys(single).length > 0 ? [single] : places).flatMap((
|
|
84
|
-
const item = record(entry);
|
|
83
|
+
const placeResults = (body) => {
|
|
84
|
+
const single = body.place_results;
|
|
85
|
+
const places = body.local_results;
|
|
86
|
+
return (places.length === 0 && Object.keys(single).length > 0 ? [single] : places).flatMap((item) => {
|
|
85
87
|
const url = mapsUrl(item);
|
|
86
|
-
if (
|
|
87
|
-
const coordinates =
|
|
88
|
+
if (url === void 0) return [];
|
|
89
|
+
const coordinates = item.gps_coordinates;
|
|
88
90
|
const place = compact({
|
|
89
|
-
address:
|
|
90
|
-
latitude:
|
|
91
|
-
longitude:
|
|
91
|
+
address: item.address,
|
|
92
|
+
latitude: coordinates.latitude,
|
|
93
|
+
longitude: coordinates.longitude
|
|
92
94
|
});
|
|
93
95
|
return [compact({
|
|
94
|
-
image:
|
|
96
|
+
image: item.thumbnail,
|
|
95
97
|
kind: "place",
|
|
96
98
|
place: Object.keys(place).length > 0 ? place : void 0,
|
|
97
|
-
rating:
|
|
98
|
-
reviewCount:
|
|
99
|
-
snippet:
|
|
100
|
-
title:
|
|
99
|
+
rating: item.rating,
|
|
100
|
+
reviewCount: item.reviews,
|
|
101
|
+
snippet: item.description,
|
|
102
|
+
title: item.title,
|
|
101
103
|
url
|
|
102
104
|
})];
|
|
103
105
|
});
|
|
104
106
|
};
|
|
105
|
-
const scholarResults = (
|
|
106
|
-
const
|
|
107
|
-
|
|
108
|
-
return url ? [compact({
|
|
107
|
+
const scholarResults = (body) => body.organic_results.flatMap((item) => {
|
|
108
|
+
const url = item.link ?? item.resources[0]?.link;
|
|
109
|
+
return url === void 0 ? [] : [compact({
|
|
109
110
|
kind: "scholar",
|
|
110
|
-
snippet:
|
|
111
|
-
title:
|
|
111
|
+
snippet: item.snippet,
|
|
112
|
+
title: item.title,
|
|
112
113
|
url
|
|
113
|
-
})]
|
|
114
|
+
})];
|
|
114
115
|
});
|
|
115
|
-
const patentResults = (
|
|
116
|
-
const
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
image: text(item.thumbnail),
|
|
116
|
+
const patentResults = (body) => body.organic_results.flatMap((item) => {
|
|
117
|
+
const url = item.patent_link;
|
|
118
|
+
return url === void 0 ? [] : [compact({
|
|
119
|
+
author: item.inventor,
|
|
120
|
+
image: item.thumbnail,
|
|
121
121
|
kind: "patent",
|
|
122
|
-
publishedAt:
|
|
123
|
-
snippet:
|
|
124
|
-
title:
|
|
122
|
+
publishedAt: item.publication_date,
|
|
123
|
+
snippet: item.snippet,
|
|
124
|
+
title: item.title,
|
|
125
125
|
url
|
|
126
|
-
})]
|
|
126
|
+
})];
|
|
127
127
|
});
|
|
128
|
-
const
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
requestId: record(response.search_metadata).id,
|
|
134
|
-
warnings: response.error
|
|
128
|
+
const answerOf = (box) => box?.answer ?? (box?.result === "" ? void 0 : box?.result) ?? box?.snippet;
|
|
129
|
+
const answerText = (body) => answerOf(body.answer_box ?? body.answer_box_list[0]);
|
|
130
|
+
const searchMeta = (body) => meta({
|
|
131
|
+
requestId: body.search_metadata.id,
|
|
132
|
+
warnings: body.error.warnings
|
|
135
133
|
});
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
cause: response,
|
|
139
|
-
provider,
|
|
140
|
-
retryable: true
|
|
141
|
-
});
|
|
142
|
-
};
|
|
134
|
+
/** The reason a search failed, or undefined when it did not. */
|
|
135
|
+
const searchFailure = (body) => body.search_metadata.status === "Error" ? `serpapi search failed: ${body.error.message ?? "no reason given"}` : void 0;
|
|
143
136
|
// src/adapters/serpapi/options.ts
|
|
144
137
|
const RECENCY_CODE = {
|
|
145
138
|
day: "d",
|
|
@@ -156,7 +149,7 @@ const googleCountry = (value) => {
|
|
|
156
149
|
};
|
|
157
150
|
const isoDate = (name, value) => {
|
|
158
151
|
if (value === void 0) return;
|
|
159
|
-
if (typeof value !== "string" || !/^\d{4}-\d{2}-\d{2}$/u.test(value)) throw
|
|
152
|
+
if (typeof value !== "string" || !/^\d{4}-\d{2}-\d{2}$/u.test(value)) throw invalidRequest(provider, `${name} must be YYYY-MM-DD`);
|
|
160
153
|
return value;
|
|
161
154
|
};
|
|
162
155
|
const compactDate = (name, value) => isoDate(name, value)?.replaceAll("-", "");
|
|
@@ -166,13 +159,15 @@ const usDate = (name, value) => {
|
|
|
166
159
|
};
|
|
167
160
|
const bounded = (engine, value, min, max) => {
|
|
168
161
|
if (value === void 0) return;
|
|
169
|
-
if (typeof value !== "number" || !Number.isInteger(value) || value < min || value > max) throw
|
|
162
|
+
if (typeof value !== "number" || !Number.isInteger(value) || value < min || value > max) throw invalidRequest(provider, `${engine} limit must be a whole number from ${min} to ${max}`);
|
|
170
163
|
return value;
|
|
171
164
|
};
|
|
165
|
+
/** The code for the recency window the caller set; undefined when it names none. */
|
|
166
|
+
const recencyCode = (o) => Object.entries(RECENCY_CODE).find(([name]) => name === o.recency)?.[1];
|
|
172
167
|
const googleTbs = (o) => {
|
|
173
168
|
refuseTogether(provider, o, "recency", "publishedAfter");
|
|
174
169
|
refuseTogether(provider, o, "recency", "publishedBefore");
|
|
175
|
-
if (o.recency !== void 0) return `qdr:${
|
|
170
|
+
if (o.recency !== void 0) return `qdr:${recencyCode(o)}`;
|
|
176
171
|
const after = usDate("publishedAfter", o.publishedAfter);
|
|
177
172
|
const before = usDate("publishedBefore", o.publishedBefore);
|
|
178
173
|
if (after === void 0 && before === void 0) return;
|
|
@@ -185,10 +180,10 @@ const googleTbs = (o) => {
|
|
|
185
180
|
const duckDuckGoFilter = (o) => {
|
|
186
181
|
refuseTogether(provider, o, "recency", "publishedAfter");
|
|
187
182
|
refuseTogether(provider, o, "recency", "publishedBefore");
|
|
188
|
-
if (o.recency === "hour") throw
|
|
189
|
-
if (o.recency !== void 0) return
|
|
183
|
+
if (o.recency === "hour") throw invalidRequest(provider, "duckduckgo has no hour window; use day, week, month or year");
|
|
184
|
+
if (o.recency !== void 0) return recencyCode(o);
|
|
190
185
|
if (o.publishedAfter === void 0 && o.publishedBefore === void 0) return;
|
|
191
|
-
if (o.publishedAfter === void 0 || o.publishedBefore === void 0) throw
|
|
186
|
+
if (o.publishedAfter === void 0 || o.publishedBefore === void 0) throw invalidRequest(provider, "duckduckgo filters a date range by both ends: pass publishedAfter and publishedBefore together");
|
|
192
187
|
return `${isoDate("publishedAfter", o.publishedAfter)}..${isoDate("publishedBefore", o.publishedBefore)}`;
|
|
193
188
|
};
|
|
194
189
|
const patentDate = (name, value) => {
|
|
@@ -199,10 +194,10 @@ const mapsOrigin = (o) => {
|
|
|
199
194
|
refuseTogether(provider, o, "location", "latitude");
|
|
200
195
|
refuseTogether(provider, o, "location", "longitude");
|
|
201
196
|
const coordinates = o.latitude !== void 0 || o.longitude !== void 0;
|
|
202
|
-
if (coordinates && (o.latitude === void 0 || o.longitude === void 0)) throw
|
|
197
|
+
if (coordinates && (o.latitude === void 0 || o.longitude === void 0)) throw invalidRequest(provider, "google_maps needs latitude and longitude together");
|
|
203
198
|
const origin = coordinates || o.location !== void 0;
|
|
204
|
-
if (origin && o.zoom === void 0) throw
|
|
205
|
-
if (!origin && o.zoom !== void 0) throw
|
|
199
|
+
if (origin && o.zoom === void 0) throw invalidRequest(provider, "google_maps needs zoom with location or latitude and longitude");
|
|
200
|
+
if (!origin && o.zoom !== void 0) throw invalidRequest(provider, "google_maps zoom needs location or latitude and longitude");
|
|
206
201
|
return {
|
|
207
202
|
lat: count(o.latitude),
|
|
208
203
|
location: str(o.location),
|
|
@@ -403,22 +398,153 @@ const IMAGE_OPTIONS = [
|
|
|
403
398
|
];
|
|
404
399
|
const onlyAllowed = (engine, options, allowed) => {
|
|
405
400
|
const refused = Object.keys(options).filter((name) => options[name] !== void 0 && !allowed.includes(name));
|
|
406
|
-
if (refused.length > 0) throw
|
|
401
|
+
if (refused.length > 0) throw invalidRequest(provider, `${engine} does not accept ${refused.join(", ")}`);
|
|
402
|
+
};
|
|
403
|
+
// src/adapters/serpapi/response.ts
|
|
404
|
+
/** SerpApi's `error` note: its text when it is a non-empty string, and the warnings it amounts to. */
|
|
405
|
+
const ErrorNote = z.union([z.pipe(nonEmptyString, z.transform((message) => ({
|
|
406
|
+
message,
|
|
407
|
+
warnings: [message]
|
|
408
|
+
}))), z.pipe(warnings, z.transform((entries) => ({
|
|
409
|
+
message: void 0,
|
|
410
|
+
warnings: entries
|
|
411
|
+
})))]);
|
|
412
|
+
/** An organic result: a web result, a scholar citation or a patent. */
|
|
413
|
+
const Organic = looseObject({
|
|
414
|
+
author: optionalString,
|
|
415
|
+
date: optionalString,
|
|
416
|
+
favicon: optionalString,
|
|
417
|
+
inventor: optionalString,
|
|
418
|
+
link: optionalString,
|
|
419
|
+
patent_link: optionalString,
|
|
420
|
+
publication_date: optionalString,
|
|
421
|
+
resources: arrayOf(looseObject({ link: optionalString })),
|
|
422
|
+
snippet: optionalString,
|
|
423
|
+
thumbnail: optionalString,
|
|
424
|
+
title: optionalString
|
|
425
|
+
});
|
|
426
|
+
const storyShape = {
|
|
427
|
+
date: optionalString,
|
|
428
|
+
iso_date: optionalString,
|
|
429
|
+
link: optionalString,
|
|
430
|
+
source: looseObject({
|
|
431
|
+
authors: arrayOf(z.string()),
|
|
432
|
+
icon: optionalString
|
|
433
|
+
}),
|
|
434
|
+
thumbnail: optionalString,
|
|
435
|
+
title: optionalString
|
|
407
436
|
};
|
|
437
|
+
const Story = looseObject(storyShape);
|
|
438
|
+
/** A news result: a story, or a group with a `highlight` and `stories`. */
|
|
439
|
+
const NewsResult = looseObject({
|
|
440
|
+
...storyShape,
|
|
441
|
+
highlight: Story,
|
|
442
|
+
stories: arrayOf(Story)
|
|
443
|
+
});
|
|
444
|
+
const ShoppingItem = looseObject({
|
|
445
|
+
extracted_price: optionalScalar,
|
|
446
|
+
link: optionalString,
|
|
447
|
+
price: optionalString,
|
|
448
|
+
product_link: optionalString,
|
|
449
|
+
rating: optionalNumber,
|
|
450
|
+
reviews: optionalNumber,
|
|
451
|
+
snippet: optionalString,
|
|
452
|
+
thumbnail: optionalString,
|
|
453
|
+
title: optionalString
|
|
454
|
+
});
|
|
455
|
+
const Place = looseObject({
|
|
456
|
+
address: optionalString,
|
|
457
|
+
description: optionalString,
|
|
458
|
+
gps_coordinates: looseObject({
|
|
459
|
+
latitude: optionalNumber,
|
|
460
|
+
longitude: optionalNumber
|
|
461
|
+
}),
|
|
462
|
+
place_id: optionalString,
|
|
463
|
+
rating: optionalNumber,
|
|
464
|
+
reviews: optionalNumber,
|
|
465
|
+
thumbnail: optionalString,
|
|
466
|
+
title: optionalString,
|
|
467
|
+
website: optionalString
|
|
468
|
+
});
|
|
469
|
+
/** An answer box. An absent or null one reads as undefined; any other value that is not an object has every field absent. */
|
|
470
|
+
const AnswerBox = looseObject({
|
|
471
|
+
answer: optionalString,
|
|
472
|
+
result: optionalScalar,
|
|
473
|
+
snippet: optionalString
|
|
474
|
+
});
|
|
475
|
+
const ImageItem = looseObject({
|
|
476
|
+
link: optionalString,
|
|
477
|
+
original: optionalString,
|
|
478
|
+
original_height: optionalNumber,
|
|
479
|
+
original_width: optionalNumber,
|
|
480
|
+
thumbnail: optionalString,
|
|
481
|
+
title: optionalString
|
|
482
|
+
});
|
|
483
|
+
/** A `/search` response for any engine; each engine reads only its own sections. */
|
|
484
|
+
const SearchResponse = looseObject({
|
|
485
|
+
answer_box: z.union([z.pipe(z.optional(z.null()), z.transform(() => void 0)), AnswerBox]),
|
|
486
|
+
answer_box_list: arrayOf(AnswerBox),
|
|
487
|
+
categorized_shopping_results: arrayOf(looseObject({ shopping_results: arrayOf(ShoppingItem) })),
|
|
488
|
+
error: ErrorNote,
|
|
489
|
+
images_results: arrayOf(ImageItem),
|
|
490
|
+
inline_shopping_results: arrayOf(ShoppingItem),
|
|
491
|
+
local_results: arrayOf(Place),
|
|
492
|
+
news_results: arrayOf(NewsResult),
|
|
493
|
+
organic_results: arrayOf(Organic),
|
|
494
|
+
place_results: Place,
|
|
495
|
+
search_metadata: looseObject({
|
|
496
|
+
id: optionalScalar,
|
|
497
|
+
status: optionalString
|
|
498
|
+
}),
|
|
499
|
+
shopping_results: arrayOf(ShoppingItem)
|
|
500
|
+
});
|
|
501
|
+
/** `/account`: the plan and its usage. */
|
|
502
|
+
const AccountResponse = looseObject({
|
|
503
|
+
last_hour_searches: optionalNumber,
|
|
504
|
+
plan_renewal_date: optionalString,
|
|
505
|
+
plan_searches_left: optionalNumber,
|
|
506
|
+
searches_per_month: optionalNumber,
|
|
507
|
+
this_hour_searches: optionalNumber,
|
|
508
|
+
this_month_usage: optionalNumber
|
|
509
|
+
});
|
|
408
510
|
// src/adapters/serpapi.ts
|
|
511
|
+
/**
|
|
512
|
+
* An adapter for SerpApi: search, images and usage. Pass it to `new Fieldwork({ adapter })`.
|
|
513
|
+
*
|
|
514
|
+
* Throws `Unauthorized` when neither `apiKey` nor `SERPAPI_API_KEY` is set.
|
|
515
|
+
*/
|
|
409
516
|
const serpapi = (config = {}) => {
|
|
410
517
|
const key = requireKey(provider, config.apiKey, "SERPAPI_API_KEY");
|
|
411
|
-
const authenticated =
|
|
412
|
-
const url = new URL(input);
|
|
518
|
+
const authenticated = async (input, init) => {
|
|
519
|
+
const url = new URL(input instanceof Request ? input.url : input);
|
|
413
520
|
url.searchParams.set("api_key", key);
|
|
414
521
|
return await (config.fetch ?? globalThis.fetch)(url, init);
|
|
415
|
-
}
|
|
522
|
+
};
|
|
416
523
|
const raw = createRawClient({
|
|
417
524
|
baseUrl: config.baseUrl ?? "https://serpapi.com",
|
|
418
525
|
fetch: authenticated,
|
|
419
526
|
headers: {},
|
|
420
|
-
provider
|
|
527
|
+
provider,
|
|
528
|
+
retries: config.retries
|
|
421
529
|
});
|
|
530
|
+
/** One `/search` call: the response as received, and its parsed body. A failed search throws. */
|
|
531
|
+
const searchCall = async (call, query) => {
|
|
532
|
+
const response = await raw.request("GET", "/search", {
|
|
533
|
+
...call,
|
|
534
|
+
query
|
|
535
|
+
});
|
|
536
|
+
const body = z.parse(SearchResponse, response);
|
|
537
|
+
const failure = searchFailure(body);
|
|
538
|
+
if (failure !== void 0) throw new FieldworkError("ProviderError", failure, {
|
|
539
|
+
cause: response,
|
|
540
|
+
provider,
|
|
541
|
+
retryable: true
|
|
542
|
+
});
|
|
543
|
+
return {
|
|
544
|
+
body,
|
|
545
|
+
response
|
|
546
|
+
};
|
|
547
|
+
};
|
|
422
548
|
return defineAdapter({
|
|
423
549
|
name: provider,
|
|
424
550
|
operations: {
|
|
@@ -427,87 +553,67 @@ const serpapi = (config = {}) => {
|
|
|
427
553
|
onlyAllowed("google_images", o, IMAGE_OPTIONS);
|
|
428
554
|
refuseTogether(provider, o, "recency", "publishedAfter");
|
|
429
555
|
refuseTogether(provider, o, "recency", "publishedBefore");
|
|
430
|
-
const response =
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
safe: o.safe,
|
|
449
|
-
start_date: compactDate("publishedAfter", o.publishedAfter)
|
|
450
|
-
})
|
|
556
|
+
const { body, response } = await searchCall(call, compact({
|
|
557
|
+
device: o.device,
|
|
558
|
+
end_date: compactDate("publishedBefore", o.publishedBefore),
|
|
559
|
+
engine: "google_images",
|
|
560
|
+
gl: googleCountry(o.country),
|
|
561
|
+
hl: o.language,
|
|
562
|
+
ijn: o.page,
|
|
563
|
+
image_color: o.color,
|
|
564
|
+
image_type: o.imageType,
|
|
565
|
+
imgar: o.aspectRatio,
|
|
566
|
+
imgsz: o.size,
|
|
567
|
+
licenses: o.license,
|
|
568
|
+
location: o.location,
|
|
569
|
+
no_cache: o.noCache === true ? true : void 0,
|
|
570
|
+
period_unit: o.recency === void 0 ? void 0 : RECENCY_CODE[o.recency],
|
|
571
|
+
q: query,
|
|
572
|
+
safe: o.safe,
|
|
573
|
+
start_date: compactDate("publishedAfter", o.publishedAfter)
|
|
451
574
|
}));
|
|
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
575
|
return compact({
|
|
466
|
-
meta: searchMeta(
|
|
576
|
+
meta: searchMeta(body),
|
|
467
577
|
raw: response,
|
|
468
|
-
results
|
|
578
|
+
results: imageResults(body)
|
|
469
579
|
});
|
|
470
580
|
},
|
|
471
581
|
async search(query, options) {
|
|
472
582
|
const { call, rest } = callSettings(options);
|
|
473
583
|
const { engine = "google", ...chosen } = rest;
|
|
474
584
|
const o = { ...chosen };
|
|
475
|
-
if (!Object.hasOwn(ENGINES, engine)) throw
|
|
585
|
+
if (!Object.hasOwn(ENGINES, engine)) throw invalidRequest(provider, `does not offer the engine "${engine}"`);
|
|
476
586
|
const spec = ENGINES[engine];
|
|
477
587
|
onlyAllowed(engine, o, spec.allowed);
|
|
478
|
-
const response =
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
...spec.params(o),
|
|
484
|
-
no_cache: o.noCache === true ? true : void 0
|
|
485
|
-
})
|
|
588
|
+
const { body, response } = await searchCall(call, compact({
|
|
589
|
+
engine,
|
|
590
|
+
q: query,
|
|
591
|
+
...spec.params(o),
|
|
592
|
+
no_cache: o.noCache === true ? true : void 0
|
|
486
593
|
}));
|
|
487
|
-
failIfError(response);
|
|
488
594
|
return compact({
|
|
489
|
-
answer: engine === "google" ? answerText(
|
|
490
|
-
meta: searchMeta(
|
|
595
|
+
answer: engine === "google" ? answerText(body) : void 0,
|
|
596
|
+
meta: searchMeta(body),
|
|
491
597
|
raw: response,
|
|
492
|
-
results: spec.results(
|
|
598
|
+
results: spec.results(body)
|
|
493
599
|
});
|
|
494
600
|
},
|
|
495
601
|
async usage(options) {
|
|
496
602
|
const { call } = callSettings(options);
|
|
497
|
-
const response =
|
|
498
|
-
const
|
|
603
|
+
const response = await raw.request("GET", "/account", call);
|
|
604
|
+
const body = z.parse(AccountResponse, response);
|
|
605
|
+
const fields = z.safeParse(z.record(z.string(), z.unknown()), response);
|
|
606
|
+
const safe = fields.success ? Object.fromEntries(Object.entries(fields.data).filter(([name]) => name !== "api_key")) : response;
|
|
499
607
|
const breakdown = {};
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
if (value !== void 0) breakdown[name] = value;
|
|
503
|
-
}
|
|
608
|
+
if (body.this_hour_searches !== void 0) breakdown.this_hour_searches = body.this_hour_searches;
|
|
609
|
+
if (body.last_hour_searches !== void 0) breakdown.last_hour_searches = body.last_hour_searches;
|
|
504
610
|
return compact({
|
|
505
611
|
breakdown: Object.keys(breakdown).length > 0 ? breakdown : void 0,
|
|
506
|
-
limit:
|
|
507
|
-
periodEnd:
|
|
612
|
+
limit: body.searches_per_month,
|
|
613
|
+
periodEnd: body.plan_renewal_date,
|
|
508
614
|
raw: safe,
|
|
509
|
-
remaining:
|
|
510
|
-
used:
|
|
615
|
+
remaining: body.plan_searches_left,
|
|
616
|
+
used: body.this_month_usage
|
|
511
617
|
});
|
|
512
618
|
}
|
|
513
619
|
},
|
package/dist/tavily.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { K as Recency, Q as SiteMap, R as Page, Y as SearchResponse, _ as Crawl, a as RawClient, j as ImageSearch, l as Answer, p as CallOptions, q as Research, s as AccountUsage, t as Adapter, z as PageBatch } from "./adapter-BgdfLBs-.js";
|
|
2
|
+
import { t as OutputSchema } from "./schema-Cupy7-7q.js";
|
|
2
3
|
interface TavilyConfig {
|
|
3
4
|
/** Defaults to the TAVILY_API_KEY environment variable. */
|
|
4
5
|
apiKey?: string;
|
|
@@ -6,6 +7,8 @@ interface TavilyConfig {
|
|
|
6
7
|
projectId?: string;
|
|
7
8
|
baseUrl?: string;
|
|
8
9
|
fetch?: typeof fetch;
|
|
10
|
+
/** Repeats of a request that can succeed on repeat; 2 unless set. `0` turns retrying off. */
|
|
11
|
+
retries?: number;
|
|
9
12
|
}
|
|
10
13
|
type TavilySearchDepth = "ultra-fast" | "fast" | "basic" | "advanced";
|
|
11
14
|
interface TavilySearchOptions extends CallOptions {
|
|
@@ -122,9 +125,9 @@ interface TavilyResearchFile {
|
|
|
122
125
|
/** The file's text, or its bytes. Sent base64-encoded. */
|
|
123
126
|
data: string | Uint8Array;
|
|
124
127
|
}
|
|
125
|
-
interface TavilyResearchOptions extends CallOptions {
|
|
128
|
+
interface TavilyResearchOptions<T = unknown> extends CallOptions {
|
|
126
129
|
/** Tavily accepts object, string, integer, number and array types only, and each property needs a description. */
|
|
127
|
-
schema?:
|
|
130
|
+
schema?: OutputSchema<T>;
|
|
128
131
|
model?: "mini" | "pro" | "auto";
|
|
129
132
|
citationFormat?: "numbered" | "mla" | "apa" | "chicago";
|
|
130
133
|
outputLength?: "short" | "standard" | "long";
|
|
@@ -143,15 +146,20 @@ interface TavilyResearchOptions extends CallOptions {
|
|
|
143
146
|
resume?: string;
|
|
144
147
|
}
|
|
145
148
|
interface TavilyOperations {
|
|
146
|
-
search(query: string, options?: TavilySearchOptions)
|
|
147
|
-
images(query: string, options?: TavilyImagesOptions)
|
|
148
|
-
read(url: string, options?: TavilyReadOptions)
|
|
149
|
-
readMany(urls: string[], options?: TavilyReadManyOptions)
|
|
150
|
-
map(url: string, options?: TavilyMapOptions)
|
|
151
|
-
crawl(url: string, options?: TavilyCrawlOptions)
|
|
152
|
-
answer(question: string, options?: TavilyAnswerOptions)
|
|
153
|
-
research<T = unknown>(question: string, options?: TavilyResearchOptions)
|
|
154
|
-
usage(options?: CallOptions)
|
|
149
|
+
search: (query: string, options?: TavilySearchOptions) => Promise<SearchResponse>;
|
|
150
|
+
images: (query: string, options?: TavilyImagesOptions) => Promise<ImageSearch>;
|
|
151
|
+
read: (url: string, options?: TavilyReadOptions) => Promise<Page>;
|
|
152
|
+
readMany: (urls: string[], options?: TavilyReadManyOptions) => Promise<PageBatch>;
|
|
153
|
+
map: (url: string, options?: TavilyMapOptions) => Promise<SiteMap>;
|
|
154
|
+
crawl: (url: string, options?: TavilyCrawlOptions) => Promise<Crawl>;
|
|
155
|
+
answer: (question: string, options?: TavilyAnswerOptions) => Promise<Answer<unknown>>;
|
|
156
|
+
research: <T = unknown>(question: string, options?: TavilyResearchOptions<T>) => Promise<Research<T>>;
|
|
157
|
+
usage: (options?: CallOptions) => Promise<AccountUsage>;
|
|
155
158
|
}
|
|
159
|
+
/**
|
|
160
|
+
* An adapter for Tavily: search, images, read, readMany, map, crawl, answer, research and usage. Pass it to `new Fieldwork({ adapter })`.
|
|
161
|
+
*
|
|
162
|
+
* Throws `Unauthorized` when neither `apiKey` nor `TAVILY_API_KEY` is set.
|
|
163
|
+
*/
|
|
156
164
|
export declare const tavily: (config?: TavilyConfig) => Adapter<"tavily", TavilyOperations, RawClient>;
|
|
157
165
|
export type { TavilyAnswerOptions, TavilyConfig, TavilyCrawlOptions, TavilyImagesOptions, TavilyMapOptions, TavilyOperations, TavilyReadManyOptions, TavilyReadOptions, TavilyResearchFile, TavilyResearchOptions, TavilySearchDepth, TavilySearchOptions };
|