@howells/fieldwork-sdk 0.11.0 → 0.12.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 +72 -15
- package/dist/{adapter-DWYJys20.js → adapter-CBSAfErv.js} +1 -1
- package/dist/adapter-J2yatZJ0.d.ts +73 -0
- package/dist/best-CO9liYDa.js +1436 -0
- package/dist/brave.d.ts +7 -6
- package/dist/brave.js +180 -173
- package/dist/browserbase.d.ts +4 -3
- package/dist/browserbase.js +185 -170
- package/dist/{document-DMIUCoDA.js → document-BSCwJgBM.js} +4 -4
- package/dist/{download-CP8noBdX.js → download-kuTqNnuZ.js} +1 -1
- package/dist/{adapter-B8kAAAgF.d.ts → errors-gMtejKHc.d.ts} +87 -69
- package/dist/exa.d.ts +18 -11
- package/dist/exa.js +306 -275
- package/dist/fieldwork-D7npPoUf.d.ts +35 -0
- package/dist/firecrawl.d.ts +4 -3
- package/dist/firecrawl.js +450 -417
- package/dist/{parse-Q7H4mrnO.js → http-CJcvTPnE.js} +223 -225
- package/dist/image.d.ts +253 -0
- package/dist/image.js +1 -0
- package/dist/index.d.ts +6 -7
- package/dist/index.js +66 -82
- package/dist/{job-DnavRq_s.js → job-JLEpszjr.js} +9 -9
- package/dist/kernel.d.ts +20 -2
- package/dist/kernel.js +65 -19
- package/dist/llamaparse.d.ts +3 -2
- package/dist/llamaparse.js +16 -16
- package/dist/parallel.d.ts +8 -8
- package/dist/parallel.js +423 -389
- package/dist/path-DO2llIdp.js +10 -0
- package/dist/perplexity.d.ts +6 -5
- package/dist/perplexity.js +21 -21
- package/dist/reducto.d.ts +3 -2
- package/dist/reducto.js +14 -14
- package/dist/{schema-CAPSX7yQ.d.ts → schema-C9xzRkKJ.d.ts} +17 -6
- package/dist/{schema--iU4AXSM.js → schema-CJXFAZ-x.js} +12 -3
- package/dist/serpapi.d.ts +3 -3
- package/dist/serpapi.js +176 -171
- package/dist/tavily.d.ts +4 -3
- package/dist/tavily.js +247 -234
- package/dist/tools.d.ts +76 -15
- package/dist/tools.js +351 -33
- package/dist/{util-DFUXQX6v.js → util-LvME_uOj.js} +35 -11
- package/package.json +6 -1
- package/dist/fieldwork-BrfoehvG.d.ts +0 -113
- package/dist/path-DL5P8XH2.js +0 -17
- package/dist/util-DhYr1OS3.d.ts +0 -1
- package/dist/{errors-DkFJysIM.js → errors-gpxg6Tnh.js} +5 -5
package/dist/serpapi.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { t as defineAdapter } from "./adapter-
|
|
2
|
-
import { r as invalidRequest, t as FieldworkError } from "./errors-
|
|
3
|
-
import {
|
|
4
|
-
import { a as compact,
|
|
5
|
-
import * as z from "zod/mini";
|
|
1
|
+
import { t as defineAdapter } from "./adapter-CBSAfErv.js";
|
|
2
|
+
import { r as invalidRequest, t as FieldworkError } from "./errors-gpxg6Tnh.js";
|
|
3
|
+
import { D as warnings, b as optionalScalar, d as looseObject, f as nonEmptyString, l as arrayOf, r as requireKey, t as createRawClient, v as optionalNumber, x as optionalString } from "./http-CJcvTPnE.js";
|
|
4
|
+
import { a as compact, d as refuseTogether, s as meta, t as callSettings } from "./util-LvME_uOj.js";
|
|
5
|
+
import * as z$1 from "zod/mini";
|
|
6
6
|
// src/adapters/serpapi/parse.ts
|
|
7
7
|
/** The name this adapter reports in errors and results. */
|
|
8
8
|
const provider = "serpapi";
|
|
@@ -262,16 +262,12 @@ const RECENCY_CODE = {
|
|
|
262
262
|
week: "w",
|
|
263
263
|
year: "y"
|
|
264
264
|
};
|
|
265
|
-
/** The value if it is a string, else undefined. Engine specs read options as `Json`. */
|
|
266
|
-
const str = (value) => typeof value === "string" ? value : void 0;
|
|
267
|
-
/** The value if it is a number, else undefined. */
|
|
268
|
-
const count = (value) => typeof value === "number" ? value : void 0;
|
|
269
265
|
/**
|
|
270
266
|
* Google's `gl` code for a country: lower case, and "uk" for "gb", as SerpApi
|
|
271
267
|
* documents.
|
|
272
268
|
*/
|
|
273
269
|
const googleCountry = (value) => {
|
|
274
|
-
const code =
|
|
270
|
+
const code = value?.toLowerCase();
|
|
275
271
|
return code === "gb" ? "uk" : code;
|
|
276
272
|
};
|
|
277
273
|
/**
|
|
@@ -280,7 +276,7 @@ const googleCountry = (value) => {
|
|
|
280
276
|
*/
|
|
281
277
|
const isoDate = (name, value) => {
|
|
282
278
|
if (value === void 0) return;
|
|
283
|
-
if (
|
|
279
|
+
if (!/^\d{4}-\d{2}-\d{2}$/u.test(value)) throw invalidRequest(provider, `${name} must be YYYY-MM-DD`);
|
|
284
280
|
return value;
|
|
285
281
|
};
|
|
286
282
|
/** A YYYY-MM-DD date as YYYYMMDD. */
|
|
@@ -296,7 +292,7 @@ const usDate = (name, value) => {
|
|
|
296
292
|
/** A result count that is a whole number from `min` to `max`; refuses any other. */
|
|
297
293
|
const bounded = (engine, value, min, max) => {
|
|
298
294
|
if (value === void 0) return;
|
|
299
|
-
if (
|
|
295
|
+
if (!Number.isInteger(value) || value < min || value > max) throw invalidRequest(provider, `${engine} limit must be a whole number from ${min} to ${max}`);
|
|
300
296
|
return value;
|
|
301
297
|
};
|
|
302
298
|
/** The code for the recency window the caller set; undefined when it names none. */
|
|
@@ -351,10 +347,10 @@ const mapsOrigin = (o) => {
|
|
|
351
347
|
if (origin && o.zoom === void 0) throw invalidRequest(provider, "google_maps needs zoom with location or latitude and longitude");
|
|
352
348
|
if (!origin && o.zoom !== void 0) throw invalidRequest(provider, "google_maps zoom needs location or latitude and longitude");
|
|
353
349
|
return {
|
|
354
|
-
lat:
|
|
355
|
-
location:
|
|
356
|
-
lon:
|
|
357
|
-
z:
|
|
350
|
+
lat: o.latitude,
|
|
351
|
+
location: o.location,
|
|
352
|
+
lon: o.longitude,
|
|
353
|
+
z: o.zoom
|
|
358
354
|
};
|
|
359
355
|
};
|
|
360
356
|
// src/adapters/serpapi/engines.ts
|
|
@@ -379,12 +375,12 @@ const ENGINES = {
|
|
|
379
375
|
params: (o) => {
|
|
380
376
|
refuseTogether(provider, o, "country", "market");
|
|
381
377
|
return {
|
|
382
|
-
cc:
|
|
383
|
-
device:
|
|
384
|
-
first:
|
|
385
|
-
location:
|
|
386
|
-
mkt:
|
|
387
|
-
safeSearch:
|
|
378
|
+
cc: o.country?.toLowerCase(),
|
|
379
|
+
device: o.device,
|
|
380
|
+
first: o.first,
|
|
381
|
+
location: o.location,
|
|
382
|
+
mkt: o.market,
|
|
383
|
+
safeSearch: o.safeSearch
|
|
388
384
|
};
|
|
389
385
|
},
|
|
390
386
|
results: webResults
|
|
@@ -403,10 +399,10 @@ const ENGINES = {
|
|
|
403
399
|
],
|
|
404
400
|
params: (o) => ({
|
|
405
401
|
df: duckDuckGoFilter(o),
|
|
406
|
-
kl:
|
|
402
|
+
kl: o.region,
|
|
407
403
|
m: bounded("duckduckgo", o.limit, 1, 50),
|
|
408
|
-
safe:
|
|
409
|
-
start:
|
|
404
|
+
safe: o.safe,
|
|
405
|
+
start: o.start
|
|
410
406
|
}),
|
|
411
407
|
results: webResults
|
|
412
408
|
},
|
|
@@ -428,12 +424,12 @@ const ENGINES = {
|
|
|
428
424
|
"noCache"
|
|
429
425
|
],
|
|
430
426
|
params: (o) => ({
|
|
431
|
-
device:
|
|
427
|
+
device: o.device,
|
|
432
428
|
gl: googleCountry(o.country),
|
|
433
|
-
hl:
|
|
434
|
-
location:
|
|
435
|
-
safe:
|
|
436
|
-
start:
|
|
429
|
+
hl: o.language,
|
|
430
|
+
location: o.location,
|
|
431
|
+
safe: o.safe,
|
|
432
|
+
start: o.start,
|
|
437
433
|
tbs: googleTbs(o)
|
|
438
434
|
}),
|
|
439
435
|
results: webResults
|
|
@@ -453,10 +449,10 @@ const ENGINES = {
|
|
|
453
449
|
"noCache"
|
|
454
450
|
],
|
|
455
451
|
params: (o) => ({
|
|
452
|
+
hl: o.language,
|
|
456
453
|
type: "search",
|
|
457
|
-
hl: str(o.language),
|
|
458
454
|
...mapsOrigin(o),
|
|
459
|
-
start:
|
|
455
|
+
start: o.start
|
|
460
456
|
}),
|
|
461
457
|
results: placeResults
|
|
462
458
|
},
|
|
@@ -469,7 +465,7 @@ const ENGINES = {
|
|
|
469
465
|
],
|
|
470
466
|
params: (o) => ({
|
|
471
467
|
gl: googleCountry(o.country),
|
|
472
|
-
hl:
|
|
468
|
+
hl: o.language
|
|
473
469
|
}),
|
|
474
470
|
results: newsResults
|
|
475
471
|
},
|
|
@@ -488,13 +484,13 @@ const ENGINES = {
|
|
|
488
484
|
],
|
|
489
485
|
params: (o) => ({
|
|
490
486
|
after: patentDate("publishedAfter", o.publishedAfter),
|
|
491
|
-
assignee:
|
|
487
|
+
assignee: o.assignee,
|
|
492
488
|
before: patentDate("publishedBefore", o.publishedBefore),
|
|
493
|
-
inventor:
|
|
489
|
+
inventor: o.inventor,
|
|
494
490
|
num: bounded("google_patents", o.limit, 10, 100),
|
|
495
|
-
page:
|
|
496
|
-
sort:
|
|
497
|
-
status:
|
|
491
|
+
page: o.page,
|
|
492
|
+
sort: o.sort,
|
|
493
|
+
status: o.status
|
|
498
494
|
}),
|
|
499
495
|
results: patentResults
|
|
500
496
|
},
|
|
@@ -510,12 +506,12 @@ const ENGINES = {
|
|
|
510
506
|
"noCache"
|
|
511
507
|
],
|
|
512
508
|
params: (o) => ({
|
|
513
|
-
as_yhi:
|
|
514
|
-
as_ylo:
|
|
515
|
-
hl:
|
|
509
|
+
as_yhi: o.yearTo,
|
|
510
|
+
as_ylo: o.yearFrom,
|
|
511
|
+
hl: o.language,
|
|
516
512
|
num: bounded("google_scholar", o.limit, 1, 20),
|
|
517
|
-
safe:
|
|
518
|
-
start:
|
|
513
|
+
safe: o.safe,
|
|
514
|
+
start: o.start
|
|
519
515
|
}),
|
|
520
516
|
results: scholarResults
|
|
521
517
|
},
|
|
@@ -536,17 +532,17 @@ const ENGINES = {
|
|
|
536
532
|
"noCache"
|
|
537
533
|
],
|
|
538
534
|
params: (o) => ({
|
|
539
|
-
device:
|
|
535
|
+
device: o.device,
|
|
540
536
|
free_shipping: o.freeShipping === true ? true : void 0,
|
|
541
537
|
gl: googleCountry(o.country),
|
|
542
|
-
hl:
|
|
543
|
-
location:
|
|
544
|
-
max_price:
|
|
545
|
-
min_price:
|
|
538
|
+
hl: o.language,
|
|
539
|
+
location: o.location,
|
|
540
|
+
max_price: o.maxPrice,
|
|
541
|
+
min_price: o.minPrice,
|
|
546
542
|
on_sale: o.onSale === true ? true : void 0,
|
|
547
|
-
safe:
|
|
543
|
+
safe: o.safe,
|
|
548
544
|
small_business: o.smallBusiness === true ? true : void 0,
|
|
549
|
-
sort_by:
|
|
545
|
+
sort_by: o.sortBy
|
|
550
546
|
}),
|
|
551
547
|
results: shoppingResults
|
|
552
548
|
}
|
|
@@ -575,7 +571,7 @@ const REVERSE_ENGINES = {
|
|
|
575
571
|
imageParam: "image_url",
|
|
576
572
|
params: (o) => ({
|
|
577
573
|
count: bounded("bing_reverse_image", o.limit, 1, 150),
|
|
578
|
-
mkt:
|
|
574
|
+
mkt: o.market
|
|
579
575
|
}),
|
|
580
576
|
results: bingMatches
|
|
581
577
|
},
|
|
@@ -589,9 +585,9 @@ const REVERSE_ENGINES = {
|
|
|
589
585
|
],
|
|
590
586
|
imageParam: "url",
|
|
591
587
|
params: (o) => ({
|
|
592
|
-
country:
|
|
593
|
-
hl:
|
|
594
|
-
q:
|
|
588
|
+
country: o.country?.toLowerCase(),
|
|
589
|
+
hl: o.language,
|
|
590
|
+
q: o.query,
|
|
595
591
|
type: lensType(o)
|
|
596
592
|
}),
|
|
597
593
|
results: lensMatches
|
|
@@ -606,8 +602,8 @@ const REVERSE_ENGINES = {
|
|
|
606
602
|
imageParam: "image_url",
|
|
607
603
|
params: (o) => ({
|
|
608
604
|
gl: googleCountry(o.country),
|
|
609
|
-
hl:
|
|
610
|
-
q:
|
|
605
|
+
hl: o.language,
|
|
606
|
+
q: o.query
|
|
611
607
|
}),
|
|
612
608
|
results: googleReverseMatches
|
|
613
609
|
},
|
|
@@ -641,15 +637,15 @@ const IMAGE_OPTIONS = [
|
|
|
641
637
|
* undefined passes.
|
|
642
638
|
*/
|
|
643
639
|
const onlyAllowed = (engine, options, allowed) => {
|
|
644
|
-
const refused = Object.
|
|
640
|
+
const refused = Object.entries(options).filter(([name, value]) => value !== void 0 && !allowed.includes(name)).map(([name]) => name);
|
|
645
641
|
if (refused.length > 0) throw invalidRequest(provider, `${engine} does not accept ${refused.join(", ")}`);
|
|
646
642
|
};
|
|
647
643
|
// src/adapters/serpapi/response.ts
|
|
648
644
|
/** SerpApi's `error` note: its text when it is a non-empty string, and the warnings it amounts to. */
|
|
649
|
-
const ErrorNote = z.union([z.pipe(nonEmptyString, z.transform((message) => ({
|
|
645
|
+
const ErrorNote = z$1.union([z$1.pipe(nonEmptyString, z$1.transform((message) => ({
|
|
650
646
|
message,
|
|
651
647
|
warnings: [message]
|
|
652
|
-
}))), z.pipe(warnings, z.transform((entries) => ({
|
|
648
|
+
}))), z$1.pipe(warnings, z$1.transform((entries) => ({
|
|
653
649
|
message: void 0,
|
|
654
650
|
warnings: entries
|
|
655
651
|
})))]);
|
|
@@ -672,7 +668,7 @@ const storyShape = {
|
|
|
672
668
|
iso_date: optionalString,
|
|
673
669
|
link: optionalString,
|
|
674
670
|
source: looseObject({
|
|
675
|
-
authors: arrayOf(z.string()),
|
|
671
|
+
authors: arrayOf(z$1.string()),
|
|
676
672
|
icon: optionalString
|
|
677
673
|
}),
|
|
678
674
|
thumbnail: optionalString,
|
|
@@ -761,7 +757,7 @@ const BingPage = looseObject({
|
|
|
761
757
|
width: optionalNumber
|
|
762
758
|
});
|
|
763
759
|
/** An image given as a URL, or as Yandex gives it: an object with `link` and the size. Either way, an object. */
|
|
764
|
-
const Pictured = z.union([z.pipe(nonEmptyString, z.transform((link) => ({ link }))), looseObject({
|
|
760
|
+
const Pictured = z$1.union([z$1.pipe(nonEmptyString, z$1.transform((link) => ({ link }))), looseObject({
|
|
765
761
|
height: optionalNumber,
|
|
766
762
|
link: optionalString,
|
|
767
763
|
width: optionalNumber
|
|
@@ -780,7 +776,7 @@ const ImagePage = looseObject({
|
|
|
780
776
|
});
|
|
781
777
|
/** A `/search` response for any engine; each engine reads only its own sections. */
|
|
782
778
|
const SearchResponse = looseObject({
|
|
783
|
-
answer_box: z.union([z.pipe(z.optional(z.null()), z.transform(() => void 0)), AnswerBox]),
|
|
779
|
+
answer_box: z$1.union([z$1.pipe(z$1.optional(z$1.null()), z$1.transform(() => void 0)), AnswerBox]),
|
|
784
780
|
answer_box_list: arrayOf(AnswerBox),
|
|
785
781
|
categorized_shopping_results: arrayOf(looseObject({ shopping_results: arrayOf(ShoppingItem) })),
|
|
786
782
|
error: ErrorNote,
|
|
@@ -810,8 +806,120 @@ const AccountResponse = looseObject({
|
|
|
810
806
|
this_month_usage: optionalNumber
|
|
811
807
|
});
|
|
812
808
|
// src/adapters/serpapi.ts
|
|
809
|
+
/** One `/search` call: the response as received, and its parsed body. A failed search throws. */
|
|
810
|
+
const searchCall = async (raw, call, query) => {
|
|
811
|
+
const response = await raw.request("GET", "/search", {
|
|
812
|
+
...call,
|
|
813
|
+
query
|
|
814
|
+
});
|
|
815
|
+
const body = z$1.parse(SearchResponse, response);
|
|
816
|
+
const failure = searchFailure(body);
|
|
817
|
+
if (failure !== void 0) throw new FieldworkError("ProviderError", failure, {
|
|
818
|
+
cause: response,
|
|
819
|
+
provider,
|
|
820
|
+
retryable: true
|
|
821
|
+
});
|
|
822
|
+
return {
|
|
823
|
+
body,
|
|
824
|
+
response
|
|
825
|
+
};
|
|
826
|
+
};
|
|
827
|
+
/** `search` and `images`: one engine query each. */
|
|
828
|
+
const searching = (raw) => ({
|
|
829
|
+
async images(query, options = {}) {
|
|
830
|
+
const { call, rest: o } = callSettings(options);
|
|
831
|
+
onlyAllowed("google_images", o, IMAGE_OPTIONS);
|
|
832
|
+
refuseTogether(provider, o, "recency", "publishedAfter");
|
|
833
|
+
refuseTogether(provider, o, "recency", "publishedBefore");
|
|
834
|
+
const { body, response } = await searchCall(raw, call, compact({
|
|
835
|
+
device: o.device,
|
|
836
|
+
end_date: compactDate("publishedBefore", o.publishedBefore),
|
|
837
|
+
engine: "google_images",
|
|
838
|
+
gl: googleCountry(o.country),
|
|
839
|
+
hl: o.language,
|
|
840
|
+
ijn: o.page,
|
|
841
|
+
image_color: o.color,
|
|
842
|
+
image_type: o.imageType,
|
|
843
|
+
imgar: o.aspectRatio,
|
|
844
|
+
imgsz: o.size,
|
|
845
|
+
licenses: o.license,
|
|
846
|
+
location: o.location,
|
|
847
|
+
no_cache: o.noCache === true ? true : void 0,
|
|
848
|
+
period_unit: o.recency === void 0 ? void 0 : RECENCY_CODE[o.recency],
|
|
849
|
+
q: query,
|
|
850
|
+
safe: o.safe,
|
|
851
|
+
start_date: compactDate("publishedAfter", o.publishedAfter)
|
|
852
|
+
}));
|
|
853
|
+
return compact({
|
|
854
|
+
meta: searchMeta(body),
|
|
855
|
+
raw: response,
|
|
856
|
+
results: imageResults(body)
|
|
857
|
+
});
|
|
858
|
+
},
|
|
859
|
+
async search(query, options = {}) {
|
|
860
|
+
const { call, rest } = callSettings(options);
|
|
861
|
+
const { engine = "google", ...chosen } = rest;
|
|
862
|
+
const o = chosen;
|
|
863
|
+
if (!Object.hasOwn(ENGINES, engine)) throw invalidRequest(provider, `does not offer the engine "${engine}"`);
|
|
864
|
+
const spec = ENGINES[engine];
|
|
865
|
+
onlyAllowed(engine, o, spec.allowed);
|
|
866
|
+
const { body, response } = await searchCall(raw, call, compact({
|
|
867
|
+
engine,
|
|
868
|
+
q: query,
|
|
869
|
+
...spec.params(o),
|
|
870
|
+
no_cache: o.noCache === true ? true : void 0
|
|
871
|
+
}));
|
|
872
|
+
return compact({
|
|
873
|
+
answer: engine === "google" ? answerText(body) : void 0,
|
|
874
|
+
meta: searchMeta(body),
|
|
875
|
+
raw: response,
|
|
876
|
+
results: spec.results(body)
|
|
877
|
+
});
|
|
878
|
+
}
|
|
879
|
+
});
|
|
880
|
+
/** `reverseImage`: the pages an image appears on, through Google Lens. */
|
|
881
|
+
const matching = (raw) => ({ async reverseImage(imageUrl, options = {}) {
|
|
882
|
+
const { call, rest } = callSettings(options);
|
|
883
|
+
const { engine = "google_lens", ...chosen } = rest;
|
|
884
|
+
const o = chosen;
|
|
885
|
+
if (!Object.hasOwn(REVERSE_ENGINES, engine)) throw invalidRequest(provider, `does not offer the reverse image engine "${engine}"`);
|
|
886
|
+
if (!URL.canParse(imageUrl) || !/^https?:$/u.test(new URL(imageUrl).protocol)) throw invalidRequest(provider, "imageUrl must be an http or https URL");
|
|
887
|
+
const spec = REVERSE_ENGINES[engine];
|
|
888
|
+
onlyAllowed(engine, o, spec.allowed);
|
|
889
|
+
const { body, response } = await searchCall(raw, call, compact({
|
|
890
|
+
engine,
|
|
891
|
+
[spec.imageParam]: imageUrl,
|
|
892
|
+
...spec.params(o),
|
|
893
|
+
no_cache: o.noCache === true ? true : void 0
|
|
894
|
+
}));
|
|
895
|
+
const results = spec.results(body);
|
|
896
|
+
return compact({
|
|
897
|
+
meta: reverseImageMeta(body, results),
|
|
898
|
+
raw: response,
|
|
899
|
+
results
|
|
900
|
+
});
|
|
901
|
+
} });
|
|
902
|
+
/** `usage`: the account's searches this month. */
|
|
903
|
+
const accounting = (raw) => ({ async usage(options = {}) {
|
|
904
|
+
const { call } = callSettings(options);
|
|
905
|
+
const response = await raw.request("GET", "/account", call);
|
|
906
|
+
const body = z$1.parse(AccountResponse, response);
|
|
907
|
+
const fields = z$1.safeParse(z$1.record(z$1.string(), z$1.unknown()), response);
|
|
908
|
+
const safe = fields.success ? Object.fromEntries(Object.entries(fields.data).filter(([name]) => name !== "api_key")) : response;
|
|
909
|
+
const breakdown = {};
|
|
910
|
+
if (body.this_hour_searches !== void 0) breakdown.this_hour_searches = body.this_hour_searches;
|
|
911
|
+
if (body.last_hour_searches !== void 0) breakdown.last_hour_searches = body.last_hour_searches;
|
|
912
|
+
return compact({
|
|
913
|
+
breakdown: Object.keys(breakdown).length > 0 ? breakdown : void 0,
|
|
914
|
+
limit: body.searches_per_month,
|
|
915
|
+
periodEnd: body.plan_renewal_date,
|
|
916
|
+
raw: safe,
|
|
917
|
+
remaining: body.plan_searches_left,
|
|
918
|
+
used: body.this_month_usage
|
|
919
|
+
});
|
|
920
|
+
} });
|
|
813
921
|
/**
|
|
814
|
-
* An adapter for SerpApi: search, images, reverse image and usage. Pass it to `
|
|
922
|
+
* An adapter for SerpApi: search, images, reverse image and usage. Pass it to `fieldwork({ adapter })`.
|
|
815
923
|
*
|
|
816
924
|
* Throws `Unauthorized` when neither `apiKey` nor `SERPAPI_API_KEY` is set.
|
|
817
925
|
*/
|
|
@@ -829,117 +937,14 @@ const serpapi = (config = {}) => {
|
|
|
829
937
|
provider,
|
|
830
938
|
retries: config.retries
|
|
831
939
|
});
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
query
|
|
837
|
-
});
|
|
838
|
-
const body = z.parse(SearchResponse, response);
|
|
839
|
-
const failure = searchFailure(body);
|
|
840
|
-
if (failure !== void 0) throw new FieldworkError("ProviderError", failure, {
|
|
841
|
-
cause: response,
|
|
842
|
-
provider,
|
|
843
|
-
retryable: true
|
|
844
|
-
});
|
|
845
|
-
return {
|
|
846
|
-
body,
|
|
847
|
-
response
|
|
848
|
-
};
|
|
940
|
+
const operations = {
|
|
941
|
+
...accounting(raw),
|
|
942
|
+
...matching(raw),
|
|
943
|
+
...searching(raw)
|
|
849
944
|
};
|
|
850
945
|
return defineAdapter({
|
|
851
946
|
name: provider,
|
|
852
|
-
operations
|
|
853
|
-
async images(query, options) {
|
|
854
|
-
const { call, rest: o } = callSettings(options);
|
|
855
|
-
onlyAllowed("google_images", o, IMAGE_OPTIONS);
|
|
856
|
-
refuseTogether(provider, o, "recency", "publishedAfter");
|
|
857
|
-
refuseTogether(provider, o, "recency", "publishedBefore");
|
|
858
|
-
const { body, response } = await searchCall(call, compact({
|
|
859
|
-
device: o.device,
|
|
860
|
-
end_date: compactDate("publishedBefore", o.publishedBefore),
|
|
861
|
-
engine: "google_images",
|
|
862
|
-
gl: googleCountry(o.country),
|
|
863
|
-
hl: o.language,
|
|
864
|
-
ijn: o.page,
|
|
865
|
-
image_color: o.color,
|
|
866
|
-
image_type: o.imageType,
|
|
867
|
-
imgar: o.aspectRatio,
|
|
868
|
-
imgsz: o.size,
|
|
869
|
-
licenses: o.license,
|
|
870
|
-
location: o.location,
|
|
871
|
-
no_cache: o.noCache === true ? true : void 0,
|
|
872
|
-
period_unit: o.recency === void 0 ? void 0 : RECENCY_CODE[o.recency],
|
|
873
|
-
q: query,
|
|
874
|
-
safe: o.safe,
|
|
875
|
-
start_date: compactDate("publishedAfter", o.publishedAfter)
|
|
876
|
-
}));
|
|
877
|
-
return compact({
|
|
878
|
-
meta: searchMeta(body),
|
|
879
|
-
raw: response,
|
|
880
|
-
results: imageResults(body)
|
|
881
|
-
});
|
|
882
|
-
},
|
|
883
|
-
async reverseImage(imageUrl, options) {
|
|
884
|
-
const { call, rest } = callSettings(options);
|
|
885
|
-
const { engine = "google_lens", ...chosen } = rest;
|
|
886
|
-
const o = { ...chosen };
|
|
887
|
-
if (!Object.hasOwn(REVERSE_ENGINES, engine)) throw invalidRequest(provider, `does not offer the reverse image engine "${engine}"`);
|
|
888
|
-
if (!URL.canParse(imageUrl) || !/^https?:$/u.test(new URL(imageUrl).protocol)) throw invalidRequest(provider, "imageUrl must be an http or https URL");
|
|
889
|
-
const spec = REVERSE_ENGINES[engine];
|
|
890
|
-
onlyAllowed(engine, o, spec.allowed);
|
|
891
|
-
const { body, response } = await searchCall(call, compact({
|
|
892
|
-
engine,
|
|
893
|
-
[spec.imageParam]: imageUrl,
|
|
894
|
-
...spec.params(o),
|
|
895
|
-
no_cache: o.noCache === true ? true : void 0
|
|
896
|
-
}));
|
|
897
|
-
const results = spec.results(body);
|
|
898
|
-
return compact({
|
|
899
|
-
meta: reverseImageMeta(body, results),
|
|
900
|
-
raw: response,
|
|
901
|
-
results
|
|
902
|
-
});
|
|
903
|
-
},
|
|
904
|
-
async search(query, options) {
|
|
905
|
-
const { call, rest } = callSettings(options);
|
|
906
|
-
const { engine = "google", ...chosen } = rest;
|
|
907
|
-
const o = { ...chosen };
|
|
908
|
-
if (!Object.hasOwn(ENGINES, engine)) throw invalidRequest(provider, `does not offer the engine "${engine}"`);
|
|
909
|
-
const spec = ENGINES[engine];
|
|
910
|
-
onlyAllowed(engine, o, spec.allowed);
|
|
911
|
-
const { body, response } = await searchCall(call, compact({
|
|
912
|
-
engine,
|
|
913
|
-
q: query,
|
|
914
|
-
...spec.params(o),
|
|
915
|
-
no_cache: o.noCache === true ? true : void 0
|
|
916
|
-
}));
|
|
917
|
-
return compact({
|
|
918
|
-
answer: engine === "google" ? answerText(body) : void 0,
|
|
919
|
-
meta: searchMeta(body),
|
|
920
|
-
raw: response,
|
|
921
|
-
results: spec.results(body)
|
|
922
|
-
});
|
|
923
|
-
},
|
|
924
|
-
async usage(options) {
|
|
925
|
-
const { call } = callSettings(options);
|
|
926
|
-
const response = await raw.request("GET", "/account", call);
|
|
927
|
-
const body = z.parse(AccountResponse, response);
|
|
928
|
-
const fields = z.safeParse(z.record(z.string(), z.unknown()), response);
|
|
929
|
-
const safe = fields.success ? Object.fromEntries(Object.entries(fields.data).filter(([name]) => name !== "api_key")) : response;
|
|
930
|
-
const breakdown = {};
|
|
931
|
-
if (body.this_hour_searches !== void 0) breakdown.this_hour_searches = body.this_hour_searches;
|
|
932
|
-
if (body.last_hour_searches !== void 0) breakdown.last_hour_searches = body.last_hour_searches;
|
|
933
|
-
return compact({
|
|
934
|
-
breakdown: Object.keys(breakdown).length > 0 ? breakdown : void 0,
|
|
935
|
-
limit: body.searches_per_month,
|
|
936
|
-
periodEnd: body.plan_renewal_date,
|
|
937
|
-
raw: safe,
|
|
938
|
-
remaining: body.plan_searches_left,
|
|
939
|
-
used: body.this_month_usage
|
|
940
|
-
});
|
|
941
|
-
}
|
|
942
|
-
},
|
|
947
|
+
operations,
|
|
943
948
|
raw
|
|
944
949
|
});
|
|
945
950
|
};
|
package/dist/tavily.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { t as OutputSchema } from "./schema-
|
|
1
|
+
import { a as RawClient, t as Adapter } from "./adapter-J2yatZJ0.js";
|
|
2
|
+
import { t as OutputSchema } from "./schema-C9xzRkKJ.js";
|
|
3
|
+
import { B as Page, J as Recency, Q as SearchResponse, V as PageBatch, Y as Research, a as Answer, j as ImageSearch, l as CallOptions, p as Crawl, r as AccountUsage, tt as SiteMap } from "./errors-gMtejKHc.js";
|
|
3
4
|
/** Settings for the Tavily adapter. */
|
|
4
5
|
interface TavilyConfig {
|
|
5
6
|
/** Defaults to the TAVILY_API_KEY environment variable. */
|
|
@@ -197,7 +198,7 @@ interface TavilyOperations {
|
|
|
197
198
|
usage: (options?: CallOptions) => Promise<AccountUsage>;
|
|
198
199
|
}
|
|
199
200
|
/**
|
|
200
|
-
* An adapter for Tavily: search, images, read, readMany, map, crawl, answer, research and usage. Pass it to `
|
|
201
|
+
* An adapter for Tavily: search, images, read, readMany, map, crawl, answer, research and usage. Pass it to `fieldwork({ adapter })`.
|
|
201
202
|
*
|
|
202
203
|
* Throws `Unauthorized` when neither `apiKey` nor `TAVILY_API_KEY` is set.
|
|
203
204
|
*/
|